From b8b8d703e352a9ddb5436fdc9310bf6c70a951ce Mon Sep 17 00:00:00 2001 From: Skarm Date: Fri, 6 Nov 2026 23:26:29 +0500 Subject: [PATCH 1/4] feat: expand SAML protocols and strengthen XML security validation - Add encrypted SAML claims, logout, NameID management, Artifact/SOAP and HoK ECP - Add authority queries, assertion retrieval and NameID Mapping with replay protection - Expose bounded nonvalidating XML parsing and fix XPath/XSLT processing edge cases - Tighten cryptographic and evidence validation; add explicit legacy RSA policies - Expand pinned interoperability checks with OpenSAML, Shibboleth, OpenAM, GSA and DSS - Consolidate shared constants and update documentation and comments --- .github/workflows/ci.yml | 4 +- README.md | 24 +- build/Makefile | 10 +- build/README.md | 20 +- build/dss.sh | 14 +- build/interop-inputs.txt | 24 + build/interop.Dockerfile | 52 +- build/interop.sh | 50 + build/openam-nameid-order.patch | 6 + build/patch-openam.sh | 17 + build/prepare-interop.sh | 28 + c14n/c14n_test.go | 8 +- c14n/doc.go | 9 +- c14n/nodeset_test.go | 8 +- c14n/testdata/normative-vectors.json | 2 +- cmd/xmlsec/app.go | 21 +- cmd/xmlsec/application_test.go | 260 +- cmd/xmlsec/cms_commands.go | 16 +- cmd/xmlsec/core.go | 51 +- cmd/xmlsec/detached.go | 23 +- cmd/xmlsec/detached_test.go | 8 +- cmd/xmlsec/encryption.go | 64 +- cmd/xmlsec/evidence_commands_test.go | 49 + cmd/xmlsec/execution.go | 93 +- cmd/xmlsec/key_commands.go | 34 +- cmd/xmlsec/keyinfo_commands.go | 11 +- cmd/xmlsec/keys.go | 27 +- cmd/xmlsec/keys_test.go | 134 +- cmd/xmlsec/main.go | 12 +- cmd/xmlsec/main_test.go | 8 +- cmd/xmlsec/pki_commands.go | 20 +- cmd/xmlsec/process_test.go | 6 +- cmd/xmlsec/saml_commands.go | 27 +- cmd/xmlsec/shared_constants.go | 33 +- cmd/xmlsec/signatures.go | 37 +- cmd/xmlsec/timestamp_commands.go | 15 +- cmd/xmlsec/transforms.go | 61 +- cmd/xmlsec/trust.go | 16 +- cmd/xmlsec/xades_commands.go | 43 +- cms/attributes.go | 160 +- cms/ber.go | 42 +- cms/cms.go | 30 +- cms/cms_test.go | 302 +- cms/parse.go | 103 +- cms/sign.go | 41 +- cms/trust.go | 23 +- cms/verify.go | 48 +- docs/architecture.md | 16 +- docs/compatibility.md | 48 +- docs/conformance.json | 16273 ++++++++++++---- docs/language-suites.md | 6 +- docs/saml-authority.md | 158 + docs/saml.md | 245 + docs/standards.md | 9 + docs/streaming.md | 12 +- docs/testing.md | 69 +- docs/transforms.md | 20 +- docs/usage.md | 39 +- document/doc.go | 23 +- document/document.go | 8 + document/document_test.go | 16 + document/parse_options.go | 98 + document/parse_options_test.go | 245 + .../FuzzNonValidatingXML/c0b432ce1eb33f95 | 2 + evidence/collect/http.go | 74 +- evidence/collect/http_test.go | 29 + evidence/evidence.go | 31 +- evidence/store.go | 60 +- examples/algorithms/main.go | 12 +- examples/encryption/main.go | 8 +- examples/internal/democert/cert.go | 15 +- examples/keyinfo/main.go | 3 +- examples/manifest/main.go | 8 +- examples/redirect/main.go | 10 +- examples/sso/main.go | 13 +- examples/streaming/main.go | 18 +- examples/trust/main.go | 18 +- internal/ber/ber.go | 81 +- internal/ber/ber_test.go | 36 + internal/der/der.go | 173 +- internal/der/validated.go | 16 +- internal/historical/crypto.go | 33 +- internal/historical/graph.go | 70 +- internal/historical/model.go | 178 +- internal/historical/order.go | 5 +- internal/historical/past.go | 26 +- internal/historical/workflow.go | 133 +- internal/historical/workflow_internal_test.go | 383 + .../workflow_policy_internal_test.go | 9 + internal/historicalproof/proof.go | 49 +- internal/historicalproof/proof_test.go | 12 +- internal/lexicon/catalog.go | 6 + internal/lexicon/errors.go | 8 + internal/lexicon/functions.go | 4 +- internal/lexicon/security.go | 173 +- internal/lexicon/serialization.go | 62 +- internal/lexicon/xml.go | 61 +- internal/lexicon/xslt.go | 53 +- internal/normalization/normalization.go | 303 +- internal/normalization/normalization_test.go | 46 + internal/normalization/reuse.go | 74 + internal/normalization/reuse_test.go | 66 + internal/packageversion/version.go | 22 +- internal/signature/legacy_dsa.go | 15 +- internal/signature/signature.go | 99 +- internal/streamio/base64.go | 6 +- internal/streamio/reader.go | 45 +- internal/streamio/writer.go | 26 +- internal/streamio/writer_test.go | 42 +- internal/testutil/files.go | 6 +- internal/testutil/fips.go | 17 +- internal/testutil/keys.go | 6 +- internal/testutil/testcert/fixtures.go | 6 +- internal/testutil/testcert/testcert.go | 33 +- internal/transformxml/transforms.go | 64 +- internal/tree/c14n.go | 57 +- internal/tree/check_limits.go | 7 +- internal/tree/constants.go | 19 +- internal/tree/doctype.go | 60 + internal/tree/dtd.go | 154 +- internal/tree/dtd_content.go | 44 +- internal/tree/dtd_subset.go | 156 +- internal/tree/encoding.go | 16 +- internal/tree/filter.go | 11 +- internal/tree/filter_test.go | 56 +- internal/tree/fragment.go | 4 +- internal/tree/metadata.go | 1 + internal/tree/metadata_test.go | 63 + internal/tree/model.go | 71 +- internal/tree/namespace.go | 7 +- internal/tree/nodeset.go | 36 +- internal/tree/normalize_test.go | 46 +- internal/tree/parse.go | 50 +- internal/tree/parse_options.go | 11 + internal/tree/read.go | 32 +- internal/tree/reader_error_test.go | 30 + internal/tree/records.go | 8 +- internal/tree/reparse.go | 16 +- internal/tree/scope.go | 109 + internal/tree/scope_test.go | 145 + internal/tree/stream_xml.go | 19 +- internal/tree/token.go | 79 +- internal/tree/uri_test.go | 106 +- internal/tree/write.go | 33 +- internal/uriref/uri.go | 9 +- internal/x509cert/cert.go | 174 +- internal/x509cert/checked.go | 2 +- internal/x509cert/names_test.go | 18 +- internal/x509crl/crl.go | 144 +- internal/x509crl/crl_internal_test.go | 10 +- internal/x509name/cache.go | 17 +- internal/x509name/name.go | 42 +- internal/x509name/name_internal_test.go | 8 +- internal/x509name/prep.go | 48 +- internal/xmlchar/xmlchar.go | 4 +- internal/xmloutput/doctype.go | 10 +- internal/xmltime/time.go | 49 +- keyinfo/build.go | 95 +- keyinfo/build_test.go | 42 + keyinfo/constants.go | 40 +- keyinfo/doc.go | 2 +- keyinfo/parse.go | 64 +- keyinfo/policy.go | 2 +- keyinfo/policy_test.go | 18 +- keyinfo/references.go | 50 +- keyinfo/references_test.go | 113 +- keyinfo/retrieval.go | 35 +- keyinfo/types.go | 153 +- keyinfo/x509.go | 54 +- keys/limits.go | 54 +- keys/parse.go | 136 +- keys/parse_test.go | 123 +- keys/password.go | 33 +- keys/password_internal_test.go | 202 +- keys/password_test.go | 184 +- keys/pbes2.go | 65 +- keys/pkcs12.go | 267 +- keys/pkcs12_forms.go | 33 +- keys/pkcs12_mac.go | 58 +- keys/pkcs8.go | 17 +- keys/pkcs8_forms.go | 6 +- keys/private_structure.go | 61 +- pki/algorithms.go | 9 +- pki/clone.go | 4 +- pki/crl.go | 2 +- pki/crl_combine.go | 13 +- pki/crl_names.go | 33 +- pki/crl_parse.go | 75 +- pki/crl_scope.go | 127 +- pki/crl_verify.go | 68 +- pki/evidence.go | 88 +- pki/evidence_sources.go | 9 +- pki/historical.go | 98 +- pki/historical_current.go | 22 +- pki/historical_evidence.go | 152 +- pki/historical_past.go | 49 +- pki/historical_path.go | 31 +- pki/name_views.go | 2 +- pki/ocsp.go | 83 +- pki/ocsp_authority.go | 12 +- pki/ocsp_issue.go | 39 +- pki/ocsp_parse.go | 86 +- pki/ocsp_test.go | 371 +- pki/ocsp_verify.go | 43 +- pki/pki.go | 400 +- pki/pki_test.go | 164 +- pki/standards_test.go | 430 +- reference/data.go | 45 +- reference/extensions.go | 38 +- reference/extensions_test.go | 34 +- reference/fingerprint.go | 3 +- reference/pipeline.go | 22 +- reference/policy.go | 21 +- reference/reader.go | 6 +- reference/same_document.go | 117 +- resource/resource.go | 31 +- resource/resource_test.go | 122 +- saml/artifact/artifact.go | 428 + saml/artifact/artifact_test.go | 182 + saml/artifact/cancellation_test.go | 102 + saml/artifact/performance_test.go | 80 + saml/artifact/store_index_test.go | 164 + saml/authority/authority.go | 388 + saml/authority/authority_test.go | 363 + saml/authority/client.go | 315 + saml/authority/uri.go | 184 + saml/binding/artifact.go | 200 + saml/binding/artifact_test.go | 99 + saml/binding/doc.go | 13 +- saml/binding/post.go | 81 +- saml/binding/post_reader.go | 18 +- saml/binding/redirect.go | 120 +- saml/binding/redirect_test.go | 48 +- saml/binding/soap.go | 612 + saml/binding/soap_http_contract_test.go | 96 + saml/binding/soap_test.go | 167 + saml/binding/uri.go | 182 + saml/binding/uri_test.go | 117 + saml/ecp/channel.go | 348 + saml/ecp/channel_test.go | 165 + saml/ecp/ecp.go | 959 + saml/ecp/ecp_test.go | 201 + saml/ecp/hok.go | 289 + saml/ecp/hok_test.go | 53 + saml/ecp/http.go | 143 + saml/ecp/roles_test.go | 268 + saml/idp/doc.go | 11 +- saml/idp/idp.go | 448 +- saml/idp/policy_test.go | 88 +- saml/internal/expiry/index.go | 145 + saml/internal/expiry/index_test.go | 219 + saml/internal/expiry/queue.go | 18 +- saml/internal/profile/values.go | 108 +- saml/internal/profile/values_test.go | 4 +- saml/internal/xmlsig/sign.go | 264 +- saml/internal/xmlsig/sign_test.go | 30 +- saml/internal/xmlutil/schema_test.go | 4 +- saml/internal/xmlutil/xml.go | 6 +- saml/metadata/authority.go | 39 + saml/metadata/authority_test.go | 60 + saml/metadata/build.go | 90 +- saml/metadata/doc.go | 13 +- saml/metadata/ecp.go | 196 + saml/metadata/ecp_test.go | 133 + saml/metadata/index_test.go | 4 +- saml/metadata/metadata.go | 270 +- saml/metadata/metadata_test.go | 89 + saml/metadata/shared_constants.go | 29 +- saml/proof/proof.go | 186 + saml/proof/proof_test.go | 277 + saml/proof/xml.go | 595 + saml/protocol/channel.go | 206 + saml/protocol/common.go | 96 +- saml/protocol/control.go | 424 + saml/protocol/control_test.go | 272 + saml/protocol/create_response.go | 170 +- saml/protocol/doc.go | 34 +- saml/protocol/encryption.go | 736 + saml/protocol/encryption_test.go | 323 + saml/protocol/exchange.go | 700 + saml/protocol/hok.go | 336 + saml/protocol/hok_test.go | 143 + saml/protocol/legacy_test.go | 104 + saml/protocol/mapping.go | 475 + saml/protocol/mapping_test.go | 162 + saml/protocol/protocol_test.go | 10 + saml/protocol/qname.go | 135 + saml/protocol/query.go | 750 + saml/protocol/query_limits.go | 47 + saml/protocol/query_response.go | 1167 ++ saml/protocol/query_statement.go | 173 + saml/protocol/query_subject.go | 145 + saml/protocol/query_test.go | 572 + saml/protocol/request.go | 447 +- saml/protocol/response.go | 393 +- saml/protocol/shared_constants.go | 99 + saml/protocol/soap.go | 67 + saml/replay/store.go | 47 +- saml/replay/store_test.go | 30 +- saml/session/cancellation_test.go | 141 + saml/session/exchange_test.go | 214 + saml/session/freshness_test.go | 204 + saml/session/logout_retention_test.go | 146 + saml/session/performance_test.go | 73 + saml/session/reply.go | 56 + saml/session/session.go | 1189 ++ saml/session/session_test.go | 301 + saml/session/soap.go | 222 + saml/session/soap_test.go | 128 + saml/session/store_index_test.go | 84 + saml/sp/artifact_test.go | 147 + saml/sp/doc.go | 11 +- saml/sp/ecp_hok_test.go | 421 + saml/sp/encryption_test.go | 196 + saml/sp/sp.go | 514 +- saml/sp/sp_test.go | 157 + saml/state/state.go | 95 +- saml/state/state_test.go | 12 +- tests/README.md | 3 +- tests/cmd/dev/conformance.go | 21 +- tests/cmd/dev/interop.go | 75 + tests/cmd/dev/interop_test.go | 31 + tests/cmd/dev/language_inputs.go | 20 +- tests/cmd/dev/main.go | 29 +- tests/cmd/dev/main_test.go | 540 +- tests/cmd/dev/run.go | 17 +- tests/cmd/dev/run_test.go | 2080 +- tests/cmd/dev/suites.go | 28 +- tests/cmd/dev/suites_assert.go | 24 +- tests/cmd/dev/suites_environment.go | 29 +- tests/consumer_test.go | 2 +- tests/integration/evidence/evidence_test.go | 12 +- tests/integration/saml/benchmark_test.go | 288 +- tests/integration/saml/interop_test.go | 3 + tests/integration/saml/sso_test.go | 224 +- tests/integration/xmldsig/profiles_test.go | 3 + tests/integration/xmldsig/signature_test.go | 2 + tests/integration/xmldsig/transforms_test.go | 11 +- tests/interop/c14n_test.go | 100 +- tests/interop/cmd/c14nprobe/main.go | 17 +- tests/interop/cmd/c14nprobe/main_test.go | 16 +- tests/interop/cmd/evidenceprobe/main.go | 24 +- tests/interop/cmd/keyinfoprobe/main.go | 12 +- tests/interop/cmd/keyinfoprobe/main_test.go | 12 +- tests/interop/cmd/keyprobe/main.go | 14 +- tests/interop/cmd/keyprobe/main_test.go | 8 +- tests/interop/cmd/nextprobe/main.go | 50 +- tests/interop/cmd/nextprobe/main_test.go | 10 +- tests/interop/cmd/parametersprobe/main.go | 12 +- .../interop/cmd/parametersprobe/main_test.go | 6 + tests/interop/cmd/samlprobe/main.go | 5 +- tests/interop/crypto_test.go | 2 +- tests/interop/encryption_test.go | 1128 +- tests/interop/evidence_test.go | 164 +- tests/interop/fixtures_test.go | 22 +- tests/interop/helpers_test.go | 26 +- tests/interop/keys_test.go | 64 +- tests/interop/main_test.go | 4 +- tests/interop/parameters_test.go | 8 +- tests/interop/profiles_test.go | 801 +- tests/interop/saml_authority_http_test.go | 334 + tests/interop/saml_authority_test.go | 134 + tests/interop/saml_control_test.go | 111 + tests/interop/saml_encryption_test.go | 126 + tests/interop/saml_gsa_http_test.go | 155 + tests/interop/saml_hok_oracle_test.go | 314 + tests/interop/saml_openam_http_test.go | 538 + tests/interop/saml_proof_test.go | 243 + tests/interop/signatures_test.go | 327 +- tests/interop/testdata/ECPProofOracle.java | 70 + .../testdata/OpenSAMLAuthorityOracle.java | 90 + tests/interop/testdata/OpenSAMLHoKOracle.java | 209 + tests/interop/testdata/c14n-oracle.c | 4 +- tests/interop/testdata/openssl-gcm.c | 15 +- tests/interop/xml_helpers_test.go | 4 +- tests/testdata/consumer/document_test.go | 22 +- tests/testdata/consumer/go.mod | 7 +- tests/testdata/consumer/helpers_test.go | 9 +- tests/testdata/consumer/resource_test.go | 21 +- tests/testdata/consumer/result_test.go | 36 +- tests/testdata/consumer/transform_test.go | 40 + tests/testdata/consumer/xmldsig_test.go | 2 + tests/testdata/consumer/xslt_test.go | 2 +- timestamp/assertion.go | 43 +- timestamp/authenticate.go | 58 +- timestamp/client.go | 2 +- timestamp/clone.go | 4 +- timestamp/format.go | 594 +- timestamp/issue.go | 70 +- timestamp/timestamp_test.go | 352 +- timestamp/verify.go | 370 +- transform/comparison_v1_test.go | 815 +- transform/context.go | 88 +- transform/doc.go | 5 +- transform/document.go | 30 +- transform/document_internal_test.go | 262 +- transform/document_test.go | 12 +- transform/engine.go | 219 +- transform/function.go | 71 +- transform/helpers_test.go | 9 + transform/internal/pattern/characters.go | 16 +- transform/internal/pattern/parser.go | 9 +- transform/internal/pattern/pattern.go | 83 +- transform/internal/pattern/pattern_test.go | 22 +- .../internal/unparsedtext/unparsedtext.go | 167 +- .../unparsedtext/unparsedtext_test.go | 60 +- transform/internal/xdm/duration.go | 20 +- transform/internal/xdm/encoding/ascii.go | 3 +- transform/internal/xdm/encoding/c1fallback.go | 60 +- transform/internal/xdm/encoding/ebcdic.go | 16 +- .../internal/xdm/encoding/encoder_test.go | 28 +- transform/internal/xdm/encoding/encoding.go | 361 +- .../internal/xdm/encoding/encoding_test.go | 148 +- transform/internal/xdm/encoding/legacy.go | 41 + transform/internal/xdm/encoding/strict.go | 59 +- transform/internal/xdm/encoding/ucs4.go | 8 +- transform/internal/xdm/float.go | 17 +- transform/internal/xdm/float_test.go | 8 +- transform/internal/xdm/html/dump.go | 861 +- transform/internal/xdm/html/dump_test.go | 127 +- transform/internal/xdm/html/elements.go | 207 +- .../internal/xdm/html/encoding_writer_test.go | 89 + transform/internal/xdm/html/options.go | 19 +- .../internal/xdm/html/output_entities.go | 5 - .../internal/xdm/html/shared_constants.go | 2 + transform/internal/xdm/html/xhtml.go | 20 +- transform/internal/xdm/id.go | 84 +- .../xdm/lexical/canonical_key_test.go | 12 + transform/internal/xdm/lexical/compare.go | 393 +- .../internal/xdm/lexical/compare_test.go | 16 +- transform/internal/xdm/lexical/facets.go | 2 +- transform/internal/xdm/lexical/lexical.go | 17 +- transform/internal/xdm/lexical/validate.go | 170 +- transform/internal/xdm/names.go | 10 +- transform/internal/xdm/node.go | 179 +- transform/internal/xdm/node_mutation_test.go | 76 +- transform/internal/xdm/nodes_axes.go | 67 +- transform/internal/xdm/nodes_docorder.go | 115 +- .../internal/xdm/nodes_navigation_test.go | 66 +- transform/internal/xdm/nodes_order_test.go | 159 + transform/internal/xdm/nodes_union.go | 17 +- transform/internal/xdm/parse.go | 9 +- transform/internal/xdm/sequence_test.go | 70 +- transform/internal/xdm/sequence_type.go | 3 + transform/internal/xdm/serialize.go | 43 +- transform/internal/xdm/serialize_sequence.go | 22 +- transform/internal/xdm/serialize_test.go | 184 + transform/internal/xdm/uri/file.go | 18 +- transform/internal/xdm/uri/resolve.go | 11 +- transform/internal/xdm/uri/uripath.go | 29 +- transform/internal/xdm/values.go | 436 +- transform/internal/xdm/xmlwriter/doc.go | 7 +- .../internal/xdm/xmlwriter/lifecycle_test.go | 84 + transform/internal/xdm/xmlwriter/stream.go | 183 +- .../internal/xdm/xmlwriter/stream_test.go | 42 +- transform/internal/xpath/analysis.go | 7 +- .../internal/xpath/arithmetic_datetime.go | 85 +- transform/internal/xpath/atomize.go | 31 +- transform/internal/xpath/budget.go | 6 +- transform/internal/xpath/budget_numeric.go | 116 +- transform/internal/xpath/cast.go | 111 +- transform/internal/xpath/cast_datetime.go | 215 +- transform/internal/xpath/cast_numeric.go | 29 +- transform/internal/xpath/cast_string.go | 87 +- transform/internal/xpath/collation.go | 215 +- transform/internal/xpath/compare.go | 120 +- transform/internal/xpath/compile_direct.go | 30 +- transform/internal/xpath/consts.go | 20 +- transform/internal/xpath/context.go | 44 +- transform/internal/xpath/context_item_test.go | 62 + transform/internal/xpath/dynamic_context.go | 6 +- transform/internal/xpath/errors.go | 17 +- transform/internal/xpath/eval.go | 20 +- transform/internal/xpath/eval_arithmetic.go | 55 +- transform/internal/xpath/eval_control.go | 48 +- transform/internal/xpath/eval_funcall.go | 119 +- transform/internal/xpath/eval_operators.go | 114 +- transform/internal/xpath/eval_path.go | 73 +- .../internal/xpath/eval_path_descendant.go | 26 +- transform/internal/xpath/eval_path_exists.go | 62 +- transform/internal/xpath/eval_reuse.go | 28 +- transform/internal/xpath/eval_types.go | 80 +- transform/internal/xpath/evaluator.go | 65 +- transform/internal/xpath/evaluator_test.go | 258 +- transform/internal/xpath/expr.go | 3 + transform/internal/xpath/format_datetime.go | 319 +- transform/internal/xpath/format_integer.go | 431 +- transform/internal/xpath/format_number.go | 52 +- transform/internal/xpath/function_catalog.go | 610 +- .../internal/xpath/function_signatures.go | 20 +- transform/internal/xpath/functions.go | 133 +- .../internal/xpath/functions_aggregate.go | 123 +- transform/internal/xpath/functions_array.go | 172 +- .../internal/xpath/functions_constructors.go | 153 +- .../internal/xpath/functions_datetime.go | 192 +- transform/internal/xpath/functions_hof.go | 95 +- transform/internal/xpath/functions_json.go | 284 +- .../internal/xpath/functions_json_xml.go | 253 +- transform/internal/xpath/functions_map.go | 81 +- transform/internal/xpath/functions_math.go | 97 +- transform/internal/xpath/functions_misc.go | 108 +- transform/internal/xpath/functions_node.go | 310 +- transform/internal/xpath/functions_numeric.go | 127 +- transform/internal/xpath/functions_qname.go | 76 +- .../internal/xpath/functions_sequence.go | 118 +- .../internal/xpath/functions_serialize.go | 728 +- transform/internal/xpath/functions_string.go | 282 +- transform/internal/xpath/functions_test.go | 353 + .../internal/xpath/functions_unparsed_text.go | 45 +- transform/internal/xpath/functions_uri.go | 54 +- transform/internal/xpath/lexer.go | 54 +- transform/internal/xpath/node_identity.go | 17 +- .../internal/xpath/number/format_number.go | 309 +- .../xpath/number/format_number_test.go | 146 + transform/internal/xpath/parse_ietf_date.go | 175 +- transform/internal/xpath/parser.go | 245 +- transform/internal/xpath/profile.go | 204 +- transform/internal/xpath/profile10.go | 67 +- transform/internal/xpath/regex/translate.go | 189 +- .../internal/xpath/regex/translate_test.go | 18 +- transform/internal/xpath/regex_cache.go | 15 +- transform/internal/xpath/runtime.go | 10 +- transform/internal/xpath/runtime_test.go | 72 +- transform/internal/xpath/sequence.go | 15 +- .../internal/xpath/serialization_adaptive.go | 88 +- .../internal/xpath/serialization_sequence.go | 29 +- .../internal/xpath/serialization_values.go | 89 +- transform/internal/xpath/shared_constants.go | 14 +- transform/internal/xpath/static_check.go | 18 +- transform/internal/xpath/static_refs.go | 14 +- transform/internal/xpath/streamability.go | 13 +- transform/internal/xpath/token.go | 10 +- transform/internal/xpath/vm.go | 41 +- transform/internal/xpath/vm_path_nodes.go | 8 +- transform/internal/xpath/xpath.go | 8 +- transform/internal/xpath/xpath10_compat.go | 6 +- transform/internal/xslt/avt.go | 15 +- transform/internal/xslt/avt_test.go | 8 +- transform/internal/xslt/compile.go | 103 +- transform/internal/xslt/compile_formats.go | 94 +- .../internal/xslt/compile_functions_modes.go | 124 +- transform/internal/xslt/compile_imports.go | 38 +- .../internal/xslt/compile_instructions.go | 90 +- .../xslt/compile_instructions_flow.go | 54 +- .../xslt/compile_instructions_nodes.go | 65 +- transform/internal/xslt/compile_patterns.go | 298 +- transform/internal/xslt/compile_scope.go | 10 +- transform/internal/xslt/compile_static.go | 14 +- transform/internal/xslt/compile_streaming.go | 113 +- transform/internal/xslt/compile_templates.go | 54 +- transform/internal/xslt/compile_toplevel.go | 32 +- transform/internal/xslt/compile_types.go | 22 +- transform/internal/xslt/dispatch_index.go | 11 +- .../internal/xslt/dispatch_index_test.go | 159 +- transform/internal/xslt/element_info_data.go | 3 +- transform/internal/xslt/errors.go | 80 +- transform/internal/xslt/execute.go | 80 +- transform/internal/xslt/execute_apply.go | 42 +- transform/internal/xslt/execute_builtin.go | 30 +- transform/internal/xslt/execute_control.go | 142 +- transform/internal/xslt/execute_copy.go | 62 +- transform/internal/xslt/execute_element.go | 33 +- transform/internal/xslt/execute_globals.go | 117 +- transform/internal/xslt/execute_lre.go | 10 +- transform/internal/xslt/execute_misc.go | 74 +- transform/internal/xslt/execute_number.go | 185 +- transform/internal/xslt/execute_resultdoc.go | 50 +- transform/internal/xslt/execute_sequence.go | 10 +- transform/internal/xslt/execute_streaming.go | 59 +- transform/internal/xslt/execute_templates.go | 75 +- transform/internal/xslt/execute_transform.go | 20 +- transform/internal/xslt/execute_trycatch.go | 60 +- transform/internal/xslt/execution_test.go | 56 +- .../internal/xslt/function_signatures.go | 24 +- transform/internal/xslt/functions.go | 153 +- .../internal/xslt/functions_introspect.go | 33 +- transform/internal/xslt/functions_key.go | 126 +- .../internal/xslt/functions_transform.go | 544 +- transform/internal/xslt/functions_user.go | 71 +- transform/internal/xslt/grammar.go | 431 +- transform/internal/xslt/instruction.go | 1 + transform/internal/xslt/invocation.go | 17 +- .../internal/xslt/invocation_output_test.go | 48 + .../internal/xslt/invocation_resources.go | 74 + transform/internal/xslt/keys.go | 263 +- transform/internal/xslt/number_words.go | 58 +- transform/internal/xslt/output.go | 217 +- transform/internal/xslt/output_adaptive.go | 2 +- transform/internal/xslt/output_charmap.go | 23 +- transform/internal/xslt/output_html.go | 10 +- transform/internal/xslt/output_json_stream.go | 26 +- transform/internal/xslt/package_override.go | 14 +- transform/internal/xslt/package_resource.go | 25 +- transform/internal/xslt/package_visibility.go | 52 +- transform/internal/xslt/profile.go | 121 +- transform/internal/xslt/profile10.go | 156 +- transform/internal/xslt/qname_resolve.go | 2 +- transform/internal/xslt/serialize_encoding.go | 24 +- transform/internal/xslt/serialize_html.go | 16 +- transform/internal/xslt/serialize_sequence.go | 22 +- transform/internal/xslt/serialize_text.go | 6 +- transform/internal/xslt/shared_constants.go | 104 +- transform/internal/xslt/sort.go | 188 +- transform/internal/xslt/stylesheet.go | 41 +- transform/internal/xslt/types.go | 175 +- transform/internal/xslt/types_test.go | 30 + transform/normalization_benchmark_test.go | 55 + transform/records.go | 9 +- transform/resource.go | 9 +- transform/resource_test.go | 1119 +- transform/result.go | 30 +- transform/serialization_test.go | 1181 +- transform/v1.go | 93 +- transform/v1_test.go | 51 +- transform/v1_xslt.go | 36 +- transform/v1_xslt_test.go | 15 +- transform/value.go | 59 +- transform/value_test.go | 56 +- transform/xpath.go | 18 +- transform/xpath_test.go | 1043 +- transform/xslt.go | 85 +- transform/xslt_test.go | 2285 ++- xades/archive.go | 12 +- xades/archive_test.go | 191 +- xades/baseline.go | 72 +- xades/baseline_counter.go | 17 +- xades/baseline_data.go | 101 +- xades/baseline_data_time.go | 58 +- xades/baseline_policy.go | 106 +- xades/baseline_properties.go | 106 +- xades/baseline_role.go | 108 +- xades/baseline_time.go | 62 +- xades/baseline_types.go | 42 +- xades/basic.go | 135 +- xades/basic_test.go | 396 +- xades/historical.go | 33 +- xades/historical_data_time.go | 2 +- xades/historical_input.go | 116 +- xades/material.go | 124 +- xades/proof.go | 12 +- xades/shared_constants.go | 90 +- xades/timestamp.go | 66 +- xades/timestamp_test.go | 8 +- xades/validate.go | 60 +- xades/with_time.go | 8 +- xades/with_time_crl.go | 5 +- xades/with_time_test.go | 676 +- xmldsig/algorithms.go | 29 +- xmldsig/builder.go | 42 +- xmldsig/core.go | 254 +- xmldsig/core_test.go | 70 +- xmldsig/detached.go | 180 +- xmldsig/detached_test.go | 154 +- xmldsig/doc.go | 4 +- xmldsig/helpers_test.go | 16 +- xmldsig/hmac.go | 21 +- xmldsig/hmac_test.go | 10 +- xmldsig/keyinfo_test.go | 60 +- xmldsig/manifest.go | 71 +- xmldsig/manifest_test.go | 292 +- xmldsig/parameters.go | 49 +- xmldsig/parameters_test.go | 196 +- xmldsig/parameters_xml.go | 67 +- xmldsig/policy.go | 94 +- xmldsig/provider.go | 2 +- xmldsig/reference.go | 4 +- xmldsig/resource_stream.go | 16 +- xmldsig/resources.go | 55 +- xmldsig/shared_constants.go | 19 +- xmldsig/sign.go | 24 +- xmldsig/template.go | 17 +- xmldsig/transforms.go | 2 +- xmldsig/transforms_test.go | 16 +- xmldsig/verify.go | 74 +- xmlenc/agreement.go | 156 +- xmlenc/codec.go | 74 +- xmlenc/content.go | 2 +- xmlenc/content_test.go | 54 +- xmlenc/data.go | 25 +- xmlenc/doc.go | 6 +- xmlenc/element_test.go | 2 +- xmlenc/fragment.go | 12 +- xmlenc/graph.go | 50 +- xmlenc/graph_agreement.go | 16 +- xmlenc/graph_cipher.go | 22 +- xmlenc/graph_decrypt.go | 7 +- xmlenc/graph_generate.go | 183 +- xmlenc/graph_keys.go | 144 +- xmlenc/graph_state.go | 118 +- xmlenc/kdf.go | 112 +- xmlenc/keywrap.go | 64 +- xmlenc/keywrap_des.go | 47 +- xmlenc/keywrap_test.go | 4 + xmlenc/method.go | 14 +- xmlenc/oaep.go | 29 +- xmlenc/policy.go | 15 +- xmlenc/provider.go | 42 + xmlenc/reference.go | 37 +- xmlenc/reference_test.go | 244 +- xmlenc/replace.go | 2 +- xmlenc/replace_test.go | 10 +- xmlenc/rsa15_test.go | 73 + xmlenc/suites_test.go | 188 +- xmlenc/symmetric.go | 21 +- xmlenc/symmetric_test.go | 38 +- xmlenc/xmlenc.go | 11 +- xmlenc/xmlenc_test.go | 308 +- 707 files changed, 73380 insertions(+), 22062 deletions(-) create mode 100644 build/interop-inputs.txt create mode 100644 build/interop.sh create mode 100644 build/openam-nameid-order.patch create mode 100755 build/patch-openam.sh create mode 100644 build/prepare-interop.sh create mode 100644 docs/saml-authority.md create mode 100644 docs/saml.md create mode 100644 document/parse_options.go create mode 100644 document/parse_options_test.go create mode 100644 document/testdata/fuzz/FuzzNonValidatingXML/c0b432ce1eb33f95 create mode 100644 internal/normalization/reuse.go create mode 100644 internal/normalization/reuse_test.go create mode 100644 internal/tree/doctype.go create mode 100644 internal/tree/scope.go create mode 100644 internal/tree/scope_test.go create mode 100644 saml/artifact/artifact.go create mode 100644 saml/artifact/artifact_test.go create mode 100644 saml/artifact/cancellation_test.go create mode 100644 saml/artifact/performance_test.go create mode 100644 saml/artifact/store_index_test.go create mode 100644 saml/authority/authority.go create mode 100644 saml/authority/authority_test.go create mode 100644 saml/authority/client.go create mode 100644 saml/authority/uri.go create mode 100644 saml/binding/artifact.go create mode 100644 saml/binding/artifact_test.go create mode 100644 saml/binding/soap.go create mode 100644 saml/binding/soap_http_contract_test.go create mode 100644 saml/binding/soap_test.go create mode 100644 saml/binding/uri.go create mode 100644 saml/binding/uri_test.go create mode 100644 saml/ecp/channel.go create mode 100644 saml/ecp/channel_test.go create mode 100644 saml/ecp/ecp.go create mode 100644 saml/ecp/ecp_test.go create mode 100644 saml/ecp/hok.go create mode 100644 saml/ecp/hok_test.go create mode 100644 saml/ecp/http.go create mode 100644 saml/ecp/roles_test.go create mode 100644 saml/internal/expiry/index.go create mode 100644 saml/internal/expiry/index_test.go create mode 100644 saml/metadata/authority.go create mode 100644 saml/metadata/authority_test.go create mode 100644 saml/metadata/ecp.go create mode 100644 saml/metadata/ecp_test.go create mode 100644 saml/proof/proof.go create mode 100644 saml/proof/proof_test.go create mode 100644 saml/proof/xml.go create mode 100644 saml/protocol/channel.go create mode 100644 saml/protocol/control.go create mode 100644 saml/protocol/control_test.go create mode 100644 saml/protocol/encryption.go create mode 100644 saml/protocol/encryption_test.go create mode 100644 saml/protocol/exchange.go create mode 100644 saml/protocol/hok.go create mode 100644 saml/protocol/hok_test.go create mode 100644 saml/protocol/legacy_test.go create mode 100644 saml/protocol/mapping.go create mode 100644 saml/protocol/mapping_test.go create mode 100644 saml/protocol/qname.go create mode 100644 saml/protocol/query.go create mode 100644 saml/protocol/query_limits.go create mode 100644 saml/protocol/query_response.go create mode 100644 saml/protocol/query_statement.go create mode 100644 saml/protocol/query_subject.go create mode 100644 saml/protocol/query_test.go create mode 100644 saml/protocol/soap.go create mode 100644 saml/session/cancellation_test.go create mode 100644 saml/session/exchange_test.go create mode 100644 saml/session/freshness_test.go create mode 100644 saml/session/logout_retention_test.go create mode 100644 saml/session/performance_test.go create mode 100644 saml/session/reply.go create mode 100644 saml/session/session.go create mode 100644 saml/session/session_test.go create mode 100644 saml/session/soap.go create mode 100644 saml/session/soap_test.go create mode 100644 saml/session/store_index_test.go create mode 100644 saml/sp/artifact_test.go create mode 100644 saml/sp/ecp_hok_test.go create mode 100644 saml/sp/encryption_test.go create mode 100644 tests/cmd/dev/interop.go create mode 100644 tests/cmd/dev/interop_test.go create mode 100644 tests/interop/saml_authority_http_test.go create mode 100644 tests/interop/saml_authority_test.go create mode 100644 tests/interop/saml_control_test.go create mode 100644 tests/interop/saml_encryption_test.go create mode 100644 tests/interop/saml_gsa_http_test.go create mode 100644 tests/interop/saml_hok_oracle_test.go create mode 100644 tests/interop/saml_openam_http_test.go create mode 100644 tests/interop/saml_proof_test.go create mode 100644 tests/interop/testdata/ECPProofOracle.java create mode 100644 tests/interop/testdata/OpenSAMLAuthorityOracle.java create mode 100644 tests/interop/testdata/OpenSAMLHoKOracle.java create mode 100644 transform/internal/xdm/encoding/legacy.go create mode 100644 transform/internal/xpath/context_item_test.go create mode 100644 transform/internal/xslt/invocation_output_test.go create mode 100644 transform/internal/xslt/invocation_resources.go create mode 100644 transform/normalization_benchmark_test.go create mode 100644 xmlenc/rsa15_test.go diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7f5d72a..47c26d2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -76,11 +76,11 @@ jobs: run: make examples interop: runs-on: ubuntu-26.04 - timeout-minutes: 45 + timeout-minutes: 90 steps: - name: Check out source uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false - - name: Run full interoperability checks in Docker + - name: Run required external checks and Shibboleth/OpenAM/GSA SOAP servers run: make interop-docker diff --git a/README.md b/README.md index 4f1f00b..d2a991d 100644 --- a/README.md +++ b/README.md @@ -16,15 +16,17 @@ verification, and supported SAML WebSSO flows. Builds without CGO. - **Sign and verify XML documents** with XMLDSig, including enveloped, enveloping and detached signatures. -- **Encrypt and decrypt XML or bytes** with AES-GCM, using direct keys, RSA-OAEP - or AES Key Wrap. +- **Encrypt and decrypt XML or bytes** with AES-GCM, using direct keys, RSA-OAEP, + AES Key Wrap or ECDH-ES/ConcatKDF. - **Select, transform and canonicalize XML** with XPath, XSLT and canonicalization APIs for documents, subtrees and node sets. - **Work with keys, certificates and signature evidence** using PEM/DER, protected PKCS#8/PKCS#12 containers, offline PKIX validation, CMS, timestamps and supported XAdES profiles. - **Build solicited SAML WebSSO flows** for service providers and identity - providers, with signed metadata, POST/Redirect bindings and state/replay stores. + providers, with signed metadata, POST/Redirect/Artifact bindings, bearer and + holder-of-key ECP, and state/replay stores. Separate APIs handle logout, + identifier management, authority queries and assertion retrieval. The optional [CLI](docs/cli.md) exposes operations through the same public APIs. @@ -138,15 +140,19 @@ The API is **experimental**. The library implements specific profiles of the listed standards; full standards conformance and an independent security audit have not been established. -The document security parser rejects DTDs and external entities. XPath/XSLT -language input has a separate bounded, nonvalidating DTD mode with explicit -external resource policy; see +The document security parser rejects DTDs and external entities. Explicit +`document.ParseOptions{NonValidating: true}` and XPath/XSLT language input provide +bounded, nonvalidating DTD processing with explicit external resource policy; see [XML input profiles](docs/transforms.md#xml-input-profiles). XSD validation is unsupported. The XPath/XSLT engine implements selected XPath 1.0/2.0/3.0/3.1 and XSLT 1.0/2.0/3.0 features. Complete Basic XSLT conformance remains open; [validation profiles](docs/language-baseline.md) define the required suite checks. Schema awareness and W3C streamable execution are unsupported. SAML support covers -solicited WebSSO; it does not include SLO, Artifact/ECP or encrypted assertions. +solicited WebSSO, including encrypted assertions, identifiers and attributes, +SLO, Artifact/SOAP, bearer and X.509 holder-of-key ECP/PAOS, and NameID management. +See [SAML integration](docs/saml.md) for supported editions and state contracts, +and [authority integration](docs/saml-authority.md) for queries, assertion retrieval +and NameID Mapping. See [compatibility](docs/compatibility.md) for supported profiles, exclusions and known interoperability differences, and [standards](docs/standards.md) for primary @@ -162,7 +168,9 @@ its consistency checks do not establish full standards conformance. [key formats](examples/keys) and [certificate and evidence verification](examples/trust). - **XML processing:** [XPath/XSLT guide](docs/transforms.md), [transform examples](examples/transforms) and [streaming and memory](docs/streaming.md). -- **SAML:** [WebSSO example](examples/sso) and [Redirect binding](examples/redirect). +- **SAML:** [integration guide](docs/saml.md), + [authority queries and mapping](docs/saml-authority.md), + [WebSSO example](examples/sso) and [Redirect binding](examples/redirect). - **Reference:** [API guide](docs/usage.md), [all examples](examples), [CLI](docs/cli.md), [package architecture](docs/architecture.md) and [security policy](SECURITY.md). diff --git a/build/Makefile b/build/Makefile index eb86876..2ff1dab 100644 --- a/build/Makefile +++ b/build/Makefile @@ -3,6 +3,7 @@ GOLANGCI_LINT ?= golangci-lint GOLANGCI_LINT_CONFIG ?= build/.golangci.yml DOCKER ?= docker INTEROP_DOCKER_IMAGE ?= xmlsec-interop:local +INTEROP_INPUT_CACHE ?= /tmp/xmlsec-interop-inputs FUZZ_ARGS ?= BENCH_ARGS ?= INTEROP_ARGS ?= @@ -53,8 +54,7 @@ suites: # Optional independent engines; never part of ordinary Go checks. interop: - $(GO) test -tags=interop -count=1 -v -timeout=20m ./transform -run=Compare - $(GO) test -tags=interop -count=1 -v -timeout=15m ./tests/interop -args $(INTEROP_ARGS) + GO="$(GO)" bash build/interop.sh $(INTEROP_ARGS) # The same complete Linux run used by the Docker image. interop-full: @@ -64,8 +64,10 @@ interop-full: java -version pkg-config --modversion xmlsec1-openssl libxml-2.0 $(GO) test -race -count=1 ./... - $(MAKE) interop INTEROP_ARGS="-suite=all" + XMLSEC_P7_EXPORT_FIXTURES=/tmp/xmlsec-interop-history $(GO) test -count=1 ./xades -run '^TestHistoricalExpiredTSAArchiveEvidence$$' + XMLSEC_DSS_FIXTURE_DIR=/tmp/xmlsec-interop-history XMLSEC_DSS_CA_PEM=root.pem sh build/dss.sh --inside-all interop-docker: - $(DOCKER) build --file build/interop.Dockerfile --tag "$(INTEROP_DOCKER_IMAGE)" . + sh build/prepare-interop.sh "$(INTEROP_INPUT_CACHE)" + $(DOCKER) build --build-context "interop-downloads=$(INTEROP_INPUT_CACHE)" --file build/interop.Dockerfile --tag "$(INTEROP_DOCKER_IMAGE)" . $(DOCKER) run --rm --network=none "$(INTEROP_DOCKER_IMAGE)" diff --git a/build/README.md b/build/README.md index 0e27706..c3208f8 100644 --- a/build/README.md +++ b/build/README.md @@ -1,8 +1,26 @@ # Build support Run make from the module root; its Makefile includes [Makefile](Makefile). +The interoperability targets require Bash as well as a POSIX shell. [.golangci.yml](.golangci.yml) configures lint/formatting and [interop.Dockerfile](interop.Dockerfile) supplies optional native test engines. -The Docker build context is the module root. +The Docker build context is the module root. `make interop-docker` first prepares +the checksum-pinned downloads listed in [interop-inputs.txt](interop-inputs.txt) +and passes their cache as the `interop-downloads` named build context. Override +`INTEROP_INPUT_CACHE` to reuse another cache directory. Tests run offline. + +[interop.sh](interop.sh) runs both external suites. In the CI image, +`XMLSEC_REQUIRE_INTEROP=1` rejects skipped, failed or incomplete test streams. +The full recipe starts the official DSS service and the test-owned Shibboleth +IdP/Jetty, OpenAM/Tomcat and Google GSA deployments; no external service +credentials are required. See [testing](../docs/testing.md) for commands and prerequisites. + +The `authority-http` selection covers AuthnQuery, AssertionIDRequest and NameID +Mapping using OpenAM 16.1.4, plus AuthzDecisionQuery using Google GSA adaptor +4.1.4. The OpenAM test WAR has one disclosed serializer correction in +[openam-nameid-order.patch](openam-nameid-order.patch), compiled offline by +[patch-openam.sh](patch-openam.sh); GSA uses its unmodified upstream example and +handler. See [the coverage boundaries](../docs/saml-authority.md#tests-and-independent-evidence) +for peer defects, the legacy Mapping cipher and GSA's TLS-only response profile. diff --git a/build/dss.sh b/build/dss.sh index 03f1676..f8120b3 100755 --- a/build/dss.sh +++ b/build/dss.sh @@ -23,6 +23,7 @@ cleanup() { } inside() { + fixtures_dir=${XMLSEC_DSS_FIXTURE_DIR:-/fixtures} for tool in java keytool jar go bash timeout sha256sum; do command -v "$tool" >/dev/null 2>&1 || fail "missing container tool: $tool" done @@ -44,7 +45,7 @@ inside() { sed -i 's/ "$CATALINA_BASE/lib/dss-custom.properties" <<'PROPERTIES' @@ -82,7 +83,7 @@ PROBE sleep 1 done export XMLSEC_DSS_URL=http://127.0.0.1:8080/ - export XMLSEC_DSS_FIXTURES="/fixtures/$XMLSEC_DSS_MANIFEST_NAME" + export XMLSEC_DSS_FIXTURES="$fixtures_dir/${XMLSEC_DSS_MANIFEST_NAME:-manifest.json}" export XMLSEC_DSS_RUNTIME_JAR="$CATALINA_BASE/webapps/ROOT/WEB-INF/lib/dss-validation-$dss_version.jar" export XMLSEC_REQUIRE_P7_PROOFS=1 # The test checks actual JAR Maven metadata and endpoint version as well as @@ -90,13 +91,18 @@ PROBE XMLSEC_DSS_REPORT_DIR=${XMLSEC_DSS_REPORT_DIR:-/tmp/xmlsec-dss-reports} export XMLSEC_DSS_REPORT_DIR mkdir -p "$XMLSEC_DSS_REPORT_DIR" + if [ "${1:-}" = all ]; then + export XMLSEC_REQUIRE_P3_PROOFS=1 XMLSEC_REQUIRE_P5_PROOFS=1 XMLSEC_REQUIRE_P6_PROOFS=1 + make interop INTEROP_ARGS=-suite=all + return + fi go run ./tests/cmd/dev proof capture -out "$XMLSEC_DSS_REPORT_DIR/dss.capture.json" -- \ -tags=interop ./tests/interop -run '^TestDSS(Historical|Archive)' -timeout=10m -args -suite=portable } -if [ "${1:-}" = --inside ]; then +if [ "${1:-}" = --inside ] || [ "${1:-}" = --inside-all ]; then [ "$#" -eq 1 ] || fail 'unexpected internal arguments' - inside + if [ "$1" = --inside-all ]; then inside all; else inside; fi exit fi case "${1:-}" in ''|--check) ;; *) fail 'usage: sh build/dss.sh [--check]' ;; esac diff --git a/build/interop-inputs.txt b/build/interop-inputs.txt new file mode 100644 index 0000000..34feb95 --- /dev/null +++ b/build/interop-inputs.txt @@ -0,0 +1,24 @@ +# SHA-256, official URL, cache-relative destination. Runtime tests are offline. +5ad3f26cfbb76c94137b79c0ccc35c068b19e209c5f8e32b9787bafb73446e96 https://shibboleth.net/downloads/identity-provider/5.2.3/shibboleth-identity-provider-5.2.3.zip idp.zip +f269b7b98691a48647e54ca5bce9d1042c0eb5f12a0887879974f418f70baa23 https://repo.maven.apache.org/maven2/org/eclipse/jetty/jetty-home/12.1.14/jetty-home-12.1.14.tar.gz jetty.tar.gz +1c7db9f726df835349c64edd631de0310eca31291100230064eba153f607b0be https://github.com/Saxonica/Saxon-HE/releases/download/SaxonHE12-10/SaxonHE12-10J.zip saxon.zip +c52862d33cd5bb34ed20d48a5ff62bc475e4279c16fd13460a19f726d1dcf62f https://repo.maven.apache.org/maven2/net/sf/saxon/Saxon-HE/9.4.0-9/Saxon-HE-9.4.0-9.jar saxon20.jar +1cbcc49ac5ed8942ba58276699254469ca2144e229f68faeeb48f784c1037953 https://www.w3.org/2014/04/xslt-xquery-serialization/schema-for-serialization-parameters.xsd schemas/ser30.xsd +256736a2f4e7923e888047dee757c17bc3ee707e285f00639fd8fb1d962d11b0 https://www.w3.org/2017/01/xslt-xquery-serialization/schema-for-serialization-parameters.xsd schemas/ser31.xsd +34407695050d165be14931c0f378f832d24abe3de682a069d2d6121f07661ec3 https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/schema-for-json.xsd schemas/json.xsd +c5301a890b0265a705f0806269aa37345aa47d7245a78900b941af65290b16fa https://www.w3.org/TR/2013/REC-xmlenc-core1-20130411/xenc-schema.xsd xmlenc/xenc-schema.xsd +2b48432f036262345638536601d4ea3224897e2d41901163c04eebf350477958 https://www.w3.org/TR/2013/REC-xmlenc-core1-20130411/xenc-schema-11.xsd xmlenc/xenc-schema-11.xsd +5dd57f074870e1d91f7eb814aa92967cefcce9011a86adf5e12a769fcf2a237e https://www.w3.org/TR/2002/REC-xmlenc-core-20021210/xenc-schema.xsd xmlenc/xenc-schema-2002.xsd +35cf8197da812c85e40d57891b35c94187569ed474a2dac813ce5090dafcd35c https://www.w3.org/TR/2008/REC-xmldsig-core-20080610/xmldsig-core-schema.xsd xmlenc/xmldsig-core-schema.xsd +457745286eaa292ae1aaa6e976e0f30eeceb0a37cc2301151576175e0ae1986c https://forge.etsi.org/rep/esi/x19_13201_XAdES/raw/v1.3.1/1913201-XAdES01903v132.xsd xades/1913201-XAdES01903v132.xsd +286bd63f122aafb907c03724c5959455d114020e523631cc22e90c5f5aa667e2 https://forge.etsi.org/rep/esi/x19_13201_XAdES/raw/v1.3.1/1913201-XAdES01903v141.xsd xades/1913201-XAdES01903v141.xsd +6b796f3d4ca40b7917f6e765fbddc9923aa80a7dce68deab2cf7ba8a79c6d7c5 https://www.rfc-editor.org/rfc/rfc9879.txt rfc9879.txt +f0e10bb218795b966824887a8c73b555ab9d88a8e84997882d3e5a807cc9cb37 https://ec.europa.eu/digital-building-blocks/artifact/repository/esignaturedss/eu/europa/ec/joinup/sd-dss/dss-demo-bundle/6.5.1/dss-demo-bundle-6.5.1.zip dss.zip +# Deployed OpenAM SOAP Authority and Google GSA authorization server. +1c8bceab5ec214b92cd2f16b76fd442c3f85483a224db9dcc6417e04a905e2dc https://github.com/OpenIdentityPlatform/OpenAM/releases/download/16.1.4/OpenAM-16.1.4.war openam.war +e04ea07b9cf12c8c06bf520a16d5c546dc3e6a5949fa9ed3ec6a8cbf473ef8cf https://github.com/OpenIdentityPlatform/OpenAM/releases/download/16.1.4/SSOConfiguratorTools-16.1.4.zip openam-configurator.zip +e0f0293ba0fdbcbc52b8554d3ff0b3cba0a59bec0b2f0f1d35b1ea9efdf029a7 https://github.com/OpenIdentityPlatform/OpenAM/releases/download/16.1.4/SSOAdminTools-16.1.4.zip openam-admin.zip +3f53672bfd9ce2a6f3e16ed6b74f8ac2673de812c630feec3e022a89ceb57d24 https://github.com/googlegsa/library/releases/download/v4.1.4/adaptor-4.1.4-withlib.jar gsa-adaptor.jar +408b0e739af1eb8c52734903a4b695db38825d8de8a6543dc918e189d22dc734 https://codeload.github.com/googlegsa/library/zip/refs/tags/v4.1.4 gsa-source.zip + +cd62823e5bae65d17ddcf612ec6e57f0aff170ce5abb8bc987f444d759fc6326 https://raw.githubusercontent.com/OpenIdentityPlatform/OpenAM/16.1.4/openam-federation/openam-federation-library/src/main/java/com/sun/identity/saml2/protocol/impl/NameIDMappingResponseImpl.java OpenAM-NameIDMappingResponseImpl.java diff --git a/build/interop.Dockerfile b/build/interop.Dockerfile index d0d0e0e..f34c673 100644 --- a/build/interop.Dockerfile +++ b/build/interop.Dockerfile @@ -1,11 +1,57 @@ FROM golang:1.27.3 AS go +FROM debian:forky-slim AS inputs + +RUN apt-get update && apt-get install -y --no-install-recommends unzip ca-certificates \ + && rm -rf /var/lib/apt/lists/* + +# Downloaded by prepare-interop.sh, verified again inside the build. +COPY --from=interop-downloads / /download/ +COPY build/interop-inputs.txt /tmp/interop-inputs.txt +RUN awk '!/^#/ && NF {print $1 " /download/" $3}' /tmp/interop-inputs.txt | sha256sum -c - \ + && mkdir -p /opt/opensaml /opt/inputs/saxon \ + && unzip -q /download/idp.zip -d /opt/inputs \ + && unzip -j /download/idp.zip '*/webapp/WEB-INF/lib/*.jar' -d /opt/opensaml \ + && tar -xzf /download/jetty.tar.gz -C /opt/inputs \ + && unzip -q /download/saxon.zip -d /opt/inputs/saxon \ + && unzip -q /download/dss.zip 'dss-demo-bundle-6.5.1/apache-tomcat-11.0.27/*' -d /tmp/dss \ + && mv /tmp/dss/dss-demo-bundle-6.5.1/apache-tomcat-11.0.27 /official-tomcat \ + && cp -R /download/schemas /download/xmlenc /download/xades /opt/inputs/ \ + && cp /download/saxon20.jar /download/rfc9879.txt /opt/inputs/ \ + && cp /opt/inputs/xmlenc/xmldsig-core-schema.xsd /opt/inputs/xades/xmldsig-core-schema-2008.xsd + +RUN mkdir -p /opt/inputs/openam/configurator /opt/inputs/openam/admin /opt/inputs/gsa \ + && cp /download/openam.war /opt/inputs/openam/ \ + && cp /download/OpenAM-NameIDMappingResponseImpl.java /opt/inputs/openam/NameIDMappingResponseImpl.java \ + && unzip -q /download/openam-configurator.zip -d /opt/inputs/openam/configurator \ + && unzip -q /download/openam-admin.zip -d /opt/inputs/openam/admin \ + && cp /download/gsa-adaptor.jar /opt/inputs/gsa/ \ + && unzip -q /download/gsa-source.zip 'library-4.1.4/src/com/google/enterprise/adaptor/examples/AdaptorWithCrawlTimeMetadataTemplate.java' -d /opt/inputs/gsa + FROM debian:forky-slim COPY --from=go /usr/local/go /usr/local/go +COPY --from=inputs /opt/opensaml /opt/opensaml +COPY --from=inputs /opt/inputs /opt/inputs +COPY --from=inputs /official-tomcat /official-tomcat ENV PATH="/usr/local/go/bin:${PATH}" \ GOTOOLCHAIN=local \ - GOPATH=/go + GOPATH=/go \ + OPENSAML_CLASSPATH="/opt/opensaml/*" \ + SHIBBOLETH_DIST=/opt/inputs/shibboleth-identity-provider-5.2.3 \ + JETTY_HOME=/opt/inputs/jetty-home-12.1.14 \ + OPENAM_DIST=/opt/inputs/openam \ + TOMCAT_HOME=/official-tomcat \ + GSA_DIST=/opt/inputs/gsa \ + SAXON_CLASSPATH="/opt/inputs/saxon/saxon-he-12.10.jar:/opt/inputs/saxon/lib/*" \ + SAXON20_CLASSPATH=/opt/inputs/saxon20.jar \ + SERIALIZATION_SCHEMA_DIR=/opt/inputs/schemas \ + JSON_SCHEMA_FILE=/opt/inputs/schemas/json.xsd \ + XMLENC_SCHEMA_DIR=/opt/inputs/xmlenc \ + XADES_SCHEMA_DIR=/opt/inputs/xades \ + RFC9879_FILE=/opt/inputs/rfc9879.txt \ + XMLSEC_REQUIRE_INTEROP=1 \ + XMLSEC_INTEROP_SHARDS=4 RUN apt-get update \ && apt-get install -y --no-install-recommends \ @@ -19,10 +65,14 @@ RUN apt-get update \ openjdk-21-jdk-headless \ openssl \ pkg-config \ + patch \ xmlsec1 \ xsltproc \ && rm -rf /var/lib/apt/lists/* +COPY build/patch-openam.sh build/openam-nameid-order.patch /opt/inputs/openam/ +RUN sh /opt/inputs/openam/patch-openam.sh /opt/inputs/openam /opt/inputs/openam/openam-nameid-order.patch + WORKDIR /workspace COPY go.mod go.sum ./ RUN go mod download && go mod verify diff --git a/build/interop.sh b/build/interop.sh new file mode 100644 index 0000000..3ee8194 --- /dev/null +++ b/build/interop.sh @@ -0,0 +1,50 @@ +#!/usr/bin/env bash +set -euo pipefail + +go_bin=${GO:-go} +reports=$(mktemp -d) +trap 'rm -rf "$reports"' EXIT + +run() { + local log=$1 + shift + if ! "$go_bin" test -json "$@" > "$log" 2>&1; then + cat "$log" + return 1 + fi + if [[ ${XMLSEC_REQUIRE_INTEROP:-} == 1 ]]; then + if ! "$go_bin" run ./tests/cmd/dev interop-check "$log"; then + cat "$log" + return 1 + fi + else + cat "$log" + fi +} + +shards=${XMLSEC_INTEROP_SHARDS:-1} +case "$shards" in [1-8]) ;; *) printf 'XMLSEC_INTEROP_SHARDS must be 1..8\n' >&2; exit 1 ;; esac +names=$("$go_bin" test -tags=interop ./transform -list '^TestCompare') +filters=() +for ((slot=0; slot&2; exit 1; } +printf 'Running %d transform comparisons in %d isolated processes\n' "$index" "$shards" +pids=() +for ((slot=0; slot/dev/null 2>&1; then + sha256sum "$1" | awk '{print $1}' + else + shasum -a 256 "$1" | awk '{print $1}' + fi +} + +while read -r pin url name; do + case "$pin" in ''|\#*) continue ;; esac + case "$name" in ''|/*|*..*) printf 'invalid input path: %s\n' "$name" >&2; exit 1 ;; esac + path="$destination/$name" + if [ -f "$path" ] && [ "$(digest "$path")" = "$pin" ]; then + continue + fi + mkdir -p "$(dirname -- "$path")" + printf 'Fetching pinned input: %s\n' "$name" + curl --fail --location --retry 3 --retry-all-errors --connect-timeout 15 --max-time 600 "$url" -o "$path.part" + [ "$(digest "$path.part")" = "$pin" ] || { printf 'SHA-256 mismatch: %s\n' "$name" >&2; exit 1; } + mv "$path.part" "$path" +done < "$root/build/interop-inputs.txt" diff --git a/c14n/c14n_test.go b/c14n/c14n_test.go index c74b6f8..7d3b558 100644 --- a/c14n/c14n_test.go +++ b/c14n/c14n_test.go @@ -15,7 +15,7 @@ import ( const ( canonicalEmptyChildXML = "" - xmlRXmlnsUrnRCXmlnsR = "" + namespaceResetXML = "" emptyRootXML = "" explicitEmptyRootXML = "" ) @@ -33,7 +33,7 @@ func TestExclusive(t *testing.T) { {name: "nested and sibling attribute order", input: ``, want: ``}, {name: "unused namespace", input: ``, want: ``}, {name: "visible prefix", input: ``, want: ``}, - {name: "default reset", input: xmlRXmlnsUrnRCXmlnsR, want: ``}, + {name: "default reset", input: namespaceResetXML, want: ``}, {name: "rebind", input: ``, want: ``}, {name: "attribute namespace order", input: ``, want: ``}, {name: "inclusive list", input: ``, opt: c14n.Options{InclusivePrefixes: []string{"p"}}, want: ``}, @@ -46,7 +46,7 @@ func TestExclusive(t *testing.T) { {name: "escaping", input: `<&> `, want: `<&> `}, {name: "CDATA", input: `]]>`, want: `<data>`}, {name: "subtree ancestor", input: ``, child: true, want: ``}, - {name: "subtree no empty default", input: xmlRXmlnsUrnRCXmlnsR, child: true, want: canonicalEmptyChildXML}, + {name: "subtree no empty default", input: namespaceResetXML, child: true, want: canonicalEmptyChildXML}, {name: "line endings", input: "x\r\ny\rz", want: "x\ny\nz"}, // Independently transcribed from W3C Exclusive C14N §2.2 (no layout whitespace). {name: "W3C re-enveloping", input: ``, child: true, want: ``}, @@ -221,7 +221,7 @@ func TestNamespaceFramesStayLocalToSubtrees(t *testing.T) { } func FuzzIdempotent(f *testing.F) { - for _, s := range []string{emptyRootXML, xmlRXmlnsUrnRCXmlnsR, ``, `t`, ``} { + for _, s := range []string{emptyRootXML, namespaceResetXML, ``, `t`, ``} { f.Add([]byte(s)) } diff --git a/c14n/doc.go b/c14n/doc.go index 112fcaa..bf44fc4 100644 --- a/c14n/doc.go +++ b/c14n/doc.go @@ -14,10 +14,11 @@ // fragment rather than a well-formed document. Inclusive algorithms apply their // respective inherited xml:* and xml:base rules to omitted ancestors. // Input is the parsed node model supplied by document, including its line-end -// and attribute normalization. The DTD-free parser does not provide declared -// entity replacement or DTD default attributes; the C14N writer does not add -// this preprocessing. Those input-model limits are distinct from the byte -// canonicalization rules and do not establish full XML processor conformance. +// and attribute normalization. The default DTD-free parser does not expand +// declared entities or supply DTD defaults; [document.ParseOptions] can enable +// that preprocessing before canonicalization. The writer does not perform DTD +// processing itself and rejects relative namespace names accepted by that mode. +// These contracts do not establish full XML processor conformance. // // # Algorithms and output // diff --git a/c14n/nodeset_test.go b/c14n/nodeset_test.go index 69061e8..457a1ba 100644 --- a/c14n/nodeset_test.go +++ b/c14n/nodeset_test.go @@ -15,8 +15,8 @@ import ( ) const ( - xmlRXmlBaseABCCXml = "" - xmlRXmlnsUrnRCXmlnsXX = "" + canonicalUnfixedBaseXML = "" + canonicalNamespaceResetXML = "" ) func TestNormativeCanonicalVectors(t *testing.T) { @@ -244,14 +244,14 @@ func TestCanonicalAlgorithms(t *testing.T) { child bool }{ {"namespace context", ``, ``, ``, ``, false}, - {"empty default", ``, xmlRXmlnsUrnRCXmlnsXX, xmlRXmlnsUrnRCXmlnsXX, xmlRXmlnsUrnRCXmlnsXX, false}, + {"empty default", ``, canonicalNamespaceResetXML, canonicalNamespaceResetXML, canonicalNamespaceResetXML, false}, {"inherit simple", ``, ``, ``, canonicalEmptyChildXML, true}, {"inherit xml id only 10", ``, ``, canonicalEmptyChildXML, canonicalEmptyChildXML, true}, {"unknown xml attribute", ``, ``, canonicalEmptyChildXML, canonicalEmptyChildXML, true}, {"xml base join", ``, ``, ``, ``, true}, {"relative bases", ``, ``, ``, ``, true}, {"empty join omitted", ``, ``, canonicalEmptyChildXML, ``, true}, - {"no fixup in full document", ``, xmlRXmlBaseABCCXml, xmlRXmlBaseABCCXml, xmlRXmlBaseABCCXml, false}, + {"no fixup in full document", ``, canonicalUnfixedBaseXML, canonicalUnfixedBaseXML, canonicalUnfixedBaseXML, false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { testCanonicalAlgorithmsCase(t, &tt) }) diff --git a/c14n/testdata/normative-vectors.json b/c14n/testdata/normative-vectors.json index de2ef17..2f35f3b 100644 --- a/c14n/testdata/normative-vectors.json +++ b/c14n/testdata/normative-vectors.json @@ -7,7 +7,7 @@ "oracle_sha256": "41939fc4e57d351fb8f2d8cff8f9cd98db9935e020863989ca90c07cf61784e0", "checked_on": "2026-10-22", "libxml2_oracle": "tests/interop/testdata/c14n-oracle.c", - "libxml2_oracle_sha256": "c67454d8702fbc574b60cda20d9863dc847098b0592f759240b03c6783969487", + "libxml2_oracle_sha256": "1ce7e654f35f75be596ab3cd9b782acff5c8368ef5083b11ae4609fd13c50af1", "verification": "Docker xmlsec-interop:local, network disabled; existing JDK/libxml2 adapters unchanged. Normative divergences have per-case reasons; they are diagnostics, not oracle agreement." }, "input_model": "DTD-free parsed node model; sparse node-set results may be XML fragments.", diff --git a/cmd/xmlsec/app.go b/cmd/xmlsec/app.go index 4d511c8..c3994c8 100644 --- a/cmd/xmlsec/app.go +++ b/cmd/xmlsec/app.go @@ -12,6 +12,15 @@ import ( "github.com/skarm/xmlsec/internal/lexicon" ) +// Exit statuses are the OS-facing CLI contract for usage, cancellation and deadlines; report status strings have a separate vocabulary. +const ( + cliExitSuccess = 0 + cliExitFailure = 1 + cliExitUsage = 2 + cliExitDeadline = 124 + cliExitCanceled = 130 +) + type ( commandHandler func(context.Context, []string, io.Reader, io.Writer, io.Writer) error @@ -33,7 +42,7 @@ func commands() []commandSpec { {"signature sign-detached", "Sign exact external resources from a local manifest", runDetached}, {"signature verify-detached", "Verify the exact resource set in a local manifest", runDetached}, {"signature inspect", "List untrusted signatures, algorithms, references and IDs", runInspect}, - {nameC14n, "Canonicalize XML, an element or an XPath node set", runXML}, + {nameC14N, "Canonicalize XML, an element or an XPath node set", runXML}, {"encryption encrypt", "Encrypt bytes, a document, an element or element content", runEncryption}, {"encryption decrypt", "Authenticate and decrypt explicitly selected XML ciphertext", runEncryption}, {"encryption externalize", "Extract selected ciphertext into a CipherReference", runEncryption}, @@ -209,22 +218,22 @@ func usageError(format string, args ...any) error { return usageFailure{fmt.Erro func exitCode(err error) int { if err == nil { - return 0 + return cliExitSuccess } if errors.Is(err, context.Canceled) { - return 130 + return cliExitCanceled } if errors.Is(err, context.DeadlineExceeded) { - return 124 + return cliExitDeadline } if _, ok := errors.AsType[usageFailure](err); ok { - return 2 + return cliExitUsage } - return 1 + return cliExitFailure } func writeJSON(w io.Writer, v any) error { diff --git a/cmd/xmlsec/application_test.go b/cmd/xmlsec/application_test.go index c899071..199a462 100644 --- a/cmd/xmlsec/application_test.go +++ b/cmd/xmlsec/application_test.go @@ -53,7 +53,7 @@ func TestApplicationHelpAndUsage(t *testing.T) { }) } - for _, args := range [][]string{{nameBogus}, {nameHelp, nameBogus}, {"version", nameExtra}, {"algorithms", nameExtra}, {nameC14n, "-unknown"}, {nameC14n, "-max-input-bytes", "0"}, {nameC14n, "-timeout", "-1s"}} { + for _, args := range [][]string{{nameBogus}, {nameHelp, nameBogus}, {"version", nameExtra}, {"algorithms", nameExtra}, {nameC14N, "-unknown"}, {nameC14N, "-max-input-bytes", "0"}, {nameC14N, "-timeout", "-1s"}} { var out bytes.Buffer err := run(t.Context(), args, strings.NewReader(``), &out, io.Discard) @@ -82,7 +82,7 @@ func TestCanceledStreamsAndDeadline(t *testing.T) { ctx, cancel := context.WithCancel(t.Context()) defer cancel() - args := []string{nameC14n} + args := []string{nameC14N} if deadline { args = append(args, "-timeout", "25ms") } @@ -142,7 +142,7 @@ func TestCanceledStreamsAndDeadline(t *testing.T) { dir := t.TempDir() out := testutil.WriteFile(t, dir, "keep.xml", []byte("original")) - rejectCLI(t, []byte(``), nameC14n, "-out", out, "-max-output-bytes", "1") + rejectCLI(t, []byte(``), nameC14N, "-out", out, "-max-output-bytes", "1") b, err := os.ReadFile(out) if err != nil || string(b) != "original" { @@ -288,7 +288,94 @@ func TestEncryptionScopesAndTransports(t *testing.T) { } } -func xmlCanonical(t *testing.T, raw []byte) string { t.Helper(); return string(cli(t, raw, nameC14n)) } +func TestExternalizeRejectsAliasedPaths(t *testing.T) { + secret := testutil.WriteFile(t, t.TempDir(), "aes", bytes.Repeat([]byte{9}, 32)) + encrypted := cli(t, []byte("secret"), nameEncrypt, flagKey, secret, "-transport", transportDirect) + + for _, alias := range []string{"directory symlink", "case insensitive"} { + for _, scenario := range []string{"input", "existing output", "new output"} { + t.Run(alias+"/"+scenario, func(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "result.xml") + other := filepath.Join(dir, "RESULT.xml") + if alias == "directory symlink" { + link := filepath.Join(t.TempDir(), "alias") + if err := os.Symlink(dir, link); err != nil { + t.Skipf("directory symlink unavailable: %v", err) + } + other = filepath.Join(link, "result.xml") + } else { + testutil.WriteFile(t, dir, "case-probe", nil) + if _, err := os.Stat(filepath.Join(dir, "CASE-PROBE")); errors.Is(err, os.ErrNotExist) { + t.Skip("filesystem distinguishes filename case") + } else if err != nil { + t.Fatal(err) + } + } + + args := []string{nameEncryption, "externalize", "-uri", "urn:cipher", "-cipher-out", other} + var original []byte + if scenario == "input" { + original = encrypted + testutil.WriteFile(t, dir, "result.xml", original) + args = append(args, "-in", path) + } else { + args = append(args, "-out", path) + if scenario == "existing output" { + original = []byte("existing output") + testutil.WriteFile(t, dir, "result.xml", original) + } + } + + var output bytes.Buffer + if err := run(t.Context(), args, bytes.NewReader(encrypted), &output, io.Discard); err == nil { + t.Error("accepted colliding ciphertext and XML paths") + } + if output.Len() != 0 { + t.Error("published XML after path collision") + } + if original != nil { + actual, err := os.ReadFile(path) + if err != nil || !bytes.Equal(actual, original) { + t.Fatalf("original file changed: %v", err) + } + } + }) + } + } +} + +func TestExternalizeDistinctOutputFiles(t *testing.T) { + dir := t.TempDir() + secret := testutil.WriteFile(t, dir, "aes", bytes.Repeat([]byte{9}, 32)) + encrypted := cli(t, []byte("secret"), nameEncrypt, flagKey, secret, "-transport", transportDirect) + input := testutil.WriteFile(t, dir, "input.xml", encrypted) + for _, separateDirectory := range []bool{false, true} { + t.Run(map[bool]string{false: "same directory", true: "separate directories"}[separateDirectory], func(t *testing.T) { + outputDir := t.TempDir() + cipherDir := outputDir + cipherName := "cipher.bin" + if separateDirectory { + cipherDir = t.TempDir() + cipherName = "output.xml" + } + output := filepath.Join(outputDir, "output.xml") + cipher := filepath.Join(cipherDir, cipherName) + cli(t, nil, nameEncryption, "externalize", "-in", input, "-out", output, "-uri", "urn:cipher", "-cipher-out", cipher) + resources := testutil.WriteJSON(t, outputDir, "resources.json", []resourceEntry{{URI: "urn:cipher", Path: cipher}}) + plain := cli(t, nil, nameDecrypt, "-in", output, flagKey, secret, "-transport", transportDirect, "-resources", resources) + if string(plain) != "secret" { + t.Fatal("distinct ciphertext/XML files did not decrypt") + } + original, err := os.ReadFile(input) + if err != nil || !bytes.Equal(original, encrypted) { + t.Fatal("input XML changed", err) + } + }) + } +} + +func xmlCanonical(t *testing.T, raw []byte) string { t.Helper(); return string(cli(t, raw, nameC14N)) } func testEncryptionRoundtrip(t *testing.T, dir string, source []byte, transport, scope, private, public, secret string) { encKey, decKey := secret, secret @@ -381,7 +468,7 @@ func TestKeyKeyInfoAndTransformCommands(t *testing.T) { } } - filtered := cli(t, []byte(`yesno`), nameC14n, "-xpath", "not(ancestor-or-self::omit)") + filtered := cli(t, []byte(`yesno`), nameC14N, "-xpath", "not(ancestor-or-self::omit)") if string(filtered) != `yes` { t.Fatal(string(filtered)) } @@ -452,7 +539,7 @@ func TestRejectedOptionsDoNotPublishOutput(t *testing.T) { password := testutil.WriteFile(t, dir, "password", []byte("password")) secret := testutil.WriteFile(t, dir, "aes", bytes.Repeat([]byte{5}, 32)) output := testutil.WriteFile(t, dir, "keep", []byte("original")) - rejectCLI(t, nil, "key", "convert", "-in", private, "-output-format", namePkcs8, "-output-password-file", password, "-encryption-bits", "129", "-out", output) + rejectCLI(t, nil, "key", "convert", "-in", private, "-output-format", namePKCS8, "-output-password-file", password, "-encryption-bits", "129", "-out", output) rejectCLI(t, []byte("secret"), nameEncrypt, "-transport", transportDirect, flagKey, secret, "-algorithm", "urn:unsupported", "-out", output) for _, args := range [][]string{{nameCMS, nameVerify, flagKey, private}, {"timestamp", nameInspect, "-nonce-file", password}, {"key", nameInspect, "-output-password-file", password}, {nameEncryption, nameDecrypt, "-cipher-out", output}} { @@ -465,60 +552,6 @@ func TestRejectedOptionsDoNotPublishOutput(t *testing.T) { } } -func verificationDetails(t *testing.T, raw []byte) json.RawMessage { - t.Helper() - - var report struct { - Status string `json:"status"` - Version int `json:"schema_version"` - Details json.RawMessage `json:"details"` - } - if err := json.Unmarshal(raw, &report); err != nil { - t.Fatal(err, string(raw)) - } - - if report.Version != 1 || report.Status == "" { - t.Fatal("missing report envelope", string(raw)) - } - - return report.Details -} - -func coreVerificationChecks(t *testing.T, raw []byte) json.RawMessage { - t.Helper() - - var detail struct { - Checks json.RawMessage `json:"checks"` - } - if err := json.Unmarshal(verificationDetails(t, raw), &detail); err != nil { - t.Fatal(err) - } - - return detail.Checks -} - -func cli(t *testing.T, input []byte, args ...string) []byte { - t.Helper() - - var out, stderr bytes.Buffer - if err := run(t.Context(), args, bytes.NewReader(input), &out, &stderr); err != nil { - t.Fatalf("%v: %v\nstderr: %s\nstdout: %s", args, err, stderr.String(), out.String()) - } - - return out.Bytes() -} - -func rejectCLI(t *testing.T, input []byte, args ...string) []byte { - t.Helper() - - var out, stderr bytes.Buffer - if err := run(t.Context(), args, bytes.NewReader(input), &out, &stderr); err == nil { - t.Fatalf("accepted invalid invocation %v", args) - } - - return out.Bytes() -} - func TestEncryptionLegacyCBCPolicy(t *testing.T) { dir := t.TempDir() secret := testutil.WriteFile(t, dir, "cbc-key", bytes.Repeat([]byte{7}, 32)) @@ -588,3 +621,110 @@ func TestStreamingOutputPublication(t *testing.T) { } } } + +func verificationDetails(t *testing.T, raw []byte) json.RawMessage { + t.Helper() + + var report struct { + Status string `json:"status"` + Version int `json:"schema_version"` + Details json.RawMessage `json:"details"` + } + if err := json.Unmarshal(raw, &report); err != nil { + t.Fatal(err, string(raw)) + } + + if report.Version != 1 || report.Status == "" { + t.Fatal("missing report envelope", string(raw)) + } + + return report.Details +} + +func coreVerificationChecks(t *testing.T, raw []byte) json.RawMessage { + t.Helper() + + var detail struct { + Checks json.RawMessage `json:"checks"` + } + if err := json.Unmarshal(verificationDetails(t, raw), &detail); err != nil { + t.Fatal(err) + } + + return detail.Checks +} + +func cli(t *testing.T, input []byte, args ...string) []byte { + t.Helper() + + var out, stderr bytes.Buffer + if err := run(t.Context(), args, bytes.NewReader(input), &out, &stderr); err != nil { + t.Fatalf("%v: %v\nstderr: %s\nstdout: %s", args, err, stderr.String(), out.String()) + } + + return out.Bytes() +} + +func rejectCLI(t *testing.T, input []byte, args ...string) []byte { + t.Helper() + + var out, stderr bytes.Buffer + if err := run(t.Context(), args, bytes.NewReader(input), &out, &stderr); err == nil { + t.Fatalf("accepted invalid invocation %v", args) + } + + return out.Bytes() +} + +func TestLanguageXMLScopeLimits(t *testing.T) { + sheet := filepath.Join(t.TempDir(), "scope-limits.xsl") + if err := os.WriteFile(sheet, []byte(`ok`), 0o600); err != nil { + t.Fatal(err) + } + cases := []struct { + name, xml, flag, limit, canonical string + reject bool + }{ + {"attributes below", ``, "-max-attributes", "1", "", true}, + {"attributes exact", ``, "-max-attributes", "2", ``, false}, + {"attributes raised", ``, "-max-attributes", "3", ``, false}, + {"namespaces below implicit", ``, "-max-namespaces", "1", "", true}, + {"namespaces below", ``, "-max-namespaces", "2", "", true}, + {"namespaces exact", ``, "-max-namespaces", "3", ``, false}, + {"namespaces raised", ``, "-max-namespaces", "4", ``, false}, + {"one namespace below", ``, "-max-namespaces", "1", "", true}, + {"one namespace exact", ``, "-max-namespaces", "2", ``, false}, + } + for _, command := range []string{"c14n", "xpath", "xslt"} { + for _, tc := range cases { + t.Run(command+"/"+tc.name, func(t *testing.T) { + args := []string{command} + switch command { + case "xpath": + args = append(args, "-expression", `'ok'`) + case "xslt": + args = append(args, "-stylesheet", sheet) + } + args = append(args, tc.flag, tc.limit) + var output, diagnostics bytes.Buffer + err := run(t.Context(), args, strings.NewReader(tc.xml), &output, &diagnostics) + if tc.reject { + if !errors.Is(err, document.ErrLimit) || exitCode(err) != 1 || output.Len() != 0 { + t.Fatalf("rejection: output=%q, error=%v, exit=%d", output.String(), err, exitCode(err)) + } + return + } + expected := tc.canonical + switch command { + case "xpath": + expected = "ok\n" + case "xslt": + expected = "ok" + } + if err != nil || exitCode(err) != 0 || output.String() != expected { + t.Fatalf("accepted boundary: output=%q, error=%v", output.String(), err) + } + }) + } + } +} diff --git a/cmd/xmlsec/cms_commands.go b/cmd/xmlsec/cms_commands.go index 6121257..279ea74 100644 --- a/cmd/xmlsec/cms_commands.go +++ b/cmd/xmlsec/cms_commands.go @@ -11,6 +11,12 @@ import ( "github.com/skarm/xmlsec/pki" ) +// The default content OID describes CMS data; signer-vector admission is counted before reading signer files. +const ( + cliDefaultCMSContentType = "1.2.840.113549.1.7.1" + cliMaxCMSSigners = 16 +) + func runCMS(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) error { cmd := args[0] fs := flag.NewFlagSet("cms "+cmd, flag.ContinueOnError) @@ -21,12 +27,12 @@ func runCMS(ctx context.Context, args []string, stdin io.Reader, stdout, stderr keyFile := fs.String("key", "", "single signer private key") certFile := fs.String("certificate", "", "single signer certificate") config := fs.String("signers", "", "trusted JSON signer array for multiple signers; paths relative to JSON") - hashName := fs.String("hash", "sha256", "signer digest hash") + hashName := fs.String("hash", cliDefaultHashName, "signer digest hash") pss := fs.Bool("pss", false, "use fixed RSA-PSS for signing") ski := fs.Bool("subject-key-identifier", false, "identify signer by certificate SKI") detached := fs.Bool("detached", false, "create detached SignedData") contentFile := fs.String("content", "", "exact detached content file for verification") - contentType := fs.String("content-type", "1.2.840.113549.1.7.1", "expected/created content type OID") + contentType := fs.String("content-type", cliDefaultCMSContentType, "expected/created content type OID") extract := fs.String("extract", "", "write authenticated content to this local file after verification") omitCerts := fs.Bool("omit-certificates", false, "omit embedded certificates when signing") legacyESS := fs.Bool("allow-legacy-ess", false, "allow legacy ESS certificate binding when verifying") @@ -208,7 +214,7 @@ func (c cmsCommand) signers(ctx context.Context) ([]cms.Signer, error) { return nil, err } - if len(specs) < 1 || len(specs) > 16 { + if len(specs) < 1 || len(specs) > cliMaxCMSSigners { return nil, usageError("require 1..16 signers") } @@ -303,6 +309,10 @@ func (c cmsCommand) verify(ctx context.Context, container *cms.SignedData, opt c if err := writeOutputContext(ctx, c.extract, stdout, result.Content()); err != nil { return err } + + if err := separateOutput(c.extract, c.output, "extract"); err != nil { + return err + } } } diff --git a/cmd/xmlsec/core.go b/cmd/xmlsec/core.go index e633c75..056b0c9 100644 --- a/cmd/xmlsec/core.go +++ b/cmd/xmlsec/core.go @@ -10,10 +10,21 @@ import ( "github.com/skarm/xmlsec/c14n" "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/reference" "github.com/skarm/xmlsec/xmldsig" ) +// Core verification and recursive Manifest traversal have separate count/depth defaults; recursive validation remains an explicit mode choice. +const ( + cliNoManifests = "none" + cliDefaultSignatures = 16 + cliMaxSignatures = 64 + cliDefaultManifestDepth = 8 + cliDefaultManifestNodes = 64 + cliDefaultManifestReferences = 256 +) + const nameRecursiveExternal = "recursive-external" type repeatedFlag []string @@ -43,22 +54,22 @@ func runCore(ctx context.Context, args []string, stdin io.Reader, stdout, stderr input := fs.String("in", "-", "input XML; - reads stdin") output := fs.String("out", "-", "output signed XML or diagnostic JSON") keyFile := fs.String("key", "", "private key for sign-template; trusted public key for verify-core") - index := fs.Int("signature-index", -1, "REQUIRED zero-based Signature index (explicit selection, not trust)") - idAttr := fs.String("id-attr", "ID", "application ID attribute local name") + index := fs.Int("signature-index", cliUnspecifiedSelectionIndex, "REQUIRED zero-based Signature index (explicit selection, not trust)") + idAttr := fs.String("id-attr", cliDefaultIDAttribute, "application ID attribute local name") idNS := fs.String("id-ns", "", "application ID namespace URI") - methods := fs.String(nameC14n, c14n.Exclusive, "comma-separated allowed SignedInfo canonicalization URIs") + methods := fs.String(nameC14N, c14n.Exclusive, "comma-separated allowed SignedInfo canonicalization URIs") modern := fs.Bool("modern-algorithms", false, "also permit fixed RSA-PSS, Ed25519 and Ed25519ph") - manifests := fs.String("manifests", "none", "Manifest validation: none, recursive or recursive-external") + manifests := fs.String("manifests", cliNoManifests, "Manifest validation: none, recursive or recursive-external") manifestTransforms := fs.Bool("manifest-transforms", false, "follow authenticated Manifest transform results; requires recursive mode") - depth := fs.Int("manifest-max-depth", 8, "recursive Manifest depth limit") - nodes := fs.Int("manifest-max-nodes", 64, "maximum unique Manifest elements") - refs := fs.Int("manifest-max-references", 256, "maximum total direct and nested Reference elements") + depth := fs.Int("manifest-max-depth", cliDefaultManifestDepth, "recursive Manifest depth limit") + nodes := fs.Int("manifest-max-nodes", cliDefaultManifestNodes, "maximum unique Manifest elements") + refs := fs.Int("manifest-max-references", cliDefaultManifestReferences, "maximum total direct and nested Reference elements") resources := fs.String("resources", "", "optional trusted JSON URI/path allowlist for external references") baseURI := fs.String("base-uri", "", "explicit absolute signature document URI for relative external references; requires -resources") - resourceLimit := fs.Int("max-resource-bytes", 8<<20, "maximum bytes per external resource") - totalLimit := fs.Int("max-total-resource-bytes", 32<<20, "maximum fetched source bytes per operation") + resourceLimit := fs.Int("max-resource-bytes", cliDefaultResourceBytes, "maximum bytes per external resource") + totalLimit := fs.Int("max-total-resource-bytes", cliDefaultTotalResourceBytes, "maximum fetched source bytes per operation") allowedTransforms := fs.String("allowed-transforms", "", "comma-separated permitted Reference transform URIs") - maxSignatures := fs.Int("max-signatures", 16, "maximum Signature elements (1..64)") + maxSignatures := fs.Int("max-signatures", cliDefaultSignatures, "maximum Signature elements (1..64)") idRules := fs.String("id-rules", "", "trusted JSON array of additional IDAttributeRule values") secretFile := fs.String("secret-file", "", "HMAC shared-secret file, instead of -key; requires -policy hmac") @@ -266,7 +277,7 @@ func (c coreValidationFlags) options(ctx context.Context, doc *document.Document } switch c.manifests { - case "none": + case cliNoManifests: case "recursive", nameRecursiveExternal: if c.manifests == nameRecursiveExternal && c.resources == "" { return opt, errors.New("external Manifest validation requires -resources") @@ -338,12 +349,12 @@ func coreExternalReferencesContext(ctx context.Context, path string, resourceLim func requiredIDElements(ctx context.Context, doc *document.Document, opt xmldsig.SignatureOptions, ids []string) ([]document.Element, error) { attribute := opt.IDAttribute if attribute == (document.Name{}) { - attribute.Local = "ID" + attribute.Local = cliDefaultIDAttribute } rules := append([]reference.IDAttributeRule{{Attribute: attribute}}, opt.AdditionalIDRules...) rules = append(rules, reference.IDAttributeRule{ - Attribute: document.Name{Local: "Id"}, + Attribute: document.Name{Local: lexicon.SecurityAttrID}, Elements: []document.Name{ { Space: xmldsig.Namespace, @@ -353,19 +364,19 @@ func requiredIDElements(ctx context.Context, doc *document.Document, opt xmldsig Local: "Manifest", }, { Space: xmldsig.Namespace, - Local: "Signature", + Local: lexicon.DSigElementSignature, }, { Space: xmldsig.Namespace, - Local: "SignedInfo", + Local: lexicon.DSigElementSignedInfo, }, { Space: xmldsig.Namespace, - Local: "Reference", + Local: lexicon.DSigElementReference, }, { Space: xmldsig.Namespace, - Local: "KeyInfo", + Local: lexicon.DSigElementKeyInfo, }, { Space: xmldsig.Namespace, - Local: "SignatureValue", + Local: lexicon.DSigElementSignatureValue, }, }, }) @@ -414,11 +425,11 @@ func (c coreInvocation) validate() error { return usageError("an explicit nonnegative -signature-index and -key or -secret-file are required") } - if c.maxSignatures < 1 || c.maxSignatures > 64 { + if c.maxSignatures < 1 || c.maxSignatures > cliMaxSignatures { return usageError("-max-signatures must be 1..64") } - if c.resourceLimit < 1 || c.resourceLimit > 64<<20 || c.totalLimit < 1 || c.totalLimit > 64<<20 { + if c.resourceLimit < 1 || c.resourceLimit > cliMaxBytes || c.totalLimit < 1 || c.totalLimit > cliMaxBytes { return usageError("resource byte limits must be 1..67108864") } diff --git a/cmd/xmlsec/detached.go b/cmd/xmlsec/detached.go index df4e2d6..cec1509 100644 --- a/cmd/xmlsec/detached.go +++ b/cmd/xmlsec/detached.go @@ -16,6 +16,11 @@ import ( "github.com/skarm/xmlsec/xmldsig" ) +// The explicit URI/path allowlist is bounded before its entries are opened. +const ( + cliMaxResourceEntries = 64 +) + // This file is a TRUSTED local manifest, not a resource list from a signature. // Paths are resolved relative to the manifest's directory; network is never used. type resourceEntry struct { @@ -58,7 +63,7 @@ func loadResourcesContext(ctx context.Context, path string) ([]resourceEntry, xm return nil, nil, fmt.Errorf("resource manifest: %w", err) } - if len(entries) == 0 || len(entries) > 64 { + if len(entries) == 0 || len(entries) > cliMaxResourceEntries { return nil, nil, errors.New("resource manifest requires 1..64 entries") } @@ -104,12 +109,12 @@ func loadResourcesContext(ctx context.Context, path string) ([]resourceEntry, xm return nil, err } - stat, err = f.Stat() - if err != nil { - return nil, errors.Join(err, f.Close()) + openedStat, statErr := f.Stat() + if statErr != nil { + return nil, errors.Join(statErr, f.Close()) } - if !stat.Mode().IsRegular() { + if !openedStat.Mode().IsRegular() { return nil, errors.Join(errors.New("resource must be a regular file"), f.Close()) } @@ -134,9 +139,9 @@ func runDetached(ctx context.Context, args []string, stdin io.Reader, stdout, st manifest := fs.String("resources", "", "trusted local JSON resource manifest") keyPath := fs.String("key", "", "private key file (sign) or externally trusted public key/certificate PEM/DER (verify)") output := fs.String("out", "-", "output path; - writes stdout") - canonical := fs.String(nameC14n, c14n.Exclusive, "SignedInfo C14N URI; verification permits exactly this method") - resourceLimit := fs.Int("max-resource-bytes", 8<<20, "maximum bytes per external resource") - totalLimit := fs.Int("max-total-resource-bytes", 32<<20, "maximum source bytes per operation") + canonical := fs.String(nameC14N, c14n.Exclusive, "SignedInfo C14N URI; verification permits exactly this method") + resourceLimit := fs.Int("max-resource-bytes", cliDefaultResourceBytes, "maximum bytes per external resource") + totalLimit := fs.Int("max-total-resource-bytes", cliDefaultTotalResourceBytes, "maximum source bytes per operation") allowedTransforms := fs.String("allowed-transforms", "", "comma-separated permitted reference transform URIs; empty uses implemented defaults") var input, method, inclusive string @@ -172,7 +177,7 @@ func runDetached(ctx context.Context, args []string, stdin io.Reader, stdout, st } limits := xmldsig.ResourceLimits{MaxResourceBytes: *resourceLimit, MaxTotalResourceBytes: *totalLimit} - if limits.MaxResourceBytes < 1 || limits.MaxResourceBytes > 64<<20 || limits.MaxTotalResourceBytes < 1 || limits.MaxTotalResourceBytes > 64<<20 { + if limits.MaxResourceBytes < 1 || limits.MaxResourceBytes > cliMaxBytes || limits.MaxTotalResourceBytes < 1 || limits.MaxTotalResourceBytes > cliMaxBytes { return usageError("resource byte limits must be 1..67108864") } diff --git a/cmd/xmlsec/detached_test.go b/cmd/xmlsec/detached_test.go index f0f917c..faa9d20 100644 --- a/cmd/xmlsec/detached_test.go +++ b/cmd/xmlsec/detached_test.go @@ -17,7 +17,7 @@ import ( "github.com/skarm/xmlsec/reference" ) -const flagC14n = "-c14n" +const flagC14N = "-c14n" func TestDetachedCLI(t *testing.T) { dir := t.TempDir() @@ -70,7 +70,7 @@ func TestDetachedCLI(t *testing.T) { } var sig, report, stderr bytes.Buffer - if err := run(t.Context(), []string{nameSignDetached, flagKey, private, flagResources, manifest, flagC14n, c14n.Canonical11}, nil, &sig, &stderr); err != nil { + if err := run(t.Context(), []string{nameSignDetached, flagKey, private, flagResources, manifest, flagC14N, c14n.Canonical11}, nil, &sig, &stderr); err != nil { t.Fatal(err, stderr.String()) } @@ -78,7 +78,7 @@ func TestDetachedCLI(t *testing.T) { t.Fatal(sig.String()) } - if err := run(t.Context(), []string{nameVerifyDetached, flagKey, public, flagResources, verifyManifest, flagC14n, c14n.Canonical11}, bytes.NewReader(sig.Bytes()), &report, &stderr); err != nil { + if err := run(t.Context(), []string{nameVerifyDetached, flagKey, public, flagResources, verifyManifest, flagC14N, c14n.Canonical11}, bytes.NewReader(sig.Bytes()), &report, &stderr); err != nil { t.Fatal(err, stderr.String()) } @@ -104,7 +104,7 @@ func TestDetachedCLI(t *testing.T) { tampered := strings.Replace(sig.String(), "", "A", 1) - err = run(t.Context(), []string{nameVerifyDetached, flagKey, public, flagResources, verifyManifest, flagC14n, c14n.Canonical11}, strings.NewReader(tampered), &report, &stderr) + err = run(t.Context(), []string{nameVerifyDetached, flagKey, public, flagResources, verifyManifest, flagC14N, c14n.Canonical11}, strings.NewReader(tampered), &report, &stderr) if err == nil || strings.Contains(err.Error(), "no such file") { t.Fatal("resource read before authentication", err) } diff --git a/cmd/xmlsec/encryption.go b/cmd/xmlsec/encryption.go index 7e7dca3..1aaf07f 100644 --- a/cmd/xmlsec/encryption.go +++ b/cmd/xmlsec/encryption.go @@ -7,12 +7,21 @@ import ( "errors" "flag" "io" + "os" "path/filepath" "github.com/skarm/xmlsec/document" "github.com/skarm/xmlsec/xmlenc" ) +// Plaintext, OAEP labels and direct AES key material have independent byte bounds. +const ( + cliDefaultPlaintextBytes = 8 << 20 + cliMaxPlaintextBytes = 32 << 20 + cliMaxOAEPLabelBytes = 1024 + cliMaxAESKeyBytes = 32 +) + func runEncryption(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) error { cmd := args[0] fs := flag.NewFlagSet("encryption "+cmd, flag.ContinueOnError) @@ -27,11 +36,11 @@ func runEncryption(ctx context.Context, args []string, stdin io.Reader, stdout, allowLegacy := fs.Bool("allow-legacy-data", false, "explicitly permit unauthenticated CBC and legacy TripleDES key wrap; denied in FIPS mode") wrap := fs.String("wrap-algorithm", xmlenc.AES256KeyWrap, "symmetric key wrap algorithm URI") name := fs.String("key-name", "", "exact untrusted KeyName hint for direct-key mode") - oaepHash := fs.String("oaep-hash", "sha256", "RSA-OAEP digest and MGF1 hash") + oaepHash := fs.String("oaep-hash", cliDefaultHashName, "RSA-OAEP digest and MGF1 hash") labelFile := fs.String("oaep-label-file", "", "optional exact OAEP label bytes") - maximum := fs.Int("max-plaintext-bytes", 8<<20, "maximum plaintext bytes (up to 32 MiB)") + maximum := fs.Int("max-plaintext-bytes", cliDefaultPlaintextBytes, "maximum plaintext bytes (up to 32 MiB)") id := fs.String("id", "", "exact selected element ID (element/content encryption or EncryptedData selection)") - idAttr := fs.String("id-attr", "ID", "selection ID attribute local name") + idAttr := fs.String("id-attr", cliDefaultIDAttribute, "selection ID attribute local name") idNS := fs.String("id-ns", "", "selection ID namespace") index := fs.Int("index", -1, "explicit zero-based EncryptedData index for decrypt/externalize") resources := fs.String("resources", "", "trusted URI/path-only manifest containing one external ciphertext") @@ -182,7 +191,7 @@ func (c encryptionCommand) options(ctx context.Context) (xmlenc.Options, error) return opt, usageError("-scope must be bytes, document, element or content") } - if c.maximum < 1 || c.maximum > 32<<20 { + if c.maximum < 1 || c.maximum > cliMaxPlaintextBytes { return opt, usageError("-max-plaintext-bytes must be 1..33554432") } @@ -199,7 +208,7 @@ func (c encryptionCommand) options(ctx context.Context) (xmlenc.Options, error) } if c.transport != nameRSA { - if c.labelFile != "" || c.oaepHash != "sha256" { + if c.labelFile != "" || c.oaepHash != cliDefaultHashName { return opt, usageError("OAEP options require -transport rsa") } } else { @@ -210,7 +219,7 @@ func (c encryptionCommand) options(ctx context.Context) (xmlenc.Options, error) opt.OAEP = &xmlenc.OAEPParameters{Hash: h} if c.labelFile != "" { - opt.OAEP.Label, err = readInputContext(ctx, c.labelFile, nil, 1024) + opt.OAEP.Label, err = readInputContext(ctx, c.labelFile, nil, cliMaxOAEPLabelBytes) if err != nil { return opt, err } @@ -299,7 +308,7 @@ func (c encryptionCommand) operations(ctx context.Context, opt xmlenc.Options) ( return encryptionOperations{}, closeKey, usageError("private key format/password flags do not apply to raw AES keys") } - secret, err := readInputContext(ctx, c.key, nil, 32) + secret, err := readInputContext(ctx, c.key, nil, cliMaxAESKeyBytes) if err != nil { return encryptionOperations{}, closeKey, err } @@ -353,6 +362,12 @@ func (c encryptionCommand) externalize(ctx context.Context, stdin io.Reader, std return err } + // A new ciphertext file exposes case aliases that had no file identity + // during the initial check. Never replace it with the XML output. + if c.output != "-" && samePath(c.cipherOut, c.output) { + return usageError("ciphertext and XML outputs must be distinct") + } + return writeOutputContext(ctx, c.output, stdout, raw) } @@ -456,6 +471,39 @@ func samePath(a, b string) bool { } y, e := filepath.Abs(b) + if e != nil { + return false + } + + if x == y { + return true + } + + xInfo, xErr := os.Stat(a) + yInfo, yErr := os.Stat(b) + + if xErr == nil && yErr == nil { + return os.SameFile(xInfo, yInfo) + } + + // Missing targets can still name the same entry through directory aliases. + xParent, xName := filepath.Split(a) + yParent, yName := filepath.Split(b) + + if xName != yName { + return false + } + + if xParent == "" { + xParent = "." + } + + if yParent == "" { + yParent = "." + } + + xInfo, xErr = os.Stat(xParent) + yInfo, yErr = os.Stat(yParent) - return e == nil && x == y + return xErr == nil && yErr == nil && os.SameFile(xInfo, yInfo) } diff --git a/cmd/xmlsec/evidence_commands_test.go b/cmd/xmlsec/evidence_commands_test.go index e151ad5..244c0bb 100644 --- a/cmd/xmlsec/evidence_commands_test.go +++ b/cmd/xmlsec/evidence_commands_test.go @@ -67,6 +67,55 @@ func TestTimestampRequestInputCompletion(t *testing.T) { } } +func TestVerificationRejectsNewCaseAliasedOutputs(t *testing.T) { + dir := t.TempDir() + testutil.WriteFile(t, dir, "case-probe", nil) + if _, err := os.Stat(filepath.Join(dir, "CASE-PROBE")); errors.Is(err, os.ErrNotExist) { + t.Skip("filesystem distinguishes filename case") + } else if err != nil { + t.Fatal(err) + } + f := evidenceSetup(t) + content := []byte("authenticated content") + signed := cli(t, content, nameCMS, nameSign, flagKey, f.key, "-certificate", f.cert) + request := cli(t, content, "timestamp", nameRequest) + requestPath := testutil.WriteFile(t, f.dir, "request.tsq", request) + response := cli(t, request, "timestamp", nameIssue, flagKey, f.tsaKey, "-certificate", f.tsaCert, "-at", f.at, "-serial", "1", "-tsa-policy", "1.2.3.4") + parsed, err := timestamp.ParseResponse(t.Context(), response) + if err != nil { + t.Fatal(err) + } + message := []byte(``) + query := cli(t, message, "saml", nameRedirectSign, flagKey, f.key) + for _, tc := range []struct { + name string + input []byte + args []string + flag string + want []byte + }{ + {"CMS content", signed, []string{nameCMS, nameVerify, "-expect-certificate", f.cert}, "-extract", content}, + {"timestamp token", response, []string{"timestamp", nameVerify, "-request", requestPath, "-trust", f.tsaTrust, "-at", f.at}, "-token-out", parsed.TokenDER()}, + {"SAML XML", query, []string{"saml", nameRedirectVerify, flagKey, f.public}, "-extract", message}, + } { + t.Run(tc.name, func(t *testing.T) { + dir := t.TempDir() + payload := filepath.Join(dir, "payload") + report := filepath.Join(dir, "PAYLOAD") + args := append([]string(nil), tc.args...) + args = append(args, tc.flag, payload, "-out", report) + err := run(t.Context(), args, bytes.NewReader(tc.input), io.Discard, io.Discard) + if err == nil || exitCode(err) != 2 { + t.Fatal("accepted colliding payload and report paths", err) + } + data, err := os.ReadFile(payload) + if err != nil || !bytes.Equal(data, tc.want) { + t.Fatal("report replaced the authenticated payload", err) + } + }) + } +} + type timestampReadFunc func([]byte) (int, error) func (f timestampReadFunc) Read(p []byte) (int, error) { return f(p) } diff --git a/cmd/xmlsec/execution.go b/cmd/xmlsec/execution.go index 8a8371d..0c8d91e 100644 --- a/cmd/xmlsec/execution.go +++ b/cmd/xmlsec/execution.go @@ -21,6 +21,30 @@ import ( "github.com/skarm/xmlsec/xmldsig" ) +// Execution defaults and validation ceilings are distinct: callers select bounded work/output and signature policy. A zero command timeout leaves the outer context untimed; language engines retain their own invocation timeout. +const ( + cliDefaultMinimumPSSSaltBytes = 0 + cliDefaultOperationTimeout = 0 + cliDefaultOutputBytes = 32 << 20 + cliDefaultReferences = 16 + cliMaxReferences = 64 + cliDefaultCanonicalBytes = 32 << 20 + cliDefaultNodeVisits = 2_000_000 + cliDefaultTransforms = 8 + cliMaxTransforms = 64 + cliDefaultMaxRSABits = 8192 + cliMaxRSABits = 16384 + cliMaxPSSSaltBytes = 2048 + cliDefaultMaxPSSSaltBytes = 1024 + cliMinHMACKeyBytes = 32 + cliDefaultMaxHMACKeyBytes = 4096 + cliMinHMACOutputBits = 128 + cliMaxHMACOutputBits = 512 + cliMaxConfigBytes = 4 << 20 + cliMaxConfigDepth = 256 + cliOutputBufferBytes = 32 << 10 +) + type executionFlags struct { xml document.Limits output int @@ -48,7 +72,7 @@ func (e *executionFlags) start(ctx context.Context) (context.Context, context.Ca return ctx, func() {}, usageError("%v", err) } - if e.output < 1 || e.output > 64<<20 || e.xml.MaxBytes > 64<<20 || e.timeout < 0 || e.references < 1 || e.references > 64 || e.canonical < 1 || e.canonical > 64<<20 || e.nodeVisits < 1 || e.transforms < 1 || e.transforms > 64 { + if e.output < 1 || e.output > cliMaxBytes || e.xml.MaxBytes > cliMaxBytes || e.timeout < 0 || e.references < 1 || e.references > cliMaxReferences || e.canonical < 1 || e.canonical > cliMaxBytes || e.nodeVisits < 1 || e.transforms < 1 || e.transforms > cliMaxTransforms { return ctx, func() {}, usageError("invalid execution limits (byte limits cannot exceed 64 MiB)") } @@ -78,9 +102,9 @@ func (e *executionFlags) signaturePolicy(modern bool) (xmldsig.Policy, error) { if modern { p = xmldsig.ModernPolicy() } - case "modern": + case cliPolicyModern: p = xmldsig.ModernPolicy() - case "pss": + case cliPolicyPSS: p = xmldsig.ParameterizedPolicy() case nameHMAC: p = xmldsig.HMACPolicy() @@ -128,7 +152,12 @@ func (e *executionFlags) processing(ctx context.Context, maxResource int, allowe return opt, usageError("-allowed-programs requires 1..64 explicit programs") } - engine, err := transform.NewEngine(transform.Config{AllowedPrograms: programs, Limits: transform.Limits{MaxInputBytes: opt.MaxInputBytes, MaxOutputBytes: e.output, MaxNodes: e.xml.MaxNodes, MaxDepth: e.xml.MaxDepth, MaxOperations: e.nodeVisits}}) + engine, err := transform.NewEngine(transform.Config{AllowedPrograms: programs, Limits: transform.Limits{ + MaxInputBytes: opt.MaxInputBytes, MaxOutputBytes: e.output, + MaxNodes: e.xml.MaxNodes, MaxDepth: e.xml.MaxDepth, MaxOperations: e.nodeVisits, + MaxAttributesPerElement: e.xml.MaxAttributesPerElement, + MaxInScopeNamespaces: e.xml.MaxInScopeNamespaces, + }}) if err != nil { return opt, err } @@ -146,19 +175,19 @@ func (e *executionFlags) processing(ctx context.Context, maxResource int, allowe } func (e *executionFlags) validateCryptoLimits() error { - if e.minRSABits < 2048 || e.maxRSABits < e.minRSABits || e.maxRSABits > 16384 { + if e.minRSABits < cliMinRSABits || e.maxRSABits < e.minRSABits || e.maxRSABits > cliMaxRSABits { return usageError("RSA bounds require 2048 <= min <= max <= 16384") } - if e.minPSSSalt < 0 || e.maxPSSSalt < e.minPSSSalt || e.maxPSSSalt > 2048 { + if e.minPSSSalt < 0 || e.maxPSSSalt < e.minPSSSalt || e.maxPSSSalt > cliMaxPSSSaltBytes { return usageError("invalid PSS salt limits") } - if e.minHMACKey < 32 || e.maxHMACKey < e.minHMACKey || e.maxHMACKey > 1<<20 { + if e.minHMACKey < cliMinHMACKeyBytes || e.maxHMACKey < e.minHMACKey || e.maxHMACKey > cliMaxHMACKeyBytes { return usageError("invalid HMAC key bounds") } - if e.minHMACBits < 128 || e.minHMACBits > 512 || e.minHMACBits%8 != 0 { + if e.minHMACBits < cliMinHMACOutputBits || e.minHMACBits > cliMaxHMACOutputBits || e.minHMACBits%8 != 0 { return usageError("HMAC output floor must be 128..512 bits in multiples of 8") } @@ -187,7 +216,7 @@ func consumeInput(ctx context.Context, path string, stdin io.Reader, maximum int return err } - if maximum < 1 || maximum > 64<<20 { + if maximum < 1 || maximum > cliMaxBytes { return usageError("input byte limit must be 1..67108864") } @@ -245,15 +274,15 @@ func openRegular(path string) (*os.File, error) { return nil, err } - stat, err = f.Stat() - if err != nil || !stat.Mode().IsRegular() { + openedStat, statErr := f.Stat() + if statErr != nil || !openedStat.Mode().IsRegular() { _ = f.Close() - if err == nil { - err = errors.New("input must be a regular file") + if statErr == nil { + statErr = errors.New("input must be a regular file") } - return nil, err + return nil, statErr } return f, nil @@ -280,7 +309,7 @@ func readJSONFile(ctx context.Context, path string, target any) error { return usageError("configuration must be a trusted local JSON file") } - raw, err := readInputContext(ctx, path, nil, 4<<20) + raw, err := readInputContext(ctx, path, nil, cliMaxConfigBytes) if err != nil { return err } @@ -307,7 +336,7 @@ func decodeJSON(raw []byte, target any) error { } func uniqueJSON(d *json.Decoder, depth int) error { - if depth > 256 { + if depth > cliMaxConfigDepth { return errors.New("JSON nesting exceeds 256") } @@ -380,23 +409,23 @@ func registerExecutionFlags(fs *flag.FlagSet) *executionFlags { fs.IntVar(&e.xml.MaxAttributesPerElement, "max-attributes", e.xml.MaxAttributesPerElement, "maximum attributes/namespaces per XML element") fs.IntVar(&e.xml.MaxNodeSetNodes, "max-nodeset-nodes", e.xml.MaxNodeSetNodes, "maximum expanded node-set nodes") fs.IntVar(&e.xml.MaxInScopeNamespaces, "max-namespaces", e.xml.MaxInScopeNamespaces, "maximum in-scope namespaces") - fs.IntVar(&e.output, "max-output-bytes", 32<<20, "maximum output and transform output bytes") - fs.IntVar(&e.references, "max-references", 16, "maximum direct signature references (1..64)") - fs.IntVar(&e.canonical, "max-canonical-bytes", 32<<20, "aggregate signature digest/canonical byte budget") - fs.IntVar(&e.nodeVisits, "max-node-visits", 2_000_000, "aggregate signature transform node visits") - fs.IntVar(&e.transforms, "max-transforms", 8, "maximum transforms per reference (1..64)") - fs.DurationVar(&e.timeout, "timeout", 0, "operation deadline, e.g. 30s; 0 has no deadline") + fs.IntVar(&e.output, "max-output-bytes", cliDefaultOutputBytes, "maximum output and transform output bytes") + fs.IntVar(&e.references, "max-references", cliDefaultReferences, "maximum direct signature references (1..64)") + fs.IntVar(&e.canonical, "max-canonical-bytes", cliDefaultCanonicalBytes, "aggregate signature digest/canonical byte budget") + fs.IntVar(&e.nodeVisits, "max-node-visits", cliDefaultNodeVisits, "aggregate signature transform node visits") + fs.IntVar(&e.transforms, "max-transforms", cliDefaultTransforms, "maximum transforms per reference (1..64)") + fs.DurationVar(&e.timeout, "timeout", cliDefaultOperationTimeout, "operation deadline, e.g. 30s; 0 has no deadline") fs.StringVar(&e.policy, "policy", nameDefault, "signature policy: default, modern, pss or hmac (explicit shared secret)") fs.StringVar(&e.methods, "allowed-methods", "", "comma-separated signature method allowlist; overrides selected policy") fs.StringVar(&e.digests, "allowed-digests", "", "comma-separated digest method allowlist") fs.StringVar(&e.programs, "allowed-programs", "", "trusted local JSON array of exact approved XPath/Filter2/XSLT transforms") - fs.IntVar(&e.minRSABits, "min-rsa-bits", 2048, "minimum RSA modulus size") - fs.IntVar(&e.maxRSABits, "max-rsa-bits", 8192, "maximum RSA modulus size (up to 16384)") - fs.IntVar(&e.minPSSSalt, "min-pss-salt-bytes", 0, "minimum generic RSA-PSS salt length") - fs.IntVar(&e.maxPSSSalt, "max-pss-salt-bytes", 1024, "maximum generic RSA-PSS salt length") - fs.IntVar(&e.minHMACKey, "min-hmac-key-bytes", 32, "minimum HMAC key bytes") - fs.IntVar(&e.maxHMACKey, "max-hmac-key-bytes", 4096, "maximum HMAC key bytes") - fs.IntVar(&e.minHMACBits, "min-hmac-output-bits", 128, "minimum explicitly permitted HMAC tag size") + fs.IntVar(&e.minRSABits, "min-rsa-bits", cliMinRSABits, "minimum RSA modulus size") + fs.IntVar(&e.maxRSABits, "max-rsa-bits", cliDefaultMaxRSABits, "maximum RSA modulus size (up to 16384)") + fs.IntVar(&e.minPSSSalt, "min-pss-salt-bytes", cliDefaultMinimumPSSSaltBytes, "minimum generic RSA-PSS salt length") + fs.IntVar(&e.maxPSSSalt, "max-pss-salt-bytes", cliDefaultMaxPSSSaltBytes, "maximum generic RSA-PSS salt length") + fs.IntVar(&e.minHMACKey, "min-hmac-key-bytes", cliMinHMACKeyBytes, "minimum HMAC key bytes") + fs.IntVar(&e.maxHMACKey, "max-hmac-key-bytes", cliDefaultMaxHMACKeyBytes, "maximum HMAC key bytes") + fs.IntVar(&e.minHMACBits, "min-hmac-output-bits", cliMinHMACOutputBits, "minimum explicitly permitted HMAC tag size") fs.BoolVar(&e.allowHMACTruncation, "allow-hmac-truncation", false, "explicitly allow bounded truncated HMAC tags") return e @@ -407,7 +436,7 @@ func execution(ctx context.Context) *executionFlags { return e } - return &executionFlags{xml: document.DefaultLimits(), output: 32 << 20, references: 16, canonical: 32 << 20, nodeVisits: 2_000_000, transforms: 8, policy: nameDefault} + return &executionFlags{xml: document.DefaultLimits(), output: cliDefaultOutputBytes, references: cliDefaultReferences, canonical: cliDefaultCanonicalBytes, nodeVisits: cliDefaultNodeVisits, transforms: cliDefaultTransforms, policy: nameDefault} } func writeOutputContext(ctx context.Context, path string, w io.Writer, b []byte) error { @@ -460,7 +489,7 @@ func parseFlags(fs *flag.FlagSet, args []string) (bool, error) { func parseHash(s string) (crypto.Hash, error) { switch strings.ToLower(s) { - case "sha256", "sha-256": + case cliHashSHA256, "sha-256": return crypto.SHA256, nil case "sha384", "sha-384": return crypto.SHA384, nil @@ -523,7 +552,7 @@ func produceOutput(ctx context.Context, path string, w io.Writer, produce func(i err := writeAtomicOutputFunc(ctx, path, w, func(target io.Writer) error { // Serializers emit short markup and 4 KiB escaped text fragments. Batch // disk writes without collecting the complete serialized document. - buffered := bufio.NewWriterSize(target, 32<<10) + buffered := bufio.NewWriterSize(target, cliOutputBufferBytes) if err := produce(&streamio.Writer{Context: ctx, Target: buffered, Limit: int64(limit), LimitError: limitErr}); err != nil { return err } diff --git a/cmd/xmlsec/key_commands.go b/cmd/xmlsec/key_commands.go index 5753d4b..31f3624 100644 --- a/cmd/xmlsec/key_commands.go +++ b/cmd/xmlsec/key_commands.go @@ -13,9 +13,21 @@ import ( "flag" "io" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/keys" ) +// Key-generation and export defaults belong to this command. Supported algorithm labels are fixed identities, independent of the selected default. +const ( + cliDefaultGeneratedRSABits = 3072 + cliMaxGeneratedRSABits = 8192 + cliRSAGenerationStepBits = 256 + cliDefaultExportIterations = 600_000 + cliDefaultExportAESBits = 256 + cliDefaultKeyAlgorithm = cliAlgorithmP256 + cliAlgorithmP256 = "p256" +) + func runKey(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) error { cmd := args[0] fs := flag.NewFlagSet("key "+cmd, flag.ContinueOnError) @@ -23,8 +35,8 @@ func runKey(ctx context.Context, args []string, stdin io.Reader, stdout, stderr exec := registerExecutionFlags(fs) input := fs.String("in", "", "input key/certificate/CRL file") output := fs.String("out", "-", "output path; private material is created with mode 0600") - algorithm := fs.String("algorithm", "p256", "generate: rsa, p256, p384, p521, ed25519, aes128, aes192, aes256 or hmac256/384/512") - bits := fs.Int("bits", 3072, "RSA modulus bits (2048..8192)") + algorithm := fs.String("algorithm", cliDefaultKeyAlgorithm, "generate: rsa, p256, p384, p521, ed25519, aes128, aes192, aes256 or hmac256/384/512") + bits := fs.Int("bits", cliDefaultGeneratedRSABits, "RSA modulus bits (2048..8192)") format := fs.String("output-format", namePlain, "private output: plain (PKCS#8), pkcs8 (encrypted) or pkcs12") outputPassword := fs.String("output-password-file", "", "password for encrypted output") pemOutput := fs.Bool("pem", true, "PEM output; false writes DER (PKCS#12 and symmetric keys always use raw bytes)") @@ -33,9 +45,9 @@ func runKey(ctx context.Context, args []string, stdin io.Reader, stdout, stderr var extras repeatedFlag fs.Var(&extras, "extra-certificate", "extra PKCS#12 certificate; repeatable") - iterations := fs.Int("iterations", 600000, "PBKDF2 iterations for encrypted export") - hashName := fs.String("hash", "sha256", "PBKDF2 SHA-2 PRF") - encryptionBits := fs.Int("encryption-bits", 256, "encrypted key AES bits: 128, 192 or 256") + iterations := fs.Int("iterations", cliDefaultExportIterations, "PBKDF2 iterations for encrypted export") + hashName := fs.String("hash", cliDefaultHashName, "PBKDF2 SHA-2 PRF") + encryptionBits := fs.Int("encryption-bits", cliDefaultExportAESBits, "encrypted key AES bits: 128, 192 or 256") classic := fs.Bool("classic-mac", false, "use classic SHA-2 PKCS#12 MAC instead of PBMAC1") flags := registerPrivateKeyFlags(fs) @@ -161,12 +173,12 @@ func (c keyCommand) generate() (crypto.Signer, []byte, error) { switch c.algorithm { case nameRSA: - if c.bits < 2048 || c.bits > 8192 || c.bits%256 != 0 { + if c.bits < cliMinRSABits || c.bits > cliMaxGeneratedRSABits || c.bits%cliRSAGenerationStepBits != 0 { return nil, nil, usageError("RSA -bits must be a multiple of 256 in 2048..8192") } signer, err = rsa.GenerateKey(rand.Reader, c.bits) - case "p256", "p384", "p521": + case cliAlgorithmP256, "p384", "p521": curve := elliptic.P256() if c.algorithm == "p384" { curve = elliptic.P384() @@ -290,7 +302,7 @@ func (c keyCommand) encode(ctx context.Context, signer crypto.Signer) ([]byte, e if c.pem { defer clear(raw) - return pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: raw}), nil + return pem.EncodeToMemory(&pem.Block{Type: lexicon.PEMPrivateKey, Bytes: raw}), nil } return raw, nil @@ -309,7 +321,7 @@ func (c keyCommand) encode(ctx context.Context, signer crypto.Signer) ([]byte, e encryption := keys.EncryptionOptions{Iterations: c.iterations, Hash: hash, KeyBits: c.encryptionBits} switch c.format { - case namePkcs8: + case namePKCS8: raw, err := keys.MarshalEncryptedPrivate(ctx, signer, password, encryption, c.flags.limits()) if err != nil { return nil, err @@ -317,7 +329,7 @@ func (c keyCommand) encode(ctx context.Context, signer crypto.Signer) ([]byte, e if c.pem { defer clear(raw) - return pem.EncodeToMemory(&pem.Block{Type: "ENCRYPTED PRIVATE KEY", Bytes: raw}), nil + return pem.EncodeToMemory(&pem.Block{Type: lexicon.PEMEncryptedPrivateKey, Bytes: raw}), nil } return raw, nil @@ -359,7 +371,7 @@ func exportPublic(ctx context.Context, path string, w io.Writer, public crypto.P } if asPEM { - raw = pem.EncodeToMemory(&pem.Block{Type: "PUBLIC KEY", Bytes: raw}) + raw = pem.EncodeToMemory(&pem.Block{Type: lexicon.PEMPublicKey, Bytes: raw}) } return writeOutputContext(ctx, path, w, raw) diff --git a/cmd/xmlsec/keyinfo_commands.go b/cmd/xmlsec/keyinfo_commands.go index ede2f92..4b6845f 100644 --- a/cmd/xmlsec/keyinfo_commands.go +++ b/cmd/xmlsec/keyinfo_commands.go @@ -6,6 +6,7 @@ import ( "io" "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/keyinfo" ) @@ -17,8 +18,8 @@ func runKeyInfo(ctx context.Context, args []string, stdin io.Reader, stdout, std input := fs.String("in", "-", "XML input for inspect") output := fs.String("out", "-", "output XML or diagnostic JSON") id := fs.String("id", "", "KeyInfo Id (build) or exact selection ID (inspect)") - index := fs.Int("index", -1, "explicit zero-based KeyInfo index") - format := fs.String("format", "der", "public key representation: der or xml") + index := fs.Int("index", cliUnspecifiedSelectionIndex, "explicit zero-based KeyInfo index") + format := fs.String("format", string(keyinfo.DERKeyValue), "public key representation: der or xml") resources := fs.String("resources", "", "separate trusted URI/path allowlist for external KeyInfo retrieval") configFile := fs.String("config", "", "trusted JSON keyinfo.Options for inspection limits, ID rules and allowed representations") baseURI := fs.String("base-uri", "", "explicit KeyInfo document base URI; requires -resources") @@ -29,7 +30,7 @@ func runKeyInfo(ctx context.Context, args []string, stdin io.Reader, stdout, std fs.Var(&certs, "certificate", "certificate file; repeatable") fs.Var(&refs, "reference", "KeyInfoReference URI; repeatable") - specific := "in out id index resources config base-uri" + specific := "in out id index resources config base-uri allowed-programs" if cmd == "build" { specific = "out id format name key certificate reference" } @@ -60,7 +61,7 @@ func runKeyInfo(ctx context.Context, args []string, stdin io.Reader, stdout, std return err } - selected, err := selectElement(doc, document.Name{Local: "Id"}, *id, *index, document.Name{Space: keyinfo.Namespace, Local: "KeyInfo"}) + selected, err := selectElement(doc, document.Name{Local: lexicon.SecurityAttrID}, *id, *index, document.Name{Space: keyinfo.Namespace, Local: lexicon.DSigElementKeyInfo}) if err != nil { return err } @@ -144,7 +145,7 @@ func inspectionKeyInfoOptions(ctx context.Context, exec *executionFlags, configF } func buildKeyInfoCLI(ctx context.Context, stdout io.Writer, output string, spec keyinfo.Template, keys, certs []string, format string) error { - if format != "der" && format != "xml" { + if format != string(keyinfo.DERKeyValue) && format != string(keyinfo.XMLKeyValue) { return usageError("-format must be der or xml") } diff --git a/cmd/xmlsec/keys.go b/cmd/xmlsec/keys.go index e83eafa..43be154 100644 --- a/cmd/xmlsec/keys.go +++ b/cmd/xmlsec/keys.go @@ -12,6 +12,13 @@ import ( "github.com/skarm/xmlsec/keys" ) +// Password-line input allows the password plus CRLF, then rejects embedded line breaks. Certificate vectors are counted separately from each DER file limit. +const ( + cliMaxPasswordBytes = 1024 + cliMaxPasswordLineBytes = cliMaxPasswordBytes + 2 + cliMaxCertificateFiles = 128 +) + type privateKeyFlags struct { format string passwordFile string @@ -28,10 +35,10 @@ func registerPrivateKeyFlags(fs *flag.FlagSet) *privateKeyFlags { f := &privateKeyFlags{} fs.StringVar(&f.format, "key-format", namePlain, "private key format: plain, pkcs8 (encrypted), pkcs12") fs.StringVar(&f.passwordFile, "password-file", "", "trusted regular file with one password line; no password in arguments") - fs.IntVar(&f.maxKDFWork, "key-max-kdf-work", 3000000, "aggregate private key KDF iteration budget") + fs.IntVar(&f.maxKDFWork, "key-max-kdf-work", keys.DefaultLimits().MaxKDFWork, "aggregate private key KDF iteration budget") - fs.IntVar(&f.minIterations, "key-min-iterations", 1000, "minimum permitted import KDF iterations") - fs.IntVar(&f.maxIterations, "key-max-iterations", 1000000, "maximum permitted iterations per KDF") + fs.IntVar(&f.minIterations, "key-min-iterations", keys.DefaultLimits().MinIterations, "minimum permitted import KDF iterations") + fs.IntVar(&f.maxIterations, "key-max-iterations", keys.DefaultLimits().MaxIterations, "maximum permitted iterations per KDF") return f } @@ -45,7 +52,7 @@ func loadPrivateConfigured(ctx context.Context, path string, flags *privateKeyFl return loadPrivateContext(ctx, path) } - if flags.format != namePkcs8 && flags.format != namePKCS12 { + if flags.format != namePKCS8 && flags.format != namePKCS12 { return nil, errors.New("-key-format must be plain, pkcs8 or pkcs12") } @@ -62,7 +69,7 @@ func loadPrivateConfigured(ctx context.Context, path string, flags *privateKeyFl return nil, errors.New("password file must be a trusted regular file") } - password, err := readInputContext(ctx, flags.passwordFile, nil, 1026) + password, err := readInputContext(ctx, flags.passwordFile, nil, cliMaxPasswordLineBytes) if err != nil { return nil, err } @@ -75,7 +82,7 @@ func loadPrivateConfigured(ctx context.Context, path string, flags *privateKeyFl } } - if len(password) > 1024 || bytes.ContainsAny(password, "\r\n\x00") { + if len(password) > cliMaxPasswordBytes || bytes.ContainsAny(password, "\r\n\x00") { return nil, errors.New("password file requires one line of at most 1024 bytes") } @@ -86,7 +93,7 @@ func loadPrivateConfigured(ctx context.Context, path string, flags *privateKeyFl defer clear(raw) limits := flags.limits() - if flags.format == namePkcs8 { + if flags.format == namePKCS8 { return keys.ParseEncryptedPrivate(ctx, raw, password, limits) } @@ -135,7 +142,7 @@ func readCertificate(ctx context.Context, path string) (*x509.Certificate, error } func readCertificates(ctx context.Context, base string, paths []string) ([][]byte, error) { - if len(paths) > 128 { + if len(paths) > cliMaxCertificateFiles { return nil, errors.New("too many certificate files") } @@ -157,7 +164,7 @@ func passwordFromFile(ctx context.Context, path string) ([]byte, error) { return nil, usageError("password requires a trusted regular file") } - password, err := readInputContext(ctx, path, nil, 1026) + password, err := readInputContext(ctx, path, nil, cliMaxPasswordLineBytes) if err != nil { return nil, err } @@ -169,7 +176,7 @@ func passwordFromFile(ctx context.Context, path string) ([]byte, error) { } } - if len(password) > 1024 || bytes.ContainsAny(password, "\r\n\x00") { + if len(password) > cliMaxPasswordBytes || bytes.ContainsAny(password, "\r\n\x00") { clear(password) return nil, usageError("password file requires one line of at most 1024 bytes") } diff --git a/cmd/xmlsec/keys_test.go b/cmd/xmlsec/keys_test.go index a358c21..ca30764 100644 --- a/cmd/xmlsec/keys_test.go +++ b/cmd/xmlsec/keys_test.go @@ -9,10 +9,15 @@ import ( "crypto/elliptic" "crypto/rand" "crypto/rsa" + "crypto/sha256" "crypto/x509" "crypto/x509/pkix" + "encoding/hex" + "encoding/json" "encoding/pem" + "errors" "fmt" + "io" "math/big" "os" "path/filepath" @@ -21,8 +26,11 @@ import ( "time" "github.com/skarm/xmlsec/c14n" + "github.com/skarm/xmlsec/internal/testutil" "github.com/skarm/xmlsec/internal/testutil/testcert" + "github.com/skarm/xmlsec/keyinfo" "github.com/skarm/xmlsec/keys" + "github.com/skarm/xmlsec/reference" "github.com/skarm/xmlsec/xmldsig" ) @@ -145,7 +153,7 @@ func TestCLIProtectedKeys(t *testing.T) { resources := write("resources.json", []byte(`[{"uri":"urn:test:data","path":"payload.xml"}]`)) template := fmt.Sprintf(`payload`, xmldsig.Namespace, c14n.Exclusive, xmldsig.ECDSASHA256, c14n.Exclusive, xmldsig.SHA256) - for format, raw := range map[string][]byte{namePkcs8: pkcs8, namePKCS12: pfx} { + for format, raw := range map[string][]byte{namePKCS8: pkcs8, namePKCS12: pfx} { private := write(format, raw) for _, mode := range []string{nameSign, nameSignTemplate, nameSignDetached} { t.Run(format+"/"+mode, func(t *testing.T) { @@ -157,7 +165,7 @@ func TestCLIProtectedKeys(t *testing.T) { private := write("encrypted.der", pkcs8) for _, content := range [][]byte{[]byte("one\ntwo\n"), []byte("one\x00two"), []byte("one\r"), bytes.Repeat([]byte{'x'}, 1025)} { bad := write("invalid-password.txt", content) - if key, err := loadPrivateConfigured(ctx, private, &privateKeyFlags{format: namePkcs8, passwordFile: bad, maxKDFWork: 3000000}); key != nil || err == nil { + if key, err := loadPrivateConfigured(ctx, private, &privateKeyFlags{format: namePKCS8, passwordFile: bad, maxKDFWork: 3000000}); key != nil || err == nil { t.Fatal("invalid password file accepted") } } @@ -165,11 +173,11 @@ func TestCLIProtectedKeys(t *testing.T) { for _, flags := range []*privateKeyFlags{ {format: namePlain, passwordFile: pwd}, {format: nameUnknown}, - {format: namePkcs8}, - {format: namePkcs8, passwordFile: "-"}, - {format: namePkcs8, passwordFile: dir}, - {format: namePkcs8, passwordFile: filepath.Join(dir, "missing")}, - {format: namePkcs8, passwordFile: pwd, maxKDFWork: 999}, + {format: namePKCS8}, + {format: namePKCS8, passwordFile: "-"}, + {format: namePKCS8, passwordFile: dir}, + {format: namePKCS8, passwordFile: filepath.Join(dir, "missing")}, + {format: namePKCS8, passwordFile: pwd, maxKDFWork: 999}, } { if key, err := loadPrivateConfigured(ctx, private, flags); key != nil || err == nil { t.Fatal("invalid key-loading options accepted") @@ -269,3 +277,115 @@ func TestCLIKeyMaterialRejectsAmbiguity(t *testing.T) { t.Fatal("mismatched PEM label accepted") } } + +func TestKeyInfoInspectionApprovedPrograms(t *testing.T) { + var corpus struct { + PublicKey string `json:"public_key_pem"` + Cases []stage4CLICase + } + + raw, err := os.ReadFile(filepath.Join("..", "..", "testdata", "next", "dsig-stage4-vectors.json")) + if err != nil { + t.Fatal(err) + } + + if err := json.Unmarshal(raw, &corpus); err != nil { + t.Fatal(err) + } + + block, rest := pem.Decode([]byte(corpus.PublicKey)) + if block == nil || block.Type != "PUBLIC KEY" || len(bytes.TrimSpace(rest)) != 0 { + t.Fatal("missing independent SPKI pin") + } + + digest := sha256.Sum256(block.Bytes) + wantFingerprint := hex.EncodeToString(digest[:]) + fixtures := make(map[string]stage4CLICase, len(corpus.Cases)) + for _, fixture := range corpus.Cases { + fixtures[fixture.Name] = fixture + } + + for _, name := range []string{"retrieval-xpath", "retrieval-filter2", "retrieval-xslt", "retrieval-prefixlist"} { + fixture, ok := fixtures[name] + if !ok { + t.Fatalf("missing independent fixture %s", name) + } + + t.Run(name, func(t *testing.T) { + testKeyInfoInspectionProgram(t, fixture, wantFingerprint) + }) + } + + dir := t.TempDir() + public := testutil.WriteFile(t, dir, "public.pem", []byte(corpus.PublicKey)) + programs := testutil.WriteJSON(t, dir, "programs.json", []reference.Transform{{Algorithm: reference.XPath, XPath: []reference.XPathExpression{{Expression: "false()"}}}}) + var output bytes.Buffer + if err := run(t.Context(), []string{"keyinfo", "build", "-key", public, "-allowed-programs", programs}, nil, &output, io.Discard); err == nil || output.Len() != 0 { + t.Fatal("inspection-only option accepted for build", err) + } +} + +func testKeyInfoInspectionProgram(t *testing.T, fixture stage4CLICase, wantFingerprint string) { + t.Helper() + dir := t.TempDir() + entries := make([]resourceEntry, 0, len(fixture.Resources)) + for uri, data := range fixture.Resources { + path := testutil.WriteFile(t, dir, fmt.Sprintf("resource-%d.xml", len(entries)), data) + entries = append(entries, resourceEntry{URI: uri, Path: path}) + } + + resources := testutil.WriteJSON(t, dir, "resources.json", entries) + processing := reference.DefaultOptions() + for _, program := range fixture.Programs { + processing.AllowedTransforms = append(processing.AllowedTransforms, program.Algorithm) + } + + config := testutil.WriteJSON(t, dir, "keyinfo.json", keyinfo.Options{RetrievalProcessing: &processing}) + args := []string{"keyinfo", nameInspect, "-index", "0", "-config", config, "-resources", resources} + t.Run("approved", func(t *testing.T) { + approved := append([]string(nil), args...) + if len(fixture.Programs) != 0 { + programs := testutil.WriteJSON(t, dir, "programs.json", fixture.Programs) + approved = append(approved, "-allowed-programs", programs) + } + + var output bytes.Buffer + if err := run(t.Context(), approved, strings.NewReader(fixture.XML), &output, io.Discard); err != nil { + t.Fatal(err) + } + + var report struct { + Status string + Candidates []struct { + Fingerprint string `json:"spki_sha256"` + } + } + + if err := json.Unmarshal(output.Bytes(), &report); err != nil { + t.Fatal(err) + } + + if report.Status != statusUnverified || len(report.Candidates) != 1 || report.Candidates[0].Fingerprint != wantFingerprint { + t.Fatal("inspection changed untrusted candidate", output.String()) + } + }) + if len(fixture.Programs) == 0 { + return + } + + for _, changed := range []bool{false, true} { + t.Run(fmt.Sprintf("changed-approval=%t", changed), func(t *testing.T) { + denied := append([]string(nil), args...) + if changed { + programs := testutil.WriteJSON(t, dir, "different.json", []reference.Transform{{Algorithm: reference.XPath, XPath: []reference.XPathExpression{{Expression: "false()"}}}}) + denied = append(denied, "-allowed-programs", programs) + } + + var output bytes.Buffer + err := run(t.Context(), denied, strings.NewReader(fixture.XML), &output, io.Discard) + if !errors.Is(err, keyinfo.ErrPolicy) || output.Len() != 0 { + t.Fatal("unapproved retrieval program accepted or lost policy error", err, output.String()) + } + }) + } +} diff --git a/cmd/xmlsec/main.go b/cmd/xmlsec/main.go index 2cd195c..4bfc876 100644 --- a/cmd/xmlsec/main.go +++ b/cmd/xmlsec/main.go @@ -39,7 +39,7 @@ func main() { if err != nil { if jsonErrors { - report := map[string]any{"schema_version": 1, fieldStatus: "error", "error": err.Error(), "exit_code": exitCode(err)} + report := map[string]any{"schema_version": cliReportSchemaVersion, fieldStatus: "error", "error": err.Error(), "exit_code": exitCode(err)} if diagnostics.Len() > 0 { report["diagnostics"] = diagnostics.String() } @@ -65,7 +65,7 @@ func runXML(ctx context.Context, args []string, stdin io.Reader, stdout, stderr exec := registerExecutionFlags(fs) input := fs.String("in", "-", "input XML file; - reads stdin") output := fs.String("out", "-", "output file; - writes stdout") - idAttr := fs.String("id-attr", "ID", "ID attribute local name (not guessed)") + idAttr := fs.String("id-attr", cliDefaultIDAttribute, "ID attribute local name (not guessed)") idNS := fs.String("id-ns", "", "ID attribute namespace URI") modern := fs.Bool("modern-algorithms", false, "also permit fixed RSA-PSS, Ed25519 and Ed25519ph") @@ -91,7 +91,7 @@ func runXML(ctx context.Context, args []string, stdin io.Reader, stdout, stderr fs.StringVar(&keyFile, "key", "", "externally trusted public key or pinned certificate, PEM/DER") fs.StringVar(&rootLocal, "root-local", "", "expected root local name") fs.StringVar(&rootNS, "root-ns", "", "expected root namespace URI") - case nameC14n: + case nameC14N: fs.StringVar(&xpathExpression, nameXPath, "", "XPath 1.0 node-set filter (evaluated for each node)") fs.Var(&xpathNamespaces, "namespace", "PREFIX=URI for -xpath; repeatable") fs.StringVar(&canonicalAlgorithm, "algorithm", c14n.Exclusive, "canonicalization URI: Exclusive, C14N 1.0 or C14N 1.1") @@ -100,7 +100,7 @@ func runXML(ctx context.Context, args []string, stdin io.Reader, stdout, stderr fs.StringVar(&selectID, "id", "", "canonicalize the unique element with this ID; default whole document") } - if cmd == nameC14n { + if cmd == nameC14N { fs = commandFlags(fs, "in out id-attr id-ns algorithm comments inclusive-prefixes id xpath namespace max-node-visits", false) } else { fs = withoutFlags(fs, "allowed-programs max-node-visits max-transforms") @@ -114,7 +114,7 @@ func runXML(ctx context.Context, args []string, stdin io.Reader, stdout, stderr return errors.New("unexpected positional arguments") } - if cmd != nameC14n && keyFile == "" { + if cmd != nameC14N && keyFile == "" { return usageError("-key is required") } @@ -150,7 +150,7 @@ func runXML(ctx context.Context, args []string, stdin io.Reader, stdout, stderr var out []byte switch cmd { - case nameC14n: + case nameC14N: opt := c14n.Options{MaxOutputBytes: exec.output, Algorithm: canonicalAlgorithm, WithComments: comments, InclusivePrefixes: strings.Fields(inclusive)} return produceOutput(ctx, *output, stdout, func(w io.Writer) error { diff --git a/cmd/xmlsec/main_test.go b/cmd/xmlsec/main_test.go index 54f646c..b737ec2 100644 --- a/cmd/xmlsec/main_test.go +++ b/cmd/xmlsec/main_test.go @@ -89,7 +89,7 @@ func TestCLI(t *testing.T) { } var canonical bytes.Buffer - if err := run(t.Context(), []string{nameC14n}, strings.NewReader(``), &canonical, &stderr); err != nil { + if err := run(t.Context(), []string{nameC14N}, strings.NewReader(``), &canonical, &stderr); err != nil { t.Fatal(err) } @@ -108,7 +108,7 @@ func TestCLI(t *testing.T) { } func TestCLIRejects(t *testing.T) { - for _, args := range [][]string{{nameBogus}, {nameSign}, {nameVerify}, {nameSign, flagKey, "-"}, {nameC14n, "unexpected"}, {nameC14n, "-id", "missing"}} { + for _, args := range [][]string{{nameBogus}, {nameSign}, {nameVerify}, {nameSign, flagKey, "-"}, {nameC14N, "unexpected"}, {nameC14N, "-id", "missing"}} { t.Run(strings.Join(args, " "), func(t *testing.T) { var out, errout bytes.Buffer if err := run(t.Context(), args, strings.NewReader(``), &out, &errout); err == nil { @@ -122,7 +122,7 @@ func TestCLICanonicalAlgorithms(t *testing.T) { for _, alg := range []string{c14n.Canonical10, c14n.Canonical11, c14n.Exclusive} { t.Run(alg, func(t *testing.T) { var out, errout bytes.Buffer - if err := run(t.Context(), []string{nameC14n, "-algorithm", alg}, + if err := run(t.Context(), []string{nameC14N, "-algorithm", alg}, strings.NewReader(``), &out, &errout); err != nil { t.Fatal(err) } @@ -139,7 +139,7 @@ func TestCLICanonicalAlgorithms(t *testing.T) { } var out, errout bytes.Buffer - if err := run(t.Context(), []string{nameC14n, "-algorithm", nameUnknown}, + if err := run(t.Context(), []string{nameC14N, "-algorithm", nameUnknown}, strings.NewReader(``), &out, &errout); err == nil { t.Fatal("unknown algorithm accepted") } diff --git a/cmd/xmlsec/pki_commands.go b/cmd/xmlsec/pki_commands.go index dfcce2b..f584c57 100644 --- a/cmd/xmlsec/pki_commands.go +++ b/cmd/xmlsec/pki_commands.go @@ -11,6 +11,14 @@ import ( "github.com/skarm/xmlsec/pki" ) +// OCSP input uses a bounded nonce and response size. Zero freshness/skew values are passed to the existing PKI default policy. +const ( + cliDefaultOCSPMaxAge = 0 + cliDefaultOCSPClockSkew = 0 + cliGeneratedNonceBytes = 32 + cliMaxOCSPResponseBytes = 8 << 20 +) + func runPKI(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) error { cmd := args[0] fs := flag.NewFlagSet("pki "+cmd, flag.ContinueOnError) @@ -22,13 +30,13 @@ func runPKI(ctx context.Context, args []string, stdin io.Reader, stdout, stderr issuer := fs.String(fieldIssuer, "", "explicit issuer certificate PEM/DER") trustFile := fs.String("trust", "", "trusted local JSON trust/evidence configuration") atValue := fs.String("at", "", "explicit verification time, RFC3339") - hashName := fs.String("hash", "sha256", "OCSP CertID hash") + hashName := fs.String("hash", cliDefaultHashName, "OCSP CertID hash") nonceFile := fs.String("nonce-file", "", "exact expected/request nonce file; request generates random nonce if omitted") responder := fs.String("responder", "", "OCSP responder certificate") keyFile := fs.String("key", "", "responder private key") config := fs.String("config", "", "trusted JSON pki.OCSPResponseTemplate for issuance") - maxAge := fs.Duration("max-age", 0, "OCSP freshness maximum age; 0 uses library default") - skew := fs.Duration("clock-skew", 0, "OCSP clock skew tolerance") + maxAge := fs.Duration("max-age", cliDefaultOCSPMaxAge, "OCSP freshness maximum age; 0 uses library default") + skew := fs.Duration("clock-skew", cliDefaultOCSPClockSkew, "OCSP clock skew tolerance") flags := registerPrivateKeyFlags(fs) specific := "out certificate" @@ -73,7 +81,7 @@ func runPKI(ctx context.Context, args []string, stdin io.Reader, stdout, stderr var nonce []byte if *nonceFile != "" { - nonce, err = readInputContext(ctx, *nonceFile, nil, 128) + nonce, err = readInputContext(ctx, *nonceFile, nil, cliMaxNonceBytes) if err != nil { return err } @@ -86,7 +94,7 @@ func runPKI(ctx context.Context, args []string, stdin io.Reader, stdout, stderr } if nonce == nil { - nonce = make([]byte, 32) + nonce = make([]byte, cliGeneratedNonceBytes) if _, err := rand.Read(nonce); err != nil { return err } @@ -170,7 +178,7 @@ func verifyOCSPCLI(ctx context.Context, raw, certificate, issuer, nonce []byte, crls = evidence.CRLs } - _, report, verifyErr := pki.VerifyOCSP(ctx, raw, certificate, issuer, at, pki.OCSPOptions{ExpectedNonce: nonce, MaxAge: maxAge, ClockSkew: skew, MaxBytes: min(execution(ctx).xml.MaxBytes, 8<<20), ResponderCRLs: crls}) + _, report, verifyErr := pki.VerifyOCSP(ctx, raw, certificate, issuer, at, pki.OCSPOptions{ExpectedNonce: nonce, MaxAge: maxAge, ClockSkew: skew, MaxBytes: min(execution(ctx).xml.MaxBytes, cliMaxOCSPResponseBytes), ResponderCRLs: crls}) return errors.Join(verifyErr, jsonOutput(ctx, output, stdout, resultReport("pki ocsp-verify", "explicit-issuer", report, verifyErr))) } diff --git a/cmd/xmlsec/process_test.go b/cmd/xmlsec/process_test.go index ff8113f..25686f4 100644 --- a/cmd/xmlsec/process_test.go +++ b/cmd/xmlsec/process_test.go @@ -33,7 +33,7 @@ func TestProcessInterruptsBlockedStdin(t *testing.T) { t.Skip("os.Interrupt is not supported by os.Process.Signal on Windows") } - cmd := testProcess(t, nameC14n) + cmd := testProcess(t, nameC14N) input, err := cmd.StdinPipe() if err != nil { @@ -73,7 +73,7 @@ func TestProcessJSONErrorsAndDeadline(t *testing.T) { for _, tc := range []struct { args []string code int - }{{[]string{jsonErrorsFlag, nameUnknown}, 2}, {[]string{jsonErrorsFlag, nameC14n, "-timeout", "25ms"}, 124}, {[]string{jsonErrorsFlag, nameC14n, "-bad-flag"}, 2}} { + }{{[]string{jsonErrorsFlag, nameUnknown}, 2}, {[]string{jsonErrorsFlag, nameC14N, "-timeout", "25ms"}, 124}, {[]string{jsonErrorsFlag, nameC14N, "-bad-flag"}, 2}} { cmd := testProcess(t, tc.args...) input, err := cmd.StdinPipe() @@ -103,7 +103,7 @@ func TestProcessInterruptsBlockedStdout(t *testing.T) { t.Skip("os.Interrupt is not supported by os.Process.Signal on Windows") } - cmd := testProcess(t, nameC14n) + cmd := testProcess(t, nameC14N) cmd.Stdin = bytes.NewReader(append(append([]byte(""), bytes.Repeat([]byte{'x'}, 2<<20)...), []byte("")...)) output, err := cmd.StdoutPipe() diff --git a/cmd/xmlsec/saml_commands.go b/cmd/xmlsec/saml_commands.go index 2704b9f..42d920f 100644 --- a/cmd/xmlsec/saml_commands.go +++ b/cmd/xmlsec/saml_commands.go @@ -19,6 +19,17 @@ import ( "github.com/skarm/xmlsec/xmldsig" ) +// Per-format byte limits cover operator-selected SAML input. A zero metadata lifetime delegates its default to the metadata API; trusted-key count is a separate CLI admission limit. +const ( + cliDefaultMetadataLifetime = 0 + cliMaxTrustedKeys = 64 + cliMaxPOSTFormBytes = 16 << 20 + cliMaxPOSTXMLBytes = 8 << 20 + cliMaxRedirectQueryBytes = 4 << 20 + cliMaxRedirectMessageBytes = 16 << 20 + cliMaxMetadataBytes = 16 << 20 +) + func runSAML(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) error { cmd := args[0] fs := flag.NewFlagSet("saml "+cmd, flag.ContinueOnError) @@ -36,7 +47,7 @@ func runSAML(ctx context.Context, args []string, stdin io.Reader, stdout, stderr extract := fs.String("extract", "", "write authenticated Redirect XML to this local file") pin := fs.String("document-sha256", "", "explicit SHA-256 pin for trusted unsigned metadata bytes") allowHTTP := fs.Bool("allow-http", false, "explicitly permit HTTP endpoints in metadata") - lifetime := fs.Duration("max-lifetime", 0, "maximum metadata lifetime; 0 uses library default") + lifetime := fs.Duration("max-lifetime", cliDefaultMetadataLifetime, "maximum metadata lifetime; 0 uses library default") var trusted repeatedFlag fs.Var(&trusted, "trust-key", "explicit trusted metadata/protocol signer key; repeatable") @@ -201,7 +212,7 @@ func (c samlCommand) keys(ctx context.Context) ([]crypto.PublicKey, error) { paths = append(paths, c.key) } - if len(paths) == 0 || len(paths) > 64 { + if len(paths) == 0 || len(paths) > cliMaxTrustedKeys { return nil, usageError("verification requires 1..64 explicit trusted keys") } @@ -227,7 +238,7 @@ func (c samlCommand) post(ctx context.Context, stdin io.Reader, stdout io.Writer } e := execution(ctx) - limits := binding.POSTLimits{MaxFormBytes: min(e.xml.MaxBytes, 16<<20), MaxXMLBytes: min(e.xml.MaxBytes, 8<<20), XMLLimits: e.xml} + limits := binding.POSTLimits{MaxFormBytes: min(e.xml.MaxBytes, cliMaxPOSTFormBytes), MaxXMLBytes: min(e.xml.MaxBytes, cliMaxPOSTXMLBytes), XMLLimits: e.xml} if c.command == namePostEncode { doc, err := readDocument(ctx, c.input, stdin) @@ -269,7 +280,7 @@ func (c samlCommand) redirect(ctx context.Context, stdin io.Reader, stdout io.Wr return err } - opt := binding.Options{Policy: policy, MaxQueryBytes: min(e.xml.MaxBytes, 4<<20), MaxMessageBytes: min(e.xml.MaxBytes, 16<<20), XMLLimits: e.xml} + opt := binding.Options{Policy: policy, MaxQueryBytes: min(e.xml.MaxBytes, cliMaxRedirectQueryBytes), MaxMessageBytes: min(e.xml.MaxBytes, cliMaxRedirectMessageBytes), XMLLimits: e.xml} if c.command == nameRedirectSign { kind := binding.Request @@ -325,6 +336,10 @@ func (c samlCommand) redirect(ctx context.Context, stdin io.Reader, stdout io.Wr if err := documentOutput(ctx, c.extract, stdout, result.Document()); err != nil { return err } + + if err := separateOutput(c.extract, c.output, "extract"); err != nil { + return err + } } } @@ -342,7 +357,7 @@ func (c samlCommand) metadata(ctx context.Context, stdin io.Reader, stdout io.Wr return err } - opt := metadata.Options{MaxBytes: min(execution(ctx).xml.MaxBytes, 16<<20), Policy: policy, AllowHTTP: c.allowHTTP, MaxLifetime: c.lifetime} + opt := metadata.Options{MaxBytes: min(execution(ctx).xml.MaxBytes, cliMaxMetadataBytes), Policy: policy, AllowHTTP: c.allowHTTP, MaxLifetime: c.lifetime} if c.command == nameMetadataBuild { return c.buildMetadata(ctx, at, opt, stdout) } @@ -451,7 +466,7 @@ func (c samlCommand) verifyRequest(ctx context.Context, stdin io.Reader, stdout return err } - result, verifyErr = protocol.VerifyRedirectRequest(ctx, string(raw), keys, opt, binding.Options{Policy: policy, MaxQueryBytes: min(execution(ctx).xml.MaxBytes, 4<<20), MaxMessageBytes: min(execution(ctx).xml.MaxBytes, 16<<20), XMLLimits: execution(ctx).xml}) + result, verifyErr = protocol.VerifyRedirectRequest(ctx, string(raw), keys, opt, binding.Options{Policy: policy, MaxQueryBytes: min(execution(ctx).xml.MaxBytes, cliMaxRedirectQueryBytes), MaxMessageBytes: min(execution(ctx).xml.MaxBytes, cliMaxRedirectMessageBytes), XMLLimits: execution(ctx).xml}) } else { doc, err := readDocument(ctx, c.input, stdin) if err != nil { diff --git a/cmd/xmlsec/shared_constants.go b/cmd/xmlsec/shared_constants.go index b1100c2..d47ee95 100644 --- a/cmd/xmlsec/shared_constants.go +++ b/cmd/xmlsec/shared_constants.go @@ -1,8 +1,37 @@ package main +// Shared CLI policy: cliMaxBytes is an upper bound on operator-selected byte limits. Resource defaults apply to core/detached URI reads; counts and crypto bounds remain separate controls. +// Command/format tokens and report keys form the CLI vocabulary; they are kept +// separate from caller identifiers and XML element/attribute vocabularies. const ( - nameC14n = "c14n" - namePkcs8 = "pkcs8" + // Version tag shared by CLI error/result envelopes. + cliReportSchemaVersion = 1 + // Selectable signature-policy label, also a key in the algorithms report. + cliPolicyModern = "modern" + // Selectable parameterized RSA-PSS policy label, independent of command boolean flags. + cliPolicyPSS = "pss" + // The omitted index requests unique selection rather than an arbitrary first matching element. + cliUnspecifiedSelectionIndex = -1 + cliMaxBytes = 64 << 20 + cliDefaultResourceBytes = 8 << 20 + cliDefaultTotalResourceBytes = 32 << 20 + // Shared verification-policy and key-generation floor; each command keeps its own upper bound. + cliMinRSABits = 2048 + // Ceiling for admitted HMAC policy and for the explicitly loaded shared-secret file. + cliMaxHMACKeyBytes = 1 << 20 + // Maximum operator-supplied nonce bytes for both OCSP and timestamp commands. + cliMaxNonceBytes = 128 + // Default unqualified application ID attribute for generic document commands; XML security vocabulary attributes keep their own names. + cliDefaultIDAttribute = "ID" + // Default hash choice; fixed algorithm parser labels remain independent of this selection. + cliDefaultHashName = cliHashSHA256 + // Fixed SHA-256 option spelling, also used to select the default hash. + cliHashSHA256 = "sha256" +) + +const ( + nameC14N = "c14n" + namePKCS8 = "pkcs8" nameSignDetached = "sign-detached" nameSignTemplate = "sign-template" nameVerify = "verify" diff --git a/cmd/xmlsec/signatures.go b/cmd/xmlsec/signatures.go index 0e58af9..19edd2e 100644 --- a/cmd/xmlsec/signatures.go +++ b/cmd/xmlsec/signatures.go @@ -11,6 +11,7 @@ import ( "github.com/skarm/xmlsec/c14n" "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/reference" "github.com/skarm/xmlsec/xmldsig" "github.com/skarm/xmlsec/xmlenc" @@ -18,7 +19,7 @@ import ( func selectElement(doc *document.Document, idName document.Name, id string, index int, name document.Name) (document.Element, error) { if idName.Local == "" { - idName.Local = "ID" + idName.Local = cliDefaultIDAttribute } var ( @@ -71,7 +72,7 @@ func runCoreHMAC(ctx context.Context, cmd string, doc *document.Document, select return usageError("HMAC preservation entries use a separate trust model and are not accepted by -preserve") } - secret, err := readInputContext(ctx, secretFile, nil, 1<<20) + secret, err := readInputContext(ctx, secretFile, nil, cliMaxHMACKeyBytes) if err != nil { return err } @@ -119,7 +120,7 @@ func runSignatureBuild(ctx context.Context, args []string, stdin io.Reader, stdo input := fs.String("in", "", "existing XML document; empty creates a standalone Signature") out := fs.String("out", "-", "output template XML") parentID := fs.String("parent-id", "", "explicit parent ID; empty selects root") - idAttr := fs.String("id-attr", "ID", "application ID attribute local name") + idAttr := fs.String("id-attr", cliDefaultIDAttribute, "application ID attribute local name") idNS := fs.String("id-ns", "", "application ID namespace") fs = commandFlags(fs, "config in out parent-id id-attr id-ns", false) @@ -230,7 +231,7 @@ func runInspect(ctx context.Context, args []string, stdin io.Reader, stdout, std input := fs.String("in", "-", "input XML") out := fs.String("out", "-", "diagnostic JSON") - fs = commandFlags(fs, "in out", false) + fs = commandFlags(fs, ioFlagNames, false) done, err := parseFlags(fs, args[1:]) if done || err != nil { @@ -261,20 +262,20 @@ func runInspect(ctx context.Context, args []string, stdin io.Reader, stdout, std for i, sig := range xmldsig.Signatures(doc) { v := signature{Index: i, References: []string{}} - v.ID, _ = sig.Attribute(document.Name{Local: "Id"}) + v.ID, _ = sig.Attribute(document.Name{Local: lexicon.SecurityAttrID}) for _, si := range sig.Children() { - if si.Name() != (document.Name{Space: xmldsig.Namespace, Local: "SignedInfo"}) { + if si.Name() != (document.Name{Space: xmldsig.Namespace, Local: lexicon.DSigElementSignedInfo}) { continue } for _, n := range si.Children() { switch n.Name().Local { - case "SignatureMethod": - v.Method, _ = n.Attribute(document.Name{Local: "Algorithm"}) - case "CanonicalizationMethod": - v.Canonicalization, _ = n.Attribute(document.Name{Local: "Algorithm"}) - case "Reference": - u, _ := n.Attribute(document.Name{Local: "URI"}) + case lexicon.DSigElementSignatureMethod: + v.Method, _ = n.Attribute(document.Name{Local: lexicon.SecurityAttrAlgorithm}) + case lexicon.DSigElementCanonicalizationMethod: + v.Canonicalization, _ = n.Attribute(document.Name{Local: lexicon.SecurityAttrAlgorithm}) + case lexicon.DSigElementReference: + u, _ := n.Attribute(document.Name{Local: lexicon.SecurityAttrURI}) v.References = append(v.References, u) } } @@ -289,10 +290,10 @@ func runInspect(ctx context.Context, args []string, stdin io.Reader, stdout, std func algorithmInfo() any { return map[string]any{ "signature_policies": map[string]any{ - nameDefault: xmldsig.DefaultPolicy().AllowedSignatureMethods, - "modern": xmldsig.ModernPolicy().AllowedSignatureMethods, - "pss": xmldsig.ParameterizedPolicy().AllowedSignatureMethods, - nameHMAC: xmldsig.HMACPolicy().AllowedSignatureMethods, + nameDefault: xmldsig.DefaultPolicy().AllowedSignatureMethods, + cliPolicyModern: xmldsig.ModernPolicy().AllowedSignatureMethods, + cliPolicyPSS: xmldsig.ParameterizedPolicy().AllowedSignatureMethods, + nameHMAC: xmldsig.HMACPolicy().AllowedSignatureMethods, }, "digests": xmldsig.DefaultPolicy().AllowedDigestMethods, "canonicalization": []string{c14n.Exclusive, c14n.ExclusiveWithComments, c14n.Canonical10, c14n.Canonical10WithComments, c14n.Canonical11, c14n.Canonical11WithComments}, @@ -314,7 +315,7 @@ func resultReport(operation, trust string, detail any, err error) map[string]any } } - v := map[string]any{"schema_version": 1, fieldStatus: status, "operation": operation, "trust": trust} + v := map[string]any{"schema_version": cliReportSchemaVersion, fieldStatus: status, "operation": operation, "trust": trust} if detail != nil { v["details"] = detail } @@ -334,6 +335,8 @@ func requiredFlag(name, value string) error { return nil } +// Check before and after payload publication: creating the first file exposes +// aliases of missing paths on filesystems that ignore filename case. func separateOutput(path, report, flagName string) error { if path == "-" || report != "-" && samePath(path, report) { return usageError("-%s requires a local file distinct from report output", flagName) diff --git a/cmd/xmlsec/timestamp_commands.go b/cmd/xmlsec/timestamp_commands.go index 0c58e4c..8b8e51c 100644 --- a/cmd/xmlsec/timestamp_commands.go +++ b/cmd/xmlsec/timestamp_commands.go @@ -11,6 +11,11 @@ import ( "github.com/skarm/xmlsec/timestamp" ) +// Zero accuracy omits the optional RFC 3161 issuance accuracy field. +const ( + cliDefaultDeclaredAccuracy = 0 +) + func runTimestamp(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) error { cmd := args[0] fs := flag.NewFlagSet("timestamp "+cmd, flag.ContinueOnError) @@ -19,7 +24,7 @@ func runTimestamp(ctx context.Context, args []string, stdin io.Reader, stdout, s input := fs.String("in", "-", "data for request, request DER for issue, response/token DER for verify/inspect") out := fs.String("out", "-", "output DER or diagnostic JSON") kind := fs.String(fieldKind, nameResponse, "inspect/verify: request (inspect only), response or token") - hashName := fs.String("hash", "sha256", "imprint/signature hash") + hashName := fs.String("hash", cliDefaultHashName, "imprint/signature hash") policy := fs.String("tsa-policy", "", "TSA policy OID for request/issue or expected token policy") nonceFile := fs.String("nonce-file", "", "exact nonce bytes; omitted requests generate a random nonce") omitNonce := fs.Bool("omit-nonce", false, "explicitly omit request nonce") @@ -29,7 +34,7 @@ func runTimestamp(ctx context.Context, args []string, stdin io.Reader, stdout, s config := fs.String("trust", "", "trusted TSA JSON configuration") atValue := fs.String("at", "", "explicit verification/issuance time, RFC3339") serialValue := fs.String("serial", "", "positive unique decimal serial for issuance") - accuracy := fs.Duration("accuracy", 0, "declared issuance accuracy (0 omits accuracy)") + accuracy := fs.Duration("accuracy", cliDefaultDeclaredAccuracy, "declared issuance accuracy (0 omits accuracy)") ordering := fs.Bool("ordering", false, "assert TSA ordering; issuer must guarantee it") pss := fs.Bool("pss", false, "RSA-PSS signature for issuance") keyFile := fs.String("key", "", "TSA private key") @@ -227,7 +232,7 @@ func (c timestampCommand) requestOptions(ctx context.Context) (timestamp.Request return opt, usageError("choose -nonce-file or -omit-nonce") } - opt.Nonce, err = readInputContext(ctx, c.nonceFile, nil, 128) + opt.Nonce, err = readInputContext(ctx, c.nonceFile, nil, cliMaxNonceBytes) } return opt, err @@ -331,6 +336,10 @@ func (c timestampCommand) verify(ctx context.Context, raw []byte, opt timestamp. if err := writeOutputContext(ctx, c.tokenOut, stdout, result.TokenDER()); err != nil { return err } + + if err := separateOutput(c.tokenOut, c.output, "token-out"); err != nil { + return err + } } } diff --git a/cmd/xmlsec/transforms.go b/cmd/xmlsec/transforms.go index fe1415d..c43799c 100644 --- a/cmd/xmlsec/transforms.go +++ b/cmd/xmlsec/transforms.go @@ -16,6 +16,14 @@ import ( "github.com/skarm/xmlsec/transform" ) +// These bound the trusted binding vector and recursive typed-JSON output; output format labels are the CLI protocol choices. +const ( + cliOutputText = "text" + cliOutputJSON = "json" + cliMaxBindings = 256 + cliMaxJSONSequenceDepth = 128 +) + type bindingValue struct { Name transform.Name `json:"name"` Type string `json:"type"` @@ -33,7 +41,7 @@ func readBindings(ctx context.Context, path string) (transform.Bindings, error) return nil, err } - if len(values) > 256 { + if len(values) > cliMaxBindings { return nil, usageError("too many variables") } @@ -48,24 +56,24 @@ func readBindings(ctx context.Context, path string) (transform.Bindings, error) ) switch v.Type { - case "string": + case lexicon.TypeString: item = transform.String(v.Value) - case "integer": + case lexicon.TypeInteger: n, ok := new(big.Int).SetString(v.Value, 10) if !ok { return nil, usageError("invalid integer variable") } item = transform.BigInteger(n) - case "decimal": + case lexicon.TypeDecimal: item, err = transform.Decimal(v.Value) - case "boolean": + case lexicon.TypeBoolean: if v.Value != lexicon.ValueTrue && v.Value != lexicon.ValueFalse { return nil, usageError("boolean variable requires true or false") } item = transform.Boolean(v.Value == lexicon.ValueTrue) - case "dateTime": + case lexicon.TypeDateTime: var t time.Time t, err = time.Parse(time.RFC3339Nano, v.Value) @@ -113,16 +121,16 @@ func runTransform(ctx context.Context, args []string, stdin io.Reader, stdout, s expression := fs.String("expression", "", "XPath expression") sheetFile := fs.String("stylesheet", "", "local XSLT stylesheet") version := fs.String("version", "", "XPath 1.0/2.0/3.0/3.1 or XSLT 1.0/2.0/3.0; empty uses library default") - format := fs.String("format", "text", "XPath output: text (singleton) or json (typed sequence); XSLT: text or json (all result documents)") + format := fs.String("format", cliOutputText, "XPath output: text (singleton) or json (typed sequence); XSLT: text or json (all result documents)") variables := fs.String("bindings", "", "trusted JSON typed variable/parameter array") static := fs.String("static-bindings", "", "trusted JSON XSLT 3.0 static parameter array") resources := fs.String("resources", "", "trusted URI/path allowlist for imports and URI functions") baseURI := fs.String("base-uri", "", "explicit base URI") template := fs.String("template", "", "named XSLT template, Clark name {URI}local or local") mode := fs.String("mode", "", "XSLT mode, Clark name {URI}local or local") - programBytes := fs.Int("max-program-bytes", 64<<10, "maximum expression/stylesheet bytes") - operations := fs.Int("max-operations", 2_000_000, "XPath/XSLT operation budget") - engineTimeout := fs.Duration("engine-timeout", 3*time.Second, "maximum duration per engine invocation") + programBytes := fs.Int("max-program-bytes", transform.DefaultLimits().MaxProgramBytes, "maximum expression/stylesheet bytes") + operations := fs.Int("max-operations", transform.DefaultLimits().MaxOperations, "XPath/XSLT operation budget") + engineTimeout := fs.Duration("engine-timeout", transform.DefaultLimits().Timeout, "maximum duration per engine invocation") var nsFlags repeatedFlag fs.Var(&nsFlags, "namespace", "PREFIX=URI static namespace; repeatable") @@ -158,13 +166,15 @@ func runTransform(ctx context.Context, args []string, stdin io.Reader, stdout, s } config := transform.Config{Limits: transform.Limits{ - MaxInputBytes: exec.xml.MaxBytes, - MaxProgramBytes: *programBytes, - MaxOutputBytes: exec.output, - MaxNodes: exec.xml.MaxNodes, - MaxDepth: exec.xml.MaxDepth, - MaxOperations: *operations, - Timeout: *engineTimeout, + MaxInputBytes: exec.xml.MaxBytes, + MaxProgramBytes: *programBytes, + MaxOutputBytes: exec.output, + MaxNodes: exec.xml.MaxNodes, + MaxDepth: exec.xml.MaxDepth, + MaxAttributesPerElement: exec.xml.MaxAttributesPerElement, + MaxInScopeNamespaces: exec.xml.MaxInScopeNamespaces, + MaxOperations: *operations, + Timeout: *engineTimeout, }} config.Resolver, err = transformResolver(ctx, *resources) @@ -244,14 +254,14 @@ func runTransform(ctx context.Context, args []string, stdin io.Reader, stdout, s options := transform.XSLTContext{Parameters: bindings, Template: parseQName(*template), Mode: parseQName(*mode), BaseOutputURI: *baseURI} switch *format { - case "json": + case cliOutputJSON: result, err := sheet.Transform(ctx, doc, options) if err != nil { return err } return jsonOutput(ctx, *output, stdout, result) - case "text": + case cliOutputText: return produceOutput(ctx, *output, stdout, func(w io.Writer) error { _, err := sheet.Write(ctx, w, doc, options) return err @@ -276,7 +286,12 @@ func canonicalizeXPath(ctx context.Context, w io.Writer, doc *document.Document, tr := reference.Transform{Algorithm: reference.XPath, XPath: []reference.XPathExpression{{Expression: expression, Namespaces: ns}}} e := execution(ctx) - engine, err := transform.NewEngine(transform.Config{AllowedPrograms: []reference.Transform{tr}, Limits: transform.Limits{MaxInputBytes: e.xml.MaxBytes, MaxOutputBytes: e.output, MaxNodes: e.xml.MaxNodes, MaxDepth: e.xml.MaxDepth, MaxOperations: e.nodeVisits}}) + engine, err := transform.NewEngine(transform.Config{AllowedPrograms: []reference.Transform{tr}, Limits: transform.Limits{ + MaxInputBytes: e.xml.MaxBytes, MaxOutputBytes: e.output, + MaxNodes: e.xml.MaxNodes, MaxDepth: e.xml.MaxDepth, MaxOperations: e.nodeVisits, + MaxAttributesPerElement: e.xml.MaxAttributesPerElement, + MaxInScopeNamespaces: e.xml.MaxInScopeNamespaces, + }}) if err != nil { return err } @@ -348,7 +363,7 @@ func (c xpathCommand) run(ctx context.Context, engine *transform.Engine, doc *tr return err } - if c.format == "text" { + if c.format == cliOutputText { value, err := result.Text() if err != nil { return err @@ -357,7 +372,7 @@ func (c xpathCommand) run(ctx context.Context, engine *transform.Engine, doc *tr return writeOutputContext(ctx, c.output, stdout, []byte(value+"\n")) } - if c.format != "json" { + if c.format != cliOutputJSON { return usageError("-format must be text or json") } @@ -370,7 +385,7 @@ func (c xpathCommand) run(ctx context.Context, engine *transform.Engine, doc *tr } func sequenceJSON(sequence transform.Sequence, depth int) (any, error) { - if depth > 128 { + if depth > cliMaxJSONSequenceDepth { return nil, errors.New("result nesting exceeds 128") } diff --git a/cmd/xmlsec/trust.go b/cmd/xmlsec/trust.go index edc2d89..885b027 100644 --- a/cmd/xmlsec/trust.go +++ b/cmd/xmlsec/trust.go @@ -14,6 +14,14 @@ import ( "github.com/skarm/xmlsec/timestamp" ) +// Trusted CRL/OCSP vectors and their aggregate encoded bytes are bounded independently; each OCSP file also has its own cap. +const ( + cliMaxCRLFiles = 256 + cliMaxOCSPFiles = 256 + cliMaxEvidenceBytes = 64 << 20 + cliMaxOCSPFileBytes = 4 << 20 +) + type evidenceFiles struct { Intermediates []string `json:"intermediates,omitempty"` CRLs []string `json:"crls,omitempty"` @@ -30,7 +38,7 @@ func (c evidenceFiles) load(ctx context.Context, base string) (pki.Evidence, err return e, err } - if len(c.CRLs) > 256 || len(c.OCSP) > 256 { + if len(c.CRLs) > cliMaxCRLFiles || len(c.OCSP) > cliMaxOCSPFiles { return e, usageError("too many revocation evidence files") } @@ -48,7 +56,7 @@ func (c evidenceFiles) load(ctx context.Context, base string) (pki.Evidence, err } total += len(crl.Raw) - if total > 64<<20 { + if total > cliMaxEvidenceBytes { return e, usageError("evidence exceeds 64 MiB") } @@ -56,13 +64,13 @@ func (c evidenceFiles) load(ctx context.Context, base string) (pki.Evidence, err } for _, path := range c.OCSP { - raw, err := readInputContext(ctx, localPath(base, path), nil, 4<<20) + raw, err := readInputContext(ctx, localPath(base, path), nil, cliMaxOCSPFileBytes) if err != nil { return e, err } total += len(raw) - if total > 64<<20 { + if total > cliMaxEvidenceBytes { return e, usageError("evidence exceeds 64 MiB") } diff --git a/cmd/xmlsec/xades_commands.go b/cmd/xmlsec/xades_commands.go index da810d5..12d6377 100644 --- a/cmd/xmlsec/xades_commands.go +++ b/cmd/xmlsec/xades_commands.go @@ -10,12 +10,23 @@ import ( "github.com/skarm/xmlsec/c14n" "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/pki" "github.com/skarm/xmlsec/timestamp" "github.com/skarm/xmlsec/xades" "github.com/skarm/xmlsec/xmldsig" ) +// Application-supplied identifier defaults remain independent from fixed JSON report keys. Validation-profile labels select current-time or historical validation. +const ( + cliDefaultSignatureID = "signature" + cliDefaultPropertiesID = "properties" + cliDefaultReferenceID = "data-reference" + cliProfileCurrentTime = "current-time" + cliProfileWithTime = "with-time" + fieldSignature = "signature" +) + type xadesConfig struct { SignerTrust trustConfig `json:"signer_trust"` TSAConfig string `json:"tsa_config,omitempty"` @@ -41,15 +52,15 @@ func runXAdES(ctx context.Context, args []string, stdin io.Reader, stdout, stder configFile := fs.String("trust", "", "trusted XAdES validator JSON with signer_trust, tsa_config and level") tsaFile := fs.String("tsa-trust", "", "TSA JSON for attaching/requesting a signature timestamp") tokenFile := fs.String(nameToken, "", "previously obtained timestamp token DER") - idAttr := fs.String("id-attr", "ID", "application ID attribute local name") + idAttr := fs.String("id-attr", cliDefaultIDAttribute, "application ID attribute local name") idNS := fs.String("id-ns", "", "application ID namespace") - sigID := fs.String("signature-id", "signature", "signature ID when signing") - propsID := fs.String("properties-id", "properties", "SignedProperties ID") - refID := fs.String("reference-id", "data-reference", "data Reference ID") - mimeType := fs.String("mime-type", "application/xml", "signed data MIME type") - index := fs.Int("signature-index", -1, "explicit zero-based signature selection; omitted requires exactly one signature") - canonical := fs.String(nameC14n, c14n.Exclusive, "timestamp canonicalization URI") - profile := fs.String("validation-profile", "current-time", "validation: current-time or with-time") + sigID := fs.String("signature-id", cliDefaultSignatureID, "signature ID when signing") + propsID := fs.String("properties-id", cliDefaultPropertiesID, "SignedProperties ID") + refID := fs.String("reference-id", cliDefaultReferenceID, "data Reference ID") + mimeType := fs.String("mime-type", lexicon.MediaTypeXML, "signed data MIME type") + index := fs.Int("signature-index", cliUnspecifiedSelectionIndex, "explicit zero-based signature selection; omitted requires exactly one signature") + canonical := fs.String(nameC14N, c14n.Exclusive, "timestamp canonicalization URI") + profile := fs.String("validation-profile", cliProfileCurrentTime, "validation: current-time or with-time") flags := registerPrivateKeyFlags(fs) specific := "in out certificate at id-attr id-ns signature-index" @@ -128,7 +139,7 @@ func runXAdES(ctx context.Context, args []string, stdin io.Reader, stdout, stder return usageError("-signature-index must be nonnegative when supplied") } - selected, err := selectElement(doc, document.Name{}, "", *index, document.Name{Space: xmldsig.Namespace, Local: "Signature"}) + selected, err := selectElement(doc, document.Name{}, "", *index, document.Name{Space: xmldsig.Namespace, Local: lexicon.DSigElementSignature}) if err != nil { return err } @@ -167,11 +178,11 @@ func runXAdES(ctx context.Context, args []string, stdin io.Reader, stdout, stder evidence := xades.ValidationEvidence{Signer: signerEvidence, TSA: tsaEvidence} - if *profile == "with-time" { + if *profile == cliProfileWithTime { return operation.withTime(ctx, cmd, cfg, signerTrust, tsa, evidence, stdout) } - if *profile != "current-time" { + if *profile != cliProfileCurrentTime { return usageError("-validation-profile must be current-time or with-time") } @@ -240,14 +251,18 @@ func (o xadesOperation) withTime(ctx context.Context, cmd string, cfg xadesConfi result, report, verifyErr := validator.ValidateEnveloped(ctx, o.doc, o.selected, o.certificate, o.at, evidence) - detail := map[string]any{"profile": report.Profile(), "indication": report.Indication(), "proof_time": report.ProofTime(), "verification_time": report.VerificationTime()} + detail := withTimeXAdESReport(report) if verifyErr == nil { - detail["signature"] = basicXAdESReport(result.Basic()) + detail[fieldSignature] = basicXAdESReport(result.Basic()) } return errors.Join(verifyErr, jsonOutput(ctx, o.output, stdout, resultReport("xades validate", "explicit-pkix-and-tsa-policy", detail, verifyErr))) } +func withTimeXAdESReport(report *xades.WithTimeReport) map[string]any { + return map[string]any{"profile": report.Profile(), "indication": report.Indication(), "proof_time": report.ProofTime(), "verification_time": report.VerificationTime()} +} + func basicXAdESReport(r *xades.BasicResult) any { return map[string]any{"signature_method": r.Signature().SignatureMethod(), "key_fingerprint_sha256": r.Signature().KeyFingerprintSHA256(), "claimed_signing_time": r.ClaimedSigningTime(), "reference_count": len(r.Signature().References())} } @@ -304,7 +319,7 @@ func runXAdESCurrent(ctx context.Context, o xadesOperation, cmd string, cfg xade detail = map[string]any{ "profile": result.Profile(), "level": result.Level(), - "signature": basicXAdESReport(result.Basic()), + fieldSignature: basicXAdESReport(result.Basic()), "timestamps": len(result.Timestamps()), "archive_timestamps": len(result.ArchiveTimestamps()), } diff --git a/cms/attributes.go b/cms/attributes.go index 5211bfc..16d50d7 100644 --- a/cms/attributes.go +++ b/cms/attributes.go @@ -12,11 +12,41 @@ import ( "time" "github.com/skarm/xmlsec/internal/der" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/signature" "github.com/skarm/xmlsec/internal/x509cert" "github.com/skarm/xmlsec/internal/x509name" ) +// SigningCertificate and SigningCertificateV2 contain required certificate IDs +// and optional policies; binding and trust paths validate this same envelope. +const ( + signingCertificateMinimumFields = 1 + signingCertificateMaximumFields = 2 +) + +const ( + maximumAlgorithmIdentifierBytes = 8192 + // Bounds DER for CMS attribute values and their aggregate encoding, in bytes. + maximumAttributeBytes = 1 << 20 + maximumAttributeValues = 64 + maximumIssuerNames = 32 + // Caps certificate references in a CMS signing-certificate attribute. + maximumSigningCertificateIDs = 32 + maximumSigningPolicies = 32 + maximumSigningTimeBytes = 32 + maximumUTCTimeYear = 2049 + minimumUTCTimeYear = 1950 +) + +// Context-specific tags in distinct CMS attribute schemas. An unsigned-attribute +// section and an algorithm-protection signature field happen to both use [1]. +const ( + signedAttributesTag = 0 + unsignedAttributesTag = 1 + algorithmProtectionSignatureTag = 1 +) + // Attribute contains an attribute type OID and complete DER-encoded values. // Result accessors copy the attribute and its mutable values. Authenticating an // attribute does not establish its meaning for the application. @@ -30,7 +60,7 @@ type Attribute struct { // CertificateBindingAttribute encodes ESSCertIDv2 using default SHA-256 and an // issuerSerial identifying the exact certificate (RFC 5035 / RFC 5816). func CertificateBindingAttribute(b []byte) (Attribute, error) { - if len(b) > 1<<20 { + if len(b) > maximumCertificateBytes { return Attribute{}, ErrPolicy } @@ -41,7 +71,7 @@ func CertificateBindingAttribute(b []byte) (Attribute, error) { sum, _ := signature.Sum(crypto.SHA256, cert.Raw) serial, _ := asn1.Marshal(cert.SerialNumber) - issuer := der.Seq(der.Seq(der.Ctx(4, cert.RawIssuer)), serial) + issuer := der.Seq(der.Seq(der.Ctx(lexicon.GeneralNameDirectory, cert.RawIssuer)), serial) id := der.Seq(der.Octets(sum), issuer) return Attribute{ @@ -83,17 +113,17 @@ func verifyESS(ctx context.Context, attrs []Attribute, cert *x509.Certificate, l } func verifyESSAttribute(ctx context.Context, raw []byte, cert *x509.Certificate, v2 bool) error { - v, err := der.Read(raw, 1<<20) + v, err := der.Read(raw, maximumAttributeBytes) if err != nil { return ErrSyntax } - top, err := der.Sequence(v, 1, 2) + top, err := der.Sequence(v, signingCertificateMinimumFields, signingCertificateMaximumFields) if err != nil { return ErrSyntax } - ids, err := der.Sequence(top[0], 1, 32) + ids, err := der.Sequence(top[0], 1, maximumSigningCertificateIDs) if err != nil { return ErrSyntax } @@ -104,17 +134,76 @@ func verifyESSAttribute(ctx context.Context, raw []byte, cert *x509.Certificate, } } - if len(top) == 2 { - if _, err := der.Sequence(top[1], 1, 32); err != nil { + if len(top) == signingCertificateMaximumFields { + if err := validateESSPolicies(ctx, top[1]); err != nil { + return err + } + } + + return nil +} + +// RFC 5035 policies are advisory and do not select the verifier's trust policy. +// Their PolicyInformation and PolicyQualifierInfo containers still require the +// declared ASN.1 structure. Qualifier meanings remain application-owned. +func validateESSPolicies(ctx context.Context, value asn1.RawValue) error { + // PolicyInformation has an OID and optional qualifiers. A qualifier + // itself contains its type OID and one encoded value. + const ( + policyInformationMinimumFields = 1 + policyInformationMaximumFields = 2 + policyQualifierFields = 2 + ) + + policies, err := der.Sequence(value, 1, maximumSigningPolicies) + if err != nil { + return ErrSyntax + } + + for _, policy := range policies { + if err := ctx.Err(); err != nil { + return err + } + + fields, err := der.Sequence(policy, policyInformationMinimumFields, policyInformationMaximumFields) + if err != nil || !der.Is(fields[0], asn1.ClassUniversal, asn1.TagOID, false) { return ErrSyntax } + + if len(fields) == policyInformationMinimumFields { + continue + } + + qualifiers := fields[1] + if !der.Is(qualifiers, asn1.ClassUniversal, asn1.TagSequence, true) || len(qualifiers.Bytes) == 0 { + return ErrSyntax + } + + // The enclosing ESS value has passed der.Read, including the shared + // node budget. Iterate without materializing another qualifier slice. + for qualifier := range der.ValidatedChildren(qualifiers) { + if err := ctx.Err(); err != nil { + return err + } + + parts, err := der.Sequence(qualifier, policyQualifierFields, policyQualifierFields) + if err != nil || !der.Is(parts[0], asn1.ClassUniversal, asn1.TagOID, false) { + return ErrSyntax + } + } } return nil } func verifyESSCertID(ctx context.Context, v asn1.RawValue, cert *x509.Certificate, v2, signer bool) error { - fields, err := der.Sequence(v, 1, 3) + // ESSCertIDv2 has a required hash plus optional hashAlgorithm and issuerSerial. + const ( + minimumESSCertIDFields = 1 + maximumESSCertIDFields = 3 + ) + + fields, err := der.Sequence(v, minimumESSCertIDFields, maximumESSCertIDFields) if err != nil { return ErrSyntax } @@ -160,7 +249,7 @@ func parseESSCertHash(fields []asn1.RawValue, v2 bool) (crypto.Hash, []byte, int if v2 { h = crypto.SHA256 - if der.Is(fields[0], 0, asn1.TagSequence, true) { + if der.Is(fields[0], asn1.ClassUniversal, asn1.TagSequence, true) { algorithm, err := signature.Algorithm(fields[0]) if err != nil { return 0, nil, 0, ErrSyntax @@ -192,12 +281,14 @@ func parseESSCertHash(fields []asn1.RawValue, v2 bool) (crypto.Hash, []byte, int } func parseESSIssuer(v asn1.RawValue) ([]asn1.RawValue, *big.Int, error) { - issuer, err := der.Sequence(v, 2, 2) + const issuerSerialFields = 2 // GeneralNames and certificate serial number + + issuer, err := der.Sequence(v, issuerSerialFields, issuerSerialFields) if err != nil { return nil, nil, ErrSyntax } - names, err := der.Sequence(issuer[0], 1, 32) + names, err := der.Sequence(issuer[0], 1, maximumIssuerNames) if err != nil { return nil, nil, ErrSyntax } @@ -213,11 +304,11 @@ func parseESSIssuer(v asn1.RawValue) ([]asn1.RawValue, *big.Int, error) { func verifyESSIssuer(ctx context.Context, names []asn1.RawValue, serial *big.Int, cert *x509.Certificate) error { // RFC5035 section4: a public-key certificate's issuerSerial contains only // its issuer's directoryName, not a matching name among unrelated aliases. - if len(names) != 1 || !der.Is(names[0], 2, 4, true) { + if len(names) != 1 || !der.Is(names[0], asn1.ClassContextSpecific, lexicon.GeneralNameDirectory, true) { return ErrCertificate } - directory, err := der.Explicit(names[0], 4) + directory, err := der.Explicit(names[0], lexicon.GeneralNameDirectory) if err != nil { return ErrSyntax } @@ -235,6 +326,9 @@ func verifyESSIssuer(ctx context.Context, names []asn1.RawValue, serial *big.Int } func verifyProtection(attrs []Attribute, alg pkix.AlgorithmIdentifier, h crypto.Hash) error { + // The supported algorithm-protection form binds digest and signature algorithms. + const algorithmProtectionFields = 2 + raw, ok, e := attributeValue(attrs, oidAlgorithmProtect()) if e != nil { return e @@ -244,12 +338,12 @@ func verifyProtection(attrs []Attribute, alg pkix.AlgorithmIdentifier, h crypto. return nil } - v, e := der.Read(raw, 8192) + v, e := der.Read(raw, maximumAlgorithmIdentifierBytes) if e != nil { return ErrSyntax } - f, e := der.Sequence(v, 2, 2) + f, e := der.Sequence(v, algorithmProtectionFields, algorithmProtectionFields) if e != nil { return ErrSyntax } @@ -264,11 +358,11 @@ func verifyProtection(attrs []Attribute, alg pkix.AlgorithmIdentifier, h crypto. return ErrSignature } - if !der.Is(f[1], 2, 1, true) { + if !der.Is(f[1], asn1.ClassContextSpecific, algorithmProtectionSignatureTag, true) { return ErrSyntax } - s := asn1.RawValue{Class: 0, Tag: asn1.TagSequence, IsCompound: true, Bytes: f[1].Bytes} + s := asn1.RawValue{Class: asn1.ClassUniversal, Tag: asn1.TagSequence, IsCompound: true, Bytes: f[1].Bytes} sa, e := signature.Algorithm(s) if e != nil { @@ -314,7 +408,9 @@ func cloneAttributes(attrs []Attribute) []Attribute { } func parseAttributes(v asn1.RawValue, tag, maximum int) ([]Attribute, error) { - f, e := der.SetFields(v, 2, tag, maximum) + const attributeFields = 2 // attribute type OID and SET OF values + + f, e := der.SetFields(v, asn1.ClassContextSpecific, tag, maximum) if e != nil || len(f) == 0 { return nil, ErrSyntax } @@ -323,7 +419,7 @@ func parseAttributes(v asn1.RawValue, tag, maximum int) ([]Attribute, error) { seen := map[string]struct{}{} for _, field := range f { - a, e := der.Sequence(field, 2, 2) + a, e := der.Sequence(field, attributeFields, attributeFields) if e != nil { return nil, ErrSyntax } @@ -341,7 +437,7 @@ func parseAttributes(v asn1.RawValue, tag, maximum int) ([]Attribute, error) { seen[key] = struct{}{} - vals, e := der.SetFields(a[1], 0, asn1.TagSet, 64) + vals, e := der.SetFields(a[1], asn1.ClassUniversal, asn1.TagSet, maximumAttributeValues) if e != nil || len(vals) == 0 { return nil, ErrSyntax } @@ -351,7 +447,7 @@ func parseAttributes(v asn1.RawValue, tag, maximum int) ([]Attribute, error) { x.ValuesDER = append(x.ValuesDER, v.FullBytes) } - if tag == 0 { + if tag == signedAttributesTag { if err := validateSigningTime(x); err != nil { return nil, ErrSyntax } @@ -378,7 +474,7 @@ func attributeValue(attrs []Attribute, oid asn1.ObjectIdentifier) ([]byte, bool, } func encodeAttributes(attrs []Attribute, maximum int) ([]byte, error) { - if len(attrs) < 2 || len(attrs) > maximum { + if len(attrs) < minimumAttributes || len(attrs) > maximum { return nil, ErrPolicy } @@ -390,7 +486,7 @@ func encodeAttributes(attrs []Attribute, maximum int) ([]byte, error) { for _, x := range attrs { key := x.OID.String() - if _, exists := seen[key]; exists || len(x.ValuesDER) < 1 || len(x.ValuesDER) > 64 || !attributePlacement(x.OID, 0) { + if _, exists := seen[key]; exists || len(x.ValuesDER) < 1 || len(x.ValuesDER) > maximumAttributeValues || !attributePlacement(x.OID, signedAttributesTag) { return nil, ErrPolicy } @@ -407,11 +503,11 @@ func encodeAttributes(attrs []Attribute, maximum int) ([]byte, error) { for _, b := range x.ValuesDER { total += len(b) - if total > 1<<20 { + if total > maximumAttributeBytes { return nil, ErrPolicy } - if _, e := der.Read(b, 1<<20); e != nil { + if _, e := der.Read(b, maximumAttributeBytes); e != nil { return nil, fmt.Errorf("%w: attribute value: %w", ErrPolicy, e) } } @@ -426,7 +522,7 @@ func encodeAttributes(attrs []Attribute, maximum int) ([]byte, error) { // 11.1--11.4, RFC 6211 section 2, and RFC 5035 section 3 / RFC 2634 section 5. // Unknown attributes retain the caller's responsibility for their semantics. func attributePlacement(oid asn1.ObjectIdentifier, tag int) bool { - if tag == 0 { + if tag == signedAttributesTag { return !oid.Equal(oidCountersignature()) } @@ -446,7 +542,7 @@ func validateSigningTime(attr Attribute) error { return ErrSyntax } - v, err := der.Read(attr.ValuesDER[0], 32) + v, err := der.Read(attr.ValuesDER[0], maximumSigningTimeBytes) if err != nil { return ErrSyntax } @@ -459,14 +555,14 @@ func validateSigningTime(attr Attribute) error { var text string switch { - case der.Is(v, 0, asn1.TagUTCTime, false): - text = instant.UTC().Format("060102150405Z") - case der.Is(v, 0, asn1.TagGeneralizedTime, false): - if instant.Year() >= 1950 && instant.Year() <= 2049 || instant.Year() < 1 { + case der.Is(v, asn1.ClassUniversal, asn1.TagUTCTime, false): + text = instant.UTC().Format(lexicon.ASN1UTCTimeLayout) + case der.Is(v, asn1.ClassUniversal, asn1.TagGeneralizedTime, false): + if instant.Year() >= minimumUTCTimeYear && instant.Year() <= maximumUTCTimeYear || instant.Year() < 1 { return ErrSyntax } - text = instant.UTC().Format("20060102150405Z") + text = instant.UTC().Format(lexicon.ASN1GeneralizedTimeLayout) default: return ErrSyntax } diff --git a/cms/ber.go b/cms/ber.go index 5c6a592..7fa6484 100644 --- a/cms/ber.go +++ b/cms/ber.go @@ -3,6 +3,7 @@ package cms import ( "bytes" "context" + "encoding/asn1" "errors" "fmt" "io" @@ -13,6 +14,19 @@ import ( "github.com/skarm/xmlsec/internal/streamio" ) +// Complete ASN.1 identifier octets shared by CMS wrapper normalization and +// streamed output; class and constructed bits are included. +const ( + encodedSequenceTag = 0x30 + encodedSetTag = 0x31 + encodedContext0Tag = 0xa0 +) + +const ( + maximumBERDepth = 33 + maximumBERNodes = 100000 +) + // ParseBER accepts the selected BER forms documented by [ParseBERReader]. // Input must remain unchanged during the call; the result owns its normalized // encoding. It does not authenticate content or embedded certificates. @@ -74,7 +88,7 @@ func ParseBERReader(ctx context.Context, src io.Reader, l Limits) (*SignedData, // parseBER borrows input only during normalization; the result owns its DER. func parseBER(ctx context.Context, input []byte, l Limits) (*SignedData, error) { - root, err := ber.Read(ctx, input, ber.Limits{MaxBytes: l.MaxBytes, MaxDepth: 33, MaxNodes: 100000}) + root, err := ber.Read(ctx, input, ber.Limits{MaxBytes: l.MaxBytes, MaxDepth: maximumBERDepth, MaxNodes: maximumBERNodes}) if err != nil { return nil, cmsBERError(err) } @@ -93,22 +107,22 @@ func parseBER(ctx context.Context, input []byte, l Limits) (*SignedData, error) } func normalizeCMSBER(ctx context.Context, root ber.Value, limits Limits) ([]byte, error) { - if root.Tag != 0x30 || len(root.Children) != 2 { + if root.Tag != encodedSequenceTag || len(root.Children) != contentInfoFields { return nil, ErrSyntax } explicit := root.Children[1] - if explicit.Tag != 0xa0 || len(explicit.Children) != 1 { + if explicit.Tag != encodedContext0Tag || len(explicit.Children) != 1 { return nil, ErrSyntax } sd := explicit.Children[0] - if sd.Tag != 0x30 || len(sd.Children) < 4 || len(sd.Children) > 6 { + if sd.Tag != encodedSequenceTag || len(sd.Children) < signedDataMinimumFields || len(sd.Children) > signedDataMaximumFields { return nil, ErrSyntax } ec := sd.Children[2] - if ec.Tag != 0x30 || len(ec.Children) < 1 || len(ec.Children) > 2 { + if ec.Tag != encodedSequenceTag || len(ec.Children) < encapsulatedContentMinimumFields || len(ec.Children) > encapsulatedContentMaximumFields { return nil, ErrSyntax } @@ -121,9 +135,9 @@ func normalizeCMSBER(ctx context.Context, root ber.Value, limits Limits) ([]byte var octetHeader, contentHeader []byte - if len(ec.Children) == 2 { + if len(ec.Children) == encapsulatedContentMaximumFields { content := ec.Children[1] - if content.Tag != 0xa0 || len(content.Children) != 1 { + if content.Tag != encodedContext0Tag || len(content.Children) != 1 { return nil, ErrSyntax } @@ -136,12 +150,12 @@ func normalizeCMSBER(ctx context.Context, root ber.Value, limits Limits) ([]byte return nil, cmsBERError(err) } - octetHeader = der.Header(4, contentSize) - contentHeader = der.Header(0xa0, len(octetHeader)+contentSize) + octetHeader = der.Header(asn1.TagOctetString, contentSize) + contentHeader = der.Header(encodedContext0Tag, len(octetHeader)+contentSize) encapSize += len(contentHeader) + len(octetHeader) + contentSize } - encapHeader := der.Header(0x30, encapSize) + encapHeader := der.Header(encodedSequenceTag, encapSize) signedSize := len(encapHeader) + encapSize for i, field := range sd.Children { @@ -150,11 +164,11 @@ func normalizeCMSBER(ctx context.Context, root ber.Value, limits Limits) ([]byte } } - signedHeader := der.Header(0x30, signedSize) - explicitHeader := der.Header(0xa0, len(signedHeader)+signedSize) + signedHeader := der.Header(encodedSequenceTag, signedSize) + explicitHeader := der.Header(encodedContext0Tag, len(signedHeader)+signedSize) rootSize := len(root.Children[0].Full) + len(explicitHeader) + len(signedHeader) + signedSize - rootHeader := der.Header(0x30, rootSize) + rootHeader := der.Header(encodedSequenceTag, rootSize) if len(rootHeader)+rootSize > limits.MaxBytes { return nil, ErrPolicy } @@ -179,7 +193,7 @@ func normalizeCMSBER(ctx context.Context, root ber.Value, limits Limits) ([]byte output.Write(encapHeader) output.Write(ec.Children[0].Full) - if len(ec.Children) == 2 { + if len(ec.Children) == encapsulatedContentMaximumFields { output.Write(contentHeader) output.Write(octetHeader) diff --git a/cms/cms.go b/cms/cms.go index 1db7307..12c349c 100644 --- a/cms/cms.go +++ b/cms/cms.go @@ -5,6 +5,28 @@ import ( "errors" ) +const ( + defaultMaxAttributes = 64 + defaultMaxBytes = 16 << 20 + defaultMaxCertificates = 32 + defaultMaxContentBytes = 12 << 20 + defaultMaxSigners = 16 + maximumAttributes = 128 + maximumBytes = 64 << 20 + maximumCertificates = 128 + maximumSigners = 32 + minimumAttributes = 2 +) + +// CMS wire versions depend on the content and signer identifier choices. +// SignedData and SignerInfo encode separate version fields with separate roles. +const ( + signedDataVersion1 = 1 + signedDataVersion3 = 3 + signerIssuerSerialVersion = 1 + signerSubjectKeyIDVersion = 3 +) + var ( // ErrUnsupported reports a semantic feature outside the selected CMS profile. ErrUnsupported = errors.New("unsupported CMS feature") @@ -38,7 +60,9 @@ type Limits struct { } // DefaultLimits returns a fresh value containing the default resource budgets. -func DefaultLimits() Limits { return Limits{16 << 20, 12 << 20, 32, 16, 64} } +func DefaultLimits() Limits { + return Limits{defaultMaxBytes, defaultMaxContentBytes, defaultMaxCertificates, defaultMaxSigners, defaultMaxAttributes} +} // DataOID returns an owned copy of the CMS id-data object identifier. func DataOID() asn1.ObjectIdentifier { return append(asn1.ObjectIdentifier(nil), oidData()...) } @@ -93,8 +117,8 @@ func normalize(l Limits) (Limits, error) { l.MaxAttributes = d.MaxAttributes } - if l.MaxBytes < 1 || l.MaxBytes > 64<<20 || l.MaxContentBytes < 1 || l.MaxCertificates < 1 || l.MaxCertificates > 128 || - l.MaxSigners < 1 || l.MaxSigners > 32 || l.MaxAttributes < 2 || l.MaxAttributes > 128 { + if l.MaxBytes < 1 || l.MaxBytes > maximumBytes || l.MaxContentBytes < 1 || l.MaxCertificates < 1 || l.MaxCertificates > maximumCertificates || + l.MaxSigners < 1 || l.MaxSigners > maximumSigners || l.MaxAttributes < minimumAttributes || l.MaxAttributes > maximumAttributes { return l, ErrPolicy } diff --git a/cms/cms_test.go b/cms/cms_test.go index 4b2fa55..a7be63f 100644 --- a/cms/cms_test.go +++ b/cms/cms_test.go @@ -253,77 +253,6 @@ func TestCMSBERNormalizedBudget(t *testing.T) { } } -func indefiniteBER(tag byte, parts ...[]byte) []byte { - return append(append([]byte{tag, 128}, bytes.Join(parts, nil)...), 0, 0) -} - -func cmsBEREnvelope(t testing.TB, raw, octets []byte) []byte { - t.Helper() - ci, fields := cmsEnvelopeFields(t, raw) - - ec, err := der.Sequence(fields[2], 1, 2) - if err != nil { - t.Fatal(err) - } - - encap := ec[0].FullBytes - if len(ec) == 2 { - encap = append(bytes.Clone(encap), indefiniteBER(0xa0, octets)...) - } - - parts := signerBytes(fields) - parts[2] = indefiniteBER(0x30, encap) - - return indefiniteBER(0x30, ci[0].FullBytes, indefiniteBER(0xa0, indefiniteBER(0x30, parts...))) -} - -func testCMSIndependentFixture(t *testing.T, path string) { - t.Helper() - - raw, err := os.ReadFile(path) - if err != nil { - t.Fatal(err) - } - - certificate, err := os.ReadFile(filepath.Join(filepath.Dir(path), "signer.der")) - if err != nil { - t.Fatal(err) - } - - message, err := os.ReadFile(filepath.Join(filepath.Dir(path), "content.bin")) - if err != nil { - t.Fatal(err) - } - - signed, err := cms.ParseReader(t.Context(), bytes.NewReader(raw), cms.Limits{}) - if err != nil { - t.Fatal(err) - } - - options := cms.VerifyOptions{ExpectedContentType: cms.DataOID()} - if signed.Detached() { - options.DetachedContent = &message - } - - for _, discard := range []bool{false, true} { - options.DiscardContent = discard - - result, err := signed.Verify(t.Context(), [][]byte{certificate}, options) - if err != nil { - t.Fatal(err) - } - - if result.ContentSize() != int64(len(message)) || (!discard && !bytes.Equal(result.Content(), message)) || (discard && result.Content() != nil) { - t.Fatal("content retention", discard) - } - } - - var output bytes.Buffer - if n, err := signed.WriterTo(t.Context()).WriteTo(&output); err != nil || n != int64(len(raw)) || !bytes.Equal(output.Bytes(), raw) { - t.Fatal("DER replay", n, err) - } -} - func TestCMSRejectsMalformedAttributeValues(t *testing.T) { c := testcert.New(t) for _, value := range [][]byte{ @@ -1025,63 +954,6 @@ func TestCMSSigningTimeSyntax(t *testing.T) { } } -// Rebuild just the unsigned container, leaving the signed attributes and -// cryptographic signature intact. The negative cases are valid DER. -func rewriteSigner(t *testing.T, raw []byte, change func([]asn1.RawValue) [][]byte) []byte { - t.Helper() - ci, f := cmsEnvelopeFields(t, raw) - - si, err := der.SetFields(f[len(f)-1], 0, asn1.TagSet, 1) - if err != nil || len(si) != 1 { - t.Fatal(err) - } - - fields, err := der.Sequence(si[0], 5, 7) - if err != nil { - t.Fatal(err) - } - - parts := signerBytes(f) - parts[len(parts)-1] = der.Set(der.Seq(change(fields)...)) - - return der.Seq(ci[0].FullBytes, der.Ctx(0, der.Seq(parts...))) -} - -func cmsEnvelopeFields(t testing.TB, raw []byte) ([]asn1.RawValue, []asn1.RawValue) { - t.Helper() - - v, err := der.Read(raw, len(raw)) - if err != nil { - t.Fatal(err) - } - - ci, err := der.Sequence(v, 2, 2) - if err != nil { - t.Fatal(err) - } - - sd, err := der.Explicit(ci[1], 0) - if err != nil { - t.Fatal(err) - } - - f, err := der.Sequence(sd, 4, 6) - if err != nil { - t.Fatal(err) - } - - return ci, f -} - -func signerBytes(f []asn1.RawValue) [][]byte { - b := make([][]byte, len(f)) - for i := range f { - b[i] = f[i].FullBytes - } - - return b -} - func TestCMSTrustBinding(t *testing.T) { chain := testcert.New(t) content := []byte("authenticated content and explicit trust") @@ -1373,6 +1245,11 @@ func TestCMSAuthenticatedESSStructure(t *testing.T) { names := der.Seq(der.Ctx(4, chain.Leaf.RawIssuer)) issuer := der.Seq(names, serial) id := der.Seq(der.Octets(sum[:]), issuer) + policyOID := der.OID(asn1.ObjectIdentifier{1, 2, 3, 4}) + qualifierOID := der.OID(asn1.ObjectIdentifier{1, 2, 3, 5}) + policyValue := func(fields ...[]byte) []byte { + return der.Seq(der.Seq(id), der.Seq(der.Seq(fields...))) + } for _, tc := range []struct { name string value []byte @@ -1391,6 +1268,15 @@ func TestCMSAuthenticatedESSStructure(t *testing.T) { {"zero issuer serial", der.Seq(der.Seq(der.Seq(der.Octets(sum[:]), der.Seq(names, der.Int(0))))), cms.ErrSyntax}, {"empty policies", der.Seq(der.Seq(id), der.Seq()), cms.ErrSyntax}, {"wrong policies type", der.Seq(der.Seq(id), der.Int(1)), cms.ErrSyntax}, + {"advisory policy", policyValue(policyOID), nil}, + {"policy qualifier", policyValue(policyOID, der.Seq(der.Seq(qualifierOID, der.Octets([]byte("qualifier"))))), nil}, + {"wrong policy information type", der.Seq(der.Seq(id), der.Seq(der.Int(1))), cms.ErrSyntax}, + {"empty policy information", policyValue(), cms.ErrSyntax}, + {"wrong policy identifier", policyValue(der.Int(1)), cms.ErrSyntax}, + {"empty policy qualifiers", policyValue(policyOID, der.Seq()), cms.ErrSyntax}, + {"wrong policy qualifiers type", policyValue(policyOID, der.Octets(nil)), cms.ErrSyntax}, + {"wrong policy qualifier identifier", policyValue(policyOID, der.Seq(der.Seq(der.Int(1), der.Octets(nil)))), cms.ErrSyntax}, + {"missing policy qualifier value", policyValue(policyOID, der.Seq(der.Seq(qualifierOID))), cms.ErrSyntax}, {"malformed additional identifier", der.Seq(der.Seq(id, der.Seq(der.Octets(sum[:]), der.Seq(names)))), cms.ErrSyntax}, } { t.Run(tc.name, func(t *testing.T) { @@ -1442,6 +1328,12 @@ func TestCMSAuthenticatedESSStructure(t *testing.T) { } else if result == nil || err != nil || !result.Signers()[0].CertificateBound() || string(result.Content()) != "ESS schema with valid signature" { t.Fatal("valid ESS certificate binding rejected", err) } + if tc.want == nil { + result, err := parsed.Verify(t.Context(), [][]byte{chain.Leaf.Raw}, cms.VerifyOptions{ExpectedContentType: cms.DataOID(), RequireCertificateBinding: true, RequireUnrestrictedCertificatePath: true}) + if result == nil || err != nil { + t.Fatal("advisory ESS policy became a certification-path restriction", err) + } + } }) } } @@ -1667,6 +1559,134 @@ func BenchmarkCMSBER(b *testing.B) { benchmarkCMSBER(b, false) } func BenchmarkCMSBERReader(b *testing.B) { benchmarkCMSBER(b, true) } +func indefiniteBER(tag byte, parts ...[]byte) []byte { + return append(append([]byte{tag, 128}, bytes.Join(parts, nil)...), 0, 0) +} + +func cmsBEREnvelope(t testing.TB, raw, octets []byte) []byte { + t.Helper() + ci, fields := cmsEnvelopeFields(t, raw) + + ec, err := der.Sequence(fields[2], 1, 2) + if err != nil { + t.Fatal(err) + } + + encap := ec[0].FullBytes + if len(ec) == 2 { + encap = append(bytes.Clone(encap), indefiniteBER(0xa0, octets)...) + } + + parts := signerBytes(fields) + parts[2] = indefiniteBER(0x30, encap) + + return indefiniteBER(0x30, ci[0].FullBytes, indefiniteBER(0xa0, indefiniteBER(0x30, parts...))) +} + +func testCMSIndependentFixture(t *testing.T, path string) { + t.Helper() + + raw, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + + certificate, err := os.ReadFile(filepath.Join(filepath.Dir(path), "signer.der")) + if err != nil { + t.Fatal(err) + } + + message, err := os.ReadFile(filepath.Join(filepath.Dir(path), "content.bin")) + if err != nil { + t.Fatal(err) + } + + signed, err := cms.ParseReader(t.Context(), bytes.NewReader(raw), cms.Limits{}) + if err != nil { + t.Fatal(err) + } + + options := cms.VerifyOptions{ExpectedContentType: cms.DataOID()} + if signed.Detached() { + options.DetachedContent = &message + } + + for _, discard := range []bool{false, true} { + options.DiscardContent = discard + + result, err := signed.Verify(t.Context(), [][]byte{certificate}, options) + if err != nil { + t.Fatal(err) + } + + if result.ContentSize() != int64(len(message)) || (!discard && !bytes.Equal(result.Content(), message)) || (discard && result.Content() != nil) { + t.Fatal("content retention", discard) + } + } + + var output bytes.Buffer + if n, err := signed.WriterTo(t.Context()).WriteTo(&output); err != nil || n != int64(len(raw)) || !bytes.Equal(output.Bytes(), raw) { + t.Fatal("DER replay", n, err) + } +} + +// Rebuild just the unsigned container, leaving the signed attributes and +// cryptographic signature intact. The negative cases are valid DER. +func rewriteSigner(t *testing.T, raw []byte, change func([]asn1.RawValue) [][]byte) []byte { + t.Helper() + ci, f := cmsEnvelopeFields(t, raw) + + si, err := der.SetFields(f[len(f)-1], 0, asn1.TagSet, 1) + if err != nil || len(si) != 1 { + t.Fatal(err) + } + + fields, err := der.Sequence(si[0], 5, 7) + if err != nil { + t.Fatal(err) + } + + parts := signerBytes(f) + parts[len(parts)-1] = der.Set(der.Seq(change(fields)...)) + + return der.Seq(ci[0].FullBytes, der.Ctx(0, der.Seq(parts...))) +} + +func cmsEnvelopeFields(t testing.TB, raw []byte) ([]asn1.RawValue, []asn1.RawValue) { + t.Helper() + + v, err := der.Read(raw, len(raw)) + if err != nil { + t.Fatal(err) + } + + ci, err := der.Sequence(v, 2, 2) + if err != nil { + t.Fatal(err) + } + + sd, err := der.Explicit(ci[1], 0) + if err != nil { + t.Fatal(err) + } + + f, err := der.Sequence(sd, 4, 6) + if err != nil { + t.Fatal(err) + } + + return ci, f +} + +func signerBytes(f []asn1.RawValue) [][]byte { + b := make([][]byte, len(f)) + for i := range f { + b[i] = f[i].FullBytes + } + + return b +} + func benchmarkCMSBER(b *testing.B, reader bool) { c := testcert.New(b) for _, size := range []int{16 << 10, 1 << 20} { @@ -1694,3 +1714,29 @@ func benchmarkCMSBER(b *testing.B, reader bool) { }) } } + +func BenchmarkCertificateBagBudget(b *testing.B) { + chain := testcert.New(b) + raw, err := cms.Sign(b.Context(), nil, []cms.Signer{{Key: chain.LeafKey, CertificateDER: chain.Leaf.Raw}}, cms.SignOptions{}) + if err != nil { + b.Fatal(err) + } + + outer, fields := cmsEnvelopeFields(b, raw) + for _, count := range []int{64, 4096, 16384} { + b.Run(strconv.Itoa(count), func(b *testing.B) { + parts := signerBytes(fields) + // A valid DER bag exceeds the certificate count before any member + // needs to be interpreted as an X.509 certificate. + parts[3] = der.Ctx(0, bytes.Repeat(der.Seq(), count)) + input := der.Seq(outer[0].FullBytes, der.Ctx(0, der.Seq(parts...))) + b.ReportAllocs() + for b.Loop() { + parsed, err := cms.Parse(b.Context(), input, cms.Limits{MaxCertificates: 1}) + if parsed != nil || !errors.Is(err, cms.ErrSyntax) { + b.Fatalf("oversized certificate bag accepted: %v", err) + } + } + }) + } +} diff --git a/cms/parse.go b/cms/parse.go index 1198dd6..2524369 100644 --- a/cms/parse.go +++ b/cms/parse.go @@ -21,16 +21,45 @@ import ( "github.com/skarm/xmlsec/internal/x509name" ) +// CMS envelope cardinalities are shared by DER validation and BER normalization. +// SignedData has two optional fields; EncapsulatedContentInfo has optional content. +const ( + contentInfoFields = 2 + signedDataMinimumFields = 4 + signedDataMaximumFields = 6 + encapsulatedContentMinimumFields = 1 + encapsulatedContentMaximumFields = 2 +) + +const ( + // Bounds certificate bytes used for signing, matching and authenticated trust. + // Inspection parsing of an embedded bag instead uses the container byte limit. + maximumCertificateBytes = 1 << 20 + // Caps positive certificate serial numbers before certificate matching or signing. + maximumSerialBits = 160 + maximumSignatureBytes = 2048 + // Bounds the subject-key identifier carried by a SignerIdentifier, in bytes. + maximumSubjectKeyIDBytes = 128 +) + // SignedData is an immutable parsed container. Parsing does not authenticate // content or embedded certificates; use Verify with caller-selected certificates. type SignedData struct { - raw []byte - content []byte - contentType asn1.ObjectIdentifier - detached bool - signers []parsedSigner + // raw owns the definite-length container encoding, including any BER + // normalization. + raw []byte + // content retains encapsulated content; detached messages supply it separately. + content []byte + // contentType identifies the encapsulated content syntax. + contentType asn1.ObjectIdentifier + // detached records the absence of encapsulated content. + detached bool + // signers retains parsed, unauthenticated signer descriptions. + signers []parsedSigner + // certificates holds embedded DER candidates without establishing trust. certificates [][]byte - limits Limits + // limits retains the normalized parsing and verification budgets. + limits Limits } type parsedSigner struct { @@ -152,7 +181,7 @@ func (s *SignedData) Certificates() [][]byte { // MatchesSigner checks only a SignerIdentifier, not a signature or PKIX trust. func (s *SignedData) MatchesSigner(index int, raw []byte) bool { - if s == nil || index < 0 || index >= len(s.signers) || len(raw) > 1<<20 { + if s == nil || index < 0 || index >= len(s.signers) || len(raw) > maximumCertificateBytes { return false } @@ -167,14 +196,14 @@ func (s *SignedData) MatchesSigner(index int, raw []byte) bool { } func (s *SignedData) parseSigners(ctx context.Context, value asn1.RawValue, digests map[crypto.Hash]struct{}, version int) error { - si, e := der.SetFields(value, 0, asn1.TagSet, s.limits.MaxSigners) + si, e := der.SetFields(value, asn1.ClassUniversal, asn1.TagSet, s.limits.MaxSigners) if e != nil || len(si) == 0 { return ErrSyntax } - wanted := 1 + wanted := signedDataVersion1 if !s.contentType.Equal(oidData()) { - wanted = 3 + wanted = signedDataVersion3 } for _, rv := range si { @@ -197,7 +226,7 @@ func (s *SignedData) parseSigners(ctx context.Context, value asn1.RawValue, dige } if len(p.ski) > 0 { - wanted = 3 + wanted = signedDataVersion3 } s.signers = append(s.signers, p) @@ -212,9 +241,12 @@ func (s *SignedData) parseSigners(ctx context.Context, value asn1.RawValue, dige // parseOwned takes a strictly validated DER value backed by private storage. func parseOwned(ctx context.Context, v asn1.RawValue, l Limits) (*SignedData, error) { + // Version, digestAlgorithms and encapContentInfo precede optional fields. + const signedDataRequiredPrefixFields = 3 + b := v.FullBytes - ci, e := der.Sequence(v, 2, 2) + ci, e := der.Sequence(v, contentInfoFields, contentInfoFields) if e != nil || !der.OIDEqual(ci[0], oidSignedData()) { return nil, ErrSyntax } @@ -224,7 +256,7 @@ func parseOwned(ctx context.Context, v asn1.RawValue, l Limits) (*SignedData, er return nil, ErrSyntax } - f, e := der.Sequence(sd, 4, 6) + f, e := der.Sequence(sd, signedDataMinimumFields, signedDataMaximumFields) if e != nil { return nil, ErrSyntax } @@ -239,19 +271,19 @@ func parseOwned(ctx context.Context, v asn1.RawValue, l Limits) (*SignedData, er return nil, e } - ec, e := der.Sequence(f[2], 1, 2) + ec, e := der.Sequence(f[2], encapsulatedContentMinimumFields, encapsulatedContentMaximumFields) if e != nil { return nil, ErrSyntax } - s := &SignedData{raw: b, limits: l, detached: len(ec) == 1} + s := &SignedData{raw: b, limits: l, detached: len(ec) == encapsulatedContentMinimumFields} if der.Decode(ec[0], &s.contentType) != nil { return nil, ErrSyntax } if !s.detached { x, e := der.Explicit(ec[1], 0) - if e != nil || !der.Is(x, 0, 4, false) || len(x.Bytes) > l.MaxContentBytes { + if e != nil || !der.Is(x, asn1.ClassUniversal, asn1.TagOctetString, false) || len(x.Bytes) > l.MaxContentBytes { return nil, ErrSyntax } } @@ -261,8 +293,8 @@ func parseOwned(ctx context.Context, v asn1.RawValue, l Limits) (*SignedData, er s.content = x.Bytes } - i := 3 - if i < len(f) && der.Is(f[i], 2, 0, true) { + i := signedDataRequiredPrefixFields + if i < len(f) && der.Is(f[i], asn1.ClassContextSpecific, 0, true) { certificates, e := parseCertificateBag(ctx, f[i], l) if e != nil { return nil, e @@ -289,7 +321,7 @@ func parseOwned(ctx context.Context, v asn1.RawValue, l Limits) (*SignedData, er } func parseDigestAlgorithms(value asn1.RawValue, l Limits) (map[crypto.Hash]struct{}, error) { - ds, e := der.SetFields(value, 0, asn1.TagSet, l.MaxSigners) + ds, e := der.SetFields(value, asn1.ClassUniversal, asn1.TagSet, l.MaxSigners) if e != nil || len(ds) == 0 { return nil, ErrSyntax } @@ -321,7 +353,7 @@ func parseCertificateBag(ctx context.Context, value asn1.RawValue, l Limits) ([] var certificates [][]byte // OpenSSL emits a definite-length BER CertificateSet in insertion order. Only // this untrusted certificate bag may be unsorted; signedAttrs remain strict DER. - cs, e := der.Children(value) + cs, e := der.ChildrenLimit(value, l.MaxCertificates) if e != nil || len(cs) == 0 || len(cs) > l.MaxCertificates { return nil, ErrSyntax } @@ -333,7 +365,7 @@ func parseCertificateBag(ctx context.Context, value asn1.RawValue, l Limits) ([] return nil, err } - if !der.Is(rv, 0, asn1.TagSequence, true) { + if !der.Is(rv, asn1.ClassUniversal, asn1.TagSequence, true) { return nil, ErrSyntax } @@ -366,7 +398,16 @@ func certificateMatch(ctx context.Context, p parsedSigner, c *x509.Certificate) } func parseSigner(v asn1.RawValue, l Limits) (parsedSigner, error) { - f, e := der.Sequence(v, 5, 7) + // Signed and unsigned attributes are optional; the first three fields + // precede signedAttrs and the signature algorithm/value pair. + const ( + signerInfoMinimumFields = 5 + signerInfoMaximumFields = 7 + signerInfoPrefixFields = 3 + issuerAndSerialFields = 2 // issuer Name and serialNumber + ) + + f, e := der.Sequence(v, signerInfoMinimumFields, signerInfoMaximumFields) if e != nil { return parsedSigner{}, ErrSyntax } @@ -378,15 +419,15 @@ func parseSigner(v asn1.RawValue, l Limits) (parsedSigner, error) { p := parsedSigner{} - if der.Is(f[1], 2, 0, false) { - if version != 3 || len(f[1].Bytes) < 1 || len(f[1].Bytes) > 128 { + if der.Is(f[1], asn1.ClassContextSpecific, 0, false) { + if version != signerSubjectKeyIDVersion || len(f[1].Bytes) < 1 || len(f[1].Bytes) > maximumSubjectKeyIDBytes { return p, ErrSyntax } p.ski = f[1].Bytes } else { - x, e := der.Sequence(f[1], 2, 2) - if e != nil || version != 1 || !der.Is(x[0], 0, asn1.TagSequence, true) || der.Decode(x[1], &p.serial) != nil || p.serial == nil || p.serial.Sign() <= 0 || p.serial.BitLen() > 160 { + x, e := der.Sequence(f[1], issuerAndSerialFields, issuerAndSerialFields) + if e != nil || version != signerIssuerSerialVersion || !der.Is(x[0], asn1.ClassUniversal, asn1.TagSequence, true) || der.Decode(x[1], &p.serial) != nil || p.serial == nil || p.serial.Sign() <= 0 || p.serial.BitLen() > maximumSerialBits { return p, ErrSyntax } @@ -398,9 +439,9 @@ func parseSigner(v asn1.RawValue, l Limits) (parsedSigner, error) { return p, ErrSyntax } - i := 3 - if der.Is(f[i], 2, 0, true) { - p.signed, e = parseAttributes(f[i], 0, l.MaxAttributes) + i := signerInfoPrefixFields + if der.Is(f[i], asn1.ClassContextSpecific, signedAttributesTag, true) { + p.signed, e = parseAttributes(f[i], signedAttributesTag, l.MaxAttributes) if e != nil { return p, e } @@ -421,13 +462,13 @@ func parseSigner(v asn1.RawValue, l Limits) (parsedSigner, error) { } i++ - if der.Decode(f[i], &p.sig) != nil || len(p.sig) == 0 || len(p.sig) > 2048 { + if der.Decode(f[i], &p.sig) != nil || len(p.sig) == 0 || len(p.sig) > maximumSignatureBytes { return p, ErrSyntax } i++ if i < len(f) { - p.unsigned, e = parseAttributes(f[i], 1, l.MaxAttributes) + p.unsigned, e = parseAttributes(f[i], unsignedAttributesTag, l.MaxAttributes) if e != nil { return p, e } diff --git a/cms/sign.go b/cms/sign.go index 424b794..17589b3 100644 --- a/cms/sign.go +++ b/cms/sign.go @@ -17,6 +17,11 @@ import ( "github.com/skarm/xmlsec/internal/x509cert" ) +const ( + signingAlgorithmIdentifierBytes = 4096 + signingOutputChunkBytes = 32 << 10 +) + // Signer supplies a signing key, its matching certificate and additional signed // attributes. type Signer struct { @@ -100,9 +105,9 @@ func SignReader(ctx context.Context, dst io.Writer, src io.Reader, signers []Sig return 0, ErrPolicy } - encoder := signedDataEncoder{contentType: ct, limits: l, certSeen: certificateSet{}, digestSeen: map[crypto.Hash]struct{}{}, version: 1} + encoder := signedDataEncoder{contentType: ct, limits: l, certSeen: certificateSet{}, digestSeen: map[crypto.Hash]struct{}{}, version: signedDataVersion1} if !o.ContentType.Equal(oidData()) { - encoder.version = 3 + encoder.version = signedDataVersion3 } certificates, hashes, err := encoder.prepareSigners(ctx, signers) @@ -231,15 +236,15 @@ func (e *signedDataEncoder) addSigner(ctx context.Context, s Signer, c *x509.Cer } // The IMPLICIT [0] tag preserves the authenticated SET length and contents. - signed[0] = 0xa0 + signed[0] = encodedContext0Tag sid, sidVersion, err := signerIdentifier(s, c) if err != nil { return err } - if sidVersion == 3 { - e.version = 3 + if sidVersion == signerSubjectKeyIDVersion { + e.version = signedDataVersion3 } e.infos = append(e.infos, der.Seq(der.Int(sidVersion), sid, digestAI, signed, algAI, der.Octets(value))) @@ -252,12 +257,12 @@ func (e *signedDataEncoder) addSigner(ctx context.Context, s Signer, c *x509.Cer } func (e *signedDataEncoder) signerCertificate(s Signer) (*x509.Certificate, error) { - if nilcheck.IsNil(s.Key) || len(s.CertificateDER) > 1<<20 || len(s.Attributes) > e.limits.MaxAttributes { + if nilcheck.IsNil(s.Key) || len(s.CertificateDER) > maximumCertificateBytes || len(s.Attributes) > e.limits.MaxAttributes { return nil, ErrPolicy } c, err := x509cert.ParseDefault(bytes.Clone(s.CertificateDER)) - if err != nil || c.SerialNumber == nil || c.SerialNumber.Sign() <= 0 || c.SerialNumber.BitLen() > 160 { + if err != nil || c.SerialNumber == nil || c.SerialNumber.Sign() <= 0 || c.SerialNumber.BitLen() > maximumSerialBits { return nil, ErrCertificate } @@ -281,8 +286,8 @@ func (e *signedDataEncoder) signedAttributes(s Signer, c *x509.Certificate, dige attrs = append(attrs, a) } - av, _ := der.Read(algAI, 4096) - protect := der.Seq(digestAI, der.Wrap(2, 1, true, av.Bytes)) + av, _ := der.Read(algAI, signingAlgorithmIdentifierBytes) + protect := der.Seq(digestAI, der.Wrap(asn1.ClassContextSpecific, algorithmProtectionSignatureTag, true, av.Bytes)) attrs = append(attrs, Attribute{oidAlgorithmProtect(), [][]byte{protect}}) for _, a := range s.Attributes { @@ -302,7 +307,7 @@ func (e *signedDataEncoder) addCertificates(ctx context.Context, extra [][]byte) return err } - if len(b) > 1<<20 { + if len(b) > maximumCertificateBytes { return ErrPolicy } @@ -349,14 +354,14 @@ func (e *signedDataEncoder) encodeTo(dst io.Writer, o SignOptions) error { return ErrPolicy } // ContentInfo, SignedData and EncapsulatedContentInfo nesting is definite-length. - for _, part := range [][]byte{der.Header(0x30, outerLength), oid, der.Header(0xa0, derSize(signedLength)), der.Header(0x30, signedLength), version, digests, der.Header(0x30, contentLength), e.contentType} { + for _, part := range [][]byte{der.Header(encodedSequenceTag, outerLength), oid, der.Header(encodedContext0Tag, derSize(signedLength)), der.Header(encodedSequenceTag, signedLength), version, digests, der.Header(encodedSequenceTag, contentLength), e.contentType} { if err := writeDERPart(dst, part); err != nil { return err } } if !o.Detached { - for _, part := range [][]byte{der.Header(0xa0, derSize(len(e.content))), der.Header(4, len(e.content)), e.content} { + for _, part := range [][]byte{der.Header(encodedContext0Tag, derSize(len(e.content))), der.Header(asn1.TagOctetString, len(e.content)), e.content} { if err := writeDERPart(dst, part); err != nil { return err } @@ -364,7 +369,7 @@ func (e *signedDataEncoder) encodeTo(dst io.Writer, o SignOptions) error { } if !o.OmitCertificates { - if err := writeDERPart(dst, der.Header(0xa0, certificateLength)); err != nil { + if err := writeDERPart(dst, der.Header(encodedContext0Tag, certificateLength)); err != nil { return err } @@ -375,7 +380,7 @@ func (e *signedDataEncoder) encodeTo(dst io.Writer, o SignOptions) error { } } - if err := writeDERPart(dst, der.Header(0x31, infosLength)); err != nil { + if err := writeDERPart(dst, der.Header(encodedSetTag, infosLength)); err != nil { return err } @@ -454,21 +459,21 @@ func signerIdentifier(s Signer, c *x509.Certificate) ([]byte, int, error) { sid := der.Seq(c.RawIssuer, serial) if !s.SubjectKeyIdentifier { - return sid, 1, nil + return sid, signerIssuerSerialVersion, nil } - if len(c.SubjectKeyId) < 1 || len(c.SubjectKeyId) > 128 { + if len(c.SubjectKeyId) < 1 || len(c.SubjectKeyId) > maximumSubjectKeyIDBytes { return nil, 0, ErrCertificate } - return der.Wrap(2, 0, false, c.SubjectKeyId), 3, nil + return der.Wrap(asn1.ClassContextSpecific, 0, false, c.SubjectKeyId), signerSubjectKeyIDVersion, nil } func derSize(length int) int { return len(der.Header(0, length)) + length } func writeDERPart(dst io.Writer, value []byte) error { for len(value) > 0 { - chunk := value[:min(len(value), 32<<10)] + chunk := value[:min(len(value), signingOutputChunkBytes)] n, err := dst.Write(chunk) if err != nil { diff --git a/cms/trust.go b/cms/trust.go index 9a2d75b..7457500 100644 --- a/cms/trust.go +++ b/cms/trust.go @@ -12,6 +12,13 @@ import ( "github.com/skarm/xmlsec/pki" ) +const ( + maximumEvidenceBytes = 64 << 20 + maximumEvidenceCRLs = 256 + maximumEvidenceIntermediates = 64 + maximumEvidenceOCSPResponses = 256 +) + // TrustOptions selects an immutable PKIX and offline revocation verifier and an // explicit validation time. Embedded certificates are untrusted path candidates. // The caller supplies trust anchors through Verifier; CMS SigningTime cannot @@ -86,11 +93,11 @@ func (s *SignedData) applyTrust(ctx context.Context, result *Result, options Tru } func (s *SignedData) signerTrustEvidence(ctx context.Context, evidence pki.Evidence) (pki.Evidence, error) { - if len(evidence.Intermediates) > 64 || len(evidence.CRLs) > 256 || len(evidence.OCSPResponses) > 256 { + if len(evidence.Intermediates) > maximumEvidenceIntermediates || len(evidence.CRLs) > maximumEvidenceCRLs || len(evidence.OCSPResponses) > maximumEvidenceOCSPResponses { return pki.Evidence{}, ErrPolicy } - remaining := 64 << 20 + remaining := maximumEvidenceBytes for _, group := range [][][]byte{evidence.Intermediates, evidence.CRLs, evidence.OCSPResponses} { for _, raw := range group { @@ -103,7 +110,7 @@ func (s *SignedData) signerTrustEvidence(ctx context.Context, evidence pki.Evide } merged := evidence - merged.Intermediates = make([][]byte, 0, min(64, len(evidence.Intermediates)+len(s.certificates))) + merged.Intermediates = make([][]byte, 0, min(maximumEvidenceIntermediates, len(evidence.Intermediates)+len(s.certificates))) seen := make(map[[sha256.Size]byte]struct{}) for _, group := range [][][]byte{evidence.Intermediates, s.certificates} { @@ -112,7 +119,7 @@ func (s *SignedData) signerTrustEvidence(ctx context.Context, evidence pki.Evide return pki.Evidence{}, err } - if len(candidate) > 1<<20 { + if len(candidate) > maximumCertificateBytes { return pki.Evidence{}, ErrPolicy } @@ -123,7 +130,7 @@ func (s *SignedData) signerTrustEvidence(ctx context.Context, evidence pki.Evide seen[digest] = struct{}{} - if len(merged.Intermediates) == 64 { + if len(merged.Intermediates) == maximumEvidenceIntermediates { return pki.Evidence{}, ErrPolicy } @@ -209,17 +216,17 @@ func unrestrictedESSBinding(attrs []Attribute) error { continue } - value, err := der.Read(raw, 1<<20) + value, err := der.Read(raw, maximumAttributeBytes) if err != nil { return ErrSyntax } - fields, err := der.Sequence(value, 1, 2) + fields, err := der.Sequence(value, signingCertificateMinimumFields, signingCertificateMaximumFields) if err != nil { return ErrSyntax } - ids, err := der.Sequence(fields[0], 1, 32) + ids, err := der.Sequence(fields[0], 1, maximumSigningCertificateIDs) if err != nil { return ErrSyntax } diff --git a/cms/verify.go b/cms/verify.go index 8742fe7..158f920 100644 --- a/cms/verify.go +++ b/cms/verify.go @@ -36,8 +36,9 @@ type VerifyOptions struct { // AllowLegacyESSCertID permits the SHA-1 ESSCertID certificate identifier. // It does not enable SHA-1 CMS signatures. AllowLegacyESSCertID bool - // RequireUnrestrictedCertificatePath rejects ESS path/policy restrictions - // that this CMS profile cannot apply. Historical trust workflows must set it, + // RequireUnrestrictedCertificatePath rejects additional ESS certificate + // identifiers that restrict path selection. Advisory ESS policies do not + // change the caller's PKIX policy. Historical trust workflows must set it, // just as VerifyWithTrust always rejects such unsupported restrictions. RequireUnrestrictedCertificatePath bool } @@ -45,12 +46,20 @@ type VerifyOptions struct { // SignerResult authenticates one caller-selected certificate and signed // attributes. It does not establish a certificate path or a trusted signing time. type SignerResult struct { + // certificate retains the exact DER certificate used to verify this signer. certificate []byte - signed []Attribute - bound bool - trust *pki.Result - hash crypto.Hash - algorithm string + // signed contains authenticated signed attributes. + signed []Attribute + // bound records whether signed ESS attributes bind the exact signer + // certificate. + bound bool + // trust is populated only after the configured PKIX checks succeed. + trust *pki.Result + // hash is the authenticated digest/signature hash; zero marks an unverified + // result. + hash crypto.Hash + // algorithm is the normalized authenticated signature algorithm OID. + algorithm string } // CertificateDER returns an owned copy of the certificate DER. @@ -74,10 +83,15 @@ func (r SignerResult) SignatureAlgorithmOID() string { return r.algorithm } // Result is returned only after every signer and the content have been checked. // Accessors return copies; a Result alone makes no claim about PKIX trust. type Result struct { - content []byte + // content retains authenticated content when verification keeps it in memory. + content []byte + // contentSize counts authenticated bytes even when detached content is + // streamed. contentSize int64 + // contentType identifies the authenticated CMS content syntax. contentType asn1.ObjectIdentifier - signers []SignerResult + // signers contains the successfully verified signer results. + signers []SignerResult } // Content returns an owned copy of retained authenticated content. A detached @@ -266,7 +280,7 @@ func (s *SignedData) verifySigner(ctx context.Context, p parsedSigner, cert *x50 } state := h.New() - _, _ = state.Write([]byte{0x31}) + _, _ = state.Write([]byte{encodedSetTag}) writer := &streamio.Writer{Context: ctx, Target: state, Limit: int64(s.limits.MaxBytes), LimitError: ErrPolicy} if err := writeDERPart(writer, p.signedInput[1:]); err != nil { @@ -307,7 +321,7 @@ func parseVerificationCertificates(ctx context.Context, expected [][]byte) ([]*x return nil, err } - if len(raw) > 1<<20 || !seen.add(raw) { + if len(raw) > maximumCertificateBytes || !seen.add(raw) { return nil, ErrCertificate } @@ -354,19 +368,25 @@ func signerCertificateIndex(ctx context.Context, p parsedSigner, certs []*x509.C } func normalizeSignature(aid pkix.AlgorithmIdentifier, h crypto.Hash) (pkix.AlgorithmIdentifier, error) { + const ( + rsaSHA256Arc = 11 + rsaSHA384Arc = 12 + rsaSHA512Arc = 13 + ) + if aid.Algorithm.Equal(signature.RSA()) { if !signature.NullOrAbsent(aid.Parameters) { return aid, ErrPolicy } - n := 11 + n := rsaSHA256Arc switch h { case crypto.SHA256: case crypto.SHA384: - n = 12 + n = rsaSHA384Arc case crypto.SHA512: - n = 13 + n = rsaSHA512Arc default: return aid, ErrPolicy } diff --git a/docs/architecture.md b/docs/architecture.md index 1d49bdb..cdd76a9 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -36,10 +36,20 @@ trust separate. [usage](usage.md) explains their composition. ## SAML -`binding`, `metadata` and `protocol` handle wire formats, trusted role descriptions -and message validation. `sp` and `idp` compose solicited WebSSO; `state` and -`replay` provide bounded single-process memory stores. Multiple instances need +Packages under `saml/` separate transport, protocol validation and stateful flows: + +- `binding`, `metadata` and `protocol`: wire formats, trusted role descriptions + and authenticated message validation. +- `sp` and `idp`: solicited WebSSO, including encrypted claims, Artifact and ECP. +- `ecp` and `proof`: PAOS routing, channel bindings and X.509 holder-of-key proof. +- `artifact`: recipient-bound single-use SOAP artifact resolution. +- `session`: logout and NameID management with application-owned session state. +- `authority`: queries, assertion-ID/URI retrieval and NameID Mapping. +- `state` and `replay`: pending WebSSO requests and atomic replay rejection. + +The supplied memory stores coordinate one process. Multiple instances need shared atomic storage. Authentication, cookies and HTTP handlers belong to callers. +See [SAML integration](saml.md) and [authority integration](saml-authority.md). ## Resources and ownership diff --git a/docs/compatibility.md b/docs/compatibility.md index d132fa6..9341ed8 100644 --- a/docs/compatibility.md +++ b/docs/compatibility.md @@ -7,8 +7,9 @@ audit have not been established. [standards](standards.md) lists primary sources ## Supported scope - XML 1.0 Fifth Edition, UTF-8/UTF-16, immutable namespace-aware snapshots and - explicit ID typing. The security parser rejects DTDs/entities. XML 1.1, - UTF-32, schema defaults and XSD validation are unsupported. + explicit ID typing. The default security parser rejects DTDs and external + entities; character references and the five predefined entities are accepted. + XML 1.1, UTF-32, schema defaults and XSD validation are unsupported. - Exclusive C14N and Canonical XML 1.0/1.1, with/without comments, over documents, subtrees and node sets. Sparse selections can produce fragments. - XMLDSig enveloped/enveloping/detached signatures, Objects, multiple signatures @@ -17,8 +18,9 @@ audit have not been established. [standards](standards.md) lists primary sources Complete fragment URIs are allowlisted and resolvers supply identified octets. Base-URI resolution is opt-in; arbitrary LEIRI/fragment selection is unsupported. - RSA/ECDSA SHA-2, RSA-PSS, Ed25519/Ed25519ph and separate HMAC profiles. - Legacy verification/HMAC policies explicitly permit SHA-1; legacy public-key - signing remains disabled. Compatibility algorithms are disabled in FIPS mode. + Legacy verification/HMAC policies explicitly permit SHA-1; RSA-SHA1 generation + additionally requires `Policy.AllowLegacySigning`. DSA signing is unsupported. + Compatibility algorithms are disabled in FIPS mode. - KeyInfo candidates, X509Data groups/selectors and explicitly authorized retrieval/processing. KeyInfo has its own resources/base URI. Omitted DSA parameters need an explicit bounded application context. Descriptions and @@ -27,10 +29,13 @@ audit have not been established. [standards](standards.md) lists primary sources and W3C streamable execution are unsupported. Streaming annotations operate on trees; independent-record processing is separate. Complete Basic XSLT remains open; see [validation profiles](language-baseline.md). -- Language XML supports bounded nonvalidating DTD processing and explicitly - enabled single-byte encodings. External entities require configured resources. - Language input preserves relative namespace names; the security parser and - C14N reject them. See [input profiles](transforms.md#xml-input-profiles). +- `document.ParseOptions{NonValidating: true}` and language XML support bounded + nonvalidating DTD processing. `NonValidating` also enables supported + single-byte encodings; language input requires `AllowSingleByteEncodings` + for those encodings. External entities require configured resources. Engine + input, including DTD-free `Engine.Parse` and stylesheet records, preserves + relative namespace names, as does `NonValidating`. The default document parser + and C14N reject them. See [input profiles](transforms.md#xml-input-profiles). - PKIX with explicit roots/time and offline complete/scoped/indirect/base-delta CRLs. OCSP selects an exact CertID and authorized responder. Required missing evidence fails without fetching. Acquisition metadata grants no trust or PoE. @@ -45,11 +50,23 @@ audit have not been established. [standards](standards.md) lists primary sources EN 319 102-1 V1.4.1 workflow, not full historical validation. Evidence records, historical countersignature/attribute-authority workflows and remaining indication branches are outside that subset. Unknown unsigned properties fail. -- Signed SAML metadata, POST/Redirect and solicited WebSSO. SLO, Artifact/ECP, - queries, unsolicited SSO and encrypted assertions/names/attributes are unsupported. - SAML/closed XAdES use positive four-digit UTC years and up to nine fractional - digits. The separate baseline API retains extended dateTime lexical values; - use `CompareSigningTime` when `time.Time` cannot represent them. +- Signed SAML metadata and solicited WebSSO over POST/Redirect/Artifact or PAOS, + with encrypted assertions, NameIDs and attributes. SLO and NameID management + use application-owned atomic session state; type 0x0004 artifacts are + recipient-bound and single-use. Bearer ECP follows the 2005 profile with + Approved Errata 05; selected CS01 channel-binding and X.509 HoK flows support + TLS client or whole-SOAP XML proof. XML proof has distinct W3C-transform and + CS01-compatibility APIs; full CS01 conformance remains unresolved. See + [SAML integration](saml.md) for metadata, key, transport and state contracts. + Authentication/attribute/authorization queries, assertion-ID/URI retrieval and + NameID Mapping use separate [authority APIs](saml-authority.md). Custom BaseID + schemas, proxying and unsolicited SSO are unsupported. Independent test coverage + and peer limitations are in [testing](testing.md#saml-interoperability). + Protocol-level HMAC requires separately provisioned `SharedSecret` credentials; + SP/IdP metadata helpers use public keys. +- SAML/closed XAdES use positive four-digit UTC years and up to nine fractional + digits. The separate XAdES baseline API retains extended dateTime lexical + values; use `CompareSigningTime` when `time.Time` cannot represent them. - PEM/DER PKCS#8, PKCS#1, SEC1, SPKI, certificates and CRLs. Protected PKCS#8 uses PBES2/PBKDF2/SHA-2/AES-CBC. PKCS#12 requires a MAC and matching key/certificate; selected BER/nested/multiple-identity imports require explicit options. @@ -64,7 +81,10 @@ audit have not been established. [standards](standards.md) lists primary sources - CBC/TripleDES data encryption and TripleDES Key Wrap are explicit legacy modes, disabled by default and in FIPS mode. CBC plaintext is unauthenticated. Bounded key graphs can resolve nested/retrieved/derived keys; key-document and - ciphertext resources have separate policies. RSA PKCS#1 v1.5, finite-field DH, + ciphertext resources have separate policies. RSA PKCS#1 v1.5 requires explicit + `KeyTransportAlgorithm: xmlenc.RSA15`, `AllowLegacy` and prior ciphertext + authentication; this transport is available in the RSA APIs, not key graphs. + Finite-field DH, XMLENC PBKDF2, EXI and padded AES key wrap remain unsupported. General key-manager/HSM management, XMLDSig decryption transforms, arbitrary diff --git a/docs/conformance.json b/docs/conformance.json index 97110c2..baf37da 100644 --- a/docs/conformance.json +++ b/docs/conformance.json @@ -1,7 +1,7 @@ { "schema_version": 4, "reviewed_on": "2026-10-25", - "completeness": "All 20 selected profile inventories are complete at named clause, grammar production, datatype, algorithm, property, role/binding/message and formal function/operator contract granularity. Related conditional rules may share one contract record. Inventory completion does not establish full implementation, test coverage or standards conformance; those gaps remain explicit.", + "completeness": "All 20 selected profile inventories are complete at named clause, grammar production, datatype, algorithm, property, role/binding/message and formal function/operator contract granularity. Related conditional rules may share one contract record. Inventory completion does not establish full implementation, test coverage or standards conformance; those gaps remain explicit. A separate Authority/query/mapping role inventory was added on 2026-11-05 and remains partial for common-role obligations; the named new protocols have executable implementation and independent message tests.", "scope": "Experimental XML security toolkit with bounded signatures, encryption, key containers, XPath/XSLT, offline trust/evidence verification, selected baseline/closed/historical XAdES workflows and solicited SAML WebSSO; not a full standards conformance claim.", "stage": "experimental-bounded-profiles", "documents": [ @@ -2729,7 +2729,7 @@ "profiles": [ { "id": "xml-security", - "title": "DTD-free XML snapshots", + "title": "Security and explicit nonvalidating XML snapshots", "role": "non-validating XML processor", "modes": [ "parse", @@ -2748,8 +2748,8 @@ ], "claim": "restricted", "inventory": "complete", - "gap": "The selected non-validating role has a DTD-free implementation. General DTD/entity/default behavior, relative namespace acceptance, some legal 1.x declarations and independent clause-specific evidence remain open.", - "closure": "Implement the applicable non-validating processor gaps through a separate bounded mode and complete independent clause-specific evidence before a full claim; preserve the default DTD-free profile.", + "gap": "A public bounded nonvalidating mode now processes DTD declarations, entities, defaults, relative namespaces and XML 1.x declarations using XML 1.0 rules. Complete clause-specific output evidence remains open; the finite publisher binary run is documented separately and is not a full processor certification.", + "closure": "Retain the DTD-free default and explicit external resolver; complete independent output/notation/encoding witnesses for every applicable clause before a full conformance claim.", "matrix": [ { "document": "XML10", @@ -3679,7 +3679,7 @@ ], "claim": "restricted", "inventory": "complete", - "gap": "The required algorithm roles and explicit bounded key graph are implemented with separate secure defaults/legacy permissions and independent controls. Optional RSA-v1.5, finite-field DH, PBKDF2, EXI, padded/general-size key wrap, DTD-aware preprocessing and general/complete optional transform programs remain outside the selected core. Timing-oracle resistance is not certified by equal errors or dummy keys. This is not an unrestricted XMLENC certification claim.", + "gap": "The required algorithm roles and explicit bounded key graph are implemented with separate secure defaults/legacy permissions and independent controls. Explicit RSA-v1.5 is available in the RSA entry points with implicit-rejection provider options and prior ciphertext authentication. Optional finite-field DH, PBKDF2, EXI, padded/general-size key wrap, DTD-aware preprocessing and general/complete optional transform programs remain outside the selected core. Timing-oracle resistance is not certified by equal errors or dummy keys. This is not an unrestricted XMLENC certification claim.", "closure": "Retain exact caller-selected algorithms, no implicit I/O, generic cryptographic failures and CBC integrity/key-separation requirements; close optional extension records only when those features and their own proof obligations are selected.", "matrix": [ { @@ -7953,7 +7953,7 @@ ], "claim": "restricted", "inventory": "complete", - "gap": "Only solicited signed Redirect/POST WebSSO and bounded SP/IdP metadata are implemented. Mandatory Artifact, SOAP, ECP, logout, NameID management, encrypted counterparts and selected legacy algorithm duties remain open; full XSD and optional profile extensions are separate gaps.", + "gap": "Full-role conformance and complete independent deployment evidence remain open. ECP CS01 2.3.11 XML-proof interpretation is unresolved; see docs/saml.md#ecp-2013-holder-of-key and docs/saml-authority.md.", "closure": "Close every linked applicable implementation/evidence gap before expanding the conformance claim; a complete inventory does not imply full standards implementation.", "matrix": [ { @@ -8598,7 +8598,7 @@ ], "claim": "restricted", "inventory": "complete", - "gap": "Only solicited signed Redirect/POST WebSSO and bounded SP/IdP metadata are implemented. Mandatory Artifact, SOAP, ECP, logout, NameID management, encrypted counterparts and selected legacy algorithm duties remain open; full XSD and optional profile extensions are separate gaps.", + "gap": "Full-role conformance and complete independent deployment evidence remain open. ECP CS01 2.3.11 XML-proof interpretation is unresolved; see docs/saml.md#ecp-2013-holder-of-key and docs/saml-authority.md.", "closure": "Close every linked applicable implementation/evidence gap before expanding the conformance claim; a complete inventory does not imply full standards implementation.", "matrix": [ { @@ -9265,17 +9265,132 @@ "closure": "" } ] + }, + { + "id": "saml-authority", + "title": "SAML Authority queries, retrieval and NameID Mapping", + "role": "SAML authorities and requester", + "modes": [ + "query", + "assertion-id", + "mapping", + "uri", + "metadata" + ], + "documents": [ + "SAMLCORE", + "SAMLBIND", + "SAMLPROF", + "SAMLMD", + "SAMLCONF" + ], + "scope_ids": [ + "SAML-QUERY-MAPPING" + ], + "package_docs": [ + "saml/authority/authority.go", + "saml/protocol/doc.go", + "saml/metadata/doc.go" + ], + "claim": "restricted", + "inventory": "partial", + "gap": "The named protocols have producer, consumer, SOAP/HTTP integration, local regressions and independent message/schema/signature checks. Full Authority-specific common-clause and deployment evidence has not been inventoried or certified.", + "closure": "Complete and verify the remaining common-role obligation inventory without confusing implementation of the requested protocols with universal SAML conformance.", + "matrix": [ + { + "document": "SAMLCORE", + "modes": [ + "assertion-id", + "mapping", + "query" + ], + "requirements": [ + "N-SAML-AUTHORITY-AUTHN-QUERY", + "N-SAML-AUTHORITY-ATTRIBUTE-QUERY", + "N-SAML-AUTHORITY-AUTHZ-QUERY", + "N-SAML-AUTHORITY-ASSERTION-ID", + "N-SAML-AUTHORITY-QUERY-MATCH", + "N-SAML-AUTHORITY-QUERY-CONTEXT", + "N-SAML-AUTHORITY-MAPPING-CORE", + "N-SAML-AUTHORITY-SAML-PREFIXLIST" + ], + "unmapped_clauses": [ + "Common datatype, extension, algorithm/transport and complete Authority-role obligations beyond the named operations." + ], + "reason": "Named operation contracts are assessed; the remaining common-role inventory is explicitly incomplete.", + "closure": "Inventory the remaining applicable clauses and attach independent positive/negative witnesses before a full claim." + }, + { + "document": "SAMLBIND", + "modes": [ + "uri" + ], + "requirements": [ + "N-SAML-AUTHORITY-URI-BINDING" + ], + "unmapped_clauses": [ + "Common datatype, extension, algorithm/transport and complete Authority-role obligations beyond the named operations." + ], + "reason": "Named operation contracts are assessed; the remaining common-role inventory is explicitly incomplete.", + "closure": "Inventory the remaining applicable clauses and attach independent positive/negative witnesses before a full claim." + }, + { + "document": "SAMLPROF", + "modes": [ + "mapping", + "query" + ], + "requirements": [ + "N-SAML-AUTHORITY-QUERY-PROFILE", + "N-SAML-AUTHORITY-MAPPING-PROFILE" + ], + "unmapped_clauses": [ + "Common datatype, extension, algorithm/transport and complete Authority-role obligations beyond the named operations." + ], + "reason": "Named operation contracts are assessed; the remaining common-role inventory is explicitly incomplete.", + "closure": "Inventory the remaining applicable clauses and attach independent positive/negative witnesses before a full claim." + }, + { + "document": "SAMLMD", + "modes": [ + "metadata" + ], + "requirements": [ + "N-SAML-AUTHORITY-AUTHORITY-METADATA" + ], + "unmapped_clauses": [ + "Common datatype, extension, algorithm/transport and complete Authority-role obligations beyond the named operations." + ], + "reason": "Named operation contracts are assessed; the remaining common-role inventory is explicitly incomplete.", + "closure": "Inventory the remaining applicable clauses and attach independent positive/negative witnesses before a full claim." + }, + { + "document": "SAMLCONF", + "modes": [ + "query" + ], + "requirements": [ + "N-SAML-AUTHORITY-QUERY-MATRIX" + ], + "unmapped_clauses": [ + "Common datatype, extension, algorithm/transport and complete Authority-role obligations beyond the named operations." + ], + "reason": "Named operation contracts are assessed; the remaining common-role inventory is explicitly incomplete.", + "closure": "Inventory the remaining applicable clauses and attach independent positive/negative witnesses before a full claim." + } + ] } ], "requirements": [ { "id": "XML-PARSE", - "notes": "UTF-8/UTF-16LE/BE, DTD-free, coalesced text nodes; Fifth Edition names including U+037F and supplementary characters through U+EFFFF; not a complete XML processor conformance claim", + "notes": "The default is DTD-free UTF-8/UTF-16. Explicit document.ParseOptions.NonValidating enables bounded XML 1.0 DTD/entity/default processing, supported single-byte encodings and Namespaces 1.0 relative names. Document.DTD returns owned notation, unparsed-entity and DTD PI metadata. No implicit external I/O, DTD validity checking or XSD validation.", "source": "https://www.w3.org/TR/xml/", "status": "implemented-restricted", "tests": [ "document/document_test.go", - "document/encoding_test.go" + "document/encoding_test.go", + "document/parse_options_test.go" ], "title": "XML 1.0 / Namespaces" }, @@ -9855,7 +9970,7 @@ "saml/idp", "saml/state" ], - "limitations": "No unsolicited/SLO/Artifact/ECP/queries/encrypted assertion application profile or real external IdP interop.", + "limitations": "Unsolicited SSO, arbitrary BaseID and automatic proxying are outside this WebSSO profile. SLO, Artifact/ECP and authority/mapping workflows have separate APIs. RSA encryption recipients must be metadata-authorized; protocol-level HMAC uses separately provisioned secrets.", "standards": [ "SAML2 Core", "SAML2 Bindings", @@ -9864,7 +9979,11 @@ "status": "implemented-closed-profile", "tests": [ "tests/integration/saml/sso_test.go", - "tests/integration/saml/interop_test.go" + "tests/integration/saml/interop_test.go", + "saml/protocol/encryption_test.go", + "saml/protocol/legacy_test.go", + "saml/sp/encryption_test.go", + "tests/interop/saml_encryption_test.go" ], "title": "Solicited SAML WebSSO" }, @@ -10357,6 +10476,33 @@ "tests/interop/profiles_test.go" ], "limitations": "Full EN processing, evidence records, historical countersignature/attribute-authority and wider preservation workflows, remaining current-process indication branches and complete policy-equivalent independent agreement remain open. Explicit TSA accuracy assumption is needed when absent; acquisition/signing times never mint PoE." + }, + { + "id": "SAML-QUERY-MAPPING", + "title": "SAML Authority queries, assertion retrieval and NameID Mapping", + "status": "implemented-restricted", + "implementation": [ + "saml/authority", + "saml/protocol", + "saml/binding", + "saml/metadata" + ], + "standards": [ + "SAML2 Core 3.3 and 3.8", + "SAML2 Profiles 6 and 7", + "SAML2 Bindings 3.2 and 3.7", + "SAML2 Conformance 3.2 Tables 3-4" + ], + "tests": [ + "saml/protocol/query_test.go", + "saml/protocol/mapping_test.go", + "saml/authority/authority_test.go", + "saml/binding/uri_test.go", + "saml/metadata/authority_test.go", + "tests/interop/saml_authority_test.go", + "tests/interop/saml_authority_http_test.go" + ], + "limitations": "Existing XML/algorithm/resource policies apply. Arbitrary BaseID schemas and automatic proxying are not selected. Application backends authorize disclosure and identity binding. Deployed Shibboleth 5.2.3 covers AttributeQuery over HTTPS/SOAP; other message families retain independent OpenSAML component tests. Unrestricted SAML role conformance is not claimed." } ], "normative_requirements": [ @@ -10381,13 +10527,15 @@ "status": "limited", "apis": [ "transform.Engine.CompileXPath", - "transform.Engine.CompileXSLT" + "transform.Engine.CompileXSLT", + "document.ParseOptions.Parse" ], "code": [ "internal/tree/dtd.go", "internal/tree/dtd_content.go", "internal/tree/dtd_subset.go", - "transform/internal/xdm/parse.go" + "transform/internal/xdm/parse.go", + "document/parse_options.go" ] }, "tests": { @@ -10401,6 +10549,14 @@ { "file": "transform/resource_test.go", "name": "TestLanguageDTDExternalEntities" + }, + { + "file": "document/parse_options_test.go", + "name": "TestNonValidatingXMLInternalSubset" + }, + { + "file": "document/parse_options_test.go", + "name": "TestNonValidatingXMLExternalEntities" } ], "negative": [ @@ -10411,6 +10567,14 @@ { "file": "transform/resource_test.go", "name": "TestLanguageDTDFailures" + }, + { + "file": "document/parse_options_test.go", + "name": "TestNonValidatingXMLWellFormedness" + }, + { + "file": "document/parse_options_test.go", + "name": "TestNonValidatingXMLFailureContracts" } ] }, @@ -10436,11 +10600,11 @@ }, "compatibility": { "status": "opt-in", - "reason": "XPath/XSLT language XML input uses bounded nonvalidating DTD processing with an explicit resolver for external entities. Engine.Parse, document.Parse, record input and approved XMLDSig programs remain DTD-free." + "reason": "document.ParseOptions.NonValidating and XPath/XSLT input enable bounded DTD processing with explicit external resolvers. The security parser, records and approved XMLDSig programs remain DTD-free." } }, - "gap": "The separate XPath/XSLT input mode implements bounded declaration/entity/default normalization for the mapped controls. A full XML processor API, official XML suite coverage and complete independent evidence for this clause remain open; no full XML conformance is claimed.", - "closure": "Complete this clause and its independent positive/negative XML fixtures in the general processor direction; retain the DTD-free security input profile and explicit resolver/budgets." + "gap": "Public bounded nonvalidating XML processing is available. A separate unchanged W3C XML TS 20130923 run checks selected XML 1.0/Namespaces 1.0 binary outcomes; full output/encoding/notation clause-specific certification remains unverified.", + "closure": "Retain explicit resource authorization and complete independent output witnesses before claiming full XML processor conformance." }, { "id": "N-NS-DEFAULT", @@ -12275,34 +12439,57 @@ "reason": "Applies to the selected public operation within the bounded profile." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "xmlenc.EncryptBytes", + "xmlenc.DecryptBytes", + "xmlenc.EncryptElement", + "xmlenc.DecryptElement" + ], + "code": [ + "xmlenc/provider.go", + "xmlenc/xmlenc.go" + ] }, "tests": { - "status": "pending", - "reason": "No finished positive/negative implementation evidence exists for this exact feature.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Explicit selection, legacy permission, standard-library transport decoding, implicit session-key provider options, corrupted padding and secure-default rejection are covered.", + "positive": [ + { + "file": "xmlenc/rsa15_test.go", + "name": "TestRSA15ExplicitPolicyAndSessionKey" + } + ], + "negative": [ + { + "file": "xmlenc/rsa15_test.go", + "name": "TestRSA15ExplicitPolicyAndSessionKey" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "No independent executable fixture for this exact operation is currently mapped; no broad family run is treated as agreement." + "reason": "Finite native RSA15/AES128-CBC SAML exchanges run in both directions. Complete independent per-clause malformed-input coverage is not claimed." }, "policy": { "default": { - "status": "unavailable", - "reason": "This precise operation is not implemented; other algorithm flags do not enable it." + "status": "forbidden", + "reason": "RSA-OAEP is the default; RSA15 is never inferred from XML." }, "compatibility": { - "status": "unavailable", - "reason": "No compatibility option supplies absent algorithm or syntax behavior." + "status": "opt-in", + "reason": "Options.KeyTransportAlgorithm=RSA15 and AllowLegacy are both required. OAEP parameters and FIPS mode reject it. Applications authenticate ciphertext first; native RSA providers receive SessionKeyLen for implicit rejection." } }, - "gap": "Optional RSAES-PKCS1-v1_5 key transport is deliberately not implemented; CBC opt-in does not enable it.", - "closure": "Implement the stated feature with exact published syntax, parameter, positive/negative, resource and independent controls if this optional target is selected; retain its applicable inventory entry rather than declaring absence non-applicable." + "gap": "Available only in the explicit RSA entry points, not key graphs. Providers must honor the documented implicit-rejection contract; timing-oracle resistance is not certified.", + "closure": "Complete independent clause witnesses and provider-side oracle analysis without enabling RSA15 by default." }, { "id": "N-FO20-SUBSEQUENCE", @@ -14466,34 +14653,69 @@ "reason": "Applies to the selected public operation within the bounded profile." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", + "saml/sp.NewServiceProvider", + "saml/idp.NewIdentityProvider" + ], + "code": [ + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "No obligation-specific positive/negative tests are currently mapped. The implementation/input and evidence gap remains explicit.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "All seven encryption target combinations, required-encryption downgrade rejection, authenticated failure order, key rotation/budgets, optional Type and sibling keys are tested within solicited WebSSO.", + "positive": [ + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedResponseTargets" + }, + { + "file": "saml/sp/encryption_test.go", + "name": "TestEncryptedWebSSO" + } + ], + "negative": [ + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedResponsePoliciesAndFailureOrder" + }, + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedCBCAndUnsignedAssertion" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Existing internal tests are not treated as independent clause-level conformance evidence." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { "status": "forbidden", - "reason": "The default security profile rejects this legacy or unsupported capability." + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "unavailable", - "reason": "No separate compatibility policy implements this requirement." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "EncryptedID, EncryptedAssertion and EncryptedAttribute support is missing for applicable plaintext contexts.", - "closure": "Implement each applicable encrypted counterpart over an independently checked XMLenc profile; test full exchanges, issuer/recipient/audience/time/signature coverage and failure cases." + "gap": "Implemented for the current NameID/string-attribute solicited WebSSO profile using explicitly selected RSA transports, inline/sibling EncryptedKey alternatives and no external key/cipher I/O. General SAML extensions, other key-management profiles and complete independent malformed-input coverage are not claimed.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-SP-ALGORITHMS", @@ -40320,7 +40542,6 @@ "format-date-008c", "format-date-009", "format-date-010", - "format-date-011", "format-date-013L", "format-date-013a", "format-date-013b", @@ -40590,6 +40811,28 @@ }, "oracle": "saxon-12.10", "source": "https://www.w3.org/TR/2014/REC-xpath-functions-30-20140408/#func-format-date" + }, + { + "check": { + "file": "tests/cmd/dev/run_test.go", + "name": "TestISOCalendarMonthWeekPrimaryExamples", + "case": "XPath3.0/January29" + }, + "oracle": "published-vector", + "source": "https://www.w3.org/TR/2014/REC-xpath-functions-30-20140408/#lang-cal-place", + "expected": "5", + "basis": "F&O section 9.8.4 explicitly gives week-in-month 5 for 2013-01-29 and 2013-02-01: a Monday-Sunday week belongs to the month containing its Thursday. This check executes that published example; the derived 2006 boundary controls are separate." + }, + { + "check": { + "file": "tests/cmd/dev/run_test.go", + "name": "TestISOCalendarMonthWeekPrimaryExamples", + "case": "XPath3.0/February1" + }, + "oracle": "published-vector", + "source": "https://www.w3.org/TR/2014/REC-xpath-functions-30-20140408/#lang-cal-place", + "expected": "5", + "basis": "F&O section 9.8.4 explicitly gives week-in-month 5 for 2013-01-29 and 2013-02-01: a Monday-Sunday week belongs to the month containing its Thursday. This check executes that published example; the derived 2006 boundary controls are separate." } ], "negative": [ @@ -45129,7 +45372,6 @@ "format-date-008c", "format-date-009", "format-date-010", - "format-date-011", "format-date-013L", "format-date-013a", "format-date-013b", @@ -45418,6 +45660,28 @@ }, "oracle": "saxon-12.10", "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#func-format-date" + }, + { + "check": { + "file": "tests/cmd/dev/run_test.go", + "name": "TestISOCalendarMonthWeekPrimaryExamples", + "case": "XPath3.1/January29" + }, + "oracle": "published-vector", + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#lang-cal-place", + "expected": "5", + "basis": "F&O section 9.8.4 explicitly gives week-in-month 5 for 2013-01-29 and 2013-02-01: a Monday-Sunday week belongs to the month containing its Thursday. This check executes that published example; the derived 2006 boundary controls are separate." + }, + { + "check": { + "file": "tests/cmd/dev/run_test.go", + "name": "TestISOCalendarMonthWeekPrimaryExamples", + "case": "XPath3.1/February1" + }, + "oracle": "published-vector", + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#lang-cal-place", + "expected": "5", + "basis": "F&O section 9.8.4 explicitly gives week-in-month 5 for 2013-01-29 and 2013-02-01: a Monday-Sunday week belongs to the month containing its Thursday. This check executes that published example; the derived 2006 boundary controls are separate." } ], "negative": [ @@ -46161,13 +46425,15 @@ "apis": [ "transform.Config.AllowSingleByteEncodings", "transform.Engine.ParseReader", - "transform.Engine.CompileXSLTReader" + "transform.Engine.CompileXSLTReader", + "document.ParseOptions.Parse" ], "code": [ "internal/tree/read.go", "internal/tree/parse.go", "transform/internal/xdm/parse.go", - "transform/engine.go" + "transform/engine.go", + "document/parse_options.go" ] }, "tests": { @@ -46185,6 +46451,14 @@ { "file": "document/encoding_test.go", "name": "TestEncodingRejects" + }, + { + "file": "document/parse_options_test.go", + "name": "TestNonValidatingXMLInternalSubset" + }, + { + "file": "document/parse_options_test.go", + "name": "TestNonValidatingXMLExternalEntities" } ], "negative": [ @@ -46199,6 +46473,14 @@ { "file": "document/encoding_test.go", "name": "TestEncodingRejects" + }, + { + "file": "document/parse_options_test.go", + "name": "TestNonValidatingXMLWellFormedness" + }, + { + "file": "document/parse_options_test.go", + "name": "TestNonValidatingXMLFailureContracts" } ] }, @@ -46220,11 +46502,11 @@ }, "compatibility": { "status": "opt-in", - "reason": "transform.Config.AllowSingleByteEncodings explicitly permits declared ASCII-compatible IANA single-byte maps. BOM conflicts, undefined mappings, invalid XML scalars and input expansion limits remain errors." + "reason": "document.ParseOptions.NonValidating and XPath/XSLT input enable bounded DTD processing with explicit external resolvers. The security parser, records and approved XMLDSig programs remain DTD-free." } }, - "gap": "The explicit transform option supports ASCII and ASCII-compatible IANA single-byte charmaps; multibyte legacy encodings and general DTD/entity processing are not implemented. Complete independent byte-map evidence remains open.", - "closure": "Complete independent positive/negative fixtures for accepted mappings and keep optional decoding separate from the default XML security profile and general XML processor obligations." + "gap": "Public bounded nonvalidating XML processing is available. A separate unchanged W3C XML TS 20130923 run checks selected XML 1.0/Namespaces 1.0 binary outcomes; full output/encoding/notation clause-specific certification remains unverified.", + "closure": "Retain explicit resource authorization and complete independent output witnesses before claiming full XML processor conformance." }, { "id": "N-XML-GRAMMAR-document", @@ -54821,13 +55103,15 @@ "status": "limited", "apis": [ "transform.Engine.CompileXPath", - "transform.Engine.CompileXSLT" + "transform.Engine.CompileXSLT", + "document.ParseOptions.Parse" ], "code": [ "internal/tree/dtd.go", "internal/tree/dtd_content.go", "internal/tree/dtd_subset.go", - "transform/internal/xdm/parse.go" + "transform/internal/xdm/parse.go", + "document/parse_options.go" ] }, "tests": { @@ -54841,6 +55125,14 @@ { "file": "transform/resource_test.go", "name": "TestLanguageDTDExternalEntities" + }, + { + "file": "document/parse_options_test.go", + "name": "TestNonValidatingXMLInternalSubset" + }, + { + "file": "document/parse_options_test.go", + "name": "TestNonValidatingXMLExternalEntities" } ], "negative": [ @@ -54851,6 +55143,14 @@ { "file": "transform/resource_test.go", "name": "TestLanguageDTDFailures" + }, + { + "file": "document/parse_options_test.go", + "name": "TestNonValidatingXMLWellFormedness" + }, + { + "file": "document/parse_options_test.go", + "name": "TestNonValidatingXMLFailureContracts" } ] }, @@ -54876,11 +55176,11 @@ }, "compatibility": { "status": "opt-in", - "reason": "XPath/XSLT language XML input uses bounded nonvalidating DTD processing with an explicit resolver for external entities. Engine.Parse, document.Parse, record input and approved XMLDSig programs remain DTD-free." + "reason": "document.ParseOptions.NonValidating and XPath/XSLT input enable bounded DTD processing with explicit external resolvers. The security parser, records and approved XMLDSig programs remain DTD-free." } }, - "gap": "The separate XPath/XSLT input mode implements bounded declaration/entity/default normalization for the mapped controls. A full XML processor API, official XML suite coverage and complete independent evidence for this clause remain open; no full XML conformance is claimed.", - "closure": "Complete this clause and its independent positive/negative XML fixtures in the general processor direction; retain the DTD-free security input profile and explicit resolver/budgets." + "gap": "Public bounded nonvalidating XML processing is available. A separate unchanged W3C XML TS 20130923 run checks selected XML 1.0/Namespaces 1.0 binary outcomes; full output/encoding/notation clause-specific certification remains unverified.", + "closure": "Retain explicit resource authorization and complete independent output witnesses before claiming full XML processor conformance." }, { "id": "N-XML-DTD-PRECEDENCE", @@ -54903,13 +55203,15 @@ "status": "limited", "apis": [ "transform.Engine.CompileXPath", - "transform.Engine.CompileXSLT" + "transform.Engine.CompileXSLT", + "document.ParseOptions.Parse" ], "code": [ "internal/tree/dtd.go", "internal/tree/dtd_content.go", "internal/tree/dtd_subset.go", - "transform/internal/xdm/parse.go" + "transform/internal/xdm/parse.go", + "document/parse_options.go" ] }, "tests": { @@ -54923,6 +55225,14 @@ { "file": "transform/resource_test.go", "name": "TestLanguageDTDExternalEntities" + }, + { + "file": "document/parse_options_test.go", + "name": "TestNonValidatingXMLInternalSubset" + }, + { + "file": "document/parse_options_test.go", + "name": "TestNonValidatingXMLExternalEntities" } ], "negative": [ @@ -54933,6 +55243,14 @@ { "file": "transform/resource_test.go", "name": "TestLanguageDTDFailures" + }, + { + "file": "document/parse_options_test.go", + "name": "TestNonValidatingXMLWellFormedness" + }, + { + "file": "document/parse_options_test.go", + "name": "TestNonValidatingXMLFailureContracts" } ] }, @@ -54958,11 +55276,11 @@ }, "compatibility": { "status": "opt-in", - "reason": "XPath/XSLT language XML input uses bounded nonvalidating DTD processing with an explicit resolver for external entities. Engine.Parse, document.Parse, record input and approved XMLDSig programs remain DTD-free." + "reason": "document.ParseOptions.NonValidating and XPath/XSLT input enable bounded DTD processing with explicit external resolvers. The security parser, records and approved XMLDSig programs remain DTD-free." } }, - "gap": "The separate XPath/XSLT input mode implements bounded declaration/entity/default normalization for the mapped controls. A full XML processor API, official XML suite coverage and complete independent evidence for this clause remain open; no full XML conformance is claimed.", - "closure": "Complete this clause and its independent positive/negative XML fixtures in the general processor direction; retain the DTD-free security input profile and explicit resolver/budgets." + "gap": "Public bounded nonvalidating XML processing is available. A separate unchanged W3C XML TS 20130923 run checks selected XML 1.0/Namespaces 1.0 binary outcomes; full output/encoding/notation clause-specific certification remains unverified.", + "closure": "Retain explicit resource authorization and complete independent output witnesses before claiming full XML processor conformance." }, { "id": "N-XML-VERSION-NUMBER", @@ -101415,29 +101733,31 @@ "reason": "Applies to the selected standard role and operation. Missing mandatory behavior stays applicable; conditions on optional formats/algorithms do not turn required selected-role duties into policy exemptions." }, "implementation": { - "status": "limited", + "status": "implemented", "apis": [ "keys.ParsePrivate", - "keys.ParsePublic" + "keys.ParsePublic", + "keys.ParseCertificate", + "keys.ParseCRL", + "keys.ParseEncryptedPrivate" ], "code": [ - "keys/parse.go", - "keys/private_structure.go" + "keys/parse.go" ] }, "tests": { - "status": "partial", - "reason": "The cited local checks cover only their stated subset; additional obligation-specific cases remain in gap/closure.", + "status": "covered", + "reason": "Exact local cases cover CRLF, CR, LF and mixed line endings for private/public keys, certificates, CRLs and encrypted PKCS#8. Malformed framing, mismatched labels, multiple objects and decoded-byte limits remain rejected.", "positive": [ { "file": "keys/parse_test.go", - "name": "TestPrivateAndPublicFormats" + "name": "TestPEMNewlineConventions" } ], "negative": [ { "file": "keys/parse_test.go", - "name": "TestRejectAmbiguousAndOversizedMaterial" + "name": "TestPEMFramingAcrossNewlines" } ] }, @@ -101457,8 +101777,8 @@ "reason": "No general compatibility switch supplies missing normative behavior." } }, - "gap": "The selected parser accepts bounded PEM for supported DER key/certificate/CRL objects and rejects ambiguous material. Lax non-Base64/preamble variants, general BER, newer OneAsymmetricKey fields and complete per-label evidence remain open.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Complete independent newline-specific evidence remains pending. Other PEM framing and format obligations are recorded separately.", + "closure": "Add independent CRLF/CR/LF fixtures or exchanges without relaxing the supported DER object, exact label, ambiguity or resource policies." }, { "id": "N-KEYS-7468-BASE64", @@ -374928,7 +375248,6 @@ "format-date-008c", "format-date-009", "format-date-010", - "format-date-011", "format-date-013L", "format-date-013a", "format-date-013b", @@ -375012,6 +375331,28 @@ "format-date-en153" ] } + }, + { + "check": { + "file": "tests/cmd/dev/run_test.go", + "name": "TestISOCalendarMonthWeekPrimaryExamples", + "case": "XPath3.0/January29" + }, + "oracle": "published-vector", + "source": "https://www.w3.org/TR/2014/REC-xpath-functions-30-20140408/#lang-cal-place", + "expected": "5", + "basis": "F&O section 9.8.4 explicitly gives week-in-month 5 for 2013-01-29 and 2013-02-01: a Monday-Sunday week belongs to the month containing its Thursday. This check executes that published example; the derived 2006 boundary controls are separate." + }, + { + "check": { + "file": "tests/cmd/dev/run_test.go", + "name": "TestISOCalendarMonthWeekPrimaryExamples", + "case": "XPath3.0/February1" + }, + "oracle": "published-vector", + "source": "https://www.w3.org/TR/2014/REC-xpath-functions-30-20140408/#lang-cal-place", + "expected": "5", + "basis": "F&O section 9.8.4 explicitly gives week-in-month 5 for 2013-01-29 and 2013-02-01: a Monday-Sunday week belongs to the month containing its Thursday. This check executes that published example; the derived 2006 boundary controls are separate." } ], "negative": [ @@ -375521,7 +375862,6 @@ "format-date-008c", "format-date-009", "format-date-010", - "format-date-011", "format-date-013L", "format-date-013a", "format-date-013b", @@ -375755,6 +376095,28 @@ "millisecs-026" ] } + }, + { + "check": { + "file": "tests/cmd/dev/run_test.go", + "name": "TestISOCalendarMonthWeekPrimaryExamples", + "case": "XPath3.0/January29" + }, + "oracle": "published-vector", + "source": "https://www.w3.org/TR/2014/REC-xpath-functions-30-20140408/#lang-cal-place", + "expected": "5", + "basis": "F&O section 9.8.4 explicitly gives week-in-month 5 for 2013-01-29 and 2013-02-01: a Monday-Sunday week belongs to the month containing its Thursday. This check executes that published example; the derived 2006 boundary controls are separate." + }, + { + "check": { + "file": "tests/cmd/dev/run_test.go", + "name": "TestISOCalendarMonthWeekPrimaryExamples", + "case": "XPath3.0/February1" + }, + "oracle": "published-vector", + "source": "https://www.w3.org/TR/2014/REC-xpath-functions-30-20140408/#lang-cal-place", + "expected": "5", + "basis": "F&O section 9.8.4 explicitly gives week-in-month 5 for 2013-01-29 and 2013-02-01: a Monday-Sunday week belongs to the month containing its Thursday. This check executes that published example; the derived 2006 boundary controls are separate." } ], "negative": [ @@ -375969,7 +376331,6 @@ "format-date-008c", "format-date-009", "format-date-010", - "format-date-011", "format-date-013L", "format-date-013a", "format-date-013b", @@ -376203,6 +376564,28 @@ "millisecs-026" ] } + }, + { + "check": { + "file": "tests/cmd/dev/run_test.go", + "name": "TestISOCalendarMonthWeekPrimaryExamples", + "case": "XPath3.0/January29" + }, + "oracle": "published-vector", + "source": "https://www.w3.org/TR/2014/REC-xpath-functions-30-20140408/#lang-cal-place", + "expected": "5", + "basis": "F&O section 9.8.4 explicitly gives week-in-month 5 for 2013-01-29 and 2013-02-01: a Monday-Sunday week belongs to the month containing its Thursday. This check executes that published example; the derived 2006 boundary controls are separate." + }, + { + "check": { + "file": "tests/cmd/dev/run_test.go", + "name": "TestISOCalendarMonthWeekPrimaryExamples", + "case": "XPath3.0/February1" + }, + "oracle": "published-vector", + "source": "https://www.w3.org/TR/2014/REC-xpath-functions-30-20140408/#lang-cal-place", + "expected": "5", + "basis": "F&O section 9.8.4 explicitly gives week-in-month 5 for 2013-01-29 and 2013-02-01: a Monday-Sunday week belongs to the month containing its Thursday. This check executes that published example; the derived 2006 boundary controls are separate." } ], "negative": [ @@ -376417,7 +376800,6 @@ "format-date-008c", "format-date-009", "format-date-010", - "format-date-011", "format-date-013L", "format-date-013a", "format-date-013b", @@ -376651,6 +377033,28 @@ "millisecs-026" ] } + }, + { + "check": { + "file": "tests/cmd/dev/run_test.go", + "name": "TestISOCalendarMonthWeekPrimaryExamples", + "case": "XPath3.0/January29" + }, + "oracle": "published-vector", + "source": "https://www.w3.org/TR/2014/REC-xpath-functions-30-20140408/#lang-cal-place", + "expected": "5", + "basis": "F&O section 9.8.4 explicitly gives week-in-month 5 for 2013-01-29 and 2013-02-01: a Monday-Sunday week belongs to the month containing its Thursday. This check executes that published example; the derived 2006 boundary controls are separate." + }, + { + "check": { + "file": "tests/cmd/dev/run_test.go", + "name": "TestISOCalendarMonthWeekPrimaryExamples", + "case": "XPath3.0/February1" + }, + "oracle": "published-vector", + "source": "https://www.w3.org/TR/2014/REC-xpath-functions-30-20140408/#lang-cal-place", + "expected": "5", + "basis": "F&O section 9.8.4 explicitly gives week-in-month 5 for 2013-01-29 and 2013-02-01: a Monday-Sunday week belongs to the month containing its Thursday. This check executes that published example; the derived 2006 boundary controls are separate." } ], "negative": [ @@ -471735,7 +472139,6 @@ "format-date-008c", "format-date-009", "format-date-010", - "format-date-011", "format-date-013L", "format-date-013a", "format-date-013b", @@ -471819,6 +472222,28 @@ "format-date-en153" ] } + }, + { + "check": { + "file": "tests/cmd/dev/run_test.go", + "name": "TestISOCalendarMonthWeekPrimaryExamples", + "case": "XPath3.1/January29" + }, + "oracle": "published-vector", + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#lang-cal-place", + "expected": "5", + "basis": "F&O section 9.8.4 explicitly gives week-in-month 5 for 2013-01-29 and 2013-02-01: a Monday-Sunday week belongs to the month containing its Thursday. This check executes that published example; the derived 2006 boundary controls are separate." + }, + { + "check": { + "file": "tests/cmd/dev/run_test.go", + "name": "TestISOCalendarMonthWeekPrimaryExamples", + "case": "XPath3.1/February1" + }, + "oracle": "published-vector", + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#lang-cal-place", + "expected": "5", + "basis": "F&O section 9.8.4 explicitly gives week-in-month 5 for 2013-01-29 and 2013-02-01: a Monday-Sunday week belongs to the month containing its Thursday. This check executes that published example; the derived 2006 boundary controls are separate." } ], "negative": [ @@ -472344,7 +472769,6 @@ "format-date-008c", "format-date-009", "format-date-010", - "format-date-011", "format-date-013L", "format-date-013a", "format-date-013b", @@ -472597,6 +473021,28 @@ "millisecs-026" ] } + }, + { + "check": { + "file": "tests/cmd/dev/run_test.go", + "name": "TestISOCalendarMonthWeekPrimaryExamples", + "case": "XPath3.1/January29" + }, + "oracle": "published-vector", + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#lang-cal-place", + "expected": "5", + "basis": "F&O section 9.8.4 explicitly gives week-in-month 5 for 2013-01-29 and 2013-02-01: a Monday-Sunday week belongs to the month containing its Thursday. This check executes that published example; the derived 2006 boundary controls are separate." + }, + { + "check": { + "file": "tests/cmd/dev/run_test.go", + "name": "TestISOCalendarMonthWeekPrimaryExamples", + "case": "XPath3.1/February1" + }, + "oracle": "published-vector", + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#lang-cal-place", + "expected": "5", + "basis": "F&O section 9.8.4 explicitly gives week-in-month 5 for 2013-01-29 and 2013-02-01: a Monday-Sunday week belongs to the month containing its Thursday. This check executes that published example; the derived 2006 boundary controls are separate." } ], "negative": [ @@ -472811,7 +473257,6 @@ "format-date-008c", "format-date-009", "format-date-010", - "format-date-011", "format-date-013L", "format-date-013a", "format-date-013b", @@ -473064,6 +473509,28 @@ "millisecs-026" ] } + }, + { + "check": { + "file": "tests/cmd/dev/run_test.go", + "name": "TestISOCalendarMonthWeekPrimaryExamples", + "case": "XPath3.1/January29" + }, + "oracle": "published-vector", + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#lang-cal-place", + "expected": "5", + "basis": "F&O section 9.8.4 explicitly gives week-in-month 5 for 2013-01-29 and 2013-02-01: a Monday-Sunday week belongs to the month containing its Thursday. This check executes that published example; the derived 2006 boundary controls are separate." + }, + { + "check": { + "file": "tests/cmd/dev/run_test.go", + "name": "TestISOCalendarMonthWeekPrimaryExamples", + "case": "XPath3.1/February1" + }, + "oracle": "published-vector", + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#lang-cal-place", + "expected": "5", + "basis": "F&O section 9.8.4 explicitly gives week-in-month 5 for 2013-01-29 and 2013-02-01: a Monday-Sunday week belongs to the month containing its Thursday. This check executes that published example; the derived 2006 boundary controls are separate." } ], "negative": [ @@ -473278,7 +473745,6 @@ "format-date-008c", "format-date-009", "format-date-010", - "format-date-011", "format-date-013L", "format-date-013a", "format-date-013b", @@ -473531,6 +473997,28 @@ "millisecs-026" ] } + }, + { + "check": { + "file": "tests/cmd/dev/run_test.go", + "name": "TestISOCalendarMonthWeekPrimaryExamples", + "case": "XPath3.1/January29" + }, + "oracle": "published-vector", + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#lang-cal-place", + "expected": "5", + "basis": "F&O section 9.8.4 explicitly gives week-in-month 5 for 2013-01-29 and 2013-02-01: a Monday-Sunday week belongs to the month containing its Thursday. This check executes that published example; the derived 2006 boundary controls are separate." + }, + { + "check": { + "file": "tests/cmd/dev/run_test.go", + "name": "TestISOCalendarMonthWeekPrimaryExamples", + "case": "XPath3.1/February1" + }, + "oracle": "published-vector", + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#lang-cal-place", + "expected": "5", + "basis": "F&O section 9.8.4 explicitly gives week-in-month 5 for 2013-01-29 and 2013-02-01: a Monday-Sunday week belongs to the month containing its Thursday. This check executes that published example; the derived 2006 boundary controls are separate." } ], "negative": [ @@ -473745,7 +474233,6 @@ "format-date-008c", "format-date-009", "format-date-010", - "format-date-011", "format-date-013L", "format-date-013a", "format-date-013b", @@ -473998,6 +474485,28 @@ "millisecs-026" ] } + }, + { + "check": { + "file": "tests/cmd/dev/run_test.go", + "name": "TestISOCalendarMonthWeekPrimaryExamples", + "case": "XPath3.1/January29" + }, + "oracle": "published-vector", + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#lang-cal-place", + "expected": "5", + "basis": "F&O section 9.8.4 explicitly gives week-in-month 5 for 2013-01-29 and 2013-02-01: a Monday-Sunday week belongs to the month containing its Thursday. This check executes that published example; the derived 2006 boundary controls are separate." + }, + { + "check": { + "file": "tests/cmd/dev/run_test.go", + "name": "TestISOCalendarMonthWeekPrimaryExamples", + "case": "XPath3.1/February1" + }, + "oracle": "published-vector", + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#lang-cal-place", + "expected": "5", + "basis": "F&O section 9.8.4 explicitly gives week-in-month 5 for 2013-01-29 and 2013-02-01: a Monday-Sunday week belongs to the month containing its Thursday. This check executes that published example; the derived 2006 boundary controls are separate." } ], "negative": [ @@ -474212,7 +474721,6 @@ "format-date-008c", "format-date-009", "format-date-010", - "format-date-011", "format-date-013L", "format-date-013a", "format-date-013b", @@ -474465,6 +474973,28 @@ "millisecs-026" ] } + }, + { + "check": { + "file": "tests/cmd/dev/run_test.go", + "name": "TestISOCalendarMonthWeekPrimaryExamples", + "case": "XPath3.1/January29" + }, + "oracle": "published-vector", + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#lang-cal-place", + "expected": "5", + "basis": "F&O section 9.8.4 explicitly gives week-in-month 5 for 2013-01-29 and 2013-02-01: a Monday-Sunday week belongs to the month containing its Thursday. This check executes that published example; the derived 2006 boundary controls are separate." + }, + { + "check": { + "file": "tests/cmd/dev/run_test.go", + "name": "TestISOCalendarMonthWeekPrimaryExamples", + "case": "XPath3.1/February1" + }, + "oracle": "published-vector", + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#lang-cal-place", + "expected": "5", + "basis": "F&O section 9.8.4 explicitly gives week-in-month 5 for 2013-01-29 and 2013-02-01: a Monday-Sunday week belongs to the month containing its Thursday. This check executes that published example; the derived 2006 boundary controls are separate." } ], "negative": [ @@ -474679,7 +475209,6 @@ "format-date-008c", "format-date-009", "format-date-010", - "format-date-011", "format-date-013L", "format-date-013a", "format-date-013b", @@ -474932,6 +475461,28 @@ "millisecs-026" ] } + }, + { + "check": { + "file": "tests/cmd/dev/run_test.go", + "name": "TestISOCalendarMonthWeekPrimaryExamples", + "case": "XPath3.1/January29" + }, + "oracle": "published-vector", + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#lang-cal-place", + "expected": "5", + "basis": "F&O section 9.8.4 explicitly gives week-in-month 5 for 2013-01-29 and 2013-02-01: a Monday-Sunday week belongs to the month containing its Thursday. This check executes that published example; the derived 2006 boundary controls are separate." + }, + { + "check": { + "file": "tests/cmd/dev/run_test.go", + "name": "TestISOCalendarMonthWeekPrimaryExamples", + "case": "XPath3.1/February1" + }, + "oracle": "published-vector", + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#lang-cal-place", + "expected": "5", + "basis": "F&O section 9.8.4 explicitly gives week-in-month 5 for 2013-01-29 and 2013-02-01: a Monday-Sunday week belongs to the month containing its Thursday. This check executes that published example; the derived 2006 boundary controls are separate." } ], "negative": [ @@ -475146,7 +475697,6 @@ "format-date-008c", "format-date-009", "format-date-010", - "format-date-011", "format-date-013L", "format-date-013a", "format-date-013b", @@ -475399,6 +475949,28 @@ "millisecs-026" ] } + }, + { + "check": { + "file": "tests/cmd/dev/run_test.go", + "name": "TestISOCalendarMonthWeekPrimaryExamples", + "case": "XPath3.1/January29" + }, + "oracle": "published-vector", + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#lang-cal-place", + "expected": "5", + "basis": "F&O section 9.8.4 explicitly gives week-in-month 5 for 2013-01-29 and 2013-02-01: a Monday-Sunday week belongs to the month containing its Thursday. This check executes that published example; the derived 2006 boundary controls are separate." + }, + { + "check": { + "file": "tests/cmd/dev/run_test.go", + "name": "TestISOCalendarMonthWeekPrimaryExamples", + "case": "XPath3.1/February1" + }, + "oracle": "published-vector", + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#lang-cal-place", + "expected": "5", + "basis": "F&O section 9.8.4 explicitly gives week-in-month 5 for 2013-01-29 and 2013-02-01: a Monday-Sunday week belongs to the month containing its Thursday. This check executes that published example; the derived 2006 boundary controls are separate." } ], "negative": [ @@ -475613,7 +476185,6 @@ "format-date-008c", "format-date-009", "format-date-010", - "format-date-011", "format-date-013L", "format-date-013a", "format-date-013b", @@ -475866,6 +476437,28 @@ "millisecs-026" ] } + }, + { + "check": { + "file": "tests/cmd/dev/run_test.go", + "name": "TestISOCalendarMonthWeekPrimaryExamples", + "case": "XPath3.1/January29" + }, + "oracle": "published-vector", + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#lang-cal-place", + "expected": "5", + "basis": "F&O section 9.8.4 explicitly gives week-in-month 5 for 2013-01-29 and 2013-02-01: a Monday-Sunday week belongs to the month containing its Thursday. This check executes that published example; the derived 2006 boundary controls are separate." + }, + { + "check": { + "file": "tests/cmd/dev/run_test.go", + "name": "TestISOCalendarMonthWeekPrimaryExamples", + "case": "XPath3.1/February1" + }, + "oracle": "published-vector", + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#lang-cal-place", + "expected": "5", + "basis": "F&O section 9.8.4 explicitly gives week-in-month 5 for 2013-01-29 and 2013-02-01: a Monday-Sunday week belongs to the month containing its Thursday. This check executes that published example; the derived 2006 boundary controls are separate." } ], "negative": [ @@ -561712,7 +562305,6 @@ "format-date-008", "format-date-009", "format-date-010", - "format-date-011", "format-date-012", "format-date-013", "format-date-014", @@ -561948,7 +562540,6 @@ "format-date-008", "format-date-009", "format-date-010", - "format-date-011", "format-date-012", "format-date-013", "format-date-014", @@ -562162,7 +562753,6 @@ "format-date-008", "format-date-009", "format-date-010", - "format-date-011", "format-date-012", "format-date-013", "format-date-014", @@ -659293,7 +659883,7 @@ ] }, { - "obligation": "Validate the exact published JSON/XML XSD structural properties using xmllint and the pinned REC schema digest: accept the listed number/null/string/map/array representations and foreign attributes, reject forbidden key/escaped attributes, missing map keys, invalid boolean attributes and nonempty null content. The corresponding native xml-to-json outcome must agree; prose-only restrictions are separate.", + "obligation": "For the selected XPath 3.1 function library (XSLT 3.0 section 27.7), validate the F&O 3.1 Appendix C.2 JSON/XML XSD structural properties using xmllint and the exact published REC schema digest: permitted foreign attributes and finite values are accepted; forbidden attributes, keys and null content are rejected. These controls use the F&O 3.1 schema and do not establish equivalence with the older XSLT 3.0 Appendix B.1 schema. Prose-only restrictions remain separate.", "positive": [ { "check": { @@ -659302,7 +659892,7 @@ "case": "published_JSON_schema_document" }, "oracle": "published-schema", - "source": "https://www.w3.org/TR/2017/REC-xslt-30-20170608/#schema-for-json" + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#schema-for-json" }, { "check": { @@ -659311,7 +659901,7 @@ "case": "published_JSON_schema_document_comments" }, "oracle": "published-schema", - "source": "https://www.w3.org/TR/2017/REC-xslt-30-20170608/#schema-for-json" + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#schema-for-json" }, { "check": { @@ -659320,7 +659910,7 @@ "case": "published_JSON_schema_null_comments" }, "oracle": "published-schema", - "source": "https://www.w3.org/TR/2017/REC-xslt-30-20170608/#schema-for-json" + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#schema-for-json" }, { "check": { @@ -659329,7 +659919,7 @@ "case": "published_JSON_schema_foreign_attributes_ignored" }, "oracle": "published-schema", - "source": "https://www.w3.org/TR/2017/REC-xslt-30-20170608/#schema-for-json" + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#schema-for-json" }, { "check": { @@ -659338,7 +659928,7 @@ "case": "published_JSON_schema_array_whitespace" }, "oracle": "published-schema", - "source": "https://www.w3.org/TR/2017/REC-xslt-30-20170608/#schema-for-json" + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#schema-for-json" }, { "check": { @@ -659347,7 +659937,25 @@ "case": "published_JSON_schema_escaped_string" }, "oracle": "published-schema", - "source": "https://www.w3.org/TR/2017/REC-xslt-30-20170608/#schema-for-json" + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#schema-for-json" + }, + { + "check": { + "file": "transform/comparison_v1_test.go", + "name": "TestCompareSaxonJSONCharacterExpansion", + "case": "published_JSON_schema_foreign_boolean_attributes" + }, + "oracle": "published-schema", + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#schema-for-json" + }, + { + "check": { + "file": "transform/comparison_v1_test.go", + "name": "TestCompareSaxonJSONCharacterExpansion", + "case": "published_JSON_schema_foreign_number_attributes" + }, + "oracle": "published-schema", + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#schema-for-json" } ], "negative": [ @@ -659358,7 +659966,7 @@ "case": "published_JSON_schema_array_key" }, "oracle": "published-schema", - "source": "https://www.w3.org/TR/2017/REC-xslt-30-20170608/#schema-for-json" + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#schema-for-json" }, { "check": { @@ -659367,7 +659975,7 @@ "case": "published_JSON_schema_array_escaped_key" }, "oracle": "published-schema", - "source": "https://www.w3.org/TR/2017/REC-xslt-30-20170608/#schema-for-json" + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#schema-for-json" }, { "check": { @@ -659376,7 +659984,7 @@ "case": "published_JSON_schema_map_escaped" }, "oracle": "published-schema", - "source": "https://www.w3.org/TR/2017/REC-xslt-30-20170608/#schema-for-json" + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#schema-for-json" }, { "check": { @@ -659385,7 +659993,7 @@ "case": "published_JSON_schema_number_escaped" }, "oracle": "published-schema", - "source": "https://www.w3.org/TR/2017/REC-xslt-30-20170608/#schema-for-json" + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#schema-for-json" }, { "check": { @@ -659394,7 +660002,7 @@ "case": "published_JSON_schema_null_text" }, "oracle": "published-schema", - "source": "https://www.w3.org/TR/2017/REC-xslt-30-20170608/#schema-for-json" + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#schema-for-json" }, { "check": { @@ -659403,7 +660011,7 @@ "case": "published_JSON_schema_missing_key" }, "oracle": "published-schema", - "source": "https://www.w3.org/TR/2017/REC-xslt-30-20170608/#schema-for-json" + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#schema-for-json" }, { "check": { @@ -659412,7 +660020,25 @@ "case": "published_JSON_schema_invalid_key_boolean" }, "oracle": "published-schema", - "source": "https://www.w3.org/TR/2017/REC-xslt-30-20170608/#schema-for-json" + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#schema-for-json" + }, + { + "check": { + "file": "transform/comparison_v1_test.go", + "name": "TestCompareSaxonJSONCharacterExpansion", + "case": "published_JSON_schema_unqualified_extra_attribute" + }, + "oracle": "published-schema", + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#schema-for-json" + }, + { + "check": { + "file": "transform/comparison_v1_test.go", + "name": "TestCompareSaxonJSONCharacterExpansion", + "case": "published_JSON_schema_function_namespace_attribute" + }, + "oracle": "published-schema", + "source": "https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/#schema-for-json" } ] } @@ -792590,33 +793216,62 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Issue", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Executable positive/negative tests cover the implemented 2005/Approved Errata 05 workflow, exact peer/destination/correlation checks and the named state boundaries. This does not certify the complete SAML role or every extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestAuthenticatedArtifactSOAP" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/binding/artifact_test.go", + "name": "TestArtifactWireAndHTTP" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_control_test.go", + "name": "TestXMLSecSAMLControlSOAP" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Native xmlsec1 verifies and independently regenerates control-message SOAP signatures with Exclusive and Inclusive C14N. Full independent SAML-server/profile exchanges are not established by that component evidence." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs use locally trusted keys/metadata, authenticated messages, bounded parsing and atomic application-owned state. No received URL or XML KeyInfo adds trust." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy signature/encryption algorithms require the existing explicit algorithm policy; compatibility never bypasses peer, correlation or replay checks." } }, - "gap": "HTTP Artifact response delivery/consumption is unimplemented.", + "gap": "Complete independent obligation-level evidence and a deployed external HoK IdP exchange remain open. ECP CS01 2.3.11 XML-proof interpretation is unresolved; see docs/saml.md#ecp-2013-holder-of-key.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -792637,33 +793292,62 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Issue", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Executable positive/negative tests cover the implemented 2005/Approved Errata 05 workflow, exact peer/destination/correlation checks and the named state boundaries. This does not certify the complete SAML role or every extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestAuthenticatedArtifactSOAP" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/binding/artifact_test.go", + "name": "TestArtifactWireAndHTTP" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_control_test.go", + "name": "TestXMLSecSAMLControlSOAP" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Native xmlsec1 verifies and independently regenerates control-message SOAP signatures with Exclusive and Inclusive C14N. Full independent SAML-server/profile exchanges are not established by that component evidence." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs use locally trusted keys/metadata, authenticated messages, bounded parsing and atomic application-owned state. No received URL or XML KeyInfo adds trust." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy signature/encryption algorithms require the existing explicit algorithm policy; compatibility never bypasses peer, correlation or replay checks." } }, - "gap": "ArtifactResolve/ArtifactResponse exchange, authentication, single-use binding and SOAP errors are unimplemented.", + "gap": "Complete independent obligation-level evidence and a deployed external HoK IdP exchange remain open. ECP CS01 2.3.11 XML-proof interpretation is unresolved; see docs/saml.md#ecp-2013-holder-of-key.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -792684,33 +793368,61 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Executable positive/negative tests cover the implemented 2005/Approved Errata 05 workflow, exact peer/destination/correlation checks and the named state boundaries. This does not certify the complete SAML role or every extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_control_test.go", + "name": "TestXMLSecSAMLControlSOAP" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Native xmlsec1 verifies and independently regenerates control-message SOAP signatures with Exclusive and Inclusive C14N. Full independent SAML-server/profile exchanges are not established by that component evidence." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs use locally trusted keys/metadata, authenticated messages, bounded parsing and atomic application-owned state. No received URL or XML KeyInfo adds trust." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy signature/encryption algorithms require the existing explicit algorithm policy; compatibility never bypasses peer, correlation or replay checks." } }, - "gap": "SP/IdP ECP and PAOS message/header/security handling are unimplemented; metadata constants do not implement the binding.", + "gap": "Complete independent obligation-level evidence and a deployed external HoK IdP exchange remain open. ECP CS01 2.3.11 XML-proof interpretation is unresolved; see docs/saml.md#ecp-2013-holder-of-key.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -792733,33 +793445,63 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.BeginManage", + "saml/session.Service.Manage", + "saml/session.Service.Complete" + ], + "code": [ + "saml/session/session.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Executable positive/negative tests cover the implemented 2005/Approved Errata 05 workflow, exact peer/destination/correlation checks and the named state boundaries. This does not certify the complete SAML role or every extension.", + "positive": [ + { + "file": "saml/session/exchange_test.go", + "name": "TestManageNameIDBothDirectionsAndBindings" + }, + { + "file": "saml/session/soap_test.go", + "name": "TestSOAPBackChannelManagementAndLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestManageAliasTerminationAndAtomicReplay" + }, + { + "file": "saml/protocol/control_test.go", + "name": "TestControlIndependentWire" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_control_test.go", + "name": "TestXMLSecSAMLControlSOAP" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Native xmlsec1 verifies and independently regenerates control-message SOAP signatures with Exclusive and Inclusive C14N. Full independent SAML-server/profile exchanges are not established by that component evidence." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs use locally trusted keys/metadata, authenticated messages, bounded parsing and atomic application-owned state. No received URL or XML KeyInfo adds trust." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy signature/encryption algorithms require the existing explicit algorithm policy; compatibility never bypasses peer, correlation or replay checks." } }, - "gap": "ManageNameID request/response, partner-state change and identifier termination are unimplemented.", + "gap": "Complete independent obligation-level evidence and a deployed external HoK IdP exchange remain open. ECP CS01 2.3.11 XML-proof interpretation is unresolved; see docs/saml.md#ecp-2013-holder-of-key.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -792782,33 +793524,63 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.BeginManage", + "saml/session.Service.Manage", + "saml/session.Service.Complete" + ], + "code": [ + "saml/session/session.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Executable positive/negative tests cover the implemented 2005/Approved Errata 05 workflow, exact peer/destination/correlation checks and the named state boundaries. This does not certify the complete SAML role or every extension.", + "positive": [ + { + "file": "saml/session/exchange_test.go", + "name": "TestManageNameIDBothDirectionsAndBindings" + }, + { + "file": "saml/session/soap_test.go", + "name": "TestSOAPBackChannelManagementAndLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestManageAliasTerminationAndAtomicReplay" + }, + { + "file": "saml/protocol/control_test.go", + "name": "TestControlIndependentWire" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_control_test.go", + "name": "TestXMLSecSAMLControlSOAP" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Native xmlsec1 verifies and independently regenerates control-message SOAP signatures with Exclusive and Inclusive C14N. Full independent SAML-server/profile exchanges are not established by that component evidence." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs use locally trusted keys/metadata, authenticated messages, bounded parsing and atomic application-owned state. No received URL or XML KeyInfo adds trust." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy signature/encryption algorithms require the existing explicit algorithm policy; compatibility never bypasses peer, correlation or replay checks." } }, - "gap": "ManageNameID request/response, partner-state change and identifier termination are unimplemented.", + "gap": "Complete independent obligation-level evidence and a deployed external HoK IdP exchange remain open. ECP CS01 2.3.11 XML-proof interpretation is unresolved; see docs/saml.md#ecp-2013-holder-of-key.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -792831,33 +793603,63 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.BeginManage", + "saml/session.Service.Manage", + "saml/session.Service.Complete" + ], + "code": [ + "saml/session/session.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Executable positive/negative tests cover the implemented 2005/Approved Errata 05 workflow, exact peer/destination/correlation checks and the named state boundaries. This does not certify the complete SAML role or every extension.", + "positive": [ + { + "file": "saml/session/exchange_test.go", + "name": "TestManageNameIDBothDirectionsAndBindings" + }, + { + "file": "saml/session/soap_test.go", + "name": "TestSOAPBackChannelManagementAndLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestManageAliasTerminationAndAtomicReplay" + }, + { + "file": "saml/protocol/control_test.go", + "name": "TestControlIndependentWire" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_control_test.go", + "name": "TestXMLSecSAMLControlSOAP" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Native xmlsec1 verifies and independently regenerates control-message SOAP signatures with Exclusive and Inclusive C14N. Full independent SAML-server/profile exchanges are not established by that component evidence." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs use locally trusted keys/metadata, authenticated messages, bounded parsing and atomic application-owned state. No received URL or XML KeyInfo adds trust." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy signature/encryption algorithms require the existing explicit algorithm policy; compatibility never bypasses peer, correlation or replay checks." } }, - "gap": "SOAP Name Identifier Management is unimplemented; it is mandatory for full IdP and optional for ordinary SP.", + "gap": "Complete independent obligation-level evidence and a deployed external HoK IdP exchange remain open. ECP CS01 2.3.11 XML-proof interpretation is unresolved; see docs/saml.md#ecp-2013-holder-of-key.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -792880,33 +793682,63 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.BeginManage", + "saml/session.Service.Manage", + "saml/session.Service.Complete" + ], + "code": [ + "saml/session/session.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Executable positive/negative tests cover the implemented 2005/Approved Errata 05 workflow, exact peer/destination/correlation checks and the named state boundaries. This does not certify the complete SAML role or every extension.", + "positive": [ + { + "file": "saml/session/exchange_test.go", + "name": "TestManageNameIDBothDirectionsAndBindings" + }, + { + "file": "saml/session/soap_test.go", + "name": "TestSOAPBackChannelManagementAndLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestManageAliasTerminationAndAtomicReplay" + }, + { + "file": "saml/protocol/control_test.go", + "name": "TestControlIndependentWire" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_control_test.go", + "name": "TestXMLSecSAMLControlSOAP" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Native xmlsec1 verifies and independently regenerates control-message SOAP signatures with Exclusive and Inclusive C14N. Full independent SAML-server/profile exchanges are not established by that component evidence." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs use locally trusted keys/metadata, authenticated messages, bounded parsing and atomic application-owned state. No received URL or XML KeyInfo adds trust." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy signature/encryption algorithms require the existing explicit algorithm policy; compatibility never bypasses peer, correlation or replay checks." } }, - "gap": "SOAP Name Identifier Management is unimplemented; it is mandatory for full IdP and optional for ordinary SP.", + "gap": "Complete independent obligation-level evidence and a deployed external HoK IdP exchange remain open. ECP CS01 2.3.11 XML-proof interpretation is unresolved; see docs/saml.md#ecp-2013-holder-of-key.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -792927,33 +793759,68 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Executable positive/negative tests cover the implemented 2005/Approved Errata 05 workflow, exact peer/destination/correlation checks and the named state boundaries. This does not certify the complete SAML role or every extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + }, + { + "file": "saml/session/soap_test.go", + "name": "TestSOAPBackChannelManagementAndLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/protocol/control_test.go", + "name": "TestControlMessages" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_control_test.go", + "name": "TestXMLSecSAMLControlSOAP" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Native xmlsec1 verifies and independently regenerates control-message SOAP signatures with Exclusive and Inclusive C14N. Full independent SAML-server/profile exchanges are not established by that component evidence." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs use locally trusted keys/metadata, authenticated messages, bounded parsing and atomic application-owned state. No received URL or XML KeyInfo adds trust." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy signature/encryption algorithms require the existing explicit algorithm policy; compatibility never bypasses peer, correlation or replay checks." } }, - "gap": "Logout messages, session participant/authority correlation and propagation are unimplemented.", + "gap": "Complete independent obligation-level evidence and a deployed external HoK IdP exchange remain open. ECP CS01 2.3.11 XML-proof interpretation is unresolved; see docs/saml.md#ecp-2013-holder-of-key.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -792974,33 +793841,68 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Executable positive/negative tests cover the implemented 2005/Approved Errata 05 workflow, exact peer/destination/correlation checks and the named state boundaries. This does not certify the complete SAML role or every extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + }, + { + "file": "saml/session/soap_test.go", + "name": "TestSOAPBackChannelManagementAndLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/protocol/control_test.go", + "name": "TestControlMessages" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_control_test.go", + "name": "TestXMLSecSAMLControlSOAP" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Native xmlsec1 verifies and independently regenerates control-message SOAP signatures with Exclusive and Inclusive C14N. Full independent SAML-server/profile exchanges are not established by that component evidence." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs use locally trusted keys/metadata, authenticated messages, bounded parsing and atomic application-owned state. No received URL or XML KeyInfo adds trust." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy signature/encryption algorithms require the existing explicit algorithm policy; compatibility never bypasses peer, correlation or replay checks." } }, - "gap": "Logout messages, session participant/authority correlation and propagation are unimplemented.", + "gap": "Complete independent obligation-level evidence and a deployed external HoK IdP exchange remain open. ECP CS01 2.3.11 XML-proof interpretation is unresolved; see docs/saml.md#ecp-2013-holder-of-key.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -793021,33 +793923,68 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Executable positive/negative tests cover the implemented 2005/Approved Errata 05 workflow, exact peer/destination/correlation checks and the named state boundaries. This does not certify the complete SAML role or every extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + }, + { + "file": "saml/session/soap_test.go", + "name": "TestSOAPBackChannelManagementAndLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/protocol/control_test.go", + "name": "TestControlMessages" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_control_test.go", + "name": "TestXMLSecSAMLControlSOAP" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Native xmlsec1 verifies and independently regenerates control-message SOAP signatures with Exclusive and Inclusive C14N. Full independent SAML-server/profile exchanges are not established by that component evidence." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs use locally trusted keys/metadata, authenticated messages, bounded parsing and atomic application-owned state. No received URL or XML KeyInfo adds trust." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy signature/encryption algorithms require the existing explicit algorithm policy; compatibility never bypasses peer, correlation or replay checks." } }, - "gap": "SOAP Single Logout is unimplemented; unlike Lite roles it is mandatory for the selected full role.", + "gap": "Complete independent obligation-level evidence and a deployed external HoK IdP exchange remain open. ECP CS01 2.3.11 XML-proof interpretation is unresolved; see docs/saml.md#ecp-2013-holder-of-key.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -793068,33 +794005,68 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Executable positive/negative tests cover the implemented 2005/Approved Errata 05 workflow, exact peer/destination/correlation checks and the named state boundaries. This does not certify the complete SAML role or every extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + }, + { + "file": "saml/session/soap_test.go", + "name": "TestSOAPBackChannelManagementAndLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/protocol/control_test.go", + "name": "TestControlMessages" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_control_test.go", + "name": "TestXMLSecSAMLControlSOAP" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Native xmlsec1 verifies and independently regenerates control-message SOAP signatures with Exclusive and Inclusive C14N. Full independent SAML-server/profile exchanges are not established by that component evidence." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs use locally trusted keys/metadata, authenticated messages, bounded parsing and atomic application-owned state. No received URL or XML KeyInfo adds trust." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy signature/encryption algorithms require the existing explicit algorithm policy; compatibility never bypasses peer, correlation or replay checks." } }, - "gap": "SOAP Single Logout is unimplemented; unlike Lite roles it is mandatory for the selected full role.", + "gap": "Complete independent obligation-level evidence and a deployed external HoK IdP exchange remain open. ECP CS01 2.3.11 XML-proof interpretation is unresolved; see docs/saml.md#ecp-2013-holder-of-key.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -793313,33 +794285,68 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignNameIDMappingRequest", + "saml/protocol.VerifyNameIDMappingRequest", + "saml/protocol.SignNameIDMappingResponse", + "saml/protocol.VerifyNameIDMappingResponse", + "saml/authority.NewService" + ], + "code": [ + "saml/protocol/mapping.go", + "saml/authority/authority.go", + "saml/authority/client.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "SOAP mapping, encrypted input/output, explicit target policy, status/correlation failures and limited-use assertions now have executable tests.", + "positive": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/authority/authority_test.go", + "name": "TestAuthoritySOAPAndURI" + } + ], + "negative": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/protocol/mapping_test.go", + "name": "TestLimitedUseMappedIdentifier" + } + ] }, "interop": { "status": "pending", - "tests": [], - "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "tests": [ + { + "file": "tests/interop/saml_authority_test.go", + "name": "TestOpenSAMLAuthorityContracts" + } + ], + "fixtures": [ + "tests/interop/testdata/OpenSAMLAuthorityOracle.java" + ], + "reason": "OpenSAML 5.2.3 generates and validates NameID Mapping messages and signatures. Full Extended-role proxying and all optional cipher/identifier forms are separate." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicitly configured SOAP mapping with trusted peers, authorization and replay state." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Existing explicit legacy policies apply; plaintext mapping requires a deployment-specific exception." } }, - "gap": "The optional Extended role and its NameIDMapping SOAP exchange are unimplemented.", + "gap": "NameID Mapping SOAP is implemented and tested; arbitrary BaseID types, automatic Extended-role proxying and full independent role conformance remain outside this record.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -793360,34 +794367,69 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", + "saml/sp.NewServiceProvider", + "saml/idp.NewIdentityProvider" + ], + "code": [ + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "All seven encryption target combinations, required-encryption downgrade rejection, authenticated failure order, key rotation/budgets, optional Type and sibling keys are tested within solicited WebSSO.", + "positive": [ + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedResponseTargets" + }, + { + "file": "saml/sp/encryption_test.go", + "name": "TestEncryptedWebSSO" + } + ], + "negative": [ + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedResponsePoliciesAndFailureOrder" + }, + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedCBCAndUnsignedAssertion" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "forbidden", + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "Encrypted SAML id production/consumption is unimplemented in the orchestration/protocol API; lower-level XML Encryption availability does not provide this role workflow.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Implemented for the current NameID/string-attribute solicited WebSSO profile using explicitly selected RSA transports, inline/sibling EncryptedKey alternatives and no external key/cipher I/O. General SAML extensions, other key-management profiles and complete independent malformed-input coverage are not claimed.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-SP-ENCRYPTED-ASSERTION", @@ -793407,34 +794449,69 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", + "saml/sp.NewServiceProvider", + "saml/idp.NewIdentityProvider" + ], + "code": [ + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "All seven encryption target combinations, required-encryption downgrade rejection, authenticated failure order, key rotation/budgets, optional Type and sibling keys are tested within solicited WebSSO.", + "positive": [ + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedResponseTargets" + }, + { + "file": "saml/sp/encryption_test.go", + "name": "TestEncryptedWebSSO" + } + ], + "negative": [ + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedResponsePoliciesAndFailureOrder" + }, + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedCBCAndUnsignedAssertion" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "forbidden", + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "Encrypted SAML assertion production/consumption is unimplemented in the orchestration/protocol API; lower-level XML Encryption availability does not provide this role workflow.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Implemented for the current NameID/string-attribute solicited WebSSO profile using explicitly selected RSA transports, inline/sibling EncryptedKey alternatives and no external key/cipher I/O. General SAML extensions, other key-management profiles and complete independent malformed-input coverage are not claimed.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-SP-ENCRYPTED-ATTRIBUTE", @@ -793454,34 +794531,69 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", + "saml/sp.NewServiceProvider", + "saml/idp.NewIdentityProvider" + ], + "code": [ + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "All seven encryption target combinations, required-encryption downgrade rejection, authenticated failure order, key rotation/budgets, optional Type and sibling keys are tested within solicited WebSSO.", + "positive": [ + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedResponseTargets" + }, + { + "file": "saml/sp/encryption_test.go", + "name": "TestEncryptedWebSSO" + } + ], + "negative": [ + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedResponsePoliciesAndFailureOrder" + }, + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedCBCAndUnsignedAssertion" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "forbidden", + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "Encrypted SAML attribute production/consumption is unimplemented in the orchestration/protocol API; lower-level XML Encryption availability does not provide this role workflow.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Implemented for the current NameID/string-attribute solicited WebSSO profile using explicitly selected RSA transports, inline/sibling EncryptedKey alternatives and no external key/cipher I/O. General SAML extensions, other key-management profiles and complete independent malformed-input coverage are not claimed.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-SP-IDENTIFIERS", @@ -793661,34 +794773,69 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", + "saml/sp.NewServiceProvider", + "saml/idp.NewIdentityProvider" + ], + "code": [ + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Explicit algorithm permission, generation/consumption and secure-default rejection are tested at protocol level. HMAC uses explicit shared-secret credentials rather than metadata certificates.", + "positive": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ], + "negative": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "forbidden", + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "The SAML API does not expose this required/recommended algorithm workflow. Generic XMLDSig/XMLENC compatibility policies do not change the closed SAML signature/encryption policy.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Protocol-level compatibility workflows exist with explicit SHA-1/HMAC/CBC/RSA15 policies. SP/IdP metadata helpers use public-key credentials; full role and every independent algorithm/context combination remain unverified.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-SP-ALGORITHM-HMACSHA1", @@ -793711,34 +794858,69 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", + "saml/sp.NewServiceProvider", + "saml/idp.NewIdentityProvider" + ], + "code": [ + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Explicit algorithm permission, generation/consumption and secure-default rejection are tested at protocol level. HMAC uses explicit shared-secret credentials rather than metadata certificates.", + "positive": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ], + "negative": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "forbidden", + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "The SAML API does not expose this required/recommended algorithm workflow. Generic XMLDSig/XMLENC compatibility policies do not change the closed SAML signature/encryption policy.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Protocol-level compatibility workflows exist with explicit SHA-1/HMAC/CBC/RSA15 policies. SP/IdP metadata helpers use public-key credentials; full role and every independent algorithm/context combination remain unverified.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-SP-ALGORITHM-C14N10", @@ -793763,38 +794945,67 @@ "implementation": { "status": "limited", "apis": [ + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", "saml/sp.NewServiceProvider", - "saml/sp.ServiceProvider.Begin", - "saml/sp.ServiceProvider.ConsumePOST" + "saml/idp.NewIdentityProvider" ], "code": [ - "saml/sp/sp.go" + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", + "saml/idp/idp.go" ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Explicit algorithm permission, generation/consumption and secure-default rejection are tested at protocol level. HMAC uses explicit shared-secret credentials rather than metadata certificates.", + "positive": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ], + "negative": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { - "status": "enabled", - "reason": "Available within the explicitly documented bounded API and its algorithm/resource/trust settings." + "status": "forbidden", + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "not-applicable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "The underlying transform exists; complete SAML mode and independent algorithm evidence remain open.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Protocol-level compatibility workflows exist with explicit SHA-1/HMAC/CBC/RSA15 policies. SP/IdP metadata helpers use public-key credentials; full role and every independent algorithm/context combination remain unverified.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-SP-ALGORITHM-ENVELOPED", @@ -793819,38 +795030,67 @@ "implementation": { "status": "limited", "apis": [ + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", "saml/sp.NewServiceProvider", - "saml/sp.ServiceProvider.Begin", - "saml/sp.ServiceProvider.ConsumePOST" + "saml/idp.NewIdentityProvider" ], "code": [ - "saml/sp/sp.go" + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", + "saml/idp/idp.go" ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Explicit algorithm permission, generation/consumption and secure-default rejection are tested at protocol level. HMAC uses explicit shared-secret credentials rather than metadata certificates.", + "positive": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ], + "negative": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { - "status": "enabled", - "reason": "Available within the explicitly documented bounded API and its algorithm/resource/trust settings." + "status": "forbidden", + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "not-applicable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "The underlying transform exists; complete SAML mode and independent algorithm evidence remain open.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Protocol-level compatibility workflows exist with explicit SHA-1/HMAC/CBC/RSA15 policies. SP/IdP metadata helpers use public-key credentials; full role and every independent algorithm/context combination remain unverified.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-SP-ALGORITHM-RSA-SHA1", @@ -793873,34 +795113,69 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", + "saml/sp.NewServiceProvider", + "saml/idp.NewIdentityProvider" + ], + "code": [ + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Explicit algorithm permission, generation/consumption and secure-default rejection are tested at protocol level. HMAC uses explicit shared-secret credentials rather than metadata certificates.", + "positive": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ], + "negative": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "forbidden", + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "The SAML API does not expose this required/recommended algorithm workflow. Generic XMLDSig/XMLENC compatibility policies do not change the closed SAML signature/encryption policy.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Protocol-level compatibility workflows exist with explicit SHA-1/HMAC/CBC/RSA15 policies. SP/IdP metadata helpers use public-key credentials; full role and every independent algorithm/context combination remain unverified.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-SP-ALGORITHM-DSA-SHA1", @@ -793973,34 +795248,69 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", + "saml/sp.NewServiceProvider", + "saml/idp.NewIdentityProvider" + ], + "code": [ + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Explicit algorithm permission, generation/consumption and secure-default rejection are tested at protocol level. HMAC uses explicit shared-secret credentials rather than metadata certificates.", + "positive": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ], + "negative": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "forbidden", + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "The SAML API does not expose this required/recommended algorithm workflow. Generic XMLDSig/XMLENC compatibility policies do not change the closed SAML signature/encryption policy.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Protocol-level compatibility workflows exist with explicit SHA-1/HMAC/CBC/RSA15 policies. SP/IdP metadata helpers use public-key credentials; full role and every independent algorithm/context combination remain unverified.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-SP-ALGORITHM-AES128-CBC", @@ -794023,34 +795333,69 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", + "saml/sp.NewServiceProvider", + "saml/idp.NewIdentityProvider" + ], + "code": [ + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Explicit algorithm permission, generation/consumption and secure-default rejection are tested at protocol level. HMAC uses explicit shared-secret credentials rather than metadata certificates.", + "positive": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ], + "negative": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "forbidden", + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "The SAML API does not expose this required/recommended algorithm workflow. Generic XMLDSig/XMLENC compatibility policies do not change the closed SAML signature/encryption policy.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Protocol-level compatibility workflows exist with explicit SHA-1/HMAC/CBC/RSA15 policies. SP/IdP metadata helpers use public-key credentials; full role and every independent algorithm/context combination remain unverified.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-SP-ALGORITHM-AES256-CBC", @@ -794073,34 +795418,69 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", + "saml/sp.NewServiceProvider", + "saml/idp.NewIdentityProvider" + ], + "code": [ + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Explicit algorithm permission, generation/consumption and secure-default rejection are tested at protocol level. HMAC uses explicit shared-secret credentials rather than metadata certificates.", + "positive": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ], + "negative": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "forbidden", + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "The SAML API does not expose this required/recommended algorithm workflow. Generic XMLDSig/XMLENC compatibility policies do not change the closed SAML signature/encryption policy.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Protocol-level compatibility workflows exist with explicit SHA-1/HMAC/CBC/RSA15 policies. SP/IdP metadata helpers use public-key credentials; full role and every independent algorithm/context combination remain unverified.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-SP-ALGORITHM-RSA15", @@ -794123,34 +795503,69 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", + "saml/sp.NewServiceProvider", + "saml/idp.NewIdentityProvider" + ], + "code": [ + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Explicit algorithm permission, generation/consumption and secure-default rejection are tested at protocol level. HMAC uses explicit shared-secret credentials rather than metadata certificates.", + "positive": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ], + "negative": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "forbidden", + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "The SAML API does not expose this required/recommended algorithm workflow. Generic XMLDSig/XMLENC compatibility policies do not change the closed SAML signature/encryption policy.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Protocol-level compatibility workflows exist with explicit SHA-1/HMAC/CBC/RSA15 policies. SP/IdP metadata helpers use public-key credentials; full role and every independent algorithm/context combination remain unverified.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-SP-ALGORITHM-RSA-OAEP", @@ -794173,34 +795588,69 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", + "saml/sp.NewServiceProvider", + "saml/idp.NewIdentityProvider" + ], + "code": [ + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Explicit algorithm permission, generation/consumption and secure-default rejection are tested at protocol level. HMAC uses explicit shared-secret credentials rather than metadata certificates.", + "positive": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ], + "negative": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "forbidden", + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "The SAML API does not expose this required/recommended algorithm workflow. Generic XMLDSig/XMLENC compatibility policies do not change the closed SAML signature/encryption policy.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Protocol-level compatibility workflows exist with explicit SHA-1/HMAC/CBC/RSA15 policies. SP/IdP metadata helpers use public-key credentials; full role and every independent algorithm/context combination remain unverified.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-SP-TRANSPORT-SOAP-AUTH", @@ -794222,33 +795672,63 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/binding.DecodeSOAP", + "saml/binding.SOAPClient.RoundTrip", + "saml/protocol.SignSOAP" + ], + "code": [ + "saml/binding/soap.go", + "saml/protocol/soap.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Executable positive/negative tests cover the implemented 2005/Approved Errata 05 workflow, exact peer/destination/correlation checks and the named state boundaries. This does not certify the complete SAML role or every extension.", + "positive": [ + { + "file": "saml/binding/soap_test.go", + "name": "TestSOAPHTTPStatusAndRedirects" + }, + { + "file": "saml/protocol/control_test.go", + "name": "TestControlResponseAndSOAPCanonicalization" + } + ], + "negative": [ + { + "file": "saml/binding/soap_test.go", + "name": "TestSOAPIndependentEnvelopes" + }, + { + "file": "saml/binding/soap_test.go", + "name": "TestSOAPHTTPStatusAndRedirects" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_control_test.go", + "name": "TestXMLSecSAMLControlSOAP" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Native xmlsec1 verifies and independently regenerates control-message SOAP signatures with Exclusive and Inclusive C14N. Full independent SAML-server/profile exchanges are not established by that component evidence." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs use locally trusted keys/metadata, authenticated messages, bounded parsing and atomic application-owned state. No received URL or XML KeyInfo adds trust." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy signature/encryption algorithms require the existing explicit algorithm policy; compatibility never bypasses peer, correlation or replay checks." } }, - "gap": "No HTTP/SOAP/TLS transport implementation is supplied by this SAML library. Existing callbacks and modern deployment policy do not establish this edition-specific transport contract.", + "gap": "Complete independent obligation-level evidence and a deployed external HoK IdP exchange remain open. ECP CS01 2.3.11 XML-proof interpretation is unresolved; see docs/saml.md#ecp-2013-holder-of-key.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -794858,34 +796338,69 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", + "saml/sp.NewServiceProvider", + "saml/idp.NewIdentityProvider" + ], + "code": [ + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "All seven encryption target combinations, required-encryption downgrade rejection, authenticated failure order, key rotation/budgets, optional Type and sibling keys are tested within solicited WebSSO.", + "positive": [ + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedResponseTargets" + }, + { + "file": "saml/sp/encryption_test.go", + "name": "TestEncryptedWebSSO" + } + ], + "negative": [ + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedResponsePoliciesAndFailureOrder" + }, + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedCBCAndUnsignedAssertion" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "forbidden", + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "This assertion extension, encrypted representation or general content/processing form is unimplemented in the selected SAML API.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Implemented for the current NameID/string-attribute solicited WebSSO profile using explicitly selected RSA transports, inline/sibling EncryptedKey alternatives and no external key/cipher I/O. General SAML extensions, other key-management profiles and complete independent malformed-input coverage are not claimed.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-SP-SAMLCORE-2-2-5", @@ -795109,34 +796624,69 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", + "saml/sp.NewServiceProvider", + "saml/idp.NewIdentityProvider" + ], + "code": [ + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "All seven encryption target combinations, required-encryption downgrade rejection, authenticated failure order, key rotation/budgets, optional Type and sibling keys are tested within solicited WebSSO.", + "positive": [ + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedResponseTargets" + }, + { + "file": "saml/sp/encryption_test.go", + "name": "TestEncryptedWebSSO" + } + ], + "negative": [ + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedResponsePoliciesAndFailureOrder" + }, + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedCBCAndUnsignedAssertion" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "forbidden", + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "This assertion extension, encrypted representation or general content/processing form is unimplemented in the selected SAML API.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Implemented for the current NameID/string-attribute solicited WebSSO profile using explicitly selected RSA transports, inline/sibling EncryptedKey alternatives and no external key/cipher I/O. General SAML extensions, other key-management profiles and complete independent malformed-input coverage are not claimed.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-SP-SAMLCORE-2-4-1-1", @@ -795888,34 +797438,69 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", + "saml/sp.NewServiceProvider", + "saml/idp.NewIdentityProvider" + ], + "code": [ + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "All seven encryption target combinations, required-encryption downgrade rejection, authenticated failure order, key rotation/budgets, optional Type and sibling keys are tested within solicited WebSSO.", + "positive": [ + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedResponseTargets" + }, + { + "file": "saml/sp/encryption_test.go", + "name": "TestEncryptedWebSSO" + } + ], + "negative": [ + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedResponsePoliciesAndFailureOrder" + }, + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedCBCAndUnsignedAssertion" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "forbidden", + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "This assertion extension, encrypted representation or general content/processing form is unimplemented in the selected SAML API.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Implemented for the current NameID/string-attribute solicited WebSSO profile using explicitly selected RSA transports, inline/sibling EncryptedKey alternatives and no external key/cipher I/O. General SAML extensions, other key-management profiles and complete independent malformed-input coverage are not claimed.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-SP-SAMLCORE-2-7-4-1", @@ -796778,17 +798363,29 @@ }, "tests": { "status": "partial", - "reason": "These checks establish the named bounded cases; they do not cover every legal branch of the full clause.", + "reason": "Solicited requests distinguish the encrypted policy marker from the inner NameID format and permit any supported inner format for unspecified policy. End-to-end tests cover encrypted-marker requests, missing producer/consumer encryption policy, metadata restrictions, downgrade rejection and replay state.", "positive": [ { "file": "saml/protocol/protocol_test.go", "name": "TestAuthnRequest" + }, + { + "file": "saml/sp/encryption_test.go", + "name": "TestEncryptedWebSSO" } ], "negative": [ { "file": "saml/protocol/protocol_test.go", "name": "TestAuthnRequest" + }, + { + "file": "saml/sp/encryption_test.go", + "name": "TestEncryptedWebSSO" + }, + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedNameIDIsAPolicyValue" } ] }, @@ -796808,7 +798405,7 @@ "reason": "No general compatibility switch supplies missing normative behavior." } }, - "gap": "Only the closed signed solicited WebSSO subset is implemented. Complete legal syntax/cardinality, optional content, role processing and independent clause-specific cases remain open.", + "gap": "The selected solicited WebSSO NameIDPolicy rules include encrypted and unspecified Format semantics. General Subject/affiliation/extensions, full role workflows and independent clause-specific NameIDPolicy exchanges remain unverified.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -797027,15 +798624,36 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Respond", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestInvalidArtifactReturnsEmptySuccess" + } + ] }, "interop": { "status": "pending", @@ -797045,15 +798663,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "Artifact Resolution protocol processing is unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -797074,15 +798692,36 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Respond", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestInvalidArtifactReturnsEmptySuccess" + } + ] }, "interop": { "status": "pending", @@ -797092,15 +798731,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "Artifact Resolution protocol processing is unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -797121,15 +798760,36 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Respond", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestInvalidArtifactReturnsEmptySuccess" + } + ] }, "interop": { "status": "pending", @@ -797139,15 +798799,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "Artifact Resolution protocol processing is unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -797170,15 +798830,37 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Manage", + "saml/session.Service.BeginManage", + "saml/session.Service.Complete" + ], + "code": [ + "saml/session/session.go", + "saml/session/reply.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/exchange_test.go", + "name": "TestManageNameIDBothDirectionsAndBindings" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestManageAliasTerminationAndAtomicReplay" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestControlSigningFailureDoesNotCommitState" + } + ] }, "interop": { "status": "pending", @@ -797188,15 +798870,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "Name Identifier Management protocol processing is unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -797219,15 +798901,37 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Manage", + "saml/session.Service.BeginManage", + "saml/session.Service.Complete" + ], + "code": [ + "saml/session/session.go", + "saml/session/reply.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/exchange_test.go", + "name": "TestManageNameIDBothDirectionsAndBindings" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestManageAliasTerminationAndAtomicReplay" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestControlSigningFailureDoesNotCommitState" + } + ] }, "interop": { "status": "pending", @@ -797237,15 +798941,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "Name Identifier Management protocol processing is unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -797270,15 +798974,37 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Manage", + "saml/session.Service.BeginManage", + "saml/session.Service.Complete" + ], + "code": [ + "saml/session/session.go", + "saml/session/reply.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/exchange_test.go", + "name": "TestManageNameIDBothDirectionsAndBindings" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestManageAliasTerminationAndAtomicReplay" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestControlSigningFailureDoesNotCommitState" + } + ] }, "interop": { "status": "pending", @@ -797288,15 +799014,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "Name Identifier Management protocol processing is unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -797320,15 +799046,41 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/session/session_test.go", + "name": "TestLogoutMarkersSurviveIdentifierChange" + } + ] }, "interop": { "status": "pending", @@ -797338,15 +799090,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "Single Logout protocol processing is unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -797367,15 +799119,41 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/session/session_test.go", + "name": "TestLogoutMarkersSurviveIdentifierChange" + } + ] }, "interop": { "status": "pending", @@ -797385,15 +799163,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "Single Logout protocol processing is unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -797416,15 +799194,41 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/session/session_test.go", + "name": "TestLogoutMarkersSurviveIdentifierChange" + } + ] }, "interop": { "status": "pending", @@ -797434,15 +799238,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "Single Logout protocol processing is unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -797465,15 +799269,41 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/session/session_test.go", + "name": "TestLogoutMarkersSurviveIdentifierChange" + } + ] }, "interop": { "status": "pending", @@ -797483,15 +799313,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "Single Logout protocol processing is unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -797512,33 +799342,68 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignNameIDMappingRequest", + "saml/protocol.VerifyNameIDMappingRequest", + "saml/protocol.SignNameIDMappingResponse", + "saml/protocol.VerifyNameIDMappingResponse", + "saml/authority.NewService" + ], + "code": [ + "saml/protocol/mapping.go", + "saml/authority/authority.go", + "saml/authority/client.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "SOAP mapping, encrypted input/output, explicit target policy, status/correlation failures and limited-use assertions now have executable tests.", + "positive": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/authority/authority_test.go", + "name": "TestAuthoritySOAPAndURI" + } + ], + "negative": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/protocol/mapping_test.go", + "name": "TestLimitedUseMappedIdentifier" + } + ] }, "interop": { "status": "pending", - "tests": [], - "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "tests": [ + { + "file": "tests/interop/saml_authority_test.go", + "name": "TestOpenSAMLAuthorityContracts" + } + ], + "fixtures": [ + "tests/interop/testdata/OpenSAMLAuthorityOracle.java" + ], + "reason": "OpenSAML 5.2.3 generates and validates NameID Mapping messages and signatures. Full Extended-role proxying and all optional cipher/identifier forms are separate." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicitly configured SOAP mapping with trusted peers, authorization and replay state." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Existing explicit legacy policies apply; plaintext mapping requires a deployment-specific exception." } }, - "gap": "The optional Extended Name Identifier Mapping protocol is unimplemented.", + "gap": "NameID Mapping SOAP is implemented and tested; arbitrary BaseID types, automatic Extended-role proxying and full independent role conformance remain outside this record.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -797559,33 +799424,68 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignNameIDMappingRequest", + "saml/protocol.VerifyNameIDMappingRequest", + "saml/protocol.SignNameIDMappingResponse", + "saml/protocol.VerifyNameIDMappingResponse", + "saml/authority.NewService" + ], + "code": [ + "saml/protocol/mapping.go", + "saml/authority/authority.go", + "saml/authority/client.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "SOAP mapping, encrypted input/output, explicit target policy, status/correlation failures and limited-use assertions now have executable tests.", + "positive": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/authority/authority_test.go", + "name": "TestAuthoritySOAPAndURI" + } + ], + "negative": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/protocol/mapping_test.go", + "name": "TestLimitedUseMappedIdentifier" + } + ] }, "interop": { "status": "pending", - "tests": [], - "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "tests": [ + { + "file": "tests/interop/saml_authority_test.go", + "name": "TestOpenSAMLAuthorityContracts" + } + ], + "fixtures": [ + "tests/interop/testdata/OpenSAMLAuthorityOracle.java" + ], + "reason": "OpenSAML 5.2.3 generates and validates NameID Mapping messages and signatures. Full Extended-role proxying and all optional cipher/identifier forms are separate." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicitly configured SOAP mapping with trusted peers, authorization and replay state." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Existing explicit legacy policies apply; plaintext mapping requires a deployment-specific exception." } }, - "gap": "The optional Extended Name Identifier Mapping protocol is unimplemented.", + "gap": "NameID Mapping SOAP is implemented and tested; arbitrary BaseID types, automatic Extended-role proxying and full independent role conformance remain outside this record.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -797606,33 +799506,68 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignNameIDMappingRequest", + "saml/protocol.VerifyNameIDMappingRequest", + "saml/protocol.SignNameIDMappingResponse", + "saml/protocol.VerifyNameIDMappingResponse", + "saml/authority.NewService" + ], + "code": [ + "saml/protocol/mapping.go", + "saml/authority/authority.go", + "saml/authority/client.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "SOAP mapping, encrypted input/output, explicit target policy, status/correlation failures and limited-use assertions now have executable tests.", + "positive": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/authority/authority_test.go", + "name": "TestAuthoritySOAPAndURI" + } + ], + "negative": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/protocol/mapping_test.go", + "name": "TestLimitedUseMappedIdentifier" + } + ] }, "interop": { "status": "pending", - "tests": [], - "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "tests": [ + { + "file": "tests/interop/saml_authority_test.go", + "name": "TestOpenSAMLAuthorityContracts" + } + ], + "fixtures": [ + "tests/interop/testdata/OpenSAMLAuthorityOracle.java" + ], + "reason": "OpenSAML 5.2.3 generates and validates NameID Mapping messages and signatures. Full Extended-role proxying and all optional cipher/identifier forms are separate." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicitly configured SOAP mapping with trusted peers, authorization and replay state." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Existing explicit legacy policies apply; plaintext mapping requires a deployment-specific exception." } }, - "gap": "The optional Extended Name Identifier Mapping protocol is unimplemented.", + "gap": "NameID Mapping SOAP is implemented and tested; arbitrary BaseID types, automatic Extended-role proxying and full independent role conformance remain outside this record.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -798431,34 +800366,69 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", + "saml/sp.NewServiceProvider", + "saml/idp.NewIdentityProvider" + ], + "code": [ + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "All seven encryption target combinations, required-encryption downgrade rejection, authenticated failure order, key rotation/budgets, optional Type and sibling keys are tested within solicited WebSSO.", + "positive": [ + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedResponseTargets" + }, + { + "file": "saml/sp/encryption_test.go", + "name": "TestEncryptedWebSSO" + } + ], + "negative": [ + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedResponsePoliciesAndFailureOrder" + }, + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedCBCAndUnsignedAssertion" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "forbidden", + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "This assertion extension, encrypted representation or general content/processing form is unimplemented in the selected SAML API.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Implemented for the current NameID/string-attribute solicited WebSSO profile using explicitly selected RSA transports, inline/sibling EncryptedKey alternatives and no external key/cipher I/O. General SAML extensions, other key-management profiles and complete independent malformed-input coverage are not claimed.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-SP-SAMLCORE-6-2", @@ -798481,34 +800451,69 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", + "saml/sp.NewServiceProvider", + "saml/idp.NewIdentityProvider" + ], + "code": [ + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "All seven encryption target combinations, required-encryption downgrade rejection, authenticated failure order, key rotation/budgets, optional Type and sibling keys are tested within solicited WebSSO.", + "positive": [ + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedResponseTargets" + }, + { + "file": "saml/sp/encryption_test.go", + "name": "TestEncryptedWebSSO" + } + ], + "negative": [ + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedResponsePoliciesAndFailureOrder" + }, + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedCBCAndUnsignedAssertion" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "forbidden", + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "This assertion extension, encrypted representation or general content/processing form is unimplemented in the selected SAML API.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Implemented for the current NameID/string-attribute solicited WebSSO profile using explicitly selected RSA transports, inline/sibling EncryptedKey alternatives and no external key/cipher I/O. General SAML extensions, other key-management profiles and complete independent malformed-input coverage are not claimed.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-SP-SAMLCORE-7-1-1", @@ -800210,15 +802215,34 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/binding.SOAPClient.RoundTrip", + "saml/binding.DecodeSOAP" + ], + "code": [ + "saml/binding/soap.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/soap_test.go", + "name": "TestSOAPBackChannelManagementAndLogout" + } + ], + "negative": [ + { + "file": "saml/binding/soap_test.go", + "name": "TestSOAPIndependentEnvelopes" + }, + { + "file": "saml/binding/soap_test.go", + "name": "TestSOAPHTTPStatusAndRedirects" + } + ] }, "interop": { "status": "pending", @@ -800228,15 +802252,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -800259,15 +802283,34 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/binding.SOAPClient.RoundTrip", + "saml/binding.DecodeSOAP" + ], + "code": [ + "saml/binding/soap.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/soap_test.go", + "name": "TestSOAPBackChannelManagementAndLogout" + } + ], + "negative": [ + { + "file": "saml/binding/soap_test.go", + "name": "TestSOAPIndependentEnvelopes" + }, + { + "file": "saml/binding/soap_test.go", + "name": "TestSOAPHTTPStatusAndRedirects" + } + ] }, "interop": { "status": "pending", @@ -800277,15 +802320,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -800306,15 +802349,34 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/binding.SOAPClient.RoundTrip", + "saml/binding.DecodeSOAP" + ], + "code": [ + "saml/binding/soap.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/soap_test.go", + "name": "TestSOAPBackChannelManagementAndLogout" + } + ], + "negative": [ + { + "file": "saml/binding/soap_test.go", + "name": "TestSOAPIndependentEnvelopes" + }, + { + "file": "saml/binding/soap_test.go", + "name": "TestSOAPHTTPStatusAndRedirects" + } + ] }, "interop": { "status": "pending", @@ -800324,15 +802386,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -800353,15 +802415,34 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/binding.SOAPClient.RoundTrip", + "saml/binding.DecodeSOAP" + ], + "code": [ + "saml/binding/soap.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/soap_test.go", + "name": "TestSOAPBackChannelManagementAndLogout" + } + ], + "negative": [ + { + "file": "saml/binding/soap_test.go", + "name": "TestSOAPIndependentEnvelopes" + }, + { + "file": "saml/binding/soap_test.go", + "name": "TestSOAPHTTPStatusAndRedirects" + } + ] }, "interop": { "status": "pending", @@ -800371,15 +802452,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -800400,15 +802481,34 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/binding.SOAPClient.RoundTrip", + "saml/binding.DecodeSOAP" + ], + "code": [ + "saml/binding/soap.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/soap_test.go", + "name": "TestSOAPBackChannelManagementAndLogout" + } + ], + "negative": [ + { + "file": "saml/binding/soap_test.go", + "name": "TestSOAPIndependentEnvelopes" + }, + { + "file": "saml/binding/soap_test.go", + "name": "TestSOAPHTTPStatusAndRedirects" + } + ] }, "interop": { "status": "pending", @@ -800418,15 +802518,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -800447,15 +802547,34 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/binding.SOAPClient.RoundTrip", + "saml/binding.DecodeSOAP" + ], + "code": [ + "saml/binding/soap.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/soap_test.go", + "name": "TestSOAPBackChannelManagementAndLogout" + } + ], + "negative": [ + { + "file": "saml/binding/soap_test.go", + "name": "TestSOAPIndependentEnvelopes" + }, + { + "file": "saml/binding/soap_test.go", + "name": "TestSOAPHTTPStatusAndRedirects" + } + ] }, "interop": { "status": "pending", @@ -800465,15 +802584,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -800494,15 +802613,34 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/binding.SOAPClient.RoundTrip", + "saml/binding.DecodeSOAP" + ], + "code": [ + "saml/binding/soap.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/soap_test.go", + "name": "TestSOAPBackChannelManagementAndLogout" + } + ], + "negative": [ + { + "file": "saml/binding/soap_test.go", + "name": "TestSOAPIndependentEnvelopes" + }, + { + "file": "saml/binding/soap_test.go", + "name": "TestSOAPHTTPStatusAndRedirects" + } + ] }, "interop": { "status": "pending", @@ -800512,15 +802650,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -800541,15 +802679,44 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -800559,15 +802726,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -800588,15 +802755,44 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -800606,15 +802802,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -800637,15 +802833,44 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -800655,15 +802880,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -800686,15 +802911,44 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -800704,15 +802958,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -800733,15 +802987,44 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -800751,15 +803034,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -800782,15 +803065,44 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -800800,15 +803112,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -800831,15 +803143,44 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -800849,15 +803190,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -801748,15 +804089,36 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Respond", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestInvalidArtifactReturnsEmptySuccess" + } + ] }, "interop": { "status": "pending", @@ -801766,15 +804128,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -801795,15 +804157,36 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Respond", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestInvalidArtifactReturnsEmptySuccess" + } + ] }, "interop": { "status": "pending", @@ -801813,15 +804196,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -801842,15 +804225,36 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Respond", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestInvalidArtifactReturnsEmptySuccess" + } + ] }, "interop": { "status": "pending", @@ -801860,15 +804264,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -801889,15 +804293,36 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Respond", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestInvalidArtifactReturnsEmptySuccess" + } + ] }, "interop": { "status": "pending", @@ -801907,15 +804332,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -801936,15 +804361,36 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Respond", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestInvalidArtifactReturnsEmptySuccess" + } + ] }, "interop": { "status": "pending", @@ -801954,15 +804400,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -801985,15 +804431,36 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Respond", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestInvalidArtifactReturnsEmptySuccess" + } + ] }, "interop": { "status": "pending", @@ -802003,15 +804470,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -802034,15 +804501,36 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Respond", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestInvalidArtifactReturnsEmptySuccess" + } + ] }, "interop": { "status": "pending", @@ -802052,15 +804540,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -802081,15 +804569,36 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Respond", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestInvalidArtifactReturnsEmptySuccess" + } + ] }, "interop": { "status": "pending", @@ -802099,15 +804608,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -802130,15 +804639,36 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Respond", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestInvalidArtifactReturnsEmptySuccess" + } + ] }, "interop": { "status": "pending", @@ -802148,15 +804678,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -802177,15 +804707,36 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Respond", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestInvalidArtifactReturnsEmptySuccess" + } + ] }, "interop": { "status": "pending", @@ -802195,15 +804746,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -802226,15 +804777,36 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Respond", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestInvalidArtifactReturnsEmptySuccess" + } + ] }, "interop": { "status": "pending", @@ -802244,15 +804816,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -803797,15 +806369,44 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -803815,15 +806416,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -803846,15 +806447,44 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -803864,15 +806494,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -803895,15 +806525,44 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -803913,15 +806572,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -803942,15 +806601,44 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -803960,15 +806648,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -803989,15 +806677,44 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -804007,15 +806724,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -804036,15 +806753,44 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -804054,15 +806800,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -804083,15 +806829,44 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -804101,15 +806876,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -804130,15 +806905,44 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -804148,15 +806952,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -804177,15 +806981,44 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -804195,15 +807028,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -804224,15 +807057,44 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -804242,15 +807104,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -804274,15 +807136,44 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -804292,15 +807183,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -804321,15 +807212,44 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -804339,15 +807259,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -804370,15 +807290,44 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -804388,15 +807337,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -804417,15 +807366,44 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -804435,15 +807413,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -804464,15 +807442,44 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -804482,15 +807489,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -804511,15 +807518,44 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -804529,15 +807565,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -804702,15 +807738,41 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/session/session_test.go", + "name": "TestLogoutMarkersSurviveIdentifierChange" + } + ] }, "interop": { "status": "pending", @@ -804720,15 +807782,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -804749,15 +807811,41 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/session/session_test.go", + "name": "TestLogoutMarkersSurviveIdentifierChange" + } + ] }, "interop": { "status": "pending", @@ -804767,15 +807855,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -804796,15 +807884,41 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/session/session_test.go", + "name": "TestLogoutMarkersSurviveIdentifierChange" + } + ] }, "interop": { "status": "pending", @@ -804814,15 +807928,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -804843,15 +807957,41 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/session/session_test.go", + "name": "TestLogoutMarkersSurviveIdentifierChange" + } + ] }, "interop": { "status": "pending", @@ -804861,15 +808001,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -804890,15 +808030,41 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/session/session_test.go", + "name": "TestLogoutMarkersSurviveIdentifierChange" + } + ] }, "interop": { "status": "pending", @@ -804908,15 +808074,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -804937,15 +808103,41 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/session/session_test.go", + "name": "TestLogoutMarkersSurviveIdentifierChange" + } + ] }, "interop": { "status": "pending", @@ -804955,15 +808147,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -804984,15 +808176,41 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/session/session_test.go", + "name": "TestLogoutMarkersSurviveIdentifierChange" + } + ] }, "interop": { "status": "pending", @@ -805002,15 +808220,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -805033,15 +808251,41 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/session/session_test.go", + "name": "TestLogoutMarkersSurviveIdentifierChange" + } + ] }, "interop": { "status": "pending", @@ -805051,15 +808295,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -805080,15 +808324,41 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/session/session_test.go", + "name": "TestLogoutMarkersSurviveIdentifierChange" + } + ] }, "interop": { "status": "pending", @@ -805098,15 +808368,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -805127,15 +808397,41 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/session/session_test.go", + "name": "TestLogoutMarkersSurviveIdentifierChange" + } + ] }, "interop": { "status": "pending", @@ -805145,15 +808441,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -805177,15 +808473,37 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Manage", + "saml/session.Service.BeginManage", + "saml/session.Service.Complete" + ], + "code": [ + "saml/session/session.go", + "saml/session/reply.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/exchange_test.go", + "name": "TestManageNameIDBothDirectionsAndBindings" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestManageAliasTerminationAndAtomicReplay" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestControlSigningFailureDoesNotCommitState" + } + ] }, "interop": { "status": "pending", @@ -805195,15 +808513,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -805227,15 +808545,37 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Manage", + "saml/session.Service.BeginManage", + "saml/session.Service.Complete" + ], + "code": [ + "saml/session/session.go", + "saml/session/reply.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/exchange_test.go", + "name": "TestManageNameIDBothDirectionsAndBindings" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestManageAliasTerminationAndAtomicReplay" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestControlSigningFailureDoesNotCommitState" + } + ] }, "interop": { "status": "pending", @@ -805245,15 +808585,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -805277,15 +808617,37 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Manage", + "saml/session.Service.BeginManage", + "saml/session.Service.Complete" + ], + "code": [ + "saml/session/session.go", + "saml/session/reply.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/exchange_test.go", + "name": "TestManageNameIDBothDirectionsAndBindings" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestManageAliasTerminationAndAtomicReplay" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestControlSigningFailureDoesNotCommitState" + } + ] }, "interop": { "status": "pending", @@ -805295,15 +808657,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -805327,15 +808689,37 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Manage", + "saml/session.Service.BeginManage", + "saml/session.Service.Complete" + ], + "code": [ + "saml/session/session.go", + "saml/session/reply.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/exchange_test.go", + "name": "TestManageNameIDBothDirectionsAndBindings" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestManageAliasTerminationAndAtomicReplay" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestControlSigningFailureDoesNotCommitState" + } + ] }, "interop": { "status": "pending", @@ -805345,15 +808729,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -805377,15 +808761,37 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Manage", + "saml/session.Service.BeginManage", + "saml/session.Service.Complete" + ], + "code": [ + "saml/session/session.go", + "saml/session/reply.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/exchange_test.go", + "name": "TestManageNameIDBothDirectionsAndBindings" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestManageAliasTerminationAndAtomicReplay" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestControlSigningFailureDoesNotCommitState" + } + ] }, "interop": { "status": "pending", @@ -805395,15 +808801,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -805427,15 +808833,37 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Manage", + "saml/session.Service.BeginManage", + "saml/session.Service.Complete" + ], + "code": [ + "saml/session/session.go", + "saml/session/reply.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/exchange_test.go", + "name": "TestManageNameIDBothDirectionsAndBindings" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestManageAliasTerminationAndAtomicReplay" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestControlSigningFailureDoesNotCommitState" + } + ] }, "interop": { "status": "pending", @@ -805445,15 +808873,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -805477,15 +808905,37 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Manage", + "saml/session.Service.BeginManage", + "saml/session.Service.Complete" + ], + "code": [ + "saml/session/session.go", + "saml/session/reply.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/exchange_test.go", + "name": "TestManageNameIDBothDirectionsAndBindings" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestManageAliasTerminationAndAtomicReplay" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestControlSigningFailureDoesNotCommitState" + } + ] }, "interop": { "status": "pending", @@ -805495,15 +808945,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -806182,33 +809632,68 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignNameIDMappingRequest", + "saml/protocol.VerifyNameIDMappingRequest", + "saml/protocol.SignNameIDMappingResponse", + "saml/protocol.VerifyNameIDMappingResponse", + "saml/authority.NewService" + ], + "code": [ + "saml/protocol/mapping.go", + "saml/authority/authority.go", + "saml/authority/client.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "SOAP mapping, encrypted input/output, explicit target policy, status/correlation failures and limited-use assertions now have executable tests.", + "positive": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/authority/authority_test.go", + "name": "TestAuthoritySOAPAndURI" + } + ], + "negative": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/protocol/mapping_test.go", + "name": "TestLimitedUseMappedIdentifier" + } + ] }, "interop": { "status": "pending", - "tests": [], - "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "tests": [ + { + "file": "tests/interop/saml_authority_test.go", + "name": "TestOpenSAMLAuthorityContracts" + } + ], + "fixtures": [ + "tests/interop/testdata/OpenSAMLAuthorityOracle.java" + ], + "reason": "OpenSAML 5.2.3 generates and validates NameID Mapping messages and signatures. Full Extended-role proxying and all optional cipher/identifier forms are separate." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicitly configured SOAP mapping with trusted peers, authorization and replay state." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Existing explicit legacy policies apply; plaintext mapping requires a deployment-specific exception." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "NameID Mapping SOAP is implemented and tested; arbitrary BaseID types, automatic Extended-role proxying and full independent role conformance remain outside this record.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -806229,33 +809714,68 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignNameIDMappingRequest", + "saml/protocol.VerifyNameIDMappingRequest", + "saml/protocol.SignNameIDMappingResponse", + "saml/protocol.VerifyNameIDMappingResponse", + "saml/authority.NewService" + ], + "code": [ + "saml/protocol/mapping.go", + "saml/authority/authority.go", + "saml/authority/client.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "SOAP mapping, encrypted input/output, explicit target policy, status/correlation failures and limited-use assertions now have executable tests.", + "positive": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/authority/authority_test.go", + "name": "TestAuthoritySOAPAndURI" + } + ], + "negative": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/protocol/mapping_test.go", + "name": "TestLimitedUseMappedIdentifier" + } + ] }, "interop": { "status": "pending", - "tests": [], - "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "tests": [ + { + "file": "tests/interop/saml_authority_test.go", + "name": "TestOpenSAMLAuthorityContracts" + } + ], + "fixtures": [ + "tests/interop/testdata/OpenSAMLAuthorityOracle.java" + ], + "reason": "OpenSAML 5.2.3 generates and validates NameID Mapping messages and signatures. Full Extended-role proxying and all optional cipher/identifier forms are separate." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicitly configured SOAP mapping with trusted peers, authorization and replay state." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Existing explicit legacy policies apply; plaintext mapping requires a deployment-specific exception." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "NameID Mapping SOAP is implemented and tested; arbitrary BaseID types, automatic Extended-role proxying and full independent role conformance remain outside this record.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -806276,33 +809796,68 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignNameIDMappingRequest", + "saml/protocol.VerifyNameIDMappingRequest", + "saml/protocol.SignNameIDMappingResponse", + "saml/protocol.VerifyNameIDMappingResponse", + "saml/authority.NewService" + ], + "code": [ + "saml/protocol/mapping.go", + "saml/authority/authority.go", + "saml/authority/client.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "SOAP mapping, encrypted input/output, explicit target policy, status/correlation failures and limited-use assertions now have executable tests.", + "positive": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/authority/authority_test.go", + "name": "TestAuthoritySOAPAndURI" + } + ], + "negative": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/protocol/mapping_test.go", + "name": "TestLimitedUseMappedIdentifier" + } + ] }, "interop": { "status": "pending", - "tests": [], - "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "tests": [ + { + "file": "tests/interop/saml_authority_test.go", + "name": "TestOpenSAMLAuthorityContracts" + } + ], + "fixtures": [ + "tests/interop/testdata/OpenSAMLAuthorityOracle.java" + ], + "reason": "OpenSAML 5.2.3 generates and validates NameID Mapping messages and signatures. Full Extended-role proxying and all optional cipher/identifier forms are separate." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicitly configured SOAP mapping with trusted peers, authorization and replay state." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Existing explicit legacy policies apply; plaintext mapping requires a deployment-specific exception." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "NameID Mapping SOAP is implemented and tested; arbitrary BaseID types, automatic Extended-role proxying and full independent role conformance remain outside this record.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -806323,33 +809878,68 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignNameIDMappingRequest", + "saml/protocol.VerifyNameIDMappingRequest", + "saml/protocol.SignNameIDMappingResponse", + "saml/protocol.VerifyNameIDMappingResponse", + "saml/authority.NewService" + ], + "code": [ + "saml/protocol/mapping.go", + "saml/authority/authority.go", + "saml/authority/client.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "SOAP mapping, encrypted input/output, explicit target policy, status/correlation failures and limited-use assertions now have executable tests.", + "positive": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/authority/authority_test.go", + "name": "TestAuthoritySOAPAndURI" + } + ], + "negative": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/protocol/mapping_test.go", + "name": "TestLimitedUseMappedIdentifier" + } + ] }, "interop": { "status": "pending", - "tests": [], - "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "tests": [ + { + "file": "tests/interop/saml_authority_test.go", + "name": "TestOpenSAMLAuthorityContracts" + } + ], + "fixtures": [ + "tests/interop/testdata/OpenSAMLAuthorityOracle.java" + ], + "reason": "OpenSAML 5.2.3 generates and validates NameID Mapping messages and signatures. Full Extended-role proxying and all optional cipher/identifier forms are separate." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicitly configured SOAP mapping with trusted peers, authorization and replay state." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Existing explicit legacy policies apply; plaintext mapping requires a deployment-specific exception." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "NameID Mapping SOAP is implemented and tested; arbitrary BaseID types, automatic Extended-role proxying and full independent role conformance remain outside this record.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -806370,33 +809960,68 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignNameIDMappingRequest", + "saml/protocol.VerifyNameIDMappingRequest", + "saml/protocol.SignNameIDMappingResponse", + "saml/protocol.VerifyNameIDMappingResponse", + "saml/authority.NewService" + ], + "code": [ + "saml/protocol/mapping.go", + "saml/authority/authority.go", + "saml/authority/client.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "SOAP mapping, encrypted input/output, explicit target policy, status/correlation failures and limited-use assertions now have executable tests.", + "positive": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/authority/authority_test.go", + "name": "TestAuthoritySOAPAndURI" + } + ], + "negative": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/protocol/mapping_test.go", + "name": "TestLimitedUseMappedIdentifier" + } + ] }, "interop": { "status": "pending", - "tests": [], - "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "tests": [ + { + "file": "tests/interop/saml_authority_test.go", + "name": "TestOpenSAMLAuthorityContracts" + } + ], + "fixtures": [ + "tests/interop/testdata/OpenSAMLAuthorityOracle.java" + ], + "reason": "OpenSAML 5.2.3 generates and validates NameID Mapping messages and signatures. Full Extended-role proxying and all optional cipher/identifier forms are separate." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicitly configured SOAP mapping with trusted peers, authorization and replay state." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Existing explicit legacy policies apply; plaintext mapping requires a deployment-specific exception." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "NameID Mapping SOAP is implemented and tested; arbitrary BaseID types, automatic Extended-role proxying and full independent role conformance remain outside this record.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -806417,33 +810042,68 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignNameIDMappingRequest", + "saml/protocol.VerifyNameIDMappingRequest", + "saml/protocol.SignNameIDMappingResponse", + "saml/protocol.VerifyNameIDMappingResponse", + "saml/authority.NewService" + ], + "code": [ + "saml/protocol/mapping.go", + "saml/authority/authority.go", + "saml/authority/client.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "SOAP mapping, encrypted input/output, explicit target policy, status/correlation failures and limited-use assertions now have executable tests.", + "positive": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/authority/authority_test.go", + "name": "TestAuthoritySOAPAndURI" + } + ], + "negative": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/protocol/mapping_test.go", + "name": "TestLimitedUseMappedIdentifier" + } + ] }, "interop": { "status": "pending", - "tests": [], - "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "tests": [ + { + "file": "tests/interop/saml_authority_test.go", + "name": "TestOpenSAMLAuthorityContracts" + } + ], + "fixtures": [ + "tests/interop/testdata/OpenSAMLAuthorityOracle.java" + ], + "reason": "OpenSAML 5.2.3 generates and validates NameID Mapping messages and signatures. Full Extended-role proxying and all optional cipher/identifier forms are separate." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicitly configured SOAP mapping with trusted peers, authorization and replay state." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Existing explicit legacy policies apply; plaintext mapping requires a deployment-specific exception." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "NameID Mapping SOAP is implemented and tested; arbitrary BaseID types, automatic Extended-role proxying and full independent role conformance remain outside this record.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -806464,33 +810124,68 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignNameIDMappingRequest", + "saml/protocol.VerifyNameIDMappingRequest", + "saml/protocol.SignNameIDMappingResponse", + "saml/protocol.VerifyNameIDMappingResponse", + "saml/authority.NewService" + ], + "code": [ + "saml/protocol/mapping.go", + "saml/authority/authority.go", + "saml/authority/client.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "SOAP mapping, encrypted input/output, explicit target policy, status/correlation failures and limited-use assertions now have executable tests.", + "positive": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/authority/authority_test.go", + "name": "TestAuthoritySOAPAndURI" + } + ], + "negative": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/protocol/mapping_test.go", + "name": "TestLimitedUseMappedIdentifier" + } + ] }, "interop": { "status": "pending", - "tests": [], - "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "tests": [ + { + "file": "tests/interop/saml_authority_test.go", + "name": "TestOpenSAMLAuthorityContracts" + } + ], + "fixtures": [ + "tests/interop/testdata/OpenSAMLAuthorityOracle.java" + ], + "reason": "OpenSAML 5.2.3 generates and validates NameID Mapping messages and signatures. Full Extended-role proxying and all optional cipher/identifier forms are separate." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicitly configured SOAP mapping with trusted peers, authorization and replay state." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Existing explicit legacy policies apply; plaintext mapping requires a deployment-specific exception." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "NameID Mapping SOAP is implemented and tested; arbitrary BaseID types, automatic Extended-role proxying and full independent role conformance remain outside this record.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -810006,15 +813701,37 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Manage", + "saml/session.Service.BeginManage", + "saml/session.Service.Complete" + ], + "code": [ + "saml/session/session.go", + "saml/session/reply.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/exchange_test.go", + "name": "TestManageNameIDBothDirectionsAndBindings" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestManageAliasTerminationAndAtomicReplay" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestControlSigningFailureDoesNotCommitState" + } + ] }, "interop": { "status": "pending", @@ -810024,15 +813741,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The corrected encrypted or additional protocol/profile workflow is unimplemented; selecting an erratum does not implement it.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -810055,15 +813772,41 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/session/session_test.go", + "name": "TestLogoutMarkersSurviveIdentifierChange" + } + ] }, "interop": { "status": "pending", @@ -810073,15 +813816,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The corrected encrypted or additional protocol/profile workflow is unimplemented; selecting an erratum does not implement it.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -810434,15 +814177,41 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/session/session_test.go", + "name": "TestLogoutMarkersSurviveIdentifierChange" + } + ] }, "interop": { "status": "pending", @@ -810452,15 +814221,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The corrected encrypted or additional protocol/profile workflow is unimplemented; selecting an erratum does not implement it.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -810752,15 +814521,37 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Manage", + "saml/session.Service.BeginManage", + "saml/session.Service.Complete" + ], + "code": [ + "saml/session/session.go", + "saml/session/reply.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/exchange_test.go", + "name": "TestManageNameIDBothDirectionsAndBindings" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestManageAliasTerminationAndAtomicReplay" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestControlSigningFailureDoesNotCommitState" + } + ] }, "interop": { "status": "pending", @@ -810770,15 +814561,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The corrected encrypted or additional protocol/profile workflow is unimplemented; selecting an erratum does not implement it.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -811675,15 +815466,36 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Respond", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestInvalidArtifactReturnsEmptySuccess" + } + ] }, "interop": { "status": "pending", @@ -811693,15 +815505,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The corrected encrypted or additional protocol/profile workflow is unimplemented; selecting an erratum does not implement it.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -811724,15 +815536,36 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Respond", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestInvalidArtifactReturnsEmptySuccess" + } + ] }, "interop": { "status": "pending", @@ -811742,15 +815575,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The corrected encrypted or additional protocol/profile workflow is unimplemented; selecting an erratum does not implement it.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -811882,71 +815715,45 @@ "status": "applicable", "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, - "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] - }, - "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] - }, - "interop": { - "status": "pending", - "tests": [], - "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." - }, - "policy": { - "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." - }, - "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." - } - }, - "gap": "The corrected encrypted or additional protocol/profile workflow is unimplemented; selecting an erratum does not implement it.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." - }, - { - "id": "N-SAML-SP-ERRATUM-SAMLBIND-E24", - "statement": "Apply Approved Errata 05 E24 to the affected base-document contract: HTTPS in URI Binding.", - "norm": { - "document": "SAMLBIND", - "clause": "3.7", - "level": "normative", - "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-bindings-2.0-os.pdf", - "errata": [ - "E24" - ] - }, - "applies": { - "profile": "saml-sp", - "role": "SAML SP", - "mode": "websso", - "status": "applicable", - "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." - }, "implementation": { "status": "limited", "apis": [ - "saml/sp.NewServiceProvider", - "saml/sp.ServiceProvider.Begin", - "saml/sp.ServiceProvider.ConsumePOST" + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" ], "code": [ - "saml/sp/sp.go" + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -811957,26 +815764,81 @@ "policy": { "default": { "status": "enabled", - "reason": "Available within the explicitly documented bounded API and its algorithm/resource/trust settings." + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "not-applicable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The correction is selected for this base document and role, but complete corrected-clause implementation and independent evidence remain open.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { - "id": "N-SAML-SP-ERRATUM-SAMLBIND-E31", - "statement": "Apply Approved Errata 05 E31 to the affected base-document contract: Various Minor Errors in Binding.", + "id": "N-SAML-SP-ERRATUM-SAMLBIND-E24", + "statement": "Apply Approved Errata 05 E24 to the affected base-document contract: HTTPS in URI Binding.", "norm": { "document": "SAMLBIND", - "clause": "3.3.5", + "clause": "3.7", "level": "normative", "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-bindings-2.0-os.pdf", "errata": [ - "E31" + "E24" + ] + }, + "applies": { + "profile": "saml-sp", + "role": "SAML SP", + "mode": "websso", + "status": "applicable", + "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." + }, + "implementation": { + "status": "limited", + "apis": [ + "saml/sp.NewServiceProvider", + "saml/sp.ServiceProvider.Begin", + "saml/sp.ServiceProvider.ConsumePOST" + ], + "code": [ + "saml/sp/sp.go" + ] + }, + "tests": { + "status": "pending", + "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", + "positive": [], + "negative": [] + }, + "interop": { + "status": "pending", + "tests": [], + "fixtures": [], + "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + }, + "policy": { + "default": { + "status": "enabled", + "reason": "Available within the explicitly documented bounded API and its algorithm/resource/trust settings." + }, + "compatibility": { + "status": "not-applicable", + "reason": "No general compatibility switch supplies missing normative behavior." + } + }, + "gap": "The correction is selected for this base document and role, but complete corrected-clause implementation and independent evidence remain open.", + "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + }, + { + "id": "N-SAML-SP-ERRATUM-SAMLBIND-E31", + "statement": "Apply Approved Errata 05 E31 to the affected base-document contract: Various Minor Errors in Binding.", + "norm": { + "document": "SAMLBIND", + "clause": "3.3.5", + "level": "normative", + "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-bindings-2.0-os.pdf", + "errata": [ + "E31" ] }, "applies": { @@ -812097,15 +815959,36 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Respond", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestInvalidArtifactReturnsEmptySuccess" + } + ] }, "interop": { "status": "pending", @@ -812115,15 +815998,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The corrected encrypted or additional protocol/profile workflow is unimplemented; selecting an erratum does not implement it.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -812421,15 +816304,44 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -812439,15 +816351,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The corrected encrypted or additional protocol/profile workflow is unimplemented; selecting an erratum does not implement it.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -812470,15 +816382,44 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -812488,15 +816429,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The corrected encrypted or additional protocol/profile workflow is unimplemented; selecting an erratum does not implement it.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -812519,15 +816460,44 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -812537,15 +816507,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The corrected encrypted or additional protocol/profile workflow is unimplemented; selecting an erratum does not implement it.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -812622,71 +816592,45 @@ "status": "applicable", "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, - "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] - }, - "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] - }, - "interop": { - "status": "pending", - "tests": [], - "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." - }, - "policy": { - "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." - }, - "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." - } - }, - "gap": "The corrected encrypted or additional protocol/profile workflow is unimplemented; selecting an erratum does not implement it.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." - }, - { - "id": "N-SAML-SP-ERRATUM-SAMLPROF-E32", - "statement": "Apply Approved Errata 05 E32 to the affected base-document contract: Missing Required Information in Profiles.", - "norm": { - "document": "SAMLPROF", - "clause": "4.3.1", - "level": "normative", - "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-profiles-2.0-os.pdf", - "errata": [ - "E32" - ] - }, - "applies": { - "profile": "saml-sp", - "role": "SAML SP", - "mode": "websso", - "status": "applicable", - "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." - }, "implementation": { "status": "limited", "apis": [ - "saml/sp.NewServiceProvider", - "saml/sp.ServiceProvider.Begin", - "saml/sp.ServiceProvider.ConsumePOST" + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" ], "code": [ - "saml/sp/sp.go" + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -812697,26 +816641,26 @@ "policy": { "default": { "status": "enabled", - "reason": "Available within the explicitly documented bounded API and its algorithm/resource/trust settings." + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "not-applicable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The correction is selected for this base document and role, but complete corrected-clause implementation and independent evidence remain open.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { - "id": "N-SAML-SP-ERRATUM-SAMLPROF-E35", - "statement": "Apply Approved Errata 05 E35 to the affected base-document contract: Response Consumer URL Rules and Example.", + "id": "N-SAML-SP-ERRATUM-SAMLPROF-E32", + "statement": "Apply Approved Errata 05 E32 to the affected base-document contract: Missing Required Information in Profiles.", "norm": { "document": "SAMLPROF", - "clause": "4.2.4.1", + "clause": "4.3.1", "level": "normative", "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-profiles-2.0-os.pdf", "errata": [ - "E35" + "E32" ] }, "applies": { @@ -812763,28 +816707,34 @@ "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { - "id": "N-SAML-SP-ERRATUM-SAMLPROF-E38", - "statement": "Apply Approved Errata 05 E38 to the affected base-document contract: Clarification Regarding Index on .", + "id": "N-SAML-SP-ERRATUM-SAMLPROF-E35", + "statement": "Apply Approved Errata 05 E35 to the affected base-document contract: Response Consumer URL Rules and Example.", "norm": { "document": "SAMLPROF", - "clause": "4.4.4.1", + "clause": "4.2.4.1", "level": "normative", "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-profiles-2.0-os.pdf", "errata": [ - "E38" + "E35" ] }, "applies": { "profile": "saml-sp", "role": "SAML SP", - "mode": "logout", + "mode": "websso", "status": "applicable", "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/sp.NewServiceProvider", + "saml/sp.ServiceProvider.Begin", + "saml/sp.ServiceProvider.ConsumePOST" + ], + "code": [ + "saml/sp/sp.go" + ] }, "tests": { "status": "pending", @@ -812800,15 +816750,90 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Available within the explicitly documented bounded API and its algorithm/resource/trust settings." }, "compatibility": { - "status": "unavailable", + "status": "not-applicable", "reason": "No general compatibility switch supplies missing normative behavior." } }, - "gap": "The corrected encrypted or additional protocol/profile workflow is unimplemented; selecting an erratum does not implement it.", + "gap": "The correction is selected for this base document and role, but complete corrected-clause implementation and independent evidence remain open.", + "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + }, + { + "id": "N-SAML-SP-ERRATUM-SAMLPROF-E38", + "statement": "Apply Approved Errata 05 E38 to the affected base-document contract: Clarification Regarding Index on .", + "norm": { + "document": "SAMLPROF", + "clause": "4.4.4.1", + "level": "normative", + "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-profiles-2.0-os.pdf", + "errata": [ + "E38" + ] + }, + "applies": { + "profile": "saml-sp", + "role": "SAML SP", + "mode": "logout", + "status": "applicable", + "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." + }, + "implementation": { + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] + }, + "tests": { + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/session/session_test.go", + "name": "TestLogoutMarkersSurviveIdentifierChange" + } + ] + }, + "interop": { + "status": "pending", + "tests": [], + "fixtures": [], + "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + }, + "policy": { + "default": { + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." + }, + "compatibility": { + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." + } + }, + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -813253,15 +817278,44 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -813271,15 +817325,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The corrected encrypted or additional protocol/profile workflow is unimplemented; selecting an erratum does not implement it.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -813302,15 +817356,37 @@ "reason": "Applies to this selected SAML SP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Manage", + "saml/session.Service.BeginManage", + "saml/session.Service.Complete" + ], + "code": [ + "saml/session/session.go", + "saml/session/reply.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/exchange_test.go", + "name": "TestManageNameIDBothDirectionsAndBindings" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestManageAliasTerminationAndAtomicReplay" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestControlSigningFailureDoesNotCommitState" + } + ] }, "interop": { "status": "pending", @@ -813320,15 +817396,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The corrected encrypted or additional protocol/profile workflow is unimplemented; selecting an erratum does not implement it.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -816625,33 +820701,62 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Issue", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Executable positive/negative tests cover the implemented 2005/Approved Errata 05 workflow, exact peer/destination/correlation checks and the named state boundaries. This does not certify the complete SAML role or every extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestAuthenticatedArtifactSOAP" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/binding/artifact_test.go", + "name": "TestArtifactWireAndHTTP" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_control_test.go", + "name": "TestXMLSecSAMLControlSOAP" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Native xmlsec1 verifies and independently regenerates control-message SOAP signatures with Exclusive and Inclusive C14N. Full independent SAML-server/profile exchanges are not established by that component evidence." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs use locally trusted keys/metadata, authenticated messages, bounded parsing and atomic application-owned state. No received URL or XML KeyInfo adds trust." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy signature/encryption algorithms require the existing explicit algorithm policy; compatibility never bypasses peer, correlation or replay checks." } }, - "gap": "HTTP Artifact response delivery/consumption is unimplemented.", + "gap": "Complete independent obligation-level evidence and a deployed external HoK IdP exchange remain open. ECP CS01 2.3.11 XML-proof interpretation is unresolved; see docs/saml.md#ecp-2013-holder-of-key.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -816672,33 +820777,62 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Issue", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Executable positive/negative tests cover the implemented 2005/Approved Errata 05 workflow, exact peer/destination/correlation checks and the named state boundaries. This does not certify the complete SAML role or every extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestAuthenticatedArtifactSOAP" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/binding/artifact_test.go", + "name": "TestArtifactWireAndHTTP" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_control_test.go", + "name": "TestXMLSecSAMLControlSOAP" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Native xmlsec1 verifies and independently regenerates control-message SOAP signatures with Exclusive and Inclusive C14N. Full independent SAML-server/profile exchanges are not established by that component evidence." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs use locally trusted keys/metadata, authenticated messages, bounded parsing and atomic application-owned state. No received URL or XML KeyInfo adds trust." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy signature/encryption algorithms require the existing explicit algorithm policy; compatibility never bypasses peer, correlation or replay checks." } }, - "gap": "ArtifactResolve/ArtifactResponse exchange, authentication, single-use binding and SOAP errors are unimplemented.", + "gap": "Complete independent obligation-level evidence and a deployed external HoK IdP exchange remain open. ECP CS01 2.3.11 XML-proof interpretation is unresolved; see docs/saml.md#ecp-2013-holder-of-key.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -816719,33 +820853,61 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Executable positive/negative tests cover the implemented 2005/Approved Errata 05 workflow, exact peer/destination/correlation checks and the named state boundaries. This does not certify the complete SAML role or every extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_control_test.go", + "name": "TestXMLSecSAMLControlSOAP" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Native xmlsec1 verifies and independently regenerates control-message SOAP signatures with Exclusive and Inclusive C14N. Full independent SAML-server/profile exchanges are not established by that component evidence." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs use locally trusted keys/metadata, authenticated messages, bounded parsing and atomic application-owned state. No received URL or XML KeyInfo adds trust." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy signature/encryption algorithms require the existing explicit algorithm policy; compatibility never bypasses peer, correlation or replay checks." } }, - "gap": "SP/IdP ECP and PAOS message/header/security handling are unimplemented; metadata constants do not implement the binding.", + "gap": "Complete independent obligation-level evidence and a deployed external HoK IdP exchange remain open. ECP CS01 2.3.11 XML-proof interpretation is unresolved; see docs/saml.md#ecp-2013-holder-of-key.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -816768,33 +820930,63 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.BeginManage", + "saml/session.Service.Manage", + "saml/session.Service.Complete" + ], + "code": [ + "saml/session/session.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Executable positive/negative tests cover the implemented 2005/Approved Errata 05 workflow, exact peer/destination/correlation checks and the named state boundaries. This does not certify the complete SAML role or every extension.", + "positive": [ + { + "file": "saml/session/exchange_test.go", + "name": "TestManageNameIDBothDirectionsAndBindings" + }, + { + "file": "saml/session/soap_test.go", + "name": "TestSOAPBackChannelManagementAndLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestManageAliasTerminationAndAtomicReplay" + }, + { + "file": "saml/protocol/control_test.go", + "name": "TestControlIndependentWire" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_control_test.go", + "name": "TestXMLSecSAMLControlSOAP" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Native xmlsec1 verifies and independently regenerates control-message SOAP signatures with Exclusive and Inclusive C14N. Full independent SAML-server/profile exchanges are not established by that component evidence." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs use locally trusted keys/metadata, authenticated messages, bounded parsing and atomic application-owned state. No received URL or XML KeyInfo adds trust." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy signature/encryption algorithms require the existing explicit algorithm policy; compatibility never bypasses peer, correlation or replay checks." } }, - "gap": "ManageNameID request/response, partner-state change and identifier termination are unimplemented.", + "gap": "Complete independent obligation-level evidence and a deployed external HoK IdP exchange remain open. ECP CS01 2.3.11 XML-proof interpretation is unresolved; see docs/saml.md#ecp-2013-holder-of-key.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -816817,33 +821009,63 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.BeginManage", + "saml/session.Service.Manage", + "saml/session.Service.Complete" + ], + "code": [ + "saml/session/session.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Executable positive/negative tests cover the implemented 2005/Approved Errata 05 workflow, exact peer/destination/correlation checks and the named state boundaries. This does not certify the complete SAML role or every extension.", + "positive": [ + { + "file": "saml/session/exchange_test.go", + "name": "TestManageNameIDBothDirectionsAndBindings" + }, + { + "file": "saml/session/soap_test.go", + "name": "TestSOAPBackChannelManagementAndLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestManageAliasTerminationAndAtomicReplay" + }, + { + "file": "saml/protocol/control_test.go", + "name": "TestControlIndependentWire" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_control_test.go", + "name": "TestXMLSecSAMLControlSOAP" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Native xmlsec1 verifies and independently regenerates control-message SOAP signatures with Exclusive and Inclusive C14N. Full independent SAML-server/profile exchanges are not established by that component evidence." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs use locally trusted keys/metadata, authenticated messages, bounded parsing and atomic application-owned state. No received URL or XML KeyInfo adds trust." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy signature/encryption algorithms require the existing explicit algorithm policy; compatibility never bypasses peer, correlation or replay checks." } }, - "gap": "ManageNameID request/response, partner-state change and identifier termination are unimplemented.", + "gap": "Complete independent obligation-level evidence and a deployed external HoK IdP exchange remain open. ECP CS01 2.3.11 XML-proof interpretation is unresolved; see docs/saml.md#ecp-2013-holder-of-key.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -816866,33 +821088,63 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.BeginManage", + "saml/session.Service.Manage", + "saml/session.Service.Complete" + ], + "code": [ + "saml/session/session.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Executable positive/negative tests cover the implemented 2005/Approved Errata 05 workflow, exact peer/destination/correlation checks and the named state boundaries. This does not certify the complete SAML role or every extension.", + "positive": [ + { + "file": "saml/session/exchange_test.go", + "name": "TestManageNameIDBothDirectionsAndBindings" + }, + { + "file": "saml/session/soap_test.go", + "name": "TestSOAPBackChannelManagementAndLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestManageAliasTerminationAndAtomicReplay" + }, + { + "file": "saml/protocol/control_test.go", + "name": "TestControlIndependentWire" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_control_test.go", + "name": "TestXMLSecSAMLControlSOAP" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Native xmlsec1 verifies and independently regenerates control-message SOAP signatures with Exclusive and Inclusive C14N. Full independent SAML-server/profile exchanges are not established by that component evidence." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs use locally trusted keys/metadata, authenticated messages, bounded parsing and atomic application-owned state. No received URL or XML KeyInfo adds trust." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy signature/encryption algorithms require the existing explicit algorithm policy; compatibility never bypasses peer, correlation or replay checks." } }, - "gap": "SOAP Name Identifier Management is unimplemented; it is mandatory for full IdP and optional for ordinary SP.", + "gap": "Complete independent obligation-level evidence and a deployed external HoK IdP exchange remain open. ECP CS01 2.3.11 XML-proof interpretation is unresolved; see docs/saml.md#ecp-2013-holder-of-key.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -816915,33 +821167,63 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.BeginManage", + "saml/session.Service.Manage", + "saml/session.Service.Complete" + ], + "code": [ + "saml/session/session.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Executable positive/negative tests cover the implemented 2005/Approved Errata 05 workflow, exact peer/destination/correlation checks and the named state boundaries. This does not certify the complete SAML role or every extension.", + "positive": [ + { + "file": "saml/session/exchange_test.go", + "name": "TestManageNameIDBothDirectionsAndBindings" + }, + { + "file": "saml/session/soap_test.go", + "name": "TestSOAPBackChannelManagementAndLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestManageAliasTerminationAndAtomicReplay" + }, + { + "file": "saml/protocol/control_test.go", + "name": "TestControlIndependentWire" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_control_test.go", + "name": "TestXMLSecSAMLControlSOAP" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Native xmlsec1 verifies and independently regenerates control-message SOAP signatures with Exclusive and Inclusive C14N. Full independent SAML-server/profile exchanges are not established by that component evidence." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs use locally trusted keys/metadata, authenticated messages, bounded parsing and atomic application-owned state. No received URL or XML KeyInfo adds trust." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy signature/encryption algorithms require the existing explicit algorithm policy; compatibility never bypasses peer, correlation or replay checks." } }, - "gap": "SOAP Name Identifier Management is unimplemented; it is mandatory for full IdP and optional for ordinary SP.", + "gap": "Complete independent obligation-level evidence and a deployed external HoK IdP exchange remain open. ECP CS01 2.3.11 XML-proof interpretation is unresolved; see docs/saml.md#ecp-2013-holder-of-key.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -816962,33 +821244,68 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Executable positive/negative tests cover the implemented 2005/Approved Errata 05 workflow, exact peer/destination/correlation checks and the named state boundaries. This does not certify the complete SAML role or every extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + }, + { + "file": "saml/session/soap_test.go", + "name": "TestSOAPBackChannelManagementAndLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/protocol/control_test.go", + "name": "TestControlMessages" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_control_test.go", + "name": "TestXMLSecSAMLControlSOAP" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Native xmlsec1 verifies and independently regenerates control-message SOAP signatures with Exclusive and Inclusive C14N. Full independent SAML-server/profile exchanges are not established by that component evidence." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs use locally trusted keys/metadata, authenticated messages, bounded parsing and atomic application-owned state. No received URL or XML KeyInfo adds trust." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy signature/encryption algorithms require the existing explicit algorithm policy; compatibility never bypasses peer, correlation or replay checks." } }, - "gap": "Logout messages, session participant/authority correlation and propagation are unimplemented.", + "gap": "Complete independent obligation-level evidence and a deployed external HoK IdP exchange remain open. ECP CS01 2.3.11 XML-proof interpretation is unresolved; see docs/saml.md#ecp-2013-holder-of-key.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -817009,33 +821326,68 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Executable positive/negative tests cover the implemented 2005/Approved Errata 05 workflow, exact peer/destination/correlation checks and the named state boundaries. This does not certify the complete SAML role or every extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + }, + { + "file": "saml/session/soap_test.go", + "name": "TestSOAPBackChannelManagementAndLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/protocol/control_test.go", + "name": "TestControlMessages" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_control_test.go", + "name": "TestXMLSecSAMLControlSOAP" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Native xmlsec1 verifies and independently regenerates control-message SOAP signatures with Exclusive and Inclusive C14N. Full independent SAML-server/profile exchanges are not established by that component evidence." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs use locally trusted keys/metadata, authenticated messages, bounded parsing and atomic application-owned state. No received URL or XML KeyInfo adds trust." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy signature/encryption algorithms require the existing explicit algorithm policy; compatibility never bypasses peer, correlation or replay checks." } }, - "gap": "Logout messages, session participant/authority correlation and propagation are unimplemented.", + "gap": "Complete independent obligation-level evidence and a deployed external HoK IdP exchange remain open. ECP CS01 2.3.11 XML-proof interpretation is unresolved; see docs/saml.md#ecp-2013-holder-of-key.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -817056,33 +821408,68 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Executable positive/negative tests cover the implemented 2005/Approved Errata 05 workflow, exact peer/destination/correlation checks and the named state boundaries. This does not certify the complete SAML role or every extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + }, + { + "file": "saml/session/soap_test.go", + "name": "TestSOAPBackChannelManagementAndLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/protocol/control_test.go", + "name": "TestControlMessages" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_control_test.go", + "name": "TestXMLSecSAMLControlSOAP" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Native xmlsec1 verifies and independently regenerates control-message SOAP signatures with Exclusive and Inclusive C14N. Full independent SAML-server/profile exchanges are not established by that component evidence." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs use locally trusted keys/metadata, authenticated messages, bounded parsing and atomic application-owned state. No received URL or XML KeyInfo adds trust." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy signature/encryption algorithms require the existing explicit algorithm policy; compatibility never bypasses peer, correlation or replay checks." } }, - "gap": "SOAP Single Logout is unimplemented; unlike Lite roles it is mandatory for the selected full role.", + "gap": "Complete independent obligation-level evidence and a deployed external HoK IdP exchange remain open. ECP CS01 2.3.11 XML-proof interpretation is unresolved; see docs/saml.md#ecp-2013-holder-of-key.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -817103,33 +821490,68 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Executable positive/negative tests cover the implemented 2005/Approved Errata 05 workflow, exact peer/destination/correlation checks and the named state boundaries. This does not certify the complete SAML role or every extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + }, + { + "file": "saml/session/soap_test.go", + "name": "TestSOAPBackChannelManagementAndLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/protocol/control_test.go", + "name": "TestControlMessages" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_control_test.go", + "name": "TestXMLSecSAMLControlSOAP" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Native xmlsec1 verifies and independently regenerates control-message SOAP signatures with Exclusive and Inclusive C14N. Full independent SAML-server/profile exchanges are not established by that component evidence." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs use locally trusted keys/metadata, authenticated messages, bounded parsing and atomic application-owned state. No received URL or XML KeyInfo adds trust." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy signature/encryption algorithms require the existing explicit algorithm policy; compatibility never bypasses peer, correlation or replay checks." } }, - "gap": "SOAP Single Logout is unimplemented; unlike Lite roles it is mandatory for the selected full role.", + "gap": "Complete independent obligation-level evidence and a deployed external HoK IdP exchange remain open. ECP CS01 2.3.11 XML-proof interpretation is unresolved; see docs/saml.md#ecp-2013-holder-of-key.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -817348,33 +821770,68 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignNameIDMappingRequest", + "saml/protocol.VerifyNameIDMappingRequest", + "saml/protocol.SignNameIDMappingResponse", + "saml/protocol.VerifyNameIDMappingResponse", + "saml/authority.NewService" + ], + "code": [ + "saml/protocol/mapping.go", + "saml/authority/authority.go", + "saml/authority/client.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "SOAP mapping, encrypted input/output, explicit target policy, status/correlation failures and limited-use assertions now have executable tests.", + "positive": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/authority/authority_test.go", + "name": "TestAuthoritySOAPAndURI" + } + ], + "negative": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/protocol/mapping_test.go", + "name": "TestLimitedUseMappedIdentifier" + } + ] }, "interop": { "status": "pending", - "tests": [], - "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "tests": [ + { + "file": "tests/interop/saml_authority_test.go", + "name": "TestOpenSAMLAuthorityContracts" + } + ], + "fixtures": [ + "tests/interop/testdata/OpenSAMLAuthorityOracle.java" + ], + "reason": "OpenSAML 5.2.3 generates and validates NameID Mapping messages and signatures. Full Extended-role proxying and all optional cipher/identifier forms are separate." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicitly configured SOAP mapping with trusted peers, authorization and replay state." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Existing explicit legacy policies apply; plaintext mapping requires a deployment-specific exception." } }, - "gap": "The optional Extended role and its NameIDMapping SOAP exchange are unimplemented.", + "gap": "NameID Mapping SOAP is implemented and tested; arbitrary BaseID types, automatic Extended-role proxying and full independent role conformance remain outside this record.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -817395,34 +821852,69 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", + "saml/sp.NewServiceProvider", + "saml/idp.NewIdentityProvider" + ], + "code": [ + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "All seven encryption target combinations, required-encryption downgrade rejection, authenticated failure order, key rotation/budgets, optional Type and sibling keys are tested within solicited WebSSO.", + "positive": [ + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedResponseTargets" + }, + { + "file": "saml/sp/encryption_test.go", + "name": "TestEncryptedWebSSO" + } + ], + "negative": [ + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedResponsePoliciesAndFailureOrder" + }, + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedCBCAndUnsignedAssertion" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "forbidden", + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "Encrypted SAML id production/consumption is unimplemented in the orchestration/protocol API; lower-level XML Encryption availability does not provide this role workflow.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Implemented for the current NameID/string-attribute solicited WebSSO profile using explicitly selected RSA transports, inline/sibling EncryptedKey alternatives and no external key/cipher I/O. General SAML extensions, other key-management profiles and complete independent malformed-input coverage are not claimed.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-IDP-ENCRYPTED-ASSERTION", @@ -817442,34 +821934,69 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", + "saml/sp.NewServiceProvider", + "saml/idp.NewIdentityProvider" + ], + "code": [ + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "All seven encryption target combinations, required-encryption downgrade rejection, authenticated failure order, key rotation/budgets, optional Type and sibling keys are tested within solicited WebSSO.", + "positive": [ + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedResponseTargets" + }, + { + "file": "saml/sp/encryption_test.go", + "name": "TestEncryptedWebSSO" + } + ], + "negative": [ + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedResponsePoliciesAndFailureOrder" + }, + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedCBCAndUnsignedAssertion" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "forbidden", + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "Encrypted SAML assertion production/consumption is unimplemented in the orchestration/protocol API; lower-level XML Encryption availability does not provide this role workflow.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Implemented for the current NameID/string-attribute solicited WebSSO profile using explicitly selected RSA transports, inline/sibling EncryptedKey alternatives and no external key/cipher I/O. General SAML extensions, other key-management profiles and complete independent malformed-input coverage are not claimed.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-IDP-ENCRYPTED-ATTRIBUTE", @@ -817489,34 +822016,69 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", + "saml/sp.NewServiceProvider", + "saml/idp.NewIdentityProvider" + ], + "code": [ + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "All seven encryption target combinations, required-encryption downgrade rejection, authenticated failure order, key rotation/budgets, optional Type and sibling keys are tested within solicited WebSSO.", + "positive": [ + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedResponseTargets" + }, + { + "file": "saml/sp/encryption_test.go", + "name": "TestEncryptedWebSSO" + } + ], + "negative": [ + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedResponsePoliciesAndFailureOrder" + }, + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedCBCAndUnsignedAssertion" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "forbidden", + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "Encrypted SAML attribute production/consumption is unimplemented in the orchestration/protocol API; lower-level XML Encryption availability does not provide this role workflow.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Implemented for the current NameID/string-attribute solicited WebSSO profile using explicitly selected RSA transports, inline/sibling EncryptedKey alternatives and no external key/cipher I/O. General SAML extensions, other key-management profiles and complete independent malformed-input coverage are not claimed.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-IDP-IDENTIFIERS", @@ -817696,34 +822258,69 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", + "saml/sp.NewServiceProvider", + "saml/idp.NewIdentityProvider" + ], + "code": [ + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Explicit algorithm permission, generation/consumption and secure-default rejection are tested at protocol level. HMAC uses explicit shared-secret credentials rather than metadata certificates.", + "positive": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ], + "negative": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "forbidden", + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "The SAML API does not expose this required/recommended algorithm workflow. Generic XMLDSig/XMLENC compatibility policies do not change the closed SAML signature/encryption policy.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Protocol-level compatibility workflows exist with explicit SHA-1/HMAC/CBC/RSA15 policies. SP/IdP metadata helpers use public-key credentials; full role and every independent algorithm/context combination remain unverified.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-IDP-ALGORITHM-HMACSHA1", @@ -817746,34 +822343,69 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", + "saml/sp.NewServiceProvider", + "saml/idp.NewIdentityProvider" + ], + "code": [ + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Explicit algorithm permission, generation/consumption and secure-default rejection are tested at protocol level. HMAC uses explicit shared-secret credentials rather than metadata certificates.", + "positive": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ], + "negative": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "forbidden", + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "The SAML API does not expose this required/recommended algorithm workflow. Generic XMLDSig/XMLENC compatibility policies do not change the closed SAML signature/encryption policy.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Protocol-level compatibility workflows exist with explicit SHA-1/HMAC/CBC/RSA15 policies. SP/IdP metadata helpers use public-key credentials; full role and every independent algorithm/context combination remain unverified.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-IDP-ALGORITHM-C14N10", @@ -817798,38 +822430,67 @@ "implementation": { "status": "limited", "apis": [ - "saml/idp.NewIdentityProvider", - "saml/idp.IdentityProvider.AcceptRedirect", - "saml/idp.IdentityProvider.Issue" + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", + "saml/sp.NewServiceProvider", + "saml/idp.NewIdentityProvider" ], "code": [ + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", "saml/idp/idp.go" ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Explicit algorithm permission, generation/consumption and secure-default rejection are tested at protocol level. HMAC uses explicit shared-secret credentials rather than metadata certificates.", + "positive": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ], + "negative": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { - "status": "enabled", - "reason": "Available within the explicitly documented bounded API and its algorithm/resource/trust settings." + "status": "forbidden", + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "not-applicable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "The underlying transform exists; complete SAML mode and independent algorithm evidence remain open.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Protocol-level compatibility workflows exist with explicit SHA-1/HMAC/CBC/RSA15 policies. SP/IdP metadata helpers use public-key credentials; full role and every independent algorithm/context combination remain unverified.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-IDP-ALGORITHM-ENVELOPED", @@ -817854,38 +822515,67 @@ "implementation": { "status": "limited", "apis": [ - "saml/idp.NewIdentityProvider", - "saml/idp.IdentityProvider.AcceptRedirect", - "saml/idp.IdentityProvider.Issue" + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", + "saml/sp.NewServiceProvider", + "saml/idp.NewIdentityProvider" ], "code": [ + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", "saml/idp/idp.go" ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Explicit algorithm permission, generation/consumption and secure-default rejection are tested at protocol level. HMAC uses explicit shared-secret credentials rather than metadata certificates.", + "positive": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ], + "negative": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { - "status": "enabled", - "reason": "Available within the explicitly documented bounded API and its algorithm/resource/trust settings." + "status": "forbidden", + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "not-applicable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "The underlying transform exists; complete SAML mode and independent algorithm evidence remain open.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Protocol-level compatibility workflows exist with explicit SHA-1/HMAC/CBC/RSA15 policies. SP/IdP metadata helpers use public-key credentials; full role and every independent algorithm/context combination remain unverified.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-IDP-ALGORITHM-RSA-SHA1", @@ -817908,34 +822598,69 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", + "saml/sp.NewServiceProvider", + "saml/idp.NewIdentityProvider" + ], + "code": [ + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Explicit algorithm permission, generation/consumption and secure-default rejection are tested at protocol level. HMAC uses explicit shared-secret credentials rather than metadata certificates.", + "positive": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ], + "negative": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "forbidden", + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "The SAML API does not expose this required/recommended algorithm workflow. Generic XMLDSig/XMLENC compatibility policies do not change the closed SAML signature/encryption policy.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Protocol-level compatibility workflows exist with explicit SHA-1/HMAC/CBC/RSA15 policies. SP/IdP metadata helpers use public-key credentials; full role and every independent algorithm/context combination remain unverified.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-IDP-ALGORITHM-DSA-SHA1", @@ -818008,34 +822733,69 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", + "saml/sp.NewServiceProvider", + "saml/idp.NewIdentityProvider" + ], + "code": [ + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Explicit algorithm permission, generation/consumption and secure-default rejection are tested at protocol level. HMAC uses explicit shared-secret credentials rather than metadata certificates.", + "positive": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ], + "negative": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "forbidden", + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "The SAML API does not expose this required/recommended algorithm workflow. Generic XMLDSig/XMLENC compatibility policies do not change the closed SAML signature/encryption policy.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Protocol-level compatibility workflows exist with explicit SHA-1/HMAC/CBC/RSA15 policies. SP/IdP metadata helpers use public-key credentials; full role and every independent algorithm/context combination remain unverified.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-IDP-ALGORITHM-AES128-CBC", @@ -818058,34 +822818,69 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", + "saml/sp.NewServiceProvider", + "saml/idp.NewIdentityProvider" + ], + "code": [ + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Explicit algorithm permission, generation/consumption and secure-default rejection are tested at protocol level. HMAC uses explicit shared-secret credentials rather than metadata certificates.", + "positive": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ], + "negative": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "forbidden", + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "The SAML API does not expose this required/recommended algorithm workflow. Generic XMLDSig/XMLENC compatibility policies do not change the closed SAML signature/encryption policy.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Protocol-level compatibility workflows exist with explicit SHA-1/HMAC/CBC/RSA15 policies. SP/IdP metadata helpers use public-key credentials; full role and every independent algorithm/context combination remain unverified.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-IDP-ALGORITHM-AES256-CBC", @@ -818108,34 +822903,69 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", + "saml/sp.NewServiceProvider", + "saml/idp.NewIdentityProvider" + ], + "code": [ + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Explicit algorithm permission, generation/consumption and secure-default rejection are tested at protocol level. HMAC uses explicit shared-secret credentials rather than metadata certificates.", + "positive": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ], + "negative": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "forbidden", + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "The SAML API does not expose this required/recommended algorithm workflow. Generic XMLDSig/XMLENC compatibility policies do not change the closed SAML signature/encryption policy.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Protocol-level compatibility workflows exist with explicit SHA-1/HMAC/CBC/RSA15 policies. SP/IdP metadata helpers use public-key credentials; full role and every independent algorithm/context combination remain unverified.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-IDP-ALGORITHM-RSA15", @@ -818158,34 +822988,69 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", + "saml/sp.NewServiceProvider", + "saml/idp.NewIdentityProvider" + ], + "code": [ + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Explicit algorithm permission, generation/consumption and secure-default rejection are tested at protocol level. HMAC uses explicit shared-secret credentials rather than metadata certificates.", + "positive": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ], + "negative": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "forbidden", + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "The SAML API does not expose this required/recommended algorithm workflow. Generic XMLDSig/XMLENC compatibility policies do not change the closed SAML signature/encryption policy.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Protocol-level compatibility workflows exist with explicit SHA-1/HMAC/CBC/RSA15 policies. SP/IdP metadata helpers use public-key credentials; full role and every independent algorithm/context combination remain unverified.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-IDP-ALGORITHM-RSA-OAEP", @@ -818208,34 +823073,69 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", + "saml/sp.NewServiceProvider", + "saml/idp.NewIdentityProvider" + ], + "code": [ + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Explicit algorithm permission, generation/consumption and secure-default rejection are tested at protocol level. HMAC uses explicit shared-secret credentials rather than metadata certificates.", + "positive": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ], + "negative": [ + { + "file": "saml/protocol/legacy_test.go", + "name": "TestLegacySAMLAlgorithms" + }, + { + "file": "saml/protocol/legacy_test.go", + "name": "TestSAMLSharedSecretAndCanonicalXML" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "forbidden", + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "The SAML API does not expose this required/recommended algorithm workflow. Generic XMLDSig/XMLENC compatibility policies do not change the closed SAML signature/encryption policy.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Protocol-level compatibility workflows exist with explicit SHA-1/HMAC/CBC/RSA15 policies. SP/IdP metadata helpers use public-key credentials; full role and every independent algorithm/context combination remain unverified.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-IDP-TRANSPORT-SOAP-AUTH", @@ -818257,33 +823157,63 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/binding.DecodeSOAP", + "saml/binding.SOAPClient.RoundTrip", + "saml/protocol.SignSOAP" + ], + "code": [ + "saml/binding/soap.go", + "saml/protocol/soap.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Executable positive/negative tests cover the implemented 2005/Approved Errata 05 workflow, exact peer/destination/correlation checks and the named state boundaries. This does not certify the complete SAML role or every extension.", + "positive": [ + { + "file": "saml/binding/soap_test.go", + "name": "TestSOAPHTTPStatusAndRedirects" + }, + { + "file": "saml/protocol/control_test.go", + "name": "TestControlResponseAndSOAPCanonicalization" + } + ], + "negative": [ + { + "file": "saml/binding/soap_test.go", + "name": "TestSOAPIndependentEnvelopes" + }, + { + "file": "saml/binding/soap_test.go", + "name": "TestSOAPHTTPStatusAndRedirects" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_control_test.go", + "name": "TestXMLSecSAMLControlSOAP" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Native xmlsec1 verifies and independently regenerates control-message SOAP signatures with Exclusive and Inclusive C14N. Full independent SAML-server/profile exchanges are not established by that component evidence." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs use locally trusted keys/metadata, authenticated messages, bounded parsing and atomic application-owned state. No received URL or XML KeyInfo adds trust." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy signature/encryption algorithms require the existing explicit algorithm policy; compatibility never bypasses peer, correlation or replay checks." } }, - "gap": "No HTTP/SOAP/TLS transport implementation is supplied by this SAML library. Existing callbacks and modern deployment policy do not establish this edition-specific transport contract.", + "gap": "Complete independent obligation-level evidence and a deployed external HoK IdP exchange remain open. ECP CS01 2.3.11 XML-proof interpretation is unresolved; see docs/saml.md#ecp-2013-holder-of-key.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -818893,34 +823823,69 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", + "saml/sp.NewServiceProvider", + "saml/idp.NewIdentityProvider" + ], + "code": [ + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "All seven encryption target combinations, required-encryption downgrade rejection, authenticated failure order, key rotation/budgets, optional Type and sibling keys are tested within solicited WebSSO.", + "positive": [ + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedResponseTargets" + }, + { + "file": "saml/sp/encryption_test.go", + "name": "TestEncryptedWebSSO" + } + ], + "negative": [ + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedResponsePoliciesAndFailureOrder" + }, + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedCBCAndUnsignedAssertion" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "forbidden", + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "This assertion extension, encrypted representation or general content/processing form is unimplemented in the selected SAML API.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Implemented for the current NameID/string-attribute solicited WebSSO profile using explicitly selected RSA transports, inline/sibling EncryptedKey alternatives and no external key/cipher I/O. General SAML extensions, other key-management profiles and complete independent malformed-input coverage are not claimed.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-IDP-SAMLCORE-2-2-5", @@ -819144,34 +824109,69 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", + "saml/sp.NewServiceProvider", + "saml/idp.NewIdentityProvider" + ], + "code": [ + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "All seven encryption target combinations, required-encryption downgrade rejection, authenticated failure order, key rotation/budgets, optional Type and sibling keys are tested within solicited WebSSO.", + "positive": [ + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedResponseTargets" + }, + { + "file": "saml/sp/encryption_test.go", + "name": "TestEncryptedWebSSO" + } + ], + "negative": [ + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedResponsePoliciesAndFailureOrder" + }, + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedCBCAndUnsignedAssertion" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "forbidden", + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "This assertion extension, encrypted representation or general content/processing form is unimplemented in the selected SAML API.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Implemented for the current NameID/string-attribute solicited WebSSO profile using explicitly selected RSA transports, inline/sibling EncryptedKey alternatives and no external key/cipher I/O. General SAML extensions, other key-management profiles and complete independent malformed-input coverage are not claimed.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-IDP-SAMLCORE-2-4-1-1", @@ -819923,34 +824923,69 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", + "saml/sp.NewServiceProvider", + "saml/idp.NewIdentityProvider" + ], + "code": [ + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "All seven encryption target combinations, required-encryption downgrade rejection, authenticated failure order, key rotation/budgets, optional Type and sibling keys are tested within solicited WebSSO.", + "positive": [ + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedResponseTargets" + }, + { + "file": "saml/sp/encryption_test.go", + "name": "TestEncryptedWebSSO" + } + ], + "negative": [ + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedResponsePoliciesAndFailureOrder" + }, + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedCBCAndUnsignedAssertion" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "forbidden", + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "This assertion extension, encrypted representation or general content/processing form is unimplemented in the selected SAML API.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Implemented for the current NameID/string-attribute solicited WebSSO profile using explicitly selected RSA transports, inline/sibling EncryptedKey alternatives and no external key/cipher I/O. General SAML extensions, other key-management profiles and complete independent malformed-input coverage are not claimed.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-IDP-SAMLCORE-2-7-4-1", @@ -820813,17 +825848,29 @@ }, "tests": { "status": "partial", - "reason": "These checks establish the named bounded cases; they do not cover every legal branch of the full clause.", + "reason": "Solicited requests distinguish the encrypted policy marker from the inner NameID format and permit any supported inner format for unspecified policy. End-to-end tests cover encrypted-marker requests, missing producer/consumer encryption policy, metadata restrictions, downgrade rejection and replay state.", "positive": [ { "file": "saml/protocol/protocol_test.go", "name": "TestAuthnRequest" + }, + { + "file": "saml/sp/encryption_test.go", + "name": "TestEncryptedWebSSO" } ], "negative": [ { "file": "saml/protocol/protocol_test.go", "name": "TestAuthnRequest" + }, + { + "file": "saml/sp/encryption_test.go", + "name": "TestEncryptedWebSSO" + }, + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedNameIDIsAPolicyValue" } ] }, @@ -820843,7 +825890,7 @@ "reason": "No general compatibility switch supplies missing normative behavior." } }, - "gap": "Only the closed signed solicited WebSSO subset is implemented. Complete legal syntax/cardinality, optional content, role processing and independent clause-specific cases remain open.", + "gap": "The selected solicited WebSSO NameIDPolicy rules include encrypted and unspecified Format semantics. General Subject/affiliation/extensions, full role workflows and independent clause-specific NameIDPolicy exchanges remain unverified.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -820973,10 +826020,20 @@ ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "Signed POST and authenticated Redirect requests cover authorized ACS selection with an omitted ProtocolBinding, endpoint defaults and rejection of unknown endpoints, unsupported bindings and conflicting index/URL/binding attributes. The rest of the processing clause remains open.", + "positive": [ + { + "file": "saml/protocol/protocol_test.go", + "name": "TestAuthnRequestACSSelection" + } + ], + "negative": [ + { + "file": "saml/protocol/protocol_test.go", + "name": "TestAuthnRequestACSSelection" + } + ] }, "interop": { "status": "pending", @@ -821062,15 +826119,36 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Respond", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestInvalidArtifactReturnsEmptySuccess" + } + ] }, "interop": { "status": "pending", @@ -821080,15 +826158,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "Artifact Resolution protocol processing is unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -821109,15 +826187,36 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Respond", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestInvalidArtifactReturnsEmptySuccess" + } + ] }, "interop": { "status": "pending", @@ -821127,15 +826226,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "Artifact Resolution protocol processing is unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -821156,15 +826255,36 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Respond", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestInvalidArtifactReturnsEmptySuccess" + } + ] }, "interop": { "status": "pending", @@ -821174,15 +826294,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "Artifact Resolution protocol processing is unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -821205,15 +826325,37 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Manage", + "saml/session.Service.BeginManage", + "saml/session.Service.Complete" + ], + "code": [ + "saml/session/session.go", + "saml/session/reply.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/exchange_test.go", + "name": "TestManageNameIDBothDirectionsAndBindings" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestManageAliasTerminationAndAtomicReplay" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestControlSigningFailureDoesNotCommitState" + } + ] }, "interop": { "status": "pending", @@ -821223,15 +826365,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "Name Identifier Management protocol processing is unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -821254,15 +826396,37 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Manage", + "saml/session.Service.BeginManage", + "saml/session.Service.Complete" + ], + "code": [ + "saml/session/session.go", + "saml/session/reply.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/exchange_test.go", + "name": "TestManageNameIDBothDirectionsAndBindings" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestManageAliasTerminationAndAtomicReplay" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestControlSigningFailureDoesNotCommitState" + } + ] }, "interop": { "status": "pending", @@ -821272,15 +826436,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "Name Identifier Management protocol processing is unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -821305,15 +826469,37 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Manage", + "saml/session.Service.BeginManage", + "saml/session.Service.Complete" + ], + "code": [ + "saml/session/session.go", + "saml/session/reply.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/exchange_test.go", + "name": "TestManageNameIDBothDirectionsAndBindings" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestManageAliasTerminationAndAtomicReplay" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestControlSigningFailureDoesNotCommitState" + } + ] }, "interop": { "status": "pending", @@ -821323,15 +826509,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "Name Identifier Management protocol processing is unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -821355,15 +826541,41 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/session/session_test.go", + "name": "TestLogoutMarkersSurviveIdentifierChange" + } + ] }, "interop": { "status": "pending", @@ -821373,15 +826585,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "Single Logout protocol processing is unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -821402,15 +826614,41 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/session/session_test.go", + "name": "TestLogoutMarkersSurviveIdentifierChange" + } + ] }, "interop": { "status": "pending", @@ -821420,15 +826658,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "Single Logout protocol processing is unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -821451,15 +826689,41 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/session/session_test.go", + "name": "TestLogoutMarkersSurviveIdentifierChange" + } + ] }, "interop": { "status": "pending", @@ -821469,15 +826733,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "Single Logout protocol processing is unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -821500,15 +826764,41 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/session/session_test.go", + "name": "TestLogoutMarkersSurviveIdentifierChange" + } + ] }, "interop": { "status": "pending", @@ -821518,15 +826808,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "Single Logout protocol processing is unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -821547,33 +826837,68 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignNameIDMappingRequest", + "saml/protocol.VerifyNameIDMappingRequest", + "saml/protocol.SignNameIDMappingResponse", + "saml/protocol.VerifyNameIDMappingResponse", + "saml/authority.NewService" + ], + "code": [ + "saml/protocol/mapping.go", + "saml/authority/authority.go", + "saml/authority/client.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "SOAP mapping, encrypted input/output, explicit target policy, status/correlation failures and limited-use assertions now have executable tests.", + "positive": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/authority/authority_test.go", + "name": "TestAuthoritySOAPAndURI" + } + ], + "negative": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/protocol/mapping_test.go", + "name": "TestLimitedUseMappedIdentifier" + } + ] }, "interop": { "status": "pending", - "tests": [], - "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "tests": [ + { + "file": "tests/interop/saml_authority_test.go", + "name": "TestOpenSAMLAuthorityContracts" + } + ], + "fixtures": [ + "tests/interop/testdata/OpenSAMLAuthorityOracle.java" + ], + "reason": "OpenSAML 5.2.3 generates and validates NameID Mapping messages and signatures. Full Extended-role proxying and all optional cipher/identifier forms are separate." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicitly configured SOAP mapping with trusted peers, authorization and replay state." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Existing explicit legacy policies apply; plaintext mapping requires a deployment-specific exception." } }, - "gap": "The optional Extended Name Identifier Mapping protocol is unimplemented.", + "gap": "NameID Mapping SOAP is implemented and tested; arbitrary BaseID types, automatic Extended-role proxying and full independent role conformance remain outside this record.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -821594,33 +826919,68 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignNameIDMappingRequest", + "saml/protocol.VerifyNameIDMappingRequest", + "saml/protocol.SignNameIDMappingResponse", + "saml/protocol.VerifyNameIDMappingResponse", + "saml/authority.NewService" + ], + "code": [ + "saml/protocol/mapping.go", + "saml/authority/authority.go", + "saml/authority/client.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "SOAP mapping, encrypted input/output, explicit target policy, status/correlation failures and limited-use assertions now have executable tests.", + "positive": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/authority/authority_test.go", + "name": "TestAuthoritySOAPAndURI" + } + ], + "negative": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/protocol/mapping_test.go", + "name": "TestLimitedUseMappedIdentifier" + } + ] }, "interop": { "status": "pending", - "tests": [], - "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "tests": [ + { + "file": "tests/interop/saml_authority_test.go", + "name": "TestOpenSAMLAuthorityContracts" + } + ], + "fixtures": [ + "tests/interop/testdata/OpenSAMLAuthorityOracle.java" + ], + "reason": "OpenSAML 5.2.3 generates and validates NameID Mapping messages and signatures. Full Extended-role proxying and all optional cipher/identifier forms are separate." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicitly configured SOAP mapping with trusted peers, authorization and replay state." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Existing explicit legacy policies apply; plaintext mapping requires a deployment-specific exception." } }, - "gap": "The optional Extended Name Identifier Mapping protocol is unimplemented.", + "gap": "NameID Mapping SOAP is implemented and tested; arbitrary BaseID types, automatic Extended-role proxying and full independent role conformance remain outside this record.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -821641,33 +827001,68 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignNameIDMappingRequest", + "saml/protocol.VerifyNameIDMappingRequest", + "saml/protocol.SignNameIDMappingResponse", + "saml/protocol.VerifyNameIDMappingResponse", + "saml/authority.NewService" + ], + "code": [ + "saml/protocol/mapping.go", + "saml/authority/authority.go", + "saml/authority/client.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "SOAP mapping, encrypted input/output, explicit target policy, status/correlation failures and limited-use assertions now have executable tests.", + "positive": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/authority/authority_test.go", + "name": "TestAuthoritySOAPAndURI" + } + ], + "negative": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/protocol/mapping_test.go", + "name": "TestLimitedUseMappedIdentifier" + } + ] }, "interop": { "status": "pending", - "tests": [], - "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "tests": [ + { + "file": "tests/interop/saml_authority_test.go", + "name": "TestOpenSAMLAuthorityContracts" + } + ], + "fixtures": [ + "tests/interop/testdata/OpenSAMLAuthorityOracle.java" + ], + "reason": "OpenSAML 5.2.3 generates and validates NameID Mapping messages and signatures. Full Extended-role proxying and all optional cipher/identifier forms are separate." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicitly configured SOAP mapping with trusted peers, authorization and replay state." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Existing explicit legacy policies apply; plaintext mapping requires a deployment-specific exception." } }, - "gap": "The optional Extended Name Identifier Mapping protocol is unimplemented.", + "gap": "NameID Mapping SOAP is implemented and tested; arbitrary BaseID types, automatic Extended-role proxying and full independent role conformance remain outside this record.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -822466,34 +827861,69 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", + "saml/sp.NewServiceProvider", + "saml/idp.NewIdentityProvider" + ], + "code": [ + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "All seven encryption target combinations, required-encryption downgrade rejection, authenticated failure order, key rotation/budgets, optional Type and sibling keys are tested within solicited WebSSO.", + "positive": [ + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedResponseTargets" + }, + { + "file": "saml/sp/encryption_test.go", + "name": "TestEncryptedWebSSO" + } + ], + "negative": [ + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedResponsePoliciesAndFailureOrder" + }, + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedCBCAndUnsignedAssertion" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "forbidden", + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "This assertion extension, encrypted representation or general content/processing form is unimplemented in the selected SAML API.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Implemented for the current NameID/string-attribute solicited WebSSO profile using explicitly selected RSA transports, inline/sibling EncryptedKey alternatives and no external key/cipher I/O. General SAML extensions, other key-management profiles and complete independent malformed-input coverage are not claimed.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-IDP-SAMLCORE-6-2", @@ -822516,34 +827946,69 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignResponse", + "saml/protocol.VerifyResponse", + "saml/sp.NewServiceProvider", + "saml/idp.NewIdentityProvider" + ], + "code": [ + "saml/protocol/encryption.go", + "saml/protocol/response.go", + "saml/protocol/create_response.go", + "saml/internal/xmlsig/sign.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "All seven encryption target combinations, required-encryption downgrade rejection, authenticated failure order, key rotation/budgets, optional Type and sibling keys are tested within solicited WebSSO.", + "positive": [ + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedResponseTargets" + }, + { + "file": "saml/sp/encryption_test.go", + "name": "TestEncryptedWebSSO" + } + ], + "negative": [ + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedResponsePoliciesAndFailureOrder" + }, + { + "file": "saml/protocol/encryption_test.go", + "name": "TestEncryptedCBCAndUnsignedAssertion" + } + ] }, "interop": { "status": "pending", - "tests": [], + "tests": [ + { + "file": "tests/interop/saml_encryption_test.go", + "name": "TestXMLSecEncryptedSAML" + } + ], "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "reason": "Generated xmlsec1 exchanges independently sign/encrypt/decrypt seven target combinations for AES128-GCM/RSA-OAEP and legacy AES128-CBC/RSA15 with RSA-SHA1/Canonical XML 1.0. This finite matrix does not establish every algorithm or malformed-input requirement." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "forbidden", + "reason": "Encrypted data needs explicit recipient providers; legacy algorithms and shared-secret MACs are not selected by defaults." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Explicit encryption/decryption options, algorithm allowlists and legacy-signing permission select only the configured behavior; XML cannot add keys, algorithms or trust." } }, - "gap": "This assertion extension, encrypted representation or general content/processing form is unimplemented in the selected SAML API.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Implemented for the current NameID/string-attribute solicited WebSSO profile using explicitly selected RSA transports, inline/sibling EncryptedKey alternatives and no external key/cipher I/O. General SAML extensions, other key-management profiles and complete independent malformed-input coverage are not claimed.", + "closure": "Complete the remaining role/profile and independent clause-specific witnesses without weakening explicit trust or default algorithms." }, { "id": "N-SAML-IDP-SAMLCORE-7-1-1", @@ -824245,15 +829710,34 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/binding.SOAPClient.RoundTrip", + "saml/binding.DecodeSOAP" + ], + "code": [ + "saml/binding/soap.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/soap_test.go", + "name": "TestSOAPBackChannelManagementAndLogout" + } + ], + "negative": [ + { + "file": "saml/binding/soap_test.go", + "name": "TestSOAPIndependentEnvelopes" + }, + { + "file": "saml/binding/soap_test.go", + "name": "TestSOAPHTTPStatusAndRedirects" + } + ] }, "interop": { "status": "pending", @@ -824263,15 +829747,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -824294,15 +829778,34 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/binding.SOAPClient.RoundTrip", + "saml/binding.DecodeSOAP" + ], + "code": [ + "saml/binding/soap.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/soap_test.go", + "name": "TestSOAPBackChannelManagementAndLogout" + } + ], + "negative": [ + { + "file": "saml/binding/soap_test.go", + "name": "TestSOAPIndependentEnvelopes" + }, + { + "file": "saml/binding/soap_test.go", + "name": "TestSOAPHTTPStatusAndRedirects" + } + ] }, "interop": { "status": "pending", @@ -824312,15 +829815,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -824341,15 +829844,34 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/binding.SOAPClient.RoundTrip", + "saml/binding.DecodeSOAP" + ], + "code": [ + "saml/binding/soap.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/soap_test.go", + "name": "TestSOAPBackChannelManagementAndLogout" + } + ], + "negative": [ + { + "file": "saml/binding/soap_test.go", + "name": "TestSOAPIndependentEnvelopes" + }, + { + "file": "saml/binding/soap_test.go", + "name": "TestSOAPHTTPStatusAndRedirects" + } + ] }, "interop": { "status": "pending", @@ -824359,15 +829881,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -824388,15 +829910,34 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/binding.SOAPClient.RoundTrip", + "saml/binding.DecodeSOAP" + ], + "code": [ + "saml/binding/soap.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/soap_test.go", + "name": "TestSOAPBackChannelManagementAndLogout" + } + ], + "negative": [ + { + "file": "saml/binding/soap_test.go", + "name": "TestSOAPIndependentEnvelopes" + }, + { + "file": "saml/binding/soap_test.go", + "name": "TestSOAPHTTPStatusAndRedirects" + } + ] }, "interop": { "status": "pending", @@ -824406,15 +829947,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -824435,15 +829976,34 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/binding.SOAPClient.RoundTrip", + "saml/binding.DecodeSOAP" + ], + "code": [ + "saml/binding/soap.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/soap_test.go", + "name": "TestSOAPBackChannelManagementAndLogout" + } + ], + "negative": [ + { + "file": "saml/binding/soap_test.go", + "name": "TestSOAPIndependentEnvelopes" + }, + { + "file": "saml/binding/soap_test.go", + "name": "TestSOAPHTTPStatusAndRedirects" + } + ] }, "interop": { "status": "pending", @@ -824453,15 +830013,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -824482,15 +830042,34 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/binding.SOAPClient.RoundTrip", + "saml/binding.DecodeSOAP" + ], + "code": [ + "saml/binding/soap.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/soap_test.go", + "name": "TestSOAPBackChannelManagementAndLogout" + } + ], + "negative": [ + { + "file": "saml/binding/soap_test.go", + "name": "TestSOAPIndependentEnvelopes" + }, + { + "file": "saml/binding/soap_test.go", + "name": "TestSOAPHTTPStatusAndRedirects" + } + ] }, "interop": { "status": "pending", @@ -824500,15 +830079,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -824529,15 +830108,34 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/binding.SOAPClient.RoundTrip", + "saml/binding.DecodeSOAP" + ], + "code": [ + "saml/binding/soap.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/soap_test.go", + "name": "TestSOAPBackChannelManagementAndLogout" + } + ], + "negative": [ + { + "file": "saml/binding/soap_test.go", + "name": "TestSOAPIndependentEnvelopes" + }, + { + "file": "saml/binding/soap_test.go", + "name": "TestSOAPHTTPStatusAndRedirects" + } + ] }, "interop": { "status": "pending", @@ -824547,15 +830145,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -824576,15 +830174,44 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -824594,15 +830221,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -824623,15 +830250,44 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -824641,15 +830297,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -824672,15 +830328,44 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -824690,15 +830375,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -824721,15 +830406,44 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -824739,15 +830453,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -824768,15 +830482,44 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -824786,15 +830529,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -824817,15 +830560,44 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -824835,15 +830607,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -824866,15 +830638,44 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -824884,15 +830685,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -825783,15 +831584,36 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Respond", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestInvalidArtifactReturnsEmptySuccess" + } + ] }, "interop": { "status": "pending", @@ -825801,15 +831623,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -825830,15 +831652,36 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Respond", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestInvalidArtifactReturnsEmptySuccess" + } + ] }, "interop": { "status": "pending", @@ -825848,15 +831691,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -825877,15 +831720,36 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Respond", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestInvalidArtifactReturnsEmptySuccess" + } + ] }, "interop": { "status": "pending", @@ -825895,15 +831759,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -825924,15 +831788,36 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Respond", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestInvalidArtifactReturnsEmptySuccess" + } + ] }, "interop": { "status": "pending", @@ -825942,15 +831827,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -825971,15 +831856,36 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Respond", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestInvalidArtifactReturnsEmptySuccess" + } + ] }, "interop": { "status": "pending", @@ -825989,15 +831895,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -826020,15 +831926,36 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Respond", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestInvalidArtifactReturnsEmptySuccess" + } + ] }, "interop": { "status": "pending", @@ -826038,15 +831965,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -826069,15 +831996,36 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Respond", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestInvalidArtifactReturnsEmptySuccess" + } + ] }, "interop": { "status": "pending", @@ -826087,15 +832035,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -826116,15 +832064,36 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Respond", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestInvalidArtifactReturnsEmptySuccess" + } + ] }, "interop": { "status": "pending", @@ -826134,15 +832103,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -826165,15 +832134,36 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Respond", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestInvalidArtifactReturnsEmptySuccess" + } + ] }, "interop": { "status": "pending", @@ -826183,15 +832173,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -826212,15 +832202,36 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Respond", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestInvalidArtifactReturnsEmptySuccess" + } + ] }, "interop": { "status": "pending", @@ -826230,15 +832241,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -826261,15 +832272,36 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Respond", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestInvalidArtifactReturnsEmptySuccess" + } + ] }, "interop": { "status": "pending", @@ -826279,15 +832311,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "This SOAP/PAOS/Artifact/URI binding contract and its transport/security behavior are unimplemented.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -827832,15 +833864,44 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -827850,15 +833911,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -827881,15 +833942,44 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -827899,15 +833989,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -827930,15 +834020,44 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -827948,15 +834067,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -827977,15 +834096,44 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -827995,15 +834143,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -828024,15 +834172,44 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -828042,15 +834219,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -828071,15 +834248,44 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -828089,15 +834295,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -828118,15 +834324,44 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -828136,15 +834371,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -828165,15 +834400,44 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -828183,15 +834447,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -828212,15 +834476,44 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -828230,15 +834523,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -828259,15 +834552,44 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -828277,15 +834599,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -828309,15 +834631,44 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -828327,15 +834678,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -828356,15 +834707,44 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -828374,15 +834754,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -828405,15 +834785,44 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -828423,15 +834832,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -828452,15 +834861,44 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -828470,15 +834908,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -828499,15 +834937,44 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -828517,15 +834984,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -828546,15 +835013,44 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -828564,15 +835060,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -828737,15 +835233,41 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/session/session_test.go", + "name": "TestLogoutMarkersSurviveIdentifierChange" + } + ] }, "interop": { "status": "pending", @@ -828755,15 +835277,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -828784,15 +835306,41 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/session/session_test.go", + "name": "TestLogoutMarkersSurviveIdentifierChange" + } + ] }, "interop": { "status": "pending", @@ -828802,15 +835350,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -828831,15 +835379,41 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/session/session_test.go", + "name": "TestLogoutMarkersSurviveIdentifierChange" + } + ] }, "interop": { "status": "pending", @@ -828849,15 +835423,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -828878,15 +835452,41 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/session/session_test.go", + "name": "TestLogoutMarkersSurviveIdentifierChange" + } + ] }, "interop": { "status": "pending", @@ -828896,15 +835496,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -828925,15 +835525,41 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/session/session_test.go", + "name": "TestLogoutMarkersSurviveIdentifierChange" + } + ] }, "interop": { "status": "pending", @@ -828943,15 +835569,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -828972,15 +835598,41 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/session/session_test.go", + "name": "TestLogoutMarkersSurviveIdentifierChange" + } + ] }, "interop": { "status": "pending", @@ -828990,15 +835642,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -829019,15 +835671,41 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/session/session_test.go", + "name": "TestLogoutMarkersSurviveIdentifierChange" + } + ] }, "interop": { "status": "pending", @@ -829037,15 +835715,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -829068,15 +835746,41 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/session/session_test.go", + "name": "TestLogoutMarkersSurviveIdentifierChange" + } + ] }, "interop": { "status": "pending", @@ -829086,15 +835790,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -829115,15 +835819,41 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/session/session_test.go", + "name": "TestLogoutMarkersSurviveIdentifierChange" + } + ] }, "interop": { "status": "pending", @@ -829133,15 +835863,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -829162,15 +835892,41 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/session/session_test.go", + "name": "TestLogoutMarkersSurviveIdentifierChange" + } + ] }, "interop": { "status": "pending", @@ -829180,15 +835936,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -829212,15 +835968,37 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Manage", + "saml/session.Service.BeginManage", + "saml/session.Service.Complete" + ], + "code": [ + "saml/session/session.go", + "saml/session/reply.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/exchange_test.go", + "name": "TestManageNameIDBothDirectionsAndBindings" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestManageAliasTerminationAndAtomicReplay" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestControlSigningFailureDoesNotCommitState" + } + ] }, "interop": { "status": "pending", @@ -829230,15 +836008,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -829262,15 +836040,37 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Manage", + "saml/session.Service.BeginManage", + "saml/session.Service.Complete" + ], + "code": [ + "saml/session/session.go", + "saml/session/reply.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/exchange_test.go", + "name": "TestManageNameIDBothDirectionsAndBindings" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestManageAliasTerminationAndAtomicReplay" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestControlSigningFailureDoesNotCommitState" + } + ] }, "interop": { "status": "pending", @@ -829280,15 +836080,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -829312,15 +836112,37 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Manage", + "saml/session.Service.BeginManage", + "saml/session.Service.Complete" + ], + "code": [ + "saml/session/session.go", + "saml/session/reply.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/exchange_test.go", + "name": "TestManageNameIDBothDirectionsAndBindings" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestManageAliasTerminationAndAtomicReplay" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestControlSigningFailureDoesNotCommitState" + } + ] }, "interop": { "status": "pending", @@ -829330,15 +836152,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -829362,15 +836184,37 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Manage", + "saml/session.Service.BeginManage", + "saml/session.Service.Complete" + ], + "code": [ + "saml/session/session.go", + "saml/session/reply.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/exchange_test.go", + "name": "TestManageNameIDBothDirectionsAndBindings" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestManageAliasTerminationAndAtomicReplay" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestControlSigningFailureDoesNotCommitState" + } + ] }, "interop": { "status": "pending", @@ -829380,15 +836224,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -829412,15 +836256,37 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Manage", + "saml/session.Service.BeginManage", + "saml/session.Service.Complete" + ], + "code": [ + "saml/session/session.go", + "saml/session/reply.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/exchange_test.go", + "name": "TestManageNameIDBothDirectionsAndBindings" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestManageAliasTerminationAndAtomicReplay" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestControlSigningFailureDoesNotCommitState" + } + ] }, "interop": { "status": "pending", @@ -829430,15 +836296,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -829462,15 +836328,37 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Manage", + "saml/session.Service.BeginManage", + "saml/session.Service.Complete" + ], + "code": [ + "saml/session/session.go", + "saml/session/reply.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/exchange_test.go", + "name": "TestManageNameIDBothDirectionsAndBindings" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestManageAliasTerminationAndAtomicReplay" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestControlSigningFailureDoesNotCommitState" + } + ] }, "interop": { "status": "pending", @@ -829480,15 +836368,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -829512,15 +836400,37 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Manage", + "saml/session.Service.BeginManage", + "saml/session.Service.Complete" + ], + "code": [ + "saml/session/session.go", + "saml/session/reply.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/exchange_test.go", + "name": "TestManageNameIDBothDirectionsAndBindings" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestManageAliasTerminationAndAtomicReplay" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestControlSigningFailureDoesNotCommitState" + } + ] }, "interop": { "status": "pending", @@ -829530,15 +836440,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -830217,33 +837127,68 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignNameIDMappingRequest", + "saml/protocol.VerifyNameIDMappingRequest", + "saml/protocol.SignNameIDMappingResponse", + "saml/protocol.VerifyNameIDMappingResponse", + "saml/authority.NewService" + ], + "code": [ + "saml/protocol/mapping.go", + "saml/authority/authority.go", + "saml/authority/client.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "SOAP mapping, encrypted input/output, explicit target policy, status/correlation failures and limited-use assertions now have executable tests.", + "positive": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/authority/authority_test.go", + "name": "TestAuthoritySOAPAndURI" + } + ], + "negative": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/protocol/mapping_test.go", + "name": "TestLimitedUseMappedIdentifier" + } + ] }, "interop": { "status": "pending", - "tests": [], - "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "tests": [ + { + "file": "tests/interop/saml_authority_test.go", + "name": "TestOpenSAMLAuthorityContracts" + } + ], + "fixtures": [ + "tests/interop/testdata/OpenSAMLAuthorityOracle.java" + ], + "reason": "OpenSAML 5.2.3 generates and validates NameID Mapping messages and signatures. Full Extended-role proxying and all optional cipher/identifier forms are separate." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicitly configured SOAP mapping with trusted peers, authorization and replay state." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Existing explicit legacy policies apply; plaintext mapping requires a deployment-specific exception." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "NameID Mapping SOAP is implemented and tested; arbitrary BaseID types, automatic Extended-role proxying and full independent role conformance remain outside this record.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -830264,33 +837209,68 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignNameIDMappingRequest", + "saml/protocol.VerifyNameIDMappingRequest", + "saml/protocol.SignNameIDMappingResponse", + "saml/protocol.VerifyNameIDMappingResponse", + "saml/authority.NewService" + ], + "code": [ + "saml/protocol/mapping.go", + "saml/authority/authority.go", + "saml/authority/client.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "SOAP mapping, encrypted input/output, explicit target policy, status/correlation failures and limited-use assertions now have executable tests.", + "positive": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/authority/authority_test.go", + "name": "TestAuthoritySOAPAndURI" + } + ], + "negative": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/protocol/mapping_test.go", + "name": "TestLimitedUseMappedIdentifier" + } + ] }, "interop": { "status": "pending", - "tests": [], - "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "tests": [ + { + "file": "tests/interop/saml_authority_test.go", + "name": "TestOpenSAMLAuthorityContracts" + } + ], + "fixtures": [ + "tests/interop/testdata/OpenSAMLAuthorityOracle.java" + ], + "reason": "OpenSAML 5.2.3 generates and validates NameID Mapping messages and signatures. Full Extended-role proxying and all optional cipher/identifier forms are separate." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicitly configured SOAP mapping with trusted peers, authorization and replay state." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Existing explicit legacy policies apply; plaintext mapping requires a deployment-specific exception." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "NameID Mapping SOAP is implemented and tested; arbitrary BaseID types, automatic Extended-role proxying and full independent role conformance remain outside this record.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -830311,33 +837291,68 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignNameIDMappingRequest", + "saml/protocol.VerifyNameIDMappingRequest", + "saml/protocol.SignNameIDMappingResponse", + "saml/protocol.VerifyNameIDMappingResponse", + "saml/authority.NewService" + ], + "code": [ + "saml/protocol/mapping.go", + "saml/authority/authority.go", + "saml/authority/client.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "SOAP mapping, encrypted input/output, explicit target policy, status/correlation failures and limited-use assertions now have executable tests.", + "positive": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/authority/authority_test.go", + "name": "TestAuthoritySOAPAndURI" + } + ], + "negative": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/protocol/mapping_test.go", + "name": "TestLimitedUseMappedIdentifier" + } + ] }, "interop": { "status": "pending", - "tests": [], - "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "tests": [ + { + "file": "tests/interop/saml_authority_test.go", + "name": "TestOpenSAMLAuthorityContracts" + } + ], + "fixtures": [ + "tests/interop/testdata/OpenSAMLAuthorityOracle.java" + ], + "reason": "OpenSAML 5.2.3 generates and validates NameID Mapping messages and signatures. Full Extended-role proxying and all optional cipher/identifier forms are separate." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicitly configured SOAP mapping with trusted peers, authorization and replay state." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Existing explicit legacy policies apply; plaintext mapping requires a deployment-specific exception." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "NameID Mapping SOAP is implemented and tested; arbitrary BaseID types, automatic Extended-role proxying and full independent role conformance remain outside this record.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -830358,33 +837373,68 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignNameIDMappingRequest", + "saml/protocol.VerifyNameIDMappingRequest", + "saml/protocol.SignNameIDMappingResponse", + "saml/protocol.VerifyNameIDMappingResponse", + "saml/authority.NewService" + ], + "code": [ + "saml/protocol/mapping.go", + "saml/authority/authority.go", + "saml/authority/client.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "SOAP mapping, encrypted input/output, explicit target policy, status/correlation failures and limited-use assertions now have executable tests.", + "positive": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/authority/authority_test.go", + "name": "TestAuthoritySOAPAndURI" + } + ], + "negative": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/protocol/mapping_test.go", + "name": "TestLimitedUseMappedIdentifier" + } + ] }, "interop": { "status": "pending", - "tests": [], - "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "tests": [ + { + "file": "tests/interop/saml_authority_test.go", + "name": "TestOpenSAMLAuthorityContracts" + } + ], + "fixtures": [ + "tests/interop/testdata/OpenSAMLAuthorityOracle.java" + ], + "reason": "OpenSAML 5.2.3 generates and validates NameID Mapping messages and signatures. Full Extended-role proxying and all optional cipher/identifier forms are separate." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicitly configured SOAP mapping with trusted peers, authorization and replay state." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Existing explicit legacy policies apply; plaintext mapping requires a deployment-specific exception." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "NameID Mapping SOAP is implemented and tested; arbitrary BaseID types, automatic Extended-role proxying and full independent role conformance remain outside this record.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -830405,33 +837455,68 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignNameIDMappingRequest", + "saml/protocol.VerifyNameIDMappingRequest", + "saml/protocol.SignNameIDMappingResponse", + "saml/protocol.VerifyNameIDMappingResponse", + "saml/authority.NewService" + ], + "code": [ + "saml/protocol/mapping.go", + "saml/authority/authority.go", + "saml/authority/client.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "SOAP mapping, encrypted input/output, explicit target policy, status/correlation failures and limited-use assertions now have executable tests.", + "positive": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/authority/authority_test.go", + "name": "TestAuthoritySOAPAndURI" + } + ], + "negative": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/protocol/mapping_test.go", + "name": "TestLimitedUseMappedIdentifier" + } + ] }, "interop": { "status": "pending", - "tests": [], - "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "tests": [ + { + "file": "tests/interop/saml_authority_test.go", + "name": "TestOpenSAMLAuthorityContracts" + } + ], + "fixtures": [ + "tests/interop/testdata/OpenSAMLAuthorityOracle.java" + ], + "reason": "OpenSAML 5.2.3 generates and validates NameID Mapping messages and signatures. Full Extended-role proxying and all optional cipher/identifier forms are separate." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicitly configured SOAP mapping with trusted peers, authorization and replay state." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Existing explicit legacy policies apply; plaintext mapping requires a deployment-specific exception." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "NameID Mapping SOAP is implemented and tested; arbitrary BaseID types, automatic Extended-role proxying and full independent role conformance remain outside this record.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -830452,33 +837537,68 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignNameIDMappingRequest", + "saml/protocol.VerifyNameIDMappingRequest", + "saml/protocol.SignNameIDMappingResponse", + "saml/protocol.VerifyNameIDMappingResponse", + "saml/authority.NewService" + ], + "code": [ + "saml/protocol/mapping.go", + "saml/authority/authority.go", + "saml/authority/client.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "SOAP mapping, encrypted input/output, explicit target policy, status/correlation failures and limited-use assertions now have executable tests.", + "positive": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/authority/authority_test.go", + "name": "TestAuthoritySOAPAndURI" + } + ], + "negative": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/protocol/mapping_test.go", + "name": "TestLimitedUseMappedIdentifier" + } + ] }, "interop": { "status": "pending", - "tests": [], - "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "tests": [ + { + "file": "tests/interop/saml_authority_test.go", + "name": "TestOpenSAMLAuthorityContracts" + } + ], + "fixtures": [ + "tests/interop/testdata/OpenSAMLAuthorityOracle.java" + ], + "reason": "OpenSAML 5.2.3 generates and validates NameID Mapping messages and signatures. Full Extended-role proxying and all optional cipher/identifier forms are separate." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicitly configured SOAP mapping with trusted peers, authorization and replay state." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Existing explicit legacy policies apply; plaintext mapping requires a deployment-specific exception." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "NameID Mapping SOAP is implemented and tested; arbitrary BaseID types, automatic Extended-role proxying and full independent role conformance remain outside this record.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -830499,33 +837619,68 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/protocol.SignNameIDMappingRequest", + "saml/protocol.VerifyNameIDMappingRequest", + "saml/protocol.SignNameIDMappingResponse", + "saml/protocol.VerifyNameIDMappingResponse", + "saml/authority.NewService" + ], + "code": [ + "saml/protocol/mapping.go", + "saml/authority/authority.go", + "saml/authority/client.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "SOAP mapping, encrypted input/output, explicit target policy, status/correlation failures and limited-use assertions now have executable tests.", + "positive": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/authority/authority_test.go", + "name": "TestAuthoritySOAPAndURI" + } + ], + "negative": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/protocol/mapping_test.go", + "name": "TestLimitedUseMappedIdentifier" + } + ] }, "interop": { "status": "pending", - "tests": [], - "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "tests": [ + { + "file": "tests/interop/saml_authority_test.go", + "name": "TestOpenSAMLAuthorityContracts" + } + ], + "fixtures": [ + "tests/interop/testdata/OpenSAMLAuthorityOracle.java" + ], + "reason": "OpenSAML 5.2.3 generates and validates NameID Mapping messages and signatures. Full Extended-role proxying and all optional cipher/identifier forms are separate." }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicitly configured SOAP mapping with trusted peers, authorization and replay state." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Existing explicit legacy policies apply; plaintext mapping requires a deployment-specific exception." } }, - "gap": "The named profile contract is unimplemented. Its selected full-role mandatory duties remain applicable; other profile extensions retain their explicit optional condition.", + "gap": "NameID Mapping SOAP is implemented and tested; arbitrary BaseID types, automatic Extended-role proxying and full independent role conformance remain outside this record.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -833587,14 +840742,110 @@ "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { - "id": "N-SAML-IDP-SAMLMD-4-2-3-2", - "statement": "Apply the selected role's Obtaining Metadata via the DNS syntax, value/cardinality and processing contract, including its stated feature conditions.", + "id": "N-SAML-IDP-SAMLMD-4-2-3-2", + "statement": "Apply the selected role's Obtaining Metadata via the DNS syntax, value/cardinality and processing contract, including its stated feature conditions.", + "norm": { + "document": "SAMLMD", + "clause": "4.2.3.2", + "level": "normative", + "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-metadata-2.0-os.pdf#page=33", + "errata": [] + }, + "applies": { + "profile": "saml-idp", + "role": "SAML IdP", + "mode": "metadata", + "status": "applicable", + "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." + }, + "implementation": { + "status": "unimplemented", + "apis": [], + "code": [] + }, + "tests": { + "status": "pending", + "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", + "positive": [], + "negative": [] + }, + "interop": { + "status": "pending", + "tests": [], + "fixtures": [], + "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + }, + "policy": { + "default": { + "status": "unavailable", + "reason": "The implementation is absent; policy cannot enable it." + }, + "compatibility": { + "status": "unavailable", + "reason": "No general compatibility switch supplies missing normative behavior." + } + }, + "gap": "The parser may skip selected metadata decoration, but does not implement this semantic publication/resolution/attribute/affiliation contract.", + "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + }, + { + "id": "N-SAML-IDP-SAMLMD-4-2-4", + "statement": "Apply the selected role's Metadata Location Caching syntax, value/cardinality and processing contract, including its stated feature conditions.", + "norm": { + "document": "SAMLMD", + "clause": "4.2.4", + "level": "normative", + "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-metadata-2.0-os.pdf#page=33", + "errata": [] + }, + "applies": { + "profile": "saml-idp", + "role": "SAML IdP", + "mode": "metadata", + "status": "applicable", + "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." + }, + "implementation": { + "status": "unimplemented", + "apis": [], + "code": [] + }, + "tests": { + "status": "pending", + "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", + "positive": [], + "negative": [] + }, + "interop": { + "status": "pending", + "tests": [], + "fixtures": [], + "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + }, + "policy": { + "default": { + "status": "unavailable", + "reason": "The implementation is absent; policy cannot enable it." + }, + "compatibility": { + "status": "unavailable", + "reason": "No general compatibility switch supplies missing normative behavior." + } + }, + "gap": "The parser may skip selected metadata decoration, but does not implement this semantic publication/resolution/attribute/affiliation contract.", + "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + }, + { + "id": "N-SAML-IDP-SAMLMD-4-3-1", + "statement": "Apply the selected role's Metadata Instance Caching syntax, value/cardinality and processing contract, including its stated feature conditions.", "norm": { "document": "SAMLMD", - "clause": "4.2.3.2", + "clause": "4.3.1", "level": "normative", "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-metadata-2.0-os.pdf#page=33", - "errata": [] + "errata": [ + "E94" + ] }, "applies": { "profile": "saml-idp", @@ -833634,11 +840885,11 @@ "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { - "id": "N-SAML-IDP-SAMLMD-4-2-4", - "statement": "Apply the selected role's Metadata Location Caching syntax, value/cardinality and processing contract, including its stated feature conditions.", + "id": "N-SAML-IDP-SAMLMD-4-3-2", + "statement": "Apply the selected role's Handling of HTTPS Redirects syntax, value/cardinality and processing contract, including its stated feature conditions.", "norm": { "document": "SAMLMD", - "clause": "4.2.4", + "clause": "4.3.2", "level": "normative", "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-metadata-2.0-os.pdf#page=33", "errata": [] @@ -833681,16 +840932,14 @@ "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { - "id": "N-SAML-IDP-SAMLMD-4-3-1", - "statement": "Apply the selected role's Metadata Instance Caching syntax, value/cardinality and processing contract, including its stated feature conditions.", + "id": "N-SAML-IDP-SAMLMD-4-3-3-1", + "statement": "Apply the selected role's Processing Signed DNS Zones syntax, value/cardinality and processing contract, including its stated feature conditions.", "norm": { "document": "SAMLMD", - "clause": "4.3.1", + "clause": "4.3.3.1", "level": "normative", - "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-metadata-2.0-os.pdf#page=33", - "errata": [ - "E94" - ] + "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-metadata-2.0-os.pdf#page=34", + "errata": [] }, "applies": { "profile": "saml-idp", @@ -833730,13 +840979,13 @@ "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { - "id": "N-SAML-IDP-SAMLMD-4-3-2", - "statement": "Apply the selected role's Handling of HTTPS Redirects syntax, value/cardinality and processing contract, including its stated feature conditions.", + "id": "N-SAML-IDP-SAMLMD-4-3-3-2", + "statement": "Apply the selected role's Processing Signed Documents and Fragments syntax, value/cardinality and processing contract, including its stated feature conditions.", "norm": { "document": "SAMLMD", - "clause": "4.3.2", + "clause": "4.3.3.2", "level": "normative", - "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-metadata-2.0-os.pdf#page=33", + "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-metadata-2.0-os.pdf#page=34", "errata": [] }, "applies": { @@ -833777,11 +841026,11 @@ "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { - "id": "N-SAML-IDP-SAMLMD-4-3-3-1", - "statement": "Apply the selected role's Processing Signed DNS Zones syntax, value/cardinality and processing contract, including its stated feature conditions.", + "id": "N-SAML-IDP-SAMLMD-4-3-3-3", + "statement": "Apply the selected role's Processing Server Authentication during Metadata Retrieval via TLS/SSL syntax, value/cardinality and processing contract, including its stated feature conditions.", "norm": { "document": "SAMLMD", - "clause": "4.3.3.1", + "clause": "4.3.3.3", "level": "normative", "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-metadata-2.0-os.pdf#page=34", "errata": [] @@ -833824,26 +841073,34 @@ "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { - "id": "N-SAML-IDP-SAMLMD-4-3-3-2", - "statement": "Apply the selected role's Processing Signed Documents and Fragments syntax, value/cardinality and processing contract, including its stated feature conditions.", + "id": "N-SAML-IDP-ERRATUM-SAMLCORE-E0", + "statement": "Apply Approved Errata 05 E0 to the affected base-document contract: Incorrect Section Reference.", "norm": { - "document": "SAMLMD", - "clause": "4.3.3.2", + "document": "SAMLCORE", + "clause": "3.7.3", "level": "normative", - "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-metadata-2.0-os.pdf#page=34", - "errata": [] + "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf", + "errata": [ + "E0" + ] }, "applies": { "profile": "saml-idp", "role": "SAML IdP", - "mode": "metadata", + "mode": "websso", "status": "applicable", "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/idp.NewIdentityProvider", + "saml/idp.IdentityProvider.AcceptRedirect", + "saml/idp.IdentityProvider.Issue" + ], + "code": [ + "saml/idp/idp.go" + ] }, "tests": { "status": "pending", @@ -833859,31 +841116,33 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Available within the explicitly documented bounded API and its algorithm/resource/trust settings." }, "compatibility": { - "status": "unavailable", + "status": "not-applicable", "reason": "No general compatibility switch supplies missing normative behavior." } }, - "gap": "The parser may skip selected metadata decoration, but does not implement this semantic publication/resolution/attribute/affiliation contract.", + "gap": "The correction is selected for this base document and role, but complete corrected-clause implementation and independent evidence remain open.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { - "id": "N-SAML-IDP-SAMLMD-4-3-3-3", - "statement": "Apply the selected role's Processing Server Authentication during Metadata Retrieval via TLS/SSL syntax, value/cardinality and processing contract, including its stated feature conditions.", + "id": "N-SAML-IDP-ERRATUM-SAMLCORE-E6", + "statement": "Apply Approved Errata 05 E6 to the affected base-document contract: Clarify Constraints on Encrypted NameID.", "norm": { - "document": "SAMLMD", - "clause": "4.3.3.3", + "document": "SAMLCORE", + "clause": "3.4.1.1", "level": "normative", - "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-metadata-2.0-os.pdf#page=34", - "errata": [] + "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf", + "errata": [ + "E6" + ] }, "applies": { "profile": "saml-idp", "role": "SAML IdP", - "mode": "metadata", + "mode": "websso", "status": "applicable", "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, @@ -833914,44 +841173,60 @@ "reason": "No general compatibility switch supplies missing normative behavior." } }, - "gap": "The parser may skip selected metadata decoration, but does not implement this semantic publication/resolution/attribute/affiliation contract.", + "gap": "The corrected encrypted or additional protocol/profile workflow is unimplemented; selecting an erratum does not implement it.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { - "id": "N-SAML-IDP-ERRATUM-SAMLCORE-E0", - "statement": "Apply Approved Errata 05 E0 to the affected base-document contract: Incorrect Section Reference.", + "id": "N-SAML-IDP-ERRATUM-SAMLCORE-E8", + "statement": "Apply Approved Errata 05 E8 to the affected base-document contract: SLO and NameID Termination.", "norm": { "document": "SAMLCORE", - "clause": "3.7.3", + "clause": "3.6.3", "level": "normative", "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf", "errata": [ - "E0" + "E8" ] }, "applies": { "profile": "saml-idp", "role": "SAML IdP", - "mode": "websso", + "mode": "nameid", "status": "applicable", "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { "status": "limited", "apis": [ - "saml/idp.NewIdentityProvider", - "saml/idp.IdentityProvider.AcceptRedirect", - "saml/idp.IdentityProvider.Issue" + "saml/session.Service.Manage", + "saml/session.Service.BeginManage", + "saml/session.Service.Complete" ], "code": [ - "saml/idp/idp.go" + "saml/session/session.go", + "saml/session/reply.go", + "saml/protocol/control.go" ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/exchange_test.go", + "name": "TestManageNameIDBothDirectionsAndBindings" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestManageAliasTerminationAndAtomicReplay" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestControlSigningFailureDoesNotCommitState" + } + ] }, "interop": { "status": "pending", @@ -833962,45 +841237,71 @@ "policy": { "default": { "status": "enabled", - "reason": "Available within the explicitly documented bounded API and its algorithm/resource/trust settings." + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "not-applicable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The correction is selected for this base document and role, but complete corrected-clause implementation and independent evidence remain open.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { - "id": "N-SAML-IDP-ERRATUM-SAMLCORE-E6", - "statement": "Apply Approved Errata 05 E6 to the affected base-document contract: Clarify Constraints on Encrypted NameID.", + "id": "N-SAML-IDP-ERRATUM-SAMLCORE-E10", + "statement": "Apply Approved Errata 05 E10 to the affected base-document contract: Logout Request Reason Mismatch with Schema.", "norm": { "document": "SAMLCORE", - "clause": "3.4.1.1", + "clause": "3.7.1", "level": "normative", "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf", "errata": [ - "E6" + "E10" ] }, "applies": { "profile": "saml-idp", "role": "SAML IdP", - "mode": "websso", + "mode": "logout", "status": "applicable", "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/session/session_test.go", + "name": "TestLogoutMarkersSurviveIdentifierChange" + } + ] }, "interop": { "status": "pending", @@ -834010,40 +841311,46 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The corrected encrypted or additional protocol/profile workflow is unimplemented; selecting an erratum does not implement it.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { - "id": "N-SAML-IDP-ERRATUM-SAMLCORE-E8", - "statement": "Apply Approved Errata 05 E8 to the affected base-document contract: SLO and NameID Termination.", + "id": "N-SAML-IDP-ERRATUM-SAMLCORE-E12", + "statement": "Apply Approved Errata 05 E12 to the affected base-document contract: � Clarification on ManageNameIDRequest.", "norm": { "document": "SAMLCORE", - "clause": "3.6.3", + "clause": "3.6", "level": "normative", "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf", "errata": [ - "E8" + "E12" ] }, "applies": { "profile": "saml-idp", "role": "SAML IdP", - "mode": "nameid", + "mode": "websso", "status": "applicable", "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/idp.NewIdentityProvider", + "saml/idp.IdentityProvider.AcceptRedirect", + "saml/idp.IdentityProvider.Issue" + ], + "code": [ + "saml/idp/idp.go" + ] }, "tests": { "status": "pending", @@ -834059,40 +841366,46 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Available within the explicitly documented bounded API and its algorithm/resource/trust settings." }, "compatibility": { - "status": "unavailable", + "status": "not-applicable", "reason": "No general compatibility switch supplies missing normative behavior." } }, - "gap": "The corrected encrypted or additional protocol/profile workflow is unimplemented; selecting an erratum does not implement it.", + "gap": "The correction is selected for this base document and role, but complete corrected-clause implementation and independent evidence remain open.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { - "id": "N-SAML-IDP-ERRATUM-SAMLCORE-E10", - "statement": "Apply Approved Errata 05 E10 to the affected base-document contract: Logout Request Reason Mismatch with Schema.", + "id": "N-SAML-IDP-ERRATUM-SAMLCORE-E13", + "statement": "Apply Approved Errata 05 E13 to the affected base-document contract: Inaccurate Description of Authorization Decision.", "norm": { "document": "SAMLCORE", - "clause": "3.7.1", + "clause": "2", "level": "normative", "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf", "errata": [ - "E10" + "E13" ] }, "applies": { "profile": "saml-idp", "role": "SAML IdP", - "mode": "logout", + "mode": "websso", "status": "applicable", "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/idp.NewIdentityProvider", + "saml/idp.IdentityProvider.AcceptRedirect", + "saml/idp.IdentityProvider.Issue" + ], + "code": [ + "saml/idp/idp.go" + ] }, "tests": { "status": "pending", @@ -834108,27 +841421,27 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Available within the explicitly documented bounded API and its algorithm/resource/trust settings." }, "compatibility": { - "status": "unavailable", + "status": "not-applicable", "reason": "No general compatibility switch supplies missing normative behavior." } }, - "gap": "The corrected encrypted or additional protocol/profile workflow is unimplemented; selecting an erratum does not implement it.", + "gap": "The correction is selected for this base document and role, but complete corrected-clause implementation and independent evidence remain open.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { - "id": "N-SAML-IDP-ERRATUM-SAMLCORE-E12", - "statement": "Apply Approved Errata 05 E12 to the affected base-document contract: � Clarification on ManageNameIDRequest.", + "id": "N-SAML-IDP-ERRATUM-SAMLCORE-E14", + "statement": "Apply Approved Errata 05 E14 to the affected base-document contract: AllowCreate.", "norm": { "document": "SAMLCORE", - "clause": "3.6", + "clause": "3.4.1.1", "level": "normative", "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf", "errata": [ - "E12" + "E14" ] }, "applies": { @@ -834175,15 +841488,15 @@ "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { - "id": "N-SAML-IDP-ERRATUM-SAMLCORE-E13", - "statement": "Apply Approved Errata 05 E13 to the affected base-document contract: Inaccurate Description of Authorization Decision.", + "id": "N-SAML-IDP-ERRATUM-SAMLCORE-E15", + "statement": "Apply Approved Errata 05 E15 to the affected base-document contract: NameID Policy Adherence.", "norm": { "document": "SAMLCORE", - "clause": "2", + "clause": "3.4.1.1", "level": "normative", "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf", "errata": [ - "E13" + "E15" ] }, "applies": { @@ -834230,15 +841543,15 @@ "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { - "id": "N-SAML-IDP-ERRATUM-SAMLCORE-E14", - "statement": "Apply Approved Errata 05 E14 to the affected base-document contract: AllowCreate.", + "id": "N-SAML-IDP-ERRATUM-SAMLCORE-E30", + "statement": "Apply Approved Errata 05 E30 to the affected base-document contract: Key Replacement.", "norm": { "document": "SAMLCORE", - "clause": "3.4.1.1", + "clause": "6.1", "level": "normative", "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf", "errata": [ - "E14" + "E30" ] }, "applies": { @@ -834285,15 +841598,15 @@ "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { - "id": "N-SAML-IDP-ERRATUM-SAMLCORE-E15", - "statement": "Apply Approved Errata 05 E15 to the affected base-document contract: NameID Policy Adherence.", + "id": "N-SAML-IDP-ERRATUM-SAMLCORE-E36", + "statement": "Apply Approved Errata 05 E36 to the affected base-document contract: Clarification on Action Element.", "norm": { "document": "SAMLCORE", - "clause": "3.4.1.1", + "clause": "2.7.4.2", "level": "normative", "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf", "errata": [ - "E15" + "E36" ] }, "applies": { @@ -834340,96 +841653,61 @@ "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { - "id": "N-SAML-IDP-ERRATUM-SAMLCORE-E30", - "statement": "Apply Approved Errata 05 E30 to the affected base-document contract: Key Replacement.", + "id": "N-SAML-IDP-ERRATUM-SAMLCORE-E38", + "statement": "Apply Approved Errata 05 E38 to the affected base-document contract: Clarification Regarding Index on .", "norm": { "document": "SAMLCORE", - "clause": "6.1", + "clause": "3.7.1", "level": "normative", "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf", "errata": [ - "E30" + "E38" ] }, "applies": { "profile": "saml-idp", "role": "SAML IdP", - "mode": "websso", + "mode": "logout", "status": "applicable", "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { "status": "limited", "apis": [ - "saml/idp.NewIdentityProvider", - "saml/idp.IdentityProvider.AcceptRedirect", - "saml/idp.IdentityProvider.Issue" + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" ], "code": [ - "saml/idp/idp.go" + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] - }, - "interop": { - "status": "pending", - "tests": [], - "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." - }, - "policy": { - "default": { - "status": "enabled", - "reason": "Available within the explicitly documented bounded API and its algorithm/resource/trust settings." - }, - "compatibility": { - "status": "not-applicable", - "reason": "No general compatibility switch supplies missing normative behavior." - } - }, - "gap": "The correction is selected for this base document and role, but complete corrected-clause implementation and independent evidence remain open.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." - }, - { - "id": "N-SAML-IDP-ERRATUM-SAMLCORE-E36", - "statement": "Apply Approved Errata 05 E36 to the affected base-document contract: Clarification on Action Element.", - "norm": { - "document": "SAMLCORE", - "clause": "2.7.4.2", - "level": "normative", - "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf", - "errata": [ - "E36" - ] - }, - "applies": { - "profile": "saml-idp", - "role": "SAML IdP", - "mode": "websso", - "status": "applicable", - "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." - }, - "implementation": { - "status": "limited", - "apis": [ - "saml/idp.NewIdentityProvider", - "saml/idp.IdentityProvider.AcceptRedirect", - "saml/idp.IdentityProvider.Issue" + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + } ], - "code": [ - "saml/idp/idp.go" + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/session/session_test.go", + "name": "TestLogoutMarkersSurviveIdentifierChange" + } ] }, - "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] - }, "interop": { "status": "pending", "tests": [], @@ -834439,63 +841717,14 @@ "policy": { "default": { "status": "enabled", - "reason": "Available within the explicitly documented bounded API and its algorithm/resource/trust settings." - }, - "compatibility": { - "status": "not-applicable", - "reason": "No general compatibility switch supplies missing normative behavior." - } - }, - "gap": "The correction is selected for this base document and role, but complete corrected-clause implementation and independent evidence remain open.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." - }, - { - "id": "N-SAML-IDP-ERRATUM-SAMLCORE-E38", - "statement": "Apply Approved Errata 05 E38 to the affected base-document contract: Clarification Regarding Index on .", - "norm": { - "document": "SAMLCORE", - "clause": "3.7.1", - "level": "normative", - "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf", - "errata": [ - "E38" - ] - }, - "applies": { - "profile": "saml-idp", - "role": "SAML IdP", - "mode": "logout", - "status": "applicable", - "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." - }, - "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] - }, - "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] - }, - "interop": { - "status": "pending", - "tests": [], - "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." - }, - "policy": { - "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The corrected encrypted or additional protocol/profile workflow is unimplemented; selecting an erratum does not implement it.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -834787,15 +842016,37 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Manage", + "saml/session.Service.BeginManage", + "saml/session.Service.Complete" + ], + "code": [ + "saml/session/session.go", + "saml/session/reply.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/exchange_test.go", + "name": "TestManageNameIDBothDirectionsAndBindings" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestManageAliasTerminationAndAtomicReplay" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestControlSigningFailureDoesNotCommitState" + } + ] }, "interop": { "status": "pending", @@ -834805,15 +842056,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The corrected encrypted or additional protocol/profile workflow is unimplemented; selecting an erratum does not implement it.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -835710,15 +842961,36 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Respond", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestInvalidArtifactReturnsEmptySuccess" + } + ] }, "interop": { "status": "pending", @@ -835728,15 +843000,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The corrected encrypted or additional protocol/profile workflow is unimplemented; selecting an erratum does not implement it.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -835759,15 +843031,36 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Respond", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestInvalidArtifactReturnsEmptySuccess" + } + ] }, "interop": { "status": "pending", @@ -835777,15 +843070,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The corrected encrypted or additional protocol/profile workflow is unimplemented; selecting an erratum does not implement it.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -835918,15 +843211,44 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -835936,15 +843258,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The corrected encrypted or additional protocol/profile workflow is unimplemented; selecting an erratum does not implement it.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -836132,15 +843454,36 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/artifact.Issuer.Respond", + "saml/artifact.Resolver.Resolve" + ], + "code": [ + "saml/artifact/artifact.go", + "saml/binding/artifact.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/sp/artifact_test.go", + "name": "TestArtifactWebSSOBothRoles" + } + ], + "negative": [ + { + "file": "saml/artifact/artifact_test.go", + "name": "TestArtifactAtomicRecipientAndExpiry" + }, + { + "file": "saml/artifact/artifact_test.go", + "name": "TestInvalidArtifactReturnsEmptySuccess" + } + ] }, "interop": { "status": "pending", @@ -836150,15 +843493,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The corrected encrypted or additional protocol/profile workflow is unimplemented; selecting an erratum does not implement it.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -836456,15 +843799,44 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -836474,15 +843846,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The corrected encrypted or additional protocol/profile workflow is unimplemented; selecting an erratum does not implement it.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -836505,15 +843877,44 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -836523,15 +843924,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The corrected encrypted or additional protocol/profile workflow is unimplemented; selecting an erratum does not implement it.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -836554,15 +843955,44 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -836572,15 +844002,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The corrected encrypted or additional protocol/profile workflow is unimplemented; selecting an erratum does not implement it.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -836658,15 +844088,44 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -836676,15 +844135,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The corrected encrypted or additional protocol/profile workflow is unimplemented; selecting an erratum does not implement it.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -836817,15 +844276,41 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Logout", + "saml/session.Service.BeginLogout", + "saml/session.BackChannel.Logout" + ], + "code": [ + "saml/session/session.go", + "saml/session/soap.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/session_test.go", + "name": "TestAuthorityFanoutPartialAndInitiation" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestIdPInitiatedRedirectLogout" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestParticipantLogoutAndLateAssertions" + }, + { + "file": "saml/session/session_test.go", + "name": "TestLogoutMarkersSurviveIdentifierChange" + } + ] }, "interop": { "status": "pending", @@ -836835,15 +844320,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The corrected encrypted or additional protocol/profile workflow is unimplemented; selecting an erratum does not implement it.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -837288,15 +844773,44 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/ecp.ParseChallenge", + "saml/ecp.Exchange.Finish", + "saml/sp.ServiceProvider.ConsumePAOS", + "saml/idp.IdentityProvider.AcceptSOAP" + ], + "code": [ + "saml/ecp/ecp.go", + "saml/ecp/http.go", + "saml/ecp/channel.go", + "saml/sp/sp.go", + "saml/idp/idp.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/ecp/roles_test.go", + "name": "TestECPServiceProviderIdentityProvider" + } + ], + "negative": [ + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPRoutingAndSignedBodies" + }, + { + "file": "saml/ecp/ecp_test.go", + "name": "TestECPIndependentHeaders" + }, + { + "file": "saml/ecp/channel_test.go", + "name": "TestECPChannelHeadersAndComparison" + } + ] }, "interop": { "status": "pending", @@ -837306,15 +844820,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The corrected encrypted or additional protocol/profile workflow is unimplemented; selecting an erratum does not implement it.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -837337,15 +844851,37 @@ "reason": "Applies to this selected SAML IdP operation, including the role-specific producer/consumer duties and explicitly stated feature conditions. A peer role's independent duties are not transferred to this role. Missing mandatory workflows remain applicable." }, "implementation": { - "status": "unimplemented", - "apis": [], - "code": [] + "status": "limited", + "apis": [ + "saml/session.Service.Manage", + "saml/session.Service.BeginManage", + "saml/session.Service.Complete" + ], + "code": [ + "saml/session/session.go", + "saml/session/reply.go", + "saml/protocol/control.go" + ] }, "tests": { - "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "status": "partial", + "reason": "The linked tests exercise the implemented 2005/Approved Errata 05 workflow and selected malformed, correlation, state and transport cases. They are partial evidence for this clause, not exhaustive coverage of every rule or optional extension.", + "positive": [ + { + "file": "saml/session/exchange_test.go", + "name": "TestManageNameIDBothDirectionsAndBindings" + } + ], + "negative": [ + { + "file": "saml/session/session_test.go", + "name": "TestManageAliasTerminationAndAtomicReplay" + }, + { + "file": "saml/session/exchange_test.go", + "name": "TestControlSigningFailureDoesNotCommitState" + } + ] }, "interop": { "status": "pending", @@ -837355,15 +844891,15 @@ }, "policy": { "default": { - "status": "unavailable", - "reason": "The implementation is absent; policy cannot enable it." + "status": "enabled", + "reason": "Explicit APIs require trusted partner, endpoint, credentials, time and bounded state; application authentication, session authorization and durable adapters remain deployment responsibilities." }, "compatibility": { - "status": "unavailable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Legacy algorithms remain behind explicit existing policies. No compatibility mode bypasses authentication, correlation or single use." } }, - "gap": "The corrected encrypted or additional protocol/profile workflow is unimplemented; selecting an erratum does not implement it.", + "gap": "Implemented bounded 2005/Approved Errata 05 processing replaces the former missing workflow. Complete independent clause-level/profile interoperability and coverage of every optional extension remain unverified. See docs/saml.md for precise API and deployment boundaries.", "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { @@ -854197,310 +861733,1442 @@ "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." }, { - "id": "N-CMS-GENERATE-CONTENT", - "statement": "Bind the declared content type and detached/encapsulated payload choice to the original content octets.", + "id": "N-CMS-GENERATE-CONTENT", + "statement": "Bind the declared content type and detached/encapsulated payload choice to the original content octets.", + "norm": { + "document": "RFC5652", + "clause": "5.2", + "level": "normative", + "source": "https://www.rfc-editor.org/rfc/rfc5652.html#section-5.2", + "errata": [] + }, + "applies": { + "profile": "cms", + "role": "CMS implementation", + "mode": "generate", + "status": "applicable", + "reason": "Applies to the selected standard role and operation. Missing mandatory behavior stays applicable; conditions on optional formats/algorithms do not turn required selected-role duties into policy exemptions." + }, + "implementation": { + "status": "limited", + "apis": [ + "cms.Sign" + ], + "code": [ + "cms/sign.go" + ] + }, + "tests": { + "status": "pending", + "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", + "positive": [], + "negative": [] + }, + "interop": { + "status": "pending", + "tests": [], + "fixtures": [], + "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + }, + "policy": { + "default": { + "status": "enabled", + "reason": "Available within the explicitly documented bounded API and its algorithm/resource/trust settings." + }, + "compatibility": { + "status": "not-applicable", + "reason": "No general compatibility switch supplies missing normative behavior." + } + }, + "gap": "Only the closed DER SignedData single-signer profile is generated. General BER/content/algorithm/attribute combinations and complete independent generation evidence remain open.", + "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + }, + { + "id": "N-CMS-GENERATE-SIGNERINFO", + "statement": "Encode SignerInfo version, signer identifier, algorithms, signed attributes and signature consistently.", + "norm": { + "document": "RFC5652", + "clause": "5.3", + "level": "normative", + "source": "https://www.rfc-editor.org/rfc/rfc5652.html#section-5.3", + "errata": [] + }, + "applies": { + "profile": "cms", + "role": "CMS implementation", + "mode": "generate", + "status": "applicable", + "reason": "Applies to the selected standard role and operation. Missing mandatory behavior stays applicable; conditions on optional formats/algorithms do not turn required selected-role duties into policy exemptions." + }, + "implementation": { + "status": "limited", + "apis": [ + "cms.Sign" + ], + "code": [ + "cms/sign.go" + ] + }, + "tests": { + "status": "pending", + "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", + "positive": [], + "negative": [] + }, + "interop": { + "status": "pending", + "tests": [], + "fixtures": [], + "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + }, + "policy": { + "default": { + "status": "enabled", + "reason": "Available within the explicitly documented bounded API and its algorithm/resource/trust settings." + }, + "compatibility": { + "status": "not-applicable", + "reason": "No general compatibility switch supplies missing normative behavior." + } + }, + "gap": "Only the closed DER SignedData single-signer profile is generated. General BER/content/algorithm/attribute combinations and complete independent generation evidence remain open.", + "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + }, + { + "id": "N-CMS-GENERATE-DIGEST", + "statement": "Compute message digests over the exact eContent octets or supplied detached bytes.", + "norm": { + "document": "RFC5652", + "clause": "5.4", + "level": "normative", + "source": "https://www.rfc-editor.org/rfc/rfc5652.html#section-5.4", + "errata": [] + }, + "applies": { + "profile": "cms", + "role": "CMS implementation", + "mode": "generate", + "status": "applicable", + "reason": "Applies to the selected standard role and operation. Missing mandatory behavior stays applicable; conditions on optional formats/algorithms do not turn required selected-role duties into policy exemptions." + }, + "implementation": { + "status": "limited", + "apis": [ + "cms.Sign" + ], + "code": [ + "cms/sign.go" + ] + }, + "tests": { + "status": "pending", + "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", + "positive": [], + "negative": [] + }, + "interop": { + "status": "pending", + "tests": [], + "fixtures": [], + "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + }, + "policy": { + "default": { + "status": "enabled", + "reason": "Available within the explicitly documented bounded API and its algorithm/resource/trust settings." + }, + "compatibility": { + "status": "not-applicable", + "reason": "No general compatibility switch supplies missing normative behavior." + } + }, + "gap": "Only the closed DER SignedData single-signer profile is generated. General BER/content/algorithm/attribute combinations and complete independent generation evidence remain open.", + "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + }, + { + "id": "N-CMS-GENERATE-SIGNATURE", + "statement": "Sign the exact content or canonical DER SET OF signed attributes as required by the selected content type.", + "norm": { + "document": "RFC5652", + "clause": "5.5", + "level": "normative", + "source": "https://www.rfc-editor.org/rfc/rfc5652.html#section-5.5", + "errata": [] + }, + "applies": { + "profile": "cms", + "role": "CMS implementation", + "mode": "generate", + "status": "applicable", + "reason": "Applies to the selected standard role and operation. Missing mandatory behavior stays applicable; conditions on optional formats/algorithms do not turn required selected-role duties into policy exemptions." + }, + "implementation": { + "status": "limited", + "apis": [ + "cms.Sign" + ], + "code": [ + "cms/sign.go" + ] + }, + "tests": { + "status": "pending", + "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", + "positive": [], + "negative": [] + }, + "interop": { + "status": "pending", + "tests": [], + "fixtures": [], + "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + }, + "policy": { + "default": { + "status": "enabled", + "reason": "Available within the explicitly documented bounded API and its algorithm/resource/trust settings." + }, + "compatibility": { + "status": "not-applicable", + "reason": "No general compatibility switch supplies missing normative behavior." + } + }, + "gap": "Only the closed DER SignedData single-signer profile is generated. General BER/content/algorithm/attribute combinations and complete independent generation evidence remain open.", + "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + }, + { + "id": "N-CMS-GENERATE-CONTENT-TYPE", + "statement": "When signed attributes are used, include exactly one correctly typed content-type value matching encapContentInfo.", + "norm": { + "document": "RFC5652", + "clause": "11.1", + "level": "normative", + "source": "https://www.rfc-editor.org/rfc/rfc5652.html#section-11.1", + "errata": [] + }, + "applies": { + "profile": "cms", + "role": "CMS implementation", + "mode": "generate", + "status": "applicable", + "reason": "Applies to the selected standard role and operation. Missing mandatory behavior stays applicable; conditions on optional formats/algorithms do not turn required selected-role duties into policy exemptions." + }, + "implementation": { + "status": "limited", + "apis": [ + "cms.Sign" + ], + "code": [ + "cms/sign.go" + ] + }, + "tests": { + "status": "pending", + "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", + "positive": [], + "negative": [] + }, + "interop": { + "status": "pending", + "tests": [], + "fixtures": [], + "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + }, + "policy": { + "default": { + "status": "enabled", + "reason": "Available within the explicitly documented bounded API and its algorithm/resource/trust settings." + }, + "compatibility": { + "status": "not-applicable", + "reason": "No general compatibility switch supplies missing normative behavior." + } + }, + "gap": "Only the closed DER SignedData single-signer profile is generated. General BER/content/algorithm/attribute combinations and complete independent generation evidence remain open.", + "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + }, + { + "id": "N-CMS-GENERATE-MESSAGE-DIGEST", + "statement": "When signed attributes are used, include exactly one message-digest value for the exact signed content.", + "norm": { + "document": "RFC5652", + "clause": "11.2", + "level": "normative", + "source": "https://www.rfc-editor.org/rfc/rfc5652.html#section-11.2", + "errata": [] + }, + "applies": { + "profile": "cms", + "role": "CMS implementation", + "mode": "generate", + "status": "applicable", + "reason": "Applies to the selected standard role and operation. Missing mandatory behavior stays applicable; conditions on optional formats/algorithms do not turn required selected-role duties into policy exemptions." + }, + "implementation": { + "status": "limited", + "apis": [ + "cms.Sign" + ], + "code": [ + "cms/sign.go" + ] + }, + "tests": { + "status": "pending", + "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", + "positive": [], + "negative": [] + }, + "interop": { + "status": "pending", + "tests": [], + "fixtures": [], + "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + }, + "policy": { + "default": { + "status": "enabled", + "reason": "Available within the explicitly documented bounded API and its algorithm/resource/trust settings." + }, + "compatibility": { + "status": "not-applicable", + "reason": "No general compatibility switch supplies missing normative behavior." + } + }, + "gap": "Only the closed DER SignedData single-signer profile is generated. General BER/content/algorithm/attribute combinations and complete independent generation evidence remain open.", + "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + }, + { + "id": "N-SAML-AUTHORITY-QUERY-MATRIX", + "statement": "Implement the corresponding authentication, attribute and authorization queries, assertion-ID SOAP requests and HTTP URI retrieval for the selected Authority modes.", "norm": { - "document": "RFC5652", - "clause": "5.2", + "document": "SAMLCONF", + "clause": "3.2 Tables 3-4", "level": "normative", - "source": "https://www.rfc-editor.org/rfc/rfc5652.html#section-5.2", + "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-conformance-2.0-os.pdf", "errata": [] }, "applies": { - "profile": "cms", - "role": "CMS implementation", - "mode": "generate", + "profile": "saml-authority", + "role": "SAML authorities and requester", + "mode": "query", "status": "applicable", - "reason": "Applies to the selected standard role and operation. Missing mandatory behavior stays applicable; conditions on optional formats/algorithms do not turn required selected-role duties into policy exemptions." + "reason": "Applies to the explicitly selected query, retrieval or mapping operation; it does not claim all Extended-provider proxying duties." }, "implementation": { "status": "limited", "apis": [ - "cms.Sign" + "saml/protocol.SignQuery", + "saml/protocol.VerifyQuery", + "saml/protocol.SignQueryResponse", + "saml/protocol.VerifyQueryResponse", + "saml/authority.NewService" ], "code": [ - "cms/sign.go" + "saml/authority/authority.go", + "saml/authority/client.go", + "saml/authority/uri.go", + "saml/protocol/query.go", + "saml/protocol/query_response.go", + "saml/protocol/mapping.go", + "saml/binding/uri.go", + "saml/metadata/authority.go" ] }, "tests": { + "status": "partial", + "reason": "Named tests exercise positive, negative and boundary contracts of the implemented profile; unrestricted datatype/extension and full-role coverage is not claimed.", + "positive": [ + { + "file": "saml/protocol/query_test.go", + "name": "TestAuthorityQueries" + }, + { + "file": "saml/authority/authority_test.go", + "name": "TestAuthoritySOAPAndURI" + } + ], + "negative": [ + { + "file": "saml/protocol/query_test.go", + "name": "TestQueryFiltersRejectDisclosure" + } + ] + }, + "interop": { "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "tests": [ + { + "file": "tests/interop/saml_authority_test.go", + "name": "TestOpenSAMLAuthorityContracts" + }, + { + "file": "tests/interop/saml_openam_http_test.go", + "name": "TestOpenAMAuthorityHTTP" + }, + { + "file": "tests/interop/saml_gsa_http_test.go", + "name": "TestGoogleGSAAuthorityHTTP" + } + ], + "fixtures": [ + "tests/interop/testdata/OpenSAMLAuthorityOracle.java" + ], + "reason": "Released OpenSAML 5.2.3 independently builds, unmarshals, signs and schema-validates all five message families in both directions. This does not certify every condition, cipher, transport or full deployed Authority role. Deployed OpenAM 16.1.4 adds exact-context/session AuthnQuery and exact-ID AssertionIDRequest using independently signed WebSSO assertions, including empty unmatched results. Other context orderings and confirmation forms remain outside this live-server evidence. Deployed Google GSA adaptor 4.1.4 adds TLS-authenticated AuthzDecisionQuery wire cases, with no signed XML acceptance claim. OpenAM Mapping uses the disclosed test-only serializer correction." + }, + "policy": { + "default": { + "status": "enabled", + "reason": "Explicitly configured authority/metadata APIs require signatures, trusted peers, authorization adapters and bounded inputs." + }, + "compatibility": { + "status": "opt-in", + "reason": "Existing explicit legacy cryptographic policies apply. Raw unauthenticated CBC/RSA-v1.5 decryption is not enabled." + } + }, + "gap": "Full per-clause evidence remains open beyond the named tests. Deployed SOAP evidence and peer/profile limits are documented in docs/saml-authority.md; arbitrary BaseID extensions, automatic proxying and unrestricted full-role conformance are not claimed.", + "closure": "Retain tested protocol behavior and close remaining common XML, algorithm, transport and independent full-role evidence before promoting to unrestricted conformance." + }, + { + "id": "N-SAML-AUTHORITY-AUTHN-QUERY", + "statement": "Return statements about previous authentication acts; enforce session and requested-context constraints across the returned set without initiating a login.", + "norm": { + "document": "SAMLCORE", + "clause": "3.3.2.2", + "level": "normative", + "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf", + "errata": [] + }, + "applies": { + "profile": "saml-authority", + "role": "SAML authorities and requester", + "mode": "query", + "status": "applicable", + "reason": "Applies to the explicitly selected query, retrieval or mapping operation; it does not claim all Extended-provider proxying duties." + }, + "implementation": { + "status": "limited", + "apis": [ + "saml/protocol.SignQuery", + "saml/protocol.VerifyQuery", + "saml/protocol.SignQueryResponse", + "saml/protocol.VerifyQueryResponse", + "saml/authority.NewService" + ], + "code": [ + "saml/authority/authority.go", + "saml/authority/client.go", + "saml/authority/uri.go", + "saml/protocol/query.go", + "saml/protocol/query_response.go", + "saml/protocol/mapping.go", + "saml/binding/uri.go", + "saml/metadata/authority.go" + ] + }, + "tests": { + "status": "partial", + "reason": "Named tests exercise positive, negative and boundary contracts of the implemented profile; unrestricted datatype/extension and full-role coverage is not claimed.", + "positive": [ + { + "file": "saml/protocol/query_test.go", + "name": "TestAuthorityQueries" + } + ], + "negative": [ + { + "file": "saml/protocol/query_test.go", + "name": "TestQueryFiltersRejectDisclosure" + } + ] }, "interop": { "status": "pending", - "tests": [], - "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "tests": [ + { + "file": "tests/interop/saml_authority_test.go", + "name": "TestOpenSAMLAuthorityContracts" + }, + { + "file": "tests/interop/saml_openam_http_test.go", + "name": "TestOpenAMAuthorityHTTP" + } + ], + "fixtures": [ + "tests/interop/testdata/OpenSAMLAuthorityOracle.java" + ], + "reason": "Released OpenSAML 5.2.3 independently builds, unmarshals, signs and schema-validates all five message families in both directions. This does not certify every condition, cipher, transport or full deployed Authority role. Deployed OpenAM 16.1.4 independently handles signed SOAP/HTTP requests using its actual WebSSO-issued assertion store, with session/context constraints and empty unknown-session/ID results. The released test WAR carries a separate disclosed Mapping serializer correction; query processing is unchanged." }, "policy": { "default": { "status": "enabled", - "reason": "Available within the explicitly documented bounded API and its algorithm/resource/trust settings." + "reason": "Explicitly configured authority/metadata APIs require signatures, trusted peers, authorization adapters and bounded inputs." }, "compatibility": { - "status": "not-applicable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Existing explicit legacy cryptographic policies apply. Raw unauthenticated CBC/RSA-v1.5 decryption is not enabled." } }, - "gap": "Only the closed DER SignedData single-signer profile is generated. General BER/content/algorithm/attribute combinations and complete independent generation evidence remain open.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Named deployed-server evidence now exists, with explicit peer/profile limitations in docs/saml-authority.md. Full per-clause, arbitrary datatype/extension, algorithm and full-role conformance remains unverified; no unrestricted conformance is claimed.", + "closure": "Retain tested protocol behavior and close remaining common XML, algorithm, transport and independent full-role evidence before promoting to unrestricted conformance." }, { - "id": "N-CMS-GENERATE-SIGNERINFO", - "statement": "Encode SignerInfo version, signer identifier, algorithms, signed attributes and signature consistently.", + "id": "N-SAML-AUTHORITY-ATTRIBUTE-QUERY", + "statement": "Identify attributes by Name and NameFormat, reject duplicate requested attributes and constrain returned attribute names and XML values.", "norm": { - "document": "RFC5652", - "clause": "5.3", + "document": "SAMLCORE", + "clause": "3.3.2.3", "level": "normative", - "source": "https://www.rfc-editor.org/rfc/rfc5652.html#section-5.3", + "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf", "errata": [] }, "applies": { - "profile": "cms", - "role": "CMS implementation", - "mode": "generate", + "profile": "saml-authority", + "role": "SAML authorities and requester", + "mode": "query", "status": "applicable", - "reason": "Applies to the selected standard role and operation. Missing mandatory behavior stays applicable; conditions on optional formats/algorithms do not turn required selected-role duties into policy exemptions." + "reason": "Applies to the explicitly selected query, retrieval or mapping operation; it does not claim all Extended-provider proxying duties." }, "implementation": { "status": "limited", "apis": [ - "cms.Sign" + "saml/protocol.SignQuery", + "saml/protocol.VerifyQuery", + "saml/protocol.SignQueryResponse", + "saml/protocol.VerifyQueryResponse", + "saml/authority.NewService" ], "code": [ - "cms/sign.go" + "saml/authority/authority.go", + "saml/authority/client.go", + "saml/authority/uri.go", + "saml/protocol/query.go", + "saml/protocol/query_response.go", + "saml/protocol/mapping.go", + "saml/binding/uri.go", + "saml/metadata/authority.go" ] }, "tests": { + "status": "partial", + "reason": "Named tests exercise positive, negative and boundary contracts of the implemented profile; unrestricted datatype/extension and full-role coverage is not claimed.", + "positive": [ + { + "file": "saml/protocol/query_test.go", + "name": "TestAuthorityQueries" + } + ], + "negative": [ + { + "file": "saml/protocol/query_test.go", + "name": "TestQueryFiltersRejectDisclosure" + }, + { + "file": "saml/protocol/query_test.go", + "name": "TestAuthorityNamespaceAndValueIntegrity" + } + ] + }, + "interop": { "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "tests": [ + { + "file": "tests/interop/saml_authority_test.go", + "name": "TestOpenSAMLAuthorityContracts" + }, + { + "file": "tests/interop/saml_authority_http_test.go", + "name": "TestShibbolethAuthorityHTTP" + } + ], + "fixtures": [ + "tests/interop/testdata/OpenSAMLAuthorityOracle.java" + ], + "reason": "OpenSAML 5.2.3 independently generates and validates the five message families. TestShibbolethAuthorityHTTP additionally deploys the unmodified pinned Shibboleth IdP 5.2.3 WAR in Jetty 12.1.14 and tests the production client against actual verified HTTPS/SOAP: signed/encrypted AttributeQuery, exact attribute selection, replay and untrusted-key rejection. Early security errors are authenticated by TLS; unsigned error envelopes remain rejected by the Go message-signature profile. Full clause-wide independent coverage of every optional condition/value/transport form remains pending. Deployed Shibboleth IdP 5.2.3 already provides signed/encrypted AttributeQuery with an exact filter, plus replay/untrusted-key rejection over pinned HTTPS. Other value profiles are not established by that test." + }, + "policy": { + "default": { + "status": "enabled", + "reason": "Explicitly configured authority/metadata APIs require signatures, trusted peers, authorization adapters and bounded inputs." + }, + "compatibility": { + "status": "opt-in", + "reason": "Existing explicit legacy cryptographic policies apply. Raw unauthenticated CBC/RSA-v1.5 decryption is not enabled." + } + }, + "gap": "Full per-clause evidence remains open beyond the named tests. Deployed SOAP evidence and peer/profile limits are documented in docs/saml-authority.md; arbitrary BaseID extensions, automatic proxying and unrestricted full-role conformance are not claimed.", + "closure": "Retain tested protocol behavior and close remaining common XML, algorithm, transport and independent full-role evidence before promoting to unrestricted conformance." + }, + { + "id": "N-SAML-AUTHORITY-AUTHZ-QUERY", + "statement": "Authenticate authorization queries containing a subject, resource and actions; return corresponding decision statements without trusting unverified Evidence.", + "norm": { + "document": "SAMLCORE", + "clause": "3.3.2.4", + "level": "normative", + "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf", + "errata": [] + }, + "applies": { + "profile": "saml-authority", + "role": "SAML authorities and requester", + "mode": "query", + "status": "applicable", + "reason": "Applies to the explicitly selected query, retrieval or mapping operation; it does not claim all Extended-provider proxying duties." + }, + "implementation": { + "status": "limited", + "apis": [ + "saml/protocol.SignQuery", + "saml/protocol.VerifyQuery", + "saml/protocol.SignQueryResponse", + "saml/protocol.VerifyQueryResponse", + "saml/authority.NewService" + ], + "code": [ + "saml/authority/authority.go", + "saml/authority/client.go", + "saml/authority/uri.go", + "saml/protocol/query.go", + "saml/protocol/query_response.go", + "saml/protocol/mapping.go", + "saml/binding/uri.go", + "saml/metadata/authority.go" + ] + }, + "tests": { + "status": "partial", + "reason": "Named tests exercise positive, negative and boundary contracts of the implemented profile; unrestricted datatype/extension and full-role coverage is not claimed.", + "positive": [ + { + "file": "saml/protocol/query_test.go", + "name": "TestAuthorityQueries" + } + ], + "negative": [ + { + "file": "saml/protocol/query_test.go", + "name": "TestQueryFiltersRejectDisclosure" + } + ] }, "interop": { "status": "pending", - "tests": [], - "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "tests": [ + { + "file": "tests/interop/saml_authority_test.go", + "name": "TestOpenSAMLAuthorityContracts" + }, + { + "file": "tests/interop/saml_gsa_http_test.go", + "name": "TestGoogleGSAAuthorityHTTP" + } + ], + "fixtures": [ + "tests/interop/testdata/OpenSAMLAuthorityOracle.java" + ], + "reason": "Released OpenSAML 5.2.3 independently builds, unmarshals, signs and schema-validates all five message families in both directions. This does not certify every condition, cipher, transport or full deployed Authority role. Deployed Google GSA adaptor 4.1.4 independently handles HTTPS/SOAP AuthzDecisionQuery with inherited ACL Permit/Deny and unknown/foreign resource cases. Its unsigned XML is checked over pinned TLS and rejected by the strict signature verifier; no signed-response acceptance is claimed." }, "policy": { "default": { "status": "enabled", - "reason": "Available within the explicitly documented bounded API and its algorithm/resource/trust settings." + "reason": "Explicitly configured authority/metadata APIs require signatures, trusted peers, authorization adapters and bounded inputs." }, "compatibility": { - "status": "not-applicable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Existing explicit legacy cryptographic policies apply. Raw unauthenticated CBC/RSA-v1.5 decryption is not enabled." } }, - "gap": "Only the closed DER SignedData single-signer profile is generated. General BER/content/algorithm/attribute combinations and complete independent generation evidence remain open.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Named deployed-server evidence now exists, with explicit peer/profile limitations in docs/saml-authority.md. Full per-clause, arbitrary datatype/extension, algorithm and full-role conformance remains unverified; no unrestricted conformance is claimed.", + "closure": "Retain tested protocol behavior and close remaining common XML, algorithm, transport and independent full-role evidence before promoting to unrestricted conformance." }, { - "id": "N-CMS-GENERATE-DIGEST", - "statement": "Compute message digests over the exact eContent octets or supplied detached bytes.", + "id": "N-SAML-AUTHORITY-ASSERTION-ID", + "statement": "Request and return only explicitly referenced existing assertions by exact ID.", "norm": { - "document": "RFC5652", - "clause": "5.4", + "document": "SAMLCORE", + "clause": "3.3.1", "level": "normative", - "source": "https://www.rfc-editor.org/rfc/rfc5652.html#section-5.4", + "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf", "errata": [] }, "applies": { - "profile": "cms", - "role": "CMS implementation", - "mode": "generate", + "profile": "saml-authority", + "role": "SAML authorities and requester", + "mode": "assertion-id", "status": "applicable", - "reason": "Applies to the selected standard role and operation. Missing mandatory behavior stays applicable; conditions on optional formats/algorithms do not turn required selected-role duties into policy exemptions." + "reason": "Applies to the explicitly selected query, retrieval or mapping operation; it does not claim all Extended-provider proxying duties." }, "implementation": { "status": "limited", "apis": [ - "cms.Sign" + "saml/protocol.SignQuery", + "saml/protocol.VerifyQuery", + "saml/protocol.SignQueryResponse", + "saml/protocol.VerifyQueryResponse", + "saml/authority.NewService" ], "code": [ - "cms/sign.go" + "saml/authority/authority.go", + "saml/authority/client.go", + "saml/authority/uri.go", + "saml/protocol/query.go", + "saml/protocol/query_response.go", + "saml/protocol/mapping.go", + "saml/binding/uri.go", + "saml/metadata/authority.go" ] }, "tests": { + "status": "partial", + "reason": "Named tests exercise positive, negative and boundary contracts of the implemented profile; unrestricted datatype/extension and full-role coverage is not claimed.", + "positive": [ + { + "file": "saml/protocol/query_test.go", + "name": "TestAuthorityQueries" + }, + { + "file": "saml/authority/authority_test.go", + "name": "TestAuthoritySOAPAndURI" + } + ], + "negative": [ + { + "file": "saml/protocol/query_test.go", + "name": "TestQueryFiltersRejectDisclosure" + } + ] + }, + "interop": { "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "tests": [ + { + "file": "tests/interop/saml_authority_test.go", + "name": "TestOpenSAMLAuthorityContracts" + }, + { + "file": "tests/interop/saml_openam_http_test.go", + "name": "TestOpenAMAuthorityHTTP" + } + ], + "fixtures": [ + "tests/interop/testdata/OpenSAMLAuthorityOracle.java" + ], + "reason": "Released OpenSAML 5.2.3 independently builds, unmarshals, signs and schema-validates all five message families in both directions. This does not certify every condition, cipher, transport or full deployed Authority role. Deployed OpenAM 16.1.4 independently handles signed SOAP/HTTP requests using its actual WebSSO-issued assertion store, with session/context constraints and empty unknown-session/ID results. The released test WAR carries a separate disclosed Mapping serializer correction; query processing is unchanged." + }, + "policy": { + "default": { + "status": "enabled", + "reason": "Explicitly configured authority/metadata APIs require signatures, trusted peers, authorization adapters and bounded inputs." + }, + "compatibility": { + "status": "opt-in", + "reason": "Existing explicit legacy cryptographic policies apply. Raw unauthenticated CBC/RSA-v1.5 decryption is not enabled." + } + }, + "gap": "Named deployed-server evidence now exists, with explicit peer/profile limitations in docs/saml-authority.md. Full per-clause, arbitrary datatype/extension, algorithm and full-role conformance remains unverified; no unrestricted conformance is claimed.", + "closure": "Retain tested protocol behavior and close remaining common XML, algorithm, transport and independent full-role evidence before promoting to unrestricted conformance." + }, + { + "id": "N-SAML-AUTHORITY-QUERY-MATCH", + "statement": "Strongly match every returned subject; represent no matches as zero assertions with Success; do not infer confirmation compatibility from method names alone.", + "norm": { + "document": "SAMLCORE", + "clause": "3.3.4", + "level": "normative", + "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf", + "errata": [] + }, + "applies": { + "profile": "saml-authority", + "role": "SAML authorities and requester", + "mode": "query", + "status": "applicable", + "reason": "Applies to the explicitly selected query, retrieval or mapping operation; it does not claim all Extended-provider proxying duties." + }, + "implementation": { + "status": "limited", + "apis": [ + "saml/protocol.SignQuery", + "saml/protocol.VerifyQuery", + "saml/protocol.SignQueryResponse", + "saml/protocol.VerifyQueryResponse", + "saml/authority.NewService" + ], + "code": [ + "saml/authority/authority.go", + "saml/authority/client.go", + "saml/authority/uri.go", + "saml/protocol/query.go", + "saml/protocol/query_response.go", + "saml/protocol/mapping.go", + "saml/binding/uri.go", + "saml/metadata/authority.go" + ] + }, + "tests": { + "status": "partial", + "reason": "Named tests exercise positive, negative and boundary contracts of the implemented profile; unrestricted datatype/extension and full-role coverage is not claimed.", + "positive": [ + { + "file": "saml/protocol/query_test.go", + "name": "TestAuthorityQueries" + }, + { + "file": "saml/protocol/query_test.go", + "name": "TestQuerySubjectConfirmationAndExtensions" + } + ], + "negative": [ + { + "file": "saml/protocol/query_test.go", + "name": "TestQueryFiltersRejectDisclosure" + } + ] }, "interop": { "status": "pending", - "tests": [], - "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "tests": [ + { + "file": "tests/interop/saml_authority_test.go", + "name": "TestOpenSAMLAuthorityContracts" + }, + { + "file": "tests/interop/saml_authority_http_test.go", + "name": "TestShibbolethAuthorityHTTP" + }, + { + "file": "tests/interop/saml_openam_http_test.go", + "name": "TestOpenAMAuthorityHTTP" + } + ], + "fixtures": [ + "tests/interop/testdata/OpenSAMLAuthorityOracle.java" + ], + "reason": "OpenSAML 5.2.3 independently generates and validates the five message families. TestShibbolethAuthorityHTTP additionally deploys the unmodified pinned Shibboleth IdP 5.2.3 WAR in Jetty 12.1.14 and tests the production client against actual verified HTTPS/SOAP: signed/encrypted AttributeQuery, exact attribute selection, replay and untrusted-key rejection. Early security errors are authenticated by TLS; unsigned error envelopes remain rejected by the Go message-signature profile. Full clause-wide independent coverage of every optional condition/value/transport form remains pending. Deployed OpenAM 16.1.4 adds exact-context/session AuthnQuery and exact-ID AssertionIDRequest using independently signed WebSSO assertions, including empty unmatched results. Other context orderings and confirmation forms remain outside this live-server evidence." }, "policy": { "default": { "status": "enabled", - "reason": "Available within the explicitly documented bounded API and its algorithm/resource/trust settings." + "reason": "Explicitly configured authority/metadata APIs require signatures, trusted peers, authorization adapters and bounded inputs." }, "compatibility": { - "status": "not-applicable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Existing explicit legacy cryptographic policies apply. Raw unauthenticated CBC/RSA-v1.5 decryption is not enabled." } }, - "gap": "Only the closed DER SignedData single-signer profile is generated. General BER/content/algorithm/attribute combinations and complete independent generation evidence remain open.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Full per-clause evidence remains open beyond the named tests. Deployed SOAP evidence and peer/profile limits are documented in docs/saml-authority.md; arbitrary BaseID extensions, automatic proxying and unrestricted full-role conformance are not claimed.", + "closure": "Retain tested protocol behavior and close remaining common XML, algorithm, transport and independent full-role evidence before promoting to unrestricted conformance." }, { - "id": "N-CMS-GENERATE-SIGNATURE", - "statement": "Sign the exact content or canonical DER SET OF signed attributes as required by the selected content type.", + "id": "N-SAML-AUTHORITY-QUERY-CONTEXT", + "statement": "Support class/declaration references and exact/minimum/maximum/better comparison, with responder-defined strength and the corrected unordered AuthnQuery/better semantics in E45.", "norm": { - "document": "RFC5652", - "clause": "5.5", + "document": "SAMLCORE", + "clause": "3.3.2.2.1", "level": "normative", - "source": "https://www.rfc-editor.org/rfc/rfc5652.html#section-5.5", + "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf", + "errata": [ + "E45" + ] + }, + "applies": { + "profile": "saml-authority", + "role": "SAML authorities and requester", + "mode": "query", + "status": "applicable", + "reason": "Applies to the explicitly selected query, retrieval or mapping operation; it does not claim all Extended-provider proxying duties." + }, + "implementation": { + "status": "limited", + "apis": [ + "saml/protocol.SignQuery", + "saml/protocol.VerifyQuery", + "saml/protocol.SignQueryResponse", + "saml/protocol.VerifyQueryResponse", + "saml/authority.NewService" + ], + "code": [ + "saml/authority/authority.go", + "saml/authority/client.go", + "saml/authority/uri.go", + "saml/protocol/query.go", + "saml/protocol/query_response.go", + "saml/protocol/mapping.go", + "saml/binding/uri.go", + "saml/metadata/authority.go" + ] + }, + "tests": { + "status": "partial", + "reason": "Named tests exercise positive, negative and boundary contracts of the implemented profile; unrestricted datatype/extension and full-role coverage is not claimed.", + "positive": [ + { + "file": "saml/protocol/query_test.go", + "name": "TestAuthorityConditionsAndContexts" + } + ], + "negative": [ + { + "file": "saml/protocol/query_test.go", + "name": "TestQueryFiltersRejectDisclosure" + } + ] + }, + "interop": { + "status": "pending", + "tests": [ + { + "file": "tests/interop/saml_authority_test.go", + "name": "TestOpenSAMLAuthorityContracts" + }, + { + "file": "tests/interop/saml_openam_http_test.go", + "name": "TestOpenAMAuthorityHTTP" + } + ], + "fixtures": [ + "tests/interop/testdata/OpenSAMLAuthorityOracle.java" + ], + "reason": "Released OpenSAML 5.2.3 independently builds, unmarshals, signs and schema-validates all five message families in both directions. This does not certify every condition, cipher, transport or full deployed Authority role. Deployed OpenAM 16.1.4 adds exact-context/session AuthnQuery and exact-ID AssertionIDRequest using independently signed WebSSO assertions, including empty unmatched results. Other context orderings and confirmation forms remain outside this live-server evidence." + }, + "policy": { + "default": { + "status": "enabled", + "reason": "Explicitly configured authority/metadata APIs require signatures, trusted peers, authorization adapters and bounded inputs." + }, + "compatibility": { + "status": "opt-in", + "reason": "Existing explicit legacy cryptographic policies apply. Raw unauthenticated CBC/RSA-v1.5 decryption is not enabled." + } + }, + "gap": "Full per-clause evidence remains open beyond the named tests. Deployed SOAP evidence and peer/profile limits are documented in docs/saml-authority.md; arbitrary BaseID extensions, automatic proxying and unrestricted full-role conformance are not claimed.", + "closure": "Retain tested protocol behavior and close remaining common XML, algorithm, transport and independent full-role evidence before promoting to unrestricted conformance." + }, + { + "id": "N-SAML-AUTHORITY-QUERY-PROFILE", + "statement": "Use synchronous exchanges with issuer, correlation and integrity checks; authorize assertion issuers independently of the response issuer.", + "norm": { + "document": "SAMLPROF", + "clause": "6.3; 6.4", + "level": "normative", + "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-profiles-2.0-os.pdf", "errata": [] }, "applies": { - "profile": "cms", - "role": "CMS implementation", - "mode": "generate", + "profile": "saml-authority", + "role": "SAML authorities and requester", + "mode": "query", "status": "applicable", - "reason": "Applies to the selected standard role and operation. Missing mandatory behavior stays applicable; conditions on optional formats/algorithms do not turn required selected-role duties into policy exemptions." + "reason": "Applies to the explicitly selected query, retrieval or mapping operation; it does not claim all Extended-provider proxying duties." }, "implementation": { "status": "limited", "apis": [ - "cms.Sign" + "saml/protocol.SignQuery", + "saml/protocol.VerifyQuery", + "saml/protocol.SignQueryResponse", + "saml/protocol.VerifyQueryResponse", + "saml/authority.NewService" ], "code": [ - "cms/sign.go" + "saml/authority/authority.go", + "saml/authority/client.go", + "saml/authority/uri.go", + "saml/protocol/query.go", + "saml/protocol/query_response.go", + "saml/protocol/mapping.go", + "saml/binding/uri.go", + "saml/metadata/authority.go" ] }, "tests": { + "status": "partial", + "reason": "Named tests exercise positive, negative and boundary contracts of the implemented profile; unrestricted datatype/extension and full-role coverage is not claimed.", + "positive": [ + { + "file": "saml/protocol/query_test.go", + "name": "TestAuthorityQueries" + }, + { + "file": "saml/authority/authority_test.go", + "name": "TestAuthoritySOAPAndURI" + } + ], + "negative": [ + { + "file": "saml/protocol/query_test.go", + "name": "TestQueryFiltersRejectDisclosure" + }, + { + "file": "saml/protocol/query_test.go", + "name": "TestIndependentAssertionIssuerTrust" + } + ] + }, + "interop": { "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "tests": [ + { + "file": "tests/interop/saml_authority_test.go", + "name": "TestOpenSAMLAuthorityContracts" + }, + { + "file": "tests/interop/saml_authority_http_test.go", + "name": "TestShibbolethAuthorityHTTP" + }, + { + "file": "tests/interop/saml_openam_http_test.go", + "name": "TestOpenAMAuthorityHTTP" + }, + { + "file": "tests/interop/saml_gsa_http_test.go", + "name": "TestGoogleGSAAuthorityHTTP" + } + ], + "fixtures": [ + "tests/interop/testdata/OpenSAMLAuthorityOracle.java" + ], + "reason": "OpenSAML 5.2.3 independently generates and validates the five message families. TestShibbolethAuthorityHTTP additionally deploys the unmodified pinned Shibboleth IdP 5.2.3 WAR in Jetty 12.1.14 and tests the production client against actual verified HTTPS/SOAP: signed/encrypted AttributeQuery, exact attribute selection, replay and untrusted-key rejection. Early security errors are authenticated by TLS; unsigned error envelopes remain rejected by the Go message-signature profile. Full clause-wide independent coverage of every optional condition/value/transport form remains pending. Deployed OpenAM 16.1.4 adds signed AuthnQuery and AssertionIDRequest over loopback SOAP/HTTP using actual WebSSO-issued assertions. Google GSA adaptor 4.1.4 adds TLS-authenticated AuthzDecisionQuery wire coverage; its unsigned XML is not accepted as a signed response." + }, + "policy": { + "default": { + "status": "enabled", + "reason": "Explicitly configured authority/metadata APIs require signatures, trusted peers, authorization adapters and bounded inputs." + }, + "compatibility": { + "status": "opt-in", + "reason": "Existing explicit legacy cryptographic policies apply. Raw unauthenticated CBC/RSA-v1.5 decryption is not enabled." + } + }, + "gap": "Named deployed-server evidence now exists, with explicit peer/profile limitations in docs/saml-authority.md. Full per-clause, arbitrary datatype/extension, algorithm and full-role conformance remains unverified; no unrestricted conformance is claimed.", + "closure": "Retain tested protocol behavior and close remaining common XML, algorithm, transport and independent full-role evidence before promoting to unrestricted conformance." + }, + { + "id": "N-SAML-AUTHORITY-MAPPING-CORE", + "statement": "Map an existing identifier under NameIDPolicy, authenticate correlated success/error responses and retain the schema-required identifier on error without granting a mapped identity.", + "norm": { + "document": "SAMLCORE", + "clause": "3.8.1; 3.8.2; 3.8.3", + "level": "normative", + "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf", + "errata": [] + }, + "applies": { + "profile": "saml-authority", + "role": "SAML authorities and requester", + "mode": "mapping", + "status": "applicable", + "reason": "Applies to the explicitly selected query, retrieval or mapping operation; it does not claim all Extended-provider proxying duties." + }, + "implementation": { + "status": "limited", + "apis": [ + "saml/protocol.SignNameIDMappingRequest", + "saml/protocol.VerifyNameIDMappingRequest", + "saml/protocol.SignNameIDMappingResponse", + "saml/protocol.VerifyNameIDMappingResponse", + "saml/authority.NewService" + ], + "code": [ + "saml/authority/authority.go", + "saml/authority/client.go", + "saml/authority/uri.go", + "saml/protocol/query.go", + "saml/protocol/query_response.go", + "saml/protocol/mapping.go", + "saml/binding/uri.go", + "saml/metadata/authority.go" + ] + }, + "tests": { + "status": "partial", + "reason": "Named tests exercise positive, negative and boundary contracts of the implemented profile; unrestricted datatype/extension and full-role coverage is not claimed.", + "positive": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + } + ], + "negative": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + } + ] }, "interop": { "status": "pending", - "tests": [], - "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "tests": [ + { + "file": "tests/interop/saml_authority_test.go", + "name": "TestOpenSAMLAuthorityContracts" + }, + { + "file": "tests/interop/saml_openam_http_test.go", + "name": "TestOpenAMAuthorityHTTP" + } + ], + "fixtures": [ + "tests/interop/testdata/OpenSAMLAuthorityOracle.java", + "build/openam-nameid-order.patch", + "build/patch-openam.sh" + ], + "reason": "Released OpenSAML 5.2.3 independently builds, unmarshals, signs and schema-validates all five message families in both directions. This does not certify every condition, cipher, transport or full deployed Authority role. Deployed OpenAM 16.1.4 maps two independently established WebSSO federations over SOAP, with a disclosed test-runtime Core 3.8.2 serialization-order correction. The signed legacy-encrypted result is compared to its target-SP-issued NameID. Stock Mapping serialization and its invalid top-level InvalidNameIDPolicy remain peer defects; target-only encryption/limited-use assertions are not covered by this server." }, "policy": { "default": { "status": "enabled", - "reason": "Available within the explicitly documented bounded API and its algorithm/resource/trust settings." + "reason": "Explicitly configured authority/metadata APIs require signatures, trusted peers, authorization adapters and bounded inputs." }, "compatibility": { - "status": "not-applicable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Existing explicit legacy cryptographic policies apply. Raw unauthenticated CBC/RSA-v1.5 decryption is not enabled." } }, - "gap": "Only the closed DER SignedData single-signer profile is generated. General BER/content/algorithm/attribute combinations and complete independent generation evidence remain open.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Named deployed-server evidence now exists, with explicit peer/profile limitations in docs/saml-authority.md. Full per-clause, arbitrary datatype/extension, algorithm and full-role conformance remains unverified; no unrestricted conformance is claimed.", + "closure": "Retain tested protocol behavior and close remaining common XML, algorithm, transport and independent full-role evidence before promoting to unrestricted conformance." }, { - "id": "N-CMS-GENERATE-CONTENT-TYPE", - "statement": "When signed attributes are used, include exactly one correctly typed content-type value matching encapContentInfo.", + "id": "N-SAML-AUTHORITY-MAPPING-PROFILE", + "statement": "Authenticate SOAP mapping peers, support target-encrypted identifiers and signed statement-free limited-use assertions, and verify conditions at the target.", "norm": { - "document": "RFC5652", - "clause": "11.1", + "document": "SAMLPROF", + "clause": "7.3; 7.4; 7.5", "level": "normative", - "source": "https://www.rfc-editor.org/rfc/rfc5652.html#section-11.1", + "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-profiles-2.0-os.pdf", "errata": [] }, "applies": { - "profile": "cms", - "role": "CMS implementation", - "mode": "generate", + "profile": "saml-authority", + "role": "SAML authorities and requester", + "mode": "mapping", "status": "applicable", - "reason": "Applies to the selected standard role and operation. Missing mandatory behavior stays applicable; conditions on optional formats/algorithms do not turn required selected-role duties into policy exemptions." + "reason": "Applies to the explicitly selected query, retrieval or mapping operation; it does not claim all Extended-provider proxying duties." }, "implementation": { "status": "limited", "apis": [ - "cms.Sign" + "saml/protocol.SignNameIDMappingRequest", + "saml/protocol.VerifyNameIDMappingRequest", + "saml/protocol.SignNameIDMappingResponse", + "saml/protocol.VerifyNameIDMappingResponse", + "saml/authority.NewService" ], "code": [ - "cms/sign.go" + "saml/authority/authority.go", + "saml/authority/client.go", + "saml/authority/uri.go", + "saml/protocol/query.go", + "saml/protocol/query_response.go", + "saml/protocol/mapping.go", + "saml/binding/uri.go", + "saml/metadata/authority.go" ] }, "tests": { + "status": "partial", + "reason": "Named tests exercise positive, negative and boundary contracts of the implemented profile; unrestricted datatype/extension and full-role coverage is not claimed.", + "positive": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/authority/authority_test.go", + "name": "TestAuthoritySOAPAndURI" + }, + { + "file": "saml/protocol/mapping_test.go", + "name": "TestLimitedUseMappedIdentifier" + } + ], + "negative": [ + { + "file": "saml/protocol/mapping_test.go", + "name": "TestNameIDMapping" + }, + { + "file": "saml/protocol/mapping_test.go", + "name": "TestLimitedUseMappedIdentifier" + } + ] + }, + "interop": { "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "tests": [ + { + "file": "tests/interop/saml_authority_test.go", + "name": "TestOpenSAMLAuthorityContracts" + }, + { + "file": "tests/interop/saml_openam_http_test.go", + "name": "TestOpenAMAuthorityHTTP" + } + ], + "fixtures": [ + "tests/interop/testdata/OpenSAMLAuthorityOracle.java", + "build/openam-nameid-order.patch", + "build/patch-openam.sh" + ], + "reason": "Released OpenSAML 5.2.3 independently builds, unmarshals, signs and schema-validates all five message families in both directions. This does not certify every condition, cipher, transport or full deployed Authority role. Deployed OpenAM 16.1.4 maps two independently established WebSSO federations over SOAP, with a disclosed test-runtime Core 3.8.2 serialization-order correction. The signed legacy-encrypted result is compared to its target-SP-issued NameID. Stock Mapping serialization and its invalid top-level InvalidNameIDPolicy remain peer defects; target-only encryption/limited-use assertions are not covered by this server." + }, + "policy": { + "default": { + "status": "enabled", + "reason": "Explicitly configured authority/metadata APIs require signatures, trusted peers, authorization adapters and bounded inputs." + }, + "compatibility": { + "status": "opt-in", + "reason": "Existing explicit legacy cryptographic policies apply. Raw unauthenticated CBC/RSA-v1.5 decryption is not enabled." + } + }, + "gap": "Named deployed-server evidence now exists, with explicit peer/profile limitations in docs/saml-authority.md. Full per-clause, arbitrary datatype/extension, algorithm and full-role conformance remains unverified; no unrestricted conformance is claimed.", + "closure": "Retain tested protocol behavior and close remaining common XML, algorithm, transport and independent full-role evidence before promoting to unrestricted conformance." + }, + { + "id": "N-SAML-AUTHORITY-URI-BINDING", + "statement": "Resolve the exact ID through the single-parameter HTTP URI binding, preserve immutable URI associations, disable caching and report missing/denied assertions without following redirects.", + "norm": { + "document": "SAMLBIND", + "clause": "3.7", + "level": "normative", + "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-bindings-2.0-os.pdf", + "errata": [] + }, + "applies": { + "profile": "saml-authority", + "role": "SAML authorities and requester", + "mode": "uri", + "status": "applicable", + "reason": "Applies to the explicitly selected query, retrieval or mapping operation; it does not claim all Extended-provider proxying duties." + }, + "implementation": { + "status": "limited", + "apis": [ + "saml/binding.AssertionURL", + "saml/authority.NewMemoryAssertions" + ], + "code": [ + "saml/authority/authority.go", + "saml/authority/client.go", + "saml/authority/uri.go", + "saml/protocol/query.go", + "saml/protocol/query_response.go", + "saml/protocol/mapping.go", + "saml/binding/uri.go", + "saml/metadata/authority.go" + ] + }, + "tests": { + "status": "partial", + "reason": "Named tests exercise positive, negative and boundary contracts of the implemented profile; unrestricted datatype/extension and full-role coverage is not claimed.", + "positive": [ + { + "file": "saml/authority/authority_test.go", + "name": "TestAuthoritySOAPAndURI" + }, + { + "file": "saml/binding/uri_test.go", + "name": "TestAssertionURIBinding" + } + ], + "negative": [ + { + "file": "saml/authority/authority_test.go", + "name": "TestAuthoritySOAPAndURI" + }, + { + "file": "saml/binding/uri_test.go", + "name": "TestAssertionURIBinding" + } + ] }, "interop": { "status": "pending", - "tests": [], - "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "tests": [ + { + "file": "tests/interop/saml_authority_test.go", + "name": "TestOpenSAMLAuthorityContracts" + } + ], + "fixtures": [ + "tests/interop/testdata/OpenSAMLAuthorityOracle.java" + ], + "reason": "Released OpenSAML 5.2.3 independently builds, unmarshals, signs and schema-validates all five message families in both directions. This does not certify every condition, cipher, transport or full deployed Authority role." }, "policy": { "default": { "status": "enabled", - "reason": "Available within the explicitly documented bounded API and its algorithm/resource/trust settings." + "reason": "Explicitly configured authority/metadata APIs require signatures, trusted peers, authorization adapters and bounded inputs." }, "compatibility": { - "status": "not-applicable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Existing explicit legacy cryptographic policies apply. Raw unauthenticated CBC/RSA-v1.5 decryption is not enabled." } }, - "gap": "Only the closed DER SignedData single-signer profile is generated. General BER/content/algorithm/attribute combinations and complete independent generation evidence remain open.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Full per-clause evidence remains open beyond the named tests. Deployed SOAP evidence and peer/profile limits are documented in docs/saml-authority.md; arbitrary BaseID extensions, automatic proxying and unrestricted full-role conformance are not claimed. Independent deployed URI-retrieval coverage remains open; the new server tests use SOAP.", + "closure": "Retain tested protocol behavior and close remaining common XML, algorithm, transport and independent full-role evidence before promoting to unrestricted conformance." }, { - "id": "N-CMS-GENERATE-MESSAGE-DIGEST", - "statement": "When signed attributes are used, include exactly one message-digest value for the exact signed content.", + "id": "N-SAML-AUTHORITY-AUTHORITY-METADATA", + "statement": "Publish and parse Authentication/Attribute/Authorization Authority descriptors with their required query endpoints, assertion-ID/URI services and valid Mapping endpoint attributes.", "norm": { - "document": "RFC5652", - "clause": "11.2", + "document": "SAMLMD", + "clause": "2.4.3; 2.4.5; 2.4.6; 2.4.7", "level": "normative", - "source": "https://www.rfc-editor.org/rfc/rfc5652.html#section-11.2", + "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-metadata-2.0-os.pdf", "errata": [] }, "applies": { - "profile": "cms", - "role": "CMS implementation", - "mode": "generate", + "profile": "saml-authority", + "role": "SAML authorities and requester", + "mode": "metadata", "status": "applicable", - "reason": "Applies to the selected standard role and operation. Missing mandatory behavior stays applicable; conditions on optional formats/algorithms do not turn required selected-role duties into policy exemptions." + "reason": "Applies to the explicitly selected query, retrieval or mapping operation; it does not claim all Extended-provider proxying duties." }, "implementation": { "status": "limited", "apis": [ - "cms.Sign" + "saml/metadata.Build", + "saml/metadata.ParseTrusted" ], "code": [ - "cms/sign.go" + "saml/authority/authority.go", + "saml/authority/client.go", + "saml/authority/uri.go", + "saml/protocol/query.go", + "saml/protocol/query_response.go", + "saml/protocol/mapping.go", + "saml/binding/uri.go", + "saml/metadata/authority.go" ] }, "tests": { + "status": "partial", + "reason": "Named tests exercise positive, negative and boundary contracts of the implemented profile; unrestricted datatype/extension and full-role coverage is not claimed.", + "positive": [ + { + "file": "saml/metadata/authority_test.go", + "name": "TestAuthorityMetadataRoles" + }, + { + "file": "saml/authority/authority_test.go", + "name": "TestAuthoritySOAPAndURI" + } + ], + "negative": [ + { + "file": "saml/metadata/authority_test.go", + "name": "TestAuthorityMetadataRoles" + }, + { + "file": "saml/metadata/authority_test.go", + "name": "TestNameIDMappingMetadataResponseLocation" + } + ] + }, + "interop": { "status": "pending", - "reason": "Obligation-specific positive/negative tests have not yet been mapped; no local coverage is claimed.", - "positive": [], - "negative": [] + "tests": [ + { + "file": "tests/interop/saml_authority_test.go", + "name": "TestOpenSAMLAuthorityContracts" + } + ], + "fixtures": [ + "tests/interop/testdata/OpenSAMLAuthorityOracle.java" + ], + "reason": "Released OpenSAML 5.2.3 independently builds, unmarshals, signs and schema-validates all five message families in both directions. This does not certify every condition, cipher, transport or full deployed Authority role." + }, + "policy": { + "default": { + "status": "enabled", + "reason": "Explicitly configured authority/metadata APIs require signatures, trusted peers, authorization adapters and bounded inputs." + }, + "compatibility": { + "status": "opt-in", + "reason": "Existing explicit legacy cryptographic policies apply. Raw unauthenticated CBC/RSA-v1.5 decryption is not enabled." + } + }, + "gap": "Full per-clause evidence remains open beyond the named tests. Deployed SOAP evidence and peer/profile limits are documented in docs/saml-authority.md; arbitrary BaseID extensions, automatic proxying and unrestricted full-role conformance are not claimed.", + "closure": "Retain tested protocol behavior and close remaining common XML, algorithm, transport and independent full-role evidence before promoting to unrestricted conformance." + }, + { + "id": "N-SAML-AUTHORITY-SAML-PREFIXLIST", + "statement": "Accept the standard Exclusive C14N InclusiveNamespaces parameter and protect QName-valued namespace bindings in rich signed query/assertion XML.", + "norm": { + "document": "SAMLCORE", + "clause": "5.4.3; 5.4.4", + "level": "normative", + "source": "https://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf", + "errata": [] + }, + "applies": { + "profile": "saml-authority", + "role": "SAML authorities and requester", + "mode": "query", + "status": "applicable", + "reason": "Applies to the explicitly selected query, retrieval or mapping operation; it does not claim all Extended-provider proxying duties." + }, + "implementation": { + "status": "limited", + "apis": [ + "saml/protocol.SignAuthorityAssertion", + "saml/protocol.VerifyAuthorityAssertion" + ], + "code": [ + "saml/authority/authority.go", + "saml/authority/client.go", + "saml/authority/uri.go", + "saml/protocol/query.go", + "saml/protocol/query_response.go", + "saml/protocol/mapping.go", + "saml/binding/uri.go", + "saml/metadata/authority.go" + ] + }, + "tests": { + "status": "partial", + "reason": "Named tests exercise positive, negative and boundary contracts of the implemented profile; unrestricted datatype/extension and full-role coverage is not claimed.", + "positive": [ + { + "file": "saml/internal/xmlsig/sign_test.go", + "name": "TestInclusiveNamespaceParameters" + } + ], + "negative": [ + { + "file": "saml/protocol/query_test.go", + "name": "TestAuthorityNamespaceAndValueIntegrity" + } + ] }, "interop": { "status": "pending", - "tests": [], - "fixtures": [], - "reason": "Complete independent obligation-specific positive/negative evidence remains pending. Existing component interoperability does not establish every clause case." + "tests": [ + { + "file": "tests/interop/saml_authority_test.go", + "name": "TestOpenSAMLAuthorityContracts" + } + ], + "fixtures": [ + "tests/interop/testdata/OpenSAMLAuthorityOracle.java" + ], + "reason": "Released OpenSAML 5.2.3 independently builds, unmarshals, signs and schema-validates all five message families in both directions. This does not certify every condition, cipher, transport or full deployed Authority role." }, "policy": { "default": { "status": "enabled", - "reason": "Available within the explicitly documented bounded API and its algorithm/resource/trust settings." + "reason": "Explicitly configured authority/metadata APIs require signatures, trusted peers, authorization adapters and bounded inputs." }, "compatibility": { - "status": "not-applicable", - "reason": "No general compatibility switch supplies missing normative behavior." + "status": "opt-in", + "reason": "Existing explicit legacy cryptographic policies apply. Raw unauthenticated CBC/RSA-v1.5 decryption is not enabled." } }, - "gap": "Only the closed DER SignedData single-signer profile is generated. General BER/content/algorithm/attribute combinations and complete independent generation evidence remain open.", - "closure": "Close the stated implementation/input and evidence boundaries with exact positive/negative cases and independent fixtures or exchanges where applicable, preserving explicit policy, cancellation and work/byte limits." + "gap": "Full per-clause evidence remains open beyond the named tests. Deployed SOAP evidence and peer/profile limits are documented in docs/saml-authority.md; arbitrary BaseID extensions, automatic proxying and unrestricted full-role conformance are not claimed.", + "closure": "Retain tested protocol behavior and close remaining common XML, algorithm, transport and independent full-role evidence before promoting to unrestricted conformance." } ], "residual_evidence": [ @@ -861225,6 +869893,119 @@ "case": "30/named_map_overrides_unnamed_output" } ] + }, + { + "suite": "qt3", + "file": "fn/format-date.xml", + "name": "format-date-011", + "disposition": "fixture", + "reason": "The unchanged pinned 48-date expectation assigns 2006-01-30 to week 5 but 2006-02-02 in the same Monday-Sunday week to week 1. F&O 3.0/3.1 section 9.8.4 assigns the week by its Thursday month, requiring 1 for both. Executable controls require all 48 values, the exact original assertion failure, a one-cell in-memory correction passing the complete assertion, and another-cell negative control. Publisher inputs and raw catalog failures remain unchanged.", + "checks": [ + { + "file": "tests/cmd/dev/run_test.go", + "name": "TestQT3ResidualCauses", + "case": "format-date-011" + } + ], + "failures": [ + { + "profile": "qt3/3.0/1.0/default/true/false", + "kind": "assertion", + "error_codes": [ + "" + ], + "cause": "assertion-mismatch", + "signature": "format-date-011 assertion was false" + }, + { + "profile": "qt3/3.1/1.0/default/true/false", + "kind": "assertion", + "error_codes": [ + "" + ], + "cause": "assertion-mismatch", + "signature": "format-date-011 assertion was false" + }, + { + "profile": "qt3/3.1/1.1/default/true/false", + "kind": "assertion", + "error_codes": [ + "" + ], + "cause": "assertion-mismatch", + "signature": "format-date-011 assertion was false" + }, + { + "profile": "qt3/3.1/1.0/language/true/false", + "kind": "assertion", + "error_codes": [ + "" + ], + "cause": "assertion-mismatch", + "signature": "format-date-011 assertion was false" + } + ] + }, + { + "suite": "xslt30", + "file": "tests/fn/format-date/_format-date-test-set.xml", + "name": "format-date-011", + "disposition": "fixture", + "reason": "The unchanged pinned 48-date expectation assigns 2006-01-30 to week 5 but 2006-02-02 in the same Monday-Sunday week to week 1. F&O 3.0/3.1 section 9.8.4 assigns the week by its Thursday month, requiring 1 for both. Executable controls require all 48 values, the exact original assertion failure, a one-cell in-memory correction passing the complete assertion, and another-cell negative control. Publisher inputs and raw catalog failures remain unchanged. XSLT 2.0 does not specify this month-week extension explicitly; its retained failure records an overly restrictive expected convention and our documented choice, rather than imposing the later F&O rule as a 2.0 mandate.", + "checks": [ + { + "file": "tests/cmd/dev/run_test.go", + "name": "TestXSLTResidualCauses", + "case": "format-date-011" + } + ], + "failures": [ + { + "profile": "xslt30/2.0/1.0/default/true/false", + "kind": "assertion", + "error_codes": [ + "" + ], + "cause": "assertion-mismatch", + "signature": "XML node values differ: \"1\" != \"5\"" + }, + { + "profile": "xslt30/3.0/1.0/default/true/false", + "kind": "assertion", + "error_codes": [ + "" + ], + "cause": "assertion-mismatch", + "signature": "XML node values differ: \"1\" != \"5\"" + }, + { + "profile": "xslt30/3.0/1.1/default/true/false", + "kind": "assertion", + "error_codes": [ + "" + ], + "cause": "assertion-mismatch", + "signature": "XML node values differ: \"1\" != \"5\"" + }, + { + "profile": "xslt30/3.0/1.0/default/false/false", + "kind": "assertion", + "error_codes": [ + "" + ], + "cause": "assertion-mismatch", + "signature": "XML node values differ: \"1\" != \"5\"" + }, + { + "profile": "xslt30/3.0/1.0/language/true/false", + "kind": "assertion", + "error_codes": [ + "" + ], + "cause": "assertion-mismatch", + "signature": "XML node values differ: \"1\" != \"5\"" + } + ] } ] } diff --git a/docs/language-suites.md b/docs/language-suites.md index c4aac0d..a1c64bb 100644 --- a/docs/language-suites.md +++ b/docs/language-suites.md @@ -90,8 +90,10 @@ and [3.1](https://www.w3.org/TR/2017/REC-xslt-xquery-serialization-31-20170321/# Recommendations, with SHA-256 pins `1cbcc49ac5ed8942ba58276699254469ca2144e229f68faeeb48f784c1037953` and `256736a2f4e7923e888047dee757c17bc3ee707e285f00639fd8fb1d962d11b0`. -The [XSLT 3.0 JSON schema](https://www.w3.org/TR/2017/REC-xslt-30-20170608/#schema-for-json) -pin is `3ba9d28cc77ac0ba876a7e3dd88308381c4e32334ea412b17961968a37ba6f9d`. +For XPath 3.1, use the [F&O 3.1 Appendix C.2 JSON schema](https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/schema-for-json.xsd). +Its pin is `34407695050d165be14931c0f378f832d24abe3de682a069d2d6121f07661ec3`. +The older XSLT 3.0 Appendix B.1 schema has different attribute declarations and +does not supply these F&O 3.1 controls. Keep inputs and generated captures/reports outside Git. Sources and register must stay fixed across baseline runs and captures. Docker mounts need the checkout's diff --git a/docs/saml-authority.md b/docs/saml-authority.md new file mode 100644 index 0000000..9cab0f8 --- /dev/null +++ b/docs/saml-authority.md @@ -0,0 +1,158 @@ +# SAML authorities, queries and identifier mapping + +The `saml/authority` package composes SOAP/HTTP transport, authenticated metadata, +protocol validation, application authorization and replay protection. It is +separate from WebSSO: a query result never establishes a login. All three query +types, assertion-ID retrieval, the HTTP URI binding and SOAP NameID Mapping have +producer, consumer and application integration APIs. + +See [standards](standards.md) for the selected SAML editions. Custom BaseID +schemas and automatic provider proxying are unsupported. + +## Requests and assertions + +`protocol.Query` selects `AuthenticationQuery`, `AttributeQuery`, +`AuthorizationQuery` or `AssertionRequest`. `NewSubject` builds a NameID subject; +an application-built Subject document can also include confirmation alternatives. +`SignQuery` and `VerifyQuery` authenticate the complete request before decrypting +its identifiers. Consent and foreign-namespace Extensions are retained. + +Attribute filters contain complete immutable `saml:Attribute` documents. Values +may be structured XML, typed simple values, empty values or explicit nils. +Name and NameFormat identify an attribute; FriendlyName does not replace them. +Duplicate requested attributes are rejected. Attribute-value comparison retains +QName namespace bindings and uses identical canonical XML unless the application +supplies the attribute profile's equality operation. + +`SignAuthorityAssertion` creates an assertion without imposing WebSSO bearer, +recipient or fresh-login requirements. It retains AuthnStatement, +AttributeStatement and AuthzDecisionStatement documents, conditions and advice. +Past AuthnInstant values remain past acts; AuthnQuery must never initiate a new +credential authentication. Assertion, identifier and attribute encryption can be +selected independently. Attribute-count/value/byte limits apply before issuance +and after authenticated decryption. Value byte limits cover canonical XML markup +and type annotations as well as text. + +`SignQueryResponse` and `VerifyQueryResponse` support zero or more assertions. +An empty Success means no matching facts. Every returned subject must strongly +match the query; identifier content, attribute values and attribute presence are +retained. Authentication filters are checked across the result set. Attribute +filters prevent returning unrequested attributes or values. Authorization results +must concern the requested resource and actions. Assertion-ID results must contain +only requested IDs. A response issuer cannot implicitly authorize another +assertion issuer: `AssertionTrust` must explicitly resolve that issuer's keys. +Forwarding a completed assertion signed by another key also requires the signing +key's explicit `PreserveKeys` adapter. The service derives that adapter from its +assertion trust resolver and verifies preservation again while signing SOAP. + +Exact AuthnContext URI comparison is built in. For minimum, maximum and better, +`QueryMatchOptions.Context` supplies the authority's strength ordering, including +maximum's strongest-available rule. It must not infer strength from URI spelling. +Errata E45 does not assign preference order to AuthnQuery references; better means +stronger than one requested context. If no context can be satisfied, the backend +returns `StatusError` with `NoAuthnContext`, or the service generates that status +when its final result check detects the mismatch. + +`QueryMatchOptions.Confirm` must establish strong confirmation compatibility when +a query includes SubjectConfirmation. Missing confirmation or non-exact context +evaluators cause rejection. Evidence in authorization queries is untrusted input; +the backend must separately authenticate any embedded assertion it relies on. +Unknown assertion conditions are rejected as indeterminate. Audience restrictions +are ANDed, with OR within a restriction. OneTimeUse is retained for atomic replay +processing; ProxyRestriction is retained without granting permission to proxy. +Advice permits foreign namespace extensions; Evidence permits only the four +SAML assertion/reference alternatives. Reference syntax is checked without +fetching references or granting trust to embedded assertions. + +## SOAP integration + +`authority.NewService` selects supported request kinds and application callbacks. +`Config.Trust` resolves explicitly authenticated peer keys; neither KeyInfo nor an +HTTPS-looking issuer URI grants trust. `Config.Query` authorizes disclosure and +returns signed assertions about existing facts. `Config.Mapping` authorizes the +source principal, target federation and any requested identifier creation. A +signed request is consumed before a backend can create a binding. Backend and +replay adapters must support concurrent calls and honor context. + +HTTP handlers compose `binding.ReadSOAPRequest`, `Service.Respond` and +`binding.WriteSOAPResponse`. `authority.Client` resolves the selected role's +current trusted endpoint, signs the request, performs the synchronous exchange, +checks the correlated result and atomically consumes replay entries. The HTTP +client supplies TLS, optional client certificates and preemptive HTTP Basic through +its transport. Applications own credentials, account authentication and authorization; +the package is not a password database or a deployed IdP product. + +All SAML messages in this integration use XML signatures. Transport +adapters handle SOAP faults and HTTP errors. Received response endpoints cannot +redirect the client to another server. SOAP may omit Destination; a present +Destination must match an explicitly configured expected destination. Inclusive +C14N is signed in its final SOAP namespace context. Exclusive C14N for rich XML +uses InclusiveNamespaces so QName bindings cannot be changed outside the digest. + +Multi-instance deployments need shared atomic replay storage. A result containing +63 OneTimeUse assertions needs a batch of 65 entries. The reference replay store +supports up to 256 entries atomically. +Configure store lifetimes to cover message age, assertion expiry and clock skew. +OneTimeUse assertions without a bounded expiry are rejected by the client. +The response limit is `protocol.MaxQueryAssertions` (63), within the shared +64-signature budget. Embedded Evidence signatures also count toward that budget. + +## NameID Mapping + +`SignNameIDMappingRequest` and `VerifyNameIDMappingRequest` retain the existing +NameID and the requested Format, SPNameQualifier and AllowCreate. Mapping does +not rename the source binding; ManageNameID remains the separate management +operation. An application can use `NewPersistentNameID` when it is authorized to +establish a new opaque pairwise persistent identifier. + +The mapping backend normally returns an EncryptedID for the target provider's +metadata-authorized key. `EncryptMappedIdentifier` constructs this representation. +`Client.Map` verifies the signed, correlated envelope and returns the opaque +identifier without demanding the target's private key. A deployment-specific +plaintext exception requires `AllowPlaintextMapping` on the service. Plaintext +output is checked against the requested format and target namespace; the backend +must enforce those rules before encrypting opaque output. + +Core's NameIDMappingResponse schema requires a NameID/EncryptedID even for an +error. `VerifiedNameIDMappingRequest.ErrorIdentifier` provides the original +representation for this purpose, preserving input encryption. Consumers expose +no mapped identifier when the status is not Success. + +`EncryptMappedAssertion` implements the optional limited-use representation from +Profiles §7.4.2.1: a signed assertion with a subject and no statements is encrypted +into an EncryptedID. `DecryptMappedAssertion` verifies its issuer, signature and +conditions at the target. It must not be treated as a bare NameID. Raw decryption +helpers require authenticated encryption; legacy CBC/RSA-v1.5 additionally needs +the exact verified enclosing mapping result's `DecryptNameID`/`DecryptAssertion` +method. Private-key attempts share a bounded aggregate budget. + +## Assertion URI retrieval + +`binding.AssertionURL` creates the single, exact `ID` query. The configured +endpoint cannot already contain a query or fragment. Wildcards, duplicate IDs, +extra query parameters and invalid escapes are rejected. `URIHandler` authenticates +and authorizes before lookup, returns 403 for denied disclosure and 404 for an +unknown ID, and disables caching. `URIClient` never follows redirects and checks +the returned assertion's ID before higher-level signature/condition verification. + +An assertion URI must forever designate the same assertion or return an error. +`MemoryAssertions` provides bounded immutable-ID insertion without eviction or +replacement. It is process-local; persistent published URIs need durable storage +with the same contract across restarts. `Get` does not itself authorize disclosure. + +## Tests and independent evidence + +`TestAuthoritySOAPAndURI` covers HTTPS/SOAP/URI integration, replay rejection, +authorized disclosure and immutable assertion IDs. Protocol tests cover filters, +multiple/empty results, independent issuers, encrypted claims and mapping; +`FuzzAuthorityQueryParsers` exercises untrusted inputs. + +`TestOpenSAMLAuthorityContracts` checks query, assertion-ID and Mapping messages +in both directions against OpenSAML, including schema and signature validation. +Deployed-server tests cover Shibboleth AttributeQuery, OpenAM AuthnQuery, +AssertionIDRequest and Mapping, and Google GSA AuthzDecisionQuery. + +OpenAM Mapping requires a test-runtime serializer patch; GSA returns unsigned +XML that the Go verifier rejects. These tests have different acceptance boundaries. +See [SAML interoperability tests](testing.md#saml-interoperability) for setup and +peer limitations. diff --git a/docs/saml.md b/docs/saml.md new file mode 100644 index 0000000..29a2dd2 --- /dev/null +++ b/docs/saml.md @@ -0,0 +1,245 @@ +# SAML protocol integration + +The library implements the SAML 2.0 (15 March 2005) Web SSO, Single Logout, +Name Identifier Management and Artifact Resolution profiles, with the applicable +[Approved Errata 05](https://docs.oasis-open.org/security/saml/v2.0/errata05/os/saml-v2.0-errata05-os.html). +These APIs provide protocol processing; applications provide user authentication, +account linking, HTTP routing, trusted metadata and authoritative session storage. + +## Web SSO and ECP + +`sp.BeginOptions.Binding` selects Redirect, POST, Artifact or PAOS. The response +binding defaults to POST; `ResponseBinding` can select Artifact. PAOS initiation +selects a PAOS ACS and a SOAP IdP SSO endpoint. Browser/session binding is required +for all response transports. Do not use RelayState as a redirect destination. + +For ECP, advertise HTTP support with `ecp.Advertise`, check negotiation with +`ecp.Negotiated`, and return `Outbound.PAOS` with `ecp.WriteChallenge`. +`ecp.ParseChallenge` retains the original SP-controlled consumer URL and relay +header. `Exchange.ToIDP` removes ECP/PAOS headers before forwarding the SAML body +to a separately trusted IdP endpoint. The IdP uses `AcceptSOAP`, authenticates the +user, calls `Issue` or `Reject`, and returns `Issued.ECP`. + +`Exchange.Finish` compares the IdP consumer URL with the retained SP URL and +restores relay/correlation headers. On mismatch it returns only a SOAP fault. +`Exchange.Deliver` posts that result, including a routing fault, to the original +SP endpoint; it never follows redirects. The SP consumes the returned envelope +with `ConsumePAOS`. Direct HTTP request decoding is available through +`ecp.ReadResponse`; parsing alone does not verify the assertion or browser state. + +ECP support is the original bearer profile in +[SAML Profiles 4.2](https://docs.oasis-open.org/security/saml/v2.0/saml-profiles-2.0-os.pdf), +including errata E20/E21/E22/E35/E54. The later +[ECP 2.0 CS01 (2013)](https://docs.oasis-open.org/security/saml/Post2.0/saml-ecp/v2.0/cs01/saml-ecp-v2.0-cs01.html) +supersedes that profile and has additional implementation requirements for +holder-of-key, channel bindings and authentication mechanisms. Those requirements +are distinguished from the original profile. Channel binding and X.509 HoK with +TLS client authentication or whole-SOAP XML proof are implemented as described +below. The scope of §2.3.11's no-Transforms wording remains unresolved; see +[holder-of-key processing](#ecp-2013-holder-of-key). Unknown mandatory extension +headers fail rather than silently downgrading the exchange. +Identity provider discovery and interactive login remain application concerns. + +For the CS01 channel-binding option, a client uses `AdvertiseChannelBindings`; +the SP checks `NegotiatedChannelBindings` before supplying `BeginOptions.ChannelBindings`. +Obtain `tls-server-end-point` with `ServerCertificateBinding` using the actual +server certificate of the authenticated client/SP connection (RFC 5929 4.1). +Do not fetch a certificate on a different connection or trust one supplied in XML. +Algorithms without a single defined certificate-signature hash are rejected. +`TLSExporterBinding` additionally implements RFC 9266 for a completed TLS 1.3 +connection; that connection must be closed after the authentication instance. + +The client calls `ParseChallengeWithBindings` with independently obtained +transport data. The SP data is inside the signed request; the client data is in +the forwarded SOAP header. `AcceptSOAP` compares them. A channel mismatch returns +both an authenticated `Request` and an error: use `Reject` to send a signed SAML +failure, never `Issue`. A successful issue includes matching signed assertion +Advice and a SOAP signal. `Finish` produces a fault if the IdP omits its signal; +`ConsumePAOS` also requires the signed Advice against the original pending state. +The ECP removes processed channel headers before the return hop. + +### ECP 2013 holder of key + +Metadata endpoints advertise HoK with `Binding: metadata.HoKSSO` and the actual +SOAP/PAOS transport in `ProtocolBinding`. `Endpoint.SupportedChannelBindings` +publishes the `cb:supportsChannelBindings` list. Nil omits the attribute; an empty +non-nil slice publishes an empty list. `Role.HoKACS` selects only explicitly +advertised HoK endpoints. SP/IdP orchestration uses their actual transport and +prefers HoK advertisements for an explicit HoK request; bearer requests cannot +select HoK-only endpoints. Ordinary PAOS/SOAP endpoints remain usable with explicit +HoK negotiation for peers that omit the SHOULD-level advertisement. Advertisement +does not prove key possession or establish a secure channel. + +The client advertises `AdvertiseHoK`; the SP checks `HoKNegotiated` and supplies +`BeginOptions.HolderOfKey`. Pending state retains that requirement, preventing a +bearer downgrade. `ParseHoKChallenge` selects the client's exact certificate and +the actual SP channel data, if requested. If both bearer and HoK are offered, +`ParseChallenge` may select the explicit bearer alternative. A HoK-only challenge +cannot be processed by a bearer-only client. + +For TLS proof, the client submits `Exchange.ToIDP` over a mutually authenticated +TLS connection. The IdP calls `ecp.VerifyTLSRequest` on the original server request +and passes the resulting opaque proof to `idp.AcceptECP`. For XML proof, the client +chooses one of two separate XML APIs: + +- Declared W3C transform: `Exchange.ToIDPEnvelopedXML` and + `ecp.VerifyEnvelopedXML`; for the return hop use `FinishEnvelopedXML`. +- Explicit CS01 compatibility: `Exchange.ToIDPCS01CompatXML` and + `ecp.VerifyCS01CompatXML`; for the return hop use `FinishCS01CompatXML`. + +The low-level methods are `proof.SignEnvelopedXML` / `VerifyEnvelopedXML` and +`proof.SignCS01CompatXML` / `VerifyCS01CompatXML`. Each verifier accepts only its +named format; neither auto-detects or retries the other. TLS proof needs neither +XML API. + +[ECP CS01 §2.3.11](https://docs.oasis-open.org/security/saml/Post2.0/saml-ecp/v2.0/cs01/saml-ecp-v2.0-cs01.html) +requires an empty-URI Reference and excludes the proof Signature from coverage, +but its no-Transforms wording does not clearly distinguish direct SignedInfo +children from nested Reference/Transforms. Ordinary W3C XMLDSig requires a +declared transform for self-exclusion. The compatibility API applies that +exclusion outside W3C reference processing; generic xmlsec1 and JDK verification +reject its Reference digest. The declared-transform API follows W3C XMLDSig; +its full CS01 status remains unresolved. See the [selected standards](standards.md) +and [independent tests](testing.md#saml-interoperability). + +The IdP's `AcceptECP` receives independently derived channel bindings and checks every +supplied client/IdP binding. These headers omit the SOAP actor; client/SP channel +headers use the next actor. `ServerCertificateBinding` supports the required +`tls-server-end-point` form on both channels. With XML proof, supplying and +verifying this binding is recommended by CS01 2.3.5.1. + +`Request.HolderOfKeyCertificate` identifies the proven certificate. Before issuing +an assertion, the application must independently associate that key with the +authenticated principal and set `Authentication.HolderOfKeyCertificate` to the +associated certificate. Possession, a certificate DN, and successful XML parsing +do not establish that association. HTTP Basic can be implemented with standard +`Request.BasicAuth` and `Request.SetBasicAuth`; TLS, cookie storage and account +authentication remain in the application's HTTP configuration. The real mTLS +integration test demonstrates Basic authentication plus explicit key association. +PKIX validation is optional in this profile and is not silently required or +granted by these proof APIs. + +`Issued.ECP` includes HoK confirmation data identical to the assertion producer's +data, including when the assertion is encrypted. Use `ecp.WriteIDPResponse` for +the SOAP HTTP response. The client checks that header in `Finish`, then presents +the same key to the SP using either TLS client authentication or the selected +`FinishEnvelopedXML` / `FinishCS01CompatXML` method. +The SP verifies the corresponding transport/XML proof and calls `ConsumeECP`. +SAML signatures, conditions, audience, recipient, request/session correlation and +replay remain mandatory. Certificates are matched byte for byte; a reissued +certificate with the same public key is insufficient for this matching form. +Proofs are bound to immutable SOAP snapshots and cannot be applied to another +document with the same XML ID. Never construct synthetic TLS state from a proxy +header. Optional non-X.509 keys and SKI/DN/issuer-serial confirmation forms are +not selected by these APIs. + +## Artifact and SOAP + +`artifact.Issuer` binds a random type 0x0004 artifact to the intended recipient, +endpoint index and an immutable message. `artifact.MemoryStore` implements atomic +single use, expiry and capacity bounds. A durable shared `artifact.Store` is +required across service instances. Its `Resolve` transaction commits consumption +only after the signed reply is ready; a signing failure preserves the message. +The default SourceID is the recommended SHA-1 entity-ID routing hash, not a +security signature. An explicitly agreed opaque SourceID can be configured on +both issuer and resolver. `Issuer.Limits` must match its request verifier's +freshness policy, including when a verified request is held before resolution. + +Use `Outbound.IssueArtifact` or `Issued.IssueArtifact` for Web SSO. Deliver the +token with `binding.ArtifactURL` or `ArtifactForm`; decode GET/POST fields with +`DecodeArtifact`. `artifact.Resolver` uses a current trusted peer entity ID, +signing keys and indexed SOAP endpoints. Received XML or token bytes never supply +an endpoint to fetch. An unknown, expired, unauthorized or consumed artifact gets +a successful empty response. Resolution does not validate the nested protocol: +use `idp.AcceptArtifact`, `sp.ConsumeArtifact` or the relevant control verifier. + +`binding.SOAPClient`, `ReadSOAPRequest` and `WriteSOAPResponse` implement SOAP 1.1 +HTTP transport. SAML error statuses use HTTP 200; SOAP faults use HTTP 500. +Additional mandatory headers need explicit processing. SOAP parsing grants no +trust. Production endpoints require HTTPS unless an application explicitly opts +into HTTP for a trusted link; TLS configuration remains in its HTTP client/server. + +Inclusive C14N signatures include ancestor namespaces. Sign control messages in +the final envelope using `protocol.SignSOAP`. For nested assertions or artifact +requests, set `protocol.Response.SOAPContext` or `AuthnRequest.SOAPContext` before +signing. High-level SP/IdP flows do this automatically. Arbitrarily wrapping an +already signed Inclusive C14N message can invalidate its signature. +Extracted payloads retain an immutable view of their original ancestors and the +full source ID domain. When inherited xml:* attributes cannot be flattened onto +a schema-constrained SAML root, isolated `XML` serialization returns an error; +serialize the original envelope or use `WrapSOAP` to preserve that context. + +## Logout and managed identifiers + +`session.Service` processes authenticated control requests, while `session.Store` +atomically manages sessions, current NameID bindings, pending exchanges and replay +markers. Each store belongs to one local entity. Register bindings only after +application-authenticated account linking. Add sessions after successful SSO, +retaining the complete `VerifiedAssertion.Identifier` and `SessionIndex`. +Authorization must consult the same authoritative session state. The in-memory +store is suitable for a single process; shared deployments need a durable adapter. +`session.Config` fixes the local entity and role, and `Store.Provider` prevents +accidental reuse for another provider. Persistent identifier qualifiers omitted +on the wire are resolved using that context. Pass the assertion's identifier to +`Session.NameID`; stale bindings are rejected, and management changes preserve +existing logout marks for the renamed identifier. `Service.Limits` must match +the incoming verifier's freshness policy. + +For incoming messages, verify issuer, keys, endpoint, freshness and signature +through the typed protocol verifier. Redirect verifiers authenticate the original +query and return RelayState separately; echo it and correlate it with the +initiator's application state. Call `Service.Logout` or `Manage` with the current +trusted peer response endpoint. `Service.Endpoint` is the receiving local +endpoint; `ResponseEndpoint` handles a different metadata ResponseLocation. +SOAP requests may omit Destination; a present Destination is always checked. + +Logout selects the complete NameID and supplied session indices; an authority +may omit indices to select all applicable sessions. Participant-initiated logout +requires at least one index. An IdP advertising SingleLogoutService must supply +SessionIndex during `Issue`; the SP rejects its omission. Incoming logout marks +also reject valid late-arriving assertions through NotOnOrAfter plus the configured +ClockSkew. `Service.Limits.ClockSkew` must cover the assertion verifier's clock +skew as well as the incoming control verifier's policy; custom Store adapters +must retain the logout block for this full interval. An authority +fans out under the matching AuthoritySession and reports PartialLogout if any +participant cannot be contacted or does not confirm success. `InitiateLogout` +starts the same process directly at the authority. Configure `Service.Notify` +using `BackChannel.Logout` or an application-managed front-channel exchange. + +`BeginLogout` invalidates matching initiating local sessions and records pending +correlation and a logout block atomically. The block covers NotOnOrAfter plus +ClockSkew (or the two-hour replay lifetime if NotOnOrAfter is absent), independently +of receiving a response or expiring pending state. `Complete` accepts only the +exact verified response kind, peer and request ID, once. A name-management request from an SP changes +SPProvidedID; one from an IdP changes the primary NameID value. The format and +local account ID remain unchanged. `BeginManage` commits the initiating side's +rename only after a successful correlated response. Existing and replacement +identifiers can be encrypted and are decrypted only after authentication. +`NewPersistentNameID` generates an opaque pairwise identifier; persist it rather +than exposing a username in the persistent format. Persistent/transient values +are limited to 256 Unicode characters; transient values also follow NCName rules. + +Terminate retires the identifier binding and preserves active sessions (errata +E8); it affects that identifier relationship, not other accounts or peers. Logout +must precede sending Terminate. Pending termination prevents a later logout for +the same binding. Sending Terminate immediately retires the initiating local +binding; a lost/error response or pending-state timeout does not restore it. +The reference store retains that retirement until the original relationship's +expiry and rejects new sessions or re-registration of the same identifier during +that period. Explicit account linking may register a different identifier. +Management of transient identifiers is rejected (E12). Unknown +or stale identifiers get Requester/UnknownPrincipal. Reply signatures are prepared +before incoming control state is committed, so signing failures leave it intact. +Custom Store adapters return Success, Success/PartialLogout, +Requester/UnknownPrincipal or Requester from Logout/Manage. Backend failures +return an error before committing; these four outcomes have prepared replies. + +Stores bound capacity and lifetimes without evicting active entries. Defaults +include five-minute pending control exchanges, at most five-minute artifacts, +two-hour replay markers in Service and at most seven-day logout marks plus clock +skew. The configured acceptance window and durable-store retention must stay consistent. +The reference store retains one current identifier binding per local account/peer. +Queries, assertion retrieval and SOAP NameID Mapping use the separate +[authority APIs](saml-authority.md). Custom BaseID schema types, unsolicited SSO +and proxying profiles remain outside these APIs. Full SAML role conformance is +not claimed merely from the presence of these workflows. diff --git a/docs/standards.md b/docs/standards.md index af8af06..14c4622 100644 --- a/docs/standards.md +++ b/docs/standards.md @@ -64,4 +64,13 @@ Supported behavior and exclusions are in [compatibility](compatibility.md). - [RFC 3394](https://www.rfc-editor.org/rfc/rfc3394.html): AES Key Wrap - [RFC 3217](https://www.rfc-editor.org/rfc/rfc3217.html): explicit legacy TripleDES Key Wrap +The [ECP integration guide](saml.md#ecp-2013-holder-of-key) uses +[ECP 2.0 CS01](https://docs.oasis-open.org/security/saml/Post2.0/saml-ecp/v2.0/cs01/saml-ecp-v2.0-cs01.html), +[Channel Binding Extensions CS01](https://docs.oasis-open.org/security/saml/Post2.0/saml-channel-binding-ext/v1.0/saml-channel-binding-ext-v1.0.html), +[HoK SSO CS02](https://docs.oasis-open.org/security/saml/Post2.0/sstc-saml-holder-of-key-browser-sso-cs-02.pdf) +and [HoK Assertion Profile CS02](https://docs.oasis-open.org/security/saml/Post2.0/sstc-saml2-holder-of-key-cs-02.pdf). +Whole-SOAP proof uses [XMLDSig Second Edition §§4.3.3.3, 6.6.4](https://www.w3.org/TR/2008/REC-xmldsig-core-20080610/); +channel bindings use [RFC 5929 §4.1](https://www.rfc-editor.org/rfc/rfc5929.html#section-4.1) +and [RFC 9266](https://www.rfc-editor.org/rfc/rfc9266.html). + The library does not perform XSD validation or download schemas. diff --git a/docs/streaming.md b/docs/streaming.md index 1baf1dc..7aa09c4 100644 --- a/docs/streaming.md +++ b/docs/streaming.md @@ -7,10 +7,14 @@ cancellation themselves. See [common contracts](usage.md#common-contracts). ## Parsing and ownership -`document.ParseReader`, `Engine.ParseReader` and `CompileXSLTReader` parse -incrementally but retain complete trees and current lexical tokens. Large tags, -comments or unfinished references can require token-sized storage. Language -resources have a separate [DTD/encoding profile](transforms.md#xml-input-profiles). +`document.ParseReader` and `Engine.ParseReader` parse incrementally but retain +complete trees and current lexical tokens. Large tags, comments or unfinished +references can require token-sized storage. + +`document.ParseOptions{NonValidating: true}`, `CompileXSLTReader` and language +resource parsing can additionally retain bounded decoded and DTD-expanded input +buffers, even when the caller supplies a reader. External entity text can remain +cached during parsing. See the [DTD/encoding profile](transforms.md#xml-input-profiles). Node handles retain their owner document. `Result.Document(ctx)` detaches one selected element into an independent snapshot. Small string views can retain a diff --git a/docs/testing.md b/docs/testing.md index d3e81c0..4c338cb 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -1,7 +1,8 @@ # Testing Run from the module root using the Go version in [go.mod](../go.mod). -Make requires a POSIX shell; [CI](../.github/workflows/ci.yml) pins tool versions. +Make requires a POSIX shell; the interoperability targets also require Bash. +[CI](../.github/workflows/ci.yml) pins tool versions. ```sh make check # modules, docs, vet, tests, CGO-free build @@ -50,9 +51,18 @@ The complete Linux interoperability run uses Docker: make interop-docker ``` -The [image](../build/interop.Dockerfile) supplies native tools, runs race tests and -interoperability checks, and disables networking during execution. System -package versions follow Debian repositories. +The [image](../build/interop.Dockerfile) supplies native tools, Saxon, released +Shibboleth/Jetty, OpenAM/Tomcat, Google GSA and DSS runtimes, and published schemas/vectors. The build step +downloads the [pinned inputs](../build/interop-inputs.txt) with retries and verifies +SHA-256 on the host and again inside Docker. Override `INTEROP_INPUT_CACHE` to +reuse a prepared cache. System package versions follow Debian repositories. + +Runtime networking is disabled except for the container's loopback. The full +recipe runs race tests, generates local historical fixtures, starts official DSS +6.5.1 and performs both external suites. `XMLSEC_REQUIRE_INTEROP=1` makes every +skipped test, failure, empty run or incomplete JSON test stream fail the gate. +Transform comparisons run in four isolated Go processes in the image +(`XMLSEC_INTEROP_SHARDS`, 1–8). Tests within each process remain sequential. For a prepared host: @@ -65,6 +75,57 @@ The portable suite needs OpenSSL 3+, JDK 21+, a C compiler, pkg-config and libxm headers. Optional checks skip when unconfigured; tests never install dependencies. Known disagreements are recorded in [compatibility](compatibility.md#known-oracle-differences). +### SAML interoperability + +The `opensaml` suite uses OpenSAML 5.2.3 from the pinned Shibboleth distribution. +Set `OPENSAML_CLASSPATH` to its JARs and dependencies. A selected run fails for +missing dependencies or another engine version: + +```sh +go test -tags=interop -count=1 -timeout=5m ./tests/interop \ + -run '^TestOpenSAML' -args -suite=opensaml +go test -tags=interop -count=1 -timeout=5m ./tests/interop \ + -run '^TestECPProofJava$' -args -suite=portable +``` + +OpenSAML checks HoK certificates, channel comparison, independently generated ECP +return messages, authority queries, assertion-ID requests and NameID Mapping. +The JDK's JSR 105 implementation exchanges whole-SOAP RSA/ECDSA proofs across all +six C14N forms. These checks cover components and messages, not a deployed HoK IdP. + +Deployed-server tests use the production Go authority client and test-owned +accounts, keys and loopback listeners. All are included by `-suite=all`: + +- `-suite=shibboleth`: set `SHIBBOLETH_DIST` and `JETTY_HOME` for Shibboleth IdP + 5.2.3 and Jetty 12.1.14. `TestShibbolethAuthorityHTTP` checks signed, + AES-128-GCM/OAEP-SHA256 AttributeQuery results. Early security errors can be + unsigned; the test checks their TLS-authenticated status separately, while the + Go signature verifier rejects them. +- `-suite=authority-http`: set `OPENAM_DIST`, `TOMCAT_HOME` and `GSA_DIST` for + OpenAM 16.1.4, Tomcat 11.0.27 and Google GSA adaptor 4.1.4. Select with: + +```sh +go test -race -tags=interop ./tests/interop \ + -run '^Test(OpenAM|GoogleGSA)AuthorityHTTP$' -args -suite=authority-http +``` + +OpenAM covers AuthnQuery, AssertionIDRequest and NameID Mapping. Its test WAR +requires a [serializer-order patch](../build/openam-nameid-order.patch), built by +[patch-openam.sh](../build/patch-openam.sh); this does not establish stock OpenAM +Mapping compatibility. The test rejects OpenAM's nonconforming top-level +InvalidNameIDPolicy status. Mapping uses explicitly permitted AES-128-CBC/RSA-v1.5 +after signature verification and encrypts to the requester, without proving the +target-only encryption profile. OpenAM's loopback HTTP connector uses explicit +HTTP permission. + +GSA covers AuthzDecisionQuery over HTTPS and returns unsigned XML. The Go verifier +rejects those responses; wire checks do not grant verified authorization. Requests +omit the optional Destination because GSA cannot determine its receiver URI when +that field is present. Selected deployments fail on missing runtimes or server +startup errors. See [authority integration](saml-authority.md) for API contracts. + +### Transform comparison inputs + Transform comparisons additionally use prepared inputs outside the repository: - `SAXON_CLASSPATH`: [Saxon HE 12.10](https://github.com/Saxonica/Saxon-HE/releases/tag/SaxonHE12-10) diff --git a/docs/transforms.md b/docs/transforms.md index bd2c778..972b8d9 100644 --- a/docs/transforms.md +++ b/docs/transforms.md @@ -27,9 +27,14 @@ for complete option contracts. ## XML input profiles -`Engine.Parse`, record input and approved XMLDSig programs use the DTD-free -UTF-8/UTF-16 security profile. `AllowSingleByteEncodings` additionally permits -supported ASCII-compatible IANA encodings; raw and decoded bytes remain bounded. +`Engine.Parse`, `Engine.ParseReader`, stylesheet record input and approved +XMLDSig programs reject DTDs and external entities. UTF-8/UTF-16 are accepted; +`AllowSingleByteEncodings` additionally permits supported ASCII-compatible IANA +encodings. Raw and decoded bytes remain bounded. + +The engine's XML input preserves relative namespace names without resolving them +against a base URI. The default `document.Parse` / `ParseReader` and C14N reject +those names. Importing or exporting a snapshot does not change its namespace names. Language XML input (`doc`, `parse-xml`, stylesheets and document resources) uses bounded nonvalidating DTD processing: entities, default attributes and declared @@ -238,5 +243,10 @@ processing.AllowedTransforms = append(processing.AllowedTransforms, approved.Alg Approval includes the exact program and complete namespace context as serialized in the signature. No program is approved by default; building XML grants no approval. Standalone application functions are unavailable in these transforms. -Resource preflight and program checks happen before execution. See the -[signature-transform example](../examples/transforms/main.go). +`MaxProgramBytes` bounds each approved XPath/Filter2 expression and the complete +approved stylesheet. `NewEngine` compiles each trusted program with a timeout of +`max(Limits.Timeout, 3s)`, sharing the work and value budgets across that program's +expressions. The configured timeout still bounds each runtime invocation, including +values shorter than three seconds. +Resource preflight and program checks happen before execution. For standalone +XPath and XSLT usage, see the [runnable examples](../examples/transforms/main.go). diff --git a/docs/usage.md b/docs/usage.md index a3f775c..2bb1ab3 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -33,8 +33,13 @@ and charset metadata. Context-aware key providers use `AdaptContextSigner`, `InclusivePrefixes` applies only to that algorithm. Use `Subtree` / `AllNodes` for full selections; sparse node sets may produce fragments. -The security parser rejects DTDs and external entities. Language input has a -separate [DTD profile](transforms.md#xml-input-profiles). See [streaming](streaming.md) +The default security parser rejects DTDs and external entities. +`document.ParseOptions{NonValidating: true}` enables bounded DTD processing, +including entity expansion and default attributes; external entities require an +explicit `EntityResolver`. `Document.DTD` returns owned declaration information. +Serialization writes expanded XML and omits the DTD. This mode also accepts +relative namespace names, which C14N rejects. Language input uses a separate +[DTD profile](transforms.md#xml-input-profiles). See [streaming](streaming.md) for retained trees, independent records and partial output. ## Signatures and coverage @@ -143,6 +148,9 @@ uses with generated 96-bit nonces. CBC/TripleDES require exact algorithms and `AllowLegacy`; they are disabled by default and in FIPS mode. CBC plaintext is unauthenticated: establish ciphertext integrity before decryption and keep legacy keys separate from GCM keys. +RSA PKCS#1 v1.5 transport likewise requires explicit `KeyTransportAlgorithm: +xmlenc.RSA15`, `AllowLegacy` and prior ciphertext authentication. It is available +in the RSA APIs and disabled in FIPS mode. ConcatKDF and ECDH-ES use explicit parameters and bounded work; ECDH supports P-256/P-384/P-521 and fresh ephemeral keys. Explicit key-graph APIs resolve @@ -162,5 +170,32 @@ consumes request/assertion IDs after validation. Multiple instances need shared atomic state/replay stores. Authentication, cookies, sessions and HTTP endpoints belong to the application. See the [SSO example](../examples/sso). +Set `idp.Config.Encryption` to `&protocol.EncryptionOptions{Targets: +protocol.EncryptAssertion | protocol.EncryptNameID | protocol.EncryptAttributes}` +to encrypt the selected claims. With no explicit recipient, the IdP selects an +RSA encryption key from trusted SP metadata. An explicit recipient must also +occur there. Signing-only metadata keys do not authorize encryption. + +The SP supplies `Config.Decryption` with its local `crypto.Decrypter` providers; +only providers matching its trusted encryption metadata are used. Set +`Config.RequireEncryption` to reject plaintext at the selected positions. The +zero cipher options select AES-256-GCM/RSA-OAEP-SHA256. CBC, RSA15 and SHA-1 +need explicit algorithm policy; CBC/RSA15 ciphertext is authenticated before +private-key operations. `MaxDecryptions` bounds attempts across all encrypted +fields and keys. Replay and browser-correlation checks still apply. + +Use `sp.BeginOptions.NameIDFormat = protocol.NameIDEncrypted` to request +`EncryptedID` specifically. This policy value describes encryption, not the +inner NameID format. An IdP with encryption configured adds `EncryptNameID` to +its targets for this request; without encryption policy it returns `ErrPolicy`. +The SP requires an authorized decryption key before sending such a request and +rejects a response that encrypts only the Assertion while leaving its NameID +plaintext inside it. + +Artifact/SOAP, logout, NameID management and bearer/holder-of-key ECP have +separate [integration contracts](saml.md). The [authority APIs](saml-authority.md) +compose queries, assertion retrieval and NameID Mapping with application +authorization and replay storage. Query results do not establish a WebSSO login. + The [XPath/XSLT guide](transforms.md) covers language contexts and [approved XMLDSig programs](transforms.md#xmldsig-v1-compatibility). diff --git a/document/doc.go b/document/doc.go index f71235a..918b012 100644 --- a/document/doc.go +++ b/document/doc.go @@ -12,18 +12,25 @@ // Document.Root and navigate elements, attributes and other node kinds without // exposing a mutable tree. [Name] uses an expanded namespace URI and local // name, independent of the source prefix. The parser accepts UTF-8 and UTF-16 -// XML 1.0. DTDs, external entities and XSD validation are unsupported; ID +// XML 1.0. The default rejects DTDs and external entities; ID // attributes are configured by consumers. Names follow XML 1.0 Fifth Edition, // including supplementary characters through U+EFFFF. This is not a complete // XML processor conformance claim. // -// Nonempty namespace names must be absolute URIs. The parser normalizes XML -// line endings and literal attribute whitespace, expands character references -// and the five predefined entities, and preserves the resulting Unicode -// sequence. It does not obtain DTD attribute defaults, declared attribute types -// or replacement entities. No resource resolver is invoked while parsing. -// A general XML processor mode would need those semantics and explicit bounded -// external resolution; this API does not expose such a mode. +// In the default mode, nonempty namespace names must be absolute URIs. The +// parser normalizes XML line endings and literal attribute whitespace, expands +// character references and the five predefined entities, and preserves the +// resulting Unicode sequence. The default parser invokes no resource resolver. +// +// [ParseOptions] with NonValidating enables a separate bounded XML 1.0 +// nonvalidating processor: DTD entities, attribute defaults and normalization, +// Namespaces 1.0 relative namespace names and supported single-byte encodings. +// XML 1.x declarations are processed using XML 1.0 syntax. External subsets and +// entities require an explicit [EntityResolver]; there is no automatic I/O. +// DTD notation, unparsed-entity and processing-instruction information is +// available through Document.DTD as owned copies. Serialization writes the +// expanded tree and omits the original DTD. DTD validity and XSD validation are +// not performed. Security consumers must deliberately choose their input mode. // // # Node selections and output // diff --git a/document/document.go b/document/document.go index 15edea0..1de48f9 100644 --- a/document/document.go +++ b/document/document.go @@ -59,6 +59,14 @@ type ( NodeSet = tree.NodeSet // RecordOptions selects independent XML subtrees and their resource limits. RecordOptions = tree.RecordOptions + // DocumentType contains declarations read by the nonvalidating XML processor. + DocumentType = tree.DocumentType + // Notation retains an XML notation's external identifiers. + Notation = tree.Notation + // UnparsedEntity retains an external unparsed entity and its notation name. + UnparsedEntity = tree.UnparsedEntity + // ProcessingInstruction is a processing instruction retained from the DTD. + ProcessingInstruction = tree.ProcessingInstruction ) // NewNodeSet selects individual nodes, not their subtrees. Nodes from different diff --git a/document/document_test.go b/document/document_test.go index 36ebd8f..15debdb 100644 --- a/document/document_test.go +++ b/document/document_test.go @@ -500,6 +500,22 @@ func BenchmarkParse(b *testing.B) { } } +func BenchmarkParseEmptyElements(b *testing.B) { + for _, size := range []int{16, 1024, 8192} { + b.Run(strconv.Itoa(size), func(b *testing.B) { + source := []byte(`` + strings.Repeat(``, size) + ``) + b.ReportAllocs() + b.SetBytes(int64(len(source))) + for b.Loop() { + doc, err := document.Parse(b.Context(), source, document.DefaultLimits()) + if err != nil || doc.Root().Name().Local != "r" { + b.Fatal("empty element parsing changed", err) + } + } + }) + } +} + // BenchmarkLongCharacterReference measures a valid reference with leading zeros. // The expanded result remains one character as the encoded input grows. func BenchmarkLongCharacterReference(b *testing.B) { diff --git a/document/parse_options.go b/document/parse_options.go new file mode 100644 index 0000000..0348286 --- /dev/null +++ b/document/parse_options.go @@ -0,0 +1,98 @@ +package document + +import ( + "bytes" + "context" + "io" + + "github.com/skarm/xmlsec/internal/tree" +) + +// EntityResolver opens an explicitly authorized external XML entity. The system +// identifier is relative to baseURI; the returned URI is the entity's actual +// retrieval base. The parser closes every returned stream, including on error. +// Resolution and blocking reads must honor cancellation. Nil permits no I/O. +type EntityResolver = tree.DTDEntityResolver + +// ParseOptions selects the XML processor and its resource policy. The zero value +// retains the DTD-free security parser and uses DefaultLimits. +type ParseOptions struct { + // Limits bounds source bytes, expanded XML, entities, declarations and depth. + // A completely zero value selects DefaultLimits; partial limits must be valid. + Limits Limits + // NonValidating enables XML 1.0 internal/external DTD processing, entity and + // attribute expansion, supported single-byte encodings, and Namespaces 1.0 + // relative namespace names. It does not perform DTD validity checking. + NonValidating bool + // BaseURI identifies the document entity for relative external identifiers. + BaseURI string + // EntityResolver supplies external subsets, parameter and general entities. + // There is no implicit filesystem, network or catalog access. External + // declarations needing retrieval fail when no resolver is configured. + EntityResolver EntityResolver +} + +// Parse constructs an immutable document using the selected XML processor. +func (o ParseOptions) Parse(ctx context.Context, data []byte) (*Document, error) { + return o.ParseReader(ctx, bytes.NewReader(data)) +} + +// ParseReader consumes src without closing it. Nonvalidating DTD processing +// additionally retains bounded decoded/expanded input; the security mode uses +// the incremental parser. External streams belong to the parser. +func (o ParseOptions) ParseReader(ctx context.Context, src io.Reader) (*Document, error) { + p, err := o.options() + if err != nil { + return nil, err + } + + d, err := p.ParseReader(ctx, src) + + return o.withBase(d, err) +} + +// ParseText parses already decoded XML characters. An encoding declaration does +// not reinterpret them; external entities still use their own byte encoding. +func (o ParseOptions) ParseText(ctx context.Context, text string) (*Document, error) { + p, err := o.options() + if err != nil { + return nil, err + } + + d, err := p.ParseText(ctx, text) + + return o.withBase(d, err) +} + +func (o ParseOptions) options() (tree.ParseOptions, error) { + if !o.NonValidating && o.EntityResolver != nil { + return tree.ParseOptions{}, ErrUnsupported + } + + if o.Limits == (Limits{}) { + o.Limits = DefaultLimits() + } + + if err := o.Limits.Validate(); err != nil { + return tree.ParseOptions{}, err + } + + return tree.ParseOptions{ + Limits: o.Limits, BaseURI: o.BaseURI, DTDResolver: o.EntityResolver, + AllowDTD: o.NonValidating, AllowSingleByteEncodings: o.NonValidating, + AllowRelativeNamespaces: o.NonValidating, + }, nil +} + +func (o ParseOptions) withBase(d *Document, err error) (*Document, error) { + if err != nil { + return nil, err + } + + if o.BaseURI != "" { + _, root := tree.Unwrap(d.Root()) + tree.SetNodeBase(d, root, o.BaseURI) + } + + return d, nil +} diff --git a/document/parse_options_test.go b/document/parse_options_test.go new file mode 100644 index 0000000..3269406 --- /dev/null +++ b/document/parse_options_test.go @@ -0,0 +1,245 @@ +package document_test + +import ( + "context" + "errors" + "io" + "net/url" + "strings" + "testing" + "testing/iotest" + + "github.com/skarm/xmlsec/document" +) + +func TestNonValidatingXMLInternalSubset(t *testing.T) { + input := `'> +%attributes; + + + + +]>&text;` + o := document.ParseOptions{NonValidating: true, BaseURI: "https://example.test/document.xml"} + d, err := o.ParseReader(t.Context(), iotest.OneByteReader(strings.NewReader(input))) + if err != nil { + t.Fatal(err) + } + if d.Root().Name().Space != "urn:root" || d.Root().Text() != "hello \r world" { + t.Fatal("entity/default namespace", d.Root().Name(), d.Root().Text()) + } + if value, ok := d.Root().Attribute(document.Name{Local: "tokens"}); !ok || value != "a b" { + t.Fatal("tokenized default attribute", value, ok) + } + dtd, ok := d.DTD() + if !ok || dtd.Name != "r" || dtd.Notations["image"].PublicID != "urn:format:image" || dtd.UnparsedEntities["picture"].Notation != "image" || len(dtd.ProcessingInstructions) != 1 || dtd.ProcessingInstructions[0].Target != "application" || dtd.ProcessingInstructions[0].Data != "retain this" { + t.Fatal("DTD information was lost", dtd, ok) + } + delete(dtd.Notations, "image") + delete(dtd.UnparsedEntities, "picture") + dtd.ProcessingInstructions[0].Target = "changed" + again, _ := d.DTD() + if len(again.Notations) != 1 || len(again.UnparsedEntities) != 1 || again.ProcessingInstructions[0].Target != "application" { + t.Fatal("DTD accessors expose mutable snapshot storage") + } + if _, err := (document.ParseOptions{}).ParseText(t.Context(), input); err == nil { + t.Fatal("default parser enabled DTD processing") + } +} + +func TestNonValidatingXMLExternalEntities(t *testing.T) { + base := "https://example.test/input/document.xml" + resources := map[string]string{ + "https://example.test/input/declarations/root.dtd": `%attrs;`, + "https://example.test/input/attrs.ent": ``, + "https://example.test/input/content.xml": `external`, + } + var opened, closed int + o := document.ParseOptions{NonValidating: true, BaseURI: base, EntityResolver: func(ctx context.Context, system, parent string) (io.ReadCloser, string, error) { + if err := ctx.Err(); err != nil { + return nil, "", err + } + b, err := url.Parse(parent) + if err != nil { + return nil, "", err + } + u, err := b.Parse(system) + if err != nil { + return nil, "", err + } + text, exists := resources[u.String()] + if !exists { + return nil, "", errors.New("resource is not authorized") + } + opened++ + return &entityReader{Reader: strings.NewReader(text), closed: &closed}, u.String(), nil + }} + d, err := o.ParseText(t.Context(), `&content;`) + if err != nil { + t.Fatal(err) + } + if opened != 3 || closed != opened || d.Root().Text() != "external" { + t.Fatal("entity processing/closure", opened, closed, d.Root().Text()) + } + if value, _ := d.Root().Attribute(document.Name{Local: "value"}); value != "one two" { + t.Fatal("external tokenized default", value) + } + dtd, _ := d.DTD() + if dtd.SystemID != "declarations/root.dtd" || dtd.Notations["last"].BaseURI != "https://example.test/input/declarations/root.dtd" { + t.Fatal("external declaration base", dtd) + } + if _, err := (document.ParseOptions{NonValidating: true}).ParseText(t.Context(), ``); !errors.Is(err, document.ErrUnsupported) { + t.Fatal("implicit external access", err) + } +} + +func TestNonValidatingXMLFailureContracts(t *testing.T) { + for _, input := range []string{ + `]>&a;`, + `">]>&a;`, + `]>`, + `]>`, + } { + if d, err := (document.ParseOptions{NonValidating: true}).ParseText(t.Context(), input); d != nil || !errors.Is(err, document.ErrMalformed) { + t.Fatal("accepted malformed DTD/entity", input, d, err) + } + } + limits := document.DefaultLimits() + limits.MaxBytes = 96 + if d, err := (document.ParseOptions{NonValidating: true, Limits: limits}).ParseText(t.Context(), `]>&a;&a;&a;&a;&a;&a;&a;&a;&a;&a;`); d != nil || !errors.Is(err, document.ErrLimit) { + t.Fatal("expanded input limit", d, err) + } + ctx, cancel := context.WithCancel(t.Context()) + cancel() + if d, err := (document.ParseOptions{NonValidating: true}).ParseText(ctx, ``); d != nil || !errors.Is(err, context.Canceled) { + t.Fatal("cancellation", d, err) + } + readFailure := errors.New("read failed") + closeFailure := errors.New("close failed") + closed := 0 + o := document.ParseOptions{NonValidating: true, EntityResolver: func(context.Context, string, string) (io.ReadCloser, string, error) { + return &entityReader{Reader: strings.NewReader(""), closed: &closed, closeErr: closeFailure}, "urn:entity", readFailure + }} + if d, err := o.ParseText(t.Context(), ``); d != nil || !errors.Is(err, readFailure) || !errors.Is(err, closeFailure) || closed != 1 { + t.Fatal("resolver error/closure", d, err, closed) + } +} + +// XML 1.0 Fifth Edition 2.8 [28]/[28a] and 4.1 [68]: parameter +// entities cannot escape subset/declaration boundaries, and standalone entity +// references must resolve to declarations in the document entity itself. +func TestNonValidatingXMLWellFormedness(t *testing.T) { + for _, tc := range []struct { + name, input, external string + valid bool + }{ + {"PE after subset", `] %e;>`, "", false}, + {"PE ends inside declaration", ``, `%e;ANY>`, false}, + {"complete PE declaration", ``, `">%e;`, true}, + {"PE within external declaration", ``, ``, true}, + {"standalone external declaration text", `&e;`, ``, false}, + {"standalone external declaration attribute", ``, ``, false}, + {"standalone PE declaration", `'>%p;]>&e;`, "", false}, + {"standalone local precedence", `]>&e;`, ``, true}, + {"not standalone", `&e;`, ``, true}, + {"external VersionNum", `]>&e;`, `text`, true}, + {"second TextDecl", `]>&e;`, `text`, false}, + {"short PI before DTD", ``, "", true}, + {"short PI before malformed DTD", `]>&e;`, ``, true}, + } { + t.Run(tc.name, func(t *testing.T) { + options := document.ParseOptions{NonValidating: true, EntityResolver: func(context.Context, string, string) (io.ReadCloser, string, error) { + return io.NopCloser(strings.NewReader(tc.external)), "urn:test:declarations", nil + }} + for _, fromBytes := range []bool{true, false} { + var d *document.Document + var err error + if fromBytes { + d, err = options.Parse(t.Context(), []byte(tc.input)) + } else { + d, err = options.ParseText(t.Context(), tc.input) + } + if tc.valid && (d == nil || err != nil) || !tc.valid && (d != nil || !errors.Is(err, document.ErrMalformed)) { + t.Fatalf("bytes=%v valid=%v result=%v error=%v", fromBytes, tc.valid, d, err) + } + } + }) + } +} + +func TestNonValidatingXMLVersionNum(t *testing.T) { + for _, version := range []string{"1.0", "1.1", "1.7", "1.123"} { + input := `text` + options := document.ParseOptions{NonValidating: true} + for _, text := range []bool{false, true} { + var d *document.Document + var err error + if text { + d, err = options.ParseText(t.Context(), input) + } else { + d, err = options.Parse(t.Context(), []byte(input)) + } + if err != nil || d == nil || d.Root().Text() != "text" { + t.Fatalf("XML 1.0 [26] VersionNum %s (text %v): %v", version, text, err) + } + } + } + if d, err := (document.ParseOptions{NonValidating: true}).ParseText(t.Context(), ``); d != nil || err == nil { + t.Fatal("version compatibility enabled XML 1.1 control characters") + } + if _, err := document.Parse(t.Context(), []byte(``), document.DefaultLimits()); err == nil { + t.Fatal("default security parser unexpectedly changed version policy") + } +} + +func FuzzNonValidatingXML(f *testing.F) { + for _, input := range []string{ + ``, + `]>&e;`, + `'>%a;]>`, + `]>&e;`, + `]>&a;`, + ``, + ``, + `]>`, + } { + f.Add([]byte(input)) + } + + f.Fuzz(func(t *testing.T, input []byte) { + if len(input) > 16<<10 { + t.Skip() + } + limits := document.DefaultLimits() + limits.MaxBytes = 64 << 10 + limits.MaxNodes = 4096 + limits.MaxDepth = 64 + doc, err := (document.ParseOptions{NonValidating: true, Limits: limits}).Parse(t.Context(), input) + if err != nil { + return + } + output, err := doc.XML(t.Context()) + if err != nil { + t.Fatal(err) + } + // The expanded snapshot must remain well formed. Use the same language + // profile: relative namespace names are permitted by NonValidating. + limits.MaxBytes = max(limits.MaxBytes, len(output)) + if _, err := (document.ParseOptions{NonValidating: true, Limits: limits}).Parse(t.Context(), output); err != nil { + t.Fatalf("expanded serialization is not parseable: %q: %v", output, err) + } + }) +} + +type entityReader struct { + io.Reader + closed *int + closeErr error +} + +func (r *entityReader) Close() error { + *r.closed++ + return r.closeErr +} diff --git a/document/testdata/fuzz/FuzzNonValidatingXML/c0b432ce1eb33f95 b/document/testdata/fuzz/FuzzNonValidatingXML/c0b432ce1eb33f95 new file mode 100644 index 0000000..da2b713 --- /dev/null +++ b/document/testdata/fuzz/FuzzNonValidatingXML/c0b432ce1eb33f95 @@ -0,0 +1,2 @@ +go test fuzz v1 +[]byte(" 256 { + if config.Client == nil || config.Client.Transport != nil && nilcheck.IsNil(config.Client.Transport) || len(config.AllowedURIs) == 0 || len(config.AllowedURIs) > maximumAllowedURIs { return nil, evidence.ErrInput } @@ -113,7 +139,7 @@ func NewHTTP(config Config) (*HTTP, error) { for _, raw := range config.AllowedURIs { u, err := collectionURI(raw) - if err != nil || u.Scheme != "https" && !(config.AllowHTTP && u.Scheme == "http") { + if err != nil || u.Scheme != lexicon.SchemeHTTPS && !(config.AllowHTTP && u.Scheme == lexicon.SchemeHTTP) { return nil, evidence.ErrInput } @@ -147,16 +173,21 @@ func (c *HTTP) CollectTo(ctx context.Context, store *evidence.Store, requests [] } func normalizeLimits(limits Limits) (Limits, error) { + const ( + minimumTimeout = time.Millisecond + maximumTimeout = 2 * time.Minute + ) + for _, field := range []struct { value *int fallback int maximum int }{ - {value: &limits.MaxResources, fallback: 8, maximum: 64}, - {value: &limits.MaxBytes, fallback: 4 << 20, maximum: 64 << 20}, - {value: &limits.MaxTotalBytes, fallback: 16 << 20, maximum: 64 << 20}, - {value: &limits.MaxRequestBytes, fallback: 64 << 10, maximum: 64 << 10}, - {value: &limits.MaxTotalRequestBytes, fallback: 512 << 10, maximum: 4 << 20}, + {value: &limits.MaxResources, fallback: defaultMaxResources, maximum: maximumResources}, + {value: &limits.MaxBytes, fallback: defaultMaxBytes, maximum: maximumBytes}, + {value: &limits.MaxTotalBytes, fallback: defaultMaxTotalBytes, maximum: maximumTotalBytes}, + {value: &limits.MaxRequestBytes, fallback: defaultMaxRequestBytes, maximum: maximumRequestBytes}, + {value: &limits.MaxTotalRequestBytes, fallback: defaultMaxTotalRequestBytes, maximum: maximumTotalRequestBytes}, } { if *field.value == 0 { *field.value = field.fallback @@ -167,15 +198,15 @@ func normalizeLimits(limits Limits) (Limits, error) { } } - if limits.MaxRedirects < 0 || limits.MaxRedirects > 10 { + if limits.MaxRedirects < 0 || limits.MaxRedirects > maximumRedirects { return Limits{}, evidence.ErrInput } if limits.Timeout == 0 { - limits.Timeout = 10 * time.Second + limits.Timeout = defaultTimeout } - if limits.Timeout < time.Millisecond || limits.Timeout > 2*time.Minute { + if limits.Timeout < minimumTimeout || limits.Timeout > maximumTimeout { return Limits{}, evidence.ErrInput } @@ -183,7 +214,7 @@ func normalizeLimits(limits Limits) (Limits, error) { } func collectionURI(raw string) (*url.URL, error) { - if len(raw) > 8192 || !utf8.ValidString(raw) || strings.ContainsAny(raw, "\x00\r\n") { + if len(raw) > maximumURIBytes || !utf8.ValidString(raw) || strings.ContainsAny(raw, "\x00\r\n") { return nil, evidence.ErrPolicy } @@ -192,6 +223,12 @@ func collectionURI(raw string) (*url.URL, error) { return nil, evidence.ErrPolicy } + // url.Parse does not validate query escapes. Keep the exact URI spelling for + // authorization, using the decoded value only to check percent encoding. + if _, err := url.QueryUnescape(u.RawQuery); err != nil { + return nil, evidence.ErrPolicy + } + return u, nil } @@ -339,7 +376,7 @@ func fetch[T collected](s *collection, ctx context.Context, input Request, read } if input.Method == http.MethodPost { - request.Header.Set("Content-Type", "application/ocsp-request") + request.Header.Set(lexicon.HeaderContentType, ocspRequestMediaType) } response, err := client.Do(request) @@ -409,9 +446,10 @@ func (t *responseTransport) RoundTrip(request *http.Request) (*http.Response, er func (t *responseTransport) failure() error { t.mutex.Lock() - defer t.mutex.Unlock() + err := t.failureError + t.mutex.Unlock() - return t.failureError + return err } func (t *responseTransport) record(err error) { @@ -420,9 +458,9 @@ func (t *responseTransport) record(err error) { } t.mutex.Lock() - defer t.mutex.Unlock() t.failureError = errors.Join(t.failureError, err) + t.mutex.Unlock() } type responseBody struct { diff --git a/evidence/collect/http_test.go b/evidence/collect/http_test.go index bedf900..af92618 100644 --- a/evidence/collect/http_test.go +++ b/evidence/collect/http_test.go @@ -130,3 +130,32 @@ func BenchmarkHTTPCollect(b *testing.B) { } } } + +func TestCollectorRejectsMalformedQueryEscapes(t *testing.T) { + calls := 0 + wantURI := "https://evidence.test/status?x=%23%2f%25+value;other=yes" + client := &http.Client{Transport: transportFunc(func(req *http.Request) (*http.Response, error) { + calls++ + if req.URL.String() != wantURI { + t.Errorf("URI changed: %q", req.URL.String()) + } + + return &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(strings.NewReader("body")), Request: req}, nil + })} + for _, query := range []string{"x=%zz", "x=%", "x=%0"} { + collector, err := collect.NewHTTP(collect.Config{Client: client, AllowedURIs: []string{"https://evidence.test/status?" + query}}) + if collector != nil || !errors.Is(err, evidence.ErrInput) || calls != 0 { + t.Errorf("malformed query %q: collector=%v, err=%v, calls=%d", query, collector, err, calls) + } + } + + collector, err := collect.NewHTTP(collect.Config{Client: client, AllowedURIs: []string{wantURI}}) + if err != nil { + t.Fatal(err) + } + + items, err := collector.Collect(t.Context(), []collect.Request{{Kind: evidence.CRL, URI: wantURI}}) + if err != nil || len(items) != 1 || string(items[0].Bytes()) != "body" || calls != 1 { + t.Fatalf("valid encoded query rejected: %v, calls=%d", err, calls) + } +} diff --git a/evidence/evidence.go b/evidence/evidence.go index ee8dcb7..cb38ed3 100644 --- a/evidence/evidence.go +++ b/evidence/evidence.go @@ -20,6 +20,13 @@ import ( "github.com/skarm/xmlsec/internal/streamio" ) +const ( + // Hard byte ceiling for an observation and for the caller-selected spool byte budget. + maximumRecordBytes = 64 << 20 + maximumRedirects = 10 + maximumURIBytes = 8192 +) + var ( // ErrInput reports malformed observation metadata or collector configuration. ErrInput = errors.New("evidence: invalid input") @@ -57,15 +64,19 @@ type Provenance struct { RetrievedAt time.Time // RequestSHA256 optionally identifies an accompanying request, such as OCSP. // Offline verification must independently bind any expected nonce or CertID. - RequestSHA256 [32]byte + RequestSHA256 [sha256.Size]byte } // Observation owns raw bytes and their digest. The zero value is invalid. // Accessors return fresh copies of all mutable data and support concurrent use. type Observation struct { - kind Kind - data []byte - digest [32]byte + // kind identifies the observed DER object's encoding. + kind Kind + // data owns the exact observed bytes. + data []byte + // digest identifies data by its SHA-256 digest. + digest [sha256.Size]byte + // provenance records collection metadata without authenticating the object. provenance Provenance } @@ -81,7 +92,7 @@ func NewObservation(ctx context.Context, kind Kind, data []byte, provenance Prov return nil, err } - if len(data) > 64<<20 { + if len(data) > maximumRecordBytes { return nil, ErrLimit } @@ -115,7 +126,7 @@ func ReadObservation(ctx context.Context, kind Kind, src io.Reader, provenance P return nil, err } - if maximum < 1 || maximum > 64<<20 { + if maximum < 1 || maximum > maximumRecordBytes { return nil, ErrLimit } @@ -174,9 +185,9 @@ func (o *Observation) Bytes() []byte { } // DigestSHA256 returns the digest of the original byte snapshot. -func (o *Observation) DigestSHA256() [32]byte { +func (o *Observation) DigestSHA256() [sha256.Size]byte { if !o.Valid() { - return [32]byte{} + return [sha256.Size]byte{} } return o.digest @@ -209,7 +220,7 @@ func ValidKind(kind Kind) bool { } func snapshotProvenance(provenance Provenance) (Provenance, error) { - if len(provenance.Redirects) > 10 { + if len(provenance.Redirects) > maximumRedirects { return Provenance{}, ErrLimit } @@ -232,7 +243,7 @@ func snapshotProvenance(provenance Provenance) (Provenance, error) { } func validProvenanceURI(raw string) bool { - if len(raw) > 8192 || !utf8.ValidString(raw) || strings.ContainsAny(raw, "\x00\r\n") { + if len(raw) > maximumURIBytes || !utf8.ValidString(raw) || strings.ContainsAny(raw, "\x00\r\n") { return false } diff --git a/evidence/store.go b/evidence/store.go index f9173c5..476c3bb 100644 --- a/evidence/store.go +++ b/evidence/store.go @@ -14,29 +14,44 @@ import ( "github.com/skarm/xmlsec/internal/streamio" ) +const ( + maximumSpoolRecords = 64 +) + +const ( + spoolRecordPrefix = "observation-" + spoolDirectoryPrefix = "xmlsec-evidence-" +) + // Store retains untrusted observation bytes in a private temporary directory and // keeps their metadata in memory. Construct it with NewStore and call Close when // finished. It supports concurrent calls. The application owns the parent directory // and available disk capacity. Open readers remain caller-owned and must be closed // before releasing storage. Reading a Record does not authenticate its contents. type Store struct { - mutex sync.Mutex + // mutex serializes storage accounting, record creation and closure. + mutex sync.Mutex + // directory holds temporary files owned by this store. directory string - maximum int64 - used int64 - records int - closed bool + // maximum bounds the aggregate stored bytes. + maximum int64 + // used counts bytes charged to the store. + used int64 + // records counts committed records and active write reservations. + records int + // closed prevents further access after store cleanup begins. + closed bool } // NewStore creates a private child directory in directory (empty uses the OS // temporary directory). maximum bounds committed plus reserved input bytes and // must be 1..64 MiB. At most 64 records, including active writes, may coexist. func NewStore(directory string, maximum int64) (*Store, error) { - if maximum < 1 || maximum > 64<<20 { + if maximum < 1 || maximum > maximumRecordBytes { return nil, ErrLimit } - root, err := os.MkdirTemp(directory, "xmlsec-evidence-") + root, err := os.MkdirTemp(directory, spoolDirectoryPrefix) if err != nil { return nil, errors.Join(ErrResource, err) } @@ -59,7 +74,7 @@ func (s *Store) PutReader(ctx context.Context, kind Kind, src io.Reader, provena return nil, err } - if maximum < 1 || maximum > 64<<20 { + if maximum < 1 || maximum > maximumRecordBytes { return nil, ErrLimit } @@ -75,12 +90,12 @@ func (s *Store) PutReader(ctx context.Context, kind Kind, src io.Reader, provena return nil, errors.Join(ErrResource, os.ErrClosed) } - if s.records >= 64 || int64(maximum) > s.maximum-s.used { + if s.records >= maximumSpoolRecords || int64(maximum) > s.maximum-s.used { s.mutex.Unlock() return nil, ErrLimit } - f, err := os.CreateTemp(s.directory, "observation-") + f, err := os.CreateTemp(s.directory, spoolRecordPrefix) if err != nil { s.mutex.Unlock() return nil, errors.Join(ErrResource, err) @@ -141,11 +156,12 @@ func (s *Store) Close() error { } s.mutex.Lock() - defer s.mutex.Unlock() s.closed = true + err := os.RemoveAll(s.directory) + s.mutex.Unlock() - if err := os.RemoveAll(s.directory); err != nil { + if err != nil { return errors.Join(ErrResource, err) } @@ -157,11 +173,17 @@ func (s *Store) Close() error { // and storage reservation; repeated Close calls are safe. Open fails after the // record or its store has closed. type Record struct { - store *Store - path string - size int - kind Kind - digest [32]byte + // store owns the backing file and controls the record's lifetime. + store *Store + // path identifies the store-owned backing file. + path string + // size is the recorded object length in bytes. + size int + // kind identifies the stored DER object's encoding. + kind Kind + // digest binds the record to its exact SHA-256 content digest. + digest [sha256.Size]byte + // provenance retains collection metadata independently of trust. provenance Provenance closed bool // protected by store.mutex } @@ -250,9 +272,9 @@ func (r *Record) Kind() Kind { } // DigestSHA256 returns the digest computed over the exact received bytes. -func (r *Record) DigestSHA256() [32]byte { +func (r *Record) DigestSHA256() [sha256.Size]byte { if r == nil { - return [32]byte{} + return [sha256.Size]byte{} } return r.digest diff --git a/examples/algorithms/main.go b/examples/algorithms/main.go index e7602dc..bacb53e 100644 --- a/examples/algorithms/main.go +++ b/examples/algorithms/main.go @@ -17,6 +17,12 @@ import ( "github.com/skarm/xmlsec/xmldsig" ) +const exampleRSABits = 2048 + +const hmacKeyBytes = 32 + +const pssSaltBytes = 24 + func main() { if err := run(); err != nil { fmt.Fprintln(os.Stderr, err) @@ -27,7 +33,7 @@ func main() { func run() error { ctx := context.Background() - key, err := rsa.GenerateKey(rand.Reader, 2048) + key, err := rsa.GenerateKey(rand.Reader, exampleRSABits) if err != nil { return err } @@ -35,7 +41,7 @@ func run() error { message := "this Object, not the entire Signature container, is protected" spec := xmldsig.SignatureTemplate{ SignatureMethod: xmldsig.RSAPSS, - SignatureParameters: xmldsig.SignatureParameters{PSS: &xmldsig.PSSParameters{Hash: crypto.SHA384, MGFHash: crypto.SHA384, SaltLength: 24}}, + SignatureParameters: xmldsig.SignatureParameters{PSS: &xmldsig.PSSParameters{Hash: crypto.SHA384, MGFHash: crypto.SHA384, SaltLength: pssSaltBytes}}, References: []xmldsig.ReferenceTemplate{{URI: "#payload", Transforms: []reference.Transform{{Algorithm: c14n.Exclusive}}}}, Objects: []xmldsig.ObjectTemplate{{ID: "payload", Text: &message}}, } @@ -71,7 +77,7 @@ func run() error { fmt.Printf("RSA-PSS: hash=%v mgf=%v salt=%d references=%d container-covered=%v\n", p.Hash, p.MGFHash, p.SaltLength, len(result.References()), result.CoversElement(signed.Root())) // This secret is local to the demo, never printed or written to disk. - secret := make([]byte, 32) + secret := make([]byte, hmacKeyBytes) if _, err = rand.Read(secret); err != nil { return err } diff --git a/examples/encryption/main.go b/examples/encryption/main.go index 35c9336..55140d3 100644 --- a/examples/encryption/main.go +++ b/examples/encryption/main.go @@ -19,6 +19,10 @@ import ( "github.com/skarm/xmlsec/xmlenc" ) +const exampleRSABits = 2048 + +const aesKeyBytes = 32 + func main() { if err := run(); err != nil { log.Fatal(err) @@ -28,7 +32,7 @@ func main() { func run() error { ctx := context.Background() - recipient, err := rsa.GenerateKey(rand.Reader, 2048) + recipient, err := rsa.GenerateKey(rand.Reader, exampleRSABits) if err != nil { return err } @@ -70,7 +74,7 @@ func run() error { fmt.Printf("Restored content of %s: %s; header is %s\n", parent.Name().Local, parent.Text(), contentPlain.Root().Children()[0].Text()) // The application can also distribute a direct AES data key independently. - secret := make([]byte, 32) + secret := make([]byte, aesKeyBytes) if _, err := rand.Read(secret); err != nil { return err } diff --git a/examples/internal/democert/cert.go b/examples/internal/democert/cert.go index d5b6882..0be2c3d 100644 --- a/examples/internal/democert/cert.go +++ b/examples/internal/democert/cert.go @@ -12,6 +12,15 @@ import ( "time" ) +const ( + rootBackdate = 24 * time.Hour + rootLifetime = 365 * 24 * time.Hour + leafBackdate = time.Hour + leafLifetime = 24 * time.Hour + crlBackdate = time.Minute + crlLifetime = time.Hour +) + // Chain contains disposable certificates, private keys and a complete CRL for examples. type Chain struct { Root *x509.Certificate @@ -50,7 +59,7 @@ func New(at time.Time) (Chain, error) { return x509.ParseCertificate(b) } - rt := &x509.Certificate{SerialNumber: big.NewInt(1), Subject: pkix.Name{CommonName: "ephemeral example root"}, NotBefore: at.Add(-24 * time.Hour), NotAfter: at.Add(365 * 24 * time.Hour), IsCA: true, BasicConstraintsValid: true, KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign, SubjectKeyId: []byte{1, 2, 3, 4}} + rt := &x509.Certificate{SerialNumber: big.NewInt(1), Subject: pkix.Name{CommonName: "ephemeral example root"}, NotBefore: at.Add(-rootBackdate), NotAfter: at.Add(rootLifetime), IsCA: true, BasicConstraintsValid: true, KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign, SubjectKeyId: []byte{1, 2, 3, 4}} var e error @@ -60,7 +69,7 @@ func New(at time.Time) (Chain, error) { } leaf := func(serial int64, cn string) *x509.Certificate { - return &x509.Certificate{SerialNumber: big.NewInt(serial), Subject: pkix.Name{CommonName: cn}, NotBefore: at.Add(-time.Hour), NotAfter: at.Add(24 * time.Hour), BasicConstraintsValid: true, KeyUsage: x509.KeyUsageDigitalSignature} + return &x509.Certificate{SerialNumber: big.NewInt(serial), Subject: pkix.Name{CommonName: cn}, NotBefore: at.Add(-leafBackdate), NotAfter: at.Add(leafLifetime), BasicConstraintsValid: true, KeyUsage: x509.KeyUsageDigitalSignature} } st := leaf(2, "example document signer") st.ExtKeyUsage = []x509.ExtKeyUsage{x509.ExtKeyUsageCodeSigning} @@ -91,7 +100,7 @@ func New(at time.Time) (Chain, error) { return c, e } - c.CRL, e = x509.CreateRevocationList(rand.Reader, &x509.RevocationList{Number: big.NewInt(1), ThisUpdate: at.Add(-time.Minute), NextUpdate: at.Add(time.Hour)}, c.Root, c.RootKey) + c.CRL, e = x509.CreateRevocationList(rand.Reader, &x509.RevocationList{Number: big.NewInt(1), ThisUpdate: at.Add(-crlBackdate), NextUpdate: at.Add(crlLifetime)}, c.Root, c.RootKey) return c, e } diff --git a/examples/keyinfo/main.go b/examples/keyinfo/main.go index 35db474..e2ac032 100644 --- a/examples/keyinfo/main.go +++ b/examples/keyinfo/main.go @@ -17,6 +17,7 @@ import ( "github.com/skarm/xmlsec/c14n" "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/keyinfo" "github.com/skarm/xmlsec/reference" "github.com/skarm/xmlsec/xmldsig" @@ -92,7 +93,7 @@ func run() error { result, err := xmldsig.VerifySignatureWithKeyInfo(ctx, received, signatures[0], xmldsig.KeyInfoVerifyOptions{ Signature: xmldsig.SignatureOptions{RequiredElements: []document.Element{payload}, ExpectedURIs: []string{"#payload"}}, - KeyInfo: keyinfo.Options{AllowedKeyData: []document.Name{{Space: keyinfo.Namespace11, Local: "ECKeyValue"}}}, + KeyInfo: keyinfo.Options{AllowedKeyData: []document.Name{{Space: keyinfo.Namespace11, Local: lexicon.DSigElementECKeyValue}}}, SelectTrustedKey: func(ctx context.Context, untrusted *keyinfo.Result) (int, error) { if err := ctx.Err(); err != nil { return -1, err diff --git a/examples/manifest/main.go b/examples/manifest/main.go index 1a5cd21..3429091 100644 --- a/examples/manifest/main.go +++ b/examples/manifest/main.go @@ -19,6 +19,12 @@ import ( "github.com/skarm/xmlsec/xmldsig" ) +const ( + manifestMaxDepth = 4 + manifestMaxNodes = 8 + manifestMaxReferences = 16 +) + func main() { if err := run(); err != nil { log.Fatal(err) @@ -53,7 +59,7 @@ func run() error { fetches := 0 opt := xmldsig.SignatureOptions{ Policy: xmldsig.ModernPolicy(), ExpectedURIs: []string{"#manifest"}, - Manifests: xmldsig.ManifestPolicy{Mode: xmldsig.ManifestRecursive, MaxDepth: 4, MaxManifests: 8, MaxReferences: 16}, + Manifests: xmldsig.ManifestPolicy{Mode: xmldsig.ManifestRecursive, MaxDepth: manifestMaxDepth, MaxManifests: manifestMaxNodes, MaxReferences: manifestMaxReferences}, External: &xmldsig.ExternalReferences{ AllowedURIs: []string{uriUrnExampleAttachment}, Resolver: xmldsig.ResolverFunc(func(ctx context.Context, uri string) (io.ReadCloser, error) { diff --git a/examples/redirect/main.go b/examples/redirect/main.go index 7aa9fad..24e26bc 100644 --- a/examples/redirect/main.go +++ b/examples/redirect/main.go @@ -20,6 +20,12 @@ import ( "github.com/skarm/xmlsec/xmldsig" ) +const ( + replayCapacity = 10 + replayMaxTTL = time.Minute + replayEntryLifetime = 30 * time.Second +) + func main() { if err := run(); err != nil { log.Fatal(err) @@ -55,14 +61,14 @@ func run() error { fmt.Printf("binding-integrity=valid kind=%s protocol-validation=not-performed\n", verified.Kind()) // Separate storage primitive: a real profile must validate issuer, destination, // request/session correlation, subject and time BEFORE consuming these IDs. - store, err := replay.NewMemoryStore(replay.Config{Capacity: 10, MaxTTL: time.Minute}) + store, err := replay.NewMemoryStore(replay.Config{Capacity: replayCapacity, MaxTTL: replayMaxTTL}) if err != nil { return err } now := time.Now().UTC() - entries := []replay.Entry{{Scope: "example-only", ID: "already-validated-id", ExpiresAt: now.Add(30 * time.Second)}} + entries := []replay.Entry{{Scope: "example-only", ID: "already-validated-id", ExpiresAt: now.Add(replayEntryLifetime)}} if err = store.Consume(ctx, entries, now); err != nil { return err } diff --git a/examples/sso/main.go b/examples/sso/main.go index d410847..5a33206 100644 --- a/examples/sso/main.go +++ b/examples/sso/main.go @@ -24,6 +24,8 @@ import ( "github.com/skarm/xmlsec/xmldsig" ) +const browserBindingBytes = 32 + const ( spEntity = "https://sp.example.test/entity" idpEntity = "https://idp.example.test/entity" @@ -108,7 +110,14 @@ func run() error { return e } - browserSecret := make([]byte, 32) + // Provision the demo account's opaque identifier before its authentication + // request. A real IdP persists this association with the account and both peers. + identifier, e := protocol.NewPersistentNameID(idpEntity, spEntity) + if e != nil { + return e + } + + browserSecret := make([]byte, browserBindingBytes) if _, e = rand.Read(browserSecret); e != nil { return e } @@ -124,7 +133,7 @@ func run() error { } // In a real IdP these values come from the application's completed, verified // authentication, never from untrusted request fields or this sample alone. - authenticated := idp.Authentication{NameID: "alice", NameIDFormat: protocol.NameIDPersistent, AuthnInstant: now.Add(time.Second), AuthnContext: authClass, FreshAuthentication: true, InteractionUsed: true, SessionIndex: "_session", Attributes: []protocol.Attribute{{Name: "email", Values: []string{"alice@example.test"}}}} + authenticated := idp.Authentication{NameID: identifier.Value, NameIDFormat: identifier.Format, NameQualifier: identifier.NameQualifier, SPNameQualifier: identifier.SPNameQualifier, AuthnInstant: now.Add(time.Second), AuthnContext: authClass, FreshAuthentication: true, InteractionUsed: true, SessionIndex: "_session", Attributes: []protocol.Attribute{{Name: "email", Values: []string{"alice@example.test"}}}} response, e := identity.Issue(ctx, accepted, authenticated, now.Add(2*time.Second)) if e != nil { diff --git a/examples/streaming/main.go b/examples/streaming/main.go index 990089b..7718edb 100644 --- a/examples/streaming/main.go +++ b/examples/streaming/main.go @@ -17,6 +17,14 @@ import ( "github.com/skarm/xmlsec/transform" ) +const ( + streamTimeout = time.Minute + maximumRecordBytes = 1 << 20 + maximumInputBytes = 64 << 20 + maximumRecords = 10_000 + maximumOutputBytes = 64 << 20 +) + func main() { stdin := flag.Bool("stdin", false, "read a from stdin instead of the demonstration input") @@ -34,7 +42,7 @@ func main() { } func run(ctx context.Context, input io.Reader, output io.Writer) error { - engine, err := transform.NewEngine(transform.Config{Limits: transform.Limits{Timeout: time.Minute}}) + engine, err := transform.NewEngine(transform.Config{Limits: transform.Limits{Timeout: streamTimeout}}) if err != nil { return err } @@ -47,17 +55,17 @@ func run(ctx context.Context, input io.Reader, output io.Writer) error { } recordLimits := document.DefaultLimits() - recordLimits.MaxBytes = 1 << 20 + recordLimits.MaxBytes = maximumRecordBytes _, err = sheet.WriteRecords(ctx, output, input, transform.StreamOptions{ Records: document.RecordOptions{ Path: []document.Name{{Local: "feed"}, {Local: "item"}}, Limits: recordLimits, - MaxInputBytes: 64 << 20, - MaxRecords: 10_000, + MaxInputBytes: maximumInputBytes, + MaxRecords: maximumRecords, }, Separator: "\n", - MaxOutputBytes: 64 << 20, + MaxOutputBytes: maximumOutputBytes, }) return err diff --git a/examples/trust/main.go b/examples/trust/main.go index d3c454d..2558c53 100644 --- a/examples/trust/main.go +++ b/examples/trust/main.go @@ -25,6 +25,16 @@ import ( "github.com/skarm/xmlsec/xmlenc" ) +const exampleRSABits = 2048 + +const ( + certificateBackdate = time.Hour + rootLifetime = 24 * time.Hour + signerLifetime = time.Hour + crlBackdate = time.Minute + crlLifetime = time.Hour +) + func main() { if err := run(); err != nil { log.Fatal(err) @@ -45,7 +55,7 @@ func run() error { return err } - caTemplate := &x509.Certificate{SerialNumber: big.NewInt(1), Subject: pkix.Name{CommonName: "Example CA"}, NotBefore: now.Add(-time.Hour), NotAfter: now.Add(24 * time.Hour), IsCA: true, BasicConstraintsValid: true, KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign, SubjectKeyId: []byte{1, 2, 3, 4}} + caTemplate := &x509.Certificate{SerialNumber: big.NewInt(1), Subject: pkix.Name{CommonName: "Example CA"}, NotBefore: now.Add(-certificateBackdate), NotAfter: now.Add(rootLifetime), IsCA: true, BasicConstraintsValid: true, KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign, SubjectKeyId: []byte{1, 2, 3, 4}} rootDER, err := x509.CreateCertificate(rand.Reader, caTemplate, caTemplate, issuer.Public(), issuer) if err != nil { @@ -57,14 +67,14 @@ func run() error { return err } - leafTemplate := &x509.Certificate{SerialNumber: big.NewInt(2), Subject: pkix.Name{CommonName: "Example signer"}, NotBefore: now.Add(-time.Hour), NotAfter: now.Add(time.Hour), KeyUsage: x509.KeyUsageDigitalSignature, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageCodeSigning}, BasicConstraintsValid: true} + leafTemplate := &x509.Certificate{SerialNumber: big.NewInt(2), Subject: pkix.Name{CommonName: "Example signer"}, NotBefore: now.Add(-certificateBackdate), NotAfter: now.Add(signerLifetime), KeyUsage: x509.KeyUsageDigitalSignature, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageCodeSigning}, BasicConstraintsValid: true} leafDER, err := x509.CreateCertificate(rand.Reader, leafTemplate, root, signingKey.Public(), issuer) if err != nil { return err } - crlDER, err := x509.CreateRevocationList(rand.Reader, &x509.RevocationList{Number: big.NewInt(1), ThisUpdate: now.Add(-time.Minute), NextUpdate: now.Add(time.Hour)}, root, issuer) + crlDER, err := x509.CreateRevocationList(rand.Reader, &x509.RevocationList{Number: big.NewInt(1), ThisUpdate: now.Add(-crlBackdate), NextUpdate: now.Add(crlLifetime)}, root, issuer) if err != nil { return err } @@ -89,7 +99,7 @@ func run() error { return err } - recipient, err := rsa.GenerateKey(rand.Reader, 2048) + recipient, err := rsa.GenerateKey(rand.Reader, exampleRSABits) if err != nil { return err } diff --git a/internal/ber/ber.go b/internal/ber/ber.go index 3ed520e..d1549c0 100644 --- a/internal/ber/ber.go +++ b/internal/ber/ber.go @@ -5,7 +5,27 @@ package ber import ( "bytes" "context" + "encoding/asn1" "errors" + "math" +) + +// BER identifier and length octets. These are wire-format values; callers +// supply independent work and content budgets through Limits. +const ( + constructedBit = 0x20 + highTagNumber = 0x1f + base128Continuation = 0x80 + base128PayloadMask = 0x7f + base128Radix = 128 + maximumTagOctets = 5 + indefiniteLength = 0x80 + longLengthCountMask = 0x7f + maximumLengthOctets = 8 + lengthOctetRadix = 256 + minimumHeaderBytes = 2 + endOfContentsBytes = 2 + constructedOctetTag = constructedBit | asn1.TagOctetString ) // ErrSyntax reports malformed BER, including misplaced or missing EOC. @@ -16,16 +36,25 @@ var ErrLimit = errors.New("BER limit exceeded") // Limits bounds a complete structural scan, with the root at depth one. type Limits struct { + // MaxBytes bounds the complete input encoding in bytes and must be positive. MaxBytes int + // MaxDepth bounds ASN.1 nesting with the root at depth one. MaxDepth int + // MaxNodes bounds the total number of scanned ASN.1 values. MaxNodes int } // Value preserves the original tag, complete encoding, body and child order. type Value struct { - Tag byte - Full []byte - Body []byte + // Tag retains the original single-octet ASN.1 tag. + Tag byte + // Full borrows the complete original encoding, including an indefinite value's + // end marker. + Full []byte + // Body borrows the value's content octets, excluding its outer end marker. + Body []byte + // Children preserves the scanned children of a constructed value in source + // order. Children []Value } @@ -77,7 +106,7 @@ func (s *berScanner) value(b []byte, depth int) (Value, int, error) { } v := Value{Tag: b[0]} - if indefinite && v.Tag&0x20 == 0 { + if indefinite && v.Tag&constructedBit == 0 { return v, 0, ErrSyntax } @@ -87,14 +116,14 @@ func (s *berScanner) value(b []byte, depth int) (Value, int, error) { } at := header - if v.Tag&0x20 == 0 { + if v.Tag&constructedBit == 0 { at = end } for at < end { - if indefinite && end-at >= 2 && b[at] == 0 && b[at+1] == 0 { - v.Body, v.Full = b[header:at], b[:at+2] - return v, at + 2, nil + if indefinite && end-at >= endOfContentsBytes && b[at] == 0 && b[at+1] == 0 { + v.Body, v.Full = b[header:at], b[:at+endOfContentsBytes] + return v, at + endOfContentsBytes, nil } child, used, err := s.value(b[at:end], depth+1) @@ -123,13 +152,13 @@ func AppendOctets(ctx context.Context, value Value, dst *bytes.Buffer, maximum i } switch value.Tag { - case 4: + case asn1.TagOctetString: if len(value.Body) > maximum-dst.Len() { return ErrLimit } dst.Write(value.Body) - case 0x24: + case constructedOctetTag: for _, child := range value.Children { if err := AppendOctets(ctx, child, dst, maximum); err != nil { return err @@ -150,13 +179,13 @@ func OctetLength(ctx context.Context, value Value, maximum int) (int, error) { } switch value.Tag { - case 4: + case asn1.TagOctetString: if len(value.Body) > maximum { return 0, ErrLimit } return len(value.Body), nil - case 0x24: + case constructedOctetTag: total := 0 for _, child := range value.Children { size, err := OctetLength(ctx, child, maximum-total) @@ -176,33 +205,35 @@ func OctetLength(ctx context.Context, value Value, maximum int) (int, error) { // BER permits nonminimal length octets; only the selected wrapper headers are // normalized. Opaque fields reach the strict DER parser with their original bytes. func berHeader(b []byte) (header, length int, indefinite bool, err error) { - if len(b) < 2 || b[0] == 0 { + // Universal tag zero is reserved for the primitive 00 00 terminator, + // consumed only by an indefinite parent's loop. It has no constructed form. + if len(b) < minimumHeaderBytes || b[0] == 0 || b[0] == constructedBit { return 0, 0, false, ErrSyntax } at := 1 - if b[0]&31 == 31 { + if b[0]&highTagNumber == highTagNumber { tag := 0 for { - if at >= len(b) || at > 5 || at == 1 && b[at]&127 == 0 { + if at >= len(b) || at > maximumTagOctets || at == 1 && b[at]&base128PayloadMask == 0 { return 0, 0, false, ErrSyntax } - if tag > (0x7fffffff-int(b[at]&127))/128 { + if tag > (math.MaxInt32-int(b[at]&base128PayloadMask))/base128Radix { return 0, 0, false, ErrSyntax } - tag = tag*128 + int(b[at]&127) + tag = tag*base128Radix + int(b[at]&base128PayloadMask) at++ - if b[at-1]&128 == 0 { + if b[at-1]&base128Continuation == 0 { break } } - if tag < 31 { + if tag < highTagNumber { return 0, 0, false, ErrSyntax } } @@ -214,23 +245,23 @@ func berHeader(b []byte) (header, length int, indefinite bool, err error) { length = int(b[at]) at++ - if length == 128 { + if length == indefiniteLength { return at, 0, true, nil } - if length > 128 { - count := length & 127 - if count > 8 || count > len(b)-at { + if length > indefiniteLength { + count := length & longLengthCountMask + if count > maximumLengthOctets || count > len(b)-at { return 0, 0, false, ErrSyntax } length = 0 for _, c := range b[at : at+count] { - if length > len(b)/256 { + if length > len(b)/lengthOctetRadix { return 0, 0, false, ErrSyntax } - length = length*256 + int(c) + length = length*lengthOctetRadix + int(c) } at += count diff --git a/internal/ber/ber_test.go b/internal/ber/ber_test.go index e7a5dcf..2d67c03 100644 --- a/internal/ber/ber_test.go +++ b/internal/ber/ber_test.go @@ -95,3 +95,39 @@ func TestMalformedAndCancelled(t *testing.T) { t.Fatal("non-octet length:", err) } } + +func TestEndOfContentsReservedTag(t *testing.T) { + limits := ber.Limits{MaxBytes: 100, MaxDepth: 8, MaxNodes: 32} + for _, tc := range []struct { + name string + raw []byte + }{ + {"constructed tag zero", []byte{0x20, 0}}, + {"constructed tag zero with contents", []byte{0x20, 2, 4, 0}}, + {"constructed tag zero indefinite", []byte{0x20, 0x80, 0, 0}}, + {"constructed tag zero in definite sequence", []byte{0x30, 2, 0x20, 0}}, + {"constructed tag zero in indefinite sequence", []byte{0x30, 0x80, 0x20, 0, 0, 0}}, + } { + t.Run(tc.name, func(t *testing.T) { + if _, err := ber.Read(t.Context(), tc.raw, limits); !errors.Is(err, ber.ErrSyntax) { + t.Fatalf("Read(%x) = %v, want ErrSyntax", tc.raw, err) + } + }) + } + + // Tag zero is valid in other classes. Only universal tag zero is reserved + // for the primitive 00 00 terminator of an indefinite-length encoding. + for _, raw := range [][]byte{ + {0x40, 0}, + {0x60, 0}, + {0x80, 0}, + {0xa0, 0}, + {0xc0, 0}, + {0xe0, 0}, + {0x30, 0x80, 4, 0, 0, 0}, + } { + if _, err := ber.Read(t.Context(), raw, limits); err != nil { + t.Fatalf("valid tag/terminator %x: %v", raw, err) + } + } +} diff --git a/internal/der/der.go b/internal/der/der.go index ad6b521..59a2c4f 100644 --- a/internal/der/der.go +++ b/internal/der/der.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "io" + "math" "slices" "time" "unicode/utf8" @@ -15,6 +16,36 @@ import ( "github.com/skarm/xmlsec/internal/streamio" ) +const ( + defaultMaxNodes = 100000 + defaultReadDepth = 33 + generalizedTimeDigits = 14 + generalizedTimeSecondsLayout = "20060102150405" + utcTimeDigits = 12 + utcTimeSecondsLayout = "060102150405" +) + +// ASN.1 identifier and length parameters shared by the DER encoder and the +// borrowed views. The views use them only after complete DER validation. +const ( + identifierClassShift = 6 + identifierClassMask = 3 + highTagNumber = 0x1f + constructedBit = 0x20 + base128Bits = 7 + base128PayloadMask = 0x7f + base128Continuation = 0x80 + longLengthFlag = 0x80 + longLengthCountMask = 0x7f + maximumLengthOctets = 8 + octetBits = 8 + octetRadix = 256 + minimumHeaderBytes = 2 + sequenceTag = constructedBit | asn1.TagSequence + setTag = constructedBit | asn1.TagSet + contextConstructed = asn1.ClassContextSpecific<= 0 && class <= 3 && tag >= 0 && tag < 31 { - tagByte := byte(class&3)<<6 | byte(tag&0x1f) + if class >= 0 && class <= asn1.ClassPrivate && tag >= 0 && tag < highTagNumber { + tagByte := byte(class&identifierClassMask)<= 0 && tag < 31 { - return wrapParts(0xa0|byte(tag), parts...) + if tag >= 0 && tag < highTagNumber { + return wrapParts(contextConstructed|byte(tag), parts...) } - return Wrap(2, tag, true, bytes.Join(parts, nil)) + return Wrap(asn1.ClassContextSpecific, tag, true, bytes.Join(parts, nil)) } // OID encodes an object identifier, returning nil for an invalid value. @@ -189,7 +220,7 @@ func OID(o asn1.ObjectIdentifier) []byte { } // Octets encodes a DER OCTET STRING. -func Octets(b []byte) []byte { return Wrap(0, asn1.TagOctetString, false, b) } +func Octets(b []byte) []byte { return Wrap(asn1.ClassUniversal, asn1.TagOctetString, false, b) } // Int encodes a DER INTEGER. func Int(n int) []byte { b, _ := asn1.Marshal(n); return b } @@ -228,46 +259,46 @@ func SetFields(v asn1.RawValue, c, t, maximum int) ([]asn1.RawValue, error) { // storage. Its length is checked before allocating the body; trailing data is // rejected. The parsed model and this encoding remain materialized. func ReadSequence(ctx context.Context, src io.Reader, maximum int) (asn1.RawValue, error) { - if ctx == nil || nilcheck.IsNil(src) || maximum < 2 { + if ctx == nil || nilcheck.IsNil(src) || maximum < minimumHeaderBytes { return asn1.RawValue{}, ErrInvalidDER } source := &streamio.Reader{Context: ctx, Source: src, Limit: int64(maximum), LimitError: ErrLimit} - var header [10]byte - if _, err := io.ReadFull(source, header[:2]); err != nil { + var header [minimumHeaderBytes + maximumLengthOctets]byte + if _, err := io.ReadFull(source, header[:minimumHeaderBytes]); err != nil { return asn1.RawValue{}, err } - if header[0] != 0x30 { + if header[0] != sequenceTag { return asn1.RawValue{}, ErrInvalidDER } - length, used := int(header[1]), 2 - if length >= 128 { - count := length & 127 - if count == 0 || count > 8 { + length, used := int(header[1]), minimumHeaderBytes + if length >= longLengthFlag { + count := length & longLengthCountMask + if count == 0 || count > maximumLengthOctets { return asn1.RawValue{}, ErrInvalidDER } - if _, err := io.ReadFull(source, header[2:2+count]); err != nil { + if _, err := io.ReadFull(source, header[minimumHeaderBytes:minimumHeaderBytes+count]); err != nil { return asn1.RawValue{}, err } - if header[2] == 0 { + if header[minimumHeaderBytes] == 0 { return asn1.RawValue{}, ErrInvalidDER } length = 0 - for _, c := range header[2 : 2+count] { - if length > (maximum-int(c))/256 { + for _, c := range header[minimumHeaderBytes : minimumHeaderBytes+count] { + if length > (maximum-int(c))/octetRadix { return asn1.RawValue{}, ErrLimit } - length = length*256 + int(c) + length = length*octetRadix + int(c) } - if length < 128 { + if length < longLengthFlag { return asn1.RawValue{}, ErrInvalidDER } @@ -301,25 +332,25 @@ func ReadSequence(ctx context.Context, src io.Reader, maximum int) (asn1.RawValu // Header encodes a short-tag DER header without allocating its payload. // tag includes class/construction bits; length must be nonnegative. func Header(tag byte, length int) []byte { - if length < 0 || tag&0x1f == 0x1f { + if length < 0 || tag&highTagNumber == highTagNumber { panic("der.Header: invalid internal tag or length") } - if length < 128 { - return []byte{tag, byte(length & 127)} + if length < longLengthFlag { + return []byte{tag, byte(length & longLengthCountMask)} } - var encoded [8]byte + var encoded [maximumLengthOctets]byte used := len(encoded) for length > 0 { used-- - encoded[used] = byte(length & 255) - length >>= 8 + encoded[used] = byte(length & math.MaxUint8) + length >>= octetBits } - result := make([]byte, 2, 2+len(encoded)-used) - result[0], result[1] = tag, 0x80|byte(len(encoded)-used) + result := make([]byte, minimumHeaderBytes, minimumHeaderBytes+len(encoded)-used) + result[0], result[1] = tag, longLengthFlag|byte(len(encoded)-used) return append(result, encoded[used:]...) } @@ -330,7 +361,7 @@ func read(b []byte, depth int, count *int, maxDepth, maxNodes int) (asn1.RawValu return asn1.RawValue{}, nil, fmt.Errorf("%w: %w", ErrInvalidDER, ErrLimit) } - if len(b) < 2 { + if len(b) < minimumHeaderBytes { return asn1.RawValue{}, nil, ErrInvalidDER } @@ -369,7 +400,7 @@ func validateConstructedDER(v asn1.RawValue, depth int, count *int, maxDepth, ma return err } - if v.Class == 0 && v.Tag == asn1.TagSet && previous.FullBytes != nil { + if v.Class == asn1.ClassUniversal && v.Tag == asn1.TagSet && previous.FullBytes != nil { lexicalOrder = lexicalOrder && bytes.Compare(previous.FullBytes, child.FullBytes) <= 0 tagOrder = tagOrder && (previous.Class < child.Class || previous.Class == child.Class && previous.Tag < child.Tag) } @@ -381,7 +412,7 @@ func validateConstructedDER(v asn1.RawValue, depth int, count *int, maxDepth, ma // SET and SET OF share a tag. Without a schema, either canonical order is // possible (X.690 10.3 / 11.6). Known SET OF fields use SetFields to enforce // their schema's lexical ordering, including implicitly tagged sets. - if v.Class == 0 && v.Tag == asn1.TagSet && !lexicalOrder && !tagOrder { + if v.Class == asn1.ClassUniversal && v.Tag == asn1.TagSet && !lexicalOrder && !tagOrder { return fmt.Errorf("%w: SET order", ErrInvalidDER) } @@ -389,7 +420,7 @@ func validateConstructedDER(v asn1.RawValue, depth int, count *int, maxDepth, ma } func validateUniversalDER(v asn1.RawValue) error { - if v.Class != 0 { + if v.Class != asn1.ClassUniversal { return nil } @@ -399,7 +430,8 @@ func validateUniversalDER(v asn1.RawValue) error { return ErrInvalidDER } case asn1.TagBoolean: - if v.IsCompound || len(v.Bytes) != 1 || (v.Bytes[0] != 0 && v.Bytes[0] != 255) { + const derTrue = 0xff + if v.IsCompound || len(v.Bytes) != 1 || (v.Bytes[0] != 0 && v.Bytes[0] != derTrue) { return ErrInvalidDER } case asn1.TagInteger, asn1.TagEnum: @@ -437,7 +469,7 @@ func validDEROID(content []byte) bool { } for at := 0; at < len(content); { - if content[at] == 128 { // Leading zero base-128 group is nonminimal. + if content[at] == base128Continuation { // Leading zero base-128 group is nonminimal. return false } @@ -451,12 +483,12 @@ func validDEROID(content []byte) bool { octet := content[at] at++ - value = value<<7 | int64(octet&127) - if value > 1<<31-1 { + value = value< math.MaxInt32 { return false } - if octet&128 == 0 { + if octet&base128Continuation == 0 { break } } @@ -476,13 +508,19 @@ func validDERContent(tag int, b []byte) bool { } } case asn1.TagBMPString: - if len(b)%2 != 0 { + const ( + codeUnitBytes = 2 + firstSurrogate = 0xd800 + lastSurrogate = 0xdfff + firstNonchar = 0xfffe + ) + if len(b)%codeUnitBytes != 0 { return false } - for i := 0; i < len(b); i += 2 { - c := uint16(b[i])<<8 | uint16(b[i+1]) - if c >= 0xd800 && c <= 0xdfff || c >= 0xfffe { + for i := 0; i < len(b); i += codeUnitBytes { + c := uint16(b[i])<= firstSurrogate && c <= lastSurrogate || c >= firstNonchar { return false } } @@ -496,18 +534,29 @@ func validDERContent(tag int, b []byte) bool { func validDERCharacter(tag int, c byte) bool { switch tag { case asn1.TagIA5String: - return c < 128 + return c < utf8.RuneSelf case asn1.TagNumericString: return c == ' ' || c >= '0' && c <= '9' default: // X.680 Table 10, without Go's legacy '*' and '&' exceptions. - return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || bytes.IndexByte([]byte(" '()+,-./:=?"), c) >= 0 + const printablePunctuation = " '()+,-./:=?" + return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || bytes.IndexByte([]byte(printablePunctuation), c) >= 0 } } func validDERTime(tag int, b []byte) bool { - width, layout := 12, "060102150405" + // Fractional GeneralizedTime needs a dot, at least one digit and final Z. + const minimumFractionSuffixBytes = 3 + + const ( + generalizedClockOffset = 8 + leapSecondClock = "235960" + ordinaryLastSecond = "59" + secondDigits = 2 + ) + + width, layout := utcTimeDigits, utcTimeSecondsLayout if tag == asn1.TagGeneralizedTime { - width, layout = 14, "20060102150405" + width, layout = generalizedTimeDigits, generalizedTimeSecondsLayout } if len(b) < width+1 || b[len(b)-1] != 'Z' { @@ -521,7 +570,7 @@ func validDERTime(tag int, b []byte) bool { } if len(b) != width+1 { - if tag != asn1.TagGeneralizedTime || len(b) < width+3 || b[width] != '.' || b[len(b)-2] == '0' { + if tag != asn1.TagGeneralizedTime || len(b) < width+minimumFractionSuffixBytes || b[width] != '.' || b[len(b)-2] == '0' { return false } @@ -534,9 +583,9 @@ func validDERTime(tag int, b []byte) bool { } calendar := string(b[:width]) - if tag == asn1.TagGeneralizedTime && calendar[8:] == "235960" { + if tag == asn1.TagGeneralizedTime && calendar[generalizedClockOffset:] == leapSecondClock { // GeneralizedTime follows ISO 8601, including the leap-second notation. - calendar = calendar[:width-2] + "59" + calendar = calendar[:width-secondDigits] + ordinaryLastSecond } _, err := time.Parse(layout, calendar) @@ -545,18 +594,24 @@ func validDERTime(tag int, b []byte) bool { } func validDERInteger(v asn1.RawValue) bool { - return !(v.IsCompound || len(v.Bytes) == 0 || len(v.Bytes) > 1 && ((v.Bytes[0] == 0 && v.Bytes[1]&128 == 0) || (v.Bytes[0] == 255 && v.Bytes[1]&128 != 0))) + const ( + signBit = 0x80 + negativePrefix = 0xff + ) + + return !(v.IsCompound || len(v.Bytes) == 0 || len(v.Bytes) > 1 && ((v.Bytes[0] == 0 && v.Bytes[1]&signBit == 0) || (v.Bytes[0] == negativePrefix && v.Bytes[1]&signBit != 0))) } func validDERBitString(v asn1.RawValue) bool { - return !(v.IsCompound || len(v.Bytes) == 0 || v.Bytes[0] > 7 || len(v.Bytes) == 1 && v.Bytes[0] != 0 || len(v.Bytes) > 1 && v.Bytes[len(v.Bytes)-1]&byte((1< maximumUnusedBits || len(v.Bytes) == 1 && v.Bytes[0] != 0 || len(v.Bytes) > 1 && v.Bytes[len(v.Bytes)-1]&byte((1< int(^uint(0)>>1)-size { + if len(part) > math.MaxInt-size { panic("der: encoded value too large") } @@ -564,7 +619,7 @@ func wrapParts(tag byte, parts ...[]byte) []byte { } header := Header(tag, size) - if size > int(^uint(0)>>1)-len(header) { + if size > math.MaxInt-len(header) { panic("der: encoded value too large") } diff --git a/internal/der/validated.go b/internal/der/validated.go index 2ab95c6..fba038c 100644 --- a/internal/der/validated.go +++ b/internal/der/validated.go @@ -12,18 +12,18 @@ import ( // Keeping this separate from Read avoids reflective decoding during schema // checks without bypassing the complete initial validation. func ValidatedValue(input []byte) (asn1.RawValue, []byte) { - value := asn1.RawValue{Class: int(input[0] >> 6), Tag: int(input[0] & 31), IsCompound: input[0]&32 != 0} + value := asn1.RawValue{Class: int(input[0] >> identifierClassShift), Tag: int(input[0] & highTagNumber), IsCompound: input[0]&constructedBit != 0} at := 1 - if value.Tag == 31 { + if value.Tag == highTagNumber { value.Tag = 0 for { octet := input[at] at++ - value.Tag = value.Tag<<7 | int(octet&127) + value.Tag = value.Tag<= 128 { - count := length & 127 + if length >= longLengthFlag { + count := length & longLengthCountMask length = 0 for range count { - length = length<<8 | int(input[at]) + length = length< 64 { + var sources map[[sha256.Size]byte]proofOrigin + if len(seen) > proofOriginsIndexThreshold { sources = g.proofOrigins() } @@ -226,7 +235,7 @@ func (g *graph) extractReferences(ctx context.Context, seen map[[32]byte]struct{ continue } - var proofSource [32]byte + var proofSource [sha256.Size]byte if origin, ok := sources[key]; ok { proofSource = origin.source } else { @@ -250,8 +259,8 @@ func (g *graph) extractReferences(ctx context.Context, seen map[[32]byte]struct{ // proofOrigins indexes the first source of each hash-reference container's // earliest proof. Later appends cannot precede an existing first matching fact. // Histories without hash references do not need an index allocation. -func (g *graph) proofOrigins() map[[32]byte]proofOrigin { - var origins map[[32]byte]proofOrigin +func (g *graph) proofOrigins() map[[sha256.Size]byte]proofOrigin { + var origins map[[sha256.Size]byte]proofOrigin for _, fact := range g.facts { if len(g.objects[fact.Digest].Refs) == 0 { @@ -259,7 +268,7 @@ func (g *graph) proofOrigins() map[[32]byte]proofOrigin { } if origins == nil { - origins = make(map[[32]byte]proofOrigin, min(len(g.facts), len(g.objects))) + origins = make(map[[sha256.Size]byte]proofOrigin, min(len(g.facts), len(g.objects))) } previous, exists := origins[fact.Digest] @@ -271,7 +280,7 @@ func (g *graph) proofOrigins() map[[32]byte]proofOrigin { return origins } -func (g *graph) containerSource(key [32]byte, at time.Time, fallback [32]byte) [32]byte { +func (g *graph) containerSource(key [sha256.Size]byte, at time.Time, fallback [sha256.Size]byte) [sha256.Size]byte { for _, fact := range g.facts { if fact.Digest == key && fact.At.Equal(at) { return fact.Source @@ -281,7 +290,7 @@ func (g *graph) containerSource(key [32]byte, at time.Time, fallback [32]byte) [ return fallback } -func (g *graph) extractReference(ctx context.Context, set *historicalproof.Set, ref HashReference, source [32]byte, at time.Time) (bool, error) { +func (g *graph) extractReference(ctx context.Context, set *historicalproof.Set, ref HashReference, source [sha256.Size]byte, at time.Time) (bool, error) { if err := ctx.Err(); err != nil { return false, err } @@ -314,7 +323,7 @@ func (g *graph) extractReference(ctx context.Context, set *historicalproof.Set, func (g *graph) add(fact historicalproof.Fact) error { key := proofIdentity{fact.Digest, fact.Kind} - if g.earliest == nil && len(g.facts) > 64 { + if g.earliest == nil && len(g.facts) > earliestProofIndexThreshold { g.earliest = make(map[proofIdentity]time.Time, len(g.facts)) for _, existing := range g.facts { @@ -350,30 +359,35 @@ func (g *graph) add(fact historicalproof.Fact) error { return nil } -func (w *containmentWalk) visit(key [32]byte, depth int) (int, error) { +func (w *containmentWalk) visit(key [sha256.Size]byte, depth int) (int, error) { + const ( + activeState = 1 + completedStateOffset = 2 // Completed states encode subtree height above unseen/active. + ) + if err := w.ctx.Err(); err != nil { return 0, err } - if depth > 64 || depth > w.maximum { + if depth > maximumContainmentDepth || depth > w.maximum { return 0, ErrLimit } state := w.states[key] - if state == 1 { + if state == activeState { return 0, fmt.Errorf("%w: containment cycle", ErrInput) } - if state >= 2 { - height := int(state) - 2 - if height > 64-depth || height > w.maximum-depth { + if state >= completedStateOffset { + height := int(state) - completedStateOffset + if height > maximumContainmentDepth-depth || height > w.maximum-depth { return 0, ErrLimit } return height, nil } - w.states[key] = 1 + w.states[key] = activeState height := 0 for _, child := range w.objects[key].Contains { @@ -385,7 +399,7 @@ func (w *containmentWalk) visit(key [32]byte, depth int) (int, error) { height = max(height, childHeight+1) } // #nosec G115 -- Each child passed the depth/height checks above; height is 0..64. - w.states[key] = uint8(height) + 2 + w.states[key] = uint8(height) + completedStateOffset return height, nil } diff --git a/internal/historical/model.go b/internal/historical/model.go index ea77b7a..b86848e 100644 --- a/internal/historical/model.go +++ b/internal/historical/model.go @@ -5,6 +5,7 @@ package historical import ( "context" "crypto" + "crypto/sha256" "crypto/x509" "errors" "time" @@ -35,112 +36,181 @@ const ( // Object is one exact original encoding identified independently of its role. // Contains is populated only by a format parser, not by application assertions. type Object struct { - Bytes []byte - Kind historicalproof.Kind - Contains [][32]byte - Refs []HashReference + // Bytes contains the exact original encoding established by the format adapter. + Bytes []byte + // Kind identifies the object's role in historical validation. + Kind historicalproof.Kind + // Contains lists digests of objects physically contained in Bytes. + Contains [][sha256.Size]byte + // Refs lists protected digest references encoded by this object. + Refs []HashReference } // HashReference binds an encoded, protected digest to actual object bytes. type HashReference struct { - Hash crypto.Hash + // Hash identifies the protected digest algorithm. + Hash crypto.Hash + // Digest is the protected digest value carried by the containing object. Digest []byte - Target [32]byte + // Target identifies the referenced object's exact encoding by SHA-256. + Target [sha256.Size]byte } // MaterialUse binds an algorithm to the exact material it protects. Recovery // cannot borrow a SignatureValue proof for unrelated reference input bytes. type MaterialUse struct { - Object [32]byte - Hash crypto.Hash + // Object identifies the exact protected material by SHA-256. + Object [sha256.Size]byte + // Hash is the algorithm used to protect that material. + Hash crypto.Hash } // Candidate is a timestamp and the exact input established by its format adapter. // Covered names objects actually present in that input; a reference to an object // must instead be represented as HashReference on the containing object. type Candidate struct { - Token *timestamp.Token + // Token is the parsed timestamp awaiting independent authentication and trust + // checks. + Token *timestamp.Token + // Imprint is the expected digest of the input established by the format + // adapter. Imprint timestamp.Imprint - Covered [][32]byte + // Covered lists exact-byte object digests present in the timestamp input. + Covered [][sha256.Size]byte + // Archive identifies an archive timestamp candidate. Archive bool + // Content identifies a content timestamp candidate. Content bool } // Input has already passed signature, reference and format-binding checks. // The workflow independently checks all timestamp bindings and certificate paths. type Input struct { - Signature [32]byte - Certificate []byte + // Signature identifies the exact signature object by SHA-256. + Signature [sha256.Size]byte + // Certificate contains the signer certificate's exact DER encoding. + Certificate []byte + // SignatureMethod is the verified signature algorithm. SignatureMethod x509.SignatureAlgorithm - Hashes []crypto.Hash - Uses []MaterialUse - ClaimedTime time.Time + // Hashes lists hash algorithms whose historical acceptability must be checked. + Hashes []crypto.Hash + // Uses binds each protected object to the hash algorithm applied to it. + Uses []MaterialUse + // ClaimedTime is a signed time claim and supplies no independent proof of + // existence. + ClaimedTime time.Time // ClaimedTimeAfter compares the exact signed UTC dateTime to a finite // threshold without truncating arbitrary year/fraction precision. // Only an authenticated format adapter supplies this pure comparison. ClaimedTimeAfter func(time.Time) (bool, error) - Objects []Object - Timestamps []Candidate + // Objects contains the format adapter's exact encodings and + // containment/reference bindings. + Objects []Object + // Timestamps contains timestamp candidates whose bindings and trust must still + // be checked. + Timestamps []Candidate } // Config is immutable after construction of a public format validator. type Config struct { - Signer *pki.Verifier + // Signer supplies the signer's current certificate and revocation policy. + Signer *pki.Verifier + // SignerHistory supplies the signer's historical certificate policy. SignerHistory pki.HistoricalOptions - TSA *timestamp.Verifier - TSAHistory pki.HistoricalOptions + // TSA supplies timestamp authentication and trust policy. + TSA *timestamp.Verifier + // TSAHistory supplies historical certificate policy for timestamp authorities. + TSAHistory pki.HistoricalOptions // MissingAccuracy is an explicit TSA policy assumption when RFC3161 accuracy // is absent. Nil refuses to invent a precision for historical proof extraction. - MissingAccuracy *time.Duration - TimestampDelay *time.Duration + MissingAccuracy *time.Duration + // TimestampDelay optionally bounds the delay between claimed signing time and + // timestamp evidence. + TimestampDelay *time.Duration + // RequireAllTimestamps requires every supplied timestamp candidate to pass. RequireAllTimestamps bool - MaxObjects int - MaxEdges int - MaxBytes int - MaxTimestamps int + // MaxObjects bounds distinct objects in the proof graph. + MaxObjects int + // MaxEdges bounds containment and digest-reference edges. + MaxEdges int + // MaxBytes bounds aggregate object encodings in bytes. + MaxBytes int + // MaxTimestamps bounds supplied timestamp candidates. + MaxTimestamps int } // Step records a normative decision without exposing mutable internal state. type Step struct { - Clause string - Action string + // Clause identifies the standard clause governing the decision. + Clause string + // Action identifies the validation operation recorded by this step. + Action string + // SubIndication records the detailed validation outcome. SubIndication string - Object [32]byte - Source [32]byte - Before time.Time - After time.Time + // Object identifies the affected object by its exact-byte SHA-256 digest. + Object [sha256.Size]byte + // Source identifies the evidence object supporting the decision. + Source [sha256.Size]byte + // Before records the time bound before this decision. + Before time.Time + // After records the time bound after this decision. + After time.Time } // Stamp records a timestamp's outcome and its exact token/binding identity. type Stamp struct { - Generation time.Time - Upper time.Time - Best time.Time - Validation time.Time - Path *pki.HistoricalPath + // Generation is the authenticated timestamp generation time. + Generation time.Time + // Upper is the generation-time upper bound including the accepted accuracy. + Upper time.Time + // Best is the best signature time established for this timestamp. + Best time.Time + // Validation is the time selected for historical certificate validation. + Validation time.Time + // Path is the selected historical TSA certificate path. + Path *pki.HistoricalPath + // SubIndication records the detailed timestamp outcome. SubIndication string - Token [32]byte - Certificate [32]byte - Indication Indication - Archive bool - Content bool - Ordering bool + // Token identifies the exact timestamp encoding by SHA-256. + Token [sha256.Size]byte + // Certificate identifies the TSA certificate encoding by SHA-256. + Certificate [sha256.Size]byte + // Indication records the timestamp's three-state validation outcome. + Indication Indication + // Archive identifies an archive timestamp. + Archive bool + // Content identifies a content timestamp. + Content bool + // Ordering records the TSA's authenticated ordering assertion. + Ordering bool } // Report is an operation-local immutable result after Validate returns. type Report struct { - Indication Indication + // Indication is the overall three-state historical validation outcome. + Indication Indication + // SubIndication is the detailed reason for the outcome. SubIndication string - At time.Time - Best time.Time - Validation time.Time - Path *pki.HistoricalPath - Current *pki.Result - Past *pki.PastCertificateResult - Proofs *historicalproof.Set - Steps []Step - Stamps []Stamp - Cause error + // At is the explicit current validation time. + At time.Time + // Best is the best signature time established by accepted evidence. + Best time.Time + // Validation is the time selected for historical certificate validation. + Validation time.Time + // Path is the selected historical signer certificate path. + Path *pki.HistoricalPath + // Current retains acceptance under the current signer policy, when established. + Current *pki.Result + // Past retains successful past certificate validation, when established. + Past *pki.PastCertificateResult + // Proofs is the immutable exact-byte proof-of-existence snapshot. + Proofs *historicalproof.Set + // Steps records validation decisions in execution order. + Steps []Step + // Stamps records the evaluated timestamp candidates and their outcomes. + Stamps []Stamp + // Cause retains the error responsible for a non-passing outcome. + Cause error } // Validate performs the separate historical workflow. A successful report is diff --git a/internal/historical/order.go b/internal/historical/order.go index 8d23a53..5931070 100644 --- a/internal/historical/order.go +++ b/internal/historical/order.go @@ -3,12 +3,13 @@ package historical import ( "bytes" "context" + "crypto/sha256" "fmt" "slices" ) -func (g *graph) covered(ctx context.Context, candidate Candidate) (map[[32]byte]struct{}, error) { - seen := make(map[[32]byte]struct{}) +func (g *graph) covered(ctx context.Context, candidate Candidate) (map[[sha256.Size]byte]struct{}, error) { + seen := make(map[[sha256.Size]byte]struct{}) stack := slices.Clone(candidate.Covered) for len(stack) > 0 { diff --git a/internal/historical/past.go b/internal/historical/past.go index 394496f..8c8af07 100644 --- a/internal/historical/past.go +++ b/internal/historical/past.go @@ -10,10 +10,20 @@ import ( "time" "github.com/skarm/xmlsec/internal/historicalproof" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/x509cert" "github.com/skarm/xmlsec/pki" ) +// Report decisions retain the normative clause and action vocabulary. +const ( + clause56247 = "5.6.2.4(7)" + stagePastSignatureValidation = "past-signature-validation" + // Sub-indication used when certificate validity has not begun at the selected validation time. + codeNotYetValid = "NOT_YET_VALID" +) + +// Recovery branches use these EN 319 102-1 sub-indications, not arbitrary errors. const ( noChain = "NO_CERTIFICATE_CHAIN_FOUND_NO_POE" outOfBounds = "OUT_OF_BOUNDS_NO_POE" @@ -28,7 +38,7 @@ const ( ) type signedMaterial struct { - digest [32]byte + digest [sha256.Size]byte certificate []byte algorithm x509.SignatureAlgorithm hashes []crypto.Hash @@ -84,7 +94,7 @@ func (e *pastSignatureEvaluation) evaluatePath(ctx context.Context, session *pki } out.indication, out.sub, out.result, out.err = Passed, "", past, nil - out.steps = append(out.steps, Step{Clause: "5.6.2.4(7)", Action: "past-signature-validation", Object: e.material.digest, Before: e.proofs.Now(), After: past.ValidationTime()}) + out.steps = append(out.steps, Step{Clause: clause56247, Action: stagePastSignatureValidation, Object: e.material.digest, Before: e.proofs.Now(), After: past.ValidationTime()}) return true } @@ -212,7 +222,7 @@ func rejectBeforeIssuance(out *pastOutcome, certificate *x509.Certificate) bool return false } - out.indication, out.sub = Failed, "NOT_YET_VALID" + out.indication, out.sub = Failed, codeNotYetValid return true } @@ -384,7 +394,7 @@ func suspended(target []byte, claims []*pki.VerifiedEvidence) bool { for _, claim := range claims { reason, ok := claim.RevocationReason() - if ok && reason == 6 && claim.AssertedStatus() == assertedRevoked && decodeDigest(claim.TargetCertificateSHA256()) == targetHash { + if ok && reason == lexicon.RevocationReasonCertificateHold && claim.AssertedStatus() == assertedRevoked && decodeDigest(claim.TargetCertificateSHA256()) == targetHash { return true } } @@ -392,11 +402,11 @@ func suspended(target []byte, claims []*pki.VerifiedEvidence) bool { return false } -func decodeDigest(value string) [32]byte { - var digest [32]byte - if len(value) == 64 { +func decodeDigest(value string) [sha256.Size]byte { + var digest [sha256.Size]byte + if len(value) == hex.EncodedLen(sha256.Size) { if _, err := hex.Decode(digest[:], []byte(value)); err != nil { - return [32]byte{} + return [sha256.Size]byte{} } } diff --git a/internal/historical/workflow.go b/internal/historical/workflow.go index f88234e..bd27d26 100644 --- a/internal/historical/workflow.go +++ b/internal/historical/workflow.go @@ -14,11 +14,45 @@ import ( "github.com/skarm/xmlsec/cms" "github.com/skarm/xmlsec/evidence" "github.com/skarm/xmlsec/internal/historicalproof" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/x509cert" "github.com/skarm/xmlsec/pki" "github.com/skarm/xmlsec/timestamp" ) +const ( + // Zero-valued workflow limits select these defaults. Byte counts cover exact + // object encodings; edges count containment and protected hash references. + defaultMaxBytes = 16 << 20 + defaultMaxEdges = 4096 + defaultMaxObjects = 512 + defaultMaxTimestamps = 16 + // Non-default limits cannot exceed these hard caps. Durations separately bound + // assumed missing TSA accuracy and the allowed recovery/delay interval. + maximumBytes = 64 << 20 + maximumCryptoConstraints = 128 + maximumEdges = 8192 + maximumInputHashes = 128 + maximumMissingAccuracy = 24 * time.Hour + maximumObjects = 2048 + maximumRecoveryInterval = 365 * 24 * time.Hour + maximumSourceObservations = 1024 + maximumTimestamps = 64 + maximumTrustAnchorSunsets = 64 + // Clause/action pairs identify decisions in the immutable workflow report. + clause5623 = "5.6.2.3" + clause563410 = "5.6.3.4(10)" + stageExtractExactObjectPoe = "extract-exact-object-poe" + stageReturnPassed = "return-passed" +) + +// Timestamp and signature rejection classes are report vocabulary shared by +// preparation, policy enforcement and the returned per-stamp diagnostics. +const ( + codeHashFailure = "HASH_FAILURE" + codeTimestampConstraintsFailure = "TIMESTAMP_CONSTRAINTS_FAILURE" +) + const ( objectData = historicalproof.Data objectOCSP = historicalproof.OCSP @@ -34,22 +68,22 @@ func Normalize(c Config) (Config, error) { } if c.MaxObjects == 0 { - c.MaxObjects = 512 + c.MaxObjects = defaultMaxObjects } if c.MaxEdges == 0 { - c.MaxEdges = 4096 + c.MaxEdges = defaultMaxEdges } if c.MaxBytes == 0 { - c.MaxBytes = 16 << 20 + c.MaxBytes = defaultMaxBytes } if c.MaxTimestamps == 0 { - c.MaxTimestamps = 16 + c.MaxTimestamps = defaultMaxTimestamps } - if c.MaxObjects < 1 || c.MaxObjects > 2048 || c.MaxEdges < c.MaxObjects || c.MaxEdges > 8192 || c.MaxBytes < 1 || c.MaxBytes > 64<<20 || c.MaxTimestamps < 1 || c.MaxTimestamps > 64 { + if c.MaxObjects < 1 || c.MaxObjects > maximumObjects || c.MaxEdges < c.MaxObjects || c.MaxEdges > maximumEdges || c.MaxBytes < 1 || c.MaxBytes > maximumBytes || c.MaxTimestamps < 1 || c.MaxTimestamps > maximumTimestamps { return c, ErrLimit } @@ -59,19 +93,19 @@ func Normalize(c Config) (Config, error) { } value := **pointer - if value < 0 || value > 365*24*time.Hour { + if value < 0 || value > maximumRecoveryInterval { return c, ErrInput } *pointer = &value } - if c.MissingAccuracy != nil && *c.MissingAccuracy > 24*time.Hour { + if c.MissingAccuracy != nil && *c.MissingAccuracy > maximumMissingAccuracy { return c, ErrInput } for _, options := range []pki.HistoricalOptions{c.SignerHistory, c.TSAHistory} { - if len(options.CryptoConstraints) > 128 || len(options.TrustAnchorSunsets) > 64 { + if len(options.CryptoConstraints) > maximumCryptoConstraints || len(options.TrustAnchorSunsets) > maximumTrustAnchorSunsets { return c, ErrLimit } } @@ -115,6 +149,10 @@ type validationWorkflow struct { tsaSession *pki.HistoricalSession graph *graph material signedMaterial + // Object positions are append-only; Bytes stay unchanged during the workflow. + // The memo holds FIRST positions within the scanned admittedPrefix objects. + admitted map[[sha256.Size]byte]int + admittedPrefix int } func (w *validationWorkflow) initialize() error { @@ -139,7 +177,7 @@ func (w *validationWorkflow) initialize() error { } w.certificate = certificate - if len(w.input.Hashes) > 128 { + if len(w.input.Hashes) > maximumInputHashes { return ErrLimit } @@ -153,27 +191,52 @@ func (w *validationWorkflow) initialize() error { return ErrLimit } - return nil + if err := w.config.Signer.CheckEvidenceLimits(w.ctx, w.signerEvidence); err != nil { + return err + } + + return w.config.TSA.CheckEvidenceLimits(w.ctx, w.tsaEvidence) } // admit records an exact object at the explicit present time. Neither source // provenance times nor SigningTime establish its earlier existence. func (w *validationWorkflow) admit(raw []byte, kind historicalproof.Kind) { digest := sha256.Sum256(raw) - for index, object := range w.input.Objects { - if sha256.Sum256(object.Bytes) == digest { - if object.Kind == objectData { - w.input.Objects[index].Kind = kind - } - return + if w.admitted == nil { + w.admitted = make(map[[sha256.Size]byte]int) + } + + index, found := w.admitted[digest] + // An early hit must not hash the unvisited tail of the initial objects. + for !found && w.admittedPrefix < len(w.input.Objects) { + position := w.admittedPrefix + key := sha256.Sum256(w.input.Objects[position].Bytes) + w.admittedPrefix++ + + if _, exists := w.admitted[key]; !exists { + w.admitted[key] = position + } + + if key == digest { + index, found = position, true + } + } + + if found { + if w.input.Objects[index].Kind == objectData { + w.input.Objects[index].Kind = kind } + + return } + w.admitted[digest] = len(w.input.Objects) w.input.Objects = append(w.input.Objects, Object{Bytes: raw, Kind: kind}) + w.admittedPrefix++ } -func (w *validationWorkflow) containCertificates(parent [32]byte, certificates [][]byte) { +func (w *validationWorkflow) containCertificates(parent [sha256.Size]byte, certificates [][]byte) { for _, raw := range certificates { w.admit(raw, historicalproof.Certificate) } @@ -277,7 +340,7 @@ func (w *validationWorkflow) prepareTimestamp(candidate Candidate) (*preparedSta accuracy, exists := assertion.Info().Accuracy() if !exists { if w.config.MissingAccuracy == nil { - outcome := Stamp{Token: assertion.TokenSHA256(), Generation: assertion.Info().GenTime(), Archive: candidate.Archive, SubIndication: "TIMESTAMP_CONSTRAINTS_FAILURE"} + outcome := Stamp{Token: assertion.TokenSHA256(), Generation: assertion.Info().GenTime(), Archive: candidate.Archive, SubIndication: codeTimestampConstraintsFailure} return nil, w.rejectTimestampConstraint(outcome) } @@ -287,7 +350,7 @@ func (w *validationWorkflow) prepareTimestamp(candidate Candidate) (*preparedSta upper := assertion.Info().GenTime().Add(accuracy) if upper.After(w.now) { - outcome := Stamp{Token: assertion.TokenSHA256(), Generation: assertion.Info().GenTime(), Upper: upper, Archive: candidate.Archive, SubIndication: "TIMESTAMP_CONSTRAINTS_FAILURE"} + outcome := Stamp{Token: assertion.TokenSHA256(), Generation: assertion.Info().GenTime(), Upper: upper, Archive: candidate.Archive, SubIndication: codeTimestampConstraintsFailure} return nil, w.rejectTimestampConstraint(outcome) } @@ -298,11 +361,11 @@ func (w *validationWorkflow) prepareTimestamp(candidate Candidate) (*preparedSta func (w *validationWorkflow) rejectTimestampAuthentication(candidate Candidate, err error) error { indication, sub := Indeterminate, signatureConstraintsFailure if errors.Is(err, cms.ErrSignature) { - indication, sub = Failed, "SIG_CRYPTO_FAILURE" + indication, sub = Failed, lexicon.HistoricalCodeSigCryptoFailure } if errors.Is(err, timestamp.ErrBinding) { - indication, sub = Failed, "HASH_FAILURE" + indication, sub = Failed, codeHashFailure } w.report.Stamps = append(w.report.Stamps, Stamp{Token: sha256.Sum256(candidate.Token.DER()), Archive: candidate.Archive, Indication: indication, SubIndication: sub}) @@ -469,7 +532,7 @@ func (w *validationWorkflow) processTimestamp(stamp preparedStamp, accepted []pr return false, err } - w.report.Steps = append(w.report.Steps, Step{Clause: "5.6.2.3", Action: "extract-exact-object-poe", Source: stamp.assertion.TokenSHA256(), Before: w.now, After: stamp.upper}) + w.report.Steps = append(w.report.Steps, Step{Clause: clause5623, Action: stageExtractExactObjectPoe, Source: stamp.assertion.TokenSHA256(), Before: w.now, After: stamp.upper}) return true, nil } @@ -481,7 +544,7 @@ func (w *validationWorkflow) rejectTimestampChronology() { func (w *validationWorkflow) verifySigner() error { if w.report.Best.Before(w.certificate.NotBefore) { - w.report.Indication, w.report.SubIndication = Failed, "NOT_YET_VALID" + w.report.Indication, w.report.SubIndication = Failed, codeNotYetValid return ErrValidation } @@ -578,7 +641,7 @@ func (w *validationWorkflow) finish() error { } w.report.Indication = Passed - w.report.Steps = append(w.report.Steps, Step{Clause: "5.6.3.4(10)", Action: "return-passed", Object: w.input.Signature, Before: w.now, After: w.report.Validation}) + w.report.Steps = append(w.report.Steps, Step{Clause: clause563410, Action: stageReturnPassed, Object: w.input.Signature, Before: w.now, After: w.report.Validation}) return nil } @@ -643,7 +706,7 @@ func validateStamp(ctx context.Context, c Config, stamp preparedStamp, now time. material := signedMaterial{digest: out.Token, certificate: assertion.CertificateDER(), algorithm: cmsAlgorithm(assertion.SignatureAlgorithmOID(), assertion.SignatureHash()), hashes: []crypto.Hash{assertion.SignatureHash(), info.Imprint().Hash}} if material.algorithm == x509.UnknownSignatureAlgorithm { - out.SubIndication = "SIG_CRYPTO_FAILURE" + out.SubIndication = lexicon.HistoricalCodeSigCryptoFailure return out, ErrValidation } @@ -704,10 +767,12 @@ func currentSubIndication(err error, report pki.Report, certificate *x509.Certif return tryLater } - target := sha256.Sum256(certificate.Raw) - for _, assertion := range report.Evidence() { - if assertion.AssertedStatus() == "revoked" && decodeDigest(assertion.TargetCertificateSHA256()) != target { - return revokedCA + if certificate != nil { + target := sha256.Sum256(certificate.Raw) + for _, assertion := range report.Evidence() { + if assertion.AssertedStatus() == assertedRevoked && decodeDigest(assertion.TargetCertificateSHA256()) != target { + return revokedCA + } } } @@ -722,11 +787,17 @@ func currentSubIndication(err error, report pki.Report, certificate *x509.Certif return noChain } - return "CHAIN_CONSTRAINTS_FAILURE" + return lexicon.HistoricalCodeChainConstraintsFailure } func removeTimestampSources(input Input, groups ...pki.Evidence) (pki.Evidence, pki.Evidence, error) { - allowed := make(map[[32]byte]struct{}, len(input.Timestamps)) + for _, group := range groups { + if len(group.Sources) > maximumSourceObservations { + return pki.Evidence{}, pki.Evidence{}, ErrLimit + } + } + + allowed := make(map[[sha256.Size]byte]struct{}, len(input.Timestamps)) for _, candidate := range input.Timestamps { if candidate.Token != nil { allowed[sha256.Sum256(candidate.Token.DER())] = struct{}{} diff --git a/internal/historical/workflow_internal_test.go b/internal/historical/workflow_internal_test.go index 4495a47..000a5f1 100644 --- a/internal/historical/workflow_internal_test.go +++ b/internal/historical/workflow_internal_test.go @@ -5,14 +5,21 @@ import ( "context" "crypto" "crypto/sha256" + "crypto/x509" + "crypto/x509/pkix" "encoding/asn1" + "encoding/base64" "errors" + "fmt" + "math/big" "math/rand/v2" + "reflect" "slices" "strconv" "testing" "time" + "github.com/skarm/xmlsec/evidence" "github.com/skarm/xmlsec/internal/historicalproof" "github.com/skarm/xmlsec/internal/testutil/testcert" "github.com/skarm/xmlsec/pki" @@ -83,6 +90,8 @@ func TestWorkflowCancellationNeverProducesPassingReport(t *testing.T) { cancel() now := testcert.Now() report, err := Validate(ctx, Config{}, Input{}, now, pki.Evidence{}, pki.Evidence{}) + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if !errors.Is(err, context.Canceled) || report == nil || report.Indication != Indeterminate || !errors.Is(report.Cause, context.Canceled) || !report.At.Equal(now) || report.Proofs != nil || report.Current != nil || report.Past != nil { t.Fatal("failed workflow granted state", report, err) } @@ -435,3 +444,377 @@ func containmentChain(depth int) (map[[32]byte]Object, [][32]byte) { } return objects, keys } + +// Independent linear oracle retained from the pre-memo helper. These tests check +// admission state, not acceptance/authentication of an entire historical Input. +func linearAdmissionOracle(objects []Object, raw []byte, kind historicalproof.Kind) []Object { + digest := sha256.Sum256(raw) + for index, object := range objects { + if sha256.Sum256(object.Bytes) == digest { + if object.Kind == objectData { + objects[index].Kind = kind + } + return objects + } + } + return append(objects, Object{Bytes: raw, Kind: kind}) +} + +func checkAdmissionObjects(t *testing.T, got, want []Object) { + t.Helper() + if !reflect.DeepEqual(got, want) { + t.Fatalf("admission changed order/bytes/kind/metadata: got%+v want%+v", got, want) + } + for i := range got { + if len(got[i].Bytes) > 0 && &got[i].Bytes[0] != &want[i].Bytes[0] { + t.Fatalf("object%d copied or replaced first bytes", i) + } + if len(got[i].Contains) > 0 && &got[i].Contains[0] != &want[i].Contains[0] { + t.Fatalf("object%d copied containment", i) + } + if len(got[i].Refs) > 0 && &got[i].Refs[0] != &want[i].Refs[0] { + t.Fatalf("object%d copied references", i) + } + } +} + +func TestAdmissionPrefixMatchesLinearFirstOccurrence(t *testing.T) { + chain := testcert.New(t) + certificate := chain.Leaf.Raw + child := sha256.Sum256(certificate) + parent := []byte(`` + base64.StdEncoding.EncodeToString(certificate) + `` + base64.StdEncoding.EncodeToString(child[:]) + ``) + metadata := Object{Bytes: parent, Kind: objectData, Contains: [][32]byte{child}, Refs: []HashReference{{Hash: crypto.SHA256, Digest: child[:], Target: child}}} + initial := []Object{{Bytes: certificate, Kind: objectData}, {Bytes: bytes.Clone(certificate), Kind: historicalproof.Certificate}, metadata} + want := slices.Clone(initial) + w := validationWorkflow{input: Input{Objects: slices.Clone(initial)}} + for _, step := range []struct { + raw []byte + kind historicalproof.Kind + }{ + {bytes.Clone(certificate), historicalproof.Certificate}, + {parent, objectData}, + {certificate, objectData}, + {[]byte("new exact data object"), objectData}, + {parent, objectData}, + } { + want = linearAdmissionOracle(want, step.raw, step.kind) + w.admit(step.raw, step.kind) + checkAdmissionObjects(t, w.input.Objects, want) + } + if w.admitted[child] != 0 || w.input.Objects[0].Kind != historicalproof.Certificate || w.input.Objects[1].Kind != historicalproof.Certificate { + t.Fatal("first duplicate promotion or retained non-Data kind changed") + } +} + +func TestAdmissionPrefixDoesNotSeedEarlyHitTail(t *testing.T) { + raw := [][]byte{[]byte("first"), []byte("middle"), []byte("last"), []byte("miss")} + initial := []Object{{Bytes: raw[0], Kind: objectData}, {Bytes: raw[1], Kind: objectData}, {Bytes: raw[2], Kind: objectData}} + w := validationWorkflow{input: Input{Objects: slices.Clone(initial)}} + want := slices.Clone(initial) + for i, position := range []int{0, 0, 2, 3, 0, 1} { + w.admit(raw[position], objectData) + want = linearAdmissionOracle(want, raw[position], objectData) + checkAdmissionObjects(t, w.input.Objects, want) + if i < 2 && (w.admittedPrefix != 1 || len(w.admitted) != 1) { + t.Fatal("first hit scanned or memoized the tail", w.admittedPrefix, len(w.admitted)) + } + } + if w.admittedPrefix != len(w.input.Objects) || len(w.admitted) != 4 { + t.Fatal("late hit/miss did not complete the prefix") + } +} + +func TestAdmissionPrefixKeepsContainmentFirstParent(t *testing.T) { + certificate := testcert.New(t).Leaf.Raw + child := sha256.Sum256(certificate) + parent := []byte(`` + base64.StdEncoding.EncodeToString(certificate) + ``) + parentKey := sha256.Sum256(parent) + initial := []Object{{Bytes: parent, Kind: objectData}, {Bytes: bytes.Clone(parent), Kind: objectData}} + w := validationWorkflow{input: Input{Objects: slices.Clone(initial)}} + w.containCertificates(parentKey, [][]byte{certificate}) + if len(w.input.Objects) != 3 || !slices.Equal(w.input.Objects[0].Contains, [][32]byte{child}) || len(w.input.Objects[1].Contains) != 0 { + t.Fatal("legacy first-parent containment changed", w.input.Objects) + } + if &w.input.Objects[0].Bytes[0] != &initial[0].Bytes[0] || &w.input.Objects[1].Bytes[0] != &initial[1].Bytes[0] || &w.input.Objects[2].Bytes[0] != &certificate[0] { + t.Fatal("containment changed byte ownership") + } + if w.admitted[parentKey] != 0 { + t.Fatal("duplicate parent replaced the first memo position") + } +} + +func TestAdmissionPrefixIsLocalToWorkflow(t *testing.T) { + a, b := []byte("a"), []byte("b") + first := validationWorkflow{input: Input{Objects: []Object{{Bytes: a, Kind: objectData}}}} + second := validationWorkflow{input: Input{Objects: []Object{{Bytes: b, Kind: objectData}}}} + first.admit(a, objectData) + first.admit(b, objectData) + second.admit(b, objectData) + second.admit(a, objectData) + checkAdmissionObjects(t, first.input.Objects, []Object{{Bytes: a, Kind: objectData}, {Bytes: b, Kind: objectData}}) + checkAdmissionObjects(t, second.input.Objects, []Object{{Bytes: b, Kind: objectData}, {Bytes: a, Kind: objectData}}) + if first.admitted[sha256.Sum256(a)] != 0 || second.admitted[sha256.Sum256(a)] != 1 { + t.Fatal("workflow indexes leaked positions") + } +} + +func historicalLimitCertificates(t testing.TB, chain testcert.Chain, count, padding int) [][]byte { + t.Helper() + var extra []pkix.Extension + if padding > 0 { + value, err := asn1.Marshal(bytes.Repeat([]byte{7}, padding)) + if err != nil { + t.Fatal(err) + } + extra = []pkix.Extension{{Id: asn1.ObjectIdentifier{1, 2, 3, 4, 5, 6, 7}, Value: value}} + } + raw := make([][]byte, count) + seen := map[[32]byte]bool{sha256.Sum256(chain.Leaf.Raw): true} + total := 0 + for i := range raw { + template := &x509.Certificate{SerialNumber: big.NewInt(int64(10000 + i)), Subject: pkix.Name{CommonName: fmt.Sprintf("bounded preflight certificate %d", i)}, NotBefore: chain.Leaf.NotBefore, NotAfter: chain.Leaf.NotAfter, KeyUsage: x509.KeyUsageDigitalSignature, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageCodeSigning}, ExtraExtensions: extra} + cert := testcert.Issue(t, template, chain.Root, chain.LeafKey.Public(), chain.RootKey) + if err := cert.CheckSignatureFrom(chain.Root); err != nil { + t.Fatal(err) + } + raw[i] = cert.Raw + key := sha256.Sum256(raw[i]) + if seen[key] { + t.Fatal("fixture DER identities must be distinct") + } + seen[key] = true + total += len(raw[i]) + } + if total > 128<<10 { + t.Fatal("fixture must remain below128KiB") + } + return raw +} + +func historicalLimitWorkflow(t *testing.T, chain testcert.Chain, group string, raw [][]byte, maximum int) validationWorkflow { + t.Helper() + policy := pki.Config{Roots: [][]byte{chain.Root.Raw}, Revocation: pki.RequireCRL, MaxEvidenceBytes: maximum, MaxCRLs: 1, MaxOCSPResponses: 1} + signer, err := pki.NewVerifier(policy) + if err != nil { + t.Fatal(err) + } + tsa, err := timestamp.NewVerifier(timestamp.Config{PKI: policy, AllowedPolicies: []asn1.ObjectIdentifier{{1, 2, 3, 4}}}) + if err != nil { + t.Fatal(err) + } + signature := []byte("already authenticated format adapter signature") + w := validationWorkflow{ctx: t.Context(), config: Config{Signer: signer, TSA: tsa}, now: testcert.Now(), report: &Report{}, input: Input{Signature: sha256.Sum256(signature), Certificate: bytes.Clone(chain.Leaf.Raw), SignatureMethod: chain.Leaf.SignatureAlgorithm, Hashes: []crypto.Hash{crypto.SHA256}, Objects: []Object{{Bytes: signature, Kind: historicalproof.Signature}}}} + if group == "signer" { + w.signerEvidence.Intermediates = raw + } else { + w.tsaEvidence.Intermediates = raw + } + return w +} + +// Invalid raw Evidence must not populate the pregraph object set. +func TestEvidenceLimitsBeforeAdmission(t *testing.T) { + chain := testcert.New(t) + counted := historicalLimitCertificates(t, chain, 65, 0) + padded := historicalLimitCertificates(t, chain, 2, 2048) + sum := len(padded[0]) + len(padded[1]) + cases := []struct { + name string + raw [][]byte + maximum int + over bool + }{ + {"count64", counted[:64], 128 << 10, false}, + {"count65", counted, 128 << 10, true}, + {"bytes_exact", padded, sum, false}, + {"bytes_over_one", padded, sum - 1, true}, + } + for _, group := range []string{"signer", "TSA"} { + for _, tc := range cases { + t.Run(group+"/"+tc.name, func(t *testing.T) { + w := historicalLimitWorkflow(t, chain, group, tc.raw, tc.maximum) + snapshots := make([][]byte, len(tc.raw)) + for i, b := range tc.raw { + snapshots[i] = bytes.Clone(b) + } + defer func() { + for i, b := range tc.raw { + if !bytes.Equal(b, snapshots[i]) { + t.Errorf("caller DER%d changed", i) + } + } + }() + e := pki.Evidence{Intermediates: tc.raw} + var directErr error + if group == "signer" { + _, directErr = w.config.Signer.NewHistoricalSession(t.Context(), w.config.SignerHistory, w.now, e) + } else { + _, directErr = w.config.TSA.NewHistoricalSession(t.Context(), w.config.TSAHistory, w.now, e) + } + if tc.over { + if !errors.Is(directErr, pki.ErrLimit) { + t.Fatalf("direct session should reject configured bound: %v", directErr) + } + } else if directErr != nil { + t.Fatal("direct session positive control", directErr) + } + before := len(w.input.Objects) + if err := w.initialize(); err != nil { + if tc.over && errors.Is(err, pki.ErrLimit) && len(w.input.Objects) == before && w.report.Proofs == nil && w.report.Current == nil && w.report.Past == nil { + return + } + t.Fatal("initialize", err) + } + err := w.admitEvidence() + t.Logf("validDER=%d bytes=%d cap=%d beforeObjects=%d afterObjects=%d admission=%v directSession=%v", len(tc.raw), historicalLimitBytes(tc.raw), tc.maximum, before, len(w.input.Objects), err, directErr) + if tc.over { + if (!errors.Is(err, pki.ErrLimit) && !errors.Is(err, ErrLimit)) || len(w.input.Objects) != before { + t.Fatalf("over-limit DER reached admitted state before session guard: objects=%d->%d err=%v", before, len(w.input.Objects), err) + } + } else if err != nil || len(w.input.Objects) != before+1+len(tc.raw) { + t.Fatalf("positive admission: objects=%d err=%v", len(w.input.Objects), err) + } + }) + } + } +} + +func historicalLimitBytes(raw [][]byte) int { + total := 0 + for _, b := range raw { + total += len(b) + } + return total +} + +func TestEvidenceLimitsStillCheckTimestampAdditions(t *testing.T) { + issuer := newTimestampIssuer(t) + raw := make([][]byte, 64) + for i := range raw { + raw[i] = issuer.chain.Root.Raw + } + w := historicalLimitWorkflow(t, issuer.chain, "TSA", raw, 128<<10) + zero := time.Duration(0) + w.config.MissingAccuracy = &zero + stamp := issuer.issueStamp(t, 17, testcert.Now().Add(-time.Second), &zero, false, w.input.Objects[0].Bytes) + stamp.candidate.Covered = [][32]byte{w.input.Signature} + w.input.Timestamps = []Candidate{stamp.candidate} + if err := w.initialize(); err != nil { + t.Fatal("initial64 evidence", err) + } + if err := w.admitEvidence(); err != nil { + t.Fatal(err) + } + if err := w.prepareTimestamps(); err != nil { + t.Fatal("timestamp preparation", err) + } + if len(w.prepared) != 1 || len(w.tsaEvidence.Intermediates) <= 64 { + t.Fatal("fixture did not add an authenticated token certificate") + } + if err := w.preparePaths(); !errors.Is(err, pki.ErrLimit) { + t.Fatal("late session bound lost", err) + } + if w.graph != nil || w.report.Proofs != nil || w.report.Current != nil || w.report.Past != nil { + t.Fatal("late limit published acceptance") + } + if len(raw) != 64 { + t.Fatal("caller evidence slice changed") + } +} + +func TestEvidenceLimitsBeforeRevocationInspection(t *testing.T) { + chain := testcert.New(t) + crl := testcert.CRL(t, chain) + for _, group := range []string{"signer", "TSA"} { + for _, kind := range []string{"CRL", "OCSP"} { + t.Run(group+"/"+kind, func(t *testing.T) { + w := historicalLimitWorkflow(t, chain, group, nil, 128<<10) + target := &w.signerEvidence + if group == "TSA" { + target = &w.tsaEvidence + } + if kind == "CRL" { + target.CRLs = [][]byte{crl, crl} + } else { + target.OCSPResponses = [][]byte{{0x30, 0}, {0x30, 0}} + } + before := len(w.input.Objects) + if err := w.initialize(); !errors.Is(err, pki.ErrLimit) || len(w.input.Objects) != before { + t.Fatal("configured count checked after admission/inspection", kind, err, len(w.input.Objects)) + } + }) + } + t.Run(group+"/under-limit OCSP still inspected", func(t *testing.T) { + w := historicalLimitWorkflow(t, chain, group, nil, 128<<10) + if group == "TSA" { + w.tsaEvidence.OCSPResponses = [][]byte{{0x30, 0}} + } else { + w.signerEvidence.OCSPResponses = [][]byte{{0x30, 0}} + } + if err := w.initialize(); err != nil { + t.Fatal("under-limit metadata", err) + } + if err := w.admitEvidence(); !errors.Is(err, pki.ErrEvidence) || errors.Is(err, pki.ErrLimit) { + t.Fatal("under-limit material parser changed", err) + } + }) + } +} + +func TestWorkflowTimestampSourcesRetainCountLimit(t *testing.T) { + t.Parallel() + + issuer := newTimestampIssuer(t) + signer, err := pki.NewVerifier(pki.Config{Roots: [][]byte{issuer.chain.Root.Raw}, Revocation: pki.RequireCRL}) + if err != nil { + t.Fatal(err) + } + signatureBytes := []byte("authenticated format signature encoding") + signature := sha256.Sum256(signatureBytes) + stamp := issuer.issueStamp(t, 1, testcert.Now().Add(-30*time.Second), new(time.Duration(0)), false, signatureBytes) + stamp.candidate.Covered = [][32]byte{signature} + source, err := evidence.NewObservation(t.Context(), evidence.Timestamp, stamp.candidate.Token.DER(), evidence.Provenance{}) + if err != nil { + t.Fatal(err) + } + input := Input{ + Signature: signature, Certificate: issuer.chain.Leaf.Raw, SignatureMethod: issuer.chain.Leaf.SignatureAlgorithm, + Hashes: []crypto.Hash{crypto.SHA256}, Objects: []Object{{Bytes: signatureBytes, Kind: historicalproof.Signature}}, + Timestamps: []Candidate{stamp.candidate}, + } + crl := testcert.CRL(t, issuer.chain) + for _, count := range []int{1024, 1025} { + for _, group := range []string{"signer", "TSA"} { + t.Run(group+"/"+strconv.Itoa(count), func(t *testing.T) { + t.Parallel() + + sources := make([]*evidence.Observation, count) + for index := range sources { + sources[index] = source + } + signerEvidence, tsaEvidence := pki.Evidence{CRLs: [][]byte{crl}}, pki.Evidence{CRLs: [][]byte{crl}} + if group == "signer" { + signerEvidence.Sources = sources + } else { + tsaEvidence.Sources = sources + } + report, err := Validate(t.Context(), Config{Signer: signer, TSA: issuer.verifier}, input, testcert.Now(), signerEvidence, tsaEvidence) + if count == 1024 { + if err != nil || report.Indication != Passed || report.Current == nil || len(report.Stamps) != 1 || report.Stamps[0].Indication != Passed { + t.Fatal("permitted observations changed valid signed-input acceptance", report, err) + } + + return + } + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil + if !errors.Is(err, ErrLimit) || report.Indication == Passed || !errors.Is(report.Cause, ErrLimit) || report.Current != nil || report.Past != nil || report.Proofs != nil { + t.Fatal("timestamp filtering bypassed evidence source count", report, err) + } + if len(sources) != count || sources[0] != source || sources[len(sources)-1] != source { + t.Fatal("source filtering mutated caller observations") + } + }) + } + } +} diff --git a/internal/historical/workflow_policy_internal_test.go b/internal/historical/workflow_policy_internal_test.go index 9d5a2d9..9efc637 100644 --- a/internal/historical/workflow_policy_internal_test.go +++ b/internal/historical/workflow_policy_internal_test.go @@ -108,6 +108,9 @@ func TestCurrentSubIndicationPreservesCertificateBoundsAndFailureClass(t *testin } }) } + if got := currentSubIndication(pki.ErrRevoked, pki.Report{}, nil, testcert.Now()); got != revoked { + t.Fatalf("revocation without a parsed certificate = %s; want %s", got, revoked) + } } func TestTimestampPreparationChecksAccuracyWithoutGrantingHistoricalTrust(t *testing.T) { @@ -303,6 +306,8 @@ func TestWorkflowTimestampFailureHonorsOptionalPolicyWithoutExtractingProofs(t * t.Fatal("rejected timestamp changed bound or lost diagnostic", report, err) } if required { + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if !errors.Is(err, ErrValidation) || !errors.Is(report.Cause, err) || report.Indication == Passed || report.Current != nil || report.Past != nil { t.Fatal("required timestamp did not stop signer acceptance", report, err) } @@ -345,6 +350,8 @@ func TestWorkflowRejectsContentChronologyEvenWhenTimestampsAreOptional(t *testin } continue } + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if !errors.Is(err, ErrValidation) || report.Indication == Passed || report.SubIndication != timestampOrderFailure || len(report.Stamps) != 2 || report.Stamps[1].SubIndication != timestampOrderFailure || report.Stamps[1].Indication != Indeterminate || report.Current != nil { t.Fatal("incoherent content chronology accepted as optional", report, err) } @@ -377,6 +384,8 @@ func TestWorkflowPastRecoveryRequiresTimestampedCertificateAndRevocationBytes(t input := Input{Signature: signature, Certificate: issuer.chain.Leaf.Raw, SignatureMethod: issuer.chain.Leaf.SignatureAlgorithm, Hashes: []crypto.Hash{crypto.SHA256}, Objects: []Object{{Bytes: signatureBytes, Kind: historicalproof.Signature}}, Timestamps: []Candidate{stamp.candidate}} report, err := Validate(t.Context(), config, input, testcert.Now(), pki.Evidence{CRLs: [][]byte{crl}}, pki.Evidence{CRLs: [][]byte{crl}}) if !fullCoverage { + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if !errors.Is(err, ErrValidation) || report.Indication == Passed || report.Past != nil { t.Fatal("signature proof alone repaired expired certificate/evidence hashes", report, err) } diff --git a/internal/historicalproof/proof.go b/internal/historicalproof/proof.go index 4593c11..4d7caf4 100644 --- a/internal/historicalproof/proof.go +++ b/internal/historicalproof/proof.go @@ -6,12 +6,22 @@ package historicalproof import ( "cmp" "context" + "crypto/sha256" "errors" "slices" "sort" "time" ) +const ( + // Selects linear lookup for small snapshots; it does not limit accepted facts. + indexedFactsThreshold = 64 + // Caps supplied facts before sorting and duplicate removal. + maximumFacts = 8192 + // Caps distinct object digests across all supplied facts and roles. + maximumObjects = 2048 +) + // ErrProof reports an invalid or over-budget proof set. var ErrProof = errors.New("invalid historical proof set") @@ -38,24 +48,31 @@ const ( // Fact binds one exact object's SHA-256 to its proven existence upper bound. // Source is the exact validating timestamp; zero denotes observation at Now. -// Kind distinguishes certificate, crl, ocsp, signature, timestamp and digest. +// Kind distinguishes certificate, crl, ocsp, signature, timestamp, digest and data. type Fact struct { - Digest [32]byte - Source [32]byte - At time.Time - Kind Kind + // Digest identifies the exact original object bytes by SHA-256. + Digest [sha256.Size]byte + // Source identifies the validating timestamp; zero denotes observation at the + // set's current time. + Source [sha256.Size]byte + // At is the proven upper bound on the object's existence time. + At time.Time + // Kind identifies the object's role without changing its exact-byte identity. + Kind Kind } // Set is an immutable, bounded proof snapshot. A zero value is invalid. type Set struct { - now time.Time + // now is the observation time limiting every retained proof bound. + now time.Time + // facts owns the validated, bounded proof snapshot. facts []Fact } // New snapshots facts after the format and trust workflows have authenticated // their bindings. At most 8192 facts and 2048 distinct objects are retained. func New(ctx context.Context, now time.Time, facts []Fact) (*Set, error) { - if ctx == nil || now.IsZero() || len(facts) > 8192 { + if ctx == nil || now.IsZero() || len(facts) > maximumFacts { return nil, ErrProof } @@ -63,7 +80,7 @@ func New(ctx context.Context, now time.Time, facts []Fact) (*Set, error) { return nil, err } - objects := make(map[[32]byte]struct{}, min(len(facts), 2048)) + objects := make(map[[sha256.Size]byte]struct{}, min(len(facts), maximumObjects)) owned := make([]Fact, 0, len(facts)) for _, fact := range facts { @@ -71,12 +88,12 @@ func New(ctx context.Context, now time.Time, facts []Fact) (*Set, error) { return nil, err } - if fact.Digest == ([32]byte{}) || fact.At.IsZero() || fact.At.After(now) || !fact.Kind.Valid() || fact.Source == ([32]byte{}) && !fact.At.Equal(now) { + if fact.Digest == ([sha256.Size]byte{}) || fact.At.IsZero() || fact.At.After(now) || !fact.Kind.Valid() || fact.Source == ([sha256.Size]byte{}) && !fact.At.Equal(now) { return nil, ErrProof } objects[fact.Digest] = struct{}{} - if len(objects) > 2048 { + if len(objects) > maximumObjects { return nil, ErrProof } @@ -105,7 +122,7 @@ func (s *Set) Valid() bool { return s != nil && !s.now.IsZero() } // Now is the explicit present-time observation used by this workflow. func (s *Set) Now() time.Time { - if !s.Valid() { + if s == nil || !s.Valid() { return time.Time{} } @@ -114,7 +131,7 @@ func (s *Set) Now() time.Time { // Facts returns an owned diagnostic copy, never a mutable authentication store. func (s *Set) Facts() []Fact { - if !s.Valid() { + if s == nil || !s.Valid() { return nil } @@ -122,19 +139,19 @@ func (s *Set) Facts() []Fact { } // Earliest returns the earliest proven existence upper bound of exact bytes. -func (s *Set) Earliest(digest [32]byte) (time.Time, bool) { +func (s *Set) Earliest(digest [sha256.Size]byte) (time.Time, bool) { return s.EarliestKind(digest, "") } // EarliestKind also requires the named object role when kind is nonempty. -func (s *Set) EarliestKind(digest [32]byte, kind Kind) (time.Time, bool) { +func (s *Set) EarliestKind(digest [sha256.Size]byte, kind Kind) (time.Time, bool) { if !s.Valid() { return time.Time{}, false } var earliest time.Time // Small workflow snapshots are cheaper to scan than to binary-search. - if len(s.facts) <= 64 { + if len(s.facts) <= indexedFactsThreshold { for _, fact := range s.facts { if fact.Digest == digest && (kind == "" || fact.Kind == kind) && (earliest.IsZero() || fact.At.Before(earliest)) { earliest = fact.At @@ -178,7 +195,7 @@ func (s *Set) EarliestKind(digest [32]byte, kind Kind) (time.Time, bool) { } // HasAtOrBefore applies the inclusive PoE boundary required by time sliding. -func (s *Set) HasAtOrBefore(digest [32]byte, at time.Time) bool { +func (s *Set) HasAtOrBefore(digest [sha256.Size]byte, at time.Time) bool { earliest, ok := s.Earliest(digest) return ok && !earliest.After(at) } diff --git a/internal/historicalproof/proof_test.go b/internal/historicalproof/proof_test.go index 8d64cb0..1af246c 100644 --- a/internal/historicalproof/proof_test.go +++ b/internal/historicalproof/proof_test.go @@ -12,12 +12,6 @@ import ( "github.com/skarm/xmlsec/internal/historicalproof" ) -func proofDigest(value int) [32]byte { - var digest [32]byte - binary.BigEndian.PutUint64(digest[:8], uint64(value)) - return digest -} - func TestProofRolesTimeAndOwnership(t *testing.T) { now := time.Unix(1700000000, 0).In(time.FixedZone("offset", 3*60*60)) first, second, source := proofDigest(1), proofDigest(2), proofDigest(3) @@ -208,3 +202,9 @@ func BenchmarkProofSkewedLookup(b *testing.B) { } } } + +func proofDigest(value int) [32]byte { + var digest [32]byte + binary.BigEndian.PutUint64(digest[:8], uint64(value)) + return digest +} diff --git a/internal/lexicon/catalog.go b/internal/lexicon/catalog.go index 4d7c107..19720d7 100644 --- a/internal/lexicon/catalog.go +++ b/internal/lexicon/catalog.go @@ -1,5 +1,7 @@ package lexicon +// Catalog element local names. Match NamespaceCatalog as well; a matching local +// name alone does not identify a catalog entry or authorize resource access. const ( ElemGroup = "group" ElemPublic = "public" @@ -13,6 +15,8 @@ const ( ElemNextCatalog = "nextCatalog" ) +// Catalog attribute names are unqualified. AttrName is also reused as the local +// name in other XML vocabularies, whose namespaces and rules remain separate. const ( AttrPrefer = "prefer" AttrPublicID = "publicId" @@ -26,6 +30,8 @@ const ( AttrName = "name" ) +// Values of the catalog prefer attribute select public/system identifier +// precedence; they do not grant permission to read the selected resource. const ( PreferPublic = "public" PreferSystem = "system" diff --git a/internal/lexicon/errors.go b/internal/lexicon/errors.go index 0e1856d..a0064cd 100644 --- a/internal/lexicon/errors.go +++ b/internal/lexicon/errors.go @@ -6,8 +6,13 @@ const ( // is not a valid XML character (e.g. fn:codepoints-to-string with an // out-of-range value). Used by xpath and asserted by test fixtures. ErrFOCH0001 = "FOCH0001" + // ErrFOAY0001 reports an array index outside the permitted range. ErrFOAY0001 = "FOAY0001" + // ErrFOAY0002 reports a negative array subarray length. + ErrFOAY0002 = "FOAY0002" ErrFODC0002 = "FODC0002" // document() / xsl:source-document load or parse error + // ErrFOJS0003 reports duplicate keys when the selected JSON or map policy rejects them. + ErrFOJS0003 = "FOJS0003" // ErrFORX0002 is the XPath FORX0002 ("invalid regular expression") error code. ErrFORX0002 = "FORX0002" ErrFOTY0013 = "FOTY0013" // atomization of map/function items @@ -15,6 +20,9 @@ const ( ErrSEPM0010 = "SEPM0010" // undeclare-prefixes="yes" requires XML 1.1 ErrSEPM0016 = "SEPM0016" // invalid serialization parameter value ErrSERE0006 = "SERE0006" // character invalid for the output XML version + // ErrSERE0008 reports unrepresentable output characters in contexts that cannot use + // character references. + ErrSERE0008 = "SERE0008" ErrSERE0022 = "SERE0022" // duplicate keys in JSON map ErrSESU0011 = "SESU0011" // unsupported normalization form ErrSESU0013 = "SESU0013" // unsupported encoding diff --git a/internal/lexicon/functions.go b/internal/lexicon/functions.go index 40032b1..d50a174 100644 --- a/internal/lexicon/functions.go +++ b/internal/lexicon/functions.go @@ -2,6 +2,8 @@ package lexicon // Function names shared by XPath and XSLT. const ( + // FnFormatNumber is the function local name used by numeric formatting and introspection. FnFormatNumber = "format-number" - FnGenerateID = "generate-id" + // FnGenerateID is the function local name used for generated node identifiers. + FnGenerateID = "generate-id" ) diff --git a/internal/lexicon/security.go b/internal/lexicon/security.go index 8e4cc9a..46a90f7 100644 --- a/internal/lexicon/security.go +++ b/internal/lexicon/security.go @@ -1,5 +1,174 @@ package lexicon +// RevocationReasonCertificateHold is the signed CRLReason ENUMERATED value for +// temporary certificate suspension. It is not a ReasonFlags bit position. +const RevocationReasonCertificateHold = 6 + +// GeneralName CHOICE tags identify context-specific X.509 name forms shared by +// certificate, CRL and TSA-name processing. They are not universal ASN.1 tags. +const ( + // GeneralNameOtherName wraps a type OID and its explicitly tagged value. + GeneralNameOtherName = 0 + // GeneralNameRFC822 carries an IA5 mailbox name. + GeneralNameRFC822 = 1 + // GeneralNameDNS carries an IA5 DNS name. + GeneralNameDNS = 2 + // GeneralNameX400 carries a constructed X.400 address. + GeneralNameX400 = 3 + // GeneralNameDirectory wraps an X.501 distinguished name. + GeneralNameDirectory = 4 + // GeneralNameEDI carries a constructed EDI party name. + GeneralNameEDI = 5 + // GeneralNameURI carries an IA5 uniform resource identifier. + GeneralNameURI = 6 + // GeneralNameIP carries address octets, rather than a textual IP address. + GeneralNameIP = 7 + // GeneralNameRegisteredID carries an implicitly tagged object identifier. + GeneralNameRegisteredID = 8 +) + +// PEM labels shared by key parsing and export. The label identifies the encoded +// ASN.1 container, so a different key container requires its own label. +const ( + // PEMPrivateKey identifies an unencrypted PKCS#8 PrivateKeyInfo. + PEMPrivateKey = "PRIVATE KEY" + // PEMEncryptedPrivateKey identifies a PKCS#8 EncryptedPrivateKeyInfo. + PEMEncryptedPrivateKey = "ENCRYPTED PRIVATE KEY" + // PEMPublicKey identifies a SubjectPublicKeyInfo encoding. + PEMPublicKey = "PUBLIC KEY" +) + +const ( + // HeaderContentType is the canonical HTTP field name used for XML and evidence media types. + HeaderContentType = "Content-Type" + // MediaTypePAOS identifies PAOS content for SAML enhanced client/proxy exchanges. + MediaTypePAOS = "application/vnd.paos+xml" + // MediaTypeSOAP identifies the text/xml media type used by SOAP 1.1 bindings and proof parsing. + MediaTypeSOAP = "text/xml" + // MediaTypeXML identifies generic XML content accepted by the XML/SAML resource adapters. + MediaTypeXML = "application/xml" + // NamespaceSAMLChannelBinding identifies the channel-binding extension shared by metadata and + // protocol processing. + NamespaceSAMLChannelBinding = "urn:oasis:names:tc:SAML:protocol:ext:channel-binding" + // SAMLAttrID names the case-sensitive, unqualified ID attribute of SAML messages and assertions. + SAMLAttrID = "ID" + // SAMLBindingPAOS identifies the SAML 2.0 PAOS binding; callers select the supported ECP profile. + SAMLBindingPAOS = "urn:oasis:names:tc:SAML:2.0:bindings:PAOS" + // SAMLElementArtifactResolve names a SAML protocol message; match NamespaceSAMLProtocol when using + // it. + SAMLElementArtifactResolve = "ArtifactResolve" + // SAMLElementArtifactResponse names a SAML protocol message; match NamespaceSAMLProtocol when + // using it. + SAMLElementArtifactResponse = "ArtifactResponse" + // SAMLElementAssertionIDRequest names a SAML protocol message; match NamespaceSAMLProtocol when + // using it. + SAMLElementAssertionIDRequest = "AssertionIDRequest" + // SAMLElementAttributeQuery names a SAML protocol message; match NamespaceSAMLProtocol when using + // it. + SAMLElementAttributeQuery = "AttributeQuery" + // SAMLElementAuthnQuery names a SAML protocol message; match NamespaceSAMLProtocol when using it. + SAMLElementAuthnQuery = "AuthnQuery" + // SAMLElementAuthnRequest names a SAML protocol message; match NamespaceSAMLProtocol when using + // it. + SAMLElementAuthnRequest = "AuthnRequest" + // SAMLElementAuthzDecisionQuery names a SAML protocol message; match NamespaceSAMLProtocol when + // using it. + SAMLElementAuthzDecisionQuery = "AuthzDecisionQuery" + // SAMLElementLogoutRequest names a SAML protocol message; match NamespaceSAMLProtocol when using + // it. + SAMLElementLogoutRequest = "LogoutRequest" + // SAMLElementLogoutResponse names a SAML protocol message; match NamespaceSAMLProtocol when using + // it. + SAMLElementLogoutResponse = "LogoutResponse" + // SAMLElementManageNameIDRequest names a SAML protocol message; match NamespaceSAMLProtocol when + // using it. + SAMLElementManageNameIDRequest = "ManageNameIDRequest" + // SAMLElementManageNameIDResponse names a SAML protocol message; match NamespaceSAMLProtocol when + // using it. + SAMLElementManageNameIDResponse = "ManageNameIDResponse" + // SAMLElementNameIDMappingRequest names a SAML protocol message; match NamespaceSAMLProtocol when + // using it. + SAMLElementNameIDMappingRequest = "NameIDMappingRequest" + // SAMLElementNameIDMappingResponse names a SAML protocol message; match NamespaceSAMLProtocol when + // using it. + SAMLElementNameIDMappingResponse = "NameIDMappingResponse" + // SAMLElementResponse names a SAML protocol message; match NamespaceSAMLProtocol when using it. + SAMLElementResponse = "Response" + // SAMLNameIDFormatEntity selects the NameID format for a SAML entity identifier, including ECP + // issuers. + SAMLNameIDFormatEntity = "urn:oasis:names:tc:SAML:2.0:nameid-format:entity" + // C14NAttrPrefixList names the Exclusive C14N inclusive-prefix list attribute. + C14NAttrPrefixList = "PrefixList" + // C14NDefaultPrefixToken denotes the default namespace in an Exclusive C14N + // PrefixList. It does not name an XSLT processing mode. + C14NDefaultPrefixToken = "#default" + // C14NElementInclusiveNamespaces names the Exclusive C14N parameter element; match its + // canonicalization namespace. + C14NElementInclusiveNamespaces = "InclusiveNamespaces" + // DSigElementECKeyValue names the XMLDSig 1.1 representation combining a named curve and an + // encoded EC point. + DSigElementECKeyValue = "ECKeyValue" + // DSigElementNamedCurve names the curve-identifier element inside an XMLDSig 1.1 ECKeyValue. + DSigElementNamedCurve = "NamedCurve" + // DSigElementObject names an XMLDSig element; match the namespace required by that element's + // schema. + DSigElementObject = "Object" + // DSigElementPublicKey names the element carrying an encoded EC public point in XMLDSig 1.1. + DSigElementPublicKey = "PublicKey" + // DSigElementRetrievalMethod names a KeyInfo resource link; external retrieval follows explicit + // caller permissions. + DSigElementRetrievalMethod = "RetrievalMethod" + // PrefixDSig is the suggested prefix for generated XMLDSig elements; their namespace URI + // determines identity. + PrefixDSig = "ds" + // PrefixDSig11 is the suggested prefix for generated XMLDSig 1.1 elements; their namespace URI + // determines identity. + PrefixDSig11 = "dsig11" + // PrefixExclusiveC14N is the suggested prefix for Exclusive C14N parameter elements. + PrefixExclusiveC14N = "ec" + // SecurityAttrAlgorithm names the unqualified Algorithm attribute carrying an algorithm URI. + SecurityAttrAlgorithm = "Algorithm" + // SecurityAttrEncoding names the unqualified Encoding attribute describing a value encoding. + SecurityAttrEncoding = "Encoding" + // SecurityAttrID names the case-sensitive, unqualified Id attribute of XML security elements. + SecurityAttrID = "Id" + // SecurityAttrMimeType names the unqualified MimeType attribute declaring a resource media type. + SecurityAttrMimeType = "MimeType" + // SecurityAttrTarget names the unqualified Target attribute linking properties to their target. + SecurityAttrTarget = "Target" + // SecurityAttrType names the unqualified Type attribute describing a resource representation. + SecurityAttrType = "Type" + // SecurityAttrURI names the unqualified URI attribute used by signature, encryption and XAdES + // links. + SecurityAttrURI = "URI" + // ASN1GeneralizedTimeLayout is the canonical UTC seconds-only DER GeneralizedTime layout. + ASN1GeneralizedTimeLayout = "20060102150405Z" + // ASN1UTCTimeLayout is the canonical UTC seconds-only DER UTCTime layout. + ASN1UTCTimeLayout = "060102150405Z" + // HistoricalCodeChainConstraintsFailure is the shared sub-indication for a certificate chain + // outside the selected constraints. + HistoricalCodeChainConstraintsFailure = "CHAIN_CONSTRAINTS_FAILURE" + // HistoricalCodeSigCryptoFailure is the shared sub-indication for a failed cryptographic signature + // check. + HistoricalCodeSigCryptoFailure = "SIG_CRYPTO_FAILURE" + // SignatureFamilyECDSA selects the parsed ECDSA signature family in internal algorithm dispatch. + SignatureFamilyECDSA = "ecdsa" + // SignatureFamilyRSA selects the parsed RSA PKCS#1 v1.5 signature family in internal algorithm + // dispatch. + SignatureFamilyRSA = "rsa" + // SignatureFamilyRSAPSS selects the parsed RSA-PSS signature family in internal algorithm + // dispatch. + SignatureFamilyRSAPSS = "pss" + // X509OIDCertificateIssuer identifies the CRL-entry extension that specifies the certificate + // issuer. + X509OIDCertificateIssuer = "2.5.29.29" + // X509OIDDeltaCRLIndicator identifies the extension carrying the base CRL number of a delta CRL. + X509OIDDeltaCRLIndicator = "2.5.29.27" + // X509OIDIssuingDistributionPoint identifies the CRL extension that describes distribution-point + // scope. + X509OIDIssuingDistributionPoint = "2.5.29.28" +) + // Shared security protocol identifiers; policy remains with each consumer. const ( // NamespaceSAMLAssertion is the SAML 2.0 assertion namespace URI. @@ -12,9 +181,9 @@ const ( SAMLBindingHTTPPost = "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" // SAMLBindingHTTPRedirect is the SAML HTTP-Redirect binding URI. SAMLBindingHTTPRedirect = "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect" - // SAMLBindingHTTPArtifact is the SAML HTTP-Artifact binding URI; artifact processing is unsupported. + // SAMLBindingHTTPArtifact is the SAML HTTP-Artifact binding URI. SAMLBindingHTTPArtifact = "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Artifact" - // SAMLBindingSOAP is the SAML SOAP binding URI; SOAP processing is unsupported. + // SAMLBindingSOAP is the SAML SOAP binding URI. SAMLBindingSOAP = "urn:oasis:names:tc:SAML:2.0:bindings:SOAP" // DigestSHA1 is the SHA-1 digest algorithm URI. DigestSHA1 = "http://www.w3.org/2000/09/xmldsig#sha1" diff --git a/internal/lexicon/serialization.go b/internal/lexicon/serialization.go index 538c872..187dd08 100644 --- a/internal/lexicon/serialization.go +++ b/internal/lexicon/serialization.go @@ -2,7 +2,9 @@ package lexicon // XML declaration versions and whitespace. const ( + // XMLVersion10 selects XML 1.0 declaration and character rules. XMLVersion10 = "1.0" + // XMLVersion11 selects XML 1.1 declaration and character rules. XMLVersion11 = "1.1" // XMLWhitespace is the set of XSD whitespace characters (#x20 space, #x9 tab, // #xD carriage return, #xA newline). Unlike Go's unicode.IsSpace this excludes @@ -31,26 +33,42 @@ const ( MethodAdaptive = "adaptive" ) -// Serialization parameter names (xsl:output / xsl:result-document attributes). -// Also shared by fn:serialize. +// Internal processing-instruction targets used to carry output-escaping state +// between result-tree construction and serialization. These marker names are +// distinct from the XSLT disable-output-escaping attribute. const ( - SerializationMethod = "method" - SerializationVersion = "version" - SerializationEncoding = "encoding" - SerializationOmitXMLDeclaration = "omit-xml-declaration" - SerializationStandalone = "standalone" - SerializationDoctypePublic = "doctype-public" + // DisableOutputEscapingPITarget starts a span emitted without XML/HTML escaping. + DisableOutputEscapingPITarget = "disable-output-escaping" + // EnableOutputEscapingPITarget ends that span and restores normal escaping. + EnableOutputEscapingPITarget = "enable-output-escaping" +) + +// Serialization parameter names shared by xsl:output, xsl:result-document and +// fn:serialize. These are keys; accepted values and defaults depend on the method. +const ( + SerializationMethod = "method" + // SerializationVersion identifies a method's output version, not the XSLT language version. + SerializationVersion = "version" + SerializationEncoding = "encoding" + SerializationOmitXMLDeclaration = "omit-xml-declaration" + SerializationStandalone = "standalone" + // SerializationDoctypePublic names emitted public-ID metadata; it performs no DTD retrieval. + SerializationDoctypePublic = "doctype-public" + // SerializationDoctypeSystem names emitted system-ID metadata; it grants no resource access. SerializationDoctypeSystem = "doctype-system" SerializationCDATASectionElements = "cdata-section-elements" SerializationIndent = "indent" - SerializationMediaType = "media-type" - SerializationByteOrderMark = "byte-order-mark" - SerializationEscapeURIAttributes = "escape-uri-attributes" - SerializationIncludeContentType = "include-content-type" - SerializationNormalizationForm = "normalization-form" - SerializationUndeclarePrefixes = "undeclare-prefixes" - SerializationUseCharacterMaps = "use-character-maps" - SerializationSuppressIndentation = "suppress-indentation" + // SerializationMediaType names declared output metadata, not an input trust decision. + SerializationMediaType = "media-type" + SerializationByteOrderMark = "byte-order-mark" + SerializationEscapeURIAttributes = "escape-uri-attributes" + SerializationIncludeContentType = "include-content-type" + // SerializationNormalizationForm selects Unicode normalization for eligible output content. + SerializationNormalizationForm = "normalization-form" + SerializationUndeclarePrefixes = "undeclare-prefixes" + SerializationUseCharacterMaps = "use-character-maps" + SerializationSuppressIndentation = "suppress-indentation" + // SerializationHTMLVersion selects the HTML edition independently of an XML declaration. SerializationHTMLVersion = "html-version" SerializationItemSeparator = "item-separator" SerializationJSONNodeOutputMethod = "json-node-output-method" @@ -58,3 +76,15 @@ const ( SerializationBuildTree = "build-tree" SerializationAllowDuplicateNames = "allow-duplicate-names" ) + +// Unicode normalization names shared by string functions and serializers. +const ( + // NormalizationFormNFC selects canonical composition. + NormalizationFormNFC = "NFC" + // NormalizationFormNFD selects canonical decomposition. + NormalizationFormNFD = "NFD" + // NormalizationFormNFKC selects compatibility composition. + NormalizationFormNFKC = "NFKC" + // NormalizationFormNFKD selects compatibility decomposition. + NormalizationFormNFKD = "NFKD" +) diff --git a/internal/lexicon/xml.go b/internal/lexicon/xml.go index 173934b..45cd8a3 100644 --- a/internal/lexicon/xml.go +++ b/internal/lexicon/xml.go @@ -1,29 +1,68 @@ package lexicon +// Byte-order marks and escaped XML spellings are exact bytes. Consumers select +// the permitted encoding and the text/attribute context before using them. const ( - PrefixXML = "xml" + // BOMUTF16BE contains the original big-endian UTF-16 byte-order mark bytes for encoding detection. + BOMUTF16BE = "\xfe\xff" + // BOMUTF16LE contains the original little-endian UTF-16 byte-order mark bytes for encoding + // detection. + BOMUTF16LE = "\xff\xfe" + // BOMUTF32BE contains the original big-endian UTF-32 byte-order mark bytes for encoding detection. + BOMUTF32BE = "\x00\x00\xfe\xff" + // BOMUTF32LE contains the original little-endian UTF-32 byte-order mark bytes for encoding + // detection. + BOMUTF32LE = "\xff\xfe\x00\x00" + // BOMUTF8 contains the original UTF-8 byte-order mark bytes for encoding detection. + BOMUTF8 = "\xef\xbb\xbf" + // EscapeAmpersand is the XML reference emitted for a literal ampersand. + EscapeAmpersand = "&" + // EscapeGreaterThan is the XML reference emitted when a literal greater-than sign must be escaped. + EscapeGreaterThan = ">" + // EscapeLessThan is the XML reference emitted for a literal less-than sign. + EscapeLessThan = "<" + // EscapeQuote is the XML reference emitted for a double quote in an attribute value. + EscapeQuote = """ +) + +// Reserved XML names, lexical attribute values and shared local names. A local +// name must be paired with the namespace required by its consuming vocabulary. +const ( + // PrefixXML names the predefined xml prefix bound to NamespaceXML. + PrefixXML = "xml" + // ReservedXMLPITarget cannot be used as a processing-instruction target, + // compared without case sensitivity. It does not select an output method. + ReservedXMLPITarget = "xml" + // PrefixXMLNS names the reserved xmlns prefix used for namespace declarations. PrefixXMLNS = "xmlns" - PrefixErr = "err" + // PrefixErr is the conventional prefix for NamespaceErr, not the error namespace itself. + PrefixErr = "err" AttrBase = "base" AttrID = "id" AttrLang = "lang" AttrSpace = "space" + // QNameXMLBase is the reserved prefixed spelling of the XML base attribute. QNameXMLBase = "xml:base" QNameXMLID = "xml:id" QNameXMLLang = "xml:lang" QNameXMLSpace = "xml:space" - SpaceDefault = "default" + // SpaceDefault selects ordinary whitespace handling through xml:space. + SpaceDefault = "default" + // SpacePreserve requests preservation through xml:space. SpacePreserve = "preserve" + // ValueYes and ValueNo are lexical yes/no tokens; accepted forms depend on the attribute. ValueYes = "yes" ValueNo = "no" // DeclVersion names the version pseudo-attribute of an XML declaration. - DeclVersion = "version" - DeclEncoding = "encoding" + DeclVersion = "version" + // DeclEncoding names an XML declaration's encoding pseudo-attribute. + DeclEncoding = "encoding" + // DeclStandalone names an XML declaration's standalone pseudo-attribute. DeclStandalone = "standalone" // AttrValue names the value attribute shared by several XML vocabularies. @@ -60,7 +99,8 @@ const ( SchemeFile = "file" ) -// XSD prefixed type name constants (xs: prefix). +// Conventional xs:-prefixed spellings used in type displays and matching. Type +// resolution still depends on NamespaceXSD, regardless of the chosen prefix. const ( XSUntyped = "xs:untyped" XSAnyType = "xs:anyType" @@ -136,7 +176,7 @@ const ( TypeNumeric = "numeric" ) -// XPath node test string constants (with parentheses). +// XPath node and item test spellings, including parentheses. const ( NodeTestNode = "node()" NodeTestElement = "element()" @@ -181,11 +221,14 @@ const ( // XSD prefix constants. const ( - PrefixXS = "xs" + // PrefixXS is the conventional prefix used for XML Schema type names. + PrefixXS = "xs" + // PrefixXSD is the alternate XML Schema prefix recognized by schema type resolution. PrefixXSD = "xsd" ) -// Output encoding constants. +// Encoding-label spellings shared by lookup and output. Keep their original +// case and punctuation when emitting a declaration; aliases are not new codecs. const ( EncodingUTF8 = "utf-8" EncodingUTF8U = "UTF-8" diff --git a/internal/lexicon/xslt.go b/internal/lexicon/xslt.go index 4481900..d612298 100644 --- a/internal/lexicon/xslt.go +++ b/internal/lexicon/xslt.go @@ -1,7 +1,40 @@ package lexicon -// XSLT element local name constants. These mirror the elem* constants in -// xslt/stylesheet.go but are exported for use across packages. +const ( + // DecimalFormatDecimalSeparator names the "decimal-separator" property shared by XSLT declarations + // and host decimal formats. + DecimalFormatDecimalSeparator = "decimal-separator" + // DecimalFormatDigit names the "digit" property shared by XSLT declarations and host decimal + // formats. + DecimalFormatDigit = "digit" + // DecimalFormatExponentSeparator names the "exponent-separator" property shared by XSLT + // declarations and host decimal formats. + DecimalFormatExponentSeparator = "exponent-separator" + // DecimalFormatGroupingSeparator names the "grouping-separator" property shared by XSLT + // declarations and host decimal formats. + DecimalFormatGroupingSeparator = "grouping-separator" + // DecimalFormatInfinity names the "infinity" property shared by XSLT declarations and host decimal + // formats. + DecimalFormatInfinity = "infinity" + // DecimalFormatMinusSign names the "minus-sign" property shared by XSLT declarations and host + // decimal formats. + DecimalFormatMinusSign = "minus-sign" + // DecimalFormatPatternSeparator names the "pattern-separator" property shared by XSLT declarations + // and host decimal formats. + DecimalFormatPatternSeparator = "pattern-separator" + // DecimalFormatPerMille names the "per-mille" property shared by XSLT declarations and host + // decimal formats. + DecimalFormatPerMille = "per-mille" + // DecimalFormatPercent names the "percent" property shared by XSLT declarations and host decimal + // formats. + DecimalFormatPercent = "percent" + // DecimalFormatZeroDigit names the "zero-digit" property shared by XSLT declarations and host + // decimal formats. + DecimalFormatZeroDigit = "zero-digit" +) + +// XSLT element local names shared by compilation and language introspection. +// Consumers must also match NamespaceXSLT. const ( XSLTElementAccumulator = "accumulator" XSLTElementAccumulatorRule = "accumulator-rule" @@ -87,7 +120,8 @@ const ( XSLTElementTransform = "transform" ) -// XSLT streamability category constants. +// Names used in static streamability metadata. A category alone does not assert +// that a particular program executes with bounded streaming storage. const ( StreamAbsorbing = "absorbing" StreamInspection = "inspection" @@ -105,11 +139,15 @@ const ( // XSLT output constants. const ( + // OutputDisableEscaping names the XSLT attribute; internal PI markers have separate names. OutputDisableEscaping = "disable-output-escaping" OutputDescending = "descending" - OutputVersion10 = "1.0" - OutputRaw = "raw" - NormFullyNormalized = "FULLY-NORMALIZED" + // OutputVersion10 is the serialization version token, distinct from XSLTVersion10. + OutputVersion10 = "1.0" + // OutputRaw selects delivery of an un-serialized result value. + OutputRaw = "raw" + // NormFullyNormalized names the fully-normalized request; support is checked by the serializer. + NormFullyNormalized = "FULLY-NORMALIZED" ) // XSLT XPath function names used in streamability analysis. @@ -126,3 +164,6 @@ const ( XSLTVersion20 = "2.0" XSLTVersion30 = "3.0" ) + +// DecimalFormatNaN names the NaN property in a decimal-format declaration. +const DecimalFormatNaN = "NaN" diff --git a/internal/normalization/normalization.go b/internal/normalization/normalization.go index 2c87dcb..40078b3 100644 --- a/internal/normalization/normalization.go +++ b/internal/normalization/normalization.go @@ -15,6 +15,17 @@ import ( "golang.org/x/text/unicode/norm" ) +const ( + cancellationInterval = 4096 + // Identifies Unicode CGJ while detecting extra joiners inserted by the underlying stream-safe + // normalizer. + combiningGraphemeJoiner = "\u034f" + normalizationReadBufferBytes = 4096 + // Target byte size for batching complete normalization segments; a combining segment can exceed + // it. + segmentChunkBytes = 4096 +) + // Form selects a Unicode normalization form. type Form norm.Form @@ -46,7 +57,7 @@ func (f Form) EachBytes(ctx context.Context, s string, emit func([]byte) error) var iterator norm.Iter return f.segments(ctx, s, func(source string) error { - if len(source) > 4096+utf8.UTFMax { + if len(source) > segmentChunkBytes+utf8.UTFMax { result, err := f.longSegment(ctx, source) if err != nil { return err @@ -59,10 +70,10 @@ func (f Form) EachBytes(ctx context.Context, s string, emit func([]byte) error) joiners := 0 for !iterator.Done() { - joiners += bytes.Count(iterator.Next(), []byte("\u034f")) + joiners += bytes.Count(iterator.Next(), []byte(combiningGraphemeJoiner)) } - if joiners != strings.Count(source, "\u034f") { + if joiners != strings.Count(source, combiningGraphemeJoiner) { result, err := f.longSegment(ctx, source) if err != nil { return err @@ -137,7 +148,7 @@ func (f Form) segments(ctx context.Context, s string, emit func(string) error) e end := len(s) for at := 0; at < len(s); { - if at >= 4096 && f.PropertiesString(s[at:]).BoundaryBefore() { + if at >= segmentChunkBytes && f.PropertiesString(s[at:]).BoundaryBefore() { end = at break } @@ -145,7 +156,7 @@ func (f Form) segments(ctx context.Context, s string, emit func(string) error) e _, size := utf8.DecodeRuneInString(s[at:]) at += size - if at%4096 < utf8.UTFMax { + if at%cancellationInterval < utf8.UTFMax { if err := ctx.Err(); err != nil { return err } @@ -163,12 +174,12 @@ func (f Form) segments(ctx context.Context, s string, emit func(string) error) e } func (f Form) segment(ctx context.Context, s string) (string, error) { - if len(s) > 4096+utf8.UTFMax { + if len(s) > segmentChunkBytes+utf8.UTFMax { return f.longSegment(ctx, s) } result := norm.Form(f).String(s) - if strings.Count(result, "\u034f") == strings.Count(s, "\u034f") { + if strings.Count(result, combiningGraphemeJoiner) == strings.Count(s, combiningGraphemeJoiner) { return result, ctx.Err() } @@ -191,7 +202,7 @@ func (f Form) longSegment(ctx context.Context, s string) (string, error) { out.Grow(len(s)) for at, c := range chars { - if at%4096 == 0 { + if at%cancellationInterval == 0 { if err := ctx.Err(); err != nil { return "", err } @@ -221,7 +232,7 @@ func (f Form) longSegmentBytes(ctx context.Context, s []byte) ([]byte, error) { out := make([]byte, 0, len(s)) for at, c := range chars { - if at%4096 == 0 { + if at%cancellationInterval == 0 { if err := ctx.Err(); err != nil { return nil, err } @@ -234,15 +245,17 @@ func (f Form) longSegmentBytes(ctx context.Context, s []byte) ([]byte, error) { } func (f Form) normalizedCharacters(ctx context.Context, size int, input iter.Seq2[int, rune]) ([]character, error) { + const initialCharacterCapacityDivisor = 2 + decomposition := norm.NFD if f == NFKC || f == NFKD { decomposition = norm.NFKD } - chars := make([]character, 0, size/2) + chars := make([]character, 0, size/initialCharacterCapacityDivisor) for at, r := range input { - if at%4096 < utf8.UTFMax { + if at%cancellationInterval < utf8.UTFMax { if err := ctx.Err(); err != nil { return nil, err } @@ -271,12 +284,12 @@ func (f Form) normalizedCharacters(ctx context.Context, size int, input iter.Seq } func (f Form) segmentBytes(ctx context.Context, s []byte) ([]byte, error) { - if len(s) > 4096+utf8.UTFMax { + if len(s) > segmentChunkBytes+utf8.UTFMax { return f.longSegmentBytes(ctx, s) } result := norm.Form(f).Bytes(s) - if bytes.Count(result, []byte("\u034f")) == bytes.Count(s, []byte("\u034f")) { + if bytes.Count(result, []byte(combiningGraphemeJoiner)) == bytes.Count(s, []byte(combiningGraphemeJoiner)) { return result, ctx.Err() } @@ -293,7 +306,7 @@ func (f Form) byteSegments(ctx context.Context, s []byte) iter.Seq2[[]byte, erro end := len(s) for at := 0; at < len(s); { - if at >= 4096 && norm.Form(f).Properties(s[at:]).BoundaryBefore() { + if at >= segmentChunkBytes && norm.Form(f).Properties(s[at:]).BoundaryBefore() { end = at break } @@ -301,7 +314,7 @@ func (f Form) byteSegments(ctx context.Context, s []byte) iter.Seq2[[]byte, erro _, size := utf8.DecodeRune(s[at:]) at += size - if at%4096 < utf8.UTFMax { + if at%cancellationInterval < utf8.UTFMax { if err := ctx.Err(); err != nil { yield(nil, err) return @@ -318,123 +331,6 @@ func (f Form) byteSegments(ctx context.Context, s []byte) iter.Seq2[[]byte, erro } } -// reorder uses stable counting sort on each non-starter run: long adversarial -// combining sequences must not turn canonical ordering into quadratic work. -func reorder(ctx context.Context, chars []character) error { - var scratch []character - - for at := 0; at < len(chars); { - if err := ctx.Err(); err != nil { - return err - } - - if chars[at].class == 0 { - at++ - continue - } - - end := at + 1 - ordered := true - - for end < len(chars) && chars[end].class != 0 { - if end%4096 == 0 { - if err := ctx.Err(); err != nil { - return err - } - } - - ordered = ordered && chars[end-1].class <= chars[end].class - end++ - } - - if !ordered { - if cap(scratch) < end-at { - scratch = make([]character, end-at) - } else { - scratch = scratch[:end-at] - } - - if err := sortRun(ctx, chars[at:end], scratch); err != nil { - return err - } - } - - at = end - } - - return nil -} - -func sortRun(ctx context.Context, chars, scratch []character) error { - var counts [256]int - for i, c := range chars { - counts[c.class]++ - - if i%4096 == 0 { - if err := ctx.Err(); err != nil { - return err - } - } - } - - offset := 0 - for i, count := range counts { - counts[i] = offset - offset += count - } - - for i, c := range chars { - scratch[counts[c.class]] = c - counts[c.class]++ - - if i%4096 == 0 { - if err := ctx.Err(); err != nil { - return err - } - } - } - - copy(chars, scratch) - - return ctx.Err() -} - -func compose(ctx context.Context, chars []character) ([]character, error) { - out := chars[:0] - starter, previous := -1, uint8(0) - - for at, c := range chars { - if at%4096 == 0 { - if err := ctx.Err(); err != nil { - return nil, err - } - } - - if starter >= 0 && (previous < c.class || previous == 0) { - var pair [utf8.UTFMax * 2]byte - - n := utf8.EncodeRune(pair[:], out[starter].value) - n += utf8.EncodeRune(pair[n:], c.value) - combined := norm.NFC.Bytes(pair[:n]) - - r, size := utf8.DecodeRune(combined) - if size == len(combined) { - out[starter].value = r - continue - } - } - - if c.class == 0 { - starter = len(out) - } - - out = append(out, c) - previous = c.class - } - - return out, nil -} - // Writer preserves incomplete UTF-8 and the last normalization segment across // writes. Close flushes it without closing the destination. No global pool // retains large segments after completion. @@ -465,7 +361,7 @@ func (w *Writer) Write(p []byte) (int, error) { return i, w.err } - nextCheck = i + 4096 + nextCheck = i + cancellationInterval } // Every ASCII scalar has BoundaryBefore in all four forms. Batch it @@ -497,7 +393,7 @@ func (w *Writer) Write(p []byte) (int, error) { return i, w.err } - if norm.Form(w.form).Properties(w.tail[:w.tailSize]).BoundaryBefore() && len(w.pending) >= 4096 { + if norm.Form(w.form).Properties(w.tail[:w.tailSize]).BoundaryBefore() && len(w.pending) >= segmentChunkBytes { if w.err = w.flush(); w.err != nil { return i, w.err } @@ -512,7 +408,7 @@ func (w *Writer) Write(p []byte) (int, error) { // WriteString feeds bounded chunks without copying the complete source string. func (w *Writer) WriteString(s string) (int, error) { - var buffer [4096]byte + var buffer [normalizationReadBufferBytes]byte written := 0 @@ -552,14 +448,14 @@ func (w *Writer) Close() error { } func (w *Writer) writeASCII(p []byte) (int, error) { - if len(w.pending) >= 4096 { + if len(w.pending) >= segmentChunkBytes { if err := w.flush(); err != nil { - return 1, err + return 0, err } } end := 1 - limit := min(len(p), 4096-len(w.pending)) + limit := min(len(p), segmentChunkBytes-len(w.pending)) for end < limit && p[end] < utf8.RuneSelf { end++ @@ -571,6 +467,10 @@ func (w *Writer) writeASCII(p []byte) (int, error) { } func (w *Writer) flush() error { + // Retain at most this many scratch bytes between completed segments. This + // is a reuse threshold, not a bound on an accepted normalization segment. + const maximumRetainedPendingBytes = 8192 + if err := w.ctx.Err(); err != nil { return err } @@ -581,9 +481,9 @@ func (w *Writer) flush() error { var result []byte - if len(w.pending) <= 4096+utf8.UTFMax { + if len(w.pending) <= segmentChunkBytes+utf8.UTFMax { w.normalized = norm.Form(w.form).Append(w.normalized[:0], w.pending...) - if bytes.Count(w.normalized, []byte("\u034f")) == bytes.Count(w.pending, []byte("\u034f")) { + if bytes.Count(w.normalized, []byte(combiningGraphemeJoiner)) == bytes.Count(w.pending, []byte(combiningGraphemeJoiner)) { result = w.normalized } } @@ -606,7 +506,7 @@ func (w *Writer) flush() error { err = io.ErrShortWrite } - if cap(w.pending) > 8192 { + if cap(w.pending) > maximumRetainedPendingBytes { w.pending = nil } else { w.pending = w.pending[:0] @@ -657,7 +557,7 @@ func IsNormal(ctx context.Context, f Form, s []byte) (bool, error) { return false, err } - if len(part) > 4096 || !norm.Form(f).IsNormal(part) { + if len(part) > segmentChunkBytes || !norm.Form(f).IsNormal(part) { result, err := f.segmentBytes(ctx, part) if err != nil { return false, err @@ -671,3 +571,122 @@ func IsNormal(ctx context.Context, f Form, s []byte) (bool, error) { return true, ctx.Err() } + +// reorder uses stable counting sort on each non-starter run: long adversarial +// combining sequences must not turn canonical ordering into quadratic work. +func reorder(ctx context.Context, chars []character) error { + var scratch []character + + for at := 0; at < len(chars); { + if err := ctx.Err(); err != nil { + return err + } + + if chars[at].class == 0 { + at++ + continue + } + + end := at + 1 + ordered := true + + for end < len(chars) && chars[end].class != 0 { + if end%cancellationInterval == 0 { + if err := ctx.Err(); err != nil { + return err + } + } + + ordered = ordered && chars[end-1].class <= chars[end].class + end++ + } + + if !ordered { + if cap(scratch) < end-at { + scratch = make([]character, end-at) + } else { + scratch = scratch[:end-at] + } + + if err := sortRun(ctx, chars[at:end], scratch); err != nil { + return err + } + } + + at = end + } + + return nil +} + +func sortRun(ctx context.Context, chars, scratch []character) error { + const combiningClassCount = 256 // All values of the uint8 canonical combining class. + + var counts [combiningClassCount]int + for i, c := range chars { + counts[c.class]++ + + if i%cancellationInterval == 0 { + if err := ctx.Err(); err != nil { + return err + } + } + } + + offset := 0 + for i, count := range counts { + counts[i] = offset + offset += count + } + + for i, c := range chars { + scratch[counts[c.class]] = c + counts[c.class]++ + + if i%cancellationInterval == 0 { + if err := ctx.Err(); err != nil { + return err + } + } + } + + copy(chars, scratch) + + return ctx.Err() +} + +func compose(ctx context.Context, chars []character) ([]character, error) { + out := chars[:0] + starter, previous := -1, uint8(0) + + for at, c := range chars { + if at%cancellationInterval == 0 { + if err := ctx.Err(); err != nil { + return nil, err + } + } + + if starter >= 0 && (previous < c.class || previous == 0) { + var pair [utf8.UTFMax * 2]byte + + n := utf8.EncodeRune(pair[:], out[starter].value) + n += utf8.EncodeRune(pair[n:], c.value) + combined := norm.NFC.Bytes(pair[:n]) + + r, size := utf8.DecodeRune(combined) + if size == len(combined) { + out[starter].value = r + continue + } + } + + if c.class == 0 { + starter = len(out) + } + + out = append(out, c) + previous = c.class + } + + return out, nil +} diff --git a/internal/normalization/normalization_test.go b/internal/normalization/normalization_test.go index 9554015..b21587f 100644 --- a/internal/normalization/normalization_test.go +++ b/internal/normalization/normalization_test.go @@ -152,6 +152,52 @@ type failingWriter struct { calls int } +func TestPendingFlushDoesNotConsumeNextByte(t *testing.T) { + const prefixBytes = 4096 + prefix := strings.Repeat("a", prefixBytes) + failure := errors.New("destination rejected pending output") + + for _, short := range []bool{false, true} { + t.Run(strconv.FormatBool(short), func(t *testing.T) { + sink := &failingWriter{err: failure} + want := failure + if short { + sink.err, want = nil, io.ErrShortWrite + } + + writer := normalization.NewWriter(t.Context(), sink, normalization.NFC) + if n, err := writer.Write([]byte(prefix)); n != prefixBytes || err != nil || sink.calls != 0 { + t.Fatalf("pending input: n=%d error=%v calls=%d", n, err, sink.calls) + } + + if n, err := writer.Write([]byte("b")); n != 0 || !errors.Is(err, want) || sink.calls != 1 { + t.Fatalf("unaccepted byte counted: n=%d error=%v calls=%d", n, err, sink.calls) + } + + if n, err := writer.Write([]byte("c")); n != 0 || !errors.Is(err, want) || sink.calls != 1 { + t.Fatalf("failure was not terminal: n=%d error=%v calls=%d", n, err, sink.calls) + } + if err := writer.Close(); !errors.Is(err, want) || sink.calls != 1 { + t.Fatal("close retried failed output", err, sink.calls) + } + }) + } + + t.Run("successful flush", func(t *testing.T) { + var output bytes.Buffer + writer := normalization.NewWriter(t.Context(), &output, normalization.NFC) + if n, err := writer.Write([]byte(prefix)); n != prefixBytes || err != nil { + t.Fatal(n, err) + } + if n, err := writer.Write([]byte("b")); n != 1 || err != nil { + t.Fatal(n, err) + } + if err := writer.Close(); err != nil || output.String() != prefix+"b" { + t.Fatal("successful flush lost current input", err, output.Len()) + } + }) +} + func (w *failingWriter) Write([]byte) (int, error) { w.calls++ return 0, w.err diff --git a/internal/normalization/reuse.go b/internal/normalization/reuse.go new file mode 100644 index 0000000..31dcbbb --- /dev/null +++ b/internal/normalization/reuse.go @@ -0,0 +1,74 @@ +package normalization + +import ( + "bytes" + "context" + "strings" + "unicode/utf8" + + "golang.org/x/text/unicode/norm" +) + +// Normalizer reuses bounded scratch storage across independent text runs, such +// as runs separated by character-map replacements. Its zero value is ready to +// use. It must not be used concurrently; emitted strings remain owned by callers. +type Normalizer struct { + iterator *norm.Iter + buffer []byte +} + +// Each has Form.Each's exact normalization and cancellation semantics. Every +// call starts a fresh normalization boundary, preserving mapped text verbatim. +func (n *Normalizer) Each(ctx context.Context, form Form, s string, emit func(string) error) error { + defer func() { + if n.iterator != nil { + // Iter holds a slice of its source. Keep scratch storage reusable + // without pinning a larger backing string after the call returns. + *n.iterator = norm.Iter{} + } + }() + + return form.segments(ctx, s, func(source string) error { + if len(source) > segmentChunkBytes+utf8.UTFMax { + result, err := form.longSegment(ctx, source) + if err != nil { + return err + } + + return emit(result) + } + + // Use the same normalized-prefix boundary as Form.String. Iterating + // already normalized ASCII one rune at a time is much more expensive. + prefix := norm.Form(form).QuickSpanString(source) + if prefix == len(source) { + return emit(source) + } + + if n.iterator == nil { + n.iterator = &norm.Iter{} + } + + n.iterator.InitString(norm.Form(form), source[prefix:]) + + n.buffer = append(n.buffer[:0], source[:prefix]...) + for !n.iterator.Done() { + n.buffer = append(n.buffer, n.iterator.Next()...) + } + + if bytes.Count(n.buffer, []byte(combiningGraphemeJoiner)) != strings.Count(source, combiningGraphemeJoiner) { + result, err := form.longSegment(ctx, source) + if err != nil { + return err + } + + return emit(result) + } + + if string(n.buffer) == source { + return emit(source) + } + + return emit(string(n.buffer)) + }) +} diff --git a/internal/normalization/reuse_test.go b/internal/normalization/reuse_test.go new file mode 100644 index 0000000..0b4b03b --- /dev/null +++ b/internal/normalization/reuse_test.go @@ -0,0 +1,66 @@ +package normalization_test + +import ( + "context" + "errors" + "math/rand/v2" + "strings" + "testing" + + "github.com/skarm/xmlsec/internal/normalization" +) + +func TestNormalizerIndependentRuns(t *testing.T) { + var normalizer normalization.Normalizer + random := rand.New(rand.NewPCG(17, 43)) + alphabet := []rune("abé\u0300\u0301\u0315\u034f①fi\u1100\u1161雪😀") + var retained []string + var expected strings.Builder + for i := range 1000 { + var input strings.Builder + for range random.IntN(80) { + input.WriteRune(alphabet[random.IntN(len(alphabet))]) + } + if i%10 == 0 { + input.WriteString(strings.Repeat("\u0301", 4096)) + } + form := []normalization.Form{normalization.NFC, normalization.NFD, normalization.NFKC, normalization.NFKD}[i%4] + source := input.String() + want, err := normalization.String(t.Context(), form, source) + if err != nil { + t.Fatal(err) + } + var actual strings.Builder + if err := normalizer.Each(t.Context(), form, source, func(part string) error { + retained = append(retained, part) + actual.WriteString(part) + return nil + }); err != nil || actual.String() != want { + t.Fatalf("normalization differs for form %d: %v", form, err) + } + expected.WriteString(want) + } + if strings.Join(retained, "") != expected.String() { + t.Fatal("later normalization overwrote an emitted string") + } +} + +func TestNormalizerErrors(t *testing.T) { + var normalizer normalization.Normalizer + failure := errors.New("sink failure") + calls := 0 + if err := normalizer.Each(t.Context(), normalization.NFC, strings.Repeat("e\u0301", 4096), func(string) error { + calls++ + return failure + }); !errors.Is(err, failure) || calls != 1 { + t.Fatal("emitter failure was not terminal", err, calls) + } + ctx, cancel := context.WithCancel(t.Context()) + cancel() + if err := normalizer.Each(ctx, normalization.NFC, "text", func(string) error { + t.Fatal("canceled normalization emitted text") + return nil + }); !errors.Is(err, context.Canceled) { + t.Fatal(err) + } +} diff --git a/internal/packageversion/version.go b/internal/packageversion/version.go index 20866f3..cc85173 100644 --- a/internal/packageversion/version.go +++ b/internal/packageversion/version.go @@ -7,9 +7,12 @@ import ( "strings" "unicode" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/xmlchar" ) +const keywordTo = "to" + // Version retains arbitrary precision integer portions as decimal strings. type Version struct { // Numbers contains normalized decimal components without leading zeros. Compare @@ -25,7 +28,7 @@ type Version struct { // Parse reads a package version, preserving arbitrarily large numeric components. func Parse(s string) Version { - s = strings.Trim(s, " \t\r\n") + s = strings.Trim(s, lexicon.XMLWhitespace) v := Version{Raw: s} numbers := s @@ -123,13 +126,13 @@ type versionRange struct { // ParseConstraint reads the XSLT version-range syntax; empty selects any version. func ParseConstraint(s string) Constraint { - s = strings.Trim(s, " \t\r\n") + s = strings.Trim(s, lexicon.XMLWhitespace) if s == "" || s == "*" { return Constraint{valid: true, alternatives: []versionRange{{wildcard: true}}} } for _, r := range s { - if unicode.IsSpace(r) && !strings.ContainsRune(" \t\r\n", r) { + if unicode.IsSpace(r) && !strings.ContainsRune(lexicon.XMLWhitespace, r) { return Constraint{} } } @@ -183,18 +186,25 @@ func (c Constraint) Matches(v Version) bool { } func parseVersionRange(part string) (versionRange, bool) { + // The grammar distinguishes a single version form, "to upper", and "lower to upper". + const ( + singleVersionRangeFields = 1 + upperOnlyRangeFields = 2 + closedRangeFields = 3 + ) + fields := strings.Fields(part) r := versionRange{} switch { - case len(fields) == 2 && fields[0] == "to": + case len(fields) == upperOnlyRangeFields && fields[0] == keywordTo: upper, prefix := parseUpperVersion(fields[1]) if !upper.valid { return versionRange{}, false } r.upper, r.prefix = &upper, prefix - case len(fields) == 3 && fields[1] == "to": + case len(fields) == closedRangeFields && fields[1] == keywordTo: lower := Parse(fields[0]) upper, prefix := parseUpperVersion(fields[2]) @@ -203,7 +213,7 @@ func parseVersionRange(part string) (versionRange, bool) { } r.lower, r.upper, r.prefix = &lower, &upper, prefix - case len(fields) == 1: + case len(fields) == singleVersionRangeFields: value := fields[0] if before, ok := strings.CutSuffix(value, "+"); ok { lower := Parse(before) diff --git a/internal/signature/legacy_dsa.go b/internal/signature/legacy_dsa.go index 8382c62..99933b6 100644 --- a/internal/signature/legacy_dsa.go +++ b/internal/signature/legacy_dsa.go @@ -10,6 +10,12 @@ import ( "math/big" ) +const ( + legacyDSAPBits = 1024 + legacyDSAQBits = 160 + primalityRounds = 32 +) + // LegacyDSAKey validates the 1024/160 public-key profile required for // XMLDSig 1.1 DSA-SHA1 verification. It does not authorize signature use. // Sizes are checked before primality tests and exponentiation bound work. @@ -18,7 +24,7 @@ func LegacyDSAKey(k *dsa.PublicKey) error { return Err } - if k.P.Sign() <= 0 || k.Q.Sign() <= 0 || k.P.BitLen() != 1024 || k.Q.BitLen() != 160 { + if k.P.Sign() <= 0 || k.Q.Sign() <= 0 || k.P.BitLen() != legacyDSAPBits || k.Q.BitLen() != legacyDSAQBits { return Err } @@ -27,7 +33,7 @@ func LegacyDSAKey(k *dsa.PublicKey) error { return Err } - if !k.P.ProbablyPrime(32) || !k.Q.ProbablyPrime(32) { + if !k.P.ProbablyPrime(primalityRounds) || !k.Q.ProbablyPrime(primalityRounds) { return Err } @@ -44,6 +50,9 @@ func LegacyDSAKey(k *dsa.PublicKey) error { // legacy DSA form that crypto/x509 can parse but does not marshal. Other key // types retain crypto/x509's validation and encoding. func MarshalPublicKey(key crypto.PublicKey) ([]byte, error) { + // ASN.1 BitString.BitLength counts bits, rather than encoded octets. + const bitsPerOctet = 8 + k, ok := key.(*dsa.PublicKey) if !ok { return x509.MarshalPKIXPublicKey(key) @@ -72,6 +81,6 @@ func MarshalPublicKey(key crypto.PublicKey) ([]byte, error) { Key asn1.BitString }{ Algorithm: pkix.AlgorithmIdentifier{Algorithm: asn1.ObjectIdentifier{1, 2, 840, 10040, 4, 1}, Parameters: asn1.RawValue{FullBytes: parameters}}, - Key: asn1.BitString{Bytes: value, BitLength: len(value) * 8}, + Key: asn1.BitString{Bytes: value, BitLength: len(value) * bitsPerOctet}, }) } diff --git a/internal/signature/signature.go b/internal/signature/signature.go index a191f92..57b2a96 100644 --- a/internal/signature/signature.go +++ b/internal/signature/signature.go @@ -21,11 +21,29 @@ import ( "github.com/skarm/xmlsec/internal/der" "github.com/skarm/xmlsec/internal/ecdsakey" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/nilcheck" ) const ( - namePss = "pss" + maximumECDSASignatureDERBytes = 256 + maximumPSSParametersBytes = 4096 + maximumRSAKeyBits = 16384 + minimumRSAExponent = 3 + minimumRSAKeyBits = 2048 +) + +const ( + // The supported PSS profile encodes exactly hash, MGF and salt fields; + // their context-specific tags are shared by construction and validation. + pssParameterFields = 3 + pssHashTag = 0 + pssMGFTag = 1 + pssSaltTag = 2 + // SHA-256 starts each contiguous SHA-2 signature OID range. SHA-384 and + // SHA-512 use the next two arcs, in the digest dispatch order below. + rsaSHA2FirstArc = 11 + ecdsaSHA2FirstArc = 2 ) // Err reports unsupported or invalid ASN.1 signature. @@ -37,6 +55,10 @@ type SignerProvider interface { Sign(context.Context, io.Reader, []byte, crypto.SignerOpts) ([]byte, error) } +type contextual interface { + sign(context.Context, io.Reader, []byte, crypto.SignerOpts) ([]byte, error) +} + // SignerAdapter bridges a context-aware provider to crypto.Signer. It can be // embedded in an adapter that exposes additional signing capabilities. type SignerAdapter struct { @@ -80,7 +102,7 @@ func MGF1() asn1.ObjectIdentifier { return asn1.ObjectIdentifier{1, 2, 840, 1135 func Key(k crypto.PublicKey) error { switch p := k.(type) { case *rsa.PublicKey: - if p == nil || p.N == nil || p.N.BitLen() < 2048 || p.N.BitLen() > 16384 || p.N.Sign() <= 0 || p.E < 3 || p.E%2 == 0 { + if p == nil || p.N == nil || p.N.BitLen() < minimumRSAKeyBits || p.N.BitLen() > maximumRSAKeyBits || p.N.Sign() <= 0 || p.E < minimumRSAExponent || p.E%2 == 0 { return Err } case *ecdsa.PublicKey: @@ -126,7 +148,7 @@ func DigestAlgorithm(h crypto.Hash) (pkix.AlgorithmIdentifier, error) { // NullOrAbsent reports whether ASN.1 parameters are NULL or omitted. func NullOrAbsent(v asn1.RawValue) bool { - return len(v.FullBytes) == 0 && len(v.Bytes) == 0 && v.Tag == 0 || der.Is(v, 0, asn1.TagNull, false) && len(v.Bytes) == 0 + return len(v.FullBytes) == 0 && len(v.Bytes) == 0 && v.Tag == 0 || der.Is(v, asn1.ClassUniversal, asn1.TagNull, false) && len(v.Bytes) == 0 } // Hash decodes a supported digest identifier; SHA-1 requires explicit permission. @@ -163,7 +185,12 @@ func Sum(h crypto.Hash, b []byte) ([]byte, error) { // Algorithm decodes a complete AlgorithmIdentifier. func Algorithm(v asn1.RawValue) (pkix.AlgorithmIdentifier, error) { - f, e := der.Sequence(v, 1, 2) + const ( + minimumFields = 1 + maximumFields = 2 + ) + + f, e := der.Sequence(v, minimumFields, maximumFields) if e != nil { return pkix.AlgorithmIdentifier{}, e } @@ -174,7 +201,7 @@ func Algorithm(v asn1.RawValue) (pkix.AlgorithmIdentifier, error) { } a := pkix.AlgorithmIdentifier{Algorithm: o} - if len(f) == 2 { + if len(f) == maximumFields { a.Parameters = f[1] } @@ -186,7 +213,7 @@ func EncAlgorithm(a pkix.AlgorithmIdentifier) []byte { p := [][]byte{der.OID(a.Algorithm)} if len(a.Parameters.FullBytes) > 0 { p = append(p, a.Parameters.FullBytes) - } else if a.Parameters.Tag != 0 || a.Parameters.Class != 0 || len(a.Parameters.Bytes) > 0 { + } else if a.Parameters.Tag != 0 || a.Parameters.Class != asn1.ClassUniversal || len(a.Parameters.Bytes) > 0 { b, _ := asn1.Marshal(a.Parameters) p = append(p, b) } @@ -218,14 +245,14 @@ func Select(k crypto.PublicKey, h crypto.Hash, pss bool) (pkix.AlgorithmIdentifi if pss { da, _ := DigestAlgorithm(h) d := EncAlgorithm(da) - params := der.Seq(der.Ctx(0, d), der.Ctx(1, der.Seq(der.OID(MGF1()), d)), der.Ctx(2, der.Int(h.Size()))) + params := der.Seq(der.Ctx(pssHashTag, d), der.Ctx(pssMGFTag, der.Seq(der.OID(MGF1()), d)), der.Ctx(pssSaltTag, der.Int(h.Size()))) return pkix.AlgorithmIdentifier{Algorithm: PSS(), Parameters: asn1.RawValue{FullBytes: params}}, nil } - return pkix.AlgorithmIdentifier{Algorithm: asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 11 + i}, Parameters: asn1.NullRawValue}, nil + return pkix.AlgorithmIdentifier{Algorithm: asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, rsaSHA2FirstArc + i}, Parameters: asn1.NullRawValue}, nil case *ecdsa.PublicKey: - return pkix.AlgorithmIdentifier{Algorithm: asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 2 + i}}, nil + return pkix.AlgorithmIdentifier{Algorithm: asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, ecdsaSHA2FirstArc + i}}, nil } return pkix.AlgorithmIdentifier{}, Err @@ -238,18 +265,18 @@ func Details(a pkix.AlgorithmIdentifier) (crypto.Hash, string, error) { if len(raw.FullBytes) > 0 { var e error - raw, e = der.Read(raw.FullBytes, 4096) + raw, e = der.Read(raw.FullBytes, maximumPSSParametersBytes) if e != nil { return 0, "", Err } } - f, e := der.Sequence(raw, 3, 3) + f, e := der.Sequence(raw, pssParameterFields, pssParameterFields) if e != nil { return 0, "", Err } - x, e := der.Explicit(f[0], 0) + x, e := der.Explicit(f[0], pssHashTag) if e != nil { return 0, "", Err } @@ -264,7 +291,7 @@ func Details(a pkix.AlgorithmIdentifier) (crypto.Hash, string, error) { return 0, "", Err } - x, e = der.Explicit(f[1], 1) + x, e = der.Explicit(f[1], pssMGFTag) if e != nil { return 0, "", Err } @@ -284,14 +311,14 @@ func Details(a pkix.AlgorithmIdentifier) (crypto.Hash, string, error) { return 0, "", Err } - x, e = der.Explicit(f[2], 2) + x, e = der.Explicit(f[2], pssSaltTag) var salt int if e != nil || der.Decode(x, &salt) != nil || salt != h.Size() { return 0, "", Err } - return h, namePss, nil + return h, lexicon.SignatureFamilyRSAPSS, nil } if returned0, returned1, matched := detailsEntries(&a); matched { @@ -301,24 +328,6 @@ func Details(a pkix.AlgorithmIdentifier) (crypto.Hash, string, error) { return 0, "", Err } -func detailsEntries(a *pkix.AlgorithmIdentifier) (returned0 crypto.Hash, returned1 string, matched bool) { - for i, h := range []crypto.Hash{crypto.SHA256, crypto.SHA384, crypto.SHA512} { - if a.Algorithm.Equal(asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 11 + i}) && NullOrAbsent(a.Parameters) { - return h, "rsa", true - } - - if a.Algorithm.Equal(asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 2 + i}) && len(a.Parameters.FullBytes) == 0 && a.Parameters.Tag == 0 { - return h, "ecdsa", true - } - } - - return -} - -type contextual interface { - sign(context.Context, io.Reader, []byte, crypto.SignerOpts) ([]byte, error) -} - // SignDigest sends the caller context to adapted providers. Plain crypto.Signer // implementations keep their standard signing contract. External providers // receive an owned copy; the standard Ed25519 key reads input without copying it. @@ -360,7 +369,7 @@ func Sign(ctx context.Context, s crypto.Signer, a pkix.AlgorithmIdentifier, mess } var opt crypto.SignerOpts = h - if mode == namePss { + if mode == lexicon.SignatureFamilyRSAPSS { opt = &rsa.PSSOptions{SaltLength: h.Size(), Hash: h} } @@ -418,19 +427,19 @@ func VerifyDigest(k crypto.PublicKey, a pkix.AlgorithmIdentifier, digest, value switch p := k.(type) { case *rsa.PublicKey: switch mode { - case "rsa": + case lexicon.SignatureFamilyRSA: e = rsa.VerifyPKCS1v15(p, h, digest, value) - case namePss: + case lexicon.SignatureFamilyRSAPSS: e = rsa.VerifyPSS(p, h, digest, value, &rsa.PSSOptions{SaltLength: h.Size(), Hash: h}) default: return Err } case *ecdsa.PublicKey: - if mode != "ecdsa" { + if mode != lexicon.SignatureFamilyECDSA { return Err } - if _, e := der.Read(value, 256); e != nil { + if _, e := der.Read(value, maximumECDSASignatureDERBytes); e != nil { return e } @@ -465,3 +474,17 @@ func MatchCertificate(s crypto.Signer, c *x509.Certificate) error { return nil } + +func detailsEntries(a *pkix.AlgorithmIdentifier) (returned0 crypto.Hash, returned1 string, matched bool) { + for i, h := range []crypto.Hash{crypto.SHA256, crypto.SHA384, crypto.SHA512} { + if a.Algorithm.Equal(asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, rsaSHA2FirstArc + i}) && NullOrAbsent(a.Parameters) { + return h, lexicon.SignatureFamilyRSA, true + } + + if a.Algorithm.Equal(asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, ecdsaSHA2FirstArc + i}) && len(a.Parameters.FullBytes) == 0 && a.Parameters.Tag == 0 { + return h, lexicon.SignatureFamilyECDSA, true + } + } + + return +} diff --git a/internal/streamio/base64.go b/internal/streamio/base64.go index 6f855cd..2593da1 100644 --- a/internal/streamio/base64.go +++ b/internal/streamio/base64.go @@ -20,6 +20,8 @@ type base64PaddingReader struct { } func (r *base64PaddingReader) Read(dst []byte) (int, error) { + const maximumBase64Padding = 2 + n, err := r.source.Read(dst) if n < 0 || n > len(dst) { return 0, errors.Join(io.ErrShortBuffer, err) @@ -32,14 +34,14 @@ func (r *base64PaddingReader) Read(dst []byte) (int, error) { continue } - if c == '=' { + if c == byte(base64.StdPadding) { r.padding++ } else if r.padding != 0 { invalid = i break } - if r.padding > 2 { + if r.padding > maximumBase64Padding { invalid = i break } diff --git a/internal/streamio/reader.go b/internal/streamio/reader.go index 52860a5..0b6cf4c 100644 --- a/internal/streamio/reader.go +++ b/internal/streamio/reader.go @@ -5,17 +5,34 @@ import ( "io" ) +const ( + // Caps one copy or string-write chunk and the retained scratch buffer, in bytes. + copyChunkBytes = 32 << 10 + initialReadBufferBytes = 4 << 10 + maximumStalledReads = 100 + whitespaceReadBufferBytes = 4096 +) + // Reader checks cancellation and a byte limit without retaining input. // At the limit it reads one sentinel byte to distinguish exact EOF from overflow. // It never closes the caller-owned source. type Reader struct { - Context context.Context - Source io.Reader - Limit int64 - ReadBytes int64 + // Context supplies cancellation and must be non-nil. + Context context.Context + // Source is the caller-owned reader; this wrapper never closes it. + Source io.Reader + // Limit is the maximum number of input bytes to return. The overflow probe is + // not returned. + Limit int64 + // ReadBytes counts bytes returned from Source within the limit. + ReadBytes int64 + // LimitError is returned when the sentinel read detects input beyond Limit. LimitError error - stalled int - readErr error + // stalled counts consecutive reads that return neither data nor an error. + stalled int + // readErr retains a terminal source error so later reads do not resume the + // source. + readErr error } // Read rejects invalid reader counts and repeated reads that make no progress. @@ -72,11 +89,11 @@ func (r *Reader) WriteTo(dst io.Writer) (int64, error) { return 0, err } - size := 4 << 10 + size := initialReadBufferBytes source, resident := r.Source.(interface{ Len() int }) if resident { - size = max(1, min(32<<10, source.Len())) + size = max(1, min(copyChunkBytes, source.Len())) } buffer := make([]byte, size) @@ -109,13 +126,13 @@ func (r *Reader) WriteTo(dst io.Writer) (int64, error) { return written, readErr } - if !resident && n == size && size < 32<<10 { + if !resident && n == size && size < copyChunkBytes { // Write 4, 4, 8, 16, then 32 KiB chunks. A 4+32 KiB start // would make bytes.Buffer retain 36 KiB * 2^k instead of // 32 KiB * 2^k when collecting a large opaque stream. - size = int(min(32<<10, written)) + size = int(min(copyChunkBytes, written)) if size > len(buffer) { - buffer = make([]byte, 32<<10) + buffer = make([]byte, copyChunkBytes) } } } @@ -126,7 +143,7 @@ func (r *Reader) progress(n int, err error) error { r.stalled = 0 } else if err == nil { r.stalled++ - if r.stalled >= 100 { + if r.stalled >= maximumStalledReads { err = io.ErrNoProgress } } @@ -141,7 +158,7 @@ func (r *Reader) progress(n int, err error) error { // supplies its own context and byte budget; non-XML whitespace is retained. type XMLWhitespaceReader struct { Source io.Reader - buffer [4096]byte + buffer [whitespaceReadBufferBytes]byte } func (r *XMLWhitespaceReader) Read(dst []byte) (int, error) { @@ -170,7 +187,7 @@ func (r *XMLWhitespaceReader) Read(dst []byte) (int, error) { if n == 0 { stalled++ - if stalled >= 100 { + if stalled >= maximumStalledReads { return 0, io.ErrNoProgress } } else { diff --git a/internal/streamio/writer.go b/internal/streamio/writer.go index 966222e..63d3b60 100644 --- a/internal/streamio/writer.go +++ b/internal/streamio/writer.go @@ -6,15 +6,25 @@ import ( "io" ) +const ( + minimumWriteBufferBytes = 512 +) + // Writer counts successful bytes and rejects writes exceeding an explicit limit. // It never closes the caller-owned destination. type Writer struct { - Context context.Context - Target io.Writer - Limit int64 - Written int64 + // Context supplies cancellation and must be non-nil. + Context context.Context + // Target is the caller-owned destination; this wrapper never closes it. + Target io.Writer + // Limit is the maximum number of bytes permitted in the output. + Limit int64 + // Written counts bytes successfully accepted by Target. + Written int64 + // LimitError is returned before a write would exceed Limit. LimitError error - buffer []byte + // buffer is reusable scratch storage for bounded string writes. + buffer []byte } // Write checks cancellation, bounds and the io.Writer short-write contract. @@ -42,6 +52,8 @@ func (w *Writer) Write(p []byte) (int, error) { // WriteString forwards bounded chunks without allocating a whole byte copy. func (w *Writer) WriteString(s string) (int, error) { + const bufferGrowthFactor = 2 + total := 0 for len(s) > 0 { @@ -49,7 +61,7 @@ func (w *Writer) WriteString(s string) (int, error) { return total, err } - chunk := s[:min(len(s), 32*1024)] + chunk := s[:min(len(s), copyChunkBytes)] if int64(len(chunk)) > w.Limit-w.Written { return total, w.LimitError } @@ -64,7 +76,7 @@ func (w *Writer) WriteString(s string) (int, error) { if len(w.buffer) < len(chunk) { // Most canonical fragments are short. Grow geometrically to retain the // 32 KiB ceiling without allocating it for every small signature digest. - size := min(32*1024, max(512, len(chunk), 2*len(w.buffer))) + size := min(copyChunkBytes, max(minimumWriteBufferBytes, len(chunk), bufferGrowthFactor*len(w.buffer))) w.buffer = make([]byte, size) } diff --git a/internal/streamio/writer_test.go b/internal/streamio/writer_test.go index d841e22..9af2625 100644 --- a/internal/streamio/writer_test.go +++ b/internal/streamio/writer_test.go @@ -228,27 +228,6 @@ func TestReaderWriteToErrors(t *testing.T) { } } -type stringSink struct { - bytes.Buffer - stringWrites int -} - -func (s *stringSink) WriteString(value string) (int, error) { - s.stringWrites++ - return s.Buffer.WriteString(value) -} - -type countWriter struct { - n int - err error -} - -func (w countWriter) Write([]byte) (int, error) { return w.n, w.err } - -type inputFunc func([]byte) (int, error) - -func (f inputFunc) Read(dst []byte) (int, error) { return f(dst) } - // BenchmarkReaderWriteTo separates resident buffers from opaque streams such // as canonicalization and decoder readers. Fixtures are outside the timed loop. func BenchmarkReaderWriteTo(b *testing.B) { @@ -292,3 +271,24 @@ func BenchmarkReaderCollection(b *testing.B) { }) } } + +type stringSink struct { + bytes.Buffer + stringWrites int +} + +func (s *stringSink) WriteString(value string) (int, error) { + s.stringWrites++ + return s.Buffer.WriteString(value) +} + +type countWriter struct { + n int + err error +} + +func (w countWriter) Write([]byte) (int, error) { return w.n, w.err } + +type inputFunc func([]byte) (int, error) + +func (f inputFunc) Read(dst []byte) (int, error) { return f(dst) } diff --git a/internal/testutil/files.go b/internal/testutil/files.go index 6608dd4..32c0b0d 100644 --- a/internal/testutil/files.go +++ b/internal/testutil/files.go @@ -7,12 +7,16 @@ import ( "testing" ) +const ( + fixtureFileMode = 0o600 +) + // WriteFile writes a fixture into an existing directory and returns its path. func WriteFile(t testing.TB, dir, name string, contents []byte) string { t.Helper() path := filepath.Join(dir, name) - if err := os.WriteFile(path, contents, 0o600); err != nil { + if err := os.WriteFile(path, contents, fixtureFileMode); err != nil { t.Fatal(err) } diff --git a/internal/testutil/fips.go b/internal/testutil/fips.go index fb3542e..c404223 100644 --- a/internal/testutil/fips.go +++ b/internal/testutil/fips.go @@ -8,6 +8,17 @@ import ( "testing" ) +const ( + childSingleRunFlag = "-test.count=1" + fipsRuntimeOnly = "fips140=only" +) + +const ( + runtimeDebugEnv = "GODEBUG" + fipsChildEnv = "XMLSEC_TEST_FIPS_CHILD" + fipsChildEnabled = "1" +) + // RunFIPSTest runs the current test in a FIPS-only child when FIPS is disabled. // It returns true when the caller should execute its FIPS assertions locally; // false means those assertions already passed in the child process. @@ -18,7 +29,7 @@ func RunFIPSTest(t *testing.T) bool { return true } - if os.Getenv("XMLSEC_TEST_FIPS_CHILD") == "1" { + if os.Getenv(fipsChildEnv) == fipsChildEnabled { t.Fatal("FIPS is disabled in the FIPS-only test process") } @@ -27,9 +38,9 @@ func RunFIPSTest(t *testing.T) bool { t.Fatal(err) } - command := exec.CommandContext(t.Context(), executable, "-test.run=^"+regexp.QuoteMeta(t.Name())+"$", "-test.count=1") // #nosec G204 -- Reruns this test binary and exact test name without a shell. + command := exec.CommandContext(t.Context(), executable, "-test.run=^"+regexp.QuoteMeta(t.Name())+"$", childSingleRunFlag) // #nosec G204 -- Reruns this test binary and exact test name without a shell. - command.Env = append(command.Environ(), "GODEBUG="+os.Getenv("GODEBUG")+",fips140=only", "XMLSEC_TEST_FIPS_CHILD=1") + command.Env = append(command.Environ(), runtimeDebugEnv+"="+os.Getenv(runtimeDebugEnv)+","+fipsRuntimeOnly, fipsChildEnv+"="+fipsChildEnabled) if output, err := command.CombinedOutput(); err != nil { t.Fatalf("FIPS-only test failed: %v\n%s", err, output) diff --git a/internal/testutil/keys.go b/internal/testutil/keys.go index 596117f..82cec11 100644 --- a/internal/testutil/keys.go +++ b/internal/testutil/keys.go @@ -6,6 +6,8 @@ import ( "encoding/pem" "os" "testing" + + "github.com/skarm/xmlsec/internal/lexicon" ) // WritePrivateKey writes a disposable test key as PKCS#8 PEM. @@ -17,7 +19,7 @@ func WritePrivateKey(t testing.TB, dir, name string, key crypto.Signer) string { t.Fatal(err) } - return WriteFile(t, dir, name, pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: der})) + return WriteFile(t, dir, name, pem.EncodeToMemory(&pem.Block{Type: lexicon.PEMPrivateKey, Bytes: der})) } // WritePublicKey writes a disposable test public key as PKIX PEM. @@ -29,7 +31,7 @@ func WritePublicKey(t testing.TB, dir, name string, key crypto.PublicKey) string t.Fatal(err) } - return WriteFile(t, dir, name, pem.EncodeToMemory(&pem.Block{Type: "PUBLIC KEY", Bytes: der})) + return WriteFile(t, dir, name, pem.EncodeToMemory(&pem.Block{Type: lexicon.PEMPublicKey, Bytes: der})) } // ReadPublicKey reads a PKIX public key from a PEM fixture. diff --git a/internal/testutil/testcert/fixtures.go b/internal/testutil/testcert/fixtures.go index 63d2e10..2e321a7 100644 --- a/internal/testutil/testcert/fixtures.go +++ b/internal/testutil/testcert/fixtures.go @@ -9,6 +9,10 @@ import ( "time" ) +const ( + fixtureManifestFileName = "manifest.json" +) + // PublicFixture contains checked-in public evidence, never private keys. // All fuzz processes use the same certificates and validation time. type PublicFixture struct { @@ -23,7 +27,7 @@ type PublicFixture struct { func LoadFixture(t testing.TB, base, name string) PublicFixture { t.Helper() - data, err := os.ReadFile(filepath.Join(base, "manifest.json")) // #nosec G304 -- Go tests supply the fixture directory. + data, err := os.ReadFile(filepath.Join(base, fixtureManifestFileName)) // #nosec G304 -- Go tests supply the fixture directory. if err != nil { t.Fatal(err) } diff --git a/internal/testutil/testcert/testcert.go b/internal/testutil/testcert/testcert.go index 318b64d..f22e264 100644 --- a/internal/testutil/testcert/testcert.go +++ b/internal/testutil/testcert/testcert.go @@ -16,6 +16,15 @@ import ( "github.com/skarm/xmlsec/internal/signature" ) +const ( + fixtureCRLBackdate = time.Minute + fixtureCRLLifetime = time.Hour + fixtureLeafValidity = 24 * time.Hour + fixtureMaxCertificateBytes = 1 << 20 + fixtureMaxExtensions = 256 + fixtureRootValidity = 365 * 24 * time.Hour +) + // Chain contains disposable test identities and their private keys. type Chain struct { Root *x509.Certificate @@ -32,14 +41,14 @@ type Chain struct { func New(t testing.TB) Chain { t.Helper() c := Chain{RootKey: Key(t), LeafKey: Key(t), TSAKey: Key(t), ResponderKey: Key(t)} - root := &x509.Certificate{SerialNumber: big.NewInt(1), Subject: pkix.Name{CommonName: "iteration 6 test root"}, NotBefore: Now().Add(-365 * 24 * time.Hour), NotAfter: Now().Add(365 * 24 * time.Hour), IsCA: true, BasicConstraintsValid: true, KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign, SubjectKeyId: []byte{1, 2, 3, 4}} + root := &x509.Certificate{SerialNumber: big.NewInt(1), Subject: pkix.Name{CommonName: "iteration 6 test root"}, NotBefore: Now().Add(-fixtureRootValidity), NotAfter: Now().Add(fixtureRootValidity), IsCA: true, BasicConstraintsValid: true, KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign, SubjectKeyId: []byte{1, 2, 3, 4}} c.Root = Issue(t, root, root, &c.RootKey.PublicKey, c.RootKey) - leaf := &x509.Certificate{SerialNumber: big.NewInt(2), Subject: pkix.Name{CommonName: "test signer"}, NotBefore: Now().Add(-24 * time.Hour), NotAfter: Now().Add(24 * time.Hour), KeyUsage: x509.KeyUsageDigitalSignature, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageCodeSigning}, SubjectKeyId: []byte{5, 6, 7, 8}} + leaf := &x509.Certificate{SerialNumber: big.NewInt(2), Subject: pkix.Name{CommonName: "test signer"}, NotBefore: Now().Add(-fixtureLeafValidity), NotAfter: Now().Add(fixtureLeafValidity), KeyUsage: x509.KeyUsageDigitalSignature, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageCodeSigning}, SubjectKeyId: []byte{5, 6, 7, 8}} c.Leaf = Issue(t, leaf, c.Root, &c.LeafKey.PublicKey, c.RootKey) eku, _ := asn1.Marshal([]asn1.ObjectIdentifier{{1, 3, 6, 1, 5, 5, 7, 3, 8}}) - tsa := &x509.Certificate{SerialNumber: big.NewInt(3), Subject: pkix.Name{CommonName: "test TSA"}, NotBefore: Now().Add(-24 * time.Hour), NotAfter: Now().Add(24 * time.Hour), KeyUsage: x509.KeyUsageDigitalSignature, ExtraExtensions: []pkix.Extension{{Id: asn1.ObjectIdentifier{2, 5, 29, 37}, Critical: true, Value: eku}}} + tsa := &x509.Certificate{SerialNumber: big.NewInt(3), Subject: pkix.Name{CommonName: "test TSA"}, NotBefore: Now().Add(-fixtureLeafValidity), NotAfter: Now().Add(fixtureLeafValidity), KeyUsage: x509.KeyUsageDigitalSignature, ExtraExtensions: []pkix.Extension{{Id: asn1.ObjectIdentifier{2, 5, 29, 37}, Critical: true, Value: eku}}} c.TSA = Issue(t, tsa, c.Root, &c.TSAKey.PublicKey, c.RootKey) - responder := &x509.Certificate{SerialNumber: big.NewInt(4), Subject: pkix.Name{CommonName: "test responder"}, NotBefore: Now().Add(-24 * time.Hour), NotAfter: Now().Add(24 * time.Hour), KeyUsage: x509.KeyUsageDigitalSignature, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageOCSPSigning}, ExtraExtensions: []pkix.Extension{{Id: asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 48, 1, 5}, Value: []byte{5, 0}}}} + responder := &x509.Certificate{SerialNumber: big.NewInt(4), Subject: pkix.Name{CommonName: "test responder"}, NotBefore: Now().Add(-fixtureLeafValidity), NotAfter: Now().Add(fixtureLeafValidity), KeyUsage: x509.KeyUsageDigitalSignature, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageOCSPSigning}, ExtraExtensions: []pkix.Extension{{Id: asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 48, 1, 5}, Value: []byte{5, 0}}}} c.Responder = Issue(t, responder, c.Root, &c.ResponderKey.PublicKey, c.RootKey) return c @@ -81,7 +90,7 @@ func Issue(t testing.TB, template, parent *x509.Certificate, public crypto.Publi func CRL(t testing.TB, c Chain, revoked ...*x509.Certificate) []byte { t.Helper() - template := &x509.RevocationList{Number: big.NewInt(1), ThisUpdate: Now().Add(-time.Minute), NextUpdate: Now().Add(time.Hour)} + template := &x509.RevocationList{Number: big.NewInt(1), ThisUpdate: Now().Add(-fixtureCRLBackdate), NextUpdate: Now().Add(fixtureCRLLifetime)} for _, cert := range revoked { template.RevokedCertificateEntries = append(template.RevokedCertificateEntries, x509.RevocationListEntry{SerialNumber: cert.SerialNumber, RevocationTime: Now().Add(-time.Hour)}) } @@ -99,7 +108,7 @@ func CRLV1(t testing.TB, c Chain, revoked ...*x509.Certificate) []byte { t.Helper() original := CRL(t, c, revoked...) - value, err := der.Read(original, 1<<20) + value, err := der.Read(original, fixtureMaxCertificateBytes) if err != nil { t.Fatal(err) } @@ -117,7 +126,7 @@ func CRLV1(t testing.TB, c Chain, revoked ...*x509.Certificate) []byte { var parts [][]byte for index, field := range tbs { - if index == 0 || field.Class == 2 && field.Tag == 0 { + if index == 0 || field.Class == asn1.ClassContextSpecific && field.Tag == 0 { continue } @@ -198,7 +207,7 @@ func CertificateEncodings(t testing.TB, c Chain) []CertificateEncoding { var v1 [][]byte for _, field := range body { - if field.Class == 2 && (field.Tag == 0 || field.Tag == 3) { + if field.Class == asn1.ClassContextSpecific && (field.Tag == 0 || field.Tag == 3) { continue } @@ -210,8 +219,8 @@ func CertificateEncodings(t testing.TB, c Chain) []CertificateEncoding { criticalParts := certificateCriticalFalse(t, body) template := *c.Leaf - attribute := der.Seq(der.OID(asn1.ObjectIdentifier{2, 5, 4, 3}), der.Wrap(0, asn1.TagUTF8String, false, []byte("relative partition"))) - point := der.Seq(der.Ctx(0, der.Wrap(2, 1, true, attribute))) + attribute := der.Seq(der.OID(asn1.ObjectIdentifier{2, 5, 4, 3}), der.Wrap(asn1.ClassUniversal, asn1.TagUTF8String, false, []byte("relative partition"))) + point := der.Seq(der.Ctx(0, der.Wrap(asn1.ClassContextSpecific, 1, true, attribute))) template.ExtraExtensions = []pkix.Extension{{Id: asn1.ObjectIdentifier{2, 5, 29, 31}, Value: der.Seq(point)}} relative, err := x509.CreateCertificate(rand.Reader, &template, c.Root, &c.LeafKey.PublicKey, c.RootKey) @@ -238,7 +247,7 @@ func certificateCriticalFalse(t testing.TB, body []asn1.RawValue) [][]byte { for i, field := range body { criticalParts[i] = field.FullBytes - if field.Class != 2 || field.Tag != 3 { + if field.Class != asn1.ClassContextSpecific || field.Tag != 3 { continue } @@ -247,7 +256,7 @@ func certificateCriticalFalse(t testing.TB, body []asn1.RawValue) [][]byte { t.Fatal(err) } - extensions, err := der.Sequence(v, 1, 256) + extensions, err := der.Sequence(v, 1, fixtureMaxExtensions) if err != nil { t.Fatal(err) } diff --git a/internal/transformxml/transforms.go b/internal/transformxml/transforms.go index ddee396..1b34aec 100644 --- a/internal/transformxml/transforms.go +++ b/internal/transformxml/transforms.go @@ -17,17 +17,27 @@ import ( "github.com/skarm/xmlsec/reference" ) +const ( + attrFilter = "Filter" + maximumInclusivePrefixes = 128 +) + +const ( + nameXPath = "XPath" + prefixXPathFilter = "xf" +) + const Namespace = lexicon.NamespaceDSig func ParseProgram(ctx context.Context, n *tree.Node, doc *document.Document) (reference.Transform, error) { var t reference.Transform - children, err := structure(n, lexicon.DSigElementTransform, "Algorithm") + children, err := structure(n, lexicon.DSigElementTransform, lexicon.SecurityAttrAlgorithm) if err != nil { return t, err } - t.Algorithm, err = requiredAttribute(n, "Algorithm") + t.Algorithm, err = requiredAttribute(n, lexicon.SecurityAttrAlgorithm) if err != nil { return t, err } @@ -60,16 +70,16 @@ func AppendProgram(ctx context.Context, n *tree.Node, t reference.Transform) err } for _, x := range t.XPath { - ns, prefix := Namespace, "ds" + ns, prefix := Namespace, lexicon.PrefixDSig if t.Algorithm == reference.XPathFilter2 { - ns, prefix = reference.XPathFilter2, "xf" + ns, prefix = reference.XPathFilter2, prefixXPathFilter } if u, ok := x.Namespaces[prefix]; ok && u != ns { return fmt.Errorf("%w: reserved transform prefix %s", reference.ErrPolicy, prefix) } - child := &tree.Node{Kind: tree.ElementNode, Name: document.Name{Space: ns, Local: "XPath"}, Prefix: prefix} + child := &tree.Node{Kind: tree.ElementNode, Name: document.Name{Space: ns, Local: nameXPath}, Prefix: prefix} bindings := make(map[string]string, len(x.Namespaces)+1) maps.Copy(bindings, x.Namespaces) bindings[prefix] = ns @@ -82,13 +92,13 @@ func AppendProgram(ctx context.Context, n *tree.Node, t reference.Transform) err sort.Strings(keys) for _, p := range keys { - if p != "xml" { + if p != lexicon.PrefixXML { child.NS = append(child.NS, tree.Namespace{Prefix: p, URI: bindings[p]}) } } if x.Filter != "" { - setAttr(child, "Filter", x.Filter) + setAttr(child, attrFilter, x.Filter) } text(child, x.Expression) @@ -99,12 +109,12 @@ func AppendProgram(ctx context.Context, n *tree.Node, t reference.Transform) err } func ParseCanonical(n *tree.Node, local string) (reference.Transform, error) { - children, err := structure(n, local, "Algorithm") + children, err := structure(n, local, lexicon.SecurityAttrAlgorithm) if err != nil { return reference.Transform{}, err } - uri, err := requiredAttribute(n, "Algorithm") + uri, err := requiredAttribute(n, lexicon.SecurityAttrAlgorithm) if err != nil { return reference.Transform{}, err } @@ -121,12 +131,12 @@ func ParseCanonical(n *tree.Node, local string) (reference.Transform, error) { return reference.Transform{}, fmt.Errorf("%w: canonicalization parameters", reference.ErrInput) } - if len(children) != 1 || children[0].Name != (document.Name{Space: c14n.Exclusive, Local: "InclusiveNamespaces"}) { + if len(children) != 1 || children[0].Name != (document.Name{Space: c14n.Exclusive, Local: lexicon.C14NElementInclusiveNamespaces}) { return reference.Transform{}, fmt.Errorf("%w: canonicalization parameters", reference.ErrInput) } child := children[0] - if err := attributes(child, "PrefixList"); err != nil { + if err := attributes(child, lexicon.C14NAttrPrefixList); err != nil { return reference.Transform{}, err } @@ -139,18 +149,18 @@ func ParseCanonical(n *tree.Node, local string) (reference.Transform, error) { return reference.Transform{}, fmt.Errorf("%w: InclusiveNamespaces must be empty", reference.ErrInput) } - list, err := requiredAttribute(child, "PrefixList") + list, err := requiredAttribute(child, lexicon.C14NAttrPrefixList) if err != nil { return reference.Transform{}, err } prefixes := strings.FieldsFunc(list, func(r rune) bool { return r == ' ' || r == '\t' || r == '\r' || r == '\n' }) - if len(prefixes) > 128 { + if len(prefixes) > maximumInclusivePrefixes { return reference.Transform{}, fmt.Errorf("%w: too many inclusive prefixes", reference.ErrLimit) } for _, p := range prefixes { - if p != "#default" && (!tree.NCName(p) || p == "xmlns") { + if p != lexicon.C14NDefaultPrefixToken && (!tree.NCName(p) || p == lexicon.PrefixXMLNS) { return reference.Transform{}, fmt.Errorf("%w: inclusive prefix", reference.ErrInput) } } @@ -168,7 +178,7 @@ func Parse(ctx context.Context, doc *document.Document, n *tree.Node) (reference return reference.Transform{}, err } - algorithm, err := requiredAttribute(n, "Algorithm") + algorithm, err := requiredAttribute(n, lexicon.SecurityAttrAlgorithm) if err != nil { return reference.Transform{}, err } @@ -181,7 +191,7 @@ func Parse(ctx context.Context, doc *document.Document, n *tree.Node) (reference return ParseCanonical(n, lexicon.DSigElementTransform) } - children, err := structure(n, lexicon.DSigElementTransform, "Algorithm") + children, err := structure(n, lexicon.DSigElementTransform, lexicon.SecurityAttrAlgorithm) if err != nil { return reference.Transform{}, err } @@ -206,12 +216,12 @@ func AppendParameters(ctx context.Context, n *tree.Node, t reference.Transform) child := &tree.Node{ Kind: tree.ElementNode, - Name: document.Name{Space: c14n.Exclusive, Local: "InclusiveNamespaces"}, - Prefix: "ec", - NS: []tree.Namespace{{Prefix: "ec", URI: c14n.Exclusive}}, + Name: document.Name{Space: c14n.Exclusive, Local: lexicon.C14NElementInclusiveNamespaces}, + Prefix: lexicon.PrefixExclusiveC14N, + NS: []tree.Namespace{{Prefix: lexicon.PrefixExclusiveC14N, URI: c14n.Exclusive}}, } - setAttr(child, "PrefixList", strings.Join(t.InclusivePrefixes, " ")) + setAttr(child, lexicon.C14NAttrPrefixList, strings.Join(t.InclusivePrefixes, " ")) add(n, child) if _, err := ParseCanonical(n, lexicon.DSigElementTransform); err != nil { @@ -231,10 +241,12 @@ func AppendParameters(ctx context.Context, n *tree.Node, t reference.Transform) } func standaloneTransformXML(ctx context.Context, doc *document.Document, n *tree.Node) ([]byte, error) { + const maximumStylesheetXMLBytes = 1 << 20 + d := tree.FromNodes([]*tree.Node{tree.ImportSubtree(n)}, tree.Policy(doc)) var output bytes.Buffer - if _, err := d.WriteXML(ctx, &output, 1<<20); err != nil { + if _, err := d.WriteXML(ctx, &output, maximumStylesheetXMLBytes); err != nil { return nil, err } @@ -267,12 +279,14 @@ func parseTransformContent(ctx context.Context, t *reference.Transform, children } func parseXPathParameter(ctx context.Context, n *tree.Node, doc *document.Document, algorithm string) (reference.XPathExpression, error) { + const maximumXPathTextBytes = 16 << 10 + ns := Namespace if algorithm == reference.XPathFilter2 { ns = reference.XPathFilter2 } - if n.Name != (document.Name{Space: ns, Local: "XPath"}) { + if n.Name != (document.Name{Space: ns, Local: nameXPath}) { return reference.XPathExpression{}, fmt.Errorf("%w: XPath element namespace", reference.ErrInput) } @@ -280,8 +294,8 @@ func parseXPathParameter(ctx context.Context, n *tree.Node, doc *document.Docume var err error if algorithm == reference.XPathFilter2 { - err = attributes(n, "Filter") - x.Filter, _ = getAttr(n, document.Name{Local: "Filter"}) + err = attributes(n, attrFilter) + x.Filter, _ = getAttr(n, document.Name{Local: attrFilter}) } else { err = attributes(n) } @@ -299,7 +313,7 @@ func parseXPathParameter(ctx context.Context, n *tree.Node, doc *document.Docume switch child.Kind { case tree.TextNode: - if len(child.Data) > (16<<10)-length { + if len(child.Data) > maximumXPathTextBytes-length { return x, reference.ErrLimit } diff --git a/internal/tree/c14n.go b/internal/tree/c14n.go index 8be9842..b1f71f5 100644 --- a/internal/tree/c14n.go +++ b/internal/tree/c14n.go @@ -15,6 +15,12 @@ import ( "github.com/skarm/xmlsec/internal/streamio" ) +// Canonical output bytes and the exclusive-prefix vector are independent admission bounds. +const ( + defaultCanonicalBytes = 32 << 20 + maximumInclusivePrefixes = 128 +) + const ( // AlgorithmExclusive is the Exclusive C14N 1.0 algorithm URI. AlgorithmExclusive = "http://www.w3.org/2001/10/xml-exc-c14n#" @@ -27,10 +33,15 @@ const ( // CanonicalOptions sets the private canonicalization algorithm, selection and output // budget. type CanonicalOptions struct { - Algorithm string - Comments bool + // Algorithm selects the canonicalization algorithm URI. + Algorithm string + // Comments includes selected comment nodes in canonical output. + Comments bool + // InclusivePrefixes retains these prefixes for Exclusive C14N; #default selects + // the default namespace. InclusivePrefixes []string - MaxBytes int + // MaxBytes bounds canonical output bytes. Zero selects 32 MiB. + MaxBytes int // Exclude removes a whole subtree for the restricted enveloped profile. // It is not an XPath filter and is not exposed as a general transform API. Exclude *Node @@ -38,10 +49,14 @@ type CanonicalOptions struct { // ExclusiveOptions is retained for the existing signing implementation. type ExclusiveOptions struct { - Comments bool + // Comments includes selected comment nodes in canonical output. + Comments bool + // InclusivePrefixes retains these prefixes in Exclusive C14N output. InclusivePrefixes []string - MaxBytes int - Exclude *Node + // MaxBytes bounds canonical output bytes. Zero selects 32 MiB. + MaxBytes int + // Exclude removes the entire selected subtree for enveloped signatures. + Exclude *Node } // Exclusive writes Exclusive C14N using the private subtree selection options. @@ -85,9 +100,17 @@ func Canonical(ctx context.Context, w io.Writer, d *Document, start *Node, set * return err } + if d.contextual { + if err := canonicalNamespaces(ctx, d.contextSource.children); err != nil { + return err + } + } + var parent *Node if start != nil { parent = start.Parent + } else if d.contextual { + parent = d.root.Parent } writer := canonicalWriter{ @@ -173,14 +196,14 @@ func normalizeCanonicalOptions(opt CanonicalOptions) (CanonicalOptions, error) { } if opt.MaxBytes == 0 { - opt.MaxBytes = 32 << 20 + opt.MaxBytes = defaultCanonicalBytes } if opt.MaxBytes < 0 { return opt, fmt.Errorf("%w: output budget", ErrLimit) } - if len(opt.InclusivePrefixes) > 128 { + if len(opt.InclusivePrefixes) > maximumInclusivePrefixes { return opt, fmt.Errorf("%w: inclusive prefixes", ErrLimit) } @@ -191,7 +214,7 @@ func canonicalInclusivePrefixes(prefixes []string) (map[string]struct{}, error) inclusive := make(map[string]struct{}, len(prefixes)) for _, prefix := range prefixes { - if prefix == "#default" { + if prefix == lexicon.C14NDefaultPrefixToken { prefix = "" } else if !NCName(prefix) || prefix == lexicon.PrefixXMLNS { return nil, fmt.Errorf("%w: inclusive namespace prefix", ErrMalformed) @@ -214,6 +237,14 @@ func (s canonicalSelection) has(n *Node) bool { return false } + if s.doc.contextual { + for parent := s.doc.root.Parent; parent != nil; parent = parent.Parent { + if parent == n { + return false + } + } + } + if s.set != nil { return s.set.Contains(ref(s.doc, n)) } @@ -286,7 +317,7 @@ func (w *canonicalWriter) carriage(value string) { if w.err == nil { w.err = w.escaper.write(&w.out, value, func(c byte) string { if c == '\r' { - return " " + return escapeCarriageReturn } return "" @@ -368,12 +399,12 @@ func (w *canonicalWriter) writeLeaf(n *Node) { w.escape(n.Data, false) case CommentNode: if w.opt.Comments { - w.put("") } case PINode: - w.put("= 6 && (!strings.HasPrefix(prefix, "") { - if err := processDTDDeclaration(&p, prefix, external); err != nil { - return "", err - } - - p.seenDecl, checked = true, true + checked, err = checkDTDInputPrefix(&p, &result, external) + if err != nil { + return "", err } } } @@ -209,11 +206,38 @@ func decodeDTDInput(ctx context.Context, source io.Reader, options ParseOptions, } text := result.String() + + return text, nil +} + +func checkDTDInputPrefix(p *documentParser, result *strings.Builder, external bool) (bool, error) { + prefix := result.String() + // An XML declaration needs "", cannot be sliced as a declaration. + if len(prefix) < 6 { + return false, nil + } + + if !strings.HasPrefix(prefix, "") { + return false, nil + } + + if err := processDTDDeclaration(p, prefix, external); err != nil { + return false, err + } + if external { - return stripTextDeclaration(text) + // A validated TextDecl is not part of replacement text. + result.Reset() } - return text, nil + p.seenDecl = true + + return true, nil } func normalizeDTDLineEndings(text string) string { @@ -272,16 +296,25 @@ func (o ParseOptions) parseDTDText(ctx context.Context, text string) (*Document, for name, entity := range state.entities { if entity.notation != "" { - doc.unparsedEntities[name] = UnparsedEntity{SystemID: entity.systemID, PublicID: entity.publicID, BaseURI: entity.base} + doc.unparsedEntities[name] = UnparsedEntity{SystemID: entity.systemID, PublicID: entity.publicID, BaseURI: entity.base, Notation: entity.notation} } } + if state.info.Name != "" { + state.info.UnparsedEntities = doc.unparsedEntities + doc.dtdInfo = &state.info + } + doc.inputBytes = state.inputBytes return doc, nil } func (o ParseOptions) parseDTDReader(ctx context.Context, source io.Reader) (*Document, error) { + if nilcheck.IsNil(source) { + return nil, ErrMalformed + } + captured := dtdProbeReader{source: source} probe, err := newDocumentReader(ctx, &captured, o.Limits, false, o.AllowSingleByteEncodings, o.AllowRelativeNamespaces) @@ -289,7 +322,7 @@ func (o ParseOptions) parseDTDReader(ctx context.Context, source io.Reader) (*Do return nil, err } - probe.input.dtdProbe, probe.stopAtRoot = true, true + probe.input.dtdProbe, probe.stopAtRoot, probe.compatibleVersions = true, true, true _, probeErr := probe.parse(ctx) if probeErr != nil && !errors.Is(probeErr, errDTDPresent) { @@ -300,7 +333,14 @@ func (o ParseOptions) parseDTDReader(ctx context.Context, source io.Reader) (*Do replay := io.MultiReader(bytes.NewReader(captured.prefix.Bytes()), &captured) if !errors.Is(probeErr, errDTDPresent) { - return parseXMLReader(ctx, replay, o.Limits, false, o.AllowSingleByteEncodings, o.AllowRelativeNamespaces) + parser, err := newDocumentReader(ctx, replay, o.Limits, false, o.AllowSingleByteEncodings, o.AllowRelativeNamespaces) + if err != nil { + return nil, err + } + + parser.compatibleVersions = true + + return finishXMLParse(ctx, &parser) } text, err := decodeDTDInput(ctx, replay, o, false) @@ -372,16 +412,16 @@ func (b *dtdOutput) writeEscaped(text string, attribute bool) error { switch r { case '&': - replacement = "&" + replacement = lexicon.EscapeAmpersand case '<': - replacement = "<" + replacement = lexicon.EscapeLessThan case '>': if !attribute { - replacement = ">" + replacement = lexicon.EscapeGreaterThan } case '"': if attribute { - replacement = """ + replacement = lexicon.EscapeQuote } case '\r': replacement = " " @@ -447,13 +487,13 @@ func dtdPredefined(name string) (string, bool) { switch name { case entityAmp: return "&", true - case "lt": + case entityLt: return "<", true - case "gt": + case entityGt: return ">", true - case "apos": + case entityApos: return "'", true - case "quot": + case entityQuot: return `"`, true default: return "", false @@ -480,10 +520,9 @@ func dtdNameEnd(text string) int { return at } -// dtdMarkupEnd respects quotes, comments and processing instructions. The -// scanner used for a DOCTYPE additionally tracks internal-subset brackets. +// dtdDelimitedEnd finds the end of a leading comment or processing instruction. func dtdDelimitedEnd(text string) (int, bool, error) { - if strings.HasPrefix(text, "") if end < 0 || strings.Contains(text[4:4+end], "--") || strings.HasSuffix(text[4:4+end], "-") { return 0, true, fmt.Errorf("%w: comment", ErrMalformed) @@ -492,7 +531,7 @@ func dtdDelimitedEnd(text string) (int, bool, error) { return end + 7, true, nil } - if strings.HasPrefix(text, "") if end < 0 { return 0, true, ErrMalformed @@ -504,6 +543,8 @@ func dtdDelimitedEnd(text string) (int, bool, error) { return 0, false, nil } +// dtdMarkupEnd respects quotes, comments and processing instructions. The +// DOCTYPE scanner also tracks internal-subset brackets. func dtdMarkupEnd(text string, doctype bool) (int, error) { if end, special, err := dtdDelimitedEnd(text); special { return end, err @@ -588,6 +629,9 @@ func (r *dtdProbeReader) Read(dst []byte) (int, error) { } n, err := r.source.Read(dst) + if n < 0 || n > len(dst) { + return 0, errors.Join(io.ErrShortBuffer, err) + } r.total += n if !r.replay { @@ -611,7 +655,11 @@ func (r *dtdCountingReader) Read(dst []byte) (int, error) { } func processDTDDeclaration(p *documentParser, prefix string, external bool) error { - declaration := []byte(prefix[5 : len(prefix)-2]) + declaration, err := normalizeDTDVersion([]byte(prefix[5 : len(prefix)-2])) + if err != nil { + return err + } + if external { attrs, err := declarationAttributes(declaration) if err != nil { @@ -622,7 +670,7 @@ func processDTDDeclaration(p *documentParser, prefix string, external bool) erro declaration = append([]byte(` version="1.0" `), declaration...) } - if _, err := stripTextDeclaration(prefix); err != nil { + if _, err := stripTextDeclaration(""); err != nil { return err } } @@ -634,6 +682,32 @@ func processDTDDeclaration(p *documentParser, prefix string, external bool) erro return nil } +// XML 1.0 Fifth Edition [26] accepts VersionNum 1.x. The expanded document +// is processed as XML 1.0; this does not enable XML 1.1-only characters/syntax. +func normalizeDTDVersion(declaration []byte) ([]byte, error) { + attrs, err := declarationAttributes(declaration) + if err != nil { + return nil, err + } + + if attrs[0].Name.Local != lexicon.DeclVersion || attrs[0].Name.Space != "" { + return declaration, nil + } + + version := attrs[0].Value + if !strings.HasPrefix(version, "1.") || len(version) < 3 { + return nil, fmt.Errorf("%w: XML 1.x is required", ErrUnsupported) + } + + for _, c := range version[2:] { + if c < '0' || c > '9' { + return nil, fmt.Errorf("%w: XML VersionNum", ErrMalformed) + } + } + + return bytes.Replace(declaration, []byte(version), []byte(lexicon.XMLVersion10), 1), nil +} + type dtdMetadata struct { doc *Document state *dtdState @@ -679,11 +753,11 @@ func (m *dtdMetadata) visit(node *Node) error { if declaration, ok := m.state.attributes[name][attrName]; ok { kind := DTDNoType switch declaration.kind { - case "ID": + case lexicon.TypeID: kind = DTDID - case "IDREF": + case lexicon.TypeIDREF: kind = DTDIDRef - case "IDREFS": + case lexicon.TypeIDREFS: kind = DTDIDRefs } diff --git a/internal/tree/dtd_content.go b/internal/tree/dtd_content.go index a7a2ff4..957e109 100644 --- a/internal/tree/dtd_content.go +++ b/internal/tree/dtd_content.go @@ -78,6 +78,10 @@ func (d *dtdState) attributeReference(text string) (string, string, error) { return "", "", fmt.Errorf("%w: undeclared entity %q", ErrMalformed, name) } + if d.standalone && !d.inDTD && entity.declaredOutside { + return "", "", fmt.Errorf("%w: standalone entity declared outside document entity", ErrMalformed) + } + if entity.external || entity.notation != "" { return "", "", fmt.Errorf("%w: external entity in attribute", ErrMalformed) } @@ -282,11 +286,11 @@ func (p *dtdContentParser) step() error { switch { case strings.HasPrefix(p.text, dtdDoctypeOpen): return p.doctype() - case strings.HasPrefix(p.text, "innerlast`), tree.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + want := []struct { + kind tree.Kind + name string + data string + }{ + {tree.TextNode, "", "first"}, + {tree.CommentNode, "", "one"}, + {tree.PINode, "p", "x"}, + {tree.ElementNode, "a", ""}, + {tree.TextNode, "", "last tail"}, + {tree.CommentNode, "", "two"}, + {tree.PINode, "q", "y"}, + } + nodes := tree.Root(doc).Children + if len(nodes) != len(want) { + t.Fatal("token kinds or text coalescing changed", len(nodes)) + } + for i, node := range nodes { + if node.Kind != want[i].kind || node.Name.Local != want[i].name || node.Data != want[i].data { + t.Fatalf("node %d changed after token reuse: %+v", i, node) + } + } + if got := nodes[3].Children; len(got) != 1 || got[0].Data != "inner" { + t.Fatal("child text changed after the next character token") + } +} + func TestDraftMetadataCloneIsolation(t *testing.T) { root := &tree.Node{Kind: tree.ElementNode, Name: tree.Name{Local: "r"}, Attrs: []tree.Attribute{ {Name: tree.Name{Local: "id"}, Value: "a"}, diff --git a/internal/tree/model.go b/internal/tree/model.go index d8233e8..d0d371b 100644 --- a/internal/tree/model.go +++ b/internal/tree/model.go @@ -11,6 +11,17 @@ import ( "github.com/skarm/xmlsec/internal/xmlchar" ) +// Zero namespace/node-set fields select the same defaults used by DefaultLimits. maximumDepth is the hard recursion ceiling rather than a caller default. +const ( + defaultMaxBytes = 8 << 20 + defaultMaxDepth = 128 + defaultMaxNodes = 100_000 + defaultMaxAttributes = 128 + defaultMaxNamespaces = 256 + defaultMaxNodeSetNodes = 250_000 + maximumDepth = 512 +) + const ( // XMLNamespace is the predefined XML namespace URI. XMLNamespace = lexicon.NamespaceXML @@ -18,7 +29,7 @@ const ( XMLNSNamespace = lexicon.NamespaceXMLNS ) -const xmlSpaceName = "space" +const xmlSpaceName = lexicon.AttrSpace var ( // ErrMalformed reports malformed XML. @@ -50,8 +61,10 @@ type Attribute struct { // Namespace is one lexical namespace declaration. type Namespace struct { + // Prefix is the declared prefix; empty declares the default namespace. Prefix string - URI string + // URI is the bound namespace URI; empty represents an undeclaration. + URI string } // Kind identifies an XML or XPath node kind. @@ -78,15 +91,24 @@ const ( // Node is mutable only inside this module. Public Documents never expose it. type Node struct { + // Kind selects the XML or XPath node representation. Kind Kind SourceLine uint32 // 1-based start-tag line; zero when unavailable - Name Name - Prefix string - Attrs []Attribute - NS []Namespace - Data string - Parent *Node - Children []*Node + // Name is the expanded name for named nodes. + Name Name + // Prefix retains the lexical prefix used in the source or draft. + Prefix string + // Attrs contains non-namespace attributes in stored order. + Attrs []Attribute + // NS contains local namespace declarations, not the complete in-scope bindings. + NS []Namespace + // Data holds text, comment or processing-instruction content. + Data string + // Parent links to the containing node; the document node has no parent. + Parent *Node + // Children contains child nodes in document order. + Children []*Node + // textBuffer accumulates adjacent text during private draft construction. textBuffer *strings.Builder } @@ -103,11 +125,13 @@ func Root(d *Document) *Node { // MaxNodes and MaxAttributesPerElement must be positive. Use DefaultLimits // for a complete policy. type Limits struct { - // MaxBytes bounds XML input bytes before and after decoding. + // MaxBytes bounds XML input bytes before and after decoding. Nonvalidating + // DTD processing also uses it for aggregate entity input and expansion work. MaxBytes int - // MaxDepth bounds element nesting and cannot exceed 512. + // MaxDepth bounds element nesting and DTD entity recursion; it cannot exceed 512. MaxDepth int - // MaxNodes bounds parsed content nodes, excluding expanded namespace and attribute axes. + // MaxNodes bounds parsed content nodes, excluding expanded namespace and + // attribute axes. It also bounds DTD declarations in nonvalidating mode. MaxNodes int // MaxAttributesPerElement bounds attributes and namespace declarations on each start tag. MaxAttributesPerElement int @@ -120,7 +144,7 @@ type Limits struct { // DefaultLimits returns a fresh value; there is no mutable global policy. func DefaultLimits() Limits { - return Limits{MaxBytes: 8 << 20, MaxDepth: 128, MaxNodes: 100_000, MaxAttributesPerElement: 128, MaxInScopeNamespaces: 256, MaxNodeSetNodes: 250_000} + return Limits{MaxBytes: defaultMaxBytes, MaxDepth: defaultMaxDepth, MaxNodes: defaultMaxNodes, MaxAttributesPerElement: defaultMaxAttributes, MaxInScopeNamespaces: defaultMaxNamespaces, MaxNodeSetNodes: defaultMaxNodeSetNodes} } // Policy returns the snapshot resource policy. @@ -136,7 +160,7 @@ func (l Limits) Validate() error { return fmt.Errorf("%w: negative namespace/node-set budget", ErrLimit) } // The recursive canonicalizer must never be handed an unbounded tree. - if l.MaxDepth > 512 { + if l.MaxDepth > maximumDepth { return fmt.Errorf("%w: MaxDepth must be <= 512", ErrLimit) } @@ -145,7 +169,7 @@ func (l Limits) Validate() error { func (l Limits) namespaceLimit() int { if l.MaxInScopeNamespaces == 0 { - return 256 + return defaultMaxNamespaces } return l.MaxInScopeNamespaces @@ -153,7 +177,7 @@ func (l Limits) namespaceLimit() int { func (l Limits) nodeSetLimit() int { if l.MaxNodeSetNodes == 0 { - return 250_000 + return defaultMaxNodeSetNodes } return l.MaxNodeSetNodes @@ -182,6 +206,8 @@ type UnparsedEntity struct { PublicID string // BaseURI is the retrieval base used to resolve the system identifier. BaseURI string + // Notation names the notation associated with this unparsed entity. + Notation string } // Document is an immutable, namespace-aware XML 1.0 snapshot. @@ -194,9 +220,13 @@ type Document struct { limits Limits inputBytes int unparsedEntities map[string]UnparsedEntity + dtdInfo *DocumentType nodeBases map[*Node]string dtdTypes map[dtdAttributeKey]DTDAttributeType views sync.Map + contextual bool + contextSource *Document + contextAnchor *Node } // Unwrap returns the private snapshot and node represented by an immutable handle. @@ -227,7 +257,7 @@ func Clone(ctx context.Context, d *Document) (*Document, error) { return nil, err } - out := &Document{limits: d.limits, inputBytes: d.inputBytes, unparsedEntities: d.unparsedEntities, children: make([]*Node, len(d.children))} + out := &Document{limits: d.limits, inputBytes: d.inputBytes, unparsedEntities: d.unparsedEntities, dtdInfo: d.dtdInfo, children: make([]*Node, len(d.children))} var cp func(*Node, *Node) (*Node, error) @@ -283,6 +313,13 @@ func Clone(ctx context.Context, d *Document) (*Document, error) { } } + if d.contextual && out.root != nil { + out.contextual = true + out.root.Parent = d.root.Parent + out.contextSource = d.contextSource + out.contextAnchor = d.contextAnchor + } + if err := ctx.Err(); err != nil { return nil, err } diff --git a/internal/tree/namespace.go b/internal/tree/namespace.go index 94b77db..6dfa34c 100644 --- a/internal/tree/namespace.go +++ b/internal/tree/namespace.go @@ -68,8 +68,11 @@ type namespaceUndo struct { // records restore shadowed prefixes without copying or rescanning ancestors. // It is operation-local and must not be confused with C14N's rendered bindings. type namespaceContext struct { + // bindings contains source namespace bindings in the current traversal scope. bindings map[string]string - undo []namespaceUndo + // undo records shadowed bindings so pop can restore scope without copying the + // map. + undo []namespaceUndo } func newNamespaceContext(parent *Node) namespaceContext { @@ -92,6 +95,8 @@ func (s *namespaceContext) push(declarations []Namespace) int { } func (s *namespaceContext) pop(mark int) { + // Restore in reverse order because one scope may shadow the same prefix + // more than once. Forward restoration would leave an intermediate binding. for i := len(s.undo) - 1; i >= mark; i-- { u := s.undo[i] if u.existed { diff --git a/internal/tree/nodeset.go b/internal/tree/nodeset.go index 37b29e5..92b9b55 100644 --- a/internal/tree/nodeset.go +++ b/internal/tree/nodeset.go @@ -147,7 +147,7 @@ func (n NodeRef) Parent() NodeRef { return ref(n.doc, n.node) } - if n.node.Parent == nil { + if n.node.Parent == nil || n.doc.contextual && n.node == n.doc.root { return n.doc.Node() } @@ -238,11 +238,36 @@ func (s NodeSet) Nodes() []NodeRef { return out } - axes := make(map[*Node][]NodeRef) + // Pack selected axes into one buffer instead of growing a slice for every + // element. Large selections otherwise allocate several slices per owner. + type axisSpan struct{ start, end, next int } + + axes := make(map[*Node]axisSpan) + axisCount := 0 + + for n := range s.members { + if n.kind == AttributeNode || n.kind == NamespaceNode { + span := axes[n.node] + span.end++ + axes[n.node] = span + axisCount++ + } + } + + axisNodes := make([]NodeRef, axisCount) + + offset := 0 + for node, span := range axes { + axes[node] = axisSpan{start: offset, end: offset + span.end, next: offset} + offset += span.end + } for n := range s.members { if n.kind == AttributeNode || n.kind == NamespaceNode { - axes[n.node] = append(axes[n.node], n) + span := axes[n.node] + axisNodes[span.next] = n + span.next++ + axes[n.node] = span } } @@ -258,7 +283,8 @@ func (s NodeSet) Nodes() []NodeRef { out = append(out, r) } - list := axes[n] + span := axes[n] + list := axisNodes[span.start:span.end] slices.SortFunc(list, func(a, b NodeRef) int { if a.kind != b.kind { if a.kind == NamespaceNode { @@ -528,6 +554,8 @@ func collectNodes(ctx context.Context, d *Document, start *Node) (NodeSet, error var parent *Node if start != nil { parent = start.Parent + } else if d.contextual { + parent = d.root.Parent } scope := newNamespaceContext(parent) diff --git a/internal/tree/normalize_test.go b/internal/tree/normalize_test.go index b233de0..a860972 100644 --- a/internal/tree/normalize_test.go +++ b/internal/tree/normalize_test.go @@ -3,6 +3,8 @@ package tree_test import ( "bytes" "context" + "crypto/sha256" + "encoding/hex" "errors" "io" "os" @@ -136,6 +138,12 @@ func TestUnicodeNormalizationConformance(t *testing.T) { t.Fatal(err) } + // Pin the complete official Unicode 17 corpus, not only a minimum row count. + const expectedSHA256 = "5019ffd530751a741900c849c0e010332f142a3612234639bd200b82138a87db" + if digest := sha256.Sum256(data); hex.EncodeToString(digest[:]) != expectedSHA256 { + t.Fatalf("Unicode 17 normalization corpus SHA-256 mismatch: %x", digest) + } + prefix := "q" + strings.Repeat("\u0315", 31) + "\u0300\u034f" normalizedPrefix := "q\u0300" + strings.Repeat("\u0315", 31) + "\u034f" count := 0 @@ -310,25 +318,6 @@ func TestByteNormalizationBoundaries(t *testing.T) { } } -func assertByteNormalization(t *testing.T, form normalization.Form, input, want string) { - t.Helper() - - source := []byte(input) - - got := form.Bytes(source) - if string(got) != want || string(source) != input { - t.Fatalf("byte normalization form %d: got %U, source %U; want %U", form, []rune(string(got)), []rune(string(source)), []rune(want)) - } - - if input == want && len(source) != 0 && &got[0] != &source[0] { - t.Fatal("already normalized bytes were copied") - } -} - -type normalizationFailWriter struct{} - -func (normalizationFailWriter) Write([]byte) (int, error) { return 0, io.ErrClosedPipe } - func BenchmarkExactNormalizationWriter(b *testing.B) { for _, tc := range []struct { name string @@ -391,6 +380,25 @@ func TestNormalizationASCIICancellationInterval(t *testing.T) { } } +func assertByteNormalization(t *testing.T, form normalization.Form, input, want string) { + t.Helper() + + source := []byte(input) + + got := form.Bytes(source) + if string(got) != want || string(source) != input { + t.Fatalf("byte normalization form %d: got %U, source %U; want %U", form, []rune(string(got)), []rune(string(source)), []rune(want)) + } + + if input == want && len(source) != 0 && &got[0] != &source[0] { + t.Fatal("already normalized bytes were copied") + } +} + +type normalizationFailWriter struct{} + +func (normalizationFailWriter) Write([]byte) (int, error) { return 0, io.ErrClosedPipe } + type normalizationCancelWriter struct{ cancel context.CancelFunc } func (w normalizationCancelWriter) Write(p []byte) (int, error) { w.cancel(); return len(p), nil } diff --git a/internal/tree/parse.go b/internal/tree/parse.go index 1da2931..59c7c61 100644 --- a/internal/tree/parse.go +++ b/internal/tree/parse.go @@ -37,6 +37,7 @@ type documentParser struct { seenDecl bool characterInput bool singleByteEncodings bool + compatibleVersions bool stopAtRoot bool relativeNamespaces bool } @@ -64,7 +65,7 @@ func (p *documentParser) parse(ctx context.Context) (*Document, error) { return nil, fmt.Errorf("%w at byte %d: %w", ErrMalformed, offset, err) } - if _, text := token.(xml.CharData); !text { + if _, text := token.(*xml.CharData); !text { if err := p.flushText(); err != nil { return nil, err } @@ -92,20 +93,20 @@ func (p *documentParser) parse(ctx context.Context) (*Document, error) { func (p *documentParser) consumeToken(token xml.Token, offset int64) error { switch token := token.(type) { - case xml.StartElement: - return p.startElement(token) - case xml.EndElement: - return p.endElement(token) - case xml.CharData: - return p.characterData(token) - case xml.Comment: - if bytes.Contains(token, []byte("--")) || bytes.HasSuffix(token, []byte("-")) { + case *xml.StartElement: + return p.startElement(*token) + case *xml.EndElement: + return p.endElement(*token) + case *xml.CharData: + return p.characterData(*token) + case *xml.Comment: + if bytes.Contains(*token, []byte("--")) || bytes.HasSuffix(*token, []byte("-")) { return fmt.Errorf("%w: invalid comment", ErrMalformed) } - return p.appendNode(&Node{Kind: CommentNode, Data: string(token)}) - case xml.ProcInst: - return p.processingInstruction(token, offset) + return p.appendNode(&Node{Kind: CommentNode, Data: string(*token)}) + case *xml.ProcInst: + return p.processingInstruction(*token, offset) case xml.Directive: return fmt.Errorf("%w: DTD/directives are disabled", ErrUnsupported) } @@ -264,6 +265,15 @@ func (p *documentParser) processingInstruction(instruction xml.ProcInst, offset } func (p *documentParser) declaration(s []byte) error { + if p.compatibleVersions { + normalized, err := normalizeDTDVersion(s) + if err != nil { + return err + } + + s = normalized + } + label, err := validateDeclaration(s) if err != nil { return err @@ -381,7 +391,7 @@ func validateNamespace(p, v string, relativeNamespaces bool) error { if v != "" { u, e := url.Parse(v) - if e != nil || !relativeNamespaces && !u.IsAbs() || strings.ContainsAny(v, " \t\r\n") { + if e != nil || !relativeNamespaces && !u.IsAbs() || strings.ContainsAny(v, lexicon.XMLWhitespace) { return fmt.Errorf("%w: namespace URI must be absolute", ErrUnsupported) } } @@ -487,13 +497,13 @@ func normalizeMarkup(out []byte, offset int) (int, error) { prefix := 0 switch { - case bytes.HasPrefix(tail, []byte("") prefix = 4 case bytes.HasPrefix(tail, []byte("") prefix = 9 - case bytes.HasPrefix(tail, []byte("") prefix = 2 case bytes.HasPrefix(tail, []byte(" 3 { return nil, fmt.Errorf("%w: declaration", ErrMalformed) } diff --git a/internal/tree/parse_options.go b/internal/tree/parse_options.go index dcf1cd7..9c999e6 100644 --- a/internal/tree/parse_options.go +++ b/internal/tree/parse_options.go @@ -33,6 +33,17 @@ func (o ParseOptions) ParseText(ctx context.Context, text string) (*Document, er return o.parseDTDText(ctx, text) } + if o.AllowDTD { + parser, err := newDocumentReader(ctx, strings.NewReader(text), o.Limits, true, false, o.AllowRelativeNamespaces) + if err != nil { + return nil, err + } + + parser.compatibleVersions = true + + return finishXMLParse(ctx, &parser) + } + return parseXMLReader(ctx, strings.NewReader(text), o.Limits, true, false, o.AllowRelativeNamespaces) } diff --git a/internal/tree/read.go b/internal/tree/read.go index e6d56a1..1c434f4 100644 --- a/internal/tree/read.go +++ b/internal/tree/read.go @@ -20,6 +20,12 @@ import ( "golang.org/x/text/encoding/ianaindex" ) +// The source buffer and lexical text chunk have separate roles: storage versus incremental parser delivery. +const ( + inputBufferBytes = 4096 + lexicalTextChunkBytes = 4096 +) + // ParseReader constructs a snapshot incrementally, without retaining source XML. // The snapshot itself occupies memory proportional to its nodes and text. func ParseReader(ctx context.Context, src io.Reader, limits Limits) (*Document, error) { @@ -58,6 +64,10 @@ func parseXMLReader(ctx context.Context, src io.Reader, limits Limits, character return nil, err } + return finishXMLParse(ctx, &parser) +} + +func finishXMLParse(ctx context.Context, parser *documentParser) (*Document, error) { doc, err := parser.parse(ctx) if err != nil { return nil, err @@ -127,7 +137,7 @@ func newXMLInput(ctx context.Context, src io.Reader, maximum int64, tokenLimit i func newXMLInputMode(ctx context.Context, src io.Reader, maximum int64, tokenLimit int, characterInput bool) (*xmlInput, error) { source := &streamio.Reader{Context: ctx, Source: src, Limit: maximum, LimitError: ErrLimit} - r := &xmlInput{source: source, reader: bufio.NewReaderSize(source, 4096), encoding: sourceEncoding{name: nameUTF8}, maximum: maximum, tokenLimit: tokenLimit} + r := &xmlInput{source: source, reader: bufio.NewReaderSize(source, inputBufferBytes), encoding: sourceEncoding{name: nameUTF8}, maximum: maximum, tokenLimit: tokenLimit} prefix, err := r.reader.Peek(4) // A short prefix is usable only at bare EOF; a joined source failure remains fatal. @@ -138,19 +148,19 @@ func newXMLInputMode(ctx context.Context, src io.Reader, maximum int64, tokenLim skip := 0 switch { - case bytes.HasPrefix(prefix, []byte{0xef, 0xbb, 0xbf}): + case bytes.HasPrefix(prefix, []byte(lexicon.BOMUTF8)): r.encoding.bom = true skip = 3 case characterInput: // NUL-containing strings must fail XML character validation rather than // being reinterpreted as UTF-16/32 octets. - case bytes.HasPrefix(prefix, []byte{0, 0, 0xfe, 0xff}), bytes.HasPrefix(prefix, []byte{0xff, 0xfe, 0, 0}): + case bytes.HasPrefix(prefix, []byte(lexicon.BOMUTF32BE)), bytes.HasPrefix(prefix, []byte(lexicon.BOMUTF32LE)): return nil, ErrUnsupported - case bytes.HasPrefix(prefix, []byte{0xfe, 0xff}): + case bytes.HasPrefix(prefix, []byte(lexicon.BOMUTF16BE)): r.encoding = sourceEncoding{lexicon.EncodingUTF16BE, true} r.order = binary.BigEndian skip = 2 - case bytes.HasPrefix(prefix, []byte{0xff, 0xfe}): + case bytes.HasPrefix(prefix, []byte(lexicon.BOMUTF16LE)): r.encoding = sourceEncoding{lexicon.EncodingUTF16LE, true} r.order = binary.LittleEndian skip = 2 @@ -353,7 +363,7 @@ func (r *xmlInput) nextUnit() ([]byte, error) { unit = utf8.AppendRune(unit, value) } - if r.outside && (!markup && len(bytes.Trim(unit, " \t\r\n")) != 0 || bytes.HasPrefix(unit, []byte("= 4096 && unit[len(unit)-1] != '\r' && !inReference, nil + return len(unit) >= lexicalTextChunkBytes && unit[len(unit)-1] != '\r' && !inReference, nil } switch { - case bytes.HasPrefix(unit, []byte("")), nil case bytes.HasPrefix(unit, []byte("= 12 && bytes.HasSuffix(unit, []byte("]]>")), nil - case bytes.HasPrefix(unit, []byte("= 4 && bytes.HasSuffix(unit, []byte("?>")), nil case bytes.HasPrefix(unit, []byte(" 0 && r.count >= r.options.MaxRecords { return ErrLimit } @@ -229,7 +229,7 @@ func inheritRecordAttributes(node *Node) error { for ancestor := node.Parent; ancestor != nil; ancestor = ancestor.Parent { for _, attr := range ancestor.Attrs { - if attr.Name.Space != XMLNamespace || attr.Name.Local != "lang" && attr.Name.Local != xmlSpaceName { + if attr.Name.Space != XMLNamespace || attr.Name.Local != lexicon.AttrLang && attr.Name.Local != xmlSpaceName { continue } diff --git a/internal/tree/reparse.go b/internal/tree/reparse.go index 0b52970..04c7023 100644 --- a/internal/tree/reparse.go +++ b/internal/tree/reparse.go @@ -14,6 +14,10 @@ func Reparse(ctx context.Context, draft *Document, limits Limits) (*Document, er return nil, ErrMalformed } + if draft.contextual { + return reparseContextual(ctx, draft, limits) + } + return ParseProduced(ctx, func(dst io.Writer) error { _, err := draft.WriteXML(ctx, dst, limits.MaxBytes) return err @@ -63,7 +67,7 @@ func ReplaceDraft(ctx context.Context, doc *Document, selected, replacement *Nod imported := replacement original := selected - for original.Parent != nil { + for original != doc.root && original.Parent != nil { if err := ctx.Err(); err != nil { return nil, err } @@ -95,6 +99,9 @@ func ReplaceDraft(ctx context.Context, doc *Document, selected, replacement *Nod } imported.Parent = nil + if doc.contextual { + imported.Parent = doc.root.Parent + } children := append([]*Node(nil), doc.children...) for i, node := range children { @@ -108,7 +115,12 @@ func ReplaceDraft(ctx context.Context, doc *Document, selected, replacement *Nod return nil, err } - return FromNodes(children, doc.limits), nil + out := FromNodes(children, doc.limits) + out.contextual = doc.contextual + out.contextSource = doc.contextSource + out.contextAnchor = doc.contextAnchor + + return out, nil } type snapshotWriter struct { diff --git a/internal/tree/scope.go b/internal/tree/scope.go new file mode 100644 index 0000000..1c9975d --- /dev/null +++ b/internal/tree/scope.go @@ -0,0 +1,109 @@ +package tree + +import ( + "context" + "slices" +) + +// ScopeDocument creates an immutable subtree view while retaining its original +// ancestors for canonicalization. It neither copies nor changes source nodes. +// Selection is restricted to that subtree. Signature verifiers use ContextElement +// for full-source reference/ID validation and pin the exact protocol root. +func ScopeDocument(source *Document, element Element) (*Document, error) { + if source == nil || element.doc != source || !element.Valid() { + return nil, ErrMalformed + } + + origin := source + if source.contextual { + origin = source.contextSource + } + + return &Document{root: element.node, children: []*Node{element.node}, limits: source.limits, inputBytes: source.inputBytes, unparsedEntities: source.unparsedEntities, dtdInfo: source.dtdInfo, nodeBases: source.nodeBases, dtdTypes: source.dtdTypes, contextual: true, contextSource: origin, contextAnchor: element.node}, nil +} + +// ContextElement maps a scoped element to its complete source snapshot for +// signature reference resolution, including duplicate IDs in the outer envelope. +// Mapping the context does not broaden signature coverage. +func ContextElement(e Element) Element { + if e.doc == nil || !e.doc.contextual { + return e + } + + return Wrap(e.doc.contextSource, e.node) +} + +func serializationChildren(d *Document) ([]*Node, error) { + if !d.contextual { + return d.children, nil + } + + for parent := d.root.Parent; parent != nil; parent = parent.Parent { + for _, a := range parent.Attrs { + if a.Name.Space == XMLNamespace { + return nil, ErrUnsupported + } + } + } + // Materialize namespace bindings for an independently serialized subtree. + // XML attributes cannot be moved onto a schema-constrained protocol root. + return []*Node{ExtractSubtree(d.root)}, nil +} + +func reparseContextual(ctx context.Context, draft *Document, limits Limits) (*Document, error) { + root := *draft.root + root.Parent = nil + branch := &root + + var path []int + + anchor := draft.contextAnchor + + for source := draft.root.Parent; source != nil; source = source.Parent { + parent := *source + parent.Attrs = slices.Clone(source.Attrs) + parent.NS = slices.Clone(source.NS) + parent.Children = slices.Clone(source.Children) + index, found := 0, false + + for i, child := range source.Children { + if child == anchor { + parent.Children[i] = branch + found = true + + break + } + + if child.Kind == ElementNode { + index++ + } + } + + if !found { + return nil, ErrMalformed + } + + path = append(path, index) + parent.Parent = nil + branch.Parent = &parent + branch = &parent + anchor = source + } + + d, err := Reparse(ctx, FromNodes([]*Node{branch}, limits), limits) + if err != nil { + return nil, err + } + + e := d.Root() + for _, p := range slices.Backward(path) { + cs := e.Children() + if p >= len(cs) { + return nil, ErrMalformed + } + + e = cs[p] + } + + return ScopeDocument(d, e) +} diff --git a/internal/tree/scope_test.go b/internal/tree/scope_test.go new file mode 100644 index 0000000..3e7561b --- /dev/null +++ b/internal/tree/scope_test.go @@ -0,0 +1,145 @@ +package tree_test + +import ( + "bytes" + "errors" + "testing" + + "github.com/skarm/xmlsec/c14n" + "github.com/skarm/xmlsec/internal/tree" +) + +func TestScopedCanonicalContextAndReparse(t *testing.T) { + raw := []byte(`old`) + d, err := tree.Parse(t.Context(), raw, tree.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + e := d.Root().Children()[0].Children()[0] + scope, err := tree.ScopeDocument(d, e) + if err != nil { + t.Fatal(err) + } + var want, got bytes.Buffer + if err := c14n.WriteElement(t.Context(), &want, e, c14n.Options{Algorithm: c14n.Canonical10}); err != nil { + t.Fatal(err) + } + if err := c14n.WriteDocument(t.Context(), &got, scope, c14n.Options{Algorithm: c14n.Canonical10}); err != nil { + t.Fatal(err) + } + if !bytes.Equal(want.Bytes(), got.Bytes()) || !bytes.Contains(got.Bytes(), []byte(`xml:lang="ru"`)) { + t.Fatalf("lost inherited context: %s != %s", got.String(), want.String()) + } + set, err := scope.Root().Subtree(t.Context()) + if err != nil { + t.Fatal(err) + } + got.Reset() + if err := c14n.WriteNodeSet(t.Context(), &got, set, c14n.Options{Algorithm: c14n.Canonical10}); err != nil { + t.Fatal(err) + } + if !bytes.Equal(want.Bytes(), got.Bytes()) { + t.Fatal("node-set lost scoped ancestor context") + } + if len(scope.Root().Attributes()) != 1 { + t.Fatal("schema-constrained root gained attributes") + } + if _, err := scope.XML(t.Context()); !errors.Is(err, tree.ErrUnsupported) { + t.Fatal("silently flattened ancestor XML attributes") + } + clone, err := tree.Clone(t.Context(), scope) + if err != nil { + t.Fatal(err) + } + tree.Root(clone).Children[0].Children[0].Data = "new" + published, err := tree.Reparse(t.Context(), clone, tree.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + got.Reset() + if err := c14n.WriteDocument(t.Context(), &got, published, c14n.Options{Algorithm: c14n.Canonical10}); err != nil { + t.Fatal(err) + } + if !bytes.Equal(got.Bytes(), bytes.Replace(want.Bytes(), []byte("old"), []byte("new"), 1)) { + t.Fatal("reparse changed inherited context") + } + after, _ := d.XML(t.Context()) + if !bytes.Equal(after, raw) { + t.Fatal("source snapshot mutated") + } + replacement := &tree.Node{Kind: tree.ElementNode, Name: tree.Name{Space: "urn:protocol", Local: "replacement"}, Prefix: "p"} + draft, err := tree.ReplaceDraft(t.Context(), scope, tree.Root(scope).Children[0], replacement) + if err != nil { + t.Fatal(err) + } + if _, err := tree.Reparse(t.Context(), draft, tree.DefaultLimits()); err != nil { + t.Fatal(err) + } +} + +func TestScopedNodeSelectionAndNamespaceLimits(t *testing.T) { + limits := tree.DefaultLimits() + source, err := tree.Parse(t.Context(), []byte(``), limits) + if err != nil { + t.Fatal(err) + } + scope, err := tree.ScopeDocument(source, source.Root().Children()[0]) + if err != nil { + t.Fatal(err) + } + t.Run("parent stays inside snapshot", func(t *testing.T) { + if !scope.Root().Node().Parent().Same(scope.Node()) { + t.Fatal("scoped root navigates into the outer document") + } + }) + t.Run("all nodes retains namespace context", func(t *testing.T) { + all, err := scope.AllNodes(t.Context()) + if err != nil { + t.Fatal(err) + } + subtree, err := scope.Root().Subtree(t.Context()) + if err != nil { + t.Fatal(err) + } + if all.Len() != subtree.Len()+1 { + t.Fatalf("all nodes = %d; subtree plus document = %d", all.Len(), subtree.Len()+1) + } + for _, node := range subtree.Nodes() { + if !all.Contains(node) { + t.Fatalf("all nodes omitted %s %q", node.Kind(), node.Name().Local) + } + } + for _, node := range all.Nodes() { + if parent := node.Parent(); parent.Valid() && !all.Contains(parent) { + t.Fatal("scoped node has a parent outside the selected snapshot") + } + for _, child := range node.Children() { + if !all.Contains(child) || !child.Parent().Same(node) { + t.Fatal("scoped child and parent axes disagree") + } + } + } + for _, algorithm := range []string{c14n.Exclusive, c14n.Canonical10, c14n.Canonical11} { + var direct, selected bytes.Buffer + if err := c14n.WriteDocument(t.Context(), &direct, scope, c14n.Options{Algorithm: algorithm}); err != nil { + t.Fatal(err) + } + if err := c14n.WriteNodeSet(t.Context(), &selected, all, c14n.Options{Algorithm: algorithm}); err != nil { + t.Fatal(err) + } + if !bytes.Equal(direct.Bytes(), selected.Bytes()) { + t.Fatalf("%s canonical context changed: %q != %q", algorithm, direct.String(), selected.String()) + } + } + }) + t.Run("namespace budget includes ancestors", func(t *testing.T) { + limits.MaxInScopeNamespaces = 2 // The original scope contains xml, p and q. + if err := tree.CheckSnapshotLimits(t.Context(), scope, limits); !errors.Is(err, tree.ErrLimit) { + t.Fatal("inherited namespaces bypassed snapshot limits", err) + } + limits.MaxInScopeNamespaces = 3 + if err := tree.CheckSnapshotLimits(t.Context(), scope, limits); err != nil { + t.Fatal("exact inherited namespace budget was rejected", err) + } + }) +} diff --git a/internal/tree/stream_xml.go b/internal/tree/stream_xml.go index df3fdb2..2592913 100644 --- a/internal/tree/stream_xml.go +++ b/internal/tree/stream_xml.go @@ -106,7 +106,12 @@ func (d *Document) WriteXML(ctx context.Context, dst io.Writer, maxBytes int) (i return 0, fmt.Errorf("%w: XML output configuration", ErrMalformed) } - return WriteXMLFragment(ctx, dst, d.children, maxBytes) + nodes, err := serializationChildren(d) + if err != nil { + return 0, err + } + + return WriteXMLFragment(ctx, dst, nodes, maxBytes) } type xmlStream struct { @@ -170,7 +175,7 @@ func (w *xmlStream) node(n *Node) { return } - w.text("") case PINode: @@ -183,7 +188,7 @@ func (w *xmlStream) node(n *Node) { return } - w.text("' { end++ } - start := xml.StartElement{Name: tokenName(string(unit[1:end]))} + start := xml.StartElement{Name: tokenName(string(unit[1:end])), Attr: attrs} empty := unit[len(unit)-2] == '/' for at := lexicalSpaceEnd(unit, end); at < len(unit)-1 && unit[at] != '/' && unit[at] != '>'; { @@ -151,13 +178,13 @@ func tokenValue(value []byte, attribute bool) ([]byte, error) { switch entity { case entityAmp: r = '&' - case "lt": + case entityLt: r = '<' - case "gt": + case entityGt: r = '>' - case "apos": + case entityApos: r = '\'' - case "quot": + case entityQuot: r = '"' default: if !strings.HasPrefix(entity, "#") { diff --git a/internal/tree/uri_test.go b/internal/tree/uri_test.go index 7c6a680..e038ac5 100644 --- a/internal/tree/uri_test.go +++ b/internal/tree/uri_test.go @@ -7,16 +7,16 @@ import ( ) const ( - name2 = "../" - name3 = "../../" - name4 = "../../../" - nameYes = "..yes/" - nameYes2 = "/..yes/" - nameYes3 = "/yes/" - nameAB = "a/b/" - uriHTTPABCDPQ = "http://a/b/c/d;p?q" - uriHTTPAG = "http://a/g" - nameYes4 = "yes/" + parentPath = "../" + twoParentsPath = "../../" + threeParentsPath = "../../../" + dotPrefixedRelativePath = "..yes/" + dotPrefixedAbsolutePath = "/..yes/" + absoluteDirectoryPath = "/yes/" + nestedRelativeDirectoryPath = "a/b/" + baseURIWithQuery = "http://a/b/c/d;p?q" + resolvedRootURI = "http://a/g" + relativeDirectoryPath = "yes/" ) // TestRemoveDotSegmentsAppendixA checks the private dot-segment primitive against @@ -32,46 +32,46 @@ func TestRemoveDotSegmentsAppendixA(t *testing.T) { {"no/..//.///pseudo-netpath/seg/file.ext", "pseudo-netpath/seg/file.ext"}, {"yes/no//..//.///pseudo-netpath/seg/file.ext", "yes/pseudo-netpath/seg/file.ext"}, {"no/../yes", "yes"}, - {"no/../yes/", nameYes4}, - {"no/../yes/no/..", nameYes4}, - {"../../no/../..", name4}, - {"no/../..", name2}, + {"no/../yes/", relativeDirectoryPath}, + {"no/../yes/no/..", relativeDirectoryPath}, + {"../../no/../..", threeParentsPath}, + {"no/../..", parentPath}, {"no/..", ""}, {"no/../", ""}, {"/a/b/c/./../../g", "/a/g"}, {"mid/content=5/../6", "mid/6"}, - {"../../..", name4}, - {"no/../../", name2}, + {"../../..", threeParentsPath}, + {"no/../../", parentPath}, {"..yes/..no/..no/..no/../../../..yes", "..yes/..yes"}, {"..yes/..no/..no/..no/../../../..yes/", "..yes/..yes/"}, - {"../..", name3}, - {name4, name4}, + {"../..", twoParentsPath}, + {threeParentsPath, threeParentsPath}, {".", ""}, {"./", ""}, {"./.", ""}, {"//no/..", "/"}, - {"../../no/..", name3}, - {"../../no/../", name3}, - {"yes/no/../", nameYes4}, - {"yes/no/no/../..", nameYes4}, - {"yes/no/no/no/../../..", nameYes4}, + {"../../no/..", twoParentsPath}, + {"../../no/../", twoParentsPath}, + {"yes/no/../", relativeDirectoryPath}, + {"yes/no/no/../..", relativeDirectoryPath}, + {"yes/no/no/no/../../..", relativeDirectoryPath}, {"yes/no/../yes/no/no/../..", "yes/yes/"}, {"yes/no/no/no/../../../yes", "yes/yes"}, {"yes/no/no/no/../../../yes/", "yes/yes/"}, {"/no/../", "/"}, - {"/yes/no/../", nameYes3}, - {"/yes/no/no/../..", nameYes3}, - {"/yes/no/no/no/../../..", nameYes3}, - {"../../..no/..", name3}, - {"../../..no/../", name3}, - {"..yes/..no/../", nameYes}, - {"..yes/..no/..no/../..", nameYes}, - {"..yes/...no/..no/..no/../../..", nameYes}, + {"/yes/no/../", absoluteDirectoryPath}, + {"/yes/no/no/../..", absoluteDirectoryPath}, + {"/yes/no/no/no/../../..", absoluteDirectoryPath}, + {"../../..no/..", twoParentsPath}, + {"../../..no/../", twoParentsPath}, + {"..yes/..no/../", dotPrefixedRelativePath}, + {"..yes/..no/..no/../..", dotPrefixedRelativePath}, + {"..yes/...no/..no/..no/../../..", dotPrefixedRelativePath}, {"..yes/..no/../..yes/..no/..no/../..", "..yes/..yes/"}, {"/..no/../", "/"}, - {"/..yes/..no/../", nameYes2}, - {"/..yes/..no/..no/../..", nameYes2}, - {"/..yes/..no/..no/..no/../../..", nameYes2}, + {"/..yes/..no/../", dotPrefixedAbsolutePath}, + {"/..yes/..no/..no/../..", dotPrefixedAbsolutePath}, + {"/..yes/..no/..no/..no/../../..", dotPrefixedAbsolutePath}, {"/", "/"}, {"/.", "/"}, {"/./", "/"}, @@ -86,8 +86,8 @@ func TestRemoveDotSegmentsAppendixA(t *testing.T) { {"/./..", "/"}, {"/./.././..", "/"}, {"/./.././.././..", "/"}, - {"..", name2}, - {name2, name2}, + {"..", parentPath}, + {parentPath, parentPath}, } for _, tt := range cases { t.Run(tt.in, func(t *testing.T) { @@ -104,26 +104,26 @@ func TestJoinURIReferences(t *testing.T) { ref string want string }{ - {"abc/", name2, ""}, - {name2, name2, name3}, - {"..", "..", name3}, - {uriHTTPABCDPQ, "g", "http://a/b/c/g"}, - {uriHTTPABCDPQ, "./g", "http://a/b/c/g"}, - {uriHTTPABCDPQ, "/g", uriHTTPAG}, - {uriHTTPABCDPQ, "//g", "http://g"}, - {uriHTTPABCDPQ, "?y", "http://a/b/c/d;p?y"}, - {uriHTTPABCDPQ, "#s", uriHTTPABCDPQ}, - {uriHTTPABCDPQ, "g?y#s", "http://a/b/c/g?y"}, - {uriHTTPABCDPQ, "../../../g", uriHTTPAG}, - {"http://a", "g", uriHTTPAG}, - {"http://a/..", "g", uriHTTPAG}, + {"abc/", parentPath, ""}, + {parentPath, parentPath, twoParentsPath}, + {"..", "..", twoParentsPath}, + {baseURIWithQuery, "g", "http://a/b/c/g"}, + {baseURIWithQuery, "./g", "http://a/b/c/g"}, + {baseURIWithQuery, "/g", resolvedRootURI}, + {baseURIWithQuery, "//g", "http://g"}, + {baseURIWithQuery, "?y", "http://a/b/c/d;p?y"}, + {baseURIWithQuery, "#s", baseURIWithQuery}, + {baseURIWithQuery, "g?y#s", "http://a/b/c/g?y"}, + {baseURIWithQuery, "../../../g", resolvedRootURI}, + {"http://a", "g", resolvedRootURI}, + {"http://a/..", "g", resolvedRootURI}, {"foo", "bar", "bar"}, - {nameAB, "%2e%2e/c", "a/b/%2e%2e/c"}, + {nestedRelativeDirectoryPath, "%2e%2e/c", "a/b/%2e%2e/c"}, {"a/b/?x", "?", "a/b/?"}, - {nameAB, "urn:test:thing", "urn:test:thing"}, - {nameAB, "http://z/x//y/../", "http://z/x/"}, + {nestedRelativeDirectoryPath, "urn:test:thing", "urn:test:thing"}, + {nestedRelativeDirectoryPath, "http://z/x//y/../", "http://z/x/"}, {"a/", "//host/x//y", "//host/x/y"}, - {nameAB, "x//y", "a/b/x/y"}, + {nestedRelativeDirectoryPath, "x//y", "a/b/x/y"}, } for _, tt := range cases { t.Run(tt.base+"+"+tt.ref, func(t *testing.T) { diff --git a/internal/tree/write.go b/internal/tree/write.go index 75c140d..a0827a6 100644 --- a/internal/tree/write.go +++ b/internal/tree/write.go @@ -5,6 +5,15 @@ import ( "context" "fmt" "io" + + "github.com/skarm/xmlsec/internal/lexicon" +) + +// Escaping uses bounded scratch storage and preserves the XML profile's exact hexadecimal character-reference spellings. +const ( + xmlEscapeBufferBytes = 4096 + escapeLineFeed = " " + escapeTab = " " ) // XML serializes the snapshot without pretty-printing or changing prefixes. @@ -18,14 +27,20 @@ func (d *Document) XML(ctx context.Context) ([]byte, error) { } var b bytes.Buffer - if _, err := WriteXMLFragment(ctx, &b, d.children, int(^uint(0)>>1)); err != nil { + + nodes, err := serializationChildren(d) + if err != nil { + return nil, err + } + + if _, err := WriteXMLFragment(ctx, &b, nodes, int(^uint(0)>>1)); err != nil { return nil, err } return b.Bytes(), nil } -type xmlEscaper struct{ buffer [4096]byte } +type xmlEscaper struct{ buffer [xmlEscapeBufferBytes]byte } // write replaces bytes into reusable bounded storage. The destination checks // cancellation and limits on every flush, including within one large text node. @@ -76,26 +91,26 @@ func (e *xmlEscaper) write(dst io.Writer, value string, replacement func(byte) s func xmlEscape(c byte, attribute bool) string { switch c { case '&': - return "&" + return lexicon.EscapeAmpersand case '<': - return "<" + return lexicon.EscapeLessThan case '>': if !attribute { - return ">" + return lexicon.EscapeGreaterThan } case '\r': - return " " + return escapeCarriageReturn case '"': if attribute { - return """ + return lexicon.EscapeQuote } case '\t': if attribute { - return " " + return escapeTab } case '\n': if attribute { - return " " + return escapeLineFeed } } diff --git a/internal/uriref/uri.go b/internal/uriref/uri.go index 1345484..f825b87 100644 --- a/internal/uriref/uri.go +++ b/internal/uriref/uri.go @@ -10,6 +10,7 @@ import ( "unicode" "unicode/utf8" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/tree" "github.com/skarm/xmlsec/internal/xmlchar" ) @@ -71,7 +72,7 @@ func Resolve(value string, base *url.URL, resource string, source *tree.Node) (s for _, n := range slices.Backward(ancestors) { for _, attr := range n.Attrs { - if attr.Name != (tree.Name{Space: tree.XMLNamespace, Local: "base"}) { + if attr.Name != (tree.Name{Space: tree.XMLNamespace, Local: lexicon.AttrBase}) { continue } @@ -99,6 +100,8 @@ func Resolve(value string, base *url.URL, resource string, source *tree.Node) (s // parse retains the validated components for callers that also need to resolve // them, avoiding a second net/url parse and its allocations. func parse(value string) (*url.URL, error) { + const escapedOctetDigits = 2 + if strings.Contains(value, "\\") || !utf8.ValidString(value) { return nil, fmt.Errorf("%w: URI encoding", Err) } @@ -109,11 +112,11 @@ func parse(value string) (*url.URL, error) { for i := 0; i < len(value); i++ { if value[i] == '%' { - if i+2 >= len(value) || !xmlchar.IsHexDigit(value[i+1]) || !xmlchar.IsHexDigit(value[i+2]) { + if i+escapedOctetDigits >= len(value) || !xmlchar.IsHexDigit(value[i+1]) || !xmlchar.IsHexDigit(value[i+escapedOctetDigits]) { return nil, fmt.Errorf("%w: malformed URI escape", Err) } - i += 2 + i += escapedOctetDigits } } diff --git a/internal/x509cert/cert.go b/internal/x509cert/cert.go index 54a7c7f..bfe1746 100644 --- a/internal/x509cert/cert.go +++ b/internal/x509cert/cert.go @@ -7,27 +7,68 @@ import ( "crypto/x509/pkix" "encoding/asn1" "errors" + "net" "github.com/skarm/xmlsec/internal/der" + "github.com/skarm/xmlsec/internal/lexicon" +) + +const ( + defaultASN1Depth = 33 + defaultASN1Nodes = 100000 + defaultCertificateBytes = 1 << 20 + maximumDistributionPoints = 256 + maximumExtensionBytes = 1 << 20 + maximumExtensions = 256 + maximumGeneralNames = 256 + relativeDistributionPointViewURI = "urn:xmlsec:private-relative-dp-view" +) + +// X.509 schema bounds and context-specific tags, independent of the parser's +// byte, nesting and node budgets. Version values are the encoded integers. +const ( + certificateFields = 3 + tbsMinimumFields = 6 + tbsMaximumFields = 10 + certificateVersionTag = 0 + certificateExtensionsTag = 3 + certificateVersion1 = 0 + certificateVersion2 = 1 + certificateVersion3 = 2 + extensionMinimumFields = 2 + extensionMaximumFields = 3 +) + +// DistributionPoint and DistributionPointName use separate tagged schemas. +const ( + distributionPointNameTag = 0 + distributionPointReasonsTag = 1 + distributionPointIssuerTag = 2 + distributionPointMaxFields = 3 + pointNameFullTag = 0 + pointNameRelativeTag = 1 ) // Limits bounds the private relative-distribution-point parsing view. type Limits struct { + // Bytes bounds the complete certificate DER encoding in bytes. Bytes int + // Depth bounds ASN.1 nesting during structural validation. Depth int + // Nodes bounds the number of ASN.1 values visited before certificate parsing. Nodes int } // ParseDefault uses the package's bounded 1MiB, 33-level and 100000-value profile. // It borrows raw and performs no authentication or trust decision. func ParseDefault(raw []byte) (*x509.Certificate, error) { - return Parse(raw, Limits{Bytes: 1 << 20, Depth: 33, Nodes: 100000}) + return Parse(raw, Limits{Bytes: defaultCertificateBytes, Depth: defaultASN1Depth, Nodes: defaultASN1Nodes}) } // ParseWithMaxBytes preserves the caller byte bound with shared depth/value caps. // It borrows raw and performs no authentication or trust decision. func ParseWithMaxBytes(raw []byte, maximum int) (*x509.Certificate, error) { - return Parse(raw, Limits{Bytes: maximum, Depth: 33, Nodes: 100000}) + return Parse(raw, Limits{Bytes: maximum, Depth: defaultASN1Depth, Nodes: defaultASN1Nodes}) } // Parse returns a certificate borrowing raw. It preserves every signed byte. @@ -44,16 +85,16 @@ func Parse(raw []byte, limits Limits) (*x509.Certificate, error) { return nil, err } - var outerFields [3]asn1.RawValue + var outerFields [certificateFields]asn1.RawValue - outer, err := checkedSequence(value, 3, outerFields[:]) + outer, err := checkedSequence(value, certificateFields, outerFields[:]) if err != nil { return nil, err } - var bodyFields [10]asn1.RawValue + var bodyFields [tbsMaximumFields]asn1.RawValue - body, err := checkedSequence(outer[0], 6, bodyFields[:]) + body, err := checkedSequence(outer[0], tbsMinimumFields, bodyFields[:]) if err != nil { return nil, err } @@ -75,7 +116,7 @@ func Parse(raw []byte, limits Limits) (*x509.Certificate, error) { parts := make([][]byte, len(body)) for index, field := range body { parts[index] = field.FullBytes - if field.Class != 2 || field.Tag != 3 { + if field.Class != asn1.ClassContextSpecific || field.Tag != certificateExtensionsTag { continue } @@ -95,11 +136,11 @@ func Parse(raw []byte, limits Limits) (*x509.Certificate, error) { } offset := 0 - if body[0].Class == 2 { + if body[0].Class == asn1.ClassContextSpecific { offset++ } - if len(body) < offset+6 { + if len(body) < offset+tbsMinimumFields { return nil, der.ErrInvalidDER } @@ -117,11 +158,11 @@ func Parse(raw []byte, limits Limits) (*x509.Certificate, error) { } func validateCertificateDefaults(body []asn1.RawValue) error { - version, offset := 0, 0 + version, offset := certificateVersion1, 0 - if body[0].Class == 2 && body[0].Tag == 0 { - value, err := checkedExplicit(body[0], 0) - if err != nil || !der.Is(value, 0, asn1.TagInteger, false) || len(value.Bytes) != 1 || value.Bytes[0] < 1 || value.Bytes[0] > 2 { + if body[0].Class == asn1.ClassContextSpecific && body[0].Tag == certificateVersionTag { + value, err := checkedExplicit(body[0], certificateVersionTag) + if err != nil || !der.Is(value, asn1.ClassUniversal, asn1.TagInteger, false) || len(value.Bytes) != 1 || value.Bytes[0] < certificateVersion2 || value.Bytes[0] > certificateVersion3 { return der.ErrInvalidDER } @@ -129,26 +170,26 @@ func validateCertificateDefaults(body []asn1.RawValue) error { offset++ } - if len(body) < offset+6 { + if len(body) < offset+tbsMinimumFields { return der.ErrInvalidDER } previous := 0 - for _, field := range body[offset+6:] { - if field.Class != 2 || field.Tag <= previous || field.Tag > 3 || version == 0 || field.Tag == 3 && version != 2 { + for _, field := range body[offset+tbsMinimumFields:] { + if field.Class != asn1.ClassContextSpecific || field.Tag <= previous || field.Tag > certificateExtensionsTag || version == certificateVersion1 || field.Tag == certificateExtensionsTag && version != certificateVersion3 { return der.ErrInvalidDER } previous = field.Tag - if field.Tag == 3 { - value, err := checkedExplicit(field, 3) - if err != nil || !der.Is(value, 0, asn1.TagSequence, true) || len(value.Bytes) == 0 { + if field.Tag == certificateExtensionsTag { + value, err := checkedExplicit(field, certificateExtensionsTag) + if err != nil || !der.Is(value, asn1.ClassUniversal, asn1.TagSequence, true) || len(value.Bytes) == 0 { return der.ErrInvalidDER } count := 0 for content := value.Bytes; len(content) > 0; count++ { - if count == 256 { + if count == maximumExtensions { return errors.Join(der.ErrInvalidDER, der.ErrLimit) } @@ -166,12 +207,12 @@ func validateCertificateDefaults(body []asn1.RawValue) error { } func relativeExtensions(wrapper asn1.RawValue) ([]pkix.Extension, []byte, bool, error) { - value, err := der.Explicit(wrapper, 3) + value, err := der.Explicit(wrapper, certificateExtensionsTag) if err != nil { return nil, nil, false, err } - fields, err := der.Sequence(value, 1, 256) + fields, err := der.Sequence(value, 1, maximumExtensions) if err != nil { return nil, nil, false, err } @@ -184,7 +225,7 @@ func relativeExtensions(wrapper asn1.RawValue) ([]pkix.Extension, []byte, bool, for index, field := range fields { var extension pkix.Extension - components, err := der.Sequence(field, 2, 3) + components, err := der.Sequence(field, extensionMinimumFields, extensionMaximumFields) if err != nil || der.Decode(field, &extension) != nil { return nil, nil, false, der.ErrInvalidDER } @@ -193,7 +234,7 @@ func relativeExtensions(wrapper asn1.RawValue) ([]pkix.Extension, []byte, bool, return nil, nil, false, der.ErrInvalidDER } - if len(components) == 3 && !extension.Critical { + if len(components) == extensionMaximumFields && !extension.Critical { return nil, nil, false, der.ErrInvalidDER } @@ -224,16 +265,16 @@ func relativeExtensions(wrapper asn1.RawValue) ([]pkix.Extension, []byte, bool, changed = true } - return originals, der.Ctx(3, der.Seq(parts...)), changed, nil + return originals, der.Ctx(certificateExtensionsTag, der.Seq(parts...)), changed, nil } func relativePoints(raw []byte) ([]byte, bool, error) { - value, err := der.Read(raw, 1<<20) + value, err := der.Read(raw, maximumExtensionBytes) if err != nil { return nil, false, err } - points, err := der.Sequence(value, 1, 256) + points, err := der.Sequence(value, 1, maximumDistributionPoints) if err != nil { return nil, false, err } @@ -260,7 +301,7 @@ func generalNames(value asn1.RawValue) error { return der.ErrInvalidDER } - names, err := der.ChildrenLimit(value, 256) + names, err := der.ChildrenLimit(value, maximumGeneralNames) if err != nil || len(names) == 0 { return errors.Join(der.ErrInvalidDER, err) } @@ -275,15 +316,17 @@ func generalNames(value asn1.RawValue) error { } func validateExtensionDefault(extension asn1.RawValue) error { - var scratch [3]asn1.RawValue + const derTrue = 0xff + + var scratch [extensionMaximumFields]asn1.RawValue - fields, err := checkedSequence(extension, 2, scratch[:]) + fields, err := checkedSequence(extension, extensionMinimumFields, scratch[:]) if err != nil { return err } - if len(fields) == 3 { - if !der.Is(fields[1], 0, asn1.TagBoolean, false) || len(fields[1].Bytes) != 1 || fields[1].Bytes[0] != 255 { + if len(fields) == extensionMaximumFields { + if !der.Is(fields[1], asn1.ClassUniversal, asn1.TagBoolean, false) || len(fields[1].Bytes) != 1 || fields[1].Bytes[0] != derTrue { return der.ErrInvalidDER } } @@ -292,9 +335,11 @@ func validateExtensionDefault(extension asn1.RawValue) error { } func relativePoint(point asn1.RawValue) ([]byte, bool, error) { + const reasonFlagCount = 9 + changed := false - fields, err := der.Sequence(point, 1, 3) + fields, err := der.Sequence(point, 1, distributionPointMaxFields) if err != nil { return nil, false, err } @@ -304,7 +349,7 @@ func relativePoint(point asn1.RawValue) ([]byte, bool, error) { named := false for position, field := range fields { - if field.Class != 2 || field.Tag <= previous || field.Tag > 2 { + if field.Class != asn1.ClassContextSpecific || field.Tag <= previous || field.Tag > distributionPointIssuerTag { return nil, false, der.ErrInvalidDER } @@ -312,7 +357,7 @@ func relativePoint(point asn1.RawValue) ([]byte, bool, error) { encoded[position] = field.FullBytes switch field.Tag { - case 0: + case distributionPointNameTag: named = true encoded[position], changed, err = relativePointName(field) @@ -320,12 +365,12 @@ func relativePoint(point asn1.RawValue) ([]byte, bool, error) { return nil, false, err } - case 1: + case distributionPointReasonsTag: var bits asn1.BitString - if field.IsCompound || der.Unmarshal(der.Wrap(0, asn1.TagBitString, false, field.Bytes), &bits) != nil || bits.BitLength > 9 || bits.At(0) != 0 { + if field.IsCompound || der.Unmarshal(der.Wrap(asn1.ClassUniversal, asn1.TagBitString, false, field.Bytes), &bits) != nil || bits.BitLength > reasonFlagCount || bits.At(0) != 0 { return nil, false, der.ErrInvalidDER } - case 2: + case distributionPointIssuerTag: named = true if err := generalNames(field); err != nil { @@ -342,12 +387,12 @@ func relativePoint(point asn1.RawValue) ([]byte, bool, error) { } func generalName(name asn1.RawValue) error { - if name.Class != 2 || name.Tag < 0 || name.Tag > 8 { + if name.Class != asn1.ClassContextSpecific || name.Tag < lexicon.GeneralNameOtherName || name.Tag > lexicon.GeneralNameRegisteredID { return der.ErrInvalidDER } switch name.Tag { - case 0: + case lexicon.GeneralNameOtherName: fields, err := der.Children(name) if err != nil || len(fields) != 2 { return der.ErrInvalidDER @@ -361,25 +406,25 @@ func generalName(name asn1.RawValue) error { if _, err := der.Explicit(fields[1], 0); err != nil { return err } - case 4: + case lexicon.GeneralNameDirectory: var dn pkix.RDNSequence if !name.IsCompound || der.Unmarshal(name.Bytes, &dn) != nil { return der.ErrInvalidDER } - case 3, 5: + case lexicon.GeneralNameX400, lexicon.GeneralNameEDI: if !name.IsCompound { return der.ErrInvalidDER } - case 1, 2, 6: + case lexicon.GeneralNameRFC822, lexicon.GeneralNameDNS, lexicon.GeneralNameURI: return asciiGeneralName(name) - case 7: - if name.IsCompound || len(name.Bytes) != 4 && len(name.Bytes) != 16 { + case lexicon.GeneralNameIP: + if name.IsCompound || len(name.Bytes) != net.IPv4len && len(name.Bytes) != net.IPv6len { return der.ErrInvalidDER } - case 8: + case lexicon.GeneralNameRegisteredID: var oid asn1.ObjectIdentifier - if name.IsCompound || der.Unmarshal(der.Wrap(0, asn1.TagOID, false, name.Bytes), &oid) != nil { + if name.IsCompound || der.Unmarshal(der.Wrap(asn1.ClassUniversal, asn1.TagOID, false, name.Bytes), &oid) != nil { return der.ErrInvalidDER } } @@ -395,12 +440,12 @@ func originalCRLURIs(extensions []pkix.Extension) ([]string, error) { continue } - value, err := der.Read(extension.Value, 1<<20) + value, err := der.Read(extension.Value, maximumExtensionBytes) if err != nil { return nil, err } - points, err := der.Sequence(value, 1, 256) + points, err := der.Sequence(value, 1, maximumDistributionPoints) if err != nil { return nil, err } @@ -419,26 +464,26 @@ func originalCRLURIs(extensions []pkix.Extension) ([]string, error) { } func fullCRLPointURIs(point asn1.RawValue) ([]string, error) { - fields, err := der.Sequence(point, 1, 3) + fields, err := der.Sequence(point, 1, distributionPointMaxFields) if err != nil { return nil, err } for _, field := range fields { - if field.Class != 2 || field.Tag != 0 { + if field.Class != asn1.ClassContextSpecific || field.Tag != distributionPointNameTag { continue } - choice, err := der.Explicit(field, 0) + choice, err := der.Explicit(field, distributionPointNameTag) if err != nil { return nil, err } - if choice.Tag != 0 { + if choice.Tag != pointNameFullTag { return nil, nil } - names, err := der.ChildrenLimit(choice, 256) + names, err := der.ChildrenLimit(choice, maximumGeneralNames) if err != nil { return nil, err } @@ -446,7 +491,7 @@ func fullCRLPointURIs(point asn1.RawValue) ([]string, error) { var uris []string for _, name := range names { - if name.Class == 2 && name.Tag == 6 { + if name.Class == asn1.ClassContextSpecific && name.Tag == lexicon.GeneralNameURI { uris = append(uris, string(name.Bytes)) } } @@ -458,12 +503,12 @@ func fullCRLPointURIs(point asn1.RawValue) ([]string, error) { } func relativePointName(field asn1.RawValue) ([]byte, bool, error) { - name, err := der.Explicit(field, 0) - if err != nil || name.Class != 2 || !name.IsCompound { + name, err := der.Explicit(field, distributionPointNameTag) + if err != nil || name.Class != asn1.ClassContextSpecific || !name.IsCompound { return nil, false, der.ErrInvalidDER } - if name.Tag == 0 { + if name.Tag == pointNameFullTag { if err := generalNames(name); err != nil { return nil, false, err } @@ -471,25 +516,30 @@ func relativePointName(field asn1.RawValue) ([]byte, bool, error) { return field.FullBytes, false, nil } - if name.Tag != 1 { + if name.Tag != pointNameRelativeTag { return nil, false, der.ErrInvalidDER } var rdn pkix.RelativeDistinguishedNameSET - if der.Unmarshal(der.Wrap(0, asn1.TagSet, true, name.Bytes), &rdn) != nil || len(rdn) == 0 { + if der.Unmarshal(der.Wrap(asn1.ClassUniversal, asn1.TagSet, true, name.Bytes), &rdn) != nil || len(rdn) == 0 { return nil, false, der.ErrInvalidDER } - return der.Ctx(0, der.Wrap(2, 0, true, der.Wrap(2, 6, false, []byte("urn:xmlsec:private-relative-dp-view")))), true, nil + return der.Ctx(distributionPointNameTag, der.Wrap(asn1.ClassContextSpecific, pointNameFullTag, true, der.Wrap(asn1.ClassContextSpecific, lexicon.GeneralNameURI, false, []byte(relativeDistributionPointViewURI)))), true, nil } func asciiGeneralName(name asn1.RawValue) error { + const ( + firstPrintableASCII = 0x20 + asciiDelete = 0x7f + ) + if name.IsCompound || len(name.Bytes) == 0 { return der.ErrInvalidDER } for _, b := range name.Bytes { - if b < 32 || b >= 127 { + if b < firstPrintableASCII || b >= asciiDelete { return der.ErrInvalidDER } } diff --git a/internal/x509cert/checked.go b/internal/x509cert/checked.go index 46ec915..68cc4ec 100644 --- a/internal/x509cert/checked.go +++ b/internal/x509cert/checked.go @@ -19,7 +19,7 @@ func checkedSequence(value asn1.RawValue, minimum int, fields []asn1.RawValue) ( } func checkedExplicit(value asn1.RawValue, tag int) (asn1.RawValue, error) { - if !der.Is(value, 2, tag, true) || len(value.Bytes) == 0 { + if !der.Is(value, asn1.ClassContextSpecific, tag, true) || len(value.Bytes) == 0 { return asn1.RawValue{}, der.ErrInvalidDER } diff --git a/internal/x509cert/names_test.go b/internal/x509cert/names_test.go index 4369446..e124b53 100644 --- a/internal/x509cert/names_test.go +++ b/internal/x509cert/names_test.go @@ -14,15 +14,6 @@ import ( "github.com/skarm/xmlsec/internal/testutil/testcert" ) -func parseNameFixture(t *testing.T, encoded []byte) asn1.RawValue { - t.Helper() - value, err := der.Read(encoded, 1<<20) - if err != nil { - t.Fatal("invalid DER fixture", err) - } - return value -} - func TestGeneralNameChoices(t *testing.T) { t.Parallel() @@ -163,3 +154,12 @@ func TestRelativeCRLPointRetainsIssuerAndFullURIs(t *testing.T) { } t.Fatal("distribution-point extension lost") } + +func parseNameFixture(t *testing.T, encoded []byte) asn1.RawValue { + t.Helper() + value, err := der.Read(encoded, 1<<20) + if err != nil { + t.Fatal("invalid DER fixture", err) + } + return value +} diff --git a/internal/x509crl/crl.go b/internal/x509crl/crl.go index b5327a8..a57ae10 100644 --- a/internal/x509crl/crl.go +++ b/internal/x509crl/crl.go @@ -14,25 +14,61 @@ import ( "time" "github.com/skarm/xmlsec/internal/der" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/signature" ) +// CRL versions are encoded ASN.1 integers. Every revoked entry starts with a +// serial number and revocation date; v2 may add entry extensions. +const ( + encodedCRLVersion1 = 0 + encodedCRLVersion2 = 1 + revokedEntryRequiredFields = 2 +) + +const ( + evidenceASN1Depth = 33 + evidenceASN1Nodes = 1000000 + evidenceMaxEntries = 100000 + generalizedTimeBytes = 15 + maximumEntryExtensions = 32 + utcTimeBytes = 13 + utcTimePivotYear = 2050 + yearsPerCentury = 100 +) + +const ( + oidDSAWithSHA1 = "1.2.840.10040.4.3" + oidDSAWithSHA256 = "2.16.840.1.101.3.4.3.2" + oidECDSAWithSHA1 = "1.2.840.10045.4.1" + oidMD5WithRSA = "1.2.840.113549.1.1.4" + oidSHA1WithRSA = "1.2.840.113549.1.1.5" +) + // ErrLimit identifies a configured byte/depth/node/entry budget, not malformed syntax. var ErrLimit = errors.New("CRL decoder resource limit exceeded") // Limits bounds decoding before the standard-library v2 allocation. type Limits struct { - Bytes int - Depth int - Nodes int + // Bytes bounds the complete CRL DER encoding in bytes. + Bytes int + // Depth bounds ASN.1 nesting during structural validation. + Depth int + // Nodes bounds the number of ASN.1 values visited. + Nodes int + // Entries bounds revoked certificate entries before standard-library + // allocation. Entries int } // View preserves the exact signed header while providing the standard X.509 view. // Returned byte slices borrow the input; callers that retain them must own input. type View struct { - CRL *x509.RevocationList - Version int + // CRL is the parsed, unauthenticated standard-library view. + CRL *x509.RevocationList + // Version is the encoded ASN.1 version value; an absent version is zero. + Version int + // Algorithm preserves the signed header's signature algorithm identifier. Algorithm pkix.AlgorithmIdentifier } @@ -48,11 +84,14 @@ type listDecoder struct { // explicit caller byte cap. It preserves original bytes and performs no trust // checks. Keys callers with configured ASN.1/entry budgets use Parse directly. func ParseEvidence(raw []byte, maximum int) (*View, error) { - return Parse(raw, Limits{Bytes: maximum, Depth: 33, Nodes: 1000000, Entries: 100000}) + return Parse(raw, Limits{Bytes: maximum, Depth: evidenceASN1Depth, Nodes: evidenceASN1Nodes, Entries: evidenceMaxEntries}) } // Parse decodes one complete DER v1/v2 list. It never authenticates the result. func Parse(raw []byte, limits Limits) (*View, error) { + // CertificateList contains TBSCertList, signatureAlgorithm and signatureValue. + const certificateListFields = 3 + value, err := der.ReadLimits(raw, limits.Bytes, limits.Depth, limits.Nodes) if err != nil { if errors.Is(err, der.ErrLimit) { @@ -62,7 +101,7 @@ func Parse(raw []byte, limits Limits) (*View, error) { return nil, err } - outer, err := der.Sequence(value, 3, 3) + outer, err := der.Sequence(value, certificateListFields, certificateListFields) if err != nil { return nil, err } @@ -77,7 +116,7 @@ func Parse(raw []byte, limits Limits) (*View, error) { return nil, err } - if decoder.version == 0 { + if decoder.version == encodedCRLVersion1 { err = decoder.decodeV1Entries(entries) } else { decoder.list, err = x509.ParseRevocationList(raw) @@ -91,14 +130,22 @@ func Parse(raw []byte, limits Limits) (*View, error) { } func parseHeader(raw []byte, outer []asn1.RawValue) (*listDecoder, error) { - body, err := der.Sequence(outer[0], 3, 7) + // Signature, issuer and thisUpdate are mandatory. Version, nextUpdate, + // revokedCertificates and extensions account for the optional fields. + const ( + tbsRequiredFields = 3 + tbsMaximumFields = 7 + signatureOctetBits = 8 + ) + + body, err := der.Sequence(outer[0], tbsRequiredFields, tbsMaximumFields) if err != nil { return nil, err } decoder := &listDecoder{body: body} - if der.Is(body[0], 0, asn1.TagInteger, false) { - if der.Decode(body[0], &decoder.version) != nil || decoder.version != 1 { + if der.Is(body[0], asn1.ClassUniversal, asn1.TagInteger, false) { + if der.Decode(body[0], &decoder.version) != nil || decoder.version != encodedCRLVersion2 { return nil, der.ErrInvalidDER } @@ -106,7 +153,7 @@ func parseHeader(raw []byte, outer []asn1.RawValue) (*listDecoder, error) { } index := decoder.index - if len(body) < index+3 || !bytes.Equal(body[index].FullBytes, outer[1].FullBytes) { + if len(body) < index+tbsRequiredFields || !bytes.Equal(body[index].FullBytes, outer[1].FullBytes) { return nil, der.ErrInvalidDER } @@ -116,7 +163,7 @@ func parseHeader(raw []byte, outer []asn1.RawValue) (*listDecoder, error) { } var bits asn1.BitString - if der.Decode(outer[2], &bits) != nil || bits.BitLength%8 != 0 || len(bits.Bytes) == 0 { + if der.Decode(outer[2], &bits) != nil || bits.BitLength%signatureOctetBits != 0 || len(bits.Bytes) == 0 { return nil, der.ErrInvalidDER } @@ -134,10 +181,10 @@ func parseHeader(raw []byte, outer []asn1.RawValue) (*listDecoder, error) { return nil, err } - decoder.index += 3 + decoder.index += tbsRequiredFields index = decoder.index - if index < len(body) && body[index].Class == 0 && (body[index].Tag == asn1.TagUTCTime || body[index].Tag == asn1.TagGeneralizedTime) { + if index < len(body) && body[index].Class == asn1.ClassUniversal && (body[index].Tag == asn1.TagUTCTime || body[index].Tag == asn1.TagGeneralizedTime) { list.NextUpdate, err = parseTime(body[index]) if err != nil { return nil, err @@ -154,7 +201,7 @@ func parseHeader(raw []byte, outer []asn1.RawValue) (*listDecoder, error) { func (d *listDecoder) entries(maximum int) ([]asn1.RawValue, error) { var entries []asn1.RawValue - if d.index < len(d.body) && der.Is(d.body[d.index], 0, asn1.TagSequence, true) { + if d.index < len(d.body) && der.Is(d.body[d.index], asn1.ClassUniversal, asn1.TagSequence, true) { sequence := d.body[d.index] count := 0 // Count before schema checks, preserving entry-budget precedence. @@ -171,7 +218,7 @@ func (d *listDecoder) entries(maximum int) ([]asn1.RawValue, error) { return nil, der.ErrInvalidDER } - if d.version == 0 { + if d.version == encodedCRLVersion1 { entries = make([]asn1.RawValue, 0, count) } @@ -180,7 +227,7 @@ func (d *listDecoder) entries(maximum int) ([]asn1.RawValue, error) { return nil, err } // v2 is decoded by crypto/x509 below. Only v1 needs these views. - if d.version == 0 { + if d.version == encodedCRLVersion1 { entries = append(entries, entry) } } @@ -188,7 +235,7 @@ func (d *listDecoder) entries(maximum int) ([]asn1.RawValue, error) { d.index++ } - if d.version == 1 && d.index < len(d.body) { + if d.version == encodedCRLVersion2 && d.index < len(d.body) { extension, err := der.Explicit(d.body[d.index], 0) if err != nil { return nil, err @@ -210,9 +257,9 @@ func (d *listDecoder) entries(maximum int) ([]asn1.RawValue, error) { func (d *listDecoder) decodeV1Entries(entries []asn1.RawValue) error { for _, entry := range entries { - var scratch [2]asn1.RawValue + var scratch [revokedEntryRequiredFields]asn1.RawValue - fields, err := der.ValidatedSequence(entry, 2, scratch[:]) + fields, err := der.ValidatedSequence(entry, revokedEntryRequiredFields, scratch[:]) if err != nil { return err } @@ -236,9 +283,12 @@ func (d *listDecoder) decodeV1Entries(entries []asn1.RawValue) error { } func validateEntry(entry asn1.RawValue, version int) error { - var scratch [3]asn1.RawValue + // Entry extensions are the third, optional field permitted only in v2. + const maximumRevokedEntryFields = 3 - fields, err := der.ValidatedSequence(entry, 2, scratch[:]) + var scratch [maximumRevokedEntryFields]asn1.RawValue + + fields, err := der.ValidatedSequence(entry, revokedEntryRequiredFields, scratch[:]) if err != nil { return err } @@ -247,8 +297,8 @@ func validateEntry(entry asn1.RawValue, version int) error { return err } - if len(fields) == 3 { - if version == 0 { + if len(fields) == maximumRevokedEntryFields { + if version == encodedCRLVersion1 { return der.ErrInvalidDER } @@ -261,14 +311,20 @@ func validateEntry(entry asn1.RawValue, version int) error { // validateExtensions checks schema-level constraints not covered by generic DER: // the DEFAULT FALSE critical flag must be omitted, and extension IDs are unique. func validateExtensions(value asn1.RawValue) error { - fields, err := der.Sequence(value, 1, 32) + // Extension contains extnID, optional critical, and extnValue. + const ( + extensionMinimumFields = 2 + extensionMaximumFields = 3 + ) + + fields, err := der.Sequence(value, 1, maximumEntryExtensions) if err != nil { return err } seen := make(map[string]struct{}, len(fields)) for _, field := range fields { - parts, err := der.Sequence(field, 2, 3) + parts, err := der.Sequence(field, extensionMinimumFields, extensionMaximumFields) if err != nil { return err } @@ -284,14 +340,14 @@ func validateExtensions(value asn1.RawValue) error { seen[oid.String()] = struct{}{} - if len(parts) == 3 { + if len(parts) == extensionMaximumFields { var critical bool if der.Decode(parts[1], &critical) != nil || !critical { return der.ErrInvalidDER } } - if !der.Is(parts[len(parts)-1], 0, asn1.TagOctetString, false) { + if !der.Is(parts[len(parts)-1], asn1.ClassUniversal, asn1.TagOctetString, false) { return der.ErrInvalidDER } } @@ -300,7 +356,7 @@ func validateExtensions(value asn1.RawValue) error { } func parseTime(value asn1.RawValue) (time.Time, error) { - if value.Class != 0 || value.IsCompound { + if value.Class != asn1.ClassUniversal || value.IsCompound { return time.Time{}, der.ErrInvalidDER } // PKIX requires seconds and a literal Z. Decoding that exact form avoids @@ -309,17 +365,17 @@ func parseTime(value asn1.RawValue) (time.Time, error) { switch value.Tag { case asn1.TagUTCTime: - if len(value.Bytes) != 13 { + if len(value.Bytes) != utcTimeBytes { return time.Time{}, der.ErrInvalidDER } - layout = "060102150405Z" + layout = lexicon.ASN1UTCTimeLayout case asn1.TagGeneralizedTime: - if len(value.Bytes) != 15 { + if len(value.Bytes) != generalizedTimeBytes { return time.Time{}, der.ErrInvalidDER } - layout = "20060102150405Z" + layout = lexicon.ASN1GeneralizedTimeLayout default: return time.Time{}, der.ErrInvalidDER } @@ -330,8 +386,8 @@ func parseTime(value asn1.RawValue) (time.Time, error) { } // ASN.1 uses a 1950 century pivot; time.Parse uses 1969. - if value.Tag == asn1.TagUTCTime && at.Year() >= 2050 { - at = at.AddDate(-100, 0, 0) + if value.Tag == asn1.TagUTCTime && at.Year() >= utcTimePivotYear { + at = at.AddDate(-yearsPerCentury, 0, 0) } return at, nil @@ -345,7 +401,7 @@ func signatureAlgorithm(algorithm pkix.AlgorithmIdentifier) x509.SignatureAlgori hash, mode, err := signature.Details(algorithm) if err == nil { switch mode { - case "rsa": + case lexicon.SignatureFamilyRSA: switch hash { case crypto.SHA256: return x509.SHA256WithRSA @@ -354,7 +410,7 @@ func signatureAlgorithm(algorithm pkix.AlgorithmIdentifier) x509.SignatureAlgori case crypto.SHA512: return x509.SHA512WithRSA } - case "pss": + case lexicon.SignatureFamilyRSAPSS: switch hash { case crypto.SHA256: return x509.SHA256WithRSAPSS @@ -363,7 +419,7 @@ func signatureAlgorithm(algorithm pkix.AlgorithmIdentifier) x509.SignatureAlgori case crypto.SHA512: return x509.SHA512WithRSAPSS } - case "ecdsa": + case lexicon.SignatureFamilyECDSA: switch hash { case crypto.SHA256: return x509.ECDSAWithSHA256 @@ -377,20 +433,20 @@ func signatureAlgorithm(algorithm pkix.AlgorithmIdentifier) x509.SignatureAlgori if signature.NullOrAbsent(algorithm.Parameters) { switch algorithm.Algorithm.String() { - case "1.2.840.113549.1.1.5": + case oidSHA1WithRSA: return x509.SHA1WithRSA - case "1.2.840.113549.1.1.4": + case oidMD5WithRSA: return x509.MD5WithRSA } } if len(algorithm.Parameters.FullBytes) == 0 { switch algorithm.Algorithm.String() { - case "1.2.840.10045.4.1": + case oidECDSAWithSHA1: return x509.ECDSAWithSHA1 - case "1.2.840.10040.4.3": + case oidDSAWithSHA1: return x509.DSAWithSHA1 - case "2.16.840.1.101.3.4.3.2": + case oidDSAWithSHA256: return x509.DSAWithSHA256 } } diff --git a/internal/x509crl/crl_internal_test.go b/internal/x509crl/crl_internal_test.go index f221e5a..ad8df8a 100644 --- a/internal/x509crl/crl_internal_test.go +++ b/internal/x509crl/crl_internal_test.go @@ -38,7 +38,15 @@ func TestPKIXTimeFormsAndCenturyBoundary(t *testing.T) { {asn1.TagUTF8String, "500101000000Z", 0, false}, } { got, err := parseTime(asn1.RawValue{Tag: tc.tag, Bytes: []byte(tc.input), FullBytes: der.Wrap(0, tc.tag, false, []byte(tc.input))}) - if (err == nil) != tc.valid || tc.valid && (got.Year() != tc.year || got.Location() != time.UTC) { + if err != nil { + if tc.valid { + t.Fatalf("%d/%s: %v", tc.tag, tc.input, err) + } + + continue + } + + if !tc.valid || got.Year() != tc.year || got.Location() != time.UTC { t.Fatalf("%d/%s: %v, %v", tc.tag, tc.input, got, err) } } diff --git a/internal/x509name/cache.go b/internal/x509name/cache.go index bb94304..a03addc 100644 --- a/internal/x509name/cache.go +++ b/internal/x509name/cache.go @@ -2,13 +2,18 @@ package x509name import "sync" +const ( + maximumCachedNames = 64 + maximumNameCacheBytes = 1 << 20 +) + // This process cache is deliberately bounded by both entry count and retained // bytes. It stores only successful public DN comparisons, never control errors. // //nolint:gochecknoglobals // Bounded shared, synchronized comparison cache. var nameCache struct { sync.Mutex - entries [64]cacheEntry + entries [maximumCachedNames]cacheEntry next int bytes int } @@ -20,25 +25,26 @@ type cacheEntry struct { func nameCacheGet(raw []byte) (string, bool) { nameCache.Lock() - defer nameCache.Unlock() for _, entry := range nameCache.entries { if entry.raw != "" && entry.raw == string(raw) { + nameCache.Unlock() return entry.key, true } } + nameCache.Unlock() + return "", false } func nameCachePut(raw, key string) { size := len(raw) + len(key) - if size > 1<<20 { + if size > maximumNameCacheBytes { return } nameCache.Lock() - defer nameCache.Unlock() evict := func(i int) { old := nameCache.entries[i] @@ -47,7 +53,7 @@ func nameCachePut(raw, key string) { } evict(nameCache.next) - for nameCache.bytes+size > 1<<20 { + for nameCache.bytes+size > maximumNameCacheBytes { nameCache.next = (nameCache.next + 1) % len(nameCache.entries) evict(nameCache.next) } @@ -55,4 +61,5 @@ func nameCachePut(raw, key string) { nameCache.entries[nameCache.next] = cacheEntry{raw, key} nameCache.bytes += size nameCache.next = (nameCache.next + 1) % len(nameCache.entries) + nameCache.Unlock() } diff --git a/internal/x509name/name.go b/internal/x509name/name.go index e6fad7a..cbb345a 100644 --- a/internal/x509name/name.go +++ b/internal/x509name/name.go @@ -15,6 +15,10 @@ import ( "github.com/skarm/xmlsec/internal/der" ) +const ( + maximumOIDBytes = 128 +) + const ( maxNameBytes = 64 << 10 maxRDNs = 256 @@ -117,12 +121,18 @@ func EqualOrExact(ctx context.Context, a, b []byte) (bool, error) { } func key(ctx context.Context, raw []byte) (string, error) { - root, err := der.ReadLimits(raw, maxNameBytes, 6, 1+maxRDNs+5*maxAVAs) + const ( + maximumNameDepth = 6 + maximumAVANodes = 5 + comparisonKeyPrefix = "DN4518\x00" + ) + + root, err := der.ReadLimits(raw, maxNameBytes, maximumNameDepth, 1+maxRDNs+maximumAVANodes*maxAVAs) if err != nil { return "", nameDERError(err) } - if root.Class != 0 || root.Tag != asn1.TagSequence || !root.IsCompound { + if root.Class != asn1.ClassUniversal || root.Tag != asn1.TagSequence || !root.IsCompound { return "", ErrInvalid } @@ -131,7 +141,7 @@ func key(ctx context.Context, raw []byte) (string, error) { return "", nameDERError(err) } - out := binary.AppendUvarint([]byte("DN4518\x00"), uint64(len(rdns))) + out := binary.AppendUvarint([]byte(comparisonKeyPrefix), uint64(len(rdns))) count := 0 unsupported := false @@ -140,7 +150,7 @@ func key(ctx context.Context, raw []byte) (string, error) { return "", err } - if rdn.Class != 0 || rdn.Tag != asn1.TagSet || !rdn.IsCompound { + if rdn.Class != asn1.ClassUniversal || rdn.Tag != asn1.TagSet || !rdn.IsCompound { return "", ErrInvalid } @@ -174,7 +184,7 @@ func key(ctx context.Context, raw []byte) (string, error) { out = binary.AppendUvarint(out, uint64(len(keys))) for _, k := range keys { - if len(k) > maxKeyBytes-len(out)-10 { + if len(k) > maxKeyBytes-len(out)-binary.MaxVarintLen64 { return "", ErrLimit } @@ -191,22 +201,28 @@ func key(ctx context.Context, raw []byte) (string, error) { } func avaKey(ctx context.Context, ava asn1.RawValue) (string, error) { - if ava.Class != 0 || ava.Tag != asn1.TagSequence || !ava.IsCompound { + const ( + avaFields = 2 + unicodeMatchKind byte = 1 + domainMatchKind byte = 2 + ) + + if ava.Class != asn1.ClassUniversal || ava.Tag != asn1.TagSequence || !ava.IsCompound { return "", ErrInvalid } - fields, err := der.ChildrenLimit(ava, 2) - if err != nil || len(fields) != 2 { + fields, err := der.ChildrenLimit(ava, avaFields) + if err != nil || len(fields) != avaFields { return "", ErrInvalid } typ, value := fields[0], fields[1] - if typ.Class != 0 || typ.Tag != asn1.TagOID || typ.IsCompound { + if typ.Class != asn1.ClassUniversal || typ.Tag != asn1.TagOID || typ.IsCompound { return "", ErrInvalid } - if len(typ.Bytes) > 128 { + if len(typ.Bytes) > maximumOIDBytes { return "", ErrLimit } @@ -215,13 +231,13 @@ func avaKey(ctx context.Context, ava asn1.RawValue) (string, error) { return "", ErrInvalid } - if value.Class != 0 || value.IsCompound { + if value.Class != asn1.ClassUniversal || value.IsCompound { return "", ErrUnsupported } var prepared string - kind := byte(1) + kind := unicodeMatchKind if oid.Equal(asn1.ObjectIdentifier{0, 9, 2342, 19200300, 100, 1, 25}) { if value.Tag != asn1.TagIA5String { @@ -229,7 +245,7 @@ func avaKey(ctx context.Context, ava asn1.RawValue) (string, error) { } prepared = strings.ToLower(string(value.Bytes)) // DER scanner already enforces ASCII. - kind = 2 + kind = domainMatchKind } else { if value.Tag != asn1.TagUTF8String && value.Tag != asn1.TagPrintableString { return "", ErrUnsupported diff --git a/internal/x509name/name_internal_test.go b/internal/x509name/name_internal_test.go index 3c9f50c..abe7fd8 100644 --- a/internal/x509name/name_internal_test.go +++ b/internal/x509name/name_internal_test.go @@ -12,10 +12,6 @@ import ( "github.com/skarm/xmlsec/internal/der" ) -func testName(tag int, value string) []byte { - return der.Seq(der.Set(der.Seq(der.OID(asn1.ObjectIdentifier{2, 5, 4, 3}), der.Wrap(0, tag, false, []byte(value))))) -} - func TestNamePreparationStructureAndFallback(t *testing.T) { for _, tc := range []struct { name, a, b string @@ -199,3 +195,7 @@ func BenchmarkNameKeyUnicode(b *testing.B) { index++ } } + +func testName(tag int, value string) []byte { + return der.Seq(der.Set(der.Seq(der.OID(asn1.ObjectIdentifier{2, 5, 4, 3}), der.Wrap(0, tag, false, []byte(value))))) +} diff --git a/internal/x509name/prep.go b/internal/x509name/prep.go index fd43c1b..37798eb 100644 --- a/internal/x509name/prep.go +++ b/internal/x509name/prep.go @@ -10,8 +10,23 @@ import ( "github.com/skarm/xmlsec/internal/normalization" ) +const ( + cancellationMask = 255 + unicodeTableVersion = "17.0.0" +) + +// Packed generated tables store code points as four big-endian bytes. A range +// stores two code points; a fold stores a code point, two-byte offset and length. +const ( + packedCodePointBytes = 4 + packedOffsetBytes = 2 + packedRangeBytes = 2 * packedCodePointBytes + packedFoldBytes = packedCodePointBytes + packedOffsetBytes + 1 + packedOctetBits = 8 +) + func uint32At(s string, i int) uint32 { - return uint32(s[i])<<24 | uint32(s[i+1])<<16 | uint32(s[i+2])<<8 | uint32(s[i+3]) + return uint32(s[i])<<(3*packedOctetBits) | uint32(s[i+1])<<(2*packedOctetBits) | uint32(s[i+2])< end { @@ -45,20 +60,20 @@ func caseFold32(r rune) string { n := uint32(r) // #nosec G115 -- decoded UTF-8 rune, checked nonnegative. - lo, hi := 0, len(foldKeys32)/7 + lo, hi := 0, len(foldKeys32)/packedFoldBytes for lo < hi { mid := lo + (hi-lo)/2 - k := uint32At(foldKeys32, mid*7) + k := uint32At(foldKeys32, mid*packedFoldBytes) if n < k { hi = mid } else if n > k { lo = mid + 1 } else { - at := mid * 7 - offset := int(foldKeys32[at+4])<<8 | int(foldKeys32[at+5]) + at := mid * packedFoldBytes + offset := int(foldKeys32[at+packedCodePointBytes])<= 'a' && r <= 'z' || r >= 'A' && r <= 'Z' || r >= '0' && r <= '9' || strings.ContainsRune(" \r\n-'()+,./:=?;!*#@$_%", r)) { + if !(r >= 'a' && r <= 'z' || r >= 'A' && r <= 'Z' || r >= '0' && r <= '9' || strings.ContainsRune(publicIDPunctuation, r)) { return false } } @@ -55,12 +57,14 @@ func Quote(value string, preferred byte) byte { // WriteDoctype validates and writes output metadata, without an internal subset. func WriteDoctype(dst io.Writer, name, publicID, systemID string) error { - return writeDoctype(dst, name, publicID, systemID, false) + const allowPublicOnly = false + return writeDoctype(dst, name, publicID, systemID, allowPublicOnly) } // WriteHTMLDoctype also permits a PUBLIC identifier without a SYSTEM identifier. func WriteHTMLDoctype(dst io.Writer, name, publicID, systemID string) error { - return writeDoctype(dst, name, publicID, systemID, true) + const allowPublicOnly = true + return writeDoctype(dst, name, publicID, systemID, allowPublicOnly) } func writeDoctype(dst io.Writer, name, publicID, systemID string, publicOnly bool) error { diff --git a/internal/xmltime/time.go b/internal/xmltime/time.go index 0a00bb4..90cf5bd 100644 --- a/internal/xmltime/time.go +++ b/internal/xmltime/time.go @@ -8,6 +8,22 @@ import ( "time" ) +const ( + hoursPerDay = 24 + maximumDateTimeBytes = 30 + maximumYear = 9999 + minimumDateTimeBytes = 20 + minimumFractionDateTimeBytes = minimumDateTimeBytes + 2 +) + +// Positions in the fixed four-digit-year lexical profile, before the final Z. +// A fractional part starts at the byte immediately following the seconds. +const ( + dateTimeLayout = "2006-01-02T15:04:05" + clockEndOffset = len(dateTimeLayout) + fractionStartOffset = clockEndOffset + 1 +) + // ErrFormat reports an invalid or unsupported XML Schema dateTime value. var ErrFormat = errors.New("invalid or unsupported UTC XML Schema dateTime") @@ -15,17 +31,27 @@ var ErrFormat = errors.New("invalid or unsupported UTC XML Schema dateTime") // most nine fractional digits. XML Schema permits 24:00:00 for midnight, but // does not permit the comma or single-digit hour accepted by time.Parse. func ParseUTC(s string) (time.Time, error) { + const ( + hourStartOffset = 11 + hourDigits = 2 + minuteStartOffset = 14 + midnightHour = "24" + zeroHour = "00" + zeroClock = "00:00" + zeroFractionChars = ".0" + ) + if !validLexical(s) { return time.Time{}, ErrFormat } - nextDay := s[11:13] == "24" + nextDay := s[hourStartOffset:hourStartOffset+hourDigits] == midnightHour if nextDay { - if s[14:19] != "00:00" || strings.Trim(s[19:len(s)-1], ".0") != "" { + if s[minuteStartOffset:clockEndOffset] != zeroClock || strings.Trim(s[clockEndOffset:len(s)-1], zeroFractionChars) != "" { return time.Time{}, ErrFormat } - s = s[:11] + "00" + s[13:] + s = s[:hourStartOffset] + zeroHour + s[hourStartOffset+hourDigits:] } t, err := time.Parse(time.RFC3339Nano, s) @@ -34,8 +60,8 @@ func ParseUTC(s string) (time.Time, error) { } if nextDay { - t = t.Add(24 * time.Hour) - if t.Year() > 9999 { + t = t.Add(hoursPerDay * time.Hour) + if t.Year() > maximumYear { return time.Time{}, ErrFormat } } @@ -44,13 +70,12 @@ func ParseUTC(s string) (time.Time, error) { } func validLexical(s string) bool { - if len(s) < 20 || len(s) > 30 || s[len(s)-1] != 'Z' { + if len(s) < minimumDateTimeBytes || len(s) > maximumDateTimeBytes || s[len(s)-1] != 'Z' { return false } - const layout = "2006-01-02T15:04:05" - for i := range len(layout) { - c := layout[i] + for i := range len(dateTimeLayout) { + c := dateTimeLayout[i] if c >= '0' && c <= '9' { if s[i] < '0' || s[i] > '9' { return false @@ -60,15 +85,15 @@ func validLexical(s string) bool { } } - if len(s) == 20 { + if len(s) == minimumDateTimeBytes { return true } - if len(s) < 22 || s[19] != '.' { + if len(s) < minimumFractionDateTimeBytes || s[clockEndOffset] != '.' { return false } - for i := 20; i < len(s)-1; i++ { + for i := fractionStartOffset; i < len(s)-1; i++ { if s[i] < '0' || s[i] > '9' { return false } diff --git a/keyinfo/build.go b/keyinfo/build.go index 6b2371d..df3f81e 100644 --- a/keyinfo/build.go +++ b/keyinfo/build.go @@ -20,6 +20,14 @@ import ( "github.com/skarm/xmlsec/reference" ) +const ( + // Caps each builder collection; aggregate candidate and element visits have + // separate default-profile budgets, including values repeated across X509Data groups. + maxBuilderEntries = 32 + // Hard ceiling on transforms attached to one RetrievalMethod. + maxRetrievalTransforms = 64 +) + // KeyFormat selects the XML representation of a public key. type KeyFormat string @@ -116,25 +124,25 @@ func Build(ctx context.Context, t Template) (*document.Document, error) { return nil, ErrInput } - if len(t.Names) > 32 || len(t.Keys) > 32 || len(t.Certificates) > 32 || len(t.KeyInfoReferences) > 32 || len(t.RetrievalMethods) > 32 { + if len(t.Names) > maxBuilderEntries || len(t.Keys) > maxBuilderEntries || len(t.Certificates) > maxBuilderEntries || len(t.KeyInfoReferences) > maxBuilderEntries || len(t.RetrievalMethods) > maxBuilderEntries { return nil, ErrLimit } - root := element(Namespace, "ds", lexicon.DSigElementKeyInfo) + root := element(Namespace, lexicon.PrefixDSig, lexicon.DSigElementKeyInfo) - root.NS = []tree.Namespace{{Prefix: "ds", URI: Namespace}, {Prefix: "dsig11", URI: Namespace11}} + root.NS = []tree.Namespace{{Prefix: lexicon.PrefixDSig, URI: Namespace}, {Prefix: lexicon.PrefixDSig11, URI: Namespace11}} if t.ID != "" { - set(root, "Id", t.ID) + set(root, lexicon.SecurityAttrID, t.ID) } total := 0 for _, name := range t.Names { total += len(name) - if total > 4<<20 { + if total > defaultMaxTotalBytes { return nil, ErrLimit } - v := element(Namespace, "ds", lexicon.DSigElementKeyName) + v := element(Namespace, lexicon.PrefixDSig, lexicon.DSigElementKeyName) addText(v, name) add(root, v) } @@ -160,16 +168,16 @@ func Build(ctx context.Context, t Template) (*document.Document, error) { } for _, uri := range t.KeyInfoReferences { - if len(uri) > 8192 { + if len(uri) > maxURIBytes { return nil, ErrLimit } - if externalURI(uri) && !validKeyURI(uri) { + if !validKeyURI(uri) { return nil, ErrInput } - r := element(Namespace11, "dsig11", nameKeyInfoReference) - set(r, "URI", uri) + r := element(Namespace11, lexicon.PrefixDSig11, nameKeyInfoReference) + set(r, lexicon.SecurityAttrURI, uri) add(root, r) } @@ -188,32 +196,41 @@ func Build(ctx context.Context, t Template) (*document.Document, error) { // Count parser visits as well as output entries: decoding a selector is work // even when the same value is repeated in several groups. func x509TemplateBudget(ctx context.Context, t *Template, total *int) error { + // Visit weights count the root and each selected XML representation's + // nodes; RSA/DSA add components beyond the item/bare-key/binary base. + const ( + keyInfoRootVisits = 1 + keyNameVisits = 2 + keyItemBaseVisits = 3 + dsaAdditionalVisits = 3 + ) + groups := len(t.X509Data) if len(t.Certificates) > 0 { groups++ } - if groups > 32 { + if groups > maxBuilderEntries { return ErrLimit } - visits := 1 + 2*len(t.Names) + len(t.KeyInfoReferences) + len(t.RetrievalMethods) + visits := keyInfoRootVisits + keyNameVisits*len(t.Names) + len(t.KeyInfoReferences) + len(t.RetrievalMethods) for _, key := range t.Keys { - visits += 3 // item, bare key, binary value + visits += keyItemBaseVisits if key.Format != DERKeyValue { switch key.Key.(type) { case *rsa.PublicKey: visits++ case *dsa.PublicKey: - visits += 3 + visits += dsaAdditionalVisits } } } candidates := len(t.Keys) charge := func(size int) error { - if size > 1<<20 || size > (4<<20)-*total { + if size > defaultMaxBlobBytes || size > defaultMaxTotalBytes-*total { return ErrLimit } @@ -231,7 +248,7 @@ func x509TemplateBudget(ctx context.Context, t *Template, total *int) error { items := 0 for _, count := range counts { - if count > 32 { + if count > maxBuilderEntries { return ErrLimit } @@ -245,7 +262,7 @@ func x509TemplateBudget(ctx context.Context, t *Template, total *int) error { visits += 1 + 2*items + len(group.IssuerSerials) candidates += len(group.Certificates) - if visits > 256 || candidates > 32 { + if visits > defaultMaxElements || candidates > defaultMaxCandidates { return ErrLimit } @@ -267,11 +284,11 @@ func x509TemplateBudget(ctx context.Context, t *Template, total *int) error { } func buildRetrieval(ctx context.Context, root *tree.Node, method RetrievalMethod, total *int) error { - if len(method.URI) > 8192 || len(method.Transforms) > 64 { + if len(method.URI) > maxURIBytes || len(method.Transforms) > maxRetrievalTransforms { return ErrLimit } - if externalURI(method.URI) && !validKeyURI(method.URI) { + if !validKeyURI(method.URI) { return ErrInput } @@ -283,18 +300,18 @@ func buildRetrieval(ctx context.Context, root *tree.Node, method RetrievalMethod return err } - n := element(Namespace, "ds", "RetrievalMethod") - set(n, "URI", method.URI) + n := element(Namespace, lexicon.PrefixDSig, lexicon.DSigElementRetrievalMethod) + set(n, lexicon.SecurityAttrURI, method.URI) if method.Type != "" { - set(n, "Type", method.Type) + set(n, lexicon.SecurityAttrType, method.Type) } if len(method.Transforms) != 0 { - transforms := element(Namespace, "ds", lexicon.DSigElementTransforms) + transforms := element(Namespace, lexicon.PrefixDSig, lexicon.DSigElementTransforms) for _, transform := range method.Transforms { - child := element(Namespace, "ds", lexicon.DSigElementTransform) - set(child, "Algorithm", transform.Algorithm) + child := element(Namespace, lexicon.PrefixDSig, lexicon.DSigElementTransform) + set(child, lexicon.SecurityAttrAlgorithm, transform.Algorithm) if err := transformxml.AppendParameters(ctx, child, transform); err != nil { return retrievalError(err) @@ -314,7 +331,7 @@ func buildRetrieval(ctx context.Context, root *tree.Node, method RetrievalMethod // Bound all retained parameter sources before importing any stylesheet tree. func retrievalTemplateBudget(ctx context.Context, method RetrievalMethod, total *int) error { charge := func(value string) error { - if len(value) > (4<<20)-*total { + if len(value) > defaultMaxTotalBytes-*total { return ErrLimit } @@ -336,7 +353,7 @@ func retrievalTemplateBudget(ctx context.Context, method RetrievalMethod, total return err } - if len(step.Stylesheet) > (4<<20)-*total { + if len(step.Stylesheet) > defaultMaxTotalBytes-*total { return ErrLimit } @@ -415,7 +432,7 @@ func buildKeys(ctx context.Context, keys []PublicKeyValue, root *tree.Node) erro return err } - if err := validateKey(value.Key, 8192); err != nil { + if err := validateKey(value.Key, defaultMaxRSABits); err != nil { return err } @@ -438,7 +455,7 @@ func buildPublicKey(ctx context.Context, value PublicKeyValue) (*tree.Node, erro return nil, err } - node := element(Namespace11, "dsig11", nameDEREncodedKeyValue) + node := element(Namespace11, lexicon.PrefixDSig11, nameDEREncodedKeyValue) encoded, err := tree.EncodeBase64(ctx, der) if err != nil { @@ -449,7 +466,7 @@ func buildPublicKey(ctx context.Context, value PublicKeyValue) (*tree.Node, erro return node, nil case "", XMLKeyValue: - node := element(Namespace, "ds", lexicon.DSigElementKeyValue) + node := element(Namespace, lexicon.PrefixDSig, lexicon.DSigElementKeyValue) child, err := buildXMLKey(ctx, value.Key) if err != nil { @@ -467,29 +484,29 @@ func buildPublicKey(ctx context.Context, value PublicKeyValue) (*tree.Node, erro func buildXMLKey(ctx context.Context, key crypto.PublicKey) (*tree.Node, error) { switch k := key.(type) { case *dsa.PublicKey: - node := element(Namespace, "ds", nameDSAKeyValue) + node := element(Namespace, lexicon.PrefixDSig, nameDSAKeyValue) for i, value := range []*big.Int{k.P, k.Q, k.G, k.Y} { - if err := blob(ctx, node, Namespace, "ds", []string{"P", "Q", "G", "Y"}[i], value.Bytes()); err != nil { + if err := blob(ctx, node, Namespace, lexicon.PrefixDSig, []string{nameP, nameQ, nameG, nameY}[i], value.Bytes()); err != nil { return nil, err } } return node, nil case *rsa.PublicKey: - node := element(Namespace, "ds", nameRSAKeyValue) - if err := blob(ctx, node, Namespace, "ds", "Modulus", k.N.Bytes()); err != nil { + node := element(Namespace, lexicon.PrefixDSig, nameRSAKeyValue) + if err := blob(ctx, node, Namespace, lexicon.PrefixDSig, nameModulus, k.N.Bytes()); err != nil { return nil, err } - if err := blob(ctx, node, Namespace, "ds", "Exponent", big.NewInt(int64(k.E)).Bytes()); err != nil { + if err := blob(ctx, node, Namespace, lexicon.PrefixDSig, nameExponent, big.NewInt(int64(k.E)).Bytes()); err != nil { return nil, err } return node, nil case *ecdsa.PublicKey: - node := element(Namespace11, "dsig11", nameECKeyValue) - curve := element(Namespace11, "dsig11", "NamedCurve") - set(curve, "URI", curveURI(k.Curve)) + node := element(Namespace11, lexicon.PrefixDSig11, nameECKeyValue) + curve := element(Namespace11, lexicon.PrefixDSig11, lexicon.DSigElementNamedCurve) + set(curve, lexicon.SecurityAttrURI, curveURI(k.Curve)) add(node, curve) point, err := k.Bytes() @@ -497,7 +514,7 @@ func buildXMLKey(ctx context.Context, key crypto.PublicKey) (*tree.Node, error) return nil, ErrKey } - if err := blob(ctx, node, Namespace11, "dsig11", "PublicKey", point); err != nil { + if err := blob(ctx, node, Namespace11, lexicon.PrefixDSig11, lexicon.DSigElementPublicKey, point); err != nil { return nil, err } diff --git a/keyinfo/build_test.go b/keyinfo/build_test.go index 811c332..798e13e 100644 --- a/keyinfo/build_test.go +++ b/keyinfo/build_test.go @@ -36,6 +36,48 @@ const ( uriUnknown = "urn:unknown" ) +func TestBuildRejectsMalformedFragmentURI(t *testing.T) { + for _, uri := range []string{"#%", "#%0", "#%zz", "#target key", "#target#key", `#target\key`} { + for _, representation := range []string{"KeyInfoReference", "RetrievalMethod"} { + t.Run(representation+"/"+uri, func(t *testing.T) { + template := keyinfo.Template{KeyInfoReferences: []string{uri}} + if representation == "RetrievalMethod" { + template = keyinfo.Template{RetrievalMethods: []keyinfo.RetrievalMethod{{URI: uri}}} + } + + if doc, err := keyinfo.Build(t.Context(), template); doc != nil || !errors.Is(err, keyinfo.ErrInput) { + t.Fatal("malformed fragment was built", doc, err) + } + }) + } + } +} + +func TestBuildAndParseFragmentURIProfiles(t *testing.T) { + for _, uri := range []string{"#target", "#%74arget", "#xpointer(id('target'))", `#xpointer(id("target"))`, "#xpointer%28id%28%27target%27%29%29"} { + for _, representation := range []string{"KeyInfoReference", "RetrievalMethod"} { + t.Run(representation+"/"+uri, func(t *testing.T) { + template := keyinfo.Template{KeyInfoReferences: []string{uri}} + if representation == "RetrievalMethod" { + template = keyinfo.Template{RetrievalMethods: []keyinfo.RetrievalMethod{{URI: uri}}} + } + + info, err := keyinfo.Build(t.Context(), template) + if err != nil { + t.Fatal(err) + } + + target := `local` + doc := parseXML(t, ``+asXML(t, info)+target+``) + result, err := keyinfo.Parse(t.Context(), doc, doc.Root().Children()[0], keyinfo.Options{}) + if err != nil || len(result.KeyNames()) != 1 || result.KeyNames()[0] != "local" || len(result.ExternalURIs()) != 0 { + t.Fatal("local URI profile changed", result, err) + } + }) + } + } +} + func TestBuildX509DataIndependentFixtures(t *testing.T) { data := make([][]byte, 0, 3) diff --git a/keyinfo/constants.go b/keyinfo/constants.go index 2cd8c51..5321d46 100644 --- a/keyinfo/constants.go +++ b/keyinfo/constants.go @@ -3,13 +3,37 @@ package keyinfo import "github.com/skarm/xmlsec/internal/lexicon" const ( + // Names the RSA public exponent in XMLDSig KeyValue encoding. + nameExponent = "Exponent" + // Names the DSA generator in XMLDSig KeyValue encoding. + nameG = "G" + // Names the RSA modulus in XMLDSig KeyValue encoding. + nameModulus = "Modulus" + // Names the DSA prime modulus in XMLDSig KeyValue encoding. + nameP = "P" + // Names the DSA subgroup order in XMLDSig KeyValue encoding. + nameQ = "Q" + // Names the DSA public value in XMLDSig KeyValue encoding. + nameY = "Y" +) + +const ( + // XMLDSig 1.1 representation carrying the DER SubjectPublicKeyInfo bytes of a public key. nameDEREncodedKeyValue = "DEREncodedKeyValue" - nameDSAKeyValue = "DSAKeyValue" - nameECKeyValue = "ECKeyValue" - nameKeyInfoReference = "KeyInfoReference" - nameRSAKeyValue = "RSAKeyValue" - nameX509Data = lexicon.DSigElementX509Data - nameX509Certificate = lexicon.DSigElementX509Certificate - nameX509CRL = lexicon.DSigElementX509CRL - nameX509SKI = "X509SKI" + // XMLDSig representation of legacy DSA public parameters. + nameDSAKeyValue = "DSAKeyValue" + // XMLDSig 1.1 representation combining a named curve and an encoded EC point. + nameECKeyValue = lexicon.DSigElementECKeyValue + // XMLDSig 1.1 reference to a separately resolved KeyInfo element. + nameKeyInfoReference = "KeyInfoReference" + // XMLDSig representation of an RSA public modulus and exponent. + nameRSAKeyValue = "RSAKeyValue" + // Groups certificate/CRL encodings and untrusted certificate selectors within KeyInfo. + nameX509Data = lexicon.DSigElementX509Data + // Carries one DER certificate within an X509Data group. + nameX509Certificate = lexicon.DSigElementX509Certificate + // Carries one DER CRL within an X509Data group. + nameX509CRL = lexicon.DSigElementX509CRL + // Carries raw subject-key-identifier bytes used for certificate selection. + nameX509SKI = "X509SKI" ) diff --git a/keyinfo/doc.go b/keyinfo/doc.go index 9c85e22..c294754 100644 --- a/keyinfo/doc.go +++ b/keyinfo/doc.go @@ -17,7 +17,7 @@ // collections. Accessors copy mutable key/certificate data. X509Data grouping // and selector spellings are retained without inferring chain relationships or // trust. -// [Template.X509Data] builds separate nonempty groups of certificates, v2 CRLs, +// [Template.X509Data] builds separate nonempty groups of certificates, v1/v2 CRLs, // issuer/serial pairs, subject names, plain subject key identifiers and certificate // digests. [Template.Certificates] retains its separate first group. Original DER // bytes and supplied DN spellings are preserved; serial numbers use the same diff --git a/keyinfo/parse.go b/keyinfo/parse.go index 123947d..e44fc04 100644 --- a/keyinfo/parse.go +++ b/keyinfo/parse.go @@ -25,6 +25,20 @@ import ( "github.com/skarm/xmlsec/reference" ) +const ( + legacyDSAPBits = 1024 + legacyDSAQBits = 160 + maxDSAKeyChildren = 7 + maxRSAExponent = 1<<31 - 1 + minRSAExponent = 3 +) + +const ( + nameJ = "J" + namePgenCounter = "PgenCounter" + nameSeed = "Seed" +) + // Parse reads one explicitly selected ds:KeyInfo in the supplied snapshot. // Unsupported choices are errors, not silently ignored extensions. KeyName is // preserved verbatim. KeyInfoReference/typed RetrievalMethod follow bounded @@ -91,7 +105,7 @@ func Parse(ctx context.Context, doc *document.Document, selected document.Elemen // DefaultIDRules returns a fresh copy of the supported default ID attribute rules. func DefaultIDRules() []reference.IDAttributeRule { - return []reference.IDAttributeRule{{Attribute: document.Name{Local: "Id"}, Elements: []document.Name{ + return []reference.IDAttributeRule{{Attribute: document.Name{Local: lexicon.SecurityAttrID}, Elements: []document.Name{ {Space: Namespace, Local: lexicon.DSigElementKeyInfo}, {Space: Namespace11, Local: nameECKeyValue}, {Space: Namespace11, Local: nameDEREncodedKeyValue}, @@ -182,7 +196,7 @@ func (p *parser) keyInfo(s *scope, n *tree.Node, depth int) error { } defer leave() - children, err := structure(n, Namespace, lexicon.DSigElementKeyInfo, "Id") + children, err := structure(n, Namespace, lexicon.DSigElementKeyInfo, lexicon.SecurityAttrID) if err != nil { return err } @@ -197,7 +211,7 @@ func (p *parser) keyInfo(s *scope, n *tree.Node, depth int) error { return err } - if is(child, Namespace, "RetrievalMethod") { + if is(child, Namespace, lexicon.DSigElementRetrievalMethod) { if _, err := p.prepareRetrieval(s, child); err != nil { return err } @@ -279,7 +293,7 @@ func (p *parser) item(s *scope, n *tree.Node, depth int) error { } return p.keyInfo(scope, target, depth+1) - case document.Name{Space: Namespace, Local: "RetrievalMethod"}: + case document.Name{Space: Namespace, Local: lexicon.DSigElementRetrievalMethod}: return p.retrieve(s, n, depth) default: return fmt.Errorf("%w: {%s}%s", ErrUnsupported, n.Name.Space, n.Name.Local) @@ -418,7 +432,7 @@ func (p *parser) rsaKeyValue(n *tree.Node) (*rsa.PublicKey, error) { return nil, err } - if len(children) != 2 || !is(children[0], Namespace, "Modulus") || !is(children[1], Namespace, "Exponent") { + if len(children) != 2 || !is(children[0], Namespace, nameModulus) || !is(children[1], Namespace, nameExponent) { return nil, ErrInput } @@ -438,7 +452,7 @@ func (p *parser) rsaKeyValue(n *tree.Node) (*rsa.PublicKey, error) { } e := new(big.Int).SetBytes(exponent) - if !e.IsInt64() || e.Int64() > 2147483647 { + if !e.IsInt64() || e.Int64() > maxRSAExponent { return nil, ErrKey } @@ -451,7 +465,7 @@ func (p *parser) dsaKeyValue(n *tree.Node) (*dsa.PublicKey, error) { return nil, err } - if len(children) < 1 || len(children) > 7 { + if len(children) < 1 || len(children) > maxDSAKeyChildren { return nil, ErrInput } @@ -460,7 +474,7 @@ func (p *parser) dsaKeyValue(n *tree.Node) (*dsa.PublicKey, error) { return nil, err } - if len(remaining) > 0 && is(remaining[0], Namespace, "J") { + if len(remaining) > 0 && is(remaining[0], Namespace, nameJ) { j, err := p.dsaInteger(remaining[0]) if err != nil { return nil, err @@ -474,7 +488,7 @@ func (p *parser) dsaKeyValue(n *tree.Node) (*dsa.PublicKey, error) { } if len(remaining) > 0 { - if len(remaining) != 2 || !is(remaining[0], Namespace, "Seed") || !is(remaining[1], Namespace, "PgenCounter") { + if len(remaining) != 2 || !is(remaining[0], Namespace, nameSeed) || !is(remaining[1], Namespace, namePgenCounter) { return nil, ErrInput } @@ -494,7 +508,7 @@ func (p *parser) configureDSAParameters() error { return nil } - if parameters.P == nil || parameters.Q == nil || parameters.G == nil || parameters.P.BitLen() != 1024 || parameters.Q.BitLen() != 160 || parameters.G.BitLen() > 1024 { + if parameters.P == nil || parameters.Q == nil || parameters.G == nil || parameters.P.BitLen() != legacyDSAPBits || parameters.Q.BitLen() != legacyDSAQBits || parameters.G.BitLen() > legacyDSAPBits { return fmt.Errorf("%w: invalid DSA parameter context", ErrPolicy) } @@ -512,8 +526,8 @@ func (p *parser) dsaPublicFields(children []*tree.Node) (*dsa.PublicKey, []*tree key := &dsa.PublicKey{} remaining := children - if is(remaining[0], Namespace, "P") { - if len(remaining) < 2 || !is(remaining[1], Namespace, "Q") { + if is(remaining[0], Namespace, nameP) { + if len(remaining) < 2 || !is(remaining[1], Namespace, nameQ) { return nil, nil, ErrInput } @@ -532,7 +546,7 @@ func (p *parser) dsaPublicFields(children []*tree.Node) (*dsa.PublicKey, []*tree remaining = remaining[2:] } - if len(remaining) > 0 && is(remaining[0], Namespace, "G") { + if len(remaining) > 0 && is(remaining[0], Namespace, nameG) { var err error key.G, err = p.dsaInteger(remaining[0]) @@ -543,7 +557,7 @@ func (p *parser) dsaPublicFields(children []*tree.Node) (*dsa.PublicKey, []*tree remaining = remaining[1:] } - if len(remaining) == 0 || !is(remaining[0], Namespace, "Y") { + if len(remaining) == 0 || !is(remaining[0], Namespace, nameY) { return nil, nil, ErrInput } @@ -573,6 +587,10 @@ func (p *parser) dsaPublicFields(children []*tree.Node) (*dsa.PublicKey, []*tree } func (p *parser) dsaInteger(n *tree.Node) (*big.Int, error) { + // Bounds each decoded CryptoBinary field in the supported legacy DSA + // representation, including generation hints; this is unrelated to curve URIs. + const maximumDSACryptoBinaryBytes = 128 + value, err := p.binary(n) if err != nil { return nil, err @@ -582,7 +600,7 @@ func (p *parser) dsaInteger(n *tree.Node) (*big.Int, error) { return nil, fmt.Errorf("%w: nonminimal DSA CryptoBinary", ErrInput) } - if len(value) > 128 { + if len(value) > maximumDSACryptoBinaryBytes { return nil, ErrKey } @@ -590,7 +608,7 @@ func (p *parser) dsaInteger(n *tree.Node) (*big.Int, error) { } func (p *parser) ecKeyValue(n *tree.Node) (*ecdsa.PublicKey, error) { - children, err := structure(n, Namespace11, nameECKeyValue, "Id") + children, err := structure(n, Namespace11, nameECKeyValue, lexicon.SecurityAttrID) if err != nil { return nil, err } @@ -599,20 +617,20 @@ func (p *parser) ecKeyValue(n *tree.Node) (*ecdsa.PublicKey, error) { return nil, ErrInput } - if !is(children[0], Namespace11, "NamedCurve") { + if !is(children[0], Namespace11, lexicon.DSigElementNamedCurve) { return nil, fmt.Errorf("%w: explicit EC parameters", ErrUnsupported) } - curveChildren, err := structure(children[0], Namespace11, "NamedCurve", "URI") + curveChildren, err := structure(children[0], Namespace11, lexicon.DSigElementNamedCurve, lexicon.SecurityAttrURI) if err != nil { return nil, err } - if len(curveChildren) != 0 || !is(children[1], Namespace11, "PublicKey") { + if len(curveChildren) != 0 || !is(children[1], Namespace11, lexicon.DSigElementPublicKey) { return nil, ErrInput } - uri, ok := attr(children[0], "URI") + uri, ok := attr(children[0], lexicon.SecurityAttrURI) if !ok { return nil, ErrInput } @@ -636,7 +654,7 @@ func (p *parser) ecKeyValue(n *tree.Node) (*ecdsa.PublicKey, error) { } func (p *parser) derEncodedKeyValue(n *tree.Node) (crypto.PublicKey, error) { - encoded, err := p.binary(n, "Id") + encoded, err := p.binary(n, lexicon.SecurityAttrID) if err != nil { return nil, err } @@ -690,7 +708,7 @@ func validateKey(key crypto.PublicKey, maxRSA int) error { return fmt.Errorf("%w: invalid legacy DSA public key", ErrKey) } case *rsa.PublicKey: - if k == nil || k.N == nil || k.N.Sign() <= 0 || k.N.Bit(0) == 0 || k.N.BitLen() > maxRSA || k.E < 3 || k.E%2 == 0 || int64(k.E) > 2147483647 || k.N.Cmp(big.NewInt(int64(k.E))) <= 0 { + if k == nil || k.N == nil || k.N.Sign() <= 0 || k.N.Bit(0) == 0 || k.N.BitLen() > maxRSA || k.E < minRSAExponent || k.E%2 == 0 || int64(k.E) > maxRSAExponent || k.N.Cmp(big.NewInt(int64(k.E))) <= 0 { return ErrKey } case *ecdsa.PublicKey: @@ -741,7 +759,7 @@ func attrs(n *tree.Node, allowed ...string) error { return fmt.Errorf("%w: unexpected attribute {%s}%s", ErrInput, a.Name.Space, a.Name.Local) } - if a.Name.Local == "Id" && !tree.NCName(a.Value) { + if a.Name.Local == lexicon.SecurityAttrID && !tree.NCName(a.Value) { return ErrInput } } diff --git a/keyinfo/policy.go b/keyinfo/policy.go index ad11006..96e383e 100644 --- a/keyinfo/policy.go +++ b/keyinfo/policy.go @@ -30,7 +30,7 @@ func (p *parser) configureKeyData() error { document.Name{Space: Namespace11, Local: nameECKeyValue}, document.Name{Space: Namespace11, Local: nameDEREncodedKeyValue}, document.Name{Space: Namespace, Local: nameX509Data}, - document.Name{Space: Namespace, Local: "RetrievalMethod"}, + document.Name{Space: Namespace, Local: lexicon.DSigElementRetrievalMethod}, document.Name{Space: Namespace11, Local: nameKeyInfoReference}: if _, exists := p.keyData[name]; exists { return fmt.Errorf("%w: duplicate AllowedKeyData", ErrPolicy) diff --git a/keyinfo/policy_test.go b/keyinfo/policy_test.go index 8999df9..dabe190 100644 --- a/keyinfo/policy_test.go +++ b/keyinfo/policy_test.go @@ -13,8 +13,8 @@ import ( ) const ( - xmlDsKeyNameHintDsKeyName = "hint" - nameKeyName = "KeyName" + keyNameHintXML = "hint" + nameKeyName = "KeyName" ) func TestAllowedKeyData(t *testing.T) { @@ -28,14 +28,14 @@ func TestAllowedKeyData(t *testing.T) { body string ok bool }{ - {"nil-all-supported", nil, xmlDsKeyNameHintDsKeyName + derXML(t, &key.PublicKey), true}, - {"empty-denies", []document.Name{}, xmlDsKeyNameHintDsKeyName, false}, - {"name-only", []document.Name{name}, xmlDsKeyNameHintDsKeyName, true}, - {"disabled-later-choice", []document.Name{name}, xmlDsKeyNameHintDsKeyName + derXML(t, &key.PublicKey), false}, + {"nil-all-supported", nil, keyNameHintXML + derXML(t, &key.PublicKey), true}, + {"empty-denies", []document.Name{}, keyNameHintXML, false}, + {"name-only", []document.Name{name}, keyNameHintXML, true}, + {"disabled-later-choice", []document.Name{name}, keyNameHintXML + derXML(t, &key.PublicKey), false}, {"der-only", []document.Name{der}, derXML(t, &key.PublicKey), true}, - {"bad-qname", []document.Name{{Space: "urn:wrong", Local: nameKeyName}}, xmlDsKeyNameHintDsKeyName, false}, - {"wrapper-not-representation", []document.Name{{Space: ns, Local: "KeyValue"}}, xmlDsKeyNameHintDsKeyName, false}, - {"duplicate", []document.Name{name, name}, xmlDsKeyNameHintDsKeyName, false}, + {"bad-qname", []document.Name{{Space: "urn:wrong", Local: nameKeyName}}, keyNameHintXML, false}, + {"wrapper-not-representation", []document.Name{{Space: ns, Local: "KeyValue"}}, keyNameHintXML, false}, + {"duplicate", []document.Name{name, name}, keyNameHintXML, false}, } { t.Run(test.name, func(t *testing.T) { doc := parseXML(t, wrap(test.body)) diff --git a/keyinfo/references.go b/keyinfo/references.go index c107893..04ff6d4 100644 --- a/keyinfo/references.go +++ b/keyinfo/references.go @@ -19,6 +19,18 @@ import ( "github.com/skarm/xmlsec/reference" ) +const ( + // Allows bounded encoded input relative to the decoded blob cap during retrieval processing. + encodedBlobSizeFactor = 2 + maxAllowedURIs = 1024 + // Caps XML depth when retrieved octets are parsed, independently of reference depth. + maxRetrievedXMLDepth = 64 + // Bounds KeyInfo resource and base URI spellings, in bytes, before resolution. + maxURIBytes = 8192 + // Converts the element budget into the parser/node-visit allowance for retrieval results. + retrievalXMLNodesPerElement = 8 +) + type resourceBlob struct { data []byte base64 bool @@ -30,7 +42,7 @@ func (p *parser) configureExternal() error { } e := p.options.External - if nilcheck.IsNil(e.Resolver) || len(e.AllowedURIs) == 0 || len(e.AllowedURIs) > 1024 { + if nilcheck.IsNil(e.Resolver) || len(e.AllowedURIs) == 0 || len(e.AllowedURIs) > maxAllowedURIs { return fmt.Errorf("%w: external resolver/allowlist", ErrPolicy) } // Copy configuration before invoking any caller callback. @@ -46,7 +58,7 @@ func (p *parser) configureExternal() error { return fmt.Errorf("%w: external BaseURI: %w", ErrPolicy, err) } - if len(copied.BaseURI) > 8192 || (p.baseURI != nil && p.baseURI.User != nil) { + if len(copied.BaseURI) > maxURIBytes || (p.baseURI != nil && p.baseURI.User != nil) { return fmt.Errorf("%w: external BaseURI", ErrPolicy) } @@ -66,11 +78,17 @@ func (p *parser) configureExternal() error { } func (p *parser) resourceURI(s *scope, n *tree.Node, uri string) (string, error) { - if len(uri) > 8192 { + if len(uri) > maxURIBytes { return "", ErrLimit } if !externalURI(uri) { + // Validate local fragments during the same preflight as external URI + // policy, before an earlier sibling can invoke an application resolver. + if _, _, err := s.resolver.Target(p.ctx, uri); err != nil { + return "", fmt.Errorf("%w: %w", ErrInput, err) + } + return uri, nil } @@ -91,12 +109,12 @@ func (p *parser) resourceURI(s *scope, n *tree.Node, uri string) (string, error) } func (p *parser) keyInfoReferenceURI(s *scope, n *tree.Node) (string, error) { - children, err := structure(n, Namespace11, nameKeyInfoReference, "Id", "URI") + children, err := structure(n, Namespace11, nameKeyInfoReference, lexicon.SecurityAttrID, lexicon.SecurityAttrURI) if err != nil { return "", err } - uri, present := attr(n, "URI") + uri, present := attr(n, lexicon.SecurityAttrURI) if len(children) != 0 || !present { return "", fmt.Errorf("%w: KeyInfoReference URI/structure", ErrInput) } @@ -121,7 +139,7 @@ func (p *parser) consumeResource(uri string, consume func(io.Reader) error) erro reader, err := p.options.External.Resolver.Resolve(p.ctx, uri) if err != nil { - if !nilcheck.IsNil(reader) { + if reader != nil && !nilcheck.IsNil(reader) { err = errors.Join(err, reader.Close()) } @@ -134,7 +152,7 @@ func (p *parser) consumeResource(uri string, consume func(io.Reader) error) erro remaining := p.limits.MaxExternalBytes - p.externalBytes - maximum := min(p.limits.MaxBlobBytes*2, remaining) + maximum := min(p.limits.MaxBlobBytes*encodedBlobSizeFactor, remaining) if p.options.RetrievalProcessing != nil { maximum = min(maximum, p.options.RetrievalProcessing.MaxInputBytes) } @@ -201,11 +219,11 @@ func (p *parser) readCertificate(uri string, decode bool) ([]byte, error) { } func (p *parser) certificateTransforms() reference.Options { - return reference.Options{AllowedTransforms: []string{reference.Base64}, MaxTransforms: 1, MaxInputBytes: p.limits.MaxBlobBytes * 2, MaxOutputBytes: p.limits.MaxBlobBytes, MaxProducedBytes: p.limits.MaxBlobBytes, MaxNodeVisits: p.limits.MaxElements * 8} + return reference.Options{AllowedTransforms: []string{reference.Base64}, MaxTransforms: 1, MaxInputBytes: p.limits.MaxBlobBytes * encodedBlobSizeFactor, MaxOutputBytes: p.limits.MaxBlobBytes, MaxProducedBytes: p.limits.MaxBlobBytes, MaxNodeVisits: p.limits.MaxElements * retrievalXMLNodesPerElement} } func (p *parser) resolveXML(s *scope, uri string) (*tree.Node, *scope, error) { - if len(uri) > 8192 { + if len(uri) > maxURIBytes { return nil, nil, ErrLimit } @@ -249,9 +267,9 @@ func (p *parser) resolveXML(s *scope, uri string) (*tree.Node, *scope, error) { } limits := document.DefaultLimits() - limits.MaxBytes = min(limits.MaxBytes, p.limits.MaxBlobBytes*2) - limits.MaxNodes = min(limits.MaxNodes, p.limits.MaxElements*8) - limits.MaxDepth = min(limits.MaxDepth, 64) + limits.MaxBytes = min(limits.MaxBytes, p.limits.MaxBlobBytes*encodedBlobSizeFactor) + limits.MaxNodes = min(limits.MaxNodes, p.limits.MaxElements*retrievalXMLNodesPerElement) + limits.MaxDepth = min(limits.MaxDepth, maxRetrievedXMLDepth) var d *document.Document @@ -369,7 +387,7 @@ func (p *parser) retrieveRawCertificate(s *scope, n *tree.Node, uri string, dept } func validExternalURI(uri string) bool { - if len(uri) > 8192 || uriref.Validate(uri) != nil { + if len(uri) > maxURIBytes || uriref.Validate(uri) != nil { return false } @@ -379,7 +397,7 @@ func validExternalURI(uri string) bool { } func validKeyURI(uri string) bool { - if len(uri) > 8192 || uriref.Validate(uri) != nil { + if len(uri) > maxURIBytes || uriref.Validate(uri) != nil { return false } @@ -405,12 +423,12 @@ func retrievalTransform(c []*tree.Node, typ string) (bool, error) { return false, fmt.Errorf("%w: RetrievalMethod transform chain", ErrUnsupported) } - body, err := structure(tc[0], Namespace, lexicon.DSigElementTransform, "Algorithm") + body, err := structure(tc[0], Namespace, lexicon.DSigElementTransform, lexicon.SecurityAttrAlgorithm) if err != nil { return false, err } - a, _ := attr(tc[0], "Algorithm") + a, _ := attr(tc[0], lexicon.SecurityAttrAlgorithm) if len(body) != 0 || a != reference.Base64 || typ != RawX509Certificate { return false, fmt.Errorf("%w: only Base64 rawX509Certificate retrieval supported", ErrUnsupported) } diff --git a/keyinfo/references_test.go b/keyinfo/references_test.go index f5d0531..481029f 100644 --- a/keyinfo/references_test.go +++ b/keyinfo/references_test.go @@ -156,13 +156,13 @@ func TestKeyInfoURIContextPolicy(t *testing.T) { } // Complete fragment identities reach the resolver, also for binary keys. - der := cert(t, key) + d := cert(t, key) options.External.Resolver = resolverFunc(func(_ context.Context, uri string) (io.ReadCloser, error) { if uri != allowed { t.Fatal("fragment was discarded", uri) } - return io.NopCloser(bytes.NewReader(der)), nil + return io.NopCloser(bytes.NewReader(d)), nil }) doc = parseXML(t, wrap(``)) @@ -172,6 +172,37 @@ func TestKeyInfoURIContextPolicy(t *testing.T) { } } +func TestFragmentURIValidationBeforeResolver(t *testing.T) { + for _, uri := range []string{"#%", "#%0", "#%zz", "#target key", "#target#key", `#target\key`, "#%FF", "#xpointer(id(target))"} { + for _, representation := range []string{"KeyInfoReference", "RetrievalMethod"} { + for _, explicit := range []bool{false, true} { + t.Run(representation+"/"+strconv.FormatBool(explicit)+"/"+uri, func(t *testing.T) { + later := `` + if representation == "RetrievalMethod" { + later = `` + } + + calls := 0 + options := keyinfo.Options{} + if explicit { + options = retrievalOptions(nil) + } + + options.External = &keyinfo.ExternalResources{AllowedURIs: []string{uriUrnKey}, Resolver: resolverFunc(func(context.Context, string) (io.ReadCloser, error) { + calls++ + return io.NopCloser(strings.NewReader(wrap(`remote`))), nil + })} + requireErr(t, wrap(``+later), options, keyinfo.ErrInput) + + if calls != 0 { + t.Fatal("resolver preceded malformed fragment preflight", calls) + } + }) + } + } + } +} + func TestRetrievalTypesAndPreflight(t *testing.T) { k := ec(t, elliptic.P256()) full := parseXML(t, wrap(derXML(t, &k.PublicKey))) @@ -216,17 +247,6 @@ func TestRetrievalTypesAndPreflight(t *testing.T) { } } -func mustSPKI(t testing.TB, k any) []byte { - t.Helper() - - b, e := x509.MarshalPKIXPublicKey(k) - if e != nil { - t.Fatal(e) - } - - return b -} - func TestDirectCRLRetainedWithoutTrust(t *testing.T) { key := ec(t, elliptic.P256()) now := time.Date(2026, 10, 8, 0, 0, 0, 0, time.UTC) @@ -379,18 +399,6 @@ func TestRetrievalTransformChains(t *testing.T) { }) } -func retrievalOptions(programs []reference.Transform) keyinfo.Options { - processing := reference.DefaultOptions() - - for _, program := range programs { - if reference.IsExtension(program.Algorithm) { - processing.AllowedTransforms = append(processing.AllowedTransforms, program.Algorithm) - } - } - - return keyinfo.Options{RetrievalProcessing: &processing} -} - func TestRetrievalApprovedPrograms(t *testing.T) { key := ec(t, elliptic.P256()) @@ -653,22 +661,6 @@ func TestRetrievalTransformReaderFailures(t *testing.T) { } } -type retrievalCloseFailure struct { - source *reader - err error -} - -func (r *retrievalCloseFailure) Read(dst []byte) (int, error) { return r.source.Read(dst) } -func (r *retrievalCloseFailure) Close() error { _ = r.source.Close(); return r.err } - -func distinguishedAVA(oid asn1.ObjectIdentifier, tag int, text string) []byte { - return der.Seq(der.OID(oid), der.Wrap(0, tag, false, []byte(text))) -} - -func distinguishedCN(tag int, text string) []byte { - return der.Seq(der.Set(distinguishedAVA(asn1.ObjectIdentifier{2, 5, 4, 3}, tag, text))) -} - func TestDistinguishedNameStringPreparation(t *testing.T) { for _, tc := range []struct { name string @@ -881,3 +873,42 @@ func TestCRLConfiguredBlobBudget(t *testing.T) { t.Fatal("configured larger CRL cap narrowed", err) } } + +func mustSPKI(t testing.TB, k any) []byte { + t.Helper() + + b, e := x509.MarshalPKIXPublicKey(k) + if e != nil { + t.Fatal(e) + } + + return b +} + +func retrievalOptions(programs []reference.Transform) keyinfo.Options { + processing := reference.DefaultOptions() + + for _, program := range programs { + if reference.IsExtension(program.Algorithm) { + processing.AllowedTransforms = append(processing.AllowedTransforms, program.Algorithm) + } + } + + return keyinfo.Options{RetrievalProcessing: &processing} +} + +type retrievalCloseFailure struct { + source *reader + err error +} + +func (r *retrievalCloseFailure) Read(dst []byte) (int, error) { return r.source.Read(dst) } +func (r *retrievalCloseFailure) Close() error { _ = r.source.Close(); return r.err } + +func distinguishedAVA(oid asn1.ObjectIdentifier, tag int, text string) []byte { + return der.Seq(der.OID(oid), der.Wrap(0, tag, false, []byte(text))) +} + +func distinguishedCN(tag int, text string) []byte { + return der.Seq(der.Set(distinguishedAVA(asn1.ObjectIdentifier{2, 5, 4, 3}, tag, text))) +} diff --git a/keyinfo/retrieval.go b/keyinfo/retrieval.go index 2b5b3f5..d68ded9 100644 --- a/keyinfo/retrieval.go +++ b/keyinfo/retrieval.go @@ -23,7 +23,7 @@ type retrievalPlan struct { } type retrievalOutput struct { - digest [32]byte + digest [sha256.Size]byte resource string } @@ -50,9 +50,9 @@ func (p *parser) configureRetrieval() error { fallback int maximum int }{ - {value: &o.MaxTransforms, fallback: defaults.MaxTransforms, maximum: 64}, - {value: &o.MaxInputBytes, fallback: defaults.MaxInputBytes, maximum: p.limits.MaxBlobBytes * 2}, - {value: &o.MaxOutputBytes, fallback: defaults.MaxOutputBytes, maximum: p.limits.MaxBlobBytes * 2}, + {value: &o.MaxTransforms, fallback: defaults.MaxTransforms, maximum: maxRetrievalTransforms}, + {value: &o.MaxInputBytes, fallback: defaults.MaxInputBytes, maximum: p.limits.MaxBlobBytes * encodedBlobSizeFactor}, + {value: &o.MaxOutputBytes, fallback: defaults.MaxOutputBytes, maximum: p.limits.MaxBlobBytes * encodedBlobSizeFactor}, {value: &o.MaxProducedBytes, fallback: defaults.MaxProducedBytes, maximum: p.limits.MaxTransformBytes}, {value: &o.MaxNodeVisits, fallback: defaults.MaxNodeVisits, maximum: p.limits.MaxTransformNodes}, } { @@ -67,10 +67,10 @@ func (p *parser) configureRetrieval() error { o.XMLLimits = document.DefaultLimits() } - o.XMLLimits.MaxBytes = min(o.XMLLimits.MaxBytes, p.limits.MaxBlobBytes*2) - o.XMLLimits.MaxNodes = min(o.XMLLimits.MaxNodes, p.limits.MaxElements*8, p.limits.MaxTransformNodes) + o.XMLLimits.MaxBytes = min(o.XMLLimits.MaxBytes, p.limits.MaxBlobBytes*encodedBlobSizeFactor) + o.XMLLimits.MaxNodes = min(o.XMLLimits.MaxNodes, p.limits.MaxElements*retrievalXMLNodesPerElement, p.limits.MaxTransformNodes) o.XMLLimits.MaxNodeSetNodes = min(o.XMLLimits.MaxNodeSetNodes, p.limits.MaxTransformNodes) - o.XMLLimits.MaxDepth = min(o.XMLLimits.MaxDepth, 64) + o.XMLLimits.MaxDepth = min(o.XMLLimits.MaxDepth, maxRetrievedXMLDepth) p.options.RetrievalProcessing = &o return nil @@ -84,23 +84,23 @@ func (p *parser) prepareRetrieval(s *scope, n *tree.Node) (retrievalPlan, error) var plan retrievalPlan - children, err := structure(n, Namespace, "RetrievalMethod", "URI", "Type") + children, err := structure(n, Namespace, lexicon.DSigElementRetrievalMethod, lexicon.SecurityAttrURI, lexicon.SecurityAttrType) if err != nil { return plan, err } var ok bool - plan.uri, ok = attr(n, "URI") + plan.uri, ok = attr(n, lexicon.SecurityAttrURI) if !ok { return plan, fmt.Errorf("%w: RetrievalMethod URI is required", ErrInput) } - if len(plan.uri) > 8192 { + if len(plan.uri) > maxURIBytes { return plan, ErrLimit } - plan.typ, _ = attr(n, "Type") + plan.typ, _ = attr(n, lexicon.SecurityAttrType) representation, err := retrievalType(plan.typ) if err != nil { @@ -286,19 +286,22 @@ func (p *parser) processRetrieval(s *scope, n *tree.Node, depth int, plan retrie } func (p *parser) retrievalScope(data []byte, resource string) (*scope, error) { + // Charge parsing and indexing each expanded node of a transformed XML snapshot. + const transformNodeVisitCost = 2 + key := retrievalOutput{digest: sha256.Sum256(data), resource: resource} if cached, ok := p.transformedScopes[key]; ok { return cached, nil } remaining := p.limits.MaxTransformNodes - p.transformNodes - if remaining < 2 { + if remaining < transformNodeVisitCost { return nil, ErrLimit } limits := p.options.RetrievalProcessing.XMLLimits - limits.MaxNodes = min(limits.MaxNodes, remaining/2) - limits.MaxNodeSetNodes = min(limits.MaxNodeSetNodes, remaining/2) + limits.MaxNodes = min(limits.MaxNodes, remaining/transformNodeVisitCost) + limits.MaxNodeSetNodes = min(limits.MaxNodeSetNodes, remaining/transformNodeVisitCost) doc, err := document.Parse(p.ctx, data, limits) if err != nil { @@ -311,11 +314,11 @@ func (p *parser) retrievalScope(data []byte, resource string) (*scope, error) { } // Account for parsing and indexing the resulting snapshot, including its // expanded namespace/attribute nodes. Cached snapshots keep graph identity. - if nodes.Len() > remaining/2 { + if nodes.Len() > remaining/transformNodeVisitCost { return nil, ErrLimit } - p.transformNodes += 2 * nodes.Len() + p.transformNodes += transformNodeVisitCost * nodes.Len() next, err := p.newScope(doc, resource) if err != nil { diff --git a/keyinfo/types.go b/keyinfo/types.go index b348385..97f2a41 100644 --- a/keyinfo/types.go +++ b/keyinfo/types.go @@ -20,6 +20,43 @@ import ( "github.com/skarm/xmlsec/reference" ) +const ( + // Default byte cap for one decoded key, certificate or CRL; builders use the same default profile. + defaultMaxBlobBytes = 1 << 20 + // Default candidate-count cap used by KeyInfo parsing and by the builder profile. + defaultMaxCandidates = 32 + // Default bound on element visits, including repeated reference visits. + defaultMaxElements = 256 + // Default maximum RSA modulus size, in bits; minimum key strength is a separate trust policy. + defaultMaxRSABits = 8192 + // Default aggregate byte budget for decoded blobs and text in one KeyInfo operation. + defaultMaxTotalBytes = 4 << 20 +) + +const ( + // Zero fields select these reference, external-resource and transform defaults. + // Byte budgets cover the complete operation; node counts include repeated visits. + defaultMaxDepth = 8 + defaultMaxExternalBytes = 4 << 20 + defaultMaxExternalResources = 8 + defaultMaxTransformBytes = 32 << 20 + defaultMaxTransformNodes = 2_000_000 + // Explicit settings cannot exceed these hard ceilings. RSA sizes are in bits; + // blob/total/external/transform sizes are bytes, and the remaining caps are counts. + maximumBlobBytes = 8 << 20 + maximumCandidates = 256 + maximumDepth = 32 + maximumElements = 4096 + maximumExternalBytes = 64 << 20 + maximumExternalResources = 64 + maximumRSABits = 16384 + maximumTotalBytes = 64 << 20 + maximumTransformBytes = 64 << 20 + maximumTransformNodes = 16_000_000 + // Structural parser floor, independently of the application's key-strength policy. + minimumRSABits = 512 +) + const ( // Namespace is the XMLDSig 1.0 namespace URI. Namespace = lexicon.NamespaceDSig @@ -147,11 +184,16 @@ type Options struct { // Its accessors copy mutable key objects, DER and certificate data. Concurrent // policy callbacks can read the same Candidate safely. type Candidate struct { - spki []byte + // spki owns the parsed key's SubjectPublicKeyInfo DER. + spki []byte + // certificate optionally retains the exact untrusted certificate DER. certificate []byte - source document.Element - resource string - kind document.Name + // source identifies the immutable XML element describing this key. + source document.Element + // resource is the external URI, or empty for local key data. + resource string + // kind is the expanded name of the key representation. + kind document.Name } // Valid returns whether this value is initialized. @@ -206,18 +248,42 @@ func (c Candidate) FingerprintSHA256() string { return hex.EncodeToString(h[:]) } +// IssuerSerial pairs an unverified X.509 issuer distinguished name with a decimal +// certificate serial number. +type IssuerSerial struct { + // Issuer is the issuer distinguished name in XMLDSig/RFC 4514 spelling. + Issuer string + // Serial is the certificate serial number in nonnegative decimal notation. + Serial string +} + +// CertificateDigest pairs unverified digest bytes with a digest algorithm URI. +type CertificateDigest struct { + // Algorithm is the digest method URI. + Algorithm string + // Value contains the raw digest bytes, without Base64 encoding. + Value []byte +} + // X509Group retains certificates and selectors from one X509Data element. // Names and distinguished-name spellings are untrusted hints. The parser does not // normalize or compare those strings to establish trust. Application policy must // resolve selectors and validate certificate relationships. type X509Group struct { - source document.Element - certs [][]byte - crls [][]byte - issuers []IssuerSerial + // source identifies the immutable X509Data element. + source document.Element + // certs retains untrusted certificate DER encodings. + certs [][]byte + // crls retains untrusted CRL DER encodings. + crls [][]byte + // issuers retains issuer-and-serial certificate selectors. + issuers []IssuerSerial + // subjects retains subject-name certificate selectors. subjects []string - skis [][]byte - digests []CertificateDigest + // skis retains subject key identifier selectors. + skis [][]byte + // digests retains certificate digest selectors. + digests []CertificateDigest } // Source returns the immutable XML source handle. @@ -248,39 +314,17 @@ func (g X509Group) CertificateDigests() []CertificateDigest { return out } -func cloneBlobs(a [][]byte) [][]byte { - out := make([][]byte, len(a)) - for i, b := range a { - out[i] = bytes.Clone(b) - } - - return out -} - -// IssuerSerial pairs an unverified X.509 issuer distinguished name with a decimal -// certificate serial number. -type IssuerSerial struct { - // Issuer is the issuer distinguished name in XMLDSig/RFC 4514 spelling. - Issuer string - // Serial is the certificate serial number in nonnegative decimal notation. - Serial string -} - -// CertificateDigest pairs unverified digest bytes with a digest algorithm URI. -type CertificateDigest struct { - // Algorithm is the digest method URI. - Algorithm string - // Value contains the raw digest bytes, without Base64 encoding. - Value []byte -} - // Result contains untrusted decoded key hints. A successful parse provides neither // a validity flag nor a trust decision. Parse returns no partial Result on error. type Result struct { + // candidates contains parsed key descriptions without granting trust. candidates []Candidate - names []string - groups []X509Group - external []string + // names retains KeyName values for application policy. + names []string + // groups preserves the grouping of related X509Data values. + groups []X509Group + // external records external resource URIs used during parsing. + external []string } // Candidates returns an owned slice of immutable untrusted key candidates. @@ -336,22 +380,31 @@ func (r *Result) CheckKeyDeclarations(selected int) error { return nil } +func cloneBlobs(a [][]byte) [][]byte { + out := make([][]byte, len(a)) + for i, b := range a { + out[i] = bytes.Clone(b) + } + + return out +} + func normalizeLimits(l Limits) (Limits, error) { fields := []struct { p *int def int cap int }{ - {&l.MaxDepth, 8, 32}, - {&l.MaxElements, 256, 4096}, - {&l.MaxCandidates, 32, 256}, - {&l.MaxBlobBytes, 1 << 20, 8 << 20}, - {&l.MaxTotalBytes, 4 << 20, 64 << 20}, - {&l.MaxExternalResources, 8, 64}, - {&l.MaxExternalBytes, 4 << 20, 64 << 20}, - {&l.MaxTransformBytes, 32 << 20, 64 << 20}, - {&l.MaxTransformNodes, 2_000_000, 16_000_000}, - {&l.MaxRSABits, 8192, 16384}, + {&l.MaxDepth, defaultMaxDepth, maximumDepth}, + {&l.MaxElements, defaultMaxElements, maximumElements}, + {&l.MaxCandidates, defaultMaxCandidates, maximumCandidates}, + {&l.MaxBlobBytes, defaultMaxBlobBytes, maximumBlobBytes}, + {&l.MaxTotalBytes, defaultMaxTotalBytes, maximumTotalBytes}, + {&l.MaxExternalResources, defaultMaxExternalResources, maximumExternalResources}, + {&l.MaxExternalBytes, defaultMaxExternalBytes, maximumExternalBytes}, + {&l.MaxTransformBytes, defaultMaxTransformBytes, maximumTransformBytes}, + {&l.MaxTransformNodes, defaultMaxTransformNodes, maximumTransformNodes}, + {&l.MaxRSABits, defaultMaxRSABits, maximumRSABits}, } for _, f := range fields { if *f.p == 0 { @@ -363,7 +416,7 @@ func normalizeLimits(l Limits) (Limits, error) { } } - if l.MaxRSABits < 512 { + if l.MaxRSABits < minimumRSABits { return l, fmt.Errorf("%w: MaxRSABits must be >=512", ErrPolicy) } diff --git a/keyinfo/x509.go b/keyinfo/x509.go index f8dbb29..ba92b09 100644 --- a/keyinfo/x509.go +++ b/keyinfo/x509.go @@ -2,6 +2,8 @@ package keyinfo import ( "context" + "crypto/sha256" + "crypto/sha512" "fmt" "strings" @@ -12,6 +14,14 @@ import ( "github.com/skarm/xmlsec/internal/x509crl" ) +const ( + nameX509Digest = "X509Digest" + nameX509IssuerName = "X509IssuerName" + nameX509IssuerSerial = "X509IssuerSerial" + nameX509SerialNumber = "X509SerialNumber" + nameX509SubjectName = "X509SubjectName" +) + func (p *parser) certificate(s *scope, n *tree.Node, der []byte) error { c, err := x509cert.ParseWithMaxBytes(der, p.limits.MaxBlobBytes) if err != nil { @@ -74,7 +84,7 @@ func (p *parser) x509DataItem(s *scope, n *tree.Node, group *X509Group) error { } group.crls = append(group.crls, der) - case document.Name{Space: Namespace, Local: "X509SubjectName"}: + case document.Name{Space: Namespace, Local: nameX509SubjectName}: name, err := p.stringValue(n) if err != nil { return err @@ -92,14 +102,14 @@ func (p *parser) x509DataItem(s *scope, n *tree.Node, group *X509Group) error { } group.skis = append(group.skis, keyID) - case document.Name{Space: Namespace, Local: "X509IssuerSerial"}: + case document.Name{Space: Namespace, Local: nameX509IssuerSerial}: issuer, err := p.x509IssuerSerial(n) if err != nil { return err } group.issuers = append(group.issuers, issuer) - case document.Name{Space: Namespace11, Local: "X509Digest"}: + case document.Name{Space: Namespace11, Local: nameX509Digest}: digest, err := p.x509Digest(n) if err != nil { return err @@ -114,12 +124,12 @@ func (p *parser) x509DataItem(s *scope, n *tree.Node, group *X509Group) error { } func (p *parser) x509IssuerSerial(n *tree.Node) (IssuerSerial, error) { - children, err := structure(n, Namespace, "X509IssuerSerial") + children, err := structure(n, Namespace, nameX509IssuerSerial) if err != nil { return IssuerSerial{}, err } - if len(children) != 2 || !is(children[0], Namespace, "X509IssuerName") || !is(children[1], Namespace, "X509SerialNumber") { + if len(children) != 2 || !is(children[0], Namespace, nameX509IssuerName) || !is(children[1], Namespace, nameX509SerialNumber) { return IssuerSerial{}, ErrInput } @@ -137,7 +147,7 @@ func (p *parser) x509IssuerSerial(n *tree.Node) (IssuerSerial, error) { } func (p *parser) x509Digest(n *tree.Node) (CertificateDigest, error) { - algorithm, ok := attr(n, "Algorithm") + algorithm, ok := attr(n, lexicon.SecurityAttrAlgorithm) if !ok { return CertificateDigest{}, ErrInput } @@ -147,7 +157,7 @@ func (p *parser) x509Digest(n *tree.Node) (CertificateDigest, error) { return CertificateDigest{}, fmt.Errorf("%w: certificate digest algorithm", ErrUnsupported) } - value, err := p.binary(n, "Algorithm") + value, err := p.binary(n, lexicon.SecurityAttrAlgorithm) if err != nil { return CertificateDigest{}, err } @@ -173,7 +183,7 @@ func (p *parser) tick() error { } func normalizeIssuerSerial(issuer, serial string) (IssuerSerial, error) { - serial = strings.Trim(serial, " \t\r\n") + serial = strings.Trim(serial, lexicon.XMLWhitespace) serial = strings.TrimPrefix(serial, "+") if serial == "" || strings.TrimSpace(issuer) == "" { @@ -235,7 +245,7 @@ func x509SourceBudget(group X509Data, charge func(int) error) error { } func buildX509Data(ctx context.Context, root *tree.Node, group X509Data) error { - n := element(Namespace, "ds", nameX509Data) + n := element(Namespace, lexicon.PrefixDSig, nameX509Data) for _, issuer := range group.IssuerSerials { value, err := normalizeIssuerSerial(issuer.Issuer, issuer.Serial) @@ -243,9 +253,9 @@ func buildX509Data(ctx context.Context, root *tree.Node, group X509Data) error { return err } - pair := element(Namespace, "ds", "X509IssuerSerial") - addString(pair, "X509IssuerName", value.Issuer) - addString(pair, "X509SerialNumber", value.Serial) + pair := element(Namespace, lexicon.PrefixDSig, nameX509IssuerSerial) + addString(pair, nameX509IssuerName, value.Issuer) + addString(pair, nameX509SerialNumber, value.Serial) add(n, pair) } @@ -254,7 +264,7 @@ func buildX509Data(ctx context.Context, root *tree.Node, group X509Data) error { return ErrInput } - addString(n, "X509SubjectName", subject) + addString(n, nameX509SubjectName, subject) } if err := buildX509Blobs(ctx, n, group); err != nil { @@ -271,8 +281,8 @@ func buildX509Data(ctx context.Context, root *tree.Node, group X509Data) error { return ErrInput } - child := element(Namespace11, "dsig11", "X509Digest") - set(child, "Algorithm", digest.Algorithm) + child := element(Namespace11, lexicon.PrefixDSig11, nameX509Digest) + set(child, lexicon.SecurityAttrAlgorithm, digest.Algorithm) encoded, err := tree.EncodeBase64(ctx, digest.Value) if err != nil { @@ -289,7 +299,7 @@ func buildX509Data(ctx context.Context, root *tree.Node, group X509Data) error { } func addString(parent *tree.Node, local, value string) { - n := element(Namespace, "ds", local) + n := element(Namespace, lexicon.PrefixDSig, local) addText(n, value) add(parent, n) } @@ -323,7 +333,7 @@ func buildX509Blobs(ctx context.Context, node *tree.Node, group X509Data) error } } - if err := blob(ctx, node, Namespace, "ds", field.name, value); err != nil { + if err := blob(ctx, node, Namespace, lexicon.PrefixDSig, field.name, value); err != nil { return err } } @@ -333,15 +343,17 @@ func buildX509Blobs(ctx context.Context, node *tree.Node, group X509Data) error } func x509DigestSize(algorithm string) int { + const legacySHA1DigestBytes = 20 + switch algorithm { case lexicon.DigestSHA1: - return 20 + return legacySHA1DigestBytes case lexicon.DigestSHA256: - return 32 + return sha256.Size case lexicon.DigestSHA384: - return 48 + return sha512.Size384 case lexicon.DigestSHA512: - return 64 + return sha512.Size default: return 0 } diff --git a/keys/limits.go b/keys/limits.go index 7e85836..8cdf9f6 100644 --- a/keys/limits.go +++ b/keys/limits.go @@ -7,6 +7,26 @@ import ( "github.com/skarm/xmlsec/internal/der" ) +const ( + defaultMaxASN1Depth = 32 + defaultMaxASN1Nodes = 1000000 + defaultMaxBags = 32 + defaultMaxCRLBytes = 16 << 20 + defaultMaxCRLEntries = 100000 + defaultMaxCertificateBytes = 1 << 20 + defaultMaxCertificates = 16 + defaultMaxContainerBytes = 16 << 20 + defaultMaxContentInfos = 8 + defaultMaxInputBytes = 32 << 20 + defaultMaxIterations = 1000000 + defaultMaxKDFWork = 3000000 + defaultMaxKeyBytes = 64 << 10 + defaultMaxPasswordBytes = 1024 + defaultMaxRSABits = 16384 + defaultMaxRSAPrimes = 8 + defaultMinIterations = 1000 +) + // Limits bounds key-material processing. Each zero field selects its default; // negative values are rejected. Budgets are independent of algorithm strength // and certificate trust policy. Parsing and export use the same limits. @@ -57,23 +77,23 @@ type Limits struct { // DefaultLimits returns independent resource budgets for key material, containers and KDFs. func DefaultLimits() Limits { return Limits{ - MaxInputBytes: 32 << 20, - MaxKeyBytes: 64 << 10, - MaxCertificateBytes: 1 << 20, - MaxCRLBytes: 16 << 20, - MaxContainerBytes: 16 << 20, - MaxASN1Depth: 32, - MaxASN1Nodes: 1000000, - MaxRSABits: 16384, - MaxRSAPrimes: 8, - MaxCertificates: 16, - MaxBags: 32, - MaxContentInfos: 8, - MaxCRLEntries: 100000, - MaxPasswordBytes: 1024, - MinIterations: 1000, - MaxIterations: 1000000, - MaxKDFWork: 3000000, + MaxInputBytes: defaultMaxInputBytes, + MaxKeyBytes: defaultMaxKeyBytes, + MaxCertificateBytes: defaultMaxCertificateBytes, + MaxCRLBytes: defaultMaxCRLBytes, + MaxContainerBytes: defaultMaxContainerBytes, + MaxASN1Depth: defaultMaxASN1Depth, + MaxASN1Nodes: defaultMaxASN1Nodes, + MaxRSABits: defaultMaxRSABits, + MaxRSAPrimes: defaultMaxRSAPrimes, + MaxCertificates: defaultMaxCertificates, + MaxBags: defaultMaxBags, + MaxContentInfos: defaultMaxContentInfos, + MaxCRLEntries: defaultMaxCRLEntries, + MaxPasswordBytes: defaultMaxPasswordBytes, + MinIterations: defaultMinIterations, + MaxIterations: defaultMaxIterations, + MaxKDFWork: defaultMaxKDFWork, } } diff --git a/keys/parse.go b/keys/parse.go index 70b82d0..2d6c902 100644 --- a/keys/parse.go +++ b/keys/parse.go @@ -14,12 +14,32 @@ import ( "math/bits" "github.com/skarm/xmlsec/internal/der" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/x509cert" "github.com/skarm/xmlsec/internal/x509crl" ) +const ( + crlPEMType = "X509 CRL" + ecPrivateKeyPEMType = "EC PRIVATE KEY" + // PEM boundary prefix shared by material detection and private-key export. + pemBeginMarker = "-----BEGIN " + pemEndMarker = "-----END " + pemHorizontalWhitespace = " \t" + pemMarkerSuffix = "-----" + rsaPrivateKeyPEMType = "RSA PRIVATE KEY" + rsaPublicKeyPEMType = "RSA PUBLIC KEY" + materialReadChunkBytes = 4096 + maximumPEMLabelBytes = 64 + minimumPEMBoundaryBytes = len(pemBeginMarker) + len(pemMarkerSuffix) + pemCancellationMask = 4095 +) + const certificatePEMType = "CERTIFICATE" +// bitsPerOctet converts encoded key bytes to cryptographic bit lengths. +const bitsPerOctet = 8 + var ( // ErrFormat reports malformed, ambiguous or unsupported key material. ErrFormat = errors.New("invalid or unsupported key material") @@ -51,6 +71,8 @@ func ParsePrivate(data []byte, limits Limits) (crypto.Signer, error) { // public key from one X.509 certificate, in PEM or DER. It does not validate // certificate trust, lifetime or revocation. Returned data does not alias input. func ParsePublic(data []byte, limits Limits) (crypto.PublicKey, error) { + const certificateOuterFields = 3 + limits, err := normalizeLimits(limits) if err != nil { return nil, err @@ -69,12 +91,12 @@ func ParsePublic(data []byte, limits Limits) (crypto.PublicKey, error) { return nil, err } - fields, err := der.Sequence(value, 2, 3) + fields, err := der.Sequence(value, 2, certificateOuterFields) if err != nil { return nil, derError(err) } - certificate := label == certificatePEMType || label == "" && len(fields) == 3 + certificate := label == certificatePEMType || label == "" && len(fields) == certificateOuterFields if !certificate && len(raw) > limits.MaxKeyBytes { return nil, ErrLimit } @@ -88,9 +110,9 @@ func ParsePublic(data []byte, limits Limits) (crypto.PublicKey, error) { var key any switch label { - case "PUBLIC KEY": + case lexicon.PEMPublicKey: key, err = x509.ParsePKIXPublicKey(raw) - case "RSA PUBLIC KEY": + case rsaPublicKeyPEMType: key, err = x509.ParsePKCS1PublicKey(raw) case certificatePEMType: return certificateKey(raw, limits) @@ -160,7 +182,7 @@ func ParseCRL(data []byte, limits Limits) (*x509.RevocationList, error) { return nil, err } - if decoded.label != "" && decoded.label != "X509 CRL" { + if decoded.label != "" && decoded.label != crlPEMType { return nil, ErrFormat } @@ -187,11 +209,11 @@ func parsePrivateDER(label string, raw []byte, limits Limits) (crypto.Signer, er ) switch label { - case "PRIVATE KEY": + case lexicon.PEMPrivateKey: key, err = x509.ParsePKCS8PrivateKey(raw) - case "RSA PRIVATE KEY": + case rsaPrivateKeyPEMType: key, err = x509.ParsePKCS1PrivateKey(raw) - case "EC PRIVATE KEY": + case ecPrivateKeyPEMType: key, err = x509.ParseECPrivateKey(raw) case "": key, err = x509.ParsePKCS8PrivateKey(raw) @@ -225,33 +247,36 @@ func parsePrivateDER(label string, raw []byte, limits Limits) (crypto.Signer, er // Bound private RSA integers before x509 validates primes and builds CRT values. // PKCS#8 carries a nested DER key inside its privateKey OCTET STRING. func privateBudget(raw []byte, limits Limits) error { + // Early wrapper inspection cap; complete schema validation follows parsing. + const maximumBudgetWrapperFields = 16 + v, err := readDER(raw, limits.MaxKeyBytes, limits) if err != nil { return err } - fields, err := der.Sequence(v, 2, 16) + fields, err := der.Sequence(v, 2, maximumBudgetWrapperFields) if err != nil { return derError(err) } - if len(fields) >= 3 && der.Is(fields[1], 0, asn1.TagSequence, true) && der.Is(fields[2], 0, asn1.TagOctetString, false) { + if len(fields) >= privateKeyInfoMinimumFields && der.Is(fields[1], asn1.ClassUniversal, asn1.TagSequence, true) && der.Is(fields[2], asn1.ClassUniversal, asn1.TagOctetString, false) { v, err = readDER(fields[2].Bytes, limits.MaxKeyBytes, limits) if err != nil { return err } // Ed25519's inner value is an OCTET STRING; EC SEC1 has an OCTET STRING second field. - if !der.Is(v, 0, asn1.TagSequence, true) { + if !der.Is(v, asn1.ClassUniversal, asn1.TagSequence, true) { return nil } - fields, err = der.Sequence(v, 2, 16) + fields, err = der.Sequence(v, 2, maximumBudgetWrapperFields) if err != nil { return derError(err) } } - if der.Is(fields[0], 0, asn1.TagInteger, false) && der.Is(fields[1], 0, asn1.TagInteger, false) { + if der.Is(fields[0], asn1.ClassUniversal, asn1.TagInteger, false) && der.Is(fields[1], asn1.ClassUniversal, asn1.TagInteger, false) { return integerBudget(v, limits) } @@ -259,7 +284,13 @@ func privateBudget(raw []byte, limits Limits) error { } func integerBudget(v asn1.RawValue, limits Limits) error { - if der.Is(v, 0, asn1.TagInteger, false) { + // PKCS#1 has nine base fields and one optional OtherPrimeInfos field. + const ( + rsaPrivateBaseFields = 9 + rsaPrivateMaximumFields = rsaPrivateBaseFields + 1 + ) + + if der.Is(v, asn1.ClassUniversal, asn1.TagInteger, false) { if !integerWithinBits(v.Bytes, limits.MaxRSABits) { return ErrLimit } @@ -272,13 +303,13 @@ func integerBudget(v asn1.RawValue, limits Limits) error { } // PKCS#1's additional primes otherwise allow an attacker to multiply a // large collection during x509 validation despite bounded components. - if der.Is(v, 0, asn1.TagSequence, true) && len(children) >= 9 && der.Is(children[0], 0, asn1.TagInteger, false) && der.Is(children[1], 0, asn1.TagInteger, false) && der.Is(children[2], 0, asn1.TagInteger, false) { - if len(children) > 10 { + if der.Is(v, asn1.ClassUniversal, asn1.TagSequence, true) && len(children) >= rsaPrivateBaseFields && der.Is(children[0], asn1.ClassUniversal, asn1.TagInteger, false) && der.Is(children[1], asn1.ClassUniversal, asn1.TagInteger, false) && der.Is(children[2], asn1.ClassUniversal, asn1.TagInteger, false) { + if len(children) > rsaPrivateMaximumFields { return ErrFormat } - if len(children) == 10 { - if _, err := der.Sequence(children[9], 0, limits.MaxRSAPrimes-2); err != nil { + if len(children) == rsaPrivateMaximumFields { + if _, err := der.Sequence(children[rsaPrivateBaseFields], 0, limits.MaxRSAPrimes-2); err != nil { return ErrLimit } } @@ -303,11 +334,11 @@ func integerWithinBits(value []byte, maximum int) bool { return true } - if len(value)-1 > maximum/8 { + if len(value)-1 > maximum/bitsPerOctet { return false } - return (len(value)-1)*8+bits.Len8(value[0]) <= maximum + return (len(value)-1)*bitsPerOctet+bits.Len8(value[0]) <= maximum } func certificateKey(raw []byte, limits Limits) (crypto.PublicKey, error) { @@ -335,32 +366,40 @@ func certificateKey(raw []byte, limits Limits) (crypto.PublicKey, error) { } func crlBudget(raw []byte, limits Limits) error { + // CertificateList has a fixed envelope; TBSCertList starts with three + // mandatory fields, alongside up to four optional fields. + const ( + certificateListFields = 3 + tbsRequiredFields = 3 + tbsMaximumFields = 7 + ) + value, err := readDER(raw, limits.MaxCRLBytes, limits) if err != nil { return err } - fields, err := der.Sequence(value, 3, 3) + fields, err := der.Sequence(value, certificateListFields, certificateListFields) if err != nil { return derError(err) } - body, err := der.Sequence(fields[0], 3, 7) + body, err := der.Sequence(fields[0], tbsRequiredFields, tbsMaximumFields) if err != nil { return derError(err) } index := 0 - if der.Is(body[0], 0, asn1.TagInteger, false) { + if der.Is(body[0], asn1.ClassUniversal, asn1.TagInteger, false) { index++ } // Signature algorithm, issuer and thisUpdate precede optional nextUpdate. - index += 3 - if index < len(body) && (der.Is(body[index], 0, asn1.TagUTCTime, false) || der.Is(body[index], 0, asn1.TagGeneralizedTime, false)) { + index += tbsRequiredFields + if index < len(body) && (der.Is(body[index], asn1.ClassUniversal, asn1.TagUTCTime, false) || der.Is(body[index], asn1.ClassUniversal, asn1.TagGeneralizedTime, false)) { index++ } - if index < len(body) && der.Is(body[index], 0, asn1.TagSequence, true) { + if index < len(body) && der.Is(body[index], asn1.ClassUniversal, asn1.TagSequence, true) { contents := body[index].Bytes for count := 0; len(contents) > 0; count++ { if count >= limits.MaxCRLEntries { @@ -418,7 +457,7 @@ func material(ctx context.Context, data []byte, maximum int, limits Limits) (dec m := decodedMaterial{} - if bytes.HasPrefix(trimmed, []byte("-----BEGIN ")) { + if bytes.HasPrefix(trimmed, []byte(pemBeginMarker)) { var err error m, err = decodePEM(ctx, trimmed, maximum) @@ -446,22 +485,22 @@ func material(ctx context.Context, data []byte, maximum int, limits Limits) (dec // before allocation. Base64 whitespace is skipped without copying encoded text. func decodePEM(ctx context.Context, data []byte, maximum int) (decodedMaterial, error) { line, body, ok := pemLine(data) - if !ok || !bytes.HasSuffix(line, []byte("-----")) || len(line) < 16 || bytes.Count(data, []byte("-----BEGIN ")) != 1 { + if !ok || !bytes.HasSuffix(line, []byte(pemMarkerSuffix)) || len(line) < minimumPEMBoundaryBytes || bytes.Count(data, []byte(pemBeginMarker)) != 1 { return decodedMaterial{}, ErrFormat } - label := line[11 : len(line)-5] - if len(label) == 0 || len(label) > 64 { + label := line[len(pemBeginMarker) : len(line)-len(pemMarkerSuffix)] + if len(label) == 0 || len(label) > maximumPEMLabelBytes { return decodedMaterial{}, ErrFormat } - end := bytes.Index(body, []byte("\n-----END ")) - if end < 0 { + end := bytes.Index(body, []byte(pemEndMarker)) + if end < 1 || body[end-1] != '\r' && body[end-1] != '\n' { return decodedMaterial{}, ErrFormat } - trailer, rest, _ := pemLine(body[end+1:]) - if len(trailer) != 9+len(label)+5 || !bytes.Equal(trailer[9:len(trailer)-5], label) || !bytes.HasSuffix(trailer, []byte("-----")) || len(bytes.TrimSpace(rest)) != 0 { + trailer, rest, _ := pemLine(body[end:]) + if len(trailer) != len(pemEndMarker)+len(label)+len(pemMarkerSuffix) || !bytes.Equal(trailer[len(pemEndMarker):len(trailer)-len(pemMarkerSuffix)], label) || !bytes.HasSuffix(trailer, []byte(pemMarkerSuffix)) || len(bytes.TrimSpace(rest)) != 0 { return decodedMaterial{}, ErrFormat } @@ -498,19 +537,32 @@ func decodePEM(ctx context.Context, data []byte, maximum int) (decodedMaterial, func pemSpace(c byte) bool { return c == ' ' || c == '\t' || c == '\r' || c == '\n' } func pemLine(data []byte) ([]byte, []byte, bool) { - line, rest, newline := bytes.Cut(data, []byte{'\n'}) - if newline && len(line) > 0 && line[len(line)-1] == '\r' { - line = line[:len(line)-1] + // RFC 7468 section 3 defines eol as CRLF / CR / LF. + end := bytes.IndexAny(data, "\r\n") + if end < 0 { + return bytes.TrimRight(data, pemHorizontalWhitespace), nil, false } - return bytes.TrimRight(line, " \t"), rest, newline + line, rest := data[:end], data[end+1:] + if data[end] == '\r' && len(rest) > 0 && rest[0] == '\n' { + rest = rest[1:] + } + + return bytes.TrimRight(line, pemHorizontalWhitespace), rest, true } func pemDecodedSize(ctx context.Context, data []byte, maximum int) (int, error) { + // Standard Base64 maps three bytes to four symbols with at most two pads. + const ( + encodedQuantumSymbols = 4 + decodedQuantumBytes = 3 + maximumPaddingSymbols = 2 + ) + count, padding := 0, 0 for i, c := range data { - if i&4095 == 0 { + if i&pemCancellationMask == 0 { if err := ctx.Err(); err != nil { return 0, err } @@ -529,11 +581,11 @@ func pemDecodedSize(ctx context.Context, data []byte, maximum int) (int, error) } } - if count%4 != 0 || padding > 2 || count == 0 { + if count%encodedQuantumSymbols != 0 || padding > maximumPaddingSymbols || count == 0 { return 0, ErrFormat } - size := count/4*3 - padding + size := count/encodedQuantumSymbols*decodedQuantumBytes - padding if size > maximum { return 0, ErrLimit } @@ -553,7 +605,7 @@ func (r *pemBodyReader) Read(dst []byte) (int, error) { return used, err } - span := r.data[:min(len(r.data), 4096)] + span := r.data[:min(len(r.data), materialReadChunkBytes)] taken := 0 for taken < len(span) && used < len(dst) { diff --git a/keys/parse_test.go b/keys/parse_test.go index a04d9bd..869795c 100644 --- a/keys/parse_test.go +++ b/keys/parse_test.go @@ -28,8 +28,8 @@ import ( ) const ( - textENCRYPTEDPRIVATEKEY = "ENCRYPTED PRIVATE KEY" - privateKeyPEMType = "PRIVATE KEY" + encryptedPrivateKeyPEMType = "ENCRYPTED PRIVATE KEY" + privateKeyPEMType = "PRIVATE KEY" ) type keyFormatCase struct { @@ -168,6 +168,123 @@ func testPrivateAndPublicFormatsCase(t *testing.T, tc *keyFormatCase, pemEncodin } } +func TestPEMNewlineConventions(t *testing.T) { + // RFC 7468 sections 2 and 3 permit CRLF, CR and LF between PEM lines. + c := testcert.New(t) + private, err := x509.MarshalPKCS8PrivateKey(c.LeafKey) + if err != nil { + t.Fatal(err) + } + public, err := x509.MarshalPKIXPublicKey(c.LeafKey.Public()) + if err != nil { + t.Fatal(err) + } + password := []byte("newline test password") + encrypted, err := keys.MarshalEncryptedPrivate(t.Context(), c.LeafKey, password, keys.EncryptionOptions{Iterations: 1000}, keys.Limits{}) + if err != nil { + t.Fatal(err) + } + + for _, tc := range []struct { + label string + raw []byte + }{ + {privateKeyPEMType, private}, + {"PUBLIC KEY", public}, + {"CERTIFICATE", c.Leaf.Raw}, + {"X509 CRL", testcert.CRL(t, c)}, + {encryptedPrivateKeyPEMType, encrypted}, + } { + for _, newline := range []struct{ name, value string }{ + {"LF", "\n"}, {"CRLF", "\r\n"}, {"CR", "\r"}, {"mixed", "\n"}, + } { + t.Run(tc.label+"/"+newline.name, func(t *testing.T) { + input := bytes.ReplaceAll(pem.EncodeToMemory(&pem.Block{Type: tc.label, Bytes: tc.raw}), []byte("\n"), []byte(newline.value)) + if newline.name == "mixed" { + input = bytes.Replace(input, []byte("\n"), []byte("\r"), 1) + } + original := bytes.Clone(input) + switch tc.label { + case privateKeyPEMType: + got, err := keys.ParsePrivate(input, keys.Limits{}) + if err != nil { + t.Fatal(err) + } + encoded, err := x509.MarshalPKCS8PrivateKey(got) + if err != nil || !bytes.Equal(encoded, private) { + t.Fatal("private key changed", err) + } + case "PUBLIC KEY": + got, err := keys.ParsePublic(input, keys.Limits{}) + if err != nil { + t.Fatal(err) + } + encoded, err := x509.MarshalPKIXPublicKey(got) + if err != nil || !bytes.Equal(encoded, public) { + t.Fatal("public key changed", err) + } + case "CERTIFICATE": + got, err := keys.ParseCertificate(input, keys.Limits{}) + if err != nil || !bytes.Equal(got.Raw, tc.raw) { + t.Fatal("certificate changed", err) + } + case "X509 CRL": + got, err := keys.ParseCRL(input, keys.Limits{}) + if err != nil || !bytes.Equal(got.Raw, tc.raw) { + t.Fatal("CRL changed", err) + } + case encryptedPrivateKeyPEMType: + got, err := keys.ParseEncryptedPrivate(t.Context(), input, password, keys.Limits{}) + if err != nil { + t.Fatal(err) + } + encoded, err := x509.MarshalPKCS8PrivateKey(got) + if err != nil || !bytes.Equal(encoded, private) { + t.Fatal("decrypted private key changed", err) + } + } + if !bytes.Equal(input, original) { + t.Fatal("PEM input changed") + } + }) + } + } +} + +func TestPEMFramingAcrossNewlines(t *testing.T) { + c := testcert.New(t) + private, err := x509.MarshalPKCS8PrivateKey(c.LeafKey) + if err != nil { + t.Fatal(err) + } + body := base64.StdEncoding.EncodeToString(private) + for _, newline := range []struct{ name, value string }{ + {"LF", "\n"}, {"CRLF", "\r\n"}, {"CR", "\r"}, + } { + t.Run(newline.name, func(t *testing.T) { + begin := "-----BEGIN PRIVATE KEY-----" + end := "-----END PRIVATE KEY-----" + valid := begin + newline.value + body + newline.value + end + for _, input := range []string{ + begin + body + newline.value + end, + begin + newline.value + body + end, + begin + newline.value + body + newline.value + "-----END PUBLIC KEY-----", + "-----begin PRIVATE KEY-----" + newline.value + body + newline.value + end, + begin + newline.value + body + "!" + newline.value + end, + valid + newline.value + valid, + valid + newline.value + "trailing data", + } { + if got, err := keys.ParsePrivate([]byte(input), keys.Limits{}); got != nil || !errors.Is(err, keys.ErrFormat) { + t.Fatalf("malformed PEM accepted: %q: %v", input, err) + } + } + if got, err := keys.ParsePrivate([]byte(valid), keys.Limits{MaxKeyBytes: len(private) - 1}); got != nil || !errors.Is(err, keys.ErrLimit) { + t.Fatal("decoded-byte limit not enforced", err) + } + }) + } +} + func TestPrivateParserRejectsAgreementOnlyKeys(t *testing.T) { agreement, err := ecdh.X25519().GenerateKey(rand.Reader) if err != nil { @@ -415,7 +532,7 @@ func TestRejectAmbiguousAndOversizedMaterial(t *testing.T) { nil, append(bytes.Clone(private), 0), append(bytes.Clone(private), private...), append(bytes.Clone(p), p...), append([]byte("preamble"), p...), append(bytes.Clone(p), []byte("trailing")...), pem.EncodeToMemory(&pem.Block{Type: privateKeyPEMType, Bytes: private, Headers: map[string]string{"Proc-Type": "4,ENCRYPTED"}}), - pem.EncodeToMemory(&pem.Block{Type: textENCRYPTEDPRIVATEKEY, Bytes: private}), + pem.EncodeToMemory(&pem.Block{Type: encryptedPrivateKeyPEMType, Bytes: private}), bytes.Repeat([]byte{0}, keys.DefaultLimits().MaxInputBytes+1), } { if key, err := keys.ParsePrivate(bad, keys.Limits{}); key != nil || err == nil { diff --git a/keys/password.go b/keys/password.go index 4a3651e..8f2b686 100644 --- a/keys/password.go +++ b/keys/password.go @@ -14,6 +14,24 @@ import ( "errors" ) +const ( + // AES key length in bits used when selecting and validating the export cipher. + aes128KeyBits = 128 + // AES key length in bits used when selecting and validating the export cipher. + aes192KeyBits = 192 + aes256KeyBits = 256 + defaultExportIterations = 600000 + defaultExportKeyBits = aes256KeyBits + // Number of KDF iterations between context checks. + kdfCancellationInterval = 1024 + // Caps a derived key length before KDF allocation, independently of salt length. + maximumDerivedKeyBytes = 64 + // Maximum accepted salt length for the supported password-based KDF profiles, in bytes. + maximumSaltBytes = 64 + // Minimum accepted salt length for the supported password-based KDF profiles, in bytes. + minimumSaltBytes = 8 +) + var ( // ErrPolicy reports unsupported password protection or invalid caller options. ErrPolicy = errors.New("key protection policy violation") @@ -82,7 +100,10 @@ func (p *passwordState) charge(iterations, blocks int) error { } func (p *passwordState) derive(hash crypto.Hash, salt []byte, iterations, size int) ([]byte, error) { - if !p.acceptHash(hash) || size < 1 || size > 64 || len(salt) < 8 || len(salt) > 64 { + // PBKDF2 appends a 32-bit big-endian block counter to the salt. + const pbkdf2CounterBytes = 4 + + if !p.acceptHash(hash) || size < 1 || size > maximumDerivedKeyBytes || len(salt) < minimumSaltBytes || len(salt) > maximumSaltBytes { return nil, ErrPolicy } @@ -98,7 +119,7 @@ func (p *passwordState) derive(hash crypto.Hash, salt []byte, iterations, size i defer clear(u) defer clear(t) - var counter [4]byte + var counter [pbkdf2CounterBytes]byte for block := 1; block <= blocks; block++ { binary.BigEndian.PutUint32(counter[:], uint32(block)) prf.Reset() @@ -108,7 +129,7 @@ func (p *passwordState) derive(hash crypto.Hash, salt []byte, iterations, size i copy(t, u) for i := 1; i < iterations; i++ { - if i%1024 == 0 { + if i%kdfCancellationInterval == 0 { if err := p.ctx.Err(); err != nil { clear(out) return nil, err @@ -147,7 +168,7 @@ func sha2(h crypto.Hash) bool { return h == crypto.SHA256 || h == crypto.SHA384 func normalizeEncryption(options EncryptionOptions) (EncryptionOptions, error) { if options.KeyBits == 0 { - options.KeyBits = 256 + options.KeyBits = defaultExportKeyBits } if options.Hash == 0 { @@ -155,10 +176,10 @@ func normalizeEncryption(options EncryptionOptions) (EncryptionOptions, error) { } if options.Iterations == 0 { - options.Iterations = 600000 + options.Iterations = defaultExportIterations } - if (options.KeyBits != 128 && options.KeyBits != 192 && options.KeyBits != 256) || !sha2(options.Hash) || options.Iterations < 1 { + if (options.KeyBits != aes128KeyBits && options.KeyBits != aes192KeyBits && options.KeyBits != aes256KeyBits) || !sha2(options.Hash) || options.Iterations < 1 { return EncryptionOptions{}, ErrPolicy } diff --git a/keys/password_internal_test.go b/keys/password_internal_test.go index d39b833..648511f 100644 --- a/keys/password_internal_test.go +++ b/keys/password_internal_test.go @@ -205,20 +205,6 @@ func TestPKCS12AuthenticatedBagAttributes(t *testing.T) { } } -func authenticatedPFX(t testing.TB, contents []byte) []byte { - t.Helper() - - authSafe := der.Seq(dataInfo(contents)) - s := testState(t.Context(), t) - - mac, err := encodePKCS12MAC(s, authSafe, PKCS12Options{Encryption: EncryptionOptions{Hash: crypto.SHA256, Iterations: 1000}}) - if err != nil { - t.Fatal(err) - } - - return der.Seq(der.Int(3), dataInfo(authSafe), mac) -} - func TestProtectionParameterPolicy(t *testing.T) { salt, iv := bytes.Repeat([]byte{1}, 16), bytes.Repeat([]byte{2}, 16) @@ -468,62 +454,6 @@ func TestPBES2WorkingStorage(t *testing.T) { } } -func rawValue(t testing.TB, b []byte) asn1.RawValue { - t.Helper() - - v, err := der.Read(b, len(b)) - if err != nil { - t.Fatal(err) - } - - return v -} - -func sequence(t testing.TB, b []byte) []asn1.RawValue { - t.Helper() - v := rawValue(t, b) - - f, err := der.Sequence(v, 0, 32) - if err != nil { - t.Fatal(err) - } - - return f -} - -func testState(ctx context.Context, t testing.TB) *passwordState { - t.Helper() - - s, err := newPasswordState(ctx, []byte("public password"), Limits{}) - if err != nil { - t.Fatal(err) - } - - t.Cleanup(s.close) - - return s -} - -func keyAllocated(t *testing.T, run func() error) uint64 { - t.Helper() - - allocated := ^uint64(0) - - for range 3 { - var before, after runtime.MemStats - runtime.ReadMemStats(&before) - - if err := run(); err != nil { - t.Fatal(err) - } - - runtime.ReadMemStats(&after) - allocated = min(allocated, after.TotalAlloc-before.TotalAlloc) - } - - return allocated -} - // RFC 6070 section 2: the 25-octet vector crosses SHA-1 output blocks and // therefore also checks the aggregate PBKDF2 work count. func TestPBKDF2LegacyPublishedVector(t *testing.T) { @@ -541,7 +471,9 @@ func TestPBKDF2LegacyPublishedVector(t *testing.T) { return } want, _ := hex.DecodeString("3d2eec4fe41c849b80c8d83662c0e44a8b291a964cf2f07038") - if err != nil || !bytes.Equal(got, want) || state.remaining != DefaultLimits().MaxKDFWork-8192 { + if err != nil { + t.Fatal(err) + } else if !bytes.Equal(got, want) || state.remaining != DefaultLimits().MaxKDFWork-8192 { t.Fatal("RFC 6070 vector or aggregate work differs", got, err, state.remaining) } clear(got) @@ -589,35 +521,6 @@ func TestPBKDF2OptionalParameters(t *testing.T) { } } -func protectedPKCS8Fixture(t testing.TB, plain, password []byte, hash crypto.Hash, bits int, defaultPRF bool) []byte { - t.Helper() - salt, iv := []byte("independent salt"), bytes.Repeat([]byte{0x42}, aes.BlockSize) - key, err := pbkdf2.Key(hash.New, string(password), salt, 1000, bits/8) - if err != nil { - t.Fatal(err) - } - defer clear(key) - block, err := aes.NewCipher(key) - if err != nil { - t.Fatal(err) - } - padding := aes.BlockSize - len(plain)%aes.BlockSize - padded := append(bytes.Clone(plain), bytes.Repeat([]byte{byte(padding)}, padding)...) - defer clear(padded) - cipher.NewCBCEncrypter(block, iv).CryptBlocks(padded, padded) - kdfParams := [][]byte{der.Octets(salt), der.Int(1000)} - if !defaultPRF { - oid := hmacOID(hash) - if hash == crypto.SHA1 { - oid = oidHMAC1() - } - kdfParams = append(kdfParams, der.Seq(der.OID(oid), []byte{5, 0})) - } - encOID := map[int]asn1.ObjectIdentifier{128: oidAES128CBC(), 192: oidAES192CBC(), 256: oidAES256CBC()}[bits] - alg := der.Seq(der.OID(oidPBES2()), der.Seq(der.Seq(der.OID(oidPBKDF2()), der.Seq(kdfParams...)), der.Seq(der.OID(encOID), der.Octets(iv)))) - return der.Seq(alg, der.Octets(padded)) -} - func TestPKCS8LegacyAndPasswordOctets(t *testing.T) { certificate := testcert.New(t) plain, err := x509.MarshalPKCS8PrivateKey(certificate.LeafKey) @@ -969,3 +872,102 @@ func FuzzPEMDecoding(f *testing.F) { } }) } + +func authenticatedPFX(t testing.TB, contents []byte) []byte { + t.Helper() + + authSafe := der.Seq(dataInfo(contents)) + s := testState(t.Context(), t) + + mac, err := encodePKCS12MAC(s, authSafe, PKCS12Options{Encryption: EncryptionOptions{Hash: crypto.SHA256, Iterations: 1000}}) + if err != nil { + t.Fatal(err) + } + + return der.Seq(der.Int(3), dataInfo(authSafe), mac) +} + +func rawValue(t testing.TB, b []byte) asn1.RawValue { + t.Helper() + + v, err := der.Read(b, len(b)) + if err != nil { + t.Fatal(err) + } + + return v +} + +func sequence(t testing.TB, b []byte) []asn1.RawValue { + t.Helper() + v := rawValue(t, b) + + f, err := der.Sequence(v, 0, 32) + if err != nil { + t.Fatal(err) + } + + return f +} + +func testState(ctx context.Context, t testing.TB) *passwordState { + t.Helper() + + s, err := newPasswordState(ctx, []byte("public password"), Limits{}) + if err != nil { + t.Fatal(err) + } + + t.Cleanup(s.close) + + return s +} + +func keyAllocated(t *testing.T, run func() error) uint64 { + t.Helper() + + allocated := ^uint64(0) + + for range 3 { + var before, after runtime.MemStats + runtime.ReadMemStats(&before) + + if err := run(); err != nil { + t.Fatal(err) + } + + runtime.ReadMemStats(&after) + allocated = min(allocated, after.TotalAlloc-before.TotalAlloc) + } + + return allocated +} + +func protectedPKCS8Fixture(t testing.TB, plain, password []byte, hash crypto.Hash, bits int, defaultPRF bool) []byte { + t.Helper() + salt, iv := []byte("independent salt"), bytes.Repeat([]byte{0x42}, aes.BlockSize) + key, err := pbkdf2.Key(hash.New, string(password), salt, 1000, bits/8) + if err != nil { + t.Fatal(err) + } + defer clear(key) + block, err := aes.NewCipher(key) + if err != nil { + t.Fatal(err) + } + padding := aes.BlockSize - len(plain)%aes.BlockSize + padded := append(bytes.Clone(plain), bytes.Repeat([]byte{byte(padding)}, padding)...) + defer clear(padded) + cipher.NewCBCEncrypter(block, iv).CryptBlocks(padded, padded) + kdfParams := [][]byte{der.Octets(salt), der.Int(1000)} + if !defaultPRF { + oid := hmacOID(hash) + if hash == crypto.SHA1 { + oid = oidHMAC1() + } + kdfParams = append(kdfParams, der.Seq(der.OID(oid), []byte{5, 0})) + } + encOID := map[int]asn1.ObjectIdentifier{128: oidAES128CBC(), 192: oidAES192CBC(), 256: oidAES256CBC()}[bits] + alg := der.Seq(der.OID(oidPBES2()), der.Seq(der.Seq(der.OID(oidPBKDF2()), der.Seq(kdfParams...)), der.Seq(der.OID(encOID), der.Octets(iv)))) + return der.Seq(alg, der.Octets(padded)) +} diff --git a/keys/password_test.go b/keys/password_test.go index 5c29dc8..cac0fde 100644 --- a/keys/password_test.go +++ b/keys/password_test.go @@ -27,66 +27,6 @@ import ( "github.com/skarm/xmlsec/keys" ) -func pkcs12FormOID(last int) asn1.ObjectIdentifier { - return asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 12, 10, 1, last} -} - -func pkcs12FormData(input []byte) []byte { - return der.Seq(der.OID(asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 1}), der.Ctx(0, der.Octets(input))) -} - -func pkcs12FormIndefinite(tag byte, parts ...[]byte) []byte { - return append(append([]byte{tag, 0x80}, bytes.Join(parts, nil)...), 0, 0) -} - -// Build the MAC with the independent standard-library PBKDF2 implementation. -// The authenticated octets deliberately need not be a canonical DER encoding. -func pkcs12FormPFX(t *testing.T, authenticated, macInput []byte, outerBER bool) []byte { - t.Helper() - salt := []byte("published form test salt") - key, err := pbkdf2.Key(sha256.New, "forms password", salt, 1000, 32) - if err != nil { - t.Fatal(err) - } - defer clear(key) - mac := hmac.New(sha256.New, key) - mac.Write(macInput) - prf := der.Seq(der.OID(asn1.ObjectIdentifier{1, 2, 840, 113549, 2, 9}), []byte{5, 0}) - kdf := der.Seq(der.OID(asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 5, 12}), der.Seq(der.Octets(salt), der.Int(1000), der.Int(32), prf)) - algorithm := der.Seq(der.OID(asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 5, 14}), der.Seq(kdf, prf)) - macData := der.Seq(der.Seq(algorithm, der.Octets(mac.Sum(nil))), der.Octets(nil)) - if outerBER { - mid := len(authenticated) / 2 - octets := pkcs12FormIndefinite(0x24, der.Octets(authenticated[:mid]), pkcs12FormIndefinite(0x24, der.Octets(authenticated[mid:]))) - info := pkcs12FormIndefinite(0x30, der.OID(asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 1}), pkcs12FormIndefinite(0xa0, octets)) - return pkcs12FormIndefinite(0x30, der.Int(3), info, macData) - } - return der.Seq(der.Int(3), pkcs12FormData(authenticated), macData) -} - -func pkcs12FormBags(t *testing.T, key crypto.Signer, certificate *x509.Certificate, encrypted bool) []byte { - t.Helper() - private, err := x509.MarshalPKCS8PrivateKey(key) - if err != nil { - t.Fatal(err) - } - defer func() { clear(private) }() - bagType := 1 - if encrypted { - clear(private) - private, err = keys.MarshalEncryptedPrivate(t.Context(), key, []byte("forms password"), protection(crypto.SHA256, 256), keys.Limits{}) - if err != nil { - t.Fatal(err) - } - bagType = 2 - } - id := sha256.Sum256(certificate.Raw) - attributes := der.Set(der.Seq(der.OID(asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 21}), der.Set(der.Octets(id[:])))) - keyBag := der.Seq(der.OID(pkcs12FormOID(bagType)), der.Ctx(0, private), attributes) - certBag := der.Seq(der.OID(pkcs12FormOID(3)), der.Ctx(0, der.Seq(der.OID(asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 22, 1}), der.Ctx(0, der.Octets(certificate.Raw)))), attributes) - return bytes.Join([][]byte{keyBag, certBag}, nil) -} - func TestPKCS12ExplicitIdentitiesAndSelection(t *testing.T) { t.Parallel() @@ -331,7 +271,7 @@ func checkEncryptedPrivateKeyCase(t *testing.T, ctx context.Context, key crypto. for _, pemInput := range []bool{false, true} { input := bytes.Clone(raw) if pemInput { - input = pem.EncodeToMemory(&pem.Block{Type: textENCRYPTEDPRIVATEKEY, Bytes: input}) + input = pem.EncodeToMemory(&pem.Block{Type: encryptedPrivateKeyPEMType, Bytes: input}) } before := bytes.Clone(input) @@ -760,6 +700,97 @@ func TestLargePKCS12ResourceLimits(t *testing.T) { } } +func FuzzPasswordContainers(f *testing.F) { + c := testcert.New(f) + ctx := f.Context() + password := []byte("public test password") + options := protection(crypto.SHA256, 256) + + a, err := keys.MarshalEncryptedPrivate(ctx, c.LeafKey, password, options, keys.Limits{}) + if err != nil { + f.Fatal(err) + } + + b, err := keys.MarshalPKCS12(ctx, keys.PKCS12Identity{PrivateKey: c.LeafKey, Certificate: c.Leaf}, password, keys.PKCS12Options{Encryption: options}, keys.Limits{}) + if err != nil { + f.Fatal(err) + } + + for _, raw := range [][]byte{a, b, pem.EncodeToMemory(&pem.Block{Type: encryptedPrivateKeyPEMType, Bytes: a})} { + f.Add(raw, password) + } + + f.Fuzz(func(t *testing.T, raw, password []byte) { + if len(raw) > 64<<10 || len(password) > 128 { + return + } + + limits := keys.Limits{MaxIterations: 1000, MaxKDFWork: 3000} + _, _ = keys.ParseEncryptedPrivate(ctx, raw, password, limits) + _, _ = keys.ParsePKCS12(ctx, raw, password, limits) + }) +} + +func pkcs12FormOID(last int) asn1.ObjectIdentifier { + return asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 12, 10, 1, last} +} + +func pkcs12FormData(input []byte) []byte { + return der.Seq(der.OID(asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 1}), der.Ctx(0, der.Octets(input))) +} + +func pkcs12FormIndefinite(tag byte, parts ...[]byte) []byte { + return append(append([]byte{tag, 0x80}, bytes.Join(parts, nil)...), 0, 0) +} + +// Build the MAC with the independent standard-library PBKDF2 implementation. +// The authenticated octets deliberately need not be a canonical DER encoding. +func pkcs12FormPFX(t *testing.T, authenticated, macInput []byte, outerBER bool) []byte { + t.Helper() + salt := []byte("published form test salt") + key, err := pbkdf2.Key(sha256.New, "forms password", salt, 1000, 32) + if err != nil { + t.Fatal(err) + } + defer clear(key) + mac := hmac.New(sha256.New, key) + mac.Write(macInput) + prf := der.Seq(der.OID(asn1.ObjectIdentifier{1, 2, 840, 113549, 2, 9}), []byte{5, 0}) + kdf := der.Seq(der.OID(asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 5, 12}), der.Seq(der.Octets(salt), der.Int(1000), der.Int(32), prf)) + algorithm := der.Seq(der.OID(asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 5, 14}), der.Seq(kdf, prf)) + macData := der.Seq(der.Seq(algorithm, der.Octets(mac.Sum(nil))), der.Octets(nil)) + if outerBER { + mid := len(authenticated) / 2 + octets := pkcs12FormIndefinite(0x24, der.Octets(authenticated[:mid]), pkcs12FormIndefinite(0x24, der.Octets(authenticated[mid:]))) + info := pkcs12FormIndefinite(0x30, der.OID(asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 1}), pkcs12FormIndefinite(0xa0, octets)) + return pkcs12FormIndefinite(0x30, der.Int(3), info, macData) + } + return der.Seq(der.Int(3), pkcs12FormData(authenticated), macData) +} + +func pkcs12FormBags(t *testing.T, key crypto.Signer, certificate *x509.Certificate, encrypted bool) []byte { + t.Helper() + private, err := x509.MarshalPKCS8PrivateKey(key) + if err != nil { + t.Fatal(err) + } + defer func() { clear(private) }() + bagType := 1 + if encrypted { + clear(private) + private, err = keys.MarshalEncryptedPrivate(t.Context(), key, []byte("forms password"), protection(crypto.SHA256, 256), keys.Limits{}) + if err != nil { + t.Fatal(err) + } + bagType = 2 + } + id := sha256.Sum256(certificate.Raw) + attributes := der.Set(der.Seq(der.OID(asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 21}), der.Set(der.Octets(id[:])))) + keyBag := der.Seq(der.OID(pkcs12FormOID(bagType)), der.Ctx(0, private), attributes) + certBag := der.Seq(der.OID(pkcs12FormOID(3)), der.Ctx(0, der.Seq(der.OID(asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 22, 1}), der.Ctx(0, der.Octets(certificate.Raw)))), attributes) + return bytes.Join([][]byte{keyBag, certBag}, nil) +} + func protection(hash crypto.Hash, bits int) keys.EncryptionOptions { return keys.EncryptionOptions{Hash: hash, KeyBits: bits, Iterations: 1000} } @@ -813,34 +844,3 @@ func rejectPrivate(t *testing.T, raw []byte) { t.Fatalf("invalid private-key structure accepted: key=%v, error=%v", key != nil, err) } } - -func FuzzPasswordContainers(f *testing.F) { - c := testcert.New(f) - ctx := f.Context() - password := []byte("public test password") - options := protection(crypto.SHA256, 256) - - a, err := keys.MarshalEncryptedPrivate(ctx, c.LeafKey, password, options, keys.Limits{}) - if err != nil { - f.Fatal(err) - } - - b, err := keys.MarshalPKCS12(ctx, keys.PKCS12Identity{PrivateKey: c.LeafKey, Certificate: c.Leaf}, password, keys.PKCS12Options{Encryption: options}, keys.Limits{}) - if err != nil { - f.Fatal(err) - } - - for _, raw := range [][]byte{a, b, pem.EncodeToMemory(&pem.Block{Type: textENCRYPTEDPRIVATEKEY, Bytes: a})} { - f.Add(raw, password) - } - - f.Fuzz(func(t *testing.T, raw, password []byte) { - if len(raw) > 64<<10 || len(password) > 128 { - return - } - - limits := keys.Limits{MaxIterations: 1000, MaxKDFWork: 3000} - _, _ = keys.ParseEncryptedPrivate(ctx, raw, password, limits) - _, _ = keys.ParsePKCS12(ctx, raw, password, limits) - }) -} diff --git a/keys/pbes2.go b/keys/pbes2.go index 0e4337e..224665b 100644 --- a/keys/pbes2.go +++ b/keys/pbes2.go @@ -13,6 +13,22 @@ import ( "github.com/skarm/xmlsec/internal/der" ) +// AlgorithmIdentifier has an OID and optional parameters. Key-structure and +// password-encryption parsers share these ASN.1 cardinalities. +const ( + algorithmIdentifierMinimumFields = 1 + algorithmIdentifierMaximumFields = 2 +) + +const ( + aes128KeyBytes = 16 + aes192KeyBytes = 24 + aes256KeyBytes = 32 + defaultPBES2SaltBytes = 16 + // Caps chunks emitted by key/container exporters, in bytes. + exportChunkBytes = 32 << 10 +) + type pbes2Parameters struct { hash crypto.Hash salt []byte @@ -22,17 +38,17 @@ type pbes2Parameters struct { } func algorithm(v asn1.RawValue) (asn1.ObjectIdentifier, asn1.RawValue, error) { - f, err := der.Sequence(v, 1, 2) + f, err := der.Sequence(v, algorithmIdentifierMinimumFields, algorithmIdentifierMaximumFields) if err != nil { return nil, asn1.RawValue{}, ErrFormat } var oid asn1.ObjectIdentifier - if !der.Is(f[0], 0, asn1.TagOID, false) || der.Decode(f[0], &oid) != nil { + if !der.Is(f[0], asn1.ClassUniversal, asn1.TagOID, false) || der.Decode(f[0], &oid) != nil { return nil, asn1.RawValue{}, ErrFormat } - if len(f) == 1 { + if len(f) == algorithmIdentifierMinimumFields { return oid, asn1.RawValue{}, nil } @@ -49,7 +65,7 @@ func parseHMACHash(v asn1.RawValue, legacy bool) (crypto.Hash, error) { return 0, err } - if len(param.FullBytes) > 0 && !der.Is(param, 0, asn1.TagNull, false) { + if len(param.FullBytes) > 0 && !der.Is(param, asn1.ClassUniversal, asn1.TagNull, false) { return 0, ErrFormat } @@ -80,7 +96,7 @@ func hmacOID(h crypto.Hash) asn1.ObjectIdentifier { func integer(v asn1.RawValue) (int, error) { var n int - if !der.Is(v, 0, asn1.TagInteger, false) || der.Decode(v, &n) != nil { + if !der.Is(v, asn1.ClassUniversal, asn1.TagInteger, false) || der.Decode(v, &n) != nil { return 0, ErrFormat } @@ -88,6 +104,9 @@ func integer(v asn1.RawValue) (int, error) { } func parseKDF(v asn1.RawValue, expectedSize int, requiredSize bool) (pbes2Parameters, error) { + // PBKDF2-params has two required and two optional fields. + const maximumPBKDF2Fields = 4 + oid, params, err := algorithm(v) if err != nil { return pbes2Parameters{}, err @@ -97,13 +116,13 @@ func parseKDF(v asn1.RawValue, expectedSize int, requiredSize bool) (pbes2Parame return pbes2Parameters{}, ErrPolicy } - f, err := der.Sequence(params, 2, 4) - if err != nil || !der.Is(f[0], 0, asn1.TagOctetString, false) { + f, err := der.Sequence(params, 2, maximumPBKDF2Fields) + if err != nil || !der.Is(f[0], asn1.ClassUniversal, asn1.TagOctetString, false) { return pbes2Parameters{}, ErrFormat } p := pbes2Parameters{salt: f[0].Bytes, keyBytes: expectedSize, hash: crypto.SHA1} - if len(p.salt) < 8 || len(p.salt) > 64 { + if len(p.salt) < minimumSaltBytes || len(p.salt) > maximumSaltBytes { return p, ErrPolicy } @@ -113,7 +132,7 @@ func parseKDF(v asn1.RawValue, expectedSize int, requiredSize bool) (pbes2Parame } at := 2 - if at < len(f) && der.Is(f[at], 0, asn1.TagInteger, false) { + if at < len(f) && der.Is(f[at], asn1.ClassUniversal, asn1.TagInteger, false) { n, err := integer(f[at]) if err != nil || n < 1 { return p, ErrFormat @@ -121,7 +140,7 @@ func parseKDF(v asn1.RawValue, expectedSize int, requiredSize bool) (pbes2Parame // PBES2 fixes the key size through its cipher. RFC 9879 only recommends // matching the MAC size; another explicitly encoded bounded size is valid. - if n > 64 || !requiredSize && n != expectedSize { + if n > maximumDerivedKeyBytes || !requiredSize && n != expectedSize { return p, ErrPolicy } @@ -148,6 +167,8 @@ func parseKDF(v asn1.RawValue, expectedSize int, requiredSize bool) (pbes2Parame } func parsePBES2(v asn1.RawValue) (pbes2Parameters, error) { + const pbes2ParameterFields = 2 // key derivation and encryption scheme + oid, params, err := algorithm(v) if err != nil { return pbes2Parameters{}, err @@ -157,7 +178,7 @@ func parsePBES2(v asn1.RawValue) (pbes2Parameters, error) { return pbes2Parameters{}, ErrPolicy } - f, err := der.Sequence(params, 2, 2) + f, err := der.Sequence(params, pbes2ParameterFields, pbes2ParameterFields) if err != nil { return pbes2Parameters{}, ErrFormat } @@ -171,16 +192,16 @@ func parsePBES2(v asn1.RawValue) (pbes2Parameters, error) { switch { case enc.Equal(oidAES128CBC()): - size = 16 + size = aes128KeyBytes case enc.Equal(oidAES192CBC()): - size = 24 + size = aes192KeyBytes case enc.Equal(oidAES256CBC()): - size = 32 + size = aes256KeyBytes default: return pbes2Parameters{}, ErrPolicy } - if !der.Is(iv, 0, asn1.TagOctetString, false) || len(iv.Bytes) != aes.BlockSize { + if !der.Is(iv, asn1.ClassUniversal, asn1.TagOctetString, false) || len(iv.Bytes) != aes.BlockSize { return pbes2Parameters{}, ErrFormat } @@ -253,7 +274,7 @@ func decryptPBES2(state *passwordState, algorithm asn1.RawValue, encrypted []byt } func kdfEncoding(salt []byte, iterations, size int, hash crypto.Hash) []byte { - params := der.Seq(der.Octets(salt), der.Int(iterations), der.Int(size), der.Seq(der.OID(hmacOID(hash)), []byte{5, 0})) + params := der.Seq(der.Octets(salt), der.Int(iterations), der.Int(size), der.Seq(der.OID(hmacOID(hash)), []byte{asn1.TagNull, 0})) return der.Seq(der.OID(oidPBKDF2()), params) } @@ -267,7 +288,7 @@ func encryptPBES2Parts(state *passwordState, plain derParts, options EncryptionO return nil, nil, ErrLimit } - salt, iv := make([]byte, 16), make([]byte, 16) + salt, iv := make([]byte, defaultPBES2SaltBytes), make([]byte, aes.BlockSize) if _, err := rand.Read(salt); err != nil { return nil, nil, err } @@ -276,7 +297,7 @@ func encryptPBES2Parts(state *passwordState, plain derParts, options EncryptionO return nil, nil, err } - key, err := state.derive(options.Hash, salt, options.Iterations, options.KeyBits/8) + key, err := state.derive(options.Hash, salt, options.Iterations, options.KeyBits/bitsPerOctet) if err != nil { return nil, nil, err } @@ -307,13 +328,13 @@ func encryptPBES2Parts(state *passwordState, plain derParts, options EncryptionO oid := oidAES256CBC() switch options.KeyBits { - case 128: + case aes128KeyBits: oid = oidAES128CBC() - case 192: + case aes192KeyBits: oid = oidAES192CBC() } - params := der.Seq(kdfEncoding(salt, options.Iterations, options.KeyBits/8, options.Hash), der.Seq(der.OID(oid), der.Octets(iv))) + params := der.Seq(kdfEncoding(salt, options.Iterations, options.KeyBits/bitsPerOctet, options.Hash), der.Seq(der.OID(oid), der.Octets(iv))) return der.Seq(der.OID(oidPBES2()), params), out, nil } @@ -324,7 +345,7 @@ func cryptCBC(ctx context.Context, mode cipher.BlockMode, dst, src []byte) error return err } - size := min(len(src), 32<<10) + size := min(len(src), exportChunkBytes) mode.CryptBlocks(dst[:size], src[:size]) dst, src = dst[size:], src[size:] } diff --git a/keys/pkcs12.go b/keys/pkcs12.go index d2a12c0..609bef7 100644 --- a/keys/pkcs12.go +++ b/keys/pkcs12.go @@ -15,6 +15,32 @@ import ( "github.com/skarm/xmlsec/internal/streamio" ) +// Complete ASN.1 identifiers used by key-container framing and BER normalization. +// Constructed [0] wraps explicit content; primitive [0] holds encrypted octets. +const ( + encodedSequenceTag = 0x30 + encodedContext0Tag = 0xa0 + encodedPrimitiveContext0Tag = 0x80 +) + +const ( + // Hard byte caps for optional PKCS#12 bag attributes. + maximumFriendlyNameBytes = 512 + maximumLocalKeyIDBytes = 64 +) + +// PKCS#12 wire roles shared by strict parsing, BER wrappers and export. +const ( + // The supported PFX version is the encoded INTEGER 3. + pkcs12Version = 3 + pfxRequiredFields = 3 // MacData is mandatory in the supported profile. + // EncryptedData without unprotected attributes has encoded version 0. + pkcs12EncryptedDataVersion = 0 + // EncryptedContentInfo contains its content type, encryption algorithm and ciphertext. + encryptedContentInfoFields = 3 + safeBagMaximumFields = 3 // Two required fields and optional bag attributes. +) + // PKCS12Identity contains caller-owned mutable objects from a password-authenticated // container. Certificate and PrivateKey match by SubjectPublicKeyInfo. // ExtraCertificates is unordered untrusted evidence. The container MAC establishes @@ -28,6 +54,43 @@ type PKCS12Identity struct { ExtraCertificates []*x509.Certificate } +// PKCS12ParseOptions enables selected container encodings and identity selection. +// The zero value requires the strict flat DER profile. Container authentication +// precedes bag processing for every identity. +type PKCS12ParseOptions struct { + // AllowLegacy permits import-only HMAC-SHA-1 PBKDF2 and traditional SHA-1 + // container MACs. RFC 9879 PBMAC1 remains SHA-2 only. FIPS rejects SHA-1. + AllowLegacy bool + // AllowNonDefaultMACKeyLength permits RFC 9879 PBMAC1 keys of 16..64 bytes + // whose size differs from the MAC output. Zero requires the MAC output size. + // Export always uses the MAC output size; shorter keys remain unsupported. + AllowNonDefaultMACKeyLength bool + // AllowBER enables indefinite container/ContentInfo/SafeBag wrappers and + // constructed OCTET STRING payloads. Keys, algorithms and attributes retain + // their strict DER rules. MAC parameters keep primitive/structure checks; + // the explicit PBMAC1 outer DEFAULT 1 placeholder in RFC 9879 vectors is + // tolerated independently of this option. Original MAC input stays unchanged. + AllowBER bool + // AllowNestedSafeContents enables bounded recursive safeContentsBag values. + AllowNestedSafeContents bool + // CertificateSHA256 selects one exact certificate when parsing one identity. + // Zero requires exactly one identity. Local names never select a key. + CertificateSHA256 [sha256.Size]byte +} + +// PKCS12Options selects encryption and integrity for one DER PKCS#12 identity. +// Both integrity modes require a MAC and independently generated salts. Export +// rejects RC2, 3DES, SHA-1, BER and containers without a MAC. Password bytes must be +// UTF-8 and nonempty; only import accepts an empty password. +type PKCS12Options struct { + // Encryption selects the private-key encryption parameters. Zero fields select + // defaults. + Encryption EncryptionOptions + // ClassicMAC selects RFC 7292 SHA-2 integrity and requires a BMP-only password. + // False selects RFC 9879 PBMAC1 with UTF-8 passwords. + ClassicMAC bool +} + // ParsePKCS12 imports exactly one identity from the strict flat DER profile. // It authenticates the mandatory password MAC before decryption, enforces // aggregate KDF work, and performs no certificate trust processing or I/O. @@ -44,7 +107,7 @@ func ParsePKCS12WithOptions(ctx context.Context, data, password []byte, options return nil, err } - if options.CertificateSHA256 == [32]byte{} { + if options.CertificateSHA256 == [sha256.Size]byte{} { if len(items) != 1 { return nil, ErrFormat } @@ -61,30 +124,6 @@ func ParsePKCS12WithOptions(ctx context.Context, data, password []byte, options return nil, ErrFormat } -// PKCS12ParseOptions enables selected container encodings and identity selection. -// The zero value requires the strict flat DER profile. Container authentication -// precedes bag processing for every identity. -type PKCS12ParseOptions struct { - // AllowLegacy permits import-only HMAC-SHA-1 PBKDF2 and traditional SHA-1 - // container MACs. RFC 9879 PBMAC1 remains SHA-2 only. FIPS rejects SHA-1. - AllowLegacy bool - // AllowNonDefaultMACKeyLength permits RFC 9879 PBMAC1 keys of 16..64 bytes - // whose size differs from the MAC output. Zero requires the MAC output size. - // Export always uses the MAC output size; shorter keys remain unsupported. - AllowNonDefaultMACKeyLength bool - // AllowBER enables indefinite container/ContentInfo/SafeBag wrappers and - // constructed OCTET STRING payloads. Keys, algorithms and attributes retain - // their strict DER rules. MAC parameters keep primitive/structure checks; - // the explicit PBMAC1 outer DEFAULT 1 placeholder in RFC 9879 vectors is - // tolerated independently of this option. Original MAC input stays unchanged. - AllowBER bool - // AllowNestedSafeContents enables bounded recursive safeContentsBag values. - AllowNestedSafeContents bool - // CertificateSHA256 selects one exact certificate when parsing one identity. - // Zero requires exactly one identity. Local names never select a key. - CertificateSHA256 [32]byte -} - // ParsePKCS12Identities imports every unambiguously bound private-key/certificate // pair in bag order. It never chooses a wire-named identity or grants PKIX trust. // CertificateSHA256 must be zero; use ParsePKCS12WithOptions for exact selection. @@ -94,13 +133,46 @@ type PKCS12ParseOptions struct { // Unsupported bags/attributes, duplicate keys/certificates, conflicting IDs and // unmatched/ambiguous keys fail the entire operation. Output remains DER-only. func ParsePKCS12Identities(ctx context.Context, data, password []byte, options PKCS12ParseOptions, limits Limits) ([]*PKCS12Identity, error) { - if options.CertificateSHA256 != [32]byte{} { + if options.CertificateSHA256 != [sha256.Size]byte{} { return nil, ErrPolicy } return parsePKCS12Identities(ctx, data, password, options, limits) } +// MarshalPKCS12 exports one private key, its matching certificate and optional +// additional certificates to a password-authenticated DER PFX. It encrypts both +// certificate SafeContents and the PKCS#8 key with separate PBES2 salts/IVs; +// the MAC is mandatory. Zero options use RFC 9879 PBMAC1. ClassicMAC explicitly +// selects RFC 7292 SHA-2 integrity for older readers. Certificate objects are +// parsed from their Raw DER, ownership is copied and no trust is inferred. +// Password must be nonempty UTF-8. It performs no file/network I/O. +func MarshalPKCS12(ctx context.Context, identity PKCS12Identity, password []byte, options PKCS12Options, limits Limits) ([]byte, error) { + parts, err := preparePKCS12(ctx, identity, password, options, limits) + if err != nil { + return nil, err + } + + return parts.bytes(ctx) +} + +// WritePKCS12 exports a password-authenticated PFX directly to dst. It retains +// certificate snapshots and ciphertext but does not build a complete PFX or +// repeated nested DER buffers. Limits and KDF work are checked before output. +// The destination stays open and may contain partial output on error. +func WritePKCS12(ctx context.Context, dst io.Writer, identity PKCS12Identity, password []byte, options PKCS12Options, limits Limits) (int64, error) { + if nilcheck.IsNil(dst) { + return 0, ErrPolicy + } + + parts, err := preparePKCS12(ctx, identity, password, options, limits) + if err != nil { + return 0, err + } + + return parts.write(ctx, dst) +} + func parsePKCS12Identities(ctx context.Context, data, password []byte, options PKCS12ParseOptions, limits Limits) ([]*PKCS12Identity, error) { state, err := newPasswordState(ctx, password, limits) if err != nil { @@ -135,18 +207,18 @@ func parsePKCS12Identities(ctx context.Context, data, password []byte, options P return nil, err } - f, err := der.Sequence(v, 3, 3) // MacData is mandatory in this profile. + f, err := der.Sequence(v, pfxRequiredFields, pfxRequiredFields) // MacData is mandatory in this profile. if err != nil { return nil, ErrFormat } version, err := integer(f[0]) - if err != nil || version != 3 { + if err != nil || version != pkcs12Version { return nil, ErrFormat } oid, authSafe, err := contentInfo(f[1]) - if err != nil || !oid.Equal(oidData()) || !der.Is(authSafe, 0, asn1.TagOctetString, false) { + if err != nil || !oid.Equal(oidData()) || !der.Is(authSafe, asn1.ClassUniversal, asn1.TagOctetString, false) { return nil, ErrFormat } @@ -196,13 +268,15 @@ func parsePKCS12Identities(ctx context.Context, data, password []byte, options P } func contentInfo(v asn1.RawValue) (asn1.ObjectIdentifier, asn1.RawValue, error) { - f, err := der.Sequence(v, 2, 2) + const contentInfoFields = 2 // content type OID and explicit content + + f, err := der.Sequence(v, contentInfoFields, contentInfoFields) if err != nil { return nil, asn1.RawValue{}, ErrFormat } var oid asn1.ObjectIdentifier - if !der.Is(f[0], 0, asn1.TagOID, false) || der.Decode(f[0], &oid) != nil { + if !der.Is(f[0], asn1.ClassUniversal, asn1.TagOID, false) || der.Decode(f[0], &oid) != nil { return nil, asn1.RawValue{}, ErrFormat } @@ -215,18 +289,20 @@ func contentInfo(v asn1.RawValue) (asn1.ObjectIdentifier, asn1.RawValue, error) } func decryptSafeContents(state *passwordState, v asn1.RawValue) ([]byte, error) { - f, err := der.Sequence(v, 2, 2) + const encryptedDataFields = 2 // version and encryptedContentInfo + + f, err := der.Sequence(v, encryptedDataFields, encryptedDataFields) if err != nil { return nil, ErrFormat } version, err := integer(f[0]) - if err != nil || version != 0 { + if err != nil || version != pkcs12EncryptedDataVersion { return nil, ErrFormat } - info, err := der.Sequence(f[1], 3, 3) - if err != nil || !der.OIDEqual(info[0], oidData()) || !der.Is(info[2], 2, 0, false) { + info, err := der.Sequence(f[1], encryptedContentInfoFields, encryptedContentInfoFields) + if err != nil || !der.OIDEqual(info[0], oidData()) || !der.Is(info[2], asn1.ClassContextSpecific, 0, false) { return nil, ErrFormat } @@ -252,7 +328,7 @@ func (p *identityParser) contentInfos(infos []asn1.RawValue) error { switch { case oid.Equal(oidData()): - if !der.Is(content, 0, asn1.TagOctetString, false) { + if !der.Is(content, asn1.ClassUniversal, asn1.TagOctetString, false) { return ErrFormat } @@ -344,7 +420,9 @@ func (p *identityParser) safeContents(raw []byte) error { } func (p *identityParser) parseSafeBag(bag asn1.RawValue) error { - f, err := der.Sequence(bag, 2, 3) + const certificateBagFields = 2 // certificate type OID and explicit value + + f, err := der.Sequence(bag, 2, safeBagMaximumFields) if err != nil { return ErrFormat } @@ -355,7 +433,7 @@ func (p *identityParser) parseSafeBag(bag asn1.RawValue) error { } var id []byte - if len(f) == 3 { + if len(f) == safeBagMaximumFields { id, err = bagID(f[2]) if err != nil { return err @@ -376,13 +454,13 @@ func (p *identityParser) parseSafeBag(bag asn1.RawValue) error { return ErrLimit } - cert, err := der.Sequence(value, 2, 2) + cert, err := der.Sequence(value, certificateBagFields, certificateBagFields) if err != nil || !der.OIDEqual(cert[0], oidX509Cert()) { return ErrFormat } data, err := der.Explicit(cert[1], 0) - if err != nil || !der.Is(data, 0, asn1.TagOctetString, false) { + if err != nil || !der.Is(data, asn1.ClassUniversal, asn1.TagOctetString, false) { return ErrFormat } @@ -437,50 +515,10 @@ func (p *identityParser) privateBag(value asn1.RawValue, id []byte, shrouded boo return nil } -// PKCS12Options selects encryption and integrity for one DER PKCS#12 identity. -// Both integrity modes require a MAC and independently generated salts. Export -// rejects RC2, 3DES, SHA-1, BER and containers without a MAC. Password bytes must be -// UTF-8 and nonempty; only import accepts an empty password. -type PKCS12Options struct { - // Encryption selects the private-key encryption parameters. Zero fields select - // defaults. - Encryption EncryptionOptions - // ClassicMAC selects RFC 7292 SHA-2 integrity and requires a BMP-only password. - // False selects RFC 9879 PBMAC1 with UTF-8 passwords. - ClassicMAC bool -} - -// MarshalPKCS12 exports one private key, its matching certificate and optional -// additional certificates to a password-authenticated DER PFX. It encrypts both -// certificate SafeContents and the PKCS#8 key with separate PBES2 salts/IVs; -// the MAC is mandatory. Zero options use RFC 9879 PBMAC1. ClassicMAC explicitly -// selects RFC 7292 SHA-2 integrity for older readers. Certificate objects are -// parsed from their Raw DER, ownership is copied and no trust is inferred. -// Password must be nonempty UTF-8. It performs no file/network I/O. -func MarshalPKCS12(ctx context.Context, identity PKCS12Identity, password []byte, options PKCS12Options, limits Limits) ([]byte, error) { - parts, err := preparePKCS12(ctx, identity, password, options, limits) - if err != nil { - return nil, err - } - - return parts.bytes(ctx) -} - -// WritePKCS12 exports a password-authenticated PFX directly to dst. It retains -// certificate snapshots and ciphertext but does not build a complete PFX or -// repeated nested DER buffers. Limits and KDF work are checked before output. -// The destination stays open and may contain partial output on error. -func WritePKCS12(ctx context.Context, dst io.Writer, identity PKCS12Identity, password []byte, options PKCS12Options, limits Limits) (int64, error) { - if nilcheck.IsNil(dst) { - return 0, ErrPolicy - } - - parts, err := preparePKCS12(ctx, identity, password, options, limits) - if err != nil { - return 0, err - } - - return parts.write(ctx, dst) +type privateBag struct { + key crypto.Signer + id []byte + public []byte } func oidData() asn1.ObjectIdentifier { return asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 1} } @@ -503,12 +541,6 @@ func oidX509Cert() asn1.ObjectIdentifier { func oidLocalKeyID() asn1.ObjectIdentifier { return asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 21} } -type privateBag struct { - key crypto.Signer - id []byte - public []byte -} - func oidKeyBag() asn1.ObjectIdentifier { return asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 12, 10, 1, 1} } @@ -518,7 +550,20 @@ func oidFriendlyName() asn1.ObjectIdentifier { } func bagID(attributes asn1.RawValue) ([]byte, error) { - fields, err := der.SetFields(attributes, 0, asn1.TagSet, 2) + // The supported bag metadata has localKeyId/friendlyName, each single-valued. + const ( + maximumBagAttributes = 2 + bagAttributeFields = 2 + bagAttributeValues = 1 + ) + + // BMPString excludes UTF-16 surrogate code points. + const ( + bmpSurrogateFirst = 0xd800 + bmpSurrogateLast = 0xdfff + ) + + fields, err := der.SetFields(attributes, asn1.ClassUniversal, asn1.TagSet, maximumBagAttributes) if err != nil { return nil, ErrFormat } @@ -528,13 +573,13 @@ func bagID(attributes asn1.RawValue) ([]byte, error) { nameSeen, idSeen := false, false for _, attr := range fields { - f, err := der.Sequence(attr, 2, 2) + f, err := der.Sequence(attr, bagAttributeFields, bagAttributeFields) if err != nil { return nil, ErrFormat } - values, err := der.SetFields(f[1], 0, asn1.TagSet, 1) - if err != nil || len(values) != 1 { + values, err := der.SetFields(f[1], asn1.ClassUniversal, asn1.TagSet, bagAttributeValues) + if err != nil || len(values) != bagAttributeValues { return nil, ErrFormat } @@ -542,20 +587,20 @@ func bagID(attributes asn1.RawValue) ([]byte, error) { switch { case der.OIDEqual(f[0], oidLocalKeyID()): - if idSeen || !der.Is(value, 0, asn1.TagOctetString, false) || len(value.Bytes) == 0 || len(value.Bytes) > 64 { + if idSeen || !der.Is(value, asn1.ClassUniversal, asn1.TagOctetString, false) || len(value.Bytes) == 0 || len(value.Bytes) > maximumLocalKeyIDBytes { return nil, ErrFormat } idSeen = true id = bytes.Clone(value.Bytes) case der.OIDEqual(f[0], oidFriendlyName()): - if nameSeen || !der.Is(value, 0, asn1.TagBMPString, false) || len(value.Bytes) > 512 || len(value.Bytes)%2 != 0 { + if nameSeen || !der.Is(value, asn1.ClassUniversal, asn1.TagBMPString, false) || len(value.Bytes) > maximumFriendlyNameBytes || len(value.Bytes)%bmpCodeUnitBytes != 0 { return nil, ErrFormat } - for i := 0; i < len(value.Bytes); i += 2 { - r := uint16(value.Bytes[i])<<8 | uint16(value.Bytes[i+1]) - if r >= 0xd800 && r <= 0xdfff { + for i := 0; i < len(value.Bytes); i += bmpCodeUnitBytes { + r := uint16(value.Bytes[i])<= bmpSurrogateFirst && r <= bmpSurrogateLast { return nil, ErrFormat } } @@ -667,7 +712,7 @@ func preparePKCS12(ctx context.Context, identity PKCS12Identity, password []byte defer clear(private) certs := make([]*x509.Certificate, 0, 1+len(identity.ExtraCertificates)) - seen := make(map[[32]byte]struct{}) + seen := make(map[[sha256.Size]byte]struct{}) totalBytes := len(private) certificateLimits := state.limits certificateLimits.MaxInputBytes = state.limits.MaxCertificateBytes @@ -715,36 +760,36 @@ func preparePKCS12(ctx context.Context, identity PKCS12Identity, password []byte attrs = attributes } - value := frameDER(0x30, derParts{der.OID(oidX509Cert())}, frameDER(0xa0, frameDER(4, derParts{c.Raw}))) - certBags = append(certBags, frameDER(0x30, derParts{der.OID(oidCertBag())}, frameDER(0xa0, value), derParts{attrs})) + value := frameDER(encodedSequenceTag, derParts{der.OID(oidX509Cert())}, frameDER(encodedContext0Tag, frameDER(asn1.TagOctetString, derParts{c.Raw}))) + certBags = append(certBags, frameDER(encodedSequenceTag, derParts{der.OID(oidCertBag())}, frameDER(encodedContext0Tag, value), derParts{attrs})) } - certContents := frameDER(0x30, certBags...) + certContents := frameDER(encodedSequenceTag, certBags...) certAlg, certCipher, err := encryptPBES2Parts(state, certContents, options.Encryption) if err != nil { return nil, err } - certInfo := frameDER(0x30, derParts{der.OID(oidEncryptedData())}, frameDER(0xa0, frameDER(0x30, derParts{der.Int(0)}, frameDER(0x30, derParts{der.OID(oidData()), certAlg}, frameDER(0x80, derParts{certCipher}))))) + certInfo := frameDER(encodedSequenceTag, derParts{der.OID(oidEncryptedData())}, frameDER(encodedContext0Tag, frameDER(encodedSequenceTag, derParts{der.Int(pkcs12EncryptedDataVersion)}, frameDER(encodedSequenceTag, derParts{der.OID(oidData()), certAlg}, frameDER(encodedPrimitiveContext0Tag, derParts{certCipher}))))) keyAlg, keyCipher, err := encryptPBES2(state, private, options.Encryption) if err != nil { return nil, err } - keyInfo := frameDER(0x30, derParts{keyAlg}, frameDER(4, derParts{keyCipher})) - keyBag := frameDER(0x30, derParts{der.OID(oidShroudedKeyBag())}, frameDER(0xa0, keyInfo), derParts{attributes}) - keyContents := frameDER(0x30, keyBag) - keyContentInfo := frameDER(0x30, derParts{der.OID(oidData())}, frameDER(0xa0, frameDER(4, keyContents))) - authSafe := frameDER(0x30, certInfo, keyContentInfo) + keyInfo := frameDER(encodedSequenceTag, derParts{keyAlg}, frameDER(asn1.TagOctetString, derParts{keyCipher})) + keyBag := frameDER(encodedSequenceTag, derParts{der.OID(oidShroudedKeyBag())}, frameDER(encodedContext0Tag, keyInfo), derParts{attributes}) + keyContents := frameDER(encodedSequenceTag, keyBag) + keyContentInfo := frameDER(encodedSequenceTag, derParts{der.OID(oidData())}, frameDER(encodedContext0Tag, frameDER(asn1.TagOctetString, keyContents))) + authSafe := frameDER(encodedSequenceTag, certInfo, keyContentInfo) mac, err := encodePKCS12MACParts(state, authSafe, options) if err != nil { return nil, err } - out := frameDER(0x30, derParts{der.Int(3)}, frameDER(0x30, derParts{der.OID(oidData())}, frameDER(0xa0, frameDER(4, authSafe))), derParts{mac}) + out := frameDER(encodedSequenceTag, derParts{der.Int(pkcs12Version)}, frameDER(encodedSequenceTag, derParts{der.OID(oidData())}, frameDER(encodedContext0Tag, frameDER(asn1.TagOctetString, authSafe))), derParts{mac}) if out.size() > state.limits.MaxContainerBytes { return nil, ErrLimit } @@ -781,7 +826,7 @@ func (p derParts) write(ctx context.Context, dst io.Writer) (int64, error) { writer := &streamio.Writer{Context: ctx, Target: dst, Limit: int64(p.size()), LimitError: ErrLimit} for _, part := range p { for len(part) > 0 { - chunk := part[:min(len(part), 32<<10)] + chunk := part[:min(len(part), exportChunkBytes)] if _, err := writer.Write(chunk); err != nil { return writer.Written, errors.Join(err, ctx.Err()) } diff --git a/keys/pkcs12_forms.go b/keys/pkcs12_forms.go index 4679f6a..986d1d8 100644 --- a/keys/pkcs12_forms.go +++ b/keys/pkcs12_forms.go @@ -30,7 +30,7 @@ func normalizePKCS12BER(state *passwordState, input []byte, form pkcs12Form) ([] return nil, pkcs12BERError(err) } - if root.Tag != 0x30 { + if root.Tag != encodedSequenceTag { return nil, ErrFormat } @@ -38,7 +38,7 @@ func normalizePKCS12BER(state *passwordState, input []byte, form pkcs12Form) ([] switch form { case pfxForm: - if len(root.Children) != 3 { + if len(root.Children) != pfxRequiredFields { return nil, ErrFormat } @@ -94,7 +94,7 @@ func normalizePKCS12BER(state *passwordState, input []byte, form pkcs12Form) ([] } func pkcs12BERInfo(state *passwordState, info ber.Value, encrypted bool) ([]byte, error) { - if info.Tag != 0x30 || len(info.Children) != 2 { + if info.Tag != encodedSequenceTag || len(info.Children) != 2 { return nil, ErrFormat } @@ -104,7 +104,7 @@ func pkcs12BERInfo(state *passwordState, info ber.Value, encrypted bool) ([]byte } explicit := info.Children[1] - if explicit.Tag != 0xa0 || len(explicit.Children) != 1 { + if explicit.Tag != encodedContext0Tag || len(explicit.Children) != 1 { return nil, ErrFormat } @@ -137,33 +137,36 @@ func pkcs12BERInfo(state *passwordState, info ber.Value, encrypted bool) ([]byte } func pkcs12BEREncrypted(state *passwordState, content ber.Value) ([]byte, error) { - if content.Tag != 0x30 || len(content.Children) != 2 { + // Keep the constructed bit while restoring the universal OCTET STRING tag. + const constructedIdentifierBit = 0x20 + + if content.Tag != encodedSequenceTag || len(content.Children) != 2 { return nil, ErrFormat } info := content.Children[1] - if info.Tag != 0x30 || len(info.Children) != 3 { + if info.Tag != encodedSequenceTag || len(info.Children) != encryptedContentInfoFields { return nil, ErrFormat } cipher := info.Children[2] - if cipher.Tag != 0x80 && cipher.Tag != 0xa0 { + if cipher.Tag != encodedPrimitiveContext0Tag && cipher.Tag != encodedContext0Tag { return nil, ErrFormat } // The implicit OCTET STRING has universal OCTET STRING segments when // constructed. Flatten only its string encoding, never ciphertext bytes. - cipher.Tag = (cipher.Tag & 0x20) | 4 + cipher.Tag = (cipher.Tag & constructedIdentifierBit) | asn1.TagOctetString var output bytes.Buffer if err := ber.AppendOctets(state.ctx, cipher, &output, state.limits.MaxContainerBytes); err != nil { return nil, pkcs12BERError(err) } - return der.Seq(content.Children[0].Full, der.Seq(info.Children[0].Full, info.Children[1].Full, der.Wrap(2, 0, false, output.Bytes()))), nil + return der.Seq(content.Children[0].Full, der.Seq(info.Children[0].Full, info.Children[1].Full, der.Wrap(asn1.ClassContextSpecific, 0, false, output.Bytes()))), nil } func pkcs12BERSafe(state *passwordState, contents ber.Value) ([]byte, error) { - if contents.Tag != 0x30 { + if contents.Tag != encodedSequenceTag { return nil, ErrFormat } @@ -183,7 +186,7 @@ func pkcs12BERSafe(state *passwordState, contents ber.Value) ([]byte, error) { return nil, err } - if bag.Tag != 0x30 || len(bag.Children) < 2 || len(bag.Children) > 3 { + if bag.Tag != encodedSequenceTag || len(bag.Children) < 2 || len(bag.Children) > safeBagMaximumFields { return nil, ErrFormat } @@ -193,7 +196,7 @@ func pkcs12BERSafe(state *passwordState, contents ber.Value) ([]byte, error) { } explicit := bag.Children[1] - if explicit.Tag != 0xa0 || len(explicit.Children) != 1 { + if explicit.Tag != encodedContext0Tag || len(explicit.Children) != 1 { return nil, ErrFormat } @@ -221,7 +224,7 @@ func pkcs12BERSafe(state *passwordState, contents ber.Value) ([]byte, error) { clear(value) } - if len(bag.Children) == 3 { + if len(bag.Children) == safeBagMaximumFields { fields = append(fields, bag.Children[2].Full) } @@ -233,12 +236,12 @@ func pkcs12BERSafe(state *passwordState, contents ber.Value) ([]byte, error) { } func pkcs12BERCertificate(state *passwordState, value ber.Value) ([]byte, error) { - if value.Tag != 0x30 || len(value.Children) != 2 { + if value.Tag != encodedSequenceTag || len(value.Children) != 2 { return nil, ErrFormat } explicit := value.Children[1] - if explicit.Tag != 0xa0 || len(explicit.Children) != 1 { + if explicit.Tag != encodedContext0Tag || len(explicit.Children) != 1 { return nil, ErrFormat } diff --git a/keys/pkcs12_mac.go b/keys/pkcs12_mac.go index 207f4df..c515ac9 100644 --- a/keys/pkcs12_mac.go +++ b/keys/pkcs12_mac.go @@ -14,11 +14,19 @@ import ( "crypto/hmac" "crypto/rand" "encoding/asn1" + "math" "unicode/utf8" "github.com/skarm/xmlsec/internal/der" ) +const ( + defaultPKCS12MACSaltBytes = 16 + minimumNonDefaultMACKeyBytes = 16 + // BMP code units are big-endian two-octet values, including the password terminator. + bmpCodeUnitBytes = 2 +) + func oidSHA1() asn1.ObjectIdentifier { return asn1.ObjectIdentifier{1, 3, 14, 3, 2, 26} } func oidSHA256() asn1.ObjectIdentifier { return asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 1} } @@ -43,17 +51,17 @@ func bmpPassword(password []byte) ([]byte, error) { return nil, ErrPolicy } - out := make([]byte, 0, len(password)*2+2) + out := make([]byte, 0, len(password)*bmpCodeUnitBytes+bmpCodeUnitBytes) for len(password) > 0 { r, size := utf8.DecodeRune(password) password = password[size:] - if r > 0xffff { + if r > math.MaxUint16 { clear(out) return nil, ErrPolicy } - out = append(out, byte((r>>8)&0xff), byte(r&0xff)) + out = append(out, byte((r>>bitsPerOctet)&math.MaxUint8), byte(r&math.MaxUint8)) } return append(out, 0, 0), nil @@ -66,7 +74,10 @@ func validPKCS12Password(password []byte) bool { return utf8.Valid(password) } // classicMACKey derives a PKCS#12 diversifier-3 MAC key after hash, salt and // iteration-budget checks. The caller must clear the returned key after use. func classicMACKey(state *passwordState, hash crypto.Hash, salt []byte, iterations int) ([]byte, error) { - if !state.acceptHash(hash) || len(salt) < 8 || len(salt) > 64 { + // PKCS#12 Appendix B.3 selects MAC material with diversifier ID 3. + const macDiversifier = 3 + + if !state.acceptHash(hash) || len(salt) < minimumSaltBytes || len(salt) > maximumSaltBytes { return nil, ErrPolicy } @@ -81,7 +92,7 @@ func classicMACKey(state *passwordState, hash crypto.Hash, salt []byte, iteratio defer clear(password) h := hash.New() - d := bytes.Repeat([]byte{3}, h.BlockSize()) + d := bytes.Repeat([]byte{macDiversifier}, h.BlockSize()) s, p := repeatBlock(salt, h.BlockSize()), repeatBlock(password, h.BlockSize()) defer clear(p) @@ -92,7 +103,7 @@ func classicMACKey(state *passwordState, hash crypto.Hash, salt []byte, iteratio out := h.Sum(nil) for i := 1; i < iterations; i++ { - if i%1024 == 0 { + if i%kdfCancellationInterval == 0 { if err := state.ctx.Err(); err != nil { clear(out) return nil, err @@ -118,7 +129,7 @@ func digestHash(v asn1.RawValue) (crypto.Hash, error) { return 0, err } - if len(param.FullBytes) > 0 && !der.Is(param, 0, asn1.TagNull, false) { + if len(param.FullBytes) > 0 && !der.Is(param, asn1.ClassUniversal, asn1.TagNull, false) { return 0, ErrFormat } @@ -140,13 +151,22 @@ func digestHash(v asn1.RawValue) (crypto.Hash, error) { // It returns the digest algorithm, owned key bytes and borrowed expected MAC; callers // must clear the key. func pkcs12MACKey(state *passwordState, mac asn1.RawValue) (crypto.Hash, []byte, []byte, error) { - f, err := der.Sequence(mac, 2, 3) - if err != nil || !der.Is(f[1], 0, asn1.TagOctetString, false) { + // MacData carries DigestInfo, salt and optional iterations. DigestInfo + // binds its algorithm to bytes; PBMAC1 binds KDF and MAC algorithm choices. + const ( + macDataMinimumFields = 2 + macDataMaximumFields = 3 + digestInfoFields = 2 + pbmac1ParameterFields = 2 + ) + + f, err := der.Sequence(mac, macDataMinimumFields, macDataMaximumFields) + if err != nil || !der.Is(f[1], asn1.ClassUniversal, asn1.TagOctetString, false) { return 0, nil, nil, ErrFormat } - info, err := der.Sequence(f[0], 2, 2) - if err != nil || !der.Is(info[1], 0, asn1.TagOctetString, false) { + info, err := der.Sequence(f[0], digestInfoFields, digestInfoFields) + if err != nil || !der.Is(info[1], asn1.ClassUniversal, asn1.TagOctetString, false) { return 0, nil, nil, ErrFormat } @@ -157,13 +177,13 @@ func pkcs12MACKey(state *passwordState, mac asn1.RawValue) (crypto.Hash, []byte, if oid.Equal(oidPBMAC1()) { // RFC 9879: outer macSalt/iterations are syntactic placeholders only. - if len(f) == 3 { - if !der.Is(f[2], 0, asn1.TagInteger, false) { + if len(f) == macDataMaximumFields { + if !der.Is(f[2], asn1.ClassUniversal, asn1.TagInteger, false) { return 0, nil, nil, ErrFormat } } - p, err := der.Sequence(params, 2, 2) + p, err := der.Sequence(params, pbmac1ParameterFields, pbmac1ParameterFields) if err != nil { return 0, nil, nil, ErrFormat } @@ -178,7 +198,7 @@ func pkcs12MACKey(state *passwordState, mac asn1.RawValue) (crypto.Hash, []byte, } kdf, err := parseKDF(p[0], hash.Size(), true) - if err == nil && (!sha2(kdf.hash) || kdf.keyBytes != hash.Size() && (!state.allowNonDefaultMACKeyLength || kdf.keyBytes < 16)) { + if err == nil && (!sha2(kdf.hash) || kdf.keyBytes != hash.Size() && (!state.allowNonDefaultMACKeyLength || kdf.keyBytes < minimumNonDefaultMACKeyBytes)) { err = ErrPolicy } @@ -201,7 +221,7 @@ func pkcs12MACKey(state *passwordState, mac asn1.RawValue) (crypto.Hash, []byte, } iterations := 1 - if len(f) == 3 { + if len(f) == macDataMaximumFields { iterations, err = integer(f[2]) if err != nil { return 0, nil, nil, err @@ -239,7 +259,7 @@ func encodePKCS12MAC(state *passwordState, message []byte, options PKCS12Options } func encodePKCS12MACParts(state *passwordState, message derParts, options PKCS12Options) ([]byte, error) { - salt := make([]byte, 16) + salt := make([]byte, defaultPKCS12MACSaltBytes) if _, err := rand.Read(salt); err != nil { return nil, err } @@ -261,10 +281,10 @@ func encodePKCS12MACParts(state *passwordState, message derParts, options PKCS12 oid = oidSHA512() } - alg = der.Seq(der.OID(oid), []byte{5, 0}) + alg = der.Seq(der.OID(oid), []byte{asn1.TagNull, 0}) } else { key, err = state.derive(hash, salt, options.Encryption.Iterations, hash.Size()) - params := der.Seq(kdfEncoding(salt, options.Encryption.Iterations, hash.Size(), hash), der.Seq(der.OID(hmacOID(hash)), []byte{5, 0})) + params := der.Seq(kdfEncoding(salt, options.Encryption.Iterations, hash.Size(), hash), der.Seq(der.OID(hmacOID(hash)), []byte{asn1.TagNull, 0})) alg = der.Seq(der.OID(oidPBMAC1()), params) } diff --git a/keys/pkcs8.go b/keys/pkcs8.go index feeb1b5..9836c2b 100644 --- a/keys/pkcs8.go +++ b/keys/pkcs8.go @@ -16,6 +16,7 @@ import ( "github.com/skarm/xmlsec/internal/der" "github.com/skarm/xmlsec/internal/ecdsakey" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/nilcheck" ) @@ -62,7 +63,7 @@ func ParsePrivateWithOptions(ctx context.Context, data []byte, options PrivatePa } defer decoded.clear() - if options.AllowBER && (decoded.label == "" || decoded.label == "PRIVATE KEY") { + if options.AllowBER && (decoded.label == "" || decoded.label == lexicon.PEMPrivateKey) { // An unlabelled traditional key is already a strict DER format. Detect // only PrivateKeyInfo's AlgorithmIdentifier in the second field. normalized, err := normalizePrivateBER(ctx, decoded.der, limits.MaxKeyBytes, limits, false) @@ -115,7 +116,7 @@ func ParseEncryptedPrivateWithOptions(ctx context.Context, data, password []byte } defer decoded.clear() - if decoded.label != "" && decoded.label != "ENCRYPTED PRIVATE KEY" { + if decoded.label != "" && decoded.label != lexicon.PEMEncryptedPrivateKey { return nil, ErrFormat } @@ -167,7 +168,7 @@ func privateMaterial(ctx context.Context, data []byte, maximum int, limits Limit } trimmed := bytes.TrimSpace(data) - if bytes.HasPrefix(trimmed, []byte("-----BEGIN ")) { + if bytes.HasPrefix(trimmed, []byte(pemBeginMarker)) { return decodePEM(ctx, trimmed, maximum) } @@ -179,6 +180,8 @@ func privateMaterial(ctx context.Context, data []byte, maximum int, limits Limit } func decryptPrivateInfo(state *passwordState, raw []byte) (crypto.Signer, error) { + const encryptedPrivateKeyInfoFields = 2 // encryptionAlgorithm and encryptedData + if state.allowBER { normalized, err := normalizePrivateBER(state.ctx, raw, state.limits.MaxContainerBytes, state.limits, true) if err != nil { @@ -194,8 +197,8 @@ func decryptPrivateInfo(state *passwordState, raw []byte) (crypto.Signer, error) return nil, err } - f, err := der.Sequence(v, 2, 2) - if err != nil || !der.Is(f[1], 0, asn1.TagOctetString, false) { + f, err := der.Sequence(v, encryptedPrivateKeyInfoFields, encryptedPrivateKeyInfoFields) + if err != nil || !der.Is(f[1], asn1.ClassUniversal, asn1.TagOctetString, false) { return nil, ErrFormat } @@ -237,7 +240,7 @@ func parseDecryptedPrivate(state *passwordState, plain []byte) (crypto.Signer, e return nil, ErrDecrypt } - key, err := parsePrivateDER("PRIVATE KEY", plain, state.limits) + key, err := parsePrivateDER(lexicon.PEMPrivateKey, plain, state.limits) if err != nil { if !errors.Is(err, ErrLimit) { return nil, ErrDecrypt @@ -387,7 +390,7 @@ func prepareEncryptedPrivate(ctx context.Context, key crypto.Signer, password [] return nil, err } - out := frameDER(0x30, derParts{alg}, frameDER(4, derParts{encrypted})) + out := frameDER(encodedSequenceTag, derParts{alg}, frameDER(asn1.TagOctetString, derParts{encrypted})) if out.size() > state.limits.MaxContainerBytes { return nil, ErrLimit } diff --git a/keys/pkcs8_forms.go b/keys/pkcs8_forms.go index 59139f3..f69cc31 100644 --- a/keys/pkcs8_forms.go +++ b/keys/pkcs8_forms.go @@ -17,13 +17,13 @@ func normalizePrivateBER(ctx context.Context, input []byte, maximum int, limits return nil, pkcs12BERError(err) } - if root.Tag != 0x30 { + if root.Tag != encodedSequenceTag { return nil, ErrFormat } // Preserve strict DER PKCS#1/SEC1 inputs on the unencrypted API. A BER // traditional key remains invalid; compatibility is confined to PKCS#8. - if !encrypted && len(root.Children) >= 2 && root.Children[1].Tag != 0x30 { + if !encrypted && len(root.Children) >= 2 && root.Children[1].Tag != encodedSequenceTag { if _, err := readDER(input, maximum, limits); err != nil { return nil, err } @@ -39,7 +39,7 @@ func normalizePrivateBER(ctx context.Context, input []byte, maximum int, limits } at = 1 - } else if len(root.Children) < 3 || len(root.Children) > 4 { + } else if len(root.Children) < privateKeyInfoMinimumFields || len(root.Children) > privateKeyInfoMaximumFields { return nil, ErrFormat } diff --git a/keys/private_structure.go b/keys/private_structure.go index 7b5af60..69d9ecb 100644 --- a/keys/private_structure.go +++ b/keys/private_structure.go @@ -12,30 +12,50 @@ import ( "github.com/skarm/xmlsec/internal/der" ) +const ( + // Count caps for optional PKCS#8 attributes and each attribute's SET OF values. + maximumPrivateKeyAttributeValues = 16 + maximumPrivateKeyAttributes = 16 +) + +// PrivateKeyInfo has three required fields and an optional attributes field. +const ( + privateKeyInfoMinimumFields = 3 + privateKeyInfoMaximumFields = 4 +) + // crypto/x509 intentionally accepts some malformed legacy key representations. // Check complete structures, including fields that x509 ignores. The key is // owned by this parse, so RSA marshaling cannot mutate a caller's key. func validatePrivateStructure(raw []byte, key crypto.Signer, limits Limits) error { + // Every supported outer key schema has at least two fields; PKCS#1 with + // OtherPrimeInfos is the largest. EC algorithm parameters carry the curve OID. + const ( + minimumPrivateKeyStructureFields = 2 + maximumPrivateKeyStructureFields = 10 + ecAlgorithmIdentifierFields = 2 + ) + v, err := readDER(raw, limits.MaxKeyBytes, limits) if err != nil { return err } - f, err := der.Sequence(v, 2, 10) + f, err := der.Sequence(v, minimumPrivateKeyStructureFields, maximumPrivateKeyStructureFields) if err != nil { return ErrFormat } var outerCurve asn1.ObjectIdentifier - if der.Is(f[1], 0, asn1.TagSequence, true) { + if der.Is(f[1], asn1.ClassUniversal, asn1.TagSequence, true) { f, err = privateKeyInfoFields(v) if err != nil { return err } if _, ok := key.(*ecdsa.PrivateKey); ok { - algorithm, err := der.Sequence(f[1], 2, 2) + algorithm, err := der.Sequence(f[1], ecAlgorithmIdentifierFields, ecAlgorithmIdentifierFields) if err != nil || der.Decode(algorithm[1], &outerCurve) != nil { return ErrFormat } @@ -67,22 +87,24 @@ func validatePrivateStructure(raw []byte, key crypto.Signer, limits Limits) erro } func privateKeyInfoFields(v asn1.RawValue) ([]asn1.RawValue, error) { - f, err := der.Sequence(v, 3, 4) - if err != nil || !der.Is(f[2], 0, asn1.TagOctetString, false) { + const privateKeyInfoVersion = 0 // original PKCS#8 PrivateKeyInfo encoded version + + f, err := der.Sequence(v, privateKeyInfoMinimumFields, privateKeyInfoMaximumFields) + if err != nil || !der.Is(f[2], asn1.ClassUniversal, asn1.TagOctetString, false) { return nil, ErrFormat } var version int - if der.Decode(f[0], &version) != nil || version != 0 { + if der.Decode(f[0], &version) != nil || version != privateKeyInfoVersion { return nil, ErrFormat } - algo, err := der.Sequence(f[1], 1, 2) - if err != nil || !der.Is(algo[0], 0, asn1.TagOID, false) { + algo, err := der.Sequence(f[1], algorithmIdentifierMinimumFields, algorithmIdentifierMaximumFields) + if err != nil || !der.Is(algo[0], asn1.ClassUniversal, asn1.TagOID, false) { return nil, ErrFormat } - if len(f) == 4 { + if len(f) == privateKeyInfoMaximumFields { if err := validatePrivateAttributes(f[3]); err != nil { return nil, err } @@ -92,18 +114,20 @@ func privateKeyInfoFields(v asn1.RawValue) ([]asn1.RawValue, error) { } func validatePrivateAttributes(v asn1.RawValue) error { - attrs, err := der.SetFields(v, 2, 0, 16) + const attributeFields = 2 // type OID and SET OF attribute values + + attrs, err := der.SetFields(v, asn1.ClassContextSpecific, 0, maximumPrivateKeyAttributes) if err != nil { return ErrFormat } for _, attr := range attrs { - fields, err := der.Sequence(attr, 2, 2) - if err != nil || !der.Is(fields[0], 0, asn1.TagOID, false) { + fields, err := der.Sequence(attr, attributeFields, attributeFields) + if err != nil || !der.Is(fields[0], asn1.ClassUniversal, asn1.TagOID, false) { return ErrFormat } - values, err := der.SetFields(fields[1], 0, asn1.TagSet, 16) + values, err := der.SetFields(fields[1], asn1.ClassUniversal, asn1.TagSet, maximumPrivateKeyAttributeValues) if err != nil || len(values) == 0 { return ErrFormat } @@ -113,8 +137,11 @@ func validatePrivateAttributes(v asn1.RawValue) error { } func validateECStructure(v asn1.RawValue, key *ecdsa.PrivateKey, outerCurve asn1.ObjectIdentifier) error { - f, err := der.Sequence(v, 2, 4) - if err != nil || !der.Is(f[1], 0, asn1.TagOctetString, false) || len(f[1].Bytes) != (key.Params().N.BitLen()+7)/8 { + // ECPrivateKey has two required fields and two optional tagged fields. + const maximumECPrivateFields = 4 + + f, err := der.Sequence(v, 2, maximumECPrivateFields) + if err != nil || !der.Is(f[1], asn1.ClassUniversal, asn1.TagOctetString, false) || len(f[1].Bytes) != (key.Params().N.BitLen()+bitsPerOctet-1)/bitsPerOctet { return ErrFormat } @@ -142,7 +169,7 @@ func validateECStructure(v asn1.RawValue, key *ecdsa.PrivateKey, outerCurve asn1 } i := 2 - if i < len(f) && der.Is(f[i], 2, 0, true) { + if i < len(f) && der.Is(f[i], asn1.ClassContextSpecific, 0, true) { value, err := der.Explicit(f[i], 0) var curve asn1.ObjectIdentifier @@ -155,7 +182,7 @@ func validateECStructure(v asn1.RawValue, key *ecdsa.PrivateKey, outerCurve asn1 return ErrFormat } - if i < len(f) && der.Is(f[i], 2, 1, true) { + if i < len(f) && der.Is(f[i], asn1.ClassContextSpecific, 1, true) { value, err := der.Explicit(f[i], 1) var public asn1.BitString diff --git a/pki/algorithms.go b/pki/algorithms.go index 460d6c3..195056b 100644 --- a/pki/algorithms.go +++ b/pki/algorithms.go @@ -12,12 +12,19 @@ import ( "github.com/skarm/xmlsec/internal/ecdsakey" ) +const ( + maximumRSAKeyBits = 16384 + // Smallest permitted odd RSA public exponent in the supported key profile. + minimumRSAExponent = 3 + minimumRSAKeyBits = 2048 +) + // This package currently uses a fixed modern offline validation profile. It does // not inherit GODEBUG exceptions allowing SHA-1 or small RSA keys. func allowedPublicKey(key crypto.PublicKey) error { switch k := key.(type) { case *rsa.PublicKey: - if k == nil || k.N == nil || k.N.BitLen() < 2048 || k.N.BitLen() > 16384 || k.E < 3 || k.E%2 == 0 { + if k == nil || k.N == nil || k.N.BitLen() < minimumRSAKeyBits || k.N.BitLen() > maximumRSAKeyBits || k.E < minimumRSAExponent || k.E%2 == 0 { return fmt.Errorf("%w: RSA key strength", ErrPolicy) } case *ecdsa.PublicKey: diff --git a/pki/clone.go b/pki/clone.go index 2755a11..9b0aff5 100644 --- a/pki/clone.go +++ b/pki/clone.go @@ -10,7 +10,7 @@ import ( // rules, replacing ONLY untrusted path-building certificates. It is useful for // checking that a document supplies its own validation material. func (v *Verifier) WithIntermediateCertificates(raw [][]byte) (*Verifier, error) { - if v == nil || len(raw) > 64 { + if v == nil || len(raw) > maxIntermediateCertificates { return nil, ErrPolicy } @@ -18,7 +18,7 @@ func (v *Verifier) WithIntermediateCertificates(raw [][]byte) (*Verifier, error) out.intermediates = x509.NewCertPool() out.issuerCertificates = append([][]byte(nil), v.anchorCertificates...) - out.originalCertificates = make(map[[32]byte]*x509.Certificate, len(v.anchorCertificates)+len(raw)) + out.originalCertificates = make(map[[sha256.Size]byte]*x509.Certificate, len(v.anchorCertificates)+len(raw)) for _, source := range v.anchorCertificates { digest := sha256.Sum256(source) diff --git a/pki/crl.go b/pki/crl.go index cc370ca..0b68f7d 100644 --- a/pki/crl.go +++ b/pki/crl.go @@ -13,7 +13,7 @@ import ( func safeMarshalPublic(public crypto.PublicKey) ([]byte, error) { switch k := public.(type) { case *rsa.PublicKey: - if k == nil || k.N == nil || k.N.Sign() <= 0 || k.E < 3 { + if k == nil || k.N == nil || k.N.Sign() <= 0 || k.E < minimumRSAExponent { return nil, ErrCertificate } case *ecdsa.PublicKey: diff --git a/pki/crl_combine.go b/pki/crl_combine.go index d71ccbd..53868cc 100644 --- a/pki/crl_combine.go +++ b/pki/crl_combine.go @@ -7,6 +7,13 @@ import ( "math/big" "sort" "time" + + "github.com/skarm/xmlsec/internal/lexicon" +) + +const ( + // Polls the context once per 256 revocation entries during list processing. + revocationCancellationMask = 255 ) func constructCRLViews(ctx context.Context, group []*authenticatedCRL, at time.Time, o CRLOptions) ([]completeCRLView, error) { @@ -134,7 +141,7 @@ func (state crlState) apply(entry *crlEntry) { } } - if entry.reason == 8 { + if entry.reason == reasonRemoveFromCRL { return } @@ -147,7 +154,7 @@ func (state crlState) apply(entry *crlEntry) { func crlEntryNames(entry *crlEntry) []string { keys := make([]string, 0, len(entry.issuers)) for _, name := range entry.issuers { - if name.tag == 4 { + if name.tag == lexicon.GeneralNameDirectory { keys = append(keys, name.key+"/"+entry.serial.String()) } } @@ -160,7 +167,7 @@ func crlViewState(ctx context.Context, view completeCRLView) (crlState, error) { apply := func(entries []crlEntry) error { for index := range entries { - if index&255 == 0 { + if index&revocationCancellationMask == 0 { if err := ctx.Err(); err != nil { return err } diff --git a/pki/crl_names.go b/pki/crl_names.go index 52c1248..71a218d 100644 --- a/pki/crl_names.go +++ b/pki/crl_names.go @@ -7,6 +7,13 @@ import ( "github.com/skarm/xmlsec/internal/lexicon" ) +const ( + ftpDefaultPort = "21" + httpDefaultPort = "80" + httpsDefaultPort = "443" + ldapDefaultPort = "389" +) + const ( crlSchemeFTP = "ftp" crlSchemeLDAP = "ldap" @@ -50,13 +57,13 @@ func crlURIKey(raw string) string { switch scheme { case lexicon.SchemeHTTP: - port = "80" + port = httpDefaultPort case lexicon.SchemeHTTPS: - port = "443" + port = httpsDefaultPort case crlSchemeFTP: - port = "21" + port = ftpDefaultPort case crlSchemeLDAP: - port = "389" + port = ldapDefaultPort } if port != "" { @@ -84,7 +91,13 @@ func crlURIKey(raw string) string { } func normalizeCRLPercent(value string) string { - const hex = "0123456789ABCDEF" + // Percent escapes encode a byte as two hexadecimal nibbles. + const ( + hex = "0123456789ABCDEF" + hexDigitMask = 15 + hexRadix = 16 + hexDigitBits = 4 + ) out := make([]byte, 0, len(value)) for index := 0; index < len(value); index++ { @@ -99,11 +112,11 @@ func normalizeCRLPercent(value string) string { continue } - c := byte(a&15)*16 + byte(b&15) + c := byte(a&hexDigitMask)*hexRadix + byte(b&hexDigitMask) if c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || strings.ContainsRune("-._~", rune(c)) { out = append(out, c) } else { - out = append(out, '%', hex[c>>4], hex[c&15]) + out = append(out, '%', hex[c>>hexDigitBits], hex[c&hexDigitMask]) } index += 2 @@ -113,13 +126,15 @@ func normalizeCRLPercent(value string) string { } func crlHex(value byte) int { + const firstHexLetterValue = 10 // a/A starts immediately after decimal digits + switch { case value >= '0' && value <= '9': return int(value - '0') case value >= 'a' && value <= 'f': - return int(value - 'a' + 10) + return int(value - 'a' + firstHexLetterValue) case value >= 'A' && value <= 'F': - return int(value - 'A' + 10) + return int(value - 'A' + firstHexLetterValue) } return -1 diff --git a/pki/crl_parse.go b/pki/crl_parse.go index f997538..8d1c686 100644 --- a/pki/crl_parse.go +++ b/pki/crl_parse.go @@ -14,10 +14,30 @@ import ( "github.com/skarm/xmlsec/evidence" "github.com/skarm/xmlsec/internal/der" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/signature" "github.com/skarm/xmlsec/internal/x509crl" ) +const ( + maxCRLExtensions = 32 + maxCRLNumberBits = 160 + // Byte cap for small scalar revocation extensions before DER decoding. + maxScalarExtensionBytes = 64 + maximumCRLBytes = 64 << 20 + // Identifies the freshest-CRL extension used to associate delta CRL distribution points. + oidFreshestCRL = "2.5.29.46" +) + +const ( + oidAuthorityKeyIdentifier = "2.5.29.35" + oidCRLNumber = "2.5.29.20" + oidExpiredCertsOnCRL = "2.5.29.60" + oidInvalidityDate = "2.5.29.24" + oidIssuerAltName = "2.5.29.18" + oidReasonCode = "2.5.29.21" +) + type parsedCRL struct { work *crlWorkBudget authentication map[crlAuthenticationKey]*x509.Certificate @@ -62,13 +82,13 @@ func (l *parsedCRL) parseEntries(ctx context.Context, entries []x509.RevocationL l.entries = make([]crlEntry, 0, len(entries)) for index, parsed := range entries { - if index&255 == 0 { + if index&revocationCancellationMask == 0 { if err := ctx.Err(); err != nil { return err } } - if parsed.SerialNumber == nil || parsed.SerialNumber.Sign() < 0 || parsed.SerialNumber.BitLen() > 160 { + if parsed.SerialNumber == nil || parsed.SerialNumber.Sign() < 0 || parsed.SerialNumber.BitLen() > maxCertificateSerialBits { return ErrEvidence } @@ -83,7 +103,7 @@ func (l *parsedCRL) parseEntries(ctx context.Context, entries []x509.RevocationL return err } - if entry.reason == 8 && l.base == nil { + if entry.reason == reasonRemoveFromCRL && l.base == nil { return ErrEvidence } @@ -113,7 +133,7 @@ func (l *parsedCRL) parseExtensions(ctx context.Context, list []pkix.Extension) func (l *parsedCRL) parseExtension(ctx context.Context, ext pkix.Extension) error { switch ext.Id.String() { - case "2.5.29.20": + case oidCRLNumber: if ext.Critical { return ErrEvidence } @@ -124,7 +144,7 @@ func (l *parsedCRL) parseExtension(ctx context.Context, ext pkix.Extension) erro if err != nil { return err } - case "2.5.29.27": + case lexicon.X509OIDDeltaCRLIndicator: if !ext.Critical { return ErrEvidence } @@ -135,7 +155,7 @@ func (l *parsedCRL) parseExtension(ctx context.Context, ext pkix.Extension) erro if err != nil { return err } - case "2.5.29.28": + case lexicon.X509OIDIssuingDistributionPoint: if !ext.Critical { return ErrEvidence } @@ -146,7 +166,7 @@ func (l *parsedCRL) parseExtension(ctx context.Context, ext pkix.Extension) erro if err != nil { return err } - case "2.5.29.35": + case oidAuthorityKeyIdentifier: if ext.Critical { return ErrEvidence } @@ -159,20 +179,20 @@ func (l *parsedCRL) parseExtension(ctx context.Context, ext pkix.Extension) erro l.authorityKeyID = id.keyID l.authorityIssuers = id.issuers l.authoritySerial = id.serial - case "2.5.29.60": + case oidExpiredCertsOnCRL: retained, err := parseExpiredCRLRetention(ext, l.this) l.retainedFrom = retained return err - case "2.5.29.46": + case oidFreshestCRL: if ext.Critical { return ErrEvidence } return l.parseFreshest(ctx, ext.Value) - case "2.5.29.18": - value, err := der.Read(ext.Value, 1<<20) + case oidIssuerAltName: + value, err := der.Read(ext.Value, maxCRLExtensionBytes) if err != nil { return ErrEvidence } @@ -205,19 +225,19 @@ func (l *parsedCRL) parseEntryExtensions(ctx context.Context, entry *crlEntry, e func (l *parsedCRL) parseEntryExtension(ctx context.Context, entry *crlEntry, ext pkix.Extension) error { switch ext.Id.String() { - case "2.5.29.21": + case oidReasonCode: var reason asn1.Enumerated if ext.Critical || der.Unmarshal(ext.Value, &reason) != nil || !validReason(int(reason)) { return ErrEvidence } entry.reason = int(reason) - case "2.5.29.24": + case oidInvalidityDate: if ext.Critical { return ErrEvidence } - v, err := der.Read(ext.Value, 64) + v, err := der.Read(ext.Value, maxScalarExtensionBytes) if err != nil { return ErrEvidence } @@ -226,7 +246,7 @@ func (l *parsedCRL) parseEntryExtension(ctx context.Context, entry *crlEntry, ex if err != nil { return err } - case "2.5.29.29": + case lexicon.X509OIDCertificateIssuer: if !ext.Critical || !l.scope.indirect { return ErrEvidence } @@ -286,7 +306,7 @@ func parseCRL(ctx context.Context, raw []byte) (*parsedCRL, error) { return nil, err } - view, err := x509crl.ParseEvidence(raw, 64<<20) + view, err := x509crl.ParseEvidence(raw, maximumCRLBytes) if err != nil { if errors.Is(err, x509crl.ErrLimit) { return nil, fmt.Errorf("%w: %w: %w", ErrPolicy, ErrLimit, err) @@ -336,7 +356,7 @@ func parseCRL(ctx context.Context, raw []byte) (*parsedCRL, error) { func crlNumber(raw []byte) (*big.Int, error) { var number *big.Int - if der.Unmarshal(raw, &number) != nil || number == nil || number.Sign() < 0 || number.BitLen() > 160 { + if der.Unmarshal(raw, &number) != nil || number == nil || number.Sign() < 0 || number.BitLen() > maxCRLNumberBits { return nil, ErrEvidence } @@ -350,21 +370,24 @@ type crlAuthority struct { } func parseCRLAuthority(ctx context.Context, raw []byte) (crlAuthority, error) { + // AuthorityKeyIdentifier has optional keyIdentifier, issuer and serial fields. + const maximumAuthorityKeyIdentifierFields = 3 + out := crlAuthority{} - value, err := der.Read(raw, 1<<20) + value, err := der.Read(raw, maxCRLExtensionBytes) if err != nil { return out, ErrEvidence } - fields, err := der.Sequence(value, 1, 3) + fields, err := der.Sequence(value, 1, maximumAuthorityKeyIdentifierFields) if err != nil { return out, ErrEvidence } previous := -1 for _, field := range fields { - if field.Class != 2 || field.Tag <= previous { + if field.Class != asn1.ClassContextSpecific || field.Tag <= previous { return out, ErrEvidence } @@ -386,7 +409,7 @@ func parseCRLAuthority(ctx context.Context, raw []byte) (crlAuthority, error) { return out, ErrEvidence } - out.serial, err = crlNumber(der.Wrap(0, asn1.TagInteger, false, field.Bytes)) + out.serial, err = crlNumber(der.Wrap(asn1.ClassUniversal, asn1.TagInteger, false, field.Bytes)) if err != nil { return out, err } @@ -403,7 +426,7 @@ func parseCRLAuthority(ctx context.Context, raw []byte) (crlAuthority, error) { } func uniqueCRLExtensions(extensions []pkix.Extension) error { - if len(extensions) > 32 { + if len(extensions) > maxCRLExtensions { return fmt.Errorf("%w: %w: CRL extension count", ErrPolicy, ErrLimit) } @@ -423,7 +446,7 @@ func uniqueCRLExtensions(extensions []pkix.Extension) error { func reserveCRLEntryNames(seen map[string]struct{}, entry *crlEntry) error { names := make(map[string]struct{}, len(entry.issuers)) for _, name := range entry.issuers { - if name.tag != 4 { + if name.tag != lexicon.GeneralNameDirectory { continue } @@ -444,7 +467,7 @@ func reserveCRLEntryNames(seen map[string]struct{}, entry *crlEntry) error { } func parseCRLCertificateIssuer(ctx context.Context, raw []byte) ([]crlGeneralName, error) { - value, err := der.Read(raw, 1<<20) + value, err := der.Read(raw, maxCRLExtensionBytes) if err != nil { return nil, ErrEvidence } @@ -455,7 +478,7 @@ func parseCRLCertificateIssuer(ctx context.Context, raw []byte) ([]crlGeneralNam } for _, name := range names { - if name.tag == 4 { + if name.tag == lexicon.GeneralNameDirectory { return names, nil } } @@ -468,7 +491,7 @@ func parseExpiredCRLRetention(extension pkix.Extension, issued time.Time) (time. return time.Time{}, ErrEvidence } - value, err := der.Read(extension.Value, 64) + value, err := der.Read(extension.Value, maxScalarExtensionBytes) if err != nil { return time.Time{}, ErrEvidence } diff --git a/pki/crl_scope.go b/pki/crl_scope.go index 0449dad..b67fabc 100644 --- a/pki/crl_scope.go +++ b/pki/crl_scope.go @@ -7,15 +7,45 @@ import ( "encoding/asn1" "errors" "fmt" + "net" "strings" "github.com/skarm/xmlsec/internal/der" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/x509name" ) +const ( + // Bounds individual CRL scope/name extension encodings, in bytes. + maxCRLExtensionBytes = 1 << 20 + maxDistributionPointFields = 256 + maxDistributionPointNames = 256 + maxRDNAttributes = 256 +) + type crlReasonMask uint16 -const allCRLReasons crlReasonMask = 255 +// CRL ReasonFlags positions 1..8 map to bits 0..7 in the internal coverage mask. +// This mask is separate from the CRLReason ENUMERATED values in signed entries. +const ( + crlReasonFlagBitLength = 9 + allCRLReasons crlReasonMask = 1<<(crlReasonFlagBitLength-1) - 1 +) + +// DistributionPoint and IssuingDistributionPoint have distinct tagged schemas. +const ( + crlPointNameTag = 0 + crlPointReasonsTag = 1 + crlPointIssuerTag = 2 + crlPointMaximumFields = 3 + crlScopeNameTag = 0 + crlScopeOnlyUserTag = 1 + crlScopeOnlyCATag = 2 + crlScopeReasonsTag = 3 + crlScopeIndirectTag = 4 + crlScopeOnlyAttrTag = 5 + crlScopeMaximumFields = 6 +) type crlGeneralName struct { tag int @@ -40,7 +70,7 @@ type crlScope struct { func parseDistinguishedName(raw []byte) (pkix.RDNSequence, error) { var name pkix.RDNSequence - if len(raw) > 1<<20 || der.Unmarshal(raw, &name) != nil { + if len(raw) > maxCRLExtensionBytes || der.Unmarshal(raw, &name) != nil { return nil, ErrEvidence } @@ -67,15 +97,15 @@ func canonicalNameKey(ctx context.Context, raw []byte) (string, error) { func crlDirectoryName(ctx context.Context, raw []byte) (crlGeneralName, error) { key, err := canonicalNameKey(ctx, raw) - return crlGeneralName{tag: 4, value: raw, key: key}, err + return crlGeneralName{tag: lexicon.GeneralNameDirectory, value: raw, key: key}, err } func parseGeneralNames(ctx context.Context, value asn1.RawValue, implicit bool) ([]crlGeneralName, error) { - if !value.IsCompound || !implicit && !der.Is(value, 0, asn1.TagSequence, true) || implicit && value.Class != 2 { + if !value.IsCompound || !implicit && !der.Is(value, asn1.ClassUniversal, asn1.TagSequence, true) || implicit && value.Class != asn1.ClassContextSpecific { return nil, ErrEvidence } - fields, err := der.ChildrenLimit(value, 256) + fields, err := der.ChildrenLimit(value, maxDistributionPointFields) if errors.Is(err, der.ErrLimit) { return nil, fmt.Errorf("%w: %w: CRL GeneralNames count", ErrPolicy, ErrLimit) } @@ -107,7 +137,7 @@ func parseCRLReasonMask(value asn1.RawValue) (crlReasonMask, error) { } var bits asn1.BitString - if der.Unmarshal(der.Wrap(0, asn1.TagBitString, false, value.Bytes), &bits) != nil || bits.BitLength > 9 || bits.At(0) != 0 { + if der.Unmarshal(der.Wrap(asn1.ClassUniversal, asn1.TagBitString, false, value.Bytes), &bits) != nil || bits.BitLength > crlReasonFlagBitLength || bits.At(0) != 0 { return 0, ErrEvidence } @@ -124,7 +154,7 @@ func parseCRLReasonMask(value asn1.RawValue) (crlReasonMask, error) { func parseCRLPointName(ctx context.Context, value asn1.RawValue) ([]crlGeneralName, []byte, error) { name, err := der.Explicit(value, 0) - if err != nil || name.Class != 2 || !name.IsCompound { + if err != nil || name.Class != asn1.ClassContextSpecific || !name.IsCompound { return nil, nil, ErrEvidence } @@ -133,13 +163,13 @@ func parseCRLPointName(ctx context.Context, value asn1.RawValue) ([]crlGeneralNa names, err := parseGeneralNames(ctx, name, true) return names, nil, err case 1: - raw := der.Wrap(0, asn1.TagSet, true, name.Bytes) - if _, err := der.Read(raw, 1<<20); err != nil { + raw := der.Wrap(asn1.ClassUniversal, asn1.TagSet, true, name.Bytes) + if _, err := der.Read(raw, maxCRLExtensionBytes); err != nil { return nil, nil, ErrEvidence } var rdn pkix.RelativeDistinguishedNameSET - if der.Unmarshal(raw, &rdn) != nil || len(rdn) == 0 || len(rdn) > 256 { + if der.Unmarshal(raw, &rdn) != nil || len(rdn) == 0 || len(rdn) > maxRDNAttributes { return nil, nil, ErrEvidence } @@ -150,19 +180,19 @@ func parseCRLPointName(ctx context.Context, value asn1.RawValue) ([]crlGeneralNa } func parseCRLDistributionPoints(ctx context.Context, raw []byte) ([]crlDistributionPoint, error) { - value, err := der.Read(raw, 1<<20) + value, err := der.Read(raw, maxCRLExtensionBytes) if err != nil { return nil, ErrEvidence } - fields, err := der.Sequence(value, 1, 256) + fields, err := der.Sequence(value, 1, maxDistributionPointNames) if err != nil { return nil, ErrEvidence } out := make([]crlDistributionPoint, 0, len(fields)) for _, value := range fields { - fields, err := der.Sequence(value, 1, 3) + fields, err := der.Sequence(value, 1, crlPointMaximumFields) if err != nil { return nil, ErrEvidence } @@ -171,18 +201,18 @@ func parseCRLDistributionPoints(ctx context.Context, raw []byte) ([]crlDistribut previous := -1 for _, field := range fields { - if field.Class != 2 || field.Tag <= previous { + if field.Class != asn1.ClassContextSpecific || field.Tag <= previous { return nil, ErrEvidence } previous = field.Tag switch field.Tag { - case 0: + case crlPointNameTag: point.names, point.relative, err = parseCRLPointName(ctx, field) - case 1: + case crlPointReasonsTag: point.reasonsPresent = true point.reasons, err = parseCRLReasonMask(field) - case 2: + case crlPointIssuerTag: point.issuer, err = parseGeneralNames(ctx, field, true) default: return nil, ErrEvidence @@ -202,7 +232,7 @@ func parseCRLDistributionPoints(ctx context.Context, raw []byte) ([]crlDistribut } for _, name := range point.issuer { - if name.tag != 4 { + if name.tag != lexicon.GeneralNameDirectory { return nil, ErrEvidence } } @@ -214,43 +244,46 @@ func parseCRLDistributionPoints(ctx context.Context, raw []byte) ([]crlDistribut } func parseCRLScope(ctx context.Context, raw []byte) (crlScope, error) { + // DER TRUE is encoded as a single all-one octet. + const crlDERTrue = 0xff + scope := crlScope{reasons: allCRLReasons, raw: raw} - value, err := der.Read(raw, 1<<20) + value, err := der.Read(raw, maxCRLExtensionBytes) if err != nil { return scope, ErrEvidence } - fields, err := der.Sequence(value, 1, 6) + fields, err := der.Sequence(value, 1, crlScopeMaximumFields) if err != nil { return scope, ErrEvidence } previous := -1 for _, field := range fields { - if field.Class != 2 || field.Tag <= previous { + if field.Class != asn1.ClassContextSpecific || field.Tag <= previous { return scope, ErrEvidence } previous = field.Tag switch field.Tag { - case 0: + case crlScopeNameTag: scope.names, scope.relative, err = parseCRLPointName(ctx, field) - case 3: + case crlScopeReasonsTag: scope.reasons, err = parseCRLReasonMask(field) - case 1, 2, 4, 5: - if field.IsCompound || len(field.Bytes) != 1 || field.Bytes[0] != 255 { + case crlScopeOnlyUserTag, crlScopeOnlyCATag, crlScopeIndirectTag, crlScopeOnlyAttrTag: + if field.IsCompound || len(field.Bytes) != 1 || field.Bytes[0] != crlDERTrue { return scope, ErrEvidence } switch field.Tag { - case 1: + case crlScopeOnlyUserTag: scope.onlyUser = true - case 2: + case crlScopeOnlyCATag: scope.onlyCA = true - case 4: + case crlScopeIndirectTag: scope.indirect = true - case 5: + case crlScopeOnlyAttrTag: scope.onlyAttribute = true } default: @@ -283,7 +316,7 @@ func crlPointNames(ctx context.Context, point crlDistributionPoint, issuer []byt } if len(point.issuer) != 0 { - if len(point.issuer) != 1 || point.issuer[0].tag != 4 { + if len(point.issuer) != 1 || point.issuer[0].tag != lexicon.GeneralNameDirectory { return nil, ErrEvidence } @@ -318,13 +351,13 @@ func equalCRLGeneralName(a, b crlGeneralName) bool { } switch a.tag { - case 4: + case lexicon.GeneralNameDirectory: return a.key != "" && a.key == b.key - case 1: + case lexicon.GeneralNameRFC822: return equalCRLMailbox(string(a.value), string(b.value)) - case 6: + case lexicon.GeneralNameURI: return crlURIKey(string(a.value)) == crlURIKey(string(b.value)) - case 2: + case lexicon.GeneralNameDNS: return strings.EqualFold(string(a.value), string(b.value)) } @@ -344,7 +377,7 @@ func intersectCRLNames(a, b []crlGeneralName) bool { } func parseCRLGeneralName(ctx context.Context, field asn1.RawValue) (crlGeneralName, error) { - if field.Class != 2 || field.Tag < 0 || field.Tag > 8 { + if field.Class != asn1.ClassContextSpecific || field.Tag < lexicon.GeneralNameOtherName || field.Tag > lexicon.GeneralNameRegisteredID { return crlGeneralName{}, ErrEvidence } @@ -353,7 +386,7 @@ func parseCRLGeneralName(ctx context.Context, field asn1.RawValue) (crlGeneralNa var err error switch field.Tag { - case 4: + case lexicon.GeneralNameDirectory: if !field.IsCompound { return crlGeneralName{}, ErrEvidence } @@ -362,25 +395,25 @@ func parseCRLGeneralName(ctx context.Context, field asn1.RawValue) (crlGeneralNa if err != nil { return crlGeneralName{}, err } - case 1, 2, 6: + case lexicon.GeneralNameRFC822, lexicon.GeneralNameDNS, lexicon.GeneralNameURI: if err := validateCRLIA5Name(field); err != nil { return crlGeneralName{}, err } - case 7: - if field.IsCompound || len(field.Bytes) != 4 && len(field.Bytes) != 16 { + case lexicon.GeneralNameIP: + if field.IsCompound || len(field.Bytes) != net.IPv4len && len(field.Bytes) != net.IPv6len { return crlGeneralName{}, ErrEvidence } - case 8: + case lexicon.GeneralNameRegisteredID: if field.IsCompound { return crlGeneralName{}, ErrEvidence } var oid asn1.ObjectIdentifier - if der.Unmarshal(der.Wrap(0, asn1.TagOID, false, field.Bytes), &oid) != nil { + if der.Unmarshal(der.Wrap(asn1.ClassUniversal, asn1.TagOID, false, field.Bytes), &oid) != nil { return crlGeneralName{}, ErrEvidence } - case 0: + case lexicon.GeneralNameOtherName: if !field.IsCompound { return crlGeneralName{}, ErrEvidence } @@ -408,17 +441,23 @@ func parseCRLGeneralName(ctx context.Context, field asn1.RawValue) (crlGeneralNa } func validateCRLIA5Name(field asn1.RawValue) error { + // GeneralName IA5 spellings here exclude space/control bytes and DEL. + const ( + ia5SpaceByte = 32 + ia5DeleteByte = 127 + ) + if field.IsCompound || len(field.Bytes) == 0 { return ErrEvidence } for _, b := range field.Bytes { - if b <= 32 || b >= 127 { + if b <= ia5SpaceByte || b >= ia5DeleteByte { return ErrEvidence } } - if field.Tag == 6 { + if field.Tag == lexicon.GeneralNameURI { if !validCRLURI(string(field.Bytes)) { return ErrEvidence } diff --git a/pki/crl_verify.go b/pki/crl_verify.go index 4cbd388..ad54498 100644 --- a/pki/crl_verify.go +++ b/pki/crl_verify.go @@ -11,12 +11,38 @@ import ( "time" "github.com/skarm/xmlsec/internal/der" + "github.com/skarm/xmlsec/internal/lexicon" +) + +// x509CertificateVersion3 is represented by 3 in crypto/x509's parsed view, +// unlike its zero-based ASN.1 INTEGER encoding. +const x509CertificateVersion3 = 3 + +const ( + // Authenticated CRL status label for an entry that removes an earlier revocation. + assertedRemoveFromCRL = "remove-from-crl" + // Authenticated evidence status label used when evaluating revocation history. + assertedRevoked = "revoked" + completeCRLScopePrefix = "complete/" + deltaCRLScopePrefix = "delta/" + defaultCRLMaxAge = 24 * time.Hour + defaultCRLSignerDepth = 10 + // Caps name-constraint work passed to certificate path verification. + maxNameConstraintComparisons = 100000 + maximumCRLMaxAge = 30 * 24 * time.Hour + // X.509 reason-code value that removes an entry from a delta CRL. + reasonRemoveFromCRL = 8 +) + +const ( + oidCRLDistributionPoints = "2.5.29.31" ) // CRLOptions controls the offline CRL algorithm. Zero enables supplied deltas. // A FreshestCRL advertisement then requires a current applicable supplied delta; // no resource is fetched. NextUpdate is always an exclusive upper boundary. type CRLOptions struct { + // signerDepth bounds recursive CRL signer path processing after normalization. signerDepth int // DisableDeltaCRLs selects RFC5280's use-deltas=false input explicitly. DisableDeltaCRLs bool @@ -26,14 +52,14 @@ type CRLOptions struct { func normalizeCRLOptions(o CRLOptions) (CRLOptions, error) { if o.signerDepth == 0 { - o.signerDepth = 10 + o.signerDepth = defaultCRLSignerDepth } if o.MaxAge == 0 { - o.MaxAge = 24 * time.Hour + o.MaxAge = defaultCRLMaxAge } - if o.MaxAge <= 0 || o.MaxAge > 30*24*time.Hour { + if o.MaxAge <= 0 || o.MaxAge > maximumCRLMaxAge { return o, ErrPolicy } @@ -43,8 +69,8 @@ func normalizeCRLOptions(o CRLOptions) (CRLOptions, error) { type ( crlWorkBudget struct{ checks int } crlAuthenticationKey struct { - anchor [32]byte - leaf [32]byte + anchor [sha256.Size]byte + leaf [sha256.Size]byte } ) @@ -219,7 +245,7 @@ func (a *crlAuthentication) authorizedSigner(ctx context.Context, list *parsedCR } list.work.checks++ - if list.work.checks > 4096 { + if list.work.checks > maxSignatureVerifications { return false, fmt.Errorf("%w: %w: CRL signature work limit", ErrPolicy, ErrLimit) } @@ -238,7 +264,7 @@ func (a *crlAuthentication) authorizedSigner(ctx context.Context, list *parsedCR Intermediates: a.intermediates, CurrentTime: a.at, KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageAny}, - MaxConstraintComparisions: 100000, + MaxConstraintComparisions: maxNameConstraintComparisons, }) return err == nil && validCRLSignerPaths(paths, a.anchor, a.maximumDepth), ctx.Err() @@ -247,7 +273,7 @@ func (a *crlAuthentication) authorizedSigner(ctx context.Context, list *parsedCR func crlCandidates(chain []*x509.Certificate, lists []*parsedCRL) ([]*x509.Certificate, error) { candidates := append([]*x509.Certificate(nil), chain...) - seen := make(map[[32]byte]struct{}) + seen := make(map[[sha256.Size]byte]struct{}) for _, cert := range candidates { seen[sha256.Sum256(cert.Raw)] = struct{}{} } @@ -285,7 +311,7 @@ func (l *parsedCRL) signerMatches(ctx context.Context, signer *x509.Certificate) return false, nil } - if signer.Version == 3 && signer.KeyUsage&x509.KeyUsageCRLSign == 0 || signer.KeyUsage != 0 && signer.KeyUsage&x509.KeyUsageCRLSign == 0 { + if signer.Version == x509CertificateVersion3 && signer.KeyUsage&x509.KeyUsageCRLSign == 0 || signer.KeyUsage != 0 && signer.KeyUsage&x509.KeyUsageCRLSign == 0 { return false, nil } @@ -341,9 +367,9 @@ func checkCRLSequence(group []*authenticatedCRL) error { continue } - kind := "complete/" + kind := completeCRLScopePrefix if list.base != nil { - kind = "delta/" + kind = deltaCRLScopePrefix } key := kind + list.number.String() @@ -379,7 +405,7 @@ func certificateCRLPoints(ctx context.Context, cert *x509.Certificate) ([]crlDis freshest := false for _, ext := range cert.Extensions { - if ext.Id.String() == "2.5.29.31" { + if ext.Id.String() == oidCRLDistributionPoints { var err error points, err = parseCRLDistributionPoints(ctx, ext.Value) @@ -388,7 +414,7 @@ func certificateCRLPoints(ctx context.Context, cert *x509.Certificate) ([]crlDis } } - if ext.Id.String() == "2.5.29.46" { + if ext.Id.String() == oidFreshestCRL { if ext.Critical { return nil, false, ErrEvidence } @@ -413,12 +439,12 @@ func certificateCRLPoints(ctx context.Context, cert *x509.Certificate) ([]crlDis func applicableCRLScope(ctx context.Context, cert *x509.Certificate, list *parsedCRL, point crlDistributionPoint) (crlReasonMask, error) { scope := list.scope - if scope.onlyAttribute || scope.onlyUser && cert.IsCA || scope.onlyCA && !cert.IsCA || (scope.onlyUser || scope.onlyCA) && cert.Version < 3 { + if scope.onlyAttribute || scope.onlyUser && cert.IsCA || scope.onlyCA && !cert.IsCA || (scope.onlyUser || scope.onlyCA) && cert.Version < x509CertificateVersion3 { return 0, nil } if len(point.issuer) > 0 { - if !scope.indirect || !intersectCRLNames(point.issuer, []crlGeneralName{{tag: 4, value: list.issuer, key: list.issuerKey}}) { + if !scope.indirect || !intersectCRLNames(point.issuer, []crlGeneralName{{tag: lexicon.GeneralNameDirectory, value: list.issuer, key: list.issuerKey}}) { return 0, nil } } else if certIssuer, err := canonicalNameKey(ctx, cert.RawIssuer); err != nil { @@ -457,7 +483,7 @@ func findCRLEntry(ctx context.Context, list *parsedCRL, cert *x509.Certificate) } for index := range list.entries { - if index&255 == 0 { + if index&revocationCancellationMask == 0 { if err := ctx.Err(); err != nil { return nil, err } @@ -480,7 +506,7 @@ func viewCRLEntry(ctx context.Context, view completeCRLView, cert *x509.Certific } if entry != nil { - if entry.reason == 8 { + if entry.reason == reasonRemoveFromCRL { return nil, nil } @@ -510,13 +536,13 @@ func recordCRLAssertion(ctx context.Context, auth *authenticatedCRL, cert, issue } if entry != nil { - item.status = "revoked" + item.status = assertedRevoked item.revoked = entry.revoked item.invalidity = entry.invalidity item.reason = entry.reason - if entry.reason == 8 { - item.status = "remove-from-crl" + if entry.reason == reasonRemoveFromCRL { + item.status = assertedRemoveFromCRL } } @@ -688,7 +714,7 @@ func certificateIssuerNames(ctx context.Context, cert *x509.Certificate) ([]crlG continue } - value, err := der.Read(extension.Value, 1<<20) + value, err := der.Read(extension.Value, maxCRLExtensionBytes) if err != nil { return nil, ErrEvidence } diff --git a/pki/evidence.go b/pki/evidence.go index 33cb259..9a18fb7 100644 --- a/pki/evidence.go +++ b/pki/evidence.go @@ -11,6 +11,10 @@ import ( "github.com/skarm/xmlsec/evidence" ) +const ( + assertedNotListed = "not-listed" +) + // EvidenceKind identifies the encoding of an authenticated offline assertion. type EvidenceKind string @@ -28,30 +32,62 @@ const ( // Result includes an assertion only after its required freshness/coverage policy // passes. Report can also expose authenticated but stale or inapplicable evidence. type VerifiedEvidence struct { - revoked time.Time - invalidity time.Time - issued time.Time - this time.Time - next time.Time - validation time.Time - signerAuthority string - signatureOID string - kind EvidenceKind - status string - scope string - signerChain [][]byte - sources []*evidence.Observation - raw []byte - dependencies []string - reason int + // revoked is the authenticated revocation-time claim, if present. + revoked time.Time + // invalidity is the issuer's signed invalidity-date claim, not independent + // proof of compromise time. + invalidity time.Time + // issued is producedAt for OCSP or thisUpdate for a CRL. + issued time.Time + // this is the authenticated thisUpdate claim. + this time.Time + // next is the original nextUpdate claim; zero records its absence. + next time.Time + // validation is the explicit time used to authenticate the evidence signer and + // path. + validation time.Time + // signerAuthority records the issuer, delegated or local signer authority used + // during authentication. + signerAuthority string + // signatureOID preserves the declared signature algorithm OID. + signatureOID string + // kind identifies CRL or OCSP evidence. + kind EvidenceKind + // status is the authenticated status assertion, independently of freshness or + // acceptance. + status string + // scope binds the assertion to its exact issuer and evidence scope. + scope string + // signerChain retains the evidence signer's certificate path. + signerChain [][]byte + // sources retains acquisition observations without granting authority or proof + // of existence. + sources []*evidence.Observation + // raw owns authenticated source bytes when observations do not retain them. + raw []byte + // dependencies identifies original evidence sources used in a composite + // assertion. + dependencies []string + // reason retains the signed revocation or removal reason. + reason int + // historicalCoverage retains reason coverage used by historical validation. historicalCoverage crlReasonMask - scopeReasons crlReasonMask - coverage crlReasonMask - fingerprint [32]byte - target [32]byte - issuer [32]byte - signer [32]byte - authenticated bool + // scopeReasons is the reason mask declared by the signed CRL scope. + scopeReasons crlReasonMask + // coverage is the reason mask checked for this exact target and validation + // time. + coverage crlReasonMask + // fingerprint identifies the exact source encoding by SHA-256. + fingerprint [sha256.Size]byte + // target identifies the exact target certificate DER by SHA-256. + target [sha256.Size]byte + // issuer identifies the issuer's SubjectPublicKeyInfo by SHA-256. + issuer [sha256.Size]byte + // signer identifies the evidence signer's exact certificate DER by SHA-256. + signer [sha256.Size]byte + // authenticated records signature and authority authentication without + // asserting freshness. + authenticated bool } func newCRLEvidence(list *parsedCRL, target, issuer, signer *x509.Certificate, at time.Time) *VerifiedEvidence { @@ -62,7 +98,7 @@ func newCRLEvidence(list *parsedCRL, target, issuer, signer *x509.Certificate, a scopeData := append([]byte(list.issuerKey), list.scope.raw...) scope := sha256.Sum256(scopeData) - return &VerifiedEvidence{authenticated: true, signatureOID: list.algorithm.Algorithm.String(), scopeReasons: list.scope.reasons, sources: append([]*evidence.Observation(nil), list.sources...), kind: EvidenceCRL, raw: list.ownedRaw, fingerprint: sha256.Sum256(list.raw), target: sha256.Sum256(target.Raw), issuer: sha256.Sum256(issuer.RawSubjectPublicKeyInfo), signer: sha256.Sum256(signer.Raw), issued: list.this, this: list.this, next: list.next, validation: at, status: "not-listed", scope: hex.EncodeToString(scope[:])} + return &VerifiedEvidence{authenticated: true, signatureOID: list.algorithm.Algorithm.String(), scopeReasons: list.scope.reasons, sources: append([]*evidence.Observation(nil), list.sources...), kind: EvidenceCRL, raw: list.ownedRaw, fingerprint: sha256.Sum256(list.raw), target: sha256.Sum256(target.Raw), issuer: sha256.Sum256(issuer.RawSubjectPublicKeyInfo), signer: sha256.Sum256(signer.Raw), issued: list.this, this: list.this, next: list.next, validation: at, status: assertedNotListed, scope: hex.EncodeToString(scope[:])} } // Kind returns the original evidence encoding. @@ -226,7 +262,7 @@ func (e *VerifiedEvidence) InvalidityDate() time.Time { // RevocationReason returns the signed status reason when revocation/removal is asserted. func (e *VerifiedEvidence) RevocationReason() (int, bool) { - if e == nil || e.status != "revoked" && e.status != "remove-from-crl" { + if e == nil || e.status != assertedRevoked && e.status != assertedRemoveFromCRL { return 0, false } @@ -289,6 +325,8 @@ func canonicalEvidence(items []*VerifiedEvidence) []*VerifiedEvidence { continue } + // Merge coverage only within the same target, authority, scope and + // validation time; identical source bytes alone do not bind those claims. key := item.FingerprintSHA256() + "/" + item.TargetCertificateSHA256() + "/" + item.IssuerScopeBinding() + "/" + item.SignerCertificateSHA256() + "/" + item.IssuerSPKISHA256() + "/" + item.validation.UTC().Format(time.RFC3339Nano) if previous := byBinding[key]; previous == nil { keys = append(keys, key) diff --git a/pki/evidence_sources.go b/pki/evidence_sources.go index 698a78d..ef3fcb5 100644 --- a/pki/evidence_sources.go +++ b/pki/evidence_sources.go @@ -8,13 +8,18 @@ import ( "github.com/skarm/xmlsec/evidence" ) +const ( + // Caps acquisition-metadata observations supplied with one evidence set. + maxSourceObservations = 1024 +) + type evidenceBinding struct { kind evidence.Kind - hash [32]byte + hash [sha256.Size]byte } func (v *Verifier) validatedSources(ctx context.Context, leaf []byte, input Evidence) (map[evidenceBinding][]*evidence.Observation, error) { - if len(input.Sources) > 1024 { + if len(input.Sources) > maxSourceObservations { return nil, ErrPolicy } diff --git a/pki/historical.go b/pki/historical.go index 7ffc16d..d0d3bc8 100644 --- a/pki/historical.go +++ b/pki/historical.go @@ -17,6 +17,14 @@ import ( "github.com/skarm/xmlsec/internal/historicalproof" ) +const ( + defaultMaxHistoricalPaths = 16 + maxConstraintKeyBits = 16385 + maxCryptoConstraints = 128 + maxTrustAnchorSunsets = 64 + maximumHistoricalPaths = 256 +) + // ValidityModel selects the historical certificate calendar model. type ValidityModel string @@ -78,20 +86,36 @@ type HistoricalConfig struct { // return immutable objects. Source authentication, freshness, proof of existence // and past acceptance are separate checks. type HistoricalSession struct { - mu sync.Mutex - verifier *Verifier - options HistoricalOptions - now time.Time - evidence Evidence - paths map[[32]byte][]*HistoricalPath - certificates map[[32]byte]*x509.Certificate - targetBytes int - pathWork int + // mu serializes operation-local caches and cumulative work counters. + mu sync.Mutex + // verifier retains the explicit certificate and revocation policy. + verifier *Verifier + // options retains the normalized historical validation policy. + options HistoricalOptions + // now fixes the operation's current observation time. + now time.Time + // evidence owns the supplied offline evidence snapshot. + evidence Evidence + // paths caches prospective paths by exact target certificate digest. + paths map[[sha256.Size]byte][]*HistoricalPath + // certificates indexes parsed certificates by exact DER digest. + certificates map[[sha256.Size]byte]*x509.Certificate + // targetBytes accounts for target certificate input charged to the session. + targetBytes int + // pathWork accounts for cumulative prospective-path search work. + pathWork int + // signatureWork accounts for cumulative cryptographic verification work. signatureWork int - sourcesReady bool - lists []*parsedCRL - responses []parsedOCSP - claims map[[32]byte][]historicalClaim + // sourcesReady records completion of source evidence preparation. + sourcesReady bool + // lists caches parsed CRLs without treating parsing as authentication. + lists []*parsedCRL + // responses caches parsed OCSP responses without treating parsing as + // authentication. + responses []parsedOCSP + // claims caches authenticated evidence claims by exact prospective path + // fingerprint. + claims map[[sha256.Size]byte][]historicalClaim } // NewHistoricalSession snapshots an explicit trust configuration and operation. @@ -217,9 +241,13 @@ func (s *HistoricalSession) validPath(path *HistoricalPath) error { // HistoricalPath is a candidate certificate path bound to its producing session. // Only that session can assess or use it. Constructing a path grants no trust. type HistoricalPath struct { - session *HistoricalSession - chain []*x509.Certificate - fingerprint [32]byte + // session binds this path to the policy and evidence operation that discovered + // it. + session *HistoricalSession + // chain retains the exact prospective certificate path. + chain []*x509.Certificate + // fingerprint identifies the complete path by SHA-256. + fingerprint [sha256.Size]byte } // ChainDER returns owned copies in target-to-anchor order. @@ -240,9 +268,12 @@ func (p *HistoricalPath) ChainDER() [][]byte { // path. It establishes neither historical proof of existence nor document signature // validity. type HistoricalPathAssessment struct { - path *HistoricalPath + // path is the prospective path checked by this assessment. + path *HistoricalPath + // reference is the calendar model's checking time, not a proven signing time. reference time.Time - model ValidityModel + // model records the selected certificate validity model. + model ValidityModel } // Model returns the selected calendar model. @@ -293,8 +324,11 @@ type TimeTransition struct { // PastCertificateResult records successful past certificate validation under the // selected policy. Document signature and past-signature validation remain separate. type PastCertificateResult struct { - path *HistoricalPath - at time.Time + // path is the exact path accepted by past certificate validation. + path *HistoricalPath + // at is the calculated historical control time. + at time.Time + // evidence retains the authenticated source assertions used in validation. evidence []*VerifiedEvidence } @@ -328,11 +362,17 @@ func (r *PastCertificateResult) Evidence() []*VerifiedEvidence { // PastCertificateReport contains past certificate diagnostics. Acceptance requires // a non-nil result in addition to the report. type PastCertificateReport struct { + // subIndication is the detailed diagnostic code, or empty on success. subIndication string - transitions []TimeTransition - cause error - path *HistoricalPath - evidence []*VerifiedEvidence + // transitions records the historical control-time changes. + transitions []TimeTransition + // cause retains the failure responsible for the diagnostic outcome. + cause error + // path is the prospective path considered by validation. + path *HistoricalPath + // evidence retains authenticated source diagnostics independently of + // acceptance. + evidence []*VerifiedEvidence } // SubIndication returns the precise diagnostic code, or empty on success. @@ -395,7 +435,7 @@ func (v *Verifier) NewHistoricalSession(ctx context.Context, options HistoricalO return nil, errors.Join(ErrLimit, err) } - if len(input.Sources) > 1024 { + if len(input.Sources) > maxSourceObservations { return nil, fmt.Errorf("%w: %w: historical source count", ErrPolicy, ErrLimit) } @@ -409,7 +449,7 @@ func (v *Verifier) NewHistoricalSession(ctx context.Context, options HistoricalO return nil, err } - return &HistoricalSession{verifier: v, options: options, now: now.UTC(), evidence: owned, paths: make(map[[32]byte][]*HistoricalPath), claims: make(map[[32]byte][]historicalClaim)}, nil + return &HistoricalSession{verifier: v, options: options, now: now.UTC(), evidence: owned, paths: make(map[[sha256.Size]byte][]*HistoricalPath), claims: make(map[[sha256.Size]byte][]historicalClaim)}, nil } func (v *Verifier) historicalOptions(options HistoricalOptions) (HistoricalOptions, error) { @@ -422,10 +462,10 @@ func (v *Verifier) historicalOptions(options HistoricalOptions) (HistoricalOptio } if options.MaxPaths == 0 { - options.MaxPaths = 16 + options.MaxPaths = defaultMaxHistoricalPaths } - if options.MaxPaths < 1 || options.MaxPaths > 256 || len(options.CryptoConstraints) > 128 || len(options.TrustAnchorSunsets) > 64 { + if options.MaxPaths < 1 || options.MaxPaths > maximumHistoricalPaths || len(options.CryptoConstraints) > maxCryptoConstraints || len(options.TrustAnchorSunsets) > maxTrustAnchorSunsets { return options, ErrPolicy } @@ -491,7 +531,7 @@ func validHistoricalCryptoRule(rule CryptoConstraint) bool { return false } - if rule.MinimumKeyBits < 1 || rule.MinimumKeyBits > 16385 { + if rule.MinimumKeyBits < 1 || rule.MinimumKeyBits > maxConstraintKeyBits { return false } diff --git a/pki/historical_current.go b/pki/historical_current.go index a2da218..cadda81 100644 --- a/pki/historical_current.go +++ b/pki/historical_current.go @@ -7,6 +7,14 @@ import ( "crypto/x509" "encoding/hex" "fmt" + + "github.com/skarm/xmlsec/internal/lexicon" +) + +const ( + codeCryptoConstraintsFailureNoPoe = "CRYPTO_CONSTRAINTS_FAILURE_NO_POE" + codeRevokedCaNoPoe = "REVOKED_CA_NO_POE" + codeRevokedNoPoe = "REVOKED_NO_POE" ) const ( @@ -54,7 +62,7 @@ func (s *HistoricalSession) CheckCurrentPolicy(ctx context.Context, result *Resu } func (s *HistoricalSession) checkCurrentPathPolicy(ctx context.Context, path *HistoricalPath) (string, error) { - evaluator := currentPolicyEvaluation{session: s, active: make(map[[32]byte]struct{})} + evaluator := currentPolicyEvaluation{session: s, active: make(map[[sha256.Size]byte]struct{})} return evaluator.check(ctx, path, false, false) } @@ -74,7 +82,7 @@ func historicalResultPath(result *Result, path *HistoricalPath) bool { type currentPolicyEvaluation struct { session *HistoricalSession - active map[[32]byte]struct{} + active map[[sha256.Size]byte]struct{} } func (e *currentPolicyEvaluation) check(ctx context.Context, path *HistoricalPath, source, skipLeaf bool) (string, error) { @@ -128,7 +136,7 @@ func (e *currentPolicyEvaluation) checkCertificate(ctx context.Context, path *Hi } if cutoff := evaluator.materialCutoff(certificate.SignatureAlgorithm, path.chain[index+1]); !cutoff.IsZero() && !s.now.Before(cutoff) { - return "CRYPTO_CONSTRAINTS_FAILURE_NO_POE", ErrPolicy + return codeCryptoConstraintsFailureNoPoe, ErrPolicy } if skipLeaf && index == 0 { @@ -142,18 +150,18 @@ func (e *currentPolicyEvaluation) checkCertificate(ctx context.Context, path *Hi if selected.revoked { if index == 0 && !source { - if selected.reason == 6 { + if selected.reason == lexicon.RevocationReasonCertificateHold { return historicalTryLater, ErrRevoked } - return "REVOKED_NO_POE", ErrRevoked + return codeRevokedNoPoe, ErrRevoked } - return "REVOKED_CA_NO_POE", ErrRevoked + return codeRevokedCaNoPoe, ErrRevoked } if evaluator.cryptoBoundary(certificate, selected, s.now) { - return "CRYPTO_CONSTRAINTS_FAILURE_NO_POE", ErrPolicy + return codeCryptoConstraintsFailureNoPoe, ErrPolicy } return e.checkSources(ctx, path, index, selected) diff --git a/pki/historical_evidence.go b/pki/historical_evidence.go index 11ff80e..7856889 100644 --- a/pki/historical_evidence.go +++ b/pki/historical_evidence.go @@ -14,10 +14,20 @@ import ( "time" "github.com/skarm/xmlsec/evidence" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/signature" "github.com/skarm/xmlsec/internal/x509name" ) +const ( + evidenceAuthorityDelegated = "delegated" + evidenceAuthorityIssuer = "issuer" + evidenceAuthorityLocal = "local" + // Caps repeated certificate/evidence signature checks within a bounded verification workflow. + maxSignatureVerifications = 4096 + maximumHistoricalEvidenceDERBytes = 64 << 20 +) + type historicalClaim struct { fact *VerifiedEvidence target *x509.Certificate @@ -31,6 +41,66 @@ type historicalClaim struct { dependency bool } +// SignerChainDER returns owned original certificates used to authenticate an +// historical assertion. A locally pinned authority can have only its own scoped +// certificate here; SignerAuthority distinguishes that explicit trust model. +func (e *VerifiedEvidence) SignerChainDER() [][]byte { + if e == nil || !e.authenticated { + return nil + } + + raw := make([][]byte, len(e.signerChain)) + for index, source := range e.signerChain { + raw[index] = bytes.Clone(source) + } + + return raw +} + +// SignerAuthority returns issuer, delegated or locally configured historical +// authorization; it is not a proof-of-existence or freshness indication. +func (e *VerifiedEvidence) SignerAuthority() string { + if e == nil || !e.authenticated { + return "" + } + + return e.signerAuthority +} + +// InspectOCSPCertificates returns owned copies of certificates actually embedded +// in one bounded OCSP response. They are untrusted input, not authenticated +// signer paths or proof of existence. It shares the response syntax decoder and +// its eight-certificate/64-status limits; the input cannot exceed64MiB. +func InspectOCSPCertificates(ctx context.Context, raw []byte) ([][]byte, error) { + if ctx == nil { + return nil, ErrPolicy + } + + if err := ctx.Err(); err != nil { + return nil, err + } + + if len(raw) > maximumHistoricalEvidenceDERBytes { + return nil, fmt.Errorf("%w: %w: OCSP inspection bytes", ErrPolicy, ErrLimit) + } + + parsed, err := parseOCSP(raw, maximumHistoricalEvidenceDERBytes) + if err != nil { + return nil, err + } + + certificates := make([][]byte, len(parsed.certs)) + for index, certificate := range parsed.certs { + if err := ctx.Err(); err != nil { + return nil, err + } + + certificates[index] = bytes.Clone(certificate.Raw) + } + + return certificates, ctx.Err() +} + func (c historicalClaim) retainsStatus(expired time.Time) bool { if c.crl != nil { return !c.crl.retainedFrom.IsZero() && !expired.Before(c.crl.retainedFrom) @@ -197,7 +267,7 @@ func (s *HistoricalSession) authenticateCRL(ctx context.Context, path *Historica } s.signatureWork++ - if s.signatureWork > 4096 { + if s.signatureWork > maxSignatureVerifications { return nil, fmt.Errorf("%w: %w: historical signature work", ErrPolicy, ErrLimit) } @@ -223,9 +293,9 @@ func (s *HistoricalSession) authenticateCRL(ctx context.Context, path *Historica historical.historicalCoverage = coverage historical.signerChain = historicalChainBytes(signerPath) - historical.signerAuthority = "issuer" + historical.signerAuthority = evidenceAuthorityIssuer if !bytes.Equal(candidate.Raw, issuer.Raw) { - historical.signerAuthority = "delegated" + historical.signerAuthority = evidenceAuthorityDelegated } return &historicalClaim{fact: &historical, target: target, issuer: issuer, signer: candidate, signerPath: signerPath, crl: list, needsSignerStatus: !bytes.Equal(candidate.Raw, issuer.Raw)}, nil @@ -313,7 +383,7 @@ func (s *HistoricalSession) authenticateOCSPCandidate(ctx context.Context, path } s.signatureWork++ - if s.signatureWork > 4096 { + if s.signatureWork > maxSignatureVerifications { return nil, fmt.Errorf("%w: %w: historical signature work", ErrPolicy, ErrLimit) } @@ -350,12 +420,12 @@ func (s *HistoricalSession) authenticateOCSPCandidate(ctx context.Context, path fact.historicalCoverage = allCRLReasons fact.signerChain = historicalChainBytes(signerPath) - fact.signerAuthority = "issuer" + fact.signerAuthority = evidenceAuthorityIssuer if local { - fact.signerAuthority = "local" + fact.signerAuthority = evidenceAuthorityLocal fact.signerChain = [][]byte{candidate.Raw} } else if !bytes.Equal(candidate.Raw, issuer.Raw) { - fact.signerAuthority = "delegated" + fact.signerAuthority = evidenceAuthorityDelegated } return &historicalClaim{fact: fact, target: target, issuer: issuer, signer: candidate, signerPath: signerPath, ocsp: entry, algorithm: historicalAlgorithm(parsed.alg), needsSignerStatus: !local && !noCheck && !bytes.Equal(candidate.Raw, issuer.Raw)}, nil @@ -399,7 +469,7 @@ func historicalResponderAuthority(ctx context.Context, responder, issuer *x509.C continue } - if extension.Critical || !bytes.Equal(extension.Value, []byte{5, 0}) { + if extension.Critical || !bytes.Equal(extension.Value, []byte{asn1.TagNull, 0}) { return false, false, nil } @@ -422,66 +492,6 @@ func historicalChainBytes(path *HistoricalPath) [][]byte { return raw } -// SignerChainDER returns owned original certificates used to authenticate an -// historical assertion. A locally pinned authority can have only its own scoped -// certificate here; SignerAuthority distinguishes that explicit trust model. -func (e *VerifiedEvidence) SignerChainDER() [][]byte { - if e == nil || !e.authenticated { - return nil - } - - raw := make([][]byte, len(e.signerChain)) - for index, source := range e.signerChain { - raw[index] = bytes.Clone(source) - } - - return raw -} - -// SignerAuthority returns issuer, delegated or locally configured historical -// authorization; it is not a proof-of-existence or freshness indication. -func (e *VerifiedEvidence) SignerAuthority() string { - if e == nil || !e.authenticated { - return "" - } - - return e.signerAuthority -} - -// InspectOCSPCertificates returns owned copies of certificates actually embedded -// in one bounded OCSP response. They are untrusted input, not authenticated -// signer paths or proof of existence. It shares the response syntax decoder and -// its eight-certificate/64-status limits; the input cannot exceed64MiB. -func InspectOCSPCertificates(ctx context.Context, raw []byte) ([][]byte, error) { - if ctx == nil { - return nil, ErrPolicy - } - - if err := ctx.Err(); err != nil { - return nil, err - } - - if len(raw) > 64<<20 { - return nil, fmt.Errorf("%w: %w: OCSP inspection bytes", ErrPolicy, ErrLimit) - } - - parsed, err := parseOCSP(raw, 64<<20) - if err != nil { - return nil, err - } - - certificates := make([][]byte, len(parsed.certs)) - for index, certificate := range parsed.certs { - if err := ctx.Err(); err != nil { - return nil, err - } - - certificates[index] = bytes.Clone(certificate.Raw) - } - - return certificates, ctx.Err() -} - func historicalAlgorithm(algorithm pkix.AlgorithmIdentifier) x509.SignatureAlgorithm { hash, mode, err := signature.Details(algorithm) if err != nil { @@ -493,7 +503,7 @@ func historicalAlgorithm(algorithm pkix.AlgorithmIdentifier) x509.SignatureAlgor } switch mode { - case "rsa": + case lexicon.SignatureFamilyRSA: switch hash { case crypto.SHA256: return x509.SHA256WithRSA @@ -502,7 +512,7 @@ func historicalAlgorithm(algorithm pkix.AlgorithmIdentifier) x509.SignatureAlgor case crypto.SHA512: return x509.SHA512WithRSA } - case "pss": + case lexicon.SignatureFamilyRSAPSS: switch hash { case crypto.SHA256: return x509.SHA256WithRSAPSS @@ -511,7 +521,7 @@ func historicalAlgorithm(algorithm pkix.AlgorithmIdentifier) x509.SignatureAlgor case crypto.SHA512: return x509.SHA512WithRSAPSS } - case "ecdsa": + case lexicon.SignatureFamilyECDSA: switch hash { case crypto.SHA256: return x509.ECDSAWithSHA256 diff --git a/pki/historical_past.go b/pki/historical_past.go index 2381248..86d5e12 100644 --- a/pki/historical_past.go +++ b/pki/historical_past.go @@ -18,6 +18,25 @@ import ( "github.com/skarm/xmlsec/internal/historicalproof" ) +const ( + clause562241 = "5.6.2.2.4(1)" + clause562242B = "5.6.2.2.4(2b)" + clause562242C = "5.6.2.2.4(2c)" + clause562242D = "5.6.2.2.4(2d)" + clause5622Note5 = "5.6.2.2.note5" + stageCryptographicConstraints = "cryptographic-constraints" + stageFreshness = "freshness" + stageRevocation = "revocation" + stageSourceSignerPath = "source-signer-path" + stageTrustAnchorSunset = "trust-anchor-sunset" + maxPastValidationWork = 4096 +) + +const ( + codeNoCertificateChainFound = "NO_CERTIFICATE_CHAIN_FOUND" + codeNoPoe = "NO_POE" +) + type historicalStatus struct { claims []historicalClaim primary historicalClaim @@ -31,7 +50,7 @@ type historicalStatus struct { type pastEvaluation struct { session *HistoricalSession proofs *historicalproof.Set - active map[[32]byte]struct{} + active map[[sha256.Size]byte]struct{} steps []TimeTransition used []*VerifiedEvidence work int @@ -70,7 +89,7 @@ func (e *pastEvaluation) slide(ctx context.Context, path *HistoricalPath, contro func (e *pastEvaluation) slideCertificate(ctx context.Context, path *HistoricalPath, index int, claims []historicalClaim, control time.Time, skipLeaf, authorizationLeaf bool) (time.Time, error) { e.work++ - if e.work > 4096 { + if e.work > maxPastValidationWork { return time.Time{}, fmt.Errorf("%w: %w: historical certificate work", ErrPolicy, ErrLimit) } @@ -104,7 +123,7 @@ func (e *pastEvaluation) slideCertificate(ctx context.Context, path *HistoricalP bound := e.cryptoBound(certificate, selected, control) if bound.Before(control) { - e.transition(certificate, selected.primary.fact, "5.6.2.2.4(2d)", "cryptographic-constraints", control, bound) + e.transition(certificate, selected.primary.fact, clause562242D, stageCryptographicConstraints, control, bound) control = bound } @@ -129,7 +148,7 @@ func (e *pastEvaluation) slideExemptCertificate(certificate, issuer *x509.Certif } if cutoff.Before(control) { - e.transition(certificate, nil, "5.6.2.2.4(2d)", "cryptographic-constraints", control, cutoff) + e.transition(certificate, nil, clause562242D, stageCryptographicConstraints, control, cutoff) control = cutoff } @@ -148,7 +167,7 @@ func (e *pastEvaluation) slideSourcePaths(ctx context.Context, path *HistoricalP } if bound.Before(control) { - e.transition(path.chain[index], claim.fact, "5.6.2.2.note5", "source-signer-path", control, bound) + e.transition(path.chain[index], claim.fact, clause5622Note5, stageSourceSignerPath, control, bound) control = bound } } @@ -163,7 +182,7 @@ func (e *pastEvaluation) slideStatus(certificate *x509.Certificate, status histo } if e.session.options.Model == ShellModel || status.reason <= 2 { - e.transition(certificate, status.primary.fact, "5.6.2.2.4(2b)", "revocation", control, status.revokedAt) + e.transition(certificate, status.primary.fact, clause562242B, stageRevocation, control, status.revokedAt) control = status.revokedAt } @@ -176,7 +195,7 @@ func (e *pastEvaluation) slideStatus(certificate *x509.Certificate, status histo } if bound := claim.fact.this; bound.Before(control) { - e.transition(certificate, claim.fact, "5.6.2.2.4(2c)", "freshness", control, bound) + e.transition(certificate, claim.fact, clause562242C, stageFreshness, control, bound) control = bound } } @@ -281,7 +300,7 @@ func (e *pastEvaluation) cryptoBound(certificate *x509.Certificate, status histo } func (s *HistoricalSession) validatePast(ctx context.Context, targetDER []byte, proofs *historicalproof.Set) (*PastCertificateResult, *PastCertificateReport, error) { - report := &PastCertificateReport{subIndication: "NO_CERTIFICATE_CHAIN_FOUND"} + report := &PastCertificateReport{subIndication: codeNoCertificateChainFound} target, err := s.historicalTarget(ctx, targetDER) if err != nil { @@ -327,19 +346,19 @@ func (s *HistoricalSession) validatePastPath(ctx context.Context, path *Historic return nil, report, err } if proofs == nil || !proofs.Valid() || !proofs.Now().Equal(s.now) { - return reject("NO_POE", ErrPolicy) + return reject(codeNoPoe, ErrPolicy) } if _, err := s.checkPath(ctx, path); err != nil { return reject(historicalPathFailure(err), err) } - evaluator := &pastEvaluation{session: s, proofs: proofs, active: make(map[[32]byte]struct{})} + evaluator := &pastEvaluation{session: s, proofs: proofs, active: make(map[[sha256.Size]byte]struct{})} bound := s.now anchor := sha256.Sum256(path.chain[len(path.chain)-1].Raw) if sunset, found := s.options.TrustAnchorSunsets[hex.EncodeToString(anchor[:])]; found && sunset.Before(bound) { - evaluator.transition(path.chain[len(path.chain)-1], nil, "5.6.2.2.4(1)", "trust-anchor-sunset", bound, sunset) + evaluator.transition(path.chain[len(path.chain)-1], nil, clause562241, stageTrustAnchorSunset, bound, sunset) bound = sunset } @@ -348,11 +367,11 @@ func (s *HistoricalSession) validatePastPath(ctx context.Context, path *Historic report.evidence = canonicalEvidence(evaluator.used) if err != nil { - return reject("NO_POE", err) + return reject(codeNoPoe, err) } if err := ctx.Err(); err != nil { - return reject("NO_POE", err) + return reject(codeNoPoe, err) } return &PastCertificateResult{path: path, at: at, evidence: report.evidence}, report, nil @@ -406,7 +425,7 @@ func (s *HistoricalSession) selectHistoricalStatusFor(ctx context.Context, certi return combineHistoricalStatuses(selected) } -func historicalProofAt(proofs *historicalproof.Set, digest [32]byte, kind historicalproof.Kind, at time.Time) bool { +func historicalProofAt(proofs *historicalproof.Set, digest [sha256.Size]byte, kind historicalproof.Kind, at time.Time) bool { proof, found := proofs.EarliestKind(digest, kind) return found && !proof.After(at) } @@ -649,7 +668,7 @@ func historicalKeyBits(certificate *x509.Certificate) int { case *ecdsa.PublicKey: return key.Curve.Params().BitSize case ed25519.PublicKey: - return 256 + return ed25519.PublicKeySize * certificateOctetBits } return 0 diff --git a/pki/historical_path.go b/pki/historical_path.go index ccb9d8c..8d53e53 100644 --- a/pki/historical_path.go +++ b/pki/historical_path.go @@ -14,6 +14,11 @@ import ( "time" "github.com/skarm/xmlsec/internal/der" + "github.com/skarm/xmlsec/internal/lexicon" +) + +const ( + maxPathSearchWork = 8192 ) func (s *HistoricalSession) initializeCertificates(ctx context.Context) error { @@ -21,7 +26,7 @@ func (s *HistoricalSession) initializeCertificates(ctx context.Context) error { return nil } - certificates := make(map[[32]byte]*x509.Certificate, len(s.verifier.originalCertificates)+len(s.evidence.Intermediates)) + certificates := make(map[[sha256.Size]byte]*x509.Certificate, len(s.verifier.originalCertificates)+len(s.evidence.Intermediates)) maps.Copy(certificates, s.verifier.originalCertificates) for _, raw := range s.evidence.Intermediates { @@ -116,13 +121,13 @@ func (s *HistoricalSession) pathsFor(ctx context.Context, target *x509.Certifica sort.Slice(certificates, func(i, j int) bool { return bytes.Compare(certificates[i].Raw, certificates[j].Raw) < 0 }) - roots := make(map[[32]byte]struct{}, len(s.verifier.anchorCertificates)) + roots := make(map[[sha256.Size]byte]struct{}, len(s.verifier.anchorCertificates)) for _, raw := range s.verifier.anchorCertificates { roots[sha256.Sum256(raw)] = struct{}{} } search := historicalPathSearch{session: s, certificates: certificates, roots: roots} - if err := search.visit(ctx, []*x509.Certificate{target}, map[[32]byte]struct{}{digest: {}}); err != nil { + if err := search.visit(ctx, []*x509.Certificate{target}, map[[sha256.Size]byte]struct{}{digest: {}}); err != nil { return nil, err } @@ -149,7 +154,7 @@ func (s *HistoricalSession) checkPathUsages(ctx context.Context, path *Historica } s.signatureWork += len(path.chain) - if s.signatureWork > 4096 { + if s.signatureWork > maxSignatureVerifications { return nil, fmt.Errorf("%w: %w: historical signature work", ErrPolicy, ErrLimit) } @@ -183,7 +188,7 @@ func (s *HistoricalSession) checkPathUsages(ctx context.Context, path *Historica intermediates.AddCert(view) } - chains, err := views[0].Verify(x509.VerifyOptions{Roots: roots, Intermediates: intermediates, CurrentTime: reference, KeyUsages: append([]x509.ExtKeyUsage(nil), usages...), MaxConstraintComparisions: 100000}) + chains, err := views[0].Verify(x509.VerifyOptions{Roots: roots, Intermediates: intermediates, CurrentTime: reference, KeyUsages: append([]x509.ExtKeyUsage(nil), usages...), MaxConstraintComparisions: maxNameConstraintComparisons}) if err != nil { return nil, fmt.Errorf("%w: %w", ErrCertificate, err) } @@ -232,7 +237,7 @@ func historicalPathFailure(err error) string { ) if errors.As(err, &configured) || errors.As(err, &certificate) && certificate.Reason == x509.IncompatibleUsage { - return "CHAIN_CONSTRAINTS_FAILURE" + return lexicon.HistoricalCodeChainConstraintsFailure } return historicalChainFailure @@ -241,11 +246,11 @@ func historicalPathFailure(err error) string { type historicalPathSearch struct { session *HistoricalSession certificates []*x509.Certificate - roots map[[32]byte]struct{} + roots map[[sha256.Size]byte]struct{} paths []*HistoricalPath } -func (search *historicalPathSearch) visit(ctx context.Context, chain []*x509.Certificate, seen map[[32]byte]struct{}) error { +func (search *historicalPathSearch) visit(ctx context.Context, chain []*x509.Certificate, seen map[[sha256.Size]byte]struct{}) error { if err := ctx.Err(); err != nil { return err } @@ -263,7 +268,7 @@ func (search *historicalPathSearch) visit(ctx context.Context, chain []*x509.Cer _, _ = hash.Write(digest[:]) } - var fingerprint [32]byte + var fingerprint [sha256.Size]byte copy(fingerprint[:], hash.Sum(nil)) search.paths = append(search.paths, &HistoricalPath{session: search.session, chain: append([]*x509.Certificate(nil), chain...), fingerprint: fingerprint}) @@ -295,7 +300,7 @@ func (search *historicalPathSearch) visit(ctx context.Context, chain []*x509.Cer } search.session.pathWork++ - if search.session.pathWork > 8192 { + if search.session.pathWork > maxPathSearchWork { return fmt.Errorf("%w: %w: historical path work", ErrPolicy, ErrLimit) } @@ -370,7 +375,7 @@ func certificateGenerationTime(certificate *x509.Certificate) (time.Time, error) continue } - value, err := der.Read(extension.Value, 64) + value, err := der.Read(extension.Value, maxScalarExtensionBytes) if err != nil { return time.Time{}, ErrCertificate } @@ -396,7 +401,9 @@ func (p *HistoricalPath) FingerprintSHA256() string { } func historicalIntermediates(chain []*x509.Certificate) []*x509.Certificate { - if len(chain) < 3 { + const minimumChainWithIntermediate = 3 // leaf, at least one intermediate, root + + if len(chain) < minimumChainWithIntermediate { return nil } diff --git a/pki/name_views.go b/pki/name_views.go index 16563ba..905467b 100644 --- a/pki/name_views.go +++ b/pki/name_views.go @@ -27,7 +27,7 @@ func normalizedCertificate(ctx context.Context, original *x509.Certificate) (*x5 return &view, nil } -func originalCertificateChains(ctx context.Context, chains [][]*x509.Certificate, originals map[[32]byte]*x509.Certificate) ([][]*x509.Certificate, error) { +func originalCertificateChains(ctx context.Context, chains [][]*x509.Certificate, originals map[[sha256.Size]byte]*x509.Certificate) ([][]*x509.Certificate, error) { out := make([][]*x509.Certificate, 0, len(chains)) for _, chain := range chains { if err := ctx.Err(); err != nil { diff --git a/pki/ocsp.go b/pki/ocsp.go index 72bc65a..9fc6640 100644 --- a/pki/ocsp.go +++ b/pki/ocsp.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "crypto" + "crypto/fips140" "crypto/subtle" "crypto/x509" "crypto/x509/pkix" @@ -18,6 +19,20 @@ import ( "github.com/skarm/xmlsec/internal/x509name" ) +// certificateOctetBits converts encoded certificate and evidence byte widths to bits. +const certificateOctetBits = 8 + +const ( + // Caps certificate serial numbers used in OCSP requests and revocation entries, in bits. + maxCertificateSerialBits = 160 + maximumOCSPExtensions = 32 + // Maximum encoded nonce value accepted by the OCSP request/response profile, in bytes. + maximumOCSPNonceBytes = 128 + maximumOCSPPublicKeyDERBytes = 1 << 20 + maximumOCSPRequestBytes = 1 << 16 + minimumOCSPRequestNonceBytes = 32 +) + // OCSPOptions selects offline response checks at an explicit verification time. // The caller must authenticate the issuer separately. SHA-1 CertID and ResponderID // hashes serve as identifiers and do not select a SHA-1 signature algorithm. @@ -142,33 +157,44 @@ type OCSPRequest struct { // ParseOCSPRequest parses one complete bounded OCSP request without authenticating its // origin. func ParseOCSPRequest(b []byte) (*OCSPRequest, error) { - if len(b) > 1<<16 { + // The supported unsigned profile has one TBS request and one certificate + // request. Only TBS-level extensions are optional; their context tag is [2]. + const ( + unsignedRequestFields = 1 + tbsRequestRequiredFields = 1 + tbsRequestMaximumFields = 2 + requestListItems = 1 + singleRequestFields = 1 + requestExtensionsTag = 2 + ) + + if len(b) > maximumOCSPRequestBytes { return nil, ErrEvidence } b = bytes.Clone(b) - v, e := der.Read(b, 1<<16) + v, e := der.Read(b, maximumOCSPRequestBytes) if e != nil { return nil, ErrEvidence } - f, e := der.Sequence(v, 1, 1) + f, e := der.Sequence(v, unsignedRequestFields, unsignedRequestFields) if e != nil { return nil, ErrUnsupported } - t, e := der.Sequence(f[0], 1, 2) + t, e := der.Sequence(f[0], tbsRequestRequiredFields, tbsRequestMaximumFields) if e != nil { return nil, ErrEvidence } - list, e := der.Sequence(t[0], 1, 1) + list, e := der.Sequence(t[0], requestListItems, requestListItems) if e != nil { return nil, ErrUnsupported } - entry, e := der.Sequence(list[0], 1, 1) + entry, e := der.Sequence(list[0], singleRequestFields, singleRequestFields) if e != nil { return nil, ErrUnsupported } @@ -180,8 +206,8 @@ func ParseOCSPRequest(b []byte) (*OCSPRequest, error) { r := &OCSPRequest{id: id, raw: b} - if len(t) > 1 { - x, e := der.Explicit(t[1], 2) + if len(t) > tbsRequestRequiredFields { + x, e := der.Explicit(t[1], requestExtensionsTag) if e != nil { return nil, ErrEvidence } @@ -240,7 +266,7 @@ func (r *OCSPRequest) Hash() crypto.Hash { // by a cryptographically strong random source (RFC 9654 section 2.1). Incoming // requests retain support for legacy 1--31 byte nonces. func CreateOCSPRequest(certificateDER, issuerDER []byte, h crypto.Hash, nonce []byte) ([]byte, error) { - if len(nonce) > 0 && len(nonce) < 32 || len(nonce) > 128 { + if len(nonce) > 0 && len(nonce) < minimumOCSPRequestNonceBytes || len(nonce) > maximumOCSPNonceBytes { return nil, ErrPolicy } @@ -268,7 +294,10 @@ func CreateOCSPRequest(certificateDER, issuerDER []byte, h crypto.Hash, nonce [] } func parseCertID(v asn1.RawValue) (certID, error) { - f, e := der.Sequence(v, 4, 4) + // CertID consists of hashAlgorithm, issuerNameHash, issuerKeyHash and serialNumber. + const certIDFields = 4 + + f, e := der.Sequence(v, certIDFields, certIDFields) if e != nil { return certID{}, ErrEvidence } @@ -287,7 +316,7 @@ func parseCertID(v asn1.RawValue) (certID, error) { n, k []byte s *big.Int ) - if der.Decode(f[1], &n) != nil || der.Decode(f[2], &k) != nil || der.Decode(f[3], &s) != nil || s == nil || s.Sign() <= 0 || s.BitLen() > 160 || len(n) != h.Size() || len(k) != h.Size() { + if der.Decode(f[1], &n) != nil || der.Decode(f[2], &k) != nil || der.Decode(f[3], &s) != nil || s == nil || s.Sign() <= 0 || s.BitLen() > maxCertificateSerialBits || len(n) != h.Size() || len(k) != h.Size() { return certID{}, ErrEvidence } @@ -295,7 +324,13 @@ func parseCertID(v asn1.RawValue) (certID, error) { } func extensions(v asn1.RawValue) ([]pkix.Extension, error) { - fields, e := der.Sequence(v, 1, 32) + // Extension contains extnID, optional critical, and extnValue. + const ( + extensionMinimumFields = 2 + extensionMaximumFields = 3 + ) + + fields, e := der.Sequence(v, 1, maximumOCSPExtensions) if e != nil { return nil, ErrEvidence } @@ -305,7 +340,7 @@ func extensions(v asn1.RawValue) ([]pkix.Extension, error) { seen := map[string]struct{}{} for _, field := range fields { - f, e := der.Sequence(field, 2, 3) + f, e := der.Sequence(field, extensionMinimumFields, extensionMaximumFields) if e != nil { return nil, ErrEvidence } @@ -323,7 +358,7 @@ func extensions(v asn1.RawValue) ([]pkix.Extension, error) { x := pkix.Extension{Id: oid} i := 1 - if len(f) == 3 { + if len(f) == extensionMaximumFields { if der.Decode(f[1], &x.Critical) != nil || !x.Critical { return nil, ErrEvidence } @@ -331,7 +366,7 @@ func extensions(v asn1.RawValue) ([]pkix.Extension, error) { i++ } - if !der.Is(f[i], 0, asn1.TagOctetString, false) { + if !der.Is(f[i], asn1.ClassUniversal, asn1.TagOctetString, false) { return nil, ErrEvidence } @@ -352,7 +387,7 @@ func nonceFrom(exts []pkix.Extension, allow bool) ([]byte, error) { return nil, ErrUnsupported } - if der.Unmarshal(e.Value, &nonce) != nil || len(nonce) < 1 || len(nonce) > 128 { + if der.Unmarshal(e.Value, &nonce) != nil || len(nonce) < 1 || len(nonce) > maximumOCSPNonceBytes { return nil, ErrEvidence } } else if e.Critical { @@ -364,18 +399,20 @@ func nonceFrom(exts []pkix.Extension, allow bool) ([]byte, error) { } func publicBits(c *x509.Certificate) ([]byte, error) { - v, e := der.Read(c.RawSubjectPublicKeyInfo, 1<<20) + const subjectPublicKeyInfoFields = 2 // algorithm and subjectPublicKey BIT STRING + + v, e := der.Read(c.RawSubjectPublicKeyInfo, maximumOCSPPublicKeyDERBytes) if e != nil { return nil, e } - f, e := der.Sequence(v, 2, 2) + f, e := der.Sequence(v, subjectPublicKeyInfoFields, subjectPublicKeyInfoFields) if e != nil { return nil, e } var b asn1.BitString - if der.Decode(f[1], &b) != nil || b.BitLength%8 != 0 { + if der.Decode(f[1], &b) != nil || b.BitLength%certificateOctetBits != 0 { return nil, ErrCertificate } @@ -387,6 +424,12 @@ func makeCertID(ctx context.Context, c, issuer *x509.Certificate, h crypto.Hash) h = crypto.SHA256 } + // CertID hashes identify the issuer, but the FIPS runtime still forbids + // invoking SHA-1. Reject the requested algorithm before entering that primitive. + if h == crypto.SHA1 && fips140.Enabled() { + return certID{}, ErrPolicy + } + a, e := signature.DigestAlgorithm(h) if e != nil { return certID{}, ErrPolicy @@ -397,7 +440,7 @@ func makeCertID(ctx context.Context, c, issuer *x509.Certificate, h crypto.Hash) return certID{}, fmt.Errorf("%w: issuer name: %w", ErrCertificate, err) } - if c.SerialNumber == nil || c.SerialNumber.Sign() <= 0 || c.SerialNumber.BitLen() > 160 || !equal || c.CheckSignatureFrom(issuer) != nil { + if c.SerialNumber == nil || c.SerialNumber.Sign() <= 0 || c.SerialNumber.BitLen() > maxCertificateSerialBits || !equal || c.CheckSignatureFrom(issuer) != nil { return certID{}, ErrCertificate } diff --git a/pki/ocsp_authority.go b/pki/ocsp_authority.go index 9fffa77..42985d7 100644 --- a/pki/ocsp_authority.go +++ b/pki/ocsp_authority.go @@ -9,6 +9,12 @@ import ( "github.com/skarm/xmlsec/internal/x509cert" ) +const ( + // Caps explicitly configured issuer-scoped OCSP signing authorities. + maxOCSPAuthorities = 8 + maximumOCSPAuthorityBytes = 8 << 20 +) + // OCSPAuthority pins one OCSP signing certificate for one exact issuer certificate. // The application establishes this trust out of band. Verification still checks // certificate validity and signing usage. Received names or certificates cannot @@ -24,17 +30,17 @@ type OCSPAuthority struct { } func normalizeOCSPAuthorities(input []OCSPAuthority) ([]OCSPAuthority, error) { - if len(input) > 8 { + if len(input) > maxOCSPAuthorities { return nil, ErrPolicy } - remaining := 8 << 20 + remaining := maximumOCSPAuthorityBytes seen := make(map[[2][sha256.Size]byte]struct{}) output := make([]OCSPAuthority, 0, len(input)) for _, authority := range input { for _, raw := range [][]byte{authority.ResponderCertificateDER, authority.IssuerCertificateDER} { - if len(raw) < 1 || len(raw) > 1<<20 || len(raw) > remaining { + if len(raw) < 1 || len(raw) > certificateDERBytes || len(raw) > remaining { return nil, ErrPolicy } diff --git a/pki/ocsp_issue.go b/pki/ocsp_issue.go index 88a1966..c8ee82d 100644 --- a/pki/ocsp_issue.go +++ b/pki/ocsp_issue.go @@ -15,6 +15,23 @@ import ( "github.com/skarm/xmlsec/internal/streamio" ) +// Complete constructed identifier octets for streamed OCSP response framing. +const ( + ocspSequenceTag = 0x30 + ocspExplicit0Tag = 0xa0 +) + +const ( + // Hard byte ceiling for supported OCSP response encodings. + maximumOCSPResponseBytes = 8 << 20 + maximumOCSPTimeYear = 9999 + ocspOutputChunkBytes = 32 << 10 +) + +const ( + generalizedTimeEncoding = "generalized" +) + // OCSPResponseTemplate supplies caller-asserted certificate status and UTC times // at whole-second precision. Encoding does not determine whether a certificate is // revoked. @@ -85,17 +102,17 @@ func WriteOCSPResponse(ctx context.Context, dst io.Writer, requestDER, certifica } func genTime(t time.Time) ([]byte, error) { - if t.IsZero() || t.Year() < 1 || t.Year() > 9999 || t.Nanosecond() != 0 { + if t.IsZero() || t.Year() < 1 || t.Year() > maximumOCSPTimeYear || t.Nanosecond() != 0 { return nil, ErrPolicy } - return asn1.MarshalWithParams(t.UTC(), "generalized") + return asn1.MarshalWithParams(t.UTC(), generalizedTimeEncoding) } func ocspResponseSize(parts [][]byte) (int, error) { size := 0 for _, part := range parts { - if len(part) > 8<<20-size { + if len(part) > maximumOCSPResponseBytes-size { return 0, ErrPolicy } @@ -118,7 +135,7 @@ func writeOCSPResponse(ctx context.Context, dst io.Writer, parts [][]byte, size for _, part := range parts { for len(part) > 0 { - chunk := part[:min(len(part), 32<<10)] + chunk := part[:min(len(part), ocspOutputChunkBytes)] if _, err := writer.Write(chunk); err != nil { return writer.Written, errors.Join(err, ctx.Err()) } @@ -182,9 +199,9 @@ func prepareOCSPResponse(ctx context.Context, requestDER, certificateDER, issuer switch t.Status { case OCSPGood: - st = der.Wrap(2, 0, false, nil) + st = der.Wrap(asn1.ClassContextSpecific, 0, false, nil) case OCSPUnknown: - st = der.Wrap(2, 2, false, nil) + st = der.Wrap(asn1.ClassContextSpecific, 2, false, nil) case OCSPRevoked: if !validReason(t.RevocationReason) || t.RevokedAt.IsZero() || t.RevokedAt.After(t.ProducedAt) { return nil, ErrPolicy @@ -202,7 +219,7 @@ func prepareOCSPResponse(ctx context.Context, requestDER, certificateDER, issuer f = append(f, der.Ctx(0, rr)) } - st = der.Wrap(2, 1, true, bytes.Join(f, nil)) + st = der.Wrap(asn1.ClassContextSpecific, 1, true, bytes.Join(f, nil)) default: return nil, ErrPolicy } @@ -245,18 +262,18 @@ func prepareOCSPResponse(ctx context.Context, requestDER, certificateDER, issuer return nil, e } - bv, _ := asn1.Marshal(asn1.BitString{Bytes: value, BitLength: len(value) * 8}) + bv, _ := asn1.Marshal(asn1.BitString{Bytes: value, BitLength: len(value) * certificateOctetBits}) bs := [][]byte{tbs, signature.EncAlgorithm(alg), bv} if !bytes.Equal(responder.Raw, issuer.Raw) { - bs = append(bs, frameOCSP(0xa0, frameOCSP(0x30, [][]byte{responder.Raw}))...) + bs = append(bs, frameOCSP(ocspExplicit0Tag, frameOCSP(ocspSequenceTag, [][]byte{responder.Raw}))...) } success, _ := asn1.Marshal(asn1.Enumerated(0)) - response := frameOCSP(0x30, append([][]byte{der.OID(oidOCSPBasic())}, frameOCSP(4, frameOCSP(0x30, bs))...)) + response := frameOCSP(ocspSequenceTag, append([][]byte{der.OID(oidOCSPBasic())}, frameOCSP(asn1.TagOctetString, frameOCSP(ocspSequenceTag, bs))...)) - return frameOCSP(0x30, append([][]byte{success}, frameOCSP(0xa0, response)...)), nil + return frameOCSP(ocspSequenceTag, append([][]byte{success}, frameOCSP(ocspExplicit0Tag, response)...)), nil } func frameOCSP(tag byte, parts [][]byte) [][]byte { diff --git a/pki/ocsp_parse.go b/pki/ocsp_parse.go index 786d62a..0a4f57b 100644 --- a/pki/ocsp_parse.go +++ b/pki/ocsp_parse.go @@ -8,9 +8,18 @@ import ( "time" "github.com/skarm/xmlsec/internal/der" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/signature" ) +const ( + generalizedTimeBytes = 15 + maximumOCSPEmbeddedCertificates = 8 + maximumOCSPEntries = 64 + reasonAACompromise = 10 + reasonUnused = 7 +) + type singleResponse struct { id certID status OCSPStatus @@ -23,11 +32,11 @@ type singleResponse struct { func (s *singleResponse) parseStatus(v asn1.RawValue) error { switch { - case der.Is(v, 2, 0, false) && len(v.Bytes) == 0: + case der.Is(v, asn1.ClassContextSpecific, 0, false) && len(v.Bytes) == 0: s.status = OCSPGood - case der.Is(v, 2, 2, false) && len(v.Bytes) == 0: + case der.Is(v, asn1.ClassContextSpecific, 2, false) && len(v.Bytes) == 0: s.status = OCSPUnknown - case der.Is(v, 2, 1, true): + case der.Is(v, asn1.ClassContextSpecific, 1, true): s.status = OCSPRevoked return s.parseRevokedInfo(v) @@ -67,7 +76,7 @@ func (s *singleResponse) parseRevokedInfo(v asn1.RawValue) error { func (s *singleResponse) parseOptionalFields(fields []asn1.RawValue) error { i := 0 - if i < len(fields) && der.Is(fields[i], 2, 0, true) { + if i < len(fields) && der.Is(fields[i], asn1.ClassContextSpecific, 0, true) { next, err := der.Explicit(fields[i], 0) if err != nil { return err @@ -105,7 +114,7 @@ func (s *singleResponse) parseOptionalFields(fields []asn1.RawValue) error { return ErrEvidence } - value, err := der.Read(extension.Value, 64) + value, err := der.Read(extension.Value, maxScalarExtensionBytes) if err != nil { return ErrEvidence } @@ -126,14 +135,14 @@ func (s *singleResponse) parseOptionalFields(fields []asn1.RawValue) error { return nil } -func validReason(r int) bool { return r >= 0 && r <= 10 && r != 7 } +func validReason(r int) bool { return r >= 0 && r <= reasonAACompromise && r != reasonUnused } func generalized(v asn1.RawValue) (time.Time, error) { var t time.Time // RFC 6960 section 4.2.2.1 applies RFC 5280 section 4.1.2.5.2: // UTC, seconds present, and no fractional seconds. encoding/asn1 also // accepts BER time forms; successful decoding alone is insufficient. - if !der.Is(v, 0, asn1.TagGeneralizedTime, false) || len(v.Bytes) != 15 || der.Decode(v, &t) != nil || t.Year() < 1 || string(v.Bytes) != t.UTC().Format("20060102150405Z") { + if !der.Is(v, asn1.ClassUniversal, asn1.TagGeneralizedTime, false) || len(v.Bytes) != generalizedTimeBytes || der.Decode(v, &t) != nil || t.Year() < 1 || string(v.Bytes) != t.UTC().Format(lexicon.ASN1GeneralizedTimeLayout) { return t, ErrEvidence } @@ -157,7 +166,7 @@ func (p *parsedOCSP) parseCertificates(v asn1.RawValue) error { return ErrEvidence } - certificates, err := der.Sequence(x, 1, 8) + certificates, err := der.Sequence(x, 1, maximumOCSPEmbeddedCertificates) if err != nil { return ErrEvidence } @@ -175,13 +184,19 @@ func (p *parsedOCSP) parseCertificates(v asn1.RawValue) error { } func (p *parsedOCSP) parseResponseData(v asn1.RawValue) error { - fields, err := der.Sequence(v, 3, 4) + // The supported v1 ResponseData omits its default version; extensions are optional. + const ( + responseDataRequiredFields = 3 + responseDataMaximumFields = 4 + ) + + fields, err := der.Sequence(v, responseDataRequiredFields, responseDataMaximumFields) if err != nil { return ErrEvidence } p.rid = fields[0] - if !der.Is(p.rid, 2, 1, true) && !der.Is(p.rid, 2, 2, true) { + if !der.Is(p.rid, asn1.ClassContextSpecific, 1, true) && !der.Is(p.rid, asn1.ClassContextSpecific, 2, true) { return ErrUnsupported } @@ -194,7 +209,7 @@ func (p *parsedOCSP) parseResponseData(v asn1.RawValue) error { return err } - if len(fields) == 4 { + if len(fields) == responseDataMaximumFields { return p.parseResponseExtensions(fields[3]) } @@ -202,7 +217,7 @@ func (p *parsedOCSP) parseResponseData(v asn1.RawValue) error { } func (p *parsedOCSP) parseEntries(v asn1.RawValue) error { - entries, err := der.Sequence(v, 1, 64) + entries, err := der.Sequence(v, 1, maximumOCSPEntries) if err != nil { return ErrEvidence } @@ -236,7 +251,13 @@ func (p *parsedOCSP) parseResponseExtensions(v asn1.RawValue) error { } func parseSingle(v asn1.RawValue) (singleResponse, error) { - fields, err := der.Sequence(v, 3, 5) + // CertID, certStatus and thisUpdate precede optional nextUpdate and extensions. + const ( + singleResponseRequiredFields = 3 + singleResponseMaximumFields = 5 + ) + + fields, err := der.Sequence(v, singleResponseRequiredFields, singleResponseMaximumFields) if err != nil { return singleResponse{}, ErrEvidence } @@ -256,7 +277,7 @@ func parseSingle(v asn1.RawValue) (singleResponse, error) { return s, err } - if err := s.parseOptionalFields(fields[3:]); err != nil { + if err := s.parseOptionalFields(fields[singleResponseRequiredFields:]); err != nil { return s, err } @@ -283,30 +304,41 @@ func parseOCSP(b []byte, maximum int) (parsedOCSP, error) { // ocspResponseBytes validates the outer status before decoding BasicOCSPResponse. func ocspResponseBytes(b []byte, maximum int) ([]byte, error) { + // OCSPResponseStatus uses encoded ENUMERATED values; 4 is unassigned and + // unauthorized (6) is the last defined status. Only successful carries a body. + const ( + responseStatusSuccessful = 0 + responseStatusUnassigned = 4 + responseStatusUnauthorized = 6 + responseMinimumFields = 1 // responseStatus alone on failure + responseMaximumFields = 2 // successful response adds responseBytes + responseBytesFields = 2 // responseType OID and response OCTET STRING + ) + v, err := der.Read(b, maximum) if err != nil { return nil, ErrEvidence } - fields, err := der.Sequence(v, 1, 2) + fields, err := der.Sequence(v, responseMinimumFields, responseMaximumFields) if err != nil { return nil, ErrEvidence } var status asn1.Enumerated - if der.Decode(fields[0], &status) != nil || status < 0 || status > 6 || status == 4 { + if der.Decode(fields[0], &status) != nil || status < 0 || status > responseStatusUnauthorized || status == responseStatusUnassigned { return nil, ErrEvidence } - if status != 0 { - if len(fields) != 1 { + if status != responseStatusSuccessful { + if len(fields) != responseMinimumFields { return nil, ErrEvidence } return nil, fmt.Errorf("%w: OCSP response status %d", ErrUnavailable, status) } - if len(fields) != 2 { + if len(fields) != responseMaximumFields { return nil, ErrEvidence } @@ -315,12 +347,12 @@ func ocspResponseBytes(b []byte, maximum int) ([]byte, error) { return nil, ErrEvidence } - response, err := der.Sequence(x, 2, 2) + response, err := der.Sequence(x, responseBytesFields, responseBytesFields) if err != nil || !der.OIDEqual(response[0], oidOCSPBasic()) { return nil, ErrUnsupported } - if !der.Is(response[1], 0, asn1.TagOctetString, false) { + if !der.Is(response[1], asn1.ClassUniversal, asn1.TagOctetString, false) { return nil, ErrEvidence } @@ -328,12 +360,18 @@ func ocspResponseBytes(b []byte, maximum int) ([]byte, error) { } func parseBasicOCSP(data []byte, maximum int) (parsedOCSP, asn1.RawValue, error) { + // BasicOCSPResponse has data, algorithm and signature fields, plus optional certificates. + const ( + basicResponseRequiredFields = 3 + basicResponseMaximumFields = 4 + ) + v, err := der.Read(data, maximum) if err != nil { return parsedOCSP{}, asn1.RawValue{}, ErrEvidence } - fields, err := der.Sequence(v, 3, 4) + fields, err := der.Sequence(v, basicResponseRequiredFields, basicResponseMaximumFields) if err != nil { return parsedOCSP{}, asn1.RawValue{}, ErrEvidence } @@ -344,12 +382,12 @@ func parseBasicOCSP(data []byte, maximum int) (parsedOCSP, asn1.RawValue, error) } var bits asn1.BitString - if der.Decode(fields[2], &bits) != nil || bits.BitLength%8 != 0 || len(bits.Bytes) == 0 { + if der.Decode(fields[2], &bits) != nil || bits.BitLength%certificateOctetBits != 0 || len(bits.Bytes) == 0 { return parsedOCSP{}, asn1.RawValue{}, ErrEvidence } p := parsedOCSP{tbs: fields[0].FullBytes, alg: algorithm, sig: bits.Bytes} - if len(fields) == 4 { + if len(fields) == basicResponseMaximumFields { if err := p.parseCertificates(fields[3]); err != nil { return p, asn1.RawValue{}, err } diff --git a/pki/ocsp_test.go b/pki/ocsp_test.go index 0f4b148..ce1657c 100644 --- a/pki/ocsp_test.go +++ b/pki/ocsp_test.go @@ -4,6 +4,8 @@ import ( "bytes" "context" "crypto" + "crypto/fips140" + "crypto/sha1" "crypto/sha256" "crypto/x509" "crypto/x509/pkix" @@ -14,12 +16,14 @@ import ( "math/big" "runtime" "strconv" + "sync/atomic" "testing" "time" "github.com/skarm/xmlsec/evidence" "github.com/skarm/xmlsec/internal/der" "github.com/skarm/xmlsec/internal/signature" + "github.com/skarm/xmlsec/internal/testutil" "github.com/skarm/xmlsec/internal/testutil/testcert" "github.com/skarm/xmlsec/pki" ) @@ -83,6 +87,8 @@ func TestOCSPRoundTripAndFailures(t *testing.T) { } r, rep, e := pki.VerifyOCSP(t.Context(), raw, c.Leaf.Raw, c.Root.Raw, testcert.Now(), pki.OCSPOptions{}) + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if r != nil || e == nil || rep.Signature != pki.Valid || rep.Status != status { t.Fatalf("%v %+v", e, rep) } @@ -121,7 +127,12 @@ func testOCSPRoundTripAndFailuresCase(t *testing.T, tc *struct { func TestOCSPDelegation(t *testing.T) { c := testcert.New(t) - req, e := pki.CreateOCSPRequest(c.Leaf.Raw, c.Root.Raw, crypto.SHA1, nil) + hash := crypto.SHA1 + if fips140.Enabled() { + hash = crypto.SHA256 + } + + req, e := pki.CreateOCSPRequest(c.Leaf.Raw, c.Root.Raw, hash, nil) if e != nil { t.Fatal(e) } @@ -169,6 +180,169 @@ func TestOCSPDelegation(t *testing.T) { } } +func TestOCSPFIPSRequestIdentifier(t *testing.T) { + if !testutil.RunFIPSTest(t) { + return + } + + chain := testcert.New(t) + if request, err := pki.CreateOCSPRequest(chain.Leaf.Raw, chain.Root.Raw, crypto.SHA256, nil); err != nil || len(request) == 0 { + t.Fatal("SHA-256 CertID must remain available in FIPS", err) + } + + if request, err := pki.CreateOCSPRequest(chain.Leaf.Raw, chain.Root.Raw, crypto.SHA1, nil); request != nil || !errors.Is(err, pki.ErrPolicy) { + t.Fatal("SHA-1 CertID must return a policy error in FIPS", err) + } +} + +func TestOCSPFIPSResponderIdentifier(t *testing.T) { + if !testutil.RunFIPSTest(t) { + return + } + + chain, byName, byKey := ocspKeyIdentifierResponses(t) + if result, _, err := pki.VerifyOCSP(t.Context(), byName, chain.Leaf.Raw, chain.Root.Raw, testcert.Now(), pki.OCSPOptions{}); result == nil || err != nil { + t.Fatal("SHA-256 response with byName responder must remain available", err) + } + + if result, _, err := pki.VerifyOCSP(t.Context(), byKey, chain.Leaf.Raw, chain.Root.Raw, testcert.Now(), pki.OCSPOptions{}); result != nil || !errors.Is(err, pki.ErrPolicy) { + t.Fatal("byKey SHA-1 identifier must return a policy error in FIPS", err) + } +} + +func TestOCSPResponderKeyIdentifier(t *testing.T) { + chain, _, byKey := ocspKeyIdentifierResponses(t) + result, report, err := pki.VerifyOCSP(t.Context(), byKey, chain.Leaf.Raw, chain.Root.Raw, testcert.Now(), pki.OCSPOptions{}) + if fips140.Enabled() { + if result != nil || !errors.Is(err, pki.ErrPolicy) { + t.Fatal("byKey identifier was not rejected by FIPS policy", err) + } + + return + } + + if result == nil || err != nil || report.Signature != pki.Valid || report.Authorization != pki.OCSPAuthorityIssuer { + t.Fatal("correctly signed byKey response was rejected", report, err) + } +} + +func TestOCSPChainPreservesCancellationCause(t *testing.T) { + chain, response, _ := ocspKeyIdentifierResponses(t) + verifier, err := pki.NewVerifier(pki.Config{Roots: [][]byte{chain.Root.Raw}, Revocation: pki.RequireOCSP}) + if err != nil { + t.Fatal(err) + } + + // An unrelated invalid extra response must not poison independent GOOD evidence. + withExtra := pki.Evidence{OCSPResponses: [][]byte{response, {0x30, 0}}} + if result, _, err := verifier.Verify(t.Context(), chain.Leaf.Raw, testcert.Now(), withExtra); result == nil || err != nil { + t.Fatal("ordinary invalid extra response poisoned GOOD evidence", err) + } + + checkRevocationCancellation(t, verifier, chain.Leaf.Raw, pki.Evidence{OCSPResponses: [][]byte{response}}) +} + +func TestCRLChainPreservesCancellationCause(t *testing.T) { + chain := testcert.New(t) + verifier, err := pki.NewVerifier(pki.Config{Roots: [][]byte{chain.Root.Raw}, Revocation: pki.RequireCRL}) + if err != nil { + t.Fatal(err) + } + + checkRevocationCancellation(t, verifier, chain.Leaf.Raw, pki.Evidence{CRLs: [][]byte{testcert.CRL(t, chain)}}) +} + +func checkRevocationCancellation(t *testing.T, verifier *pki.Verifier, certificate []byte, evidence pki.Evidence) { + t.Helper() + + if result, _, err := verifier.Verify(t.Context(), certificate, testcert.Now(), evidence); result == nil || err != nil { + t.Fatal("signed evidence must pass before cancellation", err) + } + + canceled, completed := 0, 0 + // Sweep checkpoints rather than relying on one internal call count. The wrapped + // standard context really becomes canceled; it supplies the final observed cause. + for checkpoint := int32(1); checkpoint <= 80; checkpoint++ { + parent, cancel := context.WithCancel(t.Context()) + ctx := &ocspCancelAtCheckContext{Context: parent, cancel: cancel} + ctx.remaining.Store(checkpoint) + + result, _, err := verifier.Verify(ctx, certificate, testcert.Now(), evidence) + if parent.Err() != nil { + canceled++ + if result != nil || !errors.Is(err, context.Canceled) { + t.Errorf("checkpoint %d lost cancellation: result=%v err=%v", checkpoint, result != nil, err) + } + } else { + completed++ + if result == nil || err != nil { + t.Errorf("checkpoint %d failed without cancellation: result=%v err=%v", checkpoint, result != nil, err) + } + } + + cancel() + } + + if canceled == 0 { + t.Fatal("fixture never exercised cancellation") + } + + t.Logf("canceled checkpoints=%d, completed controls=%d", canceled, completed) +} + +type ocspCancelAtCheckContext struct { + context.Context + cancel context.CancelFunc + remaining atomic.Int32 +} + +func (c *ocspCancelAtCheckContext) Err() error { + if c.remaining.Add(-1) == 0 { + c.cancel() + } + + return c.Context.Err() +} + +func ocspKeyIdentifierResponses(t *testing.T) (testcert.Chain, []byte, []byte) { + t.Helper() + + chain := testcert.New(t) + request, err := pki.CreateOCSPRequest(chain.Leaf.Raw, chain.Root.Raw, crypto.SHA256, nil) + if err != nil { + t.Fatal(err) + } + + byName, err := pki.CreateOCSPResponse(t.Context(), request, chain.Leaf.Raw, chain.Root.Raw, chain.Root.Raw, chain.RootKey, ocspTemplate()) + if err != nil { + t.Fatal(err) + } + + identifier := make([]byte, sha1.Size) + if !fips140.Enabled() { + var public struct { + Algorithm pkix.AlgorithmIdentifier + Key asn1.BitString + } + + if rest, err := asn1.Unmarshal(chain.Root.RawSubjectPublicKeyInfo, &public); err != nil || len(rest) != 0 { + t.Fatal("issuer public key", err) + } + + digest := sha1.Sum(public.Key.Bytes) // #nosec G401 -- RFC 6960 byKey identifier, not a signature. + copy(identifier, digest[:]) + } + // In FIPS a well-formed identifier is sufficient: computing its expected + // SHA-1 value must be rejected before comparison or any SHA-1 invocation. + byKey := rewriteOCSPData(t, byName, chain.RootKey, func(fields []asn1.RawValue) [][]byte { + parts := ocspFieldBytes(fields) + parts[0] = der.Ctx(2, der.Octets(identifier)) + return parts + }) + + return chain, byName, byKey +} + func TestOCSPNonceSizeAndCancellation(t *testing.T) { c := testcert.New(t) for _, n := range []int{0, 1, 16, 31, 32, 128, 129} { @@ -403,20 +577,14 @@ func FuzzOCSPResponse(f *testing.F) { } result, report, e := pki.VerifyOCSP(t.Context(), b, leaf, issuer, fixture.At, options) + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if e != nil && result != nil || e == nil && (result == nil || report.Signature != pki.Valid || report.Status != pki.OCSPGood) { t.Fatal("inconsistent OCSP verification result") } }) } -type ocspWriteFunc func([]byte) (int, error) - -func (f ocspWriteFunc) Write(p []byte) (int, error) { return f(p) } - -func ocspTemplate() pki.OCSPResponseTemplate { - return pki.OCSPResponseTemplate{Status: pki.OCSPGood, ProducedAt: testcert.Now(), ThisUpdate: testcert.Now().Add(-time.Minute), NextUpdate: testcert.Now().Add(time.Minute)} -} - func TestOCSPResponderNameCandidateMustBeAuthorized(t *testing.T) { chain := testcert.New(t) key := testcert.Key(t) @@ -519,66 +687,6 @@ func TestOCSPAuthenticatedEvidence(t *testing.T) { } } -// Re-sign each intervention so semantic failures cannot be explained by a bad signature. -func rewriteOCSPData(t *testing.T, raw []byte, key crypto.Signer, change func([]asn1.RawValue) [][]byte) []byte { - t.Helper() - root, err := der.Read(raw, len(raw)) - if err != nil { - t.Fatal(err) - } - outer, err := der.Sequence(root, 2, 2) - if err != nil { - t.Fatal(err) - } - explicit, err := der.Explicit(outer[1], 0) - if err != nil { - t.Fatal(err) - } - response, err := der.Sequence(explicit, 2, 2) - if err != nil { - t.Fatal(err) - } - var octets []byte - if der.Decode(response[1], &octets) != nil { - t.Fatal("response octets") - } - basic, err := der.Read(octets, len(octets)) - if err != nil { - t.Fatal(err) - } - fields, err := der.Sequence(basic, 3, 4) - if err != nil { - t.Fatal(err) - } - tbs, err := der.Sequence(fields[0], 3, 4) - if err != nil { - t.Fatal(err) - } - changed := der.Seq(change(tbs)...) - algorithm, err := signature.Algorithm(fields[1]) - if err != nil { - t.Fatal(err) - } - sig, err := signature.Sign(t.Context(), key, algorithm, changed) - if err != nil { - t.Fatal(err) - } - encoded, _ := asn1.Marshal(asn1.BitString{Bytes: sig, BitLength: len(sig) * 8}) - parts := [][]byte{changed, fields[1].FullBytes, encoded} - if len(fields) == 4 { - parts = append(parts, fields[3].FullBytes) - } - return der.Seq(outer[0].FullBytes, der.Ctx(0, der.Seq(response[0].FullBytes, der.Octets(der.Seq(parts...))))) -} - -func ocspFieldBytes(fields []asn1.RawValue) [][]byte { - out := make([][]byte, len(fields)) - for i := range fields { - out[i] = fields[i].FullBytes - } - return out -} - func TestOCSPMultipleEntrySelection(t *testing.T) { chain := testcert.New(t) request, err := pki.CreateOCSPRequest(chain.Leaf.Raw, chain.Root.Raw, crypto.SHA256, nil) @@ -749,6 +857,8 @@ func TestOCSPAuthorizationDependenciesAndSources(t *testing.T) { } for _, invalid := range []*evidence.Observation{nil, {}, crlSource} { options = pki.OCSPOptions{Sources: []*evidence.Observation{invalid}} + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if result, report, err := pki.VerifyOCSP(t.Context(), raw, chain.Leaf.Raw, chain.Root.Raw, testcert.Now(), options); result != nil || !errors.Is(err, pki.ErrEvidence) || report.Signature != pki.NotChecked { t.Fatal("invalid/unmatched provenance accepted", err) } @@ -853,3 +963,126 @@ func TestOCSPRequestRejectsExplicitDefaultFalse(t *testing.T) { t.Fatal("DEFAULT FALSE encoded in request", err) } } + +type ocspWriteFunc func([]byte) (int, error) + +func (f ocspWriteFunc) Write(p []byte) (int, error) { return f(p) } + +func ocspTemplate() pki.OCSPResponseTemplate { + return pki.OCSPResponseTemplate{Status: pki.OCSPGood, ProducedAt: testcert.Now(), ThisUpdate: testcert.Now().Add(-time.Minute), NextUpdate: testcert.Now().Add(time.Minute)} +} + +// Re-sign each intervention so semantic failures cannot be explained by a bad signature. +func rewriteOCSPData(t *testing.T, raw []byte, key crypto.Signer, change func([]asn1.RawValue) [][]byte) []byte { + t.Helper() + root, err := der.Read(raw, len(raw)) + if err != nil { + t.Fatal(err) + } + outer, err := der.Sequence(root, 2, 2) + if err != nil { + t.Fatal(err) + } + explicit, err := der.Explicit(outer[1], 0) + if err != nil { + t.Fatal(err) + } + response, err := der.Sequence(explicit, 2, 2) + if err != nil { + t.Fatal(err) + } + var octets []byte + if der.Decode(response[1], &octets) != nil { + t.Fatal("response octets") + } + basic, err := der.Read(octets, len(octets)) + if err != nil { + t.Fatal(err) + } + fields, err := der.Sequence(basic, 3, 4) + if err != nil { + t.Fatal(err) + } + tbs, err := der.Sequence(fields[0], 3, 4) + if err != nil { + t.Fatal(err) + } + changed := der.Seq(change(tbs)...) + algorithm, err := signature.Algorithm(fields[1]) + if err != nil { + t.Fatal(err) + } + sig, err := signature.Sign(t.Context(), key, algorithm, changed) + if err != nil { + t.Fatal(err) + } + encoded, _ := asn1.Marshal(asn1.BitString{Bytes: sig, BitLength: len(sig) * 8}) + parts := [][]byte{changed, fields[1].FullBytes, encoded} + if len(fields) == 4 { + parts = append(parts, fields[3].FullBytes) + } + return der.Seq(outer[0].FullBytes, der.Ctx(0, der.Seq(response[0].FullBytes, der.Octets(der.Seq(parts...))))) +} + +func ocspFieldBytes(fields []asn1.RawValue) [][]byte { + out := make([][]byte, len(fields)) + for i := range fields { + out[i] = fields[i].FullBytes + } + return out +} + +func TestOCSPConfiguredAuthoritySourceBindings(t *testing.T) { + chain, responder := testcert.New(t), testcert.New(t) + request, err := pki.CreateOCSPRequest(chain.Leaf.Raw, chain.Root.Raw, 0, nil) + if err != nil { + t.Fatal(err) + } + raw, err := pki.CreateOCSPResponse(t.Context(), request, chain.Leaf.Raw, chain.Root.Raw, responder.Leaf.Raw, responder.LeafKey, ocspTemplate()) + if err != nil { + t.Fatal(err) + } + authorities := []pki.OCSPAuthority{{ResponderCertificateDER: responder.Leaf.Raw, IssuerCertificateDER: chain.Root.Raw}} + options := pki.OCSPOptions{Authorities: authorities} + if result, _, err := pki.VerifyOCSP(t.Context(), raw, chain.Leaf.Raw, chain.Root.Raw, testcert.Now(), options); result == nil || err != nil { + t.Fatal("configured authority must authenticate the control response", err) + } + source, err := evidence.NewObservation(t.Context(), evidence.Certificate, responder.Leaf.Raw, evidence.Provenance{}) + if err != nil { + t.Fatal(err) + } + verifier, err := pki.NewVerifier(pki.Config{Roots: [][]byte{chain.Root.Raw}, Revocation: pki.RequireOCSP, OCSPAuthorities: authorities}) + if err != nil { + t.Fatal(err) + } + if result, _, err := verifier.Verify(t.Context(), chain.Leaf.Raw, testcert.Now(), pki.Evidence{OCSPResponses: [][]byte{raw}, Sources: []*evidence.Observation{source}}); result == nil || err != nil { + t.Fatal("full verifier must accept the exact configured certificate source", err) + } + options.Sources = []*evidence.Observation{source} + if result, report, err := pki.VerifyOCSP(t.Context(), raw, chain.Leaf.Raw, chain.Root.Raw, testcert.Now(), options); result == nil || err != nil || report.Authorization != pki.OCSPAuthorityLocal { + t.Error("standalone verifier rejected the exact configured certificate source", err) + } else if !bytes.Equal(result.ResponderDER(), responder.Leaf.Raw) { + t.Error("certificate provenance changed the authenticated responder") + } + for _, tc := range []struct { + name string + kind evidence.Kind + raw []byte + }{ + {"wrong kind", evidence.CRL, responder.Leaf.Raw}, + {"wrong certificate digest", evidence.Certificate, responder.Root.Raw}, + } { + t.Run(tc.name, func(t *testing.T) { + observation, err := evidence.NewObservation(t.Context(), tc.kind, tc.raw, evidence.Provenance{}) + if err != nil { + t.Fatal(err) + } + options.Sources = []*evidence.Observation{observation} + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil + if result, report, err := pki.VerifyOCSP(t.Context(), raw, chain.Leaf.Raw, chain.Root.Raw, testcert.Now(), options); result != nil || !errors.Is(err, pki.ErrEvidence) || report.Signature != pki.NotChecked { + t.Fatal("unmatched provenance acquired authority", result != nil, report, err) + } + }) + } +} diff --git a/pki/ocsp_verify.go b/pki/ocsp_verify.go index d4c09b0..4c7fe22 100644 --- a/pki/ocsp_verify.go +++ b/pki/ocsp_verify.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "crypto" + "crypto/fips140" "crypto/sha1" // #nosec G505 -- RFC 6960 ResponderID byKey requires SHA-1, independent of the signature algorithm. "crypto/sha256" "crypto/subtle" @@ -20,6 +21,15 @@ import ( "github.com/skarm/xmlsec/internal/x509name" ) +const ( + defaultOCSPMaxAge = 24 * time.Hour + defaultOCSPResponseBytes = 1 << 20 + maximumOCSPClockSkew = 10 * time.Minute + maximumOCSPMaxAge = 30 * 24 * time.Hour + maximumOCSPResponderCRLBytes = 8 << 20 + maximumOCSPResponderCRLs = 32 +) + // VerifyOCSP returns a result only for an authenticated fresh GOOD response. // The issuer must be trusted separately. Revoked/unknown responses yield an // authenticated diagnostic report but never a successful result. @@ -46,11 +56,11 @@ func VerifyOCSP(ctx context.Context, response, certificateDER, issuerDER []byte, return nil, report, err } - if len(response) > o.MaxBytes || len(certificateDER) > 1<<20 || len(issuerDER) > 1<<20 { + if len(response) > o.MaxBytes || len(certificateDER) > certificateDERBytes || len(issuerDER) > certificateDERBytes { return nil, report, ErrEvidence } - sources, err := (&Verifier{maxBytes: 8 << 20}).validatedSources(ctx, certificateDER, Evidence{Intermediates: [][]byte{issuerDER}, OCSPResponses: [][]byte{response}, CRLs: o.ResponderCRLs, Sources: o.Sources}) + sources, err := (&Verifier{maxBytes: defaultMaxEvidenceBytes, ocspOptions: o}).validatedSources(ctx, certificateDER, Evidence{Intermediates: [][]byte{issuerDER}, OCSPResponses: [][]byte{response}, CRLs: o.ResponderCRLs, Sources: o.Sources}) if err != nil { return nil, report, err } @@ -130,6 +140,12 @@ func responderMatches(ctx context.Context, rid asn1.RawValue, c *x509.Certificat return false, ErrEvidence } + // RFC 6960 fixes byKey to SHA-1; it cannot be substituted with another hash + // when the runtime prohibits that primitive. byName remains available. + if fips140.Enabled() { + return false, ErrPolicy + } + bits, e := publicBits(c) if e != nil { return false, e @@ -176,7 +192,7 @@ func authorizeResponder(ctx context.Context, r, issuer *x509.Certificate, at, pr for _, ex := range r.Extensions { if ex.Id.Equal(oidOCSPNoCheck()) { - if ex.Critical || !bytes.Equal(ex.Value, []byte{5, 0}) { + if ex.Critical || !bytes.Equal(ex.Value, []byte{asn1.TagNull, 0}) { return nil, ErrEvidence } @@ -219,21 +235,21 @@ func normalizeOCSP(o OCSPOptions) (OCSPOptions, error) { o.ExpectedNonce = bytes.Clone(o.ExpectedNonce) if o.MaxAge == 0 { - o.MaxAge = 24 * time.Hour + o.MaxAge = defaultOCSPMaxAge } if o.MaxBytes == 0 { - o.MaxBytes = 1 << 20 + o.MaxBytes = defaultOCSPResponseBytes } - if o.MaxAge <= 0 || o.MaxAge > 30*24*time.Hour || o.ClockSkew < 0 || o.ClockSkew > 10*time.Minute || o.MaxBytes < 1 || o.MaxBytes > 8<<20 || len(o.ExpectedNonce) > 128 || len(o.ResponderCRLs) > 32 { + if o.MaxAge <= 0 || o.MaxAge > maximumOCSPMaxAge || o.ClockSkew < 0 || o.ClockSkew > maximumOCSPClockSkew || o.MaxBytes < 1 || o.MaxBytes > maximumOCSPResponseBytes || len(o.ExpectedNonce) > maximumOCSPNonceBytes || len(o.ResponderCRLs) > maximumOCSPResponderCRLs { return o, ErrPolicy } n := 0 for _, b := range o.ResponderCRLs { n += len(b) - if n > 8<<20 { + if n > maximumOCSPResponderCRLBytes { return o, ErrPolicy } } @@ -393,10 +409,13 @@ func matchingOCSPEntry(ctx context.Context, entries []singleResponse, certificat // findOCSPEntry distinguishes an ordinary miss from malformed or ambiguous // matching status data. The standalone verifier still requires a matching entry. func findOCSPEntry(ctx context.Context, entries []singleResponse, certificate, issuer *x509.Certificate) (*singleResponse, error) { + // Initial distinct-hash cache capacity; this does not restrict accepted CertIDs. + const identifierCacheCapacity = 4 + var chosen *singleResponse // CertID algorithms can vary across entries. Cache each expected identifier // so a response cannot repeat certificate signature checks for every entry. - wantedIDs := make(map[crypto.Hash]certID, 4) + wantedIDs := make(map[crypto.Hash]certID, identifierCacheCapacity) for i := range entries { if err := ctx.Err(); err != nil { @@ -529,9 +548,15 @@ func (e *ocspEvidence) checkChain(ctx context.Context, chain []*x509.Certificate verification := ocspVerification{authorization: e.authorization, source: e.sources[j], parsed: parsed, certificate: chain[i], issuer: chain[i+1]} report, err := verifyOCSPData(ctx, &verification, at, e.options) + // Failed verification can retain authenticated revocation diagnostics. + //noinspection GoDfaErrorMayBeNotNil e.authenticated = appendOCSPEvidence(e.authenticated, report.evidence...) - if errors.Is(err, ErrRevoked) { + if cause := ctx.Err(); cause != nil { + return errors.Join(err, cause) + } + + if errors.Is(err, ErrRevoked) || errors.Is(err, ErrLimit) { return err } diff --git a/pki/pki.go b/pki/pki.go index b4bf437..92a6702 100644 --- a/pki/pki.go +++ b/pki/pki.go @@ -18,6 +18,30 @@ import ( "github.com/skarm/xmlsec/internal/x509cert" ) +const ( + // Aggregate DER byte budget selected when Config.MaxEvidenceBytes is zero. + defaultMaxEvidenceBytes = 8 << 20 +) + +const ( + certificateASN1Depth = 33 + certificateASN1Nodes = 100000 + // Per-certificate DER byte cap applied before bounded parsing and evidence processing. + certificateDERBytes = 1 << 20 + defaultMaxCRLs = 32 + defaultMaxChainDepth = 10 + defaultMaxOCSPResponses = 32 + // Hard count ceiling for supplied intermediate-certificate sets. + maxIntermediateCertificates = 64 + maxRootCertificates = 64 + maximumCRLs = 256 + maximumChainDepth = 32 + maximumEvidenceBytes = 64 << 20 + maximumOCSPResponses = 256 + minimumChainDepth = 2 + maxCertificateUsages = 32 +) + var ( // ErrPolicy reports a PKI policy violation. ErrPolicy = errors.New("PKI policy violation") @@ -85,8 +109,8 @@ type Config struct { CRL CRLOptions // MaxChainDepth bounds certificate path depth. Zero selects 10. The maximum is 32. MaxChainDepth int - // MaxEvidenceBytes bounds supplied revocation evidence bytes. Zero selects 8 MiB. The - // maximum is 64 MiB. + // MaxEvidenceBytes bounds aggregate supplied intermediate certificate and revocation + // DER bytes. Zero selects 8 MiB. The maximum is 64 MiB. MaxEvidenceBytes int // MaxCRLs bounds supplied CRLs. Zero selects 32. The maximum is 256. MaxCRLs int @@ -122,21 +146,82 @@ type Evidence struct { // Verifier retains immutable PKIX and offline revocation policy for concurrent // verification calls. type Verifier struct { - roots *x509.CertPool - intermediates *x509.CertPool - usages []x509.ExtKeyUsage - required x509.KeyUsage - allowCA bool - revocation RevocationMode - maxDepth int - maxBytes int - maxCRLs int - maxOCSP int - ocspOptions OCSPOptions - crlOptions CRLOptions - issuerCertificates [][]byte - anchorCertificates [][]byte - originalCertificates map[[32]byte]*x509.Certificate + // roots contains only explicitly configured trust anchors. + roots *x509.CertPool + // intermediates contains configured untrusted path-building candidates. + intermediates *x509.CertPool + // usages retains the accepted extended key usages. + usages []x509.ExtKeyUsage + // required contains key usage bits required on the leaf certificate. + required x509.KeyUsage + // allowCA permits a CA certificate to serve as the leaf. + allowCA bool + // revocation selects the required offline revocation checks. + revocation RevocationMode + // maxDepth bounds the number of certificates in a path. + maxDepth int + // maxBytes bounds aggregate supplied evidence bytes. + maxBytes int + // maxCRLs bounds supplied CRL objects. + maxCRLs int + // maxOCSP bounds supplied OCSP responses. + maxOCSP int + // ocspOptions retains the normalized responder and freshness policy. + ocspOptions OCSPOptions + // crlOptions retains the normalized CRL scope and freshness policy. + crlOptions CRLOptions + // issuerCertificates retains DER issuer candidates for evidence checks. + issuerCertificates [][]byte + // anchorCertificates retains exact configured trust anchor encodings. + anchorCertificates [][]byte + // originalCertificates indexes parsed certificates by exact DER digest. + originalCertificates map[[sha256.Size]byte]*x509.Certificate +} + +// Report contains certificate path and revocation diagnostics. Successful verification +// also returns a Result. +type Report struct { + // evidence retains authenticated evidence diagnostics, including evidence that + // did not satisfy acceptance policy. + evidence []*VerifiedEvidence + // Chain records the certificate path check outcome. + Chain Status `json:"chain"` + // Revocation records the evidence-check outcome; NoRevocation leaves it NotChecked. + Revocation Status `json:"revocation"` + // VerificationTime is the explicit time supplied for certificate and revocation checks. + VerificationTime time.Time `json:"verification_time"` + // ChainLength is the number of certificates in the accepted path, including its trust + // anchor. + ChainLength int `json:"chain_length,omitempty"` + // Error describes the verification failure, or is empty on success. + Error string `json:"error,omitempty"` +} + +// Result records acceptance under the configured certificate and revocation policy. +// It does not establish acceptance under other trust policies or the historical +// validation requirements of AdES. +type Result struct { + // chain retains the accepted DER path, including its trust anchor. + chain [][]byte + // spki retains the accepted leaf's SubjectPublicKeyInfo DER. + spki []byte + // at is the explicit time at which the configured policy accepted the path. + at time.Time + // revocation records the required offline revocation outcome. + revocation Status + // evidence retains the authenticated evidence accepted for this result. + evidence []*VerifiedEvidence +} + +// KeyPin matches SubjectPublicKeyInfo against an explicitly trusted public key. +// Matching does not check certificate dates, CA paths, revocation or subject names. +// Use this trust primitive when the application already trusts the key, such as +// for a configured metadata signer. +type KeyPin struct { + // spki owns the exact trusted SubjectPublicKeyInfo encoding. + spki []byte + // fingerprint caches the SHA-256 digest of spki. + fingerprint [sha256.Size]byte } // NewVerifier validates and snapshots explicit trust and revocation policy for concurrent use. @@ -162,7 +247,7 @@ func NewVerifier(c Config) (*Verifier, error) { usages = []x509.ExtKeyUsage{x509.ExtKeyUsageAny} } - if len(usages) > 32 { + if len(usages) > maxCertificateUsages { return nil, ErrPolicy } @@ -188,6 +273,22 @@ func NewVerifier(c Config) (*Verifier, error) { return v, nil } +// NewKeyPin snapshots a public key as an SPKI pin. Matching a pin does not establish PKIX +// certificate trust. +func NewKeyPin(public crypto.PublicKey) (*KeyPin, error) { + // Marshal may reject nil/unsupported types; require round-trip parse as well. + der, err := safeMarshalPublic(public) + if err != nil { + return nil, err + } + + if _, err = x509.ParsePKIXPublicKey(der); err != nil { + return nil, err + } + + return &KeyPin{spki: der, fingerprint: sha256.Sum256(der)}, nil +} + // Verify requires an explicit time. A caller-chosen past time alone is NOT a // proof that a signature existed then. Certificates are snapshotted; revocation // evidence is borrowed during the call and must not be modified concurrently. @@ -238,7 +339,7 @@ func (v *Verifier) Verify(ctx context.Context, leafDER []byte, at time.Time, e E return nil, report, err } - chains, err := leafView.Verify(x509.VerifyOptions{Roots: v.roots, Intermediates: pool, CurrentTime: at, KeyUsages: append([]x509.ExtKeyUsage(nil), v.usages...), MaxConstraintComparisions: 100000}) + chains, err := leafView.Verify(x509.VerifyOptions{Roots: v.roots, Intermediates: pool, CurrentTime: at, KeyUsages: append([]x509.ExtKeyUsage(nil), v.usages...), MaxConstraintComparisions: maxNameConstraintComparisons}) if err != nil { report.Chain = Invalid return nil, report, fmt.Errorf("%w: %w", ErrCertificate, err) @@ -324,7 +425,7 @@ func (v *Verifier) Verify(ctx context.Context, leafDER []byte, at time.Time, e E // to one byte limit. Root constraints precede each anchor's public-key policy. func (v *Verifier) snapshotCertificates(roots, intermediates [][]byte) error { configuredBytes := 0 - v.originalCertificates = make(map[[32]byte]*x509.Certificate) + v.originalCertificates = make(map[[sha256.Size]byte]*x509.Certificate) for _, group := range []struct { ders [][]byte @@ -378,8 +479,28 @@ func (v *Verifier) validateRequest(ctx context.Context, at time.Time) error { return nil } +// CheckEvidenceLimits checks only configured DER collection counts and aggregate +// byte limits. It does not parse or authenticate evidence, inspect Sources or +// validate historical options. Limit failures retain ErrPolicy and ErrLimit; +// invalid verifiers/contexts return ErrPolicy, and cancellation retains its cause. +func (v *Verifier) CheckEvidenceLimits(ctx context.Context, e Evidence) error { + if v == nil || v.roots == nil || v.intermediates == nil || ctx == nil { + return fmt.Errorf("%w: verifier and context required", ErrPolicy) + } + + if err := ctx.Err(); err != nil { + return err + } + + if err := v.validateEvidenceBounds(e); err != nil { + return errors.Join(ErrLimit, err) + } + + return nil +} + func (v *Verifier) validateEvidenceBounds(e Evidence) error { - if len(e.Intermediates) > 64 || len(e.CRLs) > v.maxCRLs || len(e.OCSPResponses) > v.maxOCSP { + if len(e.Intermediates) > maxIntermediateCertificates || len(e.CRLs) > v.maxCRLs || len(e.OCSPResponses) > v.maxOCSP { return fmt.Errorf("%w: evidence count", ErrPolicy) } @@ -419,7 +540,7 @@ func (v *Verifier) validateLeaf(der []byte) (*x509.Certificate, error) { return leaf, nil } -func (v *Verifier) evidenceIntermediatePool(ctx context.Context, ders [][]byte, originals map[[32]byte]*x509.Certificate) (*x509.CertPool, error) { +func (v *Verifier) evidenceIntermediatePool(ctx context.Context, ders [][]byte, originals map[[sha256.Size]byte]*x509.Certificate) (*x509.CertPool, error) { if len(ders) == 0 { return v.intermediates, nil } @@ -471,7 +592,11 @@ func (v *Verifier) selectVerifiedChain(ctx context.Context, chains [][]*x509.Cer if v.revocation == RequireCRL { chainErr = checkChainCRLs(ctx, chain, lists, at, v.crlOptions) - if errors.Is(chainErr, ErrLimit) || ctx.Err() != nil { + if cause := ctx.Err(); cause != nil { + return nil, errors.Join(chainErr, cause) + } + + if errors.Is(chainErr, ErrLimit) { return nil, chainErr } @@ -482,7 +607,11 @@ func (v *Verifier) selectVerifiedChain(ctx context.Context, chains [][]*x509.Cer if v.revocation == RequireOCSP { chainErr = ocsp.checkChain(ctx, chain, at) - if errors.Is(chainErr, ErrLimit) || ctx.Err() != nil { + if cause := ctx.Err(); cause != nil { + return nil, errors.Join(chainErr, cause) + } + + if errors.Is(chainErr, ErrLimit) { return nil, chainErr } @@ -515,8 +644,8 @@ func (v *Verifier) validateChainPolicy(chain []*x509.Certificate) error { return nil } -func (v *Verifier) certificateOriginals(leaf *x509.Certificate) map[[32]byte]*x509.Certificate { - originals := make(map[[32]byte]*x509.Certificate, len(v.originalCertificates)+1) +func (v *Verifier) certificateOriginals(leaf *x509.Certificate) map[[sha256.Size]byte]*x509.Certificate { + originals := make(map[[sha256.Size]byte]*x509.Certificate, len(v.originalCertificates)+1) maps.Copy(originals, v.originalCertificates) originals[sha256.Sum256(leaf.Raw)] = leaf @@ -542,10 +671,83 @@ func (v *Verifier) crlEvidence(ctx context.Context, e Evidence, sources map[evid return lists, nil } +// ChainDER returns owned copies of the verified certificate chain, starting with the leaf. +func (r *Result) ChainDER() [][]byte { + if r == nil { + return nil + } + + out := make([][]byte, len(r.chain)) + for i, b := range r.chain { + out[i] = bytes.Clone(b) + } + + return out +} + +// PublicKey returns a newly parsed public key owned by the caller. +func (r *Result) PublicKey() (crypto.PublicKey, error) { + if r == nil { + return nil, ErrCertificate + } + + return x509.ParsePKIXPublicKey(bytes.Clone(r.spki)) +} + +// VerificationTime returns the explicit time used for verification. +func (r *Result) VerificationTime() time.Time { + if r == nil { + return time.Time{} + } + + return r.at +} + +// RevocationStatus returns the verified revocation outcome. +func (r *Result) RevocationStatus() Status { + if r == nil { + return NotChecked + } + + return r.revocation +} + +// FingerprintSHA256 returns the hexadecimal SHA-256 fingerprint of the pinned SPKI. +func (p *KeyPin) FingerprintSHA256() string { + if p == nil { + return "" + } + + return hex.EncodeToString(p.fingerprint[:]) +} + +// MatchCertificate matches the certificate SPKI to the pin; it does not check PKIX trust, +// validity or revocation. +func (p *KeyPin) MatchCertificate(der []byte) error { + if p == nil { + return ErrPolicy + } + + c, err := parseBorrowedCertificate(der) + if err != nil { + return err + } + + if subtle.ConstantTimeCompare(p.spki, c.RawSubjectPublicKeyInfo) != 1 { + return fmt.Errorf("%w: public key does not match pin", ErrCertificate) + } + + return nil +} + // normalizeVerifierPolicy validates policy fields in their public error order, // filling defaults only in the caller's local configuration snapshot. func normalizeVerifierPolicy(c *Config) error { - if len(c.Roots) < 1 || len(c.Roots) > 64 || len(c.Intermediates) > 64 { + const supportedKeyUsage = x509.KeyUsageDigitalSignature | x509.KeyUsageContentCommitment | + x509.KeyUsageKeyEncipherment | x509.KeyUsageDataEncipherment | x509.KeyUsageKeyAgreement | + x509.KeyUsageCertSign | x509.KeyUsageCRLSign | x509.KeyUsageEncipherOnly | x509.KeyUsageDecipherOnly + + if len(c.Roots) < 1 || len(c.Roots) > maxRootCertificates || len(c.Intermediates) > maxIntermediateCertificates { return fmt.Errorf("%w: explicit bounded root/intermediate sets required", ErrPolicy) } @@ -557,35 +759,35 @@ func normalizeVerifierPolicy(c *Config) error { c.RequiredKeyUsage = x509.KeyUsageDigitalSignature } - if c.RequiredKeyUsage < 0 || c.RequiredKeyUsage & ^x509.KeyUsage(511) != 0 { + if c.RequiredKeyUsage < 0 || c.RequiredKeyUsage & ^supportedKeyUsage != 0 { return fmt.Errorf("%w: key usage", ErrPolicy) } if c.MaxChainDepth == 0 { - c.MaxChainDepth = 10 + c.MaxChainDepth = defaultMaxChainDepth } if c.MaxEvidenceBytes == 0 { - c.MaxEvidenceBytes = 8 << 20 + c.MaxEvidenceBytes = defaultMaxEvidenceBytes } if c.MaxCRLs == 0 { - c.MaxCRLs = 32 + c.MaxCRLs = defaultMaxCRLs } - if c.MaxChainDepth < 2 || c.MaxChainDepth > 32 || c.MaxEvidenceBytes < 1 || c.MaxEvidenceBytes > 64<<20 || c.MaxCRLs < 1 || c.MaxCRLs > 256 { + if c.MaxChainDepth < minimumChainDepth || c.MaxChainDepth > maximumChainDepth || c.MaxEvidenceBytes < 1 || c.MaxEvidenceBytes > maximumEvidenceBytes || c.MaxCRLs < 1 || c.MaxCRLs > maximumCRLs { return fmt.Errorf("%w: limits", ErrPolicy) } if c.MaxOCSPResponses == 0 { - c.MaxOCSPResponses = 32 + c.MaxOCSPResponses = defaultMaxOCSPResponses } - if c.MaxOCSPResponses < 1 || c.MaxOCSPResponses > 256 { + if c.MaxOCSPResponses < 1 || c.MaxOCSPResponses > maximumOCSPResponses { return ErrPolicy } - if len(c.OCSPAuthorities) > 8 { + if len(c.OCSPAuthorities) > maxOCSPAuthorities { return ErrPolicy } @@ -615,142 +817,20 @@ func normalizeVerifierPolicy(c *Config) error { } func parseCertificate(der []byte) (*x509.Certificate, error) { - if len(der) < 1 || len(der) > 1<<20 { + if len(der) < 1 || len(der) > certificateDERBytes { return nil, fmt.Errorf("%w: certificate size", ErrCertificate) } return parseBorrowedCertificate(bytes.Clone(der)) } -// Report contains certificate path and revocation diagnostics. Successful verification -// also returns a Result. -type Report struct { - evidence []*VerifiedEvidence - // Chain records the certificate path check outcome. - Chain Status `json:"chain"` - // Revocation records the evidence-check outcome; NoRevocation leaves it NotChecked. - Revocation Status `json:"revocation"` - // VerificationTime is the explicit time supplied for certificate and revocation checks. - VerificationTime time.Time `json:"verification_time"` - // ChainLength is the number of certificates in the accepted path, including its trust - // anchor. - ChainLength int `json:"chain_length,omitempty"` - // Error describes the verification failure, or is empty on success. - Error string `json:"error,omitempty"` -} - -// Result records acceptance under the configured certificate and revocation policy. -// It does not establish acceptance under other trust policies or the historical -// validation requirements of AdES. -type Result struct { - chain [][]byte - spki []byte - at time.Time - revocation Status - evidence []*VerifiedEvidence -} - -// ChainDER returns owned copies of the verified certificate chain, starting with the leaf. -func (r *Result) ChainDER() [][]byte { - if r == nil { - return nil - } - - out := make([][]byte, len(r.chain)) - for i, b := range r.chain { - out[i] = bytes.Clone(b) - } - - return out -} - -// PublicKey returns a newly parsed public key owned by the caller. -func (r *Result) PublicKey() (crypto.PublicKey, error) { - if r == nil { - return nil, ErrCertificate - } - - return x509.ParsePKIXPublicKey(bytes.Clone(r.spki)) -} - -// VerificationTime returns the explicit time used for verification. -func (r *Result) VerificationTime() time.Time { - if r == nil { - return time.Time{} - } - - return r.at -} - -// RevocationStatus returns the verified revocation outcome. -func (r *Result) RevocationStatus() Status { - if r == nil { - return NotChecked - } - - return r.revocation -} - -// KeyPin matches SubjectPublicKeyInfo against an explicitly trusted public key. -// Matching does not check certificate dates, CA paths, revocation or subject names. -// Use this trust primitive when the application already trusts the key, such as -// for a configured metadata signer. -type KeyPin struct { - spki []byte - fingerprint [32]byte -} - -// NewKeyPin snapshots a public key as an SPKI pin. Matching a pin does not establish PKIX -// certificate trust. -func NewKeyPin(public crypto.PublicKey) (*KeyPin, error) { - // Marshal may reject nil/unsupported types; require round-trip parse as well. - der, err := safeMarshalPublic(public) - if err != nil { - return nil, err - } - - if _, err = x509.ParsePKIXPublicKey(der); err != nil { - return nil, err - } - - return &KeyPin{spki: der, fingerprint: sha256.Sum256(der)}, nil -} - -// FingerprintSHA256 returns the hexadecimal SHA-256 fingerprint of the pinned SPKI. -func (p *KeyPin) FingerprintSHA256() string { - if p == nil { - return "" - } - - return hex.EncodeToString(p.fingerprint[:]) -} - -// MatchCertificate matches the certificate SPKI to the pin; it does not check PKIX trust, -// validity or revocation. -func (p *KeyPin) MatchCertificate(der []byte) error { - if p == nil { - return ErrPolicy - } - - c, err := parseBorrowedCertificate(der) - if err != nil { - return err - } - - if subtle.ConstantTimeCompare(p.spki, c.RawSubjectPublicKeyInfo) != 1 { - return fmt.Errorf("%w: public key does not match pin", ErrCertificate) - } - - return nil -} - // parseBorrowedCertificate is for temporary use or DER that is already owned. func parseBorrowedCertificate(der []byte) (*x509.Certificate, error) { - if len(der) < 1 || len(der) > 1<<20 { + if len(der) < 1 || len(der) > certificateDERBytes { return nil, fmt.Errorf("%w: certificate size", ErrCertificate) } - c, err := x509cert.Parse(der, x509cert.Limits{Bytes: 1 << 20, Depth: 33, Nodes: 100000}) + c, err := x509cert.Parse(der, x509cert.Limits{Bytes: certificateDERBytes, Depth: certificateASN1Depth, Nodes: certificateASN1Nodes}) if errors.Is(err, derpkg.ErrLimit) { return nil, fmt.Errorf("%w: %w: %w", ErrPolicy, ErrLimit, err) } diff --git a/pki/pki_test.go b/pki/pki_test.go index 51c54ac..0c977b1 100644 --- a/pki/pki_test.go +++ b/pki/pki_test.go @@ -16,6 +16,7 @@ import ( "testing" "time" + "github.com/skarm/xmlsec/evidence" "github.com/skarm/xmlsec/internal/testutil/testcert" "github.com/skarm/xmlsec/pki" ) @@ -252,6 +253,67 @@ func BenchmarkVerifyCRL(b *testing.B) { } } +// BenchmarkVerifyLargeCRL includes strict DER parsing, duplicate detection and +// all authenticated lookup passes; setup and CRL signing are outside the timer. +func BenchmarkVerifyLargeCRL(b *testing.B) { + for _, count := range []int{1, 1024, 65536} { + b.Run(strconv.Itoa(count), func(b *testing.B) { + c := testcert.New(b) + revoked := make([]*x509.Certificate, count) + for i := range revoked { + revoked[i] = &x509.Certificate{SerialNumber: big.NewInt(int64(i + 1000))} + } + raw := testcert.CRL(b, c, revoked...) + v, err := pki.NewVerifier(pki.Config{Roots: [][]byte{c.Root.Raw}, Revocation: pki.RequireCRL}) + if err != nil { + b.Fatal(err) + } + e := pki.Evidence{CRLs: [][]byte{raw}} + b.ReportAllocs() + b.SetBytes(int64(len(raw))) + for b.Loop() { + if _, _, err := v.Verify(b.Context(), c.Leaf.Raw, testcert.Now(), e); err != nil { + b.Fatal(err) + } + } + }) + } +} + +func BenchmarkVerifyMalformedLargeCRL(b *testing.B) { + for _, count := range []int{1024, 65536} { + b.Run(strconv.Itoa(count), func(b *testing.B) { + c := testcert.New(b) + for _, early := range []bool{true, false} { + b.Run(strconv.FormatBool(early), func(b *testing.B) { + revoked := make([]*x509.Certificate, count) + for i := range revoked { + revoked[i] = &x509.Certificate{SerialNumber: big.NewInt(int64(i + 1000))} + } + if early { + revoked[1] = revoked[0] + } else { + revoked[count-1] = revoked[0] + } + raw := testcert.CRL(b, c, revoked...) + verifier, err := pki.NewVerifier(pki.Config{Roots: [][]byte{c.Root.Raw}, Revocation: pki.RequireCRL}) + if err != nil { + b.Fatal(err) + } + input := pki.Evidence{CRLs: [][]byte{raw}} + b.ReportAllocs() + b.SetBytes(int64(len(raw))) + for b.Loop() { + if result, _, err := verifier.Verify(b.Context(), c.Leaf.Raw, testcert.Now(), input); result != nil || !errors.Is(err, pki.ErrEvidence) { + b.Fatal(result, err) + } + } + }) + } + }) + } +} + func verificationTime() time.Time { return time.Date(2026, 10, 8, 12, 0, 0, 0, time.UTC) } type chainData struct { @@ -320,63 +382,57 @@ func makeCRL(t *testing.T, c chainData, revoked bool, template *x509.RevocationL return der } -// BenchmarkVerifyLargeCRL includes strict DER parsing, duplicate detection and -// all authenticated lookup passes; setup and CRL signing are outside the timer. -func BenchmarkVerifyLargeCRL(b *testing.B) { - for _, count := range []int{1, 1024, 65536} { - b.Run(strconv.Itoa(count), func(b *testing.B) { - c := testcert.New(b) - revoked := make([]*x509.Certificate, count) - for i := range revoked { - revoked[i] = &x509.Certificate{SerialNumber: big.NewInt(int64(i + 1000))} - } - raw := testcert.CRL(b, c, revoked...) - v, err := pki.NewVerifier(pki.Config{Roots: [][]byte{c.Root.Raw}, Revocation: pki.RequireCRL}) - if err != nil { - b.Fatal(err) - } - e := pki.Evidence{CRLs: [][]byte{raw}} - b.ReportAllocs() - b.SetBytes(int64(len(raw))) - for b.Loop() { - if _, _, err := v.Verify(b.Context(), c.Leaf.Raw, testcert.Now(), e); err != nil { - b.Fatal(err) +func TestCheckEvidenceLimits(t *testing.T) { + chain := testcert.New(t) + verifier, err := pki.NewVerifier(pki.Config{Roots: [][]byte{chain.Root.Raw}, Revocation: pki.RequireCRL, MaxEvidenceBytes: 4096, MaxCRLs: 2, MaxOCSPResponses: 3}) + if err != nil { + t.Fatal(err) + } + for _, tc := range []struct { + name string + input pki.Evidence + over bool + }{ + {name: "empty"}, + {name: "intermediates64", input: pki.Evidence{Intermediates: make([][]byte, 64)}}, + {name: "intermediates65", input: pki.Evidence{Intermediates: make([][]byte, 65)}, over: true}, + {name: "CRLs2", input: pki.Evidence{CRLs: make([][]byte, 2)}}, + {name: "CRLs3", input: pki.Evidence{CRLs: make([][]byte, 3)}, over: true}, + {name: "OCSP3", input: pki.Evidence{OCSPResponses: make([][]byte, 3)}}, + {name: "OCSP4", input: pki.Evidence{OCSPResponses: make([][]byte, 4)}, over: true}, + {name: "aggregate exact", input: pki.Evidence{Intermediates: [][]byte{make([]byte, 1024)}, CRLs: [][]byte{make([]byte, 1024)}, OCSPResponses: [][]byte{make([]byte, 2048)}}}, + {name: "aggregate over", input: pki.Evidence{Intermediates: [][]byte{make([]byte, 1024)}, CRLs: [][]byte{make([]byte, 1024)}, OCSPResponses: [][]byte{make([]byte, 2049)}}, over: true}, + {name: "no DER or Sources authentication", input: pki.Evidence{Intermediates: [][]byte{[]byte("not a certificate")}, Sources: make([]*evidence.Observation, 1025)}}, + } { + t.Run(tc.name, func(t *testing.T) { + err := verifier.CheckEvidenceLimits(t.Context(), tc.input) + if tc.over { + if !errors.Is(err, pki.ErrLimit) || !errors.Is(err, pki.ErrPolicy) { + t.Fatal("limit identity", err) } + } else if err != nil { + t.Fatal(err) } }) } -} - -func BenchmarkVerifyMalformedLargeCRL(b *testing.B) { - for _, count := range []int{1024, 65536} { - b.Run(strconv.Itoa(count), func(b *testing.B) { - c := testcert.New(b) - for _, early := range []bool{true, false} { - b.Run(strconv.FormatBool(early), func(b *testing.B) { - revoked := make([]*x509.Certificate, count) - for i := range revoked { - revoked[i] = &x509.Certificate{SerialNumber: big.NewInt(int64(i + 1000))} - } - if early { - revoked[1] = revoked[0] - } else { - revoked[count-1] = revoked[0] - } - raw := testcert.CRL(b, c, revoked...) - verifier, err := pki.NewVerifier(pki.Config{Roots: [][]byte{c.Root.Raw}, Revocation: pki.RequireCRL}) - if err != nil { - b.Fatal(err) - } - input := pki.Evidence{CRLs: [][]byte{raw}} - b.ReportAllocs() - b.SetBytes(int64(len(raw))) - for b.Loop() { - if result, _, err := verifier.Verify(b.Context(), c.Leaf.Raw, testcert.Now(), input); result != nil || !errors.Is(err, pki.ErrEvidence) { - b.Fatal(result, err) - } - } - }) - } - }) + for _, v := range []*pki.Verifier{nil, {}} { + if err := v.CheckEvidenceLimits(t.Context(), pki.Evidence{}); !errors.Is(err, pki.ErrPolicy) || errors.Is(err, pki.ErrLimit) { + t.Fatal("invalid verifier", err) + } + } + if err := verifier.CheckEvidenceLimits(nil, pki.Evidence{}); !errors.Is(err, pki.ErrPolicy) { + t.Fatal("nil context", err) + } + ctx, cancel := context.WithCancel(t.Context()) + cancel() + if err := verifier.CheckEvidenceLimits(ctx, pki.Evidence{Intermediates: make([][]byte, 65)}); !errors.Is(err, context.Canceled) || errors.Is(err, pki.ErrLimit) { + t.Fatal("cancellation cause", err) + } + over := pki.Evidence{Intermediates: make([][]byte, 65)} + if result, _, err := verifier.Verify(t.Context(), chain.Leaf.Raw, testcert.Now(), over); result != nil || !errors.Is(err, pki.ErrPolicy) || errors.Is(err, pki.ErrLimit) { + t.Fatal("ordinary Verify identity changed", result, err) + } + if session, err := verifier.NewHistoricalSession(t.Context(), pki.HistoricalOptions{}, testcert.Now(), over); session != nil || !errors.Is(err, pki.ErrLimit) || !errors.Is(err, pki.ErrPolicy) { + t.Fatal("historical session identity changed", session, err) } } diff --git a/pki/standards_test.go b/pki/standards_test.go index 797b12f..562e196 100644 --- a/pki/standards_test.go +++ b/pki/standards_test.go @@ -134,6 +134,8 @@ func TestOCSPResponseStatusEnumeration(t *testing.T) { } result, report, err := pki.VerifyOCSP(t.Context(), der.Seq(encoded), c.Leaf.Raw, c.Root.Raw, testcert.Now(), pki.OCSPOptions{}) + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if result != nil || !errors.Is(err, want) || report.Signature != pki.NotChecked { t.Fatalf("status %d: result=%v, report=%v, error=%v; want %v", status, result != nil, report, err, want) } @@ -229,6 +231,8 @@ func TestCRLNumberConflictsAreOrderIndependent(t *testing.T) { {rollbackNumber, latest}, } { result, report, err := v.Verify(t.Context(), c.Leaf.Raw, testcert.Now(), pki.Evidence{CRLs: crls}) + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if result != nil || !errors.Is(err, pki.ErrEvidence) || report.Revocation != pki.Indeterminate { t.Fatalf("conflicting authenticated evidence: result=%v report=%v error=%v", result != nil, report, err) } @@ -354,6 +358,8 @@ func TestOCSPRequiresPKIXTimeEncoding(t *testing.T) { t.Fatal(err) } + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if result, report, err := pki.VerifyOCSP(t.Context(), raw, c.Leaf.Raw, c.Root.Raw, testcert.Now(), pki.OCSPOptions{}); result != nil || !errors.Is(err, pki.ErrRevoked) || report.Signature != pki.Valid { t.Fatal("invalid authenticated revoked control", err) } @@ -477,6 +483,8 @@ func TestPKIZeroVerifierRejectsBeforePathBuilding(t *testing.T) { c := testcert.New(t) result, report, err := new(pki.Verifier).Verify(t.Context(), c.Leaf.Raw, testcert.Now(), pki.Evidence{}) + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if result != nil || !errors.Is(err, pki.ErrPolicy) || report.Chain != pki.NotChecked { t.Fatalf("result=%v report=%v error=%v", result, report, err) } @@ -514,12 +522,12 @@ func TestCRLV1Compatibility(t *testing.T) { t.Fatal(err) } bits, _ := asn1.Marshal(asn1.BitString{Bytes: signed, BitLength: len(signed) * 8}) - evidence := der.Seq(raw, fields[1].FullBytes, bits) + e := der.Seq(raw, fields[1].FullBytes, bits) verifier, err := pki.NewVerifier(pki.Config{Roots: [][]byte{c.root.Raw}, Revocation: pki.RequireCRL}) if err != nil { t.Fatal(err) } - if result, report, err := verifier.Verify(t.Context(), c.leaf.Raw, verificationTime(), pki.Evidence{CRLs: [][]byte{evidence}}); err != nil || result == nil || report.Revocation != pki.Valid { + if result, report, err := verifier.Verify(t.Context(), c.leaf.Raw, verificationTime(), pki.Evidence{CRLs: [][]byte{e}}); err != nil || result == nil || report.Revocation != pki.Valid { t.Fatal("result", result != nil, report, err) } } @@ -566,18 +574,6 @@ func TestOCSPUnknownNoncriticalRequestExtensions(t *testing.T) { } } -func p6CRL(t *testing.T, c chainData, number int64, this, next time.Time, base int64, scope []byte, entries []x509.RevocationListEntry) []byte { - t.Helper() - template := &x509.RevocationList{Number: big.NewInt(number), ThisUpdate: this, NextUpdate: next, RevokedCertificateEntries: entries} - if base >= 0 { - template.ExtraExtensions = append(template.ExtraExtensions, pkix.Extension{Id: asn1.ObjectIdentifier{2, 5, 29, 27}, Critical: true, Value: der.Int(int(base))}) - } - if scope != nil { - template.ExtraExtensions = append(template.ExtraExtensions, pkix.Extension{Id: asn1.ObjectIdentifier{2, 5, 29, 28}, Critical: true, Value: scope}) - } - return makeCRL(t, c, false, template) -} - func TestCRLDeltaComposition(t *testing.T) { c := makeChain(t) at := verificationTime() @@ -644,44 +640,6 @@ func TestCRLDeltaComposition(t *testing.T) { } } -func p6PointName(uri string) []byte { - return der.Ctx(0, der.Wrap(2, 0, true, der.Wrap(2, 6, false, []byte(uri)))) -} - -func p6Reasons(mask uint16) []byte { - bits := asn1.BitString{} - for index := 1; index <= 8; index++ { - if mask&(1<<(index-1)) != 0 { - bits.BitLength = index + 1 - } - } - bits.Bytes = make([]byte, (bits.BitLength+7)/8) - for index := 1; index <= 8; index++ { - if mask&(1<<(index-1)) != 0 { - bits.Bytes[index/8] |= 1 << (7 - index%8) - } - } - encoded, _ := asn1.Marshal(bits) - var value asn1.RawValue - _, _ = asn1.Unmarshal(encoded, &value) - return value.Bytes -} - -func p6LeafExtensions(t *testing.T, c chainData, extra []pkix.Extension) *x509.Certificate { - t.Helper() - template := *c.leaf - template.ExtraExtensions = extra - encoded, err := x509.CreateCertificate(rand.Reader, &template, c.root, c.leafKey.Public(), c.rootKey) - if err != nil { - t.Fatal(err) - } - leaf, err := keys.ParseCertificate(encoded, keys.Limits{}) - if err != nil { - t.Fatal(err) - } - return leaf -} - func TestCRLScopedReasonCoverage(t *testing.T) { c := makeChain(t) at := verificationTime() @@ -786,11 +744,11 @@ func TestCRLIndirectIssuerAndEntryBinding(t *testing.T) { if err != nil { t.Fatal(err) } - evidence := pki.Evidence{CRLs: [][]byte{test.raw}} + e := pki.Evidence{CRLs: [][]byte{test.raw}} if test.candidates { - evidence.Intermediates = [][]byte{signer.Raw} + e.Intermediates = [][]byte{signer.Raw} } - result, report, err := verifier.Verify(t.Context(), leaf.Raw, at, evidence) + result, report, err := verifier.Verify(t.Context(), leaf.Raw, at, e) if test.want != nil { if result != nil || !errors.Is(err, test.want) { t.Fatal("result", result != nil, report, err) @@ -1003,6 +961,8 @@ func TestOCSPSingleResponseExtensionSchema(t *testing.T) { raw := p7SingleExtension(t, base, c, tc.ext) result, report, err := pki.VerifyOCSP(t.Context(), raw, c.leaf.Raw, c.root.Raw, at, pki.OCSPOptions{}) if tc.want != nil { + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if result != nil || !errors.Is(err, tc.want) || report.Signature != pki.NotChecked { t.Fatalf("single-response extension: %v, %+v, %v; want %v", result != nil, report, err, tc.want) } @@ -1013,61 +973,6 @@ func TestOCSPSingleResponseExtensionSchema(t *testing.T) { } } -func p6ReplaceCRLExtension(t *testing.T, raw []byte, c chainData, replacement pkix.Extension) []byte { - t.Helper() - value, err := der.Read(raw, 1<<20) - if err != nil { - t.Fatal(err) - } - fields, err := der.Sequence(value, 3, 3) - if err != nil { - t.Fatal(err) - } - tbs, err := der.Sequence(fields[0], 5, 7) - if err != nil { - t.Fatal(err) - } - ext, err := der.Explicit(tbs[len(tbs)-1], 0) - if err != nil { - t.Fatal(err) - } - old, err := der.Sequence(ext, 1, 32) - if err != nil { - t.Fatal(err) - } - var extensions [][]byte - for _, item := range old { - var parsed pkix.Extension - if der.Decode(item, &parsed) != nil { - t.Fatal("extension shape") - } - if !parsed.Id.Equal(replacement.Id) { - extensions = append(extensions, item.FullBytes) - } - } - encoded, err := asn1.Marshal(replacement) - if err != nil { - t.Fatal(err) - } - extensions = append(extensions, encoded) - tbs[len(tbs)-1].FullBytes = der.Ctx(0, der.Seq(extensions...)) - parts := make([][]byte, 0, len(tbs)) - for _, part := range tbs { - parts = append(parts, part.FullBytes) - } - data := der.Seq(parts...) - algorithm, err := signature.Algorithm(fields[1]) - if err != nil { - t.Fatal(err) - } - signed, err := signature.Sign(t.Context(), c.rootKey, algorithm, data) - if err != nil { - t.Fatal(err) - } - bits, _ := asn1.Marshal(asn1.BitString{Bytes: signed, BitLength: len(signed) * 8}) - return der.Seq(data, fields[1].FullBytes, bits) -} - func TestVerifiedCRLEvidenceSources(t *testing.T) { c := makeChain(t) at := verificationTime() @@ -1133,6 +1038,8 @@ func TestVerifiedCRLEvidenceSources(t *testing.T) { } expired := makeCRL(t, c, false, &x509.RevocationList{Number: big.NewInt(1), ThisUpdate: at.Add(-2 * time.Hour), NextUpdate: at.Add(-time.Hour)}) result, report, err := verifier.Verify(t.Context(), c.leaf.Raw, at, pki.Evidence{CRLs: [][]byte{expired}}) + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if result != nil || !errors.Is(err, pki.ErrUnavailable) || len(report.Evidence()) != 1 || report.Evidence()[0].AuthenticationStatus() != pki.Valid { t.Fatal("stale authenticated assertion became trust", result != nil, err, len(report.Evidence())) } @@ -1190,33 +1097,6 @@ func TestPKIXSemanticNamesPreserveOriginalDER(t *testing.T) { } } -func p6TransformCRL(t *testing.T, raw []byte, c chainData, edit func([]asn1.RawValue) [][]byte) []byte { - t.Helper() - value, err := der.Read(raw, 1<<20) - if err != nil { - t.Fatal(err) - } - fields, err := der.Sequence(value, 3, 3) - if err != nil { - t.Fatal(err) - } - body, err := der.Sequence(fields[0], 3, 7) - if err != nil { - t.Fatal(err) - } - data := der.Seq(edit(body)...) - algorithm, err := signature.Algorithm(fields[1]) - if err != nil { - t.Fatal(err) - } - signed, err := signature.Sign(t.Context(), c.rootKey, algorithm, data) - if err != nil { - t.Fatal(err) - } - bits, _ := asn1.Marshal(asn1.BitString{Bytes: signed, BitLength: len(signed) * 8}) - return der.Seq(data, fields[1].FullBytes, bits) -} - func TestCRLTimeAndPolicyBoundaries(t *testing.T) { c := makeChain(t) at := verificationTime() @@ -1731,20 +1611,6 @@ func TestCRLRejectsInvalidURIs(t *testing.T) { } } -func p7Proofs(t *testing.T, now time.Time, objects [][]byte, roles []string, at time.Time) *historicalproof.Set { - t.Helper() - source := sha256.Sum256([]byte("published historical building-block vector")) - facts := make([]historicalproof.Fact, 0, len(objects)) - for index, object := range objects { - facts = append(facts, historicalproof.Fact{Digest: sha256.Sum256(object), Source: source, At: at, Kind: historicalproof.Kind(roles[index])}) - } - set, err := historicalproof.New(t.Context(), now, facts) - if err != nil { - t.Fatal(err) - } - return set -} - func TestHistoricalExpiredTargetAuthentication(t *testing.T) { c := makeChain(t) now := verificationTime().Add(3 * time.Hour) @@ -1825,6 +1691,8 @@ func TestHistoricalCertificateTimeSliding(t *testing.T) { proofs := p7Proofs(t, now, test.objects, test.roles, test.proofAt) result, report, err := session.ValidatePast(t.Context(), c.leaf.Raw, proofs) if !test.pass { + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if result != nil || !errors.Is(err, pki.ErrUnavailable) || report.SubIndication() != "NO_POE" { t.Fatal(result != nil, report.SubIndication(), err) } @@ -2186,7 +2054,9 @@ func TestHistoricalNoCheckCertificateStillNeedsPreCutoffProof(t *testing.T) { } result, report, err := session.ValidatePast(t.Context(), c.leaf.Raw, proofs) if tc.pass { - if result == nil || err != nil || !result.ValidationTime().Equal(cutoff) { + if err != nil { + t.Fatal(err) + } else if result == nil || !result.ValidationTime().Equal(cutoff) { t.Fatal("pre-cutoff proof did not protect responder certificate", result != nil, report.Transitions(), err) } found := false @@ -2232,7 +2102,9 @@ func TestHistoricalCryptoAndSunsetProofs(t *testing.T) { } result, report, err := session.ValidatePast(t.Context(), c.leaf.Raw, p7Proofs(t, now, [][]byte{c.leaf.Raw, raw}, []string{"certificate", "crl"}, tc.proofAt)) if tc.pass { - if err != nil || result == nil || !result.ValidationTime().Equal(tc.want) || len(report.Transitions()) != 1 { + if err != nil { + t.Fatal(err) + } else if result == nil || !result.ValidationTime().Equal(tc.want) || len(report.Transitions()) != 1 { t.Fatal(result != nil, report.Transitions(), err) } } else if result != nil || !errors.Is(err, pki.ErrUnavailable) { @@ -2242,19 +2114,6 @@ func TestHistoricalCryptoAndSunsetProofs(t *testing.T) { } } -func p7Response(t *testing.T, c chainData, template pki.OCSPResponseTemplate) []byte { - t.Helper() - request, err := pki.CreateOCSPRequest(c.leaf.Raw, c.root.Raw, crypto.SHA256, nil) - if err != nil { - t.Fatal(err) - } - raw, err := pki.CreateOCSPResponse(t.Context(), request, c.leaf.Raw, c.root.Raw, c.root.Raw, c.rootKey, template) - if err != nil { - t.Fatal(err) - } - return raw -} - func TestHistoricalOCSPAndConflictingSources(t *testing.T) { c := makeChain(t) now := verificationTime() @@ -2327,6 +2186,8 @@ func TestHistoricalOCSPAndConflictingSources(t *testing.T) { t.Fatal("both conflicting signatures must authenticate", len(facts), err) } proofs := p7Proofs(t, now, [][]byte{c.leaf.Raw, latest, conflict}, []string{"certificate", "ocsp", "ocsp"}, now) + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if result, report, err := session.ValidatePast(t.Context(), c.leaf.Raw, proofs); result != nil || !errors.Is(err, pki.ErrEvidence) || report.SubIndication() != "NO_POE" { t.Fatal("input order resolved contradictory authenticated status", reversed, result != nil, report.SubIndication(), err) } @@ -2334,27 +2195,6 @@ func TestHistoricalOCSPAndConflictingSources(t *testing.T) { }) } -func p7SingleExtension(t *testing.T, raw []byte, c chainData, extension pkix.Extension) []byte { - t.Helper() - encoded, err := asn1.Marshal(extension) - if err != nil { - t.Fatal(err) - } - return rewriteOCSPData(t, raw, c.rootKey, func(fields []asn1.RawValue) [][]byte { - responses, err := der.Sequence(fields[2], 1, 64) - if err != nil { - t.Fatal(err) - } - single, err := der.Sequence(responses[0], 3, 5) - if err != nil { - t.Fatal(err) - } - responses[0].FullBytes = der.Seq(append(ocspFieldBytes(single), der.Ctx(1, der.Seq(encoded)))...) - fields[2].FullBytes = der.Seq(ocspFieldBytes(responses)...) - return ocspFieldBytes(fields) - }) -} - func TestHistoricalExpiredRetention(t *testing.T) { c := makeChain(t) now := verificationTime().Add(3 * time.Hour) @@ -2777,6 +2617,8 @@ func TestTSAAlternativeChainsApplyRevocationToExactCertificates(t *testing.T) { } stamp, stampErr := tsa.VerifyResponse(t.Context(), response, request, now, proof) if tc.tsaRevoked || tc.first && tc.second { + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if result != nil || stamp != nil || !errors.Is(err, pki.ErrRevoked) || !errors.Is(stampErr, pki.ErrRevoked) || report.Revocation != pki.Invalid { t.Fatal("alternative path bypassed revocation", reversed, result != nil, stamp != nil, report, err, stampErr) } @@ -2845,6 +2687,8 @@ func TestAlternativeChainsRespectDepthPolicy(t *testing.T) { } result, report, err := verifier.Verify(t.Context(), leaf.Raw, now, pki.Evidence{}) if tc.length == 0 { + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if result != nil || !errors.Is(err, pki.ErrPolicy) || report.Chain != pki.Invalid || report.Revocation != pki.NotChecked { t.Fatal("all candidate paths exceed the configured depth", result != nil, report, err) } @@ -2926,7 +2770,9 @@ func TestHistoricalDelegatedSourcePaths(t *testing.T) { } result, report, err := session.ValidatePast(t.Context(), c.leaf.Raw, p7Proofs(t, now, objects, roles, verificationTime())) if tc.pass { - if err != nil || result == nil || !result.ValidationTime().Equal(verificationTime().Add(-time.Minute)) { + if err != nil { + t.Fatal(err) + } else if result == nil || !result.ValidationTime().Equal(verificationTime().Add(-time.Minute)) { t.Fatal(result != nil, report.Transitions(), err) } } else if result != nil || !errors.Is(err, pki.ErrUnavailable) { @@ -3185,6 +3031,8 @@ func TestHistoricalOCSPAuthorizationCyclesAndDepth(t *testing.T) { } continue } + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if result != nil || !errors.Is(policyErr, tc.want) || !errors.Is(pastErr, tc.want) || sub != "REVOCATION_OUT_OF_BOUNDS_NO_POE" || report.SubIndication() != "NO_POE" { t.Fatal("cycle/depth failure returned acceptance or wrong cause", result != nil, sub, policyErr, pastErr) } @@ -3483,6 +3331,8 @@ func TestHistoricalDelegatedResponderRevocationAndProofs(t *testing.T) { if tc.targetRevoked { want = pki.ErrEvidence } + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if result != nil || !errors.Is(err, want) || report.SubIndication() != "NO_POE" { t.Fatal("borrowed response proof for a source dependency", result != nil, report.SubIndication(), err) } @@ -3492,7 +3342,9 @@ func TestHistoricalDelegatedResponderRevocationAndProofs(t *testing.T) { if tc.reason >= 0 { want = revokedAt } - if result == nil || err != nil || !result.ValidationTime().Equal(want) { + if err != nil { + t.Fatal(err) + } else if result == nil || !result.ValidationTime().Equal(want) { t.Fatal("wrong historical source-signer bound", result != nil, report.Transitions(), err) } if tc.reason >= 0 { @@ -3637,6 +3489,8 @@ func TestHistoricalSessionBoundsAndSnapshots(t *testing.T) { if err != nil { t.Fatal(err) } + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if result, report, err := empty.ValidatePast(t.Context(), foreignLeaf.Raw, p7Proofs(t, now, nil, nil, now)); result != nil || !errors.Is(err, pki.ErrUnavailable) || report.SubIndication() != "NO_CERTIFICATE_CHAIN_FOUND" { t.Fatal(result != nil, report.SubIndication(), err) } @@ -3987,11 +3841,17 @@ func TestHistoricalConstraintIndications(t *testing.T) { } result, report, err := session.ValidatePast(t.Context(), c.leaf.Raw, p7Proofs(t, now, [][]byte{c.leaf.Raw, raw}, []string{"certificate", "crl"}, now)) if tc.pass { - if err != nil || result == nil || report.SubIndication() != "" { + if err != nil { + t.Fatal(err) + } else if result == nil || report.SubIndication() != "" { + t.Fatal(result != nil, report.SubIndication(), err) + } + } else { + // Verification reports retain diagnostics on failure. + // noinspection GoDfaErrorMayBeNotNil + if result != nil || !errors.Is(err, pki.ErrCertificate) || report.SubIndication() != "CHAIN_CONSTRAINTS_FAILURE" { t.Fatal(result != nil, report.SubIndication(), err) } - } else if result != nil || !errors.Is(err, pki.ErrCertificate) || report.SubIndication() != "CHAIN_CONSTRAINTS_FAILURE" { - t.Fatal(result != nil, report.SubIndication(), err) } }) } @@ -4095,12 +3955,16 @@ func TestHistoricalFreshnessDependenciesAreTargetBound(t *testing.T) { } report, err := historical.Validate(t.Context(), config, selected, now, signerEvidence, pki.Evidence{CRLs: [][]byte{testcert.CRL(t, tsaChain)}}) if !archive { + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if err == nil || report.Indication == historical.Passed || report.SubIndication != "TRY_LATER" { t.Fatal("missing historical proof accepted", report.SubIndication, err) } return } - if err != nil || report.Indication != historical.Passed || !report.Validation.Equal(proofTime) { + if err != nil { + t.Fatal(err) + } else if report.Indication != historical.Passed || !report.Validation.Equal(proofTime) { t.Fatal("another target's delta dependency suppressed signer head", report.Indication, report.SubIndication, err) } leafHash := sha256.Sum256(leaf.Raw) @@ -4120,3 +3984,183 @@ func TestHistoricalFreshnessDependenciesAreTargetBound(t *testing.T) { }) } } + +func p6CRL(t *testing.T, c chainData, number int64, this, next time.Time, base int64, scope []byte, entries []x509.RevocationListEntry) []byte { + t.Helper() + template := &x509.RevocationList{Number: big.NewInt(number), ThisUpdate: this, NextUpdate: next, RevokedCertificateEntries: entries} + if base >= 0 { + template.ExtraExtensions = append(template.ExtraExtensions, pkix.Extension{Id: asn1.ObjectIdentifier{2, 5, 29, 27}, Critical: true, Value: der.Int(int(base))}) + } + if scope != nil { + template.ExtraExtensions = append(template.ExtraExtensions, pkix.Extension{Id: asn1.ObjectIdentifier{2, 5, 29, 28}, Critical: true, Value: scope}) + } + return makeCRL(t, c, false, template) +} + +func p6PointName(uri string) []byte { + return der.Ctx(0, der.Wrap(2, 0, true, der.Wrap(2, 6, false, []byte(uri)))) +} + +func p6Reasons(mask uint16) []byte { + bits := asn1.BitString{} + for index := 1; index <= 8; index++ { + if mask&(1<<(index-1)) != 0 { + bits.BitLength = index + 1 + } + } + bits.Bytes = make([]byte, (bits.BitLength+7)/8) + for index := 1; index <= 8; index++ { + if mask&(1<<(index-1)) != 0 { + bits.Bytes[index/8] |= 1 << (7 - index%8) + } + } + encoded, _ := asn1.Marshal(bits) + var value asn1.RawValue + _, _ = asn1.Unmarshal(encoded, &value) + return value.Bytes +} + +func p6LeafExtensions(t *testing.T, c chainData, extra []pkix.Extension) *x509.Certificate { + t.Helper() + template := *c.leaf + template.ExtraExtensions = extra + encoded, err := x509.CreateCertificate(rand.Reader, &template, c.root, c.leafKey.Public(), c.rootKey) + if err != nil { + t.Fatal(err) + } + leaf, err := keys.ParseCertificate(encoded, keys.Limits{}) + if err != nil { + t.Fatal(err) + } + return leaf +} + +func p6ReplaceCRLExtension(t *testing.T, raw []byte, c chainData, replacement pkix.Extension) []byte { + t.Helper() + value, err := der.Read(raw, 1<<20) + if err != nil { + t.Fatal(err) + } + fields, err := der.Sequence(value, 3, 3) + if err != nil { + t.Fatal(err) + } + tbs, err := der.Sequence(fields[0], 5, 7) + if err != nil { + t.Fatal(err) + } + ext, err := der.Explicit(tbs[len(tbs)-1], 0) + if err != nil { + t.Fatal(err) + } + old, err := der.Sequence(ext, 1, 32) + if err != nil { + t.Fatal(err) + } + var extensions [][]byte + for _, item := range old { + var parsed pkix.Extension + if der.Decode(item, &parsed) != nil { + t.Fatal("extension shape") + } + if !parsed.Id.Equal(replacement.Id) { + extensions = append(extensions, item.FullBytes) + } + } + encoded, err := asn1.Marshal(replacement) + if err != nil { + t.Fatal(err) + } + extensions = append(extensions, encoded) + tbs[len(tbs)-1].FullBytes = der.Ctx(0, der.Seq(extensions...)) + parts := make([][]byte, 0, len(tbs)) + for _, part := range tbs { + parts = append(parts, part.FullBytes) + } + data := der.Seq(parts...) + algorithm, err := signature.Algorithm(fields[1]) + if err != nil { + t.Fatal(err) + } + signed, err := signature.Sign(t.Context(), c.rootKey, algorithm, data) + if err != nil { + t.Fatal(err) + } + bits, _ := asn1.Marshal(asn1.BitString{Bytes: signed, BitLength: len(signed) * 8}) + return der.Seq(data, fields[1].FullBytes, bits) +} + +func p6TransformCRL(t *testing.T, raw []byte, c chainData, edit func([]asn1.RawValue) [][]byte) []byte { + t.Helper() + value, err := der.Read(raw, 1<<20) + if err != nil { + t.Fatal(err) + } + fields, err := der.Sequence(value, 3, 3) + if err != nil { + t.Fatal(err) + } + body, err := der.Sequence(fields[0], 3, 7) + if err != nil { + t.Fatal(err) + } + data := der.Seq(edit(body)...) + algorithm, err := signature.Algorithm(fields[1]) + if err != nil { + t.Fatal(err) + } + signed, err := signature.Sign(t.Context(), c.rootKey, algorithm, data) + if err != nil { + t.Fatal(err) + } + bits, _ := asn1.Marshal(asn1.BitString{Bytes: signed, BitLength: len(signed) * 8}) + return der.Seq(data, fields[1].FullBytes, bits) +} + +func p7Proofs(t *testing.T, now time.Time, objects [][]byte, roles []string, at time.Time) *historicalproof.Set { + t.Helper() + source := sha256.Sum256([]byte("published historical building-block vector")) + facts := make([]historicalproof.Fact, 0, len(objects)) + for index, object := range objects { + facts = append(facts, historicalproof.Fact{Digest: sha256.Sum256(object), Source: source, At: at, Kind: historicalproof.Kind(roles[index])}) + } + set, err := historicalproof.New(t.Context(), now, facts) + if err != nil { + t.Fatal(err) + } + return set +} + +func p7Response(t *testing.T, c chainData, template pki.OCSPResponseTemplate) []byte { + t.Helper() + request, err := pki.CreateOCSPRequest(c.leaf.Raw, c.root.Raw, crypto.SHA256, nil) + if err != nil { + t.Fatal(err) + } + raw, err := pki.CreateOCSPResponse(t.Context(), request, c.leaf.Raw, c.root.Raw, c.root.Raw, c.rootKey, template) + if err != nil { + t.Fatal(err) + } + return raw +} + +func p7SingleExtension(t *testing.T, raw []byte, c chainData, extension pkix.Extension) []byte { + t.Helper() + encoded, err := asn1.Marshal(extension) + if err != nil { + t.Fatal(err) + } + return rewriteOCSPData(t, raw, c.rootKey, func(fields []asn1.RawValue) [][]byte { + responses, err := der.Sequence(fields[2], 1, 64) + if err != nil { + t.Fatal(err) + } + single, err := der.Sequence(responses[0], 3, 5) + if err != nil { + t.Fatal(err) + } + responses[0].FullBytes = der.Seq(append(ocspFieldBytes(single), der.Ctx(1, der.Seq(encoded)))...) + fields[2].FullBytes = der.Seq(ocspFieldBytes(responses)...) + return ocspFieldBytes(fields) + }) +} diff --git a/reference/data.go b/reference/data.go index 1518361..968ba53 100644 --- a/reference/data.go +++ b/reference/data.go @@ -9,6 +9,12 @@ import ( "github.com/skarm/xmlsec/internal/lexicon" ) +const ( + invalidKindName = "invalid" + nodeSetKindName = "node-set" + octetStreamKindName = "octet-stream" +) + const ( // EnvelopedSignature is the XMLDSig enveloped-signature transform URI. EnvelopedSignature = lexicon.TransformEnvelopedSignature @@ -45,11 +51,11 @@ const ( func (k Kind) String() string { switch k { case OctetStream: - return "octet-stream" + return octetStreamKindName case NodeSet: - return "node-set" + return nodeSetKindName default: - return "invalid" + return invalidKindName } } @@ -67,11 +73,11 @@ func (k *Kind) UnmarshalText(text []byte) error { var value Kind switch string(text) { - case "invalid": + case invalidKindName: value = Invalid - case "octet-stream": + case octetStreamKindName: value = OctetStream - case "node-set": + case nodeSetKindName: value = NodeSet default: return fmt.Errorf("%w: unknown data kind", ErrInput) @@ -86,9 +92,12 @@ func (k *Kind) UnmarshalText(text []byte) error { // invalid, but an empty octet stream or initialized empty node set is valid. // Octets copies the supplied bytes. type Data struct { - kind Kind + // kind selects the active octet-stream or node-set representation. + kind Kind + // octets owns data for the octet-stream representation. octets []byte - nodes document.NodeSet + // nodes holds the immutable node-set representation. + nodes document.NodeSet } // Octets copies data into an immutable octet stream. @@ -163,11 +172,17 @@ type Step struct { // Report records a completed transform chain without retaining the final output bytes. type Report struct { - input Kind - size int - steps []Step - produced int - visits int + // input records the resolver's initial data representation. + input Kind + // size records the final output length in bytes. + size int + // steps records transform execution diagnostics in order. + steps []Step + // produced counts aggregate intermediate and final output bytes. + produced int + // visits counts aggregate node visits charged to processing. + visits int + // canonicalization identifies the final canonicalization algorithm, if applied. canonicalization string } @@ -227,8 +242,10 @@ func (o *Report) Canonicalization() string { // Output retains buffered transform bytes and their processing report. type Output struct { + // report retains immutable processing diagnostics. report *Report - bytes []byte + // bytes owns the final processed octets. + bytes []byte } // Bytes returns an owned copy of the buffered output. diff --git a/reference/extensions.go b/reference/extensions.go index 41352ce..25b676f 100644 --- a/reference/extensions.go +++ b/reference/extensions.go @@ -11,6 +11,21 @@ import ( "github.com/skarm/xmlsec/internal/tree" ) +const ( + // Filter2 operation names select how an expression changes the input node set. + filterIntersect = "intersect" + filterSubtract = "subtract" + filterUnion = "union" + + // These hard caps bound serialized programs and their static namespace context + // before approval callbacks; byte counts refer to UTF-8 source, not runtime output. + maximumNamespaceURIBytes = 4096 + maximumStylesheetBytes = 1 << 20 + maximumXPathBytes = 16 << 10 + maximumXPathNamespaces = 128 + maximumXPathParameters = 16 +) + const ( // XPath is the XMLDSig XPath 1.0 transform URI. XPath = "http://www.w3.org/TR/1999/REC-xpath-19991116" @@ -97,7 +112,7 @@ func ValidateTransformParameters(t Transform) error { return err } - if len(t.Stylesheet) != 0 || len(t.XPath) == 0 || len(t.XPath) > 16 || t.Algorithm == XPath && len(t.XPath) != 1 { + if len(t.Stylesheet) != 0 || len(t.XPath) == 0 || len(t.XPath) > maximumXPathParameters || t.Algorithm == XPath && len(t.XPath) != 1 { return fmt.Errorf("%w: XPath parameter count", ErrInput) } @@ -132,7 +147,14 @@ func PrepareTransforms(ctx context.Context, ts []Transform, opt Options) error { } func checkedStylesheet(ctx context.Context, t Transform) (*document.Document, error) { - if len(t.InclusivePrefixes) != 0 || len(t.XPath) != 0 || len(t.Stylesheet) == 0 || len(t.Stylesheet) > 1<<20 { + const ( + // The XMLDSig XSLT profile requires the stylesheet's unqualified version + // attribute, independently of an XML declaration or output serialization. + stylesheetVersionAttribute = "version" + stylesheetVersion10 = "1.0" + ) + + if len(t.InclusivePrefixes) != 0 || len(t.XPath) != 0 || len(t.Stylesheet) == 0 || len(t.Stylesheet) > maximumStylesheetBytes { return nil, fmt.Errorf("%w: stylesheet size/parameters", ErrInput) } @@ -142,12 +164,12 @@ func checkedStylesheet(ctx context.Context, t Transform) (*document.Document, er } name := doc.Root().Name() - if name.Space != lexicon.NamespaceXSLT || name.Local != "stylesheet" && name.Local != "transform" { + if name.Space != lexicon.NamespaceXSLT || name.Local != lexicon.XSLTElementStylesheet && name.Local != lexicon.XSLTElementTransform { return nil, fmt.Errorf("%w: XSLT stylesheet root", ErrInput) } - version, ok := doc.Root().Attribute(document.Name{Local: "version"}) - if !ok || version != "1.0" { + version, ok := doc.Root().Attribute(document.Name{Local: stylesheetVersionAttribute}) + if !ok || version != stylesheetVersion10 { return nil, fmt.Errorf("%w: XSLT 1.0 required", ErrUnsupported) } @@ -155,7 +177,7 @@ func checkedStylesheet(ctx context.Context, t Transform) (*document.Document, er } func validateTransformParametersNCName(p string, u string) error { - if p != "" && !tree.NCName(p) || p == "xmlns" || p == "xml" && u != tree.XMLNamespace || len(u) > 4096 || !utf8.ValidString(u) || p != "" && u == "" { + if p != "" && !tree.NCName(p) || p == lexicon.PrefixXMLNS || p == lexicon.PrefixXML && u != tree.XMLNamespace || len(u) > maximumNamespaceURIBytes || !utf8.ValidString(u) || p != "" && u == "" { return fmt.Errorf("%w: XPath namespace", ErrInput) } @@ -163,7 +185,7 @@ func validateTransformParametersNCName(p string, u string) error { } func validateTransformParametersAlgorithm(t *Transform, x *XPathExpression) error { - if t.Algorithm == XPath && x.Filter != "" || t.Algorithm == XPathFilter2 && x.Filter != "intersect" && x.Filter != "subtract" && x.Filter != "union" { + if t.Algorithm == XPath && x.Filter != "" || t.Algorithm == XPathFilter2 && x.Filter != filterIntersect && x.Filter != filterSubtract && x.Filter != filterUnion { return fmt.Errorf("%w: XPath filter operation", ErrInput) } @@ -172,7 +194,7 @@ func validateTransformParametersAlgorithm(t *Transform, x *XPathExpression) erro func validateTransformParametersXPath(t *Transform) error { for _, x := range t.XPath { - if len(x.Expression) == 0 || len(x.Expression) > 16<<10 || !utf8.ValidString(x.Expression) || len(x.Namespaces) > 128 { + if len(x.Expression) == 0 || len(x.Expression) > maximumXPathBytes || !utf8.ValidString(x.Expression) || len(x.Namespaces) > maximumXPathNamespaces { return fmt.Errorf("%w: XPath expression/context size", ErrLimit) } diff --git a/reference/extensions_test.go b/reference/extensions_test.go index ba864d6..07283d3 100644 --- a/reference/extensions_test.go +++ b/reference/extensions_test.go @@ -221,6 +221,23 @@ func TestProgramFingerprintMatchesJSONEncoding(t *testing.T) { } } +func TestCanonicalizedOutputRunsProgramOnce(t *testing.T) { + calls := 0 + engine := &extensionStub{apply: func(_ context.Context, _ reference.Transform, input reference.Data, _ reference.ExtensionContext) (reference.Data, error) { + calls++ + if calls > 1 { + return reference.Data{}, errors.New("program replayed") + } + return input, nil + }} + var original, alternate bytes.Buffer + source := `text` + report, err := reference.WriteReaderAndCanonicalize(t.Context(), &original, &alternate, strings.NewReader(source), []reference.Transform{xpathTrue()}, reference.Transform{Algorithm: c14n.Exclusive}, extensionOptions(engine)) + if err != nil || report == nil || calls != 1 || original.String() != source || alternate.String() != `text` { + t.Fatal(original.String(), alternate.String(), calls, err) + } +} + type extensionStub struct { validate func(context.Context, reference.Transform) error apply func(context.Context, reference.Transform, reference.Data, reference.ExtensionContext) (reference.Data, error) @@ -253,20 +270,3 @@ func extensionOptions(p reference.ExtensionProcessor) reference.Options { func xpathTrue() reference.Transform { return reference.Transform{Algorithm: reference.XPath, XPath: []reference.XPathExpression{{Expression: nameTrue}}} } - -func TestCanonicalizedOutputRunsProgramOnce(t *testing.T) { - calls := 0 - engine := &extensionStub{apply: func(_ context.Context, _ reference.Transform, input reference.Data, _ reference.ExtensionContext) (reference.Data, error) { - calls++ - if calls > 1 { - return reference.Data{}, errors.New("program replayed") - } - return input, nil - }} - var original, alternate bytes.Buffer - source := `text` - report, err := reference.WriteReaderAndCanonicalize(t.Context(), &original, &alternate, strings.NewReader(source), []reference.Transform{xpathTrue()}, reference.Transform{Algorithm: c14n.Exclusive}, extensionOptions(engine)) - if err != nil || report == nil || calls != 1 || original.String() != source || alternate.String() != `text` { - t.Fatal(original.String(), alternate.String(), calls, err) - } -} diff --git a/reference/fingerprint.go b/reference/fingerprint.go index 39f7ceb..c1de5d6 100644 --- a/reference/fingerprint.go +++ b/reference/fingerprint.go @@ -10,6 +10,7 @@ import ( "github.com/skarm/xmlsec/c14n" "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/lexicon" ) // ProgramFingerprint identifies exact approved extension parameters. Runtime Here @@ -33,7 +34,7 @@ func ProgramFingerprint(t Transform) (string, error) { for i := range t.XPath { t.XPath[i].Here = document.Element{} - t.XPath[i].Namespaces["xml"] = document.XMLNamespace + t.XPath[i].Namespaces[lexicon.PrefixXML] = document.XMLNamespace } b, err := json.Marshal(t) diff --git a/reference/pipeline.go b/reference/pipeline.go index 06810ea..606a27f 100644 --- a/reference/pipeline.go +++ b/reference/pipeline.go @@ -14,6 +14,10 @@ import ( "github.com/skarm/xmlsec/internal/tree" ) +const ( + stageDigestInput = "digest-input" +) + type transformPipeline struct { processor *processor ctx context.Context @@ -87,7 +91,7 @@ func (p *transformPipeline) run(dst io.Writer, transforms []Transform) (*Report, writer := streamio.Writer{Context: p.ctx, Target: target, Limit: int64(p.processor.opt.MaxOutputBytes), LimitError: ErrLimit} if _, err := io.Copy(&writer, p.reader); err != nil { - index, algorithm := -1, "digest-input" + index, algorithm := -1, stageDigestInput if len(transforms) > 0 { index = len(transforms) - 1 algorithm = transforms[index].Algorithm @@ -360,11 +364,17 @@ func (w *yieldWriter) Write(p []byte) (int, error) { } type producedReader struct { - ctx context.Context - next func() ([]byte, error, bool) - stop func() + // ctx supplies cancellation for pulls from the transform producer. + ctx context.Context + // next resumes the producer and borrows its next output chunk. + next func() ([]byte, error, bool) + // stop terminates the suspended producer and runs its deferred cleanup. + stop func() + // pending borrows the unread suffix of the last yielded chunk until the next + // pull. pending []byte - closed bool + // closed prevents further pulls and makes producer cleanup idempotent. + closed bool } func (r *producedReader) Read(p []byte) (int, error) { @@ -409,6 +419,8 @@ func (r *producedReader) Close() error { r.stop = nil r.next = nil + // Resume the suspended iterator's cleanup even when the consumer stops + // before EOF; its deferred resource releases must still run. stop() } diff --git a/reference/policy.go b/reference/policy.go index c2b7ee8..046bc4f 100644 --- a/reference/policy.go +++ b/reference/policy.go @@ -7,10 +7,21 @@ import ( "github.com/skarm/xmlsec/c14n" "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/nilcheck" "github.com/skarm/xmlsec/internal/tree" ) +const ( + defaultMaxInputBytes = 8 << 20 + defaultMaxNodeVisits = 2_000_000 + defaultMaxOutputBytes = 32 << 20 + defaultMaxProducedBytes = 32 << 20 + defaultMaxTransforms = 8 + maximumInclusivePrefixes = 128 + maximumTransforms = 64 +) + // Options sets the allowed transform chain, processing budgets and optional trusted // extension processor. Zero-valued numeric limits use [DefaultOptions]. type Options struct { @@ -47,8 +58,8 @@ func DefaultOptions() Options { EnvelopedSignature, Base64, c14n.Exclusive, c14n.ExclusiveWithComments, c14n.Canonical10, c14n.Canonical10WithComments, c14n.Canonical11, c14n.Canonical11WithComments, }, - MaxTransforms: 8, MaxInputBytes: 8 << 20, MaxOutputBytes: 32 << 20, - MaxProducedBytes: 32 << 20, MaxNodeVisits: 2_000_000, XMLLimits: document.DefaultLimits(), + MaxTransforms: defaultMaxTransforms, MaxInputBytes: defaultMaxInputBytes, MaxOutputBytes: defaultMaxOutputBytes, + MaxProducedBytes: defaultMaxProducedBytes, MaxNodeVisits: defaultMaxNodeVisits, XMLLimits: document.DefaultLimits(), } } @@ -98,7 +109,7 @@ func normalize(opt Options) (Options, error) { opt.XMLLimits = d.XMLLimits } - if opt.MaxTransforms < 1 || opt.MaxTransforms > 64 || opt.MaxInputBytes < 1 || opt.MaxOutputBytes < 1 || opt.MaxProducedBytes < 1 || opt.MaxNodeVisits < 1 { + if opt.MaxTransforms < 1 || opt.MaxTransforms > maximumTransforms || opt.MaxInputBytes < 1 || opt.MaxOutputBytes < 1 || opt.MaxProducedBytes < 1 || opt.MaxNodeVisits < 1 { return opt, fmt.Errorf("%w: invalid limits", ErrPolicy) } @@ -154,12 +165,12 @@ func validate(ctx context.Context, ts []Transform, opt Options) error { return &StepError{i, t.Algorithm, fmt.Errorf("%w: InclusiveNamespaces not applicable", ErrInput)} } - if len(t.InclusivePrefixes) > 128 { + if len(t.InclusivePrefixes) > maximumInclusivePrefixes { return &StepError{i, t.Algorithm, ErrLimit} } for _, p := range t.InclusivePrefixes { - if p != "#default" && (!tree.NCName(p) || p == "xmlns") { + if p != lexicon.C14NDefaultPrefixToken && (!tree.NCName(p) || p == lexicon.PrefixXMLNS) { return &StepError{i, t.Algorithm, fmt.Errorf("%w: inclusive prefix", ErrInput)} } } diff --git a/reference/reader.go b/reference/reader.go index c5cdd2b..86770e7 100644 --- a/reference/reader.go +++ b/reference/reader.go @@ -13,6 +13,10 @@ import ( "github.com/skarm/xmlsec/internal/tree" ) +const ( + base64LexicalBufferBytes = 4096 +) + // WriteReader processes octets from src and writes the transformed bytes to dst. // Raw and compound Base64 chains use bounded working buffers. Canonicalization // passes its bytes directly to subsequent stages. XML transforms retain a tree; @@ -85,7 +89,7 @@ func (p *processor) completed(ctx context.Context) (*Report, error) { type base64Reader struct { source io.Reader - buffer [4096]byte + buffer [base64LexicalBufferBytes]byte } func (r *base64Reader) Read(p []byte) (int, error) { diff --git a/reference/same_document.go b/reference/same_document.go index 26dc6f0..17aa366 100644 --- a/reference/same_document.go +++ b/reference/same_document.go @@ -12,19 +12,55 @@ import ( "github.com/skarm/xmlsec/internal/tree" ) +const ( + defaultIDAttribute = "ID" + xpointerIDPrefix = "xpointer(id(" + xpointerWholeDocument = "xpointer(/)" + maximumIDRuleElements = 64 + maximumIDRules = 32 +) + // SameDocumentResolver indexes explicitly typed IDs in one immutable snapshot. // IDs must be unique NCNames. Resolve supports concurrent calls; it performs no // schema-based typing or XPath evaluation. type SameDocumentResolver struct { + // doc is the immutable document in which references are resolved. doc *document.Document + // ids indexes unique IDs accepted under the configured attribute rules. ids map[string]document.Element } +// IDAttributeRule treats an exact attribute name as an ID on selected element +// names. Empty Elements applies the rule to every element. Repeating a rule for +// the same attribute is allowed; two distinct ID attributes on one element are +// rejected even if their values agree. ID typing does not use local-name guesses, +// automatic xml:id recognition, schemas or DTD declarations. +type IDAttributeRule struct { + // Attribute is the exact expanded name of the attribute to treat as an ID. + Attribute document.Name + // Elements restricts the rule to exact expanded names. Empty applies it to all elements. + Elements []document.Name +} + +// Resolved retains an immutable same-document target and its comment-selection rules. +type Resolved struct { + // data is the resolved representation before transforms. + data Data + // target identifies an element target, when the URI selects one. + target document.Element + // uri retains the original same-document reference. + uri string + // whole records whether the reference selects the document. + whole bool + // comments records whether the selected URI form retains comment nodes. + comments bool +} + // NewSameDocumentResolver indexes the explicit application ID attribute and rejects // duplicate identifiers. func NewSameDocumentResolver(ctx context.Context, doc *document.Document, idAttribute document.Name) (*SameDocumentResolver, error) { if idAttribute == (document.Name{}) { - idAttribute.Local = "ID" + idAttribute.Local = defaultIDAttribute } return NewSameDocumentResolverWithRules(ctx, doc, []IDAttributeRule{{Attribute: idAttribute}}) @@ -43,7 +79,7 @@ func NewSameDocumentResolverWithRules(ctx context.Context, doc *document.Documen return nil, err } - if len(rules) > 32 { + if len(rules) > maximumIDRules { return nil, fmt.Errorf("%w: too many ID rules", ErrPolicy) } @@ -71,8 +107,15 @@ func (r *SameDocumentResolver) Target(ctx context.Context, uri string) (document return m.target, m.whole, nil } -// Resolve selects the authorized same-document target and applies URI-specific comment rules. +// Resolve selects a same-document target and applies URI-specific comment rules. +// It accepts URI="", #ID, #xpointer(/), #xpointer(id('ID')) and the double-quoted +// id form. Fragment percent escapes are decoded once. Other XPointer expressions, +// whitespace variants and external URIs are rejected. func (r *SameDocumentResolver) Resolve(ctx context.Context, uri string) (Resolved, error) { + if r == nil { + return Resolved{}, ErrInput + } + result, err := r.identify(ctx, uri) if err != nil { return Resolved{}, err @@ -101,10 +144,11 @@ func (r *SameDocumentResolver) Resolve(ctx context.Context, uri string) (Resolve return result, nil } -// Resolve implements URI="", #ID, #xpointer(/), #xpointer(id('ID')) and the -// double-quoted id form. Percent escapes are decoded ONCE in the fragment. -// Other XPointer expressions, whitespace variants and external URIs fail closed. +// identify finds the same-document target and URI-specific comment rules +// without constructing a node set. func (r *SameDocumentResolver) identify(ctx context.Context, uri string) (Resolved, error) { + const minimumQuotedIDBytes = 3 // opening quote, nonempty NCName, closing quote + if ctx == nil || r == nil || r.doc == nil { return Resolved{}, ErrInput } @@ -128,12 +172,12 @@ func (r *SameDocumentResolver) identify(ctx context.Context, uri string) (Resolv switch { case tree.NCName(fragment): id = fragment - case fragment == "xpointer(/)": + case fragment == xpointerWholeDocument: result.whole = true result.comments = true - case strings.HasPrefix(fragment, "xpointer(id(") && strings.HasSuffix(fragment, "))"): - value := fragment[len("xpointer(id(") : len(fragment)-2] - if len(value) < 3 || value[0] != '\'' && value[0] != '"' || value[len(value)-1] != value[0] || !tree.NCName(value[1:len(value)-1]) { + case strings.HasPrefix(fragment, xpointerIDPrefix) && strings.HasSuffix(fragment, "))"): + value := fragment[len(xpointerIDPrefix) : len(fragment)-2] + if len(value) < minimumQuotedIDBytes || value[0] != '\'' && value[0] != '"' || value[len(value)-1] != value[0] || !tree.NCName(value[1:len(value)-1]) { return Resolved{}, fmt.Errorf("%w: XPointer id form", ErrUnsupported) } @@ -198,9 +242,24 @@ func (r *SameDocumentResolver) indexIDNodes(ctx context.Context, n *tree.Node, r return nil } +// Data returns the immutable resolved transform input. +func (r Resolved) Data() Data { return r.data } + +// Target is invalid for a whole-document reference (document node != root element). +func (r Resolved) Target() document.Element { return r.target } + +// URI returns the reference URI. +func (r Resolved) URI() string { return r.uri } + +// WholeDocument returns whether the URI selects the entire document. +func (r Resolved) WholeDocument() bool { return r.whole } + +// RetainsComments returns whether URI resolution retains comment nodes. +func (r Resolved) RetainsComments() bool { return r.comments } + func validateSameDocumentRules(rules []IDAttributeRule) error { for _, rule := range rules { - if !tree.NCName(rule.Attribute.Local) || rule.Attribute.Space == tree.XMLNSNamespace || len(rule.Elements) > 64 { + if !tree.NCName(rule.Attribute.Local) || rule.Attribute.Space == tree.XMLNSNamespace || len(rule.Elements) > maximumIDRuleElements { return fmt.Errorf("%w: ID rule", ErrPolicy) } @@ -231,39 +290,3 @@ func matchesIDRule(rules []IDAttributeRule, a, e document.Name) bool { return false } - -// IDAttributeRule treats an exact attribute name as an ID on selected element -// names. Empty Elements applies the rule to every element. Repeating a rule for -// the same attribute is allowed; two distinct ID attributes on one element are -// rejected even if their values agree. ID typing does not use local-name guesses, -// automatic xml:id recognition, schemas or DTD declarations. -type IDAttributeRule struct { - // Attribute is the exact expanded name of the attribute to treat as an ID. - Attribute document.Name - // Elements restricts the rule to exact expanded names. Empty applies it to all elements. - Elements []document.Name -} - -// Resolved retains an immutable same-document target and its comment-selection rules. -type Resolved struct { - data Data - target document.Element - uri string - whole bool - comments bool -} - -// Data returns the immutable resolved transform input. -func (r Resolved) Data() Data { return r.data } - -// Target is invalid for a whole-document reference (document node != root element). -func (r Resolved) Target() document.Element { return r.target } - -// URI returns the reference URI. -func (r Resolved) URI() string { return r.uri } - -// WholeDocument returns whether the URI selects the entire document. -func (r Resolved) WholeDocument() bool { return r.whole } - -// RetainsComments returns whether URI resolution retains comment nodes. -func (r Resolved) RetainsComments() bool { return r.comments } diff --git a/resource/resource.go b/resource/resource.go index a48dedc..adff03e 100644 --- a/resource/resource.go +++ b/resource/resource.go @@ -13,9 +13,14 @@ import ( "net/url" "strings" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/nilcheck" ) +const ( + maximumRedirects = 10 +) + var ( // ErrInput reports invalid adapter configuration. ErrInput = errors.New("resource: invalid input") @@ -80,7 +85,9 @@ type FSConfig struct { // FSResolver retains immutable URI mapping policy. Its application-owned filesystem // must support concurrent reads. type FSResolver struct { - fs fs.FS + // fs is the caller-supplied filesystem exposed through this resolver. + fs fs.FS + // base is the trusted absolute URI used to map resources into fs. base *url.URL } @@ -121,7 +128,7 @@ func (r *FSResolver) Resolve(ctx context.Context, uri string) (io.ReadCloser, er f, err := r.fs.Open(name) if err != nil { - if !nilcheck.IsNil(f) { + if f != nil && !nilcheck.IsNil(f) { err = errors.Join(err, f.Close()) } @@ -152,7 +159,9 @@ type HTTPConfig struct { // HTTPResolver checks the configured origin allowlist on initial requests and // redirects. type HTTPResolver struct { - client *http.Client + // client is the private HTTP client configured for resource resolution. + client *http.Client + // origins contains the explicitly authorized HTTP origins. origins map[string]struct{} } @@ -165,7 +174,7 @@ func NewHTTPResolver(config HTTPConfig) (*HTTPResolver, error) { r := &HTTPResolver{origins: make(map[string]struct{}, len(config.AllowedOrigins))} for _, origin := range config.AllowedOrigins { u, err := parseURI(origin) - if err != nil || u.Scheme != "http" && u.Scheme != "https" || u.Host == "" || u.Path != "" && u.Path != "/" { + if err != nil || u.Scheme != lexicon.SchemeHTTP && u.Scheme != lexicon.SchemeHTTPS || u.Host == "" || u.Path != "" && u.Path != "/" { return nil, ErrInput } @@ -192,7 +201,7 @@ func NewHTTPResolver(config HTTPConfig) (*HTTPResolver, error) { return nil } - if len(via) >= 10 { + if len(via) >= maximumRedirects { return fmt.Errorf("%w: too many redirects", ErrPolicy) } @@ -237,11 +246,17 @@ func (r *HTTPResolver) Resolve(ctx context.Context, uri string) (io.ReadCloser, return nil, errors.Join(fmt.Errorf("%w: HTTP status %d", ErrResource, response.StatusCode), response.Body.Close()) } - return &contextReader{ctx: ctx, ReadCloser: response.Body, mediaType: response.Header.Get("Content-Type"), size: response.ContentLength}, nil + return &contextReader{ctx: ctx, ReadCloser: response.Body, mediaType: response.Header.Get(lexicon.HeaderContentType), size: response.ContentLength}, nil } func (r *HTTPResolver) allowed(u *url.URL) bool { - if u == nil || u.User != nil || (u.Scheme != "http" && u.Scheme != "https") { + if u == nil || u.User != nil || (u.Scheme != lexicon.SchemeHTTP && u.Scheme != lexicon.SchemeHTTPS) { + return false + } + + // url.Parse leaves query escapes unchecked. Validate without changing the + // spelling used by the request or the caller's resource policy. + if _, err := url.QueryUnescape(u.RawQuery); err != nil { return false } @@ -268,7 +283,7 @@ func (t *httpTransport) RoundTrip(req *http.Request) (*http.Response, error) { } if err != nil && response != nil { - if !nilcheck.IsNil(response.Body) { + if response.Body != nil && !nilcheck.IsNil(response.Body) { err = errors.Join(err, response.Body.Close()) } diff --git a/resource/resource_test.go b/resource/resource_test.go index 2d98f3a..8f1d163 100644 --- a/resource/resource_test.go +++ b/resource/resource_test.go @@ -163,19 +163,6 @@ func TestEvidenceObservationOwnership(t *testing.T) { } } -func evidenceCollector(t testing.TB, transport http.RoundTripper, limits collect.Limits, hook func(*http.Request, []*http.Request) error) *collect.HTTP { - t.Helper() - c, err := collect.NewHTTP(collect.Config{Client: &http.Client{Transport: transport, CheckRedirect: hook}, AllowedURIs: []string{"https://evidence.test/first", "https://evidence.test/final"}, Limits: limits, Now: func() time.Time { return time.Unix(42, 0) }}) - if err != nil { - t.Fatal(err) - } - return c -} - -func evidenceResponse(req *http.Request, status int, body io.ReadCloser) *http.Response { - return &http.Response{StatusCode: status, Header: make(http.Header), Body: body, Request: req} -} - func TestEvidenceCollectorSuccessAndConcurrentCalls(t *testing.T) { request := []byte("OCSP request binding") server := httptest.NewTestServer(t, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { @@ -514,34 +501,6 @@ func TestHTTPResolverCancellationAtOpenAndFinalRead(t *testing.T) { } } -type openFunc func(string) (fs.File, error) - -func (f openFunc) Open(name string) (fs.File, error) { return f(name) } - -type testFile struct { - closes int - closeErr error -} - -func (f *testFile) Read([]byte) (int, error) { panic("unexpected read") } - -func (f *testFile) Stat() (fs.FileInfo, error) { panic("unexpected stat") } - -func (f *testFile) Close() error { f.closes++; return f.closeErr } - -type roundTripFunc func(*http.Request) (*http.Response, error) - -func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) { return f(req) } - -type callbackBody struct { - read func([]byte) (int, error) - close func() error -} - -func (r *callbackBody) Read(p []byte) (int, error) { return r.read(p) } - -func (r *callbackBody) Close() error { return r.close() } - func TestEvidenceCollectorPreservesRedirectCloseError(t *testing.T) { closeErr := errors.New("redirect close failed") calls, closed := 0, 0 @@ -1104,3 +1063,84 @@ func TestEvidenceStoreFailedCleanupStopsAcquisition(t *testing.T) { t.Fatal("close did not retry cleanup", directories, err) } } + +func evidenceCollector(t testing.TB, transport http.RoundTripper, limits collect.Limits, hook func(*http.Request, []*http.Request) error) *collect.HTTP { + t.Helper() + c, err := collect.NewHTTP(collect.Config{Client: &http.Client{Transport: transport, CheckRedirect: hook}, AllowedURIs: []string{"https://evidence.test/first", "https://evidence.test/final"}, Limits: limits, Now: func() time.Time { return time.Unix(42, 0) }}) + if err != nil { + t.Fatal(err) + } + return c +} + +func evidenceResponse(req *http.Request, status int, body io.ReadCloser) *http.Response { + return &http.Response{StatusCode: status, Header: make(http.Header), Body: body, Request: req} +} + +type openFunc func(string) (fs.File, error) + +func (f openFunc) Open(name string) (fs.File, error) { return f(name) } + +type testFile struct { + closes int + closeErr error +} + +func (f *testFile) Read([]byte) (int, error) { panic("unexpected read") } + +func (f *testFile) Stat() (fs.FileInfo, error) { panic("unexpected stat") } + +func (f *testFile) Close() error { f.closes++; return f.closeErr } + +type roundTripFunc func(*http.Request) (*http.Response, error) + +func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) { return f(req) } + +type callbackBody struct { + read func([]byte) (int, error) + close func() error +} + +func (r *callbackBody) Read(p []byte) (int, error) { return r.read(p) } + +func (r *callbackBody) Close() error { return r.close() } + +func TestHTTPResolverRejectsMalformedQueryEscapes(t *testing.T) { + calls := 0 + wantQuery := "x=%23%2f%25+value;other=yes" + resolver, err := resource.NewHTTPResolver(resource.HTTPConfig{ + AllowedOrigins: []string{"https://resource.test"}, + Client: &http.Client{Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) { + calls++ + if req.URL.RawQuery != wantQuery { + t.Errorf("query changed: %q", req.URL.RawQuery) + } + + return evidenceResponse(req, http.StatusOK, io.NopCloser(strings.NewReader("body"))), nil + })}, + }) + if err != nil { + t.Fatal(err) + } + + for _, query := range []string{"x=%zz", "x=%", "x=%0"} { + stream, err := resolver.Resolve(t.Context(), "https://resource.test/file?"+query) + if stream != nil { + _ = stream.Close() + } + + if stream != nil || !errors.Is(err, resource.ErrPolicy) || calls != 0 { + t.Errorf("malformed query %q: stream=%v, err=%v, calls=%d", query, stream, err, calls) + } + } + + calls = 0 + stream, err := resolver.Resolve(t.Context(), "https://resource.test/file?"+wantQuery) + if err != nil || stream == nil || calls != 1 { + t.Fatalf("valid encoded query rejected: %v, calls=%d", err, calls) + } + + if err := stream.Close(); err != nil { + t.Fatal(err) + } +} diff --git a/saml/artifact/artifact.go b/saml/artifact/artifact.go new file mode 100644 index 0000000..85aa7f4 --- /dev/null +++ b/saml/artifact/artifact.go @@ -0,0 +1,428 @@ +// Package artifact implements single-use SAML artifact issuance and authenticated +// SOAP resolution. Resolving an artifact does not validate the enclosed protocol. +package artifact + +import ( + "context" + "crypto" + "errors" + "sync" + "time" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/nilcheck" + "github.com/skarm/xmlsec/saml/binding" + "github.com/skarm/xmlsec/saml/internal/expiry" + "github.com/skarm/xmlsec/saml/internal/profile" + "github.com/skarm/xmlsec/saml/metadata" + "github.com/skarm/xmlsec/saml/protocol" +) + +// Issue and direct store insertion share the same five-minute lifetime ceiling; artifact expiration remains exclusive. +const ( + maximumArtifactLifetime = 5 * time.Minute +) + +// The type 0x0004 source identifier is exactly 20 octets, whether derived from metadata entityID or supplied by local agreement; it is a routing hint, not authentication. +const ( + artifactSourceBytes = 20 +) + +// ErrArtifact reports a rejected artifact, expired entry or exhausted store. +var ErrArtifact = errors.New("SAML artifact rejected") + +// Entry binds an artifact to its exact intended recipient and short expiry. +type Entry struct { + // Artifact is the opaque lookup key issued to the browser. + Artifact binding.Artifact + // Recipient is the exact entity ID authorized to resolve the artifact. + Recipient string + // Message is the immutable protocol message to release once. + Message *document.Document + // ExpiresAt is the exclusive expiration time of the artifact. + ExpiresAt time.Time +} + +// Store must atomically enforce single use across all callers and instances. +// Take returns nil for missing, expired, consumed or wrong-recipient artifacts. +// A wrong recipient must not consume an otherwise valid entry. +type Store interface { + Put(context.Context, Entry, time.Time) error + Take(context.Context, binding.Artifact, string, time.Time) (*document.Document, error) + // Resolve must publish the signed reply and consume the artifact atomically. + // A callback failure must preserve the artifact. The callback sees nil for + // unknown, consumed, expired and unauthorized artifacts. + Resolve(context.Context, binding.Artifact, string, time.Time, func(*document.Document) (*document.Document, error)) (*document.Document, error) +} + +// MemoryStore is a bounded, concurrency-safe single-process implementation. +// Distributed services must use a shared durable Store with the same contract. +type MemoryStore struct { + // mu serializes artifact consumption, reply creation and expiry maintenance. + mu sync.Mutex + // entries retains live messages and consumed tombstones until expiration. + entries map[binding.Artifact]Entry + // expiry indexes expiration once the store grows past the scan threshold. + expiry expiry.Index[binding.Artifact] + // indexed records whether expiry has been initialized from all entries. + indexed bool + // capacity bounds live entries and retained tombstones together. + capacity int +} + +// NewMemoryStore creates a store; zero capacity selects 10,000 entries. +func NewMemoryStore(capacity int) (*MemoryStore, error) { + const ( + defaultStoreCapacity = 10000 + maximumStoreCapacity = 1000000 + ) + + if capacity == 0 { + capacity = defaultStoreCapacity + } + + if capacity < 1 || capacity > maximumStoreCapacity { + return nil, ErrArtifact + } + + return &MemoryStore{entries: make(map[binding.Artifact]Entry), capacity: capacity}, nil +} + +// Put stores an immutable message for no more than five minutes. It never evicts +// an unexpired artifact or replaces a colliding handle. +func (s *MemoryStore) Put(ctx context.Context, e Entry, at time.Time) error { + const ( + expiryIndexThreshold = 64 + ) + + if ctx == nil || s == nil || s.capacity == 0 || at.IsZero() || !profile.URI(e.Recipient) || e.Message == nil || e.Message.Root().Name().Space != protocol.ProtocolNamespace || !at.Before(e.ExpiresAt) || e.ExpiresAt.Sub(at) > maximumArtifactLifetime { + return ErrArtifact + } + + if err := ctx.Err(); err != nil { + return err + } + + s.mu.Lock() + defer s.mu.Unlock() + + if err := ctx.Err(); err != nil { + return err + } + + if s.indexed { + if s.expiry.AllExpired(at) { + clear(s.entries) + s.expiry.Reset() + } else { + for { + a, ok := s.expiry.PopExpired(at) + if !ok { + break + } + + delete(s.entries, a) + } + } + + s.expiry.Trim() + } else { + // Small stores fit in a few map groups; avoid a second index until + // repeated scans become more expensive than maintaining a heap. + for a, entry := range s.entries { + if !at.Before(entry.ExpiresAt) { + delete(s.entries, a) + } + } + } + + if len(s.entries) >= s.capacity { + return ErrArtifact + } + + if _, ok := s.entries[e.Artifact]; ok { + return ErrArtifact + } + + s.entries[e.Artifact] = e + if s.indexed { + s.expiry.Set(e.Artifact, e.ExpiresAt) + } else if len(s.entries) >= expiryIndexThreshold { + for a, entry := range s.entries { + s.expiry.Set(a, entry.ExpiresAt) + } + + s.indexed = true + } + + return nil +} + +// Take atomically releases a message once to its authenticated recipient. +func (s *MemoryStore) Take(ctx context.Context, a binding.Artifact, recipient string, at time.Time) (*document.Document, error) { + if ctx == nil || s == nil || at.IsZero() { + return nil, ErrArtifact + } + + if err := ctx.Err(); err != nil { + return nil, err + } + + s.mu.Lock() + defer s.mu.Unlock() + + if err := ctx.Err(); err != nil { + return nil, err + } + + e, ok := s.entries[a] + if !ok { + return nil, nil + } + + if !at.Before(e.ExpiresAt) { + delete(s.entries, a) + s.expiry.Delete(a) + + return nil, nil + } + + if e.Recipient != recipient { + return nil, nil + } + + message := e.Message + // Keep the handle reserved until expiration so a consumed artifact cannot + // be rebound to a different message by a later Put. + e.Message = nil + s.entries[a] = e + + return message, nil +} + +// Resolve holds a transaction through reply creation and commits consumption +// only after successful signing. It retains a tombstone until the entry expires. +func (s *MemoryStore) Resolve(ctx context.Context, a binding.Artifact, recipient string, at time.Time, reply func(*document.Document) (*document.Document, error)) (*document.Document, error) { + if ctx == nil || s == nil || at.IsZero() || reply == nil { + return nil, ErrArtifact + } + + if err := ctx.Err(); err != nil { + return nil, err + } + + s.mu.Lock() + defer s.mu.Unlock() + + if err := ctx.Err(); err != nil { + return nil, err + } + + e, ok := s.entries[a] + + var message *document.Document + if ok && at.Before(e.ExpiresAt) && e.Recipient == recipient { + message = e.Message + } + + d, err := reply(message) + if err != nil { + return nil, err + } + + if d == nil { + return nil, ErrArtifact + } + + if err := ctx.Err(); err != nil { + return nil, err + } + + if message != nil { + e.Message = nil + s.entries[a] = e + } + + return d, nil +} + +// Issuer creates artifacts and replies to previously authenticated resolution +// requests. Endpoint and ReturnEndpoint come from trusted metadata/configuration. +type Issuer struct { + // EntityID identifies the local issuing provider. + EntityID string + // Endpoint is the trusted local ArtifactResolutionService URL. + Endpoint string + // ReturnEndpoint is the trusted recipient URL used as the response destination. + ReturnEndpoint string + // EndpointIndex identifies Endpoint in the issuer's metadata. + EndpointIndex uint16 + // Signing supplies the key and policy for artifact responses. + Signing protocol.SigningKey + // Store retains messages and atomically consumes resolved artifacts. + Store Store + // Limits must match the incoming ArtifactResolve verifier's freshness policy. + Limits protocol.Limits + // SourceID optionally selects a locally agreed opaque source identifier. + // Nil uses the recommended entityID hash; authentication still uses EntityID. + SourceID *[artifactSourceBytes]byte +} + +// Issue binds a new artifact to an exact recipient for the requested lifetime. +func (s Issuer) Issue(ctx context.Context, message *document.Document, recipient string, at time.Time, lifetime time.Duration) (binding.Artifact, error) { + if nilcheck.IsNil(s.Store) || lifetime <= 0 || lifetime > maximumArtifactLifetime { + return binding.Artifact{}, ErrArtifact + } + + a, err := binding.NewArtifact(s.EntityID, s.EndpointIndex) + if err != nil { + return a, err + } + + if s.SourceID != nil { + a.SourceID = *s.SourceID + } + + err = s.Store.Put(ctx, Entry{Artifact: a, Recipient: recipient, Message: message, ExpiresAt: at.Add(lifetime)}, at) + + return a, err +} + +// Respond validates source/index and authorization before consuming the entry. +// Missing, wrong-recipient and already used artifacts all produce Success with +// no message, as required by Core 3.5.3. A signing failure never returns plaintext. +func (s Issuer) Respond(ctx context.Context, r *protocol.VerifiedArtifactResolve, at time.Time) (*document.Document, error) { + if r == nil || nilcheck.IsNil(s.Store) || at.IsZero() { + return nil, ErrArtifact + } + + request := r.Request() + + age, _, skew, err := protocol.AcceptanceWindow(s.Limits) + if err != nil || request.Destination != s.Endpoint || request.IssueInstant.After(at.Add(skew)) || request.IssueInstant.Before(at.Add(-age-skew)) { + return nil, ErrArtifact + } + + reply := func(message *document.Document) (*document.Document, error) { + return protocol.SignStatusResponse(ctx, protocol.StatusResponse{Issuer: s.EntityID, Destination: s.ReturnEndpoint, IssueInstant: at, Kind: protocol.ArtifactResponse, InResponseTo: request.ID, Status: protocol.Status{Code: protocol.Success}, Message: message}, s.Signing) + } + + a, err := binding.ParseArtifact(request.Artifact) + if err != nil { + return reply(nil) + } + + source := binding.ArtifactSource(s.EntityID) + if s.SourceID != nil { + source = *s.SourceID + } + + if a.SourceID != source || a.EndpointIndex != s.EndpointIndex { + return reply(nil) + } + + return s.Store.Resolve(ctx, a, request.Issuer, at, reply) +} + +// Resolver authenticates a SOAP artifact exchange. PeerEntityID, keys, and +// endpoints must be taken from a current trusted peer metadata role. No received +// URL is fetched. Endpoint selection uses the artifact's index and source ID. +type Resolver struct { + // EntityID identifies the local provider making resolution requests. + EntityID string + // PeerEntityID is the expected artifact issuer from trusted metadata. + PeerEntityID string + // ReturnEndpoint is the expected local response destination. + ReturnEndpoint string + // Endpoints supplies the peer's trusted artifact resolution endpoints. + Endpoints []metadata.Endpoint + // Signing supplies the key and policy for resolution requests. + Signing protocol.SigningKey + // Keys contains trusted peer keys for authenticating the response. + Keys []crypto.PublicKey + // Options supplies response verification policy; Resolve sets correlation and + // time. + Options protocol.ExchangeOptions + // SOAP supplies the HTTP transport and SOAP parsing policy. + SOAP binding.SOAPClient + // SourceID is an optional trusted opaque source ID agreed with the peer. + SourceID *[artifactSourceBytes]byte +} + +// Resolve sends a signed ArtifactResolve and verifies the correlated response. +// The returned message is still unverified under its own protocol profile. +func (r Resolver) Resolve(ctx context.Context, a binding.Artifact, at time.Time) (*document.Document, error) { + const ( + maximumResolverEndpoints = 128 + ) + + started := time.Now() + + source := binding.ArtifactSource(r.PeerEntityID) + if r.SourceID != nil { + source = *r.SourceID + } + + if at.IsZero() || a.SourceID != source || len(r.Endpoints) > maximumResolverEndpoints { + return nil, ErrArtifact + } + + endpoint := "" + + for _, e := range r.Endpoints { + if e.Binding == metadata.SOAP && e.Indexed && e.Index == a.EndpointIndex { + if endpoint != "" { + return nil, ErrArtifact + } + + endpoint = e.Location + } + } + + if endpoint == "" { + return nil, ErrArtifact + } + + id, err := profile.NewID() + if err != nil { + return nil, err + } + + d, err := protocol.SignArtifactResolve(ctx, protocol.ArtifactResolve{ID: id, Issuer: r.EntityID, Destination: endpoint, IssueInstant: at, Artifact: a.String()}, r.Signing) + if err != nil { + return nil, err + } + + envelope, err := protocol.SignSOAP(ctx, d, r.Signing) + if err != nil { + return nil, err + } + + response, err := r.SOAP.RoundTrip(ctx, endpoint, envelope) + if err != nil { + return nil, err + } + + payload, err := response.Payload(ctx) + if err != nil { + return nil, err + } + + o := r.Options + o.Binding = metadata.SOAP + o.Issuer = r.PeerEntityID + o.Destination = r.ReturnEndpoint + o.At = at.Add(time.Since(started)) + o.ExpectedRequestID = id + + verified, err := protocol.VerifyStatusResponse(ctx, payload, r.Keys, o) + if err != nil { + return nil, err + } + + if verified.Kind() != protocol.ArtifactResponse || verified.Status().Code != protocol.Success { + return nil, ErrArtifact + } + + return verified.Message(), nil +} diff --git a/saml/artifact/artifact_test.go b/saml/artifact/artifact_test.go new file mode 100644 index 0000000..7e308e1 --- /dev/null +++ b/saml/artifact/artifact_test.go @@ -0,0 +1,182 @@ +package artifact_test + +import ( + "crypto" + "errors" + "net/http" + "net/http/httptest" + "sync" + "sync/atomic" + "testing" + "time" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/testutil/testcert" + "github.com/skarm/xmlsec/saml/artifact" + "github.com/skarm/xmlsec/saml/binding" + "github.com/skarm/xmlsec/saml/metadata" + "github.com/skarm/xmlsec/saml/protocol" +) + +func TestAuthenticatedArtifactSOAP(t *testing.T) { + at := testcert.Now() + cert := testcert.New(t) + const issuerID = "https://idp.test/entity" + const recipientID = "https://sp.test/entity" + const destination = "https://sp.test/acs" + key := protocol.SigningKey{Key: cert.LeafKey, CertificateDER: cert.Leaf.Raw} + peerKey := protocol.SigningKey{Key: cert.TSAKey, CertificateDER: cert.TSA.Raw} + store, err := artifact.NewMemoryStore(10) + if err != nil { + t.Fatal(err) + } + issuer := artifact.Issuer{EntityID: issuerID, ReturnEndpoint: destination, EndpointIndex: 7, Signing: key, Store: store} + opaqueSource := [20]byte{0x81, 0x23, 0x47} + issuer.SourceID = &opaqueSource + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + time.Sleep(2 * time.Millisecond) + m, err := binding.ReadSOAPRequest(r.Context(), r, binding.SOAPOptions{}) + if err != nil { + t.Error(err) + return + } + d, err := m.Payload(r.Context()) + if err != nil { + t.Error(err) + return + } + verified, err := protocol.VerifyArtifactResolve(r.Context(), d, []crypto.PublicKey{cert.TSAKey.Public()}, protocol.ExchangeOptions{Issuer: recipientID, Destination: issuer.Endpoint, At: at}) + if err != nil { + t.Error(err) + return + } + strict := issuer + strict.Limits.MaxMessageAge = time.Second + if _, err := strict.Respond(r.Context(), verified, at.Add(2*time.Second)); err == nil { + t.Error("held ArtifactResolve bypassed the responder's freshness policy") + return + } + response, err := issuer.Respond(r.Context(), verified, at.Add(time.Millisecond)) + if err != nil { + t.Error(err) + return + } + envelope, err := protocol.SignSOAP(r.Context(), response, key) + if err != nil { + t.Error(err) + return + } + if err := binding.WriteSOAPResponse(r.Context(), w, envelope); err != nil { + t.Error(err) + } + })) + defer server.Close() + issuer.Endpoint = server.URL + message, err := protocol.SignLogoutRequest(t.Context(), protocol.LogoutRequest{ID: "_embedded", Issuer: issuerID, Destination: destination, IssueInstant: at, NameID: protocol.NameIdentifier{Value: "alice"}, NotOnOrAfter: at.Add(time.Minute)}, key) + if err != nil { + t.Fatal(err) + } + a, err := issuer.Issue(t.Context(), message, recipientID, at, time.Minute) + if err != nil { + t.Fatal(err) + } + resolver := artifact.Resolver{EntityID: recipientID, PeerEntityID: issuerID, ReturnEndpoint: destination, Endpoints: []metadata.Endpoint{{Binding: metadata.SOAP, Location: server.URL, Index: 7, Indexed: true}}, Signing: peerKey, Keys: []crypto.PublicKey{cert.LeafKey.Public()}, SOAP: binding.SOAPClient{AllowHTTP: true}} + resolver.SourceID = &opaqueSource + resolved, err := resolver.Resolve(t.Context(), a, at) + if err != nil || resolved == nil { + t.Fatalf("resolve: %v", err) + } + logout, err := protocol.VerifyLogoutRequest(t.Context(), resolved, resolver.Keys, protocol.ExchangeOptions{Issuer: issuerID, Destination: destination, At: at}) + if err != nil || logout.Request().ID != "_embedded" || logout.Request().NameID.Value != "alice" { + t.Fatalf("embedded protocol lost: %v", err) + } + empty, err := resolver.Resolve(t.Context(), a, at) + if err != nil || empty != nil { + t.Fatalf("reused artifact must yield empty Success: %v", err) + } + a.EndpointIndex = 9 + if _, err := resolver.Resolve(t.Context(), a, at); err == nil { + t.Fatal("unknown index") + } + a.EndpointIndex = 7 + a.SourceID[0] ^= 1 + if _, err := resolver.Resolve(t.Context(), a, at); err == nil { + t.Fatal("untrusted source") + } +} + +func TestArtifactAtomicRecipientAndExpiry(t *testing.T) { + at := testcert.Now() + c := testcert.New(t) + d, err := protocol.SignArtifactResolve(t.Context(), protocol.ArtifactResolve{Issuer: "https://issuer.test", Destination: "https://peer.test/ars", IssueInstant: at, Artifact: "opaque"}, protocol.SigningKey{Key: c.LeafKey, CertificateDER: c.Leaf.Raw}) + if err != nil { + t.Fatal(err) + } + store, err := artifact.NewMemoryStore(1) + if err != nil { + t.Fatal(err) + } + a, err := binding.NewArtifact("https://issuer.test", 0) + if err != nil { + t.Fatal(err) + } + entry := artifact.Entry{Artifact: a, Recipient: "https://peer.test", Message: d, ExpiresAt: at.Add(time.Minute)} + if err := store.Put(t.Context(), entry, at); err != nil { + t.Fatal(err) + } + if m, err := store.Take(t.Context(), a, "https://attacker.test", at); err != nil || m != nil { + t.Fatal("wrong-recipient disclosure") + } + if _, err := store.Resolve(t.Context(), a, entry.Recipient, at, func(*document.Document) (*document.Document, error) { return nil, errors.New("signing failed") }); err == nil { + t.Fatal("callback failure ignored") + } + var successes atomic.Int32 + var wg sync.WaitGroup + for range 32 { + wg.Go(func() { + m, err := store.Take(t.Context(), a, entry.Recipient, at) + if err != nil { + t.Error(err) + } + if m != nil { + successes.Add(1) + } + }) + } + wg.Wait() + if successes.Load() != 1 { + t.Fatal("artifact not single-use") + } + if err := store.Put(t.Context(), entry, at); err == nil { + t.Fatal("consumed handle was reinserted") + } + if m, err := store.Take(t.Context(), a, entry.Recipient, entry.ExpiresAt); err != nil || m != nil { + t.Fatal("expired artifact") + } +} + +func TestInvalidArtifactReturnsEmptySuccess(t *testing.T) { + at := testcert.Now() + c := testcert.New(t) + key := protocol.SigningKey{Key: c.LeafKey, CertificateDER: c.Leaf.Raw} + store, _ := artifact.NewMemoryStore(10) + issuer := artifact.Issuer{EntityID: "https://idp.test/entity", Endpoint: "https://idp.test/ars", ReturnEndpoint: "https://sp.test/return", Store: store, Signing: key} + for _, value := range []string{"", "unrecognized-artifact"} { + d, err := protocol.SignArtifactResolve(t.Context(), protocol.ArtifactResolve{ID: "_resolve_invalid", Issuer: "https://sp.test/entity", Destination: issuer.Endpoint, IssueInstant: at, Artifact: value}, key) + if err != nil { + t.Fatal(err) + } + r, err := protocol.VerifyArtifactResolve(t.Context(), d, []crypto.PublicKey{key.Key.Public()}, protocol.ExchangeOptions{Issuer: "https://sp.test/entity", Destination: issuer.Endpoint, At: at}) + if err != nil { + t.Fatal(err) + } + reply, err := issuer.Respond(t.Context(), r, at) + if err != nil { + t.Fatal(err) + } + verified, err := protocol.VerifyStatusResponse(t.Context(), reply, []crypto.PublicKey{key.Key.Public()}, protocol.ExchangeOptions{Issuer: issuer.EntityID, Destination: issuer.ReturnEndpoint, ExpectedRequestID: "_resolve_invalid", At: at}) + if err != nil || verified.Status().Code != protocol.Success || verified.Message() != nil { + t.Fatalf("unknown artifact: %v", err) + } + } +} diff --git a/saml/artifact/cancellation_test.go b/saml/artifact/cancellation_test.go new file mode 100644 index 0000000..49bf95e --- /dev/null +++ b/saml/artifact/cancellation_test.go @@ -0,0 +1,102 @@ +package artifact_test + +import ( + "context" + "errors" + "sync" + "sync/atomic" + "testing" + "time" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/testutil/testcert" + "github.com/skarm/xmlsec/saml/artifact" + "github.com/skarm/xmlsec/saml/binding" +) + +type observedContext struct { + context.Context + checked chan struct{} + once sync.Once +} + +func (c *observedContext) Err() error { + err := c.Context.Err() + c.once.Do(func() { close(c.checked) }) + return err +} + +func TestCancellationWhileWaitingForArtifactTransaction(t *testing.T) { + for _, operation := range []string{"Put", "Take", "Resolve"} { + t.Run(operation, func(t *testing.T) { + at := testcert.Now() + doc, err := document.Parse(t.Context(), []byte(``), document.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + store, err := artifact.NewMemoryStore(10) + if err != nil { + t.Fatal(err) + } + held := binding.Artifact{MessageHandle: [20]byte{1}} + target := binding.Artifact{MessageHandle: [20]byte{2}} + added := binding.Artifact{MessageHandle: [20]byte{3}} + entry := func(a binding.Artifact) artifact.Entry { + return artifact.Entry{Artifact: a, Recipient: "https://sp.test/entity", Message: doc, ExpiresAt: at.Add(time.Minute)} + } + for _, a := range []binding.Artifact{held, target} { + if err := store.Put(t.Context(), entry(a), at); err != nil { + t.Fatal(err) + } + } + entered, release, holderDone := make(chan struct{}), make(chan struct{}), make(chan error, 1) + go func() { + _, err := store.Resolve(t.Context(), held, entry(held).Recipient, at, func(message *document.Document) (*document.Document, error) { + close(entered) + <-release + return message, nil + }) + holderDone <- err + }() + <-entered + ctx, cancel := context.WithCancel(t.Context()) + defer cancel() + observed := &observedContext{Context: ctx, checked: make(chan struct{})} + finished := make(chan error, 1) + var callbacks atomic.Int32 + go func() { + var err error + switch operation { + case "Put": + err = store.Put(observed, entry(added), at) + case "Take": + _, err = store.Take(observed, target, entry(target).Recipient, at) + case "Resolve": + _, err = store.Resolve(observed, target, entry(target).Recipient, at, func(message *document.Document) (*document.Document, error) { + callbacks.Add(1) + return message, nil + }) + } + finished <- err + }() + <-observed.checked + cancel() + close(release) + if err := <-holderDone; err != nil { + t.Fatal(err) + } + if err := <-finished; !errors.Is(err, context.Canceled) { + t.Error("queued canceled operation succeeded", err) + } + if callbacks.Load() != 0 { + t.Error("canceled resolution invoked the signing callback") + } + if message, err := store.Take(t.Context(), target, entry(target).Recipient, at); err != nil || message != doc { + t.Error("canceled operation consumed the artifact", err) + } + if message, err := store.Take(t.Context(), added, entry(added).Recipient, at); err != nil || message != nil { + t.Error("canceled operation inserted an artifact", err) + } + }) + } +} diff --git a/saml/artifact/performance_test.go b/saml/artifact/performance_test.go new file mode 100644 index 0000000..8d69417 --- /dev/null +++ b/saml/artifact/performance_test.go @@ -0,0 +1,80 @@ +package artifact_test + +import ( + "encoding/binary" + "strconv" + "testing" + "time" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/testutil/testcert" + "github.com/skarm/xmlsec/saml/artifact" + "github.com/skarm/xmlsec/saml/binding" +) + +// Measure a burst filling the bounded store. Handles and the immutable message +// are prepared outside the timer; one operation is a complete population. +func BenchmarkMemoryStoreIssue(b *testing.B) { + at := testcert.Now() + doc, err := document.Parse(b.Context(), []byte(``), document.DefaultLimits()) + if err != nil { + b.Fatal(err) + } + for _, size := range []int{16, 1024, 8192} { + b.Run(strconv.Itoa(size), func(b *testing.B) { + entries := make([]artifact.Entry, size) + for i := range entries { + a := binding.Artifact{} + binary.BigEndian.PutUint64(a.MessageHandle[:8], uint64(i)) + entries[i] = artifact.Entry{Artifact: a, Recipient: "https://sp.test/entity", Message: doc, ExpiresAt: at.Add(time.Duration(1+i%4) * time.Minute)} + } + b.ReportAllocs() + for b.Loop() { + store, err := artifact.NewMemoryStore(size) + if err != nil { + b.Fatal(err) + } + for _, entry := range entries { + if err := store.Put(b.Context(), entry, at); err != nil { + b.Fatal(err) + } + } + } + }) + } +} + +// Run with a fixed iteration count: population is deliberately outside the +// timer, and is expensive in implementations that scan on every insertion. +func BenchmarkMemoryStoreExpire(b *testing.B) { + at := testcert.Now() + doc, err := document.Parse(b.Context(), []byte(``), document.DefaultLimits()) + if err != nil { + b.Fatal(err) + } + entries := make([]artifact.Entry, 1024) + for i := range entries { + a := binding.Artifact{} + binary.BigEndian.PutUint64(a.MessageHandle[:8], uint64(i)) + entries[i] = artifact.Entry{Artifact: a, Recipient: "https://sp.test/entity", Message: doc, ExpiresAt: at.Add(time.Minute)} + } + b.ReportAllocs() + for b.Loop() { + b.StopTimer() + store, err := artifact.NewMemoryStore(len(entries)) + if err != nil { + b.Fatal(err) + } + for _, entry := range entries { + if err := store.Put(b.Context(), entry, at); err != nil { + b.Fatal(err) + } + } + entry := entries[0] + entry.ExpiresAt = at.Add(2 * time.Minute) + b.StartTimer() + if err := store.Put(b.Context(), entry, at.Add(time.Minute)); err != nil { + b.Fatal(err) + } + } +} diff --git a/saml/artifact/store_index_test.go b/saml/artifact/store_index_test.go new file mode 100644 index 0000000..e5ff185 --- /dev/null +++ b/saml/artifact/store_index_test.go @@ -0,0 +1,164 @@ +package artifact_test + +import ( + "encoding/binary" + "fmt" + "sync" + "sync/atomic" + "testing" + "time" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/testutil/testcert" + "github.com/skarm/xmlsec/saml/artifact" + "github.com/skarm/xmlsec/saml/binding" +) + +func TestStoreMixedExpiryAndReuse(t *testing.T) { + at := testcert.Now() + doc, err := document.Parse(t.Context(), []byte(``), document.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + for _, size := range []int{16, 64, 128} { + store, err := artifact.NewMemoryStore(size) + if err != nil { + t.Fatal(err) + } + entries := make([]artifact.Entry, size) + for i := range entries { + a := binding.Artifact{} + binary.BigEndian.PutUint64(a.MessageHandle[:8], uint64(i)) + entries[i] = artifact.Entry{Artifact: a, Recipient: "https://sp.test/entity", Message: doc, ExpiresAt: at.Add(time.Duration(1+i%4) * time.Minute)} + if err := store.Put(t.Context(), entries[i], at); err != nil { + t.Fatal(err) + } + } + for i, entry := range entries { + if i%4 >= 2 { + continue + } + if message, err := store.Take(t.Context(), entry.Artifact, entry.Recipient, at); err != nil || message != doc { + t.Fatal("live message lost", err) + } + if err := store.Put(t.Context(), entry, at); err == nil { + t.Fatal("consumed artifact lost its collision tombstone") + } + } + for i, entry := range entries { + if i%4 >= 2 { + continue + } + entry.ExpiresAt = at.Add(5 * time.Minute) + if err := store.Put(t.Context(), entry, at.Add(2*time.Minute)); err != nil { + t.Fatal("expired tombstone blocked capacity or handle reuse", err) + } + } + for i, entry := range entries { + message, err := store.Take(t.Context(), entry.Artifact, entry.Recipient, at.Add(2*time.Minute)) + if err != nil || message != doc { + t.Fatalf("wrong live entry %d: %v", i, err) + } + } + for _, entry := range entries { + entry.ExpiresAt = at.Add(6 * time.Minute) + if err := store.Put(t.Context(), entry, at.Add(5*time.Minute)); err != nil { + t.Fatal("store did not drain all expired entries", err) + } + } + } +} + +func TestStoreConcurrentIndexedConsumption(t *testing.T) { + at := testcert.Now() + doc, err := document.Parse(t.Context(), []byte(``), document.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + store, err := artifact.NewMemoryStore(128) + if err != nil { + t.Fatal(err) + } + var handles [128]binding.Artifact + var wg sync.WaitGroup + for worker := range 8 { + wg.Go(func() { + for i := worker; i < len(handles); i += 8 { + binary.BigEndian.PutUint64(handles[i].MessageHandle[:8], uint64(i)) + if err := store.Put(t.Context(), artifact.Entry{Artifact: handles[i], Recipient: "https://sp.test/entity", Message: doc, ExpiresAt: at.Add(time.Minute)}, at); err != nil { + t.Error(err) + } + } + }) + } + wg.Wait() + var consumed atomic.Int32 + for range 16 { + wg.Go(func() { + for _, a := range handles { + message, err := store.Take(t.Context(), a, "https://sp.test/entity", at) + if err != nil { + t.Error(err) + } + if message != nil { + consumed.Add(1) + } + } + }) + } + wg.Wait() + if consumed.Load() != int32(len(handles)) { + t.Fatal("indexed store lost atomic single-use consumption", consumed.Load()) + } +} + +func TestArtifactZeroDeadlinePreservesLiveEntries(t *testing.T) { + t.Parallel() + + doc, err := document.Parse(t.Context(), []byte(``), document.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + for _, count := range []int{16, 64, 65} { + for _, later := range []bool{false, true} { + t.Run(fmt.Sprintf("count=%d/later=%t", count, later), func(t *testing.T) { + t.Parallel() + + // MemoryStore accepts supplied nonzero times around year1. This + // exercises storage expiry, not a protocol authentication timestamp. + zero := time.Time{} + at, checkAt := zero.Add(-4*time.Minute), zero.Add(-3*time.Minute) + store, err := artifact.NewMemoryStore(count + 2) + if err != nil { + t.Fatal(err) + } + put := func(id int, expires, now time.Time) { + t.Helper() + + entry := artifact.Entry{ + Artifact: binding.Artifact{MessageHandle: [20]byte{byte(id)}}, + Recipient: "urn:recipient", Message: doc, ExpiresAt: expires, + } + if err := store.Put(t.Context(), entry, now); err != nil { + t.Fatal(err) + } + } + for id := 1; id <= count; id++ { + put(id, zero, at) + } + expires := checkAt + if later { + expires = zero.Add(time.Minute) + } + put(count+1, expires, at) + put(count+2, zero, checkAt) + for id := 1; id <= count; id++ { + got, err := store.Take(t.Context(), binding.Artifact{MessageHandle: [20]byte{byte(id)}}, "urn:recipient", checkAt) + if err != nil || got != doc { + t.Fatalf("unexpired artifact %d lost: got %p, want %p, err %v", id, got, doc, err) + } + } + }) + } + } +} diff --git a/saml/authority/authority.go b/saml/authority/authority.go new file mode 100644 index 0000000..2354b65 --- /dev/null +++ b/saml/authority/authority.go @@ -0,0 +1,388 @@ +// Package authority implements synchronous SAML assertion query/request and +// NameID Mapping workflows. Applications authorize every disclosure and own the +// authoritative identity, attribute and decision databases. Querying past +// authentication must never be treated as initiating a new login. +package authority + +import ( + "bytes" + "context" + "crypto" + "crypto/sha256" + "encoding/hex" + "errors" + "slices" + "time" + + "github.com/skarm/xmlsec/internal/lexicon" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/nilcheck" + "github.com/skarm/xmlsec/saml/internal/profile" + "github.com/skarm/xmlsec/saml/internal/xmlsig" + "github.com/skarm/xmlsec/saml/metadata" + "github.com/skarm/xmlsec/saml/protocol" + "github.com/skarm/xmlsec/saml/replay" +) + +// ErrDenied indicates that a peer or disclosure was not authorized. +var ErrDenied = errors.New("SAML authority operation denied") + +// Trust resolves keys only from authenticated configuration, never received +// KeyInfo. Providers must support concurrent calls and honor cancellation. +type Trust func(context.Context, string, time.Time) ([]crypto.PublicKey, error) + +// Config supplies the authority's policy and application backends. Query returns +// authorized, already signed assertions about existing facts, or an empty set. +// Mapping authorizes both the source principal and target federation and returns +// an identifier. AllowCreate is permission, never an instruction to bypass account +// policy. Backends may return *protocol.StatusError for an authenticated failure. +type Config struct { + // EntityID identifies the local authority. + EntityID string + // Endpoint is the trusted local SOAP service URL. + Endpoint string + // Signing supplies the authority's response signing key and policy. + Signing protocol.SigningKey + // Trust resolves trusted request verification keys for the expected peer. + Trust Trust + // AssertionTrust optionally resolves assertion issuer keys. Nil uses the local + // signing key for the local issuer and Trust for other issuers. + AssertionTrust Trust + // Replay atomically records accepted requests before backend execution. + Replay replay.Store + // Options supplies request verification policy; Respond sets the peer, + // destination, binding and time. + Options protocol.ExchangeOptions + // Matcher supplies application semantics for matching returned assertions to + // queries. + Matcher protocol.QueryMatchOptions + // Kinds explicitly lists enabled query kinds; Query requires at least one. + Kinds []protocol.QueryKind + // Query authorizes disclosure and returns already signed assertions. Nil + // disables queries. + Query func(context.Context, *protocol.VerifiedQuery) ([]*document.Document, error) + // Mapping authorizes an identifier mapping and returns its identifier document. + // Nil disables mapping. + Mapping func(context.Context, *protocol.VerifiedNameIDMappingRequest) (*document.Document, error) + // EncryptAssertions optionally encrypts verified assertions for the requester. + // It must select EncryptAssertion only. NameID/attribute encryption is also + // available when producing assertions with protocol.SignAuthorityAssertion. + EncryptAssertions func(context.Context, string) (*protocol.EncryptionOptions, error) + // AllowPlaintextMapping opts into the deployment-specific exception to the + // recommendation to encrypt mapped identifiers (Core 3.8 / Profiles 7.4.2). + AllowPlaintextMapping bool +} + +// Service verifies a request before invoking an application backend. Replay +// consumption occurs before a backend can create an identifier. A failed backend +// does not make the same signed request executable again. +type Service struct { + // c retains the validated configuration snapshot and application adapters. + c Config + // kinds contains the explicitly enabled query forms. + kinds map[protocol.QueryKind]bool +} + +// NewService validates and snapshots role selection. Signing providers, replay +// storage and callbacks remain application-owned concurrent adapters. +func NewService(c Config) (*Service, error) { + if !profile.URI(c.EntityID) || !profile.Endpoint(c.Endpoint, true) || c.Trust == nil || c.Replay == nil || c.Query == nil && c.Mapping == nil { + return nil, ErrDenied + } + + if _, _, _, err := protocol.AcceptanceWindow(c.Options.Limits); err != nil { + return nil, err + } + + kinds := map[protocol.QueryKind]bool{} + + for _, k := range c.Kinds { + switch k { + case protocol.AuthenticationQuery, protocol.AttributeQuery, protocol.AuthorizationQuery, protocol.AssertionRequest: + kinds[k] = true + default: + return nil, ErrDenied + } + } + + if c.Query != nil && len(kinds) == 0 { + return nil, ErrDenied + } + + c.Kinds = nil + c.Signing.CertificateDER = bytes.Clone(c.Signing.CertificateDER) + c.Signing.InclusivePrefixes = slices.Clone(c.Signing.InclusivePrefixes) + c.Signing.Policy = xmlsig.ClonePolicy(c.Signing.Policy) + c.Options.Policy = xmlsig.ClonePolicy(c.Options.Policy) + + var err error + + c.Options.Decryption, err = protocol.CloneDecryption(c.Options.Decryption) + if err != nil { + return nil, err + } + + return &Service{c: c, kinds: kinds}, nil +} + +// Respond authenticates one SOAP payload for the expected peer and returns a +// signed SOAP response. Use binding.ReadSOAPRequest/WriteSOAPResponse at HTTP +// boundaries. Invalid unauthenticated messages do not reach application backends. +func (s *Service) Respond(ctx context.Context, d *document.Document, peer string, at time.Time) (*document.Document, error) { + if s == nil || ctx == nil || d == nil || at.IsZero() { + return nil, ErrDenied + } + + keys, err := s.c.Trust(ctx, peer, at) + if err != nil { + return nil, err + } + + o := s.c.Options + o.Issuer = peer + o.Destination = s.c.Endpoint + o.At = at + o.Binding = metadata.SOAP + + if d.Root().Name() == (document.Name{Space: protocol.ProtocolNamespace, Local: lexicon.SAMLElementNameIDMappingRequest}) { + return s.respondMapping(ctx, d, peer, at, keys, o) + } + + return s.respondQuery(ctx, d, peer, at, keys, o) +} + +func (s *Service) consume(ctx context.Context, peer, id string, at time.Time) error { + age, _, skew, err := protocol.AcceptanceWindow(s.c.Options.Limits) + if err != nil { + return err + } + + return s.c.Replay.Consume(ctx, []replay.Entry{{Scope: scope(s.c.EntityID, peer), ID: id, ExpiresAt: at.Add(age + 2*skew)}}, at) +} + +func scope(a, b string) string { + const ( + scopeAuthorityPrefix = "authority:" + scopeSeparator = "\u0000" + ) + + v := sha256.Sum256([]byte(a + scopeSeparator + b)) + + return scopeAuthorityPrefix + hex.EncodeToString(v[:]) +} + +func backendStatus(err error) (protocol.Status, error) { + const ( + statusRequestDenied = "urn:oasis:names:tc:SAML:2.0:status:RequestDenied" + ) + + if err == nil { + return protocol.Status{Code: protocol.Success}, nil + } + + if status, ok := errors.AsType[*protocol.StatusError](err); ok { + if status.Code == protocol.Success || status.Code == "" { + return protocol.Status{}, ErrDenied + } + + return protocol.Status{Code: status.Code, Subcodes: status.Subcodes, Message: status.Message}, nil + } + + if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { + return protocol.Status{}, err + } + + if errors.Is(err, ErrDenied) { + return protocol.Status{Code: protocol.Requester, Subcodes: []string{statusRequestDenied}}, nil + } + + return protocol.Status{Code: protocol.Responder}, nil +} + +func (s *Service) respondMapping(ctx context.Context, d *document.Document, peer string, at time.Time, keys []crypto.PublicKey, o protocol.ExchangeOptions) (*document.Document, error) { + const ( + xmlNameEncryptedID = "EncryptedID" + ) + + if s.c.Mapping == nil { + return nil, ErrDenied + } + + r, err := protocol.VerifyNameIDMappingRequest(ctx, d, keys, o) + if err != nil { + return nil, err + } + + if err = s.consume(ctx, peer, r.Request().ID, at); err != nil { + return nil, err + } + + id, backendErr := s.c.Mapping(ctx, r) + + status, err := backendStatus(backendErr) + if err != nil { + return nil, err + } + + if status.Code == protocol.Success { + if id == nil { + return nil, ErrDenied + } + + if id.Root().Name().Local != xmlNameEncryptedID && !s.c.AllowPlaintextMapping { + return nil, ErrDenied + } + + if err = protocol.CheckMappedPolicy(ctx, id, r.Request().Policy); err != nil { + return nil, err + } + } else { + id = r.ErrorIdentifier() + } + + out, err := protocol.SignNameIDMappingResponse(ctx, protocol.NameIDMappingResponse{Issuer: s.c.EntityID, IssueInstant: at, InResponseTo: r.Request().ID, Status: status, Identifier: id}, s.c.Signing) + if err != nil { + return nil, err + } + + return protocol.SignSOAP(ctx, out, s.c.Signing) +} + +func (s *Service) respondQuery(ctx context.Context, d *document.Document, peer string, at time.Time, keys []crypto.PublicKey, o protocol.ExchangeOptions) (*document.Document, error) { + kind := protocol.QueryKind(d.Root().Name().Local) + if !s.kinds[kind] || s.c.Query == nil { + return nil, ErrDenied + } + + r, err := protocol.VerifyQuery(ctx, d, keys, o) + if err != nil { + return nil, err + } + + q := r.Query() + if err = s.consume(ctx, peer, q.ID, at); err != nil { + return nil, err + } + + assertions, backendErr := s.c.Query(ctx, r) + + status, err := backendStatus(backendErr) + if err != nil { + return nil, err + } + + if status.Code != protocol.Success { + assertions = nil + } + + assertions = slices.Clone(assertions) + + verified, err := s.verifyAssertions(ctx, assertions, peer, at, o) + if err != nil { + return nil, err + } + + if status.Code == protocol.Success { + if err = protocol.MatchQueryResponse(ctx, q, verified, s.c.Matcher); err != nil { + var failure *protocol.StatusError + if !errors.As(err, &failure) { + return nil, err + } + + status = protocol.Status{Code: failure.Code, Subcodes: failure.Subcodes} + assertions = nil + } + } + + if len(assertions) > 0 && s.c.EncryptAssertions != nil { + policy, err := s.c.EncryptAssertions(ctx, peer) + if err != nil { + return nil, err + } + + for i, v := range verified { + assertions[i], err = protocol.EncryptAuthorityAssertion(ctx, v, policy) + if err != nil { + return nil, err + } + } + } + + key := s.forwardingKey(at) + + out, err := protocol.SignQueryResponse(ctx, protocol.QueryResponse{Issuer: s.c.EntityID, IssueInstant: at, InResponseTo: q.ID, Status: status, Assertions: assertions}, key) + if err != nil { + return nil, err + } + + return protocol.SignSOAP(ctx, out, key) +} + +func (s *Service) forwardingKey(at time.Time) protocol.SigningKey { + const ( + xmlNameIssuer = "Issuer" + ) + + key := s.c.Signing + if key.PreserveKeys != nil { + return key + } + + key.PreserveKeys = func(ctx context.Context, parent document.Element) ([]crypto.PublicKey, error) { + children := parent.Children() + if len(children) == 0 || children[0].Name() != (document.Name{Space: protocol.AssertionNamespace, Local: xmlNameIssuer}) { + return nil, ErrDenied + } + + return s.assertionKeys(ctx, children[0].Text(), at) + } + + return key +} + +func (s *Service) verifyAssertions(ctx context.Context, assertions []*document.Document, peer string, at time.Time, o protocol.ExchangeOptions) ([]*protocol.VerifiedAuthorityAssertion, error) { + verified := make([]*protocol.VerifiedAuthorityAssertion, 0, len(assertions)) + + for _, a := range assertions { + if a == nil || len(a.Root().Children()) == 0 { + return nil, ErrDenied + } + + who := a.Root().Children()[0].Text() + + ak, err := s.assertionKeys(ctx, who, at) + if err != nil { + return nil, err + } + + v, err := protocol.VerifyAuthorityAssertion(ctx, a, ak, protocol.AssertionOptions{Issuer: who, Audience: peer, At: at, Limits: o.Limits, Policy: o.Policy}) + if err != nil { + return nil, err + } + + verified = append(verified, v) + } + + return verified, nil +} + +func (s *Service) assertionKeys(ctx context.Context, issuer string, at time.Time) ([]crypto.PublicKey, error) { + if s.c.AssertionTrust != nil { + return s.c.AssertionTrust(ctx, issuer, at) + } + + if issuer != s.c.EntityID { + return s.c.Trust(ctx, issuer, at) + } + + if s.c.Signing.Secret != nil { + return []crypto.PublicKey{*s.c.Signing.Secret}, nil + } + + if nilcheck.IsNil(s.c.Signing.Key) { + return nil, ErrDenied + } + + return []crypto.PublicKey{s.c.Signing.Key.Public()}, nil +} diff --git a/saml/authority/authority_test.go b/saml/authority/authority_test.go new file mode 100644 index 0000000..d80a211 --- /dev/null +++ b/saml/authority/authority_test.go @@ -0,0 +1,363 @@ +package authority //nolint:testpackage // This suite also checks cancellation while holding the private store lock. + +import ( + "context" + "crypto" + "crypto/rand" + "crypto/rsa" + "errors" + "net/http" + "net/http/httptest" + "strconv" + "sync" + "sync/atomic" + "testing" + "time" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/testutil/testcert" + "github.com/skarm/xmlsec/saml/binding" + "github.com/skarm/xmlsec/saml/metadata" + "github.com/skarm/xmlsec/saml/protocol" + "github.com/skarm/xmlsec/saml/replay" +) + +const ( + requester = "https://requester.test/entity" + issuer = "https://authority.test/entity" +) + +func parsed(t *testing.T, s string) *document.Document { + t.Helper() + d, err := document.Parse(t.Context(), []byte(s), document.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + return d +} + +func replayStore(t *testing.T) replay.Store { + t.Helper() + s, err := replay.NewMemoryStore(replay.Config{}) + if err != nil { + t.Fatal(err) + } + return s +} + +// An actual HTTPS SOAP exchange exercises trusted metadata, all three authority +// roles, AssertionIDRequest, opaque target mapping, URI retrieval and replay. +func TestAuthoritySOAPAndURI(t *testing.T) { + c, caller := testcert.New(t), testcert.New(t) + at := testcert.Now() + key := protocol.SigningKey{Key: c.LeafKey} + callerKey := protocol.SigningKey{Key: caller.LeafKey} + target, err := rsa.GenerateKey(rand.Reader, 2048) + if err != nil { + t.Fatal(err) + } + sub, _ := protocol.NewSubject(t.Context(), protocol.NameIdentifier{Value: "alice"}) + statement := parsed(t, `reader`) + assertion, err := protocol.SignAuthorityAssertion(t.Context(), protocol.AuthorityAssertion{ID: "_held", Issuer: issuer, IssueInstant: at, Subject: sub, Statements: []*document.Document{statement}}, key) + if err != nil { + t.Fatal(err) + } + bulk := make([]*document.Document, 0, 20) + conditions := parsed(t, ``) + for i := range 20 { + a, err := protocol.SignAuthorityAssertion(t.Context(), protocol.AuthorityAssertion{ID: "_bulk_assertion_" + strconv.Itoa(i), Issuer: issuer, IssueInstant: at, Subject: sub, Statements: []*document.Document{statement}, Conditions: conditions}, key) + if err != nil { + t.Fatal(err) + } + bulk = append(bulk, a) + } + store, _ := NewMemoryAssertions(4) + if err = store.Add(t.Context(), assertion); err != nil { + t.Fatal(err) + } + var service *Service + var calls atomic.Int32 + mux := http.NewServeMux() + mux.HandleFunc("/soap", func(w http.ResponseWriter, r *http.Request) { + m, err := binding.ReadSOAPRequest(r.Context(), r, binding.SOAPOptions{}) + if err != nil { + http.Error(w, "SOAP", 400) + return + } + d, _ := m.Payload(r.Context()) + out, err := service.Respond(r.Context(), d, requester, at) + if err != nil { + http.Error(w, "denied", http.StatusForbidden) + return + } + if err = binding.WriteSOAPResponse(r.Context(), w, out); err != nil { + t.Error(err) + } + }) + mux.Handle("/assertion", URIHandler{Authenticate: func(r *http.Request) (string, error) { + u, p, ok := r.BasicAuth() + if !ok || u != "alice" || p != "secret" { + return "", ErrDenied + } + return requester, nil + }, Lookup: func(ctx context.Context, peer, id string) (*document.Document, error) { + if peer != requester { + return nil, ErrDenied + } + return store.Get(ctx, id) + }}) + server := httptest.NewTLSServer(mux) + defer server.Close() + trust := func(_ context.Context, peer string, _ time.Time) ([]crypto.PublicKey, error) { + switch peer { + case requester: + return []crypto.PublicKey{caller.LeafKey.Public()}, nil + case issuer: + return []crypto.PublicKey{c.LeafKey.Public()}, nil + default: + return nil, ErrDenied + } + } + service, err = NewService(Config{EntityID: issuer, Endpoint: server.URL + "/soap", Signing: key, Trust: trust, Replay: replayStore(t), Kinds: []protocol.QueryKind{protocol.AttributeQuery, protocol.AuthenticationQuery, protocol.AuthorizationQuery, protocol.AssertionRequest}, Query: func(ctx context.Context, v *protocol.VerifiedQuery) ([]*document.Document, error) { + calls.Add(1) + q := v.Query() + if q.ID == "_bulk" || q.ID == "_bulk_again" { + return bulk, nil + } + if q.Kind == protocol.AttributeQuery || q.Kind == protocol.AssertionRequest { + return []*document.Document{assertion}, nil + } + return nil, nil + }, Mapping: func(ctx context.Context, v *protocol.VerifiedNameIDMappingRequest) (*document.Document, error) { + calls.Add(1) + q := v.Request() + id, err := protocol.NameIDDocument(ctx, protocol.NameIdentifier{Value: "target-alice", Format: q.Policy.Format, SPNameQualifier: q.Policy.SPNameQualifier}) + if err != nil { + return nil, err + } + return protocol.EncryptMappedIdentifier(ctx, id, &protocol.EncryptionOptions{Targets: protocol.EncryptNameID, Recipient: &target.PublicKey}) + }}) + if err != nil { + t.Fatal(err) + } + roles := func(service string) *metadata.RoleDescriptor { + return &metadata.RoleDescriptor{Keys: []metadata.KeyDescriptor{{CertificateDER: c.Leaf.Raw}}, Endpoints: map[string][]metadata.Endpoint{service: {{Binding: metadata.SOAP, Location: server.URL + "/soap"}}, metadata.AssertionIDRequestService: {{Binding: metadata.SOAP, Location: server.URL + "/soap"}, {Binding: metadata.URI, Location: server.URL + "/assertion"}}}} + } + spec := metadata.EntityDescriptor{EntityID: issuer, ValidUntil: at.Add(time.Hour), AuthnAuthority: roles(metadata.AuthnQueryService), AttributeAuthority: roles(metadata.AttributeService), PDP: roles(metadata.AuthzService), IDP: &metadata.RoleDescriptor{Keys: []metadata.KeyDescriptor{{CertificateDER: c.Leaf.Raw}}, Endpoints: map[string][]metadata.Endpoint{metadata.SingleSignOnService: {{Binding: metadata.SOAP, Location: server.URL + "/soap"}}, metadata.NameIDMappingService: {{Binding: metadata.SOAP, Location: server.URL + "/soap"}}}}} + md, err := metadata.Build(t.Context(), spec, at, metadata.Options{}) + if err != nil { + t.Fatal(err) + } + raw, _ := md.XML(t.Context()) + snapshot, err := metadata.ParseTrusted(t.Context(), raw, at, metadata.Options{}) + if err != nil { + t.Fatal(err) + } + client := Client{EntityID: requester, Peer: issuer, Role: metadata.AttributeAuthority, Metadata: snapshot, Signing: callerKey, Replay: replayStore(t), SOAP: binding.SOAPClient{Client: server.Client()}} + q := protocol.Query{ID: "_call", Kind: protocol.AttributeQuery, Subject: sub} + r, err := client.Query(t.Context(), q, at) + if err != nil || len(r.Assertions()) != 1 { + t.Fatalf("attribute SOAP: %v", err) + } + if _, err = client.Query(t.Context(), q, at); err == nil { + t.Fatal("same signed request executed twice") + } + if calls.Load() != 1 { + t.Fatalf("backend replayed: %d", calls.Load()) + } + q.ID = "_by_id" + q.Kind = protocol.AssertionRequest + q.Subject = nil + q.AssertionIDs = []string{"_held"} + if r, err = client.Query(t.Context(), q, at); err != nil || r.Assertions()[0].ID() != "_held" { + t.Fatalf("assertion ID SOAP: %v", err) + } + for _, test := range []struct { + kind protocol.QueryKind + role metadata.RoleType + }{{protocol.AuthenticationQuery, metadata.AuthenticationAuthority}, {protocol.AuthorizationQuery, metadata.PolicyDecisionPoint}} { + client.Role = test.role + q = protocol.Query{Kind: test.kind, Subject: sub} + if test.kind == protocol.AuthorizationQuery { + q.Resource = "urn:test:resource" + q.Actions = []protocol.Action{{Value: "Read"}} + } + if r, err = client.Query(t.Context(), q, at); err != nil || len(r.Assertions()) != 0 || r.Status().Code != protocol.Success { + t.Fatalf("empty %s: %v", test.kind, err) + } + } + client.Role = metadata.IdentityProvider + mapped, err := client.Map(t.Context(), protocol.NameIDMappingRequest{NameID: protocol.NameIdentifier{Value: "alice"}, Policy: protocol.NameIDPolicy{Format: protocol.NameIDPersistent, SPNameQualifier: "https://target.test/entity", AllowCreate: true}}, at) + if err != nil { + t.Fatal(err) + } + id, err := protocol.DecryptMappedNameID(t.Context(), mapped.Identifier(), &protocol.DecryptionOptions{Keys: []crypto.Decrypter{target}}) + if err != nil || id.Value != "target-alice" { + t.Fatalf("mapped target: %v", err) + } + client.Role = metadata.AttributeAuthority + client.URI = binding.URIClient{Client: &http.Client{Transport: basicTransport{base: server.Client().Transport}}} + a, err := client.Assertion(t.Context(), "_held", at) + if err != nil || a.ID() != "_held" { + t.Fatalf("URI assertion: %v", err) + } + if _, err = client.Assertion(t.Context(), "_absent", at); err == nil { + t.Fatal("unknown URI assertion accepted") + } + client.URI.Client = server.Client() + if _, err = client.Assertion(t.Context(), "_held", at); err == nil { + t.Fatal("unauthenticated URI disclosure") + } + if err = store.Add(t.Context(), assertion); err != nil { + t.Fatal(err) + } + other, _ := protocol.SignAuthorityAssertion(t.Context(), protocol.AuthorityAssertion{ID: "_held", Issuer: issuer, IssueInstant: at, Subject: sub}, key) + if err = store.Add(t.Context(), other); !errors.Is(err, ErrDenied) { + t.Fatal("URI rebound to different assertion") + } + q = protocol.Query{ID: "_bulk", Kind: protocol.AttributeQuery, Subject: sub} + if r, err = client.Query(t.Context(), q, at); err != nil || len(r.Assertions()) != len(bulk) { + t.Fatalf("large OneTimeUse response: %v", err) + } + q.ID = "_bulk_again" + if _, err = client.Query(t.Context(), q, at); !errors.Is(err, replay.ErrReplay) { + t.Fatalf("OneTimeUse assertion reused in a new response: %v", err) + } +} + +type basicTransport struct{ base http.RoundTripper } + +func (b basicTransport) RoundTrip(r *http.Request) (*http.Response, error) { + r = r.Clone(r.Context()) + r.SetBasicAuth("alice", "secret") + return b.base.RoundTrip(r) +} + +func TestAuthorityBackendFailures(t *testing.T) { + c := testcert.New(t) + key := protocol.SigningKey{Key: c.LeafKey} + keys := []crypto.PublicKey{c.LeafKey.Public()} + at := testcert.Now() + const endpoint = "https://authority.test/soap" + for _, test := range []struct { + name, code, subcode string + mapping bool + failure error + }{ + {name: "denied-mapping", mapping: true, failure: ErrDenied, code: protocol.Requester, subcode: "urn:oasis:names:tc:SAML:2.0:status:RequestDenied"}, + {name: "unknown-principal", mapping: true, failure: &protocol.StatusError{Code: protocol.Requester, Subcodes: []string{protocol.UnknownPrincipal}}, code: protocol.Requester, subcode: protocol.UnknownPrincipal}, + {name: "internal-query-error", failure: errors.New("private backend detail"), code: protocol.Responder}, + {name: "no-context", failure: &protocol.StatusError{Code: protocol.Responder, Subcodes: []string{protocol.NoAuthnContext}}, code: protocol.Responder, subcode: protocol.NoAuthnContext}, + {name: "canceled", failure: context.Canceled}, + } { + t.Run(test.name, func(t *testing.T) { + calls := 0 + service, err := NewService(Config{EntityID: issuer, Endpoint: endpoint, Signing: key, Replay: replayStore(t), Trust: func(context.Context, string, time.Time) ([]crypto.PublicKey, error) { return keys, nil }, Kinds: []protocol.QueryKind{protocol.AuthenticationQuery}, Query: func(context.Context, *protocol.VerifiedQuery) ([]*document.Document, error) { + calls++ + return nil, test.failure + }, Mapping: func(context.Context, *protocol.VerifiedNameIDMappingRequest) (*document.Document, error) { + calls++ + return nil, test.failure + }}) + if err != nil { + t.Fatal(err) + } + header := protocol.MessageHeader{ID: "_failure", Issuer: requester, Destination: endpoint, IssueInstant: at} + sub, _ := protocol.NewSubject(t.Context(), protocol.NameIdentifier{Value: "alice"}) + q := protocol.Query{MessageHeader: header, Kind: protocol.AuthenticationQuery, Subject: sub} + request, err := protocol.SignQuery(t.Context(), q, key) + if test.mapping { + request, err = protocol.SignNameIDMappingRequest(t.Context(), protocol.NameIDMappingRequest{MessageHeader: header, NameID: protocol.NameIdentifier{Value: "alice"}}, key) + } + if err != nil { + t.Fatal(err) + } + out, err := service.Respond(t.Context(), request, requester, at) + if errors.Is(test.failure, context.Canceled) { + if !errors.Is(err, context.Canceled) || out != nil { + t.Fatalf("cancellation hidden: %v", err) + } + } else { + if err != nil { + t.Fatal(err) + } + raw, _ := out.XML(t.Context()) + message, err := binding.DecodeSOAP(t.Context(), raw, binding.SOAPOptions{}) + if err != nil { + t.Fatal(err) + } + payload, _ := message.Payload(t.Context()) + o := protocol.ExchangeOptions{Issuer: issuer, ExpectedRequestID: header.ID, At: at, Binding: metadata.SOAP} + var status protocol.Status + if test.mapping { + r, err := protocol.VerifyNameIDMappingResponse(t.Context(), payload, keys, o) + if err != nil || r.Identifier() != nil { + t.Fatalf("mapping failure exposed identity: %v", err) + } + status = r.Status() + } else { + r, err := protocol.VerifyQueryResponse(t.Context(), payload, keys, q, protocol.QueryResponseOptions{ExchangeOptions: o}) + if err != nil || len(r.Assertions()) != 0 { + t.Fatalf("query failure exposed assertions: %v", err) + } + status = r.Status() + } + if status.Code != test.code || status.Message != "" || test.subcode != "" && (len(status.Subcodes) != 1 || status.Subcodes[0] != test.subcode) { + t.Fatalf("wrong failure status: %+v", status) + } + } + if _, err = service.Respond(t.Context(), request, requester, at); !errors.Is(err, replay.ErrReplay) || calls != 1 { + t.Fatalf("failed backend request executed again (%d): %v", calls, err) + } + }) + } +} + +type observedReadContext struct { + context.Context + checked chan struct{} + once sync.Once +} + +func (c *observedReadContext) Err() error { + err := c.Context.Err() + c.once.Do(func() { close(c.checked) }) + return err +} + +func TestMemoryAssertionsGetRechecksCancellation(t *testing.T) { + t.Parallel() + store, err := NewMemoryAssertions(1) + if err != nil { + t.Fatal(err) + } + doc, err := document.Parse(t.Context(), []byte(``), document.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + if err := store.Add(t.Context(), doc); err != nil { + t.Fatal(err) + } + ctx, cancel := context.WithCancel(t.Context()) + defer cancel() + observed := &observedReadContext{Context: ctx, checked: make(chan struct{})} + finished := make(chan error, 1) + store.mu.Lock() + go func() { + got, err := store.Get(observed, "_held") + if got != nil { + finished <- errors.New("canceled lookup returned an assertion") + return + } + finished <- err + }() + <-observed.checked + cancel() + store.mu.Unlock() + if err := <-finished; !errors.Is(err, context.Canceled) { + t.Fatalf("queued lookup returned %v; want context.Canceled", err) + } + if got, err := store.Get(t.Context(), "_held"); err != nil || got != doc { + t.Fatal("canceled lookup changed immutable assertion storage", got, err) + } +} diff --git a/saml/authority/client.go b/saml/authority/client.go new file mode 100644 index 0000000..856613c --- /dev/null +++ b/saml/authority/client.go @@ -0,0 +1,315 @@ +package authority + +import ( + "context" + "time" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/saml/binding" + "github.com/skarm/xmlsec/saml/internal/profile" + "github.com/skarm/xmlsec/saml/metadata" + "github.com/skarm/xmlsec/saml/protocol" + "github.com/skarm/xmlsec/saml/replay" +) + +// Separate one-time assertion IDs from request/envelope replay IDs in authority client scopes; equal peer-provided IDs must not consume another message class. +const ( + scopeAssertionSuffix = ":assertion" +) + +// Separate request IDs from response/assertion replay IDs in authority client scopes; correlation and atomic replay checks remain independent. +const ( + scopeRequestSuffix = ":request" +) + +// Client performs authenticated, correlated synchronous queries and mapping. +// Metadata is refreshed for each exchange. The HTTP transport supplies TLS and +// optional Basic/client-certificate authentication; credentials are never taken +// from received XML. Replay must be a shared atomic store in multi-instance use. +type Client struct { + // EntityID identifies the local requester and assertion audience. + EntityID string + // Peer is the expected authority entity ID. + Peer string + // Role selects the authority role in authenticated peer metadata. + Role metadata.RoleType + // Metadata resolves current trusted peer metadata for each exchange. + Metadata metadata.Resolver + // Signing supplies the request signing key and policy. + Signing protocol.SigningKey + // Replay atomically consumes correlation and one-time assertion IDs. + Replay replay.Store + // Options supplies response, assertion and query-matching policy. + Options protocol.QueryResponseOptions + // SOAP supplies the transport for queries and mapping requests. + SOAP binding.SOAPClient + // URI supplies the transport for assertion-ID retrieval. + URI binding.URIClient +} + +// Query sends one of the three queries or an assertion-ID request. It verifies +// every result and atomically consumes response/request and OneTimeUse IDs. +func (c Client) Query(ctx context.Context, q protocol.Query, at time.Time) (*protocol.VerifiedQueryResponse, error) { + started := time.Now() + + endpoint, role, err := c.endpoint(ctx, queryService(q.Kind), metadata.SOAP, at) + if err != nil { + return nil, err + } + + q.MessageHeader, err = c.header(q.MessageHeader, endpoint, at) + if err != nil { + return nil, err + } + + d, err := protocol.SignQuery(ctx, q, c.Signing) + if err != nil { + return nil, err + } + + d, err = c.roundTrip(ctx, endpoint, d) + if err != nil { + return nil, err + } + + now := at.Add(time.Since(started)) + if !now.Before(role.ExpiresAt()) { + return nil, metadata.ErrExpired + } + + o := c.Options + o.Issuer = c.Peer + o.ExpectedRequestID = q.ID + o.At = now + o.Binding = metadata.SOAP + o.Audience = c.EntityID + + r, err := protocol.VerifyQueryResponse(ctx, d, role.SigningKeys(), q, o) + if err != nil { + return nil, err + } + + age, _, skew, err := protocol.AcceptanceWindow(o.Limits) + if err != nil { + return nil, err + } + + end := now.Add(age + 2*skew) + + entries := []replay.Entry{{Scope: scope(c.EntityID, c.Peer), ID: r.Header().ID, ExpiresAt: end}, {Scope: scope(c.EntityID, c.Peer) + scopeRequestSuffix, ID: q.ID, ExpiresAt: end}} + for _, a := range r.Assertions() { + if a.OneTimeUse() { + if a.NotOnOrAfter().IsZero() { + return nil, protocol.ErrConditions + } + + entries = append(entries, replay.Entry{Scope: scope(c.EntityID, a.Issuer()) + scopeAssertionSuffix, ID: a.ID(), ExpiresAt: a.NotOnOrAfter().Add(skew)}) + } + } + + if c.Replay == nil { + return nil, ErrDenied + } + + if err = c.Replay.Consume(ctx, entries, now); err != nil { + return nil, err + } + + return r, nil +} + +// Map requests a NameID mapping from a trusted IdP's NameIDMappingService. +// Successful encrypted output is intentionally opaque and can be forwarded to +// the target SP. It does not establish a local login or change an existing NameID. +func (c Client) Map(ctx context.Context, q protocol.NameIDMappingRequest, at time.Time) (*protocol.VerifiedNameIDMappingResponse, error) { + started := time.Now() + + endpoint, role, err := c.endpoint(ctx, metadata.NameIDMappingService, metadata.SOAP, at) + if err != nil { + return nil, err + } + + q.MessageHeader, err = c.header(q.MessageHeader, endpoint, at) + if err != nil { + return nil, err + } + + d, err := protocol.SignNameIDMappingRequest(ctx, q, c.Signing) + if err != nil { + return nil, err + } + + d, err = c.roundTrip(ctx, endpoint, d) + if err != nil { + return nil, err + } + + o := c.Options.ExchangeOptions + o.Issuer = c.Peer + o.ExpectedRequestID = q.ID + o.At = at.Add(time.Since(started)) + + o.Binding = metadata.SOAP + if !o.At.Before(role.ExpiresAt()) { + return nil, metadata.ErrExpired + } + + r, err := protocol.VerifyNameIDMappingResponse(ctx, d, role.SigningKeys(), o) + if err != nil { + return nil, err + } + + if r.Status().Code == protocol.Success { + if err = protocol.CheckMappedPolicy(ctx, r.Identifier(), q.Policy); err != nil { + return nil, err + } + } + + if c.Replay == nil { + return nil, ErrDenied + } + + age, _, skew, err := protocol.AcceptanceWindow(o.Limits) + if err != nil { + return nil, err + } + + err = c.Replay.Consume(ctx, []replay.Entry{{Scope: scope(c.EntityID, c.Peer), ID: r.Header().ID, ExpiresAt: o.At.Add(age + 2*skew)}, {Scope: scope(c.EntityID, c.Peer) + scopeRequestSuffix, ID: q.ID, ExpiresAt: o.At.Add(age + 2*skew)}}, o.At) + + return r, err +} + +// Assertion retrieves an existing assertion using the URI binding and verifies +// its signature and conditions. AssertionTrust can authorize a separate issuer. +func (c Client) Assertion(ctx context.Context, id string, at time.Time) (*protocol.VerifiedAuthorityAssertion, error) { + started := time.Now() + + endpoint, role, err := c.endpoint(ctx, metadata.AssertionIDRequestService, metadata.URI, at) + if err != nil { + return nil, err + } + + d, err := c.URI.Resolve(ctx, endpoint, id) + if err != nil { + return nil, err + } + + now := at.Add(time.Since(started)) + if !now.Before(role.ExpiresAt()) { + return nil, metadata.ErrExpired + } + + who := c.Peer + keys := role.SigningKeys() + + if c.Options.AssertionTrust != nil { + cs := d.Root().Children() + if len(cs) == 0 { + return nil, ErrDenied + } + + who = cs[0].Text() + + keys, err = c.Options.AssertionTrust(ctx, who) + if err != nil { + return nil, err + } + } + + a, err := protocol.VerifyAuthorityAssertion(ctx, d, keys, protocol.AssertionOptions{Issuer: who, Audience: c.EntityID, At: now, Limits: c.Options.Limits, Policy: c.Options.Policy, Decryption: c.Options.Decryption}) + if err != nil { + return nil, err + } + + if a.OneTimeUse() { + if c.Replay == nil || a.NotOnOrAfter().IsZero() { + return nil, ErrDenied + } + + _, _, skew, err := protocol.AcceptanceWindow(c.Options.Limits) + if err != nil { + return nil, err + } + + err = c.Replay.Consume(ctx, []replay.Entry{{Scope: scope(c.EntityID, a.Issuer()) + scopeAssertionSuffix, ID: id, ExpiresAt: a.NotOnOrAfter().Add(skew)}}, now) + if err != nil { + return nil, err + } + } + + return a, nil +} + +func (c Client) roundTrip(ctx context.Context, endpoint string, d *document.Document) (*document.Document, error) { + envelope, err := protocol.SignSOAP(ctx, d, c.Signing) + if err != nil { + return nil, err + } + + m, err := c.SOAP.RoundTrip(ctx, endpoint, envelope) + if err != nil { + return nil, err + } + + return m.Payload(ctx) +} + +func (c Client) header(h protocol.MessageHeader, endpoint string, at time.Time) (protocol.MessageHeader, error) { + if h.Issuer != "" && h.Issuer != c.EntityID || h.Destination != "" && h.Destination != endpoint || !profile.URI(c.EntityID) { + return h, ErrDenied + } + + if h.ID == "" { + var err error + + h.ID, err = profile.NewID() + if err != nil { + return h, err + } + } + + h.Issuer = c.EntityID + h.Destination = endpoint + h.IssueInstant = at + + return h, nil +} + +func (c Client) endpoint(ctx context.Context, service, bindingURI string, at time.Time) (string, *metadata.Role, error) { + if c.Metadata == nil || service == "" || ctx == nil || at.IsZero() { + return "", nil, ErrDenied + } + + entity, err := c.Metadata.Lookup(ctx, c.Peer, at) + if err != nil { + return "", nil, err + } + + role, err := entity.Role(c.Role, at) + if err != nil { + return "", nil, err + } + + for _, e := range role.Endpoints(service) { + if e.Binding == bindingURI { + return e.Location, role, nil + } + } + + return "", nil, ErrDenied +} + +func queryService(k protocol.QueryKind) string { + switch k { + case protocol.AuthenticationQuery: + return metadata.AuthnQueryService + case protocol.AttributeQuery: + return metadata.AttributeService + case protocol.AuthorizationQuery: + return metadata.AuthzService + case protocol.AssertionRequest: + return metadata.AssertionIDRequestService + default: + return "" + } +} diff --git a/saml/authority/uri.go b/saml/authority/uri.go new file mode 100644 index 0000000..3ad99a5 --- /dev/null +++ b/saml/authority/uri.go @@ -0,0 +1,184 @@ +package authority + +import ( + "context" + "errors" + "net/http" + "sync" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/lexicon" + "github.com/skarm/xmlsec/saml/binding" + "github.com/skarm/xmlsec/saml/internal/profile" +) + +// ErrNotFound indicates that an assertion is not held by the authority. +var ErrNotFound = errors.New("SAML assertion not found") + +// URIHandler serves the assertion URI binding. Authenticate establishes the +// HTTP principal (or an explicitly permitted anonymous principal). Lookup must +// authorize disclosure to that principal and preserve the URI-to-assertion +// association forever: an ID may return the same assertion or an error, never +// a replacement. Signed assertions must be supplied by the application. +type URIHandler struct { + // Authenticate returns the HTTP principal allowed to request assertions. It is + // required and must explicitly permit anonymous access if desired. + Authenticate func(*http.Request) (string, error) + // Lookup receives the principal and exact assertion ID. It must authorize + // disclosure and return the immutable signed assertion, or ErrNotFound. + Lookup func(context.Context, string, string) (*document.Document, error) +} + +// ServeHTTP returns 403 for denied requests, 404 for unknown assertions and +// application/samlassertion+xml with no-store for an authorized exact-ID lookup. +func (h URIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + const ( + cacheControlNoStore = "no-cache, no-store" + cachePragma = "no-cache" + headerCacheControl = "Cache-Control" + headerPragma = "Pragma" + ) + + w.Header().Set(headerCacheControl, cacheControlNoStore) + w.Header().Set(headerPragma, cachePragma) + + id, err := binding.ReadAssertionID(r) + if err != nil { + http.Error(w, "invalid assertion request", http.StatusBadRequest) + return + } + + if h.Authenticate == nil || h.Lookup == nil { + http.Error(w, "assertion access denied", http.StatusForbidden) + return + } + + peer, err := h.Authenticate(r) + if err != nil { + http.Error(w, "assertion access denied", http.StatusForbidden) + return + } + + d, err := h.Lookup(r.Context(), peer, id) + if errors.Is(err, ErrNotFound) { + http.NotFound(w, r) + return + } + + if err != nil { + http.Error(w, "assertion access denied", http.StatusForbidden) + return + } + + if d == nil { + http.Error(w, "assertion unavailable", http.StatusInternalServerError) + return + } + + got, _ := d.Root().Attribute(document.Name{Local: lexicon.SAMLAttrID}) + if got != id { + http.Error(w, "assertion unavailable", http.StatusInternalServerError) + return + } + + if err = binding.WriteAssertion(r.Context(), w, d); err != nil { + return + } +} + +// MemoryAssertions is a bounded immutable-ID store. It deliberately has no +// replacement or eviction operation: capacity exhaustion cannot rebind an old +// URI. A durable implementation must retain the same immutability contract. +// Authorization is separate; Get must be wrapped in a principal-specific policy. +type MemoryAssertions struct { + // mu protects concurrent lookup and insertion. + mu sync.RWMutex + // capacity bounds retained immutable assertion IDs. + capacity int + // values permanently binds each stored ID to one immutable assertion. + values map[string]*document.Document +} + +// NewMemoryAssertions allocates a store with an explicit positive capacity. +func NewMemoryAssertions(capacity int) (*MemoryAssertions, error) { + const ( + maximumAssertionStoreCapacity = 100000 + ) + + if capacity < 1 || capacity > maximumAssertionStoreCapacity { + return nil, ErrDenied + } + + return &MemoryAssertions{capacity: capacity, values: make(map[string]*document.Document)}, nil +} + +// Add associates an exact assertion once. Re-adding the same immutable snapshot +// is idempotent; an attempt to replace it, even with another document having the +// same ID, fails. Only insert assertions that the application has authenticated. +func (m *MemoryAssertions) Add(ctx context.Context, d *document.Document) error { + const ( + xmlNameAssertion = "Assertion" + ) + + if m == nil || ctx == nil || d == nil || d.Root().Name() != (document.Name{Space: lexicon.NamespaceSAMLAssertion, Local: xmlNameAssertion}) { + return ErrDenied + } + + id, _ := d.Root().Attribute(document.Name{Local: lexicon.SAMLAttrID}) + if !profile.ID(id) { + return ErrDenied + } + + if err := ctx.Err(); err != nil { + return err + } + + m.mu.Lock() + defer m.mu.Unlock() + + if err := ctx.Err(); err != nil { + return err + } + + if old, exists := m.values[id]; exists { + if old == d { + return nil + } + + return ErrDenied + } + + if len(m.values) >= m.capacity { + return ErrDenied + } + + m.values[id] = d + + return nil +} + +// Get returns the immutable snapshot, without authorizing disclosure to a peer. +func (m *MemoryAssertions) Get(ctx context.Context, id string) (*document.Document, error) { + if m == nil || ctx == nil || !profile.ID(id) { + return nil, ErrDenied + } + + if err := ctx.Err(); err != nil { + return nil, err + } + + m.mu.RLock() + defer m.mu.RUnlock() + + if err := ctx.Err(); err != nil { + return nil, err + } + + d := m.values[id] + + if d == nil { + return nil, ErrNotFound + } + + return d, nil +} diff --git a/saml/binding/artifact.go b/saml/binding/artifact.go new file mode 100644 index 0000000..d718ab6 --- /dev/null +++ b/saml/binding/artifact.go @@ -0,0 +1,200 @@ +package binding + +import ( + "crypto/rand" + "crypto/sha1" // #nosec G505 -- SHA-1 is the SAML type 0x0004 source identifier, not a signature. + "encoding/base64" + "encoding/binary" + "html" + "net/url" + "unicode/utf8" + + "github.com/skarm/xmlsec/saml/internal/profile" +) + +// Fixed type 0x0004 binary layout: two-octet type, two-octet endpoint index, twenty-octet source and twenty-octet opaque handle; parsing and emission must stay identical. +const ( + artifactEndpointOffset = 2 + artifactHandleOffset = 24 + artifactIdentifierBytes = 20 + artifactSourceOffset = 4 + artifactTypeCode = 4 + artifactWireBytes = 44 +) + +// The HTTP Artifact field carries an opaque type 0x0004 lookup handle; decoding it supplies no message or peer authentication. +const ( + fieldArtifact = "SAMLart" +) + +// Artifact is the fixed 44-octet SAML type 0x0004 artifact (Bindings 3.6.4.1). +// The opaque handle is a random lookup key, never an encoded SAML message. +type Artifact struct { + // EndpointIndex selects an ArtifactResolutionService in trusted issuer + // metadata. + EndpointIndex uint16 + // SourceID identifies the issuing provider for routing; it does not + // authenticate it. + SourceID [artifactIdentifierBytes]byte + // MessageHandle is the unpredictable, single-use message identifier. + MessageHandle [artifactIdentifierBytes]byte +} + +// NewArtifact generates an unpredictable handle and the recommended SHA-1 source +// identifier from the issuing provider's entityID. +func NewArtifact(entityID string, index uint16) (Artifact, error) { + if !profile.URI(entityID) { + return Artifact{}, ErrBinding + } + + a := Artifact{EndpointIndex: index, SourceID: ArtifactSource(entityID)} + _, err := rand.Read(a.MessageHandle[:]) + + return a, err +} + +// ArtifactSource computes the source identifier mandated by the selected +// entityID-based artifact profile. SHA-1 here is a routing identifier only. +func ArtifactSource(entityID string) [artifactIdentifierBytes]byte { return sha1.Sum([]byte(entityID)) } // #nosec G401 -- Interoperable SAML artifact routing identifier. + +// String returns the standard Base64 representation, including padding. +func (a Artifact) String() string { + var b [artifactWireBytes]byte + binary.BigEndian.PutUint16(b[:artifactEndpointOffset], artifactTypeCode) + binary.BigEndian.PutUint16(b[artifactEndpointOffset:artifactSourceOffset], a.EndpointIndex) + copy(b[artifactSourceOffset:artifactHandleOffset], a.SourceID[:]) + copy(b[artifactHandleOffset:], a.MessageHandle[:]) + + return base64.StdEncoding.EncodeToString(b[:]) +} + +// ParseArtifact decodes exactly one canonical type 0x0004 artifact. +func ParseArtifact(value string) (Artifact, error) { + const ( + artifactEncodedBytes = 60 + ) + + var a Artifact + if len(value) != artifactEncodedBytes { + return a, ErrBinding + } + + b, err := base64.StdEncoding.Strict().DecodeString(value) + if err != nil || len(b) != artifactWireBytes || binary.BigEndian.Uint16(b[:artifactEndpointOffset]) != artifactTypeCode { + return a, ErrBinding + } + + a.EndpointIndex = binary.BigEndian.Uint16(b[artifactEndpointOffset:artifactSourceOffset]) + copy(a.SourceID[:], b[artifactSourceOffset:artifactHandleOffset]) + copy(a.MessageHandle[:], b[artifactHandleOffset:]) + + return a, nil +} + +// EncodeArtifact returns query/form fields for the HTTP Artifact binding. GET +// and POST use identical fields. RelayState is an opaque value of at most 80 bytes. +func EncodeArtifact(a Artifact, relay *string) (string, error) { + if relay != nil && (len(*relay) > profile.HTTPRelayStateMaxBytes || !utf8.ValidString(*relay)) { + return "", ErrBinding + } + + v := url.Values{fieldArtifact: {a.String()}} + if relay != nil { + v.Set(fieldRelayState, *relay) + } + + return v.Encode(), nil +} + +// DecodeArtifact rejects duplicate SAMLart/RelayState and mixed message bindings. +// Other query parameters are preserved by the surrounding application. +func DecodeArtifact(form string) (Artifact, *string, error) { + const ( + maximumArtifactFormBytes = 16 << 10 + ) + + if len(form) > maximumArtifactFormBytes { + return Artifact{}, nil, ErrBinding + } + + v, err := url.ParseQuery(form) + if err != nil || len(v[fieldArtifact]) != 1 || len(v[fieldRelayState]) > 1 || len(v[string(Request)])+len(v[string(Response)])+len(v[nameSignature])+len(v[fieldSignatureAlgorithm]) != 0 { + return Artifact{}, nil, ErrBinding + } + + a, err := ParseArtifact(v.Get(fieldArtifact)) + if err != nil { + return a, nil, err + } + + var relay *string + + if values, ok := v[fieldRelayState]; ok { + if len(values[0]) > profile.HTTPRelayStateMaxBytes || !utf8.ValidString(values[0]) { + return a, nil, ErrBinding + } + + relay = &values[0] + } + + return a, relay, nil +} + +// ArtifactURL constructs a GET delivery URL at an application-trusted endpoint. +func ArtifactURL(endpoint string, a Artifact, relay *string) (string, error) { + if !profile.Endpoint(endpoint, false) { + return "", ErrBinding + } + + u, err := url.Parse(endpoint) + if err != nil { + return "", ErrBinding + } + + v, err := url.ParseQuery(u.RawQuery) + if err != nil { + return "", ErrBinding + } + + for _, name := range []string{fieldArtifact, fieldRelayState, string(Request), string(Response), fieldSignatureAlgorithm, nameSignature} { + if _, ok := v[name]; ok { + return "", ErrBinding + } + } + + query, err := EncodeArtifact(a, relay) + if err != nil { + return "", err + } + + if u.RawQuery != "" { + u.RawQuery += "&" + } + + u.RawQuery += query + + return u.String(), nil +} + +// ArtifactForm renders a passive POST form without executable scripts. +// RelayState must be UTF-8 without control characters, as for WritePOSTForm. +func ArtifactForm(endpoint string, a Artifact, relay *string) ([]byte, error) { + const ( + htmlArtifactFormPrefix = "SAML artifact
` + if relay != nil { + form += htmlRelayInputPrefix + html.EscapeString(*relay) + `">` + } + + return []byte(form + htmlFormSuffix), nil +} diff --git a/saml/binding/artifact_test.go b/saml/binding/artifact_test.go new file mode 100644 index 0000000..0210800 --- /dev/null +++ b/saml/binding/artifact_test.go @@ -0,0 +1,99 @@ +package binding_test + +import ( + "bytes" + "encoding/base64" + "errors" + "net/url" + "strings" + "testing" + + "github.com/skarm/xmlsec/saml/binding" +) + +// Bindings 3.6.4.1: 2-octet type, 2-octet network-order endpoint index, +// 20-octet SourceID and 20-octet MessageHandle. This vector is assembled directly. +func TestArtifactWireAndHTTP(t *testing.T) { + wire := append([]byte{0, 4, 1, 2}, bytes.Repeat([]byte{0x31}, 20)...) + wire = append(wire, bytes.Repeat([]byte{0x42}, 20)...) + encoded := base64.StdEncoding.EncodeToString(wire) + a, err := binding.ParseArtifact(encoded) + if err != nil || a.EndpointIndex != 258 || a.SourceID[0] != 0x31 || a.MessageHandle[19] != 0x42 || a.String() != encoded { + t.Fatalf("artifact vector: %v", err) + } + relay := "opaque +/&" + form, err := binding.EncodeArtifact(a, &relay) + if err != nil { + t.Fatal(err) + } + decoded, state, err := binding.DecodeArtifact(form) + if err != nil || decoded != a || state == nil || *state != relay { + t.Fatal("form/query round trip") + } + for _, bad := range []string{form + "&SAMLart=" + url.QueryEscape(encoded), form + "&RelayState=other", form + "&SAMLResponse=bad", form + "&SigAlg=bad", "SAMLart=%invalid"} { + if _, _, err := binding.DecodeArtifact(bad); err == nil { + t.Fatalf("accepted ambiguous/bad artifact %q", bad) + } + } + for _, bad := range []string{"", encoded + "=", base64.StdEncoding.EncodeToString(append([]byte{0, 3}, wire[2:]...)), strings.Repeat("A", 60)} { + if _, err := binding.ParseArtifact(bad); err == nil { + t.Fatal("invalid artifact") + } + } + if _, err := binding.ArtifactURL("https://sp.test/acs?SAMLart=existing", a, nil); err == nil { + t.Fatal("parameter collision") + } + long := strings.Repeat("x", 81) + if _, err := binding.EncodeArtifact(a, &long); err == nil { + t.Fatal("RelayState length") + } + x, err := binding.NewArtifact("https://idp.test/entity", 0) + if err != nil { + t.Fatal(err) + } + y, err := binding.NewArtifact("https://idp.test/entity", 0) + if err != nil || x.SourceID != y.SourceID || x.MessageHandle == y.MessageHandle { + t.Fatal("source/handle generation") + } +} + +func FuzzArtifact(f *testing.F) { + f.Add("AAQAADE x") + f.Add(base64.StdEncoding.EncodeToString(make([]byte, 44))) + f.Fuzz(func(t *testing.T, input string) { + a, err := binding.ParseArtifact(input) + if err == nil && a.String() != input { + t.Fatal("accepted noncanonical artifact") + } + }) +} + +func TestArtifactFormRejectsUnstableRelayState(t *testing.T) { + t.Parallel() + a := binding.Artifact{MessageHandle: [20]byte{1}} + for _, value := range []string{"state\x00suffix", "state\rsuffix", "state\nsuffix"} { + form, err := binding.ArtifactForm("https://sp.test/acs", a, &value) + if form != nil || !errors.Is(err, binding.ErrBinding) { + t.Fatalf("HTML form accepted RelayState %q whose value changes in a browser: %v", value, err) + } + // The URL-encoded carrier preserves these opaque bytes and remains valid. + encoded, err := binding.EncodeArtifact(a, &value) + if err != nil { + t.Fatal(err) + } + _, relay, err := binding.DecodeArtifact(encoded) + if err != nil || relay == nil || *relay != value { + t.Fatal("raw artifact carrier changed opaque RelayState", relay, err) + } + } + for _, value := range []string{strings.Repeat("x", 81), "\xff"} { + if form, err := binding.ArtifactForm("https://sp.test/acs", a, &value); form != nil || !errors.Is(err, binding.ErrBinding) { + t.Fatalf("invalid HTML RelayState %q accepted: %v", value, err) + } + } + for _, value := range []string{`opaque +/&"' <é>`, strings.Repeat("x", 80)} { + if _, err := binding.ArtifactForm("https://sp.test/acs", a, &value); err != nil { + t.Fatal("printable RelayState rejected", err) + } + } +} diff --git a/saml/binding/doc.go b/saml/binding/doc.go index 6546a52..57d8787 100644 --- a/saml/binding/doc.go +++ b/saml/binding/doc.go @@ -1,7 +1,7 @@ -// Package binding encodes SAML HTTP-POST forms and signs or verifies the -// HTTP-Redirect DEFLATE binding. It handles transport representation and -// binding integrity, with separate protocol and session checks in higher-level -// packages. +// Package binding handles SAML HTTP-POST forms, signed HTTP-Redirect queries, +// SOAP 1.1 transport, type 0x0004 artifacts and assertion URI retrieval. +// It checks transport representation and configured binding integrity; +// higher-level packages apply protocol and session checks. // // # HTTP-POST // @@ -35,4 +35,9 @@ // before returning. HTTP-Redirect queries remain complete strings for their // signed wire representation. Inflation and XML parsing run only after // signature verification. Protocol XML snapshots remain materialized. +// +// SOAP 1.1 parsing enforces a single body element, actors, mandatory header +// processing and byte/XML budgets. SOAPClient never follows HTTP redirects. +// Parsing SOAP authenticates nothing. Type 0x0004 artifact helpers encode the +// fixed wire format and GET/POST fields; saml/artifact provides atomic resolution. package binding diff --git a/saml/binding/post.go b/saml/binding/post.go index c6773b5..3e20f37 100644 --- a/saml/binding/post.go +++ b/saml/binding/post.go @@ -17,6 +17,19 @@ import ( "github.com/skarm/xmlsec/saml/internal/xmlutil" ) +// Fixed HTML form template shared by POST and Artifact output; dynamic endpoint and relay data still require escaping and preflight validation. +const ( + htmlFormSuffix = "
" + htmlHiddenInputPrefix = `">SAML message
SAML message` if relay != nil { - suffix += `` + suffix += htmlRelayInputPrefix + html.EscapeString(*relay) + `">` } - suffix += `
` + suffix += htmlFormSuffix _, err = writer.WriteString(suffix) return writer.Written, errors.Join(err, ctx.Err()) } func postLimits(l POSTLimits) (POSTLimits, error) { + const ( + defaultPOSTFormBytes = 2 << 20 + defaultPOSTXMLBytes = 1 << 20 + maximumPOSTFormBytes = 16 << 20 + maximumPOSTXMLBytes = 8 << 20 + ) + if l.MaxFormBytes == 0 { - l.MaxFormBytes = 2 << 20 + l.MaxFormBytes = defaultPOSTFormBytes } if l.MaxXMLBytes == 0 { - l.MaxXMLBytes = 1 << 20 + l.MaxXMLBytes = defaultPOSTXMLBytes } - if l.MaxFormBytes < 1 || l.MaxFormBytes > 16<<20 || l.MaxXMLBytes < 1 || l.MaxXMLBytes > 8<<20 { + if l.MaxFormBytes < 1 || l.MaxFormBytes > maximumPOSTFormBytes || l.MaxXMLBytes < 1 || l.MaxXMLBytes > maximumPOSTXMLBytes { return l, ErrBinding } @@ -199,13 +211,38 @@ func postLimits(l POSTLimits) (POSTLimits, error) { } func postArguments(ctx context.Context, dst io.Writer, doc *document.Document, p Parameter, relay *string) error { - if ctx == nil || nilcheck.IsNil(dst) || doc == nil || p != Request && p != Response || relay != nil && len(*relay) > 80 { + if ctx == nil || nilcheck.IsNil(dst) || doc == nil || p != Request && p != Response || relay != nil && len(*relay) > profile.HTTPRelayStateMaxBytes { return ErrBinding } return ctx.Err() } +// HTML parsing and form submission can replace NUL and normalize line endings. +// Reject control characters instead of silently changing opaque correlation data. +func validHTMLRelay(relay *string) bool { + const ( + deleteControlRune = 127 + firstPrintableASCII = 32 + ) + + if relay == nil { + return true + } + + if len(*relay) > profile.HTTPRelayStateMaxBytes || !utf8.ValidString(*relay) { + return false + } + + for _, r := range *relay { + if r < firstPrintableASCII || r == deleteControlRune { + return false + } + } + + return true +} + func writePOSTXML(ctx context.Context, dst io.Writer, doc *document.Document, maximum int) error { encoder := base64.NewEncoder(base64.StdEncoding, dst) if _, err := doc.WriteXML(ctx, encoder, maximum); err != nil { @@ -222,7 +259,7 @@ func writePOSTXML(ctx context.Context, dst io.Writer, doc *document.Document, ma // queryBase64Writer URL-escapes the Base64 alphabet in fixed storage. type queryBase64Writer struct { dst io.Writer - buffer [4096]byte + buffer [postEscapeBufferBytes]byte } func (w *queryBase64Writer) Write(source []byte) (int, error) { diff --git a/saml/binding/post_reader.go b/saml/binding/post_reader.go index 3d7ab97..7fcdc5c 100644 --- a/saml/binding/post_reader.go +++ b/saml/binding/post_reader.go @@ -8,6 +8,8 @@ import ( "io" "net/url" + "github.com/skarm/xmlsec/saml/internal/profile" + "github.com/skarm/xmlsec/document" "github.com/skarm/xmlsec/internal/nilcheck" "github.com/skarm/xmlsec/internal/streamio" @@ -18,6 +20,10 @@ import ( // bytes. Every field is checked before returning an unauthenticated message. // It leaves src open; blocking readers must honor ctx themselves. func DecodePOSTReader(ctx context.Context, src io.Reader, expected Parameter, limits POSTLimits) (*POSTMessage, error) { + const ( + expectedPOSTFieldCount = 2 + ) + l, err := postLimits(limits) if err != nil { return nil, err @@ -33,7 +39,7 @@ func DecodePOSTReader(ctx context.Context, src io.Reader, expected Parameter, li input := bufio.NewReader(&streamio.Reader{Context: ctx, Source: src, Limit: int64(l.MaxFormBytes), LimitError: ErrBinding}) message := &POSTMessage{parameter: expected} - seen := make(map[string]struct{}, 2) + seen := make(map[string]struct{}, expectedPOSTFieldCount) for { if _, err := input.Peek(1); err != nil { @@ -90,12 +96,12 @@ func DecodePOSTReader(ctx context.Context, src io.Reader, expected Parameter, li } func readPOSTRelay(message *POSTMessage, value io.Reader) error { - raw, err := io.ReadAll(io.LimitReader(value, 81)) + raw, err := io.ReadAll(io.LimitReader(value, profile.HTTPRelayStateMaxBytes+1)) if err != nil { return err } - if len(raw) > 80 { + if len(raw) > profile.HTTPRelayStateMaxBytes { return ErrBinding } @@ -106,7 +112,11 @@ func readPOSTRelay(message *POSTMessage, value io.Reader) error { } func postFieldKey(input *bufio.Reader) (string, bool, error) { - var encoded [64]byte + const ( + encodedPOSTFieldBufferBytes = 64 + ) + + var encoded [encodedPOSTFieldBufferBytes]byte used := 0 diff --git a/saml/binding/redirect.go b/saml/binding/redirect.go index 4de3162..04d9a6a 100644 --- a/saml/binding/redirect.go +++ b/saml/binding/redirect.go @@ -12,6 +12,8 @@ import ( "net/url" "strings" + "github.com/skarm/xmlsec/saml/internal/profile" + "github.com/skarm/xmlsec/document" "github.com/skarm/xmlsec/internal/lexicon" @@ -20,9 +22,44 @@ import ( "github.com/skarm/xmlsec/xmldsig" ) +// Decoded Redirect signature byte ceiling shared by Base64 decoding and encoded-query preflight; canonical XML has an independent budget. +const ( + maximumRedirectSignatureBytes = 2048 +) + +// Poll cancellation every 4096 encoded query octets. This bit mask bounds uninterrupted scanning work; it is not a query-size limit. +const ( + contextPollMask = 4095 +) + +// Retain at most four idle DEFLATE writers. Busy callers never wait for a cache slot, and Reset detaches the previous output. +const ( + idleRedirectCompressors = 4 +) + +// SigAlg is a signed Redirect query field and a forbidden collision in Artifact URLs; never normalize the received signed octets. +const ( + fieldSignatureAlgorithm = "SigAlg" +) + +// The optional Redirect codec declaration must name the fixed raw-DEFLATE encoding; it never enables an arbitrary decompressor. +const ( + fieldSAMLEncoding = "SAMLEncoding" +) + +// These separators are part of the exact Redirect signature input. Sign and verify retain field order and original encoded octets rather than normalizing the query. +const ( + relayQueryPrefix = "&" + fieldRelayState + "=" + signatureAlgorithmQueryPrefix = "&" + fieldSignatureAlgorithm + "=" +) + const ( + // Opaque HTTP binding correlation data, never a return URL or an identity; + // all codecs apply the shared byte ceiling before publishing the value. fieldRelayState = "RelayState" - nameSignature = "Signature" + // Redirect query field, distinct from the XMLDSig element of the same name. + // This field is excluded from the exact octets that its value authenticates. + nameSignature = "Signature" ) // ProtocolNamespace is the SAML 2.0 protocol namespace URI. @@ -46,7 +83,7 @@ var ( // The retained compression workspace is bounded independently of caller input. // //nolint:gochecknoglobals // Bounded workspace reuse, not a shared protocol policy. -var redirectCompressors = make(chan *flate.Writer, 4) +var redirectCompressors = make(chan *flate.Writer, idleRedirectCompressors) // Kind identifies the SAMLRequest or SAMLResponse field in a supported binding. type Kind string @@ -133,7 +170,7 @@ func VerifyRedirect(ctx context.Context, rawQuery string, key crypto.PublicKey, kind, msg = Response, response } - sm, ok := params["SigAlg"] + sm, ok := params[fieldSignatureAlgorithm] if !ok || sm.value == "" { return nil, ErrEncoding } @@ -143,23 +180,23 @@ func VerifyRedirect(ctx context.Context, rawQuery string, key crypto.PublicKey, return nil, ErrEncoding } - if enc, ok := params["SAMLEncoding"]; ok && enc.value != DeflateEncoding { + if enc, ok := params[fieldSAMLEncoding]; ok && enc.value != DeflateEncoding { return nil, ErrPolicy } relay, hasRelay := params[fieldRelayState] - if hasRelay && len(relay.value) > 80 { + if hasRelay && len(relay.value) > profile.HTTPRelayStateMaxBytes { return nil, ErrPolicy } signedParts := []io.Reader{strings.NewReader(string(kind) + "="), strings.NewReader(msg.raw)} if hasRelay { - signedParts = append(signedParts, strings.NewReader("&RelayState="), strings.NewReader(relay.raw)) + signedParts = append(signedParts, strings.NewReader(relayQueryPrefix), strings.NewReader(relay.raw)) } - signedParts = append(signedParts, strings.NewReader("&SigAlg="), strings.NewReader(sm.raw)) + signedParts = append(signedParts, strings.NewReader(signatureAlgorithmQueryPrefix), strings.NewReader(sm.raw)) - signature, err := decodeBase64(sv.value, 2048) + signature, err := decodeBase64(sv.value, maximumRedirectSignatureBytes) if err != nil { return nil, err } @@ -252,6 +289,10 @@ func (r *RedirectResult) SignatureMethod() string { // Any direct ds:Signature on the protocol root is removed in a private snapshot; // nested signatures remain. RelayState nil and a present empty string differ. func SignRedirect(ctx context.Context, d *document.Document, kind Kind, relay *string, signer crypto.Signer, opt Options) (string, error) { + const ( + signatureQueryPrefix = "&" + nameSignature + "=" + ) + if ctx == nil { return "", ErrPolicy } @@ -274,7 +315,7 @@ func SignRedirect(ctx context.Context, d *document.Document, kind Kind, relay *s return "", ErrEncoding } - if relay != nil && len(*relay) > 80 { + if relay != nil && len(*relay) > profile.HTTPRelayStateMaxBytes { return "", fmt.Errorf("%w: RelayState exceeds 80 bytes", ErrPolicy) } @@ -336,12 +377,12 @@ func SignRedirect(ctx context.Context, d *document.Document, kind Kind, relay *s } if relay != nil { - if _, err := queryWriter.WriteString("&RelayState=" + url.QueryEscape(*relay)); err != nil { + if _, err := queryWriter.WriteString(relayQueryPrefix + url.QueryEscape(*relay)); err != nil { return "", err } } - if _, err := queryWriter.WriteString("&SigAlg=" + url.QueryEscape(opt.SignatureMethod)); err != nil { + if _, err := queryWriter.WriteString(signatureAlgorithmQueryPrefix + url.QueryEscape(opt.SignatureMethod)); err != nil { return "", err } @@ -350,7 +391,7 @@ func SignRedirect(ctx context.Context, d *document.Document, kind Kind, relay *s return "", err } - if _, err := queryWriter.WriteString("&Signature=" + url.QueryEscape(base64.StdEncoding.EncodeToString(signature))); err != nil { + if _, err := queryWriter.WriteString(signatureQueryPrefix + url.QueryEscape(base64.StdEncoding.EncodeToString(signature))); err != nil { return "", err } @@ -364,14 +405,20 @@ func SignRedirect(ctx context.Context, d *document.Document, kind Kind, relay *s } func parseQuery(ctx context.Context, raw string) (map[string]queryPart, error) { + const ( + firstURIASCII = 0x21 + lastURIASCII = 0x7e + maximumRedirectParameters = 32 + ) + for i := range len(raw) { - if i&4095 == 0 { + if i&contextPollMask == 0 { if err := ctx.Err(); err != nil { return nil, err } } - if raw[i] < 0x21 || raw[i] > 0x7e { + if raw[i] < firstURIASCII || raw[i] > lastURIASCII { return nil, fmt.Errorf("%w: query must be ASCII URI octets", ErrEncoding) } } @@ -379,7 +426,7 @@ func parseQuery(ctx context.Context, raw string) (map[string]queryPart, error) { out := make(map[string]queryPart) for count := 0; ; count++ { - if count == 32 { + if count == maximumRedirectParameters { return nil, ErrPolicy } @@ -417,20 +464,24 @@ func parseQuery(ctx context.Context, raw string) (map[string]queryPart, error) { } func queryMetadata(name, raw string, size int) (string, error) { + const ( + maximumEncodingURIBytes = 4096 + ) + switch name { case fieldRelayState: - if size > 80 { + if size > profile.HTTPRelayStateMaxBytes { return "", ErrPolicy } case nameSignature: - if size > base64.StdEncoding.EncodedLen(2048) { + if size > base64.StdEncoding.EncodedLen(maximumRedirectSignatureBytes) { return "", ErrEncoding } - case "SigAlg": - if size > 4096 { + case fieldSignatureAlgorithm: + if size > maximumEncodingURIBytes { return "", ErrPolicy } - case "SAMLEncoding": + case fieldSAMLEncoding: if size != len(DeflateEncoding) { return "", ErrPolicy } @@ -455,7 +506,7 @@ func queryValueSize(ctx context.Context, value string) (int, error) { size := 0 for i := 0; i < len(value); i++ { - if i&4095 == 0 { + if i&contextPollMask == 0 { if err := ctx.Err(); err != nil { return 0, err } @@ -476,7 +527,7 @@ func queryValueSize(ctx context.Context, value string) (int, error) { } func decodeBase64(value string, maximum int) ([]byte, error) { - if strings.ContainsAny(value, " \t\r\n") || len(value) > base64.StdEncoding.EncodedLen(maximum) { + if strings.ContainsAny(value, lexicon.XMLWhitespace) || len(value) > base64.StdEncoding.EncodedLen(maximum) { return nil, ErrEncoding } @@ -508,19 +559,28 @@ func releaseRedirectCompressor(writer *flate.Writer) { } func normalize(o Options) (Options, error) { + const ( + defaultRedirectCompressedBytes = 64 << 10 + defaultRedirectMessageBytes = 2 << 20 + defaultRedirectQueryBytes = 128 << 10 + maximumRedirectCompressedBytes = 2 << 20 + maximumRedirectMessageBytes = 16 << 20 + maximumRedirectQueryBytes = 4 << 20 + ) + if o.MaxQueryBytes == 0 { - o.MaxQueryBytes = 128 << 10 + o.MaxQueryBytes = defaultRedirectQueryBytes } if o.MaxCompressedBytes == 0 { - o.MaxCompressedBytes = 64 << 10 + o.MaxCompressedBytes = defaultRedirectCompressedBytes } if o.MaxMessageBytes == 0 { - o.MaxMessageBytes = 2 << 20 + o.MaxMessageBytes = defaultRedirectMessageBytes } - if o.MaxQueryBytes < 1 || o.MaxQueryBytes > 4<<20 || o.MaxCompressedBytes < 1 || o.MaxCompressedBytes > 2<<20 || o.MaxMessageBytes < 1 || o.MaxMessageBytes > 16<<20 { + if o.MaxQueryBytes < 1 || o.MaxQueryBytes > maximumRedirectQueryBytes || o.MaxCompressedBytes < 1 || o.MaxCompressedBytes > maximumRedirectCompressedBytes || o.MaxMessageBytes < 1 || o.MaxMessageBytes > maximumRedirectMessageBytes { return o, ErrPolicy } @@ -544,15 +604,19 @@ func normalize(o Options) (Options, error) { } func kindOf(d *document.Document) (Kind, error) { + const ( + xmlNameSubjectQuery = "SubjectQuery" + ) + n := d.Root().Name() if n.Space != ProtocolNamespace { return "", fmt.Errorf("%w: expected SAML 2.0 protocol root", ErrEncoding) } switch n.Local { - case "AuthnRequest", "LogoutRequest", "ArtifactResolve", "ManageNameIDRequest", "NameIDMappingRequest", "AuthnQuery", "AttributeQuery", "AuthzDecisionQuery", "AssertionIDRequest", "SubjectQuery": + case lexicon.SAMLElementAuthnRequest, lexicon.SAMLElementLogoutRequest, lexicon.SAMLElementArtifactResolve, lexicon.SAMLElementManageNameIDRequest, lexicon.SAMLElementNameIDMappingRequest, lexicon.SAMLElementAuthnQuery, lexicon.SAMLElementAttributeQuery, lexicon.SAMLElementAuthzDecisionQuery, lexicon.SAMLElementAssertionIDRequest, xmlNameSubjectQuery: return Request, nil - case "Response", "LogoutResponse", "ArtifactResponse", "ManageNameIDResponse", "NameIDMappingResponse": + case lexicon.SAMLElementResponse, lexicon.SAMLElementLogoutResponse, lexicon.SAMLElementArtifactResponse, lexicon.SAMLElementManageNameIDResponse, lexicon.SAMLElementNameIDMappingResponse: return Response, nil default: return "", fmt.Errorf("%w: unsupported protocol message", ErrPolicy) diff --git a/saml/binding/redirect_test.go b/saml/binding/redirect_test.go index c0af7c7..aa1dd78 100644 --- a/saml/binding/redirect_test.go +++ b/saml/binding/redirect_test.go @@ -242,30 +242,6 @@ func TestRedirectRejectsAuthenticatedTrailingData(t *testing.T) { } } -func key(t testing.TB) *ecdsa.PrivateKey { - t.Helper() - - k, e := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) - if e != nil { - t.Fatal(e) - } - - return k -} - -func doc(t testing.TB, text string) *document.Document { - t.Helper() - - d, e := document.Parse(t.Context(), []byte(text), document.DefaultLimits()) - if e != nil { - t.Fatal(e) - } - - return d -} - -func options() binding.Options { return binding.Options{SignatureMethod: xmldsig.ECDSASHA256} } - // BenchmarkSignRedirect measures the complete public binding, including XML // output, raw DEFLATE, URL encoding and signing, on a reusable snapshot/key. func BenchmarkSignRedirect(b *testing.B) { @@ -332,3 +308,27 @@ func TestRedirectCompressionReuse(t *testing.T) { }) } } + +func key(t testing.TB) *ecdsa.PrivateKey { + t.Helper() + + k, e := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + if e != nil { + t.Fatal(e) + } + + return k +} + +func doc(t testing.TB, text string) *document.Document { + t.Helper() + + d, e := document.Parse(t.Context(), []byte(text), document.DefaultLimits()) + if e != nil { + t.Fatal(e) + } + + return d +} + +func options() binding.Options { return binding.Options{SignatureMethod: xmldsig.ECDSASHA256} } diff --git a/saml/binding/soap.go b/saml/binding/soap.go new file mode 100644 index 0000000..bfa1a32 --- /dev/null +++ b/saml/binding/soap.go @@ -0,0 +1,612 @@ +package binding + +import ( + "bytes" + "context" + "errors" + "fmt" + "io" + "mime" + "net/http" + "slices" + "strings" + "time" + + "github.com/skarm/xmlsec/internal/lexicon" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/nilcheck" + "github.com/skarm/xmlsec/internal/streamio" + "github.com/skarm/xmlsec/internal/tree" + "github.com/skarm/xmlsec/saml/internal/profile" + "github.com/skarm/xmlsec/saml/internal/xmlutil" +) + +// Bound supplied SOAP headers and understood-header declarations to 64 entries before wrapping or dispatch; payload bytes have a separate limit. +const ( + maximumSOAPHeaders = 64 +) + +// Canonical HTTP field spelling shared by SOAP/URI transports; the associated cache directives keep protocol responses out of intermediary storage. +const ( + headerCacheControl = "Cache-Control" + headerPragma = "Pragma" +) + +// Outgoing fault messages and parsed fault message/actor text are bounded to 4096 UTF-8 bytes; remote diagnostics supply no trusted routing or identity. +const ( + maximumSOAPFaultTextBytes = 4096 +) + +// SOAP 1.1 HTTP output declares XML in UTF-8; receiver MIME checks and XML parsing still validate the actual message independently. +const ( + mediaTypeSOAPUTF8 = lexicon.MediaTypeSOAP + "; charset=" + lexicon.EncodingUTF8 +) + +// SOAP 1.1 containers use the envelope namespace while fault members are unqualified; these local tokens never replace expanded-name checks. +const ( + xmlNameBody = "Body" + xmlNameEnvelope = "Envelope" + xmlNameFault = "Fault" + xmlNameFaultcode = "faultcode" + xmlNameFaultstring = "faultstring" + xmlNameHeader = "Header" +) + +// Standard SOAP 1.1 fault codes emitted by this closed profile; a parsed fault still resolves its QName and treats remote diagnostic text as untrusted. +const ( + faultCodeClient = "Client" + faultCodeMustUnderstand = "MustUnderstand" + faultCodeVersionMismatch = "VersionMismatch" +) + +// Suggested prefix for emitted SOAP envelopes. Verification compares expanded names, never the sender's prefix spelling. +const ( + prefixSOAP = "env" +) + +// Transport cache directive for sensitive SAML responses; shared writers must retain the same no-store/HTTP compatibility behavior. +const ( + cacheControlNoStore = "no-cache, no-store" + cachePragma = "no-cache" +) + +const ( + // SOAPNamespace is the SOAP 1.1 envelope namespace required by SAML Bindings 3.2. + SOAPNamespace = "http://schemas.xmlsoap.org/soap/envelope/" + // SOAPNext targets the next SOAP processor. + SOAPNext = "http://schemas.xmlsoap.org/soap/actor/next" +) + +// SOAPFault describes a SOAP processing error, distinct from a SAML error status. +// Code is the expanded SOAP fault code and Message is untrusted remote text. +type SOAPFault struct { + // Code is the expanded SOAP fault code. + Code document.Name + // Message is untrusted diagnostic text from the remote processor. + Message string +} + +// Error implements error without interpreting the remote fault text. +func (f *SOAPFault) Error() string { return "SOAP fault: " + f.Code.Local + ": " + f.Message } + +// SOAPOptions bounds parsing and declares the headers handled by the caller. +// A mandatory header for this actor or SOAPNext that is not listed causes a +// MustUnderstand fault. Listed headers must still be semantically validated. +type SOAPOptions struct { + // MaxBytes bounds the serialized envelope in bytes. Zero selects 2 MiB; the + // maximum is 16 MiB. + MaxBytes int + // XMLLimits bounds envelope parsing. Zero fields use document defaults; + // its byte limit is also capped by MaxBytes. + XMLLimits document.Limits + // Actor identifies the SOAP processor whose targeted mandatory headers must be + // understood. Empty selects the ultimate receiver. + Actor string + // Understood lists at most 64 expanded header names handled by the caller. + // Listing a name does not validate its contents. + Understood []document.Name +} + +// SOAPMessage retains the original envelope and namespace context. Parsing does +// not authenticate a SAML message. Payload must pass protocol verification. +type SOAPMessage struct { + // doc retains the original immutable envelope and namespace context. + doc *document.Document + // body identifies the sole protocol payload within SOAP Body. + body document.Element + // headers retains the direct SOAP header elements without authenticating them. + headers []document.Element +} + +// Envelope returns the immutable original envelope. +func (m *SOAPMessage) Envelope() *document.Document { return m.doc } + +// Headers returns a copy of the direct SOAP header list. +func (m *SOAPMessage) Headers() []document.Element { + return append([]document.Element(nil), m.headers...) +} + +// Payload returns an immutable subtree view, retaining original ancestor context +// for signature verification. WrapSOAP preserves it for forwarding. Use Envelope +// to serialize the original message; isolated XML output rejects inherited xml:* +// attributes that cannot be flattened onto a schema-constrained SAML root. +func (m *SOAPMessage) Payload(ctx context.Context) (*document.Document, error) { + if ctx == nil || m == nil { + return nil, ErrBinding + } + + if err := ctx.Err(); err != nil { + return nil, err + } + + return tree.ScopeDocument(m.doc, m.body) +} + +// WrapSOAP puts one message into a SOAP 1.1 body and copies optional headers. +// Exclusive C14N signatures survive wrapping. Inclusive signatures must be made +// in the final namespace context; protocol.SignSOAP supports that operation. +func WrapSOAP(ctx context.Context, payload *document.Document, headers ...document.Element) (*document.Document, error) { + if ctx == nil || payload == nil || !payload.Root().Valid() || len(headers) > maximumSOAPHeaders { + return nil, ErrBinding + } + + _, node := tree.Unwrap(payload.Root()) + prefix := soapPrefix(node) + n := xmlutil.Node(prefix, SOAPNamespace, xmlNameEnvelope) + + n.NS = []tree.Namespace{{Prefix: prefix, URI: SOAPNamespace}} + if len(headers) > 0 { + h := xmlutil.Node(prefix, SOAPNamespace, xmlNameHeader) + + for _, e := range headers { + if !e.Valid() || e.Name().Space == "" { + return nil, ErrBinding + } + + _, node := tree.Unwrap(e) + xmlutil.Add(h, tree.ImportSubtree(node)) + } + + xmlutil.Add(n, h) + } + + b := xmlutil.Node(prefix, SOAPNamespace, xmlNameBody) + seen := map[document.Name]bool{} + + for ancestor := node.Parent; ancestor != nil; ancestor = ancestor.Parent { + for _, attr := range ancestor.Attrs { + if attr.Name.Space == tree.XMLNamespace && !seen[attr.Name] { + b.Attrs = append(b.Attrs, attr) + seen[attr.Name] = true + } + } + } + + xmlutil.Add(b, tree.ImportSubtree(node)) + xmlutil.Add(n, b) + + return xmlutil.Document(ctx, n) +} + +func soapPrefix(node *tree.Node) string { + for ancestor := node.Parent; ancestor != nil; ancestor = ancestor.Parent { + if ancestor.Name == xmlutil.Name(SOAPNamespace, xmlNameEnvelope) { + return ancestor.Prefix + } + } + + for _, ns := range node.NS { + if ns.URI == SOAPNamespace { + return ns.Prefix + } + } + + return prefixSOAP +} + +// DecodeSOAP parses a bounded SOAP 1.1 envelope and enforces a single body +// element. Unknown optional headers are retained. SOAP faults are returned as +// *SOAPFault errors; a valid remote fault also returns its message for forwarding. +// Other failures return no message. SAML status failures remain ordinary messages. +func DecodeSOAP(ctx context.Context, raw []byte, o SOAPOptions) (*SOAPMessage, error) { + const ( + maximumSOAPEnvelopeChildren = 2 + ) + + maxBytes, err := soapMaximum(o) + if err != nil || len(raw) > maxBytes { + return nil, ErrBinding + } + + d, err := document.Parse(ctx, raw, xmlutil.XMLLimits(o.XMLLimits, maxBytes)) + if err != nil { + return nil, err + } + + r := d.Root() + if r.Name() != xmlutil.Name(SOAPNamespace, xmlNameEnvelope) { + return nil, soapFault(faultCodeVersionMismatch, "expected SOAP 1.1") + } + + cs, err := xmlutil.Children(r) + if err != nil || len(cs) == 0 || len(cs) > maximumSOAPEnvelopeChildren { + return nil, soapFault(faultCodeClient, "invalid SOAP envelope") + } + + m := &SOAPMessage{doc: d} + i := 0 + + if cs[0].Name() == xmlutil.Name(SOAPNamespace, xmlNameHeader) { + m.headers, err = xmlutil.Children(cs[0]) + if err != nil || len(m.headers) > maximumSOAPHeaders { + return nil, soapFault(faultCodeClient, "invalid SOAP header") + } + + if err := validateSOAPHeaders(m.headers, o); err != nil { + return nil, err + } + + i++ + } + + if i != len(cs)-1 || cs[i].Name() != xmlutil.Name(SOAPNamespace, xmlNameBody) { + return nil, soapFault(faultCodeClient, "missing SOAP body") + } + + bs, err := xmlutil.Children(cs[i]) + if err != nil || len(bs) != 1 { + return nil, soapFault(faultCodeClient, "SOAP body must contain one message") + } + + m.body = bs[0] + if m.body.Name() == xmlutil.Name(SOAPNamespace, xmlNameFault) { + err := readSOAPFault(m.body) + + if _, ok := errors.AsType[*SOAPFault](err); ok { + return m, err + } + + return nil, err + } + + if m.body.Name().Space != ProtocolNamespace { + return nil, soapFault(faultCodeClient, "expected SAML protocol message") + } + + return m, nil +} + +// FaultSOAP constructs a SOAP 1.1 fault envelope suitable for an HTTP 500 response. +func FaultSOAP(ctx context.Context, f SOAPFault) (*document.Document, error) { + const ( + faultCodeServer = "Server" + qualifiedSOAPFaultPrefix = prefixSOAP + ":" + ) + + if f.Code.Space != SOAPNamespace || !slices.Contains([]string{faultCodeVersionMismatch, faultCodeMustUnderstand, faultCodeClient, faultCodeServer}, f.Code.Local) || len(f.Message) > maximumSOAPFaultTextBytes { + return nil, ErrBinding + } + + n := xmlutil.Node(prefixSOAP, SOAPNamespace, xmlNameFault) + n.NS = []tree.Namespace{{Prefix: prefixSOAP, URI: SOAPNamespace}} + c := xmlutil.Node("", "", xmlNameFaultcode) + xmlutil.Text(c, qualifiedSOAPFaultPrefix+f.Code.Local) + xmlutil.Add(n, c) + + s := xmlutil.Node("", "", xmlNameFaultstring) + xmlutil.Text(s, f.Message) + xmlutil.Add(n, s) + + d, err := xmlutil.Document(ctx, n) + if err != nil { + return nil, err + } + + return WrapSOAP(ctx, d) +} + +// SOAPClient performs a SAML SOAP HTTP exchange to an application-trusted URL. +// Redirects are always rejected, including when Client normally follows them. +// Client transports may implement TLS/mTLS and deadlines. The default client has +// a 30 second timeout. AllowHTTP is intended for explicitly trusted local links. +type SOAPClient struct { + // Client supplies the HTTP transport and timeout. Nil selects a 30-second + // timeout. The client is copied and redirects are disabled. + Client *http.Client + // Options bounds outgoing envelopes and parsing of incoming SOAP messages. + Options SOAPOptions + // AllowHTTP permits explicitly configured plain HTTP endpoints. The default + // requires HTTPS. + AllowHTTP bool +} + +// RoundTrip sends an already constructed SOAP envelope and returns its parsed +// response. A valid XML signature must still authenticate the returned payload. +func (c SOAPClient) RoundTrip(ctx context.Context, endpoint string, envelope *document.Document) (message *SOAPMessage, err error) { + const ( + headerSOAPAction = "SoapAction" + samlSOAPAction = "\"http://www.oasis-open.org/committees/security\"" + ) + + if ctx == nil || envelope == nil || envelope.Root().Name() != xmlutil.Name(SOAPNamespace, xmlNameEnvelope) || !profile.Endpoint(endpoint, c.AllowHTTP) { + return nil, ErrBinding + } + + maxBytes, err := soapMaximum(c.Options) + if err != nil { + return nil, err + } + + raw, err := envelope.XML(ctx) + if err != nil { + return nil, err + } + + if len(raw) > maxBytes { + return nil, ErrBinding + } + + req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewReader(raw)) + if err != nil { + return nil, err + } + + req.Header.Set(lexicon.HeaderContentType, mediaTypeSOAPUTF8) + req.Header.Set(headerSOAPAction, samlSOAPAction) + req.Header.Set(headerCacheControl, cacheControlNoStore) + req.Header.Set(headerPragma, cachePragma) + + client := soapHTTPClient(c.Client) + + resp, err := client.Do(req) + if err != nil { + return nil, err + } + defer func() { + if closeErr := resp.Body.Close(); closeErr != nil { + message = nil + err = errors.Join(err, closeErr) + } + }() + + if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusInternalServerError { + return nil, fmt.Errorf("%w: SOAP HTTP status %d", ErrBinding, resp.StatusCode) + } + + media, _, err := mime.ParseMediaType(resp.Header.Get(lexicon.HeaderContentType)) + if err != nil || media != soapMediaType { + return nil, ErrBinding + } + + data, err := io.ReadAll(&streamio.Reader{Context: ctx, Source: resp.Body, Limit: int64(maxBytes), LimitError: ErrBinding}) + if err != nil { + return nil, err + } + + m, err := DecodeSOAP(ctx, data, c.Options) + if err != nil { + return nil, err + } + + if resp.StatusCode != http.StatusOK { + return nil, ErrBinding + } + + return m, nil +} + +// ReadSOAPRequest checks the SOAP HTTP binding and reads at most its byte budget. +// It does not require any particular SOAPAction value (SAML Bindings 3.2.3). +func ReadSOAPRequest(ctx context.Context, r *http.Request, o SOAPOptions) (*SOAPMessage, error) { + if ctx == nil || r == nil || nilcheck.IsNil(r.Body) || r.Method != http.MethodPost { + return nil, soapFault(faultCodeClient, "expected SOAP POST") + } + + media, _, err := mime.ParseMediaType(r.Header.Get(lexicon.HeaderContentType)) + if err != nil || media != soapMediaType { + return nil, soapFault(faultCodeClient, "expected text/xml") + } + + maxBytes, err := soapMaximum(o) + if err != nil { + return nil, err + } + + raw, err := io.ReadAll(&streamio.Reader{Context: ctx, Source: r.Body, Limit: int64(maxBytes), LimitError: ErrBinding}) + if err != nil { + return nil, err + } + + return DecodeSOAP(ctx, raw, o) +} + +// WriteSOAPResponse writes a SAML SOAP response with HTTP 200, or a SOAP Fault +// with HTTP 500. SAML failure statuses deliberately retain HTTP 200. +func WriteSOAPResponse(ctx context.Context, w http.ResponseWriter, envelope *document.Document) error { + return WriteSOAPResponseWithOptions(ctx, w, envelope, SOAPOptions{}) +} + +// WriteSOAPResponseWithOptions additionally recognizes explicitly processed +// extension headers and applies their configured XML/byte limits. +func WriteSOAPResponseWithOptions(ctx context.Context, w http.ResponseWriter, envelope *document.Document, o SOAPOptions) error { + const ( + cacheControlPrivate = "no-cache, no-store, must-revalidate, private" + headerETag = "ETag" + headerLastModified = "Last-Modified" + ) + + if ctx == nil || nilcheck.IsNil(w) || envelope == nil { + return ErrBinding + } + + raw, err := envelope.XML(ctx) + if err != nil { + return err + } + + message, err := DecodeSOAP(ctx, raw, o) + + var fault *SOAPFault + + status := http.StatusOK + + if err != nil { + if message == nil || !errors.As(err, &fault) { + return err + } + + status = http.StatusInternalServerError + } + + w.Header().Set(lexicon.HeaderContentType, mediaTypeSOAPUTF8) + w.Header().Set(headerCacheControl, cacheControlPrivate) + w.Header().Set(headerPragma, cachePragma) + w.Header().Del(headerETag) + w.Header().Del(headerLastModified) + w.WriteHeader(status) + + n, err := w.Write(raw) + if err == nil && n != len(raw) { + err = io.ErrShortWrite + } + + return err +} + +func soapMaximum(o SOAPOptions) (int, error) { + const ( + defaultSOAPBytes = 2 << 20 + maximumSOAPBytes = 16 << 20 + ) + + n := o.MaxBytes + if n == 0 { + n = defaultSOAPBytes + } + + if n < 1 || n > maximumSOAPBytes || len(o.Understood) > maximumSOAPHeaders { + return 0, ErrBinding + } + + return n, nil +} + +func soapHTTPClient(source *http.Client) *http.Client { + const ( + defaultHTTPTimeout = 30 * time.Second + ) + + client := http.Client{Timeout: defaultHTTPTimeout} + if source != nil { + client = *source + } + + client.CheckRedirect = func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse } + + return &client +} + +func soapFault(code, message string) *SOAPFault { + return &SOAPFault{Code: xmlutil.Name(SOAPNamespace, code), Message: message} +} + +func readSOAPFault(e document.Element) error { + const ( + maximumSOAPFaultChildren = 4 + maximumSOAPFaultCodeBytes = 256 + minimumSOAPFaultChildren = 2 + xmlNameDetail = "detail" + xmlNameFaultactor = "faultactor" + ) + + cs, err := xmlutil.Children(e) + if err != nil || len(cs) < minimumSOAPFaultChildren || len(cs) > maximumSOAPFaultChildren || cs[0].Name() != xmlutil.Name("", xmlNameFaultcode) || cs[1].Name() != xmlutil.Name("", xmlNameFaultstring) { + return ErrBinding + } + + code, err := xmlutil.Simple(cs[0], maximumSOAPFaultCodeBytes) + if err != nil { + return ErrBinding + } + + message, err := xmlutil.Simple(cs[1], maximumSOAPFaultTextBytes) + if err != nil { + return ErrBinding + } + + prefix, local, ok := strings.Cut(strings.TrimSpace(code), ":") + if !ok { + local = prefix + prefix = "" + } + + if !tree.NCName(local) || prefix != "" && !tree.NCName(prefix) { + return ErrBinding + } + + ns, ok := cs[0].Namespace(prefix) + if !ok && prefix != "" { + return ErrBinding + } + + position := 2 + if position < len(cs) && cs[position].Name() == xmlutil.Name("", xmlNameFaultactor) { + actor, err := xmlutil.Simple(cs[position], maximumSOAPFaultTextBytes) + if err != nil || !profile.URI(actor) { + return ErrBinding + } + + position++ + } + + if position < len(cs) && cs[position].Name() == xmlutil.Name("", xmlNameDetail) { + position++ + } + + if position != len(cs) { + return ErrBinding + } + + return &SOAPFault{Code: xmlutil.Name(ns, local), Message: message} +} + +func validateSOAPHeaders(headers []document.Element, o SOAPOptions) error { + const ( + soapActorAttribute = "actor" + soapBooleanFalse = "0" + soapBooleanTrue = "1" + soapMustUnderstandAttribute = "mustUnderstand" + ) + + for _, h := range headers { + if h.Name().Space == "" { + return soapFault(faultCodeClient, "unqualified SOAP header") + } + + must, present := h.Attribute(xmlutil.Name(SOAPNamespace, soapMustUnderstandAttribute)) + if present && must != soapBooleanFalse && must != soapBooleanTrue { + return soapFault(faultCodeClient, "invalid mustUnderstand") + } + + actor, _ := h.Attribute(xmlutil.Name(SOAPNamespace, soapActorAttribute)) + if must != soapBooleanTrue || actor != "" && actor != SOAPNext && actor != o.Actor { + continue + } + + known := false + + for _, name := range o.Understood { + if name == h.Name() { + known = true + break + } + } + + if !known { + return soapFault(faultCodeMustUnderstand, "unsupported mandatory SOAP header") + } + } + + return nil +} diff --git a/saml/binding/soap_http_contract_test.go b/saml/binding/soap_http_contract_test.go new file mode 100644 index 0000000..c229c50 --- /dev/null +++ b/saml/binding/soap_http_contract_test.go @@ -0,0 +1,96 @@ +package binding_test + +import ( + "errors" + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/saml/binding" +) + +func TestWrapSOAPRejectsEmptyDocument(t *testing.T) { + for _, payload := range []*document.Document{nil, {}} { + envelope, err := binding.WrapSOAP(t.Context(), payload) + if !errors.Is(err, binding.ErrBinding) || envelope != nil { + t.Fatal("invalid payload must be rejected without an envelope", err) + } + } +} + +func TestWriteSOAPFaultRequiresValidatedEnvelope(t *testing.T) { + const headerNamespace = "urn:mandatory-header" + raw := soapPrefix + `s:Clientbad request` + envelope, err := document.Parse(t.Context(), []byte(raw), document.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + for _, understood := range []bool{false, true} { + options := binding.SOAPOptions{} + if understood { + options.Understood = []document.Name{{Space: headerNamespace, Local: "route"}} + } + writer := httptest.NewRecorder() + err := binding.WriteSOAPResponseWithOptions(t.Context(), writer, envelope, options) + if understood { + if err != nil || writer.Code != http.StatusInternalServerError || writer.Body.Len() == 0 { + t.Fatal("valid remote fault was not forwarded", err) + } + } else { + var fault *binding.SOAPFault + if !errors.As(err, &fault) || fault.Code.Local != "MustUnderstand" || writer.Body.Len() != 0 || len(writer.Header()) != 0 { + t.Fatal("invalid envelope was transmitted as a valid SOAP fault", err) + } + } + } +} + +type soapRoundTripperFunc func(*http.Request) (*http.Response, error) + +func (f soapRoundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) } + +type soapClosingBody struct { + io.Reader + err error + closed int +} + +func (b *soapClosingBody) Close() error { + b.closed++ + return b.err +} + +func TestSOAPClientPreservesResponseCloseErrors(t *testing.T) { + const valid = soapPrefix + `` + envelope, err := document.Parse(t.Context(), []byte(valid), document.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + failure := errors.New("response close failed") + for _, tc := range []struct { + name string + status int + body string + }{ + {"valid response", http.StatusOK, valid}, + {"invalid XML", http.StatusOK, "` + +func TestSOAPIndependentEnvelopes(t *testing.T) { + for _, tc := range []struct { + name, xml string + wantError bool + code string + }{ + {"minimal", soapPrefix + ``, false, ""}, + {"optional header", soapPrefix + ``, false, ""}, + {"mandatory unknown", soapPrefix + ``, true, "MustUnderstand"}, + {"different actor", soapPrefix + ``, false, ""}, + {"two payloads", soapPrefix + ``, true, "Client"}, + {"header after body", soapPrefix + ``, true, "Client"}, + {"invalid mustUnderstand", soapPrefix + ``, true, "Client"}, + {"SOAP12", ``, true, "VersionMismatch"}, + {"fault", soapPrefix + `s:Clientbad request`, true, "Client"}, + {"malformed fault QName", soapPrefix + `s:Client:badbad request`, true, ""}, + {"duplicate fault field", soapPrefix + `s:Clientbad requestduplicate`, true, ""}, + } { + t.Run(tc.name, func(t *testing.T) { + _, err := binding.DecodeSOAP(t.Context(), []byte(tc.xml), binding.SOAPOptions{}) + if (err != nil) != tc.wantError { + t.Fatalf("DecodeSOAP: %v", err) + } + if tc.code != "" { + var fault *binding.SOAPFault + if !errors.As(err, &fault) || fault.Code.Local != tc.code { + t.Fatalf("fault: %v", err) + } + } + }) + } + if _, err := binding.DecodeSOAP(t.Context(), []byte(soapPrefix+``), binding.SOAPOptions{MaxBytes: 20}); err == nil { + t.Fatal("byte bound ignored") + } +} + +func TestSOAPHTTPStatusAndRedirects(t *testing.T) { + payload, err := document.Parse(t.Context(), []byte(``), document.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + envelope, err := binding.WrapSOAP(t.Context(), payload) + if err != nil { + t.Fatal(err) + } + var action string + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + action = r.Header.Get("SoapAction") + if _, err := binding.ReadSOAPRequest(r.Context(), r, binding.SOAPOptions{}); err != nil { + t.Error(err) + return + } + if err := binding.WriteSOAPResponse(r.Context(), w, envelope); err != nil { + t.Error(err) + } + })) + defer server.Close() + client := binding.SOAPClient{AllowHTTP: true} + if _, err := client.RoundTrip(t.Context(), server.URL, envelope); err != nil || action == "" { + t.Fatalf("exchange/action: %v", err) + } + fault, err := binding.FaultSOAP(t.Context(), binding.SOAPFault{Code: document.Name{Space: binding.SOAPNamespace, Local: "Client"}, Message: "invalid"}) + if err != nil { + t.Fatal(err) + } + w := httptest.NewRecorder() + if err := binding.WriteSOAPResponse(t.Context(), w, fault); err != nil { + t.Fatal(err) + } + if w.Code != 500 || w.Header().Get("Content-Type") != "text/xml; charset=utf-8" || !strings.Contains(w.Body.String(), "faultcode") { + t.Fatal("SOAP fault HTTP mapping") + } + w = httptest.NewRecorder() + if err := binding.WriteSOAPResponse(t.Context(), w, envelope); err != nil || w.Code != 200 { + t.Fatal("SAML response must retain HTTP 200") + } + redirected := false + redirect := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + redirected = true + http.Redirect(w, r, server.URL, http.StatusFound) + })) + defer redirect.Close() + if _, err := client.RoundTrip(t.Context(), redirect.URL, envelope); err == nil || !redirected { + t.Fatal("redirect accepted") + } + if _, err := (binding.SOAPClient{}).RoundTrip(t.Context(), server.URL, envelope); err == nil { + t.Fatal("HTTP allowed implicitly") + } +} + +func FuzzSOAPEnvelope(f *testing.F) { + f.Add([]byte(soapPrefix + ``)) + f.Add([]byte(soapPrefix + ``)) + f.Fuzz(func(t *testing.T, raw []byte) { + m, err := binding.DecodeSOAP(t.Context(), raw, binding.SOAPOptions{MaxBytes: 4096, XMLLimits: document.Limits{MaxBytes: 4096, MaxDepth: 16, MaxNodes: 128, MaxAttributesPerElement: 16, MaxInScopeNamespaces: 32, MaxNodeSetNodes: 512}}) + if err != nil { + return + } + payload, err := m.Payload(t.Context()) + if err != nil || payload.Root().Name().Space != "urn:oasis:names:tc:SAML:2.0:protocol" { + t.Fatalf("invalid accepted SOAP payload: %v", err) + } + }) +} + +type shortResponseWriter struct{ *httptest.ResponseRecorder } + +func (w shortResponseWriter) Write(p []byte) (int, error) { + return w.ResponseRecorder.Write(p[:len(p)-1]) +} + +func TestWriteSOAPResponseRejectsShortWrite(t *testing.T) { + t.Parallel() + for _, fault := range []bool{false, true} { + var envelope *document.Document + var err error + if fault { + envelope, err = binding.FaultSOAP(t.Context(), binding.SOAPFault{ + Code: document.Name{Space: binding.SOAPNamespace, Local: "Server"}, Message: "unavailable", + }) + } else { + envelope, err = document.Parse(t.Context(), []byte(soapPrefix+``), document.DefaultLimits()) + } + if err != nil { + t.Fatal(err) + } + writer := shortResponseWriter{httptest.NewRecorder()} + if err := binding.WriteSOAPResponse(t.Context(), writer, envelope); !errors.Is(err, io.ErrShortWrite) { + t.Fatalf("short SOAP write returned %v; want io.ErrShortWrite", err) + } + wantStatus := http.StatusOK + if fault { + wantStatus = http.StatusInternalServerError + } + if writer.Code != wantStatus { + t.Fatalf("status = %d; want %d", writer.Code, wantStatus) + } + } +} + +func TestWriteAssertionRejectsTypedNilWriter(t *testing.T) { + t.Parallel() + d, err := document.Parse(t.Context(), []byte(``), document.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + var writer *httptest.ResponseRecorder + if err := binding.WriteAssertion(t.Context(), writer, d); !errors.Is(err, binding.ErrBinding) { + t.Fatalf("typed-nil writer returned %v; want ErrBinding", err) + } +} diff --git a/saml/binding/uri.go b/saml/binding/uri.go new file mode 100644 index 0000000..affca2f --- /dev/null +++ b/saml/binding/uri.go @@ -0,0 +1,182 @@ +package binding + +import ( + "context" + "errors" + "fmt" + "io" + "mime" + "net/http" + "net/url" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/lexicon" + "github.com/skarm/xmlsec/internal/nilcheck" + "github.com/skarm/xmlsec/internal/streamio" + "github.com/skarm/xmlsec/saml/internal/profile" + "github.com/skarm/xmlsec/saml/internal/xmlutil" +) + +// Exact query-field name of the assertion URI binding. It is distinct from the XML ID attribute even though both use the spelling ID. +const ( + assertionIDParameter = "ID" +) + +// The URI transport accepts only an assertion-namespace root; this lexical token never authenticates the returned assertion. +const ( + xmlNameAssertion = "Assertion" +) + +// AssertionMediaType is the SAML assertion MIME type used by the URI binding. +const AssertionMediaType = "application/samlassertion+xml" + +// AssertionURL constructs the exact ID query required by Bindings 3.7.5.1. +// The configured endpoint must have no query or fragment; wildcard IDs are invalid. +func AssertionURL(endpoint, id string, allowHTTP bool) (string, error) { + if !profile.Endpoint(endpoint, allowHTTP) || !profile.ID(id) { + return "", ErrBinding + } + + u, err := url.Parse(endpoint) + if err != nil || u.RawQuery != "" || u.ForceQuery || u.Fragment != "" { + return "", ErrBinding + } + + u.RawQuery = url.Values{assertionIDParameter: {id}}.Encode() + + return u.String(), nil +} + +// ReadAssertionID validates the URI binding's GET and single ID parameter. +// HTTP peer authentication and authorization remain the handler's responsibility. +func ReadAssertionID(r *http.Request) (string, error) { + if r == nil || r.URL == nil || r.Method != http.MethodGet { + return "", ErrBinding + } + + q, err := url.ParseQuery(r.URL.RawQuery) + if err != nil || len(q) != 1 || len(q[assertionIDParameter]) != 1 || !profile.ID(q.Get(assertionIDParameter)) { + return "", ErrBinding + } + + return q.Get(assertionIDParameter), nil +} + +// WriteAssertion writes an application-authorized assertion with caching disabled. +// It does not authorize disclosure or authenticate the assertion's contents. +func WriteAssertion(ctx context.Context, w http.ResponseWriter, d *document.Document) error { + const ( + maximumAssertionXMLBytes = 2 << 20 + ) + + if ctx == nil || nilcheck.IsNil(w) || d == nil || d.Root().Name() != xmlutil.Name(xmlutil.Assertion, xmlNameAssertion) { + return ErrBinding + } + + raw, err := d.XML(ctx) + if err != nil { + return err + } + + if len(raw) > maximumAssertionXMLBytes { + return ErrBinding + } + + w.Header().Set(lexicon.HeaderContentType, AssertionMediaType) + w.Header().Set(headerCacheControl, cacheControlNoStore) + w.Header().Set(headerPragma, cachePragma) + w.WriteHeader(http.StatusOK) + + n, err := w.Write(raw) + if err == nil && n != len(raw) { + err = io.ErrShortWrite + } + + return err +} + +// URIClient resolves a configured authority endpoint. It never follows redirects +// or obtains authority URLs from received XML. Authentication can be supplied +// through the caller-owned HTTP client's transport, including preemptive Basic. +// Returned XML is unverified and must pass VerifyAuthorityAssertion. +type URIClient struct { + // Client supplies the HTTP transport and timeout. Nil selects a 30-second + // timeout. The client is copied and redirects are disabled. + Client *http.Client + // AllowHTTP permits explicitly configured plain HTTP endpoints. The default + // requires HTTPS. + AllowHTTP bool + // MaxBytes bounds the assertion response in bytes. Zero selects 2 MiB; the + // maximum is 16 MiB. + MaxBytes int +} + +// Resolve retrieves precisely the requested assertion or returns an HTTP error. +// Read or close failures return no document and preserve earlier error causes. +func (c URIClient) Resolve(ctx context.Context, endpoint, id string) (message *document.Document, err error) { + const ( + defaultAssertionResponseBytes = 2 << 20 + headerAccept = "Accept" + maximumAssertionResponseBytes = 16 << 20 + ) + + u, err := AssertionURL(endpoint, id, c.AllowHTTP) + if err != nil || ctx == nil { + return nil, ErrBinding + } + + maximum := c.MaxBytes + if maximum == 0 { + maximum = defaultAssertionResponseBytes + } + + if maximum < 1 || maximum > maximumAssertionResponseBytes { + return nil, ErrBinding + } + + r, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil) + if err != nil { + return nil, err + } + + r.Header.Set(headerAccept, AssertionMediaType) + + response, err := soapHTTPClient(c.Client).Do(r) + if err != nil { + return nil, err + } + + defer func() { + if closeErr := response.Body.Close(); closeErr != nil { + message = nil + err = errors.Join(err, closeErr) + } + }() + + if response.StatusCode != http.StatusOK { + return nil, fmt.Errorf("%w: assertion HTTP status %d", ErrBinding, response.StatusCode) + } + + media, _, err := mime.ParseMediaType(response.Header.Get(lexicon.HeaderContentType)) + if err != nil || media != AssertionMediaType && media != lexicon.MediaTypeXML && media != soapMediaType { + return nil, ErrBinding + } + + raw, err := io.ReadAll(&streamio.Reader{Context: ctx, Source: response.Body, Limit: int64(maximum), LimitError: ErrBinding}) + if err != nil { + return nil, err + } + + d, err := document.Parse(ctx, raw, xmlutil.XMLLimits(document.Limits{}, maximum)) + if err != nil { + return nil, err + } + + if d.Root().Name() != xmlutil.Name(xmlutil.Assertion, xmlNameAssertion) || xmlutil.Attr(d.Root(), lexicon.SAMLAttrID) != id { + return nil, ErrBinding + } + + return d, nil +} + +const soapMediaType = lexicon.MediaTypeSOAP diff --git a/saml/binding/uri_test.go b/saml/binding/uri_test.go new file mode 100644 index 0000000..3dadf2c --- /dev/null +++ b/saml/binding/uri_test.go @@ -0,0 +1,117 @@ +package binding_test + +import ( + "errors" + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" + "testing/iotest" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/saml/binding" +) + +func TestAssertionURIBinding(t *testing.T) { + for _, endpoint := range []string{"https://authority.test/assertion?other=x", "https://authority.test/assertion?", "https://authority.test/assertion#fragment"} { + if _, err := binding.AssertionURL(endpoint, "_held", false); err == nil { + t.Fatalf("invalid endpoint accepted: %s", endpoint) + } + } + for _, query := range []string{"ID=_held&ID=_other", "ID=_held&extra=value", "id=_held", "ID=*", "ID=%ZZ"} { + r := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "https://authority.test/assertion?"+query, nil) + if _, err := binding.ReadAssertionID(r); err == nil { + t.Fatalf("invalid query accepted: %s", query) + } + } + d, err := document.Parse(t.Context(), []byte(``), document.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + response := httptest.NewRecorder() + if err = binding.WriteAssertion(t.Context(), response, d); err != nil { + t.Fatal(err) + } + if response.Header().Get("Content-Type") != binding.AssertionMediaType || response.Header().Get("Cache-Control") != "no-cache, no-store" || response.Header().Get("Pragma") != "no-cache" { + t.Fatal("URI media/cache contract") + } + for _, mode := range []string{"good", "wrong-id", "redirect", "oversize"} { + t.Run(mode, func(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if mode == "redirect" { + http.Redirect(w, r, "/elsewhere", http.StatusFound) + return + } + w.Header().Set("Content-Type", binding.AssertionMediaType) + id := "_held" + if mode == "wrong-id" { + id = "_other" + } + _, _ = w.Write([]byte(``)) + })) + defer server.Close() + client := binding.URIClient{Client: server.Client(), AllowHTTP: true} + if mode == "oversize" { + client.MaxBytes = 16 + } + _, err := client.Resolve(t.Context(), server.URL, "_held") + if (err == nil) != (mode == "good") { + t.Fatalf("URI %s: %v", mode, err) + } + }) + } +} + +func TestURIClientResponseCloseContract(t *testing.T) { + const valid = `` + closeFailure := errors.New("URI response close failed") + readFailure := errors.New("URI response read failed") + for _, tc := range []struct { + name string + status int + reader func() io.Reader + cause error + }{ + {"valid assertion", http.StatusOK, func() io.Reader { return strings.NewReader(valid) }, nil}, + {"malformed XML", http.StatusOK, func() io.Reader { return strings.NewReader(" A Q I D BA== ` + m, err := binding.DecodeSOAP(t.Context(), []byte(wire), binding.SOAPOptions{Understood: []document.Name{{Space: protocol.ChannelBindingNamespace, Local: "ChannelBindings"}}}) + if err != nil { + t.Fatal(err) + } + values, err := ecp.ReadChannelHeaders(m.Headers(), false) + if err != nil || len(values) != 1 || !bytes.Equal(values[0].Data, []byte{1, 2, 3, 4}) { + t.Fatalf("independent channel header: %v %v", values, err) + } + matched, err := ecp.MatchChannelBindings([]protocol.ChannelBinding{{Type: ecp.TLSServerEndPoint, Data: []byte{1, 2, 3, 4}}}, values) + if err != nil || len(matched) != 1 { + t.Fatalf("binary channel comparison: %v", err) + } + values[0].Data[0] = 7 + if matched[0].Data[0] != 1 { + t.Fatal("verified channel bytes alias the received input") + } + if _, err := ecp.MatchChannelBindings(matched, values); err == nil { + t.Fatal("mismatched channel accepted") + } + if _, err := ecp.MatchChannelBindings(matched, nil); err == nil { + t.Fatal("signed channel without client channel accepted") + } + if _, err := ecp.MatchChannelBindings(nil, matched); err == nil { + t.Fatal("client channel without signed SP channel accepted") + } + bad := protocol.CloneChannelBindings(matched) + bad[0].Type = "tls-unique" + if _, err := ecp.MatchChannelBindings(matched, bad); err == nil { + t.Fatal("different named channel types matched") + } + if err := protocol.ValidateChannelBindings(append(matched, matched...), false); err == nil { + t.Fatal("duplicate channel types accepted") + } + if base64.StdEncoding.EncodeToString(matched[0].Data) != "AQIDBA==" { + t.Fatal("channel bytes changed during comparison") + } +} + +func TestTLSExporterChannelBinding(t *testing.T) { + serverChannel := make(chan protocol.ChannelBinding, 1) + server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + got, err := ecp.TLSExporterBinding(r.TLS) + if err != nil { + t.Error(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + serverChannel <- got + w.WriteHeader(http.StatusNoContent) + })) + defer server.Close() + request, err := http.NewRequestWithContext(t.Context(), http.MethodGet, server.URL, nil) + if err != nil { + t.Fatal(err) + } + // RFC 9266 4.1: do not reuse a channel after the authentication instance. + request.Close = true + response, err := server.Client().Do(request) + if err != nil { + t.Fatal(err) + } + defer func() { _ = response.Body.Close() }() + if response.StatusCode != http.StatusNoContent || response.TLS.Version != tls.VersionTLS13 { + t.Fatal("expected a completed TLS 1.3 exchange") + } + if _, err := io.Copy(io.Discard, response.Body); err != nil { + t.Fatal(err) + } + client, err := ecp.TLSExporterBinding(response.TLS) + if err != nil { + t.Fatal(err) + } + peer := <-serverChannel + expected, err := response.TLS.ExportKeyingMaterial("EXPORTER-Channel-Binding", nil, 32) + if err != nil || client.Type != "tls-exporter" || !bytes.Equal(client.Data, expected) || !bytes.Equal(client.Data, peer.Data) { + t.Fatalf("RFC 9266 exporter inputs or peer equality: %v", err) + } + if _, err := ecp.TLSExporterBinding(&tls.ConnectionState{Version: tls.VersionTLS12, HandshakeComplete: true}); err == nil { + t.Fatal("legacy TLS accepted without EMS/renegotiation guarantees") + } +} diff --git a/saml/ecp/ecp.go b/saml/ecp/ecp.go new file mode 100644 index 0000000..8aabde8 --- /dev/null +++ b/saml/ecp/ecp.go @@ -0,0 +1,959 @@ +// Package ecp implements the SAML 2.0 Enhanced Client or Proxy profile and PAOS +// routing. It never authenticates an end user; IdP application authentication and +// SP assertion verification are separate, mandatory operations. +package ecp + +import ( + "context" + "errors" + "math" + "mime" + "net/http" + "net/url" + "slices" + "strconv" + "strings" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/lexicon" + "github.com/skarm/xmlsec/internal/tree" + "github.com/skarm/xmlsec/saml/binding" + "github.com/skarm/xmlsec/saml/internal/profile" + "github.com/skarm/xmlsec/saml/internal/xmlutil" + "github.com/skarm/xmlsec/saml/metadata" + "github.com/skarm/xmlsec/saml/protocol" +) + +// Byte ceiling for the optional PAOS messageID echoed between ECP hops; SAML XML request IDs retain their independent NCName/profile checks. +const ( + maximumPAOSMessageIDBytes = 1024 +) + +// ECP SOAP relay text has a separate 4096-byte ceiling; this must not be substituted for the 80-byte HTTP binding RelayState limit. +const ( + maximumRelayStateBytes = 4096 +) + +// Liberty PAOS version token shared by advertisement and negotiation; only the configured PAOS namespace/version is supported. +const ( + paosVersionParameter = "ver=" +) + +// Local wire token shared by ECP/PAOS processing; each use still checks the vocabulary namespace and the relevant SOAP processing role. +const ( + fieldRelayState = "RelayState" + soapActorAttribute = "actor" + soapMustUnderstandAttribute = "mustUnderstand" + xmlNameAssertionConsumerServiceURL = "AssertionConsumerServiceURL" + xmlNameInResponseTo = "InResponseTo" + xmlNameIsPassive = "IsPassive" + xmlNameIssuer = "Issuer" + xmlNameMessageID = "messageID" + xmlNameMethod = "Method" + xmlNameRefToMessageID = "refToMessageID" + xmlNameRequest = "Request" + xmlNameRequestAuthenticated = "RequestAuthenticated" + xmlNameResponse = "Response" + xmlNameResponseConsumerURL = "responseConsumerURL" + xmlNameService = "service" + xmlNameSubjectConfirmation = "SubjectConfirmation" +) + +// Negotiation header shared by PAOS advertisement and parsing; its version/service tokens are checked independently of HTTP Accept. +const ( + headerPAOS = "Paos" +) + +// PAOS negotiation evaluates HTTP media ranges and quality values; advertisement or a matching Accept header is not proof of possession. +const ( + headerAccept = "Accept" +) + +// SOAP 1.1 mustUnderstand accepts the numeric lexical forms 0 and 1 here; ECP creation emits mandatory 1 and validates the processing actor separately. +const ( + soapBooleanTrue = "1" +) + +// Suggested prefix for emitted ECP headers; expanded namespace/local names determine acceptance, independently of prefix spelling. +const ( + prefixECP = "ecp" +) + +// Suggested prefix for emitted PAOS headers; expanded namespace/local names determine acceptance, independently of prefix spelling. +const ( + prefixPAOS = "paos" +) + +const ( + prefixSOAP = "env" + // Namespace identifies the ECP SOAP headers and PAOS service. + Namespace = "urn:oasis:names:tc:SAML:2.0:profiles:SSO:ecp" + // PAOSNamespace is the Liberty PAOS 1.0 header namespace/version. + PAOSNamespace = "urn:liberty:paos:2003-08" + // MediaType is the registered PAOS SOAP media type. + MediaType = lexicon.MediaTypePAOS +) + +// ErrECP reports a malformed or incorrectly routed ECP exchange. +var ErrECP = errors.New("SAML ECP exchange rejected") + +// Advertise adds the HTTP negotiation headers required by Profiles 4.2.3.1 and +// Approved Errata E21/E54. It retains existing Accept alternatives. +func Advertise(h http.Header) { + const ( + paosServiceDelimiter = "\"; \"" + paosVersionQuotedParameter = paosVersionParameter + `"` + ) + + h.Add(headerAccept, MediaType) + h.Set(headerPAOS, paosVersionQuotedParameter+PAOSNamespace+paosServiceDelimiter+Namespace+`"`) +} + +// acceptsPAOS checks Accept media ranges. A q=0 entry disables that range; +// an explicit range takes precedence over wildcards. +func acceptsPAOS(h http.Header) bool { + const ( + applicationMediaSpecificity = 1 + defaultMediaQuality = 1.0 + exactMediaSpecificity = 2 + mediaRangeAny = "*/*" + mediaRangeApplication = "application/*" + qualityFloatBitSize = 64 + qualityParameter = "q" + unmatchedMediaQuality = -1.0 + unmatchedMediaSpecificity = -1 + wildcardMediaSpecificity = 0 + ) + + quality := unmatchedMediaQuality + specificity := unmatchedMediaSpecificity + + for _, line := range h.Values(headerAccept) { + for part := range strings.SplitSeq(line, ",") { + media, p, err := mime.ParseMediaType(strings.TrimSpace(part)) + if err != nil { + return false + } + + spec := unmatchedMediaSpecificity + + switch media { + case MediaType: + spec = exactMediaSpecificity + case mediaRangeApplication: + spec = applicationMediaSpecificity + case mediaRangeAny: + spec = wildcardMediaSpecificity + } + + q := defaultMediaQuality + if raw, ok := p[qualityParameter]; ok { + q, err = strconv.ParseFloat(raw, qualityFloatBitSize) + if err != nil || q < 0 || q > 1 || math.IsNaN(q) || math.IsInf(q, 0) { + return false + } + } + + if spec > specificity || spec == specificity && q > quality { + specificity = spec + quality = q + } + } + } + + if specificity < 0 || quality <= 0 { + return false + } + + return specificity >= 0 && quality > 0 +} + +// Negotiated reports PAOS media, service and version support in the HTTP headers. +func Negotiated(h http.Header) bool { + const ( + maximumNegotiationHeaderBytes = 8192 + minimumPAOSParameters = 2 + ) + + if !acceptsPAOS(h) { + return false + } + + values := h.Values(headerPAOS) + if len(values) != 1 || len(values[0]) > maximumNegotiationHeaderBytes { + return false + } + + parts := strings.Split(values[0], ";") + if len(parts) < minimumPAOSParameters { + return false + } + + version := strings.TrimSpace(parts[0]) + if !strings.HasPrefix(version, paosVersionParameter) { + return false + } + + v, err := strconv.Unquote(strings.TrimSpace(strings.TrimPrefix(version, paosVersionParameter))) + if err != nil || v != PAOSNamespace { + return false + } + + for _, p := range parts[1:] { + service, err := strconv.Unquote(strings.TrimSpace(strings.Split(p, ",")[0])) + if err == nil && service == Namespace { + return true + } + } + + return false +} + +// Challenge wraps and signs an AuthnRequest for PAOS delivery. The caller must +// persist request/browser correlation before returning it. messageID optionally +// requests a matching PAOS refToMessageID; RelayState is echoed as an XML header. +func Challenge(ctx context.Context, request protocol.AuthnRequest, key protocol.SigningKey, relay *string, messageID string) (*document.Document, error) { + return challenge(ctx, request, key, relay, messageID, false) +} + +func challenge(ctx context.Context, request protocol.AuthnRequest, key protocol.SigningKey, relay *string, messageID string, hok bool) (*document.Document, error) { + request.ResponseBinding = metadata.PAOS + + d, err := protocol.SignAuthnRequest(ctx, request, key) + if err != nil { + return nil, err + } + + return wrapChallenge(ctx, d, key, relay, messageID, hok) +} + +// WrapChallenge adds ECP/PAOS headers to an existing signed PAOS AuthnRequest. +func WrapChallenge(ctx context.Context, request *document.Document, key protocol.SigningKey, relay *string, messageID string) (*document.Document, error) { + return wrapChallenge(ctx, request, key, relay, messageID, false) +} + +func wrapChallenge(ctx context.Context, request *document.Document, key protocol.SigningKey, relay *string, messageID string, hok bool) (*document.Document, error) { + const ( + xmlNameProtocolBinding = "ProtocolBinding" + ) + + if request == nil || request.Root().Name() != xmlutil.Name(protocol.ProtocolNamespace, lexicon.SAMLElementAuthnRequest) || xmlutil.Attr(request.Root(), xmlNameProtocolBinding) != metadata.PAOS || len(messageID) > maximumPAOSMessageIDBytes { + return nil, ErrECP + } + + acs := xmlutil.Attr(request.Root(), xmlNameAssertionConsumerServiceURL) + if !profile.Endpoint(acs, false) { + return nil, ErrECP + } + + paos := header(PAOSNamespace, prefixPAOS, xmlNameRequest) + xmlutil.Set(paos, xmlNameResponseConsumerURL, acs) + xmlutil.Set(paos, xmlNameService, Namespace) + + if messageID != "" { + xmlutil.Set(paos, xmlNameMessageID, messageID) + } + + req := header(Namespace, prefixECP, xmlNameRequest) + + passive, err := xmlutil.BoolAttr(request.Root(), xmlNameIsPassive) + if err != nil { + return nil, err + } + + xmlutil.Set(req, xmlNameIsPassive, strconv.FormatBool(passive)) + + issuer, err := xmlutil.Direct(request.Root(), xmlutil.Name(protocol.AssertionNamespace, xmlNameIssuer)) + if err != nil { + return nil, err + } + + _, node := tree.Unwrap(issuer) + xmlutil.Add(req, tree.ImportSubtree(node)) + + headers, err := headerElements(ctx, paos, req) + if err != nil { + return nil, err + } + + channels, err := requestChannels(request) + if err != nil { + return nil, err + } + + channelElements, err := channelHeaders(ctx, channels, true) + if err != nil { + return nil, err + } + + headers = append(headers, channelElements...) + + if hok { + h := header(Namespace, prefixECP, xmlNameSubjectConfirmation) + xmlutil.Set(h, xmlNameMethod, protocol.HolderOfKey) + + more, err := headerElements(ctx, h) + if err != nil { + return nil, err + } + + headers = append(headers, more...) + } + + if relay != nil { + h, err := relayHeader(ctx, *relay) + if err != nil { + return nil, err + } + + headers = append(headers, h) + } + + return protocol.SignSOAP(ctx, request, key, headers...) +} + +// Exchange retains SP routing state independently of the IdP response. It is +// created only from a challenge received over the application's trusted SP HTTP +// exchange. resourceURL is that request's absolute HTTPS URL, used only to resolve +// a relative responseConsumerURL allowed by Profiles 4.2.4.4 / Errata E35. +type Exchange struct { + relay document.Element + request *document.Document + acs, requestID, messageID string + forward []document.Element + channels []protocol.ChannelBinding + certificate []byte + passive bool + holder bool + bearer, modern bool +} + +// ParseChallenge validates required header shapes, SOAP actors, ACS and message +// type before producing the envelope to send to an independently trusted IdP. +func ParseChallenge(ctx context.Context, raw []byte, resourceURL string, limits binding.SOAPOptions) (*Exchange, error) { + x, err := parseChallenge(ctx, raw, resourceURL, nil, limits) + if err == nil { + if !x.bearer { + return nil, ErrECP + } + + x.holder = false + } + + return x, err +} + +func parseChallenge(ctx context.Context, raw []byte, resourceURL string, channels []protocol.ChannelBinding, limits binding.SOAPOptions) (*Exchange, error) { + const ( + maximumECPRequestChildren = 2 + xmlNameFormat = "Format" + xmlNameIDPList = "IDPList" + ) + + limits.Understood = append(slices.Clone(limits.Understood), knownHeaders()...) + + m, err := binding.DecodeSOAP(ctx, raw, limits) + if err != nil { + return nil, err + } + + x := &Exchange{passive: true} + + x.holder, x.bearer, err = offeredHoK(m.Headers()) + if err != nil { + return nil, err + } + + x.modern = slices.ContainsFunc(m.Headers(), func(h document.Element) bool { + return h.Name() == xmlutil.Name(Namespace, xmlNameSubjectConfirmation) + }) + + if err := rejectIDPChannelHeaders(m.Headers()); err != nil { + return nil, err + } + + x.channels, err = requestedChannels(m.Headers(), channels) + if err != nil { + return nil, err + } + + paos, request, relay, forward, err := challengeHeaders(m.Headers()) + if err != nil { + return nil, err + } + + x.relay = relay + x.forward = forward + + if validateChallengeHeaders(paos, request) != nil { + return nil, ErrECP + } + + x.messageID = xmlutil.Attr(paos, xmlNameMessageID) + if len(x.messageID) > maximumPAOSMessageIDBytes { + return nil, ErrECP + } + + base, err := url.Parse(resourceURL) + if err != nil || !profile.Endpoint(resourceURL, false) { + return nil, ErrECP + } + + ref, err := url.Parse(xmlutil.Attr(paos, xmlNameResponseConsumerURL)) + if err != nil || ref.String() == "" { + return nil, ErrECP + } + + x.acs = base.ResolveReference(ref).String() + if !profile.Endpoint(x.acs, false) { + return nil, ErrECP + } + + if _, present := request.Attribute(document.Name{Local: xmlNameIsPassive}); present { + x.passive, err = xmlutil.BoolAttr(request, xmlNameIsPassive) + if err != nil { + return nil, err + } + } + + cs, err := xmlutil.Children(request) + if err != nil || len(cs) < 1 || len(cs) > maximumECPRequestChildren || cs[0].Name() != xmlutil.Name(protocol.AssertionNamespace, xmlNameIssuer) { + return nil, ErrECP + } + + if xmlutil.Attrs(cs[0], nil, []string{xmlNameFormat}) != nil || xmlutil.Attr(cs[0], xmlNameFormat) != "" && xmlutil.Attr(cs[0], xmlNameFormat) != lexicon.SAMLNameIDFormatEntity { + return nil, ErrECP + } + + if len(cs) == maximumECPRequestChildren && cs[1].Name() != xmlutil.Name(protocol.ProtocolNamespace, xmlNameIDPList) { + return nil, ErrECP + } + + x.request, err = m.Payload(ctx) + if err != nil { + return nil, err + } + + if x.request.Root().Name() != xmlutil.Name(protocol.ProtocolNamespace, lexicon.SAMLElementAuthnRequest) { + return nil, ErrECP + } + + if err := matchingIssuers(x.request.Root(), cs[0]); err != nil { + return nil, err + } + + x.requestID = xmlutil.Attr(x.request.Root(), lexicon.SAMLAttrID) + if !profile.ID(x.requestID) { + return nil, ErrECP + } + + if acs := xmlutil.Attr(x.request.Root(), xmlNameAssertionConsumerServiceURL); acs != "" && acs != x.acs { + return nil, ErrECP + } + + return x, nil +} + +// IsPassive reports whether the ECP may interact with the user. An omitted ECP +// IsPassive header attribute defaults to true, unlike AuthnRequest.IsPassive. +func (x *Exchange) IsPassive() bool { return x.passive } + +// Endpoint returns the SP-controlled ACS retained before contacting the IdP. +func (x *Exchange) Endpoint() string { return x.acs } + +// ToIDP removes PAOS Request, ECP Request and RelayState headers while preserving +// the immutable SAML body and headers addressed to other SOAP actors. +func (x *Exchange) ToIDP(ctx context.Context) (*document.Document, error) { + headers := x.forward + if x.modern { + headers = nil + } + + if len(x.channels) != 0 { + var err error + + headers, err = channelHeaders(ctx, x.channels, false) + if err != nil { + return nil, err + } + } + + return binding.WrapSOAP(ctx, x.request, headers...) +} + +// Response adds the mandatory ECP response routing header. The caller must have +// authenticated the request and signed the SAML response for its authorized ACS. +// protocol.Response.SOAPContext must be set when signing an Inclusive C14N +// assertion. responseDestination is obtained from that verified request. +func Response(ctx context.Context, message *document.Document, responseDestination string) (*document.Document, error) { + return responseEnvelope(ctx, message, responseDestination, nil, false, document.Element{}) +} + +// ResponseWithBindings adds the IdP's channel verification signal to a signed +// response whose assertions contain the same authenticated Advice. The bindings +// must be the result of MatchChannelBindings, not merely copied input headers. +// The caller must have authenticated the AuthnRequest's XML signature; the +// optional RequestAuthenticated header reports that fact to extension-aware ECPs. +func ResponseWithBindings(ctx context.Context, message *document.Document, responseDestination string, channels []protocol.ChannelBinding) (*document.Document, error) { + return responseEnvelope(ctx, message, responseDestination, channels, true, document.Element{}) +} + +func responseEnvelope(ctx context.Context, message *document.Document, responseDestination string, channels []protocol.ChannelBinding, requestAuthenticated bool, confirmation document.Element) (*document.Document, error) { + const ( + xmlNameDestination = "Destination" + ) + + if message == nil || message.Root().Name() != xmlutil.Name(protocol.ProtocolNamespace, lexicon.SAMLElementResponse) || !profile.Endpoint(responseDestination, false) || xmlutil.Attr(message.Root(), xmlNameDestination) != responseDestination { + return nil, ErrECP + } + + h := header(Namespace, prefixECP, xmlNameResponse) + xmlutil.Set(h, xmlNameAssertionConsumerServiceURL, responseDestination) + + elements, err := headerElements(ctx, h) + if err != nil { + return nil, err + } + + cb, err := channelHeaders(ctx, channels, true) + if err != nil { + return nil, err + } + + elements = append(elements, cb...) + + if confirmation.Valid() { + h := header(Namespace, prefixECP, xmlNameSubjectConfirmation) + xmlutil.Set(h, xmlNameMethod, protocol.HolderOfKey) + + _, n := tree.Unwrap(confirmation) + xmlutil.Add(h, tree.ImportSubtree(n)) + + more, err := headerElements(ctx, h) + if err != nil { + return nil, err + } + + elements = append(elements, more...) + } + + if requestAuthenticated { + auth := header(Namespace, prefixECP, xmlNameRequestAuthenticated) + // CS01 2.3.6.1 permits omission of mustUnderstand. Older ECP + // clients can ignore this signal without failing the bearer flow. + auth.Attrs = slices.DeleteFunc(auth.Attrs, func(a tree.Attribute) bool { + return a.Name == xmlutil.Name(binding.SOAPNamespace, soapMustUnderstandAttribute) + }) + + more, err := headerElements(ctx, auth) + if err != nil { + return nil, err + } + + elements = append(elements, more...) + } + + return binding.WrapSOAP(ctx, message, elements...) +} + +// Finish removes the IdP's ECP Response header and prepares the PAOS return to +// the original SP. On an ACS mismatch it returns a SOAP Fault envelope and error, +// never the SAML response; deliver that fault only to Endpoint(). The original SP +// RelayState always takes precedence over a RelayState supplied by the IdP. +func (x *Exchange) Finish(ctx context.Context, raw []byte, limits binding.SOAPOptions) (*document.Document, error) { + const ( + faultCodeClient = "Client" + ) + + limits.Understood = append(slices.Clone(limits.Understood), knownHeaders()...) + + m, err := binding.DecodeSOAP(ctx, raw, limits) + if err != nil { + if fault, ok := errors.AsType[*binding.SOAPFault](err); ok { + return forwardFault(ctx, m, fault) + } + + return nil, err + } + + if err := rejectIDPChannelHeaders(m.Headers()); err != nil { + return nil, err + } + + response, relay, forward, err := responseHeaders(m.Headers()) + if err != nil { + return nil, err + } + + confirmed, err := ReadChannelHeaders(m.Headers(), true) + if err != nil { + return nil, err + } + + if len(x.channels) != 0 && len(confirmed) == 0 { + fault := binding.SOAPFault{Code: xmlutil.Name(binding.SOAPNamespace, faultCodeClient), Message: "IdP did not confirm ECP channel bindings"} + + d, err := binding.FaultSOAP(ctx, fault) + if err != nil { + return nil, err + } + + return d, &fault + } + + if len(x.channels) != 0 || x.modern { + forward = nil + } + + if validateHeader(response, []string{xmlNameAssertionConsumerServiceURL}, nil) != nil || !xmlutil.EmptyContent(response) { + return nil, ErrECP + } + + if xmlutil.Attr(response, xmlNameAssertionConsumerServiceURL) != x.acs { + fault := binding.SOAPFault{Code: xmlutil.Name(binding.SOAPNamespace, faultCodeClient), Message: "ECP response destination mismatch"} + + d, err := binding.FaultSOAP(ctx, fault) + if err != nil { + return nil, err + } + + return d, &fault + } + + d, err := m.Payload(ctx) + if err != nil { + return nil, err + } + + if d.Root().Name() != xmlutil.Name(protocol.ProtocolNamespace, lexicon.SAMLElementResponse) || xmlutil.Attr(d.Root(), xmlNameInResponseTo) != x.requestID { + return nil, ErrECP + } + + if err := x.checkConfirmation(m.Headers(), d); err != nil { + return nil, err + } + + if x.messageID != "" { + h := header(PAOSNamespace, prefixPAOS, xmlNameResponse) + xmlutil.Set(h, xmlNameRefToMessageID, x.messageID) + + es, err := headerElements(ctx, h) + if err != nil { + return nil, err + } + + forward = append(forward, es...) + } + + if x.relay.Valid() { + relay = x.relay + } + + if relay.Valid() { + forward = append(forward, relay) + } + + return binding.WrapSOAP(ctx, d, forward...) +} + +func forwardFault(ctx context.Context, m *binding.SOAPMessage, fault *binding.SOAPFault) (*document.Document, error) { + if m != nil { + payload, err := m.Payload(ctx) + if err != nil { + return nil, errors.Join(fault, err) + } + + d, err := binding.WrapSOAP(ctx, payload) + + return d, errors.Join(fault, err) + } + + d, err := binding.FaultSOAP(ctx, *fault) + + return d, errors.Join(fault, err) +} + +// DecodeResponse extracts an ECP-to-SP PAOS response and its RelayState. The SP +// must still perform full Web SSO assertion, correlation and replay checks. +// expectedMessageID is required only if the SP used PAOS messageID. +func DecodeResponse(ctx context.Context, raw []byte, expectedMessageID string, limits binding.SOAPOptions) (*document.Document, *string, error) { + limits.Understood = append(slices.Clone(limits.Understood), knownHeaders()...) + + m, err := binding.DecodeSOAP(ctx, raw, limits) + if err != nil { + return nil, nil, err + } + + return ExtractResponse(ctx, m, expectedMessageID) +} + +// ExtractResponse preserves the SOAP snapshot authenticated by an ECP proof. +// It checks routing headers only; the SAML body still requires verification. +func ExtractResponse(ctx context.Context, m *binding.SOAPMessage, expectedMessageID string) (*document.Document, *string, error) { + if m == nil { + return nil, nil, ErrECP + } + + var ( + relay *string + response document.Element + ) + + for _, h := range m.Headers() { + switch h.Name() { + case xmlutil.Name(Namespace, fieldRelayState): + if relay != nil || validateRelay(h) != nil { + return nil, nil, ErrECP + } + + text, err := xmlutil.SimpleText(h, maximumRelayStateBytes) + if err != nil { + return nil, nil, err + } + + relay = &text + case xmlutil.Name(PAOSNamespace, xmlNameResponse): + if response.Valid() || validateHeader(h, nil, []string{xmlNameRefToMessageID}) != nil || !xmlutil.EmptyContent(h) { + return nil, nil, ErrECP + } + + response = h + case xmlutil.Name(Namespace, xmlNameRequest), xmlutil.Name(Namespace, xmlNameResponse), xmlutil.Name(PAOSNamespace, xmlNameRequest): + return nil, nil, ErrECP + case channelHeaderName(), xmlutil.Name(Namespace, xmlNameRequestAuthenticated), xmlutil.Name(Namespace, xmlNameSubjectConfirmation): + return nil, nil, ErrECP + } + } + + if expectedMessageID != "" && (!response.Valid() || xmlutil.Attr(response, xmlNameRefToMessageID) != expectedMessageID) { + return nil, nil, ErrECP + } + + d, err := m.Payload(ctx) + if err != nil { + return nil, nil, err + } + + if d.Root().Name() != xmlutil.Name(protocol.ProtocolNamespace, lexicon.SAMLElementResponse) { + return nil, nil, ErrECP + } + + return d, relay, nil +} + +func knownHeaders() []document.Name { + return []document.Name{xmlutil.Name(PAOSNamespace, xmlNameRequest), xmlutil.Name(PAOSNamespace, xmlNameResponse), xmlutil.Name(Namespace, xmlNameRequest), xmlutil.Name(Namespace, xmlNameResponse), xmlutil.Name(Namespace, fieldRelayState), channelHeaderName(), xmlutil.Name(Namespace, xmlNameRequestAuthenticated), xmlutil.Name(Namespace, xmlNameSubjectConfirmation)} +} + +func header(ns, prefix, local string) *tree.Node { + n := xmlutil.Node(prefix, ns, local) + n.NS = []tree.Namespace{{Prefix: prefix, URI: ns}, {Prefix: prefixSOAP, URI: binding.SOAPNamespace}} + n.Attrs = append(n.Attrs, tree.Attribute{Name: xmlutil.Name(binding.SOAPNamespace, soapMustUnderstandAttribute), Prefix: prefixSOAP, Value: soapBooleanTrue}, tree.Attribute{Name: xmlutil.Name(binding.SOAPNamespace, soapActorAttribute), Prefix: prefixSOAP, Value: binding.SOAPNext}) + + return n +} + +func headerElements(ctx context.Context, nodes ...*tree.Node) ([]document.Element, error) { + var out []document.Element + + for _, n := range nodes { + d, err := xmlutil.Document(ctx, n) + if err != nil { + return nil, err + } + + out = append(out, d.Root()) + } + + return out, nil +} + +func relayHeader(ctx context.Context, value string) (document.Element, error) { + if len(value) > maximumRelayStateBytes { + return document.Element{}, ErrECP + } + + n := header(Namespace, prefixECP, fieldRelayState) + xmlutil.Text(n, value) + + d, err := xmlutil.Document(ctx, n) + if err != nil { + return document.Element{}, err + } + + return d.Root(), nil +} + +func validateRelay(h document.Element) error { + if validateHeader(h, nil, nil) != nil { + return ErrECP + } + + _, err := xmlutil.SimpleText(h, maximumRelayStateBytes) + + return err +} + +func validateHeader(h document.Element, required, optional []string) error { + if !h.Valid() { + return ErrECP + } + + seen := map[string]bool{} + + for _, a := range h.Attributes() { + if a.Name.Space == binding.SOAPNamespace { + if a.Name.Local == soapMustUnderstandAttribute && a.Value == soapBooleanTrue || a.Name.Local == soapActorAttribute && a.Value == binding.SOAPNext { + seen[a.Name.Space+a.Name.Local] = true + continue + } + + return ErrECP + } + + if a.Name.Space != "" { + return ErrECP + } + + ok := slices.Contains(append(append([]string(nil), required...), optional...), a.Name.Local) + + if !ok { + return ErrECP + } + + seen[a.Name.Local] = true + } + + if !seen[binding.SOAPNamespace+soapMustUnderstandAttribute] || !seen[binding.SOAPNamespace+soapActorAttribute] { + return ErrECP + } + + for _, name := range required { + if !seen[name] { + return ErrECP + } + } + + return nil +} + +func challengeHeaders(headers []document.Element) (document.Element, document.Element, document.Element, []document.Element, error) { + var ( + relay document.Element + forward []document.Element + paos, request document.Element + ) + + for _, h := range headers { + switch h.Name() { + case xmlutil.Name(Namespace, xmlNameSubjectConfirmation): + // offeredHoK validates and consumes these headers. + case channelHeaderName(): + // requestedChannels validates and consumes these headers. + case xmlutil.Name(PAOSNamespace, xmlNameRequest): + if paos.Valid() { + return document.Element{}, document.Element{}, document.Element{}, nil, ErrECP + } + + paos = h + case xmlutil.Name(Namespace, xmlNameRequest): + if request.Valid() { + return document.Element{}, document.Element{}, document.Element{}, nil, ErrECP + } + + request = h + case xmlutil.Name(Namespace, fieldRelayState): + if relay.Valid() || validateRelay(h) != nil { + return document.Element{}, document.Element{}, document.Element{}, nil, ErrECP + } + + relay = h + case xmlutil.Name(Namespace, xmlNameResponse), xmlutil.Name(PAOSNamespace, xmlNameResponse), xmlutil.Name(Namespace, xmlNameRequestAuthenticated): + return document.Element{}, document.Element{}, document.Element{}, nil, ErrECP + default: + if actor, _ := h.Attribute(xmlutil.Name(binding.SOAPNamespace, soapActorAttribute)); actor != binding.SOAPNext { + forward = append(forward, h) + } + } + } + + return paos, request, relay, forward, nil +} + +func matchingIssuers(body, header document.Element) error { + const ( + maximumIssuerBytes = 2048 + ) + + requestIssuer, err := xmlutil.Direct(body, xmlutil.Name(protocol.AssertionNamespace, xmlNameIssuer)) + if err != nil { + return ErrECP + } + + headerIssuer, err := xmlutil.SimpleText(header, maximumIssuerBytes) + if err != nil || !profile.URI(headerIssuer) { + return ErrECP + } + + bodyIssuer, err := xmlutil.SimpleText(requestIssuer, maximumIssuerBytes) + if err != nil || bodyIssuer != headerIssuer { + return ErrECP + } + + return nil +} + +func validateChallengeHeaders(paos, request document.Element) error { + const ( + xmlNameProviderName = "ProviderName" + ) + + if validateHeader(paos, []string{xmlNameResponseConsumerURL, xmlNameService}, []string{xmlNameMessageID}) != nil || !xmlutil.EmptyContent(paos) || xmlutil.Attr(paos, xmlNameService) != Namespace || validateHeader(request, nil, []string{xmlNameIsPassive, xmlNameProviderName}) != nil { + return ErrECP + } + + return nil +} + +func responseHeaders(headers []document.Element) (document.Element, document.Element, []document.Element, error) { + var ( + response, relay document.Element + forward []document.Element + authenticated bool + ) + + for _, h := range headers { + switch h.Name() { + case xmlutil.Name(Namespace, xmlNameSubjectConfirmation): + // checkConfirmation consumes these headers before returning to SP. + case channelHeaderName(): + // ReadChannelHeaders consumes these; they are never forwarded. + case xmlutil.Name(Namespace, xmlNameRequestAuthenticated): + if authenticated || authenticatedHeader(h) != nil { + return document.Element{}, document.Element{}, nil, ErrECP + } + + authenticated = true + case xmlutil.Name(Namespace, xmlNameResponse): + if response.Valid() { + return document.Element{}, document.Element{}, nil, ErrECP + } + + response = h + case xmlutil.Name(Namespace, fieldRelayState): + if relay.Valid() || validateRelay(h) != nil { + return document.Element{}, document.Element{}, nil, ErrECP + } + + relay = h + case xmlutil.Name(PAOSNamespace, xmlNameRequest), xmlutil.Name(PAOSNamespace, xmlNameResponse), xmlutil.Name(Namespace, xmlNameRequest): + return document.Element{}, document.Element{}, nil, ErrECP + default: + if actor, _ := h.Attribute(xmlutil.Name(binding.SOAPNamespace, soapActorAttribute)); actor != binding.SOAPNext { + forward = append(forward, h) + } + } + } + + return response, relay, forward, nil +} diff --git a/saml/ecp/ecp_test.go b/saml/ecp/ecp_test.go new file mode 100644 index 0000000..59efed7 --- /dev/null +++ b/saml/ecp/ecp_test.go @@ -0,0 +1,201 @@ +package ecp_test + +import ( + "bytes" + "crypto" + "errors" + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" + + "github.com/skarm/xmlsec/c14n" + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/testutil/testcert" + "github.com/skarm/xmlsec/saml/binding" + "github.com/skarm/xmlsec/saml/ecp" + "github.com/skarm/xmlsec/saml/metadata" + "github.com/skarm/xmlsec/saml/protocol" +) + +func TestPAOSNegotiation(t *testing.T) { + h := http.Header{} + ecp.Advertise(h) + if !ecp.Negotiated(h) || strings.Contains(h.Get("Paos"), "'") { + t.Fatal("Errata E21/E54 negotiation") + } + for _, accept := range []string{"application/vnd.paos+xml;q=0", "application/vnd.paos+xml;q=0, */*;q=1", "text/html", "application/vnd.paos+xml;q=bogus"} { + bad := h.Clone() + bad.Set("Accept", accept) + if ecp.Negotiated(bad) { + t.Fatalf("accepted %q", accept) + } + } + bad := h.Clone() + bad.Set("Paos", `ver="urn:wrong"; "`+ecp.Namespace+`"`) + if ecp.Negotiated(bad) { + t.Fatal("wrong version") + } +} + +func TestECPRoutingAndSignedBodies(t *testing.T) { + cert := testcert.New(t) + at := testcert.Now() + const spID = "https://sp.test/entity" + const idpID = "https://idp.test/entity" + const acs = "https://sp.test/acs" + const sso = "https://idp.test/soap" + for _, algorithm := range []string{c14n.Exclusive, c14n.Canonical10} { + t.Run(algorithm, func(t *testing.T) { + spKey := protocol.SigningKey{Key: cert.LeafKey, CertificateDER: cert.Leaf.Raw, Canonicalization: algorithm} + idpKey := protocol.SigningKey{Key: cert.TSAKey, CertificateDER: cert.TSA.Raw, Canonicalization: algorithm} + relay := "state from SP" + challenge, err := ecp.Challenge(t.Context(), protocol.AuthnRequest{ID: "_ecp", Issuer: spID, Destination: sso, ACS: acs, IssueInstant: at}, spKey, &relay, "paos-correlation") + if err != nil { + t.Fatal(err) + } + raw, _ := challenge.XML(t.Context()) + exchange, err := ecp.ParseChallenge(t.Context(), raw, "https://sp.test/resource", binding.SOAPOptions{}) + if err != nil || exchange.Endpoint() != acs || exchange.IsPassive() { + t.Fatalf("challenge: %v", err) + } + forward, err := exchange.ToIDP(t.Context()) + if err != nil { + t.Fatal(err) + } + forwardRaw, _ := forward.XML(t.Context()) + if bytes.Contains(forwardRaw, []byte("do not forwardapp:AuthenticationFaileddeniedaccount disabled` + forwardedFault, err = exchange.Finish(t.Context(), []byte(applicationFault), binding.SOAPOptions{}) + if !errors.As(err, &routingFault) || forwardedFault == nil || routingFault.Code.Space != "urn:application" { + t.Fatalf("application fault code lost: %v", err) + } + faultBytes, _ := forwardedFault.XML(t.Context()) + if !bytes.Contains(faultBytes, []byte("account disabled")) || bytes.Contains(faultBytes, []byte("do not forward")) { + t.Fatal("SOAP fault detail or hop-header processing changed") + } + // Relative consumer URL is allowed; resolve against the originating SP. + relative := bytes.Replace(raw, []byte(`responseConsumerURL="`+acs+`"`), []byte(`responseConsumerURL="/acs"`), 1) + if x, err := ecp.ParseChallenge(t.Context(), relative, "https://sp.test/resource", binding.SOAPOptions{}); err != nil || x.Endpoint() != acs { + t.Fatalf("relative URL: %v", err) + } + }) + } +} + +// A wire fixture independent of the generator checks the ECP-specific passive +// default (true), required actors and unknown mandatory header behavior. +func TestECPIndependentHeaders(t *testing.T) { + raw := `https://sp.test/entityhttps://sp.test/entity` + x, err := ecp.ParseChallenge(t.Context(), []byte(raw), "https://sp.test/resource", binding.SOAPOptions{}) + if err != nil || !x.IsPassive() { + t.Fatalf("passive default: %v", err) + } + for _, bad := range []string{strings.Replace(raw, `s:mustUnderstand="1"`, `s:mustUnderstand="0"`, 1), strings.Replace(raw, `s:actor="http://schemas.xmlsoap.org/soap/actor/next"`, `s:actor="urn:other"`, 1), strings.Replace(raw, "", ``, 1)} { + if _, err := ecp.ParseChallenge(t.Context(), []byte(bad), "https://sp.test/resource", binding.SOAPOptions{}); err == nil { + t.Fatal("invalid header accepted") + } + } + if _, err := document.Parse(t.Context(), []byte(raw), document.DefaultLimits()); err != nil { + t.Fatal(err) + } +} + +type shortChallengeWriter struct{ *httptest.ResponseRecorder } + +func (w shortChallengeWriter) Write(p []byte) (int, error) { + return w.ResponseRecorder.Write(p[:len(p)-1]) +} + +func challengeHTTPFixture(t *testing.T) (*http.Request, *document.Document) { + t.Helper() + r := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "https://sp.test/resource", nil) + ecp.Advertise(r.Header) + d, err := document.Parse(t.Context(), []byte(``), document.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + return r, d +} + +func TestWriteChallengeRejectsShortWrite(t *testing.T) { + t.Parallel() + r, d := challengeHTTPFixture(t) + writer := shortChallengeWriter{httptest.NewRecorder()} + if err := ecp.WriteChallenge(t.Context(), writer, r, d); !errors.Is(err, io.ErrShortWrite) { + t.Fatalf("short challenge write returned %v; want io.ErrShortWrite", err) + } + if writer.Code != http.StatusOK || writer.Header().Get("Content-Type") != ecp.MediaType+"; charset=utf-8" { + t.Fatal("challenge HTTP status or media type changed") + } +} + +func TestWriteChallengeRejectsTypedNilWriter(t *testing.T) { + t.Parallel() + r, d := challengeHTTPFixture(t) + var writer *httptest.ResponseRecorder + if err := ecp.WriteChallenge(t.Context(), writer, r, d); !errors.Is(err, ecp.ErrECP) { + t.Fatalf("typed-nil challenge writer returned %v; want ErrECP", err) + } +} diff --git a/saml/ecp/hok.go b/saml/ecp/hok.go new file mode 100644 index 0000000..5f500a7 --- /dev/null +++ b/saml/ecp/hok.go @@ -0,0 +1,289 @@ +package ecp + +import ( + "bytes" + "context" + "crypto/x509" + "net/http" + "slices" + "strconv" + "strings" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/tree" + "github.com/skarm/xmlsec/saml/binding" + "github.com/skarm/xmlsec/saml/internal/xmlutil" + "github.com/skarm/xmlsec/saml/proof" + "github.com/skarm/xmlsec/saml/protocol" +) + +// AdvertiseHoK announces the client's ECP 2013 HoK and channel-binding support. +func AdvertiseHoK(h http.Header) { + AdvertiseChannelBindings(h) + h.Set(headerPAOS, h.Get(headerPAOS)+", "+strconv.Quote(protocol.HolderOfKey)) +} + +// HoKNegotiated checks the explicit client option before offering HoK. +func HoKNegotiated(h http.Header) bool { + if !Negotiated(h) { + return false + } + + for _, service := range strings.Split(h.Get(headerPAOS), ";")[1:] { + options := strings.Split(service, ",") + + name, err := strconv.Unquote(strings.TrimSpace(options[0])) + if err != nil || name != Namespace { + continue + } + + for _, option := range options[1:] { + value, err := strconv.Unquote(strings.TrimSpace(option)) + if err == nil && value == protocol.HolderOfKey { + return true + } + } + } + + return false +} + +// HoKChallenge offers HoK. The HTTP handler must first check HoKNegotiated. +// The SP must retain a requirement for HoK with its pending authentication state. +func HoKChallenge(ctx context.Context, request protocol.AuthnRequest, key protocol.SigningKey, relay *string, messageID string) (*document.Document, error) { + return challenge(ctx, request, key, relay, messageID, true) +} + +// ParseHoKChallenge selects HoK and the client's exact attesting certificate. +// channels, when requested by the SP, come from the actual client/SP TLS channel. +func ParseHoKChallenge(ctx context.Context, raw []byte, resourceURL string, certificate []byte, channels []protocol.ChannelBinding, limits binding.SOAPOptions) (*Exchange, error) { + if _, err := x509.ParseCertificate(certificate); err != nil { + return nil, ErrECP + } + + x, err := parseChallenge(ctx, raw, resourceURL, channels, limits) + if err != nil { + return nil, err + } + + if !x.holder { + return nil, ErrECP + } + + x.certificate = bytes.Clone(certificate) + + return x, nil +} + +// VerifyEnvelopedXML recognizes ECP headers and verifies the declared W3C +// enveloped-signature form. The scope of CS01 2.3.11's restriction needs +// clarification; this API does not claim full ECP conformance. +func VerifyEnvelopedXML(ctx context.Context, raw []byte, o proof.Options) (*proof.Verified, error) { + o.SOAP.Understood = append(slices.Clone(o.SOAP.Understood), knownHeaders()...) + return proof.VerifyEnvelopedXML(ctx, raw, o) +} + +// VerifyCS01CompatXML recognizes ECP headers and verifies only the explicitly +// agreed CS01 no-Transforms compatibility form with nonstandard self-exclusion. +func VerifyCS01CompatXML(ctx context.Context, raw []byte, o proof.Options) (*proof.Verified, error) { + o.SOAP.Understood = append(slices.Clone(o.SOAP.Understood), knownHeaders()...) + return proof.VerifyCS01CompatXML(ctx, raw, o) +} + +// VerifyTLSRequest obtains proof from the actual server-side TLS request. +func VerifyTLSRequest(ctx context.Context, r *http.Request, o proof.Options) (*proof.Verified, error) { + o.SOAP.Understood = append(slices.Clone(o.SOAP.Understood), knownHeaders()...) + return proof.VerifyTLSRequest(ctx, r, o) +} + +// WriteIDPResponse writes an ECP SOAP response, including its mandatory headers. +// The response must already be signed for the authenticated request's ACS. +func WriteIDPResponse(ctx context.Context, w http.ResponseWriter, envelope *document.Document) error { + return binding.WriteSOAPResponseWithOptions(ctx, w, envelope, binding.SOAPOptions{Understood: knownHeaders()}) +} + +type xmlProofSigner func(context.Context, *document.Document, proof.Signing) (*document.Document, error) + +// ToIDPEnvelopedXML signs the IdP-bound message with a declared W3C enveloped +// transform, including any supplied client/IdP channel bindings. +func (x *Exchange) ToIDPEnvelopedXML(ctx context.Context, key proof.Signing, idpChannels []protocol.ChannelBinding) (*document.Document, error) { + return x.toIDPXML(ctx, key, idpChannels, proof.SignEnvelopedXML) +} + +// ToIDPCS01CompatXML creates the explicitly agreed CS01 no-Transforms +// compatibility form. Its self-exclusion rule is not ordinary W3C processing. +func (x *Exchange) ToIDPCS01CompatXML(ctx context.Context, key proof.Signing, idpChannels []protocol.ChannelBinding) (*document.Document, error) { + return x.toIDPXML(ctx, key, idpChannels, proof.SignCS01CompatXML) +} + +func (x *Exchange) toIDPXML(ctx context.Context, key proof.Signing, idpChannels []protocol.ChannelBinding, sign xmlProofSigner) (*document.Document, error) { + if x == nil || !x.holder || !bytes.Equal(x.certificate, key.Certificate) { + return nil, ErrECP + } + + d, err := x.ToIDP(ctx) + if err != nil { + return nil, err + } + + if len(idpChannels) != 0 { + raw, err := d.XML(ctx) + if err != nil { + return nil, err + } + + m, err := binding.DecodeSOAP(ctx, raw, binding.SOAPOptions{Understood: knownHeaders()}) + if err != nil { + return nil, err + } + + body, err := m.Payload(ctx) + if err != nil { + return nil, err + } + + extra, err := channelHeaders(ctx, idpChannels, false) + if err != nil { + return nil, err + } + + for i, h := range extra { + _, original := tree.Unwrap(h) + n := tree.ImportSubtree(original) + n.Attrs = slices.DeleteFunc(n.Attrs, func(a tree.Attribute) bool { return a.Name == xmlutil.Name(binding.SOAPNamespace, soapActorAttribute) }) + + one, err := headerElements(ctx, n) + if err != nil { + return nil, err + } + + extra[i] = one[0] + } + + d, err = binding.WrapSOAP(ctx, body, append(m.Headers(), extra...)...) + if err != nil { + return nil, err + } + } + + return sign(ctx, d, key) +} + +// ResponseHoK adds confirmation data identical to the assertion producer's data. +// certificate must be the proven key explicitly associated with the principal. +func ResponseHoK(ctx context.Context, message *document.Document, destination string, certificate []byte, channels []protocol.ChannelBinding) (*document.Document, error) { + if message == nil { + return nil, ErrECP + } + + data, err := protocol.HolderOfKeyData(ctx, certificate, destination, xmlutil.Attr(message.Root(), xmlNameInResponseTo)) + if err != nil { + return nil, err + } + + return responseEnvelope(ctx, message, destination, channels, true, data) +} + +// FinishEnvelopedXML checks IdP routing and the HoK certificate, then signs +// the SP-bound message with a declared W3C enveloped transform. Routing faults +// are returned without a proof signature. +func (x *Exchange) FinishEnvelopedXML(ctx context.Context, raw []byte, key proof.Signing, limits binding.SOAPOptions) (*document.Document, error) { + return x.finishXML(ctx, raw, key, limits, proof.SignEnvelopedXML) +} + +// FinishCS01CompatXML checks IdP routing and the HoK certificate, then signs +// using the explicitly agreed CS01 no-Transforms compatibility form. Routing +// faults are returned without a proof signature. +func (x *Exchange) FinishCS01CompatXML(ctx context.Context, raw []byte, key proof.Signing, limits binding.SOAPOptions) (*document.Document, error) { + return x.finishXML(ctx, raw, key, limits, proof.SignCS01CompatXML) +} + +func (x *Exchange) finishXML(ctx context.Context, raw []byte, key proof.Signing, limits binding.SOAPOptions, sign xmlProofSigner) (*document.Document, error) { + if x == nil || !x.holder || !bytes.Equal(x.certificate, key.Certificate) { + return nil, ErrECP + } + + d, err := x.Finish(ctx, raw, limits) + if err != nil { + return d, err + } + + return sign(ctx, d, key) +} + +func offeredHoK(headers []document.Element) (bool, bool, error) { + seen := map[string]bool{} + + for _, h := range headers { + if h.Name() != xmlutil.Name(Namespace, xmlNameSubjectConfirmation) { + continue + } + + method := xmlutil.Attr(h, xmlNameMethod) + if validateHeader(h, []string{xmlNameMethod}, nil) != nil || !xmlutil.EmptyContent(h) || seen[method] || method != protocol.HolderOfKey && method != protocol.Bearer { + return false, false, ErrECP + } + + seen[method] = true + } + + return seen[protocol.HolderOfKey], len(seen) == 0 || seen[protocol.Bearer], nil +} + +func (x *Exchange) checkConfirmation(headers []document.Element, response *document.Document) error { + const ( + xmlNameStatus = "Status" + xmlNameSubjectConfirmationData = "SubjectConfirmationData" + xmlNameValue = "Value" + ) + + var found document.Element + + for _, h := range headers { + if h.Name() != xmlutil.Name(Namespace, xmlNameSubjectConfirmation) { + continue + } + + if found.Valid() || validateHeader(h, []string{xmlNameMethod}, nil) != nil { + return ErrECP + } + + found = h + } + + success := false + + for _, c := range response.Root().Children() { + if c.Name() == xmlutil.Name(protocol.ProtocolNamespace, xmlNameStatus) { + cs, err := xmlutil.Children(c) + success = err == nil && len(cs) != 0 && xmlutil.Attr(cs[0], xmlNameValue) == protocol.Success + } + } + + if !success { + return nil + } + + if !x.holder { + if found.Valid() { + return ErrECP + } + + return nil + } + + if !found.Valid() || xmlutil.Attr(found, xmlNameMethod) != protocol.HolderOfKey { + return ErrECP + } + + cs, err := xmlutil.Children(found) + if err != nil || len(cs) != 1 || cs[0].Name() != xmlutil.Name(protocol.AssertionNamespace, xmlNameSubjectConfirmationData) { + return ErrECP + } + + if err := protocol.MatchConfirmationCertificate(cs[0], x.certificate); err != nil { + return ErrECP + } + + return nil +} diff --git a/saml/ecp/hok_test.go b/saml/ecp/hok_test.go new file mode 100644 index 0000000..0477f72 --- /dev/null +++ b/saml/ecp/hok_test.go @@ -0,0 +1,53 @@ +package ecp_test + +import ( + "bytes" + "net/http" + "testing" + + "github.com/skarm/xmlsec/internal/testutil/testcert" + "github.com/skarm/xmlsec/saml/binding" + "github.com/skarm/xmlsec/saml/ecp" + "github.com/skarm/xmlsec/saml/protocol" +) + +func TestHoKNegotiationAndAlternatives(t *testing.T) { + h := http.Header{} + ecp.AdvertiseHoK(h) + if !ecp.HoKNegotiated(h) || !ecp.NegotiatedChannelBindings(h) { + t.Fatal("missing HoK/channel negotiation") + } + ecp.Advertise(h) + if ecp.HoKNegotiated(h) { + t.Fatal("legacy client negotiated HoK") + } + c := testcert.New(t) + d, err := ecp.HoKChallenge(t.Context(), protocol.AuthnRequest{ID: "_choices", Issuer: "https://sp.test/entity", Destination: "https://idp.test/sso", ACS: "https://sp.test/acs", IssueInstant: testcert.Now()}, protocol.SigningKey{Key: c.LeafKey}, nil, "") + if err != nil { + t.Fatal(err) + } + raw, err := d.XML(t.Context()) + if err != nil { + t.Fatal(err) + } + if _, err := ecp.ParseChallenge(t.Context(), raw, "https://sp.test/resource", binding.SOAPOptions{}); err == nil { + t.Fatal("HoK-only challenge accepted as bearer") + } + const bearer = `` + mixed := bytes.Replace(raw, []byte(``), []byte(bearer+``), 1) + if _, err := ecp.ParseChallenge(t.Context(), mixed, "https://sp.test/resource", binding.SOAPOptions{}); err != nil { + t.Fatalf("explicit bearer alternative: %v", err) + } + if _, err := ecp.ParseHoKChallenge(t.Context(), mixed, "https://sp.test/resource", c.Leaf.Raw, nil, binding.SOAPOptions{}); err != nil { + t.Fatalf("HoK alternative: %v", err) + } + duplicate := bytes.Replace(mixed, []byte(``), []byte(bearer+``), 1) + if _, err := ecp.ParseHoKChallenge(t.Context(), duplicate, "https://sp.test/resource", c.Leaf.Raw, nil, binding.SOAPOptions{}); err == nil { + t.Fatal("duplicate method accepted") + } + const ownChannel = `AQID` + misplaced := bytes.Replace(raw, []byte(``), []byte(ownChannel+``), 1) + if _, err := ecp.ParseHoKChallenge(t.Context(), misplaced, "https://sp.test/resource", c.Leaf.Raw, nil, binding.SOAPOptions{}); err == nil { + t.Fatal("client/IdP channel accepted in SP challenge") + } +} diff --git a/saml/ecp/http.go b/saml/ecp/http.go new file mode 100644 index 0000000..343d08e --- /dev/null +++ b/saml/ecp/http.go @@ -0,0 +1,143 @@ +package ecp + +import ( + "bytes" + "context" + "errors" + "io" + "mime" + "net/http" + "time" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/lexicon" + "github.com/skarm/xmlsec/internal/nilcheck" + "github.com/skarm/xmlsec/internal/streamio" + "github.com/skarm/xmlsec/saml/binding" +) + +// Canonical HTTP field spellings shared by the PAOS challenge and SP delivery writers; cache directives keep the ECP protocol responses out of intermediary storage. +const ( + headerCacheControl = "Cache-Control" + headerPragma = "Pragma" +) + +// The ECP HTTP writer declares its serialized SOAP bytes as UTF-8; this is an output media parameter, not a parser permission. +const ( + charsetUTF8Parameter = "; charset=" + lexicon.EncodingUTF8 +) + +// Transport cache directive for sensitive SAML responses; shared writers must retain the same no-store/HTTP compatibility behavior. +const ( + cacheControlNoStore = "no-cache, no-store" + cachePragma = "no-cache" +) + +// WriteChallenge returns the negotiated ECP challenge as HTTP 200 PAOS content. +// Persist the SP request state before calling this method. +func WriteChallenge(ctx context.Context, w http.ResponseWriter, r *http.Request, envelope *document.Document) error { + if ctx == nil || nilcheck.IsNil(w) || r == nil || envelope == nil || !Negotiated(r.Header) { + return ErrECP + } + + raw, err := envelope.XML(ctx) + if err != nil { + return err + } + + if _, err := binding.DecodeSOAP(ctx, raw, binding.SOAPOptions{Understood: knownHeaders()}); err != nil { + return err + } + + w.Header().Set(lexicon.HeaderContentType, MediaType+charsetUTF8Parameter) + w.Header().Set(headerCacheControl, cacheControlNoStore) + w.Header().Set(headerPragma, cachePragma) + w.WriteHeader(http.StatusOK) + + n, err := w.Write(raw) + if err == nil && n != len(raw) { + err = io.ErrShortWrite + } + + return err +} + +// ReadResponse reads the bounded PAOS POST delivered to the SP. It returns an +// unverified SAML response; SP.ConsumePAOS supplies the full Web SSO processing. +func ReadResponse(ctx context.Context, r *http.Request, expectedMessageID string, o binding.SOAPOptions) (*document.Document, *string, error) { + const ( + defaultSOAPBytes = 2 << 20 + maximumSOAPBytes = 16 << 20 + ) + + if ctx == nil || r == nil || r.Body == nil || r.Method != http.MethodPost { + return nil, nil, ErrECP + } + + media, _, err := mime.ParseMediaType(r.Header.Get(lexicon.HeaderContentType)) + if err != nil || media != MediaType { + return nil, nil, ErrECP + } + + maximum := o.MaxBytes + if maximum == 0 { + maximum = defaultSOAPBytes + } + + if maximum < 1 || maximum > maximumSOAPBytes { + return nil, nil, ErrECP + } + + raw, err := io.ReadAll(&streamio.Reader{Context: ctx, Source: r.Body, Limit: int64(maximum), LimitError: ErrECP}) + if err != nil { + return nil, nil, err + } + + return DecodeResponse(ctx, raw, expectedMessageID, o) +} + +// Deliver completes the ECP hop to the SP-controlled ACS. On a destination +// mismatch it sends only the generated SOAP fault to that original ACS and +// returns the routing error. Redirects are never followed. The caller owns and +// must close any returned response body, even when a routing error is returned. +func (x *Exchange) Deliver(ctx context.Context, idpResponse []byte, client *http.Client, o binding.SOAPOptions) (*http.Response, error) { + const ( + defaultHTTPTimeout = 30 * time.Second + headerSOAPAction = "SoapAction" + samlSOAPAction = "\"http://www.oasis-open.org/committees/security\"" + ) + + envelope, routingErr := x.Finish(ctx, idpResponse, o) + if envelope == nil { + return nil, routingErr + } + + raw, err := envelope.XML(ctx) + if err != nil { + return nil, errors.Join(routingErr, err) + } + + req, err := http.NewRequestWithContext(ctx, http.MethodPost, x.acs, bytes.NewReader(raw)) + if err != nil { + return nil, errors.Join(routingErr, err) + } + + req.Header.Set(lexicon.HeaderContentType, MediaType+charsetUTF8Parameter) + req.Header.Set(headerSOAPAction, samlSOAPAction) + req.Header.Set(headerCacheControl, cacheControlNoStore) + req.Header.Set(headerPragma, cachePragma) + + c := http.Client{Timeout: defaultHTTPTimeout} + if client != nil { + c = *client + } + + c.CheckRedirect = func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse } + + response, err := c.Do(req) + if err != nil { + return nil, errors.Join(routingErr, err) + } + + return response, routingErr +} diff --git a/saml/ecp/roles_test.go b/saml/ecp/roles_test.go new file mode 100644 index 0000000..b3b68be --- /dev/null +++ b/saml/ecp/roles_test.go @@ -0,0 +1,268 @@ +package ecp_test + +import ( + "bytes" + "encoding/base64" + "errors" + "testing" + "time" + + "github.com/skarm/xmlsec/c14n" + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/testutil/testcert" + "github.com/skarm/xmlsec/saml/binding" + "github.com/skarm/xmlsec/saml/ecp" + "github.com/skarm/xmlsec/saml/idp" + "github.com/skarm/xmlsec/saml/metadata" + "github.com/skarm/xmlsec/saml/protocol" + "github.com/skarm/xmlsec/saml/replay" + "github.com/skarm/xmlsec/saml/sp" + "github.com/skarm/xmlsec/saml/state" +) + +// Both high-level roles participate: current metadata, PAOS ACS selection, +// application authentication, complete assertion verification and atomic replay. +func TestECPServiceProviderIdentityProvider(t *testing.T) { + for _, bound := range []bool{false, true} { + name := "bearer" + if bound { + name = "channel-bound" + } + + t.Run(name, func(t *testing.T) { runECPRoles(t, bound) }) + } +} + +func runECPRoles(t *testing.T, bound bool) { + t.Helper() + const spID = "https://sp.test/entity" + const idpID = "https://idp.test/entity" + const acs = "https://sp.test/acs" + const sso = "https://idp.test/soap" + at := testcert.Now() + cert := testcert.New(t) + spKey := protocol.SigningKey{Key: cert.LeafKey, CertificateDER: cert.Leaf.Raw, Canonicalization: c14n.Canonical10} + idpKey := protocol.SigningKey{Key: cert.TSAKey, CertificateDER: cert.TSA.Raw, Canonicalization: c14n.Canonical10} + specs := []metadata.EntityDescriptor{ + {ID: "_sp", EntityID: spID, ValidUntil: at.Add(time.Hour), SP: &metadata.RoleDescriptor{Keys: []metadata.KeyDescriptor{{Use: "signing", CertificateDER: spKey.CertificateDER}}, Endpoints: map[string][]metadata.Endpoint{metadata.AssertionConsumerService: {{Binding: metadata.PAOS, Location: acs, Indexed: true, Index: 3, Default: true}}}, NameIDFormats: []string{protocol.NameIDPersistent}}}, + {ID: "_idp", EntityID: idpID, ValidUntil: at.Add(time.Hour), IDP: &metadata.RoleDescriptor{Keys: []metadata.KeyDescriptor{{Use: "signing", CertificateDER: idpKey.CertificateDER}}, Endpoints: map[string][]metadata.Endpoint{metadata.SingleSignOnService: {{Binding: metadata.SOAP, Location: sso}}}, NameIDFormats: []string{protocol.NameIDPersistent}}}, + } + specs[1].IDP.Endpoints[metadata.SingleLogoutService] = []metadata.Endpoint{{Binding: metadata.SOAP, Location: "https://idp.test/slo"}} + var raw bytes.Buffer + raw.WriteString(``) + for _, spec := range specs { + d, err := metadata.Build(t.Context(), spec, at, metadata.Options{}) + if err != nil { + t.Fatal(err) + } + b, err := d.XML(t.Context()) + if err != nil { + t.Fatal(err) + } + raw.Write(b) + } + raw.WriteString(``) + partners, err := metadata.ParseTrusted(t.Context(), raw.Bytes(), at, metadata.Options{}) + if err != nil { + t.Fatal(err) + } + requests, err := state.NewMemoryStore(state.Config{}) + if err != nil { + t.Fatal(err) + } + spReplay, err := replay.NewMemoryStore(replay.Config{}) + if err != nil { + t.Fatal(err) + } + idpReplay, err := replay.NewMemoryStore(replay.Config{}) + if err != nil { + t.Fatal(err) + } + service, err := sp.NewServiceProvider(sp.Config{EntityID: spID, Metadata: partners, Signing: spKey, Requests: requests, Replay: spReplay}) + if err != nil { + t.Fatal(err) + } + identity, err := idp.NewIdentityProvider(idp.Config{EntityID: idpID, Metadata: partners, Signing: idpKey, Replay: idpReplay}) + if err != nil { + t.Fatal(err) + } + browser := bytes.Repeat([]byte{42}, 32) + var channels []protocol.ChannelBinding + if bound { + channel, err := ecp.ServerCertificateBinding(cert.Leaf) + if err != nil { + t.Fatal(err) + } + channels = []protocol.ChannelBinding{channel} + } + + out, err := service.Begin(t.Context(), sp.BeginOptions{IDP: idpID, Binding: metadata.PAOS, BrowserBinding: browser, At: at, NameIDFormat: protocol.NameIDPersistent, ChannelBindings: channels}) + if err != nil { + t.Fatal(err) + } + challenge, err := out.PAOS() + if err != nil { + t.Fatal(err) + } + b, _ := challenge.XML(t.Context()) + x := parseRolesChallenge(t, b, channels) + if bound { + if _, err := ecp.ParseChallenge(t.Context(), b, "https://sp.test/resource", binding.SOAPOptions{}); err == nil { + t.Fatal("channel request accepted without trusted transport data") + } + } + + forward, err := x.ToIDP(t.Context()) + if err != nil { + t.Fatal(err) + } + b, _ = forward.XML(t.Context()) + if bound { + // Changing the client's channel header leaves the signed request valid. + // The IdP must nevertheless reject the authentication, not downgrade it. + bad := bytes.Replace(b, []byte(base64.StdEncoding.EncodeToString(channels[0].Data)), []byte(base64.StdEncoding.EncodeToString(bytes.Repeat([]byte{91}, len(channels[0].Data)))), 1) + rejected, err := identity.AcceptSOAP(t.Context(), bad, spID, sso, at) + if err == nil || rejected == nil { + t.Fatalf("authenticated mismatched channel: request=%v error=%v", rejected, err) + } + if _, err := identity.Issue(t.Context(), rejected, idp.Authentication{NameID: "alice", AuthnInstant: at, AuthnContext: "urn:test:password", SessionIndex: "device-1"}, at); err == nil { + t.Fatal("mismatched channel issued a successful assertion") + } + } + r, err := identity.AcceptSOAP(t.Context(), b, spID, sso, at) + if err != nil { + t.Fatal(err) + } + if _, err := identity.Issue(t.Context(), r, idp.Authentication{NameID: "alice", NameIDFormat: protocol.NameIDPersistent, AuthnInstant: at, AuthnContext: "urn:test:password"}, at); err == nil { + t.Fatal("SLO-capable IdP issued an assertion without SessionIndex") + } + badResponse, err := protocol.SignResponse(t.Context(), protocol.Response{ID: "_missing_session", Issuer: idpID, Audience: spID, Destination: acs, InResponseTo: out.RequestID(), IssueInstant: at, SOAPContext: true, Assertion: protocol.AssertionData{ID: "_missing_session_assertion", NameID: "alice", NameIDFormat: protocol.NameIDPersistent, NotBefore: at, NotOnOrAfter: at.Add(time.Minute), AuthnInstant: at, AuthnContext: "urn:test:password"}}, idpKey) + if err != nil { + t.Fatal(err) + } + badEnvelope, err := ecp.ResponseWithBindings(t.Context(), badResponse, acs, channels) + if err != nil { + t.Fatal(err) + } + badRaw, _ := badEnvelope.XML(t.Context()) + badReturn, err := x.Finish(t.Context(), badRaw, binding.SOAPOptions{}) + if err != nil { + t.Fatal(err) + } + badRaw, _ = badReturn.XML(t.Context()) + if _, err := service.ConsumePAOS(t.Context(), badRaw, sp.ConsumeOptions{At: at, BrowserBinding: browser}); err == nil { + t.Fatal("SP accepted an SLO-capable peer's assertion without SessionIndex") + } + issued, err := identity.Issue(t.Context(), r, idp.Authentication{NameID: "current-primary-id", NameIDFormat: protocol.NameIDPersistent, NameQualifier: idpID, SPNameQualifier: spID, SPProvidedID: "current-sp-alias", AuthnInstant: at, AuthnContext: "urn:test:password", SessionIndex: "device-1"}, at) + if err != nil { + t.Fatal(err) + } + if _, err := identity.Issue(t.Context(), r, idp.Authentication{NameID: "alice", AuthnInstant: at, AuthnContext: "urn:test:password"}, at); err == nil { + t.Fatal("IdP request reused") + } + envelope, err := issued.ECP(t.Context()) + if err != nil { + t.Fatal(err) + } + b, _ = envelope.XML(t.Context()) + if !bound { + // A 2005 ECP does not know RequestAuthenticated. The IdP must not + // make understanding this new CS01 signal mandatory for that client. + legacyHeaders := []document.Name{{Space: ecp.Namespace, Local: "Response"}, {Space: ecp.Namespace, Local: "RelayState"}} + if _, err := binding.DecodeSOAP(t.Context(), b, binding.SOAPOptions{Understood: legacyHeaders}); err != nil { + t.Fatalf("2013 extension broke a legacy bearer ECP: %v", err) + } + } + if bound { + without, err := ecp.Response(t.Context(), issued.Document(), acs) + if err != nil { + t.Fatal(err) + } + withoutRaw, _ := without.XML(t.Context()) + fault, err := x.Finish(t.Context(), withoutRaw, binding.SOAPOptions{}) + var soapFault *binding.SOAPFault + if !errors.As(err, &soapFault) || fault == nil { + t.Fatalf("missing IdP channel signal: %v", err) + } + faultRaw, _ := fault.XML(t.Context()) + if bytes.Contains(faultRaw, []byte("current-primary-id")) { + t.Fatal("fault disclosed the SAML assertion") + } + } + returned, err := x.Finish(t.Context(), b, binding.SOAPOptions{}) + if err != nil { + t.Fatal(err) + } + b, _ = returned.XML(t.Context()) + wrong := sp.ConsumeOptions{At: at, BrowserBinding: bytes.Repeat([]byte{43}, 32)} + if _, err := service.ConsumePAOS(t.Context(), b, wrong); err == nil { + t.Fatal("wrong browser accepted") + } + assertion, err := service.ConsumePAOS(t.Context(), b, sp.ConsumeOptions{At: at, BrowserBinding: browser}) + if err != nil { + t.Fatal(err) + } + if assertion.NameID() != "current-primary-id" || assertion.Identifier().SPProvidedID != "current-sp-alias" || assertion.SessionIndex() != "device-1" { + t.Fatal("managed NameID lost between IdP and SP") + } + if _, err := service.ConsumePAOS(t.Context(), b, sp.ConsumeOptions{At: at, BrowserBinding: browser}); err == nil { + t.Fatal("SP response reused") + } + // An IdP may return an authentication failure instead of an assertion. Its + // nested status remains authenticated and correlated all the way to the SP. + out, err = service.Begin(t.Context(), sp.BeginOptions{IDP: idpID, Binding: metadata.PAOS, BrowserBinding: browser, At: at, ChannelBindings: channels}) + if err != nil { + t.Fatal(err) + } + challenge, err = out.PAOS() + if err != nil { + t.Fatal(err) + } + b, _ = challenge.XML(t.Context()) + x = parseRolesChallenge(t, b, channels) + forward, err = x.ToIDP(t.Context()) + if err != nil { + t.Fatal(err) + } + b, _ = forward.XML(t.Context()) + r, err = identity.AcceptSOAP(t.Context(), b, spID, sso, at) + if err != nil { + t.Fatal(err) + } + denied, err := identity.Reject(t.Context(), r, protocol.Status{Code: protocol.Responder, Subcodes: []string{"urn:oasis:names:tc:SAML:2.0:status:AuthnFailed"}, Message: "authentication failed"}, at) + if err != nil { + t.Fatal(err) + } + envelope, err = denied.ECP(t.Context()) + if err != nil { + t.Fatal(err) + } + b, _ = envelope.XML(t.Context()) + returned, err = x.Finish(t.Context(), b, binding.SOAPOptions{}) + if err != nil { + t.Fatal(err) + } + b, _ = returned.XML(t.Context()) + _, err = service.ConsumePAOS(t.Context(), b, sp.ConsumeOptions{At: at, BrowserBinding: browser}) + var failure *protocol.StatusError + if !errors.As(err, &failure) || failure.Code != protocol.Responder || len(failure.Subcodes) != 1 || failure.Subcodes[0] != "urn:oasis:names:tc:SAML:2.0:status:AuthnFailed" { + t.Fatalf("authenticated error response: %v", err) + } +} + +func parseRolesChallenge(t *testing.T, raw []byte, channels []protocol.ChannelBinding) *ecp.Exchange { + t.Helper() + var ( + x *ecp.Exchange + err error + ) + if len(channels) == 0 { + x, err = ecp.ParseChallenge(t.Context(), raw, "https://sp.test/resource", binding.SOAPOptions{}) + } else { + x, err = ecp.ParseChallengeWithBindings(t.Context(), raw, "https://sp.test/resource", channels, binding.SOAPOptions{}) + } + if err != nil { + t.Fatal(err) + } + return x +} diff --git a/saml/idp/doc.go b/saml/idp/doc.go index dc2fe9d..2f5b9e7 100644 --- a/saml/idp/doc.go +++ b/saml/idp/doc.go @@ -14,7 +14,13 @@ // The application must truthfully report the authentication instant, context and // interaction conditions, including ForceAuthn and IsPassive requirements. // Issue signs and self-verifies the response, then atomically consumes the request -// before returning [Issued] data for POST delivery. +// before returning [Issued] data for POST, Artifact or ECP delivery. +// AcceptSOAP handles bearer ECP requests; AcceptECP additionally requires HoK +// proof and checks supplied client/IdP channel bindings. HoK issuance requires +// an application-established association between the principal and proven +// certificate. AcceptArtifact validates resolved requests. +// Reject returns an authenticated error response. Publishing SingleLogoutService +// requires an application-assigned SessionIndex in Authentication. // // # Application integration // @@ -22,5 +28,6 @@ // sessions. It relies on authenticated metadata and application-owned adapters. // A multi-instance deployment needs shared atomic replay storage; callbacks must // honor context. The supported flow is solicited WebSSO, not a general SAML -// protocol server. +// protocol server. SLO and managed identifiers are composed with +// github.com/skarm/xmlsec/saml/session and application-owned sessions/bindings. package idp diff --git a/saml/idp/idp.go b/saml/idp/idp.go index 354f6ec..f673021 100644 --- a/saml/idp/idp.go +++ b/saml/idp/idp.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "crypto" + "crypto/rsa" "crypto/sha256" "crypto/x509" "encoding/hex" @@ -13,12 +14,16 @@ import ( "time" "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/nilcheck" "github.com/skarm/xmlsec/internal/signature" + "github.com/skarm/xmlsec/saml/artifact" "github.com/skarm/xmlsec/saml/binding" + "github.com/skarm/xmlsec/saml/ecp" "github.com/skarm/xmlsec/saml/internal/profile" "github.com/skarm/xmlsec/saml/internal/xmlsig" "github.com/skarm/xmlsec/saml/metadata" + "github.com/skarm/xmlsec/saml/proof" "github.com/skarm/xmlsec/saml/protocol" "github.com/skarm/xmlsec/saml/replay" "github.com/skarm/xmlsec/xmldsig" @@ -52,6 +57,12 @@ type Config struct { // AssertionLifetime defaults to five minutes and must be at least one second // and no greater than Limits.MaxAssertionLifetime. AssertionLifetime time.Duration + // Encryption selects targets and exact ciphers. A nil Recipient selects + // the first supported RSA key from trusted SP encryption metadata. An + // explicit Recipient must occur in that metadata. Nil disables encryption + // and rejects requests that require EncryptedID. A configured policy adds + // EncryptNameID when the request uses NameIDEncrypted. + Encryption *protocol.EncryptionOptions } // IdentityProvider issues solicited SAML responses for identities authenticated by the @@ -60,7 +71,11 @@ type IdentityProvider struct{ c Config } // NewIdentityProvider validates and snapshots IdentityProvider policy and required trusted adapters. func NewIdentityProvider(c Config) (*IdentityProvider, error) { - if !profile.URI(c.EntityID) || nilcheck.IsNil(c.Metadata) || nilcheck.IsNil(c.Signing.Key) || nilcheck.IsNil(c.Replay) || protocol.ValidatePolicy(c.Limits) != nil { + const ( + defaultAssertionLifetime = 5 * time.Minute + ) + + if !profile.URI(c.EntityID) || nilcheck.IsNil(c.Metadata) || nilcheck.IsNil(c.Signing.Key) || c.Signing.Secret != nil || nilcheck.IsNil(c.Replay) || protocol.ValidatePolicy(c.Limits) != nil { return nil, ErrPolicy } @@ -69,7 +84,7 @@ func NewIdentityProvider(c Config) (*IdentityProvider, error) { } if c.AssertionLifetime == 0 { - c.AssertionLifetime = 5 * time.Minute + c.AssertionLifetime = defaultAssertionLifetime } _, maximum, _, e := protocol.AcceptanceWindow(c.Limits) @@ -78,9 +93,15 @@ func NewIdentityProvider(c Config) (*IdentityProvider, error) { } c.Signing.CertificateDER = bytes.Clone(c.Signing.CertificateDER) + c.Signing.InclusivePrefixes = append([]string(nil), c.Signing.InclusivePrefixes...) c.Signing.Policy = xmlsig.ClonePolicy(c.Signing.Policy) c.VerificationPolicy = xmlsig.ClonePolicy(c.VerificationPolicy) + c.Encryption, e = protocol.CloneEncryption(c.Encryption) + if e != nil { + return nil, ErrPolicy + } + return &IdentityProvider{c: c}, nil } @@ -103,6 +124,10 @@ func (i *IdentityProvider) AcceptPOST(ctx context.Context, form []byte, sp, dest return nil, e } + if r.ResponseBinding() == metadata.PAOS { + return nil, ErrPolicy + } + relay, present := message.RelayState() return i.wrap(r, relay, present, at) @@ -121,6 +146,10 @@ func (i *IdentityProvider) AcceptRedirect(ctx context.Context, rawQuery, sp, des return nil, e } + if r.ResponseBinding() == metadata.PAOS { + return nil, ErrPolicy + } + relay, present := r.RelayState() return i.wrap(r, relay, present, at) @@ -130,7 +159,11 @@ func (i *IdentityProvider) AcceptRedirect(ctx context.Context, rawQuery, sp, des // AuthnRequest before releasing it. A Request from another IdP instance cannot // be substituted, even if it was validated under a weaker/different trust policy. func (i *IdentityProvider) Issue(ctx context.Context, request *Request, a Authentication, at time.Time) (*Issued, error) { - if !i.acceptsIssueRequest(ctx, request, at) { + if !i.acceptsIssueRequest(ctx, request, at) || request.channelErr != nil { + return nil, ErrPolicy + } + + if !request.associatedKey(a.HolderOfKeyCertificate) { return nil, ErrPolicy } @@ -149,10 +182,14 @@ func (i *IdentityProvider) Issue(ctx context.Context, request *Request, a Authen return nil, ErrPolicy } + if len(own.Endpoints(metadata.SingleLogoutService)) > 0 && a.SessionIndex == "" { + return nil, ErrPolicy + } + found := false for _, ep := range other.Endpoints(metadata.AssertionConsumerService) { - if ep.Binding == metadata.HTTPPost && ep.Location == r.ACS() { + if ep.AcceptsTransport(r.ResponseBinding(), request.proof != nil) && ep.Location == r.ACS() { found = true } } @@ -178,7 +215,7 @@ func (i *IdentityProvider) Issue(ctx context.Context, request *Request, a Authen a.NameIDFormat = protocol.NameIDUnspecified } - if r.NameIDFormat() != "" && r.NameIDFormat() != protocol.NameIDUnspecified && a.NameIDFormat != r.NameIDFormat() || len(own.NameIDFormats()) > 0 && !contains(own.NameIDFormats(), a.NameIDFormat) || len(other.NameIDFormats()) > 0 && !contains(other.NameIDFormats(), a.NameIDFormat) { + if !nameIDPolicyAllows(r.NameIDFormat(), a.NameIDFormat) || len(own.NameIDFormats()) > 0 && !contains(own.NameIDFormats(), a.NameIDFormat) || len(other.NameIDFormats()) > 0 && !contains(other.NameIDFormats(), a.NameIDFormat) { return nil, ErrPolicy } @@ -192,45 +229,101 @@ func (i *IdentityProvider) Issue(ctx context.Context, request *Request, a Authen return nil, e } - end := at.Add(i.c.AssertionLifetime) - if own.ExpiresAt().Before(end) { - end = own.ExpiresAt() - } - - if other.ExpiresAt().Before(end) { - end = other.ExpiresAt() - } + end := i.assertionExpiry(own, other, at) spec := protocol.Response{Limits: i.c.Limits, ID: responseID, Issuer: i.c.EntityID, Audience: r.Issuer(), Destination: r.ACS(), InResponseTo: r.ID(), IssueInstant: at, Assertion: protocol.AssertionData{ID: assertionID, NameID: a.NameID, NameIDFormat: a.NameIDFormat, NotBefore: at, NotOnOrAfter: end, AuthnInstant: a.AuthnInstant, AuthnContext: a.AuthnContext, SessionIndex: a.SessionIndex, SessionNotOnOrAfter: a.SessionNotOnOrAfter, Attributes: a.Attributes}} + spec.SOAPContext = r.ResponseBinding() == metadata.PAOS || r.ResponseBinding() == metadata.HTTPArtifact + spec.Assertion.NameQualifier = a.NameQualifier + spec.Assertion.SPNameQualifier = a.SPNameQualifier + spec.Assertion.SPProvidedID = a.SPProvidedID + spec.Assertion.ChannelBindings = protocol.CloneChannelBindings(request.channels) + spec.Assertion.HolderOfKeyCertificate = bytes.Clone(a.HolderOfKeyCertificate) + + spec.Encryption, e = i.encryptionFor(other, r.NameIDFormat() == protocol.NameIDEncrypted) + if e != nil { + return nil, e + } d, e := protocol.SignResponse(ctx, spec, i.c.Signing) if e != nil { return nil, e } + issued, e := i.finishIssue(ctx, request, d, end, at) + if e != nil { + return nil, e + } + + issued.hokCertificate = bytes.Clone(a.HolderOfKeyCertificate) + + return issued, nil +} + +func (i *IdentityProvider) finishIssue(ctx context.Context, request *Request, d *document.Document, end, at time.Time) (*Issued, error) { + const ( + scopeIDPRequestPrefix = "saml:idp:request:" + scopeSeparator = "\u0000" + ) + + r := request.verified + var relay *string if request.relayPresent { s := request.relay relay = &s } - _, e = binding.WritePOST(ctx, io.Discard, d, binding.Response, relay, i.c.POSTLimits) + _, e := binding.WritePOST(ctx, io.Discard, d, binding.Response, relay, i.c.POSTLimits) if e != nil { return nil, e } - sum := sha256.Sum256([]byte(i.c.EntityID + "\x00" + r.Issuer())) + sum := sha256.Sum256([]byte(i.c.EntityID + scopeSeparator + r.Issuer())) expires := request.expires + + _, _, skew, e := protocol.AcceptanceWindow(i.c.Limits) + if e != nil { + return nil, e + } + if end.Add(skew).After(expires) { expires = end.Add(skew) } - if e = i.c.Replay.Consume(ctx, []replay.Entry{{Scope: "saml:idp:request:" + hex.EncodeToString(sum[:]), ID: r.ID(), ExpiresAt: expires}}, at); e != nil { + if e = i.c.Replay.Consume(ctx, []replay.Entry{{Scope: scopeIDPRequestPrefix + hex.EncodeToString(sum[:]), ID: r.ID(), ExpiresAt: expires}}, at); e != nil { return nil, e } - return &Issued{doc: d, destination: r.ACS(), relay: request.relay, relayPresent: request.relayPresent, limits: i.c.POSTLimits}, nil + return &Issued{doc: d, destination: r.ACS(), relay: request.relay, relayPresent: request.relayPresent, limits: i.c.POSTLimits, responseBinding: r.ResponseBinding(), issuer: i.c.EntityID, recipient: r.Issuer(), channels: protocol.CloneChannelBindings(request.channels)}, nil +} + +// Reject returns an authenticated SAML authentication-error response and consumes +// the request exactly once. Use a Requester/Responder status and a relevant SAML +// subcode (for example AuthnFailed or InvalidNameIDPolicy). It supports the same +// POST, Artifact and ECP delivery as a successful Issue operation. +func (i *IdentityProvider) Reject(ctx context.Context, request *Request, status protocol.Status, at time.Time) (*Issued, error) { + if !i.acceptsIssueRequest(ctx, request, at) || status.Code != protocol.Requester && status.Code != protocol.Responder { + return nil, ErrPolicy + } + + r := request.verified + + own, _, err := i.roles(ctx, r.Issuer(), at) + if err != nil { + return nil, err + } + + if !keyAllowed(i.c.Signing.Key.Public(), own) { + return nil, ErrPolicy + } + + d, err := protocol.SignStatusResponse(ctx, protocol.StatusResponse{Issuer: i.c.EntityID, Destination: r.ACS(), IssueInstant: at, Kind: lexicon.SAMLElementResponse, InResponseTo: r.ID(), Status: status, SOAPContext: r.ResponseBinding() == metadata.PAOS || r.ResponseBinding() == metadata.HTTPArtifact}, i.c.Signing) + if err != nil { + return nil, err + } + + return i.finishIssue(ctx, request, d, at, at) } func (i *IdentityProvider) roles(ctx context.Context, sp string, at time.Time) (*metadata.Role, *metadata.Role, error) { @@ -255,6 +348,10 @@ func (i *IdentityProvider) roles(ctx context.Context, sp string, at time.Time) ( } func (i *IdentityProvider) requestPolicy(ctx context.Context, sp, destination, bindingURI string, at time.Time) (protocol.RequestOptions, *metadata.Role, error) { + return i.requestPolicyWithProof(ctx, sp, destination, bindingURI, at, false) +} + +func (i *IdentityProvider) requestPolicyWithProof(ctx context.Context, sp, destination, bindingURI string, at time.Time, holder bool) (protocol.RequestOptions, *metadata.Role, error) { if i == nil || ctx == nil || at.IsZero() || !profile.URI(sp) { return protocol.RequestOptions{}, nil, ErrPolicy } @@ -267,7 +364,7 @@ func (i *IdentityProvider) requestPolicy(ctx context.Context, sp, destination, b found := false for _, ep := range own.Endpoints(metadata.SingleSignOnService) { - if ep.Binding == bindingURI && ep.Location == destination { + if ep.AcceptsTransport(bindingURI, holder) && ep.Location == destination { found = true } } @@ -276,49 +373,164 @@ func (i *IdentityProvider) requestPolicy(ctx context.Context, sp, destination, b return protocol.RequestOptions{}, nil, ErrPolicy } - return protocol.RequestOptions{Issuer: sp, Destination: destination, ACS: other.Endpoints(metadata.AssertionConsumerService), At: at, Limits: i.c.Limits, Policy: i.c.VerificationPolicy}, other, nil -} + opt := protocol.RequestOptions{Issuer: sp, Destination: destination, ACS: other.Endpoints(metadata.AssertionConsumerService), At: at, Limits: i.c.Limits, Policy: i.c.VerificationPolicy} -func (i *IdentityProvider) wrap(r *protocol.VerifiedRequest, relay string, present bool, at time.Time) (*Request, error) { - age, _, skew, e := protocol.AcceptanceWindow(i.c.Limits) - if e != nil { - return nil, e + opt.TransportBinding = bindingURI + + opt.AllowHolderOfKeyACS = holder + if bindingURI == metadata.SOAP { + opt.ResponseBinding = metadata.PAOS } - expires := r.IssueInstant().Add(age + skew) - if !at.Before(expires) { + return opt, other, nil +} + +// AcceptSOAP authenticates an ECP AuthnRequest at a trusted SOAP SSO endpoint. +// The ECP must already have removed its PAOS and ECP routing headers. +// If channel comparison fails after request authentication, both Request and +// error are returned. Only Reject may use that Request to generate a signed +// SAML error; Issue rejects it. Unauthenticated failures return no Request. +func (i *IdentityProvider) AcceptSOAP(ctx context.Context, raw []byte, sp, destination string, at time.Time) (*Request, error) { + const ( + xmlNameChannelBindings = "ChannelBindings" + ) + + opt, role, err := i.requestPolicy(ctx, sp, destination, metadata.SOAP, at) + if err != nil { + return nil, err + } + + m, err := binding.DecodeSOAP(ctx, raw, binding.SOAPOptions{Understood: []document.Name{{Space: protocol.ChannelBindingNamespace, Local: xmlNameChannelBindings}}}) + if err != nil { + return nil, err + } + // Own-channel headers require the complete SOAP integrity proof accepted by + // AcceptECP, not just the SP's signature over the inner SAML request. + own, err := ecp.ReadIDPChannelHeaders(m.Headers()) + if err != nil || len(own) != 0 { return nil, ErrPolicy } - return &Request{owner: i, verified: r, relay: relay, relayPresent: present, expires: expires}, nil + d, err := m.Payload(ctx) + if err != nil { + return nil, err + } + + return i.acceptSOAPMessage(ctx, m, d, opt, role, at) } -func (i *IdentityProvider) acceptsIssueRequest(ctx context.Context, request *Request, at time.Time) bool { - return !(i == nil || ctx == nil || request == nil || request.owner != i || request.verified == nil || at.IsZero() || !at.Before(request.expires)) +func (i *IdentityProvider) acceptSOAPMessage(ctx context.Context, m *binding.SOAPMessage, d *document.Document, opt protocol.RequestOptions, role *metadata.Role, at time.Time) (*Request, error) { + r, err := protocol.VerifyAuthnRequest(ctx, d, role.SigningKeys(), opt) + if err != nil { + return nil, err + } + + request, err := i.wrap(r, "", false, at) + if err != nil { + return nil, err + } + + channels, err := ecp.ReadChannelHeaders(m.Headers(), false) + if err == nil { + request.channels, err = ecp.MatchChannelBindings(r.ChannelBindings(), channels) + } + + if err != nil { + request.channelErr = err + return request, err + } + + return request, nil } -func keyAllowed(key crypto.PublicKey, role *metadata.Role) bool { - b, e := x509.MarshalPKIXPublicKey(key) - if e != nil { - return false +// AcceptECP accepts HoK proof for this SOAP AuthnRequest, independently verifies +// the SP's SAML signature, and compares channel bindings. localChannels must be +// derived from the actual client/IdP TLS connection. Any supplied own-channel +// binding must match, even if the SP did not request channel bindings. +func (i *IdentityProvider) AcceptECP(ctx context.Context, p *proof.Verified, sp, destination string, at time.Time, localChannels []protocol.ChannelBinding) (*Request, error) { + if p == nil || p.Message() == nil { + return nil, ErrPolicy } - for _, candidate := range role.SigningKeys() { - v, e := x509.MarshalPKIXPublicKey(candidate) - if e == nil && bytes.Equal(b, v) { - return true + opt, role, err := i.requestPolicyWithProof(ctx, sp, destination, metadata.SOAP, at, true) + if err != nil { + return nil, err + } + + d, err := p.Payload(ctx) + if err != nil { + return nil, err + } + + r, err := i.acceptSOAPMessage(ctx, p.Message(), d, opt, role, at) + if err != nil { + return r, err + } + + r.proof = p + + own, err := ecp.ReadIDPChannelHeaders(p.Message().Headers()) + if err == nil && len(own) != 0 { + if !p.XML() { + err = ErrPolicy + } else { + for _, channel := range own { + if _, err = ecp.MatchChannelBindings(localChannels, []protocol.ChannelBinding{channel}); err != nil { + break + } + } } } - return false + if err != nil { + r.channelErr = err + return r, err + } + + return r, nil } -func contains(a []string, s string) bool { - return slices.Contains(a, s) +// AcceptArtifact validates a resolved AuthnRequest under the HTTP Artifact SSO +// endpoint's current metadata. The message itself must carry its XML signature; +// resolution alone never substitutes for its authentication or replay checks. +func (i *IdentityProvider) AcceptArtifact(ctx context.Context, message *document.Document, relay *string, sp, destination string, at time.Time) (*Request, error) { + opt, role, err := i.requestPolicy(ctx, sp, destination, metadata.HTTPArtifact, at) + if err != nil { + return nil, err + } + + r, err := protocol.VerifyAuthnRequest(ctx, message, role.SigningKeys(), opt) + if err != nil { + return nil, err + } + + if relay == nil { + return i.wrap(r, "", false, at) + } + + if len(*relay) > profile.HTTPRelayStateMaxBytes { + return nil, ErrPolicy + } + + return i.wrap(r, *relay, true, at) } -func authenticationAllowed(a Authentication, at time.Time, skew time.Duration, r *protocol.VerifiedRequest) bool { - return !(a.AuthnInstant.IsZero() || a.AuthnInstant.After(at.Add(skew)) || !profile.URI(a.AuthnContext) || r.ForceAuthn() && (!a.FreshAuthentication || a.AuthnInstant.Before(r.IssueInstant().Add(-skew))) || r.IsPassive() && a.InteractionUsed || !r.AllowCreateNameID() && a.CreatedNameID) +func (i *IdentityProvider) wrap(r *protocol.VerifiedRequest, relay string, present bool, at time.Time) (*Request, error) { + age, _, skew, e := protocol.AcceptanceWindow(i.c.Limits) + if e != nil { + return nil, e + } + + expires := r.IssueInstant().Add(age + skew) + if !at.Before(expires) { + return nil, ErrPolicy + } + + return &Request{owner: i, verified: r, relay: relay, relayPresent: present, expires: expires}, nil +} + +func (i *IdentityProvider) acceptsIssueRequest(ctx context.Context, request *Request, at time.Time) bool { + return !(i == nil || ctx == nil || request == nil || request.owner != i || request.verified == nil || at.IsZero() || !at.Before(request.expires)) } // Request retains an authenticated request bound to the IdentityProvider that accepted @@ -329,6 +541,27 @@ type Request struct { relay string relayPresent bool expires time.Time + channels []protocol.ChannelBinding + channelErr error + proof *proof.Verified +} + +// HolderOfKeyCertificate returns the proven certificate. Before Issue the +// application must independently establish its association with the principal. +func (r *Request) HolderOfKeyCertificate() []byte { + if r == nil { + return nil + } + + return r.proof.Certificate() +} + +func (r *Request) associatedKey(certificate []byte) bool { + if r.proof == nil { + return len(certificate) == 0 + } + + return len(certificate) != 0 && bytes.Equal(certificate, r.proof.Certificate()) } // ID returns the accepted AuthnRequest identifier. @@ -397,15 +630,22 @@ func (r *Request) ExpiresAt() time.Time { // Authentication supplies the identity and authentication conditions established by // the application for response issuance. type Authentication struct { + // HolderOfKeyCertificate is the certificate the application has independently + // associated with this principal. For HoK it must match the request's proof. + HolderOfKeyCertificate []byte // NameID is the subject identifier established by the application. NameID string // NameIDFormat identifies the subject identifier format to issue. NameIDFormat string + // NameQualifier, SPNameQualifier and SPProvidedID retain the application's + // current managed identifier, including the SP's alias. + NameQualifier, SPNameQualifier, SPProvidedID string // AuthnInstant is the time at which the application authenticated the subject. AuthnInstant time.Time // AuthnContext identifies the authentication context established by the application. AuthnContext string - // SessionIndex is an optional identifier for the authenticated session. + // SessionIndex identifies the authority session. It is required when local + // metadata advertises SingleLogoutService (Profiles 4.1.4.2 / Errata E38). SessionIndex string // SessionNotOnOrAfter is an optional session expiration time. Zero omits it. SessionNotOnOrAfter time.Time @@ -425,11 +665,14 @@ type Authentication struct { // Issued retains an immutable signed response and the policy needed to serialize its // POST binding. type Issued struct { - doc *document.Document - limits binding.POSTLimits - relay string - relayPresent bool - destination string + doc *document.Document + limits binding.POSTLimits + relay string + relayPresent bool + destination string + responseBinding, issuer, recipient string + channels []protocol.ChannelBinding + hokCertificate []byte } // Document returns the immutable document snapshot. @@ -441,11 +684,35 @@ func (r *Issued) Document() *document.Document { return r.doc } +// ECP wraps the signed response with the mandatory ECP response header. This +// response was signed in SOAP context when the request selected PAOS. +func (r *Issued) ECP(ctx context.Context) (*document.Document, error) { + if r == nil || r.responseBinding != metadata.PAOS { + return nil, ErrPolicy + } + + if len(r.hokCertificate) != 0 { + return ecp.ResponseHoK(ctx, r.doc, r.destination, r.hokCertificate, r.channels) + } + + return ecp.ResponseWithBindings(ctx, r.doc, r.destination, r.channels) +} + +// IssueArtifact creates the requested one-time response artifact. The issuer's +// identity must match the IdP; the artifact is bound to the requesting SP. +func (r *Issued) IssueArtifact(ctx context.Context, issuer artifact.Issuer, at time.Time, lifetime time.Duration) (binding.Artifact, error) { + if r == nil || r.responseBinding != metadata.HTTPArtifact || issuer.EntityID != r.issuer { + return binding.Artifact{}, ErrPolicy + } + + return issuer.Issue(ctx, r.doc, r.recipient, at, lifetime) +} + // WritePOST writes the signed response directly to dst without retaining a // complete encoded form. It leaves dst open. Errors may leave partial output // which the caller must discard; the authenticated request is already consumed. func (r *Issued) WritePOST(ctx context.Context, dst io.Writer) (int64, error) { - if r == nil { + if r == nil || r.responseBinding != "" && r.responseBinding != metadata.HTTPPost { return 0, ErrPolicy } @@ -455,7 +722,7 @@ func (r *Issued) WritePOST(ctx context.Context, dst io.Writer) (int64, error) { // WritePOSTForm writes a complete HTML form to the metadata-selected endpoint. // It leaves dst open and may leave partial output on failure. func (r *Issued) WritePOSTForm(ctx context.Context, dst io.Writer) (int64, error) { - if r == nil { + if r == nil || r.responseBinding != "" && r.responseBinding != metadata.HTTPPost { return 0, ErrPolicy } @@ -478,3 +745,84 @@ func (r *Issued) relayState() *string { return &r.relay } + +func keyAllowed(key crypto.PublicKey, role *metadata.Role) bool { + b, e := x509.MarshalPKIXPublicKey(key) + if e != nil { + return false + } + + for _, candidate := range role.SigningKeys() { + v, e := x509.MarshalPKIXPublicKey(candidate) + if e == nil && bytes.Equal(b, v) { + return true + } + } + + return false +} + +func contains(a []string, s string) bool { + return slices.Contains(a, s) +} + +func authenticationAllowed(a Authentication, at time.Time, skew time.Duration, r *protocol.VerifiedRequest) bool { + return !(a.AuthnInstant.IsZero() || a.AuthnInstant.After(at.Add(skew)) || !profile.URI(a.AuthnContext) || r.ForceAuthn() && (!a.FreshAuthentication || a.AuthnInstant.Before(r.IssueInstant().Add(-skew))) || r.IsPassive() && a.InteractionUsed || !r.AllowCreateNameID() && a.CreatedNameID) +} + +func (i *IdentityProvider) encryptionFor(role *metadata.Role, requireNameID bool) (*protocol.EncryptionOptions, error) { + if i.c.Encryption == nil { + if requireNameID { + return nil, ErrPolicy + } + + return nil, nil + } + + policy := *i.c.Encryption + if requireNameID { + policy.Targets |= protocol.EncryptNameID + } + + var selected *rsa.PublicKey + + for _, key := range role.EncryptionKeys() { + candidate, ok := key.(*rsa.PublicKey) + if !ok || signature.Key(candidate) != nil { + continue + } + + if policy.Recipient != nil && (candidate.E != policy.Recipient.E || candidate.N.Cmp(policy.Recipient.N) != 0) { + continue + } + + selected = candidate + + break + } + + if selected == nil { + return nil, ErrPolicy + } + + policy.Recipient = selected + + return &policy, nil +} + +func nameIDPolicyAllows(requested, actual string) bool { + return actual != protocol.NameIDEncrypted && (requested == "" || requested == protocol.NameIDUnspecified || requested == protocol.NameIDEncrypted || requested == actual) +} + +func (i *IdentityProvider) assertionExpiry(own, other *metadata.Role, at time.Time) time.Time { + end := at.Add(i.c.AssertionLifetime) + if own.ExpiresAt().Before(end) { + end = own.ExpiresAt() + } + + if other.ExpiresAt().Before(end) { + end = other.ExpiresAt() + } + + return end +} diff --git a/saml/idp/policy_test.go b/saml/idp/policy_test.go index e6b0c00..7c9fba1 100644 --- a/saml/idp/policy_test.go +++ b/saml/idp/policy_test.go @@ -137,36 +137,6 @@ func TestIdentityProviderPolicyBoundariesAndSnapshots(t *testing.T) { } } -func trustedRoleEntity(t *testing.T, id string, kind metadata.RoleType) *metadata.Entity { - t.Helper() - role := &metadata.RoleDescriptor{Keys: []metadata.KeyDescriptor{{CertificateDER: testcert.New(t).Leaf.Raw}}} - spec := metadata.EntityDescriptor{ID: "_metadata", EntityID: id, ValidUntil: testcert.Now().Add(time.Hour)} - if kind == metadata.IdentityProvider { - role.Endpoints = map[string][]metadata.Endpoint{metadata.SingleSignOnService: {{Binding: metadata.HTTPPost, Location: singleSignOnURL}, {Binding: metadata.HTTPRedirect, Location: singleSignOnURL}}} - spec.IDP = role - } else { - role.Endpoints = map[string][]metadata.Endpoint{metadata.AssertionConsumerService: {{Binding: metadata.HTTPPost, Location: assertionConsumerURL, Index: 7}}} - spec.SP = role - } - doc, err := metadata.Build(t.Context(), spec, testcert.Now(), metadata.Options{}) - if err != nil { - t.Fatal(err) - } - raw, err := doc.XML(t.Context()) - if err != nil { - t.Fatal(err) - } - snapshot, err := metadata.ParseTrusted(t.Context(), raw, testcert.Now(), metadata.Options{}) - if err != nil { - t.Fatal(err) - } - entity, err := snapshot.Find(id, testcert.Now()) - if err != nil { - t.Fatal(err) - } - return entity -} - func TestRequestPolicyTrustLookupAndBinding(t *testing.T) { t.Parallel() @@ -248,20 +218,6 @@ func TestRequestPolicyTrustLookupAndBinding(t *testing.T) { } } -func signedAndVerifiedRequest(t *testing.T, force, passive, create bool) *protocol.VerifiedRequest { - t.Helper() - key := testcert.Key(t) - doc, err := protocol.SignAuthnRequest(t.Context(), protocol.AuthnRequest{ID: "_policy", Issuer: spEntityID, Destination: singleSignOnURL, ACS: assertionConsumerURL, IssueInstant: testcert.Now(), ForceAuthn: force, IsPassive: passive, AllowCreateNameID: create, RequestedContexts: []string{authenticationContext}}, protocol.SigningKey{Key: key}) - if err != nil { - t.Fatal(err) - } - request, err := protocol.VerifyAuthnRequest(t.Context(), doc, []crypto.PublicKey{key.Public()}, protocol.RequestOptions{Issuer: spEntityID, Destination: singleSignOnURL, ACS: []metadata.Endpoint{{Binding: metadata.HTTPPost, Location: assertionConsumerURL}}, At: testcert.Now()}) - if err != nil { - t.Fatal(err) - } - return request -} - func TestAuthenticationPolicyIndependentConditions(t *testing.T) { t.Parallel() @@ -455,3 +411,47 @@ func TestIssuedBindingWritersPropagatePartialFailures(t *testing.T) { } } } + +func trustedRoleEntity(t *testing.T, id string, kind metadata.RoleType) *metadata.Entity { + t.Helper() + role := &metadata.RoleDescriptor{Keys: []metadata.KeyDescriptor{{CertificateDER: testcert.New(t).Leaf.Raw}}} + spec := metadata.EntityDescriptor{ID: "_metadata", EntityID: id, ValidUntil: testcert.Now().Add(time.Hour)} + if kind == metadata.IdentityProvider { + role.Endpoints = map[string][]metadata.Endpoint{metadata.SingleSignOnService: {{Binding: metadata.HTTPPost, Location: singleSignOnURL}, {Binding: metadata.HTTPRedirect, Location: singleSignOnURL}}} + spec.IDP = role + } else { + role.Endpoints = map[string][]metadata.Endpoint{metadata.AssertionConsumerService: {{Binding: metadata.HTTPPost, Location: assertionConsumerURL, Index: 7}}} + spec.SP = role + } + doc, err := metadata.Build(t.Context(), spec, testcert.Now(), metadata.Options{}) + if err != nil { + t.Fatal(err) + } + raw, err := doc.XML(t.Context()) + if err != nil { + t.Fatal(err) + } + snapshot, err := metadata.ParseTrusted(t.Context(), raw, testcert.Now(), metadata.Options{}) + if err != nil { + t.Fatal(err) + } + entity, err := snapshot.Find(id, testcert.Now()) + if err != nil { + t.Fatal(err) + } + return entity +} + +func signedAndVerifiedRequest(t *testing.T, force, passive, create bool) *protocol.VerifiedRequest { + t.Helper() + key := testcert.Key(t) + doc, err := protocol.SignAuthnRequest(t.Context(), protocol.AuthnRequest{ID: "_policy", Issuer: spEntityID, Destination: singleSignOnURL, ACS: assertionConsumerURL, IssueInstant: testcert.Now(), ForceAuthn: force, IsPassive: passive, AllowCreateNameID: create, RequestedContexts: []string{authenticationContext}}, protocol.SigningKey{Key: key}) + if err != nil { + t.Fatal(err) + } + request, err := protocol.VerifyAuthnRequest(t.Context(), doc, []crypto.PublicKey{key.Public()}, protocol.RequestOptions{Issuer: spEntityID, Destination: singleSignOnURL, ACS: []metadata.Endpoint{{Binding: metadata.HTTPPost, Location: assertionConsumerURL}}, At: testcert.Now()}) + if err != nil { + t.Fatal(err) + } + return request +} diff --git a/saml/internal/expiry/index.go b/saml/internal/expiry/index.go new file mode 100644 index 0000000..951e782 --- /dev/null +++ b/saml/internal/expiry/index.go @@ -0,0 +1,145 @@ +package expiry + +import "time" + +// Index is an expiration heap with one entry per key. Updating or deleting a +// live record takes O(log n); early deletion never leaves a retained tombstone. +// Its zero value is ready to use. Like Queue, it is not concurrent-safe. +type Index[K comparable] struct { + items []item[K] + positions map[K]int + peak int + latest time.Time // conservative upper bound, including overwritten deadlines +} + +// Set inserts a key or replaces its expiration. +func (q *Index[K]) Set(key K, at time.Time) { + if len(q.items) == 0 || at.After(q.latest) { + q.latest = at + } + + if i, ok := q.positions[key]; ok { + q.items[i].at = at + q.fix(i) + + return + } + + if q.positions == nil { + q.positions = make(map[K]int) + } + + i := len(q.items) + q.items = append(q.items, item[K]{key: key, at: at}) + q.positions[key] = i + q.peak = max(q.peak, len(q.items)) + q.up(i) +} + +// Delete removes a key and clears the heap's references to it. +func (q *Index[K]) Delete(key K) { + i, ok := q.positions[key] + if !ok { + return + } + + delete(q.positions, key) + + last := len(q.items) - 1 + if i != last { + q.items[i] = q.items[last] + q.positions[q.items[i].key] = i + } + + q.items[last] = item[K]{} + + q.items = q.items[:last] + if last == 0 { + q.latest = time.Time{} + } + + if i < last { + q.fix(i) + } +} + +// AllExpired reports when every indexed record is known to be expired. Stores +// can then clear their maps in bulk instead of removing an entire heap one key +// at a time. A replaced or deleted deadline can make this conservatively false. +func (q *Index[K]) AllExpired(at time.Time) bool { + return len(q.items) != 0 && !at.Before(q.latest) +} + +// Reset releases all indexed keys and their storage. +func (q *Index[K]) Reset() { *q = Index[K]{} } + +// PopExpired removes the earliest key only when its expiration is at or before at. +func (q *Index[K]) PopExpired(at time.Time) (K, bool) { + if len(q.items) == 0 || at.Before(q.items[0].at) { + var zero K + return zero, false + } + + key := q.items[0].key + q.Delete(key) + + return key, true +} + +// Trim releases excess heap and index capacity after a usage spike. +func (q *Index[K]) Trim() { + queue := Queue[K]{items: q.items} + queue.Trim() + + q.items = queue.items + if q.peak > retainedCapacityThreshold && len(q.items) <= q.peak/retainedCapacityDivisor { + q.positions = make(map[K]int, len(q.items)) + for i, entry := range q.items { + q.positions[entry.key] = i + } + + q.peak = len(q.items) + } +} + +func (q *Index[K]) swap(i, j int) { + q.items[i], q.items[j] = q.items[j], q.items[i] + q.positions[q.items[i].key], q.positions[q.items[j].key] = i, j +} + +func (q *Index[K]) up(i int) { + for i > 0 { + parent := (i - 1) / 2 + if !q.items[i].at.Before(q.items[parent].at) { + break + } + + q.swap(i, parent) + i = parent + } +} + +func (q *Index[K]) fix(i int) { + if i > 0 && q.items[i].at.Before(q.items[(i-1)/2].at) { + q.up(i) + return + } + + for { + child := 2*i + 1 + if child >= len(q.items) { + return + } + + if child+1 < len(q.items) && q.items[child+1].at.Before(q.items[child].at) { + child++ + } + + if !q.items[child].at.Before(q.items[i].at) { + return + } + + q.swap(i, child) + i = child + } +} diff --git a/saml/internal/expiry/index_test.go b/saml/internal/expiry/index_test.go new file mode 100644 index 0000000..d2c5653 --- /dev/null +++ b/saml/internal/expiry/index_test.go @@ -0,0 +1,219 @@ +package expiry //nolint:testpackage // Checks heap ordering, index consistency and release of deleted keys. + +import ( + "math/rand/v2" + "testing" + "time" +) + +// Compare arbitrary updates, early deletions and expiry against a plain map. +// Equal deadlines need no stable ordering, but every returned key must be due. +func TestIndexAgainstMap(t *testing.T) { + var queue Index[int] + want := make(map[int]time.Time) + random := rand.New(rand.NewPCG(11, 37)) + base := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC) + for range 20000 { + key := random.IntN(1024) + at := base.Add(time.Duration(random.IntN(100)) * time.Second) + switch random.IntN(4) { + case 0, 1: + queue.Set(key, at) + want[key] = at + case 2: + queue.Delete(key) + delete(want, key) + case 3: + for { + key, ok := queue.PopExpired(at) + if !ok { + break + } + deadline, found := want[key] + if !found || deadline.After(at) { + t.Fatalf("unexpected expired key %d at %s", key, at) + } + delete(want, key) + } + for key, deadline := range want { + if !deadline.After(at) { + t.Fatalf("due key %d was not returned", key) + } + } + } + queue.Trim() + if queue.AllExpired(at) { + for _, deadline := range want { + if deadline.After(at) { + t.Fatal("bulk expiration would discard a live key") + } + } + } + if len(queue.items) != len(want) || len(queue.positions) != len(want) { + t.Fatal("index retained a deleted key or lost a live key") + } + for i, entry := range queue.items { + if queue.positions[entry.key] != i || !want[entry.key].Equal(entry.at) { + t.Fatal("index lost key or deadline after a heap move") + } + if i > 0 && entry.at.Before(queue.items[(i-1)/2].at) { + t.Fatal("expiration heap is out of order") + } + } + } +} + +func TestIndexReleasesDeletedKeys(t *testing.T) { + var queue Index[*int] + for i := range 4096 { + queue.Set(&i, time.Unix(int64(i), 0)) + } + storage := queue.items[:cap(queue.items)] + for range 4096 { + if _, ok := queue.PopExpired(time.Unix(4096, 0)); !ok { + t.Fatal("missing expired key") + } + } + for _, entry := range storage { + if entry.key != nil || !entry.at.IsZero() { + t.Fatal("removed entry still retains a reference") + } + } + queue.Trim() + if cap(queue.items) != 0 || len(queue.positions) != 0 || queue.peak != 0 { + t.Fatal("index did not shrink after draining") + } + key := 1 + queue.Set(&key, time.Unix(1, 0)) + if got, ok := queue.PopExpired(time.Unix(1, 0)); !ok || got != &key { + t.Fatal("index cannot be reused after trimming") + } +} + +func TestIndexBulkExpiry(t *testing.T) { + var queue Index[int] + first, second, later := time.Unix(1, 0), time.Unix(2, 0), time.Unix(3, 0) + queue.Set(1, first) + queue.Set(2, second) + if queue.AllExpired(first) || !queue.AllExpired(second) { + t.Fatal("bulk expiry ignored the last live deadline") + } + queue.Set(1, later) + if queue.AllExpired(second) || !queue.AllExpired(later) { + t.Fatal("updating a deadline allowed premature bulk expiry") + } + queue.Delete(1) + if !queue.AllExpired(later) { + t.Fatal("bulk expiry lost remaining entries after deletion") + } + queue.Reset() + if queue.AllExpired(later) || len(queue.items) != 0 || len(queue.positions) != 0 { + t.Fatal("reset retained indexed entries") + } + queue.Set(3, first) + if !queue.AllExpired(first) { + t.Fatal("index was not reusable after reset") + } +} + +type deadlineEntry struct { + key string + at time.Time +} + +func TestIndexZeroDeadlineUpperBound(t *testing.T) { + t.Parallel() + + zero := time.Time{} + earlier, later := zero.Add(-time.Hour), zero.Add(time.Hour) + for _, test := range []struct { + name string + initial []deadlineEntry + updates []deadlineEntry + remove string + }{ + {name: "zero then earlier", initial: []deadlineEntry{{"zero", zero}, {"earlier", earlier}}}, + {name: "earlier zero then earlier", initial: []deadlineEntry{{"earlier", earlier}, {"zero", zero}, {"oldest", earlier.Add(-time.Hour)}}}, + {name: "zero only", initial: []deadlineEntry{{"zero", zero}}}, + {name: "zero then later", initial: []deadlineEntry{{"zero", zero}, {"later", later}}}, + {name: "latest replaced earlier", initial: []deadlineEntry{{"a", later}, {"zero", zero}}, updates: []deadlineEntry{{"a", earlier}}}, + {name: "latest extended", initial: []deadlineEntry{{"a", earlier}, {"zero", zero}}, updates: []deadlineEntry{{"a", later}}}, + {name: "latest removed", initial: []deadlineEntry{{"a", later}, {"zero", zero}}, remove: "a"}, + {name: "empty"}, + } { + t.Run(test.name, func(t *testing.T) { + t.Parallel() + + var queue Index[string] + want := make(map[string]time.Time) + for _, entry := range append(test.initial, test.updates...) { + queue.Set(entry.key, entry.at) + want[entry.key] = entry.at + } + if test.remove != "" { + queue.Delete(test.remove) + delete(want, test.remove) + } + for key, deadline := range want { + if deadline.After(queue.latest) { + t.Errorf("latest %v is below live deadline %q=%v", queue.latest, key, deadline) + } + } + for _, at := range []time.Time{earlier.Add(-2 * time.Hour), earlier, zero, later} { + checkIndexDeadlineBoundary(t, &queue, want, at) + } + if len(want) != 0 || queue.AllExpired(later) || !queue.latest.IsZero() { + t.Fatal("drained index retained entries or latest deadline") + } + }) + } +} + +func checkIndexDeadlineBoundary(t *testing.T, queue *Index[string], want map[string]time.Time, at time.Time) { + t.Helper() + + if queue.AllExpired(at) { + for key, deadline := range want { + if deadline.After(at) { + t.Errorf("AllExpired(%v) would discard live %q=%v", at, key, deadline) + } + } + } + for { + key, ok := queue.PopExpired(at) + if !ok { + break + } + deadline, exists := want[key] + if !exists || deadline.After(at) { + t.Fatalf("unexpected expired key %q at %v", key, at) + } + delete(want, key) + } + for key, deadline := range want { + if !deadline.After(at) { + t.Fatalf("due key %q was not returned at %v", key, at) + } + } +} + +func TestIndexZeroDeadlineResetAndReuse(t *testing.T) { + t.Parallel() + + var queue Index[string] + zero := time.Time{} + queue.Set("zero", zero) + queue.Delete("zero") + if queue.AllExpired(zero) || !queue.latest.IsZero() { + t.Fatal("deleting the final record retained a deadline") + } + queue.Set("later", zero.Add(time.Hour)) + queue.Reset() + if queue.AllExpired(zero) || len(queue.items) != 0 || !queue.latest.IsZero() { + t.Fatal("reset retained indexed state") + } + queue.Set("earlier", zero.Add(-time.Hour)) + if !queue.AllExpired(zero.Add(-time.Hour)) { + t.Fatal("reset prevented reuse with an earlier deadline") + } +} diff --git a/saml/internal/expiry/queue.go b/saml/internal/expiry/queue.go index 839f77e..20bc942 100644 --- a/saml/internal/expiry/queue.go +++ b/saml/internal/expiry/queue.go @@ -4,6 +4,16 @@ import ( "time" ) +// Only workspaces above this many entry slots are compacted after a usage spike; this is not a limit on live records. +const ( + retainedCapacityThreshold = 1024 +) + +// Shrink at quarter occupancy while preserving every live entry and the Index conservative latest-deadline bound. +const ( + retainedCapacityDivisor = 4 +) + // Queue is a min-heap ordered by expiration. Its zero value is ready to use. // Equal expiration times have no guaranteed order. Queue is not concurrent-safe. type Queue[K any] struct{ items []item[K] } @@ -17,7 +27,11 @@ type item[K any] struct { // storage; larger queues shrink at or below one-quarter occupancy, leaving room // for reuse. The heap order and all live entries are preserved. func (q *Queue[K]) Trim() { - if cap(q.items) <= 1024 || len(q.items) > cap(q.items)/4 { + const ( + retainedCapacityGrowthFactor = 2 + ) + + if cap(q.items) <= retainedCapacityThreshold || len(q.items) > cap(q.items)/retainedCapacityDivisor { return } @@ -26,7 +40,7 @@ func (q *Queue[K]) Trim() { return } - items := make([]item[K], len(q.items), max(1024, 2*len(q.items))) + items := make([]item[K], len(q.items), max(retainedCapacityThreshold, retainedCapacityGrowthFactor*len(q.items))) copy(items, q.items) q.items = items } diff --git a/saml/internal/profile/values.go b/saml/internal/profile/values.go index dcab54f..416b78e 100644 --- a/saml/internal/profile/values.go +++ b/saml/internal/profile/values.go @@ -9,21 +9,43 @@ import ( "strings" "time" + "github.com/skarm/xmlsec/internal/lexicon" + "github.com/skarm/xmlsec/internal/xmlchar" "github.com/skarm/xmlsec/internal/xmltime" ) +// Bound the supported positive day/time duration subset to 365 days; calendar years/months are rejected and fractions retain nanosecond precision. +const ( + maximumDuration = 365 * 24 * time.Hour +) + +// HTTPRelayStateMaxBytes is the byte ceiling of opaque RelayState in HTTP bindings. ECP SOAP relay text has a separate, larger profile limit. +const ( + HTTPRelayStateMaxBytes = 80 +) + +// SAML unsigned index and day/time duration components use decimal lexical digits; hexadecimal prefixes and numeric separators are not enabled. +const ( + decimalRadix = 10 +) + // ErrSchema reports unsupported or invalid SAML structure or values. var ErrSchema = errors.New("unsupported or invalid SAML structure") // NewID generates an unpredictable SAML ID using crypto/rand. func NewID() (string, error) { - var b [20]byte + const ( + identifierPrefix = "_" + identifierRandomBytes = 20 + ) + + var b [identifierRandomBytes]byte if _, e := rand.Read(b[:]); e != nil { return "", e } - return "_" + hex.EncodeToString(b[:]), nil + return identifierPrefix + hex.EncodeToString(b[:]), nil } // ParseTime parses SAML UTC dateTime values with four-digit positive years and @@ -41,11 +63,21 @@ func ParseTime(s string) (time.Time, error) { func Time(t time.Time) string { return t.UTC().Format(time.RFC3339Nano) } // ID reports whether s is an allowed SAML ID. -func ID(s string) bool { return len(s) > 0 && len(s) <= 256 && xmlchar.IsValidNCName(s) } +func ID(s string) bool { + const ( + maximumIDBytes = 256 + ) + + return len(s) > 0 && len(s) <= maximumIDBytes && xmlchar.IsValidNCName(s) +} // URI reports whether s is a bounded absolute URI without XML whitespace. func URI(s string) bool { - if len(s) == 0 || len(s) > 2048 || strings.ContainsAny(s, " \t\r\n") { + const ( + maximumURIBytes = 2048 + ) + + if len(s) == 0 || len(s) > maximumURIBytes || strings.ContainsAny(s, lexicon.XMLWhitespace) { return false } @@ -56,16 +88,26 @@ func URI(s string) bool { // Endpoint checks a bounded absolute HTTPS endpoint, permitting HTTP only when requested. func Endpoint(s string, allowHTTP bool) bool { + const ( + maximumEndpointBytes = 4096 + ) + u, e := url.Parse(s) - return e == nil && len(s) <= 4096 && u.Host != "" && u.User == nil && u.Fragment == "" && u.Opaque == "" && (u.Scheme == "https" || (allowHTTP && u.Scheme == "http")) + + return e == nil && len(s) <= maximumEndpointBytes && u.Host != "" && u.User == nil && u.Fragment == "" && u.Opaque == "" && (u.Scheme == lexicon.SchemeHTTPS || (allowHTTP && u.Scheme == lexicon.SchemeHTTP)) } // Bool parses an XML Schema boolean. func Bool(s string) (bool, error) { - switch strings.Trim(s, " \t\r\n") { - case "false", "0": + const ( + xmlBooleanFalseDigit = "0" + xmlBooleanTrueDigit = "1" + ) + + switch strings.Trim(s, lexicon.XMLWhitespace) { + case lexicon.ValueFalse, xmlBooleanFalseDigit: return false, nil - case "true", "1": + case lexicon.ValueTrue, xmlBooleanTrueDigit: return true, nil } @@ -75,7 +117,11 @@ func Bool(s string) (bool, error) { // Index parses the XSD 1.0 xs:unsignedShort endpoint index used by SAML. // Its lexical space contains decimal digits with XML whitespace collapsed. func Index(s string) (uint16, error) { - n, err := strconv.ParseUint(strings.Trim(s, " \t\r\n"), 10, 16) + const ( + indexBitSize = 16 + ) + + n, err := strconv.ParseUint(strings.Trim(s, lexicon.XMLWhitespace), decimalRadix, indexBitSize) if err != nil { return 0, ErrSchema } @@ -85,8 +131,14 @@ func Index(s string) (uint16, error) { // Duration parses positive day/time XML Schema durations up to 365 days, with // at most nine fractional second digits. Calendar years/months are unsupported. -func Duration(s string) (time.Duration, error) { // day/time subset; calendar years/months fail explicitly. - if len(s) < 3 || len(s) > 64 || s[0] != 'P' { +func Duration(s string) (time.Duration, error) { + const ( + maximumDurationLexicalBytes = 64 + minimumDurationLexicalBytes = 3 + ) + + // day/time subset; calendar years/months fail explicitly. + if len(s) < minimumDurationLexicalBytes || len(s) > maximumDurationLexicalBytes || s[0] != 'P' { return 0, ErrSchema } @@ -134,7 +186,7 @@ func Duration(s string) (time.Duration, error) { // day/time subset; calendar ye last = order amount, err := durationAmount(s[:i], scale) - if err != nil || amount > 365*24*time.Hour-total { + if err != nil || amount > maximumDuration-total { return 0, ErrSchema } @@ -151,20 +203,26 @@ func Duration(s string) (time.Duration, error) { // day/time subset; calendar ye } func durationAmount(s string, unit time.Duration) (time.Duration, error) { + const ( + durationFractionBitSize = 32 + durationFractionDigits = 9 + durationWholeBitSize = 64 + ) + whole, fraction, hasFraction := strings.Cut(s, ".") - if whole == "" || hasFraction && (unit != time.Second || len(fraction) == 0 || len(fraction) > 9) { + if whole == "" || hasFraction && (unit != time.Second || len(fraction) == 0 || len(fraction) > durationFractionDigits) { return 0, ErrSchema } - n, err := strconv.ParseInt(whole, 10, 64) - if err != nil || n < 0 || n > int64(365*24*time.Hour/unit) { + n, err := strconv.ParseInt(whole, decimalRadix, durationWholeBitSize) + if err != nil || n < 0 || n > int64(maximumDuration/unit) { return 0, ErrSchema } amount := time.Duration(n) * unit if hasFraction { - nanos, err := strconv.ParseUint(fraction+strings.Repeat("0", 9-len(fraction)), 10, 32) + nanos, err := strconv.ParseUint(fraction+strings.Repeat("0", durationFractionDigits-len(fraction)), decimalRadix, durationFractionBitSize) if err != nil { return 0, ErrSchema } @@ -176,6 +234,14 @@ func durationAmount(s string, unit time.Duration) (time.Duration, error) { } func durationUnit(kind byte, inTime bool) (int, time.Duration, bool) { + const ( + durationDay = 24 * time.Hour + durationDayOrder = 1 + durationHourOrder = 2 + durationMinuteOrder = 3 + durationSecondOrder = 4 + ) + order := 0 var scale time.Duration @@ -186,28 +252,28 @@ func durationUnit(kind byte, inTime bool) (int, time.Duration, bool) { return 0, 0, false } - order = 1 - scale = 24 * time.Hour + order = durationDayOrder + scale = durationDay case 'H': if !inTime { return 0, 0, false } - order = 2 + order = durationHourOrder scale = time.Hour case 'M': if !inTime { return 0, 0, false } - order = 3 + order = durationMinuteOrder scale = time.Minute case 'S': if !inTime { return 0, 0, false } - order = 4 + order = durationSecondOrder scale = time.Second default: return 0, 0, false diff --git a/saml/internal/profile/values_test.go b/saml/internal/profile/values_test.go index 8e869d2..ef093aa 100644 --- a/saml/internal/profile/values_test.go +++ b/saml/internal/profile/values_test.go @@ -118,7 +118,7 @@ func TestSAMLTimeLexicalForm(t *testing.T) { } { t.Run(s, func(t *testing.T) { t.Parallel() - if got, err := profile.ParseTime(s); !got.IsZero() || !errors.Is(err, profile.ErrSchema) { + if got, err := profile.ParseTime(s); got != (time.Time{}) || !errors.Is(err, profile.ErrSchema) { t.Fatalf("invalid/unsupported dateTime accepted: %v, %v", got, err) } }) @@ -137,7 +137,7 @@ func TestSAMLMidnight(t *testing.T) { } for _, s := range []string{"2026-12-31T24:00:00.001Z", "2026-12-31T24:01:00Z", "9999-12-31T24:00:00Z"} { - if got, err := profile.ParseTime(s); !got.IsZero() || !errors.Is(err, profile.ErrSchema) { + if got, err := profile.ParseTime(s); got != (time.Time{}) || !errors.Is(err, profile.ErrSchema) { t.Fatalf("invalid/unsupported midnight accepted: %v, %v", got, err) } } diff --git a/saml/internal/xmlsig/sign.go b/saml/internal/xmlsig/sign.go index a9ef6aa..4ef505d 100644 --- a/saml/internal/xmlsig/sign.go +++ b/saml/internal/xmlsig/sign.go @@ -1,6 +1,7 @@ package xmlsig import ( + "bytes" "context" "crypto" "crypto/x509" @@ -19,16 +20,59 @@ import ( "github.com/skarm/xmlsec/xmldsig" ) +// Bound explicit public-key candidates before cloning or signature verification; received KeyInfo never adds candidates or trust. +const ( + maximumTrustedKeys = 16 +) + +// MaxMessageSignatures bounds all XML signatures visible in one SAML message +// snapshot, including an enclosing response and contained assertion signatures. +// Query responses reserve one slot for the response; any nested signatures also +// consume this same budget. +const ( + MaxMessageSignatures = 64 +) + // ErrSignature reports SAML signature rejected. var ErrSignature = errors.New("SAML signature rejected") // SigningKey supplies the signer, optional matching certificate and explicit signature // policy. type SigningKey struct { - Key crypto.Signer - CertificateDER []byte - Method string - Policy xmldsig.Policy + Key crypto.Signer + CertificateDER []byte + Method string + DigestMethod string + Canonicalization string + InclusivePrefixes []string + PreserveKeys func(context.Context, document.Element) ([]crypto.PublicKey, error) + Secret *SharedSecret + Policy xmldsig.Policy +} + +// SharedSecret is an explicitly selected SAML MAC credential. It never comes +// from XML, X509Data or a public-key byte encoding. Holders can all forge MACs. +type SharedSecret struct{ value []byte } + +// NewSharedSecret snapshots a 32-byte to 1-MiB shared secret. Signature policy +// can impose a smaller upper bound. No secret is serialized into XML. +func NewSharedSecret(value []byte) (SharedSecret, error) { + const ( + maximumSecretBytes = 1 << 20 + minimumSecretBytes = 32 + ) + + if len(value) < minimumSecretBytes || len(value) > maximumSecretBytes { + return SharedSecret{}, ErrSignature + } + + return SharedSecret{value: bytes.Clone(value)}, nil +} + +// VerifiedSignature describes authenticated SAML target coverage for either +// explicitly selected public-key or shared-secret credentials. +type VerifiedSignature interface { + CoversElement(document.Element) bool } // ClonePolicy returns an owned copy of the SAML signature algorithm policy. @@ -41,12 +85,23 @@ func ClonePolicy(p xmldsig.Policy) xmldsig.Policy { // CloneKeys returns owned parsed copies of supported explicitly supplied public keys. func CloneKeys(keys []crypto.PublicKey) ([]crypto.PublicKey, error) { - if len(keys) == 0 || len(keys) > 16 { + if len(keys) == 0 || len(keys) > maximumTrustedKeys { return nil, ErrSignature } out := make([]crypto.PublicKey, 0, len(keys)) for _, k := range keys { + if secret, ok := k.(SharedSecret); ok { + snapshot, err := NewSharedSecret(secret.value) + if err != nil { + return nil, err + } + + out = append(out, snapshot) + + continue + } + if e := signature.Key(k); e != nil { return nil, ErrSignature } @@ -69,11 +124,26 @@ func CloneKeys(keys []crypto.PublicKey) ([]crypto.PublicKey, error) { // Verify verifies the closed SAML signature shape and exact target coverage using an // explicitly supplied trusted key. -func Verify(ctx context.Context, d *document.Document, target document.Element, keys []crypto.PublicKey, p xmldsig.Policy) (*xmldsig.SignatureResult, error) { - if ctx == nil || d == nil || !target.Valid() || !profile.ID(xmlutil.Attr(target, "ID")) || len(keys) == 0 || len(keys) > 16 { +func Verify(ctx context.Context, d *document.Document, target document.Element, keys []crypto.PublicKey, p xmldsig.Policy) (VerifiedSignature, error) { + const ( + referenceTransformCount = 2 + signedInfoChildCount = 3 + ) + + if ctx == nil || d == nil || !target.Valid() || !profile.ID(xmlutil.Attr(target, lexicon.SAMLAttrID)) || len(keys) == 0 || len(keys) > maximumTrustedKeys { return nil, ErrSignature } + scope := d + + owner, _ := tree.Unwrap(target) + if owner != d { + return nil, ErrSignature + } + + target = tree.ContextElement(target) + d, _ = tree.Unwrap(target) + sig, e := xmlutil.Direct(target, xmlutil.Name(xmlutil.DS, lexicon.DSigElementSignature)) if e != nil { return nil, e @@ -85,16 +155,16 @@ func Verify(ctx context.Context, d *document.Document, target document.Element, } cs, e := xmlutil.Children(si) - if e != nil || len(cs) != 3 || cs[0].Name() != xmlutil.Name(xmlutil.DS, lexicon.DSigElementCanonicalizationMethod) || cs[1].Name() != xmlutil.Name(xmlutil.DS, lexicon.DSigElementSignatureMethod) || cs[2].Name() != xmlutil.Name(xmlutil.DS, lexicon.DSigElementReference) { + if e != nil || len(cs) != signedInfoChildCount || cs[0].Name() != xmlutil.Name(xmlutil.DS, lexicon.DSigElementCanonicalizationMethod) || cs[1].Name() != xmlutil.Name(xmlutil.DS, lexicon.DSigElementSignatureMethod) || cs[2].Name() != xmlutil.Name(xmlutil.DS, lexicon.DSigElementReference) { return nil, ErrSignature } - if xmlutil.Attr(cs[0], "Algorithm") != c14n.Exclusive || len(cs[0].Children()) != 0 || xmlutil.Attrs(cs[0], []string{"Algorithm"}, nil) != nil { + if !simpleCanonicalization(cs[0]) { return nil, ErrSignature } ref := cs[2] - if xmlutil.Attr(ref, "URI") != "#"+xmlutil.Attr(target, "ID") { + if xmlutil.Attr(ref, lexicon.SecurityAttrURI) != "#"+xmlutil.Attr(target, lexicon.SAMLAttrID) { return nil, ErrSignature } @@ -104,13 +174,28 @@ func Verify(ctx context.Context, d *document.Document, target document.Element, } ts := trs.Children() - if len(ts) != 2 || ts[0].Name() != xmlutil.Name(xmlutil.DS, lexicon.DSigElementTransform) || ts[1].Name() != xmlutil.Name(xmlutil.DS, lexicon.DSigElementTransform) || xmlutil.Attr(ts[0], "Algorithm") != xmldsig.EnvelopedSignature || xmlutil.Attr(ts[1], "Algorithm") != c14n.Exclusive || len(ts[0].Children()) != 0 || len(ts[1].Children()) != 0 { + if len(ts) != referenceTransformCount || ts[0].Name() != xmlutil.Name(xmlutil.DS, lexicon.DSigElementTransform) || ts[1].Name() != xmlutil.Name(xmlutil.DS, lexicon.DSigElementTransform) || xmlutil.Attr(ts[0], lexicon.SecurityAttrAlgorithm) != xmldsig.EnvelopedSignature || !simpleCanonicalization(ts[1]) || len(ts[0].Children()) != 0 { return nil, ErrSignature } for _, key := range keys { - r, e := xmldsig.VerifySignature(ctx, d, sig, key, xmldsig.SignatureOptions{Policy: p, ExpectedURIs: []string{"#" + xmlutil.Attr(target, "ID")}, RequiredElements: []document.Element{target}}) + opts := xmldsig.SignatureOptions{Policy: p, MaxSignatures: MaxMessageSignatures, AllowedCanonicalizationMethods: []string{c14n.Exclusive, c14n.Canonical10}, ExpectedURIs: []string{"#" + xmlutil.Attr(target, lexicon.SAMLAttrID)}, RequiredElements: []document.Element{target}} + + var ( + r VerifiedSignature + e error + ) + if secret, ok := key.(SharedSecret); ok { + r, e = xmldsig.VerifyHMACSignature(ctx, d, sig, secret.value, opts) + } else { + r, e = xmldsig.VerifySignature(ctx, d, sig, key, opts) + } + if e == nil { + if scope != d { + return scopedSignature{proof: r, scope: scope}, nil + } + return r, nil } @@ -122,22 +207,33 @@ func Verify(ctx context.Context, d *document.Document, target document.Element, return nil, ErrSignature } +type scopedSignature struct { + proof VerifiedSignature + scope *document.Document +} + +func (s scopedSignature) CoversElement(e document.Element) bool { + owner, _ := tree.Unwrap(e) + return owner == s.scope && s.proof.CoversElement(tree.ContextElement(e)) +} + +func samlCanonicalization(algorithm string) bool { + return algorithm == c14n.Exclusive || algorithm == c14n.Canonical10 +} + // Sign signs a copied SAML target and places its signature at the schema-required // position. func Sign(ctx context.Context, d *document.Document, target document.Element, k SigningKey, afterIssuer bool) (*document.Document, error) { - if ctx == nil || d == nil || !target.Valid() || nilcheck.IsNil(k.Key) || !profile.ID(xmlutil.Attr(target, "ID")) { + const ( + xmlNameIssuer = "Issuer" + ) + + if ctx == nil || d == nil || !target.Valid() || !profile.ID(xmlutil.Attr(target, lexicon.SAMLAttrID)) { return nil, ErrSignature } - if len(k.CertificateDER) > 0 { - c, e := x509cert.ParseWithMaxBytes(k.CertificateDER, len(k.CertificateDER)) - if e != nil { - return nil, e - } - - if e = signature.MatchCertificate(k.Key, c); e != nil { - return nil, e - } + if err := k.validate(); err != nil { + return nil, err } id, e := profile.NewID() @@ -145,7 +241,21 @@ func Sign(ctx context.Context, d *document.Document, target document.Element, k return nil, e } - spec := xmldsig.SignatureTemplate{ID: id, SignatureMethod: k.Method, Canonicalization: reference.Transform{Algorithm: c14n.Exclusive}, References: []xmldsig.ReferenceTemplate{{URI: "#" + xmlutil.Attr(target, "ID"), DigestMethod: xmldsig.SHA256, Transforms: []reference.Transform{{Algorithm: xmldsig.EnvelopedSignature}, {Algorithm: c14n.Exclusive}}}}} + digest := k.DigestMethod + if digest == "" { + digest = xmldsig.SHA256 + } + + canonicalization := k.Canonicalization + if canonicalization == "" { + canonicalization = c14n.Exclusive + } + + if !samlCanonicalization(canonicalization) { + return nil, ErrSignature + } + + spec := xmldsig.SignatureTemplate{ID: id, SignatureMethod: k.Method, Canonicalization: reference.Transform{Algorithm: canonicalization, InclusivePrefixes: k.InclusivePrefixes}, References: []xmldsig.ReferenceTemplate{{URI: "#" + xmlutil.Attr(target, lexicon.SAMLAttrID), DigestMethod: digest, Transforms: []reference.Transform{{Algorithm: xmldsig.EnvelopedSignature}, {Algorithm: canonicalization, InclusivePrefixes: k.InclusivePrefixes}}}}} if len(k.CertificateDER) > 0 { spec.Certificates = [][]byte{k.CertificateDER} } @@ -172,7 +282,7 @@ func Sign(ctx context.Context, d *document.Document, target document.Element, k found := false for i, c := range others { - if c.Kind == tree.ElementNode && c.Name == xmlutil.Name(xmlutil.Assertion, "Issuer") { + if c.Kind == tree.ElementNode && c.Name == xmlutil.Name(xmlutil.Assertion, xmlNameIssuer) { pos = i + 1 found = true @@ -191,14 +301,114 @@ func Sign(ctx context.Context, d *document.Document, target document.Element, k children = append(children, others[pos:]...) parent.Children = children // Template signing checks all completed preexisting signatures both before and - // after serialization. This helper only supports the same authorized key. + // after serialization. Other assertion issuers need explicitly supplied trust. + preserve, err := preservedSignatures(ctx, draft, sig, k) + if err != nil { + return nil, err + } + + options := xmldsig.TemplateSignOptions{Validation: xmldsig.SignatureOptions{Policy: k.Policy, MaxSignatures: MaxMessageSignatures, AllowedCanonicalizationMethods: []string{canonicalization}, ExpectedURIs: []string{"#" + xmlutil.Attr(target, lexicon.SAMLAttrID)}}, Preserve: preserve} + if k.Secret != nil { + return xmldsig.SignHMACTemplate(ctx, draft, sig, k.Secret.value, options) + } + + return xmldsig.SignTemplate(ctx, draft, sig, k.Key, options) +} + +func preservedSignatures(ctx context.Context, draft *document.Document, sig document.Element, k SigningKey) ([]xmldsig.SignatureRequirement, error) { var preserve []xmldsig.SignatureRequirement for _, s := range xmldsig.Signatures(draft) { if !s.Same(sig) { - preserve = append(preserve, xmldsig.SignatureRequirement{Signature: s, PublicKey: k.Key.Public(), Options: xmldsig.SignatureOptions{Policy: k.Policy}}) + requirement := xmldsig.SignatureRequirement{Signature: s, Options: xmldsig.SignatureOptions{Policy: k.Policy, MaxSignatures: MaxMessageSignatures, AllowedCanonicalizationMethods: []string{c14n.Exclusive, c14n.Canonical10}}} + if k.PreserveKeys != nil { + _, node := tree.Unwrap(s) + + keys, err := k.PreserveKeys(ctx, tree.Wrap(draft, node.Parent)) + if err != nil { + return nil, err + } + + requirement, err = preservationCredential(ctx, draft, requirement, keys) + if err != nil { + return nil, err + } + } else if k.Secret != nil { + requirement.SharedSecret = k.Secret.value + } else { + requirement.PublicKey = k.Key.Public() + } + + preserve = append(preserve, requirement) + } + } + + return preserve, nil +} + +func preservationCredential(ctx context.Context, d *document.Document, r xmldsig.SignatureRequirement, keys []crypto.PublicKey) (xmldsig.SignatureRequirement, error) { + if len(keys) == 0 || len(keys) > maximumTrustedKeys { + return r, ErrSignature + } + + for _, key := range keys { + if secret, ok := key.(SharedSecret); ok { + if _, err := xmldsig.VerifyHMACSignature(ctx, d, r.Signature, secret.value, r.Options); err == nil { + r.SharedSecret = secret.value + return r, nil + } + } else if _, err := xmldsig.VerifySignature(ctx, d, r.Signature, key, r.Options); err == nil { + r.PublicKey = key + return r, nil + } + + if err := ctx.Err(); err != nil { + return r, err } } - return xmldsig.SignTemplate(ctx, draft, sig, k.Key, xmldsig.TemplateSignOptions{Validation: xmldsig.SignatureOptions{Policy: k.Policy, ExpectedURIs: []string{"#" + xmlutil.Attr(target, "ID")}}, Preserve: preserve}) + return r, ErrSignature +} + +func (k SigningKey) validate() error { + if nilcheck.IsNil(k.Key) && k.Secret == nil || !nilcheck.IsNil(k.Key) && k.Secret != nil || k.Secret != nil && len(k.CertificateDER) != 0 { + return ErrSignature + } + + if len(k.CertificateDER) > 0 { + c, e := x509cert.ParseWithMaxBytes(k.CertificateDER, len(k.CertificateDER)) + if e != nil { + return e + } + + if e = signature.MatchCertificate(k.Key, c); e != nil { + return e + } + } + + return nil +} + +func simpleCanonicalization(e document.Element) bool { + algorithm := xmlutil.Attr(e, lexicon.SecurityAttrAlgorithm) + if !samlCanonicalization(algorithm) || xmlutil.Attrs(e, []string{lexicon.SecurityAttrAlgorithm}, nil) != nil { + return false + } + + children, err := xmlutil.Children(e) + if err != nil { + return false + } + + if len(children) == 0 { + return true + } + + if algorithm != c14n.Exclusive || len(children) != 1 { + return false + } + + parameters := children[0] + + return parameters.Name() == xmlutil.Name(c14n.Exclusive, lexicon.C14NElementInclusiveNamespaces) && xmlutil.Attrs(parameters, []string{lexicon.C14NAttrPrefixList}, nil) == nil && xmlutil.EmptyContent(parameters) } diff --git a/saml/internal/xmlsig/sign_test.go b/saml/internal/xmlsig/sign_test.go index df73c0d..ea30c2d 100644 --- a/saml/internal/xmlsig/sign_test.go +++ b/saml/internal/xmlsig/sign_test.go @@ -1,6 +1,7 @@ package xmlsig_test import ( + "bytes" "crypto" "crypto/ecdsa" "errors" @@ -12,6 +13,33 @@ import ( "github.com/skarm/xmlsec/xmldsig" ) +// SAML Core 5.4.3-5.4.4 permits the standard Exclusive C14N PrefixList. +// OpenSAML uses this for QName-valued attribute content; all listed namespace +// bindings must stay within the signature's authenticated input. +func TestInclusiveNamespaceParameters(t *testing.T) { + key := testcert.Key(t) + d, err := document.Parse(t.Context(), []byte(`signed`), document.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + signed, err := xmldsig.SignEnveloped(t.Context(), d, key, xmldsig.SignOptions{InclusivePrefixes: []string{"t", "#default"}}) + if err != nil { + t.Fatal(err) + } + if _, err = xmlsig.Verify(t.Context(), signed, signed.Root(), []crypto.PublicKey{key.Public()}, xmldsig.Policy{}); err != nil { + t.Fatal(err) + } + raw, _ := signed.XML(t.Context()) + changed := bytes.ReplaceAll(raw, []byte("urn:original"), []byte("urn:changed")) + mutant, err := document.Parse(t.Context(), changed, document.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + if _, err = xmlsig.Verify(t.Context(), mutant, mutant.Root(), []crypto.PublicKey{key.Public()}, xmldsig.Policy{}); err == nil { + t.Fatal("changed QName namespace accepted") + } +} + func TestSignCoversOnlySelectedTarget(t *testing.T) { key := testcert.Key(t) @@ -54,7 +82,7 @@ func TestCloneKeysAndPolicyOwnTheirInputs(t *testing.T) { } cloned, ok := keys[0].(*ecdsa.PublicKey) - if !ok { + if !ok || cloned == nil { t.Fatalf("unexpected key type %T", keys[0]) } diff --git a/saml/internal/xmlutil/schema_test.go b/saml/internal/xmlutil/schema_test.go index e0494b1..01307f0 100644 --- a/saml/internal/xmlutil/schema_test.go +++ b/saml/internal/xmlutil/schema_test.go @@ -97,7 +97,9 @@ func TestDirectChildRequiresOneExpandedName(t *testing.T) { } return } - if err != nil || got.Name() != (document.Name{Space: "urn:expected", Local: "item"}) { + if err != nil { + t.Fatal(err) + } else if got.Name() != (document.Name{Space: "urn:expected", Local: "item"}) { t.Fatalf("Direct selected %v: %v", got.Name(), err) } if text, err := xmlutil.Simple(got, 6); err != nil || text != "chosen" { diff --git a/saml/internal/xmlutil/xml.go b/saml/internal/xmlutil/xml.go index 0d48e4d..9870bb0 100644 --- a/saml/internal/xmlutil/xml.go +++ b/saml/internal/xmlutil/xml.go @@ -109,6 +109,10 @@ func Simple(e document.Element, maximum int) (string, error) { // SimpleText returns bounded direct text, ignoring comments/PIs and rejecting // child elements. Attributes are validated separately by the caller. func SimpleText(e document.Element, maximum int) (string, error) { + const ( + concatenationSegmentThreshold = 2 + ) + if maximum < 0 || len(e.Children()) != 0 { return "", profile.ErrSchema } @@ -138,7 +142,7 @@ func SimpleText(e document.Element, maximum int) (string, error) { segments++ } - if segments < 2 { + if segments < concatenationSegmentThreshold { return single, nil } diff --git a/saml/metadata/authority.go b/saml/metadata/authority.go new file mode 100644 index 0000000..ffa61d3 --- /dev/null +++ b/saml/metadata/authority.go @@ -0,0 +1,39 @@ +package metadata + +import "slices" + +func requiredRoleEndpoint(kind RoleType) string { + switch kind { + case IdentityProvider: + return SingleSignOnService + case ServiceProvider: + return AssertionConsumerService + case AuthenticationAuthority: + return AuthnQueryService + case AttributeAuthority: + return AttributeService + case PolicyDecisionPoint: + return AuthzService + default: + return "" + } +} + +func isAuthorityRole(kind RoleType) bool { + return kind == AuthenticationAuthority || kind == AttributeAuthority || kind == PolicyDecisionPoint +} + +func validRoleEndpoint(kind RoleType, endpoint string) bool { + return slices.Contains(roleEndpointOrder(kind), endpoint) +} + +func roleEndpointOrder(kind RoleType) []string { + switch kind { + case IdentityProvider: + return []string{ArtifactResolutionService, SingleLogoutService, ManageNameIDService, nameNameIDFormat, SingleSignOnService, NameIDMappingService, AssertionIDRequestService} + case ServiceProvider: + return []string{ArtifactResolutionService, SingleLogoutService, ManageNameIDService, nameNameIDFormat, AssertionConsumerService} + default: + return []string{requiredRoleEndpoint(kind), AssertionIDRequestService, nameNameIDFormat} + } +} diff --git a/saml/metadata/authority_test.go b/saml/metadata/authority_test.go new file mode 100644 index 0000000..53f494d --- /dev/null +++ b/saml/metadata/authority_test.go @@ -0,0 +1,60 @@ +package metadata_test + +import ( + "testing" + "time" + + "github.com/skarm/xmlsec/internal/testutil/testcert" + "github.com/skarm/xmlsec/saml/metadata" +) + +func TestAuthorityMetadataRoles(t *testing.T) { + c := testcert.New(t) + at := testcert.Now() + for _, test := range []struct { + role metadata.RoleType + endpoint string + }{{metadata.AuthenticationAuthority, metadata.AuthnQueryService}, {metadata.AttributeAuthority, metadata.AttributeService}, {metadata.PolicyDecisionPoint, metadata.AuthzService}} { + t.Run(string(test.role), func(t *testing.T) { + role := &metadata.RoleDescriptor{Keys: []metadata.KeyDescriptor{{Use: "signing", CertificateDER: c.Leaf.Raw}}, Endpoints: map[string][]metadata.Endpoint{test.endpoint: {{Binding: metadata.SOAP, Location: "https://authority.test/soap"}}, metadata.AssertionIDRequestService: {{Binding: metadata.URI, Location: "https://authority.test/assertion"}}}} + spec := metadata.EntityDescriptor{EntityID: "https://authority.test/entity", ValidUntil: at.Add(time.Hour)} + switch test.role { + case metadata.AuthenticationAuthority: + spec.AuthnAuthority = role + case metadata.AttributeAuthority: + spec.AttributeAuthority = role + case metadata.PolicyDecisionPoint: + spec.PDP = role + } + d, err := metadata.Build(t.Context(), spec, at, metadata.Options{}) + if err != nil { + t.Fatal(err) + } + raw, _ := d.XML(t.Context()) + snapshot, err := metadata.ParseTrusted(t.Context(), raw, at, metadata.Options{}) + if err != nil { + t.Fatal(err) + } + entity, err := snapshot.Find(spec.EntityID, at) + if err != nil { + t.Fatal(err) + } + got, err := entity.Role(test.role, at) + if err != nil || len(got.Endpoints(test.endpoint)) != 1 || len(got.SigningKeys()) != 1 { + t.Fatalf("authority role: %v", err) + } + role.Endpoints = map[string][]metadata.Endpoint{metadata.SingleSignOnService: {{Binding: metadata.SOAP, Location: "https://authority.test/soap"}}} + if _, err = metadata.Build(t.Context(), spec, at, metadata.Options{}); err == nil { + t.Fatal("authority without its required endpoint accepted") + } + }) + } +} + +func TestNameIDMappingMetadataResponseLocation(t *testing.T) { + at := testcert.Now() + spec := metadata.EntityDescriptor{EntityID: "https://authority.test/entity", ValidUntil: at.Add(time.Hour), IDP: &metadata.RoleDescriptor{Endpoints: map[string][]metadata.Endpoint{metadata.SingleSignOnService: {{Binding: metadata.SOAP, Location: "https://authority.test/soap"}}, metadata.NameIDMappingService: {{Binding: metadata.SOAP, Location: "https://authority.test/mapping", ResponseLocation: "https://other.test/response"}}}}} + if _, err := metadata.Build(t.Context(), spec, at, metadata.Options{}); err == nil { + t.Fatal("forbidden NameIDMappingService ResponseLocation accepted") + } +} diff --git a/saml/metadata/build.go b/saml/metadata/build.go index 1d76b74..2fe95c1 100644 --- a/saml/metadata/build.go +++ b/saml/metadata/build.go @@ -18,6 +18,11 @@ import ( "github.com/skarm/xmlsec/xmldsig" ) +// Suggested prefix used by metadata construction. Parsing accepts other prefixes and checks the metadata namespace instead. +const ( + prefixMetadata = "md" +) + // KeyDescriptor advertises a DER certificate for signing, encryption or both uses. type KeyDescriptor struct { // Use is signing, encryption or empty for both uses. @@ -48,8 +53,8 @@ type RoleDescriptor struct { WantAssertionsSigned bool } -// EntityDescriptor supplies an entity identifier, expiration and optional SP and IdP -// roles. +// EntityDescriptor supplies an entity identifier, expiration and optional SSO +// provider and authority roles. type EntityDescriptor struct { // ID is the entity descriptor XML ID. Empty generates an unpredictable ID. ID string @@ -63,6 +68,12 @@ type EntityDescriptor struct { SP *RoleDescriptor // IDP supplies the identity provider role. Nil omits that role. IDP *RoleDescriptor + // AuthnAuthority supplies an authentication-authority role. + AuthnAuthority *RoleDescriptor + // AttributeAuthority supplies an attribute-authority role. + AttributeAuthority *RoleDescriptor + // PDP supplies an authorization-decision authority role. + PDP *RoleDescriptor } // Build builds a supported metadata document with explicit validity. The returned document @@ -93,10 +104,10 @@ func Build(ctx context.Context, s EntityDescriptor, at time.Time, o Options) (*d return nil, ErrMetadata } - root := md("EntityDescriptor") - root.NS = []tree.Namespace{{Prefix: "md", URI: Namespace}, {Prefix: "ds", URI: xmlutil.DS}} - xmlutil.Set(root, "ID", id) - xmlutil.Set(root, "entityID", s.EntityID) + root := md(xmlNameEntityDescriptor) + root.NS = []tree.Namespace{{Prefix: prefixMetadata, URI: Namespace}, {Prefix: lexicon.PrefixDSig, URI: xmlutil.DS}} + xmlutil.Set(root, lexicon.SAMLAttrID, id) + xmlutil.Set(root, xmlNameEntityID, s.EntityID) xmlutil.Set(root, nameValidUntil, profile.Time(s.ValidUntil)) if returned1Value, matchedBranch := buildCacheDuration(&s, root); matchedBranch { @@ -159,7 +170,7 @@ func Sign(ctx context.Context, d *document.Document, key crypto.Signer, certific return signed, nil } -func md(local string) *tree.Node { return xmlutil.Node("md", Namespace, local) } +func md(local string) *tree.Node { return xmlutil.Node(prefixMetadata, Namespace, local) } func buildOrder(order []string, r *struct { kind RoleType @@ -180,18 +191,19 @@ func buildOrder(order []string, r *struct { used[kind] = struct{}{} for _, ep := range r.spec.Endpoints[kind] { n2 := md(kind) - xmlutil.Set(n2, "Binding", ep.Binding) - xmlutil.Set(n2, "Location", ep.Location) + xmlutil.Set(n2, xmlNameBinding, ep.Binding) + xmlutil.Set(n2, xmlNameLocation, ep.Location) + endpointExtensions(n2, ep) if ep.ResponseLocation != "" { - xmlutil.Set(n2, "ResponseLocation", ep.ResponseLocation) + xmlutil.Set(n2, xmlNameResponseLocation, ep.ResponseLocation) } if kind == AssertionConsumerService || kind == ArtifactResolutionService { - xmlutil.Set(n2, "index", strconv.Itoa(int(ep.Index))) + xmlutil.Set(n2, xmlNameIndex, strconv.Itoa(int(ep.Index))) if ep.DefaultSet || ep.Default { - xmlutil.Set(n2, "isDefault", strconv.FormatBool(ep.Default)) + xmlutil.Set(n2, xmlNameIsDefault, strconv.FormatBool(ep.Default)) } } @@ -201,7 +213,7 @@ func buildOrder(order []string, r *struct { } func buildIsZero(ctx context.Context, at *time.Time, s *EntityDescriptor) (returned1 error, matched bool) { - if ctx == nil || at.IsZero() || s.ValidUntil.IsZero() || !s.ValidUntil.After(*at) || s.SP == nil && s.IDP == nil || !profile.URI(s.EntityID) { + if ctx == nil || at.IsZero() || s.ValidUntil.IsZero() || !s.ValidUntil.After(*at) || s.SP == nil && s.IDP == nil && s.AuthnAuthority == nil && s.AttributeAuthority == nil && s.PDP == nil || !profile.URI(s.EntityID) { return ErrMetadata, true } @@ -209,17 +221,23 @@ func buildIsZero(ctx context.Context, at *time.Time, s *EntityDescriptor) (retur } func buildCacheDuration(s *EntityDescriptor, root *tree.Node) (returned1Value error, matchedBranch bool) { + const ( + decimalRadix = 10 + maximumCacheDuration = 365 * 24 * time.Hour + nanosecondsPerSecond = int64(time.Second) + ) + if s.CacheDuration != 0 { - if s.CacheDuration <= 0 || s.CacheDuration > 365*24*time.Hour { + if s.CacheDuration <= 0 || s.CacheDuration > maximumCacheDuration { return ErrMetadata, true } secs := s.CacheDuration / time.Second frac := s.CacheDuration % time.Second - value := strconv.FormatInt(int64(secs), 10) + value := strconv.FormatInt(int64(secs), decimalRadix) if frac != 0 { - value += "." + strings.TrimRight(strconv.FormatInt(int64(frac)+1e9, 10)[1:], "0") + value += "." + strings.TrimRight(strconv.FormatInt(int64(frac)+nanosecondsPerSecond, decimalRadix)[1:], "0") } xmlutil.Set(root, nameCacheDuration, "PT"+value+"S") @@ -232,31 +250,36 @@ func buildEntries(ctx context.Context, s *EntityDescriptor, root *tree.Node, max for _, r := range []struct { kind RoleType spec *RoleDescriptor - }{{IdentityProvider, s.IDP}, {ServiceProvider, s.SP}} { + }{{IdentityProvider, s.IDP}, {ServiceProvider, s.SP}, {AuthenticationAuthority, s.AuthnAuthority}, {AttributeAuthority, s.AttributeAuthority}, {PolicyDecisionPoint, s.PDP}} { if r.spec == nil { continue } - if len(r.spec.Keys) > maxKeys { + if len(r.spec.Keys) > maxKeys || validateEndpointExtensionValues(r.spec.Endpoints) != nil { + return ErrMetadata, true + } + + if isAuthorityRole(r.kind) && (r.spec.AuthnRequestsSigned || r.spec.WantAuthnRequestsSigned || r.spec.WantAssertionsSigned) { return ErrMetadata, true } n := md(string(r.kind)) - xmlutil.Set(n, "protocolSupportEnumeration", Protocol) + xmlutil.Set(n, xmlNameProtocolSupportEnumeration, Protocol) if !r.spec.ValidUntil.IsZero() { xmlutil.Set(n, nameValidUntil, profile.Time(r.spec.ValidUntil)) } - if r.kind == IdentityProvider { - xmlutil.Set(n, "WantAuthnRequestsSigned", strconv.FormatBool(r.spec.WantAuthnRequestsSigned)) - } else { - xmlutil.Set(n, "AuthnRequestsSigned", strconv.FormatBool(r.spec.AuthnRequestsSigned)) - xmlutil.Set(n, "WantAssertionsSigned", strconv.FormatBool(r.spec.WantAssertionsSigned)) + switch r.kind { + case IdentityProvider: + xmlutil.Set(n, xmlNameWantAuthnRequestsSigned, strconv.FormatBool(r.spec.WantAuthnRequestsSigned)) + case ServiceProvider: + xmlutil.Set(n, xmlNameAuthnRequestsSigned, strconv.FormatBool(r.spec.AuthnRequestsSigned)) + xmlutil.Set(n, xmlNameWantAssertionsSigned, strconv.FormatBool(r.spec.WantAssertionsSigned)) } for _, k := range r.spec.Keys { - if len(k.CertificateDER) > 32<<10 { + if len(k.CertificateDER) > maximumCertificateBytes { return ErrMetadata, true } @@ -265,14 +288,14 @@ func buildEntries(ctx context.Context, s *EntityDescriptor, root *tree.Node, max return err, true } - kd := md("KeyDescriptor") + kd := md(xmlNameKeyDescriptor) if k.Use != "" { - xmlutil.Set(kd, "use", k.Use) + xmlutil.Set(kd, lexicon.AttrUse, k.Use) } - ki := xmlutil.Node("ds", xmlutil.DS, lexicon.DSigElementKeyInfo) - xd := xmlutil.Node("ds", xmlutil.DS, lexicon.DSigElementX509Data) - xc := xmlutil.Node("ds", xmlutil.DS, lexicon.DSigElementX509Certificate) + ki := xmlutil.Node(lexicon.PrefixDSig, xmlutil.DS, lexicon.DSigElementKeyInfo) + xd := xmlutil.Node(lexicon.PrefixDSig, xmlutil.DS, lexicon.DSigElementX509Data) + xc := xmlutil.Node(lexicon.PrefixDSig, xmlutil.DS, lexicon.DSigElementX509Certificate) xmlutil.Text(xc, encoded) xmlutil.Add(xd, xc) xmlutil.Add(ki, xd) @@ -280,12 +303,7 @@ func buildEntries(ctx context.Context, s *EntityDescriptor, root *tree.Node, max xmlutil.Add(n, kd) } - order := []string{ArtifactResolutionService, SingleLogoutService, ManageNameIDService, nameNameIDFormat} - if r.kind == IdentityProvider { - order = append(order, SingleSignOnService, NameIDMappingService) - } else { - order = append(order, AssertionConsumerService) - } + order := roleEndpointOrder(r.kind) used := map[string]struct{}{} diff --git a/saml/metadata/doc.go b/saml/metadata/doc.go index 062be98..9980795 100644 --- a/saml/metadata/doc.go +++ b/saml/metadata/doc.go @@ -1,4 +1,4 @@ -// Package metadata builds, authenticates and reads bounded SAML 2.0 SP and IdP +// Package metadata builds, authenticates and reads bounded SAML 2.0 provider and authority // metadata. Its snapshots supply authorized keys, endpoints and role requirements // to the protocol and orchestration packages. // @@ -30,6 +30,13 @@ // [Resolver] supplies already-authenticated or explicitly trusted entities to SP/IdP // flows. A network implementation must authenticate, bound and cache its input and // honor cancellation. Entity IDs and endpoint URIs do not trigger retrieval in -// this package. Artifact and SOAP constants identify metadata bindings; they do -// not enable those protocols. +// this package. Artifact/SOAP, SLO, NameID and PAOS declarations are used by the +// corresponding protocol, artifact, session and ECP packages. Authentication, +// attribute and authorization authorities also publish query and assertion-ID/URI +// services used by saml/authority; parsing metadata +// alone does not start a protocol exchange. +// HoK endpoints retain the profile discriminator in [Endpoint.Binding] and the +// actual transport in [Endpoint.ProtocolBinding]. [Role.HoKACS] selects only +// explicit HoK advertisements. SupportedChannelBindings is the authenticated +// endpoint extension list, not evidence of a verified transport channel. package metadata diff --git a/saml/metadata/ecp.go b/saml/metadata/ecp.go new file mode 100644 index 0000000..66a4c1a --- /dev/null +++ b/saml/metadata/ecp.go @@ -0,0 +1,196 @@ +package metadata + +import ( + "slices" + "strings" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/lexicon" + "github.com/skarm/xmlsec/internal/tree" + "github.com/skarm/xmlsec/saml/internal/profile" + "github.com/skarm/xmlsec/saml/internal/xmlutil" +) + +// Maximum advertised channel-binding alternatives per endpoint; parser and builder apply the same list cardinality bound. +const ( + maximumChannelTypes = 16 +) + +// Metadata vocabulary local name shared by builder and parser; callers must still check the metadata or extension namespace. +const ( + xmlNameProtocolBinding = "ProtocolBinding" + xmlNameSupportsChannelBindings = "supportsChannelBindings" +) + +// Per-token byte ceiling for channel-binding type identifiers in metadata extensions; list splitting uses only XML whitespace. +const ( + maximumChannelTypeBytes = 256 +) + +// HoKSSO is the HoK SSO profile discriminator used as a metadata Binding. +// The hoksso:ProtocolBinding attribute specifies the actual SAML transport. +const HoKSSO = "urn:oasis:names:tc:SAML:2.0:profiles:holder-of-key:SSO:browser" + +// ChannelBindingNamespace identifies SAML Channel Binding Extensions CS01. +const ChannelBindingNamespace = lexicon.NamespaceSAMLChannelBinding + +// HolderOfKey reports an explicitly advertised HoK endpoint. +func (e Endpoint) HolderOfKey() bool { return e.Binding == HoKSSO } + +// TransportBinding returns the SAML transport, preserving Binding separately. +func (e Endpoint) TransportBinding() string { + if e.HolderOfKey() { + return e.ProtocolBinding + } + + return e.Binding +} + +// AcceptsTransport reports whether the endpoint supports the transport and +// permits the requested proof policy. HoK-only endpoints require holderOfKey. +func (e Endpoint) AcceptsTransport(binding string, holderOfKey bool) bool { + return e.TransportBinding() == binding && (!e.HolderOfKey() || holderOfKey) +} + +// SupportsChannelBinding reports an exact advertised binding type. It grants +// no proof of transport security or actual protocol-level channel comparison. +func (e Endpoint) SupportsChannelBinding(kind string) bool { + return slices.Contains(e.SupportedChannelBindings, kind) +} + +// SelectHoKACS selects only HoK-advertised endpoints for an actual transport, +// applying IndexedEndpointType defaults in metadata order. It never selects a +// bearer-only endpoint or silently normalizes the metadata Binding. +func SelectHoKACS(endpoints []Endpoint, transport string, index *uint16) (Endpoint, error) { + var candidates []Endpoint + + for _, endpoint := range endpoints { + if endpoint.HolderOfKey() && endpoint.ProtocolBinding == transport { + candidates = append(candidates, endpoint) + } + } + + return SelectACS(candidates, HoKSSO, index) +} + +// HoKACS selects an explicitly advertised HoK ACS for the given transport. +func (r *Role) HoKACS(transport string, index *uint16) (Endpoint, error) { + if r == nil || r.kind != ServiceProvider { + return Endpoint{}, ErrMetadata + } + + return SelectHoKACS(r.endpoints[AssertionConsumerService], transport, index) +} + +func cloneEndpoint(e Endpoint) Endpoint { + e.SupportedChannelBindings = slices.Clone(e.SupportedChannelBindings) + return e +} + +func cloneEndpoints(in []Endpoint) []Endpoint { + out := slices.Clone(in) + for i := range out { + out[i] = cloneEndpoint(out[i]) + } + + return out +} + +func endpointAttributes(e document.Element, required, optional []string) error { + seen := map[string]bool{} + + for _, a := range e.Attributes() { + if a.Name == (document.Name{Space: HoKSSO, Local: xmlNameProtocolBinding}) || a.Name == (document.Name{Space: ChannelBindingNamespace, Local: xmlNameSupportsChannelBindings}) { + continue + } + + if a.Name.Space != "" || !slices.Contains(required, a.Name.Local) && !slices.Contains(optional, a.Name.Local) { + return ErrMetadata + } + + seen[a.Name.Local] = true + } + + for _, name := range required { + if !seen[name] { + return ErrMetadata + } + } + + return nil +} + +func readEndpointExtensions(element document.Element, endpoint *Endpoint) error { + const ( + maximumChannelTypesTextBytes = 8192 + ) + + if value, present := element.Attribute(document.Name{Space: HoKSSO, Local: xmlNameProtocolBinding}); present { + if !profile.URI(value) || value == HoKSSO { + return ErrMetadata + } + + endpoint.ProtocolBinding = value + } + + if endpoint.HolderOfKey() && endpoint.ProtocolBinding == "" { + return ErrMetadata + } + + if value, present := element.Attribute(document.Name{Space: ChannelBindingNamespace, Local: xmlNameSupportsChannelBindings}); present { + if len(value) > maximumChannelTypesTextBytes { + return ErrMetadata + } + + values := strings.FieldsFunc(value, func(r rune) bool { return r == ' ' || r == '\t' || r == '\r' || r == '\n' }) + if len(values) > maximumChannelTypes { + return ErrMetadata + } + + endpoint.SupportedChannelBindings = make([]string, len(values)) + for i, value := range values { + if len(value) > maximumChannelTypeBytes { + return ErrMetadata + } + + endpoint.SupportedChannelBindings[i] = value + } + } + + return nil +} + +func endpointExtensions(node *tree.Node, endpoint Endpoint) { + const ( + prefixChannelBinding = "cb" + prefixHoKSSO = "hoksso" + ) + + if endpoint.ProtocolBinding != "" { + node.NS = append(node.NS, tree.Namespace{Prefix: prefixHoKSSO, URI: HoKSSO}) + node.Attrs = append(node.Attrs, tree.Attribute{Name: xmlutil.Name(HoKSSO, xmlNameProtocolBinding), Prefix: prefixHoKSSO, Value: endpoint.ProtocolBinding}) + } + + if endpoint.SupportedChannelBindings != nil { + node.NS = append(node.NS, tree.Namespace{Prefix: prefixChannelBinding, URI: ChannelBindingNamespace}) + node.Attrs = append(node.Attrs, tree.Attribute{Name: xmlutil.Name(ChannelBindingNamespace, xmlNameSupportsChannelBindings), Prefix: prefixChannelBinding, Value: strings.Join(endpoint.SupportedChannelBindings, " ")}) + } +} + +func validateEndpointExtensionValues(endpoints map[string][]Endpoint) error { + for _, group := range endpoints { + for _, endpoint := range group { + if len(endpoint.SupportedChannelBindings) > maximumChannelTypes { + return ErrMetadata + } + + for _, value := range endpoint.SupportedChannelBindings { + if value == "" || len(value) > maximumChannelTypeBytes || strings.ContainsAny(value, lexicon.XMLWhitespace) { + return ErrMetadata + } + } + } + } + + return nil +} diff --git a/saml/metadata/ecp_test.go b/saml/metadata/ecp_test.go new file mode 100644 index 0000000..369accc --- /dev/null +++ b/saml/metadata/ecp_test.go @@ -0,0 +1,133 @@ +package metadata_test + +import ( + "bytes" + "crypto" + "reflect" + "strings" + "testing" + "time" + + "github.com/skarm/xmlsec/internal/testutil/testcert" + "github.com/skarm/xmlsec/saml/metadata" + "github.com/skarm/xmlsec/xmldsig" +) + +func ecpMetadataSpec() metadata.EntityDescriptor { + return metadata.EntityDescriptor{EntityID: "https://entity.test", ValidUntil: testcert.Now().Add(time.Hour), SP: &metadata.RoleDescriptor{Endpoints: map[string][]metadata.Endpoint{metadata.AssertionConsumerService: { + {Binding: metadata.HTTPPost, Location: "https://sp.test/post", Indexed: true, Index: 0}, + {Binding: metadata.HoKSSO, ProtocolBinding: metadata.PAOS, Location: "https://sp.test/hok", Indexed: true, Index: 1, SupportedChannelBindings: []string{"tls-server-end-point", "tls-exporter"}}, + }}}, IDP: &metadata.RoleDescriptor{Endpoints: map[string][]metadata.Endpoint{metadata.SingleSignOnService: {{Binding: metadata.HoKSSO, ProtocolBinding: metadata.SOAP, Location: "https://idp.test/ecp", SupportedChannelBindings: []string{"tls-server-end-point"}}}}}} +} + +func TestECPMetadataSignedRoundTripAndSelection(t *testing.T) { + c := testcert.New(t) + spec := ecpMetadataSpec() + d, err := metadata.Build(t.Context(), spec, testcert.Now(), metadata.Options{}) + if err != nil { + t.Fatal(err) + } + signed, err := metadata.Sign(t.Context(), d, c.LeafKey, c.Leaf.Raw, xmldsig.ECDSASHA256, testcert.Now(), metadata.Options{}) + if err != nil { + t.Fatal(err) + } + raw, _ := signed.XML(t.Context()) + for _, value := range []string{`hoksso:ProtocolBinding="` + metadata.PAOS + `"`, `Binding="` + metadata.HoKSSO + `"`, `cb:supportsChannelBindings="tls-server-end-point tls-exporter"`} { + if !bytes.Contains(raw, []byte(value)) { + t.Fatalf("missing metadata extension %q", value) + } + } + verifier, err := metadata.NewVerifier(metadata.Config{TrustedKeys: []crypto.PublicKey{c.LeafKey.Public()}}) + if err != nil { + t.Fatal(err) + } + snapshot, err := verifier.Verify(t.Context(), raw, testcert.Now()) + if err != nil { + t.Fatal(err) + } + entity, err := snapshot.Find(spec.EntityID, testcert.Now()) + if err != nil { + t.Fatal(err) + } + role, err := entity.Role(metadata.ServiceProvider, testcert.Now()) + if err != nil { + t.Fatal(err) + } + if _, err := role.ACS(metadata.PAOS, nil); err == nil { + t.Fatal("HoK-only ACS selected for bearer") + } + endpoint, err := role.HoKACS(metadata.PAOS, nil) + if err != nil || !endpoint.HolderOfKey() || endpoint.Binding != metadata.HoKSSO || endpoint.TransportBinding() != metadata.PAOS || endpoint.Index != 1 || !endpoint.SupportsChannelBinding("tls-exporter") { + t.Fatalf("HoK selection: %+v %v", endpoint, err) + } + endpoint.SupportedChannelBindings[0] = "changed" + list := role.Endpoints(metadata.AssertionConsumerService) + list[1].SupportedChannelBindings[0] = "changed" + again, err := role.HoKACS(metadata.PAOS, nil) + if err != nil || again.SupportedChannelBindings[0] != "tls-server-end-point" { + t.Fatal("caller mutated metadata snapshot", err) + } + index := uint16(0) + if _, err := role.HoKACS(metadata.PAOS, &index); err == nil { + t.Fatal("HoK selection ignored index or selected bearer endpoint") + } + tampered := bytes.Replace(raw, []byte("tls-exporter"), []byte("attacker-type"), 1) + if _, err := verifier.Verify(t.Context(), tampered, testcert.Now()); err == nil { + t.Fatal("extension metadata was not signed") + } +} + +func TestECPMetadataNamespacesAndListSyntax(t *testing.T) { + d, err := metadata.Build(t.Context(), ecpMetadataSpec(), testcert.Now(), metadata.Options{}) + if err != nil { + t.Fatal(err) + } + raw, _ := d.XML(t.Context()) + for _, test := range []struct { + name string + wire []byte + want []string + accept bool + }{ + {"arbitrary prefixes", bytes.ReplaceAll(bytes.ReplaceAll(bytes.ReplaceAll(bytes.ReplaceAll(raw, []byte("xmlns:cb="), []byte("xmlns:c=")), []byte("cb:supportsChannelBindings"), []byte("c:supportsChannelBindings")), []byte("xmlns:hoksso="), []byte("xmlns:h=")), []byte("hoksso:ProtocolBinding"), []byte("h:ProtocolBinding")), []string{"tls-server-end-point", "tls-exporter"}, true}, + {"XML whitespace", bytes.Replace(raw, []byte(`tls-server-end-point tls-exporter`), []byte(" tls-server-end-point tls-exporter "), 1), []string{"tls-server-end-point", "tls-exporter"}, true}, + {"empty present list", bytes.Replace(raw, []byte(`tls-server-end-point tls-exporter`), nil, 1), []string{}, true}, + {"not a boolean", bytes.Replace(raw, []byte(`tls-server-end-point tls-exporter`), []byte("true"), 1), []string{"true"}, true}, + {"non XML whitespace", bytes.Replace(raw, []byte(`tls-server-end-point tls-exporter`), []byte("opaque\u00a0value"), 1), []string{"opaque\u00a0value"}, true}, + {"missing protocol", bytes.Replace(raw, []byte(` hoksso:ProtocolBinding="`+metadata.PAOS+`"`), nil, 1), nil, false}, + {"recursive protocol", bytes.Replace(raw, []byte(`hoksso:ProtocolBinding="`+metadata.PAOS+`"`), []byte(`hoksso:ProtocolBinding="`+metadata.HoKSSO+`"`), 1), nil, false}, + {"wrong namespace", bytes.ReplaceAll(raw, []byte(`xmlns:cb="`+metadata.ChannelBindingNamespace+`"`), []byte(`xmlns:cb="urn:untrusted"`)), nil, false}, + } { + t.Run(test.name, func(t *testing.T) { + snapshot, err := metadata.ParseTrusted(t.Context(), test.wire, testcert.Now(), metadata.Options{}) + if (err == nil) != test.accept { + t.Fatalf("parse: %v", err) + } + if !test.accept { + return + } + entity, err := snapshot.Find("https://entity.test", testcert.Now()) + if err != nil { + t.Fatal(err) + } + role, err := entity.Role(metadata.ServiceProvider, testcert.Now()) + if err != nil { + t.Fatal(err) + } + endpoint, err := role.HoKACS(metadata.PAOS, nil) + if err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(endpoint.SupportedChannelBindings, test.want) { + t.Fatalf("list=%#v, want=%#v", endpoint.SupportedChannelBindings, test.want) + } + }) + } + for _, list := range [][]string{{"tls-server-end-point tls-exporter"}, {""}, {strings.Repeat("x", 257)}} { + spec := ecpMetadataSpec() + spec.SP.Endpoints[metadata.AssertionConsumerService][1].SupportedChannelBindings = list + if _, err := metadata.Build(t.Context(), spec, testcert.Now(), metadata.Options{}); err == nil { + t.Fatal("invalid source list silently changed during serialization") + } + } +} diff --git a/saml/metadata/index_test.go b/saml/metadata/index_test.go index e941940..421ea6a 100644 --- a/saml/metadata/index_test.go +++ b/saml/metadata/index_test.go @@ -51,7 +51,9 @@ func TestMetadataIndexLexicalValues(t *testing.T) { t.Fatal(err) } endpoint, err := role.ACS(metadata.HTTPPost, &test.want) - if err != nil || endpoint.Index != test.want { + if err != nil { + t.Fatal(err) + } else if endpoint.Index != test.want { t.Fatalf("index=%d error=%v; want %d", endpoint.Index, err, test.want) } }) diff --git a/saml/metadata/metadata.go b/saml/metadata/metadata.go index 034b8de..0ba00de 100644 --- a/saml/metadata/metadata.go +++ b/saml/metadata/metadata.go @@ -16,12 +16,27 @@ import ( "github.com/skarm/xmlsec/internal/nilcheck" "github.com/skarm/xmlsec/internal/tree" "github.com/skarm/xmlsec/internal/x509cert" + "github.com/skarm/xmlsec/internal/xmlchar" "github.com/skarm/xmlsec/saml/internal/profile" "github.com/skarm/xmlsec/saml/internal/xmlsig" "github.com/skarm/xmlsec/saml/internal/xmlutil" "github.com/skarm/xmlsec/xmldsig" ) +// Exact metadata key-use markers; an absent use permits both classes. Certificates describe keys under metadata trust rather than establishing PKIX trust. +const ( + keyUseEncryption = "encryption" + keyUseSigning = "signing" +) + +// Metadata vocabulary local name shared by builder and parser; callers must still check the metadata or extension namespace. +const ( + xmlNameContactPerson = "ContactPerson" + xmlNameEntitiesDescriptor = "EntitiesDescriptor" + xmlNameExtensions = "Extensions" + xmlNameOrganization = "Organization" +) + // Namespace is the SAML 2.0 metadata namespace URI. const Namespace = xmlutil.Metadata @@ -34,25 +49,39 @@ const HTTPPost = lexicon.SAMLBindingHTTPPost // HTTPRedirect is the SAML HTTP-Redirect binding URI. const HTTPRedirect = lexicon.SAMLBindingHTTPRedirect -// HTTPArtifact is the SAML HTTP-Artifact binding URI; artifact processing is unsupported. +// HTTPArtifact is the SAML HTTP-Artifact binding URI. const HTTPArtifact = lexicon.SAMLBindingHTTPArtifact -// SOAP is the SAML SOAP binding URI; SOAP processing is unsupported. +// SOAP is the SAML SOAP binding URI. const SOAP = lexicon.SAMLBindingSOAP +// PAOS is the SAML reverse SOAP binding used by the ECP profile. +const PAOS = lexicon.SAMLBindingPAOS + +// URI is the SAML assertion URI binding. +const URI = "urn:oasis:names:tc:SAML:2.0:bindings:URI" + const ( // AssertionConsumerService selects assertion consumer endpoints in Role.Endpoints. AssertionConsumerService = lexicon.SAMLAssertionConsumerService // SingleSignOnService selects IdP sign-on endpoints in Role.Endpoints. SingleSignOnService = lexicon.SAMLSingleSignOnService - // SingleLogoutService selects logout declarations; logout processing is unsupported. + // SingleLogoutService selects logout endpoints. SingleLogoutService = lexicon.SAMLSingleLogoutService - // ArtifactResolutionService selects artifact declarations; artifact processing is unsupported. + // ArtifactResolutionService selects artifact resolution endpoints. ArtifactResolutionService = lexicon.SAMLArtifactResolutionService - // ManageNameIDService selects name identifier management declarations, without enabling processing. + // ManageNameIDService selects name identifier management endpoints. ManageNameIDService = lexicon.SAMLManageNameIDService - // NameIDMappingService selects name identifier mapping declarations, without enabling processing. + // NameIDMappingService selects name identifier mapping endpoints. NameIDMappingService = lexicon.SAMLNameIDMappingService + // AuthnQueryService selects authentication-authority query endpoints. + AuthnQueryService = "AuthnQueryService" + // AttributeService selects attribute-authority query endpoints. + AttributeService = "AttributeService" + // AuthzService selects authorization-decision query endpoints. + AuthzService = "AuthzService" + // AssertionIDRequestService selects assertion retrieval endpoints. + AssertionIDRequestService = "AssertionIDRequestService" ) // ErrMetadata reports SAML metadata rejected. @@ -61,7 +90,7 @@ var ErrMetadata = errors.New("SAML metadata rejected") // ErrExpired reports SAML metadata expired or requires refresh. var ErrExpired = errors.New("SAML metadata expired or requires refresh") -// RoleType identifies an SP or IdP metadata role. +// RoleType identifies an SSO provider or SAML authority metadata role. type RoleType string const ( @@ -69,12 +98,18 @@ const ( ServiceProvider RoleType = "SPSSODescriptor" // IdentityProvider identifies an IDPSSODescriptor metadata role. IdentityProvider RoleType = "IDPSSODescriptor" + // AuthenticationAuthority describes an AuthnQuery authority. + AuthenticationAuthority RoleType = "AuthnAuthorityDescriptor" + // AttributeAuthority describes an AttributeQuery authority. + AttributeAuthority RoleType = "AttributeAuthorityDescriptor" + // PolicyDecisionPoint describes an AuthzDecisionQuery authority. + PolicyDecisionPoint RoleType = "PDPDescriptor" ) // Endpoint identifies a binding and its service URL, with optional response URL and // indexed service selection. type Endpoint struct { - // Binding is the SAML binding URI supported by this endpoint. + // Binding is the wire binding URI or the HoK profile discriminator. Binding string // Location is the endpoint URL. Location string @@ -88,6 +123,13 @@ type Endpoint struct { Default bool // DefaultSet distinguishes an explicit false isDefault value from an absent attribute. DefaultSet bool + // ProtocolBinding is hoksso:ProtocolBinding, the actual transport when + // Binding identifies the HoK SSO profile. It never replaces the wire Binding. + ProtocolBinding string + // SupportedChannelBindings preserves cb:supportsChannelBindings as an XML + // whitespace-separated list. Nil means absent; an empty non-nil list is present. + // The bounded profile permits at most 16 tokens of at most 256 UTF-8 bytes each. + SupportedChannelBindings []string } // SelectACS selects from caller-authorized endpoints, preserving metadata order. @@ -104,7 +146,7 @@ func SelectACS(endpoints []Endpoint, binding string, index *uint16) (Endpoint, e } if index != nil || endpoint.Default { - return endpoint, nil + return cloneEndpoint(endpoint), nil } if !haveFirst { @@ -117,11 +159,11 @@ func SelectACS(endpoints []Endpoint, binding string, index *uint16) (Endpoint, e } if haveUnspecified { - return unspecified, nil + return cloneEndpoint(unspecified), nil } if haveFirst { - return first, nil + return cloneEndpoint(first), nil } return Endpoint{}, ErrMetadata @@ -183,7 +225,7 @@ func (r *Role) Endpoints(kind string) []Endpoint { return nil } - return append([]Endpoint(nil), r.endpoints[kind]...) + return cloneEndpoints(r.endpoints[kind]) } // Certificates returns owned certificate DER values for the requested use, including @@ -207,6 +249,17 @@ func (r *Role) Certificates(use string) [][]byte { // SigningKeys returns owned, deduplicated signing keys from trusted metadata. It does not // independently validate certificate lifetimes. func (r *Role) SigningKeys() []crypto.PublicKey { + return r.keysForUse(keyUseSigning) +} + +// EncryptionKeys returns owned, deduplicated keys from encryption or unspecified +// KeyDescriptors. Signing-only descriptors cannot authorize encryption. +// Certificates are key descriptions under metadata trust, not a PKIX chain. +func (r *Role) EncryptionKeys() []crypto.PublicKey { + return r.keysForUse(keyUseEncryption) +} + +func (r *Role) keysForUse(use string) []crypto.PublicKey { if r == nil { return nil } @@ -215,7 +268,7 @@ func (r *Role) SigningKeys() []crypto.PublicKey { seen := map[string]struct{}{} for _, k := range r.keys { - if _, exists := seen[string(k.spki)]; (k.use == "" || k.use == "signing") && !exists { + if _, exists := seen[string(k.spki)]; (k.use == "" || k.use == use) && !exists { pub, e := x509.ParsePKIXPublicKey(k.spki) if e == nil { out = append(out, pub) @@ -282,6 +335,16 @@ func (e *Entity) Role(kind RoleType, at time.Time) (*Role, error) { return r, nil } +// Config supplies metadata signer keys and parsing policy. Construction copies +// TrustedKeys. Embedded metadata keys cannot expand the trusted signer set. +type Config struct { + // TrustedKeys supplies the explicit metadata signer keys. Embedded keys never extend + // this set. + TrustedKeys []crypto.PublicKey + // Options sets metadata parsing, endpoint and signature policy. + Options Options +} + // Snapshot retains immutable metadata accepted under explicit trust and expiration // policy. type Snapshot struct { @@ -449,16 +512,6 @@ func (v *Verifier) verifyDocument(ctx context.Context, d *document.Document, at return s, nil } -// Config supplies metadata signer keys and parsing policy. Construction copies -// TrustedKeys. Embedded metadata keys cannot expand the trusted signer set. -type Config struct { - // TrustedKeys supplies the explicit metadata signer keys. Embedded keys never extend - // this set. - TrustedKeys []crypto.PublicKey - // Options sets metadata parsing, endpoint and signature policy. - Options Options -} - type key struct { use string der []byte @@ -466,27 +519,40 @@ type key struct { } func normalize(o Options) (Options, error) { + const ( + defaultMaximumEndpointsPerRole = 32 + defaultMaximumEntities = 128 + defaultMaximumKeysPerRole = 8 + defaultMetadataBytes = 2 << 20 + defaultMetadataLifetime = 24 * time.Hour + maximumEndpointsPerRole = 128 + maximumEntities = 4096 + maximumKeysPerRole = 16 + maximumMetadataBytes = 16 << 20 + maximumMetadataLifetime = 30 * 24 * time.Hour + ) + if o.MaxBytes == 0 { - o.MaxBytes = 2 << 20 + o.MaxBytes = defaultMetadataBytes } if o.MaxEntities == 0 { - o.MaxEntities = 128 + o.MaxEntities = defaultMaximumEntities } if o.MaxKeysPerRole == 0 { - o.MaxKeysPerRole = 8 + o.MaxKeysPerRole = defaultMaximumKeysPerRole } if o.MaxEndpointsPerRole == 0 { - o.MaxEndpointsPerRole = 32 + o.MaxEndpointsPerRole = defaultMaximumEndpointsPerRole } if o.MaxLifetime == 0 { - o.MaxLifetime = 24 * time.Hour + o.MaxLifetime = defaultMetadataLifetime } - if o.MaxBytes < 1 || o.MaxBytes > 16<<20 || o.MaxEntities < 1 || o.MaxEntities > 4096 || o.MaxKeysPerRole < 1 || o.MaxKeysPerRole > 16 || o.MaxEndpointsPerRole < 1 || o.MaxEndpointsPerRole > 128 || o.MaxLifetime <= 0 || o.MaxLifetime > 30*24*time.Hour { + if o.MaxBytes < 1 || o.MaxBytes > maximumMetadataBytes || o.MaxEntities < 1 || o.MaxEntities > maximumEntities || o.MaxKeysPerRole < 1 || o.MaxKeysPerRole > maximumKeysPerRole || o.MaxEndpointsPerRole < 1 || o.MaxEndpointsPerRole > maximumEndpointsPerRole || o.MaxLifetime <= 0 || o.MaxLifetime > maximumMetadataLifetime { return o, ErrMetadata } @@ -498,7 +564,7 @@ func normalize(o Options) (Options, error) { func expiry(e document.Element, at, parent time.Time) (time.Time, error) { out := parent - if s := xmlutil.Attr(e, nameValidUntil); s != "" { + if s, present := e.Attribute(document.Name{Local: nameValidUntil}); present { t, er := profile.ParseTime(s) if er != nil { return time.Time{}, er @@ -509,7 +575,7 @@ func expiry(e document.Element, at, parent time.Time) (time.Time, error) { } } - if s := xmlutil.Attr(e, nameCacheDuration); s != "" { + if s, present := e.Attribute(document.Name{Local: nameCacheDuration}); present { d, er := profile.Duration(s) if er != nil { return time.Time{}, er @@ -554,11 +620,15 @@ type metadataParser struct { } func (p *metadataParser) visit(element document.Element, parent time.Time, depth int) error { + const ( + maximumAggregateDepth = 8 + ) + if err := p.ctx.Err(); err != nil { return err } - if depth > 8 { + if depth > maximumAggregateDepth { return ErrMetadata } @@ -567,7 +637,7 @@ func (p *metadataParser) visit(element document.Element, parent time.Time, depth return err } - if element.Name() == xmlutil.Name(Namespace, "EntitiesDescriptor") { + if element.Name() == xmlutil.Name(Namespace, xmlNameEntitiesDescriptor) { return p.visitGroup(element, expires, depth) } @@ -575,11 +645,15 @@ func (p *metadataParser) visit(element document.Element, parent time.Time, depth } func (p *metadataParser) visitGroup(element document.Element, expires time.Time, depth int) error { - if xmlutil.Attrs(element, nil, []string{"ID", "Name", nameValidUntil, nameCacheDuration}) != nil { + const ( + xmlNameName = "Name" + ) + + if xmlutil.Attrs(element, nil, []string{lexicon.SAMLAttrID, xmlNameName, nameValidUntil, nameCacheDuration}) != nil { return ErrMetadata } - if id := xmlutil.Attr(element, "ID"); id != "" && !profile.ID(id) { + if id := xmlutil.Attr(element, lexicon.SAMLAttrID); id != "" && !profile.ID(id) { return ErrMetadata } @@ -592,13 +666,13 @@ func (p *metadataParser) visitGroup(element document.Element, expires time.Time, for _, child := range children { switch child.Name() { - case xmlutil.Name(Namespace, "EntityDescriptor"), xmlutil.Name(Namespace, "EntitiesDescriptor"): + case xmlutil.Name(Namespace, xmlNameEntityDescriptor), xmlutil.Name(Namespace, xmlNameEntitiesDescriptor): if err := p.visit(child, expires, depth+1); err != nil { return err } count++ - case xmlutil.Name(xmlutil.DS, lexicon.DSigElementSignature), xmlutil.Name(Namespace, "Extensions"): + case xmlutil.Name(xmlutil.DS, lexicon.DSigElementSignature), xmlutil.Name(Namespace, xmlNameExtensions): // Protected metadata content is not interpreted or fetched. default: return ErrMetadata @@ -613,16 +687,16 @@ func (p *metadataParser) visitGroup(element document.Element, expires time.Time, } func (p *metadataParser) visitEntity(element document.Element, expires time.Time) error { - if element.Name() != xmlutil.Name(Namespace, "EntityDescriptor") || xmlutil.Attrs(element, []string{"entityID"}, []string{"ID", nameValidUntil, nameCacheDuration}) != nil { + if element.Name() != xmlutil.Name(Namespace, xmlNameEntityDescriptor) || xmlutil.Attrs(element, []string{xmlNameEntityID}, []string{lexicon.SAMLAttrID, nameValidUntil, nameCacheDuration}) != nil { return ErrMetadata } - id := xmlutil.Attr(element, "entityID") + id := xmlutil.Attr(element, xmlNameEntityID) if !profile.URI(id) || p.snapshot.entities[id] != nil || len(p.snapshot.entities) >= p.options.MaxEntities { return ErrMetadata } - if xmlID := xmlutil.Attr(element, "ID"); xmlID != "" && !profile.ID(xmlID) { + if xmlID := xmlutil.Attr(element, lexicon.SAMLAttrID); xmlID != "" && !profile.ID(xmlID) { return ErrMetadata } @@ -648,9 +722,13 @@ func (p *metadataParser) visitEntity(element document.Element, expires time.Time } func (p *metadataParser) readEntityRoles(entity *Entity, children []document.Element) error { + const ( + xmlNameAdditionalMetadataLocation = "AdditionalMetadataLocation" + ) + for _, child := range children { switch child.Name() { - case xmlutil.Name(Namespace, string(ServiceProvider)), xmlutil.Name(Namespace, string(IdentityProvider)): + case xmlutil.Name(Namespace, string(ServiceProvider)), xmlutil.Name(Namespace, string(IdentityProvider)), xmlutil.Name(Namespace, string(AuthenticationAuthority)), xmlutil.Name(Namespace, string(AttributeAuthority)), xmlutil.Name(Namespace, string(PolicyDecisionPoint)): kind := RoleType(child.Name().Local) if entity.roles[kind] != nil { return ErrMetadata @@ -664,7 +742,7 @@ func (p *metadataParser) readEntityRoles(entity *Entity, children []document.Ele if role != nil { entity.roles[kind] = role } - case xmlutil.Name(xmlutil.DS, lexicon.DSigElementSignature), xmlutil.Name(Namespace, "Extensions"), xmlutil.Name(Namespace, "Organization"), xmlutil.Name(Namespace, "ContactPerson"), xmlutil.Name(Namespace, "AdditionalMetadataLocation"): + case xmlutil.Name(xmlutil.DS, lexicon.DSigElementSignature), xmlutil.Name(Namespace, xmlNameExtensions), xmlutil.Name(Namespace, xmlNameOrganization), xmlutil.Name(Namespace, xmlNameContactPerson), xmlutil.Name(Namespace, xmlNameAdditionalMetadataLocation): // Protected metadata content is not interpreted or fetched. default: return fmt.Errorf("%w: unsupported role/content %s", ErrMetadata, child.Name().Local) @@ -675,10 +753,22 @@ func (p *metadataParser) readEntityRoles(entity *Entity, children []document.Ele } func parseRole(ctx context.Context, element document.Element, at, parent time.Time, o Options) (*Role, error) { - if xmlutil.Attrs(element, []string{"protocolSupportEnumeration"}, []string{"ID", nameValidUntil, nameCacheDuration, "errorURL", "AuthnRequestsSigned", "WantAuthnRequestsSigned", "WantAssertionsSigned"}) != nil { + const ( + xmlNameErrorURL = "errorURL" + ) + + if xmlutil.Attrs(element, []string{xmlNameProtocolSupportEnumeration}, []string{lexicon.SAMLAttrID, nameValidUntil, nameCacheDuration, xmlNameErrorURL, xmlNameAuthnRequestsSigned, xmlNameWantAuthnRequestsSigned, xmlNameWantAssertionsSigned}) != nil { return nil, ErrMetadata } + if isAuthorityRole(RoleType(element.Name().Local)) { + for _, name := range []string{xmlNameAuthnRequestsSigned, xmlNameWantAuthnRequestsSigned, xmlNameWantAssertionsSigned} { + if _, ok := element.Attribute(document.Name{Local: name}); ok { + return nil, ErrMetadata + } + } + } + if !roleSupportsProtocol(element) { return nil, nil } @@ -706,7 +796,7 @@ func parseRole(ctx context.Context, element document.Element, at, parent time.Ti return nil, err } - if role.kind == IdentityProvider && len(role.endpoints[SingleSignOnService]) == 0 || role.kind == ServiceProvider && len(role.endpoints[AssertionConsumerService]) == 0 { + if len(role.endpoints[requiredRoleEndpoint(role.kind)]) == 0 { return nil, ErrMetadata } @@ -714,7 +804,7 @@ func parseRole(ctx context.Context, element document.Element, at, parent time.Ti } func roleSupportsProtocol(element document.Element) bool { - for protocol := range strings.FieldsSeq(xmlutil.Attr(element, "protocolSupportEnumeration")) { + for protocol := range strings.FieldsFuncSeq(xmlutil.Attr(element, xmlNameProtocolSupportEnumeration), xmlchar.IsSpace) { if protocol == Protocol { return true } @@ -726,22 +816,28 @@ func roleSupportsProtocol(element document.Element) bool { func readRoleSigningFlags(element document.Element, role *Role) error { var err error - role.authnSigned, err = xmlutil.BoolAttr(element, "AuthnRequestsSigned") + role.authnSigned, err = xmlutil.BoolAttr(element, xmlNameAuthnRequestsSigned) if err != nil { return err } - role.wantAuthnSigned, err = xmlutil.BoolAttr(element, "WantAuthnRequestsSigned") + role.wantAuthnSigned, err = xmlutil.BoolAttr(element, xmlNameWantAuthnRequestsSigned) if err != nil { return err } - role.wantAssertionsSigned, err = xmlutil.BoolAttr(element, "WantAssertionsSigned") + role.wantAssertionsSigned, err = xmlutil.BoolAttr(element, xmlNameWantAssertionsSigned) return err } func readRoleContent(ctx context.Context, role *Role, children []document.Element, o Options) error { + const ( + xmlNameAttribute = "Attribute" + xmlNameAttributeConsumingService = "AttributeConsumingService" + xmlNameAttributeProfile = "AttributeProfile" + ) + endpointCount := 0 for _, child := range children { @@ -754,7 +850,7 @@ func readRoleContent(ctx context.Context, role *Role, children []document.Elemen } switch child.Name().Local { - case "KeyDescriptor": + case xmlNameKeyDescriptor: if err := appendRoleKeys(ctx, role, child, o.MaxKeysPerRole); err != nil { return err } @@ -762,19 +858,16 @@ func readRoleContent(ctx context.Context, role *Role, children []document.Elemen if err := appendRoleNameIDFormat(role, child); err != nil { return err } - case SingleSignOnService, SingleLogoutService, AssertionConsumerService, ArtifactResolutionService, ManageNameIDService, NameIDMappingService: + case SingleSignOnService, SingleLogoutService, AssertionConsumerService, ArtifactResolutionService, ManageNameIDService, NameIDMappingService, AuthnQueryService, AttributeService, AuthzService, AssertionIDRequestService: endpointCount++ if endpointCount > o.MaxEndpointsPerRole { return ErrMetadata } - endpoint, err := parseEndpoint(child, o) - if err != nil { + if err := appendRoleEndpoint(role, child, o); err != nil { return err } - - role.endpoints[child.Name().Local] = append(role.endpoints[child.Name().Local], endpoint) - case lexicon.DSigElementSignature, "Extensions", "Organization", "ContactPerson", "AttributeConsumingService", "AttributeProfile", "Attribute": + case lexicon.DSigElementSignature, xmlNameExtensions, xmlNameOrganization, xmlNameContactPerson, xmlNameAttributeConsumingService, xmlNameAttributeProfile, xmlNameAttribute: // Protected role content is not interpreted. default: return ErrMetadata @@ -784,6 +877,27 @@ func readRoleContent(ctx context.Context, role *Role, children []document.Elemen return nil } +func appendRoleEndpoint(role *Role, child document.Element, o Options) error { + if !validRoleEndpoint(role.kind, child.Name().Local) { + return ErrMetadata + } + + if child.Name().Local == NameIDMappingService || child.Name().Local == SingleSignOnService { + if _, present := child.Attribute(document.Name{Local: xmlNameResponseLocation}); present { + return ErrMetadata + } + } + + endpoint, err := parseEndpoint(child, o) + if err != nil { + return err + } + + role.endpoints[child.Name().Local] = append(role.endpoints[child.Name().Local], endpoint) + + return nil +} + func appendRoleKeys(ctx context.Context, role *Role, descriptor document.Element, limit int) error { if len(role.keys) >= limit { return ErrMetadata @@ -803,13 +917,18 @@ func appendRoleKeys(ctx context.Context, role *Role, descriptor document.Element } func appendRoleNameIDFormat(role *Role, element document.Element) error { - format, err := xmlutil.Simple(element, 2048) + const ( + maximumNameIDFormatBytes = 2048 + maximumNameIDFormats = 32 + ) + + format, err := xmlutil.Simple(element, maximumNameIDFormatBytes) if err != nil || !profile.URI(format) { return ErrMetadata } role.formats = append(role.formats, format) - if len(role.formats) > 32 { + if len(role.formats) > maximumNameIDFormats { return ErrMetadata } @@ -818,33 +937,37 @@ func appendRoleNameIDFormat(role *Role, element document.Element) error { func parseEndpoint(c document.Element, o Options) (Endpoint, error) { indexed := c.Name().Local == AssertionConsumerService || c.Name().Local == ArtifactResolutionService - required := []string{"Binding", "Location"} - optional := []string{"ResponseLocation"} + required := []string{xmlNameBinding, xmlNameLocation} + optional := []string{xmlNameResponseLocation} if indexed { - required = append(required, "index") - optional = append(optional, "isDefault") + required = append(required, xmlNameIndex) + optional = append(optional, xmlNameIsDefault) } - if xmlutil.Attrs(c, required, optional) != nil || !xmlutil.EmptyContent(c) { + if endpointAttributes(c, required, optional) != nil || !xmlutil.EmptyContent(c) { return Endpoint{}, ErrMetadata } - e := Endpoint{Binding: xmlutil.Attr(c, "Binding"), Location: xmlutil.Attr(c, "Location"), ResponseLocation: xmlutil.Attr(c, "ResponseLocation"), Indexed: indexed} + e := Endpoint{Binding: xmlutil.Attr(c, xmlNameBinding), Location: xmlutil.Attr(c, xmlNameLocation), ResponseLocation: xmlutil.Attr(c, xmlNameResponseLocation), Indexed: indexed} + if err := readEndpointExtensions(c, &e); err != nil { + return Endpoint{}, err + } + if !profile.URI(e.Binding) || !profile.Endpoint(e.Location, o.AllowHTTP) || e.ResponseLocation != "" && !profile.Endpoint(e.ResponseLocation, o.AllowHTTP) { return Endpoint{}, ErrMetadata } if indexed { - n, err := profile.Index(xmlutil.Attr(c, "index")) + n, err := profile.Index(xmlutil.Attr(c, xmlNameIndex)) if err != nil { return Endpoint{}, ErrMetadata } e.Index = n - _, e.DefaultSet = c.Attribute(document.Name{Local: "isDefault"}) + _, e.DefaultSet = c.Attribute(document.Name{Local: xmlNameIsDefault}) - e.Default, err = xmlutil.BoolAttr(c, "isDefault") + e.Default, err = xmlutil.BoolAttr(c, xmlNameIsDefault) if err != nil { return Endpoint{}, err } @@ -854,12 +977,17 @@ func parseEndpoint(c document.Element, o Options) (Endpoint, error) { } func parseKey(ctx context.Context, c document.Element) ([]key, error) { - if xmlutil.Attrs(c, nil, []string{"use"}) != nil { + const ( + maximumEncodedCertificateBytes = 48 << 10 + xmlNameEncryptionMethod = "EncryptionMethod" + ) + + if xmlutil.Attrs(c, nil, []string{lexicon.AttrUse}) != nil { return nil, ErrMetadata } - use := xmlutil.Attr(c, "use") - if use != "" && use != "signing" && use != "encryption" { + use := xmlutil.Attr(c, lexicon.AttrUse) + if use != "" && use != keyUseSigning && use != keyUseEncryption { return nil, ErrMetadata } @@ -869,7 +997,7 @@ func parseKey(ctx context.Context, c document.Element) ([]key, error) { } for _, extra := range cs[1:] { - if extra.Name() != xmlutil.Name(Namespace, "EncryptionMethod") { + if extra.Name() != xmlutil.Name(Namespace, xmlNameEncryptionMethod) { return nil, ErrMetadata } } @@ -901,7 +1029,7 @@ func parseKey(ctx context.Context, c document.Element) ([]key, error) { _, node := tree.Unwrap(x) - b, e := tree.DecodeBase64(ctx, node, 48<<10, 32<<10) + b, e := tree.DecodeBase64(ctx, node, maximumEncodedCertificateBytes, maximumCertificateBytes) if e != nil { return nil, errors.Join(ErrMetadata, e) } diff --git a/saml/metadata/metadata_test.go b/saml/metadata/metadata_test.go index 831844e..0d8d0d5 100644 --- a/saml/metadata/metadata_test.go +++ b/saml/metadata/metadata_test.go @@ -346,3 +346,92 @@ func TestSelectACSPriorityAndBinding(t *testing.T) { t.Fatal("missing index accepted", err) } } + +func TestMetadataExpiryAttributesRequireValues(t *testing.T) { + t.Parallel() + at := testcert.Now() + for _, level := range []string{"aggregate", "entity", "role"} { + for _, attribute := range []string{"validUntil", "cacheDuration"} { + for _, value := range []string{"", " ", "valid"} { + t.Run(level+"/"+attribute+"/"+value, func(t *testing.T) { + t.Parallel() + valid := value == "valid" + if valid { + value = "PT60S" + if attribute == "validUntil" { + value = at.Add(time.Minute).Format(time.RFC3339Nano) + } + } + attrs := map[string]string{level: fmt.Sprintf(` %s="%s"`, attribute, value)} + raw := fmt.Sprintf(``, metadata.Namespace, attrs["aggregate"], attrs["entity"], metadata.Protocol, attrs["role"], metadata.HTTPPost) + snapshot, err := metadata.ParseTrusted(t.Context(), []byte(raw), at, metadata.Options{}) + if !valid { + if err == nil || snapshot != nil { + t.Fatal("empty expiry constraint accepted as absent") + } + return + } + if err != nil { + t.Fatal(err) + } + entity, err := snapshot.Find("urn:test:sp", at) + if err != nil { + t.Fatal(err) + } + role, err := entity.Role(metadata.ServiceProvider, at) + if err != nil { + t.Fatal(err) + } else if !role.ExpiresAt().Equal(at.Add(time.Minute)) { + t.Fatalf("effective role expiry = %v, %v; want %v", role.ExpiresAt(), err, at.Add(time.Minute)) + } + if _, err := entity.Role(metadata.ServiceProvider, at.Add(time.Minute)); err == nil { + t.Fatal("expiry boundary was inclusive") + } + }) + } + } + } +} + +func TestMetadataProtocolEnumerationUsesXMLWhitespace(t *testing.T) { + t.Parallel() + + for _, test := range []struct { + name, value string + accept bool + }{ + {"exact", metadata.Protocol, true}, + {"XML space", "urn:unsupported " + metadata.Protocol, true}, + {"XML tab CR LF", "urn:unsupported " + metadata.Protocol, true}, + {"unsupported only", "urn:unsupported", false}, + {"NBSP between", "urn:unsupported " + metadata.Protocol, false}, + {"EMSPACE between", "urn:unsupported " + metadata.Protocol, false}, + {"NBSP prefix", " " + metadata.Protocol, false}, + {"NBSP suffix", metadata.Protocol + " ", false}, + {"opaque NBSP item then XML space", "urn:opaque value " + metadata.Protocol, true}, + } { + t.Run(test.name, func(t *testing.T) { + t.Parallel() + + at := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC) + raw := fmt.Sprintf(``, metadata.Namespace, test.value, metadata.HTTPPost) + snapshot, err := metadata.ParseTrusted(t.Context(), []byte(raw), at, metadata.Options{}) + if !test.accept { + if err == nil || snapshot != nil { + t.Fatal("SAML2 role selected without an exact XML-list item") + } + return + } + if err != nil || snapshot == nil { + t.Fatalf("XML-list supported role rejected: %v", err) + } + entity, err := snapshot.Find("urn:test:protocol-list", at) + if err != nil { + t.Fatal(err) + } + if _, err := entity.Role(metadata.ServiceProvider, at); err != nil { + t.Fatal(err) + } + }) + } +} diff --git a/saml/metadata/shared_constants.go b/saml/metadata/shared_constants.go index b8437a9..984ad9f 100644 --- a/saml/metadata/shared_constants.go +++ b/saml/metadata/shared_constants.go @@ -1,10 +1,37 @@ package metadata +// Decoded DER byte ceiling shared by metadata construction and certificate parsing; metadata trust does not imply PKIX certificate validation. const ( - nameNameIDFormat = "NameIDFormat" + maximumCertificateBytes = 32 << 10 +) + +// Metadata vocabulary local name shared by builder and parser; callers must still check the metadata or extension namespace. +const ( + xmlNameAuthnRequestsSigned = "AuthnRequestsSigned" + xmlNameBinding = "Binding" + xmlNameEntityDescriptor = "EntityDescriptor" + xmlNameEntityID = "entityID" + xmlNameIndex = "index" + xmlNameIsDefault = "isDefault" + xmlNameKeyDescriptor = "KeyDescriptor" + xmlNameLocation = "Location" + xmlNameProtocolSupportEnumeration = "protocolSupportEnumeration" + xmlNameResponseLocation = "ResponseLocation" + xmlNameWantAssertionsSigned = "WantAssertionsSigned" + xmlNameWantAuthnRequestsSigned = "WantAuthnRequestsSigned" +) + +const ( + // Role declaration element in the metadata namespace; supported formats + // are exact URIs, independently of the syntax of an assertion's NameID. + nameNameIDFormat = "NameIDFormat" + // Relative metadata freshness bound, interpreted as the supported XSD + // day/time duration subset rather than as an absolute timestamp. nameCacheDuration = "cacheDuration" ) const ( + // Explicit absolute expiry is presence-sensitive: even the Go zero instant + // is an expiry when the XML attribute is present, not an omitted limit. nameValidUntil = "validUntil" ) diff --git a/saml/proof/proof.go b/saml/proof/proof.go new file mode 100644 index 0000000..d6151a5 --- /dev/null +++ b/saml/proof/proof.go @@ -0,0 +1,186 @@ +// Package proof verifies ECP 2013 proof of possession. A Verified value establishes +// possession of a certificate's key for one immutable SOAP message, not identity, +// certificate trust, SAML validity, freshness or replay protection. +// +// XML proof formats have separate APIs: [SignEnvelopedXML] and +// [VerifyEnvelopedXML] use the declared W3C transform; [SignCS01CompatXML] and +// [VerifyCS01CompatXML] use the explicitly agreed CS01 compatibility form. +// Neither API settles the ambiguous no-Transforms wording in ECP CS01 2.3.11. +package proof + +import ( + "bytes" + "context" + "crypto/x509" + "errors" + "io" + "mime" + "net/http" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/lexicon" + "github.com/skarm/xmlsec/internal/signature" + "github.com/skarm/xmlsec/internal/streamio" + "github.com/skarm/xmlsec/internal/tree" + "github.com/skarm/xmlsec/saml/binding" + "github.com/skarm/xmlsec/saml/internal/xmlutil" + "github.com/skarm/xmlsec/xmldsig" +) + +// Decoded certificate byte ceiling shared by proof construction/parsing; exact certificate bytes identify possession, not a trust chain. +const ( + maximumCertificateBytes = 64 << 10 +) + +// WS-Security wrapper local name shared by signing and decoding; its presence supplies no certificate or identity trust. +const ( + xmlNameSecurity = "Security" +) + +// SecurityNamespace identifies the WS-Security header containing an XML proof. +const SecurityNamespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" + +// ErrProof reports a malformed, mismatched or unverifiable proof of possession. +var ErrProof = errors.New("SAML ECP proof rejected") + +// Options bounds XML processing and restricts proof signature algorithms. +type Options struct { + // SOAP bounds envelope parsing and declares understood SOAP headers. + SOAP binding.SOAPOptions + // Policy restricts the XML proof's signature and reference algorithms. + Policy xmldsig.Policy + // Certificate optionally pins the exact attesting certificate. Without it, + // XML verification obtains a candidate from KeyInfo, without granting trust. + Certificate []byte +} + +// Verified binds a proven certificate to the exact immutable received message. +// Its zero value grants no proof. Applications must independently associate the +// certificate with the principal before issuing an assertion (ECP 2.3.5.1). +type Verified struct { + // message is the exact immutable SOAP message covered by the proof. + message *binding.SOAPMessage + // certificate retains the exact DER certificate whose possession was proven. + certificate []byte + // xml records whether proof was established by an XML signature. + xml bool +} + +// Certificate returns an owned copy of the exact attesting certificate bytes. +func (p *Verified) Certificate() []byte { + if p == nil { + return nil + } + + return bytes.Clone(p.certificate) +} + +// Envelope returns the immutable message authenticated by this proof. +func (p *Verified) Envelope() *document.Document { + if p == nil || p.message == nil { + return nil + } + + return p.message.Envelope() +} + +// Payload returns a context-preserving view of the exact SOAP body message. +func (p *Verified) Payload(ctx context.Context) (*document.Document, error) { + if p == nil || p.message == nil { + return nil, ErrProof + } + + return p.message.Payload(ctx) +} + +// Covers checks the exact SOAP payload snapshot; it never matches by XML ID. +func (p *Verified) Covers(d *document.Document) bool { + if p == nil || p.message == nil || d == nil { + return false + } + + view, err := p.message.Payload(context.Background()) + if err != nil { + return false + } + + return tree.ContextElement(d.Root()).Same(tree.ContextElement(view.Root())) +} + +// XML reports a whole-message XML proof rather than a TLS client proof. +func (p *Verified) XML() bool { return p != nil && p.message != nil && p.xml } + +// Message returns the immutable SOAP message whose key possession was verified. +// Its body still requires independent SAML signature/profile verification. +func (p *Verified) Message() *binding.SOAPMessage { + if p == nil { + return nil + } + + return p.message +} + +// VerifyTLSRequest reads one bounded POST and obtains the certificate from the +// completed server-side TLS handshake. Use the original net/http server request, +// never a synthetic TLS state or a certificate copied from a proxy header. +// RequestClientCert/RequireAnyClientCert can establish possession without PKIX; +// certificate-to-principal association is a separate application decision. +func VerifyTLSRequest(ctx context.Context, r *http.Request, o Options) (*Verified, error) { + const ( + defaultSOAPBytes = 2 << 20 + maximumSOAPBytes = 16 << 20 + ) + + if ctx == nil || r == nil || r.TLS == nil || !r.TLS.HandshakeComplete || len(r.TLS.PeerCertificates) == 0 || r.Method != http.MethodPost || r.Body == nil { + return nil, ErrProof + } + + media, _, err := mime.ParseMediaType(r.Header.Get(lexicon.HeaderContentType)) + if err != nil || media != lexicon.MediaTypeSOAP && media != lexicon.MediaTypePAOS { + return nil, ErrProof + } + + cert, err := certificate(r.TLS.PeerCertificates[0].Raw) + if err != nil || len(o.Certificate) != 0 && !bytes.Equal(o.Certificate, cert.Raw) { + return nil, ErrProof + } + + limit := o.SOAP.MaxBytes + if limit == 0 { + limit = defaultSOAPBytes + } + + if limit < 1 || limit > maximumSOAPBytes { + return nil, ErrProof + } + + raw, err := io.ReadAll(&streamio.Reader{Context: ctx, Source: r.Body, Limit: int64(limit), LimitError: ErrProof}) + if err != nil { + return nil, err + } + + m, err := binding.DecodeSOAP(ctx, raw, o.SOAP) + if err != nil { + return nil, err + } + + return &Verified{message: m, certificate: bytes.Clone(cert.Raw)}, nil +} + +func certificate(raw []byte) (*x509.Certificate, error) { + if len(raw) == 0 || len(raw) > maximumCertificateBytes { + return nil, ErrProof + } + + cert, err := x509.ParseCertificate(raw) + if err != nil || signature.Key(cert.PublicKey) != nil { + return nil, ErrProof + } + + return cert, nil +} + +func decode(ctx context.Context, raw []byte, o Options) (*binding.SOAPMessage, error) { + o.SOAP.Understood = append(append([]document.Name(nil), o.SOAP.Understood...), xmlutil.Name(SecurityNamespace, xmlNameSecurity)) + return binding.DecodeSOAP(ctx, raw, o.SOAP) +} diff --git a/saml/proof/proof_test.go b/saml/proof/proof_test.go new file mode 100644 index 0000000..feb69d1 --- /dev/null +++ b/saml/proof/proof_test.go @@ -0,0 +1,277 @@ +package proof_test + +import ( + "bytes" + "context" + "crypto" + "crypto/fips140" + "crypto/rand" + "crypto/rsa" + "encoding/base64" + "errors" + "math/big" + "strings" + "testing" + + "github.com/skarm/xmlsec/c14n" + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/testutil/testcert" + "github.com/skarm/xmlsec/saml/binding" + "github.com/skarm/xmlsec/saml/metadata" + "github.com/skarm/xmlsec/saml/proof" + "github.com/skarm/xmlsec/saml/protocol" + "github.com/skarm/xmlsec/xmldsig" +) + +func TestWholeSOAPProof(t *testing.T) { + c := testcert.New(t) + request := protocol.AuthnRequest{ID: "_proof", Issuer: "https://sp.test", Destination: "https://idp.test/sso", ACS: "https://sp.test/acs", IssueInstant: testcert.Now()} + key := protocol.SigningKey{Key: c.TSAKey, CertificateDER: c.TSA.Raw, Canonicalization: c14n.Canonical10} + d, err := protocol.SignAuthnRequest(t.Context(), request, key) + if err != nil { + t.Fatal(err) + } + envelope, err := protocol.SignSOAP(t.Context(), d, key) + if err != nil { + t.Fatal(err) + } + for _, method := range []string{c14n.Canonical10, c14n.Canonical10WithComments, c14n.Canonical11, c14n.Canonical11WithComments, c14n.Exclusive, c14n.ExclusiveWithComments} { + t.Run(method, func(t *testing.T) { + signed, err := proof.SignCS01CompatXML(t.Context(), envelope, proof.Signing{Key: c.LeafKey, Certificate: c.Leaf.Raw, Canonicalization: method}) + if err != nil { + t.Fatal(err) + } + raw, err := signed.XML(t.Context()) + if err != nil { + t.Fatal(err) + } + p, err := proof.VerifyCS01CompatXML(t.Context(), raw, proof.Options{Certificate: c.Leaf.Raw}) + if err != nil || !p.XML() || !bytes.Equal(p.Certificate(), c.Leaf.Raw) { + t.Fatalf("proof: %v", err) + } + body, err := p.Payload(t.Context()) + if err != nil || !p.Covers(body) { + t.Fatalf("scope: %v", err) + } + if _, err := protocol.VerifyAuthnRequest(t.Context(), body, []crypto.PublicKey{c.TSAKey.Public()}, protocol.RequestOptions{Issuer: request.Issuer, Destination: request.Destination, At: request.IssueInstant, ACS: []metadata.Endpoint{{Binding: metadata.HTTPPost, Location: request.ACS}}}); err != nil { + t.Fatalf("inner SAML signature: %v", err) + } + // Matching XML IDs or reparsing the same bytes must not transfer proof. + if p.Covers(d) { + t.Fatal("proof transferred to another document") + } + copyCert := p.Certificate() + copyCert[0] ^= 1 + if !bytes.Equal(p.Certificate(), c.Leaf.Raw) { + t.Fatal("mutable certificate escaped") + } + for name, bad := range map[string][]byte{ + "body": bytes.Replace(raw, []byte(`ID="_proof"`), []byte(`ID="_other"`), 1), + "inner signature": bytes.Replace(raw, []byte(base64.StdEncoding.EncodeToString(c.TSA.Raw)), []byte(base64.StdEncoding.EncodeToString(c.Leaf.Raw)), 1), + "header": bytes.Replace(raw, []byte(`soap:mustUnderstand="1"`), []byte(`soap:mustUnderstand="0"`), 1), + "external reference": bytes.Replace(raw, []byte(``), []byte(``), 1), + "transforms": bytes.Replace(raw, []byte(``), []byte(``), 1), + "Object": bytes.Replace(raw, []byte(``), []byte(``), 1), + } { + t.Run(name, func(t *testing.T) { + if _, err := proof.VerifyCS01CompatXML(t.Context(), bad, proof.Options{}); err == nil { + t.Fatal("tampered proof accepted") + } + }) + } + if _, err := proof.VerifyCS01CompatXML(t.Context(), raw, proof.Options{Certificate: c.TSA.Raw}); err == nil { + t.Fatal("wrong certificate pin") + } + if _, err := proof.VerifyCS01CompatXML(t.Context(), raw, proof.Options{SOAP: binding.SOAPOptions{MaxBytes: 100}}); err == nil { + t.Fatal("byte limit ignored") + } + if _, err := proof.VerifyCS01CompatXML(t.Context(), raw, proof.Options{Policy: xmldsig.Policy{MaxCanonicalBytes: 100}}); err == nil { + t.Fatal("canonical byte budget ignored") + } + if _, err := proof.VerifyCS01CompatXML(t.Context(), raw, proof.Options{Policy: xmldsig.Policy{AllowedSignatureMethods: []string{xmldsig.RSASHA256}}}); err == nil { + t.Fatal("algorithm restriction ignored") + } + ctx, cancel := context.WithCancel(t.Context()) + cancel() + if _, err := proof.VerifyCS01CompatXML(ctx, raw, proof.Options{}); err == nil { + t.Fatal("cancellation ignored") + } + // Generic XMLDSig retains its ordinary empty-URI processing. This + // literal ECP wire profile is not a generic enveloped XML signature. + if _, err := xmldsig.Verify(t.Context(), signed, c.LeafKey.Public(), xmldsig.VerifyOptions{}); err == nil { + t.Fatal("generic XMLDSig silently adopted ECP projection") + } + }) + } +} + +func TestProofExclusivePrefixList(t *testing.T) { + c := testcert.New(t) + d, err := document.Parse(t.Context(), []byte(``), document.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + key := proof.Signing{Key: c.LeafKey, Certificate: c.Leaf.Raw, InclusivePrefixes: []string{"q", "#default"}} + signed, err := proof.SignCS01CompatXML(t.Context(), d, key) + if err != nil { + t.Fatal(err) + } + raw, _ := signed.XML(t.Context()) + if _, err := proof.VerifyCS01CompatXML(t.Context(), raw, proof.Options{}); err != nil { + t.Fatal(err) + } + key.InclusivePrefixes = []string{"xmlns"} + if _, err := proof.SignCS01CompatXML(t.Context(), d, key); err == nil { + t.Fatal("invalid namespace prefix accepted") + } +} + +func TestSeparateXMLProofAPIs(t *testing.T) { + c := testcert.New(t) + d, err := document.Parse(t.Context(), []byte(``), document.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + for _, api := range []struct { + name string + sign func(context.Context, *document.Document, proof.Signing) (*document.Document, error) + verify, other func(context.Context, []byte, proof.Options) (*proof.Verified, error) + standard bool + }{ + {"CS01-compatibility", proof.SignCS01CompatXML, proof.VerifyCS01CompatXML, proof.VerifyEnvelopedXML, false}, + {"XMLDSig-enveloped", proof.SignEnvelopedXML, proof.VerifyEnvelopedXML, proof.VerifyCS01CompatXML, true}, + } { + t.Run(api.name, func(t *testing.T) { + signed, err := api.sign(t.Context(), d, proof.Signing{Key: c.LeafKey, Certificate: c.Leaf.Raw}) + if err != nil { + t.Fatal(err) + } + raw, err := signed.XML(t.Context()) + if err != nil { + t.Fatal(err) + } + if _, err := api.verify(t.Context(), raw, proof.Options{}); err != nil { + t.Fatal(err) + } + if _, err := api.other(t.Context(), raw, proof.Options{}); err == nil { + t.Fatal("different XML proof format accepted") + } + signatures := xmldsig.Signatures(signed) + if len(signatures) != 1 { + t.Fatal("proof count") + } + _, err = xmldsig.VerifySignature(t.Context(), signed, signatures[0], c.LeafKey.Public(), xmldsig.SignatureOptions{}) + if (err == nil) != api.standard { + t.Fatalf("ordinary XMLDSig verification: %v, standard format %v", err, api.standard) + } + changed := bytes.ReplaceAll(raw, []byte("AuthnRequest"), []byte("OtherRequest")) + if _, err := api.verify(t.Context(), changed, proof.Options{}); err == nil { + t.Fatal("modified SOAP body accepted") + } + }) + } +} + +func FuzzECPXMLProof(f *testing.F) { + raw := []byte(``) + f.Add(raw) + c := testcert.New(f) + d, err := document.Parse(f.Context(), raw, document.DefaultLimits()) + if err != nil { + f.Fatal(err) + } + for _, sign := range []func(context.Context, *document.Document, proof.Signing) (*document.Document, error){proof.SignCS01CompatXML, proof.SignEnvelopedXML} { + signed, err := sign(f.Context(), d, proof.Signing{Key: c.LeafKey, Certificate: c.Leaf.Raw}) + if err != nil { + f.Fatal(err) + } + valid, err := signed.XML(f.Context()) + if err != nil { + f.Fatal(err) + } + f.Add(valid) + } + f.Fuzz(func(t *testing.T, raw []byte) { + if len(raw) > 8192 { + t.Skip() + } + for _, verify := range []func(context.Context, []byte, proof.Options) (*proof.Verified, error){proof.VerifyCS01CompatXML, proof.VerifyEnvelopedXML} { + _, _ = verify(t.Context(), raw, proof.Options{SOAP: binding.SOAPOptions{MaxBytes: 8192}}) + } + }) +} + +func TestRSAProofAndKeyMismatch(t *testing.T) { + c := testcert.New(t) + rsaKey, err := rsa.GenerateKey(rand.Reader, 2048) + if err != nil { + t.Fatal(err) + } + template := *c.Leaf + template.SerialNumber = big.NewInt(99) + cert := testcert.Issue(t, &template, c.Root, &rsaKey.PublicKey, c.RootKey) + d, err := document.Parse(t.Context(), []byte(``), document.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + signed, err := proof.SignCS01CompatXML(t.Context(), d, proof.Signing{Key: rsaKey, Certificate: cert.Raw}) + if err != nil { + t.Fatal(err) + } + raw, _ := signed.XML(t.Context()) + if !strings.Contains(string(raw), xmldsig.RSASHA256) { + t.Fatal("RSA method") + } + if _, err := proof.VerifyCS01CompatXML(t.Context(), raw, proof.Options{}); err != nil { + t.Fatal(err) + } + if !fips140.Enabled() { + legacy := xmldsig.LegacyVerificationPolicy() + legacy.AllowLegacySigning = true + legacyKey := proof.Signing{Key: rsaKey, Certificate: cert.Raw, Method: xmldsig.RSASHA1, Digest: xmldsig.SHA1, Policy: legacy} + old, err := proof.SignCS01CompatXML(t.Context(), d, legacyKey) + if err != nil { + t.Fatal(err) + } + oldRaw, _ := old.XML(t.Context()) + if _, err := proof.VerifyCS01CompatXML(t.Context(), oldRaw, proof.Options{}); err == nil { + t.Fatal("legacy proof accepted by default") + } + if _, err := proof.VerifyCS01CompatXML(t.Context(), oldRaw, proof.Options{Policy: xmldsig.LegacyVerificationPolicy()}); err != nil { + t.Fatal(err) + } + legacyKey.Policy.AllowLegacySigning = false + if _, err := proof.SignCS01CompatXML(t.Context(), d, legacyKey); err == nil { + t.Fatal("legacy verification policy enabled signing") + } + } + if _, err := proof.SignCS01CompatXML(t.Context(), d, proof.Signing{Key: c.LeafKey, Certificate: cert.Raw}); err == nil { + t.Fatal("mismatched key signed") + } + var zero proof.Verified + if zero.Covers(d) || zero.Envelope() != nil || zero.Certificate() != nil { + t.Fatal("zero proof trusted") + } +} + +func TestXMLProofSigningRejectsEmptyDocument(t *testing.T) { + t.Parallel() + c := testcert.New(t) + for _, api := range []struct { + name string + sign func(context.Context, *document.Document, proof.Signing) (*document.Document, error) + }{ + {"enveloped", proof.SignEnvelopedXML}, + {"CS01 compatibility", proof.SignCS01CompatXML}, + } { + t.Run(api.name, func(t *testing.T) { + t.Parallel() + for _, input := range []*document.Document{nil, {}} { + signed, err := api.sign(t.Context(), input, proof.Signing{Key: c.LeafKey, Certificate: c.Leaf.Raw}) + if signed != nil || !errors.Is(err, proof.ErrProof) { + t.Fatalf("empty document: signed=%v, err=%v; want ErrProof", signed, err) + } + } + }) + } +} diff --git a/saml/proof/xml.go b/saml/proof/xml.go new file mode 100644 index 0000000..80eaf84 --- /dev/null +++ b/saml/proof/xml.go @@ -0,0 +1,595 @@ +package proof + +import ( + "bytes" + "context" + "crypto" + "crypto/ecdsa" + "crypto/fips140" + "crypto/rsa" + "crypto/sha256" + "crypto/sha512" + "crypto/subtle" + "encoding/base64" + "hash" + "slices" + "strings" + + "github.com/skarm/xmlsec/c14n" + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/lexicon" + "github.com/skarm/xmlsec/internal/nilcheck" + "github.com/skarm/xmlsec/internal/transformxml" + "github.com/skarm/xmlsec/internal/tree" + "github.com/skarm/xmlsec/reference" + "github.com/skarm/xmlsec/saml/binding" + "github.com/skarm/xmlsec/saml/internal/xmlutil" + "github.com/skarm/xmlsec/xmldsig" +) + +// Locate/build the SOAP 1.1 security-header container by expanded name; copying other headers does not authenticate them. +const ( + soapHeaderElement = "Header" +) + +// SOAP processing attributes used for the proof security header; the next-actor rule is checked independently of certificate possession. +const ( + soapActorAttribute = "actor" + soapMustUnderstandAttribute = "mustUnderstand" +) + +const ( + soapPrefix = "soap" + algorithmAttribute = lexicon.SecurityAttrAlgorithm +) + +// Signing selects the proof key/certificate and independent XMLDSig algorithms. +type Signing struct { + // Key is the private signing key proving possession of Certificate's public + // key. + Key crypto.Signer + // Certificate contains the exact DER certificate carried by the proof. + Certificate []byte + // Method selects the XMLDSig signature algorithm URI. + Method string + // Canonicalization selects the SignedInfo canonicalization algorithm URI. + Canonicalization string + // Digest selects the reference digest algorithm URI. + Digest string + // Policy restricts the algorithms used to create the proof. + Policy xmldsig.Policy + // InclusivePrefixes applies only to Exclusive C14N of SignedInfo. + InclusivePrefixes []string +} + +// SignEnvelopedXML signs the complete SOAP message using a declared W3C +// enveloped-signature transform. The scope of CS01 2.3.11's no-Transforms wording +// needs clarification; full ECP conformance is not established by this API. +func SignEnvelopedXML(ctx context.Context, envelope *document.Document, key Signing) (*document.Document, error) { + return signXML(ctx, envelope, key, true) +} + +// SignCS01CompatXML omits Reference transforms under the restrictive reading of +// CS01, applying an ECP-specific self-exclusion rule. This is not ordinary W3C +// XMLDSig reference processing and requires an explicitly agreeing peer. +func SignCS01CompatXML(ctx context.Context, envelope *document.Document, key Signing) (*document.Document, error) { + return signXML(ctx, envelope, key, false) +} + +// signXML preserves existing SAML signatures and their namespace context. +func signXML(ctx context.Context, envelope *document.Document, key Signing, enveloped bool) (*document.Document, error) { + const ( + soapEnvelopeElement = "Envelope" + ) + + if ctx == nil || envelope == nil || !envelope.Root().Valid() || nilcheck.IsNil(key.Key) { + return nil, ErrProof + } + + cert, err := certificate(key.Certificate) + if err != nil { + return nil, err + } + + public, ok := key.Key.Public().(interface{ Equal(crypto.PublicKey) bool }) + if !ok || !public.Equal(cert.PublicKey) { + return nil, ErrProof + } + + if key.Method == "" { + switch key.Key.Public().(type) { + case *rsa.PublicKey: + key.Method = xmldsig.RSASHA256 + case *ecdsa.PublicKey: + key.Method = xmldsig.ECDSASHA256 + default: + return nil, ErrProof + } + } + + if key.Canonicalization == "" { + key.Canonicalization = c14n.Exclusive + } + + if key.Digest == "" { + key.Digest = xmldsig.SHA256 + } + + draft, err := tree.Clone(ctx, envelope) + if err != nil { + return nil, err + } + + root := tree.Root(draft) + if root.Name != xmlutil.Name(binding.SOAPNamespace, soapEnvelopeElement) { + return nil, ErrProof + } + + security, err := addSecurity(root) + if err != nil { + return nil, err + } + + sig := xmlutil.Node(lexicon.PrefixDSig, xmlutil.DS, lexicon.DSigElementSignature) + sig.NS = []tree.Namespace{{Prefix: lexicon.PrefixDSig, URI: xmlutil.DS}} + si := xmlutil.Node(lexicon.PrefixDSig, xmlutil.DS, lexicon.DSigElementSignedInfo) + cm := xmlutil.Node(lexicon.PrefixDSig, xmlutil.DS, lexicon.DSigElementCanonicalizationMethod) + xmlutil.Set(cm, algorithmAttribute, key.Canonicalization) + + if len(key.InclusivePrefixes) != 0 { + prefixes := xmlutil.Node(lexicon.PrefixExclusiveC14N, c14n.Exclusive, lexicon.C14NElementInclusiveNamespaces) + prefixes.NS = []tree.Namespace{{Prefix: lexicon.PrefixExclusiveC14N, URI: c14n.Exclusive}} + xmlutil.Set(prefixes, lexicon.C14NAttrPrefixList, strings.Join(key.InclusivePrefixes, " ")) + xmlutil.Add(cm, prefixes) + } + + sm := xmlutil.Node(lexicon.PrefixDSig, xmlutil.DS, lexicon.DSigElementSignatureMethod) + xmlutil.Set(sm, algorithmAttribute, key.Method) + + ref := xmlutil.Node(lexicon.PrefixDSig, xmlutil.DS, lexicon.DSigElementReference) + xmlutil.Set(ref, lexicon.SecurityAttrURI, "") + + if enveloped { + transforms := xmlutil.Node(lexicon.PrefixDSig, xmlutil.DS, lexicon.DSigElementTransforms) + transform := xmlutil.Node(lexicon.PrefixDSig, xmlutil.DS, lexicon.DSigElementTransform) + xmlutil.Set(transform, algorithmAttribute, reference.EnvelopedSignature) + xmlutil.Add(transforms, transform) + xmlutil.Add(ref, transforms) + } + + dm := xmlutil.Node(lexicon.PrefixDSig, xmlutil.DS, lexicon.DSigElementDigestMethod) + xmlutil.Set(dm, algorithmAttribute, key.Digest) + + dv := xmlutil.Node(lexicon.PrefixDSig, xmlutil.DS, lexicon.DSigElementDigestValue) + + xmlutil.Add(ref, dm) + xmlutil.Add(ref, dv) + xmlutil.Add(si, cm) + xmlutil.Add(si, sm) + xmlutil.Add(si, ref) + xmlutil.Add(sig, si) + xmlutil.Add(sig, xmlutil.Node(lexicon.PrefixDSig, xmlutil.DS, lexicon.DSigElementSignatureValue)) + info := xmlutil.Node(lexicon.PrefixDSig, xmlutil.DS, lexicon.DSigElementKeyInfo) + data := xmlutil.Node(lexicon.PrefixDSig, xmlutil.DS, lexicon.DSigElementX509Data) + leaf := xmlutil.Node(lexicon.PrefixDSig, xmlutil.DS, lexicon.DSigElementX509Certificate) + xmlutil.Text(leaf, base64.StdEncoding.EncodeToString(cert.Raw)) + xmlutil.Add(data, leaf) + xmlutil.Add(info, data) + xmlutil.Add(sig, info) + xmlutil.Add(security, sig) + + d, err := tree.Reparse(ctx, draft, document.DefaultLimits()) + if err != nil { + return nil, err + } + + selected, err := proofSignature(d) + if err != nil { + return nil, err + } + + digest, consumed, err := referenceDigest(ctx, d, selected, key.Digest, key.Policy) + if err != nil { + return nil, err + } + + _, sn := tree.Unwrap(selected) + refNode := sn.Children[0].Children[2] + digestValue := refNode.Children[len(refNode.Children)-1] + digestValue.Children = nil + xmlutil.Text(digestValue, base64.StdEncoding.EncodeToString(digest)) + // d is a newly produced private snapshot, never exposed until reparsed. + d, err = tree.Reparse(ctx, d, document.DefaultLimits()) + if err != nil { + return nil, err + } + + selected, err = proofSignature(d) + if err != nil { + return nil, err + } + + canonical, err := signedInfoBytes(ctx, selected.Children()[0], key.Canonicalization, canonicalLimit(key.Policy)-consumed) + if err != nil { + return nil, err + } + + value, err := xmldsig.SignMessageWithParameters(ctx, canonical, key.Key, key.Method, xmldsig.SignatureParameters{}, key.Policy) + if err != nil { + return nil, err + } + + _, sn = tree.Unwrap(selected) + xmlutil.Text(sn.Children[1], base64.StdEncoding.EncodeToString(value)) + + out, err := tree.Reparse(ctx, d, document.DefaultLimits()) + if err != nil { + return nil, err + } + + raw, err := out.XML(ctx) + if err != nil { + return nil, err + } + + _, err = verifyXML(ctx, raw, Options{Certificate: cert.Raw, Policy: key.Policy, SOAP: binding.SOAPOptions{Understood: allHeaderNames(out)}}, enveloped) + if err != nil { + return nil, err + } + + return out, nil +} + +// VerifyEnvelopedXML verifies the whole SOAP message with exactly one declared +// enveloped-signature transform. It never accepts the CS01 compatibility form. +// Certificate association, SAML validation and replay checks remain separate. +func VerifyEnvelopedXML(ctx context.Context, raw []byte, o Options) (*Verified, error) { + return verifyXML(ctx, raw, o, true) +} + +// VerifyCS01CompatXML verifies only CS01's no-Transforms compatibility form, +// applying the explicitly agreed nonstandard self-exclusion rule. It never +// accepts declared transforms or infers trust from KeyInfo. +func VerifyCS01CompatXML(ctx context.Context, raw []byte, o Options) (*Verified, error) { + return verifyXML(ctx, raw, o, false) +} + +func verifyXML(ctx context.Context, raw []byte, o Options, enveloped bool) (*Verified, error) { + const ( + maximumDigestBytes = 128 + maximumSignatureBytes = 4096 + maximumSignatureChildren = 3 + minimumSignatureChildren = 2 + ) + + m, err := decode(ctx, raw, o) + if err != nil { + return nil, err + } + + d := m.Envelope() + + sig, err := proofSignature(d) + if err != nil { + return nil, err + } + + parts, err := xmlutil.Children(sig) + if err != nil || len(parts) < minimumSignatureChildren || len(parts) > maximumSignatureChildren || xmlutil.Attrs(sig, nil, []string{lexicon.SecurityAttrID}) != nil || parts[0].Name() != xmlutil.Name(xmlutil.DS, lexicon.DSigElementSignedInfo) || parts[1].Name() != xmlutil.Name(xmlutil.DS, lexicon.DSigElementSignatureValue) { + return nil, ErrProof + } + + info, err := readSignedInfo(parts[0], enveloped) + if err != nil { + return nil, err + } + + certBytes := bytes.Clone(o.Certificate) + + if len(parts) == maximumSignatureChildren { + provided, err := readCertificate(parts[2]) + if err != nil || len(certBytes) != 0 && !bytes.Equal(certBytes, provided) { + return nil, ErrProof + } + + certBytes = provided + } + + cert, err := certificate(certBytes) + if err != nil { + return nil, err + } + + digest, consumed, err := referenceDigest(ctx, d, sig, info.digestMethod, o.Policy) + if err != nil { + return nil, err + } + + want, err := binary(info.digestValue, maximumDigestBytes) + if err != nil || subtle.ConstantTimeCompare(digest, want) != 1 { + return nil, ErrProof + } + + canonical, err := signedInfoBytes(ctx, parts[0], info.canonicalization, canonicalLimit(o.Policy)-consumed) + if err != nil { + return nil, err + } + + value, err := binary(parts[1], maximumSignatureBytes) + if err != nil { + return nil, err + } + + if err := xmldsig.VerifyMessageWithParameters(ctx, canonical, value, cert.PublicKey, info.method, xmldsig.SignatureParameters{}, o.Policy); err != nil { + return nil, err + } + + return &Verified{message: m, certificate: bytes.Clone(cert.Raw), xml: true}, nil +} + +type signedInfo struct { + canonicalization, method, digestMethod string + digestValue document.Element +} + +func readSignedInfo(e document.Element, enveloped bool) (signedInfo, error) { + const ( + digestReferenceChildCount = 2 + envelopedReferenceChildCount = 3 + signedInfoChildCount = 3 + ) + + si, err := xmlutil.Children(e) + if err != nil || len(si) != signedInfoChildCount || xmlutil.Attrs(e, nil, []string{lexicon.SecurityAttrID}) != nil { + return signedInfo{}, ErrProof + } + + if si[0].Name() != xmlutil.Name(xmlutil.DS, lexicon.DSigElementCanonicalizationMethod) || si[1].Name() != xmlutil.Name(xmlutil.DS, lexicon.DSigElementSignatureMethod) || si[2].Name() != xmlutil.Name(xmlutil.DS, lexicon.DSigElementReference) { + return signedInfo{}, ErrProof + } + + if xmlutil.Attrs(si[1], []string{algorithmAttribute}, nil) != nil || !xmlutil.EmptyContent(si[1]) { + return signedInfo{}, ErrProof + } + + _, method := tree.Unwrap(si[0]) + if _, err := transformxml.ParseCanonical(method, lexicon.DSigElementCanonicalizationMethod); err != nil { + return signedInfo{}, err + } + + ref, err := xmlutil.Children(si[2]) + if err == nil && enveloped { + if len(ref) != envelopedReferenceChildCount || standardEnvelopedTransform(ref[0]) != nil { + return signedInfo{}, ErrProof + } + + ref = ref[1:] + } + + if err != nil || xmlutil.Attrs(si[2], []string{lexicon.SecurityAttrURI}, nil) != nil || xmlutil.Attr(si[2], lexicon.SecurityAttrURI) != "" || len(ref) != digestReferenceChildCount || ref[0].Name() != xmlutil.Name(xmlutil.DS, lexicon.DSigElementDigestMethod) || ref[1].Name() != xmlutil.Name(xmlutil.DS, lexicon.DSigElementDigestValue) || xmlutil.Attrs(ref[0], []string{algorithmAttribute}, nil) != nil || !xmlutil.EmptyContent(ref[0]) { + return signedInfo{}, ErrProof + } + + return signedInfo{canonicalization: xmlutil.Attr(si[0], algorithmAttribute), method: xmlutil.Attr(si[1], algorithmAttribute), digestMethod: xmlutil.Attr(ref[0], algorithmAttribute), digestValue: ref[1]}, nil +} + +func standardEnvelopedTransform(element document.Element) error { + if element.Name() != xmlutil.Name(xmlutil.DS, lexicon.DSigElementTransforms) || xmlutil.Attrs(element, nil, nil) != nil { + return ErrProof + } + + children, err := xmlutil.Children(element) + if err != nil || len(children) != 1 || children[0].Name() != xmlutil.Name(xmlutil.DS, lexicon.DSigElementTransform) || xmlutil.Attrs(children[0], []string{algorithmAttribute}, nil) != nil || xmlutil.Attr(children[0], algorithmAttribute) != reference.EnvelopedSignature || !xmlutil.EmptyContent(children[0]) { + return ErrProof + } + + return nil +} + +func referenceDigest(ctx context.Context, d *document.Document, sig document.Element, algorithm string, p xmldsig.Policy) ([]byte, int, error) { + allowed := p.AllowedDigestMethods + if len(allowed) == 0 { + allowed = xmldsig.DefaultPolicy().AllowedDigestMethods + } + + if !slices.Contains(allowed, algorithm) { + return nil, 0, ErrProof + } + + var h hash.Hash + + switch algorithm { + case xmldsig.SHA1: + // Only the explicit digest allowlist reaches this compatibility case. + if fips140.Enabled() || !crypto.SHA1.Available() { + return nil, 0, ErrProof + } + + h = crypto.SHA1.New() + case xmldsig.SHA256: + h = sha256.New() + case xmldsig.SHA384: + h = sha512.New384() + case xmldsig.SHA512: + h = sha512.New() + default: + return nil, 0, ErrProof + } + + resolver, err := reference.NewSameDocumentResolver(ctx, d, document.Name{Local: lexicon.SAMLAttrID}) + if err != nil { + return nil, 0, err + } + + resolved, err := resolver.Resolve(ctx, "") + if err != nil { + return nil, 0, err + } + // Both APIs exclude only the proof Signature, retaining nested SAML signatures. + // The enveloped API declares this transform; CS01 compatibility agrees it out + // of band. Empty URI still removes comments. + report, err := reference.Write(ctx, h, resolved.Data(), []reference.Transform{{Algorithm: reference.EnvelopedSignature}}, reference.Options{Signature: sig, MaxOutputBytes: canonicalLimit(p), MaxProducedBytes: canonicalLimit(p)}) + if err != nil { + return nil, 0, err + } + + return h.Sum(nil), report.Size(), nil +} + +func canonicalLimit(p xmldsig.Policy) int { + const ( + maximumCanonicalBytes = 32 << 20 + ) + + if p.MaxCanonicalBytes > 0 { + return min(p.MaxCanonicalBytes, maximumCanonicalBytes) + } + + return maximumCanonicalBytes +} + +func signedInfoBytes(ctx context.Context, si document.Element, algorithm string, maximum int) ([]byte, error) { + if maximum < 1 { + return nil, ErrProof + } + + _, cm := tree.Unwrap(si.Children()[0]) + + method, err := transformxml.ParseCanonical(cm, lexicon.DSigElementCanonicalizationMethod) + if err != nil || method.Algorithm != algorithm { + return nil, ErrProof + } + + var out bytes.Buffer + + err = c14n.WriteElement(ctx, &out, si, c14n.Options{Algorithm: algorithm, InclusivePrefixes: method.InclusivePrefixes, MaxOutputBytes: maximum}) + + return out.Bytes(), err +} + +func readCertificate(info document.Element) ([]byte, error) { + if info.Name() != xmlutil.Name(xmlutil.DS, lexicon.DSigElementKeyInfo) || xmlutil.Attrs(info, nil, []string{lexicon.SecurityAttrID}) != nil { + return nil, ErrProof + } + + cs, err := xmlutil.Children(info) + if err != nil || len(cs) != 1 || cs[0].Name() != xmlutil.Name(xmlutil.DS, lexicon.DSigElementX509Data) || xmlutil.Attrs(cs[0], nil, nil) != nil { + return nil, ErrProof + } + + data, err := xmlutil.Children(cs[0]) + if err != nil || len(data) != 1 || data[0].Name() != xmlutil.Name(xmlutil.DS, lexicon.DSigElementX509Certificate) { + return nil, ErrProof + } + + return binary(data[0], maximumCertificateBytes) +} + +func binary(e document.Element, maximum int) ([]byte, error) { + text, err := xmlutil.Simple(e, 2*maximum) + if err != nil { + return nil, err + } + + text = strings.Map(func(r rune) rune { + if r == ' ' || r == '\t' || r == '\r' || r == '\n' { + return -1 + } + + return r + }, text) + + value, err := base64.StdEncoding.Strict().DecodeString(text) + if err != nil || len(value) > maximum { + return nil, ErrProof + } + + return value, nil +} + +func addSecurity(root *tree.Node) (*tree.Node, error) { + const ( + prefixWSSE = "wsse" + soapBooleanTrue = "1" + ) + + var header *tree.Node + + for _, c := range root.Children { + if c.Kind == tree.ElementNode && c.Name == xmlutil.Name(binding.SOAPNamespace, soapHeaderElement) { + header = c + } + } + + if header == nil { + header = xmlutil.Node(root.Prefix, binding.SOAPNamespace, soapHeaderElement) + header.Parent = root + root.Children = append([]*tree.Node{header}, root.Children...) + } + + for _, c := range header.Children { + if c.Name == xmlutil.Name(SecurityNamespace, xmlNameSecurity) { + return nil, ErrProof + } + } + + security := xmlutil.Node(prefixWSSE, SecurityNamespace, xmlNameSecurity) + security.NS = []tree.Namespace{{Prefix: prefixWSSE, URI: SecurityNamespace}, {Prefix: soapPrefix, URI: binding.SOAPNamespace}} + security.Attrs = []tree.Attribute{{Name: xmlutil.Name(binding.SOAPNamespace, soapActorAttribute), Prefix: soapPrefix, Value: binding.SOAPNext}, {Name: xmlutil.Name(binding.SOAPNamespace, soapMustUnderstandAttribute), Prefix: soapPrefix, Value: soapBooleanTrue}} + xmlutil.Add(header, security) + + return security, nil +} + +func proofSignature(d *document.Document) (document.Element, error) { + var selected document.Element + + for _, header := range d.Root().Children() { + if header.Name() != xmlutil.Name(binding.SOAPNamespace, soapHeaderElement) { + continue + } + + for _, security := range header.Children() { + if security.Name() != xmlutil.Name(SecurityNamespace, xmlNameSecurity) { + continue + } + + if selected.Valid() { + return document.Element{}, ErrProof + } + + for _, a := range security.Attributes() { + if a.Name.Space != binding.SOAPNamespace || a.Name.Local == soapActorAttribute && a.Value != binding.SOAPNext && a.Value != "" || a.Name.Local != soapActorAttribute && a.Name.Local != soapMustUnderstandAttribute { + return document.Element{}, ErrProof + } + } + + cs, err := xmlutil.Children(security) + if err != nil || len(cs) != 1 || cs[0].Name() != xmlutil.Name(xmlutil.DS, lexicon.DSigElementSignature) { + return document.Element{}, ErrProof + } + + selected = cs[0] + } + } + + if !selected.Valid() { + return document.Element{}, ErrProof + } + + return selected, nil +} + +func allHeaderNames(d *document.Document) []document.Name { + var out []document.Name + + for _, h := range d.Root().Children() { + if h.Name() == xmlutil.Name(binding.SOAPNamespace, soapHeaderElement) { + for _, c := range h.Children() { + out = append(out, c.Name()) + } + } + } + + return out +} diff --git a/saml/protocol/channel.go b/saml/protocol/channel.go new file mode 100644 index 0000000..14b4fa8 --- /dev/null +++ b/saml/protocol/channel.go @@ -0,0 +1,206 @@ +package protocol + +import ( + "bytes" + "encoding/base64" + "strings" + + "github.com/skarm/xmlsec/internal/lexicon" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/tree" + "github.com/skarm/xmlsec/saml/internal/xmlutil" +) + +// Local SAML vocabulary tokens shared by protocol producers and parsers. Each call site still verifies the assertion/protocol namespace or resolves the QName prefix. +const ( + xmlNameChannelBindings = "ChannelBindings" +) + +// The extension's Type attribute is unqualified; it does not name an XML Schema +// instance type or an XML Encryption object type. +const ( + channelTypeAttribute = "Type" +) + +// Channel bindings have fixed profile ceilings for alternatives and decoded +// transport bytes; these are not configurable zero-value defaults. +const ( + maximumChannelBindings = 8 + maximumChannelDataBytes = 4096 +) + +// ChannelBindingNamespace identifies the OASIS Channel Binding Extensions 1.0. +const ChannelBindingNamespace = lexicon.NamespaceSAMLChannelBinding + +// ChannelBinding carries raw channel bytes, not their base64 representation. +// Empty Data is a verification signal in Advice; requests require actual data. +// The application must obtain these bytes from its authenticated transport. +type ChannelBinding struct { + // Type identifies the channel binding method. Empty requires an out-of-band + // agreement. + Type string + // Data contains raw transport-derived binding bytes. Empty is allowed only as + // an Advice verification signal. + Data []byte +} + +// CloneChannelBindings returns independently owned channel binding data. +func CloneChannelBindings(in []ChannelBinding) []ChannelBinding { + out := make([]ChannelBinding, len(in)) + for i, b := range in { + out[i] = ChannelBinding{Type: strings.Clone(b.Type), Data: bytes.Clone(b.Data)} + } + + return out +} + +// ValidateChannelBindings bounds a set and rejects ambiguous duplicate types. +// An empty Type is allowed when its meaning is established out of band. +func ValidateChannelBindings(in []ChannelBinding, allowEmpty bool) error { + const ( + maximumChannelTypeBytes = 256 + ) + + if len(in) > maximumChannelBindings { + return ErrProfile + } + + seen := map[string]bool{} + for _, b := range in { + if len(b.Type) > maximumChannelTypeBytes || strings.ContainsAny(b.Type, lexicon.XMLWhitespace) || seen[b.Type] || len(b.Data) > maximumChannelDataBytes || !allowEmpty && len(b.Data) == 0 { + return ErrProfile + } + + seen[b.Type] = true + } + + return nil +} + +func channelNode(b ChannelBinding) *tree.Node { + const ( + prefixChannelBinding = "cb" + ) + + n := xmlutil.Node(prefixChannelBinding, ChannelBindingNamespace, xmlNameChannelBindings) + + n.NS = []tree.Namespace{{Prefix: prefixChannelBinding, URI: ChannelBindingNamespace}} + if b.Type != "" { + xmlutil.Set(n, channelTypeAttribute, b.Type) + } + + if len(b.Data) != 0 { + xmlutil.Text(n, base64.StdEncoding.EncodeToString(b.Data)) + } + + return n +} + +func channelContainer(local string, values []ChannelBinding, allowEmpty bool) (*tree.Node, error) { + if err := ValidateChannelBindings(values, allowEmpty); err != nil { + return nil, err + } + + ns, prefix := ProtocolNamespace, prefixProtocol + if local == xmlNameAdvice { + ns, prefix = AssertionNamespace, prefixAssertion + } + + n := xmlutil.Node(prefix, ns, local) + for _, b := range values { + xmlutil.Add(n, channelNode(b)) + } + + return n, nil +} + +func appendChannelContainer(parent *tree.Node, local string, values []ChannelBinding, allowEmpty bool) error { + if len(values) == 0 { + return nil + } + + container, err := channelContainer(local, values, allowEmpty) + if err != nil { + return err + } + + xmlutil.Add(parent, container) + + return nil +} + +func readChannelContainer(e document.Element, allowEmpty bool) ([]ChannelBinding, error) { + const ( + maximumEncodedChannelDataBytes = 8192 + ) + + if xmlutil.Attrs(e, nil, nil) != nil { + return nil, ErrProfile + } + + children, err := xmlutil.Children(e) + if err != nil || len(children) == 0 || len(children) > maximumChannelBindings { + return nil, ErrProfile + } + + var out []ChannelBinding + + for _, c := range children { + if c.Name() != xmlutil.Name(ChannelBindingNamespace, xmlNameChannelBindings) || xmlutil.Attrs(c, nil, []string{channelTypeAttribute}) != nil { + return nil, ErrProfile + } + + value, err := xmlutil.SimpleText(c, maximumEncodedChannelDataBytes) + if err != nil { + return nil, err + } + + data, err := decodeChannelData(value) + if err != nil { + return nil, err + } + + out = append(out, ChannelBinding{Type: xmlutil.Attr(c, channelTypeAttribute), Data: data}) + } + + return out, ValidateChannelBindings(out, allowEmpty) +} + +func decodeChannelData(value string) ([]byte, error) { + // XML Schema base64Binary admits XML whitespace, including spaces and tabs. + value = strings.Map(func(r rune) rune { + if r == ' ' || r == '\t' || r == '\r' || r == '\n' { + return -1 + } + + return r + }, value) + + data, err := base64.StdEncoding.Strict().DecodeString(value) + if err != nil || len(data) > maximumChannelDataBytes { + return nil, ErrProfile + } + + return data, nil +} + +func requireChannelAdvice(actual, expected []ChannelBinding) error { + if err := ValidateChannelBindings(expected, false); err != nil { + return err + } + + for _, want := range expected { + for _, got := range actual { + if (got.Type == want.Type || got.Type == "") && (len(got.Data) == 0 || bytes.Equal(got.Data, want.Data)) { + return nil + } + } + } + + if len(expected) != 0 { + return ErrConditions + } + + return nil +} diff --git a/saml/protocol/common.go b/saml/protocol/common.go index ff415f9..695213f 100644 --- a/saml/protocol/common.go +++ b/saml/protocol/common.go @@ -11,6 +11,7 @@ import ( "time" "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/nilcheck" "github.com/skarm/xmlsec/internal/signature" "github.com/skarm/xmlsec/saml/internal/profile" @@ -32,6 +33,9 @@ const ( NameIDPersistent = "urn:oasis:names:tc:SAML:2.0:nameid-format:persistent" // NameIDTransient is the transient SAML NameID format URI. NameIDTransient = "urn:oasis:names:tc:SAML:2.0:nameid-format:transient" + // NameIDEncrypted is a NameIDPolicy request for EncryptedID, not a format + // of the underlying NameID. SAML Core 3.4.1.1 permits any supported inner format. + NameIDEncrypted = "urn:oasis:names:tc:SAML:2.0:nameid-format:encrypted" // AttributeUnspecified is the SAML unspecified attribute name format URI. AttributeUnspecified = "urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified" // Success is the SAML successful response status URI. @@ -79,32 +83,73 @@ type SigningKey struct { // SignatureMethod selects the XML signature algorithm. Empty selects RSA-SHA256 // for RSA keys or ECDSA-SHA256 for ECDSA keys. SignatureMethod string + // DigestMethod selects the reference digest. Empty uses SHA-256. SHA-1 + // requires an explicit legacy digest allowlist. + DigestMethod string + // Canonicalization selects Exclusive C14N (default) or Canonical XML 1.0 + // without comments, the original SAML mandatory baseline. + Canonicalization string + // InclusivePrefixes retains QName namespace bindings under Exclusive C14N. + InclusivePrefixes []string + // PreserveKeys authorizes completed nested signatures when forwarding + // assertions or Evidence from another signer. The parent XML object is a + // lookup hint, never a source of trust. Nil preserves only this signer's key. + PreserveKeys func(context.Context, document.Element) ([]crypto.PublicKey, error) + // Secret explicitly selects a shared-secret MAC at protocol level. It is + // mutually exclusive with Key and CertificateDER and requires HMAC policy. + // High-level SP/IdP metadata workflows use public-key credentials. + Secret *SharedSecret // Policy sets signature algorithms and key limits. Zero selects xmldsig.DefaultPolicy. Policy xmldsig.Policy } +// SharedSecret is an immutable, explicitly selected protocol-level SAML MAC +// credential. Every holder can produce a valid MAC. It is not a public key. +// Supply its value in the trusted verification-key list only for its peer. +type SharedSecret = xmlsig.SharedSecret + +// NewSharedSecret copies an application-provisioned secret. Secrets must contain +// at least 32 unpredictable bytes and must never be taken from received XML. +func NewSharedSecret(secret []byte) (SharedSecret, error) { + return xmlsig.NewSharedSecret(secret) +} + func normalize(l Limits) (Limits, error) { + const ( + defaultAssertionLifetime = 10 * time.Minute + defaultAttributeLimit = 64 + defaultAttributeValueBytes = 4096 + defaultMessageAge = 5 * time.Minute + defaultValuesPerAttributeLimit = 32 + maximumAssertionLifetime = time.Hour + maximumAttributeLimit = 512 + maximumAttributeValueBytes = 64 << 10 + maximumClockSkew = 5 * time.Minute + maximumMessageAge = time.Hour + maximumValuesPerAttributeLimit = 128 + ) + if l.MaxMessageAge == 0 { - l.MaxMessageAge = 5 * time.Minute + l.MaxMessageAge = defaultMessageAge } if l.MaxAssertionLifetime == 0 { - l.MaxAssertionLifetime = 10 * time.Minute + l.MaxAssertionLifetime = defaultAssertionLifetime } if l.MaxAttributes == 0 { - l.MaxAttributes = 64 + l.MaxAttributes = defaultAttributeLimit } if l.MaxValuesPerAttribute == 0 { - l.MaxValuesPerAttribute = 32 + l.MaxValuesPerAttribute = defaultValuesPerAttributeLimit } if l.MaxValueBytes == 0 { - l.MaxValueBytes = 4096 + l.MaxValueBytes = defaultAttributeValueBytes } - if l.MaxMessageAge <= 0 || l.MaxMessageAge > time.Hour || l.MaxAssertionLifetime <= 0 || l.MaxAssertionLifetime > time.Hour || l.ClockSkew < 0 || l.ClockSkew > 5*time.Minute || l.MaxAttributes < 1 || l.MaxAttributes > 512 || l.MaxValuesPerAttribute < 1 || l.MaxValuesPerAttribute > 128 || l.MaxValueBytes < 1 || l.MaxValueBytes > 64<<10 { + if l.MaxMessageAge <= 0 || l.MaxMessageAge > maximumMessageAge || l.MaxAssertionLifetime <= 0 || l.MaxAssertionLifetime > maximumAssertionLifetime || l.ClockSkew < 0 || l.ClockSkew > maximumClockSkew || l.MaxAttributes < 1 || l.MaxAttributes > maximumAttributeLimit || l.MaxValuesPerAttribute < 1 || l.MaxValuesPerAttribute > maximumValuesPerAttributeLimit || l.MaxValueBytes < 1 || l.MaxValueBytes > maximumAttributeValueBytes { return l, ErrProfile } @@ -112,7 +157,7 @@ func normalize(l Limits) (Limits, error) { } func header(e document.Element, local string, at time.Time, l Limits) (string, time.Time, error) { - if e.Name() != xmlutil.Name(ProtocolNamespace, local) || xmlutil.Attr(e, nameVersion) != version20 || !profile.ID(xmlutil.Attr(e, "ID")) { + if e.Name() != xmlutil.Name(ProtocolNamespace, local) || xmlutil.Attr(e, nameVersion) != version20 || !profile.ID(xmlutil.Attr(e, lexicon.SAMLAttrID)) { return "", time.Time{}, ErrProfile } @@ -121,15 +166,23 @@ func header(e document.Element, local string, at time.Time, l Limits) (string, t return "", time.Time{}, ErrConditions } - return xmlutil.Attr(e, "ID"), t, nil + return xmlutil.Attr(e, lexicon.SAMLAttrID), t, nil } func issuer(e document.Element, expected string) error { - if e.Name() != xmlutil.Name(AssertionNamespace, "Issuer") { + if e.Name() != xmlutil.Name(AssertionNamespace, xmlNameIssuer) { return ErrProfile } - s, er := xmlutil.Simple(e, 2048) + if xmlutil.Attrs(e, nil, []string{nameFormat}) != nil { + return ErrProfile + } + + if format := xmlutil.Attr(e, nameFormat); format != "" && format != lexicon.SAMLNameIDFormatEntity { + return ErrProfile + } + + s, er := xmlutil.SimpleText(e, maximumURITextBytes) if er != nil || s != expected { return ErrProfile } @@ -166,6 +219,19 @@ func timeWindow(start, end, at time.Time, l Limits) error { } func signing(k SigningKey) (xmlsig.SigningKey, error) { + if k.Secret != nil { + if !nilcheck.IsNil(k.Key) || len(k.CertificateDER) != 0 { + return xmlsig.SigningKey{}, ErrSignature + } + + method := k.SignatureMethod + if method == "" { + method = xmldsig.HMACSHA256 + } + + return xmlsig.SigningKey{Secret: k.Secret, Method: method, DigestMethod: k.DigestMethod, Canonicalization: k.Canonicalization, InclusivePrefixes: slices.Clone(k.InclusivePrefixes), PreserveKeys: k.PreserveKeys, Policy: k.Policy}, nil + } + if nilcheck.IsNil(k.Key) { return xmlsig.SigningKey{}, ErrSignature } @@ -186,7 +252,15 @@ func signing(k SigningKey) (xmlsig.SigningKey, error) { } } - return xmlsig.SigningKey{Key: k.Key, CertificateDER: k.CertificateDER, Method: method, Policy: k.Policy}, nil + return xmlsig.SigningKey{Key: k.Key, CertificateDER: k.CertificateDER, Method: method, DigestMethod: k.DigestMethod, Canonicalization: k.Canonicalization, InclusivePrefixes: slices.Clone(k.InclusivePrefixes), PreserveKeys: k.PreserveKeys, Policy: k.Policy}, nil +} + +func verificationKey(k SigningKey) crypto.PublicKey { + if k.Secret != nil { + return *k.Secret + } + + return k.Key.Public() } func signingCount(d *document.Document, n int) error { diff --git a/saml/protocol/control.go b/saml/protocol/control.go new file mode 100644 index 0000000..1bca6ff --- /dev/null +++ b/saml/protocol/control.go @@ -0,0 +1,424 @@ +package protocol + +import ( + "context" + "crypto" + "net/url" + "slices" + "strings" + "time" + + "github.com/skarm/xmlsec/internal/lexicon" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/saml/binding" + "github.com/skarm/xmlsec/saml/internal/profile" + "github.com/skarm/xmlsec/saml/internal/xmlutil" +) + +// Local SAML vocabulary tokens shared by protocol producers and parsers. Each call site still verifies the assertion/protocol namespace or resolves the QName prefix. +const ( + xmlNameArtifact = "Artifact" + xmlNameReason = "Reason" + xmlNameTerminate = "Terminate" +) + +// Control-message text is bounded in UTF-8 bytes in both producers and parsers. +// Equal ceilings remain independent for artifacts, identifier updates and sessions. +const ( + maximumArtifactTextBytes = 4096 + maximumNewIdentifierBytes = 4096 + maximumSessionIndexBytes = 4096 +) + +// LogoutRequest terminates the named sessions. With no SessionIndexes all +// sessions for this provider and identifier are selected (Core 3.7.3.1). +type LogoutRequest struct { + MessageHeader + // NameID identifies the subject whose peer sessions are selected. + NameID NameIdentifier + // SessionIndexes selects exact peer session indices. Empty selects all sessions + // for this provider and identifier. + SessionIndexes []string + // Reason is an optional URI describing the logout reason. + Reason string + // NotOnOrAfter is the optional exclusive request expiration time. + NotOnOrAfter time.Time + // Encryption, when set, must select EncryptNameID. + Encryption *EncryptionOptions +} + +// VerifiedLogoutRequest contains an authenticated request, never caller-supplied +// claims. Replay protection and application session termination remain necessary. +type VerifiedLogoutRequest struct{ request LogoutRequest } + +// Request returns an owned copy of the authenticated plaintext request. +func (r *VerifiedLogoutRequest) Request() LogoutRequest { + s := r.request + s.SessionIndexes = slices.Clone(s.SessionIndexes) + + return s +} + +// ManageNameIDRequest changes an identifier value or ends its use between two +// providers. Exactly one of NewID and Terminate must be selected. For an SP +// requester NewID changes SPProvidedID; for an IdP it changes NameID content. +type ManageNameIDRequest struct { + MessageHeader + // NameID is the current complete identifier binding. + NameID NameIdentifier + // NewID is the replacement NameID value for an IdP requester or SPProvidedID + // for an SP requester. It must be empty when Terminate is true. + NewID string + // Terminate ends the identifier binding instead of assigning NewID. + Terminate bool + // Encryption encrypts both the existing NameID and NewID, when present. + Encryption *EncryptionOptions +} + +// VerifiedManageNameIDRequest contains an authenticated identifier change. +type VerifiedManageNameIDRequest struct{ request ManageNameIDRequest } + +// Request returns the authenticated plaintext request. +func (r *VerifiedManageNameIDRequest) Request() ManageNameIDRequest { return r.request } + +// ArtifactResolve requests a protocol message represented by an opaque artifact. +type ArtifactResolve struct { + MessageHeader + // Artifact is the base64-encoded artifact to resolve. + Artifact string +} + +// VerifiedArtifactResolve contains an authenticated artifact resolution request. +type VerifiedArtifactResolve struct{ request ArtifactResolve } + +// Request returns the authenticated request. +func (r *VerifiedArtifactResolve) Request() ArtifactResolve { return r.request } + +// SignLogoutRequest signs a logout request, encrypting its identifier first when +// requested. Session order is retained and duplicate indices are rejected. +func SignLogoutRequest(ctx context.Context, s LogoutRequest, key SigningKey) (*document.Document, error) { + const ( + maximumLogoutSessionIndexes = 128 + ) + + if len(s.SessionIndexes) > maximumLogoutSessionIndexes || !logoutReason(s.Reason) || !s.NotOnOrAfter.IsZero() && !s.IssueInstant.Before(s.NotOnOrAfter) { + return nil, ErrProfile + } + + n, err := exchangeRoot(s.MessageHeader, lexicon.SAMLElementLogoutRequest) + if err != nil { + return nil, err + } + + if s.Reason != "" { + xmlutil.Set(n, xmlNameReason, s.Reason) + } + + if !s.NotOnOrAfter.IsZero() { + xmlutil.Set(n, nameNotOnOrAfter, profile.Time(s.NotOnOrAfter)) + } + + id, err := nameIDNode(s.NameID) + if err != nil { + return nil, err + } + + xmlutil.Add(n, id) + + seen := map[string]bool{} + for _, index := range s.SessionIndexes { + if index == "" || len(index) > maximumSessionIndexBytes || seen[index] { + return nil, ErrProfile + } + + seen[index] = true + c := xmlutil.Node(prefixProtocol, ProtocolNamespace, xmlNameSessionIndex) + xmlutil.Text(c, index) + xmlutil.Add(n, c) + } + + return signExchange(ctx, n, key, s.Encryption) +} + +// VerifyLogoutRequest authenticates and validates a logout message before any +// encrypted identifier is processed or application session is invalidated. +func VerifyLogoutRequest(ctx context.Context, d *document.Document, keys []crypto.PublicKey, o ExchangeOptions) (*VerifiedLogoutRequest, error) { + const ( + maximumLogoutPayloadChildren = 129 + ) + + h, cs, err := verifyExchange(ctx, d, lexicon.SAMLElementLogoutRequest, keys, o, []string{xmlNameReason, nameNotOnOrAfter}, false) + if err != nil { + return nil, err + } + + s := LogoutRequest{MessageHeader: h, Reason: xmlutil.Attr(d.Root(), xmlNameReason)} + if !logoutReason(s.Reason) { + return nil, ErrProfile + } + + if value, present := d.Root().Attribute(document.Name{Local: nameNotOnOrAfter}); present { + s.NotOnOrAfter, err = profile.ParseTime(value) + if err != nil || !h.IssueInstant.Before(s.NotOnOrAfter) || !o.At.Add(-o.Limits.ClockSkew).Before(s.NotOnOrAfter) { + return nil, ErrConditions + } + } + + if len(cs) < 1 || len(cs) > maximumLogoutPayloadChildren { + return nil, ErrProfile + } + + d, err = decryptControl(ctx, d, cs, o.Decryption) + if err != nil { + return nil, err + } + + cs, err = controlChildren(d, o.redirect) + if err != nil { + return nil, err + } + + s.NameID, err = readNameID(cs[0]) + if err != nil { + return nil, err + } + + seen := map[string]bool{} + + for _, c := range cs[1:] { + if c.Name() != xmlutil.Name(ProtocolNamespace, xmlNameSessionIndex) { + return nil, ErrProfile + } + + v, err := xmlutil.Simple(c, maximumSessionIndexBytes) + if err != nil || v == "" || seen[v] { + return nil, ErrProfile + } + + seen[v] = true + s.SessionIndexes = append(s.SessionIndexes, v) + } + + return &VerifiedLogoutRequest{request: s}, ctx.Err() +} + +// VerifyRedirectLogoutRequest validates a signed HTTP-Redirect logout request. +func VerifyRedirectLogoutRequest(ctx context.Context, query string, keys []crypto.PublicKey, o ExchangeOptions, limits binding.Options) (*VerifiedLogoutRequest, *string, error) { + d, relay, err := exchangeRedirect(ctx, query, keys, o, limits, binding.Request) + if err != nil { + return nil, nil, err + } + + o.redirect = true + r, err := VerifyLogoutRequest(ctx, d, nil, o) + + return r, relay, err +} + +// SignManageNameIDRequest signs the identifier update or termination. +func SignManageNameIDRequest(ctx context.Context, s ManageNameIDRequest, key SigningKey) (*document.Document, error) { + if s.Terminate == (s.NewID != "") || len(s.NewID) > maximumNewIdentifierBytes || s.NameID.Format == NameIDTransient { + return nil, ErrProfile + } + + n, err := exchangeRoot(s.MessageHeader, lexicon.SAMLElementManageNameIDRequest) + if err != nil { + return nil, err + } + + id, err := nameIDNode(s.NameID) + if err != nil { + return nil, err + } + + xmlutil.Add(n, id) + + kind := xmlNameNewID + if s.Terminate { + kind = xmlNameTerminate + } + + c := xmlutil.Node(prefixProtocol, ProtocolNamespace, kind) + if !s.Terminate { + xmlutil.Text(c, s.NewID) + } + + xmlutil.Add(n, c) + + return signExchange(ctx, n, key, s.Encryption) +} + +// VerifyManageNameIDRequest authenticates the request before decrypting names. +func VerifyManageNameIDRequest(ctx context.Context, d *document.Document, keys []crypto.PublicKey, o ExchangeOptions) (*VerifiedManageNameIDRequest, error) { + const ( + managePayloadChildren = 2 + ) + + h, cs, err := verifyExchange(ctx, d, lexicon.SAMLElementManageNameIDRequest, keys, o, nil, false) + if err != nil { + return nil, err + } + + if len(cs) != managePayloadChildren { + return nil, ErrProfile + } + + d, err = decryptControl(ctx, d, cs, o.Decryption) + if err != nil { + return nil, err + } + + cs, err = controlChildren(d, o.redirect) + if err != nil { + return nil, err + } + + s := ManageNameIDRequest{MessageHeader: h} + + s.NameID, err = readNameID(cs[0]) + if err != nil { + return nil, err + } + + if s.NameID.Format == NameIDTransient { + return nil, ErrProfile + } + + switch cs[1].Name() { + case xmlutil.Name(ProtocolNamespace, xmlNameNewID): + s.NewID, err = xmlutil.Simple(cs[1], maximumNewIdentifierBytes) + if err != nil || s.NewID == "" { + return nil, ErrProfile + } + case xmlutil.Name(ProtocolNamespace, xmlNameTerminate): + if xmlutil.Attrs(cs[1], nil, nil) != nil || !xmlutil.EmptyContent(cs[1]) { + return nil, ErrProfile + } + + s.Terminate = true + default: + return nil, ErrProfile + } + + return &VerifiedManageNameIDRequest{request: s}, ctx.Err() +} + +// VerifyRedirectManageNameIDRequest validates the exact Redirect signature. +func VerifyRedirectManageNameIDRequest(ctx context.Context, query string, keys []crypto.PublicKey, o ExchangeOptions, limits binding.Options) (*VerifiedManageNameIDRequest, *string, error) { + d, relay, err := exchangeRedirect(ctx, query, keys, o, limits, binding.Request) + if err != nil { + return nil, nil, err + } + + o.redirect = true + r, err := VerifyManageNameIDRequest(ctx, d, nil, o) + + return r, relay, err +} + +// SignArtifactResolve signs a resolution request. Artifact syntax is validated by +// the selected artifact binding; Core permits artifact formats defined elsewhere. +func SignArtifactResolve(ctx context.Context, s ArtifactResolve, key SigningKey) (*document.Document, error) { + if len(s.Artifact) > maximumArtifactTextBytes { + return nil, ErrProfile + } + + n, err := exchangeRoot(s.MessageHeader, lexicon.SAMLElementArtifactResolve) + if err != nil { + return nil, err + } + + c := xmlutil.Node(prefixProtocol, ProtocolNamespace, xmlNameArtifact) + xmlutil.Text(c, s.Artifact) + xmlutil.Add(n, c) + + return signExchange(ctx, n, key, nil) +} + +// VerifyArtifactResolve authenticates and validates an artifact resolution. +func VerifyArtifactResolve(ctx context.Context, d *document.Document, keys []crypto.PublicKey, o ExchangeOptions) (*VerifiedArtifactResolve, error) { + h, cs, err := verifyExchange(ctx, d, lexicon.SAMLElementArtifactResolve, keys, o, nil, false) + if err != nil { + return nil, err + } + + if len(cs) != 1 || cs[0].Name() != xmlutil.Name(ProtocolNamespace, xmlNameArtifact) { + return nil, ErrProfile + } + + v, err := xmlutil.Simple(cs[0], maximumArtifactTextBytes) + if err != nil { + return nil, ErrProfile + } + + return &VerifiedArtifactResolve{request: ArtifactResolve{MessageHeader: h, Artifact: v}}, nil +} + +func controlChildren(d *document.Document, redirect bool) ([]document.Element, error) { + cs, err := xmlutil.Children(d.Root()) + + offset := signedMessagePrefixChildren + if redirect { + offset = unsignedMessagePrefixChildren + } + + if err != nil || len(cs) <= offset { + return nil, ErrProfile + } + + if cs[offset].Name() == xmlutil.Name(ProtocolNamespace, xmlNameExtensions) { + offset++ + } + + return cs[offset:], nil +} + +func decryptControl(ctx context.Context, d *document.Document, cs []document.Element, options *DecryptionOptions) (*document.Document, error) { + var state *decryptionState + + for _, c := range cs { + var expected document.Name + + switch c.Name() { + case xmlutil.Name(AssertionNamespace, xmlNameEncryptedID): + expected = xmlutil.Name(AssertionNamespace, xmlNameNameID) + case xmlutil.Name(ProtocolNamespace, xmlNameNewEncryptedID): + expected = xmlutil.Name(ProtocolNamespace, xmlNameNewID) + default: + continue + } + + if state == nil { + var err error + + state, err = newDecryptionState(options) + if err != nil { + return nil, err + } + } + // A previous replacement changed ownership. Re-select from the current + // immutable snapshot rather than using stale elements. + selected, err := xmlutil.Direct(d.Root(), c.Name()) + if err != nil { + return nil, ErrProfile + } + + d, err = state.decryptSAMLName(ctx, d, selected, expected, true) + if err != nil { + return nil, err + } + } + + return d, nil +} + +func logoutReason(value string) bool { + if len(value) > maximumURITextBytes || strings.ContainsAny(value, lexicon.XMLWhitespace) { + return false + } + + _, err := url.Parse(value) + + return err == nil +} diff --git a/saml/protocol/control_test.go b/saml/protocol/control_test.go new file mode 100644 index 0000000..17e4038 --- /dev/null +++ b/saml/protocol/control_test.go @@ -0,0 +1,272 @@ +package protocol_test + +import ( + "bytes" + "crypto" + "errors" + "strings" + "testing" + "time" + + "github.com/skarm/xmlsec/c14n" + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/testutil/testcert" + "github.com/skarm/xmlsec/saml/binding" + "github.com/skarm/xmlsec/saml/internal/xmlsig" + "github.com/skarm/xmlsec/saml/protocol" + "github.com/skarm/xmlsec/xmldsig" +) + +// Core 3.6 / Errata E12: formats do not change; SPProvidedID is retained. +// Core 3.7 / Profiles 4.4: signatures cover the identifier and session indices. +func TestControlMessages(t *testing.T) { + c := testcert.New(t) + key := protocol.SigningKey{Key: c.LeafKey, CertificateDER: c.Leaf.Raw} + header := protocol.MessageHeader{ID: "_control", Issuer: idp, Destination: acs, IssueInstant: testcert.Now()} + name := protocol.NameIdentifier{Value: "alice", Format: protocol.NameIDPersistent, NameQualifier: idp, SPNameQualifier: sp, SPProvidedID: "sp-alias"} + opts := protocol.ExchangeOptions{Issuer: idp, Destination: acs, At: testcert.Now()} + keys := []crypto.PublicKey{c.LeafKey.Public()} + d, err := protocol.SignLogoutRequest(t.Context(), protocol.LogoutRequest{MessageHeader: header, NameID: name, SessionIndexes: []string{"device-2", "device-1"}, NotOnOrAfter: testcert.Now().Add(time.Minute), Reason: "relative-reason"}, key) + if err != nil { + t.Fatal(err) + } + r, err := protocol.VerifyLogoutRequest(t.Context(), d, keys, opts) + if err != nil || r.Request().NameID != name || strings.Join(r.Request().SessionIndexes, ",") != "device-2,device-1" { + t.Fatalf("logout: %v", err) + } + copyRequest := r.Request() + copyRequest.SessionIndexes[0] = "mutated" + if r.Request().SessionIndexes[0] != "device-2" { + t.Fatal("mutable verified request") + } + bad := opts + bad.At = testcert.Now().Add(time.Minute) + if _, err := protocol.VerifyLogoutRequest(t.Context(), d, keys, bad); !errors.Is(err, protocol.ErrConditions) { + t.Fatalf("exclusive expiry: %v", err) + } + bad = opts + bad.Destination = sso + if _, err := protocol.VerifyLogoutRequest(t.Context(), d, keys, bad); err == nil { + t.Fatal("wrong destination") + } + if _, err := protocol.VerifyLogoutRequest(t.Context(), d, []crypto.PublicKey{c.TSAKey.Public()}, opts); err == nil { + t.Fatal("wrong signer") + } + raw, _ := d.XML(t.Context()) + tampered, err := document.Parse(t.Context(), bytes.Replace(raw, []byte("alice"), []byte("mallory"), 1), document.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + if _, err := protocol.VerifyLogoutRequest(t.Context(), tampered, keys, opts); err == nil { + t.Fatal("tampered identifier accepted") + } + query, err := binding.SignRedirect(t.Context(), d, binding.Request, nil, c.LeafKey, binding.Options{SignatureMethod: xmldsig.ECDSASHA256}) + if err != nil { + t.Fatal(err) + } + if redirected, _, err := protocol.VerifyRedirectLogoutRequest(t.Context(), query, keys, opts, binding.Options{}); err != nil || redirected.Request().NameID != name { + t.Fatalf("redirect: %v", err) + } + for _, terminate := range []bool{false, true} { + request := protocol.ManageNameIDRequest{MessageHeader: header, NameID: name, NewID: "new-value"} + if terminate { + request.NewID = "" + request.Terminate = true + } + d, err := protocol.SignManageNameIDRequest(t.Context(), request, key) + if err != nil { + t.Fatal(err) + } + verified, err := protocol.VerifyManageNameIDRequest(t.Context(), d, keys, opts) + if err != nil || verified.Request().NameID != name || verified.Request().NewID != request.NewID || verified.Request().Terminate != terminate { + t.Fatalf("manage: %v", err) + } + } +} + +func TestEncryptedControlIdentifiers(t *testing.T) { + c, recipient := testcert.New(t), encryptionKey(t) + key := protocol.SigningKey{Key: c.LeafKey, CertificateDER: c.Leaf.Raw} + s := protocol.ManageNameIDRequest{ID: "_encrypted", Issuer: idp, Destination: acs, IssueInstant: testcert.Now(), NameID: protocol.NameIdentifier{Value: "old-secret", Format: protocol.NameIDPersistent}, NewID: "new-secret", Encryption: &protocol.EncryptionOptions{Targets: protocol.EncryptNameID, Recipient: &recipient.PublicKey}} + d, err := protocol.SignManageNameIDRequest(t.Context(), s, key) + if err != nil { + t.Fatal(err) + } + raw, _ := d.XML(t.Context()) + if bytes.Contains(raw, []byte("old-secret")) || bytes.Contains(raw, []byte("new-secret")) || !bytes.Contains(raw, []byte("samlp:NewEncryptedID")) { + t.Fatal("plaintext or wrong NewEncryptedID namespace") + } + o := protocol.ExchangeOptions{Issuer: idp, Destination: acs, At: testcert.Now(), Decryption: &protocol.DecryptionOptions{Keys: []crypto.Decrypter{recipient}}} + r, err := protocol.VerifyManageNameIDRequest(t.Context(), d, []crypto.PublicKey{c.LeafKey.Public()}, o) + if err != nil || r.Request().NameID.Value != "old-secret" || r.Request().NewID != "new-secret" { + t.Fatalf("decrypt: %v", err) + } + o.Decryption = nil + if _, err := protocol.VerifyManageNameIDRequest(t.Context(), d, []crypto.PublicKey{c.LeafKey.Public()}, o); err == nil { + t.Fatal("encrypted names without key accepted") + } +} + +// An independently written wire fixture exercises explicit Issuer Format, nested +// status codes, alternative namespace prefixes and invalid signed structures. +func TestControlIndependentWire(t *testing.T) { + c := testcert.New(t) + template := `` + idp + `alicebob` + sign := func(raw string) *document.Document { + t.Helper() + d, err := document.Parse(t.Context(), []byte(raw), document.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + d, err = xmlsig.Sign(t.Context(), d, d.Root(), xmlsig.SigningKey{Key: c.LeafKey, CertificateDER: c.Leaf.Raw, Method: xmldsig.ECDSASHA256}, true) + if err != nil { + t.Fatal(err) + } + return d + } + o := protocol.ExchangeOptions{Issuer: idp, Destination: acs, At: testcert.Now()} + if _, err := protocol.VerifyManageNameIDRequest(t.Context(), sign(template), []crypto.PublicKey{c.LeafKey.Public()}, o); err != nil { + t.Fatal(err) + } + withoutDestination := sign(strings.Replace(template, ` Destination="`+acs+`"`, "", 1)) + if _, err := protocol.VerifyManageNameIDRequest(t.Context(), withoutDestination, []crypto.PublicKey{c.LeafKey.Public()}, o); err == nil { + t.Fatal("front-channel Destination requirement bypassed") + } + soapOptions := o + soapOptions.Binding = "urn:oasis:names:tc:SAML:2.0:bindings:SOAP" + if _, err := protocol.VerifyManageNameIDRequest(t.Context(), withoutDestination, []crypto.PublicKey{c.LeafKey.Public()}, soapOptions); err != nil { + t.Fatalf("SOAP Destination is optional: %v", err) + } + for _, raw := range []string{ + strings.Replace(template, protocol.NameIDPersistent, protocol.NameIDTransient, 1), + strings.Replace(template, "bob", "bob", 1), + strings.Replace(template, "bob", "unexpected", 1), + strings.Replace(template, "bob", "bob", 1), + } { + if _, err := protocol.VerifyManageNameIDRequest(t.Context(), sign(raw), []crypto.PublicKey{c.LeafKey.Public()}, o); err == nil { + t.Fatal("invalid signed management accepted") + } + } +} + +func TestControlResponseAndSOAPCanonicalization(t *testing.T) { + c := testcert.New(t) + for _, method := range []string{c14n.Exclusive, c14n.Canonical10} { + t.Run(method, func(t *testing.T) { + key := protocol.SigningKey{Key: c.LeafKey, CertificateDER: c.Leaf.Raw, Canonicalization: method} + d, err := protocol.SignStatusResponse(t.Context(), protocol.StatusResponse{ID: "_reply", Issuer: idp, Destination: acs, IssueInstant: testcert.Now(), Kind: "LogoutResponse", InResponseTo: "_request", Status: protocol.Status{Code: protocol.Success, Subcodes: []string{protocol.PartialLogout}, Message: "one participant unavailable"}}, key) + if err != nil { + t.Fatal(err) + } + envelope, err := protocol.SignSOAP(t.Context(), d, key) + if err != nil { + t.Fatal(err) + } + raw, _ := envelope.XML(t.Context()) + m, err := binding.DecodeSOAP(t.Context(), raw, binding.SOAPOptions{}) + if err != nil { + t.Fatal(err) + } + payload, err := m.Payload(t.Context()) + if err != nil { + t.Fatal(err) + } + o := protocol.ExchangeOptions{Issuer: idp, Destination: acs, ExpectedRequestID: "_request", At: testcert.Now()} + r, err := protocol.VerifyStatusResponse(t.Context(), payload, []crypto.PublicKey{c.LeafKey.Public()}, o) + if err != nil || r.Status().Code != protocol.Success || r.Status().Subcodes[0] != protocol.PartialLogout { + t.Fatalf("SOAP status: %v", err) + } + s := r.Status() + s.Subcodes[0] = "mutated" + if r.Status().Subcodes[0] != protocol.PartialLogout { + t.Fatal("mutable status") + } + o.ExpectedRequestID = "_other" + if _, err := protocol.VerifyStatusResponse(t.Context(), payload, []crypto.PublicKey{c.LeafKey.Public()}, o); !errors.Is(err, protocol.ErrCorrelation) { + t.Fatalf("correlation: %v", err) + } + }) + } +} + +func TestNameIdentifierFormatBounds(t *testing.T) { + for _, tc := range []struct { + name protocol.NameIdentifier + valid bool + }{ + {protocol.NameIdentifier{Value: strings.Repeat("界", 256), Format: protocol.NameIDPersistent}, true}, + {protocol.NameIdentifier{Value: strings.Repeat("界", 257), Format: protocol.NameIDPersistent}, false}, + {protocol.NameIdentifier{Value: "_opaque-123", Format: protocol.NameIDTransient}, true}, + {protocol.NameIdentifier{Value: "not:an:NCName", Format: protocol.NameIDTransient}, false}, + {protocol.NameIdentifier{Value: "https://idp.test/entity", Format: "urn:oasis:names:tc:SAML:2.0:nameid-format:entity"}, true}, + {protocol.NameIdentifier{Value: "https://idp.test/entity", Format: "urn:oasis:names:tc:SAML:2.0:nameid-format:entity", SPProvidedID: "forbidden"}, false}, + } { + if err := tc.name.Validate(); (err == nil) != tc.valid { + t.Fatalf("format constraint: %+v %v", tc.name, err) + } + } + a, err := protocol.NewPersistentNameID(idp, sp) + if err != nil || a.Validate() != nil || a.NameQualifier != idp || a.SPNameQualifier != sp { + t.Fatal("persistent identifier generation") + } + b, err := protocol.NewPersistentNameID(idp, sp) + if err != nil || a.Value == b.Value { + t.Fatal("persistent identifiers reused") + } +} + +func TestSOAPInheritedContextWithEncryptedNames(t *testing.T) { + c := testcert.New(t) + recipient := encryptionKey(t) + key := protocol.SigningKey{Key: c.LeafKey, CertificateDER: c.Leaf.Raw, Canonicalization: c14n.Canonical10} + d, err := protocol.SignManageNameIDRequest(t.Context(), protocol.ManageNameIDRequest{ID: "_context", Issuer: idp, Destination: acs, IssueInstant: testcert.Now(), NameID: protocol.NameIdentifier{Value: "old-secret", Format: protocol.NameIDPersistent}, NewID: "new-secret", Encryption: &protocol.EncryptionOptions{Targets: protocol.EncryptNameID, Recipient: &recipient.PublicKey}}, key) + if err != nil { + t.Fatal(err) + } + envelope, err := protocol.SignSOAP(t.Context(), d, key) + if err != nil { + t.Fatal(err) + } + raw, _ := envelope.XML(t.Context()) + raw = bytes.ReplaceAll(raw, []byte("env:"), []byte("S:")) + raw = bytes.ReplaceAll(raw, []byte("xmlns:env="), []byte("xmlns:S=")) + raw = bytes.Replace(raw, []byte(" 0 { - statement := xmlutil.Node("saml", AssertionNamespace, "AttributeStatement") + statement := xmlutil.Node(prefixAssertion, AssertionNamespace, xmlNameAttributeStatement) seen := map[[2]string]struct{}{} for _, v := range a.Attributes { @@ -208,17 +268,17 @@ func assertionNode(s Response) (*tree.Node, error) { format = AttributeUnspecified } - if _, exists := seen[[2]string{v.Name, format}]; v.Name == "" || len(v.Name) > 1024 || !profile.URI(format) || len(v.FriendlyName) > 1024 || len(v.Values) < 1 || len(v.Values) > l.MaxValuesPerAttribute || exists { + if _, exists := seen[[2]string{v.Name, format}]; v.Name == "" || len(v.Name) > maximumAttributeNameBytes || !profile.URI(format) || len(v.FriendlyName) > maximumFriendlyNameBytes || len(v.Values) < 1 || len(v.Values) > l.MaxValuesPerAttribute || exists { return nil, ErrProfile } seen[[2]string{v.Name, format}] = struct{}{} - attr := xmlutil.Node("saml", AssertionNamespace, "Attribute") - xmlutil.Set(attr, "Name", v.Name) - xmlutil.Set(attr, "NameFormat", format) + attr := xmlutil.Node(prefixAssertion, AssertionNamespace, xmlNameAttribute) + xmlutil.Set(attr, nameAttributeName, v.Name) + xmlutil.Set(attr, xmlNameNameFormat, format) if v.FriendlyName != "" { - xmlutil.Set(attr, "FriendlyName", v.FriendlyName) + xmlutil.Set(attr, xmlNameFriendlyName, v.FriendlyName) } for _, s := range v.Values { @@ -226,7 +286,7 @@ func assertionNode(s Response) (*tree.Node, error) { return nil, ErrProfile } - value := xmlutil.Node("saml", AssertionNamespace, "AttributeValue") + value := xmlutil.Node(prefixAssertion, AssertionNamespace, xmlNameAttributeValue) xmlutil.Text(value, s) xmlutil.Add(attr, value) } @@ -239,3 +299,29 @@ func assertionNode(s Response) (*tree.Node, error) { return node, nil } + +func validateProducedAssertion(d *document.Document, s Response) error { + plain, e := xmlutil.Direct(d.Root(), xmlutil.Name(AssertionNamespace, nameAssertion)) + if e != nil { + return e + } + + l, e := normalize(s.Limits) + if e != nil { + return e + } + + if _, e = parseAssertion(plain, ResponseOptions{Issuer: s.Issuer, Audience: s.Audience, Destination: s.Destination, ExpectedRequestID: s.InResponseTo, At: s.IssueInstant, AllowUnsignedAssertion: true, producedCertificate: s.Assertion.HolderOfKeyCertificate}, l); e != nil { + return e + } + + return nil +} + +func appendNameQualifiers(name *tree.Node, a AssertionData) { + for _, attr := range [][2]string{{nameNameQualifier, a.NameQualifier}, {nameSPNameQualifier, a.SPNameQualifier}, {nameSPProvidedID, a.SPProvidedID}} { + if attr[1] != "" { + xmlutil.Set(name, attr[0], attr[1]) + } + } +} diff --git a/saml/protocol/doc.go b/saml/protocol/doc.go index 6e9f955..f6e5dc6 100644 --- a/saml/protocol/doc.go +++ b/saml/protocol/doc.go @@ -1,6 +1,7 @@ -// Package protocol builds and verifies a closed, solicited SAML 2.0 Web Browser -// SSO profile. It checks authenticated XML against application-supplied partner, -// endpoint, correlation and time expectations. +// Package protocol builds and verifies bounded SAML 2.0 messages for solicited +// WebSSO, logout, NameID management, artifact resolution, authority queries and +// NameID Mapping. It checks authenticated XML against application-supplied +// partner, endpoint, correlation and time expectations. // // # Requests and responses // @@ -19,6 +20,27 @@ // [StatusError] describes an authenticated non-success response and is never a login. // [Limits] bounds message age, assertion lifetime and attribute collections. // +// [EncryptionOptions] encrypts Assertion, NameID and/or Attribute. Verification +// authenticates the original Response, decrypts an encrypted Assertion, verifies +// its signature, then decrypts inner identifiers and attributes before checking +// claims. [DecryptionOptions] fixes algorithms and a bounded private-key ring; +// received KeyInfo never supplies trust. CBC/RSA15 require prior enclosing +// authentication. RequireEncryption rejects plaintext at selected positions. +// [SigningKey] supports the original Canonical XML/SHA-1 baseline through +// explicit policies. [NewSharedSecret] selects protocol-level HMAC credentials +// separately from public keys; every secret holder can produce a valid MAC. +// +// # Authority queries and identifier mapping +// +// [SignQuery] and [VerifyQuery] handle authentication, attribute and authorization +// queries and assertion-ID requests. [SignQueryResponse] and [VerifyQueryResponse] +// retain zero or more separately authenticated assertions, including empty success. +// [SignAuthorityAssertion] does not impose WebSSO login or bearer semantics. +// [SignNameIDMappingRequest] and [VerifyNameIDMappingResponse] implement the +// separate mapping protocol. Opaque target-encrypted identifiers can be forwarded; +// they never become local identities merely by verifying the response envelope. +// Stateful SOAP, URI, authorization and replay integration lives in saml/authority. +// // # Compose the surrounding flow // // The package does not authenticate a user, retrieve metadata, bind a request to a @@ -26,6 +48,8 @@ // github.com/skarm/xmlsec/saml/sp and github.com/skarm/xmlsec/saml/idp to compose // those checks with trusted adapters. Authentication statements supplied during // issuance must reflect decisions actually made by the application. -// Unsolicited SSO, logout, Artifact/ECP and encrypted-assertion profiles are outside -// this protocol profile. +// Logout, NameID management and artifact messages have separate authenticated +// request/response types. Their stateful processing is in saml/session and +// saml/artifact. SOAP/PAOS routing is in saml/binding and saml/ecp. Unsolicited +// SSO and custom BaseID extensions are outside this profile. package protocol diff --git a/saml/protocol/encryption.go b/saml/protocol/encryption.go new file mode 100644 index 0000000..38ab503 --- /dev/null +++ b/saml/protocol/encryption.go @@ -0,0 +1,736 @@ +package protocol + +import ( + "bytes" + "context" + "crypto" + "crypto/rsa" + "crypto/x509" + "errors" + "net/url" + "strings" + + "github.com/skarm/xmlsec/internal/lexicon" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/nilcheck" + "github.com/skarm/xmlsec/internal/tree" + "github.com/skarm/xmlsec/saml/internal/xmlutil" + "github.com/skarm/xmlsec/xmlenc" +) + +// XML Encryption local names used by the restricted SAML encryption grammar. +// Callers separately require the XML Encryption namespace and allowed child order. +const ( + xmlNameCipherData = "CipherData" + xmlNameEncryptedData = "EncryptedData" + xmlNameEncryptedKey = "EncryptedKey" + xmlNameEncryptionMethod = "EncryptionMethod" + xmlNameEncryptionProperties = "EncryptionProperties" +) + +const nameEncryptedType = lexicon.SecurityAttrType + +// EncryptionTargets selects SAML elements to encrypt or require encrypted. +type EncryptionTargets uint8 + +const ( + // EncryptAssertion encrypts the complete assertion after its signature. + EncryptAssertion EncryptionTargets = 1 << iota + // EncryptNameID encrypts NameID before the containing assertion is signed. + EncryptNameID + // EncryptAttributes encrypts each Attribute before the assertion is signed. + EncryptAttributes +) + +// ErrEncryption reports rejected encrypted SAML data without exposing whether a +// key, padding, authentication tag or decrypted XML was incorrect. +var ErrEncryption = errors.New("SAML encryption rejected") + +// EncryptionOptions selects explicit targets, recipient and algorithms. Zero +// Data uses AES-256-GCM/RSA-OAEP-SHA256. CBC needs explicit legacy permission and +// an enclosing authenticated signature. No external ciphertext is accepted. +type EncryptionOptions struct { + // Targets selects the SAML elements to encrypt; at least one target is + // required. + Targets EncryptionTargets + // Recipient is mandatory at protocol level. The IdP may select it from + // authenticated SP encryption metadata when this field is nil. + Recipient *rsa.PublicKey + // Data selects data and key transport algorithms. Zero uses AES-256-GCM with + // RSA-OAEP-SHA256. + Data xmlenc.Options +} + +// DecryptionOptions supplies a bounded, application-owned RSA key ring. Keys and +// their providers must support concurrent calls. XML key descriptions never add +// keys or trust; inline/sibling EncryptedKey alternatives use only this ring. +type DecryptionOptions struct { + // Keys supplies the trusted, application-owned RSA private-key providers to + // try. + Keys []crypto.Decrypter + // Data fixes the expected algorithms; received XML cannot broaden policy. + Data xmlenc.Options + // MaxDecryptions bounds private-key attempts across the entire response. + // Zero selects 64; the maximum is 1024. + MaxDecryptions int +} + +type decryptionState struct { + // options retains the snapshotted key ring and algorithm policy. + options DecryptionOptions + // remaining shares the private-key attempt budget across the complete message. + remaining int +} + +func validEncryptionTargets(targets EncryptionTargets) bool { + return targets&^(EncryptAssertion|EncryptNameID|EncryptAttributes) == 0 +} + +// CloneEncryption validates and snapshots producer policy without requiring a +// recipient. High-level IdPs obtain that recipient from trusted metadata. +func CloneEncryption(o *EncryptionOptions) (*EncryptionOptions, error) { + if o == nil { + return nil, nil + } + + if o.Targets == 0 || !validEncryptionTargets(o.Targets) || o.Data.External != nil { + return nil, ErrEncryption + } + + p := *o + + p.Data = cloneCipherOptions(p.Data) + if p.Recipient != nil { + if !validRSARecipient(p.Recipient) { + return nil, ErrEncryption + } + + der, err := x509.MarshalPKIXPublicKey(p.Recipient) + if err != nil { + return nil, ErrEncryption + } + + key, err := x509.ParsePKIXPublicKey(der) + if err != nil { + return nil, ErrEncryption + } + + public, ok := key.(*rsa.PublicKey) + if !ok { + return nil, ErrEncryption + } + + p.Recipient = public + } + + return &p, nil +} + +// CloneDecryption validates and snapshots the key-ring slice and cipher policy. +// Private providers remain caller-owned, as with SigningKey.Key. +func CloneDecryption(o *DecryptionOptions) (*DecryptionOptions, error) { + const ( + defaultDecryptionAttempts = 64 + maximumDecryptionAttempts = 1024 + maximumDecryptionKeys = 16 + ) + + if o == nil { + return nil, nil + } + + if len(o.Keys) == 0 || len(o.Keys) > maximumDecryptionKeys || o.Data.External != nil || o.MaxDecryptions < 0 || o.MaxDecryptions > maximumDecryptionAttempts { + return nil, ErrEncryption + } + + p := *o + p.Keys = append([]crypto.Decrypter(nil), o.Keys...) + + p.Data = cloneCipherOptions(p.Data) + for _, key := range p.Keys { + if nilcheck.IsNil(key) { + return nil, ErrEncryption + } + + public, ok := key.Public().(*rsa.PublicKey) + if !ok || !validRSARecipient(public) { + return nil, ErrEncryption + } + } + + if p.MaxDecryptions == 0 { + p.MaxDecryptions = defaultDecryptionAttempts + } + + return &p, nil +} + +func validRSARecipient(key *rsa.PublicKey) bool { + const ( + maximumRSAExponent = 0x7fffffff + maximumRSARecipientBits = 16384 + minimumRSAExponent = 3 + minimumRSARecipientBits = 2048 + ) + + return key != nil && key.N != nil && key.N.Sign() > 0 && key.N.Bit(0) != 0 && key.N.BitLen() >= minimumRSARecipientBits && key.N.BitLen() <= maximumRSARecipientBits && key.E >= minimumRSAExponent && key.E%2 != 0 && key.E <= maximumRSAExponent +} + +func cloneCipherOptions(o xmlenc.Options) xmlenc.Options { + if o.OAEP != nil { + parameters := *o.OAEP + parameters.Label = bytes.Clone(parameters.Label) + o.OAEP = ¶meters + } + + return o +} + +func newDecryptionState(o *DecryptionOptions) (*decryptionState, error) { + p, err := CloneDecryption(o) + if err != nil || p == nil { + return nil, err + } + + return &decryptionState{options: *p, remaining: p.MaxDecryptions}, nil +} + +func replaceSAML(ctx context.Context, d *document.Document, old document.Element, replacement *tree.Node) (*document.Document, error) { + owner, node := tree.Unwrap(old) + if owner != d { + return nil, ErrProfile + } + + draft, err := tree.ReplaceDraft(ctx, d, node, replacement) + if err != nil { + return nil, err + } + + return tree.Reparse(ctx, draft, tree.Policy(d)) +} + +func encryptSAML(ctx context.Context, d *document.Document, selected document.Element, wrapper string, options *EncryptionOptions) (*document.Document, error) { + encrypted, data, err := xmlenc.EncryptElement(ctx, d, selected, options.Recipient, options.Data) + if err != nil { + return nil, err + } + + _, node := tree.Unwrap(data) + + container := xmlutil.Node(prefixAssertion, AssertionNamespace, wrapper) + if wrapper == xmlNameNewEncryptedID { + container = xmlutil.Node(prefixProtocol, ProtocolNamespace, wrapper) + } + + if selected == d.Root() { + container.NS = []tree.Namespace{{Prefix: container.Prefix, URI: container.Name.Space}} + } + + xmlutil.Add(container, tree.ExtractSubtree(node)) + + return replaceSAML(ctx, encrypted, data, container) +} + +func cbcCipher(o xmlenc.Options) bool { + return o.DataAlgorithm == xmlenc.AES128CBC || o.DataAlgorithm == xmlenc.AES192CBC || o.DataAlgorithm == xmlenc.AES256CBC || o.DataAlgorithm == xmlenc.TripleDESCBC +} + +// decryptSAML is called only after the containing signature is verified, except +// explicitly allowed unsigned envelopes using authenticated GCM data encryption. +func (s *decryptionState) decryptSAML(ctx context.Context, d *document.Document, wrapper document.Element, expected string, authenticated bool) (*document.Document, error) { + return s.decryptSAMLName(ctx, d, wrapper, xmlutil.Name(AssertionNamespace, expected), authenticated) +} + +func (s *decryptionState) decryptSAMLName(ctx context.Context, d *document.Document, wrapper document.Element, expected document.Name, authenticated bool) (*document.Document, error) { + if s == nil || (cbcCipher(s.options.Data) || s.options.Data.KeyTransportAlgorithm == xmlenc.RSA15) && !authenticated || xmlutil.Attrs(wrapper, nil, nil) != nil { + return nil, ErrEncryption + } + + data, candidates, err := encryptedSAMLRecipients(wrapper) + if err != nil { + return nil, err + } + + for _, candidate := range candidates { + private, selected, err := samlCipherCandidate(ctx, wrapper, data, candidate) + if err != nil { + return nil, encryptionFailure(ctx, err) + } + + for _, key := range s.options.Keys { + if s.remaining == 0 { + return nil, errors.Join(ErrEncryption, document.ErrLimit) + } + + s.remaining-- + + _, plain, err := xmlenc.DecryptElement(ctx, private, selected, key, s.options.Data) + if ctx.Err() != nil { + return nil, ctx.Err() + } + + if err != nil { + continue + } + + if plain.Name() != expected { + return nil, ErrEncryption + } + + _, node := tree.Unwrap(plain) + + return replaceSAML(ctx, d, wrapper, tree.ExtractSubtree(node)) + } + } + + return nil, ErrEncryption +} + +func encryptedSAMLRecipients(wrapper document.Element) (document.Element, []document.Element, error) { + const ( + maximumEncryptedKeyCandidates = 16 + maximumEncryptedRecipientChildren = 17 + ) + + children, err := xmlutil.Children(wrapper) + if err != nil || len(children) == 0 || len(children) > maximumEncryptedRecipientChildren || children[0].Name() != xmlutil.Name(xmlenc.Namespace, xmlNameEncryptedData) { + return document.Element{}, nil, ErrEncryption + } + + data := children[0] + if err := samlEncryptedData(data); err != nil { + return document.Element{}, nil, err + } + + typ, present := data.Attribute(document.Name{Local: nameEncryptedType}) + if present && typ != xmlenc.ElementType { + return document.Element{}, nil, ErrEncryption + } + + var candidates []document.Element + + for _, child := range data.Children() { + if child.Name() == xmlutil.Name(xmlutil.DS, lexicon.DSigElementKeyInfo) { + for _, key := range child.Children() { + if key.Name() == xmlutil.Name(xmlenc.Namespace, xmlNameEncryptedKey) { + candidates = append(candidates, key) + } + } + } + } + + for _, child := range children[1:] { + if child.Name() != xmlutil.Name(xmlenc.Namespace, xmlNameEncryptedKey) { + return document.Element{}, nil, ErrEncryption + } + + candidates = append(candidates, child) + } + + if len(candidates) == 0 || len(candidates) > maximumEncryptedKeyCandidates { + return document.Element{}, nil, ErrEncryption + } + + return data, candidates, nil +} + +func encryptionFailure(ctx context.Context, err error) error { + if ctx.Err() != nil { + return ctx.Err() + } + + if errors.Is(err, document.ErrLimit) { + return errors.Join(ErrEncryption, document.ErrLimit) + } + + return ErrEncryption +} + +// The SAML wrapper supplies the encrypted-key candidates. KeyInfo is a hint, +// not a source of authority: retain each candidate's cryptographic method/data +// and evaluate it only with explicitly configured private keys. ReferenceList, +// when present, must actually designate this EncryptedData. +func samlCipherCandidate(ctx context.Context, wrapper, data, key document.Element) (*document.Document, document.Element, error) { + const ( + prefixEncryption = "xenc" + ) + + method, cipher, err := samlEncryptedKeyMaterial(key, xmlutil.Attr(data, lexicon.SecurityAttrID)) + if err != nil { + return nil, document.Element{}, err + } + + _, original := tree.Unwrap(data) + copyData := tree.ExtractSubtree(original) + copyData.Children = nil + // Id, MimeType and Encoding describe the encrypted object. They do not + // select algorithms or keys and need not enter the closed cipher profile. + copyData.Attrs = []tree.Attribute{{Name: document.Name{Local: nameEncryptedType}, Value: xmlenc.ElementType}} + for _, child := range original.Children { + if child.Kind == tree.ElementNode && (child.Name == xmlutil.Name(xmlutil.DS, lexicon.DSigElementKeyInfo) || child.Name == xmlutil.Name(xmlenc.Namespace, xmlNameEncryptionProperties)) { + continue + } + + copyData.Children = append(copyData.Children, child) + } + + info := xmlutil.Node(lexicon.PrefixDSig, xmlutil.DS, lexicon.DSigElementKeyInfo) + info.NS = []tree.Namespace{{Prefix: lexicon.PrefixDSig, URI: xmlutil.DS}} + wrapped := xmlutil.Node(prefixEncryption, xmlenc.Namespace, xmlNameEncryptedKey) + wrapped.NS = []tree.Namespace{{Prefix: prefixEncryption, URI: xmlenc.Namespace}} + xmlutil.Add(wrapped, method) + xmlutil.Add(wrapped, cipher) + xmlutil.Add(info, wrapped) + // EncryptionMethod precedes KeyInfo; CipherData follows it. + insert := 0 + + for i, child := range copyData.Children { + if child.Kind == tree.ElementNode && child.Name == xmlutil.Name(xmlenc.Namespace, xmlNameEncryptionMethod) { + insert = i + 1 + } + } + + copyData.Children = append(copyData.Children, nil) + copy(copyData.Children[insert+1:], copyData.Children[insert:]) + copyData.Children[insert] = info + info.Parent = copyData + _, parent := tree.Unwrap(wrapper) + container := tree.ExtractSubtree(parent) + container.Children = []*tree.Node{copyData} + copyData.Parent = container + limits := tree.Policy(documentOwner(wrapper)) + + private, err := tree.Reparse(ctx, tree.FromNodes([]*tree.Node{container}, limits), limits) + if err != nil { + return nil, document.Element{}, err + } + + selected, err := xmlutil.Direct(private.Root(), xmlutil.Name(xmlenc.Namespace, xmlNameEncryptedData)) + + return private, selected, err +} + +func samlEncryptedKeyMaterial(key document.Element, dataID string) (*tree.Node, *tree.Node, error) { + const ( + keyAfterCarriedNameState = 5 + keyAfterCipherDataState = 3 + keyAfterKeyInfoState = 2 + keyAfterMethodState = 1 + keyAfterReferencesState = 4 + keyInitialState = 0 + minimumEncryptedKeyChildren = 2 + xmlNameCarriedKeyName = "CarriedKeyName" + xmlNameReferenceList = "ReferenceList" + ) + + if xmlutil.Attrs(key, nil, []string{lexicon.SecurityAttrID, nameEncryptedType, lexicon.SecurityAttrMimeType, lexicon.SecurityAttrEncoding, nameRecipient}) != nil { + return nil, nil, ErrEncryption + } + + cs, err := xmlutil.Children(key) + if err != nil || len(cs) < minimumEncryptedKeyChildren || cs[0].Name() != xmlutil.Name(xmlenc.Namespace, xmlNameEncryptionMethod) { + return nil, nil, ErrEncryption + } + + var method, cipher *tree.Node + + position := keyInitialState + + for _, child := range cs { + _, node := tree.Unwrap(child) + switch child.Name() { + case xmlutil.Name(xmlenc.Namespace, xmlNameEncryptionMethod): + if position != keyInitialState { + return nil, nil, ErrEncryption + } + + method, position = tree.ExtractSubtree(node), keyAfterMethodState + case xmlutil.Name(xmlutil.DS, lexicon.DSigElementKeyInfo): + if position != keyAfterMethodState { + return nil, nil, ErrEncryption + } + + position = keyAfterKeyInfoState + case xmlutil.Name(xmlenc.Namespace, xmlNameCipherData): + if position != keyAfterMethodState && position != keyAfterKeyInfoState { + return nil, nil, ErrEncryption + } + + cipher, position = tree.ExtractSubtree(node), keyAfterCipherDataState + case xmlutil.Name(xmlenc.Namespace, xmlNameReferenceList): + if position != keyAfterCipherDataState || !samlKeyReferences(child, dataID) { + return nil, nil, ErrEncryption + } + + position = keyAfterReferencesState + case xmlutil.Name(xmlenc.Namespace, xmlNameCarriedKeyName): + if position < keyAfterCipherDataState || position > keyAfterReferencesState { + return nil, nil, ErrEncryption + } + + position = keyAfterCarriedNameState + default: + return nil, nil, ErrEncryption + } + } + + if method == nil || cipher == nil { + return nil, nil, ErrEncryption + } + + return method, cipher, nil +} + +func samlEncryptedData(data document.Element) error { + const ( + dataAfterCipherDataState = 3 + dataAfterKeyInfoState = 2 + dataAfterMethodState = 1 + dataAfterPropertiesState = 4 + dataInitialState = 0 + ) + + if xmlutil.Attrs(data, nil, []string{lexicon.SecurityAttrID, nameEncryptedType, lexicon.SecurityAttrMimeType, lexicon.SecurityAttrEncoding}) != nil { + return ErrEncryption + } + + children, err := xmlutil.Children(data) + if err != nil { + return ErrEncryption + } + + position, hasCipher := dataInitialState, false + + for _, child := range children { + switch child.Name() { + case xmlutil.Name(xmlenc.Namespace, xmlNameEncryptionMethod): + if position != dataInitialState { + return ErrEncryption + } + + position = dataAfterMethodState + case xmlutil.Name(xmlutil.DS, lexicon.DSigElementKeyInfo): + if position > dataAfterMethodState { + return ErrEncryption + } + + position = dataAfterKeyInfoState + case xmlutil.Name(xmlenc.Namespace, xmlNameCipherData): + if position > dataAfterKeyInfoState { + return ErrEncryption + } + + position, hasCipher = dataAfterCipherDataState, true + case xmlutil.Name(xmlenc.Namespace, xmlNameEncryptionProperties): + if position != dataAfterCipherDataState { + return ErrEncryption + } + + position = dataAfterPropertiesState + default: + return ErrEncryption + } + } + + if !hasCipher { + return ErrEncryption + } + + return nil +} + +func documentOwner(element document.Element) *document.Document { + d, _ := tree.Unwrap(element) + return d +} + +func samlKeyReferences(list document.Element, id string) bool { + const ( + xmlNameDataReference = "DataReference" + xmlNameKeyReference = "KeyReference" + ) + + if id == "" || xmlutil.Attrs(list, nil, nil) != nil { + return false + } + + children, err := xmlutil.Children(list) + if err != nil || len(children) == 0 { + return false + } + + found := false + + for _, child := range children { + if child.Name() != xmlutil.Name(xmlenc.Namespace, xmlNameDataReference) && child.Name() != xmlutil.Name(xmlenc.Namespace, xmlNameKeyReference) || xmlutil.Attrs(child, []string{lexicon.SecurityAttrURI}, nil) != nil { + return false + } + + uri := xmlutil.Attr(child, lexicon.SecurityAttrURI) + + value, err := url.PathUnescape(strings.TrimPrefix(uri, "#")) + if err != nil { + return false + } + + found = found || child.Name().Local == xmlNameDataReference && strings.HasPrefix(uri, "#") && value == id + } + + return found +} + +func assertionChild(d *document.Document, local string) (document.Element, error) { + assertion, err := xmlutil.Direct(d.Root(), xmlutil.Name(AssertionNamespace, nameAssertion)) + if err != nil { + return document.Element{}, err + } + + return xmlutil.Direct(assertion, xmlutil.Name(AssertionNamespace, local)) +} + +func encryptAssertionContents(ctx context.Context, d *document.Document, o *EncryptionOptions) (*document.Document, error) { + if o.Targets&EncryptNameID != 0 { + subject, err := assertionChild(d, xmlNameSubject) + if err != nil { + return nil, err + } + + name, err := xmlutil.Direct(subject, xmlutil.Name(AssertionNamespace, xmlNameNameID)) + if err != nil { + return nil, err + } + + d, err = encryptSAML(ctx, d, name, xmlNameEncryptedID, o) + if err != nil { + return nil, err + } + } + + if o.Targets&EncryptAttributes != 0 { + for { + statement, err := optionalAttributeStatement(d) + if err != nil || !statement.Valid() { + return d, err + } + + var selected document.Element + + for _, child := range statement.Children() { + if child.Name() == xmlutil.Name(AssertionNamespace, xmlNameAttribute) { + selected = child + break + } + } + + if !selected.Valid() { + break + } + + d, err = encryptSAML(ctx, d, selected, xmlNameEncryptedAttribute, o) + if err != nil { + return nil, err + } + } + } + + return d, nil +} + +func optionalAttributeStatement(d *document.Document) (document.Element, error) { + assertion, err := xmlutil.Direct(d.Root(), xmlutil.Name(AssertionNamespace, nameAssertion)) + if err != nil { + return document.Element{}, err + } + + var found document.Element + + for _, child := range assertion.Children() { + if child.Name() == xmlutil.Name(AssertionNamespace, xmlNameAttributeStatement) { + if found.Valid() { + return document.Element{}, ErrProfile + } + + found = child + } + } + + return found, nil +} + +func decryptAssertionContents(ctx context.Context, d *document.Document, state *decryptionState, require EncryptionTargets, authenticated bool, limits Limits) (*document.Document, EncryptionTargets, error) { + var targets EncryptionTargets + + subject, err := assertionChild(d, xmlNameSubject) + if err != nil { + return nil, 0, err + } + + children, err := xmlutil.Children(subject) + if err != nil || len(children) < minimumSubjectChildren || len(children) > maximumSubjectChildren { + return nil, 0, ErrProfile + } + + if children[0].Name() == xmlutil.Name(AssertionNamespace, xmlNameEncryptedID) { + d, err = state.decryptSAML(ctx, d, children[0], xmlNameNameID, authenticated) + if err != nil { + return nil, 0, err + } + + targets |= EncryptNameID + } else if require&EncryptNameID != 0 { + return nil, 0, ErrEncryption + } + // Inspect all attribute positions before replacing any handles. Reacquire + // the statement after each immutable snapshot replacement. + statement, err := optionalAttributeStatement(d) + if err != nil || !statement.Valid() { + return d, targets, err + } + + attributes, err := xmlutil.Children(statement) + if err != nil || len(attributes) > limits.MaxAttributes { + return nil, 0, ErrProfile + } + + for _, attribute := range attributes { + switch attribute.Name() { + case xmlutil.Name(AssertionNamespace, xmlNameEncryptedAttribute): + case xmlutil.Name(AssertionNamespace, xmlNameAttribute): + if require&EncryptAttributes != 0 { + return nil, 0, ErrEncryption + } + default: + return nil, 0, ErrProfile + } + } + + for { + statement, err = optionalAttributeStatement(d) + if err != nil { + return nil, 0, err + } + + var selected document.Element + + for _, child := range statement.Children() { + if child.Name() == xmlutil.Name(AssertionNamespace, xmlNameEncryptedAttribute) { + selected = child + break + } + } + + if !selected.Valid() { + break + } + + d, err = state.decryptSAML(ctx, d, selected, xmlNameAttribute, authenticated) + if err != nil { + return nil, 0, err + } + + targets |= EncryptAttributes + } + + return d, targets, nil +} diff --git a/saml/protocol/encryption_test.go b/saml/protocol/encryption_test.go new file mode 100644 index 0000000..c8be68b --- /dev/null +++ b/saml/protocol/encryption_test.go @@ -0,0 +1,323 @@ +package protocol_test + +import ( + "bytes" + "context" + "crypto" + "crypto/fips140" + "crypto/rand" + "crypto/rsa" + "encoding/base64" + "errors" + "fmt" + "io" + "testing" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/testutil/testcert" + "github.com/skarm/xmlsec/internal/tree" + "github.com/skarm/xmlsec/saml/internal/xmlsig" + "github.com/skarm/xmlsec/saml/internal/xmlutil" + "github.com/skarm/xmlsec/saml/protocol" + "github.com/skarm/xmlsec/xmldsig" + "github.com/skarm/xmlsec/xmlenc" +) + +func encryptionKey(t *testing.T) *rsa.PrivateKey { + t.Helper() + key, err := rsa.GenerateKey(rand.Reader, 2048) + if err != nil { + t.Fatal(err) + } + return key +} + +// SAML Core 2.0 sections 2.2.4 and 6.2 (Errata E93): encrypt inner +// identifiers/attributes before signing, and entire assertions after signing. +func TestEncryptedResponseTargets(t *testing.T) { + c, recipient := testcert.New(t), encryptionKey(t) + for targets := protocol.EncryptionTargets(1); targets < 8; targets++ { + t.Run(fmt.Sprint(targets), func(t *testing.T) { + spec := responseSpec() + spec.Encryption = &protocol.EncryptionOptions{Targets: targets, Recipient: &recipient.PublicKey} + d, err := protocol.SignResponse(t.Context(), spec, protocol.SigningKey{Key: c.LeafKey, CertificateDER: c.Leaf.Raw}) + if err != nil { + t.Fatal(err) + } + raw, err := d.XML(t.Context()) + if err != nil || targets&(protocol.EncryptAssertion|protocol.EncryptNameID) != 0 && bytes.Contains(raw, []byte(">alice<")) || targets&(protocol.EncryptAssertion|protocol.EncryptAttributes) != 0 && bytes.Contains(raw, []byte("alice@example.test")) { + t.Fatalf("identity disclosed or serialization failed: %v", err) + } + opts := options() + opts.Decryption = &protocol.DecryptionOptions{Keys: []crypto.Decrypter{recipient}} + opts.RequireEncryption = targets + result, err := protocol.VerifyResponse(t.Context(), d, []crypto.PublicKey{c.LeafKey.Public()}, opts) + if err != nil || result == nil { + t.Fatalf("decrypt/verify: %v", err) + } + if result.NameID() != "alice" || result.Encryption() != targets || len(result.Attributes()) != 2 || result.Attributes()[1].Values[1] != "editor" || !result.ResponseAuthenticated() { + t.Fatal("claims or authenticated encryption targets lost") + } + after, _ := d.XML(t.Context()) + if !bytes.Equal(raw, after) { + t.Fatal("verification mutated the ciphertext snapshot") + } + second, err := protocol.SignResponse(t.Context(), spec, protocol.SigningKey{Key: c.LeafKey, CertificateDER: c.Leaf.Raw}) + if err != nil { + t.Fatal(err) + } + other, _ := second.XML(t.Context()) + if bytes.Equal(raw, other) { + t.Fatal("encryption reused ciphertext") + } + firstNonces, nextNonces := samlGCMNonces(t, d.Root()), samlGCMNonces(t, second.Root()) + if len(firstNonces) == 0 || len(firstNonces) != len(nextNonces) { + t.Fatal("encrypted data is missing") + } + for i, nonce := range firstNonces { + if bytes.Equal(nonce, nextNonces[i]) { + t.Fatal("GCM nonce was reused across encryptions") + } + } + }) + } +} + +func samlGCMNonces(t *testing.T, element document.Element) [][]byte { + t.Helper() + if element.Name() == (document.Name{Space: xmlenc.Namespace, Local: "EncryptedData"}) { + for _, child := range element.Children() { + if child.Name() == (document.Name{Space: xmlenc.Namespace, Local: "CipherData"}) { + raw, err := base64.StdEncoding.DecodeString(child.Children()[0].Text()) + if err != nil || len(raw) < 28 { + t.Fatalf("XMLENC 1.1 GCM IV/tag: %v", err) + } + return [][]byte{raw[:12]} + } + } + } + var nonces [][]byte + for _, child := range element.Children() { + nonces = append(nonces, samlGCMNonces(t, child)...) + } + return nonces +} + +func TestEncryptedResponsePoliciesAndFailureOrder(t *testing.T) { + c, recipient, wrong := testcert.New(t), encryptionKey(t), encryptionKey(t) + spec := responseSpec() + spec.Encryption = &protocol.EncryptionOptions{Targets: protocol.EncryptAssertion, Recipient: &recipient.PublicKey} + d, err := protocol.SignResponse(t.Context(), spec, protocol.SigningKey{Key: c.LeafKey, CertificateDER: c.Leaf.Raw}) + if err != nil { + t.Fatal(err) + } + for _, tc := range []struct { + name string + policy *protocol.DecryptionOptions + ok bool + }{ + {"missing key", nil, false}, + {"wrong key", &protocol.DecryptionOptions{Keys: []crypto.Decrypter{wrong}}, false}, + {"rollover", &protocol.DecryptionOptions{Keys: []crypto.Decrypter{wrong, recipient}}, true}, + {"attempt budget", &protocol.DecryptionOptions{Keys: []crypto.Decrypter{wrong, recipient}, MaxDecryptions: 1}, false}, + {"algorithm mismatch", &protocol.DecryptionOptions{Keys: []crypto.Decrypter{recipient}, Data: xmlenc.Options{DataAlgorithm: xmlenc.AES128GCM}}, false}, + } { + t.Run(tc.name, func(t *testing.T) { + opts := options() + opts.Decryption = tc.policy + result, err := protocol.VerifyResponse(t.Context(), d, []crypto.PublicKey{c.LeafKey.Public()}, opts) + if tc.ok && (err != nil || result == nil) || !tc.ok && (err == nil || result != nil) { + t.Fatalf("result=%v err=%v", result, err) + } + }) + } + provider := &decryptCounter{PrivateKey: recipient} + opts := options() + opts.Decryption = &protocol.DecryptionOptions{Keys: []crypto.Decrypter{provider}} + // Authentication failure must not reach any private-key operation. + if result, err := protocol.VerifyResponse(t.Context(), d, []crypto.PublicKey{c.TSAKey.Public()}, opts); result != nil || err == nil || provider.calls != 0 { + t.Fatalf("invalid outer signature reached decryptor: %v, calls %d", err, provider.calls) + } + // Ciphertext integrity is still checked when an authenticated sender signs + // a corrupted tag; no decrypted assertion may be returned. + corrupt := encryptedEnvelope(t, d, c, func(root *tree.Node) { + value := find(root, "CipherValue") + value.Children[0].Data = "AAAA" + value.Children[0].Data[4:] + }) + if result, err := protocol.VerifyResponse(t.Context(), corrupt, []crypto.PublicKey{c.LeafKey.Public()}, opts); result != nil || !errors.Is(err, protocol.ErrEncryption) { + t.Fatalf("corrupted ciphertext accepted: %v", err) + } + spec.Encryption = nil + plain, err := protocol.SignResponse(t.Context(), spec, protocol.SigningKey{Key: c.LeafKey, CertificateDER: c.Leaf.Raw}) + if err != nil { + t.Fatal(err) + } + for _, target := range []protocol.EncryptionTargets{protocol.EncryptAssertion, protocol.EncryptNameID, protocol.EncryptAttributes} { + opts.RequireEncryption = target + if result, err := protocol.VerifyResponse(t.Context(), plain, []crypto.PublicKey{c.LeafKey.Public()}, opts); result != nil || !errors.Is(err, protocol.ErrEncryption) { + t.Fatalf("plaintext downgrade accepted for %d: %v", target, err) + } + } + ctx, cancel := context.WithCancel(t.Context()) + cancel() + if _, err := protocol.VerifyResponse(ctx, d, []crypto.PublicKey{c.LeafKey.Public()}, opts); !errors.Is(err, context.Canceled) { + t.Fatalf("cancellation lost: %v", err) + } +} + +func TestEncryptedNameIDIsAPolicyValue(t *testing.T) { + c, recipient := testcert.New(t), encryptionKey(t) + spec := responseSpec() + spec.Assertion.NameIDFormat = protocol.NameIDEncrypted + spec.Encryption = &protocol.EncryptionOptions{Targets: protocol.EncryptNameID, Recipient: &recipient.PublicKey} + if result, err := protocol.SignResponse(t.Context(), spec, protocol.SigningKey{Key: c.LeafKey}); result != nil || err == nil { + t.Fatal("NameIDPolicy's encrypted marker was emitted as an inner NameID format") + } +} + +func TestEncryptedResponseAlternativeEncodings(t *testing.T) { + c, recipient := testcert.New(t), encryptionKey(t) + spec := responseSpec() + spec.Encryption = &protocol.EncryptionOptions{Targets: protocol.EncryptAssertion, Recipient: &recipient.PublicKey} + d, err := protocol.SignResponse(t.Context(), spec, protocol.SigningKey{Key: c.LeafKey, CertificateDER: c.Leaf.Raw}) + if err != nil { + t.Fatal(err) + } + for _, tc := range []struct { + name string + change func(*tree.Node) + ok bool + }{ + {"optional Type absent", func(root *tree.Node) { + n := find(root, "EncryptedData") + for i, attr := range n.Attrs { + if attr.Name.Local == "Type" { + n.Attrs = append(n.Attrs[:i], n.Attrs[i+1:]...) + break + } + } + }, true}, + {"Type must be Element", func(root *tree.Node) { set(find(root, "EncryptedData"), "Type", xmlenc.ContentType) }, false}, + {"addressed encrypted key", func(root *tree.Node) { + set(find(root, "EncryptedData"), "Id", "_data") + list := xmlutil.Node("xenc", xmlenc.Namespace, "ReferenceList") + ref := xmlutil.Node("xenc", xmlenc.Namespace, "DataReference") + xmlutil.Set(ref, "URI", "#_data") + xmlutil.Add(list, ref) + xmlutil.Add(find(root, "EncryptedKey"), list) + }, true}, + {"wrong data reference", func(root *tree.Node) { + set(find(root, "EncryptedData"), "Id", "_data") + list := xmlutil.Node("xenc", xmlenc.Namespace, "ReferenceList") + ref := xmlutil.Node("xenc", xmlenc.Namespace, "DataReference") + xmlutil.Set(ref, "URI", "#_other") + xmlutil.Add(list, ref) + xmlutil.Add(find(root, "EncryptedKey"), list) + }, false}, + {"multiple recipient keys", func(root *tree.Node) { + key := find(root, "EncryptedKey") + snapshot, err := tree.Clone(t.Context(), tree.FromNodes([]*tree.Node{tree.ExtractSubtree(key)}, document.DefaultLimits())) + if err != nil { + t.Fatal(err) + } + other := tree.Root(snapshot) + value := find(other, "CipherValue").Children[0] + value.Data = "AAAA" + value.Data[4:] + info := key.Parent + other.Parent = info + info.Children = append([]*tree.Node{other}, info.Children...) + }, true}, + {"sibling key", func(root *tree.Node) { + data := find(root, "EncryptedData") + for i, child := range data.Children { + if child.Name.Local == "KeyInfo" { + key := tree.ImportSubtree(child.Children[0]) + data.Children = append(data.Children[:i], data.Children[i+1:]...) + xmlutil.Add(find(root, "EncryptedAssertion"), key) + break + } + } + }, true}, + } { + t.Run(tc.name, func(t *testing.T) { + changed := encryptedEnvelope(t, d, c, tc.change) + opts := options() + opts.Decryption = &protocol.DecryptionOptions{Keys: []crypto.Decrypter{recipient}} + result, err := protocol.VerifyResponse(t.Context(), changed, []crypto.PublicKey{c.LeafKey.Public()}, opts) + if tc.ok && (err != nil || result == nil) || !tc.ok && (err == nil || result != nil) { + t.Fatalf("result=%v err=%v", result, err) + } + }) + } +} + +func TestEncryptedCBCAndUnsignedAssertion(t *testing.T) { + if fips140.Enabled() { + t.Skip("legacy CBC is disabled in FIPS mode") + } + c, recipient := testcert.New(t), encryptionKey(t) + spec := responseSpec() + spec.Encryption = &protocol.EncryptionOptions{Targets: protocol.EncryptAssertion, Recipient: &recipient.PublicKey, Data: xmlenc.Options{DataAlgorithm: xmlenc.AES128CBC, AllowLegacy: true}} + d, err := protocol.SignResponse(t.Context(), spec, protocol.SigningKey{Key: c.LeafKey, CertificateDER: c.Leaf.Raw}) + if err != nil { + t.Fatal(err) + } + opts := options() + provider := &decryptCounter{PrivateKey: recipient} + opts.Decryption = &protocol.DecryptionOptions{Keys: []crypto.Decrypter{provider}, Data: spec.Encryption.Data} + if _, err := protocol.VerifyResponse(t.Context(), d, []crypto.PublicKey{c.LeafKey.Public()}, opts); err != nil { + t.Fatal(err) + } + provider.calls = 0 + opts.AllowUnsignedResponse = true + if result, err := protocol.VerifyResponse(t.Context(), removeSignatures(t, d, false), []crypto.PublicKey{c.LeafKey.Public()}, opts); result != nil || !errors.Is(err, protocol.ErrEncryption) || provider.calls != 0 { + t.Fatalf("unauthenticated CBC reached private provider: %v", err) + } + spec.Encryption = nil + plain, err := protocol.SignResponse(t.Context(), spec, protocol.SigningKey{Key: c.LeafKey, CertificateDER: c.Leaf.Raw}) + if err != nil { + t.Fatal(err) + } + outerOnly := encryptedEnvelope(t, removeSignatures(t, plain, true), c, func(*tree.Node) {}) + opts = options() + if result, err := protocol.VerifyResponse(t.Context(), outerOnly, []crypto.PublicKey{c.LeafKey.Public()}, opts); result != nil || !errors.Is(err, protocol.ErrSignature) { + t.Fatal("unsigned assertion accepted without policy") + } + opts.AllowUnsignedAssertion = true + if result, err := protocol.VerifyResponse(t.Context(), outerOnly, []crypto.PublicKey{c.LeafKey.Public()}, opts); err != nil || result == nil || !result.ResponseAuthenticated() { + t.Fatalf("authenticated envelope-only POST rejected: %v", err) + } + opts.AllowUnsignedResponse = true + if result, err := protocol.VerifyResponse(t.Context(), removeSignatures(t, outerOnly, true), []crypto.PublicKey{c.LeafKey.Public()}, opts); result != nil || !errors.Is(err, protocol.ErrSignature) { + t.Fatal("both signatures absent accepted") + } +} + +func encryptedEnvelope(t *testing.T, d *document.Document, c testcert.Chain, change func(*tree.Node)) *document.Document { + t.Helper() + d = removeSignatures(t, d, false) + snapshot, err := tree.Clone(t.Context(), d) + if err != nil { + t.Fatal(err) + } + change(tree.Root(snapshot)) + snapshot, err = tree.Reparse(t.Context(), snapshot, document.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + snapshot, err = xmlsig.Sign(t.Context(), snapshot, snapshot.Root(), xmlsig.SigningKey{Key: c.LeafKey, CertificateDER: c.Leaf.Raw, Method: xmldsig.ECDSASHA256}, true) + if err != nil { + t.Fatal(err) + } + return snapshot +} + +type decryptCounter struct { + *rsa.PrivateKey + calls int +} + +func (p *decryptCounter) Decrypt(random io.Reader, ciphertext []byte, opts crypto.DecrypterOpts) ([]byte, error) { + p.calls++ + return p.PrivateKey.Decrypt(random, ciphertext, opts) +} diff --git a/saml/protocol/exchange.go b/saml/protocol/exchange.go new file mode 100644 index 0000000..fab2211 --- /dev/null +++ b/saml/protocol/exchange.go @@ -0,0 +1,700 @@ +package protocol + +import ( + "context" + "crypto" + "slices" + "time" + "unicode/utf8" + + "github.com/skarm/xmlsec/internal/lexicon" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/tree" + "github.com/skarm/xmlsec/internal/xmlchar" + "github.com/skarm/xmlsec/saml/binding" + "github.com/skarm/xmlsec/saml/internal/profile" + "github.com/skarm/xmlsec/saml/internal/xmlsig" + "github.com/skarm/xmlsec/saml/internal/xmlutil" + "github.com/skarm/xmlsec/saml/metadata" + "github.com/skarm/xmlsec/xmldsig" +) + +// Local SAML vocabulary tokens shared by protocol producers and parsers. Each call site still verifies the assertion/protocol namespace or resolves the QName prefix. +const ( + xmlNameConsent = "Consent" + xmlNameStatusMessage = "StatusMessage" +) + +// Name identifiers and status messages have separate UTF-8 byte ceilings; the +// status-code count bounds nested subordinate codes in both wire directions. +const ( + maximumNameIdentifierBytes = 4096 + maximumStatusMessageBytes = 4096 + maximumStatusSubcodes = 8 +) + +const ( + // Requester indicates a request that could not be performed as sent. + Requester = "urn:oasis:names:tc:SAML:2.0:status:Requester" + // Responder indicates a failure at the responding provider. + Responder = "urn:oasis:names:tc:SAML:2.0:status:Responder" + // UnknownPrincipal indicates an unrecognized subject. + UnknownPrincipal = "urn:oasis:names:tc:SAML:2.0:status:UnknownPrincipal" + // PartialLogout indicates that not all sessions could be terminated. + PartialLogout = "urn:oasis:names:tc:SAML:2.0:status:PartialLogout" + // UnsupportedBinding indicates an unsupported requested protocol binding. + UnsupportedBinding = "urn:oasis:names:tc:SAML:2.0:status:UnsupportedBinding" +) + +// MessageHeader identifies a locally produced protocol message. An empty ID is +// replaced by a cryptographically random XML ID. Destination is a trusted endpoint. +type MessageHeader struct { + // ID is the XML message identifier. Empty generates a cryptographically random + // ID when signing. + ID string + // Issuer is the sending provider's entity ID. + Issuer string + // Destination is the recipient endpoint selected from trusted configuration. + Destination string + // IssueInstant is the message creation time. + IssueInstant time.Time + // Consent records the SAML consent identifier; it does not itself authorize an operation. + Consent string + // Extensions retains foreign-namespace extension XML for application processing. + Extensions *document.Document +} + +// ExchangeOptions fixes the peer, endpoint, time and credentials for a control +// exchange. Responses additionally require ExpectedRequestID. It supplies no trust +// from KeyInfo or from received endpoints. Replay protection belongs to the caller. +type ExchangeOptions struct { + // Issuer is the required expected peer entity ID. + Issuer string + // Destination is the trusted local endpoint used to check the received + // destination. + Destination string + // ExpectedRequestID is the request ID required for response correlation. + ExpectedRequestID string + // Binding selects a trusted SOAP transport context, in which Destination + // may be absent. A present value is always checked. Signatures are required. + Binding string + // At is the explicit verification time and must be nonzero. + At time.Time + // Limits bounds message structure and freshness. Zero fields select the + // protocol defaults. + Limits Limits + // Policy restricts signature and reference processing algorithms. + Policy xmldsig.Policy + // Decryption permits processing encrypted identifiers after signature + // verification. Nil supplies no decryption keys. + Decryption *DecryptionOptions + // redirect records that the Redirect binding has already authenticated the + // request. + redirect bool +} + +// NameIdentifier retains the full SAML NameID, including both qualifiers and the +// service provider's alias. Compare the complete value when identifying a subject. +// An absent Format has the SAML unspecified format semantics. +type NameIdentifier struct { + // Value is the identifier's text content. + Value string + // Format is the identifier format URI. Empty has the SAML unspecified-format + // semantics. + Format string + // NameQualifier scopes the identifier to its naming authority. + NameQualifier string + // SPNameQualifier scopes the identifier to a service provider or affiliation. + SPNameQualifier string + // SPProvidedID is the service provider's alias for the identifier. + SPProvidedID string +} + +// Validate checks bounded NameID structure and the persistent, transient and +// entity format constraints. Other format-specific semantics and the association +// with a real principal remain the application's responsibility. +func (id NameIdentifier) Validate() error { + const ( + maximumEntityNameRunes = 1024 + maximumOpaqueNameIDRunes = 256 + ) + + if id.Value == "" || !utf8.ValidString(id.Value) || len(id.Value) > maximumNameIdentifierBytes || id.Format != "" && (!profile.URI(id.Format) || id.Format == NameIDEncrypted) || len(id.NameQualifier) > maximumNameQualifierBytes || len(id.SPNameQualifier) > maximumNameQualifierBytes || len(id.SPProvidedID) > maximumNameIdentifierBytes { + return ErrProfile + } + + for _, text := range []string{id.Value, id.Format, id.NameQualifier, id.SPNameQualifier, id.SPProvidedID} { + if !utf8.ValidString(text) { + return ErrProfile + } + + for _, r := range text { + if !xmlchar.IsChar(r) { + return ErrProfile + } + } + } + + switch id.Format { + case NameIDPersistent: + if utf8.RuneCountInString(id.Value) > maximumOpaqueNameIDRunes { + return ErrProfile + } + case NameIDTransient: + if utf8.RuneCountInString(id.Value) > maximumOpaqueNameIDRunes || !xmlchar.IsValidNCName(id.Value) { + return ErrProfile + } + case lexicon.SAMLNameIDFormatEntity: + if utf8.RuneCountInString(id.Value) > maximumEntityNameRunes || !profile.URI(id.Value) || id.NameQualifier != "" || id.SPNameQualifier != "" || id.SPProvidedID != "" { + return ErrProfile + } + } + + return nil +} + +// NewPersistentNameID generates an opaque pairwise identifier. Persist its +// association with the application's account and the two providers; never derive +// a persistent-format identifier from a public username (Core 8.3.7). +func NewPersistentNameID(idp, sp string) (NameIdentifier, error) { + if !profile.URI(idp) || !profile.URI(sp) { + return NameIdentifier{}, ErrProfile + } + + value, err := profile.NewID() + if err != nil { + return NameIdentifier{}, err + } + + return NameIdentifier{Value: value, Format: NameIDPersistent, NameQualifier: idp, SPNameQualifier: sp}, nil +} + +// Status describes an authenticated SAML status. Subcodes are ordered outermost +// first. Success with PartialLogout is a partial outcome, not complete logout. +type Status struct { + // Code is the top-level SAML status code URI. + Code string + // Subcodes contains subordinate status code URIs, ordered outermost first. + Subcodes []string + // Message is optional diagnostic text and grants no authority. + Message string +} + +// StatusResponse produces LogoutResponse, ManageNameIDResponse or ArtifactResponse. +// Message is permitted only for ArtifactResponse. An empty artifact response is a +// successful resolution with no message, not an authentication result. +type StatusResponse struct { + MessageHeader + // Kind selects LogoutResponse, ManageNameIDResponse or ArtifactResponse. + Kind string + // InResponseTo is the exact request ID being answered. + InResponseTo string + // Status is the protocol outcome to sign. + Status Status + // Message is the optional protocol payload of an ArtifactResponse. Other + // response kinds must leave it nil. + Message *document.Document + // SOAPContext signs the response in the final SOAP namespace context. + SOAPContext bool +} + +// VerifiedStatusResponse is an immutable authenticated control response. An +// embedded artifact message still requires its own protocol/profile validation. +type VerifiedStatusResponse struct { + header MessageHeader + kind, requestID string + status Status + message *document.Document +} + +// Header returns the authenticated message header. +func (r *VerifiedStatusResponse) Header() MessageHeader { return r.header } + +// Kind returns the response element's local name. +func (r *VerifiedStatusResponse) Kind() string { return r.kind } + +// InResponseTo returns the correlated request ID. +func (r *VerifiedStatusResponse) InResponseTo() string { return r.requestID } + +// Status returns an owned copy of the status. +func (r *VerifiedStatusResponse) Status() Status { + s := r.status + s.Subcodes = slices.Clone(s.Subcodes) + + return s +} + +// Message returns the immutable embedded artifact message, or nil for an empty +// resolution. The artifact envelope signature does not validate its assertions. +func (r *VerifiedStatusResponse) Message() *document.Document { return r.message } + +// SignStatusResponse signs a correlated response with a locally provisioned key. +func SignStatusResponse(ctx context.Context, s StatusResponse, key SigningKey) (*document.Document, error) { + if s.Kind == lexicon.SAMLElementResponse && (s.Status.Code == "" || s.Status.Code == Success) { + return nil, ErrProfile + } + + if !responseKind(s.Kind) || !profile.ID(s.InResponseTo) || s.Kind != ArtifactResponse && s.Message != nil { + return nil, ErrProfile + } + + n, err := exchangeRoot(s.MessageHeader, s.Kind) + if err != nil { + return nil, err + } + + xmlutil.Set(n, nameInResponseTo, s.InResponseTo) + + if s.SOAPContext { + n.NS = append(n.NS, tree.Namespace{Prefix: prefixSOAP, URI: binding.SOAPNamespace}) + } + + status, err := statusNode(s.Status) + if err != nil { + return nil, err + } + + xmlutil.Add(n, status) + + if s.Message != nil { + if s.Message.Root().Name().Space != ProtocolNamespace || s.Status.Code != "" && s.Status.Code != Success { + return nil, ErrProfile + } + + _, child := tree.Unwrap(s.Message.Root()) + xmlutil.Add(n, tree.ImportSubtree(child)) + } + + return signExchange(ctx, n, key, nil) +} + +// VerifyStatusResponse verifies the signature, peer, destination, freshness, +// status structure and request correlation, including non-success responses. +func VerifyStatusResponse(ctx context.Context, d *document.Document, keys []crypto.PublicKey, o ExchangeOptions) (*VerifiedStatusResponse, error) { + const ( + maximumStatusPayloadChildren = 2 + ) + + if d == nil || !responseKind(d.Root().Name().Local) || !profile.ID(o.ExpectedRequestID) { + return nil, ErrProfile + } + + kind := d.Root().Name().Local + + h, cs, err := verifyExchange(ctx, d, kind, keys, o, nil, true) + if err != nil { + return nil, err + } + + if xmlutil.Attr(d.Root(), nameInResponseTo) != o.ExpectedRequestID { + return nil, ErrCorrelation + } + + if len(cs) == 0 || len(cs) > maximumStatusPayloadChildren || len(cs) == maximumStatusPayloadChildren && kind != ArtifactResponse { + return nil, ErrProfile + } + + s, err := readStatus(cs[0]) + if err != nil { + return nil, err + } + + r := &VerifiedStatusResponse{header: h, kind: kind, requestID: o.ExpectedRequestID, status: s} + if len(cs) == maximumStatusPayloadChildren { + if s.Code != Success || cs[1].Name().Space != ProtocolNamespace { + return nil, ErrProfile + } + + r.message, err = tree.ScopeDocument(d, cs[1]) + } + + return r, err +} + +// VerifyRedirectStatusResponse authenticates the exact Redirect query signature. +// RelayState is returned separately and must match the initiating exchange. +func VerifyRedirectStatusResponse(ctx context.Context, query string, keys []crypto.PublicKey, o ExchangeOptions, limits binding.Options) (*VerifiedStatusResponse, *string, error) { + d, relay, err := exchangeRedirect(ctx, query, keys, o, limits, binding.Response) + if err != nil { + return nil, nil, err + } + + o.redirect = true + r, err := VerifyStatusResponse(ctx, d, nil, o) + + return r, relay, err +} + +func responseKind(kind string) bool { + return kind == LogoutResponse || kind == ManageNameIDResponse || kind == ArtifactResponse || kind == lexicon.SAMLElementResponse +} + +func exchangeRoot(h MessageHeader, kind string) (*tree.Node, error) { + return messageRoot(h, kind, false) +} + +func soapExchangeRoot(h MessageHeader, kind string) (*tree.Node, error) { + return messageRoot(h, kind, true) +} + +func messageRoot(h MessageHeader, kind string, soap bool) (*tree.Node, error) { + if !profile.URI(h.Issuer) || (!soap || h.Destination != "") && !profile.Endpoint(h.Destination, true) || h.IssueInstant.IsZero() { + return nil, ErrProfile + } + + if h.ID == "" { + var err error + + h.ID, err = profile.NewID() + if err != nil { + return nil, err + } + } + + if !profile.ID(h.ID) { + return nil, ErrProfile + } + + n := xmlutil.Node(prefixProtocol, ProtocolNamespace, kind) + + n.NS = []tree.Namespace{{Prefix: prefixProtocol, URI: ProtocolNamespace}, {Prefix: prefixAssertion, URI: AssertionNamespace}, {Prefix: lexicon.PrefixDSig, URI: xmlutil.DS}} + for _, a := range [][2]string{{lexicon.SAMLAttrID, h.ID}, {nameVersion, version20}, {nameIssueInstant, profile.Time(h.IssueInstant)}} { + xmlutil.Set(n, a[0], a[1]) + } + + if h.Destination != "" { + xmlutil.Set(n, nameDestination, h.Destination) + } + + if h.Consent != "" { + if !profile.URI(h.Consent) { + return nil, ErrProfile + } + + xmlutil.Set(n, xmlNameConsent, h.Consent) + } + + i := xmlutil.Node(prefixAssertion, AssertionNamespace, xmlNameIssuer) + xmlutil.Text(i, h.Issuer) + xmlutil.Add(n, i) + + if h.Extensions != nil { + if err := validateExtensions(h.Extensions.Root()); err != nil { + return nil, err + } + + appendDocument(n, h.Extensions) + } + + return n, nil +} + +func signExchange(ctx context.Context, root *tree.Node, key SigningKey, encryption *EncryptionOptions) (*document.Document, error) { + d, err := xmlutil.Document(ctx, root) + if err != nil { + return nil, err + } + + if encryption != nil { + o, err := CloneEncryption(encryption) + if err != nil || o.Recipient == nil || o.Targets != EncryptNameID { + return nil, ErrEncryption + } + + for _, name := range []struct{ ns, plain, wrapper string }{{AssertionNamespace, xmlNameNameID, xmlNameEncryptedID}, {ProtocolNamespace, xmlNameNewID, xmlNameNewEncryptedID}} { + target, err := xmlutil.Direct(d.Root(), xmlutil.Name(name.ns, name.plain)) + if err != nil { + continue + } + + d, err = encryptSAML(ctx, d, target, name.wrapper, o) + if err != nil { + return nil, err + } + } + } + + k, err := signing(key) + if err != nil { + return nil, err + } + + return xmlsig.Sign(ctx, d, d.Root(), k, true) +} + +func verifyExchange(ctx context.Context, d *document.Document, kind string, keys []crypto.PublicKey, o ExchangeOptions, optional []string, response bool) (MessageHeader, []document.Element, error) { + var zero MessageHeader + if ctx == nil || d == nil || o.At.IsZero() || !profile.URI(o.Issuer) || !(o.Binding == metadata.SOAP && o.Destination == "") && !profile.Endpoint(o.Destination, true) { + return zero, nil, ErrProfile + } + + l, err := normalize(o.Limits) + if err != nil { + return zero, nil, err + } + + attrs := append(slices.Clone(optional), xmlNameConsent) + if response { + attrs = append(attrs, nameInResponseTo) + } + + root := d.Root() + required := []string{lexicon.SAMLAttrID, nameVersion, nameIssueInstant} + + if o.Binding == metadata.SOAP { + attrs = append(attrs, nameDestination) + } else { + required = append(required, nameDestination) + } + + destination, present := root.Attribute(document.Name{Local: nameDestination}) + if xmlutil.Attrs(root, required, attrs) != nil || present && (o.Destination == "" || destination != o.Destination) { + return zero, nil, ErrProfile + } + + id, instant, err := header(root, kind, o.At, l) + if err != nil { + return zero, nil, err + } + + cs, err := xmlutil.Children(root) + if err != nil || len(cs) == 0 || issuer(cs[0], o.Issuer) != nil { + return zero, nil, ErrProfile + } + + position := unsignedMessagePrefixChildren + + if o.redirect { + if len(cs) > 1 && cs[1].Name() == xmlutil.Name(xmlutil.DS, lexicon.DSigElementSignature) { + return zero, nil, ErrProfile + } + } else { + if len(cs) < signedMessagePrefixChildren || cs[1].Name() != xmlutil.Name(xmlutil.DS, lexicon.DSigElementSignature) { + return zero, nil, ErrSignature + } + + if err := targetSignature(ctx, d, root, keys, o.Policy); err != nil { + return zero, nil, err + } + + position = signedMessagePrefixChildren + } + + extensions, position, err := exchangeExtensions(d, cs, position) + if err != nil { + return zero, nil, err + } + + consent := xmlutil.Attr(root, xmlNameConsent) + if consent != "" && !profile.URI(consent) { + return zero, nil, ErrProfile + } + + return MessageHeader{ID: id, Issuer: o.Issuer, Destination: o.Destination, IssueInstant: instant, Consent: consent, Extensions: extensions}, cs[position:], ctx.Err() +} + +func validateExtensions(e document.Element) error { + const ( + maximumExtensionElements = 128 + ) + + if e.Name() != xmlutil.Name(ProtocolNamespace, xmlNameExtensions) || xmlutil.Attrs(e, nil, nil) != nil { + return ErrProfile + } + + cs, err := xmlutil.Children(e) + if err != nil || len(cs) == 0 || len(cs) > maximumExtensionElements { + return ErrProfile + } + + for _, c := range cs { + if c.Name().Space == "" || c.Name().Space == ProtocolNamespace { + return ErrProfile + } + } + + return nil +} + +func exchangeRedirect(ctx context.Context, query string, keys []crypto.PublicKey, o ExchangeOptions, limits binding.Options, kind binding.Kind) (*document.Document, *string, error) { + if len(keys) == 0 || len(keys) > maximumRedirectVerificationKeys { + return nil, nil, ErrSignature + } + + limits.Policy = o.Policy + for _, key := range keys { + r, err := binding.VerifyRedirect(ctx, query, key, limits) + if err != nil { + if ctx != nil && ctx.Err() != nil { + return nil, nil, ctx.Err() + } + + continue + } + + if r.Kind() != kind { + return nil, nil, ErrProfile + } + + var relay *string + if s, ok := r.RelayState(); ok { + relay = &s + } + + return r.Document(), relay, nil + } + + return nil, nil, ErrSignature +} + +func nameIDNode(id NameIdentifier) (*tree.Node, error) { + if err := id.Validate(); err != nil { + return nil, err + } + + n := xmlutil.Node(prefixAssertion, AssertionNamespace, xmlNameNameID) + + for _, a := range [][2]string{{nameFormat, id.Format}, {nameNameQualifier, id.NameQualifier}, {nameSPNameQualifier, id.SPNameQualifier}, {nameSPProvidedID, id.SPProvidedID}} { + if a[1] != "" { + xmlutil.Set(n, a[0], a[1]) + } + } + + xmlutil.Text(n, id.Value) + + return n, nil +} + +func readNameID(e document.Element) (NameIdentifier, error) { + var id NameIdentifier + if e.Name() != xmlutil.Name(AssertionNamespace, xmlNameNameID) || xmlutil.Attrs(e, nil, []string{nameFormat, nameNameQualifier, nameSPNameQualifier, nameSPProvidedID}) != nil { + return id, ErrProfile + } + + s, err := xmlutil.SimpleText(e, maximumNameIdentifierBytes) + if err != nil { + return id, ErrProfile + } + + id = NameIdentifier{Value: s, Format: xmlutil.Attr(e, nameFormat), NameQualifier: xmlutil.Attr(e, nameNameQualifier), SPNameQualifier: xmlutil.Attr(e, nameSPNameQualifier), SPProvidedID: xmlutil.Attr(e, nameSPProvidedID)} + _, err = nameIDNode(id) + + return id, err +} + +func statusNode(s Status) (*tree.Node, error) { + const ( + statusVersionMismatch = "urn:oasis:names:tc:SAML:2.0:status:VersionMismatch" + ) + + if s.Code == "" { + s.Code = Success + } + + if !slices.Contains([]string{Success, Requester, Responder, statusVersionMismatch}, s.Code) || len(s.Subcodes) > maximumStatusSubcodes || len(s.Message) > maximumStatusMessageBytes { + return nil, ErrProfile + } + + n := xmlutil.Node(prefixProtocol, ProtocolNamespace, xmlNameStatus) + parent := n + + for _, value := range append([]string{s.Code}, s.Subcodes...) { + if !profile.URI(value) { + return nil, ErrProfile + } + + c := xmlutil.Node(prefixProtocol, ProtocolNamespace, xmlNameStatusCode) + xmlutil.Set(c, xmlNameValue, value) + xmlutil.Add(parent, c) + parent = c + } + + if s.Message != "" { + m := xmlutil.Node(prefixProtocol, ProtocolNamespace, xmlNameStatusMessage) + xmlutil.Text(m, s.Message) + xmlutil.Add(n, m) + } + + return n, nil +} + +func readStatus(e document.Element) (Status, error) { + const ( + maximumStatusChildren = 3 + xmlNameStatusDetail = "StatusDetail" + ) + + var s Status + if e.Name() != xmlutil.Name(ProtocolNamespace, xmlNameStatus) || xmlutil.Attrs(e, nil, nil) != nil { + return s, ErrProfile + } + + cs, err := xmlutil.Children(e) + if err != nil || len(cs) == 0 || len(cs) > maximumStatusChildren { + return s, ErrProfile + } + + c := cs[0] + for depth := 0; ; depth++ { + if depth > maximumStatusSubcodes || c.Name() != xmlutil.Name(ProtocolNamespace, xmlNameStatusCode) || xmlutil.Attrs(c, []string{xmlNameValue}, nil) != nil { + return s, ErrProfile + } + + v := xmlutil.Attr(c, xmlNameValue) + if depth == 0 { + s.Code = v + } else { + s.Subcodes = append(s.Subcodes, v) + } + + cc, err := xmlutil.Children(c) + if err != nil || len(cc) > 1 { + return s, ErrProfile + } + + if len(cc) == 0 { + break + } + + c = cc[0] + } + + i := 1 + if i < len(cs) && cs[i].Name() == xmlutil.Name(ProtocolNamespace, xmlNameStatusMessage) { + s.Message, err = xmlutil.Simple(cs[i], maximumStatusMessageBytes) + if err != nil { + return s, ErrProfile + } + + i++ + } + + if i < len(cs) { + if i != len(cs)-1 || cs[i].Name() != xmlutil.Name(ProtocolNamespace, xmlNameStatusDetail) || xmlutil.Attrs(cs[i], nil, nil) != nil { + return s, ErrProfile + } + + if _, err := xmlutil.Children(cs[i]); err != nil { + return s, ErrProfile + } + } + + _, err = statusNode(s) + + return s, err +} + +func exchangeExtensions(d *document.Document, cs []document.Element, position int) (*document.Document, int, error) { + if position >= len(cs) || cs[position].Name() != xmlutil.Name(ProtocolNamespace, xmlNameExtensions) { + return nil, position, nil + } + + if err := validateExtensions(cs[position]); err != nil { + return nil, position, err + } + + extension, err := tree.ScopeDocument(d, cs[position]) + + return extension, position + 1, err +} diff --git a/saml/protocol/hok.go b/saml/protocol/hok.go new file mode 100644 index 0000000..3876cf3 --- /dev/null +++ b/saml/protocol/hok.go @@ -0,0 +1,336 @@ +package protocol + +import ( + "bytes" + "context" + "crypto/x509" + "encoding/base64" + "net" + "strings" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/lexicon" + "github.com/skarm/xmlsec/internal/signature" + "github.com/skarm/xmlsec/internal/tree" + "github.com/skarm/xmlsec/saml/internal/profile" + "github.com/skarm/xmlsec/saml/internal/xmlutil" +) + +// HoK xsi:type must resolve to this SAML assertion type; matching the QName does not replace exact certificate-byte possession checks. +const ( + confirmationDataTypeName = "KeyInfoConfirmationDataType" +) + +// HoK certificates are bounded by decoded DER bytes, independently of the +// larger base64 text ceiling used while reading XML. +const ( + maximumHoKCertificateBytes = 64 << 10 +) + +// HolderOfKey is the SAML proof-of-possession subject-confirmation method. +const HolderOfKey = "urn:oasis:names:tc:SAML:2.0:cm:holder-of-key" + +const xsiNamespace = lexicon.NamespaceXSI + +// HolderOfKeyData constructs the same confirmation data as the assertion +// producer, including for assertions encrypted before an ECP header is formed. +func HolderOfKeyData(ctx context.Context, certificate []byte, recipient, requestID string) (document.Element, error) { + if !profile.Endpoint(recipient, false) || !profile.ID(requestID) { + return document.Element{}, ErrProfile + } + + n := xmlutil.Node(prefixAssertion, AssertionNamespace, xmlNameSubjectConfirmationData) + + n.NS = []tree.Namespace{{Prefix: prefixAssertion, URI: AssertionNamespace}, {Prefix: lexicon.PrefixDSig, URI: xmlutil.DS}} + if err := setConfirmationKey(n, certificate); err != nil { + return document.Element{}, err + } + + xmlutil.Set(n, nameRecipient, recipient) + xmlutil.Set(n, nameInResponseTo, requestID) + + d, err := xmlutil.Document(ctx, n) + if err != nil { + return document.Element{}, err + } + + return d.Root(), nil +} + +func confirmationNode(s Response) (*tree.Node, error) { + conf := xmlutil.Node(prefixAssertion, AssertionNamespace, xmlNameSubjectConfirmation) + data := xmlutil.Node(prefixAssertion, AssertionNamespace, xmlNameSubjectConfirmationData) + + method := Bearer + if len(s.Assertion.HolderOfKeyCertificate) != 0 { + method = HolderOfKey + + if err := setConfirmationKey(data, s.Assertion.HolderOfKeyCertificate); err != nil { + return nil, err + } + } + + xmlutil.Set(conf, nameMethod, method) + xmlutil.Set(data, nameRecipient, s.Destination) + xmlutil.Set(data, nameInResponseTo, s.InResponseTo) + + if method == Bearer { + xmlutil.Set(data, nameNotOnOrAfter, profile.Time(s.Assertion.NotOnOrAfter)) + } + + xmlutil.Add(conf, data) + + return conf, nil +} + +func setConfirmationKey(data *tree.Node, raw []byte) error { + const ( + prefixSchemaInstance = "xsi" + qualifiedConfirmationDataTypeSuffix = ":" + confirmationDataTypeName + ) + + if len(raw) == 0 || len(raw) > maximumHoKCertificateBytes { + return ErrProfile + } + + cert, err := x509.ParseCertificate(raw) + if err != nil || signature.Key(cert.PublicKey) != nil { + return ErrProfile + } + + data.NS = append(data.NS, tree.Namespace{Prefix: prefixSchemaInstance, URI: xsiNamespace}) + data.Attrs = append(data.Attrs, tree.Attribute{Name: xmlutil.Name(xsiNamespace, lexicon.AttrType), Prefix: prefixSchemaInstance, Value: prefixAssertion + qualifiedConfirmationDataTypeSuffix}) + info := xmlutil.Node(lexicon.PrefixDSig, xmlutil.DS, lexicon.DSigElementKeyInfo) + x509data := xmlutil.Node(lexicon.PrefixDSig, xmlutil.DS, lexicon.DSigElementX509Data) + leaf := xmlutil.Node(lexicon.PrefixDSig, xmlutil.DS, lexicon.DSigElementX509Certificate) + xmlutil.Text(leaf, base64.StdEncoding.EncodeToString(cert.Raw)) + xmlutil.Add(x509data, leaf) + xmlutil.Add(info, x509data) + xmlutil.Add(data, info) + + return nil +} + +func parseHoKConfirmation(c document.Element, o ResponseOptions, l Limits, r *VerifiedAssertion) error { + if o.Proof == nil && len(o.producedCertificate) == 0 || xmlutil.Attrs(c, []string{nameMethod}, nil) != nil || xmlutil.Attr(c, nameMethod) != HolderOfKey { + return ErrConditions + } + + cs, err := xmlutil.Children(c) + if err != nil || len(cs) != 1 || cs[0].Name() != xmlutil.Name(AssertionNamespace, xmlNameSubjectConfirmationData) { + return ErrProfile + } + + data := cs[0] + // HoK timestamps are optional and have certificate-specific semantics. Our + // producer omits them and the assertion Conditions supply the validity bound. + if err := confirmationAttributes(data); err != nil { + return err + } + + if recipient, present := data.Attribute(document.Name{Local: nameRecipient}); present && recipient != o.Destination { + return ErrCorrelation + } + + if request, present := data.Attribute(document.Name{Local: nameInResponseTo}); present && request != o.ExpectedRequestID { + return ErrCorrelation + } + + expected := o.producedCertificate + if o.Proof != nil { + expected = o.Proof.Certificate() + } + + if err := MatchConfirmationCertificate(data, expected); err != nil { + return ErrConditions + } + + if err := confirmationTimes(data, o, l, r); err != nil { + return err + } + + r.confirmation = HolderOfKey + r.hokCertificate = bytes.Clone(expected) + + return nil +} + +// MatchConfirmationCertificate implements the X509Certificate matching form. +// Multiple KeyInfo elements are alternatives; no PKIX/name trust is inferred. +func MatchConfirmationCertificate(data document.Element, expected []byte) error { + if data.Name() != xmlutil.Name(AssertionNamespace, xmlNameSubjectConfirmationData) || confirmationAttributes(data) != nil { + return ErrProfile + } + + certificates, err := confirmationCertificates(data) + if err != nil { + return err + } + + for _, certificate := range certificates { + if bytes.Equal(certificate, expected) { + return nil + } + } + + return ErrConditions +} + +func confirmationCertificates(data document.Element) ([][]byte, error) { + const ( + maximumHoKKeyInfos = 8 + ) + + cs, err := xmlutil.Children(data) + if err != nil || len(cs) == 0 || len(cs) > maximumHoKKeyInfos { + return nil, ErrProfile + } + + var result [][]byte + + for _, info := range cs { + if info.Name() != xmlutil.Name(xmlutil.DS, lexicon.DSigElementKeyInfo) || xmlutil.Attrs(info, nil, []string{lexicon.SecurityAttrID}) != nil { + return nil, ErrProfile + } + + x, err := xmlutil.Children(info) + if err != nil || len(x) != 1 || x[0].Name() != xmlutil.Name(xmlutil.DS, lexicon.DSigElementX509Data) || xmlutil.Attrs(x[0], nil, nil) != nil { + return nil, ErrProfile + } + + certificates, err := confirmationX509Data(x[0]) + if err != nil { + return nil, err + } + + result = append(result, certificates...) + } + + if result == nil { + return nil, ErrProfile + } + + return result, nil +} + +func confirmationX509Data(data document.Element) ([][]byte, error) { + const ( + maximumEncodedHoKCertificateBytes = 128 << 10 + maximumHoKCertificateFields = 8 + ) + + fields, err := xmlutil.Children(data) + if err != nil || len(fields) == 0 || len(fields) > maximumHoKCertificateFields { + return nil, ErrProfile + } + + var result [][]byte + + for _, field := range fields { + if field.Name() == xmlutil.Name(xmlutil.DS, lexicon.DSigElementX509CRL) { + return nil, ErrProfile + } + + if field.Name() != xmlutil.Name(xmlutil.DS, lexicon.DSigElementX509Certificate) { + continue + } + + text, err := xmlutil.Simple(field, maximumEncodedHoKCertificateBytes) + if err != nil { + return nil, err + } + + text = strings.Map(func(r rune) rune { + if r == ' ' || r == '\t' || r == '\r' || r == '\n' { + return -1 + } + + return r + }, text) + + raw, err := base64.StdEncoding.Strict().DecodeString(text) + if err != nil || len(raw) == 0 || len(raw) > maximumHoKCertificateBytes { + return nil, ErrProfile + } + // HoK 2.5 compares decoded bytes; parsing/re-encoding would change it. + result = append(result, raw) + } + + return result, nil +} + +func confirmationAttributes(data document.Element) error { + for _, a := range data.Attributes() { + if a.Name == xmlutil.Name(xsiNamespace, lexicon.AttrType) { + prefix, local, ok := strings.Cut(a.Value, ":") + if !ok { + local = prefix + prefix = "" + } + + ns, _ := data.Namespace(prefix) + if ns != AssertionNamespace || local != confirmationDataTypeName { + return ErrProfile + } + + continue + } + + if a.Name.Space != "" || !allowed(a.Name.Local, []string{nameRecipient, nameInResponseTo, nameNotBefore, nameNotOnOrAfter, nameAddress}) { + return ErrProfile + } + } + + return nil +} + +func confirmationTimes(data document.Element, o ResponseOptions, l Limits, r *VerifiedAssertion) error { + if addr, ok := data.Attribute(document.Name{Local: nameAddress}); ok { + actual, expected := net.ParseIP(addr), net.ParseIP(o.ExpectedAddress) + if actual == nil || expected == nil || !actual.Equal(expected) { + return ErrConditions + } + } + + r.expires = o.At.Add(l.MaxAssertionLifetime + l.ClockSkew) + + if value, present := data.Attribute(document.Name{Local: nameNotOnOrAfter}); present { + end, err := profile.ParseTime(value) + if err != nil || !o.At.Add(-l.ClockSkew).Before(end) { + return ErrConditions + } + + r.expires = end + } + + if value, present := data.Attribute(document.Name{Local: nameNotBefore}); present { + start, err := profile.ParseTime(value) + if err != nil || o.At.Add(l.ClockSkew).Before(start) { + return ErrConditions + } + + if _, endPresent := data.Attribute(document.Name{Local: nameNotOnOrAfter}); endPresent && !start.Before(r.expires) { + return ErrConditions + } + } + + return nil +} + +// SubjectConfirmationMethod returns the authenticated confirmation method. +func (r *VerifiedAssertion) SubjectConfirmationMethod() string { + if r == nil { + return "" + } + + return r.confirmation +} + +// HolderOfKeyCertificate returns the exact matched certificate, not a trust chain. +func (r *VerifiedAssertion) HolderOfKeyCertificate() []byte { + if r == nil { + return nil + } + + return bytes.Clone(r.hokCertificate) +} diff --git a/saml/protocol/hok_test.go b/saml/protocol/hok_test.go new file mode 100644 index 0000000..56ebb55 --- /dev/null +++ b/saml/protocol/hok_test.go @@ -0,0 +1,143 @@ +package protocol_test + +import ( + "bytes" + "crypto" + "encoding/base64" + "strings" + "testing" + "time" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/testutil/testcert" + "github.com/skarm/xmlsec/internal/tree" + "github.com/skarm/xmlsec/saml/binding" + "github.com/skarm/xmlsec/saml/proof" + "github.com/skarm/xmlsec/saml/protocol" +) + +func TestHoKCertificateMatchingForm(t *testing.T) { + c := testcert.New(t) + valid := `` + base64.StdEncoding.EncodeToString(c.Leaf.Raw) + `` + other := strings.Replace(valid, base64.StdEncoding.EncodeToString(c.Leaf.Raw), base64.StdEncoding.EncodeToString(c.TSA.Raw), 1) + for _, test := range []struct { + name, content string + accept bool + }{ + {"exact", valid, true}, + {"alternative KeyInfo", other + valid, true}, + {"different certificate", other, false}, + {"no certificate", `AQID`, false}, + {"CRL forbidden", strings.Replace(valid, ``, `AQID`, 1), false}, + {"multiple X509Data forbidden", strings.Replace(valid, ``, ``, 1), false}, + {"bare key forbidden", ``, false}, + {"unknown extension with certificate", strings.Replace(valid, ``, ``, 1), true}, + {"XML base64 whitespace", strings.Replace(valid, base64.StdEncoding.EncodeToString(c.Leaf.Raw), "\n "+base64.StdEncoding.EncodeToString(c.Leaf.Raw)+"\t\r", 1), true}, + } { + t.Run(test.name, func(t *testing.T) { + d, err := document.Parse(t.Context(), []byte(``+test.content+``), document.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + if err := protocol.MatchConfirmationCertificate(d.Root(), c.Leaf.Raw); (err == nil) != test.accept { + t.Fatalf("match=%v, want=%v", err, test.accept) + } + }) + } +} + +// Re-sign malformed semantic controls, so rejection cannot be attributed to a +// broken cryptographic signature or to using our producer as the only parser. +func TestHoKConfirmationConditions(t *testing.T) { + c := testcert.New(t) + spec := responseSpec() + spec.Assertion.HolderOfKeyCertificate = c.TSA.Raw + d, err := protocol.SignResponse(t.Context(), spec, protocol.SigningKey{Key: c.LeafKey, CertificateDER: c.Leaf.Raw}) + if err != nil { + t.Fatal(err) + } + for _, test := range []struct { + name string + mutate func(*tree.Node) + accept bool + }{ + {"valid", func(*tree.Node) {}, true}, + {"alternative confirmation", func(root *tree.Node) { + conf := find(root, "SubjectConfirmation") + // Import from the unchanged source; descendants are borrowed + // read-only until resign reparses the private draft. + alternative := tree.ImportSubtree(find(tree.Root(d), "SubjectConfirmation")) + alternative.Parent = conf.Parent + conf.Parent.Children = append(conf.Parent.Children, alternative) + set(find(conf, "SubjectConfirmationData"), "Recipient", "https://other.test/acs") + }, true}, + {"recipient", func(root *tree.Node) { + set(find(root, "SubjectConfirmationData"), "Recipient", "https://attacker.test/acs") + }, false}, + {"request", func(root *tree.Node) { set(find(root, "SubjectConfirmationData"), "InResponseTo", "_wrong") }, false}, + {"expired", func(root *tree.Node) { + set(find(root, "SubjectConfirmationData"), "NotOnOrAfter", spec.IssueInstant.Add(-time.Second).Format(time.RFC3339Nano)) + }, false}, + {"future", func(root *tree.Node) { + set(find(root, "SubjectConfirmationData"), "NotBefore", spec.IssueInstant.Add(time.Minute).Format(time.RFC3339Nano)) + }, false}, + {"address mismatch", func(root *tree.Node) { set(find(root, "SubjectConfirmationData"), "Address", "192.0.2.8") }, false}, + {"address match", func(root *tree.Node) { set(find(root, "SubjectConfirmationData"), "Address", "192.0.2.9") }, true}, + {"wrong xsi type", func(root *tree.Node) { + data := find(root, "SubjectConfirmationData") + for i := range data.Attrs { + if data.Attrs[i].Name.Local == "type" { + data.Attrs[i].Value = "saml:SubjectConfirmationDataType" + } + } + }, false}, + } { + t.Run(test.name, func(t *testing.T) { + changed := resign(t, d, c, test.mutate) + envelope, err := binding.WrapSOAP(t.Context(), changed) + if err != nil { + t.Fatal(err) + } + signed, err := proof.SignCS01CompatXML(t.Context(), envelope, proof.Signing{Key: c.TSAKey, Certificate: c.TSA.Raw}) + if err != nil { + t.Fatal(err) + } + raw, _ := signed.XML(t.Context()) + p, err := proof.VerifyCS01CompatXML(t.Context(), raw, proof.Options{}) + if err != nil { + t.Fatal(err) + } + body, err := p.Payload(t.Context()) + if err != nil { + t.Fatal(err) + } + o := options() + o.Proof = p + o.RequireHolderOfKey = true + o.ExpectedAddress = "192.0.2.9" + result, err := protocol.VerifyResponse(t.Context(), body, []crypto.PublicKey{c.LeafKey.Public()}, o) + if (err == nil) != test.accept { + t.Fatalf("verify=%v, want=%v", err, test.accept) + } + if test.accept && !bytes.Equal(result.HolderOfKeyCertificate(), c.TSA.Raw) { + t.Fatal("wrong certificate") + } + if _, err := protocol.VerifyResponse(t.Context(), changed, []crypto.PublicKey{c.LeafKey.Public()}, o); err == nil { + t.Fatal("proof transferred to another payload snapshot") + } + }) + } + if _, err := protocol.VerifyResponse(t.Context(), d, []crypto.PublicKey{c.LeafKey.Public()}, options()); err == nil { + t.Fatal("HoK accepted without proof") + } + bearerSpec := responseSpec() + bearer, err := protocol.SignResponse(t.Context(), bearerSpec, protocol.SigningKey{Key: c.LeafKey}) + if err != nil { + t.Fatal(err) + } + o := options() + o.RequireHolderOfKey = true + if _, err := protocol.VerifyResponse(t.Context(), bearer, []crypto.PublicKey{c.LeafKey.Public()}, o); err == nil { + t.Fatal("bearer downgrade accepted") + } +} diff --git a/saml/protocol/legacy_test.go b/saml/protocol/legacy_test.go new file mode 100644 index 0000000..4c03256 --- /dev/null +++ b/saml/protocol/legacy_test.go @@ -0,0 +1,104 @@ +package protocol_test + +import ( + "bytes" + "crypto" + "crypto/fips140" + "errors" + "testing" + + "github.com/skarm/xmlsec/c14n" + "github.com/skarm/xmlsec/internal/testutil/testcert" + "github.com/skarm/xmlsec/saml/protocol" + "github.com/skarm/xmlsec/xmldsig" + "github.com/skarm/xmlsec/xmlenc" +) + +// The 2005 SAML Conformance section 4 baseline is available only through +// explicit algorithm policy. It does not replace modern default algorithms. +func TestLegacySAMLAlgorithms(t *testing.T) { + if fips140.Enabled() { + t.Skip("legacy algorithms are disabled in FIPS mode") + } + signer, recipient := encryptionKey(t), encryptionKey(t) + policy := xmldsig.LegacyVerificationPolicy() + key := protocol.SigningKey{Key: signer, SignatureMethod: xmldsig.RSASHA1, DigestMethod: xmldsig.SHA1, Policy: policy} + if _, err := protocol.SignResponse(t.Context(), responseSpec(), key); !errors.Is(err, xmldsig.ErrPolicy) { + t.Fatalf("verification permission enabled legacy generation: %v", err) + } + key.Policy.AllowLegacySigning = true + for _, algorithm := range []string{xmlenc.AES128CBC, xmlenc.AES256CBC, xmlenc.TripleDESCBC} { + for _, transport := range []string{xmlenc.RSA15, xmlenc.RSAOAEP10} { + spec := responseSpec() + data := xmlenc.Options{DataAlgorithm: algorithm, AllowLegacy: true} + if transport == xmlenc.RSA15 { + data.KeyTransportAlgorithm = transport + } else { + data.OAEP = &xmlenc.OAEPParameters{Algorithm: transport, Hash: crypto.SHA1, AllowLegacy: true} + } + spec.Encryption = &protocol.EncryptionOptions{Targets: protocol.EncryptAssertion | protocol.EncryptNameID | protocol.EncryptAttributes, Recipient: &recipient.PublicKey, Data: data} + d, err := protocol.SignResponse(t.Context(), spec, key) + if err != nil { + t.Fatal(transport, err) + } + opts := options() + opts.Policy = policy + opts.Decryption = &protocol.DecryptionOptions{Keys: []crypto.Decrypter{recipient}, Data: data} + result, err := protocol.VerifyResponse(t.Context(), d, []crypto.PublicKey{signer.Public()}, opts) + if err != nil || result == nil || result.NameID() != "alice" { + t.Fatalf("legacy SAML exchange %s: %v", transport, err) + } + if result, err := protocol.VerifyResponse(t.Context(), d, []crypto.PublicKey{signer.Public()}, options()); result != nil || err == nil { + t.Fatal("default policy accepted a legacy exchange") + } + } + } +} + +func TestSAMLSharedSecretAndCanonicalXML(t *testing.T) { + if fips140.Enabled() { + t.Skip("legacy HMAC-SHA1 is disabled in FIPS mode") + } + provisioned := bytes.Repeat([]byte{0x47}, 32) + secret, err := protocol.NewSharedSecret(provisioned) + if err != nil { + t.Fatal(err) + } + clear(provisioned) + wrong, err := protocol.NewSharedSecret(bytes.Repeat([]byte{0x48}, 32)) + if err != nil { + t.Fatal(err) + } + for _, method := range []string{xmldsig.HMACSHA1, xmldsig.HMACSHA256} { + policy := xmldsig.HMACPolicy() + if method == xmldsig.HMACSHA1 { + policy = xmldsig.LegacyHMACPolicy() + } + key := protocol.SigningKey{Secret: &secret, SignatureMethod: method, Canonicalization: c14n.Canonical10, Policy: policy} + if method == xmldsig.HMACSHA1 { + key.DigestMethod = xmldsig.SHA1 + } + d, err := protocol.SignResponse(t.Context(), responseSpec(), key) + if err != nil { + t.Fatal(method, err) + } + opts := options() + opts.Policy = policy + result, err := protocol.VerifyResponse(t.Context(), d, []crypto.PublicKey{wrong, secret}, opts) + if err != nil || result == nil || result.NameID() != "alice" { + t.Fatalf("explicit SAML MAC: %v", err) + } + for _, keys := range [][]crypto.PublicKey{{wrong}, {bytes.Repeat([]byte{0x47}, 32)}, {testcert.Key(t).Public()}} { + if result, err := protocol.VerifyResponse(t.Context(), d, keys, opts); result != nil || err == nil { + t.Fatal("wrong secret, byte-slice or public key became trusted HMAC credential") + } + } + if result, err := protocol.VerifyResponse(t.Context(), d, []crypto.PublicKey{secret}, options()); result != nil || err == nil { + t.Fatal("default policy accepted HMAC") + } + key.Key = testcert.Key(t) + if _, err := protocol.SignResponse(t.Context(), responseSpec(), key); err == nil { + t.Fatal("ambiguous public-key/MAC credentials accepted") + } + } +} diff --git a/saml/protocol/mapping.go b/saml/protocol/mapping.go new file mode 100644 index 0000000..7ccb6a6 --- /dev/null +++ b/saml/protocol/mapping.go @@ -0,0 +1,475 @@ +package protocol + +import ( + "context" + "crypto" + + "github.com/skarm/xmlsec/internal/lexicon" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/tree" + "github.com/skarm/xmlsec/saml/internal/profile" + "github.com/skarm/xmlsec/saml/internal/xmlutil" +) + +// Mapping payloads contain the original identifier and policy/result identifier +// after the common issuer/signature prefix has been removed. +const ( + mappingPayloadChildren = 2 +) + +const ( + // InvalidNameIDPolicy indicates that the requested identifier policy cannot be satisfied. + InvalidNameIDPolicy = "urn:oasis:names:tc:SAML:2.0:status:InvalidNameIDPolicy" + // NoAuthnContext indicates that no requested authentication context can be satisfied. + NoAuthnContext = "urn:oasis:names:tc:SAML:2.0:status:NoAuthnContext" +) + +// NameIDPolicy selects the desired identifier format, target federation namespace +// and permission to establish a new identifier. It does not authorize disclosure. +type NameIDPolicy struct { + // Format is the requested identifier format URI. + Format string + // SPNameQualifier identifies the target service provider or affiliation + // namespace. + SPNameQualifier string + // AllowCreate permits creation of an identifier subject to application policy. + AllowCreate bool +} + +// NameIDMappingRequest maps an existing principal identifier into another format +// or federation namespace. Encryption, if present, encrypts the input identifier +// for the identity provider. It must select EncryptNameID. +type NameIDMappingRequest struct { + MessageHeader + // NameID is the existing principal identifier to map. + NameID NameIdentifier + // Policy selects the desired identifier format and target namespace. + Policy NameIDPolicy + // Encryption optionally encrypts the input identifier and must select + // EncryptNameID. + Encryption *EncryptionOptions +} + +// VerifiedNameIDMappingRequest contains an authenticated mapping request. +type VerifiedNameIDMappingRequest struct { + request NameIDMappingRequest + originalIdentifier *document.Document +} + +// Request returns the authenticated plaintext input and requested policy. +func (r *VerifiedNameIDMappingRequest) Request() NameIDMappingRequest { return r.request } + +// ErrorIdentifier returns the original identifier representation for an error +// response. Core's NameIDMappingResponse schema requires an identifier even on +// error. Echoing the original EncryptedID preserves its confidentiality and does +// not assert a successful mapping. Error results never expose a mapped identifier. +func (r *VerifiedNameIDMappingRequest) ErrorIdentifier() *document.Document { + return r.originalIdentifier +} + +// NameIDMappingResponse contains either a plaintext NameID or an opaque +// EncryptedID for the target provider. The requester need not hold its decryption +// key: Core 3.8 permits forwarding the encrypted identifier to another provider. +// Encryption encrypts a plaintext NameID for that target and must select EncryptNameID. +type NameIDMappingResponse struct { + MessageHeader + // InResponseTo identifies the mapping request being answered. + InResponseTo string + // Status records whether mapping succeeded. + Status Status + // Identifier contains the mapped NameID or opaque EncryptedID. Error responses + // carry the original identifier representation. + Identifier *document.Document + // Encryption optionally encrypts a plaintext result for the target provider and + // must select EncryptNameID. + Encryption *EncryptionOptions +} + +// VerifiedNameIDMappingResponse authenticates the mapping result. An EncryptedID +// is not decrypted or promoted to a local principal merely by verifying its envelope. +type VerifiedNameIDMappingResponse struct { + header MessageHeader + requestID string + status Status + identifier *document.Document +} + +// Header returns the authenticated envelope header. +func (r *VerifiedNameIDMappingResponse) Header() MessageHeader { return r.header } + +// InResponseTo returns the authenticated request correlation. +func (r *VerifiedNameIDMappingResponse) InResponseTo() string { return r.requestID } + +// Status returns a copy of the authenticated status. +func (r *VerifiedNameIDMappingResponse) Status() Status { + s := r.status + s.Subcodes = append([]string(nil), s.Subcodes...) + + return s +} + +// Identifier returns the immutable NameID or opaque EncryptedID, or nil on error. +func (r *VerifiedNameIDMappingResponse) Identifier() *document.Document { return r.identifier } + +// NameIDDocument builds an immutable identifier document for mapping responses. +func NameIDDocument(ctx context.Context, id NameIdentifier) (*document.Document, error) { + n, err := nameIDNode(id) + if err != nil { + return nil, err + } + + n.NS = []tree.Namespace{{Prefix: prefixAssertion, URI: AssertionNamespace}} + + return xmlutil.Document(ctx, n) +} + +// EncryptMappedIdentifier encrypts a plaintext NameID for the target provider. +func EncryptMappedIdentifier(ctx context.Context, d *document.Document, o *EncryptionOptions) (*document.Document, error) { + if ctx == nil || d == nil || o == nil || o.Targets != EncryptNameID { + return nil, ErrEncryption + } + + if _, err := readNameID(d.Root()); err != nil { + return nil, err + } + + p, err := CloneEncryption(o) + if err != nil || p.Recipient == nil { + return nil, ErrEncryption + } + + return encryptSAML(ctx, d, d.Root(), xmlNameEncryptedID, p) +} + +// EncryptMappedAssertion implements Profiles 7.4.2.1: a signed statement-free +// assertion carries the identifier with conditions limiting use at the target. +func EncryptMappedAssertion(ctx context.Context, a *VerifiedAuthorityAssertion, o *EncryptionOptions) (*document.Document, error) { + if a == nil || !a.subject.Valid() || len(a.statements) != 0 || o == nil || o.Targets != EncryptNameID { + return nil, ErrEncryption + } + + p, err := CloneEncryption(o) + if err != nil || p.Recipient == nil { + return nil, ErrEncryption + } + + return encryptSAML(ctx, a.signed, a.signed.Root(), xmlNameEncryptedID, p) +} + +// DecryptMappedAssertion verifies the signed, limited-use assertion carried by +// an EncryptedID. Its conditions and subject remain distinct from a bare NameID. +func DecryptMappedAssertion(ctx context.Context, d *document.Document, keys []crypto.PublicKey, o AssertionOptions) (*VerifiedAuthorityAssertion, error) { + return decryptMappedAssertion(ctx, d, keys, o, false) +} + +// DecryptAssertion opens the exact authenticated mapping result and verifies its +// limited-use assertion. All private-key attempts share one decryption budget. +func (r *VerifiedNameIDMappingResponse) DecryptAssertion(ctx context.Context, keys []crypto.PublicKey, o AssertionOptions) (*VerifiedAuthorityAssertion, error) { + if r == nil || r.identifier == nil { + return nil, ErrProfile + } + + return decryptMappedAssertion(ctx, r.identifier, keys, o, true) +} + +func decryptMappedAssertion(ctx context.Context, d *document.Document, keys []crypto.PublicKey, o AssertionOptions, authenticated bool) (*VerifiedAuthorityAssertion, error) { + if ctx == nil || d == nil || d.Root().Name() != xmlutil.Name(AssertionNamespace, xmlNameEncryptedID) { + return nil, ErrEncryption + } + + s, err := newDecryptionState(o.Decryption) + if err != nil { + return nil, err + } + + plain, err := s.decryptSAML(ctx, d, d.Root(), nameAssertion, authenticated) + if err != nil { + return nil, err + } + + l, err := normalize(o.Limits) + if err != nil { + return nil, err + } + + a, err := verifyAuthorityAssertion(ctx, plain, keys, o, l, s) + if err != nil { + return nil, err + } + + if !a.subject.Valid() || len(a.statements) != 0 { + return nil, ErrProfile + } + + return a, nil +} + +// SignNameIDMappingRequest authenticates the input identifier and target policy. +func SignNameIDMappingRequest(ctx context.Context, s NameIDMappingRequest, key SigningKey) (*document.Document, error) { + n, err := soapExchangeRoot(s.MessageHeader, lexicon.SAMLElementNameIDMappingRequest) + if err != nil { + return nil, err + } + + id, err := nameIDNode(s.NameID) + if err != nil { + return nil, err + } + + xmlutil.Add(n, id) + + p, err := mappingPolicyNode(s.Policy) + if err != nil { + return nil, err + } + + xmlutil.Add(n, p) + + return signRichExchange(ctx, n, key, s.Encryption) +} + +// VerifyNameIDMappingRequest authenticates before decrypting an existing ID. +func VerifyNameIDMappingRequest(ctx context.Context, d *document.Document, keys []crypto.PublicKey, o ExchangeOptions) (*VerifiedNameIDMappingRequest, error) { + h, cs, err := verifyExchange(ctx, d, lexicon.SAMLElementNameIDMappingRequest, keys, o, nil, false) + if err != nil { + return nil, err + } + + if len(cs) != mappingPayloadChildren { + return nil, ErrProfile + } + + original, err := tree.ScopeDocument(d, cs[0]) + if err != nil { + return nil, err + } + + d, err = decryptControl(ctx, d, cs, o.Decryption) + if err != nil { + return nil, err + } + + cs, err = controlChildren(d, false) + if err != nil || len(cs) != mappingPayloadChildren { + return nil, ErrProfile + } + + id, err := readNameID(cs[0]) + if err != nil { + return nil, err + } + + p, err := readMappingPolicy(cs[1]) + if err != nil { + return nil, err + } + + return &VerifiedNameIDMappingRequest{request: NameIDMappingRequest{MessageHeader: h, NameID: id, Policy: p}, originalIdentifier: original}, ctx.Err() +} + +// SignNameIDMappingResponse signs success or error. Core 3.8.2 requires exactly +// one identifier in both cases. For an error, use the request's ErrorIdentifier; +// consumers do not interpret it as a mapped identity. +func SignNameIDMappingResponse(ctx context.Context, s NameIDMappingResponse, key SigningKey) (*document.Document, error) { + if !profile.ID(s.InResponseTo) { + return nil, ErrProfile + } + + n, err := soapExchangeRoot(s.MessageHeader, lexicon.SAMLElementNameIDMappingResponse) + if err != nil { + return nil, err + } + + xmlutil.Set(n, nameInResponseTo, s.InResponseTo) + + status, err := statusNode(s.Status) + if err != nil { + return nil, err + } + + xmlutil.Add(n, status) + + success := s.Status.Code == "" || s.Status.Code == Success + if s.Identifier == nil || !success && s.Encryption != nil { + return nil, ErrProfile + } + + if err := mappedIdentifier(s.Identifier.Root()); err != nil { + return nil, err + } + + appendDocument(n, s.Identifier) + + return signRichExchange(ctx, n, key, s.Encryption) +} + +// VerifyNameIDMappingResponse validates the peer, signature, time and request +// correlation. Opaque encrypted output can be forwarded without decrypting it. +func VerifyNameIDMappingResponse(ctx context.Context, d *document.Document, keys []crypto.PublicKey, o ExchangeOptions) (*VerifiedNameIDMappingResponse, error) { + if !profile.ID(o.ExpectedRequestID) { + return nil, ErrProfile + } + + h, cs, err := verifyExchange(ctx, d, lexicon.SAMLElementNameIDMappingResponse, keys, o, nil, true) + if err != nil { + return nil, err + } + + if xmlutil.Attr(d.Root(), nameInResponseTo) != o.ExpectedRequestID { + return nil, ErrCorrelation + } + + if len(cs) != mappingPayloadChildren { + return nil, ErrProfile + } + + s, err := readStatus(cs[0]) + if err != nil { + return nil, err + } + + r := &VerifiedNameIDMappingResponse{header: h, requestID: o.ExpectedRequestID, status: s} + if s.Code == Success { + if err := mappedIdentifier(cs[1]); err != nil { + return nil, err + } + + r.identifier, err = tree.ScopeDocument(d, cs[1]) + } else if err := mappedIdentifier(cs[1]); err != nil { + return nil, err + } + + return r, err +} + +// DecryptMappedNameID opens an identifier only with the target's explicit keys. +// Assertion-wrapped limited-use identifiers must instead be processed under +// the target application's assertion/condition policy; they are not bare names. +func DecryptMappedNameID(ctx context.Context, d *document.Document, o *DecryptionOptions) (NameIdentifier, error) { + return decryptMappedNameID(ctx, d, o, false) +} + +// DecryptNameID opens this exact authenticated mapping result. Its verified +// envelope also permits explicitly authorized legacy CBC/RSA-v1.5 policies. +func (r *VerifiedNameIDMappingResponse) DecryptNameID(ctx context.Context, o *DecryptionOptions) (NameIdentifier, error) { + if r == nil || r.identifier == nil { + return NameIdentifier{}, ErrProfile + } + + return decryptMappedNameID(ctx, r.identifier, o, true) +} + +func decryptMappedNameID(ctx context.Context, d *document.Document, o *DecryptionOptions, authenticated bool) (NameIdentifier, error) { + if ctx == nil || d == nil { + return NameIdentifier{}, ErrProfile + } + + if d.Root().Name() == xmlutil.Name(AssertionNamespace, xmlNameNameID) { + return readNameID(d.Root()) + } + + if d.Root().Name() != xmlutil.Name(AssertionNamespace, xmlNameEncryptedID) { + return NameIdentifier{}, ErrProfile + } + + state, err := newDecryptionState(o) + if err != nil { + return NameIdentifier{}, err + } + + d, err = state.decryptSAML(ctx, d, d.Root(), xmlNameNameID, authenticated) + if err != nil { + return NameIdentifier{}, err + } + + return readNameID(d.Root()) +} + +func mappedIdentifier(e document.Element) error { + switch e.Name() { + case xmlutil.Name(AssertionNamespace, xmlNameNameID): + _, err := readNameID(e) + return err + case xmlutil.Name(AssertionNamespace, xmlNameEncryptedID): + if xmlutil.Attrs(e, nil, nil) != nil { + return ErrProfile + } + + _, _, err := encryptedSAMLRecipients(e) + + return err + default: + return ErrProfile + } +} + +func mappingPolicyNode(p NameIDPolicy) (*tree.Node, error) { + if p.Format != "" && !profile.URI(p.Format) || len(p.SPNameQualifier) > maximumNameQualifierBytes { + return nil, ErrProfile + } + + n := xmlutil.Node(prefixProtocol, ProtocolNamespace, xmlNameNameIDPolicy) + if p.Format != "" { + xmlutil.Set(n, nameFormat, p.Format) + } + + if p.SPNameQualifier != "" { + xmlutil.Set(n, nameSPNameQualifier, p.SPNameQualifier) + } + + if p.AllowCreate { + xmlutil.Set(n, xmlNameAllowCreate, lexicon.ValueTrue) + } + + return n, nil +} + +func readMappingPolicy(e document.Element) (NameIDPolicy, error) { + p := NameIDPolicy{Format: xmlutil.Attr(e, nameFormat), SPNameQualifier: xmlutil.Attr(e, nameSPNameQualifier)} + if e.Name() != xmlutil.Name(ProtocolNamespace, xmlNameNameIDPolicy) || xmlutil.Attrs(e, nil, []string{nameFormat, nameSPNameQualifier, xmlNameAllowCreate}) != nil || !xmlutil.EmptyContent(e) { + return p, ErrProfile + } + + v, err := xmlutil.BoolAttr(e, xmlNameAllowCreate) + if err != nil { + return p, err + } + + p.AllowCreate = v + _, err = mappingPolicyNode(p) + + return p, err +} + +// CheckMappedPolicy checks plaintext output against the requested format and +// federation. For opaque EncryptedID the authoritative backend must enforce the +// policy before encrypting; the requesting entity may not hold the target's key. +func CheckMappedPolicy(ctx context.Context, d *document.Document, p NameIDPolicy) error { + if ctx == nil || d == nil { + return ErrProfile + } + + if err := mappedIdentifier(d.Root()); err != nil { + return err + } + + if d.Root().Name().Local == xmlNameEncryptedID { + return ctx.Err() + } + + id, err := readNameID(d.Root()) + if err != nil { + return err + } + + if p.Format == NameIDEncrypted { + return ErrEncryption + } + + if p.Format != "" && p.Format != NameIDUnspecified && id.Format != p.Format || p.SPNameQualifier != "" && id.SPNameQualifier != p.SPNameQualifier { + return ErrConditions + } + + return ctx.Err() +} diff --git a/saml/protocol/mapping_test.go b/saml/protocol/mapping_test.go new file mode 100644 index 0000000..4b3185d --- /dev/null +++ b/saml/protocol/mapping_test.go @@ -0,0 +1,162 @@ +package protocol_test + +import ( + "crypto" + "crypto/fips140" + "testing" + "time" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/testutil/testcert" + "github.com/skarm/xmlsec/saml/metadata" + "github.com/skarm/xmlsec/saml/protocol" + "github.com/skarm/xmlsec/xmlenc" +) + +// Core 3.8 / Profiles 7: mapping targets a different federation and may return +// an identifier the requester cannot decrypt. This does not rename its source ID. +func TestNameIDMapping(t *testing.T) { + c := testcert.New(t) + key := protocol.SigningKey{Key: c.LeafKey} + keys := []crypto.PublicKey{c.LeafKey.Public()} + target := encryptionKey(t) + q := protocol.NameIDMappingRequest{ID: "_mapping", Issuer: sp, Destination: sso, IssueInstant: testcert.Now(), NameID: protocol.NameIdentifier{Value: "source-alice", Format: protocol.NameIDPersistent, SPNameQualifier: sp}, Policy: protocol.NameIDPolicy{Format: protocol.NameIDPersistent, SPNameQualifier: "https://target.test/entity", AllowCreate: true}, Encryption: &protocol.EncryptionOptions{Targets: protocol.EncryptNameID, Recipient: &target.PublicKey}} + d, err := protocol.SignNameIDMappingRequest(t.Context(), q, key) + if err != nil { + t.Fatal(err) + } + o := protocol.ExchangeOptions{Issuer: sp, Destination: sso, Binding: metadata.SOAP, At: testcert.Now(), Decryption: &protocol.DecryptionOptions{Keys: []crypto.Decrypter{target}}} + v, err := protocol.VerifyNameIDMappingRequest(t.Context(), d, keys, o) + if err != nil || v.Request().NameID != q.NameID || v.Request().Policy != q.Policy { + t.Fatalf("mapping request: %v", err) + } + bad := o + bad.Decryption = nil + if _, err = protocol.VerifyNameIDMappingRequest(t.Context(), d, keys, bad); err == nil { + t.Fatal("encrypted input accepted without key") + } + mapped := protocol.NameIdentifier{Value: "pairwise-target-alice", Format: q.Policy.Format, SPNameQualifier: q.Policy.SPNameQualifier} + plain, err := protocol.NameIDDocument(t.Context(), mapped) + if err != nil { + t.Fatal(err) + } + opaque, err := protocol.EncryptMappedIdentifier(t.Context(), plain, &protocol.EncryptionOptions{Targets: protocol.EncryptNameID, Recipient: &target.PublicKey}) + if err != nil { + t.Fatal(err) + } + for _, id := range []*document.Document{plain, opaque} { + response, err := protocol.SignNameIDMappingResponse(t.Context(), protocol.NameIDMappingResponse{ID: "_mapped", Issuer: idp, IssueInstant: testcert.Now(), InResponseTo: q.ID, Identifier: id}, key) + if err != nil { + t.Fatal(err) + } + options := protocol.ExchangeOptions{Issuer: idp, ExpectedRequestID: q.ID, At: testcert.Now(), Binding: metadata.SOAP} + result, err := protocol.VerifyNameIDMappingResponse(t.Context(), response, keys, options) + if err != nil || result.Identifier() == nil { + t.Fatalf("mapping response: %v", err) + } + if err = protocol.CheckMappedPolicy(t.Context(), result.Identifier(), q.Policy); err != nil { + t.Fatal(err) + } + got, err := protocol.DecryptMappedNameID(t.Context(), result.Identifier(), &protocol.DecryptionOptions{Keys: []crypto.Decrypter{target}}) + if err != nil || got != mapped { + t.Fatalf("target identifier: %v", err) + } + options.ExpectedRequestID = "_different" + if _, err = protocol.VerifyNameIDMappingResponse(t.Context(), response, keys, options); err == nil { + t.Fatal("uncorrelated mapping accepted") + } + } + wrong := q.Policy + wrong.Format = protocol.NameIDEmail + if err = protocol.CheckMappedPolicy(t.Context(), plain, wrong); err == nil { + t.Fatal("wrong output format accepted") + } + wrong = q.Policy + wrong.SPNameQualifier = sp + if err = protocol.CheckMappedPolicy(t.Context(), plain, wrong); err == nil { + t.Fatal("wrong target federation accepted") + } + failure, err := protocol.SignNameIDMappingResponse(t.Context(), protocol.NameIDMappingResponse{Issuer: idp, IssueInstant: testcert.Now(), InResponseTo: q.ID, Status: protocol.Status{Code: protocol.Requester, Subcodes: []string{protocol.UnknownPrincipal}}, Identifier: v.ErrorIdentifier()}, key) + if err != nil { + t.Fatal(err) + } + failed, err := protocol.VerifyNameIDMappingResponse(t.Context(), failure, keys, protocol.ExchangeOptions{Issuer: idp, At: testcert.Now(), Binding: metadata.SOAP, ExpectedRequestID: q.ID}) + if err != nil || failed.Identifier() != nil || failed.Status().Code != protocol.Requester { + t.Fatalf("authenticated mapping failure: %v", err) + } +} + +func TestLimitedUseMappedIdentifier(t *testing.T) { + c := testcert.New(t) + key := protocol.SigningKey{Key: c.LeafKey} + keys := []crypto.PublicKey{c.LeafKey.Public()} + recipient := encryptionKey(t) + sub, _ := protocol.NewSubject(t.Context(), protocol.NameIdentifier{Value: "limited", Format: protocol.NameIDPersistent}) + conditions := queryXML(t, ``+sp+``) + d, err := protocol.SignAuthorityAssertion(t.Context(), protocol.AuthorityAssertion{ID: "_limited", Issuer: idp, IssueInstant: testcert.Now(), Subject: sub, Conditions: conditions}, key) + if err != nil { + t.Fatal(err) + } + o := protocol.AssertionOptions{Issuer: idp, Audience: sp, At: testcert.Now(), Decryption: &protocol.DecryptionOptions{Keys: []crypto.Decrypter{recipient}}} + a, err := protocol.VerifyAuthorityAssertion(t.Context(), d, keys, o) + if err != nil { + t.Fatal(err) + } + encrypted, err := protocol.EncryptMappedAssertion(t.Context(), a, &protocol.EncryptionOptions{Targets: protocol.EncryptNameID, Recipient: &recipient.PublicKey}) + if err != nil { + t.Fatal(err) + } + limited, err := protocol.DecryptMappedAssertion(t.Context(), encrypted, keys, o) + if err != nil || limited.ID() != "_limited" || len(limited.Statements()) != 0 { + t.Fatalf("limited identifier: %v", err) + } + bad := o + bad.Audience = idp + if _, err = protocol.DecryptMappedAssertion(t.Context(), encrypted, keys, bad); err == nil { + t.Fatal("wrong audience accepted") + } + bad = o + bad.At = testcert.Now().Add(time.Minute) + if _, err = protocol.DecryptMappedAssertion(t.Context(), encrypted, keys, bad); err == nil { + t.Fatal("expired limited identifier accepted") + } + if _, err = protocol.DecryptMappedNameID(t.Context(), encrypted, o.Decryption); err == nil { + t.Fatal("limited assertion treated as bare NameID") + } +} + +func TestMappingLegacyEncryptionNeedsVerifiedEnvelope(t *testing.T) { + if fips140.Enabled() { + t.Skip("legacy algorithms are disabled in FIPS mode") + } + c := testcert.New(t) + recipient := encryptionKey(t) + want := protocol.NameIdentifier{Value: "target-alice", Format: protocol.NameIDPersistent} + plain, err := protocol.NameIDDocument(t.Context(), want) + if err != nil { + t.Fatal(err) + } + data := xmlenc.Options{DataAlgorithm: xmlenc.AES256CBC, AllowLegacy: true} + enc, err := protocol.EncryptMappedIdentifier(t.Context(), plain, &protocol.EncryptionOptions{Targets: protocol.EncryptNameID, Recipient: &recipient.PublicKey, Data: data}) + if err != nil { + t.Fatal(err) + } + decryption := &protocol.DecryptionOptions{Keys: []crypto.Decrypter{recipient}, Data: data} + if _, err = protocol.DecryptMappedNameID(t.Context(), enc, decryption); err == nil { + t.Fatal("unauthenticated CBC identifier accepted") + } + d, err := protocol.SignNameIDMappingResponse(t.Context(), protocol.NameIDMappingResponse{Issuer: idp, IssueInstant: testcert.Now(), InResponseTo: "_legacy_mapping", Identifier: enc}, protocol.SigningKey{Key: c.LeafKey}) + if err != nil { + t.Fatal(err) + } + r, err := protocol.VerifyNameIDMappingResponse(t.Context(), d, []crypto.PublicKey{c.LeafKey.Public()}, protocol.ExchangeOptions{Issuer: idp, ExpectedRequestID: "_legacy_mapping", At: testcert.Now(), Binding: metadata.SOAP}) + if err != nil { + t.Fatal(err) + } + if got, err := r.DecryptNameID(t.Context(), decryption); err != nil || got != want { + t.Fatalf("authenticated CBC mapping: %v", err) + } + if _, err = r.DecryptNameID(t.Context(), &protocol.DecryptionOptions{Keys: []crypto.Decrypter{recipient}}); err == nil { + t.Fatal("CBC accepted without explicit legacy policy") + } +} diff --git a/saml/protocol/protocol_test.go b/saml/protocol/protocol_test.go index 547c71c..935e9fc 100644 --- a/saml/protocol/protocol_test.go +++ b/saml/protocol/protocol_test.go @@ -364,9 +364,19 @@ func TestAuthnRequestACSSelection(t *testing.T) { {name: "first explicit true", first: metadata.Endpoint{DefaultSet: true, Default: true}, want: first}, {name: "explicit index", attributes: ` AssertionConsumerServiceIndex="1"`, want: second}, {name: "explicit URL", attributes: ` AssertionConsumerServiceURL="` + second + `" ProtocolBinding="` + metadata.HTTPPost + `"`, want: second}, + {name: "explicit URL without binding", attributes: ` AssertionConsumerServiceURL="` + second + `"`, want: second}, + {name: "explicit binding without URL", attributes: ` ProtocolBinding="` + metadata.HTTPPost + `"`, want: first}, {name: "unlisted index", attributes: ` AssertionConsumerServiceIndex="2"`}, {name: "unlisted URL", attributes: ` AssertionConsumerServiceURL="https://evil.test/acs" ProtocolBinding="` + metadata.HTTPPost + `"`}, + {name: "unlisted URL without binding", attributes: ` AssertionConsumerServiceURL="https://evil.test/acs"`}, + {name: "unsupported binding", attributes: ` AssertionConsumerServiceURL="` + second + `" ProtocolBinding="` + metadata.HTTPRedirect + `"`}, + {name: "empty explicit URL", attributes: ` AssertionConsumerServiceURL=""`}, + {name: "empty explicit binding", attributes: ` ProtocolBinding=""`}, + {name: "explicit URL with empty binding", attributes: ` AssertionConsumerServiceURL="` + second + `" ProtocolBinding=""`}, {name: "index and URL conflict", attributes: ` AssertionConsumerServiceIndex="1" AssertionConsumerServiceURL="` + second + `"`}, + {name: "index and binding conflict", attributes: ` AssertionConsumerServiceIndex="1" ProtocolBinding="` + metadata.HTTPPost + `"`}, + {name: "index and empty URL conflict", attributes: ` AssertionConsumerServiceIndex="1" AssertionConsumerServiceURL=""`}, + {name: "index and empty binding conflict", attributes: ` AssertionConsumerServiceIndex="1" ProtocolBinding=""`}, } { t.Run(tc.name, func(t *testing.T) { a, b := tc.first, tc.second diff --git a/saml/protocol/qname.go b/saml/protocol/qname.go new file mode 100644 index 0000000..03c6923 --- /dev/null +++ b/saml/protocol/qname.go @@ -0,0 +1,135 @@ +package protocol + +import ( + "context" + "strings" + + "github.com/skarm/xmlsec/c14n" + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/lexicon" + "github.com/skarm/xmlsec/internal/tree" + "github.com/skarm/xmlsec/internal/xmlchar" + "github.com/skarm/xmlsec/saml/internal/xmlutil" +) + +// Rich SAML XML can contain QName-valued content (xsi:type, xs:QName or an +// application datatype). Its namespace bindings must not remain outside the +// digest. A superset PrefixList preserves these bindings without guessing a +// custom datatype's semantics. Inclusive C14N already includes those bindings. +func namespaceSigningKey(target document.Element, key SigningKey) SigningKey { + if key.Canonicalization != "" && key.Canonicalization != c14n.Exclusive { + return key + } + + prefixes := append([]string{lexicon.C14NDefaultPrefixToken}, key.InclusivePrefixes...) + seen := map[string]bool{} + + _, root := tree.Unwrap(target) + for n := root; n != nil; n = n.Parent { + for _, ns := range n.NS { + prefixes = append(prefixes, ns.Prefix) + } + } + + stack := []*tree.Node{root} + for len(stack) > 0 { + n := stack[len(stack)-1] + stack = stack[:len(stack)-1] + + for _, ns := range n.NS { + prefixes = append(prefixes, ns.Prefix) + } + + stack = append(stack, n.Children...) + } + + key.InclusivePrefixes = nil + + for _, prefix := range prefixes { + if prefix == "" { + prefix = lexicon.C14NDefaultPrefixToken + } + + if prefix == lexicon.PrefixXML || seen[prefix] { + continue + } + + seen[prefix] = true + key.InclusivePrefixes = append(key.InclusivePrefixes, prefix) + } + + return key +} + +func signRichExchange(ctx context.Context, n *tree.Node, key SigningKey, encryption *EncryptionOptions) (*document.Document, error) { + d, err := xmlutil.Document(ctx, n) + if err != nil { + return nil, err + } + + return signExchange(ctx, n, namespaceSigningKey(d.Root(), key), encryption) +} + +func hasInclusiveNamespaces(e document.Element) bool { + for _, s := range e.Children() { + if s.Name() != xmlutil.Name(xmlutil.DS, lexicon.DSigElementSignature) { + continue + } + + for _, info := range s.Children() { + if info.Name() != xmlutil.Name(xmlutil.DS, lexicon.DSigElementSignedInfo) { + continue + } + + for _, method := range info.Children() { + for _, child := range method.Children() { + if child.Name() == xmlutil.Name(c14n.Exclusive, lexicon.C14NElementInclusiveNamespaces) { + return true + } + } + } + } + } + + return false +} + +func valuePrefixes(elements ...document.Element) []string { + prefixes := []string{lexicon.C14NDefaultPrefixToken} + seen := map[string]bool{lexicon.C14NDefaultPrefixToken: true} + + for _, e := range elements { + _, n := tree.Unwrap(e) + + stack := []*tree.Node{n} + for len(stack) > 0 { + current := stack[len(stack)-1] + stack = stack[:len(stack)-1] + + for _, s := range appendAttributeValues(current) { + for token := range strings.FieldsSeq(s) { + prefix, local, ok := strings.Cut(token, ":") + if ok && xmlchar.IsValidNCName(prefix) && xmlchar.IsValidNCName(local) && !seen[prefix] { + seen[prefix] = true + prefixes = append(prefixes, prefix) + } + } + } + + stack = append(stack, current.Children...) + } + } + + return prefixes +} + +func appendAttributeValues(n *tree.Node) []string { + values := make([]string, 0, len(n.Attrs)+1) + + values = append(values, n.Data) + for _, a := range n.Attrs { + values = append(values, a.Value) + } + + return values +} diff --git a/saml/protocol/query.go b/saml/protocol/query.go new file mode 100644 index 0000000..dfe21cb --- /dev/null +++ b/saml/protocol/query.go @@ -0,0 +1,750 @@ +package protocol + +import ( + "bytes" + "context" + "crypto" + "slices" + + "github.com/skarm/xmlsec/internal/lexicon" + + "github.com/skarm/xmlsec/c14n" + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/tree" + "github.com/skarm/xmlsec/saml/internal/profile" + "github.com/skarm/xmlsec/saml/internal/xmlsig" + "github.com/skarm/xmlsec/saml/internal/xmlutil" +) + +// Local SAML vocabulary tokens shared by protocol producers and parsers. Each call site still verifies the assertion/protocol namespace or resolves the QName prefix. +const ( + xmlNameAssertionIDRef = "AssertionIDRef" + xmlNameNamespace = "Namespace" +) + +// Profile bound shared by these producer/parser paths; byte/count/time units follow the name, and default values apply only to zero options. +const ( + maximumActionValueBytes = 4096 +) + +// QueryKind identifies a Core 3.3 query or assertion request. +type QueryKind string + +const ( + // AuthenticationQuery requests statements about past authentication, not login. + AuthenticationQuery QueryKind = lexicon.SAMLElementAuthnQuery + // AttributeQuery requests attributes, optionally constrained by values. + AttributeQuery QueryKind = lexicon.SAMLElementAttributeQuery + // AuthorizationQuery requests authorization decisions for actions on a resource. + AuthorizationQuery QueryKind = lexicon.SAMLElementAuthzDecisionQuery + // AssertionRequest requests existing assertions by their exact XML IDs. + AssertionRequest QueryKind = lexicon.SAMLElementAssertionIDRequest +) + +// RequestedContext preserves references and their comparison rule. AuthnQuery +// does not impose preference ordering (Approved Errata E45). Exactly one of +// Classes and Declarations is required. Empty Comparison is exact. +type RequestedContext struct { + // Classes lists authentication context class URIs. Exactly one of Classes and + // Declarations must be nonempty. + Classes []string + // Declarations lists authentication context declaration URIs. + Declarations []string + // Comparison selects exact, minimum, maximum or better comparison. Empty + // selects exact. + Comparison string +} + +// Action is a Core 2.7.4.2 action. An omitted Namespace has the SAML default +// rwedc-negation semantics; it is not an arbitrary application namespace. +type Action struct { + // Namespace identifies the action vocabulary. Empty selects the SAML + // rwedc-negation vocabulary. + Namespace string + // Value is the action text interpreted according to Namespace. + Value string +} + +// Query represents a complete query payload. Subject is a saml:Subject document; +// Attributes are saml:Attribute documents, retaining arbitrary XML values and +// xsi:type/nil annotations. Evidence is an optional saml:Evidence document and +// remains untrusted until independently validated by the authorization backend. +// The optional Encryption must select EncryptNameID and encrypts the subject ID. +type Query struct { + MessageHeader + // Limits bounds producer-side attribute filters; verification uses the receiver's limits. + Limits Limits + // Kind selects the query or assertion-ID request form. + Kind QueryKind + // Subject is the complete saml:Subject document required by subject queries and + // absent from assertion-ID requests. + Subject *document.Document + // SessionIndex optionally restricts an authentication query to one session. + SessionIndex string + // Context optionally restricts an authentication query's authentication + // context. + Context *RequestedContext + // Attributes contains the requested saml:Attribute filters for an attribute + // query. Empty requests all attributes allowed by authority policy. + Attributes []*document.Document + // Resource is the resource URI for an authorization decision query. + Resource string + // Actions lists the actions requested by an authorization decision query. + Actions []Action + // Evidence is optional saml:Evidence for an authorization query. The backend + // must validate it independently. + Evidence *document.Document + // AssertionIDs lists exact XML IDs for an assertion-ID request. + AssertionIDs []string + // Encryption optionally encrypts the subject identifier and must select + // EncryptNameID. + Encryption *EncryptionOptions +} + +// VerifiedQuery contains a validated signed request. It neither authorizes +// disclosure nor verifies embedded Evidence. Those are separate application duties. +type VerifiedQuery struct{ query Query } + +// Query returns an owned copy; immutable XML snapshots may safely be shared. +func (q *VerifiedQuery) Query() Query { return cloneQuery(q.query) } + +// NewSubject creates a subject with a NameID. Subject confirmations, when needed, +// can instead be supplied in an application-built immutable Subject document. +func NewSubject(ctx context.Context, id NameIdentifier) (*document.Document, error) { + n, err := nameIDNode(id) + if err != nil { + return nil, err + } + + s := xmlutil.Node(prefixAssertion, AssertionNamespace, xmlNameSubject) + s.NS = []tree.Namespace{{Prefix: prefixAssertion, URI: AssertionNamespace}} + xmlutil.Add(s, n) + + return xmlutil.Document(ctx, s) +} + +// SignQuery signs a Core query. SOAP delivery must use SignSOAP so inclusive +// canonicalization is evaluated in the final namespace context. +func SignQuery(ctx context.Context, q Query, key SigningKey) (*document.Document, error) { + if ctx == nil { + return nil, ErrProfile + } + + q = cloneQuery(q) + + n, err := soapExchangeRoot(q.MessageHeader, string(q.Kind)) + if err != nil { + return nil, err + } + + if err = validateQuery(ctx, q); err != nil { + return nil, err + } + + if q.Subject != nil { + appendDocument(n, q.Subject) + } + + switch q.Kind { + case AuthenticationQuery: + if q.SessionIndex != "" { + xmlutil.Set(n, xmlNameSessionIndex, q.SessionIndex) + } + + if q.Context != nil { + c, _ := requestedContextNode(*q.Context) + xmlutil.Add(n, c) + } + case AttributeQuery: + for _, a := range q.Attributes { + appendDocument(n, a) + } + case AuthorizationQuery: + xmlutil.Set(n, xmlNameResource, q.Resource) + + for _, a := range q.Actions { + c, _ := actionNode(a) + xmlutil.Add(n, c) + } + + if q.Evidence != nil { + appendDocument(n, q.Evidence) + } + case AssertionRequest: + for _, id := range q.AssertionIDs { + c := xmlutil.Node(prefixAssertion, AssertionNamespace, xmlNameAssertionIDRef) + xmlutil.Text(c, id) + xmlutil.Add(n, c) + } + } + + d, err := xmlutil.Document(ctx, n) + if err != nil { + return nil, err + } + + if q.Encryption != nil { + o, err := CloneEncryption(q.Encryption) + if err != nil || o.Targets != EncryptNameID || o.Recipient == nil { + return nil, ErrEncryption + } + + d, err = encryptSubjectIdentifiers(ctx, d, o) + if err != nil { + return nil, err + } + } + + key = namespaceSigningKey(d.Root(), key) + + k, err := signing(key) + if err != nil { + return nil, err + } + + return xmlsig.Sign(ctx, d, d.Root(), k, true) +} + +// VerifyQuery authenticates the complete query before decrypting its subject. +// ExchangeOptions.Binding should be SOAP for a synchronous SOAP exchange. +func VerifyQuery(ctx context.Context, d *document.Document, keys []crypto.PublicKey, o ExchangeOptions) (*VerifiedQuery, error) { + if d == nil { + return nil, ErrProfile + } + + kind := QueryKind(d.Root().Name().Local) + + attrs := []string{} + if kind == AuthenticationQuery { + attrs = append(attrs, xmlNameSessionIndex) + } + + if kind == AuthorizationQuery { + attrs = append(attrs, xmlNameResource) + } + + h, cs, err := verifyExchange(ctx, d, string(kind), keys, o, attrs, false) + if err != nil { + return nil, err + } + + q := Query{MessageHeader: h, Kind: kind, Limits: o.Limits} + if kind != AssertionRequest { + d, q.Subject, err = readQuerySubject(ctx, d, cs, o.Decryption) + if err != nil { + return nil, err + } + + cs, err = controlChildren(d, false) + if err != nil { + return nil, err + } + + cs = cs[1:] + } + + if err = readQueryBody(d, cs, &q); err != nil { + return nil, err + } + + if err = validateQuery(ctx, q); err != nil { + return nil, err + } + + return &VerifiedQuery{query: q}, ctx.Err() +} + +func validateQuery(ctx context.Context, q Query) error { + const ( + maximumAssertionIDReferences = 128 + ) + + if ctx == nil { + return ErrProfile + } + + if q.Kind == AssertionRequest { + if q.Subject != nil || len(q.AssertionIDs) < 1 || len(q.AssertionIDs) > maximumAssertionIDReferences { + return ErrProfile + } + + for _, id := range q.AssertionIDs { + if !profile.ID(id) { + return ErrProfile + } + } + } else { + if err := validateQuerySubject(q.Subject); err != nil { + return err + } + + if len(q.AssertionIDs) > 0 { + return ErrProfile + } + } + + if q.Kind != AuthenticationQuery && (q.SessionIndex != "" || q.Context != nil) || q.Kind != AttributeQuery && len(q.Attributes) != 0 || q.Kind != AuthorizationQuery && (q.Resource != "" || len(q.Actions) != 0 || q.Evidence != nil) { + return ErrProfile + } + + if err := validateQueryBody(q); err != nil { + return err + } + + l, err := normalize(q.Limits) + if err != nil { + return err + } + + if len(q.Attributes) > l.MaxAttributes { + return ErrProfile + } + + for _, a := range q.Attributes { + if err := attributeBudget(ctx, a.Root(), l); err != nil { + return err + } + } + + return ctx.Err() +} + +func cloneQuery(q Query) Query { + q.Attributes = slices.Clone(q.Attributes) + q.Actions = slices.Clone(q.Actions) + + q.AssertionIDs = slices.Clone(q.AssertionIDs) + if q.Context != nil { + c := *q.Context + c.Classes = slices.Clone(c.Classes) + c.Declarations = slices.Clone(c.Declarations) + q.Context = &c + } + + return q +} + +func appendDocument(n *tree.Node, d *document.Document) { + _, child := tree.Unwrap(d.Root()) + xmlutil.Add(n, tree.ImportSubtree(child)) +} + +func validateQuerySubject(d *document.Document) error { + const ( + maximumQuerySubjectChildren = 65 + ) + + if d == nil || d.Root().Name() != xmlutil.Name(AssertionNamespace, xmlNameSubject) || xmlutil.Attrs(d.Root(), nil, nil) != nil { + return ErrProfile + } + + cs, err := xmlutil.Children(d.Root()) + if err != nil || len(cs) == 0 || len(cs) > maximumQuerySubjectChildren { + return ErrProfile + } + + i := 0 + + if cs[0].Name() == xmlutil.Name(AssertionNamespace, xmlNameNameID) { + if _, err := readNameID(cs[0]); err != nil { + return err + } + + i++ + } + + for _, c := range cs[i:] { + if err := validateQueryConfirmation(c); err != nil { + return err + } + } + + return nil +} + +func requestedContextNode(c RequestedContext) (*tree.Node, error) { + const ( + contextBetter = "better" + contextMaximum = "maximum" + contextMinimum = "minimum" + maximumRequestedContextReferences = 128 + ) + + if (len(c.Classes) == 0) == (len(c.Declarations) == 0) || len(c.Classes)+len(c.Declarations) > maximumRequestedContextReferences || !slices.Contains([]string{"", contextExact, contextMinimum, contextMaximum, contextBetter}, c.Comparison) { + return nil, ErrProfile + } + + n := xmlutil.Node(prefixProtocol, ProtocolNamespace, xmlNameRequestedAuthnContext) + if c.Comparison != "" { + xmlutil.Set(n, xmlNameComparison, c.Comparison) + } + + name, refs := xmlNameAuthnContextClassRef, c.Classes + if len(c.Declarations) > 0 { + name, refs = xmlNameAuthnContextDeclRef, c.Declarations + } + + for _, v := range refs { + if !profile.URI(v) { + return nil, ErrProfile + } + + e := xmlutil.Node(prefixAssertion, AssertionNamespace, name) + xmlutil.Text(e, v) + xmlutil.Add(n, e) + } + + return n, nil +} + +func readRequestedContext(e document.Element) (*RequestedContext, error) { + if e.Name() != xmlutil.Name(ProtocolNamespace, xmlNameRequestedAuthnContext) || xmlutil.Attrs(e, nil, []string{xmlNameComparison}) != nil { + return nil, ErrProfile + } + + c := &RequestedContext{Comparison: xmlutil.Attr(e, xmlNameComparison)} + + cs, err := xmlutil.Children(e) + if err != nil { + return nil, err + } + + for _, r := range cs { + v, err := xmlutil.Simple(r, maximumURITextBytes) + if err != nil { + return nil, err + } + + switch r.Name() { + case xmlutil.Name(AssertionNamespace, xmlNameAuthnContextClassRef): + c.Classes = append(c.Classes, v) + case xmlutil.Name(AssertionNamespace, xmlNameAuthnContextDeclRef): + c.Declarations = append(c.Declarations, v) + default: + return nil, ErrProfile + } + } + + _, err = requestedContextNode(*c) + + return c, err +} + +func actionNode(a Action) (*tree.Node, error) { + if !uriReference(a.Namespace) || a.Value == "" || len(a.Value) > maximumActionValueBytes { + return nil, ErrProfile + } + + n := xmlutil.Node(prefixAssertion, AssertionNamespace, xmlNameAction) + if a.Namespace != "" { + xmlutil.Set(n, xmlNameNamespace, a.Namespace) + } + + xmlutil.Text(n, a.Value) + + return n, nil +} + +func readAction(e document.Element) (Action, error) { + a := Action{Namespace: xmlutil.Attr(e, xmlNameNamespace)} + if e.Name() != xmlutil.Name(AssertionNamespace, xmlNameAction) || xmlutil.Attrs(e, nil, []string{xmlNameNamespace}) != nil { + return a, ErrProfile + } + + v, err := xmlutil.SimpleText(e, maximumActionValueBytes) + if err != nil { + return a, err + } + + a.Value = v + _, err = actionNode(a) + + return a, err +} + +func queryAttribute(e document.Element) ([2]string, error) { + const ( + maximumQueryAttributeNameBytes = 4096 + maximumQueryAttributeValues = 128 + ) + + k := [2]string{xmlutil.Attr(e, nameAttributeName), xmlutil.Attr(e, xmlNameNameFormat)} + if k[1] == "" { + k[1] = AttributeUnspecified + } + + if e.Name() != xmlutil.Name(AssertionNamespace, xmlNameAttribute) || attributeAttributes(e) != nil || k[0] == "" || len(k[0]) > maximumQueryAttributeNameBytes || !profile.URI(k[1]) { + return k, ErrProfile + } + + cs, err := xmlutil.Children(e) + if err != nil || len(cs) > maximumQueryAttributeValues { + return k, ErrProfile + } + + for _, v := range cs { + if v.Name() != xmlutil.Name(AssertionNamespace, xmlNameAttributeValue) { + return k, ErrProfile + } + + if err := attributeValueType(v); err != nil { + return k, err + } + } + + return k, nil +} + +func validateEvidence(e document.Element) error { + if e.Name() != xmlutil.Name(AssertionNamespace, xmlNameEvidence) || xmlutil.Attrs(e, nil, nil) != nil { + return ErrProfile + } + + return validateAssertionReferences(e, false) +} + +func validateAdvice(e document.Element) error { + if e.Name() != xmlutil.Name(AssertionNamespace, xmlNameAdvice) || xmlutil.Attrs(e, nil, nil) != nil { + return ErrProfile + } + + return validateAssertionReferences(e, true) +} + +func validateAssertionReferences(e document.Element, allowForeign bool) error { + const ( + maximumAssertionReferences = 128 + xmlNameAssertionURIRef = "AssertionURIRef" + ) + + cs, err := xmlutil.Children(e) + if err != nil || !allowForeign && len(cs) == 0 || len(cs) > maximumAssertionReferences { + return ErrProfile + } + + for _, v := range cs { + if v.Name().Space != AssertionNamespace { + if allowForeign && v.Name().Space != "" { + continue + } + + return ErrProfile + } + + switch v.Name().Local { + case xmlNameAssertionIDRef, xmlNameAssertionURIRef: + ref, err := xmlutil.Simple(v, maximumURITextBytes) + if err != nil || ref == "" || v.Name().Local == xmlNameAssertionIDRef && !profile.ID(ref) || v.Name().Local == xmlNameAssertionURIRef && !uriReference(ref) { + return ErrProfile + } + case nameAssertion, xmlNameEncryptedAssertion: + // Embedded assertions remain untrusted until independently verified. + default: + return ErrProfile + } + } + + return nil +} + +func sameXML(ctx context.Context, a, b document.Element) (bool, error) { + var x, y bytes.Buffer + + prefixes := valuePrefixes(a, b) + if err := c14n.WriteElement(ctx, &x, a, c14n.Options{Algorithm: c14n.ExclusiveWithComments, InclusivePrefixes: prefixes}); err != nil { + return false, err + } + + if err := c14n.WriteElement(ctx, &y, b, c14n.Options{Algorithm: c14n.ExclusiveWithComments, InclusivePrefixes: prefixes}); err != nil { + return false, err + } + + return bytes.Equal(x.Bytes(), y.Bytes()), nil +} + +func uriReference(s string) bool { return logoutReason(s) } + +func readQuerySubject(ctx context.Context, d *document.Document, cs []document.Element, o *DecryptionOptions) (*document.Document, *document.Document, error) { + if len(cs) == 0 || cs[0].Name() != xmlutil.Name(AssertionNamespace, xmlNameSubject) { + return nil, nil, ErrProfile + } + + state, err := newDecryptionState(o) + if err != nil { + return nil, nil, err + } + + for { + sub, err := xmlutil.Direct(d.Root(), xmlutil.Name(AssertionNamespace, xmlNameSubject)) + if err != nil { + return nil, nil, err + } + + target := subjectChild(sub, xmlNameEncryptedID) + if !target.Valid() { + scope, err := tree.ScopeDocument(d, sub) + return d, scope, err + } + + d, err = state.decryptSAML(ctx, d, target, xmlNameNameID, true) + if err != nil { + return nil, nil, err + } + } +} + +func readQueryBody(d *document.Document, cs []document.Element, q *Query) error { + const ( + maximumAssertionReferenceBytes = 1024 + ) + + switch q.Kind { + case AuthenticationQuery: + q.SessionIndex = xmlutil.Attr(d.Root(), xmlNameSessionIndex) + + if len(cs) > 1 { + return ErrProfile + } + + if len(cs) == 1 { + var err error + + q.Context, err = readRequestedContext(cs[0]) + + return err + } + case AttributeQuery: + for _, a := range cs { + v, err := tree.ScopeDocument(d, a) + if err != nil { + return err + } + + q.Attributes = append(q.Attributes, v) + } + case AuthorizationQuery: + q.Resource = xmlutil.Attr(d.Root(), xmlNameResource) + + if len(cs) > 0 && cs[len(cs)-1].Name() == xmlutil.Name(AssertionNamespace, xmlNameEvidence) { + var err error + + q.Evidence, err = tree.ScopeDocument(d, cs[len(cs)-1]) + if err != nil { + return err + } + + cs = cs[:len(cs)-1] + } + + for _, a := range cs { + v, err := readAction(a) + if err != nil { + return err + } + + q.Actions = append(q.Actions, v) + } + case AssertionRequest: + for _, c := range cs { + if c.Name() != xmlutil.Name(AssertionNamespace, xmlNameAssertionIDRef) { + return ErrProfile + } + + v, err := xmlutil.Simple(c, maximumAssertionReferenceBytes) + if err != nil { + return err + } + + q.AssertionIDs = append(q.AssertionIDs, v) + } + default: + return ErrProfile + } + + return nil +} + +func validateQueryBody(q Query) error { + const ( + maximumQueryActions = 128 + maximumQuerySessionIndexBytes = 4096 + ) + + switch q.Kind { + case AuthenticationQuery: + if len(q.SessionIndex) > maximumQuerySessionIndexBytes { + return ErrProfile + } + + if q.Context != nil { + _, err := requestedContextNode(*q.Context) + return err + } + case AttributeQuery: + return validateQueryAttributes(q.Attributes) + case AuthorizationQuery: + if !uriReference(q.Resource) || len(q.Actions) < 1 || len(q.Actions) > maximumQueryActions { + return ErrProfile + } + + for _, a := range q.Actions { + if _, err := actionNode(a); err != nil { + return err + } + } + + if q.Evidence != nil { + return validateEvidence(q.Evidence.Root()) + } + case AssertionRequest: + default: + return ErrProfile + } + + return nil +} + +func validateQueryAttributes(attributes []*document.Document) error { + const ( + maximumQueryAttributes = 512 + ) + + if len(attributes) > maximumQueryAttributes { + return ErrProfile + } + + seen := map[[2]string]bool{} + + for _, a := range attributes { + if a == nil { + return ErrProfile + } + + name, err := queryAttribute(a.Root()) + if err != nil || seen[name] { + return ErrProfile + } + + seen[name] = true + } + + return nil +} + +func subjectChild(subject document.Element, local string) document.Element { + for _, c := range subject.Children() { + if c.Name() == xmlutil.Name(AssertionNamespace, local) { + return c + } + + if c.Name() == xmlutil.Name(AssertionNamespace, xmlNameSubjectConfirmation) { + for _, id := range c.Children() { + if id.Name() == xmlutil.Name(AssertionNamespace, local) { + return id + } + } + } + } + + return document.Element{} +} diff --git a/saml/protocol/query_limits.go b/saml/protocol/query_limits.go new file mode 100644 index 0000000..41d1336 --- /dev/null +++ b/saml/protocol/query_limits.go @@ -0,0 +1,47 @@ +package protocol + +import ( + "context" + "io" + + "github.com/skarm/xmlsec/c14n" + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/saml/internal/xmlutil" +) + +func attributeBudget(ctx context.Context, a document.Element, l Limits) error { + if len(a.Children()) > l.MaxValuesPerAttribute { + return ErrProfile + } + + for _, v := range a.Children() { + if err := c14n.WriteElement(ctx, io.Discard, v, c14n.Options{Algorithm: c14n.ExclusiveWithComments, InclusivePrefixes: valuePrefixes(v), MaxOutputBytes: l.MaxValueBytes}); err != nil { + return err + } + } + + return ctx.Err() +} + +func authorityAttributeBudget(ctx context.Context, d *document.Document, l Limits) error { + total := 0 + + for _, s := range d.Root().Children() { + if s.Name() != xmlutil.Name(AssertionNamespace, xmlNameAttributeStatement) { + continue + } + + total += len(s.Children()) + if total > l.MaxAttributes { + return ErrProfile + } + + for _, a := range s.Children() { + if err := attributeBudget(ctx, a, l); err != nil { + return err + } + } + } + + return nil +} diff --git a/saml/protocol/query_response.go b/saml/protocol/query_response.go new file mode 100644 index 0000000..a699843 --- /dev/null +++ b/saml/protocol/query_response.go @@ -0,0 +1,1167 @@ +package protocol + +import ( + "context" + "crypto" + "slices" + "strings" + "time" + + "github.com/skarm/xmlsec/internal/lexicon" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/tree" + "github.com/skarm/xmlsec/saml/binding" + "github.com/skarm/xmlsec/saml/internal/profile" + "github.com/skarm/xmlsec/saml/internal/xmlsig" + "github.com/skarm/xmlsec/saml/internal/xmlutil" + "github.com/skarm/xmlsec/xmldsig" +) + +// Profile bound shared by these producer/parser paths; byte/count/time units follow the name, and default values apply only to zero options. +const ( + maximumAuthorityStatements = 128 +) + +// MaxQueryAssertions bounds a response to the shared 64-signature budget. +const MaxQueryAssertions = xmlsig.MaxMessageSignatures - 1 + +// AuthorityAssertion describes an assertion independent of WebSSO. Statements +// are complete AuthnStatement, AttributeStatement or AuthzDecisionStatement XML +// documents. This retains complex attribute values, authentication declarations +// and authorization evidence without converting them to lossy string claims. +type AuthorityAssertion struct { + // ID is the assertion's XML identifier. + ID string + // Issuer is the assertion issuer's entity ID. + Issuer string + // Limits bounds XML attribute values, including their markup and type annotations. + Limits Limits + // IssueInstant is the assertion creation time. + IssueInstant time.Time + // Subject is an optional complete saml:Subject document. + Subject *document.Document + // Conditions is an optional complete saml:Conditions document. + Conditions *document.Document + // Advice is optional saml:Advice XML; its contents grant no trust by + // themselves. + Advice *document.Document + // Statements contains complete supported SAML statement documents. + Statements []*document.Document + // Encryption optionally encrypts selected assertion contents before signing and + // the whole assertion afterward. + Encryption *EncryptionOptions +} + +// AssertionOptions fixes the assertion issuer independently of the response +// issuer (Profiles 6.4.2). Unknown conditions fail closed. Audience restrictions +// are ANDed; audience values within each restriction are ORed (Errata E46). +type AssertionOptions struct { + // Issuer is the expected assertion issuer, independently of the response + // issuer. + Issuer string + // Audience is the local relying party checked against audience restrictions. + Audience string + // At is the explicit, nonzero assertion verification time. + At time.Time + // Limits bounds assertion structure, values and validity windows. + Limits Limits + // Policy restricts assertion signature and reference algorithms. + Policy xmldsig.Policy + // Decryption supplies keys and policy for encrypted assertion data. Nil + // supplies no decryption keys. + Decryption *DecryptionOptions +} + +// QueryMatchOptions provides application-defined semantics explicitly. Context +// must implement minimum/maximum/better according to the authority's ordering, +// including maximum's strongest-available rule. Exact URI comparison is built in. +// Confirm must establish Core 3.3.4 strong confirmation matching, not merely +// equality of method names. Missing callbacks reject requests needing them. +// EqualValue optionally implements an attribute profile's value equality rules; +// otherwise the identical canonical XML representation is required. +type QueryMatchOptions struct { + // Context compares a statement's authentication context with the requested + // context when application-defined ordering is needed. + Context func(context.Context, document.Element, RequestedContext) (bool, error) + // Confirm checks strong matching between requested and returned + // SubjectConfirmation elements. + Confirm func(context.Context, document.Element, document.Element) (bool, error) + // EqualValue optionally compares requested and returned attribute values. Nil + // requires identical canonical XML. + EqualValue func(context.Context, document.Element, document.Element) (bool, error) +} + +// VerifiedAuthorityAssertion contains authenticated XML, not an application login +// or authorization grant. OneTimeUse requires atomic consumption by the relying +// application; saml/authority's client performs it before returning the result. +type VerifiedAuthorityAssertion struct { + document *document.Document + signed *document.Document + id, issuer string + subject document.Element + statements []document.Element + oneTime bool + expires time.Time +} + +// Document returns the immutable authenticated, decrypted assertion. +func (a *VerifiedAuthorityAssertion) Document() *document.Document { return a.document } + +// ID returns the assertion ID. +func (a *VerifiedAuthorityAssertion) ID() string { return a.id } + +// Issuer returns the separately authenticated assertion issuer. +func (a *VerifiedAuthorityAssertion) Issuer() string { return a.issuer } + +// Subject returns the authenticated subject, which can be absent for an ID request. +func (a *VerifiedAuthorityAssertion) Subject() document.Element { return a.subject } + +// Statements returns an owned list of immutable authenticated statements. +func (a *VerifiedAuthorityAssertion) Statements() []document.Element { + return slices.Clone(a.statements) +} + +// OneTimeUse reports the assertion's one-time-use restriction. +func (a *VerifiedAuthorityAssertion) OneTimeUse() bool { return a.oneTime } + +// NotOnOrAfter returns the assertion expiry, or zero when not supplied. +func (a *VerifiedAuthorityAssertion) NotOnOrAfter() time.Time { return a.expires } + +// EncryptAuthorityAssertion encrypts an authenticated assertion for a recipient. +// Only whole-assertion encryption can be applied after its signature; encrypting +// inner identifiers/attributes must be done before signing the assertion. +func EncryptAuthorityAssertion(ctx context.Context, a *VerifiedAuthorityAssertion, o *EncryptionOptions) (*document.Document, error) { + if a == nil || o == nil || o.Targets != EncryptAssertion { + return nil, ErrEncryption + } + + p, err := CloneEncryption(o) + if err != nil || p.Recipient == nil { + return nil, ErrEncryption + } + + return encryptSAML(ctx, a.signed, a.signed.Root(), xmlNameEncryptedAssertion, p) +} + +// SignAuthorityAssertion builds and signs an assertion in SOAP-compatible +// namespace context. Application code supplies statements about established +// facts or decisions; an AuthnQuery must never trigger a new authentication. +func SignAuthorityAssertion(ctx context.Context, s AuthorityAssertion, key SigningKey) (*document.Document, error) { + if ctx == nil { + return nil, ErrProfile + } + + n, err := authorityAssertionNode(s) + if err != nil { + return nil, err + } + + d, err := xmlutil.Document(ctx, n) + if err != nil { + return nil, err + } + + l, err := normalize(s.Limits) + if err != nil { + return nil, err + } + + if err = authorityAttributeBudget(ctx, d, l); err != nil { + return nil, err + } + + if s.Encryption != nil { + d, err = encryptAuthorityContents(ctx, d, s.Encryption) + if err != nil { + return nil, err + } + } + + key = namespaceSigningKey(d.Root(), key) + + k, err := signing(key) + if err != nil { + return nil, err + } + + d, err = xmlsig.Sign(ctx, d, d.Root(), k, true) + if err != nil { + return nil, err + } + + if s.Encryption != nil && s.Encryption.Targets&EncryptAssertion != 0 { + return encryptSAML(ctx, d, d.Root(), xmlNameEncryptedAssertion, s.Encryption) + } + + return d, nil +} + +// VerifyAuthorityAssertion verifies a standalone assertion, including the +// unencrypted or encrypted representation returned by the URI binding. +func VerifyAuthorityAssertion(ctx context.Context, d *document.Document, keys []crypto.PublicKey, o AssertionOptions) (*VerifiedAuthorityAssertion, error) { + if ctx == nil || d == nil || o.At.IsZero() || !profile.URI(o.Issuer) { + return nil, ErrProfile + } + + l, err := normalize(o.Limits) + if err != nil { + return nil, err + } + + state, err := newDecryptionState(o.Decryption) + if err != nil { + return nil, err + } + + return verifyAuthorityAssertion(ctx, d, keys, o, l, state) +} + +func verifyAuthorityAssertion(ctx context.Context, d *document.Document, keys []crypto.PublicKey, o AssertionOptions, l Limits, state *decryptionState) (*VerifiedAuthorityAssertion, error) { + if ctx == nil || d == nil || o.At.IsZero() || !profile.URI(o.Issuer) { + return nil, ErrProfile + } + + var err error + if d.Root().Name() == xmlutil.Name(AssertionNamespace, xmlNameEncryptedAssertion) { + d, err = state.decryptSAML(ctx, d, d.Root(), nameAssertion, false) + if err != nil { + return nil, err + } + } + + if d.Root().Name() != xmlutil.Name(AssertionNamespace, nameAssertion) { + return nil, ErrProfile + } + + if err := targetSignature(ctx, d, d.Root(), keys, o.Policy); err != nil { + return nil, err + } + + return parseAuthorityAssertion(ctx, d, o, l, state) +} + +func parseAuthorityAssertion(ctx context.Context, d *document.Document, o AssertionOptions, l Limits, state *decryptionState) (*VerifiedAuthorityAssertion, error) { + signed := d + + r := d.Root() + if xmlutil.Attrs(r, []string{lexicon.SAMLAttrID, nameVersion, nameIssueInstant}, nil) != nil || !profile.ID(xmlutil.Attr(r, lexicon.SAMLAttrID)) || xmlutil.Attr(r, nameVersion) != version20 { + return nil, ErrProfile + } + + t, err := profile.ParseTime(xmlutil.Attr(r, nameIssueInstant)) + if err != nil || t.After(o.At.Add(l.ClockSkew)) { + return nil, ErrConditions + } + + cs, err := xmlutil.Children(r) + if err != nil || len(cs) < signedMessagePrefixChildren || issuer(cs[0], o.Issuer) != nil || cs[1].Name() != xmlutil.Name(xmlutil.DS, lexicon.DSigElementSignature) { + return nil, ErrProfile + } + + d, err = decryptAuthorityContents(ctx, d, state) + if err != nil { + return nil, err + } + + r = d.Root() + + cs, err = xmlutil.Children(r) + if err != nil { + return nil, err + } + + a := &VerifiedAuthorityAssertion{document: d, signed: signed, id: xmlutil.Attr(r, lexicon.SAMLAttrID), issuer: o.Issuer} + + i := signedMessagePrefixChildren + if i < len(cs) && cs[i].Name() == xmlutil.Name(AssertionNamespace, xmlNameSubject) { + sub, err := tree.ScopeDocument(d, cs[i]) + if err != nil { + return nil, err + } + + if err = validateQuerySubject(sub); err != nil { + return nil, err + } + + a.subject = cs[i] + i++ + } + + if i < len(cs) && cs[i].Name() == xmlutil.Name(AssertionNamespace, xmlNameConditions) { + if err := authorityConditions(cs[i], o, l, a); err != nil { + return nil, err + } + + i++ + } + + if i < len(cs) && cs[i].Name() == xmlutil.Name(AssertionNamespace, xmlNameAdvice) { + if err := validateAdvice(cs[i]); err != nil { + return nil, err + } + + i++ + } + + if len(cs)-i > maximumAuthorityStatements { + return nil, ErrProfile + } + + for _, st := range cs[i:] { + if err := validateAuthorityStatement(st); err != nil { + return nil, err + } + + a.statements = append(a.statements, st) + } + + if len(a.statements) > 0 && !a.subject.Valid() { + return nil, ErrProfile + } + + if err := authorityAttributeBudget(ctx, d, l); err != nil { + return nil, err + } + + return a, ctx.Err() +} + +func authorityConditions(e document.Element, o AssertionOptions, l Limits, a *VerifiedAuthorityAssertion) error { + const ( + maximumAuthorityConditions = 128 + xmlNameProxyRestriction = "ProxyRestriction" + ) + + if err := authorityTimeWindow(e, o, l, a); err != nil { + return err + } + + cs, err := xmlutil.Children(e) + if err != nil || len(cs) > maximumAuthorityConditions { + return ErrConditions + } + + proxy := false + + for _, c := range cs { + if c.Name().Space != AssertionNamespace { + return ErrConditions + } + + switch c.Name().Local { + case xmlNameAudienceRestriction: + if err := authorityAudience(c, o.Audience); err != nil { + return err + } + case xmlNameOneTimeUse: + if a.oneTime || xmlutil.Attrs(c, nil, nil) != nil || !xmlutil.EmptyContent(c) { + return ErrConditions + } + + a.oneTime = true + case xmlNameProxyRestriction: + if proxy { + return ErrConditions + } + + proxy = true + + if err := authorityProxy(c); err != nil { + return err + } + default: + return ErrConditions + } + } + + return nil +} + +// QueryResponse is a correlated result with zero or more signed assertions. +// Assertion issuers may differ from the response issuer. Empty success is a +// valid no-match result; failures carry no assertions. +type QueryResponse struct { + MessageHeader + // InResponseTo identifies the query or assertion-ID request being answered. + InResponseTo string + // Status is the signed query outcome; a successful response may contain no + // assertions. + Status Status + // Assertions contains already signed assertions or encrypted assertions. + // Failure responses must leave it empty. + Assertions []*document.Document +} + +// VerifiedQueryResponse retains authenticated protocol data, including an empty +// successful result. It is not a WebSSO response and must not create a login. +type VerifiedQueryResponse struct { + header MessageHeader + status Status + assertions []*VerifiedAuthorityAssertion +} + +// Header returns the authenticated response header. +func (r *VerifiedQueryResponse) Header() MessageHeader { return r.header } + +// Status returns the authenticated status. +func (r *VerifiedQueryResponse) Status() Status { + s := r.status + s.Subcodes = slices.Clone(s.Subcodes) + + return s +} + +// Assertions returns an owned list of verified assertions. +func (r *VerifiedQueryResponse) Assertions() []*VerifiedAuthorityAssertion { + return slices.Clone(r.assertions) +} + +// QueryResponseOptions combines envelope and independently trusted assertion +// policies. AssertionTrust must authorize each assertion issuer; omission trusts +// only the response issuer with the supplied response keys. Matcher supplies +// context/confirmation semantics required by the particular query. +type QueryResponseOptions struct { + ExchangeOptions + // Audience is the local relying party required by assertion audience + // restrictions. + Audience string + // AssertionTrust optionally resolves trusted keys for each assertion issuer + // independently of the response signer. + // Nil requires the response issuer and uses the response verification keys. + AssertionTrust func(context.Context, string) ([]crypto.PublicKey, error) + // Matcher supplies application semantics for matching assertions to the + // original query. + Matcher QueryMatchOptions +} + +// SignQueryResponse signs the whole response. Each supplied assertion must +// already be signed in SOAP-compatible context, or be an encrypted assertion. +func SignQueryResponse(ctx context.Context, s QueryResponse, key SigningKey) (*document.Document, error) { + if !profile.ID(s.InResponseTo) || len(s.Assertions) > MaxQueryAssertions || s.Status.Code != "" && s.Status.Code != Success && len(s.Assertions) > 0 { + return nil, ErrProfile + } + + n, err := soapExchangeRoot(s.MessageHeader, lexicon.SAMLElementResponse) + if err != nil { + return nil, err + } + + n.NS = append(n.NS, tree.Namespace{Prefix: prefixSOAP, URI: binding.SOAPNamespace}) + xmlutil.Set(n, nameInResponseTo, s.InResponseTo) + + st, err := statusNode(s.Status) + if err != nil { + return nil, err + } + + xmlutil.Add(n, st) + + seen := map[string]bool{s.ID: true} + for _, a := range s.Assertions { + if a == nil || a.Root().Name().Space != AssertionNamespace || !slices.Contains([]string{nameAssertion, xmlNameEncryptedAssertion}, a.Root().Name().Local) { + return nil, ErrProfile + } + + if a.Root().Name().Local == nameAssertion { + id := xmlutil.Attr(a.Root(), lexicon.SAMLAttrID) + if !profile.ID(id) || seen[id] { + return nil, ErrProfile + } + + seen[id] = true + } + + appendDocument(n, a) + } + + return signRichExchange(ctx, n, key, nil) +} + +// VerifyQueryResponse checks signatures, issuer trust, correlation, conditions +// and Core query constraints before returning any assertions. +func VerifyQueryResponse(ctx context.Context, d *document.Document, keys []crypto.PublicKey, q Query, o QueryResponseOptions) (*VerifiedQueryResponse, error) { + if err := validateQuery(ctx, q); err != nil { + return nil, err + } + + if q.ID == "" || o.ExpectedRequestID != q.ID { + return nil, ErrCorrelation + } + + h, cs, err := verifyExchange(ctx, d, lexicon.SAMLElementResponse, keys, o.ExchangeOptions, nil, true) + if err != nil { + return nil, err + } + + if xmlutil.Attr(d.Root(), nameInResponseTo) != q.ID { + return nil, ErrCorrelation + } + + if len(cs) < 1 || len(cs) > MaxQueryAssertions+1 { + return nil, ErrProfile + } + + status, err := readStatus(cs[0]) + if err != nil { + return nil, err + } + + result := &VerifiedQueryResponse{header: h, status: status} + if status.Code != Success { + if len(cs) != 1 { + return nil, ErrProfile + } + + return result, nil + } + + state, err := newDecryptionState(o.Decryption) + if err != nil { + return nil, err + } + + l, err := normalize(o.Limits) + if err != nil { + return nil, err + } + + seen := map[string]bool{h.ID: true} + + for _, payload := range cs[1:] { + a, err := verifyResponseAssertion(ctx, d, payload, keys, o, l, state) + if err != nil { + return nil, err + } + + if seen[a.id] { + return nil, ErrProfile + } + + seen[a.id] = true + result.assertions = append(result.assertions, a) + } + + if err := MatchQueryResponse(ctx, q, result.assertions, o.Matcher); err != nil { + return nil, err + } + + return result, ctx.Err() +} + +// MatchQueryResponse enforces the query's subject and result filters across the +// complete returned set. It is also used by authorities before signing output. +func MatchQueryResponse(ctx context.Context, q Query, as []*VerifiedAuthorityAssertion, o QueryMatchOptions) error { + if err := validateQuery(ctx, q); err != nil { + return err + } + + if len(as) == 0 { + return nil + } + + session, contextMatch := q.SessionIndex == "", q.Context == nil + + for _, a := range as { + if a == nil { + return ErrProfile + } + + if q.Kind == AssertionRequest { + if !slices.Contains(q.AssertionIDs, a.id) { + return ErrCorrelation + } + + continue + } + + if err := strongSubject(ctx, a.subject, q.Subject.Root(), o); err != nil { + return err + } + + matched, s, c, err := matchAssertionStatements(ctx, a, q, o) + if err != nil { + return err + } + + if !matched { + return ErrProfile + } + + session = session || s + contextMatch = contextMatch || c + } + + if q.Kind == AuthenticationQuery { + if !contextMatch { + return &StatusError{Code: Responder, Subcodes: []string{NoAuthnContext}} + } + + if !session { + return ErrCorrelation + } + } + + return ctx.Err() +} + +func strongSubject(ctx context.Context, a, b document.Element, o QueryMatchOptions) error { + if !a.Valid() { + return ErrCorrelation + } + + ac, bc := a.Children(), b.Children() + if len(bc) > 0 && bc[0].Name() == xmlutil.Name(AssertionNamespace, xmlNameNameID) { + if len(ac) == 0 || ac[0].Name() != bc[0].Name() { + return ErrCorrelation + } + + x, err := readNameID(ac[0]) + if err != nil { + return err + } + + y, err := readNameID(bc[0]) + if err != nil || x != y { + return ErrCorrelation + } + // Empty and explicitly present attributes are distinct for strong matching. + if !sameAttributes(ac[0], bc[0]) { + return ErrCorrelation + } + + bc = bc[1:] + } + + if len(bc) == 0 { + return nil + } + + if o.Confirm == nil { + return ErrConditions + } + + for _, want := range bc { + for _, have := range ac { + if have.Name() != xmlutil.Name(AssertionNamespace, xmlNameSubjectConfirmation) { + continue + } + + ok, err := o.Confirm(ctx, have, want) + if err != nil { + return err + } + + if ok { + return nil + } + } + } + + return ErrConditions +} + +func sameAttributes(a, b document.Element) bool { + if len(a.Attributes()) != len(b.Attributes()) { + return false + } + + for _, attr := range a.Attributes() { + v, ok := b.Attribute(attr.Name) + if !ok || v != attr.Value { + return false + } + } + + return true +} + +func actionEqual(a, b Action) bool { + const defaultNamespace = "urn:oasis:names:tc:SAML:1.0:action:rwedc-negation" + if a.Namespace == "" { + a.Namespace = defaultNamespace + } + + if b.Namespace == "" { + b.Namespace = defaultNamespace + } + + return a == b +} + +func matchContext(ctx context.Context, st document.Element, w RequestedContext, o QueryMatchOptions) (bool, error) { + ac, err := xmlutil.Direct(st, xmlutil.Name(AssertionNamespace, xmlNameAuthnContext)) + if err != nil { + return false, err + } + + if w.Comparison != "" && w.Comparison != contextExact { + if o.Context == nil { + return false, ErrConditions + } + + return o.Context(ctx, ac, w) + } + + for _, c := range ac.Children() { + s, err := xmlutil.SimpleText(c, maximumURITextBytes) + if err != nil { + continue + } + + if c.Name() == xmlutil.Name(AssertionNamespace, xmlNameAuthnContextClassRef) && slices.Contains(w.Classes, s) || c.Name() == xmlutil.Name(AssertionNamespace, xmlNameAuthnContextDeclRef) && slices.Contains(w.Declarations, s) { + return true, nil + } + } + + return false, nil +} + +func matchAttributes(ctx context.Context, st document.Element, wants []*document.Document, o QueryMatchOptions) error { + if len(wants) == 0 { + return nil + } + + for _, a := range st.Children() { + key, err := queryAttribute(a) + if err != nil { + return err + } + + want := findRequestedAttribute(wants, key) + if want == nil { + return ErrCorrelation + } + + if err := matchAttributeValues(ctx, a, want.Root().Children(), o); err != nil { + return err + } + } + + return nil +} + +func authorityAssertionNode(s AuthorityAssertion) (*tree.Node, error) { + if !profile.ID(s.ID) || !profile.URI(s.Issuer) || s.IssueInstant.IsZero() || len(s.Statements) > maximumAuthorityStatements { + return nil, ErrProfile + } + + n := xmlutil.Node(prefixAssertion, AssertionNamespace, nameAssertion) + n.NS = []tree.Namespace{{Prefix: prefixAssertion, URI: AssertionNamespace}, {Prefix: prefixProtocol, URI: ProtocolNamespace}, {Prefix: lexicon.PrefixDSig, URI: xmlutil.DS}, {Prefix: prefixSOAP, URI: binding.SOAPNamespace}} + xmlutil.Set(n, lexicon.SAMLAttrID, s.ID) + xmlutil.Set(n, nameVersion, version20) + xmlutil.Set(n, nameIssueInstant, profile.Time(s.IssueInstant)) + + i := xmlutil.Node(prefixAssertion, AssertionNamespace, xmlNameIssuer) + xmlutil.Text(i, s.Issuer) + xmlutil.Add(n, i) + + if s.Subject != nil { + if err := validateQuerySubject(s.Subject); err != nil { + return nil, err + } + + appendDocument(n, s.Subject) + } else if len(s.Statements) > 0 { + return nil, ErrProfile + } + + if s.Conditions != nil { + if s.Conditions.Root().Name() != xmlutil.Name(AssertionNamespace, xmlNameConditions) { + return nil, ErrProfile + } + + appendDocument(n, s.Conditions) + } + + if s.Advice != nil { + if err := validateAdvice(s.Advice.Root()); err != nil { + return nil, err + } + + appendDocument(n, s.Advice) + } + + for _, stmt := range s.Statements { + if stmt == nil { + return nil, ErrProfile + } + + if err := validateAuthorityStatement(stmt.Root()); err != nil { + return nil, err + } + + appendDocument(n, stmt) + } + + return n, nil +} + +func encryptAuthorityContents(ctx context.Context, d *document.Document, options *EncryptionOptions) (*document.Document, error) { + o, err := CloneEncryption(options) + if err != nil || o.Recipient == nil { + return nil, ErrEncryption + } + + if o.Targets&EncryptNameID != 0 { + d, err = encryptSubjectIdentifiers(ctx, d, o) + if err != nil { + return nil, err + } + } + + if o.Targets&EncryptAttributes != 0 { + for { + target := authorityAttributeChild(d, xmlNameAttribute) + if !target.Valid() { + break + } + + d, err = encryptSAML(ctx, d, target, xmlNameEncryptedAttribute, o) + if err != nil { + return nil, err + } + } + } + + return d, nil +} + +func authoritySubjectChild(d *document.Document, local string) document.Element { + for _, c := range d.Root().Children() { + if c.Name() == xmlutil.Name(AssertionNamespace, xmlNameSubject) { + return subjectChild(c, local) + } + } + + return document.Element{} +} + +func authorityAttributeChild(d *document.Document, local string) document.Element { + for _, st := range d.Root().Children() { + if st.Name() != xmlutil.Name(AssertionNamespace, xmlNameAttributeStatement) { + continue + } + + for _, a := range st.Children() { + if a.Name() == xmlutil.Name(AssertionNamespace, local) { + return a + } + } + } + + return document.Element{} +} + +func decryptAuthorityContents(ctx context.Context, d *document.Document, state *decryptionState) (*document.Document, error) { + for { + target := authoritySubjectChild(d, xmlNameEncryptedID) + plain := xmlNameNameID + + if !target.Valid() { + target = authorityAttributeChild(d, xmlNameEncryptedAttribute) + plain = xmlNameAttribute + } + + if !target.Valid() { + return d, nil + } + + var err error + + d, err = state.decryptSAML(ctx, d, target, plain, true) + if err != nil { + return nil, err + } + } +} + +func authorityTimeWindow(e document.Element, o AssertionOptions, l Limits, a *VerifiedAuthorityAssertion) error { + if xmlutil.Attrs(e, nil, []string{nameNotBefore, nameNotOnOrAfter}) != nil { + return ErrConditions + } + + var start, end time.Time + + startValue, hasStart := e.Attribute(document.Name{Local: nameNotBefore}) + if hasStart { + t, err := profile.ParseTime(startValue) + if err != nil || o.At.Add(l.ClockSkew).Before(t) { + return ErrConditions + } + + start = t + } + + endValue, hasEnd := e.Attribute(document.Name{Local: nameNotOnOrAfter}) + if hasEnd { + t, err := profile.ParseTime(endValue) + if err != nil || !o.At.Add(-l.ClockSkew).Before(t) { + return ErrConditions + } + + end = t + a.expires = t + } + + if hasStart && hasEnd && (!start.Before(end) || end.Sub(start) > l.MaxAssertionLifetime) { + return ErrConditions + } + + return nil +} + +func authorityAudience(e document.Element, audience string) error { + if xmlutil.Attrs(e, nil, nil) != nil { + return ErrConditions + } + + values, err := xmlutil.Children(e) + if err != nil || len(values) == 0 { + return ErrConditions + } + + match := false + + for _, v := range values { + if v.Name() != xmlutil.Name(AssertionNamespace, xmlNameAudience) { + return ErrConditions + } + + s, err := xmlutil.Simple(v, maximumURITextBytes) + if err != nil || !profile.URI(s) { + return ErrConditions + } + + match = match || s == audience + } + + if !match { + return ErrConditions + } + + return nil +} + +func authorityProxy(e document.Element) error { + const ( + xmlNameCount = "Count" + ) + + if xmlutil.Attrs(e, nil, []string{xmlNameCount}) != nil { + return ErrConditions + } + + if s, ok := e.Attribute(document.Name{Local: xmlNameCount}); ok { + if !nonNegativeInteger(s) { + return ErrConditions + } + } + + cs, err := xmlutil.Children(e) + if err != nil { + return ErrConditions + } + + for _, v := range cs { + if v.Name() != xmlutil.Name(AssertionNamespace, xmlNameAudience) || !simpleURI(v) { + return ErrConditions + } + } + + return nil +} + +func nonNegativeInteger(s string) bool { + const ( + maximumProxyCountLexicalBytes = 4096 + ) + + s = strings.Trim(s, lexicon.XMLWhitespace) + + negative := false + if len(s) > 0 && (s[0] == '+' || s[0] == '-') { + negative = s[0] == '-' + s = s[1:] + } + + if s == "" || len(s) > maximumProxyCountLexicalBytes { + return false + } + + for _, c := range s { + if c < '0' || c > '9' || negative && c != '0' { + return false + } + } + + return true +} + +func verifyResponseAssertion(ctx context.Context, d *document.Document, payload document.Element, keys []crypto.PublicKey, o QueryResponseOptions, l Limits, state *decryptionState) (*VerifiedAuthorityAssertion, error) { + ad, err := tree.ScopeDocument(d, payload) + if err != nil { + return nil, err + } + + if payload.Name() == xmlutil.Name(AssertionNamespace, xmlNameEncryptedAssertion) { + ad, err = state.decryptSAML(ctx, ad, ad.Root(), nameAssertion, true) + if err != nil { + return nil, err + } + } + + if ad.Root().Name() != xmlutil.Name(AssertionNamespace, nameAssertion) { + return nil, ErrProfile + } + + children := ad.Root().Children() + if len(children) == 0 { + return nil, ErrProfile + } + + who, err := xmlutil.SimpleText(children[0], maximumURITextBytes) + if err != nil || !profile.URI(who) { + return nil, ErrProfile + } + + trusted := keys + if o.AssertionTrust != nil { + trusted, err = o.AssertionTrust(ctx, who) + if err != nil { + return nil, err + } + } else if who != o.Issuer { + return nil, ErrSignature + } + + if err := targetSignature(ctx, ad, ad.Root(), trusted, o.Policy); err != nil { + return nil, err + } + + return parseAuthorityAssertion(ctx, ad, AssertionOptions{Issuer: who, Audience: o.Audience, At: o.At, Limits: o.Limits, Policy: o.Policy, Decryption: o.Decryption}, l, state) +} + +func matchAssertionStatements(ctx context.Context, a *VerifiedAuthorityAssertion, q Query, o QueryMatchOptions) (matched, session, contextMatch bool, err error) { + for _, st := range a.statements { + yes, s, c, e := matchStatement(ctx, st, q, o) + if e != nil { + return false, false, false, e + } + + matched = matched || yes + session = session || s + contextMatch = contextMatch || c + } + + return +} + +func matchStatement(ctx context.Context, st document.Element, q Query, o QueryMatchOptions) (bool, bool, bool, error) { + switch q.Kind { + case AuthenticationQuery: + if st.Name() != xmlutil.Name(AssertionNamespace, xmlNameAuthnStatement) { + return false, false, false, nil + } + + contextMatch := true + + if q.Context != nil { + var err error + + contextMatch, err = matchContext(ctx, st, *q.Context, o) + if err != nil { + return false, false, false, err + } + } + + return true, xmlutil.Attr(st, xmlNameSessionIndex) == q.SessionIndex, contextMatch, nil + case AttributeQuery: + if st.Name() != xmlutil.Name(AssertionNamespace, xmlNameAttributeStatement) { + return false, false, false, nil + } + + return true, false, false, matchAttributes(ctx, st, q.Attributes, o) + case AuthorizationQuery: + if st.Name() != xmlutil.Name(AssertionNamespace, xmlNameAuthzDecisionStatement) { + return false, false, false, nil + } + + return true, false, false, matchAuthorization(st, q) + default: + return false, false, false, ErrProfile + } +} + +func matchAuthorization(st document.Element, q Query) error { + if xmlutil.Attr(st, xmlNameResource) != q.Resource { + return ErrCorrelation + } + + for _, v := range st.Children() { + if v.Name() != xmlutil.Name(AssertionNamespace, xmlNameAction) { + continue + } + + a, err := readAction(v) + if err != nil { + return err + } + + matched := false + for _, want := range q.Actions { + matched = matched || actionEqual(a, want) + } + + if !matched { + return ErrCorrelation + } + } + + return nil +} + +func findRequestedAttribute(wants []*document.Document, key [2]string) *document.Document { + for _, w := range wants { + wk, _ := queryAttribute(w.Root()) + if wk == key { + return w + } + } + + return nil +} + +func matchAttributeValues(ctx context.Context, attribute document.Element, values []document.Element, o QueryMatchOptions) error { + if len(values) == 0 { + return nil + } + + for _, v := range attribute.Children() { + match := false + + for _, allowed := range values { + var ( + ok bool + err error + ) + if o.EqualValue != nil { + ok, err = o.EqualValue(ctx, v, allowed) + } else { + ok, err = sameXML(ctx, v, allowed) + } + + if err != nil { + return err + } + + match = match || ok + } + + if !match { + return ErrCorrelation + } + } + + return nil +} diff --git a/saml/protocol/query_statement.go b/saml/protocol/query_statement.go new file mode 100644 index 0000000..63b56ab --- /dev/null +++ b/saml/protocol/query_statement.go @@ -0,0 +1,173 @@ +package protocol + +import ( + "slices" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/saml/internal/profile" + "github.com/skarm/xmlsec/saml/internal/xmlutil" +) + +func validateAuthorityStatement(e document.Element) error { + if e.Name().Space != AssertionNamespace { + return ErrProfile + } + + children, err := xmlutil.Children(e) + if err != nil { + return err + } + + switch e.Name().Local { + case xmlNameAuthnStatement: + return validateAuthenticationStatement(e, children) + case xmlNameAttributeStatement: + return validateAttributeStatement(e, children) + case xmlNameAuthzDecisionStatement: + return validateAuthorizationStatement(e, children) + default: + return ErrProfile + } +} + +func simpleURI(e document.Element) bool { + s, err := xmlutil.Simple(e, maximumURITextBytes) + return err == nil && profile.URI(s) +} + +func validateAuthenticationStatement(e document.Element, cs []document.Element) error { + const ( + xmlNameDNSName = "DNSName" + xmlNameSubjectLocality = "SubjectLocality" + ) + + if xmlutil.Attrs(e, []string{xmlNameAuthnInstant}, []string{xmlNameSessionIndex, nameSessionNotOnOrAfter}) != nil { + return ErrProfile + } + + instant, err := profile.ParseTime(xmlutil.Attr(e, xmlNameAuthnInstant)) + if err != nil { + return err + } + + if s, ok := e.Attribute(document.Name{Local: nameSessionNotOnOrAfter}); ok { + end, err := profile.ParseTime(s) + if err != nil || !instant.Before(end) { + return ErrConditions + } + } + + if len(cs) > 0 && cs[0].Name() == xmlutil.Name(AssertionNamespace, xmlNameSubjectLocality) { + if xmlutil.Attrs(cs[0], nil, []string{nameAddress, xmlNameDNSName}) != nil || !xmlutil.EmptyContent(cs[0]) { + return ErrProfile + } + + cs = cs[1:] + } + + if len(cs) != 1 || cs[0].Name() != xmlutil.Name(AssertionNamespace, xmlNameAuthnContext) || xmlutil.Attrs(cs[0], nil, nil) != nil { + return ErrProfile + } + + return validateAuthenticationContext(cs[0]) +} + +func validateAttributeStatement(e document.Element, cs []document.Element) error { + const ( + maximumStatementAttributes = 512 + ) + + if xmlutil.Attrs(e, nil, nil) != nil || len(cs) == 0 || len(cs) > maximumStatementAttributes { + return ErrProfile + } + + for _, a := range cs { + if _, err := queryAttribute(a); err != nil { + return err + } + } + + return nil +} + +func validateAuthorizationStatement(e document.Element, cs []document.Element) error { + const ( + decisionDeny = "Deny" + decisionIndeterminate = "Indeterminate" + decisionPermit = "Permit" + maximumAuthorizationStatementChildren = 129 + xmlNameDecision = "Decision" + ) + + if xmlutil.Attrs(e, []string{xmlNameResource, xmlNameDecision}, nil) != nil || !uriReference(xmlutil.Attr(e, xmlNameResource)) || !slices.Contains([]string{decisionPermit, decisionDeny, decisionIndeterminate}, xmlutil.Attr(e, xmlNameDecision)) { + return ErrProfile + } + + if len(cs) == 0 || len(cs) > maximumAuthorizationStatementChildren { + return ErrProfile + } + + if cs[len(cs)-1].Name() == xmlutil.Name(AssertionNamespace, xmlNameEvidence) { + if err := validateEvidence(cs[len(cs)-1]); err != nil { + return err + } + + cs = cs[:len(cs)-1] + } + + if len(cs) == 0 { + return ErrProfile + } + + for _, a := range cs { + if _, err := readAction(a); err != nil { + return err + } + } + + return nil +} + +func validateAuthenticationContext(context document.Element) error { + const ( + maximumAuthenticationContextChildren = 130 + xmlNameAuthnContextDecl = "AuthnContextDecl" + ) + + refs, err := xmlutil.Children(context) + if err != nil || len(refs) == 0 || len(refs) > maximumAuthenticationContextChildren { + return ErrProfile + } + + i := 0 + + if refs[0].Name() == xmlutil.Name(AssertionNamespace, xmlNameAuthnContextClassRef) { + if !simpleURI(refs[0]) { + return ErrProfile + } + + i++ + } + + if i < len(refs) && refs[i].Name() == xmlutil.Name(AssertionNamespace, xmlNameAuthnContextDecl) { + i++ + } else if i < len(refs) && refs[i].Name() == xmlutil.Name(AssertionNamespace, xmlNameAuthnContextDeclRef) { + if !simpleURI(refs[i]) { + return ErrProfile + } + + i++ + } + + if i == 0 { + return ErrProfile + } + + for _, c := range refs[i:] { + if c.Name() != xmlutil.Name(AssertionNamespace, xmlNameAuthenticatingAuthority) || !simpleURI(c) { + return ErrProfile + } + } + + return nil +} diff --git a/saml/protocol/query_subject.go b/saml/protocol/query_subject.go new file mode 100644 index 0000000..836e32e --- /dev/null +++ b/saml/protocol/query_subject.go @@ -0,0 +1,145 @@ +package protocol + +import ( + "context" + "slices" + "strings" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/lexicon" + "github.com/skarm/xmlsec/internal/xmlchar" + "github.com/skarm/xmlsec/saml/internal/profile" + "github.com/skarm/xmlsec/saml/internal/xmlutil" +) + +func validateQueryConfirmation(e document.Element) error { + if e.Name() != xmlutil.Name(AssertionNamespace, xmlNameSubjectConfirmation) || xmlutil.Attrs(e, []string{nameMethod}, nil) != nil || !profile.URI(xmlutil.Attr(e, nameMethod)) { + return ErrProfile + } + + cs, err := xmlutil.Children(e) + if err != nil { + return err + } + + if len(cs) > 0 && cs[0].Name() == xmlutil.Name(AssertionNamespace, xmlNameNameID) { + if _, err := readNameID(cs[0]); err != nil { + return err + } + + cs = cs[1:] + } + + if len(cs) == 0 { + return nil + } + + if len(cs) != 1 || cs[0].Name() != xmlutil.Name(AssertionNamespace, xmlNameSubjectConfirmationData) { + return ErrProfile + } + + for _, a := range cs[0].Attributes() { + if a.Name.Space == "" && !slices.Contains([]string{nameNotBefore, nameNotOnOrAfter, nameRecipient, nameInResponseTo, nameAddress}, a.Name.Local) { + return ErrProfile + } + } + + for _, name := range []string{nameNotBefore, nameNotOnOrAfter} { + if s, ok := cs[0].Attribute(document.Name{Local: name}); ok { + if _, err := profile.ParseTime(s); err != nil { + return err + } + } + } + + if s, ok := cs[0].Attribute(document.Name{Local: nameInResponseTo}); ok && !profile.ID(s) { + return ErrProfile + } + + if s, ok := cs[0].Attribute(document.Name{Local: nameRecipient}); ok && !uriReference(s) { + return ErrProfile + } + + return nil +} + +func attributeAttributes(e document.Element) error { + for _, a := range e.Attributes() { + if a.Name.Space == "" && !slices.Contains([]string{nameAttributeName, xmlNameNameFormat, xmlNameFriendlyName}, a.Name.Local) { + return ErrProfile + } + } + + if _, ok := e.Attribute(document.Name{Local: nameAttributeName}); !ok { + return ErrProfile + } + + return nil +} + +func attributeValueType(e document.Element) error { + const ( + xsiNilAttribute = "nil" + ) + + const xsi = lexicon.NamespaceXSI + if value, ok := e.Attribute(document.Name{Space: xsi, Local: xsiNilAttribute}); ok { + nilValue, err := profile.Bool(value) + if err != nil { + return err + } + + if nilValue && (len(e.Children()) != 0 || e.Text() != "") { + return ErrProfile + } + } + + if value, ok := e.Attribute(document.Name{Space: xsi, Local: lexicon.AttrType}); ok { + value = strings.Trim(value, lexicon.XMLWhitespace) + + prefix, local, qualified := strings.Cut(value, ":") + if !qualified { + if !xmlchar.IsValidNCName(prefix) { + return ErrProfile + } + + return nil + } + + if !xmlchar.IsValidNCName(prefix) || !xmlchar.IsValidNCName(local) { + return ErrProfile + } + + if uri, ok := e.Namespace(prefix); !ok || uri == "" { + return ErrProfile + } + } + + return nil +} + +func encryptSubjectIdentifiers(ctx context.Context, d *document.Document, o *EncryptionOptions) (*document.Document, error) { + found := false + + for { + target := authoritySubjectChild(d, xmlNameNameID) + if !target.Valid() { + break + } + + found = true + + var err error + + d, err = encryptSAML(ctx, d, target, xmlNameEncryptedID, o) + if err != nil { + return nil, err + } + } + + if !found { + return nil, ErrEncryption + } + + return d, nil +} diff --git a/saml/protocol/query_test.go b/saml/protocol/query_test.go new file mode 100644 index 0000000..f867c36 --- /dev/null +++ b/saml/protocol/query_test.go @@ -0,0 +1,572 @@ +package protocol_test + +import ( + "bytes" + "context" + "crypto" + "errors" + "fmt" + "strings" + "testing" + "time" + + "github.com/skarm/xmlsec/c14n" + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/testutil/testcert" + "github.com/skarm/xmlsec/saml/binding" + "github.com/skarm/xmlsec/saml/metadata" + "github.com/skarm/xmlsec/saml/protocol" + "github.com/skarm/xmlsec/xmldsig" +) + +func TestAuthorityNamespaceAndValueIntegrity(t *testing.T) { + c := testcert.New(t) + key := protocol.SigningKey{Key: c.LeafKey} + keys := []crypto.PublicKey{c.LeafKey.Public()} + q := queryRequest(t, protocol.AttributeQuery) + value := `reader` + q.Attributes = []*document.Document{queryXML(t, value)} + statement := queryXML(t, ``+value+``) + issued, err := protocol.SignAuthorityAssertion(t.Context(), protocol.AuthorityAssertion{ID: "_typed", Issuer: idp, IssueInstant: testcert.Now(), Subject: q.Subject, Statements: []*document.Document{statement}}, key) + if err != nil { + t.Fatal(err) + } + raw, _ := issued.XML(t.Context()) + mutant := bytes.ReplaceAll(raw, []byte("urn:original:type"), []byte("urn:modified:type")) + changed, err := document.Parse(t.Context(), mutant, document.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + if _, err = protocol.VerifyAuthorityAssertion(t.Context(), changed, keys, protocol.AssertionOptions{Issuer: idp, At: testcert.Now()}); err == nil { + t.Fatal("QName namespace changed outside signature") + } + v, err := protocol.VerifyAuthorityAssertion(t.Context(), issued, keys, protocol.AssertionOptions{Issuer: idp, At: testcert.Now()}) + if err != nil { + t.Fatal(err) + } + if err = protocol.MatchQueryResponse(t.Context(), q, []*protocol.VerifiedAuthorityAssertion{v}, protocol.QueryMatchOptions{}); err != nil { + t.Fatal(err) + } + q.Attributes = []*document.Document{queryXML(t, strings.ReplaceAll(value, "urn:original:type", "urn:other:type"))} + if err = protocol.MatchQueryResponse(t.Context(), q, []*protocol.VerifiedAuthorityAssertion{v}, protocol.QueryMatchOptions{}); err == nil { + t.Fatal("different QName-valued attribute type matched") + } + q = queryRequest(t, protocol.AttributeQuery) + q.Limits.MaxValueBytes = 16 + if _, err = protocol.SignQuery(t.Context(), q, key); err == nil { + t.Fatal("attribute XML byte budget ignored") + } +} + +func TestAuthorityConditionsAndContexts(t *testing.T) { + c := testcert.New(t) + key := protocol.SigningKey{Key: c.LeafKey} + keys := []crypto.PublicKey{c.LeafKey.Public()} + q := queryRequest(t, protocol.AuthenticationQuery) + a := authorityAssertion(t, q, "_context", key) + v, err := protocol.VerifyAuthorityAssertion(t.Context(), a, keys, protocol.AssertionOptions{Issuer: idp, At: testcert.Now()}) + if err != nil { + t.Fatal(err) + } + for _, mode := range []string{"minimum", "maximum", "better"} { + t.Run(mode, func(t *testing.T) { + q.Context.Comparison = mode + if err = protocol.MatchQueryResponse(t.Context(), q, []*protocol.VerifiedAuthorityAssertion{v}, protocol.QueryMatchOptions{}); err == nil { + t.Fatal("context ordering guessed") + } + called := false + matcher := protocol.QueryMatchOptions{Context: func(_ context.Context, ac document.Element, w protocol.RequestedContext) (bool, error) { + called = true + return ac.Children()[0].Text() == "urn:example:strong" && w.Comparison == mode, nil + }} + if err = protocol.MatchQueryResponse(t.Context(), q, []*protocol.VerifiedAuthorityAssertion{v}, matcher); err != nil || !called { + t.Fatalf("explicit ordering %s: %v", mode, err) + } + }) + } + for _, condition := range []struct { + name, xml string + valid bool + }{ + {"audience-or-and", `urn:other` + sp + `` + sp + ``, true}, + {"audience-and-fails", `` + sp + `urn:other`, false}, + {"unknown-condition", ``, false}, + {"duplicate-one-time", ``, false}, + {"large-proxy-count", ``, true}, + } { + t.Run(condition.name, func(t *testing.T) { + d, err := protocol.SignAuthorityAssertion(t.Context(), protocol.AuthorityAssertion{ID: "_condition", Issuer: idp, IssueInstant: testcert.Now(), Subject: q.Subject, Statements: []*document.Document{queryStatement(t, protocol.AuthenticationQuery)}, Conditions: queryXML(t, ``+condition.xml+``)}, key) + if err != nil { + t.Fatal(err) + } + _, err = protocol.VerifyAuthorityAssertion(t.Context(), d, keys, protocol.AssertionOptions{Issuer: idp, Audience: sp, At: testcert.Now()}) + if (err == nil) != condition.valid { + t.Fatalf("condition validity %v: %v", condition.valid, err) + } + }) + } +} + +// Core 2.6.1 / 2.7.4.3 distinguish extensible Advice from closed Evidence. +func TestAuthorityAdviceAndEvidence(t *testing.T) { + c := testcert.New(t) + key := protocol.SigningKey{Key: c.LeafKey} + keys := []crypto.PublicKey{c.LeafKey.Public()} + q := queryRequest(t, protocol.AuthorizationQuery) + for _, test := range []struct { + name, body string + valid bool + }{ + {"empty", "", true}, + {"references", `_proofhttps://issuer.test/proof`, true}, + {"foreign", ``, true}, + {"bad-ID", `not an ID`, false}, + {"empty-reference", ``, false}, + {"unknown-saml", ``, false}, + {"unqualified", ``, false}, + } { + t.Run(test.name, func(t *testing.T) { + advice := queryXML(t, ``+test.body+``) + d, err := protocol.SignAuthorityAssertion(t.Context(), protocol.AuthorityAssertion{ID: "_advice", Issuer: idp, IssueInstant: testcert.Now(), Subject: q.Subject, Statements: []*document.Document{queryStatement(t, q.Kind)}, Advice: advice}, key) + if (err == nil) != test.valid { + t.Fatalf("Advice validity %v: %v", test.valid, err) + } + if test.valid { + if _, err = protocol.VerifyAuthorityAssertion(t.Context(), d, keys, protocol.AssertionOptions{Issuer: idp, At: testcert.Now()}); err != nil { + t.Fatal(err) + } + } + q.Evidence = queryXML(t, ``+test.body+``) + _, err = protocol.SignQuery(t.Context(), q, key) + want := test.name == "references" + if (err == nil) != want { + t.Fatalf("Evidence validity %v: %v", want, err) + } + }) + } +} + +func TestQuerySubjectConfirmationAndExtensions(t *testing.T) { + c := testcert.New(t) + key := protocol.SigningKey{Key: c.LeafKey} + keys := []crypto.PublicKey{c.LeafKey.Public()} + q := queryRequest(t, protocol.AuthenticationQuery) + q.Subject = queryXML(t, ``) + q.Consent = "urn:oasis:names:tc:SAML:2.0:consent:obtained" + q.Extensions = queryXML(t, `strict`) + d, err := protocol.SignQuery(t.Context(), q, key) + if err != nil { + t.Fatal(err) + } + vq, err := protocol.VerifyQuery(t.Context(), d, keys, protocol.ExchangeOptions{Issuer: sp, Destination: sso, At: testcert.Now(), Binding: metadata.SOAP}) + if err != nil || vq.Query().Extensions == nil || vq.Query().Consent != q.Consent { + t.Fatalf("protocol extensions: %v", err) + } + a := authorityAssertion(t, q, "_confirmation", key) + v, err := protocol.VerifyAuthorityAssertion(t.Context(), a, keys, protocol.AssertionOptions{Issuer: idp, At: testcert.Now()}) + if err != nil { + t.Fatal(err) + } + if err = protocol.MatchQueryResponse(t.Context(), q, []*protocol.VerifiedAuthorityAssertion{v}, protocol.QueryMatchOptions{}); err == nil { + t.Fatal("confirmation treated as name equality") + } + matcher := protocol.QueryMatchOptions{Confirm: func(_ context.Context, a, b document.Element) (bool, error) { + x, _ := a.Children()[0].Attribute(document.Name{Local: "Recipient"}) + y, _ := b.Children()[0].Attribute(document.Name{Local: "Recipient"}) + return x == y, nil + }} + if err = protocol.MatchQueryResponse(t.Context(), q, []*protocol.VerifiedAuthorityAssertion{v}, matcher); err != nil { + t.Fatal(err) + } + q.Subject = queryXML(t, ``) + if _, err = protocol.SignQuery(t.Context(), q, key); err == nil { + t.Fatal("duplicate confirmation data accepted") + } +} + +func FuzzAuthorityQueryParsers(f *testing.F) { + c := testcert.New(f) + key := protocol.SigningKey{Key: c.LeafKey} + keys := []crypto.PublicKey{c.LeafKey.Public()} + sub, _ := protocol.NewSubject(f.Context(), protocol.NameIdentifier{Value: "alice"}) + q := protocol.Query{ID: "_fuzz", Issuer: sp, Destination: sso, IssueInstant: testcert.Now(), Kind: protocol.AttributeQuery, Subject: sub} + d, err := protocol.SignQuery(f.Context(), q, key) + if err != nil { + f.Fatal(err) + } + raw, _ := d.XML(f.Context()) + f.Add(raw) + response, err := protocol.SignQueryResponse(f.Context(), protocol.QueryResponse{Issuer: idp, IssueInstant: testcert.Now(), InResponseTo: q.ID}, key) + if err != nil { + f.Fatal(err) + } + raw, _ = response.XML(f.Context()) + f.Add(raw) + identifier, err := protocol.NameIDDocument(f.Context(), protocol.NameIdentifier{Value: "alice"}) + if err != nil { + f.Fatal(err) + } + mapping, err := protocol.SignNameIDMappingResponse(f.Context(), protocol.NameIDMappingResponse{Issuer: idp, IssueInstant: testcert.Now(), InResponseTo: q.ID, Identifier: identifier}, key) + if err != nil { + f.Fatal(err) + } + raw, _ = mapping.XML(f.Context()) + f.Add(raw) + f.Add([]byte(``)) + f.Fuzz(func(t *testing.T, raw []byte) { + if len(raw) > 16384 { + t.Skip() + } + d, err := document.Parse(t.Context(), raw, document.DefaultLimits()) + if err != nil { + return + } + o := protocol.ExchangeOptions{Issuer: sp, Destination: sso, At: testcert.Now(), Binding: metadata.SOAP} + _, _ = protocol.VerifyQuery(t.Context(), d, keys, o) + _, _ = protocol.VerifyNameIDMappingRequest(t.Context(), d, keys, o) + o.Issuer, o.Destination, o.ExpectedRequestID = idp, "", q.ID + _, _ = protocol.VerifyQueryResponse(t.Context(), d, keys, q, protocol.QueryResponseOptions{ExchangeOptions: o}) + _, _ = protocol.VerifyNameIDMappingResponse(t.Context(), d, keys, o) + }) +} + +func TestIndependentAssertionIssuerTrust(t *testing.T) { + c := testcert.New(t) + q := queryRequest(t, protocol.AttributeQuery) + a, err := protocol.SignAuthorityAssertion(t.Context(), protocol.AuthorityAssertion{ID: "_delegated", Issuer: "urn:delegated:authority", IssueInstant: testcert.Now(), Subject: q.Subject, Statements: []*document.Document{queryStatement(t, q.Kind)}}, protocol.SigningKey{Key: c.TSAKey}) + if err != nil { + t.Fatal(err) + } + spec := protocol.QueryResponse{ID: "_delegated_response", Issuer: idp, IssueInstant: testcert.Now(), InResponseTo: q.ID, Assertions: []*document.Document{a}} + if _, err = protocol.SignQueryResponse(t.Context(), spec, protocol.SigningKey{Key: c.LeafKey}); err == nil { + t.Fatal("foreign signature preserved without its key") + } + key := protocol.SigningKey{Key: c.LeafKey, PreserveKeys: func(_ context.Context, parent document.Element) ([]crypto.PublicKey, error) { + if parent.Children()[0].Text() != "urn:delegated:authority" { + return nil, protocol.ErrSignature + } + return []crypto.PublicKey{c.TSAKey.Public()}, nil + }} + r, err := protocol.SignQueryResponse(t.Context(), spec, key) + if err != nil { + t.Fatal(err) + } + o := protocol.QueryResponseOptions{Issuer: idp, ExpectedRequestID: q.ID, At: testcert.Now(), Binding: metadata.SOAP, Audience: sp} + keys := []crypto.PublicKey{c.LeafKey.Public()} + if _, err = protocol.VerifyQueryResponse(t.Context(), r, keys, q, o); err == nil { + t.Fatal("envelope issuer trusted for a different assertion issuer") + } + o.AssertionTrust = func(_ context.Context, who string) ([]crypto.PublicKey, error) { + if who != "urn:delegated:authority" { + return nil, protocol.ErrSignature + } + return []crypto.PublicKey{c.TSAKey.Public()}, nil + } + v, err := protocol.VerifyQueryResponse(t.Context(), r, keys, q, o) + if err != nil || v.Assertions()[0].Issuer() != "urn:delegated:authority" { + t.Fatalf("separately trusted issuer: %v", err) + } +} + +func queryXML(t *testing.T, s string) *document.Document { + t.Helper() + d, err := document.Parse(t.Context(), []byte(s), document.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + return d +} + +func queryStatement(t *testing.T, kind protocol.QueryKind) *document.Document { + t.Helper() + switch kind { + case protocol.AuthenticationQuery: + return queryXML(t, `urn:example:strong`) + case protocol.AttributeQuery: + return queryXML(t, `reader`) + default: + return queryXML(t, `read`) + } +} + +func queryRequest(t *testing.T, kind protocol.QueryKind) protocol.Query { + t.Helper() + sub, err := protocol.NewSubject(t.Context(), protocol.NameIdentifier{Value: "alice", Format: protocol.NameIDPersistent, NameQualifier: idp, SPNameQualifier: sp}) + if err != nil { + t.Fatal(err) + } + q := protocol.Query{ID: "_query", Issuer: sp, Destination: sso, IssueInstant: testcert.Now(), Kind: kind, Subject: sub} + switch kind { + case protocol.AuthenticationQuery: + q.SessionIndex = "past-session" + q.Context = &protocol.RequestedContext{Classes: []string{"urn:example:strong"}} + case protocol.AttributeQuery: + q.Attributes = []*document.Document{queryXML(t, `reader`)} + case protocol.AuthorizationQuery: + q.Resource = "urn:example:resource" + q.Actions = []protocol.Action{{Namespace: "urn:example:actions", Value: "read"}} + case protocol.AssertionRequest: + q.Subject = nil + q.AssertionIDs = []string{"_assertion"} + } + return q +} + +func authorityAssertion(t *testing.T, q protocol.Query, id string, key protocol.SigningKey) *document.Document { + t.Helper() + sub := q.Subject + if sub == nil { + sub, _ = protocol.NewSubject(t.Context(), protocol.NameIdentifier{Value: "alice"}) + } + s := protocol.AuthorityAssertion{ID: id, Issuer: idp, IssueInstant: testcert.Now(), Subject: sub, Statements: []*document.Document{queryStatement(t, q.Kind)}} + d, err := protocol.SignAuthorityAssertion(t.Context(), s, key) + if err != nil { + t.Fatal(err) + } + return d +} + +// Core 3.3: three distinct query contracts, empty Success, multiple assertions, +// complete subject matching, independently signed assertions and SOAP context. +func TestAuthorityQueries(t *testing.T) { + c := testcert.New(t) + keys := []crypto.PublicKey{c.LeafKey.Public()} + for _, canonical := range []string{c14n.Exclusive, c14n.Canonical10} { + for _, kind := range []protocol.QueryKind{protocol.AuthenticationQuery, protocol.AttributeQuery, protocol.AuthorizationQuery, protocol.AssertionRequest} { + t.Run(canonical+"/"+string(kind), func(t *testing.T) { + key := protocol.SigningKey{Key: c.LeafKey, CertificateDER: c.Leaf.Raw, Canonicalization: canonical} + q := queryRequest(t, kind) + d, err := protocol.SignQuery(t.Context(), q, key) + if err != nil { + t.Fatal(err) + } + soap, err := protocol.SignSOAP(t.Context(), d, key) + if err != nil { + t.Fatal(err) + } + raw, _ := soap.XML(t.Context()) + m, err := binding.DecodeSOAP(t.Context(), raw, binding.SOAPOptions{}) + if err != nil { + t.Fatal(err) + } + payload, _ := m.Payload(t.Context()) + v, err := protocol.VerifyQuery(t.Context(), payload, keys, protocol.ExchangeOptions{Issuer: sp, Destination: sso, At: testcert.Now(), Binding: metadata.SOAP}) + if err != nil || v.Query().Kind != kind { + t.Fatalf("verify query: %v", err) + } + var assertions []*document.Document + for _, id := range []string{"_assertion", "_second"} { + if kind == protocol.AssertionRequest && id == "_second" { + continue + } + assertions = append(assertions, authorityAssertion(t, q, id, key)) + } + for _, list := range [][]*document.Document{nil, assertions} { + r, err := protocol.SignQueryResponse(t.Context(), protocol.QueryResponse{ID: "_result", Issuer: idp, IssueInstant: testcert.Now(), InResponseTo: q.ID, Assertions: list}, key) + if err != nil { + t.Fatal(err) + } + soap, err = protocol.SignSOAP(t.Context(), r, key) + if err != nil { + t.Fatal(err) + } + raw, _ = soap.XML(t.Context()) + m, err = binding.DecodeSOAP(t.Context(), raw, binding.SOAPOptions{}) + if err != nil { + t.Fatal(err) + } + payload, _ = m.Payload(t.Context()) + o := protocol.QueryResponseOptions{Issuer: idp, ExpectedRequestID: q.ID, At: testcert.Now(), Binding: metadata.SOAP, Audience: sp} + verified, err := protocol.VerifyQueryResponse(t.Context(), payload, keys, q, o) + if err != nil || len(verified.Assertions()) != len(list) { + t.Fatalf("verify response %d: %v", len(list), err) + } + bad := o + bad.ExpectedRequestID = "_other" + if _, err = protocol.VerifyQueryResponse(t.Context(), payload, keys, q, bad); err == nil { + t.Fatal("wrong correlation accepted") + } + bad = o + bad.At = testcert.Now().Add(time.Hour) + if _, err = protocol.VerifyQueryResponse(t.Context(), payload, keys, q, bad); err == nil { + t.Fatal("stale response accepted") + } + changed := bytes.Replace(raw, []byte(`ID="_result"`), []byte(`ID="_changed"`), 1) + broken, _ := binding.DecodeSOAP(t.Context(), changed, binding.SOAPOptions{}) + b, _ := broken.Payload(t.Context()) + if _, err = protocol.VerifyQueryResponse(t.Context(), b, keys, q, o); err == nil { + t.Fatal("tampered envelope accepted") + } + } + }) + } + } +} + +func TestQueryFiltersRejectDisclosure(t *testing.T) { + c := testcert.New(t) + key := protocol.SigningKey{Key: c.LeafKey} + keys := []crypto.PublicKey{c.LeafKey.Public()} + for _, kind := range []protocol.QueryKind{protocol.AuthenticationQuery, protocol.AttributeQuery, protocol.AuthorizationQuery, protocol.AssertionRequest} { + t.Run(string(kind), func(t *testing.T) { + q := queryRequest(t, kind) + a := authorityAssertion(t, q, "_assertion", key) + v, err := protocol.VerifyAuthorityAssertion(t.Context(), a, keys, protocol.AssertionOptions{Issuer: idp, At: testcert.Now()}) + if err != nil { + t.Fatal(err) + } + bad := q + switch kind { + case protocol.AuthenticationQuery: + bad.SessionIndex = "other-session" + case protocol.AttributeQuery: + bad.Attributes = []*document.Document{queryXML(t, ``)} + case protocol.AuthorizationQuery: + bad.Actions = []protocol.Action{{Namespace: "urn:example:actions", Value: "write"}} + case protocol.AssertionRequest: + bad.AssertionIDs = []string{"_different"} + } + if err = protocol.MatchQueryResponse(t.Context(), bad, []*protocol.VerifiedAuthorityAssertion{v}, protocol.QueryMatchOptions{}); err == nil { + t.Fatal("unrequested data accepted") + } + if kind != protocol.AssertionRequest { + bad = q + bad.Subject, _ = protocol.NewSubject(t.Context(), protocol.NameIdentifier{Value: "mallory"}) + if err = protocol.MatchQueryResponse(t.Context(), bad, []*protocol.VerifiedAuthorityAssertion{v}, protocol.QueryMatchOptions{}); err == nil { + t.Fatal("different subject accepted") + } + } + }) + } + q := queryRequest(t, protocol.AuthenticationQuery) + a := authorityAssertion(t, q, "_assertion", key) + v, _ := protocol.VerifyAuthorityAssertion(t.Context(), a, keys, protocol.AssertionOptions{Issuer: idp, At: testcert.Now()}) + q.Context = &protocol.RequestedContext{Classes: []string{"urn:example:unavailable"}} + err := protocol.MatchQueryResponse(t.Context(), q, []*protocol.VerifiedAuthorityAssertion{v}, protocol.QueryMatchOptions{}) + var status *protocol.StatusError + if !errors.As(err, &status) || len(status.Subcodes) != 1 || status.Subcodes[0] != protocol.NoAuthnContext { + t.Fatalf("NoAuthnContext: %v", err) + } + q = queryRequest(t, protocol.AttributeQuery) + q.Attributes = append(q.Attributes, q.Attributes[0]) + if _, err = protocol.SignQuery(t.Context(), q, key); err == nil { + t.Fatal("duplicate requested attribute accepted") + } +} + +func TestEncryptedAuthorityQueries(t *testing.T) { + c := testcert.New(t) + key := protocol.SigningKey{Key: c.LeafKey} + recipient := encryptionKey(t) + keys := []crypto.PublicKey{c.LeafKey.Public()} + q := queryRequest(t, protocol.AttributeQuery) + q.Encryption = &protocol.EncryptionOptions{Targets: protocol.EncryptNameID, Recipient: &recipient.PublicKey} + d, err := protocol.SignQuery(t.Context(), q, key) + if err != nil { + t.Fatal(err) + } + v, err := protocol.VerifyQuery(t.Context(), d, keys, protocol.ExchangeOptions{Issuer: sp, Destination: sso, At: testcert.Now(), Binding: metadata.SOAP, Decryption: &protocol.DecryptionOptions{Keys: []crypto.Decrypter{recipient}}}) + if err != nil { + t.Fatal(err) + } + if v.Query().Subject.Root().Children()[0].Text() != "alice" { + t.Fatal("wrong decrypted query subject") + } + for targets := protocol.EncryptionTargets(1); targets < 8; targets++ { + t.Run(fmt.Sprint(targets), func(t *testing.T) { + a, err := protocol.SignAuthorityAssertion(t.Context(), protocol.AuthorityAssertion{ID: "_encrypted_assertion", Issuer: idp, IssueInstant: testcert.Now(), Subject: q.Subject, Statements: []*document.Document{queryStatement(t, q.Kind)}, Encryption: &protocol.EncryptionOptions{Targets: targets, Recipient: &recipient.PublicKey}}, key) + if err != nil { + t.Fatal(err) + } + r, err := protocol.SignQueryResponse(t.Context(), protocol.QueryResponse{ID: "_response", Issuer: idp, IssueInstant: testcert.Now(), InResponseTo: q.ID, Assertions: []*document.Document{a}}, key) + if err != nil { + t.Fatal(err) + } + o := protocol.QueryResponseOptions{Issuer: idp, ExpectedRequestID: q.ID, At: testcert.Now(), Binding: metadata.SOAP, Decryption: &protocol.DecryptionOptions{Keys: []crypto.Decrypter{recipient}}, Audience: sp} + got, err := protocol.VerifyQueryResponse(t.Context(), r, keys, q, o) + if err != nil || len(got.Assertions()) != 1 { + t.Fatalf("encrypted assertion %d: %v", targets, err) + } + }) + } +} + +func TestAuthorityTimeWindowAttributePresence(t *testing.T) { + at := testcert.Now() + key := testcert.Key(t) + for _, tc := range []struct { + name, start, end string + bad bool + }{ + {"absent", "", "", false}, + {"absent_start_future_end", "", at.Add(time.Minute).Format(time.RFC3339), false}, + {"zero_start_future_end", "0001-01-01T00:00:00Z", at.Add(time.Minute).Format(time.RFC3339), true}, + {"1990_start_future_end", "1990-01-01T00:00:00Z", at.Add(time.Minute).Format(time.RFC3339), true}, + {"small_window", at.Add(-time.Minute).Format(time.RFC3339), at.Add(time.Minute).Format(time.RFC3339), false}, + {"exact_window", at.Add(-time.Minute).Format(time.RFC3339), at.Add(9 * time.Minute).Format(time.RFC3339), false}, + {"long_window", at.Add(-time.Minute).Format(time.RFC3339), at.Add(10 * time.Minute).Format(time.RFC3339), true}, + {"future_start", at.Add(time.Minute).Format(time.RFC3339), at.Add(2 * time.Minute).Format(time.RFC3339), true}, + {"zero_start_only", "0001-01-01T00:00:00Z", "", false}, + {"zero_end", "", "0001-01-01T00:00:00Z", true}, + } { + t.Run(tc.name, func(t *testing.T) { + attrs := "" + if tc.start != "" { + attrs += ` NotBefore="` + tc.start + `"` + } + if tc.end != "" { + attrs += ` NotOnOrAfter="` + tc.end + `"` + } + cond, err := document.Parse(t.Context(), []byte(``), document.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + d, err := protocol.SignAuthorityAssertion(t.Context(), protocol.AuthorityAssertion{ID: "_presence", Issuer: "https://issuer.test/entity", IssueInstant: at, Conditions: cond}, protocol.SigningKey{Key: key}) + if err != nil { + t.Fatal(err) + } + got, err := protocol.VerifyAuthorityAssertion(t.Context(), d, []crypto.PublicKey{key.Public()}, protocol.AssertionOptions{Issuer: "https://issuer.test/entity", At: at, Limits: protocol.Limits{MaxAssertionLifetime: 10 * time.Minute}}) + if tc.bad { + if got != nil || !errors.Is(err, protocol.ErrConditions) { + t.Fatalf("oversized/invalid window accepted: result=%v err=%v", got != nil, err) + } + } else if got == nil || err != nil { + t.Fatalf("valid optional/window rejected: %v", err) + } + }) + } +} + +func TestQueryResponseSignatureBudget(t *testing.T) { + at := testcert.Now() + key := testcert.Key(t) + signing := protocol.SigningKey{Key: key} + const issuer = "https://issuer.test/entity" + assertions := make([]*document.Document, 64) + ids := make([]string, 64) + for i := range assertions { + ids[i] = fmt.Sprintf("_assertion-%d", i) + var err error + assertions[i], err = protocol.SignAuthorityAssertion(t.Context(), protocol.AuthorityAssertion{ID: ids[i], Issuer: issuer, IssueInstant: at}, signing) + if err != nil { + t.Fatal(err) + } + } + spec := protocol.QueryResponse{ID: "_response", Issuer: issuer, IssueInstant: at, InResponseTo: "_query", Assertions: assertions[:63]} + response, err := protocol.SignQueryResponse(t.Context(), spec, signing) + if err != nil { + t.Fatal(err) + } + if got := len(xmldsig.Signatures(response)); got != 64 { + t.Fatalf("response plus 63 assertions signatures=%d, want 64", got) + } + query := protocol.Query{ID: "_query", Kind: protocol.AssertionRequest, AssertionIDs: ids[:63]} + verified, err := protocol.VerifyQueryResponse(t.Context(), response, []crypto.PublicKey{key.Public()}, query, protocol.QueryResponseOptions{Issuer: issuer, ExpectedRequestID: "_query", At: at, Binding: metadata.SOAP}) + if err != nil || verified == nil || len(verified.Assertions()) != 63 { + t.Fatalf("63 signed assertions failed: result=%v err=%v", verified != nil, err) + } + spec.Assertions = assertions + if response, err = protocol.SignQueryResponse(t.Context(), spec, signing); response != nil || !errors.Is(err, protocol.ErrProfile) { + t.Fatalf("64 assertions exceed shared signature budget: result=%v err=%v", response != nil, err) + } +} diff --git a/saml/protocol/request.go b/saml/protocol/request.go index 45aca45..cc74477 100644 --- a/saml/protocol/request.go +++ b/saml/protocol/request.go @@ -17,6 +17,23 @@ import ( "github.com/skarm/xmlsec/xmldsig" ) +// Local SAML vocabulary tokens shared by protocol producers and parsers. Each call site still verifies the assertion/protocol namespace or resolves the QName prefix. +const ( + xmlNameAssertionConsumerServiceIndex = "AssertionConsumerServiceIndex" + xmlNameForceAuthn = "ForceAuthn" + xmlNameIsPassive = "IsPassive" +) + +// Profile bound shared by these producer/parser paths; byte/count/time units follow the name, and default values apply only to zero options. +const ( + maximumRequestedWebContexts = 8 +) + +const ( + nameACS = "AssertionConsumerServiceURL" + nameProtocolBinding = "ProtocolBinding" +) + // RequestOptions supplies trusted issuer, destination, endpoint and time expectations // for request validation. type RequestOptions struct { @@ -26,6 +43,15 @@ type RequestOptions struct { Destination string // ACS lists assertion consumer endpoints from trusted SP metadata. ACS []metadata.Endpoint + // ResponseBinding optionally fixes the expected response binding, notably + // PAOS for ECP. Empty permits the supported binding requested by the peer. + ResponseBinding string + // TransportBinding selects the trusted receiving transport. SOAP and Artifact + // permit an omitted Destination; any present value is still checked. + TransportBinding string + // AllowHolderOfKeyACS permits HoK metadata endpoints for the requested + // transport. The caller must independently establish ECP proof of possession. + AllowHolderOfKeyACS bool // At is the explicit trusted time used by this operation. At time.Time // Limits bounds request age and supported protocol values. @@ -37,20 +63,22 @@ type RequestOptions struct { // VerifiedRequest retains an immutable AuthnRequest authenticated and accepted under // RequestOptions. type VerifiedRequest struct { - instant time.Time - doc *document.Document - id string - issuer string - destination string - acs string - format string - qualifier string - relay string - contexts []string - force bool - passive bool - allowCreate bool - hasRelay bool + instant time.Time + doc *document.Document + id string + issuer string + destination string + acs string + responseBinding string + format string + qualifier string + relay string + contexts []string + force bool + passive bool + allowCreate bool + hasRelay bool + channels []ChannelBinding } // VerifyAuthnRequest requires an XML signature. There is deliberately no public @@ -62,7 +90,7 @@ func VerifyAuthnRequest(ctx context.Context, d *document.Document, keys []crypto // VerifyRedirectRequest authenticates the received Redirect signature before decoding and // validating the AuthnRequest. func VerifyRedirectRequest(ctx context.Context, rawQuery string, keys []crypto.PublicKey, o RequestOptions, limits binding.Options) (*VerifiedRequest, error) { - if len(keys) == 0 || len(keys) > 16 { + if len(keys) == 0 || len(keys) > maximumRedirectVerificationKeys { return nil, ErrSignature } @@ -131,6 +159,14 @@ func (r *VerifiedRequest) ACS() string { return r.acs } +// ResponseBinding returns the metadata-authorized assertion response binding. +func (r *VerifiedRequest) ResponseBinding() string { return r.responseBinding } + +// ChannelBindings returns authenticated SP channel data from Extensions. +func (r *VerifiedRequest) ChannelBindings() []ChannelBinding { + return CloneChannelBindings(r.channels) +} + // IssueInstant returns the message issue instant. func (r *VerifiedRequest) IssueInstant() time.Time { if r == nil { @@ -177,6 +213,152 @@ func (r *VerifiedRequest) RelayState() (string, bool) { return r.relay, r.hasRelay } +// AuthnRequest supplies identifiers, endpoints and authentication conditions for a +// solicited request. +type AuthnRequest struct { + // IssueInstant is the required request issuance time. + IssueInstant time.Time + // ID is the required request XML ID. + ID string + // Issuer is the requesting service provider's entity identifier URI. + Issuer string + // Destination is the trusted identity provider endpoint URL. + Destination string + // ACS is the requested assertion consumer endpoint URL. + ACS string + // ResponseBinding selects HTTP-POST (default), HTTP-Artifact or PAOS. The + // recipient must authorize an ACS for the selected binding in metadata. + ResponseBinding string + // NameIDFormat optionally requests an exact subject identifier format URI. + NameIDFormat string + // RequestedContexts lists authentication context class URIs for exact comparison. + RequestedContexts []string + // ChannelBindings binds a signed ECP request to its client/SP transport. + ChannelBindings []ChannelBinding + // ForceAuthn requests fresh authentication. + ForceAuthn bool + // IsPassive forbids interaction with the subject when true. + IsPassive bool + // AllowCreateNameID permits creation of a new subject identifier when true. + AllowCreateNameID bool + // SOAPContext signs in the SOAP namespace context for artifact resolution. + SOAPContext bool +} + +// BuildAuthnRequest builds an unsigned AuthnRequest in the supported solicited profile. +func BuildAuthnRequest(ctx context.Context, s AuthnRequest) (*document.Document, error) { + if s.ResponseBinding == "" { + s.ResponseBinding = metadata.HTTPPost + } + + if !allowed(s.ResponseBinding, []string{metadata.HTTPPost, metadata.HTTPArtifact, metadata.PAOS}) || len(s.ChannelBindings) != 0 && s.ResponseBinding != metadata.PAOS { + return nil, ErrProfile + } + + if ctx == nil || s.IssueInstant.IsZero() || !profile.URI(s.Issuer) || !profile.Endpoint(s.Destination, true) || !profile.Endpoint(s.ACS, true) || s.ForceAuthn && s.IsPassive { + return nil, ErrProfile + } + + if s.ID == "" { + var e error + + s.ID, e = profile.NewID() + if e != nil { + return nil, e + } + } + + if !profile.ID(s.ID) { + return nil, ErrProfile + } + + n := xmlutil.Node(prefixProtocol, ProtocolNamespace, lexicon.SAMLElementAuthnRequest) + + n.NS = []tree.Namespace{{Prefix: prefixProtocol, URI: ProtocolNamespace}, {Prefix: prefixAssertion, URI: AssertionNamespace}, {Prefix: lexicon.PrefixDSig, URI: xmlutil.DS}} + if s.SOAPContext { + n.NS = append(n.NS, tree.Namespace{Prefix: prefixSOAP, URI: binding.SOAPNamespace}) + } + + for _, a := range [][2]string{{lexicon.SAMLAttrID, s.ID}, {nameVersion, version20}, {nameIssueInstant, profile.Time(s.IssueInstant)}, {nameDestination, s.Destination}, {nameACS, s.ACS}, {nameProtocolBinding, s.ResponseBinding}, {xmlNameForceAuthn, strconv.FormatBool(s.ForceAuthn)}, {xmlNameIsPassive, strconv.FormatBool(s.IsPassive)}} { + xmlutil.Set(n, a[0], a[1]) + } + + iss := xmlutil.Node(prefixAssertion, AssertionNamespace, xmlNameIssuer) + xmlutil.Text(iss, s.Issuer) + xmlutil.Add(n, iss) + + if err := appendChannelContainer(n, xmlNameExtensions, s.ChannelBindings, false); err != nil { + return nil, err + } + + if s.NameIDFormat != "" || s.AllowCreateNameID { + if s.NameIDFormat != "" && !profile.URI(s.NameIDFormat) { + return nil, ErrProfile + } + + p := xmlutil.Node(prefixProtocol, ProtocolNamespace, xmlNameNameIDPolicy) + if s.NameIDFormat != "" { + xmlutil.Set(p, nameFormat, s.NameIDFormat) + } + + xmlutil.Set(p, xmlNameAllowCreate, strconv.FormatBool(s.AllowCreateNameID)) + xmlutil.Add(n, p) + } + + if len(s.RequestedContexts) > 0 { + if len(s.RequestedContexts) > maximumRequestedWebContexts { + return nil, ErrProfile + } + + p := xmlutil.Node(prefixProtocol, ProtocolNamespace, xmlNameRequestedAuthnContext) + xmlutil.Set(p, xmlNameComparison, contextExact) + + for _, s := range s.RequestedContexts { + if !profile.URI(s) { + return nil, ErrProfile + } + + c := xmlutil.Node(prefixAssertion, AssertionNamespace, xmlNameAuthnContextClassRef) + xmlutil.Text(c, s) + xmlutil.Add(p, c) + } + + xmlutil.Add(n, p) + } + + return xmlutil.Document(ctx, n) +} + +// SignAuthnRequest builds and signs an AuthnRequest using the explicitly configured key. +func SignAuthnRequest(ctx context.Context, s AuthnRequest, key SigningKey) (*document.Document, error) { + d, e := BuildAuthnRequest(ctx, s) + if e != nil { + return nil, e + } + + k, e := signing(key) + if e != nil { + return nil, e + } + + out, e := xmlsig.Sign(ctx, d, d.Root(), k, true) + if e != nil { + return nil, e + } + + responseBinding := s.ResponseBinding + if responseBinding == "" { + responseBinding = metadata.HTTPPost + } + + _, e = VerifyAuthnRequest(ctx, out, []crypto.PublicKey{verificationKey(key)}, RequestOptions{Issuer: s.Issuer, Destination: s.Destination, At: s.IssueInstant, ACS: []metadata.Endpoint{{Binding: responseBinding, Location: s.ACS}}, Policy: key.Policy}) + if e != nil { + return nil, e + } + + return out, nil +} + func verifyRequest(ctx context.Context, d *document.Document, keys []crypto.PublicKey, o RequestOptions, transportVerified bool) (*VerifiedRequest, error) { limits, err := normalize(o.Limits) if err != nil { @@ -199,12 +381,12 @@ func verifyRequest(ctx context.Context, d *document.Document, keys []crypto.Publ return nil, err } - acs, err := selectRequestACS(root, o.ACS) + acs, err := selectRequestACS(root, o.ACS, o.ResponseBinding, o.AllowHolderOfKeyACS) if err != nil { return nil, err } - request := &VerifiedRequest{doc: d, id: id, instant: instant, issuer: o.Issuer, destination: o.Destination, acs: acs, format: NameIDUnspecified} + request := &VerifiedRequest{doc: d, id: id, instant: instant, issuer: o.Issuer, destination: o.Destination, acs: acs.Location, responseBinding: acs.TransportBinding(), format: NameIDUnspecified} if err := readAuthnRequestFlags(root, request); err != nil { return nil, err } @@ -213,6 +395,10 @@ func verifyRequest(ctx context.Context, d *document.Document, keys []crypto.Publ return nil, err } + if transportVerified && len(request.channels) != 0 { + return nil, ErrSignature + } + if err := ctx.Err(); err != nil { return nil, err } @@ -221,7 +407,11 @@ func verifyRequest(ctx context.Context, d *document.Document, keys []crypto.Publ } func validateRequestInputs(ctx context.Context, d *document.Document, options RequestOptions) error { - if ctx == nil || d == nil || options.At.IsZero() || !profile.URI(options.Issuer) || !profile.Endpoint(options.Destination, true) || len(options.ACS) == 0 || len(options.ACS) > 128 { + const ( + maximumAuthorizedACS = 128 + ) + + if ctx == nil || d == nil || options.At.IsZero() || !profile.URI(options.Issuer) || !profile.Endpoint(options.Destination, true) || len(options.ACS) == 0 || len(options.ACS) > maximumAuthorizedACS { return ErrProfile } @@ -229,16 +419,25 @@ func validateRequestInputs(ctx context.Context, d *document.Document, options Re } func readAuthnRequestHeader(root document.Element, options RequestOptions, limits Limits) (string, time.Time, []document.Element, error) { - if xmlutil.Attrs(root, []string{"ID", nameVersion, nameIssueInstant, nameDestination}, []string{"AssertionConsumerServiceURL", "AssertionConsumerServiceIndex", "ProtocolBinding", "ForceAuthn", "IsPassive"}) != nil { + required := []string{lexicon.SAMLAttrID, nameVersion, nameIssueInstant} + + optional := []string{nameACS, xmlNameAssertionConsumerServiceIndex, nameProtocolBinding, xmlNameForceAuthn, xmlNameIsPassive} + if options.TransportBinding == metadata.SOAP || options.TransportBinding == metadata.HTTPArtifact { + optional = append(optional, nameDestination) + } else { + required = append(required, nameDestination) + } + + if xmlutil.Attrs(root, required, optional) != nil { return "", time.Time{}, nil, ErrProfile } - id, instant, err := header(root, "AuthnRequest", options.At, limits) + id, instant, err := header(root, lexicon.SAMLElementAuthnRequest, options.At, limits) if err != nil { return "", time.Time{}, nil, err } - if xmlutil.Attr(root, nameDestination) != options.Destination { + if destination, present := root.Attribute(document.Name{Local: nameDestination}); present && destination != options.Destination { return "", time.Time{}, nil, ErrProfile } @@ -256,10 +455,10 @@ func authenticateAuthnRequest(ctx context.Context, d *document.Document, root do return 0, ErrProfile } - return 1, nil + return unsignedMessagePrefixChildren, nil } - if signingCount(d, 1) != nil || len(children) < 2 || children[1].Name() != xmlutil.Name(xmlutil.DS, lexicon.DSigElementSignature) { + if signingCount(d, 1) != nil || len(children) < signedMessagePrefixChildren || children[1].Name() != xmlutil.Name(xmlutil.DS, lexicon.DSigElementSignature) { return 0, ErrProfile } @@ -267,24 +466,24 @@ func authenticateAuthnRequest(ctx context.Context, d *document.Document, root do return 0, err } - return 2, nil + return signedMessagePrefixChildren, nil } -func selectRequestACS(root document.Element, authorized []metadata.Endpoint) (string, error) { - acs := xmlutil.Attr(root, "AssertionConsumerServiceURL") - indexString, hasIndex := root.Attribute(document.Name{Local: "AssertionConsumerServiceIndex"}) +func selectRequestACS(root document.Element, authorized []metadata.Endpoint, expectedBinding string, allowHoK bool) (metadata.Endpoint, error) { + acs, hasACS := root.Attribute(document.Name{Local: nameACS}) + indexString, hasIndex := root.Attribute(document.Name{Local: xmlNameAssertionConsumerServiceIndex}) - bindingURI := xmlutil.Attr(root, "ProtocolBinding") - if bindingURI != "" && bindingURI != metadata.HTTPPost { - return "", ErrProfile + bindingURI, hasBinding := root.Attribute(document.Name{Local: nameProtocolBinding}) + if expectedBinding != "" && hasBinding && bindingURI != expectedBinding { + return metadata.Endpoint{}, ErrProfile } - if hasIndex && (acs != "" || bindingURI != "") { - return "", ErrProfile + if hasBinding && !allowed(bindingURI, []string{metadata.HTTPPost, metadata.HTTPArtifact, metadata.PAOS}) { + return metadata.Endpoint{}, ErrProfile } - if acs != "" && bindingURI != metadata.HTTPPost { - return "", ErrProfile + if hasIndex && (hasACS || hasBinding) { + return metadata.Endpoint{}, ErrProfile } var index *uint16 @@ -292,7 +491,7 @@ func selectRequestACS(root document.Element, authorized []metadata.Endpoint) (st if hasIndex { number, err := profile.Index(indexString) if err != nil { - return "", ErrProfile + return metadata.Endpoint{}, ErrProfile } index = &number @@ -301,32 +500,45 @@ func selectRequestACS(root document.Element, authorized []metadata.Endpoint) (st var candidates []metadata.Endpoint for _, endpoint := range authorized { - if acs == "" || endpoint.Location == acs { + if !hasACS || endpoint.Location == acs { candidates = append(candidates, endpoint) } } - chosen, err := metadata.SelectACS(candidates, metadata.HTTPPost, index) + if !hasBinding { + bindingURI = expectedBinding + if bindingURI == "" { + bindingURI = metadata.HTTPPost + } + } + + chosen, err := metadata.SelectACS(candidates, bindingURI, index) + if allowHoK { + if hok, hokErr := metadata.SelectHoKACS(candidates, bindingURI, index); hokErr == nil { + chosen, err = hok, nil + } + } + if err != nil { - return "", ErrProfile + return metadata.Endpoint{}, ErrProfile } if !profile.Endpoint(chosen.Location, true) { - return "", ErrProfile + return metadata.Endpoint{}, ErrProfile } - return chosen.Location, nil + return chosen, nil } func readAuthnRequestFlags(root document.Element, request *VerifiedRequest) error { var err error - request.force, err = xmlutil.BoolAttr(root, "ForceAuthn") + request.force, err = xmlutil.BoolAttr(root, xmlNameForceAuthn) if err != nil { return err } - request.passive, err = xmlutil.BoolAttr(root, "IsPassive") + request.passive, err = xmlutil.BoolAttr(root, xmlNameIsPassive) if err != nil || request.force && request.passive { return ErrProfile } @@ -335,7 +547,22 @@ func readAuthnRequestFlags(root document.Element, request *VerifiedRequest) erro } func readAuthnRequestChildren(children []document.Element, position int, expectedIssuer string, request *VerifiedRequest) error { - if position < len(children) && children[position].Name() == xmlutil.Name(ProtocolNamespace, "NameIDPolicy") { + if position < len(children) && children[position].Name() == xmlutil.Name(ProtocolNamespace, xmlNameExtensions) { + if request.ResponseBinding() != metadata.PAOS { + return ErrProfile + } + + var err error + + request.channels, err = readChannelContainer(children[position], false) + if err != nil { + return err + } + + position++ + } + + if position < len(children) && children[position].Name() == xmlutil.Name(ProtocolNamespace, xmlNameNameIDPolicy) { if err := readRequestNameIDPolicy(children[position], expectedIssuer, request); err != nil { return err } @@ -343,7 +570,7 @@ func readAuthnRequestChildren(children []document.Element, position int, expecte position++ } - if position < len(children) && children[position].Name() == xmlutil.Name(ProtocolNamespace, "RequestedAuthnContext") { + if position < len(children) && children[position].Name() == xmlutil.Name(ProtocolNamespace, xmlNameRequestedAuthnContext) { if err := readRequestedAuthnContext(children[position], request); err != nil { return err } @@ -359,11 +586,11 @@ func readAuthnRequestChildren(children []document.Element, position int, expecte } func readRequestNameIDPolicy(policy document.Element, expectedIssuer string, request *VerifiedRequest) error { - if xmlutil.Attrs(policy, nil, []string{"Format", nameSPNameQualifier, "AllowCreate"}) != nil || !xmlutil.EmptyContent(policy) { + if xmlutil.Attrs(policy, nil, []string{nameFormat, nameSPNameQualifier, xmlNameAllowCreate}) != nil || !xmlutil.EmptyContent(policy) { return ErrProfile } - if format := xmlutil.Attr(policy, "Format"); format != "" { + if format := xmlutil.Attr(policy, nameFormat); format != "" { if !profile.URI(format) { return ErrProfile } @@ -378,18 +605,18 @@ func readRequestNameIDPolicy(policy document.Element, expectedIssuer string, req var err error - request.allowCreate, err = xmlutil.BoolAttr(policy, "AllowCreate") + request.allowCreate, err = xmlutil.BoolAttr(policy, xmlNameAllowCreate) return err } func readRequestedAuthnContext(element document.Element, request *VerifiedRequest) error { - if xmlutil.Attrs(element, nil, []string{"Comparison"}) != nil || xmlutil.Attr(element, "Comparison") != "" && xmlutil.Attr(element, "Comparison") != "exact" { + if xmlutil.Attrs(element, nil, []string{xmlNameComparison}) != nil || xmlutil.Attr(element, xmlNameComparison) != "" && xmlutil.Attr(element, xmlNameComparison) != contextExact { return ErrProfile } contexts, err := xmlutil.Children(element) - if err != nil || len(contexts) == 0 || len(contexts) > 8 { + if err != nil || len(contexts) == 0 || len(contexts) > maximumRequestedWebContexts { return ErrProfile } @@ -398,11 +625,11 @@ func readRequestedAuthnContext(element document.Element, request *VerifiedReques func appendRequestContexts(contexts []document.Element, request *VerifiedRequest) error { for _, element := range contexts { - if element.Name() != xmlutil.Name(AssertionNamespace, "AuthnContextClassRef") { + if element.Name() != xmlutil.Name(AssertionNamespace, xmlNameAuthnContextClassRef) { return ErrProfile } - value, err := xmlutil.Simple(element, 2048) + value, err := xmlutil.Simple(element, maximumURITextBytes) if err != nil || !profile.URI(value) { return ErrProfile } @@ -412,121 +639,3 @@ func appendRequestContexts(contexts []document.Element, request *VerifiedRequest return nil } - -// AuthnRequest supplies identifiers, endpoints and authentication conditions for a -// solicited request. -type AuthnRequest struct { - // ID is the required request XML ID. - ID string - // Issuer is the requesting service provider's entity identifier URI. - Issuer string - // Destination is the trusted identity provider endpoint URL. - Destination string - // ACS is the requested assertion consumer endpoint URL. - ACS string - // NameIDFormat optionally requests an exact subject identifier format URI. - NameIDFormat string - // IssueInstant is the required request issuance time. - IssueInstant time.Time - // ForceAuthn requests fresh authentication. - ForceAuthn bool - // IsPassive forbids interaction with the subject when true. - IsPassive bool - // AllowCreateNameID permits creation of a new subject identifier when true. - AllowCreateNameID bool - // RequestedContexts lists authentication context class URIs for exact comparison. - RequestedContexts []string -} - -// BuildAuthnRequest builds an unsigned AuthnRequest in the supported solicited profile. -func BuildAuthnRequest(ctx context.Context, s AuthnRequest) (*document.Document, error) { - if ctx == nil || s.IssueInstant.IsZero() || !profile.URI(s.Issuer) || !profile.Endpoint(s.Destination, true) || !profile.Endpoint(s.ACS, true) || s.ForceAuthn && s.IsPassive { - return nil, ErrProfile - } - - if s.ID == "" { - var e error - - s.ID, e = profile.NewID() - if e != nil { - return nil, e - } - } - - if !profile.ID(s.ID) { - return nil, ErrProfile - } - - n := xmlutil.Node("samlp", ProtocolNamespace, "AuthnRequest") - - n.NS = []tree.Namespace{{Prefix: "samlp", URI: ProtocolNamespace}, {Prefix: "saml", URI: AssertionNamespace}, {Prefix: "ds", URI: xmlutil.DS}} - for _, a := range [][2]string{{"ID", s.ID}, {nameVersion, version20}, {nameIssueInstant, profile.Time(s.IssueInstant)}, {nameDestination, s.Destination}, {"AssertionConsumerServiceURL", s.ACS}, {"ProtocolBinding", metadata.HTTPPost}, {"ForceAuthn", strconv.FormatBool(s.ForceAuthn)}, {"IsPassive", strconv.FormatBool(s.IsPassive)}} { - xmlutil.Set(n, a[0], a[1]) - } - - iss := xmlutil.Node("saml", AssertionNamespace, "Issuer") - xmlutil.Text(iss, s.Issuer) - xmlutil.Add(n, iss) - - if s.NameIDFormat != "" || s.AllowCreateNameID { - if s.NameIDFormat != "" && !profile.URI(s.NameIDFormat) { - return nil, ErrProfile - } - - p := xmlutil.Node("samlp", ProtocolNamespace, "NameIDPolicy") - if s.NameIDFormat != "" { - xmlutil.Set(p, "Format", s.NameIDFormat) - } - - xmlutil.Set(p, "AllowCreate", strconv.FormatBool(s.AllowCreateNameID)) - xmlutil.Add(n, p) - } - - if len(s.RequestedContexts) > 0 { - if len(s.RequestedContexts) > 8 { - return nil, ErrProfile - } - - p := xmlutil.Node("samlp", ProtocolNamespace, "RequestedAuthnContext") - xmlutil.Set(p, "Comparison", "exact") - - for _, s := range s.RequestedContexts { - if !profile.URI(s) { - return nil, ErrProfile - } - - c := xmlutil.Node("saml", AssertionNamespace, "AuthnContextClassRef") - xmlutil.Text(c, s) - xmlutil.Add(p, c) - } - - xmlutil.Add(n, p) - } - - return xmlutil.Document(ctx, n) -} - -// SignAuthnRequest builds and signs an AuthnRequest using the explicitly configured key. -func SignAuthnRequest(ctx context.Context, s AuthnRequest, key SigningKey) (*document.Document, error) { - d, e := BuildAuthnRequest(ctx, s) - if e != nil { - return nil, e - } - - k, e := signing(key) - if e != nil { - return nil, e - } - - out, e := xmlsig.Sign(ctx, d, d.Root(), k, true) - if e != nil { - return nil, e - } - - _, e = VerifyAuthnRequest(ctx, out, []crypto.PublicKey{key.Key.Public()}, RequestOptions{Issuer: s.Issuer, Destination: s.Destination, At: s.IssueInstant, ACS: []metadata.Endpoint{{Binding: metadata.HTTPPost, Location: s.ACS}}, Policy: key.Policy}) - if e != nil { - return nil, e - } - - return out, nil -} diff --git a/saml/protocol/response.go b/saml/protocol/response.go index dda9edb..3d3a6bb 100644 --- a/saml/protocol/response.go +++ b/saml/protocol/response.go @@ -10,6 +10,7 @@ import ( "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/saml/internal/profile" "github.com/skarm/xmlsec/saml/internal/xmlutil" + "github.com/skarm/xmlsec/saml/proof" "github.com/skarm/xmlsec/xmldsig" ) @@ -38,6 +39,15 @@ type ResponseOptions struct { // AllowUnsignedResponse permits a Response without an envelope signature. The // Assertion signature remains required. Unsigned envelope fields are unauthenticated. AllowUnsignedResponse bool + // AllowUnsignedAssertion accepts an unsigned assertion only when the Response + // envelope has been authenticated. A present assertion signature is always + // checked. The default continues to require both signatures. + AllowUnsignedAssertion bool + // Decryption supplies the explicit recipient key ring and exact cipher policy. + Decryption *DecryptionOptions + // RequireEncryption rejects plaintext at the selected positions. Attributes + // means every present Attribute; an absent AttributeStatement is allowed. + RequireEncryption EncryptionTargets // AllowedNameIDFormats restricts subject identifier formats to exact URIs. Empty // disables this membership check. AllowedNameIDFormats []string @@ -50,6 +60,16 @@ type ResponseOptions struct { // ExpectedAddress supplies the trusted client address to require when subject // confirmation includes Address. ExpectedAddress string + // ExpectedChannelBindings requires authenticated Advice confirming at least + // one of the bindings retained with the soliciting ECP request. + ExpectedChannelBindings []ChannelBinding + // Proof is verified possession for this exact SOAP payload, never a peer + // certificate supplied independently of the received message. + // Supplying it selects HoK and never falls back to bearer confirmation. + Proof *proof.Verified + // RequireHolderOfKey rejects a bearer downgrade when HoK was requested. + RequireHolderOfKey bool + producedCertificate []byte } // Attribute is a SAML attribute and its string values. @@ -78,6 +98,7 @@ type VerifiedAssertion struct { requestID string nameID string nameIDFormat string + identifier NameIdentifier sessionIndex string contextClass string instant time.Time @@ -86,11 +107,23 @@ type VerifiedAssertion struct { sessionExpires time.Time attributes []Attribute responseSigned bool + encryption EncryptionTargets + channels []ChannelBinding + confirmation string + hokCertificate []byte } // VerifyResponse authenticates and validates a solicited response and its signed assertion // under explicit expectations. func VerifyResponse(ctx context.Context, d *document.Document, keys []crypto.PublicKey, o ResponseOptions) (*VerifiedAssertion, error) { + const ( + minimumWebResponseChildren = 2 + ) + + if o.Proof != nil && !o.Proof.Covers(d) { + return nil, ErrConditions + } + l, e := normalize(o.Limits) if e != nil { return nil, e @@ -100,16 +133,25 @@ func VerifyResponse(ctx context.Context, d *document.Document, keys []crypto.Pub return nil, e } + if !validEncryptionTargets(o.RequireEncryption) || o.RequireEncryption != 0 && o.Decryption == nil { + return nil, ErrEncryption + } + + decryption, e := newDecryptionState(o.Decryption) + if e != nil { + return nil, e + } + if e = ctx.Err(); e != nil { return nil, e } root := d.Root() - if xmlutil.Attrs(root, []string{"ID", nameVersion, nameIssueInstant, nameDestination, nameInResponseTo}, nil) != nil { + if xmlutil.Attrs(root, []string{lexicon.SAMLAttrID, nameVersion, nameIssueInstant, nameDestination, nameInResponseTo}, nil) != nil { return nil, ErrProfile } - responseID, _, e := header(root, "Response", o.At, l) + responseID, _, e := header(root, lexicon.SAMLElementResponse, o.At, l) if e != nil { return nil, e } @@ -119,7 +161,7 @@ func VerifyResponse(ctx context.Context, d *document.Document, keys []crypto.Pub } children, e := xmlutil.Children(root) - if e != nil || len(children) < 2 || issuer(children[0], o.Issuer) != nil { + if e != nil || len(children) < minimumWebResponseChildren || issuer(children[0], o.Issuer) != nil { return nil, ErrProfile } @@ -137,42 +179,96 @@ func VerifyResponse(ctx context.Context, d *document.Document, keys []crypto.Pub return nil, ErrSignature } - if pos >= len(children) || children[pos].Name() != xmlutil.Name(ProtocolNamespace, "Status") { + if pos >= len(children) || children[pos].Name() != xmlutil.Name(ProtocolNamespace, xmlNameStatus) { return nil, ErrProfile } status := children[pos] pos++ - code, e := parseResponseStatus(status) + parsedStatus, e := readStatus(status) if e != nil { return nil, e } - if code != Success { + if parsedStatus.Code != Success { if !responseSigned || pos != len(children) || signingCount(d, 1) != nil { return nil, ErrProfile } - return nil, &StatusError{Code: code} + return nil, &StatusError{Code: parsedStatus.Code, Subcodes: parsedStatus.Subcodes, Message: parsedStatus.Message} } - if pos+1 != len(children) || children[pos].Name() != xmlutil.Name(AssertionNamespace, "Assertion") { + if pos+1 != len(children) { return nil, ErrProfile } - assertion := children[pos] + return verifyAssertionPayload(ctx, d, children[pos], keys, o, l, decryption, responseID, responseSigned) +} + +func verifyAssertionPayload(ctx context.Context, d *document.Document, payload document.Element, keys []crypto.PublicKey, o ResponseOptions, l Limits, decryption *decryptionState, responseID string, responseSigned bool) (*VerifiedAssertion, error) { + var ( + e error + encrypted EncryptionTargets + ) + + if payload.Name() == xmlutil.Name(AssertionNamespace, xmlNameEncryptedAssertion) { + d, e = decryption.decryptSAML(ctx, d, payload, nameAssertion, responseSigned) + if e != nil { + return nil, e + } + + encrypted |= EncryptAssertion + + payload, e = xmlutil.Direct(d.Root(), xmlutil.Name(AssertionNamespace, nameAssertion)) + if e != nil { + return nil, e + } + } else if o.RequireEncryption&EncryptAssertion != 0 { + return nil, ErrEncryption + } + + if payload.Name() != xmlutil.Name(AssertionNamespace, nameAssertion) { + return nil, ErrProfile + } + + assertion := payload + + assertionSigned := false + for _, child := range assertion.Children() { + assertionSigned = assertionSigned || child.Name() == xmlutil.Name(xmlutil.DS, lexicon.DSigElementSignature) + } + + expectedCount := 0 + if assertionSigned { + expectedCount++ + + if e = targetSignature(ctx, d, assertion, keys, o.Policy); e != nil { + return nil, e + } + } else if !o.AllowUnsignedAssertion || !responseSigned { + return nil, ErrSignature + } - expectedCount := 1 if responseSigned { - expectedCount = 2 + expectedCount++ } if signingCount(d, expectedCount) != nil { return nil, ErrProfile } - if e = targetSignature(ctx, d, assertion, keys, o.Policy); e != nil { + signedAssertion := assertion + + d, innerEncryption, e := decryptAssertionContents(ctx, d, decryption, o.RequireEncryption, assertionSigned || responseSigned, l) + if e != nil { + return nil, e + } + + encrypted |= innerEncryption + + assertion, e = xmlutil.Direct(d.Root(), xmlutil.Name(AssertionNamespace, nameAssertion)) + if e != nil { return nil, e } @@ -183,6 +279,8 @@ func VerifyResponse(ctx context.Context, d *document.Document, keys []crypto.Pub result.responseID = responseID result.responseSigned = responseSigned + result.assertion = signedAssertion + result.encryption = encrypted if e = ctx.Err(); e != nil { return nil, e @@ -191,6 +289,16 @@ func VerifyResponse(ctx context.Context, d *document.Document, keys []crypto.Pub return result, nil } +// Encryption reports which SAML positions were successfully decrypted and +// authenticated. It does not infer confidentiality of transport or other fields. +func (r *VerifiedAssertion) Encryption() EncryptionTargets { + if r == nil { + return 0 + } + + return r.encryption +} + // AssertionID returns the authenticated assertion identifier. func (r *VerifiedAssertion) AssertionID() string { if r == nil { @@ -263,6 +371,10 @@ func (r *VerifiedAssertion) NameIDFormat() string { return r.nameIDFormat } +// Identifier returns the complete authenticated NameID including its qualifiers +// and current SPProvidedID. Retain this value for logout and name management. +func (r *VerifiedAssertion) Identifier() NameIdentifier { return r.identifier } + // SessionIndex returns the authenticated session identifier. func (r *VerifiedAssertion) SessionIndex() string { if r == nil { @@ -330,34 +442,55 @@ func (r *VerifiedAssertion) Attributes() []Attribute { // ResponseAuthenticated returns whether the response envelope itself was authenticated. func (r *VerifiedAssertion) ResponseAuthenticated() bool { return r != nil && r.responseSigned } -func validateResponseOptions(ctx context.Context, d *document.Document, o ResponseOptions) error { - if ctx == nil || d == nil || o.At.IsZero() || !profile.URI(o.Issuer) || !profile.URI(o.Audience) || !profile.Endpoint(o.Destination, true) || !profile.ID(o.ExpectedRequestID) || len(o.AllowedNameIDFormats) > 16 || len(o.ExpectedAuthnContexts) > 16 || o.ForceAuthn && o.RequestIssueInstant.IsZero() { - return ErrProfile - } - - return nil +// StatusError reports an authenticated non-success response. It does not indicate +// login success. Remote StatusMessage and StatusDetail are not exposed as trusted +// text. +type StatusError struct { + // Subcodes are authenticated nested status codes, outermost first. + Subcodes []string + // Message is bounded authenticated remote diagnostic text, not a trust decision. + Message string + // Code is the authenticated non-success SAML status code URI. + Code string } -func parseResponseStatus(status document.Element) (string, error) { - if xmlutil.Attrs(status, nil, nil) != nil { - return "", ErrProfile - } +// Error returns the diagnostic error message. +func (e *StatusError) Error() string { return "SAML response status: " + e.Code } - sc, e := xmlutil.Children(status) - if e != nil || len(sc) != 1 || sc[0].Name() != xmlutil.Name(ProtocolNamespace, "StatusCode") || xmlutil.Attrs(sc[0], []string{"Value"}, nil) != nil || !xmlutil.EmptyContent(sc[0]) { - return "", ErrProfile +// ValidatePolicy checks a reusable policy before a stateful SP/IdP accepts it. +func ValidatePolicy(l Limits) error { _, e := normalize(l); return e } + +// AcceptanceWindow returns normalized, immutable timing limits to wrappers. +func AcceptanceWindow(l Limits) (messageAge, assertionLifetime, skew time.Duration, err error) { + n, e := normalize(l) + if e != nil { + return 0, 0, 0, e } - code := xmlutil.Attr(sc[0], "Value") - if !profile.URI(code) { - return "", ErrProfile + return n.MaxMessageAge, n.MaxAssertionLifetime, n.ClockSkew, nil +} + +func validateResponseOptions(ctx context.Context, d *document.Document, o ResponseOptions) error { + const ( + maximumAllowedNameIDFormats = 16 + maximumExpectedAuthnContexts = 16 + ) + + if ctx == nil || d == nil || o.At.IsZero() || !profile.URI(o.Issuer) || !profile.URI(o.Audience) || !profile.Endpoint(o.Destination, true) || !profile.ID(o.ExpectedRequestID) || len(o.AllowedNameIDFormats) > maximumAllowedNameIDFormats || len(o.ExpectedAuthnContexts) > maximumExpectedAuthnContexts || o.ForceAuthn && o.RequestIssueInstant.IsZero() { + return ErrProfile } - return code, nil + return nil } func parseAssertion(a document.Element, o ResponseOptions, l Limits) (*VerifiedAssertion, error) { - if xmlutil.Attrs(a, []string{"ID", nameVersion, nameIssueInstant}, nil) != nil || xmlutil.Attr(a, nameVersion) != version20 || !profile.ID(xmlutil.Attr(a, "ID")) { + const ( + maximumWebAssertionChildren = 7 + minimumWebAssertionChildren = 4 + requiredWebAssertionPayloadChildren = 3 + ) + + if xmlutil.Attrs(a, []string{lexicon.SAMLAttrID, nameVersion, nameIssueInstant}, nil) != nil || xmlutil.Attr(a, nameVersion) != version20 || !profile.ID(xmlutil.Attr(a, lexicon.SAMLAttrID)) { return nil, ErrProfile } @@ -367,31 +500,62 @@ func parseAssertion(a document.Element, o ResponseOptions, l Limits) (*VerifiedA } cs, e := xmlutil.Children(a) - if e != nil || len(cs) < 5 || len(cs) > 6 || issuer(cs[0], o.Issuer) != nil || cs[1].Name() != xmlutil.Name(xmlutil.DS, lexicon.DSigElementSignature) || cs[2].Name() != xmlutil.Name(AssertionNamespace, "Subject") || cs[3].Name() != xmlutil.Name(AssertionNamespace, "Conditions") || cs[4].Name() != xmlutil.Name(AssertionNamespace, "AuthnStatement") { + if e != nil || len(cs) < minimumWebAssertionChildren || len(cs) > maximumWebAssertionChildren || issuer(cs[0], o.Issuer) != nil { return nil, ErrProfile } - r := &VerifiedAssertion{assertion: a, assertionID: xmlutil.Attr(a, "ID"), issuer: o.Issuer, audience: o.Audience, recipient: o.Destination, requestID: o.ExpectedRequestID, instant: instant} - if e = parseSubject(cs[2], o, l, r); e != nil { + pos := 1 + if cs[pos].Name() == xmlutil.Name(xmlutil.DS, lexicon.DSigElementSignature) { + pos++ + } else if !o.AllowUnsignedAssertion { + return nil, ErrSignature + } + + if len(cs) < pos+requiredWebAssertionPayloadChildren || cs[pos].Name() != xmlutil.Name(AssertionNamespace, xmlNameSubject) || cs[pos+1].Name() != xmlutil.Name(AssertionNamespace, xmlNameConditions) { + return nil, ErrProfile + } + + r := &VerifiedAssertion{assertion: a, assertionID: xmlutil.Attr(a, lexicon.SAMLAttrID), issuer: o.Issuer, audience: o.Audience, recipient: o.Destination, requestID: o.ExpectedRequestID, instant: instant} + if e = parseSubject(cs[pos], o, l, r); e != nil { return nil, e } - expiry, e := parseConditions(cs[3], o, l) + expiry, e := parseConditions(cs[pos+1], o, l) if e != nil { return nil, e } r.expires = minTime(expiry, r.expires) - if e = parseAuthn(cs[4], o, l, r); e != nil { + + pos += 2 + if cs[pos].Name() == xmlutil.Name(AssertionNamespace, xmlNameAdvice) { + r.channels, e = readChannelContainer(cs[pos], true) + if e != nil { + return nil, e + } + + pos++ + } + + if e := requireChannelAdvice(r.channels, o.ExpectedChannelBindings); e != nil { + return nil, e + } + + if pos >= len(cs) || cs[pos].Name() != xmlutil.Name(AssertionNamespace, xmlNameAuthnStatement) { + return nil, ErrProfile + } + + if e = parseAuthn(cs[pos], o, l, r); e != nil { return nil, e } - if len(cs) == 6 { - if cs[5].Name() != xmlutil.Name(AssertionNamespace, "AttributeStatement") { + pos++ + if pos < len(cs) { + if pos+1 != len(cs) || cs[pos].Name() != xmlutil.Name(AssertionNamespace, xmlNameAttributeStatement) { return nil, ErrProfile } - r.attributes, e = parseAttributes(cs[5], l) + r.attributes, e = parseAttributes(cs[pos], l) if e != nil { return nil, e } @@ -406,7 +570,7 @@ func parseSubject(s document.Element, o ResponseOptions, l Limits, r *VerifiedAs } cs, e := xmlutil.Children(s) - if e != nil || len(cs) != 2 || cs[0].Name() != xmlutil.Name(AssertionNamespace, "NameID") || cs[1].Name() != xmlutil.Name(AssertionNamespace, "SubjectConfirmation") { + if e != nil || len(cs) < minimumSubjectChildren || len(cs) > maximumSubjectChildren || cs[0].Name() != xmlutil.Name(AssertionNamespace, xmlNameNameID) { return ErrProfile } @@ -414,11 +578,37 @@ func parseSubject(s document.Element, o ResponseOptions, l Limits, r *VerifiedAs return e } - return parseBearerConfirmation(cs[1], o, l, r) + for _, conf := range cs[1:] { + if conf.Name() != xmlutil.Name(AssertionNamespace, xmlNameSubjectConfirmation) || xmlutil.Attrs(conf, []string{nameMethod}, nil) != nil { + return ErrProfile + } + } + + wantHoK := o.RequireHolderOfKey || o.Proof != nil || len(o.producedCertificate) != 0 + lastErr := ErrConditions + + for _, conf := range cs[1:] { + candidate := *r + if wantHoK && xmlutil.Attr(conf, nameMethod) == HolderOfKey { + lastErr = parseHoKConfirmation(conf, o, l, &candidate) + } else if !wantHoK && xmlutil.Attr(conf, nameMethod) == Bearer { + candidate.confirmation = Bearer + lastErr = parseBearerConfirmation(conf, o, l, &candidate) + } else { + continue + } + + if lastErr == nil { + *r = candidate + return nil + } + } + + return lastErr } func parseSubjectNameID(id document.Element, o ResponseOptions, l Limits, r *VerifiedAssertion) error { - if xmlutil.Attrs(id, nil, []string{"Format", "NameQualifier", nameSPNameQualifier}) != nil || len(id.Children()) != 0 { + if xmlutil.Attrs(id, nil, []string{nameFormat, nameNameQualifier, nameSPNameQualifier, nameSPProvidedID}) != nil || len(id.Children()) != 0 { return ErrProfile } @@ -429,16 +619,16 @@ func parseSubjectNameID(id document.Element, o ResponseOptions, l Limits, r *Ver r.nameID = value - r.nameIDFormat = xmlutil.Attr(id, "Format") + r.nameIDFormat = xmlutil.Attr(id, nameFormat) if r.nameIDFormat == "" { r.nameIDFormat = NameIDUnspecified } - if !profile.URI(r.nameIDFormat) || len(o.AllowedNameIDFormats) > 0 && !allowed(r.nameIDFormat, o.AllowedNameIDFormats) { + if !profile.URI(r.nameIDFormat) || r.nameIDFormat == NameIDEncrypted || len(o.AllowedNameIDFormats) > 0 && !allowed(r.nameIDFormat, o.AllowedNameIDFormats) { return ErrProfile } - if q, ok := id.Attribute(document.Name{Local: "NameQualifier"}); ok && q != o.Issuer { + if q, ok := id.Attribute(document.Name{Local: nameNameQualifier}); ok && q != o.Issuer { return ErrProfile } @@ -446,37 +636,47 @@ func parseSubjectNameID(id document.Element, o ResponseOptions, l Limits, r *Ver return ErrProfile } + alias := xmlutil.Attr(id, nameSPProvidedID) + if len(alias) > l.MaxValueBytes { + return ErrProfile + } + + r.identifier = NameIdentifier{Value: r.nameID, Format: r.nameIDFormat, NameQualifier: xmlutil.Attr(id, nameNameQualifier), SPNameQualifier: xmlutil.Attr(id, nameSPNameQualifier), SPProvidedID: alias} + if err := r.identifier.Validate(); err != nil { + return err + } + return nil } func parseBearerConfirmation(c document.Element, o ResponseOptions, l Limits, r *VerifiedAssertion) error { - if xmlutil.Attrs(c, []string{"Method"}, nil) != nil || xmlutil.Attr(c, "Method") != Bearer { + if xmlutil.Attrs(c, []string{nameMethod}, nil) != nil || xmlutil.Attr(c, nameMethod) != Bearer { return ErrProfile } ds, e := xmlutil.Children(c) - if e != nil || len(ds) != 1 || ds[0].Name() != xmlutil.Name(AssertionNamespace, "SubjectConfirmationData") { + if e != nil || len(ds) != 1 || ds[0].Name() != xmlutil.Name(AssertionNamespace, xmlNameSubjectConfirmationData) { return ErrProfile } data := ds[0] // Web Browser SSO bearer confirmation MUST NOT contain NotBefore. - if xmlutil.Attrs(data, []string{"Recipient", nameInResponseTo, "NotOnOrAfter"}, []string{"Address"}) != nil || !xmlutil.EmptyContent(data) { + if xmlutil.Attrs(data, []string{nameRecipient, nameInResponseTo, nameNotOnOrAfter}, []string{nameAddress}) != nil || !xmlutil.EmptyContent(data) { return ErrProfile } - if xmlutil.Attr(data, "Recipient") != o.Destination || xmlutil.Attr(data, nameInResponseTo) != o.ExpectedRequestID { + if xmlutil.Attr(data, nameRecipient) != o.Destination || xmlutil.Attr(data, nameInResponseTo) != o.ExpectedRequestID { return ErrCorrelation } - end, e := profile.ParseTime(xmlutil.Attr(data, "NotOnOrAfter")) + end, e := profile.ParseTime(xmlutil.Attr(data, nameNotOnOrAfter)) if e != nil || !o.At.Add(-l.ClockSkew).Before(end) || end.After(o.At.Add(l.MaxAssertionLifetime+l.ClockSkew)) { return ErrConditions } r.expires = end - if addr, ok := data.Attribute(document.Name{Local: "Address"}); ok { + if addr, ok := data.Attribute(document.Name{Local: nameAddress}); ok { actual := net.ParseIP(addr) expected := net.ParseIP(o.ExpectedAddress) @@ -489,22 +689,26 @@ func parseBearerConfirmation(c document.Element, o ResponseOptions, l Limits, r } func parseConditions(c document.Element, o ResponseOptions, l Limits) (time.Time, error) { - if xmlutil.Attrs(c, []string{"NotBefore", "NotOnOrAfter"}, nil) != nil { + const ( + maximumWebConditionsChildren = 17 + ) + + if xmlutil.Attrs(c, []string{nameNotBefore, nameNotOnOrAfter}, nil) != nil { return time.Time{}, ErrProfile } - start, e := profile.ParseTime(xmlutil.Attr(c, "NotBefore")) + start, e := profile.ParseTime(xmlutil.Attr(c, nameNotBefore)) if e != nil { return time.Time{}, ErrConditions } - end, e := profile.ParseTime(xmlutil.Attr(c, "NotOnOrAfter")) + end, e := profile.ParseTime(xmlutil.Attr(c, nameNotOnOrAfter)) if e != nil || timeWindow(start, end, o.At, l) != nil { return time.Time{}, ErrConditions } cs, e := xmlutil.Children(c) - if e != nil || len(cs) < 1 || len(cs) > 17 { + if e != nil || len(cs) < 1 || len(cs) > maximumWebConditionsChildren { return time.Time{}, ErrProfile } @@ -517,13 +721,13 @@ func parseConditions(c document.Element, o ResponseOptions, l Limits) (time.Time } switch child.Name() { - case xmlutil.Name(AssertionNamespace, "AudienceRestriction"): + case xmlutil.Name(AssertionNamespace, xmlNameAudienceRestriction): if e := validateAudienceRestriction(child, o.Audience); e != nil { return time.Time{}, e } groups++ - case xmlutil.Name(AssertionNamespace, "OneTimeUse"): + case xmlutil.Name(AssertionNamespace, xmlNameOneTimeUse): if once || !xmlutil.EmptyContent(child) { return time.Time{}, ErrProfile } @@ -542,19 +746,23 @@ func parseConditions(c document.Element, o ResponseOptions, l Limits) (time.Time } func validateAudienceRestriction(child document.Element, expected string) error { + const ( + maximumAudienceAlternatives = 16 + ) + audiences, e := xmlutil.Children(child) - if e != nil || len(audiences) < 1 || len(audiences) > 16 { + if e != nil || len(audiences) < 1 || len(audiences) > maximumAudienceAlternatives { return ErrProfile } match := false for _, a := range audiences { - if a.Name() != xmlutil.Name(AssertionNamespace, "Audience") { + if a.Name() != xmlutil.Name(AssertionNamespace, xmlNameAudience) { return ErrProfile } - s, e := xmlutil.Simple(a, 2048) + s, e := xmlutil.Simple(a, maximumURITextBytes) if e != nil || !profile.URI(s) { return ErrProfile } @@ -572,11 +780,16 @@ func validateAudienceRestriction(child document.Element, expected string) error } func parseAuthn(a document.Element, o ResponseOptions, l Limits, r *VerifiedAssertion) error { - if xmlutil.Attrs(a, []string{"AuthnInstant"}, []string{"SessionIndex", "SessionNotOnOrAfter"}) != nil { + const ( + maximumAuthnSessionIndexBytes = 1024 + maximumWebAuthnContextChildren = 9 + ) + + if xmlutil.Attrs(a, []string{xmlNameAuthnInstant}, []string{xmlNameSessionIndex, nameSessionNotOnOrAfter}) != nil { return ErrProfile } - t, e := profile.ParseTime(xmlutil.Attr(a, "AuthnInstant")) + t, e := profile.ParseTime(xmlutil.Attr(a, xmlNameAuthnInstant)) if e != nil || t.After(o.At.Add(l.ClockSkew)) || t.After(r.instant.Add(l.ClockSkew)) { return ErrConditions } @@ -587,12 +800,12 @@ func parseAuthn(a document.Element, o ResponseOptions, l Limits, r *VerifiedAsse r.authnInstant = t - r.sessionIndex = xmlutil.Attr(a, "SessionIndex") - if len(r.sessionIndex) > 1024 { + r.sessionIndex = xmlutil.Attr(a, xmlNameSessionIndex) + if len(r.sessionIndex) > maximumAuthnSessionIndexBytes { return ErrProfile } - if s, ok := a.Attribute(document.Name{Local: "SessionNotOnOrAfter"}); ok { + if s, ok := a.Attribute(document.Name{Local: nameSessionNotOnOrAfter}); ok { end, e := profile.ParseTime(s) if e != nil || !t.Before(end) || !o.At.Add(-l.ClockSkew).Before(end) { return ErrConditions @@ -603,16 +816,16 @@ func parseAuthn(a document.Element, o ResponseOptions, l Limits, r *VerifiedAsse } cs, e := xmlutil.Children(a) - if e != nil || len(cs) != 1 || cs[0].Name() != xmlutil.Name(AssertionNamespace, "AuthnContext") || xmlutil.Attrs(cs[0], nil, nil) != nil { + if e != nil || len(cs) != 1 || cs[0].Name() != xmlutil.Name(AssertionNamespace, xmlNameAuthnContext) || xmlutil.Attrs(cs[0], nil, nil) != nil { return ErrProfile } ac, e := xmlutil.Children(cs[0]) - if e != nil || len(ac) < 1 || len(ac) > 9 || ac[0].Name() != xmlutil.Name(AssertionNamespace, "AuthnContextClassRef") { + if e != nil || len(ac) < 1 || len(ac) > maximumWebAuthnContextChildren || ac[0].Name() != xmlutil.Name(AssertionNamespace, xmlNameAuthnContextClassRef) { return ErrProfile } - class, e := xmlutil.Simple(ac[0], 2048) + class, e := xmlutil.Simple(ac[0], maximumURITextBytes) if e != nil || !profile.URI(class) || len(o.ExpectedAuthnContexts) > 0 && !allowed(class, o.ExpectedAuthnContexts) { return ErrConditions } @@ -620,11 +833,11 @@ func parseAuthn(a document.Element, o ResponseOptions, l Limits, r *VerifiedAsse r.contextClass = class for _, x := range ac[1:] { - if x.Name() != xmlutil.Name(AssertionNamespace, "AuthenticatingAuthority") { + if x.Name() != xmlutil.Name(AssertionNamespace, xmlNameAuthenticatingAuthority) { return ErrProfile } - s, e := xmlutil.Simple(x, 2048) + s, e := xmlutil.Simple(x, maximumURITextBytes) if e != nil || !profile.URI(s) { return ErrProfile } @@ -659,19 +872,19 @@ func parseAttributes(a document.Element, l Limits) ([]Attribute, error) { } func parseAttribute(c document.Element, l Limits, seen map[[2]string]struct{}) (Attribute, error) { - if c.Name() != xmlutil.Name(AssertionNamespace, "Attribute") || xmlutil.Attrs(c, []string{"Name"}, []string{"NameFormat", "FriendlyName"}) != nil { + if c.Name() != xmlutil.Name(AssertionNamespace, xmlNameAttribute) || xmlutil.Attrs(c, []string{nameAttributeName}, []string{xmlNameNameFormat, xmlNameFriendlyName}) != nil { return Attribute{}, ErrProfile } - name := xmlutil.Attr(c, "Name") + name := xmlutil.Attr(c, nameAttributeName) - format := xmlutil.Attr(c, "NameFormat") + format := xmlutil.Attr(c, xmlNameNameFormat) if format == "" { format = AttributeUnspecified } - friendly := xmlutil.Attr(c, "FriendlyName") - if _, exists := seen[[2]string{name, format}]; len(name) == 0 || len(name) > 1024 || !profile.URI(format) || len(friendly) > 1024 || exists { + friendly := xmlutil.Attr(c, xmlNameFriendlyName) + if _, exists := seen[[2]string{name, format}]; len(name) == 0 || len(name) > maximumAttributeNameBytes || !profile.URI(format) || len(friendly) > maximumFriendlyNameBytes || exists { return Attribute{}, ErrProfile } @@ -697,12 +910,16 @@ func parseAttribute(c document.Element, l Limits, seen map[[2]string]struct{}) ( } func parseAttributeValue(x document.Element, l Limits) (string, error) { - if x.Name() != xmlutil.Name(AssertionNamespace, "AttributeValue") || len(x.Children()) != 0 { + const ( + maximumAttributeTypeNameBytes = 256 + ) + + if x.Name() != xmlutil.Name(AssertionNamespace, xmlNameAttributeValue) || len(x.Children()) != 0 { return "", ErrProfile } for _, attr := range x.Attributes() { - if attr.Name != (document.Name{Space: lexicon.NamespaceXSI, Local: "type"}) || len(attr.Value) > 256 { + if attr.Name != (document.Name{Space: lexicon.NamespaceXSI, Local: lexicon.AttrType}) || len(attr.Value) > maximumAttributeTypeNameBytes { return "", ErrProfile } } @@ -724,27 +941,3 @@ func copyAttributes(a []Attribute) []Attribute { return out } - -// StatusError reports an authenticated non-success response. It does not indicate -// login success. Remote StatusMessage and StatusDetail are not exposed as trusted -// text. -type StatusError struct { - // Code is the authenticated non-success SAML status code URI. - Code string -} - -// Error returns the diagnostic error message. -func (e *StatusError) Error() string { return "SAML response status: " + e.Code } - -// ValidatePolicy checks a reusable policy before a stateful SP/IdP accepts it. -func ValidatePolicy(l Limits) error { _, e := normalize(l); return e } - -// AcceptanceWindow returns normalized, immutable timing limits to wrappers. -func AcceptanceWindow(l Limits) (messageAge, assertionLifetime, skew time.Duration, err error) { - n, e := normalize(l) - if e != nil { - return 0, 0, 0, e - } - - return n.MaxMessageAge, n.MaxAssertionLifetime, n.ClockSkew, nil -} diff --git a/saml/protocol/shared_constants.go b/saml/protocol/shared_constants.go index 32c1e1e..2809ab9 100644 --- a/saml/protocol/shared_constants.go +++ b/saml/protocol/shared_constants.go @@ -1,16 +1,115 @@ package protocol +import "github.com/skarm/xmlsec/internal/lexicon" + +// Local SAML vocabulary tokens shared by protocol producers and parsers. Each call site still verifies the assertion/protocol namespace or resolves the QName prefix. +const ( + xmlNameAction = "Action" + xmlNameAdvice = "Advice" + xmlNameAllowCreate = "AllowCreate" + xmlNameAttribute = "Attribute" + xmlNameAttributeStatement = "AttributeStatement" + xmlNameAttributeValue = "AttributeValue" + xmlNameAudience = "Audience" + xmlNameAudienceRestriction = "AudienceRestriction" + xmlNameAuthenticatingAuthority = "AuthenticatingAuthority" + xmlNameAuthnContext = "AuthnContext" + xmlNameAuthnContextClassRef = "AuthnContextClassRef" + xmlNameAuthnContextDeclRef = "AuthnContextDeclRef" + xmlNameAuthnInstant = "AuthnInstant" + xmlNameAuthnStatement = "AuthnStatement" + xmlNameAuthzDecisionStatement = "AuthzDecisionStatement" + xmlNameComparison = "Comparison" + xmlNameConditions = "Conditions" + xmlNameEncryptedAssertion = "EncryptedAssertion" + xmlNameEncryptedAttribute = "EncryptedAttribute" + xmlNameEncryptedID = "EncryptedID" + xmlNameEvidence = "Evidence" + xmlNameExtensions = "Extensions" + xmlNameFriendlyName = "FriendlyName" + xmlNameIssuer = "Issuer" + xmlNameNameFormat = "NameFormat" + xmlNameNameID = "NameID" + xmlNameNameIDPolicy = "NameIDPolicy" + xmlNameNewEncryptedID = "NewEncryptedID" + xmlNameNewID = "NewID" + xmlNameOneTimeUse = "OneTimeUse" + xmlNameRequestedAuthnContext = "RequestedAuthnContext" + xmlNameResource = "Resource" + xmlNameSessionIndex = "SessionIndex" + xmlNameStatus = "Status" + xmlNameStatusCode = "StatusCode" + xmlNameSubject = "Subject" + xmlNameSubjectConfirmation = "SubjectConfirmation" + xmlNameSubjectConfirmationData = "SubjectConfirmationData" + xmlNameValue = "Value" +) + +// Fixed profile ceilings shared by producers and parsers. Text limits count +// UTF-8 bytes; key and child limits count alternatives or direct XML children. +// None of these values is a configurable zero-value default. +const ( + maximumAttributeNameBytes = 1024 + maximumFriendlyNameBytes = 1024 + maximumNameQualifierBytes = 2048 + maximumRedirectVerificationKeys = 16 + maximumSubjectChildren = 9 + maximumURITextBytes = 2048 + minimumSubjectChildren = 2 +) + +// XML child offsets after the mandatory issuer; a direct XML signature adds one child. These widths describe syntax, not authority to bypass authentication. +const ( + signedMessagePrefixChildren = 2 + unsignedMessagePrefixChildren = 1 +) + +// SAML 2.0 header and confirmation attributes are unqualified XML names. +// The version token is a wire value, independent of the namespace declarations. const ( version20 = "2.0" nameDestination = "Destination" + nameMethod = "Method" + nameRecipient = "Recipient" + nameAddress = "Address" + nameNotBefore = "NotBefore" nameSPNameQualifier = "SPNameQualifier" ) +// Correlation and issue-time attributes are shared across the request families. const ( nameInResponseTo = "InResponseTo" nameIssueInstant = "IssueInstant" ) +// Version is the unqualified attribute carrying version20 on protocol messages. const ( nameVersion = "Version" ) + +// Identifier qualifiers and expiry names are shared by assertion and protocol +// producers/parsers. Prefixes are serialization choices, never namespace trust. +const ( + nameFormat = "Format" + nameNameQualifier = "NameQualifier" + nameNotOnOrAfter = "NotOnOrAfter" + nameSPProvidedID = "SPProvidedID" + prefixAssertion = "saml" + prefixProtocol = "samlp" + prefixSOAP = "env" + // LogoutResponse is the SAML logout response element local name. + LogoutResponse = lexicon.SAMLElementLogoutResponse + // ManageNameIDResponse is the SAML name management response element local name. + ManageNameIDResponse = lexicon.SAMLElementManageNameIDResponse + // ArtifactResponse is the SAML artifact response element local name. + ArtifactResponse = lexicon.SAMLElementArtifactResponse +) + +// These names and the exact-comparison selector are reused by web and query +// assertion paths; their surrounding assertion/protocol namespaces remain explicit. +const ( + nameAttributeName = "Name" + contextExact = "exact" + nameSessionNotOnOrAfter = "SessionNotOnOrAfter" + nameAssertion = "Assertion" +) diff --git a/saml/protocol/soap.go b/saml/protocol/soap.go new file mode 100644 index 0000000..4a089ea --- /dev/null +++ b/saml/protocol/soap.go @@ -0,0 +1,67 @@ +package protocol + +import ( + "context" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/lexicon" + "github.com/skarm/xmlsec/internal/tree" + "github.com/skarm/xmlsec/saml/binding" + "github.com/skarm/xmlsec/saml/internal/xmlsig" + "github.com/skarm/xmlsec/saml/internal/xmlutil" +) + +// SignSOAP replaces a message's direct signature in its final SOAP namespace +// context and returns the envelope. Use this for Inclusive C14N as well as +// Exclusive C14N. Embedded messages/assertions retain their own signatures and +// must already be valid in that context. Headers are copied, never authenticated +// by the SAML body signature. ECP validates its routing headers separately. +func SignSOAP(ctx context.Context, message *document.Document, key SigningKey, headers ...document.Element) (*document.Document, error) { + envelope, err := binding.WrapSOAP(ctx, message, headers...) + if err != nil { + return nil, err + } + + private, err := tree.Clone(ctx, envelope) + if err != nil { + return nil, err + } + + root := tree.Root(private) + body := root.Children[len(root.Children)-1] + target := body.Children[0] + + if len(key.InclusivePrefixes) > 0 || hasInclusiveNamespaces(message.Root()) { + key = namespaceSigningKey(message.Root(), key) + } + + children := make([]*tree.Node, 0, len(target.Children)) + for _, child := range target.Children { + if child.Kind == tree.ElementNode && child.Name == xmlutil.Name(xmlutil.DS, lexicon.DSigElementSignature) { + continue + } + + children = append(children, child) + } + + target.Children = children + + d, err := tree.Reparse(ctx, private, document.DefaultLimits()) + if err != nil { + return nil, err + } + + element := d.Root().Children()[len(d.Root().Children())-1].Children()[0] + + k, err := signing(key) + if err != nil { + return nil, err + } + + d, err = xmlsig.Sign(ctx, d, element, k, true) + if err != nil { + return nil, err + } + + return d, nil +} diff --git a/saml/replay/store.go b/saml/replay/store.go index 110546b..ecbf5ad 100644 --- a/saml/replay/store.go +++ b/saml/replay/store.go @@ -68,15 +68,23 @@ type key struct { // NewMemoryStore creates an empty bounded replay store using default limits for zero Config // fields. func NewMemoryStore(c Config) (*MemoryStore, error) { + const ( + defaultReplayLifetime = time.Hour + defaultStoreCapacity = 100000 + maximumReplayLifetime = 7 * 24 * time.Hour + maximumStoreCapacity = 10000000 + minimumStoreLifetime = time.Second + ) + if c.Capacity == 0 { - c.Capacity = 100000 + c.Capacity = defaultStoreCapacity } if c.MaxTTL == 0 { - c.MaxTTL = time.Hour + c.MaxTTL = defaultReplayLifetime } - if c.Capacity < 1 || c.Capacity > 10000000 || c.MaxTTL < time.Second || c.MaxTTL > 7*24*time.Hour { + if c.Capacity < 1 || c.Capacity > maximumStoreCapacity || c.MaxTTL < minimumStoreLifetime || c.MaxTTL > maximumReplayLifetime { return nil, ErrPolicy } @@ -86,7 +94,13 @@ func NewMemoryStore(c Config) (*MemoryStore, error) { // Consume atomically consumes all supplied scoped identifiers or none. It prunes expired // entries and never evicts unexpired identifiers. func (m *MemoryStore) Consume(ctx context.Context, entries []Entry, now time.Time) error { - if m == nil || ctx == nil || now.IsZero() || len(entries) < 1 || len(entries) > 16 { + const ( + inlineConsumeEntries = 16 + maximumConsumeEntries = 256 + maximumReplayFieldBytes = 1024 + ) + + if m == nil || ctx == nil || now.IsZero() || len(entries) < 1 || len(entries) > maximumConsumeEntries { return ErrPolicy } @@ -94,13 +108,20 @@ func (m *MemoryStore) Consume(ctx context.Context, entries []Entry, now time.Tim return err } - var snapshot [16]Entry + var snapshot [inlineConsumeEntries]Entry - copied := snapshot[:copy(snapshot[:], entries)] + var copied []Entry + if len(entries) > len(snapshot) { + copied = make([]Entry, len(entries)) + } else { + copied = snapshot[:len(entries)] + } + + copy(copied, entries) seen := make(map[key]struct{}, len(copied)) for _, e := range copied { - if e.Scope == "" || e.ID == "" || len(e.Scope) > 1024 || len(e.ID) > 1024 || !e.ExpiresAt.After(now) || e.ExpiresAt.After(now.Add(m.maxTTL)) { + if e.Scope == "" || e.ID == "" || len(e.Scope) > maximumReplayFieldBytes || len(e.ID) > maximumReplayFieldBytes || !e.ExpiresAt.After(now) || e.ExpiresAt.After(now.Add(m.maxTTL)) { return ErrPolicy } @@ -158,8 +179,14 @@ func (m *MemoryStore) Len() int { } func (m *MemoryStore) prune(ctx context.Context, at time.Time) error { + const ( + pruneMinimumCapacity = 1024 + pruneSweepBatch = 256 + retainedCapacityDivisor = 4 + ) + for removed := 0; ; removed++ { - if removed%256 == 0 { + if removed%pruneSweepBatch == 0 { if err := ctx.Err(); err != nil { return err } @@ -174,10 +201,10 @@ func (m *MemoryStore) prune(ctx context.Context, at time.Time) error { delete(m.entries, key) } - if m.peak > 1024 && len(m.entries) <= m.peak/4 { + if m.peak > pruneMinimumCapacity && len(m.entries) <= m.peak/retainedCapacityDivisor { entries := make(map[key]time.Time, len(m.entries)) for key, value := range m.entries { - if len(entries)%256 == 0 { + if len(entries)%pruneSweepBatch == 0 { if err := ctx.Err(); err != nil { return err } diff --git a/saml/replay/store_test.go b/saml/replay/store_test.go index 29adf80..281a635 100644 --- a/saml/replay/store_test.go +++ b/saml/replay/store_test.go @@ -16,6 +16,28 @@ const nameRequest = "request" const uriUrnSpUrnIdp = "urn:sp:urn:idp" +func TestLargeAtomicBatch(t *testing.T) { + now := time.Now() + m, err := replay.NewMemoryStore(replay.Config{}) + if err != nil { + t.Fatal(err) + } + entries := make([]replay.Entry, 65) + for i := range entries { + entries[i] = replay.Entry{Scope: "query", ID: strconv.Itoa(i), ExpiresAt: now.Add(time.Minute)} + } + if err = m.Consume(t.Context(), entries, now); err != nil || m.Len() != len(entries) { + t.Fatalf("large response batch: %v", err) + } + entries[0].ID = "unconsumed" + if err = m.Consume(t.Context(), entries, now); !errors.Is(err, replay.ErrReplay) || m.Len() != len(entries) { + t.Fatalf("batch partially committed: %v", err) + } + if err = m.Consume(t.Context(), entries[:1], now); err != nil { + t.Fatalf("failed batch consumed new ID: %v", err) + } +} + func TestAtomicConsume(t *testing.T) { m, e := replay.NewMemoryStore(replay.Config{}) if e != nil { @@ -131,7 +153,9 @@ func TestReplayMassExpiryPreservesLiveScopes(t *testing.T) { expired.ExpiresAt = later.Add(time.Minute) otherScope := entries[count-1] otherScope.Scope = "assertion" - if err := m.Consume(t.Context(), []replay.Entry{expired, otherScope}, later); err != nil || m.Len() != count/4+2 { + if err := m.Consume(t.Context(), []replay.Entry{expired, otherScope}, later); err != nil { + t.Fatal(err) + } else if m.Len() != count/4+2 { t.Fatal("expired capacity or independent scope unavailable", err, m.Len()) } if err := m.Consume(t.Context(), entries[count-1:], later); !errors.Is(err, replay.ErrReplay) { @@ -141,7 +165,9 @@ func TestReplayMassExpiryPreservesLiveScopes(t *testing.T) { t.Fatal("expired input accepted", err) } expired.ExpiresAt = expired.ExpiresAt.Add(time.Minute) - if err := m.Consume(t.Context(), []replay.Entry{expired}, later.Add(time.Minute)); err != nil || m.Len() != 1 { + if err := m.Consume(t.Context(), []replay.Entry{expired}, later.Add(time.Minute)); err != nil { + t.Fatal(err) + } else if m.Len() != 1 { t.Fatal("store did not recover after all entries expired", err, m.Len()) } } diff --git a/saml/session/cancellation_test.go b/saml/session/cancellation_test.go new file mode 100644 index 0000000..0caf8b2 --- /dev/null +++ b/saml/session/cancellation_test.go @@ -0,0 +1,141 @@ +package session //nolint:testpackage // Holds the transaction mutex to reproduce cancellation while queued. + +import ( + "context" + "crypto" + "errors" + "sync" + "testing" + "time" + + "github.com/skarm/xmlsec/internal/testutil/testcert" + "github.com/skarm/xmlsec/saml/metadata" + "github.com/skarm/xmlsec/saml/protocol" +) + +type observedContext struct { + context.Context + checked chan struct{} + once sync.Once +} + +func (c *observedContext) Err() error { + err := c.Context.Err() + c.once.Do(func() { close(c.checked) }) + return err +} + +func TestCanceledSessionTransactionDoesNotCommit(t *testing.T) { + for _, operation := range []string{"Register", "Add", "Active", "Lookup", "Logout", "Manage", "Start", "Finish", "Withdraw"} { + t.Run(operation, func(t *testing.T) { + const local, peer, endpoint = "https://idp.test/entity", "https://sp.test/entity", "https://idp.test/control" + at := testcert.Now() + cert := testcert.New(t) + key := protocol.SigningKey{Key: cert.LeafKey, CertificateDER: cert.Leaf.Raw} + keys := []crypto.PublicKey{key.Key.Public()} + options := protocol.ExchangeOptions{Issuer: peer, Destination: endpoint, At: at, ExpectedRequestID: "_pending"} + store, err := NewMemoryStore(Config{EntityID: local, Role: metadata.IdentityProvider}) + if err != nil { + t.Fatal(err) + } + name := protocol.NameIdentifier{Value: "alice"} + relationship := Relationship{Principal: "alice", Peer: peer, NameID: name, ExpiresAt: at.Add(time.Hour)} + if err := store.Register(t.Context(), relationship, at); err != nil { + t.Fatal(err) + } + session := Session{ID: "active", Principal: "alice", Peer: peer, NameID: name, AuthoritySession: "group", Index: "index", ExpiresAt: at.Add(time.Minute)} + if err := store.Add(t.Context(), session, at); err != nil { + t.Fatal(err) + } + pending := Pending{ID: "_pending", Peer: peer, Kind: protocol.ManageNameIDResponse, NameID: name, NewID: "replacement", LocalIsIDP: true, ExpiresAt: at.Add(time.Minute)} + if err := store.Start(t.Context(), pending, at); err != nil { + t.Fatal(err) + } + header := protocol.MessageHeader{Issuer: peer, Destination: endpoint, IssueInstant: at} + logoutXML, err := protocol.SignLogoutRequest(t.Context(), protocol.LogoutRequest{MessageHeader: header, NameID: name, SessionIndexes: []string{"index"}, NotOnOrAfter: at.Add(time.Minute)}, key) + if err != nil { + t.Fatal(err) + } + logout, err := protocol.VerifyLogoutRequest(t.Context(), logoutXML, keys, options) + if err != nil { + t.Fatal(err) + } + manageXML, err := protocol.SignManageNameIDRequest(t.Context(), protocol.ManageNameIDRequest{MessageHeader: header, NameID: name, NewID: "new-alias"}, key) + if err != nil { + t.Fatal(err) + } + manage, err := protocol.VerifyManageNameIDRequest(t.Context(), manageXML, keys, options) + if err != nil { + t.Fatal(err) + } + responseXML, err := protocol.SignStatusResponse(t.Context(), protocol.StatusResponse{MessageHeader: header, Kind: protocol.ManageNameIDResponse, InResponseTo: pending.ID, Status: protocol.Status{Code: protocol.Success}}, key) + if err != nil { + t.Fatal(err) + } + response, err := protocol.VerifyStatusResponse(t.Context(), responseXML, keys, options) + if err != nil { + t.Fatal(err) + } + call := func(ctx context.Context) error { + switch operation { + case "Register": + r := relationship + r.Principal, r.NameID.Value = "bob", "bob" + return store.Register(ctx, r, at) + case "Add": + s := session + s.ID = "new-session" + return store.Add(ctx, s, at) + case "Active": + if store.Active(ctx, session.ID, at) { + return errors.New("canceled read returned an active session") + } + return ctx.Err() + case "Lookup": + if _, ok := store.Lookup(ctx, "alice", peer, at); ok { + return errors.New("canceled read returned a relationship") + } + return ctx.Err() + case "Logout": + _, err := store.Logout(ctx, logout, true, at, at.Add(time.Hour), 0) + return err + case "Manage": + _, err := store.Manage(ctx, manage, false, at, at.Add(time.Hour)) + return err + case "Start": + p := pending + p.ID = "_newPending" + return store.Start(ctx, p, at) + case "Finish": + _, err := store.Finish(ctx, response, at) + return err + case "Withdraw": + _, err := store.Withdraw(ctx, "alice", "group", at) + return err + } + return nil + } + ctx, cancel := context.WithCancel(t.Context()) + defer cancel() + observed := &observedContext{Context: ctx, checked: make(chan struct{})} + finished := make(chan error, 1) + store.mu.Lock() + go func() { finished <- call(observed) }() + <-observed.checked + cancel() + store.mu.Unlock() + if err := <-finished; !errors.Is(err, context.Canceled) { + t.Error("canceled transaction was not rejected", err) + } + if !store.Active(t.Context(), session.ID, at) || store.Active(t.Context(), "new-session", at) { + t.Error("canceled transaction changed sessions") + } + if r, ok := store.Lookup(t.Context(), "alice", peer, at); !ok || r.NameID != name { + t.Error("canceled transaction changed the identifier") + } + if _, ok := store.Lookup(t.Context(), "bob", peer, at); ok { + t.Error("canceled transaction inserted a relationship") + } + }) + } +} diff --git a/saml/session/exchange_test.go b/saml/session/exchange_test.go new file mode 100644 index 0000000..89f2635 --- /dev/null +++ b/saml/session/exchange_test.go @@ -0,0 +1,214 @@ +package session_test + +import ( + "crypto" + "errors" + "io" + "testing" + "time" + + "github.com/skarm/xmlsec/internal/testutil/testcert" + "github.com/skarm/xmlsec/saml/binding" + "github.com/skarm/xmlsec/saml/metadata" + "github.com/skarm/xmlsec/saml/protocol" + "github.com/skarm/xmlsec/saml/session" + "github.com/skarm/xmlsec/xmldsig" +) + +// Each provider initiates a change, the other applies it, and only a correlated +// authenticated response commits the initiator's matching local change. +func TestManageNameIDBothDirectionsAndBindings(t *testing.T) { + c := testcert.New(t) + at := testcert.Now() + spKey := protocol.SigningKey{Key: c.LeafKey, CertificateDER: c.Leaf.Raw} + idpKey := protocol.SigningKey{Key: c.TSAKey, CertificateDER: c.TSA.Raw} + spStore, _ := session.NewMemoryStore(session.Config{EntityID: spID, Role: metadata.ServiceProvider, Capacity: 100}) + idpStore, _ := session.NewMemoryStore(session.Config{EntityID: idpID, Role: metadata.IdentityProvider, Capacity: 100}) + register(t, spStore, idpID, "local-account", "group", "index", "sp-session") + register(t, idpStore, spID, "local-account", "group", "index", "idp-session") + spService := session.Service{EntityID: spID, Endpoint: "https://sp.test/manage", Role: metadata.ServiceProvider, Store: spStore, Signing: spKey} + idpService := session.Service{EntityID: idpID, Endpoint: mgmt, Role: metadata.IdentityProvider, Store: idpStore, Signing: idpKey} + for i, step := range []struct { + sender, receiver session.Service + store, remote *session.MemoryStore + senderID, receiverID, value string + redirect bool + }{ + {spService, idpService, spStore, idpStore, spID, idpID, "new-sp-alias", true}, + {idpService, spService, idpStore, spStore, idpID, spID, "new-primary", false}, + } { + old, ok := step.store.Lookup(t.Context(), "local-account", step.receiverID, at) + if !ok { + t.Fatal("missing original binding") + } + id := "_sp_change" + if i == 1 { + id = "_idp_change" + } + d, err := step.sender.BeginManage(t.Context(), step.receiverID, protocol.ManageNameIDRequest{ID: id, Issuer: step.senderID, Destination: step.receiver.Endpoint, IssueInstant: at, NameID: old.NameID, NewID: step.value}, at) + if err != nil { + t.Fatal(err) + } + o := protocol.ExchangeOptions{Issuer: step.senderID, Destination: step.receiver.Endpoint, At: at} + var request *protocol.VerifiedManageNameIDRequest + if step.redirect { + query, err := binding.SignRedirect(t.Context(), d, binding.Request, nil, step.sender.Signing.Key, binding.Options{SignatureMethod: xmldsig.ECDSASHA256}) + if err != nil { + t.Fatal(err) + } + request, _, err = protocol.VerifyRedirectManageNameIDRequest(t.Context(), query, []crypto.PublicKey{step.sender.Signing.Key.Public()}, o, binding.Options{}) + if err != nil { + t.Fatal(err) + } + } else { + request, err = protocol.VerifyManageNameIDRequest(t.Context(), d, []crypto.PublicKey{step.sender.Signing.Key.Public()}, o) + if err != nil { + t.Fatal(err) + } + } + response, err := step.receiver.Manage(t.Context(), request, step.sender.Endpoint, at) + if err != nil { + t.Fatal(err) + } + o = protocol.ExchangeOptions{Issuer: step.receiverID, Destination: step.sender.Endpoint, ExpectedRequestID: id, At: at} + var verified *protocol.VerifiedStatusResponse + if step.redirect { + query, err := binding.SignRedirect(t.Context(), response, binding.Response, nil, step.receiver.Signing.Key, binding.Options{SignatureMethod: xmldsig.ECDSASHA256}) + if err != nil { + t.Fatal(err) + } + verified, _, err = protocol.VerifyRedirectStatusResponse(t.Context(), query, []crypto.PublicKey{step.receiver.Signing.Key.Public()}, o, binding.Options{}) + if err != nil { + t.Fatal(err) + } + } else { + verified, err = protocol.VerifyStatusResponse(t.Context(), response, []crypto.PublicKey{step.receiver.Signing.Key.Public()}, o) + if err != nil { + t.Fatal(err) + } + } + before, _ := step.store.Lookup(t.Context(), "local-account", step.receiverID, at) + if before.NameID != old.NameID { + t.Fatal("outgoing change applied before reply") + } + step.sender.Limits.MaxMessageAge = time.Second + if _, err := step.sender.Complete(t.Context(), verified, at.Add(2*time.Second)); err == nil { + t.Fatal("held response bypassed the service freshness policy") + } + if _, err := step.sender.Complete(t.Context(), verified, at); err != nil { + t.Fatal(err) + } + if _, err := step.sender.Complete(t.Context(), verified, at); err == nil { + t.Fatal("response replay") + } + local, _ := step.store.Lookup(t.Context(), "local-account", step.receiverID, at) + remote, _ := step.remote.Lookup(t.Context(), "local-account", step.senderID, at) + if local.NameID != remote.NameID || local.NameID.Format != protocol.NameIDPersistent { + t.Fatal("providers diverged") + } + if i == 0 && (local.NameID.Value != "alice" || local.NameID.SPProvidedID != step.value) { + t.Fatal("SP alias change affected primary ID") + } + if i == 1 && (local.NameID.Value != step.value || local.NameID.SPProvidedID != "new-sp-alias") { + t.Fatal("IdP change lost current SP alias") + } + } + old, _ := spStore.Lookup(t.Context(), "local-account", idpID, at) + _, err := spService.BeginManage(t.Context(), idpID, protocol.ManageNameIDRequest{ID: "_termination", Issuer: spID, Destination: mgmt, IssueInstant: at, NameID: old.NameID, Terminate: true}, at) + if err != nil { + t.Fatal(err) + } + _, err = spService.BeginLogout(t.Context(), idpID, protocol.LogoutRequest{ID: "_late_logout", Issuer: spID, Destination: "https://idp.test/slo", IssueInstant: at, NameID: old.NameID, SessionIndexes: []string{"index"}}, at) + if err == nil { + t.Fatal("Errata E8: SLO sent after sending Terminate") + } + if !spStore.Active(t.Context(), "sp-session", at) || !idpStore.Active(t.Context(), "idp-session", at) { + t.Fatal("name management invalidated active sessions") + } + // The remote reply may never arrive. Expiring correlation state must not + // undo a termination already sent, or allow late assertions under its name. + later := at.Add(6 * time.Minute) + if _, ok := spStore.Lookup(t.Context(), "local-account", idpID, later); ok { + t.Fatal("termination timed out back into an active relationship") + } + if err := spStore.Add(t.Context(), session.Session{ID: "late", Principal: "local-account", Peer: idpID, NameID: old.NameID, Index: "index", ExpiresAt: later.Add(time.Minute)}, later); err == nil { + t.Fatal("late assertion accepted after sending Terminate") + } + if err := spStore.Register(t.Context(), session.Relationship{Principal: "local-account", Peer: idpID, NameID: old.NameID, ExpiresAt: later.Add(time.Hour)}, later); err == nil { + t.Fatal("terminated identifier immediately reactivated") + } + _, err = spService.BeginLogout(t.Context(), idpID, protocol.LogoutRequest{ID: "_after_timeout", Issuer: spID, Destination: "https://idp.test/slo", IssueInstant: later, NameID: old.NameID, SessionIndexes: []string{"index"}}, later) + if err == nil || !spStore.Active(t.Context(), "sp-session", later) { + t.Fatal("E8 termination lifetime or preservation of active sessions violated") + } +} + +type failingSigner struct{ crypto.Signer } + +func (f failingSigner) Sign(io.Reader, []byte, crypto.SignerOpts) ([]byte, error) { + return nil, errors.New("signing device unavailable") +} + +func TestControlSigningFailureDoesNotCommitState(t *testing.T) { + c := testcert.New(t) + at := testcert.Now() + key := protocol.SigningKey{Key: c.LeafKey, CertificateDER: c.Leaf.Raw} + store, _ := session.NewMemoryStore(session.Config{EntityID: idpID, Role: metadata.IdentityProvider, Capacity: 100}) + register(t, store, spID, "local-account", "group", "index", "active") + service := session.Service{EntityID: idpID, Endpoint: mgmt, Role: metadata.IdentityProvider, Store: store, Signing: key} + service.Signing.Key = failingSigner{Signer: c.LeafKey} + r := managementRequest(t, key, "_sign_failure", name(spID), "new-alias", false) + if _, err := service.Manage(t.Context(), r, "https://sp.test/manage", at); err == nil { + t.Fatal("signer failure ignored") + } + before, _ := store.Lookup(t.Context(), "local-account", spID, at) + if before.NameID.SPProvidedID != "original-alias" { + t.Fatal("failed reply changed identifier") + } + service.Signing = key + if _, err := service.Manage(t.Context(), r, "https://sp.test/manage", at); err != nil { + t.Fatalf("failed signing consumed request: %v", err) + } +} + +func TestIdPInitiatedRedirectLogout(t *testing.T) { + at := testcert.Now() + c := testcert.New(t) + key := protocol.SigningKey{Key: c.LeafKey, CertificateDER: c.Leaf.Raw} + spStore, _ := session.NewMemoryStore(session.Config{EntityID: spID, Role: metadata.ServiceProvider, Capacity: 100}) + idpStore, _ := session.NewMemoryStore(session.Config{EntityID: idpID, Role: metadata.IdentityProvider, Capacity: 100}) + register(t, spStore, idpID, "local-account", "group", "index", "sp-session") + register(t, idpStore, spID, "local-account", "group", "index", "idp-session") + sender := session.Service{EntityID: idpID, Endpoint: "https://idp.test/slo", Role: metadata.IdentityProvider, Store: idpStore, Signing: key} + receiver := session.Service{EntityID: spID, Endpoint: slo, Role: metadata.ServiceProvider, Store: spStore, Signing: key} + d, err := sender.BeginLogout(t.Context(), spID, protocol.LogoutRequest{ID: "_idp_logout", Issuer: idpID, Destination: slo, IssueInstant: at, NameID: name(spID), NotOnOrAfter: at.Add(time.Minute)}, at) + if err != nil { + t.Fatal(err) + } + query, err := binding.SignRedirect(t.Context(), d, binding.Request, nil, key.Key, binding.Options{SignatureMethod: xmldsig.ECDSASHA256}) + if err != nil { + t.Fatal(err) + } + r, _, err := protocol.VerifyRedirectLogoutRequest(t.Context(), query, []crypto.PublicKey{key.Key.Public()}, protocol.ExchangeOptions{Issuer: idpID, Destination: slo, At: at}, binding.Options{}) + if err != nil { + t.Fatal(err) + } + reply, _, err := receiver.Logout(t.Context(), r, sender.Endpoint, at) + if err != nil { + t.Fatal(err) + } + query, err = binding.SignRedirect(t.Context(), reply, binding.Response, nil, key.Key, binding.Options{SignatureMethod: xmldsig.ECDSASHA256}) + if err != nil { + t.Fatal(err) + } + response, _, err := protocol.VerifyRedirectStatusResponse(t.Context(), query, []crypto.PublicKey{key.Key.Public()}, protocol.ExchangeOptions{Issuer: spID, Destination: sender.Endpoint, ExpectedRequestID: "_idp_logout", At: at}, binding.Options{}) + if err != nil { + t.Fatal(err) + } + if _, err := sender.Complete(t.Context(), response, at); err != nil { + t.Fatal(err) + } + if spStore.Active(t.Context(), "sp-session", at) || idpStore.Active(t.Context(), "idp-session", at) { + t.Fatal("IdP-initiated Redirect logout did not terminate both sides") + } +} diff --git a/saml/session/freshness_test.go b/saml/session/freshness_test.go new file mode 100644 index 0000000..ecbaa29 --- /dev/null +++ b/saml/session/freshness_test.go @@ -0,0 +1,204 @@ +package session_test + +import ( + "crypto" + "errors" + "testing" + "time" + + "github.com/skarm/xmlsec/internal/testutil/testcert" + "github.com/skarm/xmlsec/saml/metadata" + "github.com/skarm/xmlsec/saml/protocol" + "github.com/skarm/xmlsec/saml/session" +) + +func TestLogoutRechecksExpirationAtApplication(t *testing.T) { + for _, tc := range []struct { + name string + at time.Duration + skew time.Duration + want bool + }{ + {"before deadline", 0, 0, true}, + {"at deadline", time.Second, 0, false}, + {"after deadline", 2 * time.Second, 0, false}, + {"within skew", 2 * time.Second, 2 * time.Second, true}, + {"at skew boundary", 3 * time.Second, 2 * time.Second, false}, + } { + t.Run(tc.name, func(t *testing.T) { + at := testcert.Now() + cert := testcert.New(t) + key := protocol.SigningKey{Key: cert.LeafKey, CertificateDER: cert.Leaf.Raw} + store, err := session.NewMemoryStore(session.Config{EntityID: spID, Role: metadata.ServiceProvider}) + if err != nil { + t.Fatal(err) + } + register(t, store, idpID, "local-account", "authority", "index", "active") + request := protocol.LogoutRequest{ID: "_expiringLogout", Issuer: idpID, Destination: slo, IssueInstant: at, NameID: name(spID), SessionIndexes: []string{"index"}, NotOnOrAfter: at.Add(time.Second)} + doc, err := protocol.SignLogoutRequest(t.Context(), request, key) + if err != nil { + t.Fatal(err) + } + limits := protocol.Limits{ClockSkew: tc.skew} + verified, err := protocol.VerifyLogoutRequest(t.Context(), doc, []crypto.PublicKey{key.Key.Public()}, protocol.ExchangeOptions{Issuer: idpID, Destination: slo, At: at, Limits: limits}) + if err != nil { + t.Fatal(err) + } + service := session.Service{EntityID: spID, Endpoint: slo, Role: metadata.ServiceProvider, Store: store, Signing: key, Limits: limits} + response, _, err := service.Logout(t.Context(), verified, "https://idp.test/slo", at.Add(tc.at)) + if tc.want { + if err != nil || response == nil || store.Active(t.Context(), "active", at.Add(tc.at)) { + t.Fatal("fresh logout did not terminate the session", err) + } + } else if !errors.Is(err, session.ErrState) || response != nil || !store.Active(t.Context(), "active", at.Add(tc.at)) { + t.Fatal("expired authenticated logout changed session state", err) + } + }) + } +} + +func TestBeginLogoutRejectsExpiredRequest(t *testing.T) { + at := testcert.Now() + cert := testcert.New(t) + store, err := session.NewMemoryStore(session.Config{EntityID: idpID, Role: metadata.IdentityProvider}) + if err != nil { + t.Fatal(err) + } + register(t, store, spID, "local-account", "authority", "index", "active") + service := session.Service{EntityID: idpID, Endpoint: mgmt, Role: metadata.IdentityProvider, Store: store, Signing: protocol.SigningKey{Key: cert.LeafKey, CertificateDER: cert.Leaf.Raw}} + request := protocol.LogoutRequest{ID: "_expiredOutgoing", Issuer: idpID, Destination: slo, IssueInstant: at, NameID: name(spID), SessionIndexes: []string{"index"}, NotOnOrAfter: at.Add(time.Second)} + response, err := service.BeginLogout(t.Context(), spID, request, at.Add(2*time.Second)) + if !errors.Is(err, session.ErrState) || response != nil || !store.Active(t.Context(), "active", at.Add(2*time.Second)) { + t.Fatal("expired outgoing request was published or changed state", err) + } +} + +func TestLogoutBlocksDelayedAssertionThroughClockSkew(t *testing.T) { + at := testcert.Now() + cert := testcert.New(t) + key := protocol.SigningKey{Key: cert.LeafKey, CertificateDER: cert.Leaf.Raw} + keys := []crypto.PublicKey{key.Key.Public()} + limits := protocol.Limits{ClockSkew: 2 * time.Second} + identifier := name(spID) + deadline := at.Add(time.Second) + response, err := protocol.SignResponse(t.Context(), protocol.Response{ + ID: "_delayedResponse", Issuer: idpID, Audience: spID, + Destination: "https://sp.test/acs", InResponseTo: "_pending", IssueInstant: at, + Limits: limits, + Assertion: protocol.AssertionData{ + ID: "_delayedAssertion", NameID: identifier.Value, NameIDFormat: identifier.Format, + NameQualifier: identifier.NameQualifier, SPNameQualifier: identifier.SPNameQualifier, + SPProvidedID: identifier.SPProvidedID, NotBefore: at, NotOnOrAfter: deadline, + AuthnInstant: at, AuthnContext: "urn:test:password", SessionIndex: "index", + SessionNotOnOrAfter: at.Add(30 * time.Minute), + }, + }, key) + if err != nil { + t.Fatal(err) + } + + logout, err := protocol.SignLogoutRequest(t.Context(), protocol.LogoutRequest{ + ID: "_logoutSkew", Issuer: idpID, Destination: slo, IssueInstant: at, + NameID: identifier, SessionIndexes: []string{"index"}, NotOnOrAfter: deadline, + }, key) + if err != nil { + t.Fatal(err) + } + verified, err := protocol.VerifyLogoutRequest(t.Context(), logout, keys, protocol.ExchangeOptions{ + Issuer: idpID, Destination: slo, At: at, Limits: limits, + }) + if err != nil { + t.Fatal(err) + } + + later := at.Add(2 * time.Second) + options := protocol.ResponseOptions{ + Issuer: idpID, Audience: spID, Destination: "https://sp.test/acs", + ExpectedRequestID: "_pending", At: later, Limits: limits, + } + assertion, err := protocol.VerifyResponse(t.Context(), response, keys, options) + if err != nil { + t.Fatal("signed delayed assertion must remain valid within clock skew", err) + } + late := session.Session{ + ID: "late", Principal: "local-account", Peer: idpID, NameID: assertion.Identifier(), + Index: assertion.SessionIndex(), ExpiresAt: assertion.SessionNotOnOrAfter(), + } + + for _, tc := range []struct { + name string + at time.Time + outgoing bool + }{ + {"applied before deadline", at, false}, + {"applied within clock skew", later, false}, + {"initiated before deadline", at, true}, + } { + t.Run(tc.name, func(t *testing.T) { + store, err := session.NewMemoryStore(session.Config{EntityID: spID, Role: metadata.ServiceProvider}) + if err != nil { + t.Fatal(err) + } + register(t, store, idpID, "local-account", "authority", "index", "active") + service := session.Service{ + EntityID: spID, Endpoint: slo, Role: metadata.ServiceProvider, + Store: store, Signing: key, Limits: limits, + } + if tc.outgoing { + _, err = service.BeginLogout(t.Context(), idpID, protocol.LogoutRequest{ + ID: "_outgoingSkew", Issuer: spID, Destination: "https://idp.test/slo", IssueInstant: at, + NameID: identifier, SessionIndexes: []string{"index"}, NotOnOrAfter: deadline, + }, tc.at) + } else { + _, _, err = service.Logout(t.Context(), verified, "https://idp.test/slo", tc.at) + } + if err != nil { + t.Fatal(err) + } + if store.Active(t.Context(), "active", tc.at) { + t.Fatal("logout did not invalidate the existing session") + } + if err := store.Add(t.Context(), late, later); !errors.Is(err, session.ErrState) { + t.Fatal("delayed assertion recreated a logged-out SessionIndex", err) + } + end := deadline.Add(limits.ClockSkew) + if err := store.Add(t.Context(), late, end.Add(-time.Nanosecond)); !errors.Is(err, session.ErrState) { + t.Fatal("logout block expired before the full acceptance window", err) + } + if err := store.Add(t.Context(), late, end); err != nil { + t.Fatal("expired logout block still prevents a new session", err) + } + }) + } + + options.At = deadline.Add(limits.ClockSkew) + if _, err := protocol.VerifyResponse(t.Context(), response, keys, options); !errors.Is(err, protocol.ErrConditions) { + t.Fatal("assertion accepted at the exclusive skew boundary", err) + } +} + +func TestLogoutRejectsInvalidClockSkewWithoutChangingState(t *testing.T) { + at := testcert.Now() + cert := testcert.New(t) + key := protocol.SigningKey{Key: cert.LeafKey, CertificateDER: cert.Leaf.Raw} + verified := logoutRequest(t, key, idpID, slo, "_invalidSkew", name(spID), "index") + + for _, skew := range []time.Duration{-time.Nanosecond, 5*time.Minute + time.Nanosecond} { + t.Run(skew.String(), func(t *testing.T) { + store, err := session.NewMemoryStore(session.Config{EntityID: spID, Role: metadata.ServiceProvider}) + if err != nil { + t.Fatal(err) + } + register(t, store, idpID, "local-account", "authority", "index", "active") + if _, err := store.Logout(t.Context(), verified, false, at, at.Add(time.Hour), skew); !errors.Is(err, session.ErrState) { + t.Fatal("invalid clock skew accepted", err) + } + if !store.Active(t.Context(), "active", at) { + t.Fatal("invalid clock skew invalidated the session") + } + if _, err := store.Logout(t.Context(), verified, false, at, at.Add(time.Hour), 0); err != nil { + t.Fatal("invalid clock skew consumed replay state", err) + } + }) + } +} diff --git a/saml/session/logout_retention_test.go b/saml/session/logout_retention_test.go new file mode 100644 index 0000000..469c763 --- /dev/null +++ b/saml/session/logout_retention_test.go @@ -0,0 +1,146 @@ +package session_test + +import ( + "crypto" + "errors" + "testing" + "time" + + "github.com/skarm/xmlsec/internal/testutil/testcert" + "github.com/skarm/xmlsec/saml/metadata" + "github.com/skarm/xmlsec/saml/protocol" + "github.com/skarm/xmlsec/saml/session" +) + +func TestOutgoingLogoutBlockSurvivesCorrelation(t *testing.T) { + at := testcert.Now() + cert := testcert.New(t) + key := protocol.SigningKey{Key: cert.LeafKey, CertificateDER: cert.Leaf.Raw} + for _, tc := range []struct { + name, status string + deadline time.Time + }{ + {"pending expiry", "", at.Add(10 * time.Minute)}, + {"success response", protocol.Success, at.Add(10 * time.Minute)}, + {"failure response", protocol.Responder, at.Add(10 * time.Minute)}, + {"no deadline", "", time.Time{}}, + } { + t.Run(tc.name, func(t *testing.T) { + store, err := session.NewMemoryStore(session.Config{EntityID: spID, Role: metadata.ServiceProvider}) + if err != nil { + t.Fatal(err) + } + identifier := name(spID) + if err := store.Register(t.Context(), session.Relationship{ + Principal: "local-account", Peer: idpID, NameID: identifier, ExpiresAt: at.Add(3 * time.Hour), + }, at); err != nil { + t.Fatal(err) + } + active := session.Session{ + ID: "active", Principal: "local-account", Peer: idpID, NameID: identifier, + Index: "index", ExpiresAt: at.Add(150 * time.Minute), + } + if err := store.Add(t.Context(), active, at); err != nil { + t.Fatal(err) + } + service := session.Service{ + EntityID: spID, Endpoint: slo, Role: metadata.ServiceProvider, Store: store, Signing: key, + Limits: protocol.Limits{ClockSkew: 5 * time.Second}, + } + request := protocol.LogoutRequest{ + ID: "_outgoing", Issuer: spID, Destination: "https://idp.test/slo", IssueInstant: at, + NameID: identifier, SessionIndexes: []string{"index"}, NotOnOrAfter: tc.deadline, + } + if _, err := service.BeginLogout(t.Context(), idpID, request, at); err != nil { + t.Fatal(err) + } + if store.Active(t.Context(), active.ID, at) { + t.Fatal("outgoing logout retained the original session") + } + if tc.status != "" { + now := at.Add(time.Minute) + doc, err := protocol.SignStatusResponse(t.Context(), protocol.StatusResponse{ + ID: "_logoutReply", Issuer: idpID, Destination: slo, IssueInstant: now, + Kind: protocol.LogoutResponse, InResponseTo: request.ID, Status: protocol.Status{Code: tc.status}, + }, key) + if err != nil { + t.Fatal(err) + } + response, err := protocol.VerifyStatusResponse(t.Context(), doc, []crypto.PublicKey{key.Key.Public()}, protocol.ExchangeOptions{ + Issuer: idpID, Destination: slo, At: now, ExpectedRequestID: request.ID, + }) + if err != nil { + t.Fatal(err) + } + if _, err := service.Complete(t.Context(), response, now); err != nil { + t.Fatal(err) + } + } + + later := at.Add(6 * time.Minute) + late := active + late.ID = "late" + if err := store.Add(t.Context(), late, later); !errors.Is(err, session.ErrState) { + t.Fatal("correlation completion or expiry removed the logout block", err) + } + other := late + other.ID, other.Index = "other-session", "other-index" + if err := store.Add(t.Context(), other, later); err != nil { + t.Fatal("outgoing logout blocked an unrelated SessionIndex", err) + } + // Reusing an expired pending ID must not overwrite a live block + // with a shorter deadline or different SessionIndex. + request.IssueInstant = later + request.NotOnOrAfter = later.Add(time.Minute) + request.SessionIndexes = []string{other.Index} + if _, err := service.BeginLogout(t.Context(), idpID, request, later); !errors.Is(err, session.ErrState) { + t.Fatal("live logout block overwritten by a reused request ID", err) + } + if !store.Active(t.Context(), other.ID, later) { + t.Fatal("rejected ID reuse invalidated an unrelated session") + } + end := at.Add(2 * time.Hour) + if !tc.deadline.IsZero() { + end = tc.deadline.Add(service.Limits.ClockSkew) + } + if err := store.Add(t.Context(), late, end); err != nil { + t.Fatal("outgoing logout block did not expire at its deadline", err) + } + }) + } +} + +func TestOutgoingLogoutCapacityFailurePreservesSession(t *testing.T) { + at := testcert.Now() + cert := testcert.New(t) + key := protocol.SigningKey{Key: cert.LeafKey, CertificateDER: cert.Leaf.Raw} + store, err := session.NewMemoryStore(session.Config{EntityID: spID, Role: metadata.ServiceProvider, Capacity: 1}) + if err != nil { + t.Fatal(err) + } + register(t, store, idpID, "local-account", "authority", "first-index", "first") + service := session.Service{EntityID: spID, Endpoint: slo, Role: metadata.ServiceProvider, Store: store, Signing: key} + request := protocol.LogoutRequest{ + ID: "_firstLogout", Issuer: spID, Destination: "https://idp.test/slo", IssueInstant: at, + NameID: name(spID), SessionIndexes: []string{"first-index"}, NotOnOrAfter: at.Add(10 * time.Minute), + } + if _, err := service.BeginLogout(t.Context(), idpID, request, at); err != nil { + t.Fatal(err) + } + later := at.Add(6 * time.Minute) + active := session.Session{ + ID: "second", Principal: "local-account", Peer: idpID, NameID: name(spID), + Index: "second-index", ExpiresAt: at.Add(30 * time.Minute), + } + if err := store.Add(t.Context(), active, later); err != nil { + t.Fatal(err) + } + request.ID, request.IssueInstant = "_secondLogout", later + request.SessionIndexes = []string{active.Index} + if _, err := service.BeginLogout(t.Context(), idpID, request, later); !errors.Is(err, session.ErrState) { + t.Fatal("outgoing logout exceeded block capacity", err) + } + if !store.Active(t.Context(), active.ID, later) { + t.Fatal("capacity failure invalidated a session without retaining its block") + } +} diff --git a/saml/session/performance_test.go b/saml/session/performance_test.go new file mode 100644 index 0000000..0945fdc --- /dev/null +++ b/saml/session/performance_test.go @@ -0,0 +1,73 @@ +package session_test + +import ( + "strconv" + "testing" + "time" + + "github.com/skarm/xmlsec/internal/testutil/testcert" + "github.com/skarm/xmlsec/saml/metadata" + "github.com/skarm/xmlsec/saml/protocol" + "github.com/skarm/xmlsec/saml/session" +) + +// Each operation registers and signs in size independent users. Input setup is +// excluded; store growth, uniqueness checks and expiry maintenance are included. +func BenchmarkMemoryStorePopulation(b *testing.B) { + at := testcert.Now() + for _, size := range []int{16, 1024, 8192} { + b.Run(strconv.Itoa(size), func(b *testing.B) { + relations := make([]session.Relationship, size) + sessions := make([]session.Session, size) + for i := range relations { + id := "user-" + strconv.Itoa(i) + n := protocol.NameIdentifier{Value: id} + relations[i] = session.Relationship{Principal: id, Peer: idpID, NameID: n, ExpiresAt: at.Add(time.Hour)} + sessions[i] = session.Session{ID: id, Principal: id, Peer: idpID, NameID: n, ExpiresAt: at.Add(time.Duration(1+i%4) * time.Minute)} + } + b.ReportAllocs() + for b.Loop() { + store, err := session.NewMemoryStore(session.Config{EntityID: spID, Role: metadata.ServiceProvider, Capacity: size}) + if err != nil { + b.Fatal(err) + } + for i, r := range relations { + if err := store.Register(b.Context(), r, at); err != nil { + b.Fatal(err) + } + if err := store.Add(b.Context(), sessions[i], at); err != nil { + b.Fatal(err) + } + } + } + }) + } +} + +func BenchmarkMemoryStoreExpire(b *testing.B) { + at := testcert.Now() + relations := make([]session.Relationship, 1024) + for i := range relations { + id := "user-" + strconv.Itoa(i) + relations[i] = session.Relationship{Principal: id, Peer: idpID, NameID: protocol.NameIdentifier{Value: id}, ExpiresAt: at.Add(time.Minute)} + } + b.ReportAllocs() + for b.Loop() { + b.StopTimer() + store, err := session.NewMemoryStore(session.Config{EntityID: spID, Role: metadata.ServiceProvider, Capacity: len(relations)}) + if err != nil { + b.Fatal(err) + } + for _, r := range relations { + if err := store.Register(b.Context(), r, at); err != nil { + b.Fatal(err) + } + } + r := relations[0] + r.ExpiresAt = at.Add(2 * time.Minute) + b.StartTimer() + if err := store.Register(b.Context(), r, at.Add(time.Minute)); err != nil { + b.Fatal(err) + } + } +} diff --git a/saml/session/reply.go b/saml/session/reply.go new file mode 100644 index 0000000..9ae31cf --- /dev/null +++ b/saml/session/reply.go @@ -0,0 +1,56 @@ +package session + +import ( + "context" + "slices" + "time" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/saml/protocol" +) + +type preparedReply struct { + status protocol.Status + message *document.Document +} +type preparedReplies []preparedReply + +// Prepare all store outcomes before committing local state. A failed signer +// therefore cannot change a name or invalidate sessions without a usable reply. +func (s Service) prepareReplies(ctx context.Context, kind, requestID, endpoint string, at time.Time) (preparedReplies, error) { + statuses := []protocol.Status{ + {Code: protocol.Success}, + {Code: protocol.Success, Subcodes: []string{protocol.PartialLogout}}, + {Code: protocol.Requester, Subcodes: []string{protocol.UnknownPrincipal}}, + {Code: protocol.Requester}, + } + + replies := make(preparedReplies, 0, len(statuses)) + for _, status := range statuses { + d, err := protocol.SignStatusResponse(ctx, protocol.StatusResponse{ + Issuer: s.EntityID, + Destination: endpoint, + IssueInstant: at, + Kind: kind, + InResponseTo: requestID, + Status: status, + }, s.Signing) + if err != nil { + return nil, err + } + + replies = append(replies, preparedReply{status: status, message: d}) + } + + return replies, nil +} + +func (replies preparedReplies) selectReply(status protocol.Status) (*document.Document, error) { + for _, r := range replies { + if r.status.Code == status.Code && slices.Equal(r.status.Subcodes, status.Subcodes) && status.Message == "" { + return r.message, nil + } + } + + return nil, ErrState +} diff --git a/saml/session/session.go b/saml/session/session.go new file mode 100644 index 0000000..76c82c5 --- /dev/null +++ b/saml/session/session.go @@ -0,0 +1,1189 @@ +// Package session applies SAML logout and identifier-management protocols to +// application-owned sessions and provider relationships. Its Store is a security +// boundary: applications must consult the same session state for authorization. +package session + +import ( + "context" + "errors" + "slices" + "sync" + "time" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/lexicon" + "github.com/skarm/xmlsec/internal/nilcheck" + "github.com/skarm/xmlsec/saml/internal/expiry" + "github.com/skarm/xmlsec/saml/internal/profile" + "github.com/skarm/xmlsec/saml/metadata" + "github.com/skarm/xmlsec/saml/protocol" +) + +const ( + // Pending logout and NameID-management exchanges share a five-minute exclusive expiry; issuance and store validation enforce the same ceiling. + pendingRequestLifetime = 5 * time.Minute + + // Retain accepted logout and NameID-management exchange IDs for two hours; this replay window does not extend an application session. + controlReplayLifetime = 2 * time.Hour + + // Separate identity components before replay/session scoping so adjoining variable-length identifiers cannot share the same key encoding. + scopeSeparator = "\u0000" +) + +var ( + // ErrState reports invalid, missing, expired or replayed exchange state. + ErrState = errors.New("SAML session state rejected") + // ErrReplay indicates that a control request has already been applied. + ErrReplay = errors.New("SAML control request replay") +) + +// Relationship is the current identifier binding to one peer. Principal is an +// application-local account ID and must not change when the federated name does. +// Termination removes this binding and preserves existing sessions (Errata E8). +type Relationship struct { + // Principal is the stable application-local account ID. + Principal string + // Peer is the entity ID of the provider sharing this identifier binding. + Peer string + // NameID is the complete current federated identifier. + NameID protocol.NameIdentifier + // ExpiresAt is the exclusive expiration time of the relationship. + ExpiresAt time.Time + // terminated retains a withdrawn binding without deleting its existing + // sessions. + terminated bool +} + +// Session records an application session issued by or to Peer. AuthoritySession +// groups the participants authenticated under the same IdP session. Index is the +// SessionIndex communicated to Peer; ID is the application's local session ID. +type Session struct { + // ID is the application's local session identifier. + ID string + // Principal is the stable application-local account ID. + Principal string + // Peer is the provider to which this session is bound. + Peer string + // AuthoritySession groups participants authenticated under the same IdP + // session. + AuthoritySession string + // Index is the SAML SessionIndex communicated to Peer. + Index string + // NameID is established from the authenticated assertion. The store retains + // its canonical current form across management changes. Add rejects stale + // or different bindings rather than guessing from Principal. + NameID protocol.NameIdentifier + // ExpiresAt is the exclusive session expiration time and cannot exceed the + // relationship's expiration. + ExpiresAt time.Time +} + +// Participant identifies another peer affected by authority-initiated logout. +type Participant struct { + // Peer is the entity ID of the provider to notify. + Peer string + // NameID is the complete identifier established with Peer. + NameID protocol.NameIdentifier + // SessionIndexes selects the peer sessions affected by logout. + SessionIndexes []string +} + +// LogoutResult contains locally invalidated session IDs and peers to notify. +type LogoutResult struct { + // Status is the local or aggregate logout outcome, including PartialLogout when + // needed. + Status protocol.Status + // SessionIDs lists application session IDs invalidated locally. + SessionIDs []string + // Participants lists peer sessions affected by the same authority logout. + Participants []Participant +} + +// Pending binds an outgoing exchange to the exact peer, request, response kind, +// old identifier and requested change. A failed response must not apply a change. +type Pending struct { + // ID is the outgoing request's exact XML identifier. + ID string + // Peer is the expected response issuer. + Peer string + // Kind is the expected response element's local name. + Kind string + // NameID retains the complete identifier before the requested change. + NameID protocol.NameIdentifier + // NewID retains the requested replacement identifier or SP alias. + NewID string + // Terminate records a request to end the identifier binding. + Terminate bool + // LocalIsIDP selects whether NewID changes NameID content or SPProvidedID. + LocalIsIDP bool + // ExpiresAt is the exclusive expiration time for response correlation. + ExpiresAt time.Time + // SessionIndexes retains the sessions selected by an outgoing logout. + SessionIndexes []string + // NotOnOrAfter and ClockSkew retain an outgoing logout's block deadline + // independently of pending correlation. Zero NotOnOrAfter uses the replay + // lifetime as a fallback; management exchanges leave both fields zero. + NotOnOrAfter time.Time + // ClockSkew retains the accepted tolerance for an outgoing logout's block + // deadline. + ClockSkew time.Duration +} + +// Store must atomically apply state changes and replay markers. Finish must +// atomically correlate and consume pending state and, on a successful management +// response, apply its identifier change. Shared deployments need a durable store. +type Store interface { + // Provider returns the immutable local entity/role owning this state. + Provider() (string, metadata.RoleType) + // Logout retains the session block through NotOnOrAfter plus clockSkew. + // The times are the current time and replay expiration; clockSkew must + // cover the policy used to accept late assertions for these sessions. + Logout(ctx context.Context, request *protocol.VerifiedLogoutRequest, authority bool, at, until time.Time, clockSkew time.Duration) (LogoutResult, error) + Manage(context.Context, *protocol.VerifiedManageNameIDRequest, bool, time.Time, time.Time) (protocol.Status, error) + // Start also invalidates outgoing logout sessions and retains their block + // independently of pending state, including after Finish or pending expiry. + Start(context.Context, Pending, time.Time) error + Finish(context.Context, *protocol.VerifiedStatusResponse, time.Time) (Pending, error) + Withdraw(context.Context, string, string, time.Time) (LogoutResult, error) +} + +// Service implements both participant and authority processing. Notify must send +// an authenticated, correlated LogoutRequest to the trusted peer endpoint and +// return its verified status. Missing/failed notifications produce PartialLogout. +type Service struct { + // EntityID identifies the local session provider. + EntityID string + // Endpoint is the trusted local endpoint at which incoming messages were + // received and authenticated. It prevents cross-service request substitution. + Endpoint string + // ResponseEndpoint is the metadata ResponseLocation; empty uses Endpoint. + ResponseEndpoint string + // Role selects local IdP or SP semantics for session and identifier changes. + Role metadata.RoleType + // Store atomically applies session changes, correlation and replay protection. + Store Store + // Signing supplies the response signing key and policy. + Signing protocol.SigningKey + // Limits must match the incoming protocol verifier's acceptance policy. + // ClockSkew must also cover the policy used to accept late assertions. + Limits protocol.Limits + // Notify sends an authenticated, correlated logout to a participant. Nil or an + // error produces PartialLogout. + Notify func(context.Context, Participant) (protocol.Status, error) +} + +// Logout applies the authenticated request once, attempts every other affected +// participant at an authority, and signs the final status. responseEndpoint must +// be selected from the requesting peer's current trusted metadata, including its +// ResponseLocation when appropriate. It never uses a received response URL. +func (s Service) Logout(ctx context.Context, r *protocol.VerifiedLogoutRequest, responseEndpoint string, at time.Time) (*document.Document, LogoutResult, error) { + if !s.valid() || r == nil || !s.freshLogout(r.Request(), at) || r.Request().Destination != s.Endpoint || s.Role == metadata.IdentityProvider && len(r.Request().SessionIndexes) == 0 || s.Role == metadata.ServiceProvider && r.Request().NotOnOrAfter.IsZero() { + return nil, LogoutResult{}, ErrState + } + + replies, err := s.prepareReplies(ctx, protocol.LogoutResponse, r.Request().ID, responseEndpoint, at) + if err != nil { + return nil, LogoutResult{}, err + } + + result, err := s.Store.Logout(ctx, r, s.Role == metadata.IdentityProvider, at, at.Add(controlReplayLifetime), s.Limits.ClockSkew) + if err != nil { + return nil, result, err + } + + for _, participant := range result.Participants { + if participant.Peer == s.EntityID { + continue + } + + status := protocol.Status{} + if s.Notify != nil { + status, err = s.Notify(ctx, participant) + } else { + err = ErrState + } + + if err != nil || status.Code != protocol.Success || slices.Contains(status.Subcodes, protocol.PartialLogout) { + result.Status = protocol.Status{Code: protocol.Success, Subcodes: []string{protocol.PartialLogout}} + } + } + + d, err := replies.selectReply(result.Status) + + return d, result, err +} + +// Manage applies the role-specific NameID or SPProvidedID change atomically and +// responds to both successful and unknown-principal requests. +func (s Service) Manage(ctx context.Context, r *protocol.VerifiedManageNameIDRequest, responseEndpoint string, at time.Time) (*document.Document, error) { + if !s.valid() || r == nil || !s.fresh(r.Request().MessageHeader, at) || r.Request().Destination != s.Endpoint { + return nil, ErrState + } + + replies, err := s.prepareReplies(ctx, protocol.ManageNameIDResponse, r.Request().ID, responseEndpoint, at) + if err != nil { + return nil, err + } + + status, err := s.Store.Manage(ctx, r, s.Role == metadata.ServiceProvider, at, at.Add(controlReplayLifetime)) + if err != nil { + return nil, err + } + + return replies.selectReply(status) +} + +// BeginManage signs a change and persists its correlation state. The local copy +// is updated only after Complete receives the successful authenticated response. +func (s Service) BeginManage(ctx context.Context, peer string, request protocol.ManageNameIDRequest, at time.Time) (*document.Document, error) { + if !s.valid() || !s.fresh(request.MessageHeader, at) || request.Issuer != s.EntityID { + return nil, ErrState + } + + request.NameID = qualifiedName(request.NameID, s.EntityID, peer, s.Role) + + if s.Role == metadata.IdentityProvider && !request.Terminate { + replacement := request.NameID + + replacement.Value = request.NewID + if err := replacement.Validate(); err != nil { + return nil, err + } + } + + d, err := protocol.SignManageNameIDRequest(ctx, request, s.Signing) + if err != nil { + return nil, err + } + + id, _ := d.Root().Attribute(document.Name{Local: lexicon.SAMLAttrID}) + + if !profile.URI(peer) || peer == s.EntityID { + return nil, ErrState + } + + err = s.Store.Start(ctx, Pending{ID: id, Peer: peer, Kind: protocol.ManageNameIDResponse, NameID: request.NameID, NewID: request.NewID, Terminate: request.Terminate, LocalIsIDP: s.Role == metadata.IdentityProvider, ExpiresAt: at.Add(pendingRequestLifetime)}, at) + if err != nil { + return nil, err + } + + return d, nil +} + +// Complete consumes the exact outgoing exchange once. The caller must verify +// response signature, destination and request correlation before calling it. +func (s Service) Complete(ctx context.Context, response *protocol.VerifiedStatusResponse, at time.Time) (Pending, error) { + expected := s.ResponseEndpoint + if expected == "" { + expected = s.Endpoint + } + + if !s.valid() || response == nil || !s.fresh(response.Header(), at) || response.Header().Destination != expected { + return Pending{}, ErrState + } + + return s.Store.Finish(ctx, response, at) +} + +// InitiateLogout terminates an IdP's application-authenticated authority session +// and attempts logout at every participant. A nonempty authoritySession prevents +// logout at another device/session of the same account. Failed notifications are +// reported with PartialLogout; every participant is attempted. +func (s Service) InitiateLogout(ctx context.Context, principal, authoritySession string, at time.Time) (LogoutResult, error) { + if !s.valid() || s.Role != metadata.IdentityProvider { + return LogoutResult{}, ErrState + } + + r, err := s.Store.Withdraw(ctx, principal, authoritySession, at) + if err != nil { + return r, err + } + + for _, p := range r.Participants { + if p.Peer == s.EntityID { + continue + } + + status := protocol.Status{} + if s.Notify != nil { + status, err = s.Notify(ctx, p) + } else { + err = ErrState + } + + if err != nil || status.Code != protocol.Success || slices.Contains(status.Subcodes, protocol.PartialLogout) { + r.Status = protocol.Status{Code: protocol.Success, Subcodes: []string{protocol.PartialLogout}} + } + } + + return r, nil +} + +// BeginLogout signs a provider-initiated request and retains its correlation. +// Start invalidates the matching initiating local sessions and blocks delayed +// assertions atomically with pending state. The block survives Complete and +// pending expiry; the authority's final status is consumed through Complete. +func (s Service) BeginLogout(ctx context.Context, peer string, request protocol.LogoutRequest, at time.Time) (*document.Document, error) { + if !s.valid() || !s.freshLogout(request, at) || request.Issuer != s.EntityID || s.Role == metadata.ServiceProvider && len(request.SessionIndexes) == 0 || s.Role == metadata.IdentityProvider && request.NotOnOrAfter.IsZero() { + return nil, ErrState + } + + request.NameID = qualifiedName(request.NameID, s.EntityID, peer, s.Role) + + d, err := protocol.SignLogoutRequest(ctx, request, s.Signing) + if err != nil { + return nil, err + } + + id, _ := d.Root().Attribute(document.Name{Local: lexicon.SAMLAttrID}) + + err = s.Store.Start(ctx, Pending{ID: id, Peer: peer, Kind: protocol.LogoutResponse, NameID: request.NameID, LocalIsIDP: s.Role == metadata.IdentityProvider, SessionIndexes: slices.Clone(request.SessionIndexes), ExpiresAt: at.Add(pendingRequestLifetime), NotOnOrAfter: request.NotOnOrAfter, ClockSkew: s.Limits.ClockSkew}, at) + if err != nil { + return nil, err + } + + return d, nil +} + +func (s Service) valid() bool { + if !profile.URI(s.EntityID) || !profile.Endpoint(s.Endpoint, true) || nilcheck.IsNil(s.Store) { + return false + } + + entity, role := s.Store.Provider() + + return entity == s.EntityID && role == s.Role && (role == metadata.ServiceProvider || role == metadata.IdentityProvider) +} + +func (s Service) fresh(h protocol.MessageHeader, at time.Time) bool { + age, _, skew, err := protocol.AcceptanceWindow(s.Limits) + return err == nil && !at.IsZero() && !h.IssueInstant.IsZero() && !h.IssueInstant.After(at.Add(skew)) && !h.IssueInstant.Before(at.Add(-age-skew)) +} + +func (s Service) freshLogout(r protocol.LogoutRequest, at time.Time) bool { + return s.fresh(r.MessageHeader, at) && (r.NotOnOrAfter.IsZero() || at.Add(-s.Limits.ClockSkew).Before(r.NotOnOrAfter)) +} + +// MemoryStore is a bounded, concurrency-safe reference session/relationship +// store. Zero values are not usable. Expired records are removed on mutations. +type MemoryStore struct { + // mu serializes all state transitions and index updates. + mu sync.Mutex + // capacity bounds records independently in each category. + capacity int + // entityID is the immutable local provider identity. + entityID string + // role fixes local IdP or SP identifier-management semantics. + role metadata.RoleType + // relationships retains current and terminated principal-to-peer bindings. + relationships map[string]Relationship + // sessions indexes application sessions by local session ID. + sessions map[string]Session + // seen retains accepted control request IDs through their replay deadlines. + seen map[string]time.Time + // pending retains outgoing requests awaiting correlated responses. + pending map[string]Pending + // logoutMarks blocks delayed assertions from recreating logged-out sessions. + logoutMarks map[string]logoutMark + // expiry indexes expiration across all record categories. + expiry expiry.Index[recordKey] + // names indexes complete peer-qualified identifiers to relationship keys. + names map[nameKey]string +} + +type recordKind uint8 + +const ( + relationshipRecord recordKind = iota + sessionRecord + seenRecord + pendingRecord + logoutRecord +) + +type recordKey struct { + kind recordKind + id string +} + +type nameKey struct { + peer string + name protocol.NameIdentifier +} + +func identifierKey(peer string, name protocol.NameIdentifier) nameKey { + if name.Format == "" { + name.Format = protocol.NameIDUnspecified + } + + return nameKey{peer, name} +} + +type logoutMark struct { + // peer identifies the provider whose delayed sessions are blocked. + peer string + // name is the complete identifier selected by logout. + name protocol.NameIdentifier + // indices selects blocked session indices; empty selects all matching sessions. + indices []string + // until is the exclusive expiration of the block, including clock tolerance. + until time.Time +} + +// Config binds a session store to one local provider and role. This context +// resolves the implicit qualifiers allowed by persistent NameID formats. +type Config struct { + // EntityID identifies the local provider owning the store. + EntityID string + // Role selects the local IdentityProvider or ServiceProvider role. + Role metadata.RoleType + // Capacity bounds records per category. Zero selects 10,000; live records are + // never evicted to make room. + Capacity int +} + +// NewMemoryStore creates a bounded in-process store; zero capacity selects 10,000 records +// per category. No live sessions, relationships or replay records are evicted. +func NewMemoryStore(c Config) (*MemoryStore, error) { + const ( + defaultSessionCapacity = 10000 + maximumSessionCapacity = 1000000 + ) + + if !profile.URI(c.EntityID) || c.Role != metadata.ServiceProvider && c.Role != metadata.IdentityProvider { + return nil, ErrState + } + + capacity := c.Capacity + if capacity == 0 { + capacity = defaultSessionCapacity + } + + if capacity < 1 || capacity > maximumSessionCapacity { + return nil, ErrState + } + + return &MemoryStore{capacity: capacity, entityID: c.EntityID, role: c.Role, relationships: map[string]Relationship{}, sessions: map[string]Session{}, seen: map[string]time.Time{}, pending: map[string]Pending{}, logoutMarks: map[string]logoutMark{}, names: map[nameKey]string{}}, nil +} + +// Provider returns the immutable local provider context. +func (s *MemoryStore) Provider() (string, metadata.RoleType) { + if s == nil { + return "", "" + } + + return s.entityID, s.role +} + +// Register establishes a binding explicitly after authenticated account linking. +// It rejects reassignment, duplicate peer identifiers and replacing an existing +// binding. Name changes must use the management transaction instead. +func (s *MemoryStore) Register(ctx context.Context, r Relationship, at time.Time) error { + const ( + maximumPrincipalBytes = 1024 + ) + + if err := s.check(ctx, at); err != nil { + return err + } + + r.NameID = qualifiedName(r.NameID, s.entityID, r.Peer, s.role) + r.terminated = false + + if r.Principal == "" || len(r.Principal) > maximumPrincipalBytes || !profile.URI(r.Peer) || !validName(r.NameID) || !at.Before(r.ExpiresAt) { + return ErrState + } + + if err := s.lock(ctx); err != nil { + return err + } + defer s.mu.Unlock() + + s.prune(at) + + k := relationshipKey(r.Principal, r.Peer) + + old, exists := s.relationships[k] + if exists && (!old.terminated || sameName(old.NameID, r.NameID)) || !exists && len(s.relationships) >= s.capacity { + return ErrState + } + + if _, exists := s.names[identifierKey(r.Peer, r.NameID)]; exists { + return ErrState + } + + s.setRelationship(k, r) + + return nil +} + +// Add records an application session for an existing unexpired relationship. +func (s *MemoryStore) Add(ctx context.Context, session Session, at time.Time) error { + const ( + maximumAuthoritySessionBytes = 1024 + maximumSessionIDBytes = 1024 + maximumSessionIndexBytes = 4096 + ) + + if err := s.check(ctx, at); err != nil { + return err + } + + session.NameID = qualifiedName(session.NameID, s.entityID, session.Peer, s.role) + + if session.ID == "" || len(session.ID) > maximumSessionIDBytes || len(session.Index) > maximumSessionIndexBytes || len(session.AuthoritySession) > maximumAuthoritySessionBytes || !at.Before(session.ExpiresAt) { + return ErrState + } + + if err := s.lock(ctx); err != nil { + return err + } + defer s.mu.Unlock() + + s.prune(at) + + r, ok := s.relationships[relationshipKey(session.Principal, session.Peer)] + if !ok || r.terminated || !sameName(session.NameID, r.NameID) || session.ExpiresAt.After(r.ExpiresAt) || len(s.sessions) >= s.capacity { + return ErrState + } + + for _, mark := range s.logoutMarks { + if mark.peer == session.Peer && sameName(mark.name, r.NameID) && (len(mark.indices) == 0 || slices.Contains(mark.indices, session.Index)) { + return ErrState + } + } + + if _, ok := s.sessions[session.ID]; ok { + return ErrState + } + + s.sessions[session.ID] = session + s.expiry.Set(recordKey{sessionRecord, session.ID}, session.ExpiresAt) + + return nil +} + +// Active reports whether the application's session is currently usable. +func (s *MemoryStore) Active(ctx context.Context, id string, at time.Time) bool { + if s.check(ctx, at) != nil { + return false + } + + if err := s.lock(ctx); err != nil { + return false + } + + e, ok := s.sessions[id] + s.mu.Unlock() + + return ok && at.Before(e.ExpiresAt) +} + +// Lookup returns the current binding for an application account and peer. +func (s *MemoryStore) Lookup(ctx context.Context, principal, peer string, at time.Time) (Relationship, bool) { + if s.check(ctx, at) != nil { + return Relationship{}, false + } + + if err := s.lock(ctx); err != nil { + return Relationship{}, false + } + + r, ok := s.relationships[relationshipKey(principal, peer)] + s.mu.Unlock() + + return r, ok && !r.terminated && at.Before(r.ExpiresAt) +} + +// Logout invalidates the selected local sessions and, at an authority, sessions +// of other participants under the same AuthoritySession. No index selects all +// sessions for the issuer and NameID. Cross-account or cross-authority sessions +// are never selected. Replay marking and invalidation happen under one lock. +// clockSkew extends an explicit NotOnOrAfter through the assertion acceptance +// window. If NotOnOrAfter is absent, until supplies the fallback block deadline. +func (s *MemoryStore) Logout(ctx context.Context, verified *protocol.VerifiedLogoutRequest, authority bool, at, until time.Time, clockSkew time.Duration) (LogoutResult, error) { + if verified == nil { + return LogoutResult{}, ErrState + } + + if err := s.check(ctx, at); err != nil { + return LogoutResult{}, err + } + + if authority != (s.role == metadata.IdentityProvider) { + return LogoutResult{}, ErrState + } + + r := verified.Request() + r.NameID = qualifiedName(r.NameID, s.entityID, r.Issuer, s.role) + + deadline, err := logoutDeadline(at, r.NotOnOrAfter, until, clockSkew) + if err != nil { + return LogoutResult{}, err + } + + if err := s.lock(ctx); err != nil { + return LogoutResult{}, err + } + defer s.mu.Unlock() + + s.prune(at) + + if len(s.logoutMarks) >= s.capacity { + return LogoutResult{}, ErrState + } + + if err := s.consume(r.Issuer, r.ID, at, until); err != nil { + return LogoutResult{}, err + } + + markKey := r.Issuer + scopeSeparator + r.ID + s.logoutMarks[markKey] = logoutMark{peer: r.Issuer, name: r.NameID, indices: slices.Clone(r.SessionIndexes), until: deadline} + s.expiry.Set(recordKey{logoutRecord, markKey}, deadline) + + result := LogoutResult{Status: protocol.Status{Code: protocol.Success}} + + principal, ok := s.find(r.Issuer, r.NameID) + if !ok { + result.Status = unknown() + return result, nil + } + + groups := map[string]bool{} + selected := map[string]bool{} + + for id, e := range s.sessions { + if e.Principal == principal && e.Peer == r.Issuer && sameName(e.NameID, r.NameID) && (len(r.SessionIndexes) == 0 || slices.Contains(r.SessionIndexes, e.Index)) { + selected[id] = true + + if e.AuthoritySession != "" { + groups[e.AuthoritySession] = true + } + } + } + + participants := map[string]int{} + + for id, e := range s.sessions { + if !selected[id] && !(authority && e.Principal == principal && e.AuthoritySession != "" && groups[e.AuthoritySession]) { + continue + } + + result.SessionIDs = append(result.SessionIDs, id) + + if authority && e.Peer != r.Issuer { + s.addParticipant(&result, participants, principal, e) + } + + s.remove(recordKey{sessionRecord, id}) + } + + slices.Sort(result.SessionIDs) + slices.SortFunc(result.Participants, func(a, b Participant) int { + if a.Peer < b.Peer { + return -1 + } + + if a.Peer > b.Peer { + return 1 + } + + return 0 + }) + + for i := range result.Participants { + slices.Sort(result.Participants[i].SessionIndexes) + } + + return result, nil +} + +// Manage updates exactly the recognized current binding, including SPProvidedID. +// Incoming requests from an IdP change Value; those from an SP change its alias. +func (s *MemoryStore) Manage(ctx context.Context, verified *protocol.VerifiedManageNameIDRequest, idpSender bool, at, until time.Time) (protocol.Status, error) { + if verified == nil { + return protocol.Status{}, ErrState + } + + if err := s.check(ctx, at); err != nil { + return protocol.Status{}, err + } + + if idpSender != (s.role == metadata.ServiceProvider) { + return protocol.Status{}, ErrState + } + + r := verified.Request() + r.NameID = qualifiedName(r.NameID, s.entityID, r.Issuer, s.role) + + if err := s.lock(ctx); err != nil { + return protocol.Status{}, err + } + defer s.mu.Unlock() + + s.prune(at) + + if err := s.consume(r.Issuer, r.ID, at, until); err != nil { + return protocol.Status{}, err + } + + current, ok := s.managedName(r.Issuer, r.NameID) + if !ok { + return unknown(), nil + } + + return s.change(r.Issuer, current, r.NewID, r.Terminate, idpSender), nil +} + +// Start records an outgoing management/logout correlation without evicting live +// exchanges. Expiry is at most five minutes. IDs are unique for the whole store. +func (s *MemoryStore) Start(ctx context.Context, p Pending, at time.Time) error { + if err := s.check(ctx, at); err != nil { + return err + } + + p.NameID = qualifiedName(p.NameID, s.entityID, p.Peer, s.role) + if !s.validPending(p, at) { + return ErrState + } + + var deadline time.Time + + if p.Kind == protocol.LogoutResponse { + var err error + + deadline, err = logoutDeadline(at, p.NotOnOrAfter, at.Add(controlReplayLifetime), p.ClockSkew) + if err != nil { + return err + } + } + + if err := s.lock(ctx); err != nil { + return err + } + defer s.mu.Unlock() + + s.prune(at) + + if len(s.pending) >= s.capacity { + return ErrState + } + + if _, ok := s.pending[p.ID]; ok { + return ErrState + } + + if _, ok := s.find(p.Peer, p.NameID); !ok { + return ErrState + } + + if p.Kind == protocol.LogoutResponse { + if len(s.logoutMarks) >= s.capacity { + return ErrState + } + + markKey := "outgoing" + scopeSeparator + p.Peer + scopeSeparator + p.ID + if _, exists := s.logoutMarks[markKey]; exists { + return ErrState + } + + for _, existing := range s.pending { + if existing.Peer == p.Peer && sameName(existing.NameID, p.NameID) && existing.Terminate { + return ErrState + } + } + } + + p.SessionIndexes = slices.Clone(p.SessionIndexes) + + s.pending[p.ID] = p + s.expiry.Set(recordKey{pendingRecord, p.ID}, p.ExpiresAt) + + if p.Terminate { + // Sending termination is unilateral: after the message is released, + // neither a missing reply nor pending-state expiry permits a later + // LogoutRequest or new assertion under this identifier (E8/E55). + principal, _ := s.find(p.Peer, p.NameID) + k := relationshipKey(principal, p.Peer) + r := s.relationships[k] + r.terminated = true + s.setRelationship(k, r) + } + + if p.Kind == protocol.LogoutResponse { + markKey := "outgoing" + scopeSeparator + p.Peer + scopeSeparator + p.ID + s.logoutMarks[markKey] = logoutMark{peer: p.Peer, name: p.NameID, indices: slices.Clone(p.SessionIndexes), until: deadline} + s.expiry.Set(recordKey{logoutRecord, markKey}, deadline) + + principal, _ := s.find(p.Peer, p.NameID) + for id, e := range s.sessions { + if e.Peer == p.Peer && e.Principal == principal && sameName(e.NameID, p.NameID) && (len(p.SessionIndexes) == 0 || slices.Contains(p.SessionIndexes, e.Index)) { + s.remove(recordKey{sessionRecord, id}) + } + } + } + + return nil +} + +// Finish atomically correlates, consumes and applies a successful outgoing +// change. A replay, wrong issuer/kind or expired response cannot update state. +func (s *MemoryStore) Finish(ctx context.Context, response *protocol.VerifiedStatusResponse, at time.Time) (Pending, error) { + if response == nil { + return Pending{}, ErrState + } + + if err := s.check(ctx, at); err != nil { + return Pending{}, err + } + + if err := s.lock(ctx); err != nil { + return Pending{}, err + } + defer s.mu.Unlock() + + s.prune(at) + + p, ok := s.pending[response.InResponseTo()] + if !ok || p.Peer != response.Header().Issuer || p.Kind != response.Kind() { + return Pending{}, ErrState + } + + if response.Status().Code == protocol.Success && p.Kind == protocol.ManageNameIDResponse && !p.Terminate { + status := s.change(p.Peer, p.NameID, p.NewID, p.Terminate, p.LocalIsIDP) + if status.Code != protocol.Success { + return Pending{}, ErrState + } + } + + s.remove(recordKey{pendingRecord, p.ID}) + + return p, nil +} + +func (s *MemoryStore) check(ctx context.Context, at time.Time) error { + if ctx == nil || s == nil || s.capacity == 0 || at.IsZero() { + return ErrState + } + + return ctx.Err() +} + +// Recheck cancellation after waiting for another transaction and before any +// reads, pruning or mutations. The caller owns the lock only on success. +func (s *MemoryStore) lock(ctx context.Context) error { + s.mu.Lock() + if err := ctx.Err(); err != nil { + s.mu.Unlock() + return err + } + + return nil +} + +func (s *MemoryStore) prune(at time.Time) { + if s.expiry.AllExpired(at) { + clear(s.relationships) + clear(s.sessions) + clear(s.pending) + clear(s.seen) + clear(s.logoutMarks) + clear(s.names) + s.expiry.Reset() + + return + } + + for { + key, ok := s.expiry.PopExpired(at) + if !ok { + break + } + + s.remove(key) + } + + s.expiry.Trim() +} + +func (s *MemoryStore) setRelationship(key string, r Relationship) { + if old, ok := s.relationships[key]; ok { + delete(s.names, identifierKey(old.Peer, old.NameID)) + } + + s.relationships[key] = r + s.names[identifierKey(r.Peer, r.NameID)] = key + s.expiry.Set(recordKey{relationshipRecord, key}, r.ExpiresAt) +} + +func (s *MemoryStore) remove(key recordKey) { + s.expiry.Delete(key) + + switch key.kind { + case relationshipRecord: + r := s.relationships[key.id] + delete(s.names, identifierKey(r.Peer, r.NameID)) + delete(s.relationships, key.id) + case sessionRecord: + delete(s.sessions, key.id) + case seenRecord: + delete(s.seen, key.id) + case pendingRecord: + delete(s.pending, key.id) + case logoutRecord: + delete(s.logoutMarks, key.id) + } +} + +func (s *MemoryStore) consume(issuer, id string, at, until time.Time) error { + key := issuer + scopeSeparator + id + + if !at.Before(until) || until.Sub(at) > controlReplayLifetime { + return ErrState + } + + if _, ok := s.seen[key]; ok { + return ErrReplay + } + + if len(s.seen) >= s.capacity { + return ErrState + } + + s.seen[key] = until + s.expiry.Set(recordKey{seenRecord, key}, until) + + return nil +} + +func (s *MemoryStore) find(peer string, name protocol.NameIdentifier) (string, bool) { + key, ok := s.names[identifierKey(peer, name)] + + r := s.relationships[key] + if !ok || r.terminated { + return "", false + } + + return r.Principal, true +} + +func (s *MemoryStore) managedName(peer string, name protocol.NameIdentifier) (protocol.NameIdentifier, bool) { + for _, r := range s.relationships { + if r.terminated || r.Peer != peer { + continue + } + + candidate := name + if candidate.Format == protocol.NameIDPersistent { + // Core 3.6.3 permits a qualifier to be omitted when its value + // would be the containing message's authenticated Issuer. + if candidate.NameQualifier == "" && r.NameID.NameQualifier == peer { + candidate.NameQualifier = peer + } + + if candidate.SPNameQualifier == "" && r.NameID.SPNameQualifier == peer { + candidate.SPNameQualifier = peer + } + } + + if sameName(r.NameID, candidate) { + return r.NameID, true + } + } + + return protocol.NameIdentifier{}, false +} + +func (s *MemoryStore) change(peer string, old protocol.NameIdentifier, value string, terminate, idpSender bool) protocol.Status { + principal, ok := s.find(peer, old) + if !ok { + return unknown() + } + + k := relationshipKey(principal, peer) + + r := s.relationships[k] + if terminate { + r.terminated = true + s.setRelationship(k, r) + } else { + if idpSender { + r.NameID.Value = value + } else { + r.NameID.SPProvidedID = value + } + + if r.NameID.Validate() != nil { + return protocol.Status{Code: protocol.Requester} + } + + if other, exists := s.names[identifierKey(peer, r.NameID)]; exists && other != k { + return protocol.Status{Code: protocol.Requester} + } + + s.setRelationship(k, r) + + for id, e := range s.sessions { + if e.Peer == peer && e.Principal == principal && sameName(e.NameID, old) { + e.NameID = r.NameID + s.sessions[id] = e + } + } + + for id, mark := range s.logoutMarks { + if mark.peer == peer && sameName(mark.name, old) { + mark.name = r.NameID + s.logoutMarks[id] = mark + } + } + } + + return protocol.Status{Code: protocol.Success} +} + +// Withdraw atomically invalidates an application-selected authority session and +// returns all affected peers. It does not select other authority sessions. +func (s *MemoryStore) Withdraw(ctx context.Context, principal, group string, at time.Time) (LogoutResult, error) { + if err := s.check(ctx, at); err != nil { + return LogoutResult{}, err + } + + if s.role != metadata.IdentityProvider { + return LogoutResult{}, ErrState + } + + if principal == "" || group == "" { + return LogoutResult{}, ErrState + } + + if err := s.lock(ctx); err != nil { + return LogoutResult{}, err + } + defer s.mu.Unlock() + + s.prune(at) + + r := LogoutResult{Status: protocol.Status{Code: protocol.Success}} + peers := map[string]int{} + + for id, e := range s.sessions { + if e.Principal != principal || e.AuthoritySession != group { + continue + } + + r.SessionIDs = append(r.SessionIDs, id) + s.remove(recordKey{sessionRecord, id}) + + s.addParticipant(&r, peers, principal, e) + } + + return r, nil +} + +func relationshipKey(principal, peer string) string { return principal + scopeSeparator + peer } +func sameName(a, b protocol.NameIdentifier) bool { + if a.Format == "" { + a.Format = protocol.NameIDUnspecified + } + + if b.Format == "" { + b.Format = protocol.NameIDUnspecified + } + + return a == b +} + +func qualifiedName(n protocol.NameIdentifier, local, peer string, role metadata.RoleType) protocol.NameIdentifier { + if n.Format != protocol.NameIDPersistent { + return n + } + + idp, sp := local, peer + if role == metadata.ServiceProvider { + idp, sp = peer, local + } + + if n.NameQualifier == "" { + n.NameQualifier = idp + } + + if n.SPNameQualifier == "" { + n.SPNameQualifier = sp + } + + return n +} + +func validName(n protocol.NameIdentifier) bool { + return n.Validate() == nil +} + +func unknown() protocol.Status { + return protocol.Status{Code: protocol.Requester, Subcodes: []string{protocol.UnknownPrincipal}} +} + +func (s *MemoryStore) addParticipant(result *LogoutResult, positions map[string]int, principal string, e Session) { + rel, ok := s.relationships[relationshipKey(principal, e.Peer)] + if !ok || rel.terminated || !sameName(rel.NameID, e.NameID) { + result.Status = protocol.Status{Code: protocol.Success, Subcodes: []string{protocol.PartialLogout}} + return + } + + for _, p := range s.pending { + if p.Terminate && p.Peer == e.Peer && sameName(p.NameID, rel.NameID) { + result.Status = protocol.Status{Code: protocol.Success, Subcodes: []string{protocol.PartialLogout}} + return + } + } + + pos, exists := positions[e.Peer] + if !exists { + pos = len(result.Participants) + positions[e.Peer] = pos + result.Participants = append(result.Participants, Participant{Peer: e.Peer, NameID: rel.NameID}) + } + + if e.Index != "" && !slices.Contains(result.Participants[pos].SessionIndexes, e.Index) { + result.Participants[pos].SessionIndexes = append(result.Participants[pos].SessionIndexes, e.Index) + } +} + +func (s *MemoryStore) validPending(p Pending, at time.Time) bool { + const ( + maximumNewIDBytes = 4096 + maximumPendingSessionIndexes = 128 + ) + + if p.Kind == protocol.LogoutResponse && (p.Terminate || p.NewID != "") { + return false + } + + if len(p.SessionIndexes) > maximumPendingSessionIndexes || p.Kind == protocol.LogoutResponse && !p.LocalIsIDP && len(p.SessionIndexes) == 0 { + return false + } + + if p.LocalIsIDP != (s.role == metadata.IdentityProvider) || !profile.ID(p.ID) || !profile.URI(p.Peer) || !validName(p.NameID) || !at.Before(p.ExpiresAt) || p.ExpiresAt.Sub(at) > pendingRequestLifetime || p.Kind != protocol.ManageNameIDResponse && p.Kind != protocol.LogoutResponse { + return false + } + + return p.Kind != protocol.ManageNameIDResponse || p.Terminate != (p.NewID != "") && len(p.NewID) <= maximumNewIDBytes && p.NotOnOrAfter.IsZero() && p.ClockSkew == 0 +} + +func logoutDeadline(at, notOnOrAfter, fallback time.Time, clockSkew time.Duration) (time.Time, error) { + const maximumClosedSessionLifetime = 7 * 24 * time.Hour + + if protocol.ValidatePolicy(protocol.Limits{ClockSkew: clockSkew}) != nil || notOnOrAfter.After(at.Add(maximumClosedSessionLifetime)) { + return time.Time{}, ErrState + } + + deadline := fallback + if !notOnOrAfter.IsZero() { + deadline = notOnOrAfter.Add(clockSkew) + } + + if !at.Before(deadline) { + return time.Time{}, ErrState + } + + return deadline, nil +} diff --git a/saml/session/session_test.go b/saml/session/session_test.go new file mode 100644 index 0000000..1f181d8 --- /dev/null +++ b/saml/session/session_test.go @@ -0,0 +1,301 @@ +package session_test + +import ( + "context" + "crypto" + "errors" + "sync" + "sync/atomic" + "testing" + "time" + + "github.com/skarm/xmlsec/internal/testutil/testcert" + "github.com/skarm/xmlsec/saml/metadata" + "github.com/skarm/xmlsec/saml/protocol" + "github.com/skarm/xmlsec/saml/session" +) + +const ( + spID = "https://sp.test/entity" + idpID = "https://idp.test/entity" + slo = "https://sp.test/slo" + mgmt = "https://idp.test/manage" +) + +func name(peer string) protocol.NameIdentifier { + return protocol.NameIdentifier{Value: "alice", Format: protocol.NameIDPersistent, NameQualifier: idpID, SPNameQualifier: peer, SPProvidedID: "original-alias"} +} + +func register(t *testing.T, store *session.MemoryStore, peer, principal, group, index, id string) { + t.Helper() + at := testcert.Now() + if _, ok := store.Lookup(t.Context(), principal, peer, at); !ok { + n := name(peer) + if peer == idpID { + n = name(spID) + } + if principal != "local-account" { + n.Value = principal + } + if err := store.Register(t.Context(), session.Relationship{Principal: principal, Peer: peer, NameID: n, ExpiresAt: at.Add(time.Hour)}, at); err != nil { + t.Fatal(err) + } + } + rel, _ := store.Lookup(t.Context(), principal, peer, at) + if err := store.Add(t.Context(), session.Session{ID: id, Principal: principal, Peer: peer, NameID: rel.NameID, AuthoritySession: group, Index: index, ExpiresAt: at.Add(30 * time.Minute)}, at); err != nil { + t.Fatal(err) + } +} + +func logoutRequest(t *testing.T, key protocol.SigningKey, issuer, destination, id string, n protocol.NameIdentifier, indices ...string) *protocol.VerifiedLogoutRequest { + t.Helper() + at := testcert.Now() + d, err := protocol.SignLogoutRequest(t.Context(), protocol.LogoutRequest{ID: id, Issuer: issuer, Destination: destination, IssueInstant: at, NameID: n, SessionIndexes: indices, NotOnOrAfter: at.Add(10 * time.Minute)}, key) + if err != nil { + t.Fatal(err) + } + r, err := protocol.VerifyLogoutRequest(t.Context(), d, []crypto.PublicKey{key.Key.Public()}, protocol.ExchangeOptions{Issuer: issuer, Destination: destination, At: at}) + if err != nil { + t.Fatal(err) + } + return r +} + +func managementRequest(t *testing.T, key protocol.SigningKey, id string, n protocol.NameIdentifier, value string, terminate bool) *protocol.VerifiedManageNameIDRequest { + t.Helper() + at := testcert.Now() + d, err := protocol.SignManageNameIDRequest(t.Context(), protocol.ManageNameIDRequest{ID: id, Issuer: spID, Destination: mgmt, IssueInstant: at, NameID: n, NewID: value, Terminate: terminate}, key) + if err != nil { + t.Fatal(err) + } + r, err := protocol.VerifyManageNameIDRequest(t.Context(), d, []crypto.PublicKey{key.Key.Public()}, protocol.ExchangeOptions{Issuer: spID, Destination: mgmt, At: at}) + if err != nil { + t.Fatal(err) + } + return r +} + +// Core 3.7.3.1 requires logout to cover valid assertions arriving later, while +// preserving different SessionIndex values and unrelated principals. +func TestParticipantLogoutAndLateAssertions(t *testing.T) { + c := testcert.New(t) + key := protocol.SigningKey{Key: c.LeafKey, CertificateDER: c.Leaf.Raw} + at := testcert.Now() + store, err := session.NewMemoryStore(session.Config{EntityID: spID, Role: metadata.ServiceProvider, Capacity: 100}) + if err != nil { + t.Fatal(err) + } + register(t, store, idpID, "local-account", "authority", "index-a", "session-a") + register(t, store, idpID, "local-account", "authority", "index-b", "session-b") + register(t, store, idpID, "bob", "other", "index-a", "bob-session") + n := name(spID) + r := logoutRequest(t, key, idpID, slo, "_logout", n, "index-a") + service := session.Service{EntityID: spID, Endpoint: slo, Role: metadata.ServiceProvider, Store: store, Signing: key} + if _, _, err := service.Logout(t.Context(), r, "https://idp.test/slo", at.Add(6*time.Minute)); err == nil { + t.Fatal("held verified request bypassed freshness") + } + d, result, err := service.Logout(t.Context(), r, "https://idp.test/slo", at) + if err != nil || d == nil || len(result.SessionIDs) != 1 || result.SessionIDs[0] != "session-a" { + t.Fatalf("logout: %+v %v", result, err) + } + if store.Active(t.Context(), "session-a", at) || !store.Active(t.Context(), "session-b", at) || !store.Active(t.Context(), "bob-session", at) { + t.Fatal("wrong session scope") + } + late := session.Session{ID: "late", Principal: "local-account", Peer: idpID, NameID: n, Index: "index-a", ExpiresAt: at.Add(30 * time.Minute)} + if err := store.Add(t.Context(), late, at.Add(time.Minute)); err == nil { + t.Fatal("late assertion resurrected logged-out index") + } + if _, _, err := service.Logout(t.Context(), r, "https://idp.test/slo", at); !errors.Is(err, session.ErrReplay) { + t.Fatalf("replay: %v", err) + } + if err := store.Add(t.Context(), late, at.Add(10*time.Minute)); err != nil { + t.Fatalf("expired logout still blocks session: %v", err) + } + all := logoutRequest(t, key, idpID, slo, "_all", n) + if _, _, err := service.Logout(t.Context(), all, "https://idp.test/slo", at); err != nil { + t.Fatal(err) + } + if store.Active(t.Context(), "session-b", at) { + t.Fatal("omitted indices did not select all") + } +} + +func TestAuthorityFanoutPartialAndInitiation(t *testing.T) { + c := testcert.New(t) + key := protocol.SigningKey{Key: c.LeafKey, CertificateDER: c.Leaf.Raw} + at := testcert.Now() + store, err := session.NewMemoryStore(session.Config{EntityID: idpID, Role: metadata.IdentityProvider, Capacity: 100}) + if err != nil { + t.Fatal(err) + } + register(t, store, spID, "local-account", "group-1", "one", "session-1") + register(t, store, "https://sp2.test/entity", "local-account", "group-1", "two", "session-2") + register(t, store, "https://sp3.test/entity", "local-account", "group-2", "three", "session-3") + var notified []string + service := session.Service{EntityID: idpID, Endpoint: mgmt, Role: metadata.IdentityProvider, Store: store, Signing: key, Notify: func(_ context.Context, p session.Participant) (protocol.Status, error) { + notified = append(notified, p.Peer) + return protocol.Status{}, errors.New("unreachable") + }} + r := logoutRequest(t, key, spID, mgmt, "_fanout", name(spID), "one") + d, result, err := service.Logout(t.Context(), r, "https://sp.test/slo", at) + if err != nil || len(notified) != 1 || notified[0] != "https://sp2.test/entity" || len(result.Status.Subcodes) != 1 || result.Status.Subcodes[0] != protocol.PartialLogout { + t.Fatalf("fanout: %+v %v", result, err) + } + if store.Active(t.Context(), "session-1", at) || store.Active(t.Context(), "session-2", at) || !store.Active(t.Context(), "session-3", at) { + t.Fatal("authority session isolation") + } + v, err := protocol.VerifyStatusResponse(t.Context(), d, []crypto.PublicKey{c.LeafKey.Public()}, protocol.ExchangeOptions{Issuer: idpID, Destination: "https://sp.test/slo", ExpectedRequestID: "_fanout", At: at}) + if err != nil || v.Status().Code != protocol.Success || v.Status().Subcodes[0] != protocol.PartialLogout { + t.Fatal("partial logout status mapping") + } + result, err = service.InitiateLogout(t.Context(), "local-account", "group-2", at) + if err != nil || store.Active(t.Context(), "session-3", at) || len(result.Participants) != 1 { + t.Fatal("IdP-initiated logout") + } +} + +// Errata E8/E12/E55: an SP changes its alias, retains primary identity and active +// sessions, and termination removes the binding without forcing SLO. +func TestManageAliasTerminationAndAtomicReplay(t *testing.T) { + c := testcert.New(t) + key := protocol.SigningKey{Key: c.LeafKey, CertificateDER: c.Leaf.Raw} + at := testcert.Now() + store, err := session.NewMemoryStore(session.Config{EntityID: idpID, Role: metadata.IdentityProvider, Capacity: 100}) + if err != nil { + t.Fatal(err) + } + register(t, store, spID, "local-account", "group", "index", "active") + r := managementRequest(t, key, "_alias", name(spID), "new-alias", false) + var applied atomic.Int32 + var replayed atomic.Int32 + var wg sync.WaitGroup + for range 24 { + wg.Go(func() { + status, err := store.Manage(t.Context(), r, false, at, at.Add(time.Hour)) + if err == nil && status.Code == protocol.Success { + applied.Add(1) + } else if errors.Is(err, session.ErrReplay) { + replayed.Add(1) + } else { + t.Errorf("unexpected result: %+v %v", status, err) + } + }) + } + wg.Wait() + if applied.Load() != 1 || replayed.Load() != 23 { + t.Fatal("non-atomic NameID/replay transaction") + } + rel, ok := store.Lookup(t.Context(), "local-account", spID, at) + if !ok || rel.NameID.Value != "alice" || rel.NameID.SPProvidedID != "new-alias" || !store.Active(t.Context(), "active", at) { + t.Fatal("SPProvidedID semantics") + } + stale := managementRequest(t, key, "_stale", name(spID), "bad", false) + status, err := store.Manage(t.Context(), stale, false, at, at.Add(time.Hour)) + if err != nil || status.Code != protocol.Requester || status.Subcodes[0] != protocol.UnknownPrincipal { + t.Fatal("stale identifier must fail") + } + termination := managementRequest(t, key, "_terminate", rel.NameID, "", true) + status, err = store.Manage(t.Context(), termination, false, at, at.Add(time.Hour)) + if err != nil || status.Code != protocol.Success { + t.Fatal(err) + } + if _, ok := store.Lookup(t.Context(), "local-account", spID, at); ok || !store.Active(t.Context(), "active", at) { + t.Fatal("termination must remove binding and preserve active sessions") + } +} + +func TestLogoutMarkersSurviveIdentifierChange(t *testing.T) { + at := testcert.Now() + c := testcert.New(t) + key := protocol.SigningKey{Key: c.LeafKey, CertificateDER: c.Leaf.Raw} + store, _ := session.NewMemoryStore(session.Config{EntityID: spID, Role: metadata.ServiceProvider, Capacity: 100}) + register(t, store, idpID, "local-account", "group", "index-a", "session-a") + old := name(spID) + r := logoutRequest(t, key, idpID, slo, "_before_rename", old, "index-a") + if _, err := store.Logout(t.Context(), r, false, at, at.Add(time.Hour), 0); err != nil { + t.Fatal(err) + } + d, err := protocol.SignManageNameIDRequest(t.Context(), protocol.ManageNameIDRequest{ID: "_rename", Issuer: idpID, Destination: slo, IssueInstant: at, NameID: old, NewID: "new-primary"}, key) + if err != nil { + t.Fatal(err) + } + change, err := protocol.VerifyManageNameIDRequest(t.Context(), d, []crypto.PublicKey{key.Key.Public()}, protocol.ExchangeOptions{Issuer: idpID, Destination: slo, At: at}) + if err != nil { + t.Fatal(err) + } + status, err := store.Manage(t.Context(), change, true, at, at.Add(time.Hour)) + if err != nil || status.Code != protocol.Success { + t.Fatal(err) + } + late := session.Session{ID: "late", Principal: "local-account", Peer: idpID, NameID: old, Index: "index-a", ExpiresAt: at.Add(time.Minute)} + if err := store.Add(t.Context(), late, at); err == nil { + t.Fatal("stale signed identifier accepted after rename") + } + current, _ := store.Lookup(t.Context(), "local-account", idpID, at) + late.NameID = current.NameID + if err := store.Add(t.Context(), late, at); err == nil { + t.Fatal("rename bypassed a live logout marker") + } + late.Index = "fresh-index" + if err := store.Add(t.Context(), late, at); err != nil { + t.Fatalf("unrelated fresh session blocked: %v", err) + } +} + +func TestTerminatedBindingDoesNotWidenLogout(t *testing.T) { + at := testcert.Now() + c := testcert.New(t) + key := protocol.SigningKey{Key: c.LeafKey, CertificateDER: c.Leaf.Raw} + store, _ := session.NewMemoryStore(session.Config{EntityID: spID, Role: metadata.ServiceProvider, Capacity: 100}) + register(t, store, idpID, "local-account", "old-group", "shared-index", "old-session") + old := name(spID) + d, err := protocol.SignManageNameIDRequest(t.Context(), protocol.ManageNameIDRequest{ID: "_end_old", Issuer: idpID, Destination: slo, IssueInstant: at, NameID: old, Terminate: true}, key) + if err != nil { + t.Fatal(err) + } + r, err := protocol.VerifyManageNameIDRequest(t.Context(), d, []crypto.PublicKey{key.Key.Public()}, protocol.ExchangeOptions{Issuer: idpID, Destination: slo, At: at}) + if err != nil { + t.Fatal(err) + } + if _, err := store.Manage(t.Context(), r, true, at, at.Add(time.Hour)); err != nil { + t.Fatal(err) + } + current := old + current.Value = "second-primary" + if err := store.Register(t.Context(), session.Relationship{Principal: "local-account", Peer: idpID, NameID: current, ExpiresAt: at.Add(time.Hour)}, at); err != nil { + t.Fatal(err) + } + if err := store.Add(t.Context(), session.Session{ID: "new-session", Principal: "local-account", Peer: idpID, NameID: current, AuthoritySession: "new-group", Index: "shared-index", ExpiresAt: at.Add(time.Minute)}, at); err != nil { + t.Fatal(err) + } + logout := logoutRequest(t, key, idpID, slo, "_end_new", current) + if _, err := store.Logout(t.Context(), logout, false, at, at.Add(time.Hour), 0); err != nil { + t.Fatal(err) + } + if !store.Active(t.Context(), "old-session", at) || store.Active(t.Context(), "new-session", at) { + t.Fatal("logout crossed a terminated identifier relationship") + } +} + +func TestPersistentImplicitQualifiersAndStoreOwner(t *testing.T) { + at := testcert.Now() + c := testcert.New(t) + key := protocol.SigningKey{Key: c.LeafKey, CertificateDER: c.Leaf.Raw} + store, _ := session.NewMemoryStore(session.Config{EntityID: spID, Role: metadata.ServiceProvider, Capacity: 100}) + minimal := protocol.NameIdentifier{Value: "opaque", Format: protocol.NameIDPersistent} + if err := store.Register(t.Context(), session.Relationship{Principal: "account", Peer: idpID, NameID: minimal, ExpiresAt: at.Add(time.Hour)}, at); err != nil { + t.Fatal(err) + } + if err := store.Add(t.Context(), session.Session{ID: "session", Principal: "account", Peer: idpID, NameID: minimal, Index: "index", ExpiresAt: at.Add(time.Minute)}, at); err != nil { + t.Fatal(err) + } + request := logoutRequest(t, key, idpID, slo, "_implicit", minimal, "index") + wrong := session.Service{EntityID: "https://other-sp.test/entity", Role: metadata.ServiceProvider, Endpoint: slo, Store: store, Signing: key} + if _, _, err := wrong.Logout(t.Context(), request, "https://idp.test/return", at); err == nil || !store.Active(t.Context(), "session", at) { + t.Fatal("cross-provider store reuse") + } + service := session.Service{EntityID: spID, Role: metadata.ServiceProvider, Endpoint: slo, Store: store, Signing: key} + if _, _, err := service.Logout(t.Context(), request, "https://idp.test/return", at); err != nil || store.Active(t.Context(), "session", at) { + t.Fatalf("implicit qualifiers: %v", err) + } +} diff --git a/saml/session/soap.go b/saml/session/soap.go new file mode 100644 index 0000000..924fa26 --- /dev/null +++ b/saml/session/soap.go @@ -0,0 +1,222 @@ +package session + +import ( + "context" + "time" + + "github.com/skarm/xmlsec/internal/lexicon" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/saml/binding" + "github.com/skarm/xmlsec/saml/internal/profile" + "github.com/skarm/xmlsec/saml/metadata" + "github.com/skarm/xmlsec/saml/protocol" +) + +// BackChannel performs authenticated SLO and NameID SOAP exchanges using current +// trusted metadata. It implements the notification operation used by Service. +// Metadata and signing providers must support concurrent operations. +type BackChannel struct { + // EntityID identifies the local provider making the request. + EntityID string + // Role selects the local provider role used to resolve peer metadata. + Role metadata.RoleType + // Metadata supplies current authenticated peer endpoints and keys. + Metadata metadata.Resolver + // Signing supplies the request signing key and policy. + Signing protocol.SigningKey + // Options supplies response verification and freshness policy. + Options protocol.ExchangeOptions + // SOAP supplies the HTTP transport and SOAP processing limits. + SOAP binding.SOAPClient +} + +// Logout sends an authority-initiated request to one participant and validates +// the signed, correlated response. NotOnOrAfter is always present. Lifetime is +// the configured maximum accepted assertion lifetime, ensuring that delayed +// assertions cannot recreate the logged-out session during their validity. +func (c BackChannel) Logout(ctx context.Context, participant Participant, at time.Time) (protocol.Status, error) { + endpoint, _, _, err := c.endpoints(ctx, participant.Peer, metadata.SingleLogoutService, at) + if err != nil { + return protocol.Status{}, err + } + + _, lifetime, skew, err := protocol.AcceptanceWindow(c.Options.Limits) + if err != nil { + return protocol.Status{}, err + } + + id, err := profile.NewID() + if err != nil { + return protocol.Status{}, err + } + + d, err := protocol.SignLogoutRequest(ctx, protocol.LogoutRequest{ID: id, Issuer: c.EntityID, Destination: endpoint, IssueInstant: at, NameID: participant.NameID, SessionIndexes: participant.SessionIndexes, NotOnOrAfter: at.Add(lifetime + skew)}, c.Signing) + if err != nil { + return protocol.Status{}, err + } + + r, err := c.Exchange(ctx, participant.Peer, d, at) + if err != nil { + return protocol.Status{}, err + } + + return r.Status(), nil +} + +// Exchange transmits a locally signed control request whose destination already +// matches the peer's current SOAP endpoint. It validates the returned issuer, +// local endpoint, request ID and response kind. Service.Complete applies an +// outgoing managed identifier change only after this succeeds. +func (c BackChannel) Exchange(ctx context.Context, peer string, request *document.Document, at time.Time) (*protocol.VerifiedStatusResponse, error) { + const ( + xmlNameDestination = "Destination" + ) + + started := time.Now() + + if request == nil { + return nil, ErrState + } + + kind := request.Root().Name() + service, responseKind := "", "" + + if kind.Space != protocol.ProtocolNamespace { + return nil, ErrState + } + + switch kind.Local { + case lexicon.SAMLElementLogoutRequest: + service = metadata.SingleLogoutService + responseKind = protocol.LogoutResponse + case lexicon.SAMLElementManageNameIDRequest: + service = metadata.ManageNameIDService + responseKind = protocol.ManageNameIDResponse + default: + return nil, ErrState + } + + endpoint, returnEndpoint, role, err := c.endpoints(ctx, peer, service, at) + if err != nil { + return nil, err + } + + destination, _ := request.Root().Attribute(document.Name{Local: xmlNameDestination}) + + id, _ := request.Root().Attribute(document.Name{Local: lexicon.SAMLAttrID}) + if destination != endpoint || !profile.ID(id) { + return nil, ErrState + } + + envelope, err := protocol.SignSOAP(ctx, request, c.Signing) + if err != nil { + return nil, err + } + + m, err := c.SOAP.RoundTrip(ctx, endpoint, envelope) + if err != nil { + return nil, err + } + + d, err := m.Payload(ctx) + if err != nil { + return nil, err + } + + o := c.Options + o.Binding = metadata.SOAP + o.Issuer = peer + o.Destination = returnEndpoint + o.ExpectedRequestID = id + + o.At = at.Add(time.Since(started)) + if !o.At.Before(role.ExpiresAt()) { + return nil, metadata.ErrExpired + } + + r, err := protocol.VerifyStatusResponse(ctx, d, role.SigningKeys(), o) + if err != nil { + return nil, err + } + + if r.Kind() != responseKind { + return nil, ErrState + } + + return r, nil +} + +func (c BackChannel) endpoints(ctx context.Context, peer, service string, at time.Time) (string, string, *metadata.Role, error) { + if ctx == nil || c.Metadata == nil || at.IsZero() || !profile.URI(c.EntityID) || !profile.URI(peer) || c.Role != metadata.ServiceProvider && c.Role != metadata.IdentityProvider { + return "", "", nil, ErrState + } + + local, err := c.Metadata.Lookup(ctx, c.EntityID, at) + if err != nil { + return "", "", nil, err + } + + remote, err := c.Metadata.Lookup(ctx, peer, at) + if err != nil { + return "", "", nil, err + } + + if local == nil || remote == nil { + return "", "", nil, ErrState + } + + var own, other *metadata.Role + if c.Role == metadata.IdentityProvider { + own, err = local.Role(metadata.IdentityProvider, at) + if err == nil { + other, err = remote.Role(metadata.ServiceProvider, at) + } + } else { + own, err = local.Role(metadata.ServiceProvider, at) + if err == nil { + other, err = remote.Role(metadata.IdentityProvider, at) + } + } + + if err != nil { + return "", "", nil, err + } + + selectEndpoint := func(role *metadata.Role, response bool) (string, error) { + value := "" + + for _, e := range role.Endpoints(service) { + if e.Binding != metadata.SOAP { + continue + } + + if value != "" { + return "", ErrState + } + + value = e.Location + if response && e.ResponseLocation != "" { + value = e.ResponseLocation + } + } + + if value == "" { + return "", ErrState + } + + return value, nil + } + + endpoint, err := selectEndpoint(other, false) + if err != nil { + return "", "", nil, err + } + + back, err := selectEndpoint(own, true) + if err != nil { + return "", "", nil, err + } + + return endpoint, back, other, nil +} diff --git a/saml/session/soap_test.go b/saml/session/soap_test.go new file mode 100644 index 0000000..b13937e --- /dev/null +++ b/saml/session/soap_test.go @@ -0,0 +1,128 @@ +package session_test + +import ( + "bytes" + "crypto" + "net/http" + "net/http/httptest" + "testing" + "time" + + "github.com/skarm/xmlsec/internal/testutil/testcert" + "github.com/skarm/xmlsec/saml/binding" + "github.com/skarm/xmlsec/saml/metadata" + "github.com/skarm/xmlsec/saml/protocol" + "github.com/skarm/xmlsec/saml/session" +) + +func TestSOAPBackChannelManagementAndLogout(t *testing.T) { + at := testcert.Now() + c := testcert.New(t) + spKey := protocol.SigningKey{Key: c.LeafKey, CertificateDER: c.Leaf.Raw} + idpKey := protocol.SigningKey{Key: c.TSAKey, CertificateDER: c.TSA.Raw} + spStore, _ := session.NewMemoryStore(session.Config{EntityID: spID, Role: metadata.ServiceProvider, Capacity: 100}) + idpStore, _ := session.NewMemoryStore(session.Config{EntityID: idpID, Role: metadata.IdentityProvider, Capacity: 100}) + register(t, spStore, idpID, "local-account", "group", "index", "sp-session") + register(t, idpStore, spID, "local-account", "group", "index", "idp-session") + spService := session.Service{EntityID: spID, Role: metadata.ServiceProvider, Store: spStore, Signing: spKey, ResponseEndpoint: "https://sp.test/manage-result"} + idpService := session.Service{EntityID: idpID, Role: metadata.IdentityProvider, Store: idpStore, Signing: idpKey} + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + m, err := binding.ReadSOAPRequest(r.Context(), r, binding.SOAPOptions{}) + if err != nil { + t.Error(err) + return + } + d, err := m.Payload(r.Context()) + if err != nil { + t.Error(err) + return + } + if r.URL.Path == "/idp-manage" { + request, err := protocol.VerifyManageNameIDRequest(r.Context(), d, []crypto.PublicKey{c.LeafKey.Public()}, protocol.ExchangeOptions{Issuer: spID, Destination: idpService.Endpoint, At: at, Binding: metadata.SOAP}) + if err != nil { + t.Error(err) + return + } + response, err := idpService.Manage(r.Context(), request, "https://sp.test/manage-result", at) + if err != nil { + t.Error(err) + return + } + envelope, err := protocol.SignSOAP(r.Context(), response, idpKey) + if err != nil { + t.Error(err) + return + } + if err := binding.WriteSOAPResponse(r.Context(), w, envelope); err != nil { + t.Error(err) + } + return + } + service := spService + service.Endpoint = serverURL(r) + "/sp-slo" + request, err := protocol.VerifyLogoutRequest(r.Context(), d, []crypto.PublicKey{c.TSAKey.Public()}, protocol.ExchangeOptions{Issuer: idpID, Destination: service.Endpoint, At: at, Binding: metadata.SOAP}) + if err != nil { + t.Error(err) + return + } + response, _, err := service.Logout(r.Context(), request, "https://idp.test/slo-result", at) + if err != nil { + t.Error(err) + return + } + envelope, err := protocol.SignSOAP(r.Context(), response, spKey) + if err != nil { + t.Error(err) + return + } + if err := binding.WriteSOAPResponse(r.Context(), w, envelope); err != nil { + t.Error(err) + } + })) + defer server.Close() + spService.Endpoint = server.URL + "/sp-manage" + idpService.Endpoint = server.URL + "/idp-manage" + specs := []metadata.EntityDescriptor{ + {ID: "_sp", EntityID: spID, ValidUntil: at.Add(time.Hour), SP: &metadata.RoleDescriptor{Keys: []metadata.KeyDescriptor{{Use: "signing", CertificateDER: spKey.CertificateDER}}, Endpoints: map[string][]metadata.Endpoint{metadata.AssertionConsumerService: {{Binding: metadata.HTTPPost, Location: "https://sp.test/acs", Indexed: true}}, metadata.ManageNameIDService: {{Binding: metadata.SOAP, Location: spService.Endpoint, ResponseLocation: spService.ResponseEndpoint}}, metadata.SingleLogoutService: {{Binding: metadata.SOAP, Location: server.URL + "/sp-slo", ResponseLocation: "https://sp.test/slo-result"}}}}}, + {ID: "_idp", EntityID: idpID, ValidUntil: at.Add(time.Hour), IDP: &metadata.RoleDescriptor{Keys: []metadata.KeyDescriptor{{Use: "signing", CertificateDER: idpKey.CertificateDER}}, Endpoints: map[string][]metadata.Endpoint{metadata.SingleSignOnService: {{Binding: metadata.HTTPPost, Location: "https://idp.test/sso"}}, metadata.ManageNameIDService: {{Binding: metadata.SOAP, Location: idpService.Endpoint}}, metadata.SingleLogoutService: {{Binding: metadata.SOAP, Location: server.URL + "/idp-slo", ResponseLocation: "https://idp.test/slo-result"}}}}}, + } + var raw bytes.Buffer + raw.WriteString(``) + for _, spec := range specs { + d, err := metadata.Build(t.Context(), spec, at, metadata.Options{AllowHTTP: true}) + if err != nil { + t.Fatal(err) + } + b, _ := d.XML(t.Context()) + raw.Write(b) + } + raw.WriteString(``) + partners, err := metadata.ParseTrusted(t.Context(), raw.Bytes(), at, metadata.Options{AllowHTTP: true}) + if err != nil { + t.Fatal(err) + } + channel := session.BackChannel{EntityID: spID, Role: metadata.ServiceProvider, Metadata: partners, Signing: spKey, SOAP: binding.SOAPClient{AllowHTTP: true}} + old, _ := spStore.Lookup(t.Context(), "local-account", idpID, at) + request, err := spService.BeginManage(t.Context(), idpID, protocol.ManageNameIDRequest{ID: "_soap_alias", Issuer: spID, Destination: idpService.Endpoint, IssueInstant: at, NameID: old.NameID, NewID: "soap-alias"}, at) + if err != nil { + t.Fatal(err) + } + response, err := channel.Exchange(t.Context(), idpID, request, at) + if err != nil { + t.Fatal(err) + } + if _, err := spService.Complete(t.Context(), response, at); err != nil { + t.Fatal(err) + } + current, _ := idpStore.Lookup(t.Context(), "local-account", spID, at) + if current.NameID.Value != "alice" || current.NameID.SPProvidedID != "soap-alias" { + t.Fatal("SOAP alias management") + } + channel = session.BackChannel{EntityID: idpID, Role: metadata.IdentityProvider, Metadata: partners, Signing: idpKey, SOAP: binding.SOAPClient{AllowHTTP: true}} + status, err := channel.Logout(t.Context(), session.Participant{Peer: spID, NameID: current.NameID, SessionIndexes: []string{"index"}}, at) + if err != nil || status.Code != protocol.Success || spStore.Active(t.Context(), "sp-session", at) { + t.Fatalf("SOAP SLO: %v", err) + } +} + +func serverURL(r *http.Request) string { return "http://" + r.Host } diff --git a/saml/session/store_index_test.go b/saml/session/store_index_test.go new file mode 100644 index 0000000..0367ad2 --- /dev/null +++ b/saml/session/store_index_test.go @@ -0,0 +1,84 @@ +package session_test + +import ( + "strconv" + "sync" + "testing" + "time" + + "github.com/skarm/xmlsec/internal/testutil/testcert" + "github.com/skarm/xmlsec/saml/metadata" + "github.com/skarm/xmlsec/saml/protocol" + "github.com/skarm/xmlsec/saml/session" +) + +func TestStoreExpiryReleasesNamesAndCapacity(t *testing.T) { + at := testcert.Now() + store, err := session.NewMemoryStore(session.Config{EntityID: spID, Role: metadata.ServiceProvider, Capacity: 1}) + if err != nil { + t.Fatal(err) + } + r := session.Relationship{Principal: "alice", Peer: idpID, NameID: protocol.NameIdentifier{Value: "federated"}, ExpiresAt: at.Add(2 * time.Minute)} + if err := store.Register(t.Context(), r, at); err != nil { + t.Fatal(err) + } + duplicate := r + duplicate.Principal = "bob" + duplicate.NameID.Format = protocol.NameIDUnspecified + if err := store.Register(t.Context(), duplicate, at); err == nil { + t.Fatal("equivalent unspecified formats admitted the same identifier twice") + } + s := session.Session{ID: "session", Principal: r.Principal, Peer: r.Peer, NameID: r.NameID, ExpiresAt: at.Add(time.Minute)} + if err := store.Add(t.Context(), s, at); err != nil { + t.Fatal(err) + } + s.ExpiresAt = r.ExpiresAt + if err := store.Add(t.Context(), s, at.Add(time.Minute)); err != nil || !store.Active(t.Context(), s.ID, at.Add(time.Minute)) { + t.Fatal("expired session blocked reuse at its exact deadline", err) + } + duplicate.ExpiresAt = at.Add(3 * time.Minute) + if err := store.Register(t.Context(), duplicate, r.ExpiresAt); err != nil { + t.Fatal("expired relationship retained its identifier or capacity", err) + } + if _, ok := store.Lookup(t.Context(), r.Principal, r.Peer, r.ExpiresAt); ok || store.Active(t.Context(), s.ID, r.ExpiresAt) { + t.Fatal("expired relationship or session remained active") + } + if _, ok := store.Lookup(t.Context(), duplicate.Principal, duplicate.Peer, r.ExpiresAt); !ok { + t.Fatal("replacement relationship was lost") + } +} + +func TestStoreConcurrentPopulationAndExpiry(t *testing.T) { + at := testcert.Now() + store, err := session.NewMemoryStore(session.Config{EntityID: spID, Role: metadata.ServiceProvider, Capacity: 1024}) + if err != nil { + t.Fatal(err) + } + for round := range 2 { + now := at.Add(time.Duration(round) * time.Hour) + var wg sync.WaitGroup + for worker := range 8 { + wg.Go(func() { + for i := worker; i < 1024; i += 8 { + id := "user-" + strconv.Itoa(i) + r := session.Relationship{Principal: id, Peer: idpID, NameID: protocol.NameIdentifier{Value: id}, ExpiresAt: now.Add(time.Hour)} + if err := store.Register(t.Context(), r, now); err != nil { + t.Error(err) + return + } + if err := store.Add(t.Context(), session.Session{ID: id, Principal: id, Peer: r.Peer, NameID: r.NameID, ExpiresAt: now.Add(10 * time.Minute)}, now); err != nil { + t.Error(err) + return + } + } + }) + } + wg.Wait() + for i := range 1024 { + id := "user-" + strconv.Itoa(i) + if _, ok := store.Lookup(t.Context(), id, idpID, now); !ok || !store.Active(t.Context(), id, now) { + t.Fatal("concurrent insertion or prior expiry lost an active user", id) + } + } + } +} diff --git a/saml/sp/artifact_test.go b/saml/sp/artifact_test.go new file mode 100644 index 0000000..623ee25 --- /dev/null +++ b/saml/sp/artifact_test.go @@ -0,0 +1,147 @@ +package sp_test + +import ( + "bytes" + "crypto" + "net/http" + "net/http/httptest" + "testing" + "time" + + "github.com/skarm/xmlsec/c14n" + "github.com/skarm/xmlsec/internal/testutil/testcert" + "github.com/skarm/xmlsec/saml/artifact" + "github.com/skarm/xmlsec/saml/binding" + "github.com/skarm/xmlsec/saml/idp" + "github.com/skarm/xmlsec/saml/metadata" + "github.com/skarm/xmlsec/saml/protocol" + "github.com/skarm/xmlsec/saml/replay" + "github.com/skarm/xmlsec/saml/sp" + "github.com/skarm/xmlsec/saml/state" +) + +// Resolve both halves over real SOAP HTTP exchanges and keep the nested +// AuthnRequest/Response correlation separate from ArtifactResolve correlation. +func TestArtifactWebSSOBothRoles(t *testing.T) { + const spID = "https://sp.test/entity" + const idpID = "https://idp.test/entity" + const acs = "https://sp.test/acs" + const sso = "https://idp.test/sso" + at := testcert.Now() + c := testcert.New(t) + spKey := protocol.SigningKey{Key: c.LeafKey, CertificateDER: c.Leaf.Raw, Canonicalization: c14n.Canonical10} + idpKey := protocol.SigningKey{Key: c.TSAKey, CertificateDER: c.TSA.Raw, Canonicalization: c14n.Canonical10} + spStore, _ := artifact.NewMemoryStore(10) + idpStore, _ := artifact.NewMemoryStore(10) + spIssuer := artifact.Issuer{EntityID: spID, ReturnEndpoint: sso, Signing: spKey, Store: spStore} + idpIssuer := artifact.Issuer{EntityID: idpID, ReturnEndpoint: acs, Signing: idpKey, Store: idpStore} + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + issuer, peer, keys := idpIssuer, spID, []crypto.PublicKey{c.LeafKey.Public()} + if r.URL.Path == "/sp-ars" { + issuer, peer, keys = spIssuer, idpID, []crypto.PublicKey{c.TSAKey.Public()} + } + m, err := binding.ReadSOAPRequest(r.Context(), r, binding.SOAPOptions{}) + if err != nil { + t.Error(err) + return + } + d, err := m.Payload(r.Context()) + if err != nil { + t.Error(err) + return + } + request, err := protocol.VerifyArtifactResolve(r.Context(), d, keys, protocol.ExchangeOptions{Issuer: peer, Destination: issuer.Endpoint, At: at, Binding: metadata.SOAP}) + if err != nil { + t.Error(err) + return + } + response, err := issuer.Respond(r.Context(), request, at) + if err != nil { + t.Error(err) + return + } + envelope, err := protocol.SignSOAP(r.Context(), response, issuer.Signing) + if err != nil { + t.Error(err) + return + } + if err := binding.WriteSOAPResponse(r.Context(), w, envelope); err != nil { + t.Error(err) + } + })) + defer server.Close() + spIssuer.Endpoint = server.URL + "/sp-ars" + idpIssuer.Endpoint = server.URL + "/idp-ars" + specs := []metadata.EntityDescriptor{ + {ID: "_sp", EntityID: spID, ValidUntil: at.Add(time.Hour), SP: &metadata.RoleDescriptor{Keys: []metadata.KeyDescriptor{{Use: "signing", CertificateDER: spKey.CertificateDER}}, Endpoints: map[string][]metadata.Endpoint{metadata.AssertionConsumerService: {{Binding: metadata.HTTPArtifact, Location: acs, Indexed: true}}, metadata.ArtifactResolutionService: {{Binding: metadata.SOAP, Location: spIssuer.Endpoint, Indexed: true}}}}}, + {ID: "_idp", EntityID: idpID, ValidUntil: at.Add(time.Hour), IDP: &metadata.RoleDescriptor{Keys: []metadata.KeyDescriptor{{Use: "signing", CertificateDER: idpKey.CertificateDER}}, Endpoints: map[string][]metadata.Endpoint{metadata.SingleSignOnService: {{Binding: metadata.HTTPArtifact, Location: sso}}, metadata.ArtifactResolutionService: {{Binding: metadata.SOAP, Location: idpIssuer.Endpoint, Indexed: true}}}}}, + } + var raw bytes.Buffer + raw.WriteString(``) + for _, spec := range specs { + d, err := metadata.Build(t.Context(), spec, at, metadata.Options{AllowHTTP: true}) + if err != nil { + t.Fatal(err) + } + b, err := d.XML(t.Context()) + if err != nil { + t.Fatal(err) + } + raw.Write(b) + } + raw.WriteString(``) + partners, err := metadata.ParseTrusted(t.Context(), raw.Bytes(), at, metadata.Options{AllowHTTP: true}) + if err != nil { + t.Fatal(err) + } + requests, _ := state.NewMemoryStore(state.Config{}) + spReplay, _ := replay.NewMemoryStore(replay.Config{}) + idpReplay, _ := replay.NewMemoryStore(replay.Config{}) + service, err := sp.NewServiceProvider(sp.Config{EntityID: spID, Metadata: partners, Signing: spKey, Requests: requests, Replay: spReplay}) + if err != nil { + t.Fatal(err) + } + identity, err := idp.NewIdentityProvider(idp.Config{EntityID: idpID, Metadata: partners, Signing: idpKey, Replay: idpReplay}) + if err != nil { + t.Fatal(err) + } + browser := bytes.Repeat([]byte{71}, 32) + out, err := service.Begin(t.Context(), sp.BeginOptions{IDP: idpID, Binding: metadata.HTTPArtifact, ResponseBinding: metadata.HTTPArtifact, BrowserBinding: browser, At: at}) + if err != nil { + t.Fatal(err) + } + a, err := out.IssueArtifact(t.Context(), spIssuer, at, time.Minute) + if err != nil { + t.Fatal(err) + } + resolve := artifact.Resolver{EntityID: idpID, PeerEntityID: spID, ReturnEndpoint: sso, Signing: idpKey, Keys: []crypto.PublicKey{c.LeafKey.Public()}, Endpoints: []metadata.Endpoint{{Binding: metadata.SOAP, Location: spIssuer.Endpoint, Indexed: true}}, SOAP: binding.SOAPClient{AllowHTTP: true}} + requestDoc, err := resolve.Resolve(t.Context(), a, at) + if err != nil { + t.Fatal(err) + } + relay := out.RelayState() + request, err := identity.AcceptArtifact(t.Context(), requestDoc, &relay, spID, sso, at) + if err != nil { + t.Fatal(err) + } + issued, err := identity.Issue(t.Context(), request, idp.Authentication{NameID: "alice", AuthnInstant: at, AuthnContext: "urn:test:password", SessionIndex: "device"}, at) + if err != nil { + t.Fatal(err) + } + responseArtifact, err := issued.IssueArtifact(t.Context(), idpIssuer, at, time.Minute) + if err != nil { + t.Fatal(err) + } + resolve = artifact.Resolver{EntityID: spID, PeerEntityID: idpID, ReturnEndpoint: acs, Signing: spKey, Keys: []crypto.PublicKey{c.TSAKey.Public()}, Endpoints: []metadata.Endpoint{{Binding: metadata.SOAP, Location: idpIssuer.Endpoint, Indexed: true}}, SOAP: binding.SOAPClient{AllowHTTP: true}} + responseDoc, err := resolve.Resolve(t.Context(), responseArtifact, at) + if err != nil { + t.Fatal(err) + } + assertion, err := service.ConsumeArtifact(t.Context(), responseDoc, relay, sp.ConsumeOptions{At: at, BrowserBinding: browser}) + if err != nil || assertion.NameID() != "alice" { + t.Fatalf("artifact SSO: %v", err) + } + if _, err := service.ConsumeArtifact(t.Context(), responseDoc, relay, sp.ConsumeOptions{At: at, BrowserBinding: browser}); err == nil { + t.Fatal("nested response replay accepted") + } +} diff --git a/saml/sp/doc.go b/saml/sp/doc.go index 87dc913..1895942 100644 --- a/saml/sp/doc.go +++ b/saml/sp/doc.go @@ -7,8 +7,8 @@ // [NewServiceProvider] snapshots [Config] and requires application-owned metadata, signing, // request-state and replay adapters. [ServiceProvider.Begin] selects trusted IdP // and ACS endpoints, creates a signed AuthnRequest and stores its pending state. -// [Outbound] exposes the destination and binding-specific Redirect query or POST -// form for the application to send. +// [Outbound] exposes the destination, Redirect query, POST writers, artifact +// issuance or PAOS challenge for the application to send. // // The application supplies an unpredictable browser/session secret in // [BeginOptions.BrowserBinding] and presents the same secret when consuming the @@ -25,5 +25,10 @@ // Metadata resolvers and stores must honor context. In-memory state and replay // stores support one process; multiple service instances need shared storage with // atomic consumption. The package implements the documented solicited profile, -// not unsolicited login, logout or a general SAML server. +// not unsolicited login or a general SAML server. Begin also supports Artifact +// and bearer or X.509 holder-of-key ECP/PAOS. ConsumeArtifact and ConsumePAOS +// retain the same browser, metadata, assertion and replay checks. ConsumeECP +// additionally requires proof bound to the exact HoK SOAP snapshot. +// Logout and identifier management use +// github.com/skarm/xmlsec/saml/session with the application's session store. package sp diff --git a/saml/sp/ecp_hok_test.go b/saml/sp/ecp_hok_test.go new file mode 100644 index 0000000..259d6b9 --- /dev/null +++ b/saml/sp/ecp_hok_test.go @@ -0,0 +1,421 @@ +package sp_test + +import ( + "bytes" + "context" + "crypto" + "crypto/rand" + "crypto/rsa" + "crypto/tls" + "crypto/x509" + "encoding/base64" + "io" + "math/big" + "net/http" + "net/http/httptest" + "strconv" + "testing" + "time" + + "github.com/skarm/xmlsec/c14n" + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/testutil/testcert" + "github.com/skarm/xmlsec/saml/binding" + "github.com/skarm/xmlsec/saml/ecp" + "github.com/skarm/xmlsec/saml/idp" + "github.com/skarm/xmlsec/saml/metadata" + "github.com/skarm/xmlsec/saml/proof" + "github.com/skarm/xmlsec/saml/protocol" + "github.com/skarm/xmlsec/saml/replay" + "github.com/skarm/xmlsec/saml/sp" + "github.com/skarm/xmlsec/saml/state" +) + +const hokSP, hokIDP = "https://sp.test/entity", "https://idp.test/entity" + +func hokPartners(t *testing.T, c testcert.Chain, acs, sso, canonical string, encrypted, advertiseHoK bool) (*sp.ServiceProvider, *idp.IdentityProvider) { + t.Helper() + spRole := &metadata.RoleDescriptor{Keys: []metadata.KeyDescriptor{{Use: "signing", CertificateDER: c.Leaf.Raw}}, Endpoints: map[string][]metadata.Endpoint{metadata.AssertionConsumerService: {{Binding: metadata.PAOS, Location: acs, Indexed: true}}}} + idpEndpoint := metadata.Endpoint{Binding: metadata.SOAP, Location: sso} + if advertiseHoK { + spRole.Endpoints[metadata.AssertionConsumerService] = []metadata.Endpoint{{Binding: metadata.HoKSSO, ProtocolBinding: metadata.PAOS, Location: acs, Indexed: true, SupportedChannelBindings: []string{ecp.TLSServerEndPoint}}} + idpEndpoint = metadata.Endpoint{Binding: metadata.HoKSSO, ProtocolBinding: metadata.SOAP, Location: sso, SupportedChannelBindings: []string{ecp.TLSServerEndPoint}} + } + var decryption *protocol.DecryptionOptions + var encryption *protocol.EncryptionOptions + if encrypted { + key, err := rsa.GenerateKey(rand.Reader, 2048) + if err != nil { + t.Fatal(err) + } + template := *c.Leaf + template.KeyUsage = x509.KeyUsageKeyEncipherment + cert := testcert.Issue(t, &template, c.Root, &key.PublicKey, c.RootKey) + spRole.Keys = append(spRole.Keys, metadata.KeyDescriptor{Use: "encryption", CertificateDER: cert.Raw}) + decryption = &protocol.DecryptionOptions{Keys: []crypto.Decrypter{key}} + encryption = &protocol.EncryptionOptions{Targets: protocol.EncryptAssertion | protocol.EncryptNameID | protocol.EncryptAttributes} + } + var aggregate bytes.Buffer + aggregate.WriteString(``) + for _, spec := range []metadata.EntityDescriptor{ + {ID: "_sp", EntityID: hokSP, ValidUntil: testcert.Now().Add(time.Hour), SP: spRole}, + {ID: "_idp", EntityID: hokIDP, ValidUntil: testcert.Now().Add(time.Hour), IDP: &metadata.RoleDescriptor{Keys: []metadata.KeyDescriptor{{Use: "signing", CertificateDER: c.TSA.Raw}}, Endpoints: map[string][]metadata.Endpoint{metadata.SingleSignOnService: {idpEndpoint}}}}, + } { + d, err := metadata.Build(t.Context(), spec, testcert.Now(), metadata.Options{}) + if err != nil { + t.Fatal(err) + } + raw, err := d.XML(t.Context()) + if err != nil { + t.Fatal(err) + } + aggregate.Write(raw) + } + aggregate.WriteString(``) + partners, err := metadata.ParseTrusted(t.Context(), aggregate.Bytes(), testcert.Now(), metadata.Options{}) + if err != nil { + t.Fatal(err) + } + requests, err := state.NewMemoryStore(state.Config{}) + if err != nil { + t.Fatal(err) + } + spReplay, err := replay.NewMemoryStore(replay.Config{}) + if err != nil { + t.Fatal(err) + } + idpReplay, err := replay.NewMemoryStore(replay.Config{}) + if err != nil { + t.Fatal(err) + } + service, err := sp.NewServiceProvider(sp.Config{EntityID: hokSP, Metadata: partners, Signing: protocol.SigningKey{Key: c.LeafKey, CertificateDER: c.Leaf.Raw, Canonicalization: canonical}, Requests: requests, Replay: spReplay, Decryption: decryption}) + if err != nil { + t.Fatal(err) + } + identity, err := idp.NewIdentityProvider(idp.Config{EntityID: hokIDP, Metadata: partners, Signing: protocol.SigningKey{Key: c.TSAKey, CertificateDER: c.TSA.Raw, Canonicalization: canonical}, Replay: idpReplay, Encryption: encryption}) + if err != nil { + t.Fatal(err) + } + return service, identity +} + +type xmlProofMethods struct { + name string + sign func(context.Context, *document.Document, proof.Signing) (*document.Document, error) + verify func(context.Context, []byte, proof.Options) (*proof.Verified, error) + toIDP func(*ecp.Exchange, context.Context, proof.Signing, []protocol.ChannelBinding) (*document.Document, error) + finish func(*ecp.Exchange, context.Context, []byte, proof.Signing, binding.SOAPOptions) (*document.Document, error) +} + +func TestECPHoKXMLRoles(t *testing.T) { + for _, methods := range []xmlProofMethods{ + {"CS01-compatibility", proof.SignCS01CompatXML, ecp.VerifyCS01CompatXML, (*ecp.Exchange).ToIDPCS01CompatXML, (*ecp.Exchange).FinishCS01CompatXML}, + {"XMLDSig-enveloped", proof.SignEnvelopedXML, ecp.VerifyEnvelopedXML, (*ecp.Exchange).ToIDPEnvelopedXML, (*ecp.Exchange).FinishEnvelopedXML}, + } { + t.Run(methods.name, func(t *testing.T) { testECPHoKXMLRoles(t, methods) }) + } +} + +func TestHoKOnlyMetadataRejectsBearerBegin(t *testing.T) { + c := testcert.New(t) + service, _ := hokPartners(t, c, "https://sp.test/acs", "https://idp.test/sso", c14n.Exclusive, false, true) + if _, err := service.Begin(t.Context(), sp.BeginOptions{IDP: hokIDP, Binding: metadata.PAOS, At: testcert.Now(), BrowserBinding: bytes.Repeat([]byte{19}, 32)}); err == nil { + t.Fatal("bearer login selected HoK-only metadata") + } +} + +func testECPHoKXMLRoles(t *testing.T, methods xmlProofMethods) { + c := testcert.New(t) + client := proof.Signing{Key: c.ResponderKey, Certificate: c.Responder.Raw} + for _, canonical := range []string{c14n.Exclusive, c14n.Canonical10} { + for _, encrypted := range []bool{false, true} { + t.Run(canonical+"/encrypted="+strconv.FormatBool(encrypted), func(t *testing.T) { + const acs, sso = "https://sp.test/acs", "https://idp.test/sso" + service, identity := hokPartners(t, c, acs, sso, canonical, encrypted, encrypted) + at := testcert.Now() + browser := bytes.Repeat([]byte{19}, 32) + spChannel := protocol.ChannelBinding{Type: ecp.TLSServerEndPoint, Data: bytes.Repeat([]byte{1}, 32)} + idpChannel := protocol.ChannelBinding{Type: ecp.TLSServerEndPoint, Data: bytes.Repeat([]byte{2}, 32)} + out, err := service.Begin(t.Context(), sp.BeginOptions{IDP: hokIDP, Binding: metadata.PAOS, At: at, BrowserBinding: browser, HolderOfKey: true, ChannelBindings: []protocol.ChannelBinding{spChannel}}) + if err != nil { + t.Fatal(err) + } + challenge, err := out.PAOS() + if err != nil { + t.Fatal(err) + } + raw, _ := challenge.XML(t.Context()) + if _, err := ecp.ParseChallengeWithBindings(t.Context(), raw, "https://sp.test/resource", []protocol.ChannelBinding{spChannel}, binding.SOAPOptions{}); err == nil { + t.Fatal("HoK challenge silently downgraded") + } + x, err := ecp.ParseHoKChallenge(t.Context(), raw, "https://sp.test/resource", c.Responder.Raw, []protocol.ChannelBinding{spChannel}, binding.SOAPOptions{}) + if err != nil { + t.Fatal(err) + } + if encrypted { + plain, err := x.ToIDP(t.Context()) + if err != nil { + t.Fatal(err) + } + plainRaw, err := plain.XML(t.Context()) + if err != nil { + t.Fatal(err) + } + if _, err := identity.AcceptSOAP(t.Context(), plainRaw, hokSP, sso, at); err == nil { + t.Fatal("bearer receiver accepted a HoK-only metadata endpoint") + } + } + toIDP, err := methods.toIDP(x, t.Context(), client, []protocol.ChannelBinding{idpChannel}) + if err != nil { + t.Fatal(err) + } + raw, _ = toIDP.XML(t.Context()) + p, err := methods.verify(t.Context(), raw, proof.Options{}) + if err != nil { + t.Fatal(err) + } + failed, err := identity.AcceptECP(t.Context(), p, hokSP, sso, at, []protocol.ChannelBinding{spChannel}) + if err == nil || failed == nil { + t.Fatal("client/IdP channel mismatch accepted") + } + auth := idp.Authentication{NameID: "alice", AuthnInstant: at, AuthnContext: "urn:test:certificate", HolderOfKeyCertificate: c.Responder.Raw, Attributes: []protocol.Attribute{{Name: "role", Values: []string{"reader"}}}} + if _, err := identity.Issue(t.Context(), failed, auth, at); err == nil { + t.Fatal("failed channel request issued assertion") + } + request, err := identity.AcceptECP(t.Context(), p, hokSP, sso, at, []protocol.ChannelBinding{idpChannel}) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(request.HolderOfKeyCertificate(), c.Responder.Raw) { + t.Fatal("wrong authentication key") + } + bad := auth + bad.HolderOfKeyCertificate = nil + if _, err := identity.Issue(t.Context(), request, bad, at); err == nil { + t.Fatal("missing principal/key association accepted") + } + bad.HolderOfKeyCertificate = c.Leaf.Raw + if _, err := identity.Issue(t.Context(), request, bad, at); err == nil { + t.Fatal("another principal key accepted") + } + issued, err := identity.Issue(t.Context(), request, auth, at) + if err != nil { + t.Fatal(err) + } + idpEnvelope, err := issued.ECP(t.Context()) + if err != nil { + t.Fatal(err) + } + raw, _ = idpEnvelope.XML(t.Context()) + if !encrypted { + data := hokConfirmationElements(idpEnvelope.Root()) + if len(data) != 2 { + t.Fatalf("confirmation data count: %d", len(data)) + } + var a, b bytes.Buffer + if err := c14n.WriteElement(t.Context(), &a, data[0], c14n.Options{Algorithm: c14n.Exclusive}); err != nil { + t.Fatal(err) + } + if err := c14n.WriteElement(t.Context(), &b, data[1], c14n.Options{Algorithm: c14n.Exclusive}); err != nil { + t.Fatal(err) + } + if !bytes.Equal(a.Bytes(), b.Bytes()) { + t.Fatal("ECP/header assertion confirmation data differ") + } + } + // A reissued certificate with the same key must not satisfy the + // X509Certificate matching form (HoK 2.5), even with valid proof. + template := *c.Responder + template.SerialNumber = big.NewInt(123) + reissued := testcert.Issue(t, &template, c.Root, c.ResponderKey.Public(), c.RootKey) + wrong := bytes.Replace(raw, []byte(base64.StdEncoding.EncodeToString(c.Responder.Raw)), []byte(base64.StdEncoding.EncodeToString(reissued.Raw)), 1) + if _, err := x.Finish(t.Context(), wrong, binding.SOAPOptions{}); err == nil { + t.Fatal("different certificate accepted by ECP") + } + returned, err := x.Finish(t.Context(), raw, binding.SOAPOptions{}) + if err != nil { + t.Fatal(err) + } + unsigned, _ := returned.XML(t.Context()) + if _, err := service.ConsumePAOS(t.Context(), unsigned, sp.ConsumeOptions{At: at, BrowserBinding: browser}); err == nil { + t.Fatal("HoK accepted without proof") + } + wrongProof, err := methods.sign(t.Context(), returned, proof.Signing{Key: c.ResponderKey, Certificate: reissued.Raw}) + if err != nil { + t.Fatal(err) + } + wrongRaw, _ := wrongProof.XML(t.Context()) + wrongP, err := methods.verify(t.Context(), wrongRaw, proof.Options{}) + if err != nil { + t.Fatal(err) + } + if _, err := service.ConsumeECP(t.Context(), wrongP, sp.ConsumeOptions{At: at, BrowserBinding: browser}); err == nil { + t.Fatal("SP matched certificate by public key only") + } + final, err := methods.finish(x, t.Context(), raw, client, binding.SOAPOptions{}) + if err != nil { + t.Fatal(err) + } + finalRaw, _ := final.XML(t.Context()) + verified, err := methods.verify(t.Context(), finalRaw, proof.Options{}) + if err != nil { + t.Fatal(err) + } + assertion, err := service.ConsumeECP(t.Context(), verified, sp.ConsumeOptions{At: at, BrowserBinding: browser}) + if err != nil { + t.Fatal(err) + } + if assertion.NameID() != "alice" || assertion.SubjectConfirmationMethod() != protocol.HolderOfKey || !bytes.Equal(assertion.HolderOfKeyCertificate(), c.Responder.Raw) { + t.Fatal("wrong authenticated subject") + } + if _, err := service.ConsumeECP(t.Context(), verified, sp.ConsumeOptions{At: at, BrowserBinding: browser}); err == nil { + t.Fatal("HoK response replay accepted") + } + }) + } + } +} + +func hokConfirmationElements(e document.Element) []document.Element { + var out []document.Element + if e.Name() == (document.Name{Space: protocol.AssertionNamespace, Local: "SubjectConfirmationData"}) { + out = append(out, e) + } + for _, child := range e.Children() { + out = append(out, hokConfirmationElements(child)...) + } + return out +} + +// Both ECP hops use real mutually authenticated TLS; no synthetic TLS state or +// forwarded certificate headers are used to establish proof. HTTP Basic at the +// IdP independently maps the demonstrated certificate to the authenticated user. +func TestECPHoKMutualTLSAndBasic(t *testing.T) { + c := testcert.New(t) + var service *sp.ServiceProvider + var identity *idp.IdentityProvider + at := testcert.Now() + browser := bytes.Repeat([]byte{39}, 32) + spServer := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + p, err := ecp.VerifyTLSRequest(r.Context(), r, proof.Options{}) + if err != nil { + t.Error(err) + http.Error(w, "proof", 400) + return + } + assertion, err := service.ConsumeECP(r.Context(), p, sp.ConsumeOptions{At: at, BrowserBinding: browser}) + if err != nil || assertion.NameID() != "alice" { + t.Errorf("SP: %v", err) + http.Error(w, "SAML", 400) + return + } + w.WriteHeader(http.StatusNoContent) + })) + spServer.TLS = &tls.Config{MinVersion: tls.VersionTLS12, ClientAuth: tls.RequireAnyClientCert} + spServer.StartTLS() + defer spServer.Close() + idpServer := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + user, password, ok := r.BasicAuth() + if !ok || user != "alice" || password != "test-password" { + http.Error(w, "auth", http.StatusUnauthorized) + return + } + p, err := ecp.VerifyTLSRequest(r.Context(), r, proof.Options{}) + if err != nil { + t.Error(err) + http.Error(w, "proof", 400) + return + } + request, err := identity.AcceptECP(r.Context(), p, hokSP, "https://"+r.Host+r.URL.Path, at, nil) + if err != nil { + t.Error(err) + http.Error(w, "request", 400) + return + } + // This fixed mapping is application policy, independent of KeyInfo. + if !bytes.Equal(request.HolderOfKeyCertificate(), c.Responder.Raw) { + http.Error(w, "key association", http.StatusUnauthorized) + return + } + issued, err := identity.Issue(r.Context(), request, idp.Authentication{NameID: user, AuthnInstant: at, AuthnContext: "urn:test:password", HolderOfKeyCertificate: c.Responder.Raw}, at) + if err != nil { + t.Error(err) + http.Error(w, "issue", 400) + return + } + envelope, err := issued.ECP(r.Context()) + if err != nil { + t.Error(err) + return + } + if err := ecp.WriteIDPResponse(r.Context(), w, envelope); err != nil { + t.Error(err) + } + })) + idpServer.TLS = &tls.Config{MinVersion: tls.VersionTLS12, ClientAuth: tls.RequireAnyClientCert} + idpServer.StartTLS() + defer idpServer.Close() + service, identity = hokPartners(t, c, spServer.URL+"/acs", idpServer.URL+"/sso", c14n.Canonical10, false, true) + out, err := service.Begin(t.Context(), sp.BeginOptions{IDP: hokIDP, Binding: metadata.PAOS, At: at, BrowserBinding: browser, HolderOfKey: true}) + if err != nil { + t.Fatal(err) + } + challenge, err := out.PAOS() + if err != nil { + t.Fatal(err) + } + raw, _ := challenge.XML(t.Context()) + x, err := ecp.ParseHoKChallenge(t.Context(), raw, spServer.URL+"/resource", c.Responder.Raw, nil, binding.SOAPOptions{}) + if err != nil { + t.Fatal(err) + } + toIDP, err := x.ToIDP(t.Context()) + if err != nil { + t.Fatal(err) + } + raw, _ = toIDP.XML(t.Context()) + roots := x509.NewCertPool() + roots.AddCert(spServer.Certificate()) + roots.AddCert(idpServer.Certificate()) + transport := &http.Transport{TLSClientConfig: &tls.Config{MinVersion: tls.VersionTLS12, RootCAs: roots, Certificates: []tls.Certificate{{Certificate: [][]byte{c.Responder.Raw}, PrivateKey: c.ResponderKey}}}} + defer transport.CloseIdleConnections() + client := &http.Client{Transport: transport} + req, err := http.NewRequestWithContext(t.Context(), http.MethodPost, idpServer.URL+"/sso", bytes.NewReader(raw)) + if err != nil { + t.Fatal(err) + } + req.Header.Set("Content-Type", "text/xml") + req.SetBasicAuth("alice", "test-password") + response, err := client.Do(req) + if err != nil { + t.Fatal(err) + } + idpRaw, err := io.ReadAll(response.Body) + response.Body.Close() + if err != nil { + t.Fatal(err) + } else if response.StatusCode != http.StatusOK { + t.Fatalf("IdP HTTP: %d %v %s", response.StatusCode, err, idpRaw) + } + final, err := x.Finish(t.Context(), idpRaw, binding.SOAPOptions{}) + if err != nil { + t.Fatal(err) + } + raw, _ = final.XML(t.Context()) + req, err = http.NewRequestWithContext(t.Context(), http.MethodPost, spServer.URL+"/acs", bytes.NewReader(raw)) + if err != nil { + t.Fatal(err) + } + req.Header.Set("Content-Type", ecp.MediaType) + response, err = client.Do(req) + if err != nil { + t.Fatal(err) + } + response.Body.Close() + if response.StatusCode != http.StatusNoContent { + t.Fatalf("SP HTTP: %d", response.StatusCode) + } + if _, err := ecp.VerifyTLSRequest(t.Context(), httptest.NewRequestWithContext(t.Context(), http.MethodPost, "http://plain.test", bytes.NewReader(raw)), proof.Options{}); err == nil { + t.Fatal("plaintext or proxy-supplied certificate granted proof") + } +} diff --git a/saml/sp/encryption_test.go b/saml/sp/encryption_test.go new file mode 100644 index 0000000..40e8aea --- /dev/null +++ b/saml/sp/encryption_test.go @@ -0,0 +1,196 @@ +package sp_test + +import ( + "bytes" + "crypto" + "crypto/rand" + "crypto/rsa" + "crypto/x509" + "errors" + "net/url" + "testing" + "time" + + "github.com/skarm/xmlsec/internal/testutil/testcert" + "github.com/skarm/xmlsec/saml/binding" + "github.com/skarm/xmlsec/saml/idp" + "github.com/skarm/xmlsec/saml/metadata" + "github.com/skarm/xmlsec/saml/protocol" + "github.com/skarm/xmlsec/saml/replay" + "github.com/skarm/xmlsec/saml/sp" + "github.com/skarm/xmlsec/saml/state" +) + +// Exercise metadata trust, encryption, browser correlation and replay in the +// same public API flow. Encryption-only certificates cannot sign requests. +func TestEncryptedWebSSO(t *testing.T) { + const spID, idpID = "https://sp.example.test/entity", "https://idp.example.test/entity" + const acs, sso = "https://sp.example.test/acs", "https://idp.example.test/sso" + const innerFormat = "urn:test:subject-format" + c := testcert.New(t) + recipient, err := rsa.GenerateKey(rand.Reader, 2048) + if err != nil { + t.Fatal(err) + } + certificate := *c.Leaf + certificate.KeyUsage = x509.KeyUsageKeyEncipherment + encryptionCert := testcert.Issue(t, &certificate, c.Root, &recipient.PublicKey, c.RootKey) + for _, wireBinding := range []string{metadata.HTTPRedirect, metadata.HTTPPost} { + for _, policy := range []struct { + keyUse, requestedFormat string + producerDisabled, consumerDisabled bool + }{{"encryption", "", false, false}, {"", "", false, false}, {"signing", "", false, false}, {"encryption", protocol.NameIDUnspecified, false, false}, {"encryption", protocol.NameIDEncrypted, false, false}, {"", protocol.NameIDEncrypted, false, false}, {"encryption", protocol.NameIDEncrypted, true, false}, {"encryption", protocol.NameIDEncrypted, false, true}} { + name := wireBinding + "/" + policy.keyUse + "/" + policy.requestedFormat + if policy.producerDisabled { + name += "/producer-disabled" + } + if policy.consumerDisabled { + name += "/consumer-disabled" + } + t.Run(name, func(t *testing.T) { + keyUse := policy.keyUse + at := testcert.Now() + var aggregate bytes.Buffer + aggregate.WriteString(``) + for _, spec := range []metadata.EntityDescriptor{ + {ID: "_sp", EntityID: spID, ValidUntil: at.Add(time.Hour), SP: &metadata.RoleDescriptor{ + Keys: []metadata.KeyDescriptor{{Use: "signing", CertificateDER: c.Leaf.Raw}, {Use: keyUse, CertificateDER: encryptionCert.Raw}}, + Endpoints: map[string][]metadata.Endpoint{"AssertionConsumerService": {{Binding: metadata.HTTPPost, Location: acs, Indexed: true, Default: true}}}, + NameIDFormats: []string{protocol.NameIDUnspecified, innerFormat}, + AuthnRequestsSigned: true, WantAssertionsSigned: true, + }}, + {ID: "_idp", EntityID: idpID, ValidUntil: at.Add(time.Hour), IDP: &metadata.RoleDescriptor{ + Keys: []metadata.KeyDescriptor{{Use: "signing", CertificateDER: c.TSA.Raw}}, + Endpoints: map[string][]metadata.Endpoint{"SingleSignOnService": {{Binding: wireBinding, Location: sso}}}, + NameIDFormats: []string{protocol.NameIDUnspecified, innerFormat}, + WantAuthnRequestsSigned: true, + }}, + } { + d, err := metadata.Build(t.Context(), spec, at, metadata.Options{}) + if err != nil { + t.Fatal(err) + } + raw, err := d.XML(t.Context()) + if err != nil { + t.Fatal(err) + } + aggregate.Write(raw) + } + aggregate.WriteString(``) + partners, err := metadata.ParseTrusted(t.Context(), aggregate.Bytes(), at, metadata.Options{}) + if err != nil { + t.Fatal(err) + } + requests, err := state.NewMemoryStore(state.Config{}) + if err != nil { + t.Fatal(err) + } + spReplay, err := replay.NewMemoryStore(replay.Config{}) + if err != nil { + t.Fatal(err) + } + idpReplay, err := replay.NewMemoryStore(replay.Config{}) + if err != nil { + t.Fatal(err) + } + all := protocol.EncryptAssertion | protocol.EncryptNameID | protocol.EncryptAttributes + encryption := &protocol.EncryptionOptions{Targets: all} + requireEncryption := all + if policy.requestedFormat == protocol.NameIDEncrypted { + // The request itself requires EncryptedID, independently of + // the SP's default policy and the IdP's other target choices. + encryption.Targets = protocol.EncryptAssertion | protocol.EncryptAttributes + requireEncryption = 0 + } + producerPolicy := encryption + if policy.producerDisabled { + producerPolicy = nil + } + identity, err := idp.NewIdentityProvider(idp.Config{EntityID: idpID, Metadata: partners, Replay: idpReplay, Signing: protocol.SigningKey{Key: c.TSAKey}, Encryption: producerPolicy}) + if err != nil { + t.Fatal(err) + } + decryption := &protocol.DecryptionOptions{Keys: []crypto.Decrypter{recipient}} + consumerPolicy := decryption + if policy.consumerDisabled { + consumerPolicy = nil + } + service, err := sp.NewServiceProvider(sp.Config{EntityID: spID, Metadata: partners, Requests: requests, Replay: spReplay, Signing: protocol.SigningKey{Key: c.LeafKey}, Decryption: consumerPolicy, RequireEncryption: requireEncryption}) + if err != nil { + t.Fatal(err) + } + // Constructors own policy slices; caller mutation cannot change them. + encryption.Targets = 0 + decryption.Keys[0] = nil + browser := bytes.Repeat([]byte{42}, 32) + outbound, err := service.Begin(t.Context(), sp.BeginOptions{IDP: idpID, Binding: wireBinding, NameIDFormat: policy.requestedFormat, BrowserBinding: browser, At: at}) + if policy.consumerDisabled { + if outbound != nil || !errors.Is(err, sp.ErrPolicy) || requests.Len() != 0 { + t.Fatalf("unsatisfiable encryption request was saved: %v", err) + } + return + } + if err != nil { + t.Fatal(err) + } + var request *idp.Request + if wireBinding == metadata.HTTPRedirect { + request, err = identity.AcceptRedirect(t.Context(), outbound.RawQuery(), spID, sso, at) + } else { + var form bytes.Buffer + if _, err := outbound.WritePOST(t.Context(), &form); err != nil { + t.Fatal(err) + } + request, err = identity.AcceptPOST(t.Context(), form.Bytes(), spID, sso, at) + } + if err != nil { + t.Fatal(err) + } + response, err := identity.Issue(t.Context(), request, idp.Authentication{NameID: "alice", NameIDFormat: innerFormat, AuthnInstant: at, AuthnContext: "urn:test:password", Attributes: []protocol.Attribute{{Name: "role", Values: []string{"reader"}}}}, at) + if keyUse == "signing" || policy.producerDisabled { + if response != nil || !errors.Is(err, idp.ErrPolicy) { + t.Fatalf("signing-only key authorized encryption: %v", err) + } + return + } + if err != nil { + t.Fatal(err) + } + var form bytes.Buffer + if _, err := response.WritePOST(t.Context(), &form); err != nil { + t.Fatal(err) + } + if policy.requestedFormat == protocol.NameIDEncrypted { + encoded, err := url.ParseQuery(form.String()) + if err != nil { + t.Fatal(err) + } + relay := encoded.Get("RelayState") + bad, err := protocol.SignResponse(t.Context(), protocol.Response{ID: "_downgrade", Issuer: idpID, Audience: spID, Destination: acs, InResponseTo: outbound.RequestID(), IssueInstant: at, Encryption: &protocol.EncryptionOptions{Targets: protocol.EncryptAssertion, Recipient: &recipient.PublicKey}, Assertion: protocol.AssertionData{ID: "_downgrade-assertion", NameID: "alice", NotBefore: at, NotOnOrAfter: at.Add(time.Minute), AuthnInstant: at, AuthnContext: "urn:test:password"}}, protocol.SigningKey{Key: c.TSAKey}) + if err != nil { + t.Fatal(err) + } + post, err := binding.EncodePOST(t.Context(), bad, binding.Response, &relay, binding.POSTLimits{}) + if err != nil { + t.Fatal(err) + } + if result, err := service.ConsumePOST(t.Context(), post, sp.ConsumeOptions{BrowserBinding: browser, At: at}); result != nil || !errors.Is(err, protocol.ErrEncryption) { + t.Fatalf("NameIDPolicy downgrade accepted: %v", err) + } + } + wrong := bytes.Clone(browser) + wrong[0] ^= 1 + if result, err := service.ConsumePOST(t.Context(), form.Bytes(), sp.ConsumeOptions{BrowserBinding: wrong, At: at}); result != nil || !errors.Is(err, sp.ErrCorrelation) { + t.Fatalf("browser correlation bypassed: %v", err) + } + result, err := service.ConsumePOST(t.Context(), form.Bytes(), sp.ConsumeOptions{BrowserBinding: browser, At: at}) + if err != nil || result == nil || result.NameID() != "alice" || result.NameIDFormat() != innerFormat || result.Encryption() != all { + t.Fatalf("encrypted WebSSO: %v", err) + } + if result, err := service.ConsumePOST(t.Context(), form.Bytes(), sp.ConsumeOptions{BrowserBinding: browser, At: at}); result != nil || !errors.Is(err, replay.ErrReplay) { + t.Fatalf("encrypted replay accepted: %v", err) + } + }) + } + } +} diff --git a/saml/sp/sp.go b/saml/sp/sp.go index 56acb7e..6541e44 100644 --- a/saml/sp/sp.go +++ b/saml/sp/sp.go @@ -15,16 +15,20 @@ import ( "errors" "io" "net/url" + "reflect" "slices" "time" "github.com/skarm/xmlsec/document" "github.com/skarm/xmlsec/internal/nilcheck" "github.com/skarm/xmlsec/internal/signature" + "github.com/skarm/xmlsec/saml/artifact" "github.com/skarm/xmlsec/saml/binding" + "github.com/skarm/xmlsec/saml/ecp" "github.com/skarm/xmlsec/saml/internal/profile" "github.com/skarm/xmlsec/saml/internal/xmlsig" "github.com/skarm/xmlsec/saml/metadata" + "github.com/skarm/xmlsec/saml/proof" "github.com/skarm/xmlsec/saml/protocol" "github.com/skarm/xmlsec/saml/replay" "github.com/skarm/xmlsec/saml/state" @@ -61,7 +65,8 @@ type Config struct { // POSTLimits bounds POST binding messages and generated forms. POSTLimits binding.POSTLimits // RedirectLimits sets Redirect binding signature policy and query, inflation and XML - // limits. + // limits. An empty policy inherits Signing.Policy; an explicit policy must permit + // the outgoing signature together with Signing.Policy. RedirectLimits binding.Options // RequestLifetime bounds pending requests; zero selects five minutes. // Values must be between one second and ten minutes. @@ -69,6 +74,62 @@ type Config struct { // AllowUnsignedResponse permits an unsigned Response envelope; the Assertion still // requires authentication. AllowUnsignedResponse bool + // AllowUnsignedAssertion accepts an unsigned assertion only under an + // authenticated Response. A present assertion signature is still required + // to validate. + AllowUnsignedAssertion bool + // Decryption selects explicit local RSA providers and exact algorithms. + // Only providers authorized by this SP's trusted encryption metadata are used. + Decryption *protocol.DecryptionOptions + // RequireEncryption rejects plaintext at selected assertion positions. + RequireEncryption protocol.EncryptionTargets +} + +// BeginOptions selects an IdP, binding and authentication conditions and supplies the +// browser correlation secret. +type BeginOptions struct { + // At is the explicit trusted time used by this operation. + At time.Time + // ACSIndex selects an exact metadata endpoint index. Nil uses the default endpoint; + // a pointer to zero selects index zero explicitly. + ACSIndex *uint16 + // IDP selects the trusted identity provider by its metadata entity identifier. + IDP string + // Binding selects HTTP-Redirect (default), HTTP-POST or PAOS (ECP). + Binding string + // ResponseBinding selects HTTP-POST (default), HTTP-Artifact or PAOS. + // PAOS initiation always selects PAOS responses. + ResponseBinding string + // NameIDFormat optionally requests an exact subject identifier format URI. + NameIDFormat string + // BrowserBinding is a server-generated, unpredictable browser or session secret. + BrowserBinding []byte + // RequestedContexts lists requested authentication context class URIs for exact + // comparison. + RequestedContexts []string + // ChannelBindings enables the negotiated ECP 2.0 channel binding option. + // Supply data from the actual client/SP TLS connection; PAOS is required. + ChannelBindings []protocol.ChannelBinding + // ForceAuthn requests fresh authentication. + ForceAuthn bool + // IsPassive forbids interaction with the subject when true. + IsPassive bool + // AllowCreateNameID permits the IdP to create a new subject identifier when true. + AllowCreateNameID bool + // HolderOfKey requires ECP HoK; the handler must check client negotiation. + HolderOfKey bool +} + +// ConsumeOptions supplies trusted verification time, browser correlation secret and an +// optional expected client address. +type ConsumeOptions struct { + // At is the explicit trusted time used by this operation. + At time.Time + // BrowserBinding is an unpredictable, application-protected browser correlation secret. + BrowserBinding []byte + // ClientAddress is the expected client address when validating subject confirmation. + ClientAddress string + proof *proof.Verified } // ServiceProvider initiates solicited SAML login and validates and consumes correlated @@ -77,15 +138,21 @@ type ServiceProvider struct{ c Config } // NewServiceProvider validates and snapshots ServiceProvider policy and required trusted adapters. func NewServiceProvider(c Config) (*ServiceProvider, error) { - if !profile.URI(c.EntityID) || nilcheck.IsNil(c.Metadata) || nilcheck.IsNil(c.Requests) || nilcheck.IsNil(c.Replay) || nilcheck.IsNil(c.Signing.Key) || protocol.ValidatePolicy(c.Limits) != nil { + const ( + defaultRequestLifetime = 5 * time.Minute + maximumRequestLifetime = 10 * time.Minute + minimumRequestLifetime = time.Second + ) + + if !profile.URI(c.EntityID) || nilcheck.IsNil(c.Metadata) || nilcheck.IsNil(c.Requests) || nilcheck.IsNil(c.Replay) || nilcheck.IsNil(c.Signing.Key) || c.Signing.Secret != nil || protocol.ValidatePolicy(c.Limits) != nil { return nil, ErrPolicy } if c.RequestLifetime == 0 { - c.RequestLifetime = 5 * time.Minute + c.RequestLifetime = defaultRequestLifetime } - if c.RequestLifetime < time.Second || c.RequestLifetime > 10*time.Minute { + if c.RequestLifetime < minimumRequestLifetime || c.RequestLifetime > maximumRequestLifetime { return nil, ErrPolicy } @@ -106,8 +173,16 @@ func NewServiceProvider(c Config) (*ServiceProvider, error) { c.VerificationPolicy = xmlsig.ClonePolicy(c.VerificationPolicy) c.Signing.CertificateDER = bytes.Clone(c.Signing.CertificateDER) + c.Signing.InclusivePrefixes = append([]string(nil), c.Signing.InclusivePrefixes...) c.Signing.Policy = xmlsig.ClonePolicy(c.Signing.Policy) - c.RedirectLimits.Policy = xmlsig.ClonePolicy(c.Signing.Policy) + c.RedirectLimits.Policy = xmlsig.ClonePolicy(c.RedirectLimits.Policy) + + var err error + + c.Decryption, err = protocol.CloneDecryption(c.Decryption) + if err != nil || c.RequireEncryption&^(protocol.EncryptAssertion|protocol.EncryptNameID|protocol.EncryptAttributes) != 0 || c.RequireEncryption != 0 && c.Decryption == nil { + return nil, ErrPolicy + } return &ServiceProvider{c: c}, nil } @@ -115,6 +190,16 @@ func NewServiceProvider(c Config) (*ServiceProvider, error) { // Begin creates a signed solicited request and records its browser-bound pending state // before returning transport data. func (s *ServiceProvider) Begin(ctx context.Context, o BeginOptions) (*Outbound, error) { + const ( + fieldRelayState = "RelayState" + fieldSAMLEncoding = "SAMLEncoding" + fieldSAMLRequest = "SAMLRequest" + fieldSAMLResponse = "SAMLResponse" + fieldSignature = "Signature" + fieldSignatureAlgorithm = "SigAlg" + relayStateRandomBytes = 32 + ) + if s == nil || ctx == nil || o.At.IsZero() || !profile.URI(o.IDP) { return nil, ErrPolicy } @@ -128,12 +213,8 @@ func (s *ServiceProvider) Begin(ctx context.Context, o BeginOptions) (*Outbound, return nil, e } - if o.Binding == "" { - o.Binding = metadata.HTTPRedirect - } - - if o.Binding != metadata.HTTPRedirect && o.Binding != metadata.HTTPPost { - return nil, ErrPolicy + if err := normalizeBeginBindings(&o); err != nil { + return nil, err } own, other, e := s.roles(ctx, o.IDP, o.At) @@ -145,12 +226,12 @@ func (s *ServiceProvider) Begin(ctx context.Context, o BeginOptions) (*Outbound, return nil, ErrPolicy } - acs, e := own.ACS(metadata.HTTPPost, o.ACSIndex) + acs, e := beginACS(own, o) if e != nil { return nil, e } - if returned1, matched := beginNameIDFormat(&o, own, other); matched { + if returned1, matched := s.beginNameIDFormat(&o, own, other); matched { return nil, returned1 } @@ -174,7 +255,7 @@ func (s *ServiceProvider) Begin(ctx context.Context, o BeginOptions) (*Outbound, return nil, ErrPolicy } - for _, k := range []string{"SAMLRequest", "SAMLResponse", "RelayState", "SigAlg", "Signature", "SAMLEncoding"} { + for _, k := range []string{fieldSAMLRequest, fieldSAMLResponse, fieldRelayState, fieldSignatureAlgorithm, fieldSignature, fieldSAMLEncoding} { if _, ok := values[k]; ok { return nil, ErrPolicy } @@ -185,7 +266,7 @@ func (s *ServiceProvider) Begin(ctx context.Context, o BeginOptions) (*Outbound, return nil, e } - relayBytes := make([]byte, 32) + relayBytes := make([]byte, relayStateRandomBytes) if _, e = rand.Read(relayBytes); e != nil { return nil, e } @@ -201,12 +282,23 @@ func (s *ServiceProvider) Begin(ctx context.Context, o BeginOptions) (*Outbound, expires = other.ExpiresAt() } - spec := protocol.AuthnRequest{ID: requestID, Issuer: s.c.EntityID, Destination: destination, ACS: acs.Location, IssueInstant: o.At, NameIDFormat: o.NameIDFormat, RequestedContexts: append([]string(nil), o.RequestedContexts...), ForceAuthn: o.ForceAuthn, IsPassive: o.IsPassive, AllowCreateNameID: o.AllowCreateNameID} + spec := protocol.AuthnRequest{ID: requestID, Issuer: s.c.EntityID, Destination: destination, ACS: acs.Location, ResponseBinding: o.ResponseBinding, IssueInstant: o.At, NameIDFormat: o.NameIDFormat, RequestedContexts: append([]string(nil), o.RequestedContexts...), ForceAuthn: o.ForceAuthn, IsPassive: o.IsPassive, AllowCreateNameID: o.AllowCreateNameID} + spec.SOAPContext = o.Binding == metadata.HTTPArtifact + + spec.ChannelBindings = protocol.CloneChannelBindings(o.ChannelBindings) var signed *document.Document - if o.Binding == metadata.HTTPRedirect { + + switch o.Binding { + case metadata.HTTPRedirect: signed, e = protocol.BuildAuthnRequest(ctx, spec) - } else { + case metadata.PAOS: + if o.HolderOfKey { + signed, e = ecp.HoKChallenge(ctx, spec, s.c.Signing, &relay, "") + } else { + signed, e = ecp.Challenge(ctx, spec, s.c.Signing, &relay, "") + } + default: signed, e = protocol.SignAuthnRequest(ctx, spec, s.c.Signing) } @@ -215,11 +307,12 @@ func (s *ServiceProvider) Begin(ctx context.Context, o BeginOptions) (*Outbound, } out := &Outbound{destination: destination, binding: o.Binding, requestID: requestID, relay: relay, expires: expires, doc: signed, limits: s.c.POSTLimits} + out.issuer = s.c.EntityID + + out.recipient = o.IDP if o.Binding == metadata.HTTPRedirect { - opt := s.c.RedirectLimits - opt.SignatureMethod = s.c.Signing.SignatureMethod - out.query, e = binding.SignRedirect(ctx, signed, binding.Request, &relay, s.c.Signing.Key, opt) - } else { + out.query, e = s.signRedirect(ctx, signed, &relay) + } else if o.Binding != metadata.PAOS { _, e = binding.WritePOST(ctx, io.Discard, signed, binding.Request, &relay, s.c.POSTLimits) } @@ -228,6 +321,12 @@ func (s *ServiceProvider) Begin(ctx context.Context, o BeginOptions) (*Outbound, } pending := state.Pending{SP: s.c.EntityID, IDP: o.IDP, RequestID: requestID, RelayState: relay, ACS: acs.Location, IssuedAt: o.At, ExpiresAt: expires, BrowserSHA256: browser, NameIDFormat: pendingNameID(o.NameIDFormat), RequestedContexts: append([]string(nil), o.RequestedContexts...), ForceAuthn: o.ForceAuthn} + + pending.ResponseBinding = o.ResponseBinding + + pending.ChannelBindings = protocol.CloneChannelBindings(spec.ChannelBindings) + + pending.RequireHolderOfKey = o.HolderOfKey if e = s.c.Requests.Put(ctx, pending, o.At); e != nil { return nil, e } @@ -235,28 +334,37 @@ func (s *ServiceProvider) Begin(ctx context.Context, o BeginOptions) (*Outbound, return out, nil } -func keyAllowed(key crypto.PublicKey, role *metadata.Role) bool { - b, e := x509.MarshalPKIXPublicKey(key) - if e != nil { - return false +func (s *ServiceProvider) signRedirect(ctx context.Context, d *document.Document, relay *string) (string, error) { + opt := s.c.RedirectLimits + if emptyRedirectPolicy(opt.Policy) { + opt.Policy = s.c.Signing.Policy } - for _, candidate := range role.SigningKeys() { - v, e := x509.MarshalPKIXPublicKey(candidate) - if e == nil && bytes.Equal(b, v) { - return true - } + var err error + + opt.Policy, err = xmldsig.IntersectPolicies(s.c.Signing.Policy, opt.Policy) + if err != nil { + return "", err } - return false -} + opt.SignatureMethod = s.c.Signing.SignatureMethod -func contains(a []string, s string) bool { - return slices.Contains(a, s) + return binding.SignRedirect(ctx, d, binding.Request, relay, s.c.Signing.Key, opt) } -// A returned URL is only a metadata-selected endpoint plus a signed query. It -// does not include a user-controlled return URL from RelayState. +func emptyRedirectPolicy(p xmldsig.Policy) bool { + // Empty algorithm lists select defaults, so their allocated-empty and nil + // forms both retain the historical inheritance of the signing policy. + if len(p.AllowedSignatureMethods) == 0 { + p.AllowedSignatureMethods = nil + } + + if len(p.AllowedDigestMethods) == 0 { + p.AllowedDigestMethods = nil + } + + return reflect.ValueOf(p).IsZero() +} func pendingNameID(s string) string { if s == protocol.NameIDUnspecified { @@ -266,7 +374,16 @@ func pendingNameID(s string) string { return s } -func beginNameIDFormat(o *BeginOptions, own *metadata.Role, other *metadata.Role) (returned1 error, matched bool) { +func (s *ServiceProvider) beginNameIDFormat(o *BeginOptions, own *metadata.Role, other *metadata.Role) (returned1 error, matched bool) { + if o.NameIDFormat == protocol.NameIDEncrypted { + decryption, err := s.decryptionFor(own) + if err != nil || decryption == nil { + return ErrPolicy, true + } + + return nil, false + } + if o.NameIDFormat != "" && o.NameIDFormat != protocol.NameIDUnspecified { if len(own.NameIDFormats()) > 0 && !contains(own.NameIDFormats(), o.NameIDFormat) || len(other.NameIDFormats()) > 0 && !contains(other.NameIDFormats(), o.NameIDFormat) { return ErrPolicy, true @@ -276,9 +393,27 @@ func beginNameIDFormat(o *BeginOptions, own *metadata.Role, other *metadata.Role return } +func beginACS(own *metadata.Role, o BeginOptions) (metadata.Endpoint, error) { + if o.HolderOfKey { + if endpoint, err := own.HoKACS(o.ResponseBinding, o.ACSIndex); err == nil { + return endpoint, nil + } + } + + return own.ACS(o.ResponseBinding, o.ACSIndex) +} + func beginEndpoints(other *metadata.Role, o *BeginOptions, destination *string) (returned1Value error, matchedBranch bool) { - for _, ep := range other.Endpoints(metadata.SingleSignOnService) { - if ep.Binding == o.Binding { + requestBinding := o.Binding + if requestBinding == metadata.PAOS { + requestBinding = metadata.SOAP + } + + endpoints := other.Endpoints(metadata.SingleSignOnService) + + preferHoK := o.HolderOfKey && slices.ContainsFunc(endpoints, func(ep metadata.Endpoint) bool { return ep.HolderOfKey() && ep.TransportBinding() == requestBinding }) + for _, ep := range endpoints { + if ep.HolderOfKey() == preferHoK && ep.TransportBinding() == requestBinding { if (*destination) != "" { return ErrPolicy, true } @@ -298,22 +433,92 @@ func (s *ServiceProvider) ConsumePOST(ctx context.Context, form []byte, o Consum return nil, ErrPolicy } - if e := ctx.Err(); e != nil { - return nil, e + if err := ctx.Err(); err != nil { + return nil, err } - browser, e := state.BrowserDigest(o.BrowserBinding) + if _, err := state.BrowserDigest(o.BrowserBinding); err != nil { + return nil, err + } + + message, e := binding.DecodePOST(ctx, form, binding.Response, s.c.POSTLimits) if e != nil { return nil, e } - message, e := binding.DecodePOST(ctx, form, binding.Response, s.c.POSTLimits) + relay, present := message.RelayState() + if !present { + return nil, ErrCorrelation + } + + return s.consumeDocument(ctx, message.UnverifiedDocument(), relay, metadata.HTTPPost, o) +} + +// ConsumePAOS validates an ECP response using the same browser binding, metadata, +// assertion policy and atomic replay checks as HTTP-POST Web SSO. +func (s *ServiceProvider) ConsumePAOS(ctx context.Context, raw []byte, o ConsumeOptions) (*protocol.VerifiedAssertion, error) { + d, relay, err := ecp.DecodeResponse(ctx, raw, "", binding.SOAPOptions{}) + if err != nil { + return nil, err + } + + if relay == nil { + return nil, ErrCorrelation + } + + return s.consumeDocument(ctx, d, *relay, metadata.PAOS, o) +} + +// ConsumeECP validates a PAOS response with possession established for its exact +// SOAP snapshot. SAML signatures, pending HoK requirements and replay are checked. +func (s *ServiceProvider) ConsumeECP(ctx context.Context, p *proof.Verified, o ConsumeOptions) (*protocol.VerifiedAssertion, error) { + if p == nil { + return nil, ErrPolicy + } + + d, relay, err := ecp.ExtractResponse(ctx, p.Message(), "") + if err != nil { + return nil, err + } + + if relay == nil { + return nil, ErrCorrelation + } + + o.proof = p + + return s.consumeDocument(ctx, d, *relay, metadata.PAOS, o) +} + +// ConsumeArtifact validates a resolved artifact's SAML response. Resolution must +// already have authenticated the peer and single-use artifact. RelayState and +// browser binding are correlated with the enclosed response's own request ID. +func (s *ServiceProvider) ConsumeArtifact(ctx context.Context, message *document.Document, relay string, o ConsumeOptions) (*protocol.VerifiedAssertion, error) { + return s.consumeDocument(ctx, message, relay, metadata.HTTPArtifact, o) +} + +func (s *ServiceProvider) consumeDocument(ctx context.Context, message *document.Document, relay, responseBinding string, o ConsumeOptions) (*protocol.VerifiedAssertion, error) { + const ( + minimumRelayStateBytes = 16 + scopeAssertionKind = "assertion" + scopeRequestKind = "request" + scopeResponseKind = "response" + ) + + if s == nil || ctx == nil || o.At.IsZero() || message == nil { + return nil, ErrPolicy + } + + if err := ctx.Err(); err != nil { + return nil, err + } + + browser, e := state.BrowserDigest(o.BrowserBinding) if e != nil { return nil, e } - relay, ok := message.RelayState() - if !ok || len(relay) < 16 { + if len(relay) < minimumRelayStateBytes { return nil, ErrCorrelation } @@ -326,6 +531,15 @@ func (s *ServiceProvider) ConsumePOST(ctx context.Context, form []byte, o Consum return nil, ErrCorrelation } + expectedBinding := pending.ResponseBinding + if expectedBinding == "" { + expectedBinding = metadata.HTTPPost + } + + if responseBinding != expectedBinding { + return nil, ErrPolicy + } + own, other, e := s.roles(ctx, pending.IDP, o.At) if e != nil { return nil, e @@ -334,7 +548,7 @@ func (s *ServiceProvider) ConsumePOST(ctx context.Context, form []byte, o Consum allowedACS := false for _, ep := range own.Endpoints(metadata.AssertionConsumerService) { - if ep.Binding == metadata.HTTPPost && ep.Location == pending.ACS { + if ep.AcceptsTransport(responseBinding, pending.RequireHolderOfKey) && ep.Location == pending.ACS { allowedACS = true } } @@ -344,17 +558,29 @@ func (s *ServiceProvider) ConsumePOST(ctx context.Context, form []byte, o Consum } opt := protocol.ResponseOptions{Issuer: pending.IDP, Audience: s.c.EntityID, Destination: pending.ACS, ExpectedRequestID: pending.RequestID, At: o.At, RequestIssueInstant: pending.IssuedAt, ForceAuthn: pending.ForceAuthn, ExpectedAuthnContexts: append([]string(nil), pending.RequestedContexts...), ExpectedAddress: o.ClientAddress, Limits: s.c.Limits, Policy: s.c.VerificationPolicy, AllowUnsignedResponse: s.c.AllowUnsignedResponse} - if pending.NameIDFormat != "" { - opt.AllowedNameIDFormats = []string{pending.NameIDFormat} - } else { - opt.AllowedNameIDFormats = own.NameIDFormats() + opt.AllowUnsignedAssertion = s.c.AllowUnsignedAssertion + opt.ExpectedChannelBindings = protocol.CloneChannelBindings(pending.ChannelBindings) + opt.Proof = o.proof + opt.RequireHolderOfKey = pending.RequireHolderOfKey + + opt.RequireEncryption = s.c.RequireEncryption + + opt.Decryption, e = s.decryptionFor(own) + if e != nil { + return nil, e } - result, e := protocol.VerifyResponse(ctx, message.UnverifiedDocument(), other.SigningKeys(), opt) + responseNameIDPolicy(pending.NameIDFormat, own, &opt) + + result, e := protocol.VerifyResponse(ctx, message, other.SigningKeys(), opt) if e != nil { return nil, e } + if len(other.Endpoints(metadata.SingleLogoutService)) > 0 && result.SessionIndex() == "" { + return nil, protocol.ErrProfile + } + age, _, skew, e := protocol.AcceptanceWindow(s.c.Limits) if e != nil { return nil, e @@ -369,7 +595,7 @@ func (s *ServiceProvider) ConsumePOST(ctx context.Context, form []byte, o Consum expires = o.At.Add(age + skew) } - entries := []replay.Entry{{Scope: scope("request", s.c.EntityID, pending.IDP), ID: pending.RequestID, ExpiresAt: expires}, {Scope: scope("assertion", s.c.EntityID, pending.IDP), ID: result.AssertionID(), ExpiresAt: expires}, {Scope: scope("response", s.c.EntityID, pending.IDP), ID: result.ResponseID(), ExpiresAt: expires}} + entries := []replay.Entry{{Scope: scope(scopeRequestKind, s.c.EntityID, pending.IDP), ID: pending.RequestID, ExpiresAt: expires}, {Scope: scope(scopeAssertionKind, s.c.EntityID, pending.IDP), ID: result.AssertionID(), ExpiresAt: expires}, {Scope: scope(scopeResponseKind, s.c.EntityID, pending.IDP), ID: result.ResponseID(), ExpiresAt: expires}} if e = s.c.Replay.Consume(ctx, entries, o.At); e != nil { return nil, e } @@ -377,11 +603,6 @@ func (s *ServiceProvider) ConsumePOST(ctx context.Context, form []byte, o Consum return result, nil } -func scope(kind, sp, idp string) string { - sum := sha256.Sum256([]byte(sp + "\x00" + idp)) - return "saml:sp:" + kind + ":" + hex.EncodeToString(sum[:]) -} - func (s *ServiceProvider) roles(ctx context.Context, idp string, at time.Time) (*metadata.Role, *metadata.Role, error) { a, e := s.c.Metadata.Lookup(ctx, s.c.EntityID, at) if e != nil { @@ -406,44 +627,38 @@ func (s *ServiceProvider) roles(ctx context.Context, idp string, at time.Time) ( return own, other, nil } -// BeginOptions selects an IdP, binding and authentication conditions and supplies the -// browser correlation secret. -type BeginOptions struct { - // IDP selects the trusted identity provider by its metadata entity identifier. - IDP string - // Binding selects HTTP-Redirect (the default) or HTTP-POST. - Binding string - // ACSIndex selects an exact metadata endpoint index. Nil uses the default endpoint; - // a pointer to zero selects index zero explicitly. - ACSIndex *uint16 - // BrowserBinding is a server-generated, unpredictable browser or session secret. - BrowserBinding []byte - // At is the explicit trusted time used by this operation. - At time.Time - // NameIDFormat optionally requests an exact subject identifier format URI. - NameIDFormat string - // RequestedContexts lists requested authentication context class URIs for exact - // comparison. - RequestedContexts []string - // ForceAuthn requests fresh authentication. - ForceAuthn bool - // IsPassive forbids interaction with the subject when true. - IsPassive bool - // AllowCreateNameID permits the IdP to create a new subject identifier when true. - AllowCreateNameID bool -} - // Outbound retains an immutable signed request and its binding-specific transport // data. type Outbound struct { - destination string - binding string - requestID string - relay string - query string - limits binding.POSTLimits - doc *document.Document - expires time.Time + destination string + binding string + requestID string + relay string + query string + limits binding.POSTLimits + doc *document.Document + expires time.Time + issuer, recipient string +} + +// IssueArtifact creates a one-time AuthnRequest artifact bound to the selected +// IdP. Deliver it at Destination using binding.ArtifactURL or ArtifactForm. +func (o *Outbound) IssueArtifact(ctx context.Context, issuer artifact.Issuer, at time.Time, lifetime time.Duration) (binding.Artifact, error) { + if o == nil || o.binding != metadata.HTTPArtifact || issuer.EntityID != o.issuer { + return binding.Artifact{}, ErrPolicy + } + + return issuer.Issue(ctx, o.doc, o.recipient, at, lifetime) +} + +// PAOS returns the immutable ECP challenge envelope for an HTTP 200 response +// with Content-Type application/vnd.paos+xml. Negotiate ECP before calling Begin. +func (o *Outbound) PAOS() (*document.Document, error) { + if o == nil || o.binding != metadata.PAOS { + return nil, ErrPolicy + } + + return o.doc, nil } // Destination returns the validated destination endpoint. @@ -522,7 +737,7 @@ func (o *Outbound) ExpiresAt() time.Time { } // RedirectURL returns the destination URL with the signed query appended for a Redirect -// request. +// request. RelayState is an opaque signed query value, not a return destination. func (o *Outbound) RedirectURL() (string, error) { if o == nil || o.binding != metadata.HTTPRedirect { return "", ErrPolicy @@ -542,13 +757,106 @@ func (o *Outbound) RedirectURL() (string, error) { return u.String(), nil } -// ConsumeOptions supplies trusted verification time, browser correlation secret and an -// optional expected client address. -type ConsumeOptions struct { - // At is the explicit trusted time used by this operation. - At time.Time - // BrowserBinding is an unpredictable, application-protected browser correlation secret. - BrowserBinding []byte - // ClientAddress is the expected client address when validating subject confirmation. - ClientAddress string +func keyAllowed(key crypto.PublicKey, role *metadata.Role) bool { + b, e := x509.MarshalPKIXPublicKey(key) + if e != nil { + return false + } + + for _, candidate := range role.SigningKeys() { + v, e := x509.MarshalPKIXPublicKey(candidate) + if e == nil && bytes.Equal(b, v) { + return true + } + } + + return false +} + +func contains(a []string, s string) bool { + return slices.Contains(a, s) +} + +func scope(kind, sp, idp string) string { + const ( + scopeSPPrefix = "saml:sp:" + scopeSeparator = "\u0000" + ) + + sum := sha256.Sum256([]byte(sp + scopeSeparator + idp)) + + return scopeSPPrefix + kind + ":" + hex.EncodeToString(sum[:]) +} + +func (s *ServiceProvider) decryptionFor(own *metadata.Role) (*protocol.DecryptionOptions, error) { + if s.c.Decryption == nil { + return nil, nil + } + + decryption := *s.c.Decryption + + decryption.Keys = nil + for _, candidate := range s.c.Decryption.Keys { + encoded, err := x509.MarshalPKIXPublicKey(candidate.Public()) + if err != nil { + return nil, ErrPolicy + } + + for _, allowed := range own.EncryptionKeys() { + other, err := x509.MarshalPKIXPublicKey(allowed) + if err == nil && bytes.Equal(encoded, other) { + decryption.Keys = append(decryption.Keys, candidate) + break + } + } + } + + if len(decryption.Keys) != 0 { + return &decryption, nil + } + + return nil, nil +} + +func normalizeBeginBindings(o *BeginOptions) error { + if (len(o.ChannelBindings) != 0 || o.HolderOfKey) && o.Binding != metadata.PAOS { + return ErrPolicy + } + + if o.Binding == "" { + o.Binding = metadata.HTTPRedirect + } + + if o.Binding != metadata.HTTPRedirect && o.Binding != metadata.HTTPPost && o.Binding != metadata.PAOS && o.Binding != metadata.HTTPArtifact { + return ErrPolicy + } + + if o.ResponseBinding == "" { + o.ResponseBinding = metadata.HTTPPost + } + + if o.Binding == metadata.PAOS { + o.ResponseBinding = metadata.PAOS + } + + if !contains([]string{metadata.HTTPPost, metadata.HTTPArtifact, metadata.PAOS}, o.ResponseBinding) { + return ErrPolicy + } + + if o.ResponseBinding == metadata.PAOS && o.Binding != metadata.PAOS { + return ErrPolicy + } + + return nil +} + +func responseNameIDPolicy(nameIDFormat string, own *metadata.Role, options *protocol.ResponseOptions) { + if nameIDFormat == protocol.NameIDEncrypted { + options.RequireEncryption |= protocol.EncryptNameID + options.AllowedNameIDFormats = own.NameIDFormats() + } else if nameIDFormat != "" && nameIDFormat != protocol.NameIDUnspecified { + options.AllowedNameIDFormats = []string{nameIDFormat} + } else { + options.AllowedNameIDFormats = own.NameIDFormats() + } } diff --git a/saml/sp/sp_test.go b/saml/sp/sp_test.go index 3a6bb63..3eac2cb 100644 --- a/saml/sp/sp_test.go +++ b/saml/sp/sp_test.go @@ -5,11 +5,13 @@ import ( "context" "crypto" "crypto/ecdsa" + "crypto/fips140" "crypto/rand" "crypto/rsa" "crypto/sha256" "encoding/base64" "errors" + "io" "net/url" "testing" "time" @@ -521,3 +523,158 @@ func TestBeginRejectsMetadataPolicyBeforeSaving(t *testing.T) { }) } } + +type redirectPolicySignerSpy struct { + crypto.Signer + calls int +} + +func (s *redirectPolicySignerSpy) Sign(random io.Reader, digest []byte, options crypto.SignerOpts) ([]byte, error) { + s.calls++ + return s.Signer.Sign(random, digest, options) +} + +func TestBeginRedirectIntersectsPolicies(t *testing.T) { + for _, tc := range []struct { + name string + redirect xmldsig.Policy + signing xmldsig.Policy + denied bool + }{ + {name: "zero inherits"}, + {name: "empty lists inherit", redirect: xmldsig.Policy{AllowedSignatureMethods: []string{}, AllowedDigestMethods: []string{}}}, + {name: "explicit permits", redirect: xmldsig.Policy{AllowedSignatureMethods: []string{xmldsig.ECDSASHA256}}}, + {name: "explicit denies", redirect: xmldsig.Policy{AllowedSignatureMethods: []string{xmldsig.ECDSASHA384}}, denied: true}, + {name: "signing denies", redirect: xmldsig.Policy{AllowedSignatureMethods: []string{xmldsig.ECDSASHA256}}, signing: xmldsig.Policy{AllowedSignatureMethods: []string{xmldsig.ECDSASHA384}}, denied: true}, + {name: "invalid redirect bounds", redirect: xmldsig.Policy{MaxRSABits: -1}, denied: true}, + {name: "incompatible bounds", redirect: xmldsig.Policy{MinRSABits: 4096}, signing: xmldsig.Policy{MaxRSABits: 2048}, denied: true}, + {name: "disjoint digest sets", redirect: xmldsig.Policy{AllowedDigestMethods: []string{xmldsig.SHA384}}, signing: xmldsig.Policy{AllowedDigestMethods: []string{xmldsig.SHA256}}, denied: true}, + } { + t.Run(tc.name, func(t *testing.T) { + c, resolver, requests, consumed := serviceProviderConfig(t) + signer := &redirectPolicySignerSpy{Signer: c.Signing.Key} + c.Signing.Key = signer + c.Signing.Policy = tc.signing + c.RedirectLimits.Policy = tc.redirect + resolver.entities = trustedMetadataEntities(t, c, time.Minute, time.Minute, singleSignOnURL) + service, err := NewServiceProvider(c) + if err != nil { + t.Fatal(err) + } + out, err := service.Begin(t.Context(), BeginOptions{IDP: idpEntityID, At: testcert.Now(), BrowserBinding: bytes.Repeat([]byte{7}, 32)}) + if tc.denied { + if out != nil || !errors.Is(err, xmldsig.ErrPolicy) || requests.puts != 0 || signer.calls != 0 { + t.Fatalf("policy denial: out=%v err=%v puts=%d signs=%d", out != nil, err, requests.puts, signer.calls) + } + } else if err != nil || out == nil || requests.puts != 1 || signer.calls != 1 { + t.Fatalf("allowed Redirect: out=%v err=%v puts=%d signs=%d", out != nil, err, requests.puts, signer.calls) + } + if consumed.calls != 0 { + t.Fatal("Begin consumed replay state") + } + }) + } +} + +func TestBeginRedirectPolicyRSAAndLegacy(t *testing.T) { + key, err := rsa.GenerateKey(rand.Reader, 2048) + if err != nil { + t.Fatal(err) + } + chain := testcert.New(t) + certificate := testcert.Issue(t, chain.Leaf, chain.Root, key.Public(), chain.RootKey) + for _, tc := range []struct { + name string + method string + redirect xmldsig.Policy + signing xmldsig.Policy + denied bool + wantErr error + }{ + {name: "RSA lower bound", redirect: xmldsig.Policy{MinRSABits: 4096}, denied: true, wantErr: xmldsig.ErrKey}, + {name: "RSA exact bound", redirect: xmldsig.Policy{MinRSABits: 2048, MaxRSABits: 2048}}, + {name: "legacy zero inherits", method: xmldsig.RSASHA1, signing: legacyRedirectSigningPolicy(), denied: fips140.Enabled()}, + {name: "legacy empty inherits", method: xmldsig.RSASHA1, redirect: xmldsig.Policy{AllowedSignatureMethods: []string{}, AllowedDigestMethods: []string{}}, signing: legacyRedirectSigningPolicy(), denied: fips140.Enabled()}, + {name: "legacy explicit permission denied", method: xmldsig.RSASHA1, redirect: xmldsig.Policy{AllowedSignatureMethods: []string{xmldsig.RSASHA1}}, signing: legacyRedirectSigningPolicy(), denied: true}, + } { + t.Run(tc.name, func(t *testing.T) { + c, resolver, requests, _ := serviceProviderConfig(t) + signer := &redirectPolicySignerSpy{Signer: key} + c.Signing = protocol.SigningKey{Key: signer, CertificateDER: certificate.Raw, SignatureMethod: tc.method, Policy: tc.signing} + c.RedirectLimits.Policy = tc.redirect + resolver.entities = trustedMetadataEntities(t, c, time.Minute, time.Minute, singleSignOnURL) + service, err := NewServiceProvider(c) + if err != nil { + t.Fatal(err) + } + out, err := service.Begin(t.Context(), BeginOptions{IDP: idpEntityID, At: testcert.Now(), BrowserBinding: bytes.Repeat([]byte{7}, 32)}) + if tc.denied { + wantErr := tc.wantErr + if wantErr == nil { + wantErr = xmldsig.ErrPolicy + } + if out != nil || !errors.Is(err, wantErr) || requests.puts != 0 || signer.calls != 0 { + t.Fatalf("RSA policy denial: out=%v err=%v puts=%d signs=%d", out != nil, err, requests.puts, signer.calls) + } + } else if err != nil || out == nil || requests.puts != 1 || signer.calls != 1 { + t.Fatalf("RSA Redirect: out=%v err=%v puts=%d signs=%d", out != nil, err, requests.puts, signer.calls) + } + }) + } +} + +func legacyRedirectSigningPolicy() xmldsig.Policy { + p := xmldsig.LegacyVerificationPolicy() + p.AllowLegacySigning = true + return p +} + +func TestRedirectPolicySnapshotAndUnusedPOST(t *testing.T) { + t.Run("owned lists", func(t *testing.T) { + c, resolver, requests, _ := serviceProviderConfig(t) + c.RedirectLimits.Policy = xmldsig.Policy{AllowedSignatureMethods: []string{xmldsig.ECDSASHA256}, AllowedDigestMethods: []string{xmldsig.SHA256}} + resolver.entities = trustedMetadataEntities(t, c, time.Minute, time.Minute, singleSignOnURL) + service, err := NewServiceProvider(c) + if err != nil { + t.Fatal(err) + } + c.RedirectLimits.Policy.AllowedSignatureMethods[0] = xmldsig.ECDSASHA384 + c.RedirectLimits.Policy.AllowedDigestMethods[0] = xmldsig.SHA384 + if len(service.c.RedirectLimits.Policy.AllowedSignatureMethods) != 1 || len(service.c.RedirectLimits.Policy.AllowedDigestMethods) != 1 || service.c.RedirectLimits.Policy.AllowedSignatureMethods[0] != xmldsig.ECDSASHA256 || service.c.RedirectLimits.Policy.AllowedDigestMethods[0] != xmldsig.SHA256 { + t.Fatal("caller mutation changed Redirect policy snapshot") + } + out, err := service.Begin(t.Context(), BeginOptions{IDP: idpEntityID, At: testcert.Now(), BrowserBinding: bytes.Repeat([]byte{7}, 32)}) + if err != nil || out == nil || requests.puts != 1 { + t.Fatalf("owned Redirect policy: out=%v err=%v puts=%d", out != nil, err, requests.puts) + } + }) + t.Run("POST ignores unused Redirect policy", func(t *testing.T) { + c, resolver, requests, _ := serviceProviderConfig(t) + c.RedirectLimits.Policy = xmldsig.Policy{MaxRSABits: -1} + resolver.entities = trustedMetadataEntities(t, c, time.Minute, time.Minute, singleSignOnURL) + doc, err := metadata.Build(t.Context(), metadata.EntityDescriptor{ID: "_post-idp", EntityID: idpEntityID, ValidUntil: testcert.Now().Add(time.Minute), IDP: &metadata.RoleDescriptor{Keys: []metadata.KeyDescriptor{{Use: "signing", CertificateDER: c.Signing.CertificateDER}}, Endpoints: map[string][]metadata.Endpoint{metadata.SingleSignOnService: {{Binding: metadata.HTTPPost, Location: singleSignOnURL}}}}}, testcert.Now(), metadata.Options{}) + if err != nil { + t.Fatal(err) + } + raw, err := doc.XML(t.Context()) + if err != nil { + t.Fatal(err) + } + snapshot, err := metadata.ParseTrusted(t.Context(), raw, testcert.Now(), metadata.Options{}) + if err != nil { + t.Fatal(err) + } + resolver.entities[idpEntityID], err = snapshot.Find(idpEntityID, testcert.Now()) + if err != nil { + t.Fatal(err) + } + service, err := NewServiceProvider(c) + if err != nil { + t.Fatal(err) + } + out, err := service.Begin(t.Context(), BeginOptions{IDP: idpEntityID, Binding: metadata.HTTPPost, At: testcert.Now(), BrowserBinding: bytes.Repeat([]byte{7}, 32)}) + if err != nil || out == nil || requests.puts != 1 { + t.Fatalf("POST with unused Redirect policy: out=%v err=%v puts=%d", out != nil, err, requests.puts) + } + }) +} diff --git a/saml/state/state.go b/saml/state/state.go index c2446c6..537dfcd 100644 --- a/saml/state/state.go +++ b/saml/state/state.go @@ -8,10 +8,19 @@ import ( "sync" "time" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/saml/internal/expiry" "github.com/skarm/xmlsec/saml/internal/profile" + "github.com/skarm/xmlsec/saml/protocol" ) +// Store only the fixed 32-byte SHA-256 browser-secret digest; the original unpredictable browser secret is never retained in pending state. +const ( + browserDigestBytes = 32 +) + +const paosBinding = lexicon.SAMLBindingPAOS + var ( // ErrPolicy reports invalid SAML request state. ErrPolicy = errors.New("invalid SAML request state") @@ -26,6 +35,10 @@ var ( // Pending retains a solicited request, its browser-secret digest, requested // authentication conditions and expiration. type Pending struct { + // IssuedAt is the authentication request issuance time. + IssuedAt time.Time + // ExpiresAt is the pending request's expiration time. + ExpiresAt time.Time // SP is the requesting service provider's entity identifier. SP string // IDP is the selected identity provider's entity identifier. @@ -36,17 +49,21 @@ type Pending struct { RelayState string // ACS is the exact assertion consumer endpoint selected for the request. ACS string + // ResponseBinding fixes the assertion delivery binding. Empty means the + // historical HTTP-POST default for existing durable-store records. + ResponseBinding string // NameIDFormat is the requested subject identifier format URI, if specified. NameIDFormat string - // IssuedAt is the authentication request issuance time. - IssuedAt time.Time - // ExpiresAt is the pending request's expiration time. - ExpiresAt time.Time - // BrowserSHA256 binds the request to the browser secret without storing the secret. - BrowserSHA256 [32]byte + // ChannelBindings retains the actual SP channel data sent in the signed ECP + // request, so a missing or mismatched assertion signal cannot downgrade it. + ChannelBindings []protocol.ChannelBinding // RequestedContexts contains the authentication context class URIs requested with exact // comparison. RequestedContexts []string + // BrowserSHA256 binds the request to the browser secret without storing the secret. + BrowserSHA256 [browserDigestBytes]byte + // RequireHolderOfKey prevents a bearer downgrade of a solicited ECP login. + RequireHolderOfKey bool // ForceAuthn records whether the request requires fresh authentication. ForceAuthn bool } @@ -54,6 +71,8 @@ type Pending struct { // Clone returns an owned copy of the request state and its context list. func (p Pending) Clone() Pending { p.RequestedContexts = append([]string(nil), p.RequestedContexts...) + p.ChannelBindings = protocol.CloneChannelBindings(p.ChannelBindings) + return p } @@ -66,6 +85,7 @@ func (p Pending) own() Pending { out.RequestID = strings.Clone(out.RequestID) out.RelayState = strings.Clone(out.RelayState) out.ACS = strings.Clone(out.ACS) + out.ResponseBinding = strings.Clone(out.ResponseBinding) out.NameIDFormat = strings.Clone(out.NameIDFormat) for i, value := range out.RequestedContexts { @@ -117,15 +137,23 @@ type stateKey struct { // NewMemoryStore creates an empty bounded request store using default limits for zero Config // fields. func NewMemoryStore(c Config) (*MemoryStore, error) { + const ( + defaultStateLifetime = 15 * time.Minute + defaultStoreCapacity = 10000 + maximumStateLifetime = time.Hour + maximumStoreCapacity = 1000000 + minimumStoreLifetime = time.Second + ) + if c.Capacity == 0 { - c.Capacity = 10000 + c.Capacity = defaultStoreCapacity } if c.MaxTTL == 0 { - c.MaxTTL = 15 * time.Minute + c.MaxTTL = defaultStateLifetime } - if c.Capacity < 1 || c.Capacity > 1000000 || c.MaxTTL < time.Second || c.MaxTTL > time.Hour { + if c.Capacity < 1 || c.Capacity > maximumStoreCapacity || c.MaxTTL < minimumStoreLifetime || c.MaxTTL > maximumStateLifetime { return nil, ErrPolicy } @@ -174,7 +202,7 @@ func (m *MemoryStore) Put(ctx context.Context, p Pending, at time.Time) error { // Get returns an owned copy of unexpired request state without consuming it. func (m *MemoryStore) Get(ctx context.Context, sp, relay string, at time.Time) (Pending, error) { - if m == nil || ctx == nil || at.IsZero() || sp == "" || relay == "" || len(relay) > 80 { + if m == nil || ctx == nil || at.IsZero() || sp == "" || relay == "" || len(relay) > profile.HTTPRelayStateMaxBytes { return Pending{}, ErrPolicy } @@ -208,14 +236,21 @@ func (m *MemoryStore) Len() int { } m.mu.Lock() - defer m.mu.Unlock() + n := len(m.entries) + m.mu.Unlock() - return len(m.entries) + return n } func (m *MemoryStore) prune(ctx context.Context, at time.Time) error { + const ( + pruneMinimumCapacity = 1024 + pruneSweepBatch = 256 + retainedCapacityDivisor = 4 + ) + for removed := 0; ; removed++ { - if removed%256 == 0 { + if removed%pruneSweepBatch == 0 { if err := ctx.Err(); err != nil { return err } @@ -230,10 +265,10 @@ func (m *MemoryStore) prune(ctx context.Context, at time.Time) error { delete(m.entries, key) } - if m.peak > 1024 && len(m.entries) <= m.peak/4 { + if m.peak > pruneMinimumCapacity && len(m.entries) <= m.peak/retainedCapacityDivisor { entries := make(map[stateKey]Pending, len(m.entries)) for key, value := range m.entries { - if len(entries)%256 == 0 { + if len(entries)%pruneSweepBatch == 0 { if err := ctx.Err(); err != nil { return err } @@ -252,16 +287,38 @@ func (m *MemoryStore) prune(ctx context.Context, at time.Time) error { // BrowserDigest hashes a browser correlation secret of 16 to 1024 bytes. The caller must // generate and protect the secret. -func BrowserDigest(secret []byte) ([32]byte, error) { - if len(secret) < 16 || len(secret) > 1024 { - return [32]byte{}, ErrPolicy +func BrowserDigest(secret []byte) ([browserDigestBytes]byte, error) { + const ( + maximumBrowserBindingBytes = 1024 + minimumBrowserBindingBytes = 16 + ) + + if len(secret) < minimumBrowserBindingBytes || len(secret) > maximumBrowserBindingBytes { + return [browserDigestBytes]byte{}, ErrPolicy } return sha256.Sum256(secret), nil } func valid(p Pending, at time.Time, maxTTL time.Duration) bool { - if !profile.URI(p.SP) || !profile.URI(p.IDP) || !profile.ID(p.RequestID) || len(p.RelayState) < 16 || len(p.RelayState) > 80 || !profile.Endpoint(p.ACS, true) || p.IssuedAt.IsZero() || p.IssuedAt.After(at) || !p.ExpiresAt.After(at) || p.ExpiresAt.Sub(p.IssuedAt) > maxTTL || len(p.RequestedContexts) > 8 || p.NameIDFormat != "" && !profile.URI(p.NameIDFormat) { + const ( + maximumRequestedContexts = 8 + minimumRelayStateBytes = 16 + ) + + if p.RequireHolderOfKey && p.ResponseBinding != paosBinding { + return false + } + + if protocol.ValidateChannelBindings(p.ChannelBindings, false) != nil || len(p.ChannelBindings) != 0 && p.ResponseBinding != paosBinding { + return false + } + + if p.ResponseBinding != "" && p.ResponseBinding != lexicon.SAMLBindingHTTPPost && p.ResponseBinding != lexicon.SAMLBindingHTTPArtifact && p.ResponseBinding != paosBinding { + return false + } + + if !profile.URI(p.SP) || !profile.URI(p.IDP) || !profile.ID(p.RequestID) || len(p.RelayState) < minimumRelayStateBytes || len(p.RelayState) > profile.HTTPRelayStateMaxBytes || !profile.Endpoint(p.ACS, true) || p.IssuedAt.IsZero() || p.IssuedAt.After(at) || !p.ExpiresAt.After(at) || p.ExpiresAt.Sub(p.IssuedAt) > maxTTL || len(p.RequestedContexts) > maximumRequestedContexts || p.NameIDFormat != "" && !profile.URI(p.NameIDFormat) { return false } diff --git a/saml/state/state_test.go b/saml/state/state_test.go index 30599f3..45af767 100644 --- a/saml/state/state_test.go +++ b/saml/state/state_test.go @@ -171,7 +171,9 @@ func TestStateMassExpiryPreservesPendingRequests(t *testing.T) { } p := requests[0] p.IssuedAt, p.ExpiresAt = later, later.Add(time.Minute) - if err := m.Put(t.Context(), p, later); err != nil || m.Len() != count/4+1 { + if err := m.Put(t.Context(), p, later); err != nil { + t.Fatal(err) + } else if m.Len() != count/4+1 { t.Fatal("expired key or capacity not reusable", err, m.Len()) } if _, err := m.Get(t.Context(), p.SP, p.RelayState, p.ExpiresAt); !errors.Is(err, state.ErrMissing) || m.Len() != 0 { @@ -179,10 +181,6 @@ func TestStateMassExpiryPreservesPendingRequests(t *testing.T) { } } -func pending(at time.Time) state.Pending { - return state.Pending{SP: "https://sp.test", IDP: "https://idp.test", RequestID: nameRequest, RelayState: "abcdefghijklmnop", ACS: uriHTTPSSpTestAcs, IssuedAt: at, ExpiresAt: at.Add(time.Minute), RequestedContexts: []string{uriUrnTest}} -} - func BenchmarkPutAndExpire(b *testing.B) { m, err := state.NewMemoryStore(state.Config{}) if err != nil { @@ -229,3 +227,7 @@ func BenchmarkGetParallel(b *testing.B) { } }) } + +func pending(at time.Time) state.Pending { + return state.Pending{SP: "https://sp.test", IDP: "https://idp.test", RequestID: nameRequest, RelayState: "abcdefghijklmnop", ACS: uriHTTPSSpTestAcs, IssuedAt: at, ExpiresAt: at.Add(time.Minute), RequestedContexts: []string{uriUrnTest}} +} diff --git a/tests/README.md b/tests/README.md index f5ad790..37c8ddb 100644 --- a/tests/README.md +++ b/tests/README.md @@ -5,4 +5,5 @@ Cross-package tests are in `integration`; optional native comparisons in `intero suite. Unit tests, examples, fuzz targets and benchmarks live beside their packages. See [testing](../docs/testing.md) for commands, fixtures and Docker prerequisites. -`cmd/dev` provides the documentation, example and fuzz runners. +`cmd/dev` provides documentation, example and fuzz runners, pinned W3C suite +diagnostics, language-proof capture/cache commands and the interoperability-log gate. diff --git a/tests/cmd/dev/conformance.go b/tests/cmd/dev/conformance.go index 8cbe86e..79114da 100644 --- a/tests/cmd/dev/conformance.go +++ b/tests/cmd/dev/conformance.go @@ -19,6 +19,11 @@ import ( "time" ) +// This is the reviewed conformance-register schema, not a language or report version. +const ( + conformanceSchemaVersion = 4 +) + const ( conformanceFull = "full" conformancePartial = "partial" @@ -755,18 +760,18 @@ func (c *conformanceCheck) test(id string, test conformanceTest) { return } - path, err := localPath(c.root, c.root, filepath.FromSlash(test.File)) + p, err := localPath(c.root, c.root, filepath.FromSlash(test.File)) if err != nil { c.problem(id, "invalid test path: "+err.Error()) return } - symbols, parsed := c.symbols[path] + symbols, parsed := c.symbols[p] if !parsed { symbols = make(map[string]struct{}) - c.symbols[path] = symbols + c.symbols[p] = symbols - file, err := parser.ParseFile(token.NewFileSet(), path, nil, 0) + file, err := parser.ParseFile(token.NewFileSet(), p, nil, 0) if err != nil { c.problem(id, "cannot parse test: "+err.Error()) return @@ -808,12 +813,12 @@ func (c *conformanceCheck) api(id, reference string) { return } - for _, path := range paths { - if strings.HasSuffix(path, "_test.go") { + for _, p := range paths { + if strings.HasSuffix(p, "_test.go") { continue } - file, err := parser.ParseFile(token.NewFileSet(), path, nil, 0) + file, err := parser.ParseFile(token.NewFileSet(), p, nil, 0) if err != nil { c.problem(id, "cannot parse API: "+err.Error()) return @@ -878,7 +883,7 @@ func checkConformanceRegister(root string, data []byte) (int, []error, error) { return 0, nil, errors.New("conformance register has trailing JSON") } - if register.SchemaVersion != 4 || len(register.Requirements) == 0 || len(register.Normative) == 0 || len(register.Profiles) == 0 || len(register.Documents) == 0 { + if register.SchemaVersion != conformanceSchemaVersion || len(register.Requirements) == 0 || len(register.Normative) == 0 || len(register.Profiles) == 0 || len(register.Documents) == 0 { return 0, nil, errors.New("conformance register requires schema 4 and nonempty scope, normative, profile and document inventories") } diff --git a/tests/cmd/dev/interop.go b/tests/cmd/dev/interop.go new file mode 100644 index 0000000..7b1d4b9 --- /dev/null +++ b/tests/cmd/dev/interop.go @@ -0,0 +1,75 @@ +package main + +import ( + "encoding/json" + "errors" + "fmt" + "io" + "os" +) + +// checkInteropLogs makes skipped or incomplete external checks fail the CI gate. +func checkInteropLogs(paths []string, out io.Writer) error { + if len(paths) == 0 { + return errors.New("interop-check requires go test -json log files") + } + + for _, path := range paths { + file, err := os.Open(path) // #nosec G304 G703 -- Explicit developer-selected local test log. + if err != nil { + return err + } + + count, checkErr := validateInteropLog(file) + if err = errors.Join(checkErr, file.Close()); err != nil { + return fmt.Errorf("%s: %w", path, err) + } + + if _, err = fmt.Fprintf(out, "%s: %d passed tests, zero skips\n", path, count); err != nil { + return err + } + } + + return nil +} + +func validateInteropLog(input io.Reader) (int, error) { + decoder := json.NewDecoder(input) + + var events []languageProofEvent + + passed := 0 + + for { + var event languageProofEvent + + err := decoder.Decode(&event) + if errors.Is(err, io.EOF) { + break + } + + if err != nil { + return 0, err + } + + if event.Action == goTestSkipEvent { + return 0, fmt.Errorf("external check skipped: %s/%s", event.Package, event.Test) + } + + if event.Action == "pass" && event.Test != "" { + passed++ + } + + events = append(events, event) + } + + if err := validateLanguageProofPackages(events); err != nil { + return 0, err + } + + if passed == 0 { + return 0, errors.New("no external tests passed") + } + + return passed, nil +} diff --git a/tests/cmd/dev/interop_test.go b/tests/cmd/dev/interop_test.go new file mode 100644 index 0000000..1dc82e6 --- /dev/null +++ b/tests/cmd/dev/interop_test.go @@ -0,0 +1,31 @@ +package main + +import ( + "strings" + "testing" +) + +func TestInteropCompletionGate(t *testing.T) { + start := `{"Action":"start","Package":"p"}` + "\n" + test := `{"Action":"pass","Package":"p","Test":"TestExternal"}` + "\n" + end := `{"Action":"pass","Package":"p"}` + "\n" + for _, tc := range []struct { + name, log string + valid bool + }{ + {"complete", start + test + end, true}, + {"skip", start + `{"Action":"skip","Package":"p","Test":"TestMissingEngine"}` + "\n" + end, false}, + {"failure", start + `{"Action":"fail","Package":"p"}`, false}, + {"truncated", start + test, false}, + {"empty", "", false}, + {"no-tests", start + end, false}, + {"malformed", start + test + end + "invalid", false}, + } { + t.Run(tc.name, func(t *testing.T) { + passed, err := validateInteropLog(strings.NewReader(tc.log)) + if (err == nil) != tc.valid || tc.valid && passed != 1 { + t.Fatalf("passed=%d error=%v", passed, err) + } + }) + } +} diff --git a/tests/cmd/dev/language_inputs.go b/tests/cmd/dev/language_inputs.go index b456b2f..e1f1e2d 100644 --- a/tests/cmd/dev/language_inputs.go +++ b/tests/cmd/dev/language_inputs.go @@ -15,6 +15,14 @@ import ( "strings" ) +// Preparation bounds subprocess parallelism and sets explicit capture/oracle deadlines. The cache directory is private to the user running this developer command. +const ( + languageCacheDirectoryMode = 0o700 + languagePrepareParallelism = 4 + languageCaptureTimeout = "-timeout=10m" + languageOracleTimeout = "-timeout=15m" +) + // Inputs and generated evidence belong to a checkout-specific external cache. // They are never inferred from an unrelated checkout or an old audit report. type languageInputs struct { @@ -85,7 +93,7 @@ func configureLanguageInputs(ctx context.Context, root string, out io.Writer) er return err } - if err := os.MkdirAll(directory, 0o700); err != nil { + if err := os.MkdirAll(directory, languageCacheDirectoryMode); err != nil { return err } @@ -207,11 +215,11 @@ func prepareLanguageInputs(ctx context.Context, root string, out, stderr io.Writ } streams := []string{filepath.Join(work, "native.json"), filepath.Join(work, "residuals.json")} - if err := command("proof", "capture", "-out", streams[0], "--", "-timeout=10m", "-skip=^Test(Language(NormativeEvidence|ResidualCoverage)|(QT3|XSLT)ResidualCauses)$", "./..."); err != nil { + if err := command("proof", "capture", "-out", streams[0], "--", languageCaptureTimeout, "-skip=^Test(Language(NormativeEvidence|ResidualCoverage)|(QT3|XSLT)ResidualCauses)$", "./..."); err != nil { return err } - if err := command("proof", "capture", "-out", streams[1], "--", "-timeout=10m", "-run=^Test(QT3|XSLT)ResidualCauses$", "./tests/cmd/dev"); err != nil { + if err := command("proof", "capture", "-out", streams[1], "--", languageCaptureTimeout, "-run=^Test(QT3|XSLT)ResidualCauses$", "./tests/cmd/dev"); err != nil { return err } @@ -226,7 +234,7 @@ func prepareLanguageInputs(ctx context.Context, root string, out, stderr io.Writ file := filepath.Join(work, fmt.Sprintf("interop-%d.json", i)) streams = append(streams, file) jobs = append(jobs, func() error { - return command("proof", "capture", "-out", file, "--", "-tags=interop", "-timeout=15m", "-run="+pattern, "./transform") + return command("proof", "capture", "-out", file, "--", "-tags=interop", languageOracleTimeout, "-run="+pattern, "./transform") }) } @@ -254,7 +262,7 @@ func prepareLanguageInputs(ctx context.Context, root string, out, stderr io.Writ } // Classified publisher failures remain visible in the report and are // checked by TestLanguageResidualCoverage, rather than hidden here. - err := command("suites", "-suite="+parts[0], "-dir="+checkout, "-version="+parts[1], "-xsd-version="+parts[2], "-limits="+parts[3], "-assertions="+parts[4], "-xpath10-compatibility="+parts[5], "-timeout=1s", "-report="+reports[i]) + err := command("suites", "-suite="+parts[0], "-dir="+checkout, "-version="+parts[1], "-xsd-version="+parts[2], "-limits="+parts[3], "-assertions="+parts[4], "-xpath10-compatibility="+parts[5], "-timeout="+suiteDefaultTimeout.String(), "-report="+reports[i]) var report suiteReport if readErr := readLanguageProofFile(reports[i], &report); readErr != nil || report.SourceSHA256 != stamp.SourceSHA256 { @@ -324,7 +332,7 @@ func languageIndependentPatterns(root string) ([]string, error) { return nil, errors.New("register contains no independent comparison tests") } - groups := make([][]string, min(4, len(ordered))) + groups := make([][]string, min(languagePrepareParallelism, len(ordered))) for i, name := range ordered { groups[i%len(groups)] = append(groups[i%len(groups)], regexp.QuoteMeta(name)) } diff --git a/tests/cmd/dev/main.go b/tests/cmd/dev/main.go index f4ef70c..006ec13 100644 --- a/tests/cmd/dev/main.go +++ b/tests/cmd/dev/main.go @@ -17,6 +17,15 @@ import ( "strings" ) +// The shared event/case threshold chooses the larger report write buffer. Schema versioning and buffer byte sizes describe different parts of evidence publication. +const ( + languageProofSchemaVersion = 1 + // Counts report cases/events; it changes buffering, not which entries are published. + reportStreamingThreshold = 128 + reportBufferBytes = 4 << 10 + reportStreamingBufferBytes = 32 << 10 +) + const goTestSkipEvent = "skip" func main() { @@ -34,7 +43,7 @@ func main() { func run(ctx context.Context, args []string, out, stderr io.Writer) error { if len(args) == 0 { - return errors.New("usage: go run ./tests/cmd/dev {docs|examples|fuzz|suites|proof} [options]") + return errors.New("usage: go run ./tests/cmd/dev {docs|examples|fuzz|suites|proof|interop-check} [options]") } root, err := repositoryRoot() @@ -57,6 +66,8 @@ func run(ctx context.Context, args []string, out, stderr io.Writer) error { return runSuites(ctx, root, args[1:], out, stderr) case "proof": return runLanguageProof(ctx, root, args[1:], out, stderr) + case "interop-check": + return checkInteropLogs(args[1:], out) default: return fmt.Errorf("unknown development command %q", args[0]) } @@ -131,7 +142,7 @@ type languageProofStream struct { } func (s languageProofStream) validate(stamp languageProofStamp) error { - if s.SchemaVersion != 1 || s.Before != stamp || s.After != stamp { + if s.SchemaVersion != languageProofSchemaVersion || s.Before != stamp || s.After != stamp { return errors.New("test stream has stale or changed source/register stamps") } @@ -153,7 +164,7 @@ type languageProofArtifact struct { } func (a languageProofArtifact) validate(stamp languageProofStamp) ([]languageProofEvent, error) { - if a.SchemaVersion != 1 || a.Stamp != stamp || len(a.Streams) == 0 { + if a.SchemaVersion != languageProofSchemaVersion || a.Stamp != stamp || len(a.Streams) == 0 { return nil, errors.New("proof artifact has stale stamps or no captured streams") } @@ -273,10 +284,10 @@ func writeLanguageProofFile(path string, value any) error { switch value := value.(type) { case languageProofStream: - large = len(value.Events) > 128 + large = len(value.Events) > reportStreamingThreshold case languageProofArtifact: for _, stream := range value.Streams { - large = large || len(stream.Events) > 128 + large = large || len(stream.Events) > reportStreamingThreshold } } @@ -353,9 +364,9 @@ func writeJSONFile(path string, large bool, write func(io.Writer) error) (result } }() - bufferSize := 4 << 10 + bufferSize := reportBufferBytes if large { - bufferSize = 32 << 10 + bufferSize = reportStreamingBufferBytes } buffered := bufio.NewWriterSize(file, bufferSize) @@ -520,7 +531,7 @@ func runLanguageProof(ctx context.Context, root string, args []string, out, stde return err } - artifact := languageProofArtifact{SchemaVersion: 1, Stamp: stamp} + artifact := languageProofArtifact{SchemaVersion: languageProofSchemaVersion, Stamp: stamp} if err := validateProofDestination(*path, flags.Args()); err != nil { return err @@ -564,7 +575,7 @@ func captureLanguageProof(ctx context.Context, root, path string, args []string, return err } - stream := languageProofStream{SchemaVersion: 1, Before: before, Arguments: append([]string{"test", "-json", "-count=1"}, args...), ExitCode: -1} + stream := languageProofStream{SchemaVersion: languageProofSchemaVersion, Before: before, Arguments: append([]string{"test", "-json", "-count=1"}, args...), ExitCode: -1} if err := invalidateLanguageProofFile(path); err != nil { return err diff --git a/tests/cmd/dev/main_test.go b/tests/cmd/dev/main_test.go index b9dc4d2..10c7e03 100644 --- a/tests/cmd/dev/main_test.go +++ b/tests/cmd/dev/main_test.go @@ -484,172 +484,6 @@ func TestConformanceCatalogPaths(t *testing.T) { } } -func conformanceFixture(t *testing.T, root string) []byte { - t.Helper() - writeFixture(t, root, "fixture/api.go", "package fixture\nfunc Apply() {}\n") - writeFixture(t, root, "fixture/doc.go", "// Package fixture supplies registry test data.\npackage fixture\n") - writeFixture(t, root, "fixture_test.go", "package fixture_test\nimport tst \"testing\"\nfunc TestPresent(t *tst.T) {}\n") - - return []byte(`{ - "schema_version":4,"reviewed_on":"2026-10-22","scope":"Test fixture","stage":"experimental","completeness":"Partial normative inventory", - "documents":[{"id":"standard","title":"Standard","edition":"Published edition","source":"https://example.test/standard","errata":{"sources":[],"review":"reviewed","reviewed_on":"2026-10-22","items":[],"notes":"No published corrections in this fixture."}}], - "profiles":[{"id":"profile","title":"Selected profile","role":"verifier","modes":["verify"],"claim":"restricted","inventory":"partial","documents":["standard"],"scope_ids":["supported"],"package_docs":["fixture/doc.go"],"gap":"Inventory remains partial.","closure":"Map all applicable clauses.","matrix":[{"document":"standard","modes":["verify"],"requirements":["norm"],"unmapped_clauses":["3.3: additional verifier rules"],"reason":"Selected verifier duties.","closure":"Assess section 3.3."}]}], - "requirements":[{"id":"supported","title":"Supported capability","status":"implemented","tests":["fixture_test.go"]},{"id":"unimplemented","title":"Open capability","status":"unimplemented","tests":[]}], - "normative_requirements":[{ - "id":"norm","statement":"Validate the input.","norm":{"document":"standard","clause":"3.2","source":"https://example.test/standard#verify","level":"MUST","errata":[]}, - "applies":{"profile":"profile","role":"verifier","mode":"verify","status":"applicable","reason":"Required for the selected verifier role."}, - "implementation":{"status":"implemented","apis":["fixture.Apply"],"code":["fixture/api.go"]}, - "tests":{"status":"covered","reason":"Exact local fixture cases.","positive":[{"file":"fixture_test.go","name":"TestPresent"}],"negative":[{"file":"fixture_test.go","name":"TestPresent"}]}, - "interop":{"status":"pending","tests":[],"fixtures":[],"reason":"Independent fixture pending."}, - "policy":{"default":{"status":"enabled","reason":"Within the default profile."},"compatibility":{"status":"not-applicable","reason":"No separate compatibility mode."}}, - "gap":"Independent evidence pending.","closure":"Add an independent positive/negative pair." - }] - }`) -} - -func writeFixture(t *testing.T, root, name, text string) { - t.Helper() - - path := filepath.Join(root, filepath.FromSlash(name)) - if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { - t.Fatal(err) - } - - if err := os.WriteFile(path, []byte(text), 0o600); err != nil { - t.Fatal(err) - } -} - -// Proof events come from configured go test -json runs. Requiring a matching -// source and register digest prevents a successful old run from closing new work. -type languageProofRun struct { - SourceSHA256 string `json:"source_sha256"` - RegisterSHA256 string `json:"register_sha256"` - Events []languageProofEvent `json:"events"` - index map[string]languageProofStatus -} - -func (r *languageProofRun) passed(check conformanceTest) error { - if r.index == nil { - r.index = make(map[string]languageProofStatus) - for _, event := range r.Events { - if event.Package != "github.com/skarm/xmlsec" && !strings.HasPrefix(event.Package, "github.com/skarm/xmlsec/") { - continue - } - directory := strings.TrimPrefix(event.Package, "github.com/skarm/xmlsec/") - if event.Package == "github.com/skarm/xmlsec" { - directory = "." - } - key := directory + "\x00" + event.Test - switch event.Action { - case "pass": - state := r.index[key] - state.passed = true - r.index[key] = state - case "output": - // An ancestor cannot borrow an unrelated descendant's oracle or vector. - state := r.index[key] - state.recordOutput(event.Output) - r.index[key] = state - case "skip", "fail": - // A group with any omitted or failing descendant cannot be cited as passed. - for name := event.Test; name != ""; { - key = directory + "\x00" + name - state := r.index[key] - state.invalid = event.Action + " at " + event.Test - r.index[key] = state - index := strings.LastIndex(name, "/") - if index < 0 { - break - } - name = name[:index] - } - } - } - } - name := check.Name + "/" + check.Case - key := filepath.ToSlash(filepath.Dir(check.File)) + "\x00" + name - state := r.index[key] - if state.invalid != "" { - return fmt.Errorf("%s has %s", name, state.invalid) - } - if !state.passed { - return fmt.Errorf("no successful execution of %s in %s", name, check.File) - } - return nil -} - -func (r *languageProofRun) independent(proof normativeProof) error { - if err := r.passed(proof.Check); err != nil { - return err - } - key := filepath.ToSlash(filepath.Dir(proof.Check.File)) + "\x00" + proof.Check.Name + "/" + proof.Check.Case - state := r.index[key] - if proof.Oracle == "published-vector" || proof.Oracle == "published-inventory" { - if proof.Expected == nil { - return fmt.Errorf("%s/%s has no published fixture value", proof.Check.Name, proof.Check.Case) - } - if _, exists := state.vectors[*proof.Expected]; !exists { - return fmt.Errorf("%s/%s does not verify published fixture value %q", proof.Check.Name, proof.Check.Case, *proof.Expected) - } - return nil - } - if _, exists := state.oracles[proof.Oracle]; !exists { - return fmt.Errorf("%s/%s has no exact %s agreement; diagnostics cannot close independent evidence", proof.Check.Name, proof.Check.Case, proof.Oracle) - } - if _, exists := state.diagnostics[proof.Oracle]; exists { - return fmt.Errorf("%s/%s has no exact %s agreement; diagnostics cannot close independent evidence", proof.Check.Name, proof.Check.Case, proof.Oracle) - } - return nil -} - -type languageProofStatus struct { - passed bool - invalid string - oracles map[string]struct{} - diagnostics map[string]struct{} - vectors map[string]struct{} -} - -func (s *languageProofStatus) recordOutput(output string) { - for _, marker := range []string{"P3_ORACLE ", "P3_DIAGNOSTIC ", "P3_VECTOR "} { - _, tail, found := strings.Cut(output, marker) - if !found { - continue - } - if marker == "P3_VECTOR " { - var expected string - if err := json.Unmarshal([]byte(strings.TrimSpace(tail)), &expected); err == nil { - if s.vectors == nil { - s.vectors = make(map[string]struct{}) - } - s.vectors[expected] = struct{}{} - } - continue - } - fields := strings.Fields(tail) - if len(fields) == 0 { - continue - } - if marker == "P3_ORACLE " { - if s.oracles == nil { - s.oracles = make(map[string]struct{}) - } - s.oracles[fields[0]] = struct{}{} - } else { - if s.diagnostics == nil { - s.diagnostics = make(map[string]struct{}) - } - s.diagnostics[fields[0]] = struct{}{} - } - } -} - -func languageProofPassed(events []languageProofEvent, check conformanceTest) error { - run := languageProofRun{Events: events} - return run.passed(check) -} - func TestLanguageProofEvents(t *testing.T) { wantTrue := "true" check := conformanceTest{File: "transform/xpath_test.go", Name: "TestXPath", Case: "numeric/overflow"} @@ -706,46 +540,6 @@ func TestLanguageProofEvents(t *testing.T) { } } -func languageEvidenceInputs(t *testing.T) (string, conformanceRegister, languageProofRun) { - t.Helper() - ledgerPath := residualRequired(t, "XMLSEC_P3_TEST_EVENTS") - root, err := repositoryRoot() - if err != nil { - t.Fatal(err) - } - raw, err := os.ReadFile(filepath.Join(root, "docs", "conformance.json")) - if err != nil { - t.Fatal(err) - } - var register conformanceRegister - if err := json.Unmarshal(raw, ®ister); err != nil { - t.Fatal(err) - } - _, problems, err := checkConformanceRegister(root, raw) - if err := errors.Join(append(problems, err)...); err != nil { - t.Fatal(err) - } - var artifact languageProofArtifact - if err := readLanguageProofFile(ledgerPath, &artifact); err != nil { - t.Fatal(err) - } - digest, err := suiteSourceDigest(t.Context(), root) - stamp := languageProofStamp{SourceSHA256: digest, RegisterSHA256: fmt.Sprintf("%x", sha256.Sum256(raw))} - events, artifactErr := artifact.validate(stamp) - if err != nil || artifactErr != nil { - t.Fatal("proof events do not belong to completed runs of the current sources and register", err, artifactErr) - } - run := languageProofRun{SourceSHA256: stamp.SourceSHA256, RegisterSHA256: stamp.RegisterSHA256, Events: events} - t.Cleanup(func() { - current, err := suiteSourceDigest(context.WithoutCancel(t.Context()), root) - currentRegister, readErr := os.ReadFile(filepath.Join(root, "docs", "conformance.json")) - if err != nil || readErr != nil || current != digest || run.RegisterSHA256 != fmt.Sprintf("%x", sha256.Sum256(currentRegister)) { - t.Error("sources or register changed during evidence verification", err, readErr) - } - }) - return root, register, run -} - func TestLanguageProofArtifacts(t *testing.T) { stamp := languageProofStamp{SourceSHA256: "current-source", RegisterSHA256: "current-register"} const module = "github.com/skarm/xmlsec/transform" @@ -979,20 +773,6 @@ func TestResidualFailureConditions(t *testing.T) { } } -// File confinement uses physical roots, including when /tmp or the explicit -// checkout directory is a symlink on the calling platform. -func languageProofDirectory(t *testing.T, variable string) string { - t.Helper() - directory, err := filepath.EvalSymlinks(residualRequired(t, variable)) - if err == nil { - directory, err = filepath.Abs(directory) - } - if err != nil { - t.Fatal(err) - } - return directory -} - func TestLanguageNormativeEvidence(t *testing.T) { configureLanguageTest(t, true) root, register, run := languageEvidenceInputs(t) @@ -1229,58 +1009,15 @@ func TestLanguageResidualCoverage(t *testing.T) { } } -func residualFailureMatches(evidence residualEvidence, profile string, item suiteOutcome) bool { - if evidence.Disposition == "fixed" || item.Kind == "panic" { - return false - } - return slices.ContainsFunc(evidence.Failures, func(condition residualFailureCondition) bool { - return condition.Profile == profile && condition.Kind == item.Kind && condition.Cause == item.FailureCause && condition.Signature == item.FailureSignature && slices.Contains(condition.ErrorCodes, item.ErrorCode) - }) -} - -func languageProofSetting[T comparable](value any, want T) bool { - raw, err := json.Marshal(value) +// Appendix D is a documentation duty: another processor need not choose the +// same host defaults. This checklist transcribes its eight REC 2.0 items and +// fourteen REC 3.0/3.1 items; it does not compare incompatible host APIs. +func TestXPathPublishedHostChoiceInventory(t *testing.T) { + root, err := repositoryRoot() if err != nil { - return false + t.Fatal(err) } - var actual T - decoder := json.NewDecoder(bytes.NewReader(raw)) - decoder.DisallowUnknownFields() - return decoder.Decode(&actual) == nil && actual == want -} - -func languageCatalogInventory(t *testing.T, directory string, pin suitePin) map[string]struct{} { - t.Helper() - catalog, _, err := readSuiteNode(directory, directory, pin.Catalog) - if err != nil { - t.Fatal(err) - } - inventory := make(map[string]struct{}) - for _, entry := range catalog.children("test-set") { - set, _, err := readSuiteReferencedNode(directory, directory, entry) - if err != nil { - t.Fatal(err) - } - for _, node := range set.children("test-case") { - key := entry.attr("file") + "/" + node.attr("name") - if _, exists := inventory[key]; exists { - t.Fatal("duplicate original catalog identity", key) - } - inventory[key] = struct{}{} - } - } - return inventory -} - -// Appendix D is a documentation duty: another processor need not choose the -// same host defaults. This checklist transcribes its eight REC 2.0 items and -// fourteen REC 3.0/3.1 items; it does not compare incompatible host APIs. -func TestXPathPublishedHostChoiceInventory(t *testing.T) { - root, err := repositoryRoot() - if err != nil { - t.Fatal(err) - } - raw, err := os.ReadFile(filepath.Join(root, "docs", "transforms.md")) + raw, err := os.ReadFile(filepath.Join(root, "docs", "transforms.md")) if err != nil { t.Fatal(err) } @@ -1337,3 +1074,266 @@ func TestXPathPublishedHostChoiceInventory(t *testing.T) { } } } + +func conformanceFixture(t *testing.T, root string) []byte { + t.Helper() + writeFixture(t, root, "fixture/api.go", "package fixture\nfunc Apply() {}\n") + writeFixture(t, root, "fixture/doc.go", "// Package fixture supplies registry test data.\npackage fixture\n") + writeFixture(t, root, "fixture_test.go", "package fixture_test\nimport tst \"testing\"\nfunc TestPresent(t *tst.T) {}\n") + + return []byte(`{ + "schema_version":4,"reviewed_on":"2026-10-22","scope":"Test fixture","stage":"experimental","completeness":"Partial normative inventory", + "documents":[{"id":"standard","title":"Standard","edition":"Published edition","source":"https://example.test/standard","errata":{"sources":[],"review":"reviewed","reviewed_on":"2026-10-22","items":[],"notes":"No published corrections in this fixture."}}], + "profiles":[{"id":"profile","title":"Selected profile","role":"verifier","modes":["verify"],"claim":"restricted","inventory":"partial","documents":["standard"],"scope_ids":["supported"],"package_docs":["fixture/doc.go"],"gap":"Inventory remains partial.","closure":"Map all applicable clauses.","matrix":[{"document":"standard","modes":["verify"],"requirements":["norm"],"unmapped_clauses":["3.3: additional verifier rules"],"reason":"Selected verifier duties.","closure":"Assess section 3.3."}]}], + "requirements":[{"id":"supported","title":"Supported capability","status":"implemented","tests":["fixture_test.go"]},{"id":"unimplemented","title":"Open capability","status":"unimplemented","tests":[]}], + "normative_requirements":[{ + "id":"norm","statement":"Validate the input.","norm":{"document":"standard","clause":"3.2","source":"https://example.test/standard#verify","level":"MUST","errata":[]}, + "applies":{"profile":"profile","role":"verifier","mode":"verify","status":"applicable","reason":"Required for the selected verifier role."}, + "implementation":{"status":"implemented","apis":["fixture.Apply"],"code":["fixture/api.go"]}, + "tests":{"status":"covered","reason":"Exact local fixture cases.","positive":[{"file":"fixture_test.go","name":"TestPresent"}],"negative":[{"file":"fixture_test.go","name":"TestPresent"}]}, + "interop":{"status":"pending","tests":[],"fixtures":[],"reason":"Independent fixture pending."}, + "policy":{"default":{"status":"enabled","reason":"Within the default profile."},"compatibility":{"status":"not-applicable","reason":"No separate compatibility mode."}}, + "gap":"Independent evidence pending.","closure":"Add an independent positive/negative pair." + }] + }`) +} + +func writeFixture(t *testing.T, root, name, text string) { + t.Helper() + + path := filepath.Join(root, filepath.FromSlash(name)) + if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { + t.Fatal(err) + } + + if err := os.WriteFile(path, []byte(text), 0o600); err != nil { + t.Fatal(err) + } +} + +// Proof events come from configured go test -json runs. Requiring a matching +// source and register digest prevents a successful old run from closing new work. +type languageProofRun struct { + SourceSHA256 string `json:"source_sha256"` + RegisterSHA256 string `json:"register_sha256"` + Events []languageProofEvent `json:"events"` + index map[string]languageProofStatus +} + +func (r *languageProofRun) passed(check conformanceTest) error { + if r.index == nil { + r.index = make(map[string]languageProofStatus) + for _, event := range r.Events { + if event.Package != "github.com/skarm/xmlsec" && !strings.HasPrefix(event.Package, "github.com/skarm/xmlsec/") { + continue + } + directory := strings.TrimPrefix(event.Package, "github.com/skarm/xmlsec/") + if event.Package == "github.com/skarm/xmlsec" { + directory = "." + } + key := directory + "\x00" + event.Test + switch event.Action { + case "pass": + state := r.index[key] + state.passed = true + r.index[key] = state + case "output": + // An ancestor cannot borrow an unrelated descendant's oracle or vector. + state := r.index[key] + state.recordOutput(event.Output) + r.index[key] = state + case "skip", "fail": + // A group with any omitted or failing descendant cannot be cited as passed. + for name := event.Test; name != ""; { + key = directory + "\x00" + name + state := r.index[key] + state.invalid = event.Action + " at " + event.Test + r.index[key] = state + index := strings.LastIndex(name, "/") + if index < 0 { + break + } + name = name[:index] + } + } + } + } + name := check.Name + "/" + check.Case + key := filepath.ToSlash(filepath.Dir(check.File)) + "\x00" + name + state := r.index[key] + if state.invalid != "" { + return fmt.Errorf("%s has %s", name, state.invalid) + } + if !state.passed { + return fmt.Errorf("no successful execution of %s in %s", name, check.File) + } + return nil +} + +func (r *languageProofRun) independent(proof normativeProof) error { + if err := r.passed(proof.Check); err != nil { + return err + } + key := filepath.ToSlash(filepath.Dir(proof.Check.File)) + "\x00" + proof.Check.Name + "/" + proof.Check.Case + state := r.index[key] + if proof.Oracle == "published-vector" || proof.Oracle == "published-inventory" { + if proof.Expected == nil { + return fmt.Errorf("%s/%s has no published fixture value", proof.Check.Name, proof.Check.Case) + } + if _, exists := state.vectors[*proof.Expected]; !exists { + return fmt.Errorf("%s/%s does not verify published fixture value %q", proof.Check.Name, proof.Check.Case, *proof.Expected) + } + return nil + } + if _, exists := state.oracles[proof.Oracle]; !exists { + return fmt.Errorf("%s/%s has no exact %s agreement; diagnostics cannot close independent evidence", proof.Check.Name, proof.Check.Case, proof.Oracle) + } + if _, exists := state.diagnostics[proof.Oracle]; exists { + return fmt.Errorf("%s/%s has no exact %s agreement; diagnostics cannot close independent evidence", proof.Check.Name, proof.Check.Case, proof.Oracle) + } + return nil +} + +type languageProofStatus struct { + passed bool + invalid string + oracles map[string]struct{} + diagnostics map[string]struct{} + vectors map[string]struct{} +} + +func (s *languageProofStatus) recordOutput(output string) { + for _, marker := range []string{"P3_ORACLE ", "P3_DIAGNOSTIC ", "P3_VECTOR "} { + _, tail, found := strings.Cut(output, marker) + if !found { + continue + } + if marker == "P3_VECTOR " { + var expected string + if err := json.Unmarshal([]byte(strings.TrimSpace(tail)), &expected); err == nil { + if s.vectors == nil { + s.vectors = make(map[string]struct{}) + } + s.vectors[expected] = struct{}{} + } + continue + } + fields := strings.Fields(tail) + if len(fields) == 0 { + continue + } + if marker == "P3_ORACLE " { + if s.oracles == nil { + s.oracles = make(map[string]struct{}) + } + s.oracles[fields[0]] = struct{}{} + } else { + if s.diagnostics == nil { + s.diagnostics = make(map[string]struct{}) + } + s.diagnostics[fields[0]] = struct{}{} + } + } +} + +func languageProofPassed(events []languageProofEvent, check conformanceTest) error { + run := languageProofRun{Events: events} + return run.passed(check) +} + +func languageEvidenceInputs(t *testing.T) (string, conformanceRegister, languageProofRun) { + t.Helper() + ledgerPath := residualRequired(t, "XMLSEC_P3_TEST_EVENTS") + root, err := repositoryRoot() + if err != nil { + t.Fatal(err) + } + raw, err := os.ReadFile(filepath.Join(root, "docs", "conformance.json")) + if err != nil { + t.Fatal(err) + } + var register conformanceRegister + if err := json.Unmarshal(raw, ®ister); err != nil { + t.Fatal(err) + } + _, problems, err := checkConformanceRegister(root, raw) + if err := errors.Join(append(problems, err)...); err != nil { + t.Fatal(err) + } + var artifact languageProofArtifact + if err := readLanguageProofFile(ledgerPath, &artifact); err != nil { + t.Fatal(err) + } + digest, err := suiteSourceDigest(t.Context(), root) + stamp := languageProofStamp{SourceSHA256: digest, RegisterSHA256: fmt.Sprintf("%x", sha256.Sum256(raw))} + events, artifactErr := artifact.validate(stamp) + if err != nil || artifactErr != nil { + t.Fatal("proof events do not belong to completed runs of the current sources and register", err, artifactErr) + } + run := languageProofRun{SourceSHA256: stamp.SourceSHA256, RegisterSHA256: stamp.RegisterSHA256, Events: events} + t.Cleanup(func() { + current, err := suiteSourceDigest(context.WithoutCancel(t.Context()), root) + currentRegister, readErr := os.ReadFile(filepath.Join(root, "docs", "conformance.json")) + if err != nil || readErr != nil || current != digest || run.RegisterSHA256 != fmt.Sprintf("%x", sha256.Sum256(currentRegister)) { + t.Error("sources or register changed during evidence verification", err, readErr) + } + }) + return root, register, run +} + +// File confinement uses physical roots, including when /tmp or the explicit +// checkout directory is a symlink on the calling platform. +func languageProofDirectory(t *testing.T, variable string) string { + t.Helper() + directory, err := filepath.EvalSymlinks(residualRequired(t, variable)) + if err == nil { + directory, err = filepath.Abs(directory) + } + if err != nil { + t.Fatal(err) + } + return directory +} + +func residualFailureMatches(evidence residualEvidence, profile string, item suiteOutcome) bool { + if evidence.Disposition == "fixed" || item.Kind == "panic" { + return false + } + return slices.ContainsFunc(evidence.Failures, func(condition residualFailureCondition) bool { + return condition.Profile == profile && condition.Kind == item.Kind && condition.Cause == item.FailureCause && condition.Signature == item.FailureSignature && slices.Contains(condition.ErrorCodes, item.ErrorCode) + }) +} + +func languageProofSetting[T comparable](value any, want T) bool { + raw, err := json.Marshal(value) + if err != nil { + return false + } + var actual T + decoder := json.NewDecoder(bytes.NewReader(raw)) + decoder.DisallowUnknownFields() + return decoder.Decode(&actual) == nil && actual == want +} + +func languageCatalogInventory(t *testing.T, directory string, pin suitePin) map[string]struct{} { + t.Helper() + catalog, _, err := readSuiteNode(directory, directory, pin.Catalog) + if err != nil { + t.Fatal(err) + } + inventory := make(map[string]struct{}) + for _, entry := range catalog.children("test-set") { + set, _, err := readSuiteReferencedNode(directory, directory, entry) + if err != nil { + t.Fatal(err) + } + for _, node := range set.children("test-case") { + key := entry.attr("file") + "/" + node.attr("name") + if _, exists := inventory[key]; exists { + t.Fatal("duplicate original catalog identity", key) + } + inventory[key] = struct{}{} + } + } + return inventory +} diff --git a/tests/cmd/dev/run.go b/tests/cmd/dev/run.go index 2ca3585..5d6513e 100644 --- a/tests/cmd/dev/run.go +++ b/tests/cmd/dev/run.go @@ -16,6 +16,15 @@ import ( "time" ) +// Fuzz duration, worker selection and timeout overhead are developer-run controls; example subprocesses have an independent deadline. +const ( + exampleTimeout = time.Minute + defaultFuzzDuration = 5 * time.Second + minimumFuzzTimeout = 2 * time.Minute + fuzzTimeoutOverhead = time.Minute + languageFuzzParallelism = "-parallel=2" +) + func listPackages(ctx context.Context, root, pattern string, stderr io.Writer) ([]goPackage, error) { cmd := exec.CommandContext(ctx, "go", "list", "-json", pattern) // #nosec G204 -- Package patterns come from fixed repository tasks. cmd.Dir, cmd.Stderr = root, stderr @@ -74,7 +83,7 @@ func runExamples(ctx context.Context, root string, args []string, out, stderr io } command := []string{"run", p.ImportPath} - limit, cancel := context.WithTimeout(ctx, time.Minute) + limit, cancel := context.WithTimeout(ctx, exampleTimeout) err := runGo(limit, root, out, stderr, nil, command...) cancel() @@ -160,7 +169,7 @@ func discoverFuzz(ctx context.Context, root, pattern string, stderr io.Writer) ( func runFuzz(ctx context.Context, root string, args []string, out, stderr io.Writer) error { fs := flag.NewFlagSet("fuzz", flag.ContinueOnError) fs.SetOutput(stderr) - duration := fs.Duration("time", 5*time.Second, "fuzz budget per target") + duration := fs.Duration("time", defaultFuzzDuration, "fuzz budget per target") pattern := fs.String("match", ".*", "regular expression for target names") list := fs.Bool("list", false, "list selected targets without running tests") @@ -199,8 +208,8 @@ func runFuzz(ctx context.Context, root string, args []string, out, stderr io.Wri continue } - timeout := max(2*time.Minute, *duration+time.Minute) - if err := runGo(ctx, root, out, stderr, env, "test", target.Package, "-run=^$", "-fuzz=^"+target.Name+"$", "-fuzztime="+duration.String(), "-parallel=2", "-timeout="+timeout.String()); err != nil { + timeout := max(minimumFuzzTimeout, *duration+fuzzTimeoutOverhead) + if err := runGo(ctx, root, out, stderr, env, "test", target.Package, "-run=^$", "-fuzz=^"+target.Name+"$", "-fuzztime="+duration.String(), languageFuzzParallelism, "-timeout="+timeout.String()); err != nil { return err } } diff --git a/tests/cmd/dev/run_test.go b/tests/cmd/dev/run_test.go index 45807c2..220c09d 100644 --- a/tests/cmd/dev/run_test.go +++ b/tests/cmd/dev/run_test.go @@ -1483,1070 +1483,1083 @@ func TestSuiteSourceOptions(t *testing.T) { } } -// residualCaseResult retains typed errors; a report's explanatory string alone -// cannot establish that a failure was caused by a configured resource limit. -type residualCaseResult struct { - Evaluation *suiteEvaluation - Environment *preparedSuiteEnvironment - PrepareError error - AssertionError error -} - -func (r residualCaseResult) executionError() error { - if r.PrepareError != nil { - return r.PrepareError +func TestQT3ResidualCauses(t *testing.T) { + configureLanguageTest(t, false) + directory, err := filepath.EvalSymlinks(residualRequired(t, "XMLSEC_QT3_DIR")) + if err != nil { + t.Fatal(err) + } + residualRequired(t, "SAXON_CLASSPATH") + residualRequired(t, "SAXON20_CLASSPATH") + residualRequired(t, "SERIALIZATION_SCHEMA_DIR") + proofs := make([]qtResidualProof, 0, len(qtResidualCases())) + t.Cleanup(func() { + if file := os.Getenv("XMLSEC_QT3_PROOF_REPORT"); file != "" { + data, err := json.MarshalIndent(proofs, "", " ") + if err != nil { + t.Error(err) + return + } + if err := os.WriteFile(file, append(data, '\n'), 0o600); err != nil { + t.Error(err) + } + } + }) + for _, test := range qtResidualCases() { + t.Run(test.name, func(t *testing.T) { + item := loadPinnedResidualCase(t, directory, suiteQT3, test.file, test.name) + options := suiteOptions{kind: suiteQT3, directory: directory, version: test.version, xsdVersion: suiteVersion10, timeout: time.Second, limitsProfile: "default"} + original := evaluateResidualCase(t, options, item) + data, err := os.ReadFile(filepath.Join(directory, test.file)) + if err != nil { + t.Fatal(err) + } + digest := sha256.Sum256(data) + // The configured case budget covers evaluation and its original + // publisher assertion; retain the typed cause from either phase. + resourceCause := errors.Join(original.executionError(), original.AssertionError) + proof := qtResidualProof{Name: test.name, File: test.file, Version: test.version, Category: test.category, XSDVersion: suiteXSDVersionName(options), CaseTimeout: options.timeout.String(), LimitsProfile: options.limitsProfile, SourceSHA256: hex.EncodeToString(digest[:]), OriginalLimit: errors.Is(resourceCause, transform.ErrLimit), OriginalError: fmt.Sprint(original.executionError()), OriginalAssertion: fmt.Sprint(original.AssertionError)} + proof.OriginalDeadline = errors.Is(resourceCause, context.DeadlineExceeded) + defer func() { proofs = append(proofs, proof) }() + switch test.category { + case "limit": + if !proof.OriginalLimit && !proof.OriginalDeadline { + t.Fatal("original cause is neither a typed implementation limit nor the configured deadline", original.executionError()) + } + qtResidualLimitControls(t, options, item, &proof) + case "format": + qtResidualFormatControls(t, options, item, original, &proof) + case "calendar-month-week": + qtResidualMonthWeekControls(t, options, item, original, &proof) + case "serialize": + qtResidualSerializationControls(t, options, item, original, &proof) + case "transform43": + qtResidualTransform43Controls(t, original, &proof) + } + }) } - return r.Evaluation.err } -func residualRequired(t *testing.T, name string) string { +func residualSaxonQuery(t *testing.T, expression, source string) string { t.Helper() - value := os.Getenv(name) - if value == "" { - if os.Getenv("XMLSEC_REQUIRE_P3_PROOFS") == "1" { - t.Fatal("required proof input is missing:", name) - } - t.Skip("set " + name + " to run pinned external causal proofs") + classpath := residualRequired(t, "SAXON_CLASSPATH") + binary, err := exec.LookPath("java") + if err != nil { + t.Fatal(err) } - return value + ctx, cancel := context.WithTimeout(t.Context(), 20*time.Second) + defer cancel() + queryFile := filepath.Join(t.TempDir(), "proof.xq") + // Inputs are XPath expressions. XQuery resolves entity references inside + // string literals; quote ampersands once so Saxon sees the same strings. + if err := os.WriteFile(queryFile, []byte(strings.ReplaceAll(expression, "&", "&")), 0o600); err != nil { + t.Fatal(err) + } + arguments := []string{"-cp", classpath, "net.sf.saxon.Query", "-q:" + queryFile, "!method=text", "!omit-xml-declaration=yes"} + if source != "" { + arguments = append(arguments, "-s:"+source) + } + command := exec.CommandContext(ctx, binary, arguments...) + var diagnostic bytes.Buffer + command.Stderr = &diagnostic + output, err := command.Output() + if err != nil { + t.Fatalf("independent query: %s: %v", diagnostic.String(), err) + } + return string(output) } -func loadPinnedResidualCase(t *testing.T, directory, kind, file, name string) suiteCase { +func residualSaxon20Expression(t *testing.T, expression string) string { t.Helper() - pinsData, err := os.ReadFile(filepath.Join("..", "..", "..", "build", "w3c-suites.json")) + classpath := residualRequired(t, "SAXON20_CLASSPATH") + binary, err := exec.LookPath("java") if err != nil { t.Fatal(err) } - var pins map[string]suitePin - if err := json.Unmarshal(pinsData, &pins); err != nil { + directory := t.TempDir() + file := filepath.Join(directory, "proof.xsl") + var escaped strings.Builder + if err := xml.EscapeText(&escaped, []byte(expression)); err != nil { t.Fatal(err) } - pin := pins[kind] - if err := verifySuiteCheckout(t.Context(), directory, pin); err != nil { + source := `` + if err := os.WriteFile(file, []byte(source), 0o600); err != nil { t.Fatal(err) } - catalog, _, err := readSuiteNode(directory, directory, pin.Catalog) + command := exec.CommandContext(t.Context(), binary, "-cp", classpath, "net.sf.saxon.Transform", "-xsl:"+file, "-it:start") + output, err := command.CombinedOutput() if err != nil { - t.Fatal(err) + t.Fatalf("independent old-edition transform: %s: %v", output, err) } - set, path, err := readSuiteNode(directory, directory, file) + return string(output) +} + +func residualControl(t *testing.T, proof *qtResidualProof, environment *preparedSuiteEnvironment, version, name, expression, want, obligation string) { + t.Helper() + value, err := residualXPath(t, environment, version, expression) + actual := residualSequenceText(t, value) if err != nil { - t.Fatal(err) + actual = residualErrorCode(err) } - listed := slices.ContainsFunc(catalog.children("test-set"), func(entry *suiteNode) bool { return entry.attr(suiteFile) == file }) - if !listed || catalog.XMLName.Space != pin.Namespace || set.XMLName.Space != pin.Namespace { - t.Fatal("proof case is not listed in the pinned catalog", file) + if actual != want { + t.Fatal(name, "native", actual, err, "want", want) } - for _, node := range set.children("test-case") { - if node.attr("name") == name { - return suiteCase{ - set: set.attr("name"), file: file, base: filepath.Dir(path), setNode: set, node: node, - environments: suiteEnvironments(set, filepath.Dir(path), suiteEnvironments(catalog, directory, nil)), - } - } + oracleExpression := `try { (` + expression + `) } catch * { local-name-from-QName($err:code) }` + independent := residualSaxonQuery(t, oracleExpression, "") + if independent != want { + t.Fatal(name, "independent", independent, "want", want) } - t.Fatal("pinned case missing", kind, file, name) - return suiteCase{} + proof.Controls = append(proof.Controls, qtResidualProofControl{Name: name, Expression: expression, Native: actual, Independent: independent, Obligation: obligation}) } -func evaluateResidualCase(t *testing.T, options suiteOptions, item suiteCase) residualCaseResult { +func qtResidualLimitControls(t *testing.T, options suiteOptions, item suiteCase, proof *qtResidualProof) { t.Helper() - ctx := t.Context() - if options.timeout > 0 { - bounded, cancel := context.WithTimeout(ctx, options.timeout) - defer cancel() - ctx = bounded + options.limitsProfile = suiteLanguage + options.timeout = 30 * time.Second + environment, err := prepareSuiteEnvironment(t.Context(), options, item) + if err != nil { + t.Fatal(err) } - if status, reason := suiteApplicability(options, item); status != "" { - t.Fatal("proof must execute an applicable original case", status, reason) + if proof.Name == "ForExpr013" { + control := evaluateResidualCase(t, options, item) + if control.executionError() != nil || control.AssertionError != nil { + t.Fatal("raised bounded profile did not reproduce publisher value", control.executionError(), control.AssertionError) + } + evaluation := control.Evaluation + expression := "count((" + requiredSuiteChild(t, item.node, "test").Text + "))" + want := strconv.Itoa(evaluation.sequence.Len()) + oracle := residualSaxonQuery(t, expression, filepath.Join(options.directory, "prod", "ForClause", "fsx.xml")) + if oracle != want { + t.Fatal(oracle, want) + } + proof.Controls = append(proof.Controls, qtResidualProofControl{Name: "same source and publisher assertion; explicit language profile and 30s case deadline", Expression: expression, Native: want, Independent: oracle, Obligation: "XPath20 2.3.1 implementation limits; exact unmodified publisher input/output"}) + return } - environment, err := prepareSuiteEnvironment(ctx, options, item) - if err != nil { - return residualCaseResult{PrepareError: err} + if proof.Name == "fn-transform-27" { + qtResidualTransform27Controls(t, options, item, environment, proof) + return } - evaluation := evaluateSuiteCase(ctx, options, item, environment) - return residualCaseResult{ - Evaluation: evaluation, Environment: environment, - AssertionError: checkSuiteAssertion(ctx, evaluation, item.node.child(suiteResultVariable)), + expression, want := "", "" + switch proof.Name { + case "cbcl-codepoints-to-string-005": + expression, want = `string-length(codepoints-to-string(65536 to 65539))`, "4" + case "cbcl-count-001": + expression, want = `count(1 to 1000)`, "1000" + case "cbcl-matches-004": + expression, want = `matches('aaa','a{4}')`, "false" + case "cbcl-subsequence-010": + expression, want = `count(subsequence(1 to 1000,-5,4))`, "0" + case "cbcl-subsequence-011": + expression, want = `count(subsequence(1 to 1000,-5))`, "1000" + case "cbcl-subsequence-012": + expression, want = `count(subsequence(1 to 1000,748))`, "253" + case "cbcl-subsequence-013": + expression, want = `count(subsequence(1 to 1000,747))`, "254" + case "cbcl-subsequence-014": + expression, want = `subsequence(2147483647 to 2147483651,1,5)`, "2147483647 2147483648 2147483649 2147483650 2147483651" + case "environment-variable-012": + expression, want = strings.ReplaceAll(requiredSuiteChild(t, item.node, "test").Text, "1024 * 1024", "1000"), "true" + case "fold-left-020", "fold-right-013": + expression, want = strings.ReplaceAll(requiredSuiteChild(t, item.node, "test").Text, "1000000", "1000"), "1000" + case "map-for-each-014", "map-keys-014": + expression, want = `let $result := (`+strings.ReplaceAll(requiredSuiteChild(t, item.node, "test").Text, "500000", "1000")+`) return count($result)=1000 and $result=1 and $result=1000`, "true" + case "map-remove-012": + expression, want = `let $result := (`+strings.NewReplacer("500000", "1000", "123456", "456").Replace(requiredSuiteChild(t, item.node, "test").Text)+`) return map:size($result)=999 and map:contains($result,1) and map:contains($result,1000) and not(map:contains($result,456))`, "true" + default: + t.Fatal("missing case-specific limit control", proof.Name) } -} - -func cloneResidualNode(node *suiteNode) *suiteNode { - if node == nil { - return nil + residualControl(t, proof, environment, options.version, "bounded same-operator control", expression, want, "XPath implementation-limit permission; the operator succeeds with independently verified bounded operands") + if proof.Name == "cbcl-codepoints-to-string-005" { + residualControl(t, proof, environment, options.version, "semantic invalid scalar remains FOCH0001", `codepoints-to-string(1114112)`, "FOCH0001", "FO 5.2.1 invalid Unicode codepoint error independent of range materialization") } - cloned := *node - cloned.Attributes = slices.Clone(node.Attributes) - cloned.namespaces = make(map[string]string, len(node.namespaces)) - maps.Copy(cloned.namespaces, node.namespaces) - cloned.Children = make([]*suiteNode, len(node.Children)) - for index, child := range node.Children { - cloned.Children[index] = cloneResidualNode(child) + if proof.Name == "cbcl-matches-004" { + residualControl(t, proof, environment, options.version, "actual invalid regex remains FORX0002", `matches('aaa','a{')`, "FORX0002", "FO matches invalid grammar differs from valid enormous quantifier implementation cap") } - return &cloned } -func residualErrorCode(err error) string { - if detail, ok := errors.AsType[*transform.Error](err); ok { - return detail.Code +func requiredSuiteChild(t *testing.T, node *suiteNode, name string) *suiteNode { + t.Helper() + child := node.child(name) + if child == nil { + t.Fatalf("required fixture element %q missing", name) } - return "" + return child } -func residualSequenceText(t *testing.T, value transform.Sequence) string { +func qtResidualTransform27Controls(t *testing.T, options suiteOptions, item suiteCase, environment *preparedSuiteEnvironment, proof *qtResidualProof) { t.Helper() - parts := make([]string, 0, value.Len()) - for index := range value.Len() { - item, ok := value.At(index) - if !ok { - t.Fatal("missing result", index) - } - part, err := item.Text() - if err != nil { - t.Fatal(err) + original := evaluateSuiteCase(t.Context(), options, item, environment) + if original.err != nil || residualSequenceText(t, original.sequence) != "true" { + t.Fatal("raised original case", original.err, residualSequenceText(t, original.sequence)) + } + proof.Controls = append(proof.Controls, qtResidualProofControl{Name: "original expression with raised bounded profile and fixed host clock", Native: "true", Obligation: "FO31 16.3.2 cache option has no effect on transformation result; repeated results may be identical"}) + bounded := strings.NewReplacer("-100000", "-100", "+100000", "+100").Replace(requiredSuiteChild(t, item.node, "test").Text) + for _, cache := range []string{"false()", "true()"} { + expression := strings.ReplaceAll(bounded, `"cache":false()`, `"cache":`+cache) + value, err := residualXPath(t, environment, suiteVersion31, expression) + if err != nil || residualSequenceText(t, value) != "true" { + t.Fatal(cache, value, err) } - parts = append(parts, part) + proof.Controls = append(proof.Controls, qtResidualProofControl{Name: "fixed host clock, cache hint " + cache, Expression: expression, Native: "true", Obligation: "FO31 16.3.2 cache is an efficiency hint, not a promise of unequal current-dateTime values"}) } - return strings.Join(parts, " ") -} - -func residualXPath(t *testing.T, environment *preparedSuiteEnvironment, version, expression string) (transform.Sequence, error) { - t.Helper() - selected := transform.XPath31 - switch version { - case suiteVersion20: - selected = transform.XPath20 - case suiteVersion30: - selected = transform.XPath30 - } - query, err := environment.engine.CompileXPath(t.Context(), expression, transform.XPathOptions{Version: selected, Namespaces: environment.namespaces, BaseURI: environment.baseURI, DefaultCollation: environment.collation, DecimalFormats: environment.decimalFormats}) - if err != nil { - return transform.Sequence{}, err - } - return query.Evaluate(t.Context(), environment.document, transform.XPathContext{Variables: environment.variables, ContextItem: environment.contextItem, Context: suiteDynamicContext(environment.currentTime)}) -} - -type qtResidualProofControl struct { - Name string `json:"name"` - Expression string `json:"expression,omitempty"` - Native string `json:"native"` - Independent string `json:"independent,omitempty"` - Obligation string `json:"obligation"` -} - -type qtResidualProof struct { - Name string - File string - Version string - Category string - XSDVersion string `json:"xsd_version"` - CaseTimeout string `json:"case_timeout"` - LimitsProfile string `json:"limits_profile"` - SourceSHA256 string `json:"source_sha256"` - OriginalLimit bool `json:"original_typed_limit"` - OriginalDeadline bool `json:"original_typed_deadline"` - OriginalError string `json:"original_error,omitempty"` - OriginalAssertion string `json:"original_assertion_error,omitempty"` - Controls []qtResidualProofControl `json:"controls"` -} - -// This table identifies original publisher cases, not replacement expectations. -// The test keeps their non-passing assertions visible and separately checks why. -// The original publisher serialization regex remains untouched. These controls -// distinguish required character/namespace semantics from a particular spelling. -func runXSLTResidualSerialization(t *testing.T, directory string) { - for _, test := range []struct { - name string - file string - kind string - }{ - {"attribute-0301", "tests/insn/attribute/_attribute-test-set.xml", "uri"}, - {"output-0102c", "tests/decl/output/_output-test-set.xml", "xml-attribute"}, - {"output-0103c", "tests/decl/output/_output-test-set.xml", "xml-attribute"}, - {"output-0602a", "tests/decl/output/_output-test-set.xml", "namespace"}, - {"output-0603a", "tests/decl/output/_output-test-set.xml", "namespace"}, - {"output-0724", "tests/decl/output/_output-test-set.xml", "uri"}, - } { - t.Run(test.name, func(t *testing.T) { - item := loadPinnedResidualCase(t, directory, suiteXSLT, test.file, test.name) - options := suiteOptions{kind: suiteXSLT, directory: directory, version: suiteVersion30, xsdVersion: suiteVersion10, timeout: time.Second, limitsProfile: "default"} - original := evaluateResidualCase(t, options, item) - if original.executionError() != nil || original.AssertionError == nil { - t.Fatal("unchanged original residual", original.executionError(), original.AssertionError) - } - actual, err := original.Evaluation.serialized(t.Context()) - if err != nil { - t.Fatal(err) - } - proof := qtResidualProof{Name: test.name, File: test.file, Version: suiteVersion30, Category: "permitted-outcome"} - switch test.kind { - case "xml-attribute": - want := `% %C2%96 %C2%96 a " %C2%A1 < > & end` - if test.name == "output-0103c" { - want = "% %C2%96 \u0096 a \" ¡ < > & end" - } - file := filepath.Join(t.TempDir(), "actual.xml") - if err := os.WriteFile(file, actual, 0o600); err != nil { - t.Fatal(err) - } - oracle := residualSaxonQuery(t, `string(doc('`+suiteFileURI(file)+`')/*/@href)`, "") - if oracle != want { - t.Fatal("independent XML parsing changed attribute meaning", oracle, want) - } - residualControl(t, &proof, original.Environment, suiteVersion31, "legal XML attribute quote spellings", `parse-xml('')/a/@href eq parse-xml('')/a/@href`, "true", "XML10 2.3/4.6 permit named and numeric quote references") - residualControl(t, &proof, original.Environment, suiteVersion31, "legal literal and escaped greater-than", `parse-xml('')/a/@href eq parse-xml('')/a/@href`, "true", "XML10 attribute value grammar permits literal greater-than; SER character expansion must preserve value") - t.Log("P3_ORACLE published-vector") - case "uri": - if test.name == "attribute-0301" { - residualLegacyAttribute301(t, directory, item, options) - } - originalValue := "père" - if test.name == "output-0724" { - originalValue = "✈" - } - want := residualSaxonQuery(t, `iri-to-uri('`+originalValue+`')`, "") - values := residualHTMLInputValues(t, actual) - if len(values) == 0 { - t.Fatal("no serialized input/@value") - } - for _, value := range values { - if value != want { - t.Fatal("published URI table escaping", value, want) - } - } - controlled := residualSerializationVariant(t, options, item, original.Environment, func(raw []byte) []byte { - if !bytes.Contains(raw, []byte(`method="html"`)) { - t.Fatal("publisher output method changed") - } - return bytes.ReplaceAll(raw, []byte(`method="html"`), []byte(`method="html" escape-uri-attributes="no"`)) - }) - decoded := residualHTMLInputValues(t, controlled) - if len(decoded) != len(values) { - t.Fatal("controlled input count", decoded, values) - } - for _, value := range decoded { - if value != originalValue { - t.Fatal("explicit escaping=no must retain original value", value, originalValue) - } - } - t.Log("P3_ORACLE published-vector") - case "namespace": - originalBinding, originalAttribute := residualHTMLNamespaceState(t, actual) - if !originalBinding || test.name == "output-0603a" && !originalAttribute { - t.Fatal("ancestor namespace is not bound", originalBinding, originalAttribute, string(actual)) - } - controlled := residualXHTMLNamespaceControl(t, options, item, original.Environment) - bodyBinding, attribute := residualHTMLNamespaceState(t, controlled) - if bodyBinding || test.name == "output-0603a" && !attribute { - t.Fatal("XHTML prefix-normalization intervention", bodyBinding, attribute, string(controlled)) - } - residualControl(t, &proof, original.Environment, suiteVersion31, "ancestor/local declarations bind the same expanded attribute", `deep-equal(parse-xml('')/body/mi/@*,parse-xml('')/body/mi/@*)`, "true", "Namespaces in XML 1.0 6.2 namespace scope; SER31 HTML 7.1 differs from XHTML global prefix normalization") - t.Log("P3_ORACLE published-vector") - } - }) + residualControl(t, proof, environment, suiteVersion31, "current-dateTime is stable within one dynamic context", `current-dateTime() eq current-dateTime()`, "true", "FO31 15.3 same dynamic current-dateTime; host controls the supplied instant") + // A fresh public invocation can receive another explicit host clock. This + // isolates clock input from stylesheet caching without sleeping or wall time. + clockExpression := `transform(map{'stylesheet-text':"",'initial-template':xs:QName('start'),'cache':false()})?output/string()` + for _, instant := range []string{"2026-01-01T00:00:00Z", "2026-01-02T00:00:00Z"} { + environment.currentTime, _ = time.Parse(time.RFC3339, instant) + value, err := residualXPath(t, environment, suiteVersion31, clockExpression) + if err != nil || residualSequenceText(t, value) != instant { + t.Fatal(value, err, instant) + } + proof.Controls = append(proof.Controls, qtResidualProofControl{Name: "explicit independent host clock " + instant, Expression: clockExpression, Native: instant, Obligation: "Configured per-invocation clock, not the cache hint, determines static current-dateTime here"}) } } -func residualXHTMLNamespaceControl(t *testing.T, options suiteOptions, item suiteCase, environment *preparedSuiteEnvironment) []byte { +func residualInlineBindings(t *testing.T, expression string, environment *preparedSuiteEnvironment) string { t.Helper() - invalid := evaluateXSLTResidualSourceVariant(t, options, item, `method="html"`, `method="xhtml"`) - if got := residualErrorCode(invalid.executionError()); got != "SESU0013" { - t.Fatal("changing only the method retains unsupported XML version 5.0", got, invalid.executionError()) - } - // XHTML has separate HTML and XML version parameters. The pinned HTML5 - // declaration uses version 5.0; change that output attribute, never the - // stylesheet's independent version 3.0 attribute, when selecting XHTML5. - const before = `method="html" version="5.0" html-version="5.0"` - const after = `method="xhtml" version="1.0" html-version="5.0"` - var source []byte - controlled := residualSerializationVariant(t, options, item, environment, func(raw []byte) []byte { - if bytes.Count(raw, []byte(before)) != 1 { - t.Fatal("the pinned output's HTML5 version tuple changed") + for name, value := range environment.variables { + if name.Space != "" { + t.Fatal("proof needs an explicit namespace binding", name) } - source = bytes.Replace(raw, []byte(before), []byte(after), 1) - return source - }) - main := filepath.Join(t.TempDir(), "xhtml5.xsl") - if err := os.WriteFile(main, source, 0o600); err != nil { - t.Fatal(err) - } - _, input := xsltResidualInputBytes(t, options, item) - oracle, err := xsltResidualOracleTransform(t, suiteVersion30, main, input, "", "") - if err != nil { - t.Fatal(err) - } - nativeBinding, nativeAttribute := residualHTMLNamespaceState(t, controlled) - oracleBinding, oracleAttribute := residualHTMLNamespaceState(t, oracle) - if nativeBinding != oracleBinding || nativeAttribute != oracleAttribute { - t.Fatal("independent XHTML5 namespace intervention", nativeBinding, nativeAttribute, oracleBinding, oracleAttribute, string(oracle)) + item, err := value.Single() + if err != nil { + t.Fatal(err) + } + text, err := item.Text() + if err != nil { + t.Fatal(err) + } + expression = strings.ReplaceAll(expression, "$"+name.Local, "xs:"+item.Type().Local+"('"+strings.ReplaceAll(text, "'", "''")+"')") } - t.Log("P3_ORACLE saxon-12.10") - return controlled + return expression } -func residualSerializationVariant(t *testing.T, options suiteOptions, item suiteCase, environment *preparedSuiteEnvironment, edit func([]byte) []byte) []byte { +func qtResidualFormatControls(t *testing.T, options suiteOptions, item suiteCase, original residualCaseResult, proof *qtResidualProof) { t.Helper() - raw, compileOptions, err := suiteStylesheetSource(options, environment) - if err != nil { - t.Fatal(err) + if original.executionError() != nil || original.AssertionError == nil { + t.Fatal("expected original publisher value mismatch", original.executionError(), original.AssertionError) } - changed := edit(slices.Clone(raw)) - if bytes.Equal(raw, changed) { - t.Fatal("causal intervention did not change the input") + old := map[string]string{"format-dateTime-002g": "9:15:06.5", "format-dateTime-002h": "9:15:06.5", "format-dateTime-002i": "9:15:06.46", "format-dateTime-003m": "9:15:06.5", "format-dateTime-003n": "9:15:06.5", "format-dateTime-003p": "9:15:06.46", "format-dateTime-013s": "46", "format-dateTime-013u": "46", "millisecs-018": "12", "millisecs-022": "00"}[proof.Name] + if residualSequenceText(t, original.Evaluation.sequence) != old { + t.Fatal("old-edition algorithm changed", original.Evaluation.sequence, old) } - compiled, err := environment.engine.CompileXSLT(t.Context(), changed, compileOptions) - if err != nil { - t.Fatal(err) + expression := residualInlineBindings(t, requiredSuiteChild(t, item.node, "test").Text, original.Environment) + oracle := residualSaxon20Expression(t, expression) + if oracle != old { + t.Fatal("published old-formatting algorithm disagreement", oracle, old) } - result, err := evaluateCompiledXSLTSuite(t.Context(), item, environment, compiled) - if err != nil { - t.Fatal(err) + proof.Controls = append(proof.Controls, qtResidualProofControl{Name: "same literal date/picture under pre-3.1 formatting", Expression: expression, Native: old, Independent: oracle, Obligation: "FO30 9.8.4.1 old rounding/width precedence; FO31 9.8.4.5 truncation is a later algorithm"}) + options.version = suiteVersion31 + modern := evaluateResidualCase(t, options, item) + if modern.executionError() != nil || modern.AssertionError != nil { + t.Fatal("same pinned fixture must match its later-edition expectation", modern.executionError(), modern.AssertionError) } - output, err := result.Serialize(t.Context()) - if err != nil { - t.Fatal(err) + want := residualSequenceText(t, modern.Evaluation.sequence) + oracle = residualSaxonQuery(t, expression, "") + if oracle != want { + t.Fatal("modern oracle", oracle, want) } - return output.Primary + proof.Controls = append(proof.Controls, qtResidualProofControl{Name: "same publisher input/assertion under processor 3.1", Expression: expression, Native: want, Independent: oracle, Obligation: "FO31 9.8.4.5 explicit fractional-second/width algorithm; publisher modifications in 2016 reference bugs 29749/29788"}) } -func residualLegacyAttribute301(t *testing.T, directory string, item suiteCase, options suiteOptions) { +func qtResidualTransform43Controls(t *testing.T, original residualCaseResult, proof *qtResidualProof) { t.Helper() - options.version = suiteVersion20 - original := evaluateResidualCase(t, options, item) - if original.executionError() != nil || original.AssertionError != nil { - t.Fatal("selected SER20 must satisfy every unchanged publisher assertion", original.executionError(), original.AssertionError) + if original.executionError() != nil || !strings.Contains(fmt.Sprint(original.AssertionError), "FODC0006") { + t.Fatal("expected assertion's document parse error", original.executionError(), original.AssertionError) } - classpath := residualRequired(t, "SAXON20_CLASSPATH") - binary, err := exec.LookPath("java") - if err != nil { - t.Fatal(err) + assertions := requiredSuiteChild(t, original.Evaluation.item.node.child(suiteResultVariable), "all-of").Children + if len(assertions) != 4 { + t.Fatal("publisher assertion inventory changed") } - identity, err := exec.CommandContext(t.Context(), binary, "-cp", classpath, "net.sf.saxon.Version").CombinedOutput() - if err != nil || !strings.Contains(string(identity), "SAXON 9.4.0.9") { - t.Fatal("independent old processor identity", string(identity), err) + for index := range 3 { + if err := checkSuiteAssertion(t.Context(), original.Evaluation, assertions[index]); err != nil { + t.Fatal(index, err) + } } - style := filepath.Join(directory, "tests", "insn", "attribute", "attribute-0301.xsl") - input := filepath.Join(directory, "tests", "insn", "attribute", "attribute-03.xml") - output, err := exec.CommandContext(t.Context(), binary, "-cp", classpath, "net.sf.saxon.Transform", "-xsl:"+style, "-s:"+input).Output() + value, err := original.Evaluation.sequence.Single() if err != nil { - if failure, ok := errors.AsType[*exec.ExitError](err); ok { - t.Fatalf("original Saxon9.4: %s: %v", failure.Stderr, err) - } t.Fatal(err) } - for _, assertion := range item.node.child(suiteResultVariable).child("all-of").Children { - if assertion.XMLName.Local != "serialization-matches" { - t.Fatal("original assertion type changed", assertion.XMLName) - } - value, err := original.Environment.query(t.Context(), `matches($s,$pattern,$flags)`, transform.Bindings{ - {Local: "s"}: transform.Values(transform.String(string(output))), {Local: "pattern"}: transform.Values(transform.String(assertion.Text)), {Local: "flags"}: transform.Values(transform.String(assertion.attr("flags"))), - }) - if err != nil || residualSequenceText(t, value) != "true" { - t.Fatal("independent unchanged original assertion", assertion.Text, string(output), err) - } + keys, err := value.Keys() + if err != nil || len(keys) != 4 { + t.Fatal(keys, err) } - t.Log("P3_ORACLE saxon-9.4") -} - -func residualHTMLInputValues(t *testing.T, data []byte) []string { - t.Helper() - // A trailing lexical boundary lets encoding/xml close a final HTML void - // element; it is not part of the original bytes or an asserted result. - decoder := xml.NewDecoder(io.MultiReader(bytes.NewReader(data), strings.NewReader("<__end/>"))) - decoder.Strict = false - decoder.AutoClose = xml.HTMLAutoClose - decoder.Entity = xml.HTMLEntity - var values []string - for { - token, err := decoder.Token() + principal := "" + for _, key := range keys { + name, err := key.Text() if err != nil { - if errors.Is(err, io.EOF) { - break - } - t.Fatal(err) } - if start, ok := token.(xml.StartElement); ok && start.Name.Local == "input" { - for _, attribute := range start.Attr { - if attribute.Name.Local == "value" { - values = append(values, attribute.Value) - } + if strings.HasSuffix(name, "/output.html") { + part, found := value.Lookup(key) + if !found { + t.Fatal(name) } + principal = residualSequenceText(t, part) } } - return values + if principal == "" { + t.Fatal("missing principal serialization") + } + environment := original.Environment + literal := "'" + strings.ReplaceAll(principal, "'", "''") + "'" + residualControl(t, proof, environment, suiteVersion31, "principal is a valid empty external parsed entity", `count(parse-xml-fragment(`+literal+`)/node())`, "0", "SER31 5 XML external parsed entity; empty principal need not be a well-formed XML document") + residualControl(t, proof, environment, suiteVersion31, "the same principal is not an XML document", `parse-xml(`+literal+`)`, "FODC0006", "FO31 14.7.1 parse-xml requires a document element") + residualControl(t, proof, environment, suiteVersion31, "quantifier error binding visited first", `boolean(some $s in ('','

Header for section 2

') satisfies parse-xml($s)//h1='Header for section 2')`, "FODC0006", "XPath31 3.13 permits a satisfies error before the true witness; boolean does not catch errors") + residualControl(t, proof, environment, suiteVersion31, "quantifier witness visited first", `some $s in ('

Header for section 2

','') satisfies parse-xml($s)//h1='Header for section 2'`, "true", "XPath31 3.13 true witness is an allowed outcome; map keys have implementation-dependent order") + filtered, err := environment.query(t.Context(), `some $key in map:keys($result)[contains(.,'section')] satisfies parse-xml($result?($key))//h1='Header for section 2'`, transform.Bindings{{Local: "result"}: original.Evaluation.sequence}) + if err != nil || residualSequenceText(t, filtered) != "true" { + t.Fatal("secondary map entry was lost", filtered, err) + } + proof.Controls = append(proof.Controls, qtResidualProofControl{Name: "unchanged result map contains the required secondary document", Native: "true; first three exact catalog assertions also pass", Obligation: "FO31 16.3.2 serialized result map preserves secondary results; no reordering or publisher assertion rewrite"}) } -func residualHTMLNamespaceState(t *testing.T, data []byte) (bool, bool) { +func qtResidualSerializationControls(t *testing.T, options suiteOptions, item suiteCase, original residualCaseResult, proof *qtResidualProof) { t.Helper() - decoder := xml.NewDecoder(bytes.NewReader(data)) - decoder.Strict = false - decoder.AutoClose = xml.HTMLAutoClose - decoder.Entity = xml.HTMLEntity - bodyBinding, attribute := false, false - for { - token, err := decoder.Token() - if err != nil { - if errors.Is(err, io.EOF) { - break - } - - t.Fatal(err) + if original.AssertionError == nil { + t.Fatal("the original catalog assertion unexpectedly passed") + } + for _, version := range []string{suiteVersion30, suiteVersion31} { + options.version = version + value := evaluateResidualCase(t, options, item) + want := "" + if version == suiteVersion30 { + want = "SEPM0017" + } else if proof.Name == "serialize-xml-025b" { + want = "SEPM0019" } - if start, ok := token.(xml.StartElement); ok { - for _, a := range start.Attr { - if start.Name.Local == "body" && a.Name.Space == "xmlns" && a.Name.Local == "svg" && a.Value == "http://www.w3.org/2000/svg" { - bodyBinding = true - } - if start.Name.Local == "mi" && a.Name.Space == "http://www.w3.org/2000/svg" && a.Name.Local == "att" && a.Value == "34" { - attribute = true - } + code := residualErrorCode(value.executionError()) + if code != want || want == "" && value.executionError() != nil { + t.Fatal("edition-specific extraction", version, value.executionError(), want) + } + actual := code + if want == "" { + actual = residualSequenceText(t, value.Evaluation.sequence) + if !strings.Contains(actual, "atomic") { + t.Fatal("source document was not serialized", actual) } } + proof.Controls = append(proof.Controls, qtResidualProofControl{Name: "same source and parameter document under " + version, Native: actual, Obligation: "SER30 3.1 assesses the whole parameter document; SER31 3.1 first selects known standard parameter children; duplicate QNames have their own SEPM0019 rule"}) } - return bodyBinding, attribute -} - -func qtResidualCases() []struct { - name string - file string - version string - category string -} { - return []struct { - name string - file string - version string - category string - }{ - {"ForExpr013", "prod/ForClause.xml", "2.0", "limit"}, - {"cbcl-codepoints-to-string-005", "fn/codepoints-to-string.xml", "2.0", "limit"}, - {"cbcl-count-001", "fn/count.xml", "2.0", "limit"}, - {"cbcl-matches-004", "fn/matches.xml", "2.0", "limit"}, - {"cbcl-subsequence-010", "fn/subsequence.xml", "2.0", "limit"}, - {"cbcl-subsequence-011", "fn/subsequence.xml", "2.0", "limit"}, - {"cbcl-subsequence-012", "fn/subsequence.xml", "2.0", "limit"}, - {"cbcl-subsequence-013", "fn/subsequence.xml", "2.0", "limit"}, - {"cbcl-subsequence-014", "fn/subsequence.xml", "2.0", "limit"}, - {"environment-variable-012", "fn/environment-variable.xml", "3.1", "limit"}, - {"fn-transform-27", "fn/transform.xml", "3.1", "limit"}, - {"fn-transform-43", "fn/transform.xml", "3.1", "transform43"}, - {"fold-left-020", "fn/fold-left.xml", "3.1", "limit"}, - {"fold-right-013", "fn/fold-right.xml", "3.1", "limit"}, - {"format-dateTime-002g", "fn/format-dateTime.xml", "3.0", "format"}, - {"format-dateTime-002h", "fn/format-dateTime.xml", "3.0", "format"}, - {"format-dateTime-002i", "fn/format-dateTime.xml", "3.0", "format"}, - {"format-dateTime-003m", "fn/format-dateTime.xml", "3.0", "format"}, - {"format-dateTime-003n", "fn/format-dateTime.xml", "3.0", "format"}, - {"format-dateTime-003p", "fn/format-dateTime.xml", "3.0", "format"}, - {"format-dateTime-013s", "fn/format-dateTime.xml", "3.0", "format"}, - {"format-dateTime-013u", "fn/format-dateTime.xml", "3.0", "format"}, - {"map-for-each-014", "map/for-each.xml", "3.1", "limit"}, - {"map-keys-014", "map/keys.xml", "3.1", "limit"}, - {"map-remove-012", "map/remove.xml", "3.1", "limit"}, - {"millisecs-018", "fn/format-time.xml", "3.0", "format"}, - {"millisecs-022", "fn/format-time.xml", "3.0", "format"}, - {"serialize-xml-007b", "fn/serialize.xml", "3.1", "serialize"}, - {"serialize-xml-007c", "fn/serialize.xml", "3.0", "serialize"}, - {"serialize-xml-015b", "fn/serialize.xml", "3.1", "serialize"}, - {"serialize-xml-016b", "fn/serialize.xml", "3.1", "serialize"}, - {"serialize-xml-020b", "fn/serialize.xml", "3.1", "serialize"}, - {"serialize-xml-025b", "fn/serialize.xml", "3.0", "serialize"}, - {"serialize-xml-026b", "fn/serialize.xml", "3.0", "serialize"}, + parameterFile := "" + for _, source := range item.node.child("environment").children("source") { + if source.attr("role") == "$params" { + _, parameterFile, _ = readSuiteReferencedFile(options.directory, item.base, source) + } } -} - -func TestQT3ResidualCauses(t *testing.T) { - configureLanguageTest(t, false) - directory, err := filepath.EvalSymlinks(residualRequired(t, "XMLSEC_QT3_DIR")) - if err != nil { - t.Fatal(err) + if parameterFile == "" { + t.Fatal("publisher parameter resource missing") } - residualRequired(t, "SAXON_CLASSPATH") - residualRequired(t, "SAXON20_CLASSPATH") - residualRequired(t, "SERIALIZATION_SCHEMA_DIR") - proofs := make([]qtResidualProof, 0, len(qtResidualCases())) - t.Cleanup(func() { - if file := os.Getenv("XMLSEC_QT3_PROOF_REPORT"); file != "" { - data, err := json.MarshalIndent(proofs, "", " ") + for _, edition := range []struct { + version string + checksum string + }{ + {"30", "1cbcc49ac5ed8942ba58276699254469ca2144e229f68faeeb48f784c1037953"}, + {"31", "256736a2f4e7923e888047dee757c17bc3ee707e285f00639fd8fb1d962d11b0"}, + } { + schema := filepath.Join(residualRequired(t, "SERIALIZATION_SCHEMA_DIR"), "ser"+edition.version+".xsd") + data, err := os.ReadFile(schema) + if err != nil { + t.Fatal(err) + } + digest := sha256.Sum256(data) + if hex.EncodeToString(digest[:]) != edition.checksum { + t.Fatal("published schema pin changed", schema) + } + parts := []string{} + if edition.version == "30" { + raw, err := os.ReadFile(parameterFile) if err != nil { - t.Error(err) - return + t.Fatal(err) } - if err := os.WriteFile(file, append(data, '\n'), 0o600); err != nil { - t.Error(err) + parts = append(parts, string(raw)) + } else { + declared, _, err := decodeSuiteNode(data, schema) + if err != nil { + t.Fatal(err) + } + names := []string{} + for _, element := range declared.children("element") { + if name := element.attr("name"); name != "" && name != "serialization-parameters" { + names = append(names, "'"+name+"'") + } + } + if len(names) < 20 { + t.Fatal("published serialization declaration inventory", len(names)) + } + // Selection names come from the independent published XSD, rather + // than the engine's parameter registry. Saxon performs the projection. + expression := `serialize(array{for $p in doc('` + suiteFileURI(parameterFile) + `')/*/*[namespace-uri()='http://www.w3.org/2010/xslt-xquery-serialization'][local-name()=(` + strings.Join(names, ",") + `)] return serialize($p,map{'method':'xml','omit-xml-declaration':true()})},map{'method':'json'})` + selected := residualSaxonQuery(t, expression, "") + if err := json.Unmarshal([]byte(selected), &parts); err != nil { + t.Fatal("independent projection result", selected, err) } } - }) - for _, test := range qtResidualCases() { - t.Run(test.name, func(t *testing.T) { - item := loadPinnedResidualCase(t, directory, suiteQT3, test.file, test.name) - options := suiteOptions{kind: suiteQT3, directory: directory, version: test.version, xsdVersion: suiteVersion10, timeout: time.Second, limitsProfile: "default"} - original := evaluateResidualCase(t, options, item) - data, err := os.ReadFile(filepath.Join(directory, test.file)) + valid := true + for _, part := range parts { + binary, err := exec.LookPath("xmllint") if err != nil { t.Fatal(err) } - digest := sha256.Sum256(data) - // The configured case budget covers evaluation and its original - // publisher assertion; retain the typed cause from either phase. - resourceCause := errors.Join(original.executionError(), original.AssertionError) - proof := qtResidualProof{Name: test.name, File: test.file, Version: test.version, Category: test.category, XSDVersion: suiteXSDVersionName(options), CaseTimeout: options.timeout.String(), LimitsProfile: options.limitsProfile, SourceSHA256: hex.EncodeToString(digest[:]), OriginalLimit: errors.Is(resourceCause, transform.ErrLimit), OriginalError: fmt.Sprint(original.executionError()), OriginalAssertion: fmt.Sprint(original.AssertionError)} - proof.OriginalDeadline = errors.Is(resourceCause, context.DeadlineExceeded) - defer func() { proofs = append(proofs, proof) }() - switch test.category { - case "limit": - if !proof.OriginalLimit && !proof.OriginalDeadline { - t.Fatal("original cause is neither a typed implementation limit nor the configured deadline", original.executionError()) + command := exec.CommandContext(t.Context(), binary, "--nonet", "--noout", "--schema", schema, "-") + command.Stdin = strings.NewReader(part) + output, err := command.CombinedOutput() + if err != nil { + var exit *exec.ExitError + if !errors.As(err, &exit) || exit.ExitCode() != 3 { + t.Fatalf("published XSD validator: %s: %v; selected part %q", output, err, part) } - qtResidualLimitControls(t, options, item, &proof) - case "format": - qtResidualFormatControls(t, options, item, original, &proof) - case "serialize": - qtResidualSerializationControls(t, options, item, original, &proof) - case "transform43": - qtResidualTransform43Controls(t, original, &proof) + valid = false } - }) - } + } + want := edition.version == "31" + if valid != want { + t.Fatal("published schema assessment", edition.version, valid, want) + } + t.Log("P3_ORACLE published-schema") + proof.Controls = append(proof.Controls, qtResidualProofControl{Name: "published SER" + edition.version + " schema after prescribed extraction", Native: strconv.FormatBool(valid), Independent: fmt.Sprintf("xmllint validity=%t; selected parts=%d; schema SHA256=%s", valid, len(parts), edition.checksum), Obligation: "Published SER30/31 3.1 parameter-document extraction and associated XSD; no extension schema declarations supplied"}) + } + if proof.Name == "serialize-xml-025b" { + data, err := os.ReadFile(parameterFile) + if err != nil { + t.Fatal(err) + } + expression := `let $p:=parse-xml('` + strings.ReplaceAll(string(data), "'", "''") + `')/*/* return count($p)=2 and node-name($p[1]) eq node-name($p[2])` + residualControl(t, proof, original.Environment, suiteVersion31, "duplicate foreign QName exists independently of schema validity", expression, "true", "SER31 3.1 duplicate parameter QName SEPM0019; SER30 also has a simultaneous whole-document SEPM0017") + } } -func residualSaxonQuery(t *testing.T, expression, source string) string { +// The original publisher case is always executed before changing its selected +// processor. The second run retains the same fixture, parameters and assertion. +type xsltResidualEditionCase struct { + name string + file string + version string + code string +} + +func xsltResidualEditionCases() []xsltResidualEditionCase { + return []xsltResidualEditionCase{ + {"backwards-046", "tests/misc/backwards/_backwards-test-set.xml", "2.0", "XTSE0080"}, + {"character-map-026", "tests/decl/character-map/_character-map-test-set.xml", "2.0", "XTSE0020"}, + {"character-map-901", "tests/decl/character-map/_character-map-test-set.xml", "2.0", "XTSE0080"}, + {"choose-0202", "tests/insn/choose/_choose-test-set.xml", "2.0", "XTSE0080"}, + {"collations-0128", "tests/misc/collations/_collations-test-set.xml", "2.0", "XTSE0010"}, + {"collection-005", "tests/fn/collection/_collection-test-set.xml", "2.0", "XTSE0010"}, + {"doe-0409", "tests/attr/disable-output-escaping/_disable-output-escaping-test-set.xml", "2.0", "XTSE0080"}, + {"error-0600a", "tests/misc/error/_error-test-set.xml", "3.0", "XTTE0590"}, + {"error-0720a", "tests/misc/error/_error-test-set.xml", "3.0", "XTDE0640"}, + {"error-0720b", "tests/misc/error/_error-test-set.xml", "3.0", "XTDE0640"}, + {"error-0720c", "tests/misc/error/_error-test-set.xml", "3.0", "XTDE0640"}, + {"error-0720d", "tests/misc/error/_error-test-set.xml", "3.0", "XTDE0640"}, + {"error-1370a", "tests/misc/error/_error-test-set.xml", "3.0", "XPDY0002"}, + {"error-1380a", "tests/misc/error/_error-test-set.xml", "3.0", "XPDY0002"}, + {"for-each-group-084", "tests/insn/for-each-group/_for-each-group-test-set.xml", "2.0", "XPST0003"}, + {"for-each-group-091", "tests/insn/for-each-group/_for-each-group-test-set.xml", "2.0", "XTSE0080"}, + {"function-0302", "tests/decl/function/_function-test-set.xml", "2.0", "XPST0003"}, + {"function-1902", "tests/decl/function/_function-test-set.xml", "2.0", "XTSE0090"}, + {"id-041", "tests/fn/id/_id-test-set.xml", "2.0", "XPST0003"}, + {"match-248", "tests/attr/match/_match-test-set.xml", "2.0", "XPST0003"}, + {"match-254", "tests/attr/match/_match-test-set.xml", "2.0", "XPST0003"}, + {"namespace-0912", "tests/type/namespace/_namespace-test-set.xml", "2.0", "XTTE0570"}, + {"number-1004", "tests/insn/number/_number-test-set.xml", "2.0", "XTSE0010"}, + {"output-0725", "tests/decl/output/_output-test-set.xml", "2.0", "XTSE0090"}, + {"output-0726", "tests/decl/output/_output-test-set.xml", "2.0", "XTSE0090"}, + {"result-document-0286", "tests/insn/result-document/_result-document-test-set.xml", "2.0", "XTSE0020"}, + {"result-document-0287", "tests/insn/result-document/_result-document-test-set.xml", "2.0", "XTSE0020"}, + {"result-document-1006", "tests/insn/result-document/_result-document-test-set.xml", "2.0", "XPST0017"}, + {"sequence-0132", "tests/insn/sequence/_sequence-test-set.xml", "2.0", "XTSE0010"}, + {"system-property-022", "tests/fn/system-property/_system-property-test-set.xml", "2.0", "XTSE0010"}, + {"variable-4802", "tests/decl/variable/_variable-test-set.xml", "2.0", ""}, + } +} + +func evaluateXSLTResidualVariant(t *testing.T, options suiteOptions, item suiteCase) residualCaseResult { t.Helper() - classpath := residualRequired(t, "SAXON_CLASSPATH") - binary, err := exec.LookPath("java") + // Some source fixtures declare a 2.0-only dependency while containing + // 3.0 syntax. This explicit controlled run bypasses applicability alone; + // it retains the pinned stylesheet and the complete publisher assertion. + environment, err := prepareSuiteEnvironment(t.Context(), options, item) if err != nil { - t.Fatal(err) - } - ctx, cancel := context.WithTimeout(t.Context(), 20*time.Second) - defer cancel() - queryFile := filepath.Join(t.TempDir(), "proof.xq") - // Inputs are XPath expressions. XQuery resolves entity references inside - // string literals; quote ampersands once so Saxon sees the same strings. - if err := os.WriteFile(queryFile, []byte(strings.ReplaceAll(expression, "&", "&")), 0o600); err != nil { - t.Fatal(err) - } - arguments := []string{"-cp", classpath, "net.sf.saxon.Query", "-q:" + queryFile, "!method=text", "!omit-xml-declaration=yes"} - if source != "" { - arguments = append(arguments, "-s:"+source) + return residualCaseResult{PrepareError: err} } - command := exec.CommandContext(ctx, binary, arguments...) - var diagnostic bytes.Buffer - command.Stderr = &diagnostic - output, err := command.Output() - if err != nil { - t.Fatalf("independent query: %s: %v", diagnostic.String(), err) + evaluation := evaluateSuiteCase(t.Context(), options, item, environment) + return residualCaseResult{ + Evaluation: evaluation, Environment: environment, + AssertionError: checkSuiteAssertion(t.Context(), evaluation, item.node.child(suiteResultVariable)), } - return string(output) } -func residualSaxon20Expression(t *testing.T, expression string) string { - t.Helper() - classpath := residualRequired(t, "SAXON20_CLASSPATH") - binary, err := exec.LookPath("java") +func TestXSLTResidualCauses(t *testing.T) { + configureLanguageTest(t, false) + configured := os.Getenv("XMLSEC_XSLT_DIR") + if configured == "" { + configured = residualRequired(t, "XMLSEC_XSLT30_DIR") + } + directory, err := filepath.EvalSymlinks(configured) if err != nil { t.Fatal(err) } - directory := t.TempDir() - file := filepath.Join(directory, "proof.xsl") - var escaped strings.Builder - if err := xml.EscapeText(&escaped, []byte(expression)); err != nil { - t.Fatal(err) + runXSLTResidualCompetingErrors(t, directory) + runXSLTResidualLimits(t, directory) + runXSLTResidualCalendarMonthWeek(t, directory) + runXSLTResidualMetadataControls(t, directory) + runXSLTResidualSingleCorrections(t, directory) + runXSLTResidualRegexEditions(t, directory) + runXSLTResidualExplicitResources(t, directory) + runXSLTResidualSerialization(t, directory) + runXSLTResidualWhitespace(t, directory) + runXSLTResidualWarnings(t, directory) + runXSLTResidualOptionalLibraries(t, directory) + runXSLTResidualPackageCorrections(t, directory) + runXSLTResidualTypeProfiles(t, directory) + runXSLTResidualPriming(t, directory) + runXSLTResidualEvaluateLibrary(t, directory) + runXSLTResidualJSONTyping(t, directory) + runXSLTResidualNumberProfiles(t, directory) + runXSLTResidualNamedOutput(t, directory) + for _, test := range xsltResidualEditionCases() { + t.Run(test.name, func(t *testing.T) { + item := loadPinnedResidualCase(t, directory, suiteXSLT, test.file, test.name) + options := suiteOptions{ + kind: suiteXSLT, directory: directory, version: test.version, + xsdVersion: suiteVersion10, timeout: time.Second, limitsProfile: "default", + } + t.Run("original", func(t *testing.T) { + original := evaluateResidualCase(t, options, item) + if original.AssertionError == nil { + t.Fatal("the unchanged publisher assertion no longer reproduces the residual") + } + if got := residualErrorCode(original.executionError()); got != test.code { + t.Fatal("typed original error", got, original.executionError(), test.code) + } + if test.name == "variable-4802" { + value, err := original.Evaluation.assertionValue(t.Context(), `exists(//out[. = '{.}'])`, nil) + if err != nil || residualSequenceText(t, value) != suiteTrue { + t.Fatal("2.0 must preserve the unexpanded text-template literal", value, err) + } + } + }) + t.Run("matching-edition", func(t *testing.T) { + var controlled residualCaseResult + switch test.name { + case "function-0302": + controlled = evaluateXSLTResidualSourceVariant(t, options, item, `$zls || 'value-of'`, `concat($zls, 'value-of')`) + case "function-1902": + controlled = evaluateXSLTResidualSourceVariant(t, options, item, ` expand-text="yes"`, "") + default: + options.version = suiteVersion30 + if test.version == suiteVersion30 { + options.version = suiteVersion20 + } + controlled = evaluateXSLTResidualVariant(t, options, item) + } + if controlled.PrepareError != nil || controlled.AssertionError != nil { + if test.name == "function-1902" && controlled.Evaluation != nil { + names, _ := controlled.Evaluation.assertionValue(t.Context(), `string-join(//incorrectly-available-function/concat(@name,'#',@arity),',')`, nil) + t.Log("functions available after grammar-only correction", residualSequenceText(t, names)) + } + t.Fatal("matching selected edition must satisfy the unchanged assertion", controlled.executionError(), controlled.AssertionError) + } + }) + }) } - source := `` - if err := os.WriteFile(file, []byte(source), 0o600); err != nil { - t.Fatal(err) + t.Run("collection-006", func(t *testing.T) { + item := loadPinnedResidualCase(t, directory, suiteXSLT, "tests/fn/collection/_collection-test-set.xml", "collection-006") + options := suiteOptions{ + kind: suiteXSLT, directory: directory, version: suiteVersion30, + xsdVersion: suiteVersion10, timeout: time.Second, limitsProfile: "default", + } + t.Run("original-30", func(t *testing.T) { + result := evaluateResidualCase(t, options, item) + if result.executionError() != nil || result.AssertionError != nil { + t.Fatal("fixed package-local collection views must pass the exact original", result.executionError(), result.AssertionError) + } + }) + t.Run("original-20", func(t *testing.T) { + options.version = suiteVersion20 + result := evaluateResidualCase(t, options, item) + if result.AssertionError == nil || residualErrorCode(result.executionError()) != "XTSE0080" { + t.Fatal("XSLT20 reserves the XSLT namespace even when a source 3.0 module names its initial template", result.executionError(), result.AssertionError) + } + }) + runXSLTResidualCollectionEdition(t, options, item) + }) +} + +// residualCaseResult retains typed errors; a report's explanatory string alone +// cannot establish that a failure was caused by a configured resource limit. +type residualCaseResult struct { + Evaluation *suiteEvaluation + Environment *preparedSuiteEnvironment + PrepareError error + AssertionError error +} + +func (r residualCaseResult) executionError() error { + if r.PrepareError != nil { + return r.PrepareError } - command := exec.CommandContext(t.Context(), binary, "-cp", classpath, "net.sf.saxon.Transform", "-xsl:"+file, "-it:start") - output, err := command.CombinedOutput() - if err != nil { - t.Fatalf("independent old-edition transform: %s: %v", output, err) + return r.Evaluation.err +} + +func residualRequired(t *testing.T, name string) string { + t.Helper() + value := os.Getenv(name) + if value == "" { + if os.Getenv("XMLSEC_REQUIRE_P3_PROOFS") == "1" { + t.Fatal("required proof input is missing:", name) + } + t.Skip("set " + name + " to run pinned external causal proofs") } - return string(output) + return value } -func residualControl(t *testing.T, proof *qtResidualProof, environment *preparedSuiteEnvironment, version, name, expression, want, obligation string) { +func loadPinnedResidualCase(t *testing.T, directory, kind, file, name string) suiteCase { t.Helper() - value, err := residualXPath(t, environment, version, expression) - actual := residualSequenceText(t, value) + pinsData, err := os.ReadFile(filepath.Join("..", "..", "..", "build", "w3c-suites.json")) if err != nil { - actual = residualErrorCode(err) + t.Fatal(err) } - if actual != want { - t.Fatal(name, "native", actual, err, "want", want) + var pins map[string]suitePin + if err := json.Unmarshal(pinsData, &pins); err != nil { + t.Fatal(err) } - oracleExpression := `try { (` + expression + `) } catch * { local-name-from-QName($err:code) }` - independent := residualSaxonQuery(t, oracleExpression, "") - if independent != want { - t.Fatal(name, "independent", independent, "want", want) + pin := pins[kind] + if err := verifySuiteCheckout(t.Context(), directory, pin); err != nil { + t.Fatal(err) } - proof.Controls = append(proof.Controls, qtResidualProofControl{Name: name, Expression: expression, Native: actual, Independent: independent, Obligation: obligation}) -} - -func qtResidualLimitControls(t *testing.T, options suiteOptions, item suiteCase, proof *qtResidualProof) { - t.Helper() - options.limitsProfile = suiteLanguage - options.timeout = 30 * time.Second - environment, err := prepareSuiteEnvironment(t.Context(), options, item) + catalog, _, err := readSuiteNode(directory, directory, pin.Catalog) if err != nil { t.Fatal(err) } - if proof.Name == "ForExpr013" { - control := evaluateResidualCase(t, options, item) - if control.executionError() != nil || control.AssertionError != nil { - t.Fatal("raised bounded profile did not reproduce publisher value", control.executionError(), control.AssertionError) - } - evaluation := control.Evaluation - expression := "count((" + item.node.child("test").Text + "))" - want := strconv.Itoa(evaluation.sequence.Len()) - oracle := residualSaxonQuery(t, expression, filepath.Join(options.directory, "prod", "ForClause", "fsx.xml")) - if oracle != want { - t.Fatal(oracle, want) - } - proof.Controls = append(proof.Controls, qtResidualProofControl{Name: "same source and publisher assertion; explicit language profile and 30s case deadline", Expression: expression, Native: want, Independent: oracle, Obligation: "XPath20 2.3.1 implementation limits; exact unmodified publisher input/output"}) - return - } - if proof.Name == "fn-transform-27" { - qtResidualTransform27Controls(t, options, item, environment, proof) - return - } - expression, want := "", "" - switch proof.Name { - case "cbcl-codepoints-to-string-005": - expression, want = `string-length(codepoints-to-string(65536 to 65539))`, "4" - case "cbcl-count-001": - expression, want = `count(1 to 1000)`, "1000" - case "cbcl-matches-004": - expression, want = `matches('aaa','a{4}')`, "false" - case "cbcl-subsequence-010": - expression, want = `count(subsequence(1 to 1000,-5,4))`, "0" - case "cbcl-subsequence-011": - expression, want = `count(subsequence(1 to 1000,-5))`, "1000" - case "cbcl-subsequence-012": - expression, want = `count(subsequence(1 to 1000,748))`, "253" - case "cbcl-subsequence-013": - expression, want = `count(subsequence(1 to 1000,747))`, "254" - case "cbcl-subsequence-014": - expression, want = `subsequence(2147483647 to 2147483651,1,5)`, "2147483647 2147483648 2147483649 2147483650 2147483651" - case "environment-variable-012": - expression, want = strings.ReplaceAll(item.node.child("test").Text, "1024 * 1024", "1000"), "true" - case "fold-left-020", "fold-right-013": - expression, want = strings.ReplaceAll(item.node.child("test").Text, "1000000", "1000"), "1000" - case "map-for-each-014", "map-keys-014": - expression, want = `let $result := (`+strings.ReplaceAll(item.node.child("test").Text, "500000", "1000")+`) return count($result)=1000 and $result=1 and $result=1000`, "true" - case "map-remove-012": - expression, want = `let $result := (`+strings.NewReplacer("500000", "1000", "123456", "456").Replace(item.node.child("test").Text)+`) return map:size($result)=999 and map:contains($result,1) and map:contains($result,1000) and not(map:contains($result,456))`, "true" - default: - t.Fatal("missing case-specific limit control", proof.Name) + set, path, err := readSuiteNode(directory, directory, file) + if err != nil { + t.Fatal(err) } - residualControl(t, proof, environment, options.version, "bounded same-operator control", expression, want, "XPath implementation-limit permission; the operator succeeds with independently verified bounded operands") - if proof.Name == "cbcl-codepoints-to-string-005" { - residualControl(t, proof, environment, options.version, "semantic invalid scalar remains FOCH0001", `codepoints-to-string(1114112)`, "FOCH0001", "FO 5.2.1 invalid Unicode codepoint error independent of range materialization") + listed := slices.ContainsFunc(catalog.children("test-set"), func(entry *suiteNode) bool { return entry.attr(suiteFile) == file }) + if !listed || catalog.XMLName.Space != pin.Namespace || set.XMLName.Space != pin.Namespace { + t.Fatal("proof case is not listed in the pinned catalog", file) } - if proof.Name == "cbcl-matches-004" { - residualControl(t, proof, environment, options.version, "actual invalid regex remains FORX0002", `matches('aaa','a{')`, "FORX0002", "FO matches invalid grammar differs from valid enormous quantifier implementation cap") + for _, node := range set.children("test-case") { + if node.attr("name") == name { + return suiteCase{ + set: set.attr("name"), file: file, base: filepath.Dir(path), setNode: set, node: node, + environments: suiteEnvironments(set, filepath.Dir(path), suiteEnvironments(catalog, directory, nil)), + } + } } + t.Fatal("pinned case missing", kind, file, name) + return suiteCase{} } -func qtResidualTransform27Controls(t *testing.T, options suiteOptions, item suiteCase, environment *preparedSuiteEnvironment, proof *qtResidualProof) { +func evaluateResidualCase(t *testing.T, options suiteOptions, item suiteCase) residualCaseResult { t.Helper() - original := evaluateSuiteCase(t.Context(), options, item, environment) - if original.err != nil || residualSequenceText(t, original.sequence) != "true" { - t.Fatal("raised original case", original.err, residualSequenceText(t, original.sequence)) + ctx := t.Context() + if options.timeout > 0 { + bounded, cancel := context.WithTimeout(ctx, options.timeout) + defer cancel() + ctx = bounded } - proof.Controls = append(proof.Controls, qtResidualProofControl{Name: "original expression with raised bounded profile and fixed host clock", Native: "true", Obligation: "FO31 16.3.2 cache option has no effect on transformation result; repeated results may be identical"}) - bounded := strings.NewReplacer("-100000", "-100", "+100000", "+100").Replace(item.node.child("test").Text) - for _, cache := range []string{"false()", "true()"} { - expression := strings.ReplaceAll(bounded, `"cache":false()`, `"cache":`+cache) - value, err := residualXPath(t, environment, suiteVersion31, expression) - if err != nil || residualSequenceText(t, value) != "true" { - t.Fatal(cache, value, err) - } - proof.Controls = append(proof.Controls, qtResidualProofControl{Name: "fixed host clock, cache hint " + cache, Expression: expression, Native: "true", Obligation: "FO31 16.3.2 cache is an efficiency hint, not a promise of unequal current-dateTime values"}) + if status, reason := suiteApplicability(options, item); status != "" { + t.Fatal("proof must execute an applicable original case", status, reason) } - residualControl(t, proof, environment, suiteVersion31, "current-dateTime is stable within one dynamic context", `current-dateTime() eq current-dateTime()`, "true", "FO31 15.3 same dynamic current-dateTime; host controls the supplied instant") - // A fresh public invocation can receive another explicit host clock. This - // isolates clock input from stylesheet caching without sleeping or wall time. - clockExpression := `transform(map{'stylesheet-text':"",'initial-template':xs:QName('start'),'cache':false()})?output/string()` - for _, instant := range []string{"2026-01-01T00:00:00Z", "2026-01-02T00:00:00Z"} { - environment.currentTime, _ = time.Parse(time.RFC3339, instant) - value, err := residualXPath(t, environment, suiteVersion31, clockExpression) - if err != nil || residualSequenceText(t, value) != instant { - t.Fatal(value, err, instant) - } - proof.Controls = append(proof.Controls, qtResidualProofControl{Name: "explicit independent host clock " + instant, Expression: clockExpression, Native: instant, Obligation: "Configured per-invocation clock, not the cache hint, determines static current-dateTime here"}) + environment, err := prepareSuiteEnvironment(ctx, options, item) + if err != nil { + return residualCaseResult{PrepareError: err} } -} - -func residualInlineBindings(t *testing.T, expression string, environment *preparedSuiteEnvironment) string { - t.Helper() - for name, value := range environment.variables { - if name.Space != "" { - t.Fatal("proof needs an explicit namespace binding", name) - } - item, err := value.Single() - if err != nil { - t.Fatal(err) - } - text, err := item.Text() - if err != nil { - t.Fatal(err) - } - expression = strings.ReplaceAll(expression, "$"+name.Local, "xs:"+item.Type().Local+"('"+strings.ReplaceAll(text, "'", "''")+"')") + evaluation := evaluateSuiteCase(ctx, options, item, environment) + return residualCaseResult{ + Evaluation: evaluation, Environment: environment, + AssertionError: checkSuiteAssertion(ctx, evaluation, item.node.child(suiteResultVariable)), } - return expression } -func qtResidualFormatControls(t *testing.T, options suiteOptions, item suiteCase, original residualCaseResult, proof *qtResidualProof) { - t.Helper() - if original.executionError() != nil || original.AssertionError == nil { - t.Fatal("expected original publisher value mismatch", original.executionError(), original.AssertionError) - } - old := map[string]string{"format-dateTime-002g": "9:15:06.5", "format-dateTime-002h": "9:15:06.5", "format-dateTime-002i": "9:15:06.46", "format-dateTime-003m": "9:15:06.5", "format-dateTime-003n": "9:15:06.5", "format-dateTime-003p": "9:15:06.46", "format-dateTime-013s": "46", "format-dateTime-013u": "46", "millisecs-018": "12", "millisecs-022": "00"}[proof.Name] - if residualSequenceText(t, original.Evaluation.sequence) != old { - t.Fatal("old-edition algorithm changed", original.Evaluation.sequence, old) - } - expression := residualInlineBindings(t, item.node.child("test").Text, original.Environment) - oracle := residualSaxon20Expression(t, expression) - if oracle != old { - t.Fatal("published old-formatting algorithm disagreement", oracle, old) +func cloneResidualNode(node *suiteNode) *suiteNode { + if node == nil { + return nil } - proof.Controls = append(proof.Controls, qtResidualProofControl{Name: "same literal date/picture under pre-3.1 formatting", Expression: expression, Native: old, Independent: oracle, Obligation: "FO30 9.8.4.1 old rounding/width precedence; FO31 9.8.4.5 truncation is a later algorithm"}) - options.version = suiteVersion31 - modern := evaluateResidualCase(t, options, item) - if modern.executionError() != nil || modern.AssertionError != nil { - t.Fatal("same pinned fixture must match its later-edition expectation", modern.executionError(), modern.AssertionError) + cloned := *node + cloned.Attributes = slices.Clone(node.Attributes) + cloned.namespaces = make(map[string]string, len(node.namespaces)) + maps.Copy(cloned.namespaces, node.namespaces) + cloned.Children = make([]*suiteNode, len(node.Children)) + for index, child := range node.Children { + cloned.Children[index] = cloneResidualNode(child) } - want := residualSequenceText(t, modern.Evaluation.sequence) - oracle = residualSaxonQuery(t, expression, "") - if oracle != want { - t.Fatal("modern oracle", oracle, want) + return &cloned +} + +func residualErrorCode(err error) string { + if detail, ok := errors.AsType[*transform.Error](err); ok { + return detail.Code } - proof.Controls = append(proof.Controls, qtResidualProofControl{Name: "same publisher input/assertion under processor 3.1", Expression: expression, Native: want, Independent: oracle, Obligation: "FO31 9.8.4.5 explicit fractional-second/width algorithm; publisher modifications in 2016 reference bugs 29749/29788"}) + return "" } -func qtResidualTransform43Controls(t *testing.T, original residualCaseResult, proof *qtResidualProof) { +func residualSequenceText(t *testing.T, value transform.Sequence) string { t.Helper() - if original.executionError() != nil || !strings.Contains(fmt.Sprint(original.AssertionError), "FODC0006") { - t.Fatal("expected assertion's document parse error", original.executionError(), original.AssertionError) - } - assertions := original.Evaluation.item.node.child(suiteResultVariable).child("all-of").Children - if len(assertions) != 4 { - t.Fatal("publisher assertion inventory changed") - } - for index := range 3 { - if err := checkSuiteAssertion(t.Context(), original.Evaluation, assertions[index]); err != nil { - t.Fatal(index, err) + parts := make([]string, 0, value.Len()) + for index := range value.Len() { + item, ok := value.At(index) + if !ok { + t.Fatal("missing result", index) } - } - value, err := original.Evaluation.sequence.Single() - if err != nil { - t.Fatal(err) - } - keys, err := value.Keys() - if err != nil || len(keys) != 4 { - t.Fatal(keys, err) - } - principal := "" - for _, key := range keys { - name, err := key.Text() + part, err := item.Text() if err != nil { t.Fatal(err) } - if strings.HasSuffix(name, "/output.html") { - part, found := value.Lookup(key) - if !found { - t.Fatal(name) - } - principal = residualSequenceText(t, part) - } - } - if principal == "" { - t.Fatal("missing principal serialization") - } - environment := original.Environment - literal := "'" + strings.ReplaceAll(principal, "'", "''") + "'" - residualControl(t, proof, environment, suiteVersion31, "principal is a valid empty external parsed entity", `count(parse-xml-fragment(`+literal+`)/node())`, "0", "SER31 5 XML external parsed entity; empty principal need not be a well-formed XML document") - residualControl(t, proof, environment, suiteVersion31, "the same principal is not an XML document", `parse-xml(`+literal+`)`, "FODC0006", "FO31 14.7.1 parse-xml requires a document element") - residualControl(t, proof, environment, suiteVersion31, "quantifier error binding visited first", `boolean(some $s in ('','

Header for section 2

') satisfies parse-xml($s)//h1='Header for section 2')`, "FODC0006", "XPath31 3.13 permits a satisfies error before the true witness; boolean does not catch errors") - residualControl(t, proof, environment, suiteVersion31, "quantifier witness visited first", `some $s in ('

Header for section 2

','') satisfies parse-xml($s)//h1='Header for section 2'`, "true", "XPath31 3.13 true witness is an allowed outcome; map keys have implementation-dependent order") - filtered, err := environment.query(t.Context(), `some $key in map:keys($result)[contains(.,'section')] satisfies parse-xml($result?($key))//h1='Header for section 2'`, transform.Bindings{{Local: "result"}: original.Evaluation.sequence}) - if err != nil || residualSequenceText(t, filtered) != "true" { - t.Fatal("secondary map entry was lost", filtered, err) + parts = append(parts, part) } - proof.Controls = append(proof.Controls, qtResidualProofControl{Name: "unchanged result map contains the required secondary document", Native: "true; first three exact catalog assertions also pass", Obligation: "FO31 16.3.2 serialized result map preserves secondary results; no reordering or publisher assertion rewrite"}) + return strings.Join(parts, " ") } -func qtResidualSerializationControls(t *testing.T, options suiteOptions, item suiteCase, original residualCaseResult, proof *qtResidualProof) { +func residualXPath(t *testing.T, environment *preparedSuiteEnvironment, version, expression string) (transform.Sequence, error) { t.Helper() - if original.AssertionError == nil { - t.Fatal("the original catalog assertion unexpectedly passed") - } - for _, version := range []string{suiteVersion30, suiteVersion31} { - options.version = version - value := evaluateResidualCase(t, options, item) - want := "" - if version == suiteVersion30 { - want = "SEPM0017" - } else if proof.Name == "serialize-xml-025b" { - want = "SEPM0019" - } - code := residualErrorCode(value.executionError()) - if code != want || want == "" && value.executionError() != nil { - t.Fatal("edition-specific extraction", version, value.executionError(), want) - } - actual := code - if want == "" { - actual = residualSequenceText(t, value.Evaluation.sequence) - if !strings.Contains(actual, "atomic") { - t.Fatal("source document was not serialized", actual) - } - } - proof.Controls = append(proof.Controls, qtResidualProofControl{Name: "same source and parameter document under " + version, Native: actual, Obligation: "SER30 3.1 assesses the whole parameter document; SER31 3.1 first selects known standard parameter children; duplicate QNames have their own SEPM0019 rule"}) - } - parameterFile := "" - for _, source := range item.node.child("environment").children("source") { - if source.attr("role") == "$params" { - _, parameterFile, _ = readSuiteReferencedFile(options.directory, item.base, source) - } + selected := transform.XPath31 + switch version { + case suiteVersion20: + selected = transform.XPath20 + case suiteVersion30: + selected = transform.XPath30 } - if parameterFile == "" { - t.Fatal("publisher parameter resource missing") + query, err := environment.engine.CompileXPath(t.Context(), expression, transform.XPathOptions{Version: selected, Namespaces: environment.namespaces, BaseURI: environment.baseURI, DefaultCollation: environment.collation, DecimalFormats: environment.decimalFormats}) + if err != nil { + return transform.Sequence{}, err } - for _, edition := range []struct { - version string - checksum string - }{ - {"30", "1cbcc49ac5ed8942ba58276699254469ca2144e229f68faeeb48f784c1037953"}, - {"31", "256736a2f4e7923e888047dee757c17bc3ee707e285f00639fd8fb1d962d11b0"}, + return query.Evaluate(t.Context(), environment.document, transform.XPathContext{Variables: environment.variables, ContextItem: environment.contextItem, Context: suiteDynamicContext(environment.currentTime)}) +} + +type qtResidualProofControl struct { + Name string `json:"name"` + Expression string `json:"expression,omitempty"` + Native string `json:"native"` + Independent string `json:"independent,omitempty"` + Obligation string `json:"obligation"` +} + +type qtResidualProof struct { + Name string + File string + Version string + Category string + XSDVersion string `json:"xsd_version"` + CaseTimeout string `json:"case_timeout"` + LimitsProfile string `json:"limits_profile"` + SourceSHA256 string `json:"source_sha256"` + OriginalLimit bool `json:"original_typed_limit"` + OriginalDeadline bool `json:"original_typed_deadline"` + OriginalError string `json:"original_error,omitempty"` + OriginalAssertion string `json:"original_assertion_error,omitempty"` + Controls []qtResidualProofControl `json:"controls"` +} + +// This table identifies original publisher cases, not replacement expectations. +// The test keeps their non-passing assertions visible and separately checks why. +// The original publisher serialization regex remains untouched. These controls +// distinguish required character/namespace semantics from a particular spelling. +func runXSLTResidualSerialization(t *testing.T, directory string) { + for _, test := range []struct { + name string + file string + kind string + }{ + {"attribute-0301", "tests/insn/attribute/_attribute-test-set.xml", "uri"}, + {"output-0102c", "tests/decl/output/_output-test-set.xml", "xml-attribute"}, + {"output-0103c", "tests/decl/output/_output-test-set.xml", "xml-attribute"}, + {"output-0602a", "tests/decl/output/_output-test-set.xml", "namespace"}, + {"output-0603a", "tests/decl/output/_output-test-set.xml", "namespace"}, + {"output-0724", "tests/decl/output/_output-test-set.xml", "uri"}, } { - schema := filepath.Join(residualRequired(t, "SERIALIZATION_SCHEMA_DIR"), "ser"+edition.version+".xsd") - data, err := os.ReadFile(schema) - if err != nil { - t.Fatal(err) - } - digest := sha256.Sum256(data) - if hex.EncodeToString(digest[:]) != edition.checksum { - t.Fatal("published schema pin changed", schema) - } - parts := []string{} - if edition.version == "30" { - raw, err := os.ReadFile(parameterFile) - if err != nil { - t.Fatal(err) + t.Run(test.name, func(t *testing.T) { + item := loadPinnedResidualCase(t, directory, suiteXSLT, test.file, test.name) + options := suiteOptions{kind: suiteXSLT, directory: directory, version: suiteVersion30, xsdVersion: suiteVersion10, timeout: time.Second, limitsProfile: "default"} + original := evaluateResidualCase(t, options, item) + if original.executionError() != nil || original.AssertionError == nil { + t.Fatal("unchanged original residual", original.executionError(), original.AssertionError) } - parts = append(parts, string(raw)) - } else { - declared, _, err := decodeSuiteNode(data, schema) + actual, err := original.Evaluation.serialized(t.Context()) if err != nil { t.Fatal(err) } - names := []string{} - for _, element := range declared.children("element") { - if name := element.attr("name"); name != "" && name != "serialization-parameters" { - names = append(names, "'"+name+"'") + proof := qtResidualProof{Name: test.name, File: test.file, Version: suiteVersion30, Category: "permitted-outcome"} + switch test.kind { + case "xml-attribute": + want := `% %C2%96 %C2%96 a " %C2%A1 < > & end` + if test.name == "output-0103c" { + want = "% %C2%96 \u0096 a \" ¡ < > & end" } - } - if len(names) < 20 { - t.Fatal("published serialization declaration inventory", len(names)) - } - // Selection names come from the independent published XSD, rather - // than the engine's parameter registry. Saxon performs the projection. - expression := `serialize(array{for $p in doc('` + suiteFileURI(parameterFile) + `')/*/*[namespace-uri()='http://www.w3.org/2010/xslt-xquery-serialization'][local-name()=(` + strings.Join(names, ",") + `)] return serialize($p,map{'method':'xml','omit-xml-declaration':true()})},map{'method':'json'})` - selected := residualSaxonQuery(t, expression, "") - if err := json.Unmarshal([]byte(selected), &parts); err != nil { - t.Fatal("independent projection result", selected, err) - } - } - valid := true - for _, part := range parts { - binary, err := exec.LookPath("xmllint") - if err != nil { - t.Fatal(err) - } - command := exec.CommandContext(t.Context(), binary, "--nonet", "--noout", "--schema", schema, "-") - command.Stdin = strings.NewReader(part) - output, err := command.CombinedOutput() - if err != nil { - var exit *exec.ExitError - if !errors.As(err, &exit) || exit.ExitCode() != 3 { - t.Fatalf("published XSD validator: %s: %v; selected part %q", output, err, part) + file := filepath.Join(t.TempDir(), "actual.xml") + if err := os.WriteFile(file, actual, 0o600); err != nil { + t.Fatal(err) } - valid = false + oracle := residualSaxonQuery(t, `string(doc('`+suiteFileURI(file)+`')/*/@href)`, "") + if oracle != want { + t.Fatal("independent XML parsing changed attribute meaning", oracle, want) + } + residualControl(t, &proof, original.Environment, suiteVersion31, "legal XML attribute quote spellings", `parse-xml('
')/a/@href eq parse-xml('')/a/@href`, "true", "XML10 2.3/4.6 permit named and numeric quote references") + residualControl(t, &proof, original.Environment, suiteVersion31, "legal literal and escaped greater-than", `parse-xml('')/a/@href eq parse-xml('')/a/@href`, "true", "XML10 attribute value grammar permits literal greater-than; SER character expansion must preserve value") + t.Log("P3_ORACLE published-vector") + case "uri": + if test.name == "attribute-0301" { + residualLegacyAttribute301(t, directory, item, options) + } + originalValue := "père" + if test.name == "output-0724" { + originalValue = "✈" + } + want := residualSaxonQuery(t, `iri-to-uri('`+originalValue+`')`, "") + values := residualHTMLInputValues(t, actual) + if len(values) == 0 { + t.Fatal("no serialized input/@value") + } + for _, value := range values { + if value != want { + t.Fatal("published URI table escaping", value, want) + } + } + controlled := residualSerializationVariant(t, options, item, original.Environment, func(raw []byte) []byte { + if !bytes.Contains(raw, []byte(`method="html"`)) { + t.Fatal("publisher output method changed") + } + return bytes.ReplaceAll(raw, []byte(`method="html"`), []byte(`method="html" escape-uri-attributes="no"`)) + }) + decoded := residualHTMLInputValues(t, controlled) + if len(decoded) != len(values) { + t.Fatal("controlled input count", decoded, values) + } + for _, value := range decoded { + if value != originalValue { + t.Fatal("explicit escaping=no must retain original value", value, originalValue) + } + } + t.Log("P3_ORACLE published-vector") + case "namespace": + originalBinding, originalAttribute := residualHTMLNamespaceState(t, actual) + if !originalBinding || test.name == "output-0603a" && !originalAttribute { + t.Fatal("ancestor namespace is not bound", originalBinding, originalAttribute, string(actual)) + } + controlled := residualXHTMLNamespaceControl(t, options, item, original.Environment) + bodyBinding, attribute := residualHTMLNamespaceState(t, controlled) + if bodyBinding || test.name == "output-0603a" && !attribute { + t.Fatal("XHTML prefix-normalization intervention", bodyBinding, attribute, string(controlled)) + } + residualControl(t, &proof, original.Environment, suiteVersion31, "ancestor/local declarations bind the same expanded attribute", `deep-equal(parse-xml('')/body/mi/@*,parse-xml('')/body/mi/@*)`, "true", "Namespaces in XML 1.0 6.2 namespace scope; SER31 HTML 7.1 differs from XHTML global prefix normalization") + t.Log("P3_ORACLE published-vector") } - } - want := edition.version == "31" - if valid != want { - t.Fatal("published schema assessment", edition.version, valid, want) - } - t.Log("P3_ORACLE published-schema") - proof.Controls = append(proof.Controls, qtResidualProofControl{Name: "published SER" + edition.version + " schema after prescribed extraction", Native: strconv.FormatBool(valid), Independent: fmt.Sprintf("xmllint validity=%t; selected parts=%d; schema SHA256=%s", valid, len(parts), edition.checksum), Obligation: "Published SER30/31 3.1 parameter-document extraction and associated XSD; no extension schema declarations supplied"}) - } - if proof.Name == "serialize-xml-025b" { - data, err := os.ReadFile(parameterFile) - if err != nil { - t.Fatal(err) - } - expression := `let $p:=parse-xml('` + strings.ReplaceAll(string(data), "'", "''") + `')/*/* return count($p)=2 and node-name($p[1]) eq node-name($p[2])` - residualControl(t, proof, original.Environment, suiteVersion31, "duplicate foreign QName exists independently of schema validity", expression, "true", "SER31 3.1 duplicate parameter QName SEPM0019; SER30 also has a simultaneous whole-document SEPM0017") + }) } } -// The original publisher case is always executed before changing its selected -// processor. The second run retains the same fixture, parameters and assertion. -type xsltResidualEditionCase struct { - name string - file string - version string - code string +func residualXHTMLNamespaceControl(t *testing.T, options suiteOptions, item suiteCase, environment *preparedSuiteEnvironment) []byte { + t.Helper() + invalid := evaluateXSLTResidualSourceVariant(t, options, item, `method="html"`, `method="xhtml"`) + if got := residualErrorCode(invalid.executionError()); got != "SESU0013" { + t.Fatal("changing only the method retains unsupported XML version 5.0", got, invalid.executionError()) + } + // XHTML has separate HTML and XML version parameters. The pinned HTML5 + // declaration uses version 5.0; change that output attribute, never the + // stylesheet's independent version 3.0 attribute, when selecting XHTML5. + const before = `method="html" version="5.0" html-version="5.0"` + const after = `method="xhtml" version="1.0" html-version="5.0"` + var source []byte + controlled := residualSerializationVariant(t, options, item, environment, func(raw []byte) []byte { + if bytes.Count(raw, []byte(before)) != 1 { + t.Fatal("the pinned output's HTML5 version tuple changed") + } + source = bytes.Replace(raw, []byte(before), []byte(after), 1) + return source + }) + main := filepath.Join(t.TempDir(), "xhtml5.xsl") + if err := os.WriteFile(main, source, 0o600); err != nil { + t.Fatal(err) + } + _, input := xsltResidualInputBytes(t, options, item) + oracle, err := xsltResidualOracleTransform(t, suiteVersion30, main, input, "", "") + if err != nil { + t.Fatal(err) + } + nativeBinding, nativeAttribute := residualHTMLNamespaceState(t, controlled) + oracleBinding, oracleAttribute := residualHTMLNamespaceState(t, oracle) + if nativeBinding != oracleBinding || nativeAttribute != oracleAttribute { + t.Fatal("independent XHTML5 namespace intervention", nativeBinding, nativeAttribute, oracleBinding, oracleAttribute, string(oracle)) + } + t.Log("P3_ORACLE saxon-12.10") + return controlled } -func xsltResidualEditionCases() []xsltResidualEditionCase { - return []xsltResidualEditionCase{ - {"backwards-046", "tests/misc/backwards/_backwards-test-set.xml", "2.0", "XTSE0080"}, - {"character-map-026", "tests/decl/character-map/_character-map-test-set.xml", "2.0", "XTSE0020"}, - {"character-map-901", "tests/decl/character-map/_character-map-test-set.xml", "2.0", "XTSE0080"}, - {"choose-0202", "tests/insn/choose/_choose-test-set.xml", "2.0", "XTSE0080"}, - {"collations-0128", "tests/misc/collations/_collations-test-set.xml", "2.0", "XTSE0010"}, - {"collection-005", "tests/fn/collection/_collection-test-set.xml", "2.0", "XTSE0010"}, - {"doe-0409", "tests/attr/disable-output-escaping/_disable-output-escaping-test-set.xml", "2.0", "XTSE0080"}, - {"error-0600a", "tests/misc/error/_error-test-set.xml", "3.0", "XTTE0590"}, - {"error-0720a", "tests/misc/error/_error-test-set.xml", "3.0", "XTDE0640"}, - {"error-0720b", "tests/misc/error/_error-test-set.xml", "3.0", "XTDE0640"}, - {"error-0720c", "tests/misc/error/_error-test-set.xml", "3.0", "XTDE0640"}, - {"error-0720d", "tests/misc/error/_error-test-set.xml", "3.0", "XTDE0640"}, - {"error-1370a", "tests/misc/error/_error-test-set.xml", "3.0", "XPDY0002"}, - {"error-1380a", "tests/misc/error/_error-test-set.xml", "3.0", "XPDY0002"}, - {"for-each-group-084", "tests/insn/for-each-group/_for-each-group-test-set.xml", "2.0", "XPST0003"}, - {"for-each-group-091", "tests/insn/for-each-group/_for-each-group-test-set.xml", "2.0", "XTSE0080"}, - {"function-0302", "tests/decl/function/_function-test-set.xml", "2.0", "XPST0003"}, - {"function-1902", "tests/decl/function/_function-test-set.xml", "2.0", "XTSE0090"}, - {"id-041", "tests/fn/id/_id-test-set.xml", "2.0", "XPST0003"}, - {"match-248", "tests/attr/match/_match-test-set.xml", "2.0", "XPST0003"}, - {"match-254", "tests/attr/match/_match-test-set.xml", "2.0", "XPST0003"}, - {"namespace-0912", "tests/type/namespace/_namespace-test-set.xml", "2.0", "XTTE0570"}, - {"number-1004", "tests/insn/number/_number-test-set.xml", "2.0", "XTSE0010"}, - {"output-0725", "tests/decl/output/_output-test-set.xml", "2.0", "XTSE0090"}, - {"output-0726", "tests/decl/output/_output-test-set.xml", "2.0", "XTSE0090"}, - {"result-document-0286", "tests/insn/result-document/_result-document-test-set.xml", "2.0", "XTSE0020"}, - {"result-document-0287", "tests/insn/result-document/_result-document-test-set.xml", "2.0", "XTSE0020"}, - {"result-document-1006", "tests/insn/result-document/_result-document-test-set.xml", "2.0", "XPST0017"}, - {"sequence-0132", "tests/insn/sequence/_sequence-test-set.xml", "2.0", "XTSE0010"}, - {"system-property-022", "tests/fn/system-property/_system-property-test-set.xml", "2.0", "XTSE0010"}, - {"variable-4802", "tests/decl/variable/_variable-test-set.xml", "2.0", ""}, +func residualSerializationVariant(t *testing.T, options suiteOptions, item suiteCase, environment *preparedSuiteEnvironment, edit func([]byte) []byte) []byte { + t.Helper() + raw, compileOptions, err := suiteStylesheetSource(options, environment) + if err != nil { + t.Fatal(err) + } + changed := edit(slices.Clone(raw)) + if bytes.Equal(raw, changed) { + t.Fatal("causal intervention did not change the input") } + compiled, err := environment.engine.CompileXSLT(t.Context(), changed, compileOptions) + if err != nil { + t.Fatal(err) + } + result, err := evaluateCompiledXSLTSuite(t.Context(), item, environment, compiled) + if err != nil { + t.Fatal(err) + } + output, err := result.Serialize(t.Context()) + if err != nil { + t.Fatal(err) + } + return output.Primary } -func evaluateXSLTResidualVariant(t *testing.T, options suiteOptions, item suiteCase) residualCaseResult { +func residualLegacyAttribute301(t *testing.T, directory string, item suiteCase, options suiteOptions) { t.Helper() - // Some source fixtures declare a 2.0-only dependency while containing - // 3.0 syntax. This explicit controlled run bypasses applicability alone; - // it retains the pinned stylesheet and the complete publisher assertion. - environment, err := prepareSuiteEnvironment(t.Context(), options, item) + options.version = suiteVersion20 + original := evaluateResidualCase(t, options, item) + if original.executionError() != nil || original.AssertionError != nil { + t.Fatal("selected SER20 must satisfy every unchanged publisher assertion", original.executionError(), original.AssertionError) + } + classpath := residualRequired(t, "SAXON20_CLASSPATH") + binary, err := exec.LookPath("java") if err != nil { - return residualCaseResult{PrepareError: err} + t.Fatal(err) } - evaluation := evaluateSuiteCase(t.Context(), options, item, environment) - return residualCaseResult{ - Evaluation: evaluation, Environment: environment, - AssertionError: checkSuiteAssertion(t.Context(), evaluation, item.node.child(suiteResultVariable)), + identity, err := exec.CommandContext(t.Context(), binary, "-cp", classpath, "net.sf.saxon.Version").CombinedOutput() + if err != nil || !strings.Contains(string(identity), "SAXON 9.4.0.9") { + t.Fatal("independent old processor identity", string(identity), err) + } + style := filepath.Join(directory, "tests", "insn", "attribute", "attribute-0301.xsl") + input := filepath.Join(directory, "tests", "insn", "attribute", "attribute-03.xml") + output, err := exec.CommandContext(t.Context(), binary, "-cp", classpath, "net.sf.saxon.Transform", "-xsl:"+style, "-s:"+input).Output() + if err != nil { + if failure, ok := errors.AsType[*exec.ExitError](err); ok { + t.Fatalf("original Saxon9.4: %s: %v", failure.Stderr, err) + } + t.Fatal(err) + } + for _, assertion := range item.node.child(suiteResultVariable).child("all-of").Children { + if assertion.XMLName.Local != "serialization-matches" { + t.Fatal("original assertion type changed", assertion.XMLName) + } + value, err := original.Environment.query(t.Context(), `matches($s,$pattern,$flags)`, transform.Bindings{ + {Local: "s"}: transform.Values(transform.String(string(output))), {Local: "pattern"}: transform.Values(transform.String(assertion.Text)), {Local: "flags"}: transform.Values(transform.String(assertion.attr("flags"))), + }) + if err != nil || residualSequenceText(t, value) != "true" { + t.Fatal("independent unchanged original assertion", assertion.Text, string(output), err) + } + } + t.Log("P3_ORACLE saxon-9.4") +} + +func residualHTMLInputValues(t *testing.T, data []byte) []string { + t.Helper() + // A trailing lexical boundary lets encoding/xml close a final HTML void + // element; it is not part of the original bytes or an asserted result. + decoder := xml.NewDecoder(io.MultiReader(bytes.NewReader(data), strings.NewReader("<__end/>"))) + decoder.Strict = false + decoder.AutoClose = xml.HTMLAutoClose + decoder.Entity = xml.HTMLEntity + var values []string + for { + token, err := decoder.Token() + if err != nil { + if errors.Is(err, io.EOF) { + break + } + + t.Fatal(err) + } + if start, ok := token.(xml.StartElement); ok && start.Name.Local == "input" { + for _, attribute := range start.Attr { + if attribute.Name.Local == "value" { + values = append(values, attribute.Value) + } + } + } } + return values } -func TestXSLTResidualCauses(t *testing.T) { - configureLanguageTest(t, false) - configured := os.Getenv("XMLSEC_XSLT_DIR") - if configured == "" { - configured = residualRequired(t, "XMLSEC_XSLT30_DIR") - } - directory, err := filepath.EvalSymlinks(configured) - if err != nil { - t.Fatal(err) - } - runXSLTResidualCompetingErrors(t, directory) - runXSLTResidualLimits(t, directory) - runXSLTResidualMetadataControls(t, directory) - runXSLTResidualSingleCorrections(t, directory) - runXSLTResidualRegexEditions(t, directory) - runXSLTResidualExplicitResources(t, directory) - runXSLTResidualSerialization(t, directory) - runXSLTResidualWhitespace(t, directory) - runXSLTResidualWarnings(t, directory) - runXSLTResidualOptionalLibraries(t, directory) - runXSLTResidualPackageCorrections(t, directory) - runXSLTResidualTypeProfiles(t, directory) - runXSLTResidualPriming(t, directory) - runXSLTResidualEvaluateLibrary(t, directory) - runXSLTResidualJSONTyping(t, directory) - runXSLTResidualNumberProfiles(t, directory) - runXSLTResidualNamedOutput(t, directory) - for _, test := range xsltResidualEditionCases() { - t.Run(test.name, func(t *testing.T) { - item := loadPinnedResidualCase(t, directory, suiteXSLT, test.file, test.name) - options := suiteOptions{ - kind: suiteXSLT, directory: directory, version: test.version, - xsdVersion: suiteVersion10, timeout: time.Second, limitsProfile: "default", +func residualHTMLNamespaceState(t *testing.T, data []byte) (bool, bool) { + t.Helper() + decoder := xml.NewDecoder(bytes.NewReader(data)) + decoder.Strict = false + decoder.AutoClose = xml.HTMLAutoClose + decoder.Entity = xml.HTMLEntity + bodyBinding, attribute := false, false + for { + token, err := decoder.Token() + if err != nil { + if errors.Is(err, io.EOF) { + break } - t.Run("original", func(t *testing.T) { - original := evaluateResidualCase(t, options, item) - if original.AssertionError == nil { - t.Fatal("the unchanged publisher assertion no longer reproduces the residual") - } - if got := residualErrorCode(original.executionError()); got != test.code { - t.Fatal("typed original error", got, original.executionError(), test.code) - } - if test.name == "variable-4802" { - value, err := original.Evaluation.assertionValue(t.Context(), `exists(//out[. = '{.}'])`, nil) - if err != nil || residualSequenceText(t, value) != suiteTrue { - t.Fatal("2.0 must preserve the unexpanded text-template literal", value, err) - } - } - }) - t.Run("matching-edition", func(t *testing.T) { - var controlled residualCaseResult - switch test.name { - case "function-0302": - controlled = evaluateXSLTResidualSourceVariant(t, options, item, `$zls || 'value-of'`, `concat($zls, 'value-of')`) - case "function-1902": - controlled = evaluateXSLTResidualSourceVariant(t, options, item, ` expand-text="yes"`, "") - default: - options.version = suiteVersion30 - if test.version == suiteVersion30 { - options.version = suiteVersion20 - } - controlled = evaluateXSLTResidualVariant(t, options, item) + + t.Fatal(err) + } + if start, ok := token.(xml.StartElement); ok { + for _, a := range start.Attr { + if start.Name.Local == "body" && a.Name.Space == "xmlns" && a.Name.Local == "svg" && a.Value == "http://www.w3.org/2000/svg" { + bodyBinding = true } - if controlled.PrepareError != nil || controlled.AssertionError != nil { - if test.name == "function-1902" && controlled.Evaluation != nil { - names, _ := controlled.Evaluation.assertionValue(t.Context(), `string-join(//incorrectly-available-function/concat(@name,'#',@arity),',')`, nil) - t.Log("functions available after grammar-only correction", residualSequenceText(t, names)) - } - t.Fatal("matching selected edition must satisfy the unchanged assertion", controlled.executionError(), controlled.AssertionError) + if start.Name.Local == "mi" && a.Name.Space == "http://www.w3.org/2000/svg" && a.Name.Local == "att" && a.Value == "34" { + attribute = true } - }) - }) - } - t.Run("collection-006", func(t *testing.T) { - item := loadPinnedResidualCase(t, directory, suiteXSLT, "tests/fn/collection/_collection-test-set.xml", "collection-006") - options := suiteOptions{ - kind: suiteXSLT, directory: directory, version: suiteVersion30, - xsdVersion: suiteVersion10, timeout: time.Second, limitsProfile: "default", - } - t.Run("original-30", func(t *testing.T) { - result := evaluateResidualCase(t, options, item) - if result.executionError() != nil || result.AssertionError != nil { - t.Fatal("fixed package-local collection views must pass the exact original", result.executionError(), result.AssertionError) - } - }) - t.Run("original-20", func(t *testing.T) { - options.version = suiteVersion20 - result := evaluateResidualCase(t, options, item) - if result.AssertionError == nil || residualErrorCode(result.executionError()) != "XTSE0080" { - t.Fatal("XSLT20 reserves the XSLT namespace even when a source 3.0 module names its initial template", result.executionError(), result.AssertionError) } - }) - runXSLTResidualCollectionEdition(t, options, item) - }) + } + } + return bodyBinding, attribute +} + +func qtResidualCases() []struct { + name string + file string + version string + category string +} { + return []struct { + name string + file string + version string + category string + }{ + {"format-date-011", "fn/format-date.xml", "3.1", "calendar-month-week"}, + {"ForExpr013", "prod/ForClause.xml", "2.0", "limit"}, + {"cbcl-codepoints-to-string-005", "fn/codepoints-to-string.xml", "2.0", "limit"}, + {"cbcl-count-001", "fn/count.xml", "2.0", "limit"}, + {"cbcl-matches-004", "fn/matches.xml", "2.0", "limit"}, + {"cbcl-subsequence-010", "fn/subsequence.xml", "2.0", "limit"}, + {"cbcl-subsequence-011", "fn/subsequence.xml", "2.0", "limit"}, + {"cbcl-subsequence-012", "fn/subsequence.xml", "2.0", "limit"}, + {"cbcl-subsequence-013", "fn/subsequence.xml", "2.0", "limit"}, + {"cbcl-subsequence-014", "fn/subsequence.xml", "2.0", "limit"}, + {"environment-variable-012", "fn/environment-variable.xml", "3.1", "limit"}, + {"fn-transform-27", "fn/transform.xml", "3.1", "limit"}, + {"fn-transform-43", "fn/transform.xml", "3.1", "transform43"}, + {"fold-left-020", "fn/fold-left.xml", "3.1", "limit"}, + {"fold-right-013", "fn/fold-right.xml", "3.1", "limit"}, + {"format-dateTime-002g", "fn/format-dateTime.xml", "3.0", "format"}, + {"format-dateTime-002h", "fn/format-dateTime.xml", "3.0", "format"}, + {"format-dateTime-002i", "fn/format-dateTime.xml", "3.0", "format"}, + {"format-dateTime-003m", "fn/format-dateTime.xml", "3.0", "format"}, + {"format-dateTime-003n", "fn/format-dateTime.xml", "3.0", "format"}, + {"format-dateTime-003p", "fn/format-dateTime.xml", "3.0", "format"}, + {"format-dateTime-013s", "fn/format-dateTime.xml", "3.0", "format"}, + {"format-dateTime-013u", "fn/format-dateTime.xml", "3.0", "format"}, + {"map-for-each-014", "map/for-each.xml", "3.1", "limit"}, + {"map-keys-014", "map/keys.xml", "3.1", "limit"}, + {"map-remove-012", "map/remove.xml", "3.1", "limit"}, + {"millisecs-018", "fn/format-time.xml", "3.0", "format"}, + {"millisecs-022", "fn/format-time.xml", "3.0", "format"}, + {"serialize-xml-007b", "fn/serialize.xml", "3.1", "serialize"}, + {"serialize-xml-007c", "fn/serialize.xml", "3.0", "serialize"}, + {"serialize-xml-015b", "fn/serialize.xml", "3.1", "serialize"}, + {"serialize-xml-016b", "fn/serialize.xml", "3.1", "serialize"}, + {"serialize-xml-020b", "fn/serialize.xml", "3.1", "serialize"}, + {"serialize-xml-025b", "fn/serialize.xml", "3.0", "serialize"}, + {"serialize-xml-026b", "fn/serialize.xml", "3.0", "serialize"}, + } } func xsltResidualOracleTransform(t *testing.T, version, main, input, initial, library string) ([]byte, error) { @@ -4070,3 +4083,270 @@ func runXSLTResidualNumberProfiles(t *testing.T, directory string) { } }) } + +// These independent literal rows apply F&O 3.0/3.1 section 9.8.4: a week +// belongs to the month containing its Thursday. The unchanged publisher vector +// differs at exactly 2006-01-30, whose Thursday is 2006-02-02. +type isoMonthWeekFixturePair struct { + Date string `xml:"date,attr"` + Week string `xml:",chardata"` +} + +func isoMonthWeekFixture(t *testing.T) []isoMonthWeekFixturePair { + t.Helper() + rows := []isoMonthWeekFixturePair{ + {"2005-12-04", "1"}, + {"2005-12-07", "2"}, + {"2005-12-10", "2"}, + {"2005-12-13", "3"}, + {"2005-12-16", "3"}, + {"2005-12-19", "4"}, + {"2005-12-22", "4"}, + {"2005-12-25", "4"}, + {"2005-12-28", "5"}, + {"2005-12-31", "5"}, + {"2006-01-03", "1"}, + {"2006-01-06", "1"}, + {"2006-01-09", "2"}, + {"2006-01-12", "2"}, + {"2006-01-15", "2"}, + {"2006-01-18", "3"}, + {"2006-01-21", "3"}, + {"2006-01-24", "4"}, + {"2006-01-27", "4"}, + {"2006-01-30", "1"}, + {"2006-02-02", "1"}, + {"2006-02-05", "1"}, + {"2006-02-08", "2"}, + {"2006-02-11", "2"}, + {"2006-02-14", "3"}, + {"2006-02-17", "3"}, + {"2006-02-20", "4"}, + {"2006-02-23", "4"}, + {"2006-02-26", "4"}, + {"2006-03-01", "1"}, + {"2006-03-04", "1"}, + {"2006-03-07", "2"}, + {"2006-03-10", "2"}, + {"2006-03-13", "3"}, + {"2006-03-16", "3"}, + {"2006-03-19", "3"}, + {"2006-03-22", "4"}, + {"2006-03-25", "4"}, + {"2006-03-28", "5"}, + {"2006-03-31", "5"}, + {"2006-04-03", "1"}, + {"2006-04-06", "1"}, + {"2006-04-09", "1"}, + {"2006-04-12", "2"}, + {"2006-04-15", "2"}, + {"2006-04-18", "3"}, + {"2006-04-21", "3"}, + {"2006-04-24", "4"}, + } + if len(rows) != 48 { + t.Fatal("incomplete month-week fixture controls", len(rows)) + } + seen := make(map[string]bool, len(rows)) + for _, row := range rows { + if seen[row.Date] { + t.Fatal("duplicate month-week fixture date", row.Date) + } + seen[row.Date] = true + } + return rows +} + +func checkISOMonthWeekRows(t *testing.T, actual []isoMonthWeekFixturePair, publisher bool) { + t.Helper() + want := isoMonthWeekFixture(t) + if len(actual) != len(want) { + t.Fatal("month-week row count", len(actual), len(want)) + } + for i, expected := range want { + if publisher && expected.Date == "2006-01-30" { + expected.Week = "5" + } + if actual[i] != expected { + t.Fatalf("month-week row %d = %+v; want %+v (publisher=%t)", i, actual[i], expected, publisher) + } + } +} + +func checkISOMonthWeekAssertionCorrection(t *testing.T, original residualCaseResult, corrected, wrong *suiteNode) { + t.Helper() + if original.executionError() != nil || original.AssertionError == nil { + t.Fatal("unchanged publisher must execute successfully and retain its value mismatch", original.executionError(), original.AssertionError) + } + if err := checkSuiteAssertion(t.Context(), original.Evaluation, corrected); err != nil { + t.Fatal("correcting only the known publisher cell must satisfy the complete assertion", err) + } + if err := checkSuiteAssertion(t.Context(), original.Evaluation, wrong); err == nil { + t.Fatal("changing another publisher cell must still fail the complete assertion") + } + if err := checkSuiteAssertion(t.Context(), original.Evaluation, original.Evaluation.item.node.child(suiteResultVariable)); err == nil { + t.Fatal("in-memory controls changed the original publisher assertion") + } +} + +func qtResidualMonthWeekControls(t *testing.T, options suiteOptions, item suiteCase, initial residualCaseResult, proof *qtResidualProof) { + t.Helper() + for _, version := range []string{suiteVersion30, suiteVersion31} { + t.Run("XPath"+version, func(t *testing.T) { + original := initial + if version != options.version { + selected := options + selected.version = version + original = evaluateResidualCase(t, selected, item) + } + if original.executionError() != nil || original.AssertionError == nil { + t.Fatal("expected unchanged publisher value mismatch", original.executionError(), original.AssertionError) + } + if original.Evaluation.sequence.Len() != 48 { + t.Fatal("original expression must return all 48 date strings", original.Evaluation.sequence.Len()) + } + corrected := cloneResidualNode(item.node.child(suiteResultVariable)) + assertion := corrected.child("assert-string-value") + if assertion == nil || assertion.attr("normalize-space") != suiteTrue { + t.Fatal("pinned publisher assertion shape changed") + } + rows := isoMonthWeekFixture(t) + want, publisher := make([]string, 0, len(rows)), make([]string, 0, len(rows)) + for _, row := range rows { + want = append(want, "["+row.Date+":"+row.Week+"]") + if row.Date == "2006-01-30" { + row.Week = "5" + } + publisher = append(publisher, "["+row.Date+":"+row.Week+"]") + } + if !slices.Equal(strings.Fields(assertion.Text), publisher) { + t.Fatal("original 48 publisher expectations changed") + } + actual := residualSequenceText(t, original.Evaluation.sequence) + if !slices.Equal(strings.Fields(actual), want) { + t.Fatal("full native 48-date result disagrees with normative literals", actual, want) + } + if strings.Count(assertion.Text, "[2006-01-30:5]") != 1 { + t.Fatal("known bad publisher cell must occur exactly once") + } + assertion.Text = strings.Replace(assertion.Text, "[2006-01-30:5]", "[2006-01-30:1]", 1) + wrong := cloneResidualNode(corrected) + wrongValue := wrong.child("assert-string-value") + if strings.Count(wrongValue.Text, "[2005-12-04:1]") != 1 { + t.Fatal("negative control cell must occur exactly once") + } + wrongValue.Text = strings.Replace(wrongValue.Text, "[2005-12-04:1]", "[2005-12-04:9]", 1) + checkISOMonthWeekAssertionCorrection(t, original, corrected, wrong) + proof.Controls = append(proof.Controls, qtResidualProofControl{Name: "XPath" + version + " complete 48-date month-week and one-cell assertion controls", Native: actual, Obligation: "F&O 3.0/3.1 section 9.8.4 Thursday-month rule; original assertion remains failed, 47 other publisher cells remain unchanged"}) + }) + } +} + +func runXSLTResidualCalendarMonthWeek(t *testing.T, directory string) { + t.Helper() + t.Run("format-date-011", func(t *testing.T) { + item := loadPinnedResidualCase(t, directory, suiteXSLT, "tests/fn/format-date/_format-date-test-set.xml", "format-date-011") + for _, version := range []string{suiteVersion20, suiteVersion30} { + t.Run("XSLT"+version, func(t *testing.T) { + options := suiteOptions{kind: suiteXSLT, directory: directory, version: version, xsdVersion: suiteVersion10, timeout: time.Second, limitsProfile: "default"} + original := evaluateResidualCase(t, options, item) + if original.executionError() != nil || original.AssertionError == nil { + t.Fatal("expected unchanged publisher XML value mismatch", original.executionError(), original.AssertionError) + } + corrected := cloneResidualNode(item.node.child(suiteResultVariable)) + assertion := corrected.child("assert-xml") + if assertion == nil || assertion.attr(suiteFile) != "format-date-011.out" { + t.Fatal("pinned publisher XML assertion shape changed") + } + raw, err := suiteExpectedBytes(original.Evaluation, assertion) + if err != nil { + t.Fatal(err) + } + var publisher struct { + XMLName xml.Name `xml:"out"` + Rows []isoMonthWeekFixturePair `xml:"t"` + } + if err := xml.Unmarshal(raw, &publisher); err != nil { + t.Fatal(err) + } + checkISOMonthWeekRows(t, publisher.Rows, true) + actualBytes, err := original.Evaluation.serialized(t.Context()) + if err != nil { + t.Fatal(err) + } + actual := publisher + actual.Rows = nil + if err := xml.Unmarshal(actualBytes, &actual); err != nil { + t.Fatal(err) + } + checkISOMonthWeekRows(t, actual.Rows, false) + const oldCell = `5` + if bytes.Count(raw, []byte(oldCell)) != 1 { + t.Fatal("known bad publisher XML cell must occur exactly once") + } + assertion.Text = strings.Replace(string(raw), oldCell, `1`, 1) + assertion.Attributes = slices.DeleteFunc(assertion.Attributes, func(a xml.Attr) bool { return a.Name.Space == "" && a.Name.Local == suiteFile }) + wrong := cloneResidualNode(corrected) + wrongValue := wrong.child("assert-xml") + const otherCell = `1` + if strings.Count(wrongValue.Text, otherCell) != 1 { + t.Fatal("negative XML control cell must occur exactly once") + } + wrongValue.Text = strings.Replace(wrongValue.Text, otherCell, `9`, 1) + checkISOMonthWeekAssertionCorrection(t, original, corrected, wrong) + // XSLT 2.0 does not spell out this month-week extension. Its check + // records our chosen convention, not a later-version mandate. + }) + } + }) +} + +func TestISOCalendarMonthWeekPrimaryExamples(t *testing.T) { + e, err := transform.NewEngine(transform.Config{}) + if err != nil { + t.Fatal(err) + } + for _, version := range []struct { + name string + value transform.XPathVersion + }{{"XPath3.0", transform.XPath30}, {"XPath3.1", transform.XPath31}} { + t.Run(version.name, func(t *testing.T) { + for _, test := range []struct { + name, date, want string + published bool + }{ + {"January29", "2013-01-29", "5", true}, + {"February1", "2013-02-01", "5", true}, + {"BeforeBoundary", "2006-01-29", "4", false}, + {"January30Boundary", "2006-01-30", "1", false}, + {"January31Boundary", "2006-01-31", "1", false}, + {"February1Boundary", "2006-02-01", "1", false}, + {"February2Boundary", "2006-02-02", "1", false}, + {"February5Boundary", "2006-02-05", "1", false}, + } { + t.Run(test.name, func(t *testing.T) { + expression := `format-date(xs:date('` + test.date + `'),'[w]','en','ISO',())` + query, err := e.CompileXPath(t.Context(), expression, transform.XPathOptions{Version: version.value}) + if err != nil { + t.Fatal(err) + } + result, err := query.Evaluate(t.Context(), nil, transform.XPathContext{}) + if err != nil { + t.Fatal(err) + } + text, err := result.Text() + if err != nil { + t.Fatal(err) + } else if result.Len() != 1 || text != test.want { + t.Fatal("primary calendar example", text, result.Len(), err, test.want) + } + if test.published { + // The REC prints only the 2013 examples with result 5. The derived 2006 + // boundary controls deliberately emit no published-vector marker. + t.Logf("P3_VECTOR %q", test.want) + } + }) + } + }) + } +} diff --git a/tests/cmd/dev/suites.go b/tests/cmd/dev/suites.go index 31d9533..caf8b1c 100644 --- a/tests/cmd/dev/suites.go +++ b/tests/cmd/dev/suites.go @@ -31,8 +31,24 @@ import ( "github.com/skarm/xmlsec/transform" ) +// The harness pins its default dynamic language and per-case timeout. The language profile explicitly raises individual resource budgets; prepared runs and direct suite execution use the same timeout default. +const ( + // Pinned language for dependency matching, evaluation and reports; it does not read the process locale. + suiteDefaultLanguage = "en" + // Per-case wall-clock default, also emitted by the preparation launcher. + suiteDefaultTimeout = time.Second + suiteLanguageInputBytes = 32 << 20 + suiteLanguageResourceBytes = 64 << 20 + suiteLanguageProgramBytes = 1 << 20 + suiteLanguageOutputBytes = 64 << 20 + suiteLanguageNodes = 2_000_000 + suiteLanguageOperations = 50_000_000 +) + const suiteXSLTNamespace = "http://www.w3.org/2012/10/xslt-test-catalog" +// Outcome spellings distinguish harness failures and resource exhaustion from +// normative error expectations; unsupported assertions cannot pass as errors. const ( suiteVersion10 = "1.0" suiteVersion11 = "1.1" @@ -284,9 +300,9 @@ func suiteSourceDigest(ctx context.Context, root string) (string, error) { func suiteLimits(options suiteOptions) transform.Limits { limits := transform.DefaultLimits() if options.limitsProfile == suiteLanguage { - limits.MaxInputBytes, limits.MaxResourceBytes = 32<<20, 64<<20 - limits.MaxProgramBytes, limits.MaxOutputBytes = 1<<20, 64<<20 - limits.MaxNodes, limits.MaxOperations = 2_000_000, 50_000_000 + limits.MaxInputBytes, limits.MaxResourceBytes = suiteLanguageInputBytes, suiteLanguageResourceBytes + limits.MaxProgramBytes, limits.MaxOutputBytes = suiteLanguageProgramBytes, suiteLanguageOutputBytes + limits.MaxNodes, limits.MaxOperations = suiteLanguageNodes, suiteLanguageOperations } return limits @@ -329,7 +345,7 @@ func parseSuiteOptions(args []string, stderr io.Writer) (suiteOptions, error) { setPattern := flags.String("sets", ".*", "test-set name pattern") casePattern := flags.String("match", ".*", "test-case name pattern") flags.IntVar(&options.limit, "limit", 0, "selected-case limit; remaining cases are explicitly skipped") - flags.DurationVar(&options.timeout, "timeout", time.Second, "per-case wall-clock budget") + flags.DurationVar(&options.timeout, "timeout", suiteDefaultTimeout, "per-case wall-clock budget") currentTime := flags.String("current-time", suiteDefaultCurrentTime, "fixed RFC3339 dynamic-context time shared by cases and assertions") if err := flags.Parse(args); err != nil { @@ -599,7 +615,7 @@ func writeSuiteReport(path string, out io.Writer, report *suiteReport) error { return writeJSONListObject(dst, metadata, "cases", report.Cases, func(_ io.Writer, item *suiteOutcome) error { return encoder.Encode(item) }) } if path != "" { - return writeJSONFile(path, len(report.Cases) > 128, write) + return writeJSONFile(path, len(report.Cases) > reportStreamingThreshold, write) } return write(out) @@ -642,7 +658,7 @@ func executeSuite(ctx context.Context, options suiteOptions, pin suitePin) (*sui "case_timeout": options.timeout.String(), "current_time": suiteDynamicContext(options.currentTime).CurrentTime.Format(time.RFC3339Nano), "implicit_timezone": "UTC", - "default_language": "en", + "default_language": suiteDefaultLanguage, "default_collation": "Unicode codepoint", "schema_aware": false, "xsd_version": suiteXSDVersionName(options), diff --git a/tests/cmd/dev/suites_assert.go b/tests/cmd/dev/suites_assert.go index c68e553..ae8e910 100644 --- a/tests/cmd/dev/suites_assert.go +++ b/tests/cmd/dev/suites_assert.go @@ -20,6 +20,12 @@ import ( "github.com/skarm/xmlsec/transform" ) +// Mismatch diagnostics retain bounded byte excerpts on both sides of the first difference. +const ( + reportExcerptBeforeBytes = 8 + reportExcerptAfterBytes = 24 +) + const ( suiteUTF16BE = "utf-16be" suiteUTF16LE = "utf-16le" @@ -323,7 +329,7 @@ func suitePermutationAssertion(ctx context.Context, evaluation *suiteEvaluation, } func suiteExpectedBytes(e *suiteEvaluation, assertion *suiteNode) ([]byte, error) { - if file := assertion.attr("file"); file != "" { + if file := assertion.attr(suiteFile); file != "" { raw, _, err := readSuiteReferencedFile(e.options.directory, e.item.base, assertion) if err != nil { return nil, suiteHarness("expected result file: %w", err) @@ -657,7 +663,7 @@ func suiteSerializationAssertion(ctx context.Context, e *suiteEvaluation, assert } if assertion.XMLName.Local == "serialization-matches" { - encoding := "UTF-8" + encoding := lexicon.EncodingUTF8U if e.result != nil { encoding, err = e.result.OutputEncoding(e.secondaryURI) if err != nil { @@ -677,8 +683,8 @@ func suiteSerializationAssertion(ctx context.Context, e *suiteEvaluation, assert // bare CR data and every byte of the serializer's actual output. encoding := strings.ToLower(assertion.attr("encoding")) - textFile := encoding == "" || encoding == "utf-8" || encoding == "utf8" || encoding == "iso-8859-1" || encoding == "us-ascii" - if assertion.attr(suiteFile) != "" && textFile && !bytes.HasPrefix(expected, []byte{0xff, 0xfe}) && !bytes.HasPrefix(expected, []byte{0xfe, 0xff}) { + textFile := encoding == "" || encoding == lexicon.EncodingUTF8 || encoding == lexicon.EncodingUTF8Alt || encoding == "iso-8859-1" || encoding == "us-ascii" + if assertion.attr(suiteFile) != "" && textFile && !bytes.HasPrefix(expected, []byte(lexicon.BOMUTF16LE)) && !bytes.HasPrefix(expected, []byte(lexicon.BOMUTF16BE)) { expected = bytes.ReplaceAll(expected, []byte("\r\n"), []byte("\n")) } @@ -693,9 +699,9 @@ func compareSuiteBytes(kind string, actual, expected []byte) error { offset++ } - start := max(0, offset-8) + start := max(0, offset-reportExcerptBeforeBytes) - return fmt.Errorf("%s bytes differ at offset %d (actual %d bytes, expected %d): got %q, want %q", kind, offset, len(actual), len(expected), actual[start:min(len(actual), offset+24)], expected[start:min(len(expected), offset+24)]) + return fmt.Errorf("%s bytes differ at offset %d (actual %d bytes, expected %d): got %q, want %q", kind, offset, len(actual), len(expected), actual[start:min(len(actual), offset+reportExcerptAfterBytes)], expected[start:min(len(expected), offset+reportExcerptAfterBytes)]) } return nil @@ -709,7 +715,7 @@ func suiteSerializedText(data []byte, encoding string) (string, error) { return suiteSerializedUTF16(data, label) } - if label == "" || label == "utf-8" || label == "utf8" { + if label == "" || label == lexicon.EncodingUTF8 || label == lexicon.EncodingUTF8Alt { if !utf8.Valid(data) { return "", errors.New("serialized text is not UTF-8") } @@ -750,8 +756,8 @@ func suiteSerializedText(data []byte, encoding string) (string, error) { } func suiteSerializedUTF16(data []byte, encoding string) (string, error) { - littleBOM := bytes.HasPrefix(data, []byte{0xff, 0xfe}) - bigBOM := bytes.HasPrefix(data, []byte{0xfe, 0xff}) + littleBOM := bytes.HasPrefix(data, []byte(lexicon.BOMUTF16LE)) + bigBOM := bytes.HasPrefix(data, []byte(lexicon.BOMUTF16BE)) little := encoding == suiteUTF16LE switch encoding { diff --git a/tests/cmd/dev/suites_environment.go b/tests/cmd/dev/suites_environment.go index c893a76..4e84280 100644 --- a/tests/cmd/dev/suites_environment.go +++ b/tests/cmd/dev/suites_environment.go @@ -22,6 +22,13 @@ import ( "github.com/skarm/xmlsec/transform" ) +// Environment references are counted during resolution. Output URI and language/catalog identities come from the deterministic offline harness context. +const ( + suiteBaseOutputURI = "file:///xmlsec-suite-output/" + suiteQT3Namespace = "http://www.w3.org/2010/09/qt-fots-catalog" + suiteMaximumEnvironmentReferences = 64 +) + func suiteApplicability(options suiteOptions, item suiteCase) (string, string) { var ( dependencies []*suiteNode @@ -108,7 +115,7 @@ func suiteEnvironmentApplicability(item suiteCase) (string, string) { // annotations to run those cases as untyped Basic input. XSLT's catalog has // a different declaration contract, so unused XSLT schemas stay applicable. for _, schema := range ref.node.children("schema") { - if schema.XMLName.Space == "http://www.w3.org/2010/09/qt-fots-catalog" && !suiteBuiltinJSONSchema(schema) { + if schema.XMLName.Space == suiteQT3Namespace && !suiteBuiltinJSONSchema(schema) { return suiteNotApplicable, "QT3 catalog schema declaration implies schema-awareness, which is outside the selected Basic profile (catalog-schema.xsd: schema)" } } @@ -134,7 +141,7 @@ func suiteSourceRequirements(source *suiteNode) (string, string) { continue } - if value != "true" && value != "1" { + if value != suiteTrue && value != "1" { return suiteFailed, fmt.Sprintf("invalid source %s=%q", name, value) } @@ -192,15 +199,15 @@ func suiteDependency(options suiteOptions, kind, value string) (bool, bool) { // and case mappings can change between Unicode releases. return value == strings.TrimSuffix(unicode.Version, ".0"), true case "default-language", "default_language_for_numbering": - return value == "en", true + return value == suiteDefaultLanguage, true case suiteLanguage, "languages_for_numbering": - return value == "en", true + return value == suiteDefaultLanguage, true case "unicode-normalization-form": return slices.Contains([]string{"NFC", "NFD", "NFKC", "NFKD", "FULLY-NORMALIZED"}, value), true case "additional_normalization_form": return strings.HasPrefix(value, "support "), true case "default_output_encoding": - return value == "UTF-8", true + return value == lexicon.EncodingUTF8U, true case "on-multiple-match": return value == "recover", true case "maximum_number_of_decimal_digits": @@ -235,7 +242,7 @@ func suiteImplementationDependency(options suiteOptions, kind, value string) (bo case "ignore_doc_failure": return false, true case "unparsed_text_encoding": - return strings.EqualFold(value, "UTF-8"), true + return strings.EqualFold(value, lexicon.EncodingUTF8U), true case "default_html_version": return value == "5", true case "available_documents": @@ -523,7 +530,7 @@ func prepareSuiteEnvironmentWithLimits(ctx context.Context, options suiteOptions return nil, err } - environment := &preparedSuiteEnvironment{currentTime: options.currentTime, engine: engine, namespaces: make(map[string]string), variables: make(transform.Bindings), staticParameters: make(transform.Bindings), baseURI: suiteFileURI(item.base) + "/", resources: resources, baseOutputURI: "file:///xmlsec-suite-output/"} + environment := &preparedSuiteEnvironment{currentTime: options.currentTime, engine: engine, namespaces: make(map[string]string), variables: make(transform.Bindings), staticParameters: make(transform.Bindings), baseURI: suiteFileURI(item.base) + "/", resources: resources, baseOutputURI: suiteBaseOutputURI} suiteNamespaceDeclarations(item.setNode, environment.namespaces) suiteNamespaceDeclarations(item.node, environment.namespaces) @@ -568,7 +575,7 @@ func prepareSuiteEnvironmentWithLimits(ctx context.Context, options suiteOptions } func resolveSuiteEnvironment(item suiteCase, ref suiteEnvironmentRef, seen map[*suiteNode]struct{}) (suiteEnvironmentRef, error) { - if _, exists := seen[ref.node]; exists || len(seen) >= 64 { + if _, exists := seen[ref.node]; exists || len(seen) >= suiteMaximumEnvironmentReferences { return suiteEnvironmentRef{}, suiteHarness("environment reference cycle or depth limit") } @@ -754,7 +761,7 @@ func loadSuiteSchema(root, base string, resources *suiteResourceResolver, enviro } func suiteBuiltinJSONSchema(node *suiteNode) bool { - return node.XMLName.Space == "http://www.w3.org/2010/09/qt-fots-catalog" && node.attr("role") == "import" && node.attr("uri") == lexicon.NamespaceFn && node.attr(suiteFile) == "" + return node.XMLName.Space == suiteQT3Namespace && node.attr("role") == "import" && node.attr("uri") == lexicon.NamespaceFn && node.attr(suiteFile) == "" } func loadSuiteOutput(base string, environment *preparedSuiteEnvironment, node *suiteNode) error { @@ -1105,7 +1112,7 @@ func suiteSourceValue(ctx context.Context, environment *preparedSuiteEnvironment func registerSuiteResource(root, base string, resources *suiteResourceResolver, staticBase string, node *suiteNode) (string, error) { file, err := url.Parse(node.attr(suiteFile)) - if err == nil && node.XMLName.Local == suiteResourceTag && (file.Scheme == "http" || file.Scheme == "https") && file.Host != "" && file.User == nil && file.Fragment == "" { + if err == nil && node.XMLName.Local == suiteResourceTag && (file.Scheme == lexicon.SchemeHTTP || file.Scheme == lexicon.SchemeHTTPS) && file.Host != "" && file.User == nil && file.Fragment == "" { return registerSuiteRemoteResource(resources, staticBase, node, file.String()) } @@ -1399,5 +1406,5 @@ func suiteDynamicContext(currentTime time.Time) transform.DynamicContext { currentTime, _ = time.Parse(time.RFC3339Nano, suiteDefaultCurrentTime) } - return transform.DynamicContext{CurrentTime: currentTime, ImplicitTimezone: time.UTC, DefaultLanguage: "en"} + return transform.DynamicContext{CurrentTime: currentTime, ImplicitTimezone: time.UTC, DefaultLanguage: suiteDefaultLanguage} } diff --git a/tests/consumer_test.go b/tests/consumer_test.go index 8268084..a86d9dc 100644 --- a/tests/consumer_test.go +++ b/tests/consumer_test.go @@ -93,7 +93,7 @@ func TestExternalConsumer(t *testing.T) { } testArgs = append(testArgs, "./...") - for _, args := range [][]string{{"mod", "edit", "-replace=github.com/skarm/xmlsec=" + root}, testArgs} { + for _, args := range [][]string{{"mod", "edit", "-replace=github.com/skarm/xmlsec=" + root}, {"mod", "tidy", "-diff"}, testArgs} { cmd := exec.CommandContext(ctx, "go", args...) cmd.Dir = dir diff --git a/tests/integration/evidence/evidence_test.go b/tests/integration/evidence/evidence_test.go index a0f54fc..2963709 100644 --- a/tests/integration/evidence/evidence_test.go +++ b/tests/integration/evidence/evidence_test.go @@ -99,8 +99,16 @@ func testIndependentEvidenceCase(t *testing.T, base string, tc evidenceFixture) if e != nil || r == nil { t.Fatalf("%v %+v", e, rep) } - } else if e == nil || r != nil || string(rep.Status) != tc.Status { - t.Fatalf("%v %+v", e, rep) + } else { + if e == nil || r != nil { + t.Fatalf("%v %+v", e, rep) + } + + // VerifyOCSP returns diagnostic status even when verification fails. + //noinspection GoDfaErrorMayBeNotNil + if string(rep.Status) != tc.Status { + t.Fatalf("%v %+v", e, rep) + } } case "timestamp", "xades", "archive": checkTimedEvidence(t, tc, read, at) diff --git a/tests/integration/saml/benchmark_test.go b/tests/integration/saml/benchmark_test.go index 13924ba..37b8fd7 100644 --- a/tests/integration/saml/benchmark_test.go +++ b/tests/integration/saml/benchmark_test.go @@ -34,6 +34,8 @@ import ( "github.com/skarm/xmlsec/transform" ) +const benchmarkPendingRefreshInterval = 4096 + // BenchmarkLogin includes SP initiation, IdP verification and issuance, and SP consumption. func BenchmarkLogin(b *testing.B) { s := setup(b) @@ -64,7 +66,7 @@ func BenchmarkSPBegin(b *testing.B) { for b.Loop() { // Keep the real bounded store, but renew fixtures outside the timer // before it fills so long benchmarks do not measure ErrCapacity. - if pending == 4096 { + if pending == benchmarkPendingRefreshInterval { b.StopTimer() s = setup(b) pending = 0 @@ -279,6 +281,148 @@ func TestSustainedLoad(t *testing.T) { runtime.KeepAlive(sizes) } +// BenchmarkSPBeginPOST measures signed POST request creation, including pending state. +func BenchmarkSPBeginPOST(b *testing.B) { + s := setup(b) + browser := make([]byte, 32) + options := sp.BeginOptions{IDP: idpID, Binding: metadata.HTTPPost, BrowserBinding: browser, At: testcert.Now()} + b.ReportAllocs() + pending := 0 + for b.Loop() { + if pending == benchmarkPendingRefreshInterval { + b.StopTimer() + s = setup(b) + pending = 0 + b.StartTimer() + } + if _, err := s.sp.Begin(b.Context(), options); err != nil { + b.Fatal(err) + } + pending++ + } +} + +// BenchmarkLoginParallel covers shared metadata, request/replay stores and compression. +func BenchmarkLoginParallel(b *testing.B) { + operation, _ := soakSAML(b) + var serial atomic.Uint64 + b.ReportAllocs() + b.ResetTimer() + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + if err := operation(b.Context(), serial.Add(1)); err != nil { + b.Error(err) + return + } + } + }) +} + +// BenchmarkIssuedPOST compares collection with direct output using the same +// immutable signed message and limits. Fixture signing is outside the timer. +func BenchmarkIssuedPOST(b *testing.B) { + for _, size := range []int{0, 512 << 10} { + b.Run(strconv.Itoa(size), func(b *testing.B) { + s := setup(b) + out, request, _ := begin(b, s, metadata.HTTPRedirect) + authentication := auth() + if size > 0 { + authentication.Attributes = make([]protocol.Attribute, 64) + for i := range authentication.Attributes { + authentication.Attributes[i] = protocol.Attribute{Name: fmt.Sprintf("a%d", i), Values: []string{strings.Repeat("x", 4096), strings.Repeat("é", 2048)}} + } + } + issued, err := s.idp.Issue(b.Context(), request, authentication, testcert.Now().Add(2*time.Second)) + if err != nil { + b.Fatal(err) + } + relay := out.RelayState() + for name, write := range map[string]func() error{ + "URLBuffer": func() error { + _, err := binding.EncodePOST(b.Context(), issued.Document(), binding.Response, &relay, binding.POSTLimits{}) + return err + }, + "URLWriter": func() error { _, err := issued.WritePOST(b.Context(), io.Discard); return err }, + "HTMLBuffer": func() error { + _, err := binding.RenderPOSTForm(b.Context(), issued.Destination(), issued.Document(), binding.Response, &relay, binding.POSTLimits{}) + return err + }, + "HTMLWriter": func() error { _, err := issued.WritePOSTForm(b.Context(), io.Discard); return err }, + } { + b.Run(name, func(b *testing.B) { + b.ReportAllocs() + for b.Loop() { + if err := write(); err != nil { + b.Fatal(err) + } + } + }) + } + }) + } +} + +func soakPauseSnapshot() metrics.Float64Histogram { + sample := []metrics.Sample{{Name: "/gc/pauses:seconds"}} + metrics.Read(sample) + value := sample[0].Value.Float64Histogram() + return metrics.Float64Histogram{Counts: append([]uint64(nil), value.Counts...), Buckets: append([]float64(nil), value.Buckets...)} +} + +func soakPausePercentiles(before, after metrics.Float64Histogram) map[string]float64 { + counts := make([]uint64, len(after.Counts)) + var total uint64 + for i := range counts { + counts[i] = after.Counts[i] - before.Counts[i] + total += counts[i] + } + quantile := func(percent uint64) float64 { + if total == 0 { + return 0 + } + var sum uint64 + for i, count := range counts { + sum += count + if sum >= (total*percent+99)/100 { + return after.Buckets[i+1] * 1000 + } + } + return 0 + } + return map[string]float64{"p50": quantile(50), "p95": quantile(95), "p99": quantile(99)} +} + +// BenchmarkSoakInstrumentation isolates the latency/count bookkeeping around +// the same complete login. Runtime memory sampling and OS RSS collection are +// outside this benchmark and their overhead is reported separately. +func BenchmarkSoakInstrumentation(b *testing.B) { + for _, instrumented := range []bool{false, true} { + b.Run(strconv.FormatBool(instrumented), func(b *testing.B) { + operation, _ := soakSAML(b) + var serial uint64 + var completed atomic.Uint64 + var histogram [2048]atomic.Uint64 + b.ReportAllocs() + for b.Loop() { + serial++ + if !instrumented { + if err := operation(b.Context(), serial); err != nil { + b.Fatal(err) + } + continue + } + began := time.Now() + if err := operation(b.Context(), serial); err != nil { + b.Fatal(err) + } + completed.Add(1) + bucket := min(len(histogram)-1, max(0, int(math.Log2(float64(max(1, time.Since(began).Nanoseconds())))*32))) + histogram[bucket].Add(1) + } + }) + } +} + func soakCPU() float64 { samples := []metrics.Sample{{Name: "/cpu/classes/total:cpu-seconds"}, {Name: "/cpu/classes/idle:cpu-seconds"}} metrics.Read(samples) @@ -454,145 +598,3 @@ func soakSAML(t testing.TB) (func(context.Context, uint64) error, func() any) { return map[string]int{"pending": requests.Len(), "sp_replay": sr.Len(), "idp_replay": ir.Len()} } } - -// BenchmarkSPBeginPOST measures signed POST request creation, including pending state. -func BenchmarkSPBeginPOST(b *testing.B) { - s := setup(b) - browser := make([]byte, 32) - options := sp.BeginOptions{IDP: idpID, Binding: metadata.HTTPPost, BrowserBinding: browser, At: testcert.Now()} - b.ReportAllocs() - pending := 0 - for b.Loop() { - if pending == 4096 { - b.StopTimer() - s = setup(b) - pending = 0 - b.StartTimer() - } - if _, err := s.sp.Begin(b.Context(), options); err != nil { - b.Fatal(err) - } - pending++ - } -} - -// BenchmarkLoginParallel covers shared metadata, request/replay stores and compression. -func BenchmarkLoginParallel(b *testing.B) { - operation, _ := soakSAML(b) - var serial atomic.Uint64 - b.ReportAllocs() - b.ResetTimer() - b.RunParallel(func(pb *testing.PB) { - for pb.Next() { - if err := operation(b.Context(), serial.Add(1)); err != nil { - b.Error(err) - return - } - } - }) -} - -// BenchmarkIssuedPOST compares collection with direct output using the same -// immutable signed message and limits. Fixture signing is outside the timer. -func BenchmarkIssuedPOST(b *testing.B) { - for _, size := range []int{0, 512 << 10} { - b.Run(strconv.Itoa(size), func(b *testing.B) { - s := setup(b) - out, request, _ := begin(b, s, metadata.HTTPRedirect) - authentication := auth() - if size > 0 { - authentication.Attributes = make([]protocol.Attribute, 64) - for i := range authentication.Attributes { - authentication.Attributes[i] = protocol.Attribute{Name: fmt.Sprintf("a%d", i), Values: []string{strings.Repeat("x", 4096), strings.Repeat("é", 2048)}} - } - } - issued, err := s.idp.Issue(b.Context(), request, authentication, testcert.Now().Add(2*time.Second)) - if err != nil { - b.Fatal(err) - } - relay := out.RelayState() - for name, write := range map[string]func() error{ - "URLBuffer": func() error { - _, err := binding.EncodePOST(b.Context(), issued.Document(), binding.Response, &relay, binding.POSTLimits{}) - return err - }, - "URLWriter": func() error { _, err := issued.WritePOST(b.Context(), io.Discard); return err }, - "HTMLBuffer": func() error { - _, err := binding.RenderPOSTForm(b.Context(), issued.Destination(), issued.Document(), binding.Response, &relay, binding.POSTLimits{}) - return err - }, - "HTMLWriter": func() error { _, err := issued.WritePOSTForm(b.Context(), io.Discard); return err }, - } { - b.Run(name, func(b *testing.B) { - b.ReportAllocs() - for b.Loop() { - if err := write(); err != nil { - b.Fatal(err) - } - } - }) - } - }) - } -} - -func soakPauseSnapshot() metrics.Float64Histogram { - sample := []metrics.Sample{{Name: "/gc/pauses:seconds"}} - metrics.Read(sample) - value := sample[0].Value.Float64Histogram() - return metrics.Float64Histogram{Counts: append([]uint64(nil), value.Counts...), Buckets: append([]float64(nil), value.Buckets...)} -} - -func soakPausePercentiles(before, after metrics.Float64Histogram) map[string]float64 { - counts := make([]uint64, len(after.Counts)) - var total uint64 - for i := range counts { - counts[i] = after.Counts[i] - before.Counts[i] - total += counts[i] - } - quantile := func(percent uint64) float64 { - if total == 0 { - return 0 - } - var sum uint64 - for i, count := range counts { - sum += count - if sum >= (total*percent+99)/100 { - return after.Buckets[i+1] * 1000 - } - } - return 0 - } - return map[string]float64{"p50": quantile(50), "p95": quantile(95), "p99": quantile(99)} -} - -// BenchmarkSoakInstrumentation isolates the latency/count bookkeeping around -// the same complete login. Runtime memory sampling and OS RSS collection are -// outside this benchmark and their overhead is reported separately. -func BenchmarkSoakInstrumentation(b *testing.B) { - for _, instrumented := range []bool{false, true} { - b.Run(strconv.FormatBool(instrumented), func(b *testing.B) { - operation, _ := soakSAML(b) - var serial uint64 - var completed atomic.Uint64 - var histogram [2048]atomic.Uint64 - b.ReportAllocs() - for b.Loop() { - serial++ - if !instrumented { - if err := operation(b.Context(), serial); err != nil { - b.Fatal(err) - } - continue - } - began := time.Now() - if err := operation(b.Context(), serial); err != nil { - b.Fatal(err) - } - completed.Add(1) - bucket := min(len(histogram)-1, max(0, int(math.Log2(float64(max(1, time.Since(began).Nanoseconds())))*32))) - histogram[bucket].Add(1) - } - }) - } -} diff --git a/tests/integration/saml/interop_test.go b/tests/integration/saml/interop_test.go index a4f67a7..2b9da34 100644 --- a/tests/integration/saml/interop_test.go +++ b/tests/integration/saml/interop_test.go @@ -35,6 +35,9 @@ func TestIndependentSignedSAMLFixtures(t *testing.T) { if e := json.Unmarshal(read("manifest.json"), &cases); e != nil { t.Fatal(e) } + if len(cases) != 16 { + t.Fatalf("independent SAML fixture count = %d, want 16", len(cases)) + } for _, c := range cases { t.Run(c.Name, func(t *testing.T) { diff --git a/tests/integration/saml/sso_test.go b/tests/integration/saml/sso_test.go index 678deee..6bf6a01 100644 --- a/tests/integration/saml/sso_test.go +++ b/tests/integration/saml/sso_test.go @@ -180,6 +180,118 @@ func TestUnspecifiedNameIDDoesNotRequireLiteralUnspecifiedFormat(t *testing.T) { } } +func TestOutboundPOSTSerialization(t *testing.T) { + s := setup(t) + out, request, _ := begin(t, s, metadata.HTTPPost) + issued, err := s.idp.Issue(t.Context(), request, auth(), testcert.Now().Add(2*time.Second)) + if err != nil { + t.Fatal(err) + } + sentinel := errors.New("late sink failure") + for name, write := range map[string]func(context.Context, io.Writer) (int64, error){ + "request": out.WritePOST, "response": issued.WritePOST, + "requestHTML": out.WritePOSTForm, "responseHTML": issued.WritePOSTForm, + } { + t.Run(name, func(t *testing.T) { + var expected bytes.Buffer + count, err := write(t.Context(), &expected) + if err != nil || count != int64(expected.Len()) || expected.Len() == 0 { + t.Fatalf("output: %d %v", count, err) + } + var repeated bytes.Buffer + if _, err := write(t.Context(), &repeated); err != nil || !bytes.Equal(expected.Bytes(), repeated.Bytes()) { + t.Fatalf("repeatability: %v", err) + } + ctx, cancel := context.WithCancel(t.Context()) + cancel() + var untouched bytes.Buffer + if n, err := write(ctx, &untouched); n != 0 || untouched.Len() != 0 || !errors.Is(err, context.Canceled) { + t.Fatalf("canceled: %d %v", n, err) + } + if _, err := write(t.Context(), &postFailureWriter{}); !errors.Is(err, io.ErrShortWrite) { + t.Fatalf("short write: %v", err) + } + if n, err := write(t.Context(), &postFailureWriter{remaining: 512, err: sentinel}); n == 0 || !errors.Is(err, sentinel) { + t.Fatalf("late failure: %d %v", n, err) + } + repeated.Reset() + if _, err := write(t.Context(), &repeated); err != nil || !bytes.Equal(expected.Bytes(), repeated.Bytes()) { + t.Fatalf("failed writer mutated message: %v", err) + } + + var workers sync.WaitGroup + for range 8 { + workers.Go(func() { + var concurrent bytes.Buffer + if _, err := write(t.Context(), &concurrent); err != nil || !bytes.Equal(expected.Bytes(), concurrent.Bytes()) { + t.Errorf("concurrent serialization: %v", err) + } + }) + } + workers.Wait() + }) + } + redirect, _, _ := begin(t, s, metadata.HTTPRedirect) + if _, err := redirect.WritePOST(t.Context(), io.Discard); !errors.Is(err, sp.ErrPolicy) { + t.Fatal(err) + } + if _, err := redirect.WritePOSTForm(t.Context(), io.Discard); !errors.Is(err, sp.ErrPolicy) { + t.Fatal(err) + } +} + +type postFailureWriter struct { + remaining int + err error +} + +func (w *postFailureWriter) Write(p []byte) (int, error) { + if w.remaining <= 0 { + return 0, w.err + } + n := min(w.remaining, len(p)) + w.remaining -= n + if n < len(p) { + return n, w.err + } + return n, nil +} + +func TestPOSTLimitPrecedesStoreCommit(t *testing.T) { + s := setup(t) + requests, err := state.NewMemoryStore(state.Config{}) + if err != nil { + t.Fatal(err) + } + sr, err := replay.NewMemoryStore(replay.Config{}) + if err != nil { + t.Fatal(err) + } + service, err := sp.NewServiceProvider(sp.Config{EntityID: spID, Metadata: s.registry, Signing: protocol.SigningKey{Key: s.cert.LeafKey, CertificateDER: s.cert.Leaf.Raw}, Requests: requests, Replay: sr, POSTLimits: binding.POSTLimits{MaxFormBytes: 1}}) + if err != nil { + t.Fatal(err) + } + if out, err := service.Begin(t.Context(), sp.BeginOptions{IDP: idpID, Binding: metadata.HTTPPost, At: testcert.Now(), BrowserBinding: bytes.Repeat([]byte{7}, 32)}); out != nil || !errors.Is(err, binding.ErrBinding) || requests.Len() != 0 { + t.Fatalf("pending commit: %v %d", err, requests.Len()) + } + config := s.idpConfig + replayStore := &countReplayStore{Store: config.Replay} + config.Replay = replayStore + config.POSTLimits.MaxFormBytes = 1 + identity, err := idp.NewIdentityProvider(config) + if err != nil { + t.Fatal(err) + } + out, _, _ := begin(t, s, metadata.HTTPRedirect) + request, err := identity.AcceptRedirect(t.Context(), out.RawQuery(), spID, ssoRedirect, testcert.Now()) + if err != nil { + t.Fatal(err) + } + if issued, err := identity.Issue(t.Context(), request, auth(), testcert.Now().Add(2*time.Second)); issued != nil || !errors.Is(err, binding.ErrBinding) || replayStore.calls != 0 { + t.Fatalf("replay commit: %v %d", err, replayStore.calls) + } +} + type registry map[string]*metadata.Snapshot func (r registry) Lookup(ctx context.Context, id string, at time.Time) (*metadata.Entity, error) { @@ -317,118 +429,6 @@ func postBody(t testing.TB, message postSerializer) []byte { return body.Bytes() } -func TestOutboundPOSTSerialization(t *testing.T) { - s := setup(t) - out, request, _ := begin(t, s, metadata.HTTPPost) - issued, err := s.idp.Issue(t.Context(), request, auth(), testcert.Now().Add(2*time.Second)) - if err != nil { - t.Fatal(err) - } - sentinel := errors.New("late sink failure") - for name, write := range map[string]func(context.Context, io.Writer) (int64, error){ - "request": out.WritePOST, "response": issued.WritePOST, - "requestHTML": out.WritePOSTForm, "responseHTML": issued.WritePOSTForm, - } { - t.Run(name, func(t *testing.T) { - var expected bytes.Buffer - count, err := write(t.Context(), &expected) - if err != nil || count != int64(expected.Len()) || expected.Len() == 0 { - t.Fatalf("output: %d %v", count, err) - } - var repeated bytes.Buffer - if _, err := write(t.Context(), &repeated); err != nil || !bytes.Equal(expected.Bytes(), repeated.Bytes()) { - t.Fatalf("repeatability: %v", err) - } - ctx, cancel := context.WithCancel(t.Context()) - cancel() - var untouched bytes.Buffer - if n, err := write(ctx, &untouched); n != 0 || untouched.Len() != 0 || !errors.Is(err, context.Canceled) { - t.Fatalf("canceled: %d %v", n, err) - } - if _, err := write(t.Context(), &postFailureWriter{}); !errors.Is(err, io.ErrShortWrite) { - t.Fatalf("short write: %v", err) - } - if n, err := write(t.Context(), &postFailureWriter{remaining: 512, err: sentinel}); n == 0 || !errors.Is(err, sentinel) { - t.Fatalf("late failure: %d %v", n, err) - } - repeated.Reset() - if _, err := write(t.Context(), &repeated); err != nil || !bytes.Equal(expected.Bytes(), repeated.Bytes()) { - t.Fatalf("failed writer mutated message: %v", err) - } - - var workers sync.WaitGroup - for range 8 { - workers.Go(func() { - var concurrent bytes.Buffer - if _, err := write(t.Context(), &concurrent); err != nil || !bytes.Equal(expected.Bytes(), concurrent.Bytes()) { - t.Errorf("concurrent serialization: %v", err) - } - }) - } - workers.Wait() - }) - } - redirect, _, _ := begin(t, s, metadata.HTTPRedirect) - if _, err := redirect.WritePOST(t.Context(), io.Discard); !errors.Is(err, sp.ErrPolicy) { - t.Fatal(err) - } - if _, err := redirect.WritePOSTForm(t.Context(), io.Discard); !errors.Is(err, sp.ErrPolicy) { - t.Fatal(err) - } -} - -type postFailureWriter struct { - remaining int - err error -} - -func (w *postFailureWriter) Write(p []byte) (int, error) { - if w.remaining <= 0 { - return 0, w.err - } - n := min(w.remaining, len(p)) - w.remaining -= n - if n < len(p) { - return n, w.err - } - return n, nil -} - -func TestPOSTLimitPrecedesStoreCommit(t *testing.T) { - s := setup(t) - requests, err := state.NewMemoryStore(state.Config{}) - if err != nil { - t.Fatal(err) - } - sr, err := replay.NewMemoryStore(replay.Config{}) - if err != nil { - t.Fatal(err) - } - service, err := sp.NewServiceProvider(sp.Config{EntityID: spID, Metadata: s.registry, Signing: protocol.SigningKey{Key: s.cert.LeafKey, CertificateDER: s.cert.Leaf.Raw}, Requests: requests, Replay: sr, POSTLimits: binding.POSTLimits{MaxFormBytes: 1}}) - if err != nil { - t.Fatal(err) - } - if out, err := service.Begin(t.Context(), sp.BeginOptions{IDP: idpID, Binding: metadata.HTTPPost, At: testcert.Now(), BrowserBinding: bytes.Repeat([]byte{7}, 32)}); out != nil || !errors.Is(err, binding.ErrBinding) || requests.Len() != 0 { - t.Fatalf("pending commit: %v %d", err, requests.Len()) - } - config := s.idpConfig - replayStore := &countReplayStore{Store: config.Replay} - config.Replay = replayStore - config.POSTLimits.MaxFormBytes = 1 - identity, err := idp.NewIdentityProvider(config) - if err != nil { - t.Fatal(err) - } - out, _, _ := begin(t, s, metadata.HTTPRedirect) - request, err := identity.AcceptRedirect(t.Context(), out.RawQuery(), spID, ssoRedirect, testcert.Now()) - if err != nil { - t.Fatal(err) - } - if issued, err := identity.Issue(t.Context(), request, auth(), testcert.Now().Add(2*time.Second)); issued != nil || !errors.Is(err, binding.ErrBinding) || replayStore.calls != 0 { - t.Fatalf("replay commit: %v %d", err, replayStore.calls) - } -} - type countReplayStore struct { replay.Store calls int diff --git a/tests/integration/xmldsig/profiles_test.go b/tests/integration/xmldsig/profiles_test.go index 4db9c20..581f3af 100644 --- a/tests/integration/xmldsig/profiles_test.go +++ b/tests/integration/xmldsig/profiles_test.go @@ -42,6 +42,9 @@ func TestNextIndependentFixtures(t *testing.T) { if e = json.Unmarshal(raw, &wire); e != nil { t.Fatal(e) } + if len(wire.Cases) != 34 { + t.Fatalf("independent fixture count = %d, want 34", len(wire.Cases)) + } for _, tc := range wire.Cases { t.Run(tc.Name, func(t *testing.T) { testNextIndependentFixturesCase(t, dir, tc) }) diff --git a/tests/integration/xmldsig/signature_test.go b/tests/integration/xmldsig/signature_test.go index 39a8ec7..125e973 100644 --- a/tests/integration/xmldsig/signature_test.go +++ b/tests/integration/xmldsig/signature_test.go @@ -79,6 +79,8 @@ func testCoreIndependentFixturesCase(t *testing.T, tc *coreFixtureCase, dir *str } var re *xmldsig.ReferenceError + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if result != nil || !errors.Is(err, xmldsig.ErrDigest) || !errors.As(err, &re) || re.Index != tc.FailedReference || report.SignatureValue != xmldsig.Valid { t.Fatal(err, report) } diff --git a/tests/integration/xmldsig/transforms_test.go b/tests/integration/xmldsig/transforms_test.go index 3087937..eb99dcd 100644 --- a/tests/integration/xmldsig/transforms_test.go +++ b/tests/integration/xmldsig/transforms_test.go @@ -33,6 +33,9 @@ func TestJDKV1Transforms(t *testing.T) { if e = json.Unmarshal(raw, &m); e != nil { t.Fatal(e) } + if len(m.Cases) != 12 { + t.Fatalf("transform fixture count = %d, want 12", len(m.Cases)) + } raw, e = os.ReadFile(filepath.Join(dir, m.PublicKey)) if e != nil { @@ -77,8 +80,12 @@ func TestJDKV1Transforms(t *testing.T) { if e != nil || r == nil || report.Status != "valid" { t.Fatal(report, e) } - } else if e == nil || r != nil || report.Status == "valid" { - t.Fatal("tampered transform fixture accepted", report, e) + } else { + // Verification reports retain diagnostics on failure. + // noinspection GoDfaErrorMayBeNotNil + if e == nil || r != nil || report.Status == "valid" { + t.Fatal("tampered transform fixture accepted", report, e) + } } }) } diff --git a/tests/interop/c14n_test.go b/tests/interop/c14n_test.go index e584f49..77d37c0 100644 --- a/tests/interop/c14n_test.go +++ b/tests/interop/c14n_test.go @@ -10,19 +10,28 @@ import ( "encoding/base64" byteorder "encoding/binary" "encoding/json" + "errors" "fmt" "io" "math/rand/v2" + "os" "os/exec" "path/filepath" "slices" "strconv" "strings" + "sync" "testing" "time" "unicode/utf16" ) +const ( + canonicalSessionTimeout = 8 * time.Minute + jdkXPathOperationLimitFlag = "-Djdk.xml.xpathExprOpLimit=10000" + jdkXPathGroupLimitFlag = "-Djdk.xml.xpathExprGrpLimit=10000" +) + func TestCanonicalVectorsAgainstJDK(t *testing.T) { fixture := newInteropFixture(t) selected(t, "portable") @@ -41,6 +50,9 @@ func TestCanonicalVectorsAgainstJDK(t *testing.T) { } `json:"cases"` } decode(t, read(t, filepath.Join(fixture.root, "c14n/testdata/normative-vectors.json")), &corpus) + if len(corpus.Cases) != 138 { + t.Fatalf("canonical fixture count = %d, want 138", len(corpus.Cases)) + } oracle := read(t, filepath.Join(fixture.root, "tests/interop/testdata/C14NOracle.java")) if fmt.Sprintf("%x", sha256.Sum256(oracle)) != corpus.Producer.OracleSHA256 { @@ -96,6 +108,9 @@ func TestCanonicalVectorsAgainstLibXML(t *testing.T) { } `json:"cases"` } decode(t, read(t, filepath.Join(fixture.root, "c14n/testdata/normative-vectors.json")), &corpus) + if len(corpus.Cases) != 138 { + t.Fatalf("canonical fixture count = %d, want 138", len(corpus.Cases)) + } oracle := read(t, filepath.Join(fixture.root, "tests/interop/testdata/c14n-oracle.c")) if fmt.Sprintf("%x", sha256.Sum256(oracle)) != corpus.Producer.OracleSHA256 { @@ -179,11 +194,30 @@ func canonicalCorpus() []string { return docs } +// processStderr permits diagnostics while os/exec still copies child stderr. +// String returns an immutable snapshot, so later writes cannot change its bytes. +type processStderr struct { + mu sync.Mutex + buffer bytes.Buffer +} + +func (s *processStderr) Write(p []byte) (int, error) { + s.mu.Lock() + defer s.mu.Unlock() + return s.buffer.Write(p) +} + +func (s *processStderr) String() string { + s.mu.Lock() + defer s.mu.Unlock() + return s.buffer.String() +} + type lineProcess struct { cmd *exec.Cmd input io.WriteCloser output *bufio.Reader - stderr bytes.Buffer + stderr processStderr cancel context.CancelFunc } @@ -196,7 +230,7 @@ func (p *lineProcess) call(t *testing.T, data []byte) []byte { line, err := p.output.ReadBytes('\n') if err != nil { - t.Fatalf("helper response: %v: %s", err, p.stderr.Bytes()) + t.Fatalf("helper response: %v: %s", err, p.stderr.String()) } return bytes.TrimSuffix(line, []byte{'\n'}) @@ -249,7 +283,7 @@ func newCanonicalSession(t *testing.T, fixture *interopFixture) *canonicalSessio compileJava(t, fixture) c := cOracle(t, fixture) goRPC := lines(t, fixture, binary(t, fixture, "c14nprobe")) - params := append(append([]string{}, javaExports()...), "-Djdk.xml.xpathExprOpLimit=10000", "-Djdk.xml.xpathExprGrpLimit=10000", "-cp", fixture.work, "C14NOracle") + params := append(append([]string{}, javaExports()...), jdkXPathOperationLimitFlag, jdkXPathGroupLimitFlag, "-cp", fixture.work, "C14NOracle") jdk := lines(t, fixture, "java", params...) return &canonicalSession{fixture: fixture, goRPC: goRPC, jdk: jdk, nativeProgram: c} @@ -552,7 +586,7 @@ func lines(t *testing.T, fixture *interopFixture, name string, args ...string) * t.Helper() // These helpers exit on EOF in Cleanup, after the test context is canceled. - ctx, cancel := context.WithTimeout(context.WithoutCancel(t.Context()), 8*time.Minute) + ctx, cancel := context.WithTimeout(context.WithoutCancel(t.Context()), canonicalSessionTimeout) p := &lineProcess{cmd: exec.CommandContext(ctx, name, args...), cancel: cancel} p.cmd.Dir = fixture.root p.cmd.Stderr = &p.stderr @@ -582,7 +616,7 @@ func lines(t *testing.T, fixture *interopFixture, name string, args ...string) * cancel() if err != nil { - t.Errorf("helper exited: %v: %s", err, p.stderr.Bytes()) + t.Errorf("helper exited: %v: %s", err, p.stderr.String()) } }) @@ -660,3 +694,59 @@ func selectionXPath(r canonicalRequest, markers bool) string { return strings.Join(terms, " | ") } + +// TestLineProcessDiagnosticsProcess is only entered by the bounded lifecycle +// test below. The peer deliberately stays alive after stdout closes; production +// cleanup must release it through stdin EOF, without waiting in call(). +func TestLineProcessDiagnosticsProcess(t *testing.T) { + switch os.Getenv("XMLSEC_LINE_PROCESS_DIAGNOSTIC_ROLE") { + case "peer": + _, _ = io.WriteString(os.Stderr, "ordinary helper diagnostic\n") + _, _ = io.WriteString(os.Stdout, "ready\n") + input := bufio.NewReader(os.Stdin) + _, _ = input.ReadString('\n') + if os.Getenv("XMLSEC_LINE_PROCESS_DIAGNOSTIC_MODE") == "eof" { + _ = os.Stdout.Close() + } else { + _, _ = io.WriteString(os.Stdout, "ok\n") + } + _, _ = input.ReadString('\n') + case "caller": + t.Setenv("XMLSEC_LINE_PROCESS_DIAGNOSTIC_ROLE", "peer") + p := lines(t, &interopFixture{root: t.TempDir()}, os.Args[0], "-test.run=^TestLineProcessDiagnosticsProcess$") + ready, err := p.output.ReadString('\n') + if err != nil || ready != "ready\n" { + t.Fatalf("helper readiness: %q: %v", ready, err) + } + if got := p.call(t, []byte("request")); string(got) != "ok" { + t.Fatalf("helper reply: %q", got) + } + default: + t.Skip("child process used by TestLineProcessDiagnostics") + } +} + +func TestLineProcessDiagnostics(t *testing.T) { + for _, mode := range []string{"reply", "eof"} { + t.Run(mode, func(t *testing.T) { + ctx, cancel := context.WithTimeout(t.Context(), 10*time.Second) + defer cancel() + command := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestLineProcessDiagnosticsProcess$") + command.Env = append(os.Environ(), "XMLSEC_LINE_PROCESS_DIAGNOSTIC_ROLE=caller", "XMLSEC_LINE_PROCESS_DIAGNOSTIC_MODE="+mode) + output, err := command.CombinedOutput() + if ctx.Err() != nil || bytes.Contains(output, []byte("DATA RACE")) { + t.Fatalf("helper lifecycle: %v: %s", ctx.Err(), output) + } + if mode == "reply" { + if err != nil { + t.Fatalf("successful helper reply: %v: %s", err, output) + } + return + } + var failure *exec.ExitError + if !errors.As(err, &failure) || failure.ExitCode() != 1 || !bytes.Contains(output, []byte("helper response: EOF: ordinary helper diagnostic")) { + t.Fatalf("helper EOF diagnostic: %v: %s", err, output) + } + }) + } +} diff --git a/tests/interop/cmd/c14nprobe/main.go b/tests/interop/cmd/c14nprobe/main.go index c8c5962..6df97eb 100644 --- a/tests/interop/cmd/c14nprobe/main.go +++ b/tests/interop/cmd/c14nprobe/main.go @@ -14,6 +14,11 @@ import ( "github.com/skarm/xmlsec/document" ) +const ( + initialLineBufferBytes = 64 << 10 + maximumLineBytes = 32 << 20 +) + type request struct { Select *[]selector `json:"select"` XML string `json:"xml"` @@ -28,7 +33,11 @@ type request struct { type response struct { Canonical []byte `json:"canonical,omitempty"` Nodes []selector `json:"nodes,omitempty"` - Error string `json:"error,omitempty"` +} + +type reply struct { + response + Error string `json:"error,omitempty"` } type selector struct { @@ -41,21 +50,21 @@ type selector struct { func main() { scanner := bufio.NewScanner(os.Stdin) - scanner.Buffer(make([]byte, 64<<10), 32<<20) + scanner.Buffer(make([]byte, initialLineBufferBytes), maximumLineBytes) encoder := json.NewEncoder(os.Stdout) for scanner.Scan() { var ( r request - out response + out reply ) if err := json.Unmarshal(scanner.Bytes(), &r); err != nil { out.Error = err.Error() } else { var err error - out, err = process(r) + out.response, err = process(r) if err != nil { out.Error = err.Error() } diff --git a/tests/interop/cmd/c14nprobe/main_test.go b/tests/interop/cmd/c14nprobe/main_test.go index bf7eabf..17eeeff 100644 --- a/tests/interop/cmd/c14nprobe/main_test.go +++ b/tests/interop/cmd/c14nprobe/main_test.go @@ -33,15 +33,21 @@ func TestCanonicalRequestSelection(t *testing.T) { test.request.Algorithm = c14n.Exclusive result, err := process(test.request) if test.invalid { + // Failure assertions inspect the probe's diagnostic payload. + //noinspection GoDfaErrorMayBeNotNil if err == nil || len(result.Canonical) != 0 { t.Fatalf("invalid input produced data: %+v %v", result, err) } return } + if err != nil { + t.Fatal(err) + } + if len(result.Nodes) != 0 { t.Fatal("description was computed without being requested") } - if err != nil || string(result.Canonical) != test.want { + if string(result.Canonical) != test.want { t.Fatalf("canonical=%q error=%v want=%q", result.Canonical, err, test.want) } }) @@ -69,12 +75,14 @@ func TestSelectorAxesAndDescriptionOwnership(t *testing.T) { } { node, err := resolve(doc, test.selection) if test.invalid { - if err == nil || node.Valid() { + if err == nil || node != (document.Node{}) { t.Fatalf("invalid selector accepted: %+v %v", test.selection, err) } continue } - if err != nil || node.Value() != test.want { + if err != nil { + t.Fatal(err) + } else if node.Value() != test.want { t.Fatalf("selector %+v value=%q error=%v", test.selection, node.Value(), err) } } @@ -95,6 +103,8 @@ func TestSelectorAxesAndDescriptionOwnership(t *testing.T) { t.Fatalf("missing described axes: %+v", result.Nodes) } failed, err := process(request{XML: ``, Describe: true, Subtree: []int{2}, Algorithm: c14n.Exclusive}) + // Failure assertions inspect the probe's diagnostic payload. + //noinspection GoDfaErrorMayBeNotNil if err == nil || len(failed.Nodes) < 2 || len(failed.Canonical) != 0 { t.Fatalf("selection error lost its diagnostic description: %+v %v", failed, err) } diff --git a/tests/interop/cmd/evidenceprobe/main.go b/tests/interop/cmd/evidenceprobe/main.go index 760b6a9..d20b142 100644 --- a/tests/interop/cmd/evidenceprobe/main.go +++ b/tests/interop/cmd/evidenceprobe/main.go @@ -31,6 +31,14 @@ import ( "github.com/skarm/xmlsec/xmldsig" ) +const outputFileMode = 0o600 + +const ( + probeDefaultHashBits = 256 + probeDefaultTime = "2026-10-08T12:00:00Z" + probeDefaultTSAPolicy = "1.2.3.4.1" +) + func main() { mode := flag.String("mode", "", "operation") input := flag.String("in", "", "input") @@ -44,11 +52,11 @@ func main() { req := flag.String("request", "", "request") det := flag.Bool("detached", false, "detached CMS") pss := flag.Bool("pss", false, "RSA-PSS") - hash := flag.Int("hash", 256, "hash bits") - at := flag.String("at", "2026-10-08T12:00:00Z", "time") - status := flag.String("status", "good", "OCSP status") + hash := flag.Int("hash", probeDefaultHashBits, "hash bits") + at := flag.String("at", probeDefaultTime, "time") + status := flag.String("status", string(pki.OCSPGood), "OCSP status") nonceHex := flag.String("nonce", "", "nonce hex") - policyText := flag.String("policy", "1.2.3.4.1", "TSA policy") + policyText := flag.String("policy", probeDefaultTSAPolicy, "TSA policy") alg := flag.String("alg", c14n.Exclusive, "archive canonicalization") token := flag.String("token", "", "archive token CMS") @@ -136,6 +144,12 @@ func main() { must(err) method := xmldsig.ECDSASHA256 + + if key == nil { + must(errors.New("private key is required")) + return + } + if _, ok := key.Public().(*rsa.PublicKey); ok { method = xmldsig.RSASHA256 } @@ -216,7 +230,7 @@ func main() { must(e) if *out != "" { - must(os.WriteFile(*out, output, 0o600)) + must(os.WriteFile(*out, output, outputFileMode)) } fmt.Println("OK", *mode) diff --git a/tests/interop/cmd/keyinfoprobe/main.go b/tests/interop/cmd/keyinfoprobe/main.go index be1a137..3413916 100644 --- a/tests/interop/cmd/keyinfoprobe/main.go +++ b/tests/interop/cmd/keyinfoprobe/main.go @@ -36,7 +36,11 @@ type answer struct { Data []byte `json:"data,omitempty"` Keys [][]byte `json:"keys,omitempty"` Valid bool `json:"valid,omitempty"` - Error string `json:"error,omitempty"` +} + +type reply struct { + answer + Error string `json:"error,omitempty"` } func main() { @@ -49,7 +53,11 @@ func main() { return } - a, e := run(t) + a := reply{} + + var e error + + a.answer, e = run(t) if e != nil { a.Error = e.Error() } diff --git a/tests/interop/cmd/keyinfoprobe/main_test.go b/tests/interop/cmd/keyinfoprobe/main_test.go index 27dea93..cc7ec3d 100644 --- a/tests/interop/cmd/keyinfoprobe/main_test.go +++ b/tests/interop/cmd/keyinfoprobe/main_test.go @@ -22,7 +22,9 @@ func TestKeyInfoBuildParseAndPinnedVerification(t *testing.T) { } defer clear(private) built, err := run(task{Mode: "build", SPKI: spki, Format: string(keyinfo.DERKeyValue)}) - if err != nil || !bytes.Contains(built.Data, []byte(" exact key name ")) { + if err != nil { + t.Fatal(err) + } else if !bytes.Contains(built.Data, []byte(" exact key name ")) { t.Fatalf("build lost exact name: %q %v", built.Data, err) } parsed, err := run(task{Mode: "parse", Input: built.Data}) @@ -43,7 +45,9 @@ func TestKeyInfoBuildParseAndPinnedVerification(t *testing.T) { t.Fatalf("pinned verification: %+v %v", verified, err) } certificateInfo, err := run(task{Mode: "build", SPKI: spki, Certificate: chain.Leaf.Raw}) - if err != nil || !bytes.Contains(certificateInfo.Data, []byte("X509Certificate")) || bytes.Contains(certificateInfo.Data, []byte("DEREncodedKeyValue")) { + if err != nil { + t.Fatal(err) + } else if !bytes.Contains(certificateInfo.Data, []byte("X509Certificate")) || bytes.Contains(certificateInfo.Data, []byte("DEREncodedKeyValue")) { t.Fatalf("certificate projection lost its shape: %q %v", certificateInfo.Data, err) } certificateKeys, err := run(task{Mode: "parse", Input: certificateInfo.Data}) @@ -55,11 +59,15 @@ func TestKeyInfoBuildParseAndPinnedVerification(t *testing.T) { t.Fatal(err) } rejected, err := run(task{Mode: "verify", Input: signed.Data, SPKI: other}) + // Failure assertions inspect the probe's diagnostic payload. + //noinspection GoDfaErrorMayBeNotNil if err == nil || rejected.Valid { t.Fatal("untrusted KeyInfo candidate accepted") } for _, request := range []task{{Mode: "unknown"}, {Mode: "parse", Input: []byte(``), SPKI: spki}, {Mode: "build", SPKI: []byte("invalid")}} { answer, err := run(request) + // Failure assertions inspect the probe's diagnostic payload. + //noinspection GoDfaErrorMayBeNotNil if err == nil || answer.Valid || len(answer.Data) != 0 { t.Fatalf("invalid request leaked a success: %+v %v", request, err) } diff --git a/tests/interop/cmd/keyprobe/main.go b/tests/interop/cmd/keyprobe/main.go index 8136b01..5ae1cb8 100644 --- a/tests/interop/cmd/keyprobe/main.go +++ b/tests/interop/cmd/keyprobe/main.go @@ -17,6 +17,8 @@ import ( "github.com/skarm/xmlsec/keys" ) +const probeTimeout = 30 * time.Second + type task struct { Mode string Input string @@ -35,7 +37,11 @@ type output struct { Fingerprint string `json:"fingerprint,omitempty"` Certificate string `json:"certificate,omitempty"` Extras int `json:"extras,omitempty"` - Error string `json:"error,omitempty"` +} + +type reply struct { + output + Error string `json:"error,omitempty"` } func main() { @@ -44,15 +50,15 @@ func main() { d := json.NewDecoder(os.Stdin) d.DisallowUnknownFields() - o := output{} + o := reply{} if err := d.Decode(&t); err != nil { o.Error = err.Error() } else { - ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), probeTimeout) var err error - o, err = process(ctx, t) + o.output, err = process(ctx, t) cancel() diff --git a/tests/interop/cmd/keyprobe/main_test.go b/tests/interop/cmd/keyprobe/main_test.go index 188fa3d..f9f0d5b 100644 --- a/tests/interop/cmd/keyprobe/main_test.go +++ b/tests/interop/cmd/keyprobe/main_test.go @@ -51,7 +51,9 @@ func TestKeyFingerprintAndExportCertificateOrder(t *testing.T) { key := write("key.pem", pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: private})) password := []byte("unit fixture password") encrypted, err := process(t.Context(), task{Mode: "marshal-pkcs8", Key: key, Password: password}) - if err != nil || len(encrypted.Data) == 0 { + if err != nil { + t.Fatal(err) + } else if len(encrypted.Data) == 0 { t.Fatalf("encrypted key output: %q %v", encrypted.Data, err) } var envelope struct { @@ -73,11 +75,15 @@ func TestKeyFingerprintAndExportCertificateOrder(t *testing.T) { t.Fatalf("key import fingerprint: %+v %v", imported, err) } rejected, err := process(t.Context(), task{Mode: "parse-pkcs8", Input: protected, Password: []byte("wrong password")}) + // Failure assertions inspect the probe's diagnostic payload. + //noinspection GoDfaErrorMayBeNotNil if err == nil || rejected.Fingerprint != "" || len(rejected.Data) != 0 { t.Fatal("wrong password released key metadata") } for _, request := range []task{{Mode: "unknown", Key: key}, {Mode: "marshal-pkcs8", Key: key, Hash: 224, Password: password}, {Mode: "parse-pkcs8", Input: invalid}, {Mode: "parse-pkcs12", Input: invalid}} { output, err := process(t.Context(), request) + // Failure assertions inspect the probe's diagnostic payload. + //noinspection GoDfaErrorMayBeNotNil if err == nil || len(output.Data) != 0 || output.Fingerprint != "" { t.Fatalf("invalid key request produced success: %+v %v", request, err) } diff --git a/tests/interop/cmd/nextprobe/main.go b/tests/interop/cmd/nextprobe/main.go index 1810db3..db862f6 100644 --- a/tests/interop/cmd/nextprobe/main.go +++ b/tests/interop/cmd/nextprobe/main.go @@ -26,6 +26,10 @@ import ( "github.com/skarm/xmlsec/xmlenc" ) +const outputFileMode = 0o600 + +const probeTimeout = 30 * time.Second + type task struct { Mode string Key string @@ -55,7 +59,11 @@ type output struct { Data []byte `json:"data,omitempty"` Text string `json:"text,omitempty"` Report any `json:"report,omitempty"` - Error string `json:"error,omitempty"` +} + +type reply struct { + output + Error string `json:"error,omitempty"` } func main() { @@ -64,15 +72,15 @@ func main() { d := json.NewDecoder(os.Stdin) d.DisallowUnknownFields() - o := output{} + o := reply{} if e := d.Decode(&t); e != nil { o.Error = e.Error() } else { - ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), probeTimeout) var e error - o, e = process(ctx, t) + o.output, e = process(ctx, t) cancel() @@ -173,12 +181,22 @@ func process(ctx context.Context, t task) (o output, err error) { return o, e } - var doc *document.Document - if t.Mode != "redirect-verify" && t.Mode != "encrypt" && t.Mode != nameEncryptWrap && t.Mode != nameEncryptDirect && t.Mode != "pki-verify" { - doc, e = document.Parse(ctx, b, document.DefaultLimits()) - if e != nil { - return o, e - } + switch t.Mode { + case "encrypt": + return probeEncrypt(ctx, t, b, options) + case nameEncryptWrap: + return probeKeyWrap(ctx, t, b, nil, options) + case nameEncryptDirect: + return probeSymmetric(ctx, t, b, nil, options) + case "redirect-verify": + return probeRedirectVerify(ctx, t, b) + case "pki-verify": + return probePKIVerify(ctx, t, b) + } + + doc, e := document.Parse(ctx, b, document.DefaultLimits()) + if e != nil { + return o, e } switch t.Mode { @@ -186,24 +204,18 @@ func process(ctx context.Context, t task) (o output, err error) { return probeElement(ctx, t, doc, options) case "externalize": return probeExternalize(ctx, t, doc) - case nameEncryptDirect, "decrypt-direct": + case "decrypt-direct": return probeSymmetric(ctx, t, b, doc, options) case "xades-sign": return probeXAdESSign(ctx, t, doc) case "xades-verify": return probeXAdESVerify(ctx, t, doc) - case "encrypt": - return probeEncrypt(ctx, t, b, options) case "decrypt": return probeDecrypt(ctx, t, doc, options) - case nameEncryptWrap, "decrypt-wrap": + case "decrypt-wrap": return probeKeyWrap(ctx, t, b, doc, options) case "redirect-sign": return probeRedirectSign(ctx, t, doc) - case "redirect-verify": - return probeRedirectVerify(ctx, t, b) - case "pki-verify": - return probePKIVerify(ctx, t, b) default: return o, errors.New("unknown mode") } @@ -290,7 +302,7 @@ func probeExternalize(ctx context.Context, t task, doc *document.Document) (o ou return o, err } - if err := os.WriteFile(t.Resource, body, 0o600); err != nil { + if err := os.WriteFile(t.Resource, body, outputFileMode); err != nil { return o, err } diff --git a/tests/interop/cmd/nextprobe/main_test.go b/tests/interop/cmd/nextprobe/main_test.go index 4c4e1c9..3f7fc61 100644 --- a/tests/interop/cmd/nextprobe/main_test.go +++ b/tests/interop/cmd/nextprobe/main_test.go @@ -95,7 +95,9 @@ func TestSymmetricProbeRoutesAndRejectsWrongKey(t *testing.T) { for _, test := range []struct{ encrypt, decrypt string }{{nameEncryptDirect, "decrypt-direct"}, {nameEncryptWrap, "decrypt-wrap"}} { t.Run(test.encrypt, func(t *testing.T) { encrypted, err := process(t.Context(), task{Mode: test.encrypt, Input: input, Key: secret, KeyAlgorithm: xmlenc.AES256KeyWrap, DataAlgorithm: xmlenc.AES256GCM}) - if err != nil || len(encrypted.Data) == 0 || bytes.Contains(encrypted.Data, plaintext) || !bytes.Contains(encrypted.Data, []byte(xmlenc.AES256GCM)) { + if err != nil { + t.Fatal(err) + } else if len(encrypted.Data) == 0 || bytes.Contains(encrypted.Data, plaintext) || !bytes.Contains(encrypted.Data, []byte(xmlenc.AES256GCM)) { t.Fatalf("encryption route: %q %v", encrypted.Data, err) } wrapped := bytes.Contains(encrypted.Data, []byte(xmlenc.AES256KeyWrap)) @@ -104,11 +106,15 @@ func TestSymmetricProbeRoutesAndRejectsWrongKey(t *testing.T) { } encoded := write(test.encrypt+".xml", encrypted.Data) decrypted, err := process(t.Context(), task{Mode: test.decrypt, Input: encoded, Key: secret}) - if err != nil || !bytes.Equal(decrypted.Data, plaintext) { + if err != nil { + t.Fatal(err) + } else if !bytes.Equal(decrypted.Data, plaintext) { t.Fatalf("decryption route: %x %v", decrypted.Data, err) } wrong := write("wrong-key.bin", bytes.Repeat([]byte{0x71}, 32)) rejected, err := process(t.Context(), task{Mode: test.decrypt, Input: encoded, Key: wrong}) + // Failure assertions inspect the probe's diagnostic payload. + //noinspection GoDfaErrorMayBeNotNil if err == nil || len(rejected.Data) != 0 { t.Fatal("wrong key released plaintext") } diff --git a/tests/interop/cmd/parametersprobe/main.go b/tests/interop/cmd/parametersprobe/main.go index d7bc3e3..1cdb665 100644 --- a/tests/interop/cmd/parametersprobe/main.go +++ b/tests/interop/cmd/parametersprobe/main.go @@ -40,11 +40,15 @@ type task struct { type answer struct { Data []byte `json:"data,omitempty"` Valid bool `json:"valid"` - Error string `json:"error,omitempty"` Report *xmldsig.VerificationReport `json:"report,omitempty"` ProviderCalls int `json:"provider_calls,omitempty"` } +type reply struct { + answer + Error string `json:"error,omitempty"` +} + func main() { in := json.NewDecoder(os.Stdin) out := json.NewEncoder(os.Stdout) @@ -59,7 +63,11 @@ func main() { return } - a, err := run(t) + a := reply{} + + var err error + + a.answer, err = run(t) if err != nil { a.Error = err.Error() } diff --git a/tests/interop/cmd/parametersprobe/main_test.go b/tests/interop/cmd/parametersprobe/main_test.go index df629fb..0835129 100644 --- a/tests/interop/cmd/parametersprobe/main_test.go +++ b/tests/interop/cmd/parametersprobe/main_test.go @@ -84,6 +84,8 @@ func TestHMACSignVerifyAndTampering(t *testing.T) { } tampered := bytes.Replace(signed.Data, []byte("independent payload"), []byte("changed payload"), 1) verified, err = run(task{Mode: "verify", Method: xmldsig.HMACSHA256, Secret: secret, Input: tampered}) + // Failure assertions inspect the probe's diagnostic payload. + //noinspection GoDfaErrorMayBeNotNil if err == nil || verified.Valid { t.Fatal("tampered signed object accepted") } @@ -117,11 +119,15 @@ func TestProviderDefaultParametersDoNotPanic(t *testing.T) { } for _, invalid := range []task{{Mode: "provider-sign", Method: xmldsig.RSAPSS, Input: input, SPKI: spki, Hash: 224}, {Mode: "provider-sign", Method: xmldsig.RSAPSS, Input: input, SPKI: spki, Hash: 256, MGF: 1}} { result, err := run(invalid) + // Failure assertions inspect the probe's diagnostic payload. + //noinspection GoDfaErrorMayBeNotNil if !errors.Is(err, xmldsig.ErrAlgorithm) || result.Valid || result.ProviderCalls != 0 || len(result.Data) != 0 { t.Fatalf("invalid hash downgraded to default: %+v %v", result, err) } } answer, err := run(task{Mode: "provider-sign", Method: xmldsig.RSAPSS, DefaultParams: true, Input: input, SPKI: spki}) + // Failure assertions inspect the probe's diagnostic payload. + //noinspection GoDfaErrorMayBeNotNil if err == nil || answer.Valid || answer.ProviderCalls != 1 || !strings.Contains(err.Error(), "independent precomputed signature input differs") { t.Fatalf("default provider did not reach its input guard: %+v %v", answer, err) } diff --git a/tests/interop/cmd/samlprobe/main.go b/tests/interop/cmd/samlprobe/main.go index 83d68c3..df019f6 100644 --- a/tests/interop/cmd/samlprobe/main.go +++ b/tests/interop/cmd/samlprobe/main.go @@ -21,6 +21,8 @@ import ( "github.com/skarm/xmlsec/xmldsig" ) +const outputFileMode = 0o600 + func main() { mode := flag.String("mode", "", "mode") in := flag.String("in", "", "input") @@ -41,6 +43,7 @@ func main() { p, _ := pem.Decode(read(*keyPath)) if p == nil { must(errors.New("key")) + return } k, e := x509.ParsePKCS8PrivateKey(p.Bytes) @@ -73,7 +76,7 @@ func main() { must(err) } - must(os.WriteFile(*out, encoded, 0o600)) + must(os.WriteFile(*out, encoded, outputFileMode)) } fmt.Println("OK", *mode) diff --git a/tests/interop/crypto_test.go b/tests/interop/crypto_test.go index d7a55ba..b26334d 100644 --- a/tests/interop/crypto_test.go +++ b/tests/interop/crypto_test.go @@ -21,7 +21,7 @@ func TestEncryptionPrimitives(t *testing.T) { selected(t, "portable") dir := t.TempDir() - key := generateKey(t, dir, nameRsaSha256) + key := generateKey(t, dir, nameRSASHA256) gcm := filepath.Join(dir, "openssl-gcm") flags, _ := command(t, fixture, true, nil, "pkg-config", "--cflags", "--libs", "openssl") libraryFlags := strings.Fields(string(flags)) diff --git a/tests/interop/encryption_test.go b/tests/interop/encryption_test.go index aa29f77..28cb4ec 100644 --- a/tests/interop/encryption_test.go +++ b/tests/interop/encryption_test.go @@ -40,7 +40,7 @@ func TestXMLSecEncryption(t *testing.T) { selected(t, "xmlsec") dir := t.TempDir() - key := generateKey(t, dir, nameRsaSha256) + key := generateKey(t, dir, nameRSASHA256) suite := encryptionSuite{fixture: fixture, dir: dir, key: key} recipes := encryptionRecipes() @@ -60,7 +60,7 @@ func encryptionRecipes() []encryptionRecipe { for _, bits := range []int{128, 192, 256} { for _, wrap := range []int{0, 128, 192, 256} { - mode := nameRsa + mode := nameRSA if wrap > 0 { mode = nameWrap } @@ -75,7 +75,7 @@ func encryptionRecipes() []encryptionRecipe { for _, h := range []string{"256", "384", "512"} { for _, label := range [][]byte{nil, []byte("application\x00label\xff")} { - recipes = append(recipes, encryptionRecipe{mode: nameRsa, bits: 256, hash: h, label: label}) + recipes = append(recipes, encryptionRecipe{mode: nameRSA, bits: 256, hash: h, label: label}) } } @@ -86,12 +86,12 @@ func encryptionRecipes() []encryptionRecipe { } for _, label := range [][]byte{nil, []byte("application\x00label\xff")} { - recipes = append(recipes, encryptionRecipe{mode: nameRsa, bits: 256, hash: h, mgf: mgf, legacy: h == "1" || mgf == "1", label: label}) + recipes = append(recipes, encryptionRecipe{mode: nameRSA, bits: 256, hash: h, mgf: mgf, legacy: h == "1" || mgf == "1", label: label}) } } for _, label := range [][]byte{nil, []byte("application\x00label\xff")} { - recipes = append(recipes, encryptionRecipe{mode: nameRsa, bits: 256, hash: h, mgf: "1", algorithm: enc + "rsa-oaep-mgf1p", legacy: true, label: label}) + recipes = append(recipes, encryptionRecipe{mode: nameRSA, bits: 256, hash: h, mgf: "1", algorithm: enc + "rsa-oaep-mgf1p", legacy: true, label: label}) } } @@ -105,7 +105,7 @@ func TestXMLSecSelectedEncryption(t *testing.T) { selected(t, "xmlsec") dir := t.TempDir() - key := generateKey(t, dir, nameRsaSha256) + key := generateKey(t, dir, nameRSASHA256) cli := binary(t, fixture, "xmlsec") buffer := bufferOracle(t, fixture) element, content := selectedEncryptionForms() @@ -132,6 +132,502 @@ func TestXMLSecSelectedEncryption(t *testing.T) { } } +func TestXMLSecLegacyEncryption(t *testing.T) { + fixture := newInteropFixture(t) + selected(t, "xmlsec") + dir := t.TempDir() + key := generateKey(t, dir, nameRSASHA256) + suite := encryptionSuite{fixture: fixture, dir: dir, key: key} + for _, data := range []struct { + uri string + bits int + }{{enc + "aes128-cbc", 128}, {enc + "aes192-cbc", 192}, {enc + "aes256-cbc", 256}, {enc + "tripledes-cbc", 192}} { + routes := []encryptionRecipe{{mode: nameRSA, bits: data.bits, data: data.uri, dataLegacy: true}, {mode: nameRSA, bits: data.bits, data: data.uri, dataLegacy: true, algorithm: enc + "rsa-oaep-mgf1p", hash: "1", mgf: "1", legacy: true}, {mode: nameDirect, bits: data.bits, data: data.uri, dataLegacy: true}, {mode: nameWrap, bits: data.bits, wrap: 128, data: data.uri, dataLegacy: true}, {mode: nameWrap, bits: data.bits, wrap: 256, data: data.uri, dataLegacy: true}} + if strings.HasSuffix(data.uri, "tripledes-cbc") { + routes = append(routes, encryptionRecipe{mode: nameWrap, bits: 192, data: data.uri, dataLegacy: true, tripleWrap: true}) + } + for index, r := range routes { + t.Run(data.uri+"/"+strconv.Itoa(index), func(t *testing.T) { + scenario := suite.prepare(t, r, false) + if r.tripleWrap { + suite.exchangeTripleDESWrap(t, scenario) + } else { + suite.exchange(t, scenario) + } + if r.mode == nameDirect { + suite.checkZeroCBCPadding(t, scenario) + } + denied := copyTask(scenario.task) + denied["AllowLegacy"] = false + probe(t, fixture, "nextprobe", denied, false) + // Incomplete CBC blocks are rejected in both implementations. + // Pad-length-zero acceptance differs in xmlsec1; exact padding + // validation is covered separately by the normative native cases. + raw := read(t, scenario.theirs) + doc := xmlDoc(t, raw) + cv := legacyDataCipherValue(t, doc) + ciphertext := decode64(t, text(cv)) + ciphertext = ciphertext[:len(ciphertext)-1] + + bad := write(t, filepath.Join(dir, "bad-padding.xml"), bytes.Replace(raw, []byte(text(cv)), []byte(base64String(ciphertext)), 1)) + negative := copyTask(scenario.task) + negative[nameInput] = bad + probe(t, fixture, "nextprobe", negative, false) + os.Remove(scenario.output) + native(t, fixture, false, "decrypt", scenario.decryptArg, scenario.decryptKey, "--output", scenario.output, bad) + noOutput(t, scenario.output) + }) + } + } +} + +func TestXMLSecEncryptionMethodKeySize(t *testing.T) { + fixture := newInteropFixture(t) + selected(t, "xmlsec") + dir := t.TempDir() + key := generateKey(t, dir, nameRSASHA256) + suite := encryptionSuite{fixture: fixture, dir: dir, key: key} + for _, recipe := range []encryptionRecipe{{mode: nameDirect, bits: 256}, {mode: nameWrap, bits: 256, wrap: 128}, {mode: nameRSA, bits: 256}} { + t.Run(recipe.mode, func(t *testing.T) { + scenario := suite.prepare(t, recipe, false) + raw := probeData(t, fixture, "nextprobe", scenario.task) + method := `` + raw = bytes.Replace(raw, []byte(method), []byte(method+`256`), 1) + if recipe.mode != nameDirect { + algorithm, size := enc11+"rsa-oaep", "2048" + if recipe.mode == nameWrap { + algorithm, size = enc+"kw-aes128", "128" + } + method = `` + raw = bytes.Replace(raw, []byte(method), []byte(method+``+size+``), 1) + } + write(t, scenario.ours, raw) + scenario.task[nameMode] = "decrypt" + scenario.suffix + scenario.task[nameInput] = scenario.ours + scenario.task[nameKey] = scenario.decryptKey + comparePlain(t, fixture, probeData(t, fixture, "nextprobe", scenario.task), scenario.plain, false) + if recipe.mode == nameRSA { + // XMLENC3.2 permits KeySize; the explicitly supplied RSA key + // has a2048-bit modulus. This native key-search outcome is a + // diagnostic, rather than agreement for the RSA-size branch. + os.Remove(scenario.output) + native(t, fixture, false, "decrypt", scenario.decryptArg, scenario.decryptKey, "--output", scenario.output, scenario.ours) + noOutput(t, scenario.output) + withoutRSAKeySize := bytes.Replace(raw, []byte(`2048`), nil, 1) + write(t, scenario.ours, withoutRSAKeySize) + t.Log("DIAGNOSTIC xmlsec1 rejects RSA-OAEP KeySize2048 during key search; removing only this optional parameter succeeds. Excluded from RSA KeySize agreement.") + } + native(t, fixture, true, "decrypt", scenario.decryptArg, scenario.decryptKey, "--output", scenario.output, scenario.ours) + comparePlain(t, fixture, read(t, scenario.output), scenario.plain, false) + }) + } +} + +func legacyDataCipherValue(t *testing.T, doc *tree.Document) *tree.Node { + t.Helper() + for _, child := range tree.Root(doc).Children { + if child.Name.Space == enc && child.Name.Local == nameCipherData { + return one(t, child, enc, "CipherValue") + } + } + t.Fatal("missing data CipherValue") + return nil +} + +func TestXMLSecECDHAgreement(t *testing.T) { + fixture := newInteropFixture(t) + selected(t, "xmlsec") + for _, curve := range []struct { + name string + openssl string + curve ecdh.Curve + }{{"P256", "P-256", ecdh.P256()}, {"P384", "P-384", ecdh.P384()}, {"P521", "P-521", ecdh.P521()}} { + recipient, err := curve.curve.GenerateKey(rand.Reader) + if err != nil { + t.Fatal(err) + } + for _, data := range []struct { + name string + uri string + bits int + }{{"GCM128", xmlenc.AES128GCM, 128}, {"GCM192", xmlenc.AES192GCM, 192}, {"GCM256", xmlenc.AES256GCM, 256}} { + for _, wrap := range []struct { + name string + uri string + bytes int + }{{"KW128", xmlenc.AES128KeyWrap, 16}, {"KW192", xmlenc.AES192KeyWrap, 24}, {"KW256", xmlenc.AES256KeyWrap, 32}} { + for _, hash := range []struct { + name string + hash crypto.Hash + }{{"default", 0}, {"SHA256", crypto.SHA256}, {"SHA1", crypto.SHA1}} { + t.Run(curve.name+"/"+data.name+"/"+wrap.name+"/"+hash.name, func(t *testing.T) { + options := xmlenc.ECDHOptions{Data: xmlenc.Options{DataAlgorithm: data.uri}, KeyAlgorithm: wrap.uri, RecipientKeyName: "recipient"} + if hash.hash != 0 { + options.KDF = &xmlenc.ConcatKDFParameters{Hash: hash.hash, AlgorithmID: append([]byte{0}, []byte(wrap.uri)...), PartyUInfo: []byte{0, 'U'}, PartyVInfo: []byte{0, 'V'}, SuppPubInfo: []byte{0, 0, 0, 1, 0}, SuppPrivInfo: []byte{0, 0x42}, AllowLegacy: hash.hash == crypto.SHA1} + } + ecdhExchange(t, fixture, recipient, curve.openssl, data.bits, wrap.bytes, options) + }) + } + } + } + } +} + +func TestXMLSecECDHLegacyData(t *testing.T) { + fixture := newInteropFixture(t) + selected(t, "xmlsec") + recipient, err := ecdh.P256().GenerateKey(rand.Reader) + if err != nil { + t.Fatal(err) + } + for _, data := range []struct { + name string + uri string + bits int + }{{"CBC128", xmlenc.AES128CBC, 128}, {"CBC192", xmlenc.AES192CBC, 192}, {"CBC256", xmlenc.AES256CBC, 256}, {"TripleDES", xmlenc.TripleDESCBC, 192}} { + t.Run(data.name, func(t *testing.T) { + options := xmlenc.ECDHOptions{Data: xmlenc.Options{DataAlgorithm: data.uri, AllowLegacy: true}, KeyAlgorithm: xmlenc.AES256KeyWrap, RecipientKeyName: "recipient"} + if data.uri != xmlenc.TripleDESCBC { + ecdhExchange(t, fixture, recipient, "P-256", data.bits, 32, options) + return + } + private, _ := ecdhPEM(t, fixture, recipient) + plain := []byte("ECDH and legacy TripleDES\x00\xff") + doc, err := xmlenc.EncryptBytesWithECDH(t.Context(), plain, recipient.PublicKey(), options) + if err != nil { + t.Fatal(err) + } + raw, err := doc.XML(t.Context()) + if err != nil { + t.Fatal(err) + } + input := write(t, filepath.Join(fixture.work, "ecdh-3des.xml"), raw) + output := filepath.Join(fixture.work, "ecdh-3des-plain.bin") + native(t, fixture, true, "decrypt", "--enabled-key-data", ecdhKeyData+",des", "--privkey-pem:recipient", private, "--output", output, input) + if !bytes.Equal(read(t, output), plain) { + t.Fatal("xmlsec TripleDES decrypt differs") + } + t.Log("P5_ORACLE xmlsec1 ECDH/AES256KW/TripleDES decryption; reverse DES CEK parity independently covered by LegacyEncryption") + }) + } +} + +func TestXMLSecECDHLeadingZero(t *testing.T) { + fixture := newInteropFixture(t) + selected(t, "xmlsec") + scalar := make([]byte, 32) + scalar[31] = 1 + recipient, err := ecdh.P256().NewPrivateKey(scalar) + if err != nil { + t.Fatal(err) + } + scalar[30], scalar[31] = 1, 123 // 379G begins with the zero x-coordinate octet. + originator, err := ecdh.P256().NewPrivateKey(scalar) + if err != nil { + t.Fatal(err) + } + private, _ := ecdhPEM(t, fixture, recipient) + senderDER, err := x509.MarshalPKIXPublicKey(originator.PublicKey()) + if err != nil { + t.Fatal(err) + } + sender := write(t, filepath.Join(fixture.work, "zero-peer.pem"), pem.EncodeToMemory(&pem.Block{Type: "PUBLIC KEY", Bytes: senderDER})) + out := filepath.Join(fixture.work, "zero-secret.bin") + command(t, fixture, true, nil, "openssl", "pkeyutl", "-derive", "-inkey", private, "-peerkey", sender, "-out", out) + secret, err := recipient.ECDH(originator.PublicKey()) + if err != nil { + t.Fatal(err) + } + defer clear(secret) + if len(secret) != 32 || secret[0] != 0 || !bytes.Equal(read(t, out), secret) { + t.Fatal("leading-zero ECDH octet was lost") + } + for _, hash := range []crypto.Hash{crypto.SHA256, crypto.SHA1} { + t.Run(hash.String(), func(t *testing.T) { + parameters := xmlenc.ConcatKDFParameters{Hash: hash, AlgorithmID: []byte{0}, PartyUInfo: []byte{0}, PartyVInfo: []byte{0}, AllowLegacy: hash == crypto.SHA1} + ecdhOpenSSLKDF(t, fixture, secret, 32, parameters) + }) + } + t.Log("P5_ORACLE OpenSSL full-width leading-zero ECDH and multi-round ConcatKDF") +} + +// TestXMLSecEncryptionGrammar checks exact XMLENC field grammar against the +// published schemas, independently of cryptographic provider failure handling. +func TestXMLSecEncryptionGrammar(t *testing.T) { + fixture := newInteropFixture(t) + selected(t, "xmlsec") + schema := encryptionSchema(t, fixture) + dir := t.TempDir() + key := generateKey(t, dir, nameRSASHA256) + suite := encryptionSuite{fixture: fixture, dir: dir, key: key} + scenario := suite.prepare(t, encryptionRecipe{mode: nameRSA, bits: 256, label: []byte("schema-label")}, false) + raw := probeData(t, fixture, "nextprobe", scenario.task) + for name, c := range encryptionGrammarCases(t, raw) { + t.Run(name, func(t *testing.T) { + path := write(t, filepath.Join(t.TempDir(), "input.xml"), c.wire) + schemaValid := c.valid + if name == "CipherValue lexical base64" { + // XSD1.0 section3.2.16's B64 production excludes '!': + // https://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#base64Binary + // This libxml2 validator accepts it; the corresponding native + // rejection is enforced under the published lexical grammar. + schemaValid = true + t.Log("DIAGNOSTIC libxml2 schema validation accepts CipherValue '!'; excluded from Base64 lexical rejection agreement") + } + command(t, fixture, schemaValid, nil, "xmllint", "--nonet", "--noout", "--schema", schema, path) + check := copyTask(scenario.task) + check[nameMode], check[nameInput], check[nameKey] = "decrypt", path, scenario.decryptKey + if c.valid { + comparePlain(t, fixture, probeData(t, fixture, "nextprobe", check), scenario.plain, false) + native(t, fixture, true, "decrypt", scenario.decryptArg, scenario.decryptKey, "--output", scenario.output, path) + comparePlain(t, fixture, read(t, scenario.output), scenario.plain, false) + } else { + probe(t, fixture, "nextprobe", check, false) + } + }) + } +} + +// TestXMLSecKeyGraph exercises actual XML relationships, in addition to the +// primitive RFC vectors. Every oracle result is checked against plaintext. +func TestXMLSecKeyGraph(t *testing.T) { + fixture := newInteropFixture(t) + selected(t, "xmlsec") + plain := []byte("XMLENC key graph\x00\xff") + secret, kek := bytes.Repeat([]byte{0x52}, 32), bytes.Repeat([]byte{0x93}, 32) + wrap := func(raw, kek []byte, name, id, carried string) *document.Document { + t.Helper() + doc, err := xmlenc.EncryptKey(t.Context(), raw, xmlenc.KeyGraphKey{Name: name, Algorithm: xmlenc.AES256KeyWrap, Secret: kek}, xmlenc.EncryptedKeyOptions{Algorithm: xmlenc.AES256KeyWrap, ID: id, CarriedKeyName: carried}) + if err != nil { + t.Fatal(err) + } + return doc + } + wrapped := wrap(secret, kek, "master", "key", "session") + options := xmlenc.KeyGraphOptions{Keys: []xmlenc.KeyGraphKey{{Name: "master", Algorithm: xmlenc.AES256KeyWrap, Secret: kek}}} + for _, kind := range []string{"inline", "retrieval", "carried", "nested"} { + t.Run(kind, func(t *testing.T) { + data, err := xmlenc.EncryptBytesWithKey(t.Context(), plain, secret, xmlenc.SymmetricOptions{}) + if err != nil { + t.Fatal(err) + } + raw := xmlBytes(t, data) + info := string(xmlBytes(t, wrapped)) + sibling := "" + switch kind { + case "retrieval": + info = `` + sibling = string(xmlBytes(t, wrapped)) + case "carried": + info = `session` + sibling = string(xmlBytes(t, wrapped)) + case "nested": + intermediate := bytes.Repeat([]byte{0x24}, 32) + outer := wrap(intermediate, kek, "master", "outer", "") + inner := wrap(secret, intermediate, "intermediate", "inner", "") + info = strings.Replace(string(xmlBytes(t, inner)), `intermediate`, string(xmlBytes(t, outer)), 1) + } + wire := strings.Replace(string(raw), "", ""+info+"", 1) + if sibling != "" { + wire = "" + wire + sibling + "" + } + doc := xmlDoc(t, []byte(wire)) + target := doc.Root() + if sibling != "" { + target = target.Children()[0] + } + got, err := xmlenc.DecryptBytesWithKeyGraph(t.Context(), doc, target, options) + if err != nil || !bytes.Equal(got, plain) { + t.Fatal("native key relationship", err) + } + path := write(t, filepath.Join(t.TempDir(), "graph.xml"), []byte(wire)) + keyfile := write(t, filepath.Join(t.TempDir(), "kek.bin"), kek) + output := filepath.Join(t.TempDir(), "plaintext.bin") + if kind == "carried" { + native(t, fixture, false, "decrypt", "--id-attr:Id", enc+":EncryptedKey", "--aeskey:master", keyfile, path) + t.Log("DIAGNOSTIC xmlsec1 does not discover this sibling through CarriedKeyName alone; explicit RetrievalMethod control is compared separately") + controlled := strings.Replace(wire, `session`, ``, 1) + if controlled == wire { + t.Fatal("carried-name intervention did not change fixture") + } + controlledDoc := xmlDoc(t, []byte(controlled)) + controlledPlain, err := xmlenc.DecryptBytesWithKeyGraph(t.Context(), controlledDoc, controlledDoc.Root().Children()[0], options) + if err != nil || !bytes.Equal(controlledPlain, plain) { + t.Fatal("retrieval control", err) + } + path = write(t, filepath.Join(t.TempDir(), "carried-retrieval-control.xml"), []byte(controlled)) + } + native(t, fixture, true, "decrypt", "--id-attr:Id", enc+":EncryptedKey", "--aeskey:master", keyfile, "--output", output, path) + if !bytes.Equal(read(t, output), plain) { + t.Fatal("xmlsec graph plaintext differs") + } + if kind == "carried" { + t.Log("P5_ORACLE xmlsec1 carried-name explicit RetrievalMethod control") + } else { + t.Log("P5_ORACLE xmlsec1 AES key graph", kind) + } + }) + } +} + +func TestXMLSecECDHKeyGraph(t *testing.T) { + fixture := newInteropFixture(t) + selected(t, "xmlsec") + recipient, err := ecdh.P256().GenerateKey(rand.Reader) + if err != nil { + t.Fatal(err) + } + private, _ := ecdhPEM(t, fixture, recipient) + plain := []byte("ECDH key graph\x00\xff") + secret := bytes.Repeat([]byte{0x16}, 32) + wrapped, err := xmlenc.EncryptKey(t.Context(), secret, xmlenc.KeyGraphKey{Name: "recipient", Algorithm: xmlenc.AES256KeyWrap, ECDHPublicKey: recipient.PublicKey()}, xmlenc.EncryptedKeyOptions{Algorithm: xmlenc.AES256KeyWrap, ID: "outer"}) + if err != nil { + t.Fatal(err) + } + for _, kind := range []string{"inline", "retrieval", "nested"} { + t.Run(kind, func(t *testing.T) { + data, err := xmlenc.EncryptBytesWithKey(t.Context(), plain, secret, xmlenc.SymmetricOptions{}) + if err != nil { + t.Fatal(err) + } + keywire := string(xmlBytes(t, wrapped)) + outerwire := "" + algorithms := []string(nil) + if kind == "nested" { + middle := bytes.Repeat([]byte{0x42}, 16) + outer, err := xmlenc.EncryptKey(t.Context(), middle, xmlenc.KeyGraphKey{Name: "recipient", Algorithm: xmlenc.AES256KeyWrap, ECDHPublicKey: recipient.PublicKey()}, xmlenc.EncryptedKeyOptions{Algorithm: xmlenc.AES256KeyWrap, ID: "outer"}) + if err != nil { + t.Fatal(err) + } + inner, err := xmlenc.EncryptKey(t.Context(), secret, xmlenc.KeyGraphKey{Name: "middle", Algorithm: xmlenc.AES128KeyWrap, Secret: middle}, xmlenc.EncryptedKeyOptions{Algorithm: xmlenc.AES128KeyWrap, ID: "inner"}) + if err != nil { + t.Fatal(err) + } + innerwire := strings.Replace(string(xmlBytes(t, inner)), `middle`, ``, 1) + outerwire = string(xmlBytes(t, outer)) + keywire = innerwire + outerwire + algorithms = []string{xmlenc.AES128KeyWrap} + } + info := string(xmlBytes(t, wrapped)) + if kind == "retrieval" || kind == "nested" { + id := "outer" + if kind == "nested" { + id = "inner" + } + info = `` + } + datawire := strings.Replace(string(xmlBytes(t, data)), "", ``+info+``, 1) + wire := []byte(datawire) + if kind != "inline" { + wire = []byte("" + datawire + keywire + "") + } + doc := xmlDoc(t, wire) + target := doc.Root() + if kind != "inline" { + target = target.Children()[0] + } + opts := xmlenc.KeyGraphOptions{Keys: []xmlenc.KeyGraphKey{{Name: "recipient", Algorithm: xmlenc.AES256KeyWrap, ECDHRecipient: recipient}}, KeyAlgorithms: algorithms} + got, err := xmlenc.DecryptBytesWithKeyGraph(t.Context(), doc, target, opts) + if err != nil || !bytes.Equal(got, plain) { + t.Fatal("ECDH graph", err) + } + path := write(t, filepath.Join(t.TempDir(), "ecdh-graph.xml"), wire) + output := filepath.Join(t.TempDir(), "plain.bin") + if kind == "nested" { + native(t, fixture, false, "decrypt", "--id-attr:Id", enc+":EncryptedKey", "--enabled-key-data", ecdhKeyData+",retrieval-method", "--privkey-pem:recipient", private, path) + t.Log("DIAGNOSTIC xmlsec1 refuses this two-hop RetrievalMethod chain; one-hop control with the same outer ECDH key inline is compared separately") + controlled := strings.Replace(string(wire), ``, outerwire, 1) + // Remove the detached copy after moving that exact key inline, preserving unique IDs. + last := strings.LastIndex(controlled, outerwire) + if last < 0 { + t.Fatal("missing detached outer key") + } + controlled = controlled[:last] + controlled[last+len(outerwire):] + controlledDoc := xmlDoc(t, []byte(controlled)) + controlledPlain, err := xmlenc.DecryptBytesWithKeyGraph(t.Context(), controlledDoc, controlledDoc.Root().Children()[0], opts) + if err != nil || !bytes.Equal(controlledPlain, plain) { + t.Fatal("single-retrieval ECDH control", err) + } + path = write(t, filepath.Join(t.TempDir(), "single-hop-control.xml"), []byte(controlled)) + } + native(t, fixture, true, "decrypt", "--id-attr:Id", enc+":EncryptedKey", "--enabled-key-data", ecdhKeyData+",retrieval-method", "--privkey-pem:recipient", private, "--output", output, path) + if !bytes.Equal(read(t, output), plain) { + t.Fatal("xmlsec ECDH graph plaintext differs") + } + if kind == "nested" { + t.Log("P5_ORACLE xmlsec1 mixed-size ECDH nested key graph one-hop control") + } else { + t.Log("P5_ORACLE xmlsec1 explicit ECDH standalone-key graph", kind) + } + }) + } +} + +func TestXMLSecKeyGraphSchemas(t *testing.T) { + fixture := newInteropFixture(t) + schema := encryptionSchema(t, fixture) + secret, kek := bytes.Repeat([]byte{0x52}, 32), bytes.Repeat([]byte{0x93}, 32) + wrapped, err := xmlenc.EncryptKey(t.Context(), secret, xmlenc.KeyGraphKey{Name: "master", Algorithm: xmlenc.AES256KeyWrap, Secret: kek}, xmlenc.EncryptedKeyOptions{Algorithm: xmlenc.AES256KeyWrap, ID: "key", CarriedKeyName: " session "}) + if err != nil { + t.Fatal(err) + } + path := write(t, filepath.Join(t.TempDir(), "encrypted-key.xml"), xmlBytes(t, wrapped)) + command(t, fixture, true, nil, "xmllint", "--nonet", "--noout", "--schema", schema, path) + derived, err := xmlenc.BuildDerivedKey(t.Context(), xmlenc.DerivedKeyOptions{Parameters: xmlenc.ConcatKDFParameters{AlgorithmID: []byte{0, 1}, PartyUInfo: []byte{3, 0xd8}}, ID: "derived", MasterKeyName: "master", DerivedKeyName: "derived key", References: []xmlenc.KeyGraphReference{{URI: "#data"}, {URI: "#key", Key: true}}}) + if err != nil { + t.Fatal(err) + } + path = write(t, filepath.Join(t.TempDir(), "derived-key.xml"), xmlBytes(t, derived)) + command(t, fixture, true, nil, "xmllint", "--nonet", "--noout", "--schema", schema, path) + for _, body := range []struct { + name string + content string + valid bool + }{{"foreign element", `hint`, true}, {"empty", "", false}, {"text only", "hint", false}, {"same namespace", ``, false}, {"no namespace", ``, false}} { + t.Run(body.name, func(t *testing.T) { + wire := strings.Replace(string(xmlBytes(t, wrapped)), "", ``+body.content+``, 1) + if !strings.Contains(wire, "EncryptionProperties") { + t.Fatal("property intervention did not change fixture") + } + path := write(t, filepath.Join(t.TempDir(), "properties.xml"), []byte(wire)) + command(t, fixture, body.valid, nil, "xmllint", "--nonet", "--noout", "--schema", schema, path) + }) + } + t.Log("P5_ORACLE pinned published XMLENC schemas: standalone EncryptedKey, DerivedKey, and property cardinality/namespace") +} + +func TestXMLSecImplicitCipherCanonicalization(t *testing.T) { + fixture := newInteropFixture(t) + selected(t, "xmlsec") + key := bytes.Repeat([]byte{0x0e}, 16) + keyfile := write(t, filepath.Join(t.TempDir(), "aes128.key"), key) + want, err := hex.DecodeString("cea85e37d6cbce0572990f541088335ab8d6e8d01f385d6d19ad35c8d0fa0a") + if err != nil { + t.Fatal(err) + } + for name, node := range map[string]string{ + "canonical input": `AAAAAAAAAAAAAAAAAA`, + "reordered and commented input": `AAAAAAAAAAAAAAAAAA`, + } { + t.Run(name, func(t *testing.T) { + wire := `` + node + `` + doc, err := document.Parse(t.Context(), []byte(wire), document.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + options := xmlenc.KeyGraphOptions{Data: xmlenc.Options{DataAlgorithm: xmlenc.AES128CBC, AllowLegacy: true}, Keys: []xmlenc.KeyGraphKey{{Algorithm: xmlenc.AES128CBC, Secret: key}}} + out, err := xmlenc.DecryptBytesWithKeyGraph(t.Context(), doc, doc.Root().Children()[1], options) + if err != nil || !bytes.Equal(out, want) { + t.Fatal(out, err) + } + input := write(t, filepath.Join(t.TempDir(), "cipher.xml"), []byte(wire)) + output := filepath.Join(t.TempDir(), "plaintext") + native(t, fixture, true, "decrypt", "--aeskey", keyfile, "--output", output, input) + comparePlain(t, fixture, read(t, output), want, false) + }) + } +} + func selectedEncryptionForms() (map[string]string, map[string]string) { inherited := `visibleй 😀` rootInput := `й 😀` @@ -240,7 +736,7 @@ func (suite *encryptionSuite) prepare(t *testing.T, r encryptionRecipe, xml bool ours, theirs, output := filepath.Join(suite.dir, "go.xml"), filepath.Join(suite.dir, "ref.xml"), filepath.Join(suite.dir, "output") keyfile, decryptKey := suite.key.public, suite.key.private - if r.mode != nameRsa { + if r.mode != nameRSA { n := r.bits / 8 if r.mode == nameWrap { n = r.wrap / 8 @@ -259,7 +755,7 @@ func (suite *encryptionSuite) prepare(t *testing.T, r encryptionRecipe, xml bool } suffix := "" - if r.mode != nameRsa { + if r.mode != nameRSA { suffix = "-" + r.mode } @@ -287,7 +783,7 @@ func (suite *encryptionSuite) checkCase(t *testing.T, r encryptionRecipe, xml, l suite.exchange(t, scenario) suite.rejectTamper(t, scenario) - external := r.keyName == "" && (r.mode == nameRsa && r.hash == "384" && len(r.label) > 0 || r.mode == nameWrap && last || r.mode == nameDirect && r.bits == 256) + external := r.keyName == "" && (r.mode == nameRSA && r.hash == "384" && len(r.label) > 0 || r.mode == nameWrap && last || r.mode == nameDirect && r.bits == 256) if external { suite.checkCipherReferences(t, scenario) } @@ -596,13 +1092,13 @@ func (suite *selectedEncryptionSuite) prepare(t *testing.T, forms map[string]str source := write(t, filepath.Join(suite.dir, "source.xml"), []byte(forms[r.form])) if r.cm != "" { - signature := signatureTemplate(0, more+nameRsaSha256, r.cm, referenceTemplate("#secret", ds+"enveloped-signature", r.cm), "") + signature := signatureTemplate(0, more+nameRSASHA256, r.cm, referenceTemplate("#secret", ds+"enveloped-signature", r.cm), "") write(t, source, bytes.Replace(read(t, source), []byte(""), []byte(signature+""), 1)) command(t, suite.fixture, true, nil, suite.cli, "sign-template", flagKey, suite.key.private, "-signature-index", "0", "-c14n", r.cm, flagIn, source, flagOut, source) } original := read(t, source) - mode, keyfile, decryptKey, suffix := nameRsa, suite.key.public, suite.key.private, "" + mode, keyfile, decryptKey, suffix := nameRSA, suite.key.public, suite.key.private, "" algorithm := enc11 + "rsa-oaep" if r.wrap > 0 { @@ -627,7 +1123,7 @@ func (suite *selectedEncryptionSuite) decryptOurs(t *testing.T, scenario *select t.Helper() if scenario.empty { - bufferKind := nameRsa + bufferKind := nameRSA if scenario.recipe.wrap > 0 { bufferKind = "aes" } @@ -668,197 +1164,56 @@ func (suite *selectedEncryptionSuite) encryptTheirs(t *testing.T, scenario *sele os.Remove(scenario.theirs) args := []string{scenario.encryptArg, scenario.keyfile, "--session-key", fmt.Sprintf("aes-%d", scenario.recipe.bits), "--xml-data", scenario.source, "--node-xpath", "//*[local-name()='secret']", "--output", scenario.theirs, pattern} - native(t, suite.fixture, !scenario.empty, "encrypt", args...) - - if scenario.empty { - noOutput(t, scenario.theirs) - } else { - scenario.task[nameMode] = "decrypt-" + suite.kind + scenario.suffix - scenario.task[nameInput] = scenario.theirs - scenario.task["Selection"] = nameEncryptedData - scenario.task[nameKey] = scenario.decryptKey - restored := probeData(t, suite.fixture, "nextprobe", scenario.task) - equalXML(t, suite.fixture, scenario.original, restored) - - if scenario.recipe.cm != "" { - write(t, scenario.output, restored) - raw, _ := command(t, suite.fixture, true, nil, suite.cli, "verify-core", flagKey, suite.key.public, "-signature-index", "0", "-c14n", scenario.recipe.cm, flagIn, scenario.output) - verifyReport(t, raw, true) - native(t, suite.fixture, true, nameVerify, flagPubkeyPem, suite.key.public, flagIDAttrID, "urn:shadow:secret", flagEnabledReferenceUris, "same-doc", scenario.output) - } - - suite.count++ - } -} - -func (suite *selectedEncryptionSuite) rejectTamper(t *testing.T, scenario *selectedEncryptionCase) { - t.Helper() - - input := scenario.ours - if !scenario.empty { - input = scenario.theirs - } - - bad := write(t, filepath.Join(suite.dir, "bad.xml"), corruptCipher(t, read(t, input))) - - os.Remove(scenario.output) - native(t, suite.fixture, false, "decrypt", scenario.decryptArg, scenario.decryptKey, "--output", scenario.output, bad) - noOutput(t, scenario.output) - - scenario.task[nameMode] = "decrypt-" + suite.kind + scenario.suffix - scenario.task[nameInput] = bad - scenario.task["Selection"] = nameEncryptedData - scenario.task[nameKey] = scenario.decryptKey - probe(t, suite.fixture, "nextprobe", scenario.task, false) - - if scenario.empty { - bufferKind := nameRsa - if scenario.recipe.wrap > 0 { - bufferKind = "aes" - } - - command(t, suite.fixture, false, nil, suite.buffer, bad, bufferKind, scenario.decryptKey) - } -} - -func TestXMLSecLegacyEncryption(t *testing.T) { - fixture := newInteropFixture(t) - selected(t, "xmlsec") - dir := t.TempDir() - key := generateKey(t, dir, nameRsaSha256) - suite := encryptionSuite{fixture: fixture, dir: dir, key: key} - for _, data := range []struct { - uri string - bits int - }{{enc + "aes128-cbc", 128}, {enc + "aes192-cbc", 192}, {enc + "aes256-cbc", 256}, {enc + "tripledes-cbc", 192}} { - routes := []encryptionRecipe{{mode: nameRsa, bits: data.bits, data: data.uri, dataLegacy: true}, {mode: nameRsa, bits: data.bits, data: data.uri, dataLegacy: true, algorithm: enc + "rsa-oaep-mgf1p", hash: "1", mgf: "1", legacy: true}, {mode: nameDirect, bits: data.bits, data: data.uri, dataLegacy: true}, {mode: nameWrap, bits: data.bits, wrap: 128, data: data.uri, dataLegacy: true}, {mode: nameWrap, bits: data.bits, wrap: 256, data: data.uri, dataLegacy: true}} - if strings.HasSuffix(data.uri, "tripledes-cbc") { - routes = append(routes, encryptionRecipe{mode: nameWrap, bits: 192, data: data.uri, dataLegacy: true, tripleWrap: true}) - } - for index, r := range routes { - t.Run(data.uri+"/"+strconv.Itoa(index), func(t *testing.T) { - scenario := suite.prepare(t, r, false) - if r.tripleWrap { - suite.exchangeTripleDESWrap(t, scenario) - } else { - suite.exchange(t, scenario) - } - if r.mode == nameDirect { - suite.checkZeroCBCPadding(t, scenario) - } - denied := copyTask(scenario.task) - denied["AllowLegacy"] = false - probe(t, fixture, "nextprobe", denied, false) - // Incomplete CBC blocks are rejected in both implementations. - // Pad-length-zero acceptance differs in xmlsec1; exact padding - // validation is covered separately by the normative native cases. - raw := read(t, scenario.theirs) - doc := xmlDoc(t, raw) - cv := legacyDataCipherValue(t, doc) - ciphertext := decode64(t, text(cv)) - ciphertext = ciphertext[:len(ciphertext)-1] + native(t, suite.fixture, !scenario.empty, "encrypt", args...) - bad := write(t, filepath.Join(dir, "bad-padding.xml"), bytes.Replace(raw, []byte(text(cv)), []byte(base64String(ciphertext)), 1)) - negative := copyTask(scenario.task) - negative[nameInput] = bad - probe(t, fixture, "nextprobe", negative, false) - os.Remove(scenario.output) - native(t, fixture, false, "decrypt", scenario.decryptArg, scenario.decryptKey, "--output", scenario.output, bad) - noOutput(t, scenario.output) - }) + if scenario.empty { + noOutput(t, scenario.theirs) + } else { + scenario.task[nameMode] = "decrypt-" + suite.kind + scenario.suffix + scenario.task[nameInput] = scenario.theirs + scenario.task["Selection"] = nameEncryptedData + scenario.task[nameKey] = scenario.decryptKey + restored := probeData(t, suite.fixture, "nextprobe", scenario.task) + equalXML(t, suite.fixture, scenario.original, restored) + + if scenario.recipe.cm != "" { + write(t, scenario.output, restored) + raw, _ := command(t, suite.fixture, true, nil, suite.cli, "verify-core", flagKey, suite.key.public, "-signature-index", "0", "-c14n", scenario.recipe.cm, flagIn, scenario.output) + verifyReport(t, raw, true) + native(t, suite.fixture, true, nameVerify, flagPublicKeyPEM, suite.key.public, flagIDAttrID, "urn:shadow:secret", flagEnabledReferenceURIs, "same-doc", scenario.output) } - } -} -func TestXMLSecEncryptionMethodKeySize(t *testing.T) { - fixture := newInteropFixture(t) - selected(t, "xmlsec") - dir := t.TempDir() - key := generateKey(t, dir, nameRsaSha256) - suite := encryptionSuite{fixture: fixture, dir: dir, key: key} - for _, recipe := range []encryptionRecipe{{mode: nameDirect, bits: 256}, {mode: nameWrap, bits: 256, wrap: 128}, {mode: nameRsa, bits: 256}} { - t.Run(recipe.mode, func(t *testing.T) { - scenario := suite.prepare(t, recipe, false) - raw := probeData(t, fixture, "nextprobe", scenario.task) - method := `` - raw = bytes.Replace(raw, []byte(method), []byte(method+`256`), 1) - if recipe.mode != nameDirect { - algorithm, size := enc11+"rsa-oaep", "2048" - if recipe.mode == nameWrap { - algorithm, size = enc+"kw-aes128", "128" - } - method = `` - raw = bytes.Replace(raw, []byte(method), []byte(method+``+size+``), 1) - } - write(t, scenario.ours, raw) - scenario.task[nameMode] = "decrypt" + scenario.suffix - scenario.task[nameInput] = scenario.ours - scenario.task[nameKey] = scenario.decryptKey - comparePlain(t, fixture, probeData(t, fixture, "nextprobe", scenario.task), scenario.plain, false) - if recipe.mode == nameRsa { - // XMLENC3.2 permits KeySize; the explicitly supplied RSA key - // has a2048-bit modulus. This native key-search outcome is a - // diagnostic, rather than agreement for the RSA-size branch. - os.Remove(scenario.output) - native(t, fixture, false, "decrypt", scenario.decryptArg, scenario.decryptKey, "--output", scenario.output, scenario.ours) - noOutput(t, scenario.output) - withoutRSAKeySize := bytes.Replace(raw, []byte(`2048`), nil, 1) - write(t, scenario.ours, withoutRSAKeySize) - t.Log("DIAGNOSTIC xmlsec1 rejects RSA-OAEP KeySize2048 during key search; removing only this optional parameter succeeds. Excluded from RSA KeySize agreement.") - } - native(t, fixture, true, "decrypt", scenario.decryptArg, scenario.decryptKey, "--output", scenario.output, scenario.ours) - comparePlain(t, fixture, read(t, scenario.output), scenario.plain, false) - }) + suite.count++ } } -func legacyDataCipherValue(t *testing.T, doc *tree.Document) *tree.Node { +func (suite *selectedEncryptionSuite) rejectTamper(t *testing.T, scenario *selectedEncryptionCase) { t.Helper() - for _, child := range tree.Root(doc).Children { - if child.Name.Space == enc && child.Name.Local == nameCipherData { - return one(t, child, enc, "CipherValue") - } + + input := scenario.ours + if !scenario.empty { + input = scenario.theirs } - t.Fatal("missing data CipherValue") - return nil -} -func TestXMLSecECDHAgreement(t *testing.T) { - fixture := newInteropFixture(t) - selected(t, "xmlsec") - for _, curve := range []struct { - name string - openssl string - curve ecdh.Curve - }{{"P256", "P-256", ecdh.P256()}, {"P384", "P-384", ecdh.P384()}, {"P521", "P-521", ecdh.P521()}} { - recipient, err := curve.curve.GenerateKey(rand.Reader) - if err != nil { - t.Fatal(err) - } - for _, data := range []struct { - name string - uri string - bits int - }{{"GCM128", xmlenc.AES128GCM, 128}, {"GCM192", xmlenc.AES192GCM, 192}, {"GCM256", xmlenc.AES256GCM, 256}} { - for _, wrap := range []struct { - name string - uri string - bytes int - }{{"KW128", xmlenc.AES128KeyWrap, 16}, {"KW192", xmlenc.AES192KeyWrap, 24}, {"KW256", xmlenc.AES256KeyWrap, 32}} { - for _, hash := range []struct { - name string - hash crypto.Hash - }{{"default", 0}, {"SHA256", crypto.SHA256}, {"SHA1", crypto.SHA1}} { - t.Run(curve.name+"/"+data.name+"/"+wrap.name+"/"+hash.name, func(t *testing.T) { - options := xmlenc.ECDHOptions{Data: xmlenc.Options{DataAlgorithm: data.uri}, KeyAlgorithm: wrap.uri, RecipientKeyName: "recipient"} - if hash.hash != 0 { - options.KDF = &xmlenc.ConcatKDFParameters{Hash: hash.hash, AlgorithmID: append([]byte{0}, []byte(wrap.uri)...), PartyUInfo: []byte{0, 'U'}, PartyVInfo: []byte{0, 'V'}, SuppPubInfo: []byte{0, 0, 0, 1, 0}, SuppPrivInfo: []byte{0, 0x42}, AllowLegacy: hash.hash == crypto.SHA1} - } - ecdhExchange(t, fixture, recipient, curve.openssl, data.bits, wrap.bytes, options) - }) - } - } + bad := write(t, filepath.Join(suite.dir, "bad.xml"), corruptCipher(t, read(t, input))) + + os.Remove(scenario.output) + native(t, suite.fixture, false, "decrypt", scenario.decryptArg, scenario.decryptKey, "--output", scenario.output, bad) + noOutput(t, scenario.output) + + scenario.task[nameMode] = "decrypt-" + suite.kind + scenario.suffix + scenario.task[nameInput] = bad + scenario.task["Selection"] = nameEncryptedData + scenario.task[nameKey] = scenario.decryptKey + probe(t, suite.fixture, "nextprobe", scenario.task, false) + + if scenario.empty { + bufferKind := nameRSA + if scenario.recipe.wrap > 0 { + bufferKind = "aes" } + + command(t, suite.fixture, false, nil, suite.buffer, bad, bufferKind, scenario.decryptKey) } } @@ -963,137 +1318,22 @@ func ecdhOpenSSLKDF(t *testing.T, fixture *interopFixture, secret []byte, kekByt if field[0] != 0 { t.Fatal("whole-octet OpenSSL fixture required") } - other = append(other, field[1:]...) - } - } - digest := "SHA256" - if parameters.Hash == crypto.SHA1 { - digest = "SHA1" - } - out := filepath.Join(fixture.work, "ecdh-kdf.bin") - command(t, fixture, true, nil, "openssl", "kdf", "-binary", "-out", out, "-keylen", strconv.Itoa(kekBytes), "-kdfopt", "digest:"+digest, "-kdfopt", "hexsecret:"+hex.EncodeToString(secret), "-kdfopt", "hexinfo:"+hex.EncodeToString(other), "SSKDF") - ours, err := xmlenc.DeriveConcatKey(t.Context(), secret, kekBytes*8, parameters) - if err != nil { - t.Fatal(err) - } - defer clear(ours) - if !bytes.Equal(ours, read(t, out)) { - t.Fatal("OpenSSL ConcatKDF differs") - } -} - -func TestXMLSecECDHLegacyData(t *testing.T) { - fixture := newInteropFixture(t) - selected(t, "xmlsec") - recipient, err := ecdh.P256().GenerateKey(rand.Reader) - if err != nil { - t.Fatal(err) - } - for _, data := range []struct { - name string - uri string - bits int - }{{"CBC128", xmlenc.AES128CBC, 128}, {"CBC192", xmlenc.AES192CBC, 192}, {"CBC256", xmlenc.AES256CBC, 256}, {"TripleDES", xmlenc.TripleDESCBC, 192}} { - t.Run(data.name, func(t *testing.T) { - options := xmlenc.ECDHOptions{Data: xmlenc.Options{DataAlgorithm: data.uri, AllowLegacy: true}, KeyAlgorithm: xmlenc.AES256KeyWrap, RecipientKeyName: "recipient"} - if data.uri != xmlenc.TripleDESCBC { - ecdhExchange(t, fixture, recipient, "P-256", data.bits, 32, options) - return - } - private, _ := ecdhPEM(t, fixture, recipient) - plain := []byte("ECDH and legacy TripleDES\x00\xff") - doc, err := xmlenc.EncryptBytesWithECDH(t.Context(), plain, recipient.PublicKey(), options) - if err != nil { - t.Fatal(err) - } - raw, err := doc.XML(t.Context()) - if err != nil { - t.Fatal(err) - } - input := write(t, filepath.Join(fixture.work, "ecdh-3des.xml"), raw) - output := filepath.Join(fixture.work, "ecdh-3des-plain.bin") - native(t, fixture, true, "decrypt", "--enabled-key-data", ecdhKeyData+",des", "--privkey-pem:recipient", private, "--output", output, input) - if !bytes.Equal(read(t, output), plain) { - t.Fatal("xmlsec TripleDES decrypt differs") - } - t.Log("P5_ORACLE xmlsec1 ECDH/AES256KW/TripleDES decryption; reverse DES CEK parity independently covered by LegacyEncryption") - }) - } -} - -func TestXMLSecECDHLeadingZero(t *testing.T) { - fixture := newInteropFixture(t) - selected(t, "xmlsec") - scalar := make([]byte, 32) - scalar[31] = 1 - recipient, err := ecdh.P256().NewPrivateKey(scalar) - if err != nil { - t.Fatal(err) - } - scalar[30], scalar[31] = 1, 123 // 379G begins with the zero x-coordinate octet. - originator, err := ecdh.P256().NewPrivateKey(scalar) - if err != nil { - t.Fatal(err) - } - private, _ := ecdhPEM(t, fixture, recipient) - senderDER, err := x509.MarshalPKIXPublicKey(originator.PublicKey()) - if err != nil { - t.Fatal(err) - } - sender := write(t, filepath.Join(fixture.work, "zero-peer.pem"), pem.EncodeToMemory(&pem.Block{Type: "PUBLIC KEY", Bytes: senderDER})) - out := filepath.Join(fixture.work, "zero-secret.bin") - command(t, fixture, true, nil, "openssl", "pkeyutl", "-derive", "-inkey", private, "-peerkey", sender, "-out", out) - secret, err := recipient.ECDH(originator.PublicKey()) - if err != nil { - t.Fatal(err) - } - defer clear(secret) - if len(secret) != 32 || secret[0] != 0 || !bytes.Equal(read(t, out), secret) { - t.Fatal("leading-zero ECDH octet was lost") - } - for _, hash := range []crypto.Hash{crypto.SHA256, crypto.SHA1} { - t.Run(hash.String(), func(t *testing.T) { - parameters := xmlenc.ConcatKDFParameters{Hash: hash, AlgorithmID: []byte{0}, PartyUInfo: []byte{0}, PartyVInfo: []byte{0}, AllowLegacy: hash == crypto.SHA1} - ecdhOpenSSLKDF(t, fixture, secret, 32, parameters) - }) - } - t.Log("P5_ORACLE OpenSSL full-width leading-zero ECDH and multi-round ConcatKDF") -} - -// TestXMLSecEncryptionGrammar checks exact XMLENC field grammar against the -// published schemas, independently of cryptographic provider failure handling. -func TestXMLSecEncryptionGrammar(t *testing.T) { - fixture := newInteropFixture(t) - selected(t, "xmlsec") - schema := encryptionSchema(t, fixture) - dir := t.TempDir() - key := generateKey(t, dir, nameRsaSha256) - suite := encryptionSuite{fixture: fixture, dir: dir, key: key} - scenario := suite.prepare(t, encryptionRecipe{mode: nameRsa, bits: 256, label: []byte("schema-label")}, false) - raw := probeData(t, fixture, "nextprobe", scenario.task) - for name, c := range encryptionGrammarCases(t, raw) { - t.Run(name, func(t *testing.T) { - path := write(t, filepath.Join(t.TempDir(), "input.xml"), c.wire) - schemaValid := c.valid - if name == "CipherValue lexical base64" { - // XSD1.0 section3.2.16's B64 production excludes '!': - // https://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#base64Binary - // This libxml2 validator accepts it; the corresponding native - // rejection is enforced under the published lexical grammar. - schemaValid = true - t.Log("DIAGNOSTIC libxml2 schema validation accepts CipherValue '!'; excluded from Base64 lexical rejection agreement") - } - command(t, fixture, schemaValid, nil, "xmllint", "--nonet", "--noout", "--schema", schema, path) - check := copyTask(scenario.task) - check[nameMode], check[nameInput], check[nameKey] = "decrypt", path, scenario.decryptKey - if c.valid { - comparePlain(t, fixture, probeData(t, fixture, "nextprobe", check), scenario.plain, false) - native(t, fixture, true, "decrypt", scenario.decryptArg, scenario.decryptKey, "--output", scenario.output, path) - comparePlain(t, fixture, read(t, scenario.output), scenario.plain, false) - } else { - probe(t, fixture, "nextprobe", check, false) - } - }) + other = append(other, field[1:]...) + } + } + digest := "SHA256" + if parameters.Hash == crypto.SHA1 { + digest = "SHA1" + } + out := filepath.Join(fixture.work, "ecdh-kdf.bin") + command(t, fixture, true, nil, "openssl", "kdf", "-binary", "-out", out, "-keylen", strconv.Itoa(kekBytes), "-kdfopt", "digest:"+digest, "-kdfopt", "hexsecret:"+hex.EncodeToString(secret), "-kdfopt", "hexinfo:"+hex.EncodeToString(other), "SSKDF") + ours, err := xmlenc.DeriveConcatKey(t.Context(), secret, kekBytes*8, parameters) + if err != nil { + t.Fatal(err) + } + defer clear(ours) + if !bytes.Equal(ours, read(t, out)) { + t.Fatal("OpenSSL ConcatKDF differs") } } @@ -1181,243 +1421,3 @@ func encryptionSchema(t *testing.T, fixture *interopFixture) string { entry := `` return write(t, filepath.Join(fixture.work, "xmlenc-proof-schema.xsd"), []byte(entry)) } - -// TestXMLSecKeyGraph exercises actual XML relationships, in addition to the -// primitive RFC vectors. Every oracle result is checked against plaintext. -func TestXMLSecKeyGraph(t *testing.T) { - fixture := newInteropFixture(t) - selected(t, "xmlsec") - plain := []byte("XMLENC key graph\x00\xff") - secret, kek := bytes.Repeat([]byte{0x52}, 32), bytes.Repeat([]byte{0x93}, 32) - wrap := func(raw, kek []byte, name, id, carried string) *document.Document { - t.Helper() - doc, err := xmlenc.EncryptKey(t.Context(), raw, xmlenc.KeyGraphKey{Name: name, Algorithm: xmlenc.AES256KeyWrap, Secret: kek}, xmlenc.EncryptedKeyOptions{Algorithm: xmlenc.AES256KeyWrap, ID: id, CarriedKeyName: carried}) - if err != nil { - t.Fatal(err) - } - return doc - } - wrapped := wrap(secret, kek, "master", "key", "session") - options := xmlenc.KeyGraphOptions{Keys: []xmlenc.KeyGraphKey{{Name: "master", Algorithm: xmlenc.AES256KeyWrap, Secret: kek}}} - for _, kind := range []string{"inline", "retrieval", "carried", "nested"} { - t.Run(kind, func(t *testing.T) { - data, err := xmlenc.EncryptBytesWithKey(t.Context(), plain, secret, xmlenc.SymmetricOptions{}) - if err != nil { - t.Fatal(err) - } - raw := xmlBytes(t, data) - info := string(xmlBytes(t, wrapped)) - sibling := "" - switch kind { - case "retrieval": - info = `` - sibling = string(xmlBytes(t, wrapped)) - case "carried": - info = `session` - sibling = string(xmlBytes(t, wrapped)) - case "nested": - intermediate := bytes.Repeat([]byte{0x24}, 32) - outer := wrap(intermediate, kek, "master", "outer", "") - inner := wrap(secret, intermediate, "intermediate", "inner", "") - info = strings.Replace(string(xmlBytes(t, inner)), `intermediate`, string(xmlBytes(t, outer)), 1) - } - wire := strings.Replace(string(raw), "", ""+info+"", 1) - if sibling != "" { - wire = "" + wire + sibling + "" - } - doc := xmlDoc(t, []byte(wire)) - target := doc.Root() - if sibling != "" { - target = target.Children()[0] - } - got, err := xmlenc.DecryptBytesWithKeyGraph(t.Context(), doc, target, options) - if err != nil || !bytes.Equal(got, plain) { - t.Fatal("native key relationship", err) - } - path := write(t, filepath.Join(t.TempDir(), "graph.xml"), []byte(wire)) - keyfile := write(t, filepath.Join(t.TempDir(), "kek.bin"), kek) - output := filepath.Join(t.TempDir(), "plaintext.bin") - if kind == "carried" { - native(t, fixture, false, "decrypt", "--id-attr:Id", enc+":EncryptedKey", "--aeskey:master", keyfile, path) - t.Log("DIAGNOSTIC xmlsec1 does not discover this sibling through CarriedKeyName alone; explicit RetrievalMethod control is compared separately") - controlled := strings.Replace(wire, `session`, ``, 1) - if controlled == wire { - t.Fatal("carried-name intervention did not change fixture") - } - controlledDoc := xmlDoc(t, []byte(controlled)) - controlledPlain, err := xmlenc.DecryptBytesWithKeyGraph(t.Context(), controlledDoc, controlledDoc.Root().Children()[0], options) - if err != nil || !bytes.Equal(controlledPlain, plain) { - t.Fatal("retrieval control", err) - } - path = write(t, filepath.Join(t.TempDir(), "carried-retrieval-control.xml"), []byte(controlled)) - } - native(t, fixture, true, "decrypt", "--id-attr:Id", enc+":EncryptedKey", "--aeskey:master", keyfile, "--output", output, path) - if !bytes.Equal(read(t, output), plain) { - t.Fatal("xmlsec graph plaintext differs") - } - if kind == "carried" { - t.Log("P5_ORACLE xmlsec1 carried-name explicit RetrievalMethod control") - } else { - t.Log("P5_ORACLE xmlsec1 AES key graph", kind) - } - }) - } -} - -func TestXMLSecECDHKeyGraph(t *testing.T) { - fixture := newInteropFixture(t) - selected(t, "xmlsec") - recipient, err := ecdh.P256().GenerateKey(rand.Reader) - if err != nil { - t.Fatal(err) - } - private, _ := ecdhPEM(t, fixture, recipient) - plain := []byte("ECDH key graph\x00\xff") - secret := bytes.Repeat([]byte{0x16}, 32) - wrapped, err := xmlenc.EncryptKey(t.Context(), secret, xmlenc.KeyGraphKey{Name: "recipient", Algorithm: xmlenc.AES256KeyWrap, ECDHPublicKey: recipient.PublicKey()}, xmlenc.EncryptedKeyOptions{Algorithm: xmlenc.AES256KeyWrap, ID: "outer"}) - if err != nil { - t.Fatal(err) - } - for _, kind := range []string{"inline", "retrieval", "nested"} { - t.Run(kind, func(t *testing.T) { - data, err := xmlenc.EncryptBytesWithKey(t.Context(), plain, secret, xmlenc.SymmetricOptions{}) - if err != nil { - t.Fatal(err) - } - keywire := string(xmlBytes(t, wrapped)) - outerwire := "" - algorithms := []string(nil) - if kind == "nested" { - middle := bytes.Repeat([]byte{0x42}, 16) - outer, err := xmlenc.EncryptKey(t.Context(), middle, xmlenc.KeyGraphKey{Name: "recipient", Algorithm: xmlenc.AES256KeyWrap, ECDHPublicKey: recipient.PublicKey()}, xmlenc.EncryptedKeyOptions{Algorithm: xmlenc.AES256KeyWrap, ID: "outer"}) - if err != nil { - t.Fatal(err) - } - inner, err := xmlenc.EncryptKey(t.Context(), secret, xmlenc.KeyGraphKey{Name: "middle", Algorithm: xmlenc.AES128KeyWrap, Secret: middle}, xmlenc.EncryptedKeyOptions{Algorithm: xmlenc.AES128KeyWrap, ID: "inner"}) - if err != nil { - t.Fatal(err) - } - innerwire := strings.Replace(string(xmlBytes(t, inner)), `middle`, ``, 1) - outerwire = string(xmlBytes(t, outer)) - keywire = innerwire + outerwire - algorithms = []string{xmlenc.AES128KeyWrap} - } - info := string(xmlBytes(t, wrapped)) - if kind == "retrieval" || kind == "nested" { - id := "outer" - if kind == "nested" { - id = "inner" - } - info = `` - } - datawire := strings.Replace(string(xmlBytes(t, data)), "", ``+info+``, 1) - wire := []byte(datawire) - if kind != "inline" { - wire = []byte("" + datawire + keywire + "") - } - doc := xmlDoc(t, wire) - target := doc.Root() - if kind != "inline" { - target = target.Children()[0] - } - opts := xmlenc.KeyGraphOptions{Keys: []xmlenc.KeyGraphKey{{Name: "recipient", Algorithm: xmlenc.AES256KeyWrap, ECDHRecipient: recipient}}, KeyAlgorithms: algorithms} - got, err := xmlenc.DecryptBytesWithKeyGraph(t.Context(), doc, target, opts) - if err != nil || !bytes.Equal(got, plain) { - t.Fatal("ECDH graph", err) - } - path := write(t, filepath.Join(t.TempDir(), "ecdh-graph.xml"), wire) - output := filepath.Join(t.TempDir(), "plain.bin") - if kind == "nested" { - native(t, fixture, false, "decrypt", "--id-attr:Id", enc+":EncryptedKey", "--enabled-key-data", ecdhKeyData+",retrieval-method", "--privkey-pem:recipient", private, path) - t.Log("DIAGNOSTIC xmlsec1 refuses this two-hop RetrievalMethod chain; one-hop control with the same outer ECDH key inline is compared separately") - controlled := strings.Replace(string(wire), ``, outerwire, 1) - // Remove the detached copy after moving that exact key inline, preserving unique IDs. - last := strings.LastIndex(controlled, outerwire) - if last < 0 { - t.Fatal("missing detached outer key") - } - controlled = controlled[:last] + controlled[last+len(outerwire):] - controlledDoc := xmlDoc(t, []byte(controlled)) - controlledPlain, err := xmlenc.DecryptBytesWithKeyGraph(t.Context(), controlledDoc, controlledDoc.Root().Children()[0], opts) - if err != nil || !bytes.Equal(controlledPlain, plain) { - t.Fatal("single-retrieval ECDH control", err) - } - path = write(t, filepath.Join(t.TempDir(), "single-hop-control.xml"), []byte(controlled)) - } - native(t, fixture, true, "decrypt", "--id-attr:Id", enc+":EncryptedKey", "--enabled-key-data", ecdhKeyData+",retrieval-method", "--privkey-pem:recipient", private, "--output", output, path) - if !bytes.Equal(read(t, output), plain) { - t.Fatal("xmlsec ECDH graph plaintext differs") - } - if kind == "nested" { - t.Log("P5_ORACLE xmlsec1 mixed-size ECDH nested key graph one-hop control") - } else { - t.Log("P5_ORACLE xmlsec1 explicit ECDH standalone-key graph", kind) - } - }) - } -} - -func TestXMLSecKeyGraphSchemas(t *testing.T) { - fixture := newInteropFixture(t) - schema := encryptionSchema(t, fixture) - secret, kek := bytes.Repeat([]byte{0x52}, 32), bytes.Repeat([]byte{0x93}, 32) - wrapped, err := xmlenc.EncryptKey(t.Context(), secret, xmlenc.KeyGraphKey{Name: "master", Algorithm: xmlenc.AES256KeyWrap, Secret: kek}, xmlenc.EncryptedKeyOptions{Algorithm: xmlenc.AES256KeyWrap, ID: "key", CarriedKeyName: " session "}) - if err != nil { - t.Fatal(err) - } - path := write(t, filepath.Join(t.TempDir(), "encrypted-key.xml"), xmlBytes(t, wrapped)) - command(t, fixture, true, nil, "xmllint", "--nonet", "--noout", "--schema", schema, path) - derived, err := xmlenc.BuildDerivedKey(t.Context(), xmlenc.DerivedKeyOptions{Parameters: xmlenc.ConcatKDFParameters{AlgorithmID: []byte{0, 1}, PartyUInfo: []byte{3, 0xd8}}, ID: "derived", MasterKeyName: "master", DerivedKeyName: "derived key", References: []xmlenc.KeyGraphReference{{URI: "#data"}, {URI: "#key", Key: true}}}) - if err != nil { - t.Fatal(err) - } - path = write(t, filepath.Join(t.TempDir(), "derived-key.xml"), xmlBytes(t, derived)) - command(t, fixture, true, nil, "xmllint", "--nonet", "--noout", "--schema", schema, path) - for _, body := range []struct { - name string - content string - valid bool - }{{"foreign element", `hint`, true}, {"empty", "", false}, {"text only", "hint", false}, {"same namespace", ``, false}, {"no namespace", ``, false}} { - t.Run(body.name, func(t *testing.T) { - wire := strings.Replace(string(xmlBytes(t, wrapped)), "", ``+body.content+``, 1) - if !strings.Contains(wire, "EncryptionProperties") { - t.Fatal("property intervention did not change fixture") - } - path := write(t, filepath.Join(t.TempDir(), "properties.xml"), []byte(wire)) - command(t, fixture, body.valid, nil, "xmllint", "--nonet", "--noout", "--schema", schema, path) - }) - } - t.Log("P5_ORACLE pinned published XMLENC schemas: standalone EncryptedKey, DerivedKey, and property cardinality/namespace") -} - -func TestXMLSecImplicitCipherCanonicalization(t *testing.T) { - fixture := newInteropFixture(t) - selected(t, "xmlsec") - key := bytes.Repeat([]byte{0x0e}, 16) - keyfile := write(t, filepath.Join(t.TempDir(), "aes128.key"), key) - want, err := hex.DecodeString("cea85e37d6cbce0572990f541088335ab8d6e8d01f385d6d19ad35c8d0fa0a") - if err != nil { - t.Fatal(err) - } - for name, node := range map[string]string{ - "canonical input": `AAAAAAAAAAAAAAAAAA`, - "reordered and commented input": `AAAAAAAAAAAAAAAAAA`, - } { - t.Run(name, func(t *testing.T) { - wire := `` + node + `` - doc, err := document.Parse(t.Context(), []byte(wire), document.DefaultLimits()) - if err != nil { - t.Fatal(err) - } - options := xmlenc.KeyGraphOptions{Data: xmlenc.Options{DataAlgorithm: xmlenc.AES128CBC, AllowLegacy: true}, Keys: []xmlenc.KeyGraphKey{{Algorithm: xmlenc.AES128CBC, Secret: key}}} - out, err := xmlenc.DecryptBytesWithKeyGraph(t.Context(), doc, doc.Root().Children()[1], options) - if err != nil || !bytes.Equal(out, want) { - t.Fatal(out, err) - } - input := write(t, filepath.Join(t.TempDir(), "cipher.xml"), []byte(wire)) - output := filepath.Join(t.TempDir(), "plaintext") - native(t, fixture, true, "decrypt", "--aeskey", keyfile, "--output", output, input) - comparePlain(t, fixture, read(t, output), want, false) - }) - } -} diff --git a/tests/interop/evidence_test.go b/tests/interop/evidence_test.go index e0ea1a0..3aad1b7 100644 --- a/tests/interop/evidence_test.go +++ b/tests/interop/evidence_test.go @@ -33,14 +33,14 @@ const ( const ( flagData = "-data" - nameCms = "cms" + nameCMS = "cms" ) func TestCMSBEROpenSSL(t *testing.T) { fixture := newInteropFixture(t) selected(t, "portable") - for _, name := range []string{nameRsaSha256, nameEcdsaSha256} { + for _, name := range []string{nameRSASHA256, nameECDSASHA256} { t.Run(name, func(t *testing.T) { checkCMSBEROpenSSL(t, fixture, name) }) } } @@ -118,7 +118,7 @@ func TestEvidenceExchange(t *testing.T) { goProbe := func(mode string, args ...string) { command(t, fixture, true, nil, p, append([]string{"-mode", mode, "-at", at.Format(time.RFC3339)}, args...)...) } - rootKey := generateKey(t, dir, nameEcdsaSha384) + rootKey := generateKey(t, dir, nameECDSASHA384) rootTpl := &x509.Certificate{SerialNumber: big.NewInt(1), Subject: pkix.Name{CommonName: "temporary evidence root"}, NotBefore: at.Add(-24 * time.Hour), NotAfter: at.Add(24 * time.Hour), BasicConstraintsValid: true, IsCA: true, KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign, SubjectKeyId: []byte{1, 2, 3, 4}} rootCert, rootDER, rootPEM := certificateFiles(t, dir, nameRoot, rootTpl, rootTpl, rootKey.signer, rootKey.signer) @@ -130,7 +130,7 @@ func TestEvidenceExchange(t *testing.T) { crl := write(t, filepath.Join(dir, "crl.der"), crlRaw) data := write(t, filepath.Join(dir, nameContent), []byte("CMS and timestamp payload\x00\r\n")) - for _, name := range []string{nameRsaSha256, nameEcdsaSha256} { + for _, name := range []string{nameRSASHA256, nameECDSASHA256} { key := generateKey(t, dir, name) issue := func(label string, serial int64, eku x509.ExtKeyUsage) (*x509.Certificate, string, string) { tpl := &x509.Certificate{SerialNumber: big.NewInt(serial), Subject: pkix.Name{CommonName: label}, NotBefore: at.Add(-time.Hour), NotAfter: at.Add(time.Hour), BasicConstraintsValid: true, KeyUsage: x509.KeyUsageDigitalSignature, ExtKeyUsage: []x509.ExtKeyUsage{eku}} @@ -144,7 +144,7 @@ func TestEvidenceExchange(t *testing.T) { for _, hash := range []int{256, 384, 512} { pssValues := []bool{false} - if name == nameRsaSha256 { + if name == nameRSASHA256 { pssValues = append(pssValues, true) } @@ -163,7 +163,7 @@ func TestEvidenceExchange(t *testing.T) { goProbe("ocsp-request", flagCert, leafDER, "-issuer", rootDER, "-nonce", "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", flagOut, request) args := []string{"-request", request, flagCert, responderDER, "-issuer", rootDER, flagData, leafDER, flagKey, key.private, flagOut, response} - if name == nameRsaSha256 { + if name == nameRSASHA256 { args = append(args, "-pss") } @@ -242,70 +242,6 @@ func TestEvidenceExchange(t *testing.T) { t.Log("18 CMS, 6 RFC3161, 2 XAdES-T, 4 archive exchanges each direction; 2 Go OCSP responses verified by OpenSSL") } -func certificateFiles(t *testing.T, dir, name string, tpl, parent *x509.Certificate, signer, issuer crypto.Signer) (*x509.Certificate, string, string) { - t.Helper() - - der, err := x509.CreateCertificate(rand.Reader, tpl, parent, signer.Public(), issuer) - if err != nil { - t.Fatal(err) - } - - cert, err := x509.ParseCertificate(der) - if err != nil { - t.Fatal(err) - } - - return cert, write(t, filepath.Join(dir, name+".der"), der), write(t, filepath.Join(dir, name+".pem"), pem.EncodeToMemory(&pem.Block{Type: nameCERTIFICATE, Bytes: der})) -} - -func testEvidenceExchangeCase(t *testing.T, dir *string, key *testKey, signerDER *string, data *string, hash *int, pss *bool, detached *bool, goProbe *func(mode string, args ...string), fixture **interopFixture, signerPEM *string) { - ours, theirs, verified := filepath.Join((*dir), "go.cms"), filepath.Join((*dir), "ref.cms"), filepath.Join((*dir), "verified") - - args := []string{flagKey, key.private, flagCert, (*signerDER), flagData, (*data), flagOut, ours, "-hash", strconv.Itoa((*hash))} - if *pss { - args = append(args, "-pss") - } - - if *detached { - args = append(args, "-detached") - } - - (*goProbe)("cms-sign", args...) - - args = []string{nameCms, flagVerify, flagBinary, "-inform", nameDER, flagIn, ours, "-noverify", flagOut, verified} - if *detached { - args = append(args, "-content", (*data)) - } - - command(t, (*fixture), true, nil, "openssl", args...) - - if !bytes.Equal(read(t, verified), read(t, (*data))) { - t.Fatal("CMS content changed") - } - - args = []string{nameCms, "-sign", flagBinary, flagIn, (*data), "-signer", (*signerPEM), flagInkey, key.private, "-md", fmt.Sprintf("sha%d", (*hash)), "-nosmimecap", "-cades", "-outform", nameDER, flagOut, theirs} - if !(*detached) { - args = append(args, "-nodetach") - } - - if *pss { - args = append(args, "-keyopt", "rsa_padding_mode:pss", "-keyopt", "rsa_pss_saltlen:digest") - } - - command(t, (*fixture), true, nil, "openssl", args...) - - args = []string{flagIn, theirs, flagCert, (*signerDER), flagData, (*data), flagOut, verified} - if *detached { - args = append(args, "-detached") - } - - (*goProbe)("cms-verify", args...) - - if !bytes.Equal(read(t, verified), read(t, (*data))) { - t.Fatal("CMS content changed") - } -} - func TestEvidenceFixtures(t *testing.T) { fixture := newInteropFixture(t) @@ -348,14 +284,14 @@ func testEvidenceFixturesCase(t *testing.T, base *string, tc *struct { epoch := strconv.FormatInt(at.Unix(), 10) asPEM := func(source, name string) string { - return write(t, filepath.Join((*dir), name+".pem"), pem.EncodeToMemory(&pem.Block{Type: nameCERTIFICATE, Bytes: read(t, source)})) + return write(t, filepath.Join((*dir), name+".pem"), pem.EncodeToMemory(&pem.Block{Type: certificatePEMType, Bytes: read(t, source)})) } switch tc.Group { - case nameCms: + case nameCMS: output := filepath.Join((*dir), "cms-content") - args := []string{nameCms, flagVerify, flagBinary, "-inform", nameDER, flagIn, path("signature.der"), "-noverify", flagOut, output} + args := []string{nameCMS, flagVerify, flagBinary, "-inform", nameDER, flagIn, path("signature.der"), "-noverify", flagOut, output} if tc.Detached { args = append(args, "-content", path("content.bin")) } @@ -479,10 +415,10 @@ func TestCMSTrustBindingsOpenSSL(t *testing.T) { selected(t, "portable") dir := t.TempDir() at := time.Now().UTC().Truncate(time.Second) - rootKey := generateKey(t, dir, nameEcdsaSha256) + rootKey := generateKey(t, dir, nameECDSASHA256) rootTemplate := &x509.Certificate{SerialNumber: big.NewInt(1), Subject: pkix.Name{CommonName: "CMS trust fixture root"}, NotBefore: at.Add(-time.Hour), NotAfter: at.Add(24 * time.Hour), BasicConstraintsValid: true, IsCA: true, KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign, SubjectKeyId: []byte{1, 2, 3, 4}} root, rootDER, rootPEM := certificateFiles(t, dir, "cms-root", rootTemplate, rootTemplate, rootKey.signer, rootKey.signer) - signerKey := generateKey(t, dir, nameEcdsaSha384) + signerKey := generateKey(t, dir, nameECDSASHA384) signerTemplate := &x509.Certificate{SerialNumber: big.NewInt(2), Subject: pkix.Name{CommonName: "CMS selected signer"}, NotBefore: at.Add(-time.Hour), NotAfter: at.Add(time.Hour), KeyUsage: x509.KeyUsageDigitalSignature, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageCodeSigning}} signer, _, signerPEM := certificateFiles(t, dir, "cms-signer", signerTemplate, root, signerKey.signer, rootKey.signer) content := []byte("independent CMS content, certificate and trust bindings\x00\xff") @@ -549,10 +485,10 @@ func TestOCSPAuthorizationOpenSSL(t *testing.T) { selected(t, "portable") dir := t.TempDir() at := time.Now().UTC().Truncate(time.Second) - rootKey := generateKey(t, dir, nameEcdsaSha256) + rootKey := generateKey(t, dir, nameECDSASHA256) rootTemplate := &x509.Certificate{SerialNumber: big.NewInt(1), Subject: pkix.Name{CommonName: "OCSP authority root"}, NotBefore: at.Add(-time.Hour), NotAfter: at.Add(time.Hour), IsCA: true, BasicConstraintsValid: true, KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign, SubjectKeyId: []byte{1, 2, 3, 4}} root, _, rootPEM := certificateFiles(t, dir, "ocsp-root", rootTemplate, rootTemplate, rootKey.signer, rootKey.signer) - leafKey := generateKey(t, dir, nameEcdsaSha384) + leafKey := generateKey(t, dir, nameECDSASHA384) leafTemplate := &x509.Certificate{SerialNumber: big.NewInt(2), Subject: pkix.Name{CommonName: "OCSP target"}, NotBefore: at.Add(-time.Hour), NotAfter: at.Add(time.Hour), KeyUsage: x509.KeyUsageDigitalSignature} leaf, _, leafPEM := certificateFiles(t, dir, "ocsp-leaf", leafTemplate, root, leafKey.signer, rootKey.signer) request, err := pki.CreateOCSPRequest(leaf.Raw, root.Raw, crypto.SHA256, nil) @@ -561,7 +497,7 @@ func TestOCSPAuthorizationOpenSSL(t *testing.T) { } for _, local := range []bool{false, true} { t.Run(strconv.FormatBool(local), func(t *testing.T) { - responderKey := generateKey(t, t.TempDir(), nameEcdsaSha256) + responderKey := generateKey(t, t.TempDir(), nameECDSASHA256) responderTemplate := &x509.Certificate{AuthorityKeyId: root.SubjectKeyId, SerialNumber: big.NewInt(3), Subject: root.Subject, NotBefore: at.Add(-time.Hour), NotAfter: at.Add(time.Hour), KeyUsage: x509.KeyUsageDigitalSignature, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageOCSPSigning}, ExtraExtensions: []pkix.Extension{{Id: asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 48, 1, 5}, Value: []byte{5, 0}}}} parent, issuerKey := root, rootKey.signer if local { @@ -642,10 +578,10 @@ func TestOCSPNonceBindingOpenSSL(t *testing.T) { selected(t, "portable") dir := t.TempDir() at := time.Now().UTC().Truncate(time.Second) - rootKey := generateKey(t, dir, nameEcdsaSha256) + rootKey := generateKey(t, dir, nameECDSASHA256) rootTemplate := &x509.Certificate{SerialNumber: big.NewInt(1), Subject: pkix.Name{CommonName: "OCSP nonce root"}, NotBefore: at.Add(-time.Hour), NotAfter: at.Add(time.Hour), IsCA: true, BasicConstraintsValid: true, KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign} root, _, rootPEM := certificateFiles(t, dir, "nonce-root", rootTemplate, rootTemplate, rootKey.signer, rootKey.signer) - leafKey := generateKey(t, dir, nameEcdsaSha384) + leafKey := generateKey(t, dir, nameECDSASHA384) leaf, _, _ := certificateFiles(t, dir, "nonce-leaf", &x509.Certificate{SerialNumber: big.NewInt(2), Subject: pkix.Name{CommonName: "OCSP nonce target"}, NotBefore: at.Add(-time.Hour), NotAfter: at.Add(time.Hour), KeyUsage: x509.KeyUsageDigitalSignature}, root, leafKey.signer, rootKey.signer) nonce := make([]byte, 32) if _, err := rand.Read(nonce); err != nil { @@ -696,10 +632,10 @@ func TestHistoricalPKIAuthenticationOpenSSL(t *testing.T) { dir := t.TempDir() issued := time.Date(2026, 10, 8, 12, 0, 0, 0, time.UTC) now := issued.Add(3 * time.Hour) - rootKey := generateKey(t, dir, nameEcdsaSha256) + rootKey := generateKey(t, dir, nameECDSASHA256) rootTemplate := &x509.Certificate{SerialNumber: big.NewInt(1), Subject: pkix.Name{CommonName: "historical PKI oracle root"}, NotBefore: issued.Add(-24 * time.Hour), NotAfter: issued.Add(24 * time.Hour), BasicConstraintsValid: true, IsCA: true, KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign, SubjectKeyId: []byte{9, 8, 7}} root, _, rootPEM := certificateFiles(t, dir, "history-root", rootTemplate, rootTemplate, rootKey.signer, rootKey.signer) - leafKey := generateKey(t, dir, nameEcdsaSha384) + leafKey := generateKey(t, dir, nameECDSASHA384) leafTemplate := &x509.Certificate{SerialNumber: big.NewInt(2), Subject: pkix.Name{CommonName: "expired historical target"}, NotBefore: issued.Add(-time.Hour), NotAfter: issued.Add(time.Hour), KeyUsage: x509.KeyUsageDigitalSignature} leaf, _, leafPEM := certificateFiles(t, dir, "history-target", leafTemplate, root, leafKey.signer, rootKey.signer) command(t, fixture, true, nil, "openssl", "verify", "-purpose", "any", "-CAfile", rootPEM, "-attime", strconv.FormatInt(issued.Unix(), 10), leafPEM) @@ -770,3 +706,67 @@ func TestHistoricalPKIAuthenticationOpenSSL(t *testing.T) { }) } } + +func certificateFiles(t *testing.T, dir, name string, tpl, parent *x509.Certificate, signer, issuer crypto.Signer) (*x509.Certificate, string, string) { + t.Helper() + + der, err := x509.CreateCertificate(rand.Reader, tpl, parent, signer.Public(), issuer) + if err != nil { + t.Fatal(err) + } + + cert, err := x509.ParseCertificate(der) + if err != nil { + t.Fatal(err) + } + + return cert, write(t, filepath.Join(dir, name+".der"), der), write(t, filepath.Join(dir, name+".pem"), pem.EncodeToMemory(&pem.Block{Type: certificatePEMType, Bytes: der})) +} + +func testEvidenceExchangeCase(t *testing.T, dir *string, key *testKey, signerDER *string, data *string, hash *int, pss *bool, detached *bool, goProbe *func(mode string, args ...string), fixture **interopFixture, signerPEM *string) { + ours, theirs, verified := filepath.Join((*dir), "go.cms"), filepath.Join((*dir), "ref.cms"), filepath.Join((*dir), "verified") + + args := []string{flagKey, key.private, flagCert, (*signerDER), flagData, (*data), flagOut, ours, "-hash", strconv.Itoa((*hash))} + if *pss { + args = append(args, "-pss") + } + + if *detached { + args = append(args, "-detached") + } + + (*goProbe)("cms-sign", args...) + + args = []string{nameCMS, flagVerify, flagBinary, "-inform", nameDER, flagIn, ours, "-noverify", flagOut, verified} + if *detached { + args = append(args, "-content", (*data)) + } + + command(t, (*fixture), true, nil, "openssl", args...) + + if !bytes.Equal(read(t, verified), read(t, (*data))) { + t.Fatal("CMS content changed") + } + + args = []string{nameCMS, "-sign", flagBinary, flagIn, (*data), "-signer", (*signerPEM), flagInkey, key.private, "-md", fmt.Sprintf("sha%d", (*hash)), "-nosmimecap", "-cades", "-outform", nameDER, flagOut, theirs} + if !(*detached) { + args = append(args, "-nodetach") + } + + if *pss { + args = append(args, "-keyopt", "rsa_padding_mode:pss", "-keyopt", "rsa_pss_saltlen:digest") + } + + command(t, (*fixture), true, nil, "openssl", args...) + + args = []string{flagIn, theirs, flagCert, (*signerDER), flagData, (*data), flagOut, verified} + if *detached { + args = append(args, "-detached") + } + + (*goProbe)("cms-verify", args...) + + if !bytes.Equal(read(t, verified), read(t, (*data))) { + t.Fatal("CMS content changed") + } +} diff --git a/tests/interop/fixtures_test.go b/tests/interop/fixtures_test.go index 398ad82..c3a94aa 100644 --- a/tests/interop/fixtures_test.go +++ b/tests/interop/fixtures_test.go @@ -3,9 +3,9 @@ package interop_test const ( - flagEnabledReferenceUris = "--enabled-reference-uris" + flagEnabledReferenceURIs = "--enabled-reference-uris" flagIDAttrID = "--id-attr:ID" - flagPubkeyPem = "--pubkey-pem" + flagPublicKeyPEM = "--pubkey-pem" flagNativeOutput = "--output" flagO2 = "-O2" flagBinary = "-binary" @@ -13,7 +13,7 @@ const ( flagIn = "-in" flagInkey = "-inkey" flagVerify = "-verify" - nameCERTIFICATE = "CERTIFICATE" + certificatePEMType = "CERTIFICATE" nameCipherData = "CipherData" nameDER = "DER" nameDataAlgorithm = "DataAlgorithm" @@ -23,14 +23,14 @@ const ( nameMethod = "Method" publicKeyPEMType = "PUBLIC KEY" nameContent = "content" - nameEcdsaSha256 = "ecdsa-sha256" - nameEcdsaSha384 = "ecdsa-sha384" - nameEcdsaSha512 = "ecdsa-sha512" + nameECDSASHA256 = "ecdsa-sha256" + nameECDSASHA384 = "ecdsa-sha384" + nameECDSASHA512 = "ecdsa-sha512" nameEd25519 = "ed25519" nameElement = "element" - nameRsa = "rsa" - nameRsaSha256 = "rsa-sha256" - nameSha256 = "sha256" + nameRSA = "rsa" + nameRSASHA256 = "rsa-sha256" + nameSHA256 = "sha256" nameSign = "sign" nameText = "text" nameValid = "valid" @@ -42,8 +42,8 @@ const ( nameMode = "Mode" nameEmptySameDoc = "empty,same-doc" nameRoot = "root" - nameRsaSha384 = "rsa-sha384" + nameRSASHA384 = "rsa-sha384" flagWerror = "-Werror" - nameRsaSha512 = "rsa-sha512" + nameRSASHA512 = "rsa-sha512" flagWextra = "-Wextra" ) diff --git a/tests/interop/helpers_test.go b/tests/interop/helpers_test.go index ba68877..f8b2ee0 100644 --- a/tests/interop/helpers_test.go +++ b/tests/interop/helpers_test.go @@ -29,6 +29,14 @@ import ( "github.com/skarm/xmlsec/internal/testutil" ) +const ( + fixtureFileMode = 0o600 + fixtureDirectoryMode = 0o700 + fixtureReadOnlyFileMode = 0o400 +) + +const externalCommandTimeout = 2 * time.Minute + const ( ds = "http://www.w3.org/2000/09/xmldsig#" more = "http://www.w3.org/2001/04/xmldsig-more#" @@ -43,7 +51,7 @@ func canonicalizations() []string { return []string{exc, exc + "WithComments", c10, c10 + "#WithComments", c11, c11 + "#WithComments"} } -func digests() []string { return []string{enc + nameSha256, more + "sha384", enc + "sha512"} } +func digests() []string { return []string{enc + nameSHA256, more + "sha384", enc + "sha512"} } func javaExports() []string { return []string{"--add-exports=java.xml.crypto/com.sun.org.apache.xml.internal.security=ALL-UNNAMED", "--add-exports=java.xml.crypto/com.sun.org.apache.xml.internal.security.c14n=ALL-UNNAMED"} @@ -65,7 +73,7 @@ func selected(t *testing.T, suite string) { func command(t *testing.T, fixture *interopFixture, success bool, input []byte, name string, args ...string) ([]byte, []byte) { t.Helper() - ctx, cancel := context.WithTimeout(t.Context(), 2*time.Minute) + ctx, cancel := context.WithTimeout(t.Context(), externalCommandTimeout) defer cancel() c := exec.CommandContext(ctx, name, args...) @@ -99,7 +107,7 @@ func command(t *testing.T, fixture *interopFixture, success bool, input []byte, func write(t *testing.T, path string, data []byte) string { t.Helper() - if err := os.WriteFile(path, data, 0o600); err != nil { + if err := os.WriteFile(path, data, fixtureFileMode); err != nil { t.Fatal(err) } @@ -201,7 +209,7 @@ func compileJava(t *testing.T, fixture *interopFixture) { } args := append(append([]string{}, javaExports()...), "-d", fixture.work) - for _, name := range []string{"DetachedOracle", "CoreOracle", "TransformOracle", "ManifestOracle", "C14NOracle"} { + for _, name := range []string{"DetachedOracle", "CoreOracle", "TransformOracle", "ManifestOracle", "C14NOracle", "ECPProofOracle"} { args = append(args, filepath.Join(fixture.root, "tests/interop/testdata", name+".java")) } @@ -255,13 +263,13 @@ func generateKey(t *testing.T, dir, name string) testKey { method := more + name switch name { - case nameRsaSha256, nameRsaSha384, nameRsaSha512: + case nameRSASHA256, nameRSASHA384, nameRSASHA512: signer, err = rsa.GenerateKey(rand.Reader, 2048) - case nameEcdsaSha256: + case nameECDSASHA256: signer, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader) - case nameEcdsaSha384: + case nameECDSASHA384: signer, err = ecdsa.GenerateKey(elliptic.P384(), rand.Reader) - case nameEcdsaSha512: + case nameECDSASHA512: signer, err = ecdsa.GenerateKey(elliptic.P521(), rand.Reader) case nameEd25519: _, signer, err = ed25519.GenerateKey(rand.Reader) @@ -291,7 +299,7 @@ func generateKey(t *testing.T, dir, name string) testKey { name: name, method: method, signer: signer, cert: cert, private: testutil.WritePrivateKey(t, dir, name+".key.pem", signer), public: testutil.WritePublicKey(t, dir, name+".pub.pem", signer.Public()), - certificate: write(t, filepath.Join(dir, name+".cert.pem"), pem.EncodeToMemory(&pem.Block{Type: nameCERTIFICATE, Bytes: certDER})), + certificate: write(t, filepath.Join(dir, name+".cert.pem"), pem.EncodeToMemory(&pem.Block{Type: certificatePEMType, Bytes: certDER})), } } diff --git a/tests/interop/keys_test.go b/tests/interop/keys_test.go index 4da7d8c..63477cf 100644 --- a/tests/interop/keys_test.go +++ b/tests/interop/keys_test.go @@ -44,9 +44,9 @@ const ( ) const ( - namePkcs12Pbmac1 = "pkcs12-pbmac1" + namePKCS12PBMAC1 = "pkcs12-pbmac1" namePassword = "Password" - namePkcs12 = "pkcs12" + namePKCS12 = "pkcs12" ) func pkcs12InteropFields(t *testing.T, raw []byte) []asn1.RawValue { @@ -93,7 +93,7 @@ func TestXMLSecPKCS12Forms(t *testing.T) { passwordFile := write(t, filepath.Join(dir, "password"), []byte("forms password\n")) var infos [][]byte certs := make([][]byte, 0, 2) - for i, name := range []string{nameEcdsaSha256, nameEd25519} { + for i, name := range []string{nameECDSASHA256, nameEd25519} { key := generateKey(t, dir, name) path := filepath.Join(dir, strconv.Itoa(i)+".p12") command(t, fixture, true, nil, "openssl", "pkcs12", "-export", "-inkey", key.private, "-in", key.certificate, "-out", path, "-passout", passwordFilePrefix+passwordFile, "-keypbe", "AES-256-CBC", "-certpbe", "AES-256-CBC", "-iter", "1000", "-macalg", "SHA256", "-pbmac1_pbkdf2", "-pbmac1_pbkdf2_md", "SHA256") @@ -173,7 +173,7 @@ func TestXMLSecKeyFormats(t *testing.T) { cli := binary(t, fixture, "xmlsec") count := 0 - for _, name := range []string{nameRsaSha256, nameEcdsaSha256, nameEd25519} { + for _, name := range []string{nameRSASHA256, nameECDSASHA256, nameEd25519} { key := generateKey(t, dir, name) pkcs8, err := x509.MarshalPKCS8PrivateKey(key.signer) @@ -188,7 +188,7 @@ func TestXMLSecKeyFormats(t *testing.T) { t.Fatal("private key PEM") } - if name == nameRsaSha256 { + if name == nameRSASHA256 { k, err := keys.ParsePrivate(read(t, key.private), keys.Limits{}) if err != nil { t.Fatal(err) @@ -197,7 +197,7 @@ func TestXMLSecKeyFormats(t *testing.T) { formats = append(formats, traditionalRSA(t, k)) } - if name == nameEcdsaSha256 { + if name == nameECDSASHA256 { k, err := keys.ParsePrivate(read(t, key.private), keys.Limits{}) if err != nil { t.Fatal(err) @@ -216,7 +216,7 @@ func TestXMLSecKeyFormats(t *testing.T) { source := write(t, filepath.Join(dir, "template.xml"), []byte(coreTemplate("whole", key.method, exc))) ours, theirs := filepath.Join(dir, "go.xml"), filepath.Join(dir, "ref.xml") command(t, fixture, true, nil, cli, "sign-template", flagKey, private, "-modern-algorithms", "-signature-index", "0", flagIn, source, flagOut, ours) - args := append([]string{"--privkey-der", private, flagEnabledReferenceUris, nameEmptySameDoc}, idArgs()...) + args := append([]string{"--privkey-der", private, flagEnabledReferenceURIs, nameEmptySameDoc}, idArgs()...) native(t, fixture, true, nameSign, append(args, "--output", theirs, source)...) for i, publicDER := range [][]byte{spki, key.cert.Raw} { @@ -227,7 +227,7 @@ func TestXMLSecKeyFormats(t *testing.T) { arg = "--pubkey-cert-der" } - args := append([]string{arg, public, flagEnabledReferenceUris, nameEmptySameDoc}, idArgs()...) + args := append([]string{arg, public, flagEnabledReferenceURIs, nameEmptySameDoc}, idArgs()...) native(t, fixture, true, nameVerify, append(args, ours)...) raw, _ := command(t, fixture, true, nil, cli, "verify-core", flagKey, public, "-modern-algorithms", "-signature-index", "0", flagIn, theirs) verifyReport(t, raw, true) @@ -264,12 +264,12 @@ func TestXMLSecProtectedKeys(t *testing.T) { selected(t, "xmlsec") dir := t.TempDir() cli := binary(t, fixture, "xmlsec") - chain := generateKey(t, dir, nameEcdsaSha384) + chain := generateKey(t, dir, nameECDSASHA384) count, macControls := 0, 0 openssl, _ := command(t, fixture, true, nil, "openssl", "version") t.Log(string(openssl)) - for _, name := range []string{nameRsaSha256, nameEcdsaSha256, nameEd25519} { + for _, name := range []string{nameRSASHA256, nameECDSASHA256, nameEd25519} { key := generateKey(t, dir, name) expectedDER, err := x509.MarshalPKIXPublicKey(key.signer.Public()) @@ -283,7 +283,7 @@ func TestXMLSecProtectedKeys(t *testing.T) { for _, pair := range [][2]int{{128, 256}, {192, 384}, {256, 512}} { bits, digest := pair[0], pair[1] - for _, form := range []string{"pkcs8-der", pkcs8PEM, pkcs12Classic, namePkcs12Pbmac1} { + for _, form := range []string{"pkcs8-der", pkcs8PEM, pkcs12Classic, namePKCS12PBMAC1} { t.Run(name+"/"+strconv.Itoa(bits)+"/"+form, func(t *testing.T) { testXMLSecProtectedKeysCase(t, &form, &dir, &fixture, &key, &digest, &bits, &chain, &expectedDER, &expected, &certificateHash, &source, &cli, &macControls, &count) }) @@ -294,9 +294,9 @@ func TestXMLSecProtectedKeys(t *testing.T) { container := filepath.Join(dir, "empty.p12") passwordFile := write(t, filepath.Join(dir, "empty-password"), []byte{'\n'}) - args := []string{namePkcs12, flagExportPKCS12, flagInkey, key.private, flagIn, key.certificate, flagCertificateFile, chain.certificate, flagOut, container, flagPasswordOut, passwordFilePrefix + passwordFile, flagKeyPBE, "AES-256-CBC", flagCertificatePBE, "AES-256-CBC", flagMACAlgorithm, nameSha256, flagKDFIterations, protectedKeyIterations} + args := []string{namePKCS12, flagExportPKCS12, flagInkey, key.private, flagIn, key.certificate, flagCertificateFile, chain.certificate, flagOut, container, flagPasswordOut, passwordFilePrefix + passwordFile, flagKeyPBE, "AES-256-CBC", flagCertificatePBE, "AES-256-CBC", flagMACAlgorithm, nameSHA256, flagKDFIterations, protectedKeyIterations} if modern { - args = append(args, flagPBMAC1, flagPBMAC1Digest, nameSha256) + args = append(args, flagPBMAC1, flagPBMAC1Digest, nameSHA256) } command(t, fixture, true, nil, "openssl", args...) @@ -386,8 +386,8 @@ func alterPFX(t *testing.T, data []byte, remove bool) []byte { } func testXMLSecProtectedKeysCase(t *testing.T, form *string, dir *string, fixture **interopFixture, key *testKey, digest *int, bits *int, chain *testKey, expectedDER *[]byte, expected *[32]byte, certificateHash *[32]byte, source *string, cli *string, macControls *int, count *int) { - pfx := (*form) == pkcs12Classic || (*form) == namePkcs12Pbmac1 - modern := (*form) == namePkcs12Pbmac1 + pfx := (*form) == pkcs12Classic || (*form) == namePKCS12PBMAC1 + modern := (*form) == namePKCS12PBMAC1 password := "public пароль ü" if modern { @@ -418,9 +418,9 @@ func testXMLSecProtectedKeysCase(t *testing.T, form *string, dir *string, fixtur write(t, ours, encoded) if pfx { - command(t, (*fixture), true, nil, "openssl", namePkcs12, flagIn, ours, "-passin", passwordFilePrefix+passwordFile, "-noenc", flagOut, restored) + command(t, (*fixture), true, nil, "openssl", namePKCS12, flagIn, ours, "-passin", passwordFilePrefix+passwordFile, "-noenc", flagOut, restored) - args := []string{namePkcs12, flagExportPKCS12, flagInkey, key.private, flagIn, key.certificate, flagCertificateFile, chain.certificate, "-name", "test identity", flagOut, theirs, flagPasswordOut, passwordFilePrefix + passwordFile, flagKeyPBE, fmt.Sprintf("AES-%d-CBC", (*bits)), flagCertificatePBE, fmt.Sprintf("AES-%d-CBC", (*bits)), flagKDFIterations, protectedKeyIterations, flagMACAlgorithm, fmt.Sprintf("sha%d", (*digest))} + args := []string{namePKCS12, flagExportPKCS12, flagInkey, key.private, flagIn, key.certificate, flagCertificateFile, chain.certificate, "-name", "test identity", flagOut, theirs, flagPasswordOut, passwordFilePrefix + passwordFile, flagKeyPBE, fmt.Sprintf("AES-%d-CBC", (*bits)), flagCertificatePBE, fmt.Sprintf("AES-%d-CBC", (*bits)), flagKDFIterations, protectedKeyIterations, flagMACAlgorithm, fmt.Sprintf("sha%d", (*digest))} if modern { args = append(args, flagPBMAC1, flagPBMAC1Digest, fmt.Sprintf("sha%d", (*digest))) } @@ -453,19 +453,19 @@ func testXMLSecProtectedKeysCase(t *testing.T, form *string, dir *string, fixtur assertJSON(t, r, "extras", 1) } - args := append([]string{nativeArg, ours, "--pwd", password, flagEnabledReferenceUris, nameEmptySameDoc}, idArgs()...) + args := append([]string{nativeArg, ours, "--pwd", password, flagEnabledReferenceURIs, nameEmptySameDoc}, idArgs()...) native(t, (*fixture), true, nameSign, append(args, "--output", signed, (*source))...) raw, _ := command(t, (*fixture), true, nil, (*cli), "verify-core", flagKey, key.public, "-signature-index", "0", "-modern-algorithms", flagIn, signed) verifyReport(t, raw, true) keyFormat := "pkcs8" if pfx { - keyFormat = namePkcs12 + keyFormat = namePKCS12 } command(t, (*fixture), true, nil, (*cli), "sign-template", flagKey, theirs, "-key-format", keyFormat, "-password-file", passwordFile, "-signature-index", "0", "-modern-algorithms", flagIn, (*source), flagOut, signed) - args = append([]string{flagPubkeyPem, key.public, flagEnabledReferenceUris, nameEmptySameDoc}, idArgs()...) + args = append([]string{flagPublicKeyPEM, key.public, flagEnabledReferenceURIs, nameEmptySameDoc}, idArgs()...) native(t, (*fixture), true, nameVerify, append(args, signed)...) parse[namePassword] = []byte("wrong test password") @@ -477,7 +477,7 @@ func testXMLSecProtectedKeysCase(t *testing.T, form *string, dir *string, fixtur wrong := write(t, filepath.Join((*dir), "wrong-password"), []byte("wrong test password\n")) if pfx { - command(t, (*fixture), false, nil, "openssl", namePkcs12, flagIn, theirs, "-passin", passwordFilePrefix+wrong, "-noout") + command(t, (*fixture), false, nil, "openssl", namePKCS12, flagIn, theirs, "-passin", passwordFilePrefix+wrong, "-noout") } else { command(t, (*fixture), false, nil, "openssl", "pkcs8", flagIn, theirs, "-inform", format, "-passin", passwordFilePrefix+wrong, flagOut, restored) } @@ -489,10 +489,10 @@ func testXMLSecProtectedKeysCase(t *testing.T, form *string, dir *string, fixtur parse[namePassword] = []byte(password) parse[nameInput] = bad probe(t, (*fixture), "keyprobe", parse, false) - command(t, (*fixture), false, nil, "openssl", namePkcs12, flagIn, bad, "-passin", passwordFilePrefix+passwordFile, "-noout") + command(t, (*fixture), false, nil, "openssl", namePKCS12, flagIn, bad, "-passin", passwordFilePrefix+passwordFile, "-noout") os.Remove(signed) - args = append([]string{"--pkcs12", bad, "--pwd", password, flagEnabledReferenceUris, nameEmptySameDoc}, idArgs()...) + args = append([]string{"--pkcs12", bad, "--pwd", password, flagEnabledReferenceURIs, nameEmptySameDoc}, idArgs()...) native(t, (*fixture), false, nameSign, append(args, "--output", signed, (*source))...) noOutput(t, signed) write(t, bad, alterPFX(t, read(t, theirs), true)) @@ -508,15 +508,15 @@ func TestOpenSSLProtectedKeys(t *testing.T) { fixture := newInteropFixture(t) dir := t.TempDir() - chain := generateKey(t, dir, nameEcdsaSha384) - for _, name := range []string{nameRsaSha256, nameEcdsaSha256, nameEd25519} { + chain := generateKey(t, dir, nameECDSASHA384) + for _, name := range []string{nameRSASHA256, nameECDSASHA256, nameEd25519} { key := generateKey(t, dir, name) for _, pair := range []struct { bits int hash crypto.Hash }{{128, crypto.SHA256}, {192, crypto.SHA384}, {256, crypto.SHA512}} { - for _, form := range []string{"pkcs8-der", pkcs8PEM, pkcs12Classic, namePkcs12Pbmac1} { + for _, form := range []string{"pkcs8-der", pkcs8PEM, pkcs12Classic, namePKCS12PBMAC1} { t.Run(name+"/"+strconv.Itoa(pair.bits)+"/"+form, func(t *testing.T) { testOpenSSLProtectedKey(t, fixture, key, chain, form, keys.EncryptionOptions{Hash: pair.hash, KeyBits: pair.bits, Iterations: 2048}) }) @@ -531,7 +531,7 @@ func testOpenSSLProtectedKey(t *testing.T, fixture *interopFixture, key, chain t password := []byte("public пароль ü") passwordFile := write(t, filepath.Join(dir, "password"), append(bytes.Clone(password), '\n')) ours, theirs, restored := filepath.Join(dir, "ours"), filepath.Join(dir, "theirs"), filepath.Join(dir, "restored.pem") - pfx := form == pkcs12Classic || form == namePkcs12Pbmac1 + pfx := form == pkcs12Classic || form == namePKCS12PBMAC1 var ( output bytes.Buffer @@ -561,10 +561,10 @@ func testOpenSSLProtectedKey(t *testing.T, fixture *interopFixture, key, chain t digest := options.Hash.Size() * 8 if pfx { - command(t, fixture, true, nil, "openssl", namePkcs12, flagIn, ours, "-passin", passwordFilePrefix+passwordFile, "-noenc", flagOut, restored) + command(t, fixture, true, nil, "openssl", namePKCS12, flagIn, ours, "-passin", passwordFilePrefix+passwordFile, "-noenc", flagOut, restored) - args := []string{namePkcs12, flagExportPKCS12, flagInkey, key.private, flagIn, key.certificate, flagCertificateFile, chain.certificate, flagOut, theirs, flagPasswordOut, passwordFilePrefix + passwordFile, flagKeyPBE, fmt.Sprintf("AES-%d-CBC", options.KeyBits), flagCertificatePBE, fmt.Sprintf("AES-%d-CBC", options.KeyBits), flagKDFIterations, protectedKeyIterations, flagMACAlgorithm, fmt.Sprintf("sha%d", digest)} - if form == namePkcs12Pbmac1 { + args := []string{namePKCS12, flagExportPKCS12, flagInkey, key.private, flagIn, key.certificate, flagCertificateFile, chain.certificate, flagOut, theirs, flagPasswordOut, passwordFilePrefix + passwordFile, flagKeyPBE, fmt.Sprintf("AES-%d-CBC", options.KeyBits), flagCertificatePBE, fmt.Sprintf("AES-%d-CBC", options.KeyBits), flagKDFIterations, protectedKeyIterations, flagMACAlgorithm, fmt.Sprintf("sha%d", digest)} + if form == namePKCS12PBMAC1 { args = append(args, flagPBMAC1, flagPBMAC1Digest, fmt.Sprintf("sha%d", digest)) } @@ -627,7 +627,7 @@ func checkCertificates(t *testing.T, data []byte, expected ...[]byte) { data = rest - if p.Type == nameCERTIFICATE { + if p.Type == certificatePEMType { found[string(p.Bytes)] = struct{}{} } } @@ -675,7 +675,7 @@ func TestXMLSecPKCS8Compatibility(t *testing.T) { fixture := newInteropFixture(t) selected(t, "xmlsec") dir := t.TempDir() - key := generateKey(t, dir, nameEcdsaSha256) + key := generateKey(t, dir, nameECDSASHA256) password := []byte("compatibility password") passwordFile := write(t, filepath.Join(dir, "password"), append(bytes.Clone(password), '\n')) for _, bits := range []int{128, 192, 256} { diff --git a/tests/interop/main_test.go b/tests/interop/main_test.go index c9d1706..8c00998 100644 --- a/tests/interop/main_test.go +++ b/tests/interop/main_test.go @@ -11,12 +11,12 @@ import ( ) func TestMain(m *testing.M) { - suites := flag.String("suite", "portable", "comma-separated optional suites: portable,xmlsec,all") + suites := flag.String("suite", "portable", "comma-separated optional suites: portable,xmlsec,opensaml,shibboleth,authority-http,all") flag.String("xmlsec1", envDefault("XMLSEC1", "xmlsec1"), "external xmlsec1 executable") flag.Parse() for _, s := range strings.Split(*suites, ",") { - if s != "portable" && s != "xmlsec" && s != "all" { + if s != "portable" && s != "xmlsec" && s != "opensaml" && s != "shibboleth" && s != "authority-http" && s != "all" { fmt.Fprintln(os.Stderr, "unknown -suite:", s) os.Exit(2) } diff --git a/tests/interop/parameters_test.go b/tests/interop/parameters_test.go index 698df86..0db30e1 100644 --- a/tests/interop/parameters_test.go +++ b/tests/interop/parameters_test.go @@ -32,7 +32,7 @@ func TestParameters(t *testing.T) { selected(t, "portable") dir := t.TempDir() - key := generateKey(t, dir, nameRsaSha256) + key := generateKey(t, dir, nameRSASHA256) priv, err := x509.MarshalPKCS8PrivateKey(key.signer) if err != nil { @@ -241,8 +241,8 @@ func TestXMLSecHMAC(t *testing.T) { ours := write(t, filepath.Join(dir, "ours.xml"), probeData(t, fixture, "parametersprobe", map[string]any{nameMode: nameSign, nameMethod: method, nameSecret: secret, "Bits": output})) theirs := filepath.Join(dir, "theirs.xml") - native(t, fixture, true, nameVerify, "--hmackey", key, flagEnabledReferenceUris, "same-doc", ours) - native(t, fixture, true, nameSign, "--hmackey", key, flagEnabledReferenceUris, "same-doc", "--output", theirs, ours) + native(t, fixture, true, nameVerify, "--hmackey", key, flagEnabledReferenceURIs, "same-doc", ours) + native(t, fixture, true, nameSign, "--hmackey", key, flagEnabledReferenceURIs, "same-doc", "--output", theirs, ours) r := probe(t, fixture, "parametersprobe", map[string]any{nameMode: nameVerify, nameMethod: method, nameSecret: secret, nameInput: read(t, theirs)}, true) assertJSON(t, r, nameValid, true) } @@ -260,7 +260,7 @@ func verifySignatureValue(t *testing.T, fixture *interopFixture, d *tree.Documen value := decode64(t, text(one(t, sig, ds, "SignatureValue"))) dir := t.TempDir() messagePath := write(t, filepath.Join(dir, "message"), message) - hash := nameSha256 + hash := nameSHA256 for _, bits := range []string{"384", "512"} { if strings.Contains(method, bits) { diff --git a/tests/interop/profiles_test.go b/tests/interop/profiles_test.go index f04b8ad..efe9577 100644 --- a/tests/interop/profiles_test.go +++ b/tests/interop/profiles_test.go @@ -51,8 +51,8 @@ const uriManifest = "urn:manifest" func TestXMLSecExternalManifestURIContext(t *testing.T) { fixture := newInteropFixture(t) selected(t, "xmlsec") - key := generateKey(t, fixture.work, nameRsaSha256) - _, privateArg := nativeKeyArgs(nameRsa, "") + key := generateKey(t, fixture.work, nameRSASHA256) + _, privateArg := nativeKeyArgs(nameRSA, "") const resourceURI = "urn:external#manifest" @@ -103,7 +103,7 @@ func TestXMLSecExternalManifestURIContext(t *testing.T) { ours := write(t, filepath.Join(fixture.work, "fragment-go.xml"), xmlBytes(t, signed)) nativeArgs := []string{"--url-map:urn:external", nativeManifest, "--url-map:urn:attachment", payloadPath} - native(t, fixture, true, nameVerify, append(append([]string{flagPubkeyPem, key.public}, nativeArgs...), ours)...) + native(t, fixture, true, nameVerify, append(append([]string{flagPublicKeyPEM, key.public}, nativeArgs...), ours)...) theirs := filepath.Join(fixture.work, "fragment-native.xml") native(t, fixture, true, nameSign, append(append([]string{privateArg, key.private, flagNativeOutput, theirs}, nativeArgs...), template)...) nativeDoc := xmlDoc(t, read(t, theirs)) @@ -116,25 +116,27 @@ func TestXMLSecExternalManifestURIContext(t *testing.T) { write(t, payloadPath, []byte("changed fragment payload")) result, report, err = xmldsig.VerifySignatureDetailed(t.Context(), nativeDoc, nativeDoc.Root(), key.signer.Public(), opt) + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if !errors.Is(err, xmldsig.ErrDigest) || result != nil || report.References[0].Status != xmldsig.Valid { t.Fatal(result, report, err) } // xmlsec1 validates this external Manifest's incoming Reference, but does // not recursively discover its children. Record that boundary explicitly. - native(t, fixture, true, nameVerify, append(append([]string{flagPubkeyPem, key.public}, nativeArgs...), ours)...) + native(t, fixture, true, nameVerify, append(append([]string{flagPublicKeyPEM, key.public}, nativeArgs...), ours)...) changedDigest := sha256.Sum256([]byte("different nested digest")) changedManifest := strings.Replace(manifestXML, base64.StdEncoding.EncodeToString(digest[:]), base64.StdEncoding.EncodeToString(changedDigest[:]), 1) write(t, nativeManifest, []byte(`]>`+changedManifest)) - native(t, fixture, false, nameVerify, append(append([]string{flagPubkeyPem, key.public}, nativeArgs...), ours)...) + native(t, fixture, false, nameVerify, append(append([]string{flagPublicKeyPEM, key.public}, nativeArgs...), ours)...) } func TestXMLSecRelativeURIContext(t *testing.T) { fixture := newInteropFixture(t) selected(t, "xmlsec") - key := generateKey(t, fixture.work, nameRsaSha256) - _, privateArg := nativeKeyArgs(nameRsa, "") + key := generateKey(t, fixture.work, nameRSASHA256) + _, privateArg := nativeKeyArgs(nameRSA, "") const resolved = "https://example.test/payload.bin?version=1" @@ -168,9 +170,9 @@ func TestXMLSecRelativeURIContext(t *testing.T) { // xmlsec1's CLI opens the literal relative URI in this profile instead of // applying inherited xml:base. Keep that difference visible, then exchange // identical octets through an explicit literal mapping in the native adapter. - native(t, fixture, false, nameVerify, append(append([]string{flagPubkeyPem, key.public}, args...), ours)...) + native(t, fixture, false, nameVerify, append(append([]string{flagPublicKeyPEM, key.public}, args...), ours)...) args = []string{"--url-map:../payload.bin?version=1", payload} - native(t, fixture, true, nameVerify, append(append([]string{flagPubkeyPem, key.public}, args...), ours)...) + native(t, fixture, true, nameVerify, append(append([]string{flagPublicKeyPEM, key.public}, args...), ours)...) template := write(t, filepath.Join(fixture.work, "relative-template.xml"), xmlBytes(t, draft)) theirs := filepath.Join(fixture.work, "relative-native.xml") native(t, fixture, true, nameSign, append(append([]string{privateArg, key.private, flagNativeOutput, theirs}, args...), template)...) @@ -181,7 +183,7 @@ func TestXMLSecRelativeURIContext(t *testing.T) { } write(t, payload, []byte("changed relative resource")) - native(t, fixture, false, nameVerify, append(append([]string{flagPubkeyPem, key.public}, args...), ours)...) + native(t, fixture, false, nameVerify, append(append([]string{flagPublicKeyPEM, key.public}, args...), ours)...) if result, _, err := xmldsig.VerifySignatureDetailed(t.Context(), nativeDoc, xmldsig.Signatures(nativeDoc)[0], key.signer.Public(), opt); !errors.Is(err, xmldsig.ErrDigest) || result != nil { t.Fatal(result, err) @@ -191,7 +193,7 @@ func TestXMLSecRelativeURIContext(t *testing.T) { func TestExternalManifestInterop(t *testing.T) { fixture := newInteropFixture(t) selected(t, "portable") - key := generateKey(t, fixture.work, nameRsaSha256) + key := generateKey(t, fixture.work, nameRSASHA256) theirs := filepath.Join(fixture.work, "jdk-external.xml") manifest := filepath.Join(fixture.work, "external-manifest.xml") payload := write(t, filepath.Join(fixture.work, "external-payload.bin"), []byte("external Manifest payload\x00\xff")) @@ -254,6 +256,8 @@ func TestExternalManifestInterop(t *testing.T) { write(t, payload, []byte("changed external payload")) result, report, err = xmldsig.VerifySignatureDetailed(t.Context(), signed, signed.Root(), key.signer.Public(), options) + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if !errors.Is(err, xmldsig.ErrDigest) || result != nil || report.SignatureValue != xmldsig.Valid || report.References[0].Status != xmldsig.Valid { t.Fatal(result, report, err) } @@ -270,9 +274,9 @@ func TestManifestProfiles(t *testing.T) { attachment := write(t, filepath.Join(dir, "attachment.bin"), []byte("external bytes\x00\xff")) resources := write(t, filepath.Join(dir, "resources.json"), []byte(`[{"uri":"urn:attachment","path":"attachment.bin"}]`)) - for _, name := range []string{nameRsaSha256, nameRsaSha384, nameRsaSha512, nameEcdsaSha256} { + for _, name := range []string{nameRSASHA256, nameRSASHA384, nameRSASHA512, nameECDSASHA256} { key := generateKey(t, dir, name) - if strings.HasPrefix(name, nameRsa) { + if strings.HasPrefix(name, nameRSA) { key.method = pssURI + strings.TrimPrefix(name, "rsa-") + "-rsa-MGF1" } @@ -321,7 +325,7 @@ func TestSAMLProfiles(t *testing.T) { at := time.Now().UTC().Truncate(time.Second).Format(time.RFC3339) p := binary(t, fixture, "samlprobe") - for _, name := range []string{nameRsaSha256, nameEcdsaSha256} { + for _, name := range []string{nameRSASHA256, nameECDSASHA256} { key := generateKey(t, dir, name) cert := write(t, filepath.Join(dir, "cert.der"), key.cert.Raw) @@ -350,6 +354,9 @@ func TestSAMLProfiles(t *testing.T) { Valid bool } decode(t, read(t, filepath.Join(base, "manifest.json")), &cases) + if len(cases) != 16 { + t.Fatalf("independent SAML fixture count = %d, want 16", len(cases)) + } for _, tc := range cases { t.Run(tc.Name, func(t *testing.T) { @@ -437,11 +444,11 @@ func TestRedirectProfiles(t *testing.T) { relay := "state/ü?x=1&y=two" cases := []redirectProfileCase{ - {nameRsaSha256, nameSha256}, - {nameRsaSha384, "sha384"}, - {nameRsaSha512, "sha512"}, - {nameEcdsaSha256, nameSha256}, - {nameEd25519, nameSha256}, + {nameRSASHA256, nameSHA256}, + {nameRSASHA384, "sha384"}, + {nameRSASHA512, "sha512"}, + {nameECDSASHA256, nameSHA256}, + {nameEd25519, nameSHA256}, } for _, scenario := range cases { key := generateKey(t, dir, scenario.name) @@ -548,9 +555,9 @@ func TestCRLFixtures(t *testing.T) { base := filepath.Join(fixture.root, "testdata/next") leaf := filepath.Join(base, "pki-leaf.der") rootDER := filepath.Join(base, "pki-root.der") - rootPEM := write(t, filepath.Join(dir, "root.pem"), pem.EncodeToMemory(&pem.Block{Type: nameCERTIFICATE, Bytes: read(t, rootDER)})) + rootPEM := write(t, filepath.Join(dir, "root.pem"), pem.EncodeToMemory(&pem.Block{Type: certificatePEMType, Bytes: read(t, rootDER)})) - leafPEM := write(t, filepath.Join(dir, "leaf.pem"), pem.EncodeToMemory(&pem.Block{Type: nameCERTIFICATE, Bytes: read(t, leaf)})) + leafPEM := write(t, filepath.Join(dir, "leaf.pem"), pem.EncodeToMemory(&pem.Block{Type: certificatePEMType, Bytes: read(t, leaf)})) for _, mode := range []string{nameFresh, "revoked", "stale"} { crl := filepath.Join(base, "pki-"+mode+".crl.der") probe(t, fixture, "nextprobe", map[string]any{nameMode: "pki-verify", nameInput: leaf, "Root": rootDER, "CRL": crl, "At": "2026-10-07T12:00:00Z"}, mode == nameFresh) @@ -559,143 +566,6 @@ func TestCRLFixtures(t *testing.T) { } } -func referenceInput(t *testing.T, fixture *interopFixture, d *tree.Document, sig, ref *tree.Node, attachment []byte) []byte { - t.Helper() - - uri := attr(ref, "URI") - - var ( - target *tree.Node - octets []byte - ) - if uri == uriAttachment { - octets = bytes.Clone(attachment) - } else if uri == "" { - target = tree.Root(d) - } else if strings.HasPrefix(uri, "#") { - ident := strings.TrimPrefix(uri, "#") - - var walk func(*tree.Node) - - walk = func(n *tree.Node) { testreferenceInputCase(n, &ident, &target, &t, &walk) } - walk(tree.Root(d)) - - if target == nil { - t.Fatalf("unresolved ID %s", uri) - } - } else { - t.Fatalf("unallowlisted test URI %s", uri) - } - - var canonical string - - enveloped := false - - for _, tr := range nodes(ref, ds, "Transform") { - alg := attr(tr, "Algorithm") - if alg == ds+"enveloped-signature" { - enveloped = true - } else if alg == ds+"base64" { - if target != nil { - octets = []byte(text(target)) - target = nil - } - - octets = decode64(t, string(octets)) - } else if strings.HasPrefix(alg, exc) || strings.HasPrefix(alg, c10) || strings.HasPrefix(alg, c11) { - canonical = alg - } else { - t.Fatalf("unsupported fixed profile transform %s", alg) - } - } - - if target == nil { - if canonical != "" { - doc := xmlDoc(t, octets) - return externalCanonical(t, fixture, doc, nil, canonical) - } - - return octets - } - - if canonical == "" { - canonical = c10 - } - // Bare ID references strip comments before transforms, even WithComments. - removed := make(map[*tree.Node][]*tree.Node) - - var prune func(*tree.Node) - - prune = func(n *tree.Node) { testreferenceInputCase2(n, &sig, &enveloped, &prune, &removed) } - prune(target) - - defer func() { - for n, children := range removed { - n.Children = children - } - }() - - return externalCanonical(t, fixture, d, target, canonical) -} - -func checkReferences(t *testing.T, fixture *interopFixture, d *tree.Document, sig *tree.Node) bool { - t.Helper() - - valid := true - - for _, ref := range nodes(sig, ds, "Reference") { - alg := attr(one(t, ref, ds, "DigestMethod"), "Algorithm") - - bits := 256 - if strings.HasSuffix(alg, "384") { - bits = 384 - } else if strings.HasSuffix(alg, "512") { - bits = 512 - } - - input := referenceInput(t, fixture, d, sig, ref, nil) - - digest := hashBytes(bits, input) - if !bytes.Equal(digest, decode64(t, text(one(t, ref, ds, "DigestValue")))) { - valid = false - } - } - - return valid -} - -func testreferenceInputCase(n *tree.Node, ident *string, target **tree.Node, t **testing.T, walk *func(*tree.Node)) { - if attr(n, "Id") == (*ident) || attr(n, "ID") == (*ident) { - if (*target) != nil { - (*t).Fatal("duplicate ID") - } - - (*target) = n - } - - for _, c := range n.Children { - (*walk)(c) - } -} - -func testreferenceInputCase2(n *tree.Node, sig **tree.Node, enveloped *bool, prune *func(*tree.Node), removed *map[*tree.Node][]*tree.Node) { - original := n.Children - - var kept []*tree.Node - - for _, c := range original { - if c == (*sig) && (*enveloped) || c.Kind == tree.CommentNode { - continue - } - - kept = append(kept, c) - (*prune)(c) - } - - (*removed)[n] = original - n.Children = kept -} - func TestCRLCompositionsOpenSSL(t *testing.T) { fixture := newInteropFixture(t) selected(t, "portable") @@ -817,30 +687,383 @@ func TestCRLCompositionsOpenSSL(t *testing.T) { } crls := write(t, filepath.Join(directory, "crls.pem"), bundle.Bytes()) - args := []string{"verify", "-CAfile", root, "-CRLfile", crls, "-crl_check", "-extended_crl", "-use_deltas", "-attime", strconv.FormatInt(at.Unix(), 10)} - if strings.HasPrefix(test.name, "indirect ") { - untrusted := write(t, filepath.Join(directory, "delegate.pem"), pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: delegate.Raw})) - args = append(args, "-untrusted", untrusted) - } - args = append(args, leaf) - output, diagnostic := command(t, fixture, test.want == nil, nil, "openssl", args...) - if test.want == nil && !bytes.Contains(output, []byte(": OK")) { - t.Fatal(string(output), string(diagnostic)) - } - if test.want != nil { - expected := "unable to get certificate CRL" - if errors.Is(test.want, pki.ErrRevoked) { - expected = "certificate revoked" - } - if test.name == "expired complete" { - expected = "CRL has expired" - } - if !bytes.Contains(diagnostic, []byte(expected)) { - t.Fatal(string(output), string(diagnostic)) - } - } - }) + args := []string{"verify", "-CAfile", root, "-CRLfile", crls, "-crl_check", "-extended_crl", "-use_deltas", "-attime", strconv.FormatInt(at.Unix(), 10)} + if strings.HasPrefix(test.name, "indirect ") { + untrusted := write(t, filepath.Join(directory, "delegate.pem"), pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: delegate.Raw})) + args = append(args, "-untrusted", untrusted) + } + args = append(args, leaf) + output, diagnostic := command(t, fixture, test.want == nil, nil, "openssl", args...) + if test.want == nil && !bytes.Contains(output, []byte(": OK")) { + t.Fatal(string(output), string(diagnostic)) + } + if test.want != nil { + expected := "unable to get certificate CRL" + if errors.Is(test.want, pki.ErrRevoked) { + expected = "certificate revoked" + } + if test.name == "expired complete" { + expected = "CRL has expired" + } + if !bytes.Contains(diagnostic, []byte(expected)) { + t.Fatal(string(output), string(diagnostic)) + } + } + }) + } +} + +func TestDSSHistoricalValidation(t *testing.T) { + selected(t, "portable") + + manifestPath := os.Getenv("XMLSEC_DSS_FIXTURES") + endpoint := os.Getenv("XMLSEC_DSS_URL") + runtimeJar := os.Getenv("XMLSEC_DSS_RUNTIME_JAR") + if manifestPath == "" || endpoint == "" || runtimeJar == "" { + if os.Getenv("XMLSEC_REQUIRE_P7_PROOFS") == "1" { + t.Fatal("XMLSEC_DSS_FIXTURES, XMLSEC_DSS_URL and XMLSEC_DSS_RUNTIME_JAR are required") + } + + t.Skip("optional local DSS historical compatibility fixtures/runtime") + } + + base, err := dssLoopbackURL(endpoint) + if err != nil { + t.Fatal(err) + } + + var manifest dssHistoricalManifest + decoder := json.NewDecoder(bytes.NewReader(dssReadFile(t, filepath.Dir(manifestPath), filepath.Base(manifestPath), 256<<10))) + decoder.DisallowUnknownFields() + if err = decoder.Decode(&manifest); err != nil { + t.Fatal(err) + } + if err = decoder.Decode(new(any)); !errors.Is(err, io.EOF) { + t.Fatal("trailing DSS manifest data:", err) + } + + if manifest.Version != "6.5.1" || manifest.ValidationTime.IsZero() || len(manifest.Pairs) == 0 || len(manifest.Pairs) > 16 { + t.Fatal("invalid DSS fixture version, explicit validation time or bounded pair count") + } + anchor, err := hex.DecodeString(manifest.TrustAnchorSHA256) + if err != nil || len(anchor) != sha256.Size { + t.Fatal("missing exact DSS trust-anchor certificate SHA-256") + } + anchorID := "C-" + strings.ToUpper(manifest.TrustAnchorSHA256) + + client := &http.Client{Timeout: time.Minute, Transport: &http.Transport{Proxy: nil}, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }} + t.Cleanup(client.CloseIdleConnections) + dssCheckRuntime(t, client, base, runtimeJar, manifest.Version) + + directory := filepath.Dir(manifestPath) + var policy *dssRemoteDocument + if manifest.Policy != "" { + policy = &dssRemoteDocument{Bytes: dssReadFile(t, directory, manifest.Policy, 1<<20), Name: filepath.Base(manifest.Policy)} + } + + seen := map[string]struct{}{} + for _, pair := range manifest.Pairs { + if _, exists := seen[pair.Name]; pair.Name == "" || exists { + t.Fatal("empty or duplicate historical pair name") + } + + seen[pair.Name] = struct{}{} + t.Run(pair.Name, func(t *testing.T) { + sufficient := dssReadFile(t, directory, pair.Sufficient, 16<<20) + insufficient := dssReadFile(t, directory, pair.Insufficient, 16<<20) + dssCheckSameSignature(t, sufficient, insufficient) + if pair.WantSufficient.Indication != "TOTAL_PASSED" || pair.WantSufficient.SubIndication != "" || pair.WantInsufficient.Indication != "INDETERMINATE" || pair.WantInsufficient.SubIndication == "" { + t.Fatal("pair must specify total success and one exact indeterminate reason") + } + + for _, item := range []struct { + name string + raw []byte + want dssExpectation + }{{"sufficient", sufficient, pair.WantSufficient}, {"insufficient", insufficient, pair.WantInsufficient}} { + t.Run(item.name, func(t *testing.T) { + raw := dssPostValidation(t, client, base, dssValidationRequest{SignedDocument: dssRemoteDocument{Bytes: item.raw, Name: pair.Name + ".xml"}, Policy: policy, ValidationTime: manifest.ValidationTime}) + dssSaveReport(t, item.raw, raw) + var report dssReports + if err := json.Unmarshal(raw, &report); err != nil { + t.Fatal(err) + } + + if err := report.check(manifest.ValidationTime, item.want, anchorID); err != nil { + t.Fatalf("DSS verdict: %v; summary=%+v\n%.4096s", err, report.SimpleReport, raw) + } + if err := report.checkArchiveRecovery(item.want.Indication == "TOTAL_PASSED"); err != nil { + t.Fatal("DSS archive workflow:", err) + } + + t.Logf("DSS %s %s/%s input-sha256=%x best-signature-time=%s", manifest.Version, item.want.Indication, item.want.SubIndication, sha256.Sum256(item.raw), item.want.BestSignatureTime.Format(time.RFC3339Nano)) + }) + } + }) + } +} + +func TestDSSHistoricalReportContracts(t *testing.T) { + at := time.Date(2026, 10, 25, 0, 0, 0, 0, time.UTC) + proof := at.AddDate(-1, 0, 0) + const raw = `{"DiagnosticData":{"ValidationDate":"2026-10-25T00:00:00Z","Signature":[{"Id":"s","StructuralValidation":{"valid":true},"BasicSignature":{"SignatureIntact":true,"SignatureValid":true},"DigestMatcher":[{"DataFound":true,"DataIntact":true},{"DataFound":true,"DataIntact":true}]}]},"SimpleReport":{"ValidationTime":"2026-10-25T00:00:00Z","SignaturesCount":1,"ValidSignaturesCount":1,"signatureOrTimestampOrEvidenceRecord":[{"Signature":{"Id":"s","Indication":"TOTAL_PASSED","SubIndication":null,"BestSignatureTime":"2025-10-25T00:00:00Z","CertificateChain":{"Certificate":[{"Id":"C-ROOT","trusted":true}]}}}]},"DetailedReport":{"ValidationTime":"2026-10-25T00:00:00Z","signatureOrTimestampOrEvidenceRecord":[{"Signature":{"Id":"s"}}]}}` + want := dssExpectation{Indication: "TOTAL_PASSED", BestSignatureTime: proof} + for _, test := range []struct { + name string + from string + to string + }{ + {"complete report", "", ""}, + {"missing diagnostic", `"DiagnosticData"`, `"OtherData"`}, + {"wrong signature identity", `"Id":"s","Indication"`, `"Id":"other","Indication"`}, + {"unrelated indeterminate reason", `"TOTAL_PASSED"`, `"INDETERMINATE"`}, + {"missing proof time", `"2025-10-25T00:00:00Z"`, `null`}, + {"wrong proof time", `"2025-10-25T00:00:00Z"`, `"2025-10-24T00:00:00Z"`}, + {"wrong trusted anchor", `"Id":"C-ROOT"`, `"Id":"C-OTHER"`}, + {"untrusted anchor", `"trusted":true`, `"trusted":false`}, + {"signature invalid", `"SignatureValid":true`, `"SignatureValid":false`}, + {"missing signature validity", `"SignatureValid":true`, `"SignatureValid":null`}, + {"damaged reference", `"DataIntact":true`, `"DataIntact":false`}, + {"wrong explicit validation time", `"ValidationDate":"2026-10-25T00:00:00Z"`, `"ValidationDate":"2026-10-24T00:00:00Z"`}, + {"detailed report signature mismatch", `"Signature":{"Id":"s"}`, `"Signature":{"Id":"other"}`}, + {"detailed report time mismatch", `"DetailedReport":{"ValidationTime":"2026-10-25T00:00:00Z"`, `"DetailedReport":{"ValidationTime":"2026-10-24T00:00:00Z"`}, + {"no detailed report", `"DetailedReport":{"ValidationTime":"2026-10-25T00:00:00Z","signatureOrTimestampOrEvidenceRecord":[{"Signature":{"Id":"s"}}]}`, `"DetailedReport":null`}, + } { + t.Run(test.name, func(t *testing.T) { + value := raw + if test.from != "" { + value = strings.Replace(value, test.from, test.to, 1) + if value == raw { + t.Fatal("test mutation missed") + } + } + + var report dssReports + if err := json.Unmarshal([]byte(value), &report); err != nil { + t.Fatal(err) + } + + err := report.check(at, want, "C-ROOT") + if (err == nil) != (test.from == "") { + t.Fatal(err) + } + }) + } + + for _, endpoint := range []string{"https://127.0.0.1:8080/", "http://example.test/", "http://localhost/", "http://127.0.0.1/?query=1", "http://user@127.0.0.1/"} { + if _, err := dssLoopbackURL(endpoint); err == nil { + t.Fatal("nonlocal or ambiguous endpoint accepted:", endpoint) + } + } + + if _, err := dssLoopbackURL("http://127.0.0.1:8080/"); err != nil { + t.Fatal(err) + } + + t.Run("exact indeterminate cause with intact signature", func(t *testing.T) { + value := strings.NewReplacer(`"TOTAL_PASSED"`, `"INDETERMINATE"`, `"ValidSignaturesCount":1`, `"ValidSignaturesCount":0`, `"SubIndication":null`, `"SubIndication":"OUT_OF_BOUNDS_NO_POE"`, `"2025-10-25T00:00:00Z"`, `"2026-10-25T00:00:00Z"`).Replace(raw) + var report dssReports + if err := json.Unmarshal([]byte(value), &report); err != nil { + t.Fatal(err) + } + + want := dssExpectation{Indication: "INDETERMINATE", SubIndication: "OUT_OF_BOUNDS_NO_POE", BestSignatureTime: at} + if err := report.check(at, want, "C-ROOT"); err != nil { + t.Fatal(err) + } + + want.SubIndication = "NO_CERTIFICATE_CHAIN_FOUND" + if err := report.check(at, want, "C-ROOT"); err == nil { + t.Fatal("unrelated missing trust cause accepted as missing historical proof") + } + }) + + for _, metadata := range []string{"artifactId=dss-validation\nversion=6.5.1", "# generated\nversion=6.5.1\nartifactId=dss-validation\n", "artifactId=dss-validation\r\nversion=6.5.1\r\n"} { + if !dssPinnedMetadata([]byte(metadata), "6.5.1") { + t.Fatal("valid Maven runtime version metadata rejected") + } + } + for _, metadata := range []string{"artifactId=other\nversion=6.5.1\n", "artifactId=dss-validation\nversion=6.5.10\n", "artifactId=dss-validation\nversion=6.5\n"} { + if dssPinnedMetadata([]byte(metadata), "6.5.1") { + t.Fatal("different DSS implementation version accepted") + } + } +} + +func TestDSSArchiveRecoveryReportContracts(t *testing.T) { + const raw = `{"DetailedReport":{"signatureOrTimestampOrEvidenceRecord":[{"Signature":{"ValidationProcessBasicSignature":{"Conclusion":{"Indication":"INDETERMINATE"}},"ValidationProcessLongTermData":{"Conclusion":{"Indication":"INDETERMINATE"}},"ValidationProcessArchivalData":{"Conclusion":{"Indication":"PASSED"}},"Timestamp":[{"Id":"st","Conclusion":{"Indication":"PASSED"}},{"Id":"at","Conclusion":{"Indication":"PASSED"}}]}}]},"DiagnosticData":{"Timestamp":[{"Id":"st","Type":"SIGNATURE_TIMESTAMP","BasicSignature":{"SignatureIntact":true,"SignatureValid":true},"DigestMatcher":[{"DataFound":true,"DataIntact":true}]},{"Id":"at","Type":"ARCHIVE_TIMESTAMP","BasicSignature":{"SignatureIntact":true,"SignatureValid":true},"DigestMatcher":[{"DataFound":true,"DataIntact":true}]}]}}` + for _, test := range []struct { + name string + from string + to string + }{ + {"archive required and verified", "", ""}, + {"current signature passed instead", `"ValidationProcessBasicSignature":{"Conclusion":{"Indication":"INDETERMINATE"}}`, `"ValidationProcessBasicSignature":{"Conclusion":{"Indication":"PASSED"}}`}, + {"LT passed instead", `"ValidationProcessLongTermData":{"Conclusion":{"Indication":"INDETERMINATE"}}`, `"ValidationProcessLongTermData":{"Conclusion":{"Indication":"PASSED"}}`}, + {"archive process did not pass", `"ValidationProcessArchivalData":{"Conclusion":{"Indication":"PASSED"}}`, `"ValidationProcessArchivalData":{"Conclusion":{"Indication":"INDETERMINATE"}}`}, + {"missing archive timestamp", `"ARCHIVE_TIMESTAMP"`, `"OTHER_TIMESTAMP"`}, + {"timestamp signature invalid", `"SignatureValid":true`, `"SignatureValid":false`}, + {"timestamp imprint invalid", `"DataIntact":true`, `"DataIntact":false`}, + {"missing detailed report", `"DetailedReport"`, `"OtherReport"`}, + {"old timestamp ignored", `"Id":"st","Conclusion":{"Indication":"PASSED"}`, `"Id":"st","Conclusion":{"Indication":"INDETERMINATE"}`}, + {"missing old timestamp trust result", `{"Id":"st","Conclusion":{"Indication":"PASSED"}},`, ``}, + {"duplicate timestamp trust identity", `"Id":"at","Conclusion"`, `"Id":"st","Conclusion"`}, + {"different timestamp trust identity", `"Id":"at","Conclusion"`, `"Id":"other","Conclusion"`}, + } { + t.Run(test.name, func(t *testing.T) { + value := raw + if test.from != "" { + value = strings.Replace(value, test.from, test.to, 1) + if value == raw { + t.Fatal("test mutation missed") + } + } + var report dssReports + if err := json.Unmarshal([]byte(value), &report); err != nil { + t.Fatal(err) + } + if err := report.checkArchiveRecovery(true); (err == nil) != (test.from == "") { + t.Fatal(err) + } + }) + } +} + +func referenceInput(t *testing.T, fixture *interopFixture, d *tree.Document, sig, ref *tree.Node, attachment []byte) []byte { + t.Helper() + + uri := attr(ref, "URI") + + var ( + target *tree.Node + octets []byte + ) + if uri == uriAttachment { + octets = bytes.Clone(attachment) + } else if uri == "" { + target = tree.Root(d) + } else if strings.HasPrefix(uri, "#") { + ident := strings.TrimPrefix(uri, "#") + + var walk func(*tree.Node) + + walk = func(n *tree.Node) { testreferenceInputCase(n, &ident, &target, &t, &walk) } + walk(tree.Root(d)) + + if target == nil { + t.Fatalf("unresolved ID %s", uri) + } + } else { + t.Fatalf("unallowlisted test URI %s", uri) + } + + var canonical string + + enveloped := false + + for _, tr := range nodes(ref, ds, "Transform") { + alg := attr(tr, "Algorithm") + if alg == ds+"enveloped-signature" { + enveloped = true + } else if alg == ds+"base64" { + if target != nil { + octets = []byte(text(target)) + target = nil + } + + octets = decode64(t, string(octets)) + } else if strings.HasPrefix(alg, exc) || strings.HasPrefix(alg, c10) || strings.HasPrefix(alg, c11) { + canonical = alg + } else { + t.Fatalf("unsupported fixed profile transform %s", alg) + } + } + + if target == nil { + if canonical != "" { + doc := xmlDoc(t, octets) + return externalCanonical(t, fixture, doc, nil, canonical) + } + + return octets + } + + if canonical == "" { + canonical = c10 + } + // Bare ID references strip comments before transforms, even WithComments. + removed := make(map[*tree.Node][]*tree.Node) + + var prune func(*tree.Node) + + prune = func(n *tree.Node) { testreferenceInputCase2(n, &sig, &enveloped, &prune, &removed) } + prune(target) + + defer func() { + for n, children := range removed { + n.Children = children + } + }() + + return externalCanonical(t, fixture, d, target, canonical) +} + +func checkReferences(t *testing.T, fixture *interopFixture, d *tree.Document, sig *tree.Node) bool { + t.Helper() + + valid := true + + for _, ref := range nodes(sig, ds, "Reference") { + alg := attr(one(t, ref, ds, "DigestMethod"), "Algorithm") + + bits := 256 + if strings.HasSuffix(alg, "384") { + bits = 384 + } else if strings.HasSuffix(alg, "512") { + bits = 512 + } + + input := referenceInput(t, fixture, d, sig, ref, nil) + + digest := hashBytes(bits, input) + if !bytes.Equal(digest, decode64(t, text(one(t, ref, ds, "DigestValue")))) { + valid = false + } + } + + return valid +} + +func testreferenceInputCase(n *tree.Node, ident *string, target **tree.Node, t **testing.T, walk *func(*tree.Node)) { + if attr(n, "Id") == (*ident) || attr(n, "ID") == (*ident) { + if (*target) != nil { + (*t).Fatal("duplicate ID") + } + + (*target) = n + } + + for _, c := range n.Children { + (*walk)(c) + } +} + +func testreferenceInputCase2(n *tree.Node, sig **tree.Node, enveloped *bool, prune *func(*tree.Node), removed *map[*tree.Node][]*tree.Node) { + original := n.Children + + var kept []*tree.Node + + for _, c := range original { + if c == (*sig) && (*enveloped) || c.Kind == tree.CommentNode { + continue + } + + kept = append(kept, c) + (*prune)(c) } + + (*removed)[n] = original + n.Children = kept } // DSS is a separately started, offline demonstration runtime. Only its loopback @@ -1051,96 +1274,6 @@ func (r *dssReports) checkArchiveRecovery(success bool) error { return nil } -func TestDSSHistoricalValidation(t *testing.T) { - selected(t, "portable") - - manifestPath := os.Getenv("XMLSEC_DSS_FIXTURES") - endpoint := os.Getenv("XMLSEC_DSS_URL") - runtimeJar := os.Getenv("XMLSEC_DSS_RUNTIME_JAR") - if manifestPath == "" || endpoint == "" || runtimeJar == "" { - if os.Getenv("XMLSEC_REQUIRE_P7_PROOFS") == "1" { - t.Fatal("XMLSEC_DSS_FIXTURES, XMLSEC_DSS_URL and XMLSEC_DSS_RUNTIME_JAR are required") - } - - t.Skip("optional local DSS historical compatibility fixtures/runtime") - } - - base, err := dssLoopbackURL(endpoint) - if err != nil { - t.Fatal(err) - } - - var manifest dssHistoricalManifest - decoder := json.NewDecoder(bytes.NewReader(dssReadFile(t, filepath.Dir(manifestPath), filepath.Base(manifestPath), 256<<10))) - decoder.DisallowUnknownFields() - if err = decoder.Decode(&manifest); err != nil { - t.Fatal(err) - } - if err = decoder.Decode(new(any)); !errors.Is(err, io.EOF) { - t.Fatal("trailing DSS manifest data:", err) - } - - if manifest.Version != "6.5.1" || manifest.ValidationTime.IsZero() || len(manifest.Pairs) == 0 || len(manifest.Pairs) > 16 { - t.Fatal("invalid DSS fixture version, explicit validation time or bounded pair count") - } - anchor, err := hex.DecodeString(manifest.TrustAnchorSHA256) - if err != nil || len(anchor) != sha256.Size { - t.Fatal("missing exact DSS trust-anchor certificate SHA-256") - } - anchorID := "C-" + strings.ToUpper(manifest.TrustAnchorSHA256) - - client := &http.Client{Timeout: time.Minute, Transport: &http.Transport{Proxy: nil}, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }} - t.Cleanup(client.CloseIdleConnections) - dssCheckRuntime(t, client, base, runtimeJar, manifest.Version) - - directory := filepath.Dir(manifestPath) - var policy *dssRemoteDocument - if manifest.Policy != "" { - policy = &dssRemoteDocument{Bytes: dssReadFile(t, directory, manifest.Policy, 1<<20), Name: filepath.Base(manifest.Policy)} - } - - seen := map[string]struct{}{} - for _, pair := range manifest.Pairs { - if _, exists := seen[pair.Name]; pair.Name == "" || exists { - t.Fatal("empty or duplicate historical pair name") - } - - seen[pair.Name] = struct{}{} - t.Run(pair.Name, func(t *testing.T) { - sufficient := dssReadFile(t, directory, pair.Sufficient, 16<<20) - insufficient := dssReadFile(t, directory, pair.Insufficient, 16<<20) - dssCheckSameSignature(t, sufficient, insufficient) - if pair.WantSufficient.Indication != "TOTAL_PASSED" || pair.WantSufficient.SubIndication != "" || pair.WantInsufficient.Indication != "INDETERMINATE" || pair.WantInsufficient.SubIndication == "" { - t.Fatal("pair must specify total success and one exact indeterminate reason") - } - - for _, item := range []struct { - name string - raw []byte - want dssExpectation - }{{"sufficient", sufficient, pair.WantSufficient}, {"insufficient", insufficient, pair.WantInsufficient}} { - t.Run(item.name, func(t *testing.T) { - raw := dssPostValidation(t, client, base, dssValidationRequest{SignedDocument: dssRemoteDocument{Bytes: item.raw, Name: pair.Name + ".xml"}, Policy: policy, ValidationTime: manifest.ValidationTime}) - dssSaveReport(t, item.raw, raw) - var report dssReports - if err := json.Unmarshal(raw, &report); err != nil { - t.Fatal(err) - } - - if err := report.check(manifest.ValidationTime, item.want, anchorID); err != nil { - t.Fatalf("DSS verdict: %v; summary=%+v\n%.4096s", err, report.SimpleReport, raw) - } - if err := report.checkArchiveRecovery(item.want.Indication == "TOTAL_PASSED"); err != nil { - t.Fatal("DSS archive workflow:", err) - } - - t.Logf("DSS %s %s/%s input-sha256=%x best-signature-time=%s", manifest.Version, item.want.Indication, item.want.SubIndication, sha256.Sum256(item.raw), item.want.BestSignatureTime.Format(time.RFC3339Nano)) - }) - } - }) - } -} - func dssSaveReport(t *testing.T, input, report []byte) { t.Helper() directory := os.Getenv("XMLSEC_DSS_REPORT_DIR") @@ -1325,129 +1458,3 @@ func dssCheckSameSignature(t *testing.T, left, right []byte) { } func dssTrue(value *bool) bool { return value != nil && *value } - -func TestDSSHistoricalReportContracts(t *testing.T) { - at := time.Date(2026, 10, 25, 0, 0, 0, 0, time.UTC) - proof := at.AddDate(-1, 0, 0) - const raw = `{"DiagnosticData":{"ValidationDate":"2026-10-25T00:00:00Z","Signature":[{"Id":"s","StructuralValidation":{"valid":true},"BasicSignature":{"SignatureIntact":true,"SignatureValid":true},"DigestMatcher":[{"DataFound":true,"DataIntact":true},{"DataFound":true,"DataIntact":true}]}]},"SimpleReport":{"ValidationTime":"2026-10-25T00:00:00Z","SignaturesCount":1,"ValidSignaturesCount":1,"signatureOrTimestampOrEvidenceRecord":[{"Signature":{"Id":"s","Indication":"TOTAL_PASSED","SubIndication":null,"BestSignatureTime":"2025-10-25T00:00:00Z","CertificateChain":{"Certificate":[{"Id":"C-ROOT","trusted":true}]}}}]},"DetailedReport":{"ValidationTime":"2026-10-25T00:00:00Z","signatureOrTimestampOrEvidenceRecord":[{"Signature":{"Id":"s"}}]}}` - want := dssExpectation{Indication: "TOTAL_PASSED", BestSignatureTime: proof} - for _, test := range []struct { - name string - from string - to string - }{ - {"complete report", "", ""}, - {"missing diagnostic", `"DiagnosticData"`, `"OtherData"`}, - {"wrong signature identity", `"Id":"s","Indication"`, `"Id":"other","Indication"`}, - {"unrelated indeterminate reason", `"TOTAL_PASSED"`, `"INDETERMINATE"`}, - {"missing proof time", `"2025-10-25T00:00:00Z"`, `null`}, - {"wrong proof time", `"2025-10-25T00:00:00Z"`, `"2025-10-24T00:00:00Z"`}, - {"wrong trusted anchor", `"Id":"C-ROOT"`, `"Id":"C-OTHER"`}, - {"untrusted anchor", `"trusted":true`, `"trusted":false`}, - {"signature invalid", `"SignatureValid":true`, `"SignatureValid":false`}, - {"missing signature validity", `"SignatureValid":true`, `"SignatureValid":null`}, - {"damaged reference", `"DataIntact":true`, `"DataIntact":false`}, - {"wrong explicit validation time", `"ValidationDate":"2026-10-25T00:00:00Z"`, `"ValidationDate":"2026-10-24T00:00:00Z"`}, - {"detailed report signature mismatch", `"Signature":{"Id":"s"}`, `"Signature":{"Id":"other"}`}, - {"detailed report time mismatch", `"DetailedReport":{"ValidationTime":"2026-10-25T00:00:00Z"`, `"DetailedReport":{"ValidationTime":"2026-10-24T00:00:00Z"`}, - {"no detailed report", `"DetailedReport":{"ValidationTime":"2026-10-25T00:00:00Z","signatureOrTimestampOrEvidenceRecord":[{"Signature":{"Id":"s"}}]}`, `"DetailedReport":null`}, - } { - t.Run(test.name, func(t *testing.T) { - value := raw - if test.from != "" { - value = strings.Replace(value, test.from, test.to, 1) - if value == raw { - t.Fatal("test mutation missed") - } - } - - var report dssReports - if err := json.Unmarshal([]byte(value), &report); err != nil { - t.Fatal(err) - } - - err := report.check(at, want, "C-ROOT") - if (err == nil) != (test.from == "") { - t.Fatal(err) - } - }) - } - - for _, endpoint := range []string{"https://127.0.0.1:8080/", "http://example.test/", "http://localhost/", "http://127.0.0.1/?query=1", "http://user@127.0.0.1/"} { - if _, err := dssLoopbackURL(endpoint); err == nil { - t.Fatal("nonlocal or ambiguous endpoint accepted:", endpoint) - } - } - - if _, err := dssLoopbackURL("http://127.0.0.1:8080/"); err != nil { - t.Fatal(err) - } - - t.Run("exact indeterminate cause with intact signature", func(t *testing.T) { - value := strings.NewReplacer(`"TOTAL_PASSED"`, `"INDETERMINATE"`, `"ValidSignaturesCount":1`, `"ValidSignaturesCount":0`, `"SubIndication":null`, `"SubIndication":"OUT_OF_BOUNDS_NO_POE"`, `"2025-10-25T00:00:00Z"`, `"2026-10-25T00:00:00Z"`).Replace(raw) - var report dssReports - if err := json.Unmarshal([]byte(value), &report); err != nil { - t.Fatal(err) - } - - want := dssExpectation{Indication: "INDETERMINATE", SubIndication: "OUT_OF_BOUNDS_NO_POE", BestSignatureTime: at} - if err := report.check(at, want, "C-ROOT"); err != nil { - t.Fatal(err) - } - - want.SubIndication = "NO_CERTIFICATE_CHAIN_FOUND" - if err := report.check(at, want, "C-ROOT"); err == nil { - t.Fatal("unrelated missing trust cause accepted as missing historical proof") - } - }) - - for _, metadata := range []string{"artifactId=dss-validation\nversion=6.5.1", "# generated\nversion=6.5.1\nartifactId=dss-validation\n", "artifactId=dss-validation\r\nversion=6.5.1\r\n"} { - if !dssPinnedMetadata([]byte(metadata), "6.5.1") { - t.Fatal("valid Maven runtime version metadata rejected") - } - } - for _, metadata := range []string{"artifactId=other\nversion=6.5.1\n", "artifactId=dss-validation\nversion=6.5.10\n", "artifactId=dss-validation\nversion=6.5\n"} { - if dssPinnedMetadata([]byte(metadata), "6.5.1") { - t.Fatal("different DSS implementation version accepted") - } - } -} - -func TestDSSArchiveRecoveryReportContracts(t *testing.T) { - const raw = `{"DetailedReport":{"signatureOrTimestampOrEvidenceRecord":[{"Signature":{"ValidationProcessBasicSignature":{"Conclusion":{"Indication":"INDETERMINATE"}},"ValidationProcessLongTermData":{"Conclusion":{"Indication":"INDETERMINATE"}},"ValidationProcessArchivalData":{"Conclusion":{"Indication":"PASSED"}},"Timestamp":[{"Id":"st","Conclusion":{"Indication":"PASSED"}},{"Id":"at","Conclusion":{"Indication":"PASSED"}}]}}]},"DiagnosticData":{"Timestamp":[{"Id":"st","Type":"SIGNATURE_TIMESTAMP","BasicSignature":{"SignatureIntact":true,"SignatureValid":true},"DigestMatcher":[{"DataFound":true,"DataIntact":true}]},{"Id":"at","Type":"ARCHIVE_TIMESTAMP","BasicSignature":{"SignatureIntact":true,"SignatureValid":true},"DigestMatcher":[{"DataFound":true,"DataIntact":true}]}]}}` - for _, test := range []struct { - name string - from string - to string - }{ - {"archive required and verified", "", ""}, - {"current signature passed instead", `"ValidationProcessBasicSignature":{"Conclusion":{"Indication":"INDETERMINATE"}}`, `"ValidationProcessBasicSignature":{"Conclusion":{"Indication":"PASSED"}}`}, - {"LT passed instead", `"ValidationProcessLongTermData":{"Conclusion":{"Indication":"INDETERMINATE"}}`, `"ValidationProcessLongTermData":{"Conclusion":{"Indication":"PASSED"}}`}, - {"archive process did not pass", `"ValidationProcessArchivalData":{"Conclusion":{"Indication":"PASSED"}}`, `"ValidationProcessArchivalData":{"Conclusion":{"Indication":"INDETERMINATE"}}`}, - {"missing archive timestamp", `"ARCHIVE_TIMESTAMP"`, `"OTHER_TIMESTAMP"`}, - {"timestamp signature invalid", `"SignatureValid":true`, `"SignatureValid":false`}, - {"timestamp imprint invalid", `"DataIntact":true`, `"DataIntact":false`}, - {"missing detailed report", `"DetailedReport"`, `"OtherReport"`}, - {"old timestamp ignored", `"Id":"st","Conclusion":{"Indication":"PASSED"}`, `"Id":"st","Conclusion":{"Indication":"INDETERMINATE"}`}, - {"missing old timestamp trust result", `{"Id":"st","Conclusion":{"Indication":"PASSED"}},`, ``}, - {"duplicate timestamp trust identity", `"Id":"at","Conclusion"`, `"Id":"st","Conclusion"`}, - {"different timestamp trust identity", `"Id":"at","Conclusion"`, `"Id":"other","Conclusion"`}, - } { - t.Run(test.name, func(t *testing.T) { - value := raw - if test.from != "" { - value = strings.Replace(value, test.from, test.to, 1) - if value == raw { - t.Fatal("test mutation missed") - } - } - var report dssReports - if err := json.Unmarshal([]byte(value), &report); err != nil { - t.Fatal(err) - } - if err := report.checkArchiveRecovery(true); (err == nil) != (test.from == "") { - t.Fatal(err) - } - }) - } -} diff --git a/tests/interop/saml_authority_http_test.go b/tests/interop/saml_authority_http_test.go new file mode 100644 index 0000000..5bb2eec --- /dev/null +++ b/tests/interop/saml_authority_http_test.go @@ -0,0 +1,334 @@ +//go:build interop + +package interop_test + +import ( + "bytes" + "context" + "crypto" + "crypto/tls" + "crypto/x509" + "encoding/pem" + "errors" + "fmt" + "io" + "net" + "net/http" + "os" + "os/exec" + "path/filepath" + "strings" + "sync/atomic" + "testing" + "time" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/saml/authority" + "github.com/skarm/xmlsec/saml/binding" + "github.com/skarm/xmlsec/saml/metadata" + "github.com/skarm/xmlsec/saml/protocol" + "github.com/skarm/xmlsec/saml/replay" + "github.com/skarm/xmlsec/xmlenc" +) + +const ( + shibbolethHTTPTimeout = 10 * time.Second + shibbolethProcessWaitDelay = 5 * time.Second + shibbolethStartupTimeout = 90 * time.Second + shibbolethStartupPollInterval = 250 * time.Millisecond + shibbolethLogExcerptBytes = 18_000 + shibbolethStatusBytes = 65_536 + shibbolethStatusLogRunes = 200 + authorityRecordedBodyBytes = 3 << 20 + shibbolethHeapFlag = "-Xmx768m" +) + +const ( + shibbolethEntity = "https://authority.test/entity" + shibbolethRequester = "https://requester.test/entity" +) + +// This runs the released, unmodified IdP WAR in Jetty. Request decoding, peer +// authentication, resolution, filtering, encryption and signing are all upstream +// Shibboleth code, with only deployment configuration supplied by this test. +func TestShibbolethAuthorityHTTP(t *testing.T) { + selected(t, "shibboleth") + dist, jetty := os.Getenv("SHIBBOLETH_DIST"), os.Getenv("JETTY_HOME") + if dist == "" || jetty == "" { + t.Fatal("SHIBBOLETH_DIST and JETTY_HOME are required for the selected deployed Authority test") + } + f := newInteropFixture(t) + key := generateKey(t, f.work, nameRSASHA256) + home := installShibboleth(t, f, dist, key) + endpoint, httpClient := startShibboleth(t, f, home, jetty) + recorder := &authorityHTTPRecorder{base: httpClient.Transport, directory: t.ArtifactDir()} + httpClient.Transport = recorder + block, _ := pem.Decode(read(t, filepath.Join(home, "credentials/idp-signing.crt"))) + if block == nil { + t.Fatal("missing upstream signing certificate") + } + at := time.Now().UTC() + md, err := metadata.Build(t.Context(), metadata.EntityDescriptor{EntityID: shibbolethEntity, ValidUntil: at.Add(time.Hour), AttributeAuthority: &metadata.RoleDescriptor{Keys: []metadata.KeyDescriptor{{CertificateDER: block.Bytes}}, Endpoints: map[string][]metadata.Endpoint{metadata.AttributeService: {{Binding: metadata.SOAP, Location: endpoint}}}}}, at, metadata.Options{}) + if err != nil { + t.Fatal(err) + } + snapshot, err := metadata.ParseTrusted(t.Context(), xmlBytes(t, md), at, metadata.Options{}) + if err != nil { + t.Fatal(err) + } + store, _ := replay.NewMemoryStore(replay.Config{}) + c := authority.Client{EntityID: shibbolethRequester, Peer: shibbolethEntity, Role: metadata.AttributeAuthority, Metadata: snapshot, Signing: protocol.SigningKey{Key: key.signer, CertificateDER: key.cert.Raw}, Replay: store, SOAP: binding.SOAPClient{Client: httpClient}, Options: protocol.QueryResponseOptions{ExchangeOptions: protocol.ExchangeOptions{Decryption: &protocol.DecryptionOptions{Keys: []crypto.Decrypter{key.signer.(crypto.Decrypter)}, Data: xmlenc.Options{DataAlgorithm: xmlenc.AES128GCM}}}}} + subject, err := protocol.NewSubject(t.Context(), protocol.NameIdentifier{Value: "alice", Format: protocol.NameIDUnspecified}) + if err != nil { + t.Fatal(err) + } + filter, err := document.Parse(t.Context(), []byte(``), document.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + q := protocol.Query{MessageHeader: protocol.MessageHeader{ID: "_live_query"}, Kind: protocol.AttributeQuery, Subject: subject, Attributes: []*document.Document{filter}} + t.Run("signed-encrypted-attribute-query", func(t *testing.T) { + r, err := c.Query(t.Context(), q, time.Now().UTC()) + if err != nil { + t.Fatal(err) + } + if r.Status().Code != protocol.Success || len(r.Assertions()) != 1 { + t.Fatalf("independent Authority status: %+v, assertions=%d", r.Status(), len(r.Assertions())) + } + message, err := binding.DecodeSOAP(t.Context(), read(t, filepath.Join(recorder.directory, "authority-response-1.xml")), binding.SOAPOptions{}) + if err != nil { + t.Fatal(err) + } + payload, err := message.Payload(t.Context()) + if err != nil { + t.Fatal(err) + } + signed, encrypted := false, false + for _, child := range payload.Root().Children() { + signed = signed || child.Name() == (document.Name{Space: ds, Local: "Signature"}) + encrypted = encrypted || child.Name() == (document.Name{Space: protocol.AssertionNamespace, Local: "EncryptedAssertion"}) + } + if !signed || !encrypted { + t.Fatal("wire response lacks signed envelope or encrypted assertion") + } + statements := r.Assertions()[0].Statements() + if len(statements) != 1 || len(statements[0].Children()) != 1 || statements[0].Children()[0].Children()[0].Text() != "alice@example.org" { + t.Fatal("incorrect independent resolution or query filter") + } + }) + t.Run("replay-rejected-by-authority", func(t *testing.T) { + fresh := c + fresh.Replay, _ = replay.NewMemoryStore(replay.Config{}) + r, err := fresh.Query(t.Context(), q, time.Now().UTC()) + checkAuthorityDenial(t, recorder, q.ID, r, err) + }) + t.Run("untrusted-signing-key", func(t *testing.T) { + other := generateKey(t, t.TempDir(), nameRSASHA256) + bad := c + bad.Signing = protocol.SigningKey{Key: other.signer} + query := q + query.ID = "_untrusted_query" + r, err := bad.Query(t.Context(), query, time.Now().UTC()) + checkAuthorityDenial(t, recorder, query.ID, r, err) + }) +} + +func checkAuthorityDenial(t *testing.T, recorder *authorityHTTPRecorder, requestID string, result *protocol.VerifiedQueryResponse, err error) { + t.Helper() + // The early Shibboleth security failures have no outbound signing parameters. + // Their wire status is authenticated by the verified TLS connection. The Go + // message-signature profile must continue to reject an unsigned envelope. + if err != nil && (!errors.Is(err, protocol.ErrSignature) || result != nil) { + t.Fatalf("unexpected client failure: %v", err) + } + if err == nil && (result.Status().Code != protocol.Requester || len(result.Assertions()) != 0) { + t.Fatal("client accepted a rejected Authority request") + } + path := filepath.Join(recorder.directory, fmt.Sprintf("authority-response-%d.xml", recorder.sequence.Load())) + message, decodeErr := binding.DecodeSOAP(t.Context(), read(t, path), binding.SOAPOptions{}) + if decodeErr != nil { + t.Fatal(decodeErr) + } + payload, decodeErr := message.Payload(t.Context()) + if decodeErr != nil { + t.Fatal(decodeErr) + } + if payload.Root().Name() != (document.Name{Space: protocol.ProtocolNamespace, Local: "Response"}) { + t.Fatal("unexpected rejection message type") + } + id, _ := payload.Root().Attribute(document.Name{Local: "InResponseTo"}) + if id != requestID { + t.Fatal("uncorrelated wire rejection") + } + found, issuer := false, false + for _, child := range payload.Root().Children() { + issuer = issuer || child.Name() == (document.Name{Space: protocol.AssertionNamespace, Local: "Issuer"}) && child.Text() == shibbolethEntity + if child.Name() == (document.Name{Space: protocol.ProtocolNamespace, Local: "Status"}) { + codes := child.Children() + if len(codes) > 0 { + code, _ := codes[0].Attribute(document.Name{Local: "Value"}) + inner := codes[0].Children() + if code == protocol.Requester && len(inner) == 1 { + subcode, _ := inner[0].Attribute(document.Name{Local: "Value"}) + found = subcode == "urn:oasis:names:tc:SAML:2.0:status:RequestDenied" + } + } + } + if child.Name().Space == protocol.AssertionNamespace && child.Name().Local != "Issuer" { + t.Fatal("failure response disclosed an assertion") + } + } + if !found || !issuer { + t.Fatal("Authority did not report Requester/RequestDenied over authenticated HTTPS") + } +} + +type authorityHTTPRecorder struct { + base http.RoundTripper + directory string + sequence atomic.Uint64 +} + +func (r *authorityHTTPRecorder) RoundTrip(request *http.Request) (*http.Response, error) { + response, err := r.base.RoundTrip(request) + if err != nil { + return nil, err + } + raw, err := io.ReadAll(io.LimitReader(response.Body, authorityRecordedBodyBytes)) + closeErr := response.Body.Close() + if err != nil { + return nil, err + } + if closeErr != nil { + return nil, closeErr + } + if err = os.WriteFile(filepath.Join(r.directory, fmt.Sprintf("authority-response-%d.xml", r.sequence.Add(1))), raw, fixtureFileMode); err != nil { + return nil, err + } + response.Body = io.NopCloser(bytes.NewReader(raw)) + return response, nil +} + +func installShibboleth(t *testing.T, f *interopFixture, dist string, key testKey) string { + t.Helper() + home := filepath.Join(f.work, "idp") + props := write(t, filepath.Join(f.work, "install.properties"), []byte("idp.scope=example.test\n")) + out, stderr := command(t, f, true, nil, "bash", filepath.Join(dist, "bin/install.sh"), "--noPrompt", "--propertyFile", props, "--targetDir", home, "--hostName", "localhost", "--entityID", shibbolethEntity, "--keystorePassword", "xmlsec-interop-only", "--sealerPassword", "xmlsec-interop-only") + if !strings.Contains(string(out)+string(stderr), "Version: 5.2.3") { + t.Fatal("Shibboleth 5.2.3 installer version was not observed") + } + md, err := metadata.Build(t.Context(), metadata.EntityDescriptor{EntityID: shibbolethRequester, ValidUntil: time.Now().UTC().Add(time.Hour), SP: &metadata.RoleDescriptor{WantAssertionsSigned: true, Keys: []metadata.KeyDescriptor{{CertificateDER: key.cert.Raw}}, Endpoints: map[string][]metadata.Endpoint{metadata.AssertionConsumerService: {{Binding: metadata.HTTPPost, Location: "https://requester.test/acs", Index: 0}}}}}, time.Now().UTC(), metadata.Options{}) + if err != nil { + t.Fatal(err) + } + write(t, filepath.Join(home, "metadata/requester.xml"), xmlBytes(t, md)) + conf := filepath.Join(home, "conf") + write(t, filepath.Join(conf, "metadata-providers.xml"), []byte(``)) + beans := `` + write(t, filepath.Join(conf, "relying-party.xml"), []byte(beans+`http://www.w3.org/2009/xmlenc11#rsa-oaep`)) + path := filepath.Join(conf, "c14n/subject-c14n.xml") + text := string(read(t, path)) + old := `` + if strings.Count(text, old) != 1 { + t.Fatal("upstream NameID consumption configuration changed") + } + write(t, path, []byte(strings.Replace(text, old, ""+shibbolethRequester+"", 1))) + write(t, filepath.Join(conf, "attribute-filter.xml"), []byte(``)) + return home +} + +func startShibboleth(t *testing.T, f *interopFixture, home, jetty string) (string, *http.Client) { + t.Helper() + base := filepath.Join(f.work, "jetty-base") + if err := os.MkdirAll(base, fixtureDirectoryMode); err != nil { + t.Fatal(err) + } + command(t, f, true, nil, "java", "-Djetty.base="+base, "-jar", filepath.Join(jetty, "start.jar"), "--add-modules=ee9-deploy,ee9-annotations,ee9-jstl,ssl,https", "--approve-all-licenses") + cert, key, keystore := filepath.Join(base, "tls.crt"), filepath.Join(base, "tls.key"), filepath.Join(base, "tls.p12") + command(t, f, true, nil, "openssl", "req", "-new", "-x509", "-newkey", "rsa:2048", "-noenc", "-days", "1", "-subj", "/CN=127.0.0.1", "-addext", "subjectAltName=IP:127.0.0.1", "-keyout", key, "-out", cert) + command(t, f, true, nil, "openssl", "pkcs12", "-export", "-inkey", key, "-in", cert, "-out", keystore, "-passout", "pass:xmlsec-interop-only") + write(t, filepath.Join(base, "webapps/idp.xml"), []byte(``+home+`/war/idp.war/idpfalsetrue`)) + listener, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + address := listener.Addr().String() + _, port, _ := net.SplitHostPort(address) + if err = listener.Close(); err != nil { + t.Fatal(err) + } + pool := x509.NewCertPool() + if !pool.AppendCertsFromPEM(read(t, cert)) { + t.Fatal("invalid local TLS certificate") + } + transport := &http.Transport{TLSClientConfig: &tls.Config{RootCAs: pool, MinVersion: tls.VersionTLS12}} + t.Cleanup(transport.CloseIdleConnections) + client := &http.Client{Transport: transport, Timeout: shibbolethHTTPTimeout} + ctx, cancel := context.WithCancel(t.Context()) + logPath := filepath.Join(t.ArtifactDir(), "shibboleth.log") + log, err := os.Create(logPath) + if err != nil { + cancel() + t.Fatal(err) + } + cmd := exec.CommandContext(ctx, "java", shibbolethHeapFlag, "-Didp.home="+home, "-Djetty.base="+base, "-jar", filepath.Join(jetty, "start.jar"), "jetty.ssl.host=127.0.0.1", "jetty.ssl.port="+port, "jetty.sslContext.keyStorePath="+keystore, "jetty.sslContext.keyStorePassword=xmlsec-interop-only", "jetty.sslContext.keyManagerPassword=xmlsec-interop-only") + cmd.Stdout, cmd.Stderr = log, log + cmd.WaitDelay = shibbolethProcessWaitDelay + if err = cmd.Start(); err != nil { + cancel() + _ = log.Close() + t.Fatal(err) + } + done := make(chan error, 1) + go func() { done <- cmd.Wait() }() + t.Cleanup(func() { + cancel() + <-done + _ = log.Close() + if t.Failed() { + for _, path := range []string{logPath, filepath.Join(home, "logs/idp-process.log")} { + text, err := os.ReadFile(path) + if err != nil { + continue + } + if len(text) > shibbolethLogExcerptBytes { + text = text[len(text)-shibbolethLogExcerptBytes:] + } + t.Log(string(text)) + } + } + }) + deadline := time.NewTimer(shibbolethStartupTimeout) + defer deadline.Stop() + tick := time.NewTicker(shibbolethStartupPollInterval) + defer tick.Stop() + for { + select { + case <-ctx.Done(): + t.Fatal(ctx.Err()) + case <-deadline.C: + t.Fatal("Shibboleth startup timed out") + case err := <-done: + done <- err + t.Fatalf("Shibboleth exited during startup: %v", err) + case <-tick.C: + request, _ := http.NewRequestWithContext(ctx, http.MethodGet, "https://"+address+"/idp/status", nil) + response, err := client.Do(request) + if err != nil { + continue + } + raw, err := io.ReadAll(io.LimitReader(response.Body, shibbolethStatusBytes)) + _ = response.Body.Close() + if response.StatusCode != http.StatusOK && len(raw) > 0 { + t.Logf("startup probe HTTP %d: %.*s", response.StatusCode, shibbolethStatusLogRunes, raw) + } + if err == nil && response.StatusCode == http.StatusOK { + if !strings.Contains(string(raw), "5.2.3") { + t.Fatalf("unexpected deployed IdP version: %s", raw) + } + t.Log("deployed Shibboleth Identity Provider 5.2.3 over HTTPS") + return "https://" + address + "/idp/profile/SAML2/SOAP/AttributeQuery", client + } + } + } +} diff --git a/tests/interop/saml_authority_test.go b/tests/interop/saml_authority_test.go new file mode 100644 index 0000000..78bbc63 --- /dev/null +++ b/tests/interop/saml_authority_test.go @@ -0,0 +1,134 @@ +//go:build interop + +package interop_test + +import ( + "crypto" + "os" + "path/filepath" + "testing" + "time" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/saml/metadata" + "github.com/skarm/xmlsec/saml/protocol" +) + +func TestOpenSAMLAuthorityContracts(t *testing.T) { + selected(t, "opensaml") + f := newInteropFixture(t) + _ = openSAMLOracle(t, f) + cp := f.work + string(os.PathListSeparator) + os.Getenv("OPENSAML_CLASSPATH") + command(t, f, true, nil, "javac", "-cp", cp, "-d", f.work, filepath.Join(f.root, "tests/interop/testdata/OpenSAMLAuthorityOracle.java")) + oracle := func(t *testing.T, args ...string) { + t.Helper() + command(t, f, true, nil, "java", append([]string{"-Dlogback.configurationFile=" + filepath.Join(f.work, "oracle-logback.xml"), "-cp", cp, "OpenSAMLAuthorityOracle"}, args...)...) + } + key := generateKey(t, f.work, nameRSASHA256) + signer := protocol.SigningKey{Key: key.signer} + keys := []crypto.PublicKey{key.signer.Public()} + at := time.Now().UTC() + const requester, authority, endpoint = "https://requester.test/entity", "https://authority.test/entity", "https://authority.test/soap" + for _, kind := range []string{"AuthnQuery", "AttributeQuery", "AuthzDecisionQuery", "AssertionIDRequest", "NameIDMappingRequest"} { + t.Run(kind, func(t *testing.T) { + queryPath := filepath.Join(f.work, "independent-query.xml") + oracle(t, "query", kind, key.private, key.certificate, queryPath, requester, endpoint, at.Format(time.RFC3339Nano)) + d, err := document.Parse(t.Context(), read(t, queryPath), document.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + o := protocol.ExchangeOptions{Issuer: requester, Destination: endpoint, At: at, Binding: metadata.SOAP} + var q protocol.Query + var mapping protocol.NameIDMappingRequest + if kind == "NameIDMappingRequest" { + v, err := protocol.VerifyNameIDMappingRequest(t.Context(), d, keys, o) + if err != nil { + t.Fatal(err) + } + mapping = v.Request() + d, err = protocol.SignNameIDMappingRequest(t.Context(), mapping, signer) + if err != nil { + t.Fatal(err) + } + } else { + v, err := protocol.VerifyQuery(t.Context(), d, keys, o) + if err != nil { + t.Fatal(err) + } + q = v.Query() + d, err = protocol.SignQuery(t.Context(), q, signer) + if err != nil { + t.Fatal(err) + } + } + ours := write(t, filepath.Join(f.work, "go-query.xml"), xmlBytes(t, d)) + oracle(t, "verify", ours, key.public) + responsePath := filepath.Join(f.work, "independent-response.xml") + oracle(t, "response", ours, key.private, key.certificate, responsePath, authority, at.Format(time.RFC3339Nano)) + raw := read(t, responsePath) + response, err := document.Parse(t.Context(), raw, document.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + requestID, _ := d.Root().Attribute(document.Name{Local: "ID"}) + options := protocol.ExchangeOptions{Issuer: authority, ExpectedRequestID: requestID, At: at, Binding: metadata.SOAP} + if kind == "NameIDMappingRequest" { + v, err := protocol.VerifyNameIDMappingResponse(t.Context(), response, keys, options) + if err != nil { + t.Fatal(err) + } + if err = protocol.CheckMappedPolicy(t.Context(), v.Identifier(), mapping.Policy); err != nil { + t.Fatal(err) + } + response, err = protocol.SignNameIDMappingResponse(t.Context(), protocol.NameIDMappingResponse{MessageHeader: protocol.MessageHeader{Issuer: authority, IssueInstant: at}, InResponseTo: mapping.ID, Identifier: v.Identifier()}, signer) + if err != nil { + t.Fatal(err) + } + input, _ := protocol.NameIDDocument(t.Context(), mapping.NameID) + failure, err := protocol.SignNameIDMappingResponse(t.Context(), protocol.NameIDMappingResponse{MessageHeader: protocol.MessageHeader{Issuer: authority, IssueInstant: at}, InResponseTo: mapping.ID, Status: protocol.Status{Code: protocol.Requester, Subcodes: []string{protocol.UnknownPrincipal}}, Identifier: input}, signer) + if err != nil { + t.Fatal(err) + } + oracle(t, "verify", write(t, filepath.Join(f.work, "go-error.xml"), xmlBytes(t, failure)), key.public) + } else { + v, err := protocol.VerifyQueryResponse(t.Context(), response, keys, q, protocol.QueryResponseOptions{ExchangeOptions: options, Audience: requester}) + if err != nil { + t.Fatal(err) + } + want := 2 + if kind == "AssertionIDRequest" { + want = 1 + } + if len(v.Assertions()) != want { + t.Fatal("independent assertion count") + } + empty, err := protocol.SignQueryResponse(t.Context(), protocol.QueryResponse{MessageHeader: protocol.MessageHeader{Issuer: authority, IssueInstant: at}, InResponseTo: q.ID}, signer) + if err != nil { + t.Fatal(err) + } + oracle(t, "verify", write(t, filepath.Join(f.work, "go-empty.xml"), xmlBytes(t, empty)), key.public) + verified := v.Assertions() + assertions := make([]*document.Document, 0, len(verified)) + for _, a := range verified { + assertions = append(assertions, a.Document()) + } + response, err = protocol.SignQueryResponse(t.Context(), protocol.QueryResponse{MessageHeader: protocol.MessageHeader{Issuer: authority, IssueInstant: at}, InResponseTo: q.ID, Assertions: assertions}, signer) + if err != nil { + t.Fatal(err) + } + } + path := write(t, filepath.Join(f.work, "go-response.xml"), xmlBytes(t, response)) + oracle(t, "verify", path, key.public) + }) + } + t.Run("authority-metadata", func(t *testing.T) { + role := func(service string) *metadata.RoleDescriptor { + return &metadata.RoleDescriptor{Keys: []metadata.KeyDescriptor{{CertificateDER: key.cert.Raw}}, Endpoints: map[string][]metadata.Endpoint{service: {{Binding: metadata.SOAP, Location: endpoint}}, metadata.AssertionIDRequestService: {{Binding: metadata.SOAP, Location: endpoint}, {Binding: metadata.URI, Location: "https://authority.test/assertions"}}}} + } + d, err := metadata.Build(t.Context(), metadata.EntityDescriptor{EntityID: authority, ValidUntil: at.Add(time.Hour), AuthnAuthority: role(metadata.AuthnQueryService), AttributeAuthority: role(metadata.AttributeService), PDP: role(metadata.AuthzService)}, at, metadata.Options{}) + if err != nil { + t.Fatal(err) + } + oracle(t, "schema", write(t, filepath.Join(f.work, "authority-metadata.xml"), xmlBytes(t, d))) + }) +} diff --git a/tests/interop/saml_control_test.go b/tests/interop/saml_control_test.go new file mode 100644 index 0000000..4720e85 --- /dev/null +++ b/tests/interop/saml_control_test.go @@ -0,0 +1,111 @@ +//go:build interop + +package interop_test + +import ( + "bytes" + "crypto" + "fmt" + "path/filepath" + "testing" + "time" + + "github.com/skarm/xmlsec/c14n" + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/saml/binding" + "github.com/skarm/xmlsec/saml/protocol" +) + +// Native xmlsec1 verifies our control signatures and independently recomputes +// signatures in the full SOAP namespace context, including Inclusive C14N. +func TestXMLSecSAMLControlSOAP(t *testing.T) { + f := newInteropFixture(t) + selected(t, "xmlsec") + signer := generateKey(t, f.work, nameRSASHA256) + at := time.Date(2026, 11, 4, 12, 0, 0, 0, time.UTC) + header := protocol.MessageHeader{ID: "_request", Issuer: "https://idp.test/entity", Destination: "https://sp.test/soap", IssueInstant: at} + name := protocol.NameIdentifier{Value: "managed-primary", Format: protocol.NameIDPersistent, NameQualifier: header.Issuer, SPNameQualifier: "https://sp.test/entity", SPProvidedID: "managed-alias"} + for _, algorithm := range []string{c14n.Exclusive, c14n.Canonical10} { + key := protocol.SigningKey{Key: signer.signer, Canonicalization: algorithm} + logout, err := protocol.SignLogoutRequest(t.Context(), protocol.LogoutRequest{MessageHeader: header, NameID: name, SessionIndexes: []string{"session-1", "session-2"}, NotOnOrAfter: at.Add(time.Minute)}, key) + if err != nil { + t.Fatal(err) + } + manage, err := protocol.SignManageNameIDRequest(t.Context(), protocol.ManageNameIDRequest{MessageHeader: header, NameID: name, NewID: "new-primary"}, key) + if err != nil { + t.Fatal(err) + } + resolve, err := protocol.SignArtifactResolve(t.Context(), protocol.ArtifactResolve{MessageHeader: header, Artifact: "AAQAADE x"}, key) + if err != nil { + t.Fatal(err) + } + response, err := protocol.SignStatusResponse(t.Context(), protocol.StatusResponse{MessageHeader: header, Kind: protocol.LogoutResponse, InResponseTo: "_request", Status: protocol.Status{Code: protocol.Success, Subcodes: []string{protocol.PartialLogout}}}, key) + if err != nil { + t.Fatal(err) + } + for i, message := range []*document.Document{logout, manage, resolve, response} { + t.Run(fmt.Sprintf("%s/%d", algorithm, i), func(t *testing.T) { + envelope, err := protocol.SignSOAP(t.Context(), message, key) + if err != nil { + t.Fatal(err) + } + kind := message.Root().Name().Local + path := write(t, filepath.Join(f.work, "control-go.xml"), xmlBytes(t, envelope)) + selection := "(//*[local-name()='" + kind + "']/*[local-name()='Signature'])[1]" + native(t, f, true, nameVerify, flagPublicKeyPEM, signer.public, flagIDAttrID, kind, "--node-xpath", selection, path) + nativePath := filepath.Join(f.work, "control-native.xml") + native(t, f, true, nameSign, "--privkey-pem", signer.private, flagIDAttrID, kind, "--node-xpath", selection, flagNativeOutput, nativePath, path) + soap, err := binding.DecodeSOAP(t.Context(), read(t, nativePath), binding.SOAPOptions{}) + if err != nil { + t.Fatal(err) + } + d, err := soap.Payload(t.Context()) + if err != nil { + t.Fatal(err) + } + verify := func(d *document.Document) { + opts := protocol.ExchangeOptions{Issuer: header.Issuer, Destination: header.Destination, At: at, ExpectedRequestID: "_request", Binding: "urn:oasis:names:tc:SAML:2.0:bindings:SOAP"} + keys := []crypto.PublicKey{signer.signer.Public()} + switch kind { + case "LogoutRequest": + _, err = protocol.VerifyLogoutRequest(t.Context(), d, keys, opts) + case "ManageNameIDRequest": + _, err = protocol.VerifyManageNameIDRequest(t.Context(), d, keys, opts) + case "ArtifactResolve": + _, err = protocol.VerifyArtifactResolve(t.Context(), d, keys, opts) + case protocol.LogoutResponse: + _, err = protocol.VerifyStatusResponse(t.Context(), d, keys, opts) + default: + t.Fatal("unexpected message") + } + if err != nil { + t.Fatalf("native signature: %v", err) + } + } + verify(d) + if algorithm == c14n.Canonical10 { + wire := bytes.ReplaceAll(read(t, nativePath), []byte("env:"), []byte("S:")) + wire = bytes.ReplaceAll(wire, []byte("xmlns:env="), []byte("xmlns:S=")) + wire = bytes.Replace(wire, []byte("`)) + return func(t *testing.T, args ...string) []byte { + t.Helper() + params := []string{"-Dlogback.configurationFile=" + logConfig, "-cp", classpath, "OpenSAMLHoKOracle"} + out, _ := command(t, f, true, nil, "java", append(params, args...)...) + return out + } +} + +func hokOracleKey(t *testing.T, f *interopFixture, name, kind string) testKey { + t.Helper() + dir := filepath.Join(f.work, name) + if err := os.Mkdir(dir, 0o700); err != nil { + t.Fatal(err) + } + return generateKey(t, dir, kind) +} + +func reissuedCertificate(t *testing.T, f *interopFixture, key testKey) string { + t.Helper() + template := *key.cert + template.SerialNumber = big.NewInt(97) + der, err := x509.CreateCertificate(rand.Reader, &template, &template, key.signer.Public(), key.signer) + if err != nil { + t.Fatal(err) + } + return write(t, filepath.Join(f.work, "reissued.pem"), pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der})) +} + +func TestOpenSAMLHolderOfKey(t *testing.T) { + selected(t, "opensaml") + f := newInteropFixture(t) + oracle := openSAMLOracle(t, f) + idpKey := hokOracleKey(t, f, "idp", nameRSASHA256) + client := hokOracleKey(t, f, "presenter", nameECDSASHA256) + at := time.Now().UTC() + const recipient, requestID = "https://sp.test/acs", "_opensaml_request" + spec := protocol.Response{ID: "_response", Issuer: "https://idp.test/entity", Audience: "https://sp.test/entity", Destination: recipient, InResponseTo: requestID, IssueInstant: at, SOAPContext: true, Assertion: protocol.AssertionData{ID: "_assertion", NameID: "alice", NotBefore: at, NotOnOrAfter: at.Add(5 * time.Minute), AuthnInstant: at, AuthnContext: "urn:test:certificate", HolderOfKeyCertificate: client.cert.Raw}} + d, err := protocol.SignResponse(t.Context(), spec, protocol.SigningKey{Key: idpKey.signer}) + if err != nil { + t.Fatal(err) + } + soap, err := binding.WrapSOAP(t.Context(), d) + if err != nil { + t.Fatal(err) + } + path := write(t, filepath.Join(f.work, "go-hok.xml"), xmlBytes(t, soap)) + for _, test := range []struct { + name, certificate, recipient, request string + accept bool + }{ + {"matching-certificate", client.certificate, recipient, requestID, true}, + {"other-key", idpKey.certificate, recipient, requestID, false}, + {"same-key-different-certificate", reissuedCertificate(t, f, client), recipient, requestID, false}, + {"wrong-recipient", client.certificate, "https://other.test/acs", requestID, false}, + {"wrong-request", client.certificate, recipient, "_other_request", false}, + } { + t.Run(test.name, func(t *testing.T) { + raw := oracle(t, "verify", path, idpKey.public, test.certificate, test.recipient, test.request) + var result struct { + Engine string `json:"engine"` + Signatures, Confirmed bool + } + decode(t, raw, &result) + if result.Engine != "OpenSAML 5.2.3" || !result.Signatures || result.Confirmed != test.accept { + t.Fatalf("OpenSAML result: %s", raw) + } + }) + } + t.Run("modified-signed-assertion", func(t *testing.T) { + raw := read(t, path) + changed := bytes.Replace(raw, []byte("alice"), []byte("mallory"), 1) + if bytes.Equal(raw, changed) { + t.Fatal("tamper control did not alter assertion") + } + mutant := write(t, filepath.Join(f.work, "modified-signed-assertion.xml"), changed) + var result struct{ Signatures, Confirmed bool } + decode(t, oracle(t, "verify", mutant, idpKey.public, client.certificate, recipient, requestID), &result) + if result.Signatures || result.Confirmed { + t.Fatal("OpenSAML accepted modified signed assertion") + } + }) +} + +func TestOpenSAMLChannelBindings(t *testing.T) { + selected(t, "opensaml") + f := newInteropFixture(t) + oracle := openSAMLOracle(t, f) + signed := protocol.ChannelBinding{Type: ecp.TLSServerEndPoint, Data: bytes.Repeat([]byte{17}, 32)} + for _, test := range []struct { + name string + peer protocol.ChannelBinding + accept bool + }{ + {"matching", signed, true}, + {"different-data", protocol.ChannelBinding{Type: signed.Type, Data: bytes.Repeat([]byte{18}, 32)}, false}, + {"different-type", protocol.ChannelBinding{Type: "tls-exporter", Data: signed.Data}, false}, + {"missing-peer-data", protocol.ChannelBinding{}, false}, + } { + t.Run(test.name, func(t *testing.T) { + var result struct{ Matched bool } + decode(t, oracle(t, "channels", signed.Type, base64.StdEncoding.EncodeToString(signed.Data), test.peer.Type, base64.StdEncoding.EncodeToString(test.peer.Data)), &result) + if result.Matched != test.accept { + t.Fatalf("OpenSAML channel comparison: %+v", result) + } + var peer []protocol.ChannelBinding + if len(test.peer.Data) != 0 { + peer = []protocol.ChannelBinding{test.peer} + } + _, err := ecp.MatchChannelBindings([]protocol.ChannelBinding{signed}, peer) + if (err == nil) != test.accept { + t.Fatalf("Go channel comparison: %v", err) + } + }) + } +} + +func TestOpenSAMLHoKECPReturn(t *testing.T) { + selected(t, "opensaml") + f := newInteropFixture(t) + oracle := openSAMLOracle(t, f) + idpKey := hokOracleKey(t, f, "idp", nameRSASHA256) + spKey := hokOracleKey(t, f, "sp", nameECDSASHA256) + client := hokOracleKey(t, f, "presenter", nameECDSASHA256) + at := time.Now().UTC() + const issuer, audience, recipient = "https://idp.test/entity", "https://sp.test/entity", "https://sp.test/acs" + var aggregate bytes.Buffer + aggregate.WriteString(``) + for _, spec := range []metadata.EntityDescriptor{ + {EntityID: issuer, ValidUntil: at.Add(time.Hour), IDP: &metadata.RoleDescriptor{Keys: []metadata.KeyDescriptor{{Use: "signing", CertificateDER: idpKey.cert.Raw}}, Endpoints: map[string][]metadata.Endpoint{metadata.SingleSignOnService: {{Binding: metadata.HoKSSO, ProtocolBinding: metadata.SOAP, Location: "https://idp.test/sso"}}}}}, + {EntityID: audience, ValidUntil: at.Add(time.Hour), SP: &metadata.RoleDescriptor{Keys: []metadata.KeyDescriptor{{Use: "signing", CertificateDER: spKey.cert.Raw}}, Endpoints: map[string][]metadata.Endpoint{metadata.AssertionConsumerService: {{Binding: metadata.HoKSSO, ProtocolBinding: metadata.PAOS, Location: recipient, Indexed: true}}}}}, + } { + d, err := metadata.Build(t.Context(), spec, at, metadata.Options{}) + if err != nil { + t.Fatal(err) + } + aggregate.Write(xmlBytes(t, d)) + } + aggregate.WriteString(``) + partners, err := metadata.ParseTrusted(t.Context(), aggregate.Bytes(), at, metadata.Options{}) + if err != nil { + t.Fatal(err) + } + requests, err := state.NewMemoryStore(state.Config{}) + if err != nil { + t.Fatal(err) + } + replays, err := replay.NewMemoryStore(replay.Config{}) + if err != nil { + t.Fatal(err) + } + service, err := sp.NewServiceProvider(sp.Config{EntityID: audience, Metadata: partners, Signing: protocol.SigningKey{Key: spKey.signer}, Requests: requests, Replay: replays}) + if err != nil { + t.Fatal(err) + } + for _, alternative := range []bool{false, true} { + t.Run(map[bool]string{false: "single-keyinfo", true: "alternative-keyinfo"}[alternative], func(t *testing.T) { + browser := bytes.Repeat([]byte{31}, 32) + out, err := service.Begin(t.Context(), sp.BeginOptions{IDP: issuer, Binding: metadata.PAOS, HolderOfKey: true, At: at, BrowserBinding: browser}) + if err != nil { + t.Fatal(err) + } + challenge, err := out.PAOS() + if err != nil { + t.Fatal(err) + } + x, err := ecp.ParseHoKChallenge(t.Context(), xmlBytes(t, challenge), "https://sp.test/resource", client.cert.Raw, nil, binding.SOAPOptions{}) + if err != nil { + t.Fatal(err) + } + output := filepath.Join(f.work, "opensaml-response.xml") + args := []string{"issue", idpKey.private, idpKey.certificate, client.certificate, output, issuer, audience, recipient, out.RequestID(), at.Format(time.RFC3339Nano)} + if alternative { + args = append(args, spKey.certificate) + } + oracle(t, args...) + raw := read(t, output) + returned, err := x.Finish(t.Context(), raw, binding.SOAPOptions{}) + if err != nil { + t.Fatalf("OpenSAML ECP headers: %v", err) + } + input := write(t, filepath.Join(f.work, "paos-input.xml"), xmlBytes(t, returned)) + proofFile := filepath.Join(f.work, "jdk-paos-proof.xml") + java(t, f, "ECPProofOracle", client.private, client.certificate, input, proofFile, c14n.Exclusive, client.method) + p, err := ecp.VerifyEnvelopedXML(t.Context(), read(t, proofFile), proof.Options{}) + if err != nil { + t.Fatal(err) + } + if _, err := service.ConsumeECP(t.Context(), p, sp.ConsumeOptions{At: at, BrowserBinding: bytes.Repeat([]byte{32}, 32)}); err == nil { + t.Fatal("independent HoK return accepted for another browser") + } + wrongProof := filepath.Join(f.work, "wrong-key-paos-proof.xml") + java(t, f, "ECPProofOracle", idpKey.private, idpKey.certificate, input, wrongProof, c14n.Exclusive, idpKey.method) + wrong, err := ecp.VerifyEnvelopedXML(t.Context(), read(t, wrongProof), proof.Options{}) + if err != nil { + t.Fatal(err) + } + if _, err := service.ConsumeECP(t.Context(), wrong, sp.ConsumeOptions{At: at, BrowserBinding: browser}); err == nil { + t.Fatal("independent HoK return accepted proof of another key") + } + assertion, err := service.ConsumeECP(t.Context(), p, sp.ConsumeOptions{At: at, BrowserBinding: browser}) + if err != nil || assertion.NameID() != "alice" || !bytes.Equal(assertion.HolderOfKeyCertificate(), client.cert.Raw) { + t.Fatalf("independently produced HoK return: %v", err) + } + if _, err := service.ConsumeECP(t.Context(), p, sp.ConsumeOptions{At: at, BrowserBinding: browser}); err == nil { + t.Fatal("independent HoK return replay accepted") + } + }) + } +} diff --git a/tests/interop/saml_openam_http_test.go b/tests/interop/saml_openam_http_test.go new file mode 100644 index 0000000..b75f275 --- /dev/null +++ b/tests/interop/saml_openam_http_test.go @@ -0,0 +1,538 @@ +//go:build interop + +package interop_test + +import ( + "context" + "crypto" + "crypto/x509" + "encoding/base64" + "encoding/json" + "encoding/pem" + "errors" + "fmt" + "html" + "io" + "net" + "net/http" + "net/url" + "os" + "os/exec" + "path/filepath" + "regexp" + "strings" + "syscall" + "testing" + "time" + + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/lexicon" + "github.com/skarm/xmlsec/internal/tree" + "github.com/skarm/xmlsec/saml/authority" + "github.com/skarm/xmlsec/saml/binding" + "github.com/skarm/xmlsec/saml/metadata" + "github.com/skarm/xmlsec/saml/protocol" + "github.com/skarm/xmlsec/saml/replay" + "github.com/skarm/xmlsec/xmlenc" +) + +const ( + openAMHTTPTimeout = 15 * time.Second + authorityReadinessTimeout = 2 * time.Minute + authorityReadinessPollInterval = 200 * time.Millisecond + authorityProcessWaitDelay = 20 * time.Second + authorityProcessLogExcerptBytes = 16_000 + authorityHTTPBodyBytes = 4 << 20 + openAMSOAPLogExcerptBytes = 10_000 + openAMInstallLogExcerptBytes = 12_000 + openAMSSODiagnosticRunes = 500 + authorityHTTPDiagnosticRunes = 300 + openAMHeapFlag = "-Xmx768m" +) + +const ( + openAMEntity = "https://openam.test/entity" + mappingTarget = "https://target.test/entity" +) + +// Deployment uses the released OpenAM WAR with the documented test-only +// NameIDMappingResponse serialization-order correction. All SOAP parsing, query +// resolution, account federation, mapping and signatures are upstream code. +func TestOpenAMAuthorityHTTP(t *testing.T) { + selected(t, "authority-http") + dist, tomcat := os.Getenv("OPENAM_DIST"), os.Getenv("TOMCAT_HOME") + if dist == "" || tomcat == "" { + t.Fatal("OPENAM_DIST and TOMCAT_HOME are required") + } + f := newInteropFixture(t) + key := generateKey(t, f.work, nameRSASHA256) + base, client, cert := installOpenAM(t, f, dist, tomcat, key) + recorder := &authorityHTTPRecorder{base: client.Transport, directory: t.ArtifactDir()} + client.Transport = recorder + t.Cleanup(func() { + if t.Failed() { + for n := uint64(1); n <= recorder.sequence.Load(); n++ { + b, e := os.ReadFile(filepath.Join(recorder.directory, fmt.Sprintf("authority-response-%d.xml", n))) + if e == nil && strings.Contains(string(b), "Envelope") { + b = regexp.MustCompile(`(?s)`).ReplaceAll(b, []byte("[signature omitted]")) + if len(b) > openAMSOAPLogExcerptBytes { + b = b[:openAMSOAPLogExcerptBytes] + } + t.Logf("SOAP %d: %s", n, b) + } + } + } + }) + // Seed through the product's public authentication and WebSSO endpoints. + signingCert, err := x509.ParseCertificate(cert) + if err != nil { + t.Fatal(err) + } + token := openAMLogin(t, client, base) + source := openAMSSO(t, client, base, token, shibbolethRequester, signingCert.PublicKey) + at := time.Now().UTC() + endpoint := func(path string) []metadata.Endpoint { + return []metadata.Endpoint{{Binding: metadata.SOAP, Location: base + path}} + } + role := &metadata.RoleDescriptor{Keys: []metadata.KeyDescriptor{{CertificateDER: cert}}, Endpoints: map[string][]metadata.Endpoint{ + metadata.AuthnQueryService: endpoint("/AuthnQueryServiceSoap/metaAlias/authn"), + metadata.AssertionIDRequestService: endpoint("/AIDReqSoap/AuthnAuthRole/metaAlias/authn"), + }} + idp := &metadata.RoleDescriptor{Keys: role.Keys, Endpoints: map[string][]metadata.Endpoint{ + "SingleSignOnService": {{Binding: metadata.HTTPPost, Location: base + "/SSOPOST/metaAlias/idp"}}, + metadata.NameIDMappingService: endpoint("/NIMSoap/metaAlias/idp"), + }} + md, err := metadata.Build(t.Context(), metadata.EntityDescriptor{EntityID: openAMEntity, ValidUntil: at.Add(time.Hour), AuthnAuthority: role, IDP: idp}, at, metadata.Options{AllowHTTP: true}) + if err != nil { + t.Fatal(err) + } + snapshot, err := metadata.ParseTrusted(t.Context(), xmlBytes(t, md), at, metadata.Options{AllowHTTP: true}) + if err != nil { + t.Fatal(err) + } + store, _ := replay.NewMemoryStore(replay.Config{}) + c := authority.Client{EntityID: shibbolethRequester, Peer: openAMEntity, Role: metadata.AuthenticationAuthority, Metadata: snapshot, Signing: protocol.SigningKey{Key: key.signer, CertificateDER: key.cert.Raw}, Replay: store, SOAP: binding.SOAPClient{Client: client, AllowHTTP: true}} + subject, err := protocol.NewSubject(t.Context(), source.name) + if err != nil { + t.Fatal(err) + } + q := protocol.Query{MessageHeader: protocol.MessageHeader{ID: "_openam_authn"}, Kind: protocol.AuthenticationQuery, Subject: subject, SessionIndex: source.session, Context: &protocol.RequestedContext{Classes: []string{"urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport"}}} + t.Run("AuthnQuery/past-authentication", func(t *testing.T) { + r, err := c.Query(t.Context(), q, time.Now().UTC()) + if err != nil { + t.Fatal(err) + } + requireIssuedAssertion(t, r, source) + }) + t.Run("AuthnQuery/unknown-session", func(t *testing.T) { + missing := q + missing.ID = "_openam_unknown_session" + missing.SessionIndex = "_not_an_existing_session" + r, err := c.Query(t.Context(), missing, time.Now().UTC()) + if err != nil { + t.Fatal(err) + } + if r.Status().Code != protocol.Success || len(r.Assertions()) != 0 { + t.Fatal("unknown session returned authentication statements") + } + }) + t.Run("AssertionIDRequest/issued-assertion", func(t *testing.T) { + r, err := c.Query(t.Context(), protocol.Query{MessageHeader: protocol.MessageHeader{ID: "_openam_assertion"}, Kind: protocol.AssertionRequest, AssertionIDs: []string{source.id}}, time.Now().UTC()) + if err != nil { + t.Fatal(err) + } + requireIssuedAssertion(t, r, source) + }) + t.Run("AssertionIDRequest/unknown-id", func(t *testing.T) { + r, err := c.Query(t.Context(), protocol.Query{MessageHeader: protocol.MessageHeader{ID: "_openam_unknown_id"}, Kind: protocol.AssertionRequest, AssertionIDs: []string{"_absent_assertion"}}, time.Now().UTC()) + if err != nil { + t.Fatal(err) + } + if r.Status().Code != protocol.Success || len(r.Assertions()) != 0 { + t.Fatal("unknown ID returned an assertion") + } + }) + target := openAMSSO(t, client, base, token, mappingTarget, signingCert.PublicKey) + if source.name.Value == target.name.Value { + t.Fatal("upstream did not create pairwise identifiers") + } + c.Role = metadata.IdentityProvider + mapping := protocol.NameIDMappingRequest{MessageHeader: protocol.MessageHeader{ID: "_openam_mapping"}, NameID: source.name, Policy: protocol.NameIDPolicy{Format: protocol.NameIDPersistent, SPNameQualifier: mappingTarget}} + t.Run("NameIDMapping/encrypted-target-identifier", func(t *testing.T) { + r, err := c.Map(t.Context(), mapping, time.Now().UTC()) + if err != nil { + t.Fatal(err) + } + if r.Status().Code != protocol.Success || r.Identifier() == nil || r.Identifier().Root().Name().Local != "EncryptedID" { + t.Fatal("missing encrypted mapped identifier") + } + // OpenAM's released Mapping implementation encrypts to the requester. The + // envelope signature is verified before its explicitly configured CBC policy. + id, err := r.DecryptNameID(t.Context(), &protocol.DecryptionOptions{Keys: []crypto.Decrypter{key.signer.(crypto.Decrypter)}, Data: xmlenc.Options{DataAlgorithm: xmlenc.AES128CBC, KeyTransportAlgorithm: xmlenc.RSA15, AllowLegacy: true}}) + if err != nil { + t.Fatal(err) + } + if id != target.name { + t.Fatalf("mapped identifier differs from independently issued target NameID: %+v", id) + } + }) + t.Run("NameIDMapping/unsupported-policy", func(t *testing.T) { + bad := mapping + bad.ID = "_openam_bad_mapping" + bad.Policy.Format = protocol.NameIDTransient + r, err := c.Map(t.Context(), bad, time.Now().UTC()) + // OpenAM incorrectly uses the subordinate InvalidNameIDPolicy as the + // top-level StatusCode (Core 3.2.2.2). Keep this known peer defect visible + // and require the strict client to reject it without an identifier. + if !errors.Is(err, protocol.ErrProfile) || r != nil { + t.Fatalf("nonconforming peer status accepted: %v", err) + } + path := filepath.Join(recorder.directory, fmt.Sprintf("authority-response-%d.xml", recorder.sequence.Load())) + message, err := binding.DecodeSOAP(t.Context(), read(t, path), binding.SOAPOptions{}) + if err != nil { + t.Fatal(err) + } + payload, err := message.Payload(t.Context()) + if err != nil { + t.Fatal(err) + } + status := onlyAuthorityChild(t, payload.Root(), protocol.ProtocolNamespace, "Status") + code := onlyAuthorityChild(t, status, protocol.ProtocolNamespace, "StatusCode") + value, _ := code.Attribute(document.Name{Local: "Value"}) + if value != protocol.InvalidNameIDPolicy { + t.Fatal("unexpected upstream mapping failure") + } + id, _ := payload.Root().Attribute(document.Name{Local: "InResponseTo"}) + if id != bad.ID { + t.Fatal("uncorrelated upstream failure") + } + }) +} + +type issuedOpenAMAssertion struct { + id, session string + name protocol.NameIdentifier +} + +func requireIssuedAssertion(t *testing.T, r *protocol.VerifiedQueryResponse, want issuedOpenAMAssertion) { + t.Helper() + if r.Status().Code != protocol.Success || len(r.Assertions()) != 1 || r.Assertions()[0].ID() != want.id { + t.Fatal("Authority did not return the independently issued assertion") + } + statements := r.Assertions()[0].Statements() + if len(statements) != 1 || statements[0].Name().Local != "AuthnStatement" { + t.Fatal("missing authentication statement") + } + session, _ := statements[0].Attribute(document.Name{Local: "SessionIndex"}) + if session != want.session { + t.Fatal("Authority changed the original session") + } +} + +func openAMLogin(t *testing.T, client *http.Client, base string) string { + t.Helper() + req, err := http.NewRequestWithContext(t.Context(), http.MethodPost, base+"/json/authenticate", nil) + if err != nil { + t.Fatal(err) + } + req.Header.Set("X-Openam-Username", "alice") + req.Header.Set("X-Openam-Password", "TestPassword123!") + req.Header.Set(lexicon.HeaderContentType, "application/json") + body := authorityReadHTTP(t, client, req) + var result struct { + Token string `json:"tokenId"` + } + if err = json.Unmarshal(body, &result); err != nil || result.Token == "" { + t.Fatal("OpenAM fixture login failed") + } + return result.Token +} + +func openAMSSO(t *testing.T, client *http.Client, base, token, sp string, key crypto.PublicKey) issuedOpenAMAssertion { + t.Helper() + u := base + "/idpssoinit?metaAlias=/idp&binding=HTTP-POST&spEntityID=" + url.QueryEscape(sp) + "&NameIDFormat=" + url.QueryEscape(protocol.NameIDPersistent) + req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, u, nil) + if err != nil { + t.Fatal(err) + } + req.AddCookie(&http.Cookie{Name: "iPlanetDirectoryPro", Value: token}) + body := authorityReadHTTP(t, client, req) + match := regexp.MustCompile(`name="SAMLResponse"\s+value="([^"]+)"`).FindSubmatch(body) + if len(match) != 2 { + t.Fatalf("OpenAM did not return a POST response: %.*s", openAMSSODiagnosticRunes, body) + } + raw, err := base64.StdEncoding.DecodeString(html.UnescapeString(string(match[1]))) + if err != nil { + t.Fatal(err) + } + write(t, filepath.Join(t.ArtifactDir(), "sso-"+url.PathEscape(sp)+".xml"), raw) + d, err := document.Parse(t.Context(), raw, document.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + var result issuedOpenAMAssertion + for _, a := range d.Root().Children() { + if a.Name() != (document.Name{Space: protocol.AssertionNamespace, Local: "Assertion"}) { + continue + } + signed, err := tree.ScopeDocument(d, a) + if err != nil { + t.Fatal(err) + } + if _, err = protocol.VerifyAuthorityAssertion(t.Context(), signed, []crypto.PublicKey{key}, protocol.AssertionOptions{Issuer: openAMEntity, Audience: sp, At: time.Now().UTC()}); err != nil { + t.Fatalf("upstream WebSSO assertion signature/conditions: %v", err) + } + result.id, _ = a.Attribute(document.Name{Local: "ID"}) + for _, e := range a.Children() { + if e.Name().Local == "AuthnStatement" { + result.session, _ = e.Attribute(document.Name{Local: "SessionIndex"}) + } + if e.Name().Local == "Subject" { + for _, n := range e.Children() { + if n.Name().Local == "NameID" { + result.name = protocol.NameIdentifier{Value: n.Text()} + result.name.Format, _ = n.Attribute(document.Name{Local: "Format"}) + result.name.NameQualifier, _ = n.Attribute(document.Name{Local: "NameQualifier"}) + result.name.SPNameQualifier, _ = n.Attribute(document.Name{Local: "SPNameQualifier"}) + } + } + } + } + } + if result.id == "" || result.session == "" || result.name.Format != protocol.NameIDPersistent || result.name.SPNameQualifier != sp { + t.Fatal("incomplete upstream WebSSO assertion") + } + return result +} + +func authorityReadHTTP(t *testing.T, client *http.Client, req *http.Request) []byte { + t.Helper() + r, err := client.Do(req) + if err != nil { + t.Fatal(err) + } + defer r.Body.Close() + b, err := io.ReadAll(io.LimitReader(r.Body, authorityHTTPBodyBytes)) + if err != nil { + t.Fatal(err) + } + if r.StatusCode != http.StatusOK { + t.Fatalf("Authority HTTP %s: %.*s", r.Status, authorityHTTPDiagnosticRunes, b) + } + return b +} + +func installOpenAM(t *testing.T, f *interopFixture, dist, tomcat string, key testKey) (string, *http.Client, []byte) { + t.Helper() + home := filepath.Join(f.work, "openam-config") + t.Cleanup(func() { + if t.Failed() { + b, _ := os.ReadFile(filepath.Join(home, "install.log")) + if len(b) > openAMInstallLogExcerptBytes { + b = b[len(b)-openAMInstallLogExcerptBytes:] + } + t.Logf("OpenAM install: %s", b) + } + }) + base := filepath.Join(f.work, "tomcat") + command(t, f, true, nil, "cp", "-R", filepath.Join(tomcat, "conf"), base) + // cp creates base as the conf directory; move it into the Tomcat layout. + conf := base + "-conf" + if err := os.Rename(base, conf); err != nil { + t.Fatal(err) + } + for _, p := range []string{"logs", "temp", "work", "webapps"} { + if err := os.MkdirAll(filepath.Join(base, p), fixtureDirectoryMode); err != nil { + t.Fatal(err) + } + } + if err := os.Rename(conf, filepath.Join(base, "conf")); err != nil { + t.Fatal(err) + } + if err := os.Symlink(filepath.Join(dist, "openam.war"), filepath.Join(base, "webapps/openam.war")); err != nil { + t.Fatal(err) + } + port := authorityFreePort(t) + serverXML := string(read(t, filepath.Join(base, "conf/server.xml"))) + serverXML = strings.Replace(serverXML, `\s*)[^<]*`).ReplaceAllString(configXML, "${1}0") + configXML = regexp.MustCompile(`(\s*)[^<]*`).ReplaceAllString(configXML, "${1}300") + old := "\n false" + if !strings.Contains(configXML, old) { + t.Fatal("upstream assertion cache configuration changed") + } + write(t, ext, []byte(strings.Replace(configXML, old, strings.Replace(old, "false", "true", 1), 1))) + adm("import-entity", "--realm", "/", "--cot", "test", "--meta-data-file", idp, "--extended-data-file", ext) + for i, entity := range []string{shibbolethRequester, mappingTarget} { + md, err := metadata.Build(t.Context(), metadata.EntityDescriptor{EntityID: entity, ValidUntil: time.Now().Add(time.Hour), SP: &metadata.RoleDescriptor{WantAssertionsSigned: true, Keys: []metadata.KeyDescriptor{{CertificateDER: key.cert.Raw}}, Endpoints: map[string][]metadata.Endpoint{"AssertionConsumerService": {{Binding: metadata.HTTPPost, Location: entity + "/acs", Index: 0, Default: true}}}}}, time.Now(), metadata.Options{}) + if err != nil { + t.Fatal(err) + } + path := filepath.Join(f.work, fmt.Sprintf("sp-%d.xml", i)) + write(t, path, xmlBytes(t, md)) + adm("import-entity", "--realm", "/", "--cot", "test", "--meta-data-file", path) + } + adm("create-identity", "--realm", "/", "--idname", "alice", "--idtype", "User", "--attributevalues", "userpassword=TestPassword123!", "cn=Alice", "sn=Test", "inetuserstatus=Active") + certFile := filepath.Join(f.work, "openam-signing.pem") + command(t, f, true, nil, "keytool", "-exportcert", "-rfc", "-alias", "live", "-keystore", keystore, "-storetype", "JCEKS", "-storepass:file", filepath.Join(home, "openam/.storepass"), "-file", certFile) + block, _ := pem.Decode(read(t, certFile)) + if block == nil { + t.Fatal("missing OpenAM signing certificate") + } + stop() + startAuthorityProcess(t, f, "openam", "java", args...) + waitAuthorityHTTP(t, client, endpoint+"/isAlive.jsp", http.StatusOK) + return endpoint, client, block.Bytes +} + +func authorityFreePort(t *testing.T) string { + t.Helper() + l, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + _, port, err := net.SplitHostPort(l.Addr().String()) + if err != nil { + t.Fatal(err) + } + if err = l.Close(); err != nil { + t.Fatal(err) + } + return port +} + +func startAuthorityProcess(t *testing.T, f *interopFixture, label, program string, args ...string) func() { + t.Helper() + ctx, cancel := context.WithCancel(t.Context()) + logPath := filepath.Join(t.ArtifactDir(), label+".log") + log, err := os.Create(logPath) + if err != nil { + cancel() + t.Fatal(err) + } + cmd := exec.CommandContext(ctx, program, args...) + cmd.Dir = f.work + cmd.Stdout = log + cmd.Stderr = log + cmd.Cancel = func() error { return cmd.Process.Signal(syscall.SIGTERM) } + cmd.WaitDelay = authorityProcessWaitDelay + if err = cmd.Start(); err != nil { + cancel() + _ = log.Close() + t.Fatal(err) + } + done := make(chan error, 1) + go func() { done <- cmd.Wait() }() + stopped := false + stop := func() { + if !stopped { + cancel() + <-done + _ = log.Close() + stopped = true + } + } + t.Cleanup(func() { + stop() + if t.Failed() { + b, _ := os.ReadFile(logPath) + if len(b) > authorityProcessLogExcerptBytes { + b = b[len(b)-authorityProcessLogExcerptBytes:] + } + t.Logf("%s: %s", label, b) + } + }) + return stop +} + +func waitAuthorityHTTP(t *testing.T, client *http.Client, endpoint string, status int) { + t.Helper() + ctx, cancel := context.WithTimeout(t.Context(), authorityReadinessTimeout) + defer cancel() + for { + req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil) + if err != nil { + t.Fatal(err) + } + r, err := client.Do(req) + if err == nil { + _ = r.Body.Close() + if r.StatusCode == status { + return + } + } + select { + case <-ctx.Done(): + t.Fatalf("Authority did not become ready at %s: %v", endpoint, errors.Join(ctx.Err(), err)) + case <-time.After(authorityReadinessPollInterval): + } + } +} diff --git a/tests/interop/saml_proof_test.go b/tests/interop/saml_proof_test.go new file mode 100644 index 0000000..4dfa79c --- /dev/null +++ b/tests/interop/saml_proof_test.go @@ -0,0 +1,243 @@ +//go:build interop + +package interop_test + +import ( + "bytes" + "context" + "crypto" + "crypto/ecdsa" + "crypto/sha256" + "encoding/asn1" + "encoding/base64" + "math/big" + "path/filepath" + "strconv" + "strings" + "testing" + "time" + + "github.com/skarm/xmlsec/c14n" + "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/saml/binding" + "github.com/skarm/xmlsec/saml/metadata" + "github.com/skarm/xmlsec/saml/proof" + "github.com/skarm/xmlsec/saml/protocol" + "github.com/skarm/xmlsec/xmldsig" +) + +// Both engines authenticate the foreign attributes in actual SAML metadata. +func TestXMLSecECPMetadata(t *testing.T) { + selected(t, "xmlsec") + f := newInteropFixture(t) + key := generateKey(t, f.work, nameRSASHA256) + at := time.Now().UTC() + spec := metadata.EntityDescriptor{ID: "_ecp_metadata", EntityID: "https://sp.test/entity", ValidUntil: at.Add(time.Hour), SP: &metadata.RoleDescriptor{Endpoints: map[string][]metadata.Endpoint{metadata.AssertionConsumerService: {{Binding: metadata.HoKSSO, ProtocolBinding: metadata.PAOS, Location: "https://sp.test/acs", Indexed: true, SupportedChannelBindings: []string{"tls-server-end-point", "tls-exporter"}}}}}} + d, err := metadata.Build(t.Context(), spec, at, metadata.Options{}) + if err != nil { + t.Fatal(err) + } + signed, err := metadata.Sign(t.Context(), d, key.signer, key.cert.Raw, xmldsig.RSASHA256, at, metadata.Options{}) + if err != nil { + t.Fatal(err) + } + path := write(t, filepath.Join(f.work, "ecp-metadata.xml"), xmlBytes(t, signed)) + native(t, f, true, nameVerify, flagPublicKeyPEM, key.public, flagIDAttrID, "EntityDescriptor", path) + out := filepath.Join(f.work, "native-ecp-metadata.xml") + native(t, f, true, nameSign, "--privkey-pem", key.private, flagIDAttrID, "EntityDescriptor", flagNativeOutput, out, path) + verifier, err := metadata.NewVerifier(metadata.Config{TrustedKeys: []crypto.PublicKey{key.signer.Public()}}) + if err != nil { + t.Fatal(err) + } + snapshot, err := verifier.Verify(t.Context(), read(t, out), at) + if err != nil { + t.Fatal(err) + } + entity, err := snapshot.Find(spec.EntityID, at) + if err != nil { + t.Fatal(err) + } + role, err := entity.Role(metadata.ServiceProvider, at) + if err != nil { + t.Fatal(err) + } + endpoint, err := role.HoKACS(metadata.PAOS, nil) + if err != nil || !endpoint.SupportsChannelBinding("tls-exporter") { + t.Fatal("independently signed metadata lost HoK/channel bindings", err) + } +} + +// Independent primitives, not an assertion that a third-party ECP stack adopts +// our interpretation of CS01 2.3.11's contradictory no-Transforms/self-exclusion +// clauses. libxml2 selects exactly the SOAP nodes minus the proof Signature; +// OpenSSL verifies and independently produces SignatureValue. xmlsec1's ordinary +// XMLDSig verifier rejects the literal no-Transforms empty-URI reference and +// accepts the profile declaring the standard enveloped-signature transform. +func TestECPXMLProofIndependentPrimitives(t *testing.T) { + f := newInteropFixture(t) + selected(t, "xmlsec") + oracle := cOracle(t, f) + for _, keyType := range []string{nameRSASHA256, nameECDSASHA256} { + key := generateKey(t, f.work, keyType) + for _, wire := range []struct { + name string + sign func(context.Context, *document.Document, proof.Signing) (*document.Document, error) + verify func(context.Context, []byte, proof.Options) (*proof.Verified, error) + generic bool + }{{"CS01-compatibility", proof.SignCS01CompatXML, proof.VerifyCS01CompatXML, false}, {"XMLDSig-enveloped", proof.SignEnvelopedXML, proof.VerifyEnvelopedXML, true}} { + for _, algorithm := range canonicalizations() { + t.Run(keyType+"/"+wire.name+"/"+algorithm, func(t *testing.T) { + at := time.Date(2026, 11, 5, 12, 0, 0, 0, time.UTC) + d, err := protocol.SignAuthnRequest(t.Context(), protocol.AuthnRequest{ID: "_native_proof", Issuer: "https://sp.test/entity", Destination: "https://idp.test/sso", ACS: "https://sp.test/acs", IssueInstant: at}, protocol.SigningKey{Key: key.signer}) + if err != nil { + t.Fatal(err) + } + envelope, err := protocol.SignSOAP(t.Context(), d, protocol.SigningKey{Key: key.signer}) + if err != nil { + t.Fatal(err) + } + signed, err := wire.sign(t.Context(), envelope, proof.Signing{Key: key.signer, Certificate: key.cert.Raw, Canonicalization: algorithm}) + if err != nil { + t.Fatal(err) + } + raw := xmlBytes(t, signed) + path := write(t, filepath.Join(f.work, "proof.xml"), raw) + // Retain nested SAML signatures. Omit only the proof's exact + // subtree, its attributes/namespaces, and empty-URI comments. + proofPath := "/*[local-name()='Envelope']/*[local-name()='Header']/*[local-name()='Security']/*[local-name()='Signature']" + exclude := "(//node() | //@* | //namespace::*)[not(ancestor-or-self::*[local-name()='Signature' and namespace-uri()='" + ds + "' and parent::*[local-name()='Security' and namespace-uri()='" + proof.SecurityNamespace + "']]) and not(self::comment())]" + canonical, _ := command(t, f, true, nil, oracle, path, "0", "0", exclude, "-") + digest := sha256.Sum256(canonical) + sig := ecpProofElement(t, signed) + ref := sig.Children()[0].Children()[2] + if value := ref.Children()[len(ref.Children())-1].Text(); value != base64.StdEncoding.EncodeToString(digest[:]) { + t.Fatalf("independent reference digest: %s", value) + } + mode, comments := "0", "0" + if strings.HasPrefix(algorithm, c14n.Exclusive) { + mode = "1" + } else if strings.HasPrefix(algorithm, c14n.Canonical11) { + mode = "2" + } + if strings.HasSuffix(algorithm, "WithComments") { + comments = "1" + } + selectSI := "(" + proofPath + "/*[local-name()='SignedInfo']/descendant-or-self::node() | " + proofPath + "/*[local-name()='SignedInfo']/descendant-or-self::*/@* | " + proofPath + "/*[local-name()='SignedInfo']/descendant-or-self::*/namespace::*)" + si, _ := command(t, f, true, nil, oracle, path, mode, comments, selectSI, "-") + budget := len(canonical) + len(si) + if _, err := wire.verify(t.Context(), raw, proof.Options{Policy: xmldsig.Policy{MaxCanonicalBytes: budget}}); err != nil { + t.Fatalf("exact canonical budget: %v", err) + } + if _, err := wire.verify(t.Context(), raw, proof.Options{Policy: xmldsig.Policy{MaxCanonicalBytes: budget - 1}}); err == nil { + t.Fatal("combined reference/SignedInfo budget exceeded") + } + siFile := write(t, filepath.Join(f.work, "proof-si.bin"), si) + value, err := base64.StdEncoding.DecodeString(sig.Children()[1].Text()) + if err != nil { + t.Fatal(err) + } + if _, ok := key.signer.(*ecdsa.PrivateKey); ok { + value, err = asn1.Marshal(struct{ R, S *big.Int }{new(big.Int).SetBytes(value[:len(value)/2]), new(big.Int).SetBytes(value[len(value)/2:])}) + if err != nil { + t.Fatal(err) + } + } + valueFile := write(t, filepath.Join(f.work, "proof-signature.bin"), value) + command(t, f, true, nil, "openssl", "dgst", "-sha256", "-verify", key.public, "-signature", valueFile, siFile) + nativeValue, _ := command(t, f, true, nil, "openssl", "dgst", "-sha256", "-sign", key.private, siFile) + if ec, ok := key.signer.(*ecdsa.PrivateKey); ok { + var parts struct{ R, S *big.Int } + rest, err := asn1.Unmarshal(nativeValue, &parts) + if err != nil || len(rest) != 0 { + t.Fatal("OpenSSL ECDSA encoding", err) + } + size := (ec.Curve.Params().BitSize + 7) / 8 + nativeValue = append(parts.R.FillBytes(make([]byte, size)), parts.S.FillBytes(make([]byte, size))...) + } + nativeRaw := bytes.Replace(raw, []byte(sig.Children()[1].Text()), []byte(base64.StdEncoding.EncodeToString(nativeValue)), 1) + if _, err := wire.verify(t.Context(), nativeRaw, proof.Options{}); err != nil { + t.Fatalf("OpenSSL-produced ECP proof: %v", err) + } + // Independent XMLDSig checks accept the nested SAML signature; + // the proof result depends on the declared transform profile. + native(t, f, true, nameVerify, flagPublicKeyPEM, key.public, flagIDAttrID, "AuthnRequest", "--node-xpath", "(//*[local-name()='AuthnRequest']/*[local-name()='Signature'])[1]", path) + native(t, f, wire.generic, nameVerify, flagPublicKeyPEM, key.public, "--node-xpath", proofPath, path) + t.Log("libxml2/OpenSSL bidirectional proof; explicit XML profile versus ordinary xmlsec1; C14N mode=" + mode + ", comments=" + comments + ", signed bytes=" + strconv.Itoa(len(canonical))) + }) + } + } + } +} + +func ecpProofElement(t *testing.T, d *document.Document) document.Element { + t.Helper() + for _, h := range d.Root().Children() { + if h.Name() != (document.Name{Space: binding.SOAPNamespace, Local: "Header"}) { + continue + } + for _, s := range h.Children() { + if s.Name() == (document.Name{Space: proof.SecurityNamespace, Local: "Security"}) { + return s.Children()[0] + } + } + } + t.Fatal("missing ECP proof") + return document.Element{} +} + +// xmlsec1 independently recomputes both HoK assertion and Response signatures +// in their actual SOAP context. The ECP proof is then added without invalidating +// either signature; the SP checks the independently signed X.509 confirmation. +func TestXMLSecSAMLHolderOfKey(t *testing.T) { + f := newInteropFixture(t) + selected(t, "xmlsec") + idpKey := generateKey(t, f.work, nameRSASHA256) + client := generateKey(t, f.work, nameECDSASHA256) + at := time.Date(2026, 11, 5, 12, 0, 0, 0, time.UTC) + for _, algorithm := range []string{c14n.Exclusive, c14n.Canonical10} { + t.Run(algorithm, func(t *testing.T) { + spec := protocol.Response{ID: "_response", Issuer: "https://idp.test/entity", Audience: "https://sp.test/entity", Destination: "https://sp.test/acs", InResponseTo: "_request", IssueInstant: at, SOAPContext: true, Assertion: protocol.AssertionData{ID: "_assertion", NameID: "alice", NotBefore: at, NotOnOrAfter: at.Add(time.Minute), AuthnInstant: at, AuthnContext: "urn:test:certificate", HolderOfKeyCertificate: client.cert.Raw}} + response, err := protocol.SignResponse(t.Context(), spec, protocol.SigningKey{Key: idpKey.signer, Canonicalization: algorithm}) + if err != nil { + t.Fatal(err) + } + envelope, err := binding.WrapSOAP(t.Context(), response) + if err != nil { + t.Fatal(err) + } + path := write(t, filepath.Join(f.work, "hok-template.xml"), xmlBytes(t, envelope)) + for _, element := range []string{"Assertion", "Response"} { + out := filepath.Join(f.work, "hok-"+element+".xml") + selection := "(//*[local-name()='" + element + "']/*[local-name()='Signature'])[1]" + native(t, f, true, nameSign, "--privkey-pem", idpKey.private, flagIDAttrID, element, "--node-xpath", selection, flagNativeOutput, out, path) + path = out + } + m, err := binding.DecodeSOAP(t.Context(), read(t, path), binding.SOAPOptions{}) + if err != nil { + t.Fatal(err) + } + signed, err := proof.SignCS01CompatXML(t.Context(), m.Envelope(), proof.Signing{Key: client.signer, Certificate: client.cert.Raw}) + if err != nil { + t.Fatal(err) + } + raw := xmlBytes(t, signed) + path = write(t, filepath.Join(f.work, "hok-proof.xml"), raw) + for _, element := range []string{"Assertion", "Response"} { + selection := "(//*[local-name()='" + element + "']/*[local-name()='Signature'])[1]" + native(t, f, true, nameVerify, flagPublicKeyPEM, idpKey.public, flagIDAttrID, element, "--node-xpath", selection, path) + } + p, err := proof.VerifyCS01CompatXML(t.Context(), raw, proof.Options{}) + if err != nil { + t.Fatal(err) + } + body, err := p.Payload(t.Context()) + if err != nil { + t.Fatal(err) + } + assertion, err := protocol.VerifyResponse(t.Context(), body, []crypto.PublicKey{idpKey.signer.Public()}, protocol.ResponseOptions{Issuer: spec.Issuer, Audience: spec.Audience, Destination: spec.Destination, ExpectedRequestID: spec.InResponseTo, At: at, Proof: p, RequireHolderOfKey: true}) + if err != nil || assertion.NameID() != "alice" || !bytes.Equal(assertion.HolderOfKeyCertificate(), client.cert.Raw) { + t.Fatalf("independent HoK assertion: %v", err) + } + }) + } +} diff --git a/tests/interop/signatures_test.go b/tests/interop/signatures_test.go index 9c282d8..7df2249 100644 --- a/tests/interop/signatures_test.go +++ b/tests/interop/signatures_test.go @@ -90,7 +90,7 @@ func TestXMLSecX509DataGroups(t *testing.T) { // of the Go XML builder. Scratch CA files and generated XML stay in TempDir. func x509DataIdentity(t *testing.T, fixture *interopFixture) (testKey, []byte) { t.Helper() - key := generateKey(t, fixture.work, nameRsaSha256) + key := generateKey(t, fixture.work, nameRSASHA256) dir := t.TempDir() certificate := filepath.Join(dir, "certificate.pem") command(t, fixture, true, nil, "openssl", "req", "-new", "-x509", "-key", key.private, "-out", certificate, "-subj", "/CN=X509Data interop", "-days", "1", "-set_serial", "123456789012345678901234567890", "-addext", "subjectKeyIdentifier=hash", "-addext", "basicConstraints=critical,CA:TRUE", "-addext", "keyUsage=critical,digitalSignature,keyCertSign,cRLSign") @@ -256,7 +256,7 @@ func TestLegacySignatureFixtures(t *testing.T) { verifyJava(t, java(t, fixture, "CoreOracle", "verify-legacy-hmac", "-", ours, "0"), true) bad := write(t, filepath.Join(fixture.work, "changed-hmac-sha1.xml"), bytes.Replace(output, []byte("legacy fixture"), []byte("changed fixture"), 1)) verifyJava(t, java(t, fixture, "CoreOracle", "verify-legacy-hmac", "-", bad, "0"), false) - key := generateKey(t, fixture.work, nameRsaSha256) + key := generateKey(t, fixture.work, nameRSASHA256) input, err := document.Parse(t.Context(), []byte(`SHA-1 reference`), document.DefaultLimits()) if err != nil { @@ -340,9 +340,12 @@ func TestXMLSecStage4Vectors(t *testing.T) { } `json:"uri_contexts"` } decode(t, read(t, filepath.Join(fixture.root, "testdata", "next", "dsig-stage4-vectors.json")), &corpus) + if len(corpus.Cases) != 11 || len(corpus.Contexts) != 4 { + t.Fatalf("stage4 fixture counts = %d cases, %d URI contexts; want 11, 4", len(corpus.Cases), len(corpus.Contexts)) + } public := write(t, filepath.Join(fixture.work, "stage4-public.pem"), []byte(corpus.PublicKey)) - key := generateKey(t, fixture.work, nameRsaSha256) + key := generateKey(t, fixture.work, nameRSASHA256) for _, test := range corpus.Cases { t.Run(test.Name, func(t *testing.T) { stage4InteropExchange(t, fixture, test, public, key, corpus.Certificate) }) } @@ -383,7 +386,7 @@ func stage4InteropExchange(t *testing.T, fixture *interopFixture, test stage4Int args := append([]string{nameVerify, flagInsecure, "--verbose", "--print-debug", "--store-references", flagIDAttrID, stage4Payload, flagMixedCaseIDAttribute, ds + ":KeyInfo"}, stage4NativeResources(t, nativeResources)...) if test.Kind == stage4Manifest { - args = append(args, "--lax-key-search", flagPubkeyPem, public) + args = append(args, "--lax-key-search", flagPublicKeyPEM, public) } path := write(t, filepath.Join(dir, "independent.xml"), []byte(test.XML)) @@ -456,7 +459,7 @@ func stage4InteropExchange(t *testing.T, fixture *interopFixture, test stage4Int verify := append([]string{nameVerify, flagInsecure, flagIDAttrID, stage4Payload, flagMixedCaseIDAttribute, ds + ":KeyInfo"}, stage4NativeResources(t, nativeResources)...) if test.Kind == stage4Manifest { - verify = append(verify, "--lax-key-search", flagPubkeyPem, key.public) + verify = append(verify, "--lax-key-search", flagPublicKeyPEM, key.public) } command(t, fixture, true, nil, xmlsecExecutable(), append(verify, goPath)...) @@ -472,7 +475,7 @@ func stage4InteropExchange(t *testing.T, fixture *interopFixture, test stage4Int func TestXMLSecRetrievalMethods(t *testing.T) { fixture := newInteropFixture(t) selected(t, "xmlsec") - key := generateKey(t, fixture.work, nameRsaSha256) + key := generateKey(t, fixture.work, nameRSASHA256) publicInfo, err := keyinfo.Build(t.Context(), keyinfo.Template{ID: "shared", Certificates: [][]byte{key.cert.Raw}}) if err != nil { @@ -509,7 +512,7 @@ func TestXMLSecRetrievalMethods(t *testing.T) { t.Run(test.name, func(t *testing.T) { retrievalExchange{fixture, key, publicInfo, test}.run(t) }) } - ecKey := generateKey(t, fixture.work, nameEcdsaSha256) + ecKey := generateKey(t, fixture.work, nameECDSASHA256) for _, format := range []keyinfo.KeyFormat{keyinfo.XMLKeyValue, keyinfo.DERKeyValue} { test := keyRetrievalRecipe(t, ecKey, "EC-", format) t.Run(test.name, func(t *testing.T) { retrievalExchange{fixture, ecKey, publicInfo, test}.run(t) }) @@ -575,7 +578,7 @@ func (exchange retrievalExchange) run(t *testing.T) { // then verify core cryptography with an explicit key; Go checks retrieval. if test.typ != ds+"X509Data" && test.typ != keyinfo.RawX509Certificate { native(t, fixture, false, nameVerify, append(append([]string(nil), args...), goPath)...) - args = append(args, flagPubkeyPem, key.public) + args = append(args, flagPublicKeyPEM, key.public) } native(t, fixture, true, nameVerify, append(append([]string(nil), args...), goPath)...) @@ -683,7 +686,7 @@ func TestDetachedSignatures(t *testing.T) { verificationManifest := write(t, filepath.Join(dir, "verify-resources.json"), encoded) count := 0 - for _, name := range []string{nameRsaSha256, nameRsaSha384, nameRsaSha512, nameEcdsaSha256, nameEcdsaSha384, nameEcdsaSha512} { + for _, name := range []string{nameRSASHA256, nameRSASHA384, nameRSASHA512, nameECDSASHA256, nameECDSASHA384, nameECDSASHA512} { key := generateKey(t, dir, name) for _, cm := range canonicalizations() { ours, theirs := filepath.Join(dir, "go.xml"), filepath.Join(dir, "jdk.xml") @@ -726,6 +729,154 @@ func TestDetachedSignatures(t *testing.T) { t.Logf("%d detached exchanges each direction, %d references per signature, 6 bad-second controls", count, len(entries)) } +func TestXAdESBaselineFormats(t *testing.T) { + fixture := newInteropFixture(t) + selected(t, "xmlsec") + schema := xadesPublishedSchema(t) + key := generateKey(t, fixture.work, nameECDSASHA256) + extra := generateKey(t, t.TempDir(), nameECDSASHA384) + for _, method := range canonicalizations() { + for _, form := range []string{"nested-enveloped", "sibling", "enveloping", "external"} { + t.Run(method+"/"+form, func(t *testing.T) { + options := xades.BaselineSignOptions{Template: xmldsig.SignatureTemplate{ID: "baseline", SignatureMethod: key.method, Canonicalization: reference.Transform{Algorithm: method}, Certificates: [][]byte{key.cert.Raw, extra.cert.Raw}}, SignedPropertiesID: "properties", Properties: xades.BaselineProperties{SigningTime: time.Now().UTC(), SigningCertificateDigest: xmldsig.SHA512, AdditionalCertificates: []xades.CertificateIdentifier{{DER: extra.cert.Raw, DigestMethod: xmldsig.SHA384, IncludeIssuerSerial: true}}, ProductionPlace: &xades.ProductionPlace{City: "Perm", StreetAddress: "Main", CountryName: "RU"}, ClaimedRoles: []xades.PropertyValue{{Text: "author"}}, Commitments: []xades.CommitmentType{{Identifier: xades.ObjectIdentifier{Identifier: "urn:commitment:origin"}, All: true}}, Policy: &xades.SignaturePolicy{Identifier: xades.ObjectIdentifier{Identifier: "urn:policy:1"}, DigestMethod: xmldsig.SHA384, Qualifiers: []xades.PolicyQualifier{{URI: "https://example.test/policy"}, {Notice: &xades.PolicyNotice{Organization: "Example", NoticeNumbers: []string{"1", "-9999999999999999999999999999"}, ExplicitText: "terms"}}, {Specification: &xades.ObjectIdentifier{Identifier: "urn:oid:1.2.3", Qualifier: "OIDAsURN"}}}}}, Validation: xades.BaselineVerifyOptions{PolicyDocument: []byte("independent policy bytes")}} + var doc *document.Document + var parent document.Element + switch form { + case "nested-enveloped": + doc = xmlDoc(t, []byte(`123`)) + parent = doc.Root().Children()[0] + options.Template.References = []xmldsig.ReferenceTemplate{{ID: "data", URI: "#payload", Transforms: []reference.Transform{{Algorithm: reference.EnvelopedSignature}, {Algorithm: method}}}} + case "sibling": + doc = xmlDoc(t, []byte(`123`)) + parent = doc.Root().Children()[1] + options.Template.References = []xmldsig.ReferenceTemplate{{ID: "data", URI: "#payload", Transforms: []reference.Transform{{Algorithm: method}}}} + case "enveloping": + value := "123" + options.Template.Objects = []xmldsig.ObjectTemplate{{ID: "payload", Text: &value}} + options.Template.References = []xmldsig.ReferenceTemplate{{ID: "data", URI: "#payload", Transforms: []reference.Transform{{Algorithm: method}}}} + case "external": + path := write(t, filepath.Join(t.TempDir(), "payload.bin"), []byte("123")) + uri := (&url.URL{Scheme: "file", Path: path}).String() + options.Template.References = []xmldsig.ReferenceTemplate{{ID: "data", URI: uri}} + options.Validation.Signature.External = &xmldsig.ExternalReferences{AllowedURIs: []string{uri}, Resolver: xmldsig.ResolverFunc(func(context.Context, string) (io.ReadCloser, error) { + return io.NopCloser(strings.NewReader("123")), nil + })} + } + options.Properties.DataObjects = []xades.DataObjectFormat{{ObjectReference: "#data", Description: "data", MIMEType: "application/octet-stream", ObjectIdentifier: &xades.ObjectIdentifier{Identifier: "urn:oid:1.2.840.113549", Qualifier: "OIDAsURN", Documentation: []string{"https://example.test/format"}}}} + signed, result, err := xades.SignBaseline(t.Context(), doc, parent, key.signer, key.cert.Raw, options) + if err != nil { + t.Fatal(err) + } + path := write(t, filepath.Join(t.TempDir(), "baseline.xml"), xmlBytes(t, signed)) + arguments := []string{flagPublicKeyPEM, key.public, flagIDAttrID, "payload", flagMixedCaseIDAttribute, xades.Namespace + ":SignedProperties", flagMixedCaseIDAttribute, ds + ":Signature", flagMixedCaseIDAttribute, ds + ":Object", "--node-id", "baseline", flagEnabledReferenceURIs, "same-doc,local", path} + native(t, fixture, true, nameVerify, arguments...) + xadesSchemaFragment(t, fixture, schema, result.Basic().Signature().Signature(), true) + for _, reference := range result.CertificateReferences() { + hash := "-sha512" + if reference.DigestMethod() == xmldsig.SHA384 { + hash = "-sha384" + } + expected, _ := command(t, fixture, true, reference.CertificateDER(), "openssl", "dgst", hash, flagBinary) + if !bytes.Equal(reference.Digest(), expected) { + t.Fatal("OpenSSL certificate digest") + } + } + expected, _ := command(t, fixture, true, options.Validation.PolicyDocument, "openssl", "dgst", "-sha384", flagBinary) + if !bytes.Equal(result.SignaturePolicy().Digest, expected) { + t.Fatal("OpenSSL policy digest") + } + // The ready XMLSec engine independently signs the same property-bearing + // template; native validation then checks the exact policy/certificate binding. + draft, err := tree.Clone(t.Context(), signed) + if err != nil { + t.Fatal(err) + } + selectedSignature := xmldsig.Signatures(draft)[0] + _, sv := tree.Unwrap(selectedSignature.Children()[1]) + sv.Children = nil + for _, ref := range selectedSignature.Children()[0].Children()[2:] { + fields := ref.Children() + _, value := tree.Unwrap(fields[len(fields)-1]) + value.Children = nil + } + template := write(t, filepath.Join(t.TempDir(), "template.xml"), xmlBytes(t, draft)) + theirs := filepath.Join(t.TempDir(), "xmlsec.xml") + signArgs := []string{"--privkey-pem", key.private, flagIDAttrID, "payload", flagMixedCaseIDAttribute, xades.Namespace + ":SignedProperties", flagMixedCaseIDAttribute, ds + ":Signature", flagMixedCaseIDAttribute, ds + ":Object", "--node-id", "baseline", flagEnabledReferenceURIs, "same-doc,local", flagNativeOutput, theirs, template} + native(t, fixture, true, nameSign, signArgs...) + parsed := xmlDoc(t, read(t, theirs)) + if _, err := xades.VerifyBaseline(t.Context(), parsed, xmldsig.Signatures(parsed)[0], key.cert.Raw, options.Validation); err != nil { + t.Fatal(err) + } + if method == exc && form == "sibling" { + xadesPublishedSchemaRejects(t, fixture, schema, signed) + } + t.Log("P7_ORACLE xmlsec1 baseline topology/core; pinned ETSI XSD properties; OpenSSL certificate and policy digests") + }) + } + } +} + +func TestXAdESBaselineDataTimestampInputs(t *testing.T) { + fixture := newInteropFixture(t) + selected(t, "xmlsec") + schema := xadesPublishedSchema(t) + key := generateKey(t, fixture.work, nameECDSASHA256) + chain := testcert.New(t) + rootPEM := write(t, filepath.Join(fixture.work, "timestamp-root.pem"), pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: chain.Root.Raw})) + for _, method := range canonicalizations() { + for _, individual := range []bool{false, true} { + t.Run(method+"/"+strconv.FormatBool(individual), func(t *testing.T) { + source := xmlDoc(t, []byte(`content`)) + uri := `#xpointer(id('payload'))` + oracle, _, err := xmldsig.BuildTemplate(t.Context(), source, source.Root().Children()[1], xmldsig.SignatureTemplate{SignatureMethod: key.method, References: []xmldsig.ReferenceTemplate{{URI: uri, Transforms: []reference.Transform{{Algorithm: method}}}}}) + if err != nil { + t.Fatal(err) + } + template := write(t, filepath.Join(t.TempDir(), "oracle.xml"), xmlBytes(t, oracle)) + signedOracle := filepath.Join(t.TempDir(), "signed-oracle.xml") + command(t, fixture, true, nil, xmlsecExecutable(), nameSign, "--lax-key-search", "--privkey-pem", key.private, flagIDAttrID, "payload", flagNativeOutput, signedOracle, template) + output, stderr := command(t, fixture, true, nil, xmlsecExecutable(), nameVerify, "--lax-key-search", "--verbose", "--print-debug", "--store-references", flagPublicKeyPEM, key.public, flagIDAttrID, "payload", signedOracle) + buffers := regexp.MustCompile(`(?s)== PreDigest data - start buffer:\n(.*?)\n== PreDigest data - end buffer`).FindAllSubmatch(append(output, stderr...), -1) + if len(buffers) != 1 { + t.Fatalf("XMLSec exact canonical reference input missing: stdout=%s stderr=%s", output, stderr) + } + expected := buffers[0][1] + request, err := timestamp.CreateRequest(t.Context(), expected, timestamp.RequestOptions{Hash: crypto.SHA384}) + if err != nil { + t.Fatal(err) + } + response, err := timestamp.IssueResponse(t.Context(), request.DER(), chain.TSAKey, chain.TSA.Raw, timestamp.IssueOptions{Policy: []int{1, 2, 3, 4}, SerialNumber: big.NewInt(1), GenTime: testcert.Now(), ExtraCertificates: [][]byte{chain.Root.Raw}}) + if err != nil { + t.Fatal(err) + } + parsed, err := timestamp.ParseResponse(t.Context(), response) + if err != nil { + t.Fatal(err) + } + data := write(t, filepath.Join(t.TempDir(), "content.bin"), expected) + reply := write(t, filepath.Join(t.TempDir(), "response.tsr"), response) + command(t, fixture, true, nil, "openssl", "ts", flagVerify, flagIn, reply, "-data", data, "-CAfile", rootPEM, "-attime", strconv.FormatInt(testcert.Now().Unix(), 10)) + property := xades.DataTimestampProperty{Individual: individual, Canonicalization: reference.Transform{Algorithm: method}, TokenDER: parsed.TokenDER()} + if individual { + property.ObjectReferences = []string{"#data"} + } + doc, result, err := xades.SignBaseline(t.Context(), source, source.Root().Children()[1], key.signer, key.cert.Raw, xades.BaselineSignOptions{Template: xmldsig.SignatureTemplate{SignatureMethod: key.method, References: []xmldsig.ReferenceTemplate{{ID: "data", URI: uri}}}, Properties: xades.BaselineProperties{SigningTime: time.Now().UTC(), DataTimestamps: []xades.DataTimestampProperty{property}}}) + if err != nil { + t.Fatal(err) + } + var actual bytes.Buffer + if _, err := result.DataTimestamps()[0].WriteImprintInput(t.Context(), &actual); err != nil || !bytes.Equal(actual.Bytes(), expected) { + t.Fatal("timestamp bytes disagree with ready XMLSec canonicalization", actual.String(), string(expected), err) + } + xadesSchemaFragment(t, fixture, schema, result.Basic().Signature().Signature(), true) + path := write(t, filepath.Join(t.TempDir(), "baseline.xml"), xmlBytes(t, doc)) + native(t, fixture, true, nameVerify, flagPublicKeyPEM, key.public, flagIDAttrID, "payload", flagMixedCaseIDAttribute, xades.Namespace+":SignedProperties", path) + t.Log("P7_ORACLE XMLSec exact final-node-set canonicalization; OpenSSL RFC3161 SHA384 token; published XAdES Include/encapsulation schema") + }) + } + } +} + type javaVerdict struct { Core bool `json:"core"` Valid bool `json:"valid"` @@ -758,7 +909,7 @@ func coreSignatures(t *testing.T, fixture *interopFixture, externalXMLSec bool) t.Helper() cli := binary(t, fixture, "xmlsec") - names := []string{nameRsaSha256, nameRsaSha384, nameRsaSha512, nameEcdsaSha256, nameEcdsaSha384, nameEcdsaSha512} + names := []string{nameRSASHA256, nameRSASHA384, nameRSASHA512, nameECDSASHA256, nameECDSASHA384, nameECDSASHA512} if externalXMLSec { names = append(names, "rsa-pss256", "rsa-pss384", "rsa-pss512", nameEd25519) } @@ -790,7 +941,7 @@ func (suite *coreSignatureSuite) algorithm(t *testing.T, name string, number int keyName := name if strings.HasPrefix(name, "rsa-pss") { - keyName = nameRsaSha256 + keyName = nameRSASHA256 } key := generateKey(t, dir, keyName) @@ -894,7 +1045,7 @@ func (scenario coreSignatureCase) signExchange(t *testing.T, exchange coreSignat input = exchange.theirs } - args = append([]string{"--privkey-pem", scenario.key.private, flagEnabledReferenceUris, nameEmptySameDoc}, idArgs()...) + args = append([]string{"--privkey-pem", scenario.key.private, flagEnabledReferenceURIs, nameEmptySameDoc}, idArgs()...) args = append(args, "--node-id", "s"+strconv.Itoa(index), "--output", exchange.theirs, input) native(t, scenario.suite.fixture, true, nameSign, args...) } @@ -910,7 +1061,7 @@ func (scenario coreSignatureCase) verifyExchange(t *testing.T, exchange coreSign for index := range exchange.total { if scenario.suite.externalXMLSec { - args := append([]string{flagPubkeyPem, scenario.key.public, flagEnabledReferenceUris, nameEmptySameDoc}, idArgs()...) + args := append([]string{flagPublicKeyPEM, scenario.key.public, flagEnabledReferenceURIs, nameEmptySameDoc}, idArgs()...) native(t, scenario.suite.fixture, true, nameVerify, append(args, "--node-id", "s"+strconv.Itoa(index), exchange.ours)...) } else { verifyJava(t, java(t, scenario.suite.fixture, "CoreOracle", nameVerify, scenario.key.public, exchange.ours, strconv.Itoa(index)), true) @@ -933,7 +1084,7 @@ func (scenario coreSignatureCase) rejectTamper(t *testing.T, exchange coreSignat } bad := write(t, filepath.Join(scenario.dir, "bad.xml"), changed) - args := append([]string{flagPubkeyPem, scenario.key.public, flagEnabledReferenceUris, nameEmptySameDoc}, idArgs()...) + args := append([]string{flagPublicKeyPEM, scenario.key.public, flagEnabledReferenceURIs, nameEmptySameDoc}, idArgs()...) native(t, scenario.suite.fixture, false, nameVerify, append(args, "--node-id", "s0", bad)...) raw, _ := command(t, scenario.suite.fixture, false, nil, scenario.suite.cli, "verify-core", flagKey, scenario.key.public, "-signature-index", "0", "-modern-algorithms", "-c14n", exchange.recipe.cm, flagIn, bad) verifyReport(t, raw, false) @@ -1068,93 +1219,6 @@ func native(t *testing.T, fixture *interopFixture, success bool, mode string, ar return out } -func TestXAdESBaselineFormats(t *testing.T) { - fixture := newInteropFixture(t) - selected(t, "xmlsec") - schema := xadesPublishedSchema(t) - key := generateKey(t, fixture.work, nameEcdsaSha256) - extra := generateKey(t, t.TempDir(), nameEcdsaSha384) - for _, method := range canonicalizations() { - for _, form := range []string{"nested-enveloped", "sibling", "enveloping", "external"} { - t.Run(method+"/"+form, func(t *testing.T) { - options := xades.BaselineSignOptions{Template: xmldsig.SignatureTemplate{ID: "baseline", SignatureMethod: key.method, Canonicalization: reference.Transform{Algorithm: method}, Certificates: [][]byte{key.cert.Raw, extra.cert.Raw}}, SignedPropertiesID: "properties", Properties: xades.BaselineProperties{SigningTime: time.Now().UTC(), SigningCertificateDigest: xmldsig.SHA512, AdditionalCertificates: []xades.CertificateIdentifier{{DER: extra.cert.Raw, DigestMethod: xmldsig.SHA384, IncludeIssuerSerial: true}}, ProductionPlace: &xades.ProductionPlace{City: "Perm", StreetAddress: "Main", CountryName: "RU"}, ClaimedRoles: []xades.PropertyValue{{Text: "author"}}, Commitments: []xades.CommitmentType{{Identifier: xades.ObjectIdentifier{Identifier: "urn:commitment:origin"}, All: true}}, Policy: &xades.SignaturePolicy{Identifier: xades.ObjectIdentifier{Identifier: "urn:policy:1"}, DigestMethod: xmldsig.SHA384, Qualifiers: []xades.PolicyQualifier{{URI: "https://example.test/policy"}, {Notice: &xades.PolicyNotice{Organization: "Example", NoticeNumbers: []string{"1", "-9999999999999999999999999999"}, ExplicitText: "terms"}}, {Specification: &xades.ObjectIdentifier{Identifier: "urn:oid:1.2.3", Qualifier: "OIDAsURN"}}}}}, Validation: xades.BaselineVerifyOptions{PolicyDocument: []byte("independent policy bytes")}} - var doc *document.Document - var parent document.Element - switch form { - case "nested-enveloped": - doc = xmlDoc(t, []byte(`123`)) - parent = doc.Root().Children()[0] - options.Template.References = []xmldsig.ReferenceTemplate{{ID: "data", URI: "#payload", Transforms: []reference.Transform{{Algorithm: reference.EnvelopedSignature}, {Algorithm: method}}}} - case "sibling": - doc = xmlDoc(t, []byte(`123`)) - parent = doc.Root().Children()[1] - options.Template.References = []xmldsig.ReferenceTemplate{{ID: "data", URI: "#payload", Transforms: []reference.Transform{{Algorithm: method}}}} - case "enveloping": - value := "123" - options.Template.Objects = []xmldsig.ObjectTemplate{{ID: "payload", Text: &value}} - options.Template.References = []xmldsig.ReferenceTemplate{{ID: "data", URI: "#payload", Transforms: []reference.Transform{{Algorithm: method}}}} - case "external": - path := write(t, filepath.Join(t.TempDir(), "payload.bin"), []byte("123")) - uri := (&url.URL{Scheme: "file", Path: path}).String() - options.Template.References = []xmldsig.ReferenceTemplate{{ID: "data", URI: uri}} - options.Validation.Signature.External = &xmldsig.ExternalReferences{AllowedURIs: []string{uri}, Resolver: xmldsig.ResolverFunc(func(context.Context, string) (io.ReadCloser, error) { - return io.NopCloser(strings.NewReader("123")), nil - })} - } - options.Properties.DataObjects = []xades.DataObjectFormat{{ObjectReference: "#data", Description: "data", MIMEType: "application/octet-stream", ObjectIdentifier: &xades.ObjectIdentifier{Identifier: "urn:oid:1.2.840.113549", Qualifier: "OIDAsURN", Documentation: []string{"https://example.test/format"}}}} - signed, result, err := xades.SignBaseline(t.Context(), doc, parent, key.signer, key.cert.Raw, options) - if err != nil { - t.Fatal(err) - } - path := write(t, filepath.Join(t.TempDir(), "baseline.xml"), xmlBytes(t, signed)) - arguments := []string{flagPubkeyPem, key.public, flagIDAttrID, "payload", flagMixedCaseIDAttribute, xades.Namespace + ":SignedProperties", flagMixedCaseIDAttribute, ds + ":Signature", flagMixedCaseIDAttribute, ds + ":Object", "--node-id", "baseline", flagEnabledReferenceUris, "same-doc,local", path} - native(t, fixture, true, nameVerify, arguments...) - xadesSchemaFragment(t, fixture, schema, result.Basic().Signature().Signature(), true) - for _, reference := range result.CertificateReferences() { - hash := "-sha512" - if reference.DigestMethod() == xmldsig.SHA384 { - hash = "-sha384" - } - expected, _ := command(t, fixture, true, reference.CertificateDER(), "openssl", "dgst", hash, flagBinary) - if !bytes.Equal(reference.Digest(), expected) { - t.Fatal("OpenSSL certificate digest") - } - } - expected, _ := command(t, fixture, true, options.Validation.PolicyDocument, "openssl", "dgst", "-sha384", flagBinary) - if !bytes.Equal(result.SignaturePolicy().Digest, expected) { - t.Fatal("OpenSSL policy digest") - } - // The ready XMLSec engine independently signs the same property-bearing - // template; native validation then checks the exact policy/certificate binding. - draft, err := tree.Clone(t.Context(), signed) - if err != nil { - t.Fatal(err) - } - selectedSignature := xmldsig.Signatures(draft)[0] - _, sv := tree.Unwrap(selectedSignature.Children()[1]) - sv.Children = nil - for _, ref := range selectedSignature.Children()[0].Children()[2:] { - fields := ref.Children() - _, value := tree.Unwrap(fields[len(fields)-1]) - value.Children = nil - } - template := write(t, filepath.Join(t.TempDir(), "template.xml"), xmlBytes(t, draft)) - theirs := filepath.Join(t.TempDir(), "xmlsec.xml") - signArgs := []string{"--privkey-pem", key.private, flagIDAttrID, "payload", flagMixedCaseIDAttribute, xades.Namespace + ":SignedProperties", flagMixedCaseIDAttribute, ds + ":Signature", flagMixedCaseIDAttribute, ds + ":Object", "--node-id", "baseline", flagEnabledReferenceUris, "same-doc,local", flagNativeOutput, theirs, template} - native(t, fixture, true, nameSign, signArgs...) - parsed := xmlDoc(t, read(t, theirs)) - if _, err := xades.VerifyBaseline(t.Context(), parsed, xmldsig.Signatures(parsed)[0], key.cert.Raw, options.Validation); err != nil { - t.Fatal(err) - } - if method == exc && form == "sibling" { - xadesPublishedSchemaRejects(t, fixture, schema, signed) - } - t.Log("P7_ORACLE xmlsec1 baseline topology/core; pinned ETSI XSD properties; OpenSSL certificate and policy digests") - }) - } - } -} - func xadesPublishedSchema(t *testing.T) string { t.Helper() dir := os.Getenv("XADES_SCHEMA_DIR") @@ -1227,64 +1291,3 @@ func xadesPublishedSchemaRejects(t *testing.T, fixture *interopFixture, schema s }) } } - -func TestXAdESBaselineDataTimestampInputs(t *testing.T) { - fixture := newInteropFixture(t) - selected(t, "xmlsec") - schema := xadesPublishedSchema(t) - key := generateKey(t, fixture.work, nameEcdsaSha256) - chain := testcert.New(t) - rootPEM := write(t, filepath.Join(fixture.work, "timestamp-root.pem"), pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: chain.Root.Raw})) - for _, method := range canonicalizations() { - for _, individual := range []bool{false, true} { - t.Run(method+"/"+strconv.FormatBool(individual), func(t *testing.T) { - source := xmlDoc(t, []byte(`content`)) - uri := `#xpointer(id('payload'))` - oracle, _, err := xmldsig.BuildTemplate(t.Context(), source, source.Root().Children()[1], xmldsig.SignatureTemplate{SignatureMethod: key.method, References: []xmldsig.ReferenceTemplate{{URI: uri, Transforms: []reference.Transform{{Algorithm: method}}}}}) - if err != nil { - t.Fatal(err) - } - template := write(t, filepath.Join(t.TempDir(), "oracle.xml"), xmlBytes(t, oracle)) - signedOracle := filepath.Join(t.TempDir(), "signed-oracle.xml") - command(t, fixture, true, nil, xmlsecExecutable(), nameSign, "--lax-key-search", "--privkey-pem", key.private, flagIDAttrID, "payload", flagNativeOutput, signedOracle, template) - output, stderr := command(t, fixture, true, nil, xmlsecExecutable(), nameVerify, "--lax-key-search", "--verbose", "--print-debug", "--store-references", flagPubkeyPem, key.public, flagIDAttrID, "payload", signedOracle) - buffers := regexp.MustCompile(`(?s)== PreDigest data - start buffer:\n(.*?)\n== PreDigest data - end buffer`).FindAllSubmatch(append(output, stderr...), -1) - if len(buffers) != 1 { - t.Fatalf("XMLSec exact canonical reference input missing: stdout=%s stderr=%s", output, stderr) - } - expected := buffers[0][1] - request, err := timestamp.CreateRequest(t.Context(), expected, timestamp.RequestOptions{Hash: crypto.SHA384}) - if err != nil { - t.Fatal(err) - } - response, err := timestamp.IssueResponse(t.Context(), request.DER(), chain.TSAKey, chain.TSA.Raw, timestamp.IssueOptions{Policy: []int{1, 2, 3, 4}, SerialNumber: big.NewInt(1), GenTime: testcert.Now(), ExtraCertificates: [][]byte{chain.Root.Raw}}) - if err != nil { - t.Fatal(err) - } - parsed, err := timestamp.ParseResponse(t.Context(), response) - if err != nil { - t.Fatal(err) - } - data := write(t, filepath.Join(t.TempDir(), "content.bin"), expected) - reply := write(t, filepath.Join(t.TempDir(), "response.tsr"), response) - command(t, fixture, true, nil, "openssl", "ts", flagVerify, flagIn, reply, "-data", data, "-CAfile", rootPEM, "-attime", strconv.FormatInt(testcert.Now().Unix(), 10)) - property := xades.DataTimestampProperty{Individual: individual, Canonicalization: reference.Transform{Algorithm: method}, TokenDER: parsed.TokenDER()} - if individual { - property.ObjectReferences = []string{"#data"} - } - doc, result, err := xades.SignBaseline(t.Context(), source, source.Root().Children()[1], key.signer, key.cert.Raw, xades.BaselineSignOptions{Template: xmldsig.SignatureTemplate{SignatureMethod: key.method, References: []xmldsig.ReferenceTemplate{{ID: "data", URI: uri}}}, Properties: xades.BaselineProperties{SigningTime: time.Now().UTC(), DataTimestamps: []xades.DataTimestampProperty{property}}}) - if err != nil { - t.Fatal(err) - } - var actual bytes.Buffer - if _, err := result.DataTimestamps()[0].WriteImprintInput(t.Context(), &actual); err != nil || !bytes.Equal(actual.Bytes(), expected) { - t.Fatal("timestamp bytes disagree with ready XMLSec canonicalization", actual.String(), string(expected), err) - } - xadesSchemaFragment(t, fixture, schema, result.Basic().Signature().Signature(), true) - path := write(t, filepath.Join(t.TempDir(), "baseline.xml"), xmlBytes(t, doc)) - native(t, fixture, true, nameVerify, flagPubkeyPem, key.public, flagIDAttrID, "payload", flagMixedCaseIDAttribute, xades.Namespace+":SignedProperties", path) - t.Log("P7_ORACLE XMLSec exact final-node-set canonicalization; OpenSSL RFC3161 SHA384 token; published XAdES Include/encapsulation schema") - }) - } - } -} diff --git a/tests/interop/testdata/ECPProofOracle.java b/tests/interop/testdata/ECPProofOracle.java new file mode 100644 index 0000000..87afd3d --- /dev/null +++ b/tests/interop/testdata/ECPProofOracle.java @@ -0,0 +1,70 @@ +// Whole-SOAP proof producer using the independent JDK JSR 105 implementation. +// It declares the standard enveloped transform; it does not implement the +// nonstandard CS01 self-exclusion interpretation. +import java.nio.file.*; +import java.security.cert.*; +import java.util.*; +import javax.xml.XMLConstants; +import javax.xml.crypto.dsig.*; +import javax.xml.crypto.dsig.dom.*; +import javax.xml.crypto.dsig.spec.*; +import javax.xml.transform.*; +import javax.xml.transform.dom.*; +import javax.xml.transform.stream.*; +import org.w3c.dom.*; + +public class ECPProofOracle { + static final String SOAP = "http://schemas.xmlsoap.org/soap/envelope/"; + static final String WSSE = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"; + + public static void main(String[] args) throws Exception { + // private.pem certificate.pem input.xml output.xml C14N signature-method + Document document = DetachedOracle.builder().parse(Path.of(args[2]).toFile()); + Element root = document.getDocumentElement(); + if (!SOAP.equals(root.getNamespaceURI()) || !"Envelope".equals(root.getLocalName())) + throw new IllegalArgumentException("SOAP Envelope required"); + Element header = null; + for (Node n = root.getFirstChild(); n != null; n = n.getNextSibling()) { + if (n instanceof Element e && SOAP.equals(e.getNamespaceURI()) && "Header".equals(e.getLocalName())) + header = e; + } + if (header == null) { + header = document.createElementNS(SOAP, "soap:Header"); + header.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:soap", SOAP); + root.insertBefore(header, root.getFirstChild()); + } + if (document.getElementsByTagNameNS(WSSE, "Security").getLength() != 0) + throw new IllegalArgumentException("existing proof header"); + Element security = document.createElementNS(WSSE, "wsse:Security"); + security.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:wsse", WSSE); + security.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:soap", SOAP); + security.setAttributeNS(SOAP, "soap:actor", "http://schemas.xmlsoap.org/soap/actor/next"); + security.setAttributeNS(SOAP, "soap:mustUnderstand", "1"); + header.appendChild(security); + XMLSignatureFactory factory = XMLSignatureFactory.getInstance("DOM"); + Reference reference = factory.newReference("", + factory.newDigestMethod(DigestMethod.SHA256, null), + List.of(factory.newTransform(Transform.ENVELOPED, (TransformParameterSpec) null)), null, null); + SignedInfo info = factory.newSignedInfo( + factory.newCanonicalizationMethod(args[4], (C14NMethodParameterSpec) null), + factory.newSignatureMethod(args[5], null), List.of(reference)); + X509Certificate certificate; + try (var input = Files.newInputStream(Path.of(args[1]))) { + certificate = (X509Certificate) CertificateFactory.getInstance("X.509").generateCertificate(input); + } + var keyInfo = factory.getKeyInfoFactory().newKeyInfo( + List.of(factory.getKeyInfoFactory().newX509Data(List.of(certificate)))); + DOMSignContext context = new DOMSignContext(DetachedOracle.key(Path.of(args[0]), true), security); + context.setDefaultNamespacePrefix("proofds"); + context.setProperty("org.jcp.xml.dsig.secureValidation", Boolean.TRUE); + context.setURIDereferencer(CoreOracle.resolver(factory)); + factory.newXMLSignature(info, keyInfo).sign(context); + TransformerFactory transformers = TransformerFactory.newInstance(); + transformers.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, ""); + transformers.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, ""); + Transformer serializer = transformers.newTransformer(); + serializer.setOutputProperty(OutputKeys.INDENT, "no"); + serializer.transform(new DOMSource(document), new StreamResult(Path.of(args[3]).toFile())); + System.out.println("JDK whole-SOAP proof signed"); + } +} diff --git a/tests/interop/testdata/OpenSAMLAuthorityOracle.java b/tests/interop/testdata/OpenSAMLAuthorityOracle.java new file mode 100644 index 0000000..7ea2a44 --- /dev/null +++ b/tests/interop/testdata/OpenSAMLAuthorityOracle.java @@ -0,0 +1,90 @@ +// Independent OpenSAML 5.2.3 builders, schema validator and signature engine. +// No SAML parser, XML canonicalizer or cryptographic implementation is supplied here. +import java.nio.file.*; +import java.security.*; +import java.time.Instant; +import javax.xml.namespace.QName; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.TransformerFactory; +import org.opensaml.core.Version; +import org.opensaml.core.config.InitializationService; +import org.opensaml.core.xml.XMLObject; +import org.opensaml.core.xml.schema.XSString; +import org.opensaml.core.xml.schema.impl.XSStringBuilder; +import org.opensaml.core.xml.util.XMLObjectSupport; +import org.opensaml.saml.common.*; +import org.opensaml.saml.common.xml.SAMLSchemaBuilder; +import org.opensaml.saml.saml2.core.*; +import org.opensaml.security.x509.BasicX509Credential; +import org.opensaml.xmlsec.signature.support.Signer; +import org.w3c.dom.*; + +public class OpenSAMLAuthorityOracle { + static final String NS = "urn:oasis:names:tc:SAML:2.0:protocol"; + static XMLObject object(QName name) { return XMLObjectSupport.buildXMLObject(name); } + static Issuer issuer(String name) { Issuer i=(Issuer)object(Issuer.DEFAULT_ELEMENT_NAME);i.setValue(name);return i; } + static Subject subject() { + Subject s=(Subject)object(Subject.DEFAULT_ELEMENT_NAME); + NameID n=(NameID)object(NameID.DEFAULT_ELEMENT_NAME);n.setValue("alice");s.setNameID(n);return s; + } + static Subject copySubject(Subject original) { + Subject s=(Subject)object(Subject.DEFAULT_ELEMENT_NAME);NameID old=original.getNameID(); + NameID n=(NameID)object(NameID.DEFAULT_ELEMENT_NAME);n.setValue(old.getValue());n.setFormat(old.getFormat());n.setNameQualifier(old.getNameQualifier());n.setSPNameQualifier(old.getSPNameQualifier());n.setSPProvidedID(old.getSPProvidedID());s.setNameID(n);return s; + } + static BasicX509Credential credential(String key,String cert) throws Exception { + return new BasicX509Credential(OpenSAMLHoKOracle.certificate(cert),(PrivateKey)DetachedOracle.key(Path.of(key),true)); + } + static void sign(SignableSAMLObject value,BasicX509Credential key) throws Exception { + value.setSignature(OpenSAMLHoKOracle.signing(key));XMLObjectSupport.marshall(value);Signer.signObject(value.getSignature()); + } + static void write(XMLObject object,String file) throws Exception { + Element root=XMLObjectSupport.marshall(object); + new SAMLSchemaBuilder(SAMLSchemaBuilder.SAML1Version.SAML_11).getSAMLSchema().newValidator().validate(new DOMSource(root)); + TransformerFactory.newInstance().newTransformer().transform(new DOMSource(root),new StreamResult(Path.of(file).toFile())); + } + static XMLObject read(String file) throws Exception { + Document d=DetachedOracle.builder().parse(Path.of(file).toFile()); + new SAMLSchemaBuilder(SAMLSchemaBuilder.SAML1Version.SAML_11).getSAMLSchema().newValidator().validate(new DOMSource(d)); + return XMLObjectSupport.getUnmarshaller(d.getDocumentElement()).unmarshall(d.getDocumentElement()); + } + static void verify(XMLObject value,PublicKey key) throws Exception { + if (value instanceof SignableSAMLObject s) OpenSAMLHoKOracle.signature(s.getSignature(),key); + if (value instanceof Response response) for (Assertion a:response.getAssertions()) verify(a,key); + } + static void query(String[] a) throws Exception { + // query kind key cert out requester endpoint instant + RequestAbstractType q=(RequestAbstractType)object(new QName(NS,a[1],"samlp")); + q.setID("_oracle_query_"+java.util.UUID.randomUUID());q.setVersion(SAMLVersion.VERSION_20);q.setIssuer(issuer(a[5]));q.setDestination(a[6]);q.setIssueInstant(Instant.parse(a[7])); + if(q instanceof SubjectQuery s)s.setSubject(subject()); + if(q instanceof AuthnQuery s){s.setSessionIndex("past-session");RequestedAuthnContext c=(RequestedAuthnContext)object(RequestedAuthnContext.DEFAULT_ELEMENT_NAME);AuthnContextClassRef r=(AuthnContextClassRef)object(AuthnContextClassRef.DEFAULT_ELEMENT_NAME);r.setURI("urn:example:strong");c.getAuthnContextClassRefs().add(r);s.setRequestedAuthnContext(c);} + if(q instanceof AttributeQuery s){Attribute v=(Attribute)object(Attribute.DEFAULT_ELEMENT_NAME);v.setName("role");s.getAttributes().add(v);} + if(q instanceof AuthzDecisionQuery s){s.setResource("urn:example:resource");Action v=(Action)object(Action.DEFAULT_ELEMENT_NAME);v.setNamespace("urn:example:actions");v.setValue("read");s.getActions().add(v);} + if(q instanceof AssertionIDRequest s){AssertionIDRef ref=(AssertionIDRef)object(AssertionIDRef.DEFAULT_ELEMENT_NAME);ref.setValue("_held");s.getAssertionIDRefs().add(ref);} + if(q instanceof NameIDMappingRequest s){NameID n=(NameID)object(NameID.DEFAULT_ELEMENT_NAME);n.setValue("alice");s.setNameID(n);NameIDPolicy p=(NameIDPolicy)object(NameIDPolicy.DEFAULT_ELEMENT_NAME);p.setFormat(NameID.PERSISTENT);p.setSPNameQualifier("https://target.test/entity");p.setAllowCreate(true);s.setNameIDPolicy(p);} + sign(q,credential(a[2],a[3]));write(q,a[4]); + } + static Status status(String code){Status s=(Status)object(Status.DEFAULT_ELEMENT_NAME);StatusCode c=(StatusCode)object(StatusCode.DEFAULT_ELEMENT_NAME);c.setValue(code);s.setStatusCode(c);return s;} + static void response(String[] a) throws Exception { + // response query key cert out authority instant + RequestAbstractType request=(RequestAbstractType)read(a[1]);Instant at=Instant.parse(a[6]);BasicX509Credential key=credential(a[2],a[3]); + if(request instanceof NameIDMappingRequest mapping){ + NameIDMappingResponse r=(NameIDMappingResponse)object(NameIDMappingResponse.DEFAULT_ELEMENT_NAME);r.setID("_oracle_mapping");r.setVersion(SAMLVersion.VERSION_20);r.setIssueInstant(at);r.setIssuer(issuer(a[5]));r.setInResponseTo(request.getID());r.setStatus(status(StatusCode.SUCCESS)); + NameID n=(NameID)object(NameID.DEFAULT_ELEMENT_NAME);n.setValue("mapped-alice");n.setFormat(mapping.getNameIDPolicy().getFormat());n.setSPNameQualifier(mapping.getNameIDPolicy().getSPNameQualifier());r.setNameID(n);sign(r,key);write(r,a[4]);return; + } + Response r=(Response)object(Response.DEFAULT_ELEMENT_NAME);r.setID("_oracle_response");r.setVersion(SAMLVersion.VERSION_20);r.setIssueInstant(at);r.setIssuer(issuer(a[5]));r.setInResponseTo(request.getID());r.setStatus(status(StatusCode.SUCCESS)); + for(int index=0;index<2;index++){ + Assertion assertion=(Assertion)object(Assertion.DEFAULT_ELEMENT_NAME);assertion.setID(request instanceof AssertionIDRequest?"_held":"_oracle_assertion_"+index);assertion.setVersion(SAMLVersion.VERSION_20);assertion.setIssueInstant(at);assertion.setIssuer(issuer(a[5])); + assertion.setSubject(request instanceof SubjectQuery q?copySubject(q.getSubject()):subject()); + if(request instanceof AuthnQuery q){AuthnStatement s=(AuthnStatement)object(AuthnStatement.DEFAULT_ELEMENT_NAME);s.setAuthnInstant(at.minusSeconds(3600));s.setSessionIndex(q.getSessionIndex());AuthnContext c=(AuthnContext)object(AuthnContext.DEFAULT_ELEMENT_NAME);AuthnContextClassRef ref=(AuthnContextClassRef)object(AuthnContextClassRef.DEFAULT_ELEMENT_NAME);ref.setURI("urn:example:strong");c.setAuthnContextClassRef(ref);s.setAuthnContext(c);assertion.getAuthnStatements().add(s);} + else if(request instanceof AuthzDecisionQuery q){AuthzDecisionStatement s=(AuthzDecisionStatement)object(AuthzDecisionStatement.DEFAULT_ELEMENT_NAME);s.setDecision(DecisionTypeEnumeration.PERMIT);s.setResource(q.getResource());for(Action action:q.getActions())s.getActions().add((Action)XMLObjectSupport.cloneXMLObject(action));assertion.getAuthzDecisionStatements().add(s);} + else {AttributeStatement s=(AttributeStatement)object(AttributeStatement.DEFAULT_ELEMENT_NAME);Attribute v=(Attribute)object(Attribute.DEFAULT_ELEMENT_NAME);v.setName("role");XSString value=new XSStringBuilder().buildObject(AttributeValue.DEFAULT_ELEMENT_NAME,XSString.TYPE_NAME);value.setValue("reader");v.getAttributeValues().add(value);s.getAttributes().add(v);assertion.getAttributeStatements().add(s);} + assertion.setSignature(OpenSAMLHoKOracle.signing(key));r.getAssertions().add(assertion);if(request instanceof AssertionIDRequest)break; + } + r.setSignature(OpenSAMLHoKOracle.signing(key));XMLObjectSupport.marshall(r);for(Assertion assertion:r.getAssertions())Signer.signObject(assertion.getSignature());Signer.signObject(r.getSignature());write(r,a[4]); + } + public static void main(String[] a) throws Exception { + InitializationService.initialize();if(!"5.2.3".equals(Version.getVersion()))throw new IllegalStateException("OpenSAML 5.2.3 required"); + switch(a[0]){case "query":query(a);break;case "response":response(a);break;case "schema":read(a[1]);System.out.println("schema valid");break;case "verify":verify(read(a[1]),(PublicKey)DetachedOracle.key(Path.of(a[2]),false));System.out.println("verified");break;default:throw new IllegalArgumentException("mode");} + } +} diff --git a/tests/interop/testdata/OpenSAMLHoKOracle.java b/tests/interop/testdata/OpenSAMLHoKOracle.java new file mode 100644 index 0000000..d8d3163 --- /dev/null +++ b/tests/interop/testdata/OpenSAMLHoKOracle.java @@ -0,0 +1,209 @@ +// Thin adapter around the released OpenSAML validator, builders and signer. +// No SAML parsing, X.509 matching or signature algorithm is reimplemented here. +import java.nio.file.*; +import java.security.*; +import java.security.cert.*; +import java.time.Instant; +import java.util.*; +import javax.xml.XMLConstants; +import javax.xml.transform.*; +import javax.xml.transform.dom.*; +import javax.xml.transform.stream.*; +import org.w3c.dom.*; +import org.opensaml.core.Version; +import org.opensaml.core.config.InitializationService; +import org.opensaml.core.xml.util.XMLObjectSupport; +import org.opensaml.saml.common.SAMLVersion; +import org.opensaml.saml.common.assertion.*; +import org.opensaml.saml.common.messaging.context.ChannelBindingsContext; +import org.opensaml.saml.common.profile.impl.VerifyChannelBindings; +import org.opensaml.saml.ext.saml2cb.ChannelBindings; +import org.opensaml.profile.context.ProfileRequestContext; +import org.opensaml.saml.saml2.assertion.SAML2AssertionValidationParameters; +import org.opensaml.saml.saml2.assertion.impl.HolderOfKeySubjectConfirmationValidator; +import org.opensaml.saml.saml2.core.*; +import org.opensaml.saml.security.impl.SAMLSignatureProfileValidator; +import org.opensaml.security.credential.BasicCredential; +import org.opensaml.security.x509.BasicX509Credential; +import org.opensaml.xmlsec.keyinfo.KeyInfoSupport; +import org.opensaml.xmlsec.signature.KeyInfo; +import org.opensaml.xmlsec.signature.Signature; +import org.opensaml.xmlsec.signature.support.*; +import org.opensaml.xmlsec.signature.support.Signer; +import org.opensaml.xmlsec.signature.support.SignatureException; + +public class OpenSAMLHoKOracle { + static final String SOAP = "http://schemas.xmlsoap.org/soap/envelope/"; + static final String NEXT = "http://schemas.xmlsoap.org/soap/actor/next"; + + static X509Certificate certificate(String file) throws Exception { + try (var input = Files.newInputStream(Path.of(file))) { + return (X509Certificate) CertificateFactory.getInstance("X.509").generateCertificate(input); + } + } + + static void signature(Signature signature, PublicKey key) throws Exception { + if (signature == null) throw new GeneralSecurityException("missing SAML signature"); + new SAMLSignatureProfileValidator().validate(signature); + SignatureValidator.validate(signature, new BasicCredential(key)); + } + + static Response response(String file) throws Exception { + Document document = DetachedOracle.builder().parse(Path.of(file).toFile()); + Element root = document.getDocumentElement(); + if (SOAP.equals(root.getNamespaceURI())) { + NodeList bodies = root.getElementsByTagNameNS(SOAP, "Body"); + if (bodies.getLength() != 1) throw new IllegalArgumentException("one SOAP body required"); + root = null; + for (Node n = bodies.item(0).getFirstChild(); n != null; n = n.getNextSibling()) { + if (n instanceof Element e) { + if (root != null) throw new IllegalArgumentException("one SOAP payload required"); + root = e; + } + } + } + return (Response) XMLObjectSupport.getUnmarshaller(root).unmarshall(root); + } + + static void verify(String[] args) throws Exception { + // verify input.xml idp-public.pem presenter-cert.pem recipient request-id + Response response = response(args[1]); + PublicKey key = (PublicKey) DetachedOracle.key(Path.of(args[2]), false); + if (response.getAssertions().size() != 1) throw new IllegalArgumentException("one assertion required"); + Assertion assertion = response.getAssertions().get(0); + try { + signature(response.getSignature(), key); + signature(assertion.getSignature(), key); + } catch (SignatureException rejected) { + System.out.println("{\"engine\":\"OpenSAML 5.2.3\",\"signatures\":false,\"confirmed\":false}"); + return; + } + Map params = new HashMap<>(); + params.put(SAML2AssertionValidationParameters.SC_HOK_PRESENTER_CERT, certificate(args[3])); + params.put(SAML2AssertionValidationParameters.SC_RECIPIENT_REQUIRED, true); + params.put(SAML2AssertionValidationParameters.SC_VALID_RECIPIENTS, Set.of(args[4])); + params.put(SAML2AssertionValidationParameters.SC_IN_RESPONSE_TO_REQUIRED, true); + params.put(SAML2AssertionValidationParameters.SC_VALID_IN_RESPONSE_TO, args[5]); + params.put(SAML2AssertionValidationParameters.SC_NOT_BEFORE_REQUIRED, false); + params.put(SAML2AssertionValidationParameters.SC_NOT_ON_OR_AFTER_REQUIRED, false); + params.put(SAML2AssertionValidationParameters.SC_ADDRESS_REQUIRED, false); + params.put(SAML2AssertionValidationParameters.SC_CHECK_ADDRESS, false); + boolean confirmed = false; + HolderOfKeySubjectConfirmationValidator validator = new HolderOfKeySubjectConfirmationValidator(); + for (SubjectConfirmation confirmation : assertion.getSubject().getSubjectConfirmations()) { + if (validator.validate(confirmation, assertion, new ValidationContext(params)) == ValidationResult.VALID) + confirmed = true; + } + System.out.println("{\"engine\":\"OpenSAML 5.2.3\",\"signatures\":true,\"confirmed\":" + confirmed + "}"); + } + + static Signature signing(BasicX509Credential credential) { + Signature signature = (Signature) XMLObjectSupport.buildXMLObject(Signature.DEFAULT_ELEMENT_NAME); + signature.setSigningCredential(credential); + signature.setSignatureAlgorithm(SignatureConstants.ALGO_ID_SIGNATURE_RSA_SHA256); + signature.setCanonicalizationAlgorithm(SignatureConstants.ALGO_ID_C14N_EXCL_OMIT_COMMENTS); + return signature; + } + + static void channels(String[] args) throws Exception { + // channels signed-type signed-base64 received-type received-base64 + ChannelBindingsContext signed = new ChannelBindingsContext(); + ChannelBindingsContext received = new ChannelBindingsContext(); + ChannelBindingsContext verified = new ChannelBindingsContext(); + if (!args[2].isEmpty()) { + ChannelBindings value = (ChannelBindings) XMLObjectSupport.buildXMLObject(ChannelBindings.DEFAULT_ELEMENT_NAME); + value.setType(args[1]); value.setValue(args[2]); signed.getChannelBindings().add(value); + } + if (!args[4].isEmpty()) { + ChannelBindings value = (ChannelBindings) XMLObjectSupport.buildXMLObject(ChannelBindings.DEFAULT_ELEMENT_NAME); + value.setType(args[3]); value.setValue(args[4]); received.getChannelBindings().add(value); + } + VerifyChannelBindings action = new VerifyChannelBindings(); + action.setChannelBindingsLookupStrategy1(context -> signed); + action.setChannelBindingsLookupStrategy2(context -> received); + action.setChannelBindingsCreationStrategy(context -> verified); + action.initialize(); + action.execute(new ProfileRequestContext()); + System.out.println("{\"matched\":" + !verified.getChannelBindings().isEmpty() + "}"); + } + + static void issue(String[] args) throws Exception { + // issue private.pem idp-cert.pem presenter-cert.pem output.xml issuer audience recipient request-id instant [alternative-cert.pem] + Instant at = Instant.parse(args[9]); + BasicX509Credential credential = new BasicX509Credential(certificate(args[2]), + (PrivateKey) DetachedOracle.key(Path.of(args[1]), true)); + Response response = (Response) XMLObjectSupport.buildXMLObject(Response.DEFAULT_ELEMENT_NAME); + response.setID("_opensaml_response_" + UUID.randomUUID()); response.setVersion(SAMLVersion.VERSION_20); + response.setIssueInstant(at); response.setDestination(args[7]); response.setInResponseTo(args[8]); + Issuer issuer = (Issuer) XMLObjectSupport.buildXMLObject(Issuer.DEFAULT_ELEMENT_NAME); + issuer.setValue(args[5]); response.setIssuer(issuer); + Status status = (Status) XMLObjectSupport.buildXMLObject(Status.DEFAULT_ELEMENT_NAME); + StatusCode code = (StatusCode) XMLObjectSupport.buildXMLObject(StatusCode.DEFAULT_ELEMENT_NAME); + code.setValue(StatusCode.SUCCESS); status.setStatusCode(code); response.setStatus(status); + Assertion assertion = (Assertion) XMLObjectSupport.buildXMLObject(Assertion.DEFAULT_ELEMENT_NAME); + assertion.setID("_opensaml_assertion_" + UUID.randomUUID()); assertion.setVersion(SAMLVersion.VERSION_20); assertion.setIssueInstant(at); + Issuer assertionIssuer = (Issuer) XMLObjectSupport.buildXMLObject(Issuer.DEFAULT_ELEMENT_NAME); + assertionIssuer.setValue(args[5]); assertion.setIssuer(assertionIssuer); + Subject subject = (Subject) XMLObjectSupport.buildXMLObject(Subject.DEFAULT_ELEMENT_NAME); + NameID name = (NameID) XMLObjectSupport.buildXMLObject(NameID.DEFAULT_ELEMENT_NAME); + name.setFormat(NameID.UNSPECIFIED); name.setValue("alice"); subject.setNameID(name); + SubjectConfirmation confirmation = (SubjectConfirmation) XMLObjectSupport.buildXMLObject(SubjectConfirmation.DEFAULT_ELEMENT_NAME); + confirmation.setMethod(SubjectConfirmation.METHOD_HOLDER_OF_KEY); + KeyInfoConfirmationDataType data = (KeyInfoConfirmationDataType) XMLObjectSupport + .getBuilder(KeyInfoConfirmationDataType.TYPE_NAME) + .buildObject(SubjectConfirmationData.DEFAULT_ELEMENT_NAME, KeyInfoConfirmationDataType.TYPE_NAME); + data.setRecipient(args[7]); data.setInResponseTo(args[8]); + if (args.length > 10) { + KeyInfo alternative = (KeyInfo) XMLObjectSupport.buildXMLObject(KeyInfo.DEFAULT_ELEMENT_NAME); + KeyInfoSupport.addCertificate(alternative, certificate(args[10])); data.getKeyInfos().add(alternative); + } + KeyInfo info = (KeyInfo) XMLObjectSupport.buildXMLObject(KeyInfo.DEFAULT_ELEMENT_NAME); + KeyInfoSupport.addCertificate(info, certificate(args[3])); data.getKeyInfos().add(info); + confirmation.setSubjectConfirmationData(data); subject.getSubjectConfirmations().add(confirmation); + assertion.setSubject(subject); + Conditions conditions = (Conditions) XMLObjectSupport.buildXMLObject(Conditions.DEFAULT_ELEMENT_NAME); + conditions.setNotBefore(at); conditions.setNotOnOrAfter(at.plusSeconds(300)); + AudienceRestriction restriction = (AudienceRestriction) XMLObjectSupport.buildXMLObject(AudienceRestriction.DEFAULT_ELEMENT_NAME); + Audience audience = (Audience) XMLObjectSupport.buildXMLObject(Audience.DEFAULT_ELEMENT_NAME); + audience.setURI(args[6]); restriction.getAudiences().add(audience); + conditions.getAudienceRestrictions().add(restriction); assertion.setConditions(conditions); + AuthnStatement statement = (AuthnStatement) XMLObjectSupport.buildXMLObject(AuthnStatement.DEFAULT_ELEMENT_NAME); + statement.setAuthnInstant(at); + AuthnContext authn = (AuthnContext) XMLObjectSupport.buildXMLObject(AuthnContext.DEFAULT_ELEMENT_NAME); + AuthnContextClassRef classRef = (AuthnContextClassRef) XMLObjectSupport.buildXMLObject(AuthnContextClassRef.DEFAULT_ELEMENT_NAME); + classRef.setURI("urn:oasis:names:tc:SAML:2.0:ac:classes:TLSClient"); authn.setAuthnContextClassRef(classRef); + statement.setAuthnContext(authn); assertion.getAuthnStatements().add(statement); + response.getAssertions().add(assertion); + Signature inner = signing(credential), outer = signing(credential); + assertion.setSignature(inner); response.setSignature(outer); + Document document = DetachedOracle.builder().newDocument(); + Element envelope = document.createElementNS(SOAP, "soap:Envelope"); + envelope.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:soap", SOAP); document.appendChild(envelope); + Element header = document.createElementNS(SOAP, "soap:Header"); envelope.appendChild(header); + var routing = (org.opensaml.saml.saml2.ecp.Response) XMLObjectSupport.buildXMLObject(org.opensaml.saml.saml2.ecp.Response.DEFAULT_ELEMENT_NAME); + routing.setAssertionConsumerServiceURL(args[7]); routing.setSOAP11Actor(NEXT); routing.setSOAP11MustUnderstand(true); + XMLObjectSupport.getMarshaller(routing).marshall(routing, header); + var hint = (org.opensaml.saml.saml2.ecp.SubjectConfirmation) XMLObjectSupport.buildXMLObject(org.opensaml.saml.saml2.ecp.SubjectConfirmation.DEFAULT_ELEMENT_NAME); + hint.setMethod(SubjectConfirmation.METHOD_HOLDER_OF_KEY); + hint.setSubjectConfirmationData(XMLObjectSupport.cloneXMLObject(data)); + hint.setSOAP11Actor(NEXT); hint.setSOAP11MustUnderstand(true); + XMLObjectSupport.getMarshaller(hint).marshall(hint, header); + Element body = document.createElementNS(SOAP, "soap:Body"); envelope.appendChild(body); + XMLObjectSupport.getMarshaller(response).marshall(response, body); + Signer.signObject(inner); Signer.signObject(outer); + TransformerFactory transformers = TransformerFactory.newInstance(); + transformers.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, ""); transformers.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, ""); + Transformer serializer = transformers.newTransformer(); serializer.setOutputProperty(OutputKeys.INDENT, "no"); + serializer.transform(new DOMSource(document), new StreamResult(Path.of(args[4]).toFile())); + System.out.println("OpenSAML 5.2.3 HoK response produced"); + } + + public static void main(String[] args) throws Exception { + if (!"5.2.3".equals(Version.getVersion())) throw new IllegalStateException("OpenSAML 5.2.3 required"); + InitializationService.initialize(); + if (args[0].equals("verify")) verify(args); + else if (args[0].equals("issue")) issue(args); + else if (args[0].equals("channels")) channels(args); + else throw new IllegalArgumentException("mode"); + } +} diff --git a/tests/interop/testdata/c14n-oracle.c b/tests/interop/testdata/c14n-oracle.c index 1dbac73..b3a100e 100644 --- a/tests/interop/testdata/c14n-oracle.c +++ b/tests/interop/testdata/c14n-oracle.c @@ -8,6 +8,8 @@ #include #include +#define MAX_INPUT_BYTES (8 * 1024 * 1024) + static xmlParserInputPtr deny_external(const char *url, const char *id, xmlParserCtxtPtr ctx) { (void)url; (void)id; (void)ctx; return NULL; } @@ -18,7 +20,7 @@ int main(int argc, char **argv) { if (!f) { perror("fopen"); return 2; } if (fseek(f, 0, SEEK_END) != 0) { fclose(f); return 2; } long size = ftell(f); - if (size < 0 || size > 8 * 1024 * 1024) { fclose(f); return 2; } + if (size < 0 || size > MAX_INPUT_BYTES) { fclose(f); return 2; } rewind(f); char *data = malloc((size_t)size + 1); if (!data || fread(data, 1, (size_t)size, f) != (size_t)size) { free(data); fclose(f); return 2; } diff --git a/tests/interop/testdata/openssl-gcm.c b/tests/interop/testdata/openssl-gcm.c index 89f62a2..3209abd 100644 --- a/tests/interop/testdata/openssl-gcm.c +++ b/tests/interop/testdata/openssl-gcm.c @@ -6,7 +6,10 @@ #include #include -#define LIMIT 16384 +#define MAX_PLAINTEXT_BYTES 16384 +#define GCM_NONCE_BYTES 12 +#define GCM_TAG_BYTES 16 +#define GCM_OVERHEAD_BYTES (GCM_NONCE_BYTES + GCM_TAG_BYTES) static int read_file(const char *path, unsigned char *data, size_t max) { FILE *file = fopen(path, "rb"); if (file == NULL) return -1; @@ -16,7 +19,7 @@ static int read_file(const char *path, unsigned char *data, size_t max) { return failed ? -1 : (int)n; } int main(int argc, char **argv) { - unsigned char key[32], input[LIMIT + 28], output[LIMIT + 28], nonce[12]; + unsigned char key[32], input[MAX_PLAINTEXT_BYTES + GCM_OVERHEAD_BYTES], output[MAX_PLAINTEXT_BYTES + GCM_OVERHEAD_BYTES], nonce[GCM_NONCE_BYTES]; EVP_CIPHER_CTX *ctx = NULL; int result = 1, n = 0, final = 0; if (argc != 5 || (strcmp(argv[1], "encrypt") && strcmp(argv[1], "decrypt"))) return 2; @@ -25,12 +28,12 @@ int main(int argc, char **argv) { int size = read_file(argv[3], input, sizeof(input)); if (cipher == NULL || size < 0 || (ctx = EVP_CIPHER_CTX_new()) == NULL) goto done; if (!strcmp(argv[1], "encrypt")) { - if (size > LIMIT || RAND_bytes(nonce, sizeof(nonce)) != 1) goto done; + if (size > MAX_PLAINTEXT_BYTES || RAND_bytes(nonce, sizeof(nonce)) != 1) goto done; memcpy(output, nonce, sizeof(nonce)); - if (EVP_EncryptInit_ex(ctx, cipher, NULL, key, nonce) != 1 || EVP_EncryptUpdate(ctx, output + 12, &n, input, size) != 1 || EVP_EncryptFinal_ex(ctx, output + 12 + n, &final) != 1 || EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, output + 12 + n + final) != 1) goto done; - n += final + 28; + if (EVP_EncryptInit_ex(ctx, cipher, NULL, key, nonce) != 1 || EVP_EncryptUpdate(ctx, output + GCM_NONCE_BYTES, &n, input, size) != 1 || EVP_EncryptFinal_ex(ctx, output + GCM_NONCE_BYTES + n, &final) != 1 || EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, GCM_TAG_BYTES, output + GCM_NONCE_BYTES + n + final) != 1) goto done; + n += final + GCM_OVERHEAD_BYTES; } else { - if (size < 28 || EVP_DecryptInit_ex(ctx, cipher, NULL, key, input) != 1 || EVP_DecryptUpdate(ctx, output, &n, input + 12, size - 28) != 1 || EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, input + size - 16) != 1 || EVP_DecryptFinal_ex(ctx, output + n, &final) != 1) goto done; + if (size < GCM_OVERHEAD_BYTES || EVP_DecryptInit_ex(ctx, cipher, NULL, key, input) != 1 || EVP_DecryptUpdate(ctx, output, &n, input + GCM_NONCE_BYTES, size - GCM_OVERHEAD_BYTES) != 1 || EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, GCM_TAG_BYTES, input + size - GCM_TAG_BYTES) != 1 || EVP_DecryptFinal_ex(ctx, output + n, &final) != 1) goto done; n += final; } FILE *out = fopen(argv[4], "wb"); diff --git a/tests/interop/xml_helpers_test.go b/tests/interop/xml_helpers_test.go index 94ef730..b27a417 100644 --- a/tests/interop/xml_helpers_test.go +++ b/tests/interop/xml_helpers_test.go @@ -237,8 +237,8 @@ func xmlElement(t *testing.T, d *document.Document, n *tree.Node) []byte { } func nativeKeyArgs(mode, keyName string) (string, string) { - if mode == nameRsa { - return flagPubkeyPem, "--privkey-pem" + if mode == nameRSA { + return flagPublicKeyPEM, "--privkey-pem" } arg := "--aeskey" diff --git a/tests/testdata/consumer/document_test.go b/tests/testdata/consumer/document_test.go index d9d9e6e..abcc8c2 100644 --- a/tests/testdata/consumer/document_test.go +++ b/tests/testdata/consumer/document_test.go @@ -22,7 +22,7 @@ import ( // leaking through the document.Document alias. New public methods must be // deliberately added here after reviewing their ownership contract. func TestDocumentPublicMethods(t *testing.T) { - want := []string{"AllNodes", "Node", "Root", "WriteXML", "XML"} + want := []string{"AllNodes", "DTD", "Node", "Root", "WriteXML", "XML"} typ := reflect.TypeFor[*document.Document]() got := make([]string, typ.NumMethod()) for i := range got { @@ -33,6 +33,26 @@ func TestDocumentPublicMethods(t *testing.T) { } } +func TestNonValidatingDocumentAPI(t *testing.T) { + options := document.ParseOptions{NonValidating: true} + d, err := options.ParseText(t.Context(), `]>&e;`) + if err != nil { + t.Fatal(err) + } + if words, ok := d.Root().Attribute(document.Name{Local: "words"}); !ok || words != "one two" || d.Root().Text() != "value" { + t.Fatal("public parser lost XML DTD semantics") + } + info, ok := d.DTD() + if !ok || info.Name != "r" || info.Notations["png"].SystemID != "image/png" { + t.Fatal("public notation information unavailable") + } + delete(info.Notations, "png") + info, _ = d.DTD() + if len(info.Notations) != 1 { + t.Fatal("public DTD metadata is mutable") + } +} + func TestRecordsEmitBeforeReadingRemainder(t *testing.T) { emitted := 0 reader := &gatedInput{first: strings.NewReader(`1`), rest: strings.NewReader(`2`), ready: func() bool { return emitted > 0 }} diff --git a/tests/testdata/consumer/go.mod b/tests/testdata/consumer/go.mod index 0a53285..58b392e 100644 --- a/tests/testdata/consumer/go.mod +++ b/tests/testdata/consumer/go.mod @@ -2,8 +2,9 @@ module example.com/xmlsec-consumer go 1.27.3 -require github.com/skarm/xmlsec v0.0.0 - -require golang.org/x/text v0.43.0 // indirect +require ( + github.com/skarm/xmlsec v0.0.0 + golang.org/x/text v0.43.0 +) replace github.com/skarm/xmlsec => ../../.. diff --git a/tests/testdata/consumer/helpers_test.go b/tests/testdata/consumer/helpers_test.go index cf473aa..a6f37bd 100644 --- a/tests/testdata/consumer/helpers_test.go +++ b/tests/testdata/consumer/helpers_test.go @@ -19,6 +19,11 @@ import ( "github.com/skarm/xmlsec/transform" ) +const ( + allocationMeasurementTrials = 3 + peakHeapSampleInterval = 100 * time.Microsecond +) + func newEngine(t *testing.T, config transform.Config) *transform.Engine { t.Helper() e, err := transform.NewEngine(config) @@ -111,7 +116,7 @@ func (r *streamingInput) Read(p []byte) (int, error) { func allocatedDuring(t *testing.T, run func() error) uint64 { t.Helper() allocated := ^uint64(0) - for range 3 { + for range allocationMeasurementTrials { var before, after runtime.MemStats runtime.ReadMemStats(&before) if err := run(); err != nil { @@ -157,7 +162,7 @@ func peakHeapDuring(t *testing.T, run func() error) uint64 { ready, stop := make(chan struct{}), make(chan struct{}) done := make(chan uint64, 1) go func() { - ticker := time.NewTicker(100 * time.Microsecond) + ticker := time.NewTicker(peakHeapSampleInterval) defer ticker.Stop() peak := before close(ready) diff --git a/tests/testdata/consumer/resource_test.go b/tests/testdata/consumer/resource_test.go index 0ec9b40..9bd01f6 100644 --- a/tests/testdata/consumer/resource_test.go +++ b/tests/testdata/consumer/resource_test.go @@ -66,11 +66,18 @@ func TestHTTPTextMetadataAndDecodedLimit(t *testing.T) { } value, err := q.Evaluate(t.Context(), nil, transform.XPathContext{}) if maximum == 1 { - if !errors.Is(err, transform.ErrLimit) || value.Len() != 0 { + if !errors.Is(err, transform.ErrLimit) || value != (transform.Sequence{}) { t.Fatal("decoded text escaped its byte budget", value, err) } - } else if text, textErr := value.Text(); err != nil || textErr != nil || text != "é" { - t.Fatal("HTTP metadata was lost", text, err, textErr) + } else { + if err != nil { + t.Fatal(err) + } + + text, textErr := value.Text() + if textErr != nil || text != "é" { + t.Fatal("HTTP metadata was lost", text, textErr) + } } if closed != 1 { t.Fatal("HTTP stream close count", closed) @@ -93,8 +100,8 @@ func TestTypedNilResourceAdapters(t *testing.T) { if err != nil { t.Fatal(err) } - if value, err := query.Evaluate(t.Context(), nil, transform.XPathContext{}); err == nil || value.Len() != 0 { - t.Fatalf("nil resolver: %d items, %v", value.Len(), err) + if value, err := query.Evaluate(t.Context(), nil, transform.XPathContext{}); err == nil || value != (transform.Sequence{}) { + t.Fatalf("nil resolver: result %+v, %v", value, err) } var absentReader *strings.Reader @@ -297,7 +304,9 @@ func TestTextResourceChunksAndCompletion(t *testing.T) { t.Fatal(err) } result, err := query.Evaluate(t.Context(), nil, transform.XPathContext{}) - if err != nil || result.Len() != 3 { + if err != nil { + t.Fatal(err) + } else if result.Len() != 3 { t.Fatal(result.Len(), err) } for i, want := range []string{strings.Repeat("x", (32<<10)-1) + "🙂", "я", "end"} { diff --git a/tests/testdata/consumer/result_test.go b/tests/testdata/consumer/result_test.go index 37c229c..7228427 100644 --- a/tests/testdata/consumer/result_test.go +++ b/tests/testdata/consumer/result_test.go @@ -33,7 +33,9 @@ func TestResultsRetainTypesOrderAndOutputDefinitions(t *testing.T) { } secondary := r.Secondary() value, err := secondary["https://result.test/secondary.json"].Single() - if err != nil || value.Kind() != transform.MapValue { + if err != nil { + t.Fatal(err) + } else if value.Kind() != transform.MapValue { t.Fatal(value.Kind(), err) } delete(secondary, "https://result.test/secondary.json") @@ -493,21 +495,6 @@ func TestWriterSerializationSupportsBufferedOptions(t *testing.T) { } } -func streamingResult(t *testing.T, input, output, body string) *transform.Result { - t.Helper() - e := newEngine(t, transform.Config{Limits: transform.Limits{MaxInputBytes: 16 << 20, MaxOutputBytes: 16 << 20, Timeout: time.Minute}}) - s := compileSheet(t, e, ``+body+``) - d, err := e.ParseReader(t.Context(), strings.NewReader(input)) - if err != nil { - t.Fatal(err) - } - r, err := s.Execute(t.Context(), d, transform.XSLTContext{}) - if err != nil { - t.Fatal(err) - } - return r -} - func TestSerializeBoundsCharacterMapExpansion(t *testing.T) { e := newEngine(t, transform.Config{Limits: transform.Limits{MaxOutputBytes: 64 << 10}}) variables := transform.Bindings{ @@ -523,7 +510,7 @@ func TestSerializeBoundsCharacterMapExpansion(t *testing.T) { // The requested expansion is 16 MiB; the invocation permits 64 KiB. allocated := allocatedDuring(t, func() error { got, err := q.Evaluate(t.Context(), nil, transform.XPathContext{Variables: variables}) - if !errors.Is(err, transform.ErrLimit) || got.Len() != 0 { + if !errors.Is(err, transform.ErrLimit) || got != (transform.Sequence{}) { return errors.New("serialization output budget was ignored") } return nil @@ -555,3 +542,18 @@ func TestXSLTBoundsCharacterMapExpansion(t *testing.T) { }) } } + +func streamingResult(t *testing.T, input, output, body string) *transform.Result { + t.Helper() + e := newEngine(t, transform.Config{Limits: transform.Limits{MaxInputBytes: 16 << 20, MaxOutputBytes: 16 << 20, Timeout: time.Minute}}) + s := compileSheet(t, e, ``+body+``) + d, err := e.ParseReader(t.Context(), strings.NewReader(input)) + if err != nil { + t.Fatal(err) + } + r, err := s.Execute(t.Context(), d, transform.XSLTContext{}) + if err != nil { + t.Fatal(err) + } + return r +} diff --git a/tests/testdata/consumer/transform_test.go b/tests/testdata/consumer/transform_test.go index 87799f4..9c55fa9 100644 --- a/tests/testdata/consumer/transform_test.go +++ b/tests/testdata/consumer/transform_test.go @@ -453,3 +453,43 @@ func (e readerReferenceExtension) WriteTransform(ctx context.Context, w io.Write _, err := io.Copy(w, r) return err } + +func TestPublicXMLScopeLimits(t *testing.T) { + for _, tc := range []struct { + name, xml string + limits transform.Limits + reject bool + }{ + {"attribute policy", ``, transform.Limits{MaxAttributesPerElement: 1}, true}, + {"attribute exact", ``, transform.Limits{MaxAttributesPerElement: 2}, false}, + {"namespace policy", ``, transform.Limits{MaxInScopeNamespaces: 1}, true}, + {"namespace exact", ``, transform.Limits{MaxInScopeNamespaces: 2}, false}, + {"old custom limits", ``, transform.Limits{MaxInputBytes: 1024}, false}, + } { + t.Run(tc.name, func(t *testing.T) { + engine := newEngine(t, transform.Config{Limits: tc.limits}) + result, err := engine.ParseReader(t.Context(), strings.NewReader(tc.xml)) + if tc.reject { + if result != nil || !errors.Is(err, transform.ErrLimit) || !errors.Is(err, document.ErrLimit) { + t.Fatal("public reader lost XML policy", result, err) + } + } else if err != nil || result == nil { + t.Fatal(result, err) + } + snapshot, err := document.Parse(t.Context(), []byte(tc.xml), document.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + imported, err := engine.Import(t.Context(), snapshot) + if tc.reject { + if imported != nil || !errors.Is(err, transform.ErrLimit) || !errors.Is(err, document.ErrLimit) { + t.Fatal("public import lost XML policy", imported, err) + } + } else if err != nil { + t.Fatal(err) + } else if exported, err := engine.Export(t.Context(), imported); err != nil || exported != snapshot { + t.Fatal("snapshot identity changed", err) + } + }) + } +} diff --git a/tests/testdata/consumer/xmldsig_test.go b/tests/testdata/consumer/xmldsig_test.go index 4209e48..6859bca 100644 --- a/tests/testdata/consumer/xmldsig_test.go +++ b/tests/testdata/consumer/xmldsig_test.go @@ -546,6 +546,8 @@ func TestReferenceNodeSetWorkingStorage(t *testing.T) { allocated := allocatedDuring(t, func() error { hash := sha256.New() report, err := reference.Write(t.Context(), hash, data, tc.transforms, tc.options) + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if err == nil && (report.Size() != len(tc.want) || !bytes.Equal(hash.Sum(nil), expected[:])) { return errors.New("reference output changed") } diff --git a/tests/testdata/consumer/xslt_test.go b/tests/testdata/consumer/xslt_test.go index 6450f3c..240809d 100644 --- a/tests/testdata/consumer/xslt_test.go +++ b/tests/testdata/consumer/xslt_test.go @@ -105,7 +105,7 @@ func TestReturnedXSLTFunctionIdentityAndBudget(t *testing.T) { for range 2 { result, err := query.Evaluate(t.Context(), nil, transform.XPathContext{Variables: transform.Bindings{{Local: "f"}: created.Primary()}}) if outputLimit == 128 { - if !errors.Is(err, transform.ErrLimit) || result.Len() != 0 { + if !errors.Is(err, transform.ErrLimit) || result != (transform.Sequence{}) { t.Fatal("function bypassed caller output budget", err) } diff --git a/timestamp/assertion.go b/timestamp/assertion.go index fcbfa2b..92e5703 100644 --- a/timestamp/assertion.go +++ b/timestamp/assertion.go @@ -13,6 +13,11 @@ import ( "github.com/skarm/xmlsec/pki" ) +const ( + // Caps raw provenance entries before timestamp observations are filtered out. + maximumSourceObservations = 1024 +) + // VerifiedAssertion is an immutable authenticated TSA assertion minted only by // successful token/response verification. A caller-created zero value is invalid. // It binds the exact token, imprint and asserted GenTime to the TSA and explicit @@ -20,18 +25,32 @@ import ( // TSA expiration/revocation or authenticate an arbitrary caller-supplied time. // Trust retains whether revocation was required or explicitly left unchecked. type VerifiedAssertion struct { - generation time.Time - validation time.Time - trust *pki.Result + // generation is the TSA's authenticated generation-time assertion. + generation time.Time + // validation is the explicit time at which TSA trust policy passed. + validation time.Time + // trust retains the successful TSA certificate and revocation result. + trust *pki.Result + // tokenFingerprint is the hexadecimal SHA-256 of the exact authenticated token. tokenFingerprint string - tsaFingerprint string - imprint Imprint - sources []*evidence.Observation - policy asn1.ObjectIdentifier - accuracy time.Duration - authenticated bool - hasAccuracy bool - ordering bool + // tsaFingerprint is the hexadecimal SHA-256 of the exact TSA certificate DER. + tsaFingerprint string + // imprint owns the authenticated message digest and its algorithm. + imprint Imprint + // sources retains acquisition observations without granting historical + // existence or trust. + sources []*evidence.Observation + // policy retains the accepted TSA policy OID. + policy asn1.ObjectIdentifier + // accuracy is the authenticated uncertainty around generation when hasAccuracy + // is true. + accuracy time.Duration + // authenticated marks completion of the configured verification policy. + authenticated bool + // hasAccuracy distinguishes absent accuracy from an explicit zero uncertainty. + hasAccuracy bool + // ordering retains the TSA's authenticated within-TSA ordering assertion. + ordering bool } // Valid reports whether this value came from a complete configured-policy check. @@ -163,7 +182,7 @@ func (v *Verifier) timestampSources(ctx context.Context, token *Token, response return input, nil, nil } - if len(input.Sources) > 1024 { + if len(input.Sources) > maximumSourceObservations { return pki.Evidence{}, nil, ErrPolicy } diff --git a/timestamp/authenticate.go b/timestamp/authenticate.go index 865c0f6..718cf50 100644 --- a/timestamp/authenticate.go +++ b/timestamp/authenticate.go @@ -5,6 +5,7 @@ import ( "context" "crypto" "crypto/sha256" + "fmt" "time" "github.com/skarm/xmlsec/cms" @@ -17,12 +18,19 @@ import ( // contains NO PKIX, revocation or historical trust result. A zero value is invalid. // Only a complete validation workflow may promote this assertion to trusted PoE. type AuthenticatedToken struct { - token *Token + // token is the cryptographically authenticated token, without a TSA trust + // decision. + token *Token + // certificate retains the exact certificate used for signature verification. certificate []byte - hash crypto.Hash - algorithm string - digest [32]byte - valid bool + // hash is the authenticated token signature's digest algorithm. + hash crypto.Hash + // algorithm is the authenticated signature algorithm OID. + algorithm string + // digest identifies the exact token encoding by SHA-256. + digest [sha256.Size]byte + // valid marks successful cryptographic authentication only. + valid bool } // Valid reports cryptographic authentication ONLY, never trust or existence. @@ -56,9 +64,9 @@ func (a *AuthenticatedToken) CertificateDER() []byte { } // TokenSHA256 identifies the exact token encoding, without changing its DER. -func (a *AuthenticatedToken) TokenSHA256() [32]byte { +func (a *AuthenticatedToken) TokenSHA256() [sha256.Size]byte { if !a.Valid() { - return [32]byte{} + return [sha256.Size]byte{} } return a.digest @@ -95,7 +103,7 @@ func (v *Verifier) Authenticate(ctx context.Context, token *Token, want Expectat return nil, err } - if want.Nonce != nil && (want.Nonce.Sign() <= 0 || want.Nonce.BitLen() > 256) { + if want.Nonce != nil && (want.Nonce.Sign() <= 0 || want.Nonce.BitLen() > maximumNonceBits) { return nil, ErrPolicy } @@ -128,7 +136,7 @@ func (v *Verifier) Authenticate(ctx context.Context, token *Token, want Expectat return nil, err } - var fingerprint [32]byte + var fingerprint [sha256.Size]byte copy(fingerprint[:], digest.Sum(nil)) return &AuthenticatedToken{ @@ -141,6 +149,23 @@ func (v *Verifier) Authenticate(ctx context.Context, token *Token, want Expectat }, nil } +// CheckEvidenceLimits checks configured DER collection counts and aggregate bytes, +// including configured lookup certificates after exact-byte deduplication. Raw +// inputs are checked before lookup merging. It does not parse or authenticate +// evidence, inspect Sources or validate historical options. PKI limit and context +// error causes are retained; an invalid timestamp verifier returns ErrPolicy. +func (v *Verifier) CheckEvidenceLimits(ctx context.Context, evidence pki.Evidence) error { + if v == nil || v.trust == nil { + return ErrPolicy + } + + if err := v.trust.CheckEvidenceLimits(ctx, evidence); err != nil { + return err + } + + return v.trust.CheckEvidenceLimits(ctx, v.historicalEvidence(evidence)) +} + // NewHistoricalSession creates a separate operation using this verifier's exact // immutable TSA roots, usages and responder policy. It does not validate a token // or grant PoE. Configured lookup certificates remain untrusted intermediates. @@ -149,6 +174,19 @@ func (v *Verifier) NewHistoricalSession(ctx context.Context, options pki.Histori return nil, ErrPolicy } + // Preserve the underlying PKI request error before checking evidence limits. + if ctx == nil || now.IsZero() { + return nil, fmt.Errorf("%w: verifier, context and explicit time required", pki.ErrPolicy) + } + + if err := v.trust.CheckEvidenceLimits(ctx, evidence); err != nil { + return nil, err + } + + return v.trust.NewHistoricalSession(ctx, options, now, v.historicalEvidence(evidence)) +} + +func (v *Verifier) historicalEvidence(evidence pki.Evidence) pki.Evidence { evidence.Intermediates = append([][]byte(nil), evidence.Intermediates...) for _, certificate := range v.certificates { @@ -166,5 +204,5 @@ func (v *Verifier) NewHistoricalSession(ctx context.Context, options pki.Histori } } - return v.trust.NewHistoricalSession(ctx, options, now, evidence) + return evidence } diff --git a/timestamp/client.go b/timestamp/client.go index 866d6f1..bec83c5 100644 --- a/timestamp/client.go +++ b/timestamp/client.go @@ -47,7 +47,7 @@ func (v *Verifier) Fetch(ctx context.Context, t Transport, q *Request, at time.T body, err := t.RoundTrip(ctx, q.DER()) if err != nil { - if !nilcheck.IsNil(body) { + if body != nil && !nilcheck.IsNil(body) { err = errors.Join(err, body.Close()) } diff --git a/timestamp/clone.go b/timestamp/clone.go index 91955b9..869f9bc 100644 --- a/timestamp/clone.go +++ b/timestamp/clone.go @@ -10,7 +10,7 @@ import ( // WithEvidenceCertificates replaces UNTRUSTED certificate lookup/path material, // keeping roots, algorithm policy, TSA policy identifiers and time constraints. func (v *Verifier) WithEvidenceCertificates(raw [][]byte) (*Verifier, error) { - if v == nil || len(raw) > 32 { + if v == nil || len(raw) > maximumLookupCertificates { return nil, ErrPolicy } @@ -20,7 +20,7 @@ func (v *Verifier) WithEvidenceCertificates(raw [][]byte) (*Verifier, error) { total := 0 for _, b := range raw { total += len(b) - if len(b) > 1<<20 || total > v.maxBytes { + if len(b) > maximumCertificateBytes || total > v.maxBytes { return nil, ErrPolicy } diff --git a/timestamp/format.go b/timestamp/format.go index 23d7dac..f322ac3 100644 --- a/timestamp/format.go +++ b/timestamp/format.go @@ -21,6 +21,75 @@ import ( "github.com/skarm/xmlsec/internal/streamio" ) +const ( + // Hard byte ceiling shared by timestamp response/token parsing and issuance. + maximumResponseBytes = 16 << 20 +) + +const ( + // GeneralizedTime uses whole UTC seconds plus an optional canonical fraction. + generalizedTimeSecondsLayout = "20060102150405" + // Locally generated nonces have a fixed byte width; parsing separately caps bit length. + generatedNonceBytes = 32 + // Accuracy components are bounded independently when parsing received TSTInfo. + maximumAccuracyFraction = 999 + maximumAccuracySeconds = 86400 + // Caps the TSTInfo DER payload separately from its enclosing CMS token, in bytes. + maximumInfoBytes = 1 << 20 + // Caps distinct optional extension records in received TSTInfo. + maximumInfoExtensions = 32 + // Upper bound on a positive RFC 3161 nonce, in bits. + maximumNonceBits = 256 + // Maximum width of locally supplied nonces; received nonces use the bit cap. + maximumNonceBytes = 32 + // Caps the complete encoded request, independently of response/token limits. + maximumRequestBytes = 1 << 16 + // Upper bound on the timestamp serial number, in bits. + maximumSerialBits = 160 + // Status text is untrusted: cap each UTF-8 value and the number of values. + maximumStatusTextBytes = 4096 + maximumStatusTexts = 16 + // Canonical GeneralizedTime represents years with exactly four digits. + maximumTimeYear = 9999 + // Minimum width of locally supplied nonces; generated nonces use their fixed width. + minimumNonceBytes = 16 + // RFC 3161 version value emitted by issuers and required by request/TSTInfo parsing. + timestampVersion = 1 + // PKIFailureInfo uses bit positions, independently of the PKIStatus enumeration. + failureAdditionalInfoNotAvailable = 17 + failureBadAlgorithm = 0 + failureBadDataFormat = 5 + failureBadRequest = 2 + failureSystem = 25 + failureTimeNotAvailable = 14 + failureUnacceptedExtension = 16 + failureUnacceptedPolicy = 15 + // Widths include the UTC designator; fractional forms also contain the decimal point. + generalizedTimeBaseBytes = 15 + generalizedTimeMaximumFractionBytes = generalizedTimeBaseBytes + 10 + generalizedTimeMinimumFractionBytes = generalizedTimeBaseBytes + 2 + // Bounds the encoded failure bitmap through its highest recognized bit. + maximumFailureInfoBits = failureSystem + 1 + // These two success statuses require an attached timestamp token. + statusGranted = 0 + statusGrantedWithMods = 1 + // Highest recognized PKIStatus; unsuccessful statuses return ResponseError. + statusRevocationNotification = 5 +) + +// TSTInfo context tags and Accuracy component tags belong to different ASN.1 +// structures. Their equal numeric values do not make them interchangeable. +const ( + infoTSANameTag = 0 + infoExtensionsTag = 1 + accuracyMillisTag = 0 + accuracyMicrosTag = 1 + infoRequiredFields = 5 + infoMaximumFields = 10 + responseStatusFields = 1 + responseWithTokenFields = 2 +) + var ( // ErrFormat reports invalid or unsupported timestamp format. ErrFormat = errors.New("invalid or unsupported timestamp format") @@ -96,10 +165,15 @@ type RequestOptions struct { // Request retains an immutable parsed RFC 3161 request. Parsing establishes no sender // authenticity. type Request struct { - raw []byte + // raw owns the complete encoded timestamp request. + raw []byte + // imprint retains the requested message digest and algorithm. imprint Imprint - policy asn1.ObjectIdentifier - nonce *big.Int + // policy is the requested TSA policy OID, if supplied. + policy asn1.ObjectIdentifier + // nonce binds a response to this request; nil records an omitted nonce. + nonce *big.Int + // certReq requests inclusion of the TSA certificate in the response token. certReq bool } @@ -123,11 +197,13 @@ func CreateRequestReader(ctx context.Context, src io.Reader, o RequestOptions) ( // CreateImprintRequest builds an RFC 3161 request from an explicit message imprint and // request options. func CreateImprintRequest(m Imprint, o RequestOptions) (*Request, error) { + const nonceTopBit = 0x80 // Generated positive integers retain the full nonce bit width. + if !validImprint(m) || o.OmitNonce && len(o.Nonce) > 0 { return nil, ErrPolicy } - f := [][]byte{der.Int(1), encodeImprint(m)} + f := [][]byte{der.Int(timestampVersion), encodeImprint(m)} if len(o.Policy) > 0 { b := der.OID(o.Policy) @@ -141,15 +217,15 @@ func CreateImprintRequest(m Imprint, o RequestOptions) (*Request, error) { if !o.OmitNonce { n := bytes.Clone(o.Nonce) if n == nil { - n = make([]byte, 32) + n = make([]byte, generatedNonceBytes) if _, e := rand.Read(n); e != nil { return nil, e } - n[0] |= 128 + n[0] |= nonceTopBit } - if len(n) < 16 || len(n) > 32 { + if len(n) < minimumNonceBytes || len(n) > maximumNonceBytes { return nil, ErrPolicy } @@ -173,7 +249,7 @@ func CreateImprintRequest(m Imprint, o RequestOptions) (*Request, error) { // ParseRequest parses one complete bounded RFC 3161 request without authenticating its // origin. func ParseRequest(b []byte) (*Request, error) { - if len(b) > 1<<16 { + if len(b) > maximumRequestBytes { return nil, ErrPolicy } @@ -220,86 +296,27 @@ func (r *Request) Policy() asn1.ObjectIdentifier { // certificates. func (r *Request) CertificatesRequested() bool { return r != nil && r.certReq } -func validImprint(m Imprint) bool { - return (m.Hash == crypto.SHA256 || m.Hash == crypto.SHA384 || m.Hash == crypto.SHA512) && len(m.Digest) == m.Hash.Size() -} - -func encodeImprint(m Imprint) []byte { - a, _ := signature.DigestAlgorithm(m.Hash) - return der.Seq(signature.EncAlgorithm(a), der.Octets(m.Digest)) -} - -func parseOwnedRequest(b []byte) (*Request, error) { - if len(b) > 1<<16 { - return nil, ErrPolicy - } - - v, e := der.Read(b, 1<<16) - if e != nil { - return nil, ErrFormat - } - - f, e := der.Sequence(v, 2, 5) - if e != nil { - return nil, ErrFormat - } - - var ver int - if der.Decode(f[0], &ver) != nil || ver != 1 { - return nil, ErrFormat - } - - m, e := parseImprint(f[1]) - if e != nil { - return nil, e - } - - r := &Request{raw: b, imprint: m} - - i := 2 - if i < len(f) && der.Is(f[i], 0, asn1.TagOID, false) { - if der.Decode(f[i], &r.policy) != nil { - return nil, ErrFormat - } - - i++ - } - - if i < len(f) && der.Is(f[i], 0, asn1.TagInteger, false) { - if der.Decode(f[i], &r.nonce) != nil || r.nonce == nil || r.nonce.Sign() <= 0 || r.nonce.BitLen() > 256 { - return nil, ErrFormat - } - - i++ - } - - if i < len(f) && der.Is(f[i], 0, asn1.TagBoolean, false) { - if der.Decode(f[i], &r.certReq) != nil || !r.certReq { - return nil, ErrFormat - } - - i++ - } - - if i != len(f) { - return nil, ErrFormat - } - - return r, nil -} - // Info retains parsed TSTInfo. Accept its time claims only after successful token // verification. type Info struct { - policy asn1.ObjectIdentifier - imprint Imprint - serial *big.Int - nonce *big.Int - at time.Time - accuracy time.Duration + // policy is the claimed TSA policy OID. + policy asn1.ObjectIdentifier + // imprint is the claimed message digest and algorithm. + imprint Imprint + // serial is the TSA-assigned token serial number. + serial *big.Int + // nonce is the echoed request nonce, if present. + nonce *big.Int + // at is the claimed timestamp generation time. + at time.Time + // accuracy is the claimed uncertainty around at when hasAccuracy is true. + accuracy time.Duration + // hasAccuracy distinguishes absent accuracy from an explicit zero value. hasAccuracy bool - ordering bool - tsa asn1.RawValue + // ordering is the claimed within-TSA ordering guarantee. + ordering bool + // tsa retains the optional encoded TSA name. + tsa asn1.RawValue } // Policy returns an owned copy of the timestamp policy OID. @@ -347,7 +364,7 @@ func (i *Info) parseRequiredFields(fields []asn1.RawValue) error { return e } - if der.Decode(fields[3], &i.serial) != nil || i.serial == nil || i.serial.Sign() <= 0 || i.serial.BitLen() > 160 { + if der.Decode(fields[3], &i.serial) != nil || i.serial == nil || i.serial.Sign() <= 0 || i.serial.BitLen() > maximumSerialBits { return ErrFormat } @@ -361,7 +378,7 @@ func (i *Info) parseRequiredFields(fields []asn1.RawValue) error { func (i *Info) parseOptionalFields(fields []asn1.RawValue) error { index := 0 - if index < len(fields) && der.Is(fields[index], 0, asn1.TagSequence, true) { + if index < len(fields) && der.Is(fields[index], asn1.ClassUniversal, asn1.TagSequence, true) { var e error i.accuracy, e = parseAccuracy(fields[index]) @@ -373,7 +390,7 @@ func (i *Info) parseOptionalFields(fields []asn1.RawValue) error { index++ } - if index < len(fields) && der.Is(fields[index], 0, asn1.TagBoolean, false) { + if index < len(fields) && der.Is(fields[index], asn1.ClassUniversal, asn1.TagBoolean, false) { if der.Decode(fields[index], &i.ordering) != nil || !i.ordering { return ErrFormat } @@ -381,26 +398,26 @@ func (i *Info) parseOptionalFields(fields []asn1.RawValue) error { index++ } - if index < len(fields) && der.Is(fields[index], 0, asn1.TagInteger, false) { - if der.Decode(fields[index], &i.nonce) != nil || i.nonce == nil || i.nonce.Sign() <= 0 || i.nonce.BitLen() > 256 { + if index < len(fields) && der.Is(fields[index], asn1.ClassUniversal, asn1.TagInteger, false) { + if der.Decode(fields[index], &i.nonce) != nil || i.nonce == nil || i.nonce.Sign() <= 0 || i.nonce.BitLen() > maximumNonceBits { return ErrFormat } index++ } - if index < len(fields) && der.Is(fields[index], 2, 0, true) { + if index < len(fields) && der.Is(fields[index], asn1.ClassContextSpecific, infoTSANameTag, true) { var e error - i.tsa, e = der.Explicit(fields[index], 0) - if e != nil || i.tsa.Class != 2 { + i.tsa, e = der.Explicit(fields[index], infoTSANameTag) + if e != nil || i.tsa.Class != asn1.ClassContextSpecific { return ErrFormat } index++ } - if index < len(fields) && der.Is(fields[index], 2, 1, true) { + if index < len(fields) && der.Is(fields[index], asn1.ClassContextSpecific, infoExtensionsTag, true) { if err := validateInfoExtensions(fields[index]); err != nil { return err } @@ -415,30 +432,222 @@ func (i *Info) parseOptionalFields(fields []asn1.RawValue) error { return nil } +// Response retains an immutable parsed RFC 3161 response. Parsing does not +// authenticate its token. +type Response struct { + // raw owns the complete encoded timestamp response. + raw []byte + // token retains the attached CMS token only for a successful status. + token []byte + // status is the parsed RFC 3161 PKIStatus value. + status int + // text contains untrusted TSA diagnostic strings. + text []string + // fail retains the PKIFailureInfo bitmap without interpreting it as trust. + fail asn1.BitString +} + +// ParseResponse parses one complete bounded RFC 3161 response and rejects unsuccessful +// status values. +func ParseResponse(ctx context.Context, b []byte) (*Response, error) { + return ParseResponseReader(ctx, bytes.NewReader(b)) +} + +// ParseResponseReader reads one bounded DER response into owned storage. The +// full response and token remain materialized; src remains caller-owned. +func ParseResponseReader(ctx context.Context, src io.Reader) (*Response, error) { + if ctx == nil || nilcheck.IsNil(src) { + return nil, ErrPolicy + } + + value, err := der.ReadSequence(ctx, src, maximumResponseBytes) + if err != nil { + if errors.Is(err, der.ErrLimit) { + return nil, fmt.Errorf("%w: %w", ErrPolicy, err) + } + + return nil, fmt.Errorf("%w: %w", ErrFormat, err) + } + + return parseResponseValue(ctx, value, true) +} + +// DER returns an owned copy of the original DER encoding. +func (r *Response) DER() []byte { + if r == nil { + return nil + } + + return bytes.Clone(r.raw) +} + +// TokenDER returns an owned copy of the timestamp token DER. +func (r *Response) TokenDER() []byte { + if r == nil { + return nil + } + + return bytes.Clone(r.token) +} + +// Status returns the RFC 3161 response status code. +func (r *Response) Status() int { + if r == nil { + return -1 + } + + return r.status +} + +// bindToken checks whether the response status permits an attached CMS token. +func (r *Response) bindToken(ctx context.Context, f []asn1.RawValue, validateToken bool) (*Response, error) { + if r.status == statusGranted || r.status == statusGrantedWithMods { + if len(f) != responseWithTokenFields || r.fail.BitLength != 0 { + return nil, ErrFormat + } + + r.token = f[1].FullBytes + if validateToken { + if _, e := cms.Parse(ctx, r.token, cms.DefaultLimits()); e != nil { + return nil, fmt.Errorf("%w: %w", ErrFormat, e) + } + } + } else { + if len(f) != responseStatusFields { + return nil, ErrFormat + } + + err := &ResponseError{Status: r.status, Text: append([]string(nil), r.text...)} + for n := 0; n < r.fail.BitLength; n++ { + if r.fail.At(n) != 0 { + err.FailureBits = append(err.FailureBits, n) + } + } + + return r, err + } + + return r, nil +} + +// ResponseError reports a TSA failure status, untrusted status text and failure bit +// positions. +type ResponseError struct { + // Status is the RFC 3161 PKIStatus value returned by the TSA. + Status int + // Text contains the TSA's untrusted status strings. + Text []string + // FailureBits lists the set bit positions in the response's PKIFailureInfo. + FailureBits []int +} + +// Error returns the diagnostic error message. +func (e *ResponseError) Error() string { + return fmt.Sprintf("TSA response status %d (failure bits %v)", e.Status, e.FailureBits) +} + +// TSTInfoOID returns an owned copy of the RFC 3161 id-ct-TSTInfo object identifier. +func TSTInfoOID() asn1.ObjectIdentifier { return append(asn1.ObjectIdentifier(nil), tstOID()...) } + +func validImprint(m Imprint) bool { + return (m.Hash == crypto.SHA256 || m.Hash == crypto.SHA384 || m.Hash == crypto.SHA512) && len(m.Digest) == m.Hash.Size() +} + +func encodeImprint(m Imprint) []byte { + a, _ := signature.DigestAlgorithm(m.Hash) + return der.Seq(signature.EncAlgorithm(a), der.Octets(m.Digest)) +} + +func parseOwnedRequest(b []byte) (*Request, error) { + const ( + requestRequiredFields = 2 + requestMaximumFields = 5 + ) + + if len(b) > maximumRequestBytes { + return nil, ErrPolicy + } + + v, e := der.Read(b, maximumRequestBytes) + if e != nil { + return nil, ErrFormat + } + + f, e := der.Sequence(v, requestRequiredFields, requestMaximumFields) + if e != nil { + return nil, ErrFormat + } + + var ver int + if der.Decode(f[0], &ver) != nil || ver != timestampVersion { + return nil, ErrFormat + } + + m, e := parseImprint(f[1]) + if e != nil { + return nil, e + } + + r := &Request{raw: b, imprint: m} + + i := requestRequiredFields + if i < len(f) && der.Is(f[i], asn1.ClassUniversal, asn1.TagOID, false) { + if der.Decode(f[i], &r.policy) != nil { + return nil, ErrFormat + } + + i++ + } + + if i < len(f) && der.Is(f[i], asn1.ClassUniversal, asn1.TagInteger, false) { + if der.Decode(f[i], &r.nonce) != nil || r.nonce == nil || r.nonce.Sign() <= 0 || r.nonce.BitLen() > maximumNonceBits { + return nil, ErrFormat + } + + i++ + } + + if i < len(f) && der.Is(f[i], asn1.ClassUniversal, asn1.TagBoolean, false) { + if der.Decode(f[i], &r.certReq) != nil || !r.certReq { + return nil, ErrFormat + } + + i++ + } + + if i != len(f) { + return nil, ErrFormat + } + + return r, nil +} + func encodeTime(t time.Time) ([]byte, error) { - if t.IsZero() || t.Year() < 1 || t.Year() > 9999 { + const nanosecondDigitsFormat = "%09d" + + if t.IsZero() || t.Year() < 1 || t.Year() > maximumTimeYear { return nil, ErrPolicy } - s := t.UTC().Format("20060102150405") + s := t.UTC().Format(generalizedTimeSecondsLayout) if t.Nanosecond() != 0 { - s += "." + strings.TrimRight(fmt.Sprintf("%09d", t.Nanosecond()), "0") + s += "." + strings.TrimRight(fmt.Sprintf(nanosecondDigitsFormat, t.Nanosecond()), "0") } - return der.Wrap(0, asn1.TagGeneralizedTime, false, []byte(s+"Z")), nil + return der.Wrap(asn1.ClassUniversal, asn1.TagGeneralizedTime, false, []byte(s+"Z")), nil } func parseTime(v asn1.RawValue) (time.Time, error) { - if !der.Is(v, 0, asn1.TagGeneralizedTime, false) { + if !der.Is(v, asn1.ClassUniversal, asn1.TagGeneralizedTime, false) { return time.Time{}, ErrFormat } s := string(v.Bytes) - if len(s) < 15 || !strings.HasSuffix(s, "Z") { + if len(s) < generalizedTimeBaseBytes || !strings.HasSuffix(s, "Z") { return time.Time{}, ErrFormat } - if len(s) > 15 && (s[14] != '.' || len(s) < 17 || len(s) > 25 || s[len(s)-2] == '0') { + if len(s) > generalizedTimeBaseBytes && (s[len(generalizedTimeSecondsLayout)] != '.' || len(s) < generalizedTimeMinimumFractionBytes || len(s) > generalizedTimeMaximumFractionBytes || s[len(s)-2] == '0') { return time.Time{}, ErrFormat } @@ -456,7 +665,14 @@ func parseTime(v asn1.RawValue) (time.Time, error) { } func parseAccuracy(v asn1.RawValue) (time.Duration, error) { - f, e := der.Sequence(v, 0, 3) + const ( + accuracySeconds = iota + accuracyMillis + accuracyMicros + accuracyComponentCount + ) + + f, e := der.Sequence(v, 0, accuracyComponentCount) if e != nil { return 0, ErrFormat } @@ -472,16 +688,16 @@ func parseAccuracy(v asn1.RawValue) (time.Duration, error) { b := x.FullBytes switch { - case der.Is(x, 0, asn1.TagInteger, false): - kind = 0 - case der.Is(x, 2, 0, false): - kind = 1 + case der.Is(x, asn1.ClassUniversal, asn1.TagInteger, false): + kind = accuracySeconds + case der.Is(x, asn1.ClassContextSpecific, accuracyMillisTag, false): + kind = accuracyMillis unit = time.Millisecond - b = der.Wrap(0, asn1.TagInteger, false, x.Bytes) - case der.Is(x, 2, 1, false): - kind = 2 + b = der.Wrap(asn1.ClassUniversal, asn1.TagInteger, false, x.Bytes) + case der.Is(x, asn1.ClassContextSpecific, accuracyMicrosTag, false): + kind = accuracyMicros unit = time.Microsecond - b = der.Wrap(0, asn1.TagInteger, false, x.Bytes) + b = der.Wrap(asn1.ClassUniversal, asn1.TagInteger, false, x.Bytes) default: return 0, ErrFormat } @@ -493,7 +709,7 @@ func parseAccuracy(v asn1.RawValue) (time.Duration, error) { last = kind var n int - if der.Unmarshal(b, &n) != nil || n < 0 || kind > 0 && (n < 1 || n > 999) || kind == 0 && n > 86400 { + if der.Unmarshal(b, &n) != nil || n < 0 || kind > accuracySeconds && (n < 1 || n > maximumAccuracyFraction) || kind == accuracySeconds && n > maximumAccuracySeconds { return 0, ErrFormat } @@ -504,15 +720,20 @@ func parseAccuracy(v asn1.RawValue) (time.Duration, error) { } func validateInfoExtensions(v asn1.RawValue) error { + const ( + extensionRequiredFields = 2 + extensionWithCritical = 3 + ) + xs, err := der.Children(v) - if err != nil || len(xs) < 1 || len(xs) > 32 { + if err != nil || len(xs) < 1 || len(xs) > maximumInfoExtensions { return ErrFormat } seen := make(map[string]struct{}, len(xs)) for _, x := range xs { - fields, e := der.Sequence(x, 2, 3) + fields, e := der.Sequence(x, extensionRequiredFields, extensionWithCritical) if e != nil { return ErrFormat } @@ -528,7 +749,7 @@ func validateInfoExtensions(v asn1.RawValue) error { seen[oid.String()] = struct{}{} - if len(fields) == 3 { + if len(fields) == extensionWithCritical { var critical bool if der.Decode(fields[1], &critical) != nil || !critical { return ErrFormat @@ -537,7 +758,7 @@ func validateInfoExtensions(v asn1.RawValue) error { return ErrPolicy } - if !der.Is(fields[1], 0, asn1.TagOctetString, false) { + if !der.Is(fields[1], asn1.ClassUniversal, asn1.TagOctetString, false) { return ErrFormat } } @@ -545,127 +766,39 @@ func validateInfoExtensions(v asn1.RawValue) error { return nil } -// Response retains an immutable parsed RFC 3161 response. Parsing does not -// authenticate its token. -type Response struct { - raw []byte - token []byte - status int - text []string - fail asn1.BitString -} - -// ParseResponse parses one complete bounded RFC 3161 response and rejects unsuccessful -// status values. -func ParseResponse(ctx context.Context, b []byte) (*Response, error) { - return ParseResponseReader(ctx, bytes.NewReader(b)) -} - -// ParseResponseReader reads one bounded DER response into owned storage. The -// full response and token remain materialized; src remains caller-owned. -func ParseResponseReader(ctx context.Context, src io.Reader) (*Response, error) { - if ctx == nil || nilcheck.IsNil(src) { - return nil, ErrPolicy - } - - value, err := der.ReadSequence(ctx, src, 16<<20) - if err != nil { - if errors.Is(err, der.ErrLimit) { - return nil, fmt.Errorf("%w: %w", ErrPolicy, err) - } - - return nil, fmt.Errorf("%w: %w", ErrFormat, err) - } - - return parseResponseValue(ctx, value, true) -} - -// DER returns an owned copy of the original DER encoding. -func (r *Response) DER() []byte { - if r == nil { - return nil - } - - return bytes.Clone(r.raw) -} - -// TokenDER returns an owned copy of the timestamp token DER. -func (r *Response) TokenDER() []byte { - if r == nil { - return nil - } - - return bytes.Clone(r.token) -} - -// Status returns the RFC 3161 response status code. -func (r *Response) Status() int { - if r == nil { - return -1 - } - - return r.status -} - -// bindToken checks whether the response status permits an attached CMS token. -func (r *Response) bindToken(ctx context.Context, f []asn1.RawValue, validateToken bool) (*Response, error) { - if r.status == 0 || r.status == 1 { - if len(f) != 2 || r.fail.BitLength != 0 { - return nil, ErrFormat - } - - r.token = f[1].FullBytes - if validateToken { - if _, e := cms.Parse(ctx, r.token, cms.DefaultLimits()); e != nil { - return nil, fmt.Errorf("%w: %w", ErrFormat, e) - } - } - } else { - if len(f) != 1 { - return nil, ErrFormat - } - - err := &ResponseError{Status: r.status, Text: append([]string(nil), r.text...)} - for n := 0; n < r.fail.BitLength; n++ { - if r.fail.At(n) != 0 { - err.FailureBits = append(err.FailureBits, n) - } - } - - return r, err - } - - return r, nil -} - func parseResponseValue(ctx context.Context, v asn1.RawValue, validateToken bool) (*Response, error) { + const ( + statusRequiredFields = 1 + statusMaximumFields = 3 + ) + b := v.FullBytes - f, e := der.Sequence(v, 1, 2) + f, e := der.Sequence(v, responseStatusFields, responseWithTokenFields) if e != nil { return nil, ErrFormat } - sf, e := der.Sequence(f[0], 1, 3) + sf, e := der.Sequence(f[0], statusRequiredFields, statusMaximumFields) if e != nil { return nil, ErrFormat } r := &Response{raw: b} - if der.Decode(sf[0], &r.status) != nil || r.status < 0 || r.status > 5 { + if der.Decode(sf[0], &r.status) != nil || r.status < statusGranted || r.status > statusRevocationNotification { return nil, ErrFormat } i := 1 - if i < len(sf) && der.Is(sf[i], 0, asn1.TagSequence, true) { - texts, e := der.Sequence(sf[i], 1, 16) + if i < len(sf) && der.Is(sf[i], asn1.ClassUniversal, asn1.TagSequence, true) { + texts, e := der.Sequence(sf[i], 1, maximumStatusTexts) if e != nil { return nil, ErrFormat } for _, s := range texts { var msg string - if !der.Is(s, 0, asn1.TagUTF8String, false) || der.Decode(s, &msg) != nil || len(msg) > 4096 { + if !der.Is(s, asn1.ClassUniversal, asn1.TagUTF8String, false) || der.Decode(s, &msg) != nil || len(msg) > maximumStatusTextBytes { return nil, ErrFormat } @@ -676,14 +809,14 @@ func parseResponseValue(ctx context.Context, v asn1.RawValue, validateToken bool } if i < len(sf) { - if der.Decode(sf[i], &r.fail) != nil || r.fail.BitLength > 26 { + if der.Decode(sf[i], &r.fail) != nil || r.fail.BitLength > maximumFailureInfoBits { return nil, ErrFormat } i++ for n := 0; n < r.fail.BitLength; n++ { - if r.fail.At(n) != 0 && n != 0 && n != 2 && n != 5 && n != 14 && n != 15 && n != 16 && n != 17 && n != 25 { + if r.fail.At(n) != 0 && n != failureBadAlgorithm && n != failureBadRequest && n != failureBadDataFormat && n != failureTimeNotAvailable && n != failureUnacceptedPolicy && n != failureUnacceptedExtension && n != failureAdditionalInfoNotAvailable && n != failureSystem { return nil, ErrFormat } } @@ -696,29 +829,12 @@ func parseResponseValue(ctx context.Context, v asn1.RawValue, validateToken bool return r.bindToken(ctx, f, validateToken) } -// ResponseError reports a TSA failure status, untrusted status text and failure bit -// positions. -type ResponseError struct { - // Status is the RFC 3161 PKIStatus value returned by the TSA. - Status int - // Text contains the TSA's untrusted status strings. - Text []string - // FailureBits lists the set bit positions in the response's PKIFailureInfo. - FailureBits []int -} - -// Error returns the diagnostic error message. -func (e *ResponseError) Error() string { - return fmt.Sprintf("TSA response status %d (failure bits %v)", e.Status, e.FailureBits) -} - -// TSTInfoOID returns an owned copy of the RFC 3161 id-ct-TSTInfo object identifier. -func TSTInfoOID() asn1.ObjectIdentifier { return append(asn1.ObjectIdentifier(nil), tstOID()...) } - func tstOID() asn1.ObjectIdentifier { return asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 16, 1, 4} } func parseImprint(v asn1.RawValue) (Imprint, error) { - f, e := der.Sequence(v, 2, 2) + const imprintFields = 2 + + f, e := der.Sequence(v, imprintFields, imprintFields) if e != nil { return Imprint{}, ErrFormat } @@ -742,18 +858,18 @@ func parseImprint(v asn1.RawValue) (Imprint, error) { } func parseInfo(b []byte) (Info, error) { - v, e := der.Read(b, 1<<20) + v, e := der.Read(b, maximumInfoBytes) if e != nil { return Info{}, ErrFormat } - f, e := der.Sequence(v, 5, 10) + f, e := der.Sequence(v, infoRequiredFields, infoMaximumFields) if e != nil { return Info{}, ErrFormat } var version int - if der.Decode(f[0], &version) != nil || version != 1 { + if der.Decode(f[0], &version) != nil || version != timestampVersion { return Info{}, ErrFormat } @@ -762,7 +878,7 @@ func parseInfo(b []byte) (Info, error) { return info, err } - if err := info.parseOptionalFields(f[5:]); err != nil { + if err := info.parseOptionalFields(f[infoRequiredFields:]); err != nil { return info, err } // Only the GeneralName borrows TSTInfo bytes. Detach that small value so diff --git a/timestamp/issue.go b/timestamp/issue.go index e43a0e1..6d49bbf 100644 --- a/timestamp/issue.go +++ b/timestamp/issue.go @@ -12,11 +12,29 @@ import ( "github.com/skarm/xmlsec/cms" "github.com/skarm/xmlsec/internal/der" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/nilcheck" "github.com/skarm/xmlsec/internal/streamio" "github.com/skarm/xmlsec/internal/x509cert" ) +const ( + // Caps issued accuracy and the configurable verifier accuracy bound. Received + // TSTInfo components have separate parsing bounds; MaxAccuracy zero is unlimited. + maximumAccuracy = 24 * time.Hour +) + +// DER framing of the streamed CMS token and its response SEQUENCE. These wire +// widths do not change the separate maximumResponseBytes policy. +const ( + responseSequenceTag = 0x30 + responseLongLengthFlag = 0x80 + responseLengthCountMask = 0x7f + responseMinimumHeaderBytes = 2 + responseMaximumLengthOctets = 8 + responseLengthRadix = 256 +) + // IssueOptions uses a caller-supplied clock/serial. A real TSA must independently // guarantee trusted time, serial uniqueness and any Ordering claim. type IssueOptions struct { @@ -61,7 +79,7 @@ func WriteResponse(ctx context.Context, dst io.Writer, requestDER []byte, signer return 0, ErrPolicy } - if ctx == nil || o.SerialNumber == nil || o.SerialNumber.Sign() <= 0 || o.SerialNumber.BitLen() > 160 || len(der.OID(o.Policy)) == 0 || len(tsaCertificateDER) > 1<<20 { + if ctx == nil || o.SerialNumber == nil || o.SerialNumber.Sign() <= 0 || o.SerialNumber.BitLen() > maximumSerialBits || len(der.OID(o.Policy)) == 0 || len(tsaCertificateDER) > maximumCertificateBytes { return 0, ErrPolicy } @@ -98,7 +116,7 @@ func WriteResponse(ctx context.Context, dst io.Writer, requestDER []byte, signer return 0, e } - f := [][]byte{der.Int(1), der.OID(o.Policy), encodeImprint(q.imprint), serial, gen} + f := [][]byte{der.Int(timestampVersion), der.OID(o.Policy), encodeImprint(q.imprint), serial, gen} if o.Accuracy != nil { a, e := encodeAccuracy(*o.Accuracy) if e != nil { @@ -119,7 +137,7 @@ func WriteResponse(ctx context.Context, dst io.Writer, requestDER []byte, signer } if !o.OmitTSAName { - f = append(f, der.Ctx(0, der.Ctx(4, c.RawSubject))) + f = append(f, der.Ctx(infoTSANameTag, der.Ctx(lexicon.GeneralNameDirectory, c.RawSubject))) } opt := cms.SignOptions{ContentType: TSTInfoOID(), OmitCertificates: !q.certReq} @@ -127,7 +145,7 @@ func WriteResponse(ctx context.Context, dst io.Writer, requestDER []byte, signer opt.ExtraCertificates = o.ExtraCertificates } - writer := &streamio.Writer{Context: ctx, Target: dst, Limit: 16 << 20, LimitError: ErrPolicy} + writer := &streamio.Writer{Context: ctx, Target: dst, Limit: maximumResponseBytes, LimitError: ErrPolicy} envelope := responseWriter{target: writer} _, e = cms.SignReader(ctx, &envelope, bytes.NewBuffer(der.Seq(f...)), []cms.Signer{{Key: signer, CertificateDER: c.Raw, Hash: o.Hash, RSAPSS: o.RSAPSS}}, opt) @@ -139,7 +157,9 @@ func WriteResponse(ctx context.Context, dst io.Writer, requestDER []byte, signer } func encodeAccuracy(d time.Duration) ([]byte, error) { - if d < 0 || d > 24*time.Hour || d%time.Microsecond != 0 { + const maximumEncodedAccuracyIntegerBytes = 16 + + if d < 0 || d > maximumAccuracy || d%time.Microsecond != 0 { return nil, ErrPolicy } @@ -149,13 +169,13 @@ func encodeAccuracy(d time.Duration) ([]byte, error) { } if ms := d % time.Second / time.Millisecond; ms > 0 { - x, _ := der.Read(der.Int(int(ms)), 16) - f = append(f, der.Wrap(2, 0, false, x.Bytes)) + x, _ := der.Read(der.Int(int(ms)), maximumEncodedAccuracyIntegerBytes) + f = append(f, der.Wrap(asn1.ClassContextSpecific, accuracyMillisTag, false, x.Bytes)) } if us := d % time.Millisecond / time.Microsecond; us > 0 { - x, _ := der.Read(der.Int(int(us)), 16) - f = append(f, der.Wrap(2, 1, false, x.Bytes)) + x, _ := der.Read(der.Int(int(us)), maximumEncodedAccuracyIntegerBytes) + f = append(f, der.Wrap(asn1.ClassContextSpecific, accuracyMicrosTag, false, x.Bytes)) } return der.Seq(f...), nil @@ -167,7 +187,7 @@ type responseWriter struct { target *streamio.Writer used int expected int64 - header [10]byte + header [responseMinimumHeaderBytes + responseMaximumLengthOctets]byte started bool } @@ -186,19 +206,19 @@ func (w *responseWriter) Write(p []byte) (int, error) { w.used++ consumed++ - if w.used < 2 { + if w.used < responseMinimumHeaderBytes { continue } - if w.header[0] != 0x30 { + if w.header[0] != responseSequenceTag { return consumed, ErrFormat } - need := 2 + need := responseMinimumHeaderBytes - if w.header[1]&0x80 != 0 { - count := int(w.header[1] & 0x7f) - if count == 0 || count > len(w.header)-2 { + if w.header[1]&responseLongLengthFlag != 0 { + count := int(w.header[1] & responseLengthCountMask) + if count == 0 || count > len(w.header)-responseMinimumHeaderBytes { return consumed, ErrFormat } @@ -223,30 +243,30 @@ func (w *responseWriter) Write(p []byte) (int, error) { func (w *responseWriter) start() error { length := int(w.header[1]) - if length >= 128 { - if w.header[2] == 0 { + if length >= responseLongLengthFlag { + if w.header[responseMinimumHeaderBytes] == 0 { return ErrFormat } length = 0 - for _, b := range w.header[2:w.used] { - if length > (16<<20)/256 { + for _, b := range w.header[responseMinimumHeaderBytes:w.used] { + if length > maximumResponseBytes/responseLengthRadix { return ErrPolicy } - length = length*256 + int(b) + length = length*responseLengthRadix + int(b) } - if length < 128 { + if length < responseLongLengthFlag { return ErrFormat } } - status := der.Seq(der.Int(0)) + status := der.Seq(der.Int(statusGranted)) body := len(status) + w.used + length - header := der.Header(0x30, body) - if body > 16<<20-len(header) { + header := der.Header(responseSequenceTag, body) + if body > maximumResponseBytes-len(header) { return ErrPolicy } diff --git a/timestamp/timestamp_test.go b/timestamp/timestamp_test.go index bdb3fcc..f9e2495 100644 --- a/timestamp/timestamp_test.go +++ b/timestamp/timestamp_test.go @@ -284,7 +284,7 @@ func TestResponseWriterCompatibilityAndErrors(t *testing.T) { c := testcert.New(t) v := verifier(t, c) - evidence := pki.Evidence{CRLs: [][]byte{testcert.CRL(t, c)}} + e := pki.Evidence{CRLs: [][]byte{testcert.CRL(t, c)}} for _, h := range []crypto.Hash{crypto.SHA256, crypto.SHA384, crypto.SHA512} { q, err := timestamp.CreateRequest(t.Context(), []byte("payload"), timestamp.RequestOptions{Hash: h, Policy: policy()}) if err != nil { @@ -300,7 +300,7 @@ func TestResponseWriterCompatibilityAndErrors(t *testing.T) { t.Fatal("response size", n, err) } - result, err := v.VerifyResponseReader(t.Context(), bytes.NewReader(output.Bytes()), q, testcert.Now(), evidence) + result, err := v.VerifyResponseReader(t.Context(), bytes.NewReader(output.Bytes()), q, testcert.Now(), e) if err != nil || result.Info().Imprint().Hash != h { t.Fatal("writer response failed verification", err) } @@ -1047,7 +1047,7 @@ func TestTimestampTSAAlternativeNameBindings(t *testing.T) { t.Fatal(err) } v := verifier(t, chain) - evidence := pki.Evidence{CRLs: [][]byte{testcert.CRL(t, chain)}} + e := pki.Evidence{CRLs: [][]byte{testcert.CRL(t, chain)}} for _, tc := range []struct { name string tag int @@ -1076,7 +1076,7 @@ func TestTimestampTSAAlternativeNameBindings(t *testing.T) { if err != nil { t.Fatal(err) } - result, err := v.VerifyToken(t.Context(), token, timestamp.Expectation{Imprint: request.Imprint(), Nonce: request.Nonce(), Policy: request.Policy()}, testcert.Now(), evidence) + result, err := v.VerifyToken(t.Context(), token, timestamp.Expectation{Imprint: request.Imprint(), Nonce: request.Nonce(), Policy: request.Policy()}, testcert.Now(), e) if tc.want != nil { if result != nil || !errors.Is(err, tc.want) { t.Fatalf("TSA binding returned %v, %v; want %v", result != nil, err, tc.want) @@ -1181,67 +1181,6 @@ func TestTimestampRejectsUnevaluatedESSPathRestrictions(t *testing.T) { } } -func timestampDuplicateESSID(t *testing.T, raw []byte, key crypto.Signer, variant ...string) []byte { - t.Helper() - outer, _ := der.Read(raw, len(raw)) - ci, _ := der.Sequence(outer, 2, 2) - content, _ := der.Explicit(ci[1], 0) - sd, _ := der.Sequence(content, 4, 6) - signers, _ := der.SetFields(sd[len(sd)-1], 0, asn1.TagSet, 1) - signer, _ := der.Sequence(signers[0], 6, 6) - attributes, _ := der.Children(signer[3]) - attrs := make([][]byte, 0, len(attributes)) - for _, value := range attributes { - attribute, _ := der.Sequence(value, 2, 2) - var oid asn1.ObjectIdentifier - if der.Decode(attribute[0], &oid) != nil { - t.Fatal("attribute OID") - } - if oid.Equal(asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 16, 2, 47}) { - values, _ := der.SetFields(attribute[1], 0, asn1.TagSet, 1) - top, _ := der.Sequence(values[0], 1, 2) - ids, _ := der.Sequence(top[0], 1, 1) - if len(variant) == 0 { - attrs = append(attrs, der.Seq(attribute[0].FullBytes, der.Set(der.Seq(der.Seq(ids[0].FullBytes, ids[0].FullBytes))))) - } else { - id, _ := der.Sequence(ids[0], 2, 2) - issuer, _ := der.Sequence(id[1], 2, 2) - names, _ := der.Sequence(issuer[0], 1, 1) - extra := der.Wrap(2, 6, false, []byte("https://unrelated.test/")) - if variant[0] == "directoryName" { - extra = names[0].FullBytes - } - changed := der.Seq(id[0].FullBytes, der.Seq(der.Seq(names[0].FullBytes, extra), issuer[1].FullBytes)) - attrs = append(attrs, der.Seq(attribute[0].FullBytes, der.Set(der.Seq(der.Seq(changed))))) - } - } else { - attrs = append(attrs, value.FullBytes) - } - } - set := der.Set(attrs...) - signedAttrs, _ := der.Read(set, len(set)) - parts := make([][]byte, len(signer)) - for i := range signer { - parts[i] = signer[i].FullBytes - } - parts[3] = der.Wrap(2, 0, true, signedAttrs.Bytes) - algorithm, err := signature.Algorithm(signer[4]) - if err != nil { - t.Fatal(err) - } - signed, err := signature.Sign(t.Context(), key, algorithm, set) - if err != nil { - t.Fatal(err) - } - parts[5] = der.Octets(signed) - sdParts := make([][]byte, len(sd)) - for i := range sd { - sdParts[i] = sd[i].FullBytes - } - sdParts[len(sdParts)-1] = der.Set(der.Seq(parts...)) - return der.Seq(ci[0].FullBytes, der.Ctx(0, der.Seq(sdParts...))) -} - func TestTimestampRejectsExplicitDefaultFields(t *testing.T) { c := testcert.New(t) request, err := timestamp.CreateRequest(t.Context(), []byte("DER defaults"), timestamp.RequestOptions{Policy: policy(), OmitNonce: true, OmitCertificates: true}) @@ -1396,8 +1335,8 @@ func FuzzResponse(f *testing.F) { f.Fatal(e) } - evidence := pki.Evidence{CRLs: [][]byte{fixture.Read(f, "ts-ecdsa-256/crl.der")}} - if result, e := v.VerifyResponse(f.Context(), raw, q, fixture.At, evidence); e != nil || result == nil { + ev := pki.Evidence{CRLs: [][]byte{fixture.Read(f, "ts-ecdsa-256/crl.der")}} + if result, e := v.VerifyResponse(f.Context(), raw, q, fixture.At, ev); e != nil || result == nil { f.Fatalf("invalid positive timestamp fixture: %v", e) } @@ -1412,7 +1351,7 @@ func FuzzResponse(f *testing.F) { return } - result, e := v.VerifyResponse(t.Context(), b, q, fixture.At, evidence) + result, e := v.VerifyResponse(t.Context(), b, q, fixture.At, ev) if e != nil && result != nil || e == nil && (result == nil || result.Trust() == nil || result.Trust().RevocationStatus() != pki.Valid) { t.Fatal("inconsistent timestamp verification result") } @@ -1442,3 +1381,280 @@ func BenchmarkVerifyResponse(b *testing.B) { } } } + +func timestampDuplicateESSID(t *testing.T, raw []byte, key crypto.Signer, variant ...string) []byte { + t.Helper() + outer, _ := der.Read(raw, len(raw)) + ci, _ := der.Sequence(outer, 2, 2) + content, _ := der.Explicit(ci[1], 0) + sd, _ := der.Sequence(content, 4, 6) + signers, _ := der.SetFields(sd[len(sd)-1], 0, asn1.TagSet, 1) + signer, _ := der.Sequence(signers[0], 6, 6) + attributes, _ := der.Children(signer[3]) + attrs := make([][]byte, 0, len(attributes)) + for _, value := range attributes { + attribute, _ := der.Sequence(value, 2, 2) + var oid asn1.ObjectIdentifier + if der.Decode(attribute[0], &oid) != nil { + t.Fatal("attribute OID") + } + if oid.Equal(asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 16, 2, 47}) { + values, _ := der.SetFields(attribute[1], 0, asn1.TagSet, 1) + top, _ := der.Sequence(values[0], 1, 2) + ids, _ := der.Sequence(top[0], 1, 1) + if len(variant) == 0 { + attrs = append(attrs, der.Seq(attribute[0].FullBytes, der.Set(der.Seq(der.Seq(ids[0].FullBytes, ids[0].FullBytes))))) + } else { + id, _ := der.Sequence(ids[0], 2, 2) + issuer, _ := der.Sequence(id[1], 2, 2) + names, _ := der.Sequence(issuer[0], 1, 1) + extra := der.Wrap(2, 6, false, []byte("https://unrelated.test/")) + if variant[0] == "directoryName" { + extra = names[0].FullBytes + } + changed := der.Seq(id[0].FullBytes, der.Seq(der.Seq(names[0].FullBytes, extra), issuer[1].FullBytes)) + attrs = append(attrs, der.Seq(attribute[0].FullBytes, der.Set(der.Seq(der.Seq(changed))))) + } + } else { + attrs = append(attrs, value.FullBytes) + } + } + set := der.Set(attrs...) + signedAttrs, _ := der.Read(set, len(set)) + parts := make([][]byte, len(signer)) + for i := range signer { + parts[i] = signer[i].FullBytes + } + parts[3] = der.Wrap(2, 0, true, signedAttrs.Bytes) + algorithm, err := signature.Algorithm(signer[4]) + if err != nil { + t.Fatal(err) + } + signed, err := signature.Sign(t.Context(), key, algorithm, set) + if err != nil { + t.Fatal(err) + } + parts[5] = der.Octets(signed) + sdParts := make([][]byte, len(sd)) + for i := range sd { + sdParts[i] = sd[i].FullBytes + } + sdParts[len(sdParts)-1] = der.Set(der.Seq(parts...)) + return der.Seq(ci[0].FullBytes, der.Ctx(0, der.Seq(sdParts...))) +} + +func TestConfiguredCertificatesBuildTimestampPath(t *testing.T) { + for _, separateIntermediate := range []bool{false, true} { + name := "lookup candidates" + if separateIntermediate { + name = "retain PKI intermediates" + } + t.Run(name, func(t *testing.T) { + chain := testcert.New(t) + parent, parentKey := chain.Root, crypto.Signer(chain.RootKey) + var configured [][]byte + if separateIntermediate { + key := testcert.Key(t) + parent = testcert.Issue(t, timestampIntermediate(20, "configured CA"), parent, key.Public(), parentKey) + parentKey = key + configured = [][]byte{parent.Raw} + } + intermediate := testcert.Issue(t, timestampIntermediate(21, "lookup CA"), parent, chain.LeafKey.Public(), parentKey) + template := *chain.TSA + for _, extension := range chain.TSA.Extensions { + if extension.Id.Equal(asn1.ObjectIdentifier{2, 5, 29, 37}) { + template.ExtraExtensions = append(template.ExtraExtensions, extension) + } + } + tsa := testcert.Issue(t, &template, intermediate, chain.TSAKey.Public(), chain.LeafKey) + request, err := timestamp.CreateRequest(t.Context(), []byte("configured path candidates"), timestamp.RequestOptions{Policy: policy(), OmitCertificates: true}) + if err != nil { + t.Fatal(err) + } + raw, err := timestamp.IssueResponse(t.Context(), request.DER(), chain.TSAKey, tsa.Raw, timestamp.IssueOptions{Policy: policy(), SerialNumber: big.NewInt(22), GenTime: testcert.Now()}) + if err != nil { + t.Fatal(err) + } + config := timestamp.Config{ + PKI: pki.Config{Roots: [][]byte{chain.Root.Raw}, Intermediates: configured, Revocation: pki.NoRevocation}, + AllowedPolicies: []asn1.ObjectIdentifier{policy()}, + Certificates: [][]byte{tsa.Raw, intermediate.Raw}, + AllowUncheckedRevocation: true, + } + verifier, err := timestamp.NewVerifier(config) + if err != nil { + t.Fatal(err) + } + allCandidates := append([][]byte{tsa.Raw, intermediate.Raw}, configured...) + control, err := verifier.WithEvidenceCertificates(allCandidates) + if err != nil { + t.Fatal(err) + } + if result, err := control.VerifyResponse(t.Context(), raw, request, testcert.Now(), pki.Evidence{}); result == nil || err != nil { + t.Fatal("same candidate set must form a valid control path", err) + } + if result, err := verifier.VerifyResponse(t.Context(), raw, request, testcert.Now(), pki.Evidence{}); result == nil || err != nil { + t.Error("Config.Certificates did not supply the timestamp path", err) + } else if len(result.Trust().ChainDER()) != 3+len(configured) || result.Trust().RevocationStatus() != pki.NotChecked { + t.Error("configured path or unchecked-revocation result changed") + } + response, err := timestamp.ParseResponse(t.Context(), raw) + if err != nil { + t.Fatal(err) + } + token, err := timestamp.ParseToken(t.Context(), response.TokenDER()) + if err != nil { + t.Fatal(err) + } + want := timestamp.Expectation{Imprint: request.Imprint(), Nonce: request.Nonce(), Policy: request.Policy()} + if authenticated, err := verifier.Authenticate(t.Context(), token, want, testcert.Now().AddDate(100, 0, 0)); authenticated == nil || err != nil || !authenticated.Valid() { + t.Error("crypto-only authentication acquired a current-PKIX requirement", err) + } + session, err := verifier.NewHistoricalSession(t.Context(), pki.HistoricalOptions{}, testcert.Now(), pki.Evidence{}) + if err != nil { + t.Fatal(err) + } + if paths, err := session.Paths(t.Context(), tsa.Raw); err != nil || len(paths) != 1 || len(paths[0].ChainDER()) != 3+len(configured) { + t.Error("historical discovery lost configured path candidates", err) + } + config.PKI.Roots = [][]byte{testcert.New(t).Root.Raw} + config.Certificates = append([][]byte(nil), allCandidates...) + config.Certificates = append(config.Certificates, chain.Root.Raw) + untrusted, err := timestamp.NewVerifier(config) + if err != nil { + t.Fatal(err) + } + if result, err := untrusted.VerifyResponse(t.Context(), raw, request, testcert.Now(), pki.Evidence{}); result != nil || !errors.Is(err, timestamp.ErrTrust) { + t.Error("lookup candidates became trust anchors", err) + } + }) + } +} + +func TestConfiguredCertificatesSharePKIBounds(t *testing.T) { + chain := testcert.New(t) + config := timestamp.Config{ + PKI: pki.Config{Roots: [][]byte{chain.Root.Raw}, Intermediates: [][]byte{chain.TSA.Raw}}, + AllowedPolicies: []asn1.ObjectIdentifier{policy()}, + Certificates: [][]byte{chain.TSA.Raw}, + AllowUncheckedRevocation: true, + } + config.PKI.MaxEvidenceBytes = len(chain.Root.Raw) + len(chain.TSA.Raw) + if verifier, err := timestamp.NewVerifier(config); verifier == nil || err != nil { + t.Fatal("duplicate configured certificate must consume one byte budget", err) + } + config.PKI.Intermediates = nil + config.PKI.MaxEvidenceBytes-- + if verifier, err := timestamp.NewVerifier(config); verifier != nil || !errors.Is(err, pki.ErrPolicy) { + t.Fatal("configured lookup candidate escaped aggregate PKI byte bound", err) + } + if len(config.Certificates) != 1 || !bytes.Equal(config.Certificates[0], chain.TSA.Raw) { + t.Fatal("constructor mutated caller certificate configuration") + } + + config.PKI.MaxEvidenceBytes = 0 + for index := range 64 { + template := *chain.TSA + template.SerialNumber = big.NewInt(int64(100 + index)) + certificate := testcert.Issue(t, &template, chain.Root, chain.TSAKey.Public(), chain.RootKey) + config.PKI.Intermediates = append(config.PKI.Intermediates, certificate.Raw) + } + if verifier, err := timestamp.NewVerifier(config); verifier != nil || !errors.Is(err, pki.ErrPolicy) { + t.Fatal("configured lookup candidate escaped combined PKI certificate count", err) + } + config.Certificates = config.PKI.Intermediates[:1] + if verifier, err := timestamp.NewVerifier(config); verifier == nil || err != nil { + t.Fatal("duplicate candidate must not exceed the 64-certificate count", err) + } +} + +func timestampIntermediate(serial int64, name string) *x509.Certificate { + return &x509.Certificate{ + SerialNumber: big.NewInt(serial), Subject: pkix.Name{CommonName: name}, + NotBefore: testcert.Now().Add(-time.Hour), NotAfter: testcert.Now().Add(time.Hour), + IsCA: true, BasicConstraintsValid: true, KeyUsage: x509.KeyUsageCertSign, + } +} + +func TestCheckEvidenceLimits(t *testing.T) { + chain := testcert.New(t) + verifier, err := timestamp.NewVerifier(timestamp.Config{PKI: pki.Config{Roots: [][]byte{chain.Root.Raw}, Revocation: pki.RequireCRL, MaxEvidenceBytes: 4096}, Certificates: [][]byte{chain.TSA.Raw}, AllowedPolicies: []asn1.ObjectIdentifier{{1, 2, 3, 4}}}) + if err != nil { + t.Fatal(err) + } + roots := make([][]byte, 64) + for i := range roots { + roots[i] = chain.Root.Raw + } + dedup := append([][]byte(nil), roots...) + dedup[0] = chain.TSA.Raw + for _, tc := range []struct { + name string + input pki.Evidence + over bool + }{ + {name: "empty adds lookup"}, + {name: "combined exact bytes", input: pki.Evidence{CRLs: [][]byte{make([]byte, 4096-len(chain.TSA.Raw))}}}, + {name: "combined byte over", input: pki.Evidence{CRLs: [][]byte{make([]byte, 4097-len(chain.TSA.Raw))}}, over: true}, + {name: "raw count over", input: pki.Evidence{Intermediates: make([][]byte, 65)}, over: true}, + } { + t.Run(tc.name, func(t *testing.T) { + var first []byte + if len(tc.input.Intermediates) > 0 { + first = bytes.Clone(tc.input.Intermediates[0]) + } + err := verifier.CheckEvidenceLimits(t.Context(), tc.input) + if tc.over { + if !errors.Is(err, pki.ErrLimit) { + t.Fatal("limit", err) + } + } else if err != nil { + t.Fatal(err) + } + if len(tc.input.Intermediates) > 0 && !bytes.Equal(tc.input.Intermediates[0], first) { + t.Fatal("input overwritten") + } + }) + } + // A separate generous byte budget isolates configured lookup count and dedup. + counted, err := timestamp.NewVerifier(timestamp.Config{PKI: pki.Config{Roots: [][]byte{chain.Root.Raw}, Revocation: pki.RequireCRL, MaxEvidenceBytes: 128 << 10}, Certificates: [][]byte{chain.TSA.Raw}, AllowedPolicies: []asn1.ObjectIdentifier{{1, 2, 3, 4}}}) + if err != nil { + t.Fatal(err) + } + for _, input := range []pki.Evidence{{Intermediates: roots[:63]}, {Intermediates: dedup}} { + if err := counted.CheckEvidenceLimits(t.Context(), input); err != nil { + t.Fatal("count or dedup positive", err) + } + } + for i := range roots { + if !bytes.Equal(roots[i], chain.Root.Raw) { + t.Fatal("lookup merge overwrote caller slice", i) + } + } + if err := counted.CheckEvidenceLimits(t.Context(), pki.Evidence{Intermediates: roots}); !errors.Is(err, pki.ErrLimit) { + t.Fatal("combined count", err) + } + for _, v := range []*timestamp.Verifier{nil, {}} { + if err := v.CheckEvidenceLimits(t.Context(), pki.Evidence{}); !errors.Is(err, timestamp.ErrPolicy) { + t.Fatal("invalid verifier", err) + } + } + if err := verifier.CheckEvidenceLimits(nil, pki.Evidence{}); !errors.Is(err, pki.ErrPolicy) { + t.Fatal("nil context identity", err) + } + ctx, cancel := context.WithCancel(t.Context()) + cancel() + if err := verifier.CheckEvidenceLimits(ctx, pki.Evidence{Intermediates: make([][]byte, 65)}); !errors.Is(err, context.Canceled) { + t.Fatal("canceled preflight", err) + } + over := pki.Evidence{Intermediates: make([][]byte, 65)} + if session, err := verifier.NewHistoricalSession(nil, pki.HistoricalOptions{}, testcert.Now(), over); session != nil || !errors.Is(err, pki.ErrPolicy) || errors.Is(err, pki.ErrLimit) { + t.Fatal("nil context request identity", session, err) + } + if session, err := verifier.NewHistoricalSession(ctx, pki.HistoricalOptions{}, time.Time{}, over); session != nil || !errors.Is(err, pki.ErrPolicy) || errors.Is(err, pki.ErrLimit) { + t.Fatal("zero time request identity", session, err) + } + if session, err := verifier.NewHistoricalSession(ctx, pki.HistoricalOptions{}, testcert.Now(), over); session != nil || !errors.Is(err, context.Canceled) { + t.Fatal("session cancellation cause", session, err) + } +} diff --git a/timestamp/verify.go b/timestamp/verify.go index ad74a96..45066b6 100644 --- a/timestamp/verify.go +++ b/timestamp/verify.go @@ -3,23 +3,39 @@ package timestamp import ( "bytes" "context" + "crypto/sha256" "crypto/x509" "encoding/asn1" "errors" "fmt" "io" "math/big" + "net" "slices" "time" "github.com/skarm/xmlsec/cms" "github.com/skarm/xmlsec/internal/der" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/nilcheck" "github.com/skarm/xmlsec/internal/x509cert" "github.com/skarm/xmlsec/internal/x509name" "github.com/skarm/xmlsec/pki" ) +const ( + // Caps each TSA lookup or signer certificate encoding before parsing, in bytes. + maximumCertificateBytes = 1 << 20 + // Caps the configured TSA lookup-certificate set and immutable replacement snapshots. + maximumLookupCertificates = 32 +) + +const ( + // Limits explicit clock tolerance and the number of distinct permitted TSA policy OIDs. + maximumClockSkew = 10 * time.Minute + maximumPolicies = 64 +) + // Config supplies TSA trust and token validation policy. Construction copies its // configuration. Embedded certificates are untrusted path candidates and cannot // expand PKI.Roots. PKI must require revocation checking unless @@ -54,30 +70,41 @@ type Config struct { // Verifier checks RFC 3161 tokens against explicit TSA policy, certificate trust and // offline revocation evidence. type Verifier struct { - trust *pki.Verifier - policies map[string]struct{} - certificates [][]byte - skew time.Duration - maxAge time.Duration - maxAccuracy time.Duration - maxBytes int - allowUnchecked bool - legacy bool + // trust supplies explicit TSA certificate and revocation policy. + trust *pki.Verifier + // policies contains the explicitly accepted TSA policy OIDs. + policies map[string]struct{} + // certificates retains application-supplied TSA certificate candidates. + certificates [][]byte + // skew is the accepted generation-time clock tolerance. + skew time.Duration + // maxAge bounds token age when the configured policy requires it. + maxAge time.Duration + // maxAccuracy bounds the accepted TSA uncertainty. + maxAccuracy time.Duration + // maxBytes bounds encoded token and response input. + maxBytes int + // allowUnchecked explicitly permits TSA verification without a successful + // revocation check. + allowUnchecked bool + // legacy permits the legacy SHA-1 ESSCertID certificate-binding attribute. + legacy bool + // requireAccuracy rejects tokens that omit authenticated accuracy. requireAccuracy bool } // NewVerifier validates and snapshots explicit TSA policies, trust anchors and evidence // limits. func NewVerifier(c Config) (*Verifier, error) { - if len(c.AllowedPolicies) < 1 || len(c.AllowedPolicies) > 64 || len(c.Certificates) > 32 || c.ClockSkew < 0 || c.ClockSkew > 10*time.Minute || c.MaxAge < 0 || c.MaxAccuracy < 0 || c.MaxAccuracy > 24*time.Hour { + if len(c.AllowedPolicies) < 1 || len(c.AllowedPolicies) > maximumPolicies || len(c.Certificates) > maximumLookupCertificates || c.ClockSkew < 0 || c.ClockSkew > maximumClockSkew || c.MaxAge < 0 || c.MaxAccuracy < 0 || c.MaxAccuracy > maximumAccuracy { return nil, ErrPolicy } if c.MaxBytes == 0 { - c.MaxBytes = 16 << 20 + c.MaxBytes = maximumResponseBytes } - if c.MaxBytes < 1 || c.MaxBytes > 16<<20 { + if c.MaxBytes < 1 || c.MaxBytes > maximumResponseBytes { return nil, ErrPolicy } @@ -110,7 +137,7 @@ func NewVerifier(c Config) (*Verifier, error) { total := 0 for _, b := range c.Certificates { total += len(b) - if len(b) > 1<<20 || total > c.MaxBytes { + if len(b) > maximumCertificateBytes || total > c.MaxBytes { return nil, ErrPolicy } @@ -121,9 +148,43 @@ func NewVerifier(c Config) (*Verifier, error) { v.certificates = append(v.certificates, bytes.Clone(b)) } + if len(v.certificates) > 0 { + // Both groups have already passed their count and byte limits. Keep + // configured PKI intermediates and charge every distinct candidate to + // the same PKI constructor limits without promoting any trust anchor. + c.PKI.Intermediates = mergeCertificateCandidates(c.PKI.Intermediates, v.certificates) + + v.trust, e = pki.NewVerifier(c.PKI) + if e != nil { + return nil, e + } + } + return v, nil } +// mergeCertificateCandidates is called only after both groups pass their input +// limits. The PKI constructor then checks the combined candidate policy. +func mergeCertificateCandidates(configured, lookup [][]byte) [][]byte { + seen := make(map[[sha256.Size]byte]struct{}) + intermediates := make([][]byte, 0, len(configured)+len(lookup)) + + for _, group := range [][][]byte{configured, lookup} { + for _, certificate := range group { + digest := sha256.Sum256(certificate) + if _, exists := seen[digest]; exists { + continue + } + + seen[digest] = struct{}{} + + intermediates = append(intermediates, certificate) + } + } + + return intermediates +} + // VerifyToken checks PKIX at the explicit validation time, never silently at // the token's claimed genTime. Historical LTA validation is a separate process. func (v *Verifier) VerifyToken(ctx context.Context, raw []byte, want Expectation, at time.Time, evidence pki.Evidence) (*Result, error) { @@ -135,7 +196,7 @@ func (v *Verifier) VerifyToken(ctx context.Context, raw []byte, want Expectation return nil, e } - if want.Nonce != nil && (want.Nonce.Sign() <= 0 || want.Nonce.BitLen() > 256) { + if want.Nonce != nil && (want.Nonce.Sign() <= 0 || want.Nonce.BitLen() > maximumNonceBits) { return nil, ErrPolicy } @@ -226,7 +287,7 @@ func (v *Verifier) verify(ctx context.Context, token *Token, want Expectation, a return nil, nil, err } - if want.Nonce != nil && (want.Nonce.Sign() <= 0 || want.Nonce.BitLen() > 256) { + if want.Nonce != nil && (want.Nonce.Sign() <= 0 || want.Nonce.BitLen() > maximumNonceBits) { return nil, nil, ErrPolicy } @@ -338,132 +399,6 @@ func (v *Verifier) validateTimestampTime(info Info, at time.Time) error { return nil } -func tokenEvidence(evidence pki.Evidence, certificates [][]byte, chosen []byte) pki.Evidence { - ev := evidence - ev.Intermediates = append([][]byte(nil), evidence.Intermediates...) - - for _, c := range certificates { - if !bytes.Equal(c, chosen) { - ev.Intermediates = append(ev.Intermediates, c) - } - } - - return ev -} - -func checkTSACertificate(c *x509.Certificate, at time.Time) error { - if c == nil || c.IsCA || c.KeyUsage&x509.KeyUsageDigitalSignature == 0 || len(c.ExtKeyUsage) != 1 || c.ExtKeyUsage[0] != x509.ExtKeyUsageTimeStamping || len(c.UnknownExtKeyUsage) > 0 || at.Before(c.NotBefore) || at.After(c.NotAfter) { - return ErrTrust - } - - critical := false - - for _, e := range c.Extensions { - if e.Id.Equal(asn1.ObjectIdentifier{2, 5, 29, 37}) { - critical = e.Critical - } - } - - if !critical { - return fmt.Errorf("%w: critical timeStamping-only EKU required", ErrTrust) - } - - return nil -} - -func matchTSAName(ctx context.Context, n asn1.RawValue, c *x509.Certificate) error { - if len(n.FullBytes) == 0 { - return nil - } - - if n.Class != 2 { - return ErrFormat - } - - switch n.Tag { - case 4: - rv, e := der.Explicit(n, 4) - if e != nil { - return ErrFormat - } - - equal, err := x509name.EqualOrExact(ctx, rv.FullBytes, c.RawSubject) - if err != nil { - return fmt.Errorf("%w: TSA name: %w", ErrTrust, err) - } - - if equal { - return nil - } - case 1: - if n.IsCompound { - return ErrFormat - } - - if slices.Contains(c.EmailAddresses, string(n.Bytes)) { - return nil - } - case 2: - if n.IsCompound { - return ErrFormat - } - - if slices.Contains(c.DNSNames, string(n.Bytes)) { - return nil - } - case 6: - if n.IsCompound { - return ErrFormat - } - - if tsaURINameMatches(string(n.Bytes), c) { - return nil - } - - case 7: - if n.IsCompound || (len(n.Bytes) != 4 && len(n.Bytes) != 16) { - return ErrFormat - } - - if tsaIPNameMatches(n.Bytes, c) { - return nil - } - - default: - return ErrFormat - } - - return fmt.Errorf("%w: tsa name", ErrTrust) -} - -func tsaURINameMatches(name string, c *x509.Certificate) bool { - for _, uri := range c.URIs { - if uri.String() == name { - return true - } - } - - return false -} - -func tsaIPNameMatches(name []byte, c *x509.Certificate) bool { - for _, ip := range c.IPAddresses { - if ip.Equal(name) { - return true - } - } - - return false -} - -func validateTimestampBinding(info Info, want Expectation) error { - if want.Imprint.Hash != info.imprint.Hash || !bytes.Equal(want.Imprint.Digest, info.imprint.Digest) || len(want.Policy) > 0 && !want.Policy.Equal(info.policy) || want.Nonce != nil && (info.nonce == nil || want.Nonce.Cmp(info.nonce) != 0) { - return ErrBinding - } - - return nil -} - // Expectation supplies the required data imprint and optional exact nonce and TSA // policy bindings. type Expectation struct { @@ -478,8 +413,10 @@ type Expectation struct { // Token retains an immutable parsed RFC 3161 CMS token. Use Verifier.Verify to // authenticate its information and certificates before accepting its claims. type Token struct { + // signed owns the parsed CMS container; parsing alone authenticates nothing. signed *cms.SignedData - info Info + // info retains the parsed, unverified timestamp claims. + info Info } // ParseToken parses a bounded token into owned storage, without authenticating it. @@ -490,7 +427,7 @@ func ParseToken(ctx context.Context, raw []byte) (*Token, error) { // ParseTokenReader reads one token, retaining its complete DER and parsed model. // It limits DER to 16 MiB and TSTInfo to 1 MiB, and leaves src open. func ParseTokenReader(ctx context.Context, src io.Reader) (*Token, error) { - return parseTokenReader(ctx, src, 16<<20) + return parseTokenReader(ctx, src, maximumResponseBytes) } // UnverifiedInfo returns parsed timestamp claims, without authenticating them. @@ -543,10 +480,15 @@ func (t *Token) WriterTo(ctx context.Context) io.WriterTo { // Result retains an immutable timestamp token with authenticated TSTInfo and the // accepted TSA trust result. type Result struct { - token *Token - info Info - tsa []byte - trust *pki.Result + // token is the token accepted by the configured verifier. + token *Token + // info retains its authenticated timestamp claims. + info Info + // tsa owns the exact accepted TSA certificate DER. + tsa []byte + // trust retains the successful TSA certificate and revocation result. + trust *pki.Result + // assertion binds the accepted token, imprint, time and trust result. assertion *VerifiedAssertion } @@ -606,8 +548,138 @@ func InspectToken(ctx context.Context, b []byte) (Info, error) { return token.UnverifiedInfo(), nil } +func tokenEvidence(evidence pki.Evidence, certificates [][]byte, chosen []byte) pki.Evidence { + ev := evidence + ev.Intermediates = append([][]byte(nil), evidence.Intermediates...) + + for _, c := range certificates { + if !bytes.Equal(c, chosen) { + ev.Intermediates = append(ev.Intermediates, c) + } + } + + return ev +} + +func checkTSACertificate(c *x509.Certificate, at time.Time) error { + const requiredTSAKeyUsages = 1 + + if c == nil || c.IsCA || c.KeyUsage&x509.KeyUsageDigitalSignature == 0 || len(c.ExtKeyUsage) != requiredTSAKeyUsages || c.ExtKeyUsage[0] != x509.ExtKeyUsageTimeStamping || len(c.UnknownExtKeyUsage) > 0 || at.Before(c.NotBefore) || at.After(c.NotAfter) { + return ErrTrust + } + + critical := false + + for _, e := range c.Extensions { + if e.Id.Equal(asn1.ObjectIdentifier{2, 5, 29, 37}) { + critical = e.Critical + } + } + + if !critical { + return fmt.Errorf("%w: critical timeStamping-only EKU required", ErrTrust) + } + + return nil +} + +func matchTSAName(ctx context.Context, n asn1.RawValue, c *x509.Certificate) error { + if len(n.FullBytes) == 0 { + return nil + } + + if n.Class != asn1.ClassContextSpecific { + return ErrFormat + } + + switch n.Tag { + case lexicon.GeneralNameDirectory: + rv, e := der.Explicit(n, lexicon.GeneralNameDirectory) + if e != nil { + return ErrFormat + } + + equal, err := x509name.EqualOrExact(ctx, rv.FullBytes, c.RawSubject) + if err != nil { + return fmt.Errorf("%w: TSA name: %w", ErrTrust, err) + } + + if equal { + return nil + } + case lexicon.GeneralNameRFC822: + if n.IsCompound { + return ErrFormat + } + + if slices.Contains(c.EmailAddresses, string(n.Bytes)) { + return nil + } + case lexicon.GeneralNameDNS: + if n.IsCompound { + return ErrFormat + } + + if slices.Contains(c.DNSNames, string(n.Bytes)) { + return nil + } + case lexicon.GeneralNameURI: + if n.IsCompound { + return ErrFormat + } + + if tsaURINameMatches(string(n.Bytes), c) { + return nil + } + + case lexicon.GeneralNameIP: + if n.IsCompound || (len(n.Bytes) != net.IPv4len && len(n.Bytes) != net.IPv6len) { + return ErrFormat + } + + if tsaIPNameMatches(n.Bytes, c) { + return nil + } + + default: + return ErrFormat + } + + return fmt.Errorf("%w: tsa name", ErrTrust) +} + +func tsaURINameMatches(name string, c *x509.Certificate) bool { + for _, uri := range c.URIs { + if uri.String() == name { + return true + } + } + + return false +} + +func tsaIPNameMatches(name []byte, c *x509.Certificate) bool { + for _, ip := range c.IPAddresses { + if ip.Equal(name) { + return true + } + } + + return false +} + +func validateTimestampBinding(info Info, want Expectation) error { + if want.Imprint.Hash != info.imprint.Hash || !bytes.Equal(want.Imprint.Digest, info.imprint.Digest) || len(want.Policy) > 0 && !want.Policy.Equal(info.policy) || want.Nonce != nil && (info.nonce == nil || want.Nonce.Cmp(info.nonce) != 0) { + return ErrBinding + } + + return nil +} + func parseTokenReader(ctx context.Context, src io.Reader, maximum int) (*Token, error) { - s, err := cms.ParseReader(ctx, src, cms.Limits{MaxBytes: maximum, MaxContentBytes: min(maximum, 1<<20)}) + const requiredTokenSigners = 1 + + s, err := cms.ParseReader(ctx, src, cms.Limits{MaxBytes: maximum, MaxContentBytes: min(maximum, maximumInfoBytes)}) if err != nil { if errors.Is(err, cms.ErrPolicy) { return nil, fmt.Errorf("%w: %w", ErrPolicy, err) @@ -616,7 +688,7 @@ func parseTokenReader(ctx context.Context, src io.Reader, maximum int) (*Token, return nil, fmt.Errorf("%w: %w", ErrFormat, err) } - if s.SignerCount() != 1 || s.Detached() || !s.ContentType().Equal(tstOID()) { + if s.SignerCount() != requiredTokenSigners || s.Detached() || !s.ContentType().Equal(tstOID()) { return nil, ErrFormat } diff --git a/transform/comparison_v1_test.go b/transform/comparison_v1_test.go index e47725a..d70a097 100644 --- a/transform/comparison_v1_test.go +++ b/transform/comparison_v1_test.go @@ -1267,96 +1267,6 @@ func TestCompareSaxon20CompilerContracts(t *testing.T) { } } -func reservedNameProofStylesheet(version, name string, prefixed bool) string { - function := `` - call := name + "(1,2)" - if prefixed { - call = "p:" + call - } - return sheet(version, function+``+valueOfExpression(call)+``) -} - -func compareSaxonCompiler(t *testing.T, binary, classpath, source, module, want string, processor transform.XSLTVersion, oracle ...string) { - t.Helper() - directory := t.TempDir() - main := filepath.Join(directory, "main.xsl") - for name, data := range map[string]string{"main.xsl": source, "module.xsl": module} { - if err := os.WriteFile(filepath.Join(directory, name), []byte(data), 0o600); err != nil { - t.Fatal(err) - } - } - config := transform.Config{Resolver: transform.ResolverFunc(func(ctx context.Context, uri string) (io.ReadCloser, error) { - if !strings.HasSuffix(uri, "/module.xsl") { - return nil, transform.ErrResource - } - return io.NopCloser(strings.NewReader(module)), ctx.Err() - })} - if processor == transform.XSLT20 { - // The pinned Saxon 9.4 comparison selects XSD 1.0 builtin datatypes. - config.XMLSchemaVersion = transform.XMLSchema10 - } - e := engine(t, config) - compiled, err := e.CompileXSLT(t.Context(), []byte(source), transform.XSLTOptions{ProcessorVersion: processor, BaseURI: (&url.URL{Scheme: "file", Path: filepath.ToSlash(main)}).String()}) - got := "" - if err == nil { - result, executeErr := compiled.Transform(t.Context(), nil, transform.XSLTContext{Template: transform.Name{Local: nameStart}}) - err = executeErr - if err == nil { - got = string(result.Primary) - } - } - if err != nil { - var detail *transform.Error - if !errors.As(err, &detail) { - t.Fatal(err) - } - got = detail.Code - } - if got != want { - t.Fatal("native", processor, got, err, want) - } - publishedProofVector(t, want) - nativeWant := want - if len(oracle) > 0 { - want = oracle[0] - } - ctx, cancel := context.WithTimeout(t.Context(), 20*time.Second) - defer cancel() - output, err := exec.CommandContext(ctx, binary, saxonCompilerArguments(classpath, main, processor)...).Output() - if diagnostic, separate := strings.CutPrefix(want, "diagnostic:"); separate { - var failure *exec.ExitError - if !errors.As(err, &failure) || !strings.Contains(string(failure.Stderr), diagnostic) { - t.Fatal("independent compiler diagnostic", output, err, want) - } - - saxonProofAgreement(t, binary, classpath, false) - t.Log("Independent engine diagnostic; excluded from exact agreements:", diagnostic) - - return - } - - got = string(output) - if err != nil { - got = saxonDiagnosticCode(err) - } - if got != want { - var detail *exec.ExitError - if errors.As(err, &detail) { - t.Fatalf("Saxon: %q, %s; want %q", output, detail.Stderr, want) - } - t.Fatalf("Saxon: %q, %v; want %q", output, err, want) - } - saxonProofAgreement(t, binary, classpath, nativeWant == want) -} - -func saxonCompilerArguments(classpath, main string, processor transform.XSLTVersion) []string { - args := []string{"-cp", classpath, "net.sf.saxon.Transform"} - if processor == transform.XSLT20 { - args = append(args, "-xsltversion:2.0") - } - return append(args, "-xsl:"+main, "-it:start") -} - func TestCompareSaxonResidualEditionProofs(t *testing.T) { classpath, binary := saxonRuntime(t) oldClasspath := os.Getenv("SAXON20_CLASSPATH") @@ -2151,92 +2061,6 @@ func TestCompareSaxonBuildTreeDiagnostic(t *testing.T) { } } -func compareSaxonExpression(t *testing.T, ctx context.Context, e *transform.Engine, binary, classpath, expression, want string, oracleWant ...string) { - t.Helper() - compareSaxonConfiguredExpression(t, ctx, e, binary, classpath, expression, want, nil, oracleWant...) -} - -func compareSaxonConfiguredExpression(t *testing.T, ctx context.Context, e *transform.Engine, binary, classpath, expression, want string, options []string, oracleWant ...string) { - t.Helper() - - program, err := e.CompileXPath(ctx, expression, transform.XPathOptions{Version: transform.XPath31}) - - var value transform.Sequence - if err == nil { - value, err = program.Evaluate(ctx, nil, transform.XPathContext{}) - } - - oracle := strings.ReplaceAll(expression, "&", "&") - - if comparisonErrorCode(want) { - var detail *transform.Error - if !errors.As(err, &detail) || detail.Code != want { - t.Fatal(value, err, want) - } - } else if err != nil || text(t, value) != want { - t.Fatal(value, err, want) - } - - publishedProofVector(t, want) - expected := want - if len(oracleWant) != 0 { - expected = oracleWant[0] - } - if expected != want { - saxonProofAgreement(t, binary, classpath, false) - t.Log("Independent diagnostic; excluded from exact agreements:", want, expected) - } - - nonstandardCode, nonstandard := strings.CutPrefix(expected, "error:") - if nonstandard { - expected = nonstandardCode - } - - if comparisonErrorCode(expected) || nonstandard { - oracle = "try { " + oracle + " } catch * { local-name-from-QName($err:code) }" - } - - output, err := saxonQuery(t, ctx, binary, classpath, oracle, options...) - if diagnostic, separate := strings.CutPrefix(expected, "diagnostic:"); separate { - var failure *exec.ExitError - if !errors.As(err, &failure) || !strings.Contains(string(failure.Stderr), diagnostic) { - t.Fatalf("independent diagnostic: %q, %v; want %q", output, err, diagnostic) - } - return - } - if target, crash := strings.CutPrefix(expected, "crash:"); crash { - var failure *exec.ExitError - if !errors.As(err, &failure) { - t.Fatalf("expected pinned Saxon crash diagnostic: %q, %v", output, err) - } - - diagnostic := string(failure.Stderr) - if !strings.Contains(diagnostic, "java.lang.ClassCastException") || !strings.Contains(diagnostic, target) { - t.Fatal("unexpected independent engine failure", diagnostic) - } - - return - } - - if err != nil && comparisonErrorCode(expected) { - if code := saxonDiagnosticCode(err); code != "" { - output, err = []byte(code), nil - } - } - - if err != nil || string(output) != expected { - var failure *exec.ExitError - if errors.As(err, &failure) { - t.Log(string(failure.Stderr)) - } - - t.Fatalf("Saxon %s: %q, %v; want %q", expression, output, err, expected) - } - if expected == want { - saxonProofAgreement(t, binary, classpath, true) - } -} - // Unicode 7.0.0 UnicodeData.txt already maps U+037F to U+03F3 and back. // The pinned Saxon HE 12.10 case tables retain the older unchanged characters. // Keep this diagnostic separate from agreement evidence and publisher totals. @@ -2283,72 +2107,6 @@ func TestCompareSaxonXMLDeclarationDiagnostic(t *testing.T) { } } -func saxonProofOracle(t *testing.T, binary, classpath string) string { - t.Helper() - key := binary + "\x00" + classpath - if found, ok := saxonProofVersions.Load(key); ok { - return found.(string) - } - ctx, cancel := context.WithTimeout(t.Context(), 20*time.Second) - defer cancel() - output, err := exec.CommandContext(ctx, binary, "-cp", classpath, "net.sf.saxon.Version").CombinedOutput() - if err != nil { - t.Fatal("independent processor identity", string(output), err) - } - oracle := "" - switch { - case strings.Contains(string(output), "SAXON-J-HE 12.10"): - oracle = "saxon-12.10" - case strings.Contains(string(output), "SAXON 9.4.0.9"): - oracle = "saxon-9.4" - default: - t.Fatal("unreviewed independent processor version", string(output)) - } - saxonProofVersions.Store(key, oracle) - return oracle -} - -func saxonProofAgreement(t *testing.T, binary, classpath string, agreement bool) { - t.Helper() - marker := "P3_ORACLE " - if !agreement { - marker = "P3_DIAGNOSTIC " - } - t.Log(marker + saxonProofOracle(t, binary, classpath)) -} - -func saxonRuntime(t *testing.T) (string, string) { - t.Helper() - - classpath := os.Getenv("SAXON_CLASSPATH") - if classpath == "" { - t.Skip("SAXON_CLASSPATH is not configured") - } - - binary, err := exec.LookPath("java") - if err != nil { - t.Fatal("configured Saxon requires java", err) - } - - return classpath, binary -} - -func saxonQuery(t *testing.T, ctx context.Context, binary, classpath, source string, options ...string) ([]byte, error) { - t.Helper() - // A query file avoids the JVM command-line charset, which can replace - // Unicode characters under the Docker image's C locale. - queryPath := filepath.Join(t.TempDir(), "query.xq") - if err := os.WriteFile(queryPath, []byte(source), 0o600); err != nil { - t.Fatal(err) - } - - arguments := make([]string, 0, 5+len(options)) - arguments = append(arguments, "-cp", classpath, "net.sf.saxon.Query", "-q:"+queryPath, "!method=text") - arguments = append(arguments, options...) - - return exec.CommandContext(ctx, binary, arguments...).Output() -} - func TestCompareSaxonTransformAssertionOptions(t *testing.T) { classpath, binary := saxonRuntime(t) @@ -2436,61 +2194,11 @@ func TestCompareSaxonBasicProfile(t *testing.T) { } } -func compareSaxonStylesheet(t *testing.T, ctx context.Context, e *transform.Engine, binary, classpath, body, want string, oracleWant ...string) { - t.Helper() - - source := sheet(version30, body) - expression := `let $s := '` + strings.ReplaceAll(source, "'", "''") + `' return transform(map{'stylesheet-text':$s,'initial-template':QName('', 'start'),'delivery-format':'serialized'})?output` +func TestCompareSaxonAttributeSetCycleDiagnostic(t *testing.T) { + classpath, binary := saxonRuntime(t) - result, err := query(t, e, expression, transform.XPath31).Evaluate(ctx, nil, transform.XPathContext{}) - got := "" - - if err != nil { - var detail *transform.Error - if !errors.As(err, &detail) { - t.Fatal(err) - } - - got = detail.Code - } else { - got = text(t, result) - } - - if got != want { - t.Fatal(body, got, want) - } - - publishedProofVector(t, want) - - // The oracle runs XQuery, whose string literals decode entity references. - output, err := saxonQuery(t, ctx, binary, classpath, strings.ReplaceAll(expression, "&", "&")) - - expected := want - if len(oracleWant) != 0 { - expected = oracleWant[0] - } - - got = string(output) - if err != nil { - got = saxonDiagnosticCode(err) - } - - if got != expected { - var diagnostic *exec.ExitError - if errors.As(err, &diagnostic) { - t.Fatalf("Saxon %s: %q, %s; want %q", body, output, diagnostic.Stderr, expected) - } - - t.Fatalf("Saxon %s: %q, %v; want %q", body, output, err, expected) - } - saxonProofAgreement(t, binary, classpath, expected == want) -} - -func TestCompareSaxonAttributeSetCycleDiagnostic(t *testing.T) { - classpath, binary := saxonRuntime(t) - - ctx, cancel := context.WithTimeout(t.Context(), time.Minute) - defer cancel() + ctx, cancel := context.WithTimeout(t.Context(), time.Minute) + defer cancel() e := engine(t, transform.Config{}) // XSLT 3.0 9.11 removed the old static XTSE0720 classification. @@ -2659,26 +2367,6 @@ func TestCompareSaxonEncodedXMLInputs(t *testing.T) { } } -// Static type errors may be diagnosed before XQuery try/catch can run. -func saxonDiagnosticCode(err error) string { - var diagnostic *exec.ExitError - if !errors.As(err, &diagnostic) { - return "" - } - - for _, line := range strings.Split(string(diagnostic.Stderr), "\n") { - fields := strings.Fields(line) - if len(fields) > 0 { - code := strings.TrimSuffix(fields[0], ":") - if comparisonErrorCode(code) && !strings.HasPrefix(code, "SXWN") { - return code - } - } - } - - return "" -} - func TestCompareLibxmlXPath10(t *testing.T) { binary, err := exec.LookPath("xmllint") if err != nil { @@ -3488,22 +3176,6 @@ func TestCompareSaxonHTMLCDATA(t *testing.T) { } } -// Serialization 3.1 sections 7.1/7.4.4 apply XML rules to islands and ignore -// CDATA on HTML5 XHTML elements. Saxon retains separate namespace/CR/DEL/PI -// behavior in these bounded diagnostics; they are not exact agreements. -// XML 1.0 CDATA diagnostics also retain Saxon's literal C1/NEL/LS output, -// while SER31 section 5 requires references even in that output version. -func markupSerializationOracleWant(name string) string { - switch strings.TrimSuffix(name, " fallback") { - case "HTML5 CDATA XHTML ignored", "HTML CDATA split and CR", "HTML XML island whitespace", "HTML4 XML island control", "HTML4 XML qualified attribute control", "HTML XML island line separator", "XML 1.0 CDATA control references", "XHTML5 CDATA markup immunity", "XHTML5 null namespace": - return nameFalse - case "HTML XML island PI", "HTML4 XML island PI": - return "SERE0015" - default: - return nameTrue - } -} - func TestCompareSaxonHTMLVersions(t *testing.T) { classpath, binary := saxonRuntime(t) e := engine(t, transform.Config{}) @@ -3733,20 +3405,6 @@ func TestCompareSaxonSerializationSequences(t *testing.T) { } } -func serializationSequenceOracle(name, want string) string { - // SER31 §2 gives selected text a document parent and coalesces text before - // §4 character expansion. Saxon 12.10 loses this detached CDATA text and - // normalizes the original text chunks separately; retain its exact output. - switch name { - case "HTML detached text ignores source CDATA parent": - return "" - case "XML text coalescence before NFC", "HTML text coalescence before NFC", "XHTML text coalescence before NFC", "XML document flattening precedes NFC": - return "e\u0301" - default: - return want - } -} - func TestCompareSaxonJSONCharacterExpansion(t *testing.T) { classpath, binary := saxonRuntime(t) e := engine(t, transform.Config{}) @@ -3810,8 +3468,10 @@ func comparePublishedJSONSchema(t *testing.T, e *transform.Engine) { if err != nil { t.Fatal(err) } - if digest := sha256.Sum256(data); hex.EncodeToString(digest[:]) != "3ba9d28cc77ac0ba876a7e3dd88308381c4e32334ea412b17961968a37ba6f9d" { - t.Fatal("published XSLT30 Appendix B.1 JSON schema checksum mismatch") + // XPath 3.1 uses F&O 3.1 Appendix C.2. XSLT 3.0 Appendix B.1 + // publishes an older schema with different attribute declarations. + if digest := sha256.Sum256(data); hex.EncodeToString(digest[:]) != "34407695050d165be14931c0f378f832d24abe3de682a069d2d6121f07661ec3" { + t.Fatal("published FO31 Appendix C.2 JSON schema checksum mismatch") } binary, err := exec.LookPath("xmllint") if err != nil { @@ -3822,8 +3482,10 @@ func comparePublishedJSONSchema(t *testing.T, e *transform.Engine) { validity := map[string]bool{ "document": true, "document comments": true, "null comments": true, "foreign attributes ignored": true, "array whitespace": true, "escaped string": true, + "foreign boolean attributes": true, "foreign number attributes": true, "array key": false, "array escaped key": false, "map escaped": false, "number escaped": false, "null text": false, "missing key": false, "invalid key boolean": false, + "unqualified extra attribute": false, "function namespace attribute": false, } for _, test := range xmlToJSONSchemaCases() { valid, selected := validity[test.name] @@ -4341,7 +4003,8 @@ func compareSelectedSerializationVector(t *testing.T, binary, classpath, edition test.want = strings.TrimPrefix(test.want, "\uFEFF") } check := markupSerializationCheck(test) - for role, value := range map[string]string{"native": actual, "independent": output} { + for _, result := range []struct{ role, value string }{{"native", actual}, {"independent", output}} { + role, value := result.role, result.value result, err := query(t, e, check, transform.XPath31).Evaluate(t.Context(), nil, transform.XPathContext{Variables: transform.Bindings{{Local: "s"}: transform.Values(transform.String(value))}}) if err != nil { t.Fatal(role, "control evaluation", err) @@ -4396,62 +4059,6 @@ func TestCompareSerializationURIAttributeTables(t *testing.T) { } } -func serializationURIOracle(t *testing.T, binary, classpath, source string) string { - t.Helper() - file := filepath.Join(t.TempDir(), "uri.xsl") - if err := os.WriteFile(file, []byte(source), 0o600); err != nil { - t.Fatal(err) - } - output, err := exec.CommandContext(t.Context(), binary, "-cp", classpath, "net.sf.saxon.Transform", "-xsl:"+file, "-it:start").Output() - if err != nil { - var failure *exec.ExitError - if errors.As(err, &failure) { - t.Fatalf("independent URI table: %s: %v", failure.Stderr, err) - } - t.Fatal(err) - } - return string(output) -} - -func compareSerializationURICell(t *testing.T, binary, classpath, edition, method string, cell serializationURICell) { - t.Helper() - e := engine(t, transform.Config{}) - vector := serializationURICellVector(edition, method, cell.element, cell.attribute) - constructor := stylesheet(t, e, markupSerializationSheet(markupSerializationCase{body: vector.body}, false)) - input, err := constructor.Execute(t.Context(), nil, transform.XSLTContext{Template: transform.Name{Local: nameStart}, ResultMode: transform.ResultRaw}) - if err != nil { - t.Fatal(err) - } - actual, err := selectedSerializationVector(t, edition, vector, input.Primary()) - if err != nil || !serializationURICellValid(actual, edition, cell.attribute, cell.legacy, true) { - t.Fatal("native URI table", actual, err) - } - source := markupSerializationSheet(vector, false) - if edition == "20" { - source = strings.Replace(source, `version="3.0"`, `version="2.0"`, 1) - } - output := serializationURIOracle(t, binary, classpath, source) - agreed := serializationURICellValid(output, edition, cell.attribute, cell.legacy, true) - if !agreed { - // Saxon12 retains the REC20 URI table for HTML5 and does not apply URI - // escaping in its XHTML5 emitter. These exact diagnostics retain the - // published REC30/31 expectation. They never constitute URI-cell agreement. - diagnostic := edition != "20" && (method == "xhtml" || !cell.legacy) - if !diagnostic || !serializationURICellValid(output, edition, cell.attribute, cell.legacy, false) { - t.Fatal("unreviewed independent URI diagnostic", output, "native", actual) - } - } - saxonProofAgreement(t, binary, classpath, agreed) - // Turning the parameter off is a distinct finite negative control. Both - // processors must preserve ordinary, qualified and URI-valued attributes. - source = strings.ReplaceAll(source, `escape-uri-attributes="yes"`, `escape-uri-attributes="no"`) - plain := serializationURIOracle(t, binary, classpath, source) - if !serializationURICellValid(plain, edition, cell.attribute, cell.legacy, false) { - t.Fatal("independent escaping=no", plain) - } - saxonProofAgreement(t, binary, classpath, true) -} - // XML 1.1 construction belongs to an external host. Saxon supplies its // normalized literal here; the selected XPath evaluator must consume that text // without silently applying another XML source normalization pass. @@ -4631,3 +4238,401 @@ func TestCompareSaxonNamedOutputCharacterMaps(t *testing.T) { } } } + +func reservedNameProofStylesheet(version, name string, prefixed bool) string { + function := `` + call := name + "(1,2)" + if prefixed { + call = "p:" + call + } + return sheet(version, function+``+valueOfExpression(call)+``) +} + +func compareSaxonCompiler(t *testing.T, binary, classpath, source, module, want string, processor transform.XSLTVersion, oracle ...string) { + t.Helper() + directory := t.TempDir() + main := filepath.Join(directory, "main.xsl") + for name, data := range map[string]string{"main.xsl": source, "module.xsl": module} { + if err := os.WriteFile(filepath.Join(directory, name), []byte(data), 0o600); err != nil { + t.Fatal(err) + } + } + config := transform.Config{Resolver: transform.ResolverFunc(func(ctx context.Context, uri string) (io.ReadCloser, error) { + if !strings.HasSuffix(uri, "/module.xsl") { + return nil, transform.ErrResource + } + return io.NopCloser(strings.NewReader(module)), ctx.Err() + })} + if processor == transform.XSLT20 { + // The pinned Saxon 9.4 comparison selects XSD 1.0 builtin datatypes. + config.XMLSchemaVersion = transform.XMLSchema10 + } + e := engine(t, config) + compiled, err := e.CompileXSLT(t.Context(), []byte(source), transform.XSLTOptions{ProcessorVersion: processor, BaseURI: (&url.URL{Scheme: "file", Path: filepath.ToSlash(main)}).String()}) + got := "" + if err == nil { + result, executeErr := compiled.Transform(t.Context(), nil, transform.XSLTContext{Template: transform.Name{Local: nameStart}}) + err = executeErr + if err == nil { + got = string(result.Primary) + } + } + if err != nil { + var detail *transform.Error + if !errors.As(err, &detail) { + t.Fatal(err) + } + got = detail.Code + } + if got != want { + t.Fatal("native", processor, got, err, want) + } + publishedProofVector(t, want) + nativeWant := want + if len(oracle) > 0 { + want = oracle[0] + } + ctx, cancel := context.WithTimeout(t.Context(), 20*time.Second) + defer cancel() + output, err := exec.CommandContext(ctx, binary, saxonCompilerArguments(classpath, main, processor)...).Output() + if diagnostic, separate := strings.CutPrefix(want, "diagnostic:"); separate { + var failure *exec.ExitError + if !errors.As(err, &failure) || !strings.Contains(string(failure.Stderr), diagnostic) { + t.Fatal("independent compiler diagnostic", output, err, want) + } + + saxonProofAgreement(t, binary, classpath, false) + t.Log("Independent engine diagnostic; excluded from exact agreements:", diagnostic) + + return + } + + got = string(output) + if err != nil { + got = saxonDiagnosticCode(err) + } + if got != want { + var detail *exec.ExitError + if errors.As(err, &detail) { + t.Fatalf("Saxon: %q, %s; want %q", output, detail.Stderr, want) + } + t.Fatalf("Saxon: %q, %v; want %q", output, err, want) + } + saxonProofAgreement(t, binary, classpath, nativeWant == want) +} + +func saxonCompilerArguments(classpath, main string, processor transform.XSLTVersion) []string { + args := []string{"-cp", classpath, "net.sf.saxon.Transform"} + if processor == transform.XSLT20 { + args = append(args, "-xsltversion:2.0") + } + return append(args, "-xsl:"+main, "-it:start") +} + +func compareSaxonExpression(t *testing.T, ctx context.Context, e *transform.Engine, binary, classpath, expression, want string, oracleWant ...string) { + t.Helper() + compareSaxonConfiguredExpression(t, ctx, e, binary, classpath, expression, want, nil, oracleWant...) +} + +func compareSaxonConfiguredExpression(t *testing.T, ctx context.Context, e *transform.Engine, binary, classpath, expression, want string, options []string, oracleWant ...string) { + t.Helper() + + program, err := e.CompileXPath(ctx, expression, transform.XPathOptions{Version: transform.XPath31}) + + var value transform.Sequence + if err == nil { + value, err = program.Evaluate(ctx, nil, transform.XPathContext{}) + } + + oracle := strings.ReplaceAll(expression, "&", "&") + + if comparisonErrorCode(want) { + var detail *transform.Error + if !errors.As(err, &detail) || detail.Code != want { + t.Fatal(value, err, want) + } + } else if err != nil || text(t, value) != want { + t.Fatal(value, err, want) + } + + publishedProofVector(t, want) + expected := want + if len(oracleWant) != 0 { + expected = oracleWant[0] + } + if expected != want { + saxonProofAgreement(t, binary, classpath, false) + t.Log("Independent diagnostic; excluded from exact agreements:", want, expected) + } + + nonstandardCode, nonstandard := strings.CutPrefix(expected, "error:") + if nonstandard { + expected = nonstandardCode + } + + if comparisonErrorCode(expected) || nonstandard { + oracle = "try { " + oracle + " } catch * { local-name-from-QName($err:code) }" + } + + output, err := saxonQuery(t, ctx, binary, classpath, oracle, options...) + if diagnostic, separate := strings.CutPrefix(expected, "diagnostic:"); separate { + var failure *exec.ExitError + if !errors.As(err, &failure) || !strings.Contains(string(failure.Stderr), diagnostic) { + t.Fatalf("independent diagnostic: %q, %v; want %q", output, err, diagnostic) + } + return + } + if target, crash := strings.CutPrefix(expected, "crash:"); crash { + var failure *exec.ExitError + if !errors.As(err, &failure) { + t.Fatalf("expected pinned Saxon crash diagnostic: %q, %v", output, err) + } + + diagnostic := string(failure.Stderr) + if !strings.Contains(diagnostic, "java.lang.ClassCastException") || !strings.Contains(diagnostic, target) { + t.Fatal("unexpected independent engine failure", diagnostic) + } + + return + } + + if err != nil && comparisonErrorCode(expected) { + if code := saxonDiagnosticCode(err); code != "" { + output, err = []byte(code), nil + } + } + + if err != nil || string(output) != expected { + var failure *exec.ExitError + if errors.As(err, &failure) { + t.Log(string(failure.Stderr)) + } + + t.Fatalf("Saxon %s: %q, %v; want %q", expression, output, err, expected) + } + if expected == want { + saxonProofAgreement(t, binary, classpath, true) + } +} + +func saxonProofOracle(t *testing.T, binary, classpath string) string { + t.Helper() + key := binary + "\x00" + classpath + if found, ok := saxonProofVersions.Load(key); ok { + return found.(string) + } + ctx, cancel := context.WithTimeout(t.Context(), 20*time.Second) + defer cancel() + output, err := exec.CommandContext(ctx, binary, "-cp", classpath, "net.sf.saxon.Version").CombinedOutput() + if err != nil { + t.Fatal("independent processor identity", string(output), err) + } + oracle := "" + switch { + case strings.Contains(string(output), "SAXON-J-HE 12.10"): + oracle = "saxon-12.10" + case strings.Contains(string(output), "SAXON 9.4.0.9"): + oracle = "saxon-9.4" + default: + t.Fatal("unreviewed independent processor version", string(output)) + } + saxonProofVersions.Store(key, oracle) + return oracle +} + +func saxonProofAgreement(t *testing.T, binary, classpath string, agreement bool) { + t.Helper() + marker := "P3_ORACLE " + if !agreement { + marker = "P3_DIAGNOSTIC " + } + t.Log(marker + saxonProofOracle(t, binary, classpath)) +} + +func saxonRuntime(t *testing.T) (string, string) { + t.Helper() + + classpath := os.Getenv("SAXON_CLASSPATH") + if classpath == "" { + t.Skip("SAXON_CLASSPATH is not configured") + } + + binary, err := exec.LookPath("java") + if err != nil { + t.Fatal("configured Saxon requires java", err) + } + + return classpath, binary +} + +func saxonQuery(t *testing.T, ctx context.Context, binary, classpath, source string, options ...string) ([]byte, error) { + t.Helper() + // A query file avoids the JVM command-line charset, which can replace + // Unicode characters under the Docker image's C locale. + queryPath := filepath.Join(t.TempDir(), "query.xq") + if err := os.WriteFile(queryPath, []byte(source), 0o600); err != nil { + t.Fatal(err) + } + + arguments := make([]string, 0, 5+len(options)) + arguments = append(arguments, "-cp", classpath, "net.sf.saxon.Query", "-q:"+queryPath, "!method=text") + arguments = append(arguments, options...) + + return exec.CommandContext(ctx, binary, arguments...).Output() +} + +func compareSaxonStylesheet(t *testing.T, ctx context.Context, e *transform.Engine, binary, classpath, body, want string, oracleWant ...string) { + t.Helper() + + source := sheet(version30, body) + expression := `let $s := '` + strings.ReplaceAll(source, "'", "''") + `' return transform(map{'stylesheet-text':$s,'initial-template':QName('', 'start'),'delivery-format':'serialized'})?output` + + result, err := query(t, e, expression, transform.XPath31).Evaluate(ctx, nil, transform.XPathContext{}) + got := "" + + if err != nil { + var detail *transform.Error + if !errors.As(err, &detail) { + t.Fatal(err) + } + + got = detail.Code + } else { + got = text(t, result) + } + + if got != want { + t.Fatal(body, got, want) + } + + publishedProofVector(t, want) + + // The oracle runs XQuery, whose string literals decode entity references. + output, err := saxonQuery(t, ctx, binary, classpath, strings.ReplaceAll(expression, "&", "&")) + + expected := want + if len(oracleWant) != 0 { + expected = oracleWant[0] + } + + got = string(output) + if err != nil { + got = saxonDiagnosticCode(err) + } + + if got != expected { + var diagnostic *exec.ExitError + if errors.As(err, &diagnostic) { + t.Fatalf("Saxon %s: %q, %s; want %q", body, output, diagnostic.Stderr, expected) + } + + t.Fatalf("Saxon %s: %q, %v; want %q", body, output, err, expected) + } + saxonProofAgreement(t, binary, classpath, expected == want) +} + +// Static type errors may be diagnosed before XQuery try/catch can run. +func saxonDiagnosticCode(err error) string { + var diagnostic *exec.ExitError + if !errors.As(err, &diagnostic) { + return "" + } + + for _, line := range strings.Split(string(diagnostic.Stderr), "\n") { + fields := strings.Fields(line) + if len(fields) > 0 { + code := strings.TrimSuffix(fields[0], ":") + if comparisonErrorCode(code) && !strings.HasPrefix(code, "SXWN") { + return code + } + } + } + + return "" +} + +// Serialization 3.1 sections 7.1/7.4.4 apply XML rules to islands and ignore +// CDATA on HTML5 XHTML elements. Saxon retains separate namespace/CR/DEL/PI +// behavior in these bounded diagnostics; they are not exact agreements. +// XML 1.0 CDATA diagnostics also retain Saxon's literal C1/NEL/LS output, +// while SER31 section 5 requires references even in that output version. +func markupSerializationOracleWant(name string) string { + switch strings.TrimSuffix(name, " fallback") { + case "HTML5 CDATA XHTML ignored", "HTML CDATA split and CR", "HTML XML island whitespace", "HTML4 XML island control", "HTML4 XML qualified attribute control", "HTML XML island line separator", "XML 1.0 CDATA control references", "XHTML5 CDATA markup immunity", "XHTML5 null namespace": + return nameFalse + case "HTML XML island PI", "HTML4 XML island PI": + return "SERE0015" + default: + return nameTrue + } +} + +func serializationSequenceOracle(name, want string) string { + // SER31 §2 gives selected text a document parent and coalesces text before + // §4 character expansion. Saxon 12.10 loses this detached CDATA text and + // normalizes the original text chunks separately; retain its exact output. + switch name { + case "HTML detached text ignores source CDATA parent": + return "" + case "XML text coalescence before NFC", "HTML text coalescence before NFC", "XHTML text coalescence before NFC", "XML document flattening precedes NFC": + return "e\u0301" + default: + return want + } +} + +func serializationURIOracle(t *testing.T, binary, classpath, source string) string { + t.Helper() + file := filepath.Join(t.TempDir(), "uri.xsl") + if err := os.WriteFile(file, []byte(source), 0o600); err != nil { + t.Fatal(err) + } + output, err := exec.CommandContext(t.Context(), binary, "-cp", classpath, "net.sf.saxon.Transform", "-xsl:"+file, "-it:start").Output() + if err != nil { + var failure *exec.ExitError + if errors.As(err, &failure) { + t.Fatalf("independent URI table: %s: %v", failure.Stderr, err) + } + t.Fatal(err) + } + return string(output) +} + +func compareSerializationURICell(t *testing.T, binary, classpath, edition, method string, cell serializationURICell) { + t.Helper() + e := engine(t, transform.Config{}) + vector := serializationURICellVector(edition, method, cell.element, cell.attribute) + constructor := stylesheet(t, e, markupSerializationSheet(markupSerializationCase{body: vector.body}, false)) + input, err := constructor.Execute(t.Context(), nil, transform.XSLTContext{Template: transform.Name{Local: nameStart}, ResultMode: transform.ResultRaw}) + if err != nil { + t.Fatal(err) + } + actual, err := selectedSerializationVector(t, edition, vector, input.Primary()) + if err != nil || !serializationURICellValid(actual, edition, cell.attribute, cell.legacy, true) { + t.Fatal("native URI table", actual, err) + } + source := markupSerializationSheet(vector, false) + if edition == "20" { + source = strings.Replace(source, `version="3.0"`, `version="2.0"`, 1) + } + output := serializationURIOracle(t, binary, classpath, source) + agreed := serializationURICellValid(output, edition, cell.attribute, cell.legacy, true) + if !agreed { + // Saxon12 retains the REC20 URI table for HTML5 and does not apply URI + // escaping in its XHTML5 emitter. These exact diagnostics retain the + // published REC30/31 expectation. They never constitute URI-cell agreement. + diagnostic := edition != "20" && (method == "xhtml" || !cell.legacy) + if !diagnostic || !serializationURICellValid(output, edition, cell.attribute, cell.legacy, false) { + t.Fatal("unreviewed independent URI diagnostic", output, "native", actual) + } + } + saxonProofAgreement(t, binary, classpath, agreed) + // Turning the parameter off is a distinct finite negative control. Both + // processors must preserve ordinary, qualified and URI-valued attributes. + source = strings.ReplaceAll(source, `escape-uri-attributes="yes"`, `escape-uri-attributes="no"`) + plain := serializationURIOracle(t, binary, classpath, source) + if !serializationURICellValid(plain, edition, cell.attribute, cell.legacy, false) { + t.Fatal("independent escaping=no", plain) + } + saxonProofAgreement(t, binary, classpath, true) +} diff --git a/transform/context.go b/transform/context.go index 25392bb..7bbc95f 100644 --- a/transform/context.go +++ b/transform/context.go @@ -7,12 +7,47 @@ import ( "time" "unicode/utf8" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/xmlchar" "github.com/skarm/xmlsec/transform/internal/xdm" lexicalpkg "github.com/skarm/xmlsec/transform/internal/xdm/lexical" "github.com/skarm/xmlsec/transform/internal/xpath" ) +// Go reports zone offsets in seconds; XML Schema implicit timezones use whole minutes with magnitude at most fourteen hours. +const ( + maximumImplicitTimezoneSeconds = 14 * 60 * 60 +) + +// DynamicContext supplies shared XPath and XSLT execution settings. +// The current time and implicit timezone are fixed for each invocation. +type DynamicContext struct { + // CurrentTime is shared by all current-date/time calls in one invocation. + // Zero samples the clock once at invocation start, in UTC. + CurrentTime time.Time + // ImplicitTimezone defaults to CurrentTime.Location(). Its offset at the + // invocation time must be a whole minute within -14 to +14 hours. + ImplicitTimezone *time.Location + // DefaultLanguage is an xs:language value; XML whitespace is collapsed. + // Empty selects the default language, en. + DefaultLanguage string + // TraceWriter receives XPath trace and XSLT trace output. Nil discards it. + // The caller owns the writer and synchronizes concurrent invocations. + TraceWriter io.Writer +} + +// CollectionResolver supplies immutable collections and their member URIs. +// Callbacks must honor context and support concurrent use. Empty URI identifies +// the default collection; no implicit collection is provided by the engine. +type CollectionResolver interface { + // ResolveCollection returns the members of collection(uri). + // An empty URI requests the default collection. + ResolveCollection(context.Context, string) (Sequence, error) + // ResolveURICollection returns the member URIs of uri-collection(uri). + // An empty URI requests the default URI collection. + ResolveURICollection(context.Context, string) ([]string, error) +} + func (e *Engine) xpathDecimalFormats(ctx context.Context, evaluator xpath.Evaluator, formats map[Name]map[string]string, version XPathVersion) (xpath.Evaluator, error) { if len(formats) == 0 { return evaluator, nil @@ -91,7 +126,7 @@ func (e *Engine) dynamicContext(ctx context.Context, options DynamicContext) (co } _, offset := now.In(zone).Zone() - if offset%60 != 0 || offset < -14*60*60 || offset > 14*60*60 { + if offset%60 != 0 || offset < -maximumImplicitTimezoneSeconds || offset > maximumImplicitTimezoneSeconds { return nil, ErrInput } @@ -112,15 +147,15 @@ func (e *Engine) collectionResolver() xpath.CollectionResolver { func xpathDecimalFormat(properties map[string]string) (xpath.DecimalFormat, error) { format := xpath.DefaultDecimalFormat() characters := map[string]*rune{ - "decimal-separator": &format.DecimalSeparator, - "grouping-separator": &format.GroupingSeparator, - "minus-sign": &format.MinusSign, - "percent": &format.Percent, - "per-mille": &format.PerMille, - "zero-digit": &format.ZeroDigit, - "digit": &format.Digit, - "pattern-separator": &format.PatternSeparator, - "exponent-separator": &format.ExponentSeparator, + lexicon.DecimalFormatDecimalSeparator: &format.DecimalSeparator, + lexicon.DecimalFormatGroupingSeparator: &format.GroupingSeparator, + lexicon.DecimalFormatMinusSign: &format.MinusSign, + lexicon.DecimalFormatPercent: &format.Percent, + lexicon.DecimalFormatPerMille: &format.PerMille, + lexicon.DecimalFormatZeroDigit: &format.ZeroDigit, + lexicon.DecimalFormatDigit: &format.Digit, + lexicon.DecimalFormatPatternSeparator: &format.PatternSeparator, + lexicon.DecimalFormatExponentSeparator: &format.ExponentSeparator, } for property, v := range properties { @@ -136,9 +171,9 @@ func xpathDecimalFormat(properties map[string]string) (xpath.DecimalFormat, erro *character, _ = utf8.DecodeRuneInString(v) } else { switch property { - case "infinity": + case lexicon.DecimalFormatInfinity: format.Infinity = v - case "NaN": + case lexicon.DecimalFormatNaN: format.NaN = v default: return format, ErrInput @@ -166,35 +201,6 @@ func xpathDecimalFormat(properties map[string]string) (xpath.DecimalFormat, erro return format, nil } -// DynamicContext supplies shared XPath and XSLT execution settings. -// The current time and implicit timezone are fixed for each invocation. -type DynamicContext struct { - // CurrentTime is shared by all current-date/time calls in one invocation. - // Zero samples the clock once at invocation start, in UTC. - CurrentTime time.Time - // ImplicitTimezone defaults to CurrentTime.Location(). Its offset at the - // invocation time must be a whole minute within -14 to +14 hours. - ImplicitTimezone *time.Location - // DefaultLanguage is an xs:language value; XML whitespace is collapsed. - // Empty selects the default language, en. - DefaultLanguage string - // TraceWriter receives XPath trace and XSLT trace output. Nil discards it. - // The caller owns the writer and synchronizes concurrent invocations. - TraceWriter io.Writer -} - -// CollectionResolver supplies immutable collections and their member URIs. -// Callbacks must honor context and support concurrent use. Empty URI identifies -// the default collection; no implicit collection is provided by the engine. -type CollectionResolver interface { - // ResolveCollection returns the members of collection(uri). - // An empty URI requests the default collection. - ResolveCollection(context.Context, string) (Sequence, error) - // ResolveURICollection returns the member URIs of uri-collection(uri). - // An empty URI requests the default URI collection. - ResolveURICollection(context.Context, string) ([]string, error) -} - type checkedTrace struct { ctx context.Context writer io.Writer diff --git a/transform/doc.go b/transform/doc.go index a4f4151..2c643bc 100644 --- a/transform/doc.go +++ b/transform/doc.go @@ -30,6 +30,8 @@ // DTD processing. External DTDs and entities require the configured resolver // and invocation resource policy. This mode supports entity expansion, default // attributes and declared attribute normalization without DTD or XSD validation. +// Engine XML input preserves relative namespace names, unlike the default +// document parser and C14N. Import and Export do not rewrite those names. // // # Results, resources and limits // @@ -41,7 +43,8 @@ // [Stylesheet.Write] return byte counts and allow partial output on error. // Ordinary execution retains the complete result; serialization writes common // formats incrementally and uses intermediate storage for specialized options. -// [Engine.ParseReader] and [Engine.CompileXSLTReader] parse incrementally. For +// [Engine.ParseReader] parses incrementally; stylesheet and language-resource +// parsing can also retain bounded decoded and DTD-expanded input buffers. For // independent records, [Stylesheet.WriteRecords] retains one record and its // result alongside parser state, stylesheet, parameters and invocation resources; // it does not retain the complete enclosing document or collect all output. diff --git a/transform/document.go b/transform/document.go index 3b6f51a..5fc697c 100644 --- a/transform/document.go +++ b/transform/document.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/nilcheck" "github.com/skarm/xmlsec/internal/tree" "github.com/skarm/xmlsec/transform/internal/xdm" @@ -88,15 +89,15 @@ func (n Node) Kind() string { case xdm.DocumentNode: return "document" case xdm.ElementNode: - return "element" + return lexicon.KindElement case xdm.AttributeNode: - return "attribute" + return lexicon.KindAttribute case xdm.TextNode, xdm.CDATASectionNode: - return "text" + return lexicon.KindText case xdm.CommentNode: - return "comment" + return lexicon.KindComment case xdm.ProcessingInstructionNode: - return "processing-instruction" + return lexicon.KindPI case xdm.NamespaceNode: return "namespace" default: @@ -107,6 +108,7 @@ func (n Node) Kind() string { type inputLimitsKey struct{} // Parse validates XML, rejects DTDs and applies the engine's input limits. +// Relative namespace names are preserved without base-URI resolution. func (e *Engine) Parse(ctx context.Context, xml []byte) (*Document, error) { ctx, cancel, err := e.invocation(ctx) if err != nil { @@ -161,12 +163,7 @@ func (e *Engine) Import(ctx context.Context, d *document.Document) (*Document, e return nil, wrap("import", ErrInput) } - limits := document.DefaultLimits() - limits.MaxBytes = e.limits.MaxInputBytes - limits.MaxDepth = e.limits.MaxDepth - limits.MaxNodes = e.limits.MaxNodes - - if err = tree.CheckSnapshotLimits(ctx, d, limits); err != nil { + if err = tree.CheckSnapshotLimits(ctx, d, e.limits.xmlLimits()); err != nil { return nil, wrap("import", err) } @@ -262,15 +259,16 @@ func (e *Engine) parse(ctx context.Context, xml []byte, baseURI string) (*Docume func checkInputDocument(ctx context.Context, native *xdm.Document, limits Limits) error { native.Freeze() - policy := document.DefaultLimits() - policy.MaxBytes = limits.MaxInputBytes - policy.MaxNodes = limits.MaxNodes - policy.MaxDepth = limits.MaxDepth + policy := limits.xmlLimits() snapshot := native.Snapshot() key := inputLimitsKey{} if value, ok := tree.LoadView(snapshot, key); ok { - if checked, ok := value.(document.Limits); ok && policy.MaxBytes >= checked.MaxBytes && policy.MaxNodes >= checked.MaxNodes && policy.MaxDepth >= checked.MaxDepth { + if checked, ok := value.(document.Limits); ok && + policy.MaxBytes >= checked.MaxBytes && policy.MaxNodes >= checked.MaxNodes && + policy.MaxDepth >= checked.MaxDepth && + policy.MaxAttributesPerElement >= checked.MaxAttributesPerElement && + policy.MaxInScopeNamespaces >= checked.MaxInScopeNamespaces { return ctx.Err() } } diff --git a/transform/document_internal_test.go b/transform/document_internal_test.go index bf3dd13..7eee71a 100644 --- a/transform/document_internal_test.go +++ b/transform/document_internal_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "errors" + "io" "strings" "sync" "testing" @@ -123,17 +124,29 @@ func TestParseUsesProjectParser(t *testing.T) { d, parseErr := document.Parse(t.Context(), []byte(xml), document.DefaultLimits()) view, viewErr := e.Parse(t.Context(), []byte(xml)) - if (parseErr == nil) != (viewErr == nil) { - t.Fatalf("different XML parsing policy for %s: %v / %v", xml, parseErr, viewErr) + if parseErr != nil || viewErr != nil { + if (parseErr == nil) != (viewErr == nil) { + t.Fatalf("different XML parsing policy for %s: %v / %v", xml, parseErr, viewErr) + } + + continue + } + if view == nil { + t.Fatal("Parse returned no document without an error") } - if parseErr == nil { - want, _ := d.XML(t.Context()) + want, err := d.XML(t.Context()) + if err != nil { + t.Fatal(err) + } - got, _ := view.native.Snapshot().XML(t.Context()) - if !bytes.Equal(got, want) { - t.Fatalf("different parser trees: %s / %s", got, want) - } + got, err := view.native.Snapshot().XML(t.Context()) + if err != nil { + t.Fatal(err) + } + + if !bytes.Equal(got, want) { + t.Fatalf("different parser trees: %s / %s", got, want) } } } @@ -150,6 +163,9 @@ func TestInputPolicyCacheRemainsBoundedAndChecksStricterLimits(t *testing.T) { if err != nil { t.Fatal(err) } + if d == nil { + t.Fatal("Parse returned no document without an error") + } limits := DefaultLimits() @@ -231,3 +247,233 @@ func TestInvocationContextLifetime(t *testing.T) { t.Fatal("caller cancellation/cause lost", ctx.Err(), context.Cause(ctx)) } } + +func TestXMLScopeLimitDefaultsAndValidation(t *testing.T) { + defaults := DefaultLimits() + if defaults.MaxAttributesPerElement != 128 || defaults.MaxInScopeNamespaces != 256 { + t.Fatal("XML defaults changed", defaults) + } + engine, err := NewEngine(Config{Limits: Limits{MaxInputBytes: 1024}}) + if err != nil { + t.Fatal(err) + } + if _, err := engine.Parse(t.Context(), []byte(``)); err != nil { + t.Fatal("old partial limits did not select XML defaults", err) + } + for _, limits := range []Limits{{MaxAttributesPerElement: -1}, {MaxInScopeNamespaces: -1}} { + if engine, err := NewEngine(Config{Limits: limits}); engine != nil || !errors.Is(err, ErrInput) { + t.Fatal("negative XML scope accepted", engine, err) + } + } +} + +func TestXMLScopeLimitsAcrossSnapshotsAndPolicyCache(t *testing.T) { + for _, tc := range []struct { + name, xml string + limits Limits + reject bool + }{ + {"attributes below", ``, Limits{MaxAttributesPerElement: 1}, true}, + {"attributes exact", ``, Limits{MaxAttributesPerElement: 2}, false}, + {"attributes raised", ``, Limits{MaxAttributesPerElement: 3}, false}, + {"namespaces below", ``, Limits{MaxInScopeNamespaces: 2}, true}, + {"namespaces exact", ``, Limits{MaxInScopeNamespaces: 3}, false}, + {"namespaces raised", ``, Limits{MaxInScopeNamespaces: 4}, false}, + } { + t.Run(tc.name, func(t *testing.T) { + ctx := t.Context() + consumer, err := NewEngine(Config{Limits: tc.limits}) + if err != nil { + t.Fatal(err) + } + snapshot, err := document.Parse(ctx, []byte(tc.xml), document.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + for _, route := range []struct { + name string + read func() (*Document, error) + }{ + {"bytes", func() (*Document, error) { return consumer.Parse(ctx, []byte(tc.xml)) }}, + {"reader", func() (*Document, error) { return consumer.ParseReader(ctx, strings.NewReader(tc.xml)) }}, + {"import", func() (*Document, error) { return consumer.Import(ctx, snapshot) }}, + } { + t.Run(route.name, func(t *testing.T) { + actual, err := route.read() + if tc.reject { + if actual != nil || !errors.Is(err, ErrLimit) || !errors.Is(err, document.ErrLimit) { + t.Fatal("XML limit lost", actual, err) + } + } else if err != nil || actual == nil { + t.Fatal(actual, err) + } else if route.name == "import" && actual.native.Snapshot() != snapshot { + t.Fatal("import copied the snapshot") + } + }) + } + producer, err := NewEngine(Config{}) + if err != nil { + t.Fatal(err) + } + source, err := producer.Parse(ctx, []byte(tc.xml)) + if err != nil { + t.Fatal(err) + } + warm, err := producer.CompileXPath(ctx, `count(/*)`, XPathOptions{}) + if err != nil { + t.Fatal(err) + } + if result, err := warm.Evaluate(ctx, source, XPathContext{}); err != nil || result.Len() != 1 { + t.Fatal("cache warmup", result, err) + } + cached, ok := tree.LoadView(source.native.Snapshot(), inputLimitsKey{}) + if !ok { + t.Fatal("policy not cached") + } + query, err := consumer.CompileXPath(ctx, `count(/*)`, XPathOptions{}) + if err != nil { + t.Fatal(err) + } + result, err := query.Evaluate(ctx, source, XPathContext{}) + if tc.reject { + if result.Len() != 0 || !errors.Is(err, ErrLimit) || !errors.Is(err, document.ErrLimit) { + t.Fatal("looser cached policy bypassed consumer", result, err) + } + after, ok := tree.LoadView(source.native.Snapshot(), inputLimitsKey{}) + if !ok || after != cached { + t.Fatal("failed policy replaced successful cache", after, cached) + } + } else if err != nil { + t.Fatal(err) + } else if value, err := result.Text(); err != nil || value != "1" { + t.Fatal(value, err) + } + exported, exportErr := consumer.Export(ctx, source) + if tc.reject { + if exported != nil || !errors.Is(exportErr, ErrLimit) || !errors.Is(exportErr, document.ErrLimit) { + t.Fatal("export lost consumer policy", exported, exportErr) + } + } else if exportErr != nil || exported != source.native.Snapshot() { + t.Fatal("export changed identity", exportErr) + } + bound, err := consumer.CompileXPath(ctx, `$node`, XPathOptions{}) + if err != nil { + t.Fatal(err) + } + sequence, err := bound.Evaluate(ctx, nil, XPathContext{Variables: Bindings{{Local: "node"}: Values(source.Root())}}) + if tc.reject { + if sequence.Len() != 0 || !errors.Is(err, ErrLimit) { + t.Fatal("node binding bypassed consumer policy", sequence, err) + } + } else if err != nil || sequence.Len() != 1 { + t.Fatal(sequence, err) + } + }) + } +} + +func TestXMLScopeLimitsInLanguageParsers(t *testing.T) { + for _, tc := range []struct { + name, xml string + limits Limits + reject bool + }{ + {"resource attributes below", ``, Limits{MaxAttributesPerElement: 2}, true}, + {"resource attributes exact", ``, Limits{MaxAttributesPerElement: 2}, false}, + {"resource namespaces below", ``, Limits{MaxInScopeNamespaces: 2}, true}, + {"resource namespaces exact", ``, Limits{MaxInScopeNamespaces: 2}, false}, + } { + for _, expression := range []string{`count(doc('urn:scope-limits')/*)`, `count(parse-xml($xml)/*)`} { + t.Run(tc.name+"/"+expression, func(t *testing.T) { + engine, err := NewEngine(Config{Limits: tc.limits, Resolver: ResolverFunc(func(ctx context.Context, uri string) (io.ReadCloser, error) { + if err := ctx.Err(); err != nil { + return nil, err + } + if uri != "urn:scope-limits" { + return nil, ErrResource + } + return io.NopCloser(strings.NewReader(tc.xml)), nil + })}) + if err != nil { + t.Fatal(err) + } + query, err := engine.CompileXPath(t.Context(), expression, XPathOptions{}) + if err != nil { + t.Fatal(err) + } + result, err := query.Evaluate(t.Context(), nil, XPathContext{Variables: Bindings{{Local: "xml"}: Values(String(tc.xml))}}) + if tc.reject { + if result.Len() != 0 || !errors.Is(err, ErrLimit) { + t.Fatal("language parser lost XML limits", result, err) + } + } else if err != nil { + t.Fatal(err) + } else if value, err := result.Text(); err != nil || value != "1" { + t.Fatal(value, err) + } + }) + } + } + const stylesheet = `` + for _, tc := range []struct { + name string + limits Limits + reject bool + }{ + {"stylesheet attributes below", Limits{MaxAttributesPerElement: 1}, true}, + {"stylesheet attributes exact", Limits{MaxAttributesPerElement: 2}, false}, + {"stylesheet attributes raised", Limits{MaxAttributesPerElement: 3}, false}, + {"stylesheet namespaces below", Limits{MaxInScopeNamespaces: 1}, true}, + {"stylesheet namespaces exact", Limits{MaxInScopeNamespaces: 2}, false}, + {"stylesheet namespaces raised", Limits{MaxInScopeNamespaces: 3}, false}, + } { + t.Run(tc.name, func(t *testing.T) { + engine, err := NewEngine(Config{Limits: tc.limits}) + if err != nil { + t.Fatal(err) + } + sheet, err := engine.CompileXSLT(t.Context(), []byte(stylesheet), XSLTOptions{}) + if tc.reject { + if sheet != nil || !errors.Is(err, ErrLimit) || !errors.Is(err, document.ErrLimit) { + t.Fatal("stylesheet parser lost limits", sheet, err) + } + } else if err != nil || sheet == nil { + t.Fatal(sheet, err) + } + }) + } +} + +func TestApprovedCompilationDeadlineAndCancellation(t *testing.T) { + for _, tc := range []struct { + name string + timeout time.Duration + want time.Duration + }{ + {"runtime timeout below setup floor", time.Millisecond, 3 * time.Second}, + {"configured setup timeout", 5 * time.Second, 5 * time.Second}, + } { + t.Run(tc.name, func(t *testing.T) { + e, err := NewEngine(Config{Limits: Limits{Timeout: tc.timeout}}) + if err != nil { + t.Fatal(err) + } + + before := time.Now() + ctx, cancel := e.approvedCompilationContext() + defer cancel() + after := time.Now() + deadline, ok := ctx.Deadline() + if !ok || deadline.Before(before.Add(tc.want)) || deadline.After(after.Add(tc.want)) { + t.Fatalf("setup deadline=%v (present=%v), want duration %v", deadline, ok, tc.want) + } + + cancel() + select { + case <-ctx.Done(): + default: + t.Fatal("setup context remains active after cancellation") + } + }) + } +} diff --git a/transform/document_test.go b/transform/document_test.go index e596a18..379821c 100644 --- a/transform/document_test.go +++ b/transform/document_test.go @@ -77,6 +77,15 @@ func TestParseXMLCharacterInput(t *testing.T) { e := engine(t, transform.Config{}) for _, version := range []transform.XPathVersion{transform.XPath30, transform.XPath31} { + // XML 1.0 Fifth Edition [26] accepts 1.x declarations when the + // document uses only XML 1.0 syntax; encoding does not decode a string. + for _, declaration := range []string{"1.1", "1.7"} { + source := `text` + value, err := query(t, e, `string(parse-xml($source)/r)`, version).Evaluate(t.Context(), nil, transform.XPathContext{Variables: transform.Bindings{{Local: sourceName}: transform.Values(transform.String(source))}}) + if err != nil || text(t, value) != "text" { + t.Fatal("XML 1.x declaration compatibility", declaration, value, err) + } + } for _, encoding := range []string{xmlLatinEncoding, "UTF-16", xmlASCIIEncoding, "x-unregistered-encoding"} { source := `café 中` @@ -90,7 +99,8 @@ func TestParseXMLCharacterInput(t *testing.T) { ``, ``, ``, - ``, + ``, + ``, ``, } { _, err := query(t, e, `parse-xml($source)`, version).Evaluate(t.Context(), nil, transform.XPathContext{Variables: transform.Bindings{{Local: sourceName}: transform.Values(transform.String(source))}}) diff --git a/transform/engine.go b/transform/engine.go index 1205412..a4e5ad5 100644 --- a/transform/engine.go +++ b/transform/engine.go @@ -17,6 +17,20 @@ import ( "github.com/skarm/xmlsec/transform/internal/xpath" ) +// Zero engine limit fields select these per-invocation defaults; maximumDepth is a validation ceiling. Resource loads, aggregate bytes and intermediate work have independent budgets. +const ( + defaultInputBytes = 8 << 20 + defaultResourceBytes = 32 << 20 + defaultResourceLoads = 128 + defaultProgramBytes = 64 << 10 + defaultOutputBytes = 8 << 20 + defaultNodes = 100_000 + defaultOperations = 2_000_000 + defaultDepth = 128 + defaultTimeout = 3 * time.Second + maximumDepth = 512 +) + var ( // ErrInput reports invalid API input or an invalid zero-value handle. ErrInput = errors.New("transform: invalid input") @@ -55,12 +69,29 @@ type Limits struct { MaxDepth int // Timeout bounds each compilation or invocation; the default is three seconds. // Zero selects that default and does not disable the deadline. + // Trusted AllowedPrograms compilation uses at least three seconds per program; + // the configured timeout still applies to each runtime invocation. Timeout time.Duration + // MaxAttributesPerElement bounds attributes and namespace declarations per + // element when parsing XML or checking an existing snapshot; the default is 128. + MaxAttributesPerElement int + // MaxInScopeNamespaces bounds namespace bindings visible on each XML + // element, including the implicit xml binding; the default is 256. + MaxInScopeNamespaces int } // DefaultLimits returns an independent set of bounded defaults. func DefaultLimits() Limits { - return Limits{MaxInputBytes: 8 << 20, MaxResourceBytes: 32 << 20, MaxResources: 128, MaxProgramBytes: 64 << 10, MaxOutputBytes: 8 << 20, MaxNodes: 100_000, MaxOperations: 2_000_000, MaxDepth: 128, Timeout: 3 * time.Second} + xmlLimits := document.DefaultLimits() + + return Limits{ + MaxInputBytes: defaultInputBytes, MaxResourceBytes: defaultResourceBytes, + MaxResources: defaultResourceLoads, MaxProgramBytes: defaultProgramBytes, + MaxOutputBytes: defaultOutputBytes, MaxNodes: defaultNodes, + MaxOperations: defaultOperations, MaxDepth: defaultDepth, Timeout: defaultTimeout, + MaxAttributesPerElement: xmlLimits.MaxAttributesPerElement, + MaxInScopeNamespaces: xmlLimits.MaxInScopeNamespaces, + } } // Config supplies immutable engine policy and explicit resource adapters. @@ -99,6 +130,8 @@ type Config struct { Environment map[string]string // AllowedPrograms authorizes exact XMLDSig 1.0 transform programs. This application // allowlist is independent of Functions registration. + // NewEngine compiles each program with a timeout of max(Limits.Timeout, 3s), + // sharing its compilation budget across all expressions in that program. AllowedPrograms []reference.Transform } @@ -114,18 +147,50 @@ type ResolverFunc = resource.ResolverFunc // Engine retains immutable policy for compilation and execution. Construct it with // NewEngine; its zero value is invalid. type Engine struct { - packages PackageResolver - collections CollectionResolver - functions map[xpath.QualifiedName]xpath.Function - functionNames map[string]xpath.Function - limits Limits - resolver Resolver - environment map[string]string - v1 *v1Processor - xmlSchemaVersion XMLSchemaVersion - xsltAssertions bool + // packages is the application adapter for explicitly enabled stylesheet + // packages. + packages PackageResolver + // collections is the application adapter for XPath collections. + collections CollectionResolver + // functions indexes snapshotted callbacks by expanded function name. + functions map[xpath.QualifiedName]xpath.Function + // functionNames indexes callbacks by their expanded-name string representation. + functionNames map[string]xpath.Function + // limits retains normalized compilation and invocation budgets. + limits Limits + // resolver is the explicit external-resource adapter; nil grants no resource + // access. + resolver Resolver + // environment is the snapshotted environment exposed to transformations. + environment map[string]string + // v1 retains the compatibility processor for version 1.0 semantics. + v1 *v1Processor + // xmlSchemaVersion selects builtin datatype and regular-expression rules. + xmlSchemaVersion XMLSchemaVersion + // xsltAssertions enables xsl:assert for direct invocations. + xsltAssertions bool + // xsltDynamicEvaluationDisabled statically disables xsl:evaluate, including + // nested transforms. xsltDynamicEvaluationDisabled bool - allowSingleByteEncodings bool + // allowSingleByteEncodings permits the configured legacy XML input encodings. + allowSingleByteEncodings bool +} + +// Error identifies a failed operation and retains its XPath, XSLT or application +// error code when available. errors.Is preserves underlying cancellation and +// resource-limit errors. +type Error struct { + // Operation identifies the library operation that failed. + Operation string + // Code is the local part of the XPath, XSLT or application error code, + // or empty when none is available. CodeName also preserves its namespace. + Code string + // name retains the expanded XPath or XSLT error code. + name Name + // cause retains the wrapped execution or policy error. + cause error + // messages retains xsl:message diagnostics produced before failure. + messages []Message } // NewEngine validates policy and returns a reusable engine. Config{} is valid. @@ -177,6 +242,14 @@ func NewEngine(config Config) (*Engine, error) { value: &limits.MaxDepth, fallback: d.MaxDepth, }, + { + value: &limits.MaxAttributesPerElement, + fallback: d.MaxAttributesPerElement, + }, + { + value: &limits.MaxInScopeNamespaces, + fallback: d.MaxInScopeNamespaces, + }, } for _, field := range fields { @@ -189,7 +262,7 @@ func NewEngine(config Config) (*Engine, error) { } } - if limits.Timeout < 0 || limits.MaxDepth > 512 { + if limits.Timeout < 0 || limits.MaxDepth > maximumDepth { return nil, wrap("configure", ErrInput) } @@ -226,6 +299,50 @@ func NewEngine(config Config) (*Engine, error) { return e, nil } +// Messages returns an owned copy of XSLT diagnostics emitted before failure. +// It exposes no partial primary or secondary result. +func (e *Error) Messages() []Message { + if e == nil { + return nil + } + + return slices.Clone(e.messages) +} + +// Error implements error. +func (e *Error) Error() string { + if e.Code != "" { + return fmt.Sprintf("transform %s [%s]: %v", e.Operation, e.Code, e.cause) + } + + return fmt.Sprintf("transform %s: %v", e.Operation, e.cause) +} + +// Unwrap exposes the original cause. +func (e *Error) Unwrap() error { return e.cause } + +// CodeName returns the expanded error QName, or a zero Name when no code exists. +// Standard codes use http://www.w3.org/2005/xqt-errors. Application errors may +// use another namespace, including the empty namespace. +func (e *Error) CodeName() Name { + if e == nil || e.Code == "" { + return Name{} + } + + if e.name.Local != "" { + return e.name + } + + if detail, ok := errors.AsType[*xdm.Error](e.cause); ok { + name := detail.CodeQName() + if name.Local != "" { + return Name{Space: name.URI, Local: name.Local} + } + } + + return Name{Space: xpath.NSErr, Local: e.Code} +} + func (e *Engine) invocation(ctx context.Context) (context.Context, context.CancelFunc, error) { if e == nil || e.limits.MaxNodes == 0 || ctx == nil { return nil, nil, ErrInput @@ -248,13 +365,21 @@ func (e *Engine) invocation(ctx context.Context) (context.Context, context.Cance return ctx, cancel, nil } -func (e *Engine) parseOptions() xdm.ParseOptions { +// xmlLimits projects normalized engine policy onto the shared XML parser and +// immutable snapshot checker. Other XML limits keep their document defaults. +func (l Limits) xmlLimits() document.Limits { limits := document.DefaultLimits() - limits.MaxBytes = e.limits.MaxInputBytes - limits.MaxDepth = e.limits.MaxDepth - limits.MaxNodes = e.limits.MaxNodes + limits.MaxBytes = l.MaxInputBytes + limits.MaxDepth = l.MaxDepth + limits.MaxNodes = l.MaxNodes + limits.MaxAttributesPerElement = l.MaxAttributesPerElement + limits.MaxInScopeNamespaces = l.MaxInScopeNamespaces + + return limits +} - return xdm.ParseOptions{Limits: limits, AllowSingleByteEncodings: e.allowSingleByteEncodings, AllowRelativeNamespaces: true} +func (e *Engine) parseOptions() xdm.ParseOptions { + return xdm.ParseOptions{Limits: e.limits.xmlLimits(), AllowSingleByteEncodings: e.allowSingleByteEncodings, AllowRelativeNamespaces: true} } // Language parsing supports DTD semantics; security snapshots and approved @@ -293,61 +418,3 @@ func wrap(operation string, err error) error { return &Error{Operation: operation, Code: code, cause: err} } - -// Error identifies a failed operation and retains its XPath, XSLT or application -// error code when available. errors.Is preserves underlying cancellation and -// resource-limit errors. -type Error struct { - // Operation identifies the library operation that failed. - Operation string - // Code is the local part of the XPath, XSLT or application error code, - // or empty when none is available. CodeName also preserves its namespace. - Code string - name Name - cause error - messages []Message -} - -// Messages returns an owned copy of XSLT diagnostics emitted before failure. -// It exposes no partial primary or secondary result. -func (e *Error) Messages() []Message { - if e == nil { - return nil - } - - return slices.Clone(e.messages) -} - -// Error implements error. -func (e *Error) Error() string { - if e.Code != "" { - return fmt.Sprintf("transform %s [%s]: %v", e.Operation, e.Code, e.cause) - } - - return fmt.Sprintf("transform %s: %v", e.Operation, e.cause) -} - -// Unwrap exposes the original cause. -func (e *Error) Unwrap() error { return e.cause } - -// CodeName returns the expanded error QName, or a zero Name when no code exists. -// Standard codes use http://www.w3.org/2005/xqt-errors. Application errors may -// use another namespace, including the empty namespace. -func (e *Error) CodeName() Name { - if e == nil || e.Code == "" { - return Name{} - } - - if e.name.Local != "" { - return e.name - } - - if detail, ok := errors.AsType[*xdm.Error](e.cause); ok { - name := detail.CodeQName() - if name.Local != "" { - return Name{Space: name.URI, Local: name.Local} - } - } - - return Name{Space: xpath.NSErr, Local: e.Code} -} diff --git a/transform/function.go b/transform/function.go index 919e534..fac3b10 100644 --- a/transform/function.go +++ b/transform/function.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "io" + "math/big" "time" "github.com/skarm/xmlsec/internal/lexicon" @@ -48,6 +49,21 @@ func (e *Engine) registerFunctions(functions []Function) error { } func (e *Engine) languageContext(ctx context.Context) context.Context { + ctx = context.WithValue(ctx, languageLimitsKey{}, e.limits) + + resources := xslt.InvocationResources{ + Collections: e.collectionResolver(), ParseOptions: e.languageParseOptions(), + MaxResourceBytes: int64(e.limits.MaxInputBytes), + } + if e.resolver != nil { + resources.URIResolver = resourceAdapter{engine: e} + } + + if e.packages != nil { + resources.Packages = packageAdapter{engine: e} + } + + ctx = xslt.WithInvocationResources(ctx, resources) ctx = xpath.WithInvocationScope(ctx) ctx = xpath.WithTextResourceCacheLimit(ctx, e.limits.MaxResourceBytes) ctx = xpath.WithXMLSchemaVersion(ctx, int(e.xmlSchemaVersion)) @@ -113,6 +129,8 @@ type applicationFunction struct { resultType xdm.SequenceType } +type languageLimitsKey struct{} + func (f *applicationFunction) MinArity() int { return f.definition.Arity } func (f *applicationFunction) MaxArity() int { return f.definition.Arity } @@ -128,14 +146,30 @@ func (f *applicationFunction) Call(ctx context.Context, arguments []xdm.Sequence return nil, executionControlCause(err) } - checker := sequenceChecker{ctx: ctx, limits: f.limits, input: true} + limits := f.limits + if current, ok := ctx.Value(languageLimitsKey{}).(Limits); ok { + limits = current + } + + checker := sequenceChecker{ctx: ctx, limits: limits, input: true} args := make([]Sequence, len(arguments)) for i, arg := range arguments { + before := checker.count if err := checker.walk(arg, 0); err != nil { return nil, executionControlCause(err) } + // Validation is bounded by MaxNodes; charge that work before cloning + // the argument or allowing the application callback to run. + if err := xpath.Charge(ctx, checker.count-before); err != nil { + return nil, executionControlCause(err) + } + + if err := chargeCallbackArgumentCopies(ctx, arg); err != nil { + return nil, executionControlCause(err) + } + args[i] = ownSequence(arg) } @@ -144,7 +178,7 @@ func (f *applicationFunction) Call(ctx context.Context, arguments []xdm.Sequence return nil, executionControlCause(err) } - checker = sequenceChecker{ctx: ctx, limits: f.limits, input: true} + checker = sequenceChecker{ctx: ctx, limits: limits, input: true} if err := checker.walk(result.items, 0); err != nil { return nil, executionControlCause(err) } @@ -159,3 +193,36 @@ func (f *applicationFunction) Call(ctx context.Context, arguments []xdm.Sequence return xdm.CloneSequence(result.items), nil } + +// Immutable map/array entries and scalar strings are shared by CloneSequence. +// Charge only top-level atomic payloads that CloneItem actually duplicates. +func chargeCallbackArgumentCopies(ctx context.Context, argument xdm.Sequence) error { + for item := range xdm.SequenceItems(argument) { + if err := ctx.Err(); err != nil { + return err + } + + value, ok := item.(xdm.AtomicValue) + if !ok { + continue + } + + switch value.Value.(type) { + case *big.Int, *big.Rat, *xdm.FloatValue: + if err := xpath.ChargeNumber(ctx, value); err != nil { + return err + } + case []byte, xdm.Duration, *xdm.Duration: + text, err := lexical(value) + if err != nil { + return err + } + + if err := xpath.ChargeString(ctx, len(text)); err != nil { + return err + } + } + } + + return nil +} diff --git a/transform/helpers_test.go b/transform/helpers_test.go index 6a2d121..996080e 100644 --- a/transform/helpers_test.go +++ b/transform/helpers_test.go @@ -3,6 +3,7 @@ package transform_test import ( "testing" + "github.com/skarm/xmlsec/reference" "github.com/skarm/xmlsec/transform" ) @@ -196,3 +197,11 @@ func comparisonErrorCode(value string) bool { return true } + +func requireInvalidReferenceResult(t *testing.T, result reference.Data) { + t.Helper() + + if result.Kind() != reference.Invalid { + t.Fatal("failed transform returned a partial result", result.Kind()) + } +} diff --git a/transform/internal/pattern/characters.go b/transform/internal/pattern/characters.go index f6ae8a0..a90a6f1 100644 --- a/transform/internal/pattern/characters.go +++ b/transform/internal/pattern/characters.go @@ -11,6 +11,16 @@ import ( "unicode/utf8" ) +// Character-class parsing bounds nested subtraction and parses Unicode escapes as 32-bit hexadecimal values. +const ( + maxCharacterClassDepth = 128 + hexadecimalRadix = 16 + codepointParseBits = 32 +) + +// Range search adds this fixed work weight to its size-dependent charge. +const rangeSearchWorkWeight = 4 + type ( interval struct { lo rune @@ -55,7 +65,7 @@ func (s *characterSet) contains(r rune) bool { // complexity charges the binary searches, property predicates and subtraction // depth before evaluation. Large unions cannot consume unaccounted match work. func (s *characterSet) complexity() int { - cost := 1 + 4*bits.Len(uint(len(s.ranges))) + cost := 1 + rangeSearchWorkWeight*bits.Len(uint(len(s.ranges))) for _, p := range s.properties { cost += 1 + bits.Len(uint(len(p.table.R16)+len(p.table.R32))) } @@ -150,7 +160,7 @@ type classParser struct { } func (p *classParser) bracket(depth int) (*characterSet, error) { - if depth > 128 { + if depth > maxCharacterClassDepth { return nil, ErrLimit } @@ -310,7 +320,7 @@ func (p *classParser) hexRune() (rune, error) { return 0, errors.New("unterminated hex escape") } - n, err := strconv.ParseUint(p.src[p.pos+1:p.pos+end], 16, 32) + n, err := strconv.ParseUint(p.src[p.pos+1:p.pos+end], hexadecimalRadix, codepointParseBits) if err != nil || n > unicode.MaxRune { return 0, errors.New("invalid codepoint") } diff --git a/transform/internal/pattern/parser.go b/transform/internal/pattern/parser.go index d79f7e0..211b019 100644 --- a/transform/internal/pattern/parser.go +++ b/transform/internal/pattern/parser.go @@ -10,8 +10,13 @@ import ( "strings" "unicode" "unicode/utf8" + + "github.com/skarm/xmlsec/internal/lexicon" ) +// maxGroupDepth caps nested parenthesized groups independently of the capture-count ceiling. +const maxGroupDepth = 128 + // maxBacktrackGroups bounds the capture count. The capture vector is copied on // each saved branch, so the limit bounds snapshot size. const maxBacktrackGroups = 128 @@ -114,7 +119,7 @@ type btParser struct { } func (p *btParser) errf(format string, a ...any) error { - return fmt.Errorf("FORX0002: "+format, a...) + return fmt.Errorf(lexicon.ErrFORX0002+": "+format, a...) } // parseAlternation is the top level: branch ("|" branch)*. @@ -335,7 +340,7 @@ func (p *btParser) parseGroup() (btNode, error) { p.depth++ defer func() { p.depth-- }() - if p.depth > 128 { + if p.depth > maxGroupDepth { return nil, ErrLimit } diff --git a/transform/internal/pattern/pattern.go b/transform/internal/pattern/pattern.go index 854a34f..c2665f8 100644 --- a/transform/internal/pattern/pattern.go +++ b/transform/internal/pattern/pattern.go @@ -12,6 +12,45 @@ import ( "unicode/utf8" ) +// Compilation ceilings independently bound source bytes, VM instructions and repeat counters. +const ( + maxPatternBytes = 64 << 10 + maxProgramInstructions = 8192 + maxRepeatCounters = 128 +) + +// Zero Work limits select these defaults, measured in matching steps and logical storage cells. +const ( + defaultMaxSteps = 2_000_000 + defaultMaxCells = 1 << 20 +) + +// CacheCost uses these conservative admission weights, not exact Go heap sizes. +// Equal weights describe different structures and remain independently named. +const ( + programCacheOverhead = 128 + instructionCacheWeight = 128 + literalRuneCacheWeight = 8 + characterSetCacheOverhead = 128 + characterRangeCacheWeight = 16 + propertyReferenceCacheWeight = 32 + propertyTableCacheOverhead = 128 + unicodeRange16CacheWeight = 16 + unicodeRange32CacheWeight = 32 +) + +// VM state accounting counts capture offsets and counter fields in logical cells. +// Visit keys encode integer fields as varints; their byte storage is converted to cells. +const ( + captureOffsetsPerGroup = 2 + counterStateCells = 3 + fixedStateCells = 2 + visitKeyBytesPerCell = 8 + visitedStateOverhead = 4 + stateKeyHeaderBytes = fixedStateCells * binary.MaxVarintLen64 + stateKeyCounterBytes = counterStateCells * binary.MaxVarintLen64 +) + // ErrLimit reports an exhausted parser, work or storage limit. var ErrLimit = errors.New("pattern: resource limit exceeded") @@ -125,7 +164,7 @@ type stateStack struct { // Compile parses a pattern without expanding counted repetitions. func Compile(source string, options Options) (*Program, error) { - if len(source) > 64<<10 { + if len(source) > maxPatternBytes { return nil, ErrLimit } @@ -181,22 +220,22 @@ func (p *Program) CacheCost(limit int) int { return true } - if !charge(128 + cap(p.code)*128) { + if !charge(programCacheOverhead + cap(p.code)*instructionCacheWeight) { return cost } for _, inst := range p.code { - if !charge(cap(inst.text) * 8) { + if !charge(cap(inst.text) * literalRuneCacheWeight) { return cost } for set := inst.set; set != nil; set = set.subtract { - if !charge(128 + cap(set.ranges)*16 + cap(set.properties)*32) { + if !charge(characterSetCacheOverhead + cap(set.ranges)*characterRangeCacheWeight + cap(set.properties)*propertyReferenceCacheWeight) { return cost } for _, property := range set.properties { - if !charge(128 + cap(property.table.R16)*16 + cap(property.table.R32)*32) { + if !charge(propertyTableCacheOverhead + cap(property.table.R16)*unicodeRange16CacheWeight + cap(property.table.R32)*unicodeRange32CacheWeight) { return cost } } @@ -266,7 +305,7 @@ func (p *Program) Each(ctx context.Context, s string, limit int, work Work, fn f } func (p *Program) append(i instruction) (int, error) { - if len(p.code) >= 8192 { + if len(p.code) >= maxProgramInstructions { return 0, ErrLimit } @@ -339,7 +378,7 @@ func (p *Program) emitGroup(n *btGroup, next int) (int, error) { return p.emit(n.node, next) } - end, err := p.append(instruction{op: opSave, index: 2*n.index + 1, x: next}) + end, err := p.append(instruction{op: opSave, index: captureOffsetsPerGroup*n.index + 1, x: next}) if err != nil { return 0, err } @@ -349,7 +388,7 @@ func (p *Program) emitGroup(n *btGroup, next int) (int, error) { return 0, err } - return p.append(instruction{op: opSave, index: 2 * n.index, x: inner}) + return p.append(instruction{op: opSave, index: captureOffsetsPerGroup * n.index, x: inner}) } func (p *Program) emitRepeat(n *btRepeat, next int) (int, error) { @@ -381,7 +420,7 @@ func (p *Program) emitRepeat(n *btRepeat, next int) (int, error) { id := p.counters p.counters++ - if p.counters > 128 { + if p.counters > maxRepeatCounters { return 0, ErrLimit } @@ -445,8 +484,8 @@ func replacementPart(replacement, source string, match []int, at int) (string, i group, err := parseCount(replacement[at+2 : at+2+closing]) next := at + closing + 3 - if err == nil && group < len(match)/2 && match[2*group] >= 0 { - return source[match[2*group]:match[2*group+1]], next, true + if err == nil && group < len(match)/captureOffsetsPerGroup && match[captureOffsetsPerGroup*group] >= 0 { + return source[match[captureOffsetsPerGroup*group]:match[captureOffsetsPerGroup*group+1]], next, true } return "", next, false @@ -462,11 +501,11 @@ func newMachine(ctx context.Context, p *Program, s string, work Work) (*machine, } if work.MaxSteps == 0 { - work.MaxSteps = 2_000_000 + work.MaxSteps = defaultMaxSteps } if work.MaxCells == 0 { - work.MaxCells = 1 << 20 + work.MaxCells = defaultMaxCells } if work.MaxSteps < 0 || work.MaxCells < 0 { @@ -506,16 +545,16 @@ func (m *machine) find(from int) ([]int, error) { func (m *machine) attempt(start int) ([]int, error) { p := m.program - if err := m.charge(2*(p.groups+1) + 3*p.counters + 2); err != nil { + if err := m.charge(captureOffsetsPerGroup*(p.groups+1) + counterStateCells*p.counters + fixedStateCells); err != nil { return nil, err } - s := state{pc: p.start, pos: start, caps: make([]int, 2*(p.groups+1)), counters: make([]counter, p.counters)} + s := state{pc: p.start, pos: start, caps: make([]int, captureOffsetsPerGroup*(p.groups+1)), counters: make([]counter, p.counters)} for i := range s.caps { s.caps[i] = -1 } - stack := stateStack{weight: len(s.caps) + 3*len(s.counters) + 2} + stack := stateStack{weight: len(s.caps) + counterStateCells*len(s.counters) + fixedStateCells} for { if err := m.charge(1); err != nil { @@ -558,11 +597,11 @@ func (m *machine) visit(s state, stackCells int) (bool, error) { } // Preserve the general path's exact logical cell charge, including // varint-width boundaries, without allocating a serialized key. - var encoded [20]byte + var encoded [stateKeyHeaderBytes]byte used := binary.PutVarint(encoded[:], int64(s.pc)) used += binary.PutVarint(encoded[used:], int64(s.pos)) - cells := used/8 + 4 + cells := used/visitKeyBytesPerCell + visitedStateOverhead if cells > m.work.MaxCells-m.cells-stackCells { return false, ErrLimit @@ -579,7 +618,7 @@ func (m *machine) visit(s state, stackCells int) (bool, error) { return false, nil } - cells := len(key)/8 + 4 + cells := len(key)/visitKeyBytesPerCell + visitedStateOverhead if cells > m.work.MaxCells-m.cells-stackCells { return false, ErrLimit } @@ -687,7 +726,7 @@ func (m *machine) reference(s *state, i instruction) (bool, error) { } func (m *machine) captured(s *state, i instruction) (bool, error) { - a, b := s.caps[2*i.index], s.caps[2*i.index+1] + a, b := s.caps[captureOffsetsPerGroup*i.index], s.caps[captureOffsetsPerGroup*i.index+1] if a < 0 || b < a { return true, nil } @@ -779,9 +818,9 @@ func (m *machine) charge(n int) error { } func (m *machine) stateKey(s state) string { - size := 20 + 30*len(s.counters) + size := stateKeyHeaderBytes + stateKeyCounterBytes*len(s.counters) if m.program.backrefs { - size += 10 * len(s.caps) + size += binary.MaxVarintLen64 * len(s.caps) } buffer := make([]byte, 0, size) diff --git a/transform/internal/pattern/pattern_test.go b/transform/internal/pattern/pattern_test.go index 1aa2f04..b222cfd 100644 --- a/transform/internal/pattern/pattern_test.go +++ b/transform/internal/pattern/pattern_test.go @@ -198,17 +198,6 @@ func FuzzPattern(f *testing.F) { }) } -func compileTest(t testing.TB, source string, options patternpkg.Options) *patternpkg.Program { - t.Helper() - - p, err := patternpkg.Compile(source, options) - if err != nil { - t.Fatal(err) - } - - return p -} - // BenchmarkPrograms separates immutable program reuse from parsing costs and // includes a bounded ambiguous failure rather than unbounded backtracking. func BenchmarkPrograms(b *testing.B) { @@ -257,3 +246,14 @@ func BenchmarkPrograms(b *testing.B) { }) } } + +func compileTest(t testing.TB, source string, options patternpkg.Options) *patternpkg.Program { + t.Helper() + + p, err := patternpkg.Compile(source, options) + if err != nil { + t.Fatal(err) + } + + return p +} diff --git a/transform/internal/unparsedtext/unparsedtext.go b/transform/internal/unparsedtext/unparsedtext.go index c4bd4c0..95b71ad 100644 --- a/transform/internal/unparsedtext/unparsedtext.go +++ b/transform/internal/unparsedtext/unparsedtext.go @@ -23,6 +23,7 @@ import ( "strings" "unicode/utf8" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/nilcheck" "github.com/skarm/xmlsec/internal/streamio" "github.com/skarm/xmlsec/internal/tree" @@ -33,6 +34,62 @@ import ( "golang.org/x/text/transform" ) +// Encoding labels preserve the canonical output spelling separately from +// separator-free identifiers used by this package's encoding normalization. +const ( + encodingUTF32BE = "utf-32be" + encodingUTF32LE = "utf-32le" + utf32NamePrefix = "utf-32" + normalizedUTF16 = "utf16" + normalizedUTF16BE = "utf16be" + normalizedUTF16LE = "utf16le" + normalizedUTF32 = "utf32" +) + +// MIME charset inference recognizes these media-type families and XML suffixes. +const ( + charsetParameter = "charset" + mediaTypeTextXML = "text/xml" + mediaTypeTextPlain = "text/plain" + textMediaTypePrefix = "text/" + applicationMediaTypePrefix = "application/" + xmlMediaSubtypeSuffix = "+xml" +) + +// Read and sniff scratch sizes are bytes, separate from Config.MaxBytes admission. +// The XML declaration bound limits the sniffing heuristic, not the declaration grammar. +// Character modes select XML Char checking or unrestricted Unicode decoding; +// zero lines and minus-one decoded bytes disable the respective local limit. +const ( + uriReadBufferBytes = 32 << 10 + decodedTextBufferBytes = 32 << 10 + encodingSniffBufferBytes = 4096 + maxEncodingPrefixBytes = len(lexicon.BOMUTF32BE) + utf16CodeUnitBytes = 2 + utf32CodeUnitBytes = 4 + maxXMLDeclarationBytes = 200 + xmlDeclarationStart = "" + jsonUnicodeEscapeBytes = 6 + requireXMLCharacters = true + allowUnicodeCharacters = false + unlimitedLines = 0 + unlimitedDecodedBytes = -1 +) + +// These scalar ranges implement the XML 1.0 Char production. A genuine U+FFFD +// remains permitted; malformed source bytes are rejected by the decoder separately. +const ( + xmlAllowedTab = '\t' + xmlAllowedLineFeed = '\n' + xmlAllowedCarriageReturn = '\r' + xmlBasicRangeStart = 0x20 + xmlBasicRangeEnd = 0xD7FF + xmlBMPRemainderStart = 0xE000 + xmlBMPRemainderEnd = 0xFFFD + xmlSupplementaryStart = 0x10000 +) + // DefaultMaxBytes is the default per-resource read cap applied when // Config.MaxBytes is 0. It guards against a hostile or pathological resource // (an effectively unbounded HTTP body or a "/dev/zero"-style stream) exhausting @@ -40,7 +97,7 @@ import ( const DefaultMaxBytes int64 = 10 << 20 // 10 MiB const ( - encodingUTF8 = "utf-8" + encodingUTF8 = lexicon.EncodingUTF8 encodingUTF16BE = "utf-16be" encodingUTF16LE = "utf-16le" ) @@ -138,7 +195,7 @@ func ReadText(ctx context.Context, cfg *Config, href, encoding string, consume f // LoadTextLines reads and validates text incrementally, retaining only the // returned lines rather than a second complete source or normalized string. func LoadTextLines(ctx context.Context, cfg *Config, href, encoding string) ([]string, error) { - lines, _, err := LoadTextLinesBounded(ctx, cfg, href, encoding, 0) + lines, _, err := LoadTextLinesBounded(ctx, cfg, href, encoding, unlimitedLines) return lines, err } @@ -266,7 +323,7 @@ func ReadURI(ctx context.Context, cfg *Config, uri string) (text string, err err var ( out strings.Builder - buffer [32 << 10]byte + buffer [uriReadBufferBytes]byte ) for { @@ -315,7 +372,7 @@ func ReadJSONText(ctx context.Context, cfg *Config, uri string, consume func([]b return false, err } - err = readDecodedText(ctx, reader, effectiveMaxBytes(cfg), false, func(chunk []byte) error { + err = readDecodedText(ctx, reader, effectiveMaxBytes(cfg), allowUnicodeCharacters, func(chunk []byte) error { if cfg != nil && cfg.DecodedBytes != nil { cfg.DecodedBytes(len(chunk)) } @@ -347,7 +404,7 @@ func DecodeText(data []byte, encoding string, transportHints ...string) (string, if hint != "" { metadata = hint if !strings.Contains(hint, "/") { - metadata = mime.FormatMediaType("text/plain", map[string]string{"charset": hint}) + metadata = mime.FormatMediaType(mediaTypeTextPlain, map[string]string{charsetParameter: hint}) } break @@ -361,7 +418,7 @@ func DecodeText(data []byte, encoding string, transportHints ...string) (string, var text strings.Builder - err = readText(context.Background(), reader, -1, func(chunk []byte) error { _, err := text.Write(chunk); return err }) + err = readText(context.Background(), reader, unlimitedDecodedBytes, func(chunk []byte) error { _, err := text.Write(chunk); return err }) if err != nil { return "", err } @@ -373,7 +430,7 @@ func DecodeText(data []byte, encoding string, transportHints ...string) (string, // normalized: CR+LF -> LF, CR -> LF. A trailing newline does not produce // an extra empty line. func SplitLines(text string) []string { - lines, _ := SplitLinesBounded(text, 0) + lines, _ := SplitLinesBounded(text, unlimitedLines) return lines } @@ -422,19 +479,19 @@ func ValidateXMLChars(s string) error { // IsValidXMLChar reports whether r is a valid XML 1.0 character. func IsValidXMLChar(r rune) bool { - if r == 0x9 || r == 0xA || r == 0xD { + if r == xmlAllowedTab || r == xmlAllowedLineFeed || r == xmlAllowedCarriageReturn { return true } - if r >= 0x20 && r <= 0xD7FF { + if r >= xmlBasicRangeStart && r <= xmlBasicRangeEnd { return true } - if r >= 0xE000 && r <= 0xFFFD { + if r >= xmlBMPRemainderStart && r <= xmlBMPRemainderEnd { return true } - return r >= 0x10000 && r <= 0x10FFFF + return r >= xmlSupplementaryStart && r <= utf8.MaxRune } // EncodingsCompatible checks if a user-specified encoding is compatible @@ -447,7 +504,7 @@ func EncodingsCompatible(specified, detected string) bool { return true } - if s == "utf16" && (d == "utf16le" || d == "utf16be") { + if s == normalizedUTF16 && (d == normalizedUTF16LE || d == normalizedUTF16BE) { return true } @@ -488,7 +545,7 @@ func isWindowsDriveScheme(parsed *url.URL) bool { func isSupportedScheme(scheme string) bool { switch scheme { - case "file", "http", "https": + case lexicon.SchemeFile, lexicon.SchemeHTTP, lexicon.SchemeHTTPS: return true default: return false @@ -514,7 +571,7 @@ func jsonCharacterChunk(cfg *Config, chunk []byte) ([]byte, bool, error) { for offset := 0; offset < len(chunk); { value, width := utf8.DecodeRune(chunk[offset:]) if !IsValidXMLChar(value) { - size += 6 - width + size += jsonUnicodeEscapeBytes - width } offset += width @@ -554,9 +611,9 @@ func decodedJSONReader(src io.Reader, contentType string) (io.Reader, bool, erro return nil, false, err } - reader := bufio.NewReaderSize(src, 4096) + reader := bufio.NewReaderSize(src, encodingSniffBufferBytes) - prefix, err := reader.Peek(4) + prefix, err := reader.Peek(maxEncodingPrefixBytes) // Only bare EOF permits a short encoding prefix; joined source errors remain fatal. if err != nil && err != io.EOF { //nolint:errorlint // Only bare EOF is completion; wrapped or joined failures must propagate. return nil, false, err @@ -567,14 +624,14 @@ func decodedJSONReader(src io.Reader, contentType string) (io.Reader, bool, erro effective, compatible := "", false if charset != "" { effective = resolveEncoding(charset, detected) - if normalizeEncodingName(charset) == "utf32" && strings.HasPrefix(detected, "utf-32") { + if normalizeEncodingName(charset) == normalizedUTF32 && strings.HasPrefix(detected, utf32NamePrefix) { effective = detected } - compatible = !strings.HasPrefix(detected, "utf-32") + compatible = !strings.HasPrefix(detected, utf32NamePrefix) } else if detected != "" { effective = detected - compatible = !strings.HasPrefix(detected, "utf-32") + compatible = !strings.HasPrefix(detected, utf32NamePrefix) } else { effective = jsonInitialEncoding(prefix) compatible = effective == encodingUTF8 @@ -596,26 +653,26 @@ func decodedJSONReader(src io.Reader, contentType string) (io.Reader, bool, erro } func jsonBOMEncoding(prefix []byte) (string, int) { - if bytes.HasPrefix(prefix, []byte{0, 0, 0xfe, 0xff}) { - return "utf-32be", 4 + if bytes.HasPrefix(prefix, []byte{lexicon.BOMUTF32BE[0], lexicon.BOMUTF32BE[1], lexicon.BOMUTF32BE[2], lexicon.BOMUTF32BE[3]}) { + return encodingUTF32BE, len(lexicon.BOMUTF32BE) } - if bytes.HasPrefix(prefix, []byte{0xff, 0xfe, 0, 0}) { - return "utf-32le", 4 + if bytes.HasPrefix(prefix, []byte{lexicon.BOMUTF32LE[0], lexicon.BOMUTF32LE[1], lexicon.BOMUTF32LE[2], lexicon.BOMUTF32LE[3]}) { + return encodingUTF32LE, len(lexicon.BOMUTF32LE) } - encoding, clean := detectBOM(prefix) + bom, clean := detectBOM(prefix) - return encoding, len(prefix) - len(clean) + return bom, len(prefix) - len(clean) } func jsonInitialEncoding(prefix []byte) string { - if len(prefix) >= 4 { + if len(prefix) >= utf32CodeUnitBytes { switch { case prefix[0] == 0 && prefix[1] == 0 && prefix[2] == 0: - return "utf-32be" + return encodingUTF32BE case prefix[1] == 0 && prefix[2] == 0 && prefix[3] == 0: - return "utf-32le" + return encodingUTF32LE case prefix[0] == 0 && prefix[2] == 0: return encodingUTF16BE case prefix[1] == 0 && prefix[3] == 0: @@ -623,7 +680,7 @@ func jsonInitialEncoding(prefix []byte) string { } } - if len(prefix) == 2 { + if len(prefix) == utf16CodeUnitBytes { if prefix[0] == 0 { return encodingUTF16BE } @@ -716,7 +773,7 @@ func openURI(ctx context.Context, cfg *Config, uri string) (io.ReadCloser, strin rc, err = cfg.URIResolver.Resolve(ctx, uri) if err != nil { - if !nilcheck.IsNil(rc) { + if rc != nil && !nilcheck.IsNil(rc) { err = errors.Join(err, rc.Close()) } @@ -800,25 +857,25 @@ func textMediaType(contentType string) (string, bool, error) { return "", false, &Error{Code: ErrCodeEncoding, Message: "invalid resource media type", Cause: err} } - xml := mediaType == "text/xml" || mediaType == "application/xml" || - (strings.HasPrefix(mediaType, "text/") || strings.HasPrefix(mediaType, "application/")) && strings.HasSuffix(mediaType, "+xml") + xml := mediaType == mediaTypeTextXML || mediaType == lexicon.MediaTypeXML || + (strings.HasPrefix(mediaType, textMediaTypePrefix) || strings.HasPrefix(mediaType, applicationMediaTypePrefix)) && strings.HasSuffix(mediaType, xmlMediaSubtypeSuffix) - return parameters["charset"], xml, nil + return parameters[charsetParameter], xml, nil } // detectBOM checks for a byte-order mark and returns the detected encoding // name and data with the BOM stripped. func detectBOM(data []byte) (string, []byte) { - if len(data) >= 3 && data[0] == 0xEF && data[1] == 0xBB && data[2] == 0xBF { - return encodingUTF8, data[3:] + if len(data) >= len(lexicon.BOMUTF8) && data[0] == lexicon.BOMUTF8[0] && data[1] == lexicon.BOMUTF8[1] && data[2] == lexicon.BOMUTF8[2] { + return encodingUTF8, data[len(lexicon.BOMUTF8):] } - if len(data) >= 2 && data[0] == 0xFF && data[1] == 0xFE { - return encodingUTF16LE, data[2:] + if len(data) >= len(lexicon.BOMUTF16LE) && data[0] == lexicon.BOMUTF16LE[0] && data[1] == lexicon.BOMUTF16LE[1] { + return encodingUTF16LE, data[len(lexicon.BOMUTF16LE):] } - if len(data) >= 2 && data[0] == 0xFE && data[1] == 0xFF { - return encodingUTF16BE, data[2:] + if len(data) >= len(lexicon.BOMUTF16BE) && data[0] == lexicon.BOMUTF16BE[0] && data[1] == lexicon.BOMUTF16BE[1] { + return encodingUTF16BE, data[len(lexicon.BOMUTF16BE):] } return "", data @@ -829,30 +886,30 @@ func detectBOM(data []byte) (string, []byte) { // It works on raw bytes assuming the declaration is in ASCII-compatible encoding. func detectXMLDeclEncoding(data []byte) string { // XML declaration must start at the very beginning. - if !bytes.HasPrefix(data, []byte("")) - if end < 0 || end > 200 { + end := bytes.Index(data, []byte(xmlDeclarationEnd)) + if end < 0 || end > maxXMLDeclarationBytes { return "" } decl := data[:end] // Look for encoding="..." or encoding='...'. - _, rest, found := bytes.Cut(decl, []byte("encoding")) + _, rest, found := bytes.Cut(decl, []byte(lexicon.DeclEncoding)) if !found { return "" } - rest = bytes.TrimLeft(rest, " \t\r\n") + rest = bytes.TrimLeft(rest, lexicon.XMLWhitespace) if len(rest) == 0 || rest[0] != '=' { return "" } rest = rest[1:] - rest = bytes.TrimLeft(rest, " \t\r\n") + rest = bytes.TrimLeft(rest, lexicon.XMLWhitespace) if len(rest) == 0 { return "" } @@ -882,7 +939,7 @@ func resolveEncoding(specified, detected string) string { // would fall back to its little-endian default. When the BOM told us // the concrete endianness, prefer the detected endian-specific // encoding so a big-endian payload is decoded as big-endian. - if detected != "" && normalizeEncodingName(specified) == "utf16" { + if detected != "" && normalizeEncodingName(specified) == normalizedUTF16 { return detected } @@ -915,10 +972,10 @@ func decodedReader(src io.Reader, specified, contentType string) (io.Reader, err specified = "" } - reader := bufio.NewReaderSize(src, 4096) + reader := bufio.NewReaderSize(src, encodingSniffBufferBytes) // These prefix probes tolerate only bare EOF, never a joined resource failure. - prefix, err := reader.Peek(4) + prefix, err := reader.Peek(maxEncodingPrefixBytes) if err != nil && err != io.EOF { //nolint:errorlint // Only bare EOF is completion; wrapped or joined failures must propagate. return nil, err } @@ -931,13 +988,13 @@ func decodedReader(src io.Reader, specified, contentType string) (io.Reader, err } if detected == "" && specified == "" { - prefix, err = reader.Peek(5) + prefix, err = reader.Peek(len(xmlDeclarationStart)) if err != nil && err != io.EOF { //nolint:errorlint // Only bare EOF is completion; wrapped or joined failures must propagate. return nil, err } - if bytes.Equal(prefix, []byte("= 0x80 { + if b >= utf8.RuneSelf { return nDst, nSrc, ErrInvalidASCII } diff --git a/transform/internal/xdm/encoding/c1fallback.go b/transform/internal/xdm/encoding/c1fallback.go index 46c9532..7cf1555 100644 --- a/transform/internal/xdm/encoding/c1fallback.go +++ b/transform/internal/xdm/encoding/c1fallback.go @@ -18,36 +18,48 @@ package encoding // output to replace U+FFFD with the C1 character corresponding to the // original byte // - c1EncoderTransformer: wraps an encoder Transformer, pre-processing its -// input to pass C1 characters (U+0080-U+009F) directly as bytes 0x80-0x9F +// input to reverse the decoder's fallback for otherwise unassigned C1 bytes import ( "encoding/binary" "unicode/utf8" enc "golang.org/x/text/encoding" + "golang.org/x/text/encoding/charmap" "golang.org/x/text/transform" ) +// C1 fallback covers only the unassigned control-code range, not every +// undefined single-byte mapping. The ASCII fast path handles one uint64 word +// in bytes; its mask selects the high bit of each octet. +const ( + c1ControlStart = 0x80 + c1ControlEnd = 0x9F + asciiWordBytes = 8 + asciiWordHighBits = 0x8080808080808080 +) + // c1Encoding wraps a charmap Encoding to provide C1 control character support. -// Bytes 0x80-0x9F are mapped to/from U+0080-U+009F instead of U+FFFD. +// Unassigned bytes 0x80-0x9F are mapped to/from U+0080-U+009F instead of U+FFFD. type c1Encoding struct { - enc.Encoding + *charmap.Charmap } func (e *c1Encoding) NewDecoder() *enc.Decoder { return &enc.Decoder{Transformer: &c1DecoderTransformer{ - base: e.Encoding.NewDecoder().Transformer, + base: e.Charmap.NewDecoder().Transformer, }} } func (e *c1Encoding) NewEncoder() *enc.Encoder { return &enc.Encoder{Transformer: &c1EncoderTransformer{ - base: e.Encoding.NewEncoder().Transformer, + base: e.Charmap.NewEncoder().Transformer, + charmap: e.Charmap, }} } -func withC1Fallback(e enc.Encoding) enc.Encoding { - return &c1Encoding{Encoding: e} +func withC1Fallback(e *charmap.Charmap) enc.Encoding { + return &c1Encoding{Charmap: e} } // c1DecoderTransformer wraps a decoder transformer. After the base decoder @@ -73,16 +85,16 @@ func (t *c1DecoderTransformer) Transform(dst, src []byte, atEOF bool) (nDst, nSr // set holds eight ASCII bytes, each of which the branch below would // copy unchanged. A word that does not fit in dst is left to that // branch, which reports ErrShortDst after the bytes that fit. - for len(src)-nSrc >= 8 && len(dst)-nDst >= 8 { + for len(src)-nSrc >= asciiWordBytes && len(dst)-nDst >= asciiWordBytes { w := binary.LittleEndian.Uint64(src[nSrc:]) - if w&0x8080808080808080 != 0 { + if w&asciiWordHighBits != 0 { break } binary.LittleEndian.PutUint64(dst[nDst:], w) - nSrc += 8 - nDst += 8 + nSrc += asciiWordBytes + nDst += asciiWordBytes } if nSrc >= len(src) { @@ -92,7 +104,7 @@ func (t *c1DecoderTransformer) Transform(dst, src []byte, atEOF bool) (nDst, nSr b := src[nSrc] // ASCII bytes pass through unchanged - if b < 0x80 { + if b < utf8.RuneSelf { if nDst >= len(dst) { err = transform.ErrShortDst return @@ -122,9 +134,14 @@ func (t *c1DecoderTransformer) decodeByte(dst, src []byte, atEOF bool) (int, int written, consumed, err := t.base.Transform(decoded, src, atEOF) fallback := consumed == 0 - if src[0] <= 0x9f { + + replacement := written == len(replacementUTF8) && decoded[0] == replacementUTF8[0] && decoded[1] == replacementUTF8[1] && decoded[2] == replacementUTF8[2] + if src[0] <= c1ControlEnd { // C1 bytes use their own code points when the base decoder has no mapping. - fallback = fallback || written == 3 && decoded[0] == 0xef && decoded[1] == 0xbf && decoded[2] == 0xbd + fallback = fallback || replacement + } else if replacement { + // Outside C1, the replacement denotes an undefined source octet. + return 0, 0, ErrInvalidEncodedChar } else if fallback && (err == transform.ErrShortDst || err == transform.ErrShortSrc) { //nolint:errorlint // Only exact transform retry sentinels permit conversion to continue. // Only the Transformer's exact retry signals request more input/output space. return 0, 0, err @@ -148,10 +165,11 @@ func (t *c1DecoderTransformer) decodeByte(dst, src []byte, atEOF bool) (int, int } // c1EncoderTransformer wraps an encoder transformer. It handles C1 control -// characters (U+0080-U+009F) by writing them directly as bytes 0x80-0x9F -// instead of passing them through the base encoder (which would error). +// characters (U+0080-U+009F) by reversing the decoder's fallback for unassigned +// bytes. Characters with an existing mapping stay with the base encoder. type c1EncoderTransformer struct { - base transform.Transformer + base transform.Transformer + charmap *charmap.Charmap } func (t *c1EncoderTransformer) Reset() { @@ -176,7 +194,7 @@ func (t *c1EncoderTransformer) Transform(dst, src []byte, atEOF bool) (nDst, nSr } r, size := utf8.DecodeRune(src[nSrc:]) - if r == utf8.RuneError && size <= 1 { + if r == utf8.RuneError && size <= malformedUTF8Width { if !atEOF && !utf8.FullRune(src[nSrc:]) { err = transform.ErrShortSrc return @@ -194,8 +212,10 @@ func (t *c1EncoderTransformer) Transform(dst, src []byte, atEOF bool) (nDst, nSr continue } - // C1 control character? Write directly as the byte value. - if r >= 0x80 && r <= 0x9F { + // Add only missing C1 mappings. An already assigned byte (for example + // Windows-1252's 0x80 for U+20AC) must retain its original character; + // writing U+0080 there would silently change it on the next decode. + if r >= c1ControlStart && r <= c1ControlEnd && t.charmap.DecodeByte(byte(r)) == utf8.RuneError { if nDst >= len(dst) { err = transform.ErrShortDst return diff --git a/transform/internal/xdm/encoding/ebcdic.go b/transform/internal/xdm/encoding/ebcdic.go index 5a12828..bcd70d7 100644 --- a/transform/internal/xdm/encoding/ebcdic.go +++ b/transform/internal/xdm/encoding/ebcdic.go @@ -4,19 +4,23 @@ package encoding import ( + "fmt" "unicode/utf8" enc "golang.org/x/text/encoding" "golang.org/x/text/transform" ) +// A single-byte code page has one mapping for every possible octet. +const singleByteCodePageEntries = 256 + // cp1141Table maps IBM Code Page 1141 (EBCDIC Austria/Germany + euro) // byte values to Unicode code points. CP1141 is CP273 with the currency // sign (U+00A4) at position 0x9F replaced by the euro sign (U+20AC). // // Source: ibm-1141_P100-1997.ucm from unicode-org/icu-data. -func cp1141Table() [256]rune { - return [256]rune{ +func cp1141Table() [singleByteCodePageEntries]rune { + return [singleByteCodePageEntries]rune{ // 0x00-0x0F 0x0000, 0x0001, 0x0002, 0x0003, 0x009C, 0x0009, 0x0086, 0x007F, 0x0097, 0x008D, 0x008E, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, // 0x10-0x1F @@ -66,7 +70,7 @@ func newCodePage1141() enc.Encoding { } type ebcdicEncoding struct { - forward [256]rune + forward [singleByteCodePageEntries]rune reverse map[rune]byte } @@ -80,7 +84,7 @@ func (e *ebcdicEncoding) NewEncoder() *enc.Encoder { type ebcdicDecoder struct { transform.NopResetter - table *[256]rune + table *[singleByteCodePageEntries]rune } func (d *ebcdicDecoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) { @@ -109,7 +113,7 @@ type ebcdicEncoder struct { func (e *ebcdicEncoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) { for nSrc < len(src) { r, size := utf8.DecodeRune(src[nSrc:]) - if r == utf8.RuneError && size <= 1 { + if r == utf8.RuneError && size <= malformedUTF8Width { if !atEOF && !utf8.FullRune(src[nSrc:]) { err = transform.ErrShortSrc return @@ -123,7 +127,7 @@ func (e *ebcdicEncoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, b, ok := e.table[r] if !ok { - b = 0x3F // EBCDIC question mark (substitution character) + return nDst, nSrc, fmt.Errorf("encoding: character %U cannot be represented in IBM1141", r) } dst[nDst] = b diff --git a/transform/internal/xdm/encoding/encoder_test.go b/transform/internal/xdm/encoding/encoder_test.go index 138d8a3..a57ddcb 100644 --- a/transform/internal/xdm/encoding/encoder_test.go +++ b/transform/internal/xdm/encoding/encoder_test.go @@ -115,7 +115,11 @@ func TestRepertoireProbeResetsStateAfterUnsupportedCharacters(t *testing.T) { name string yes, no rune }{ - {"iso-8859-1", 'é', '中'}, {"windows-1252", '€', '😀'}, {"iso-2022-jp", 'あ', '😀'}, + {"iso-8859-1", 'é', '中'}, + {"windows-1252", '€', '😀'}, + {"iso-2022-jp", 'あ', '😀'}, + {"ibm1141", '€', '中'}, + {"windows-1252", '\u0081', '\u0080'}, } { t.Run(test.name, func(t *testing.T) { t.Parallel() @@ -130,6 +134,28 @@ func TestRepertoireProbeResetsStateAfterUnsupportedCharacters(t *testing.T) { } } +func TestSingleByteEncodersPreserveRepresentableCharacters(t *testing.T) { + t.Parallel() + + for _, name := range []string{"windows-1250", "windows-1251", "windows-1252", "windows-1253", "windows-1254", "windows-1255", "windows-1256", "windows-1257", "windows-1258", "windows-874", "iso-8859-1", "iso-8859-2", "ibm1141"} { + t.Run(name, func(t *testing.T) { + t.Parallel() + + codec := encoding.Load(name) + for r := rune(0); r <= 0xff; r++ { + encoded, err := codec.NewEncoder().String(string(r)) + if err != nil { + continue + } + decoded, err := codec.NewDecoder().String(encoded) + if err != nil || decoded != string(r) { + t.Fatalf("encoding U+%04X succeeded but decoded to %q, %v", r, decoded, err) + } + } + }) + } +} + func TestASCIILabelAliasesAndNonAliases(t *testing.T) { t.Parallel() diff --git a/transform/internal/xdm/encoding/encoding.go b/transform/internal/xdm/encoding/encoding.go index 90da8e7..e5298df 100644 --- a/transform/internal/xdm/encoding/encoding.go +++ b/transform/internal/xdm/encoding/encoding.go @@ -10,6 +10,8 @@ import ( "strings" "unicode/utf8" + "github.com/skarm/xmlsec/internal/lexicon" + enc "golang.org/x/text/encoding" "golang.org/x/text/encoding/charmap" "golang.org/x/text/encoding/japanese" @@ -46,16 +48,22 @@ func IsASCII(name string) bool { // Repertoire returns a character probe using a dedicated encoder. The closure // reuses its buffers and must not be shared across concurrent serializations. func Repertoire(encoder *enc.Encoder) func(rune) bool { + // Scratch capacity is in bytes; the probe supplies one complete scalar. + const ( + repertoireBufferBytes = 64 + completeScalarInput = true + ) + var ( source [utf8.UTFMax]byte - destination [64]byte + destination [repertoireBufferBytes]byte ) return func(r rune) bool { n := utf8.EncodeRune(source[:], r) encoder.Reset() - _, consumed, err := encoder.Transform(destination[:], source[:n], true) + _, consumed, err := encoder.Transform(destination[:], source[:n], completeScalarInput) return err == nil && consumed == n } @@ -80,8 +88,23 @@ func normalizeEncodingName(name string) string { // "ascii" synonym. It is the single source of truth for both Load (which returns // the strict US-ASCII codec) and IsASCII. func asciiEncodingNames(key string) bool { + // These normalized aliases preserve the exact recognized codec vocabulary. + const ( + aliasUSASCII = "usascii" + aliasASCII = "ascii" + aliasANSIX341968 = "ansix341968" + aliasANSIX341986 = "ansix341986" + aliasCSASCII = "csascii" + aliasISOIR6 = "isoir6" + aliasISO646US = "iso646us" + aliasISO646IRV1991 = "iso646irv1991" + aliasUS = "us" + aliasIBM367 = "ibm367" + aliasCP367 = "cp367" + ) + switch key { - case "usascii", "ascii", "ansix341968", "ansix341986", "csascii", "isoir6", "iso646us", "iso646irv1991", "us", "ibm367", "cp367": + case aliasUSASCII, aliasASCII, aliasANSIX341968, aliasANSIX341986, aliasCSASCII, aliasISOIR6, aliasISO646US, aliasISO646IRV1991, aliasUS, aliasIBM367, aliasCP367: return true } @@ -89,90 +112,230 @@ func asciiEncodingNames(key string) bool { } func loadUnicodeEncoding(norm string) enc.Encoding { + // These normalized aliases preserve the exact recognized codec vocabulary. + const ( + aliasUNICODE11UTF8 = "unicode11utf8" + aliasUNICODE20UTF8 = "unicode20utf8" + aliasXUNICODE20UTF8 = "xunicode20utf8" + aliasUTF16LE = "utf16le" + aliasUNICODEFEFF = "unicodefeff" + aliasUTF16BE = "utf16be" + aliasUNICODEFFFE = "unicodefffe" + aliasUTF16 = "utf16" + aliasUNICODE = "unicode" + aliasCSUNICODE = "csunicode" + aliasUCS4BE = "ucs4be" + aliasUTF32BE = "utf32be" + aliasISO10646UCS4 = "iso10646ucs4" + aliasUCS4LE = "ucs4le" + aliasUTF32LE = "utf32le" + aliasUCS4 = "ucs4" + aliasUTF32 = "utf32" + aliasUCS42143 = "ucs42143" + aliasUCS43412 = "ucs43412" + aliasUCS2 = "ucs2" + aliasISO10646UCS2 = "iso10646ucs2" + ) + switch norm { - case "utf8", "unicode11utf8", "unicode20utf8", "xunicode20utf8": + case lexicon.EncodingUTF8Alt, aliasUNICODE11UTF8, aliasUNICODE20UTF8, aliasXUNICODE20UTF8: return unicode.UTF8 - case "utf16le", "unicodefeff": - return withStrictDecode(unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM), 2, orderLE2, false) - case "utf16be", "unicodefffe": - return withStrictDecode(unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM), 2, orderBE2, false) - case "utf16", "unicode", "csunicode": + case aliasUTF16LE, aliasUNICODEFEFF: + return withStrictDecode(unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM), utf16CodeUnitBytes, orderLE2, ignoreLeadingBOM) + case aliasUTF16BE, aliasUNICODEFFFE: + return withStrictDecode(unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM), utf16CodeUnitBytes, orderBE2, ignoreLeadingBOM) + case aliasUTF16, aliasUNICODE, aliasCSUNICODE: // UseBOM with LittleEndian default: x/text's decoder falls back to the // configured default (LE) when no BOM is present, so the validator's // no-BOM order must be LE to match. - return withStrictDecode(unicode.UTF16(unicode.LittleEndian, unicode.UseBOM), 2, orderLE2, true) - case "ucs4be", "utf32be", "iso10646ucs4": - return withStrictDecode(utf32.UTF32(utf32.BigEndian, utf32.IgnoreBOM), 4, orderBE4, false) - case "ucs4le", "utf32le": - return withStrictDecode(utf32.UTF32(utf32.LittleEndian, utf32.IgnoreBOM), 4, orderLE4, false) - case "ucs4", "utf32": + return withStrictDecode(unicode.UTF16(unicode.LittleEndian, unicode.UseBOM), utf16CodeUnitBytes, orderLE2, honorLeadingBOM) + case aliasUCS4BE, aliasUTF32BE, aliasISO10646UCS4: + return withStrictDecode(utf32.UTF32(utf32.BigEndian, utf32.IgnoreBOM), utf32CodeUnitBytes, orderBE4, ignoreLeadingBOM) + case aliasUCS4LE, aliasUTF32LE: + return withStrictDecode(utf32.UTF32(utf32.LittleEndian, utf32.IgnoreBOM), utf32CodeUnitBytes, orderLE4, ignoreLeadingBOM) + case aliasUCS4, aliasUTF32: // UseBOM with BigEndian default: x/text's decoder falls back to the // configured default (BE) when no BOM is present, so the validator's // no-BOM order must be BE to match. - return withStrictDecode(utf32.UTF32(utf32.BigEndian, utf32.UseBOM), 4, orderBE4, true) - case "ucs42143": - return withStrictDecode(&ucs4SwapEncoding{swap: swap2143}, 4, order2143, false) - case "ucs43412": - return withStrictDecode(&ucs4SwapEncoding{swap: swap3412}, 4, order3412, false) - case "ucs2", "iso10646ucs2": - return withStrictDecode(unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM), 2, orderBE2, false) + return withStrictDecode(utf32.UTF32(utf32.BigEndian, utf32.UseBOM), utf32CodeUnitBytes, orderBE4, honorLeadingBOM) + case aliasUCS42143: + return withStrictDecode(&ucs4SwapEncoding{swap: swap2143}, utf32CodeUnitBytes, order2143, ignoreLeadingBOM) + case aliasUCS43412: + return withStrictDecode(&ucs4SwapEncoding{swap: swap3412}, utf32CodeUnitBytes, order3412, ignoreLeadingBOM) + case aliasUCS2, aliasISO10646UCS2: + return withStrictDecode(unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM), utf16CodeUnitBytes, orderBE2, ignoreLeadingBOM) } return nil } func loadEastAsianEncoding(norm string) enc.Encoding { + // These normalized aliases preserve the exact recognized codec vocabulary. + const ( + aliasEUCJP = "eucjp" + aliasXEUCJP = "xeucjp" + aliasCSEUCPKDFMTJAPANESE = "cseucpkdfmtjapanese" + aliasSHIFTJIS = "shiftjis" + aliasCP932 = "cp932" + aliasSJIS = "sjis" + aliasMS932 = "ms932" + aliasMSKANJI = "mskanji" + aliasWINDOWS31J = "windows31j" + aliasXSJIS = "xsjis" + aliasCSSHIFTJIS = "csshiftjis" + aliasJIS = "jis" + aliasISO2022JP = "iso2022jp" + aliasCSISO2022JP = "csiso2022jp" + aliasBIG5 = "big5" + aliasCSBIG5 = "csbig5" + aliasEUCKR = "euckr" + aliasCSEUCKR = "cseuckr" + aliasKSC56011987 = "ksc56011987" + aliasCSKSC56011987 = "csksc56011987" + aliasHZGB2312 = "hzgb2312" + ) + switch norm { - case "eucjp", "xeucjp", "cseucpkdfmtjapanese": - return japanese.EUCJP - case "shiftjis", "cp932", "sjis", "ms932", "mskanji", "windows31j", "xsjis", "csshiftjis": - return japanese.ShiftJIS - case "jis", "iso2022jp", "csiso2022jp": - return japanese.ISO2022JP - case "big5", "csbig5": - return traditionalchinese.Big5 - case "euckr", "cseuckr", "ksc56011987", "csksc56011987": - return korean.EUCKR - case "hzgb2312": - return simplifiedchinese.HZGB2312 + case aliasEUCJP, aliasXEUCJP, aliasCSEUCPKDFMTJAPANESE: + return withStrictLegacyDecode(japanese.EUCJP) + case aliasSHIFTJIS, aliasCP932, aliasSJIS, aliasMS932, aliasMSKANJI, aliasWINDOWS31J, aliasXSJIS, aliasCSSHIFTJIS: + return withStrictLegacyDecode(japanese.ShiftJIS) + case aliasJIS, aliasISO2022JP, aliasCSISO2022JP: + return withStrictLegacyDecode(japanese.ISO2022JP) + case aliasBIG5, aliasCSBIG5: + return withStrictLegacyDecode(traditionalchinese.Big5) + case aliasEUCKR, aliasCSEUCKR, aliasKSC56011987, aliasCSKSC56011987: + return withStrictLegacyDecode(korean.EUCKR) + case aliasHZGB2312: + return withStrictLegacyDecode(simplifiedchinese.HZGB2312) } return nil } func loadISOEncoding(norm string) enc.Encoding { + // These normalized aliases preserve the exact recognized codec vocabulary. + const ( + aliasISO885910 = "iso885910" + aliasLATIN6 = "latin6" + aliasL6 = "l6" + aliasISOIR157 = "isoir157" + aliasCSISOLATIN6 = "csisolatin6" + aliasISO885913 = "iso885913" + aliasISO885914 = "iso885914" + aliasLATIN8 = "latin8" + aliasL8 = "l8" + aliasISOIR199 = "isoir199" + aliasCSISOLATIN8 = "csisolatin8" + aliasISO885915 = "iso885915" + aliasLATIN9 = "latin9" + aliasL9 = "l9" + aliasCSISOLATIN9 = "csisolatin9" + aliasISO885916 = "iso885916" + aliasLATIN10 = "latin10" + aliasL10 = "l10" + aliasISO88592 = "iso88592" + aliasISO885921987 = "iso885921987" + aliasISOIR101 = "isoir101" + aliasLATIN2 = "latin2" + aliasL2 = "l2" + aliasCSISOLATIN2 = "csisolatin2" + aliasISOLATIN2 = "isolatin2" + aliasISO88593 = "iso88593" + aliasISO885931988 = "iso885931988" + aliasISOIR109 = "isoir109" + aliasLATIN3 = "latin3" + aliasL3 = "l3" + aliasCSISOLATIN3 = "csisolatin3" + aliasISO88594 = "iso88594" + aliasISO885941988 = "iso885941988" + aliasISOIR110 = "isoir110" + aliasLATIN4 = "latin4" + aliasL4 = "l4" + aliasCSISOLATIN4 = "csisolatin4" + aliasISO88595 = "iso88595" + aliasISO885951988 = "iso885951988" + aliasISOIR144 = "isoir144" + aliasCYRILLIC = "cyrillic" + aliasCSISOLATINCYRILLIC = "csisolatincyrillic" + aliasISO88596 = "iso88596" + aliasISO885961987 = "iso885961987" + aliasISOIR127 = "isoir127" + aliasARABIC = "arabic" + aliasECMA114 = "ecma114" + aliasASMO708 = "asmo708" + aliasCSISOLATINARABIC = "csisolatinarabic" + aliasCSISO88596E = "csiso88596e" + aliasCSISO88596I = "csiso88596i" + aliasISO88597 = "iso88597" + aliasISO885971987 = "iso885971987" + aliasISOIR126 = "isoir126" + aliasGREEK = "greek" + aliasGREEK8 = "greek8" + aliasELOT928 = "elot928" + aliasECMA118 = "ecma118" + aliasCSISOLATINGREEK = "csisolatingreek" + aliasSUNEUGREEK = "suneugreek" + aliasISO88598 = "iso88598" + aliasISO885981988 = "iso885981988" + aliasISOIR138 = "isoir138" + aliasHEBREW = "hebrew" + aliasCSISOLATINHEBREW = "csisolatinhebrew" + aliasCSISO88598E = "csiso88598e" + aliasCSISO88598I = "csiso88598i" + aliasLOGICAL = "logical" + aliasVISUAL = "visual" + aliasISO88599 = "iso88599" + aliasISO885991989 = "iso885991989" + aliasISOIR148 = "isoir148" + aliasLATIN5 = "latin5" + aliasL5 = "l5" + aliasCSISOLATIN5 = "csisolatin5" + aliasISO885911 = "iso885911" + aliasTIS620 = "tis620" + aliasISO88591 = "iso88591" + aliasISO885911987 = "iso885911987" + aliasISOIR100 = "isoir100" + aliasLATIN1 = "latin1" + aliasL1 = "l1" + aliasIBM819 = "ibm819" + aliasCP819 = "cp819" + aliasCSISOLATIN1 = "csisolatin1" + aliasISOLATIN1 = "isolatin1" + ) + switch norm { - case "iso885910", "latin6", "l6", "isoir157", "csisolatin6": + case aliasISO885910, aliasLATIN6, aliasL6, aliasISOIR157, aliasCSISOLATIN6: return withC1Fallback(charmap.ISO8859_10) - case "iso885913": + case aliasISO885913: return withC1Fallback(charmap.ISO8859_13) - case "iso885914", "latin8", "l8", "isoir199", "csisolatin8": + case aliasISO885914, aliasLATIN8, aliasL8, aliasISOIR199, aliasCSISOLATIN8: return withC1Fallback(charmap.ISO8859_14) - case "iso885915", "latin9", "l9", "csisolatin9": + case aliasISO885915, aliasLATIN9, aliasL9, aliasCSISOLATIN9: return withC1Fallback(charmap.ISO8859_15) - case "iso885916", "latin10", "l10": + case aliasISO885916, aliasLATIN10, aliasL10: return withC1Fallback(charmap.ISO8859_16) - case "iso88592", "iso885921987", "isoir101", "latin2", "l2", "csisolatin2", "isolatin2": + case aliasISO88592, aliasISO885921987, aliasISOIR101, aliasLATIN2, aliasL2, aliasCSISOLATIN2, aliasISOLATIN2: return withC1Fallback(charmap.ISO8859_2) - case "iso88593", "iso885931988", "isoir109", "latin3", "l3", "csisolatin3": + case aliasISO88593, aliasISO885931988, aliasISOIR109, aliasLATIN3, aliasL3, aliasCSISOLATIN3: return withC1Fallback(charmap.ISO8859_3) - case "iso88594", "iso885941988", "isoir110", "latin4", "l4", "csisolatin4": + case aliasISO88594, aliasISO885941988, aliasISOIR110, aliasLATIN4, aliasL4, aliasCSISOLATIN4: return withC1Fallback(charmap.ISO8859_4) - case "iso88595", "iso885951988", "isoir144", "cyrillic", "csisolatincyrillic": + case aliasISO88595, aliasISO885951988, aliasISOIR144, aliasCYRILLIC, aliasCSISOLATINCYRILLIC: return withC1Fallback(charmap.ISO8859_5) - case "iso88596", "iso885961987", "isoir127", "arabic", "ecma114", "asmo708", "csisolatinarabic", "csiso88596e", "csiso88596i": + case aliasISO88596, aliasISO885961987, aliasISOIR127, aliasARABIC, aliasECMA114, aliasASMO708, aliasCSISOLATINARABIC, aliasCSISO88596E, aliasCSISO88596I: return withC1Fallback(charmap.ISO8859_6) - case "iso88597", "iso885971987", "isoir126", "greek", "greek8", "elot928", "ecma118", "csisolatingreek", "suneugreek": + case aliasISO88597, aliasISO885971987, aliasISOIR126, aliasGREEK, aliasGREEK8, aliasELOT928, aliasECMA118, aliasCSISOLATINGREEK, aliasSUNEUGREEK: return withC1Fallback(charmap.ISO8859_7) - case "iso88598", "iso885981988", "isoir138", "hebrew", "csisolatinhebrew", "csiso88598e", "csiso88598i", "logical", "visual": + case aliasISO88598, aliasISO885981988, aliasISOIR138, aliasHEBREW, aliasCSISOLATINHEBREW, aliasCSISO88598E, aliasCSISO88598I, aliasLOGICAL, aliasVISUAL: return withC1Fallback(charmap.ISO8859_8) - case "iso88599", "iso885991989", "isoir148", "latin5", "l5", "csisolatin5": + case aliasISO88599, aliasISO885991989, aliasISOIR148, aliasLATIN5, aliasL5, aliasCSISOLATIN5: return withC1Fallback(charmap.ISO8859_9) - case "iso885911", "tis620": + case aliasISO885911, aliasTIS620: // x/text/charmap does not expose ISO8859_11 directly; Windows-874 is // the closest Thai code page used in practice. return withC1Fallback(charmap.Windows874) - case "iso88591", "iso885911987", "isoir100", "latin1", "l1", "ibm819", "cp819", "csisolatin1", "isolatin1": + case aliasISO88591, aliasISO885911987, aliasISOIR100, aliasLATIN1, aliasL1, aliasIBM819, aliasCP819, aliasCSISOLATIN1, aliasISOLATIN1: // True ISO-8859-1: bytes 0x80-0x9F map to the C1 controls // U+0080-U+009F, consistent with iso-8859-2..16 above. libxml2's XML // path uses the IANA/ISO codec here, not the Windows-1252 alias the @@ -184,48 +347,106 @@ func loadISOEncoding(norm string) enc.Encoding { } func loadLegacyEncoding(norm string) enc.Encoding { + // These normalized aliases preserve the exact recognized codec vocabulary. + const ( + aliasCP437 = "cp437" + aliasIBM437 = "ibm437" + aliasCSPC8CODEPAGE437 = "cspc8codepage437" + aliasCP866 = "cp866" + aliasIBM866 = "ibm866" + aliasCSIBM866 = "csibm866" + aliasKOI8R = "koi8r" + aliasCSKOI8R = "cskoi8r" + aliasKOI8U = "koi8u" + aliasKOIR8U = "koir8u" + aliasMACINTOSH = "macintosh" + aliasMAC = "mac" + aliasCSMACINTOSH = "csmacintosh" + aliasMACINTOSHCYRILLIC = "macintoshcyrillic" + aliasXMACCYRILLIC = "xmaccyrillic" + aliasMACCYRILLIC = "maccyrillic" + aliasWINDOWS1250 = "windows1250" + aliasCP1250 = "cp1250" + aliasWINDOWS1251 = "windows1251" + aliasCP1251 = "cp1251" + aliasWINDOWS1252 = "windows1252" + aliasCP1252 = "cp1252" + aliasXCP1252 = "xcp1252" + aliasWINDOWS1253 = "windows1253" + aliasCP1253 = "cp1253" + aliasWINDOWS1254 = "windows1254" + aliasCP1254 = "cp1254" + aliasWINDOWS1255 = "windows1255" + aliasCP1255 = "cp1255" + aliasWINDOWS1256 = "windows1256" + aliasCP1256 = "cp1256" + aliasWINDOWS1257 = "windows1257" + aliasCP1257 = "cp1257" + aliasWINDOWS1258 = "windows1258" + aliasCP1258 = "cp1258" + aliasWINDOWS874 = "windows874" + aliasCP874 = "cp874" + aliasXUSERDEFINED = "xuserdefined" + aliasEBCDIC = "ebcdic" + aliasIBM037 = "ibm037" + aliasCP037 = "cp037" + aliasEBCDICUS = "ebcdicus" + aliasEBCDICCPUS = "ebcdiccpus" + aliasCSIBM037 = "csibm037" + aliasIBM1047 = "ibm1047" + aliasCP1047 = "cp1047" + aliasIBM1140 = "ibm1140" + aliasIBM01140 = "ibm01140" + aliasCP1140 = "cp1140" + aliasCCSID01140 = "ccsid01140" + aliasIBM1141 = "ibm1141" + aliasIBM01141 = "ibm01141" + aliasCP1141 = "cp1141" + aliasCCSID01141 = "ccsid01141" + ) + switch norm { - case "cp437", "ibm437", "cspc8codepage437": + case aliasCP437, aliasIBM437, aliasCSPC8CODEPAGE437: return charmap.CodePage437 - case "cp866", "ibm866", "csibm866": + case aliasCP866, aliasIBM866, aliasCSIBM866: return charmap.CodePage866 - case "koi8r", "cskoi8r": + case aliasKOI8R, aliasCSKOI8R: return charmap.KOI8R - case "koi8u", "koir8u": + case aliasKOI8U, aliasKOIR8U: return charmap.KOI8U - case "macintosh", "mac", "csmacintosh": + case aliasMACINTOSH, aliasMAC, aliasCSMACINTOSH: return charmap.Macintosh - case "macintoshcyrillic", "xmaccyrillic", "maccyrillic": + case aliasMACINTOSHCYRILLIC, aliasXMACCYRILLIC, aliasMACCYRILLIC: return charmap.MacintoshCyrillic - case "windows1250", "cp1250": + case aliasWINDOWS1250, aliasCP1250: return withC1Fallback(charmap.Windows1250) - case "windows1251", "cp1251": + case aliasWINDOWS1251, aliasCP1251: return withC1Fallback(charmap.Windows1251) - case "windows1252", "cp1252", "xcp1252": + case aliasWINDOWS1252, aliasCP1252, aliasXCP1252: return withC1Fallback(charmap.Windows1252) - case "windows1253", "cp1253": + case aliasWINDOWS1253, aliasCP1253: return withC1Fallback(charmap.Windows1253) - case "windows1254", "cp1254": + case aliasWINDOWS1254, aliasCP1254: return withC1Fallback(charmap.Windows1254) - case "windows1255", "cp1255": + case aliasWINDOWS1255, aliasCP1255: return withC1Fallback(charmap.Windows1255) - case "windows1256", "cp1256": + case aliasWINDOWS1256, aliasCP1256: return withC1Fallback(charmap.Windows1256) - case "windows1257", "cp1257": + case aliasWINDOWS1257, aliasCP1257: return withC1Fallback(charmap.Windows1257) - case "windows1258", "cp1258": + case aliasWINDOWS1258, aliasCP1258: return withC1Fallback(charmap.Windows1258) - case "windows874", "cp874": + case aliasWINDOWS874, aliasCP874: return withC1Fallback(charmap.Windows874) - case "xuserdefined": + case aliasXUSERDEFINED: return charmap.XUserDefined - case "ebcdic", "ibm037", "cp037", "ebcdicus", "ebcdiccpus", "csibm037": + case aliasEBCDIC, aliasIBM037, aliasCP037, aliasEBCDICUS, aliasEBCDICCPUS, aliasCSIBM037: return charmap.CodePage037 - case "ibm1047", "cp1047": + case aliasIBM1047, aliasCP1047: return charmap.CodePage1047 - case "ibm1140", "ibm01140", "cp1140", "ccsid01140": + case aliasIBM1140, aliasIBM01140, aliasCP1140, aliasCCSID01140: return charmap.CodePage1140 - case "ibm1141", "ibm01141", "cp1141", "ccsid01141": + case aliasIBM1141, aliasIBM01141, aliasCP1141, aliasCCSID01141: return newCodePage1141() } diff --git a/transform/internal/xdm/encoding/encoding_test.go b/transform/internal/xdm/encoding/encoding_test.go index 72e4c9d..dc6267e 100644 --- a/transform/internal/xdm/encoding/encoding_test.go +++ b/transform/internal/xdm/encoding/encoding_test.go @@ -9,9 +9,10 @@ import ( "testing" "testing/iotest" - "github.com/skarm/xmlsec/transform/internal/xdm/encoding" - "golang.org/x/text/transform" + + "github.com/skarm/xmlsec/transform/internal/unparsedtext" + "github.com/skarm/xmlsec/transform/internal/xdm/encoding" ) func TestLoadDecodesIncrementally(t *testing.T) { @@ -74,6 +75,12 @@ func TestLoadRejectsMalformedInput(t *testing.T) { {"utf-32le", []byte{0, 0, 0x11, 0}, encoding.ErrInvalidEncodedChar}, {"ucs4-2143", []byte{0, 0, 0}, encoding.ErrInvalidEncodedChar}, {"ucs4-3412", []byte{0, 0, 0}, encoding.ErrInvalidEncodedChar}, + {"euc-jp", []byte{0xff}, encoding.ErrInvalidEncodedChar}, + {"shift-jis", []byte{0xff}, encoding.ErrInvalidEncodedChar}, + {"iso-2022-jp", []byte{0x1b}, encoding.ErrInvalidEncodedChar}, + {"big5", []byte{0xff}, encoding.ErrInvalidEncodedChar}, + {"euc-kr", []byte{0xff}, encoding.ErrInvalidEncodedChar}, + {"hz-gb-2312", []byte{0xff}, encoding.ErrInvalidEncodedChar}, } { t.Run(tc.name, func(t *testing.T) { t.Parallel() @@ -98,6 +105,36 @@ func TestLoadRejectsMalformedInput(t *testing.T) { } } +func TestLegacyCodecsDecodeValidTextWithoutReplacement(t *testing.T) { + t.Parallel() + + for _, name := range []string{"euc-jp", "shift-jis", "iso-2022-jp", "big5", "euc-kr", "hz-gb-2312"} { + t.Run(name, func(t *testing.T) { + t.Parallel() + + codec := encoding.Load(name) + // The strict legacy decoder relies on U+FFFD not being a member of + // the codec's character repertoire, unlike the Unicode codecs. + if _, err := codec.NewEncoder().String("\ufffd"); err == nil { + t.Fatal("U+FFFD is representable; replacement detection would reject valid input") + } + const want = "A中" + encoded, err := codec.NewEncoder().String(want) + if err != nil { + t.Fatal(err) + } + decoder := codec.NewDecoder() + for range 2 { + decoder.Reset() + got, err := io.ReadAll(transform.NewReader(iotest.OneByteReader(strings.NewReader(encoded)), decoder)) + if err != nil || string(got) != want { + t.Fatalf("decoded %q, %v; want %q", got, err, want) + } + } + }) + } +} + func BenchmarkCodecDecoding(b *testing.B) { for _, size := range []int{64, 65536} { text := strings.Repeat("café日本😀", max(1, size/len("café日本😀"))) @@ -123,3 +160,110 @@ func BenchmarkCodecDecoding(b *testing.B) { } } } + +func TestUndefinedSingleByteDecodeRejectsSubstitution(t *testing.T) { + t.Parallel() + + for _, input := range [][]byte{{0xa1}, {'A', 0xa1, 'B'}} { + if got, err := encoding.Load("iso-8859-6").NewDecoder().Bytes(input); !errors.Is(err, encoding.ErrInvalidEncodedChar) { + t.Fatalf("undefined octet decoded to %q, %v; want ErrInvalidEncodedChar", got, err) + } + got, err := unparsedtext.DecodeText(input, "iso-8859-6") + var detail *unparsedtext.Error + if got != "" || !errors.As(err, &detail) || detail.Code != unparsedtext.ErrCodeEncoding || !errors.Is(err, encoding.ErrInvalidEncodedChar) { + t.Fatalf("undefined octet text %q, %v; want empty/FOUT1190 retaining ErrInvalidEncodedChar", got, err) + } + } +} + +func TestSingleByteDecodePreservesAssignedAndReplacementCharacters(t *testing.T) { + t.Parallel() + + for _, tc := range []struct { + name, label string + input []byte + want string + }{ + {"assigned Arabic", "iso-8859-6", []byte{0xc7}, "ا"}, + {"assigned ISO88591", "iso-8859-1", []byte{0xa1}, "¡"}, + {"missing C1 fallback", "iso-8859-6", []byte{0x80}, "\u0080"}, + {"Windows Euro assigned C1", "windows-1252", []byte{0x80}, "€"}, + {"Windows missing C1 fallback", "windows-1252", []byte{0x81}, "\u0081"}, + {"genuine UTF8 replacement", "utf-8", []byte{0xef, 0xbf, 0xbd}, "\ufffd"}, + {"genuine UTF16 replacement", "utf-16be", []byte{0xff, 0xfd}, "\ufffd"}, + } { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + got, err := encoding.Load(tc.label).NewDecoder().Bytes(tc.input) + if err != nil || string(got) != tc.want { + t.Fatalf("codec got %q, %v; want %q", got, err, tc.want) + } + text, err := unparsedtext.DecodeText(tc.input, tc.label) + if err != nil || text != tc.want { + t.Fatalf("text got %q, %v; want %q", text, err, tc.want) + } + // Unicode codecs may emit a BOM, so the exact inverse-byte assertion + // belongs only to these independently specified single-byte mappings. + if tc.label != "utf-8" && tc.label != "utf-16be" { + encoded, err := encoding.Load(tc.label).NewEncoder().Bytes([]byte(tc.want)) + if err != nil || !bytes.Equal(encoded, tc.input) { + t.Fatalf("encoder got %x, %v; want %x", encoded, err, tc.input) + } + } + }) + } +} + +func TestUndefinedSingleByteTransformCountsAndReset(t *testing.T) { + t.Parallel() + + for _, tc := range []struct { + name string + input []byte + capacity int + want string + wantSource int + }{ + {"first octet", []byte{0xa1}, 8, "", 0}, + {"after prefix", []byte{'A', 0xa1, 'B'}, 8, "A", 1}, + {"no destination", []byte{0xa1}, 0, "", 0}, + } { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + decoder := encoding.Load("iso-8859-6").NewDecoder() + dst := make([]byte, tc.capacity) + nd, ns, err := decoder.Transform(dst, tc.input, true) + if nd != len(tc.want) || ns != tc.wantSource || !errors.Is(err, encoding.ErrInvalidEncodedChar) || string(dst[:nd]) != tc.want { + t.Fatalf("Transform = %q (%d,%d), %v; want %q (%d,%d), ErrInvalidEncodedChar", dst[:nd], nd, ns, err, tc.want, len(tc.want), tc.wantSource) + } + decoder.Reset() + dst = make([]byte, 4) + nd, ns, err = decoder.Transform(dst, []byte{0xc7, 0x80}, true) + if nd != 4 || ns != 2 || err != nil || string(dst[:nd]) != "ا\u0080" { + t.Fatalf("Reset after rejection = %q (%d,%d), %v; want Arabic/C1 (4,2), nil", dst[:nd], nd, ns, err) + } + }) + } + + decoder := encoding.Load("iso-8859-6").NewDecoder() + input := []byte{'A', 0xc7, 'B'} + dst := make([]byte, 2) + nd, ns, err := decoder.Transform(dst, input, true) + if nd != 1 || ns != 1 || !errors.Is(err, transform.ErrShortDst) || string(dst[:nd]) != "A" { + t.Fatalf("short destination = %q (%d,%d), %v; want A (1,1), ErrShortDst", dst[:nd], nd, ns, err) + } + dst = make([]byte, 3) + nd, consumed, err := decoder.Transform(dst, input[ns:], true) + if nd != 3 || consumed != 2 || err != nil || string(dst[:nd]) != "اB" { + t.Fatalf("retry = %q (%d,%d), %v; want اB (3,2), nil", dst[:nd], nd, consumed, err) + } + + decoder.Reset() + dst = make([]byte, 4) + nd, ns, err = decoder.Transform(dst, []byte{0xc7, 0x80}, true) + if nd != 4 || ns != 2 || err != nil || string(dst[:nd]) != "ا\u0080" { + t.Fatalf("Reset = %q (%d,%d), %v; want Arabic/C1 (4,2), nil", dst[:nd], nd, ns, err) + } +} diff --git a/transform/internal/xdm/encoding/legacy.go b/transform/internal/xdm/encoding/legacy.go new file mode 100644 index 0000000..6a32fcf --- /dev/null +++ b/transform/internal/xdm/encoding/legacy.go @@ -0,0 +1,41 @@ +package encoding + +import ( + "bytes" + + enc "golang.org/x/text/encoding" + "golang.org/x/text/transform" +) + +// Legacy and single-byte wrappers recognize this exact UTF-8 replacement +// sequence. Unicode codecs can represent it genuinely and validate source units. +const replacementUTF8 = "\xef\xbf\xbd" + +// DecodeRune reports one consumed byte for malformed, nonempty UTF-8 input. +// Encoders share this discriminator while retaining their existing error path. +const malformedUTF8Width = 1 + +// These legacy codecs cannot represent U+FFFD. When their decoders emit it, +// they have replaced malformed source bytes; XML and unparsed-text must report +// that failure instead of accepting the substituted text. Unicode codecs can +// represent U+FFFD and require source-unit validation instead (see strict.go). +func withStrictLegacyDecode(codec enc.Encoding) enc.Encoding { + return strictLegacyEncoding{Encoding: codec} +} + +type strictLegacyEncoding struct{ enc.Encoding } + +func (e strictLegacyEncoding) NewDecoder() *enc.Decoder { + return &enc.Decoder{Transformer: strictLegacyDecoder{Transformer: e.Encoding.NewDecoder()}} +} + +type strictLegacyDecoder struct{ transform.Transformer } + +func (d strictLegacyDecoder) Transform(dst, src []byte, atEOF bool) (int, int, error) { + nDst, nSrc, err := d.Transformer.Transform(dst, src, atEOF) + if bytes.Contains(dst[:nDst], []byte(replacementUTF8)) { + return nDst, nSrc, ErrInvalidEncodedChar + } + + return nDst, nSrc, err +} diff --git a/transform/internal/xdm/encoding/strict.go b/transform/internal/xdm/encoding/strict.go index 86235ae..af6c421 100644 --- a/transform/internal/xdm/encoding/strict.go +++ b/transform/internal/xdm/encoding/strict.go @@ -11,11 +11,34 @@ package encoding import ( "errors" + "unicode/utf8" + + "github.com/skarm/xmlsec/internal/lexicon" enc "golang.org/x/text/encoding" "golang.org/x/text/transform" ) +// Unicode validation and codec construction share code-unit widths in bytes. +// A UTF-16 surrogate pair contains two units; honoring a leading BOM permits it +// to override the configured default order, while ignoring it keeps that order. +const ( + utf16CodeUnitBytes = 2 + utf32CodeUnitBytes = 4 + utf16SurrogatePairUnits = 2 + honorLeadingBOM = true + ignoreLeadingBOM = false +) + +// Unicode scalar packing uses octets, and UTF-16 reserves these surrogate ranges. +const ( + octetBits = 8 + highSurrogateStart = 0xD800 + highSurrogateEnd = 0xDBFF + lowSurrogateStart = 0xDC00 + lowSurrogateEnd = 0xDFFF +) + // ErrInvalidEncodedChar is returned when the source contains malformed input // that the base decoder would otherwise silently replace with U+FFFD (for // example an unpaired UTF-16 surrogate or an out-of-range UTF-32 scalar). @@ -38,7 +61,7 @@ const ( func (o byteOrder) scalar(unit []byte) uint32 { var v uint32 for _, idx := range o { - v = v<<8 | uint32(unit[idx]) + v = v< 0x10FFFF || (v >= 0xD800 && v <= 0xDFFF) { + if v > utf8.MaxRune || (v >= highSurrogateStart && v <= lowSurrogateEnd) { return off, unitBad } - return off + 4, unitGood + return off + utf32CodeUnitBytes, unitGood } // width == 2 (UTF-16 / UCS-2). switch { - case v >= 0xD800 && v <= 0xDBFF: + case v >= highSurrogateStart && v <= highSurrogateEnd: // High surrogate: must be followed by a low surrogate. - if off+2*t.width > len(buf) { + if off+utf16SurrogatePairUnits*t.width > len(buf) { return off, unitIncomplete } - lo := order.scalar(buf[off+t.width : off+2*t.width]) - if lo < 0xDC00 || lo > 0xDFFF { + lo := order.scalar(buf[off+t.width : off+utf16SurrogatePairUnits*t.width]) + if lo < lowSurrogateStart || lo > lowSurrogateEnd { return off, unitBad } - return off + 2*t.width, unitGood - case v >= 0xDC00 && v <= 0xDFFF: + return off + utf16SurrogatePairUnits*t.width, unitGood + case v >= lowSurrogateStart && v <= lowSurrogateEnd: // Lone low surrogate. return off, unitBad default: - // BMP scalar (includes a genuine 0xFFFD). + // BMP scalar (includes a genuine utf8.RuneError). return off + t.width, unitGood } } diff --git a/transform/internal/xdm/encoding/ucs4.go b/transform/internal/xdm/encoding/ucs4.go index 806c457..c79353c 100644 --- a/transform/internal/xdm/encoding/ucs4.go +++ b/transform/internal/xdm/encoding/ucs4.go @@ -37,15 +37,15 @@ type ucs4SwapTransformer struct { func (t *ucs4SwapTransformer) Reset() {} func (t *ucs4SwapTransformer) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) { - for nSrc+4 <= len(src) { - if nDst+4 > len(dst) { + for nSrc+utf32CodeUnitBytes <= len(src) { + if nDst+utf32CodeUnitBytes > len(dst) { err = transform.ErrShortDst return } t.swap(dst[nDst:], src[nSrc:]) - nDst += 4 - nSrc += 4 + nDst += utf32CodeUnitBytes + nSrc += utf32CodeUnitBytes } if nSrc < len(src) { diff --git a/transform/internal/xdm/float.go b/transform/internal/xdm/float.go index 939637d..46dc98d 100644 --- a/transform/internal/xdm/float.go +++ b/transform/internal/xdm/float.go @@ -8,6 +8,13 @@ import ( "math/big" ) +// Math sign selectors are shared by IEEE special-value construction and +// numeric map-key normalization; they select positive or negative magnitude. +const ( + positiveSign = 1 + negativeSign = -1 +) + // IEEE 754 mantissa precision constants. const ( PrecisionFloat = 24 // xs:float (IEEE 754 single) @@ -53,11 +60,11 @@ func newFloatValue(f float64, prec uint) *FloatValue { return &FloatValue{special: floatNaN, bf: new(big.Float).SetPrec(prec)} } - if math.IsInf(f, 1) { + if math.IsInf(f, positiveSign) { return &FloatValue{special: floatPosInf, bf: new(big.Float).SetPrec(prec)} } - if math.IsInf(f, -1) { + if math.IsInf(f, negativeSign) { return &FloatValue{special: floatNegInf, bf: new(big.Float).SetPrec(prec)} } @@ -77,11 +84,11 @@ func (fv *FloatValue) Float64() float64 { case floatNaN: return math.NaN() case floatPosInf: - return math.Inf(1) + return math.Inf(positiveSign) case floatNegInf: - return math.Inf(-1) + return math.Inf(negativeSign) case floatNegZero: - return math.Copysign(0, -1) + return math.Copysign(0, negativeSign) } panic("unreachable") diff --git a/transform/internal/xdm/float_test.go b/transform/internal/xdm/float_test.go index 0ca85e5..7ecbb88 100644 --- a/transform/internal/xdm/float_test.go +++ b/transform/internal/xdm/float_test.go @@ -5,10 +5,6 @@ import ( "testing" ) -func sameFloat(a, b float64) bool { - return (math.IsNaN(a) && math.IsNaN(b)) || (a == b && math.Signbit(a) == math.Signbit(b)) -} - func TestFloatRoundingAndNegation(t *testing.T) { t.Parallel() @@ -65,3 +61,7 @@ func TestFloatCloneDetachesBigFloat(t *testing.T) { t.Fatal("nil clone did not preserve nil") } } + +func sameFloat(a, b float64) bool { + return (math.IsNaN(a) && math.IsNaN(b)) || (a == b && math.Signbit(a) == math.Signbit(b)) +} diff --git a/transform/internal/xdm/html/dump.go b/transform/internal/xdm/html/dump.go index 60afbdc..81e3ef0 100644 --- a/transform/internal/xdm/html/dump.go +++ b/transform/internal/xdm/html/dump.go @@ -11,8 +11,10 @@ import ( "io" "iter" "maps" + "math" "strconv" "strings" + "unicode" "unicode/utf8" norm "github.com/skarm/xmlsec/internal/normalization" @@ -23,16 +25,147 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) +// Legacy document output labels select the matching single-byte codec. +// Strict Latin-1 emits references outside its repertoire; Windows mode also +// retains the reverse Windows-1252 mappings and legacy UTF-8 passthrough. +const ( + encISO88591 = "ISO-8859-1" + encWindows1252 = "Windows-1252" + strictLatin1Mode = true + windows1252Mode = false +) + +// HTML attribute names and their context sets encode the pinned serialization +// tables. Sets are space-delimited ASCII element names; the wildcard admits all. +const ( + attrAction = "action" + attrArchive = "archive" + attrAsync = "async" + attrAutofocus = "autofocus" + attrAutoplay = "autoplay" + attrBackground = "background" + attrChecked = "checked" + attrCite = "cite" + attrClassID = "classid" + attrCodebase = "codebase" + attrCompact = "compact" + attrContent = "content" + attrControls = "controls" + attrData = "data" + attrDataSrc = "datasrc" + attrDeclare = "declare" + attrDefault = "default" + attrDefer = "defer" + attrDisabled = "disabled" + attrFor = "for" + attrFormAction = "formaction" + attrFormNoValidate = "formnovalidate" + attrHidden = "hidden" + attrHTTPEquiv = "http-equiv" + attrIcon = "icon" + attrIsMap = "ismap" + attrLongDesc = "longdesc" + attrLoop = "loop" + attrManifest = "manifest" + attrMultiple = "multiple" + attrMuted = "muted" + attrNoHref = "nohref" + attrNoResize = "noresize" + attrNoShade = "noshade" + attrNoValidate = "novalidate" + attrNoWrap = "nowrap" + attrOpen = "open" + attrPoster = "poster" + attrProfile = "profile" + attrReadOnly = "readonly" + attrRequired = "required" + attrReversed = "reversed" + attrScoped = "scoped" + attrSeamless = "seamless" + attrSelected = "selected" + attrTypeMustMatch = "typemustmatch" + attrUseMap = "usemap" +) + +const ( + allHTMLElements = "*" + uriHrefElements = "a area base link" + uriCodebaseElements = "applet object" + uriSourceElements = "audio embed frame iframe img input script source track video" + mediaPlaybackElements = "audio video" + citationElements = "blockquote del ins q" + uriDataSourceElements = "button div input object select span table textarea" + modernDisabledElements = "button fieldset input keygen optgroup option select textarea" + formSubmitElements = "button input" + autofocusElements = "button input keygen select textarea" + legacyDisabledElements = "button input optgroup option select textarea" + openElements = "details dialog" + compactElements = "dir dl menu ol ul" + longDescriptionElements = "frame iframe img" + useMapElements = "img input object" + multipleElements = "input select" + requiredElements = "input select textarea" + readOnlyElements = "input textarea" + noWrapElements = "td th" +) + +// Output fragments preserve exact metadata/namespace spelling. The legacy +// compatibility system identifier suppresses a redundant HTML system literal. +const ( + htmlLegacyCompatSystemID = "about:legacy-compat" + contentTypeMetaStart = "<" + elemMeta + " " + attrHTTPEquiv + "=\"" + lexicon.HeaderContentType + "\" " + attrContent + "=\"" + defaultNamespaceStart = " " + lexicon.PrefixXMLNS + "=\"" + prefixedNamespaceStart = " " + lexicon.PrefixXMLNS + ":" + decimalCharacterReferenceFormat = "&#%d;" + hexCharacterReferenceFormat = "&#x%X;" + decimalCharRefRadix = 10 +) + +// Profile arguments distinguish ordinary/mapped segments, text escaping, +// HTML versus XML URI-attribute expansion and raw versus percent-encoded URI +// output. XML islands and namespace values retain control-character escaping. +const ( + ordinarySegment = false + mappedSegment = true + escapedTextMode = false + rawTextMode = true + htmlAttributeMode = true + xmlAttributeMode = false + rawURIMode = true + encodedURIMode = false + nonURIAttribute = false + suppressNamedEntities = true + xmlEscapeControlCharacters = true +) + +// Scalar and byte thresholds preserve the distinct name, C1 and printable-URI +// rules. URI scratch capacities/reserve are bytes; the alignment hint is only +// a starting allocation estimate, not a bound on decoded characters. +const ( + maxASCIIRune = utf8.RuneSelf - 1 + asciiDelete = 0x7F + c1ControlStart = 0x80 + escapeControlEnd = 0x9F + minimumNameRune = 0x20 + paragraphNamePrefix = 'p' + uriPrintableASCIIStart = 0x20 + uriPrintableASCIIEnd = 0x7E + minURIScratchBytes = 64 + maxURIScratchBytes = 4096 + uriScratchReserveBytes = 12 + alignmentBytesPerRuneHint = 2 +) + // HTML attribute value escaping sequences. const ( - htmlAttrEscAmp = "&" - htmlAttrEscLt = "<" - htmlAttrEscGt = ">" - htmlAttrEscQuot = """ + htmlAttrEscAmp = lexicon.EscapeAmpersand + htmlAttrEscLt = lexicon.EscapeLessThan + htmlAttrEscGt = lexicon.EscapeGreaterThan + htmlAttrEscQuot = lexicon.EscapeQuote ) const ( - markupEncodingError = "SERE0008" + markupEncodingError = lexicon.ErrSERE0008 ) // windows1252Runes and windows1252Bytes retain the reverse Windows-1252 @@ -81,7 +214,7 @@ func (w Writer) WriteSequence(ctx context.Context, out io.Writer, items iter.Seq if err := d.dumpNode(out, item.Node); err != nil { return err } - } else if err := d.dumpTextContent(out, item.Text, false); err != nil { + } else if err := d.dumpTextContent(out, item.Text, escapedTextMode); err != nil { return err } } @@ -168,9 +301,9 @@ func (d *htmlDumper) dumpDocument(out io.Writer, doc *xdm.Document) error { enc := doc.Encoding() switch enc { case encISO88591: - out = &latin1EncodingWriter{w: out, strict: true} + out = &latin1EncodingWriter{w: out, strict: strictLatin1Mode} case encWindows1252: - out = &latin1EncodingWriter{w: out, strict: false} + out = &latin1EncodingWriter{w: out, strict: windows1252Mode} } // Output DTD if present, or default DTD for HTML documents @@ -209,12 +342,16 @@ func (d *htmlDumper) writeString(out io.Writer, s string) { return } + d.check(writeHTMLString(out, s)) +} + +func writeHTMLString(out io.Writer, s string) error { n, err := io.WriteString(out, s) - if err == nil && n < len(s) { - err = io.ErrShortWrite + if err == nil && n != len(s) { + return io.ErrShortWrite } - d.check(err) + return err } // checkName verifies that an element or attribute name is safe to write into @@ -339,10 +476,10 @@ func (d *htmlDumper) dumpNode(out io.Writer, n xdm.Node) error { case xdm.ProcessingInstructionNode: if d.doeMarkers { switch n.Name() { - case "disable-output-escaping": + case lexicon.DisableOutputEscapingPITarget: d.doeActive = true return d.err - case "enable-output-escaping": + case lexicon.EnableOutputEscapingPITarget: d.doeActive = false return d.err } @@ -428,7 +565,7 @@ func (d *htmlDumper) dumpDTD(out io.Writer, dtd *xdm.Doctype) { d.writeString(out, sysID) d.writeString(out, "\"") } - } else if sysID != "" && sysID != "about:legacy-compat" { + } else if sysID != "" && sysID != htmlLegacyCompatSystemID { d.writeString(out, " SYSTEM \"") d.writeString(out, sysID) d.writeString(out, "\"") @@ -441,7 +578,7 @@ func (d *htmlDumper) dumpDTD(out io.Writer, dtd *xdm.Doctype) { // are emitted verbatim; ordinary runs are normalized and emitted incrementally. func (d *htmlDumper) normalizedContent(s string, emit func(string, bool) error) error { plain := func(value string) error { - return d.normForm.Each(d.ctx, value, func(part string) error { return emit(part, false) }) + return d.normForm.Each(d.ctx, value, func(part string) error { return emit(part, ordinarySegment) }) } start := 0 @@ -455,11 +592,13 @@ func (d *htmlDumper) normalizedContent(s string, emit func(string, bool) error) return err } - if err := emit(replacement, true); err != nil { + if err := emit(replacement, mappedSegment); err != nil { return err } - start = at + utf8.RuneLen(r) + // Advance through the original bytes, including one-byte decoding errors. + _, width := utf8.DecodeRuneInString(s[at:]) + start = at + width } return plain(s[start:]) @@ -491,9 +630,9 @@ func (d *htmlDumper) dumpText(out io.Writer, n xdm.Node) error { } func (d *htmlDumper) dumpXMLCharacters(out io.Writer, content string, mode tree.XMLCharacterMode) error { - options := tree.XMLWriteOptions{CanEncode: d.canEncode, CharacterMap: d.charMap, Normalization: d.normalizationForm, EscapeControlChars: true} + options := tree.XMLWriteOptions{CanEncode: d.canEncode, CharacterMap: d.charMap, Normalization: d.normalizationForm, EscapeControlChars: xmlEscapeControlCharacters} - _, err := tree.WriteXMLCharacters(d.ctx, out, content, mode, int(^uint(0)>>1), options) + _, err := tree.WriteXMLCharacters(d.ctx, out, content, mode, math.MaxInt, options) if errors.Is(err, tree.ErrMalformed) { err = &xdm.Error{Code: lexicon.ErrSERE0006, Message: "character is not permitted in an XML 1.0 island", Cause: err} } @@ -604,9 +743,7 @@ func (d *htmlDumper) htmlEscapeAttrValue(w io.Writer, s string, isURI bool, noEn if !mapped { if d.preserveCase && r == '&' && i+width == len(s) && pendingAmp != nil { *pendingAmp = true - _, err := io.WriteString(w, s[last:i]) - - return err + return writeHTMLString(w, s[last:i]) } if d.preserveCase && (r == '<' || r == '&' && i+width < len(s) && s[i+width] == '{') { @@ -630,17 +767,15 @@ func (d *htmlDumper) htmlEscapeAttrValue(w io.Writer, s string, isURI bool, noEn last = i } - _, err := io.WriteString(w, s[last:]) - - return err + return writeHTMLString(w, s[last:]) } func (d *htmlDumper) attributeRuneEscape(r rune, isURI, noEntityEnc bool) (string, bool) { switch { - case d.escapeControlChars && r >= 0x80 && r <= 0x9f: - return fmt.Sprintf("&#%d;", r), false - case r > 127 && d.canEncode != nil && !d.canEncode(r): - return fmt.Sprintf("&#%d;", r), false + case d.escapeControlChars && r >= c1ControlStart && r <= escapeControlEnd: + return fmt.Sprintf(decimalCharacterReferenceFormat, r), false + case r > maxASCIIRune && d.canEncode != nil && !d.canEncode(r): + return fmt.Sprintf(decimalCharacterReferenceFormat, r), false case r == '&': return htmlAttrEscAmp, false case r == '"': @@ -649,7 +784,7 @@ func (d *htmlDumper) attributeRuneEscape(r rune, isURI, noEntityEnc bool) (strin return htmlAttrEscLt, false case r == '>' && !isURI: return htmlAttrEscGt, false - case r >= 0x80 && !noEntityEnc: + case r >= utf8.RuneSelf && !noEntityEnc: return d.lookupEntityByRune(r), true default: return "", false @@ -724,8 +859,8 @@ func (d *htmlDumper) dumpElement(out io.Writer, e *xdm.Element) error { // Children if d.metadataHead(e) { - d.writeString(out, ``) } @@ -735,7 +870,7 @@ func (d *htmlDumper) dumpElement(out io.Writer, e *xdm.Element) error { d.doeActive = false for child := range xdm.Children(e) { - if meta, ok := child.(*xdm.Element); ok && d.metadataHead(e) && d.isHTMLElement(meta) && EqualFoldASCII(meta.LocalName(), "meta") && isContentTypeMeta(meta) { + if meta, ok := child.(*xdm.Element); ok && d.metadataHead(e) && d.isHTMLElement(meta) && EqualFoldASCII(meta.LocalName(), elemMeta) && isContentTypeMeta(meta) { continue } @@ -770,204 +905,7 @@ func (d *htmlDumper) dumpElement(out io.Writer, e *xdm.Element) error { } func (d *htmlDumper) rawTextElement(element *xdm.Element) bool { - return d.isHTMLElement(element) && (EqualFoldASCII(element.LocalName(), nameScript) || EqualFoldASCII(element.LocalName(), "style")) -} - -// isUnsafeNameRune reports whether r may not appear in a serialized HTML -// element or attribute name because it would terminate or escape the tag. -// The set mirrors the characters that terminate an attribute name in the -// HTML parseOptions (see parseOptions.parseAttrName) plus ASCII control characters. -// '&' is intentionally excluded: the parseOptions's liberal attribute-name rule -// accepts it and it does not break out of the tag, so rejecting it would -// regress parse/serialize round-trip parity. A validly-encoded U+FFFD is -// likewise accepted; only invalid UTF-8 is rejected, by the caller. -// -// This is the loose rule used for ATTRIBUTE names. Element names use the -// stricter checkElementName / isElementNameRune grammar instead. -func isUnsafeNameRune(r rune) bool { - switch r { - case ' ', '\t', '\n', '\r', '\f', '"', '\'', '<', '>', '=', '/': - return true - } - - return r < 0x20 || r == 0x7f -} - -// isElementNameRune reports whether r may appear in a serialized HTML element -// name. ASCII runes must be a valid HTML tag-name character (letters, digits, -// ':', '-', '_', '.'), mirroring the parseOptions's isNameChar; this rejects -// tag-breaking and clearly-malformed characters such as '?', '&', '=', '/', -// quotes, angle brackets, whitespace and control characters. Non-ASCII runes -// are permitted to avoid over-rejecting legitimate Unicode element names. -func isElementNameRune(r rune) bool { - if r >= 0x80 { - return true - } - - switch { - case r >= 'A' && r <= 'Z': - return true - case r >= 'a' && r <= 'z': - return true - case r >= '0' && r <= '9': - return true - } - - switch r { - case ':', '-', '_', '.': - return true - } - - return false -} - -// writeHTMLCharMapped writes s to w, substituting each character-map rune with -// its literal (unescaped) replacement string. With a nil/empty map it writes s -// unchanged. -func writeHTMLCharMapped(w io.Writer, s string, charMap map[rune]string) error { - if len(charMap) == 0 { - _, err := io.WriteString(w, s) - return err - } - - last := 0 - - for i := 0; i < len(s); { - r, width := utf8.DecodeRuneInString(s[i:]) - if repl, ok := charMap[r]; ok { - if _, err := io.WriteString(w, s[last:i]); err != nil { - return err - } - - if _, err := io.WriteString(w, repl); err != nil { - return err - } - - i += width - last = i - - continue - } - - i += width - } - - _, err := io.WriteString(w, s[last:]) - - return err -} - -func flushHTMLAttributeAmp(w io.Writer, next string, pending *bool) error { - if pending == nil || !*pending || next == "" { - return nil - } - - *pending = false - - replacement := htmlAttrEscAmp - if next[0] == '{' { - replacement = "&" - } - - written, err := io.WriteString(w, replacement) - if err == nil && written != len(replacement) { - return io.ErrShortWrite - } - - return err -} - -func writeHTMLAttributeReplacement(w io.Writer, prefix, replacement string, entity bool) error { - if _, err := io.WriteString(w, prefix); err != nil { - return err - } - - if entity { - if _, err := io.WriteString(w, "&"); err != nil { - return err - } - } - - if _, err := io.WriteString(w, replacement); err != nil { - return err - } - - if entity { - if _, err := io.WriteString(w, ";"); err != nil { - return err - } - } - - return nil -} - -// Format newline helper functions matching libxml2's HTMLtree.c:968-1108. - -func parentNameStartsWithP(n xdm.Node) bool { - parent := n.Parent() - if parent == nil || parent.Type() != xdm.ElementNode { - return false - } - - name := LowerASCII(parent.Name()) - - return len(name) > 0 && name[0] == 'p' -} - -func isTextOrEntityRef(n xdm.Node) bool { - if n == nil { - return false - } - - t := n.Type() - - return t == xdm.TextNode -} - -func shouldNewlineInside(e *xdm.Element, info *htmlElemDesc, boundary xdm.Node) bool { - if info != nil && info.inline != 0 { - return false - } - - if boundary == nil { - return false - } - - if isTextOrEntityRef(boundary) { - return false - } - - if e.FirstChild() == e.LastChild() { - return false - } - - name := LowerASCII(e.Name()) - if len(name) > 0 && name[0] == 'p' { - return false - } - - return true -} - -func shouldNewlineAfterElement(e *xdm.Element, info *htmlElemDesc) bool { - if info != nil && info.inline != 0 { - return false - } - - next := e.NextSibling() - if next == nil { - return false - } - - if isTextOrEntityRef(next) { - return false - } - - if parentNameStartsWithP(e) { - return false - } - - return true + return d.isHTMLElement(element) && (EqualFoldASCII(element.LocalName(), nameScript) || EqualFoldASCII(element.LocalName(), nameStyle)) } func (d *htmlDumper) foreign(e *xdm.Element) bool { @@ -1011,7 +949,7 @@ func (d *htmlDumper) metadataHead(e *xdm.Element) bool { func (d *htmlDumper) metadataHeadElement(n xdm.Node) bool { e, ok := n.(*xdm.Element) - return ok && d.isHTMLElement(e) && EqualFoldASCII(e.LocalName(), "head") + return ok && d.isHTMLElement(e) && EqualFoldASCII(e.LocalName(), elemHead) } // Namespace suppression and scope tracking share the same HTML5 rules. @@ -1127,18 +1065,18 @@ func (d *htmlDumper) dumpAttributes(out io.Writer, e *xdm.Element) error { // URI-escaped value is already ASCII (percent-encoded), so normalization // is a no-op there and character mapping is skipped. switch { - case d.rawTextScope && d.serializationVersion != 20 && uriEscaped: + case d.rawTextScope && d.serializationVersion != serializationEdition20 && uriEscaped: // URI percent encoding still precedes the HTML character // expansion exception for descendants of script/style. - d.check(writeURIAttributeMode(d.ctx, out, val, true, true)) - case d.rawTextScope && d.serializationVersion != 20: + d.check(writeURIAttributeMode(d.ctx, out, val, htmlAttributeMode, rawURIMode)) + case d.rawTextScope && d.serializationVersion != serializationEdition20: // REC30/31 7.1 extends the raw-text exception to attributes // of descendant elements; REC20 only specifies their text. - d.check(d.dumpTextContent(out, val, true)) + d.check(d.dumpTextContent(out, val, rawTextMode)) case !d.isHTMLElement(e) || attr.URI() != "": d.check(d.dumpXMLCharacters(out, val, tree.XMLAttributeValue)) case uriEscaped: - d.check(writeURIAttribute(d.ctx, out, val, true)) + d.check(writeURIAttribute(d.ctx, out, val, htmlAttributeMode)) case d.normalize: d.writeNormalizedAttrValue(out, val, isURI) default: @@ -1157,31 +1095,34 @@ func (d *htmlDumper) writeNSDecl(out io.Writer, prefix, uri string) { // implicitly to the XML namespace everywhere), so a literal // xmlns:xml="http://www.w3.org/XML/1998/namespace" declaration is // redundant and non-canonical and must never be emitted. - if prefix == "xml" { + if prefix == lexicon.PrefixXML { return } if prefix == "" { - d.writeString(out, " xmlns=\"") - d.writeString(out, uri) - d.writeString(out, "\"") - - return + d.writeString(out, defaultNamespaceStart) + } else { + // Prefixes form part of the attribute name and must be safe markup. + d.checkName("namespace prefix", prefix) + d.writeString(out, prefixedNamespaceStart) + d.writeString(out, prefix) + d.writeString(out, "=\"") } - // A non-empty prefix is written verbatim into the xmlns: attribute name, - // so an unsafe prefix could break out of the tag (e.g. a prefix - // containing a space and quote yields a separate injected attribute). - // Reject it the same way element/attribute names are rejected. - d.checkName("namespace prefix", prefix) if d.err != nil { return } - d.writeString(out, " xmlns:") - d.writeString(out, prefix) - d.writeString(out, "=\"") - d.writeString(out, uri) + // Namespace identity must survive XML attribute normalization. Character + // maps and Unicode normalization apply to content, not namespace URIs. + options := tree.XMLWriteOptions{CanEncode: d.canEncode, EscapeControlChars: xmlEscapeControlCharacters} + + _, err := tree.WriteXMLCharacters(d.ctx, out, uri, tree.XMLAttributeValue, math.MaxInt, options) + if errors.Is(err, tree.ErrMalformed) { + err = &xdm.Error{Code: lexicon.ErrSERE0006, Message: "character is not permitted in an XML 1.0 namespace URI", Cause: err} + } + + d.check(err) d.writeString(out, "\"") } @@ -1200,7 +1141,7 @@ func (d *htmlDumper) emitAttrNSDecls(out io.Writer, e *xdm.Element) { for _, attr := range e.Attributes() { prefix := attr.Prefix() - if prefix == "" || prefix == "xml" { + if prefix == "" || prefix == lexicon.PrefixXML { continue } @@ -1226,6 +1167,195 @@ func (d *htmlDumper) emitAttrNSDecls(out io.Writer, e *xdm.Element) { } } +// isUnsafeNameRune reports whether r may not appear in a serialized HTML +// element or attribute name because it would terminate or escape the tag. +// The set mirrors the characters that terminate an attribute name in the +// HTML parseOptions (see parseOptions.parseAttrName) plus ASCII control characters. +// '&' is intentionally excluded: the parseOptions's liberal attribute-name rule +// accepts it and it does not break out of the tag, so rejecting it would +// regress parse/serialize round-trip parity. A validly-encoded U+FFFD is +// likewise accepted; only invalid UTF-8 is rejected, by the caller. +// +// This is the loose rule used for ATTRIBUTE names. Element names use the +// stricter checkElementName / isElementNameRune grammar instead. +func isUnsafeNameRune(r rune) bool { + switch r { + case ' ', '\t', '\n', '\r', '\f', '"', '\'', '<', '>', '=', '/': + return true + } + + return r < minimumNameRune || r == asciiDelete +} + +// isElementNameRune reports whether r may appear in a serialized HTML element +// name. ASCII runes must be a valid HTML tag-name character (letters, digits, +// ':', '-', '_', '.'), mirroring the parseOptions's isNameChar; this rejects +// tag-breaking and clearly-malformed characters such as '?', '&', '=', '/', +// quotes, angle brackets, whitespace and control characters. Non-ASCII runes +// are permitted to avoid over-rejecting legitimate Unicode element names. +func isElementNameRune(r rune) bool { + if r >= utf8.RuneSelf { + return true + } + + switch { + case r >= 'A' && r <= 'Z': + return true + case r >= 'a' && r <= 'z': + return true + case r >= '0' && r <= '9': + return true + } + + switch r { + case ':', '-', '_', '.': + return true + } + + return false +} + +// writeHTMLCharMapped writes s to w, substituting each character-map rune with +// its literal (unescaped) replacement string. With a nil/empty map it writes s +// unchanged. +func writeHTMLCharMapped(w io.Writer, s string, charMap map[rune]string) error { + if len(charMap) == 0 { + return writeHTMLString(w, s) + } + + last := 0 + + for i := 0; i < len(s); { + r, width := utf8.DecodeRuneInString(s[i:]) + if repl, ok := charMap[r]; ok { + if err := writeHTMLString(w, s[last:i]); err != nil { + return err + } + + if err := writeHTMLString(w, repl); err != nil { + return err + } + + i += width + last = i + + continue + } + + i += width + } + + return writeHTMLString(w, s[last:]) +} + +func flushHTMLAttributeAmp(w io.Writer, next string, pending *bool) error { + if pending == nil || !*pending || next == "" { + return nil + } + + *pending = false + + replacement := htmlAttrEscAmp + if next[0] == '{' { + replacement = "&" + } + + return writeHTMLString(w, replacement) +} + +func writeHTMLAttributeReplacement(w io.Writer, prefix, replacement string, entity bool) error { + if err := writeHTMLString(w, prefix); err != nil { + return err + } + + if entity { + if err := writeHTMLString(w, "&"); err != nil { + return err + } + } + + if err := writeHTMLString(w, replacement); err != nil { + return err + } + + if entity { + if err := writeHTMLString(w, ";"); err != nil { + return err + } + } + + return nil +} + +// Format newline helper functions matching libxml2's HTMLtree.c:968-1108. + +func parentNameStartsWithP(n xdm.Node) bool { + parent := n.Parent() + if parent == nil || parent.Type() != xdm.ElementNode { + return false + } + + name := LowerASCII(parent.Name()) + + return len(name) > 0 && name[0] == paragraphNamePrefix +} + +func isTextOrEntityRef(n xdm.Node) bool { + if n == nil { + return false + } + + t := n.Type() + + return t == xdm.TextNode +} + +func shouldNewlineInside(e *xdm.Element, info *htmlElemDesc, boundary xdm.Node) bool { + if info != nil && info.inline != blockLayout { + return false + } + + if boundary == nil { + return false + } + + if isTextOrEntityRef(boundary) { + return false + } + + if e.FirstChild() == e.LastChild() { + return false + } + + name := LowerASCII(e.Name()) + if len(name) > 0 && name[0] == paragraphNamePrefix { + return false + } + + return true +} + +func shouldNewlineAfterElement(e *xdm.Element, info *htmlElemDesc) bool { + if info != nil && info.inline != blockLayout { + return false + } + + next := e.NextSibling() + if next == nil { + return false + } + + if isTextOrEntityRef(next) { + return false + } + + if parentNameStartsWithP(e) { + return false + } + + return true +} + // copyMap returns a shallow copy of a string map. If src is nil, returns // an empty map. func copyMap(src map[string]string) map[string]string { @@ -1243,11 +1373,11 @@ func isURIAttr(attribute, element string, version int) bool { // Serialization 2.0 Appendix C has 37 element/attribute cells. The eleven // additions in Serialization 3.0/3.1 Appendix D are not URI-valued under // the earlier contract, even when a host chooses an HTML version later. - if version > 0 && version <= 20 { + if version > defaultSerializationEdition && version <= serializationEdition20 { switch attribute { - case "formaction", "icon", "manifest", "poster", "value": + case attrFormAction, attrIcon, attrManifest, attrPoster, lexicon.AttrValue: return false - case "src": + case lexicon.AttrSrc: switch element { case elemAudio, elemEmbed, elemSource, elemTrack, elemVideo: return false @@ -1258,41 +1388,41 @@ func isURIAttr(attribute, element string, version int) bool { var elements string switch attribute { - case "action": - elements = "form" - case "archive", "classid", elemData: + case attrAction: + elements = elemForm + case attrArchive, attrClassID, attrData: elements = elemObject - case "background": - elements = "body" - case "cite": - elements = "blockquote del ins q" - case "codebase": - elements = "applet object" - case "datasrc": - elements = "button div input object select span table textarea" - case "for": + case attrBackground: + elements = elemBody + case attrCite: + elements = citationElements + case attrCodebase: + elements = uriCodebaseElements + case attrDataSrc: + elements = uriDataSourceElements + case attrFor: elements = nameScript - case "formaction": - elements = "button input" - case "href": - elements = "a area base link" - case "icon": - elements = "command" - case "longdesc": - elements = "frame iframe img" - case "manifest": - elements = "html" - case "name": - elements = "a" - case "poster": + case attrFormAction: + elements = formSubmitElements + case lexicon.AttrHref: + elements = uriHrefElements + case attrIcon: + elements = elemCommand + case attrLongDesc: + elements = longDescriptionElements + case attrManifest: + elements = elemHTML + case lexicon.AttrName: + elements = elemA + case attrPoster: elements = elemVideo - case "profile": - elements = "head" - case "src": - elements = "audio embed frame iframe img input script source track video" - case "usemap": - elements = "img input object" - case "value": + case attrProfile: + elements = elemHead + case lexicon.AttrSrc: + elements = uriSourceElements + case attrUseMap: + elements = useMapElements + case lexicon.AttrValue: elements = elemInput } @@ -1305,8 +1435,7 @@ func isURIAttr(attribute, element string, version int) bool { return false } -// isHTMLBooleanAttribute reports attributes minimized when their value matches -// their name (XSLT/XQuery Serialization 3.1, HTML boolean attributes). +// stringsContainsWord tests membership in a whitespace-separated word list. func stringsContainsWord(words, word string) bool { for candidate := range strings.FieldsSeq(words) { if candidate == word { @@ -1317,48 +1446,50 @@ func stringsContainsWord(words, word string) bool { return false } +// isHTMLBooleanAttribute checks the element/attribute pair against the boolean +// attribute rules for the selected serialization edition. func isHTMLBooleanAttribute(attribute, element string, version int) bool { if stringsContainsWord(legacyBooleanAttributeElements(attribute), element) { return true } - if version > 0 && version <= 20 { + if version > defaultSerializationEdition && version <= serializationEdition20 { return false } elements := modernBooleanAttributeElements(attribute) - return elements == "*" || stringsContainsWord(elements, element) + return elements == allHTMLElements || stringsContainsWord(elements, element) } func legacyBooleanAttributeElements(attribute string) string { switch attribute { - case "checked": - return "input" - case "compact": - return "dir dl menu ol ul" - case "declare": + case attrChecked: + return elemInput + case attrCompact: + return compactElements + case attrDeclare: return elemObject - case "defer": + case attrDefer: return nameScript - case "disabled": - return "button input optgroup option select textarea" - case "ismap": + case attrDisabled: + return legacyDisabledElements + case attrIsMap: return elemImg - case "multiple": - return "select" - case "nohref": + case attrMultiple: + return elemSelect + case attrNoHref: return elemArea - case "noresize": + case attrNoResize: return elemFrame - case "noshade": - return "hr" - case "nowrap": - return "td th" - case "readonly": - return "input textarea" - case "selected": - return "option" + case attrNoShade: + return elemHr + case attrNoWrap: + return noWrapElements + case attrReadOnly: + return readOnlyElements + case attrSelected: + return nameOption default: return "" } @@ -1368,35 +1499,35 @@ func legacyBooleanAttributeElements(attribute string) string { // one-value DTD attributes. These cells do not change the SER20 contract. func modernBooleanAttributeElements(attribute string) string { switch attribute { - case "async", "defer": + case attrAsync, attrDefer: return nameScript - case "autofocus": - return "button input keygen select textarea" - case "autoplay", "controls", "loop", "muted": - return "audio video" - case "default": - return "track" - case "disabled": - return "button fieldset input keygen optgroup option select textarea" - case "formnovalidate": - return "button input" - case "hidden": - return "*" - case "multiple": - return "input select" - case "novalidate": - return "form" - case "open": - return "details dialog" - case "required": - return "input select textarea" - case "reversed": - return "ol" - case "scoped": - return "style" - case "seamless": - return "iframe" - case "typemustmatch": + case attrAutofocus: + return autofocusElements + case attrAutoplay, attrControls, attrLoop, attrMuted: + return mediaPlaybackElements + case attrDefault: + return elemTrack + case attrDisabled: + return modernDisabledElements + case attrFormNoValidate: + return formSubmitElements + case attrHidden: + return allHTMLElements + case attrMultiple: + return multipleElements + case attrNoValidate: + return elemForm + case attrOpen: + return openElements + case attrRequired: + return requiredElements + case attrReversed: + return elemOl + case attrScoped: + return nameStyle + case attrSeamless: + return elemIframe + case attrTypeMustMatch: return elemObject default: return "" @@ -1407,7 +1538,7 @@ func modernBooleanAttributeElements(attribute string) string { // escaping ampersands for the surrounding HTML attribute. Output scratch is // capped; normalization may retain an incomplete combining sequence. func writeURIAttribute(ctx context.Context, out io.Writer, s string, htmlAttribute bool) error { - return writeURIAttributeMode(ctx, out, s, htmlAttribute, false) + return writeURIAttributeMode(ctx, out, s, htmlAttribute, encodedURIMode) } func writeURIAttributeMode(ctx context.Context, out io.Writer, s string, htmlAttribute, raw bool) error { @@ -1417,7 +1548,7 @@ func writeURIAttributeMode(ctx context.Context, out io.Writer, s string, htmlAtt const hex = "0123456789ABCDEF" - buffer := make([]byte, min(4096, max(64, len(s)))) + buffer := make([]byte, min(maxURIScratchBytes, max(minURIScratchBytes, len(s)))) n := 0 flush := func() error { @@ -1435,7 +1566,7 @@ func writeURIAttributeMode(ctx context.Context, out io.Writer, s string, htmlAtt if err := norm.NFC.EachBytes(ctx, s, func(part []byte) error { for _, c := range part { - if n+12 > len(buffer) { + if n+uriScratchReserveBytes > len(buffer) { if err := flush(); err != nil { return err } @@ -1453,7 +1584,7 @@ func writeURIAttributeMode(ctx context.Context, out io.Writer, s string, htmlAtt } switch { - case raw && c >= 0x20 && c <= 0x7e: + case raw && c >= uriPrintableASCIIStart && c <= uriPrintableASCIIEnd: buffer[n] = c n++ case c == '&' && htmlAttribute: @@ -1464,7 +1595,7 @@ func writeURIAttributeMode(ctx context.Context, out io.Writer, s string, htmlAtt n += copy(buffer[n:], htmlAttrEscQuot) case c == '<' && !htmlAttribute: n += copy(buffer[n:], htmlAttrEscLt) - case c >= 0x20 && c <= 0x7e: + case c >= uriPrintableASCIIStart && c <= uriPrintableASCIIEnd: buffer[n] = c n++ default: @@ -1496,11 +1627,11 @@ func htmlEscapeString(w io.Writer, s string, escCtrl bool, charMap map[rune]stri i += width if repl, ok := charMap[r]; ok { // Character map: emit the literal replacement verbatim (not escaped). - if _, err := io.WriteString(w, s[last:i-width]); err != nil { + if err := writeHTMLString(w, s[last:i-width]); err != nil { return err } - if _, err := io.WriteString(w, repl); err != nil { + if err := writeHTMLString(w, repl); err != nil { return err } @@ -1516,13 +1647,13 @@ func htmlEscapeString(w io.Writer, s string, escCtrl bool, charMap map[rune]stri esc = htmlAttrEscLt case r == '>': esc = htmlAttrEscGt - case escCtrl && r >= 0x7F && r <= 0x9F || r > 127 && canEncode != nil && !canEncode(r): - if _, err := io.WriteString(w, s[last:i-width]); err != nil { + case escCtrl && r >= asciiDelete && r <= escapeControlEnd || r > maxASCIIRune && canEncode != nil && !canEncode(r): + if err := writeHTMLString(w, s[last:i-width]); err != nil { return err } - ref := fmt.Sprintf("&#x%X;", r) - if _, err := io.WriteString(w, ref); err != nil { + ref := fmt.Sprintf(hexCharacterReferenceFormat, r) + if err := writeHTMLString(w, ref); err != nil { return err } @@ -1533,18 +1664,18 @@ func htmlEscapeString(w io.Writer, s string, escCtrl bool, charMap map[rune]stri continue } - if _, err := io.WriteString(w, s[last:i-width]); err != nil { + if err := writeHTMLString(w, s[last:i-width]); err != nil { return err } - if _, err := io.WriteString(w, esc); err != nil { + if err := writeHTMLString(w, esc); err != nil { return err } last = i } - if _, err := io.WriteString(w, s[last:]); err != nil { + if err := writeHTMLString(w, s[last:]); err != nil { return err } @@ -1553,7 +1684,7 @@ func htmlEscapeString(w io.Writer, s string, escCtrl bool, charMap map[rune]stri func isContentTypeMeta(e *xdm.Element) bool { for _, a := range e.Attributes() { - if a.URI() == "" && EqualFoldASCII(a.Name(), "http-equiv") && EqualFoldASCII(strings.Trim(a.Value(), " \t\r\n"), "Content-Type") { + if a.URI() == "" && EqualFoldASCII(a.Name(), attrHTTPEquiv) && EqualFoldASCII(strings.Trim(a.Value(), lexicon.XMLWhitespace), lexicon.HeaderContentType) { return true } } @@ -1565,13 +1696,13 @@ func isContentTypeMeta(e *xdm.Element) bool { // golang.org/x/text norm.Form and reports whether normalization is active. func htmlNormalizationForm(form string) (norm.Form, bool) { switch form { - case "NFC": + case lexicon.NormalizationFormNFC: return norm.NFC, true - case "NFD": + case lexicon.NormalizationFormNFD: return norm.NFD, true - case "NFKC": + case lexicon.NormalizationFormNFKC: return norm.NFKC, true - case "NFKD": + case lexicon.NormalizationFormNFKD: return norm.NFKD, true } @@ -1669,7 +1800,7 @@ func utf8ToLatin1WithAlign(data []byte, strict bool) ([]byte, []alignPoint) { allASCII := true for _, b := range data { - if b >= 0x80 { + if b >= utf8.RuneSelf { allASCII = false break } @@ -1681,12 +1812,12 @@ func utf8ToLatin1WithAlign(data []byte, strict bool) ([]byte, []alignPoint) { var buf bytes.Buffer buf.Grow(len(data)) - align := make([]alignPoint, 0, len(data)/2+1) + align := make([]alignPoint, 0, len(data)/alignmentBytesPerRuneHint+1) align = append(align, alignPoint{0, 0}) for i := 0; i < len(data); { b := data[i] - if b < 0x80 { + if b < utf8.RuneSelf { buf.WriteByte(b) i++ @@ -1696,11 +1827,11 @@ func utf8ToLatin1WithAlign(data []byte, strict bool) ([]byte, []alignPoint) { } r, size := utf8.DecodeRune(data[i:]) - if r >= 0x80 && r <= 0xFF { + if r >= utf8.RuneSelf && r <= unicode.MaxLatin1 { buf.WriteByte(byte(r)) } else if strict { ref := append(buf.AvailableBuffer(), '&', '#') - ref = strconv.AppendInt(ref, int64(r), 10) + ref = strconv.AppendInt(ref, int64(r), decimalCharRefRadix) buf.Write(append(ref, ';')) } else if wb, ok := windows1252Byte(r); ok { buf.WriteByte(wb) diff --git a/transform/internal/xdm/html/dump_test.go b/transform/internal/xdm/html/dump_test.go index 6c72098..65c2977 100644 --- a/transform/internal/xdm/html/dump_test.go +++ b/transform/internal/xdm/html/dump_test.go @@ -2,6 +2,7 @@ package html_test import ( "bytes" + "encoding/xml" "errors" "io" "strconv" @@ -94,10 +95,6 @@ func TestHTMLLatin1CharacterReferences(t *testing.T) { } } -type errorWriter struct{ err error } - -func (w errorWriter) Write([]byte) (int, error) { return 0, w.err } - func TestXMLIslandCharacterErrors(t *testing.T) { t.Parallel() @@ -252,3 +249,125 @@ func BenchmarkHTMLURIAttributeSize(b *testing.B) { }) } } + +type errorWriter struct{ err error } + +func (w errorWriter) Write([]byte) (int, error) { return 0, w.err } + +func TestHTMLNamespaceIdentityUsesXMLAttributeEscaping(t *testing.T) { + t.Parallel() + + const uri = "urn:e\u0301&raw\"<\t\n\r" + for _, prefix := range []string{"", "p"} { + doc := xdm.NewDefaultDocument() + root, err := doc.CreateElementNS("r", xdm.NewNamespace(prefix, uri)) + if err != nil { + t.Fatal(err) + } + if err := root.DeclareNamespace(prefix, uri); err != nil { + t.Fatal(err) + } + if err := doc.AddChild(root); err != nil { + t.Fatal(err) + } + var output bytes.Buffer + writer := html.NewWriter().DefaultDTD(false).Format(false).PreserveCase(true). + Normalization("NFC").CharacterMap(map[rune]string{'e': "CHANGED", '&': "MAPPED"}) + if err := writer.WriteTo(&output, doc); err != nil { + t.Fatal(err) + } + token, err := xml.NewDecoder(bytes.NewReader(output.Bytes())).Token() + if err != nil { + t.Fatalf("namespace declaration is not XML-safe: %q, %v", output.String(), err) + } + start, ok := token.(xml.StartElement) + if !ok || start.Name.Space != uri { + t.Fatalf("namespace identity changed: %q; token=%+v", output.String(), token) + } + } +} + +type selectedShortWriter struct { + bytes.Buffer + target string + failure error + failed bool + afterFailure int +} + +func (w *selectedShortWriter) WriteString(s string) (int, error) { return w.Write([]byte(s)) } + +func (w *selectedShortWriter) Write(p []byte) (int, error) { + if w.failed { + w.afterFailure++ + } + if string(p) == w.target { + w.failed = true + n, _ := w.Buffer.Write(p[:len(p)-1]) + return n, w.failure + } + return w.Buffer.Write(p) +} + +func TestHTMLEscapingReportsShortWritesWithoutContinuing(t *testing.T) { + t.Parallel() + + parse := func(source string) *xdm.Document { + t.Helper() + doc, err := (xdm.ParseOptions{}).Parse(t.Context(), []byte(source)) + if err != nil { + t.Fatal(err) + } + return doc + } + base := html.NewWriter().DefaultDTD(false).Format(false).PreserveCase(true) + mapped := base.CharacterMap(map[rune]string{'|': "mapped"}) + normalized := base.Normalization("NFC") + failure := errors.New("selected write failed") + for _, test := range []struct { + name, target string + node xdm.Node + writer html.Writer + }{ + {"plain text", "payload", xdm.NewDefaultDocument().CreateText("payload"), base}, + {"escaped text", "&", xdm.NewDefaultDocument().CreateText("a&b"), base}, + {"mapped text", "mapped", xdm.NewDefaultDocument().CreateText("a|b"), mapped}, + {"raw mapped text", "mapped", parse(``), mapped}, + {"plain attribute", "payload", parse(``), base}, + {"escaped attribute", "&", parse(``), base}, + {"mapped attribute", "mapped", parse(``), mapped}, + {"numeric reference", "é", xdm.NewDefaultDocument().CreateText("é"), base.Repertoire(func(r rune) bool { return r < 128 })}, + {"named entity", "&", parse(``), base.PreserveCase(false)}, + {"normalized text", "é", xdm.NewDefaultDocument().CreateText("e\u0301"), normalized}, + {"normalized attribute", "é", parse(``), normalized}, + } { + t.Run(test.name, func(t *testing.T) { + t.Parallel() + + for _, stream := range []bool{false, true} { + for _, destinationError := range []error{nil, failure} { + out := &selectedShortWriter{target: test.target, failure: destinationError} + var err error + if stream { + err = test.writer.WriteSequence(t.Context(), out, func(yield func(xdm.XMLItem, error) bool) { + if test.node.Type() == xdm.TextNode { + yield(xdm.XMLItem{Text: xdm.CharacterData(test.node)}, nil) + } else { + yield(xdm.XMLItem{Node: test.node}, nil) + } + }) + } else { + err = test.writer.WriteTo(out, test.node) + } + want := destinationError + if want == nil { + want = io.ErrShortWrite + } + if !out.failed || !errors.Is(err, want) || out.afterFailure != 0 { + t.Fatalf("stream=%v: error=%v; want %v, failed=%v, calls after failure=%d", stream, err, want, out.failed, out.afterFailure) + } + } + } + }) + } +} diff --git a/transform/internal/xdm/html/elements.go b/transform/internal/xdm/html/elements.go index aa1c41a..eeb8d30 100644 --- a/transform/internal/xdm/html/elements.go +++ b/transform/internal/xdm/html/elements.go @@ -5,9 +5,9 @@ package html import "strings" -// HTML element name constants used throughout the html package. -// Extracted to satisfy the goconst linter; these names appear many times -// in the element tables and parseOptions logic below. +// Exact HTML element spellings are shared by descriptor lookup, HTML/XHTML +// serialization rules and attribute-context tables. Recognition remains ASCII +// case-insensitive at the existing call sites. const ( elemBasefont = "basefont" elemIsindex = "isindex" @@ -49,10 +49,111 @@ const ( elemXmp = "xmp" ) -// HTML5 named character entity keys that collide with element-name literals. -// These are entity names (e.g. ÷ == U+00F7), not HTML element references, -// so they are kept distinct from the elem* constants. -const () +// Descriptor layout codes preserve the libxml2 table distinctions. Both +// nonzero codes suppress automatic formatting line breaks. Empty-element +// metadata selects whether this table treats a tag as having no content. +const ( + blockLayout = 0 + inlineLayout = 1 + mixedInlineLayout = 2 + emptyElement = true + pairedElement = false +) + +// Additional element vocabulary follows the pinned HTML4/HTML5 descriptor +// and XHTML classification tables; these names do not add table membership. +const ( + elemA = "a" + elemAbbr = "abbr" + elemAcronym = "acronym" + elemApplet = "applet" + elemArticle = "article" + elemAside = "aside" + elemB = "b" + elemBase = "base" + elemBdi = "bdi" + elemBdo = "bdo" + elemBgsound = "bgsound" + elemBr = "br" + elemButton = "button" + elemCanvas = "canvas" + elemCite = "cite" + elemCode = "code" + elemCommand = "command" + elemDatalist = "datalist" + elemDd = "dd" + elemDel = "del" + elemDetails = "details" + elemDfn = "dfn" + elemDialog = "dialog" + elemDl = "dl" + elemDt = "dt" + elemEm = "em" + elemFigcaption = "figcaption" + elemFigure = "figure" + elemFooter = "footer" + elemH1 = "h1" + elemH2 = "h2" + elemH3 = "h3" + elemH4 = "h4" + elemH5 = "h5" + elemH6 = "h6" + elemHeader = "header" + elemHgroup = "hgroup" + elemHr = "hr" + elemI = "i" + elemIframe = "iframe" + elemIns = "ins" + elemKbd = "kbd" + elemLabel = "label" + elemLegend = "legend" + elemLi = "li" + elemMain = "main" + elemMap = "map" + elemMark = "mark" + elemMath = "math" + elemMenuitem = "menuitem" + elemMeta = "meta" + elemMeter = "meter" + elemNav = "nav" + elemNobr = "nobr" + elemNoembed = "noembed" + elemNoframes = "noframes" + elemNoscript = "noscript" + elemOl = "ol" + elemOptgroup = "optgroup" + elemOutput = "output" + elemP = "p" + elemParam = "param" + elemPicture = "picture" + elemPlaintext = "plaintext" + elemProgress = "progress" + elemQ = "q" + elemRb = "rb" + elemRp = "rp" + elemRt = "rt" + elemRtc = "rtc" + elemRuby = "ruby" + elemS = "s" + elemSamp = "samp" + elemSection = "section" + elemSelect = "select" + elemSlot = "slot" + elemStrong = "strong" + elemSummary = "summary" + elemSup = "sup" + elemSVG = "svg" + elemTd = "td" + elemTemplate = "template" + elemTextarea = "textarea" + elemTh = "th" + elemTime = "time" + elemTr = "tr" + elemTt = "tt" + elemU = "u" + elemUl = "ul" + elemVar = "var" +) // LowerASCII folds HTML names without changing non-ASCII code points. Unicode // lowercasing would turn U+0130 into i and falsely recognize an HTML element. @@ -127,10 +228,10 @@ type htmlElemDesc struct { // retains basefont, frame and isindex, an explicitly permitted choice. func SerializationVoidElement(name string, html5 bool, edition int) bool { switch LowerASCII(name) { - case elemArea, "base", "br", elemCol, "hr", elemImg, elemInput, nameLink, "meta", "param", elemBasefont, elemFrame, elemIsindex: + case elemArea, elemBase, elemBr, elemCol, elemHr, elemImg, elemInput, nameLink, elemMeta, elemParam, elemBasefont, elemFrame, elemIsindex: return true case elemEmbed: - return html5 || edition != 20 + return html5 || edition != serializationEdition20 case elemKeygen, elemSource, elemTrack, elemWbr: return html5 default: @@ -144,62 +245,62 @@ func SerializationVoidElement(name string, html5 bool, edition int) bool { func lookupElement(name string) *htmlElemDesc { name = LowerASCII(name) switch name { - case "a", "abbr", "acronym", "bdo", "cite", "code", "dfn", "kbd", "label", "q", "samp", "select", nameSpan, "var": - return &htmlElemDesc{empty: false, inline: 1, dataMode: dataNormal} - case elemAddress, nameBlockquote, elemCaption, elemDiv, "dl", elemFieldset, elemForm, "h1", "h2", "h3", "h4", "h5", "h6", "legend", "noscript", "ol", "optgroup", elemPre, elemTable, "td", "tr", "ul": - return &htmlElemDesc{empty: false, inline: 0, dataMode: dataNormal} - case "applet": - return &htmlElemDesc{empty: false, inline: 2, dataMode: dataNormal} - case elemArea, "base", elemCol, "hr", nameLink, "meta", "param": - return &htmlElemDesc{empty: true, inline: 0, dataMode: dataNormal} - case "b", nameBig, "em", "i", "nobr", nameSmall, "strong", nameSub, "sup", "tt": - return &htmlElemDesc{empty: false, inline: 1, dataMode: dataNormal} + case elemA, elemAbbr, elemAcronym, elemBdo, elemCite, elemCode, elemDfn, elemKbd, elemLabel, elemQ, elemSamp, elemSelect, nameSpan, elemVar: + return &htmlElemDesc{empty: pairedElement, inline: inlineLayout, dataMode: dataNormal} + case elemAddress, nameBlockquote, elemCaption, elemDiv, elemDl, elemFieldset, elemForm, elemH1, elemH2, elemH3, elemH4, elemH5, elemH6, elemLegend, elemNoscript, elemOl, elemOptgroup, elemPre, elemTable, elemTd, elemTr, elemUl: + return &htmlElemDesc{empty: pairedElement, inline: blockLayout, dataMode: dataNormal} + case elemApplet: + return &htmlElemDesc{empty: pairedElement, inline: mixedInlineLayout, dataMode: dataNormal} + case elemArea, elemBase, elemCol, elemHr, nameLink, elemMeta, elemParam: + return &htmlElemDesc{empty: emptyElement, inline: blockLayout, dataMode: dataNormal} + case elemB, nameBig, elemEm, elemI, elemNobr, nameSmall, elemStrong, nameSub, elemSup, elemTt: + return &htmlElemDesc{empty: pairedElement, inline: inlineLayout, dataMode: dataNormal} case elemBasefont: - return &htmlElemDesc{empty: true, inline: 1, dataMode: dataNormal} - case "bgsound", elemKeygen, elemSource, elemTrack, elemWbr: - return &htmlElemDesc{empty: true, inline: 0, dataMode: dataNormal} + return &htmlElemDesc{empty: emptyElement, inline: inlineLayout, dataMode: dataNormal} + case elemBgsound, elemKeygen, elemSource, elemTrack, elemWbr: + return &htmlElemDesc{empty: emptyElement, inline: blockLayout, dataMode: dataNormal} case elemBody, elemHead, elemHTML: - return &htmlElemDesc{empty: false, inline: 0, dataMode: dataNormal} - case "br", elemImg, elemInput: - return &htmlElemDesc{empty: true, inline: 1, dataMode: dataNormal} - case "button", "del", "ins", "map", elemObject: - return &htmlElemDesc{empty: false, inline: 2, dataMode: dataNormal} + return &htmlElemDesc{empty: pairedElement, inline: blockLayout, dataMode: dataNormal} + case elemBr, elemImg, elemInput: + return &htmlElemDesc{empty: emptyElement, inline: inlineLayout, dataMode: dataNormal} + case elemButton, elemDel, elemIns, elemMap, elemObject: + return &htmlElemDesc{empty: pairedElement, inline: mixedInlineLayout, dataMode: dataNormal} case elemCenter: - return &htmlElemDesc{empty: false, inline: 0, dataMode: dataNormal} - case elemColgroup, "dd", "dt", nameOption, "p", elemTfoot, "th", elemThead: - return &htmlElemDesc{empty: false, inline: 0, dataMode: dataNormal} + return &htmlElemDesc{empty: pairedElement, inline: blockLayout, dataMode: dataNormal} + case elemColgroup, elemDd, elemDt, nameOption, elemP, elemTfoot, elemTh, elemThead: + return &htmlElemDesc{empty: pairedElement, inline: blockLayout, dataMode: dataNormal} case elemDir, elemMenu: - return &htmlElemDesc{empty: false, inline: 0, dataMode: dataNormal} + return &htmlElemDesc{empty: pairedElement, inline: blockLayout, dataMode: dataNormal} case elemEmbed: - return &htmlElemDesc{empty: true, inline: 1, dataMode: dataNormal} - case elemFont, "s", nameStrike, "u": - return &htmlElemDesc{empty: false, inline: 1, dataMode: dataNormal} + return &htmlElemDesc{empty: emptyElement, inline: inlineLayout, dataMode: dataNormal} + case elemFont, elemS, nameStrike, elemU: + return &htmlElemDesc{empty: pairedElement, inline: inlineLayout, dataMode: dataNormal} case elemFrame: - return &htmlElemDesc{empty: true, inline: 0, dataMode: dataNormal} + return &htmlElemDesc{empty: emptyElement, inline: blockLayout, dataMode: dataNormal} case elemFrameset: - return &htmlElemDesc{empty: false, inline: 0, dataMode: dataNormal} - case "iframe": - return &htmlElemDesc{empty: false, inline: 2, dataMode: dataRawText} + return &htmlElemDesc{empty: pairedElement, inline: blockLayout, dataMode: dataNormal} + case elemIframe: + return &htmlElemDesc{empty: pairedElement, inline: mixedInlineLayout, dataMode: dataRawText} case elemIsindex: - return &htmlElemDesc{empty: true, inline: 0, dataMode: dataNormal} - case "li": - return &htmlElemDesc{empty: false, inline: 0, dataMode: dataNormal} - case "noembed", nameStyle: - return &htmlElemDesc{empty: false, inline: 0, dataMode: dataRawText} - case "noframes": - return &htmlElemDesc{empty: false, inline: 0, dataMode: dataRawText} - case "plaintext": - return &htmlElemDesc{empty: false, inline: 0, dataMode: dataPlaintext} + return &htmlElemDesc{empty: emptyElement, inline: blockLayout, dataMode: dataNormal} + case elemLi: + return &htmlElemDesc{empty: pairedElement, inline: blockLayout, dataMode: dataNormal} + case elemNoembed, nameStyle: + return &htmlElemDesc{empty: pairedElement, inline: blockLayout, dataMode: dataRawText} + case elemNoframes: + return &htmlElemDesc{empty: pairedElement, inline: blockLayout, dataMode: dataRawText} + case elemPlaintext: + return &htmlElemDesc{empty: pairedElement, inline: blockLayout, dataMode: dataPlaintext} case nameScript: - return &htmlElemDesc{empty: false, inline: 2, dataMode: dataScript} + return &htmlElemDesc{empty: pairedElement, inline: mixedInlineLayout, dataMode: dataScript} case elemTbody: - return &htmlElemDesc{empty: false, inline: 0, dataMode: dataNormal} - case "textarea": - return &htmlElemDesc{empty: false, inline: 1, dataMode: dataRCDATA} + return &htmlElemDesc{empty: pairedElement, inline: blockLayout, dataMode: dataNormal} + case elemTextarea: + return &htmlElemDesc{empty: pairedElement, inline: inlineLayout, dataMode: dataRCDATA} case elemTitle: - return &htmlElemDesc{empty: false, inline: 0, dataMode: dataRCDATA} + return &htmlElemDesc{empty: pairedElement, inline: blockLayout, dataMode: dataRCDATA} case elemXmp: - return &htmlElemDesc{empty: false, inline: 1, dataMode: dataRawText} + return &htmlElemDesc{empty: pairedElement, inline: inlineLayout, dataMode: dataRawText} default: return nil } diff --git a/transform/internal/xdm/html/encoding_writer_test.go b/transform/internal/xdm/html/encoding_writer_test.go index c4b03fc..6d3775b 100644 --- a/transform/internal/xdm/html/encoding_writer_test.go +++ b/transform/internal/xdm/html/encoding_writer_test.go @@ -4,7 +4,10 @@ import ( "bytes" "errors" "io" + "reflect" "testing" + + "github.com/skarm/xmlsec/transform/internal/xdm" ) type limitedEncodingWriter struct { @@ -91,3 +94,89 @@ func TestMarkupNameValidationAndStickyFailure(t *testing.T) { t.Fatal("sticky first error was replaced or output continued", d.err, output.String()) } } + +func TestMappedMalformedUTF8SourceBoundaries(t *testing.T) { + t.Parallel() + + for _, tc := range []struct { + name, source, form, want, attributeWant string + mapping map[rune]string + }{ + {"one malformed byte", "\xff", "NFC", "x", "x", map[rune]string{'\uFFFD': "x"}}, + {"two malformed bytes", "\xff\xfe", "NFC", "xx", "xx", map[rune]string{'\uFFFD': "x"}}, + {"malformed middle", "A\xffB", "NFC", "AxB", "AxB", map[rune]string{'\uFFFD': "x"}}, + {"encoded replacement", "\uFFFD", "NFC", "x", "x", map[rune]string{'\uFFFD': "x"}}, + {"without map", "\xff", "NFC", "\xff", "\xff", nil}, + {"unrelated map", "\xff", "NFC", "\xff", "\xff", map[rune]string{'Q': "y"}}, + {"without normalization", "\xff", "none", "x", "x", map[rune]string{'\uFFFD': "x"}}, + {"valid composition", "A\u030A", "NFC", "Å", "Å", map[rune]string{'\uFFFD': "x"}}, + } { + for _, attribute := range []bool{false, true} { + route := "text" + if attribute { + route = "attribute" + } + + t.Run(tc.name+"/"+route, func(t *testing.T) { + t.Parallel() + + defer func() { + if p := recover(); p != nil { + t.Errorf("serialization must not panic for a raw Go string: %v", p) + } + }() + + doc := xdm.NewDefaultDocument() + var node xdm.Node = doc.CreateText(tc.source) + want := tc.want + if attribute { + element, err := doc.CreateElement("r") + if err != nil { + t.Fatal(err) + } + + if err := element.SetAttribute("a", tc.source); err != nil { + t.Fatal(err) + } + + node, want = element, `` + } + + var output bytes.Buffer + + err := NewWriter().Format(false).CharacterMap(tc.mapping).Normalization(tc.form).WriteTo(&output, node) + if err != nil { + t.Fatal(err) + } + + if output.String() != want { + t.Fatalf("got %q, want %q", output.String(), want) + } + }) + } + } +} + +func TestMappedMalformedUTF8PreservesCallbackOrderAndError(t *testing.T) { + t.Parallel() + + sentinel := errors.New("mapped segment callback failed") + dumper := NewWriter().CharacterMap(map[rune]string{'\uFFFD': "x"}).Normalization("NFC").dumper() + var segments []string + + err := dumper.normalizedContent("A\xffB", func(segment string, mapped bool) error { + segments = append(segments, segment) + if mapped { + return sentinel + } + + return nil + }) + if !errors.Is(err, sentinel) { + t.Fatalf("got %v, want callback error", err) + } + + if !reflect.DeepEqual(segments, []string{"A", "x"}) { + t.Fatalf("got callback segments %q, want [A x]", segments) + } +} diff --git a/transform/internal/xdm/html/options.go b/transform/internal/xdm/html/options.go index 5f479a8..b78d69b 100644 --- a/transform/internal/xdm/html/options.go +++ b/transform/internal/xdm/html/options.go @@ -13,6 +13,19 @@ import ( norm "github.com/skarm/xmlsec/internal/normalization" ) +// HTML5 is the default requested version; supported numeric values retain +// the recognized HTML4, HTML4.01 and HTML5 profiles. The edition selector is +// independent: zero uses the current serialization table, while 20 pins REC2.0. +const ( + defaultHTMLVersion = "5" + htmlVersion4 = 4 + htmlVersion401 = 4.01 + htmlVersion5 = 5 + htmlVersionParseBits = 64 + defaultSerializationEdition = 0 + serializationEdition20 = 20 +) + // IsHTMLNamespace selects names subject to the special HTML output rules. func IsHTMLNamespace(uri string, html5 bool) bool { return uri == "" || html5 && uri == lexicon.NamespaceXHTML @@ -29,13 +42,13 @@ func RequestedVersion(htmlVersion, version string) string { return version } - return "5" + return defaultHTMLVersion } // SupportedVersion identifies the HTML versions selected by this engine. func SupportedVersion(version string) bool { - value, err := strconv.ParseFloat(version, 64) - return err == nil && (value == 4 || value == 4.01 || value == 5) + value, err := strconv.ParseFloat(version, htmlVersionParseBits) + return err == nil && (value == htmlVersion4 || value == htmlVersion401 || value == htmlVersion5) } // Writer configures HTML serialization. It is a value-style wrapper: diff --git a/transform/internal/xdm/html/output_entities.go b/transform/internal/xdm/html/output_entities.go index 1284fde..f0f26dc 100644 --- a/transform/internal/xdm/html/output_entities.go +++ b/transform/internal/xdm/html/output_entities.go @@ -2,11 +2,6 @@ package html -const ( - encISO88591 = "ISO-8859-1" - encWindows1252 = "Windows-1252" -) - func (d *htmlDumper) lookupEntityByRune(r rune) string { if d.entities == nil { d.entities = newHTMLEntityTable() diff --git a/transform/internal/xdm/html/shared_constants.go b/transform/internal/xdm/html/shared_constants.go index 9ddcd15..5874848 100644 --- a/transform/internal/xdm/html/shared_constants.go +++ b/transform/internal/xdm/html/shared_constants.go @@ -1,5 +1,7 @@ package html +// These case-sensitive spelling tokens are shared by HTML element descriptors +// and serialization rules. Entity-table values keep their established spelling. const ( nameBig = "big" nameBlockquote = "blockquote" diff --git a/transform/internal/xdm/html/xhtml.go b/transform/internal/xdm/html/xhtml.go index 9ada5f3..ed3fd18 100644 --- a/transform/internal/xdm/html/xhtml.go +++ b/transform/internal/xdm/html/xhtml.go @@ -34,7 +34,7 @@ func ConfigureXHTML(options *tree.XMLWriteOptions, html5, escapeURI bool, serial return false, nil } - return true, writeURIAttribute(ctx, out, attribute.Value, false) + return true, writeURIAttribute(ctx, out, attribute.Value, xmlAttributeMode) } } @@ -48,7 +48,7 @@ func ConfigureXHTML(options *tree.XMLWriteOptions, html5, escapeURI bool, serial local := LowerASCII(name.Local) - return xhtmlInline(local) || local == "pre" || local == nameScript || local == "style" || local == "title" || local == "textarea" + return xhtmlInline(local) || local == elemPre || local == nameScript || local == nameStyle || local == elemTitle || local == elemTextarea } } @@ -72,7 +72,7 @@ func PrepareXHTML(ctx context.Context, doc *xdm.Document, html5 bool, contentTyp return err } - if element, ok := node.(*xdm.Element); ok && XHTMLIsElement(element.URI(), element.LocalName(), html5) && EqualFoldASCII(element.LocalName(), "head") { + if element, ok := node.(*xdm.Element); ok && XHTMLIsElement(element.URI(), element.LocalName(), html5) && EqualFoldASCII(element.LocalName(), elemHead) { heads = append(heads, element) } @@ -96,7 +96,7 @@ func html5ElementName(local string) bool { } switch local { - case "article", "aside", elemAudio, "bdi", "canvas", "command", elemData, "datalist", "details", "dialog", elemEmbed, "figcaption", "figure", "footer", "header", "hgroup", elemKeygen, "main", "mark", "math", "menuitem", "meter", "nav", "output", "picture", "progress", "rb", "rp", "rt", "rtc", "ruby", "section", "slot", elemSource, "summary", "svg", "template", "time", elemTrack, elemVideo, elemWbr: + case elemArticle, elemAside, elemAudio, elemBdi, elemCanvas, elemCommand, elemData, elemDatalist, elemDetails, elemDialog, elemEmbed, elemFigcaption, elemFigure, elemFooter, elemHeader, elemHgroup, elemKeygen, elemMain, elemMark, elemMath, elemMenuitem, elemMeter, elemNav, elemOutput, elemPicture, elemProgress, elemRb, elemRp, elemRt, elemRtc, elemRuby, elemSection, elemSlot, elemSource, elemSummary, elemSVG, elemTemplate, elemTime, elemTrack, elemVideo, elemWbr: return true } @@ -104,12 +104,12 @@ func html5ElementName(local string) bool { } func xhtmlInline(local string) bool { - if descriptor := lookupElement(local); descriptor != nil && descriptor.inline != 0 { + if descriptor := lookupElement(local); descriptor != nil && descriptor.inline != blockLayout { return true } switch local { - case elemAudio, "bdi", "canvas", elemData, "datalist", elemEmbed, elemKeygen, "mark", "math", "meter", "output", "picture", "progress", "rb", "rp", "rt", "rtc", "ruby", "svg", "template", "time", elemVideo, elemWbr: + case elemAudio, elemBdi, elemCanvas, elemData, elemDatalist, elemEmbed, elemKeygen, elemMark, elemMath, elemMeter, elemOutput, elemPicture, elemProgress, elemRb, elemRp, elemRt, elemRtc, elemRuby, elemSVG, elemTemplate, elemTime, elemVideo, elemWbr: return true } @@ -146,23 +146,23 @@ func replaceXHTMLMetadata(doc *xdm.Document, head *xdm.Element, html5 bool, cont xdm.UnlinkNode(mutable) meta, ok := child.(*xdm.Element) - if !ok || !XHTMLIsElement(meta.URI(), meta.LocalName(), html5) || !EqualFoldASCII(meta.LocalName(), "meta") || !isContentTypeMeta(meta) { + if !ok || !XHTMLIsElement(meta.URI(), meta.LocalName(), html5) || !EqualFoldASCII(meta.LocalName(), elemMeta) || !isContentTypeMeta(meta) { children = append(children, child) } child = next } - meta, err := doc.CreateElementNS("meta", xdm.NewNamespace(head.Prefix(), head.URI())) + meta, err := doc.CreateElementNS(elemMeta, xdm.NewNamespace(head.Prefix(), head.URI())) if err != nil { return err } - if err = meta.SetAttribute("http-equiv", "Content-Type"); err != nil { + if err = meta.SetAttribute(attrHTTPEquiv, lexicon.HeaderContentType); err != nil { return err } - if err = meta.SetAttribute("content", contentType); err != nil { + if err = meta.SetAttribute(attrContent, contentType); err != nil { return err } diff --git a/transform/internal/xdm/id.go b/transform/internal/xdm/id.go index aaa6996..ecd03c7 100644 --- a/transform/internal/xdm/id.go +++ b/transform/internal/xdm/id.go @@ -6,6 +6,16 @@ package xdm import ( "context" "strings" + + "github.com/skarm/xmlsec/internal/lexicon" +) + +// The generic ID heuristic accepts exactly these additional spellings beside +// the lowercase XML local name; this is the FROZEN name set documented below. +const ( + idAttributeTitleCase = "Id" + idAttributeUpperCase = "ID" + xmlIDAttribute = lexicon.PrefixXML + ":" + lexicon.AttrID ) // FindElementsByID walks the subtree rooted at root (root included) and @@ -35,24 +45,25 @@ func FindElementsByID(root Node, id string) []*Element { ) walk = func(n Node) { - elem, ok := AsNode[*Element](n) - if !ok { + if IsNilNode(n) { return } - for _, attr := range elem.Attributes() { - if !isIDAttribute(attr) { - continue - } - // xs:ID derives from xs:NCName, which collapses whitespace; - // apply XML whitespace collapsing for xml:id. - if strings.TrimSpace(attr.Value()) == id { - matches = append(matches, elem) - break + if elem, ok := AsNode[*Element](n); ok { + for _, attr := range elem.Attributes() { + if !isIDAttribute(attr) { + continue + } + // xs:ID derives from xs:NCName, which collapses whitespace; + // apply XML whitespace collapsing for xml:id. + if strings.TrimSpace(attr.Value()) == id { + matches = append(matches, elem) + break + } } } - for child := elem.FirstChild(); child != nil; child = child.NextSibling() { + for child := n.FirstChild(); child != nil; child = child.NextSibling() { walk(child) } } @@ -88,35 +99,36 @@ func BuildIDIndex(ctx context.Context, root Node) (map[string][]*Element, error) } } - elem, ok := AsNode[*Element](n) - if !ok { + if IsNilNode(n) { return nil } - for _, attr := range elem.Attributes() { - if !isIDAttribute(attr) { - continue - } - // xs:ID derives from xs:NCName, which collapses whitespace; - // apply XML whitespace collapsing for xml:id. - id := strings.TrimSpace(attr.Value()) - // Every id on the element is recorded, so an element carrying - // two ID attributes cannot hide the second one from a caller - // checking len(index[id]) > 1. Only a repeat of the SAME id on - // the SAME element is dropped, matching the value-conditional - // break in FindElementsByID. Checking the last entry suffices: - // the walk runs one element's attributes to completion before - // descending into its children, so an element's appends for a - // given id are never interleaved with another element's. - existing := index[id] - if len(existing) > 0 && existing[len(existing)-1] == elem { - continue + if elem, ok := AsNode[*Element](n); ok { + for _, attr := range elem.Attributes() { + if !isIDAttribute(attr) { + continue + } + // xs:ID derives from xs:NCName, which collapses whitespace; + // apply XML whitespace collapsing for xml:id. + id := strings.TrimSpace(attr.Value()) + // Every id on the element is recorded, so an element carrying + // two ID attributes cannot hide the second one from a caller + // checking len(index[id]) > 1. Only a repeat of the SAME id on + // the SAME element is dropped, matching the value-conditional + // break in FindElementsByID. Checking the last entry suffices: + // the walk runs one element's attributes to completion before + // descending into its children, so an element's appends for a + // given id are never interleaved with another element's. + existing := index[id] + if len(existing) > 0 && existing[len(existing)-1] == elem { + continue + } + + index[id] = append(index[id], elem) } - - index[id] = append(index[id], elem) } - for child := elem.FirstChild(); child != nil; child = child.NextSibling() { + for child := n.FirstChild(); child != nil; child = child.NextSibling() { if err := walk(child); err != nil { return err } @@ -137,5 +149,5 @@ func BuildIDIndex(ctx context.Context, root Node) (map[string][]*Element, error) // rule lives. func isIDAttribute(attr *Attribute) bool { name := attr.Name() - return name == "Id" || name == "ID" || name == "id" || name == "xml:id" || attr.IDType() == IDAttribute + return name == idAttributeTitleCase || name == idAttributeUpperCase || name == lexicon.AttrID || name == xmlIDAttribute || attr.IDType() == IDAttribute } diff --git a/transform/internal/xdm/lexical/canonical_key_test.go b/transform/internal/xdm/lexical/canonical_key_test.go index 3cfcdde..23e07bb 100644 --- a/transform/internal/xdm/lexical/canonical_key_test.go +++ b/transform/internal/xdm/lexical/canonical_key_test.go @@ -48,6 +48,10 @@ func TestCanonicalKeyValueEquivalenceClasses(t *testing.T) { {"negative duration", "duration", []string{"-P1Y2M3DT4H5M6.7S", "-P14MT273906.7S"}, "P14MT273906.7S"}, {"dayTime duration", "dayTimeDuration", []string{"P1DT1H", "PT25H"}, "PT24H"}, {"yearMonth duration", "yearMonthDuration", []string{"P1Y", "P12M"}, "P11M"}, + {"dayTime negative zero", "dayTimeDuration", []string{"-PT0S", "P0D"}, "-PT0.001S"}, + {"dayTime negative fraction", "dayTimeDuration", []string{"-PT0.10S", "-PT0.100S"}, "-PT0.101S"}, + {"yearMonth negative", "yearMonthDuration", []string{"-P1Y", "-P12M"}, "-P13M"}, + {"yearMonth negative zero", "yearMonthDuration", []string{"-P0M", "P0Y"}, "-P1M"}, } { t.Run(test.name, func(t *testing.T) { t.Parallel() @@ -89,6 +93,14 @@ func TestCanonicalKeyRejectsStrictInvalidValues(t *testing.T) { {"gDay", "---00"}, {"gMonthDay", "--02-30"}, {"duration", "PT1.5"}, + {"dateTimeStamp", "2000-01-01T00:00:00"}, + {"dayTimeDuration", "P1Y"}, + {"dayTimeDuration", "P0Y"}, + {"dayTimeDuration", "PT-1S"}, + {"yearMonthDuration", "P1D"}, + {"yearMonthDuration", "PT0S"}, + {"yearMonthDuration", "P1Y-1M"}, + {"yearMonthDuration", "P0.5M"}, } { if _, ok := lexicalpkg.CanonicalKey(test.invalid, test.kind); ok { t.Fatalf("invalid %s %q acquired a value key", test.kind, test.invalid) diff --git a/transform/internal/xdm/lexical/compare.go b/transform/internal/xdm/lexical/compare.go index 72887c1..f04112a 100644 --- a/transform/internal/xdm/lexical/compare.go +++ b/transform/internal/xdm/lexical/compare.go @@ -17,6 +17,58 @@ import ( "github.com/skarm/xmlsec/internal/lexicon" ) +// XML Schema whiteSpace facet names select preservation, replacement or collapse. +const ( + whiteSpacePreserveFacet = "preserve" + whiteSpaceReplaceFacet = "replace" + whiteSpaceCollapseFacet = "collapse" +) + +// Numeric comparison and key formatting preserve IEEE widths and strconv conventions. +// The precision mode selects float32 rounding; infinity signs follow math.Inf/IsInf. +// The decimal radix is shared with lexical validation; key formats preserve their field order. +const ( + decimalComparisonInvalid = -2 + singlePrecisionBits = 32 + doublePrecisionBits = 64 + singlePrecisionMode = true + doublePrecisionMode = false + positiveInfinitySign = 1 + negativeInfinitySign = -1 + anyInfinitySign = 0 + floatKeyNotation = 'g' + shortestFloatPrecision = -1 + decimalRadix = 10 + durationKeyFormat = "%s|%s" + dateTimeKeyFormat = "%s|%d|%d|%d|%d|%s|%t" +) + +// Calendar arithmetic and validation share these units and Gregorian cycle lengths. +// XML Schema timezones are bounded by fourteen hours. The time reference date is +// this implementation's common anchor for comparing xs:time and constructing keys. +const ( + monthsPerYear = 12 + minutesPerHour = 60 + secondsPerMinute = 60 + hoursPerDay = 24 + secondsPerHour = 3600 + secondsPerDay = 86400 + maxTimezoneHours = 14 + calendarFirstMonth = 1 + calendarFirstDay = 1 + daysPerCommonYear = 365 + gregorianLeapInterval = 4 + gregorianCenturyYears = 100 + gregorianEraYears = 400 + durationReferenceDateCount = 4 + timeReferenceYear = 2000 + timeReferenceMonth = 1 + timeReferenceDay = 15 +) + +// Date lexicals split into year, month and day fields. +const dateFieldCount = 3 + // XSDFields splits s into items on runs of XSD whitespace only (space, tab, CR, // LF), the exported form of xsdFields. xs:list item separation is defined over // XSD whitespace, so callers must use this in place of strings.Fields: a list @@ -48,13 +100,13 @@ func Compare(a, b, builtinLocal string) (int, bool) { case lexicon.TypeBoolean: return compareBoolean(a, b) case lexicon.TypeFloat: - return compareFloat(a, b, true) + return compareFloat(a, b, singlePrecisionMode) case lexicon.TypeDouble: - return compareFloat(a, b, false) + return compareFloat(a, b, doublePrecisionMode) case lexicon.TypeDateTime, lexicon.TypeDateTimeStamp: // xs:dateTimeStamp is a subtype of xs:dateTime; compare in the dateTime - // value space. - return compareDateTime(a, b, lexicon.TypeDateTime) + // value space after validating the requested subtype. + return compareDateTime(a, b, builtinLocal) case lexicon.TypeDate: return compareDate(a, b, builtinLocal) case lexicon.TypeTime: @@ -72,7 +124,7 @@ func Compare(a, b, builtinLocal string) (int, bool) { case lexicon.TypeDuration, lexicon.TypeDayTimeDuration, lexicon.TypeYearMonthDuration: // xs:dayTimeDuration and xs:yearMonthDuration are subtypes of xs:duration; // compare in the duration value space. - return compareDuration(a, b) + return compareDuration(a, b, builtinLocal) case lexicon.TypeHexBinary: return compareHexBinary(a, b) case lexicon.TypeBase64Binary: @@ -93,7 +145,7 @@ func Compare(a, b, builtinLocal string) (int, bool) { } cmp := CompareDecimal(trimXSDSpace(a), trimXSDSpace(b)) - if cmp == -2 { + if cmp == decimalComparisonInvalid { return 0, false } @@ -114,20 +166,20 @@ func CanonicalKey(s, builtinLocal string) (string, bool) { return trimmed, false } - if trimmed == lexicon.ValueTrue || trimmed == "1" { - return "1", true + if trimmed == lexicon.ValueTrue || trimmed == booleanTrueDigit { + return booleanTrueDigit, true } - return "0", true + return booleanFalseDigit, true case lexicon.TypeFloat: - return canonicalFloatKey(s, 32) // xs:float is 32-bit IEEE-754 + return canonicalFloatKey(s, singlePrecisionBits) // xs:float is 32-bit IEEE-754 case lexicon.TypeDouble: - return canonicalFloatKey(s, 64) + return canonicalFloatKey(s, doublePrecisionBits) case lexicon.TypeDateTime, lexicon.TypeDate, lexicon.TypeTime, lexicon.TypeGYear, lexicon.TypeGYearMonth, lexicon.TypeGMonth, lexicon.TypeGDay, lexicon.TypeGMonthDay: return canonicalDateTimeKey(trimXSDSpace(s), builtinLocal) case lexicon.TypeDateTimeStamp: // xs:dateTimeStamp canonicalizes in the xs:dateTime value space. - return canonicalDateTimeKey(trimXSDSpace(s), lexicon.TypeDateTime) + return canonicalDateTimeKey(trimXSDSpace(s), builtinLocal) case lexicon.TypeHexBinary: trimmed := trimXSDSpace(s) if ValidateBuiltin(trimmed, lexicon.TypeHexBinary, Version11) != nil { @@ -157,10 +209,9 @@ func CanonicalKey(s, builtinLocal string) (string, bool) { // xs:dayTimeDuration / xs:yearMonthDuration canonicalize in the xs:duration // value space (they are valid xs:duration lexicals). trimmed := trimXSDSpace(s) - // Validate against the strict xs:duration lexical space before the lenient - // parseXSDDurationValue below, so CanonicalKey never canonicalizes a value - // the validator rejects. - if ValidateBuiltin(trimmed, lexicon.TypeDuration, Version11) != nil { + // Validate the requested subtype before the shared base-type parser, so + // CanonicalKey never canonicalizes a value the subtype validator rejects. + if ValidateBuiltin(trimmed, builtinLocal, Version11) != nil { return trimmed, false } @@ -178,7 +229,7 @@ func CanonicalKey(s, builtinLocal string) (string, bool) { months, seconds = new(big.Int).Neg(months), new(big.Rat).Neg(seconds) } - return fmt.Sprintf("%s|%s", months.String(), seconds.RatString()), true + return fmt.Sprintf(durationKeyFormat, months.String(), seconds.RatString()), true case lexicon.TypeDecimal, lexicon.TypeInteger, lexicon.TypeNonPositiveInteger, lexicon.TypeNegativeInteger, lexicon.TypeLong, lexicon.TypeInt, lexicon.TypeShort, lexicon.TypeByte, lexicon.TypeNonNegativeInteger, lexicon.TypeUnsignedLong, lexicon.TypeUnsignedInt, lexicon.TypeUnsignedShort, lexicon.TypeUnsignedByte, @@ -197,7 +248,7 @@ func CanonicalKey(s, builtinLocal string) (string, bool) { } return r.RatString(), true - case "string": + case lexicon.TypeString: // whiteSpace=preserve: the value space is the exact lexical string, so // leading/trailing/internal whitespace is significant. Do not alter it. return s, false @@ -225,12 +276,12 @@ func CanonicalKey(s, builtinLocal string) (string, bool) { // "collapse" so callers normalize conservatively. func WhiteSpace(builtinLocal string) string { switch builtinLocal { - case "string": - return "preserve" + case lexicon.TypeString: + return whiteSpacePreserveFacet case lexicon.TypeNormalizedString: - return "replace" + return whiteSpaceReplaceFacet default: - return "collapse" + return whiteSpaceCollapseFacet } } @@ -241,15 +292,92 @@ func WhiteSpace(builtinLocal string) string { // and "collapse" additionally collapses runs of spaces and trims the ends. func Normalize(s, builtinLocal string) string { switch WhiteSpace(builtinLocal) { - case "preserve": + case whiteSpacePreserveFacet: return s - case "replace": + case whiteSpaceReplaceFacet: return whitespaceReplace(s) default: // collapse return whitespaceCollapse(s) } } +// CompareDecimal compares two decimal string values with the ordering of +// math/big.Rat. Returns -1 if a < b, 0 if a == b, 1 if a > b, or -2 on parse +// error. +// +// When both operands match the plain xs:decimal lexical grammar +// (+|-)?(digits('.'digits?)?|'.'digits) the comparison runs digit-wise without +// allocating. Every other input (exponents, fractions such as "1/2", base +// prefixes, whitespace, malformed text) goes through big.Rat.SetString, so the +// result, including the -2 error value, is always what big.Rat gives. +func CompareDecimal(a, b string) int { + da, ok := parsePlainDecimal(a) + if !ok { + return compareDecimalRat(a, b) + } + + db, ok := parsePlainDecimal(b) + if !ok { + return compareDecimalRat(a, b) + } + + return comparePlainDecimal(da, db) +} + +// IsFloatNaN reports whether s is a valid xs:float/xs:double lexical form that +// denotes NaN. Only the bare "NaN" qualifies; the sign-prefixed +NaN/-NaN are +// not valid XSD lexical forms and are rejected. +func IsFloatNaN(s string) bool { + f, ok := parseXSDFloat(s) + return ok && math.IsNaN(f) +} + +// CompareFloatFacetBound compares two xs:float/xs:double facet BOUND lexicals for +// the purpose of the same-type range-facet consistency check (e.g. minInclusive +// vs maxInclusive). It differs from the ordinary value-space Compare in its +// treatment of NaN: where Compare returns incomparable (ok=false) for any NaN +// operand, this orders NaN as EQUAL to NaN and GREATER THAN every non-NaN value. +// That matches xmllint, which rejects a schema whose minInclusive="NaN" exceeds +// a finite maxInclusive while accepting minInclusive=finite with maxInclusive=NaN. +// +// builtinLocal must be xs:float or xs:double; for any other type it returns +// ok=false so the caller falls back to its normal comparator. Both operands are +// validated as valid float/double lexicals first: an invalid bound yields +// ok=false (no ordering decision), so the caller's invalid-bound check — not this +// consistency check — reports the error. +func CompareFloatFacetBound(a, b, builtinLocal string) (int, bool) { + if builtinLocal != lexicon.TypeFloat && builtinLocal != lexicon.TypeDouble { + return 0, false + } + + if !validBuiltinOperands(a, b, builtinLocal) { + return 0, false + } + + fa, ok1 := parseXSDFloat(trimXSDSpace(a)) + + fb, ok2 := parseXSDFloat(trimXSDSpace(b)) + if !ok1 || !ok2 { + return 0, false + } + + aNaN := math.IsNaN(fa) + + bNaN := math.IsNaN(fb) + if aNaN || bNaN { + switch { + case aNaN && bNaN: + return 0, true + case aNaN: + return 1, true + default: + return -1, true + } + } + + return compareFloat(a, b, builtinLocal == lexicon.TypeFloat) +} + // numericComparableTypes is the set of integer-family and decimal builtins that // Compare and CanonicalKey treat as value-comparable via math/big.Rat after // strict lexical validation (including range checks for the bounded subtypes). @@ -280,9 +408,9 @@ func validDateTimeOperands(a, b, builtinLocal string) bool { // true and "false"/"0" map to false. Any other input is not a valid boolean. func parseXSDBoolean(s string) (bool, bool) { switch s { - case lexicon.ValueTrue, "1": + case lexicon.ValueTrue, booleanTrueDigit: return true, true - case "false", "0": + case lexicon.ValueFalse, booleanFalseDigit: return false, true } @@ -425,7 +553,7 @@ func compareDateTimeMixedTZ(a, b xsdDateTime) (int, bool) { // of partial Gregorian types would otherwise cause invalid day/month rollover. // Year 0000 is valid; only nil denotes a missing year. compareTime supplies its // own reference date, so time values can use the determinate path. - if a.year == nil || b.year == nil || a.month < 1 || b.month < 1 || a.day < 1 || b.day < 1 { + if a.year == nil || b.year == nil || a.month < calendarFirstMonth || b.month < calendarFirstMonth || a.day < calendarFirstDay || b.day < calendarFirstDay { return 0, false } @@ -446,12 +574,12 @@ func compareDateTimeMixedTZ(a, b xsdDateTime) (int, bool) { // build the UTC-normalized plain value at each extreme. low := plain low.hasTZ = true - low.tzMin = 14 * 60 + low.tzMin = maxTimezoneHours * minutesPerHour low = low.normalizeToUTC() high := plain high.hasTZ = true - high.tzMin = -14 * 60 + high.tzMin = -maxTimezoneHours * minutesPerHour high = high.normalizeToUTC() cmpLow := compareDateTimeFields(low, tz) @@ -534,8 +662,8 @@ func compareTime(a, b, builtinLocal string) (int, bool) { da = da.normalizeHour24() db = db.normalizeHour24() // Set a reference date so TZ normalization day overflow works. - da.year, da.month, da.day = big.NewInt(2000), 1, 15 - db.year, db.month, db.day = big.NewInt(2000), 1, 15 + da.year, da.month, da.day = big.NewInt(timeReferenceYear), timeReferenceMonth, timeReferenceDay + db.year, db.month, db.day = big.NewInt(timeReferenceYear), timeReferenceMonth, timeReferenceDay return compareDateTimeParsed(da, db) } @@ -620,8 +748,8 @@ func compareGMonthDay(a, b, builtinLocal string) (int, bool) { return compareDateTimeParsed(da, db) } -func compareDuration(a, b string) (int, bool) { - if !validBuiltinOperands(a, b, lexicon.TypeDuration) { +func compareDuration(a, b, builtinLocal string) (int, bool) { + if !validBuiltinOperands(a, b, builtinLocal) { return 0, false } @@ -638,8 +766,8 @@ func compareDuration(a, b string) (int, bool) { return compareDurationComponents(am, as, bm, bs) } -func durationReferenceDates() [4]durationReferenceDate { - return [4]durationReferenceDate{ +func durationReferenceDates() [durationReferenceDateCount]durationReferenceDate { + return [durationReferenceDateCount]durationReferenceDate{ {1696, 9, 1}, {1697, 2, 1}, {1903, 3, 1}, @@ -698,12 +826,12 @@ func compareDurationTimelinePoint(a, b durationTimelinePoint) int { } func addMonthsToDate(year *big.Int, month, day int, delta *big.Int) (*big.Int, int, int) { - totalMonths := new(big.Int).Mul(year, big.NewInt(12)) + totalMonths := new(big.Int).Mul(year, big.NewInt(monthsPerYear)) totalMonths.Add(totalMonths, big.NewInt(int64(month-1))) totalMonths.Add(totalMonths, delta) newYear, monthRemainder := new(big.Int), new(big.Int) - newYear.DivMod(totalMonths, big.NewInt(12), monthRemainder) + newYear.DivMod(totalMonths, big.NewInt(monthsPerYear), monthRemainder) newMonth := int(monthRemainder.Int64()) + 1 if maxDay := daysInMonth(newYear, newMonth); day > maxDay { @@ -721,10 +849,10 @@ func daysBeforeDate(year *big.Int, month, day int) *big.Int { } func daysBeforeYear(year *big.Int) *big.Int { - days := new(big.Int).Mul(year, big.NewInt(365)) - days.Add(days, floorDivInt(new(big.Int).Add(year, big.NewInt(3)), 4)) - days.Sub(days, floorDivInt(new(big.Int).Add(year, big.NewInt(99)), 100)) - days.Add(days, floorDivInt(new(big.Int).Add(year, big.NewInt(399)), 400)) + days := new(big.Int).Mul(year, big.NewInt(daysPerCommonYear)) + days.Add(days, floorDivInt(new(big.Int).Add(year, big.NewInt(gregorianLeapInterval-1)), gregorianLeapInterval)) + days.Sub(days, floorDivInt(new(big.Int).Add(year, big.NewInt(gregorianCenturyYears-1)), gregorianCenturyYears)) + days.Add(days, floorDivInt(new(big.Int).Add(year, big.NewInt(gregorianEraYears-1)), gregorianEraYears)) return days } @@ -733,12 +861,12 @@ func daysBeforeMonth(year *big.Int, month int) int { common := [...]int{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334} leap := [...]int{0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335} - if month < 1 { + if month < calendarFirstMonth { return 0 } - if month > 12 { - month = 12 + if month > monthsPerYear { + month = monthsPerYear } if isLeapYearBig(year) { @@ -754,7 +882,7 @@ func floorDivInt(n *big.Int, d int64) *big.Int { func splitSecondsInDay(seconds *big.Rat) (*big.Int, *big.Rat) { numerator := seconds.Num() - divisor := new(big.Int).Mul(seconds.Denom(), big.NewInt(86400)) + divisor := new(big.Int).Mul(seconds.Denom(), big.NewInt(secondsPerDay)) dayOffset, remainder := new(big.Int), new(big.Int) dayOffset.DivMod(numerator, divisor, remainder) secondOfDay := new(big.Rat).SetFrac(remainder, seconds.Denom()) @@ -784,15 +912,15 @@ func canonicalFloatKey(s string, bitSize int) (string, bool) { return lexicon.FloatNaN, true } - if math.IsInf(f, 1) { + if math.IsInf(f, positiveInfinitySign) { return lexicon.FloatINF, true } - if math.IsInf(f, -1) { + if math.IsInf(f, negativeInfinitySign) { return lexicon.FloatNegINF, true } - if bitSize == 32 { + if bitSize == singlePrecisionBits { f = float64(float32(f)) // round to xs:float precision } // A finite lexical value can overflow to infinity at the target precision @@ -800,11 +928,11 @@ func canonicalFloatKey(s string, bitSize int) (string, bool) { // key matches the XSD-canonical "INF"/"-INF" form, in place of Go's // strconv.FormatFloat output ("+Inf"/"-Inf"), keeping equal values' keys // consistent with the literal INF/-INF spellings above. - if math.IsInf(f, 1) { + if math.IsInf(f, positiveInfinitySign) { return lexicon.FloatINF, true } - if math.IsInf(f, -1) { + if math.IsInf(f, negativeInfinitySign) { return lexicon.FloatNegINF, true } @@ -812,7 +940,7 @@ func canonicalFloatKey(s string, bitSize int) (string, bool) { f = 0 // normalize -0 to +0; they are equal in the value space } - return strconv.FormatFloat(f, 'g', -1, bitSize), true + return strconv.FormatFloat(f, floatKeyNotation, shortestFloatPrecision, bitSize), true } func canonicalDateTimeKey(s, builtinLocal string) (string, bool) { @@ -830,7 +958,7 @@ func canonicalDateTimeKey(s, builtinLocal string) (string, bool) { ) switch builtinLocal { - case lexicon.TypeDateTime: + case lexicon.TypeDateTime, lexicon.TypeDateTimeStamp: dt, ok = parseXSDDateTime(s) case lexicon.TypeDate: dt, ok = parseXSDDate(s) @@ -860,14 +988,14 @@ func canonicalDateTimeKey(s, builtinLocal string) (string, bool) { // timezoned value whose UTC offset crosses midnight (e.g. "11:30:00+01:00" // vs "10:30:00Z", both 10:30 UTC) would key off a zero date and differ in // the day/month/year fields, missing the equality compareTime reports. - dt.year, dt.month, dt.day = big.NewInt(2000), 1, 15 + dt.year, dt.month, dt.day = big.NewInt(timeReferenceYear), timeReferenceMonth, timeReferenceDay } if dt.hasTZ { dt = dt.normalizeToUTC() } - return fmt.Sprintf("%s|%d|%d|%d|%d|%s|%t", dt.yearVal().String(), dt.month, dt.day, dt.hour, dt.min, dt.secVal().RatString(), dt.hasTZ), true + return fmt.Sprintf(dateTimeKeyFormat, dt.yearVal().String(), dt.month, dt.day, dt.hour, dt.min, dt.secVal().RatString(), dt.hasTZ), true } // whitespaceCollapse applies the XSD whiteSpace="collapse" normalization: @@ -911,83 +1039,6 @@ type plainDecimal struct { frac string } -// CompareDecimal compares two decimal string values with the ordering of -// math/big.Rat. Returns -1 if a < b, 0 if a == b, 1 if a > b, or -2 on parse -// error. -// -// When both operands match the plain xs:decimal lexical grammar -// (+|-)?(digits('.'digits?)?|'.'digits) the comparison runs digit-wise without -// allocating. Every other input (exponents, fractions such as "1/2", base -// prefixes, whitespace, malformed text) goes through big.Rat.SetString, so the -// result, including the -2 error value, is always what big.Rat gives. -func CompareDecimal(a, b string) int { - da, ok := parsePlainDecimal(a) - if !ok { - return compareDecimalRat(a, b) - } - - db, ok := parsePlainDecimal(b) - if !ok { - return compareDecimalRat(a, b) - } - - return comparePlainDecimal(da, db) -} - -// IsFloatNaN reports whether s is a valid xs:float/xs:double lexical form that -// denotes NaN. Only the bare "NaN" qualifies; the sign-prefixed +NaN/-NaN are -// not valid XSD lexical forms and are rejected. -func IsFloatNaN(s string) bool { - f, ok := parseXSDFloat(s) - return ok && math.IsNaN(f) -} - -// CompareFloatFacetBound compares two xs:float/xs:double facet BOUND lexicals for -// the purpose of the same-type range-facet consistency check (e.g. minInclusive -// vs maxInclusive). It differs from the ordinary value-space Compare in its -// treatment of NaN: where Compare returns incomparable (ok=false) for any NaN -// operand, this orders NaN as EQUAL to NaN and GREATER THAN every non-NaN value. -// That matches xmllint, which rejects a schema whose minInclusive="NaN" exceeds -// a finite maxInclusive while accepting minInclusive=finite with maxInclusive=NaN. -// -// builtinLocal must be xs:float or xs:double; for any other type it returns -// ok=false so the caller falls back to its normal comparator. Both operands are -// validated as valid float/double lexicals first: an invalid bound yields -// ok=false (no ordering decision), so the caller's invalid-bound check — not this -// consistency check — reports the error. -func CompareFloatFacetBound(a, b, builtinLocal string) (int, bool) { - if builtinLocal != lexicon.TypeFloat && builtinLocal != lexicon.TypeDouble { - return 0, false - } - - if !validBuiltinOperands(a, b, builtinLocal) { - return 0, false - } - - fa, ok1 := parseXSDFloat(trimXSDSpace(a)) - - fb, ok2 := parseXSDFloat(trimXSDSpace(b)) - if !ok1 || !ok2 { - return 0, false - } - - aNaN := math.IsNaN(fa) - - bNaN := math.IsNaN(fb) - if aNaN || bNaN { - switch { - case aNaN && bNaN: - return 0, true - case aNaN: - return 1, true - default: - return -1, true - } - } - - return compareFloat(a, b, builtinLocal == lexicon.TypeFloat) -} - // parsePlainDecimal parses s against the xs:decimal lexical grammar // (+|-)?(digits('.'digits?)?|'.'digits) and reports false for anything else. func parsePlainDecimal(s string) (plainDecimal, bool) { @@ -1082,7 +1133,7 @@ func compareDecimalRat(a, b string) int { rb, ok2 := new(big.Rat).SetString(b) if !ok1 || !ok2 { - return -2 + return decimalComparisonInvalid } return ra.Cmp(rb) @@ -1159,10 +1210,10 @@ func decodeBase64Binary(s string) ([]byte, bool) { func parseXSDFloat(s string) (float64, bool) { switch s { - case lexicon.FloatINF, "+INF": - return math.Inf(1), true + case lexicon.FloatINF, lexicon.FloatPosINF: + return math.Inf(positiveInfinitySign), true case lexicon.FloatNegINF: - return math.Inf(-1), true + return math.Inf(negativeInfinitySign), true // Per XSD the only valid lexical form for NaN is the bare "NaN"; +NaN and // -NaN are not valid (floatRegex rejects them). Reject them here too so the // value space stays consistent with the lexical space — otherwise an invalid @@ -1171,12 +1222,12 @@ func parseXSDFloat(s string) (float64, bool) { return math.NaN(), true } - f, err := strconv.ParseFloat(s, 64) + f, err := strconv.ParseFloat(s, doublePrecisionBits) // XSD 1.1 maps a float/double lexical whose magnitude overflows the value // space to ±INF, and ValidateBuiltin accepts such lexicals (e.g. "1e400"). // strconv.ParseFloat signals this with ErrRange and returns ±Inf as the // result, so honor that infinity and accept the value. - if errors.Is(err, strconv.ErrRange) && math.IsInf(f, 0) { + if errors.Is(err, strconv.ErrRange) && math.IsInf(f, anyInfinitySign) { return f, true } @@ -1187,7 +1238,7 @@ func parseXSDFloat(s string) (float64, bool) { // "NAN", "inf", and "Infinity". The only valid XSD lexicals denoting NaN/±INF // are the exact forms handled in the switch above, so any NaN/Inf reaching // here came from one of those lenient spellings and must be rejected. - if math.IsNaN(f) || math.IsInf(f, 0) { + if math.IsNaN(f) || math.IsInf(f, anyInfinitySign) { return 0, false } @@ -1299,31 +1350,31 @@ func (dt xsdDateTime) normalizeToUTC() xsdDateTime { // Propagate minutes overflow. for r.min < 0 { - r.min += 60 + r.min += minutesPerHour r.hour-- } - for r.min >= 60 { - r.min -= 60 + for r.min >= minutesPerHour { + r.min -= minutesPerHour r.hour++ } // Propagate hours overflow. for r.hour < 0 { - r.hour += 24 + r.hour += hoursPerDay r.day-- } - for r.hour >= 24 { - r.hour -= 24 + for r.hour >= hoursPerDay { + r.hour -= hoursPerDay r.day++ } // Propagate day overflow. - for r.day < 1 { + for r.day < calendarFirstDay { r.month-- - if r.month < 1 { - r.month = 12 + if r.month < calendarFirstMonth { + r.month = monthsPerYear r.year.Sub(r.year, big.NewInt(1)) } @@ -1334,8 +1385,8 @@ func (dt xsdDateTime) normalizeToUTC() xsdDateTime { r.day -= daysInMonth(r.year, r.month) r.month++ - if r.month > 12 { - r.month = 1 + if r.month > monthsPerYear { + r.month = calendarFirstMonth r.year.Add(r.year, big.NewInt(1)) } } @@ -1351,7 +1402,7 @@ func (dt xsdDateTime) normalizeToUTC() xsdDateTime { // the year-agnostic g-types) simply reset the hour, matching how XSD treats // 24:00:00 and 00:00:00 of the next day as the same xs:time value. func (dt xsdDateTime) normalizeHour24() xsdDateTime { - if dt.hour != 24 { + if dt.hour != hoursPerDay { return dt } @@ -1363,7 +1414,7 @@ func (dt xsdDateTime) normalizeHour24() xsdDateTime { } // Only advance the calendar date when a full date is present; partial // gregorian types (year/month/day zero) carry no date to roll over. - if r.day < 1 || r.month < 1 { + if r.day < calendarFirstDay || r.month < calendarFirstMonth { return r } @@ -1372,14 +1423,14 @@ func (dt xsdDateTime) normalizeHour24() xsdDateTime { return r } - r.day = 1 + r.day = calendarFirstDay r.month++ - if r.month <= 12 { + if r.month <= monthsPerYear { return r } - r.month = 1 + r.month = calendarFirstMonth if r.year == nil { r.year = big.NewInt(0) } @@ -1406,7 +1457,7 @@ func parseYearBig(s string, neg bool) (*big.Int, bool) { } } - n, ok := new(big.Int).SetString(s, 10) + n, ok := new(big.Int).SetString(s, decimalRadix) if !ok { return nil, false } @@ -1435,7 +1486,7 @@ func parseTZ(s string) (bool, int) { return false, 0 } - offset := hh*60 + mm + offset := hh*minutesPerHour + mm if s[0] == '-' { offset = -offset } @@ -1460,8 +1511,8 @@ func parseXSDDateTime(s string) (xsdDateTime, bool) { return dt, false } - dParts := strings.SplitN(datePart, "-", 3) - if len(dParts) != 3 { + dParts := strings.SplitN(datePart, "-", dateFieldCount) + if len(dParts) != dateFieldCount { return dt, false } @@ -1559,7 +1610,7 @@ func parseXSDDate(s string) (xsdDateTime, bool) { } dashIdx := strings.IndexByte(s, '-') - if dashIdx < 4 { + if dashIdx < minExpandedYearDigits { return dt, false } @@ -1616,7 +1667,7 @@ func parseXSDGYear(s string) (xsdDateTime, bool) { i++ } - if i < 4 { + if i < minExpandedYearDigits { return dt, false } @@ -1647,7 +1698,7 @@ func parseXSDGYearMonth(s string) (xsdDateTime, bool) { i++ } - if i < 4 || i >= len(s) || s[i] != '-' { + if i < minExpandedYearDigits || i >= len(s) || s[i] != '-' { return dt, false } @@ -1768,15 +1819,15 @@ func isLeapYearBig(year *big.Int) bool { mod := func(m int64) int64 { return new(big.Int).Mod(year, big.NewInt(m)).Int64() } - if mod(4) != 0 { + if mod(gregorianLeapInterval) != 0 { return false } - if mod(100) != 0 { + if mod(gregorianCenturyYears) != 0 { return true } - return mod(400) == 0 + return mod(gregorianEraYears) == 0 } type xsdDuration struct { @@ -1818,18 +1869,18 @@ func (d *xsdDuration) addDateComponent(numStr string, designator byte) bool { // (the scan admits digits and at most one '.', but '.' is only valid // for seconds and is rejected here). Parse as *big.Int so huge // year/month components do not overflow. - n, ok := new(big.Int).SetString(numStr, 10) + n, ok := new(big.Int).SetString(numStr, decimalRadix) if !ok { return false } switch designator { case 'Y': - d.months.Add(d.months, new(big.Int).Mul(n, big.NewInt(12))) + d.months.Add(d.months, new(big.Int).Mul(n, big.NewInt(monthsPerYear))) case 'M': d.months.Add(d.months, n) case 'D': - d.seconds.Add(d.seconds, new(big.Rat).Mul(new(big.Rat).SetInt(n), big.NewRat(86400, 1))) + d.seconds.Add(d.seconds, new(big.Rat).Mul(new(big.Rat).SetInt(n), big.NewRat(secondsPerDay, 1))) default: return false } @@ -1840,19 +1891,19 @@ func (d *xsdDuration) addDateComponent(numStr string, designator byte) bool { func (d *xsdDuration) addTimeComponent(numStr string, designator byte) bool { switch designator { case 'H': - n, ok := new(big.Int).SetString(numStr, 10) + n, ok := new(big.Int).SetString(numStr, decimalRadix) if !ok { return false } - d.seconds.Add(d.seconds, new(big.Rat).Mul(new(big.Rat).SetInt(n), big.NewRat(3600, 1))) + d.seconds.Add(d.seconds, new(big.Rat).Mul(new(big.Rat).SetInt(n), big.NewRat(secondsPerHour, 1))) case 'M': - n, ok := new(big.Int).SetString(numStr, 10) + n, ok := new(big.Int).SetString(numStr, decimalRadix) if !ok { return false } - d.seconds.Add(d.seconds, new(big.Rat).Mul(new(big.Rat).SetInt(n), big.NewRat(60, 1))) + d.seconds.Add(d.seconds, new(big.Rat).Mul(new(big.Rat).SetInt(n), big.NewRat(secondsPerMinute, 1))) case 'S': // big.Rat.SetString accepts forms the seconds field must not use, // but the digit-run scan above admits only ASCII digits and a single diff --git a/transform/internal/xdm/lexical/compare_test.go b/transform/internal/xdm/lexical/compare_test.go index 9185ba4..29c8281 100644 --- a/transform/internal/xdm/lexical/compare_test.go +++ b/transform/internal/xdm/lexical/compare_test.go @@ -31,6 +31,12 @@ func TestComparisonValueSpacesAndPrecision(t *testing.T) { {"negative and positive", "decimal", "-0.001", "0", -1}, {"significantly different lengths", "integer", "999", "1000", -1}, {"unbounded integer", "integer", "999999999999999999999999999999", "1000000000000000000000000000000", -1}, + {"timestamp UTC aliases", "dateTimeStamp", "2000-01-01T01:00:00+01:00", "2000-01-01T00:00:00Z", 0}, + {"dayTime negative zero", "dayTimeDuration", "-PT0S", "P0D", 0}, + {"dayTime negative aliases", "dayTimeDuration", "-P1DT1H", "-PT25H", 0}, + {"dayTime fractional precision", "dayTimeDuration", "PT0.0001S", "PT0S", 1}, + {"yearMonth negative aliases", "yearMonthDuration", "-P1Y", "-P12M", 0}, + {"yearMonth negative zero", "yearMonthDuration", "-P0M", "P0Y", 0}, } { t.Run(test.name, func(t *testing.T) { t.Parallel() @@ -65,11 +71,19 @@ func TestComparisonRejectsEitherInvalidOperand(t *testing.T) { {"gDay", "---01", "---32"}, {"gMonthDay", "--02-29", "--02-30"}, {"duration", "PT1S", "PT1.0"}, + {"dateTimeStamp", "2000-01-01T00:00:00Z", "2000-01-01T00:00:00"}, + {"dayTimeDuration", "PT1S", "P1Y"}, + {"dayTimeDuration", "PT0S", "P0Y"}, + {"dayTimeDuration", "PT0S", "PT-1S"}, + {"yearMonthDuration", "P1M", "P1D"}, + {"yearMonthDuration", "P0Y", "PT0S"}, + {"yearMonthDuration", "P0Y", "P1Y-1M"}, + {"yearMonthDuration", "P0Y", "P0.5M"}, } { t.Run(test.kind, func(t *testing.T) { t.Parallel() - for _, operands := range [][2]string{{test.valid, test.invalid}, {test.invalid, test.valid}} { + for _, operands := range [][2]string{{test.valid, test.invalid}, {test.invalid, test.valid}, {test.invalid, test.invalid}} { if _, ok := lexicalpkg.Compare(operands[0], operands[1], test.kind); ok { t.Fatalf("invalid operand accepted: %q vs %q", operands[0], operands[1]) } diff --git a/transform/internal/xdm/lexical/facets.go b/transform/internal/xdm/lexical/facets.go index 6835f27..9de54b4 100644 --- a/transform/internal/xdm/lexical/facets.go +++ b/transform/internal/xdm/lexical/facets.go @@ -113,7 +113,7 @@ func CountFractionDigits(value string) int { // String lengths count characters; binary lengths count octets. func lengthApplicableTypes(key string) bool { switch key { - case lexicon.TypeString, lexicon.TypeNormalizedString, lexicon.TypeToken, "language", "Name", "NCName", "ID", lexicon.TypeIDREF, lexicon.TypeIDREFS, "ENTITY", lexicon.TypeENTITIES, "NMTOKEN", lexicon.TypeNMTokens, lexicon.TypeAnyURI, lexicon.TypeQName, lexicon.TypeNotation, lexicon.TypeHexBinary, lexicon.TypeBase64Binary: + case lexicon.TypeString, lexicon.TypeNormalizedString, lexicon.TypeToken, lexicon.TypeLanguage, lexicon.TypeName, lexicon.TypeNCName, lexicon.TypeID, lexicon.TypeIDREF, lexicon.TypeIDREFS, lexicon.TypeENTITY, lexicon.TypeENTITIES, lexicon.TypeNMToken, lexicon.TypeNMTokens, lexicon.TypeAnyURI, lexicon.TypeQName, lexicon.TypeNotation, lexicon.TypeHexBinary, lexicon.TypeBase64Binary: return true } diff --git a/transform/internal/xdm/lexical/lexical.go b/transform/internal/xdm/lexical/lexical.go index 8246c65..7efcc72 100644 --- a/transform/internal/xdm/lexical/lexical.go +++ b/transform/internal/xdm/lexical/lexical.go @@ -11,12 +11,19 @@ import ( "github.com/skarm/xmlsec/internal/lexicon" ) +// Language subtags are bounded in ASCII bytes. Expanded calendar years require +// at least four digits; this minimum does not impose a maximum year magnitude. +const ( + maxLanguageSubtagBytes = 8 + minExpandedYearDigits = 4 +) + // ValidLanguage checks the ASCII subtag grammar of xs:language directly. func ValidLanguage(s string) bool { first := true for part := range strings.SplitSeq(s, "-") { - if len(part) < 1 || len(part) > 8 { + if len(part) < 1 || len(part) > maxLanguageSubtagBytes { return false } @@ -69,7 +76,7 @@ func decimalLexical(s string) bool { } func floatLexical(s string, version Version) bool { - if s == lexicon.FloatNaN || s == lexicon.FloatINF || s == lexicon.FloatNegINF || s == "+INF" && version == Version11 { + if s == lexicon.FloatNaN || s == lexicon.FloatINF || s == lexicon.FloatNegINF || s == lexicon.FloatPosINF && version == Version11 { return true } @@ -115,7 +122,7 @@ func timezoneBody(s string, required bool) (string, bool) { func yearLexical(s string) bool { s = strings.TrimPrefix(s, "-") - return len(s) >= 4 && digits(s) && (len(s) == 4 || s[0] != '0') + return len(s) >= minExpandedYearDigits && digits(s) && (len(s) == minExpandedYearDigits || s[0] != '0') } func dateBody(s string) bool { @@ -177,7 +184,7 @@ func durationLexical(s, kind string) bool { for len(s) > 0 { if s[0] == 'T' { - if inTime || kind == "yearMonthDuration" { + if inTime || kind == lexicon.TypeYearMonthDuration { return false } @@ -269,5 +276,5 @@ func durationTimeOrder(unit byte, fraction bool, previous int) (int, bool) { func durationDateOrder(unit byte, fraction bool, kind string, previous int) (int, bool) { order := strings.IndexByte("YMD", unit) + 1 - return order, !(order <= previous || fraction || kind == "dayTimeDuration" && unit != 'D' || kind == "yearMonthDuration" && unit == 'D') + return order, !(order <= previous || fraction || kind == lexicon.TypeDayTimeDuration && unit != 'D' || kind == lexicon.TypeYearMonthDuration && unit == 'D') } diff --git a/transform/internal/xdm/lexical/validate.go b/transform/internal/xdm/lexical/validate.go index f975493..3208257 100644 --- a/transform/internal/xdm/lexical/validate.go +++ b/transform/internal/xdm/lexical/validate.go @@ -6,6 +6,7 @@ package lexical import ( "encoding/base64" "fmt" + "math" "math/big" "net/netip" "strings" @@ -15,6 +16,42 @@ import ( "github.com/skarm/xmlsec/internal/xmlchar" ) +// XML Schema booleans admit these numeric spellings as well as true and false. +const ( + booleanTrueDigit = "1" + booleanFalseDigit = "0" +) + +// Calendar fields are scanned as decimal integers. +const decimalFieldScanFormat = "%d" + +// Calendar day validation first applies the largest possible Gregorian month bound. +const maxGregorianDay = 31 + +// Lexical shape limits count IPv4 octets, decimal digits and QName fields. +// The timezone mode requires a zone for dateTimeStamp and permits its omission otherwise. +const ( + ipv4OctetCount = 4 + maxIPv4OctetDigits = 3 + qnameFieldCount = 2 + timezoneRequired = true + timezoneOptional = false +) + +// Inclusive XML Schema integer-subtype bounds remain independent of host int width. +// String bounds preserve the existing big.Int construction; each subtype keeps its own role. +const ( + longLowerBound = "-9223372036854775808" + longUpperBound = "9223372036854775807" + unsignedIntUpperBound = "4294967295" + unsignedLongUpperBound = "18446744073709551615" + unsignedIntegerLowerBound = 0 + nonNegativeIntegerMin = 0 + nonPositiveIntegerMax = 0 + positiveIntegerMin = 1 + negativeIntegerMax = -1 +) + // Version selects the XSD specification version for the version-sensitive // lexical rules. The zero value is Version10 (strict XSD 1.0), so callers that // have no version concept default to 1.0 behavior. @@ -55,7 +92,7 @@ func ValidateBuiltin(value, builtinLocal string, version Version) error { return validateDate(value, version) case lexicon.TypeBoolean: return validateBoolean(value) - case "language": + case lexicon.TypeLanguage: return validateLanguage(value) case lexicon.TypeFloat, lexicon.TypeDouble: return validateFloat(value, version) @@ -95,25 +132,25 @@ func ValidateBuiltin(value, builtinLocal string, version Version) error { func validateStringBuiltin(value, builtinLocal string, version Version) error { switch builtinLocal { - case "NCName", "ID", "IDREF", "ENTITY": + case lexicon.TypeNCName, lexicon.TypeID, lexicon.TypeIDREF, lexicon.TypeENTITY: return validateNCName(value) - case "Name": + case lexicon.TypeName: return validateName(value) - case "NMTOKEN": + case lexicon.TypeNMToken: return validateNMTOKEN(value) - case "QName", "NOTATION": + case lexicon.TypeQName, lexicon.TypeNotation: return validateQName(value) case lexicon.TypeBase64Binary: return validateBase64Binary(value) case lexicon.TypeNormalizedString: return validateNormalizedString(value) - case "token": + case lexicon.TypeToken: return validateToken(value) case lexicon.TypeIDREFS, lexicon.TypeENTITIES: return validateSpaceSeparatedList(value, validateNCName) case lexicon.TypeNMTokens: return validateSpaceSeparatedList(value, validateNMTOKEN) - case "anyURI": + case lexicon.TypeAnyURI: return validateAnyURI(value, version) } @@ -144,29 +181,29 @@ func validateInteger(value string) error { func lookupIntegerRange(key string) (integerRange, bool) { switch key { case lexicon.TypeByte: - return integerRange{big.NewInt(-128), big.NewInt(127)}, true + return integerRange{big.NewInt(math.MinInt8), big.NewInt(math.MaxInt8)}, true case lexicon.TypeShort: - return integerRange{big.NewInt(-32768), big.NewInt(32767)}, true + return integerRange{big.NewInt(math.MinInt16), big.NewInt(math.MaxInt16)}, true case lexicon.TypeInt: - return integerRange{big.NewInt(-2147483648), big.NewInt(2147483647)}, true + return integerRange{big.NewInt(math.MinInt32), big.NewInt(math.MaxInt32)}, true case lexicon.TypeLong: - return integerRange{newBigInt("-9223372036854775808"), newBigInt("9223372036854775807")}, true + return integerRange{newBigInt(longLowerBound), newBigInt(longUpperBound)}, true case lexicon.TypeUnsignedByte: - return integerRange{big.NewInt(0), big.NewInt(255)}, true + return integerRange{big.NewInt(unsignedIntegerLowerBound), big.NewInt(math.MaxUint8)}, true case lexicon.TypeUnsignedShort: - return integerRange{big.NewInt(0), big.NewInt(65535)}, true + return integerRange{big.NewInt(unsignedIntegerLowerBound), big.NewInt(math.MaxUint16)}, true case lexicon.TypeUnsignedInt: - return integerRange{big.NewInt(0), newBigInt("4294967295")}, true + return integerRange{big.NewInt(unsignedIntegerLowerBound), newBigInt(unsignedIntUpperBound)}, true case lexicon.TypeUnsignedLong: - return integerRange{big.NewInt(0), newBigInt("18446744073709551615")}, true + return integerRange{big.NewInt(unsignedIntegerLowerBound), newBigInt(unsignedLongUpperBound)}, true case lexicon.TypeNonNegativeInteger: - return integerRange{big.NewInt(0), nil}, true + return integerRange{big.NewInt(nonNegativeIntegerMin), nil}, true case lexicon.TypeNonPositiveInteger: - return integerRange{nil, big.NewInt(0)}, true + return integerRange{nil, big.NewInt(nonPositiveIntegerMax)}, true case lexicon.TypePositiveInteger: - return integerRange{big.NewInt(1), nil}, true + return integerRange{big.NewInt(positiveIntegerMin), nil}, true case lexicon.TypeNegativeInteger: - return integerRange{nil, big.NewInt(-1)}, true + return integerRange{nil, big.NewInt(negativeIntegerMax)}, true } var zero integerRange @@ -175,7 +212,7 @@ func lookupIntegerRange(key string) (integerRange, bool) { } func newBigInt(s string) *big.Int { - n, _ := new(big.Int).SetString(s, 10) + n, _ := new(big.Int).SetString(s, decimalRadix) return n } @@ -189,7 +226,7 @@ func validateIntegerWithRange(value, typeName string) error { return nil } - n, ok := new(big.Int).SetString(value, 10) + n, ok := new(big.Int).SetString(value, decimalRadix) if !ok { return fmt.Errorf("invalid integer") } @@ -219,7 +256,7 @@ func validateHexBinary(value string) error { // validateDate checks for xs:date: YYYY-MM-DD with optional timezone. func validateDate(value string, version Version) error { - if !calendarLexical(value, lexicon.TypeDate, false) { + if !calendarLexical(value, lexicon.TypeDate, timezoneOptional) { return fmt.Errorf("invalid date") } @@ -244,7 +281,7 @@ func validateLanguage(value string) error { func validateBoolean(value string) error { switch value { - case lexicon.ValueTrue, "false", "1", "0": + case lexicon.ValueTrue, lexicon.ValueFalse, booleanTrueDigit, booleanFalseDigit: return nil } @@ -271,7 +308,7 @@ func validateFloat(value string, version Version) error { // validateDateTime checks xs:dateTime. func validateDateTime(value string, version Version) error { - if !calendarLexical(value, lexicon.TypeDateTime, false) { + if !calendarLexical(value, lexicon.TypeDateTime, timezoneOptional) { return fmt.Errorf("invalid dateTime") } @@ -290,7 +327,7 @@ func validateDateTime(value string, version Version) error { // validateTime checks xs:time. func validateTime(value string) error { - if !calendarLexical(value, lexicon.TypeTime, false) { + if !calendarLexical(value, lexicon.TypeTime, timezoneOptional) { return fmt.Errorf("invalid time") } @@ -324,33 +361,33 @@ func validateTimeComponents(value string) error { } var hour, minute, sec int - if _, err := fmt.Sscanf(hourStr, "%d", &hour); err != nil { + if _, err := fmt.Sscanf(hourStr, decimalFieldScanFormat, &hour); err != nil { return fmt.Errorf("invalid time") } - if _, err := fmt.Sscanf(minStr, "%d", &minute); err != nil { + if _, err := fmt.Sscanf(minStr, decimalFieldScanFormat, &minute); err != nil { return fmt.Errorf("invalid time") } intSecStr, fracStr, _ := strings.Cut(secStr, ".") - if _, err := fmt.Sscanf(intSecStr, "%d", &sec); err != nil { + if _, err := fmt.Sscanf(intSecStr, decimalFieldScanFormat, &sec); err != nil { return fmt.Errorf("invalid time") } - if minute < 0 || minute > 59 { + if minute < 0 || minute > minutesPerHour-1 { return fmt.Errorf("invalid time: minute %d out of range", minute) } - if sec < 0 || sec > 59 { + if sec < 0 || sec > secondsPerMinute-1 { return fmt.Errorf("invalid time: second %d out of range", sec) } - if hour < 0 || hour > 24 { + if hour < 0 || hour > hoursPerDay { return fmt.Errorf("invalid time: hour %d out of range", hour) } // 24:00:00 (with zero minutes, seconds, and fractional seconds) is the only // permitted use of hour 24; it denotes midnight at the end of the day. - if hour == 24 && (minute != 0 || sec != 0 || !isZeroFraction(fracStr)) { + if hour == hoursPerDay && (minute != 0 || sec != 0 || !isZeroFraction(fracStr)) { return fmt.Errorf("invalid time: 24:00:00 is the only allowed value with hour 24") } @@ -403,32 +440,29 @@ func validateTimezone(tz string) error { } var hour, minute int - if _, err := fmt.Sscanf(hourStr, "%d", &hour); err != nil { + if _, err := fmt.Sscanf(hourStr, decimalFieldScanFormat, &hour); err != nil { return fmt.Errorf("invalid timezone") } - if _, err := fmt.Sscanf(minStr, "%d", &minute); err != nil { + if _, err := fmt.Sscanf(minStr, decimalFieldScanFormat, &minute); err != nil { return fmt.Errorf("invalid timezone") } - if minute < 0 || minute > 59 { + if minute < 0 || minute > minutesPerHour-1 { return fmt.Errorf("invalid timezone: minute %d out of range", minute) } - if hour < 0 || hour > 14 { + if hour < 0 || hour > maxTimezoneHours { return fmt.Errorf("invalid timezone: hour %d out of range", hour) } - if hour == 14 && minute != 0 { + if hour == maxTimezoneHours && minute != 0 { return fmt.Errorf("invalid timezone: offset exceeds 14:00") } return nil } -// validateDateComponents parses YYYY-MM-DD from a date/dateTime string and -// checks month (1-12) and day (1-maxDay) ranges. The value must already have -// passed the lexical check. // yearForbiddenInXSD10 reports whether yearStr (the digits-only year field, sign // already stripped) is the year "0000". XSD 1.0 has no year zero; XSD 1.1 adds // it. yearStr is assumed to have passed its lexical check (all digits). @@ -436,6 +470,8 @@ func yearForbiddenInXSD10(yearStr string) bool { return strings.Trim(yearStr, "0") == "" } +// validateDateComponents checks the year, month and day against the selected +// XSD edition. The value must already have passed its lexical check. func validateDateComponents(value string, version Version) error { s := value if len(s) > 0 && s[0] == '-' { @@ -467,25 +503,25 @@ func validateDateComponents(value string, version Version) error { // (e.g. "999999999999999999999999"), so do not parse it into a fixed-width // int; leap-year status is computed from the digit string directly. var month, day int - if _, err := fmt.Sscanf(monthStr, "%d", &month); err != nil { + if _, err := fmt.Sscanf(monthStr, decimalFieldScanFormat, &month); err != nil { return fmt.Errorf("invalid date") } - if _, err := fmt.Sscanf(dayStr, "%d", &day); err != nil { + if _, err := fmt.Sscanf(dayStr, decimalFieldScanFormat, &day); err != nil { return fmt.Errorf("invalid date") } - if month < 1 || month > 12 { + if month < calendarFirstMonth || month > monthsPerYear { return fmt.Errorf("invalid date: month %d out of range", month) } // Maximum days per month. February's length depends on whether the year is // a leap year in the proleptic Gregorian calendar. - maxDays := [13]int{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} + maxDays := [monthsPerYear + 1]int{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} if month == 2 && isLeapYearStr(yearStr) { maxDays[2] = 29 } - if day < 1 || day > maxDays[month] { + if day < calendarFirstDay || day > maxDays[month] { return fmt.Errorf("invalid date: day %d out of range for month %d", day, month) } @@ -506,15 +542,15 @@ func isLeapYearStr(year string) bool { return v } - if mod(year, 4) != 0 { + if mod(year, gregorianLeapInterval) != 0 { return false } - if mod(year, 100) != 0 { + if mod(year, gregorianCenturyYears) != 0 { return true } - return mod(year, 400) == 0 + return mod(year, gregorianEraYears) == 0 } // validateDuration checks xs:duration. When the time designator 'T' is present it @@ -552,7 +588,7 @@ func validateDuration(value string) error { // validateDateTimeStamp validates xs:dateTimeStamp: an xs:dateTime value that // must carry a timezone. Year-0000 gating follows the dateTime rules. func validateDateTimeStamp(value string, version Version) error { - if !calendarLexical(value, lexicon.TypeDateTime, true) { + if !calendarLexical(value, lexicon.TypeDateTime, timezoneRequired) { return fmt.Errorf("invalid dateTimeStamp") } @@ -564,7 +600,7 @@ func validateDateTimeStamp(value string, version Version) error { // be present. func validateDayTimeDuration(value string) error { - if !durationLexical(value, "dayTimeDuration") { + if !durationLexical(value, lexicon.TypeDayTimeDuration) { return fmt.Errorf("invalid dayTimeDuration") } // Reuse the shared duration body checks (at least one component, no dangling @@ -577,7 +613,7 @@ func validateDayTimeDuration(value string) error { // be present. func validateYearMonthDuration(value string) error { - if !durationLexical(value, "yearMonthDuration") { + if !durationLexical(value, lexicon.TypeYearMonthDuration) { return fmt.Errorf("invalid yearMonthDuration") } // "P" with no component is invalid. @@ -591,7 +627,7 @@ func validateYearMonthDuration(value string) error { // validateGYear checks xs:gYear. func validateGYear(value string, version Version) error { - if !calendarLexical(value, lexicon.TypeGYear, false) { + if !calendarLexical(value, lexicon.TypeGYear, timezoneOptional) { return fmt.Errorf("invalid gYear") } @@ -625,11 +661,11 @@ func stripAndCheckTimezone(value string) (string, error) { func gMonthRange(s, typeName string) error { var month int - if _, err := fmt.Sscanf(s, "%d", &month); err != nil { + if _, err := fmt.Sscanf(s, decimalFieldScanFormat, &month); err != nil { return fmt.Errorf("invalid %s", typeName) } - if month < 1 || month > 12 { + if month < calendarFirstMonth || month > monthsPerYear { return fmt.Errorf("invalid %s: month %d out of range", typeName, month) } @@ -638,11 +674,11 @@ func gMonthRange(s, typeName string) error { func gDayRange(s, typeName string) error { var day int - if _, err := fmt.Sscanf(s, "%d", &day); err != nil { + if _, err := fmt.Sscanf(s, decimalFieldScanFormat, &day); err != nil { return fmt.Errorf("invalid %s", typeName) } - if day < 1 || day > 31 { + if day < calendarFirstDay || day > maxGregorianDay { return fmt.Errorf("invalid %s: day %d out of range", typeName, day) } @@ -652,7 +688,7 @@ func gDayRange(s, typeName string) error { // validateGYearMonth checks xs:gYearMonth. func validateGYearMonth(value string, version Version) error { - if !calendarLexical(value, lexicon.TypeGYearMonth, false) { + if !calendarLexical(value, lexicon.TypeGYearMonth, timezoneOptional) { return fmt.Errorf("invalid gYearMonth") } @@ -674,7 +710,7 @@ func validateGYearMonth(value string, version Version) error { // validateGMonth checks xs:gMonth. func validateGMonth(value string) error { - if !calendarLexical(value, lexicon.TypeGMonth, false) { + if !calendarLexical(value, lexicon.TypeGMonth, timezoneOptional) { return fmt.Errorf("invalid gMonth") } @@ -689,7 +725,7 @@ func validateGMonth(value string) error { // validateGDay checks xs:gDay. func validateGDay(value string) error { - if !calendarLexical(value, lexicon.TypeGDay, false) { + if !calendarLexical(value, lexicon.TypeGDay, timezoneOptional) { return fmt.Errorf("invalid gDay") } @@ -704,7 +740,7 @@ func validateGDay(value string) error { // validateGMonthDay checks xs:gMonthDay. func validateGMonthDay(value string) error { - if !calendarLexical(value, lexicon.TypeGMonthDay, false) { + if !calendarLexical(value, lexicon.TypeGMonthDay, timezoneOptional) { return fmt.Errorf("invalid gMonthDay") } @@ -722,15 +758,15 @@ func validateGMonthDay(value string) error { } // Reject month/day combinations that never occur (e.g. --02-30, --04-31). var month, day int - if _, err := fmt.Sscanf(body[2:4], "%d", &month); err != nil { + if _, err := fmt.Sscanf(body[2:4], decimalFieldScanFormat, &month); err != nil { return fmt.Errorf("invalid gMonthDay") } - if _, err := fmt.Sscanf(body[5:7], "%d", &day); err != nil { + if _, err := fmt.Sscanf(body[5:7], decimalFieldScanFormat, &day); err != nil { return fmt.Errorf("invalid gMonthDay") } // gMonthDay is year-agnostic, so February permits its maximal length (29). - maxDays := [13]int{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} + maxDays := [monthsPerYear + 1]int{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} if day > maxDays[month] { return fmt.Errorf("invalid gMonthDay: day %d out of range for month %d", day, month) } @@ -924,13 +960,13 @@ func validXSD10IPv6(host string) bool { // RFC 2732 uses RFC 2373's 1*3DIGIT IPv4 octets. Unlike netip, // that grammar permits leading zeroes, interpreted as decimal. // Normalize only this validation copy; retain the original anyURI value. - octets := strings.SplitN(host[colon+1:], ".", 5) - if len(octets) != 4 { + octets := strings.SplitN(host[colon+1:], ".", ipv4OctetCount+1) + if len(octets) != ipv4OctetCount { return false } for i, octet := range octets { - if octet == "" || len(octet) > 3 { + if octet == "" || len(octet) > maxIPv4OctetDigits { return false } @@ -959,7 +995,7 @@ func isHexDigit(ch byte) bool { } func validateQName(value string) error { - parts := strings.SplitN(value, ":", 2) + parts := strings.SplitN(value, ":", qnameFieldCount) if len(parts) == 1 { return validateNCName(value) } diff --git a/transform/internal/xdm/names.go b/transform/internal/xdm/names.go index 278e1e0..deffea5 100644 --- a/transform/internal/xdm/names.go +++ b/transform/internal/xdm/names.go @@ -37,7 +37,7 @@ func XMLLangMatches(n Node, langArg string) (matched, found bool) { } for _, attr := range elem.Attributes() { - if attr.LocalName() == "lang" && attr.URI() == lexicon.NamespaceXML { + if attr.LocalName() == lexicon.AttrLang && attr.URI() == lexicon.NamespaceXML { val := strings.ToLower(attr.Value()) if val == langArg || strings.HasPrefix(val, langArg+"-") { return true, true @@ -69,7 +69,11 @@ func InScopeNamespaces(e *Element, dropXML bool) map[string]*Namespace { // Outermost to innermost so the innermost binding wins. for _, anc := range slices.Backward(chain) { for _, ns := range anc.Namespaces() { - byPrefix[ns.Prefix()] = ns + if ns.URI() == "" { + delete(byPrefix, ns.Prefix()) + } else { + byPrefix[ns.Prefix()] = ns + } } if ns := anc.Namespace(); ns != nil { @@ -81,6 +85,8 @@ func InScopeNamespaces(e *Element, dropXML bool) map[string]*Namespace { if dropXML { delete(byPrefix, lexicon.PrefixXML) + } else { + byPrefix[lexicon.PrefixXML] = NewNamespace(lexicon.PrefixXML, lexicon.NamespaceXML) } return byPrefix diff --git a/transform/internal/xdm/node.go b/transform/internal/xdm/node.go index 50198cb..b731d73 100644 --- a/transform/internal/xdm/node.go +++ b/transform/internal/xdm/node.go @@ -9,10 +9,21 @@ import ( "strings" "sync/atomic" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/tree" "github.com/skarm/xmlsec/internal/xmlchar" ) +// Detached draft views retain storage ownership without occupying a child or +// attribute position. Sibling selectors advance one slot in the requested direction. +const ( + detachedNodeSlot = -1 + detachedAttributeIndex = -1 + nextSiblingStep = 1 + previousSiblingStep = -1 + attributeOwnerName = "attribute-owner" +) + var nextNodeIdentity atomic.Uint64 //nolint:gochecknoglobals // Process-unique, non-retaining identities must survive node collection. // ElementType identifies a node kind in the XDM view, including internal CDATA @@ -110,7 +121,7 @@ func CopyNode(src Node, d *Document) (Node, error) { } r := tree.Children(cp)[0] - if err = d.wrap(context.Background(), r, -1); err != nil { + if err = d.wrap(context.Background(), r, detachedNodeSlot); err != nil { return nil, err } @@ -225,7 +236,7 @@ type sharedViewKey struct{} // NewDocument creates an empty mutable draft with explicit XML declaration metadata. func NewDocument(version, encoding string, standalone DocumentStandaloneType) *Document { d := &Document{nodes: map[*tree.Node]Node{}, attrs: map[attrKey]*Attribute{}, ids: map[string]*Element{}, version: version, encoding: encoding, standalone: standalone} - d.nodeView = nodeView{raw: &tree.Node{Kind: tree.DocumentNode}, doc: d, slot: -1, kind: DocumentNode, self: d} + d.nodeView = nodeView{raw: &tree.Node{Kind: tree.DocumentNode}, doc: d, slot: detachedNodeSlot, kind: DocumentNode, self: d} d.native = tree.FromNodes(nil, tree.DefaultLimits()) return d @@ -254,7 +265,9 @@ func WrapDocument(ctx context.Context, d *tree.Document) (*Document, error) { // NewDefaultDocument creates an empty mutable XML 1.0 UTF-8 draft with implicit // standalone=no metadata. -func NewDefaultDocument() *Document { return NewDocument("1.0", "UTF-8", StandaloneImplicitNo) } +func NewDefaultDocument() *Document { + return NewDocument(lexicon.XMLVersion10, lexicon.EncodingUTF8U, StandaloneImplicitNo) +} // CopyDoc clones tree storage and document metadata into a mutable independent // document. @@ -339,7 +352,7 @@ func (d *Document) CreateElement(name string) (*Element, error) { prefix, local := splitName(name) r := &tree.Node{Kind: tree.ElementNode, Name: tree.Name{Local: local}, Prefix: prefix} - _ = d.wrap(context.Background(), r, -1) + _ = d.wrap(context.Background(), r, detachedNodeSlot) return mustNode[*Element](d.nodes[r]), nil } @@ -362,7 +375,7 @@ func (d *Document) CreateAttribute(name, value string, ns *Namespace) (*Attribut return nil, tree.ErrMalformed } - e, _ := d.CreateElement("attribute-owner") + e, _ := d.CreateElement(attributeOwnerName) if ns == nil { prefix, local := splitName(name) @@ -392,7 +405,7 @@ func (d *Document) CreateNamespace(prefix, uri string) (*Namespace, error) { // CreateText stores immutable text without copying its bytes. func (d *Document) CreateText(value string) *Text { r := &tree.Node{Kind: tree.TextNode, Data: value} - _ = d.wrap(context.Background(), r, -1) + _ = d.wrap(context.Background(), r, detachedNodeSlot) return mustNode[*Text](d.nodes[r]) } @@ -400,7 +413,7 @@ func (d *Document) CreateText(value string) *Text { // CreateComment stores immutable text; serialization checks XML comment syntax. func (d *Document) CreateComment(value string) *Comment { r := &tree.Node{Kind: tree.CommentNode, Data: value} - _ = d.wrap(context.Background(), r, -1) + _ = d.wrap(context.Background(), r, detachedNodeSlot) return mustNode[*Comment](d.nodes[r]) } @@ -409,7 +422,7 @@ func (d *Document) CreateComment(value string) *Comment { // target and data syntax. func (d *Document) CreatePI(name, data string) *ProcessingInstruction { r := &tree.Node{Kind: tree.PINode, Name: tree.Name{Local: name}, Data: data} - _ = d.wrap(context.Background(), r, -1) + _ = d.wrap(context.Background(), r, detachedNodeSlot) return mustNode[*ProcessingInstruction](d.nodes[r]) } @@ -417,7 +430,7 @@ func (d *Document) CreatePI(name, data string) *ProcessingInstruction { // CreateCDATASection retains immutable text with a CDATA node identity. func (d *Document) CreateCDATASection(value string) *CDATASection { r := &tree.Node{Kind: tree.TextNode, Data: value} - n := &CDATASection{raw: r, doc: d, slot: -1, kind: CDATASectionNode} + n := &CDATASection{raw: r, doc: d, slot: detachedNodeSlot, kind: CDATASectionNode} n.self = n d.nodes[r] = n @@ -434,7 +447,7 @@ func (d *Document) SetURL(s string) { d.url = s } // Encoding returns the encoding metadata, defaulting to UTF-8 when empty. func (d *Document) Encoding() string { if d.encoding == "" { - return "UTF-8" + return lexicon.EncodingUTF8U } return d.encoding @@ -511,7 +524,7 @@ func (d *Document) wrap(ctx context.Context, r *tree.Node, slot int) error { for i, a := range r.Attrs { d.attribute(e, a.Name, i) - if tree.AttributeDTDType(d.native, r, a.Name) == tree.DTDID || a.Name == (tree.Name{Space: tree.XMLNamespace, Local: "id"}) { + if tree.AttributeDTDType(d.native, r, a.Name) == tree.DTDID || a.Name == (tree.Name{Space: tree.XMLNamespace, Local: lexicon.AttrID}) { d.ids[d.attrs[attrKey{r, a.Name}].Value()] = e } } @@ -540,7 +553,7 @@ func (d *Document) adopt(n Node) { d.nodes[n.view().raw] = n if e, ok := n.(*Element); ok { for i, entry := range e.raw.Attrs { - if entry.Name == (tree.Name{Space: tree.XMLNamespace, Local: "id"}) { + if entry.Name == (tree.Name{Space: tree.XMLNamespace, Local: lexicon.AttrID}) { d.ids[xmlchar.CollapseSpace(entry.Value)] = e } @@ -576,55 +589,12 @@ func (d *Document) attribute(e *Element, name tree.Name, index int) { return } - a := &Attribute{raw: e.raw, doc: d, kind: AttributeNode, slot: -1, parent: e, key: name, owner: e, attributeIndex: index} + a := &Attribute{raw: e.raw, doc: d, kind: AttributeNode, slot: detachedNodeSlot, parent: e, key: name, owner: e, attributeIndex: index} a.self = a a.refreshXMLID() d.attrs[key] = a } -func copyIDProperties(source, destination Node) { - first, ok := source.(*Element) - if ok { - if second, ok := destination.(*Element); ok { - for _, attribute := range first.Attributes() { - if kind := attribute.IDType(); kind != UntypedAttribute { - if target := second.GetAttributeNodeNS(attribute.LocalName(), attribute.URI()); target != nil { - target.SetIDType(kind) - } - } - } - } - } - - a, b := source.FirstChild(), destination.FirstChild() - for a != nil && b != nil { - copyIDProperties(a, b) - a, b = a.NextSibling(), b.NextSibling() - } -} - -// Snapshot cloning preserves construction bases stored outside XML attributes. -// Language copies into new parents apply their own inheritance rules separately. -func copyDocumentBases(source, destination Node) { - destination.view().baseURI = source.view().baseURI - tree.SetNodeBase(destination.OwnerDocument().native, destination.view().raw, source.view().baseURI) - - first, second := source.FirstChild(), destination.FirstChild() - for first != nil && second != nil { - copyDocumentBases(first, second) - first, second = first.NextSibling(), second.NextSibling() - } -} - -func checkedDocumentView(v any) *Document { - d, ok := v.(*Document) - if !ok || d == nil { - panic("invalid internal snapshot view") - } - - return d -} - type ( // Element is an XDM element view over project-tree storage. Element struct{ nodeView } @@ -761,7 +731,7 @@ func (e *Element) SetAttributeNS(local, value string, ns *Namespace) error { } name := tree.Name{Space: uri, Local: local} - if uri == tree.XMLNamespace && local == "id" { + if uri == tree.XMLNamespace && local == lexicon.AttrID { value = xmlchar.CollapseSpace(value) } @@ -779,7 +749,7 @@ func (e *Element) SetAttributeNS(local, value string, ns *Namespace) error { e.doc.RegisterID(value, e) - if uri == tree.XMLNamespace && local == "id" { + if uri == tree.XMLNamespace && local == lexicon.AttrID { view.xmlIDValue = value } } @@ -791,7 +761,7 @@ func (e *Element) SetAttributeNS(local, value string, ns *Namespace) error { e.raw.Attrs = append(e.raw.Attrs, tree.Attribute{Name: name, Prefix: prefix, Value: value}) e.doc.attribute(e, name, len(e.raw.Attrs)-1) - if uri == tree.XMLNamespace && local == "id" { + if uri == tree.XMLNamespace && local == lexicon.AttrID { e.doc.RegisterID(value, e) } @@ -917,7 +887,7 @@ func (e *Element) RemoveAttributeNS(local, uri string) bool { if view != nil { view.detached = true - view.attributeIndex = -1 + view.attributeIndex = detachedAttributeIndex } tree.SetAttributeDTDType(e.doc.native, e.raw, a.Name, tree.DTDNoType) @@ -943,7 +913,7 @@ func (e *Element) FindAttribute(p AttributePredicate) (*Attribute, bool) { } func (e *Element) attributeValue(a tree.Attribute) string { - if a.Name == (tree.Name{Space: tree.XMLNamespace, Local: "id"}) { + if a.Name == (tree.Name{Space: tree.XMLNamespace, Local: lexicon.AttrID}) { return e.doc.attrs[attrKey{e.raw, a.Name}].Value() } @@ -985,7 +955,7 @@ func (a *Attribute) URI() string { return a.key.Space } // Value exposes the XDM-normalized xml:id while retaining the original snapshot // for references and export. Other attributes keep the XML parser's value. func (a *Attribute) Value() string { - if a.key == (tree.Name{Space: tree.XMLNamespace, Local: "id"}) { + if a.key == (tree.Name{Space: tree.XMLNamespace, Local: lexicon.AttrID}) { return a.xmlIDValue } @@ -1033,11 +1003,11 @@ func (a *Attribute) LastChild() Node { return nil } // IDType returns the retained ID or IDREF classification, recognizing xml:id // independently of DTD metadata. func (a *Attribute) IDType() AttributeIDType { - if a.key == (tree.Name{Space: tree.XMLNamespace, Local: "id"}) { + if a.key == (tree.Name{Space: tree.XMLNamespace, Local: lexicon.AttrID}) { return IDAttribute } - if a.atype != 0 { + if a.atype != UntypedAttribute { return a.atype } @@ -1080,7 +1050,7 @@ func (a *Attribute) SetIDType(t AttributeIDType) { } func (a *Attribute) refreshXMLID() { - if a.key == (tree.Name{Space: tree.XMLNamespace, Local: "id"}) { + if a.key == (tree.Name{Space: tree.XMLNamespace, Local: lexicon.AttrID}) { a.xmlIDValue = xmlchar.CollapseSpace(a.entry().Value) } } @@ -1147,8 +1117,8 @@ func NewNamespace(prefix, uri string) *Namespace { // LookupNSByPrefix searches the node and its ancestors for a prefix binding. The xml // prefix always has its predefined binding. func LookupNSByPrefix(n Node, prefix string) *Namespace { - if prefix == "xml" { - return NewNamespace("xml", tree.XMLNamespace) + if prefix == lexicon.PrefixXML { + return NewNamespace(lexicon.PrefixXML, tree.XMLNamespace) } for ; n != nil; n = n.Parent() { @@ -1238,7 +1208,7 @@ func NewNamespaceNodeWrapper(ns *Namespace, owner Node) *NamespaceNodeWrapper { doc = owner.OwnerDocument() } - n := &NamespaceNodeWrapper{doc: doc, kind: NamespaceNode, parent: owner, slot: -1, ns: ns} + n := &NamespaceNodeWrapper{doc: doc, kind: NamespaceNode, parent: owner, slot: detachedNodeSlot, ns: ns} n.self = n return n @@ -1263,7 +1233,7 @@ func UnlinkNode(n MutableNode) { } p.view().raw.Children = slices.Delete(p.view().raw.Children, v.slot, v.slot+1) - v.slot = -1 + v.slot = detachedNodeSlot v.raw.Parent = nil v.parent = nil @@ -1379,7 +1349,7 @@ func NodeGetBase(doc *Document, n Node) string { } if e, ok := p.(*Element); ok { - if s, ok := e.GetAttributeNS("base", tree.XMLNamespace); ok { + if s, ok := e.GetAttributeNS(lexicon.AttrBase, tree.XMLNamespace); ok { base = BuildURI(s, base) } } @@ -1425,7 +1395,7 @@ func FilterChildren(n Node, keep func(Node) bool) { if keep(child) { out = append(out, r) } else { - child.view().slot = -1 + child.view().slot = detachedNodeSlot r.Parent = nil } } @@ -1436,6 +1406,22 @@ func FilterChildren(n Node, keep func(Node) bool) { reindex(n) } +// NodeIdentity returns a non-recycled number without retaining the node. Lazy +// assignment is safe for shared immutable snapshots and survives node adoption. +func NodeIdentity(n Node) uint64 { + nv := n.view() + if id := nv.identity.Load(); id != 0 { + return id + } + + id := nextNodeIdentity.Add(1) + if nv.identity.CompareAndSwap(0, id) { + return id + } + + return nv.identity.Load() +} + // Views are initialized in place; an assigned identity stays with its node. type nodeView struct { // identity lazily assigns a process-unique node number without retaining other nodes. @@ -1528,9 +1514,9 @@ func (nv *nodeView) LastChild() Node { return nv.doc.nodes[nv.raw.Children[len(nv.raw.Children)-1]] } -func (nv *nodeView) NextSibling() Node { return nv.sibling(1) } +func (nv *nodeView) NextSibling() Node { return nv.sibling(nextSiblingStep) } -func (nv *nodeView) PrevSibling() Node { return nv.sibling(-1) } +func (nv *nodeView) PrevSibling() Node { return nv.sibling(previousSiblingStep) } func (nv *nodeView) AddChild(child Node) error { if err := nv.mutable(); err != nil { @@ -1781,20 +1767,47 @@ func (nv *nodeView) mutable() error { return nil } -// NodeIdentity returns a non-recycled number without retaining the node. Lazy -// assignment is safe for shared immutable snapshots and survives node adoption. -func NodeIdentity(n Node) uint64 { - nv := n.view() - if id := nv.identity.Load(); id != 0 { - return id +func copyIDProperties(source, destination Node) { + first, ok := source.(*Element) + if ok { + if second, ok := destination.(*Element); ok { + for _, attribute := range first.Attributes() { + if kind := attribute.IDType(); kind != UntypedAttribute { + if target := second.GetAttributeNodeNS(attribute.LocalName(), attribute.URI()); target != nil { + target.SetIDType(kind) + } + } + } + } } - id := nextNodeIdentity.Add(1) - if nv.identity.CompareAndSwap(0, id) { - return id + a, b := source.FirstChild(), destination.FirstChild() + for a != nil && b != nil { + copyIDProperties(a, b) + a, b = a.NextSibling(), b.NextSibling() } +} - return nv.identity.Load() +// Snapshot cloning preserves construction bases stored outside XML attributes. +// Language copies into new parents apply their own inheritance rules separately. +func copyDocumentBases(source, destination Node) { + destination.view().baseURI = source.view().baseURI + tree.SetNodeBase(destination.OwnerDocument().native, destination.view().raw, source.view().baseURI) + + first, second := source.FirstChild(), destination.FirstChild() + for first != nil && second != nil { + copyDocumentBases(first, second) + first, second = first.NextSibling(), second.NextSibling() + } +} + +func checkedDocumentView(v any) *Document { + d, ok := v.(*Document) + if !ok || d == nil { + panic("invalid internal snapshot view") + } + + return d } func reindex(p Node) { diff --git a/transform/internal/xdm/node_mutation_test.go b/transform/internal/xdm/node_mutation_test.go index 1f68b10..e416f8a 100644 --- a/transform/internal/xdm/node_mutation_test.go +++ b/transform/internal/xdm/node_mutation_test.go @@ -9,44 +9,6 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) -func newDraftElement(t *testing.T, d *xdm.Document, name string) *xdm.Element { - t.Helper() - e, err := d.CreateElement(name) - if err != nil { - t.Fatal(err) - } - return e -} - -func requireNodeSuccess(t *testing.T, errs ...error) { - t.Helper() - for index, err := range errs { - if err != nil { - t.Fatalf("node operation %d failed: %v", index+1, err) - } - } -} - -func assertNodeChildren(t *testing.T, parent xdm.Node, want ...xdm.Node) { - t.Helper() - got := slices.Collect(xdm.Children(parent)) - if !slices.Equal(got, want) { - t.Fatalf("children of %s=%v; want %v", parent.Name(), got, want) - } - for i, child := range got { - var prev, next xdm.Node - if i > 0 { - prev = got[i-1] - } - if i+1 < len(got) { - next = got[i+1] - } - if child.Parent() != parent || child.PrevSibling() != prev || child.NextSibling() != next { - t.Fatalf("child %d has stale parent/sibling links", i) - } - } -} - //nolint:tparallel // These rejection cases intentionally mutate and inspect the same draft graph in sequence. func TestNodeDraftRejectsCyclesAndInvalidChildrenWithoutDetaching(t *testing.T) { t.Parallel() @@ -310,3 +272,41 @@ func TestNodeDraftFrozenMutationsAndCopyOnAttach(t *testing.T) { t.Fatal("panicking frozen mutation changed source namespace") } } + +func newDraftElement(t *testing.T, d *xdm.Document, name string) *xdm.Element { + t.Helper() + e, err := d.CreateElement(name) + if err != nil { + t.Fatal(err) + } + return e +} + +func requireNodeSuccess(t *testing.T, errs ...error) { + t.Helper() + for index, err := range errs { + if err != nil { + t.Fatalf("node operation %d failed: %v", index+1, err) + } + } +} + +func assertNodeChildren(t *testing.T, parent xdm.Node, want ...xdm.Node) { + t.Helper() + got := slices.Collect(xdm.Children(parent)) + if !slices.Equal(got, want) { + t.Fatalf("children of %s=%v; want %v", parent.Name(), got, want) + } + for i, child := range got { + var prev, next xdm.Node + if i > 0 { + prev = got[i-1] + } + if i+1 < len(got) { + next = got[i+1] + } + if child.Parent() != parent || child.PrevSibling() != prev || child.NextSibling() != next { + t.Fatalf("child %d has stale parent/sibling links", i) + } + } +} diff --git a/transform/internal/xdm/nodes_axes.go b/transform/internal/xdm/nodes_axes.go index 1ae0fcc..57316ca 100644 --- a/transform/internal/xdm/nodes_axes.go +++ b/transform/internal/xdm/nodes_axes.go @@ -11,11 +11,28 @@ import ( "github.com/skarm/xmlsec/internal/nilcheck" ) -// maxInt is the largest value an int can hold, used to saturate the node-set -// limit when offsetting it past the nodes already in the destination slice -// would overflow. +// maxInt is the largest host int. Axis accumulation saturates at this bound +// when adding existing nodes would overflow; serialization uses the same bound +// as its representable output-byte ceiling. const maxInt = int(^uint(0) >> 1) +// XPath axis names are the lexical vocabulary mapped to the AxisType enum. +const ( + axisNameChild = "child" + axisNameDescendant = "descendant" + axisNameParent = "parent" + axisNameAncestor = "ancestor" + axisNameFollowingSibling = "following-sibling" + axisNamePrecedingSibling = "preceding-sibling" + axisNameFollowing = "following" + axisNamePreceding = "preceding" + axisNameAttribute = "attribute" + axisNameNamespace = "namespace" + axisNameSelf = "self" + axisNameDescendantOrSelf = "descendant-or-self" + axisNameAncestorOrSelf = "ancestor-or-self" +) + // AxisType identifies one of the 13 XPath axes. type AxisType int @@ -278,10 +295,9 @@ func NamespacePrefixesInScope(ctx context.Context, ancestors []Node) (map[string return inScope, nil } -// CollectNamespaceNodes builds the namespace node list for an element, adding -// the xml prefix first and then ancestor-declared prefixes in document order. -// For each prefix, the innermost (closest ancestor) declaration provides the -// URI, but the output order follows document order (outermost first). +// CollectNamespaceNodes builds the namespace node list for an element in stable +// document order, consistent with the cache and direct comparisons. For each +// prefix, the innermost (closest ancestor) declaration provides the URI. // It checks ctx for cancellation in both passes (outer and inner loops) so a // long namespace::* traversal can be aborted promptly. func CollectNamespaceNodes(ctx context.Context, ancestors []Node, inScope map[string]struct{}, elem *Element) ([]Node, error) { @@ -295,12 +311,11 @@ func CollectNamespaceNodes(ctx context.Context, ancestors []Node, inScope map[st var result []Node - xmlNS := NewNamespace("xml", lexicon.NamespaceXML) + xmlNS := NewNamespace(lexicon.PrefixXML, lexicon.NamespaceXML) result = append(result, NewNamespaceNodeWrapper(xmlNS, elem)) - // Second pass: output in document order (outermost to innermost), but - // use the innermost URI for each prefix. - emitted := map[string]struct{}{"xml": {}} + // Second pass: collect each active prefix once, using its innermost URI. + emitted := map[string]struct{}{lexicon.PrefixXML: {}} for _, v := range slices.Backward(ancestors) { if err := ctx.Err(); err != nil { @@ -333,37 +348,41 @@ func CollectNamespaceNodes(ctx context.Context, ancestors []Node, inScope map[st } } + slices.SortFunc(result, func(a, b Node) int { + return compareNamespacePrefixes(a.Name(), b.Name()) + }) + return result, nil } func lookupAxisName(key AxisType) (string, bool) { switch key { case AxisChild: - return "child", true + return axisNameChild, true case AxisDescendant: - return "descendant", true + return axisNameDescendant, true case AxisParent: - return "parent", true + return axisNameParent, true case AxisAncestor: - return "ancestor", true + return axisNameAncestor, true case AxisFollowingSibling: - return "following-sibling", true + return axisNameFollowingSibling, true case AxisPrecedingSibling: - return "preceding-sibling", true + return axisNamePrecedingSibling, true case AxisFollowing: - return "following", true + return axisNameFollowing, true case AxisPreceding: - return "preceding", true + return axisNamePreceding, true case AxisAttribute: - return "attribute", true + return axisNameAttribute, true case AxisNamespace: - return "namespace", true + return axisNameNamespace, true case AxisSelf: - return "self", true + return axisNameSelf, true case AxisDescendantOrSelf: - return "descendant-or-self", true + return axisNameDescendantOrSelf, true case AxisAncestorOrSelf: - return "ancestor-or-self", true + return axisNameAncestorOrSelf, true } var zero string diff --git a/transform/internal/xdm/nodes_docorder.go b/transform/internal/xdm/nodes_docorder.go index 3815cf0..fabd0a5 100644 --- a/transform/internal/xdm/nodes_docorder.go +++ b/transform/internal/xdm/nodes_docorder.go @@ -7,7 +7,21 @@ import ( "cmp" "slices" "sort" + "strings" "sync" + + "github.com/skarm/xmlsec/internal/lexicon" +) + +// Cached positions leave one virtual namespace slot after each real node. +// Stack capacity is a starting node count, not an admission limit. Unknown +// document registration and within-tree position retain separate sentinel roles. +const ( + docOrderPositionStride = 2 + namespacePositionOffset = 1 + initialDocOrderStackNodes = 256 + unknownDocumentOrder = -1 + unknownNodePosition = -1 ) // DocOrderCache caches document-order positions for nodes grouped by @@ -44,11 +58,11 @@ type DocOrderCache struct { // order results may be stale. func (c *DocOrderCache) Reset() { c.mu.Lock() - defer c.mu.Unlock() c.keys = nil c.documents = nil c.reserved = nil + c.mu.Unlock() } // Position returns the document-order position of a node, or -1 if unknown. @@ -57,8 +71,8 @@ func (c *DocOrderCache) Reset() { // fresh NamespaceNodeWrapper on each traversal) so they cannot be indexed // during BuildFrom. They receive position parent_pos + 1, which is a dedicated slot between the // parent element and its first attribute/child (indexWalk uses stride 2). -// SliceStable preserves input order for equal positions, keeping -// same-parent namespace nodes in their traversal order. Namespace nodes +// Namespace prefixes break ties between same-parent namespace nodes in the +// comparisons and sorting paths. Namespace nodes // of an element are deduplicated by NamespaceNodeKey in DeduplicateNodes/MergeNodeSets, // so duplicates from different union branches are already eliminated. func (c *DocOrderCache) Position(n Node) int { @@ -103,13 +117,7 @@ func (c *DocOrderCache) Compare(a, b Node) int { kb := c.lookupSortKeyLocked(b) if ka.docOrder >= 0 && kb.docOrder >= 0 { - // Both nodes are indexed, so their document registration order - // already tells whether they share a root. - if ka.docOrder == kb.docOrder { - return cmp.Compare(ka.position, kb.position) - } - - return cmp.Compare(ka.docOrder, kb.docOrder) + return compareNodeSortKeys(a, b, ka, kb) } // At least one node is not described by any indexed document. Resolve the @@ -161,7 +169,7 @@ func (c *DocOrderCache) sortKeyLocked(n Node) sortKey { return unknownSortKey() } - return sortKey{docOrder: pk.docOrder, position: pk.position + 1} + return sortKey{docOrder: pk.docOrder, position: pk.position + namespacePositionOffset} } // ensureSortKeyLocked returns the sort key of n, indexing n's document first if @@ -178,7 +186,7 @@ func (c *DocOrderCache) ensureSortKeyLocked(n Node) sortKey { return unknownSortKey() } - return sortKey{docOrder: pk.docOrder, position: pk.position + 1} + return sortKey{docOrder: pk.docOrder, position: pk.position + namespacePositionOffset} } } // A parentless namespace wrapper resolves to itself as a root, so it is @@ -225,13 +233,16 @@ func (c *DocOrderCache) indexInDocOrder(nodes []Node) bool { ordered := true - prev := c.ensureSortKeyLocked(nodes[0]) + previous := nodes[0] + + prev := c.ensureSortKeyLocked(previous) for _, n := range nodes[1:] { key := c.ensureSortKeyLocked(n) - if ordered && !prev.before(key) { + if ordered && compareNodeSortKeys(previous, n, prev, key) >= 0 { ordered = false } + previous = n prev = key } @@ -323,7 +334,7 @@ func (c *DocOrderCache) indexWalk(cur Node, order int, pos *int) { return } - stack := make([]Node, 0, 256) + stack := make([]Node, 0, initialDocOrderStackNodes) stack = append(stack, cur) // childBuf is reused across stack iterations to collect a node's owned // children before pushing them in reverse. @@ -336,12 +347,12 @@ func (c *DocOrderCache) indexWalk(cur Node, order int, pos *int) { c.keys[n] = sortKey{docOrder: order, position: *pos} // Stride 2: each node occupies an even slot, leaving odd slots // for virtual namespace nodes (position = parent + 1). - *pos += 2 + *pos += docOrderPositionStride if elem, ok := n.(*Element); ok { elem.ForEachAttribute(func(attr *Attribute) bool { c.keys[Node(attr)] = sortKey{docOrder: order, position: *pos} - *pos += 2 + *pos += docOrderPositionStride return true }) @@ -359,9 +370,6 @@ func (c *DocOrderCache) indexWalk(cur Node, order int, pos *int) { } } -// unknownSortKey is the key of a node no indexed document describes. -func unknownSortKey() sortKey { return sortKey{docOrder: -1, position: -1} } - // NSNodeKey identifies a namespace node of an element by that parent element // and its prefix (see NamespaceNodeKey). // NamespaceNodeWrapper objects are created fresh each time the namespace axis @@ -399,9 +407,9 @@ func DocumentRegistered(c *DocOrderCache, n Node) bool { root := DocumentRoot(n) c.mu.Lock() - defer c.mu.Unlock() _, ok := c.documents[root] + c.mu.Unlock() return ok } @@ -534,11 +542,16 @@ func DeduplicateNodesPreserveOrder(nodes []Node, maxNodes int) ([]Node, error) { return result, nil } -// CompareNodeOrder compares two nodes by document order using ancestor-chain -// walking. Returns -1 if a comes before b, +1 if after, 0 if same node. -// This is O(depth) per call, avoiding the need to index the entire document. +// CompareNodeOrder compares nodes within one rooted tree by walking ancestor +// and sibling chains. It returns -1 if a comes before b, +1 if after, and 0 +// for the same logical node, nil inputs, or nodes from different roots. Use +// DocOrderCache to establish an order spanning multiple trees. func CompareNodeOrder(a, b Node) int { - if a == b { + if IsNilNode(a) || IsNilNode(b) || a == b { + return 0 + } + + if DocumentRoot(a) != DocumentRoot(b) { return 0 } @@ -639,6 +652,11 @@ func DocumentRoot(n Node) Node { return top } +// unknownSortKey is the key of a node no indexed document describes. +func unknownSortKey() sortKey { + return sortKey{docOrder: unknownDocumentOrder, position: unknownNodePosition} +} + func compareOwnedEntryOrder(a, b, owner Node) int { // Namespace nodes come before attribute nodes. if a.Type() == NamespaceNode && b.Type() == AttributeNode { @@ -650,7 +668,7 @@ func compareOwnedEntryOrder(a, b, owner Node) int { } if a.Type() != AttributeNode { - return compareSiblingOrder(a, b) + return compareNamespacePrefixes(a.Name(), b.Name()) } element, ok := AsNode[*Element](owner) @@ -826,12 +844,38 @@ type sortKey struct { position int // position within the document } -func (k sortKey) before(other sortKey) bool { - if k.docOrder != other.docOrder { - return k.docOrder < other.docOrder +func compareNodeSortKeys(a, b Node, ka, kb sortKey) int { + if ka.docOrder != kb.docOrder { + return cmp.Compare(ka.docOrder, kb.docOrder) } - return k.position < other.position + if ka.position != kb.position { + return cmp.Compare(ka.position, kb.position) + } + + if ka.position >= 0 && a.Type() == NamespaceNode && b.Type() == NamespaceNode { + return compareNamespacePrefixes(a.Name(), b.Name()) + } + + return 0 +} + +// Namespace order is implementation-dependent, but every path must use the +// same total order. Keep the implicit xml binding first. +func compareNamespacePrefixes(a, b string) int { + if a == b { + return 0 + } + + if a == lexicon.PrefixXML { + return -1 + } + + if b == lexicon.PrefixXML { + return 1 + } + + return strings.Compare(a, b) } // sortByPrecomputedKeys sorts result by precomputed sort keys, avoiding @@ -842,8 +886,7 @@ func sortByPrecomputedKeys(result []Node, keys []sortKey) { sorted := true for i := 1; i < len(keys); i++ { - ki, kj := keys[i-1], keys[i] - if ki.docOrder > kj.docOrder || (ki.docOrder == kj.docOrder && ki.position > kj.position) { + if compareNodeSortKeys(result[i-1], result[i], keys[i-1], keys[i]) > 0 { sorted = false break } @@ -862,12 +905,8 @@ func sortByPrecomputedKeys(result []Node, keys []sortKey) { } sort.SliceStable(idx, func(i, j int) bool { - ki, kj := keys[idx[i]], keys[idx[j]] - if ki.docOrder != kj.docOrder { - return ki.docOrder < kj.docOrder - } - - return ki.position < kj.position + a, b := idx[i], idx[j] + return compareNodeSortKeys(result[a], result[b], keys[a], keys[b]) < 0 }) // Apply the permutation to result. tmp := make([]Node, n) diff --git a/transform/internal/xdm/nodes_navigation_test.go b/transform/internal/xdm/nodes_navigation_test.go index fc370a9..56a42fe 100644 --- a/transform/internal/xdm/nodes_navigation_test.go +++ b/transform/internal/xdm/nodes_navigation_test.go @@ -10,23 +10,6 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) -func parseNodeDocument(t *testing.T, source string) *xdm.Document { - t.Helper() - d, err := (xdm.ParseOptions{}).Parse(t.Context(), []byte(source)) - if err != nil { - t.Fatal(err) - } - return d -} - -func assertAxisNodes(t *testing.T, axis xdm.AxisType, from xdm.Node, want ...xdm.Node) { - t.Helper() - got, err := xdm.TraverseAxis(t.Context(), axis, from, 100) - if err != nil || !slices.Equal(got, want) { - t.Fatalf("%s from %T: %v,%v; want %v", axis, from, got, err, want) - } -} - func TestNodeAxesPreserveForwardAndReverseOrder(t *testing.T) { t.Parallel() @@ -149,22 +132,6 @@ func TestNodeAxesNamespaceShadowingUndeclarationAndIdentity(t *testing.T) { } } -type cancelAfterErrChecks struct { - context.Context - cancel context.CancelFunc - checks int - remaining int -} - -func (c *cancelAfterErrChecks) Err() error { - c.checks++ - c.remaining-- - if c.remaining == 0 { - c.cancel() - } - return c.Context.Err() -} - func TestNodeAxesLimitsAppendBudgetAndCancellation(t *testing.T) { t.Parallel() @@ -419,3 +386,36 @@ func TestNodePublicationAndReferenceRoundTripPreserveIdentity(t *testing.T) { t.Fatal("cached view bypassed cancellation", got, err) } } + +func parseNodeDocument(t *testing.T, source string) *xdm.Document { + t.Helper() + d, err := (xdm.ParseOptions{}).Parse(t.Context(), []byte(source)) + if err != nil { + t.Fatal(err) + } + return d +} + +func assertAxisNodes(t *testing.T, axis xdm.AxisType, from xdm.Node, want ...xdm.Node) { + t.Helper() + got, err := xdm.TraverseAxis(t.Context(), axis, from, 100) + if err != nil || !slices.Equal(got, want) { + t.Fatalf("%s from %T: %v,%v; want %v", axis, from, got, err, want) + } +} + +type cancelAfterErrChecks struct { + context.Context + cancel context.CancelFunc + checks int + remaining int +} + +func (c *cancelAfterErrChecks) Err() error { + c.checks++ + c.remaining-- + if c.remaining == 0 { + c.cancel() + } + return c.Context.Err() +} diff --git a/transform/internal/xdm/nodes_order_test.go b/transform/internal/xdm/nodes_order_test.go index bdd98df..486a2fd 100644 --- a/transform/internal/xdm/nodes_order_test.go +++ b/transform/internal/xdm/nodes_order_test.go @@ -255,3 +255,162 @@ func TestNodeOrderUnionAndStepResultFastPathsRespectLimits(t *testing.T) { t.Fatal("general merge ignored limit", got, err) } } + +func TestDirectNodeOrderReportsUnknownOutsideSharedTree(t *testing.T) { + t.Parallel() + + first := parseNodeDocument(t, ``) + second := parseNodeDocument(t, ``) + r, s := first.DocumentElement(), second.DocumentElement() + ns := xdm.NewNamespaceNodeWrapper(xdm.NewNamespace("p", "urn:p"), r) + otherNS := xdm.NewNamespaceNodeWrapper(xdm.NewNamespace("q", "urn:q"), s) + detachedNS := xdm.NewNamespaceNodeWrapper(xdm.NewNamespace("p", "urn:p"), nil) + otherDetachedNS := xdm.NewNamespaceNodeWrapper(xdm.NewNamespace("q", "urn:q"), nil) + detachedAttribute, err := first.CreateAttribute("detached", "value", nil) + if err != nil { + t.Fatal(err) + } + first.Freeze() + second.Freeze() + + for _, tc := range []struct { + name string + a, b xdm.Node + }{ + {"unrelated elements", r, s}, + {"unrelated namespaces", ns, otherNS}, + {"unrelated attribute and child", r.GetAttributeNodeNS("a", ""), s.FirstChild()}, + {"detached namespace and element", detachedNS, r}, + {"detached namespace and owned namespace", detachedNS, ns}, + {"distinct detached namespaces", detachedNS, otherDetachedNS}, + {"detached attribute and element", detachedAttribute, r}, + {"nil", nil, r}, + {"typed nil document", (*xdm.Document)(nil), r}, + {"typed nil namespace", (*xdm.NamespaceNodeWrapper)(nil), ns}, + {"typed nil attribute", (*xdm.Attribute)(nil), r}, + } { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + defer func() { + if value := recover(); value != nil { + t.Errorf("comparison outside a shared tree panicked: %v", value) + } + }() + + if forward, reverse := xdm.CompareNodeOrder(tc.a, tc.b), xdm.CompareNodeOrder(tc.b, tc.a); forward != 0 || reverse != 0 { + t.Fatalf("unknown order acquired node-kind-dependent direction: %d/%d", forward, reverse) + } + }) + } + + if xdm.CompareNodeOrder(r, r.FirstChild()) >= 0 || xdm.CompareNodeOrder(r.FirstChild(), r) <= 0 { + t.Fatal("shared-tree ancestor order changed") + } + + var cache xdm.DocOrderCache + cache.BuildFrom(first) + cache.BuildFrom(second) + if cache.Compare(detachedAttribute, r) != 0 || cache.Compare(r, s) >= 0 || cache.Compare(s, r) <= 0 { + t.Fatal("cache cross-document registration contract changed") + } +} + +func TestNamespaceOrderIsTotalStableAndSharedAcrossOperations(t *testing.T) { + t.Parallel() + + doc := parseNodeDocument(t, ``) + root := doc.DocumentElement() + nodes, err := xdm.TraverseAxis(t.Context(), xdm.AxisNamespace, root, 100) + requireNodeSuccess(t, err) + if len(nodes) != 4 { + t.Fatal("namespace bindings lost", len(nodes)) + } + duplicates := make([]xdm.Node, len(nodes)) + for i, n := range nodes { + duplicates[i] = xdm.NewNamespaceNodeWrapper(xdm.NewNamespace(n.Name(), xdm.StringValue(n)), root) + } + doc.Freeze() + var cache xdm.DocOrderCache + cache.BuildFrom(root) + attribute, child := root.GetAttributeNodeNS("attr", ""), root.FirstChild() + for i, a := range nodes { + for j, b := range duplicates { + want := cmp.Compare(i, j) + if got := cache.Compare(a, b); got != want || xdm.CompareNodeOrder(a, b) != want || xdm.SameNode(a, b) != (i == j) { + t.Fatalf("namespace order/identity inconsistent at %d,%d: cache=%d direct=%d same=%v", i, j, got, xdm.CompareNodeOrder(a, b), xdm.SameNode(a, b)) + } + } + for _, following := range []xdm.Node{attribute, child} { + if cache.Compare(root, a) >= 0 || cache.Compare(a, following) >= 0 || xdm.CompareNodeOrder(a, following) >= 0 { + t.Fatal("namespace node escaped its element/attribute boundary") + } + } + } + + for _, reverseOperands := range []bool{false, true} { + first := slices.Clone(nodes) + slices.Reverse(first) + second := append(slices.Clone(duplicates), attribute, child) + if reverseOperands { + first, second = second, first + } + input := append(slices.Clone(first), second...) + want := append(slices.Clone(nodes), attribute, child) + for _, operation := range []func(int) ([]xdm.Node, error){ + func(limit int) ([]xdm.Node, error) { return xdm.DeduplicateNodes(slices.Clone(input), &cache, limit) }, + func(limit int) ([]xdm.Node, error) { return xdm.MergeNodeSets(first, second, &cache, limit) }, + func(limit int) ([]xdm.Node, error) { + return xdm.UnionNodeSets(slices.Clone(input), len(first), &cache, limit) + }, + } { + got, err := operation(len(want)) + requireNodeSuccess(t, err) + if len(got) != len(want) { + t.Fatal("namespace union lost bindings or failed to deduplicate", got) + } + for i := range want { + if !xdm.SameNode(got[i], want[i]) { + t.Fatal("namespace order depends on operand order", i) + } + } + if got, err := operation(len(want) - 1); got != nil || !errors.Is(err, xdm.ErrNodeSetLimit) { + t.Fatal("namespace ordering bypassed node-set limit", got, err) + } + } + } + + var readers sync.WaitGroup + for range 8 { + readers.Go(func() { + for range 100 { + for i := range nodes { + if cache.Compare(nodes[i], duplicates[i]) != 0 || cache.Compare(nodes[0], duplicates[len(nodes)-1]) >= 0 { + t.Error("concurrent namespace ordering changed identity or direction") + } + } + } + }) + } + readers.Wait() +} + +func TestNamespaceOrderIncreasingMergeAndLocationStep(t *testing.T) { + t.Parallel() + + doc := parseNodeDocument(t, ``) + nodes, err := xdm.TraverseAxis(t.Context(), xdm.AxisNamespace, doc.DocumentElement(), 100) + requireNodeSuccess(t, err) + var cache xdm.DocOrderCache + got, err := xdm.OrderStepResult(slices.Clone(nodes), []xdm.Node{doc.DocumentElement()}, xdm.AxisNamespace, &cache, len(nodes)) + requireNodeSuccess(t, err) + if !slices.Equal(got, nodes) { + t.Fatal("namespace location step changed axis document order") + } + first, second := []xdm.Node{nodes[0], nodes[2]}, []xdm.Node{nodes[1], nodes[3]} + merged, err := xdm.UnionNodeSets(append(first, second...), len(first), &cache, len(nodes)) + requireNodeSuccess(t, err) + if !slices.Equal(merged, nodes) { + t.Fatal("increasing-run merge disagreed with namespace axis order") + } +} diff --git a/transform/internal/xdm/nodes_union.go b/transform/internal/xdm/nodes_union.go index bd13f88..30414cf 100644 --- a/transform/internal/xdm/nodes_union.go +++ b/transform/internal/xdm/nodes_union.go @@ -93,9 +93,8 @@ func inElementOrder(nodes []Node) bool { // mergeIncreasingRuns merges a and b by their sort keys (keys holds the keys // of a followed by those of b) when the keys of each operand are strictly // increasing. Strictly increasing keys mean an operand is sorted and holds no -// duplicates: every indexed node owns its own position, and the nodes that -// share one (the namespace nodes of one element, and nodes no index -// describes) never form a strictly increasing run. The merge emits a node +// duplicates: namespace prefixes break ties within one owner's namespace slot, +// while nodes no index describes cannot form a strictly increasing run. The merge emits a node // common to both operands once; two different nodes with equal keys stop the // merge. The result is then strictly increasing, which is the one order // MergeNodeSets can produce for the same node-set. @@ -108,7 +107,7 @@ func mergeIncreasingRuns(a, b []Node, keys []sortKey, maxNodes int) ([]Node, boo ka := keys[:len(a)] kb := keys[len(a):] - if !strictlyIncreasing(ka) || !strictlyIncreasing(kb) { + if !strictlyIncreasing(a, ka) || !strictlyIncreasing(b, kb) { return nil, false, nil } @@ -116,11 +115,13 @@ func mergeIncreasingRuns(a, b []Node, keys []sortKey, maxNodes int) ([]Node, boo i, j := 0, 0 for i < len(a) && j < len(b) { + order := compareNodeSortKeys(a[i], b[j], ka[i], kb[j]) + switch { - case ka[i].before(kb[j]): + case order < 0: result = append(result, a[i]) i++ - case kb[j].before(ka[i]): + case order > 0: result = append(result, b[j]) j++ case a[i] == b[j]: @@ -146,9 +147,9 @@ func mergeIncreasingRuns(a, b []Node, keys []sortKey, maxNodes int) ([]Node, boo return result, true, nil } -func strictlyIncreasing(keys []sortKey) bool { +func strictlyIncreasing(nodes []Node, keys []sortKey) bool { for i := 1; i < len(keys); i++ { - if !keys[i-1].before(keys[i]) { + if compareNodeSortKeys(nodes[i-1], nodes[i], keys[i-1], keys[i]) >= 0 { return false } } diff --git a/transform/internal/xdm/parse.go b/transform/internal/xdm/parse.go index d6bb699..3b4d94a 100644 --- a/transform/internal/xdm/parse.go +++ b/transform/internal/xdm/parse.go @@ -7,10 +7,14 @@ import ( "io" "strings" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/tree" uripath "github.com/skarm/xmlsec/transform/internal/xdm/uri" ) +// Local absolute POSIX entity bases retain the file URI authority prefix. +const localFileURIPrefix = lexicon.SchemeFile + "://" + // ParseOptions configures XML-to-XDM construction through internal/tree. // DTD resources can only use the explicitly supplied invocation opener. type ParseOptions struct { @@ -61,7 +65,8 @@ func (o ParseOptions) ParseText(ctx context.Context, text string) (*Document, er return o.wrapDocument(ctx, d) } -// ParseReader incrementally constructs XDM nodes without buffering source XML. +// ParseReader constructs an XDM view over the parsed snapshot. DTD-free mode +// parses incrementally; AllowDTD can retain decoded and expanded input buffers. func (o ParseOptions) ParseReader(ctx context.Context, r io.Reader) (*Document, error) { d, err := o.parserOptions().ParseReader(ctx, r) if err != nil { @@ -109,7 +114,7 @@ func (o ParseOptions) parserOptions() tree.ParseOptions { if uripath.IsWindowsAbsolute(options.BaseURI) { options.BaseURI = uripath.WindowsToFileURI(options.BaseURI) } else if strings.HasPrefix(options.BaseURI, "/") { - options.BaseURI = "file://" + options.BaseURI + options.BaseURI = localFileURIPrefix + options.BaseURI } if o.DTDOpen != nil { diff --git a/transform/internal/xdm/sequence_test.go b/transform/internal/xdm/sequence_test.go index ca5622e..e4cd22a 100644 --- a/transform/internal/xdm/sequence_test.go +++ b/transform/internal/xdm/sequence_test.go @@ -91,19 +91,6 @@ func TestCloneRangeKeepsLazyStorage(t *testing.T) { } } -func assertRangeInteger(t *testing.T, item xdm.Item, want int64) { - t.Helper() - - atomic, ok := item.(xdm.AtomicValue) - if !ok || atomic.TypeName != xdm.TypeInteger || atomic.BaseType != "" { - t.Fatalf("range item is not a built-in integer: %#v", item) - } - - if got, ok := atomic.Int64Val(); !ok || got != want { - t.Fatalf("range item: %v, want %d", item, want) - } -} - func BenchmarkRangeSequenceConsumption(b *testing.B) { for _, size := range []int{16, 8192} { seq := xdm.NewRangeSequence(1, int64(size)) @@ -138,28 +125,6 @@ func BenchmarkRangeSequenceConsumption(b *testing.B) { } } -// iteratorSequence exercises the generic Sequence path without relying on ItemSlice. -type iteratorSequence struct { - items xdm.ItemSlice - yielded *int -} - -func (s iteratorSequence) Len() int { return len(s.items) } -func (s iteratorSequence) Get(i int) xdm.Item { return s.items[i] } -func (s iteratorSequence) Materialize() []xdm.Item { return s.items.Materialize() } -func (s iteratorSequence) Items() iter.Seq[xdm.Item] { - return func(yield func(xdm.Item) bool) { - for _, item := range s.items { - if s.yielded != nil { - *s.yielded++ - } - if !yield(item) { - return - } - } - } -} - func TestSequenceNodeConversionRejectsMixedAndFragments(t *testing.T) { t.Parallel() @@ -238,3 +203,38 @@ func TestSequenceIteratorsStopAndCloneGenericItems(t *testing.T) { assertStringItems(t, cloned[0]) assertStringItems(t, cloned[1], "original") } + +func assertRangeInteger(t *testing.T, item xdm.Item, want int64) { + t.Helper() + + atomic, ok := item.(xdm.AtomicValue) + if !ok || atomic.TypeName != xdm.TypeInteger || atomic.BaseType != "" { + t.Fatalf("range item is not a built-in integer: %#v", item) + } + + if got, ok := atomic.Int64Val(); !ok || got != want { + t.Fatalf("range item: %v, want %d", item, want) + } +} + +// iteratorSequence exercises the generic Sequence path without relying on ItemSlice. +type iteratorSequence struct { + items xdm.ItemSlice + yielded *int +} + +func (s iteratorSequence) Len() int { return len(s.items) } +func (s iteratorSequence) Get(i int) xdm.Item { return s.items[i] } +func (s iteratorSequence) Materialize() []xdm.Item { return s.items.Materialize() } +func (s iteratorSequence) Items() iter.Seq[xdm.Item] { + return func(yield func(xdm.Item) bool) { + for _, item := range s.items { + if s.yielded != nil { + *s.yielded++ + } + if !yield(item) { + return + } + } + } +} diff --git a/transform/internal/xdm/sequence_type.go b/transform/internal/xdm/sequence_type.go index b8582ec..3945e19 100644 --- a/transform/internal/xdm/sequence_type.go +++ b/transform/internal/xdm/sequence_type.go @@ -27,6 +27,9 @@ type NameTest struct { // Local is the local name, or * for a wildcard. Local string URI string // set for URIQualifiedNames (Q{uri}local) + // URIQualified preserves an explicit Q{} namespace even when URI is empty. + // A nonempty URI remains explicit for callers constructing NameTest values. + URIQualified bool } func (NameTest) nodeTest() {} diff --git a/transform/internal/xdm/serialize.go b/transform/internal/xdm/serialize.go index 1b5e83e..45e8cd1 100644 --- a/transform/internal/xdm/serialize.go +++ b/transform/internal/xdm/serialize.go @@ -5,11 +5,24 @@ import ( "errors" "fmt" "io" + "strings" + "github.com/skarm/xmlsec/internal/lexicon" + "github.com/skarm/xmlsec/internal/nilcheck" "github.com/skarm/xmlsec/internal/tree" + "github.com/skarm/xmlsec/internal/xmlchar" "github.com/skarm/xmlsec/internal/xmloutput" ) +// XML declaration fragments preserve the exact output spelling and quoting. +// Version suffixes accept only decimal digits after the XML 1.x prefix. +const ( + xmlDeclarationVersionPrefix = "1." + xmlDeclarationDigits = "0123456789" + xmlDeclarationFormat = ""+separator); err != nil { + declaration := decl + "?>" + separator + + n, err := io.WriteString(dst, declaration) + if err != nil { return err } + if n != len(declaration) { + return io.ErrShortWrite + } + return nil } diff --git a/transform/internal/xdm/serialize_sequence.go b/transform/internal/xdm/serialize_sequence.go index 118b8c5..459d2d6 100644 --- a/transform/internal/xdm/serialize_sequence.go +++ b/transform/internal/xdm/serialize_sequence.go @@ -11,6 +11,16 @@ import ( "github.com/skarm/xmlsec/internal/tree" ) +// Sequence serialization enables compact empty elements, reconciles namespace +// bindings and normalizes xml:id. The canonical ASCII label selects the +// existing ASCII-only output option without changing codec admission. +const ( + sequenceSelfClose = true + sequenceReconcileNamespaces = true + sequenceNormalizeXMLID = true + asciiOutputEncoding = "US-ASCII" +) + // XMLItem borrows a node or supplies text to the sequence serializer. type XMLItem struct { // Node supplies a borrowed XML node. When non-nil, the serializer uses it instead of @@ -23,9 +33,9 @@ type XMLItem struct { // WriteAttributeValue applies the XML character rules to a standalone adaptive // attribute/namespace representation without constructing a containing tree. func (w XMLSerialization) WriteAttributeValue(ctx context.Context, dst io.Writer, value string) error { - _, err := tree.WriteXMLCharacters(ctx, dst, value, tree.XMLAttributeValue, int(^uint(0)>>1), w.XMLWriteOptions) + _, err := tree.WriteXMLCharacters(ctx, dst, value, tree.XMLAttributeValue, maxInt, w.XMLWriteOptions) if errors.Is(err, tree.ErrUnencodable) { - return &Error{Code: "SERE0008", Message: err.Error(), Cause: err} + return &Error{Code: lexicon.ErrSERE0008, Message: err.Error(), Cause: err} } if errors.Is(err, tree.ErrMalformed) { @@ -40,8 +50,8 @@ func (w XMLSerialization) WriteAttributeValue(ctx context.Context, dst io.Writer func (w XMLSerialization) WriteSequence(ctx context.Context, dst io.Writer, items iter.Seq2[XMLItem, error]) error { options := w.XMLWriteOptions - options.SelfClose, options.Reconcile, options.NormalizeXMLID = true, true, true - if strings.EqualFold(w.Encoding, "US-ASCII") { + options.SelfClose, options.Reconcile, options.NormalizeXMLID = sequenceSelfClose, sequenceReconcileNamespaces, sequenceNormalizeXMLID + if strings.EqualFold(w.Encoding, asciiOutputEncoding) { options.ASCII = true } @@ -79,9 +89,9 @@ func (w XMLSerialization) WriteSequence(ctx context.Context, dst io.Writer, item } } - _, err := tree.WriteXMLSequence(ctx, dst, nodes, int(^uint(0)>>1), options) + _, err := tree.WriteXMLSequence(ctx, dst, nodes, maxInt, options) if errors.Is(err, tree.ErrUnencodable) { - return &Error{Code: "SERE0008", Message: err.Error(), Cause: err} + return &Error{Code: lexicon.ErrSERE0008, Message: err.Error(), Cause: err} } if errors.Is(err, tree.ErrMalformed) { diff --git a/transform/internal/xdm/serialize_test.go b/transform/internal/xdm/serialize_test.go index 42a967c..bd54760 100644 --- a/transform/internal/xdm/serialize_test.go +++ b/transform/internal/xdm/serialize_test.go @@ -6,6 +6,9 @@ import ( "errors" "io" "iter" + "math" + "reflect" + "strings" "testing" "github.com/skarm/xmlsec/internal/tree" @@ -147,3 +150,184 @@ func TestXMLSerializationStandaloneAttributeEscaping(t *testing.T) { t.Fatalf("attribute target cause %v", err) } } + +type declarationShortWriter struct { + output bytes.Buffer + cause error + failed bool + after int +} + +func (w *declarationShortWriter) WriteString(s string) (int, error) { return w.Write([]byte(s)) } + +func (w *declarationShortWriter) Write(p []byte) (int, error) { + if w.failed { + w.after++ + } + if strings.HasPrefix(string(p), "`) + sentinel := errors.New("declaration destination failed") + for _, cause := range []error{nil, sentinel} { + out := &declarationShortWriter{cause: cause} + err := (xdm.XMLSerialization{}).WriteTo(out, doc) + want := cause + if want == nil { + want = io.ErrShortWrite + } + if !out.failed || !errors.Is(err, want) || out.after != 0 { + t.Fatalf("error=%v; want %v, declaration attempted=%v, later writes=%d", err, want, out.failed, out.after) + } + } +} + +func TestXMLDeclarationRejectsInvalidMetadataBeforeWriting(t *testing.T) { + t.Parallel() + for _, writer := range []xdm.XMLSerialization{ + {Version: `1.0" extra="yes`}, + {Version: "1."}, + {Version: "2.0"}, + {Encoding: `UTF-8" extra="yes`}, + {Encoding: "1UTF8"}, + {Encoding: "UTF 8"}, + {Standalone: `yes" extra="yes`}, + {Standalone: "YES"}, + } { + doc := parseNodeDocument(t, ``) + var output bytes.Buffer + if err := writer.WriteTo(&output, doc); err == nil || output.Len() != 0 { + t.Errorf("invalid declaration emitted %q; error=%v", output.String(), err) + } + } + for _, encoding := range []bool{false, true} { + doc := parseNodeDocument(t, ``) + if encoding { + doc.SetEncoding("bad encoding") + } else { + doc.SetVersion("1.0\" bad=\"yes") + } + var output bytes.Buffer + if err := (xdm.XMLSerialization{}).WriteTo(&output, doc); err == nil || output.Len() != 0 { + t.Error("invalid source declaration metadata was emitted", output.String(), err) + } + output.Reset() + if err := (xdm.XMLSerialization{OmitDeclaration: true}).WriteTo(&output, doc); err != nil || output.String() != "" { + t.Fatal("omitted declaration consumed unused metadata", output.String(), err) + } + } + for _, version := range []string{"1.0", "1.1", "1.23"} { + for _, encoding := range []string{"UTF-8", "ISO_8859-1", "custom.encoding"} { + doc := parseNodeDocument(t, ``) + var output bytes.Buffer + writer := xdm.XMLSerialization{Version: version, Encoding: encoding, Standalone: "no", CompactDeclaration: true} + if err := writer.WriteTo(&output, doc); err != nil || !strings.HasPrefix(output.String(), ``) { + t.Fatal("valid declaration spelling rejected", output.String(), err) + } + } + } +} + +func TestXMLSerializationRejectsTypedNilInputs(t *testing.T) { + t.Parallel() + doc := parseNodeDocument(t, ``) + var absentDocument *xdm.Document + var absentWriter *bytes.Buffer + for _, run := range []func() error{ + func() error { return (xdm.XMLSerialization{}).WriteTo(io.Discard, absentDocument) }, + func() error { return (xdm.XMLSerialization{}).WriteTo(absentWriter, doc) }, + } { + func() { + defer func() { + if value := recover(); value != nil { + t.Errorf("typed nil input panicked: %v", value) + } + }() + if err := run(); err == nil { + t.Error("typed nil input was accepted") + } + }() + } +} + +func TestIDWalkSupportsDocumentRootAndPreservesDuplicatePolicy(t *testing.T) { + t.Parallel() + doc := parseNodeDocument(t, `
`) + root := doc.DocumentElement() + root.FirstChild().NextSibling().(*xdm.Element).GetAttributeNodeNS("token", "").SetIDType(xdm.IDAttribute) + want, err := xdm.BuildIDIndex(t.Context(), root) + requireNodeSuccess(t, err) + got, err := xdm.BuildIDIndex(t.Context(), doc) + requireNodeSuccess(t, err) + if !reflect.DeepEqual(got, want) || len(got["same"]) != 2 || len(got["typed"]) != 1 || len(got["hidden"]) != 0 { + t.Fatal("document-root ID index changed completeness or ID profile", got, want) + } + for id, expected := range want { + if actual := xdm.FindElementsByID(doc, id); !reflect.DeepEqual(actual, expected) { + t.Fatal("document-root ID search disagrees with index", id, actual, expected) + } + } + for _, node := range []xdm.Node{nil, (*xdm.Document)(nil), (*xdm.Element)(nil), (*xdm.Attribute)(nil)} { + index, err := xdm.BuildIDIndex(nil, node) + if err != nil || len(index) != 0 || len(xdm.FindElementsByID(node, "same")) != 0 { + t.Fatal("nil ID root produced a result", node, index, err) + } + } + ctx, cancel := context.WithCancel(t.Context()) + mid := &cancelAfterErrChecks{Context: ctx, cancel: cancel, remaining: 2} + index, err := xdm.BuildIDIndex(mid, doc) + cancel() + if index != nil || !errors.Is(err, context.Canceled) { + t.Fatal("cancelled document walk returned partial ID index", index, err) + } +} + +func TestInScopeNamespacesIncludesImplicitXMLAndOmitsUndeclarations(t *testing.T) { + t.Parallel() + doc := parseNodeDocument(t, ``) + leaf := doc.DocumentElement().FirstChild().FirstChild().(*xdm.Element) + scope := xdm.InScopeNamespaces(leaf, false) + if len(scope) != 2 || scope["xml"] == nil || scope["xml"].URI() != tree.XMLNamespace || scope["p"] == nil || scope["p"].URI() != "urn:new" { + t.Fatal("implicit binding or namespace shadowing/undeclaration lost", scope) + } + if scope = xdm.InScopeNamespaces(leaf, true); len(scope) != 1 || scope["p"] == nil { + t.Fatal("dropXML changed non-xml scope", scope) + } + if uri, found := xdm.LookupNSPrefixURI(leaf, "xml"); found || uri != "" { + t.Fatal("lexical namespace profile gained implicit binding", uri, found) + } +} + +func TestAtomicIsNaNRecognizesSupportedFloatRepresentations(t *testing.T) { + t.Parallel() + for _, kind := range []string{xdm.TypeDouble, xdm.TypeFloat} { + for _, value := range []any{math.NaN(), float32(math.NaN()), xdm.NewDouble(math.NaN())} { + for _, custom := range []bool{false, true} { + a := xdm.AtomicValue{TypeName: kind, Value: value} + if custom { + a.TypeName, a.BaseType = "Q{u}derived", kind + } + if !a.IsNaN() || !math.IsNaN(a.ToFloat64()) { + t.Error("supported NaN representation missed", a) + } + } + } + } + for _, a := range []xdm.AtomicValue{ + {TypeName: xdm.TypeString, Value: "NaN"}, + {TypeName: xdm.TypeDouble, Value: math.Inf(1)}, + {TypeName: xdm.TypeFloat, Value: math.Copysign(0, -1)}, + {TypeName: xdm.TypeDouble, Value: xdm.NewDouble(1)}, + } { + if a.IsNaN() { + t.Error("non-NaN classified as NaN", a) + } + } +} diff --git a/transform/internal/xdm/uri/file.go b/transform/internal/xdm/uri/file.go index 0531a20..5131cec 100644 --- a/transform/internal/xdm/uri/file.go +++ b/transform/internal/xdm/uri/file.go @@ -10,9 +10,19 @@ import ( "runtime" "strings" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/xmlchar" ) +// Local file conversion accepts the localhost authority and uses these byte +// widths for UNC and slash-prefixed Windows drive forms on the Windows platform. +const ( + localFileHost = "localhost" + windowsPlatform = "windows" + uncPrefixBytes = 2 + fileDrivePrefixBytes = 3 +) + // FileURIToPath converts a local file URI to a filesystem path. // The host must be empty or localhost (case-insensitive); UNC paths, opaque // URIs and empty paths are rejected. URI escaping is decoded before conversion. @@ -22,11 +32,11 @@ func FileURIToPath(ref string) (string, error) { return "", fmt.Errorf("failed to parse file URI %q: %w", ref, err) } - if u.Scheme != "file" { + if u.Scheme != lexicon.SchemeFile { return "", fmt.Errorf("not a file URI: %q", ref) } - if u.Host != "" && !strings.EqualFold(u.Host, "localhost") { + if u.Host != "" && !strings.EqualFold(u.Host, localFileHost) { return "", fmt.Errorf("non-local file URI host %q in %q", u.Host, ref) } @@ -60,7 +70,7 @@ func FileURIToPath(ref string) (string, error) { // This is the single source of truth for the UNC rejection shared by // the explicit XPath and XSLT resource loaders. func IsUNCFileURIPath(p string) bool { - return len(p) >= 2 && isPathSep(p[0]) && isPathSep(p[1]) + return len(p) >= uncPrefixBytes && isPathSep(p[0]) && isPathSep(p[1]) } // fileURIPathFor is the OS-parameterized conversion of a "file:" URI path @@ -69,7 +79,7 @@ func IsUNCFileURIPath(p string) bool { // untouched. goos is threaded explicitly so the conversion is deterministically // testable on a non-Windows CI host. func fileURIPathFor(goos, p string) string { - if goos == "windows" && len(p) >= 3 && p[0] == '/' && xmlchar.IsASCIILetter(p[1]) && p[2] == ':' { + if goos == windowsPlatform && len(p) >= fileDrivePrefixBytes && p[0] == '/' && xmlchar.IsASCIILetter(p[1]) && p[2] == ':' { p = p[1:] } diff --git a/transform/internal/xdm/uri/resolve.go b/transform/internal/xdm/uri/resolve.go index 5e95bda..4b58ad6 100644 --- a/transform/internal/xdm/uri/resolve.go +++ b/transform/internal/xdm/uri/resolve.go @@ -14,6 +14,15 @@ import ( "github.com/skarm/xmlsec/internal/xmlchar" ) +// IRI resource escaping percent-encodes bytes at or below space and above ASCII tilde. +const ( + iriEscapedASCIIEnd = 0x20 + iriUnescapedASCIIEnd = 0x7E +) + +// These excluded ASCII punctuation bytes are percent-encoded in resource references. +const iriExcludedPunctuation = "\"<>{}|\\^`" + // ResolveSchemaURI resolves a schema-location reference ref against a base // (the FULL location of the schema that contains the reference) and returns // the canonical name to hand to the configured loader. @@ -95,7 +104,7 @@ func EscapeResourceReference(reference string) string { for index := 0; index < len(reference); index++ { value := reference[index] - if value > 0x7e || value <= 0x20 || strings.ContainsRune("\"<>{}|\\^`", rune(value)) { + if value > iriUnescapedASCIIEnd || value <= iriEscapedASCIIEnd || strings.ContainsRune(iriExcludedPunctuation, rune(value)) { if encoded.Len() == 0 { encoded.Grow(len(reference)) encoded.WriteString(reference[:index]) diff --git a/transform/internal/xdm/uri/uripath.go b/transform/internal/xdm/uri/uripath.go index 626205a..7646ee0 100644 --- a/transform/internal/xdm/uri/uripath.go +++ b/transform/internal/xdm/uri/uripath.go @@ -16,6 +16,19 @@ package uripath import ( "path" "strings" + + "github.com/skarm/xmlsec/internal/lexicon" +) + +// File-URI prefixes preserve their serialized spelling. Scheme detection requires +// at least two characters to distinguish a one-letter Windows drive; drive prefix +// widths count bytes and the filename separator is the legacy filename hint. +const ( + fileURISchemePrefix = lexicon.SchemeFile + ":" + fileURIDrivePrefix = fileURISchemePrefix + "///" + minURISchemeLength = 2 + drivePrefixBytes = 2 + filenameHintSeparator = '.' ) // IsWindowsDriveLetter reports whether b is an ASCII letter usable as a Windows @@ -30,7 +43,7 @@ func IsWindowsDriveLetter(b byte) bool { // "c:/x", "D:". A lone "C:foo" (drive-relative, no separator) does NOT match, // matching the conservative shape used elsewhere in the tree. func HasWindowsDrivePrefix(s string) bool { - if len(s) < 2 { + if len(s) < drivePrefixBytes { return false } @@ -38,11 +51,11 @@ func HasWindowsDrivePrefix(s string) bool { return false } - if len(s) == 2 { + if len(s) == drivePrefixBytes { return true } - return s[2] == '/' || s[2] == '\\' + return s[drivePrefixBytes] == '/' || s[drivePrefixBytes] == '\\' } // HasURIScheme reports whether s begins with an absolute-URI scheme followed by @@ -65,7 +78,7 @@ func HasURIScheme(s string) bool { // lowercased because URI schemes are case-insensitive (RFC 3986 §3.1), so a // caller can compare against a fixed scheme name without re-normalizing. func URIScheme(s string) string { - if len(s) < 2 || !IsWindowsDriveLetter(s[0]) { + if len(s) < minURISchemeLength || !IsWindowsDriveLetter(s[0]) { return "" } @@ -79,7 +92,7 @@ func URIScheme(s string) string { case c == ':': // A single-character scheme ("C:") is a Windows drive letter, not a // URI scheme. Require at least two scheme characters. - if i < 2 { + if i < minURISchemeLength { return "" } @@ -125,11 +138,11 @@ func IsAbsolutePath(s string) bool { func WindowsToFileURI(s string) string { slashed := ToSlash(s) if HasWindowsDrivePrefix(s) { - return "file:///" + slashed + return fileURIDrivePrefix + slashed } // UNC: "//server/share" already begins with the two slashes after // normalization, so "file:" + slashed yields "file://server/share". - return "file:" + slashed + return fileURISchemePrefix + slashed } // ToSlash replaces every backslash with a forward slash. Unlike @@ -292,7 +305,7 @@ func SlashCommonDir(a, b string) string { func containsDot(s string) bool { for i := range len(s) { - if s[i] == '.' { + if s[i] == filenameHintSeparator { return true } } diff --git a/transform/internal/xdm/values.go b/transform/internal/xdm/values.go index f2e83cd..6fcf3dc 100644 --- a/transform/internal/xdm/values.go +++ b/transform/internal/xdm/values.go @@ -16,6 +16,46 @@ import ( "github.com/skarm/xmlsec/internal/lexicon" ) +// XSD atomic type names retain their conventional qualified spelling. The +// local names are shared with other XML type consumers through lexicon. +const ( + xsdTypePrefix = lexicon.PrefixXS + ":" + alternateXSDTypePrefix = lexicon.PrefixXSD + ":" +) + +// Numeric lexical conversion uses decimal digits and shortest float64 keys. +// Nonterminating decimal fallback precision counts fractional decimal digits; +// the no-timezone marker uses a zero-second location offset but remains distinct. +const ( + decimalRadix = 10 + nonTerminatingDecimalDigits = 20 + decimalZero = "0" + decimalPoint = "." + numericKeyNotation = 'g' + shortestFloatPrecision = -1 + doubleFormatBits = 64 + noTimezoneOffsetSeconds = 0 +) + +// Canonical map-key markers distinguish absence of timezone and numeric +// infinities. Delimiters separate QName and duration components unambiguously. +const ( + noTimezoneKeyPrefix = "notz:" + noTimezoneKeyLayout = "2006-01-02T15:04:05.999999999" + qnameMapKeySeparator = "\x00" + durationMapKeySeparator = "|" + positiveInfinityMapKey = "+Inf" + negativeInfinityMapKey = "-Inf" +) + +// Collection cardinalities describe separate contracts: arrays are one-based, +// single-entry maps skip their index, and repeated parameter metadata has one type. +const ( + arrayFirstIndex = 1 + singleEntryMapSize = 1 + repeatedParameterTypeCount = 1 +) + // Type family constants used for aggregate, comparison, and collation grouping. const ( FamilyNumeric = "numeric" @@ -25,70 +65,70 @@ const ( // Atomic type name constants matching XSD types. const ( - TypeString = "xs:string" - TypeInteger = "xs:integer" - TypeDecimal = "xs:decimal" - TypeDouble = "xs:double" - TypeFloat = "xs:float" - TypeBoolean = "xs:boolean" - TypeDate = "xs:date" - TypeDateTime = "xs:dateTime" - TypeTime = "xs:time" - TypeDuration = "xs:duration" - TypeDayTimeDuration = "xs:dayTimeDuration" - TypeYearMonthDuration = "xs:yearMonthDuration" - TypeAnyURI = "xs:anyURI" - TypeQName = "xs:QName" - TypeBase64Binary = "xs:base64Binary" - TypeHexBinary = "xs:hexBinary" - TypeUntypedAtomic = "xs:untypedAtomic" - TypeAnyAtomicType = "xs:anyAtomicType" + TypeString = lexicon.XSString + TypeInteger = lexicon.XSInteger + TypeDecimal = xsdTypePrefix + lexicon.TypeDecimal + TypeDouble = xsdTypePrefix + lexicon.TypeDouble + TypeFloat = xsdTypePrefix + lexicon.TypeFloat + TypeBoolean = lexicon.XSBoolean + TypeDate = xsdTypePrefix + lexicon.TypeDate + TypeDateTime = xsdTypePrefix + lexicon.TypeDateTime + TypeTime = xsdTypePrefix + lexicon.TypeTime + TypeDuration = xsdTypePrefix + lexicon.TypeDuration + TypeDayTimeDuration = xsdTypePrefix + lexicon.TypeDayTimeDuration + TypeYearMonthDuration = xsdTypePrefix + lexicon.TypeYearMonthDuration + TypeAnyURI = xsdTypePrefix + lexicon.TypeAnyURI + TypeQName = xsdTypePrefix + lexicon.TypeQName + TypeBase64Binary = xsdTypePrefix + lexicon.TypeBase64Binary + TypeHexBinary = xsdTypePrefix + lexicon.TypeHexBinary + TypeUntypedAtomic = lexicon.XSUntypedAtomic + TypeAnyAtomicType = lexicon.XSAnyAtomicType // TypeLong and the following are derived integer types. - TypeLong = "xs:long" - TypeInt = "xs:int" - TypeShort = "xs:short" - TypeByte = "xs:byte" - TypeUnsignedLong = "xs:unsignedLong" - TypeUnsignedInt = "xs:unsignedInt" - TypeUnsignedShort = "xs:unsignedShort" - TypeUnsignedByte = "xs:unsignedByte" - TypeNonNegativeInteger = "xs:nonNegativeInteger" - TypeNonPositiveInteger = "xs:nonPositiveInteger" - TypePositiveInteger = "xs:positiveInteger" - TypeNegativeInteger = "xs:negativeInteger" + TypeLong = xsdTypePrefix + lexicon.TypeLong + TypeInt = xsdTypePrefix + lexicon.TypeInt + TypeShort = xsdTypePrefix + lexicon.TypeShort + TypeByte = xsdTypePrefix + lexicon.TypeByte + TypeUnsignedLong = xsdTypePrefix + lexicon.TypeUnsignedLong + TypeUnsignedInt = xsdTypePrefix + lexicon.TypeUnsignedInt + TypeUnsignedShort = xsdTypePrefix + lexicon.TypeUnsignedShort + TypeUnsignedByte = xsdTypePrefix + lexicon.TypeUnsignedByte + TypeNonNegativeInteger = xsdTypePrefix + lexicon.TypeNonNegativeInteger + TypeNonPositiveInteger = xsdTypePrefix + lexicon.TypeNonPositiveInteger + TypePositiveInteger = xsdTypePrefix + lexicon.TypePositiveInteger + TypeNegativeInteger = xsdTypePrefix + lexicon.TypeNegativeInteger // TypeNormalizedString and the following are derived string types. - TypeNormalizedString = "xs:normalizedString" - TypeToken = "xs:token" - TypeLanguage = "xs:language" - TypeName = "xs:Name" - TypeNCName = "xs:NCName" - TypeNMTOKEN = "xs:NMTOKEN" // #nosec G101 -- XML Schema type name, not a credential. - TypeNMTOKENS = "xs:NMTOKENS" // #nosec G101 -- XML Schema type name, not a credential. - TypeENTITY = "xs:ENTITY" - TypeID = "xs:ID" - TypeIDREF = "xs:IDREF" - TypeIDREFS = "xs:IDREFS" - TypeENTITIES = "xs:ENTITIES" + TypeNormalizedString = xsdTypePrefix + lexicon.TypeNormalizedString + TypeToken = xsdTypePrefix + lexicon.TypeToken + TypeLanguage = xsdTypePrefix + lexicon.TypeLanguage + TypeName = xsdTypePrefix + lexicon.TypeName + TypeNCName = xsdTypePrefix + lexicon.TypeNCName + TypeNMTOKEN = xsdTypePrefix + lexicon.TypeNMToken // #nosec G101 -- XML Schema type name, not a credential. + TypeNMTOKENS = xsdTypePrefix + lexicon.TypeNMTokens // #nosec G101 -- XML Schema type name, not a credential. + TypeENTITY = xsdTypePrefix + lexicon.TypeENTITY + TypeID = xsdTypePrefix + lexicon.TypeID + TypeIDREF = xsdTypePrefix + lexicon.TypeIDREF + TypeIDREFS = xsdTypePrefix + lexicon.TypeIDREFS + TypeENTITIES = xsdTypePrefix + lexicon.TypeENTITIES // TypeGDay and the following are Gregorian date part types. - TypeGDay = "xs:gDay" - TypeGMonth = "xs:gMonth" - TypeGMonthDay = "xs:gMonthDay" - TypeGYear = "xs:gYear" - TypeGYearMonth = "xs:gYearMonth" + TypeGDay = xsdTypePrefix + lexicon.TypeGDay + TypeGMonth = xsdTypePrefix + lexicon.TypeGMonth + TypeGMonthDay = xsdTypePrefix + lexicon.TypeGMonthDay + TypeGYear = xsdTypePrefix + lexicon.TypeGYear + TypeGYearMonth = xsdTypePrefix + lexicon.TypeGYearMonth // TypeDateTimeStamp and the following are other derived types. - TypeDateTimeStamp = "xs:dateTimeStamp" - TypeError = "xs:error" - TypeNumeric = "xs:numeric" + TypeDateTimeStamp = xsdTypePrefix + lexicon.TypeDateTimeStamp + TypeError = xsdTypePrefix + lexicon.TypeError + TypeNumeric = xsdTypePrefix + lexicon.TypeNumeric // TypeAnyType and the following are non-atomic / structural types. - TypeAnyType = "xs:anyType" - TypeAnySimpleType = "xs:anySimpleType" - TypeUntyped = "xs:untyped" - TypeNOTATION = "xs:NOTATION" + TypeAnyType = lexicon.XSAnyType + TypeAnySimpleType = xsdTypePrefix + lexicon.TypeAnySimpleType + TypeUntyped = lexicon.XSUntyped + TypeNOTATION = xsdTypePrefix + lexicon.TypeNotation ) // noTimezoneName is reserved for XSD values without an explicit timezone. @@ -254,7 +294,7 @@ func (a AtomicValue) BigInt() *big.Int { case int64: return big.NewInt(v) case string: - n, ok := new(big.Int).SetString(v, 10) + n, ok := new(big.Int).SetString(v, decimalRadix) if ok { return n } @@ -341,13 +381,21 @@ func (a AtomicValue) QNameVal() QNameValue { // IsNaN returns true if the atomic value is NaN (xs:double or xs:float). func (a AtomicValue) IsNaN() bool { - if a.TypeName != TypeDouble && a.TypeName != TypeFloat { + kind := a.effectiveNumericType() + if kind != TypeDouble && kind != TypeFloat { return false } - fv, ok := a.Value.(*FloatValue) + switch value := a.Value.(type) { + case *FloatValue: + return value != nil && value.IsNaN() + case float64: + return math.IsNaN(value) + case float32: + return math.IsNaN(float64(value)) + } - return ok && fv.IsNaN() + return false } // IsNumeric returns true if the type is xs:integer (or derived), xs:decimal, xs:double, or xs:float. @@ -514,7 +562,7 @@ func FunctionParameterType(params []SequenceType, repeated bool, index int) (Seq return SequenceType{}, false } - if repeated && len(params) == 1 { + if repeated && len(params) == repeatedParameterTypeCount { return params[0], true } @@ -532,19 +580,11 @@ type MapItem struct { index map[MapKey]int // index into entries } -// SingleEntryMap creates a MapItem with exactly one entry without cloning the value. -// Used by map:entry where the value is already owned by the caller. -func SingleEntryMap(key AtomicValue, value Sequence) MapItem { - return MapItem{ - entries: []EntryView{{key: CloneMapKey(key), Value: value}}, - } -} - // NewMap creates a MapItem from a slice of MapEntry. func NewMap(entries []MapEntry) MapItem { // Fast path for single-entry maps (e.g., map:entry results): // skip the index map allocation entirely. - if len(entries) == 1 { + if len(entries) == singleEntryMapSize { return MapItem{ entries: []EntryView{{key: CloneMapKey(entries[0].Key), Value: CloneSequence(entries[0].Value)}}, } @@ -562,6 +602,14 @@ func NewMap(entries []MapEntry) MapItem { return m } +// SingleEntryMap creates a MapItem with exactly one entry without cloning the value. +// Used by map:entry where the value is already owned by the caller. +func SingleEntryMap(key AtomicValue, value Sequence) MapItem { + return MapItem{ + entries: []EntryView{{key: CloneMapKey(key), Value: value}}, + } +} + // MergeMaps merges multiple maps according to the given policy. func MergeMaps(maps []MapItem, policy MergePolicy) (MapItem, error) { var allEntries []EntryView @@ -580,7 +628,7 @@ func MergeMaps(maps []MapItem, policy MergePolicy) (MapItem, error) { continue case MergeReject: return MapItem{}, &Error{ - Code: "FOJS0003", + Code: lexicon.ErrFOJS0003, Message: fmt.Sprintf("duplicate key in map merge: %v", e.key.Value), } case MergeCombine: @@ -611,7 +659,7 @@ func MergeMaps(maps []MapItem, policy MergePolicy) (MapItem, error) { func (m MapItem) Get(key AtomicValue) (Sequence, bool) { if m.index == nil { // Single-entry fast path: compare directly - if len(m.entries) == 1 && NormalizeMapKey(m.entries[0].key) == NormalizeMapKey(key) { + if len(m.entries) == singleEntryMapSize && NormalizeMapKey(m.entries[0].key) == NormalizeMapKey(key) { return CloneSequence(m.entries[0].Value), true } @@ -633,7 +681,7 @@ func (m MapItem) Get(key AtomicValue) (Sequence, bool) { // value's length before deciding whether to clone/materialize it. func (m MapItem) GetView(key AtomicValue) (Sequence, bool) { if m.index == nil { - if len(m.entries) == 1 && NormalizeMapKey(m.entries[0].key) == NormalizeMapKey(key) { + if len(m.entries) == singleEntryMapSize && NormalizeMapKey(m.entries[0].key) == NormalizeMapKey(key) { return m.entries[0].Value, true } @@ -672,7 +720,7 @@ func (m MapItem) Put(key AtomicValue, value Sequence) MapItem { // Contains returns true if the map contains the given key. func (m MapItem) Contains(key AtomicValue) bool { if m.index == nil { - return len(m.entries) == 1 && NormalizeMapKey(m.entries[0].key) == NormalizeMapKey(key) + return len(m.entries) == singleEntryMapSize && NormalizeMapKey(m.entries[0].key) == NormalizeMapKey(key) } _, ok := m.index[NormalizeMapKey(key)] @@ -749,7 +797,7 @@ func (m MapItem) EntriesView() []EntryView { func (m MapItem) Remove(key AtomicValue) MapItem { nk := NormalizeMapKey(key) if m.index == nil { - if len(m.entries) == 1 && NormalizeMapKey(m.entries[0].key) == nk { + if len(m.entries) == singleEntryMapSize && NormalizeMapKey(m.entries[0].key) == nk { return MapItem{} } @@ -842,12 +890,12 @@ func NormalizeMapKey(key AtomicValue) MapKey { return MapKey{typeName: tn, value: v.UTC().Format(time.RFC3339Nano)} } - return MapKey{typeName: tn, value: "notz:" + v.Format("2006-01-02T15:04:05.999999999")} + return MapKey{typeName: tn, value: noTimezoneKeyPrefix + v.Format(noTimezoneKeyLayout)} case []byte: return MapKey{typeName: tn, value: hex.EncodeToString(v)} case QNameValue: // QName equality is based on URI and local name, not prefix - return MapKey{typeName: tn, value: v.URI + "\x00" + v.Local} + return MapKey{typeName: tn, value: v.URI + qnameMapKeySeparator + v.Local} case Duration: // Duration holds a *big.Rat (FracSec) whose pointer breaks Go map // equality, and Seconds (a float64) can carry an exact fractional part @@ -856,9 +904,9 @@ func NormalizeMapKey(key AtomicValue) MapKey { // seconds (whole seconds plus the rational fraction). DurationToRat // folds the sign into each rational, and big.Rat normalizes -0 to 0, // so -PT0S and PT0S produce the same key. - months := DurationToRat(v, true) - secs := DurationToRat(v, false) - canon := months.RatString() + "|" + secs.RatString() + months := DurationToRat(v, durationMonthsMode) + secs := DurationToRat(v, durationSecondsMode) + canon := months.RatString() + durationMapKeySeparator + secs.RatString() // The Go value is unambiguously a duration, so fold every duration-derived // key (built-in or schema-derived through any number of levels) to // TypeDuration. This catches schema types whose BaseType is itself another @@ -869,93 +917,6 @@ func NormalizeMapKey(key AtomicValue) MapKey { } } -func normalizeNumericMapKey(key AtomicValue) MapKey { - // Fast path: integer keys stored as int64 — use directly as map key - // to avoid big.Rat/RatString overhead. This is safe because int64 - // has Go map equality and any numeric type that equals this integer - // will also be detected as an exact integer below. - if v, ok := key.Value.(int64); ok { - return MapKey{typeName: FamilyNumeric, value: v} - } - // Exact numeric types must normalize with exact arithmetic, never float64: - // ToFloat64 overflows values > MaxFloat64 to +Inf, collapsing distinct keys. - // Also consult BaseType so a user-defined schema type derived from - // xs:integer/xs:decimal is normalized exactly, and never via float64. - if IsIntegerDerived(key.TypeName) || IsIntegerDerived(key.BaseType) { - bi := key.BigInt() - if bi.IsInt64() { - return MapKey{typeName: FamilyNumeric, value: bi.Int64()} - } - - return MapKey{typeName: FamilyNumeric, value: new(big.Rat).SetInt(bi).RatString()} - } - - if key.TypeName == TypeDecimal || key.BaseType == TypeDecimal { - r := key.BigRat() - if r.IsInt() && r.Num().IsInt64() { - return MapKey{typeName: FamilyNumeric, value: r.Num().Int64()} - } - - return MapKey{typeName: FamilyNumeric, value: new(big.Rat).Set(r).RatString()} - } - // Remaining numerics are xs:float / xs:double (and user-defined types whose - // effective base is a float/double). ToFloat64 keys off TypeName only, so a - // schema-derived double/float (custom TypeName, BaseType=xs:double/xs:float) - // would collapse to 0. Promote to the built-in base type first so ToFloat64 - // reads the underlying FloatValue exactly. - f := PromoteSchemaType(key).ToFloat64() - // xs:float keys are compared in single precision: a plain float64/float32 - // backing (schema-derived or directly constructed) may not yet be rounded, - // so collapse to single precision before keying. Otherwise 16777217 and - // 16777216 — equal as xs:float — would land in distinct slots. - if key.effectiveNumericType() == TypeFloat { - f = float64(float32(f)) - } - - if math.IsNaN(f) { - return MapKey{typeName: FamilyNumeric, value: "NaN"} - } - - if math.IsInf(f, 1) { - return MapKey{typeName: FamilyNumeric, value: "+Inf"} - } - - if math.IsInf(f, -1) { - return MapKey{typeName: FamilyNumeric, value: "-Inf"} - } - // Remaining numerics are xs:float / xs:double (and user-defined types whose - // underlying value is a float). Build an exact rational and use the int64 - // fast path ONLY when the value is an exact integer that fits in int64. - // A naive "f <= math.MaxInt64" guard is unsafe: math.MaxInt64 (2^63-1) - // rounds UP to 2^63 as a float64, so xs:double(2^63) would pass the check - // and int64(f) would overflow-wrap to -2^63, colliding with xs:integer(-2^63). - // Routing through the rational avoids that: r.Num().IsInt64() is exact. - r := new(big.Rat).SetFloat64(f) - if r == nil { - // SetFloat64 returns nil for NaN/Inf; those are handled above, but - // guard defensively so a stray non-finite value still produces a key. - return MapKey{typeName: FamilyNumeric, value: strconv.FormatFloat(f, 'g', -1, 64)} - } - - if r.IsInt() && r.Num().IsInt64() { - return MapKey{typeName: FamilyNumeric, value: r.Num().Int64()} - } - - return MapKey{typeName: FamilyNumeric, value: r.RatString()} -} - -func isStringMapKey(key AtomicValue) bool { - return key.TypeName == TypeUntypedAtomic || key.TypeName == TypeAnyURI || IsStringDerived(key.TypeName) || key.BaseType == TypeAnyURI || IsStringDerived(key.BaseType) -} - -func mapKeyTypeName(typeName, baseType string) string { - if typeName == TypeDuration || typeName == TypeYearMonthDuration || typeName == TypeDayTimeDuration || baseType == TypeDuration || baseType == TypeYearMonthDuration || baseType == TypeDayTimeDuration { - return TypeDuration - } - - return typeName -} - // MergePolicy controls how duplicate keys are handled in MergeMaps. type MergePolicy int @@ -1004,7 +965,7 @@ func (b *MapBuilder) Add(key AtomicValue, value Sequence) error { return nil case MergeReject: return &Error{ - Code: "FOJS0003", + Code: lexicon.ErrFOJS0003, Message: fmt.Sprintf("duplicate key in map merge: %v", key.Value), } case MergeCombine: @@ -1073,14 +1034,14 @@ func NewArrayFromImmutableMembers(members []Sequence) ArrayItem { // Get returns the member at the given 1-based index. func (a ArrayItem) Get(index int) (Sequence, error) { - if index < 1 || index > len(a.members) { + if index < arrayFirstIndex || index > len(a.members) { return nil, &Error{ Code: lexicon.ErrFOAY0001, Message: fmt.Sprintf("array index %d out of bounds (size %d)", index, len(a.members)), } } - return CloneSequence(a.members[index-1]), nil + return CloneSequence(a.members[index-arrayFirstIndex]), nil } // Size returns the number of members. @@ -1090,7 +1051,7 @@ func (a ArrayItem) Size() int { // Put returns a new array with the member at the given 1-based index replaced. func (a ArrayItem) Put(index int, value Sequence) (ArrayItem, error) { - if index < 1 || index > len(a.members) { + if index < arrayFirstIndex || index > len(a.members) { return ArrayItem{}, &Error{ Code: lexicon.ErrFOAY0001, Message: fmt.Sprintf("array index %d out of bounds (size %d)", index, len(a.members)), @@ -1099,7 +1060,7 @@ func (a ArrayItem) Put(index int, value Sequence) (ArrayItem, error) { newMembers := make([]Sequence, len(a.members)) copy(newMembers, a.members) - newMembers[index-1] = CloneSequence(value) + newMembers[index-arrayFirstIndex] = CloneSequence(value) return ArrayItem{members: newMembers}, nil } @@ -1127,14 +1088,14 @@ func (a ArrayItem) MembersView() []Sequence { // GetView returns the member at the given 1-based index without cloning. // Callers must not mutate the returned sequence. func (a ArrayItem) GetView(index int) (Sequence, error) { - if index < 1 || index > len(a.members) { + if index < arrayFirstIndex || index > len(a.members) { return nil, &Error{ Code: lexicon.ErrFOAY0001, Message: fmt.Sprintf("array index %d out of bounds (size %d)", index, len(a.members)), } } - return a.members[index-1], nil + return a.members[index-arrayFirstIndex], nil } // SubArray returns a new array from start to end (1-based, inclusive). @@ -1143,10 +1104,10 @@ func (a ArrayItem) SubArray(start, length int) (ArrayItem, error) { // Bounds are checked without forming start+length, which can overflow for // huge positions and bypass the guard, leading to a make([]Sequence, length) // panic. With start>=1 established first, size-start+1 cannot overflow. - if start < 1 || start > size+1 || length < 0 || length > size-start+1 { + if start < arrayFirstIndex || start > size+arrayFirstIndex || length < 0 || length > size-start+arrayFirstIndex { code := lexicon.ErrFOAY0001 - if start >= 1 && start <= size+1 && length < 0 { - code = "FOAY0002" + if start >= arrayFirstIndex && start <= size+arrayFirstIndex && length < 0 { + code = lexicon.ErrFOAY0002 } return ArrayItem{}, &Error{ @@ -1156,7 +1117,7 @@ func (a ArrayItem) SubArray(start, length int) (ArrayItem, error) { } newMembers := make([]Sequence, length) - copy(newMembers, a.members[start-1:start-1+length]) + copy(newMembers, a.members[start-arrayFirstIndex:start-arrayFirstIndex+length]) return ArrayItem{members: newMembers}, nil } @@ -1209,12 +1170,11 @@ func (a ArrayItem) Flatten() Sequence { func (ArrayItem) itemTag() {} -// CloneSequence returns a deep copy of the Sequence. Bounded call sites charge -// its length against OpLimit before materializing it. It then deep-copies each item -// so that pointer-backed atomic values (*big.Int, *big.Rat, *FloatValue, -// []byte) and nested maps/arrays are not shared with the source. This preserves -// XPath 3.1 value semantics: mutating a value after it was inserted into a -// map/array must not change the stored map/array. +// CloneSequence copies a materialized sequence using CloneItem: mutable atomic +// payloads and item metadata slices are copied, while immutable maps, arrays, +// ranges and shared node/function identities retain CloneItem's ownership rules. +// Immutable range descriptors are returned without materializing their items. +// Bounded call sites charge the sequence length before materialization. func CloneSequence(seq Sequence) Sequence { if seq == nil { return nil @@ -1350,11 +1310,11 @@ func DecimalToString(r *big.Rat) string { n, exact := r.FloatPrec() if !exact { // Not exactly representable in decimal; use high precision - s := r.FloatString(20) + s := r.FloatString(nonTerminatingDecimalDigits) - s = strings.TrimRight(s, "0") - if strings.HasSuffix(s, ".") { - s += "0" + s = strings.TrimRight(s, decimalZero) + if strings.HasSuffix(s, decimalPoint) { + s += decimalZero } return s @@ -1362,16 +1322,16 @@ func DecimalToString(r *big.Rat) string { s := r.FloatString(n) - s = strings.TrimRight(s, "0") - if strings.HasSuffix(s, ".") { - s += "0" + s = strings.TrimRight(s, decimalZero) + if strings.HasSuffix(s, decimalPoint) { + s += decimalZero } return s } // NoTimezone constructs the location for a timezone-less XSD date/time. -func NoTimezone() *time.Location { return time.FixedZone(noTimezoneName, 0) } +func NoTimezone() *time.Location { return time.FixedZone(noTimezoneName, noTimezoneOffsetSeconds) } // HasTimezone reports whether a value carries an explicit timezone. func HasTimezone(t time.Time) bool { return t.Location().String() != noTimezoneName } @@ -1406,7 +1366,7 @@ func SchemaAnnotationParts(name string) (local, ns string, ok bool) { return name[end+1:], name[2:end], true } - if strings.HasPrefix(name, "xs:") || strings.HasPrefix(name, "xsd:") { + if strings.HasPrefix(name, xsdTypePrefix) || strings.HasPrefix(name, alternateXSDTypePrefix) { return "", "", false } @@ -1421,6 +1381,100 @@ func SchemaAnnotationParts(name string) (local, ns string, ok bool) { return name, "", true } +func normalizeNumericMapKey(key AtomicValue) MapKey { + // Fast path: integer keys stored as int64 — use directly as map key + // to avoid big.Rat/RatString overhead. This is safe because int64 + // has Go map equality and any numeric type that equals this integer + // will also be detected as an exact integer below. + if v, ok := key.Value.(int64); ok { + return MapKey{typeName: FamilyNumeric, value: v} + } + // Exact numeric types must normalize with exact arithmetic, never float64: + // ToFloat64 overflows values > MaxFloat64 to +Inf, collapsing distinct keys. + // Also consult BaseType so a user-defined schema type derived from + // xs:integer/xs:decimal is normalized exactly, and never via float64. + if IsIntegerDerived(key.TypeName) || IsIntegerDerived(key.BaseType) { + bi := key.BigInt() + if bi.IsInt64() { + return MapKey{typeName: FamilyNumeric, value: bi.Int64()} + } + + return MapKey{typeName: FamilyNumeric, value: new(big.Rat).SetInt(bi).RatString()} + } + + if key.TypeName == TypeDecimal || key.BaseType == TypeDecimal { + r := key.BigRat() + if r.IsInt() && r.Num().IsInt64() { + return MapKey{typeName: FamilyNumeric, value: r.Num().Int64()} + } + + return MapKey{typeName: FamilyNumeric, value: new(big.Rat).Set(r).RatString()} + } + // Remaining numerics are xs:float / xs:double (and user-defined types whose + // effective base is a float/double). ToFloat64 keys off TypeName only, so a + // schema-derived double/float (custom TypeName, BaseType=xs:double/xs:float) + // would collapse to 0. Promote to the built-in base type first so ToFloat64 + // reads the underlying FloatValue exactly. + f := PromoteSchemaType(key).ToFloat64() + // xs:float keys are compared in single precision: a plain float64/float32 + // backing (schema-derived or directly constructed) may not yet be rounded, + // so collapse to single precision before keying. Otherwise 16777217 and + // 16777216 — equal as xs:float — would land in distinct slots. + if key.effectiveNumericType() == TypeFloat { + f = float64(float32(f)) + } + + if math.IsNaN(f) { + return MapKey{typeName: FamilyNumeric, value: lexicon.FloatNaN} + } + + if math.IsInf(f, positiveSign) { + return MapKey{typeName: FamilyNumeric, value: positiveInfinityMapKey} + } + + if math.IsInf(f, negativeSign) { + return MapKey{typeName: FamilyNumeric, value: negativeInfinityMapKey} + } + // Remaining numerics are xs:float / xs:double (and user-defined types whose + // underlying value is a float). Build an exact rational and use the int64 + // fast path ONLY when the value is an exact integer that fits in int64. + // A naive "f <= math.MaxInt64" guard is unsafe: math.MaxInt64 (2^63-1) + // rounds UP to 2^63 as a float64, so xs:double(2^63) would pass the check + // and int64(f) would overflow-wrap to -2^63, colliding with xs:integer(-2^63). + // Routing through the rational avoids that: r.Num().IsInt64() is exact. + r := new(big.Rat).SetFloat64(f) + if r == nil { + // SetFloat64 returns nil for NaN/Inf; those are handled above, but + // guard defensively so a stray non-finite value still produces a key. + return MapKey{typeName: FamilyNumeric, value: strconv.FormatFloat(f, numericKeyNotation, shortestFloatPrecision, doubleFormatBits)} + } + + if r.IsInt() && r.Num().IsInt64() { + return MapKey{typeName: FamilyNumeric, value: r.Num().Int64()} + } + + return MapKey{typeName: FamilyNumeric, value: r.RatString()} +} + +func isStringMapKey(key AtomicValue) bool { + return key.TypeName == TypeUntypedAtomic || key.TypeName == TypeAnyURI || IsStringDerived(key.TypeName) || key.BaseType == TypeAnyURI || IsStringDerived(key.BaseType) +} + +func mapKeyTypeName(typeName, baseType string) string { + if !IsKnownXSDType(typeName) && IsKnownXSDType(baseType) { + typeName = baseType + } + + switch typeName { + case TypeDateTimeStamp: + return TypeDateTime + case TypeDuration, TypeYearMonthDuration, TypeDayTimeDuration: + return TypeDuration + } + + return typeName +} + func computeIsSubtypeOf(actualType, targetType string) bool { // xs:numeric is a union of xs:integer, xs:decimal, xs:float, xs:double if targetType == TypeNumeric { diff --git a/transform/internal/xdm/xmlwriter/doc.go b/transform/internal/xdm/xmlwriter/doc.go index 1da41f6..8d49f9c 100644 --- a/transform/internal/xdm/xmlwriter/doc.go +++ b/transform/internal/xdm/xmlwriter/doc.go @@ -6,9 +6,10 @@ // style and XML version before the first write. // // StartDocument, StartElement and the Write methods emit content. EndElement -// and EndDocument finish open structures. Errors are sticky: check Writer.Error -// after a sequence of calls. A failed operation can leave partial output, and -// the writer never closes the destination. +// and EndDocument finish open structures. Destination errors are sticky: check +// Writer.Error after a sequence of calls. Validation errors leave the writer +// usable. A failed write can leave partial output, and the writer never closes +// the destination. // // Writer retains open-element and namespace state, so memory use depends on // nesting and declarations. A Writer is not safe for concurrent use, and copies diff --git a/transform/internal/xdm/xmlwriter/lifecycle_test.go b/transform/internal/xdm/xmlwriter/lifecycle_test.go index faf4c5b..e097693 100644 --- a/transform/internal/xdm/xmlwriter/lifecycle_test.go +++ b/transform/internal/xdm/xmlwriter/lifecycle_test.go @@ -55,6 +55,51 @@ func TestDocumentDeclarationAndAutomaticConstructClosing(t *testing.T) { } } +func TestDocumentDeclarationSetsItsOwnCharacterPolicy(t *testing.T) { + t.Parallel() + + for _, reuse := range []bool{false, true} { + var output strings.Builder + w := stream.NewWriter(&output).XMLVersion("1.1") + if reuse { + writeSteps(t, func() error { return w.StartDocument("1.1", "", "") }, func() error { return w.WriteElement("root", "\x01") }, w.EndDocument) + output.Reset() + } + writeSteps(t, func() error { return w.StartDocument("1.0", "", "") }) + before := output.String() + if err := w.WriteElement("root", "\x01"); err == nil || output.String() != before { + t.Fatalf("XML 1.0 declaration retained XML 1.1 controls (reuse=%v): %q, %v", reuse, output.String(), err) + } + writeSteps(t, func() error { return w.WriteElement("root", "safe") }, w.EndDocument) + if output.String() != "\nsafe\n" { + t.Fatal("rejected control changed subsequent output", output.String()) + } + } +} + +func TestRejectedDocumentDeclarationPreservesCharacterPolicy(t *testing.T) { + t.Parallel() + + for _, invalidEncoding := range []string{"bad encoding", "X-No-Such-Encoding"} { + var output strings.Builder + w := stream.NewWriter(&output) + if err := w.StartDocument("1.1", invalidEncoding, ""); err == nil || output.Len() != 0 { + t.Fatal("invalid encoding accepted", output.String(), err) + } + if err := w.WriteElement("root", "\x01"); err == nil || output.Len() != 0 { + t.Fatal("rejected XML 1.1 declaration changed the XML 1.0 policy", output.String(), err) + } + w = stream.NewWriter(&output).XMLVersion("1.1") + if err := w.StartDocument("1.0", invalidEncoding, ""); err == nil || output.Len() != 0 { + t.Fatal("invalid encoding accepted", output.String(), err) + } + writeSteps(t, func() error { return w.WriteElement("root", "\x01") }) + if output.String() != "" { + t.Fatal("rejected XML 1.0 declaration changed the XML 1.1 policy", output.String()) + } + } +} + func TestWriterQuoteEscapingRawOutputAndFullEnd(t *testing.T) { t.Parallel() @@ -200,3 +245,42 @@ func TestFlushFailureIsStickyAndDestinationRemainsBorrowed(t *testing.T) { t.Fatal("destination used after sticky failure or closed by borrower", dst.flushes, dst.closes, dst.String()) } } + +func TestStickyDestinationErrorsPrecedeEmptyNames(t *testing.T) { + t.Parallel() + + for _, operation := range []struct { + name string + call func(*stream.Writer) error + }{ + {"StartElement", func(w *stream.Writer) error { return w.StartElement("") }}, + {"StartAttribute", func(w *stream.Writer) error { return w.StartAttribute("") }}, + {"StartPI", func(w *stream.Writer) error { return w.StartPI("") }}, + {"StartDTD", func(w *stream.Writer) error { return w.StartDTD("", "", "") }}, + } { + t.Run(operation.name, func(t *testing.T) { + t.Parallel() + + failure := errors.New("destination failed before empty name") + dst := &failingWriter{err: failure} + w := stream.NewWriter(dst) + if err := w.StartElement("root"); !errors.Is(err, failure) { + t.Fatal(err) + } + calls := dst.calls + if err := operation.call(&w); !errors.Is(err, failure) || dst.calls != calls || !errors.Is(w.Error(), failure) { + t.Fatalf("empty name replaced sticky failure or wrote again: %v, calls=%d; want %d", err, dst.calls, calls) + } + + var healthyOutput strings.Builder + healthy := stream.NewWriter(&healthyOutput) + if err := operation.call(&healthy); err == nil || healthyOutput.Len() != 0 || healthy.Error() != nil { + t.Fatalf("healthy validation changed state/output: %v, %q, sticky=%v", err, healthyOutput.String(), healthy.Error()) + } + writeSteps(t, func() error { return healthy.WriteElement("root", "safe") }) + if healthyOutput.String() != "safe" { + t.Fatal("healthy writer did not recover", healthyOutput.String()) + } + }) + } +} diff --git a/transform/internal/xdm/xmlwriter/stream.go b/transform/internal/xdm/xmlwriter/stream.go index 0c5c87e..52112f4 100644 --- a/transform/internal/xdm/xmlwriter/stream.go +++ b/transform/internal/xdm/xmlwriter/stream.go @@ -17,6 +17,56 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm/encoding" ) +// Writer defaults and reserved XML syntax keep quote selection separate from PI +// target validation. Namespace revisions start nonzero to track binding-index updates. +const ( + doubleQuoteChar = '"' + singleQuoteChar = '\'' + defaultQuoteChar = doubleQuoteChar + xmlVersionPrefix = "1." + initialNamespaceRevision = 1 +) + +// Declaration and namespace prefixes retain the writer's exact spacing and spelling. +const ( + xmlDeclarationVersionStart = "" + cdataReopen = cdataEnd + cdataStart + cdataClosingBrackets = len(cdataEnd) - 1 +) + +// XML 1.1 scalar bounds enforce its Char production; byte thresholds select +// candidates that require checking or escaping before UTF-8 decoding. +const ( + xml11FirstChar = 0x1 + xml11FirstRangeEnd = 0xD7FF + xml11PrivateStart = 0xE000 + xml11PrivateEnd = 0xFFFD + xml11SupplementaryStart = 0x10000 + xml11C0ByteLimit = 0x20 + xml11SpecialByteStart = 0x7F +) + var errNilOutputWriter = errors.New("stream: output writer is nil") // Writer writes XML incrementally to an io.Writer. @@ -125,7 +175,7 @@ type nsEntry struct { func NewWriter(w io.Writer) Writer { return Writer{ out: w, - quoteChar: '"', + quoteChar: defaultQuoteChar, state: stateNone, } } @@ -142,7 +192,7 @@ func (w Writer) Indent(indent string) Writer { // quote character set to q. Must be '"' or '\”. Any other value // is silently ignored. The default is '"'. func (w Writer) QuoteChar(q byte) Writer { - if q == '\'' || q == '"' { + if q == singleQuoteChar || q == doubleQuoteChar { w.quoteChar = q } @@ -159,13 +209,13 @@ func (w Writer) QuoteChar(q byte) Writer { // need not call this explicitly; it is required only when the declaration // is omitted. func (w Writer) XMLVersion(v string) Writer { - w.xml11 = v == "1.1" + w.xml11 = v == lexicon.XMLVersion11 return w } -// Error returns the sticky error, if any. Once an error occurs, all -// subsequent write operations become no-ops. The error is also returned -// by [Writer.Flush]. +// Error returns the sticky destination error, if any. Once a write or flush +// fails, subsequent write operations become no-ops. Validation errors leave +// the writer usable. The sticky error is also returned by [Writer.Flush]. func (w *Writer) Error() error { return w.err } // --- Document lifecycle --- @@ -182,7 +232,7 @@ func (w *Writer) StartDocument(version, enc, standalone string) error { } if version == "" { - version = "1.0" + version = lexicon.XMLVersion10 } // Validate version and standalone before writing, so an untrusted value // cannot inject markup into (or otherwise corrupt) the XML declaration. @@ -190,13 +240,10 @@ func (w *Writer) StartDocument(version, enc, standalone string) error { return fmt.Errorf("stream: invalid XML version %q", version) } - if standalone != "" && standalone != "yes" && standalone != "no" { + if standalone != "" && standalone != lexicon.ValueYes && standalone != lexicon.ValueNo { return fmt.Errorf("stream: invalid standalone value %q (want \"yes\", \"no\", or empty)", standalone) } - if version == "1.1" { - w.xml11 = true - } // Validate the encoding name BEFORE writing any output: first against the XML // EncName production (so a syntactically malformed value like "utf 8" cannot // be emitted raw into the declaration), then for actual support. encoding.Load @@ -212,20 +259,22 @@ func (w *Writer) StartDocument(version, enc, standalone string) error { } } - w.writeStr("`). func isValidXMLVersion(v string) bool { - rest, ok := strings.CutPrefix(v, "1.") + rest, ok := strings.CutPrefix(v, xmlVersionPrefix) if !ok { return false } @@ -306,13 +355,13 @@ func isValidXMLVersion(v string) bool { // StartElement opens a new element with the given local name. func (w *Writer) StartElement(name string) error { - if name == "" { - return errors.New("stream: element name must not be empty") - } - if w.err != nil { return w.err } + + if name == "" { + return errors.New("stream: element name must not be empty") + } // Validate the name before touching any writer state or output, so an // untrusted name cannot inject markup (extra attributes, '>') or produce // malformed XML. Namespaced names (prefix:local) and the xml:/xmlns @@ -531,13 +580,13 @@ func (w *Writer) WriteElementNS(prefix, localName, namespaceURI, content string) // StartAttribute opens an attribute on the current element. func (w *Writer) StartAttribute(name string) error { - if name == "" { - return errors.New("stream: attribute name must not be empty") - } - if w.err != nil { return w.err } + + if name == "" { + return errors.New("stream: attribute name must not be empty") + } // Validate the name before writing, so an untrusted name cannot inject // markup (extra attributes, '>') into the start tag. The xmlns/xmlns: // declaration idiom and namespaced names are valid QNames and remain @@ -867,16 +916,16 @@ func (w *Writer) WriteComment(content string) error { // StartPI opens a processing instruction (") + w.writeStr(cdataEnd) w.popState(stateText) return w.err @@ -1025,7 +1074,7 @@ func (w *Writer) WriteCDATA(content string) error { } for { - idx := strings.Index(content, "]]>") + idx := strings.Index(content, cdataEnd) if idx < 0 { break } @@ -1034,7 +1083,7 @@ func (w *Writer) WriteCDATA(content string) error { return err } - if err := w.WriteString(content[:idx+2]); err != nil { + if err := w.WriteString(content[:idx+cdataClosingBrackets]); err != nil { return err } @@ -1042,7 +1091,7 @@ func (w *Writer) WriteCDATA(content string) error { return err } // Continue with ">" onwards in the next CDATA section - content = content[idx+2:] + content = content[idx+cdataClosingBrackets:] } if err := w.StartCDATA(); err != nil { @@ -1061,14 +1110,14 @@ func (w *Writer) WriteCDATA(content string) error { // StartDTD opens a DOCTYPE declaration. pubid and sysid may be empty. // If pubid is non-empty, sysid must also be non-empty. func (w *Writer) StartDTD(name, pubid, sysid string) error { - if name == "" { - return errors.New("stream: DTD name must not be empty") - } - if w.err != nil { return w.err } + if name == "" { + return errors.New("stream: DTD name must not be empty") + } + if w.depth != 0 || (w.state != stateDocument && w.state != stateNone && w.state != stateText) { return errors.New("stream: StartDTD called in invalid state") } @@ -1159,9 +1208,7 @@ func (w *Writer) WriteDTD(name, pubid, sysid string) error { return w.EndDTD() } -// isPubidChar reports whether r is a valid XML PubidChar (XML 1.0 §2.3): -// -// PubidChar ::= #x20 | #xD | #xA | [a-zA-Z0-9] | [-'()+,./:=?;!*#@$_%] +// validatePubid checks every character against XML 1.0 §2.3 PubidChar. func validatePubid(s string) bool { return xmloutput.ValidPublicID(s) } func validateSystemID(s string) bool { return xmloutput.ValidSystemID(s) } @@ -1239,34 +1286,34 @@ func (w *Writer) writeEscaped(s string, escape escapeMode) { switch s[i] { case '&': - replacement = "&" + replacement = lexicon.EscapeAmpersand case '<': - replacement = "<" + replacement = lexicon.EscapeLessThan case '>': - replacement = ">" + replacement = lexicon.EscapeGreaterThan case '\r': - replacement = " " - case '"': - if escape == escapeAttr && w.quoteChar == '"' { - replacement = """ + replacement = escapeCarriageReturnDecimal + case doubleQuoteChar: + if escape == escapeAttr && w.quoteChar == doubleQuoteChar { + replacement = lexicon.EscapeQuote } else { writeRawByte = true } - case '\'': - if escape == escapeAttr && w.quoteChar == '\'' { - replacement = "'" + case singleQuoteChar: + if escape == escapeAttr && w.quoteChar == singleQuoteChar { + replacement = escapeApostrophe } else { writeRawByte = true } case '\n': if escape == escapeAttr { - replacement = " " + replacement = escapeLineFeedDecimal } else { writeRawByte = true } case '\t': if escape == escapeAttr { - replacement = " " + replacement = escapeTabDecimal } else { writeRawByte = true } @@ -1301,7 +1348,7 @@ func (w *Writer) writeEscaped(s string, escape escapeMode) { } func (w *Writer) writeDecimalCharRef(r rune) { - var buf [12]byte + var buf [decimalCharRefBufferBytes]byte n := len(buf) n-- @@ -1315,8 +1362,8 @@ func (w *Writer) writeDecimalCharRef(r rune) { for v > 0 { n-- - buf[n] = byte('0' + v%10) - v /= 10 + buf[n] = byte('0' + v%decimalCharRefRadix) + v /= decimalCharRefRadix } n-- @@ -1414,12 +1461,12 @@ func (w *Writer) emitPendingNS() { for i := scope.emitted; i < len(scope.decls); i++ { ns := scope.decls[i] if ns.prefix == "" { - w.writeStr(" xmlns=") + w.writeStr(defaultNamespaceStart) w.writeByte(w.quoteChar) w.writeAttrEscaped(ns.uri) w.writeByte(w.quoteChar) } else { - w.writeStr(" xmlns:") + w.writeStr(prefixedNamespaceStart) w.writeStr(ns.prefix) w.writeByte('=') w.writeByte(w.quoteChar) @@ -1478,8 +1525,8 @@ func (w *Writer) ownNSBind() map[string]string { } } - w.nsBind = &nsBindings{m: m, seq: 1} - w.nsSeq = 1 + w.nsBind = &nsBindings{m: m, seq: initialNamespaceRevision} + w.nsSeq = initialNamespaceRevision return m } @@ -1591,7 +1638,7 @@ func (w *Writer) validateContentChars(kind, s string) error { func writeEscapedXML11(w *Writer, s string, i int, start *int) { if w.xml11 { - if b := s[i]; b < 0x20 || b >= 0x7F { + if b := s[i]; b < xml11C0ByteLimit || b >= xml11SpecialByteStart { if r, width := utf8.DecodeRuneInString(s[i:]); xmlchar.IsXML11SerializeAsCharRef(r) { if (*start) < i { w.writeStr(s[(*start):i]) @@ -1608,9 +1655,9 @@ func writeEscapedXML11(w *Writer, s string, i int, start *int) { // isXML11Char implements the XML 1.1 Char production (excluding U+0000): // Char ::= [#x1-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]. func isXML11Char(r rune) bool { - return (r >= 0x1 && r <= 0xD7FF) || - (r >= 0xE000 && r <= 0xFFFD) || - (r >= 0x10000 && r <= 0x10FFFF) + return (r >= xml11FirstChar && r <= xml11FirstRangeEnd) || + (r >= xml11PrivateStart && r <= xml11PrivateEnd) || + (r >= xml11SupplementaryStart && r <= utf8.MaxRune) } // --- Escaping --- @@ -1634,7 +1681,7 @@ func (w *Writer) writeCDATAEscaped(s string) { for i := range len(s) { c := s[i] - if c == '>' && w.cdataBrackets >= 2 { + if c == '>' && w.cdataBrackets >= cdataClosingBrackets { // "]]>" detected: the preceding "]]" has already been // written into this section, so flush up to here, close the // section, and reopen a new one before emitting the '>'. @@ -1645,7 +1692,7 @@ func (w *Writer) writeCDATAEscaped(s string) { w.writeStr(s[start:i]) } - w.writeStr("]]> maxExactFloat64 { return nil, true, &xdm.Error{Code: errCodeFODT0002, Message: "dayTimeDuration value too large for exact division"} } @@ -636,24 +667,24 @@ func resultDurationType(a, b string) string { func arithmeticDurationFloat(op TokenType, typeName string, d xdm.Duration, n float64) (xdm.Sequence, error) { if math.IsNaN(n) { - return nil, &xdm.Error{Code: "FOCA0005", Message: "NaN in duration arithmetic"} + return nil, &xdm.Error{Code: errCodeFOCA0005, Message: "NaN in duration arithmetic"} } if op == TokenDiv && n == 0 { return nil, &xdm.Error{Code: errCodeFODT0002, Message: "division of duration by zero"} } - if op == TokenStar && math.IsInf(n, 0) { + if op == TokenStar && math.IsInf(n, eitherInfinitySign) { return nil, &xdm.Error{Code: errCodeFODT0002, Message: textDurationOverflow} } - if op == TokenStar && n == 0 || op == TokenDiv && math.IsInf(n, 0) { + if op == TokenStar && n == 0 || op == TokenDiv && math.IsInf(n, eitherInfinitySign) { return xdm.SingleAtomic(xdm.AtomicValue{TypeName: typeName, Value: xdm.Duration{}}), nil } value := float64(d.Months) if typeName == xdm.TypeDayTimeDuration { - value, _ = xdm.DurationToRat(d, false).Float64() + value, _ = xdm.DurationToRat(d, durationSecondsMode).Float64() } else if d.Negative { value = -value } @@ -664,7 +695,7 @@ func arithmeticDurationFloat(op TokenType, typeName string, d xdm.Duration, n fl value /= n } - if math.IsInf(value, 0) || math.IsNaN(value) { + if math.IsInf(value, eitherInfinitySign) || math.IsNaN(value) { return nil, &xdm.Error{Code: errCodeFODT0002, Message: textDurationOverflow} } @@ -687,7 +718,7 @@ func durationFromExactSeconds(typeName string, seconds *big.Rat) xdm.Sequence { } func durationFromRoundedMonths(value float64) (xdm.Sequence, error) { - rounded, _ := new(big.Float).SetFloat64(roundHalfUpFloat(value, 0)).Int(nil) + rounded, _ := new(big.Float).SetFloat64(roundHalfUpFloat(value, wholeMonthPrecision)).Int(new(big.Int)) return durationFromMonthCount(rounded) } diff --git a/transform/internal/xpath/atomize.go b/transform/internal/xpath/atomize.go index e28cb1b..5c7c908 100644 --- a/transform/internal/xpath/atomize.go +++ b/transform/internal/xpath/atomize.go @@ -10,22 +10,19 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) -// --- NodeItem --- +// Single-item atomization admits one array member holding one atomic item; +// the array API uses a one-based origin. Base-type lookup follows at most +// thirty-two host-supplied ancestry links before declining a concrete type. +const ( + singletonAtomizationMembers = 1 + atomizationArrayFirstIndex = 1 + singletonAtomizationItems = 1 + maxAtomizedBaseTypeDepth = 32 +) // --- AtomicValue --- - // key: [2]string{actual,target} → bool -// --- Supporting Types --- - -// --- FunctionItem --- - -// --- MapItem --- - -// --- ArrayItem --- - -// --- Atomization --- - // AtomizeItem converts a single item to an atomic value per XPath 3.1 Section 2.6.2. func AtomizeItem(item xdm.Item) (xdm.AtomicValue, error) { switch v := item.(type) { @@ -42,9 +39,9 @@ func AtomizeItem(item xdm.Item) (xdm.AtomicValue, error) { } } - if v.Size() == 1 { - member, _ := v.Get(1) - if xdm.SequenceLen(member) == 1 { + if v.Size() == singletonAtomizationMembers { + member, _ := v.Get(atomizationArrayFirstIndex) + if xdm.SequenceLen(member) == singletonAtomizationItems { return AtomizeItem(member.Get(0)) } } @@ -75,7 +72,7 @@ func resolveQNameFromNode(s string, node xdm.Node, noDefaultNS bool) (xdm.QNameV scope = node.Parent() } - if prefix == "xml" { + if prefix == lexicon.PrefixXML { // The "xml" prefix is predeclared (XML Namespaces) and need not be bound on // any node — matches xsd.resolveLexicalQName, so e.g. xml:lang atomizes as a // valid xs:QName in an assertion instead of failing with an undeclared prefix. @@ -221,7 +218,7 @@ func atomizedTypeForAnnotation(annotation string, decls SchemaDeclarations) stri } current := annotation - for range 32 { + for range maxAtomizedBaseTypeDepth { local, ns, ok := xdm.SchemaAnnotationParts(current) if !ok { return "" diff --git a/transform/internal/xpath/budget.go b/transform/internal/xpath/budget.go index 764f64e..89b4bcc 100644 --- a/transform/internal/xpath/budget.go +++ b/transform/internal/xpath/budget.go @@ -86,12 +86,16 @@ func FunctionsFromContext(ctx context.Context) map[QualifiedName]Function { // ChargeTree bounds constructed trees, including temporary variable trees. func ChargeTree(ctx context.Context, node xdm.Node) error { + // One operation starts the whole tree walk; node and byte limits below + // charge individual nodes separately. + const constructedTreeBaseWork = 1 + b := getBudget(ctx) if b == nil || node == nil { return nil } - if err := Charge(ctx, 1); err != nil { + if err := Charge(ctx, constructedTreeBaseWork); err != nil { return err } diff --git a/transform/internal/xpath/budget_numeric.go b/transform/internal/xpath/budget_numeric.go index 356af77..7eb72f0 100644 --- a/transform/internal/xpath/budget_numeric.go +++ b/transform/internal/xpath/budget_numeric.go @@ -8,9 +8,46 @@ import ( "strconv" "strings" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/transform/internal/xdm" ) +// Representation sizes count magnitude bytes, with a one-byte minimum per +// integer component. IEEE storage sizes stay separate from accounting quanta: +// numeric work charges per eight BYTES, while preflight words contain 64 BITS. +// These are logical representation costs, not measured Go allocation sizes. +const ( + bitsPerByte = 8 + minimumNumericBytes = 1 + singleNumericStorageBytes = 4 + doubleNumericStorageBytes = 8 + numericChargeBytes = 8 + minimumNumericWork = 1 + numericWorkWordBits = 64 + minimumPreflightWords = 1 + constructedDurationWork = 1 + additionCarryBits = 1 + numericDecimalRadix = 10 + numericParseBits = 64 +) + +// Numeric preflight retains its rational upper estimate 3322/1000 for log2(10), +// ceiling offset and independent overflow margin. Round temporaries keep their +// existing spare-bit bounds; a binary64 subnormal denominator can need 1075 bits. +const ( + decimalBitEstimateNumerator = 3322 + decimalBitEstimateDenominator = 1000 + decimalBitCeilOffset = 999 + numericLexicalOverflowMargin = 1000 + decimalPowerExtraBits = 1 + decimalRoundSpareBits = 3 + integerRoundSpareBits = 1 + minimumRationalDenominatorBits = 1 + invalidNumericBitCount = -1 + doubleRationalMarginBits = xdm.PrecisionDouble + 1 + doubleMaxRationalComponentBits = 1075 +) + // NumericSize measures unsigned binary magnitudes, with at least one byte per // integer component (including zero). Decimals count numerator AND denominator; // IEEE float/double values count four/eight bytes. It never formats a number. @@ -29,9 +66,9 @@ func NumericSize(value xdm.AtomicValue) (int, bool) { r := value.BigRat() return magnitudeBytes(r.Num().BitLen()) + magnitudeBytes(r.Denom().BitLen()), true case xdm.TypeFloat: - return 4, true + return singleNumericStorageBytes, true case xdm.TypeDouble: - return 8, true + return doubleNumericStorageBytes, true } return 0, false @@ -45,7 +82,7 @@ func ChargeNumber(ctx context.Context, value xdm.AtomicValue) error { return ctx.Err() } - if err := Charge(ctx, max(1, n/8)); err != nil { + if err := Charge(ctx, max(minimumNumericWork, n/numericChargeBytes)); err != nil { return err } @@ -69,17 +106,17 @@ func CastAtomicContext(ctx context.Context, value xdm.AtomicValue, target string err = preflightNumericLexical(ctx, source.StringVal(), target) case source.TypeName == xdm.TypeFloat || source.TypeName == xdm.TypeDouble: f := source.ToFloat64() - if !math.IsNaN(f) && !math.IsInf(f, 0) { + if !math.IsNaN(f) && !math.IsInf(f, eitherInfinitySign) { _, exponent := math.Frexp(f) if target == xdm.TypeDecimal { - err = preflightNumeric(ctx, max(0, exponent)+53, max(1, 54-exponent)) + err = preflightNumeric(ctx, max(0, exponent)+xdm.PrecisionDouble, max(minimumRationalDenominatorBits, doubleRationalMarginBits-exponent)) } else { err = preflightNumeric(ctx, max(0, exponent)) } } default: if n, numeric := NumericSize(source); numeric { - err = preflightNumeric(ctx, n*8) + err = preflightNumeric(ctx, n*bitsPerByte) } } @@ -94,12 +131,42 @@ func CastAtomicContext(ctx context.Context, value xdm.AtomicValue, target string result, err := CastAtomic(value, target) if err == nil { - err = ChargeNumber(ctx, result) + err = chargeConstructedAtomic(ctx, result) } return result, err } +// Duration values use the same canonical lexical size as public result checks. +// Input length is not that size: long fractional zeros can reduce to PT0S. +func chargeConstructedAtomic(ctx context.Context, value xdm.AtomicValue) error { + switch value.TypeName { + case xdm.TypeDuration, xdm.TypeDayTimeDuration, xdm.TypeYearMonthDuration: + if err := Charge(ctx, constructedDurationWork); err != nil { + return err + } + + text, err := CastAtomic(value, xdm.TypeString) + if err != nil { + return err + } + + return ChargeString(ctx, len(text.StringVal())) + default: + return ChargeNumber(ctx, value) + } +} + +// boundedDurationResult applies the canonical size guard before publishing a +// newly constructed duration from an aggregate or timezone accessor. +func boundedDurationResult(ctx context.Context, value xdm.AtomicValue) (xdm.Sequence, error) { + if err := chargeConstructedAtomic(ctx, value); err != nil { + return nil, err + } + + return xdm.SingleAtomic(value), nil +} + func int64Bits(n int64) int { u := uint64(n) // #nosec G115 -- Unsigned conversion and negation compute the magnitude even for MinInt64. if n < 0 { @@ -109,7 +176,9 @@ func int64Bits(n int64) int { return bits.Len64(u) } -func magnitudeBytes(n int) int { return max(1, n/8+boolInt(n%8 != 0)) } +func magnitudeBytes(n int) int { + return max(minimumNumericBytes, n/bitsPerByte+boolInt(n%bitsPerByte != 0)) +} func boolInt(b bool) int { if b { @@ -146,7 +215,7 @@ func preflightNumeric(ctx context.Context, componentBits ...int) error { } remaining -= n - words := max(1, size/64+boolInt(size%64 != 0)) + words := max(minimumPreflightWords, size/numericWorkWordBits+boolInt(size%numericWorkWordBits != 0)) available := b.operations - b.usedOperations - work if words > available/words { @@ -161,7 +230,7 @@ func preflightNumeric(ctx context.Context, componentBits ...int) error { func addBits(a, b int) int { if a < 0 || b < 0 || a > math.MaxInt-b { - return -1 + return invalidNumericBitCount } return a + b @@ -180,7 +249,7 @@ func preflightInteger(ctx context.Context, op TokenType, a, b *big.Int) error { switch op { case TokenPlus, TokenMinus: - size = addBits(max(size, b.BitLen()), 1) + size = addBits(max(size, b.BitLen()), additionCarryBits) case TokenStar: size = productBits(a, b) case TokenDiv: @@ -197,7 +266,7 @@ func preflightDecimal(ctx context.Context, op TokenType, a, b *big.Rat) error { switch op { case TokenPlus, TokenMinus: - numerator = addBits(max(productBits(a.Num(), b.Denom()), productBits(b.Num(), a.Denom())), 1) + numerator = addBits(max(productBits(a.Num(), b.Denom()), productBits(b.Num(), a.Denom())), additionCarryBits) denominator = productBits(a.Denom(), b.Denom()) case TokenStar: numerator, denominator = productBits(a.Num(), b.Num()), productBits(a.Denom(), b.Denom()) @@ -209,29 +278,30 @@ func preflightDecimal(ctx context.Context, op TokenType, a, b *big.Rat) error { } func preflightRound(ctx context.Context, value xdm.AtomicValue, scale int) error { - if scale < 0 || scale > (math.MaxInt-999)/3322 { + if scale < 0 || scale > (math.MaxInt-decimalBitCeilOffset)/decimalBitEstimateNumerator { return ErrBudget } - powerBits := (scale*3322+999)/1000 + 1 + powerBits := (scale*decimalBitEstimateNumerator+decimalBitCeilOffset)/decimalBitEstimateDenominator + decimalPowerExtraBits if value.TypeName == xdm.TypeDecimal { r := value.BigRat() - return preflightNumeric(ctx, addBits(r.Num().BitLen(), powerBits+3), addBits(r.Denom().BitLen(), powerBits)) + return preflightNumeric(ctx, addBits(r.Num().BitLen(), powerBits+decimalRoundSpareBits), addBits(r.Denom().BitLen(), powerBits)) } if xdm.IsIntegerDerived(value.TypeName) { - return preflightNumeric(ctx, addBits(value.BigInt().BitLen(), powerBits+1)) + return preflightNumeric(ctx, addBits(value.BigInt().BitLen(), powerBits+integerRoundSpareBits)) } - return preflightNumeric(ctx, addBits(powerBits, 1075)) + return preflightNumeric(ctx, addBits(powerBits, doubleMaxRationalComponentBits)) } -// lexicalNumberBits uses a safe rational upper approximation to log2(10). +// preflightNumericLexical bounds numeric storage using a safe rational upper +// approximation to log2(10). // Leading integer zeroes are ignored. Decimal denominator scale is counted // even when normalization could subsequently cancel factors. func preflightNumericLexical(ctx context.Context, source, target string) error { - source = strings.Trim(source, " \t\r\n") + source = strings.Trim(source, lexicon.XMLWhitespace) source = strings.TrimPrefix(strings.TrimPrefix(source, "+"), "-") if strings.ContainsAny(source, "eE") { @@ -241,20 +311,20 @@ func preflightNumericLexical(ctx context.Context, source, target string) error { whole, fraction, decimal := strings.Cut(source, ".") digits := len(strings.TrimLeft(whole, "0")) + len(fraction) - if digits > (math.MaxInt-1000)/3322 || len(fraction) > (math.MaxInt-1000)/3322 { + if digits > (math.MaxInt-numericLexicalOverflowMargin)/decimalBitEstimateNumerator || len(fraction) > (math.MaxInt-numericLexicalOverflowMargin)/decimalBitEstimateNumerator { return ErrBudget } - numerator := (digits*3322 + 999) / 1000 + numerator := (digits*decimalBitEstimateNumerator + decimalBitCeilOffset) / decimalBitEstimateDenominator if fraction == "" { - if number, err := strconv.ParseUint(whole, 10, 64); err == nil { + if number, err := strconv.ParseUint(whole, numericDecimalRadix, numericParseBits); err == nil { numerator = bits.Len64(number) } } if decimal || target == xdm.TypeDecimal { - return preflightNumeric(ctx, numerator, (len(fraction)*3322+999)/1000+1) + return preflightNumeric(ctx, numerator, (len(fraction)*decimalBitEstimateNumerator+decimalBitCeilOffset)/decimalBitEstimateDenominator+decimalPowerExtraBits) } return preflightNumeric(ctx, numerator) diff --git a/transform/internal/xpath/cast.go b/transform/internal/xpath/cast.go index 84e9a32..2ae5764 100644 --- a/transform/internal/xpath/cast.go +++ b/transform/internal/xpath/cast.go @@ -19,6 +19,23 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm/lexical" ) +// Cast dispatch uses XML Schema editions and qualified type names, independently +// of the XPath language edition. Integer sign restrictions keep exact bounds. +const ( + schemaEdition10 = 10 + xsdCastTypePrefix = lexicon.PrefixXS + ":" + integerZeroBound = 0 + integerPositiveMinimum = 1 + integerNegativeMaximum = -1 + booleanTrueDigit = "1" + booleanFalseDigit = "0" + // The leap-year probe validates the rest of an overflowing Gregorian year. + gregorianLeapYearProbe = "2000" + minimumSignedYearBytes = 5 + positiveZeroTimezone = "+00:00" + negativeZeroTimezone = "-00:00" +) + // msgDateTimeStampTimezone is the FORG0001 message for an xs:dateTimeStamp // value that is missing its mandatory timezone. const msgDateTimeStampTimezone = "xs:dateTimeStamp requires a timezone" @@ -89,12 +106,12 @@ func CastFromString(s string, targetType string) (xdm.AtomicValue, error) { return xdm.AtomicValue{TypeName: targetType, Value: s}, nil case xdm.TypeNormalizedString: return castDerivedString(s, targetType) - case "xs:anyType", "xs:untyped": + case xdm.TypeAnyType, xdm.TypeUntyped: return xdm.AtomicValue{TypeName: xdm.TypeUntypedAtomic, Value: s}, nil } // XSD whiteSpace applies only to XML S, not all Unicode whitespace. - trimmed := strings.Trim(s, " \t\r\n") + trimmed := strings.Trim(s, lexicon.XMLWhitespace) switch targetType { case xdm.TypeInteger, xdm.TypeDecimal, xdm.TypeDouble, xdm.TypeFloat, xdm.TypeBoolean: @@ -144,7 +161,7 @@ func CastFromString(s string, targetType string) (xdm.AtomicValue, error) { } func validateVersionedCast(ctx context.Context, source xdm.AtomicValue, target string) error { - if XMLSchemaVersion(ctx) != 10 { + if XMLSchemaVersion(ctx) != schemaEdition10 { return nil } @@ -157,7 +174,7 @@ func validateVersionedCast(ctx context.Context, source xdm.AtomicValue, target s case xdm.TypeDouble, xdm.TypeFloat, xdm.TypeDate, xdm.TypeDateTime, xdm.TypeDateTimeStamp, xdm.TypeGYear, xdm.TypeGYearMonth, xdm.TypeAnyURI: text := collapseXMLWhitespace(source.StringVal()) - if err := lexical.ValidateBuiltin(text, strings.TrimPrefix(target, "xs:"), lexical.Version10); err != nil { + if err := lexical.ValidateBuiltin(text, strings.TrimPrefix(target, xsdCastTypePrefix), lexical.Version10); err != nil { return castError(text, target) } } @@ -182,7 +199,7 @@ func castToNumeric(ctx context.Context, value xdm.AtomicValue) (xdm.AtomicValue, func isAbstractCastTarget(typeName string) bool { return typeName == xdm.TypeAnyAtomicType || - typeName == "xs:anySimpleType" || + typeName == xdm.TypeAnySimpleType || typeName == xdm.TypeNOTATION } @@ -387,27 +404,27 @@ func integerTypeRange(typeName string) (minVal *big.Int, maxVal *big.Int) { case xdm.TypeLong: return big.NewInt(math.MinInt64), big.NewInt(math.MaxInt64) case xdm.TypeInt: - return big.NewInt(-2147483648), big.NewInt(2147483647) + return big.NewInt(math.MinInt32), big.NewInt(math.MaxInt32) case xdm.TypeShort: - return big.NewInt(-32768), big.NewInt(32767) + return big.NewInt(math.MinInt16), big.NewInt(math.MaxInt16) case xdm.TypeByte: - return big.NewInt(-128), big.NewInt(127) + return big.NewInt(math.MinInt8), big.NewInt(math.MaxInt8) case xdm.TypeUnsignedLong: - return big.NewInt(0), new(big.Int).SetUint64(math.MaxUint64) + return big.NewInt(integerZeroBound), new(big.Int).SetUint64(math.MaxUint64) case xdm.TypeUnsignedInt: - return big.NewInt(0), big.NewInt(4294967295) + return big.NewInt(integerZeroBound), big.NewInt(math.MaxUint32) case xdm.TypeUnsignedShort: - return big.NewInt(0), big.NewInt(65535) + return big.NewInt(integerZeroBound), big.NewInt(math.MaxUint16) case xdm.TypeUnsignedByte: - return big.NewInt(0), big.NewInt(255) + return big.NewInt(integerZeroBound), big.NewInt(math.MaxUint8) case xdm.TypeNonNegativeInteger: - return big.NewInt(0), nil + return big.NewInt(integerZeroBound), nil case xdm.TypeNonPositiveInteger: - return nil, big.NewInt(0) + return nil, big.NewInt(integerZeroBound) case xdm.TypePositiveInteger: - return big.NewInt(1), nil + return big.NewInt(integerPositiveMinimum), nil case xdm.TypeNegativeInteger: - return nil, big.NewInt(-1) + return nil, big.NewInt(integerNegativeMaximum) } return nil, nil @@ -460,9 +477,9 @@ func castNumericLexical(s, targetType string) (xdm.AtomicValue, error) { return castStringToFloat(s) default: switch s { - case lexicon.ValueTrue, "1": + case lexicon.ValueTrue, booleanTrueDigit: return xdm.AtomicValue{TypeName: targetType, Value: true}, nil - case lexicon.ValueFalse, "0": + case lexicon.ValueFalse, booleanFalseDigit: return xdm.AtomicValue{TypeName: targetType, Value: false}, nil default: return xdm.AtomicValue{}, castError(s, targetType) @@ -471,7 +488,7 @@ func castNumericLexical(s, targetType string) (xdm.AtomicValue, error) { } func castIntegerLexical(s, targetType string) (xdm.AtomicValue, error) { - n, ok := new(big.Int).SetString(s, 10) + n, ok := new(big.Int).SetString(s, numericDecimalRadix) if !ok { return xdm.AtomicValue{}, castError(s, targetType) } @@ -561,7 +578,7 @@ func castGregorianLexical(s, targetType string, matches func(string) bool) (xdm. if targetType == xdm.TypeGYear || targetType == xdm.TypeGYearMonth { if _, rest, err := splitXSDYear(s); errors.Is(err, errCalendarYearRange) { // Validate complete syntax before classifying representability. - probe := "2000" + rest + probe := gregorianLeapYearProbe + rest if matches(probe) && validateGregorianValue(targetType, probe) { return xdm.AtomicValue{}, &xdm.Error{Code: errCodeFODT0001, Message: errCalendarYearRange.Error()} } @@ -583,10 +600,10 @@ func castGregorianLexical(s, targetType string, matches func(string) bool) (xdm. func isIntegerSubtype(t string) bool { switch t { - case "xs:long", "xs:int", "xs:short", "xs:byte", - "xs:unsignedLong", "xs:unsignedInt", "xs:unsignedShort", "xs:unsignedByte", - "xs:positiveInteger", "xs:nonPositiveInteger", - "xs:negativeInteger", "xs:nonNegativeInteger": + case xdm.TypeLong, xdm.TypeInt, xdm.TypeShort, xdm.TypeByte, + xdm.TypeUnsignedLong, xdm.TypeUnsignedInt, xdm.TypeUnsignedShort, xdm.TypeUnsignedByte, + xdm.TypePositiveInteger, xdm.TypeNonPositiveInteger, + xdm.TypeNegativeInteger, xdm.TypeNonNegativeInteger: return true } @@ -597,31 +614,31 @@ func validateIntegerRange(n *big.Int, targetType string) error { var minVal, maxVal *big.Int switch targetType { - case "xs:long": + case xdm.TypeLong: minVal, maxVal = big.NewInt(math.MinInt64), big.NewInt(math.MaxInt64) - case "xs:int": + case xdm.TypeInt: minVal, maxVal = big.NewInt(math.MinInt32), big.NewInt(math.MaxInt32) - case "xs:short": + case xdm.TypeShort: minVal, maxVal = big.NewInt(math.MinInt16), big.NewInt(math.MaxInt16) - case "xs:byte": + case xdm.TypeByte: minVal, maxVal = big.NewInt(math.MinInt8), big.NewInt(math.MaxInt8) - case "xs:unsignedLong": - minVal = big.NewInt(0) + case xdm.TypeUnsignedLong: + minVal = big.NewInt(integerZeroBound) maxVal = new(big.Int).SetUint64(math.MaxUint64) - case "xs:unsignedInt": - minVal, maxVal = big.NewInt(0), big.NewInt(math.MaxUint32) - case "xs:unsignedShort": - minVal, maxVal = big.NewInt(0), big.NewInt(math.MaxUint16) - case "xs:unsignedByte": - minVal, maxVal = big.NewInt(0), big.NewInt(math.MaxUint8) - case "xs:positiveInteger": - minVal = big.NewInt(1) - case "xs:nonPositiveInteger": - maxVal = big.NewInt(0) - case "xs:negativeInteger": - maxVal = big.NewInt(-1) - case "xs:nonNegativeInteger": - minVal = big.NewInt(0) + case xdm.TypeUnsignedInt: + minVal, maxVal = big.NewInt(integerZeroBound), big.NewInt(math.MaxUint32) + case xdm.TypeUnsignedShort: + minVal, maxVal = big.NewInt(integerZeroBound), big.NewInt(math.MaxUint16) + case xdm.TypeUnsignedByte: + minVal, maxVal = big.NewInt(integerZeroBound), big.NewInt(math.MaxUint8) + case xdm.TypePositiveInteger: + minVal = big.NewInt(integerPositiveMinimum) + case xdm.TypeNonPositiveInteger: + maxVal = big.NewInt(integerZeroBound) + case xdm.TypeNegativeInteger: + maxVal = big.NewInt(integerNegativeMaximum) + case xdm.TypeNonNegativeInteger: + minVal = big.NewInt(integerZeroBound) default: return nil } @@ -640,7 +657,7 @@ func validateIntegerRange(n *big.Int, targetType string) error { // normalizeNegZeroYear strips leading '-' from "-0000" years (e.g. "-0000-05" → "0000-05"). // Per XSD, negative zero is identical to positive zero for year values. func normalizeNegZeroYear(s string) string { - if len(s) < 5 || s[0] != '-' { + if len(s) < minimumSignedYearBytes || s[0] != '-' { return s } // Check if all year digits are zero: "-0000..." or "-00000..." @@ -658,8 +675,8 @@ func normalizeNegZeroYear(s string) string { func normalizeZeroTimezoneLexical(s string) string { switch { - case strings.HasSuffix(s, "+00:00"), strings.HasSuffix(s, "-00:00"): - return s[:len(s)-6] + "Z" + case strings.HasSuffix(s, positiveZeroTimezone), strings.HasSuffix(s, negativeZeroTimezone): + return s[:len(s)-timezoneOffsetBytes] + utcTimezoneLexical default: return s } diff --git a/transform/internal/xpath/cast_datetime.go b/transform/internal/xpath/cast_datetime.go index 56a6e9d..22ace48 100644 --- a/transform/internal/xpath/cast_datetime.go +++ b/transform/internal/xpath/cast_datetime.go @@ -16,6 +16,70 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) +// Calendar lexical limits are separate from arithmetic movement limits. The +// year ceiling protects time.Time construction; timezone offsets are six bytes +// and XML Schema allows at most fourteen hours, with zero minutes at that bound. +const ( + minimumYearDigits = 4 + maxCastCalendarYear = 999_999_999 + minimumCalendarMonth = 1 + maximumCalendarMonth = 12 + minimumCalendarDay = 1 + maximumClockHour = 23 + maximumClockMinute = 59 + maximumClockSecond = 59 + maximumTimezoneHours = 14 + timezoneOffsetBytes = 6 + utcTimezoneLexical = "Z" + midnightRolloverDays = 1 + leapDayOfMonth = 29 + goParseReferenceYear = "2006" + leapDayMonthDay = "-02-29" + leapDayParseReplacement = "-02-28" + endOfDayClock = "24:00:00" + startOfDayClock = "00:00:00" + endOfDayDateTimeClock = "T24:00:00" + startOfDayDateTimeClock = "T00:00:00" +) + +// Go parsing layouts and canonical Gregorian printf layouts keep their exact +// reference digits and padding. Their order at each call site is significant. +const ( + gregorianDayFormat = "---%02d%s" + gregorianMonthFormat = "--%02d%s" + gregorianMonthDayFormat = "--%02d-%02d%s" + gregorianYearFormat = "%s%s" + gregorianYearMonthFormat = "%s-%02d%s" + timezoneOffsetFormat = "%c%02d:%02d" + xsdDateZoneLayout = "2006-01-02Z07:00" + xsdDateLayout = "2006-01-02" + xsdDateTimeFractionZoneLayout = "2006-01-02T15:04:05.999999999Z07:00" + xsdDateTimeZoneLayout = "2006-01-02T15:04:05Z07:00" + xsdDateTimeFractionLayout = "2006-01-02T15:04:05.999999999" + xsdDateTimeLayout = "2006-01-02T15:04:05" + xsdTimeFractionZoneLayout = "15:04:05.999999999Z07:00" + xsdTimeZoneLayout = "15:04:05Z07:00" + xsdTimeFractionLayout = "15:04:05.999999999" + xsdTimeLayout = "15:04:05" +) + +// Component ranks enforce XML Schema duration ordering, independently of unit +// conversion. Legacy float mirrors round to microseconds; exact rationals do not. +const ( + durationMonthParseBits = 64 // ParseInt month magnitudes, not IEEE storage + durationYearOrder = 1 + durationMonthOrder = 2 + durationDayOrder = 3 + durationHourOrder = 4 + durationMinuteOrder = 5 + durationSecondOrder = 6 + microsecondsPerSecond = 1e6 + durationMicrosecondFormat = "%06d" + signedInt64DecimalBytes = 20 // includes a possible minus sign + zeroYearMonthDurationSuffix = "0M" + zeroDayTimeDurationSuffix = "T0S" +) + // errDurationMonthsRange reports a duration outside the supported month range. // Lexical validation finishes before a cast raises FODT0002. var errDurationMonthsRange = errors.New("duration month count out of int range") @@ -44,15 +108,15 @@ func castToGType(v xdm.AtomicValue, targetType string) (xdm.AtomicValue, error) func formatGregorianPartial(t time.Time, targetType string) string { switch targetType { case xdm.TypeGDay: - return fmt.Sprintf("---%02d%s", t.Day(), formatXSDTimezone(t)) + return fmt.Sprintf(gregorianDayFormat, t.Day(), formatXSDTimezone(t)) case xdm.TypeGMonth: - return fmt.Sprintf("--%02d%s", t.Month(), formatXSDTimezone(t)) + return fmt.Sprintf(gregorianMonthFormat, t.Month(), formatXSDTimezone(t)) case xdm.TypeGMonthDay: - return fmt.Sprintf("--%02d-%02d%s", t.Month(), t.Day(), formatXSDTimezone(t)) + return fmt.Sprintf(gregorianMonthDayFormat, t.Month(), t.Day(), formatXSDTimezone(t)) case xdm.TypeGYear: - return fmt.Sprintf("%s%s", formatXSDYear(t.Year()), formatXSDTimezone(t)) + return fmt.Sprintf(gregorianYearFormat, formatXSDYear(t.Year()), formatXSDTimezone(t)) default: - return fmt.Sprintf("%s-%02d%s", formatXSDYear(t.Year()), t.Month(), formatXSDTimezone(t)) + return fmt.Sprintf(gregorianYearMonthFormat, formatXSDYear(t.Year()), t.Month(), formatXSDTimezone(t)) } } @@ -63,7 +127,7 @@ func formatXSDTimezone(t time.Time) string { _, offset := t.Zone() if offset == 0 { - return "Z" + return utcTimezoneLexical } sign := '+' @@ -72,10 +136,10 @@ func formatXSDTimezone(t time.Time) string { offset = -offset } - h := offset / 3600 - m := (offset % 3600) / 60 + h := offset / calendarSecondsPerHour + m := (offset % calendarSecondsPerHour) / calendarSecondsPerMinute - return fmt.Sprintf("%c%02d:%02d", sign, h, m) + return fmt.Sprintf(timezoneOffsetFormat, sign, h, m) } // splitXSDYear splits an XSD date/dateTime string into the year (as int) and @@ -98,23 +162,23 @@ func splitXSDYear(s string) (int, string, error) { } digits := i - start - if digits < 4 { + if digits < minimumYearDigits { return 0, "", fmt.Errorf("year must have at least 4 digits") } // Per XSD: years with more than 4 digits must not have leading zeros - if digits > 4 && s[start] == '0' { + if digits > minimumYearDigits && s[start] == '0' { return 0, "", fmt.Errorf("year with leading zeros is not valid") } yearStr := s[start:i] year, err := strconv.Atoi(yearStr) - if err != nil || year > 999_999_999 { + if err != nil || year > maxCastCalendarYear { // Retain the Gregorian leap-year cycle so callers can distinguish a // malformed date from a valid value outside the supported year range. year = 0 for _, digit := range yearStr { - year = (year*10 + int(digit-'0')) % 400 + year = (year*numericDecimalRadix + int(digit-'0')) % gregorianCycleYears } if neg { @@ -154,7 +218,7 @@ func buildTimeFromParts(year int, rest string, layouts []string, original string // Alternative approach: if the actual year is a leap year, temporarily // parse as Jan 1 to get the timezone, then construct manually. - synthetic := "2006" + rest + synthetic := goParseReferenceYear + rest for _, layout := range layouts { if t, err := time.Parse(layout, synthetic); err == nil { t = time.Date(year, t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), t.Location()) @@ -166,13 +230,13 @@ func buildTimeFromParts(year int, rest string, layouts []string, original string // Retry with a leap-year workaround for Feb 29: parse a modified string // with day=28, then adjust the day back to the original value. - if strings.HasPrefix(rest, "-02-29") { - modRest := "-02-28" + rest[6:] + if strings.HasPrefix(rest, leapDayMonthDay) { + modRest := leapDayParseReplacement + rest[len(leapDayMonthDay):] - synthetic = "2006" + modRest + synthetic = goParseReferenceYear + modRest for _, layout := range layouts { if t, err := time.Parse(layout, synthetic); err == nil { - t = time.Date(year, t.Month(), 29, t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), t.Location()) + t = time.Date(year, t.Month(), leapDayOfMonth, t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), t.Location()) t = ensureExplicitTZ(t, original) return t, true @@ -218,8 +282,8 @@ func parseXSDDate(s string) (time.Time, error) { } layouts := []string{ - "2006-01-02Z07:00", - "2006-01-02", + xsdDateZoneLayout, + xsdDateLayout, } if t, ok := buildTimeFromParts(year, rest, layouts, s); ok { if err := validateDateComponents(int(t.Month()), t.Day(), year); err != nil { @@ -261,10 +325,10 @@ func parseXSDDateTime(s string) (time.Time, error) { } layouts := []string{ - "2006-01-02T15:04:05.999999999Z07:00", - "2006-01-02T15:04:05Z07:00", - "2006-01-02T15:04:05.999999999", - "2006-01-02T15:04:05", + xsdDateTimeFractionZoneLayout, + xsdDateTimeZoneLayout, + xsdDateTimeFractionLayout, + xsdDateTimeLayout, } if t, ok := buildTimeFromParts(year, rest, layouts, s); ok { if err := validateDateComponents(int(t.Month()), t.Day(), year); err != nil { @@ -278,10 +342,10 @@ func parseXSDDateTime(s string) (time.Time, error) { } if isMidnight24 { - t = t.AddDate(0, 0, 1) // advance to next day + t = t.AddDate(0, 0, midnightRolloverDays) // advance to next day } - if overflow || t.Year() > 999_999_999 || t.Year() < -999_999_999 { + if overflow || t.Year() > maxCastCalendarYear || t.Year() < -maxCastCalendarYear { return time.Time{}, &xdm.Error{Code: errCodeFODT0001, Message: errCalendarYearRange.Error()} } @@ -304,10 +368,10 @@ func parseXSDTime(s string) (time.Time, error) { } for _, layout := range []string{ - "15:04:05.999999999Z07:00", - "15:04:05Z07:00", - "15:04:05.999999999", - "15:04:05", + xsdTimeFractionZoneLayout, + xsdTimeZoneLayout, + xsdTimeFractionLayout, + xsdTimeLayout, } { if t, err := time.Parse(layout, target); err == nil { if !isMidnight24 { @@ -328,7 +392,7 @@ func parseXSDTime(s string) (time.Time, error) { // a midnight-24 value. The caller must advance the date by one day. // 24:00:00.nnn is valid only when the fractional part is all zeros. func normalizeMidnight24DateTime(s string) (string, bool) { - before, rest, found := strings.Cut(s, "T24:00:00") + before, rest, found := strings.Cut(s, endOfDayDateTimeClock) if !found { return s, false } @@ -350,18 +414,18 @@ func normalizeMidnight24DateTime(s string) (string, bool) { rest = rest[i:] } - return before + "T00:00:00" + rest, true + return before + startOfDayDateTimeClock + rest, true } // normalizeMidnight24Time checks if a time string starts with 24:00:00 and // replaces it with 00:00:00. For xs:time, 24:00:00 equals 00:00:00 (no date rollover). // 24:00:00.nnn is valid only when the fractional part is all zeros. func normalizeMidnight24Time(s string) (string, bool) { - if !strings.HasPrefix(s, "24:00:00") { + if !strings.HasPrefix(s, endOfDayClock) { return s, false } - rest := s[len("24:00:00"):] + rest := s[len(endOfDayClock):] if len(rest) > 0 && rest[0] == '.' { i := 1 for i < len(rest) && rest[i] >= '0' && rest[i] <= '9' { @@ -379,7 +443,7 @@ func normalizeMidnight24Time(s string) (string, bool) { rest = rest[i:] } - return "00:00:00" + rest, true + return startOfDayClock + rest, true } func ensureExplicitTZ(t time.Time, s string) time.Time { @@ -404,8 +468,8 @@ func hasExplicitTimezone(s string) bool { return true } - if len(s) >= 6 { - tail := s[len(s)-6:] + if len(s) >= timezoneOffsetBytes { + tail := s[len(s)-timezoneOffsetBytes:] if (tail[0] == '+' || tail[0] == '-') && tail[1] >= '0' && tail[1] <= '9' && tail[2] >= '0' && tail[2] <= '9' && @@ -422,12 +486,12 @@ func hasExplicitTimezone(s string) bool { // validateDateComponents checks that month and day are in valid ranges. // XSD 1.1: year 0000 is valid. func validateDateComponents(month, day, year int) error { - if month < 1 || month > 12 { + if month < minimumCalendarMonth || month > maximumCalendarMonth { return fmt.Errorf("month %d out of range 1-12", month) } maxDay := daysInMonth(year, month) - if day < 1 || day > maxDay { + if day < minimumCalendarDay || day > maxDay { return fmt.Errorf("day %d out of range for month %d (max %d)", day, month, maxDay) } @@ -437,15 +501,15 @@ func validateDateComponents(month, day, year int) error { // validateTimeComponents checks that hours, minutes, seconds are in valid ranges. // Note: hour 24 is handled separately (normalizeMidnight24*) before this is called. func validateTimeComponents(hour, minute, second, nanosecond int) error { - if hour < 0 || hour > 23 { + if hour < 0 || hour > maximumClockHour { return fmt.Errorf("hour %d out of range 0-23", hour) } - if minute < 0 || minute > 59 { + if minute < 0 || minute > maximumClockMinute { return fmt.Errorf("minute %d out of range 0-59", minute) } - if second < 0 || second > 59 { + if second < 0 || second > maximumClockSecond { return fmt.Errorf("second %d out of range 0-59", second) } @@ -465,24 +529,24 @@ func validateTimezoneInString(s string) error { return nil // Z is always valid } - if len(s) < 6 { + if len(s) < timezoneOffsetBytes { return nil // no timezone present } - tail := s[len(s)-6:] + tail := s[len(s)-timezoneOffsetBytes:] if (tail[0] != '+' && tail[0] != '-') || tail[3] != ':' { return nil // no timezone suffix } - hh := (int(tail[1]-'0') * 10) + int(tail[2]-'0') + hh := (int(tail[1]-'0') * numericDecimalRadix) + int(tail[2]-'0') - mm := (int(tail[4]-'0') * 10) + int(tail[5]-'0') - if hh > 14 || (hh == 14 && mm != 0) { + mm := (int(tail[4]-'0') * numericDecimalRadix) + int(tail[5]-'0') + if hh > maximumTimezoneHours || (hh == maximumTimezoneHours && mm != 0) { return fmt.Errorf("timezone offset %s out of range (-14:00 to +14:00)", tail) } - if mm > 59 { + if mm > maximumClockMinute { return fmt.Errorf("timezone offset minutes %d out of range 0-59", mm) } @@ -636,23 +700,23 @@ func (parser *durationParser) addDateComponent(component durationComponent) erro switch component.designator { case 'Y': - return parser.addMonthComponent(component, 1, 12) + return parser.addMonthComponent(component, durationYearOrder, calendarMonthsPerYear) case 'M': - return parser.addMonthComponent(component, 2, 1) + return parser.addMonthComponent(component, durationMonthOrder, 1) case 'D': - if err := parser.advanceOrder(3); err != nil { + if err := parser.advanceOrder(durationDayOrder); err != nil { return err } // Day counts can exceed int64; retain their exact value in seconds. - days, ok := new(big.Int).SetString(component.number, 10) + days, ok := new(big.Int).SetString(component.number, numericDecimalRadix) if !ok { return fmt.Errorf("invalid duration number: %q", component.number) } dayFloat, _ := new(big.Float).SetInt(days).Float64() - parser.value.Seconds += dayFloat * 86400 - parser.addSeconds(new(big.Rat).SetInt(days), 86400) + parser.value.Seconds += dayFloat * calendarSecondsPerDay + parser.addSeconds(new(big.Rat).SetInt(days), calendarSecondsPerDay) return nil default: @@ -665,7 +729,7 @@ func (parser *durationParser) addMonthComponent(component durationComponent, ord return err } - n, err := strconv.ParseInt(component.number, 10, 64) + n, err := strconv.ParseInt(component.number, numericDecimalRadix, durationMonthParseBits) if errors.Is(err, strconv.ErrRange) { parser.monthsOverflow = true return nil @@ -703,7 +767,7 @@ func (parser *durationParser) addTimeComponent(component durationComponent) erro // An overflowing float mirror may saturate to infinity; the exact // rational remains valid and must not be rejected on that account. - f, err := strconv.ParseFloat(component.number, 64) + f, err := strconv.ParseFloat(component.number, doubleFloatBits) if err != nil && !errors.Is(err, strconv.ErrRange) { return fmt.Errorf("invalid duration number: %q", component.number) } @@ -772,11 +836,11 @@ func addCheckedMonths(a, b int64) (int64, bool) { func durationTimeComponent(designator byte) (int, int64) { switch designator { case 'H': - return 4, 3600 + return durationHourOrder, calendarSecondsPerHour case 'M': - return 5, 60 + return durationMinuteOrder, calendarSecondsPerMinute case 'S': - return 6, 1 + return durationSecondOrder, 1 default: return 0, 0 } @@ -807,11 +871,12 @@ func exactFractionDigits(frac *big.Rat) string { // long division. FloatString would ROUND, which can carry into the integer // part (e.g. 0.999... → 1.0) and corrupt the formatted duration; truncation // never carries, so the integer part stays 0 and no stray "." is emitted. + // Forty decimal digits cap only non-terminating duration fractions. const maxDigits = 40 num := new(big.Int).Set(frac.Num()) den := frac.Denom() - ten := big.NewInt(10) + ten := big.NewInt(numericDecimalRadix) var b strings.Builder @@ -850,9 +915,9 @@ func formatDuration(d xdm.Duration, typeName string) string { if b.Len() == 1 || (d.Negative && b.Len() == 2) { if typeName == xdm.TypeYearMonthDuration { - b.WriteString("0M") + b.WriteString(zeroYearMonthDurationSuffix) } else { - b.WriteString("T0S") + b.WriteString(zeroDayTimeDurationSuffix) } } @@ -865,12 +930,12 @@ func writeDurationMonths(b *strings.Builder, d xdm.Duration) { months = -months } - if years := months / 12; years != 0 { + if years := months / calendarMonthsPerYear; years != 0 { writeDurationInteger(b, int64(years)) b.WriteByte('Y') } - if remainder := months % 12; remainder != 0 { + if remainder := months % calendarMonthsPerYear; remainder != 0 { writeDurationInteger(b, int64(remainder)) b.WriteByte('M') } @@ -906,11 +971,11 @@ func legacyDurationSecondParts(d xdm.Duration) (*big.Int, int64, *big.Rat) { whole := int64(seconds) fraction := seconds - float64(whole) - micro := int64(math.Round(fraction * 1e6)) + micro := int64(math.Round(fraction * microsecondsPerSecond)) - if micro >= 1e6 { + if micro >= microsecondsPerSecond { whole++ - micro -= 1e6 + micro -= microsecondsPerSecond } if micro < 0 { @@ -928,11 +993,11 @@ func writeDurationDayTime(b *strings.Builder, totalSeconds *big.Int, micro int64 // The day count may exceed int64; all sub-day parts remain bounded. days := new(big.Int) remainder := new(big.Int) - days.QuoRem(totalSeconds, big.NewInt(86400), remainder) - hours := remainder.Int64() / 3600 - subHour := remainder.Int64() - hours*3600 - minutes := subHour / 60 - seconds := subHour - minutes*60 + days.QuoRem(totalSeconds, big.NewInt(calendarSecondsPerDay), remainder) + hours := remainder.Int64() / calendarSecondsPerHour + subHour := remainder.Int64() - hours*calendarSecondsPerHour + minutes := subHour / calendarSecondsPerMinute + seconds := subHour - minutes*calendarSecondsPerMinute hasFraction := micro != 0 || (fraction != nil && fraction.Sign() != 0) hasSeconds := seconds != 0 || hasFraction @@ -973,7 +1038,7 @@ func writeDurationSeconds(b *strings.Builder, whole, micro int64, fraction *big. b.WriteString(digits) } } else if micro != 0 { - digits := strings.TrimRight(fmt.Sprintf("%06d", micro), "0") + digits := strings.TrimRight(fmt.Sprintf(durationMicrosecondFormat, micro), "0") b.WriteByte('.') b.WriteString(digits) @@ -983,6 +1048,6 @@ func writeDurationSeconds(b *strings.Builder, whole, micro int64, fraction *big. } func writeDurationInteger(b *strings.Builder, value int64) { - var digits [20]byte - b.Write(strconv.AppendInt(digits[:0], value, 10)) + var digits [signedInt64DecimalBytes]byte + b.Write(strconv.AppendInt(digits[:0], value, numericDecimalRadix)) } diff --git a/transform/internal/xpath/cast_numeric.go b/transform/internal/xpath/cast_numeric.go index 24fddf2..a1ee1ef 100644 --- a/transform/internal/xpath/cast_numeric.go +++ b/transform/internal/xpath/cast_numeric.go @@ -14,6 +14,13 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) +// Numeric casts map booleans to the XML Schema numeric values, while a +// rational's denominator one remains the mathematical identity. +const ( + booleanNumericTrue = 1 + booleanNumericFalse = 0 +) + func castToDouble(v xdm.AtomicValue) (xdm.AtomicValue, error) { switch v.TypeName { case xdm.TypeDouble: @@ -34,10 +41,10 @@ func castToDouble(v xdm.AtomicValue) (xdm.AtomicValue, error) { return xdm.AtomicValue{TypeName: xdm.TypeDouble, Value: xdm.NewDouble(f)}, nil case xdm.TypeBoolean: if v.BooleanVal() { - return xdm.AtomicValue{TypeName: xdm.TypeDouble, Value: xdm.NewDouble(1)}, nil + return xdm.AtomicValue{TypeName: xdm.TypeDouble, Value: xdm.NewDouble(booleanNumericTrue)}, nil } - return xdm.AtomicValue{TypeName: xdm.TypeDouble, Value: xdm.NewDouble(0)}, nil + return xdm.AtomicValue{TypeName: xdm.TypeDouble, Value: xdm.NewDouble(booleanNumericFalse)}, nil case xdm.TypeString, xdm.TypeUntypedAtomic: return CastFromString(v.StringVal(), xdm.TypeDouble) default: @@ -61,7 +68,7 @@ func castToFloat(v xdm.AtomicValue) (xdm.AtomicValue, error) { // When casting from string, use the xs:float lexical rules directly // (XSD 1.1 accepts "INF", lexicon.FloatNegINF, "+INF", "NaN") if v.TypeName == xdm.TypeString || v.TypeName == xdm.TypeUntypedAtomic || xdm.IsStringDerived(v.TypeName) { - return castStringToFloat(strings.Trim(v.StringVal(), " \t\r\n")) + return castStringToFloat(strings.Trim(v.StringVal(), lexicon.XMLWhitespace)) } // Round exact decimal/integer values directly to binary32. Narrowing // through double can erase which side of a float midpoint they lie on. @@ -98,7 +105,7 @@ func numericToFloat32(value xdm.AtomicValue) float32 { // castStringToFloat rounds directly to binary32, avoiding double rounding. func castStringToFloat(s string) (xdm.AtomicValue, error) { - f, err := parseXPathFloating(s, 32) + f, err := parseXPathFloating(s, singleFloatBits) if err != nil { return xdm.AtomicValue{}, castError(s, xdm.TypeFloat) } @@ -112,11 +119,11 @@ func castToInteger(v xdm.AtomicValue) (xdm.AtomicValue, error) { return v, nil case xdm.TypeDouble, xdm.TypeFloat: f := v.DoubleVal() - if math.IsNaN(f) || math.IsInf(f, 0) { + if math.IsNaN(f) || math.IsInf(f, eitherInfinitySign) { return xdm.AtomicValue{}, &xdm.Error{Code: errCodeFOCA0002, Message: "cannot cast NaN/INF to xs:integer"} } - bi, _ := new(big.Float).SetFloat64(f).Int(nil) + bi, _ := new(big.Float).SetFloat64(f).Int(new(big.Int)) return xdm.AtomicValue{TypeName: xdm.TypeInteger, Value: bi}, nil case xdm.TypeDecimal: @@ -127,10 +134,10 @@ func castToInteger(v xdm.AtomicValue) (xdm.AtomicValue, error) { return xdm.AtomicValue{TypeName: xdm.TypeInteger, Value: q}, nil case xdm.TypeBoolean: if v.BooleanVal() { - return xdm.AtomicValue{TypeName: xdm.TypeInteger, Value: int64(1)}, nil + return xdm.AtomicValue{TypeName: xdm.TypeInteger, Value: int64(booleanNumericTrue)}, nil } - return xdm.AtomicValue{TypeName: xdm.TypeInteger, Value: int64(0)}, nil + return xdm.AtomicValue{TypeName: xdm.TypeInteger, Value: int64(booleanNumericFalse)}, nil default: return castStringLikeOrFail(v, xdm.TypeInteger) } @@ -148,7 +155,7 @@ func castToDecimal(v xdm.AtomicValue) (xdm.AtomicValue, error) { return xdm.AtomicValue{TypeName: xdm.TypeDecimal, Value: r}, nil case xdm.TypeDouble, xdm.TypeFloat: f := v.DoubleVal() - if math.IsNaN(f) || math.IsInf(f, 0) { + if math.IsNaN(f) || math.IsInf(f, eitherInfinitySign) { return xdm.AtomicValue{}, &xdm.Error{Code: errCodeFOCA0002, Message: "cannot cast NaN/INF to xs:decimal"} } @@ -157,10 +164,10 @@ func castToDecimal(v xdm.AtomicValue) (xdm.AtomicValue, error) { return xdm.AtomicValue{TypeName: xdm.TypeDecimal, Value: r}, nil case xdm.TypeBoolean: if v.BooleanVal() { - return xdm.AtomicValue{TypeName: xdm.TypeDecimal, Value: big.NewRat(1, 1)}, nil + return xdm.AtomicValue{TypeName: xdm.TypeDecimal, Value: big.NewRat(booleanNumericTrue, 1)}, nil } - return xdm.AtomicValue{TypeName: xdm.TypeDecimal, Value: big.NewRat(0, 1)}, nil + return xdm.AtomicValue{TypeName: xdm.TypeDecimal, Value: big.NewRat(booleanNumericFalse, 1)}, nil default: return castStringLikeOrFail(v, xdm.TypeDecimal) } diff --git a/transform/internal/xpath/cast_string.go b/transform/internal/xpath/cast_string.go index 5e10bb1..aef20fd 100644 --- a/transform/internal/xpath/cast_string.go +++ b/transform/internal/xpath/cast_string.go @@ -20,6 +20,35 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm/lexical" ) +// IEEE parsing/formatting widths are bits, not mantissa precision or accounting +// bytes. Canonical XPath float output uses decimal notation in [1e-6, 1e6), +// otherwise scientific notation; -1 requests strconv's shortest round-trip form. +const ( + singleFloatBits = 32 + doubleFloatBits = 64 + positiveInfinitySign = 1 + negativeInfinitySign = -1 + plainFloatMinimum = 0.000001 + plainFloatMaximum = 1_000_000 + plainFloatFormat = 'f' + scientificFloatFormat = 'E' + shortestFloatDigits = -1 + positiveZeroLexical = "0" + negativeZeroLexical = "-0" + scientificExponentMarker = "E" + scientificIntegralSuffix = ".0" +) + +// Temporal output layouts preserve XML Schema canonical padding, including +// nine nanosecond digits before trailing-zero removal and at least four year digits. +const ( + canonicalDateFormat = "%s-%02d-%02d" + canonicalClockFormat = "%02d:%02d:%02d" + canonicalNanosecondFormat = ".%09d" + negativeYearFormat = "-%04d" + nonnegativeYearFormat = "%04d" +) + // AtomicToString returns the canonical string representation of an atomic value. func AtomicToString(v xdm.AtomicValue) (string, error) { return atomicToString(v) @@ -76,7 +105,7 @@ func formatAtomicString(value xdm.AtomicValue) (string, error) { func formatAtomicInteger(value xdm.AtomicValue) (string, error) { switch number := value.Value.(type) { case int64: - return strconv.FormatInt(number, 10), nil + return strconv.FormatInt(number, numericDecimalRadix), nil case *big.Int: return number.String(), nil default: @@ -130,13 +159,13 @@ func formatAtomicTemporal(value xdm.AtomicValue) (string, error) { } func formatAtomicDate(instant time.Time) string { - return fmt.Sprintf("%s-%02d-%02d", formatXSDYear(instant.Year()), instant.Month(), instant.Day()) + return fmt.Sprintf(canonicalDateFormat, formatXSDYear(instant.Year()), instant.Month(), instant.Day()) } func formatAtomicClock(instant time.Time) string { - clock := fmt.Sprintf("%02d:%02d:%02d", instant.Hour(), instant.Minute(), instant.Second()) + clock := fmt.Sprintf(canonicalClockFormat, instant.Hour(), instant.Minute(), instant.Second()) if nanos := instant.Nanosecond(); nanos > 0 { - clock += strings.TrimRight(fmt.Sprintf(".%09d", nanos), "0") + clock += strings.TrimRight(fmt.Sprintf(canonicalNanosecondFormat, nanos), "0") } return clock @@ -186,7 +215,7 @@ func formatUserAtomicValue(value any) string { case string: return typed case int64: - return strconv.FormatInt(typed, 10) + return strconv.FormatInt(typed, numericDecimalRadix) case *big.Int: return typed.String() case *big.Rat: @@ -213,29 +242,29 @@ func formatXPathDouble(f float64) string { return lexicon.FloatNaN } - if math.IsInf(f, 1) { + if math.IsInf(f, positiveInfinitySign) { return lexicon.FloatINF } - if math.IsInf(f, -1) { + if math.IsInf(f, negativeInfinitySign) { return lexicon.FloatNegINF } if f == 0 { if math.Signbit(f) { - return "-0" + return negativeZeroLexical } - return "0" + return positiveZeroLexical } abs := math.Abs(f) // XPath F&O: plain decimal for abs in [1e-6, 1e6), scientific notation otherwise. - if abs >= 0.000001 && abs < 1_000_000 { - return strconv.FormatFloat(f, 'f', -1, 64) + if abs >= plainFloatMinimum && abs < plainFloatMaximum { + return strconv.FormatFloat(f, plainFloatFormat, shortestFloatDigits, doubleFloatBits) } - return canonicalScientificFloat(strconv.FormatFloat(f, 'E', -1, 64)) + return canonicalScientificFloat(strconv.FormatFloat(f, scientificFloatFormat, shortestFloatDigits, doubleFloatBits)) } // formatXPathFloat formats a float64 (representing an xs:float) using @@ -246,39 +275,39 @@ func formatXPathFloat(f float64) string { return lexicon.FloatNaN } - if math.IsInf(float64(f32), 1) { + if math.IsInf(float64(f32), positiveInfinitySign) { return lexicon.FloatINF } - if math.IsInf(float64(f32), -1) { + if math.IsInf(float64(f32), negativeInfinitySign) { return lexicon.FloatNegINF } if f32 == 0 { if math.Signbit(float64(f32)) { - return "-0" + return negativeZeroLexical } - return "0" + return positiveZeroLexical } abs := math.Abs(float64(f32)) - if abs >= 0.000001 && abs < 1_000_000 { - return strconv.FormatFloat(float64(f32), 'f', -1, 32) + if abs >= plainFloatMinimum && abs < plainFloatMaximum { + return strconv.FormatFloat(float64(f32), plainFloatFormat, shortestFloatDigits, singleFloatBits) } - return canonicalScientificFloat(strconv.FormatFloat(float64(f32), 'E', -1, 32)) + return canonicalScientificFloat(strconv.FormatFloat(float64(f32), scientificFloatFormat, shortestFloatDigits, singleFloatBits)) } // canonicalScientificFloat normalizes the exponent and requires a decimal point // in the mantissa of the scientific form produced by strconv.FormatFloat. func canonicalScientificFloat(s string) string { - if idx := strings.Index(s, "E"); idx >= 0 { + if idx := strings.Index(s, scientificExponentMarker); idx >= 0 { mantissa := s[:idx] expPart := s[idx+1:] if !strings.Contains(mantissa, ".") { - mantissa += ".0" + mantissa += scientificIntegralSuffix } expPart = strings.TrimPrefix(expPart, "+") @@ -296,7 +325,7 @@ func canonicalScientificFloat(s string) string { } } - s = mantissa + "E" + expPart + s = mantissa + scientificExponentMarker + expPart } return s @@ -308,10 +337,10 @@ func canonicalScientificFloat(s string) string { // - Years > 9999 use as many digits as needed func formatXSDYear(year int) string { if year < 0 { - return fmt.Sprintf("-%04d", -year) + return fmt.Sprintf(negativeYearFormat, -year) } - return fmt.Sprintf("%04d", year) + return fmt.Sprintf(nonnegativeYearFormat, year) } func castToString(v xdm.AtomicValue) (xdm.AtomicValue, error) { @@ -379,7 +408,7 @@ func collapseXMLWhitespace(s string) string { return xmlchar.CollapseSpace(s) } // parseXPathDouble parses an XML Schema double, using infinity on overflow. func parseXPathDouble(s string) (float64, error) { - return parseXPathFloating(s, 64) + return parseXPathFloating(s, doubleFloatBits) } func parseXPathFloating(s string, bits int) (float64, error) { @@ -388,16 +417,16 @@ func parseXPathFloating(s string, bits int) (float64, error) { } switch s { - case "INF", "+INF": - return math.Inf(1), nil + case lexicon.FloatINF, lexicon.FloatPosINF: + return math.Inf(positiveInfinitySign), nil case lexicon.FloatNegINF: - return math.Inf(-1), nil + return math.Inf(negativeInfinitySign), nil case lexicon.FloatNaN: return math.NaN(), nil } f, err := strconv.ParseFloat(s, bits) - if errors.Is(err, strconv.ErrRange) && math.IsInf(f, 0) { + if errors.Is(err, strconv.ErrRange) && math.IsInf(f, eitherInfinitySign) { return f, nil } diff --git a/transform/internal/xpath/collation.go b/transform/internal/xpath/collation.go index 0d5a477..f655e00 100644 --- a/transform/internal/xpath/collation.go +++ b/transform/internal/xpath/collation.go @@ -20,11 +20,49 @@ import ( "github.com/skarm/xmlsec/internal/lexicon" ) +// UCA query selectors keep their defined case; locale extensions enable the +// matching x/text options without changing query fallback semantics. const ( collationOptionNumeric = "numeric" collationAlternateShifted = "shifted" - maxIdleCollators = 2 - maxPooledCollationInput = 4096 +) + +const ( + collationOptionStrength = "strength" + collationOptionAlternate = "alternate" + collationOptionCaseFirst = "caseFirst" + collationOptionCaseLevel = "caseLevel" + collationOptionBackwards = "backwards" + collationOptionNormalization = "normalization" + collationOptionReorder = "reorder" + collationOptionMaxVariable = "maxVariable" + collationOptionVersion = "version" + collationOptionKeyword = "keyword" + collationOptionHiraganaQuaternary = "hiraganaQuaternary" + collationStrengthIdentical = "identical" + collationStrengthPrimary = "primary" + collationStrengthSecondary = "secondary" + collationAlternateNonIgnorable = "non-ignorable" + collationFallbackUnknown = "unknown" + collationExtensionRootTag = "und-u" + collationBackwardsExtension = "kb-true" + collationCaseLevelExtension = "kc-true" +) + +// Idle retention is bounded by two collator objects. Inputs above 4096 bytes +// drop their collator to avoid retaining enlarged internal element buffers. +const ( + maxIdleCollators = 2 + maxPooledCollationInput = 4096 +) + +// Case tie-break keys sort the preferred case before the other case after a +// zero separator from the base key. Missing substring boundaries use -1. +const ( + collationCaseKeySeparator = 0 + collationPreferredCaseWeight = 0x01 + collationOtherCaseWeight = 0x02 + collationMatchNotFound = -1 ) // caseblindCollationURI is the QT3 test suite's case-blind collation URI. @@ -143,7 +181,7 @@ func newCodepointCollation() *collationImpl { indexOf: func(s, substr string) (int, int) { idx := strings.Index(s, substr) if idx < 0 { - return -1, 0 + return collationMatchNotFound, 0 } return idx, len(substr) @@ -181,7 +219,7 @@ func newHTMLASCIICaseInsensitiveCollation() *collationImpl { idx := strings.Index(ls, lsub) if idx < 0 { - return -1, 0 + return collationMatchNotFound, 0 } return idx, len(substr) @@ -242,13 +280,13 @@ func makeUCACollation(params string) (*collationImpl, error) { } if opts.caseLevel || opts.backwards { - tagParts := []string{"und-u"} + tagParts := []string{collationExtensionRootTag} if opts.backwards { - tagParts = append(tagParts, "kb-true") + tagParts = append(tagParts, collationBackwardsExtension) } if opts.caseLevel { - tagParts = append(tagParts, "kc-true") + tagParts = append(tagParts, collationCaseLevelExtension) } if extraTag, err := language.Parse(strings.Join(tagParts, "-")); err == nil { @@ -300,7 +338,7 @@ func makeUCACollation(params string) (*collationImpl, error) { return base } - base = append(base, 0) + base = append(base, collationCaseKeySeparator) return append(base, caseKey...) } @@ -352,10 +390,10 @@ type ucaParams struct { func (p ucaParams) ignoreVariableRunes() bool { switch p.alternate { case nameBlanked: - return p.strength != "identical" + return p.strength != collationStrengthIdentical case collationAlternateShifted: switch p.strength { - case "primary", "secondary", nameTertiary: + case collationStrengthPrimary, collationStrengthSecondary, nameTertiary: return true default: return false @@ -406,19 +444,19 @@ func (p *ucaParamParser) apply(key, value string) { p.applyFallback(value) case lexicon.AttrLang: p.applyLanguage(value) - case "strength": + case collationOptionStrength: p.applyStrength(value) - case "alternate": + case collationOptionAlternate: p.applyAlternate(value) - case "caseFirst": - if value == nameUpper2 || value == nameLower { + case collationOptionCaseFirst: + if value == nameUpperCase || value == nameLower { p.params.caseFirst = value } else { p.invalid = true } - case "caseLevel", "backwards", collationOptionNumeric, "normalization": + case collationOptionCaseLevel, collationOptionBackwards, collationOptionNumeric, collationOptionNormalization: p.applyBoolean(key, value) - case "reorder", "maxVariable", "version", "keyword", "hiraganaQuaternary": + case collationOptionReorder, collationOptionMaxVariable, collationOptionVersion, collationOptionKeyword, collationOptionHiraganaQuaternary: p.unsupported = true default: p.invalid = true @@ -429,7 +467,7 @@ func (p *ucaParamParser) applyFallback(value string) { switch value { case lexicon.ValueNo: p.params.fallbackNo = true - case lexicon.ValueYes, "unknown": + case lexicon.ValueYes, collationFallbackUnknown: // Unknown values are implementation-defined unless fallback=no // explicitly requests strict rejection. default: @@ -455,10 +493,10 @@ func (p *ucaParamParser) applyStrength(value string) { p.params.strength = value switch value { - case "primary": + case collationStrengthPrimary: p.params.collateOpts = append(p.params.collateOpts, collate.IgnoreCase, collate.IgnoreDiacritics) p.params.ignoreCase = true - case "secondary": + case collationStrengthSecondary: p.params.collateOpts = append(p.params.collateOpts, collate.IgnoreCase) p.params.ignoreCase = true case nameTertiary: @@ -470,7 +508,7 @@ func (p *ucaParamParser) applyStrength(value string) { func (p *ucaParamParser) applyAlternate(value string) { switch value { - case "non-ignorable": + case collationAlternateNonIgnorable: p.params.alternate = "" case nameBlanked, collationAlternateShifted: p.params.alternate = value @@ -490,12 +528,12 @@ func (p *ucaParamParser) applyBoolean(key, value string) { enabled := value == lexicon.ValueYes switch key { - case "caseLevel": + case collationOptionCaseLevel: p.params.caseLevel = enabled if enabled { p.unsupported = true } - case "backwards": + case collationOptionBackwards: p.params.backwards = enabled case collationOptionNumeric: if enabled { @@ -503,7 +541,7 @@ func (p *ucaParamParser) applyBoolean(key, value string) { } p.params.numeric = enabled - case "normalization": + case collationOptionNormalization: if enabled { p.unsupported = true } @@ -550,23 +588,72 @@ func projectVariableRunes(s string) projectedString { func ucaIndexOf(cmp func(a, b string) int, s, substr string, numeric bool) (int, int) { start, end := ucaIndexOfRange(cmp, s, substr, numeric) if start < 0 { - return -1, 0 + return collationMatchNotFound, 0 } - sRunes := []rune(s) - bytePos := len(string(sRunes[:start])) - byteLen := len(string(sRunes[start:end])) + bytePos := ucaRuneByteOffset(s, start) + byteLen := ucaRuneByteOffset(s, end) - bytePos return bytePos, byteLen } +// ucaRuneByteOffset maps a decoded rune boundary to the original byte offset. +// A malformed UTF-8 byte occupies one source byte, even though its rune is +// RuneError. The caller supplies a boundary between zero and the rune count. +func ucaRuneByteOffset(s string, index int) int { + for offset := range s { + if index == 0 { + return offset + } + + index-- + } + + return len(s) +} + +func ucaCandidateBoundaries(s, needle string, runeCount int) []int { + if !utf8.ValidString(needle) || !utf8.ValidString(s) { + return nil + } + + if runeCount == len(s) { + return []int{} // A non-nil empty slice marks ASCII byte boundaries. + } + + boundaries := make([]int, runeCount+1) + + index := 0 + for offset := range s { + boundaries[index] = offset + index++ + } + + boundaries[index] = len(s) + + return boundaries +} + +func ucaCandidateString(s string, runes []rune, boundaries []int, start, end int) string { + if boundaries == nil { + return string(runes[start:end]) + } + + if len(boundaries) == 0 { + return s[start:end] + } + + return s[boundaries[start]:boundaries[end]] +} + func ucaIndexOfRange(cmp func(a, b string) int, s, substr string, numeric bool) (int, int) { - if substr == "" { + if substr == "" || cmp("", substr) == 0 { return 0, 0 } subRuneLen := len([]rune(substr)) sRunes := []rune(s) + boundaries := ucaCandidateBoundaries(s, substr, len(sRunes)) for i := range sRunes { // Try match lengths from subRuneLen to end of string @@ -574,7 +661,7 @@ func ucaIndexOfRange(cmp func(a, b string) int, s, substr string, numeric bool) maxLen := len(sRunes) - i for matchLen := minLen; matchLen <= maxLen; matchLen++ { - candidate := string(sRunes[i : i+matchLen]) + candidate := ucaCandidateString(s, sRunes, boundaries, i, i+matchLen) if cmp(candidate, substr) == 0 { if numeric && splitsDigitRun(sRunes, i, i+matchLen) { continue @@ -585,7 +672,7 @@ func ucaIndexOfRange(cmp func(a, b string) int, s, substr string, numeric bool) } // Also try shorter matches (for cases where collation compresses) for matchLen := minLen - 1; matchLen > 0; matchLen-- { - candidate := string(sRunes[i : i+matchLen]) + candidate := ucaCandidateString(s, sRunes, boundaries, i, i+matchLen) if cmp(candidate, substr) == 0 { if numeric && splitsDigitRun(sRunes, i, i+matchLen) { continue @@ -596,7 +683,7 @@ func ucaIndexOfRange(cmp func(a, b string) int, s, substr string, numeric bool) } } - return -1, -1 + return collationMatchNotFound, collationMatchNotFound } // splitsDigitRun returns true if the match boundary [start, end) in runes @@ -625,21 +712,22 @@ func ucaHasPrefix(cmp func(a, b string) int, s, prefix string, numeric bool) (bo return false, 0 } - sRunes := []rune(s) - - return true, len(string(sRunes[:matchEnd])) + return true, ucaRuneByteOffset(s, matchEnd) } func ucaHasPrefixRange(cmp func(a, b string) int, s, prefix string, numeric bool) (int, bool) { + if prefix == "" || cmp("", prefix) == 0 { + return 0, true + } + prefixRunes := []rune(prefix) sRunes := []rune(s) - if len(sRunes) < len(prefixRunes) { - return 0, false - } + boundaries := ucaCandidateBoundaries(s, prefix, len(sRunes)) + initialLength := min(len(prefixRunes), len(sRunes)) // Try match lengths from len(prefixRunes) upward to handle numeric expansion - for matchLen := len(prefixRunes); matchLen <= len(sRunes); matchLen++ { - candidate := string(sRunes[:matchLen]) + for matchLen := initialLength; matchLen <= len(sRunes); matchLen++ { + candidate := ucaCandidateString(s, sRunes, boundaries, 0, matchLen) if cmp(candidate, prefix) == 0 { if numeric && splitsDigitRun(sRunes, 0, matchLen) { continue @@ -649,8 +737,8 @@ func ucaHasPrefixRange(cmp func(a, b string) int, s, prefix string, numeric bool } } // Also try shorter matches - for matchLen := len(prefixRunes) - 1; matchLen > 0; matchLen-- { - candidate := string(sRunes[:matchLen]) + for matchLen := initialLength - 1; matchLen > 0; matchLen-- { + candidate := ucaCandidateString(s, sRunes, boundaries, 0, matchLen) if cmp(candidate, prefix) == 0 { if numeric && splitsDigitRun(sRunes, 0, matchLen) { continue @@ -669,23 +757,24 @@ func ucaHasSuffix(cmp func(a, b string) int, s, suffix string, numeric bool) (bo return false, 0 } - sRunes := []rune(s) - - return true, len(string(sRunes[matchStart:])) + return true, len(s) - ucaRuneByteOffset(s, matchStart) } func ucaHasSuffixRange(cmp func(a, b string) int, s, suffix string, numeric bool) (int, bool) { + if suffix == "" || cmp("", suffix) == 0 { + return utf8.RuneCountInString(s), true + } + suffixRunes := []rune(suffix) sRunes := []rune(s) - if len(sRunes) < len(suffixRunes) { - return 0, false - } + boundaries := ucaCandidateBoundaries(s, suffix, len(sRunes)) + initialLength := min(len(suffixRunes), len(sRunes)) // Try match lengths from len(suffixRunes) upward - for matchLen := len(suffixRunes); matchLen <= len(sRunes); matchLen++ { + for matchLen := initialLength; matchLen <= len(sRunes); matchLen++ { start := len(sRunes) - matchLen - candidate := string(sRunes[start:]) + candidate := ucaCandidateString(s, sRunes, boundaries, start, len(sRunes)) if cmp(candidate, suffix) == 0 { if numeric && splitsDigitRun(sRunes, start, len(sRunes)) { continue @@ -695,10 +784,10 @@ func ucaHasSuffixRange(cmp func(a, b string) int, s, suffix string, numeric bool } } // Also try shorter matches - for matchLen := len(suffixRunes) - 1; matchLen > 0; matchLen-- { + for matchLen := initialLength - 1; matchLen > 0; matchLen-- { start := len(sRunes) - matchLen - candidate := string(sRunes[start:]) + candidate := ucaCandidateString(s, sRunes, boundaries, start, len(sRunes)) if cmp(candidate, suffix) == 0 { if numeric && splitsDigitRun(sRunes, start, len(sRunes)) { continue @@ -721,7 +810,11 @@ func ucaVariableIndexOf(cmp func(a, b string) int, s, substr string, numeric boo start, end := ucaIndexOfRange(cmp, projectedS.value, projectedSub.value, numeric) if start < 0 { - return -1, 0 + return collationMatchNotFound, 0 + } + + if start == end { + return 0, 0 } byteStart := projectedS.starts[start] @@ -743,6 +836,10 @@ func ucaVariableHasPrefix(cmp func(a, b string) int, s, prefix string, numeric b return false, 0 } + if matchEnd == 0 { + return true, 0 + } + return true, projectedS.ends[matchEnd-1] } @@ -759,6 +856,10 @@ func ucaVariableHasSuffix(cmp func(a, b string) int, s, suffix string, numeric b return false, 0 } + if matchStart == len(projectedS.starts) { + return true, 0 + } + return true, len(s) - projectedS.starts[matchStart] } @@ -776,15 +877,15 @@ func buildCaseFirstKey(s, caseFirst string) []byte { switch caseFirst { case nameLower: if unicode.IsLower(r) { - key = append(key, 0x01) + key = append(key, collationPreferredCaseWeight) } else { - key = append(key, 0x02) + key = append(key, collationOtherCaseWeight) } - case nameUpper2: + case nameUpperCase: if unicode.IsUpper(r) { - key = append(key, 0x01) + key = append(key, collationPreferredCaseWeight) } else { - key = append(key, 0x02) + key = append(key, collationOtherCaseWeight) } } } diff --git a/transform/internal/xpath/compare.go b/transform/internal/xpath/compare.go index 3519f93..7e4040e 100644 --- a/transform/internal/xpath/compare.go +++ b/transform/internal/xpath/compare.go @@ -17,6 +17,36 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) +// Value comparisons accept one atom per operand. The atomization probe stops +// at two atoms to distinguish a singleton without draining a lazy multi-value input. +// Range-side flags preserve the direction of noncommutative comparison operators. +const ( + singleComparisonOperand = 1 + maxSingletonComparisonAtoms = 2 + comparisonRangeOnRight = false + comparisonRangeOnLeft = true +) + +// F&O temporal comparisons anchor times to 1972-12-31. Gregorian partial dates +// use the same reference year and substitute January/day one when absent. +const ( + comparisonReferenceYear = 1972 + comparisonReferenceMonth = 12 + comparisonReferenceDay = 31 + comparisonPartialMonth = 1 + comparisonPartialDay = 1 +) + +// Duration families keep month and second value spaces distinct. Boolean ranks +// express false-before-true ordering, independently of numeric cast values. +const ( + comparisonYearMonthFamily = "duration:YM" + comparisonDayTimeFamily = "duration:DT" + durationMonthsMode = true + booleanComparisonFalse = 0 + booleanComparisonTrue = 1 +) + // GeneralCompare performs a general comparison between two sequences. // Atomizes both sides and returns true if any pair of atomic values // satisfies the operator. @@ -56,6 +86,30 @@ func TimeToReferenceDateTime(t time.Time) time.Time { return timeToReferenceDateTime(t) } +// GregorianToReferenceDateTime converts a Gregorian partial-date value to the +// same reference date used by XPath value comparison, preserving its timezone. +// It rejects values outside the Gregorian partial-date types or lexical space. +func GregorianToReferenceDateTime(value xdm.AtomicValue) (time.Time, bool) { + value = xdm.PromoteSchemaType(value) + switch value.TypeName { + case xdm.TypeGDay, xdm.TypeGMonth, xdm.TypeGMonthDay, xdm.TypeGYear, xdm.TypeGYearMonth: + default: + return time.Time{}, false + } + + text, ok := value.Value.(string) + if !ok { + return time.Time{}, false + } + + validated, err := CastFromString(text, value.TypeName) + if err != nil { + return time.Time{}, false + } + + return parseGTypeToTime(value.TypeName, validated.StringVal()) +} + // ApplyImplicitTZ applies the implicit timezone to a time that has // no explicit timezone. Times with explicit timezones are returned as-is. func ApplyImplicitTZ(t time.Time, implicitTZ *time.Location) time.Time { @@ -171,7 +225,7 @@ func castUntypedToType(ctx context.Context, untyped xdm.AtomicValue, targetType // define binary equality alone. Keep this check at the language evaluation // boundary because exported atomic helpers use the current operator library. func validateComparisonEdition(op TokenType, a, b xdm.AtomicValue, ec *evalContext) error { - if ec == nil || ec.version == 0 || ec.version >= 31 { + if ec == nil || ec.version == xpathVersionDefault || ec.version >= xpathVersion31 { return nil } @@ -291,11 +345,11 @@ func evalGeneralComparison(evalFn exprEvaluator, ctx context.Context, ec *evalCo func evalGeneralComparisonAgainstRange(evalFn exprEvaluator, ctx context.Context, ec *evalContext, e BinaryExpr) (bool, bool, error) { if re, ok := e.Right.(RangeExpr); ok { - return compareSingletonAgainstRange(evalFn, ctx, ec, e.Op, e.Left, re, false) + return compareSingletonAgainstRange(evalFn, ctx, ec, e.Op, e.Left, re, comparisonRangeOnRight) } if re, ok := e.Left.(RangeExpr); ok { - return compareSingletonAgainstRange(evalFn, ctx, ec, e.Op, e.Right, re, true) + return compareSingletonAgainstRange(evalFn, ctx, ec, e.Op, e.Right, re, comparisonRangeOnLeft) } return false, false, nil @@ -320,7 +374,7 @@ func compareSingletonAgainstRange(evalFn exprEvaluator, ctx context.Context, ec return false, true, err } - if len(singletonAtoms) != 1 { + if len(singletonAtoms) != singleComparisonOperand { return false, false, nil } @@ -378,7 +432,7 @@ func evalRangeBounds(evalFn exprEvaluator, ctx context.Context, ec *evalContext, return nil, nil, false, err } - if len(startAtoms) != 1 || len(endAtoms) != 1 { + if len(startAtoms) != singleComparisonOperand || len(endAtoms) != singleComparisonOperand { return nil, nil, false, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "range bounds must be singletons"} } @@ -502,7 +556,7 @@ func evalValueComparison(evalFn exprEvaluator, ctx context.Context, ec *evalCont return nil, nil } - if len(leftAtoms) > 1 || len(rightAtoms) > 1 { + if len(leftAtoms) > singleComparisonOperand || len(rightAtoms) > singleComparisonOperand { return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "value comparison requires singletons"} } @@ -530,17 +584,17 @@ func atomizeSingletonOperand(seq xdm.Sequence) ([]xdm.AtomicValue, error) { // machinery — preserving perf on hot numeric paths (arithmetic, range). Arrays // (flattened) and schema list/union-typed nodes (expanded) fall through to // atomizeStream so their multi-atom typed value is seen. - if xdm.SequenceLen(seq) == 1 { + if xdm.SequenceLen(seq) == singleComparisonOperand { if av, ok := seq.Get(0).(xdm.AtomicValue); ok { return []xdm.AtomicValue{av}, nil } } - atoms := make([]xdm.AtomicValue, 0, 2) + atoms := make([]xdm.AtomicValue, 0, maxSingletonComparisonAtoms) err := atomizeStream(seq, func(av xdm.AtomicValue) (bool, error) { atoms = append(atoms, av) - return len(atoms) < 2, nil + return len(atoms) < maxSingletonComparisonAtoms, nil }) if err != nil { return nil, err @@ -564,7 +618,7 @@ func evalNodeComparison(evalFn exprEvaluator, ctx context.Context, ec *evalConte return nil, nil } - if left.Len() > 1 || right.Len() > 1 { + if left.Len() > singleComparisonOperand || right.Len() > singleComparisonOperand { return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "node comparison requires singletons"} } @@ -796,9 +850,9 @@ func comparisonFamily(typeName string) string { case xdm.TypeDuration: return lexicon.TypeDuration case xdm.TypeYearMonthDuration: - return "duration:YM" + return comparisonYearMonthFamily case xdm.TypeDayTimeDuration: - return "duration:DT" + return comparisonDayTimeFamily case xdm.TypeBase64Binary: return lexicon.TypeBase64Binary case xdm.TypeHexBinary: @@ -1043,7 +1097,7 @@ func timeToReferenceDateTime(t time.Time) time.Time { _, offset := t.Zone() loc := time.FixedZone("", offset) - return time.Date(1972, 12, 31, t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), loc) + return time.Date(comparisonReferenceYear, comparisonReferenceMonth, comparisonReferenceDay, t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), loc) } // applyImplicitTZ applies the system's implicit timezone to a time that has @@ -1065,8 +1119,8 @@ func applyImplicitTZ(t time.Time, implicitTZ *time.Location) time.Time { // normalizeGTZ normalizes timezone suffixes in g* type string values // so that -00:00, +00:00, and Z are all treated as equivalent. func normalizeGTZ(s string) string { - if strings.HasSuffix(s, "+00:00") || strings.HasSuffix(s, "-00:00") { - return s[:len(s)-6] + "Z" + if strings.HasSuffix(s, positiveZeroTimezone) || strings.HasSuffix(s, negativeZeroTimezone) { + return s[:len(s)-timezoneOffsetBytes] + utcTimezoneLexical } return s @@ -1088,7 +1142,7 @@ func parseGTypeToTime(typeName, s string) (time.Time, bool) { day := parseInt(rest[3:5]) - return time.Date(1972, 12, day, 0, 0, 0, 0, loc), true + return time.Date(comparisonReferenceYear, comparisonReferenceMonth, day, 0, 0, 0, 0, loc), true case xdm.TypeGMonth: // Format: --MM if len(rest) < 4 { @@ -1097,7 +1151,7 @@ func parseGTypeToTime(typeName, s string) (time.Time, bool) { month := parseInt(rest[2:4]) - return time.Date(1972, time.Month(month), 1, 0, 0, 0, 0, loc), true + return time.Date(comparisonReferenceYear, time.Month(month), comparisonPartialDay, 0, 0, 0, 0, loc), true case xdm.TypeGMonthDay: // Format: --MM-DD if len(rest) < 7 { @@ -1107,13 +1161,13 @@ func parseGTypeToTime(typeName, s string) (time.Time, bool) { month := parseInt(rest[2:4]) day := parseInt(rest[5:7]) - return time.Date(1972, time.Month(month), day, 0, 0, 0, 0, loc), true + return time.Date(comparisonReferenceYear, time.Month(month), day, 0, 0, 0, 0, loc), true case xdm.TypeGYear: // Format: [-]YYYY year, endIdx := parseGYear(rest) _ = endIdx - return time.Date(year, 1, 1, 0, 0, 0, 0, loc), true + return time.Date(year, comparisonPartialMonth, comparisonPartialDay, 0, 0, 0, 0, loc), true case xdm.TypeGYearMonth: // Format: [-]YYYY-MM year, endIdx := parseGYear(rest) @@ -1123,7 +1177,7 @@ func parseGTypeToTime(typeName, s string) (time.Time, bool) { month := parseInt(rest[endIdx+1 : endIdx+3]) - return time.Date(year, time.Month(month), 1, 0, 0, 0, 0, loc), true + return time.Date(year, time.Month(month), comparisonPartialDay, 0, 0, 0, 0, loc), true } return time.Time{}, false @@ -1132,22 +1186,22 @@ func parseGTypeToTime(typeName, s string) (time.Time, bool) { // parseGTimezone extracts the timezone from a g* string value, returning the // location and the string with timezone removed. func parseGTimezone(s string) (*time.Location, string) { - if strings.HasSuffix(s, "Z") { + if strings.HasSuffix(s, utcTimezoneLexical) { return time.UTC, s[:len(s)-1] } - if len(s) >= 6 { - tz := s[len(s)-6:] + if len(s) >= timezoneOffsetBytes { + tz := s[len(s)-timezoneOffsetBytes:] if (tz[0] == '+' || tz[0] == '-') && tz[3] == ':' { hours := parseInt(tz[1:3]) mins := parseInt(tz[4:6]) - offset := hours*3600 + mins*60 + offset := hours*calendarSecondsPerHour + mins*calendarSecondsPerMinute if tz[0] == '-' { offset = -offset } - return time.FixedZone("", offset), s[:len(s)-6] + return time.FixedZone("", offset), s[:len(s)-timezoneOffsetBytes] } } // No timezone — use noTZLocation sentinel @@ -1158,7 +1212,7 @@ func parseGTimezone(s string) (*time.Location, string) { func parseInt(s string) int { n := 0 for _, c := range s { - n = n*10 + int(c-'0') + n = n*numericDecimalRadix + int(c-'0') } return n @@ -1212,10 +1266,10 @@ func compareDuration(op TokenType, a, b xdm.Duration) (bool, error) { // exact value space — so consecutive huge whole seconds and near-1 fractional // seconds stay distinct (no float64 epsilon collapse). durationToRat folds the // Negative sign into each rational. - aMonthsRat := xdm.DurationToRat(a, true) - aSecsRat := xdm.DurationToRat(a, false) - bMonthsRat := xdm.DurationToRat(b, true) - bSecsRat := xdm.DurationToRat(b, false) + aMonthsRat := xdm.DurationToRat(a, durationMonthsMode) + aSecsRat := xdm.DurationToRat(a, durationSecondsMode) + bMonthsRat := xdm.DurationToRat(b, durationMonthsMode) + bSecsRat := xdm.DurationToRat(b, durationSecondsMode) eq := aMonthsRat.Cmp(bMonthsRat) == 0 && aSecsRat.Cmp(bSecsRat) == 0 @@ -1384,13 +1438,13 @@ func compareFloats(op TokenType, a, b float64) bool { } func compareBooleans(op TokenType, a, b bool) bool { - ai, bi := 0, 0 + ai, bi := booleanComparisonFalse, booleanComparisonFalse if a { - ai = 1 + ai = booleanComparisonTrue } if b { - bi = 1 + bi = booleanComparisonTrue } switch op { diff --git a/transform/internal/xpath/compile_direct.go b/transform/internal/xpath/compile_direct.go index c0e3eae..06dfc73 100644 --- a/transform/internal/xpath/compile_direct.go +++ b/transform/internal/xpath/compile_direct.go @@ -11,6 +11,15 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) +// Direct-path parsing preallocates these numbers of steps/predicates as hints, +// not acceptance limits. A negative predicate position means no concrete lexer +// was available to record a rewind point. +const ( + directPathStepCapacity = 4 + directPredicateCapacity = 2 + noDirectPredicatePosition = -1 +) + func compileFromLexer(l *lexer) (*vmProgram, prefixValidationPlan, error) { if program, prefixPlan, ok, err := tryCompileDirectFromLexer(l); ok || err != nil { return program, prefixPlan, err @@ -94,7 +103,7 @@ func (p *parseOptions) tryParseDirectFunctionCall() (Expr, bool, error) { switch tok.Value { case lexicon.KindNode, lexicon.KindText, lexicon.KindComment, lexicon.KindPI, lexicon.KindElement, lexicon.KindAttribute, lexicon.KindDocumentNode, lexicon.KindSchemaElement, - lexicon.KindSchemaAttribute, lexicon.KindNamespaceNode, "item", nameEmptySequence: + lexicon.KindSchemaAttribute, lexicon.KindNamespaceNode, lexicon.KindItem, nameEmptySequence: return nil, false, nil } } @@ -156,7 +165,7 @@ func (p *parseOptions) tryParseDirectLocationPath() (Expr, bool, error) { } absolute := false - steps := make([]vmLocationStep, 0, 4) + steps := make([]vmLocationStep, 0, directPathStepCapacity) switch tok.Type { case TokenSlash: @@ -240,8 +249,11 @@ func (p *parseOptions) tryParseDirectStep() (vmLocationStep, bool, error) { } axis := AxisChild + axisSpecified := false + if tok.Type == TokenAt { axis = AxisAttribute + axisSpecified = true p.lexer.Next() } @@ -254,6 +266,7 @@ func (p *parseOptions) tryParseDirectStep() (vmLocationStep, bool, error) { } axis = a + axisSpecified = true p.lexer.Next() p.lexer.Next() @@ -271,9 +284,10 @@ func (p *parseOptions) tryParseDirectStep() (vmLocationStep, bool, error) { } return vmLocationStep{ - Axis: axis, - NodeTest: nodeTest, - Predicates: predicates, + Axis: axis, + ExplicitAxis: axisSpecified, + NodeTest: nodeTest, + Predicates: predicates, }, true, nil } @@ -328,7 +342,7 @@ func (p *parseOptions) tryParseDirectNodeTest(axis AxisType) (xdm.NodeTest, bool p.lexer.Next() - return xdm.NameTest{URI: uri, Local: local}, true, nil + return xdm.NameTest{URI: uri, Local: local, URIQualified: true}, true, nil } if p.joinedQNameAt(1) { @@ -364,13 +378,13 @@ func (p *parseOptions) tryParseDirectNodeTest(axis AxisType) (xdm.NodeTest, bool func (p *parseOptions) parseDirectPredicates() ([]Expr, error) { var predicates []Expr if p.lexer.Peek().Type == TokenLBracket { - predicates = make([]Expr, 0, 2) + predicates = make([]Expr, 0, directPredicateCapacity) } for p.lexer.Peek().Type == TokenLBracket { p.lexer.Next() - predStart := -1 + predStart := noDirectPredicatePosition if l, ok := p.lexer.(*lexer); ok { predStart = l.idx } diff --git a/transform/internal/xpath/consts.go b/transform/internal/xpath/consts.go index 2dac28b..999baf8 100644 --- a/transform/internal/xpath/consts.go +++ b/transform/internal/xpath/consts.go @@ -16,11 +16,25 @@ const ( // http://www.w3.org/2005/xpath-functions namespace. Kept separate from // keywordMap/keywordArray because the conceptual roles are unrelated // even though the string values coincide. - jsonKindNull = "null" - jsonKindMap = "map" - jsonKindArray = "array" + jsonKindNull = "null" + jsonKindMap = "map" + jsonKindArray = "array" + jsonKindString = "string" + jsonKindNumber = "number" + jsonKindBoolean = "boolean" // Duplicate-key handling option values for JSON-related functions. duplicatesUseFirst = "use-first" duplicatesReject = "reject" ) + +// XPath 1.0 retains legacy errors; XPath 2.0 introduces the standard dynamic +// error-code vocabulary; 3.0 and 3.1 select later grammars and function libraries. +// These language codes are separate from schema and XSLT. +const ( + xpathVersionDefault = 0 // selects the engine default + xpathVersion10 = 10 + xpathVersion20 = 20 + xpathVersion30 = 30 + xpathVersion31 = 31 +) diff --git a/transform/internal/xpath/context.go b/transform/internal/xpath/context.go index 38b2823..6c3bc4b 100644 --- a/transform/internal/xpath/context.go +++ b/transform/internal/xpath/context.go @@ -275,6 +275,32 @@ func FnXPath10Compat(ctx context.Context) bool { return getFnContext(ctx) != nil && getFnContext(ctx).xpath10Compat } +// WithXMLSchemaVersion selects lexical and regex rules for nested evaluations. +func WithXMLSchemaVersion(ctx context.Context, version int) context.Context { + return context.WithValue(ctx, xmlSchemaVersionKey{}, version) +} + +// Schema edition zero inherits the surrounding context; without an explicit +// edition the nested lexical/regex context retains XML Schema 1.1. These codes +// are independent of XPath language editions. +const ( + schemaEditionDefault = 0 + schemaEdition11 = 11 +) + +// XMLSchemaVersion returns the edition retained by the current function context. +func XMLSchemaVersion(ctx context.Context) int { + if ec := getFnContext(ctx); ec != nil && ec.xmlSchemaVersion != schemaEditionDefault { + return ec.xmlSchemaVersion + } + + if version, _ := ctx.Value(xmlSchemaVersionKey{}).(int); version != schemaEditionDefault { + return version + } + + return schemaEdition11 +} + func bindInvocationScope(ctx context.Context, ec *evalContext) { if state, ok := ctx.Value(invocationScopeKey{}).(*invocationState); ok { ec.invocation = state @@ -294,24 +320,6 @@ type invocationScopeKey struct{} type xmlSchemaVersionKey struct{} -// WithXMLSchemaVersion selects lexical and regex rules for nested evaluations. -func WithXMLSchemaVersion(ctx context.Context, version int) context.Context { - return context.WithValue(ctx, xmlSchemaVersionKey{}, version) -} - -// XMLSchemaVersion returns the edition retained by the current function context. -func XMLSchemaVersion(ctx context.Context) int { - if ec := getFnContext(ctx); ec != nil && ec.xmlSchemaVersion != 0 { - return ec.xmlSchemaVersion - } - - if version, _ := ctx.Value(xmlSchemaVersionKey{}).(int); version != 0 { - return version - } - - return 11 -} - // dynamicCallKey marks a function call as originating from a dynamic // function reference (e.g. $f(args) where $f holds a named function ref). type dynamicCallKey struct{} diff --git a/transform/internal/xpath/context_item_test.go b/transform/internal/xpath/context_item_test.go new file mode 100644 index 0000000..fbdab96 --- /dev/null +++ b/transform/internal/xpath/context_item_test.go @@ -0,0 +1,62 @@ +package xpath //nolint:testpackage // Compares the context-item shortcut with VM evaluation. + +import ( + "context" + "errors" + "math/big" + "reflect" + "testing" + + "github.com/skarm/xmlsec/transform/internal/xdm" +) + +func TestContextItemMatchesVM(t *testing.T) { + doc, err := (xdm.ParseOptions{}).ParseText(t.Context(), `text`) + if err != nil { + t.Fatal(err) + } + root := doc.DocumentElement() + expr := compileEvaluatorExpression(t, `.`) + if expr == nil || expr.program == nil { + t.Fatal("context-item expression has no compiled program") + } + vmExpression := *expr + program := *expr.program + program.stream.isContextItem = false + vmExpression.program = &program + base := NewEvaluator(DefaultEvaluatorOptions) + for _, evaluator := range []Evaluator{ + base, + base.ContextItem(xdm.AtomicValue{TypeName: xdm.TypeString, Value: "atomic"}), + base.ContextItem(xdm.AtomicValue{TypeName: xdm.TypeInteger, Value: new(big.Int).Lsh(big.NewInt(1), 4096)}), + base.ContextItem(xdm.NodeItem{Node: root, TypeAnnotation: "xs:untyped"}), + base.ContextItem(xdm.NodeItem{}), + base.ContextItem(xdm.NewArray([]xdm.Sequence{xdm.SingleInteger(1)})), + base.TypeAnnotations(map[xdm.Node]string{root: "xs:untyped"}), + } { + for _, node := range []xdm.Node{nil, doc, root} { + for _, canceled := range []bool{false, true} { + for _, byteLimit := range []int{1, 8192} { + ctx, cancel := context.WithCancel(t.Context()) + if canceled { + cancel() + } + fastCtx := WithBudget(ctx, 100, 100, byteLimit, 10) + vmCtx := WithBudget(ctx, 100, 100, byteLimit, 10) + fast, fastErr := evaluator.Evaluate(fastCtx, expr, node) + slow, slowErr := evaluator.Evaluate(vmCtx, &vmExpression, node) + cancel() + if (fastErr == nil) != (slowErr == nil) { + t.Fatalf("error mismatch: %v / %v", fastErr, slowErr) + } + if fastErr != nil && slowErr != nil && (reflect.TypeOf(fastErr) != reflect.TypeOf(slowErr) || fastErr.Error() != slowErr.Error() || errors.Is(fastErr, context.Canceled) != errors.Is(slowErr, context.Canceled)) { + t.Fatalf("error mismatch: %v / %v", fastErr, slowErr) + } + if !reflect.DeepEqual(fast, slow) || !reflect.DeepEqual(getBudget(fastCtx), getBudget(vmCtx)) { + t.Fatalf("result or budget mismatch: %+v / %+v", fast, slow) + } + } + } + } + } +} diff --git a/transform/internal/xpath/dynamic_context.go b/transform/internal/xpath/dynamic_context.go index e78c64b..a0e95c4 100644 --- a/transform/internal/xpath/dynamic_context.go +++ b/transform/internal/xpath/dynamic_context.go @@ -6,6 +6,10 @@ import ( "time" ) +// defaultFormattingLanguage is the formatting fallback when the invocation has +// no explicit language; escaped functions retain this value without capabilities. +const defaultFormattingLanguage = "en" + // EvaluationContext is invocation-local configuration shared by both languages. type EvaluationContext struct { // CurrentTime is the invocation time shared by date and time functions. @@ -35,7 +39,7 @@ type DynamicProperties struct { // values use fallback, its location and en; trace and resource capabilities are // excluded. func CaptureDynamicProperties(ctx context.Context, fallback time.Time) DynamicProperties { - properties := DynamicProperties{CurrentTime: fallback, Timezone: fallback.Location(), Language: "en"} + properties := DynamicProperties{CurrentTime: fallback, Timezone: fallback.Location(), Language: defaultFormattingLanguage} if value, ok := evaluationContextFrom(ctx); ok { if !value.CurrentTime.IsZero() { properties.CurrentTime = value.CurrentTime diff --git a/transform/internal/xpath/errors.go b/transform/internal/xpath/errors.go index 1b7f1e8..0f3fed0 100644 --- a/transform/internal/xpath/errors.go +++ b/transform/internal/xpath/errors.go @@ -11,7 +11,15 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) +// Standard dynamic/static error codes are stored without an err namespace prefix. +// Existing common lexicon definitions retain their exact wire values. +// Static-context errors keep their existing standard code spellings. const ( + errCodeXPTY0117 = "XPTY0117" + errCodeXQDY0137 = "XQDY0137" + errCodeXQST0039 = "XQST0039" + errCodeXQST0052 = "XQST0052" + errCodeXQST0070 = "XQST0070" errCodeXPST0051 = "XPST0051" // unknown atomic or union type errCodeXPDY0050 = "XPDY0050" errCodeXPST0080 = "XPST0080" @@ -19,6 +27,7 @@ const ( errCodeFOAR0001 = "FOAR0001" errCodeFOAR0002 = "FOAR0002" errCodeFOAP0001 = "FOAP0001" + errCodeFOCA0005 = "FOCA0005" // NaN is not permitted by the operation. errCodeFOCA0002 = "FOCA0002" errCodeFOCH0002 = "FOCH0002" errCodeFOCH0003 = "FOCH0003" @@ -28,6 +37,7 @@ const ( errCodeFODC0006 = "FODC0006" errCodeFODT0001 = "FODT0001" errCodeFODT0002 = "FODT0002" + errCodeFODT0003 = "FODT0003" errCodeFODF1280 = "FODF1280" errCodeFODF1310 = "FODF1310" errCodeFOFD1340 = "FOFD1340" @@ -36,13 +46,14 @@ const ( errCodeFONS0004 = "FONS0004" errCodeFORG0001 = "FORG0001" errCodeFORG0006 = "FORG0006" + errCodeFORG0008 = "FORG0008" errCodeFOER0000 = "FOER0000" errCodeFORX0001 = "FORX0001" errCodeFORX0003 = "FORX0003" errCodeFORX0004 = "FORX0004" errCodeFOTY0014 = "FOTY0014" errCodeFOJS0001 = "FOJS0001" - errCodeFOJS0003 = "FOJS0003" + errCodeFOJS0003 = lexicon.ErrFOJS0003 errCodeFOJS0004 = "FOJS0004" errCodeFOJS0005 = "FOJS0005" errCodeFOJS0006 = "FOJS0006" @@ -94,7 +105,7 @@ func UndefinedVariableError(name string, version int) error { } func languageError(cause error, code string, version int) error { - if version == 10 { + if version == xpathVersion10 { return cause } @@ -104,7 +115,7 @@ func languageError(cause error, code string, version int) error { // syntaxError preserves parser sentinels and adds the standard error code for // XPath 2.0 and later. XPath 1.0 has no standard error-code vocabulary. func syntaxError(err error, version int) error { - if version == 10 || err == nil { + if version == xpathVersion10 || err == nil { return err } diff --git a/transform/internal/xpath/eval.go b/transform/internal/xpath/eval.go index b49d365..4615e7d 100644 --- a/transform/internal/xpath/eval.go +++ b/transform/internal/xpath/eval.go @@ -16,6 +16,16 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) +// Single-binding and scalar-result cardinalities select fast paths, not caps. +// Nonpositive local operation/recursion limits disable their respective local +// checks; these selectors do not disable the shared invocation budget. +const ( + singleScopeBinding = 1 + singleExpressionResult = 1 + disabledLocalOperationLimit = 0 + disabledLocalRecursionLimit = 0 +) + const maxNodeSetLength = xdm.DefaultMaxNodeSetLength // DefaultMaxRecursionDepth bounds nested expressions in each evaluation. @@ -190,7 +200,7 @@ func (ec *evalContext) getDefaultLanguage() string { return ec.defaultLanguage } - return "en" + return defaultFormattingLanguage } func (ec *evalContext) withVar(name string, val xdm.Sequence) *evalContext { @@ -225,7 +235,7 @@ func (ec *evalContext) countOps(ctx context.Context, n int) error { return err } - if ec.opLimit <= 0 { + if ec.opLimit <= disabledLocalOperationLimit { return nil } // Saturate instead of wrapping: where int is 32 bits a long evaluation @@ -279,7 +289,7 @@ func scopeWithBindings(parent *variableScope, bindings map[string]xdm.Sequence) return parent } - if len(bindings) == 1 { + if len(bindings) == singleScopeBinding { for name, value := range bindings { return scopeWithBinding(parent, name, value) } @@ -304,13 +314,13 @@ type exprEvaluator func(context.Context, *evalContext, Expr) (xdm.Sequence, erro func evalWith(evalFn exprEvaluator, ctx context.Context, ec *evalContext, expr Expr) (xdm.Sequence, error) { ec.depth++ - if ec.maxRecursionDepth > 0 && ec.depth > ec.maxRecursionDepth { + if ec.maxRecursionDepth > disabledLocalRecursionLimit && ec.depth > ec.maxRecursionDepth { return nil, ErrRecursionLimit } defer func() { ec.depth-- }() result, err := evalFn(ctx, ec, expr) - if err == nil && xdm.SequenceLen(result) == 1 { + if err == nil && xdm.SequenceLen(result) == singleExpressionResult { if value, ok := result.Get(0).(xdm.AtomicValue); ok { err = ChargeNumber(ctx, value) } diff --git a/transform/internal/xpath/eval_arithmetic.go b/transform/internal/xpath/eval_arithmetic.go index 4148131..f1bfc06 100644 --- a/transform/internal/xpath/eval_arithmetic.go +++ b/transform/internal/xpath/eval_arithmetic.go @@ -13,6 +13,23 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) +// Scalar arithmetic operands and duration results have independent singleton +// cardinalities; these names preserve the existing checks/accounting fast path. +const ( + singleArithmeticOperand = 1 + singleArithmeticDurationResult = 1 +) + +// Recurring decimal quotients retain at least twenty fractional/significant +// places. Preflight conservatively budgets four bits per added decimal digit, +// one denominator spare bit, and three lexical bytes for sign/point/leading zero. +const ( + decimalQuotientMinimumPlaces = 20 + decimalQuotientBitsPerDigit = 4 + decimalQuotientDenominatorSpareBit = 1 + decimalQuotientLexicalOverhead = 3 +) + func evalArithmetic(evalFn exprEvaluator, ctx context.Context, ec *evalContext, e BinaryExpr) (xdm.Sequence, error) { left, err := evalFn(ctx, ec, e.Left) if err != nil { @@ -59,7 +76,7 @@ func evalArithmetic(evalFn exprEvaluator, ctx context.Context, ec *evalContext, return nil, nil // empty sequence } - if len(lAtoms) > 1 || len(rAtoms) > 1 { + if len(lAtoms) > singleArithmeticOperand || len(rAtoms) > singleArithmeticOperand { return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "arithmetic operand must be a single item"} } @@ -70,7 +87,7 @@ func evalArithmetic(evalFn exprEvaluator, ctx context.Context, ec *evalContext, // Contract: evalDateTimeArithmetic returns handled==false only when both // operands are non-duration/non-datetime, in which case err is always nil. if result, handled, err := evalDateTimeArithmetic(ctx, ec, e.Op, la, ra); err != nil || handled { - return result, err + return chargeDurationArithmeticResult(ctx, result, err) } // Promote xs:untypedAtomic to xs:double for arithmetic @@ -124,6 +141,24 @@ func evalArithmetic(evalFn exprEvaluator, ctx context.Context, ec *evalContext, return floatArith(e.Op, la, ra) } +// Charge only constructed durations here. Numeric duration ratios retain their +// existing numeric accounting, and date/time results keep their existing path. +func chargeDurationArithmeticResult(ctx context.Context, result xdm.Sequence, err error) (xdm.Sequence, error) { + if err != nil { + return nil, err + } + + if xdm.SequenceLen(result) == singleArithmeticDurationResult { + if value, ok := result.Get(0).(xdm.AtomicValue); ok && isDurationType(value.TypeName) { + if err := chargeConstructedAtomic(ctx, value); err != nil { + return nil, err + } + } + } + + return result, nil +} + // promoteSchemaNumeric promotes a user-defined numeric type to its built-in base. func promoteSchemaNumeric(a xdm.AtomicValue) xdm.AtomicValue { return xdm.PromoteSchemaType(a) @@ -382,9 +417,9 @@ func finiteDecimalValue(ctx context.Context, value *big.Rat) (*big.Rat, error) { return nil, err } - numerator := value.Num().Text(10) + numerator := value.Num().Text(numericDecimalRadix) - denominator := value.Denom().Text(10) + denominator := value.Denom().Text(numericDecimalRadix) if err := ChargeString(ctx, len(numerator)+len(denominator)); err != nil { return nil, err } @@ -394,12 +429,12 @@ func finiteDecimalValue(ctx context.Context, value *big.Rat) (*big.Rat, error) { digits-- } - places := 20 + max(0, len(denominator)-digits) - if err := preflightNumeric(ctx, value.Num().BitLen()+places*4, places*4+1); err != nil { + places := decimalQuotientMinimumPlaces + max(0, len(denominator)-digits) + if err := preflightNumeric(ctx, value.Num().BitLen()+places*decimalQuotientBitsPerDigit, places*decimalQuotientBitsPerDigit+decimalQuotientDenominatorSpareBit); err != nil { return nil, err } - if err := ChargeString(ctx, digits+places+3); err != nil { + if err := ChargeString(ctx, digits+places+decimalQuotientLexicalOverhead); err != nil { return nil, err } @@ -453,13 +488,13 @@ func floatArith(op TokenType, la, ra xdm.AtomicValue) (xdm.Sequence, error) { return nil, &xdm.Error{Code: errCodeFOAR0001, Message: textIntegerDivisionByZero} } - if math.IsInf(ln, 0) { + if math.IsInf(ln, eitherInfinitySign) { return nil, &xdm.Error{Code: errCodeFOAR0002, Message: "idiv with infinite dividend"} } // Per F&O §6.2.4: finite idiv ±INF = 0 (math.Trunc(finite/Inf) = 0). truncated := math.Trunc(ln / rn) - bi, _ := new(big.Float).SetFloat64(truncated).Int(nil) + bi, _ := new(big.Float).SetFloat64(truncated).Int(new(big.Int)) if bi == nil { return nil, &xdm.Error{Code: errCodeFOAR0002, Message: "idiv quotient overflow"} } @@ -509,7 +544,7 @@ func evalUnaryExpr(evalFn exprEvaluator, ctx context.Context, ec *evalContext, e return nil, nil } - if len(rAtoms) > 1 { + if len(rAtoms) > singleArithmeticOperand { return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "unary minus operand must be a single item"} } diff --git a/transform/internal/xpath/eval_control.go b/transform/internal/xpath/eval_control.go index 4d62e7a..fca5fdd 100644 --- a/transform/internal/xpath/eval_control.go +++ b/transform/internal/xpath/eval_control.go @@ -14,6 +14,28 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) +// Streaming control flow charges one logical operation at each respective +// clause/item/binding site. Positional variables use XPath's one-based origin, +// independently of zero-based internal clause and binding cursors. +const ( + flworClauseWork = 1 + flworItemWork = 1 + quantifiedBindingWork = 1 + flworPositionOrigin = 1 +) + +// Catch bindings use the standard error namespace's conventional lexical prefix. +// Keep the qualified spellings because VariableKey performs the existing lookup. +const ( + catchVariableCode = lexicon.PrefixErr + ":code" + catchVariableDescription = lexicon.PrefixErr + ":description" + catchVariableValue = lexicon.PrefixErr + ":value" + catchVariableModule = lexicon.PrefixErr + ":module" + catchVariableLineNumber = lexicon.PrefixErr + ":line-number" + catchVariableColumnNumber = lexicon.PrefixErr + ":column-number" + catchVariableAdditional = lexicon.PrefixErr + ":additional" +) + // checkedArrayIndex converts an xs:integer array index to int. An index outside // the int range (above 2^31-1 where int is 32 bits) raises FOAY0001: no array // has that many members, and converting it would wrap to a valid index. @@ -201,7 +223,7 @@ func iterateFLWORClauses(evalFn exprEvaluator, ctx context.Context, ec *evalCont return consumer.ConsumeTuple(scope) } - if err := ec.countOps(ctx, 1); err != nil { + if err := ec.countOps(ctx, flworClauseWork); err != nil { return err } @@ -218,9 +240,13 @@ func iterateFLWORClauses(evalFn exprEvaluator, ctx context.Context, ec *evalCont pos := 0 for item := range xdm.SequenceItems(domain) { + if err := ec.countOps(ctx, flworItemWork); err != nil { + return err + } + inner := scopeWithBinding(scope, VariableKey(c.Var, ec.namespaces), xdm.ItemSlice{item}) if c.PosVar != "" { - inner = scopeWithBinding(inner, VariableKey(c.PosVar, ec.namespaces), xdm.ItemSlice{xdm.AtomicValue{TypeName: xdm.TypeInteger, Value: int64(pos + 1)}}) + inner = scopeWithBinding(inner, VariableKey(c.PosVar, ec.namespaces), xdm.ItemSlice{xdm.AtomicValue{TypeName: xdm.TypeInteger, Value: int64(pos + flworPositionOrigin)}}) } if err := iterateFLWORClauses(evalFn, ctx, ec, clauses, i+1, inner, consumer); err != nil { @@ -278,6 +304,10 @@ func evalQuantifiedBindings(evalFn exprEvaluator, ctx context.Context, ec *evalC } for item := range xdm.SequenceItems(domain) { + if err := ec.countOps(ctx, quantifiedBindingWork); err != nil { + return nil, err + } + oldScope := ec.pushScope(scopeWithBinding(ec.vars, VariableKey(binding.Var, ec.namespaces), xdm.ItemSlice{item})) result, err := evalQuantifiedBindings(evalFn, ctx, ec, e, idx+1) ec.restoreScope(oldScope) @@ -427,13 +457,13 @@ func buildCatchContext(ec *evalContext, xpErr *xdm.Error) *evalContext { TypeName: xdm.TypeQName, Value: errQName, }) - ctx := ec.withVar("err:code", errQN) - ctx = ctx.withVar("err:description", xdm.SingleString(xpErr.Message)) - ctx = ctx.withVar("err:value", xpErr.Value) - ctx = ctx.withVar("err:module", nil) - ctx = ctx.withVar("err:line-number", nil) - ctx = ctx.withVar("err:column-number", nil) - ctx = ctx.withVar("err:additional", nil) + ctx := ec.withVar(catchVariableCode, errQN) + ctx = ctx.withVar(catchVariableDescription, xdm.SingleString(xpErr.Message)) + ctx = ctx.withVar(catchVariableValue, xpErr.Value) + ctx = ctx.withVar(catchVariableModule, nil) + ctx = ctx.withVar(catchVariableLineNumber, nil) + ctx = ctx.withVar(catchVariableColumnNumber, nil) + ctx = ctx.withVar(catchVariableAdditional, nil) return ctx } diff --git a/transform/internal/xpath/eval_funcall.go b/transform/internal/xpath/eval_funcall.go index 975eb21..b59a18d 100644 --- a/transform/internal/xpath/eval_funcall.go +++ b/transform/internal/xpath/eval_funcall.go @@ -12,6 +12,29 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) +// Standard calls distinguish constructor/collection arities from callee and +// diagnostic singleton fast paths. Diagnostic argument numbers are one-based. +const ( + qnameConstructorArity = 1 + currentDateTimeArity = 0 + collectionLookupArity = 1 + singleFunctionCallee = 1 + singleDiagnosticArgument = 1 + functionArgumentOrigin = 1 + fnNameCurrentDateTime = "current-dateTime" + mapLookupFunctionLabel = "map lookup" + arrayLookupFunctionLabel = "array lookup" +) + +// A map key must atomize to one value; probing two detects excess cardinality. +// Capture analysis and each map value retain independent minimum logical work. +const ( + mapKeyCardinality = 1 + mapKeyAtomizationProbe = 2 + minimumClosureCaptureWork = 1 + minimumMapValueWork = 1 +) + func evalFunctionCall(evalFn exprEvaluator, ctx context.Context, ec *evalContext, e FunctionCall) (xdm.Sequence, error) { args := make([]xdm.Sequence, len(e.Args)) hasPlaceholders := false @@ -49,11 +72,11 @@ func evalFunctionCall(evalFn exprEvaluator, ctx context.Context, ec *evalContext // callResolvedFunction coerces args to the declared parameter types of r and // calls it. args holds evaluated, enriched arguments and no placeholders. func callResolvedFunction(ctx context.Context, ec *evalContext, r resolvedFunction, args []xdm.Sequence, sourceArgs []Expr) (xdm.Sequence, error) { - if ec.version == 10 && r.isBuiltin { + if ec.version == xpathVersion10 && r.isBuiltin { return callXPath10Builtin(ctx, ec, r, args) } - if ec.version == 20 && r.isBuiltin && r.uri == NSXS && r.name == lexicon.TypeQName && len(args) == 1 { + if ec.version == xpathVersion20 && r.isBuiltin && r.uri == NSXS && r.name == lexicon.TypeQName && len(args) == qnameConstructorArity { value, empty, err := atomizeConstructorArg(args[0], xdm.TypeQName) if err != nil { return nil, err @@ -102,14 +125,14 @@ func lookupParamTypes(ec *evalContext, r resolvedFunction, arity int) ([]xdm.Seq } func resolvedFunctionSignature(ec *evalContext, r resolvedFunction, arity int) *functionSignature { - version := 0 + version := xpathVersionDefault if ec != nil { version = ec.version } - if r.isBuiltin && r.uri == NSFn && r.name == "current-dateTime" && arity == 0 && ec != nil && ec.xmlSchemaVersion == 10 { + if r.isBuiltin && r.uri == NSFn && r.name == fnNameCurrentDateTime && arity == currentDateTimeArity && ec != nil && ec.xmlSchemaVersion == schemaEdition10 { // F&O permits xs:dateTime when XSD 1.1 datatypes are unavailable. - return lookupStandardSignature(20, sigKey{URI: NSFn, Name: r.name, Arity: arity}) + return lookupStandardSignature(xpathVersion20, sigKey{URI: NSFn, Name: r.name, Arity: arity}) } if r.isBuiltin { @@ -135,7 +158,7 @@ func evalDynamicFunctionCall(evalFn exprEvaluator, ctx context.Context, ec *eval return nil, err } - if xdm.SequenceLen(funcSeq) != 1 { + if xdm.SequenceLen(funcSeq) != singleFunctionCallee { return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "dynamic function call requires single function item"} } @@ -159,7 +182,7 @@ func evalDynamicFunctionCall(evalFn exprEvaluator, ctx context.Context, ec *eval return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "partial application requires function item"} } - if fi.Arity >= 0 && len(e.Args) != fi.Arity { + if fi.Arity >= finiteArityFloor && len(e.Args) != fi.Arity { return nil, languageError(fmt.Errorf("%w: expected %d arguments, got %d", ErrArityMismatch, fi.Arity, len(e.Args)), lexicon.ErrXPTY0004, ec.version) } @@ -176,17 +199,17 @@ func evalDynamicFunctionCall(evalFn exprEvaluator, ctx context.Context, ec *eval switch v := funcSeq.Get(0).(type) { case xdm.FunctionItem: - if v.Arity >= 0 && len(args) != v.Arity { + if v.Arity >= finiteArityFloor && len(args) != v.Arity { return nil, languageError(fmt.Errorf("%w: expected %d arguments, got %d", ErrArityMismatch, v.Arity, len(args)), lexicon.ErrXPTY0004, ec.version) } return v.Invoke(withDynamicCall(ec.fnContext(ctx)), args) case xdm.MapItem: // Maps are functions: $map($key) → value - return mapLookup(v, args) + return mapLookup(ec.fnContext(ctx), v, args) case xdm.ArrayItem: // Arrays are functions: $array($index) → member - return arrayLookup(v, args) + return arrayLookup(ec.fnContext(ctx), v, args) default: return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: fmt.Sprintf("dynamic function call requires function item, got %T", funcSeq.Get(0))} } @@ -239,16 +262,16 @@ func asFunctionItem(item xdm.Item) (xdm.FunctionItem, bool) { return v, true case xdm.MapItem: return xdm.FunctionItem{ - Arity: 1, + Arity: collectionLookupArity, ParamTypes: []xdm.SequenceType{stAtomic(xdm.TypeAnyAtomicType, xdm.OccurrenceExactlyOne)}, ReturnType: &resultType, - Invoke: func(_ context.Context, args []xdm.Sequence) (xdm.Sequence, error) { return mapLookup(v, args) }, + Invoke: func(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { return mapLookup(ctx, v, args) }, }, true case xdm.ArrayItem: return xdm.FunctionItem{ - Arity: 1, + Arity: collectionLookupArity, ParamTypes: []xdm.SequenceType{stAtomic(xdm.TypeInteger, xdm.OccurrenceExactlyOne)}, ReturnType: &resultType, - Invoke: func(_ context.Context, args []xdm.Sequence) (xdm.Sequence, error) { return arrayLookup(v, args) }, + Invoke: func(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { return arrayLookup(ctx, v, args) }, }, true default: return xdm.FunctionItem{}, false @@ -256,52 +279,44 @@ func asFunctionItem(item xdm.Item) (xdm.FunctionItem, bool) { } // mapLookup implements the map-as-function call $map($key) → value. -func mapLookup(m xdm.MapItem, args []xdm.Sequence) (xdm.Sequence, error) { - if len(args) != 1 || xdm.SequenceLen(args[0]) != 1 { +func mapLookup(ctx context.Context, m xdm.MapItem, args []xdm.Sequence) (xdm.Sequence, error) { + if len(args) != collectionLookupArity { return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "map lookup requires exactly one argument"} } - key, err := AtomizeItem(args[0].Get(0)) + key, err := coerceFuncallArg(ctx, args[0], stAtomic(xdm.TypeAnyAtomicType, xdm.OccurrenceExactlyOne), mapLookupFunctionLabel, 0, getFnContext(ctx)) if err != nil { return nil, err } - val, ok := m.Get(key) - if !ok { - return nil, nil - } - - return val, nil + return fnMapGet(ctx, []xdm.Sequence{xdm.ItemSlice{m}, key}) } -// arrayLookup implements the array-as-function call $array($index) → member. -func arrayLookup(a xdm.ArrayItem, args []xdm.Sequence) (xdm.Sequence, error) { - if len(args) != 1 || xdm.SequenceLen(args[0]) != 1 { +// arrayLookup applies the same function conversion and bounded retrieval to raw +// calls, partial applications and maps/arrays adapted for higher-order calls. +func arrayLookup(ctx context.Context, a xdm.ArrayItem, args []xdm.Sequence) (xdm.Sequence, error) { + if len(args) != collectionLookupArity { return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "array lookup requires exactly one argument"} } - key, err := AtomizeItem(args[0].Get(0)) + key, err := coerceFuncallArg(ctx, args[0], stAtomic(xdm.TypeInteger, xdm.OccurrenceExactlyOne), arrayLookupFunctionLabel, 0, getFnContext(ctx)) if err != nil { return nil, err } - if key.TypeName == xdm.TypeUntypedAtomic { - key, err = CastAtomic(key, xdm.TypeInteger) - if err != nil { - return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "array lookup requires xs:integer index"} - } - } - - if !xdm.IsIntegerDerived(key.TypeName) { - return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "array lookup requires xs:integer index"} + idx, err := extractArrayIndex(key) + if err != nil { + return nil, err } - idx, err := checkedArrayIndex(key) + value, err := a.GetView(idx) if err != nil { return nil, err } - return a.Get(idx) + ec := getFnContext(ctx) + + return appendBoundedClonedSeq(ctx, ec, nil, value, fnMaxNodes(ec)) } func evalNamedFunctionRef(ctx context.Context, ec *evalContext, e NamedFunctionRef) (xdm.Sequence, error) { @@ -320,7 +335,7 @@ func evalNamedFunctionRef(ctx context.Context, ec *evalContext, e NamedFunctionR } minArity := fn.MinArity() - ns, _ := resolvePrefix(ec, e.Prefix) + ns := r.uri // Per XPath 3.1 Section 3.1.6: if the function is focus-dependent, // the dynamic context (including focus) is fixed at reference creation time. // Capture the evalContext snapshot for focus/variables, but use the caller's @@ -344,8 +359,8 @@ func evalNamedFunctionRef(ctx context.Context, ec *evalContext, e NamedFunctionR // If so, create a function item that always raises the specified error. if dr, ok := fn.(DynamicRefRestricted); ok && dr.NoDynamicRef() { errCode := dr.DynRefErrorCode() - fnName := e.Name - ns, _ := resolvePrefix(ec, e.Prefix) + fnName := r.name + ns := r.uri fi := xdm.FunctionItem{ Arity: e.Arity, ParamTypes: paramTypes, RepeatParamType: repeated, ReturnType: returnType, @@ -361,7 +376,7 @@ func evalNamedFunctionRef(ctx context.Context, ec *evalContext, e NamedFunctionR fi := xdm.FunctionItem{ Arity: e.Arity, - Name: e.Name, + Name: r.name, Namespace: ns, ParamTypes: paramTypes, RepeatParamType: repeated, @@ -413,7 +428,7 @@ func evalInlineFunctionExpr(evalFn exprEvaluator, ctx context.Context, ec *evalC collector.walkInlineFunction(definition) - if err := fnCountOps(ctx, ec, max(collector.visits, 1)); err != nil { + if err := fnCountOps(ctx, ec, max(collector.visits, minimumClosureCaptureWork)); err != nil { return nil, err } @@ -611,9 +626,9 @@ func coerceFuncallArg(ctx context.Context, arg xdm.Sequence, st xdm.SequenceType return nil, err } - message := fmt.Sprintf("fn:%s: argument %d does not match required type %v", fnName, idx+1, st) + message := fmt.Sprintf("fn:%s: argument %d does not match required type %v", fnName, idx+functionArgumentOrigin, st) - if xdm.SequenceLen(arg) == 1 { + if xdm.SequenceLen(arg) == singleDiagnosticArgument { if actual, atomic := arg.Get(0).(xdm.AtomicValue); atomic { message += "; got " + actual.TypeName } @@ -627,6 +642,10 @@ func coerceFuncallArg(ctx context.Context, arg xdm.Sequence, st xdm.SequenceType func evalMapConstructorExpr(evalFn exprEvaluator, ctx context.Context, ec *evalContext, e MapConstructorExpr) (xdm.Sequence, error) { maxNodes := ec.maxNodes + if maxNodes > disabledSequenceLimit && len(e.Pairs) > maxNodes { + return nil, ErrNodeSetLimit + } + entries := make([]xdm.MapEntry, 0, len(e.Pairs)) seen := make(map[xdm.MapKey]struct{}, len(e.Pairs)) @@ -640,12 +659,12 @@ func evalMapConstructorExpr(evalFn exprEvaluator, ctx context.Context, ec *evalC if err := atomizeBounded(ctx, ec, keySeq, func(atom xdm.AtomicValue) (bool, error) { keyAtoms = append(keyAtoms, atom) - return len(keyAtoms) < 2, nil + return len(keyAtoms) < mapKeyAtomizationProbe, nil }); err != nil { return nil, err } - if len(keyAtoms) != 1 { + if len(keyAtoms) != mapKeyCardinality { return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "map key must atomize to a single atomic value"} } @@ -653,7 +672,7 @@ func evalMapConstructorExpr(evalFn exprEvaluator, ctx context.Context, ec *evalC // Per XPath 3.1, duplicate keys in a map constructor raise XQDY0137 nk := xdm.NormalizeMapKey(ka) if _, dup := seen[nk]; dup { - return nil, &xdm.Error{Code: "XQDY0137", Message: fmt.Sprintf("duplicate key in map constructor: %v", ka.Value)} + return nil, &xdm.Error{Code: errCodeXQDY0137, Message: fmt.Sprintf("duplicate key in map constructor: %v", ka.Value)} } seen[nk] = struct{}{} @@ -668,11 +687,11 @@ func evalMapConstructorExpr(evalFn exprEvaluator, ctx context.Context, ec *evalC // value is rejected here via the O(1) seqLen check instead of OOMing the // engine when NewMap materializes it. vLen := xdm.SequenceLen(valSeq) - if maxNodes > 0 && vLen > maxNodes { + if maxNodes > disabledSequenceLimit && vLen > maxNodes { return nil, ErrNodeSetLimit } - if err := fnCountOps(ctx, ec, max(vLen, 1)); err != nil { + if err := fnCountOps(ctx, ec, max(vLen, minimumMapValueWork)); err != nil { return nil, err } diff --git a/transform/internal/xpath/eval_operators.go b/transform/internal/xpath/eval_operators.go index e86234d..1180932 100644 --- a/transform/internal/xpath/eval_operators.go +++ b/transform/internal/xpath/eval_operators.go @@ -12,6 +12,24 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) +// XPath focus positions are one-based; a location path evaluated from a single +// node has focus size one. Nonpositive sequence limits retain the existing +// disabled-limit convention, separate from work and recursion limits. +const ( + focusPositionOrigin = 1 + singletonFocusSize = 1 + disabledSequenceLimit = 0 +) + +// Range operands accept at most one atom. Legacy compatibility keeps the first +// atom instead. Concat indices identify the two arguments for type diagnostics. +const ( + singleRangeOperand = 1 + xpath10RangeRetainedAtoms = 1 + concatLeftArgumentIndex = 0 + concatRightArgumentIndex = 1 +) + func evalBinaryExpr(evalFn exprEvaluator, ctx context.Context, ec *evalContext, e BinaryExpr) (xdm.Sequence, error) { switch e.Op { case TokenOr: @@ -98,12 +116,12 @@ func evalConcatExpr(evalFn exprEvaluator, ctx context.Context, ec *evalContext, return nil, err } - ls, err := concatToString(ctx, ec, left, 0) + ls, err := concatToString(ctx, ec, left, concatLeftArgumentIndex) if err != nil { return nil, err } - rs, err := concatToString(ctx, ec, right, 1) + rs, err := concatToString(ctx, ec, right, concatRightArgumentIndex) if err != nil { return nil, err } @@ -132,7 +150,7 @@ func concatToString(ctx context.Context, ec *evalContext, seq xdm.Sequence, inde // concatenate per-item sub-sequences and could otherwise materialize unbounded // output regardless of maxNodes. func appendBounded(dst xdm.ItemSlice, src []xdm.Item, maxNodes int) (xdm.ItemSlice, error) { - if maxNodes > 0 && len(dst)+len(src) > maxNodes { + if maxNodes > disabledSequenceLimit && len(dst)+len(src) > maxNodes { return nil, ErrNodeSetLimit } @@ -155,7 +173,7 @@ func appendBoundedSeq(ctx context.Context, ec *evalContext, dst xdm.ItemSlice, s return nil, err } - if maxNodes > 0 && len(dst)+1 > maxNodes { + if maxNodes > disabledSequenceLimit && len(dst)+1 > maxNodes { return nil, ErrNodeSetLimit } @@ -178,7 +196,7 @@ func appendBoundedClonedSeq(ctx context.Context, ec *evalContext, dst xdm.ItemSl return nil, err } - if maxNodes > 0 && len(dst)+1 > maxNodes { + if maxNodes > disabledSequenceLimit && len(dst)+1 > maxNodes { return nil, ErrNodeSetLimit } @@ -202,9 +220,9 @@ func evalSimpleMapExpr(evalFn exprEvaluator, ctx context.Context, ec *evalContex for item := range xdm.SequenceItems(left) { var frame evalContextFrame if ni, ok := item.(xdm.NodeItem); ok { - frame = ec.pushNodeContext(ni.Node, i+1, size) + frame = ec.pushNodeContext(ni.Node, i+focusPositionOrigin, size) } else { - frame = ec.pushContextItem(item, i+1, size) + frame = ec.pushContextItem(item, i+focusPositionOrigin, size) } r, err := evalFn(ctx, ec, e.Right) @@ -235,7 +253,7 @@ func simpleMapNodes(evalFn exprEvaluator, ctx context.Context, ec *evalContext, var result xdm.ItemSlice for i, n := range nodes { - frame := ec.pushNodeContext(n, i+1, len(nodes)) + frame := ec.pushNodeContext(n, i+focusPositionOrigin, len(nodes)) r, err := evalFn(ctx, ec, e.Right) ec.restoreContext(frame) @@ -280,11 +298,11 @@ func evalRangeExpr(evalFn exprEvaluator, ctx context.Context, ec *evalContext, e // XPath 1.0 compatibility mode: a range bound keeps only its first atom rather // than raising a cardinality error. if ec.xpath10CompatMode() { - sAtoms = sAtoms[:1] - eAtoms = eAtoms[:1] + sAtoms = sAtoms[:xpath10RangeRetainedAtoms] + eAtoms = eAtoms[:xpath10RangeRetainedAtoms] } - if len(sAtoms) > 1 || len(eAtoms) > 1 { + if len(sAtoms) > singleRangeOperand || len(eAtoms) > singleRangeOperand { return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "to operator operands must each be xs:integer? (at most one item)"} } @@ -341,6 +359,12 @@ func evalRangeExpr(evalFn exprEvaluator, ctx context.Context, ec *evalContext, e cur := new(big.Int).Set(start) for cur.Cmp(end) <= 0 { + // The int64 range is a lazy descriptor; this fallback actually allocates + // every magnitude, even when its caller consumes only count(). + if err := ChargeNumber(ctx, xdm.AtomicValue{TypeName: xdm.TypeInteger, Value: cur}); err != nil { + return nil, err + } + result = append(result, xdm.AtomicValue{TypeName: xdm.TypeInteger, Value: new(big.Int).Set(cur)}) cur.Add(cur, big.NewInt(1)) } @@ -448,7 +472,7 @@ func evalFilterExpr(evalFn exprEvaluator, ctx context.Context, ec *evalContext, return nil, err } - if ec.version == 10 { + if ec.version == xpathVersion10 { if err := xpath10NodeSet(base); err != nil { return nil, err } @@ -493,9 +517,9 @@ func applySequencePredicate(evalFn exprEvaluator, ctx context.Context, ec *evalC for item := range xdm.SequenceItems(seq) { var frame evalContextFrame if ni, ok := item.(xdm.NodeItem); ok { - frame = ec.pushNodeContext(ni.Node, i+1, size) + frame = ec.pushNodeContext(ni.Node, i+focusPositionOrigin, size) } else { - frame = ec.pushContextItem(item, i+1, size) + frame = ec.pushContextItem(item, i+focusPositionOrigin, size) } r, err := evalFn(ctx, ec, pred) @@ -505,7 +529,7 @@ func applySequencePredicate(evalFn exprEvaluator, ctx context.Context, ec *evalC return nil, err } - b, err := predicateTrue(r, i+1) + b, err := predicateTrue(r, i+focusPositionOrigin) if err != nil { return nil, err } @@ -537,7 +561,7 @@ func evalPathExpr(evalFn exprEvaluator, ctx context.Context, ec *evalContext, e result := make([]xdm.Node, 0, len(baseNodes)) for _, n := range baseNodes { - frame := ec.pushNodeContext(n, 1, 1) + frame := ec.pushNodeContext(n, focusPositionOrigin, singletonFocusSize) subResult, err := evalLocationPath(evalFn, ctx, ec, e.Path) ec.restoreContext(frame) @@ -548,16 +572,8 @@ func evalPathExpr(evalFn exprEvaluator, ctx context.Context, ec *evalContext, e subNodes, _ := xdm.NodesFrom(subResult) result = append(result, subNodes...) } - // Per XPath 3.1 §3.3.5: E1/E2 returns nodes in document order. - // However, when the filter is an ordering function (reverse, sort), - // preserve the explicit ordering and only deduplicate. - var deduped []xdm.Node - if filterPreservesOrder(ctx, ec, e.Filter) { - deduped, err = xdm.DeduplicateNodesPreserveOrder(result, ec.maxNodes) - } else { - deduped, err = xdm.DeduplicateNodes(result, ec.docOrder, ec.maxNodes) - } - + // Node results of E1/E2 are in document order, regardless of E1's order. + deduped, err := xdm.DeduplicateNodes(result, ec.docOrder, ec.maxNodes) if err != nil { return nil, err } @@ -594,12 +610,11 @@ func evalVMPathExpr(evalFn exprEvaluator, ctx context.Context, ec *evalContext, } // vmPathStepNodes evaluates the location path e.Path from every node of -// baseNodes and returns the results merged in document order, or in the -// order of baseNodes when e.OrderFilter is fn:reverse or fn:sort. +// baseNodes and returns the results merged in document order. func vmPathStepNodes(evalFn exprEvaluator, ctx context.Context, ec *evalContext, e vmPathExpr, baseNodes []xdm.Node) ([]xdm.Node, error) { result := make([]xdm.Node, 0, len(baseNodes)) for _, n := range baseNodes { - frame := ec.pushNodeContext(n, 1, 1) + frame := ec.pushNodeContext(n, focusPositionOrigin, singletonFocusSize) subNodes, err := evalVMLocationPathNodes(evalFn, ctx, ec, *e.Path) ec.restoreContext(frame) @@ -610,40 +625,9 @@ func vmPathStepNodes(evalFn exprEvaluator, ctx context.Context, ec *evalContext, result = append(result, subNodes...) } - if filterPreservesOrder(ctx, ec, e.OrderFilter) { - return xdm.DeduplicateNodesPreserveOrder(result, ec.maxNodes) - } - return xdm.DeduplicateNodes(result, ec.docOrder, ec.maxNodes) } -// filterPreservesOrder returns true if the filter expression is a call to the -// built-in fn:reverse or fn:sort, which explicitly control sequence order. In -// these cases, a subsequent path step (/@attr) should preserve the caller's -// ordering, re-sorting into no document order. -// -// The decision is made against the runtime function resolver so that a rebound -// "fn" prefix or a user function overriding the built-in is not mistaken for -// the order-controlling built-in. (Do not use the lexical streamability helper -// StreamFnLocalName here — that is a static spelling check, not a binding.) -func filterPreservesOrder(ctx context.Context, ec *evalContext, e Expr) bool { - fc, ok := e.(FunctionCall) - if !ok { - return false - } - - r, err := resolveFunctionInfo(ctx, ec, fc.Prefix, fc.Name, len(fc.Args)) - if err != nil { - return false - } - - if !r.isBuiltin || r.uri != NSFn { - return false - } - - return r.name == nameReverse || r.name == nameSort -} - // evalPathStepExpr evaluates E1/E2 where E2 is a non-axis expression. // Per XPath 3.1 §3.3.1.1: E1 must produce a node sequence (XPTY0019); E2 is // evaluated for each node with that node as context. If all results are nodes, @@ -674,7 +658,7 @@ func pathStepFromNodes(evalFn exprEvaluator, ctx context.Context, ec *evalContex hasNonNodes := false for i, n := range baseNodes { - frame := ec.pushNodeContext(n, i+1, len(baseNodes)) + frame := ec.pushNodeContext(n, i+focusPositionOrigin, len(baseNodes)) r, err := evalFn(ctx, ec, e.Right) ec.restoreContext(frame) @@ -712,13 +696,7 @@ func pathStepFromNodes(evalFn exprEvaluator, ctx context.Context, ec *evalContex } if hasNodes { - var err error - if filterPreservesOrder(ctx, ec, e.Left) { - allNodes, err = xdm.DeduplicateNodesPreserveOrder(allNodes, ec.maxNodes) - } else { - allNodes, err = xdm.DeduplicateNodes(allNodes, ec.docOrder, ec.maxNodes) - } - + allNodes, err := xdm.DeduplicateNodes(allNodes, ec.docOrder, ec.maxNodes) if err != nil { return nil, err } diff --git a/transform/internal/xpath/eval_path.go b/transform/internal/xpath/eval_path.go index 03a83b4..41ad0ae 100644 --- a/transform/internal/xpath/eval_path.go +++ b/transform/internal/xpath/eval_path.go @@ -14,6 +14,27 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) +// Name-test modes select the default-namespace rule: an unprefixed attribute +// has no namespace, while an element uses the static default. Namespace +// projection keeps the universally bound xml prefix (dropXML=false). +const ( + elementNamespaceMode = false + attributeNamespaceMode = true + namespaceDropXMLDisabled = false +) + +// These cardinalities describe distinct XPath contracts or recognition fast +// paths. Zero is the nonpositive boundary before the first valid focus position; +// position() itself is a zero-argument function. +const ( + qnameTokenCardinality = 1 + documentElementCardinality = 1 + singlePredicateResult = 1 + singleAttributePathStep = 1 + nonpositiveFocusPosition = 0 + positionFunctionArity = 0 +) + func evalLiteral(e LiteralExpr) (xdm.Sequence, error) { switch v := e.Value.(type) { case string: @@ -418,7 +439,7 @@ func unionMemberCastOK(val string, m xdm.NodeItemUnionMember, node xdm.Node, qna if m.Atomized == xdm.TypeQName || m.Atomized == xdm.TypeNOTATION || m.TypeName == xdm.TypeQName || m.TypeName == xdm.TypeNOTATION { - if len(xsdListFields(val)) != 1 { + if len(xsdListFields(val)) != qnameTokenCardinality { return false } @@ -453,7 +474,7 @@ func inScopeNSMap(n xdm.Node) map[string]string { } out := make(map[string]string) - for prefix, ns := range xdm.InScopeNamespaces(e, false) { + for prefix, ns := range xdm.InScopeNamespaces(e, namespaceDropXMLDisabled) { out[prefix] = ns.URI() } @@ -549,7 +570,7 @@ func applyVMPredicate(evalFn exprEvaluator, ctx context.Context, ec *evalContext } func applyVMPositionPredicate(nodes []xdm.Node, pred vmPositionPredicateExpr) []xdm.Node { - if pred.Position <= 0 || pred.Position > len(nodes) { + if pred.Position <= nonpositiveFocusPosition || pred.Position > len(nodes) { return nil } @@ -591,7 +612,7 @@ func applyVMAttributeEqualsStringPredicate(evalFn exprEvaluator, ctx context.Con } if !ok { - frame := ec.pushNodeContext(n, i+1, size) + frame := ec.pushNodeContext(n, i+focusPositionOrigin, size) r, err := evalFn(ctx, ec, pred.Fallback) ec.restoreContext(frame) @@ -599,7 +620,7 @@ func applyVMAttributeEqualsStringPredicate(evalFn exprEvaluator, ctx context.Con return nil, err } - predMatch, err := predicateTrue(r, i+1) + predMatch, err := predicateTrue(r, i+focusPositionOrigin) if err != nil { return nil, err } @@ -623,6 +644,7 @@ func vmAttributeEqualsStringPredicateMatches(ctx context.Context, ec *evalContex mustFallback := false matched := false + collation := ec.resolveDefaultCollation() var cancelErr error @@ -641,7 +663,12 @@ func vmAttributeEqualsStringPredicateMatches(ctx context.Context, ec *evalContex return false } - if attr.Value() == pred.Value { + equal := attr.Value() == pred.Value + if collation != nil { + equal = collation.compare(attr.Value(), pred.Value) == 0 + } + + if equal { matched = true return false } @@ -838,7 +865,7 @@ func matchElementTest(test xdm.ElementTest, n xdm.Node, ec *evalContext) bool { } if test.Name != "" && test.Name != "*" { - if !matchElementOrAttributeName(test.Name, n, ec, false) { + if !matchElementOrAttributeName(test.Name, n, ec, elementNamespaceMode) { return false } } @@ -869,7 +896,7 @@ func matchAttributeTest(test xdm.AttributeTest, n xdm.Node, ec *evalContext) boo } if test.Name != "" && test.Name != "*" { - if !matchElementOrAttributeName(test.Name, n, ec, true) { + if !matchElementOrAttributeName(test.Name, n, ec, attributeNamespaceMode) { return false } } @@ -903,7 +930,7 @@ func matchDocumentTest(test xdm.DocumentTest, n xdm.Node, ec *evalContext) bool switch c.Type() { case xdm.ElementNode: elemCount++ - if elemCount > 1 { + if elemCount > documentElementCardinality { return false } @@ -915,7 +942,7 @@ func matchDocumentTest(test xdm.DocumentTest, n xdm.Node, ec *evalContext) bool } } - return elemCount == 1 && matchedInner + return elemCount == documentElementCardinality && matchedInner } return true @@ -937,7 +964,7 @@ func matchSchemaElementTest(test xdm.SchemaElementTest, n xdm.Node, ec *evalCont return xdm.LocalNameOf(n) == local } - local, ns := resolveSchemaTestName(test.Name, ec, false) + local, ns := resolveSchemaTestName(test.Name, ec, elementNamespaceMode) nameMatch := xdm.LocalNameOf(n) == local && xdm.NodeNamespaceURI(n) == ns if !nameMatch { @@ -994,7 +1021,7 @@ func matchSchemaAttributeTest(test xdm.SchemaAttributeTest, n xdm.Node, ec *eval return attr.LocalName() == local } - local, ns := resolveSchemaTestName(test.Name, ec, true) + local, ns := resolveSchemaTestName(test.Name, ec, attributeNamespaceMode) if attr.LocalName() != local || attr.URI() != ns { return false } @@ -1102,7 +1129,7 @@ func matchNameTest(test xdm.NameTest, n xdm.Node, axis AxisType, ec *evalContext } if test.Local == "*" { - if test.URI != "" { + if test.URIQualified || test.URI != "" { return xdm.NodeNamespaceURI(n) == test.URI } @@ -1117,7 +1144,7 @@ func matchNameTest(test xdm.NameTest, n xdm.Node, axis AxisType, ec *evalContext return false } - if test.URI != "" { + if test.URIQualified || test.URI != "" { return xdm.NodeNamespaceURI(n) == test.URI } @@ -1193,7 +1220,7 @@ func applyPredicate(evalFn exprEvaluator, ctx context.Context, ec *evalContext, var result []xdm.Node for i, n := range nodes { - frame := ec.pushNodeContext(n, i+1, size) + frame := ec.pushNodeContext(n, i+focusPositionOrigin, size) r, err := evalFn(ctx, ec, pred) ec.restoreContext(frame) @@ -1201,7 +1228,7 @@ func applyPredicate(evalFn exprEvaluator, ctx context.Context, ec *evalContext, return nil, err } - match, err := predicateTrue(r, i+1) + match, err := predicateTrue(r, i+focusPositionOrigin) if err != nil { return nil, err } @@ -1284,7 +1311,7 @@ func resolveTestTypeName(raw string, ec *evalContext) string { // predicateTrue evaluates a predicate result per XPath spec: // numeric → compare to position, otherwise → EBV. func predicateTrue(r xdm.Sequence, position int) (bool, error) { - if xdm.SequenceLen(r) == 1 { + if xdm.SequenceLen(r) == singlePredicateResult { if av, ok := r.Get(0).(xdm.AtomicValue); ok && av.IsNumeric() { return compareNumeric(TokenEq, xdm.PromoteSchemaType(av), xdm.AtomicValue{ TypeName: xdm.TypeInteger, Value: int64(position), @@ -1357,13 +1384,13 @@ func vmPositionFromLiteral(expr LiteralExpr) (int, bool) { } n := v.Int64() - if n <= 0 || n > math.MaxInt { + if n <= nonpositiveFocusPosition || n > math.MaxInt { return 0, false } return int(n), true case float64: - if v <= 0 || v != math.Trunc(v) || v > math.MaxInt { + if v <= nonpositiveFocusPosition || v != math.Trunc(v) || v > math.MaxInt { return 0, false } @@ -1377,7 +1404,7 @@ func positionCall(expr Expr) bool { switch e := expr.(type) { case FunctionCall: local, isFn := lexicalFnLocalName(e.Name, e.Prefix) - return len(e.Args) == 0 && isFn && local == lexicon.FnPosition + return len(e.Args) == positionFunctionArity && isFn && local == lexicon.FnPosition case *FunctionCall: if e == nil { return false @@ -1385,7 +1412,7 @@ func positionCall(expr Expr) bool { local, isFn := lexicalFnLocalName(e.Name, e.Prefix) - return len(e.Args) == 0 && isFn && local == lexicon.FnPosition + return len(e.Args) == positionFunctionArity && isFn && local == lexicon.FnPosition default: return false } @@ -1454,7 +1481,7 @@ func vmSingleRelativeAttributePath(expr Expr) (xdm.NodeTest, bool) { } func vmAttributeNodeTestFromLocationPath(absolute bool, steps []Step) (xdm.NodeTest, bool) { - if absolute || len(steps) != 1 { + if absolute || len(steps) != singleAttributePathStep { return nil, false } @@ -1467,7 +1494,7 @@ func vmAttributeNodeTestFromLocationPath(absolute bool, steps []Step) (xdm.NodeT } func vmAttributeNodeTestFromVMLocationPath(absolute bool, steps []vmLocationStep) (xdm.NodeTest, bool) { - if absolute || len(steps) != 1 { + if absolute || len(steps) != singleAttributePathStep { return nil, false } diff --git a/transform/internal/xpath/eval_path_descendant.go b/transform/internal/xpath/eval_path_descendant.go index 7f83728..34d442f 100644 --- a/transform/internal/xpath/eval_path_descendant.go +++ b/transform/internal/xpath/eval_path_descendant.go @@ -10,6 +10,16 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) +// Fusion checks a single context separately. Descendant-or-self includes its +// one starting node. Ordered results above one node register their document and +// clamp capacity; these cardinalities have independent algorithmic roles. +const ( + singleFusionContext = 1 + descendantSelfNodeCount = 1 + singleOrderedNodeResult = 1 + measureDescendantSubtrees = true +) + // Fuse descendant-or-self::node() plus a child/attribute step into a // preorder walk, producing document order without a whole-document order index. // Predicates still see each parent's candidate list, so position() and last() @@ -56,7 +66,7 @@ func fusesDescendantContexts(nodes []xdm.Node) bool { return false } - if len(nodes) == 1 { + if len(nodes) == singleFusionContext { return xdm.OrderedFrom(nodes[0]) } @@ -217,7 +227,7 @@ func appendDescendantMatches(ctx context.Context, ec *evalContext, out []xdm.Nod return nil, 0, 0, err } - visited := 1 + visited := descendantSelfNodeCount candidates := 0 if step.Axis == AxisAttribute { @@ -336,7 +346,7 @@ func descendantStepWithPredicates(evalFn exprEvaluator, ctx context.Context, ec // charge nothing, so the second step's charges can wait until the walk ends. func descendantStepQuietPredicates(evalFn exprEvaluator, ctx context.Context, ec *evalContext, contexts []xdm.Node, step vmLocationStep) ([]xdm.Node, error) { total := 0 - w := descendantWalker{step: step, counting: true} + w := descendantWalker{step: step, counting: measureDescendantSubtrees} for i := 0; i < len(contexts); { c := contexts[i] @@ -346,7 +356,7 @@ func descendantStepQuietPredicates(evalFn exprEvaluator, ctx context.Context, ec return nil, err } - if ec.maxNodes < 1 { + if ec.maxNodes < descendantSelfNodeCount { return nil, xdm.ErrNodeSetLimit } @@ -586,7 +596,7 @@ func startDescendantWalk(ctx context.Context, ec *evalContext, c xdm.Node) ([]xd return nil, err } - if ec.maxNodes < 1 { + if ec.maxNodes < descendantSelfNodeCount { return nil, xdm.ErrNodeSetLimit } @@ -606,7 +616,7 @@ func countDescendantOrSelf(ctx context.Context, ec *evalContext, c xdm.Node, siz return 0, err } - visited := 1 + visited := descendantSelfNodeCount for len(stack) > 0 { if err := ctx.Err(); err != nil { @@ -739,7 +749,7 @@ func finishDescendantOrSelfStep(ec *evalContext, c xdm.Node, total int) error { return xdm.ErrNodeSetLimit } - if total > 1 { + if total > singleOrderedNodeResult { ec.docOrder.ReserveDocument(c) } @@ -751,7 +761,7 @@ func finishDescendantOrSelfStep(ec *evalContext, c xdm.Node, total int) error { // than maxNodes nodes fails, and the capacity of a longer-than-one result is // clamped. func clampDescendantResult(ec *evalContext, out []xdm.Node) ([]xdm.Node, error) { - if len(out) <= 1 { + if len(out) <= singleOrderedNodeResult { return out, nil } diff --git a/transform/internal/xpath/eval_path_exists.go b/transform/internal/xpath/eval_path_exists.go index 7a952ba..046ce2b 100644 --- a/transform/internal/xpath/eval_path_exists.go +++ b/transform/internal/xpath/eval_path_exists.go @@ -10,6 +10,25 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) +// An existence probe runs at most one fused descendant walk depth first; that +// walk consumes two VM steps. Inline counters cover eight steps before spilling. +const ( + singleProbeDescendantWalk = 1 + fusedProbeStepCount = 2 + inlineProbeStepCounts = 8 +) + +// One selected node proves existence, while two nodes settle a step's document +// registration. Walked flags distinguish a fused walk from an ordinary step; +// predicate evaluation retains one logical operation per inspected node. +const ( + minimumProbeMatches = 1 + probeRegistrationNodeCount = 2 + ordinaryProbeTraversal = false + descendantProbeTraversal = true + probePredicateWork = 1 +) + // Existence-only paths stop after selecting a node. Depth-first execution // is limited to steps that cannot revisit a node through different contexts; // earlier steps use ordinary materialized evaluation (pathExistsSplit). @@ -20,7 +39,8 @@ import ( // nodes are not observed. Document registration must be settled before stopping // so later cross-document unions retain their ordering. -// maxPooledProbeBuffer is the largest buffer capacity a pooled probe keeps. +// maxPooledProbeBuffer bounds retained capacity in buffer ELEMENTS, not bytes. +// Node, integer-count and pending-subtree buffers have different element sizes. const maxPooledProbeBuffer = 1024 // foundSlack is how many more nodes a probe that has found a node, but has @@ -55,7 +75,7 @@ func pathExistsSplit(steps []vmLocationStep) int { lastWalk = i walks++ - i += 2 + i += fusedProbeStepCount continue } @@ -69,9 +89,9 @@ func pathExistsSplit(steps []vmLocationStep) int { } switch { - case walks > 1: + case walks > singleProbeDescendantWalk: return lastWalk - case walks == 1 && split > 0 && firstWalk != split: + case walks == singleProbeDescendantWalk && split > 0 && firstWalk != split: return firstWalk } @@ -129,7 +149,7 @@ func pathExists(evalFn exprEvaluator, ctx context.Context, ec *evalContext, lp v p.steps = lp.Steps p.resultRegisters = resultRegisters // A step result of more than one node has registered its document. - p.reserved = len(contexts) > 1 + p.reserved = len(contexts) > singleOrderedNodeResult if len(lp.Steps) <= len(p.countsBuf) { p.counts = p.countsBuf[:len(lp.Steps)] clear(p.counts) @@ -167,7 +187,7 @@ type pathProbe struct { // walk is the pre-order stack of the `//` walk, of which a probe runs at // most one at a time. walk []xdm.Node - countsBuf [8]int + countsBuf [inlineProbeStepCounts]int // pending counts the operations the `//` walk has done and not charged // yet. pending int @@ -285,7 +305,7 @@ func (p *pathProbe) run(evalFn exprEvaluator, ctx context.Context, i int, contex return false, err } - if len(nodes) > 1 { + if len(nodes) > singleOrderedNodeResult { p.reserved = true } @@ -330,7 +350,7 @@ func (p *pathProbe) run(evalFn exprEvaluator, ctx context.Context, i int, contex return false, err } - stop, err := p.selectFrom(evalFn, ctx, i, p.buf[mark:], false) + stop, err := p.selectFrom(evalFn, ctx, i, p.buf[mark:], ordinaryProbeTraversal) p.buf = p.buf[:mark] if err != nil || stop { @@ -387,7 +407,7 @@ func (p *pathProbe) runChildren(evalFn exprEvaluator, ctx context.Context, i int traversed = 0 - stop, err := p.selectNode(evalFn, ctx, i, child, false) + stop, err := p.selectNode(evalFn, ctx, i, child, ordinaryProbeTraversal) if err != nil || stop { return stop, err } @@ -445,7 +465,7 @@ func (p *pathProbe) selectFrom(evalFn exprEvaluator, ctx context.Context, i int, func (p *pathProbe) selectNode(evalFn exprEvaluator, ctx context.Context, i int, n xdm.Node, walked bool) (bool, error) { mark := len(p.buf) p.buf = append(p.buf, n) - stop, err := p.selectNodes(evalFn, ctx, i, p.buf[mark:], 1, walked) + stop, err := p.selectNodes(evalFn, ctx, i, p.buf[mark:], minimumProbeMatches, walked) p.buf = p.buf[:mark] return stop, err @@ -465,7 +485,7 @@ func (p *pathProbe) selectNodes(evalFn exprEvaluator, ctx context.Context, i int // A step result of more than one node registers its document. The // result of a `//` walk does not (clampDescendantResult): the walk // registers it for the nodes it covers. - if p.counts[i] > 1 && (!walked || (last && p.resultRegisters)) { + if p.counts[i] > singleOrderedNodeResult && (!walked || (last && p.resultRegisters)) { p.reserve() } @@ -495,9 +515,9 @@ func (p *pathProbe) countLastPredicate(evalFn exprEvaluator, ctx context.Context return len(selected), err } - need := 1 + need := minimumProbeMatches if !p.settled() { - need = max(1, 2-p.counts[i]) + need = max(minimumProbeMatches, probeRegistrationNodeCount-p.counts[i]) } return countPredicateUpTo(evalFn, ctx, p.ec, nodes, pred, need) @@ -515,7 +535,7 @@ func (p *pathProbe) runDescendant(evalFn exprEvaluator, ctx context.Context, i i // than one node (finishDescendantOrSelfStep): with several context // nodes, or one with a child. Registering first keeps it ahead of any // document a predicate registers, as in the step-by-step evaluation. - if len(contexts) > 1 || hasXDMChild(contexts[0]) { + if len(contexts) > singleOrderedNodeResult || hasXDMChild(contexts[0]) { p.reserve() } @@ -533,7 +553,7 @@ func (p *pathProbe) runDescendant(evalFn exprEvaluator, ctx context.Context, i i return false, err } - if ec.maxNodes < 1 { + if ec.maxNodes < descendantSelfNodeCount { return false, xdm.ErrNodeSetLimit } @@ -615,7 +635,7 @@ func (p *pathProbe) walkSubtree(evalFn exprEvaluator, ctx context.Context, si in err error ) if streaming { - stop, err = p.walkChild(evalFn, ctx, si, cur, visited > 1, !last) + stop, err = p.walkChild(evalFn, ctx, si, cur, visited > descendantSelfNodeCount, !last) } else { stop, err = p.walkParent(evalFn, ctx, si, cur, chargeEach) } @@ -662,7 +682,7 @@ func (p *pathProbe) walkChild(evalFn exprEvaluator, ctx context.Context, si int, } } - stop, err := p.selectNode(evalFn, ctx, si, cur, true) + stop, err := p.selectNode(evalFn, ctx, si, cur, descendantProbeTraversal) if err != nil || stop { return stop, err } @@ -710,7 +730,7 @@ func (p *pathProbe) walkParent(evalFn exprEvaluator, ctx context.Context, si int } } // The next steps leave p.walk alone: a probe runs one `//` walk. - stop, err := p.selectFrom(evalFn, ctx, si, p.buf[mark:], true) + stop, err := p.selectFrom(evalFn, ctx, si, p.buf[mark:], descendantProbeTraversal) p.buf = p.buf[:mark] return stop, err @@ -745,11 +765,11 @@ func countPredicateUpTo(evalFn exprEvaluator, ctx context.Context, ec *evalConte kept := 0 for i, n := range nodes { - if err := ec.countOps(ctx, 1); err != nil { + if err := ec.countOps(ctx, probePredicateWork); err != nil { return 0, err } - frame := ec.pushNodeContext(n, i+1, size) + frame := ec.pushNodeContext(n, i+focusPositionOrigin, size) r, err := evalFn(ctx, ec, pred) ec.restoreContext(frame) @@ -757,7 +777,7 @@ func countPredicateUpTo(evalFn exprEvaluator, ctx context.Context, ec *evalConte return 0, err } - match, err := predicateTrue(r, i+1) + match, err := predicateTrue(r, i+focusPositionOrigin) if err != nil { return 0, err } diff --git a/transform/internal/xpath/eval_reuse.go b/transform/internal/xpath/eval_reuse.go index 015b7b7..78c7c08 100644 --- a/transform/internal/xpath/eval_reuse.go +++ b/transform/internal/xpath/eval_reuse.go @@ -12,6 +12,14 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) +// Single-item results borrow one reusable slot until the next evaluation. +// StringValue separates multiple items with a single space, preserving its +// established display conversion independently of serialization parameters. +const ( + singleReuseResultItem = 1 + reuseResultItemSeparator = ' ' +) + // EvalState holds pre-allocated evaluation state that can be reused // across multiple EvaluateReuse calls on the same Expression. // Not safe for concurrent use. @@ -22,7 +30,7 @@ import ( type EvalState struct { // ec retains mutable evaluation context and scratch storage between calls. ec evalContext - oneItem [1]xdm.Item // reusable backing for single-item results + oneItem [singleReuseResultItem]xdm.Item // reusable backing for single-item results // nodes is the reusable backing of a result that is a node list. nodes resultNodes @@ -39,6 +47,8 @@ type EvalState struct { baseNamespaces map[string]string // baseXMLNamespace restores the host XML default namespace for unbound expressions. baseXMLNamespace string + // baseDefaultCollation restores the evaluator collation between expressions. + baseDefaultCollation string // explicitTime is true when the Evaluator had an explicitly configured // CurrentTime. When false, EvaluateReuse refreshes the clock per call so @@ -86,13 +96,18 @@ func (e *Expression) EvaluateReuse(ctx context.Context, state *EvalState, node x ec.version = e.version ec.defaultXMLNamespace = state.baseXMLNamespace + ec.defaultCollation = state.baseDefaultCollation + if e.staticCollation != "" { + ec.defaultCollation = e.staticCollation + } + ec.namespaces = state.baseNamespaces if e.staticNamespaces != nil { ec.namespaces = e.staticNamespaces ec.defaultXMLNamespace = e.defaultXMLNamespace } - ec.xpath10Compat = state.baseCompat || e.version == 10 + ec.xpath10Compat = state.baseCompat || e.version == xpathVersion10 applyBudget(ctx, ec) // Start from the base focus seeded by NewEvalState/SetContextItem so a @@ -141,7 +156,7 @@ func (e *Expression) EvaluateReuse(ctx context.Context, state *EvalState, node x return Result{seq: xdm.ItemSlice(state.oneItem[:])}, nil } - if err := e.prefixPlan.Validate(ec.namespaces, e.staticNamespaces != nil || ec.strictPrefixes || e.version == 10, ec.schemaDeclarations, ec.version); err != nil { + if err := e.prefixPlan.Validate(ec.namespaces, e.staticNamespaces != nil || ec.strictPrefixes || e.version == xpathVersion10, ec.schemaDeclarations, ec.version); err != nil { return Result{}, err } @@ -167,7 +182,7 @@ func (r Result) StringValue() string { switch len(rn.nodes) { case 0: return "" - case 1: + case singleReuseResultItem: return xdm.StringValue(rn.nodes[0]) } } @@ -177,7 +192,7 @@ func (r Result) StringValue() string { return "" } - if xdm.SequenceLen(seq) == 1 { + if xdm.SequenceLen(seq) == singleReuseResultItem { switch v := seq.Get(0).(type) { case xdm.NodeItem: return xdm.StringValue(v.Node) @@ -192,7 +207,7 @@ func (r Result) StringValue() string { i := 0 for item := range xdm.SequenceItems(seq) { if i > 0 { - sb.WriteByte(' ') + sb.WriteByte(reuseResultItemSeparator) } av, err := AtomizeItem(item) @@ -233,6 +248,7 @@ func (e Evaluator) NewEvalState(node xdm.Node) *EvalState { s.baseCompat = s.ec.xpath10Compat s.baseNamespaces = s.ec.namespaces s.baseXMLNamespace = s.ec.defaultXMLNamespace + s.baseDefaultCollation = s.ec.defaultCollation s.baseNode = s.ec.node s.baseContextItem = s.ec.contextItem diff --git a/transform/internal/xpath/eval_types.go b/transform/internal/xpath/eval_types.go index 6c09b57..a0bafec 100644 --- a/transform/internal/xpath/eval_types.go +++ b/transform/internal/xpath/eval_types.go @@ -15,6 +15,24 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) +// Cast and occurrence contracts accept a singleton where specified. A zero +// atom bound means no singleton-specific stopping cap; node/work limits remain. +const ( + singletonTypeCardinality = 1 + unboundedCoercionAtoms = 0 +) + +// Type binding and user-list token conversion charge independent logical work. +// The list capacity is an item allocation hint, while schema-base lookup stops +// after 32 declarations; neither value changes the atomization ancestry ceiling. +const ( + sequenceTypeBindingWork = 1 + userListTokenWork = 1 + userListInitialCapacity = 8 + maxSchemaBaseLookupDepth = 32 + generatedQNamePrefixFormat = "ns%d" +) + // errCoerceMismatch is a sentinel returned by coerceToSequenceTypeE for a plain // type or cardinality mismatch — one with no more-specific typed error. Callers // map it to XPTY0004. A more-specific typed error (FOTY0013 from atomizing a @@ -41,7 +59,7 @@ func CoerceToSequenceTypeContext(ctx context.Context, seq xdm.Sequence, st xdm.S // Plain type/cardinality mismatches are XPTY0004; cast, atomization and control // errors retain their own codes and causes. func ConvertToSequenceType(ctx context.Context, seq xdm.Sequence, st xdm.SequenceType) (xdm.Sequence, error) { - return (Evaluator{}).ConvertToSequenceType(ctx, seq, st, 31) + return (Evaluator{}).ConvertToSequenceType(ctx, seq, st, xpathVersion31) } // ConvertToSequenceType applies function conversion in this evaluator's static @@ -63,7 +81,7 @@ func (e Evaluator) ConvertToSequenceType(ctx context.Context, seq xdm.Sequence, // BindSequenceType expands signature names at their declaration site. Function // metadata can then cross modules and survive a caller rebinding its prefixes. func BindSequenceType(ctx context.Context, st xdm.SequenceType, namespaces map[string]string, defaultNS string) (xdm.SequenceType, error) { - if err := Charge(ctx, 1); err != nil { + if err := Charge(ctx, sequenceTypeBindingWork); err != nil { return st, err } @@ -211,7 +229,7 @@ func qnameCastLexical(qv xdm.QNameValue, base map[string]string) (string, map[st prefix := qv.Prefix if prefix == "" { for i := 0; ; i++ { - cand := fmt.Sprintf("ns%d", i) + cand := fmt.Sprintf(generatedQNamePrefixFormat, i) if _, taken := ns[cand]; !taken { prefix = cand break @@ -390,7 +408,7 @@ func schemaAwareCastViaBuiltin(ctx context.Context, ec *evalContext, av xdm.Atom } func evalCastExpr(evalFn exprEvaluator, ctx context.Context, ec *evalContext, e CastExpr) (xdm.Sequence, error) { - if err := validateCastTarget(e.Type, ec.namespaces, ec.schemaDeclarations, ec.version, ec.strictPrefixes || ec.version == 10); err != nil { + if err := validateCastTarget(e.Type, ec.namespaces, ec.schemaDeclarations, ec.version, ec.strictPrefixes || ec.version == xpathVersion10); err != nil { return nil, err } @@ -417,7 +435,7 @@ func evalCastExpr(evalFn exprEvaluator, ctx context.Context, ec *evalContext, e return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "cast requires non-empty sequence"} } - if len(atoms) > 1 { + if len(atoms) > singletonTypeCardinality { return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "cast requires singleton"} } @@ -466,7 +484,7 @@ func evalCastExpr(evalFn exprEvaluator, ctx context.Context, ec *evalContext, e } func evalCastableExpr(evalFn exprEvaluator, ctx context.Context, ec *evalContext, e CastableExpr) (xdm.Sequence, error) { - if err := validateCastTarget(e.Type, ec.namespaces, ec.schemaDeclarations, ec.version, ec.strictPrefixes || ec.version == 10); err != nil { + if err := validateCastTarget(e.Type, ec.namespaces, ec.schemaDeclarations, ec.version, ec.strictPrefixes || ec.version == xpathVersion10); err != nil { return nil, err } @@ -498,7 +516,7 @@ func evalCastableExpr(evalFn exprEvaluator, ctx context.Context, ec *evalContext return xdm.SingleBoolean(e.AllowEmpty), nil } - if len(atoms) > 1 { + if len(atoms) > singletonTypeCardinality { return xdm.SingleBoolean(false), nil } @@ -579,14 +597,14 @@ func castToUserList(ctx context.Context, ec *evalContext, av xdm.AtomicValue, li } limit := NodeLimit(ctx, fnMaxNodes(ec)) - items := make(xdm.ItemSlice, 0, min(limit, 8)) + items := make(xdm.ItemSlice, 0, min(limit, userListInitialCapacity)) for token := range strings.FieldsFuncSeq(s, xmlchar.IsSpace) { if len(items) >= limit { return nil, ErrNodeSetLimit } - if err := fnCountOps(ctx, ec, 1); err != nil { + if err := fnCountOps(ctx, ec, userListTokenWork); err != nil { return nil, err } @@ -631,7 +649,7 @@ func resolveToBuiltinBase(local, ns string, decls SchemaDeclarations) string { current := local currentNS := ns - for range 32 { + for range maxSchemaBaseLookupDepth { baseType, ok := decls.LookupSchemaType(current, currentNS) if !ok { return "" @@ -665,7 +683,7 @@ func resolveAtomicTypeName(tn AtomicTypeName, ec *evalContext) string { uri := resolveStaticNameURI(lexical, namespaces, namespaces[""]) if uri == lexicon.NamespaceXSD { - return "xs:" + local + return xsdCastTypePrefix + local } return xdm.QAnnotation(uri, local) @@ -830,9 +848,9 @@ func coerceFunctionSequence(ctx context.Context, seq xdm.Sequence, occurrence xd func coercionOccurrenceMatches(length int, occurrence xdm.Occurrence) bool { switch occurrence { case xdm.OccurrenceExactlyOne: - return length == 1 + return length == singletonTypeCardinality case xdm.OccurrenceZeroOrOne: - return length <= 1 + return length <= singletonTypeCardinality case xdm.OccurrenceOneOrMore: return length > 0 default: @@ -844,9 +862,9 @@ func coercionOccurrenceMatches(length int, occurrence xdm.Occurrence) bool { // stop after the second atom; typed-value errors encountered before that limit // still propagate, before the cardinality mismatch is reported. func visitCoercionAtoms(ctx context.Context, seq xdm.Sequence, occurrence xdm.Occurrence, ec *evalContext, yield func(xdm.AtomicValue)) error { - maxAtoms := 0 + maxAtoms := unboundedCoercionAtoms if occurrence == xdm.OccurrenceExactlyOne || occurrence == xdm.OccurrenceZeroOrOne { - maxAtoms = 1 + maxAtoms = singletonTypeCardinality } count := 0 @@ -861,7 +879,7 @@ func visitCoercionAtoms(ctx context.Context, seq xdm.Sequence, occurrence xdm.Oc yield(atom) - if maxAtoms > 0 && count > maxAtoms { + if maxAtoms > unboundedCoercionAtoms && count > maxAtoms { tooMany = true return false, nil } @@ -905,8 +923,8 @@ func coerceAtomicFunctionArgument(ctx context.Context, value xdm.AtomicValue, ta } if value.TypeName == xdm.TypeUntypedAtomic { - if ec != nil && ec.version >= 30 && (target == xdm.TypeQName || target == xdm.TypeNOTATION) { - return xdm.AtomicValue{}, &xdm.Error{Code: "XPTY0117", Message: "untypedAtomic cannot be converted to a namespace-sensitive type by function conversion"} + if ec != nil && ec.version >= xpathVersion30 && (target == xdm.TypeQName || target == xdm.TypeNOTATION) { + return xdm.AtomicValue{}, &xdm.Error{Code: errCodeXPTY0117, Message: "untypedAtomic cannot be converted to a namespace-sensitive type by function conversion"} } if target == xdm.TypeNumeric { @@ -946,13 +964,13 @@ func coerceFunctionItem(item xdm.Item, target xdm.FunctionTest, ec *evalContext) return nil, false } - if actual.Arity >= 0 && actual.Arity != len(target.ParamTypes) { + if actual.Arity >= finiteArityFloor && actual.Arity != len(target.ParamTypes) { return nil, false } if ec == nil { ec = (Evaluator{}).newEvalCtx(nil) - ec.version = 31 + ec.version = xpathVersion31 } paramTypes := append([]xdm.SequenceType(nil), target.ParamTypes...) @@ -1039,7 +1057,7 @@ func coerceFunctionItemArgs(ctx context.Context, actual xdm.FunctionItem, args [ if err != nil { return nil, &xdm.Error{ Code: lexicon.ErrXPTY0004, - Message: fmt.Sprintf("function argument %d does not match required type %v", i+1, parameter), + Message: fmt.Sprintf("function argument %d does not match required type %v", i+functionArgumentOrigin, parameter), } } @@ -1162,11 +1180,11 @@ func matchesSequenceType(seq xdm.Sequence, st xdm.SequenceType, ec *evalContext) switch st.Occurrence { case xdm.OccurrenceExactlyOne: - if xdm.SequenceLen(seq) != 1 { + if xdm.SequenceLen(seq) != singletonTypeCardinality { return false } case xdm.OccurrenceZeroOrOne: - if xdm.SequenceLen(seq) > 1 { + if xdm.SequenceLen(seq) > singletonTypeCardinality { return false } case xdm.OccurrenceOneOrMore: @@ -1266,7 +1284,7 @@ func matchesSchemaElementItem(node xdm.NodeItem, test xdm.SchemaElementTest, ec return false } - local, ns := resolveSchemaTestName(test.Name, ec, false) + local, ns := resolveSchemaTestName(test.Name, ec, elementNamespaceMode) annotation := node.TypeAnnotation if annotation == "" { @@ -1294,7 +1312,7 @@ func matchesSchemaAttributeItem(node xdm.NodeItem, test xdm.SchemaAttributeTest, return false } - local, ns := resolveSchemaTestName(test.Name, ec, true) + local, ns := resolveSchemaTestName(test.Name, ec, attributeNamespaceMode) if xdm.LocalNameOf(node.Node) != local || xdm.NodeNamespaceURI(node.Node) != ns { return false } @@ -1330,7 +1348,7 @@ func matchesFunctionItemTest(item xdm.Item, test xdm.FunctionTest, ec *evalConte case xdm.MapItem: return matchesMapFunctionItem(value, test, ec) case xdm.ArrayItem: - return len(test.ParamTypes) == 1 && + return len(test.ParamTypes) == collectionLookupArity && isSequenceSubtype(test.ParamTypes[0], stAtomic(xdm.TypeInteger, xdm.OccurrenceExactlyOne), ec) && arrayMembersMatchType(value, test.ReturnType, ec) default: @@ -1339,7 +1357,7 @@ func matchesFunctionItemTest(item xdm.Item, test xdm.FunctionTest, ec *evalConte } func matchesTypedFunctionItem(value xdm.FunctionItem, test xdm.FunctionTest, ec *evalContext) bool { - if value.Arity >= 0 && value.Arity != len(test.ParamTypes) { + if value.Arity >= finiteArityFloor && value.Arity != len(test.ParamTypes) { return false } @@ -1361,7 +1379,7 @@ func matchesTypedFunctionItem(value xdm.FunctionItem, test xdm.FunctionTest, ec } func matchesMapFunctionItem(value xdm.MapItem, test xdm.FunctionTest, ec *evalContext) bool { - if len(test.ParamTypes) != 1 || + if len(test.ParamTypes) != collectionLookupArity || !isSequenceSubtype(test.ParamTypes[0], stAtomic(xdm.TypeAnyAtomicType, xdm.OccurrenceExactlyOne), ec) { return false } @@ -1632,7 +1650,7 @@ func isElementTestSubtype(a xdm.NodeTest, bt xdm.ElementTest, ec *evalContext) b return true // element() matches any element } - if bt.Name != "" && bt.Name != "*" && !sameTypeTestName(at.Name, bt.Name, ec, false) { + if bt.Name != "" && bt.Name != "*" && !sameTypeTestName(at.Name, bt.Name, ec, elementNamespaceMode) { return false } // Check type annotation: if bt specifies a type, at must have a @@ -1663,7 +1681,7 @@ func isAttributeTestSubtype(a xdm.NodeTest, bt xdm.AttributeTest, ec *evalContex return false } - if bt.Name != "" && bt.Name != "*" && !sameTypeTestName(at.Name, bt.Name, ec, true) { + if bt.Name != "" && bt.Name != "*" && !sameTypeTestName(at.Name, bt.Name, ec, attributeNamespaceMode) { return false } @@ -1755,7 +1773,7 @@ func isAnyItemTest(t xdm.NodeTest) bool { // XPath 2.0 restricts lexical QName casts to string literal operands. Inspect // the source expression, rather than an evaluated or constant-folded value. func qnameCastOperandAllowed(ec *evalContext, source Expr, value xdm.AtomicValue) bool { - if ec == nil || ec.version != 20 || value.TypeName == xdm.TypeQName || value.BaseType == xdm.TypeQName { + if ec == nil || ec.version != xpathVersion20 || value.TypeName == xdm.TypeQName || value.BaseType == xdm.TypeQName { return true } diff --git a/transform/internal/xpath/evaluator.go b/transform/internal/xpath/evaluator.go index 568772d..4c207e3 100644 --- a/transform/internal/xpath/evaluator.go +++ b/transform/internal/xpath/evaluator.go @@ -5,6 +5,7 @@ package xpath import ( "context" + "fmt" "io" "maps" "time" @@ -13,6 +14,24 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) +// Initial focus defaults are independent position/count values. Positive +// configured positions override their default; size zero selects its default, +// while a negative size retains the host's absent-context-size convention. +const ( + defaultFocusPosition = 1 + defaultFocusSize = 1 + configuredPositionFloor = 0 + defaultFocusSizeSelector = 0 +) + +// Only positive configured node limits override the package default. This zero +// threshold selects a default, unlike the disabled-bound convention in append +// helpers. Static reference analysis retains at least one logical work operation. +const ( + configuredNodeLimitFloor = 0 + minimumStaticReferenceWork = 1 +) + // DefaultEvaluatorOptions is the zero value — all setters clone by default. const DefaultEvaluatorOptions EvaluatorOption = 0 @@ -498,6 +517,36 @@ func (e Evaluator) TraceWriter(w io.Writer) Evaluator { // Evaluate evaluates the compiled expression against the given context node. // ctx is used for cancellation/deadlines only, not for configuration. func (e Evaluator) Evaluate(ctx context.Context, expr *Expression, node xdm.Node) (*Result, error) { + if expr == nil { + return nil, fmt.Errorf("xpath: expression has no compiled program") + } + + // A bare context item needs no variable scopes, document caches or runtime + // state. XSLT evaluates it once per selected item. Keep annotated nodes on + // the normal path, where nodeItemFor supplies their complete type metadata. + if expr.deferred == nil && expr.program != nil && expr.program.stream.isContextItem && (e.cfg == nil || e.cfg.typeAnnotations == nil) { + var item xdm.Item + if e.cfg != nil { + item = e.cfg.contextItem + } + + if item == nil { + if xdm.IsNilNode(node) { + return nil, &xdm.Error{Code: lexicon.ErrXPDY0002, Message: errMsgContextItemAbsent} + } + + item = xdm.NodeItem{Node: node} + } + + if value, ok := item.(xdm.AtomicValue); ok { + if err := ChargeNumber(ctx, value); err != nil { + return nil, err + } + } + + return &Result{seq: xdm.ItemSlice{item}}, nil + } + ec, err := e.prepareEvaluation(ctx, expr, node) if err != nil { return nil, err @@ -549,7 +598,7 @@ func (e Evaluator) ValidateExpression(ctx context.Context, expr *Expression) err collector := staticRefCollector{bound: map[string]int{}, namespaces: ec.namespaces} collector.walk(expr.astExpr()) - if err := ec.countOps(ctx, max(1, collector.visits)); err != nil { + if err := ec.countOps(ctx, max(minimumStaticReferenceWork, collector.visits)); err != nil { return err } @@ -619,13 +668,13 @@ func (e Evaluator) prepareEvaluation(ctx context.Context, expr *Expression, node ec.namespaces = expr.staticNamespaces } - if expr.version == 10 { + if expr.version == xpathVersion10 { ec.xpath10Compat = true } applyBudget(ctx, ec) - if err := expr.prefixPlan.Validate(ec.namespaces, expr.staticNamespaces != nil || ec.strictPrefixes || ec.version == 10, ec.schemaDeclarations, ec.version); err != nil { + if err := expr.prefixPlan.Validate(ec.namespaces, expr.staticNamespaces != nil || ec.strictPrefixes || ec.version == xpathVersion10, ec.schemaDeclarations, ec.version); err != nil { return nil, err } @@ -655,7 +704,7 @@ func (e Evaluator) newEvalCtx(node xdm.Node) *evalContext { } maxNodes := maxNodeSetLength - if cfg.maxNodes > 0 { + if cfg.maxNodes > configuredNodeLimitFloor { maxNodes = cfg.maxNodes } @@ -663,8 +712,8 @@ func (e Evaluator) newEvalCtx(node xdm.Node) *evalContext { node: node, runtime: cfg.runtime, invocation: &invocationState{}, - position: 1, - size: 1, + position: defaultFocusPosition, + size: defaultFocusSize, opCount: &opCount, maxNodes: maxNodes, docCache: make(map[string]xdm.Node), @@ -735,11 +784,11 @@ func (e Evaluator) newEvalCtx(node xdm.Node) *evalContext { ec.xpath10Compat = cfg.xpath10Compat // dynamic focus overrides - if cfg.position > 0 { + if cfg.position > configuredPositionFloor { ec.position = cfg.position } - if cfg.size != 0 { + if cfg.size != defaultFocusSizeSelector { ec.size = cfg.size } diff --git a/transform/internal/xpath/evaluator_test.go b/transform/internal/xpath/evaluator_test.go index 8a712b0..e5068bd 100644 --- a/transform/internal/xpath/evaluator_test.go +++ b/transform/internal/xpath/evaluator_test.go @@ -4,6 +4,7 @@ import ( "context" "errors" "math/big" + "reflect" "slices" "testing" "time" @@ -11,26 +12,6 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) -func compileEvaluatorExpression(t *testing.T, source string) *Expression { - t.Helper() - expression, err := NewCompiler().Language(31, nil).Compile(source) - if err != nil { - t.Fatal(err) - } - return expression -} - -func assertEvaluatorString(t *testing.T, evaluator Evaluator, expression *Expression, want string) { - t.Helper() - result, err := evaluator.Evaluate(t.Context(), expression, nil) - if err != nil { - t.Fatal(err) - } - if got, ok := result.IsString(); !ok || got != want { - t.Fatalf("result = %v; want string %q", result.Sequence(), want) - } -} - func TestEvaluatorVariableSnapshot(t *testing.T) { t.Parallel() number := big.NewInt(123) @@ -131,3 +112,240 @@ func TestResultCopyPreservesNodesAfterEvalStateReuse(t *testing.T) { t.Fatal("Copy changed after state reuse", nodes, nodesErr) } } + +func compileEvaluatorExpression(t *testing.T, source string) *Expression { + t.Helper() + expression, err := NewCompiler().Language(31, nil).Compile(source) + if err != nil { + t.Fatal(err) + } + return expression +} + +func assertEvaluatorString(t *testing.T, evaluator Evaluator, expression *Expression, want string) { + t.Helper() + result, err := evaluator.Evaluate(t.Context(), expression, nil) + if err != nil { + t.Fatal(err) + } + if got, ok := result.IsString(); !ok || got != want { + t.Fatalf("result = %v; want string %q", result.Sequence(), want) + } +} + +type axisMetadata struct { + axis AxisType + explicit bool +} + +func TestAxisSpecifierMetadata(t *testing.T) { + for _, tc := range []struct { + source string + want []axisMetadata + }{ + {`document-node()`, []axisMetadata{{AxisChild, false}}}, + {`child::document-node()`, []axisMetadata{{AxisChild, true}}}, + {`(: leading :) (document-node())`, []axisMetadata{{AxisChild, false}}}, + {`(: leading :) (child (: axis :) :: document-node())`, []axisMetadata{{AxisChild, true}}}, + {`child::n`, []axisMetadata{{AxisChild, true}}}, + {`n`, []axisMetadata{{AxisChild, false}}}, + {`@n`, []axisMetadata{{AxisAttribute, true}}}, + {`attribute(n)`, []axisMetadata{{AxisAttribute, false}}}, + {`namespace-node()`, []axisMetadata{{AxisNamespace, false}}}, + {`self::node()`, []axisMetadata{{AxisSelf, true}}}, + {`.`, nil}, + {`..`, []axisMetadata{{AxisParent, false}}}, + {`//child::n`, []axisMetadata{{AxisDescendantOrSelf, false}, {AxisChild, true}}}, + } { + t.Run(tc.source, func(t *testing.T) { + ast, err := Parse(tc.source) + if err != nil { + t.Fatal(err) + } + assertAxisMetadata(t, axisMetadataFromExpr(ast), tc.want) + compiled, err := NewCompiler().Compile(tc.source) + if err != nil { + t.Fatal(err) + } + assertAxisMetadata(t, axisMetadataFromExpr(compiled.AST()), tc.want) + assertAxisMetadata(t, axisMetadataFromProgram(compiled.program), tc.want) + compiledAST, err := NewCompiler().CompileExpr(ast) + if err != nil { + t.Fatal(err) + } + assertAxisMetadata(t, axisMetadataFromExpr(compiledAST.AST()), tc.want) + assertAxisMetadata(t, axisMetadataFromProgram(compiledAST.program), tc.want) + }) + } +} + +func assertAxisMetadata(t *testing.T, got, want []axisMetadata) { + t.Helper() + if !reflect.DeepEqual(got, want) { + t.Fatalf("metadata=%v, want %v", got, want) + } +} + +func axisMetadataFromExpr(expr Expr) []axisMetadata { + var result []axisMetadata + WalkExpr(expr, func(node Expr) bool { + if path, ok := AsExpr[LocationPath](node); ok { + for _, step := range path.Steps { + result = append(result, axisMetadata{step.Axis, step.ExplicitAxis}) + } + } + return true + }) + return result +} + +func axisMetadataFromProgram(program *vmProgram) []axisMetadata { + var result []axisMetadata + for _, instruction := range program.instructions { + path, ok := instruction.payload.(vmLocationPathExpr) + if !ok { + continue + } + for _, step := range path.Steps { + result = append(result, axisMetadata{step.Axis, step.ExplicitAxis}) + } + } + return result +} + +func TestXPath10RejectsURIQualifiedNameTest(t *testing.T) { + for _, tc := range []struct { + source string + wantError bool + }{{"Q{}n", true}, {"Q{}*", true}, {"Q{urn:other}n", true}, {"n", false}, {"*", false}} { + t.Run(tc.source, func(t *testing.T) { + ast, err := Parse(tc.source) + if err != nil { + t.Fatal(err) + } + q, err := NewCompiler().Language(10, nil).CompileExpr(ast) + var detail *xdm.Error + if tc.wantError { + if !errors.As(err, &detail) || detail.Code != "XPST0003" || q != nil { + t.Errorf("queryNil=%t/error=%v; expected nil query/XPST0003", q == nil, err) + } + } else if err != nil || q == nil { + t.Fatalf("bare control query=%v error=%v", q, err) + } + }) + } +} + +func TestNameTestNamespacePresence(t *testing.T) { + for _, tc := range []struct { + source string + explicit bool + }{ + {"Q{}n", true}, + {"Q{}*", true}, + {"Q{urn:other}n", true}, + {"Q{urn:other}*", true}, + {"n", false}, + {"*", false}, + {"p:n", false}, + {"p:*", false}, + {"*:n", false}, + } { + t.Run(tc.source, func(t *testing.T) { + ast, err := Parse(tc.source) + if err != nil { + t.Fatal(err) + } + checkNameTestPresence(t, nameTestFromSyntax(t, ast), tc.explicit) + direct, err := NewCompiler().Compile(tc.source) + if err != nil { + t.Fatal(err) + } + checkNameTestPresence(t, nameTestFromProgram(t, direct.program), tc.explicit) + fromAST, err := NewCompiler().CompileExpr(ast) + if err != nil { + t.Fatal(err) + } + checkNameTestPresence(t, nameTestFromProgram(t, fromAST.program), tc.explicit) + }) + } +} + +func nameTestFromSyntax(t *testing.T, ast Expr) xdm.NameTest { + t.Helper() + var tests []xdm.NameTest + WalkExpr(ast, func(node Expr) bool { + if path, ok := node.(LocationPath); ok { + for _, step := range path.Steps { + if nt, ok := step.NodeTest.(xdm.NameTest); ok { + tests = append(tests, nt) + } + } + } + return true + }) + if len(tests) != 1 { + t.Fatalf("name tests=%#v", tests) + } + return tests[0] +} + +func nameTestFromProgram(t *testing.T, program *vmProgram) xdm.NameTest { + t.Helper() + var tests []xdm.NameTest + for _, instruction := range program.instructions { + if path, ok := instruction.payload.(vmLocationPathExpr); ok { + for _, step := range path.Steps { + if nt, ok := step.NodeTest.(xdm.NameTest); ok { + tests = append(tests, nt) + } + } + } + } + if len(tests) != 1 { + t.Fatalf("VM name tests=%#v", tests) + } + return tests[0] +} + +func checkNameTestPresence(t *testing.T, test xdm.NameTest, want bool) { + t.Helper() + if test.URIQualified != want { + t.Fatalf("name test=%#v; expected URIQualified=%t", test, want) + } +} + +func TestManualNameTestNamespaceCompatibility(t *testing.T) { + doc, err := (xdm.ParseOptions{}).ParseText(t.Context(), `plainnamespaced`) + if err != nil { + t.Fatal(err) + } + for _, tc := range []struct { + name string + test xdm.NameTest + want string + }{ + {"legacy nonempty URI", xdm.NameTest{URI: "urn:other", Local: "n"}, "namespaced"}, + {"legacy bare name", xdm.NameTest{Local: "n"}, "namespaced"}, + {"explicit empty URI", xdm.NameTest{URIQualified: true, Local: "n"}, "plain"}, + } { + t.Run(tc.name, func(t *testing.T) { + ast := LocationPath{Steps: []Step{{Axis: AxisChild, NodeTest: tc.test}}} + expr, err := NewCompiler().CompileExpr(ast) + if err != nil { + t.Fatal(err) + } + result, err := NewEvaluator(DefaultEvaluatorOptions).Namespaces(map[string]string{"": "urn:other"}).Evaluate(t.Context(), expr, doc.DocumentElement()) + if err != nil { + t.Fatal(err) + } + nodes, err := result.Nodes() + if err != nil || len(nodes) != 1 { + t.Fatalf("nodes=%v error=%v", nodes, err) + } + if got := xdm.StringValue(nodes[0]); got != tc.want { + t.Errorf("actual %q; expected %q", got, tc.want) + } + }) + } +} diff --git a/transform/internal/xpath/expr.go b/transform/internal/xpath/expr.go index d50b4cd..334ba0f 100644 --- a/transform/internal/xpath/expr.go +++ b/transform/internal/xpath/expr.go @@ -82,6 +82,9 @@ func (LocationPath) exprNode() {} type Step struct { // Axis selects the navigation axis for this step. Axis AxisType + // ExplicitAxis records a written axis:: or @ specifier. It is false for + // default axes, synthetic // steps, and abbreviated . or .. steps. + ExplicitAxis bool // NodeTest filters nodes reached through Axis. NodeTest xdm.NodeTest // Predicates applies filters in source order. diff --git a/transform/internal/xpath/format_datetime.go b/transform/internal/xpath/format_datetime.go index 0b13465..0badccd 100644 --- a/transform/internal/xpath/format_datetime.go +++ b/transform/internal/xpath/format_datetime.go @@ -19,6 +19,89 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) +// Calendar and integer presentation selectors share the F&O picture syntax. +// Language codes select supported vocabularies; they are distinct from the +// caller-configurable defaultFormattingLanguage. Case labels are internal modes. +const ( + formatLanguageEnglish = "en" + formatLanguageGerman = "de" + formatLanguageFrench = "fr" + formatLanguageItalian = "it" + formatCaseTitle = "title" + formatUppercase = true // alphabetic/Roman uppercase mode + formatLowercase = false // alphabetic/Roman lowercase mode + formatNameUpper = "N" + formatNameLower = "n" + formatNameTitle = "Nn" + formatAlphaUpper = "A" + formatAlphaLower = "a" + formatRomanUpper = "I" + formatRomanLower = "i" + formatWordsUpper = "W" + formatWordsLower = "w" + formatWordsTitle = "Ww" + formatDecimalDefault = "1" + formatZeroDigit = "0" + formatDecimalPrintf = "%d" +) + +// Width sentinels have independent meanings: no explicit minimum versus no +// maximum. Other widths/precisions count decimal digits, not bytes or operations. +const ( + dateMinimumWidthAbsent = -1 + dateMaximumWidthUnlimited = -1 + dateWidthFieldCount = 2 + dateWidthMinimumOnlyFields = 1 + dateDefaultMinimumDigits = 1 + dateImplicitClockDigits = 2 + dateYearModulusMinimumDigits = 2 + dateFractionMinimumDigits = 1 + dateFractionMaximumDigits = 9 + dateRoundingDivisor = 2 // half-even quotient/remainder and parity + dateNanosecondDecimalFormat = "%09d" +) + +// These civil-calendar quantities and labels describe F&O date presentation, +// including its identified fallback. Era output and the calendar AD identifier +// deliberately have separate names despite identical spelling. +const ( + twelveHourClockHours = 12 + dateDaysPerWeek = 7 + dateMonthDayOrigin = 1 + dateWeekOrigin = 1 + dateEraAnnoDomini = "AD" + dateEraBeforeChrist = "BC" + dateMeridiemAM = "am" + dateMeridiemPM = "pm" + dateCalendarAD = "AD" + dateLanguageFallbackPrefix = "[Language: " + dateCalendarFallbackPrefix = "[Calendar: AD]" + dateComponentLetters = "YMDdFWwHhPmsfZzCE" +) + +// Timezone picture widths count digits per hour/minute field. Military letters +// preserve their ordered hour mapping, omitting J (unknown local timezone). +const ( + timezoneDefaultHourDigits = 2 + timezoneDefaultMinuteDigits = 2 + timezoneDefaultSeparator = ":" + timezoneGMTPrefix = "GMT" + formatMilitaryTimezoneToken = "Z" + timezoneZeroDesignator = "Z" + timezoneUnknownMilitaryCode = "J" + militaryTimezoneMaximumHours = 12 + militaryTimezoneEastLetters = "ABCDEFGHIKLM" + militaryTimezoneWestLetters = "NOPQRSTUVWXY" + timezoneFieldFormat = "%%0%dd" +) + +// Unicode Nd alphabets contain ten digits, with the last nine offsets after +// their zero. Calendar, integer and decimal-picture formatters share this fact. +const ( + formatterDecimalDigitCount = 10 + formatterLastDecimalDigitOffset = formatterDecimalDigitCount - 1 +) + // maxInt64Digits is the number of decimal digits in math.MaxInt64. const maxInt64Digits = 19 @@ -63,7 +146,7 @@ func formatDateTimeCommon(ctx context.Context, args []xdm.Sequence, typeName str return nil, err } - lang := "en" + lang := defaultFormattingLanguage if ec := getFnContext(ctx); ec != nil { lang = ec.getDefaultLanguage() } @@ -98,7 +181,7 @@ func formatDateTimeCommon(ctx context.Context, args []xdm.Sequence, typeName str legacy := false if ec := getFnContext(ctx); ec != nil { - legacy = ec.version == 20 || ec.version == 30 + legacy = ec.version == xpathVersion20 || ec.version == xpathVersion30 } result, err := formatDateTimePicture(ctx, t, picture, lang, calendar, typeName, defaultLanguage, legacy) @@ -217,15 +300,15 @@ func dateFormattingDefaults(lang, calendar, defaultLanguage string) (string, str if !isSupportedLanguage(effectiveLang) { effectiveLang = strings.ToLower(defaultLanguage) if !isSupportedLanguage(effectiveLang) { - effectiveLang = "en" + effectiveLang = formatLanguageEnglish } - prefix = "[Language: " + effectiveLang + "]" + prefix = dateLanguageFallbackPrefix + effectiveLang + "]" } effectiveCalendar := calendar if effectiveCalendar != "" && !isISOCalendar(effectiveCalendar) { - prefix += "[Calendar: AD]" + prefix += dateCalendarFallbackPrefix effectiveCalendar = nameISO } @@ -234,7 +317,7 @@ func dateFormattingDefaults(lang, calendar, defaultLanguage string) (string, str func isSupportedLanguage(lang string) bool { switch lang { - case "en", "de": + case formatLanguageEnglish, formatLanguageGerman: return true default: return false @@ -285,9 +368,9 @@ func formatComponent(ctx context.Context, t time.Time, spec, lang, calendar, typ case 'H': value = int64(t.Hour()) case 'h': - h := t.Hour() % 12 + h := t.Hour() % twelveHourClockHours if h == 0 { - h = 12 + h = twelveHourClockHours } value = int64(h) @@ -308,7 +391,7 @@ func formatComponent(ctx context.Context, t time.Time, spec, lang, calendar, typ if isISOCalendar(calendar) { value = int64(isoWeekOfMonth(t)) } else { - value = int64((t.Day()-1)/7 + 1) + value = int64((t.Day()-dateMonthDayOrigin)/dateDaysPerWeek + dateWeekOrigin) } case 'E': return formatEra(t.Year(), presentation), nil @@ -371,9 +454,9 @@ type dtWidth struct { } func parseDatePresentation(rest string) (dtPresentation, dtWidth) { - presentation := dtPresentation{format: "1", implicit: true} + presentation := dtPresentation{format: formatDecimalDefault, implicit: true} - width := dtWidth{minWidth: -1, maxWidth: -1} + width := dtWidth{minWidth: dateMinimumWidthAbsent, maxWidth: dateMaximumWidthUnlimited} if rest == "" { return presentation, width } @@ -419,7 +502,7 @@ func splitDatePresentationModifiers(format string) (string, string) { } func parseDateWidth(value string) dtWidth { - width := dtWidth{minWidth: -1, maxWidth: -1} + width := dtWidth{minWidth: dateMinimumWidthAbsent, maxWidth: dateMaximumWidthUnlimited} if value == "" { return width } @@ -427,7 +510,7 @@ func parseDateWidth(value string) dtWidth { width.present = true parts := strings.Split(value, "-") - if len(parts) > 2 { + if len(parts) > dateWidthFieldCount { width.invalid = true return width } @@ -454,8 +537,8 @@ func parseDateWidth(value string) dtWidth { width.minWidth = parseSimpleInt(parts[0]) } - width.maxOmitted = len(parts) == 1 - if len(parts) == 2 && parts[1] != "*" { + width.maxOmitted = len(parts) == dateWidthMinimumOnlyFields + if len(parts) == dateWidthFieldCount && parts[1] != "*" { width.maxWidth = parseSimpleInt(parts[1]) } @@ -490,11 +573,11 @@ func parseSimpleInt(s string) int { } d := int(r - '0') - if n > (math.MaxInt-d)/10 { + if n > (math.MaxInt-d)/numericDecimalRadix { return math.MaxInt } - n = n*10 + d + n = n*numericDecimalRadix + d } return n @@ -505,28 +588,28 @@ func formatDateTimeValue(value int64, comp byte, p dtPresentation, w dtWidth, la numericValue := normalizeDateNumericValue(value, comp, p, w) switch format { - case "N", "n", "Nn": + case formatNameUpper, formatNameLower, formatNameTitle: // Named format (for months and days) return formatNamedValue(value, comp, format, w, lang) - case "A": - return formatAlpha(new(big.Int).SetInt64(numericValue), true) - case "a": - return formatAlpha(new(big.Int).SetInt64(numericValue), false) - case "I", "i": + case formatAlphaUpper: + return formatAlpha(new(big.Int).SetInt64(numericValue), formatUppercase) + case formatAlphaLower: + return formatAlpha(new(big.Int).SetInt64(numericValue), formatLowercase) + case formatRomanUpper, formatRomanLower: // Roman numerals n := new(big.Int).SetInt64(numericValue) - return applyTextWidth(formatRoman(n, format == "I"), w) - case "W", "w", "Ww": + return applyTextWidth(formatRoman(n, format == formatRomanUpper), w) + case formatWordsUpper, formatWordsLower, formatWordsTitle: n := new(big.Int).SetInt64(numericValue) style := nameLower switch format { - case "W": - style = nameUpper2 - case "w": + case formatWordsUpper: + style = nameUpperCase + case formatWordsLower: style = nameLower - case "Ww": - style = "title" + case formatWordsTitle: + style = formatCaseTitle } result := formatWords(n, lang, style) @@ -563,7 +646,7 @@ func formatDateDecimal(value int64, p dtPresentation, w dtWidth, comp byte) stri } if minDigits == 0 { - minDigits = 1 + minDigits = dateDefaultMinimumDigits } // Apply width constraints. F&O 3.1 §9.8.4.3 ignores the maximum width of @@ -571,7 +654,7 @@ func formatDateDecimal(value int64, p dtPresentation, w dtWidth, comp byte) stri // only its low-order digits through the modulus that // normalizeDateNumericValue already applied. if p.implicit && (comp == 'm' || comp == 's') && w.minWidth < 0 { - minDigits = 2 + minDigits = dateImplicitClockDigits } if w.minWidth > 0 { @@ -586,11 +669,11 @@ func formatDateDecimal(value int64, p dtPresentation, w dtWidth, comp byte) stri neg = true } - s := fmt.Sprintf("%d", abs) + s := fmt.Sprintf(formatDecimalPrintf, abs) // Pad to minimum digits if len(s) < minDigits { - s = strings.Repeat("0", minDigits-len(s)) + s + s = strings.Repeat(formatZeroDigit, minDigits-len(s)) + s } // Apply grouping separators from format pattern @@ -633,7 +716,7 @@ func normalizeDateNumericValue(value int64, comp byte, p dtPresentation, w dtWid } } - if digits < 2 { + if digits < dateYearModulusMinimumDigits { return value } } @@ -643,7 +726,7 @@ func normalizeDateNumericValue(value int64, comp byte, p dtPresentation, w dtWid if digits < maxInt64Digits { mod := int64(1) for range digits { - mod *= 10 + mod *= numericDecimalRadix } value %= mod @@ -719,31 +802,31 @@ func formatNamedValue(value int64, comp byte, format string, w dtWidth, lang str switch comp { case 'M': - if value >= 1 && value <= 12 { + if value >= int64(time.January) && value <= int64(time.December) { months := monthNamesForLang(lang) - name = months[value-1] + name = months[value-int64(time.January)] } case 'F': - if value >= 0 && value <= 6 { + if value >= int64(time.Sunday) && value <= int64(time.Saturday) { days := dayNamesForLang(lang) name = days[value] } default: - return fmt.Sprintf("%d", value) + return fmt.Sprintf(formatDecimalPrintf, value) } if name == "" { - return fmt.Sprintf("%d", value) + return fmt.Sprintf(formatDecimalPrintf, value) } name = applyNameWidth(name, comp, w) switch format { - case "N": + case formatNameUpper: return strings.ToUpper(name) - case "n": + case formatNameLower: return strings.ToLower(name) - case "Nn": + case formatNameTitle: r, size := utf8.DecodeRuneInString(name) return string(unicode.ToUpper(r)) + strings.ToLower(name[size:]) } @@ -753,7 +836,7 @@ func formatNamedValue(value int64, comp byte, format string, w dtWidth, lang str func monthNamesForLang(lang string) []string { switch strings.ToLower(lang) { - case "de": + case formatLanguageGerman: return []string{"Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"} default: return []string{"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"} @@ -762,7 +845,7 @@ func monthNamesForLang(lang string) []string { func dayNamesForLang(lang string) []string { switch strings.ToLower(lang) { - case "de": + case formatLanguageGerman: return []string{"Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"} default: return []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"} @@ -797,13 +880,13 @@ func formatDayOfWeek(t time.Time, p dtPresentation, w dtWidth, lang string) stri format := p.format switch format { - case "N", "n", "Nn": + case formatNameUpper, formatNameLower, formatNameTitle: return formatNamedValue(dow, 'F', format, w, lang) default: // Numeric day of week: 1=Monday, ..., 7=Sunday per ISO isoDow := int64(t.Weekday()) if isoDow == 0 { - isoDow = 7 + isoDow = dateDaysPerWeek } return formatDateTimeValue(isoDow, 'F', p, w, lang) @@ -812,18 +895,18 @@ func formatDayOfWeek(t time.Time, p dtPresentation, w dtWidth, lang string) stri func formatAMPM(t time.Time, p dtPresentation, w dtWidth) string { var s string - if t.Hour() < 12 { - s = "am" + if t.Hour() < twelveHourClockHours { + s = dateMeridiemAM } else { - s = "pm" + s = dateMeridiemPM } switch p.format { - case "N": + case formatNameUpper: s = strings.ToUpper(s) - case "n": + case formatNameLower: s = strings.ToLower(s) - case "Nn": + case formatNameTitle: s = strings.ToUpper(s[:1]) + s[1:] } // Apply maximum width constraint: truncate to maxWidth characters @@ -853,22 +936,22 @@ func formatFractionalSeconds(t time.Time, p dtPresentation, w dtWidth, legacy bo nanos := t.Nanosecond() - if legacy && maximum < 9 { + if legacy && maximum < dateFractionMaximumDigits { // XSLT 2.0 §16.5.1 and F&O 3.0 §9.8.4.1 round this component; // it does not carry into other displayed date/time components. - scale := int(math.Pow10(9 - maximum)) + scale := int(math.Pow10(dateFractionMaximumDigits - maximum)) quotient, remainder := nanos/scale, nanos%scale - if remainder > scale/2 || remainder == scale/2 && quotient%2 != 0 { + if remainder > scale/dateRoundingDivisor || remainder == scale/dateRoundingDivisor && quotient%dateRoundingDivisor != 0 { quotient++ } - nanos = quotient * scale % 1_000_000_000 + nanos = quotient * scale % int(nanosecondsPerSecond) } - value := fmt.Sprintf("%09d", nanos)[:maximum] + value := fmt.Sprintf(dateNanosecondDecimalFormat, nanos)[:maximum] if len(value) < minimum { - value += strings.Repeat("0", minimum-len(value)) + value += strings.Repeat(formatZeroDigit, minimum-len(value)) } for len(value) > minimum && value[len(value)-1] == '0' { @@ -907,8 +990,8 @@ func parseFractionalPicture(format string) fractionalPicture { } if mandatoryDigits == 0 && optionalDigits == 0 { - mandatoryDigits = 1 - totalDigits = 1 + mandatoryDigits = dateFractionMinimumDigits + totalDigits = dateFractionMinimumDigits } return fractionalPicture{ @@ -926,9 +1009,9 @@ func fractionalPrecision(picture fractionalPicture, w dtWidth, legacy bool) (int minPlaces := picture.mandatoryDigits var maxPlaces int - if picture.mandatoryDigits <= 1 && picture.optionalDigits == 0 { + if picture.mandatoryDigits <= dateFractionMinimumDigits && picture.optionalDigits == 0 { // Single-digit or default: unbounded max (show all significant) - maxPlaces = 9 + maxPlaces = dateFractionMaximumDigits } else { maxPlaces = picture.totalDigits } @@ -951,13 +1034,13 @@ func fractionalPrecision(picture fractionalPicture, w dtWidth, legacy bool) (int if w.present && !w.maxOmitted && w.maxWidth < 0 { // An explicit * maximum permits every significant fractional digit. - maxPlaces = 9 + maxPlaces = dateFractionMaximumDigits } if legacy && w.present { // In XSLT 2.0 and F&O 3.0 an explicit width replaces the picture's widths; // an omitted maximum means unbounded, including [f,3]. - minPlaces, maxPlaces = 1, 9 + minPlaces, maxPlaces = dateFractionMinimumDigits, dateFractionMaximumDigits if w.minWidth > 0 { minPlaces = w.minWidth } @@ -971,12 +1054,12 @@ func fractionalPrecision(picture fractionalPicture, w dtWidth, legacy bool) (int maxPlaces = minPlaces } - if minPlaces < 1 { - minPlaces = 1 + if minPlaces < dateFractionMinimumDigits { + minPlaces = dateFractionMinimumDigits } - if maxPlaces > 9 { - maxPlaces = 9 + if maxPlaces > dateFractionMaximumDigits { + maxPlaces = dateFractionMaximumDigits } return minPlaces, maxPlaces @@ -1005,8 +1088,8 @@ func applyFractionalGrouping(s string, picture fractionalPicture) string { func formatTimezone(t time.Time, comp byte, p dtPresentation, w dtWidth) string { if !xdm.HasTimezone(t) { - if p.format == "Z" && !p.implicit { - return "J" + if p.format == formatMilitaryTimezoneToken && !p.implicit { + return timezoneUnknownMilitaryCode } return "" @@ -1014,13 +1097,13 @@ func formatTimezone(t time.Time, comp byte, p dtPresentation, w dtWidth) string _, offset := t.Zone() - if p.format == "N" || p.format == "n" || p.format == "Nn" { + if p.format == formatNameUpper || p.format == formatNameLower || p.format == formatNameTitle { if name := formatTimezoneName(t, p.format); name != "" { return name } } - if p.format == "Z" && !p.implicit { + if p.format == formatMilitaryTimezoneToken && !p.implicit { if code, ok := militaryTimezoneCode(offset); ok { return code } @@ -1047,19 +1130,29 @@ func isoWeekOfMonth(t time.Time) int { return isoWeekOfMonth(prevMonth) } - return int(weekStart.Sub(monthWeek1)/(7*24*time.Hour)) + 1 + // The week belongs to its Thursday's month. Construct that civil date in + // UTC so local clock transitions cannot change its day or month ordinal. + const ( + calendarWeekDays = dateDaysPerWeek // calendar days per week + monthDayOrigin = 1 // first day number in a month + monthWeekOrigin = 1 // first week number within Thursday's month + ) + + thursday := time.Date(weekStart.Year(), weekStart.Month(), weekStart.Day()+int(time.Thursday-time.Monday), 0, 0, 0, 0, time.UTC) + + return (thursday.Day()-monthDayOrigin)/calendarWeekDays + monthWeekOrigin } func formatEra(year int, p dtPresentation) string { - era := "AD" + era := dateEraAnnoDomini if year < 0 { - era = "BC" + era = dateEraBeforeChrist } switch p.format { - case "n": + case formatNameLower: return strings.ToLower(era) - case "Nn": + case formatNameTitle: return strings.ToUpper(era[:1]) + strings.ToLower(era[1:]) default: return era @@ -1079,14 +1172,14 @@ type timezonePicture struct { func parseTimezonePicture(comp byte, p dtPresentation) timezonePicture { spec := timezonePicture{ - hourWidth: 2, - minuteWidth: 2, - separator: ":", + hourWidth: timezoneDefaultHourDigits, + minuteWidth: timezoneDefaultMinuteDigits, + separator: timezoneDefaultSeparator, showMinutes: true, zeroDigit: '0', } if comp == 'z' { - spec.prefix = "GMT" + spec.prefix = timezoneGMTPrefix } if p.implicit { @@ -1101,12 +1194,12 @@ func parseTimezonePicture(comp byte, p dtPresentation) timezonePicture { spec.zeroAsZ = true } - if format == "Z" { + if format == formatMilitaryTimezoneToken { spec.militaryCode = true return spec } - if format == "N" || format == "n" || format == "Nn" { + if format == formatNameUpper || format == formatNameLower || format == formatNameTitle { return spec } @@ -1124,9 +1217,9 @@ func parseTimezonePicture(comp byte, p dtPresentation) timezonePicture { prefixDigits := formatRunes[:firstNonDigit] if len(prefixDigits) > 0 { - if firstNonDigit == len(formatRunes) && len(prefixDigits) > 2 { - spec.hourWidth = len(prefixDigits) - 2 - spec.minuteWidth = 2 + if firstNonDigit == len(formatRunes) && len(prefixDigits) > timezoneDefaultMinuteDigits { + spec.hourWidth = len(prefixDigits) - timezoneDefaultMinuteDigits + spec.minuteWidth = timezoneDefaultMinuteDigits spec.separator = "" spec.showMinutes = true @@ -1155,7 +1248,7 @@ func parseTimezonePicture(comp byte, p dtPresentation) timezonePicture { } if spec.minuteWidth == 0 { - spec.minuteWidth = 2 + spec.minuteWidth = timezoneDefaultMinuteDigits } return spec @@ -1169,7 +1262,7 @@ func renderTimezoneOffset(offset int, spec timezonePicture) string { } if spec.zeroAsZ && offset == 0 { - return "Z" + return timezoneZeroDesignator } sign := "+" @@ -1178,18 +1271,18 @@ func renderTimezoneOffset(offset int, spec timezonePicture) string { offset = -offset } - hours := offset / 3600 - minutes := (offset % 3600) / 60 + hours := offset / calendarSecondsPerHour + minutes := (offset % calendarSecondsPerHour) / calendarSecondsPerMinute - hourFmt := fmt.Sprintf("%%0%dd", spec.hourWidth) + hourFmt := fmt.Sprintf(timezoneFieldFormat, spec.hourWidth) body := sign + fmt.Sprintf(hourFmt, hours) if spec.showMinutes { - minuteFmt := fmt.Sprintf("%%0%dd", spec.minuteWidth) + minuteFmt := fmt.Sprintf(timezoneFieldFormat, spec.minuteWidth) body += spec.separator + fmt.Sprintf(minuteFmt, minutes) } else if minutes != 0 { - minuteFmt := fmt.Sprintf("%%0%dd", spec.minuteWidth) - body += ":" + fmt.Sprintf(minuteFmt, minutes) + minuteFmt := fmt.Sprintf(timezoneFieldFormat, spec.minuteWidth) + body += timezoneDefaultSeparator + fmt.Sprintf(minuteFmt, minutes) } if spec.zeroDigit != '0' { @@ -1206,9 +1299,9 @@ func formatTimezoneName(t time.Time, format string) string { } switch format { - case "N": + case formatNameUpper: return strings.ToUpper(name) - case "n": + case formatNameLower: return strings.ToLower(name) default: return name @@ -1216,19 +1309,19 @@ func formatTimezoneName(t time.Time, format string) string { } func militaryTimezoneCode(offset int) (string, bool) { - if offset%3600 != 0 { + if offset%calendarSecondsPerHour != 0 { return "", false } - hours := offset / 3600 + hours := offset / calendarSecondsPerHour switch { case hours == 0: - return "Z", true - case hours >= 1 && hours <= 12: - letters := "ABCDEFGHIKLM" + return timezoneZeroDesignator, true + case hours >= 1 && hours <= militaryTimezoneMaximumHours: + letters := militaryTimezoneEastLetters return string(letters[hours-1]), true - case hours <= -1 && hours >= -12: - letters := "NOPQRSTUVWXY" + case hours <= -1 && hours >= -militaryTimezoneMaximumHours: + letters := militaryTimezoneWestLetters return string(letters[-hours-1]), true default: return "", false @@ -1237,7 +1330,7 @@ func militaryTimezoneCode(offset int) (string, bool) { func isISOCalendar(calendar string) bool { switch calendar { - case nameISO, "Q{}ISO", "AD", "Q{}AD": + case nameISO, "Q{}" + nameISO, dateCalendarAD, "Q{}" + dateCalendarAD: return true default: return false @@ -1250,7 +1343,7 @@ func resolveCalendarName(ctx context.Context, calendar string) (string, error) { } if strings.HasPrefix(calendar, "Q{") { - if ec := getFnContext(ctx); ec != nil && ec.version == 20 { + if ec := getFnContext(ctx); ec != nil && ec.version == xpathVersion20 { return "", invalidCalendarName(calendar) } @@ -1287,7 +1380,7 @@ func invalidCalendarName(calendar string) error { func isoWeekStart(t time.Time) time.Time { weekday := int(t.Weekday()) if weekday == 0 { - weekday = 7 + weekday = dateDaysPerWeek } return t.AddDate(0, 0, -(weekday - 1)) @@ -1295,14 +1388,14 @@ func isoWeekStart(t time.Time) time.Time { func isoMonthWeekOneStart(t time.Time) time.Time { loc := t.Location() - first := time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, loc) + first := time.Date(t.Year(), t.Month(), dateMonthDayOrigin, 0, 0, 0, 0, loc) firstWeekday := int(first.Weekday()) if firstWeekday == 0 { - firstWeekday = 7 + firstWeekday = dateDaysPerWeek } - offsetToThursday := (4 - firstWeekday + 7) % 7 + offsetToThursday := (int(time.Thursday) - firstWeekday + dateDaysPerWeek) % dateDaysPerWeek firstThursday := first.AddDate(0, 0, offsetToThursday) return isoWeekStart(firstThursday) @@ -1310,7 +1403,7 @@ func isoMonthWeekOneStart(t time.Time) time.Time { func isKnownCalendarName(calendar string) bool { switch calendar { - case nameISO, "AD", "AH", "AME", "AM", "AP", "AS", "BE", "CB", "CE", "CL", + case nameISO, dateCalendarAD, "AH", "AME", "AM", "AP", "AS", "BE", "CB", "CE", "CL", "CS", "EE", "FE", "JE", "KE", "KY", "ME", "MS", "NS", "OS", "RS", "SE", "SH", "SS", "TE", "VE", "VS": return true @@ -1322,7 +1415,7 @@ func isKnownCalendarName(calendar string) bool { // validateComponentForType checks that a component specifier is valid for the given type. // Per XPath F&O §9.8.4: raises FOFD1350 if the component is not available. func validateComponentForType(comp byte, typeName string) error { - if !strings.ContainsRune("YMDdFWwHhPmsfZzCE", rune(comp)) { + if !strings.ContainsRune(dateComponentLetters, rune(comp)) { return &xdm.Error{Code: errCodeFOFD1340, Message: fmt.Sprintf("unknown component specifier: %c", comp)} } diff --git a/transform/internal/xpath/format_integer.go b/transform/internal/xpath/format_integer.go index 9118af0..1fd7ad5 100644 --- a/transform/internal/xpath/format_integer.go +++ b/transform/internal/xpath/format_integer.go @@ -16,8 +16,107 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) +// Alphabetic and sequential-symbol numbering preserve these ordered Unicode +// alphabets/ranges. Symbol count is per block; the Roman ceiling and CJK myriad +// unit have different roles although their numeric values coincide. +const ( + greekAlphabetLower = "αβγδεζηθικλμνξοπρστυφχψω" + greekAlphabetUpper = "ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ" + greekLowerFirst = 0x03B1 + greekLowerLast = 0x03C9 + greekUpperFirst = 0x0391 + greekUpperLast = 0x03A9 + circledSymbolFirst = 0x2460 + circledSymbolLast = 0x2473 + parenthesizedSymbolFirst = 0x2474 + parenthesizedSymbolLast = 0x2487 + fullstopSymbolFirst = 0x2488 + fullstopSymbolLast = 0x249B + sequentialSymbolCount = 20 + latinAlphabetSize = 26 + cjkMyriadUnit = 10000 + romanMaximumValue = 10000 +) + +// Word-formatting control thresholds count numeric values, not string lengths. +// The vocabulary tables below remain language data. German zero is a word, +// separate from the identical JSON null element-name spelling. +const ( + wordGermanZero = "null" + wordSmallNumberLimit = 20 + wordHundred = 100 + wordThousand = 1000 + wordMillion = 1000000 + wordBillion = 1000000000 + wordTrillion = 1000000000000 +) + +// Ordinal selectors and suffixes are implementation-supported F&O language +// hints. English teen exceptions inspect the last two decimal digits; French +// composite tens and Italian vowel elision are independent language rules. +const ( + ordinalSpellout = "%spellout-ordinal" + ordinalSpelloutMasculine = "%spellout-ordinal-masculine" + ordinalSpelloutFeminine = "%spellout-ordinal-feminine" + ordinalMasculineSuffix = "-o" + ordinalFeminineSuffix = "-a" + ordinalLastTwoDigitsModulus = 100 + ordinalTeenFirst = 11 + ordinalTeenLast = 13 + ordinalFirstLastDigit = 1 + ordinalSecondLastDigit = 2 + ordinalThirdLastDigit = 3 + ordinalEnglishGeneralSuffix = "th" + ordinalEnglishFirstSuffix = "st" + ordinalEnglishSecondSuffix = "nd" + ordinalEnglishThirdSuffix = "rd" + frenchCompositeSeventy = 7 + frenchCompositeEighty = 8 + frenchCompositeNinety = 9 + frenchElevenUnit = 11 + italianElidedOne = 1 + italianElidedEight = 8 + frenchConjunctionOne = 1 // units digit receiving et outside composite80/90 + ordinalMasculine = false // masculine vocabulary in italianOrdinalWord + ordinalFeminine = true // feminine vocabulary in italianOrdinalWord + italianOrdinalDecimalFormat = "%d°" + integerRepeatingGroupPair = 2 // one separator between two groups repeats + integerMinimumDecimalDigits = 1 +) + const wordGermanSeven = "sieben" +// FormatGreekAlphabetic uses the 24-letter Greek alphabet, excluding the +// unassigned capital U+03A2 and the context-specific lowercase final sigma. +// It is shared with XSLT numbering so both execution paths use the same digits. +func FormatGreekAlphabetic(n *big.Int, upper bool) string { + if n.Sign() <= 0 { + return n.String() + } + + alphabet := []rune(greekAlphabetLower) + if upper { + alphabet = []rune(greekAlphabetUpper) + } + + value, one, size := new(big.Int).Set(n), big.NewInt(1), big.NewInt(int64(len(alphabet))) + + var ( + digits []rune + remainder big.Int + ) + + for value.Sign() > 0 { + value.Sub(value, one) + value.DivMod(value, size, &remainder) + digits = append(digits, alphabet[remainder.Int64()]) + } + + slices.Reverse(digits) + + return string(digits) +} + func fnFormatInteger(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { // format-integer($value, $picture [, $lang]) valSeq := args[0] @@ -64,7 +163,7 @@ func fnFormatInteger(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, er return nil, fmt.Errorf("xpath: internal error: expected integer value for %s, got %T", valAtom.TypeName, valAtom.Value) } - lang := "en" + lang := defaultFormattingLanguage if ec := getFnContext(ctx); ec != nil { lang = ec.getDefaultLanguage() } @@ -85,17 +184,35 @@ func fnFormatInteger(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, er lang = strings.ToLower(defaultLanguage) } + // Bound picture scanning and integer conversion before allocating formatted + // text; charge its output even if a caller consumes only string-length(). + if err := Charge(ctx, len(picture)); err != nil { + return nil, err + } + + if err := ChargeString(ctx, len(picture)); err != nil { + return nil, err + } + + if err := preflightNumeric(ctx, n.BitLen()); err != nil { + return nil, err + } + result, err := formatIntegerPicture(n, picture, lang) if err != nil { return nil, err } + if err := ChargeString(ctx, len(result)); err != nil { + return nil, err + } + return xdm.SingleString(result), nil } func isSupportedIntegerLanguage(lang string) bool { switch lang { - case "en", "de", "fr", "it": + case formatLanguageEnglish, formatLanguageGerman, formatLanguageFrench, formatLanguageItalian: return true default: return false @@ -133,28 +250,28 @@ func formatIntegerPicture(n *big.Int, picture, lang string) (string, error) { ) switch primary { - case "A": - result = formatAlpha(absN, true) - case "a": - result = formatAlpha(absN, false) - case "I": - result = formatRoman(absN, true) - case "i": - result = formatRoman(absN, false) - case "W": - result = formatWords(absN, lang, nameUpper2) + case formatAlphaUpper: + result = formatAlpha(absN, formatUppercase) + case formatAlphaLower: + result = formatAlpha(absN, formatLowercase) + case formatRomanUpper: + result = formatRoman(absN, formatUppercase) + case formatRomanLower: + result = formatRoman(absN, formatLowercase) + case formatWordsUpper: + result = formatWords(absN, lang, nameUpperCase) if ordinal { - result = applyOrdinalWords(result, lang, nameUpper2, ordSuffix) + result = applyOrdinalWords(result, lang, nameUpperCase, ordSuffix) } - case "w": + case formatWordsLower: result = formatWords(absN, lang, nameLower) if ordinal { result = applyOrdinalWords(result, lang, nameLower, ordSuffix) } - case "Ww": - result = formatWords(absN, lang, "title") + case formatWordsTitle: + result = formatWords(absN, lang, formatCaseTitle) if ordinal { - result = applyOrdinalWords(result, lang, "title", ordSuffix) + result = applyOrdinalWords(result, lang, formatCaseTitle, ordSuffix) } default: if tok := classifyFormatToken(primary); tok != fmtTokenDecimal { @@ -198,37 +315,6 @@ const ( fmtTokenUnknown ) -// FormatGreekAlphabetic uses the 24-letter Greek alphabet, excluding the -// unassigned capital U+03A2 and the context-specific lowercase final sigma. -// It is shared with XSLT numbering so both execution paths use the same digits. -func FormatGreekAlphabetic(n *big.Int, upper bool) string { - if n.Sign() <= 0 { - return n.String() - } - - alphabet := []rune("αβγδεζηθικλμνξοπρστυφχψω") - if upper { - alphabet = []rune("ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ") - } - - value, one, size := new(big.Int).Set(n), big.NewInt(1), big.NewInt(int64(len(alphabet))) - - var ( - digits []rune - remainder big.Int - ) - - for value.Sign() > 0 { - value.Sub(value, one) - value.DivMod(value, size, &remainder) - digits = append(digits, alphabet[remainder.Int64()]) - } - - slices.Reverse(digits) - - return string(digits) -} - func classifyFormatToken(primary string) formatTokenKind { runes := []rune(primary) if len(runes) == 0 { @@ -267,15 +353,15 @@ func classifyFormatToken(primary string) formatTokenKind { } switch { - case r >= 0x03B1 && r <= 0x03C9: + case r >= greekLowerFirst && r <= greekLowerLast: return fmtTokenGreekLower - case r >= 0x0391 && r <= 0x03A9: + case r >= greekUpperFirst && r <= greekUpperLast: return fmtTokenGreekUpper - case r >= 0x2460 && r <= 0x2473: + case r >= circledSymbolFirst && r <= circledSymbolLast: return fmtTokenCircled - case r >= 0x2474 && r <= 0x2487: + case r >= parenthesizedSymbolFirst && r <= parenthesizedSymbolLast: return fmtTokenParenthesized - case r >= 0x2488 && r <= 0x249B: + case r >= fullstopSymbolFirst && r <= fullstopSymbolLast: return fmtTokenFullstopDigit default: return fmtTokenUnknown @@ -285,20 +371,20 @@ func classifyFormatToken(primary string) formatTokenKind { func formatSpecialToken(n *big.Int, primary string, kind formatTokenKind) (string, error) { switch kind { case fmtTokenGreekLower: - return FormatGreekAlphabetic(n, false), nil + return FormatGreekAlphabetic(n, formatLowercase), nil case fmtTokenGreekUpper: - return FormatGreekAlphabetic(n, true), nil + return FormatGreekAlphabetic(n, formatUppercase), nil case fmtTokenCJK: return formatCJK(n), nil case fmtTokenCircled: - return formatSequentialSymbol(n, 0x2460, 20), nil + return formatSequentialSymbol(n, circledSymbolFirst, sequentialSymbolCount), nil case fmtTokenParenthesized: - return formatSequentialSymbol(n, 0x2474, 20), nil + return formatSequentialSymbol(n, parenthesizedSymbolFirst, sequentialSymbolCount), nil case fmtTokenFullstopDigit: - return formatSequentialSymbol(n, 0x2488, 20), nil + return formatSequentialSymbol(n, fullstopSymbolFirst, sequentialSymbolCount), nil case fmtTokenUnknown: // Spec: if the format token is not recognized, use decimal fallback - return formatIntegerDecimal(n, "1") + return formatIntegerDecimal(n, formatDecimalDefault) default: return formatIntegerDecimal(n, primary) } @@ -349,8 +435,8 @@ func int64ToCJK(n int64) string { // Split into 4-digit (myriad) groups, least-significant first. var groups []int64 for n > 0 { - groups = append(groups, n%10000) - n /= 10000 + groups = append(groups, n%cjkMyriadUnit) + n /= cjkMyriadUnit } var parts []string @@ -490,12 +576,12 @@ func validateModifier(mod string) error { // formatAlpha formats a number using alphabetic numbering (a=1, b=2, ..., z=26, aa=27, ...). func formatAlpha(n *big.Int, upper bool) string { if n.Sign() == 0 { - return "0" + return formatZeroDigit } val := new(big.Int).Set(n) one := big.NewInt(1) - twentySix := big.NewInt(26) + twentySix := big.NewInt(latinAlphabetSize) var digits []rune @@ -517,7 +603,11 @@ func formatAlpha(n *big.Int, upper bool) string { func formatRoman(n *big.Int, upper bool) string { if n.Sign() == 0 { - return "0" + return formatZeroDigit + } + + if !n.IsInt64() { + return n.String() } val := n.Int64() @@ -525,7 +615,7 @@ func formatRoman(n *big.Int, upper bool) string { val = -val } - if val > 10000 { + if val > romanMaximumValue { return n.String() } @@ -568,7 +658,7 @@ func formatRoman(n *big.Int, upper bool) string { } func formatWords(n *big.Int, lang, caseStyle string) string { - if lang != "en" && lang != "" { + if lang != formatLanguageEnglish && lang != "" { return formatWordsLang(n, lang, caseStyle) } @@ -579,9 +669,9 @@ func formatWords(n *big.Int, lang, caseStyle string) string { func applyCase(s, caseStyle string) string { switch caseStyle { - case nameUpper2: + case nameUpperCase: return strings.ToUpper(s) - case "title": + case formatCaseTitle: return toTitleCase(s) default: return strings.ToLower(s) @@ -649,10 +739,10 @@ func int64ToEnglishWords(n int64) string { } units := []unit{ - {1000000000000, "trillion"}, - {1000000000, "billion"}, - {1000000, "million"}, - {1000, "thousand"}, + {wordTrillion, "trillion"}, + {wordBillion, "billion"}, + {wordMillion, "million"}, + {wordThousand, "thousand"}, } for _, u := range units { @@ -662,9 +752,9 @@ func int64ToEnglishWords(n int64) string { } } - if n >= 100 { - parts = append(parts, onesWords(n/100)+" hundred") - n %= 100 + if n >= wordHundred { + parts = append(parts, onesWords(n/wordHundred)+" hundred") + n %= wordHundred } if n > 0 { @@ -672,12 +762,12 @@ func int64ToEnglishWords(n int64) string { parts = append(parts, nameAnd) } - if n < 20 { + if n < wordSmallNumberLimit { parts = append(parts, onesWords(n)) } else { - s := tensWords(n / 10) - if n%10 > 0 { - s += " " + onesWords(n%10) + s := tensWords(n / numericDecimalRadix) + if n%numericDecimalRadix > 0 { + s += " " + onesWords(n%numericDecimalRadix) } parts = append(parts, s) @@ -688,15 +778,15 @@ func int64ToEnglishWords(n int64) string { } func applyOrdinalWords(words, lang, caseStyle, suffix string) string { - if lang == "de" { + if lang == formatLanguageGerman { return applyOrdinalWordsDe(words, caseStyle, suffix) } - if lang == "fr" { + if lang == formatLanguageFrench { return applyOrdinalWordsFr(words, caseStyle, suffix) } - if lang == "it" { + if lang == formatLanguageItalian { return applyOrdinalWordsIt(words, caseStyle, suffix) } @@ -743,7 +833,7 @@ func englishWordToOrdinal(word string) string { return word[:len(word)-1] + "ieth" } - return word + "th" + return word + ordinalEnglishGeneralSuffix } func applyOrdinalDecimal(result string, n *big.Int, lang, suffix string) string { @@ -751,29 +841,29 @@ func applyOrdinalDecimal(result string, n *big.Int, lang, suffix string) string // NOT a literal suffix. We still use language-appropriate ordinal suffixes. _ = suffix // language hint, not literal - if lang != "en" && lang != "" { + if lang != formatLanguageEnglish && lang != "" { // For non-English, just append the number — implementation-defined return result } // English ordinal suffixes val := new(big.Int).Abs(n) - mod100 := new(big.Int).Mod(val, big.NewInt(100)).Int64() - mod10 := new(big.Int).Mod(val, big.NewInt(10)).Int64() + mod100 := new(big.Int).Mod(val, big.NewInt(ordinalLastTwoDigitsModulus)).Int64() + mod10 := new(big.Int).Mod(val, big.NewInt(numericDecimalRadix)).Int64() - if mod100 >= 11 && mod100 <= 13 { - return result + "th" + if mod100 >= ordinalTeenFirst && mod100 <= ordinalTeenLast { + return result + ordinalEnglishGeneralSuffix } switch mod10 { - case 1: - return result + "st" - case 2: - return result + "nd" - case 3: - return result + "rd" + case ordinalFirstLastDigit: + return result + ordinalEnglishFirstSuffix + case ordinalSecondLastDigit: + return result + ordinalEnglishSecondSuffix + case ordinalThirdLastDigit: + return result + ordinalEnglishThirdSuffix default: - return result + "th" + return result + ordinalEnglishGeneralSuffix } } @@ -785,7 +875,7 @@ func formatIntegerDecimal(n *big.Int, picture string) (string, error) { result := n.String() if len(result) < parsed.minDigits { - result = strings.Repeat("0", parsed.minDigits-len(result)) + result + result = strings.Repeat(formatZeroDigit, parsed.minDigits-len(result)) + result } if len(parsed.groups) > 1 { @@ -858,13 +948,13 @@ func parseIntegerDecimalPicture(picture string) (integerDecimalPicture, error) { return parsed, &xdm.Error{Code: errCodeFODF1310, Message: fmt.Sprintf("invalid picture: '#' after '0' in %q", picture)} } - parsed.minDigits = max(parsed.minDigits, 1) + parsed.minDigits = max(parsed.minDigits, integerMinimumDecimalDigits) return parsed, nil } func integerGroupingRepeats(groups []int) bool { - if len(groups) == 2 { + if len(groups) == integerRepeatingGroupPair { return true // One separator repeats at its rightmost interval. } @@ -929,19 +1019,24 @@ func unicodeDigitZero(r rune) rune { if r >= '0' && r <= '9' { return '0' } - // Unicode decimal digit blocks are consecutive groups of 10. - // Walk backwards from r to find the zero. - for z := r; z >= r-9 && z >= 0; z-- { - if !unicode.IsDigit(z) { - return z + 1 + // Contiguous Nd ranges can contain several adjacent digit alphabets. + for _, block := range unicode.Nd.R16 { + if r >= rune(block.Lo) && r <= rune(block.Hi) { + return r - (r-rune(block.Lo))%formatterDecimalDigitCount } } - return r - 9 // Fallback: assume it's a digit 9 + for _, block := range unicode.Nd.R32 { + if r >= rune(block.Lo) && r <= rune(block.Hi) { // #nosec G115 -- Unicode Nd tables contain only scalar code points. + return r - (r-rune(block.Lo))%formatterDecimalDigitCount // #nosec G115 -- Unicode Nd tables contain only scalar code points. + } + } + + return '0' } func isDecimalDigitInRange(r rune, zero rune) bool { - return r >= zero && r <= zero+9 + return r >= zero && r <= zero+formatterLastDecimalDigitOffset } func translateDigits(s string, zeroDigit rune) string { @@ -962,11 +1057,11 @@ func translateDigits(s string, zeroDigit rune) string { func formatWordsLang(n *big.Int, lang, caseStyle string) string { switch lang { - case "de": + case formatLanguageGerman: return applyCase(intToGermanWords(n), caseStyle) - case "fr": + case formatLanguageFrench: return applyCase(intToFrenchWords(n), caseStyle) - case "it": + case formatLanguageItalian: return applyCase(intToItalianWords(n), caseStyle) default: return applyCase(intToEnglishWords(n), caseStyle) @@ -975,7 +1070,7 @@ func formatWordsLang(n *big.Int, lang, caseStyle string) string { func intToGermanWords(n *big.Int) string { if n.Sign() == 0 { - return jsonKindNull + return wordGermanZero } if !n.IsInt64() { @@ -987,19 +1082,19 @@ func intToGermanWords(n *big.Int) string { func int64ToGermanWords(n int64) string { if n == 0 { - return jsonKindNull + return wordGermanZero } if n < 0 { return "minus " + int64ToGermanWords(-n) } - if n < 1000 { + if n < wordThousand { return germanWordsUnderThousand(n) } - if n < 1000000 { - q := n / 1000 + if n < wordMillion { + q := n / wordThousand w := "" if q == 1 { @@ -1009,15 +1104,15 @@ func int64ToGermanWords(n int64) string { } w += "tausend" - if n%1000 != 0 { - w += int64ToGermanWords(n % 1000) + if n%wordThousand != 0 { + w += int64ToGermanWords(n % wordThousand) } return w } - if n < 1000000000 { - q := n / 1000000 + if n < wordBillion { + q := n / wordMillion w := "" if q == 1 { @@ -1026,14 +1121,14 @@ func int64ToGermanWords(n int64) string { w = int64ToGermanWords(q) + " Millionen " } - if n%1000000 != 0 { - w += int64ToGermanWords(n % 1000000) + if n%wordMillion != 0 { + w += int64ToGermanWords(n % wordMillion) } return w } - return fmt.Sprintf("%d", n) + return fmt.Sprintf(formatDecimalPrintf, n) } func germanWordsUnderThousand(n int64) string { @@ -1043,31 +1138,31 @@ func germanWordsUnderThousand(n int64) string { } tens := []string{"", "", "zwanzig", "dreißig", "vierzig", "fünfzig", "sechzig", "siebzig", "achtzig", "neunzig"} - if n < 20 { + if n < wordSmallNumberLimit { return ones[n] // #nosec G602 -- The caller passes 1..999; this branch restricts n to 1..19. } - if n < 100 { - if n%10 == 0 { - return tens[n/10] + if n < wordHundred { + if n%numericDecimalRadix == 0 { + return tens[n/numericDecimalRadix] } - unit := ones[n%10] - if n%10 == 1 { + unit := ones[n%numericDecimalRadix] + if n%numericDecimalRadix == 1 { unit = nameEin } - return unit + "und" + tens[n/10] + return unit + "und" + tens[n/numericDecimalRadix] } - w := ones[n/100] + w := ones[n/wordHundred] if w == nameEins { w = nameEin } w += "hundert" - if n%100 != 0 { - w += int64ToGermanWords(n % 100) + if n%wordHundred != 0 { + w += int64ToGermanWords(n % wordHundred) } return w @@ -1075,7 +1170,7 @@ func germanWordsUnderThousand(n int64) string { func applyOrdinalWordsDe(words, caseStyle, suffix string) string { base := germanOrdinalWord(strings.ToLower(words)) - if suffix == "" || suffix == "%spellout-ordinal" { + if suffix == "" || suffix == ordinalSpellout { return applyCase(base, caseStyle) } @@ -1084,7 +1179,7 @@ func applyOrdinalWordsDe(words, caseStyle, suffix string) string { func germanOrdinalWord(words string) string { irregulars := map[string]string{ - jsonKindNull: "nullte", + wordGermanZero: "nullte", nameEin: "erste", nameEins: "erste", nameDrei: "dritte", @@ -1095,7 +1190,7 @@ func germanOrdinalWord(words string) string { return ordinal } - if n, ok := germanWordToNumber(words); ok && n < 20 { + if n, ok := germanWordToNumber(words); ok && n < wordSmallNumberLimit { return words + "te" } @@ -1104,7 +1199,7 @@ func germanOrdinalWord(words string) string { func germanWordToNumber(word string) (int64, bool) { units := map[string]int64{ - jsonKindNull: 0, nameEin: 1, nameEins: 1, "zwei": 2, nameDrei: 3, "vier": 4, + wordGermanZero: 0, nameEin: 1, nameEins: 1, "zwei": 2, nameDrei: 3, "vier": 4, "fünf": 5, "sechs": 6, wordGermanSeven: 7, nameAcht: 8, "neun": 9, "zehn": 10, "elf": 11, "zwölf": 12, "dreizehn": 13, "vierzehn": 14, "fünfzehn": 15, "sechzehn": 16, "siebzehn": 17, "achtzehn": 18, "neunzehn": 19, @@ -1188,16 +1283,16 @@ func int64ToFrenchWords(n int64) string { } tens := []string{"", "", "vingt", "trente", "quarante", "cinquante", "soixante", "soixante", "quatre-vingt", "quatre-vingt"} //nolint:misspell // French cardinal number, not an English spelling. - if n < 20 { + if n < wordSmallNumberLimit { return ones[n] } - if n < 100 { + if n < wordHundred { return frenchTensWords(n, ones, tens) } - if n < 1000 { - q := n / 100 + if n < wordThousand { + q := n / wordHundred w := "" if q == 1 { @@ -1206,7 +1301,7 @@ func int64ToFrenchWords(n int64) string { w = ones[q] + " cent" } - r := n % 100 + r := n % wordHundred if r == 0 && q > 1 { w += "s" } @@ -1218,8 +1313,8 @@ func int64ToFrenchWords(n int64) string { return w } - if n < 1000000 { - q := n / 1000 + if n < wordMillion { + q := n / wordThousand w := "" if q == 1 { @@ -1228,7 +1323,7 @@ func int64ToFrenchWords(n int64) string { w = int64ToFrenchWords(q) + " mille" } - r := n % 1000 + r := n % wordThousand if r != 0 { w += " " + int64ToFrenchWords(r) } @@ -1236,19 +1331,19 @@ func int64ToFrenchWords(n int64) string { return w } - return fmt.Sprintf("%d", n) + return fmt.Sprintf(formatDecimalPrintf, n) } func frenchTensWords(n int64, ones, tens []string) string { - t := n / 10 + t := n / numericDecimalRadix - u := n % 10 - if t == 7 || t == 9 { - u += 10 + u := n % numericDecimalRadix + if t == frenchCompositeSeventy || t == frenchCompositeNinety { + u += numericDecimalRadix } if u == 0 { - if t == 8 { + if t == frenchCompositeEighty { return "quatre-vingts" } @@ -1256,11 +1351,11 @@ func frenchTensWords(n int64, ones, tens []string) string { } sep := "-" - if u == 1 && t != 8 && t != 9 { + if u == frenchConjunctionOne && t != frenchCompositeEighty && t != frenchCompositeNinety { sep = " et " } - if u == 11 && t == 7 { + if u == frenchElevenUnit && t == frenchCompositeSeventy { sep = " et " } @@ -1305,28 +1400,28 @@ func int64ToItalianWords(n int64) string { } tens := []string{"", "", "venti", "trenta", "quaranta", "cinquanta", "sessanta", "settanta", "ottanta", "novanta"} - if n < 20 { + if n < wordSmallNumberLimit { return ones[n] } - if n < 100 { - t := n / 10 - u := n % 10 + if n < wordHundred { + t := n / numericDecimalRadix + u := n % numericDecimalRadix base := tens[t] if u == 0 { return base } - if u == 1 || u == 8 { + if u == italianElidedOne || u == italianElidedEight { base = base[:len(base)-1] } return base + ones[u] } - if n < 1000 { - q := n / 100 + if n < wordThousand { + q := n / wordHundred w := "" if q == 1 { @@ -1335,7 +1430,7 @@ func int64ToItalianWords(n int64) string { w = ones[q] + "cento" } - r := n % 100 + r := n % wordHundred if r != 0 { w += int64ToItalianWords(r) } @@ -1343,8 +1438,8 @@ func int64ToItalianWords(n int64) string { return w } - if n < 1000000 { - q := n / 1000 + if n < wordMillion { + q := n / wordThousand w := "" if q == 1 { @@ -1353,7 +1448,7 @@ func int64ToItalianWords(n int64) string { w = int64ToItalianWords(q) + "mila" } - r := n % 1000 + r := n % wordThousand if r != 0 { w += int64ToItalianWords(r) } @@ -1361,21 +1456,21 @@ func int64ToItalianWords(n int64) string { return w } - return fmt.Sprintf("%d", n) + return fmt.Sprintf(formatDecimalPrintf, n) } func applyOrdinalWordsIt(words, caseStyle, suffix string) string { lower := strings.ToLower(words) switch suffix { - case "", "%spellout-ordinal", "%spellout-ordinal-masculine", "-o": - return applyCase(italianOrdinalWord(lower, false), caseStyle) - case "%spellout-ordinal-feminine", "-a": - return applyCase(italianOrdinalWord(lower, true), caseStyle) + case "", ordinalSpellout, ordinalSpelloutMasculine, ordinalMasculineSuffix: + return applyCase(italianOrdinalWord(lower, ordinalMasculine), caseStyle) + case ordinalSpelloutFeminine, ordinalFeminineSuffix: + return applyCase(italianOrdinalWord(lower, ordinalFeminine), caseStyle) default: n := italianWordToNumber(lower) if n == 0 { - return applyCase(italianOrdinalWord(lower, false), caseStyle) + return applyCase(italianOrdinalWord(lower, ordinalMasculine), caseStyle) } return applyCase(italianOrdinalWithSuffix(n, suffix), caseStyle) @@ -1438,5 +1533,5 @@ func italianOrdinalWithSuffix(n int64, suffix string) string { return stem + sfx } - return fmt.Sprintf("%d°", n) + return fmt.Sprintf(italianOrdinalDecimalFormat, n) } diff --git a/transform/internal/xpath/format_number.go b/transform/internal/xpath/format_number.go index 732c08d..0aed85f 100644 --- a/transform/internal/xpath/format_number.go +++ b/transform/internal/xpath/format_number.go @@ -12,10 +12,25 @@ import ( "strings" "unicode" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/transform/internal/xdm" "github.com/skarm/xmlsec/transform/internal/xpath/number" ) +// Percent/per-mille multiply the value before decimal-picture rounding. +// Infinity selectors are math.IsInf conventions. Picture splitting retains a +// third part so invalid extra separators reach the existing picture validator. +const ( + formatPercentMultiplier = 100 + formatPerMilleMultiplier = 1000 + formatPositiveInfinitySign = 1 + formatNegativeInfinitySign = -1 + numberPictureSplitLimit = 3 + numberPositivePictureCount = 1 + numberNegativePictureIndex = 1 + floatExponentMarkers = "eE" +) + // DecimalFormat aliases the shared decimal-picture symbol and formatting // configuration. type DecimalFormat = number.DecimalFormat @@ -34,13 +49,13 @@ func DecimalZeroDigit(character rune) bool { for _, block := range unicode.Nd.R16 { if character >= rune(block.Lo) && character <= rune(block.Hi) { - return (character-rune(block.Lo))%10 == 0 + return (character-rune(block.Lo))%formatterDecimalDigitCount == 0 } } for _, block := range unicode.Nd.R32 { if character >= rune(block.Lo) && character <= rune(block.Hi) { // #nosec G115 -- Standard Unicode Nd tables contain code points at most utf8.MaxRune. - return (character-rune(block.Lo))%10 == 0 // #nosec G115 -- Standard Unicode Nd tables contain code points at most utf8.MaxRune. + return (character-rune(block.Lo))%formatterDecimalDigitCount == 0 // #nosec G115 -- Standard Unicode Nd tables contain code points at most utf8.MaxRune. } } @@ -56,7 +71,7 @@ func defaultDecimalFormat(ctx context.Context) number.DecimalFormat { } func resolveDecimalFormat(ctx context.Context, name string) (number.DecimalFormat, error) { - name = strings.Trim(name, " \t\r\n") + name = strings.Trim(name, lexicon.XMLWhitespace) if name == "" { return defaultDecimalFormat(ctx), nil } @@ -108,7 +123,7 @@ func resolveDecimalFormatName(ctx context.Context, name string) (QualifiedName, } func boundedFormatNumber(ctx context.Context, value xdm.AtomicValue, picture string, df number.DecimalFormat) (string, error) { - if ec := getFnContext(ctx); ec != nil && ec.version != 0 && ec.version < 31 { + if ec := getFnContext(ctx); ec != nil && ec.version != xpathVersionDefault && ec.version < xpathVersion31 { df.ExponentDisabled = true } @@ -135,8 +150,8 @@ func formatNumber(a xdm.AtomicValue, picture string, df number.DecimalFormat) (s f := a.ToFloat64() isNaN := math.IsNaN(f) - isPosInf := math.IsInf(f, 1) - isNegInf := math.IsInf(f, -1) + isPosInf := math.IsInf(f, formatPositiveInfinitySign) + isNegInf := math.IsInf(f, formatNegativeInfinitySign) negative := f < 0 || (f == 0 && math.Signbit(f)) var precise *big.Rat @@ -147,8 +162,13 @@ func formatNumber(a xdm.AtomicValue, picture string, df number.DecimalFormat) (s } else { precise = new(big.Rat).SetInt(a.BigInt()) } + + isNaN, isPosInf, isNegInf = false, false, false + negative = precise.Sign() < 0 } else if a.TypeName == xdm.TypeDecimal { precise = new(big.Rat).Set(a.BigRat()) + isNaN, isPosInf, isNegInf = false, false, false + negative = precise.Sign() < 0 } else if (a.TypeName == xdm.TypeDouble || a.TypeName == xdm.TypeFloat) && !isNaN && !isPosInf && !isNegInf { if s, err := atomicToString(a); err == nil { precise = parseCanonicalFloatRat(s) @@ -181,16 +201,16 @@ func scaledFloatPrecise(typeName string, f float64, picture string, negative boo } scaled := f - divisor := int64(100) + divisor := int64(formatPercentMultiplier) if pp.IsPercent { - scaled *= 100 + scaled *= formatPercentMultiplier } else { - scaled *= 1000 - divisor = 1000 + scaled *= formatPerMilleMultiplier + divisor = formatPerMilleMultiplier } - if math.IsNaN(scaled) || math.IsInf(scaled, 0) { + if math.IsNaN(scaled) || math.IsInf(scaled, eitherInfinitySign) { return nil } @@ -208,16 +228,16 @@ func scaledFloatPrecise(typeName string, f float64, picture string, negative boo } func selectedNumberPicture(picture string, negative bool, df number.DecimalFormat) string { - parts := strings.SplitN(picture, string(df.PatternSeparator), 3) - if negative && len(parts) > 1 { - return parts[1] + parts := strings.SplitN(picture, string(df.PatternSeparator), numberPictureSplitLimit) + if negative && len(parts) > numberPositivePictureCount { + return parts[numberNegativePictureIndex] } return parts[0] } func parseCanonicalFloatRat(s string) *big.Rat { - if idx := strings.IndexAny(s, "eE"); idx >= 0 { + if idx := strings.IndexAny(s, floatExponentMarkers); idx >= 0 { mantissa := parseCanonicalFloatRat(s[:idx]) if mantissa == nil { return nil @@ -228,7 +248,7 @@ func parseCanonicalFloatRat(s string) *big.Rat { return nil } - scale := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(absInt(exp))), nil) + scale := new(big.Int).Exp(big.NewInt(numericDecimalRadix), big.NewInt(int64(absInt(exp))), nil) if exp >= 0 { return mantissa.Mul(mantissa, new(big.Rat).SetInt(scale)) } diff --git a/transform/internal/xpath/function_catalog.go b/transform/internal/xpath/function_catalog.go index 495d5e1..5e728c4 100644 --- a/transform/internal/xpath/function_catalog.go +++ b/transform/internal/xpath/function_catalog.go @@ -9,6 +9,30 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) +// Catalog slots follow XPath2.0/3.0/3.1 order. The untyped bits encode which +// editions share a standard declaration; combinations below retain that mask. +const ( + catalogXPath20Bit = 1 << iota + catalogXPath30Bit + catalogXPath31Bit +) + +const ( + catalogXPath20Index = iota + catalogXPath30Index + catalogXPath31Index + standardFunctionCatalogCount = 3 +) + +// Each declaration has name and result headers before its parameters. The +// sentinel denotes a function that never returns, not empty-sequence(). The +// serialization prefix belongs to this library's own signature static context. +const ( + catalogSignatureHeaderFields = 2 + catalogNeverReturnsType = "none" + catalogSerializationPrefix = "output" +) + // Standard function type facts from the published W3C F&O recommendations: // 2.0: https://www.w3.org/TR/2010/REC-xpath-functions-20101214/ // 3.0: https://www.w3.org/TR/2014/REC-xpath-functions-30-20140408/ @@ -24,298 +48,298 @@ var standardFunctionDeclarations = [...]struct { versions uint8 signature string }{ - {4, "array:append|array(*)|array(*)|item()*"}, - {4, "array:filter|array(*)|array(*)|function(item()*) as xs:boolean"}, - {4, "array:flatten|item()*|item()*"}, - {4, "array:fold-left|item()*|array(*)|item()*|function(item()*, item()*) as item()*"}, - {4, "array:fold-right|item()*|array(*)|item()*|function(item()*, item()*) as item()*"}, - {4, "array:for-each-pair|array(*)|array(*)|array(*)|function(item()*, item()*) as item()*"}, - {4, "array:for-each|array(*)|array(*)|function(item()*) as item()*"}, - {4, "array:get|item()*|array(*)|xs:integer"}, - {4, "array:head|item()*|array(*)"}, - {4, "array:insert-before|array(*)|array(*)|xs:integer|item()*"}, - {4, "array:join|array(*)|array(*)*"}, - {4, "array:put|array(*)|array(*)|xs:integer|item()*"}, - {4, "array:remove|array(*)|array(*)|xs:integer*"}, - {4, "array:reverse|array(*)|array(*)"}, - {4, "array:size|xs:integer|array(*)"}, - {4, "array:sort|array(*)|array(*)"}, - {4, "array:sort|array(*)|array(*)|xs:string?"}, - {4, "array:sort|array(*)|array(*)|xs:string?|function(item()*) as xs:anyAtomicType*"}, - {4, "array:subarray|array(*)|array(*)|xs:integer"}, - {4, "array:subarray|array(*)|array(*)|xs:integer|xs:integer"}, - {4, "array:tail|array(*)|array(*)"}, - {7, "fn:QName|xs:QName|xs:string?|xs:string"}, - {7, "fn:abs|xs:numeric?|xs:numeric?"}, - {7, "fn:adjust-date-to-timezone|xs:date?|xs:date?"}, - {7, "fn:adjust-date-to-timezone|xs:date?|xs:date?|xs:dayTimeDuration?"}, - {7, "fn:adjust-dateTime-to-timezone|xs:dateTime?|xs:dateTime?"}, - {7, "fn:adjust-dateTime-to-timezone|xs:dateTime?|xs:dateTime?|xs:dayTimeDuration?"}, - {7, "fn:adjust-time-to-timezone|xs:time?|xs:time?"}, - {7, "fn:adjust-time-to-timezone|xs:time?|xs:time?|xs:dayTimeDuration?"}, - {6, "fn:analyze-string|element(fn:analyze-string-result)|xs:string?|xs:string"}, - {6, "fn:analyze-string|element(fn:analyze-string-result)|xs:string?|xs:string|xs:string"}, - {4, "fn:apply|item()*|function(*)|array(*)"}, - {6, "fn:available-environment-variables|xs:string*"}, - {7, "fn:avg|xs:anyAtomicType?|xs:anyAtomicType*"}, - {7, "fn:base-uri|xs:anyURI?"}, - {7, "fn:base-uri|xs:anyURI?|node()?"}, - {7, "fn:boolean|xs:boolean|item()*"}, - {7, "fn:ceiling|xs:numeric?|xs:numeric?"}, - {7, "fn:codepoint-equal|xs:boolean?|xs:string?|xs:string?"}, - {7, "fn:codepoints-to-string|xs:string|xs:integer*"}, - {4, "fn:collation-key|xs:base64Binary|xs:string"}, - {4, "fn:collation-key|xs:base64Binary|xs:string|xs:string"}, - {4, "fn:collection|item()*"}, - {4, "fn:collection|item()*|xs:string?"}, - {3, "fn:collection|node()*"}, - {3, "fn:collection|node()*|xs:string?"}, - {7, "fn:compare|xs:integer?|xs:string?|xs:string?"}, - {7, "fn:compare|xs:integer?|xs:string?|xs:string?|xs:string"}, - {4, "fn:contains-token|xs:boolean|xs:string*|xs:string"}, - {4, "fn:contains-token|xs:boolean|xs:string*|xs:string|xs:string"}, - {7, "fn:contains|xs:boolean|xs:string?|xs:string?"}, - {7, "fn:contains|xs:boolean|xs:string?|xs:string?|xs:string"}, - {7, "fn:count|xs:integer|item()*"}, - {1, "fn:current-dateTime|xs:dateTime"}, - {6, "fn:current-dateTime|xs:dateTimeStamp"}, - {7, "fn:current-date|xs:date"}, - {7, "fn:current-time|xs:time"}, - {6, "fn:data|xs:anyAtomicType*"}, - {7, "fn:data|xs:anyAtomicType*|item()*"}, - {7, "fn:dateTime|xs:dateTime?|xs:date?|xs:time?"}, - {7, "fn:day-from-dateTime|xs:integer?|xs:dateTime?"}, - {7, "fn:day-from-date|xs:integer?|xs:date?"}, - {7, "fn:days-from-duration|xs:integer?|xs:duration?"}, - {7, "fn:deep-equal|xs:boolean|item()*|item()*"}, - {7, "fn:deep-equal|xs:boolean|item()*|item()*|xs:string"}, - {7, "fn:default-collation|xs:string"}, - {4, "fn:default-language|xs:language"}, - {7, "fn:distinct-values|xs:anyAtomicType*|xs:anyAtomicType*"}, - {7, "fn:distinct-values|xs:anyAtomicType*|xs:anyAtomicType*|xs:string"}, - {7, "fn:doc-available|xs:boolean|xs:string?"}, - {6, "fn:document-uri|xs:anyURI?"}, - {7, "fn:document-uri|xs:anyURI?|node()?"}, - {7, "fn:doc|document-node()?|xs:string?"}, - {7, "fn:element-with-id|element()*|xs:string*"}, - {7, "fn:element-with-id|element()*|xs:string*|node()"}, - {7, "fn:empty|xs:boolean|item()*"}, - {7, "fn:encode-for-uri|xs:string|xs:string?"}, - {7, "fn:ends-with|xs:boolean|xs:string?|xs:string?"}, - {7, "fn:ends-with|xs:boolean|xs:string?|xs:string?|xs:string"}, - {6, "fn:environment-variable|xs:string?|xs:string"}, - {7, "fn:error|none"}, - {3, "fn:error|none|xs:QName"}, - {4, "fn:error|none|xs:QName?"}, - {7, "fn:error|none|xs:QName?|xs:string"}, - {7, "fn:error|none|xs:QName?|xs:string|item()*"}, - {7, "fn:escape-html-uri|xs:string|xs:string?"}, - {7, "fn:exactly-one|item()|item()*"}, - {7, "fn:exists|xs:boolean|item()*"}, - {7, "fn:false|xs:boolean"}, - {6, "fn:filter|item()*|item()*|function(item()) as xs:boolean"}, - {7, "fn:floor|xs:numeric?|xs:numeric?"}, - {6, "fn:fold-left|item()*|item()*|item()*|function(item()*, item()) as item()*"}, - {2, "fn:fold-right|item()*|item()*|item()*|function(item()*, item()) as item()*"}, - {4, "fn:fold-right|item()*|item()*|item()*|function(item(), item()*) as item()*"}, - {6, "fn:for-each-pair|item()*|item()*|item()*|function(item(), item()) as item()*"}, - {6, "fn:for-each|item()*|item()*|function(item()) as item()*"}, - {6, "fn:format-dateTime|xs:string?|xs:dateTime?|xs:string"}, - {6, "fn:format-dateTime|xs:string?|xs:dateTime?|xs:string|xs:string?|xs:string?|xs:string?"}, - {6, "fn:format-date|xs:string?|xs:date?|xs:string"}, - {6, "fn:format-date|xs:string?|xs:date?|xs:string|xs:string?|xs:string?|xs:string?"}, - {6, "fn:format-integer|xs:string|xs:integer?|xs:string"}, - {6, "fn:format-integer|xs:string|xs:integer?|xs:string|xs:string?"}, - {6, "fn:format-number|xs:string|xs:numeric?|xs:string"}, - {6, "fn:format-number|xs:string|xs:numeric?|xs:string|xs:string?"}, - {6, "fn:format-time|xs:string?|xs:time?|xs:string"}, - {6, "fn:format-time|xs:string?|xs:time?|xs:string|xs:string?|xs:string?|xs:string?"}, - {6, "fn:function-arity|xs:integer|function(*)"}, - {6, "fn:function-lookup|function(*)?|xs:QName|xs:integer"}, - {6, "fn:function-name|xs:QName?|function(*)"}, - {6, "fn:generate-id|xs:string"}, - {6, "fn:generate-id|xs:string|node()?"}, - {6, "fn:has-children|xs:boolean"}, - {6, "fn:has-children|xs:boolean|node()?"}, - {6, "fn:head|item()?|item()*"}, - {7, "fn:hours-from-dateTime|xs:integer?|xs:dateTime?"}, - {7, "fn:hours-from-duration|xs:integer?|xs:duration?"}, - {7, "fn:hours-from-time|xs:integer?|xs:time?"}, - {7, "fn:idref|node()*|xs:string*"}, - {7, "fn:idref|node()*|xs:string*|node()"}, - {7, "fn:id|element()*|xs:string*"}, - {7, "fn:id|element()*|xs:string*|node()"}, - {7, "fn:implicit-timezone|xs:dayTimeDuration"}, - {7, "fn:in-scope-prefixes|xs:string*|element()"}, - {7, "fn:index-of|xs:integer*|xs:anyAtomicType*|xs:anyAtomicType"}, - {7, "fn:index-of|xs:integer*|xs:anyAtomicType*|xs:anyAtomicType|xs:string"}, - {6, "fn:innermost|node()*|node()*"}, - {7, "fn:insert-before|item()*|item()*|xs:integer|item()*"}, - {7, "fn:iri-to-uri|xs:string|xs:string?"}, - {4, "fn:json-doc|item()?|xs:string?"}, - {4, "fn:json-doc|item()?|xs:string?|map(*)"}, - {4, "fn:json-to-xml|document-node()?|xs:string?"}, - {4, "fn:json-to-xml|document-node()?|xs:string?|map(*)"}, - {7, "fn:lang|xs:boolean|xs:string?"}, - {7, "fn:lang|xs:boolean|xs:string?|node()"}, - {7, "fn:last|xs:integer"}, - {4, "fn:load-xquery-module|map(*)|xs:string"}, - {4, "fn:load-xquery-module|map(*)|xs:string|map(*)"}, - {7, "fn:local-name-from-QName|xs:NCName?|xs:QName?"}, - {7, "fn:local-name|xs:string"}, - {7, "fn:local-name|xs:string|node()?"}, - {7, "fn:lower-case|xs:string|xs:string?"}, - {7, "fn:matches|xs:boolean|xs:string?|xs:string"}, - {7, "fn:matches|xs:boolean|xs:string?|xs:string|xs:string"}, - {7, "fn:max|xs:anyAtomicType?|xs:anyAtomicType*"}, - {7, "fn:max|xs:anyAtomicType?|xs:anyAtomicType*|xs:string"}, - {7, "fn:minutes-from-dateTime|xs:integer?|xs:dateTime?"}, - {7, "fn:minutes-from-duration|xs:integer?|xs:duration?"}, - {7, "fn:minutes-from-time|xs:integer?|xs:time?"}, - {7, "fn:min|xs:anyAtomicType?|xs:anyAtomicType*"}, - {7, "fn:min|xs:anyAtomicType?|xs:anyAtomicType*|xs:string"}, - {7, "fn:month-from-dateTime|xs:integer?|xs:dateTime?"}, - {7, "fn:month-from-date|xs:integer?|xs:date?"}, - {7, "fn:months-from-duration|xs:integer?|xs:duration?"}, - {7, "fn:namespace-uri-for-prefix|xs:anyURI?|xs:string?|element()"}, - {7, "fn:namespace-uri-from-QName|xs:anyURI?|xs:QName?"}, - {7, "fn:namespace-uri|xs:anyURI"}, - {7, "fn:namespace-uri|xs:anyURI|node()?"}, - {7, "fn:name|xs:string"}, - {7, "fn:name|xs:string|node()?"}, - {2, "fn:nilled|xs:boolean"}, - {4, "fn:nilled|xs:boolean?"}, - {7, "fn:nilled|xs:boolean?|node()?"}, - {6, "fn:node-name|xs:QName?"}, - {7, "fn:node-name|xs:QName?|node()?"}, - {7, "fn:normalize-space|xs:string"}, - {7, "fn:normalize-space|xs:string|xs:string?"}, - {7, "fn:normalize-unicode|xs:string|xs:string?"}, - {7, "fn:normalize-unicode|xs:string|xs:string?|xs:string"}, - {7, "fn:not|xs:boolean|item()*"}, - {7, "fn:number|xs:double"}, - {7, "fn:number|xs:double|xs:anyAtomicType?"}, - {7, "fn:one-or-more|item()+|item()*"}, - {6, "fn:outermost|node()*|node()*"}, - {4, "fn:parse-ietf-date|xs:dateTime?|xs:string?"}, - {4, "fn:parse-json|item()?|xs:string?"}, - {4, "fn:parse-json|item()?|xs:string?|map(*)"}, - {6, "fn:parse-xml-fragment|document-node()?|xs:string?"}, - {6, "fn:parse-xml|document-node(element(*))?|xs:string?"}, - {6, "fn:path|xs:string?"}, - {6, "fn:path|xs:string?|node()?"}, - {7, "fn:position|xs:integer"}, - {7, "fn:prefix-from-QName|xs:NCName?|xs:QName?"}, - {4, "fn:random-number-generator|map(xs:string, item())"}, - {4, "fn:random-number-generator|map(xs:string, item())|xs:anyAtomicType?"}, - {7, "fn:remove|item()*|item()*|xs:integer"}, - {7, "fn:replace|xs:string|xs:string?|xs:string|xs:string"}, - {7, "fn:replace|xs:string|xs:string?|xs:string|xs:string|xs:string"}, - {7, "fn:resolve-QName|xs:QName?|xs:string?|element()"}, - {7, "fn:resolve-uri|xs:anyURI?|xs:string?"}, - {7, "fn:resolve-uri|xs:anyURI?|xs:string?|xs:string"}, - {7, "fn:reverse|item()*|item()*"}, - {7, "fn:root|node()"}, - {7, "fn:root|node()?|node()?"}, - {7, "fn:round-half-to-even|xs:numeric?|xs:numeric?"}, - {7, "fn:round-half-to-even|xs:numeric?|xs:numeric?|xs:integer"}, - {7, "fn:round|xs:numeric?|xs:numeric?"}, - {6, "fn:round|xs:numeric?|xs:numeric?|xs:integer"}, - {7, "fn:seconds-from-dateTime|xs:decimal?|xs:dateTime?"}, - {7, "fn:seconds-from-duration|xs:decimal?|xs:duration?"}, - {7, "fn:seconds-from-time|xs:decimal?|xs:time?"}, - {6, "fn:serialize|xs:string|item()*"}, - {2, "fn:serialize|xs:string|item()*|element(output:serialization-parameters)?"}, - {4, "fn:serialize|xs:string|item()*|item()?"}, - {4, "fn:sort|item()*|item()*"}, - {4, "fn:sort|item()*|item()*|xs:string?"}, - {4, "fn:sort|item()*|item()*|xs:string?|function(item()) as xs:anyAtomicType*"}, - {7, "fn:starts-with|xs:boolean|xs:string?|xs:string?"}, - {7, "fn:starts-with|xs:boolean|xs:string?|xs:string?|xs:string"}, - {7, "fn:static-base-uri|xs:anyURI?"}, - {4, "fn:string-join|xs:string|xs:anyAtomicType*"}, - {4, "fn:string-join|xs:string|xs:anyAtomicType*|xs:string"}, - {2, "fn:string-join|xs:string|xs:string*"}, - {3, "fn:string-join|xs:string|xs:string*|xs:string"}, - {7, "fn:string-length|xs:integer"}, - {7, "fn:string-length|xs:integer|xs:string?"}, - {7, "fn:string-to-codepoints|xs:integer*|xs:string?"}, - {7, "fn:string|xs:string"}, - {7, "fn:string|xs:string|item()?"}, - {7, "fn:subsequence|item()*|item()*|xs:double"}, - {7, "fn:subsequence|item()*|item()*|xs:double|xs:double"}, - {7, "fn:substring-after|xs:string|xs:string?|xs:string?"}, - {7, "fn:substring-after|xs:string|xs:string?|xs:string?|xs:string"}, - {7, "fn:substring-before|xs:string|xs:string?|xs:string?"}, - {7, "fn:substring-before|xs:string|xs:string?|xs:string?|xs:string"}, - {7, "fn:substring|xs:string|xs:string?|xs:double"}, - {7, "fn:substring|xs:string|xs:string?|xs:double|xs:double"}, - {7, "fn:sum|xs:anyAtomicType?|xs:anyAtomicType*|xs:anyAtomicType?"}, - {7, "fn:sum|xs:anyAtomicType|xs:anyAtomicType*"}, - {6, "fn:tail|item()*|item()*"}, - {7, "fn:timezone-from-dateTime|xs:dayTimeDuration?|xs:dateTime?"}, - {7, "fn:timezone-from-date|xs:dayTimeDuration?|xs:date?"}, - {7, "fn:timezone-from-time|xs:dayTimeDuration?|xs:time?"}, - {4, "fn:tokenize|xs:string*|xs:string?"}, - {7, "fn:tokenize|xs:string*|xs:string?|xs:string"}, - {7, "fn:tokenize|xs:string*|xs:string?|xs:string|xs:string"}, - {4, "fn:trace|item()*|item()*"}, - {7, "fn:trace|item()*|item()*|xs:string"}, - {4, "fn:transform|map(*)|map(*)"}, - {7, "fn:translate|xs:string|xs:string?|xs:string|xs:string"}, - {7, "fn:true|xs:boolean"}, - {7, "fn:unordered|item()*|item()*"}, - {6, "fn:unparsed-text-available|xs:boolean|xs:string?"}, - {6, "fn:unparsed-text-available|xs:boolean|xs:string?|xs:string"}, - {6, "fn:unparsed-text-lines|xs:string*|xs:string?"}, - {6, "fn:unparsed-text-lines|xs:string*|xs:string?|xs:string"}, - {6, "fn:unparsed-text|xs:string?|xs:string?"}, - {6, "fn:unparsed-text|xs:string?|xs:string?|xs:string"}, - {7, "fn:upper-case|xs:string|xs:string?"}, - {6, "fn:uri-collection|xs:anyURI*"}, - {6, "fn:uri-collection|xs:anyURI*|xs:string?"}, - {4, "fn:xml-to-json|xs:string?|node()?"}, - {4, "fn:xml-to-json|xs:string?|node()?|map(*)"}, - {7, "fn:year-from-dateTime|xs:integer?|xs:dateTime?"}, - {7, "fn:year-from-date|xs:integer?|xs:date?"}, - {7, "fn:years-from-duration|xs:integer?|xs:duration?"}, - {7, "fn:zero-or-one|item()?|item()*"}, - {4, "map:contains|xs:boolean|map(*)|xs:anyAtomicType"}, - {4, "map:entry|map(*)|xs:anyAtomicType|item()*"}, - {4, "map:find|array(*)|item()*|xs:anyAtomicType"}, - {4, "map:for-each|item()*|map(*)|function(xs:anyAtomicType, item()*) as item()*"}, - {4, "map:get|item()*|map(*)|xs:anyAtomicType"}, - {4, "map:keys|xs:anyAtomicType*|map(*)"}, - {4, "map:merge|map(*)|map(*)*"}, - {4, "map:merge|map(*)|map(*)*|map(*)"}, - {4, "map:put|map(*)|map(*)|xs:anyAtomicType|item()*"}, - {4, "map:remove|map(*)|map(*)|xs:anyAtomicType*"}, - {4, "map:size|xs:integer|map(*)"}, - {6, "math:acos|xs:double?|xs:double?"}, - {6, "math:asin|xs:double?|xs:double?"}, - {6, "math:atan2|xs:double|xs:double|xs:double"}, - {6, "math:atan|xs:double?|xs:double?"}, - {6, "math:cos|xs:double?|xs:double?"}, - {6, "math:exp10|xs:double?|xs:double?"}, - {6, "math:exp|xs:double?|xs:double?"}, - {6, "math:log10|xs:double?|xs:double?"}, - {6, "math:log|xs:double?|xs:double?"}, - {6, "math:pi|xs:double"}, - {6, "math:pow|xs:double?|xs:double?|xs:numeric"}, - {6, "math:sin|xs:double?|xs:double?"}, - {6, "math:sqrt|xs:double?|xs:double?"}, - {6, "math:tan|xs:double?|xs:double?"}, + {catalogXPath31Bit, "array:append|array(*)|array(*)|item()*"}, + {catalogXPath31Bit, "array:filter|array(*)|array(*)|function(item()*) as xs:boolean"}, + {catalogXPath31Bit, "array:flatten|item()*|item()*"}, + {catalogXPath31Bit, "array:fold-left|item()*|array(*)|item()*|function(item()*, item()*) as item()*"}, + {catalogXPath31Bit, "array:fold-right|item()*|array(*)|item()*|function(item()*, item()*) as item()*"}, + {catalogXPath31Bit, "array:for-each-pair|array(*)|array(*)|array(*)|function(item()*, item()*) as item()*"}, + {catalogXPath31Bit, "array:for-each|array(*)|array(*)|function(item()*) as item()*"}, + {catalogXPath31Bit, "array:get|item()*|array(*)|xs:integer"}, + {catalogXPath31Bit, "array:head|item()*|array(*)"}, + {catalogXPath31Bit, "array:insert-before|array(*)|array(*)|xs:integer|item()*"}, + {catalogXPath31Bit, "array:join|array(*)|array(*)*"}, + {catalogXPath31Bit, "array:put|array(*)|array(*)|xs:integer|item()*"}, + {catalogXPath31Bit, "array:remove|array(*)|array(*)|xs:integer*"}, + {catalogXPath31Bit, "array:reverse|array(*)|array(*)"}, + {catalogXPath31Bit, "array:size|xs:integer|array(*)"}, + {catalogXPath31Bit, "array:sort|array(*)|array(*)"}, + {catalogXPath31Bit, "array:sort|array(*)|array(*)|xs:string?"}, + {catalogXPath31Bit, "array:sort|array(*)|array(*)|xs:string?|function(item()*) as xs:anyAtomicType*"}, + {catalogXPath31Bit, "array:subarray|array(*)|array(*)|xs:integer"}, + {catalogXPath31Bit, "array:subarray|array(*)|array(*)|xs:integer|xs:integer"}, + {catalogXPath31Bit, "array:tail|array(*)|array(*)"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:QName|xs:QName|xs:string?|xs:string"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:abs|xs:numeric?|xs:numeric?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:adjust-date-to-timezone|xs:date?|xs:date?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:adjust-date-to-timezone|xs:date?|xs:date?|xs:dayTimeDuration?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:adjust-dateTime-to-timezone|xs:dateTime?|xs:dateTime?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:adjust-dateTime-to-timezone|xs:dateTime?|xs:dateTime?|xs:dayTimeDuration?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:adjust-time-to-timezone|xs:time?|xs:time?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:adjust-time-to-timezone|xs:time?|xs:time?|xs:dayTimeDuration?"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:analyze-string|element(fn:analyze-string-result)|xs:string?|xs:string"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:analyze-string|element(fn:analyze-string-result)|xs:string?|xs:string|xs:string"}, + {catalogXPath31Bit, "fn:apply|item()*|function(*)|array(*)"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:available-environment-variables|xs:string*"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:avg|xs:anyAtomicType?|xs:anyAtomicType*"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:base-uri|xs:anyURI?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:base-uri|xs:anyURI?|node()?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:boolean|xs:boolean|item()*"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:ceiling|xs:numeric?|xs:numeric?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:codepoint-equal|xs:boolean?|xs:string?|xs:string?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:codepoints-to-string|xs:string|xs:integer*"}, + {catalogXPath31Bit, "fn:collation-key|xs:base64Binary|xs:string"}, + {catalogXPath31Bit, "fn:collation-key|xs:base64Binary|xs:string|xs:string"}, + {catalogXPath31Bit, "fn:collection|item()*"}, + {catalogXPath31Bit, "fn:collection|item()*|xs:string?"}, + {catalogXPath20Bit | catalogXPath30Bit, "fn:collection|node()*"}, + {catalogXPath20Bit | catalogXPath30Bit, "fn:collection|node()*|xs:string?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:compare|xs:integer?|xs:string?|xs:string?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:compare|xs:integer?|xs:string?|xs:string?|xs:string"}, + {catalogXPath31Bit, "fn:contains-token|xs:boolean|xs:string*|xs:string"}, + {catalogXPath31Bit, "fn:contains-token|xs:boolean|xs:string*|xs:string|xs:string"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:contains|xs:boolean|xs:string?|xs:string?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:contains|xs:boolean|xs:string?|xs:string?|xs:string"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:count|xs:integer|item()*"}, + {catalogXPath20Bit, "fn:current-dateTime|xs:dateTime"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:current-dateTime|xs:dateTimeStamp"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:current-date|xs:date"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:current-time|xs:time"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:data|xs:anyAtomicType*"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:data|xs:anyAtomicType*|item()*"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:dateTime|xs:dateTime?|xs:date?|xs:time?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:day-from-dateTime|xs:integer?|xs:dateTime?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:day-from-date|xs:integer?|xs:date?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:days-from-duration|xs:integer?|xs:duration?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:deep-equal|xs:boolean|item()*|item()*"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:deep-equal|xs:boolean|item()*|item()*|xs:string"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:default-collation|xs:string"}, + {catalogXPath31Bit, "fn:default-language|xs:language"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:distinct-values|xs:anyAtomicType*|xs:anyAtomicType*"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:distinct-values|xs:anyAtomicType*|xs:anyAtomicType*|xs:string"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:doc-available|xs:boolean|xs:string?"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:document-uri|xs:anyURI?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:document-uri|xs:anyURI?|node()?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:doc|document-node()?|xs:string?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:element-with-id|element()*|xs:string*"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:element-with-id|element()*|xs:string*|node()"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:empty|xs:boolean|item()*"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:encode-for-uri|xs:string|xs:string?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:ends-with|xs:boolean|xs:string?|xs:string?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:ends-with|xs:boolean|xs:string?|xs:string?|xs:string"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:environment-variable|xs:string?|xs:string"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:error|none"}, + {catalogXPath20Bit | catalogXPath30Bit, "fn:error|none|xs:QName"}, + {catalogXPath31Bit, "fn:error|none|xs:QName?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:error|none|xs:QName?|xs:string"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:error|none|xs:QName?|xs:string|item()*"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:escape-html-uri|xs:string|xs:string?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:exactly-one|item()|item()*"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:exists|xs:boolean|item()*"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:false|xs:boolean"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:filter|item()*|item()*|function(item()) as xs:boolean"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:floor|xs:numeric?|xs:numeric?"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:fold-left|item()*|item()*|item()*|function(item()*, item()) as item()*"}, + {catalogXPath30Bit, "fn:fold-right|item()*|item()*|item()*|function(item()*, item()) as item()*"}, + {catalogXPath31Bit, "fn:fold-right|item()*|item()*|item()*|function(item(), item()*) as item()*"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:for-each-pair|item()*|item()*|item()*|function(item(), item()) as item()*"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:for-each|item()*|item()*|function(item()) as item()*"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:format-dateTime|xs:string?|xs:dateTime?|xs:string"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:format-dateTime|xs:string?|xs:dateTime?|xs:string|xs:string?|xs:string?|xs:string?"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:format-date|xs:string?|xs:date?|xs:string"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:format-date|xs:string?|xs:date?|xs:string|xs:string?|xs:string?|xs:string?"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:format-integer|xs:string|xs:integer?|xs:string"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:format-integer|xs:string|xs:integer?|xs:string|xs:string?"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:format-number|xs:string|xs:numeric?|xs:string"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:format-number|xs:string|xs:numeric?|xs:string|xs:string?"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:format-time|xs:string?|xs:time?|xs:string"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:format-time|xs:string?|xs:time?|xs:string|xs:string?|xs:string?|xs:string?"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:function-arity|xs:integer|function(*)"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:function-lookup|function(*)?|xs:QName|xs:integer"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:function-name|xs:QName?|function(*)"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:generate-id|xs:string"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:generate-id|xs:string|node()?"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:has-children|xs:boolean"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:has-children|xs:boolean|node()?"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:head|item()?|item()*"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:hours-from-dateTime|xs:integer?|xs:dateTime?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:hours-from-duration|xs:integer?|xs:duration?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:hours-from-time|xs:integer?|xs:time?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:idref|node()*|xs:string*"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:idref|node()*|xs:string*|node()"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:id|element()*|xs:string*"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:id|element()*|xs:string*|node()"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:implicit-timezone|xs:dayTimeDuration"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:in-scope-prefixes|xs:string*|element()"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:index-of|xs:integer*|xs:anyAtomicType*|xs:anyAtomicType"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:index-of|xs:integer*|xs:anyAtomicType*|xs:anyAtomicType|xs:string"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:innermost|node()*|node()*"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:insert-before|item()*|item()*|xs:integer|item()*"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:iri-to-uri|xs:string|xs:string?"}, + {catalogXPath31Bit, "fn:json-doc|item()?|xs:string?"}, + {catalogXPath31Bit, "fn:json-doc|item()?|xs:string?|map(*)"}, + {catalogXPath31Bit, "fn:json-to-xml|document-node()?|xs:string?"}, + {catalogXPath31Bit, "fn:json-to-xml|document-node()?|xs:string?|map(*)"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:lang|xs:boolean|xs:string?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:lang|xs:boolean|xs:string?|node()"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:last|xs:integer"}, + {catalogXPath31Bit, "fn:load-xquery-module|map(*)|xs:string"}, + {catalogXPath31Bit, "fn:load-xquery-module|map(*)|xs:string|map(*)"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:local-name-from-QName|xs:NCName?|xs:QName?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:local-name|xs:string"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:local-name|xs:string|node()?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:lower-case|xs:string|xs:string?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:matches|xs:boolean|xs:string?|xs:string"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:matches|xs:boolean|xs:string?|xs:string|xs:string"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:max|xs:anyAtomicType?|xs:anyAtomicType*"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:max|xs:anyAtomicType?|xs:anyAtomicType*|xs:string"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:minutes-from-dateTime|xs:integer?|xs:dateTime?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:minutes-from-duration|xs:integer?|xs:duration?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:minutes-from-time|xs:integer?|xs:time?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:min|xs:anyAtomicType?|xs:anyAtomicType*"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:min|xs:anyAtomicType?|xs:anyAtomicType*|xs:string"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:month-from-dateTime|xs:integer?|xs:dateTime?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:month-from-date|xs:integer?|xs:date?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:months-from-duration|xs:integer?|xs:duration?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:namespace-uri-for-prefix|xs:anyURI?|xs:string?|element()"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:namespace-uri-from-QName|xs:anyURI?|xs:QName?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:namespace-uri|xs:anyURI"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:namespace-uri|xs:anyURI|node()?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:name|xs:string"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:name|xs:string|node()?"}, + {catalogXPath30Bit, "fn:nilled|xs:boolean"}, + {catalogXPath31Bit, "fn:nilled|xs:boolean?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:nilled|xs:boolean?|node()?"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:node-name|xs:QName?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:node-name|xs:QName?|node()?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:normalize-space|xs:string"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:normalize-space|xs:string|xs:string?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:normalize-unicode|xs:string|xs:string?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:normalize-unicode|xs:string|xs:string?|xs:string"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:not|xs:boolean|item()*"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:number|xs:double"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:number|xs:double|xs:anyAtomicType?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:one-or-more|item()+|item()*"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:outermost|node()*|node()*"}, + {catalogXPath31Bit, "fn:parse-ietf-date|xs:dateTime?|xs:string?"}, + {catalogXPath31Bit, "fn:parse-json|item()?|xs:string?"}, + {catalogXPath31Bit, "fn:parse-json|item()?|xs:string?|map(*)"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:parse-xml-fragment|document-node()?|xs:string?"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:parse-xml|document-node(element(*))?|xs:string?"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:path|xs:string?"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:path|xs:string?|node()?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:position|xs:integer"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:prefix-from-QName|xs:NCName?|xs:QName?"}, + {catalogXPath31Bit, "fn:random-number-generator|map(xs:string, item())"}, + {catalogXPath31Bit, "fn:random-number-generator|map(xs:string, item())|xs:anyAtomicType?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:remove|item()*|item()*|xs:integer"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:replace|xs:string|xs:string?|xs:string|xs:string"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:replace|xs:string|xs:string?|xs:string|xs:string|xs:string"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:resolve-QName|xs:QName?|xs:string?|element()"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:resolve-uri|xs:anyURI?|xs:string?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:resolve-uri|xs:anyURI?|xs:string?|xs:string"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:reverse|item()*|item()*"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:root|node()"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:root|node()?|node()?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:round-half-to-even|xs:numeric?|xs:numeric?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:round-half-to-even|xs:numeric?|xs:numeric?|xs:integer"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:round|xs:numeric?|xs:numeric?"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:round|xs:numeric?|xs:numeric?|xs:integer"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:seconds-from-dateTime|xs:decimal?|xs:dateTime?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:seconds-from-duration|xs:decimal?|xs:duration?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:seconds-from-time|xs:decimal?|xs:time?"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:serialize|xs:string|item()*"}, + {catalogXPath30Bit, "fn:serialize|xs:string|item()*|element(output:serialization-parameters)?"}, + {catalogXPath31Bit, "fn:serialize|xs:string|item()*|item()?"}, + {catalogXPath31Bit, "fn:sort|item()*|item()*"}, + {catalogXPath31Bit, "fn:sort|item()*|item()*|xs:string?"}, + {catalogXPath31Bit, "fn:sort|item()*|item()*|xs:string?|function(item()) as xs:anyAtomicType*"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:starts-with|xs:boolean|xs:string?|xs:string?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:starts-with|xs:boolean|xs:string?|xs:string?|xs:string"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:static-base-uri|xs:anyURI?"}, + {catalogXPath31Bit, "fn:string-join|xs:string|xs:anyAtomicType*"}, + {catalogXPath31Bit, "fn:string-join|xs:string|xs:anyAtomicType*|xs:string"}, + {catalogXPath30Bit, "fn:string-join|xs:string|xs:string*"}, + {catalogXPath20Bit | catalogXPath30Bit, "fn:string-join|xs:string|xs:string*|xs:string"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:string-length|xs:integer"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:string-length|xs:integer|xs:string?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:string-to-codepoints|xs:integer*|xs:string?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:string|xs:string"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:string|xs:string|item()?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:subsequence|item()*|item()*|xs:double"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:subsequence|item()*|item()*|xs:double|xs:double"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:substring-after|xs:string|xs:string?|xs:string?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:substring-after|xs:string|xs:string?|xs:string?|xs:string"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:substring-before|xs:string|xs:string?|xs:string?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:substring-before|xs:string|xs:string?|xs:string?|xs:string"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:substring|xs:string|xs:string?|xs:double"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:substring|xs:string|xs:string?|xs:double|xs:double"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:sum|xs:anyAtomicType?|xs:anyAtomicType*|xs:anyAtomicType?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:sum|xs:anyAtomicType|xs:anyAtomicType*"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:tail|item()*|item()*"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:timezone-from-dateTime|xs:dayTimeDuration?|xs:dateTime?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:timezone-from-date|xs:dayTimeDuration?|xs:date?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:timezone-from-time|xs:dayTimeDuration?|xs:time?"}, + {catalogXPath31Bit, "fn:tokenize|xs:string*|xs:string?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:tokenize|xs:string*|xs:string?|xs:string"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:tokenize|xs:string*|xs:string?|xs:string|xs:string"}, + {catalogXPath31Bit, "fn:trace|item()*|item()*"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:trace|item()*|item()*|xs:string"}, + {catalogXPath31Bit, "fn:transform|map(*)|map(*)"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:translate|xs:string|xs:string?|xs:string|xs:string"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:true|xs:boolean"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:unordered|item()*|item()*"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:unparsed-text-available|xs:boolean|xs:string?"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:unparsed-text-available|xs:boolean|xs:string?|xs:string"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:unparsed-text-lines|xs:string*|xs:string?"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:unparsed-text-lines|xs:string*|xs:string?|xs:string"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:unparsed-text|xs:string?|xs:string?"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:unparsed-text|xs:string?|xs:string?|xs:string"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:upper-case|xs:string|xs:string?"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:uri-collection|xs:anyURI*"}, + {catalogXPath30Bit | catalogXPath31Bit, "fn:uri-collection|xs:anyURI*|xs:string?"}, + {catalogXPath31Bit, "fn:xml-to-json|xs:string?|node()?"}, + {catalogXPath31Bit, "fn:xml-to-json|xs:string?|node()?|map(*)"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:year-from-dateTime|xs:integer?|xs:dateTime?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:year-from-date|xs:integer?|xs:date?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:years-from-duration|xs:integer?|xs:duration?"}, + {catalogXPath20Bit | catalogXPath30Bit | catalogXPath31Bit, "fn:zero-or-one|item()?|item()*"}, + {catalogXPath31Bit, "map:contains|xs:boolean|map(*)|xs:anyAtomicType"}, + {catalogXPath31Bit, "map:entry|map(*)|xs:anyAtomicType|item()*"}, + {catalogXPath31Bit, "map:find|array(*)|item()*|xs:anyAtomicType"}, + {catalogXPath31Bit, "map:for-each|item()*|map(*)|function(xs:anyAtomicType, item()*) as item()*"}, + {catalogXPath31Bit, "map:get|item()*|map(*)|xs:anyAtomicType"}, + {catalogXPath31Bit, "map:keys|xs:anyAtomicType*|map(*)"}, + {catalogXPath31Bit, "map:merge|map(*)|map(*)*"}, + {catalogXPath31Bit, "map:merge|map(*)|map(*)*|map(*)"}, + {catalogXPath31Bit, "map:put|map(*)|map(*)|xs:anyAtomicType|item()*"}, + {catalogXPath31Bit, "map:remove|map(*)|map(*)|xs:anyAtomicType*"}, + {catalogXPath31Bit, "map:size|xs:integer|map(*)"}, + {catalogXPath30Bit | catalogXPath31Bit, "math:acos|xs:double?|xs:double?"}, + {catalogXPath30Bit | catalogXPath31Bit, "math:asin|xs:double?|xs:double?"}, + {catalogXPath30Bit | catalogXPath31Bit, "math:atan2|xs:double|xs:double|xs:double"}, + {catalogXPath30Bit | catalogXPath31Bit, "math:atan|xs:double?|xs:double?"}, + {catalogXPath30Bit | catalogXPath31Bit, "math:cos|xs:double?|xs:double?"}, + {catalogXPath30Bit | catalogXPath31Bit, "math:exp10|xs:double?|xs:double?"}, + {catalogXPath30Bit | catalogXPath31Bit, "math:exp|xs:double?|xs:double?"}, + {catalogXPath30Bit | catalogXPath31Bit, "math:log10|xs:double?|xs:double?"}, + {catalogXPath30Bit | catalogXPath31Bit, "math:log|xs:double?|xs:double?"}, + {catalogXPath30Bit | catalogXPath31Bit, "math:pi|xs:double"}, + {catalogXPath30Bit | catalogXPath31Bit, "math:pow|xs:double?|xs:double?|xs:numeric"}, + {catalogXPath30Bit | catalogXPath31Bit, "math:sin|xs:double?|xs:double?"}, + {catalogXPath30Bit | catalogXPath31Bit, "math:sqrt|xs:double?|xs:double?"}, + {catalogXPath30Bit | catalogXPath31Bit, "math:tan|xs:double?|xs:double?"}, } //nolint:gochecknoglobals // Immutable standard type data is shared across independent evaluations. -var standardFunctionCatalogs = sync.OnceValue(func() [3]map[sigKey]*functionSignature { - var catalogs [3]map[sigKey]*functionSignature +var standardFunctionCatalogs = sync.OnceValue(func() [standardFunctionCatalogCount]map[sigKey]*functionSignature { + var catalogs [standardFunctionCatalogCount]map[sigKey]*functionSignature for i := range catalogs { catalogs[i] = make(map[sigKey]*functionSignature) } namespaces := PredeclaredNamespaces() - namespaces["output"] = lexicon.NamespaceSerialization + namespaces[catalogSerializationPrefix] = lexicon.NamespaceSerialization types := make(map[string]xdm.SequenceType) sequenceType := func(source string) xdm.SequenceType { if result, found := types[source]; found { @@ -341,12 +365,12 @@ var standardFunctionCatalogs = sync.OnceValue(func() [3]map[sigKey]*functionSign fields := strings.Split(declaration.signature, "|") prefix, name, _ := strings.Cut(fields[0], ":") - signature := &functionSignature{ParamTypes: make([]xdm.SequenceType, len(fields)-2)} - for i, parameter := range fields[2:] { + signature := &functionSignature{ParamTypes: make([]xdm.SequenceType, len(fields)-catalogSignatureHeaderFields)} + for i, parameter := range fields[catalogSignatureHeaderFields:] { signature.ParamTypes[i] = sequenceType(parameter) } - if fields[1] != "none" { + if fields[1] != catalogNeverReturnsType { result := sequenceType(fields[1]) signature.ReturnType = &result } @@ -354,7 +378,7 @@ var standardFunctionCatalogs = sync.OnceValue(func() [3]map[sigKey]*functionSign key := sigKey{URI: namespaces[prefix], Name: name, Arity: len(signature.ParamTypes)} for i := range catalogs { - if declaration.versions&(1<= 2 { + if name == nameConcat && arity >= concatMinimumArity { signature := newFunctionSignature([]xdm.SequenceType{stAtomic(xdm.TypeAnyAtomicType, xdm.OccurrenceZeroOrOne)}, stAtomic(xdm.TypeString, xdm.OccurrenceExactlyOne)) - signature.RepeatParamType = true + signature.RepeatParamType = concatRepeatedParameterType return signature } // XSLT 2.0 supplies these host functions before their adoption into F&O. switch name { case lexicon.FnFormatNumber: - if arity == 2 || arity == 3 { + if arity == formatNumberRequiredArity || arity == formatNumberNamedArity { params := []xdm.SequenceType{stAtomic(xdm.TypeNumeric, xdm.OccurrenceZeroOrOne), stAtomic(xdm.TypeString, xdm.OccurrenceExactlyOne)} - if arity == 3 { + if arity == formatNumberNamedArity { params = append(params, stAtomic(xdm.TypeString, xdm.OccurrenceExactlyOne)) } @@ -83,7 +93,7 @@ func lookupFunctionSignature(version int, uri, name string, arity int) *function } case lexicon.FnGenerateID, fnNameFormatDate, fnNameFormatDateTime, fnNameFormatTime, fnNameUnparsedText, nameUnparsedTextLines, fnNameUnparsedTextAvailable: - return lookupStandardSignature(31, key) + return lookupStandardSignature(xpathVersion31, key) } return nil diff --git a/transform/internal/xpath/functions.go b/transform/internal/xpath/functions.go index f0b62a0..71ca9fe 100644 --- a/transform/internal/xpath/functions.go +++ b/transform/internal/xpath/functions.go @@ -17,6 +17,22 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) +// Standard built-in registry arities count arguments. These package-shared +// values do not describe sequence cardinality, focus positions or output limits. +const ( + builtinArityZero = 0 + builtinArityOne = 1 + builtinArityTwo = 2 + builtinArityThree = 3 + builtinArityFour = 4 + builtinArityFive = 5 +) + +// prefixFn is the conventional prefix for the standard function namespace. +const ( + prefixFn = "fn" +) + // Namespace URIs for standard XPath 3.1 function namespaces. const ( NSFn = lexicon.NamespaceFn @@ -27,6 +43,43 @@ const ( NSXS = lexicon.NamespaceXSD ) +// Function argument cardinality and result fast-path cardinality have distinct +// roles. The latter only selects an accounting shortcut, not a result limit. +const ( + singletonFunctionArgument = 1 + singleBuiltinResult = 1 +) + +// Built-in arities retain the standard signatures. Boolean predicates are +// unary, truth constants are nullary, and error/trace keep independent ranges. +// Nonnegative maximum arities are finite; negative maxima retain variadic behavior. +const ( + predicateFunctionArity = 1 + constantBooleanArity = 0 + fnErrorMinArity = 0 + fnErrorMaxArity = 3 + fnTraceMinArity = 1 + fnTraceMaxArity = 2 + finiteArityFloor = 0 + fnNameError = "error" + fnNameTrace = "trace" +) + +// These message fragments are inputs to the existing error-category remapping. +// The default error description and trace fragments preserve public diagnostics +// exactly; trace accounting charges one logical operation per inspected item. +const ( + emptySequenceMessageMarker = "empty sequence" + multiItemMessageMarker = "length > 1" + defaultErrorDescription = "error() called" + traceOutputPrefix = "[trace] " + traceLabelSeparator = ": " + traceItemSeparator = ", " + traceOutputTerminator = "\n" + traceOpaqueItemFormat = "<%T>" + traceItemWork = 1 +) + // Function implements a built-in or host-supplied XPath function. Implementations // receive invocation context and argument sequences; shared implementations must // support concurrent evaluation. @@ -68,14 +121,6 @@ type FunctionRefBinder interface { BindFunctionReference() Function } -func bindFunctionReference(fn Function) Function { - if binder, ok := fn.(FunctionRefBinder); ok { - return binder.BindFunctionReference() - } - - return fn -} - // TypedFunction extends Function with type signature information. // Implementations that expose parameter and return types enable // correct instance-of checks and function coercion for user-defined functions. @@ -137,7 +182,7 @@ func (f *hostBuiltin) FuncReturnTypeForArity(arity int) *xdm.SequenceType { // PredeclaredNamespace resolves a standard XPath namespace prefix. func PredeclaredNamespace(prefix string) (string, bool) { switch prefix { - case "fn": + case prefixFn: return NSFn, true case nameMath: return NSMath, true @@ -147,7 +192,7 @@ func PredeclaredNamespace(prefix string) (string, bool) { return NSArray, true case lexicon.PrefixErr: return NSErr, true - case "xs": + case lexicon.PrefixXS: return NSXS, true } @@ -156,7 +201,7 @@ func PredeclaredNamespace(prefix string) (string, bool) { // PredeclaredNamespaces returns caller-owned standard namespace bindings. func PredeclaredNamespaces() map[string]string { - return map[string]string{"fn": NSFn, nameMath: NSMath, keywordMap: NSMap, keywordArray: NSArray, lexicon.PrefixErr: NSErr, "xs": NSXS} + return map[string]string{prefixFn: NSFn, nameMath: NSMath, keywordMap: NSMap, keywordArray: NSArray, lexicon.PrefixErr: NSErr, lexicon.PrefixXS: NSXS} } // IsBuiltinFunction returns true if name is a registered XPath built-in function. @@ -195,6 +240,14 @@ func StandardFunctionAcceptsArity(uri, name string, arity int) bool { return checkArity(fn, name, arity) == nil } +func bindFunctionReference(fn Function) Function { + if binder, ok := fn.(FunctionRefBinder); ok { + return binder.BindFunctionReference() + } + + return fn +} + // isExtensionBuiltin identifies built-ins outside F&O 3.1. They remain // static-call-only so function-lookup cannot bypass a restricted static context. func isExtensionBuiltin(fn Function) bool { @@ -212,7 +265,7 @@ func unavailableModuleFunction(fn Function) bool { func namespacePrefixFor(uri string) string { switch uri { case NSFn: - return "fn" + return prefixFn case NSMath: return nameMath case NSMap: @@ -222,7 +275,7 @@ func namespacePrefixFor(uri string) string { case NSErr: return lexicon.PrefixErr case NSXS: - return "xs" + return lexicon.PrefixXS } return "" @@ -336,7 +389,7 @@ func resolveFunctionByURIInfo(ctx context.Context, ec *evalContext, uri, name st } cause := fmt.Errorf("%w: %s#%d", ErrUnknownFunction, name, arity) - if ec.version >= 20 { + if ec.version >= xpathVersion20 { return resolveUnavailableFunction(ctx, ec, uri, name, arity, &xdm.Error{Code: lexicon.ErrXPST0017, Message: cause.Error(), Cause: cause}) } @@ -377,7 +430,7 @@ func checkArity(fn Function, name string, arity int) error { return fmt.Errorf("%w: %s requires at least %d arguments, got %d", ErrArityMismatch, name, minArity, arity) } - if maxArity >= 0 && arity > maxArity { + if maxArity >= finiteArityFloor && arity > maxArity { return fmt.Errorf("%w: %s accepts at most %d arguments, got %d", ErrArityMismatch, name, maxArity, arity) } @@ -409,7 +462,7 @@ func (f *builtinFunc) Call(ctx context.Context, args []xdm.Sequence) (xdm.Sequen return nil, err } // Function items and higher-order calls can bypass scalar AST/VM evaluation. - if xdm.SequenceLen(result) == 1 { + if xdm.SequenceLen(result) == singleBuiltinResult { if value, ok := result.Get(0).(xdm.AtomicValue); ok { err = ChargeNumber(ctx, value) } @@ -451,7 +504,7 @@ func seqToStringErr(ctx context.Context, seq xdm.Sequence) (string, error) { return "", nil } - if len(atoms) > 1 { + if len(atoms) > singletonFunctionArgument { return "", &xdm.Error{Code: lexicon.ErrXPTY0004, Message: fmt.Sprintf("expected single string, got sequence of length %d", len(atoms))} } @@ -522,7 +575,7 @@ func coerceAtomizedString(ctx context.Context, seq xdm.Sequence) (value string, if _, serr := atomizeStreamCont(seq, typedValueItemCheck(ctx), func(av xdm.AtomicValue) (bool, error) { count++ - if count == 1 { + if count == singletonFunctionArgument { first = av return true, nil } @@ -537,7 +590,7 @@ func coerceAtomizedString(ctx context.Context, seq xdm.Sequence) (value string, return "", true, nil } - if count > 1 { + if count > singletonFunctionArgument { return "", false, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "expected xs:string?, got sequence of length > 1"} } @@ -568,7 +621,7 @@ func coerceArgToInteger(ctx context.Context, seq xdm.Sequence) (int64, error) { switch xdm.SequenceLen(seq) { case 0: return 0, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "expected xs:integer, got empty sequence"} - case 1: + case singletonFunctionArgument: default: return 0, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "expected xs:integer, got sequence of length > 1"} } @@ -619,13 +672,13 @@ func coerceArgToInteger(ctx context.Context, seq xdm.Sequence) (int64, error) { // coerceArgToDoubleRequired applies XPath 3.1 function coercion rules for xs:double params. // Accepts any numeric type and xs:untypedAtomic after casting. Rejects empty and multi-item sequences. func coerceArgToDoubleRequired(ctx context.Context, seq xdm.Sequence) (float64, error) { - a, err := extractSingleAtomicArg(seq, "xs:double") + a, err := extractSingleAtomicArg(seq, xdm.TypeDouble) if err != nil { if xpErr, ok := errors.AsType[*xdm.Error](err); ok { switch { - case strings.Contains(xpErr.Message, "empty sequence"): + case strings.Contains(xpErr.Message, emptySequenceMessageMarker): return 0, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "expected xs:double, got empty sequence"} - case strings.Contains(xpErr.Message, "length > 1"): + case strings.Contains(xpErr.Message, multiItemMessageMarker): return 0, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "expected xs:double, got sequence of length > 1"} } } @@ -657,7 +710,7 @@ func extractSingleAtomicArg(seq xdm.Sequence, fnName string) (xdm.AtomicValue, e switch xdm.SequenceLen(seq) { case 0: return xdm.AtomicValue{}, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: fnName + ": expected single atomic value, got empty sequence"} - case 1: + case singletonFunctionArgument: return AtomizeItem(seq.Get(0)) default: return xdm.AtomicValue{}, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: fnName + ": expected single atomic value, got sequence of length > 1"} @@ -691,13 +744,13 @@ func coerceToInteger(ctx context.Context, a xdm.AtomicValue) (xdm.AtomicValue, e func lookupFunctions(name string) (*builtinFunc, bool) { switch name { case fnNameBoolean: - return &builtinFunc{name: name, min: 1, max: 1, fn: fnBoolean}, true + return &builtinFunc{name: name, min: predicateFunctionArity, max: predicateFunctionArity, fn: fnBoolean}, true case fnNameNot: - return &builtinFunc{name: name, min: 1, max: 1, fn: fnNot}, true + return &builtinFunc{name: name, min: predicateFunctionArity, max: predicateFunctionArity, fn: fnNot}, true case lexicon.ValueTrue: - return &builtinFunc{name: name, min: 0, max: 0, fn: fnTrue}, true + return &builtinFunc{name: name, min: constantBooleanArity, max: constantBooleanArity, fn: fnTrue}, true case lexicon.ValueFalse: - return &builtinFunc{name: name, min: 0, max: 0, fn: fnFalse}, true + return &builtinFunc{name: name, min: constantBooleanArity, max: constantBooleanArity, fn: fnFalse}, true } return nil, false @@ -731,10 +784,10 @@ func fnFalse(_ context.Context, _ []xdm.Sequence) (xdm.Sequence, error) { func lookupFunctions2(name string) (*builtinFunc, bool) { switch name { - case lexicon.TypeError: - return &builtinFunc{name: name, min: 0, max: 3, fn: fnError}, true - case "trace": - return &builtinFunc{name: name, min: 1, max: 2, fn: fnTrace}, true + case fnNameError: + return &builtinFunc{name: name, min: fnErrorMinArity, max: fnErrorMaxArity, fn: fnError}, true + case fnNameTrace: + return &builtinFunc{name: name, min: fnTraceMinArity, max: fnTraceMaxArity, fn: fnTrace}, true } return nil, false @@ -742,7 +795,7 @@ func lookupFunctions2(name string) (*builtinFunc, bool) { func fnError(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { code := xdm.QNameValue{Prefix: lexicon.PrefixErr, URI: NSErr, Local: errCodeFOER0000} - msg := "error() called" + msg := defaultErrorDescription if len(args) > 0 { qv, hasCode, err := coerceErrorCode(args[0]) @@ -776,7 +829,7 @@ func coerceErrorCode(seq xdm.Sequence) (xdm.QNameValue, bool, error) { switch xdm.SequenceLen(seq) { case 0: return xdm.QNameValue{}, false, nil - case 1: + case singletonFunctionArgument: default: return xdm.QNameValue{}, false, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "fn:error code argument must be xs:QName?"} } @@ -818,9 +871,9 @@ func fnTrace(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { } } - prefix := "[trace] " + prefix := traceOutputPrefix if label != "" { - prefix += label + ": " + prefix += label + traceLabelSeparator } if err := write(prefix); err != nil { @@ -828,19 +881,19 @@ func fnTrace(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { } for i := range xdm.SequenceLen(args[0]) { - if err := Charge(ctx, 1); err != nil { + if err := Charge(ctx, traceItemWork); err != nil { return nil, err } if i > 0 { - if err := write(", "); err != nil { + if err := write(traceItemSeparator); err != nil { return nil, err } } item := args[0].Get(i) - value := fmt.Sprintf("<%T>", item) + value := fmt.Sprintf(traceOpaqueItemFormat, item) if atomic, err := AtomizeItem(item); err == nil { value, err = atomicToString(atomic) if err != nil { @@ -853,7 +906,7 @@ func fnTrace(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { } } - if err := write("\n"); err != nil { + if err := write(traceOutputTerminator); err != nil { return nil, err } diff --git a/transform/internal/xpath/functions_aggregate.go b/transform/internal/xpath/functions_aggregate.go index 6df6127..ef11fba 100644 --- a/transform/internal/xpath/functions_aggregate.go +++ b/transform/internal/xpath/functions_aggregate.go @@ -15,20 +15,49 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) +// Aggregate dispatch spellings and max/min selection modes belong to F&O. +// Numeric ranks order promotion families, rather than byte or IEEE bit widths. +const ( + fnNameAvg = "avg" + fnNameMax = "max" + fnNameMin = "min" + fnNameDistinctValues = "distinct-values" + aggregateSelectMaximum = true + aggregateSelectMinimum = false + aggregateIntegerRank = 1 + aggregateDecimalRank = 2 + aggregateFloatRank = 3 + aggregateDoubleRank = 4 + aggregateSingletonAtoms = 1 + aggregateCollationArgumentIndex = 1 + aggregateWorkBatch = 64 // logical item charges per average/sum batch +) + +// Distinct-value keys separate comparison domains within an evaluation. IEEE +// bit patterns use hexadecimal text; these prefixes are internal key framing. +const ( + distinctStringKeyPrefix = "s:" + distinctBooleanKeyPrefix = "b:" + distinctExactNumericKeyPrefix = "n:" + distinctFloatKeyPrefix = "f:" + distinctDoubleKeyPrefix = "d:" + distinctFloatingKeyRadix = 16 +) + func lookupFunctionsAggregate(name string) (*builtinFunc, bool) { switch name { case fnNameCount: - return &builtinFunc{name: name, min: 1, max: 1, fn: fnCount}, true - case "avg": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnAvg}, true - case "max": - return &builtinFunc{name: name, min: 1, max: 2, fn: fnMax}, true - case "min": - return &builtinFunc{name: name, min: 1, max: 2, fn: fnMin}, true + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnCount}, true + case fnNameAvg: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnAvg}, true + case fnNameMax: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityTwo, fn: fnMax}, true + case fnNameMin: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityTwo, fn: fnMin}, true case nameSum: - return &builtinFunc{name: name, min: 1, max: 2, fn: fnSum}, true - case "distinct-values": - return &builtinFunc{name: name, min: 1, max: 2, fn: fnDistinctValues}, true + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityTwo, fn: fnSum}, true + case fnNameDistinctValues: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityTwo, fn: fnDistinctValues}, true } return nil, false @@ -144,8 +173,8 @@ func fnAvg(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { for i, a := range atoms { // Charge bounded batches before work; exact arithmetic below also // checks cancellation at every math/big operation. - if i%64 == 0 { - if err := Charge(ctx, min(64, len(atoms)-i)); err != nil { + if i%aggregateWorkBatch == 0 { + if err := Charge(ctx, min(aggregateWorkBatch, len(atoms)-i)); err != nil { return nil, err } } @@ -270,7 +299,7 @@ func avgDurations(ctx context.Context, seq xdm.Sequence, family string) (xdm.Seq for item := range xdm.SequenceItems(seq) { a, _ := AtomizeItem(item) - totalSecs.Add(totalSecs, xdm.DurationToRat(a.DurationVal(), false)) + totalSecs.Add(totalSecs, xdm.DurationToRat(a.DurationVal(), durationSecondsComponent)) } avgSecs, err := finiteDecimalValue(ctx, new(big.Rat).Quo(totalSecs, new(big.Rat).SetInt64(int64(count)))) @@ -287,10 +316,10 @@ func avgDurations(ctx context.Context, seq xdm.Sequence, family string) (xdm.Seq secs, frac := durationFromRatSeconds(absSecs) - return xdm.SingleAtomic(xdm.AtomicValue{ + return boundedDurationResult(ctx, xdm.AtomicValue{ TypeName: xdm.TypeDayTimeDuration, Value: xdm.Duration{Seconds: secs, FracSec: frac, SecRat: absSecs, Negative: negative}, - }), nil + }) } totalMonths := new(big.Int) @@ -332,10 +361,10 @@ func avgDurations(ctx context.Context, seq xdm.Sequence, family string) (xdm.Seq return nil, &xdm.Error{Code: errCodeFODT0002, Message: textDurationOverflow} } - return xdm.SingleAtomic(xdm.AtomicValue{ + return boundedDurationResult(ctx, xdm.AtomicValue{ TypeName: xdm.TypeYearMonthDuration, Value: xdm.Duration{Months: int(absInt.Int64()), Negative: negative}, - }), nil + }) } // promoteForAggregate promotes an atomic value for aggregate operations. @@ -389,18 +418,18 @@ func promoteForAggregate(ctx context.Context, a xdm.AtomicValue) (xdm.AtomicValu func numericTypeWidth(typeName string) int { switch typeName { case xdm.TypeDouble: - return 4 + return aggregateDoubleRank case xdm.TypeFloat: - return 3 + return aggregateFloatRank case xdm.TypeDecimal: - return 2 + return aggregateDecimalRank default: - return 1 + return aggregateIntegerRank } } func fnMax(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { - return maxMin(ctx, args, true) + return maxMin(ctx, args, aggregateSelectMaximum) } func maxMin(ctx context.Context, args []xdm.Sequence, isMax bool) (xdm.Sequence, error) { @@ -413,7 +442,7 @@ func maxMin(ctx context.Context, args []xdm.Sequence, isMax bool) (xdm.Sequence, return nil, nil } - coll, err := getCollation(ctx, args, 1) + coll, err := getCollation(ctx, args, aggregateCollationArgumentIndex) if err != nil { return nil, err } @@ -426,16 +455,16 @@ func maxMin(ctx context.Context, args []xdm.Sequence, isMax bool) (xdm.Sequence, } func fnMin(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { - return maxMin(ctx, args, false) + return maxMin(ctx, args, aggregateSelectMinimum) } func maxMinCommon(ctx context.Context, atoms []xdm.AtomicValue, isMax bool, coll *collationImpl) (xdm.Sequence, error) { - fnName := "fn:min" + fnName := prefixFn + ":" + fnNameMin if isMax { - fnName = "fn:max" + fnName = prefixFn + ":" + fnNameMax } - if len(atoms) == 1 { + if len(atoms) == aggregateSingletonAtoms { return maxMinSingle(ctx, atoms[0], fnName) } @@ -581,7 +610,7 @@ func maxMinTypeFamily(ctx context.Context, value xdm.AtomicValue, fnName string) family := aggregateTypeFamily(value) legacyBinary := false - if ec := getFnContext(ctx); ec != nil && ec.version != 0 && ec.version < 31 { + if ec := getFnContext(ctx); ec != nil && ec.version != xpathVersionDefault && ec.version < xpathVersion31 { legacyBinary = family == lexicon.TypeBase64Binary || family == lexicon.TypeHexBinary } @@ -639,7 +668,7 @@ func fnSum(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { return xdm.SingleInteger(0), nil } - if len(atoms) == 1 { + if len(atoms) == aggregateSingletonAtoms { return sumSingle(ctx, atoms[0]) } @@ -654,8 +683,8 @@ func fnSum(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { } for i, value := range atoms { - if i%64 == 0 { - if err := Charge(ctx, min(64, len(atoms)-i)); err != nil { + if i%aggregateWorkBatch == 0 { + if err := Charge(ctx, min(aggregateWorkBatch, len(atoms)-i)); err != nil { return nil, err } } @@ -671,7 +700,7 @@ func fnSum(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { atomSeq[i] = atom } - return sumDurations(atomSeq, sum.family) + return sumDurations(ctx, atomSeq, sum.family) } return sum.numericResult(), nil @@ -856,7 +885,7 @@ func checkAggregateHomogeneity(family, newFamily string) (string, error) { } } -func sumDurations(seq xdm.Sequence, family string) (xdm.Sequence, error) { +func sumDurations(ctx context.Context, seq xdm.Sequence, family string) (xdm.Sequence, error) { if family == xdm.FamilyDurationDT { // Accumulate dayTime seconds EXACTLY so large totals (beyond float64's // 2^53 range) and fractional seconds canonicalize precisely. @@ -864,7 +893,7 @@ func sumDurations(seq xdm.Sequence, family string) (xdm.Sequence, error) { for item := range xdm.SequenceItems(seq) { a, _ := AtomizeItem(item) - totalSecs.Add(totalSecs, xdm.DurationToRat(a.DurationVal(), false)) + totalSecs.Add(totalSecs, xdm.DurationToRat(a.DurationVal(), durationSecondsComponent)) } negative := totalSecs.Sign() < 0 @@ -876,10 +905,10 @@ func sumDurations(seq xdm.Sequence, family string) (xdm.Sequence, error) { secs, frac := durationFromRatSeconds(absSecs) - return xdm.SingleAtomic(xdm.AtomicValue{ + return boundedDurationResult(ctx, xdm.AtomicValue{ TypeName: xdm.TypeDayTimeDuration, Value: xdm.Duration{Seconds: secs, FracSec: frac, SecRat: absSecs, Negative: negative}, - }), nil + }) } // Accumulate months via big.Int so a total near the int limit does not wrap @@ -909,10 +938,10 @@ func sumDurations(seq xdm.Sequence, family string) (xdm.Sequence, error) { return nil, &xdm.Error{Code: errCodeFODT0002, Message: "yearMonthDuration sum overflow"} } - return xdm.SingleAtomic(xdm.AtomicValue{ + return boundedDurationResult(ctx, xdm.AtomicValue{ TypeName: xdm.TypeYearMonthDuration, Value: xdm.Duration{Months: int(absMonths.Int64()), Negative: negative}, - }), nil + }) } func fnDistinctValues(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { @@ -920,11 +949,11 @@ func fnDistinctValues(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, e return nil, nil } - if err := validateCollationArg(ctx, args, 1); err != nil { + if err := validateCollationArg(ctx, args, aggregateCollationArgumentIndex); err != nil { return nil, err } - coll, err := getCollation(ctx, args, 1) + coll, err := getCollation(ctx, args, aggregateCollationArgumentIndex) if err != nil { return nil, err } @@ -1007,7 +1036,7 @@ func (s *distinctValueAccumulator) add(value xdm.AtomicValue) error { func (s *distinctValueAccumulator) addFast(value xdm.AtomicValue, group distinctGroup, key string) error { // Collation-equal strings must share their bucket. if s.coll != nil && group == distinctGroupString { - key = "s:" + string(s.coll.key(value.StringVal())) + key = distinctStringKeyPrefix + string(s.coll.key(value.StringVal())) } if _, exists := s.seenFast[key]; exists { @@ -1067,16 +1096,16 @@ func distinctValueFastKey(a xdm.AtomicValue) (distinctGroup, string, bool) { et := distinctEffectiveType(a) switch { case xdm.IsStringDerived(et): - return distinctGroupString, "s:" + a.StringVal(), true + return distinctGroupString, distinctStringKeyPrefix + a.StringVal(), true case et == xdm.TypeAnyURI: - return distinctGroupString, "s:" + stringFromAtomic(a), true + return distinctGroupString, distinctStringKeyPrefix + stringFromAtomic(a), true case et == xdm.TypeBoolean: - return distinctGroupBoolean, "b:" + strconv.FormatBool(a.BooleanVal()), true + return distinctGroupBoolean, distinctBooleanKeyPrefix + strconv.FormatBool(a.BooleanVal()), true case xdm.IsIntegerDerived(et) || et == xdm.TypeDecimal: // Promote via BaseType so a schema-derived integer/decimal (custom TypeName, // built-in BaseType, backed by int64/*big.Int/*big.Rat) keys on its effective // value. The un-promoted toRatForCompare would produce 0. - return distinctGroupDecimalInt, "n:" + toRatForCompare(xdm.PromoteSchemaType(a)).RatString(), true + return distinctGroupDecimalInt, distinctExactNumericKeyPrefix + toRatForCompare(xdm.PromoteSchemaType(a)).RatString(), true case et == xdm.TypeFloat: // Promote via BaseType so a schema-derived float (custom TypeName, built-in // BaseType, backed by float64/float32/*FloatValue) keys on its effective @@ -1086,14 +1115,14 @@ func distinctValueFastKey(a xdm.AtomicValue) (distinctGroup, string, bool) { f = 0 } - return distinctGroupFloat, "f:" + strconv.FormatUint(uint64(math.Float32bits(f)), 16), true + return distinctGroupFloat, distinctFloatKeyPrefix + strconv.FormatUint(uint64(math.Float32bits(f)), distinctFloatingKeyRadix), true case et == xdm.TypeDouble: f := xdm.PromoteSchemaType(a).ToFloat64() if f == 0 { f = 0 } - return distinctGroupDouble, "d:" + strconv.FormatUint(math.Float64bits(f), 16), true + return distinctGroupDouble, distinctDoubleKeyPrefix + strconv.FormatUint(math.Float64bits(f), distinctFloatingKeyRadix), true default: return distinctGroupUnknown, "", false } diff --git a/transform/internal/xpath/functions_array.go b/transform/internal/xpath/functions_array.go index 5979b64..1c14d15 100644 --- a/transform/internal/xpath/functions_array.go +++ b/transform/internal/xpath/functions_array.go @@ -12,53 +12,81 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) +// These local names are in the array function namespace; extension=true +// keeps flat-map static-call-only under the existing dynamic-lookup policy. +const ( + fnNameArraySize = "size" + fnNameArrayGet = "get" + fnNameArrayPut = "put" + fnNameArrayAppend = "append" + fnNameArraySubarray = "subarray" + fnNameArrayInsertBefore = "insert-before" + fnNameArrayJoin = "join" + fnNameArrayFlatten = "flatten" + fnNameArrayFlatMap = "flat-map" + staticOnlyArrayExtension = true +) + +// Array member positions are one-based; zero requests a new member during +// update preflight. Argument/index/predicate cardinalities and callback arity +// are distinct contracts, not materialization limits. +const ( + arrayFirstMemberPosition = 1 + arrayTailStartPosition = 2 + arrayNewMemberPosition = 0 + arrayArgumentCardinality = 1 + arrayIndexCardinality = 1 + arrayPredicateResultCardinality = 1 + arrayPairCallbackArity = 2 +) + func lookupFunctionsArray(name string) (*builtinFunc, bool) { switch name { - case "size": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnArraySize}, true - case "get": - return &builtinFunc{name: name, min: 2, max: 2, fn: fnArrayGet}, true - case "put": - return &builtinFunc{name: name, min: 3, max: 3, fn: fnArrayPut}, true - case "append": - return &builtinFunc{name: name, min: 2, max: 2, fn: fnArrayAppend}, true - case "subarray": - return &builtinFunc{name: name, min: 2, max: 3, fn: fnArraySubarray}, true + case fnNameArraySize: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnArraySize}, true + case fnNameArrayGet: + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityTwo, fn: fnArrayGet}, true + case fnNameArrayPut: + return &builtinFunc{name: name, min: builtinArityThree, max: builtinArityThree, fn: fnArrayPut}, true + case fnNameArrayAppend: + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityTwo, fn: fnArrayAppend}, true + case fnNameArraySubarray: + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityThree, fn: fnArraySubarray}, true case nameRemove: - return &builtinFunc{name: name, min: 2, max: 2, fn: fnArrayRemove}, true - case "insert-before": - return &builtinFunc{name: name, min: 3, max: 3, fn: fnArrayInsertBefore}, true + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityTwo, fn: fnArrayRemove}, true + case fnNameArrayInsertBefore: + return &builtinFunc{name: name, min: builtinArityThree, max: builtinArityThree, fn: fnArrayInsertBefore}, true case fnNameHead: - return &builtinFunc{name: name, min: 1, max: 1, fn: fnArrayHead}, true + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnArrayHead}, true case nameTail: - return &builtinFunc{name: name, min: 1, max: 1, fn: fnArrayTail}, true + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnArrayTail}, true case nameReverse: - return &builtinFunc{name: name, min: 1, max: 1, fn: fnArrayReverse}, true - case "join": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnArrayJoin}, true - case "flatten": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnArrayFlatten}, true - case "flat-map": - return &builtinFunc{name: name, min: 2, max: 2, fn: fnArrayFlatMap, extension: true}, true + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnArrayReverse}, true + case fnNameArrayJoin: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnArrayJoin}, true + case fnNameArrayFlatten: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnArrayFlatten}, true + case fnNameArrayFlatMap: + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityTwo, fn: fnArrayFlatMap, extension: staticOnlyArrayExtension}, true case nameFilter: - return &builtinFunc{name: name, min: 2, max: 2, fn: fnArrayFilter}, true + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityTwo, fn: fnArrayFilter}, true case nameFoldLeft: - return &builtinFunc{name: name, min: 3, max: 3, fn: fnArrayFoldLeft}, true + return &builtinFunc{name: name, min: builtinArityThree, max: builtinArityThree, fn: fnArrayFoldLeft}, true case nameFoldRight: - return &builtinFunc{name: name, min: 3, max: 3, fn: fnArrayFoldRight}, true + return &builtinFunc{name: name, min: builtinArityThree, max: builtinArityThree, fn: fnArrayFoldRight}, true case nameForEach: - return &builtinFunc{name: name, min: 2, max: 2, fn: fnArrayForEach}, true + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityTwo, fn: fnArrayForEach}, true case nameForEachPair: - return &builtinFunc{name: name, min: 3, max: 3, fn: fnArrayForEachPair}, true + return &builtinFunc{name: name, min: builtinArityThree, max: builtinArityThree, fn: fnArrayForEachPair}, true case nameSort: - return &builtinFunc{name: name, min: 1, max: 3, fn: fnArraySort}, true + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityThree, fn: fnArraySort}, true } return nil, false } func extractArray(seq xdm.Sequence) (xdm.ArrayItem, error) { - if xdm.SequenceLen(seq) != 1 { + if xdm.SequenceLen(seq) != arrayArgumentCardinality { return xdm.ArrayItem{}, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "expected single array"} } @@ -100,7 +128,7 @@ func extractArrayIndex(seq xdm.Sequence) (int, error) { } func extractArrayInteger(seq xdm.Sequence, negativeOverflowCode string) (int, error) { - if xdm.SequenceLen(seq) != 1 { + if xdm.SequenceLen(seq) != arrayIndexCardinality { return 0, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "array index must be a single xs:integer"} } @@ -137,7 +165,7 @@ func extractArrayInteger(seq xdm.Sequence, negativeOverflowCode string) (int, er return int(iv), nil } -func fnArrayPut(_ context.Context, args []xdm.Sequence) (xdm.Sequence, error) { +func fnArrayPut(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { a, err := extractArray(args[0]) if err != nil { return nil, err @@ -148,6 +176,15 @@ func fnArrayPut(_ context.Context, args []xdm.Sequence) (xdm.Sequence, error) { return nil, err } + // Preserve the index error before checking the replacement's resource cost. + if _, err := a.GetView(idx); err != nil { + return nil, err + } + + if err := preflightArrayUpdate(ctx, a, args[2], idx); err != nil { + return nil, err + } + result, err := a.Put(idx, args[2]) if err != nil { return nil, err @@ -156,15 +193,58 @@ func fnArrayPut(_ context.Context, args []xdm.Sequence) (xdm.Sequence, error) { return xdm.ItemSlice{result}, nil } -func fnArrayAppend(_ context.Context, args []xdm.Sequence) (xdm.Sequence, error) { +func fnArrayAppend(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { a, err := extractArray(args[0]) if err != nil { return nil, err } + if err := preflightArrayUpdate(ctx, a, args[1], arrayNewMemberPosition); err != nil { + return nil, err + } + return xdm.ItemSlice{a.Append(args[1])}, nil } +// preflightArrayUpdate applies the builder's member/item limits before copying. +// replaced is a one-based member position, or zero when adding a new member. +func preflightArrayUpdate(ctx context.Context, a xdm.ArrayItem, value xdm.Sequence, replaced int) error { + ec := getFnContext(ctx) + + limit := fnMaxNodes(ec) + if a.Size() > limit || (replaced == arrayNewMemberPosition && a.Size() == limit) { + return ErrNodeSetLimit + } + + total := xdm.SequenceLen(value) + if total > limit { + return ErrNodeSetLimit + } + + if err := fnCountOps(ctx, ec, a.Size()+1); err != nil { + return err + } + + for i, member := range a.MembersView() { + if err := ctx.Err(); err != nil { + return err + } + + if i+1 == replaced { + continue + } + + length := xdm.SequenceLen(member) + if length > limit-total { + return ErrNodeSetLimit + } + + total += length + } + + return fnCountOps(ctx, ec, total) +} + func fnArraySubarray(_ context.Context, args []xdm.Sequence) (xdm.Sequence, error) { a, err := extractArray(args[0]) if err != nil { @@ -180,7 +260,7 @@ func fnArraySubarray(_ context.Context, args []xdm.Sequence) (xdm.Sequence, erro if len(args) > 2 { // A negative length remains FOAY0002 even beyond the machine index // range; positive out-of-range lengths and positions use FOAY0001. - length, err = extractArrayInteger(args[2], "FOAY0002") + length, err = extractArrayInteger(args[2], lexicon.ErrFOAY0002) if err != nil { return nil, err } @@ -219,7 +299,7 @@ func fnArrayRemove(_ context.Context, args []xdm.Sequence) (xdm.Sequence, error) } // Range-check on int64 before converting so an out-of-int value cannot // wrap into a valid-looking position (e.g. on 32-bit platforms). - if iv < 1 || iv > int64(size) { + if iv < arrayFirstMemberPosition || iv > int64(size) { return nil, &xdm.Error{Code: lexicon.ErrFOAY0001, Message: fmt.Sprintf("array:remove: position %d out of range 1..%d", iv, size)} } @@ -231,7 +311,7 @@ func fnArrayRemove(_ context.Context, args []xdm.Sequence) (xdm.Sequence, error) var result []xdm.Sequence for i, m := range members { - if _, skip := positions[i+1]; !skip { + if _, skip := positions[i+arrayFirstMemberPosition]; !skip { result = append(result, m) } } @@ -239,7 +319,7 @@ func fnArrayRemove(_ context.Context, args []xdm.Sequence) (xdm.Sequence, error) return xdm.ItemSlice{xdm.NewArray(result)}, nil } -func fnArrayInsertBefore(_ context.Context, args []xdm.Sequence) (xdm.Sequence, error) { +func fnArrayInsertBefore(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { a, err := extractArray(args[0]) if err != nil { return nil, err @@ -251,10 +331,14 @@ func fnArrayInsertBefore(_ context.Context, args []xdm.Sequence) (xdm.Sequence, } members := a.MembersView() - if pos < 1 || pos > len(members)+1 { + if pos < arrayFirstMemberPosition || pos > len(members)+1 { return nil, &xdm.Error{Code: lexicon.ErrFOAY0001, Message: "array index out of bounds"} } + if err := preflightArrayUpdate(ctx, a, args[2], arrayNewMemberPosition); err != nil { + return nil, err + } + result := make([]xdm.Sequence, 0, len(members)+1) result = append(result, members[:pos-1]...) result = append(result, args[2]) @@ -273,7 +357,7 @@ func fnArrayHead(_ context.Context, args []xdm.Sequence) (xdm.Sequence, error) { return nil, &xdm.Error{Code: lexicon.ErrFOAY0001, Message: "array:head on empty array"} } - return a.Get(1) + return a.Get(arrayFirstMemberPosition) } func fnArrayTail(_ context.Context, args []xdm.Sequence) (xdm.Sequence, error) { @@ -286,7 +370,7 @@ func fnArrayTail(_ context.Context, args []xdm.Sequence) (xdm.Sequence, error) { return nil, &xdm.Error{Code: lexicon.ErrFOAY0001, Message: "array:tail on empty array"} } - sub, err := a.SubArray(2, a.Size()-1) + sub, err := a.SubArray(arrayTailStartPosition, a.Size()-1) if err != nil { return nil, err } @@ -571,7 +655,7 @@ func fnArrayFilter(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, erro return nil, err } // Per XPath 3.1, the callback must return exactly one xs:boolean - if xdm.SequenceLen(r) != 1 { + if xdm.SequenceLen(r) != arrayPredicateResultCardinality { return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "array:filter callback must return a single xs:boolean value"} } @@ -624,7 +708,7 @@ func fnArrayFoldLeft(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, er } // The accumulator can grow without bound across iterations; reject once // it would exceed the configured sequence/node-set limit. - if maxNodes > 0 && xdm.SequenceLen(acc) > maxNodes { + if maxNodes > accumulatingSequenceLimitDisabled && xdm.SequenceLen(acc) > maxNodes { return nil, ErrNodeSetLimit } } @@ -660,7 +744,7 @@ func fnArrayFoldRight(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, e } // The accumulator can grow without bound across iterations; reject once // it would exceed the configured sequence/node-set limit. - if maxNodes > 0 && xdm.SequenceLen(acc) > maxNodes { + if maxNodes > accumulatingSequenceLimitDisabled && xdm.SequenceLen(acc) > maxNodes { return nil, ErrNodeSetLimit } } @@ -725,7 +809,7 @@ func fnArrayForEachPair(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, return nil, err } - if fi.Arity >= 0 && fi.Arity != 2 { + if fi.Arity >= finiteArityFloor && fi.Arity != arrayPairCallbackArity { return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: fmt.Sprintf("array:for-each-pair callback must have arity 2, got %d", fi.Arity)} } @@ -739,7 +823,7 @@ func fnArrayForEachPair(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, // compare is overflow-safe (rLen > maxNodes-total). total := 0 - for i := 1; i <= size; i++ { + for i := arrayFirstMemberPosition; i <= size; i++ { if err := fnCountOp(ctx, ec); err != nil { return nil, err } diff --git a/transform/internal/xpath/functions_constructors.go b/transform/internal/xpath/functions_constructors.go index da9a8ea..dbb4164 100644 --- a/transform/internal/xpath/functions_constructors.go +++ b/transform/internal/xpath/functions_constructors.go @@ -17,6 +17,35 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm/lexical" ) +// Constructor numeric subtype boundaries are independent of scalar atom +// cardinality. Token capacity is an initial item allocation hint; token work +// counts one logical operation per validated string, with all limits unchanged. +const ( + unsignedConstructorMinimum = 0 + nonnegativeConstructorMinimum = 0 + nonpositiveConstructorMaximum = 0 + positiveConstructorMinimum = 1 + negativeConstructorMaximum = -1 + constructorAtomCardinality = 1 + initialConstructorTokenCapacity = 8 + constructorTokenWork = 1 +) + +// ASCII Gregorian lexical field offsets count bytes in ---DD/--MM/--MM-DD. +// Year four is a leap-year reference, not the minimum year lexical digit count. +const ( + constructorMaximumDay = 31 + gregorianLeapReferenceYear = 4 + constructorMaximumYearDigits = 9 + gregorianDayFieldStart = 3 + gregorianDayFieldEnd = 5 + gregorianMonthFieldStart = 2 + gregorianMonthFieldEnd = 4 + gregorianMonthDayFieldStart = 5 + gregorianMonthDayFieldEnd = 7 + gregorianYearMonthFieldEnd = 3 // delimiter byte plus two month digits +) + func lookupConstructor(requestedName string) (*builtinFunc, bool) { // Base XSD types — delegate to CastAtomic for _, entry := range []struct { @@ -24,30 +53,30 @@ func lookupConstructor(requestedName string) (*builtinFunc, bool) { targetType string }{ {lexicon.TypeString, xdm.TypeString}, - {fnNameBoolean, xdm.TypeBoolean}, + {lexicon.TypeBoolean, xdm.TypeBoolean}, {lexicon.TypeDecimal, xdm.TypeDecimal}, - {"double", xdm.TypeDouble}, - {"float", xdm.TypeFloat}, - {"integer", xdm.TypeInteger}, + {lexicon.TypeDouble, xdm.TypeDouble}, + {lexicon.TypeFloat, xdm.TypeFloat}, + {lexicon.TypeInteger, xdm.TypeInteger}, {lexicon.TypeDate, xdm.TypeDate}, {lexicon.TypeDateTime, xdm.TypeDateTime}, {lexicon.TypeTime, xdm.TypeTime}, {lexicon.TypeDuration, xdm.TypeDuration}, {lexicon.TypeDayTimeDuration, xdm.TypeDayTimeDuration}, {lexicon.TypeYearMonthDuration, xdm.TypeYearMonthDuration}, - {"anyURI", xdm.TypeAnyURI}, + {lexicon.TypeAnyURI, xdm.TypeAnyURI}, {lexicon.TypeBase64Binary, xdm.TypeBase64Binary}, {lexicon.TypeHexBinary, xdm.TypeHexBinary}, {lexicon.TypeUntypedAtomic, xdm.TypeUntypedAtomic}, } { if requestedName == entry.name { - return &builtinFunc{name: requestedName, min: 1, max: 1, fn: makeXSConstructor(entry.targetType)}, true + return &builtinFunc{name: requestedName, min: builtinArityOne, max: builtinArityOne, fn: makeXSConstructor(entry.targetType)}, true } } // xs:QName constructor needs namespace context to resolve prefixes if requestedName == lexicon.TypeQName { - return &builtinFunc{name: requestedName, min: 1, max: 1, fn: makeXSQNameConstructor()}, true + return &builtinFunc{name: requestedName, min: builtinArityOne, max: builtinArityOne, fn: makeXSQNameConstructor()}, true } // Derived integer types — cast to integer, then validate range @@ -57,38 +86,38 @@ func lookupConstructor(requestedName string) (*builtinFunc, bool) { min int64 max int64 }{ - {"long", xdm.TypeLong, math.MinInt64, math.MaxInt64}, - {"int", xdm.TypeInt, -2147483648, 2147483647}, - {"short", xdm.TypeShort, -32768, 32767}, - {"byte", xdm.TypeByte, -128, 127}, - {"unsignedInt", xdm.TypeUnsignedInt, 0, 4294967295}, - {"unsignedShort", xdm.TypeUnsignedShort, 0, 65535}, - {"unsignedByte", xdm.TypeUnsignedByte, 0, 255}, + {lexicon.TypeLong, xdm.TypeLong, math.MinInt64, math.MaxInt64}, + {lexicon.TypeInt, xdm.TypeInt, math.MinInt32, math.MaxInt32}, + {lexicon.TypeShort, xdm.TypeShort, math.MinInt16, math.MaxInt16}, + {lexicon.TypeByte, xdm.TypeByte, math.MinInt8, math.MaxInt8}, + {lexicon.TypeUnsignedInt, xdm.TypeUnsignedInt, unsignedConstructorMinimum, math.MaxUint32}, + {lexicon.TypeUnsignedShort, xdm.TypeUnsignedShort, unsignedConstructorMinimum, math.MaxUint16}, + {lexicon.TypeUnsignedByte, xdm.TypeUnsignedByte, unsignedConstructorMinimum, math.MaxUint8}, } { if requestedName == entry.name { - return &builtinFunc{name: requestedName, min: 1, max: 1, fn: makeXSIntegerRange(entry.typeName, entry.min, entry.max)}, true + return &builtinFunc{name: requestedName, min: builtinArityOne, max: builtinArityOne, fn: makeXSIntegerRange(entry.typeName, entry.min, entry.max)}, true } } // Unbounded integer subtypes need big.Int constructor (no fixed max/min) - if requestedName == "nonNegativeInteger" { - return &builtinFunc{name: requestedName, min: 1, max: 1, fn: makeXSIntegerRangeBig(xdm.TypeNonNegativeInteger, big.NewInt(0), nil)}, true + if requestedName == lexicon.TypeNonNegativeInteger { + return &builtinFunc{name: requestedName, min: builtinArityOne, max: builtinArityOne, fn: makeXSIntegerRangeBig(xdm.TypeNonNegativeInteger, big.NewInt(nonnegativeConstructorMinimum), nil)}, true } - if requestedName == "nonPositiveInteger" { - return &builtinFunc{name: requestedName, min: 1, max: 1, fn: makeXSIntegerRangeBig(xdm.TypeNonPositiveInteger, nil, big.NewInt(0))}, true + if requestedName == lexicon.TypeNonPositiveInteger { + return &builtinFunc{name: requestedName, min: builtinArityOne, max: builtinArityOne, fn: makeXSIntegerRangeBig(xdm.TypeNonPositiveInteger, nil, big.NewInt(nonpositiveConstructorMaximum))}, true } - if requestedName == "positiveInteger" { - return &builtinFunc{name: requestedName, min: 1, max: 1, fn: makeXSIntegerRangeBig(xdm.TypePositiveInteger, big.NewInt(1), nil)}, true + if requestedName == lexicon.TypePositiveInteger { + return &builtinFunc{name: requestedName, min: builtinArityOne, max: builtinArityOne, fn: makeXSIntegerRangeBig(xdm.TypePositiveInteger, big.NewInt(positiveConstructorMinimum), nil)}, true } - if requestedName == "negativeInteger" { - return &builtinFunc{name: requestedName, min: 1, max: 1, fn: makeXSIntegerRangeBig(xdm.TypeNegativeInteger, nil, big.NewInt(-1))}, true + if requestedName == lexicon.TypeNegativeInteger { + return &builtinFunc{name: requestedName, min: builtinArityOne, max: builtinArityOne, fn: makeXSIntegerRangeBig(xdm.TypeNegativeInteger, nil, big.NewInt(negativeConstructorMaximum))}, true } // xs:unsignedLong needs big.Int max since MaxUint64 exceeds int64 - if requestedName == "unsignedLong" { - return &builtinFunc{name: requestedName, min: 1, max: 1, fn: makeXSIntegerRangeBig(xdm.TypeUnsignedLong, big.NewInt(0), new(big.Int).SetUint64(math.MaxUint64))}, true + if requestedName == lexicon.TypeUnsignedLong { + return &builtinFunc{name: requestedName, min: builtinArityOne, max: builtinArityOne, fn: makeXSIntegerRangeBig(xdm.TypeUnsignedLong, big.NewInt(unsignedConstructorMinimum), new(big.Int).SetUint64(math.MaxUint64))}, true } // Derived string types — cast to string, validate, store with derived type name @@ -97,32 +126,32 @@ func lookupConstructor(requestedName string) (*builtinFunc, bool) { typeName string re lexicalCheck // nil = no validation, just string cast }{ - {"normalizedString", xdm.TypeNormalizedString, nil}, - {"token", xdm.TypeToken, nil}, - {"language", xdm.TypeLanguage, reLang}, + {lexicon.TypeNormalizedString, xdm.TypeNormalizedString, nil}, + {lexicon.TypeToken, xdm.TypeToken, nil}, + {lexicon.TypeLanguage, xdm.TypeLanguage, reLang}, {lexicon.TypeName, xdm.TypeName, reName}, - {"NCName", xdm.TypeNCName, reNCName}, + {lexicon.TypeNCName, xdm.TypeNCName, reNCName}, {lexicon.TypeNMToken, xdm.TypeNMTOKEN, reNMTOKEN}, {lexicon.TypeENTITY, xdm.TypeENTITY, reNCName}, - {"ID", xdm.TypeID, reNCName}, - {"IDREF", xdm.TypeIDREF, reNCName}, + {lexicon.TypeID, xdm.TypeID, reNCName}, + {lexicon.TypeIDREF, xdm.TypeIDREF, reNCName}, } { if requestedName == entry.name { - return &builtinFunc{name: requestedName, min: 1, max: 1, fn: makeXSStringRestriction(entry.typeName, entry.re)}, true + return &builtinFunc{name: requestedName, min: builtinArityOne, max: builtinArityOne, fn: makeXSStringRestriction(entry.typeName, entry.re)}, true } } // List types — split by whitespace, produce sequence of item type values if requestedName == lexicon.TypeNMTokens { - return &builtinFunc{name: requestedName, min: 1, max: 1, fn: makeXSTokenList(xdm.TypeNMTOKENS, xdm.TypeNMTOKEN, reNMTOKEN)}, true + return &builtinFunc{name: requestedName, min: builtinArityOne, max: builtinArityOne, fn: makeXSTokenList(xdm.TypeNMTOKENS, xdm.TypeNMTOKEN, reNMTOKEN)}, true } if requestedName == lexicon.TypeIDREFS { - return &builtinFunc{name: requestedName, min: 1, max: 1, fn: makeXSTokenList(xdm.TypeIDREFS, xdm.TypeIDREF, reNCName)}, true + return &builtinFunc{name: requestedName, min: builtinArityOne, max: builtinArityOne, fn: makeXSTokenList(xdm.TypeIDREFS, xdm.TypeIDREF, reNCName)}, true } if requestedName == lexicon.TypeENTITIES { - return &builtinFunc{name: requestedName, min: 1, max: 1, fn: makeXSTokenList(xdm.TypeENTITIES, xdm.TypeENTITY, reNCName)}, true + return &builtinFunc{name: requestedName, min: builtinArityOne, max: builtinArityOne, fn: makeXSTokenList(xdm.TypeENTITIES, xdm.TypeENTITY, reNCName)}, true } // Gregorian date part types. CastAtomic/CastFromString perform the @@ -138,24 +167,24 @@ func lookupConstructor(requestedName string) (*builtinFunc, bool) { {lexicon.TypeGYearMonth, xdm.TypeGYearMonth}, } { if requestedName == entry.name { - return &builtinFunc{name: requestedName, min: 1, max: 1, fn: makeXSConstructor(entry.typeName)}, true + return &builtinFunc{name: requestedName, min: builtinArityOne, max: builtinArityOne, fn: makeXSConstructor(entry.typeName)}, true } } // xs:dateTimeStamp — dateTime with required timezone. CastAtomic enforces // the mandatory-timezone rule, so the generic constructor suffices. if requestedName == lexicon.TypeDateTimeStamp { - return &builtinFunc{name: requestedName, min: 1, max: 1, fn: makeXSConstructor(xdm.TypeDateTimeStamp)}, true + return &builtinFunc{name: requestedName, min: builtinArityOne, max: builtinArityOne, fn: makeXSConstructor(xdm.TypeDateTimeStamp)}, true } // xs:error — always raises an error (used for type checking tests) if requestedName == lexicon.TypeError { - return &builtinFunc{name: requestedName, min: 1, max: 1, fn: fnXSError}, true + return &builtinFunc{name: requestedName, min: builtinArityOne, max: builtinArityOne, fn: fnXSError}, true } // xs:numeric preserves numeric members and otherwise selects xs:double. if requestedName == lexicon.TypeNumeric { - return &builtinFunc{name: requestedName, min: 1, max: 1, fn: makeXSConstructor(xdm.TypeNumeric)}, true + return &builtinFunc{name: requestedName, min: builtinArityOne, max: builtinArityOne, fn: makeXSConstructor(xdm.TypeNumeric)}, true } return nil, false @@ -312,7 +341,7 @@ func makeXSTokenList(listType, itemType string, tokenRe lexicalCheck) func(conte ec := getFnContext(ctx) limit := NodeLimit(ctx, fnMaxNodes(ec)) - result := make(xdm.ItemSlice, 0, min(limit, 8)) + result := make(xdm.ItemSlice, 0, min(limit, initialConstructorTokenCapacity)) for token := range strings.FieldsFuncSeq(s, xmlchar.IsSpace) { if len(result) >= limit { @@ -320,10 +349,10 @@ func makeXSTokenList(listType, itemType string, tokenRe lexicalCheck) func(conte } if ec != nil { - if err := ec.countOps(ctx, 1); err != nil { + if err := ec.countOps(ctx, constructorTokenWork); err != nil { return nil, err } - } else if err := Charge(ctx, 1); err != nil { + } else if err := Charge(ctx, constructorTokenWork); err != nil { return nil, err } @@ -344,7 +373,7 @@ func isBuiltinListType(target string) bool { // Builtin list casts and constructors use one bounded token/member builder. func castToBuiltinList(ctx context.Context, ec *evalContext, value xdm.AtomicValue, target string) (xdm.Sequence, error) { - constructor, _ := lookupConstructor(strings.TrimPrefix(target, "xs:")) + constructor, _ := lookupConstructor(strings.TrimPrefix(target, lexicon.PrefixXS+":")) if ec != nil { ctx = withFnContext(ctx, ec) @@ -384,22 +413,22 @@ func validateGregorianValue(typeName, s string) bool { case xdm.TypeGDay: // ---DD format: day must be 01-31 day := extractGDayValue(s) - return day >= 1 && day <= 31 + return day >= minimumCalendarDay && day <= constructorMaximumDay case xdm.TypeGMonth: // --MM format: month must be 01-12 month := extractGMonthValue(s) - return month >= 1 && month <= 12 + return month >= minimumCalendarMonth && month <= maximumCalendarMonth case xdm.TypeGMonthDay: // --MM-DD format: month 01-12, day valid for month month, day := extractGMonthDayValues(s) - if month < 1 || month > 12 { + if month < minimumCalendarMonth || month > maximumCalendarMonth { return false } // Use a leap year for gMonthDay since no year is specified; // February allows up to 29 because gMonthDay must accommodate leap years. - maxDay := daysInMonth(4, month) // year 4 is a leap year + maxDay := daysInMonth(gregorianLeapReferenceYear, month) // year 4 is a leap year - return day >= 1 && day <= maxDay + return day >= minimumCalendarDay && day <= maxDay case xdm.TypeGYear: // XSD 1.1: year 0000 is valid y := extractYearDigits(s) @@ -415,7 +444,7 @@ func validateGregorianValue(typeName, s string) bool { month := extractGYearMonthMonth(s) - return month >= 1 && month <= 12 + return month >= minimumCalendarMonth && month <= maximumCalendarMonth } return true @@ -424,11 +453,11 @@ func validateGregorianValue(typeName, s string) bool { // extractGDayValue extracts the day from a ---DD[tz] string. func extractGDayValue(s string) int { // format: ---DD... - if len(s) < 5 { + if len(s) < gregorianDayFieldEnd { return 0 } - d, err := strconv.Atoi(s[3:5]) + d, err := strconv.Atoi(s[gregorianDayFieldStart:gregorianDayFieldEnd]) if err != nil { return 0 } @@ -439,11 +468,11 @@ func extractGDayValue(s string) int { // extractGMonthValue extracts the month from a --MM[tz] string. func extractGMonthValue(s string) int { // format: --MM... - if len(s) < 4 { + if len(s) < gregorianMonthFieldEnd { return 0 } - m, err := strconv.Atoi(s[2:4]) + m, err := strconv.Atoi(s[gregorianMonthFieldStart:gregorianMonthFieldEnd]) if err != nil { return 0 } @@ -454,16 +483,16 @@ func extractGMonthValue(s string) int { // extractGMonthDayValues extracts month and day from a --MM-DD[tz] string. func extractGMonthDayValues(s string) (int, int) { // format: --MM-DD... - if len(s) < 7 { + if len(s) < gregorianMonthDayFieldEnd { return 0, 0 } - m, err := strconv.Atoi(s[2:4]) + m, err := strconv.Atoi(s[gregorianMonthFieldStart:gregorianMonthFieldEnd]) if err != nil { return 0, 0 } - d, err := strconv.Atoi(s[5:7]) + d, err := strconv.Atoi(s[gregorianMonthDayFieldStart:gregorianMonthDayFieldEnd]) if err != nil { return 0, 0 } @@ -486,11 +515,11 @@ func extractGYearMonthMonth(s string) int { return 0 } // work[i] == '-', month starts at i+1 - if i+3 > len(work) { + if i+gregorianYearMonthFieldEnd > len(work) { return 0 } - m, err := strconv.Atoi(work[i+1 : i+3]) + m, err := strconv.Atoi(work[i+1 : i+gregorianYearMonthFieldEnd]) if err != nil { return 0 } @@ -510,11 +539,11 @@ func extractYearDigits(s string) string { } func hasValidGregorianYearDigits(y string) bool { - if len(y) < 4 || len(y) > 9 { + if len(y) < minimumYearDigits || len(y) > constructorMaximumYearDigits { return false } - return len(y) == 4 || y[0] != '0' + return len(y) == minimumYearDigits || y[0] != '0' } func atomizeConstructorArg(seq xdm.Sequence, typeName string) (xdm.AtomicValue, bool, error) { @@ -533,7 +562,7 @@ func atomizeConstructorArg(seq xdm.Sequence, typeName string) (xdm.AtomicValue, err := atomizeStream(seq, func(av xdm.AtomicValue) (bool, error) { count++ - if count == 1 { + if count == constructorAtomCardinality { first = av } @@ -547,7 +576,7 @@ func atomizeConstructorArg(seq xdm.Sequence, typeName string) (xdm.AtomicValue, return xdm.AtomicValue{}, true, nil } - if count > 1 { + if count > constructorAtomCardinality { return xdm.AtomicValue{}, false, &xdm.Error{ Code: lexicon.ErrXPTY0004, Message: fmt.Sprintf("%s constructor requires a singleton argument", typeName), diff --git a/transform/internal/xpath/functions_datetime.go b/transform/internal/xpath/functions_datetime.go index 343c23d..f6c3eea 100644 --- a/transform/internal/xpath/functions_datetime.go +++ b/transform/internal/xpath/functions_datetime.go @@ -12,64 +12,100 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) +// Date/time function local names are separate from XSD type names. These +// namespace dispatch values preserve the exact case required by F&O. +const ( + fnNameYearFromDateTime = "year-from-dateTime" + fnNameMonthFromDateTime = "month-from-dateTime" + fnNameDayFromDateTime = "day-from-dateTime" + fnNameHoursFromDateTime = "hours-from-dateTime" + fnNameMinutesFromDateTime = "minutes-from-dateTime" + fnNameSecondsFromDateTime = "seconds-from-dateTime" + fnNameTimezoneFromDateTime = "timezone-from-dateTime" + fnNameYearFromDate = "year-from-date" + fnNameMonthFromDate = "month-from-date" + fnNameDayFromDate = "day-from-date" + fnNameTimezoneFromDate = "timezone-from-date" + fnNameHoursFromTime = "hours-from-time" + fnNameMinutesFromTime = "minutes-from-time" + fnNameSecondsFromTime = "seconds-from-time" + fnNameTimezoneFromTime = "timezone-from-time" + fnNameYearsFromDuration = "years-from-duration" + fnNameMonthsFromDuration = "months-from-duration" + fnNameDaysFromDuration = "days-from-duration" + fnNameHoursFromDuration = "hours-from-duration" + fnNameMinutesFromDuration = "minutes-from-duration" + fnNameSecondsFromDuration = "seconds-from-duration" + fnNameAdjustDateTimeToTimezone = "adjust-dateTime-to-timezone" + fnNameAdjustDateToTimezone = "adjust-date-to-timezone" + fnNameAdjustTimeToTimezone = "adjust-time-to-timezone" +) + +// The timezone ceiling is fourteen hours expressed in seconds. DurationToRat's +// false useMonths selector requests exact seconds rather than calendar months. +const ( + timezoneMaximumOffsetSeconds = maximumTimezoneHours * calendarSecondsPerHour + durationSecondsComponent = false +) + func lookupFunctionsDatetime(name string) (*builtinFunc, bool) { switch name { case lexicon.TypeDateTime: - return &builtinFunc{name: name, min: 2, max: 2, fn: fnDateTime}, true - case "year-from-dateTime": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnYearFromDateTime}, true - case "month-from-dateTime": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnMonthFromDateTime}, true - case "day-from-dateTime": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnDayFromDateTime}, true - case "hours-from-dateTime": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnHoursFromDateTime}, true - case "minutes-from-dateTime": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnMinutesFromDateTime}, true - case "seconds-from-dateTime": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnSecondsFromDateTime}, true - case "timezone-from-dateTime": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnTimezoneFromDateTime}, true - case "year-from-date": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnYearFromDate}, true - case "month-from-date": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnMonthFromDate}, true - case "day-from-date": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnDayFromDate}, true - case "timezone-from-date": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnTimezoneFromDate}, true - case "hours-from-time": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnHoursFromTime}, true - case "minutes-from-time": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnMinutesFromTime}, true - case "seconds-from-time": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnSecondsFromTime}, true - case "timezone-from-time": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnTimezoneFromTime}, true - case "years-from-duration": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnYearsFromDuration}, true - case "months-from-duration": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnMonthsFromDuration}, true - case "days-from-duration": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnDaysFromDuration}, true - case "hours-from-duration": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnHoursFromDuration}, true - case "minutes-from-duration": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnMinutesFromDuration}, true - case "seconds-from-duration": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnSecondsFromDuration}, true - case "adjust-dateTime-to-timezone": - return &builtinFunc{name: name, min: 1, max: 2, fn: fnAdjustDateTimeToTimezone}, true - case "adjust-date-to-timezone": - return &builtinFunc{name: name, min: 1, max: 2, fn: fnAdjustDateToTimezone}, true - case "adjust-time-to-timezone": - return &builtinFunc{name: name, min: 1, max: 2, fn: fnAdjustTimeToTimezone}, true + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityTwo, fn: fnDateTime}, true + case fnNameYearFromDateTime: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnYearFromDateTime}, true + case fnNameMonthFromDateTime: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnMonthFromDateTime}, true + case fnNameDayFromDateTime: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnDayFromDateTime}, true + case fnNameHoursFromDateTime: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnHoursFromDateTime}, true + case fnNameMinutesFromDateTime: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnMinutesFromDateTime}, true + case fnNameSecondsFromDateTime: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnSecondsFromDateTime}, true + case fnNameTimezoneFromDateTime: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnTimezoneFromDateTime}, true + case fnNameYearFromDate: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnYearFromDate}, true + case fnNameMonthFromDate: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnMonthFromDate}, true + case fnNameDayFromDate: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnDayFromDate}, true + case fnNameTimezoneFromDate: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnTimezoneFromDate}, true + case fnNameHoursFromTime: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnHoursFromTime}, true + case fnNameMinutesFromTime: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnMinutesFromTime}, true + case fnNameSecondsFromTime: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnSecondsFromTime}, true + case fnNameTimezoneFromTime: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnTimezoneFromTime}, true + case fnNameYearsFromDuration: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnYearsFromDuration}, true + case fnNameMonthsFromDuration: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnMonthsFromDuration}, true + case fnNameDaysFromDuration: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnDaysFromDuration}, true + case fnNameHoursFromDuration: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnHoursFromDuration}, true + case fnNameMinutesFromDuration: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnMinutesFromDuration}, true + case fnNameSecondsFromDuration: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnSecondsFromDuration}, true + case fnNameAdjustDateTimeToTimezone: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityTwo, fn: fnAdjustDateTimeToTimezone}, true + case fnNameAdjustDateToTimezone: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityTwo, fn: fnAdjustDateToTimezone}, true + case fnNameAdjustTimeToTimezone: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityTwo, fn: fnAdjustTimeToTimezone}, true case fnNameFormatDateTime: - return &builtinFunc{name: name, min: 2, max: 5, arities: []int{2, 5}, fn: fnFormatDateTime}, true + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityFive, arities: []int{builtinArityTwo, builtinArityFive}, fn: fnFormatDateTime}, true case fnNameFormatDate: - return &builtinFunc{name: name, min: 2, max: 5, arities: []int{2, 5}, fn: fnFormatDate}, true + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityFive, arities: []int{builtinArityTwo, builtinArityFive}, fn: fnFormatDate}, true case fnNameFormatTime: - return &builtinFunc{name: name, min: 2, max: 5, arities: []int{2, 5}, fn: fnFormatTime}, true + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityFive, arities: []int{builtinArityTwo, builtinArityFive}, fn: fnFormatTime}, true } return nil, false @@ -213,7 +249,7 @@ func fnDateTime(_ context.Context, args []xdm.Sequence) (xdm.Sequence, error) { _, toff := t.Zone() if doff != toff { - return nil, &xdm.Error{Code: "FORG0008", Message: "date and time timezone values are not equal"} + return nil, &xdm.Error{Code: errCodeFORG0008, Message: "date and time timezone values are not equal"} } loc = d.Location() @@ -310,8 +346,8 @@ func fnSecondsFromDateTime(_ context.Context, args []xdm.Sequence) (xdm.Sequence return xdm.SingleDecimal(secondsToRat(t)), nil } -func fnTimezoneFromDateTime(_ context.Context, args []xdm.Sequence) (xdm.Sequence, error) { - return timezoneFromTemporal(args[0], xdm.TypeDateTime) +func fnTimezoneFromDateTime(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { + return timezoneFromTemporal(ctx, args[0], xdm.TypeDateTime) } // --- date accessors --- @@ -355,8 +391,8 @@ func fnDayFromDate(_ context.Context, args []xdm.Sequence) (xdm.Sequence, error) return xdm.SingleInteger(int64(t.Day())), nil } -func fnTimezoneFromDate(_ context.Context, args []xdm.Sequence) (xdm.Sequence, error) { - return timezoneFromTemporal(args[0], xdm.TypeDate) +func fnTimezoneFromDate(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { + return timezoneFromTemporal(ctx, args[0], xdm.TypeDate) } // --- time accessors --- @@ -406,17 +442,17 @@ func fnSecondsFromTime(_ context.Context, args []xdm.Sequence) (xdm.Sequence, er func secondsToRat(t time.Time) *big.Rat { r := new(big.Rat).SetInt64(int64(t.Second())) if ns := t.Nanosecond(); ns != 0 { - r.Add(r, new(big.Rat).SetFrac(big.NewInt(int64(ns)), big.NewInt(1_000_000_000))) + r.Add(r, new(big.Rat).SetFrac(big.NewInt(int64(ns)), big.NewInt(nanosecondsPerSecond))) } return r } -func fnTimezoneFromTime(_ context.Context, args []xdm.Sequence) (xdm.Sequence, error) { - return timezoneFromTemporal(args[0], xdm.TypeTime) +func fnTimezoneFromTime(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { + return timezoneFromTemporal(ctx, args[0], xdm.TypeTime) } -func timezoneFromTemporal(arg xdm.Sequence, typeName string) (xdm.Sequence, error) { +func timezoneFromTemporal(ctx context.Context, arg xdm.Sequence, typeName string) (xdm.Sequence, error) { t, ok, err := extractTime(arg, typeName) if err != nil { return nil, err @@ -441,7 +477,7 @@ func timezoneFromTemporal(arg xdm.Sequence, typeName string) (xdm.Sequence, erro // path is exact by construction (sign lives in Negative). d := xdm.Duration{Seconds: float64(absOffset), SecRat: big.NewRat(int64(absOffset), 1), Negative: neg} - return xdm.SingleAtomic(xdm.AtomicValue{TypeName: xdm.TypeDayTimeDuration, Value: d}), nil + return boundedDurationResult(ctx, xdm.AtomicValue{TypeName: xdm.TypeDayTimeDuration, Value: d}) } // --- duration accessors --- @@ -456,7 +492,7 @@ func fnYearsFromDuration(_ context.Context, args []xdm.Sequence) (xdm.Sequence, return nil, nil } - years := d.Months / 12 + years := d.Months / calendarMonthsPerYear if d.Negative { years = -years } @@ -474,7 +510,7 @@ func fnMonthsFromDuration(_ context.Context, args []xdm.Sequence) (xdm.Sequence, return nil, nil } - months := d.Months % 12 + months := d.Months % calendarMonthsPerYear if d.Negative { months = -months } @@ -487,7 +523,7 @@ func fnMonthsFromDuration(_ context.Context, args []xdm.Sequence) (xdm.Sequence, // components stay correct even for fractions arbitrarily close to a whole // second (where the float64 Seconds field would round up). func dayTimeWholeSeconds(d xdm.Duration) *big.Int { - total := xdm.DurationToRat(d, false) + total := xdm.DurationToRat(d, durationSecondsComponent) abs := total if abs.Sign() < 0 { @@ -507,7 +543,7 @@ func fnDaysFromDuration(_ context.Context, args []xdm.Sequence) (xdm.Sequence, e return nil, nil } - days := new(big.Int).Quo(dayTimeWholeSeconds(d), big.NewInt(86400)) + days := new(big.Int).Quo(dayTimeWholeSeconds(d), big.NewInt(calendarSecondsPerDay)) if d.Negative { days.Neg(days) } @@ -526,9 +562,9 @@ func fnHoursFromDuration(_ context.Context, args []xdm.Sequence) (xdm.Sequence, return nil, nil } - withinDay := new(big.Int).Rem(dayTimeWholeSeconds(d), big.NewInt(86400)) + withinDay := new(big.Int).Rem(dayTimeWholeSeconds(d), big.NewInt(calendarSecondsPerDay)) - hours := new(big.Int).Quo(withinDay, big.NewInt(3600)) + hours := new(big.Int).Quo(withinDay, big.NewInt(calendarSecondsPerHour)) if d.Negative { hours.Neg(hours) } @@ -546,9 +582,9 @@ func fnMinutesFromDuration(_ context.Context, args []xdm.Sequence) (xdm.Sequence return nil, nil } - withinHour := new(big.Int).Rem(dayTimeWholeSeconds(d), big.NewInt(3600)) + withinHour := new(big.Int).Rem(dayTimeWholeSeconds(d), big.NewInt(calendarSecondsPerHour)) - minutes := new(big.Int).Quo(withinHour, big.NewInt(60)) + minutes := new(big.Int).Quo(withinHour, big.NewInt(calendarSecondsPerMinute)) if d.Negative { minutes.Neg(minutes) } @@ -567,7 +603,7 @@ func fnSecondsFromDuration(_ context.Context, args []xdm.Sequence) (xdm.Sequence } // seconds-from-duration returns an xs:decimal: the exact total seconds mod 60. // durationToRat is SecRat-aware, so the fractional part is preserved exactly. - total := xdm.DurationToRat(d, false) + total := xdm.DurationToRat(d, durationSecondsComponent) abs := total if abs.Sign() < 0 { @@ -576,7 +612,7 @@ func fnSecondsFromDuration(_ context.Context, args []xdm.Sequence) (xdm.Sequence whole := new(big.Int).Quo(abs.Num(), abs.Denom()) frac := new(big.Rat).Sub(abs, new(big.Rat).SetInt(whole)) - secsInt := new(big.Int).Rem(whole, big.NewInt(60)) + secsInt := new(big.Int).Rem(whole, big.NewInt(calendarSecondsPerMinute)) result := new(big.Rat).SetInt(secsInt) result.Add(result, frac) @@ -664,18 +700,18 @@ func validateTimezoneOffset(d xdm.Duration) error { // Use the exact SecRat-aware total-seconds rational. An offset whose seconds // underflow float64 (e.g. PT0.1S) is still exactly nonzero here, // so it is not silently accepted as UTC. - abs := xdm.DurationToRat(d, false) + abs := xdm.DurationToRat(d, durationSecondsComponent) if abs.Sign() < 0 { abs = new(big.Rat).Neg(abs) } - if abs.Cmp(big.NewRat(50400, 1)) > 0 { // 14 * 3600 - return &xdm.Error{Code: "FODT0003", Message: "timezone offset out of range (-PT14H to PT14H)"} + if abs.Cmp(big.NewRat(timezoneMaximumOffsetSeconds, 1)) > 0 { // 14 * 3600 + return &xdm.Error{Code: errCodeFODT0003, Message: "timezone offset out of range (-PT14H to PT14H)"} } // Whole number of minutes: the exact seconds rational must be divisible by 60. - rem := new(big.Rat).Quo(abs, big.NewRat(60, 1)) + rem := new(big.Rat).Quo(abs, big.NewRat(calendarSecondsPerMinute, 1)) if !rem.IsInt() { - return &xdm.Error{Code: "FODT0003", Message: "timezone offset must be a whole number of minutes"} + return &xdm.Error{Code: errCodeFODT0003, Message: "timezone offset must be a whole number of minutes"} } return nil @@ -685,7 +721,7 @@ func durationToLocation(d xdm.Duration) *time.Location { // Bounded by validateTimezoneOffset (|offset| <= 50400, whole minutes), so the // exact seconds magnitude fits an int. Read it from the SecRat-aware rational // so a sub-float64 offset is not flattened to zero. - abs := xdm.DurationToRat(d, false) + abs := xdm.DurationToRat(d, durationSecondsComponent) if abs.Sign() < 0 { abs = new(big.Rat).Neg(abs) } diff --git a/transform/internal/xpath/functions_hof.go b/transform/internal/xpath/functions_hof.go index 44493f7..8f10b65 100644 --- a/transform/internal/xpath/functions_hof.go +++ b/transform/internal/xpath/functions_hof.go @@ -11,6 +11,29 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) +// Higher-order function dispatch and callback shape belong to F&O. A normal +// typed reference has one declared type per parameter, not a repeated last type. +const ( + functionReferenceRepeatedParameterType = false + unaryCallbackArity = 1 + binaryCallbackArity = 2 + hofPredicateResultCardinality = 1 + hofApplyArrayCardinality = 1 + hofFunctionArgumentCardinality = 1 +) + +// Nonpositive node configuration selects the existing default; nonpositive +// operation limits disable only local accounting. Positive accumulating limits +// bound item counts, including array members that contain no items. Work units +// count logical iterations or the minimum cost of cloning one array member. +const ( + builtinNodeLimitDefault = 0 + builtinOperationLimitDisabled = 0 + accumulatingSequenceLimitDisabled = 0 + builtinIterationWork = 1 + builtinMemberMinimumWork = 1 +) + // NewFunctionReference creates a typed reference without retaining an evaluation. // Argument conversion and calls use the live caller's context and resource policy. func NewFunctionReference(name QualifiedName, arity int, fn TypedFunction) xdm.FunctionItem { @@ -19,7 +42,7 @@ func NewFunctionReference(name QualifiedName, arity int, fn TypedFunction) xdm.F return xdm.FunctionItem{ Arity: arity, Name: name.Name, Namespace: name.URI, ParamTypes: fn.FuncParamTypes(), ReturnType: fn.FuncReturnType(), - Invoke: lookupFunctionInvoker(nil, fn.FuncParamTypes(), false, qv, arity, fn), + Invoke: lookupFunctionInvoker(nil, fn.FuncParamTypes(), functionReferenceRepeatedParameterType, qv, arity, fn), } } @@ -32,7 +55,7 @@ func CallFunctionLookup(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, } func fnFunctionLookup(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { - nameArg, err := extractSingleAtomicArg(args[0], "function-lookup()") + nameArg, err := extractSingleAtomicArg(args[0], fnNameFunctionLookup+"()") if err != nil { return nil, err } @@ -41,7 +64,7 @@ func fnFunctionLookup(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, e return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "function-lookup() first argument must be QName"} } - arityArg, err := extractSingleAtomicArg(args[1], "function-lookup()") + arityArg, err := extractSingleAtomicArg(args[1], fnNameFunctionLookup+"()") if err != nil { return nil, err } @@ -166,23 +189,23 @@ func lookupRegisteredFunction(ec *evalContext, qv xdm.QNameValue, arity int) (Fu func lookupFunctionsHof(name string) (*builtinFunc, bool) { switch name { case nameForEach: - return &builtinFunc{name: name, min: 2, max: 2, fn: fnForEach}, true + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityTwo, fn: fnForEach}, true case nameFilter: - return &builtinFunc{name: name, min: 2, max: 2, fn: fnFilter}, true + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityTwo, fn: fnFilter}, true case nameFoldLeft: - return &builtinFunc{name: name, min: 3, max: 3, fn: fnFoldLeft}, true + return &builtinFunc{name: name, min: builtinArityThree, max: builtinArityThree, fn: fnFoldLeft}, true case nameFoldRight: - return &builtinFunc{name: name, min: 3, max: 3, fn: fnFoldRight}, true + return &builtinFunc{name: name, min: builtinArityThree, max: builtinArityThree, fn: fnFoldRight}, true case nameForEachPair: - return &builtinFunc{name: name, min: 3, max: 3, fn: fnForEachPair}, true - case "apply": - return &builtinFunc{name: name, min: 2, max: 2, fn: fnApply}, true - case "function-lookup": - return &builtinFunc{name: name, min: 2, max: 2, fn: fnFunctionLookup}, true - case "function-arity": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnFunctionArity}, true - case "function-name": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnFunctionName}, true + return &builtinFunc{name: name, min: builtinArityThree, max: builtinArityThree, fn: fnForEachPair}, true + case fnNameApply: + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityTwo, fn: fnApply}, true + case fnNameFunctionLookup: + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityTwo, fn: fnFunctionLookup}, true + case fnNameFunctionArity: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnFunctionArity}, true + case fnNameFunctionName: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnFunctionName}, true } return nil, false @@ -194,7 +217,7 @@ func lookupFunctionsHof(name string) (*builtinFunc, bool) { // (ec == nil) or the evaluation did not set an explicit limit, the package // default applies so unbounded materialization is still rejected. func fnMaxNodes(ec *evalContext) int { - if ec == nil || ec.maxNodes <= 0 { + if ec == nil || ec.maxNodes <= builtinNodeLimitDefault { return maxNodeSetLength } @@ -210,7 +233,7 @@ func fnMaxNodes(ec *evalContext) int { // not just maxNodes, so a small OpLimit does not first materialize a slice // proportional to the input before the op charge rejects it. func fnRemainingOps(ec *evalContext) (remaining int, bounded bool) { - if ec == nil || ec.opLimit <= 0 || ec.opCount == nil { + if ec == nil || ec.opLimit <= builtinOperationLimitDisabled || ec.opCount == nil { return 0, false } @@ -223,7 +246,7 @@ func fnRemainingOps(ec *evalContext) (remaining int, bounded bool) { // per iteration so a long-running higher-order call respects op/time limits. func fnCountOp(ctx context.Context, ec *evalContext) error { if ec != nil { - return ec.countOps(ctx, 1) + return ec.countOps(ctx, builtinIterationWork) } return ctx.Err() @@ -258,23 +281,23 @@ func fnCountOps(ctx context.Context, ec *evalContext, n int) error { // - item count: a single member with a huge (possibly lazy) sequence could // otherwise be cloned by NewArray past maxNodes items, so member length is // bounded against the remaining headroom (maxNodes-total). The subtraction is -// overflow-safe because it only runs when maxNodes > 0. +// overflow-safe because it only runs when maxNodes > accumulatingSequenceLimitDisabled. // // The op charge is max(seqLen(member), 1): NewArray clones each member, so a // member with many items below maxNodes but above OpLimit must be rejected, AND // empty members must still each cost an op so a flood of them cannot bypass // OpLimit. func appendArrayMember(ctx context.Context, ec *evalContext, maxNodes int, dst []xdm.Sequence, total int, member xdm.Sequence) ([]xdm.Sequence, int, error) { - if maxNodes > 0 && len(dst)+1 > maxNodes { + if maxNodes > accumulatingSequenceLimitDisabled && len(dst)+1 > maxNodes { return dst, total, ErrNodeSetLimit } mLen := xdm.SequenceLen(member) - if maxNodes > 0 && mLen > maxNodes-total { + if maxNodes > accumulatingSequenceLimitDisabled && mLen > maxNodes-total { return dst, total, ErrNodeSetLimit } - if err := fnCountOps(ctx, ec, max(mLen, 1)); err != nil { + if err := fnCountOps(ctx, ec, max(mLen, builtinMemberMinimumWork)); err != nil { return dst, total, err } @@ -294,7 +317,7 @@ func fnForEach(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { var result xdm.ItemSlice - callArgs := make([]xdm.Sequence, 1) + callArgs := make([]xdm.Sequence, unaryCallbackArity) for item := range xdm.SequenceItems(seq) { if err := fnCountOp(ctx, ec); err != nil { @@ -325,7 +348,7 @@ func fnFilter(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { return nil, err } // Per XPath 3.1, the callback must have arity 1 - if fi.Arity >= 0 && fi.Arity != 1 { + if fi.Arity >= finiteArityFloor && fi.Arity != unaryCallbackArity { return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: fmt.Sprintf("fn:filter callback must have arity 1, got %d", fi.Arity)} } @@ -334,7 +357,7 @@ func fnFilter(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { var result xdm.ItemSlice - callArgs := make([]xdm.Sequence, 1) + callArgs := make([]xdm.Sequence, unaryCallbackArity) for item := range xdm.SequenceItems(seq) { if err := fnCountOp(ctx, ec); err != nil { @@ -348,7 +371,7 @@ func fnFilter(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { return nil, err } // Per XPath 3.1, the callback must return exactly one xs:boolean - if xdm.SequenceLen(r) != 1 { + if xdm.SequenceLen(r) != hofPredicateResultCardinality { return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "fn:filter callback must return a single xs:boolean value"} } @@ -379,7 +402,7 @@ func fnFoldLeft(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) ec := getFnContext(ctx) maxNodes := fnMaxNodes(ec) - callArgs := make([]xdm.Sequence, 2) + callArgs := make([]xdm.Sequence, binaryCallbackArity) for item := range xdm.SequenceItems(seq) { if err := fnCountOp(ctx, ec); err != nil { @@ -395,7 +418,7 @@ func fnFoldLeft(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) } // The accumulator can grow without bound across iterations; reject once // it would exceed the configured sequence/node-set limit. - if maxNodes > 0 && xdm.SequenceLen(acc) > maxNodes { + if maxNodes > accumulatingSequenceLimitDisabled && xdm.SequenceLen(acc) > maxNodes { return nil, ErrNodeSetLimit } } @@ -417,7 +440,7 @@ func fnFoldRight(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) // Iterate from last to first WITHOUT materializing the input sequence, so a // lazy/borrowed input is never fully realized before the per-item op-count // and accumulator size-bound checks run (mirrors fnFoldLeft's streaming). - callArgs := make([]xdm.Sequence, 2) + callArgs := make([]xdm.Sequence, binaryCallbackArity) for i := xdm.SequenceLen(seq); i > 0; i-- { if err := fnCountOp(ctx, ec); err != nil { @@ -433,7 +456,7 @@ func fnFoldRight(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) } // The accumulator can grow without bound across iterations; reject once // it would exceed the configured sequence/node-set limit. - if maxNodes > 0 && xdm.SequenceLen(acc) > maxNodes { + if maxNodes > accumulatingSequenceLimitDisabled && xdm.SequenceLen(acc) > maxNodes { return nil, ErrNodeSetLimit } } @@ -450,7 +473,7 @@ func fnForEachPair(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, erro return nil, err } // Per XPath 3.1, the callback must have arity 2 - if fi.Arity >= 0 && fi.Arity != 2 { + if fi.Arity >= finiteArityFloor && fi.Arity != binaryCallbackArity { return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: fmt.Sprintf("fn:for-each-pair callback must have arity 2, got %d", fi.Arity)} } @@ -460,7 +483,7 @@ func fnForEachPair(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, erro var result xdm.ItemSlice - callArgs := make([]xdm.Sequence, 2) + callArgs := make([]xdm.Sequence, binaryCallbackArity) for i := range size { if err := fnCountOp(ctx, ec); err != nil { @@ -490,7 +513,7 @@ func fnApply(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { return nil, err } - if xdm.SequenceLen(args[1]) != 1 { + if xdm.SequenceLen(args[1]) != hofApplyArrayCardinality { return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "apply() second argument must be a single array"} } @@ -499,7 +522,7 @@ func fnApply(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "apply() second argument must be array"} } - if fi.Arity >= 0 && arr.Size() != fi.Arity { + if fi.Arity >= finiteArityFloor && arr.Size() != fi.Arity { return nil, &xdm.Error{Code: errCodeFOAP0001, Message: fmt.Sprintf("fn:apply: function has arity %d but array has %d members", fi.Arity, arr.Size())} } @@ -543,7 +566,7 @@ func fnFunctionName(_ context.Context, args []xdm.Sequence) (xdm.Sequence, error } func extractFunctionItem(seq xdm.Sequence) (xdm.FunctionItem, error) { - if xdm.SequenceLen(seq) != 1 { + if xdm.SequenceLen(seq) != hofFunctionArgumentCardinality { return xdm.FunctionItem{}, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "expected single function item"} } // asFunctionItem is the single source of truth for adapting maps and arrays diff --git a/transform/internal/xpath/functions_json.go b/transform/internal/xpath/functions_json.go index a6cb932..b1e8bcc 100644 --- a/transform/internal/xpath/functions_json.go +++ b/transform/internal/xpath/functions_json.go @@ -17,17 +17,60 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) +// JSON escape alphabets retain their case across parsers, XML mapping and +// serializers; lowercase fallback and uppercase serialization remain distinct. const ( jsonHexLower = "0123456789abcdef" jsonHexUpper = "0123456789ABCDEF" ) +// JSON option keys are shared by parsing and XML mapping. Argument-count +// presence, map-item cardinality and diagnostic positions have different roles. +const ( + jsonOptionLiberal = "liberal" + jsonOptionEscape = "escape" + + jsonOptionsArgumentThreshold = 1 + jsonOptionsMapCardinality = 1 + jsonOptionDiagnosticArgumentIndex = 0 // zero-based synthetic option argument +) + +// An internal invalid-character frame decodes to one NUL byte followed by a +// six-byte raw escape payload. Short escapes add four padding spaces. +// These are representation widths, never accepted-input limits. +const ( + jsonInvalidEscapeSentinel = 0 + jsonInvalidEscapePayloadStart = 1 + jsonInvalidEscapeFrameBytes = 7 + jsonInvalidEscapeWirePrefix = `\u0000\\` + jsonInvalidShortEscapePadding = " " + jsonInvalidBackspacePayload = `\b ` + jsonInvalidFormfeedPayload = `\f ` +) + +// Unicode escapes use source byte offsets and UTF-16 payload bits as named. +// Two-byte short escapes and the two-byte Unicode escape prefix remain distinct. +const ( + jsonShortEscapeBytes = 2 + jsonCodeUnitEscapePrefixBytes = 2 + jsonLowSurrogateMarkerOffset = 7 + jsonLowSurrogateHexStart = 8 + jsonHighSurrogateFirst = 0xD800 + jsonHighSurrogateLast = 0xDBFF + jsonLowSurrogateFirst = 0xDC00 + jsonLowSurrogateLast = 0xDFFF + jsonSurrogatePayloadBits = 10 + jsonSupplementaryPlaneFirst = 0x10000 + jsonC0ControlFirst = 0x00 + jsonC0ControlLast = 0x1F +) + func lookupFunctionsJSON(name string) (*builtinFunc, bool) { switch name { - case "parse-json": - return &builtinFunc{name: name, min: 1, max: 2, fn: fnParseJSON}, true - case "json-doc": - return &builtinFunc{name: name, min: 1, max: 2, fn: fnJSONDoc}, true + case fnNameParseJSON: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityTwo, fn: fnParseJSON}, true + case fnNameJSONDoc: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityTwo, fn: fnJSONDoc}, true } return nil, false @@ -36,8 +79,7 @@ func lookupFunctionsJSON(name string) (*builtinFunc, bool) { // jsonOptions holds parsed options for fn:parse-json. type jsonOptions struct { fallback *xdm.FunctionItem - invalidEsc map[rune]string - duplicates string // "reject", "use-first", "use-last" (default) + duplicates string // parse-json starts with use-first; json-to-xml may retain duplicates liberal bool escape bool validate bool // validate=true(): type-annotate the json-to-xml result tree @@ -68,13 +110,11 @@ func fnParseJSON(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) return nil, err } - s, invalidEsc, err := preprocessJSONStringLiterals(s) + s, err = preprocessJSONStringLiterals(s) if err != nil { return nil, err } - opts.invalidEsc = invalidEsc - dec := json.NewDecoder(strings.NewReader(s)) dec.UseNumber() @@ -105,11 +145,11 @@ func fnParseJSON(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) // parseJSONOptions extracts and validates options from the second argument. func parseJSONOptions(ctx context.Context, args []xdm.Sequence) (jsonOptions, error) { opts := jsonOptions{duplicates: duplicatesUseFirst} // XPath spec default. - if len(args) <= 1 || xdm.SequenceLen(args[1]) == 0 { + if len(args) <= jsonOptionsArgumentThreshold || xdm.SequenceLen(args[1]) == 0 { return opts, nil } - if xdm.SequenceLen(args[1]) > 1 { + if xdm.SequenceLen(args[1]) > jsonOptionsMapCardinality { return opts, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "options argument must be a single map"} } @@ -120,7 +160,7 @@ func parseJSONOptions(ctx context.Context, args []xdm.Sequence) (jsonOptions, er var err error - opts.liberal, err = parseJSONBooleanOption(ctx, options, "liberal") + opts.liberal, err = parseJSONBooleanOption(ctx, options, jsonOptionLiberal) if err != nil { return opts, err } @@ -130,7 +170,7 @@ func parseJSONOptions(ctx context.Context, args []xdm.Sequence) (jsonOptions, er return opts, err } - opts.escape, err = parseJSONBooleanOption(ctx, options, "escape") + opts.escape, err = parseJSONBooleanOption(ctx, options, jsonOptionEscape) if err != nil { return opts, err } @@ -146,7 +186,7 @@ func parseJSONBooleanOption(ctx context.Context, options xdm.MapItem, name strin return false, nil } - converted, err := coerceFuncallArg(ctx, value, stAtomic(xdm.TypeBoolean, xdm.OccurrenceExactlyOne), "JSON option "+name, 0, getFnContext(ctx)) + converted, err := coerceFuncallArg(ctx, value, stAtomic(xdm.TypeBoolean, xdm.OccurrenceExactlyOne), "JSON option "+name, jsonOptionDiagnosticArgumentIndex, getFnContext(ctx)) if err != nil { return false, err } @@ -192,7 +232,7 @@ func finalizeJSONFallbackOption(ctx context.Context, options xdm.MapItem, opts * required := stFunc([]xdm.SequenceType{stAtomic(xdm.TypeString, xdm.OccurrenceExactlyOne)}, stAtomic(xdm.TypeString, xdm.OccurrenceExactlyOne)) - converted, err := coerceFuncallArg(ctx, value, required, "JSON fallback option", 0, getFnContext(ctx)) + converted, err := coerceFuncallArg(ctx, value, required, "JSON fallback option", jsonOptionDiagnosticArgumentIndex, getFnContext(ctx)) if err != nil { return err } @@ -225,10 +265,12 @@ type jsonBudget struct { } func newJSONBudget(ctx context.Context) *jsonBudget { + const jsonDefaultDepthThreshold = 0 // nonpositive selects fallback depth + ec := getFnContext(ctx) maxDepth := DefaultMaxRecursionDepth - if ec != nil && ec.maxRecursionDepth > 0 { + if ec != nil && ec.maxRecursionDepth > jsonDefaultDepthThreshold { maxDepth = ec.maxRecursionDepth } @@ -239,8 +281,10 @@ func newJSONBudget(ctx context.Context) *jsonBudget { // nests deeper than the recursion limit, so adversarial JSON cannot exhaust the // goroutine stack. It returns the incremented depth for nested calls. func (b *jsonBudget) enter(depth int) (int, error) { + const jsonDepthLimitDisabled = 0 // nonpositive means no local depth cap + depth++ - if b.maxDepth > 0 && depth > b.maxDepth { + if b.maxDepth > jsonDepthLimitDisabled && depth > b.maxDepth { return depth, ErrRecursionLimit } @@ -252,7 +296,9 @@ func (b *jsonBudget) enter(depth int) (int, error) { // context cancellation), so a wide JSON container respects OpLimit and stays // cancellable. func (b *jsonBudget) charge(ctx context.Context, count int) error { - if b.maxNodes > 0 && count+1 > b.maxNodes { + const jsonMemberLimitDisabled = 0 // nonpositive means no local member cap + + if b.maxNodes > jsonMemberLimitDisabled && count+1 > b.maxNodes { return ErrNodeSetLimit } @@ -423,8 +469,8 @@ func consumeJSONClose(dec *json.Decoder, expected json.Delim, kind string) error return nil } -func preprocessJSONStringLiterals(source string) (string, map[rune]string, error) { - preprocessor := jsonStringPreprocessor{source: source, invalidEsc: make(map[rune]string)} +func preprocessJSONStringLiterals(source string) (string, error) { + preprocessor := jsonStringPreprocessor{source: source} inString := false for i := 0; i < len(source); { @@ -447,13 +493,13 @@ func preprocessJSONStringLiterals(source string) (string, map[rune]string, error case '\\': next, err := preprocessor.writeEscape(i) if err != nil { - return "", nil, err + return "", err } i = next default: - if char < 0x20 { - return "", nil, &xdm.Error{Code: errCodeFOJS0001, Message: "invalid JSON: unescaped control character in string"} + if char < jsonFirstPrintableASCII { + return "", &xdm.Error{Code: errCodeFOJS0001, Message: "invalid JSON: unescaped control character in string"} } preprocessor.output.WriteString(source[i : i+size]) @@ -461,13 +507,12 @@ func preprocessJSONStringLiterals(source string) (string, map[rune]string, error } } - return preprocessor.output.String(), preprocessor.invalidEsc, nil + return preprocessor.output.String(), nil } type jsonStringPreprocessor struct { - source string - output strings.Builder - invalidEsc map[rune]string + source string + output strings.Builder } func (p *jsonStringPreprocessor) writeEscape(offset int) (int, error) { @@ -477,14 +522,12 @@ func (p *jsonStringPreprocessor) writeEscape(offset int) (int, error) { switch next := p.source[offset+1]; next { case '"', '\\', '/', 'n', 'r', 't': - p.output.WriteString(p.source[offset : offset+2]) - return offset + 2, nil + p.output.WriteString(p.source[offset : offset+jsonShortEscapeBytes]) + return offset + jsonShortEscapeBytes, nil case 'b', 'f': - if err := p.writePlaceholder(p.source[offset : offset+2]); err != nil { - return 0, err - } + p.writeInvalidEscape(p.source[offset : offset+jsonShortEscapeBytes]) - return offset + 2, nil + return offset + jsonShortEscapeBytes, nil case 'u': return p.writeUnicodeEscape(offset) default: @@ -493,71 +536,72 @@ func (p *jsonStringPreprocessor) writeEscape(offset int) (int, error) { } func (p *jsonStringPreprocessor) writeUnicodeEscape(offset int) (int, error) { - if offset+6 > len(p.source) { + if offset+jsonCodeUnitEscapeBytes > len(p.source) { return 0, &xdm.Error{Code: errCodeFOJS0001, Message: "invalid JSON: incomplete \\u escape"} } - escapeText := p.source[offset : offset+6] + escapeText := p.source[offset : offset+jsonCodeUnitEscapeBytes] - codepoint, err := parseJSONHexEscape(p.source[offset+2 : offset+6]) + codepoint, err := parseJSONHexEscape(p.source[offset+jsonCodeUnitEscapePrefixBytes : offset+jsonCodeUnitEscapeBytes]) if err != nil { return 0, err } if validJSONSurrogatePair(p.source, offset, codepoint) { - p.output.WriteString(p.source[offset : offset+12]) - return offset + 12, nil + p.output.WriteString(p.source[offset : offset+jsonSurrogatePairEscapeBytes]) + return offset + jsonSurrogatePairEscapeBytes, nil } - if codepoint >= 0xD800 && codepoint <= 0xDFFF || !isValidXMLCodepoint(int(codepoint)) { - if err := p.writePlaceholder(escapeText); err != nil { - return 0, err - } + if codepoint >= jsonHighSurrogateFirst && codepoint <= jsonLowSurrogateLast || !isValidXMLCodepoint(int(codepoint)) { + p.writeInvalidEscape(escapeText) } else { p.output.WriteString(escapeText) } - return offset + 6, nil + return offset + jsonCodeUnitEscapeBytes, nil } -func (p *jsonStringPreprocessor) writePlaceholder(escapeText string) error { - placeholder, err := nextJSONPlaceholderRune(p.source, p.invalidEsc) - if err != nil { - return err - } - - p.invalidEsc[placeholder] = escapeText - p.output.WriteRune(placeholder) +// Encode an invalid escape as a decoded NUL plus its original six-byte raw +// spelling. Short escapes have four padding spaces. A genuine XML string cannot +// contain NUL: raw controls are rejected above and escaped NUL is encoded here. +// This preserves every valid character without reserving a finite rune pool. +func (p *jsonStringPreprocessor) writeInvalidEscape(escapeText string) { + p.output.WriteString(jsonInvalidEscapeWirePrefix) + p.output.WriteString(escapeText[1:]) - return nil + if len(escapeText) == jsonShortEscapeBytes { + p.output.WriteString(jsonInvalidShortEscapePadding) + } } func validJSONSurrogatePair(source string, offset int, high uint32) bool { - if high < 0xD800 || high > 0xDBFF || offset+12 > len(source) { + if high < jsonHighSurrogateFirst || high > jsonHighSurrogateLast || offset+jsonSurrogatePairEscapeBytes > len(source) { return false } - if source[offset+6] != '\\' || source[offset+7] != 'u' { + if source[offset+jsonCodeUnitEscapeBytes] != '\\' || source[offset+jsonLowSurrogateMarkerOffset] != 'u' { return false } - low, err := parseJSONHexEscape(source[offset+8 : offset+12]) + low, err := parseJSONHexEscape(source[offset+jsonLowSurrogateHexStart : offset+jsonSurrogatePairEscapeBytes]) - return err == nil && low >= 0xDC00 && low <= 0xDFFF + return err == nil && low >= jsonLowSurrogateFirst && low <= jsonLowSurrogateLast } func parseJSONHexEscape(hex string) (uint32, error) { + const jsonHexLetterDigitOffset = 10 // letters follow decimal hexadecimal digits + var cp uint32 for _, c := range hex { - cp <<= 4 + cp <<= jsonHexDigitBits switch { case c >= '0' && c <= '9': cp += uint32(c - '0') case c >= 'a' && c <= 'f': - cp += uint32(c-'a') + 10 + cp += uint32(c-'a') + jsonHexLetterDigitOffset case c >= 'A' && c <= 'F': - cp += uint32(c-'A') + 10 + cp += uint32(c-'A') + jsonHexLetterDigitOffset default: return 0, &xdm.Error{Code: errCodeFOJS0001, Message: fmt.Sprintf("invalid JSON: invalid hex digit in \\u escape: %c", c)} } @@ -566,64 +610,102 @@ func parseJSONHexEscape(hex string) (uint32, error) { return cp, nil } -func nextJSONPlaceholderRune(source string, used map[rune]string) (rune, error) { - for r := rune(0xF0000); r <= 0xFFFFD; r++ { - if _, exists := used[r]; exists { +func applyJSONStringOptions(ctx context.Context, s string, opts jsonOptions) (string, error) { + var b strings.Builder + + for offset := 0; offset < len(s); { + if err := ctx.Err(); err != nil { + return "", err + } + + r, size := utf8.DecodeRuneInString(s[offset:]) + if r != jsonInvalidEscapeSentinel { + if err := ChargeString(ctx, jsonStringRuneSize(r, opts.escape)); err != nil { + return "", err + } + + if opts.escape { + appendEscapedJSONStringRune(&b, r) + } else { + b.WriteRune(r) + } + + offset += size + continue } - if !strings.ContainsRune(source, r) { - return r, nil + raw, err := jsonInvalidEscape(s[offset:]) + if err != nil { + return "", err } - } - return 0, &xdm.Error{Code: errCodeFOJS0001, Message: "exhausted JSON placeholder runes"} -} + offset += jsonInvalidEscapeFrameBytes -func applyJSONStringOptions(ctx context.Context, s string, opts jsonOptions) (string, error) { - if opts.escape { - var b strings.Builder + replacement, err := jsonInvalidEscapeReplacement(ctx, raw, opts) + if err != nil { + return "", err + } - for _, r := range s { - if raw, found := opts.invalidEsc[r]; found { - b.WriteString(canonicalInvalidJSONEscape(raw)) + if err := ChargeString(ctx, len(replacement)); err != nil { + return "", err + } - continue - } + b.WriteString(replacement) + } - appendEscapedJSONStringRune(&b, r) + return b.String(), nil +} + +func jsonStringRuneSize(r rune, escape bool) int { + if escape { + switch r { + case '\\', '\b', '\f', '\n', '\r', '\t': + return jsonShortEscapeBytes + default: + if !isValidXMLCodepoint(int(r)) { + return jsonCodeUnitEscapeBytes + } } + } + + return utf8.RuneLen(r) +} - return b.String(), nil +func jsonInvalidEscapeReplacement(ctx context.Context, raw string, opts jsonOptions) (string, error) { + if opts.escape { + return canonicalInvalidJSONEscape(raw), nil } - var b strings.Builder + if opts.fallback == nil { + return string(utf8.RuneError), nil + } - for _, r := range s { - raw, found := opts.invalidEsc[r] - if !found { - b.WriteRune(r) - continue - } + seq, err := opts.fallback.Invoke(ctx, []xdm.Sequence{xdm.SingleString(raw)}) + if err != nil { + return "", err + } - repl := string(rune(0xFFFD)) + return seqToStringErr(ctx, seq) +} - if opts.fallback != nil { - seq, err := opts.fallback.Invoke(ctx, []xdm.Sequence{xdm.ItemSlice{xdm.AtomicValue{TypeName: xdm.TypeString, Value: raw}}}) - if err != nil { - return "", err - } +// jsonInvalidEscape decodes the fixed-width payload after the internal sentinel. +// Reject malformed payloads rather than slicing beyond the decoded JSON string. +func jsonInvalidEscape(encoded string) (string, error) { + if len(encoded) >= jsonInvalidEscapeFrameBytes { + raw := encoded[jsonInvalidEscapePayloadStart:jsonInvalidEscapeFrameBytes] + if raw == jsonInvalidBackspacePayload || raw == jsonInvalidFormfeedPayload { + return raw[:jsonShortEscapeBytes], nil + } - repl, err = seqToStringErr(ctx, seq) - if err != nil { - return "", err + if strings.HasPrefix(raw, `\u`) { + if _, err := parseJSONHexEscape(raw[jsonCodeUnitEscapePrefixBytes:]); err == nil { + return raw, nil } } - - b.WriteString(repl) } - return b.String(), nil + return "", &xdm.Error{Code: errCodeFOJS0001, Message: "invalid internal JSON escape marker"} } func appendEscapedJSONStringRune(b *strings.Builder, r rune) { @@ -641,7 +723,7 @@ func appendEscapedJSONStringRune(b *strings.Builder, r rune) { case '\t': b.WriteString(`\t`) default: - if (r >= 0x00 && r <= 0x1F) || !isValidXMLCodepoint(int(r)) { + if (r >= jsonC0ControlFirst && r <= jsonC0ControlLast) || !isValidXMLCodepoint(int(r)) { appendJSONCodeUnit(b, uint16(r), jsonHexLower) // #nosec G115 -- Decoded Unicode runes reach this branch only for disallowed BMP code points. return } @@ -669,7 +751,7 @@ func appendSerializedJSONStringRune(b *strings.Builder, r rune) { case '\t': b.WriteString(`\t`) default: - if (r >= 0x00 && r <= 0x1F) || (r >= 0x7F && r <= 0x9F) || !isValidXMLCodepoint(int(r)) { + if (r >= jsonC0ControlFirst && r <= jsonC0ControlLast) || (r >= jsonEscapedControlStart && r <= jsonEscapedControlEnd) || !isValidXMLCodepoint(int(r)) { appendJSONCodeUnit(b, uint16(r), jsonHexUpper) // #nosec G115 -- Decoded Unicode runes reach this branch only for disallowed BMP code points. return } @@ -680,7 +762,9 @@ func appendSerializedJSONStringRune(b *strings.Builder, r rune) { // appendJSONCodeUnit writes one UTF-16 code unit as a fixed-width JSON escape. func appendJSONCodeUnit(b *strings.Builder, unit uint16, digits string) { - escaped := [6]byte{'\\', 'u', digits[unit>>12], digits[(unit>>8)&0x0F], digits[(unit>>4)&0x0F], digits[unit&0x0F]} + const jsonCodeUnitMiddleShift = 8 // second hexadecimal nibble in a 16-bit code unit + + escaped := [jsonCodeUnitEscapeBytes]byte{'\\', 'u', digits[unit>>jsonCodeUnitHighShift], digits[(unit>>jsonCodeUnitMiddleShift)&jsonHexDigitMask], digits[(unit>>jsonHexDigitBits)&jsonHexDigitMask], digits[unit&jsonHexDigitMask]} b.Write(escaped[:]) } @@ -714,7 +798,7 @@ func fnJSONDoc(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { // Delegate to parse-json logic, preserving any caller-supplied options. parseArgs := []xdm.Sequence{xdm.ItemSlice{xdm.AtomicValue{TypeName: xdm.TypeString, Value: body}}} - if len(args) > 1 { + if len(args) > jsonOptionsArgumentThreshold { parseArgs = append(parseArgs, args[1]) } diff --git a/transform/internal/xpath/functions_json_xml.go b/transform/internal/xpath/functions_json_xml.go index a3f78b4..cb4249e 100644 --- a/transform/internal/xpath/functions_json_xml.go +++ b/transform/internal/xpath/functions_json_xml.go @@ -18,12 +18,24 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) +// These attributes belong to the F&O JSON XML representation independently +// of similarly spelled names in other XML vocabularies. +const ( + jsonXMLAttributeKey = "key" + jsonXMLAttributeEscaped = "escaped" + jsonXMLAttributeEscapedKey = "escaped-key" +) + +// Scalar XML representations permit comments and PIs while rejecting element +// children; both string and numeric/boolean scalar readers use this mode. +const jsonScalarCommentsAllowed = true + func lookupFunctionsJSONXML(name string) (*builtinFunc, bool) { switch name { - case "json-to-xml": - return &builtinFunc{name: name, min: 1, max: 2, fn: fnJSONToXML}, true - case "xml-to-json": - return &builtinFunc{name: name, min: 1, max: 2, fn: fnXMLToJSON}, true + case fnNameJSONToXML: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityTwo, fn: fnJSONToXML}, true + case fnNameXMLToJSON: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityTwo, fn: fnXMLToJSON}, true } return nil, false @@ -51,15 +63,13 @@ func fnJSONToXML(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) return nil, &xdm.Error{Code: errCodeFOJS0004, Message: "json-to-xml: schema validation and typed data are unavailable"} } - s = strings.TrimPrefix(s, "\uFEFF") + s = strings.TrimPrefix(s, lexicon.BOMUTF8) - s, invalidEsc, err := preprocessJSONStringLiterals(s) + s, err = preprocessJSONStringLiterals(s) if err != nil { return nil, err } - opts.invalidEsc = invalidEsc - dec := json.NewDecoder(strings.NewReader(s)) dec.UseNumber() @@ -132,19 +142,28 @@ func installJSONToXMLAnnotations(ec *evalContext, annotations map[xdm.Node]strin // whose target namespace is the fn namespace. The boolean element is typed // directly as xs:boolean; the others use the named complex/simple types. func jsonToXMLTypeAnnotation(name string) string { + // Type local names from the JSON XML representation schema. + const ( + jsonSchemaMapType = "mapType" + jsonSchemaArrayType = "arrayType" + jsonSchemaStringType = "stringType" + jsonSchemaNumberType = "numberType" + jsonSchemaNullType = "nullType" + ) + switch name { case jsonKindMap: - return xdm.QAnnotation(NSFn, "mapType") + return xdm.QAnnotation(NSFn, jsonSchemaMapType) case jsonKindArray: - return xdm.QAnnotation(NSFn, "arrayType") - case lexicon.TypeString: - return xdm.QAnnotation(NSFn, "stringType") - case lexicon.TypeNumber: - return xdm.QAnnotation(NSFn, "numberType") - case lexicon.TypeBoolean: + return xdm.QAnnotation(NSFn, jsonSchemaArrayType) + case jsonKindString: + return xdm.QAnnotation(NSFn, jsonSchemaStringType) + case jsonKindNumber: + return xdm.QAnnotation(NSFn, jsonSchemaNumberType) + case jsonKindBoolean: return xdm.TypeBoolean case jsonKindNull: - return xdm.QAnnotation(NSFn, "nullType") + return xdm.QAnnotation(NSFn, jsonSchemaNullType) default: return "" } @@ -170,15 +189,15 @@ func jsonToXMLItemName(item xdm.Item) string { case xdm.ArrayItem: return jsonKindArray case xdm.JSONNumber: - return lexicon.TypeNumber + return jsonKindNumber case xdm.AtomicValue: switch value.TypeName { case xdm.TypeString: - return lexicon.TypeString + return jsonKindString case xdm.TypeBoolean: - return lexicon.TypeBoolean + return jsonKindBoolean default: - return lexicon.TypeNumber + return jsonKindNumber } default: return jsonKindNull @@ -235,10 +254,10 @@ func buildJSONToXMLMapEntries(value xdm.MapItem, doc *xdm.Document, opts jsonOpt } keyText := key.StringVal() - _ = child.SetAttribute("key", keyText) + _ = child.SetAttribute(jsonXMLAttributeKey, keyText) // Escaped-key is present only when escape=true and a backslash occurs. if opts.escape && strings.Contains(keyText, "\\") { - _ = child.SetAttribute("escaped-key", lexicon.ValueTrue) + _ = child.SetAttribute(jsonXMLAttributeEscapedKey, lexicon.ValueTrue) } if err := elem.AddChild(child); err != nil { @@ -254,17 +273,17 @@ func appendJSONToXMLAtomic(elem *xdm.Element, value xdm.AtomicValue, opts jsonOp case xdm.TypeString: // Escaped strings carry this attribute only when a backslash occurs. if opts.escape && strings.Contains(value.StringVal(), "\\") { - _ = elem.SetAttribute("escaped", lexicon.ValueTrue) + _ = elem.SetAttribute(jsonXMLAttributeEscaped, lexicon.ValueTrue) } - return appendJSONToXMLText(elem, value.StringVal(), lexicon.TypeString) + return appendJSONToXMLText(elem, value.StringVal(), jsonKindString) case xdm.TypeBoolean: text := lexicon.ValueFalse if value.BooleanVal() { text = lexicon.ValueTrue } - return appendJSONToXMLText(elem, text, lexicon.TypeBoolean) + return appendJSONToXMLText(elem, text, jsonKindBoolean) default: text, err := atomicToString(value) if err != nil { @@ -292,12 +311,18 @@ func jsonSequenceToItem(seq xdm.Sequence) xdm.Item { } func parseJSONToXMLOptions(ctx context.Context, args []xdm.Sequence) (jsonOptions, error) { - opts := jsonOptions{duplicates: nameRetain, retainNumberLexical: true} - if len(args) <= 1 || xdm.SequenceLen(args[1]) == 0 { + // XML mapping alone exposes validation and retains numeric source spelling. + const ( + jsonOptionValidate = "validate" + retainJSONNumberLexical = true + ) + + opts := jsonOptions{duplicates: nameRetain, retainNumberLexical: retainJSONNumberLexical} + if len(args) <= jsonOptionsArgumentThreshold || xdm.SequenceLen(args[1]) == 0 { return opts, nil } - if xdm.SequenceLen(args[1]) != 1 { + if xdm.SequenceLen(args[1]) != jsonOptionsMapCardinality { return opts, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "json-to-xml options must be a single map"} } @@ -308,17 +333,17 @@ func parseJSONToXMLOptions(ctx context.Context, args []xdm.Sequence) (jsonOption var err error - opts.liberal, err = parseJSONBooleanOption(ctx, options, "liberal") + opts.liberal, err = parseJSONBooleanOption(ctx, options, jsonOptionLiberal) if err != nil { return opts, err } - opts.validate, err = parseJSONBooleanOption(ctx, options, "validate") + opts.validate, err = parseJSONBooleanOption(ctx, options, jsonOptionValidate) if err != nil { return opts, err } - opts.escape, err = parseJSONBooleanOption(ctx, options, "escape") + opts.escape, err = parseJSONBooleanOption(ctx, options, jsonOptionEscape) if err != nil { return opts, err } @@ -393,11 +418,13 @@ type xmlJSONMeta struct { } func fnXMLToJSON(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { + const jsonXMLInputCardinality = 1 // nonempty input is a single node + if xdm.SequenceLen(args[0]) == 0 { return nil, nil } - if xdm.SequenceLen(args[0]) != 1 { + if xdm.SequenceLen(args[0]) != jsonXMLInputCardinality { return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "xml-to-json expects zero or one node"} } @@ -426,11 +453,11 @@ func fnXMLToJSON(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) func parseXMLToJSONOptions(ctx context.Context, args []xdm.Sequence) (xmlToJSONOptions, error) { var opts xmlToJSONOptions - if len(args) <= 1 { + if len(args) <= jsonOptionsArgumentThreshold { return opts, nil } - if xdm.SequenceLen(args[1]) != 1 { + if xdm.SequenceLen(args[1]) != jsonOptionsMapCardinality { return opts, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "xml-to-json options must be a single map"} } @@ -441,12 +468,14 @@ func parseXMLToJSONOptions(ctx context.Context, args []xdm.Sequence) (xmlToJSONO var err error - opts.indent, err = parseJSONBooleanOption(ctx, m, "indent") + opts.indent, err = parseJSONBooleanOption(ctx, m, lexicon.SerializationIndent) return opts, err } func xmlToJSONRootElement(node xdm.Node) (*xdm.Element, error) { + const jsonXMLDocumentRootCount = 1 // one document element child + switch n := node.(type) { case *xdm.Document: root := n.DocumentElement() @@ -465,7 +494,7 @@ func xmlToJSONRootElement(node xdm.Node) (*xdm.Element, error) { } } - if elemCount != 1 { + if elemCount != jsonXMLDocumentRootCount { return nil, &xdm.Error{Code: errCodeFOJS0006, Message: "xml-to-json: document must have exactly one element child"} } @@ -497,17 +526,17 @@ func serializeJSONXMLContent(elem *xdm.Element, meta xmlJSONMeta, opts xmlToJSON return serializeJSONXMLMap(elem, opts, depth) case jsonKindArray: return serializeJSONXMLArray(elem, opts, depth) - case lexicon.TypeString: + case jsonKindString: return serializeJSONXMLString(elem, meta.escaped) - case lexicon.TypeNumber: - content, err := scalarElementText(elem, true) + case jsonKindNumber: + content, err := scalarElementText(elem, jsonScalarCommentsAllowed) if err != nil { return "", err } return canonicalizeXMLJSONNumber(content) - case fnNameBoolean: - content, err := scalarElementText(elem, true) + case jsonKindBoolean: + content, err := scalarElementText(elem, jsonScalarCommentsAllowed) if err != nil { return "", err } @@ -525,7 +554,9 @@ func serializeJSONXMLContent(elem *xdm.Element, meta xmlJSONMeta, opts xmlToJSON } func serializeJSONXMLMap(elem *xdm.Element, opts xmlToJSONOptions, depth int) (string, error) { - children, err := jsonElementChildren(elem, true) + const jsonMapUsesMapRules = true // select the parent-kind validation mode + + children, err := jsonElementChildren(elem, jsonMapUsesMapRules) if err != nil { return "", err } @@ -555,7 +586,9 @@ func serializeJSONXMLMap(elem *xdm.Element, opts xmlToJSONOptions, depth int) (s } func serializeJSONXMLArray(elem *xdm.Element, opts xmlToJSONOptions, depth int) (string, error) { - children, err := jsonElementChildren(elem, false) + const jsonArrayUsesMapRules = false // array children do not use map rules + + children, err := jsonElementChildren(elem, jsonArrayUsesMapRules) if err != nil { return "", err } @@ -574,7 +607,7 @@ func serializeJSONXMLArray(elem *xdm.Element, opts xmlToJSONOptions, depth int) } func serializeJSONXMLString(elem *xdm.Element, escaped bool) (string, error) { - content, err := scalarElementText(elem, true) + content, err := scalarElementText(elem, jsonScalarCommentsAllowed) if err != nil { return "", err } @@ -599,7 +632,7 @@ func parseXMLJSONMeta(elem *xdm.Element, position xmlJSONPosition) (xmlJSONMeta, meta := xmlJSONMeta{kind: elem.LocalName()} switch meta.kind { - case jsonKindMap, jsonKindArray, lexicon.TypeString, lexicon.TypeNumber, fnNameBoolean, jsonKindNull: + case jsonKindMap, jsonKindArray, jsonKindString, jsonKindNumber, jsonKindBoolean, jsonKindNull: default: return xmlJSONMeta{}, &xdm.Error{Code: errCodeFOJS0006, Message: fmt.Sprintf("xml-to-json: invalid element %q", meta.kind)} } @@ -631,7 +664,7 @@ func parseXMLJSONAttribute(meta *xmlJSONMeta, attr *xdm.Attribute, position xmlJ switch attr.URI() { case "": switch attr.LocalName() { - case "key": + case jsonXMLAttributeKey: if position == xmlJSONArrayMember { return &xdm.Error{Code: errCodeFOJS0006, Message: "xml-to-json: array child cannot have a key attribute"} } @@ -642,8 +675,8 @@ func parseXMLJSONAttribute(meta *xmlJSONMeta, attr *xdm.Attribute, position xmlJ meta.hasKey = true meta.keyActual = attr.Value() - case "escaped": - if meta.kind != lexicon.TypeString { + case jsonXMLAttributeEscaped: + if meta.kind != jsonKindString { return &xdm.Error{Code: errCodeFOJS0006, Message: "xml-to-json: escaped is permitted only on a string element"} } @@ -653,7 +686,7 @@ func parseXMLJSONAttribute(meta *xmlJSONMeta, attr *xdm.Attribute, position xmlJ } meta.escaped = v - case "escaped-key": + case jsonXMLAttributeEscapedKey: if position == xmlJSONArrayMember { return &xdm.Error{Code: errCodeFOJS0006, Message: "xml-to-json: array child cannot have an escaped-key attribute"} } @@ -688,7 +721,7 @@ func jsonElementChildren(elem *xdm.Element, insideMap bool) ([]*xdm.Element, err case *xdm.Element: children = append(children, v) case *xdm.Text, *xdm.CDATASection: - if strings.Trim(xdm.CharacterData(child), " \t\r\n") != "" { + if strings.Trim(xdm.CharacterData(child), lexicon.XMLWhitespace) != "" { kind := jsonKindArray if insideMap { kind = jsonKindMap @@ -745,10 +778,10 @@ func validateNullElement(elem *xdm.Element) error { } func parseXMLJSONBooleanAttr(s string) (bool, error) { - switch strings.Trim(s, " \t\r\n") { - case lexicon.ValueTrue, "1": + switch strings.Trim(s, lexicon.XMLWhitespace) { + case lexicon.ValueTrue, booleanTrueDigit: return true, nil - case lexicon.ValueFalse, "0": + case lexicon.ValueFalse, booleanFalseDigit: return false, nil default: return false, &xdm.Error{Code: errCodeFOJS0006, Message: fmt.Sprintf("xml-to-json: invalid boolean attribute value %q", s)} @@ -756,10 +789,10 @@ func parseXMLJSONBooleanAttr(s string) (bool, error) { } func canonicalizeXMLJSONBoolean(s string) (string, error) { - switch strings.Trim(s, " \t\r\n") { - case lexicon.ValueTrue, "1": + switch strings.Trim(s, lexicon.XMLWhitespace) { + case lexicon.ValueTrue, booleanTrueDigit: return lexicon.ValueTrue, nil - case lexicon.ValueFalse, "0": + case lexicon.ValueFalse, booleanFalseDigit: return lexicon.ValueFalse, nil default: return "", &xdm.Error{Code: errCodeFOJS0006, Message: fmt.Sprintf("xml-to-json: invalid boolean value %q", s)} @@ -767,71 +800,25 @@ func canonicalizeXMLJSONBoolean(s string) (string, error) { } func canonicalizeXMLJSONNumber(s string) (string, error) { - norm := strings.Trim(s, " \t\r\n") + const jsonNonFiniteNaNSpelling = "nan" // exact existing case-insensitive spelling + + norm := strings.Trim(s, lexicon.XMLWhitespace) if norm == "" { return "", &xdm.Error{Code: errCodeFOJS0006, Message: "xml-to-json: invalid number value"} } - if strings.EqualFold(norm, "nan") || strings.Contains(strings.ToUpper(norm), "INF") { + if strings.EqualFold(norm, jsonNonFiniteNaNSpelling) || strings.Contains(strings.ToUpper(norm), lexicon.FloatINF) { return "", &xdm.Error{Code: errCodeFOJS0006, Message: fmt.Sprintf("xml-to-json: invalid number value %q", s)} } - norm = preprocessXMLJSONNumber(norm) - av, err := CastFromString(norm, xdm.TypeDouble) - if err != nil || math.IsInf(av.DoubleVal(), 0) || math.IsNaN(av.DoubleVal()) { + if err != nil || math.IsInf(av.DoubleVal(), eitherInfinitySign) || math.IsNaN(av.DoubleVal()) { return "", &xdm.Error{Code: errCodeFOJS0006, Message: fmt.Sprintf("xml-to-json: invalid number value %q", s)} } return formatXPathDouble(av.DoubleVal()), nil } -func preprocessXMLJSONNumber(s string) string { - sign := "" - if strings.HasPrefix(s, "+") || strings.HasPrefix(s, "-") { - sign = s[:1] - s = s[1:] - } - - exp := "" - if idx := strings.IndexAny(s, "eE"); idx >= 0 { - exp = s[idx:] - s = s[:idx] - } - - if strings.HasPrefix(s, ".") { - s = "0" + s - } - - s = strings.TrimSuffix(s, ".") - if s == "" { - s = "0" - } - - intPart := s - fracPart := "" - - if before, after, ok := strings.Cut(s, "."); ok { - intPart = before - fracPart = after - } - - if intPart == "" { - intPart = "0" - } - - intPart = strings.TrimLeft(intPart, "0") - if intPart == "" { - intPart = "0" - } - - if fracPart != "" { - return sign + intPart + "." + fracPart + exp - } - - return sign + intPart + exp -} - func validateEscapedJSONContent(s string) (string, string, error) { var ( out strings.Builder @@ -858,42 +845,42 @@ func validateEscapedJSONContent(s string) (string, string, error) { out.WriteString(`\"`) decoded.WriteByte('"') - i += 2 + i += jsonShortEscapeBytes case '\\': out.WriteString(`\\`) decoded.WriteByte('\\') - i += 2 + i += jsonShortEscapeBytes case '/': out.WriteString(`\/`) decoded.WriteByte('/') - i += 2 + i += jsonShortEscapeBytes case 'b': out.WriteString(`\b`) decoded.WriteByte('\b') - i += 2 + i += jsonShortEscapeBytes case 'f': out.WriteString(`\f`) decoded.WriteByte('\f') - i += 2 + i += jsonShortEscapeBytes case 'n': out.WriteString(`\n`) decoded.WriteByte('\n') - i += 2 + i += jsonShortEscapeBytes case 'r': out.WriteString(`\r`) decoded.WriteByte('\r') - i += 2 + i += jsonShortEscapeBytes case 't': out.WriteString(`\t`) decoded.WriteByte('\t') - i += 2 + i += jsonShortEscapeBytes case 'u': encoded, actual, consumed, err := parseEscapedUnicodeSequence(s[i:]) if err != nil { @@ -913,40 +900,40 @@ func validateEscapedJSONContent(s string) (string, string, error) { } func parseEscapedUnicodeSequence(s string) (string, string, int, error) { - if len(s) < 6 { + if len(s) < jsonCodeUnitEscapeBytes { return "", "", 0, &xdm.Error{Code: errCodeFOJS0007, Message: "xml-to-json: incomplete \\u escape"} } - if s[0] != '\\' || s[1] != 'u' || !isHexQuad(s[2:6]) { + if s[0] != '\\' || s[1] != 'u' || !isHexQuad(s[jsonCodeUnitEscapePrefixBytes:jsonCodeUnitEscapeBytes]) { return "", "", 0, &xdm.Error{Code: errCodeFOJS0007, Message: "xml-to-json: invalid \\u escape"} } - cp, _ := parseJSONHexEscape(s[2:6]) - encoded := s[:6] + cp, _ := parseJSONHexEscape(s[jsonCodeUnitEscapePrefixBytes:jsonCodeUnitEscapeBytes]) + encoded := s[:jsonCodeUnitEscapeBytes] switch { - case cp >= 0xD800 && cp <= 0xDBFF: - if len(s) < 12 || s[6] != '\\' || s[7] != 'u' || !isHexQuad(s[8:12]) { + case cp >= jsonHighSurrogateFirst && cp <= jsonHighSurrogateLast: + if len(s) < jsonSurrogatePairEscapeBytes || s[jsonCodeUnitEscapeBytes] != '\\' || s[jsonLowSurrogateMarkerOffset] != 'u' || !isHexQuad(s[jsonLowSurrogateHexStart:jsonSurrogatePairEscapeBytes]) { return "", "", 0, &xdm.Error{Code: errCodeFOJS0007, Message: "xml-to-json: invalid surrogate pair"} } - cp2, _ := parseJSONHexEscape(s[8:12]) - if cp2 < 0xDC00 || cp2 > 0xDFFF { + cp2, _ := parseJSONHexEscape(s[jsonLowSurrogateHexStart:jsonSurrogatePairEscapeBytes]) + if cp2 < jsonLowSurrogateFirst || cp2 > jsonLowSurrogateLast { return "", "", 0, &xdm.Error{Code: errCodeFOJS0007, Message: "xml-to-json: invalid surrogate pair"} } - r := rune(((cp - 0xD800) << 10) + (cp2 - 0xDC00) + 0x10000) + r := rune(((cp - jsonHighSurrogateFirst) << jsonSurrogatePayloadBits) + (cp2 - jsonLowSurrogateFirst) + jsonSupplementaryPlaneFirst) - return encoded + s[6:12], string(r), 12, nil - case cp >= 0xDC00 && cp <= 0xDFFF: + return encoded + s[jsonCodeUnitEscapeBytes:jsonSurrogatePairEscapeBytes], string(r), jsonSurrogatePairEscapeBytes, nil + case cp >= jsonLowSurrogateFirst && cp <= jsonLowSurrogateLast: return "", "", 0, &xdm.Error{Code: errCodeFOJS0007, Message: "xml-to-json: invalid low surrogate"} default: - return encoded, string(rune(cp)), 6, nil // #nosec G115 -- cp comes from exactly four validated hexadecimal digits. + return encoded, string(rune(cp)), jsonCodeUnitEscapeBytes, nil // #nosec G115 -- cp comes from exactly four validated hexadecimal digits. } } func isHexQuad(s string) bool { - if len(s) != 4 { + if len(s) != jsonCodeUnitHexDigits { return false } @@ -973,14 +960,16 @@ func encodeJSONStringContent(s string) string { } func formatJSONComposite(open, closeBracket string, parts []string, depth int, indent bool) string { + const jsonIndentUnit = " " // two spaces per nesting level + if !indent || len(parts) == 0 { return open + strings.Join(parts, ",") + closeBracket } var b strings.Builder - childIndent := strings.Repeat(" ", depth+1) - parentIndent := strings.Repeat(" ", depth) + childIndent := strings.Repeat(jsonIndentUnit, depth+1) + parentIndent := strings.Repeat(jsonIndentUnit, depth) b.WriteString(open) b.WriteByte('\n') @@ -1002,8 +991,10 @@ func formatJSONComposite(open, closeBracket string, parts []string, depth int, i } func valueSeparator(indent bool) string { + const jsonIndentedValueSeparator = " " // one space after an indented member colon + if indent { - return " " + return jsonIndentedValueSeparator } return "" diff --git a/transform/internal/xpath/functions_map.go b/transform/internal/xpath/functions_map.go index 032b4de..1a68e31 100644 --- a/transform/internal/xpath/functions_map.go +++ b/transform/internal/xpath/functions_map.go @@ -12,34 +12,46 @@ import ( ) func lookupFunctionsMap(name string) (*builtinFunc, bool) { + // Local dispatch names belong to the map namespace, independently of fn: and array:. + const ( + fnNameMapMerge = "merge" + fnNameMapSize = "size" + fnNameMapKeys = "keys" + fnNameMapGet = "get" + fnNameMapPut = "put" + fnNameMapEntry = "entry" + fnNameMapFind = "find" + fnNameMapContains = "contains" + ) + switch name { - case "merge": - return &builtinFunc{name: name, min: 1, max: 2, fn: fnMapMerge}, true - case "size": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnMapSize}, true - case "keys": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnMapKeys}, true - case fnNameContains: - return &builtinFunc{name: name, min: 2, max: 2, fn: fnMapContains}, true - case "get": - return &builtinFunc{name: name, min: 2, max: 2, fn: fnMapGet}, true - case "put": - return &builtinFunc{name: name, min: 3, max: 3, fn: fnMapPut}, true - case "entry": - return &builtinFunc{name: name, min: 2, max: 2, fn: fnMapEntry}, true + case fnNameMapMerge: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityTwo, fn: fnMapMerge}, true + case fnNameMapSize: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnMapSize}, true + case fnNameMapKeys: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnMapKeys}, true + case fnNameMapContains: + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityTwo, fn: fnMapContains}, true + case fnNameMapGet: + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityTwo, fn: fnMapGet}, true + case fnNameMapPut: + return &builtinFunc{name: name, min: builtinArityThree, max: builtinArityThree, fn: fnMapPut}, true + case fnNameMapEntry: + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityTwo, fn: fnMapEntry}, true case nameRemove: - return &builtinFunc{name: name, min: 2, max: 2, fn: fnMapRemove}, true + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityTwo, fn: fnMapRemove}, true case nameForEach: - return &builtinFunc{name: name, min: 2, max: 2, fn: fnMapForEach}, true - case "find": - return &builtinFunc{name: name, min: 2, max: 2, fn: fnMapFind}, true + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityTwo, fn: fnMapForEach}, true + case fnNameMapFind: + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityTwo, fn: fnMapFind}, true } return nil, false } func extractMap(seq xdm.Sequence) (xdm.MapItem, error) { - if xdm.SequenceLen(seq) != 1 { + if xdm.SequenceLen(seq) != singletonFunctionArgument { return xdm.MapItem{}, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "expected single map"} } @@ -52,14 +64,20 @@ func extractMap(seq xdm.Sequence) (xdm.MapItem, error) { } func fnMapMerge(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { + // Additional duplicates policies supplement the existing shared options. + const ( + mapDuplicatesUseAny = "use-any" + mapDuplicatesCombine = "combine" + ) + duplicates := xdm.MergeUseFirst - if len(args) > 1 { + if len(args) > builtinArityOne { if xdm.SequenceLen(args[1]) == 0 { return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "map:merge: options argument must be a map, got empty sequence"} } - if xdm.SequenceLen(args[1]) > 1 { + if xdm.SequenceLen(args[1]) > singletonFunctionArgument { return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "map:merge: options argument must be a single map"} } // The options map should contain "duplicates" key @@ -85,13 +103,13 @@ func fnMapMerge(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) duplicates = xdm.MergeUseFirst case nameUseLast: duplicates = xdm.MergeUseLast - case "use-any": + case mapDuplicatesUseAny: // use-any allows the implementation to pick any value; // reusing use-first behavior is conformant. duplicates = xdm.MergeUseFirst case duplicatesReject: duplicates = xdm.MergeReject - case "combine": + case mapDuplicatesCombine: duplicates = xdm.MergeCombine default: return nil, &xdm.Error{Code: errCodeFOJS0005, Message: fmt.Sprintf("map:merge: invalid value for 'duplicates' option: %q", s)} @@ -164,7 +182,7 @@ func prepareMergedValue(ctx context.Context, ec *evalContext, lengths map[xdm.Ma } total := previous + incoming - if err := fnCountOps(ctx, ec, max(1, incoming)); err != nil { + if err := fnCountOps(ctx, ec, max(minimumMapValueWork, incoming)); err != nil { return err } @@ -238,7 +256,7 @@ func fnMapGet(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "map:get requires a key argument"} } - if xdm.SequenceLen(args[1]) > 1 { + if xdm.SequenceLen(args[1]) > singletonFunctionArgument { return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "map:get key must be a single atomic value"} } @@ -262,7 +280,7 @@ func fnMapGet(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { return appendBoundedClonedSeq(ctx, ec, nil, val, fnMaxNodes(ec)) } -func fnMapPut(_ context.Context, args []xdm.Sequence) (xdm.Sequence, error) { +func fnMapPut(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { m, err := extractMap(args[0]) if err != nil { return nil, err @@ -273,6 +291,15 @@ func fnMapPut(_ context.Context, args []xdm.Sequence) (xdm.Sequence, error) { return nil, err } + ec := getFnContext(ctx) + if m.Size() >= fnMaxNodes(ec) && !m.Contains(ka) { + return nil, ErrNodeSetLimit + } + + if err := fnCountOps(ctx, ec, max(minimumMapValueWork, xdm.SequenceLen(args[2]))); err != nil { + return nil, err + } + return xdm.ItemSlice{m.Put(ka, args[2])}, nil } @@ -329,7 +356,7 @@ func fnMapForEach(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error return ErrNodeSetLimit } - if err := fnCountOps(ctx, ec, max(vLen, 1)); err != nil { + if err := fnCountOps(ctx, ec, max(vLen, minimumMapValueWork)); err != nil { return err } diff --git a/transform/internal/xpath/functions_math.go b/transform/internal/xpath/functions_math.go index dab3103..8de0c99 100644 --- a/transform/internal/xpath/functions_math.go +++ b/transform/internal/xpath/functions_math.go @@ -11,36 +11,54 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) +// Math namespace dispatch uses standard function local names, without prefixes. +const ( + fnNameMathPi = "pi" + fnNameMathExp = "exp" + fnNameMathExp10 = "exp10" + fnNameMathLog = "log" + fnNameMathLog10 = "log10" + fnNameMathPow = "pow" + fnNameMathSqrt = "sqrt" + fnNameMathSin = "sin" + fnNameMathCos = "cos" + fnNameMathTan = "tan" + fnNameMathAsin = "asin" + fnNameMathAcos = "acos" + fnNameMathAtan = "atan" + fnNameMathAtan2 = "atan2" +) + func lookupFunctionsMath(name string) (*builtinFunc, bool) { switch name { - case "pi": - return &builtinFunc{name: name, min: 0, max: 0, fn: fnMathPi}, true - case "exp": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnMathExp}, true - case "exp10": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnMathExp10}, true - case "log": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnMathLog}, true - case "log10": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnMathLog10}, true - case "pow": - return &builtinFunc{name: name, min: 2, max: 2, fn: fnMathPow}, true - case "sqrt": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnMathSqrt}, true - case "sin": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnMathSin}, true - case "cos": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnMathCos}, true - case "tan": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnMathTan}, true - case "asin": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnMathAsin}, true - case "acos": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnMathAcos}, true - case "atan": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnMathAtan}, true - case "atan2": - return &builtinFunc{name: name, min: 2, max: 2, fn: fnMathAtan2}, true + case fnNameMathPi: + return &builtinFunc{name: name, min: builtinArityZero, max: builtinArityZero, fn: fnMathPi}, true + case fnNameMathExp: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnMathExp}, true + case fnNameMathExp10: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnMathExp10}, true + case fnNameMathLog: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnMathLog}, true + case fnNameMathLog10: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnMathLog10}, true + case fnNameMathPow: + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityTwo, fn: fnMathPow}, true + case fnNameMathSqrt: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnMathSqrt}, true + case fnNameMathSin: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnMathSin}, true + case fnNameMathCos: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnMathCos}, true + case fnNameMathTan: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnMathTan}, true + case fnNameMathAsin: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnMathAsin}, true + case fnNameMathAcos: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnMathAcos}, true + case fnNameMathAtan: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnMathAtan}, true + case fnNameMathAtan2: + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityTwo, fn: fnMathAtan2}, true } return nil, false @@ -85,7 +103,10 @@ func fnMathExp(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { } func fnMathExp10(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { - return mathUnary(ctx, args, func(x float64) float64 { return math.Pow(10, x) }) + // This function raises decimal base ten to its argument. + const mathDecimalExponentBase = 10 + + return mathUnary(ctx, args, func(x float64) float64 { return math.Pow(mathDecimalExponentBase, x) }) } func fnMathLog(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { @@ -136,6 +157,14 @@ func fnMathPow(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { // F&O 4.8.6 without first rounding the exponent to xs:double. The wider working // precision also avoids accumulating binary64 rounding at every multiplication. func mathIntegerPower(ctx context.Context, base float64, exponent *big.Int) (float64, error) { + // Working precision is in bits; surrogate exponents retain odd/even signs + // for zero, infinite and unit bases without converting the full exponent. + const ( + mathIntegerPowerPrecision = 256 + mathOddExponentSurrogate = 1 + mathEvenExponentSurrogate = 2.0 + ) + if err := fnCountOp(ctx, getFnContext(ctx)); err != nil { return 0, err } @@ -150,9 +179,9 @@ func mathIntegerPower(ctx context.Context, base float64, exponent *big.Int) (flo } if base == 0 || math.IsInf(base, 0) || math.Abs(base) == 1 { - surrogate := 2.0 + surrogate := mathEvenExponentSurrogate if negative { - surrogate = 1 + surrogate = mathOddExponentSurrogate } if exponent.Sign() < 0 { @@ -162,13 +191,13 @@ func mathIntegerPower(ctx context.Context, base float64, exponent *big.Int) (flo return math.Pow(base, surrogate), nil } - if err := preflightNumeric(ctx, exponent.BitLen(), 256); err != nil { + if err := preflightNumeric(ctx, exponent.BitLen(), mathIntegerPowerPrecision); err != nil { return 0, err } magnitude := new(big.Int).Abs(exponent) - factor := new(big.Float).SetPrec(256).SetFloat64(math.Abs(base)) - result := new(big.Float).SetPrec(256).SetInt64(1) + factor := new(big.Float).SetPrec(mathIntegerPowerPrecision).SetFloat64(math.Abs(base)) + result := new(big.Float).SetPrec(mathIntegerPowerPrecision).SetInt64(1) if exponent.Sign() < 0 { factor.Quo(result, factor) diff --git a/transform/internal/xpath/functions_misc.go b/transform/internal/xpath/functions_misc.go index 18055dc..c453a92 100644 --- a/transform/internal/xpath/functions_misc.go +++ b/transform/internal/xpath/functions_misc.go @@ -19,32 +19,49 @@ import ( //nolint:gochecknoglobals // One immutable process seed hashes canonical atomic values consistently. var randomSeedHash = sync.OnceValue(maphash.MakeSeed) +// Dynamic-context accessors use standard local names. The RNG map keys are +// F&O result members; its number key has no XML Schema type-name role. +const ( + fnNameStaticBaseURI = "static-base-uri" + fnNameDefaultCollation = "default-collation" + fnNameCurrentDate = "current-date" + fnNameCurrentTime = "current-time" + fnNameImplicitTimezone = "implicit-timezone" + rngNumberKey = "number" + rngNextKey = "next" + rngPermuteKey = "permute" + errCodeFOQM0006 = "FOQM0006" +) + +// Zero clock fields select civil midnight while retaining the implicit zone. +const currentDateMidnightField = 0 + func lookupFunctionsMisc(name string) (*builtinFunc, bool) { switch name { - case "static-base-uri": - return &builtinFunc{name: name, min: 0, max: 0, fn: fnStaticBaseURI}, true - case "default-collation": - return &builtinFunc{name: name, min: 0, max: 0, fn: fnDefaultCollation}, true - case "available-environment-variables": - return &builtinFunc{name: name, min: 0, max: 0, fn: fnAvailableEnvVars}, true - case "environment-variable": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnEnvironmentVariable}, true - case "current-dateTime": - return &builtinFunc{name: name, min: 0, max: 0, fn: fnCurrentDateTime}, true - case "current-date": - return &builtinFunc{name: name, min: 0, max: 0, fn: fnCurrentDate}, true - case "current-time": - return &builtinFunc{name: name, min: 0, max: 0, fn: fnCurrentTime}, true - case "implicit-timezone": - return &builtinFunc{name: name, min: 0, max: 0, fn: fnImplicitTimezone}, true - case "default-language": - return &builtinFunc{name: name, min: 0, max: 0, fn: fnDefaultLanguage}, true - case "random-number-generator": - return &builtinFunc{name: name, min: 0, max: 1, fn: fnRandomNumberGenerator}, true + case fnNameStaticBaseURI: + return &builtinFunc{name: name, min: builtinArityZero, max: builtinArityZero, fn: fnStaticBaseURI}, true + case fnNameDefaultCollation: + return &builtinFunc{name: name, min: builtinArityZero, max: builtinArityZero, fn: fnDefaultCollation}, true + case fnNameAvailableEnvironmentVariables: + return &builtinFunc{name: name, min: builtinArityZero, max: builtinArityZero, fn: fnAvailableEnvVars}, true + case fnNameEnvironmentVariable: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnEnvironmentVariable}, true + case fnNameCurrentDateTime: + return &builtinFunc{name: name, min: builtinArityZero, max: builtinArityZero, fn: fnCurrentDateTime}, true + case fnNameCurrentDate: + return &builtinFunc{name: name, min: builtinArityZero, max: builtinArityZero, fn: fnCurrentDate}, true + case fnNameCurrentTime: + return &builtinFunc{name: name, min: builtinArityZero, max: builtinArityZero, fn: fnCurrentTime}, true + case fnNameImplicitTimezone: + return &builtinFunc{name: name, min: builtinArityZero, max: builtinArityZero, fn: fnImplicitTimezone}, true + case fnNameDefaultLanguage: + return &builtinFunc{name: name, min: builtinArityZero, max: builtinArityZero, fn: fnDefaultLanguage}, true + case fnNameRandomNumberGenerator: + return &builtinFunc{name: name, min: builtinArityZero, max: builtinArityOne, fn: fnRandomNumberGenerator}, true case nameLoadXQueryModule: - return &builtinFunc{name: name, min: 1, max: 2, fn: fnLoadXQueryModule}, true - case "transform": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnTransform}, true + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityTwo, fn: fnLoadXQueryModule}, true + case fnNameTransform: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnTransform}, true } return nil, false @@ -105,7 +122,7 @@ func fnCurrentDateTime(ctx context.Context, _ []xdm.Sequence) (xdm.Sequence, err now := currentTimeFromCtx(ctx) typeName := xdm.TypeDateTime - if XMLSchemaVersion(ctx) == 11 { + if XMLSchemaVersion(ctx) == schemaEdition11 { typeName = xdm.TypeDateTimeStamp } @@ -114,7 +131,7 @@ func fnCurrentDateTime(ctx context.Context, _ []xdm.Sequence) (xdm.Sequence, err func fnCurrentDate(ctx context.Context, _ []xdm.Sequence) (xdm.Sequence, error) { now := currentTimeFromCtx(ctx) - date := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) + date := time.Date(now.Year(), now.Month(), now.Day(), currentDateMidnightField, currentDateMidnightField, currentDateMidnightField, currentDateMidnightField, now.Location()) return xdm.SingleAtomic(xdm.AtomicValue{TypeName: xdm.TypeDate, Value: date}), nil } @@ -175,18 +192,35 @@ func makeRNGMap(seed int64) xdm.MapItem { }} nextFn := xdm.FunctionItem{ - Arity: 0, ReturnType: &generatorType, + Arity: builtinArityZero, ReturnType: &generatorType, Invoke: func(_ context.Context, _ []xdm.Sequence) (xdm.Sequence, error) { return xdm.ItemSlice{makeRNGMap(nextSeed)}, nil }, } permuteFn := xdm.FunctionItem{ - Arity: 1, ParamTypes: []xdm.SequenceType{itemsType}, ReturnType: &itemsType, - Invoke: func(_ context.Context, callArgs []xdm.Sequence) (xdm.Sequence, error) { + Arity: builtinArityOne, ParamTypes: []xdm.SequenceType{itemsType}, ReturnType: &itemsType, + Invoke: func(ctx context.Context, callArgs []xdm.Sequence) (xdm.Sequence, error) { seq := callArgs[0] - perm := make(xdm.ItemSlice, xdm.SequenceLen(seq)) - copy(perm, xdm.Materialize(seq)) + ec := getFnContext(ctx) + + length := xdm.SequenceLen(seq) + if length > fnMaxNodes(ec) { + return nil, ErrNodeSetLimit + } + + if err := fnCountOps(ctx, ec, length); err != nil { + return nil, err + } + + perm := make(xdm.ItemSlice, length) + for i := range length { + if err := ctx.Err(); err != nil { + return nil, err + } + + perm[i] = seq.Get(i) + } localRng := rand.New(rand.NewSource(seed)) // #nosec G404 -- XPath permutation repeats the generator seed; cryptographic randomness is not required. @@ -194,14 +228,14 @@ func makeRNGMap(seed int64) xdm.MapItem { perm[i], perm[j] = perm[j], perm[i] }) - return perm, nil + return perm, ctx.Err() }, } return xdm.NewMap([]xdm.MapEntry{ - {Key: xdm.AtomicValue{TypeName: xdm.TypeString, Value: lexicon.TypeNumber}, Value: xdm.ItemSlice{xdm.AtomicValue{TypeName: xdm.TypeDouble, Value: xdm.NewDouble(number)}}}, - {Key: xdm.AtomicValue{TypeName: xdm.TypeString, Value: "next"}, Value: xdm.ItemSlice{nextFn}}, - {Key: xdm.AtomicValue{TypeName: xdm.TypeString, Value: "permute"}, Value: xdm.ItemSlice{permuteFn}}, + {Key: xdm.AtomicValue{TypeName: xdm.TypeString, Value: rngNumberKey}, Value: xdm.ItemSlice{xdm.AtomicValue{TypeName: xdm.TypeDouble, Value: xdm.NewDouble(number)}}}, + {Key: xdm.AtomicValue{TypeName: xdm.TypeString, Value: rngNextKey}, Value: xdm.ItemSlice{nextFn}}, + {Key: xdm.AtomicValue{TypeName: xdm.TypeString, Value: rngPermuteKey}, Value: xdm.ItemSlice{permuteFn}}, }) } @@ -215,7 +249,7 @@ func fnDefaultLanguage(ctx context.Context, _ []xdm.Sequence) (xdm.Sequence, err return xdm.SingleAtomic(xdm.AtomicValue{ TypeName: xdm.TypeLanguage, - Value: "en", + Value: defaultFormattingLanguage, }), nil } @@ -241,11 +275,11 @@ func fnImplicitTimezone(ctx context.Context, _ []xdm.Sequence) (xdm.Sequence, er d.Negative = true } - return xdm.SingleAtomic(xdm.AtomicValue{TypeName: xdm.TypeDayTimeDuration, Value: d}), nil + return boundedDurationResult(ctx, xdm.AtomicValue{TypeName: xdm.TypeDayTimeDuration, Value: d}) } func fnLoadXQueryModule(_ context.Context, _ []xdm.Sequence) (xdm.Sequence, error) { - return nil, &xdm.Error{Code: "FOQM0006", Message: "the selected profile has no XQuery processor"} + return nil, &xdm.Error{Code: errCodeFOQM0006, Message: "the selected profile has no XQuery processor"} } func fnTransform(_ context.Context, _ []xdm.Sequence) (xdm.Sequence, error) { diff --git a/transform/internal/xpath/functions_node.go b/transform/internal/xpath/functions_node.go index e8b303f..ff5562a 100644 --- a/transform/internal/xpath/functions_node.go +++ b/transform/internal/xpath/functions_node.go @@ -18,58 +18,160 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) +// Standard function local names shared by dispatch and document/motion lookup. +const ( + fnNameBaseURI = "base-uri" + fnNameDoc = "doc" + fnNameCollection = "collection" +) + +// Collection identity conflicts use the standard FODC0003 category. Visiting an +// owner, collection item or document snapshot charges one logical operation. +const ( + errCodeFODC0003 = "FODC0003" + nodeFunctionVisitWork = 1 +) + +// fn:path sibling predicates number matching siblings from one, independently +// of the zero-based backing child/index representation. +const nodePathFirstSiblingPosition = 1 + +// NodeBaseURI applies the XDM base-uri accessor, including xml:base and +// construction-time bases. Parentless non-element nodes have no base URI. +func NodeBaseURI(n xdm.Node) string { + if xdm.IsNilNode(n) { + return "" + } + // Namespace nodes have no base URI per the XPath data model. + if n.Type() == xdm.NamespaceNode { + return "" + } + // Per XDM: attribute, text, comment, and PI nodes derive their base URI + // from their parent element. If they have no parent, their base URI is + // the empty sequence. + nodeType := n.Type() + if nodeType == xdm.AttributeNode || nodeType == xdm.TextNode || + nodeType == xdm.CommentNode || nodeType == xdm.ProcessingInstructionNode { + if n.Parent() == nil { + return "" + } + } + // Walk up the parent chain to find the actual document the node lives + // in. OwnerDocument() may return a different document when nodes are + // created in one document and then moved to another (e.g. in XSLT + // xsl:result-document). + var doc *xdm.Document + if d, ok := n.(*xdm.Document); ok { + doc = d + } else { + cur := n + for cur.Parent() != nil { + cur = cur.Parent() + } + + if d, ok := cur.(*xdm.Document); ok { + doc = d + } else { + doc = n.OwnerDocument() + } + } + + return xdm.NodeGetBase(doc, n) +} + +// DocumentLoadSnapshot retains an available-document outcome in one host +// invocation. scope identifies the host's document pool; key includes its +// package/preprocessing view. Callers must check live read policy before this +// function, including on a hit. Controls and policy failures are never retained. +func DocumentLoadSnapshot(ctx context.Context, scope any, key string, load func() (*xdm.Document, error)) (*xdm.Document, error) { + store, _ := InvocationState(ctx, documentSnapshotKey{scope: scope}, func() any { + return &documentSnapshotStore{} + }).(*documentSnapshotStore) + + return loadDocumentSnapshot(ctx, store, key, load) +} + +// ResolveDocumentURI applies fn:doc's URI rules in the calling static context. +// Hosts overriding retrieval must retain these rules, including fragment rejection. +func ResolveDocumentURI(ctx context.Context, uri string) (string, error) { + if strings.Contains(uri, "#") { + return "", &xdm.Error{Code: errCodeFODC0005, Message: "fn:doc: URI must not contain a fragment identifier"} + } + + resolved, err := resolveDocURI(ctx, uri) + if err != nil { + return "", err + } + // An empty argument resolves to the base URI verbatim, which may itself + // carry a fragment identifier. Re-check the resolved URI so doc("") with a + // fragmented base URI is rejected the same as a fragmented argument. + if strings.Contains(resolved, "#") { + return "", &xdm.Error{Code: errCodeFODC0005, Message: "fn:doc: URI must not contain a fragment identifier"} + } + + return resolved, nil +} + func lookupFunctionsNode(name string) (*builtinFunc, bool) { + // These names complete the node-function signatures in the standard fn: namespace. + const ( + fnNameRoot = "root" + fnNameDocAvailable = "doc-available" + fnNameIDRef = "idref" + fnNameElementWithID = "element-with-id" + ) + switch name { case nameNodeName: - return &builtinFunc{name: name, min: 0, max: 1, fn: fnNodeName}, true + return &builtinFunc{name: name, min: builtinArityZero, max: builtinArityOne, fn: fnNodeName}, true case fnNameNilled: - return &builtinFunc{name: name, min: 0, max: 1, fn: fnNilled}, true - case "data": - return &builtinFunc{name: name, min: 0, max: 1, fn: fnData}, true - case "base-uri": - return &builtinFunc{name: name, min: 0, max: 1, fn: fnBaseURI}, true + return &builtinFunc{name: name, min: builtinArityZero, max: builtinArityOne, fn: fnNilled}, true + case fnNameData: + return &builtinFunc{name: name, min: builtinArityZero, max: builtinArityOne, fn: fnData}, true + case fnNameBaseURI: + return &builtinFunc{name: name, min: builtinArityZero, max: builtinArityOne, fn: fnBaseURI}, true case fnNameDocumentURI: - return &builtinFunc{name: name, min: 0, max: 1, fn: fnDocumentURI}, true - case "root": - return &builtinFunc{name: name, min: 0, max: 1, fn: fnRoot}, true + return &builtinFunc{name: name, min: builtinArityZero, max: builtinArityOne, fn: fnDocumentURI}, true + case fnNameRoot: + return &builtinFunc{name: name, min: builtinArityZero, max: builtinArityOne, fn: fnRoot}, true case namePath: - return &builtinFunc{name: name, min: 0, max: 1, fn: fnPath}, true + return &builtinFunc{name: name, min: builtinArityZero, max: builtinArityOne, fn: fnPath}, true case nameHasChildren: - return &builtinFunc{name: name, min: 0, max: 1, fn: fnHasChildren}, true - case "innermost": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnInnermost}, true - case "outermost": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnOutermost}, true - case lexicon.AttrLang: - return &builtinFunc{name: name, min: 1, max: 2, fn: fnLang}, true + return &builtinFunc{name: name, min: builtinArityZero, max: builtinArityOne, fn: fnHasChildren}, true + case fnNameInnermost: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnInnermost}, true + case fnNameOutermost: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnOutermost}, true + case fnNameLang: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityTwo, fn: fnLang}, true case nameLocalName: - return &builtinFunc{name: name, min: 0, max: 1, fn: fnLocalName}, true + return &builtinFunc{name: name, min: builtinArityZero, max: builtinArityOne, fn: fnLocalName}, true case lexicon.FnName: - return &builtinFunc{name: name, min: 0, max: 1, fn: fnName}, true + return &builtinFunc{name: name, min: builtinArityZero, max: builtinArityOne, fn: fnName}, true case nameNamespaceURI: - return &builtinFunc{name: name, min: 0, max: 1, fn: fnNamespaceURI}, true - case lexicon.TypeNumber: - return &builtinFunc{name: name, min: 0, max: 1, fn: fnNumber}, true + return &builtinFunc{name: name, min: builtinArityZero, max: builtinArityOne, fn: fnNamespaceURI}, true + case fnNameNumber: + return &builtinFunc{name: name, min: builtinArityZero, max: builtinArityOne, fn: fnNumber}, true case lexicon.FnGenerateID: - return &builtinFunc{name: name, min: 0, max: 1, fn: fnGenerateID}, true - case "parse-xml": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnParseXML}, true - case "parse-xml-fragment": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnParseXMLFragment}, true - case "doc": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnDoc}, true - case "doc-available": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnDocAvailable}, true - case "id": - return &builtinFunc{name: name, min: 1, max: 2, fn: fnID}, true - case "idref": - return &builtinFunc{name: name, min: 1, max: 2, fn: fnIDRef}, true - case "element-with-id": - return &builtinFunc{name: name, min: 1, max: 2, fn: fnElementWithID}, true - case "collection": - return &builtinFunc{name: name, min: 0, max: 1, fn: fnCollection}, true - case "uri-collection": - return &builtinFunc{name: name, min: 0, max: 1, fn: fnURICollection}, true + return &builtinFunc{name: name, min: builtinArityZero, max: builtinArityOne, fn: fnGenerateID}, true + case fnNameParseXML: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnParseXML}, true + case fnNameParseXMLFragment: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnParseXMLFragment}, true + case fnNameDoc: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnDoc}, true + case fnNameDocAvailable: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnDocAvailable}, true + case fnNameID: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityTwo, fn: fnID}, true + case fnNameIDRef: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityTwo, fn: fnIDRef}, true + case fnNameElementWithID: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityTwo, fn: fnElementWithID}, true + case fnNameCollection: + return &builtinFunc{name: name, min: builtinArityZero, max: builtinArityOne, fn: fnCollection}, true + case fnNameURICollection: + return &builtinFunc{name: name, min: builtinArityZero, max: builtinArityOne, fn: fnURICollection}, true } return nil, false @@ -150,7 +252,7 @@ func nodeIsNilled(ctx context.Context, n xdm.Node) bool { } func fnData(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { - if len(args) == 0 { + if len(args) == builtinArityZero { fc := getFnContext(ctx) if fc == nil { return nil, &xdm.Error{Code: lexicon.ErrXPDY0002, Message: "data() requires a context item"} @@ -370,49 +472,6 @@ func (ec *evalContext) isElementOnlyContent(n xdm.Node, provider ContentTypeKind return ok && kind == ContentTypeElementOnly } -// NodeBaseURI applies the XDM base-uri accessor, including xml:base and -// construction-time bases. Parentless non-element nodes have no base URI. -func NodeBaseURI(n xdm.Node) string { - if xdm.IsNilNode(n) { - return "" - } - // Namespace nodes have no base URI per the XPath data model. - if n.Type() == xdm.NamespaceNode { - return "" - } - // Per XDM: attribute, text, comment, and PI nodes derive their base URI - // from their parent element. If they have no parent, their base URI is - // the empty sequence. - nodeType := n.Type() - if nodeType == xdm.AttributeNode || nodeType == xdm.TextNode || - nodeType == xdm.CommentNode || nodeType == xdm.ProcessingInstructionNode { - if n.Parent() == nil { - return "" - } - } - // Walk up the parent chain to find the actual document the node lives - // in. OwnerDocument() may return a different document when nodes are - // created in one document and then moved to another (e.g. in XSLT - // xsl:result-document). - var doc *xdm.Document - if d, ok := n.(*xdm.Document); ok { - doc = d - } else { - cur := n - for cur.Parent() != nil { - cur = cur.Parent() - } - - if d, ok := cur.(*xdm.Document); ok { - doc = d - } else { - doc = n.OwnerDocument() - } - } - - return xdm.NodeGetBase(doc, n) -} - // isInsignificantWhitespaceText reports whether n is a text or CDATA-section node, // or an entity reference expanding to text, whose content is entirely XSD // whitespace (space, tab, CR, LF). XSD treats whitespace-only text and CDATA @@ -606,7 +665,7 @@ func buildNodePath(n xdm.Node) string { func elementPosition(n xdm.Node) int { local := xdm.LocalNameOf(n) uri := xdm.NodeNamespaceURI(n) - pos := 1 + pos := nodePathFirstSiblingPosition for sib := n.PrevSibling(); sib != nil; sib = sib.PrevSibling() { if sib.Type() == xdm.ElementNode && @@ -620,7 +679,7 @@ func elementPosition(n xdm.Node) int { } func textPosition(n xdm.Node) int { - pos := 1 + pos := nodePathFirstSiblingPosition for sib := n.PrevSibling(); sib != nil; sib = sib.PrevSibling() { if sib.Type() == xdm.TextNode || sib.Type() == xdm.CDATASectionNode { @@ -632,7 +691,7 @@ func textPosition(n xdm.Node) int { } func commentPosition(n xdm.Node) int { - pos := 1 + pos := nodePathFirstSiblingPosition for sib := n.PrevSibling(); sib != nil; sib = sib.PrevSibling() { if sib.Type() == xdm.CommentNode { @@ -644,7 +703,7 @@ func commentPosition(n xdm.Node) int { } func processingInstructionPosition(n xdm.Node) int { - pos := 1 + pos := nodePathFirstSiblingPosition for sibling := n.PrevSibling(); sibling != nil; sibling = sibling.PrevSibling() { if sibling.Type() == xdm.ProcessingInstructionNode && sibling.Name() == n.Name() { @@ -753,7 +812,7 @@ func fnLang(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { var n xdm.Node - if len(args) > 1 { + if len(args) > builtinArityOne { nodes, ok := xdm.NodesFrom(args[1]) if !ok || len(nodes) == 0 { return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "fn:lang second argument must be a node"} @@ -774,7 +833,7 @@ func fnLang(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { } for owner := n; owner != nil; owner = owner.Parent() { - if err := Charge(ctx, 1); err != nil { + if err := Charge(ctx, nodeFunctionVisitWork); err != nil { return nil, err } @@ -840,7 +899,7 @@ func fnNamespaceURI(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, err } func fnNumber(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { - if len(args) == 0 { + if len(args) == builtinArityZero { fc := getFnContext(ctx) if fc == nil || (fc.contextItem == nil && fc.node == nil) { return nil, &xdm.Error{Code: lexicon.ErrXPDY0002, Message: errMsgContextItemAbsent} @@ -1064,7 +1123,7 @@ func idElementsFromTypeAnnotations(doc *xdm.Document, tokens []string, ec *evalC func idNodeResult(node xdm.Node, wanted map[string]struct{}, elementWithID bool) (xdm.Node, bool) { switch typed := node.(type) { case *xdm.Attribute: - if _, ok := wanted[strings.Trim(xdm.StringValue(typed), " \t\r\n")]; !ok { + if _, ok := wanted[strings.Trim(xdm.StringValue(typed), lexicon.XMLWhitespace)]; !ok { return nil, false } @@ -1075,7 +1134,7 @@ func idNodeResult(node xdm.Node, wanted map[string]struct{}, elementWithID bool) return parent, true case *xdm.Element: - if _, ok := wanted[strings.Trim(xdm.StringValue(typed), " \t\r\n")]; !ok { + if _, ok := wanted[strings.Trim(xdm.StringValue(typed), lexicon.XMLWhitespace)]; !ok { return nil, false } @@ -1160,7 +1219,7 @@ func (l *idRefLookup) visit(node xdm.Node) error { l.collectAttributes(element) if l.ec != nil && nodeHasIDRefAnnotation(l.ec, element) { - if hasIDRefToken(strings.Trim(xdm.StringValue(element), " \t\r\n"), l.wanted) { + if hasIDRefToken(strings.Trim(xdm.StringValue(element), lexicon.XMLWhitespace), l.wanted) { l.nodes = append(l.nodes, element) } } @@ -1231,7 +1290,7 @@ func fnCollection(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error } if hasURI { - uri, err = resolveCollectionURI(ctx, uri, "fn:collection") + uri, err = resolveCollectionURI(ctx, uri, prefixFn+":"+fnNameCollection) if err != nil { return nil, err } @@ -1258,14 +1317,14 @@ func fnCollection(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error seq, err := ec.collectionResolver.ResolveCollection(ctx, uri) if err != nil { var detail *xdm.Error - if errors.As(err, &detail) && detail.Code == "FODC0003" { + if errors.As(err, &detail) && detail.Code == errCodeFODC0003 { return nil, err } return nil, &xdm.Error{Code: lexicon.ErrFODC0002, Message: fmt.Sprintf("fn:collection: cannot resolve collection: %v", err), Cause: err} } - if ec.version == 20 || ec.version == 30 { + if ec.version == xpathVersion20 || ec.version == xpathVersion30 { required := xdm.SequenceType{ItemTest: xdm.TypeTest{Kind: xdm.NodeKindNode}, Occurrence: xdm.OccurrenceZeroOrMore} seq, err = coerceFuncallArg(ctx, seq, required, "collection result", 0, ec) @@ -1304,7 +1363,7 @@ func collectionInvocationCacheKey(resolver CollectionResolver, uris bool) collec // Reject inconsistent identities instead of silently substituting different data. func registerCollectionDocuments(ctx context.Context, ec *evalContext, seq xdm.Sequence) error { for item := range xdm.SequenceItems(seq) { - if err := Charge(ctx, 1); err != nil { + if err := Charge(ctx, nodeFunctionVisitWork); err != nil { return err } @@ -1326,7 +1385,7 @@ func registerCollectionDocuments(ctx context.Context, ec *evalContext, seq xdm.S } if previous, found := ec.docCache[uri]; found && previous != doc { - return &xdm.Error{Code: "FODC0003", Message: "collection supplied conflicting document identities for " + uri} + return &xdm.Error{Code: errCodeFODC0003, Message: "collection supplied conflicting document identities for " + uri} } ec.docCache[uri] = doc @@ -1342,7 +1401,7 @@ func fnURICollection(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, er } if hasURI { - uri, err = resolveCollectionURI(ctx, uri, "fn:uri-collection") + uri, err = resolveCollectionURI(ctx, uri, prefixFn+":"+fnNameURICollection) if err != nil { return nil, err } @@ -1418,7 +1477,7 @@ func fnDocAvailable(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, err return nil, err } - if ec := getFnContext(ctx); ec != nil && (ec.version == 20 || ec.version == 30) { + if ec := getFnContext(ctx); ec != nil && (ec.version == xpathVersion20 || ec.version == xpathVersion30) { if detail, ok := errors.AsType[*xdm.Error](err); ok && detail.Code == errCodeFODC0005 { return nil, err } @@ -1431,7 +1490,7 @@ func availableDocument(ctx context.Context, uri string) (xdm.Sequence, error) { if ec := getFnContext(ctx); ec != nil { // A host's fn:doc supplies its available-documents component, including // package-specific whitespace views. Availability must use that same pool. - if fn := ec.fnsNS[QualifiedName{URI: NSFn, Name: "doc"}]; fn != nil { + if fn := ec.fnsNS[QualifiedName{URI: NSFn, Name: fnNameDoc}]; fn != nil { return fn.Call(ctx, []xdm.Sequence{xdm.SingleString(uri)}) } } @@ -1455,41 +1514,8 @@ type documentSnapshotStore struct { type documentSnapshotKey struct{ scope any } -// DocumentLoadSnapshot retains an available-document outcome in one host -// invocation. scope identifies the host's document pool; key includes its -// package/preprocessing view. Callers must check live read policy before this -// function, including on a hit. Controls and policy failures are never retained. -func DocumentLoadSnapshot(ctx context.Context, scope any, key string, load func() (*xdm.Document, error)) (*xdm.Document, error) { - store, _ := InvocationState(ctx, documentSnapshotKey{scope: scope}, func() any { - return &documentSnapshotStore{} - }).(*documentSnapshotStore) - - return loadDocumentSnapshot(ctx, store, key, load) -} - -// ResolveDocumentURI applies fn:doc's URI rules in the calling static context. -// Hosts overriding retrieval must retain these rules, including fragment rejection. -func ResolveDocumentURI(ctx context.Context, uri string) (string, error) { - if strings.Contains(uri, "#") { - return "", &xdm.Error{Code: errCodeFODC0005, Message: "fn:doc: URI must not contain a fragment identifier"} - } - - resolved, err := resolveDocURI(ctx, uri) - if err != nil { - return "", err - } - // An empty argument resolves to the base URI verbatim, which may itself - // carry a fragment identifier. Re-check the resolved URI so doc("") with a - // fragmented base URI is rejected the same as a fragmented argument. - if strings.Contains(resolved, "#") { - return "", &xdm.Error{Code: errCodeFODC0005, Message: "fn:doc: URI must not contain a fragment identifier"} - } - - return resolved, nil -} - func loadDocumentSnapshot(ctx context.Context, store *documentSnapshotStore, key string, load func() (*xdm.Document, error)) (*xdm.Document, error) { - if err := Charge(ctx, 1); err != nil { + if err := Charge(ctx, nodeFunctionVisitWork); err != nil { return nil, err } @@ -1619,7 +1645,7 @@ func retrieveDocument(ctx context.Context, resolved string, options xdm.ParseOpt } func collectionURIArg(ctx context.Context, args []xdm.Sequence) (string, bool, error) { - if len(args) == 0 { + if len(args) == builtinArityZero { return "", false, nil } // xs:string? URI: atomize FIRST, then apply the singleton-or-empty @@ -1702,8 +1728,8 @@ func resolveDocURI(ctx context.Context, uri string) (string, error) { func resolveIDLookupDocument(ctx context.Context, args []xdm.Sequence) (*xdm.Document, error) { var node xdm.Node - if len(args) > 1 { - if xdm.SequenceLen(args[1]) != 1 { + if len(args) > builtinArityOne { + if xdm.SequenceLen(args[1]) != singletonFunctionArgument { return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "fn:id second argument must be a single node"} } @@ -1801,7 +1827,7 @@ func sequenceFromDocOrderedNodes(ctx context.Context, nodes []xdm.Node) (xdm.Seq // nodeArgOrCtx extracts a node from the first argument or falls back to the context node. func nodeArgOrCtx(ctx context.Context, args []xdm.Sequence) (xdm.Node, error) { - if len(args) == 0 { + if len(args) == builtinArityZero { fc := getFnContext(ctx) if fc == nil || fc.node == nil && fc.contextItem == nil { return nil, &xdm.Error{Code: lexicon.ErrXPDY0002, Message: errMsgContextItemAbsent} @@ -1818,7 +1844,7 @@ func nodeArgOrCtx(ctx context.Context, args []xdm.Sequence) (xdm.Node, error) { return nil, nil //nolint:nilnil // An empty optional node argument has no node. } - if xdm.SequenceLen(args[0]) > 1 { + if xdm.SequenceLen(args[0]) > singletonFunctionArgument { // XPath 1.0 compatibility mode uses the first node; otherwise a >1 node // argument is a type error. if !getFnContext(ctx).xpath10CompatMode() { diff --git a/transform/internal/xpath/functions_numeric.go b/transform/internal/xpath/functions_numeric.go index cb67f9a..1cd48fd 100644 --- a/transform/internal/xpath/functions_numeric.go +++ b/transform/internal/xpath/functions_numeric.go @@ -36,6 +36,14 @@ const roundMaxComputeScale = 1 << 20 // an astronomically large exponent to Exp. const ratFracDigitNonTerminating = 1 << 30 +// Exact-binary rounding uses 256-bit math/big.Float intermediates. This +// working precision is independent of decimal operand scales and their caps. +const roundFloatPrecisionBits = 256 + +// XPath position arguments round to an integer (zero fractional decimal places). +// Shared by substring and subsequence, independently of their one-based positions. +const positionRoundPrecision = 0 + // compatArgString applies the XPath 1.0 function-conversion rule for an // xs:string argument: fn:string of its first item. Used for format-number's // picture and decimal-format-name arguments under compatibility mode. @@ -67,7 +75,7 @@ func fnFormatNumber(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, err return nil, err } default: - if xdm.SequenceLen(args[0]) != 1 { + if xdm.SequenceLen(args[0]) != singletonFunctionArgument { return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "format-number() first argument must be a singleton numeric value"} } @@ -105,7 +113,7 @@ func fnFormatNumber(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, err // Optional decimal-format-name argument (xs:string), coerced the same way. df := defaultDecimalFormat(ctx) - if len(args) > 2 && xdm.SequenceLen(args[2]) > 0 { + if len(args) > builtinArityTwo && xdm.SequenceLen(args[2]) > 0 { var formatName string if compat { formatName, err = compatArgString(args[2]) @@ -131,21 +139,23 @@ func fnFormatNumber(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, err } func lookupFunctionsNumeric(name string) (*builtinFunc, bool) { + const fnNameAbs = "abs" // standard numeric function local name + switch name { - case "abs": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnAbs}, true + case fnNameAbs: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnAbs}, true case nameCeiling: - return &builtinFunc{name: name, min: 1, max: 1, fn: fnCeiling}, true + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnCeiling}, true case nameFloor: - return &builtinFunc{name: name, min: 1, max: 1, fn: fnFloor}, true + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnFloor}, true case nameRound: - return &builtinFunc{name: name, min: 1, max: 2, fn: fnRound}, true + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityTwo, fn: fnRound}, true case nameRoundHalfToEven: - return &builtinFunc{name: name, min: 1, max: 2, fn: fnRoundHalfToEven}, true - case "format-integer": - return &builtinFunc{name: name, min: 2, max: 3, fn: fnFormatInteger}, true + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityTwo, fn: fnRoundHalfToEven}, true + case fnNameFormatInteger: + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityThree, fn: fnFormatInteger}, true case lexicon.FnFormatNumber: - return &builtinFunc{name: name, min: 2, max: 3, fn: fnFormatNumber}, true + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityThree, fn: fnFormatNumber}, true } return nil, false @@ -180,7 +190,7 @@ func promoteSeqToNumeric(seq xdm.Sequence) (xdm.AtomicValue, bool, error) { switch xdm.SequenceLen(seq) { case 0: return xdm.AtomicValue{}, false, nil - case 1: + case singletonFunctionArgument: a, err := promoteToNumeric(seq.Get(0)) if err != nil { return xdm.AtomicValue{}, false, err @@ -349,6 +359,12 @@ func resolveRoundScale(precision *big.Int, intDigits, fracDigits int) (int, roun // intDigitCount returns the number of base-10 digits in |n|, with 0 counted as // a single digit. Used as the integer-magnitude input to resolveRoundScale. func intDigitCount(n *big.Int) int { + // This rational upper bound estimates log10(2) from a bit count. + const ( + binaryToDecimalEstimateNumerator = 30103 + binaryToDecimalEstimateDenominator = 100000 + ) + if n.Sign() == 0 { return 1 } @@ -356,14 +372,14 @@ func intDigitCount(n *big.Int) int { // Estimate from BitLen, then compare magnitudes; do not allocate a decimal // string just to count its digits. 30103/100000 bounds log10(2) above. b := n.BitLen() - digits := b/100000*30103 + int(int64(b%100000)*30103/100000) + 1 - power := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(digits-1)), nil) + digits := b/binaryToDecimalEstimateDenominator*binaryToDecimalEstimateNumerator + int(int64(b%binaryToDecimalEstimateDenominator)*binaryToDecimalEstimateNumerator/binaryToDecimalEstimateDenominator) + 1 + power := new(big.Int).Exp(big.NewInt(numericDecimalRadix), big.NewInt(int64(digits-1)), nil) abs := new(big.Int).Abs(n) for digits > 1 && abs.Cmp(power) < 0 { digits-- - power.Quo(power, big.NewInt(10)) + power.Quo(power, big.NewInt(numericDecimalRadix)) } return digits @@ -434,14 +450,21 @@ func fnRoundHalfToEven(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, // large 10^|precision| is never materialised: cases whose result is determined // trivially (rounds to 0, or operand unchanged) short-circuit before any Exp. func roundImpl(ctx context.Context, args []xdm.Sequence, halfToEven bool) (xdm.Sequence, error) { + // The binary64 fractional bound selects an unchanged-result shortcut; it + // is independent of the cap for non-terminating decimal computations. + const ( + defaultRoundPrecision = 0 // absent $precision requests integral rounding + binary64MaximumFractionDigits = 324 + ) + // When the 2-arg form is used, $precision is a required "exactly one // xs:integer" parameter and must be validated whether or not $arg is empty: // the empty first argument does not excuse an absent or ill-typed precision. // round((), "bad") and round(1, ()) both raise XPTY0004; round((), 3) and // round((), 1) are () only because the precision itself is valid. - precision := big.NewInt(0) + precision := big.NewInt(defaultRoundPrecision) - if len(args) > 1 { + if len(args) > builtinArityOne { p, err := roundPrecisionArg(ctx, args[1]) if err != nil { return nil, err @@ -460,7 +483,7 @@ func roundImpl(ctx context.Context, args []xdm.Sequence, halfToEven bool) (xdm.S } if size, numeric := NumericSize(a); numeric { - if err := preflightNumeric(ctx, size*8); err != nil { + if err := preflightNumeric(ctx, size*bitsPerByte); err != nil { return nil, err } } @@ -482,7 +505,7 @@ func roundImpl(ctx context.Context, args []xdm.Sequence, halfToEven bool) (xdm.S // without ever computing a huge scale. intDigits := floatIntDigitCount(n) - scale, decision, err := resolveRoundScale(precision, intDigits, 324) + scale, decision, err := resolveRoundScale(precision, intDigits, binary64MaximumFractionDigits) if err != nil { return nil, err } @@ -561,13 +584,11 @@ func floatIntDigitCount(n float64) int { return 1 } - intPart, _ := new(big.Float).SetPrec(256).SetFloat64(m).Int(nil) + intPart, _ := new(big.Float).SetPrec(roundFloatPrecisionBits).SetFloat64(m).Int(new(big.Int)) return intDigitCount(intPart) } -// roundHalfToEvenFloat rounds a float64 to the given precision using -// half-to-even (banker's rounding). It uses big.Float to scale the value // roundHalfUpFloat rounds a float64 using "half towards positive infinity" // semantics (XPath fn:round). Uses big.Float to preserve precision. func roundHalfUpFloat(n float64, precision int) float64 { @@ -576,16 +597,16 @@ func roundHalfUpFloat(n float64, precision int) float64 { } if precision >= 0 { - bf := new(big.Float).SetPrec(256).SetFloat64(n) + bf := new(big.Float).SetPrec(roundFloatPrecisionBits).SetFloat64(n) scale := bigPow10(precision) - scaled := new(big.Float).SetPrec(256).Mul(bf, scale) + scaled := new(big.Float).SetPrec(roundFloatPrecisionBits).Mul(bf, scale) // XPath round: floor(x + 0.5) — rounds half towards +∞ - half := new(big.Float).SetPrec(256).SetFloat64(0.5) - shifted := new(big.Float).SetPrec(256).Add(scaled, half) + half := new(big.Float).SetPrec(roundFloatPrecisionBits).SetFloat64(0.5) + shifted := new(big.Float).SetPrec(roundFloatPrecisionBits).Add(scaled, half) intPart := bigFloatFloor(shifted) - result := new(big.Float).SetPrec(256).SetInt(intPart) + result := new(big.Float).SetPrec(roundFloatPrecisionBits).SetInt(intPart) result.Quo(result, scale) f, _ := result.Float64() @@ -594,15 +615,15 @@ func roundHalfUpFloat(n float64, precision int) float64 { // F&O requires rounding the actual binary value, including at coarse scales. // Its shortest decimal representation can fall on the other side of a tie. - bf := new(big.Float).SetPrec(256).SetFloat64(n) + bf := new(big.Float).SetPrec(roundFloatPrecisionBits).SetFloat64(n) divisor := bigPow10(-precision) - scaled := new(big.Float).SetPrec(256).Quo(bf, divisor) + scaled := new(big.Float).SetPrec(roundFloatPrecisionBits).Quo(bf, divisor) - half := new(big.Float).SetPrec(256).SetFloat64(0.5) - shifted := new(big.Float).SetPrec(256).Add(scaled, half) + half := new(big.Float).SetPrec(roundFloatPrecisionBits).SetFloat64(0.5) + shifted := new(big.Float).SetPrec(roundFloatPrecisionBits).Add(scaled, half) intPart := bigFloatFloor(shifted) - result := new(big.Float).SetPrec(256).SetInt(intPart) + result := new(big.Float).SetPrec(roundFloatPrecisionBits).SetInt(intPart) result.Mul(result, divisor) f, _ := result.Float64() @@ -611,15 +632,15 @@ func roundHalfUpFloat(n float64, precision int) float64 { // bigPow10 returns 10^n as a big.Float with 256-bit precision. func bigPow10(n int) *big.Float { - p := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(n)), nil) - return new(big.Float).SetPrec(256).SetInt(p) + p := new(big.Int).Exp(big.NewInt(numericDecimalRadix), big.NewInt(int64(n)), nil) + return new(big.Float).SetPrec(roundFloatPrecisionBits).SetInt(p) } // bigFloatFloor returns the floor of a big.Float as a big.Int. func bigFloatFloor(bf *big.Float) *big.Int { - intPart, _ := bf.Int(nil) + intPart, _ := bf.Int(new(big.Int)) if bf.Sign() < 0 { - frac := new(big.Float).SetPrec(256).Sub(bf, new(big.Float).SetPrec(256).SetInt(intPart)) + frac := new(big.Float).SetPrec(roundFloatPrecisionBits).Sub(bf, new(big.Float).SetPrec(roundFloatPrecisionBits).SetInt(intPart)) if frac.Sign() != 0 { intPart.Sub(intPart, big.NewInt(1)) } @@ -635,13 +656,13 @@ func bigFloatFloor(bf *big.Float) *big.Int { // scale factor. func roundHalfToEvenFloat(n float64, precision int) float64 { if precision >= 0 { - bf := new(big.Float).SetPrec(256).SetFloat64(n) + bf := new(big.Float).SetPrec(roundFloatPrecisionBits).SetFloat64(n) scale := bigPow10(precision) - scaled := new(big.Float).SetPrec(256).Mul(bf, scale) + scaled := new(big.Float).SetPrec(roundFloatPrecisionBits).Mul(bf, scale) rounded := roundScaledHalfToEven(scaled) - result := new(big.Float).SetPrec(256).SetInt(rounded) + result := new(big.Float).SetPrec(roundFloatPrecisionBits).SetInt(rounded) result.Quo(result, scale) f, _ := result.Float64() // Preserve negative zero per IEEE 754 @@ -653,11 +674,11 @@ func roundHalfToEvenFloat(n float64, precision int) float64 { } // Negative precision: round to 10^(-precision) - bf := new(big.Float).SetPrec(256).SetFloat64(n) + bf := new(big.Float).SetPrec(roundFloatPrecisionBits).SetFloat64(n) divisor := bigPow10(-precision) - scaled := new(big.Float).SetPrec(256).Quo(bf, divisor) + scaled := new(big.Float).SetPrec(roundFloatPrecisionBits).Quo(bf, divisor) rounded := roundScaledHalfToEven(scaled) - result := new(big.Float).SetPrec(256).SetInt(rounded) + result := new(big.Float).SetPrec(roundFloatPrecisionBits).SetInt(rounded) result.Mul(result, divisor) @@ -672,19 +693,19 @@ func roundHalfToEvenFloat(n float64, precision int) float64 { func roundScaledHalfToEven(scaled *big.Float) *big.Int { // Determine floor and the fractional part - intPart, _ := scaled.Int(nil) // truncates towards zero + intPart, _ := scaled.Int(new(big.Int)) // truncates towards zero if scaled.Sign() < 0 { // For negative numbers, Int truncates towards zero (ceiling); // we want the floor. - frac := new(big.Float).SetPrec(256).Sub(scaled, new(big.Float).SetPrec(256).SetInt(intPart)) + frac := new(big.Float).SetPrec(roundFloatPrecisionBits).Sub(scaled, new(big.Float).SetPrec(roundFloatPrecisionBits).SetInt(intPart)) if frac.Sign() != 0 { intPart.Sub(intPart, big.NewInt(1)) } } - floor := new(big.Float).SetPrec(256).SetInt(intPart) - frac := new(big.Float).SetPrec(256).Sub(scaled, floor) - half := new(big.Float).SetPrec(256).SetFloat64(0.5) + floor := new(big.Float).SetPrec(roundFloatPrecisionBits).SetInt(intPart) + frac := new(big.Float).SetPrec(roundFloatPrecisionBits).Sub(scaled, floor) + half := new(big.Float).SetPrec(roundFloatPrecisionBits).SetFloat64(0.5) var rounded *big.Int @@ -750,7 +771,7 @@ func ratRound(r *big.Rat) *big.Rat { // roundIntegerHalfToEven rounds a *big.Int to 10^scale using half-to-even. func roundIntegerHalfToEven(n *big.Int, scale int) *big.Int { - pow := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(scale)), nil) + pow := new(big.Int).Exp(big.NewInt(numericDecimalRadix), big.NewInt(int64(scale)), nil) q, r := new(big.Int).QuoRem(n, pow, new(big.Int)) half := new(big.Int).Quo(pow, big.NewInt(2)) absR := new(big.Int).Abs(r) @@ -785,7 +806,7 @@ func roundIntegerHalfToEven(n *big.Int, scale int) *big.Int { // roundIntegerHalfUp rounds a *big.Int to 10^scale using half-up (towards positive infinity). func roundIntegerHalfUp(n *big.Int, scale int) *big.Int { - pow := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(scale)), nil) + pow := new(big.Int).Exp(big.NewInt(numericDecimalRadix), big.NewInt(int64(scale)), nil) q, r := new(big.Int).QuoRem(n, pow, new(big.Int)) half := new(big.Int).Quo(pow, big.NewInt(2)) absR := new(big.Int).Abs(r) @@ -810,7 +831,7 @@ func roundIntegerHalfUp(n *big.Int, scale int) *big.Int { // using half-up rounding (towards positive infinity on tie). func ratRoundPrecision(r *big.Rat, precision int) *big.Rat { if precision >= 0 { - scale := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(precision)), nil) + scale := new(big.Int).Exp(big.NewInt(numericDecimalRadix), big.NewInt(int64(precision)), nil) // Multiply by scale, round, divide by scale shifted := new(big.Rat).Mul(r, new(big.Rat).SetInt(scale)) half := new(big.Rat).SetFrac64(1, 2) @@ -823,7 +844,7 @@ func ratRoundPrecision(r *big.Rat, precision int) *big.Rat { // integer first — flooring discards the fractional part and // can push the value past the rounding boundary (e.g. -249.9 floors to -250 // or even -251, landing on the wrong multiple of 100). - scale := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(-precision)), nil) + scale := new(big.Int).Exp(big.NewInt(numericDecimalRadix), big.NewInt(int64(-precision)), nil) scaleRat := new(big.Rat).SetInt(scale) divided := new(big.Rat).Quo(r, scaleRat) rounded := ratRound(divided) @@ -838,7 +859,7 @@ func ratRoundPrecision(r *big.Rat, precision int) *big.Rat { // so 10^|precision| is bounded by the operand and never astronomically large. func ratRoundPrecisionHalfToEven(r *big.Rat, precision int) *big.Rat { if precision >= 0 { - scale := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(precision)), nil) + scale := new(big.Int).Exp(big.NewInt(numericDecimalRadix), big.NewInt(int64(precision)), nil) scaleRat := new(big.Rat).SetInt(scale) shifted := new(big.Rat).Mul(r, scaleRat) rounded := number.RatRoundHalfToEvenInt(shifted) @@ -850,7 +871,7 @@ func ratRoundPrecisionHalfToEven(r *big.Rat, precision int) *big.Rat { // integer first — flooring would discard the fractional part that can // break a tie at the rounding boundary (e.g. ...896.5...123 must round up, // not be treated as an exact half that lands on the even digit). - scale := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(-precision)), nil) + scale := new(big.Int).Exp(big.NewInt(numericDecimalRadix), big.NewInt(int64(-precision)), nil) scaleRat := new(big.Rat).SetInt(scale) divided := new(big.Rat).Quo(r, scaleRat) rounded := number.RatRoundHalfToEvenInt(divided) diff --git a/transform/internal/xpath/functions_qname.go b/transform/internal/xpath/functions_qname.go index ab05d73..5852db4 100644 --- a/transform/internal/xpath/functions_qname.go +++ b/transform/internal/xpath/functions_qname.go @@ -14,34 +14,54 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) +// QName constructor/accessor local names are function dispatch vocabulary. +const ( + fnNameQName = "QName" + fnNamePrefixFromQName = "prefix-from-QName" + fnNameLocalNameFromQName = "local-name-from-QName" + fnNameNamespaceURIFromQName = "namespace-uri-from-QName" + fnNameNamespaceURIForPrefix = "namespace-uri-for-prefix" + fnNameInScopePrefixes = "in-scope-prefixes" + fnNameResolveQName = "resolve-QName" +) + +// Optional QName strings allow an atomized-empty namespace/prefix. Atomization +// and required-element cardinalities are independent argument constraints. +const ( + qnameStringOptional = true + qnameStringRequired = false + qnameMaximumAtoms = 1 + qnameRequiredElements = 1 +) + func lookupFunctionsQName(name string) (*builtinFunc, bool) { switch name { - case lexicon.TypeQName: - return &builtinFunc{name: name, min: 2, max: 2, fn: fnQName}, true - case "prefix-from-QName": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnPrefixFromQName}, true - case "local-name-from-QName": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnLocalNameFromQName}, true - case "namespace-uri-from-QName": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnNamespaceURIFromQName}, true - case "namespace-uri-for-prefix": - return &builtinFunc{name: name, min: 2, max: 2, fn: fnNamespaceURIForPrefix}, true - case "in-scope-prefixes": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnInScopePrefixes}, true - case "resolve-QName": - return &builtinFunc{name: name, min: 2, max: 2, fn: fnResolveQName}, true + case fnNameQName: + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityTwo, fn: fnQName}, true + case fnNamePrefixFromQName: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnPrefixFromQName}, true + case fnNameLocalNameFromQName: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnLocalNameFromQName}, true + case fnNameNamespaceURIFromQName: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnNamespaceURIFromQName}, true + case fnNameNamespaceURIForPrefix: + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityTwo, fn: fnNamespaceURIForPrefix}, true + case fnNameInScopePrefixes: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnInScopePrefixes}, true + case fnNameResolveQName: + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityTwo, fn: fnResolveQName}, true } return nil, false } func fnQName(_ context.Context, args []xdm.Sequence) (xdm.Sequence, error) { - uri, err := coerceQNameString(args[0], true, "fn:QName namespace argument must be a string") + uri, err := coerceQNameString(args[0], qnameStringOptional, "fn:QName namespace argument must be a string") if err != nil { return nil, err } - qname, err := coerceQNameString(args[1], false, "fn:QName QName argument must be a string") + qname, err := coerceQNameString(args[1], qnameStringRequired, "fn:QName QName argument must be a string") if err != nil { return nil, err } @@ -74,7 +94,7 @@ func atomizeQNameArg(seq xdm.Sequence, fname string) (xdm.AtomicValue, bool, err err := atomizeStream(seq, func(av xdm.AtomicValue) (bool, error) { count++ - if count == 1 { + if count == qnameMaximumAtoms { first = av return true, nil } @@ -89,7 +109,7 @@ func atomizeQNameArg(seq xdm.Sequence, fname string) (xdm.AtomicValue, bool, err return xdm.AtomicValue{}, true, nil } - if count > 1 { + if count > qnameMaximumAtoms { return xdm.AtomicValue{}, false, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: fname + " expects a single QName"} } @@ -102,7 +122,7 @@ func atomizeQNameArg(seq xdm.Sequence, fname string) (xdm.AtomicValue, bool, err } func fnPrefixFromQName(_ context.Context, args []xdm.Sequence) (xdm.Sequence, error) { - a, empty, err := atomizeQNameArg(args[0], "fn:prefix-from-QName") + a, empty, err := atomizeQNameArg(args[0], prefixFn+":"+fnNamePrefixFromQName) if err != nil { return nil, err } @@ -120,7 +140,7 @@ func fnPrefixFromQName(_ context.Context, args []xdm.Sequence) (xdm.Sequence, er } func fnLocalNameFromQName(_ context.Context, args []xdm.Sequence) (xdm.Sequence, error) { - a, empty, err := atomizeQNameArg(args[0], "fn:local-name-from-QName") + a, empty, err := atomizeQNameArg(args[0], prefixFn+":"+fnNameLocalNameFromQName) if err != nil { return nil, err } @@ -133,7 +153,7 @@ func fnLocalNameFromQName(_ context.Context, args []xdm.Sequence) (xdm.Sequence, } func fnNamespaceURIFromQName(_ context.Context, args []xdm.Sequence) (xdm.Sequence, error) { - a, empty, err := atomizeQNameArg(args[0], "fn:namespace-uri-from-QName") + a, empty, err := atomizeQNameArg(args[0], prefixFn+":"+fnNameNamespaceURIFromQName) if err != nil { return nil, err } @@ -148,12 +168,12 @@ func fnNamespaceURIFromQName(_ context.Context, args []xdm.Sequence) (xdm.Sequen func fnNamespaceURIForPrefix(_ context.Context, args []xdm.Sequence) (xdm.Sequence, error) { // The element() second argument is required and must be exactly one // element() regardless of the first argument, so validate it FIRST. - elem, err := requireSingleElement(args[1], "fn:namespace-uri-for-prefix") + elem, err := requireSingleElement(args[1], prefixFn+":"+fnNameNamespaceURIForPrefix) if err != nil { return nil, err } - prefix, err := coerceQNameString(args[0], true, "fn:namespace-uri-for-prefix prefix argument must be a string") + prefix, err := coerceQNameString(args[0], qnameStringOptional, "fn:namespace-uri-for-prefix prefix argument must be a string") if err != nil { return nil, err } @@ -170,7 +190,7 @@ func fnNamespaceURIForPrefix(_ context.Context, args []xdm.Sequence) (xdm.Sequen // before any sibling argument is coerced so an invalid element() arg yields // XPTY0004, and never an error from atomizing the other argument. func requireSingleElement(seq xdm.Sequence, fname string) (*xdm.Element, error) { - if xdm.SequenceLen(seq) != 1 { + if xdm.SequenceLen(seq) != qnameRequiredElements { return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: fname + ": expects a single element"} } @@ -190,7 +210,7 @@ func requireSingleElement(seq xdm.Sequence, fname string) (*xdm.Element, error) func fnResolveQName(_ context.Context, args []xdm.Sequence) (xdm.Sequence, error) { // The element() second argument is required and must be exactly one // element() regardless of whether $qname is empty, so validate it FIRST. - elem, err := requireSingleElement(args[1], "resolve-QName") + elem, err := requireSingleElement(args[1], fnNameResolveQName) if err != nil { return nil, err } @@ -300,7 +320,7 @@ func coerceOptionalQNameString(seq xdm.Sequence, message string) (string, bool, err := atomizeStream(seq, func(av xdm.AtomicValue) (bool, error) { count++ - if count == 1 { + if count == qnameMaximumAtoms { first = av return true, nil } @@ -315,7 +335,7 @@ func coerceOptionalQNameString(seq xdm.Sequence, message string) (string, bool, return "", true, nil } - if count > 1 { + if count > qnameMaximumAtoms { return "", false, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: message} } @@ -340,7 +360,7 @@ func coerceOptionalQNameString(seq xdm.Sequence, message string) (string, bool, } func fnInScopePrefixes(_ context.Context, args []xdm.Sequence) (xdm.Sequence, error) { - if xdm.SequenceLen(args[0]) != 1 { + if xdm.SequenceLen(args[0]) != qnameRequiredElements { return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "fn:in-scope-prefixes expects a single element"} } diff --git a/transform/internal/xpath/functions_sequence.go b/transform/internal/xpath/functions_sequence.go index ab309fe..4870b0e 100644 --- a/transform/internal/xpath/functions_sequence.go +++ b/transform/internal/xpath/functions_sequence.go @@ -15,44 +15,62 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) +// XPath sequence positions are one-based; backing Sequence.Get indices remain +// zero-based. These comparisons clamp the requested insertion/removal position. +const sequenceFirstPosition = 1 + func lookupFunctionsSequence(name string) (*builtinFunc, bool) { + // These fn: local names and the static-only extension flag retain dispatch policy. + const ( + fnNameInsertBefore = "insert-before" + fnNameSubsequence = "subsequence" + fnNameUnordered = "unordered" + fnNameZeroOrOne = "zero-or-one" + fnNameOneOrMore = "one-or-more" + fnNameExactlyOne = "exactly-one" + fnNameDeepEqual = "deep-equal" + fnNameIndexOf = "index-of" + fnNameFlatten = "flatten" + staticOnlySequenceExtension = true + ) + switch name { - case "empty": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnEmpty}, true - case "exists": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnExists}, true + case fnNameEmpty: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnEmpty}, true + case fnNameExists: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnExists}, true case fnNameHead: - return &builtinFunc{name: name, min: 1, max: 1, fn: fnHead}, true + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnHead}, true case nameTail: - return &builtinFunc{name: name, min: 1, max: 1, fn: fnTail}, true - case "insert-before": - return &builtinFunc{name: name, min: 3, max: 3, fn: fnInsertBefore}, true + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnTail}, true + case fnNameInsertBefore: + return &builtinFunc{name: name, min: builtinArityThree, max: builtinArityThree, fn: fnInsertBefore}, true case nameRemove: - return &builtinFunc{name: name, min: 2, max: 2, fn: fnRemove}, true + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityTwo, fn: fnRemove}, true case nameReverse: - return &builtinFunc{name: name, min: 1, max: 1, fn: fnReverse}, true - case "subsequence": - return &builtinFunc{name: name, min: 2, max: 3, fn: fnSubsequence}, true - case "unordered": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnUnordered}, true - case "zero-or-one": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnZeroOrOne}, true - case "one-or-more": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnOneOrMore}, true - case "exactly-one": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnExactlyOne}, true - case "deep-equal": - return &builtinFunc{name: name, min: 2, max: 3, fn: fnDeepEqual}, true - case "index-of": - return &builtinFunc{name: name, min: 2, max: 3, fn: fnIndexOf}, true + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnReverse}, true + case fnNameSubsequence: + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityThree, fn: fnSubsequence}, true + case fnNameUnordered: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnUnordered}, true + case fnNameZeroOrOne: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnZeroOrOne}, true + case fnNameOneOrMore: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnOneOrMore}, true + case fnNameExactlyOne: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnExactlyOne}, true + case fnNameDeepEqual: + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityThree, fn: fnDeepEqual}, true + case fnNameIndexOf: + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityThree, fn: fnIndexOf}, true case lexicon.FnLast: - return &builtinFunc{name: name, min: 0, max: 0, fn: fnLast}, true + return &builtinFunc{name: name, min: builtinArityZero, max: builtinArityZero, fn: fnLast}, true case lexicon.FnPosition: - return &builtinFunc{name: name, min: 0, max: 0, fn: fnPosition}, true + return &builtinFunc{name: name, min: builtinArityZero, max: builtinArityZero, fn: fnPosition}, true case nameSort: - return &builtinFunc{name: name, min: 1, max: 3, fn: fnSort}, true - case "flatten": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnFlatten, extension: true}, true + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityThree, fn: fnSort}, true + case fnNameFlatten: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnFlatten, extension: staticOnlySequenceExtension}, true } return nil, false @@ -105,7 +123,7 @@ func fnInsertBefore(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, err pos := 0 if posVal > int64(n) { pos = n - } else if posVal > 1 { + } else if posVal > sequenceFirstPosition { pos = int(posVal) - 1 } @@ -136,7 +154,7 @@ func fnRemove(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { target := args[0] n := xdm.SequenceLen(target) - if posVal < 1 || posVal > int64(n) { + if posVal < sequenceFirstPosition || posVal > int64(n) { return target, nil } @@ -236,7 +254,7 @@ func roundSubsequenceArgument(arg xdm.Sequence, compat bool, role string) (float return 0, err } - return roundHalfUpFloat(value.ToFloat64(), 0), nil + return roundHalfUpFloat(value.ToFloat64(), positionRoundPrecision), nil } switch xdm.SequenceLen(arg) { @@ -264,7 +282,7 @@ func roundSubsequenceArgument(arg xdm.Sequence, compat bool, role string) (float return 0, subsequenceArgumentError(role, "must be numeric") } - return roundHalfUpFloat(value.ToFloat64(), 0), nil + return roundHalfUpFloat(value.ToFloat64(), positionRoundPrecision), nil } func subsequenceArgumentError(role, problem string) error { @@ -272,7 +290,7 @@ func subsequenceArgumentError(role, problem string) error { } func subsequenceLength(args []xdm.Sequence, compat bool) (float64, bool, error) { - if len(args) <= 2 { + if len(args) <= builtinArityTwo { return 0, false, nil } @@ -311,24 +329,30 @@ func fnUnordered(_ context.Context, args []xdm.Sequence) (xdm.Sequence, error) { } func fnZeroOrOne(_ context.Context, args []xdm.Sequence) (xdm.Sequence, error) { - if xdm.SequenceLen(args[0]) > 1 { - return nil, &xdm.Error{Code: "FORG0003", Message: fmt.Sprintf("zero-or-one() called with sequence of length %d", xdm.SequenceLen(args[0]))} + const errCodeFORG0003 = "FORG0003" // standard error category for this function contract + + if xdm.SequenceLen(args[0]) > singletonFunctionArgument { + return nil, &xdm.Error{Code: errCodeFORG0003, Message: fmt.Sprintf("zero-or-one() called with sequence of length %d", xdm.SequenceLen(args[0]))} } return args[0], nil } func fnOneOrMore(_ context.Context, args []xdm.Sequence) (xdm.Sequence, error) { + const errCodeFORG0004 = "FORG0004" // standard error category for this function contract + if xdm.SequenceLen(args[0]) == 0 { - return nil, &xdm.Error{Code: "FORG0004", Message: "one-or-more() called with empty sequence"} + return nil, &xdm.Error{Code: errCodeFORG0004, Message: "one-or-more() called with empty sequence"} } return args[0], nil } func fnExactlyOne(_ context.Context, args []xdm.Sequence) (xdm.Sequence, error) { - if xdm.SequenceLen(args[0]) != 1 { - return nil, &xdm.Error{Code: "FORG0005", Message: fmt.Sprintf("exactly-one() called with sequence of length %d", xdm.SequenceLen(args[0]))} + const errCodeFORG0005 = "FORG0005" // standard error category for this function contract + + if xdm.SequenceLen(args[0]) != singletonFunctionArgument { + return nil, &xdm.Error{Code: errCodeFORG0005, Message: fmt.Sprintf("exactly-one() called with sequence of length %d", xdm.SequenceLen(args[0]))} } return args[0], nil @@ -351,8 +375,10 @@ func (opts deepEqualOptions) stringsEqual(a, b string) bool { } func (opts deepEqualOptions) descend() (deepEqualOptions, error) { + const defaultDeepEqualDepth = 128 // fallback nesting cap when no invocation depth is supplied + opts.depth++ - if opts.depth > DepthLimit(opts.ctx, 128) { + if opts.depth > DepthLimit(opts.ctx, defaultDeepEqualDepth) { return opts, ErrRecursionLimit } @@ -402,12 +428,14 @@ func deepEqualSequence(a, b xdm.Sequence, opts deepEqualOptions) (bool, error) { } func deepEqualItem(a, b xdm.Item, opts deepEqualOptions) (bool, error) { + const errCodeFOTY0015 = "FOTY0015" // standard error category for this function contract + if _, function := a.(xdm.FunctionItem); function { - return false, &xdm.Error{Code: "FOTY0015", Message: "deep-equal: cannot compare function items"} + return false, &xdm.Error{Code: errCodeFOTY0015, Message: "deep-equal: cannot compare function items"} } if _, function := b.(xdm.FunctionItem); function { - return false, &xdm.Error{Code: "FOTY0015", Message: "deep-equal: cannot compare function items"} + return false, &xdm.Error{Code: errCodeFOTY0015, Message: "deep-equal: cannot compare function items"} } switch av := a.(type) { @@ -701,13 +729,13 @@ func fnIndexOf(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { return nil, err } - if len(searchAtoms) != 1 { + if len(searchAtoms) != singletonFunctionArgument { return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "index-of: search value must be exactly one atomic item"} } search := searchAtoms[0] // Explicit 3rd arg with empty sequence is a type error (xs:string, not xs:string?) - if len(args) > 2 && xdm.SequenceLen(args[2]) == 0 { + if len(args) > builtinArityTwo && xdm.SequenceLen(args[2]) == 0 { return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "collation argument must not be empty"} } // Resolve collation: explicit 3rd arg, or default collation @@ -851,7 +879,7 @@ func sortCollation(ctx context.Context, args []xdm.Sequence) (*collationImpl, er } func sortKeyFunction(args []xdm.Sequence) (*xdm.FunctionItem, error) { - if len(args) < 3 || xdm.SequenceLen(args[2]) == 0 { + if len(args) < builtinArityThree || xdm.SequenceLen(args[2]) == 0 { return nil, nil } diff --git a/transform/internal/xpath/functions_serialize.go b/transform/internal/xpath/functions_serialize.go index 23aed64..f29a810 100644 --- a/transform/internal/xpath/functions_serialize.go +++ b/transform/internal/xpath/functions_serialize.go @@ -43,10 +43,259 @@ const ( serializeCharacterMapsParamType = "use-character-maps-param-type" ) +// Published serialization schema type, element and attribute spellings; these are not XSLT element names. +const ( + serializeYesNoParamType = "yes-no-param-type" + serializeQNamesParamType = "QNames-param-type" + serializeDecimalParamType = "decimal-param-type" + serializeNMTokenParamType = lexicon.TypeNMToken + serializeParameterTypeSuffix + serializeYesNoOmitParamType = "yes-no-omit-param-type" + serializeBaseParamType = "base-param-type" + serializeDoctypePublicParamType = "doctype-public-param-type" + serializeDoctypeSystemParamType = "doctype-system-param-type" + serializeEncodingParamType = "encoding-param-type" + serializeJSONNodeMethodParamType = "json-node-output-method-param-type" + serializeMethodParamType = "method-param-type" + serializeParameterTypeSuffix = "-param-type" + serializeDomainTypeSuffix = "-type" + serializeYesNoDomainType = "yes-no-type" + serializeYesNoOmitDomainType = "yes-no-omit-type" + serializeCharacterDomainType = "char-type" + serializeEncodingDomainType = "encoding-string-type" + serializePublicIDDomainType = "pubid-char-string-type" + serializeSystemIDDomainType = "system-id-string-type" + serializeMethodDomainType = "method-type" + serializeQNamesDomainType = "QNames-type" + serializeEQNameDomainType = "EQName" + serializeTokenizedStringDomain = "tokenized-string" + serializeParameterRootElement = "serialization-parameters" + serializeAbstractParameterElement = "serialization-parameter-element" + serializeCharacterMapElement = "character-map" + serializeCharacterMapCharacterAttribute = "character" + serializeCharacterMapStringAttribute = "map-string" + serializeXSINilAttribute = "nil" + serializeXSISchemaLocationAttribute = "schemaLocation" + serializeXSINoNamespaceSchemaLocationAttribute = "noNamespaceSchemaLocation" +) + +// Serialization schema editions are independent of XPath source-language editions. +const serializationEdition30 = 30 + +// Shared defaults and cardinalities retain distinct logical roles; counts below are characters or JSON items, not argument arities. +const ( + serializeDefaultOmitDeclaration = true + serializeSchemaExtractCharacterMaps = false + serializeSingleCharacterCount = 1 + serializedJSONValueCardinality = 1 +) + +// HTML metadata uses a version threshold, MIME label, charset framing and root element name; it grants no resource access. +const ( + serializeHTML5Boundary = 5 + serializeHTMLMediaType = "text/html" + serializeCharsetParameterPrefix = "; charset=" + serializeHTMLRootElement = "html" +) + +// Parameter/schema work counts inspected items. HTML validation fallback depth counts nesting levels, separately from evaluator recursion. +const ( + serializeParameterVisitWork = 1 + serializeSchemaVisitWork = 1 + defaultHTMLValidationDepth = 512 +) + +// SerializationParameters is a validated, presence-preserving view of a +// parameter document. It contains no fn:serialize defaults or Unicode-output +// overrides, so a byte serializer can apply its own host defaults afterwards. +type SerializationParameters struct { + // Strings contains supplied string-valued serialization parameters by local name. + Strings map[string]string + // Booleans contains supplied boolean parameters, preserving explicit false values. + Booleans map[string]bool + // Names contains resolved expanded-name lists for name-valued parameters. + Names map[string][]string + // Characters maps input characters to verbatim character-map replacements. + Characters map[rune]string + // CharactersSet distinguishes an explicitly supplied character map from an absent + // parameter. + CharactersSet bool + // Reset lists recognized map parameters explicitly supplied as an empty sequence. + Reset []string +} + +// ReadSerializationParameters applies the selected published parameter schema +// and namespace-resolution rules without validating method-specific output +// restrictions. Those restrictions depend on the receiving host's final method. +func ReadSerializationParameters(ctx context.Context, root *xdm.Element, edition int) (SerializationParameters, error) { + var result SerializationParameters + + opts, err := parseSerializeOptionsNode(serializeOptions{ctx: ctx, edition: edition}, root) + if err != nil { + return result, err + } + + result.Strings = make(map[string]string) + result.Booleans = make(map[string]bool) + result.Names = make(map[string][]string) + + for child := range xdm.Children(root) { + if err := Charge(ctx, serializeParameterVisitWork); err != nil { + return result, err + } + + param, ok := child.(*xdm.Element) + if !ok || param.URI() != lexicon.NamespaceSerialization || serializeParameterType(param.LocalName(), edition) == "" { + continue + } + + name := param.LocalName() + if target, boolean := opts.booleanParam(name); boolean { + if target != nil { + result.Booleans[name] = *target + } else { + // fn:serialize ignores byte-order-mark for its string result, + // but the byte host must retain its validated explicit value. + value, err := readSerializeParamYesNo(param, edition) + if err != nil { + return result, err + } + + result.Booleans[name] = value + } + + continue + } + + if target := opts.stringParam(name); target != nil { + result.Strings[name] = *target + continue + } + + switch name { + case lexicon.SerializationCDATASectionElements: + result.Names[name] = slices.Sorted(maps.Keys(opts.cdataElements)) + case lexicon.SerializationSuppressIndentation: + result.Names[name] = slices.Sorted(maps.Keys(opts.suppressIndent)) + case lexicon.SerializationUseCharacterMaps: + result.Characters, result.CharactersSet = opts.charMap, true + } + } + + return result, nil +} + +// ReadSerializationParameterMap validates map-form parameters using the same +// conversions as fn:serialize, retaining only supplied values and explicit resets. +// No fn:serialize defaults or method-specific Unicode-output overrides escape. +func ReadSerializationParameterMap(ctx context.Context, params xdm.MapItem, edition int) (SerializationParameters, error) { + var result SerializationParameters + if edition == serializationEdition30 { + return result, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "Serialization 3.0 parameters must be an element"} + } + + opts, err := parseSerializeOptionsMap(ctx, serializeOptions{ctx: ctx, edition: edition}, params) + if err != nil { + return result, err + } + + result.Strings, result.Booleans, result.Names = make(map[string]string), make(map[string]bool), make(map[string][]string) + + for _, key := range params.Keys() { + if err := Charge(ctx, serializeParameterVisitWork); err != nil { + return SerializationParameters{}, err + } + // A QName extension is never a standard string option with the same local name. + if !xdm.IsStringDerived(key.TypeName) && key.TypeName != xdm.TypeUntypedAtomic && key.TypeName != xdm.TypeAnyURI { + continue + } + + name, err := atomicToString(key) + if err != nil { + return SerializationParameters{}, err + } + + if serializeParameterType(name, edition) == "" { + continue + } + + value, _ := params.Get(key) + if xdm.SequenceLen(value) == 0 { + result.Reset = append(result.Reset, name) + continue + } + + if target, boolean := opts.booleanParam(name); boolean { + if target != nil { + result.Booleans[name] = *target + } else { + result.Booleans[name], _, err = readSerializeBooleanOption(ctx, params, name) + if err != nil { + return SerializationParameters{}, err + } + } + + continue + } + + if target := opts.stringParam(name); target != nil { + result.Strings[name] = *target + continue + } + + switch name { + case lexicon.SerializationCDATASectionElements: + result.Names[name] = slices.Sorted(maps.Keys(opts.cdataElements)) + case lexicon.SerializationSuppressIndentation: + result.Names[name] = slices.Sorted(maps.Keys(opts.suppressIndent)) + case lexicon.SerializationUseCharacterMaps: + result.Characters, result.CharactersSet = opts.charMap, true + } + } + + return result, nil +} + +// SerializeNodeForJSON returns the inner node serialization before JSON string +// escaping. XPath and XSLT share the node-kind checks and XML/HTML/text rules. +func SerializeNodeForJSON(ctx context.Context, v xdm.NodeItem, method string) (string, error) { + if method == "" { + method = lexicon.MethodXML + } + // SER31 §9 supplies only the node output method and omit-xml-declaration. + // JSON character maps and normalization apply to the resulting JSON string. + nodeOpts := serializeOptions{ + ctx: ctx, + method: method, omitXMLDeclaration: jsonNodeOmitXMLDeclaration, + standalone: lexicon.ValueOmit, includeContentType: jsonNodeIncludeContentType, escapeURIAttributes: jsonNodeEscapeURIAttributes, + } + if err := nodeOpts.validate(); err != nil { + return "", err + } + + switch method { + case lexicon.MethodText: + return serializeTextSequence(xdm.ItemSlice{v}, nodeOpts) + case lexicon.MethodHTML: + return serializeHTMLSequence(xdm.ItemSlice{v}, nodeOpts) + default: + return serializeXMLSequence(xdm.ItemSlice{v}, nodeOpts) + } +} + +// ValidateHTMLItem checks the actual normalized item before HTML serialization. +// It is shared with XSLT's streaming writer, which need not have a result DOM. +func ValidateHTMLItem(ctx context.Context, item xdm.XMLItem, html5 bool) error { + if item.Node == nil { + return checkHTMLCharacters(ctx, item.Text, html5) + } + + return validateHTMLNode(ctx, item.Node, html5, 0) +} + func lookupFunctionsSerialize(name string) (*builtinFunc, bool) { switch name { - case "serialize": - return &builtinFunc{name: name, min: 1, max: 2, fn: fnSerialize}, true + case fnNameSerialize: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityTwo, fn: fnSerialize}, true } return nil, false @@ -258,7 +507,7 @@ func (opts serializeOptions) methodEmitsDoctype() bool { // validate checks method-specific parameter constraints before output starts. func (opts serializeOptions) validate() error { - if opts.edition == 30 && (opts.method == lexicon.MethodJSON || opts.method == lexicon.MethodAdaptive) { + if opts.edition == serializationEdition30 && (opts.method == lexicon.MethodJSON || opts.method == lexicon.MethodAdaptive) { return &xdm.Error{Code: lexicon.ErrSEPM0016, Message: fmt.Sprintf("output method %q is not defined by Serialization 3.0", opts.method)} } // An extension method (a prefixed QName) is a valid method-type value but @@ -435,33 +684,43 @@ func isBuiltinSerializeMethod(v string) bool { } func parseSerializeOptions(ctx context.Context, args []xdm.Sequence) (serializeOptions, error) { + // These host defaults differ from JSON node embedding; presence and map-item + // cardinality are independent argument constraints. + const ( + serializeDefaultIncludeContentType = true + serializeDefaultEscapeURIAttributes = true + serializeDefaultItemSeparator = " " + serializeOptionsArgumentThreshold = 1 + serializeParameterArgumentCardinality = 1 + ) + // The host defaults to XML without a declaration for both node and atomic // input. Adaptive output must be requested explicitly. opts := serializeOptions{ ctx: ctx, - omitXMLDeclaration: true, + omitXMLDeclaration: serializeDefaultOmitDeclaration, method: "", - itemSeparator: " ", + itemSeparator: serializeDefaultItemSeparator, // The Serialization 3.1 default for the standalone parameter is "omit". standalone: lexicon.ValueOmit, // include-content-type and escape-uri-attributes default to yes. - includeContentType: true, - escapeURIAttributes: true, + includeContentType: serializeDefaultIncludeContentType, + escapeURIAttributes: serializeDefaultEscapeURIAttributes, } - if ec := getFnContext(ctx); ec != nil && ec.version == 30 { - opts.edition = 30 + if ec := getFnContext(ctx); ec != nil && ec.version == xpathVersion30 { + opts.edition = serializationEdition30 } - if len(args) <= 1 || xdm.SequenceLen(args[1]) == 0 { + if len(args) <= serializeOptionsArgumentThreshold || xdm.SequenceLen(args[1]) == 0 { return opts, nil } - if xdm.SequenceLen(args[1]) != 1 { + if xdm.SequenceLen(args[1]) != serializeParameterArgumentCardinality { return opts, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "serialize options must be a singleton"} } if m, ok := args[1].Get(0).(xdm.MapItem); ok { - if opts.edition == 30 { + if opts.edition == serializationEdition30 { return opts, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "XPath 3.0 serialization parameters must be an output:serialization-parameters element"} } @@ -477,11 +736,13 @@ func parseSerializeOptions(ctx context.Context, args []xdm.Sequence) (serializeO } func parseSerializeOptionsMap(ctx context.Context, opts serializeOptions, m xdm.MapItem) (serializeOptions, error) { + const serializeDefaultHTMLVersion = "5" // map-option HTML edition default + // Map-form parameters default to UTF-8, including its XML declaration label. opts.encoding = lexicon.EncodingUTF8U // F&O 3.1 section 14.7.3 fixes this map-option default at 5. // It is supplied even when the separate version option is explicit. - opts.htmlVersion = "5" + opts.htmlVersion = serializeDefaultHTMLVersion var err error @@ -530,7 +791,7 @@ func parseSerializeMapOutputOptions(ctx context.Context, opts serializeOptions, // supplying an empty map has the same effect as omitting the argument, and // the XML declaration is omitted unless the option requests otherwise // (F&O 3.1 §14.7.3; W3C serialize-xml-127a). - opts.omitXMLDeclaration = true + opts.omitXMLDeclaration = serializeDefaultOmitDeclaration omit, found, err := readSerializeBooleanOption(ctx, m, lexicon.SerializationOmitXMLDeclaration) if err != nil { @@ -550,17 +811,17 @@ func parseSerializeMapOutputOptions(ctx context.Context, opts serializeOptions, opts.allowDuplicateNames = allow } - encoding, found, err := readSerializeStringOption(ctx, m, lexicon.SerializationEncoding) + enc, found, err := readSerializeStringOption(ctx, m, lexicon.SerializationEncoding) if err != nil { return opts, err } if found { - if !xmlchar.IsValidEncName(encoding) { + if !xmlchar.IsValidEncName(enc) { return opts, &xdm.Error{Code: lexicon.ErrSEPM0016, Message: "invalid serialization encoding name"} } - opts.encoding = encoding + opts.encoding = enc } return opts, nil @@ -713,14 +974,21 @@ func parseSerializeMapContentOptions(ctx context.Context, opts serializeOptions, // serializeOptionValue applies the map option's declared function conversion, // independently of an enclosing XSLT backwards-compatibility mode. func serializeOptionValue(ctx context.Context, value xdm.Sequence, target string, occurrence xdm.Occurrence, name string) (xdm.Sequence, error) { + // Option conversion uses modern strict coercion and its synthetic first + // argument for diagnostics, independently of caller compatibility mode. + const ( + serializeXPath10Compatibility = false + serializeOptionDiagnosticIndex = 0 + ) + var strict evalContext if ec := getFnContext(ctx); ec != nil { strict = *ec } - strict.xpath10Compat = false + strict.xpath10Compat = serializeXPath10Compatibility - return coerceFuncallArg(ctx, value, stAtomic(target, occurrence), "serialize option "+name, 0, &strict) + return coerceFuncallArg(ctx, value, stAtomic(target, occurrence), "serialize option "+name, serializeOptionDiagnosticIndex, &strict) } func serializeOptionalAtomic(ctx context.Context, value xdm.Sequence, target, name string) (xdm.AtomicValue, bool, error) { @@ -829,6 +1097,10 @@ func resolveSerializeJSONNodeMethodMap(ctx context.Context, m xdm.MapItem) (stri // applies the parameter's lexical domain to string values. Empty values request // the default; built-in methods must be strings rather than no-namespace QNames. func resolveSerializeMethodOption(ctx context.Context, m xdm.MapItem, name string, valid func(string) bool) (string, bool, error) { + const ( + serializeMethodValueCardinality = 1 // atomized union member count + ) + v, present := m.Get(xdm.AtomicValue{TypeName: xdm.TypeString, Value: name}) if !present { return "", false, nil @@ -843,7 +1115,7 @@ func resolveSerializeMethodOption(ctx context.Context, m xdm.MapItem, name strin return "", false, nil } - if len(atoms) != 1 { + if len(atoms) != serializeMethodValueCardinality { return "", true, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: fmt.Sprintf("serialize option %q must be a singleton", name)} } @@ -864,7 +1136,7 @@ func resolveSerializeMethodOption(ctx context.Context, m xdm.MapItem, name strin // The expected type is a namespace-sensitive pure union. Unlike an // expected xs:string, it does not permit URI promotion or untyped casting. if av.TypeName == xdm.TypeUntypedAtomic { - return "", true, &xdm.Error{Code: "XPTY0117", Message: "untypedAtomic cannot be converted to the serialization method union"} + return "", true, &xdm.Error{Code: errCodeXPTY0117, Message: "untypedAtomic cannot be converted to the serialization method union"} } if !atomicMatchesTargetType(av, xdm.TypeString, getFnContext(ctx)) { @@ -906,7 +1178,7 @@ func parseSerializeOptionsNode(opts serializeOptions, n xdm.Node) (serializeOpti seen[key] = struct{}{} if param.URI() != lexicon.NamespaceSerialization { - if opts.edition == 30 { + if opts.edition == serializationEdition30 { return opts, &xdm.Error{Code: errCodeSEPM0017, Message: "Serialization 3.0 does not declare extension parameter elements"} } @@ -924,122 +1196,17 @@ func parseSerializeOptionsNode(opts serializeOptions, n xdm.Node) (serializeOpti return opts, nil } -// SerializationParameters is a validated, presence-preserving view of a -// parameter document. It contains no fn:serialize defaults or Unicode-output -// overrides, so a byte serializer can apply its own host defaults afterwards. -type SerializationParameters struct { - // Strings contains supplied string-valued serialization parameters by local name. - Strings map[string]string - // Booleans contains supplied boolean parameters, preserving explicit false values. - Booleans map[string]bool - // Names contains resolved expanded-name lists for name-valued parameters. - Names map[string][]string - // Characters maps input characters to verbatim character-map replacements. - Characters map[rune]string - // CharactersSet distinguishes an explicitly supplied character map from an absent - // parameter. - CharactersSet bool -} - -// ReadSerializationParameters applies the selected published parameter schema -// and namespace-resolution rules without validating method-specific output -// restrictions. Those restrictions depend on the receiving host's final method. -func ReadSerializationParameters(ctx context.Context, root *xdm.Element, edition int) (SerializationParameters, error) { - var result SerializationParameters - - opts, err := parseSerializeOptionsNode(serializeOptions{ctx: ctx, edition: edition}, root) - if err != nil { - return result, err - } - - result.Strings = make(map[string]string) - result.Booleans = make(map[string]bool) - result.Names = make(map[string][]string) - - for child := range xdm.Children(root) { - if err := Charge(ctx, 1); err != nil { - return result, err - } - - param, ok := child.(*xdm.Element) - if !ok || param.URI() != lexicon.NamespaceSerialization || serializeParameterType(param.LocalName(), edition) == "" { - continue - } - - name := param.LocalName() - if target, boolean := opts.booleanParam(name); boolean { - if target != nil { - result.Booleans[name] = *target - } else { - // fn:serialize ignores byte-order-mark for its string result, - // but the byte host must retain its validated explicit value. - value, err := readSerializeParamYesNo(param, edition) - if err != nil { - return result, err - } - - result.Booleans[name] = value - } - - continue - } - - if target := opts.stringParam(name); target != nil { - result.Strings[name] = *target - continue - } - - switch name { - case lexicon.SerializationCDATASectionElements: - result.Names[name] = slices.Sorted(maps.Keys(opts.cdataElements)) - case lexicon.SerializationSuppressIndentation: - result.Names[name] = slices.Sorted(maps.Keys(opts.suppressIndent)) - case lexicon.SerializationUseCharacterMaps: - result.Characters, result.CharactersSet = opts.charMap, true - } - } - - return result, nil -} - -// SerializeNodeForJSON returns the inner node serialization before JSON string -// escaping. XPath and XSLT share the node-kind checks and XML/HTML/text rules. -func SerializeNodeForJSON(ctx context.Context, v xdm.NodeItem, method string) (string, error) { - if method == "" { - method = lexicon.MethodXML - } - // SER31 §9 supplies only the node output method and omit-xml-declaration. - // JSON character maps and normalization apply to the resulting JSON string. - nodeOpts := serializeOptions{ - ctx: ctx, - method: method, omitXMLDeclaration: true, - standalone: lexicon.ValueOmit, includeContentType: true, escapeURIAttributes: true, - } - if err := nodeOpts.validate(); err != nil { - return "", err - } - - switch method { - case lexicon.MethodText: - return serializeTextSequence(xdm.ItemSlice{v}, nodeOpts) - case lexicon.MethodHTML: - return serializeHTMLSequence(xdm.ItemSlice{v}, nodeOpts) - default: - return serializeXMLSequence(xdm.ItemSlice{v}, nodeOpts) - } -} - func readSerializeOptionsRoot(n xdm.Node, edition int) (*xdm.Element, error) { elem, ok := n.(*xdm.Element) if !ok { return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "serialize options node must be an element"} } - if elem.URI() != lexicon.NamespaceSerialization || elem.LocalName() != "serialization-parameters" { + if elem.URI() != lexicon.NamespaceSerialization || elem.LocalName() != serializeParameterRootElement { return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "serialize options root must be output:serialization-parameters"} } - if edition == 30 { + if edition == serializationEdition30 { for _, attr := range elem.Attributes() { if attr.URI() != lexicon.NamespaceXSI || !serializeInstanceAttribute(attr.LocalName()) { return nil, &xdm.Error{Code: errCodeSEPM0017, Message: "Serialization 3.0 options root must not have attributes"} @@ -1056,7 +1223,7 @@ func readSerializeOptionsRoot(n xdm.Node, edition int) (*xdm.Element, error) { func serializeInstanceAttribute(name string) bool { switch name { - case lexicon.AttrType, "nil", "schemaLocation", "noNamespaceSchemaLocation": + case lexicon.AttrType, serializeXSINilAttribute, serializeXSISchemaLocationAttribute, serializeXSINoNamespaceSchemaLocationAttribute: return true } @@ -1068,7 +1235,7 @@ func serializeInstanceAttribute(name string) bool { func readSerializeOptionsChild(child xdm.Node, edition int) (*xdm.Element, error) { switch child.Type() { case xdm.TextNode, xdm.CDATASectionNode: - if edition != 30 || isXSDWhitespaceOnly(xdm.CharacterData(child)) { + if edition != serializationEdition30 || isXSDWhitespaceOnly(xdm.CharacterData(child)) { return nil, nil } @@ -1081,7 +1248,7 @@ func readSerializeOptionsChild(child xdm.Node, edition int) (*xdm.Element, error } param, _ := xdm.AsNode[*xdm.Element](child) - if edition == 30 && param.URI() == "" { + if edition == serializationEdition30 && param.URI() == "" { return nil, &xdm.Error{Code: errCodeSEPM0017, Message: "serialize options parameters must be namespace-qualified"} } @@ -1089,13 +1256,15 @@ func readSerializeOptionsChild(child xdm.Node, edition int) (*xdm.Element, error } func applySerializeNodeParam(opts serializeOptions, param *xdm.Element) (serializeOptions, error) { + const serializeExtractCharacterMaps = true // active parameter entries are extracted + name := param.LocalName() - if opts.edition == 30 && (name == lexicon.SerializationAllowDuplicateNames || name == lexicon.SerializationJSONNodeOutputMethod) { + if opts.edition == serializationEdition30 && (name == lexicon.SerializationAllowDuplicateNames || name == lexicon.SerializationJSONNodeOutputMethod) { return opts, &xdm.Error{Code: errCodeSEPM0017, Message: fmt.Sprintf("serialization parameter %q is not defined by Serialization 3.0", name)} } parameterType := serializeParameterType(name, opts.edition) - if opts.edition != 30 && parameterType == "" { + if opts.edition != serializationEdition30 && parameterType == "" { // SER31 validates the selected standard parameters individually. A // child outside that finite set participates only in duplicate checks. return opts, nil @@ -1146,7 +1315,7 @@ func applySerializeNodeParam(opts serializeOptions, param *xdm.Element) (seriali opts.suppressIndent = names } case lexicon.SerializationUseCharacterMaps: - charMap, err := resolveSerializeCharacterMapsElement(opts, param, true) + charMap, err := resolveSerializeCharacterMapsElement(opts, param, serializeExtractCharacterMaps) if err != nil { return opts, err } @@ -1167,26 +1336,26 @@ func serializeParameterType(name string, edition int) string { case lexicon.SerializationByteOrderMark, lexicon.SerializationEscapeURIAttributes, lexicon.SerializationIncludeContentType, lexicon.SerializationIndent, lexicon.SerializationOmitXMLDeclaration, lexicon.SerializationUndeclarePrefixes, lexicon.SerializationAllowDuplicateNames: - return "yes-no-param-type" + return serializeYesNoParamType case lexicon.SerializationCDATASectionElements, lexicon.SerializationSuppressIndentation: - return "QNames-param-type" + return serializeQNamesParamType case lexicon.SerializationHTMLVersion: - return "decimal-param-type" + return serializeDecimalParamType case lexicon.SerializationItemSeparator: return serializeStringParamType case lexicon.SerializationMediaType, lexicon.SerializationVersion: - if edition == 30 { + if edition == serializationEdition30 { return serializeStringParamType } return serializeTokenizedStringParamType case lexicon.SerializationNormalizationForm: - return "NMTOKEN-param-type" + return serializeNMTokenParamType case lexicon.SerializationStandalone: - return "yes-no-omit-param-type" + return serializeYesNoOmitParamType case lexicon.SerializationDoctypePublic, lexicon.SerializationDoctypeSystem, lexicon.SerializationEncoding, lexicon.SerializationMethod, lexicon.SerializationJSONNodeOutputMethod, lexicon.SerializationUseCharacterMaps: - return name + "-param-type" + return name + serializeParameterTypeSuffix } return "" @@ -1199,7 +1368,7 @@ func validateSerializeInstanceAttributes(elem *xdm.Element, expectedType string) } switch attr.LocalName() { - case "nil": + case serializeXSINilAttribute: // XSD Element Locally Valid (Element) forbids xsi:nil entirely // when the declaration is not nillable, even xsi:nil="false". return &xdm.Error{Code: errCodeSEPM0017, Message: "serialization parameter element is not nillable"} @@ -1227,7 +1396,7 @@ func validateSerializeInstanceAttributes(elem *xdm.Element, expectedType string) // its XSD 1.0 builtins. No extension schemas are loaded. An undeclared foreign // element still assesses declared descendants and an explicit xsi:type. func validateSerializeLaxElement(opts serializeOptions, elem *xdm.Element) error { - if err := Charge(opts.ctx, 1); err != nil { + if err := Charge(opts.ctx, serializeSchemaVisitWork); err != nil { return err } @@ -1270,15 +1439,15 @@ func validateSerializeDeclaredElement(opts serializeOptions, elem *xdm.Element) } name := elem.LocalName() - if name == "serialization-parameters" { + if name == serializeParameterRootElement { return true, validateSerializeLaxContainer(opts, elem) } - if name == "serialization-parameter-element" { + if name == serializeAbstractParameterElement { return true, serializeSchemaError("serialization-parameter-element is abstract") } - if serializeParameterType(name, opts.edition) == "" || opts.edition == 30 && (name == lexicon.SerializationAllowDuplicateNames || name == lexicon.SerializationJSONNodeOutputMethod) { + if serializeParameterType(name, opts.edition) == "" || opts.edition == serializationEdition30 && (name == lexicon.SerializationAllowDuplicateNames || name == lexicon.SerializationJSONNodeOutputMethod) { return false, nil } @@ -1289,7 +1458,7 @@ func validateSerializeDeclaredElement(opts serializeOptions, elem *xdm.Element) return true, err } - _, err := resolveSerializeCharacterMapsElement(opts, elem, false) + _, err := resolveSerializeCharacterMapsElement(opts, elem, serializeSchemaExtractCharacterMaps) return true, err } @@ -1301,7 +1470,7 @@ func validateSerializeDeclaredElement(opts serializeOptions, elem *xdm.Element) func validateSerializeLaxChildren(opts serializeOptions, elem *xdm.Element) error { for child := range xdm.Children(elem) { - if err := Charge(opts.ctx, 1); err != nil { + if err := Charge(opts.ctx, serializeSchemaVisitWork); err != nil { return err } @@ -1318,16 +1487,16 @@ func validateSerializeLaxChildren(opts serializeOptions, elem *xdm.Element) erro func validateSerializeLaxContainer(opts serializeOptions, elem *xdm.Element) error { // The global schema declaration has no attributes in either edition; this // differs from the unvalidated outer container in the SER31 procedure. - if _, err := readSerializeOptionsRoot(elem, 30); err != nil { + if _, err := readSerializeOptionsRoot(elem, serializationEdition30); err != nil { return err } for child := range xdm.Children(elem) { - if err := Charge(opts.ctx, 1); err != nil { + if err := Charge(opts.ctx, serializeSchemaVisitWork); err != nil { return err } - param, err := readSerializeOptionsChild(child, 30) + param, err := readSerializeOptionsChild(child, serializationEdition30) if err != nil { return err } @@ -1337,10 +1506,10 @@ func validateSerializeLaxContainer(opts serializeOptions, elem *xdm.Element) err } if param.URI() == lexicon.NamespaceSerialization { - if serializeParameterType(param.LocalName(), opts.edition) == "" || opts.edition == 30 && (param.LocalName() == lexicon.SerializationAllowDuplicateNames || param.LocalName() == lexicon.SerializationJSONNodeOutputMethod) { + if serializeParameterType(param.LocalName(), opts.edition) == "" || opts.edition == serializationEdition30 && (param.LocalName() == lexicon.SerializationAllowDuplicateNames || param.LocalName() == lexicon.SerializationJSONNodeOutputMethod) { return serializeSchemaError("unknown child of serialization-parameters") } - } else if opts.edition == 30 { + } else if opts.edition == serializationEdition30 { return serializeSchemaError("Serialization 3.0 container has no foreign-element wildcard") } @@ -1353,16 +1522,16 @@ func validateSerializeLaxContainer(opts serializeOptions, elem *xdm.Element) err } func validateSerializeNamedType(opts serializeOptions, elem *xdm.Element, name string) error { - if opts.edition == 30 && name == serializeQNameOrEQNameType { + if opts.edition == serializationEdition30 && name == serializeQNameOrEQNameType { return serializeSchemaError("unknown Serialization 3.0 type") } if name == serializeCharacterMapsParamType { - _, err := resolveSerializeCharacterMapsElement(opts, elem, false) + _, err := resolveSerializeCharacterMapsElement(opts, elem, serializeSchemaExtractCharacterMaps) return err } - if name == "base-param-type" { + if name == serializeBaseParamType { if err := validateSerializeMapAttributes(elem); err != nil { return err } @@ -1377,22 +1546,22 @@ func validateSerializeNamedType(opts serializeOptions, elem *xdm.Element, name s domain := "" switch name { - case serializeStringParamType, serializeTokenizedStringParamType, "decimal-param-type", "NMTOKEN-param-type": - if name == serializeTokenizedStringParamType && opts.edition == 30 { + case serializeStringParamType, serializeTokenizedStringParamType, serializeDecimalParamType, serializeNMTokenParamType: + if name == serializeTokenizedStringParamType && opts.edition == serializationEdition30 { return serializeSchemaError("unknown Serialization 3.0 type") } - domain = strings.TrimSuffix(name, "-param-type") - case "yes-no-param-type", "yes-no-omit-param-type", "QNames-param-type", "method-param-type": - domain = strings.TrimSuffix(name, "-param-type") + "-type" - case "doctype-public-param-type": - domain = "pubid-char-string-type" - case "doctype-system-param-type": - domain = "system-id-string-type" - case "encoding-param-type": - domain = "encoding-string-type" - case "json-node-output-method-param-type": - if opts.edition == 30 { + domain = strings.TrimSuffix(name, serializeParameterTypeSuffix) + case serializeYesNoParamType, serializeYesNoOmitParamType, serializeQNamesParamType, serializeMethodParamType: + domain = strings.TrimSuffix(name, serializeParameterTypeSuffix) + serializeDomainTypeSuffix + case serializeDoctypePublicParamType: + domain = serializePublicIDDomainType + case serializeDoctypeSystemParamType: + domain = serializeSystemIDDomainType + case serializeEncodingParamType: + domain = serializeEncodingDomainType + case serializeJSONNodeMethodParamType: + if opts.edition == serializationEdition30 { return serializeSchemaError("unknown Serialization 3.0 type") } @@ -1418,7 +1587,7 @@ func validateSerializeNamedType(opts serializeOptions, elem *xdm.Element, name s func serializeSimpleContent(opts serializeOptions, elem *xdm.Element) (string, error) { for _, attr := range elem.Attributes() { - if err := Charge(opts.ctx, 1); err != nil { + if err := Charge(opts.ctx, serializeSchemaVisitWork); err != nil { return "", err } @@ -1428,7 +1597,7 @@ func serializeSimpleContent(opts serializeOptions, elem *xdm.Element) (string, e } for child := range xdm.Children(elem) { - if err := Charge(opts.ctx, 1); err != nil { + if err := Charge(opts.ctx, serializeSchemaVisitWork); err != nil { return "", err } @@ -1446,7 +1615,7 @@ func serializeSimpleContent(opts serializeOptions, elem *xdm.Element) (string, e } func validateSerializeBuiltinType(opts serializeOptions, elem *xdm.Element, name string) error { - if !xdm.IsKnownXSDType("xs:" + name) { + if !xdm.IsKnownXSDType(lexicon.PrefixXS + ":" + name) { return serializeSchemaError("unknown XSD xsi:type") } @@ -1506,34 +1675,34 @@ func validateSerializeTypeValue(opts serializeOptions, elem *xdm.Element, name, valid := true switch name { - case "string", "tokenized-string": + case lexicon.TypeString, serializeTokenizedStringDomain: return nil case lexicon.TypeDecimal: valid = isValidXSDecimal(token) case lexicon.TypeNMToken: valid = serializeNormalizationFormValid(token) - case "yes-no-type", "yes-no-omit-type": + case serializeYesNoDomainType, serializeYesNoOmitDomainType: _, valid = serializeEditionBooleanValue(token, opts.edition) - valid = valid || name == "yes-no-omit-type" && token == lexicon.ValueOmit - case "char-type": - valid = utf8.RuneCountInString(value) == 1 - case "encoding-string-type": - valid = xmlchar.IsValidEncName(token) && (opts.edition != 30 || value == token) - case "pubid-char-string-type": + valid = valid || name == serializeYesNoOmitDomainType && token == lexicon.ValueOmit + case serializeCharacterDomainType: + valid = utf8.RuneCountInString(value) == serializeSingleCharacterCount + case serializeEncodingDomainType: + valid = xmlchar.IsValidEncName(token) && (opts.edition != serializationEdition30 || value == token) + case serializePublicIDDomainType: valid = xmloutput.ValidPublicID(strings.ReplaceAll(value, "\t", " ")) - case "system-id-string-type": + case serializeSystemIDDomainType: valid = xmloutput.ValidSystemID(value) - case "method-type", serializeJSONNodeMethodType: - if name == serializeJSONNodeMethodType && opts.edition == 30 { + case serializeMethodDomainType, serializeJSONNodeMethodType: + if name == serializeJSONNodeMethodType && opts.edition == serializationEdition30 { return serializeSchemaError("unknown Serialization 3.0 type") } - _, err := readSerializeNodeMethodValue(elem, value, token, opts.edition, name == "method-type") + _, err := readSerializeNodeMethodValue(elem, value, token, opts.edition, name == serializeMethodDomainType) return err - case "QNames-type": + case serializeQNamesDomainType: for _, part := range xsdListFields(value) { - if err := Charge(opts.ctx, 1); err != nil { + if err := Charge(opts.ctx, serializeSchemaVisitWork); err != nil { return err } @@ -1541,7 +1710,7 @@ func validateSerializeTypeValue(opts serializeOptions, elem *xdm.Element, name, return err } } - case serializeQNameOrEQNameType, "EQName", serializePrefixedQNameType, serializeQualifiedEQNameType: + case serializeQNameOrEQNameType, serializeEQNameDomainType, serializePrefixedQNameType, serializeQualifiedEQNameType: return validateSerializeQNameType(opts, elem, name, token) default: valid = false @@ -1555,19 +1724,19 @@ func validateSerializeTypeValue(opts serializeOptions, elem *xdm.Element, name, } func validateSerializeQNameType(opts serializeOptions, elem *xdm.Element, name, token string) error { - if opts.edition == 30 && name != serializeQNameOrEQNameType { + if opts.edition == serializationEdition30 && name != serializeQNameOrEQNameType { return serializeSchemaError("unknown Serialization 3.0 type") } if uri, local, eqname := parseEQNameToken(token); eqname { - if opts.edition == 30 || name == serializePrefixedQNameType || !xmlchar.IsValidNCName(local) || name == serializeQualifiedEQNameType && strings.Trim(uri, lexicon.XMLWhitespace) == "" { + if opts.edition == serializationEdition30 || name == serializePrefixedQNameType || !xmlchar.IsValidNCName(local) || name == serializeQualifiedEQNameType && strings.Trim(uri, lexicon.XMLWhitespace) == "" { return serializeSchemaError("invalid published EQName type") } return nil } - if name == "EQName" || name == serializeQualifiedEQNameType || name == serializePrefixedQNameType && !strings.Contains(token, ":") { + if name == serializeEQNameDomainType || name == serializeQualifiedEQNameType || name == serializePrefixedQNameType && !strings.Contains(token, ":") { return serializeSchemaError("invalid published QName type") } @@ -1597,7 +1766,7 @@ func readSerializeNodeStringParam(param *xdm.Element, edition int) (string, erro switch name { case lexicon.SerializationEncoding: - if !xmlchar.IsValidEncName(token) || edition == 30 && value != token { + if !xmlchar.IsValidEncName(token) || edition == serializationEdition30 && value != token { return "", &xdm.Error{Code: errCodeSEPM0017, Message: "invalid serialization encoding name"} } case lexicon.SerializationMethod, lexicon.SerializationJSONNodeOutputMethod: @@ -1611,7 +1780,7 @@ func readSerializeNodeStringParam(param *xdm.Element, edition int) (string, erro return "", &xdm.Error{Code: errCodeSEPM0017, Message: fmt.Sprintf("serialize parameter html-version must be an xs:decimal, got %q", value)} } case lexicon.SerializationDoctypePublic: - if edition != 30 { + if edition != serializationEdition30 { value = xmlchar.CollapseSpace(value) } @@ -1627,13 +1796,13 @@ func readSerializeNodeStringParam(param *xdm.Element, edition int) (string, erro return value, nil case lexicon.SerializationMediaType: - if edition != 30 { + if edition != serializationEdition30 { return xmlchar.CollapseSpace(value), nil } return value, nil case lexicon.SerializationVersion: - if edition == 30 { + if edition == serializationEdition30 { return value, nil // SER30 uses xs:string, SER31 uses xs:token. } default: @@ -1648,7 +1817,7 @@ func readSerializeNodeMethod(param *xdm.Element, value, token string, edition in } func readSerializeNodeMethodValue(param *xdm.Element, value, token string, edition int, method bool) (string, error) { - if edition != 30 { + if edition != serializationEdition30 { // The published 3.1 union also admits Q{\s*}builtin. It denotes // the same absent-namespace method as the corresponding token. if uri, local, ok := parseEQNameToken(token); ok && strings.Trim(uri, lexicon.XMLWhitespace) == "" && isBuiltinSerializeMethod(local) { @@ -1659,7 +1828,7 @@ func readSerializeNodeMethodValue(param *xdm.Element, value, token string, editi valid := serializeJSONNodeOutputMethodValid(token) if method { valid = serializeMethodValid(token) - if edition == 30 && (isBuiltinSerializeMethod(token) && value != token || + if edition == serializationEdition30 && (isBuiltinSerializeMethod(token) && value != token || token == lexicon.MethodJSON || token == lexicon.MethodAdaptive || strings.HasPrefix(token, "Q{")) { valid = false } @@ -1794,7 +1963,7 @@ func readSerializeParamValue(elem *xdm.Element) (string, error) { continue // base-param-type permits foreign attributes. } - if attr.URI() != "" || attr.LocalName() != "value" { + if attr.URI() != "" || attr.LocalName() != lexicon.AttrValue { return "", &xdm.Error{Code: errCodeSEPM0017, Message: "serialize parameter must use an unqualified value attribute"} } @@ -1827,7 +1996,7 @@ func serializeBooleanValue(value string) (string, bool) { // Serialization 3.0 parameter documents admit only yes/no; the boolean // synonyms were added in the 3.1 schema. func serializeEditionBooleanValue(value string, edition int) (string, bool) { - if edition == 30 { + if edition == serializationEdition30 { value = strings.Trim(value, lexicon.XMLWhitespace) return value, value == lexicon.ValueYes || value == lexicon.ValueNo } @@ -1890,7 +2059,7 @@ func readSerializeParamQNameNames(elem *xdm.Element, edition int) (map[string]st // (#x20/#x9/#xA/#xD) — NBSP and other Unicode whitespace stay inside the token // and then fail NCName validation, and are never silently split. for _, token := range xsdListFields(value) { - if edition == 30 && strings.HasPrefix(token, "Q{") { + if edition == serializationEdition30 && strings.HasPrefix(token, "Q{") { return nil, &xdm.Error{Code: errCodeSEPM0017, Message: "Serialization 3.0 requires lexical QNames"} } @@ -2095,7 +2264,7 @@ func resolveSerializeCharacterMapsElement(opts serializeOptions, elem *xdm.Eleme continue } - if foreignChild || charMap.URI() != lexicon.NamespaceSerialization || charMap.LocalName() != "character-map" { + if foreignChild || charMap.URI() != lexicon.NamespaceSerialization || charMap.LocalName() != serializeCharacterMapElement { return nil, &xdm.Error{Code: errCodeSEPM0017, Message: "use-character-maps children must be output:character-map"} } @@ -2154,16 +2323,16 @@ func readSerializeCharacterMap(elem *xdm.Element) (character, replacement string } switch attr.LocalName() { - case "character": + case serializeCharacterMapCharacterAttribute: character, present = attr.Value(), true - case "map-string": + case serializeCharacterMapStringAttribute: replacement = attr.Value() default: return "", "", false, &xdm.Error{Code: errCodeSEPM0017, Message: "character-map has unsupported attribute"} } } - if present && utf8.RuneCountInString(character) != 1 { + if present && utf8.RuneCountInString(character) != serializeSingleCharacterCount { return "", "", false, &xdm.Error{Code: errCodeSEPM0017, Message: "character-map character must be a single character"} } @@ -2192,6 +2361,13 @@ func hasNonWhitespaceContent(elem *xdm.Element) bool { // strings and values strings (option (function) conventions apply — a QName key // is err:XPTY0004; W3C serialize-xml-139/139b). func resolveSerializeCharacterMaps(ctx context.Context, v xdm.Sequence) (map[rune]string, error) { + // The option is one map; each replacement is one string. These are not + // the one-character key's Unicode length constraint. + const ( + serializeCharacterMapCardinality = 1 + serializeCharacterReplacementCardinality = 1 + ) + // A use-character-maps entry present with the empty sequence selects the // default (no character map); present-empty = use default, not an error. An // empty (non-nil) map disables the feature just like a nil one. @@ -2199,7 +2375,7 @@ func resolveSerializeCharacterMaps(ctx context.Context, v xdm.Sequence) (map[run return map[rune]string{}, nil } - if xdm.SequenceLen(v) != 1 { + if xdm.SequenceLen(v) != serializeCharacterMapCardinality { return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "serialize option 'use-character-maps' must be a singleton map"} } @@ -2220,11 +2396,11 @@ func resolveSerializeCharacterMaps(ctx context.Context, v xdm.Sequence) (map[run } keyString, err := atomicToString(key) - if err != nil || utf8.RuneCountInString(keyString) != 1 { + if err != nil || utf8.RuneCountInString(keyString) != serializeSingleCharacterCount { return &xdm.Error{Code: lexicon.ErrSEPM0016, Message: "serialize use-character-maps keys must be single characters"} } - if xdm.SequenceLen(value) != 1 { + if xdm.SequenceLen(value) != serializeCharacterReplacementCardinality { return &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "serialize use-character-maps values must be singleton strings"} } @@ -2275,7 +2451,7 @@ func serializeAdaptiveSequence(seq xdm.Sequence, opts serializeOptions) (string, } func serializeJSONSequence(seq xdm.Sequence, opts serializeOptions) (string, error) { - if xdm.SequenceLen(seq) > 1 { + if xdm.SequenceLen(seq) > serializedJSONValueCardinality { return "", &xdm.Error{Code: errCodeSERE0023, Message: "json serialization requires at most one top-level item"} } @@ -2339,12 +2515,12 @@ func serializeJSONMap(v xdm.MapItem, opts serializeOptions) (string, error) { seen[keyText] = struct{}{} - if xdm.SequenceLen(value) > 1 { + if xdm.SequenceLen(value) > serializedJSONValueCardinality { return &xdm.Error{Code: errCodeSERE0023, Message: "json serialization map values must be singleton or empty"} } valText := jsonKindNull - if xdm.SequenceLen(value) == 1 { + if xdm.SequenceLen(value) == serializedJSONValueCardinality { valText, err = serializeJSONItem(value.Get(0), opts) if err != nil { return err @@ -2397,7 +2573,7 @@ func serializeJSONAtomic(v xdm.AtomicValue, opts serializeOptions) (string, erro return "", err } - if s == lexicon.FloatNaN || s == "INF" || s == "-INF" { + if s == lexicon.FloatNaN || s == lexicon.FloatINF || s == lexicon.FloatNegINF { return "", &xdm.Error{Code: errCodeSERE0020, Message: "cannot serialize NaN or infinity as JSON"} } @@ -2432,10 +2608,12 @@ func serializeJSONAtomic(v xdm.AtomicValue, opts serializeOptions) (string, erro // or a function item is a serialization error [err:SENR0001]. Character maps apply // to the resulting text. func serializeTextSequence(seq xdm.Sequence, opts serializeOptions) (string, error) { + const serializeCoalesceText = false // text is assembled before character expansion + var buf serializationBuffer buf.ctx = opts.serializationContext() - for item, err := range SerializationItems(buf.ctx, seq, opts.serializationSeparator(), false) { + for item, err := range SerializationItems(buf.ctx, seq, opts.serializationSeparator(), serializeCoalesceText) { if err != nil { return "", err } @@ -2531,6 +2709,8 @@ func writeXMLSequence(seq xdm.Sequence, opts serializeOptions, dst io.Writer) er // indent, standalone, undeclare-prefixes, cdata-section-elements, // suppress-indentation, and character maps. func newSerializeXMLWriter(opts serializeOptions) xdm.XMLSerialization { + const serializeXMLControlEscaping = true // retain XML control escaping rules + writer := xdm.XMLSerialization{ // The effective output version (version param, default "1.0") drives the XML // declaration text AND the XML 1.1 escaping/undeclaration behavior, so they @@ -2546,7 +2726,7 @@ func newSerializeXMLWriter(opts serializeOptions) xdm.XMLSerialization { CompactDeclaration: !opts.indent, CanEncode: serializeRepertoire(opts.encoding), - EscapeControlChars: true, + EscapeControlChars: serializeXMLControlEscaping, } if opts.omitXMLDeclaration { @@ -2559,9 +2739,9 @@ func newSerializeXMLWriter(opts serializeOptions) xdm.XMLSerialization { switch opts.standalone { case lexicon.ValueYes: - writer.Standalone = "yes" + writer.Standalone = lexicon.ValueYes case lexicon.ValueNo: - writer.Standalone = "no" + writer.Standalone = lexicon.ValueNo default: // "omit" (the default) or an empty-sequence value: force omission of the // standalone pseudo-attribute, overriding the source declaration. @@ -2610,16 +2790,16 @@ func serializeHTMLSequence(seq xdm.Sequence, opts serializeOptions) (string, err hw := serializeHTMLWriter(opts) if opts.includeContentType { - encoding, media := opts.encoding, opts.mediaType - if encoding == "" { - encoding = lexicon.EncodingUTF8U + enc, media := opts.encoding, opts.mediaType + if enc == "" { + enc = lexicon.EncodingUTF8U } if media == "" { - media = "text/html" + media = serializeHTMLMediaType } - hw = hw.ContentType(media + "; charset=" + encoding) + hw = hw.ContentType(media + serializeCharsetParameterPrefix + enc) } var buf serializationBuffer @@ -2678,6 +2858,8 @@ func (p *htmlSerializationProlog) explicit() bool { } func (p *htmlSerializationProlog) before(item xdm.XMLItem) error { + const serializeHTML5NamespaceMode = true // automatic prolog uses HTML5 namespace rules + if p.seenElement { return nil } @@ -2692,7 +2874,7 @@ func (p *htmlSerializationProlog) before(item xdm.XMLItem) error { } p.seenElement = true - if p.explicit() || p.automatic && strings.EqualFold(element.LocalName(), "html") && html.IsHTMLNamespace(element.URI(), true) { + if p.explicit() || p.automatic && strings.EqualFold(element.LocalName(), serializeHTMLRootElement) && html.IsHTMLNamespace(element.URI(), serializeHTML5NamespaceMode) { _, err := p.buf.WriteString(htmlDoctype(p.opts)) p.emitted = err == nil @@ -2702,18 +2884,15 @@ func (p *htmlSerializationProlog) before(item xdm.XMLItem) error { return nil } -// ValidateHTMLItem checks the actual normalized item before HTML serialization. -// It is shared with XSLT's streaming writer, which need not have a result DOM. -func ValidateHTMLItem(ctx context.Context, item xdm.XMLItem, html5 bool) error { - if item.Node == nil { - return checkHTMLCharacters(ctx, item.Text, html5) - } - - return validateHTMLNode(ctx, item.Node, html5, 0) -} - func serializeHTMLWriter(opts serializeOptions) html.Writer { - return html.NewWriter().DefaultDTD(false).Format(false).PreserveCase(true). + // The caller owns DOCTYPE/format framing; preserve source element case. + const ( + serializeHTMLWriterDefaultDoctype = false + serializeHTMLWriterFormat = false + serializeHTMLWriterPreserveCase = true + ) + + return html.NewWriter().DefaultDTD(serializeHTMLWriterDefaultDoctype).Format(serializeHTMLWriterFormat).PreserveCase(serializeHTMLWriterPreserveCase). SerializationVersion(opts.edition). EscapeURIAttributes(opts.escapeURIAttributes).CharacterMap(opts.charMap). Repertoire(serializeRepertoire(opts.encoding)).CDATAElements(opts.cdataElements). @@ -2725,7 +2904,9 @@ func serializeHTMLWriter(opts serializeOptions) html.Writer { // section 5.1.3. Known encodings retain their character repertoire even though // fn:serialize returns a string rather than encoded octets. func serializeRepertoire(name string) func(rune) bool { - if name == "" || strings.EqualFold(name, "UTF-8") || strings.EqualFold(name, "UTF8") { + const serializeBareUTF8Alias = "UTF8" // preserve the uppercase alias comparison spelling + + if name == "" || strings.EqualFold(name, lexicon.EncodingUTF8U) || strings.EqualFold(name, serializeBareUTF8Alias) { return nil } @@ -2742,12 +2923,18 @@ func serializeRepertoire(name string) func(rune) bool { } func htmlDoctype(opts serializeOptions) string { + // The HTML5 declaration and private legacy fallback retain established bytes; public HTML4 has no automatic declaration. + const ( + serializeHTML5DoctypeDeclaration = "\n" + serializeLegacyHTML4DoctypeDeclaration = "\n" + ) + if opts.doctypePublic != "" || opts.doctypeSystem != "" { // The html output method's DOCTYPE name is the fixed token "html" (per the // Serialization 3.1 html output method / HTML5 doctype rule — the name is // always html/HTML), NOT the document element's own (possibly mixed-case) // name. So a source root still emits ``. - rootName := lexicon.MethodHTML + rootName := serializeHTMLRootElement var b strings.Builder b.WriteString("\n" + return serializeHTML5DoctypeDeclaration } - return `` + "\n" + return serializeLegacyHTML4DoctypeDeclaration } // writeDoctypeSystemLiteral encloses a validated system identifier in double quotes, // or apostrophes when the value contains a double quote. Parameter validation must // reject values containing both quote characters. func writeDoctypeSystemLiteral(b *strings.Builder, sys string) { - quote := xmloutput.Quote(sys, '"') + const preferredSystemIDQuote = '"' // apostrophes remain the existing fallback + + quote := xmloutput.Quote(sys, preferredSystemIDQuote) b.WriteByte(quote) b.WriteString(sys) b.WriteByte(quote) @@ -2797,9 +2986,9 @@ func writeDoctypeSystemLiteral(b *strings.Builder, sys string) { // 5.0. Parameter validation rejects unsupported values. func serializeHTMLIsVersion5(opts serializeOptions) bool { version := html.RequestedVersion(opts.htmlVersion, opts.xmlVersion) - value, err := strconv.ParseFloat(version, 64) + value, err := strconv.ParseFloat(version, doubleFloatBits) - return err == nil && value >= 5 + return err == nil && value >= serializeHTML5Boundary } // The document is already owned by NormalizeSerialization. Prefix/metadata @@ -2817,10 +3006,10 @@ func serializeXHTMLDocument(doc *xdm.Document, opts serializeOptions) (string, e } if media == "" { - media = "text/html" + media = serializeHTMLMediaType } - contentType = media + "; charset=" + name + contentType = media + serializeCharsetParameterPrefix + name } if err := html.PrepareXHTML(ctx, doc, html5, contentType); err != nil { @@ -2848,17 +3037,19 @@ func serializeXHTMLDocument(doc *xdm.Document, opts serializeOptions) (string, e } func isSerializeXHTML5(opts serializeOptions) bool { - value, err := strconv.ParseFloat(opts.htmlVersion, 64) - return err == nil && value >= 5 + value, err := strconv.ParseFloat(opts.htmlVersion, doubleFloatBits) + return err == nil && value >= serializeHTML5Boundary } func xhtmlAutomaticDoctype(doc *xdm.Document) bool { + const serializeXHTML5ElementMode = true // automatic XHTML5 root classification + for child := range xdm.Children(doc) { if element, ok := child.(*xdm.Element); ok { - return html.XHTMLIsElement(element.URI(), element.LocalName(), true) && strings.EqualFold(element.LocalName(), "html") + return html.XHTMLIsElement(element.URI(), element.LocalName(), serializeXHTML5ElementMode) && strings.EqualFold(element.LocalName(), serializeHTMLRootElement) } - if child.Type() == xdm.TextNode && strings.Trim(xdm.CharacterData(child), " \t\r\n") != "" { + if child.Type() == xdm.TextNode && strings.Trim(xdm.CharacterData(child), lexicon.XMLWhitespace) != "" { return false } } @@ -3036,19 +3227,26 @@ func encodeJSONStringForSerialization(s string, opts serializeOptions) (string, } func utf16SurrogatePair(r rune) (uint16, uint16) { - cp := uint32(r - 0x10000) // #nosec G115 -- The caller passes a supplementary Unicode scalar in 0x10000..0x10FFFF. - hi := uint16(0xD800 + (cp >> 10)) // #nosec G115 -- The supplementary scalar yields a high surrogate in 0xD800..0xDBFF. - lo := uint16(0xDC00 + (cp & 0x3FF)) + const utf16LowSurrogatePayloadMask = 0x3FF // ten low payload bits + + cp := uint32(r - jsonSupplementaryPlaneFirst) // #nosec G115 -- The caller passes a supplementary Unicode scalar in jsonSupplementaryPlaneFirst..0x10FFFF. + hi := uint16(jsonHighSurrogateFirst + (cp >> jsonSurrogatePayloadBits)) // #nosec G115 -- The supplementary scalar yields a high surrogate in jsonHighSurrogateFirst..0xDBFF. + lo := uint16(jsonLowSurrogateFirst + (cp & utf16LowSurrogatePayloadMask)) return hi, lo } func validateHTMLNode(ctx context.Context, node xdm.Node, html5 bool, depth int) error { - if depth > DepthLimit(ctx, 512) { + const ( + serializeHTMLNodeVisitWork = 1 // one inspected markup node + errCodeSERE0015 = "SERE0015" + ) + + if depth > DepthLimit(ctx, defaultHTMLValidationDepth) { return ErrBudget } - if err := Charge(ctx, 1); err != nil { + if err := Charge(ctx, serializeHTMLNodeVisitWork); err != nil { return err } @@ -3058,12 +3256,12 @@ func validateHTMLNode(ctx context.Context, node xdm.Node, html5 bool, depth int) if node.Type() == xdm.ProcessingInstructionNode && !htmlXMLIslandNode(node, html5) { content := xdm.CharacterData(node) - if err := Charge(ctx, 1+len(content)/64); err != nil { + if err := Charge(ctx, serializedRunBaseWork+len(content)/serializedWorkChunkBytes); err != nil { return err } if strings.Contains(content, ">") { - return &xdm.Error{Code: "SERE0015", Message: "HTML processing instruction contains '>'"} + return &xdm.Error{Code: errCodeSERE0015, Message: "HTML processing instruction contains '>'"} } } @@ -3106,17 +3304,31 @@ func htmlXMLIslandNode(node xdm.Node, html5 bool) bool { } func checkHTMLCharacters(ctx context.Context, value string, html5 bool) error { + // Inclusive pre-HTML5 prohibited control-code-point ranges; these are HTML + // rejection rules, distinct from JSON escape-output decisions. + const ( + htmlC1ControlFirst = 0x7F + htmlC1ControlLast = 0x9F + htmlC0LeadingFirst = 1 + htmlC0LeadingLast = 8 + htmlVerticalTab = 0x0B + htmlFormFeed = 0x0C + htmlC0TrailingFirst = 0x0E + htmlC0TrailingLast = 0x1F + errCodeSERE0014 = "SERE0014" + ) + if html5 { return ctx.Err() } - if err := Charge(ctx, 1+len(value)/64); err != nil { + if err := Charge(ctx, serializedRunBaseWork+len(value)/serializedWorkChunkBytes); err != nil { return err } for _, r := range value { - if r >= 0x7f && r <= 0x9f || r >= 1 && r <= 8 || r == 0xb || r == 0xc || r >= 0xe && r <= 0x1f { - return &xdm.Error{Code: "SERE0014", Message: fmt.Sprintf("character U+%04X is not permitted in HTML before version 5", r)} + if r >= htmlC1ControlFirst && r <= htmlC1ControlLast || r >= htmlC0LeadingFirst && r <= htmlC0LeadingLast || r == htmlVerticalTab || r == htmlFormFeed || r >= htmlC0TrailingFirst && r <= htmlC0TrailingLast { + return &xdm.Error{Code: errCodeSERE0014, Message: fmt.Sprintf("character U+%04X is not permitted in HTML before version 5", r)} } } @@ -3131,12 +3343,14 @@ type htmlSequenceValidator struct { } func (v *htmlSequenceValidator) sequence(seq xdm.Sequence, depth int) error { - if depth > DepthLimit(v.ctx, 512) { + const serializeHTMLSequenceVisitWork = 1 // one flattened sequence-item visit + + if depth > DepthLimit(v.ctx, defaultHTMLValidationDepth) { return ErrBudget } for item := range xdm.SequenceItems(seq) { - if err := Charge(v.ctx, 1); err != nil { + if err := Charge(v.ctx, serializeHTMLSequenceVisitWork); err != nil { return err } diff --git a/transform/internal/xpath/functions_string.go b/transform/internal/xpath/functions_string.go index 2df632a..1fc9b27 100644 --- a/transform/internal/xpath/functions_string.go +++ b/transform/internal/xpath/functions_string.go @@ -14,6 +14,7 @@ import ( "unicode/utf8" "github.com/skarm/xmlsec/internal/lexicon" + "github.com/skarm/xmlsec/internal/xmlchar" "github.com/skarm/xmlsec/transform/internal/pattern" "github.com/skarm/xmlsec/transform/internal/xdm" xpathregex "github.com/skarm/xmlsec/transform/internal/xpath/regex" @@ -36,65 +37,89 @@ const ( fnNameMatches = "matches" ) +// Regex acceptance bounds source bytes independently of cache retention. +const xpathRegexSourceBytes = 64 << 10 + +// Free-spacing and literal flags are shared by preprocessing and compilation. +// Negative match limits enumerate all matches, independently of function arity. +const ( + regexFreeSpacingRune = 'x' + regexLiteralRune = 'q' + regexUnlimitedMatches = -1 +) + func lookupFunctionsString(name string) (*builtinFunc, bool) { + // Standard string-function local names; concat alone keeps an unbounded maximum arity. + const ( + fnNameCodepointsToString = "codepoints-to-string" + fnNameStringToCodepoints = "string-to-codepoints" + fnNameCompare = "compare" + fnNameCodepointEqual = "codepoint-equal" + fnNameUpperCase = "upper-case" + fnNameLowerCase = "lower-case" + fnNameEndsWith = "ends-with" + fnNameReplace = "replace" + variadicConcatArity = -1 + ) + switch name { - case lexicon.TypeString: - return &builtinFunc{name: name, min: 0, max: 1, fn: fnString}, true - case "codepoints-to-string": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnCodepointsToString}, true - case "string-to-codepoints": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnStringToCodepoints}, true - case "compare": - return &builtinFunc{name: name, min: 2, max: 3, fn: fnCompare}, true - case "codepoint-equal": - return &builtinFunc{name: name, min: 2, max: 2, fn: fnCodepointEqual}, true + case fnNameString: + return &builtinFunc{name: name, min: builtinArityZero, max: builtinArityOne, fn: fnString}, true + case fnNameCodepointsToString: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnCodepointsToString}, true + case fnNameStringToCodepoints: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnStringToCodepoints}, true + case fnNameCompare: + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityThree, fn: fnCompare}, true + case fnNameCodepointEqual: + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityTwo, fn: fnCodepointEqual}, true case nameConcat: - return &builtinFunc{name: name, min: 2, max: -1, fn: fnConcat}, true + return &builtinFunc{name: name, min: builtinArityTwo, max: variadicConcatArity, fn: fnConcat}, true case fnNameStringJoin: - return &builtinFunc{name: name, min: 1, max: 2, fn: fnStringJoin}, true + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityTwo, fn: fnStringJoin}, true case fnNameSubstring: - return &builtinFunc{name: name, min: 2, max: 3, fn: fnSubstring}, true + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityThree, fn: fnSubstring}, true case nameStringLength: - return &builtinFunc{name: name, min: 0, max: 1, fn: fnStringLength}, true + return &builtinFunc{name: name, min: builtinArityZero, max: builtinArityOne, fn: fnStringLength}, true case fnNameNormalizeSpace: - return &builtinFunc{name: name, min: 0, max: 1, fn: fnNormalizeSpace}, true + return &builtinFunc{name: name, min: builtinArityZero, max: builtinArityOne, fn: fnNormalizeSpace}, true case fnNameNormalizeUnicode: - return &builtinFunc{name: name, min: 1, max: 2, fn: fnNormalizeUnicode}, true - case "upper-case": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnUpperCase}, true - case "lower-case": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnLowerCase}, true + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityTwo, fn: fnNormalizeUnicode}, true + case fnNameUpperCase: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnUpperCase}, true + case fnNameLowerCase: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnLowerCase}, true case fnNameTranslate: - return &builtinFunc{name: name, min: 3, max: 3, fn: fnTranslate}, true + return &builtinFunc{name: name, min: builtinArityThree, max: builtinArityThree, fn: fnTranslate}, true case fnNameContains: - return &builtinFunc{name: name, min: 2, max: 3, fn: fnContains}, true + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityThree, fn: fnContains}, true case fnNameStartsWith: - return &builtinFunc{name: name, min: 2, max: 3, fn: fnStartsWith}, true - case "ends-with": - return &builtinFunc{name: name, min: 2, max: 3, fn: fnEndsWith}, true + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityThree, fn: fnStartsWith}, true + case fnNameEndsWith: + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityThree, fn: fnEndsWith}, true case fnNameSubstringBefore: - return &builtinFunc{name: name, min: 2, max: 3, fn: fnSubstringBefore}, true + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityThree, fn: fnSubstringBefore}, true case fnNameSubstringAfter: - return &builtinFunc{name: name, min: 2, max: 3, fn: fnSubstringAfter}, true + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityThree, fn: fnSubstringAfter}, true case fnNameMatches: - return &builtinFunc{name: name, min: 2, max: 3, fn: fnMatches}, true - case "replace": - return &builtinFunc{name: name, min: 3, max: 4, fn: fnReplace}, true - case "tokenize": - return &builtinFunc{name: name, min: 1, max: 3, fn: fnTokenize}, true - case "analyze-string": - return &builtinFunc{name: name, min: 2, max: 3, fn: fnAnalyzeString}, true - case "contains-token": - return &builtinFunc{name: name, min: 2, max: 3, fn: fnContainsToken}, true - case "collation-key": - return &builtinFunc{name: name, min: 1, max: 2, fn: fnCollationKey}, true + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityThree, fn: fnMatches}, true + case fnNameReplace: + return &builtinFunc{name: name, min: builtinArityThree, max: builtinArityFour, fn: fnReplace}, true + case fnNameTokenize: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityThree, fn: fnTokenize}, true + case fnNameAnalyzeString: + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityThree, fn: fnAnalyzeString}, true + case fnNameContainsToken: + return &builtinFunc{name: name, min: builtinArityTwo, max: builtinArityThree, fn: fnContainsToken}, true + case fnNameCollationKey: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityTwo, fn: fnCollationKey}, true } return nil, false } func fnString(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { - if len(args) == 0 { + if len(args) == builtinArityZero { s, err := contextItemString(ctx) if err != nil { return nil, err @@ -107,7 +132,7 @@ func fnString(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { return xdm.SingleString(""), nil } - if xdm.SequenceLen(args[0]) > 1 { + if xdm.SequenceLen(args[0]) > singletonFunctionArgument { return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "fn:string requires a single item, got sequence of length > 1"} } @@ -154,6 +179,8 @@ func contextItemString(ctx context.Context) (string, error) { } func fnCodepointsToString(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { + const singleCodepointInput = 1 // singleton input fast path, not an output limit + seq := args[0] if err := fnCountOps(ctx, getFnContext(ctx), xdm.SequenceLen(seq)); err != nil { return nil, err @@ -171,7 +198,7 @@ func fnCodepointsToString(ctx context.Context, args []xdm.Sequence) (xdm.Sequenc // Fast path: singleton integer (common in unicode-90 where each codepoint // is mapped individually via codepoints-to-string(.)) - if xdm.SequenceLen(seq) == 1 { + if xdm.SequenceLen(seq) == singleCodepointInput { cp, err := itemToCodepoint(seq.Get(0)) if err != nil { return nil, err @@ -249,7 +276,7 @@ func isValidXMLCodepoint(cp int) bool { return true } - if cp >= 0x10000 && cp <= 0x10FFFF { + if cp >= 0x10000 && cp <= utf8.MaxRune { return true } @@ -268,7 +295,7 @@ func isValidXML11Codepoint(cp int) bool { return true } - if cp >= 0x10000 && cp <= 0x10FFFF { + if cp >= 0x10000 && cp <= utf8.MaxRune { return true } @@ -379,7 +406,7 @@ func fnConcat(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { func fnStringJoin(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { sep := "" - if len(args) > 1 { + if len(args) > builtinArityOne { if getFnContext(ctx).xpath10CompatMode() { // The separator's expected type is xs:string, so XPath 1.0 compatibility // mode converts it with fn:string applied to its first item. @@ -431,6 +458,11 @@ func fnStringJoin(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error } func fnSubstring(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { + const ( + substringFirstPosition = 1.0 // XPath character positions count from one + substringContextCheckBytes = 4096 // cancellation polling stride, not an input cap + ) + s, err := coerceArgToString(ctx, args[0]) if err != nil { return nil, err @@ -442,24 +474,24 @@ func fnSubstring(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) } // XPath round - rStart := roundHalfUpFloat(startPos, 0) + rStart := roundHalfUpFloat(startPos, positionRoundPrecision) end := math.Inf(1) - if len(args) == 3 { + if len(args) == builtinArityThree { length, err := coerceArgToDoubleRequired(ctx, args[2]) if err != nil { return nil, err } - end = rStart + roundHalfUpFloat(length, 0) + end = rStart + roundHalfUpFloat(length, positionRoundPrecision) } - if math.IsNaN(rStart) || math.IsNaN(end) || end <= 1 || end <= rStart || math.IsInf(rStart, 1) { + if math.IsNaN(rStart) || math.IsNaN(end) || end <= substringFirstPosition || end <= rStart || math.IsInf(rStart, 1) { return xdm.SingleString(""), nil } startByte, endByte := len(s), len(s) - position := 1.0 + position := substringFirstPosition checked := 0 for offset := range s { @@ -468,7 +500,7 @@ func fnSubstring(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) return nil, err } - checked = offset + 4096 + checked = offset + substringContextCheckBytes } if position >= end { @@ -498,7 +530,7 @@ func fnSubstring(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) func fnStringLength(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { var s string - if len(args) == 0 { + if len(args) == builtinArityZero { var err error s, err = contextItemString(ctx) @@ -520,7 +552,7 @@ func fnStringLength(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, err func fnNormalizeSpace(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { var s string - if len(args) == 0 { + if len(args) == builtinArityZero { var err error s, err = contextItemString(ctx) @@ -553,13 +585,13 @@ func fnNormalizeUnicode(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, formName := nameNFC // default - if len(args) > 1 { + if len(args) > builtinArityOne { form, err := coerceArgToStringRequired(ctx, args[1]) if err != nil { return nil, err } - formName = strings.Trim(strings.ToUpper(form), " \t\r\n") + formName = strings.Trim(strings.ToUpper(form), lexicon.XMLWhitespace) if formName == "" { // Empty form string means return input unchanged return xdm.SingleString(s), nil @@ -581,7 +613,7 @@ func fnNormalizeUnicode(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, nf = norm.NFKC case nameNFKD: nf = norm.NFKD - case "FULLY-NORMALIZED": + case lexicon.NormFullyNormalized: // W3C Charmod Normalization: NFC + if the result starts with a // composing character, prepend a space. A composing character is // one that can be consumed by NFC composition with a preceding @@ -819,7 +851,7 @@ func fnMatches(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { flags := "" - if len(args) > 2 { + if len(args) > builtinArityTwo { if xdm.SequenceLen(args[2]) == 0 { return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "fn:matches flags must not be empty sequence"} } @@ -905,7 +937,7 @@ func fnReplace(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { flags := "" - if len(args) > 3 { + if len(args) > builtinArityThree { if xdm.SequenceLen(args[3]) == 0 { return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "fn:replace flags must not be empty sequence"} } @@ -916,7 +948,7 @@ func fnReplace(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { } } - isLiteral := strings.Contains(flags, "q") + isLiteral := strings.Contains(flags, regexLiteralFlag) if err := checkVersionRegex(ctx, required, flags); err != nil { return nil, err @@ -1041,6 +1073,8 @@ func appendXPathReplacementGroup(builder *strings.Builder, repl string, i, numGr } func fnTokenize(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { + const allSplitParts = -1 // the regex split keeps every resulting part + if xdm.SequenceLen(args[0]) == 0 { return nil, nil // input is xs:string? — empty yields empty } @@ -1055,8 +1089,11 @@ func fnTokenize(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) } // 1-arg form: normalize XML whitespace (#x20, #x9, #xA, #xD), then split - if len(args) == 1 { - tokens := splitXMLWhitespace(s) + if len(args) == builtinArityOne { + tokens, err := splitXMLWhitespaceBounded(ctx, s) + if err != nil { + return nil, err + } result := make(xdm.ItemSlice, len(tokens)) for i, t := range tokens { @@ -1077,7 +1114,7 @@ func fnTokenize(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) flags := "" - if len(args) > 2 { + if len(args) > builtinArityTwo { if xdm.SequenceLen(args[2]) == 0 { return nil, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "fn:tokenize flags must not be empty sequence"} } @@ -1107,7 +1144,7 @@ func fnTokenize(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) return nil, &xdm.Error{Code: errCodeFORX0003, Message: "tokenize pattern matches zero-length string"} } - parts, err := re.Split(ctx, s, -1) + parts, err := re.Split(ctx, s, allSplitParts) if err != nil { return nil, regexExecutionError(ctx, err, "regex split failed") } @@ -1149,6 +1186,36 @@ func splitXMLWhitespace(s string) []string { return tokens } +// Bound token production before append even when a caller consumes only count(). +func splitXMLWhitespaceBounded(ctx context.Context, s string) ([]string, error) { + // Charge a base visit and one logical scan operation per 64 input bytes. + const ( + whitespaceTokenScanBaseWork = 1 + whitespaceTokenScanChunkBytes = 64 + ) + + ec := getFnContext(ctx) + if err := fnCountOps(ctx, ec, whitespaceTokenScanBaseWork+len(s)/whitespaceTokenScanChunkBytes); err != nil { + return nil, err + } + + var tokens []string + + for token := range strings.FieldsFuncSeq(s, xmlchar.IsSpace) { + if len(tokens) >= fnMaxNodes(ec) { + return nil, ErrNodeSetLimit + } + + if err := fnCountOp(ctx, ec); err != nil { + return nil, err + } + + tokens = append(tokens, strings.Clone(token)) + } + + return tokens, nil +} + func fnAnalyzeString(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { s, err := coerceArgToString(ctx, args[0]) if err != nil { @@ -1161,7 +1228,7 @@ func fnAnalyzeString(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, er } flags := "" - if len(args) > 2 { + if len(args) > builtinArityTwo { flags, err = coerceArgToString(ctx, args[2]) if err != nil { return nil, err @@ -1190,14 +1257,21 @@ func fnAnalyzeString(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, er } func buildAnalyzeStringResult(ctx context.Context, re *compiledXPathRegex, s, pattern, flags string) (xdm.Sequence, error) { + // F&O analyze-string result element names in the standard fn: namespace. + const ( + analyzeStringResultElement = "analyze-string-result" + analyzeStringNonMatchElement = "non-match" + analyzeStringMatchElement = "match" + ) + doc := xdm.NewDefaultDocument() - root, err := createAnalyzeStringElement(doc, "analyze-string-result") + root, err := createAnalyzeStringElement(doc, analyzeStringResultElement) if err != nil { return nil, &xdm.Error{Code: errCodeFOER0000, Message: fmt.Sprintf("analyze-string: failed to build result: %v", err)} } - if err := root.DeclareNamespace("fn", NSFn); err != nil { + if err := root.DeclareNamespace(prefixFn, NSFn); err != nil { return nil, &xdm.Error{Code: errCodeFOER0000, Message: fmt.Sprintf("analyze-string: failed to build result: %v", err)} } @@ -1222,7 +1296,7 @@ func buildAnalyzeStringResult(ctx context.Context, re *compiledXPathRegex, s, pa // callback observes the one match that exhausts the budget and rejects // without producing the rest; an unbounded budget streams uncapped. ec := getFnContext(ctx) - findLimit := -1 + findLimit := regexUnlimitedMatches if remaining, bounded := fnRemainingOps(ec); bounded { if n := remaining + 1; n > 0 { @@ -1250,12 +1324,12 @@ func buildAnalyzeStringResult(ctx context.Context, re *compiledXPathRegex, s, pa start, end := m[0], m[1] if start > pos { - if err := appendAnalyzeStringTextElement(doc, root, "non-match", s[pos:start]); err != nil { + if err := appendAnalyzeStringTextElement(doc, root, analyzeStringNonMatchElement, s[pos:start]); err != nil { return buildFail(err) } } - matchElem, err := createAnalyzeStringElement(doc, "match") + matchElem, err := createAnalyzeStringElement(doc, analyzeStringMatchElement) if err != nil { return buildFail(err) } @@ -1282,7 +1356,7 @@ func buildAnalyzeStringResult(ctx context.Context, re *compiledXPathRegex, s, pa } if pos < len(s) { - if err := appendAnalyzeStringTextElement(doc, root, "non-match", s[pos:]); err != nil { + if err := appendAnalyzeStringTextElement(doc, root, analyzeStringNonMatchElement, s[pos:]); err != nil { return nil, &xdm.Error{Code: errCodeFOER0000, Message: fmt.Sprintf("analyze-string: failed to build result: %v", err)} } } @@ -1319,11 +1393,11 @@ type analyzeStringGroup struct { // (e.g. "( a \ ) (b) )" with "x" compiles to "(a\)(b))" — group 2 nested in // group 1 — but the raw form looks like two siblings). func analyzeStringGroupParents(pattern, flags string) []int { - if strings.ContainsRune(flags, 'q') { + if strings.ContainsRune(flags, regexLiteralRune) { return nil // literal pattern: no capturing groups } - if strings.ContainsRune(flags, 'x') { + if strings.ContainsRune(flags, regexFreeSpacingRune) { pattern = stripFreeSpacing(pattern) } @@ -1554,6 +1628,12 @@ func analyzeStringParentOf(groupParents []int, g int) int { // renderAnalyzeStringGroup emits, into parent, the text spans of node not covered // by a child group interleaved with the child fn:group elements, in document order. func renderAnalyzeStringGroup(doc *xdm.Document, parent *xdm.Element, node *analyzeStringGroup, s string) error { + // Captures render as fn:group with an unqualified nr attribute. + const ( + analyzeStringGroupElement = "group" + analyzeStringGroupNumberAttribute = "nr" + ) + cursor := node.start for _, child := range node.children { if child.start > cursor { @@ -1562,12 +1642,12 @@ func renderAnalyzeStringGroup(doc *xdm.Document, parent *xdm.Element, node *anal } } - groupElem, err := createAnalyzeStringElement(doc, "group") + groupElem, err := createAnalyzeStringElement(doc, analyzeStringGroupElement) if err != nil { return err } - if err := groupElem.SetAttribute("nr", fmt.Sprintf("%d", child.nr)); err != nil { + if err := groupElem.SetAttribute(analyzeStringGroupNumberAttribute, fmt.Sprintf("%d", child.nr)); err != nil { return err } @@ -1597,7 +1677,7 @@ func createAnalyzeStringElement(doc *xdm.Document, localName string) (*xdm.Eleme return nil, err } - if err := elem.SetActiveNamespace("fn", NSFn); err != nil { + if err := elem.SetActiveNamespace(prefixFn, NSFn); err != nil { return nil, err } @@ -1641,7 +1721,7 @@ func (r *compiledXPathRegex) ReplaceAllString(ctx context.Context, s, replacemen var failure error - err := r.program.Each(ctx, s, -1, patternWork(ctx), func(m []int) bool { + err := r.program.Each(ctx, s, regexUnlimitedMatches, patternWork(ctx), func(m []int) bool { if failure = ChargeString(ctx, m[0]-last); failure != nil { return false } @@ -1675,19 +1755,26 @@ func (r *compiledXPathRegex) ReplaceAllString(ctx context.Context, s, replacemen } func (r *compiledXPathRegex) Split(ctx context.Context, s string, n int) ([]string, error) { + const splitPartVisitWork = 1 // one logical operation before appending a split part + var out []string last := 0 var failure error - limit := -1 + limit := regexUnlimitedMatches if n > 0 { limit = n - 1 } err := r.program.Each(ctx, s, limit, patternWork(ctx), func(m []int) bool { - if failure = Charge(ctx, 1); failure != nil { + if len(out) >= NodeLimit(ctx, fnMaxNodes(getFnContext(ctx))) { + failure = ErrNodeSetLimit + return false + } + + if failure = Charge(ctx, splitPartVisitWork); failure != nil { return false } @@ -1704,6 +1791,10 @@ func (r *compiledXPathRegex) Split(ctx context.Context, s string, n int) ([]stri return nil, failure } + if len(out) >= NodeLimit(ctx, fnMaxNodes(getFnContext(ctx))) { + return nil, ErrNodeSetLimit + } + return append(out, stringPart(s, last, len(s))), nil } @@ -1717,7 +1808,7 @@ func (r *compiledXPathRegex) FindAllStringSubmatchIndex(ctx context.Context, s s func (r *compiledXPathRegex) eachStringSubmatchIndex(ctx context.Context, s string, limit int, fn func([]int) bool) error { if limit <= 0 { - limit = -1 + limit = regexUnlimitedMatches } return r.program.Each(ctx, s, limit, patternWork(ctx), fn) @@ -1739,17 +1830,21 @@ func patternWork(ctx context.Context) pattern.Work { // compileXPathRegex reuses immutable programs within the owning runtime. func compileXPathRegex(ctx context.Context, source, flags string) (*compiledXPathRegex, error) { + // Retained source+flag key bytes have an independent cap; larger valid + // keys remain usable uncached. This is not the regex source acceptance cap. + const xpathRegexCacheKeyBytes = 64 << 10 + if err := ctx.Err(); err != nil { return nil, err } - if len(source) > 64<<10 { + if len(source) > xpathRegexSourceBytes { return nil, pattern.ErrLimit } - if XMLSchemaVersion(ctx) == 10 && !strings.ContainsRune(flags, 'q') { + if XMLSchemaVersion(ctx) == schemaEdition10 && !strings.ContainsRune(flags, regexLiteralRune) { checked := source - if strings.ContainsRune(flags, 'x') { + if strings.ContainsRune(flags, regexFreeSpacingRune) { checked = stripFreeSpacing(checked) } @@ -1776,7 +1871,7 @@ func compileXPathRegex(ctx context.Context, source, flags string) (*compiledXPat // Dynamic keys may be slices of much larger caller-owned strings. Bound // retained key bytes and compile from owned strings on a cache miss. - if len(source) > 64<<10 || len(flags) > (64<<10)-len(source) { + if len(source) > xpathRegexCacheKeyBytes || len(flags) > xpathRegexCacheKeyBytes-len(source) { return compileXPathRegexUncached(source, flags) } @@ -1797,7 +1892,14 @@ func compileXPathRegex(ctx context.Context, source, flags string) (*compiledXPat } func compileXPathRegexUncached(source, flags string) (*compiledXPathRegex, error) { - if len(source) > 64<<10 { + // F&O rune-valued options consumed only by this pattern compiler. + const ( + regexIgnoreCaseRune = 'i' + regexMultilineRune = 'm' + regexDotAllRune = 's' + ) + + if len(source) > xpathRegexSourceBytes { return nil, pattern.ErrLimit } @@ -1805,14 +1907,14 @@ func compileXPathRegexUncached(source, flags string) (*compiledXPathRegex, error for _, flag := range flags { switch flag { - case 'i': + case regexIgnoreCaseRune: options.IgnoreCase = true - case 'm': + case regexMultilineRune: options.Multiline = true - case 's': + case regexDotAllRune: options.DotAll = true - case 'x': - case 'q': + case regexFreeSpacingRune: + case regexLiteralRune: options.Literal = true default: return nil, &xdm.Error{Code: errCodeFORX0001, Message: fmt.Sprintf("invalid regex flag: %c", flag)} @@ -1820,7 +1922,7 @@ func compileXPathRegexUncached(source, flags string) (*compiledXPathRegex, error } if !options.Literal { - if strings.ContainsRune(flags, 'x') { + if strings.ContainsRune(flags, regexFreeSpacingRune) { source = stripFreeSpacing(source) } @@ -1915,7 +2017,7 @@ func fnContainsToken(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, er return nil, err } - token = strings.Trim(token, " \t\r\n") + token = strings.Trim(token, lexicon.XMLWhitespace) if token == "" { return xdm.SingleBoolean(false), nil } diff --git a/transform/internal/xpath/functions_test.go b/transform/internal/xpath/functions_test.go index dfcd8f7..0b416db 100644 --- a/transform/internal/xpath/functions_test.go +++ b/transform/internal/xpath/functions_test.go @@ -3,8 +3,11 @@ package xpath //nolint:testpackage // Checks built-in function results on the AS import ( "errors" "math" + "reflect" "strconv" + "strings" "testing" + "time" "github.com/skarm/xmlsec/transform/internal/xdm" ) @@ -246,3 +249,353 @@ func TestFunctionErrorsASTAndVM(t *testing.T) { } } } + +func TestISOWeekWithinMonthCalendarDates(t *testing.T) { + for _, tc := range []struct { + name, source, place string + want int + }{ + {"March3_NY", "2025-03-03T12:00:00-05:00", "America/New_York", 1}, + {"March10_NY", "2025-03-10T12:00:00-04:00", "America/New_York", 2}, + {"March17_NY", "2025-03-17T12:00:00-04:00", "America/New_York", 3}, + {"March3_UTC", "2025-03-03T12:00:00-05:00", "UTC", 1}, + {"March10_UTC", "2025-03-10T12:00:00-04:00", "UTC", 2}, + {"March17_UTC", "2025-03-17T12:00:00-04:00", "UTC", 3}, + {"March3_Fixed", "2025-03-03T12:00:00-05:00", "Etc/GMT+4", 1}, + {"March10_Fixed", "2025-03-10T12:00:00-04:00", "Etc/GMT+4", 2}, + {"March17_Fixed", "2025-03-17T12:00:00-04:00", "Etc/GMT+4", 3}, + {"FallOctober27", "2025-10-27T12:00:00-04:00", "America/New_York", 5}, + {"FallNovember3", "2025-11-03T12:00:00-05:00", "America/New_York", 1}, + {"FallNovember10", "2025-11-10T12:00:00-05:00", "America/New_York", 2}, + {"December30", "2024-12-30T12:00:00-05:00", "America/New_York", 1}, + {"January1", "2025-01-01T12:00:00-05:00", "America/New_York", 1}, + {"W3CJanuary29", "2013-01-29T12:00:00-05:00", "America/New_York", 5}, + {"W3CFebruary1", "2013-02-01T12:00:00-05:00", "America/New_York", 5}, + {"LeapFebruary29", "2024-02-29T12:00:00-05:00", "America/New_York", 5}, + {"AfterLeapMarch1", "2024-03-01T12:00:00-05:00", "America/New_York", 5}, + } { + t.Run(tc.name, func(t *testing.T) { + loc, err := time.LoadLocation(tc.place) + if err != nil { + t.Fatal(err) + } + instant, err := time.Parse(time.RFC3339, tc.source) + if err != nil { + t.Fatal(err) + } + instant = instant.In(loc) + // F&O3.1 numbers the Monday-to-Sunday week by its Thursday's ordinal + // within that Thursday's month. This oracle uses civil dates, not elapsed hours. + civil := time.Date(instant.Year(), instant.Month(), instant.Day(), 12, 0, 0, 0, time.UTC) + weekday := int(civil.Weekday()) + if weekday == 0 { + weekday = 7 + } + thursday := civil.AddDate(0, 0, 4-weekday) + if oracle := (thursday.Day()-1)/7 + 1; oracle != tc.want { + t.Fatalf("fixture/oracle = %d, want %d", oracle, tc.want) + } + if got := isoWeekOfMonth(instant); got != tc.want { + t.Fatalf("week = %d, want %d (Thursday %s)", got, tc.want, thursday.Format("2006-01-02")) + } + }) + } +} + +// TestHTMLDoctypeLegacyFallbackIdentifier covers the private legacy fallback. +// The public HTML4 prolog does not select this fallback without explicit metadata. +func TestHTMLDoctypeLegacyFallbackIdentifier(t *testing.T) { + t.Parallel() + for _, tc := range []struct { + name string + opts serializeOptions + want string + }{ + {"legacy HTML4 fallback", serializeOptions{htmlVersion: "4.01"}, "\n"}, + {"HTML5 control", serializeOptions{htmlVersion: "5"}, "\n"}, + {"explicit identifier control", serializeOptions{htmlVersion: "4.01", doctypeSystem: "urn:custom"}, "\n"}, + } { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + if got := htmlDoctype(tc.opts); got != tc.want { + t.Fatalf("got %q; want %q", got, tc.want) + } + }) + } +} + +func TestUCAMalformedOriginalByteIndex(t *testing.T) { + for _, tc := range []struct { + name, source, needle string + numeric bool + position, length int + }{ + {"MalformedPrefix", "\xffa", "a", false, 1, 1}, + {"MalformedMatch", "L\xffR", "\ufffd", false, 1, 1}, + {"TruncatedPrefix", "\xe2\x82a", "a", false, 2, 1}, + {"OverlongPrefix", "\xc0\xafa", "a", false, 2, 1}, + {"SurrogatePrefix", "\xed\xa0\x80a", "a", false, 3, 1}, + {"ValidReplacement", "\ufffda", "a", false, 3, 1}, + {"Supplementary", "😀a", "a", false, 4, 1}, + {"EmptyNeedle", "\xffa", "", false, 0, 0}, + {"Miss", "\xffa", "x", false, -1, 0}, + {"NumericSplit", "100", "10", true, -1, 0}, + {"NumericWhole", "a001b", "001", true, 1, 3}, + } { + t.Run(tc.name, func(t *testing.T) { + pos, length := ucaIndexOf(strings.Compare, tc.source, tc.needle, tc.numeric) + if pos != tc.position || length != tc.length { + t.Fatalf("got(%d,%d); want(%d,%d)", pos, length, tc.position, tc.length) + } + }) + } +} + +func TestUCAMalformedOriginalBytePrefixAndSuffix(t *testing.T) { + for _, tc := range []struct { + name, prefixSource, suffixSource, needle string + match bool + length int + }{ + {"Malformed", "\xffa", "a\xff", "\ufffd", true, 1}, + {"Truncated", "\xc3a", "a\xc3", "\ufffd", true, 1}, + {"ValidReplacement", "\ufffda", "a\ufffd", "\ufffd", true, 3}, + {"Supplementary", "😀a", "a😀", "😀", true, 4}, + {"EmptyNeedle", "\xffa", "a\xff", "", true, 0}, + {"Miss", "\xffa", "a\xff", "x", false, 0}, + } { + t.Run(tc.name, func(t *testing.T) { + ok, length := ucaHasPrefix(strings.Compare, tc.prefixSource, tc.needle, false) + if ok != tc.match || length != tc.length { + t.Fatalf("prefix got(%t,%d); want(%t,%d)", ok, length, tc.match, tc.length) + } + ok, length = ucaHasSuffix(strings.Compare, tc.suffixSource, tc.needle, false) + if ok != tc.match || length != tc.length { + t.Fatalf("suffix got(%t,%d); want(%t,%d)", ok, length, tc.match, tc.length) + } + }) + } +} + +func TestUCACandidateComparatorOrderAndMalformedFallback(t *testing.T) { + cases := []struct { + name, method, source, needle string + numeric bool + want [2]int + candidates []string + }{ + {"IndexLongerFirst", "index", "abc", "zz", false, [2]int{-1, -1}, []string{"", "ab", "abc", "a", "bc", "b", "c"}}, + {"PrefixLongerFirst", "prefix", "abc", "zz", false, [2]int{0, 0}, []string{"", "ab", "abc", "a"}}, + {"SuffixLongerFirst", "suffix", "abc", "zz", false, [2]int{0, 0}, []string{"", "bc", "abc", "c"}}, + {"ShortSourceIndex", "index", "a", "longer", false, [2]int{-1, -1}, []string{"", "a"}}, + {"ShortSourcePrefix", "prefix", "a", "longer", false, [2]int{0, 0}, []string{"", "a"}}, + {"EmptySourceIndex", "index", "", "a", false, [2]int{-1, -1}, []string{""}}, + {"EmptySourcePrefix", "prefix", "", "a", false, [2]int{0, 0}, []string{"", ""}}, + {"EmptySourceSuffix", "suffix", "", "a", false, [2]int{0, 0}, []string{"", ""}}, + {"EmptyNeedleIndex", "index", "abc", "", false, [2]int{0, 0}, nil}, + {"EmptyNeedlePrefix", "prefix", "abc", "", false, [2]int{0, 1}, nil}, + {"EmptyNeedleSuffix", "suffix", "abc", "", false, [2]int{3, 1}, nil}, + {"NumericSplitContinues", "index", "100", "10", true, [2]int{-1, -1}, []string{"", "10", "100", "1", "00", "0", "0"}}, + {"NonNumericAccepts", "index", "100", "10", false, [2]int{0, 2}, []string{"", "10"}}, + {"MalformedSourceIndex", "index", "\xffa", "a", false, [2]int{1, 2}, []string{"", "\ufffd", "\ufffda", "a"}}, + {"MalformedSourcePrefix", "prefix", "\xffa", "\ufffd", false, [2]int{1, 1}, []string{"", "\ufffd"}}, + {"MalformedSourceSuffix", "suffix", "a\xff", "\ufffd", false, [2]int{1, 1}, []string{"", "\ufffd"}}, + {"MalformedNeedle", "index", "\ufffd", "\xff", false, [2]int{-1, -1}, []string{"", "\ufffd"}}, + {"ValidReplacement", "index", "\ufffda", "a", false, [2]int{1, 2}, []string{"", "\ufffd", "\ufffda", "a"}}, + {"Supplementary", "index", "😀a", "a", false, [2]int{1, 2}, []string{"", "😀", "😀a", "a"}}, + {"CombiningNeedleRuneCount", "prefix", "é", "e\u0301", false, [2]int{0, 0}, []string{"", "é"}}, + } + type comparison struct { + candidate, needle string + result int + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + var trace []comparison + cmp := func(a, b string) int { + r := strings.Compare(a, b) + trace = append(trace, comparison{a, b, r}) + return r + } + var got [2]int + switch tc.method { + case "index": + got[0], got[1] = ucaIndexOfRange(cmp, tc.source, tc.needle, tc.numeric) + case "prefix": + end, ok := ucaHasPrefixRange(cmp, tc.source, tc.needle, tc.numeric) + got[0] = end + if ok { + got[1] = 1 + } + case "suffix": + start, ok := ucaHasSuffixRange(cmp, tc.source, tc.needle, tc.numeric) + got[0] = start + if ok { + got[1] = 1 + } + default: + t.Fatal("unknown test method") + } + var want []comparison + // Candidate order is explicit test data; only comparator result comes from + // the independent strings.Compare, never from the candidate constructor. + for _, candidate := range tc.candidates { + want = append(want, comparison{candidate, tc.needle, strings.Compare(candidate, tc.needle)}) + } + if got != tc.want || !reflect.DeepEqual(trace, want) { + t.Fatalf("got%v trace%#v; want%v trace%#v", got, trace, tc.want, want) + } + }) + } +} + +func TestUCACandidateBoundaryModes(t *testing.T) { + for _, tc := range []struct { + name, source, needle string + wantBoundaries []int + start, end int + wantCandidate string + }{ + {"ASCII", "abc", "a", []int{}, 1, 2, "b"}, + {"EmptyASCII", "", "a", []int{}, 0, 0, ""}, + {"ASCIIWithUnicodeNeedle", "abc", "é", []int{}, 0, 2, "ab"}, + {"ASCIIControls", "\x00a", "a", []int{}, 0, 1, "\x00"}, + {"Unicode", "aéz", "a", []int{0, 1, 3, 4}, 1, 2, "é"}, + {"Combining", "e\u0301z", "é", []int{0, 1, 3, 4}, 0, 2, "e\u0301"}, + {"ValidReplacement", "\ufffda", "a", []int{0, 3, 4}, 0, 1, "\ufffd"}, + {"MalformedSourceWidthOne", "\xffa", "a", nil, 0, 1, "\ufffd"}, + {"MalformedSourceTruncated", "\xe2\x82a", "a", nil, 0, 2, "\ufffd\ufffd"}, + {"MalformedNeedle", "abc", "\xff", nil, 1, 2, "b"}, + {"EmptySourceMalformedNeedle", "", "\xff", nil, 0, 0, ""}, + } { + t.Run(tc.name, func(t *testing.T) { + runes := []rune(tc.source) + boundaries := ucaCandidateBoundaries(tc.source, tc.needle, len(runes)) + if !reflect.DeepEqual(boundaries, tc.wantBoundaries) { + t.Fatalf("boundaries %#v; want %#v", boundaries, tc.wantBoundaries) + } + + candidate := ucaCandidateString(tc.source, runes, boundaries, tc.start, tc.end) + if candidate != tc.wantCandidate { + t.Fatalf("candidate %q; want %q", candidate, tc.wantCandidate) + } + }) + } +} + +func TestSerializationCharacterRunsOriginalByteBoundaries(t *testing.T) { + t.Parallel() + for _, form := range []string{"none", "NFC"} { + for _, tc := range []struct { + name, source, want string + mapping map[rune]string + }{ + {"one invalid byte", "\xff", "x", map[rune]string{'\uFFFD': "x"}}, + {"two invalid bytes", "\xff\xfe", "xx", map[rune]string{'\uFFFD': "x"}}, + {"invalid middle", "A\xffB", "AxB", map[rune]string{'\uFFFD': "x"}}, + {"delete invalid middle", "A\xffB", "AB", map[rune]string{'\uFFFD': ""}}, + {"encoded replacement", "\uFFFD", "x", map[rune]string{'\uFFFD': "x"}}, + {"no map", "\xff", "\xff", nil}, + {"unrelated map", "\xff", "\xff", map[rune]string{'Q': "y"}}, + {"valid composition", "A\u030A", "Å", map[rune]string{'\uFFFD': "x"}}, + } { + if form == "none" && tc.name == "valid composition" { + tc.want = tc.source + } + t.Run(form+"/"+tc.name, func(t *testing.T) { + t.Parallel() + defer func() { + if p := recover(); p != nil { + t.Errorf("character mapping must not panic for original bytes %x: %v", tc.source, p) + } + }() + opts := serializeOptions{ctx: t.Context(), normalizationForm: form, charMap: tc.mapping} + var out strings.Builder + emit := func(s string) error { _, err := out.WriteString(s); return err } + if err := opts.characterRuns(tc.source, emit, emit); err != nil { + t.Fatal(err) + } + if got := out.String(); got != tc.want { + t.Fatalf("actual %q bytes %x; want %q bytes %x", got, got, tc.want, tc.want) + } + }) + } + } +} + +func TestSerializationMalformedRunsCallbackErrors(t *testing.T) { + t.Parallel() + sentinel := errors.New("character callback stopped") + for _, failAt := range []string{"plain", "mapped"} { + t.Run(failAt, func(t *testing.T) { + t.Parallel() + opts := serializeOptions{ctx: t.Context(), charMap: map[rune]string{'\uFFFD': "x"}} + var calls []string + err := opts.characterRuns("A\xffB", func(s string) error { + calls = append(calls, "plain:"+s) + if failAt == "plain" { + return sentinel + } + return nil + }, func(s string) error { calls = append(calls, "mapped:"+s); return sentinel }) + want := []string{"plain:A"} + if failAt == "mapped" { + want = append(want, "mapped:x") + } + if !errors.Is(err, sentinel) || !reflect.DeepEqual(calls, want) { + t.Fatalf("calls %q/error %v; want %q/sentinel", calls, err, want) + } + }) + } +} + +func TestSerializationJSONRunOriginalByteBoundaries(t *testing.T) { + t.Parallel() + for _, tc := range []struct{ name, source, want string }{ + {"one invalid byte", "\xff", `\uFFFD`}, + {"two invalid bytes", "\xff\xfe", `\uFFFD\uFFFD`}, + {"invalid middle", "A\xffB", `A\uFFFDB`}, + {"encoded replacement", "\uFFFD", `\uFFFD`}, + {"valid ASCII", "ABC", "ABC"}, + } { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + defer func() { + if p := recover(); p != nil { + t.Errorf("JSON escaping must not panic for original bytes %x: %v", tc.source, p) + } + }() + var out strings.Builder + if err := writeSerializedJSONRun(t.Context(), &out, tc.source, func(r rune) bool { return r < 128 }); err != nil { + t.Fatal(err) + } + if got := out.String(); got != tc.want { + t.Fatalf("actual %q; want %q", got, tc.want) + } + }) + } + sentinel := errors.New("JSON destination stopped") + for _, failAt := range []int{1, 2} { + t.Run(string(rune('0'+failAt)), func(t *testing.T) { + t.Parallel() + dst := &malformedJSONSentinelWriter{cause: sentinel, failAt: failAt} + err := writeSerializedJSONRun(t.Context(), dst, "A\xffB", func(r rune) bool { return r < 128 }) + if !errors.Is(err, sentinel) || dst.calls != failAt { + t.Fatalf("calls %d/error %v; want %d/sentinel", dst.calls, err, failAt) + } + }) + } +} + +type malformedJSONSentinelWriter struct { + cause error + failAt, calls int +} + +func (w *malformedJSONSentinelWriter) Write(p []byte) (int, error) { + w.calls++ + if w.calls == w.failAt { + return 0, w.cause + } + return len(p), nil +} diff --git a/transform/internal/xpath/functions_unparsed_text.go b/transform/internal/xpath/functions_unparsed_text.go index 377443b..d7ffd70 100644 --- a/transform/internal/xpath/functions_unparsed_text.go +++ b/transform/internal/xpath/functions_unparsed_text.go @@ -13,14 +13,29 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) +// Explicit encoding requires the second argument. A zero byte/cache setting +// selects defaults; line limit zero disables the line bound, while one is the +// minimum splitter request when an exhausted operation budget must still fail. +const ( + textEncodingArgumentThreshold = 1 + defaultTextResourceByteLimit = 0 + textCacheDefaultThreshold = 0 + noTextResourceLineLimit = 0 + minimumTextSplitLineLimit = 1 +) + +// A negative remaining-byte value means the available() validation has no +// output budget bound; retain int64 so subtraction and comparisons stay typed. +const unlimitedTextRemainingBytes = int64(-1) + func lookupFunctionsUnparsedText(name string) (*builtinFunc, bool) { switch name { case fnNameUnparsedText: - return &builtinFunc{name: name, min: 1, max: 2, fn: fnUnparsedText}, true + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityTwo, fn: fnUnparsedText}, true case fnNameUnparsedTextAvailable: - return &builtinFunc{name: name, min: 1, max: 2, fn: fnUnparsedTextAvailable}, true + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityTwo, fn: fnUnparsedTextAvailable}, true case nameUnparsedTextLines: - return &builtinFunc{name: name, min: 1, max: 2, fn: fnUnparsedTextLines}, true + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityTwo, fn: fnUnparsedTextLines}, true } return nil, false @@ -84,12 +99,12 @@ const ( func textResourceStore(ctx context.Context, ec *evalContext) *textSnapshotStore { cache, _ := InvocationState(ctx, textSnapshotKey{}, func() any { limit, _ := ctx.Value(textResourceCacheLimitKey{}).(int64) - if limit <= 0 { + if limit <= textCacheDefaultThreshold { if ec != nil { limit = ec.maxResourceBytes } - if limit <= 0 { + if limit <= textCacheDefaultThreshold { limit = unparsedtext.DefaultMaxBytes } } @@ -183,7 +198,7 @@ func readTextResourceSnapshot(ctx context.Context, ec *evalContext, cfg *unparse } maximum := cfg.MaxBytes - if maximum == 0 { + if maximum == defaultTextResourceByteLimit { maximum = unparsedtext.DefaultMaxBytes } @@ -289,7 +304,7 @@ func readTextResourceSource(ctx context.Context, cfg *unparsedtext.Config, uri, func configureTextResourceRead(ctx context.Context, cfg *unparsedtext.Config, entry *textResourceSnapshot, mode textResourceMode, retentionLimit int64) int64 { cfg.ReadBytes = func(n int) { entry.rawBytes += int64(n) } - remaining := int64(-1) + remaining := unlimitedTextRemainingBytes if mode == textResourceAvailable { cfg.CheckBytes = nil // A validation probe does not produce an XDM string. @@ -380,14 +395,14 @@ func fnUnparsedText(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, err } encoding := "" - if len(args) > 1 { + if len(args) > textEncodingArgumentThreshold { encoding, err = coerceArgToString(ctx, args[1]) if err != nil { return nil, err } } - text, err := loadTextResource(ctx, href, encoding, len(args) > 1, textResourceValue, 0, nil) + text, err := loadTextResource(ctx, href, encoding, len(args) > textEncodingArgumentThreshold, textResourceValue, noTextResourceLineLimit, nil) if err != nil { return nil, wrapUnparsedTextError(err) } @@ -408,14 +423,14 @@ func fnUnparsedTextAvailable(ctx context.Context, args []xdm.Sequence) (xdm.Sequ } encoding := "" - if len(args) > 1 { + if len(args) > textEncodingArgumentThreshold { encoding, err = coerceArgToString(ctx, args[1]) if err != nil { return nil, err } } - _, err = loadTextResource(ctx, href, encoding, len(args) > 1, textResourceAvailable, 0, nil) + _, err = loadTextResource(ctx, href, encoding, len(args) > textEncodingArgumentThreshold, textResourceAvailable, noTextResourceLineLimit, nil) if ControlFailure(ctx, err) != nil || errors.Is(err, tree.ErrLimit) { return nil, err } @@ -440,7 +455,7 @@ func fnUnparsedTextLines(ctx context.Context, args []xdm.Sequence) (xdm.Sequence } encoding := "" - if len(args) > 1 { + if len(args) > textEncodingArgumentThreshold { encoding, err = coerceArgToString(ctx, args[1]) if err != nil { return nil, err @@ -466,8 +481,8 @@ func fnUnparsedTextLines(ctx context.Context, args []xdm.Sequence) (xdm.Sequence // budget (rem == 0) must still cap allocation, so floor the splitter bound at // 1. The op charge below then rejects the single produced line via ErrOpLimit. splitLimit := limit - if opBounded && splitLimit < 1 { - splitLimit = 1 + if opBounded && splitLimit < minimumTextSplitLineLimit { + splitLimit = minimumTextSplitLineLimit } lineFailure := ErrNodeSetLimit @@ -475,7 +490,7 @@ func fnUnparsedTextLines(ctx context.Context, args []xdm.Sequence) (xdm.Sequence lineFailure = ErrOpLimit } - body, err := loadTextResource(ctx, href, encoding, len(args) > 1, textResourceLines, splitLimit, lineFailure) + body, err := loadTextResource(ctx, href, encoding, len(args) > textEncodingArgumentThreshold, textResourceLines, splitLimit, lineFailure) lines, truncated := unparsedtext.SplitLinesBounded(body, splitLimit) if err != nil { diff --git a/transform/internal/xpath/functions_uri.go b/transform/internal/xpath/functions_uri.go index d18309b..3ae88d0 100644 --- a/transform/internal/xpath/functions_uri.go +++ b/transform/internal/xpath/functions_uri.go @@ -13,6 +13,26 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) +// URI helper dispatch and the absent-static-base error retain F&O spellings. +const ( + fnNameEncodeForURI = "encode-for-uri" + fnNameIRIToURI = "iri-to-uri" + fnNameEscapeHTMLURI = "escape-html-uri" + fnNameResolveURI = "resolve-uri" + errCodeFONS0005 = "FONS0005" +) + +// Percent escapes encode each byte as two hexadecimal nibbles. URI escaping +// and IRI validation retain their distinct space/control boundary comparisons. +const ( + uriHexDigitBits = 4 + uriHexDigitMask = 0x0F + uriEscapeHexDigits = 2 + uriPrintableASCIIMin = 0x20 + uriPrintableASCIIMax = 0x7E + iriC1ControlEnd = 0xA0 +) + // ResolveURIReference applies fn:resolve-uri's IRI rules to explicit strings. // An absolute reference does not inspect the base. Hosts can share the standard // URI semantics without invoking an XPath evaluator or rewriting IRI characters. @@ -35,14 +55,14 @@ func ResolveURIReference(relative, base string) (string, error) { func lookupFunctionsURI(name string) (*builtinFunc, bool) { switch name { - case "encode-for-uri": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnEncodeForURI}, true - case "iri-to-uri": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnIRIToURI}, true - case "escape-html-uri": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnEscapeHTMLURI}, true - case "resolve-uri": - return &builtinFunc{name: name, min: 1, max: 2, fn: fnResolveURI}, true + case fnNameEncodeForURI: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnEncodeForURI}, true + case fnNameIRIToURI: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnIRIToURI}, true + case fnNameEscapeHTMLURI: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnEscapeHTMLURI}, true + case fnNameResolveURI: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityTwo, fn: fnResolveURI}, true } return nil, false @@ -75,11 +95,11 @@ func encodeForURI(s string) string { } func writePercentEncodedByte(b *strings.Builder, c byte) { - const hex = "0123456789ABCDEF" + const percentEscapeHexAlphabet = "0123456789ABCDEF" b.WriteByte('%') - b.WriteByte(hex[c>>4]) - b.WriteByte(hex[c&0x0F]) + b.WriteByte(percentEscapeHexAlphabet[c>>uriHexDigitBits]) + b.WriteByte(percentEscapeHexAlphabet[c&uriHexDigitMask]) } func isUnreservedChar(c byte) bool { @@ -101,13 +121,13 @@ func fnIRIToURI(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) for i := range len(s) { c := s[i] - if c == '%' && i+2 < len(s) && xmlchar.IsHexDigit(s[i+1]) && xmlchar.IsHexDigit(s[i+2]) { + if c == '%' && i+uriEscapeHexDigits < len(s) && xmlchar.IsHexDigit(s[i+1]) && xmlchar.IsHexDigit(s[i+uriEscapeHexDigits]) { // Already percent-encoded: pass through b.WriteByte(c) continue } - if c > 0x7E || c <= 0x20 || c == '"' || c == '<' || c == '>' || + if c > uriPrintableASCIIMax || c <= uriPrintableASCIIMin || c == '"' || c == '<' || c == '>' || c == '{' || c == '}' || c == '|' || c == '\\' || c == '^' || c == '`' { writePercentEncodedByte(&b, c) } else { @@ -147,7 +167,7 @@ func fnResolveURI(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error } base := "" - if len(args) == 2 { + if len(args) == builtinArityTwo { base, err = coerceArgToString(ctx, args[1]) if err != nil { return nil, err @@ -155,7 +175,7 @@ func fnResolveURI(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error } else { base = baseURIFromContext(ctx) if base == "" { - return nil, &xdm.Error{Code: "FONS0005", Message: "static base URI is absent"} + return nil, &xdm.Error{Code: errCodeFONS0005, Message: "static base URI is absent"} } } @@ -207,7 +227,7 @@ func validateIRI(s string) error { } for _, r := range s { - if r < 0x20 || (r > 0x7E && r < 0xA0) { + if r < uriPrintableASCIIMin || (r > uriPrintableASCIIMax && r < iriC1ControlEnd) { return fmt.Errorf("invalid IRI character U+%04X", r) } } @@ -225,7 +245,7 @@ func fnEscapeHTMLURI(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, er for i := range len(s) { c := s[i] - if c > 0x7E || c < 0x20 { + if c > uriPrintableASCIIMax || c < uriPrintableASCIIMin { writePercentEncodedByte(&b, c) } else { b.WriteByte(c) diff --git a/transform/internal/xpath/lexer.go b/transform/internal/xpath/lexer.go index c620609..c31aad7 100644 --- a/transform/internal/xpath/lexer.go +++ b/transform/internal/xpath/lexer.go @@ -29,6 +29,20 @@ type lexer struct { version int } +// SplitUnion returns the outer union operands, using XPath's lexical rules. +// Parentheses enclosing an entire union are stripped, as required by XSLT's +// default-priority rules. Other expressions retain their original source. +func SplitUnion(input string, version int) ([]string, error) { + return splitUnion(input, version, false) +} + +// SplitPatternUnion retains the narrower XSLT 1.0/2.0 pattern grammar: +// only a top-level pipe separates alternatives, and grouping parentheses +// and explicit non-pattern axes remain forbidden outside predicates. +func SplitPatternUnion(input string, version int) ([]string, error) { + return splitUnion(input, version, version == xpathVersion10 || version == xpathVersion20) +} + // newLexer creates a lexer and tokenizes the entire input. func newLexer(input string) (*lexer, error) { return newVersionLexer(input, 0) @@ -176,8 +190,8 @@ func (l *lexer) scanToken() error { func (l *lexer) scanParenthesisOrComment() error { if l.pos+1 < len(l.input) && l.input[l.pos+1] == ':' { - if l.version == 10 { - return profileError(lexicon.ErrXPST0003, "comments", 10) + if l.version == xpathVersion10 { + return profileError(lexicon.ErrXPST0003, "comments", xpathVersion10) } return l.skipComment() @@ -365,8 +379,8 @@ func (l *lexer) scanString() (string, error) { if l.input[l.pos] == quote { // XPath 3.1: doubled quote is an escape ('' → ', "" → ") if l.pos+1 < len(l.input) && l.input[l.pos+1] == quote { - if l.version == 10 { - return "", profileError(lexicon.ErrXPST0003, "doubled quote escape", 10) + if l.version == xpathVersion10 { + return "", profileError(lexicon.ErrXPST0003, "doubled quote escape", xpathVersion10) } l.pos += 2 @@ -502,7 +516,7 @@ func (l *lexer) scanURIQualifiedName(wildcard bool) (string, error) { l.pos++ // '}' if uri == lexicon.NamespaceXMLNS { - return "", &xdm.Error{Code: "XQST0070", Message: "the xmlns namespace URI cannot be used in an EQName"} + return "", &xdm.Error{Code: errCodeXQST0070, Message: "the xmlns namespace URI cannot be used in an EQName"} } local := l.scanNCName() @@ -580,22 +594,8 @@ type sourceSpan struct { end int } -// SplitUnion returns the outer union operands, using XPath's lexical rules. -// Parentheses enclosing an entire union are stripped, as required by XSLT's -// default-priority rules. Other expressions retain their original source. -func SplitUnion(input string, version int) ([]string, error) { - return splitUnion(input, version, false) -} - -// SplitPatternUnion retains the narrower XSLT 1.0/2.0 pattern grammar: -// only a top-level pipe separates alternatives, and grouping parentheses -// and explicit non-pattern axes remain forbidden outside predicates. -func SplitPatternUnion(input string, version int) ([]string, error) { - return splitUnion(input, version, version == 10 || version == 20) -} - func splitUnion(input string, version int, legacyPattern bool) ([]string, error) { - if !legacyPattern && !strings.Contains(input, "|") && !strings.Contains(input, "union") { + if !legacyPattern && !strings.Contains(input, "|") && !strings.Contains(input, nameUnion) { return []string{input}, nil } @@ -698,13 +698,21 @@ func unionTokenBounds(tokens []Token) (int, int) { } func estimateTokenCapacity(input string) int { - if len(input) < 8 { - return 8 + // Short sources get a fixed token reservation; larger sources use a capped + // estimate. These are allocation hints, not source or token acceptance limits. + const ( + shortSourceThresholdBytes = 8 + shortSourceTokenCapacity = 8 + maximumEstimatedTokenCapacity = 256 + ) + + if len(input) < shortSourceThresholdBytes { + return shortSourceTokenCapacity } // Comments and whitespace can dominate the source without producing tokens. // Grow for actual tokens rather than reserving a source-sized token array. - return min(len(input)/2+1, 256) + return min(len(input)/2+1, maximumEstimatedTokenCapacity) } func singleRuneToken(r rune) (TokenType, bool) { diff --git a/transform/internal/xpath/node_identity.go b/transform/internal/xpath/node_identity.go index f9102a8..0403c71 100644 --- a/transform/internal/xpath/node_identity.go +++ b/transform/internal/xpath/node_identity.go @@ -15,22 +15,31 @@ import ( // prefix rather than the newly allocated wrapper. Components are delimited so // different owner/prefix combinations cannot collide. func StableNodeID(n xdm.Node) string { + // Prefixes separate ordinary and namespace identities; hex encoding prevents + // delimiter collisions. A hex NUL cannot be a valid namespace prefix. + const ( + namespaceNodeIDPrefix = "idns" + ordinaryNodeIDPrefix = "idn" + emptyNamespacePrefixHex = "00" + nodeIDRadix = 16 + ) + if n == nil { return "" } if nsk, ok := xdm.NamespaceNodeKey(n); ok { - parentHex := strconv.FormatUint(xdm.NodeIdentity(nsk.Parent), 16) + parentHex := strconv.FormatUint(xdm.NodeIdentity(nsk.Parent), nodeIDRadix) prefixHex := hex.EncodeToString([]byte(nsk.Prefix)) if prefixHex == "" { - prefixHex = "00" + prefixHex = emptyNamespacePrefixHex } - return "idns" + parentHex + "p" + prefixHex + return namespaceNodeIDPrefix + parentHex + "p" + prefixHex } - return "idn" + strconv.FormatUint(xdm.NodeIdentity(n), 16) + return ordinaryNodeIDPrefix + strconv.FormatUint(xdm.NodeIdentity(n), nodeIDRadix) } // nodeIdentityKey is a map key for node identity (see xdm.SameNode): the diff --git a/transform/internal/xpath/number/format_number.go b/transform/internal/xpath/number/format_number.go index 3e28cde..4b85ca4 100644 --- a/transform/internal/xpath/number/format_number.go +++ b/transform/internal/xpath/number/format_number.go @@ -12,6 +12,63 @@ import ( "slices" "strconv" "strings" + + "github.com/skarm/xmlsec/internal/lexicon" +) + +// Decimal-format defaults follow the XPath/XSLT picture vocabulary. The +// exponent-disable flag is false by default so exponent syntax is enabled. +const ( + defaultInfinity = "Infinity" + defaultDecimalSeparator = '.' + defaultGroupingSeparator = ',' + defaultPercent = '%' + defaultPerMille = '\u2030' + defaultZeroDigit = '0' + defaultDigit = '#' + defaultPatternSeparator = ';' + defaultExponentSeparator = 'e' + defaultMinusSign = '-' + defaultExponentDisabled = false +) + +// Picture cardinalities and fallback digit counts describe separate grammar +// rules, even where their values coincide. Scaling multiplies values by the +// percent or per-mille denominator before picture rounding. +const ( + maxPictureSubpatterns = 2 + maxPercentMarkers = 1 + maxPerMilleMarkers = 1 + minimumRepeatedGroups = 2 + minimumExponentIntegerSlots = 2 + minimumExponentDigits = 1 + minimumMantissaDigits = 1 + percentScale = 100 + perMilleScale = 1000 +) + +// Numeric formatting uses ASCII decimal lexicals before digit localization. +// Powers through 10^18 fit in int64; larger decimal scales use big.Int. +const ( + decimalRadix = 10 + decimalFactorFive = 5 + maxInt64ScaleDigits = 18 + maxDigitOffset = 9 + asciiZeroDigit = '0' + asciiLastDigit = '9' + decimalZero = "0" + asciiDecimalPoint = "." + fixedFloatNotation = 'f' + doublePrecisionBits = 64 + anyInfinitySign = 0 + minimumFractionalMantissa = 0.1 +) + +// Infinity rendering adds a minus prefix only when the selected picture does +// not already supply the negative-value affix. +const ( + prefixWithoutMinus = false + prefixWithMinus = true ) // DecimalFormat defines picture symbols and special-value text for XPath and XSLT @@ -48,18 +105,18 @@ type DecimalFormat struct { // including e as the exponent separator. func DefaultDecimalFormat() DecimalFormat { return DecimalFormat{ - Infinity: "Infinity", - NaN: "NaN", - DecimalSeparator: '.', - GroupingSeparator: ',', - Percent: '%', - PerMille: '\u2030', - ZeroDigit: '0', - Digit: '#', - PatternSeparator: ';', - ExponentSeparator: 'e', - MinusSign: '-', - ExponentDisabled: false, + Infinity: defaultInfinity, + NaN: lexicon.FloatNaN, + DecimalSeparator: defaultDecimalSeparator, + GroupingSeparator: defaultGroupingSeparator, + Percent: defaultPercent, + PerMille: defaultPerMille, + ZeroDigit: defaultZeroDigit, + Digit: defaultDigit, + PatternSeparator: defaultPatternSeparator, + ExponentSeparator: defaultExponentSeparator, + MinusSign: defaultMinusSign, + ExponentDisabled: defaultExponentDisabled, } } @@ -130,7 +187,7 @@ func ParsePicture(pic string, df DecimalFormat) (ParsedPicture, error) { // - df: the decimal format specification func FormatNumber(f float64, isNaN, isPosInf, isNegInf, negative bool, precise *big.Rat, picture string, df DecimalFormat) (string, error) { parts := strings.Split(picture, string(df.PatternSeparator)) - if len(parts) > 2 { + if len(parts) > maxPictureSubpatterns { return "", fmt.Errorf("picture %q contains more than one pattern separator", picture) } @@ -144,15 +201,15 @@ func FormatNumber(f float64, isNaN, isPosInf, isNegInf, negative bool, precise * } if isPosInf { - return formatInfinityPicture(positive, false, df) + return formatInfinityPicture(positive, prefixWithoutMinus, df) } if isNegInf { if negativePicture != "" { - return formatInfinityPicture(negativePicture, false, df) + return formatInfinityPicture(negativePicture, prefixWithoutMinus, df) } - return formatInfinityPicture(positive, true, df) + return formatInfinityPicture(positive, prefixWithMinus, df) } if negative { @@ -170,9 +227,9 @@ func FormatNumber(f float64, isNaN, isPosInf, isNegInf, negative bool, precise * } if pp.IsPercent { - f *= 100 + f *= percentScale } else if pp.IsPerMille { - f *= 1000 + f *= perMilleScale } prefix := pp.Prefix @@ -180,8 +237,9 @@ func FormatNumber(f float64, isNaN, isPosInf, isNegInf, negative bool, precise * prefix = string(df.MinusSign) + prefix } - // Multiplier overflow takes priority even when a precise value is supplied. - if math.IsInf(f, 0) { + // A precise integer/decimal is scaled by exact arithmetic below. Its + // float64 approximation can overflow even when the actual value is finite. + if precise == nil && math.IsInf(f, anyInfinitySign) { return prefix + df.Infinity + pp.Suffix, nil } @@ -197,7 +255,7 @@ func FormatFloat(f float64, pp ParsedPicture, df DecimalFormat) string { // no-op for the representable value, so leaving f unchanged is correct and // keeps big.Float.SetFloat64 from ever seeing NaN/Inf. if pp.MaxFracDigits >= 0 { - scale := math.Pow(10, float64(pp.MaxFracDigits)) + scale := math.Pow(decimalRadix, float64(pp.MaxFracDigits)) if scaled := f * scale; isFinite(scale) && isFinite(scaled) { f = math.RoundToEven(scaled) / scale } @@ -207,12 +265,12 @@ func FormatFloat(f float64, pp ParsedPicture, df DecimalFormat) string { trunc := math.Trunc(f) // Format integer part - bigTrunc, _ := new(big.Float).SetFloat64(trunc).Int(nil) + bigTrunc, _ := new(big.Float).SetFloat64(trunc).Int(new(big.Int)) intStr := FormatBigInt(bigTrunc, pp.MinIntDigits, pp.GroupingSizes, pp.RepeatGrouping, df) if !pp.HasDecimalPoint && pp.MaxFracDigits == 0 { if intStr == "" { - intStr = "0" + intStr = decimalZero } return intStr @@ -221,8 +279,8 @@ func FormatFloat(f float64, pp ParsedPicture, df DecimalFormat) string { // Render the rounded value as a whole. Subtracting its integer part and // repeatedly multiplying the remainder can lose the last decimal digit // (for example, 0.123456 became 0.123455). - _, fracStr, _ := strings.Cut(strconv.FormatFloat(f, 'f', pp.MaxFracDigits, 64), ".") - for len(fracStr) > pp.MinFracDigits && strings.HasSuffix(fracStr, "0") { + _, fracStr, _ := strings.Cut(strconv.FormatFloat(f, fixedFloatNotation, pp.MaxFracDigits, doublePrecisionBits), asciiDecimalPoint) + for len(fracStr) > pp.MinFracDigits && strings.HasSuffix(fracStr, decimalZero) { fracStr = fracStr[:len(fracStr)-1] } @@ -232,9 +290,9 @@ func FormatFloat(f float64, pp ParsedPicture, df DecimalFormat) string { // to zero with MinIntDigits=0 and MinFracDigits=0), show at least one digit. if intStr == "" && fracStr == "" { if pp.MaxFracDigits > 0 { - fracStr = "0" + fracStr = decimalZero } else { - intStr = "0" + intStr = decimalZero } } @@ -253,9 +311,9 @@ func FormatDecimalPrecise(r *big.Rat, pp ParsedPicture, df DecimalFormat) string r = new(big.Rat).Set(r) if pp.IsPercent { - r.Mul(r, new(big.Rat).SetInt64(100)) + r.Mul(r, new(big.Rat).SetInt64(percentScale)) } else if pp.IsPerMille { - r.Mul(r, new(big.Rat).SetInt64(1000)) + r.Mul(r, new(big.Rat).SetInt64(perMilleScale)) } if r.Sign() < 0 { @@ -272,7 +330,7 @@ func FormatDecimalPrecise(r *big.Rat, pp ParsedPicture, df DecimalFormat) string if !pp.HasDecimalPoint && pp.MaxFracDigits == 0 { if intStr == "" { - intStr = "0" + intStr = decimalZero } return intStr @@ -284,9 +342,9 @@ func FormatDecimalPrecise(r *big.Rat, pp ParsedPicture, df DecimalFormat) string // to zero with MinIntDigits=0 and MinFracDigits=0), show at least one digit. if intStr == "" && fracStr == "" { if pp.MaxFracDigits > 0 { - fracStr = "0" + fracStr = decimalZero } else { - intStr = "0" + intStr = decimalZero } } @@ -302,12 +360,12 @@ func FormatDecimalPrecise(r *big.Rat, pp ParsedPicture, df DecimalFormat) string // FormatBigInt formats an integer with minimum digits and grouping separators. func FormatBigInt(n *big.Int, minDigits int, groupingSizes []int, repeatGrouping bool, df DecimalFormat) string { s := n.String() - if minDigits == 0 && s == "0" { + if minDigits == 0 && s == decimalZero { s = "" } // Pad to minDigits if padding := minDigits - len(s); padding > 0 { - s = strings.Repeat("0", padding) + s + s = strings.Repeat(decimalZero, padding) + s } if len(groupingSizes) == 0 || len(s) == 0 { @@ -358,80 +416,97 @@ func FormatFrac(frac float64, minDigits, maxDigits int, df DecimalFormat) string remaining := frac for range maxDigits { - remaining *= 10 + remaining *= decimalRadix d := int(remaining) - digits = append(digits, byte('0'+d)) // #nosec G115 -- The nonnegative fractional remainder produces a decimal digit in 0..9. + digits = append(digits, byte(asciiZeroDigit+d)) // #nosec G115 -- The nonnegative fractional remainder produces a decimal digit in 0..9. remaining -= float64(d) } // Trim trailing zeros beyond minDigits end := len(digits) - for end > minDigits && digits[end-1] == '0' { + for end > minDigits && digits[end-1] == asciiZeroDigit { end-- } return localizeDigits(string(digits[:end]), df.ZeroDigit) } -// FormatRatFrac formats the fractional part of a *big.Rat. +// FormatRatFrac formats a nonnegative proper fraction, truncating at maxDigits. +// The caller bounds maxDigits before allocating the decimal scale. func FormatRatFrac(frac *big.Rat, minDigits, maxDigits int, df DecimalFormat) string { - if maxDigits == 0 { + if maxDigits <= 0 { return "" } - var digits []byte + // One scaled integer division produces all digits; per-digit long division + // repeatedly divides by a potentially wide rounded decimal denominator. + scale := new(big.Int) - // Long division keeps the denominator fixed. Rebuilding and reducing a - // rational for each digit needlessly allocates big integers and runs GCD. - // The private remainder also preserves the caller's reusable fraction. - ten := big.NewInt(10) + if maxDigits <= maxInt64ScaleDigits { + // These powers fit in int64; avoid big-integer exponentiation overhead. + power := int64(1) + for range maxDigits { + power *= decimalRadix + } + + scale.SetInt64(power) + } else { + scale.Exp(big.NewInt(decimalRadix), big.NewInt(int64(maxDigits)), nil) + } - var rem, digit big.Int - rem.Set(frac.Num()) + scaled := new(big.Int).Mul(frac.Num(), scale) + scaled.Quo(scaled, frac.Denom()) - for range maxDigits { - rem.Mul(&rem, ten) - digit.QuoRem(&rem, frac.Denom(), &rem) - digits = append(digits, byte('0'+digit.Int64())) // #nosec G115 -- Long division of a nonnegative proper fraction produces digits in 0..9. + digits := scaled.String() + if padding := maxDigits - len(digits); padding > 0 { + digits = strings.Repeat(decimalZero, padding) + digits } end := len(digits) - for end > minDigits && digits[end-1] == '0' { + for end > minDigits && digits[end-1] == asciiZeroDigit { end-- } - return localizeDigits(string(digits[:end]), df.ZeroDigit) + if end < len(digits) { + // A short result must not retain the backing string for a wide picture. + digits = strings.Clone(digits[:end]) + } + + return localizeDigits(digits, df.ZeroDigit) } // RatRoundHalfToEven rounds a *big.Rat to the given precision using -// half-to-even rounding. +// half-to-even rounding. Callers must bound nontrivial precision before this +// helper allocates a power of ten; the host bounds picture widths and numeric +// work before formatting. A precision at least the finite decimal scale is +// handled without exponentiation, even when it is very large. func RatRoundHalfToEven(r *big.Rat, precision int) *big.Rat { if precision < 0 { - // Guard against absurdly large negative precision - if -precision > 1000 { + // If the decimal scale exceeds even the numerator's binary width, + // the value is less than half a rounding unit. This also handles + // MinInt without overflowing when negating precision. + if precision < -r.Num().BitLen() { return new(big.Rat) // rounds to zero } // Round to 10^(-precision) — convert to integer, round, convert back - scale := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(-precision)), nil) - scaleRat := new(big.Rat).SetInt(scale) - divided := new(big.Rat).Quo(r, scaleRat) - rounded := RatRoundHalfToEvenInt(divided) + scale := new(big.Int).Exp(big.NewInt(decimalRadix), big.NewInt(int64(-precision)), nil) + denominator := new(big.Int).Mul(r.Denom(), scale) + rounded := roundHalfToEvenQuotient(r.Num(), denominator) - return new(big.Rat).Mul(new(big.Rat).SetInt(rounded), scaleRat) + return new(big.Rat).SetInt(rounded.Mul(rounded, scale)) } // If precision is very large and already exceeds the denominator's decimal // digits, the value is already exact — return as-is. This avoids computing // astronomically large powers of 10 (e.g. 10^4294967296). - if precision > 1000 || RatDecimalDigits(r) <= precision { + if RatDecimalDigits(r) <= precision { return new(big.Rat).Set(r) } // Multiply by 10^precision, round to integer, divide back - scale := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(precision)), nil) - scaleRat := new(big.Rat).SetInt(scale) - shifted := new(big.Rat).Mul(r, scaleRat) - rounded := RatRoundHalfToEvenInt(shifted) + scale := new(big.Int).Exp(big.NewInt(decimalRadix), big.NewInt(int64(precision)), nil) + numerator := new(big.Int).Mul(r.Num(), scale) + rounded := roundHalfToEvenQuotient(numerator, r.Denom()) - return new(big.Rat).SetFrac(rounded, new(big.Int).Set(scale)) + return new(big.Rat).SetFrac(rounded, scale) } // RatDecimalDigits returns the number of decimal digits needed to represent @@ -450,16 +525,18 @@ func RatDecimalDigits(r *big.Rat) int { twos++ } - five := big.NewInt(5) + five := big.NewInt(decimalFactorFive) - mod := new(big.Int) + var quotient, mod big.Int for { - d.QuoRem(d, five, mod) + quotient.QuoRem(d, five, &mod) if mod.Sign() != 0 { break } + d.Set("ient) + fives++ } // If d != 1 after removing all 2s and 5s, the decimal is non-terminating @@ -481,33 +558,25 @@ func RatRoundHalfToEvenInt(r *big.Rat) *big.Int { return new(big.Int).Set(r.Num()) } - intPart := new(big.Int).Quo(r.Num(), r.Denom()) - // Fractional remainder - rem := new(big.Rat).Sub(r, new(big.Rat).SetInt(intPart)) - rem.Abs(rem) + return roundHalfToEvenQuotient(r.Num(), r.Denom()) +} - half := new(big.Rat).SetFrac64(1, 2) - cmp := rem.Cmp(half) +func roundHalfToEvenQuotient(numerator, denominator *big.Int) *big.Int { + var remainder big.Int - if cmp < 0 { - // Closer to floor - return intPart - } + intPart := new(big.Int) + intPart.QuoRem(numerator, denominator, &remainder) + // Compare the exact fractional distance with one half without constructing + // temporary rationals and reducing them by GCD. + remainder.Abs(&remainder) + remainder.Lsh(&remainder, 1) - if cmp > 0 { - // Closer to ceil - if r.Sign() > 0 { - return intPart.Add(intPart, big.NewInt(1)) - } - - return intPart.Sub(intPart, big.NewInt(1)) - } - // Exactly half — round to even - if new(big.Int).And(intPart, big.NewInt(1)).Sign() == 0 { - return intPart // already even + comparison := remainder.Cmp(denominator) + if comparison < 0 || comparison == 0 && intPart.Bit(0) == 0 { + return intPart } - if r.Sign() > 0 { + if numerator.Sign() > 0 { return intPart.Add(intPart, big.NewInt(1)) } @@ -515,7 +584,7 @@ func RatRoundHalfToEvenInt(r *big.Rat) *big.Int { } func isMandatoryDigit(r, zeroDigit rune) bool { - return r >= zeroDigit && r <= zeroDigit+9 + return r >= zeroDigit && r <= zeroDigit+maxDigitOffset } func isPatternDigit(r rune, df DecimalFormat) bool { @@ -563,12 +632,12 @@ func parseGroupedPart(part []rune, sep rune, df DecimalFormat) ([]int, int, int, } func shouldRepeatGrouping(groups []int) bool { - if len(groups) < 2 { + if len(groups) < minimumRepeatedGroups { return false } repeated := groups[1] - for i := 2; i < len(groups); i++ { + for i := minimumRepeatedGroups; i < len(groups); i++ { if groups[i] != repeated { return false } @@ -597,7 +666,7 @@ func splitPictureExponent(runes []rune, df DecimalFormat) ([]rune, []rune, bool) separator := df.ExponentSeparator if separator == 0 { - separator = 'e' + separator = defaultExponentSeparator } precededByActive := false @@ -657,11 +726,11 @@ func validatePictureMultipliers(runes []rune, df DecimalFormat) error { } } - if percent > 1 { + if percent > maxPercentMarkers { return fmt.Errorf("picture contains more than one percent sign") } - if perMille > 1 { + if perMille > maxPerMilleMarkers { return fmt.Errorf("picture contains more than one per-mille sign") } @@ -699,7 +768,7 @@ func parsePictureSuffix(pp *ParsedPicture, mantissaSuffix, exponent []rune, df D } if pp.MinExpDigits == 0 { - pp.MinExpDigits = 1 + pp.MinExpDigits = minimumExponentDigits } suffix := exponent[end:] @@ -857,7 +926,7 @@ func exponentScalingFactor(pp ParsedPicture) int { return pp.MinIntDigits } - if pp.MaxIntDigits >= 2 { + if pp.MaxIntDigits >= minimumExponentIntegerSlots { return pp.MaxIntDigits } @@ -883,11 +952,11 @@ func adjustMantissaPP(pp ParsedPicture, m int) ParsedPicture { } // Show leading zero when pattern had integer digit placeholders if pp.MaxIntDigits > 0 { - r.MinIntDigits = 1 + r.MinIntDigits = minimumMantissaDigits } else if pp.MaxFracDigits > 0 && r.MinFracDigits == 0 { // With pictures like ".#e0", rounding can carry into the integer part; // keep one fractional digit in that case. - r.MinFracDigits = 1 + r.MinFracDigits = minimumMantissaDigits } } @@ -909,25 +978,25 @@ func formatExponentFloat(f float64, pp ParsedPicture, df DecimalFormat) string { // Normalize so mantissa is in [0.1, 1) — first significant digit // is the first fractional digit for abs >= 1 { - abs /= 10 + abs /= decimalRadix exp++ } - for abs > 0 && abs < 0.1 { - abs *= 10 + for abs > 0 && abs < minimumFractionalMantissa { + abs *= decimalRadix exp-- } } else { - upperBound := math.Pow(10, float64(m)) - lowerBound := math.Pow(10, float64(m-1)) + upperBound := math.Pow(decimalRadix, float64(m)) + lowerBound := math.Pow(decimalRadix, float64(m-1)) for abs >= upperBound { - abs /= 10 + abs /= decimalRadix exp++ } for abs < lowerBound { - abs *= 10 + abs *= decimalRadix exp-- } } @@ -944,9 +1013,9 @@ func formatExponentPrecise(r *big.Rat, pp ParsedPicture, df DecimalFormat) strin } if pp.IsPercent { - r = new(big.Rat).Mul(r, new(big.Rat).SetInt64(100)) + r = new(big.Rat).Mul(r, new(big.Rat).SetInt64(percentScale)) } else if pp.IsPerMille { - r = new(big.Rat).Mul(r, new(big.Rat).SetInt64(1000)) + r = new(big.Rat).Mul(r, new(big.Rat).SetInt64(perMilleScale)) } m := exponentScalingFactor(pp) @@ -954,10 +1023,10 @@ func formatExponentPrecise(r *big.Rat, pp ParsedPicture, df DecimalFormat) strin // Compute exponent by normalizing the value exp := 0 abs := new(big.Rat).Abs(r) - ten := new(big.Rat).SetInt64(10) + ten := new(big.Rat).SetInt64(decimalRadix) one := new(big.Rat).SetInt64(1) - tenth := new(big.Rat).SetFrac64(1, 10) + tenth := new(big.Rat).SetFrac64(1, decimalRadix) if m == 0 { // Normalize so mantissa is in [0.1, 1) @@ -973,9 +1042,9 @@ func formatExponentPrecise(r *big.Rat, pp ParsedPicture, df DecimalFormat) strin exp-- } } else { - threshold := new(big.Rat).SetInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(m)), nil)) + threshold := new(big.Rat).SetInt(new(big.Int).Exp(big.NewInt(decimalRadix), big.NewInt(int64(m)), nil)) - lowerBound := new(big.Rat).SetInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(m-1)), nil)) + lowerBound := new(big.Rat).SetInt(new(big.Int).Exp(big.NewInt(decimalRadix), big.NewInt(int64(m-1)), nil)) if m == 1 { lowerBound = one } @@ -1012,7 +1081,7 @@ func joinExponent(mantissa string, exp int, pp ParsedPicture, df DecimalFormat) expSep := df.ExponentSeparator if expSep == 0 { - expSep = 'e' + expSep = defaultExponentSeparator } return mantissa + string(expSep) + formatExponentDigits(exp, pp.MinExpDigits, df) @@ -1031,7 +1100,7 @@ func formatExponentDigits(exp, minDigits int, df DecimalFormat) string { digits := big.NewInt(int64(exp)).String() if padding := minDigits - len(digits); padding > 0 { - digits = strings.Repeat("0", padding) + digits + digits = strings.Repeat(decimalZero, padding) + digits } s.WriteString(localizeDigits(digits, df.ZeroDigit)) @@ -1040,11 +1109,11 @@ func formatExponentDigits(exp, minDigits int, df DecimalFormat) string { } func isFinite(f float64) bool { - return !math.IsNaN(f) && !math.IsInf(f, 0) + return !math.IsNaN(f) && !math.IsInf(f, anyInfinitySign) } func localizeDigits(s string, zeroDigit rune) string { - if s == "" || zeroDigit == '0' { + if s == "" || zeroDigit == asciiZeroDigit { return s } @@ -1052,8 +1121,8 @@ func localizeDigits(s string, zeroDigit rune) string { b.Grow(len(s)) for _, r := range s { - if r >= '0' && r <= '9' { - b.WriteRune(zeroDigit + (r - '0')) + if r >= asciiZeroDigit && r <= asciiLastDigit { + b.WriteRune(zeroDigit + (r - asciiZeroDigit)) continue } diff --git a/transform/internal/xpath/number/format_number_test.go b/transform/internal/xpath/number/format_number_test.go index a441b45..084a692 100644 --- a/transform/internal/xpath/number/format_number_test.go +++ b/transform/internal/xpath/number/format_number_test.go @@ -100,6 +100,121 @@ func TestExactRoundingBoundariesAndOwnership(t *testing.T) { } } +func TestDecimalDigitsAndWideRounding(t *testing.T) { + t.Parallel() + + for _, test := range []struct { + value string + want int + }{{"42", 0}, {"1/8", 3}, {"1/40", 3}, {"1/125", 3}, {"1/3", math.MaxInt}, {"1/7", math.MaxInt}, {"1/9", math.MaxInt}} { + value, ok := new(big.Rat).SetString(test.value) + if !ok { + t.Fatal(test.value) + } + if got := number.RatDecimalDigits(value); got != test.want { + t.Errorf("decimal digits(%s) = %d; want %d", value, got, test.want) + } + } + for _, test := range []struct { + value, want string + precision int + }{ + {"1/7", "0.1429", 4}, + {"1/9", "0.1111", 4}, + {"0." + strings.Repeat("0", 1000) + "59", "0." + strings.Repeat("0", 1000) + "6", 1001}, + {"-0." + strings.Repeat("0", 1000) + "59", "-0." + strings.Repeat("0", 1000) + "6", 1001}, + {"125" + strings.Repeat("0", 1001), "12" + strings.Repeat("0", 1002), -1002}, + {"42", "0", math.MinInt}, + } { + value, ok := new(big.Rat).SetString(test.value) + if !ok { + t.Fatal(test.value) + } + want, ok := new(big.Rat).SetString(test.want) + if !ok { + t.Fatal(test.want) + } + if got := number.RatRoundHalfToEven(value, test.precision); got.Cmp(want) != 0 { + t.Errorf("precision %d: got %s; want %s", test.precision, got, want) + } + } +} + +func TestIntegerHalfEvenRoundingDistanceAndOwnership(t *testing.T) { + t.Parallel() + + check := func(value *big.Rat) { + t.Helper() + + before := value.RatString() + got := number.RatRoundHalfToEvenInt(value) + distance := new(big.Rat).Sub(value, new(big.Rat).SetInt(got)) + distance.Abs(distance) + comparison := distance.Cmp(big.NewRat(1, 2)) + if comparison > 0 || comparison == 0 && got.Bit(0) != 0 { + t.Fatalf("rounded %s to %s, distance %s; want nearest integer with even ties", value, got, distance) + } + got.SetInt64(999) + if value.RatString() != before { + t.Fatal("rounding changed or aliased its source", before, value) + } + } + for numerator := int64(-64); numerator <= 64; numerator++ { + for denominator := int64(1); denominator <= 33; denominator++ { + check(big.NewRat(numerator, denominator)) + } + } + // Exercise multiword division around half, with even/odd quotients and + // both signs. The distance/parity oracle does not perform quotient rounding. + denominator := new(big.Int).Lsh(big.NewInt(1), 128) + for _, odd := range []int64{0, 1} { + quotient := new(big.Int).Lsh(big.NewInt(1), 256) + quotient.Add(quotient, big.NewInt(odd)) + for _, offset := range []int64{-1, 0, 1} { + numerator := new(big.Int).Mul(quotient, denominator) + remainder := new(big.Int).Rsh(new(big.Int).Set(denominator), 1) + remainder.Add(remainder, big.NewInt(offset)) + numerator.Add(numerator, remainder) + value := new(big.Rat).SetFrac(numerator, denominator) + check(value) + check(new(big.Rat).Neg(value)) + } + } +} + +func TestScaledHalfEvenRoundingDistanceAndOwnership(t *testing.T) { + t.Parallel() + + for precision := -3; precision <= 3; precision++ { + exponent := precision + if exponent < 0 { + exponent = -exponent + } + quantum := new(big.Rat).SetInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(exponent)), nil)) + if precision > 0 { + quantum.Inv(quantum) + } + for numerator := int64(-24); numerator <= 24; numerator++ { + for denominator := int64(1); denominator <= 17; denominator++ { + value := big.NewRat(numerator, denominator) + before := value.RatString() + got := number.RatRoundHalfToEven(value, precision) + units := new(big.Rat).Quo(got, quantum) + distance := new(big.Rat).Sub(value, got) + distance.Quo(distance.Abs(distance), quantum) + comparison := distance.Cmp(big.NewRat(1, 2)) + if !units.IsInt() || comparison > 0 || comparison == 0 && units.Num().Bit(0) != 0 { + t.Fatalf("precision %d: rounded %s to %s, distance %s units; want nearest with even ties", precision, value, got, distance) + } + got.SetInt64(999) + if value.RatString() != before { + t.Fatal("scaled rounding changed or aliased its source", before, value) + } + } + } + } +} + func TestScientificAndFractionGroupingPictures(t *testing.T) { t.Parallel() for _, tc := range []struct{ value, picture, want string }{ @@ -142,6 +257,13 @@ func TestFractionLongDivisionOwnership(t *testing.T) { {"1/7", strings.Repeat("142857", 21) + "14", 128, 128}, {"1/8", "125", 0, 128}, {"1/8", "12500", 5, 5}, + {"1/8", "125", 5, 3}, + {"1/8", "12", 0, 2}, + {"1/8", "", 0, 0}, + {"1/1024", "00097", 0, 5}, + {"1/1024", "", 0, 2}, + {"1/1024", "00", 2, 2}, + {"99/100", "9", 0, 1}, {"0", "000", 3, 128}, } { r, ok := new(big.Rat).SetString(tc.value) @@ -154,6 +276,30 @@ func TestFractionLongDivisionOwnership(t *testing.T) { t.Fatalf("%s: %q, source %s; want %q", tc.value, got, r.RatString(), tc.want) } } + df.ZeroDigit = '٠' + if got := number.FormatRatFrac(big.NewRat(1, 8), 5, 5, df); got != "١٢٥٠٠" { + t.Fatal("localized fractional digits", got) + } +} + +func BenchmarkFractionFormattingWidth(b *testing.B) { + for _, width := range []int{3, 128, 4096} { + for _, test := range []struct { + name string + value *big.Rat + }{ + {"RoundedSeventh", number.RatRoundHalfToEven(big.NewRat(1, 7), width)}, + {"LeadingZeros", big.NewRat(1, 1024)}, + {"TrailingZeros", big.NewRat(1, 8)}, + } { + b.Run(test.name+"/"+strconv.Itoa(width), func(b *testing.B) { + b.ReportAllocs() + for b.Loop() { + _ = number.FormatRatFrac(test.value, min(3, width), width, number.DefaultDecimalFormat()) + } + }) + } + } } func TestWideIntegerPictures(t *testing.T) { diff --git a/transform/internal/xpath/parse_ietf_date.go b/transform/internal/xpath/parse_ietf_date.go index bdc6f55..01073a7 100644 --- a/transform/internal/xpath/parse_ietf_date.go +++ b/transform/internal/xpath/parse_ietf_date.go @@ -11,20 +11,34 @@ import ( "time" "unicode" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/xmlchar" "github.com/skarm/xmlsec/transform/internal/xdm" ) +// IETF field widths count ASCII decimal digits; the end-of-day hour and minute maximum are shared by actual clock/timezone helpers. +const ( + ietfMinimumComponentDigits = 1 + ietfFixedClockDigits = 2 + ietfEndOfDayHour = 24 + ietfMaximumMinute = 59 +) + +// Fixed-offset locations deliberately carry numeric offsets without retaining input abbreviations. +const ietfFixedTimezoneName = "" + func lookupParseIETFDate(name string) (*builtinFunc, bool) { switch name { - case "parse-ietf-date": - return &builtinFunc{name: name, min: 1, max: 1, fn: fnParseIETFDate}, true + case fnNameParseIETFDate: + return &builtinFunc{name: name, min: builtinArityOne, max: builtinArityOne, fn: fnParseIETFDate}, true } return nil, false } func fnParseIETFDate(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { + const errCodeFORG0010 = "FORG0010" + // xs:string? value: an atomized-empty argument (empty array / nilled node) // returns the empty sequence, like a syntactically empty one. s, empty, err := coerceAtomizedString(ctx, args[0]) @@ -39,7 +53,7 @@ func fnParseIETFDate(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, er t, err := parseIETFDate(s) if err != nil { return nil, &xdm.Error{ - Code: "FORG0010", + Code: errCodeFORG0010, Message: fmt.Sprintf("fn:parse-ietf-date: invalid date string: %s", err), } } @@ -57,7 +71,7 @@ func fnParseIETFDate(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, er // - asctime: "Wed Jun 06 11:54:45 EST 2013" // - Various with optional day-of-week, flexible separators func parseIETFDate(s string) (time.Time, error) { - s = strings.Trim(s, " \t\r\n") + s = strings.Trim(s, lexicon.XMLWhitespace) if s == "" { return time.Time{}, fmt.Errorf("empty date string") } @@ -102,7 +116,7 @@ func (p *ietfDateParser) parse() (time.Time, error) { // parseRFC parses: day [-] month [-] year time [timezone] func (p *ietfDateParser) parseRFC() (time.Time, error) { - day, err := p.readIntN(1) + day, err := p.readIntN(ietfMinimumComponentDigits) if err != nil { return time.Time{}, fmt.Errorf("expected day (1-2 digits): %w", err) } @@ -169,7 +183,7 @@ func (p *ietfDateParser) parseAsctime() (time.Time, error) { p.skipSep() - day, err := p.readIntN(1) + day, err := p.readIntN(ietfMinimumComponentDigits) if err != nil { return time.Time{}, fmt.Errorf("expected day (1-2 digits): %w", err) } @@ -216,6 +230,8 @@ func (p *ietfDateParser) parseAsctime() (time.Time, error) { } func (p *ietfDateParser) finishDate(year, month, day, hour, minute, sec int, frac float64, loc *time.Location) (time.Time, error) { + const ietfMidnightHour = 0 // 24:00 normalizes to midnight on the next civil day + p.skipWS() if p.pos < len(p.input) { @@ -226,10 +242,10 @@ func (p *ietfDateParser) finishDate(year, month, day, hour, minute, sec int, fra return time.Time{}, err } - ns := int(frac * 1e9) + ns := int(frac * float64(nanosecondsPerSecond)) - if hour == 24 { - hour = 0 + if hour == ietfEndOfDayHour { + hour = ietfMidnightHour // advance to next day t := time.Date(year, time.Month(month), day+1, hour, minute, sec, ns, loc) @@ -242,7 +258,7 @@ func (p *ietfDateParser) finishDate(year, month, day, hour, minute, sec int, fra } func (p *ietfDateParser) parseTime() (hour, minute, sec int, frac float64, err error) { - hour, err = p.readIntN(1) + hour, err = p.readIntN(ietfMinimumComponentDigits) if err != nil { return 0, 0, 0, 0, fmt.Errorf("expected hour (1-2 digits): %w", err) } @@ -253,7 +269,7 @@ func (p *ietfDateParser) parseTime() (hour, minute, sec int, frac float64, err e p.pos++ // skip ':' - minute, err = p.readIntN(2) + minute, err = p.readIntN(ietfFixedClockDigits) if err != nil { return 0, 0, 0, 0, fmt.Errorf("expected minute (exactly 2 digits): %w", err) } @@ -262,7 +278,7 @@ func (p *ietfDateParser) parseTime() (hour, minute, sec int, frac float64, err e if p.pos < len(p.input) && p.input[p.pos] == ':' { p.pos++ // skip ':' - sec, err = p.readIntN(2) + sec, err = p.readIntN(ietfFixedClockDigits) if err != nil { return 0, 0, 0, 0, fmt.Errorf("expected second (exactly 2 digits): %w", err) } @@ -279,7 +295,7 @@ func (p *ietfDateParser) parseTime() (hour, minute, sec int, frac float64, err e return 0, 0, 0, 0, fmt.Errorf("expected digits after decimal point") } - frac, _ = strconv.ParseFloat("0."+p.input[fracStart:p.pos], 64) + frac, _ = strconv.ParseFloat("0."+p.input[fracStart:p.pos], doubleFloatBits) } } @@ -327,7 +343,7 @@ func (p *ietfDateParser) skipComment() error { return fmt.Errorf("unclosed comment at position %d", start) } // The suffix is a timezone name, not arbitrary comment text. - content := strings.Trim(p.input[contentStart:p.pos-1], " \t\r\n") + content := strings.Trim(p.input[contentStart:p.pos-1], lexicon.XMLWhitespace) if _, ok := lookupIETFTimezone(strings.ToUpper(content)); !ok { return fmt.Errorf("invalid timezone-name suffix") } @@ -451,6 +467,13 @@ func (p *ietfDateParser) skipSep() { // readYear applies the century adjustment only to a two-digit year. func (p *ietfDateParser) readYear() (int, error) { + // Only a two-digit lexical year receives the fixed century adjustment. + const ( + ietfShortYearDigits = 2 + ietfFullYearDigits = 4 + ietfShortYearCentury = 1900 + ) + start := p.pos for p.pos < len(p.input) && p.input[p.pos] >= '0' && p.input[p.pos] <= '9' { p.pos++ @@ -461,7 +484,7 @@ func (p *ietfDateParser) readYear() (int, error) { return 0, fmt.Errorf("expected year at position %d", start) } - if n != 2 && n != 4 { + if n != ietfShortYearDigits && n != ietfFullYearDigits { return 0, fmt.Errorf("year must have 2 or 4 digits") } @@ -470,8 +493,8 @@ func (p *ietfDateParser) readYear() (int, error) { return 0, err } - if n == 2 { - year += 1900 + if n == ietfShortYearDigits { + year += ietfShortYearCentury } return year, nil @@ -493,7 +516,7 @@ func (p *ietfDateParser) parseNamedTimezone() (*time.Location, error) { return nil, fmt.Errorf("timezone name followed by digits without separator") } - return time.FixedZone("", offset), nil + return time.FixedZone(ietfFixedTimezoneName, offset), nil } // Unknown timezone name — reject p.pos = saved @@ -502,6 +525,17 @@ func (p *ietfDateParser) parseNamedTimezone() (*time.Location, error) { } func (p *ietfDateParser) parseNumericTimezone() (*time.Location, error) { + // ASCII timezone field widths are byte counts. The hour ceiling is separate + // from the minute-component range; 14 hours requires a zero minute field. + const ( + ietfTimezoneHourMinDigits = 1 + ietfTimezoneHourMaxDigits = 2 + ietfTimezoneMinuteDigits = 2 + ietfCompactHMMWidth = 3 + ietfCompactHHMMWidth = 4 + ietfMaximumTimezoneHours = 14 + ) + sign := 1 if p.input[p.pos] == '-' { sign = -1 @@ -520,13 +554,13 @@ func (p *ietfDateParser) parseNumericTimezone() (*time.Location, error) { var h, m int if hPart, mPart, ok := strings.Cut(offsetStr, ":"); ok { - if len(hPart) < 1 || len(hPart) > 2 { + if len(hPart) < ietfTimezoneHourMinDigits || len(hPart) > ietfTimezoneHourMaxDigits { return nil, fmt.Errorf("invalid timezone offset hours") } h, _ = strconv.Atoi(hPart) - if len(mPart) != 0 && len(mPart) != 2 { + if len(mPart) != 0 && len(mPart) != ietfTimezoneMinuteDigits { return nil, fmt.Errorf("timezone offset minutes must be 2 digits, got %d", len(mPart)) } @@ -536,34 +570,34 @@ func (p *ietfDateParser) parseNumericTimezone() (*time.Location, error) { } else { // Compact format without colon: H, HH, HMM, HHMM switch len(offsetStr) { - case 1, 2: + case ietfTimezoneHourMinDigits, ietfTimezoneHourMaxDigits: h, _ = strconv.Atoi(offsetStr) - case 3: - h, _ = strconv.Atoi(offsetStr[:1]) - m, _ = strconv.Atoi(offsetStr[1:3]) - case 4: - h, _ = strconv.Atoi(offsetStr[:2]) - m, _ = strconv.Atoi(offsetStr[2:4]) + case ietfCompactHMMWidth: + h, _ = strconv.Atoi(offsetStr[:ietfTimezoneHourMinDigits]) + m, _ = strconv.Atoi(offsetStr[ietfTimezoneHourMinDigits:ietfCompactHMMWidth]) + case ietfCompactHHMMWidth: + h, _ = strconv.Atoi(offsetStr[:ietfTimezoneHourMaxDigits]) + m, _ = strconv.Atoi(offsetStr[ietfTimezoneHourMaxDigits:ietfCompactHHMMWidth]) default: return nil, fmt.Errorf("invalid timezone offset: %q", offsetStr) } } - if h > 14 || (h == 14 && m > 0) { + if h > ietfMaximumTimezoneHours || (h == ietfMaximumTimezoneHours && m > 0) { return nil, fmt.Errorf("timezone offset hours out of range: %d", h) } - if m > 59 { + if m > ietfMaximumMinute { return nil, fmt.Errorf("timezone offset minutes out of range: %d", m) } - offset := sign * (h*3600 + m*60) + offset := sign * (h*calendarSecondsPerHour + m*calendarSecondsPerMinute) // A numeric offset may have an optional timezone-name suffix. if err := p.skipComment(); err != nil { return nil, err } - return time.FixedZone("", offset), nil + return time.FixedZone(ietfFixedTimezoneName, offset), nil } func dayOfWeekNames(key string) bool { @@ -576,7 +610,9 @@ func dayOfWeekNames(key string) bool { } func parseMonth(s string) (int, error) { - if len(s) != 3 { + const ietfMonthAbbreviationBytes = 3 // grammar uses ASCII three-letter month names + + if len(s) != ietfMonthAbbreviationBytes { return 0, fmt.Errorf("month abbreviation must be exactly 3 letters: %q", s) } @@ -589,11 +625,16 @@ func parseMonth(s string) (int, error) { } func validateIETFDate(year, month, day, hour, minute, sec int) error { - if month < 1 || month > 12 { + const ( + ietfMinimumCalendarDay = 1 + ietfMaximumLeapSecond = 60 // seconds permit a leap second, unlike minutes + ) + + if month < int(time.January) || month > int(time.December) { return fmt.Errorf("month %d out of range", month) } - if day < 1 { + if day < ietfMinimumCalendarDay { return fmt.Errorf("day %d out of range", day) } @@ -602,15 +643,15 @@ func validateIETFDate(year, month, day, hour, minute, sec int) error { return fmt.Errorf("day %d out of range for month %d", day, month) } - if hour > 24 || (hour == 24 && (minute != 0 || sec != 0)) { + if hour > ietfEndOfDayHour || (hour == ietfEndOfDayHour && (minute != 0 || sec != 0)) { return fmt.Errorf("hour %d out of range", hour) } - if minute < 0 || minute > 59 { + if minute < 0 || minute > ietfMaximumMinute { return fmt.Errorf("minute %d out of range", minute) } - if sec < 0 || sec > 60 { // 60 for leap seconds + if sec < 0 || sec > ietfMaximumLeapSecond { // 60 for leap seconds return fmt.Errorf("second %d out of range", sec) } @@ -618,48 +659,56 @@ func validateIETFDate(year, month, day, hour, minute, sec int) error { } func daysInMonth(year, month int) int { + // Gregorian month lengths are calendar-day counts, not elapsed durations. + const ( + ietfLongMonthDays = 31 + ietfShortMonthDays = 30 + ietfLeapFebruaryDays = 29 + ietfCommonFebruaryDays = 28 + ) + switch month { - case 1, 3, 5, 7, 8, 10, 12: - return 31 - case 4, 6, 9, 11: - return 30 - case 2: - if year%4 == 0 && (year%100 != 0 || year%400 == 0) { - return 29 + case int(time.January), int(time.March), int(time.May), int(time.July), int(time.August), int(time.October), int(time.December): + return ietfLongMonthDays + case int(time.April), int(time.June), int(time.September), int(time.November): + return ietfShortMonthDays + case int(time.February): + if year%gregorianLeapInterval == 0 && (year%gregorianCenturyYears != 0 || year%gregorianCycleYears == 0) { + return ietfLeapFebruaryDays } - return 28 + return ietfCommonFebruaryDays } - return 31 + return ietfLongMonthDays } func lookupMonthNumber(key string) (int, bool) { switch key { case "jan": - return 1, true + return int(time.January), true case "feb": - return 2, true + return int(time.February), true case "mar": - return 3, true + return int(time.March), true case "apr": - return 4, true + return int(time.April), true case "may": - return 5, true + return int(time.May), true case "jun": - return 6, true + return int(time.June), true case "jul": - return 7, true + return int(time.July), true case "aug": - return 8, true + return int(time.August), true case "sep": - return 9, true + return int(time.September), true case "oct": - return 10, true + return int(time.October), true case "nov": - return 11, true + return int(time.November), true case "dec": - return 12, true + return int(time.December), true } var zero int @@ -674,15 +723,15 @@ func lookupIETFTimezone(key string) (int, bool) { case "UT", "UTC", "GMT", "Z": return 0, true case "EST", "CDT": - return -5 * 3600, true + return -5 * calendarSecondsPerHour, true case "EDT": - return -4 * 3600, true + return -4 * calendarSecondsPerHour, true case "CST", "MDT": - return -6 * 3600, true + return -6 * calendarSecondsPerHour, true case "MST", "PDT": - return -7 * 3600, true + return -7 * calendarSecondsPerHour, true case "PST": - return -8 * 3600, true + return -8 * calendarSecondsPerHour, true } var zero int diff --git a/transform/internal/xpath/parser.go b/transform/internal/xpath/parser.go index 7ccd89d..94e3ab3 100644 --- a/transform/internal/xpath/parser.go +++ b/transform/internal/xpath/parser.go @@ -15,6 +15,8 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) +// maxParseDepth caps parser expression/item-type nesting; it is separate from +// the invocation evaluator recursion policy. const maxParseDepth = 200 // Parse parses an XPath 3.1 expression string into an AST. @@ -527,7 +529,7 @@ func (p *parseOptions) parseArrowTarget() (Expr, string, string, error) { p.lexer.Next() // consume ':' localTok := p.lexer.Next() - if !isNameLikeToken(localTok.Type) { + if localTok.SpaceBefore || !isNameLikeToken(localTok.Type) { return nil, "", "", fmt.Errorf("%w: name after '%s:' in arrow but got %s", ErrExpectedToken, name, localTok) } @@ -833,7 +835,7 @@ func (p *parseOptions) parsePrimaryExpr() (Expr, error) { case TokenFunction: // nameFunction followed by "(" is an inline function or function test. // Otherwise it's a name test (e.g., child::function or xs:QName(function)). - if (p.version == 0 || p.version >= 30) && p.lexer.PeekAt(1).Type == TokenLParen { + if (p.version == xpathVersionDefault || p.version >= xpathVersion30) && p.lexer.PeekAt(1).Type == TokenLParen { return p.parseFunctionKeyword() } // Treat as a regular name (name test / path step). @@ -1229,9 +1231,10 @@ func (p *parseOptions) parseStep() (Step, error) { } return Step{ - Axis: axis, - NodeTest: nodeTest, - Predicates: predicates, + Axis: axis, + ExplicitAxis: axisSpecified, + NodeTest: nodeTest, + Predicates: predicates, }, nil } @@ -1281,7 +1284,7 @@ func (p *parseOptions) parseNodeTest(_ AxisType) (xdm.NodeTest, error) { return nil, &xdm.Error{Code: errCodeXPST0081, Message: "the xmlns namespace URI cannot be used in name tests"} } - return xdm.NameTest{URI: uri, Local: local}, nil + return xdm.NameTest{URI: uri, Local: local, URIQualified: true}, nil } } @@ -1300,7 +1303,7 @@ func (p *parseOptions) parseNodeTest(_ AxisType) (xdm.NodeTest, error) { // Returns (result, true, nil) on success, (nil, false, nil) when name is not a kind test keyword. func (p *parseOptions) parseKindTest(name string) (xdm.NodeTest, bool, error) { switch name { - case "node": + case lexicon.KindNode: p.lexer.Next() // consume '(' if err := p.expectToken(TokenRParen); err != nil { @@ -1308,7 +1311,7 @@ func (p *parseOptions) parseKindTest(name string) (xdm.NodeTest, bool, error) { } return xdm.TypeTest{Kind: xdm.NodeKindNode}, true, nil - case "text": + case lexicon.KindText: p.lexer.Next() if err := p.expectToken(TokenRParen); err != nil { @@ -1316,7 +1319,7 @@ func (p *parseOptions) parseKindTest(name string) (xdm.NodeTest, bool, error) { } return xdm.TypeTest{Kind: xdm.NodeKindText}, true, nil - case "comment": + case lexicon.KindComment: p.lexer.Next() if err := p.expectToken(TokenRParen); err != nil { @@ -1341,7 +1344,7 @@ func (p *parseOptions) parseKindTest(name string) (xdm.NodeTest, bool, error) { test := xdm.PITest{Target: target, HasTarget: hasTarget} - if p.version != 10 { + if p.version != xpathVersion10 { var err error test, err = normalizePITest(test) @@ -1355,13 +1358,13 @@ func (p *parseOptions) parseKindTest(name string) (xdm.NodeTest, bool, error) { return p.parseElementOrAttributeTest(true) case lexicon.KindAttribute: return p.parseElementOrAttributeTest(false) - case "document-node": + case lexicon.KindDocumentNode: return p.parseDocumentNodeTest() case lexicon.KindSchemaElement: return p.parseSchemaTest(true) - case "schema-attribute": + case lexicon.KindSchemaAttribute: return p.parseSchemaTest(false) - case "namespace-node": + case lexicon.KindNamespaceNode: p.lexer.Next() if err := p.expectToken(TokenRParen); err != nil { @@ -1369,7 +1372,7 @@ func (p *parseOptions) parseKindTest(name string) (xdm.NodeTest, bool, error) { } return xdm.NamespaceNodeTest{}, true, nil - case "item": + case lexicon.KindItem: p.lexer.Next() if err := p.expectToken(TokenRParen); err != nil { @@ -1382,7 +1385,7 @@ func (p *parseOptions) parseKindTest(name string) (xdm.NodeTest, bool, error) { return nil, false, nil } -// parseElementOrAttributeTest parses element(name?, type?) or attribute(name?, type?). +// parseElementOrAttributeTest parses element and attribute kind tests. func (p *parseOptions) parseElementOrAttributeTest(isElement bool) (xdm.NodeTest, bool, error) { p.lexer.Next() // consume '(' @@ -1395,13 +1398,20 @@ func (p *parseOptions) parseElementOrAttributeTest(isElement bool) (xdm.NodeTest p.lexer.Next() name = "*" - } else if p.lexer.Peek().Type == TokenName { + } else if isNameLikeToken(p.lexer.Peek().Type) { name = p.scanQName() + } else { + return nil, true, fmt.Errorf("%w: name or '*' in kind test", ErrExpectedToken) } if p.lexer.Peek().Type == TokenComma { p.lexer.Next() + typeName = p.scanQName() + if typeName == "" { + return nil, true, fmt.Errorf("%w: type name after ',' in kind test", ErrExpectedToken) + } + // element(name, type?) — trailing ? means nillable if isElement && p.lexer.Peek().Type == TokenQMark { p.lexer.Next() @@ -1531,97 +1541,6 @@ func (p *parseOptions) parsePredicate() (Expr, error) { return expr, nil } -func isGeneralComp(t TokenType) bool { - return t == TokenEquals || t == TokenNotEquals || - t == TokenLess || t == TokenLessEq || - t == TokenGreater || t == TokenGreaterEq -} - -func isValueComp(t TokenType) bool { - return t == TokenEq || t == TokenNe || - t == TokenLt || t == TokenLe || - t == TokenGt || t == TokenGe -} - -func isNodeComp(t TokenType) bool { - return t == TokenIs || t == TokenNodePre || t == TokenNodeFol -} - -// parseNumberLiteral converts a numeric token value to a LiteralExpr. -// Per XPath 3.1: no dot and no e/E → xs:integer (*big.Int), -// dot but no e/E → xs:decimal (*big.Rat), -// e/E → xs:double (float64). -func parseNumberLiteral(s string) (LiteralExpr, error) { - hasE := strings.ContainsAny(s, "eE") - - hasDot := strings.Contains(s, ".") - if !hasDot && !hasE { - // Integer literal → *big.Int (arbitrary precision) - v, ok := new(big.Int).SetString(s, 10) - if !ok { - return LiteralExpr{}, fmt.Errorf("%w: invalid integer %q", ErrUnexpectedToken, s) - } - - return LiteralExpr{Value: v}, nil - } - - if !hasE { - // Decimal literal → *big.Rat (exact rational) - v, ok := new(big.Rat).SetString(s) - if !ok { - return LiteralExpr{}, fmt.Errorf("%w: invalid decimal %q", ErrUnexpectedToken, s) - } - - return LiteralExpr{Value: v}, nil - } - // Double literal → float64 - v, err := parseXPathDouble(s) - if err != nil { - return LiteralExpr{}, fmt.Errorf("%w: invalid number %q: %w", ErrUnexpectedToken, s, err) - } - - return LiteralExpr{Value: v}, nil -} - -// tokenAsNCName returns the NCName string for a token that can be used as an -// NCName in lookup keys and other non-keyword contexts. Returns "" if the -// token is not a valid NCName. -func tokenAsNCName(tok Token) string { - switch tok.Type { - case TokenName: - // Reject URIQualifiedNames (Q{uri}local) — they are not NCNames - if strings.HasPrefix(tok.Value, "Q{") { - return "" - } - // Reject prefixed names (prefix:local) — they are QNames, not NCNames - if strings.Contains(tok.Value, ":") { - return "" - } - - return tok.Value - case TokenDiv, TokenMod, TokenAnd, TokenOr, TokenReturn, TokenElse, TokenEq, TokenNe, TokenLt, TokenLe, TokenGt, TokenGe, TokenIdiv, TokenIf, TokenThen, TokenFor, TokenLet, TokenIn, TokenSome, TokenEvery, TokenSatisfies, TokenIs, TokenTo, TokenUnion, TokenIntersect, TokenExcept, TokenInstanceOf, TokenTreatAs, TokenCastableAs, TokenCastAs, TokenAs, TokenOf: - return tok.Type.String() - } - - return "" -} - -// XPath 2.0 erratum XP.E19 and 3.x kind tests compare normalized PI targets. -// An explicitly empty or otherwise invalid NCName is XPTY0004, not a syntax -// error; an absent target continues to match every processing instruction. -func normalizePITest(test xdm.PITest) (xdm.PITest, error) { - if !test.HasTarget { - return test, nil - } - - test.Target = collapseXMLWhitespace(test.Target) - if !xmlchar.IsValidNCName(test.Target) { - return test, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "processing-instruction target must be an NCName"} - } - - return test, nil -} - // --- XPath 3.1 Expression Extensions --- // parseFLWOR parses for/let expressions (XPath 3.1 simple for/let only). @@ -2082,7 +2001,7 @@ func (p *parseOptions) parseInlineFunctionParams() ([]FunctionParam, error) { seen := make(map[string]struct{}, len(params)) for _, param := range params { if _, ok := seen[param.Name]; ok { - return nil, &xdm.Error{Code: "XQST0039", Message: fmt.Sprintf("duplicate parameter name $%s in inline function", param.Name)} + return nil, &xdm.Error{Code: errCodeXQST0039, Message: fmt.Sprintf("duplicate parameter name $%s in inline function", param.Name)} } seen[param.Name] = struct{}{} @@ -2392,12 +2311,11 @@ func (p *parseOptions) parseFunctionItemType() (xdm.NodeTest, error) { } returnType = rt - } else if len(paramTypes) > 0 { - // Per XPath 3.1 §3.1.5.4: when parameter types are present, - // "as SequenceType" is required. + } else { + // TypedFunctionTest requires a return type at every arity. return nil, &xdm.Error{ Code: lexicon.ErrXPST0003, - Message: "function type with parameter types requires 'as ReturnType'", + Message: "function type requires 'as ReturnType'", } } @@ -2512,7 +2430,7 @@ func (p *parseOptions) parseSingleType() (AtomicTypeName, bool, error) { // scanQName scans a possibly-prefixed QName (prefix:local) from the token stream. func (p *parseOptions) scanQName() string { tok := p.lexer.Peek() - if tok.Type != TokenName { + if !isNameLikeToken(tok.Type) { return "" } @@ -2571,12 +2489,12 @@ func (p *parseOptions) looksLikeStepOffset(offset int) bool { // element(), attribute(), document-node(), schema-element(), // schema-attribute(), namespace-node() are steps switch tok.Value { - case "node", "text", "comment", nameProcessingInstruction, - lexicon.KindElement, "attribute", "document-node", lexicon.KindSchemaElement, - "schema-attribute": + case lexicon.KindNode, lexicon.KindText, lexicon.KindComment, nameProcessingInstruction, + lexicon.KindElement, lexicon.KindAttribute, lexicon.KindDocumentNode, lexicon.KindSchemaElement, + lexicon.KindSchemaAttribute: return true - case "namespace-node": - return p.version == 0 || p.version >= 30 + case lexicon.KindNamespaceNode: + return p.version == xpathVersionDefault || p.version >= xpathVersion30 } return false // function call @@ -2627,6 +2545,97 @@ func (p *parseOptions) joinedQNameAt(offset int) bool { return colon.Type == TokenColon && !colon.SpaceBefore && !p.lexer.PeekAt(offset+1).SpaceBefore } +func isGeneralComp(t TokenType) bool { + return t == TokenEquals || t == TokenNotEquals || + t == TokenLess || t == TokenLessEq || + t == TokenGreater || t == TokenGreaterEq +} + +func isValueComp(t TokenType) bool { + return t == TokenEq || t == TokenNe || + t == TokenLt || t == TokenLe || + t == TokenGt || t == TokenGe +} + +func isNodeComp(t TokenType) bool { + return t == TokenIs || t == TokenNodePre || t == TokenNodeFol +} + +// parseNumberLiteral converts a numeric token value to a LiteralExpr. +// Per XPath 3.1: no dot and no e/E → xs:integer (*big.Int), +// dot but no e/E → xs:decimal (*big.Rat), +// e/E → xs:double (float64). +func parseNumberLiteral(s string) (LiteralExpr, error) { + hasE := strings.ContainsAny(s, "eE") + + hasDot := strings.Contains(s, ".") + if !hasDot && !hasE { + // Integer literal → *big.Int (arbitrary precision) + v, ok := new(big.Int).SetString(s, 10) + if !ok { + return LiteralExpr{}, fmt.Errorf("%w: invalid integer %q", ErrUnexpectedToken, s) + } + + return LiteralExpr{Value: v}, nil + } + + if !hasE { + // Decimal literal → *big.Rat (exact rational) + v, ok := new(big.Rat).SetString(s) + if !ok { + return LiteralExpr{}, fmt.Errorf("%w: invalid decimal %q", ErrUnexpectedToken, s) + } + + return LiteralExpr{Value: v}, nil + } + // Double literal → float64 + v, err := parseXPathDouble(s) + if err != nil { + return LiteralExpr{}, fmt.Errorf("%w: invalid number %q: %w", ErrUnexpectedToken, s, err) + } + + return LiteralExpr{Value: v}, nil +} + +// tokenAsNCName returns the NCName string for a token that can be used as an +// NCName in lookup keys and other non-keyword contexts. Returns "" if the +// token is not a valid NCName. +func tokenAsNCName(tok Token) string { + switch tok.Type { + case TokenName: + // Reject URIQualifiedNames (Q{uri}local) — they are not NCNames + if strings.HasPrefix(tok.Value, "Q{") { + return "" + } + // Reject prefixed names (prefix:local) — they are QNames, not NCNames + if strings.Contains(tok.Value, ":") { + return "" + } + + return tok.Value + case TokenDiv, TokenMod, TokenAnd, TokenOr, TokenReturn, TokenElse, TokenEq, TokenNe, TokenLt, TokenLe, TokenGt, TokenGe, TokenIdiv, TokenIf, TokenThen, TokenFor, TokenLet, TokenIn, TokenSome, TokenEvery, TokenSatisfies, TokenIs, TokenTo, TokenUnion, TokenIntersect, TokenExcept, TokenInstanceOf, TokenTreatAs, TokenCastableAs, TokenCastAs, TokenAs, TokenOf: + return tok.Type.String() + } + + return "" +} + +// XPath 2.0 erratum XP.E19 and 3.x kind tests compare normalized PI targets. +// An explicitly empty or otherwise invalid NCName is XPTY0004, not a syntax +// error; an absent target continues to match every processing instruction. +func normalizePITest(test xdm.PITest) (xdm.PITest, error) { + if !test.HasTarget { + return test, nil + } + + test.Target = collapseXMLWhitespace(test.Target) + if !xmlchar.IsValidNCName(test.Target) { + return test, &xdm.Error{Code: lexicon.ErrXPTY0004, Message: "processing-instruction target must be an NCName"} + } + + return test, nil +} + // isKeywordToken returns true if the token type is an XPath keyword // that can still be used as a local name in a QName (e.g., prefix:map). func isKeywordToken(t TokenType) bool { diff --git a/transform/internal/xpath/profile.go b/transform/internal/xpath/profile.go index c0934ad..764f31b 100644 --- a/transform/internal/xpath/profile.go +++ b/transform/internal/xpath/profile.go @@ -15,16 +15,66 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) +// Profile availability tables use standard function local names, with no +// namespace-prefix implication. Keep membership/order independent of dispatch. +const ( + fnNameInnermost = "innermost" + fnNameOutermost = "outermost" + fnNameAnalyzeString = "analyze-string" + fnNameFormatInteger = "format-integer" + fnNameFunctionName = "function-name" + fnNameFunctionArity = "function-arity" + fnNameFunctionLookup = "function-lookup" + fnNameEnvironmentVariable = "environment-variable" + fnNameAvailableEnvironmentVariables = "available-environment-variables" + fnNameURICollection = "uri-collection" + fnNameSerialize = "serialize" + fnNameParseXML = "parse-xml" + fnNameParseXMLFragment = "parse-xml-fragment" + fnNameParseJSON = "parse-json" + fnNameJSONDoc = "json-doc" + fnNameJSONToXML = "json-to-xml" + fnNameXMLToJSON = "xml-to-json" + fnNameTransform = "transform" + fnNameDefaultLanguage = "default-language" + fnNameApply = "apply" + fnNameCollationKey = "collation-key" + fnNameContainsToken = "contains-token" + fnNameParseIETFDate = "parse-ietf-date" + fnNameRandomNumberGenerator = "random-number-generator" + fnNameTokenize = "tokenize" + fnNameData = "data" +) + +// Display labels describe XPath editions, not XML document versions. A negative +// arity denotes an AST node without a function call; the arities below select +// signatures introduced by particular language editions rather than limits. +const ( + xpathLabel10 = "1.0" + xpathLabel20 = "2.0" + xpathLabel30 = "3.0" + xpathLabel31 = "3.1" + unspecifiedFunctionArity = -1 + profileNullaryArity = 0 + profileUnaryArity = 1 + profileBinaryArity = 2 + regexLiteralFlag = "q" + regexNoncapturingPrefix = "(?:" + profileKeywordIf = "if" + profileKeywordTypeSwitch = "typeswitch" + profileKeywordSwitch = "switch" +) + // CheckTypeLanguage applies the same version boundaries to standalone types. func CheckTypeLanguage(st xdm.SequenceType, version int) error { return walkProfile(reflect.ValueOf(st), func(node any) error { switch node.(type) { case xdm.FunctionTest: - if version < 30 { + if version < xpathVersion30 { return profileError(lexicon.ErrXPST0003, "function types", version) } case xdm.MapTest, xdm.ArrayTest: - if version < 31 { + if version < xpathVersion31 { return profileError(lexicon.ErrXPST0003, "map or array types", version) } } @@ -41,8 +91,8 @@ func CheckStaticNamespaces(namespaces map[string]string) error { for prefix, uri := range namespaces { if prefix == lexicon.PrefixXML && uri != lexicon.NamespaceXML || prefix != "" && prefix != lexicon.PrefixXML && uri == lexicon.NamespaceXML || - prefix == "xmlns" || uri == lexicon.NamespaceXMLNS { - return &xdm.Error{Code: "XQST0070", Message: "invalid reserved namespace binding: " + prefix} + prefix == lexicon.PrefixXMLNS || uri == lexicon.NamespaceXMLNS { + return &xdm.Error{Code: errCodeXQST0070, Message: "invalid reserved namespace binding: " + prefix} } } @@ -56,7 +106,7 @@ func CheckStandaloneFunctions(expr *Expression, namespaces map[string]string, ex return walkProfile(reflect.ValueOf(expr.astExpr()), func(node any) error { var prefix, name string - arity := -1 + arity := unspecifiedFunctionArity switch n := node.(type) { case FunctionCall: @@ -71,12 +121,45 @@ func CheckStandaloneFunctions(expr *Expression, namespaces map[string]string, ex }) } +// Language pins the grammar and function library to XPath 1.0, 2.0, 3.0 or 3.1. +// Zero selects the engine default profile. +func (c Compiler) Language(version int, namespaces map[string]string) Compiler { + cfg := compilerCfg{} + if c.cfg != nil { + cfg = *c.cfg + } + + cfg.version, cfg.namespaces = version, maps.Clone(namespaces) + + return Compiler{cfg: &cfg} +} + +// HostFunctions augments the selected library without changing its grammar. +// The host still validates declarations and arities in its own static context. +func (c Compiler) HostFunctions(available func(uri, name string, arity int) bool) Compiler { + cfg := compilerCfg{} + if c.cfg != nil { + cfg = *c.cfg + } + + cfg.hostFunction = available + + return Compiler{cfg: &cfg} +} + +// BuiltinFunctionAvailable reports the selected language's standard function +// library, including version-specific arities, for host introspection. +func BuiltinFunctionAvailable(version int, uri, name string, arity int) bool { + return functionVisible(version, uri, name, arity) && IsBuiltinFunctionNS(uri, name) && + (arity < finiteArityFloor || BuiltinFunctionAcceptsArity(uri, name, arity)) +} + func profileError(code, feature string, version int) error { - return &xdm.Error{Code: code, Message: feature + " is unavailable in XPath " + map[int]string{10: lexicon.XMLVersion10, 20: "2.0", 30: "3.0", 31: "3.1"}[version]} + return &xdm.Error{Code: code, Message: feature + " is unavailable in XPath " + map[int]string{xpathVersion10: xpathLabel10, xpathVersion20: xpathLabel20, xpathVersion30: xpathLabel30, xpathVersion31: xpathLabel31}[version]} } func checkStandaloneCall(version int, prefix, name string, arity int, namespaces map[string]string, extensions []map[string]Function) error { - if arity < 0 { + if arity < finiteArityFloor { return nil } @@ -112,13 +195,13 @@ func checkStandaloneCall(version int, prefix, name string, arity int, namespaces } } - if version == 10 && (prefix != "" || !xpath10FunctionArity(name, arity)) || !StandardFunctionAcceptsArity(uri, name, arity) { + if version == xpathVersion10 && (prefix != "" || !xpath10FunctionArity(name, arity)) || !StandardFunctionAcceptsArity(uri, name, arity) { return &xdm.Error{Code: lexicon.ErrXPST0017, Message: "unknown function or arity: " + name} } // These functions belonged to the XSLT 2.0 host library before F&O 3.0 // added them to standalone XPath. XSLT performs its own host checks. - if version == 20 && uri == NSFn { + if version == xpathVersion20 && uri == NSFn { switch name { case fnNameUnparsedText, fnNameUnparsedTextAvailable, lexicon.FnFormatNumber, fnNameFormatDate, fnNameFormatDateTime, fnNameFormatTime: return profileError(lexicon.ErrXPST0017, name+" function", version) @@ -128,60 +211,27 @@ func checkStandaloneCall(version int, prefix, name string, arity int, namespaces return nil } -// Language pins the grammar and function library to XPath 1.0, 2.0, 3.0 or 3.1. -// Zero selects the engine default profile. -func (c Compiler) Language(version int, namespaces map[string]string) Compiler { - cfg := compilerCfg{} - if c.cfg != nil { - cfg = *c.cfg - } - - cfg.version, cfg.namespaces = version, maps.Clone(namespaces) - - return Compiler{cfg: &cfg} -} - -// HostFunctions augments the selected library without changing its grammar. -// The host still validates declarations and arities in its own static context. -func (c Compiler) HostFunctions(available func(uri, name string, arity int) bool) Compiler { - cfg := compilerCfg{} - if c.cfg != nil { - cfg = *c.cfg - } - - cfg.hostFunction = available - - return Compiler{cfg: &cfg} -} - -// BuiltinFunctionAvailable reports the selected language's standard function -// library, including version-specific arities, for host introspection. -func BuiltinFunctionAvailable(version int, uri, name string, arity int) bool { - return functionVisible(version, uri, name, arity) && IsBuiltinFunctionNS(uri, name) && - (arity < 0 || BuiltinFunctionAcceptsArity(uri, name, arity)) -} - func validateVersionTokens(tokens []Token, version int) error { - if version != 10 && version != 20 && version != 30 && version != 31 { + if version != xpathVersion10 && version != xpathVersion20 && version != xpathVersion30 && version != xpathVersion31 { return profileError(lexicon.ErrXPST0003, "language version", version) } for i, token := range tokens { - if version == 10 { + if version == xpathVersion10 { if token.Type == TokenNumber && strings.ContainsAny(token.Value, "eE") || token.Type == TokenUnion { return profileError(lexicon.ErrXPST0003, "numeric literal or union keyword", version) } if token.Value == nameProcessingInstruction && i+2 < len(tokens) && tokens[i+1].Type == TokenLParen && tokens[i+2].Type != TokenString && tokens[i+2].Type != TokenRParen { - return profileError(lexicon.ErrXPST0003, "PI target must be a string literal", 10) + return profileError(lexicon.ErrXPST0003, "PI target must be a string literal", xpathVersion10) } } - if version < 31 && token.Type == TokenArrow { + if version < xpathVersion31 && token.Type == TokenArrow { return profileError(lexicon.ErrXPST0003, "arrow operator", version) } - if version < 30 && token.Type != TokenString && strings.HasPrefix(token.Value, "Q{") { + if version < xpathVersion30 && token.Type != TokenString && strings.HasPrefix(token.Value, "Q{") { return profileError(lexicon.ErrXPST0003, "URI-qualified names", version) } } @@ -238,7 +288,7 @@ func walkProfile(value reflect.Value, visit func(any) error) error { } func validateVersionExpr(ast Expr, version int, namespaces map[string]string, hostFunction func(string, string, int) bool) error { - if version == 10 { + if version == xpathVersion10 { return validateXPath10Expr(ast) } @@ -249,7 +299,7 @@ func validateVersionExpr(ast Expr, version int, namespaces map[string]string, ho func lookupXPath30Function(key string) (bool, bool) { switch key { - case fnNameHead, nameTail, nameHasChildren, "innermost", "outermost", namePath, "analyze-string", lexicon.FnGenerateID, "format-integer", "function-name", "function-arity", "function-lookup", nameForEach, nameFilter, nameFoldLeft, nameFoldRight, nameForEachPair, "environment-variable", "available-environment-variables", "uri-collection", "serialize", "parse-xml", "parse-xml-fragment", nameUnparsedTextLines: + case fnNameHead, nameTail, nameHasChildren, fnNameInnermost, fnNameOutermost, namePath, fnNameAnalyzeString, lexicon.FnGenerateID, fnNameFormatInteger, fnNameFunctionName, fnNameFunctionArity, fnNameFunctionLookup, nameForEach, nameFilter, nameFoldLeft, nameFoldRight, nameForEachPair, fnNameEnvironmentVariable, fnNameAvailableEnvironmentVariables, fnNameURICollection, fnNameSerialize, fnNameParseXML, fnNameParseXMLFragment, nameUnparsedTextLines: return true, true } @@ -262,7 +312,7 @@ func functions30(key string) bool { value, _ := lookupXPath30Function(key); retu func lookupXPath31Function(key string) (bool, bool) { switch key { - case "parse-json", "json-doc", "json-to-xml", "xml-to-json", "transform", "default-language", nameLoadXQueryModule, "apply", "collation-key", "contains-token", "parse-ietf-date", "random-number-generator", nameSort: + case fnNameParseJSON, fnNameJSONDoc, fnNameJSONToXML, fnNameXMLToJSON, fnNameTransform, fnNameDefaultLanguage, nameLoadXQueryModule, fnNameApply, fnNameCollationKey, fnNameContainsToken, fnNameParseIETFDate, fnNameRandomNumberGenerator, nameSort: return true, true } @@ -274,19 +324,19 @@ func lookupXPath31Function(key string) (bool, bool) { func functions31(key string) bool { value, _ := lookupXPath31Function(key); return value } func functionVisible(version int, uri, name string, arity int) bool { - if version == 0 { + if version == xpathVersionDefault { return true } - if version == 10 { + if version == xpathVersion10 { return uri == NSFn && xpath10FunctionArity(name, arity) } - if (uri == NSMap || uri == NSArray) && version < 31 { + if (uri == NSMap || uri == NSArray) && version < xpathVersion31 { return false } - if uri == NSMath && version < 30 { + if uri == NSMath && version < xpathVersion30 { return false } @@ -298,26 +348,26 @@ func functionVisible(version int, uri, name string, arity int) bool { return true } - if functions31(name) && version < 31 { + if functions31(name) && version < xpathVersion31 { return false } - if functions30(name) && version < 30 { + if functions30(name) && version < xpathVersion30 { return false } - if version < 30 && (name == nameRound && arity == 2 || name == fnNameStringJoin && arity == 1) { + if version < xpathVersion30 && (name == nameRound && arity == profileBinaryArity || name == fnNameStringJoin && arity == profileUnaryArity) { return false } - if version < 30 && arity == 0 { + if version < xpathVersion30 && arity == profileNullaryArity { switch name { - case "data", fnNameDocumentURI, fnNameNilled, nameNodeName: + case fnNameData, fnNameDocumentURI, fnNameNilled, nameNodeName: return false } } - if version < 31 && (name == "tokenize" && arity == 1 || name == "trace" && arity == 1) { + if version < xpathVersion31 && (name == fnNameTokenize && arity == profileUnaryArity || name == fnNameTrace && arity == profileUnaryArity) { return false } @@ -325,21 +375,21 @@ func functionVisible(version int, uri, name string, arity int) bool { } func xsConstructorVisible(version int, name string) bool { - if name == lexicon.TypeNumeric && version < 31 { + if name == lexicon.TypeNumeric && version < xpathVersion31 { return false } - return version != 20 || !isBuiltinListLocalName(name) && name != lexicon.TypeError + return version != xpathVersion20 || !isBuiltinListLocalName(name) && name != lexicon.TypeError } func checkVersionRegex(ctx context.Context, pattern, flags string) error { ec := getFnContext(ctx) - if ec == nil || ec.version != 20 { + if ec == nil || ec.version != xpathVersion20 { return nil } - if strings.Contains(flags, "q") { - return &xdm.Error{Code: "FORX0001", Message: "flag q requires XPath 3.0"} + if strings.Contains(flags, regexLiteralFlag) { + return &xdm.Error{Code: errCodeFORX0001, Message: "flag q requires XPath 3.0"} } // Escaped characters and character classes cannot start a non-capturing group. inClass, escaped := false, false @@ -362,7 +412,7 @@ func checkVersionRegex(ctx context.Context, pattern, flags string) error { inClass = false } - if !inClass && r == '(' && strings.HasPrefix(pattern[i:], "(?:") { + if !inClass && r == '(' && strings.HasPrefix(pattern[i:], regexNoncapturingPrefix) { return &xdm.Error{Code: lexicon.ErrFORX0002, Message: "non-capturing groups require XPath 3.0"} } } @@ -377,11 +427,11 @@ func validateProfileNode(node any, version int, namespaces map[string]string, ho switch node.(type) { case ConcatExpr, SimpleMapExpr, LetClause, NamedFunctionRef, DynamicFunctionCall, InlineFunctionExpr, PlaceholderExpr, xdm.FunctionTest, xdm.NamespaceNodeTest: - if version < 30 { + if version < xpathVersion30 { return profileError(lexicon.ErrXPST0003, "XPath 3.0 construct", version) } case MapConstructorExpr, ArrayConstructorExpr, xdm.MapTest, xdm.ArrayTest, LookupExpr, UnaryLookupExpr: - if version < 31 { + if version < xpathVersion31 { return profileError(lexicon.ErrXPST0003, "maps, arrays or lookup", version) } case TryCatchExpr: @@ -408,7 +458,7 @@ func validateProfileNode(node any, version int, namespaces map[string]string, ho func validateProfileFunction(node any, version int, namespaces map[string]string, hostFunction func(string, string, int) bool) error { var prefix, name string - arity := -1 + arity := unspecifiedFunctionArity switch n := node.(type) { case FunctionCall: @@ -417,7 +467,7 @@ func validateProfileFunction(node any, version int, namespaces map[string]string prefix, name, arity = n.Prefix, n.Name, n.Arity } - if arity >= 0 { + if arity >= finiteArityFloor { uri := NSFn if prefix != "" { @@ -448,7 +498,7 @@ func validateFunctionSyntax(node any, version int, namespaces map[string]string) switch n := node.(type) { case InlineFunctionExpr: - if n.EmptyBody && version < 31 { + if n.EmptyBody && version < xpathVersion31 { return profileError(lexicon.ErrXPST0003, "empty inline function body", version) } @@ -473,7 +523,7 @@ func checkInlineParameterNames(params []FunctionParam, namespaces map[string]str for _, param := range params { key := VariableKey(param.Name, namespaces) if _, duplicate := seen[key]; duplicate { - return &xdm.Error{Code: "XQST0039", Message: "duplicate inline function parameter: " + param.Name} + return &xdm.Error{Code: errCodeXQST0039, Message: "duplicate inline function parameter: " + param.Name} } seen[key] = struct{}{} @@ -484,12 +534,12 @@ func checkInlineParameterNames(params []FunctionParam, namespaces map[string]str func reservedFunctionName(name string, version int) bool { switch name { - case lexicon.KindAttribute, lexicon.KindComment, lexicon.KindDocumentNode, lexicon.KindElement, nameEmptySequence, "if", lexicon.KindItem, lexicon.KindNode, lexicon.KindPI, lexicon.KindSchemaAttribute, lexicon.KindSchemaElement, lexicon.KindText, "typeswitch": + case lexicon.KindAttribute, lexicon.KindComment, lexicon.KindDocumentNode, lexicon.KindElement, nameEmptySequence, profileKeywordIf, lexicon.KindItem, lexicon.KindNode, lexicon.KindPI, lexicon.KindSchemaAttribute, lexicon.KindSchemaElement, lexicon.KindText, profileKeywordTypeSwitch: return true - case nameFunction, lexicon.KindNamespaceNode, "switch": - return version >= 30 - case "array", "map": - return version >= 31 + case nameFunction, lexicon.KindNamespaceNode, profileKeywordSwitch: + return version >= xpathVersion30 + case keywordArray, keywordMap: + return version >= xpathVersion31 } return false diff --git a/transform/internal/xpath/profile10.go b/transform/internal/xpath/profile10.go index ddcb791..bd0ec94 100644 --- a/transform/internal/xpath/profile10.go +++ b/transform/internal/xpath/profile10.go @@ -14,6 +14,18 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) +// XPath 1.0 core function local names are dispatch vocabulary, separate from +// similarly spelled schema type names and XML attribute names. +const ( + fnNameID = "id" + fnNameLang = "lang" + fnNameString = "string" + fnNameNumber = "number" +) + +// Both lexical parsing and formatting use IEEE binary64 precision in XPath 1.0. +const xpath10FloatBitSize = 64 + // XPath10String implements XPath 1.0 string(), including double formatting. func XPath10String(seq xdm.Sequence) (string, error) { return xpath10String(seq) } @@ -72,18 +84,24 @@ func XPath10Number(seq xdm.Sequence) (xdm.AtomicValue, error) { return xpath10Nu // Adapted from lestrrat-go/helium number.ToString; Copyright (c) 2015 // lestrrat, MIT license in licenses/MIT-helium.txt. func xpath10NumberString(v float64) string { + // XPath 1.0 number-to-string uses these words, unlike XSD INF/-INF lexicals. + const ( + xpath10PositiveInfinity = "Infinity" + xpath10NegativeInfinity = "-Infinity" + ) + switch { case math.IsNaN(v): return lexicon.FloatNaN case math.IsInf(v, 1): - return "Infinity" + return xpath10PositiveInfinity case math.IsInf(v, -1): - return "-Infinity" + return xpath10NegativeInfinity case v == 0: return "0" } - return strconv.FormatFloat(v, 'f', -1, 64) + return strconv.FormatFloat(v, 'f', -1, xpath10FloatBitSize) } func xpath10String(seq xdm.Sequence) (string, error) { @@ -115,21 +133,24 @@ func xpath10Double(v float64) xdm.Sequence { // its core library; stylesheet functions and explicitly registered extensions // are checked by their host, rather than being mistaken for newer builtins. func lookupXPath10FunctionArity(key string) ([2]int, bool) { + // A negative maximum denotes a variadic signature, not an argument count. + const xpath10VariadicMaximumArity = -1 + switch key { case lexicon.FnLast, lexicon.FnPosition, lexicon.ValueTrue, lexicon.ValueFalse: return [2]int{0, 0}, true - case fnNameCount, "id", fnNameBoolean, fnNameNot, lexicon.AttrLang, nameSum, nameFloor, nameCeiling, nameRound: - return [2]int{1, 1}, true - case nameLocalName, nameNamespaceURI, lexicon.FnName, lexicon.TypeString, nameStringLength, fnNameNormalizeSpace, lexicon.TypeNumber: - return [2]int{0, 1}, true + case fnNameCount, fnNameID, fnNameBoolean, fnNameNot, fnNameLang, nameSum, nameFloor, nameCeiling, nameRound: + return [2]int{builtinArityOne, builtinArityOne}, true + case nameLocalName, nameNamespaceURI, lexicon.FnName, fnNameString, nameStringLength, fnNameNormalizeSpace, fnNameNumber: + return [2]int{0, builtinArityOne}, true case nameConcat: - return [2]int{2, -1}, true + return [2]int{builtinArityTwo, xpath10VariadicMaximumArity}, true case fnNameStartsWith, fnNameContains, fnNameSubstringBefore, fnNameSubstringAfter: - return [2]int{2, 2}, true + return [2]int{builtinArityTwo, builtinArityTwo}, true case fnNameSubstring: - return [2]int{2, 3}, true + return [2]int{builtinArityTwo, builtinArityThree}, true case fnNameTranslate: - return [2]int{3, 3}, true + return [2]int{builtinArityThree, builtinArityThree}, true } var zero [2]int @@ -164,7 +185,7 @@ func xpath10Number(seq xdm.Sequence) (xdm.AtomicValue, error) { return xdm.AtomicValue{}, err } - s = strings.Trim(s, " \t\r\n") + s = strings.Trim(s, lexicon.XMLWhitespace) body := strings.TrimPrefix(s, "-") digits, dot := 0, false @@ -183,7 +204,7 @@ func xpath10Number(seq xdm.Sequence) (xdm.AtomicValue, error) { return doubleNaNAtom(), nil } - v, err := strconv.ParseFloat(s, 64) + v, err := strconv.ParseFloat(s, xpath10FloatBitSize) if err != nil && !math.IsInf(v, 0) { return doubleNaNAtom(), nil } @@ -206,24 +227,24 @@ func validateXPath10Expr(ast Expr) error { switch n.Op { case TokenPlus, TokenMinus, TokenStar, TokenDiv, TokenMod, TokenAnd, TokenOr, TokenEquals, TokenNotEquals, TokenLess, TokenLessEq, TokenGreater, TokenGreaterEq: default: - return profileError(lexicon.ErrXPST0003, "operator", 10) + return profileError(lexicon.ErrXPST0003, "operator", xpathVersion10) } case UnaryExpr: if !n.Negate { - return profileError(lexicon.ErrXPST0003, "unary plus", 10) + return profileError(lexicon.ErrXPST0003, "unary plus", xpathVersion10) } default: - return profileError(lexicon.ErrXPST0003, "expression", 10) + return profileError(lexicon.ErrXPST0003, "expression", xpathVersion10) } case xdm.NameTest: - if n.URI != "" || n.Prefix == "*" { - return profileError(lexicon.ErrXPST0003, "name test", 10) + if n.URIQualified || n.URI != "" || n.Prefix == "*" { + return profileError(lexicon.ErrXPST0003, "name test", xpathVersion10) } case xdm.NodeTest: switch n.(type) { case xdm.NameTest, xdm.TypeTest, xdm.PITest: default: - return profileError(lexicon.ErrXPST0003, "kind test", 10) + return profileError(lexicon.ErrXPST0003, "kind test", xpathVersion10) } } @@ -312,7 +333,7 @@ func xpath10NodeSet(seq xdm.Sequence) error { // builtin. Only the functions whose definitions changed need their own case. func callXPath10Builtin(ctx context.Context, ec *evalContext, function resolvedFunction, args []xdm.Sequence) (xdm.Sequence, error) { switch function.name { - case lexicon.TypeString, lexicon.TypeNumber: + case fnNameString, fnNameNumber: return callXPath10ContextConversion(ctx, ec, function.name, args) case nameSum: return sumXPath10Nodes(ctx, args[0]) @@ -341,7 +362,7 @@ func callXPath10ContextConversion(ctx context.Context, ec *evalContext, name str return nil, err } - if name == lexicon.TypeString { + if name == fnNameString { return coerceXPath10StringArgument(ctx, argument) } @@ -381,7 +402,7 @@ func coerceXPath10Argument(ctx context.Context, name string, index int, argument } return argument, nil - case nameConcat, fnNameStartsWith, fnNameContains, fnNameSubstringBefore, fnNameSubstringAfter, fnNameTranslate, nameStringLength, fnNameNormalizeSpace, lexicon.AttrLang: + case nameConcat, fnNameStartsWith, fnNameContains, fnNameSubstringBefore, fnNameSubstringAfter, fnNameTranslate, nameStringLength, fnNameNormalizeSpace, fnNameLang: return coerceXPath10StringArgument(ctx, argument) case fnNameSubstring: if index == 0 { @@ -391,7 +412,7 @@ func coerceXPath10Argument(ctx context.Context, name string, index int, argument return coerceXPath10NumberArgument(argument) case nameFloor, nameCeiling, nameRound: return coerceXPath10NumberArgument(argument) - case "id": + case fnNameID: return coerceXPath10IDArgument(ctx, argument) default: return argument, nil diff --git a/transform/internal/xpath/regex/translate.go b/transform/internal/xpath/regex/translate.go index 7ee3bf8..c90b61a 100644 --- a/transform/internal/xpath/regex/translate.go +++ b/transform/internal/xpath/regex/translate.go @@ -17,6 +17,41 @@ import ( "github.com/skarm/xmlsec/internal/lexicon" ) +// Unicode block escapes use the Is prefix. Property ranges are split at the +// BMP boundary into contiguous 16-bit and 32-bit RangeTable records. +const ( + unicodeBlockPrefix = "Is" + maxBMPRune = 0xFFFF + supplementaryRuneStart = 0x10000 + contiguousRangeStride = 1 +) + +// Class expansion spellings retain XPath/XSD character semantics both inside +// and outside bracketed groups. A simple backslash escape consumes two runes. +const ( + multiCharacterEscapes = "pPdDsSwWiIcC" + simpleEscapeRunes = 2 + anyCharacterClass = `[\s\S]` + nonLineBreakClass = `[^\n\r]` + decimalDigitClass = `\p{Nd}` + nonDecimalDigitClass = `[^\p{Nd}]` + wordCharacterClass = `[^\p{P}\p{Z}\p{C}]` + nonWordCharacterClass = `[\p{P}\p{Z}\p{C}]` + whitespaceClass = `[\t\n\r ]` + nonWhitespaceClass = `[^\t\n\r ]` + nonDecimalDigitRange = `\P{Nd}` + wordCharacterRange = `\p{L}\p{M}\p{N}\p{S}` + nonWordCharacterRange = `\p{P}\p{Z}\p{C}` +) + +// Hex property ranges are parsed into int64 endpoints; numeric backreferences +// accumulate decimal digits while the existing capture-count bound is applied. +const ( + hexadecimalRadix = 16 + codepointParseBits = 64 + decimalRadix = 10 +) + // XML NameStartChar as a character class (for use outside []) const ( xmlNameStartCharClass = `[a-zA-Z_:\x{C0}-\x{D6}\x{D8}-\x{F6}\x{F8}-\x{2FF}\x{370}-\x{37D}\x{37F}-\x{1FFF}\x{200C}-\x{200D}\x{2070}-\x{218F}\x{2C00}-\x{2FEF}\x{3001}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFFD}\x{10000}-\x{EFFFF}]` @@ -457,6 +492,49 @@ func ValidateXSD10(pattern string) error { return nil } +// Validate enforces the XPath/XSD pattern grammar before compilation. +func Validate(pattern string, allowBackrefs bool) error { + return validateXPathRegex(pattern, allowBackrefs) +} + +// HasBackrefs reports whether the pattern contains a back-reference. +func HasBackrefs(pattern string) bool { return hasXPathBackrefs(pattern) } + +// RejectPerlSpecific rejects Perl-only constructs not allowed in XPath regex. +func RejectPerlSpecific(pattern string) error { return rejectPerlSpecific(pattern) } + +// ResolveProperty preserves Unicode block escapes as predicates, so the i flag +// cannot accidentally case-fold them after expansion into character ranges. +func ResolveProperty(name string) *unicode.RangeTable { + if !strings.HasPrefix(name, unicodeBlockPrefix) { + return nil + } + + spec, ok := lookupUnicodeBlockRange(name[len(unicodeBlockPrefix):]) + if !ok { + return nil + } + + ranges, err := parseClassRanges(spec) + if err != nil { + return nil + } + + table := &unicode.RangeTable{} + + for _, r := range ranges { + if r.low <= maxBMPRune { + table.R16 = append(table.R16, unicode.Range16{Lo: uint16(r.low), Hi: uint16(min(r.high, maxBMPRune)), Stride: contiguousRangeStride}) // #nosec G115 -- The Unicode block table supplies nonnegative scalar ranges; the R16 branch caps the high bound. + } + + if r.high > maxBMPRune { + table.R32 = append(table.R32, unicode.Range32{Lo: uint32(max(r.low, supplementaryRuneStart)), Hi: uint32(r.high), Stride: contiguousRangeStride}) // #nosec G115 -- The Unicode block table bounds every range by 0x10FFFF. + } + } + + return table +} + type xsd10Class struct { first bool negated bool @@ -503,17 +581,6 @@ func (c *xsd10Class) character(ch rune, next byte) error { return nil } -// Validate enforces the XPath/XSD pattern grammar before compilation. -func Validate(pattern string, allowBackrefs bool) error { - return validateXPathRegex(pattern, allowBackrefs) -} - -// HasBackrefs reports whether the pattern contains a back-reference. -func HasBackrefs(pattern string) bool { return hasXPathBackrefs(pattern) } - -// RejectPerlSpecific rejects Perl-only constructs not allowed in XPath regex. -func RejectPerlSpecific(pattern string) error { return rejectPerlSpecific(pattern) } - func xsd10Escape(pattern string, at int) (int, bool) { if at >= len(pattern) { return at, false @@ -522,7 +589,7 @@ func xsd10Escape(pattern string, at int) (int, bool) { escaped, size := utf8.DecodeRuneInString(pattern[at:]) at += size - single := !strings.ContainsRune("pPdDsSwWiIcC", escaped) + single := !strings.ContainsRune(multiCharacterEscapes, escaped) if (escaped == 'p' || escaped == 'P') && at < len(pattern) && pattern[at] == '{' { if end := strings.IndexByte(pattern[at:], '}'); end >= 0 { at += end + 1 @@ -584,9 +651,9 @@ func translateXPathRegex(pattern string, dotAll, ignoreCase bool) (string, error if r == '.' { // XPath's dot excludes both CR and LF unless dotAll is set. if dotAll { - b.WriteString(`[\s\S]`) + b.WriteString(anyCharacterClass) } else { - b.WriteString(`[^\n\r]`) + b.WriteString(nonLineBreakClass) } } else { b.WriteRune(r) @@ -620,27 +687,27 @@ func translateRegexEscape(runes []rune, start int, ignoreCase bool) (string, int switch next { case 'i': - return xmlNameStartCharClass, 2, nil + return xmlNameStartCharClass, simpleEscapeRunes, nil case 'I': - return xmlNameStartCharClassNeg, 2, nil + return xmlNameStartCharClassNeg, simpleEscapeRunes, nil case 'c': - return xpathRegexNameCharClass, 2, nil + return xpathRegexNameCharClass, simpleEscapeRunes, nil case 'C': - return xpathRegexNameCharClassNeg, 2, nil + return xpathRegexNameCharClassNeg, simpleEscapeRunes, nil case 'd': - return `\p{Nd}`, 2, nil + return decimalDigitClass, simpleEscapeRunes, nil case 'D': - return `[^\p{Nd}]`, 2, nil + return nonDecimalDigitClass, simpleEscapeRunes, nil case 'w': - return `[^\p{P}\p{Z}\p{C}]`, 2, nil + return wordCharacterClass, simpleEscapeRunes, nil case 'W': - return `[\p{P}\p{Z}\p{C}]`, 2, nil + return nonWordCharacterClass, simpleEscapeRunes, nil case 's': - return `[\t\n\r ]`, 2, nil + return whitespaceClass, simpleEscapeRunes, nil case 'S': - return `[^\t\n\r ]`, 2, nil + return nonWhitespaceClass, simpleEscapeRunes, nil default: - return string(runes[start : start+2]), 2, nil + return string(runes[start : start+2]), simpleEscapeRunes, nil } } @@ -845,21 +912,21 @@ func translateClassEscape(runes []rune, start int) (string, int, error) { switch next { case 'i': - return xmlNameStartCharRange, 2, nil + return xmlNameStartCharRange, simpleEscapeRunes, nil case 'I': - return xmlNameStartCharRangeNeg, 2, nil + return xmlNameStartCharRangeNeg, simpleEscapeRunes, nil case 'c': - return xpathRegexNameCharRange, 2, nil + return xpathRegexNameCharRange, simpleEscapeRunes, nil case 'C': - return xpathRegexNameCharRangeNeg, 2, nil + return xpathRegexNameCharRangeNeg, simpleEscapeRunes, nil case 'd': - return `\p{Nd}`, 2, nil + return decimalDigitClass, simpleEscapeRunes, nil case 'D': - return `\P{Nd}`, 2, nil + return nonDecimalDigitRange, simpleEscapeRunes, nil case 'w': - return `\p{L}\p{M}\p{N}\p{S}`, 2, nil + return wordCharacterRange, simpleEscapeRunes, nil case 'W': - return `\p{P}\p{Z}\p{C}`, 2, nil + return nonWordCharacterRange, simpleEscapeRunes, nil default: return "", 0, nil } @@ -937,6 +1004,8 @@ func hasXPathBackrefs(pattern string) bool { func rejectPerlSpecific(pattern string) error { runes := []rune(pattern) + inCharClass := 0 + for i := 0; i < len(runes); i++ { if runes[i] == '\\' && i+1 < len(runes) { switch runes[i+1] { @@ -951,8 +1020,17 @@ func rejectPerlSpecific(pattern string) error { continue } + + switch runes[i] { + case '[': + inCharClass++ + case ']': + if inCharClass > 0 { + inCharClass-- + } + } // Reject inline flags (?...) — XPath uses flags parameter instead - if runes[i] == '(' && i+1 < len(runes) && runes[i+1] == '?' { + if inCharClass == 0 && runes[i] == '(' && i+1 < len(runes) && runes[i+1] == '?' { // Allow (?:...) for non-capturing groups if i+2 < len(runes) && runes[i+2] == ':' { continue @@ -973,38 +1051,6 @@ type classRange struct { high int64 } -// ResolveProperty preserves Unicode block escapes as predicates, so the i flag -// cannot accidentally case-fold them after expansion into character ranges. -func ResolveProperty(name string) *unicode.RangeTable { - if !strings.HasPrefix(name, "Is") { - return nil - } - - spec, ok := lookupUnicodeBlockRange(name[2:]) - if !ok { - return nil - } - - ranges, err := parseClassRanges(spec) - if err != nil { - return nil - } - - table := &unicode.RangeTable{} - - for _, r := range ranges { - if r.low <= 0xFFFF { - table.R16 = append(table.R16, unicode.Range16{Lo: uint16(r.low), Hi: uint16(min(r.high, 0xFFFF)), Stride: 1}) // #nosec G115 -- The Unicode block table supplies nonnegative scalar ranges; the R16 branch caps the high bound. - } - - if r.high > 0xFFFF { - table.R32 = append(table.R32, unicode.Range32{Lo: uint32(max(r.low, 0x10000)), Hi: uint32(r.high), Stride: 1}) // #nosec G115 -- The Unicode block table bounds every range by 0x10FFFF. - } - } - - return table -} - func parseClassRanges(spec string) ([]classRange, error) { runes := []rune(spec) @@ -1066,7 +1112,7 @@ func parseClassRune(runes []rune, start int) (int64, int, error) { return 0, start, fmt.Errorf("unterminated hex escape in character class range") } - value, err := strconv.ParseInt(string(runes[start+3:end]), 16, 64) + value, err := strconv.ParseInt(string(runes[start+3:end]), hexadecimalRadix, codepointParseBits) if err != nil { return 0, start, err } @@ -1102,8 +1148,8 @@ func translateUnicodeProperty(name string, neg bool) (string, error) { prefix = `\P` } - if strings.HasPrefix(name, "Is") { - blockName := name[2:] + if strings.HasPrefix(name, unicodeBlockPrefix) { + blockName := name[len(unicodeBlockPrefix):] if _, ok := lookupUnicodeBlockRange(blockName); ok { return prefix + "{" + name + "}", nil } @@ -1127,6 +1173,7 @@ func lookupUnicodeBlockRange(name string) (string, bool) { } // XML Schema category escapes use general categories, not Unicode scripts. +// The switch lists the complete general-category vocabulary admitted by this grammar. func isGoSupportedProperty(name string) bool { switch name { case "L", "Lu", "Ll", "Lt", "Lm", "Lo", "M", "Mn", "Mc", "Me", @@ -1254,7 +1301,7 @@ func resolveXPathBackref(runes []rune, start, captureCount int, groupStack []int // grow it, and a long digit run would wrap int (past 2^31-1 where int // is 32 bits) back into the capture range. if value <= captureCount { - value = value*10 + int(runes[end]-'0') + value = value*decimalRadix + int(runes[end]-'0') } end++ @@ -1274,7 +1321,7 @@ func resolveXPathBackref(runes []rune, start, captureCount int, groupStack []int break } - value = value*10 + int(runes[i]-'0') + value = value*decimalRadix + int(runes[i]-'0') if value > 0 && value <= captureCount && !isOpenCaptureGroup(groupStack, value) { ref = value validEnd = i + 1 diff --git a/transform/internal/xpath/regex/translate_test.go b/transform/internal/xpath/regex/translate_test.go index be39a0f..836f8ed 100644 --- a/transform/internal/xpath/regex/translate_test.go +++ b/transform/internal/xpath/regex/translate_test.go @@ -314,18 +314,32 @@ func TestQuantifierGrammarAndEscapedBraces(t *testing.T) { func TestPerlSpecificSyntaxAndEscapedLookalikes(t *testing.T) { t.Parallel() - for _, source := range []string{`\b`, `\B`, `\A`, `\Z`, `\z`, `\x41`, `\u0041`, `\U00000041`, `(?i)a`, `(?=a)`, `(?!a)`, `(?<=a)b`, `(?a)`, `(?>a)`} { + for _, source := range []string{`\b`, `\B`, `\A`, `\Z`, `\z`, `\x41`, `\u0041`, `\U00000041`, `(?i)a`, `(?=a)`, `(?!a)`, `(?<=a)b`, `(?a)`, `(?>a)`, `[a](?=b)`, `[a-[b]](?=c)`, `\[(?=a)`, `[\\](?=a)`, `[\]](?=a)`} { if err := xpathregex.RejectPerlSpecific(source); err == nil { t.Errorf("Perl syntax accepted: %q", source) } } - for _, source := range []string{`(?:a)`, `\\b`, `\(\?`, `\p{Lu}`, `plain`} { + for _, source := range []string{`(?:a)`, `\\b`, `\(\?`, `\p{Lu}`, `plain`, `[(?]`, `[(?=]`, `[(?-[?]]`, `[\](?]`} { if err := xpathregex.RejectPerlSpecific(source); err != nil { t.Errorf("XPath syntax rejected: %q: %v", source, err) } } } +func TestCharacterClassGroupLookalikesRemainLiteral(t *testing.T) { + t.Parallel() + for _, test := range []struct{ source, yes, no string }{ + {`[(?]`, "(", "a"}, + {`[(?=]`, "=", "a"}, + {`[(?-[?]]`, "(", "?"}, + {`[\](?]`, "]", "a"}, + } { + compiled := compileXPathPattern(t, "^(?:"+test.source+")$", false, false) + assertRegexMatch(t, compiled, test.yes, true) + assertRegexMatch(t, compiled, test.no, false) + } +} + func TestUnicodeBlockResolverBoundsAndUnknownNames(t *testing.T) { t.Parallel() for _, test := range []struct { diff --git a/transform/internal/xpath/regex_cache.go b/transform/internal/xpath/regex_cache.go index c5e24c1..fc0c991 100644 --- a/transform/internal/xpath/regex_cache.go +++ b/transform/internal/xpath/regex_cache.go @@ -45,16 +45,18 @@ func newRegexLRUCache(capacity int) *regexLRUCache { // Load returns the cached compiled regex for key, marking it most-recently-used. func (c *regexLRUCache) Load(key xpathRegexCacheKey) (*compiledXPathRegex, bool) { c.mu.Lock() - defer c.mu.Unlock() el, ok := c.items[key] if !ok { + c.mu.Unlock() return nil, false } c.ll.MoveToFront(el) + value := el.Value.(*regexLRUEntry).value //nolint:forcetypeassert // Only regexLRUEntry values are inserted into this list. + c.mu.Unlock() - return el.Value.(*regexLRUEntry).value, true //nolint:forcetypeassert // Only regexLRUEntry values are inserted into this list. + return value, true } // LoadOrStore returns the existing value for key, marking it most-recently-used, @@ -62,6 +64,13 @@ func (c *regexLRUCache) Load(key xpathRegexCacheKey) (*compiledXPathRegex, bool) // The bool reports whether an existing value was loaded. Admission evicts the // least-recently-used entries as needed. func (c *regexLRUCache) LoadOrStore(key xpathRegexCacheKey, value *compiledXPathRegex) (*compiledXPathRegex, bool) { + // Conservative accounting charges retained key storage and entry metadata; + // the pattern program reports its own cost. These are not exact heap sizes. + const ( + regexCacheKeyWeightFactor = 2 + regexCacheEntryOverheadBytes = 256 + ) + c.mu.Lock() defer c.mu.Unlock() @@ -70,7 +79,7 @@ func (c *regexLRUCache) LoadOrStore(key xpathRegexCacheKey, value *compiledXPath return el.Value.(*regexLRUEntry).value, true //nolint:forcetypeassert // Only regexLRUEntry values are inserted into this list. } - weight := 2*(len(key.pattern)+len(key.flags)) + 256 + value.program.CacheCost(xpathRegexCacheBytes+1) + weight := regexCacheKeyWeightFactor*(len(key.pattern)+len(key.flags)) + regexCacheEntryOverheadBytes + value.program.CacheCost(xpathRegexCacheBytes+1) if weight > xpathRegexCacheBytes { return value, false } diff --git a/transform/internal/xpath/runtime.go b/transform/internal/xpath/runtime.go index e4cd6fa..e6422d5 100644 --- a/transform/internal/xpath/runtime.go +++ b/transform/internal/xpath/runtime.go @@ -11,7 +11,8 @@ import ( const ( collationCacheCapacity = 128 collationCacheKeyBytes = 4096 - maxIdlePathProbes = 4 + // maxIdlePathProbes bounds retained scratch probes, not active evaluations. + maxIdlePathProbes = 4 ) // evaluationRuntime owns reusable execution resources for an evaluator family. @@ -79,10 +80,12 @@ func (r *evaluationRuntime) resolveCollation(uri, baseURI string) (*collationImp // Resolve outside the lock; readers keep the sync.Map fast path. Serialize // insertion and eviction so concurrent misses cannot exceed the capacity. r.collationMu.Lock() - defer r.collationMu.Unlock() if stored, ok := r.collations.Load(key); ok { - return stored.(*collationImpl), nil //nolint:forcetypeassert // Only collationImpl pointers are stored in this cache. + coll = stored.(*collationImpl) //nolint:forcetypeassert // Only collationImpl pointers are stored in this cache. + r.collationMu.Unlock() + + return coll, nil } if len(r.collationKeys) == collationCacheCapacity { @@ -94,6 +97,7 @@ func (r *evaluationRuntime) resolveCollation(uri, baseURI string) (*collationImp } r.collations.Store(key, coll) + r.collationMu.Unlock() return coll, nil } diff --git a/transform/internal/xpath/runtime_test.go b/transform/internal/xpath/runtime_test.go index 32cb2b7..8d306fd 100644 --- a/transform/internal/xpath/runtime_test.go +++ b/transform/internal/xpath/runtime_test.go @@ -587,12 +587,6 @@ func TestNumericBudgetASTAndVM(t *testing.T) { } } -func evaluateRuntimeAST(ctx context.Context, ec *evalContext, expr Expr) (xdm.Sequence, error) { - return evalWith(func(ctx context.Context, ec *evalContext, expr Expr) (xdm.Sequence, error) { - return dispatchExpr(evaluateRuntimeAST, ctx, ec, expr) - }, ctx, ec, expr) -} - func TestVersionedContractsASTAndVM(t *testing.T) { for _, version := range []int{20, 30, 31} { for _, tc := range []struct { @@ -1282,36 +1276,6 @@ func TestWhitespaceNormalizationPreservesValues(t *testing.T) { } } -func checkSubstring(t *testing.T, source string, start, length float64, bounded bool) { - t.Helper() - - args := []xdm.Sequence{xdm.SingleString(source), xdm.SingleDouble(start)} - if bounded { - args = append(args, xdm.SingleDouble(length)) - } - - actual, err := fnSubstring(t.Context(), args) - if err != nil { - t.Fatal(err) - } - - var expected strings.Builder - - roundedStart := roundHalfUpFloat(start, 0) - - for i, r := range []rune(source) { - p := float64(i + 1) - if p >= roundedStart && (!bounded || p < roundedStart+roundHalfUpFloat(length, 0)) { - expected.WriteRune(r) - } - } - - value, ok := actual.Get(0).(xdm.AtomicValue) - if !ok || value.StringVal() != expected.String() { - t.Fatalf("substring(%.80q, %v, %v), bounded=%v: %.80q, want %.80q", source, start, length, bounded, value.StringVal(), expected.String()) - } -} - func TestFunctionConversionBudgets(t *testing.T) { for _, tc := range []struct { name string @@ -1987,3 +1951,39 @@ func BenchmarkRegexRuntimeCache(b *testing.B) { }) }) } + +func evaluateRuntimeAST(ctx context.Context, ec *evalContext, expr Expr) (xdm.Sequence, error) { + return evalWith(func(ctx context.Context, ec *evalContext, expr Expr) (xdm.Sequence, error) { + return dispatchExpr(evaluateRuntimeAST, ctx, ec, expr) + }, ctx, ec, expr) +} + +func checkSubstring(t *testing.T, source string, start, length float64, bounded bool) { + t.Helper() + + args := []xdm.Sequence{xdm.SingleString(source), xdm.SingleDouble(start)} + if bounded { + args = append(args, xdm.SingleDouble(length)) + } + + actual, err := fnSubstring(t.Context(), args) + if err != nil { + t.Fatal(err) + } + + var expected strings.Builder + + roundedStart := roundHalfUpFloat(start, 0) + + for i, r := range []rune(source) { + p := float64(i + 1) + if p >= roundedStart && (!bounded || p < roundedStart+roundHalfUpFloat(length, 0)) { + expected.WriteRune(r) + } + } + + value, ok := actual.Get(0).(xdm.AtomicValue) + if !ok || value.StringVal() != expected.String() { + t.Fatalf("substring(%.80q, %v, %v), bounded=%v: %.80q, want %.80q", source, start, length, bounded, value.StringVal(), expected.String()) + } +} diff --git a/transform/internal/xpath/sequence.go b/transform/internal/xpath/sequence.go index ac2a9f1..4b89f5e 100644 --- a/transform/internal/xpath/sequence.go +++ b/transform/internal/xpath/sequence.go @@ -15,6 +15,8 @@ import ( // EBV computes the Effective Boolean Value of a sequence per XPath 3.1 Section 2.4.3. func EBV(seq xdm.Sequence) (bool, error) { + const effectiveBooleanSingleton = 1 // exactly one non-node atom + n := xdm.SequenceLen(seq) if n == 0 { return false, nil @@ -26,7 +28,7 @@ func EBV(seq xdm.Sequence) (bool, error) { return true, nil } - if n == 1 { + if n == effectiveBooleanSingleton { return ebvSingle(first) } @@ -145,12 +147,19 @@ func atomizeStream(seq xdm.Sequence, yield func(xdm.AtomicValue) (bool, error)) // atomizeBounded charges array traversal even when every leaf atomizes to (). // The callback can stop before a later item, preserving cardinality error order. func atomizeBounded(ctx context.Context, ec *evalContext, seq xdm.Sequence, yield func(xdm.AtomicValue) (bool, error)) error { + // Array nesting has its own fallback depth; cursor steps add one work + // unit to the separately counted empty members. + const ( + defaultAtomizationArrayDepth = 128 + atomizationCursorBaseWork = 1 + ) + check := typedValueItemCheckFor(ec) stack := []seqCursor{{members: []xdm.Sequence{seq}}} for len(stack) != 0 { item, ok, empty := stack[len(stack)-1].next() - if err := fnCountOps(ctx, ec, empty+1); err != nil { + if err := fnCountOps(ctx, ec, empty+atomizationCursorBaseWork); err != nil { return err } @@ -160,7 +169,7 @@ func atomizeBounded(ctx context.Context, ec *evalContext, seq xdm.Sequence, yiel } if array, isArray := item.(xdm.ArrayItem); isArray { - if len(stack) >= DepthLimit(ctx, 128) { + if len(stack) >= DepthLimit(ctx, defaultAtomizationArrayDepth) { return ErrBudget } diff --git a/transform/internal/xpath/serialization_adaptive.go b/transform/internal/xpath/serialization_adaptive.go index 429990b..d2cf8a8 100644 --- a/transform/internal/xpath/serialization_adaptive.go +++ b/transform/internal/xpath/serialization_adaptive.go @@ -6,10 +6,16 @@ import ( "io" "strconv" "strings" + "unicode/utf8" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/transform/internal/xdm" ) +// Container members are parenthesized according to cardinality; the outer +// sequence's alternate mode is local to AdaptiveSerialization.WriteTo. +const adaptiveContainerMember = true + // AdaptiveSerialization shares SER31 §10 value representations between hosts. // XML parameters apply to each delegated node; the separator applies only to // the outer sequence, while container members use parenthesized comma lists. @@ -24,14 +30,20 @@ type AdaptiveSerialization struct { // WriteTo serializes seq under ctx and the shared evaluation budgets. It leaves dst // open and may leave partial output on error. func (options AdaptiveSerialization) WriteTo(ctx context.Context, dst io.Writer, seq xdm.Sequence) error { + // Outer sequences default to newline without member parentheses. + const ( + adaptiveDefaultItemSeparator = "\n" + adaptiveOuterSequence = false + ) + state := adaptiveSerialization{ctx: ctx, dst: dst, options: options} - separator := "\n" + separator := adaptiveDefaultItemSeparator if options.ItemSeparator != nil { separator = *options.ItemSeparator } - return state.sequence(seq, separator, false, 0) + return state.sequence(seq, separator, adaptiveOuterSequence, 0) } type adaptiveSerialization struct { @@ -41,7 +53,7 @@ type adaptiveSerialization struct { } func (s *adaptiveSerialization) write(value string) error { - if err := Charge(s.ctx, 1+len(value)/64); err != nil { + if err := Charge(s.ctx, serializedRunBaseWork+len(value)/serializedWorkChunkBytes); err != nil { return err } @@ -49,11 +61,13 @@ func (s *adaptiveSerialization) write(value string) error { } func (s *adaptiveSerialization) sequence(seq xdm.Sequence, separator string, member bool, depth int) error { - if depth > DepthLimit(s.ctx, 128) { + const adaptiveSingleMemberCardinality = 1 // singleton needs no parentheses + + if depth > DepthLimit(s.ctx, defaultSerializedValueDepth) { return ErrBudget } - wrap := member && xdm.SequenceLen(seq) != 1 + wrap := member && xdm.SequenceLen(seq) != adaptiveSingleMemberCardinality if wrap { if err := s.write("("); err != nil { return err @@ -83,7 +97,14 @@ func (s *adaptiveSerialization) sequence(seq xdm.Sequence, separator string, mem } func (s *adaptiveSerialization) item(item xdm.Item, depth int) error { - if err := Charge(s.ctx, 1); err != nil { + // Each value visit costs one operation. Anonymous functions use this + // adaptive label instead of an expanded QName. + const ( + adaptiveItemVisitWork = 1 + adaptiveAnonymousFunctionName = "(anonymous-function)" + ) + + if err := Charge(s.ctx, adaptiveItemVisitWork); err != nil { return err } @@ -97,20 +118,20 @@ func (s *adaptiveSerialization) item(item xdm.Item, depth int) error { case xdm.MapItem: return s.mapping(value, depth) case xdm.FunctionItem: - name := "(anonymous-function)" + name := adaptiveAnonymousFunctionName if value.Name != "" { switch value.Namespace { case NSFn: - name = "fn:" + value.Name + name = prefixFn + ":" + value.Name case NSMath: - name = "math:" + value.Name + name = nameMath + ":" + value.Name case NSMap: - name = "map:" + value.Name + name = keywordMap + ":" + value.Name case NSArray: - name = "array:" + value.Name + name = keywordArray + ":" + value.Name case NSXS: - name = "xs:" + value.Name + name = lexicon.PrefixXS + ":" + value.Name default: name = "Q{" + value.Namespace + "}" + value.Name } @@ -123,6 +144,9 @@ func (s *adaptiveSerialization) item(item xdm.Item, depth int) error { } func (s *adaptiveSerialization) atomic(value xdm.AtomicValue) error { + // SER31 adaptive doubles retain this scientific-notation picture. + const adaptiveDoublePicture = "0.0##########################e0" + text, err := atomicToString(value) if err != nil { return err @@ -136,7 +160,7 @@ func (s *adaptiveSerialization) atomic(value xdm.AtomicValue) error { case xdm.IsSubtypeOf(value.TypeName, xdm.TypeDecimal): return s.write(text) case value.TypeName == xdm.TypeDouble: - text, err = boundedFormatNumber(s.ctx, value, "0.0##########################e0", DefaultDecimalFormat()) + text, err = boundedFormatNumber(s.ctx, value, adaptiveDoublePicture, DefaultDecimalFormat()) if err != nil { return err } @@ -164,18 +188,25 @@ func (s *adaptiveSerialization) atomic(value xdm.AtomicValue) error { } func (s *adaptiveSerialization) quoted(value string) error { + // Framing adds two bytes. The threshold chooses a writer shortcut, not + // an accepted-value cap; maps and normalization retain buffering. + const ( + adaptiveQuotedFrameBytes = 2 + adaptiveDirectQuoteThresholdBytes = 4096 + ) + // Quotes are doubled before the resulting text undergoes character mapping. // A quote introduced by a map is emitted verbatim, never doubled again. quotes := strings.Count(value, `"`) - if err := ChargeString(s.ctx, len(value), quotes, 2); err != nil { + if err := ChargeString(s.ctx, len(value), quotes, adaptiveQuotedFrameBytes); err != nil { return err } // Quotes, maps and normalization retain their character-expansion order. // Unquoted large values need only framing; retaining the short path avoids // additional writer calls on tiny values. - if len(value) < 4096 || quotes != 0 || len(s.options.XML.CharacterMap) != 0 || serializeNormalizationActive(s.options.XML.Normalization) { + if len(value) < adaptiveDirectQuoteThresholdBytes || quotes != 0 || len(s.options.XML.CharacterMap) != 0 || serializeNormalizationActive(s.options.XML.Normalization) { quoted := `"` + strings.ReplaceAll(value, `"`, `""`) + `"` text, err := SerializeTextValue(s.ctx, quoted, s.options.XML.CharacterMap, s.options.XML.Normalization) @@ -186,7 +217,7 @@ func (s *adaptiveSerialization) quoted(value string) error { return s.write(text) } - if err := Charge(s.ctx, 1+(len(value)+2)/64); err != nil { + if err := Charge(s.ctx, serializedRunBaseWork+(len(value)+adaptiveQuotedFrameBytes)/serializedWorkChunkBytes); err != nil { return err } @@ -216,9 +247,9 @@ func (s *adaptiveSerialization) node(node xdm.Node) error { name := node.Name() if node.Type() == xdm.NamespaceNode { if name == "" { - name = "xmlns" + name = lexicon.PrefixXMLNS } else { - name = "xmlns:" + name + name = lexicon.PrefixXMLNS + ":" + name } } @@ -255,11 +286,18 @@ func (s *adaptiveSerialization) node(node xdm.Node) error { // repertoire. Choose that recovery before writing; character references still // handle ordinary text/attributes and XML character validity remains enforced. func (s *adaptiveSerialization) nodeWriter(node xdm.Node) (xdm.XMLSerialization, error) { + // Delegated markup nodes have a separate visit charge. UTF-8 recovery + // disables only the writer's ASCII repertoire flag. + const ( + adaptiveMarkupNodeVisitWork = 1 + adaptiveUTF8RecoveryASCII = false + ) + writer := s.options.XML canEncode := writer.CanEncode if canEncode == nil && writer.ASCII { - canEncode = func(r rune) bool { return r < 128 } + canEncode = func(r rune) bool { return r < utf8.RuneSelf } } if canEncode == nil { @@ -268,7 +306,7 @@ func (s *adaptiveSerialization) nodeWriter(node xdm.Node) (xdm.XMLSerialization, unencodable := false check := func(value string) error { - if err := Charge(s.ctx, 1+len(value)/64); err != nil { + if err := Charge(s.ctx, serializedRunBaseWork+len(value)/serializedWorkChunkBytes); err != nil { return err } @@ -282,7 +320,7 @@ func (s *adaptiveSerialization) nodeWriter(node xdm.Node) (xdm.XMLSerialization, return nil } err := xdm.Walk(node, xdm.NodeWalkerFunc(func(n xdm.Node) error { - if err := Charge(s.ctx, 1); err != nil { + if err := Charge(s.ctx, adaptiveMarkupNodeVisitWork); err != nil { return err } @@ -311,8 +349,8 @@ func (s *adaptiveSerialization) nodeWriter(node xdm.Node) (xdm.XMLSerialization, })) if unencodable { - writer.Encoding = "UTF-8" - writer.ASCII = false + writer.Encoding = lexicon.EncodingUTF8U + writer.ASCII = adaptiveUTF8RecoveryASCII writer.CanEncode = nil } @@ -331,7 +369,7 @@ func (s *adaptiveSerialization) array(value xdm.ArrayItem, depth int) error { } } - if err := s.sequence(member, ",", true, depth+1); err != nil { + if err := s.sequence(member, ",", adaptiveContainerMember, depth+1); err != nil { return err } } @@ -363,7 +401,7 @@ func (s *adaptiveSerialization) mapping(value xdm.MapItem, depth int) error { return err } - return s.sequence(member, ",", true, depth+1) + return s.sequence(member, ",", adaptiveContainerMember, depth+1) }) if err != nil { return err diff --git a/transform/internal/xpath/serialization_sequence.go b/transform/internal/xpath/serialization_sequence.go index 9a3a643..6e32bf7 100644 --- a/transform/internal/xpath/serialization_sequence.go +++ b/transform/internal/xpath/serialization_sequence.go @@ -37,12 +37,14 @@ type serializationItems struct { func (s *serializationItems) fail(err error) bool { s.yield(xdm.XMLItem{}, err); return false } func (s *serializationItems) append(seq xdm.Sequence, depth int) bool { - if depth > DepthLimit(s.ctx, 128) { + const serializedSequenceItemWork = 1 // one logical item visit + + if depth > DepthLimit(s.ctx, defaultSerializedValueDepth) { return s.fail(ErrBudget) } for item := range xdm.SequenceItems(seq) { - if err := Charge(s.ctx, 1); err != nil { + if err := Charge(s.ctx, serializedSequenceItemWork); err != nil { return s.fail(err) } @@ -65,13 +67,15 @@ func (s *serializationItems) append(seq xdm.Sequence, depth int) bool { } func (s *serializationItems) item(item xdm.Item) bool { + const serializedAtomicSeparator = " " // default between adjacent atoms + if s.previousItem && s.separator != nil && !s.text(*s.separator) { return false } switch value := item.(type) { case xdm.AtomicValue: - if s.previousAtomic && s.separator == nil && !s.text(" ") { + if s.previousAtomic && s.separator == nil && !s.text(serializedAtomicSeparator) { return false } @@ -192,10 +196,12 @@ func (s *serializationItems) flush() bool { // tree construction or XHTML prefix/metadata changes. Ordinary XML/HTML writers // can consume SerializationItems directly without copying element subtrees. func NormalizeSerialization(ctx context.Context, seq xdm.Sequence, separator *string, baseURI string) (*xdm.Document, error) { + const serializationCoalesceText = true // Sequence normalization coalesces adjacent text + doc := xdm.NewDefaultDocument() doc.SetURL(baseURI) - for item, err := range SerializationItems(ctx, seq, separator, true) { + for item, err := range SerializationItems(ctx, seq, separator, serializationCoalesceText) { if err != nil { return nil, err } @@ -310,7 +316,7 @@ func (w *serializationBuffer) charge(size int) error { } if w.err == nil { - w.err = Charge(w.ctx, 1+size/64) + w.err = Charge(w.ctx, serializedRunBaseWork+size/serializedWorkChunkBytes) } return w.err @@ -319,9 +325,16 @@ func (w *serializationBuffer) charge(size int) error { // ValidateSerializationDocument enforces the document shape required when a // host requests an XML/XHTML DOCTYPE or standalone declaration. func ValidateSerializationDocument(ctx context.Context, seq xdm.Sequence, separator *string) error { + // Shape validation can keep borrowed text chunks and accepts one root. + const ( + shapeCoalesceText = false + serializationMaximumDocumentRoots = 1 + errCodeSEPM0004 = "SEPM0004" + ) + roots := 0 - for item, err := range SerializationItems(ctx, seq, separator, false) { + for item, err := range SerializationItems(ctx, seq, separator, shapeCoalesceText) { if err != nil { return err } @@ -330,8 +343,8 @@ func ValidateSerializationDocument(ctx context.Context, seq xdm.Sequence, separa roots++ } - if roots > 1 || item.Node == nil && item.Text != "" { - return &xdm.Error{Code: "SEPM0004", Message: "doctype or standalone requires document-shaped output"} + if roots > serializationMaximumDocumentRoots || item.Node == nil && item.Text != "" { + return &xdm.Error{Code: errCodeSEPM0004, Message: "doctype or standalone requires document-shaped output"} } } diff --git a/transform/internal/xpath/serialization_values.go b/transform/internal/xpath/serialization_values.go index 52edfee..26f2727 100644 --- a/transform/internal/xpath/serialization_values.go +++ b/transform/internal/xpath/serialization_values.go @@ -6,18 +6,60 @@ import ( "unicode/utf8" "github.com/skarm/xmlsec/internal/lexicon" + "github.com/skarm/xmlsec/internal/normalization" "github.com/skarm/xmlsec/transform/internal/xdm" ) +// Container depth is a serialization fallback, separate from evaluator call depth. +// Work units count visits and base run work; the shared chunk size counts source +// bytes per operation rather than an accepted-input or output-byte limit. +const ( + defaultSerializedValueDepth = 128 + serializedContainerVisitWork = 1 + serializedRunBaseWork = 1 + serializedWorkChunkBytes = 64 +) + +// JSON node serialization uses XML without its declaration. The other fixed +// flags retain the established markup writer defaults for this embedding path. +const ( + jsonNodeOmitXMLDeclaration = true + jsonNodeIncludeContentType = true + jsonNodeEscapeURIAttributes = true +) + +// JSON escapes cover C0, DEL/C1 and the requested encoding repertoire. A UTF-16 +// code unit uses four hexadecimal digits and six output bytes (\uXXXX); a +// surrogate pair needs twelve bytes. These sizes are syntax, not policy caps. +const ( + jsonFirstPrintableASCII = 0x20 + jsonEscapedControlStart = 0x7F + jsonEscapedControlEnd = 0x9F + jsonSurrogatePairEscapeBytes = 12 + jsonCodeUnitEscapeBytes = 6 + jsonCodeUnitHexDigits = 4 + jsonHexDigitBits = 4 + jsonCodeUnitHighShift = 12 + jsonHexDigitMask = 15 + jsonMaximumSingleCodeUnit = 0xFFFF +) + +// The fused node writer preserves work for its former XML buffer and two JSON +// expansion passes. Invalid UTF-8 decodes to RuneError consuming one input byte. +const ( + jsonNodeExpansionPasses = 3 + invalidUTF8RuneBytes = 1 +) + // enterValue bounds recursive JSON/adaptive containers independently of the // evaluator's own call depth, including values received from an application. func (opts serializeOptions) enterValue() error { ctx := opts.serializationContext() - if opts.depth > DepthLimit(ctx, 128) { + if opts.depth > DepthLimit(ctx, defaultSerializedValueDepth) { return ErrBudget } - return Charge(ctx, 1) + return Charge(ctx, serializedContainerVisitWork) } // join charges each copied part and delimiter before growing the result. @@ -61,15 +103,20 @@ func (opts serializeOptions) jsonComposite(open, closeBracket string, parts []st // potentially much larger concatenation. func (opts serializeOptions) characterRuns(value string, plain, mapped func(string) error) error { ctx := opts.serializationContext() - if err := Charge(ctx, 1+len(value)/64); err != nil { + if err := Charge(ctx, serializedRunBaseWork+len(value)/serializedWorkChunkBytes); err != nil { return err } form, normalize := serializeCharMapNorm(opts) + var normalizer *normalization.Normalizer + if normalize { + normalizer = &normalization.Normalizer{} + } + emit := func(run string) error { if normalize { - return form.Each(ctx, run, plain) + return normalizer.Each(ctx, form, run, plain) } return plain(run) @@ -94,7 +141,9 @@ func (opts serializeOptions) characterRuns(value string, plain, mapped func(stri return err } - start = offset + utf8.RuneLen(r) + // A malformed original byte consumes one byte, even though r is RuneError. + _, width := utf8.DecodeRuneInString(value[offset:]) + start = offset + width } return emit(value[start:]) @@ -142,7 +191,7 @@ func WriteJSONNode(ctx context.Context, dst io.Writer, value xdm.NodeItem, metho return WriteJSONString(ctx, dst, text, charMap, normalizationForm, canEncode) } - opts := serializeOptions{ctx: ctx, method: lexicon.MethodXML, omitXMLDeclaration: true, standalone: lexicon.ValueOmit, includeContentType: true, escapeURIAttributes: true} + opts := serializeOptions{ctx: ctx, method: lexicon.MethodXML, omitXMLDeclaration: jsonNodeOmitXMLDeclaration, standalone: lexicon.ValueOmit, includeContentType: jsonNodeIncludeContentType, escapeURIAttributes: jsonNodeEscapeURIAttributes} if err := opts.validate(); err != nil { return err } @@ -171,7 +220,7 @@ func normalizeSerializedValue(value string, opts serializeOptions) (string, erro } ctx := opts.serializationContext() - if err := Charge(ctx, 1+len(value)/64); err != nil { + if err := Charge(ctx, serializedRunBaseWork+len(value)/serializedWorkChunkBytes); err != nil { return "", err } @@ -193,7 +242,7 @@ func writeSerializationString(dst io.Writer, value string) error { } func writeSerializedJSONRun(ctx context.Context, dst io.Writer, run string, canEncode func(rune) bool) error { - if err := Charge(ctx, 1+len(run)/64); err != nil { + if err := Charge(ctx, serializedRunBaseWork+len(run)/serializedWorkChunkBytes); err != nil { return err } @@ -213,7 +262,9 @@ func writeSerializedJSONRun(ctx context.Context, dst io.Writer, run string, canE return err } - start = offset + utf8.RuneLen(r) + // Preserve boundaries in the original run when RuneError came from one byte. + _, width := utf8.DecodeRuneInString(run[offset:]) + start = offset + width } return writeSerializationString(dst, run[start:]) @@ -239,26 +290,26 @@ func serializedJSONRune(r rune, canEncode func(rune) bool) string { return `\t` } - if r >= 0x20 && !(r >= 0x7F && r <= 0x9F) && isValidXMLCodepoint(int(r)) && (canEncode == nil || canEncode(r)) { + if r >= jsonFirstPrintableASCII && !(r >= jsonEscapedControlStart && r <= jsonEscapedControlEnd) && isValidXMLCodepoint(int(r)) && (canEncode == nil || canEncode(r)) { return "" } - var data [12]byte + var data [jsonSurrogatePairEscapeBytes]byte unit := func(offset int, value uint16) { data[offset], data[offset+1] = '\\', 'u' - for i := range 4 { - data[offset+2+i] = jsonHexUpper[(value>>uint(12-i*4))&15] + for i := range jsonCodeUnitHexDigits { + data[offset+2+i] = jsonHexUpper[(value>>uint(jsonCodeUnitHighShift-i*jsonHexDigitBits))&jsonHexDigitMask] } } - if r <= 0xFFFF { + if r <= jsonMaximumSingleCodeUnit { unit(0, uint16(r)) // #nosec G115 -- r is a decoded Unicode rune and this branch restricts it to the BMP. - return string(data[:6]) + return string(data[:jsonCodeUnitEscapeBytes]) } hi, lo := utf16SurrogatePair(r) unit(0, hi) - unit(6, lo) + unit(jsonCodeUnitEscapeBytes, lo) return string(data[:]) } @@ -277,7 +328,7 @@ func (w *jsonNodeWriter) Write(p []byte) (int, error) { } // Preserve work accounting for the former XML buffer and its two JSON // expansion passes. Chunk boundaries may conservatively charge more work. - if err := Charge(w.ctx, 3+3*(len(p)/64)); err != nil { + if err := Charge(w.ctx, jsonNodeExpansionPasses+jsonNodeExpansionPasses*(len(p)/serializedWorkChunkBytes)); err != nil { return 0, err } @@ -294,7 +345,7 @@ func (w *jsonNodeWriter) Write(p []byte) (int, error) { } r, size := utf8.DecodeRune(w.tail[:w.tailSize]) - if r == utf8.RuneError && size == 1 { + if r == utf8.RuneError && size == invalidUTF8RuneBytes { return offset, io.ErrUnexpectedEOF } @@ -308,7 +359,7 @@ func (w *jsonNodeWriter) Write(p []byte) (int, error) { start := offset for offset < len(p) { r, size := utf8.DecodeRune(p[offset:]) - if r == utf8.RuneError && size == 1 { + if r == utf8.RuneError && size == invalidUTF8RuneBytes { if utf8.FullRune(p[offset:]) { return offset, io.ErrUnexpectedEOF } diff --git a/transform/internal/xpath/shared_constants.go b/transform/internal/xpath/shared_constants.go index adb2c3a..c703155 100644 --- a/transform/internal/xpath/shared_constants.go +++ b/transform/internal/xpath/shared_constants.go @@ -1,5 +1,9 @@ package xpath +import "github.com/skarm/xmlsec/internal/lexicon" + +// Package-shared spellings support function dispatch, grammar keywords, +// formatting labels and diagnostics. Their established case remains significant. const ( fnNameStringJoin = "string-join" fnNameFormatDate = "format-date" @@ -15,10 +19,10 @@ const ( nameEmptySequence = "empty-sequence" nameFunction = "function" nameISO = "ISO" - nameNFC = "NFC" - nameNFD = "NFD" - nameNFKC = "NFKC" - nameNFKD = "NFKD" + nameNFC = lexicon.NormalizationFormNFC + nameNFD = lexicon.NormalizationFormNFD + nameNFKC = lexicon.NormalizationFormNFKC + nameNFKD = lexicon.NormalizationFormNFKD nameAcht = "acht" nameAnd = "and" textArrayIndexOutOfRange = "array index out of range" @@ -60,7 +64,7 @@ const ( nameThen = "then" nameTreat = "treat" nameUnion = "union" - nameUpper2 = "upper" + nameUpperCase = "upper" nameUseLast = "use-last" textXMLToJSONInvalidChildNode = "xml-to-json: invalid child node" textXsDurationCannotBeUsedInArithmetic = "xs:duration cannot be used in arithmetic" diff --git a/transform/internal/xpath/static_check.go b/transform/internal/xpath/static_check.go index 0f2d00c..d216178 100644 --- a/transform/internal/xpath/static_check.go +++ b/transform/internal/xpath/static_check.go @@ -56,7 +56,7 @@ func (p prefixValidationPlan) Validate(namespaces map[string]string, strict bool } } - if version == 20 { + if version == xpathVersion20 { for _, literal := range p.qnameLiterals { if err := validateLiteralQName(literal, namespaces, strict, decls); err != nil { return err @@ -104,7 +104,7 @@ func validateAtomicTypeRequirement(req atomicTypeRequirement, namespaces map[str uri := resolveStaticNameURI(lexical, namespaces, namespaces[""]) if uri == lexicon.NamespaceXSD { - if knownXSTypeForVersion(local, version) && !isBuiltinListLocalName(local) && (version != 20 || local != lexicon.TypeError) { + if knownXSTypeForVersion(local, version) && !isBuiltinListLocalName(local) && (version != xpathVersion20 || local != lexicon.TypeError) { return nil } } else if decls != nil { @@ -197,11 +197,11 @@ func validateCastTarget(target AtomicTypeName, namespaces map[string]string, dec uri := resolveStaticNameURI(lexical, namespaces, namespaces[""]) if uri == lexicon.NamespaceXSD { - if local == lexicon.TypeAnyAtomicType || local == lexicon.TypeNotation || local == lexicon.TypeAnySimpleType && version != 20 { + if local == lexicon.TypeAnyAtomicType || local == lexicon.TypeNotation || local == lexicon.TypeAnySimpleType && version != xpathVersion20 { return &xdm.Error{Code: errCodeXPST0080, Message: "abstract cast target " + lexical} } - if knownXSTypeForVersion(local, version) && (version != 20 || !isBuiltinListLocalName(local) && local != lexicon.TypeError) { + if knownXSTypeForVersion(local, version) && (version != xpathVersion20 || !isBuiltinListLocalName(local) && local != lexicon.TypeError) { return nil } } else if decls != nil { @@ -210,13 +210,13 @@ func validateCastTarget(target AtomicTypeName, namespaces map[string]string, dec union := len(decls.UnionMemberTypes(name)) > 0 base := resolveToBuiltinBase(local, uri, decls) - if version != 20 && (list || union) || !list && !union && strings.HasPrefix(base, "xs:") && knownXSTypeNames(strings.TrimPrefix(base, "xs:")) { + if version != xpathVersion20 && (list || union) || !list && !union && strings.HasPrefix(base, lexicon.PrefixXS+":") && knownXSTypeNames(strings.TrimPrefix(base, lexicon.PrefixXS+":")) { return nil } } - code := "XQST0052" - if version == 20 { + code := errCodeXQST0052 + if version == xpathVersion20 { code = errCodeXPST0051 } @@ -226,7 +226,7 @@ func validateCastTarget(target AtomicTypeName, namespaces map[string]string, dec func knownXSTypeForVersion(local string, version int) bool { // Earlier libraries use bare "numeric" only as signature shorthand; // xs:numeric becomes an implicitly imported union type in F&O 3.1. - return knownXSTypeNames(local) && (local != lexicon.TypeNumeric || version == 0 || version >= 31) + return knownXSTypeNames(local) && (local != lexicon.TypeNumeric || version == xpathVersionDefault || version >= xpathVersion31) } func isBuiltinListLocalName(local string) bool { @@ -237,7 +237,7 @@ func isBuiltinListLocalName(local string) bool { // for use in SequenceType (instance of, cast as, etc.). func knownXSTypeNames(key string) bool { switch key { - case lexicon.TypeString, "integer", lexicon.TypeDecimal, "double", "float", fnNameBoolean, lexicon.TypeDate, lexicon.TypeDateTime, lexicon.TypeTime, lexicon.TypeDuration, lexicon.TypeDayTimeDuration, lexicon.TypeYearMonthDuration, "anyURI", lexicon.TypeQName, lexicon.TypeBase64Binary, lexicon.TypeHexBinary, lexicon.TypeUntypedAtomic, lexicon.TypeAnyAtomicType, "long", "int", "short", "byte", "unsignedLong", "unsignedInt", "unsignedShort", "unsignedByte", "nonNegativeInteger", "nonPositiveInteger", "positiveInteger", "negativeInteger", "normalizedString", "token", "language", lexicon.TypeName, "NCName", lexicon.TypeNMToken, lexicon.TypeENTITY, "ID", "IDREF", lexicon.TypeGDay, lexicon.TypeGMonth, lexicon.TypeGMonthDay, lexicon.TypeGYear, lexicon.TypeGYearMonth, lexicon.TypeDateTimeStamp, lexicon.TypeError, lexicon.TypeNumeric, lexicon.TypeNotation, lexicon.TypeNMTokens, lexicon.TypeIDREFS, lexicon.TypeENTITIES: + case lexicon.TypeString, lexicon.TypeInteger, lexicon.TypeDecimal, lexicon.TypeDouble, lexicon.TypeFloat, lexicon.TypeBoolean, lexicon.TypeDate, lexicon.TypeDateTime, lexicon.TypeTime, lexicon.TypeDuration, lexicon.TypeDayTimeDuration, lexicon.TypeYearMonthDuration, lexicon.TypeAnyURI, lexicon.TypeQName, lexicon.TypeBase64Binary, lexicon.TypeHexBinary, lexicon.TypeUntypedAtomic, lexicon.TypeAnyAtomicType, lexicon.TypeLong, lexicon.TypeInt, lexicon.TypeShort, lexicon.TypeByte, lexicon.TypeUnsignedLong, lexicon.TypeUnsignedInt, lexicon.TypeUnsignedShort, lexicon.TypeUnsignedByte, lexicon.TypeNonNegativeInteger, lexicon.TypeNonPositiveInteger, lexicon.TypePositiveInteger, lexicon.TypeNegativeInteger, lexicon.TypeNormalizedString, lexicon.TypeToken, lexicon.TypeLanguage, lexicon.TypeName, lexicon.TypeNCName, lexicon.TypeNMToken, lexicon.TypeENTITY, lexicon.TypeID, lexicon.TypeIDREF, lexicon.TypeGDay, lexicon.TypeGMonth, lexicon.TypeGMonthDay, lexicon.TypeGYear, lexicon.TypeGYearMonth, lexicon.TypeDateTimeStamp, lexicon.TypeError, lexicon.TypeNumeric, lexicon.TypeNotation, lexicon.TypeNMTokens, lexicon.TypeIDREFS, lexicon.TypeENTITIES: return true } diff --git a/transform/internal/xpath/static_refs.go b/transform/internal/xpath/static_refs.go index b062e6b..58d258c 100644 --- a/transform/internal/xpath/static_refs.go +++ b/transform/internal/xpath/static_refs.go @@ -547,13 +547,13 @@ func derefExprNode(node Expr) (Expr, bool) { // free variable. Kept in sync with eval_control.go buildCatchContext. func catchErrorVarKeys() [7]string { return [7]string{ - "err:code", - "err:description", - "err:value", - "err:module", - "err:line-number", - "err:column-number", - "err:additional", + catchVariableCode, + catchVariableDescription, + catchVariableValue, + catchVariableModule, + catchVariableLineNumber, + catchVariableColumnNumber, + catchVariableAdditional, } } diff --git a/transform/internal/xpath/streamability.go b/transform/internal/xpath/streamability.go index c46501c..1364e79 100644 --- a/transform/internal/xpath/streamability.go +++ b/transform/internal/xpath/streamability.go @@ -403,6 +403,13 @@ func streamFnLocalName(fc FunctionCall) (string, bool) { } func inspectPredicateMotion(e, pred Expr, step *Step) (bool, bool) { + // These legacy motion-exclusion labels compare lexical function local names; + // retaining self/current here neither registers nor validates such functions. + const ( + predicateMotionSelfName = "self" + predicateMotionCurrentName = "current" + ) + switch v := e.(type) { case LocationPath: for _, step := range v.Steps { @@ -431,8 +438,8 @@ func inspectPredicateMotion(e, pred Expr, step *Step) (bool, bool) { if isFn { switch local { case lexicon.FnName, nameLocalName, nameNamespaceURI, nameNodeName, - "self", lexicon.FnGenerateID, "base-uri", fnNameDocumentURI, - fnNameNilled, nameHasChildren, nameStringLength, "current": + predicateMotionSelfName, lexicon.FnGenerateID, fnNameBaseURI, fnNameDocumentURI, + fnNameNilled, nameHasChildren, nameStringLength, predicateMotionCurrentName: return false, false } } @@ -475,7 +482,7 @@ func inspectPredicateMotion(e, pred Expr, step *Step) (bool, bool) { // prefix is pathological and unsupported here; consistency with the evaluator is // preferred. func isFnNamespacePrefix(prefix string) bool { - return prefix == "" || prefix == "fn" + return prefix == "" || prefix == prefixFn } // lexicalFnLocalName resolves a function call's lexical (name, prefix) to its diff --git a/transform/internal/xpath/token.go b/transform/internal/xpath/token.go index ef8486a..f051b40 100644 --- a/transform/internal/xpath/token.go +++ b/transform/internal/xpath/token.go @@ -175,16 +175,16 @@ func (t TokenType) String() string { TokenLet: nameLet, TokenIn: "in", TokenReturn: nameReturn, - TokenWhere: "where", + TokenWhere: keywordWhere, TokenOrderBy: "order", TokenBy: "by", - TokenAscending: "ascending", - TokenDescending: "descending", - TokenStable: "stable", + TokenAscending: keywordAscending, + TokenDescending: keywordDescending, + TokenStable: keywordStable, TokenSome: nameSome, TokenEvery: nameEvery, TokenSatisfies: nameSatisfies, - TokenIf: "if", + TokenIf: profileKeywordIf, TokenThen: nameThen, TokenElse: nameElse, TokenTry: "try", diff --git a/transform/internal/xpath/vm.go b/transform/internal/xpath/vm.go index 462b9a0..6520494 100644 --- a/transform/internal/xpath/vm.go +++ b/transform/internal/xpath/vm.go @@ -64,9 +64,10 @@ type compiledExprRef struct { func (compiledExprRef) exprNode() {} type vmLocationStep struct { - Axis AxisType - NodeTest xdm.NodeTest - Predicates []Expr + ExplicitAxis bool + Axis AxisType + NodeTest xdm.NodeTest + Predicates []Expr } type vmPositionPredicateExpr struct { @@ -99,12 +100,6 @@ func (vmLocationPathExpr) exprNode() {} type vmPathExpr struct { Filter Expr Path *vmLocationPathExpr - // OrderFilter holds the original (un-lowered) filter expression so the - // order-preservation decision can be made at runtime against the actual - // function binding. The lowered Filter becomes an opaque compiledExprRef - // that cannot be inspected, so the un-lowered call is retained here for - // filterPreservesOrder. Nil when the filter is not a function call. - OrderFilter Expr } func (vmPathExpr) exprNode() {} @@ -191,10 +186,13 @@ func compileOwnedVMProgram(ast Expr) (*vmProgram, prefixValidationPlan, error) { } func compileVMProgramWithOptions(ast Expr, reuseInput bool) (*vmProgram, prefixValidationPlan, error) { + // Initial storage grows with compiled instructions; this does not cap a program. + const initialVMInstructionCapacity = 8 + si := computeStreamInfo(ast) builder := vmBuilder{ - instructions: make([]vmInstruction, 0, 8), + instructions: make([]vmInstruction, 0, initialVMInstructionCapacity), prefixPlan: newPrefixPlanBuilder(), reuseInput: reuseInput, } @@ -318,9 +316,10 @@ func (b *vmBuilder) lowerLocationPathSteps(absolute bool, steps []Step) (Expr, e } loweredSteps[i] = vmLocationStep{ - Axis: step.Axis, - NodeTest: step.NodeTest, - Predicates: preds, + Axis: step.Axis, + ExplicitAxis: step.ExplicitAxis, + NodeTest: step.NodeTest, + Predicates: preds, } } @@ -480,7 +479,7 @@ func (b *vmBuilder) lowerPathExpr(expr PathExpr) (Expr, error) { path = &lp } - return vmPathExpr{Filter: filter, Path: path, OrderFilter: orderFilterFrom(expr.Filter)}, nil + return vmPathExpr{Filter: filter, Path: path}, nil } func (b *vmBuilder) lowerVMPathExpr(expr vmPathExpr) (Expr, error) { @@ -505,7 +504,7 @@ func (b *vmBuilder) lowerVMPathExpr(expr vmPathExpr) (Expr, error) { path = &lp } - return vmPathExpr{Filter: filter, Path: path, OrderFilter: expr.OrderFilter}, nil + return vmPathExpr{Filter: filter, Path: path}, nil } func (b *vmBuilder) lowerPathStepExpr(expr PathStepExpr) (Expr, error) { @@ -1114,18 +1113,6 @@ func isGeneralComparisonToken(op TokenType) bool { } } -// orderFilterFrom returns the filter expression to retain for runtime -// order-preservation resolution: the un-lowered function call when the filter is -// one, otherwise nil. Keeping only function calls avoids holding unrelated -// subtrees alive. -func orderFilterFrom(filter Expr) Expr { - if _, ok := filter.(FunctionCall); ok { - return filter - } - - return nil -} - func lowerPointerExpr[T Expr](expr *T, kind string, lower func(T) (Expr, error)) (Expr, error) { if expr == nil { return nil, fmt.Errorf("%w: nil *%s", ErrUnsupportedExpr, kind) diff --git a/transform/internal/xpath/vm_path_nodes.go b/transform/internal/xpath/vm_path_nodes.go index 3d01935..a9cae76 100644 --- a/transform/internal/xpath/vm_path_nodes.go +++ b/transform/internal/xpath/vm_path_nodes.go @@ -206,11 +206,9 @@ func (v *vm) pathExprExists(ctx context.Context, ec *evalContext, e vmPathExpr) return len(nodes) > 0, nil } // vmPathStepNodes merges the result with DeduplicateNodes, which - // registers its document when it holds more than one node, unless E1 is a - // call to fn:reverse or fn:sort. - resultRegisters := !filterPreservesOrder(ctx, ec, e.OrderFilter) + // registers its document when it holds more than one node. frame := ec.pushNodeContext(baseNodes[0], 1, 1) - found, err := pathExists(v.evalExpr, ctx, ec, *e.Path, resultRegisters) + found, err := pathExists(v.evalExpr, ctx, ec, *e.Path, true) ec.restoreContext(frame) return found, err @@ -560,7 +558,7 @@ func callNodeListFunction(ctx context.Context, ec *evalContext, r resolvedFuncti switch r.name { case fnNameCount: - if ec.version == 10 { + if ec.version == xpathVersion10 { return xpath10Double(float64(len(nodes))), true } diff --git a/transform/internal/xpath/xpath.go b/transform/internal/xpath/xpath.go index 241c9e0..75c5948 100644 --- a/transform/internal/xpath/xpath.go +++ b/transform/internal/xpath/xpath.go @@ -94,7 +94,7 @@ func (e *Expression) ValidateWithSchema(namespaces map[string]string, decls Sche return err } - return e.prefixPlan.Validate(namespaces, e.staticNamespaces != nil || e.version == 10, decls, e.version) + return e.prefixPlan.Validate(namespaces, e.staticNamespaces != nil || e.version == xpathVersion10, decls, e.version) } // String returns the original XPath expression string. @@ -395,7 +395,7 @@ func (c Compiler) Compile(expr string) (*Expression, error) { } } - if c.cfg != nil && c.cfg.version != 0 { + if c.cfg != nil && c.cfg.version != xpathVersionDefault { if err := validateVersionTokens(l.tokens, c.cfg.version); err != nil { return nil, err } @@ -409,7 +409,7 @@ func (c Compiler) Compile(expr string) (*Expression, error) { return nil, err } - if version == 10 { + if version == xpathVersion10 { ast = xpath10Literals(ast) } @@ -451,7 +451,7 @@ func (c Compiler) CompileExpr(ast Expr) (*Expression, error) { version = c.cfg.version } - if version == 10 { + if version == xpathVersion10 { if err := validateXPath10Expr(ast); err != nil { return nil, err } diff --git a/transform/internal/xpath/xpath10_compat.go b/transform/internal/xpath/xpath10_compat.go index a73201e..4403e71 100644 --- a/transform/internal/xpath/xpath10_compat.go +++ b/transform/internal/xpath/xpath10_compat.go @@ -39,7 +39,7 @@ func doubleNaNAtom() xdm.AtomicValue { // atomToCompatDouble converts a single atom to xs:double with fn:number // semantics: a lexically-invalid value yields NaN, and never an error. func atomToCompatDouble(a xdm.AtomicValue, ec ...*evalContext) xdm.AtomicValue { - if len(ec) > 0 && ec[0] != nil && ec[0].version == 10 { + if len(ec) > 0 && ec[0] != nil && ec[0].version == xpathVersion10 { v, err := xpath10Number(xdm.ItemSlice{a}) if err != nil { return doubleNaNAtom() @@ -86,7 +86,7 @@ func xpath10CompatStringItem(seq xdm.Sequence) (xdm.AtomicValue, error) { // xpath10CompatNumberItem implements the xs:double(?)/xs:numeric(?) // function-conversion rule: fn:number of the first item (empty or invalid → NaN). func xpath10CompatNumberItem(seq xdm.Sequence, ec ...*evalContext) (xdm.AtomicValue, error) { - if len(ec) > 0 && ec[0] != nil && ec[0].version == 10 { + if len(ec) > 0 && ec[0] != nil && ec[0].version == xpathVersion10 { return xpath10Number(seq) } @@ -175,7 +175,7 @@ func generalCompareXPath10Compat(ctx context.Context, op TokenType, left, right return false, err } - if ec != nil && ec.version == 10 { + if ec != nil && ec.version == xpathVersion10 { if err := generalCompareXPath10CompatEntries(ctx, la, ra); err != nil { return false, err } diff --git a/transform/internal/xslt/avt.go b/transform/internal/xslt/avt.go index a0fbcce..996327b 100644 --- a/transform/internal/xslt/avt.go +++ b/transform/internal/xslt/avt.go @@ -12,6 +12,11 @@ import ( "github.com/skarm/xmlsec/transform/internal/xpath" ) +// Fallback recursive depth used only without a caller-provided XPath depth limit; each traversal retains its existing boundary comparison. +const ( + simpleContentDepthFallback = 128 +) + // avt is a compiled Attribute Value template. It consists of alternating // literal strings and XPath expressions enclosed in curly braces. type avt struct { @@ -67,7 +72,7 @@ func (a *avt) evaluate(ctx context.Context, node xdm.Node) (string, error) { // XSLT 1.0 behavior (backwards-compatible processing): when converting // the AVT expression value to a string, all items after the first are // discarded (§5.6.1). - if ec != nil && ec.stylesheet.processorLanguage() == 10 { + if ec != nil && ec.stylesheet.processorLanguage() == processorXSLT10 { s, err := xpath.XPath10String(result.Sequence()) if err != nil { return "", err @@ -232,7 +237,7 @@ func stringifySimpleContent(ctx context.Context, seq xdm.Sequence, sep string) ( } } - state := simpleContentState{ctx: ctx, separator: sep, depthLimit: xpath.DepthLimit(ctx, 128)} + state := simpleContentState{ctx: ctx, separator: sep, depthLimit: xpath.DepthLimit(ctx, simpleContentDepthFallback)} if err := state.appendSequence(seq, 0, true); err != nil { return "", err } @@ -245,7 +250,7 @@ func stringifyAVTContent(ctx context.Context, seq xdm.Sequence, backwards bool) return stringifySimpleContent(ctx, seq, " ") } - state := simpleContentState{ctx: ctx, depthLimit: xpath.DepthLimit(ctx, 128), firstOnly: true} + state := simpleContentState{ctx: ctx, depthLimit: xpath.DepthLimit(ctx, simpleContentDepthFallback), firstOnly: true} if err := state.appendSequence(seq, 0, false); err != nil { return "", err } @@ -568,8 +573,8 @@ func appendLiteral(parts []avtPart, s string) []avtPart { func compileAVTIsXPathCommentOnly(trimmed string, c *compiler, parts *[]avtPart, exprStr string, nsBindings map[string]string) (returned1 error, matchedBranch bool) { if trimmed == "" || isXPathCommentOnly(trimmed) { - if c.stylesheet.processorLanguage() == 10 { - return staticError("XTSE0370", "empty XPath 1.0 AVT expression"), true + if c.stylesheet.processorLanguage() == processorXSLT10 { + return staticError(errCodeXTSE0370, "empty XPath 1.0 AVT expression"), true } *parts = appendLiteral(*parts, "") diff --git a/transform/internal/xslt/avt_test.go b/transform/internal/xslt/avt_test.go index 94db156..20ae40c 100644 --- a/transform/internal/xslt/avt_test.go +++ b/transform/internal/xslt/avt_test.go @@ -71,10 +71,6 @@ func TestAVTCommentOnlyExpressions(t *testing.T) { } } -func stringItem(value string) xdm.AtomicValue { - return xdm.AtomicValue{TypeName: "xs:string", Value: value} -} - func TestSimpleContentNormalization(t *testing.T) { t.Parallel() doc := xdm.NewDefaultDocument() @@ -180,3 +176,7 @@ func TestAVTContentCompatibility(t *testing.T) { } } } + +func stringItem(value string) xdm.AtomicValue { + return xdm.AtomicValue{TypeName: "xs:string", Value: value} +} diff --git a/transform/internal/xslt/compile.go b/transform/internal/xslt/compile.go index c4a3346..b2cc3d5 100644 --- a/transform/internal/xslt/compile.go +++ b/transform/internal/xslt/compile.go @@ -18,6 +18,17 @@ import ( "github.com/skarm/xmlsec/transform/internal/xpath" ) +// A single leading underscore marks an XSLT 3.0 shadow attribute; doubled underscores and namespace declarations remain excluded by the existing lexical guard. +const ( + shadowAttributePrefix = "_" +) + +// Modern XSLT shadow-attribute local names are distinct from their evaluated target attributes; support and namespace checks remain unchanged. +const ( + shadowAttrName = "_name" + shadowAttrVersion = "_version" +) + // Compiler configures XSLT compilation with a processor profile, static parameters, // module resolvers and XML parsing policy. Fluent methods return updated configuration // copies; Compile creates separate mutable compilation state. @@ -233,16 +244,18 @@ func compile(ctx context.Context, doc *xdm.Document, cfg *compileConfig) (*Style } s, err := compilePrepared(ctx, prepared, cfg) - if s != nil { - s.sourceDoc = doc - for uri, module := range s.moduleDocs { - if module == prepared { - s.moduleDocs[uri] = doc - } + if err != nil { + return nil, err + } + + s.sourceDoc = doc + for uri, module := range s.moduleDocs { + if module == prepared { + s.moduleDocs[uri] = doc } } - return s, err + return s, nil } func compilePrepared(ctx context.Context, doc *xdm.Document, cfg *compileConfig) (*Stylesheet, error) { @@ -260,7 +273,7 @@ func compilePrepared(ctx context.Context, doc *xdm.Document, cfg *compileConfig) } localName := root.LocalName() - if localName != nameStylesheet && localName != fnNameTransform && localName != namePackage { + if localName != nameStylesheet && localName != lexicon.XSLTElementTransform && localName != namePackage { return nil, staticError(errCodeXTSE0010, "root element must be xsl:stylesheet, xsl:transform, or xsl:package, got %s", root.Name()) } @@ -293,7 +306,7 @@ func compilePrepared(ctx context.Context, doc *xdm.Document, cfg *compileConfig) // Namespace and base URI are available to root shadow expressions. Other // root attributes are interpreted only after their shadows have replaced them. - if xmlBase, present := root.GetAttributeNS("base", lexicon.NamespaceXML); present { + if xmlBase, present := root.GetAttributeNS(lexicon.AttrBase, lexicon.NamespaceXML); present { c.baseURI = resolveRootXMLBase(c.baseURI, xmlBase) } @@ -473,7 +486,7 @@ func (c *compiler) resolveAsType(_ context.Context, asAttr string) string { return asAttr } - candidate := "xs:" + typePart + candidate := lexicon.PrefixXS + ":" + typePart if xdm.IsKnownXSDType(candidate) { suffix := asAttr[len(typePart):] return candidate + suffix @@ -683,7 +696,7 @@ func (c *compiler) resolveShadowAttributes(ctx context.Context, elem *xdm.Elemen // Remove the shadow attribute and set the real one. // SetAttribute stores verbatim: avt values are plain text that may contain &. - elem.RemoveAttribute("_" + realName) + elem.RemoveAttribute(shadowAttributePrefix + realName) _ = elem.SetAttribute(realName, val) } @@ -693,7 +706,7 @@ func (c *compiler) resolveShadowAttributes(ctx context.Context, elem *xdm.Elemen // resolveSingleShadowAttribute resolves just one shadow attribute (_name) on // an element, leaving all other shadow attributes intact. func (c *compiler) resolveSingleShadowAttribute(ctx context.Context, elem *xdm.Element, name string) error { - shadowName := "_" + name + shadowName := shadowAttributePrefix + name avtStr, hasShadow := elem.GetAttribute(shadowName) if !hasShadow { @@ -718,14 +731,13 @@ func (c *compiler) resolveSingleShadowAttribute(ctx context.Context, elem *xdm.E return nil } -// compileXPath compiles an XPath expression with the given namespace bindings. // staticEvaluator returns an Evaluator suitable for evaluating static // XPath expressions at compile time (static="yes" params/variables). It // provides fn:transform and other XSLT static functions so that compile-time // evaluation can invoke dynamic transforms (used by use-when patterns). func (c *compiler) staticEvaluator(ctx context.Context) xpath.Evaluator { fns := c.useWhenFunctions() - fns[fnNameTransform] = &xsltFunc{min: 1, max: 1, fn: c.staticFnTransform, name: fnNameTransform, signature: xpath.HostBuiltin(31, fnNameTransform)} + fns[fnNameTransform] = &xsltFunc{min: 1, max: 1, fn: c.staticFnTransform, name: fnNameTransform, signature: xpath.HostBuiltin(xpathProfile31, fnNameTransform)} eval := c.useWhenEvaluator(ctx).Functions(fns, staticFunctionsNS(fns)) if c.parseOptions != nil { @@ -828,7 +840,7 @@ func (c *compiler) useWhenEvaluator(_ context.Context) xpath.Evaluator { } // XSLT 2.0 use-when has no available documents (section 3.12). // The 3.0 static context may access explicitly resolved resources. - if c.resolver != nil && c.stylesheet.processorLanguage() >= 30 { + if c.resolver != nil && c.stylesheet.processorLanguage() >= processorXSLT30 { eval = eval.URIResolver(resourceURIResolver{c.resolver}) } // Honor the compiler's per-resource read cap on compile-time doc() / @@ -884,7 +896,12 @@ func (c *compiler) expressionNamespaces(ns map[string]string) (map[string]string return c.xpathNamespaces(ns), ns[""] } +// compileXPath compiles an XPath expression with the supplied namespace bindings. func (c *compiler) compileXPath(expr string, ns map[string]string) (*xpath.Expression, error) { + const ( + errCodeXTSE3075 = "XTSE3075" + ) + version := c.stylesheet.xpathVersion() if err := xpath.Charge(c.invocation, len(ns)); err != nil { return nil, err @@ -910,7 +927,7 @@ func (c *compiler) compileXPath(expr string, ns map[string]string) (*xpath.Expre for _, name := range refs.FreeVariables { if xpath.VariableKey(name, bound) == xdm.ClarkName(lexicon.NamespaceXSLT, nameOriginal) { if c.originalVariableVisibility == visAbstract { - return nil, staticError("XTSE3075", "abstract variable has no xsl:original value") + return nil, staticError(errCodeXTSE3075, "abstract variable has no xsl:original value") } if c.originalVariableVisibility == "" { @@ -930,7 +947,7 @@ func (c *compiler) compileXPath(expr string, ns map[string]string) (*xpath.Expre c.markCompatExpr(compiled) } - if c.stylesheet.processorLanguage() >= 20 || !c.backwardsCompatible() && !c.forwardsCompatible(c.effectiveVersion) { + if c.stylesheet.processorLanguage() >= processorXSLT20 || !c.backwardsCompatible() && !c.forwardsCompatible(c.effectiveVersion) { if err := compiled.Validate(bound); err != nil { return nil, err } @@ -952,7 +969,7 @@ func (c *compiler) xpathCompilationError(expr string, version int, err error) (* return nil, failure } - if c.stylesheet.processorLanguage() == 10 && c.forwardsCompatible(c.effectiveVersion) { + if c.stylesheet.processorLanguage() == processorXSLT10 && c.forwardsCompatible(c.effectiveVersion) { return xpath.DeferredExpression(err, version), nil } @@ -970,13 +987,13 @@ func (c *compiler) readPackageMetadata(root *xdm.Element, localName string) erro c.stylesheet.packageVersion = getAttr(root, namePackageVersion) if _, present := root.GetAttribute(namePackageVersion); !present { - c.stylesheet.packageVersion = "1" + c.stylesheet.packageVersion = defaultPackageVersion } c.stylesheet.isPackage = true c.stylesheet.declaredModes = true // default for xsl:package - if dm := getAttr(root, "declared-modes"); dm != "" { + if dm := getAttr(root, nameDeclaredModes); dm != "" { if v, ok := parseXSDBool(dm); ok { c.stylesheet.declaredModes = v } @@ -984,7 +1001,7 @@ func (c *compiler) readPackageMetadata(root *xdm.Element, localName string) erro } else { // package-version is only allowed on xsl:package, not xsl:stylesheet/xsl:transform. // In forwards-compatible mode (version > 3.0), unknown attributes are silently ignored. - ver := getAttr(root, lexicon.SerializationVersion) + ver := getAttr(root, xslAttrVersion) if _, present := root.GetAttribute(namePackageVersion); present && !isForwardsCompatible(ver) { return staticError(errCodeXTSE0090, "attribute package-version is not allowed on xsl:%s", localName) @@ -1030,7 +1047,7 @@ func (c *compiler) configureRootContext(ctx context.Context, root *xdm.Element) // Read xpath-default-namespace from stylesheet root. Presence (even an // empty value) governs unprefixed pattern names; absence leaves the // inherited/default behavior untouched. - if xdn, ok := root.GetAttribute(nameXpathDefaultNamespace); ok { + if xdn, ok := root.GetAttribute(nameXPathDefaultNamespace); ok { c.xpathDefaultNS = xdn c.hasXPathDefaultNS = true } @@ -1116,14 +1133,14 @@ func (c *compiler) readRootVersion(ctx context.Context, root *xdm.Element, local // which governs whether backwards-compatible processing applies — reflects the // computed value (e.g. _version="{system-property('xsl:version')}" yields 3.0, // disabling BC even when version="1.0" is also present). - if _, hasShadow := root.GetAttribute("_version"); hasShadow { - if err := c.resolveSingleShadowAttribute(ctx, root, lexicon.SerializationVersion); err != nil { + if _, hasShadow := root.GetAttribute(shadowAttrVersion); hasShadow { + if err := c.resolveSingleShadowAttribute(ctx, root, xslAttrVersion); err != nil { return err } } // Read version — required on xsl:stylesheet and xsl:transform (XTSE0010). // Allow _version shadow attribute (resolved above) to satisfy the check. - c.stylesheet.version = getAttr(root, lexicon.SerializationVersion) + c.stylesheet.version = getAttr(root, xslAttrVersion) if err := compilePreparedVersion(c, root, localName); err != nil { return err } @@ -1131,7 +1148,7 @@ func (c *compiler) readRootVersion(ctx context.Context, root *xdm.Element, local c.effectiveVersion = c.stylesheet.version // Validate attributes on root element now that effectiveVersion is known. - if err := c.validateXSLTAttrs(ctx, root, attributeNames(lexicon.SerializationVersion, "id", nameDefaultMode, nameDefaultValidation, nameInputTypeAnnotations, nameDefaultCollation, xslAttrUseWhen, xslAttrName, namePackageVersion, "declared-modes")); err != nil { + if err := c.validateXSLTAttrs(ctx, root, attributeNames(xslAttrVersion, lexicon.AttrID, nameDefaultMode, nameDefaultValidation, nameInputTypeAnnotations, nameDefaultCollation, xslAttrUseWhen, xslAttrName, namePackageVersion, nameDeclaredModes)); err != nil { return err } @@ -1346,7 +1363,7 @@ func (c *compiler) staticFunctionAvailable(ctx context.Context, uri, name string } func (c *compiler) hostFunctionAvailable(uri, name string, arity int) bool { - if c.stylesheet.processorLanguage() >= 20 && c.backwardsCompatible() && uri != xpath.NSFn { + if c.stylesheet.processorLanguage() >= processorXSLT20 && c.backwardsCompatible() && uri != xpath.NSFn { return true // Every unavailable foreign name/arity has the BC error binding. } @@ -1360,7 +1377,7 @@ func (c *compiler) hostFunctionAvailable(uri, name string, arity int) bool { } return uri == lexicon.NamespaceFn && xsltFunctionAcceptsVersionArity(c.stylesheet.processorLanguage(), name, arity) && - (c.stylesheet.processorLanguage() >= 30 || !xslt30Function(name)) + (c.stylesheet.processorLanguage() >= processorXSLT30 || !xslt30Function(name)) } func (c *compiler) finalizeCompiledStylesheet() { @@ -1434,14 +1451,14 @@ func elementXSLTVersionAttribute(elem *xdm.Element) (string, bool) { return "", false // These select serialization, not the XSLT language. } - return elem.GetAttribute(lexicon.SerializationVersion) + return elem.GetAttribute(xslAttrVersion) } - return elem.GetAttributeNS(lexicon.SerializationVersion, lexicon.NamespaceXSLT) + return elem.GetAttributeNS(xslAttrVersion, lexicon.NamespaceXSLT) } func isShadowAttributeName(name string) bool { - return len(name) > 1 && name[0] == '_' && name[1] != '_' && name[1:] != "xmlns" + return len(name) > 1 && name[0] == '_' && name[1] != '_' && name[1:] != lexicon.PrefixXMLNS } func staticFunctionsNS(fns map[string]xpath.Function) map[xpath.QualifiedName]xpath.Function { @@ -1456,7 +1473,7 @@ func staticFunctionsNS(fns map[string]xpath.Function) map[xpath.QualifiedName]xp // isForwardsCompatible returns true if the version string indicates // forwards-compatible mode (version > 3.0). func isForwardsCompatible(ver string) bool { - comparison, valid := compareLanguageVersion(ver, 3) + comparison, valid := compareLanguageVersion(ver, xsltMajorVersion3) return valid && comparison > 0 } @@ -1491,7 +1508,7 @@ func resolveRootXMLBase(baseURI, xmlBase string) string { // isXSDecimal returns true if s is a valid xs:decimal lexical form: // optional sign, digits, optional dot followed by digits. No exponent. func isXSDecimal(s string) bool { - s = strings.Trim(s, " \t\r\n") + s = strings.Trim(s, lexicon.XMLWhitespace) if s == "" { return false } @@ -1523,8 +1540,8 @@ func isXSDecimal(s string) bool { } func compilePreparedVersion(c *compiler, root *xdm.Element, localName string) error { - if _, present := root.GetAttribute(lexicon.SerializationVersion); !present { - if _, hasShadow := root.GetAttribute("_version"); !hasShadow { + if _, present := root.GetAttribute(xslAttrVersion); !present { + if _, hasShadow := root.GetAttribute(shadowAttrVersion); !hasShadow { return staticError(errCodeXTSE0010, "xsl:%s requires a version attribute", localName) } @@ -1539,7 +1556,7 @@ func compilePreparedVersion(c *compiler, root *xdm.Element, localName string) er } func isReservedNamespace(uri string) bool { - return reservedNamespaceForProcessor(uri, 30) + return reservedNamespaceForProcessor(uri, processorXSLT30) } func reservedNamespaceForProcessor(uri string, processor int) bool { @@ -1549,7 +1566,7 @@ func reservedNamespaceForProcessor(uri string, processor int) bool { return true case lexicon.NamespaceMath, lexicon.NamespaceMap, lexicon.NamespaceArray, lexicon.NamespaceErr, lexicon.NamespaceXMLNS: - return processor >= 30 + return processor >= processorXSLT30 } return false @@ -1748,10 +1765,10 @@ func getAttr(elem *xdm.Element, name string) string { // parseXSDBool parses an xs:boolean value ("yes"/"no", lexicon.ValueTrue/lexicon.ValueFalse, "1"/"0") // with whitespace trimming per the XSD specification. func parseXSDBool(s string) (bool, bool) { - switch strings.Trim(s, " \t\r\n") { - case lexicon.ValueYes, lexicon.ValueTrue, "1": + switch strings.Trim(s, lexicon.XMLWhitespace) { + case lexicon.ValueYes, lexicon.ValueTrue, booleanDigitTrue: return true, true - case lexicon.ValueNo, lexicon.ValueFalse, "0": + case lexicon.ValueNo, lexicon.ValueFalse, booleanDigitFalse: return false, true default: return false, false @@ -1770,7 +1787,7 @@ func xsdBoolTrue(s string) bool { // compatibility mode. Per XSLT 3.0 §3.10 this is any effective version below 2.0 // (an effective version in [2.0, 3.0) behaves identically to 3.0). func isBackwardsCompatible(ver string) bool { - comparison, valid := compareLanguageVersion(ver, 2) + comparison, valid := compareLanguageVersion(ver, xsltMajorVersion2) return valid && comparison < 0 } @@ -1838,9 +1855,9 @@ func hasNonEmptyContent(elem *xdm.Element) bool { func parseDOEAttr(v string) (bool, error) { v = strings.TrimSpace(v) switch v { - case lexicon.ValueYes, lexicon.ValueTrue, "1": + case lexicon.ValueYes, lexicon.ValueTrue, booleanDigitTrue: return true, nil - case lexicon.ValueNo, lexicon.ValueFalse, "0": + case lexicon.ValueNo, lexicon.ValueFalse, booleanDigitFalse: return false, nil default: return false, staticError(lexicon.ErrSEPM0016, diff --git a/transform/internal/xslt/compile_formats.go b/transform/internal/xslt/compile_formats.go index 0a41d60..c3d9220 100644 --- a/transform/internal/xslt/compile_formats.go +++ b/transform/internal/xslt/compile_formats.go @@ -62,14 +62,24 @@ func (c *compiler) compileCharacterMap(ctx context.Context, elem *xdm.Element) e return staticError(errCodeXTSE0010, "unexpected child element in xsl:character-map: %s", childElem.Name()) } - charAttr := getAttr(childElem, "character") - if charAttr == "" { + charAttr, hasCharacter := childElem.GetAttribute(nameCharacter) + if !hasCharacter { return staticError(errCodeXTSE0010, "xsl:output-character requires 'character' attribute") } + if utf8.RuneCountInString(charAttr) != 1 { + return staticError(errCodeXTSE0020, "xsl:output-character/@character must contain exactly one Unicode character") + } + r := firstRune(charAttr) - str := getAttr(childElem, "string") + + str, hasString := childElem.GetAttribute(nameOutputCharacterString) + if !hasString { + return staticError(errCodeXTSE0010, + "xsl:output-character requires 'string' attribute") + } + cm.Mappings[r] = str } @@ -144,7 +154,7 @@ func (c *compiler) compileKey(ctx context.Context, elem *xdm.Element) error { savedDefaultNS, savedHasDefaultNS := c.xpathDefaultNS, c.hasXPathDefaultNS defer func() { c.xpathDefaultNS, c.hasXPathDefaultNS = savedDefaultNS, savedHasDefaultNS }() - if xdn, ok := elem.GetAttribute(nameXpathDefaultNamespace); ok { + if xdn, ok := elem.GetAttribute(nameXPathDefaultNamespace); ok { xpathDefaultNS = xdn hasXPathDefaultNS = true c.xpathDefaultNS, c.hasXPathDefaultNS = xdn, true @@ -226,7 +236,7 @@ func (c *compiler) compileKeyValue(ctx context.Context, elem *xdm.Element, kd *k return err } - if c.stylesheet.processorLanguage() == 10 { + if c.stylesheet.processorLanguage() == processorXSLT10 { if err := validateLegacyKeyUse(useExpr, c.nsBindings); err != nil { return err } @@ -289,7 +299,7 @@ func (c *compiler) compileOutput(ctx context.Context, elem *xdm.Element) error { // XSLT 2.0 (and 1.0) restrict them to yes/no; XSLT 3.0+ accepts the full // xs:boolean lexical space. The selected processor governs these rules; // XSLT 3.0 defines no differing behavior for a 2.0 declaration. - yesNoOnly := c.stylesheet.processorLanguage() < 30 + yesNoOnly := c.stylesheet.processorLanguage() < processorXSLT30 if err := compileOutputBooleans(elem, outDef, yesNoOnly); err != nil { return err @@ -362,7 +372,7 @@ func (c *compiler) compileOutputSerialization(elem *xdm.Element, out *OutputDef, if version := getAttr(elem, lexicon.SerializationHTMLVersion); version != "" { version = strings.TrimSpace(version) - if _, err := strconv.ParseFloat(version, 64); err != nil { + if _, err := strconv.ParseFloat(version, float64ParseBits); err != nil { return staticError(errCodeXTSE0020, "%q is not a valid value for xsl:output/@html-version", version) } @@ -532,7 +542,7 @@ func (c *compiler) compileAttributeSet(ctx context.Context, elem *xdm.Element) e } func (c *compiler) compileDecimalFormat(ctx context.Context, elem *xdm.Element) error { - if err := c.validateXSLTAttrs(ctx, elem, attributeNames(xslAttrName, nameDecimalSeparator, nameGroupingSeparator, nameInfinity, nameMinusSign, lexicon.FloatNaN, namePercent, namePerMille, nameZeroDigit, nameDigit, namePatternSeparator, nameExponentSeparator, xslAttrUseWhen)); err != nil { + if err := c.validateXSLTAttrs(ctx, elem, attributeNames(xslAttrName, nameDecimalSeparator, nameGroupingSeparator, nameInfinity, nameMinusSign, lexicon.DecimalFormatNaN, namePercent, namePerMille, nameZeroDigit, nameDigit, namePatternSeparator, nameExponentSeparator, xslAttrUseWhen)); err != nil { return err } // Push element-local namespace declarations so prefixed names resolve correctly @@ -568,7 +578,7 @@ func (c *compiler) compileDecimalFormat(ctx context.Context, elem *xdm.Element) if !exists { df = xpath.DefaultDecimalFormat() - df.ExponentDisabled = c.stylesheet.processorLanguage() < 30 + df.ExponentDisabled = c.stylesheet.processorLanguage() < processorXSLT30 } // XTSE1290: record potential conflicts for deferred checking after all imports. @@ -607,7 +617,7 @@ func (c *compiler) compileDecimalFormat(ctx context.Context, elem *xdm.Element) for _, attr := range []string{ nameDecimalSeparator, nameGroupingSeparator, namePercent, namePerMille, nameZeroDigit, nameDigit, namePatternSeparator, nameExponentSeparator, - nameInfinity, lexicon.FloatNaN, nameMinusSign, + nameInfinity, lexicon.DecimalFormatNaN, nameMinusSign, } { if elem.HasAttribute(attr) { setProps[attr] = struct{}{} @@ -625,13 +635,16 @@ func (c *compiler) decimalFormatName(elem *xdm.Element) (xpath.QualifiedName, er } name = strings.TrimFunc(name, xmlchar.IsSpace) - if !xmlchar.IsValidQName(name) && !(c.stylesheet.processorLanguage() >= 30 && isValidEQName(name)) { + if !xmlchar.IsValidQName(name) && !(c.stylesheet.processorLanguage() >= processorXSLT30 && isValidEQName(name)) { return xpath.QualifiedName{}, staticError(errCodeXTSE0020, "invalid name %q on xsl:decimal-format", name) } qn, err := resolveIntrospectionName(name, "", errCodeXTSE0280, nsResolverFromMap(inScopeNamespaces(elem))) + if err != nil { + return xpath.QualifiedName{}, err + } - return xpath.QualifiedName{URI: qn.URI, Name: qn.Local}, err + return xpath.QualifiedName{URI: qn.URI, Name: qn.Local}, nil } func (c *compiler) compileSpaceHandling(ctx context.Context, elem *xdm.Element, strip bool) error { @@ -655,7 +668,7 @@ func (c *compiler) compileSpaceHandling(ctx context.Context, elem *xdm.Element, // Apply per-element xpath-default-namespace savedXPathDefaultNS := c.xpathDefaultNS - if xdn, ok := elem.GetAttribute(nameXpathDefaultNamespace); ok { + if xdn, ok := elem.GetAttribute(nameXPathDefaultNamespace); ok { c.xpathDefaultNS = xdn } defer func() { c.xpathDefaultNS = savedXPathDefaultNS }() @@ -775,7 +788,7 @@ func (c *compiler) spaceDeclNamespaces(elem *xdm.Element) map[string]string { // does not cover the name-set of an "a" vs "a" conflict, so it must not cancel // that genuine same-precedence conflict. func (c *compiler) checkSpaceConflicts(_ context.Context) error { - if c.stylesheet.processorLanguage() == 20 { + if c.stylesheet.processorLanguage() == processorXSLT20 { // XSLT 2.0 §3.4 permits recovery from XTRE0270 by using the last // declaration. XSLT 3.0 makes the conflict a static error instead. return nil @@ -841,12 +854,12 @@ func (c *compiler) spaceRegionCovered(region nameTest, prec int) bool { func validateLegacyKeyUse(expression *xpath.Expression, bindings map[string]string) error { refs := expression.StaticReferences(bindings) if len(refs.FreeVariables) > 0 { - return staticError("XTSE1205", "variables are unavailable in XSLT 1.0 keys") + return staticError(errCodeXTSE1205, "variables are unavailable in XSLT 1.0 keys") } for _, fn := range refs.FunctionNames { - if fn.Prefix == "" && fn.Name == nameKey2 { - return staticError("XTSE1205", "key() is unavailable in XSLT 1.0 key declarations") + if fn.Prefix == "" && fn.Name == keyLocalName { + return staticError(errCodeXTSE1205, "key() is unavailable in XSLT 1.0 key declarations") } } @@ -1035,11 +1048,11 @@ func outputStandalone(value string, yesNoOnly bool) (string, error) { switch value { case lexicon.ValueYes, lexicon.ValueNo, lexicon.ValueOmit: return value, nil - case lexicon.ValueTrue, "1": + case lexicon.ValueTrue, booleanDigitTrue: if !yesNoOnly { return lexicon.ValueYes, nil } - case lexicon.ValueFalse, "0": + case lexicon.ValueFalse, booleanDigitFalse: if !yesNoOnly { return lexicon.ValueNo, nil } @@ -1066,7 +1079,7 @@ func loadParameterDocumentFromFile(ctx context.Context, injected *xdm.ParseOptio return values.explicit, nil, nil } - parameters, err := xpath.ReadSerializationParameters(ctx, root, 31) + parameters, err := xpath.ReadSerializationParameters(ctx, root, xpathProfile31) if err != nil { return values.explicit, nil, err } @@ -1086,6 +1099,14 @@ func parameterDocumentURI(baseURI, href string) string { } func loadParameterDocumentRoot(ctx context.Context, injected *xdm.ParseOptions, baseURI, href string, openResource func(context.Context, string) (io.ReadCloser, error), runtime bool, resourceLimit int64) (*xdm.Element, error) { + const ( + serializationParametersElement = "serialization-parameters" + ) + + const ( + errCodeSEPM0017 = "SEPM0017" + ) + reader, err := openResource(ctx, parameterDocumentURI(baseURI, href)) if err != nil { if failure := preserveControlFailure(ctx, err); failure != nil { @@ -1106,12 +1127,12 @@ func loadParameterDocumentRoot(ctx context.Context, injected *xdm.ParseOptions, return nil, failure } - return nil, &xdm.Error{Code: "SEPM0017", Message: "ill-formed serialization parameter document", Cause: err} + return nil, &xdm.Error{Code: errCodeSEPM0017, Message: "ill-formed serialization parameter document", Cause: err} } root := doc.DocumentElement() - if root == nil || root.LocalName() != "serialization-parameters" || root.URI() != lexicon.NamespaceSerialization { - return nil, &xdm.Error{Code: "SEPM0017", Message: "serialization parameter document requires output:serialization-parameters"} + if root == nil || root.LocalName() != serializationParametersElement || root.URI() != lexicon.NamespaceSerialization { + return nil, &xdm.Error{Code: errCodeSEPM0017, Message: "serialization parameter document requires output:serialization-parameters"} } return root, nil @@ -1181,7 +1202,7 @@ func applyDecimalFormatAttributes(df *xpath.DecimalFormat, elem *xdm.Element) { df.Infinity = v } - if v, present := elem.GetAttribute(lexicon.FloatNaN); present { + if v, present := elem.GetAttribute(lexicon.DecimalFormatNaN); present { df.NaN = v } @@ -1240,8 +1261,8 @@ func checkDecimalFormatConflictExplicit(existing xpath.DecimalFormat, elem *xdm. } } - if v, present := elem.GetAttribute(lexicon.FloatNaN); present { - if _, wasSet := prevSet[lexicon.FloatNaN]; wasSet && v != existing.NaN { + if v, present := elem.GetAttribute(lexicon.DecimalFormatNaN); present { + if _, wasSet := prevSet[lexicon.DecimalFormatNaN]; wasSet && v != existing.NaN { return staticError(errCodeXTSE1290, "conflicting xsl:decimal-format declarations: NaN was %q, now %q", existing.NaN, v) @@ -1269,6 +1290,13 @@ func checkDecimalFormatConflictExplicit(existing xpath.DecimalFormat, elem *xdm. // criterion (one fixes the namespace, the other fixes the local name), so they // are kept as separate kinds. func nameTestKey(nt nameTest) string { + const ( + spaceConflictExactPrefix = "E:" + spaceConflictLocalPrefix = "L:" + spaceConflictNamespacePrefix = "N:" + spaceConflictUniversalKey = "U:" + ) + uri := "" if nt.HasURI { uri = nt.URI @@ -1277,16 +1305,16 @@ func nameTestKey(nt nameTest) string { switch { case nt.Prefix == "*": // "*:local" — local-name wildcard (namespace unconstrained). - return "L:" + nt.Local + return spaceConflictLocalPrefix + nt.Local case nt.Local == "*" && nt.HasURI: // "prefix:*" / "Q{uri}*" — namespace wildcard. - return "N:" + uri + return spaceConflictNamespacePrefix + uri case nt.Local == "*": // "*" — universal wildcard. - return "U:" + return spaceConflictUniversalKey default: // Exact expanded name. - return "E:" + xdm.ClarkName(uri, nt.Local) + return spaceConflictExactPrefix + xdm.ClarkName(uri, nt.Local) } } @@ -1406,8 +1434,8 @@ func ruleCoversRegion(h, region nameTest) bool { // 1.0) use the earlier serialization spec, restricting them to {yes, no}; when // yesNoOnly is true any other lexical form (including true/false/1/0) is invalid. func outputBoolAttr(elem *xdm.Element, attr string, yesNoOnly bool) (bool, bool, error) { - v := getAttr(elem, attr) - if v == "" { + v, present := elem.GetAttribute(attr) + if !present { return false, false, nil } @@ -1602,7 +1630,7 @@ func checkAttributeSetCycles(ss *Stylesheet) error { case done: return nil case visiting: - if ss.processorLanguage() >= 30 { + if ss.processorLanguage() >= processorXSLT30 { return nil } diff --git a/transform/internal/xslt/compile_functions_modes.go b/transform/internal/xslt/compile_functions_modes.go index b46650f..f895141 100644 --- a/transform/internal/xslt/compile_functions_modes.go +++ b/transform/internal/xslt/compile_functions_modes.go @@ -14,11 +14,11 @@ import ( ) func (c *compiler) compileGlobalContextItem(ctx context.Context, elem *xdm.Element) error { - if err := c.validateXSLTAttrs(ctx, elem, attributeNames("as", lexicon.AttrUse, xslAttrUseWhen)); err != nil { + if err := c.validateXSLTAttrs(ctx, elem, attributeNames(nameAs, lexicon.AttrUse, xslAttrUseWhen)); err != nil { return err } - asAttr := getAttr(elem, "as") + asAttr := getAttr(elem, nameAs) // Capture the declaration-site static namespace context BEFORE validating // the @as sequence type, so that schema-element()/schema-attribute() (and @@ -36,7 +36,7 @@ func (c *compiler) compileGlobalContextItem(ctx context.Context, elem *xdm.Eleme xpathDefaultNS := c.xpathDefaultNS hasXPathDefaultNS := c.xpathDefaultNS != "" - if xdn, has := elem.GetAttribute(nameXpathDefaultNamespace); has { + if xdn, has := elem.GetAttribute(nameXPathDefaultNamespace); has { xpathDefaultNS = xdn hasXPathDefaultNS = true } @@ -104,7 +104,7 @@ func (c *compiler) compileGlobalContextItem(ctx context.Context, elem *xdm.Eleme // declarationTypeKey captures type identity in the declaration's static scope. func (c *compiler) declarationTypeKey(elem *xdm.Element, as string) string { if strings.TrimSpace(as) == "" { - as = "item()*" + as = lexicon.NodeTestItem + "*" } defaultNS, hasDefault := c.declarationDefaultNamespace(elem) @@ -120,9 +120,9 @@ func (c *compiler) declarationDefaultNamespace(elem *xdm.Element) (string, bool) for node := xdm.Node(elem); node != nil; node = node.Parent() { if e, ok := node.(*xdm.Element); ok { - value, present := e.GetAttribute(nameXpathDefaultNamespace) + value, present := e.GetAttribute(nameXPathDefaultNamespace) if e.URI() != lexicon.NamespaceXSLT { - value, present = e.GetAttributeNS(nameXpathDefaultNamespace, lexicon.NamespaceXSLT) + value, present = e.GetAttributeNS(nameXPathDefaultNamespace, lexicon.NamespaceXSLT) } if present { @@ -138,7 +138,7 @@ func (c *compiler) declarationDefaultNamespace(elem *xdm.Element) (string, bool) func (c *compiler) compileFunction(ctx context.Context, elem *xdm.Element) error { defer c.pushElementVersion(elem)() - if err := c.validateXSLTAttrs(ctx, elem, attributeNames(xslAttrName, "as", xslAttrVisibility, nameStreamable, "streamability", "override-extension-function", xslElemOverride, "identity-sensitive", "cache", "new-each-time", xslAttrUseWhen)); err != nil { + if err := c.validateXSLTAttrs(ctx, elem, attributeNames(xslAttrName, nameAs, xslAttrVisibility, nameStreamable, nameStreamability, nameOverrideExtensionFunction, xslElemOverride, nameIdentitySensitive, nameCache, nameNewEachTime, xslAttrUseWhen)); err != nil { return err } @@ -195,13 +195,13 @@ func (c *compiler) compileFunction(ctx context.Context, elem *xdm.Element) error return err } - fnAs := getAttr(elem, "as") + fnAs := getAttr(elem, nameAs) if err := c.validateAsSequenceType(ctx, elem, fnAs, "xsl:function "+name); err != nil { return err } // XTSE3155: xsl:function with no params and streamability != unclassified - streamability := getAttr(elem, "streamability") + streamability := getAttr(elem, nameStreamability) if streamability == "" { streamability = getAttr(elem, nameStreamable) switch streamability { @@ -222,10 +222,10 @@ func (c *compiler) compileFunction(ctx context.Context, elem *xdm.Element) error Params: params, Body: body, As: fnAs, - Cache: xsdBoolTrue(getAttr(elem, "cache")), + Cache: xsdBoolTrue(getAttr(elem, nameCache)), Streamability: streamability, Visibility: getAttr(elem, xslAttrVisibility), - NewEachTime: getAttr(elem, "new-each-time"), + NewEachTime: getAttr(elem, nameNewEachTime), ImportPrec: c.importPrec, } if c.stylesheet.isPackage { @@ -269,9 +269,9 @@ func (c *compiler) validateFunctionParameter(childElem *xdm.Element) error { return nil } - if c.stylesheet.processorLanguage() == 20 { - if value, present := childElem.GetAttribute(ctxItemRequired); present { - if err := validateBooleanAttr("xsl:param", ctxItemRequired, value); err != nil { + if c.stylesheet.processorLanguage() == processorXSLT20 { + if value, present := childElem.GetAttribute(xslAttrRequired); present { + if err := validateBooleanAttr("xsl:param", xslAttrRequired, value); err != nil { return err } @@ -279,8 +279,8 @@ func (c *compiler) validateFunctionParameter(childElem *xdm.Element) error { } } - if reqVal, hasReq := childElem.GetAttribute(ctxItemRequired); hasReq { - if reqVal != lexicon.ValueYes && reqVal != "1" && reqVal != lexicon.ValueTrue { + if reqVal, hasReq := childElem.GetAttribute(xslAttrRequired); hasReq { + if reqVal != lexicon.ValueYes && reqVal != booleanDigitTrue && reqVal != lexicon.ValueTrue { pname := getAttr(childElem, xslAttrName) return staticError(errCodeXTSE0020, @@ -308,7 +308,7 @@ func (c *compiler) validateFunctionParameter(childElem *xdm.Element) error { } func (c *compiler) compileMode(ctx context.Context, elem *xdm.Element) error { - if err := c.validateXSLTAttrs(ctx, elem, attributeNames(xslAttrName, nameStreamable, "on-no-match", "on-multiple-match", "warning-on-no-match", "warning-on-multiple-match", nameTyped, xslAttrVisibility, xslAttrUseWhen, nameUseAccumulators)); err != nil { + if err := c.validateXSLTAttrs(ctx, elem, attributeNames(xslAttrName, nameStreamable, nameOnNoMatch, nameOnMultipleMatch, nameWarningOnNoMatch, nameWarningOnMultipleMatch, nameTyped, xslAttrVisibility, xslAttrUseWhen, nameUseAccumulators)); err != nil { return err } // xsl:mode must be empty (no children) @@ -337,7 +337,7 @@ func (c *compiler) modeDefinition(ctx context.Context, elem *xdm.Element) (*mode return nil, "", err } - md := &modeDef{Name: name, Streamable: streamable, ImportPrec: c.importPrec} + md := &modeDef{Name: name, Streamable: streamable, HasStreamable: streamableStr != "", ImportPrec: c.importPrec} if err := readModePolicies(elem, md); err != nil { return nil, "", err } @@ -378,7 +378,7 @@ func (c *compiler) modeDeclarationName(_ context.Context, elem *xdm.Element) (st return modeDefault, nil } - return modeQName(strings.Trim(raw, " \t\r\n"), elem, errCodeXTSE0020) + return modeQName(strings.Trim(raw, lexicon.XMLWhitespace), elem, errCodeXTSE0020) } func (c *compiler) modeAccumulatorNames(elem *xdm.Element) *string { @@ -393,7 +393,7 @@ func (c *compiler) modeAccumulatorNames(elem *xdm.Element) *string { var resolvedParts []string for tok := range strings.FieldsSeq(rawUA) { - if tok == "#all" { + if tok == lexicon.ModeAll { resolvedParts = append(resolvedParts, tok) } else { resolvedParts = append(resolvedParts, resolveQName(tok, c.nsBindings)) @@ -417,7 +417,9 @@ func (c *compiler) mergeModeImportPrecedence(existing, md *modeDef, streamableSt md.conflictOnNoMatch = existing.conflictOnNoMatch && md.OnNoMatch == "" md.conflictVisibility = existing.conflictVisibility && md.Visibility == "" md.conflictOnMultiple = existing.conflictOnMultiple && md.OnMultipleMatch == "" + md.conflictAccumulator = existing.conflictAccumulator && md.UseAccumulators == nil + inheritModeStreamable(md, existing) // Inherit attributes not explicitly set in higher-precedence decl if md.OnNoMatch == "" { md.OnNoMatch = existing.OnNoMatch @@ -438,6 +440,8 @@ func (c *compiler) mergeModeImportPrecedence(existing, md *modeDef, streamableSt c.stylesheet.modeDefs[name] = md } else { // Lower precedence than existing: merge unspecified attrs into existing. + inheritModeStreamable(existing, md) + if existing.OnNoMatch == "" && md.OnNoMatch != "" { existing.OnNoMatch = md.OnNoMatch } @@ -456,6 +460,12 @@ func (c *compiler) mergeModeImportPrecedence(existing, md *modeDef, streamableSt } } +func inheritModeStreamable(target, source *modeDef) { + if !target.HasStreamable && source.HasStreamable { + target.Streamable, target.HasStreamable = source.Streamable, true + } +} + // canonicalSequenceTypeKey returns a namespace-resolved canonical string for an // @as sequence type. It reuses the xpath sequence-type parseOptions AST and the single // resolveSequenceTypeQName resolver so that the XTSE3087 cross-module agreement @@ -509,8 +519,8 @@ func inScopeNamespaces(elem *xdm.Element) map[string]string { func validateFunctionOptions(elem *xdm.Element) error { // XTSE0020: validate override-extension-function (boolean) - if oef, has := elem.GetAttribute("override-extension-function"); has { - if err := validateBooleanAttr("xsl:function", "override-extension-function", oef); err != nil { + if oef, has := elem.GetAttribute(nameOverrideExtensionFunction); has { + if err := validateBooleanAttr("xsl:function", nameOverrideExtensionFunction, oef); err != nil { return err } } @@ -521,7 +531,7 @@ func validateFunctionOptions(elem *xdm.Element) error { } // Having both @override and @override-extension-function is only an error // when they disagree (XTSE0020 per spec function-0117). - if oef, hasOEF := elem.GetAttribute("override-extension-function"); hasOEF { + if oef, hasOEF := elem.GetAttribute(nameOverrideExtensionFunction); hasOEF { ovBool, _ := parseXSDBool(ov) oefBool, _ := parseXSDBool(oef) @@ -532,7 +542,7 @@ func validateFunctionOptions(elem *xdm.Element) error { } } // XTSE0020: validate new-each-time (yes|no|maybe) - if net := getAttr(elem, "new-each-time"); net != "" { + if net := getAttr(elem, nameNewEachTime); net != "" { switch net { case lexicon.ValueYes, lexicon.ValueNo, nameMaybe: // valid @@ -579,7 +589,7 @@ func parseModeStreamable(elem *xdm.Element) (bool, string, error) { func readModePolicies(elem *xdm.Element, md *modeDef) error { // Validate on-no-match - md.OnNoMatch = strings.TrimSpace(getAttr(elem, "on-no-match")) + md.OnNoMatch = strings.TrimSpace(getAttr(elem, nameOnNoMatch)) if md.OnNoMatch != "" { switch md.OnNoMatch { case onNoMatchTextOnlyCopy, onNoMatchShallowCopy, onNoMatchDeepCopy, onNoMatchShallowSkip, onNoMatchDeepSkip, onNoMatchFail: @@ -596,7 +606,7 @@ func readModePolicies(elem *xdm.Element, md *modeDef) error { } if raw, present := elem.GetAttribute(xslAttrVisibility); present { - md.Visibility = strings.Trim(raw, " \t\r\n") + md.Visibility = strings.Trim(raw, lexicon.XMLWhitespace) switch md.Visibility { case visPublic, visPrivate, visFinal: default: @@ -608,7 +618,7 @@ func readModePolicies(elem *xdm.Element, md *modeDef) error { return staticError(errCodeXTSE0020, "unnamed mode cannot have visibility %q", md.Visibility) } - md.OnMultipleMatch = strings.TrimSpace(getAttr(elem, "on-multiple-match")) + md.OnMultipleMatch = strings.TrimSpace(getAttr(elem, nameOnMultipleMatch)) if md.OnMultipleMatch != "" { switch md.OnMultipleMatch { case onMultipleMatchUseLast, onNoMatchFail: @@ -623,13 +633,13 @@ func readModePolicies(elem *xdm.Element, md *modeDef) error { func validateModeFlags(elem *xdm.Element) error { // Validate boolean attributes - if v := getAttr(elem, "warning-on-no-match"); v != "" { + if v := getAttr(elem, nameWarningOnNoMatch); v != "" { if _, ok := parseXSDBool(v); !ok { return staticError(errCodeXTSE0020, "invalid value %q for warning-on-no-match on xsl:mode", v) } } - if v := getAttr(elem, "warning-on-multiple-match"); v != "" { + if v := getAttr(elem, nameWarningOnMultipleMatch); v != "" { if _, ok := parseXSDBool(v); !ok { return staticError(errCodeXTSE0020, "invalid value %q for warning-on-multiple-match on xsl:mode", v) } @@ -659,8 +669,12 @@ func mergeSamePrecedenceMode(existing, md *modeDef, hasStreamable bool) { existing.conflictOnNoMatch = true } - if hasStreamable && existing.Streamable != md.Streamable { - existing.conflictStreamable = true + if hasStreamable { + if existing.HasStreamable && existing.Streamable != md.Streamable { + existing.conflictStreamable = true + } + + existing.Streamable, existing.HasStreamable = md.Streamable, true } if existing.Visibility != "" && md.Visibility != "" && existing.Visibility != md.Visibility { @@ -791,6 +805,10 @@ func (writer canonicalTypeWriter) sequenceType(st xdm.SequenceType) { // resolveSequenceTypeQName, so prefixes and default-namespace differences are // normalized away. func (writer canonicalTypeWriter) nodeTest(nt xdm.NodeTest) { + const ( + unknownSequenceTypeMarker = "?unknown?" + ) + switch t := nt.(type) { case xdm.AtomicOrUnionType: qn := t.Name @@ -800,7 +818,7 @@ func (writer canonicalTypeWriter) nodeTest(nt xdm.NodeTest) { writer.qname(qn, qnameTypeName) case xdm.ElementTest: - writer.b.WriteString("element(") + writer.b.WriteString(lexicon.KindElement + "(") writer.qname(t.Name, qnameElementName) if t.TypeName != "" { @@ -814,7 +832,7 @@ func (writer canonicalTypeWriter) nodeTest(nt xdm.NodeTest) { writer.b.WriteByte(')') case xdm.AttributeTest: - writer.b.WriteString("attribute(") + writer.b.WriteString(lexicon.KindAttribute + "(") writer.qname(t.Name, qnameAttributeName) if t.TypeName != "" { @@ -824,15 +842,15 @@ func (writer canonicalTypeWriter) nodeTest(nt xdm.NodeTest) { writer.b.WriteByte(')') case xdm.SchemaElementTest: - writer.b.WriteString("schema-element(") + writer.b.WriteString(lexicon.KindSchemaElement + "(") writer.qname(t.Name, qnameElementName) writer.b.WriteByte(')') case xdm.SchemaAttributeTest: - writer.b.WriteString("schema-attribute(") + writer.b.WriteString(lexicon.KindSchemaAttribute + "(") writer.qname(t.Name, qnameAttributeName) writer.b.WriteByte(')') case xdm.DocumentTest: - writer.b.WriteString("document-node(") + writer.b.WriteString(lexicon.KindDocumentNode + "(") if t.Inner != nil { writer.nodeTest(t.Inner) @@ -840,27 +858,27 @@ func (writer canonicalTypeWriter) nodeTest(nt xdm.NodeTest) { writer.b.WriteByte(')') case xdm.PITest: - writer.b.WriteString("processing-instruction(") + writer.b.WriteString(lexicon.KindPI + "(") writer.b.WriteString(t.Target) writer.b.WriteByte(')') case xdm.TypeTest: switch t.Kind { case xdm.NodeKindText: - writer.b.WriteString("text()") + writer.b.WriteString(lexicon.NodeTestText) case xdm.NodeKindComment: - writer.b.WriteString("comment()") + writer.b.WriteString(lexicon.NodeTestComment) case xdm.NodeKindProcessingInstruction: - writer.b.WriteString("processing-instruction()") + writer.b.WriteString(lexicon.NodeTestPI) default: - writer.b.WriteString("node()") + writer.b.WriteString(lexicon.NodeTestNode) } case xdm.NamespaceNodeTest: - writer.b.WriteString("namespace-node()") + writer.b.WriteString(lexicon.KindNamespaceNode + "()") case xdm.AnyItemTest: - writer.b.WriteString("item()") + writer.b.WriteString(lexicon.NodeTestItem) case xdm.NameTest: qn := t.Local - if t.URI != "" { + if t.URIQualified || t.URI != "" { qn = "Q{" + t.URI + "}" + t.Local } else if t.Prefix != "" { qn = t.Prefix + ":" + t.Local @@ -871,28 +889,28 @@ func (writer canonicalTypeWriter) nodeTest(nt xdm.NodeTest) { writer.functionTest(t) case xdm.MapTest: if t.AnyType { - writer.b.WriteString("map(*)") + writer.b.WriteString(sequenceTypeMapStart + "*)") return } - writer.b.WriteString("map(") + writer.b.WriteString(sequenceTypeMapStart) writer.nodeTest(t.KeyType) writer.b.WriteByte(',') writer.sequenceType(t.ValType) writer.b.WriteByte(')') case xdm.ArrayTest: if t.AnyType { - writer.b.WriteString("array(*)") + writer.b.WriteString(sequenceTypeArrayStart + "*)") return } - writer.b.WriteString("array(") + writer.b.WriteString(sequenceTypeArrayStart) writer.sequenceType(t.MemberType) writer.b.WriteByte(')') default: // Unknown/absent test: a stable placeholder so two unknowns compare equal // without misrepresenting them as item(). - writer.b.WriteString("?unknown?") + writer.b.WriteString(unknownSequenceTypeMarker) } } @@ -911,11 +929,11 @@ func (writer canonicalTypeWriter) qname(qname string, kind qnameKind) { func (writer canonicalTypeWriter) functionTest(t xdm.FunctionTest) { if t.AnyFunction { - writer.b.WriteString("function(*)") + writer.b.WriteString(sequenceTypeFunctionStart + "*)") return } - writer.b.WriteString("function(") + writer.b.WriteString(sequenceTypeFunctionStart) for i, pt := range t.ParamTypes { if i > 0 { @@ -935,8 +953,8 @@ func (writer canonicalTypeWriter) functionTest(t xdm.FunctionTest) { func isBuiltinXSDLocalName(local string) bool { switch local { case lexicon.TypeString, lexicon.TypeInteger, lexicon.TypeDecimal, lexicon.TypeDouble, lexicon.TypeFloat, - lexicon.TypeBoolean, "date", lexicon.TypeDateTime, lexicon.TypeTime, lexicon.TypeDuration, - lexicon.TypeDayTimeDuration, lexicon.TypeYearMonthDuration, "anyURI", lexicon.TypeUntypedAtomic: + lexicon.TypeBoolean, lexicon.TypeDate, lexicon.TypeDateTime, lexicon.TypeTime, lexicon.TypeDuration, + lexicon.TypeDayTimeDuration, lexicon.TypeYearMonthDuration, lexicon.TypeAnyURI, lexicon.TypeUntypedAtomic: return true } diff --git a/transform/internal/xslt/compile_imports.go b/transform/internal/xslt/compile_imports.go index 5775d10..aee5843 100644 --- a/transform/internal/xslt/compile_imports.go +++ b/transform/internal/xslt/compile_imports.go @@ -47,6 +47,10 @@ func (c *compiler) enterModuleNamespaceScope(root *xdm.Element) func() { // extension-element-prefixes to the set of namespace URIs to exclude from result // trees. inScope is the module root's in-scope namespaces. func (c *compiler) moduleResultPrefixExcludes(root *xdm.Element, inScope map[string]string) map[string]struct{} { + const ( + moduleExclusionInitialCapacity = 4 + ) + erp := getAttr(root, nameExcludeResultPrefixes) eep := getAttr(root, nameExtensionElementPrefixes) @@ -54,7 +58,7 @@ func (c *compiler) moduleResultPrefixExcludes(root *xdm.Element, inScope map[str return nil } - out := make(map[string]struct{}, 4) + out := make(map[string]struct{}, moduleExclusionInitialCapacity) addPrefix := func(prefix string) { if prefix == lexicon.ModeDefault { @@ -411,7 +415,7 @@ func (c *compiler) compileIncludeTemplates(ctx context.Context, elem *xdm.Elemen savedXPathDefaultNS := c.xpathDefaultNS savedHasXPathDefaultNS := c.hasXPathDefaultNS - if xdn, ok := root.GetAttribute(nameXpathDefaultNamespace); ok { + if xdn, ok := root.GetAttribute(nameXPathDefaultNamespace); ok { c.xpathDefaultNS = xdn c.hasXPathDefaultNS = true } @@ -427,13 +431,13 @@ func (c *compiler) compileIncludeTemplates(ctx context.Context, elem *xdm.Elemen // root — so an included module whose computed version is < 2.0 enters // backwards-compatible mode. savedVersion := c.effectiveVersion - if _, hasShadow := root.GetAttribute("_version"); hasShadow { - if err := c.resolveSingleShadowAttribute(ctx, root, lexicon.SerializationVersion); err != nil { + if _, hasShadow := root.GetAttribute(shadowAttrVersion); hasShadow { + if err := c.resolveSingleShadowAttribute(ctx, root, xslAttrVersion); err != nil { return err } } - if ver := getAttr(root, lexicon.SerializationVersion); ver != "" { + if ver := getAttr(root, xslAttrVersion); ver != "" { c.effectiveVersion = ver } @@ -618,13 +622,13 @@ func (c *compiler) compileExternalModule(ctx context.Context, doc *xdm.Document, // and included modules — so an imported module whose computed version is < 2.0 // enters backwards-compatible mode. savedVersion := c.effectiveVersion - if _, hasShadow := importedRoot.GetAttribute("_version"); hasShadow { - if err := c.resolveSingleShadowAttribute(ctx, importedRoot, lexicon.SerializationVersion); err != nil { + if _, hasShadow := importedRoot.GetAttribute(shadowAttrVersion); hasShadow { + if err := c.resolveSingleShadowAttribute(ctx, importedRoot, xslAttrVersion); err != nil { return err } } - if ver := getAttr(importedRoot, lexicon.SerializationVersion); ver != "" { + if ver := getAttr(importedRoot, xslAttrVersion); ver != "" { c.effectiveVersion = ver } @@ -717,7 +721,7 @@ func (c *compiler) enterModule(uri string, imported bool) (func(), error) { // original source remains available to document(""); only a private module tree // receives the implicit stylesheet/template wrappers. func expandSimplifiedModule(ctx context.Context, doc *xdm.Document, root *xdm.Element) (*xdm.Document, error) { - version := root.GetAttributeNodeNS(lexicon.SerializationVersion, lexicon.NamespaceXSLT) + version := root.GetAttributeNodeNS(xslAttrVersion, lexicon.NamespaceXSLT) if version == nil { return nil, staticError(errCodeXTSE0150, "simplified stylesheet requires xsl:version") } @@ -733,7 +737,7 @@ func expandSimplifiedModule(ctx context.Context, doc *xdm.Document, root *xdm.El wrapper := &tree.Node{ Kind: tree.ElementNode, Name: tree.Name{Space: lexicon.NamespaceXSLT, Local: nameStylesheet}, Prefix: version.Prefix(), SourceLine: literal.SourceLine, - NS: []tree.Namespace{{Prefix: version.Prefix(), URI: lexicon.NamespaceXSLT}}, Attrs: []tree.Attribute{{Name: tree.Name{Local: lexicon.SerializationVersion}, Value: version.Value()}}, + NS: []tree.Namespace{{Prefix: version.Prefix(), URI: lexicon.NamespaceXSLT}}, Attrs: []tree.Attribute{{Name: tree.Name{Local: xslAttrVersion}, Value: version.Value()}}, } // Extraction loses ancestor xml:space. Keep it on the synthetic wrapper so // re-normalization preserves the same text without adding an LRE attribute. @@ -742,7 +746,7 @@ func expandSimplifiedModule(ctx context.Context, doc *xdm.Document, root *xdm.El } if base := xpath.NodeBaseURI(root.Parent()); base != "" && base != doc.URL() { - wrapper.Attrs = append(wrapper.Attrs, tree.Attribute{Name: tree.Name{Space: lexicon.NamespaceXML, Local: "base"}, Prefix: "xml", Value: base}) + wrapper.Attrs = append(wrapper.Attrs, tree.Attribute{Name: tree.Name{Space: lexicon.NamespaceXML, Local: lexicon.AttrBase}, Prefix: lexicon.PrefixXML, Value: base}) } tmpl := &tree.Node{ @@ -770,7 +774,7 @@ func expandSimplifiedModule(ctx context.Context, doc *xdm.Document, root *xdm.El func inheritedStylesheetXMLSpace(node xdm.Node) string { for ; node != nil; node = node.Parent() { if elem, ok := node.(*xdm.Element); ok { - if space, present := elem.GetAttributeNS("space", lexicon.NamespaceXML); present { + if space, present := elem.GetAttributeNS(lexicon.AttrSpace, lexicon.NamespaceXML); present { return space } } @@ -871,7 +875,7 @@ func isWindowsDrivePath(s string) bool { // the boundary that descendant walks stop at. func moduleEffectiveBaseURI(root *xdm.Element, uri string) string { if _, isDoc := root.Parent().(*xdm.Document); isDoc { - if xmlBase, present := root.GetAttributeNS("base", lexicon.NamespaceXML); present { + if xmlBase, present := root.GetAttributeNS(lexicon.AttrBase, lexicon.NamespaceXML); present { return resolveRootXMLBase(uri, xmlBase) } @@ -887,7 +891,7 @@ func moduleEffectiveBaseURI(root *xdm.Element, uri string) string { continue } - if xmlBase, ok := elem.GetAttributeNS("base", lexicon.NamespaceXML); ok { + if xmlBase, ok := elem.GetAttributeNS(lexicon.AttrBase, lexicon.NamespaceXML); ok { bases = append(bases, xmlBase) } } @@ -938,7 +942,7 @@ func stylesheetBaseURI(n xdm.Node, fallback string, stopAt *xdm.Element) string } } - if xmlBase, ok := elem.GetAttributeNS("base", lexicon.NamespaceXML); ok { + if xmlBase, ok := elem.GetAttributeNS(lexicon.AttrBase, lexicon.NamespaceXML); ok { bases = append(bases, xmlBase) } // An external parsed entity supplies a new physical base, independent @@ -1041,11 +1045,11 @@ func findElementByID(doc *xdm.Document, id string) *xdm.Element { continue } - if v, found := elem.GetAttribute("id"); found && v == id { + if v, found := elem.GetAttribute(lexicon.AttrID); found && v == id { return elem } - if v, found := elem.GetAttributeNS("id", lexicon.NamespaceXML); found && v == id { + if v, found := elem.GetAttributeNS(lexicon.AttrID, lexicon.NamespaceXML); found && v == id { return elem } diff --git a/transform/internal/xslt/compile_instructions.go b/transform/internal/xslt/compile_instructions.go index 13d1bf4..0c68a51 100644 --- a/transform/internal/xslt/compile_instructions.go +++ b/transform/internal/xslt/compile_instructions.go @@ -20,7 +20,7 @@ import ( // Attribute predicates validate compile-time vocabulary without mutable tables. func withParamAllowedAttrs(name string) bool { switch name { - case xslAttrName, xslAttrSelect, "as", nameTunnel: + case xslAttrName, xslAttrSelect, nameAs, nameTunnel: return true default: return false @@ -29,7 +29,7 @@ func withParamAllowedAttrs(name string) bool { func paramAllowedAttrs(name string) bool { switch name { - case xslAttrName, xslAttrSelect, "as", ctxItemRequired, nameTunnel, nameStatic: + case xslAttrName, xslAttrSelect, nameAs, xslAttrRequired, nameTunnel, nameStatic: return true default: return false @@ -38,7 +38,7 @@ func paramAllowedAttrs(name string) bool { func variableAllowedAttrs(name string) bool { switch name { - case xslAttrName, xslAttrSelect, "as", nameStatic, xslAttrVisibility: + case xslAttrName, xslAttrSelect, nameAs, nameStatic, xslAttrVisibility: return true default: return false @@ -47,8 +47,8 @@ func variableAllowedAttrs(name string) bool { func lreAllowedXSLTAttrs(name string) bool { switch name { - case nameUseAttributeSets, nameExpandText, nameXpathDefaultNamespace, nameExcludeResultPrefixes, - nameExtensionElementPrefixes, lexicon.SerializationVersion, lexicon.AttrType, nameValidation, + case nameUseAttributeSets, nameExpandText, nameXPathDefaultNamespace, nameExcludeResultPrefixes, + nameExtensionElementPrefixes, xslAttrVersion, lexicon.AttrType, nameValidation, nameDefaultCollation, nameDefaultMode, nameDefaultValidation, nameInheritNamespaces, xslAttrUseWhen: return true @@ -78,7 +78,7 @@ func attributeNames(names ...string) func(string) bool { // In forwards-compatible mode (version > 3.0), unknown attributes are silently ignored // per XSLT 3.0 §3.8. func (c *compiler) validateXSLTAttrs(ctx context.Context, elem *xdm.Element, allowed func(string) bool) error { - if c.stylesheet.processorLanguage() >= 20 { + if c.stylesheet.processorLanguage() >= processorXSLT20 { if err := checkComponentNames(elem, c.stylesheet.processorLanguage()); err != nil { return err } @@ -91,7 +91,7 @@ func (c *compiler) validateXSLTAttrs(ctx context.Context, elem *xdm.Element, all fc := c.forwardsCompatible(c.effectiveVersion) if !fc { // Also check the element's own version attribute - if elemVer := getAttr(elem, lexicon.SerializationVersion); elemVer != "" { + if elemVer := getAttr(elem, xslAttrVersion); elemVer != "" { fc = c.forwardsCompatible(elemVer) } } @@ -242,14 +242,14 @@ func (c *compiler) compileInstruction(ctx context.Context, elem *xdm.Element) (i // directly from normal instructions whose static context and source are recorded. func (c *compiler) compileInstructionPayload(ctx context.Context, elem *xdm.Element) (instruction, bool, error) { if elem.URI() == lexicon.NamespaceXSLT { - if c.stylesheet.processorLanguage() == 10 { + if c.stylesheet.processorLanguage() == processorXSLT10 { if _, known := lookupXSLT10Attribute(elem.LocalName()); !known { inst, err := c.compileForwardsCompat(ctx, elem) return inst, false, err } } - if c.stylesheet.processorLanguage() == 20 && unavailableInstruction(elem.LocalName(), 20) && c.forwardsCompatible(c.effectiveVersion) { + if c.stylesheet.processorLanguage() == processorXSLT20 && unavailableInstruction(elem.LocalName(), processorXSLT20) && c.forwardsCompatible(c.effectiveVersion) { inst, err := c.compileForwardsCompat(ctx, elem) return inst, false, err } @@ -300,7 +300,7 @@ func (c *compiler) prepareInstructionAttributes(ctx context.Context, elem *xdm.E // their existing fallback path; unknown attributes obey forwards compatibility. func (c *compiler) validateKnownElementAttributes(ctx context.Context, elem *xdm.Element) error { name := elem.LocalName() - if name == fnNameTransform { + if name == lexicon.XSLTElementTransform { name = nameStylesheet } @@ -339,12 +339,12 @@ func (c *compiler) setInstructionXPathDefaultNS(elem *xdm.Element) bool { hasLocalXPNS := false if elem.URI() == lexicon.NamespaceXSLT { - if xdn, ok := elem.GetAttribute(nameXpathDefaultNamespace); ok { + if xdn, ok := elem.GetAttribute(nameXPathDefaultNamespace); ok { c.xpathDefaultNS = xdn c.hasXPathDefaultNS = true hasLocalXPNS = true } - } else if xdn, ok := elem.GetAttributeNS(nameXpathDefaultNamespace, lexicon.NamespaceXSLT); ok { + } else if xdn, ok := elem.GetAttributeNS(nameXPathDefaultNamespace, lexicon.NamespaceXSLT); ok { c.xpathDefaultNS = xdn c.hasXPathDefaultNS = true hasLocalXPNS = true @@ -502,7 +502,7 @@ func (c *compiler) evaluateUseWhen(ctx context.Context, expr string) (bool, erro compiled = compiled.BindNamespaceContext(c.xpathNamespaces(c.nsBindings), c.nsBindings[""]) eval := c.useWhenEvaluator(ctx) - if c.stylesheet.processorLanguage() >= 20 { + if c.stylesheet.processorLanguage() >= processorXSLT20 { if err := eval.ValidateExpression(ctx, compiled); err != nil { return false, err } @@ -772,6 +772,10 @@ func (c *compiler) compileUnknownXSLTInstruction(ctx context.Context, elem *xdm. } func (c *compiler) compileResultDocument(ctx context.Context, elem *xdm.Element) (instruction, error) { + const ( + itemSeparatorAbsentToken = "#absent" + ) + inst := &resultDocumentInst{} if href, present := elem.GetAttribute(lexicon.AttrHref); present { @@ -787,7 +791,7 @@ func (c *compiler) compileResultDocument(ctx context.Context, elem *xdm.Element) return nil, err } - if fmtAttr := getAttr(elem, "format"); fmtAttr != "" { + if fmtAttr := getAttr(elem, nameFormat); fmtAttr != "" { if strings.ContainsAny(fmtAttr, "{}") { avt, err := c.compileAVT(fmtAttr, c.nsBindings) if err != nil { @@ -801,10 +805,10 @@ func (c *compiler) compileResultDocument(ctx context.Context, elem *xdm.Element) } inst.Method = getAttr(elem, lexicon.SerializationMethod) - if is := getAttr(elem, lexicon.SerializationItemSeparator); is != "" { + if is, present := elem.GetAttribute(lexicon.SerializationItemSeparator); present { inst.ItemSeparatorSet = true // item-separator="#absent" means explicitly absent (use default, blocks format inheritance) - if is != "#absent" { + if is != itemSeparatorAbsentToken { avt, err := c.compileAVT(is, c.nsBindings) if err != nil { return nil, err @@ -868,7 +872,7 @@ func (c *compiler) compileResultDocumentOutputAVTs(elem *xdm.Element, inst *resu attr string dst **avt }{ - {"output-version", &inst.OutputVersion}, + {nameOutputVersion, &inst.OutputVersion}, {lexicon.SerializationEncoding, &inst.Encoding}, {lexicon.SerializationIndent, &inst.Indent}, {lexicon.SerializationOmitXMLDeclaration, &inst.OmitXMLDeclaration}, @@ -950,7 +954,7 @@ func (c *compiler) compileForwardsCompat(ctx context.Context, elem *xdm.Element) return nil, err } - if !inst.HasFallback && c.stylesheet.processorLanguage() >= 20 { + if !inst.HasFallback && c.stylesheet.processorLanguage() >= processorXSLT20 { return nil, staticError(errCodeXTSE0010, "unknown XSLT instruction xsl:%s has no xsl:fallback", elem.LocalName()) } @@ -1143,7 +1147,7 @@ func (c *compiler) compileLocalVariable(ctx context.Context, elem *xdm.Element) return nil, err } - asAttr := c.resolveAsType(ctx, getAttr(elem, "as")) + asAttr := c.resolveAsType(ctx, getAttr(elem, nameAs)) if err := c.validateAsSequenceType(ctx, elem, asAttr, "xsl:variable "+name); err != nil { return nil, err } @@ -1189,14 +1193,14 @@ func (c *compiler) compileMessage(ctx context.Context, elem *xdm.Element) (*mess inst.Body = body - if termAttr, present := elem.GetAttribute("terminate"); present { + if termAttr, present := elem.GetAttribute(nameTerminate); present { avt, err := c.compileAVT(termAttr, c.nsBindings) if err != nil { return nil, err } // Validate literal values using the selected processor edition. if sv, ok := avt.staticValue(); ok { - if _, valid := parseMessageTerminate(sv, c.stylesheet.processorLanguage() >= 30); !valid { + if _, valid := parseMessageTerminate(sv, c.stylesheet.processorLanguage() >= processorXSLT30); !valid { return nil, staticError(errCodeXTSE0020, "%q is not a valid value for xsl:message/@terminate", sv) } } @@ -1204,7 +1208,7 @@ func (c *compiler) compileMessage(ctx context.Context, elem *xdm.Element) (*mess inst.Terminate = avt } - if errorCodeAttr, present := elem.GetAttribute("error-code"); present { + if errorCodeAttr, present := elem.GetAttribute(nameErrorCode); present { avt, err := c.compileMessageErrorCode(errorCodeAttr) if err != nil { return nil, err @@ -1226,7 +1230,7 @@ func (c *compiler) compileMessageErrorCode(value string) (*avt, error) { } if literal, known := code.staticValue(); known { - literal = strings.Trim(literal, " \t\r\n") + literal = strings.Trim(literal, lexicon.XMLWhitespace) if xmlchar.IsValidQName(literal) { if prefix, _, prefixed := strings.Cut(literal, ":"); prefixed && c.nsBindings[prefix] == "" { return nil, staticError(errCodeXTSE0280, "undeclared error-code prefix %q", prefix) @@ -1249,7 +1253,7 @@ func (c *compiler) compileMap(ctx context.Context, elem *xdm.Element) (*mapInst, func (c *compiler) compileMapEntry(ctx context.Context, elem *xdm.Element) (*mapEntryInst, error) { inst := &mapEntryInst{} - keyAttr, hasKey := elem.GetAttribute(nameKey2) + keyAttr, hasKey := elem.GetAttribute(keyLocalName) if hasKey { expr, err := c.compileXPath(keyAttr, c.nsBindings) if err != nil { @@ -1305,13 +1309,13 @@ func (c *compiler) compileAssert(ctx context.Context, elem *xdm.Element) (instru inst := &assertInst{Test: testExpr} // xpath-default-namespace - if xdn := getAttr(elem, nameXpathDefaultNamespace); xdn != "" { + if xdn := getAttr(elem, nameXPathDefaultNamespace); xdn != "" { inst.XPathDefaultNS = xdn inst.HasXPathDefaultNS = true } // error-code attribute - if ec, present := elem.GetAttribute("error-code"); present { + if ec, present := elem.GetAttribute(nameErrorCode); present { code, err := c.compileMessageErrorCode(ec) if err != nil { return nil, err @@ -1346,8 +1350,8 @@ func (c *compiler) compileAssert(ctx context.Context, elem *xdm.Element) (instru func xsltStandardAttrs(name string) bool { switch name { case nameDefaultCollation, nameDefaultMode, nameDefaultValidation, nameExcludeResultPrefixes, - nameExpandText, nameExtensionElementPrefixes, xslAttrUseWhen, nameXpathDefaultNamespace, - lexicon.SerializationVersion: + nameExpandText, nameExtensionElementPrefixes, xslAttrUseWhen, nameXPathDefaultNamespace, + xslAttrVersion: return true default: return false @@ -1358,7 +1362,7 @@ func xsltStandardAttrs(name string) bool { // namespace (http://www.w3.org/2001/XMLSchema). It recognises the conventional // xs: and xsd: prefixes directly, and also resolves any other prefix via ns. func isBuiltinXSDTypeName(typeName string, ns map[string]string) bool { - if strings.HasPrefix(typeName, "xs:") || strings.HasPrefix(typeName, "xsd:") { + if strings.HasPrefix(typeName, lexicon.PrefixXS+":") || strings.HasPrefix(typeName, lexicon.PrefixXSD+":") { return true } @@ -1392,17 +1396,17 @@ func useWhenFunctionAvailable(ctx context.Context, args []xdm.Sequence, processo case fnNameFunctionAvailable, funcSystemProperty, fnNameTypeAvailable, fnNameElementAvailable: return xdm.SingleBoolean(xsltFunctionAcceptsArity(name.Local, arity) || arity < 0), nil case funcAvailableSystemProperties: - return xdm.SingleBoolean(processor >= 30 && (arity < 0 || arity == 0)), nil + return xdm.SingleBoolean(processor >= processorXSLT30 && (arity < 0 || arity == 0)), nil } } - version := 31 - if processor == 20 { - version = 20 + version := xpathProfile31 + if processor == processorXSLT20 { + version = xpathProfile20 } - if processor == 10 { - version = 10 + if processor == processorXSLT10 { + version = xpathProfile10 } return xdm.SingleBoolean(xpath.BuiltinFunctionAvailable(version, name.URI, name.Local, arity)), nil @@ -1423,7 +1427,7 @@ func validateResultDocumentOutputAttrs(elem *xdm.Element) error { // Validate html-version: must be a decimal number if present and not an avt if hv := getAttr(elem, lexicon.SerializationHTMLVersion); hv != "" { if !strings.ContainsAny(hv, "{}") { - if _, err := strconv.ParseFloat(hv, 64); err != nil { + if _, err := strconv.ParseFloat(hv, float64ParseBits); err != nil { return staticError(errCodeXTSE0020, "%q is not a valid value for xsl:result-document/@html-version", hv) } } @@ -1446,7 +1450,7 @@ func validateResultDocumentOutputAttrs(elem *xdm.Element) error { if !strings.ContainsAny(v, "{}") { sv := strings.TrimSpace(v) switch sv { - case lexicon.ValueYes, lexicon.ValueNo, lexicon.ValueOmit, lexicon.ValueTrue, lexicon.ValueFalse, "1", "0": + case lexicon.ValueYes, lexicon.ValueNo, lexicon.ValueOmit, lexicon.ValueTrue, lexicon.ValueFalse, booleanDigitTrue, booleanDigitFalse: // valid default: return staticError(lexicon.ErrSEPM0016, "%q is not a valid value for xsl:result-document/@standalone", v) @@ -1513,6 +1517,18 @@ func (state *sequenceConstructorState) add(inst instruction) error { } state.body = append(state.body, inst) + + // Retain lexical collation scopes in the body, but classify the instruction + // they contain when enforcing sequence-constructor ordering. + for { + scope, wrapped := inst.(*collationScopeInst) + if !wrapped { + break + } + + inst = scope.Inner + } + switch inst.(type) { case *breakInst, *nextIterationInst: state.sawTerminator = true diff --git a/transform/internal/xslt/compile_instructions_flow.go b/transform/internal/xslt/compile_instructions_flow.go index 8c16e65..a653fb7 100644 --- a/transform/internal/xslt/compile_instructions_flow.go +++ b/transform/internal/xslt/compile_instructions_flow.go @@ -73,7 +73,7 @@ func (c *compiler) compileApplyTemplates(ctx context.Context, elem *xdm.Element) func (c *compiler) compileApplyTemplatesMode(ctx context.Context, elem *xdm.Element) (string, error) { raw, present := elem.GetAttribute(lexicon.AttrMode) - mode := strings.Trim(raw, " \t\r\n") + mode := strings.Trim(raw, lexicon.XMLWhitespace) var err error if !present || mode == modeDefault { @@ -99,7 +99,7 @@ func (c *compiler) compileApplyTemplatesMode(ctx context.Context, elem *xdm.Elem func (c *compiler) appendCompiledSortKey(ctx context.Context, elem *xdm.Element, keys *[]*sortKey) error { if len(*keys) > 0 { - if _, present := elem.GetAttribute("stable"); present { + if _, present := elem.GetAttribute(nameStable); present { return staticError(errCodeXTSE1017, "the stable attribute is permitted only on the first xsl:sort element") } } @@ -155,7 +155,7 @@ func (c *compiler) compileCallTemplate(ctx context.Context, elem *xdm.Element) ( } inst := &callTemplateInst{Name: resolveQName(name, namespaces), Compat: c.backwardsCompatible()} - c.pendingTemplateCalls = append(c.pendingTemplateCalls, templateCallReference{Name: inst.Name, LegacyForwards: c.stylesheet.processorLanguage() == 10 && c.forwardsCompatible(c.effectiveVersion)}) + c.pendingTemplateCalls = append(c.pendingTemplateCalls, templateCallReference{Name: inst.Name, LegacyForwards: c.stylesheet.processorLanguage() == processorXSLT10 && c.forwardsCompatible(c.effectiveVersion)}) params, err := c.compileWithParams(ctx, elem) if err != nil { @@ -303,7 +303,7 @@ func (c *compiler) pushFlowClauseTextContext(elem *xdm.Element) (bool, func()) { savedExpand := c.expandText hasLocal := false - if namespace, present := elem.GetAttribute(nameXpathDefaultNamespace); present { + if namespace, present := elem.GetAttribute(nameXPathDefaultNamespace); present { c.xpathDefaultNS = namespace c.hasXPathDefaultNS = true hasLocal = true @@ -462,16 +462,16 @@ func (c *compiler) compileSortKey(ctx context.Context, elem *xdm.Element) (*sort // Validate boolean attribute: stable (including empty string). // Skip validation for AVTs (contain '{' and '}') since the value // is determined at runtime. - if stableAttr, hasStable := elem.GetAttribute("stable"); hasStable { + if stableAttr, hasStable := elem.GetAttribute(nameStable); hasStable { if !strings.ContainsAny(stableAttr, "{}") { - if err := validateBooleanAttr("xsl:sort", "stable", stableAttr); err != nil { + if err := validateBooleanAttr("xsl:sort", nameStable, stableAttr); err != nil { return nil, err } } } sk := &sortKey{ - forwards10: c.stylesheet.processorLanguage() == 10 && forwards10(elem), + forwards10: c.stylesheet.processorLanguage() == processorXSLT10 && forwards10(elem), backwards: c.backwardsCompatible(), BaseURI: stylesheetBaseURI(elem, c.baseURI, c.moduleRoot), } @@ -524,7 +524,7 @@ func (c *compiler) compileSortKeyOptions(elem *xdm.Element, key *sortKey) error {nameCaseOrder, &key.CaseOrder}, {lexicon.AttrLang, &key.Lang}, {nameCollation, &key.Collation}, - {"stable", &key.Stable}, + {nameStable, &key.Stable}, } { value, present := elem.GetAttribute(option.name) if !present { @@ -536,7 +536,7 @@ func (c *compiler) compileSortKeyOptions(elem *xdm.Element, key *sortKey) error return err } - if literal, static := compiled.staticValue(); static && c.stylesheet.processorLanguage() >= 20 { + if literal, static := compiled.staticValue(); static && c.stylesheet.processorLanguage() >= processorXSLT20 { if err := validateLiteralSortOption(option.name, literal); err != nil { return err } @@ -588,7 +588,7 @@ func (c *compiler) compileWithParam(ctx context.Context, elem *xdm.Element) (*wi } } - asAttr := getAttr(elem, "as") + asAttr := getAttr(elem, nameAs) if err := c.validateAsSequenceType(ctx, elem, asAttr, "xsl:with-param "+name); err != nil { return nil, err } @@ -752,7 +752,7 @@ func (c *compiler) compileApplyImports(ctx context.Context, elem *xdm.Element) ( func (c *compiler) compileTry(ctx context.Context, elem *xdm.Element) (*tryCatchInst, error) { inst := &tryCatchInst{RollbackOutput: true} - if rb, present := elem.GetAttribute("rollback-output"); present { + if rb, present := elem.GetAttribute(nameRollbackOutput); present { value, valid := parseXSDBool(rb) if !valid { return nil, staticError(errCodeXTSE0020, "invalid xsl:try rollback-output value %q", rb) @@ -863,7 +863,7 @@ func (c *compiler) compileCatchClause(ctx context.Context, elem *xdm.Element) (* // Parse errors attribute (space-separated list of error codes). // Namespace declarations on xsl:catch itself must be visible // when resolving prefixed error codes (e.g. err:XTDE0555). - if errAttr := getAttr(elem, "errors"); errAttr != "" { + if errAttr := getAttr(elem, nameErrors); errAttr != "" { for code := range strings.FieldsSeq(errAttr) { clause.Errors = append(clause.Errors, resolveQName(code, c.nsBindings)) } @@ -947,7 +947,7 @@ func (c *compiler) compileGroupingOptions(elem *xdm.Element, inst *forEachGroupI inst.Composite = xsdBoolTrue(comp) } - if gb, present := elem.GetAttribute("group-by"); present { + if gb, present := elem.GetAttribute(nameGroupBy); present { gbExpr, gbErr := c.compileXPath(gb, c.nsBindings) if gbErr != nil { return gbErr @@ -956,7 +956,7 @@ func (c *compiler) compileGroupingOptions(elem *xdm.Element, inst *forEachGroupI inst.GroupBy = gbExpr } - if ga, present := elem.GetAttribute("group-adjacent"); present { + if ga, present := elem.GetAttribute(nameGroupAdjacent); present { gaExpr, gaErr := c.compileXPath(ga, c.nsBindings) if gaErr != nil { return gaErr @@ -965,7 +965,7 @@ func (c *compiler) compileGroupingOptions(elem *xdm.Element, inst *forEachGroupI inst.GroupAdjacent = gaExpr } - if gs, present := elem.GetAttribute("group-starting-with"); present { + if gs, present := elem.GetAttribute(nameGroupStartingWith); present { gsPat, gsErr := c.compilePattern(gs, elem, c.xpathDefaultNS, c.hasXPathDefaultNS) if gsErr != nil { return gsErr @@ -974,7 +974,7 @@ func (c *compiler) compileGroupingOptions(elem *xdm.Element, inst *forEachGroupI inst.GroupStartingWith = gsPat } - if ge, present := elem.GetAttribute("group-ending-with"); present { + if ge, present := elem.GetAttribute(nameGroupEndingWith); present { gePat, geErr := c.compilePattern(ge, elem, c.xpathDefaultNS, c.hasXPathDefaultNS) if geErr != nil { return geErr @@ -1029,7 +1029,7 @@ func (c *compiler) compileAnalyzeString(ctx context.Context, elem *xdm.Element) return nil, staticError(errCodeXTSE0010, "xsl:analyze-string requires select attribute") } - regexAttr, regexFound := elem.GetAttribute("regex") + regexAttr, regexFound := elem.GetAttribute(nameRegex) if !regexFound { return nil, staticError(errCodeXTSE0010, "xsl:analyze-string requires regex attribute") } @@ -1049,7 +1049,7 @@ func (c *compiler) compileAnalyzeString(ctx context.Context, elem *xdm.Element) Regex: regexAVT, } - flagsAttr := getAttr(elem, "flags") + flagsAttr := getAttr(elem, nameFlags) if flagsAttr != "" { flagsAVT, err := c.compileAVT(flagsAttr, c.nsBindings) if err != nil { @@ -1135,7 +1135,7 @@ func (c *compiler) compileEvaluate(ctx context.Context, elem *xdm.Element) (inst } // xpath attribute is required - xpathAttr, present := elem.GetAttribute("xpath") + xpathAttr, present := elem.GetAttribute(nameXPath) if !present { return nil, staticError(errCodeXTSE0010, "xsl:evaluate requires an xpath attribute") } @@ -1158,7 +1158,7 @@ func (c *compiler) compileEvaluate(ctx context.Context, elem *xdm.Element) (inst } // base-uri (optional avt) - if bu := getAttr(elem, "base-uri"); bu != "" { + if bu := getAttr(elem, nameBaseURI); bu != "" { avt, err := c.compileAVT(bu, c.nsBindings) if err != nil { return nil, err @@ -1168,7 +1168,7 @@ func (c *compiler) compileEvaluate(ctx context.Context, elem *xdm.Element) (inst } // namespace-context (optional XPath expression producing a node) - if nc, present := elem.GetAttribute("namespace-context"); present { + if nc, present := elem.GetAttribute(nameNamespaceContext); present { ncExpr, err := c.compileXPath(nc, c.nsBindings) if err != nil { return nil, err @@ -1178,7 +1178,7 @@ func (c *compiler) compileEvaluate(ctx context.Context, elem *xdm.Element) (inst } // with-params (optional map expression) - if wp, present := elem.GetAttribute("with-params"); present { + if wp, present := elem.GetAttribute(nameWithParams); present { wpExpr, err := c.compileXPath(wp, c.nsBindings) if err != nil { return nil, err @@ -1188,7 +1188,7 @@ func (c *compiler) compileEvaluate(ctx context.Context, elem *xdm.Element) (inst } // as (optional type name) - inst.As = getAttr(elem, "as") + inst.As = getAttr(elem, nameAs) if inst.As != "" { if err := c.validateAsSequenceType(ctx, elem, inst.As, "xsl:evaluate"); err != nil { return nil, err @@ -1210,7 +1210,7 @@ func (c *compiler) compileEvaluate(ctx context.Context, elem *xdm.Element) (inst } // schema-aware (optional avt that evaluates to boolean) - if saStr, hasSA := elem.GetAttribute("schema-aware"); hasSA { + if saStr, hasSA := elem.GetAttribute(nameSchemaAware); hasSA { saAVT, err := c.compileAVT(saStr, c.nsBindings) if err != nil { return nil, err @@ -1325,7 +1325,7 @@ func validateGroupingAttributes(elem *xdm.Element) error { // group-ending-with must be present groupingCount := 0 - for _, name := range []string{"group-by", "group-adjacent", "group-starting-with", "group-ending-with"} { + for _, name := range []string{nameGroupBy, nameGroupAdjacent, nameGroupStartingWith, nameGroupEndingWith} { if _, present := elem.GetAttribute(name); present { groupingCount++ } @@ -1340,9 +1340,9 @@ func validateGroupingAttributes(elem *xdm.Element) error { } // XTSE1090: collation/composite only with group-by or group-adjacent - _, hasGroupBy := elem.GetAttribute("group-by") + _, hasGroupBy := elem.GetAttribute(nameGroupBy) - _, hasGroupAdjacent := elem.GetAttribute("group-adjacent") + _, hasGroupAdjacent := elem.GetAttribute(nameGroupAdjacent) if !hasGroupBy && !hasGroupAdjacent { if getAttr(elem, nameCollation) != "" { return staticError(errCodeXTSE1090, diff --git a/transform/internal/xslt/compile_instructions_nodes.go b/transform/internal/xslt/compile_instructions_nodes.go index 8e88147..e8b33d8 100644 --- a/transform/internal/xslt/compile_instructions_nodes.go +++ b/transform/internal/xslt/compile_instructions_nodes.go @@ -13,15 +13,20 @@ import ( "github.com/skarm/xmlsec/transform/internal/xpath" ) +// Modern XSLT shadow-attribute local names are distinct from their evaluated target attributes; support and namespace checks remain unchanged. +const ( + shadowAttrCopyNamespaces = "_copy-namespaces" +) + func (c *compiler) compileValueOf(ctx context.Context, elem *xdm.Element) (*valueOfInst, error) { inst := &valueOfInst{} selectAttr, hasSelect := elem.GetAttribute(xslAttrSelect) - if !hasSelect && c.stylesheet.processorLanguage() == 10 { + if !hasSelect && c.stylesheet.processorLanguage() == processorXSLT10 { return nil, staticError(errCodeXTSE0010, "xsl:value-of requires select in XSLT 1.0") } - if !hasSelect && c.stylesheet.processorLanguage() == 20 && !c.hasEffectiveContent(ctx, elem) { + if !hasSelect && c.stylesheet.processorLanguage() == processorXSLT20 && !c.hasEffectiveContent(ctx, elem) { return nil, staticError(errCodeXTSE0870, "XSLT 2.0 xsl:value-of requires select or non-empty content") } @@ -34,7 +39,7 @@ func (c *compiler) compileValueOf(ctx context.Context, elem *xdm.Element) (*valu inst.Select = expr } - if sep, ok := elem.GetAttribute("separator"); ok { + if sep, ok := elem.GetAttribute(nameSeparator); ok { avt, err := c.compileAVT(sep, c.nsBindings) if err != nil { return nil, err @@ -227,7 +232,7 @@ func (c *compiler) compileAttribute(ctx context.Context, elem *xdm.Element) (*at return nil, err } - inst := &attributeInst{Name: nameAVT} + inst := &attributeInst{Name: nameAVT, Namespaces: inScopeNamespaces(elem)} if typeAttr := getAttr(elem, lexicon.AttrType); typeAttr != "" { if err := c.checkTypeAttrSchemaAware(ctx, "xsl:attribute", typeAttr); err != nil { @@ -258,7 +263,7 @@ func (c *compiler) compileAttribute(ctx context.Context, elem *xdm.Element) (*at inst.Namespace = nsAVT } - if sep, hasSep := elem.GetAttribute("separator"); hasSep { + if sep, hasSep := elem.GetAttribute(nameSeparator); hasSep { sepAVT, err := c.compileAVT(sep, c.nsBindings) if err != nil { return nil, err @@ -321,6 +326,10 @@ func (c *compiler) compileComment(ctx context.Context, elem *xdm.Element) (*comm } func (c *compiler) compilePI(ctx context.Context, elem *xdm.Element) (*piInst, error) { + const ( + errCodeXTSE0880 = "XTSE0880" + ) + nameAttr, hasName := elem.GetAttribute(xslAttrName) if !hasName { return nil, staticError(errCodeXTSE0010, "xsl:processing-instruction requires name attribute") @@ -337,7 +346,7 @@ func (c *compiler) compilePI(ctx context.Context, elem *xdm.Element) (*piInst, e if hasSelect { // XTSE0940: select and non-empty content are mutually exclusive. if err := c.validateEmptyElement(ctx, elem, "xsl:processing-instruction"); err != nil { - return nil, staticError("XTSE0880", "xsl:processing-instruction has both @select and content") + return nil, staticError(errCodeXTSE0880, "xsl:processing-instruction has both @select and content") } expr, err := c.compileXPath(selectAttr, c.nsBindings) @@ -359,7 +368,11 @@ func (c *compiler) compilePI(ctx context.Context, elem *xdm.Element) (*piInst, e } func (c *compiler) compileCopy(ctx context.Context, elem *xdm.Element) (*copyInst, error) { - if err := validateCopyBooleanAttrs(elem, "xsl:copy", nameInheritNamespaces, "copy-namespaces"); err != nil { + const ( + shadowAttrInheritNamespaces = "_inherit-namespaces" + ) + + if err := validateCopyBooleanAttrs(elem, "xsl:copy", nameInheritNamespaces, nameCopyNamespaces); err != nil { return nil, err } @@ -368,7 +381,7 @@ func (c *compiler) compileCopy(ctx context.Context, elem *xdm.Element) (*copyIns InheritNamespaces: true, } - if cn := getAttr(elem, "copy-namespaces"); cn != "" { + if cn := getAttr(elem, nameCopyNamespaces); cn != "" { if v, ok := parseXSDBool(cn); ok && !v { inst.CopyNamespaces = false } @@ -381,7 +394,7 @@ func (c *compiler) compileCopy(ctx context.Context, elem *xdm.Element) (*copyIns } // Shadow attributes (_attr overrides attr via avt at runtime) - if scn := getAttr(elem, "_copy-namespaces"); scn != "" { + if scn := getAttr(elem, shadowAttrCopyNamespaces); scn != "" { avt, err := c.compileAVT(scn, c.nsBindings) if err != nil { return nil, err @@ -390,7 +403,7 @@ func (c *compiler) compileCopy(ctx context.Context, elem *xdm.Element) (*copyIns inst.CopyNamespacesAVT = avt } - if sin := getAttr(elem, "_inherit-namespaces"); sin != "" { + if sin := getAttr(elem, shadowAttrInheritNamespaces); sin != "" { avt, err := c.compileAVT(sin, c.nsBindings) if err != nil { return nil, err @@ -443,7 +456,7 @@ func (c *compiler) compileCopyOf(_ context.Context, elem *xdm.Element) (*copyOfI return nil, err } - if err := validateCopyBooleanAttrs(elem, "xsl:copy-of", "copy-namespaces"); err != nil { + if err := validateCopyBooleanAttrs(elem, "xsl:copy-of", nameCopyNamespaces); err != nil { return nil, err } @@ -462,13 +475,13 @@ func (c *compiler) compileCopyOf(_ context.Context, elem *xdm.Element) (*copyOfI CopyNamespaces: true, } - if cn := getAttr(elem, "copy-namespaces"); cn != "" { + if cn := getAttr(elem, nameCopyNamespaces); cn != "" { if v, ok := parseXSDBool(cn); ok && !v { inst.CopyNamespaces = false } } // Shadow attribute _copy-namespaces overrides at runtime - if scn := getAttr(elem, "_copy-namespaces"); scn != "" { + if scn := getAttr(elem, shadowAttrCopyNamespaces); scn != "" { avt, err := c.compileAVT(scn, c.nsBindings) if err != nil { return nil, err @@ -489,7 +502,7 @@ func (c *compiler) compileCopyOf(_ context.Context, elem *xdm.Element) (*copyOfI inst.TypeName = resolveXSDTypeName(typeAttr, c.nsBindings) } - if ca := getAttr(elem, "copy-accumulators"); ca != "" { + if ca := getAttr(elem, nameCopyAccumulators); ca != "" { if v, ok := parseXSDBool(ca); ok && v { inst.CopyAccumulators = true } @@ -500,7 +513,7 @@ func (c *compiler) compileCopyOf(_ context.Context, elem *xdm.Element) (*copyOfI func (c *compiler) compileNumber(_ context.Context, elem *xdm.Element) (*numberInst, error) { inst := &numberInst{ - Level: getAttr(elem, "level"), + Level: getAttr(elem, nameLevel), } if inst.Level == "" { inst.Level = nameSingle @@ -539,7 +552,7 @@ func (c *compiler) compileNumber(_ context.Context, elem *xdm.Element) (*numberI } func (c *compiler) compileNumberCounting(elem *xdm.Element, inst *numberInst) error { - if countAttr, present := elem.GetAttribute("count"); present { + if countAttr, present := elem.GetAttribute(lexicon.AttrCount); present { p, err := c.compilePattern(countAttr, elem, c.xpathDefaultNS, c.hasXPathDefaultNS) if err != nil { return err @@ -548,7 +561,7 @@ func (c *compiler) compileNumberCounting(elem *xdm.Element, inst *numberInst) er inst.Count = p } - if fromAttr, present := elem.GetAttribute("from"); present { + if fromAttr, present := elem.GetAttribute(lexicon.AttrFrom); present { p, err := c.compilePattern(fromAttr, elem, c.xpathDefaultNS, c.hasXPathDefaultNS) if err != nil { return err @@ -559,9 +572,9 @@ func (c *compiler) compileNumberCounting(elem *xdm.Element, inst *numberInst) er inst.memoizable = !patternReadsVariables(inst.Count) && !patternReadsVariables(inst.From) - if valueAttr, present := elem.GetAttribute("value"); present { + if valueAttr, present := elem.GetAttribute(lexicon.AttrValue); present { // XTSE0975: when value is present, select/level/count/from must be absent. - for _, attribute := range []string{xslAttrSelect, "level", lexicon.AttrCount, lexicon.AttrFrom} { + for _, attribute := range []string{xslAttrSelect, nameLevel, lexicon.AttrCount, lexicon.AttrFrom} { if _, specified := elem.GetAttribute(attribute); specified { return staticError(errCodeXTSE0975, "xsl:number: when @value is present, @select, @level, @count, and @from must be absent") } @@ -586,7 +599,7 @@ func (c *compiler) compileNumberFormatting(elem *xdm.Element, inst *numberInst) {nameFormat, &inst.Format}, {nameGroupingSeparator, &inst.GroupingSeparator}, {nameGroupingSize, &inst.GroupingSize}, - {"ordinal", &inst.Ordinal}, + {nameOrdinal, &inst.Ordinal}, {nameStartAt, &inst.StartAt}, } { if err := c.compileNumberOption(elem, option.name, option.value); err != nil { @@ -608,7 +621,7 @@ func (c *compiler) compileNumberOption(elem *xdm.Element, name string, target ** return err } - if literal, static := compiled.staticValue(); static && c.stylesheet.processorLanguage() >= 20 && !validNumberOption(name, literal) { + if literal, static := compiled.staticValue(); static && c.stylesheet.processorLanguage() >= processorXSLT20 && !validNumberOption(name, literal) { return staticError(errCodeXTSE0020, "%q is not a valid value for xsl:number/@%s", literal, name) } @@ -696,7 +709,7 @@ func (c *compiler) compileSequence(ctx context.Context, elem *xdm.Element) (inst // XTSE3185: when @select is present, only xsl:fallback children are allowed. if hasSignificantChildren(elem) { code := errCodeXTSE3185 - if c.stylesheet.processorLanguage() == 20 { + if c.stylesheet.processorLanguage() == processorXSLT20 { code = errCodeXTSE0010 } @@ -823,6 +836,10 @@ func (c *compiler) pushResultNamespacePolicy(elem *xdm.Element) (func(), error) } func (c *compiler) elementResultExcludes(elem *xdm.Element) (map[string]struct{}, error) { + const ( + literalExclusionSpareCapacity = 4 + ) + erp, hasERP := elem.GetAttributeNS(nameExcludeResultPrefixes, lexicon.NamespaceXSLT) eep, hasEEP := elem.GetAttributeNS(nameExtensionElementPrefixes, lexicon.NamespaceXSLT) @@ -835,7 +852,7 @@ func (c *compiler) elementResultExcludes(elem *xdm.Element) (map[string]struct{} return c.localExcludes, nil } - excludes := make(map[string]struct{}, len(c.localExcludes)+4) + excludes := make(map[string]struct{}, len(c.localExcludes)+literalExclusionSpareCapacity) maps.Copy(excludes, c.localExcludes) if hasERP { @@ -1140,7 +1157,7 @@ func validateCopyOfStructure(elem *xdm.Element) error { continue } - if attr.LocalName() == lexicon.AttrMatch || attr.LocalName() == lexicon.AttrCount || attr.LocalName() == "from" { + if attr.LocalName() == lexicon.AttrMatch || attr.LocalName() == lexicon.AttrCount || attr.LocalName() == lexicon.AttrFrom { return staticError(errCodeXTSE0090, "attribute %q is not allowed on xsl:copy-of", attr.LocalName()) } diff --git a/transform/internal/xslt/compile_patterns.go b/transform/internal/xslt/compile_patterns.go index e0bcc94..a9f3b29 100644 --- a/transform/internal/xslt/compile_patterns.go +++ b/transform/internal/xslt/compile_patterns.go @@ -16,6 +16,12 @@ import ( "github.com/skarm/xmlsec/transform/internal/xpath" ) +// XSLT 3.0 section 6.5 default pattern priority classes; explicit xsl:template priority remains independently parsed. +const ( + patternPriorityComplex = 0.5 + patternPriorityNodeKind = -0.5 +) + type predicateOutcome int const ( @@ -295,17 +301,43 @@ func (p *pattern) matchNodeContextItemPositional(ctx context.Context, ec *execCo // attribute nodes. This is a conservative heuristic used by key table building // to decide whether to visit attribute nodes during the document walk. func (p *pattern) matchesAttributes() bool { - return strings.Contains(p.source, "@") || strings.Contains(p.source, "attribute") + for _, alt := range p.Alternatives { + if !alt.neverMatches && exprMatchesAttributeNodes(alt.expr) { + return true + } + } + + return false +} + +// Non-path patterns can select attributes dynamically. Only a terminal axis +// known to exclude attributes makes skipping them safe. +func exprMatchesAttributeNodes(expr xpath.Expr) bool { + switch e := expr.(type) { + case *xpath.LocationPath: + return exprMatchesAttributeNodes(*e) + case xpath.LocationPath: + if len(e.Steps) == 0 { + return false + } + + axis := e.Steps[len(e.Steps)-1].Axis + + return axis == xpath.AxisAttribute || axis == xpath.AxisSelf || axis == xpath.AxisDescendantOrSelf + case xpath.RootExpr, *xpath.RootExpr: + return false + case xpath.UnionExpr: + return exprMatchesAttributeNodes(e.Left) || exprMatchesAttributeNodes(e.Right) + case xpath.PathStepExpr: + return exprMatchesAttributeNodes(e.Right) + default: + return true + } } -// matchesNamespaceNodes reports whether the pattern could match a namespace -// node. A namespace node is only ever selected structurally by a step on the -// namespace axis (nodeMatchesStep rejects it on every other axis), so a pattern -// whose alternatives are all paths ending on another axis can never match one. -// Every other alternative form — "." and ".[p]", a variable or function-call -// pattern matched by evaluating the expression — is treated as though it could. -// Key table building uses this to decide whether to enumerate in-scope -// namespace nodes during the document walk. +// matchesNamespaceNodes conservatively reports whether a pattern can select +// namespaces, including self steps that preserve an earlier namespace step. +// Key table building uses this to decide whether to enumerate namespaces. func (p *pattern) matchesNamespaceNodes() bool { for _, alt := range p.Alternatives { // Skipping a neverMatches alternative cannot lose a namespace node: @@ -518,6 +550,10 @@ func matchLocationPath(ctx context.Context, ec *execContext, path xpath.Location // evaluation here — predicates will be evaluated with proper // descendant-set position relative to the ancestor later. lastStep := path.Steps[len(path.Steps)-1] + if lastStep.Axis == xpath.AxisSelf || lastStep.Axis == xpath.AxisDescendantOrSelf && isPatternSideNode(node) { + return matchStepsUpward(ctx, ec, path.Steps, path.Absolute, node) + } + descendantAxis := lastStep.Axis == xpath.AxisDescendant || lastStep.Axis == xpath.AxisDescendantOrSelf hasDescPreds := descendantAxis && len(lastStep.Predicates) > 0 @@ -653,6 +689,8 @@ func matchStepsUpward(ctx context.Context, ec *execContext, steps []xpath.Step, lastStep := steps[len(steps)-1] switch lastStep.Axis { + case xpath.AxisSelf: + return matchSelfStepUpward(ctx, ec, steps, absolute, node) case xpath.AxisChild: // child axis in pattern means "the parent must match" if !nodeMatchesStep(ctx, ec, lastStep, node) { @@ -660,7 +698,16 @@ func matchStepsUpward(ctx context.Context, ec *execContext, steps []xpath.Step, } return matchStepsUpward(ctx, ec, steps[:len(steps)-1], absolute, node.Parent()) + case xpath.AxisDescendant: + return matchLocationPath(ctx, ec, xpath.LocationPath{Steps: steps, Absolute: absolute}, node) case xpath.AxisDescendantOrSelf: + if isPatternSideNode(node) { + return matchSelfStepUpward(ctx, ec, steps, absolute, node) + } + + if lastStep.ExplicitAxis { + return matchLocationPath(ctx, ec, xpath.LocationPath{Steps: steps, Absolute: absolute}, node) + } // // in pattern means any ancestor may match // This step is the desc-or-self::node() inserted by // // Try matching from this node and any ancestor @@ -681,6 +728,26 @@ func matchStepsUpward(ctx context.Context, ec *execContext, steps []xpath.Step, } } +func isPatternSideNode(node xdm.Node) bool { + return node.Type() == xdm.AttributeNode || node.Type() == xdm.NamespaceNode +} + +func matchSelfStepUpward(ctx context.Context, ec *execContext, steps []xpath.Step, absolute bool, node xdm.Node) bool { + if !nodeMatchesStep(ctx, ec, steps[len(steps)-1], node) { + return false + } + + remaining := steps[:len(steps)-1] + // root(.)//EE includes a side-node as an initial context only when it + // is itself the root. An earlier attribute/namespace step can select a + // parented side-node, after which self steps preserve that node. + if len(remaining) == 0 && !absolute && isPatternSideNode(node) && node.Parent() != nil { + return false + } + + return matchStepsUpward(ctx, ec, remaining, absolute, node) +} + // nodeMatchesStep checks if a node matches a single step's node test and predicates. func nodeMatchesStep(ctx context.Context, ec *execContext, step xpath.Step, node xdm.Node) bool { nodeType := node.Type() @@ -700,21 +767,17 @@ func nodeMatchesStep(ctx context.Context, ec *execContext, step xpath.Step, node } } } - // Attribute nodes are only matched by the attribute axis (e.g., @*, attribute::node()). - // The default child axis does not include attributes. - // Conversely, the attribute axis only matches attribute nodes. - isAttr := nodeType == xdm.AttributeNode - if isAttr != (step.Axis == xpath.AxisAttribute) { - return false - } - // Namespace nodes are only matched by the namespace axis or namespace-node() test. - // matchesNamespaceNodes relies on this being the only way a namespace node - // can match: widening it (e.g. letting self:: select one) requires updating - // matchesNamespaceNodes too, or key table building will silently drop - // namespace-node entries. - isNS := nodeType == xdm.NamespaceNode - if isNS != (step.Axis == xpath.AxisNamespace) { - return false + // Attribute and namespace steps select their own node kinds. Self axes + // may preserve either kind after an earlier step selected the node. + selfAxis := step.Axis == xpath.AxisSelf || step.Axis == xpath.AxisDescendantOrSelf + if !selfAxis { + if (nodeType == xdm.AttributeNode) != (step.Axis == xpath.AxisAttribute) { + return false + } + + if (nodeType == xdm.NamespaceNode) != (step.Axis == xpath.AxisNamespace) { + return false + } } if !nodeMatchesTest(ctx, ec, step.NodeTest, node) { @@ -722,6 +785,10 @@ func nodeMatchesStep(ctx context.Context, ec *execContext, step xpath.Step, node } if len(step.Predicates) > 0 { + if step.Axis == xpath.AxisSelf || selfAxis && isPatternSideNode(node) { + return passesChainedPredicates(ctx, ec, step.Predicates, []xdm.Node{node}, node) + } + return evaluateChainedPredicates(ctx, ec, step, node) } @@ -884,7 +951,7 @@ func matchNameTest(_ context.Context, ec *execContext, nt xdm.NameTest, node xdm return false } - if nt.Local == "*" && nt.Prefix == "" && nt.URI == "" { + if nt.Local == "*" && nt.Prefix == "" && !nt.URIQualified && nt.URI == "" { // * matches all elements/attributes/namespace-nodes return true } @@ -900,7 +967,7 @@ func matchNameTest(_ context.Context, ec *execContext, nt xdm.NameTest, node xdm } // Exact name match - if nt.URI != "" { + if nt.URIQualified || nt.URI != "" { return nodeLocal == nt.Local && nodeURI == nt.URI } @@ -945,7 +1012,7 @@ func matchNameTest(_ context.Context, ec *execContext, nt xdm.NameTest, node xdm // isTypedStrictMode returns true if the current mode has typed="strict" or typed="yes". func isTypedStrictMode(ec *execContext) bool { if md := currentModeDef(ec); md != nil { - return md.Typed == validationStrict || md.Typed == lexicon.ValueYes || md.Typed == lexicon.ValueTrue || md.Typed == "1" + return md.Typed == validationStrict || md.Typed == lexicon.ValueYes || md.Typed == lexicon.ValueTrue || md.Typed == booleanDigitTrue } return false @@ -966,28 +1033,28 @@ func currentModeDef(ec *execContext) *modeDef { return ec.lookupModeDef(ec.currentMode) } -// matchElementTest checks if a node matches an element() test. // isBuiltinXSDType returns true for type names that are built-in XSD types // (e.g., lexicon.TypeString, "integer", "decimal", "untyped", "untypedAtomic"). func isBuiltinXSDType(name string) bool { switch name { case lexicon.TypeString, lexicon.TypeBoolean, lexicon.TypeDecimal, lexicon.TypeFloat, lexicon.TypeDouble, lexicon.TypeDuration, lexicon.TypeDateTime, lexicon.TypeTime, lexicon.TypeDate, - "gYearMonth", "gYear", "gMonthDay", "gDay", "gMonth", - "hexBinary", "base64Binary", lexicon.TypeAnyURI, "QName", "NOTATION", - "normalizedString", "token", "language", "NMTOKEN", "NMTOKENS", - "Name", "NCName", "ID", "IDREF", "IDREFS", "ENTITY", "ENTITIES", - lexicon.TypeInteger, "nonPositiveInteger", "negativeInteger", "long", "int", - "short", "byte", "nonNegativeInteger", "unsignedLong", "unsignedInt", - "unsignedShort", "unsignedByte", "positiveInteger", - lexicon.TypeYearMonthDuration, lexicon.TypeDayTimeDuration, "dateTimeStamp", - "untyped", lexicon.TypeUntypedAtomic, "anyType", "anySimpleType", "anyAtomicType": + lexicon.TypeGYearMonth, lexicon.TypeGYear, lexicon.TypeGMonthDay, lexicon.TypeGDay, lexicon.TypeGMonth, + lexicon.TypeHexBinary, lexicon.TypeBase64Binary, lexicon.TypeAnyURI, lexicon.TypeQName, lexicon.TypeNotation, + lexicon.TypeNormalizedString, lexicon.TypeToken, lexicon.TypeLanguage, lexicon.TypeNMToken, lexicon.TypeNMTokens, + lexicon.TypeName, lexicon.TypeNCName, lexicon.TypeID, lexicon.TypeIDREF, lexicon.TypeIDREFS, lexicon.TypeENTITY, lexicon.TypeENTITIES, + lexicon.TypeInteger, lexicon.TypeNonPositiveInteger, lexicon.TypeNegativeInteger, lexicon.TypeLong, lexicon.TypeInt, + lexicon.TypeShort, lexicon.TypeByte, lexicon.TypeNonNegativeInteger, lexicon.TypeUnsignedLong, lexicon.TypeUnsignedInt, + lexicon.TypeUnsignedShort, lexicon.TypeUnsignedByte, lexicon.TypePositiveInteger, + lexicon.TypeYearMonthDuration, lexicon.TypeDayTimeDuration, lexicon.TypeDateTimeStamp, + lexicon.TypeUntyped, lexicon.TypeUntypedAtomic, lexicon.TypeAnyType, lexicon.TypeAnySimpleType, lexicon.TypeAnyAtomicType: return true } return false } +// matchElementTest checks whether a node matches an element() test. func matchElementTest(ctx context.Context, ec *execContext, et xdm.ElementTest, node xdm.Node) bool { if node.Type() != xdm.ElementNode { return false @@ -1084,7 +1151,7 @@ func matchTypeAnnotation(_ context.Context, ec *execContext, node xdm.Node, type if ann == "" { switch node.Type() { case xdm.ElementNode: - ann = "untyped" + ann = lexicon.TypeUntyped case xdm.AttributeNode: ann = lexicon.TypeUntypedAtomic default: @@ -1108,13 +1175,13 @@ func matchTypeAnnotation(_ context.Context, ec *execContext, node xdm.Node, type } fullAnn := ann - if !strings.HasPrefix(fullAnn, "xs:") && !strings.HasPrefix(fullAnn, "Q{") { - fullAnn = "xs:" + fullAnn + if !strings.HasPrefix(fullAnn, lexicon.PrefixXS+":") && !strings.HasPrefix(fullAnn, "Q{") { + fullAnn = lexicon.PrefixXS + ":" + fullAnn } fullType := typeName - if !strings.HasPrefix(fullType, "xs:") && !strings.HasPrefix(fullType, "Q{") { - fullType = "xs:" + fullType + if !strings.HasPrefix(fullType, lexicon.PrefixXS+":") && !strings.HasPrefix(fullType, "Q{") { + fullType = lexicon.PrefixXS + ":" + fullType } if xdm.BuiltinIsSubtypeOf(fullAnn, fullType) { @@ -1125,7 +1192,7 @@ func matchTypeAnnotation(_ context.Context, ec *execContext, node xdm.Node, type } func normalizePatternTypeName(name string) string { - for _, prefix := range []string{"xs:", "xsd:", "http://www.w3.org/2001/XMLSchema:"} { + for _, prefix := range []string{lexicon.PrefixXS + ":", lexicon.PrefixXSD + ":", lexicon.NamespaceXSD + ":"} { if strings.HasPrefix(name, prefix) { return name[len(prefix):] } @@ -1550,7 +1617,9 @@ func locationPathMatchesNamespaceNodes(path xpath.LocationPath) bool { return false } - return path.Steps[len(path.Steps)-1].Axis == xpath.AxisNamespace + axis := path.Steps[len(path.Steps)-1].Axis + + return axis == xpath.AxisNamespace || axis == xpath.AxisSelf || axis == xpath.AxisDescendantOrSelf } // patternPredicate is one step predicate of a match pattern, compiled once @@ -1619,7 +1688,7 @@ func (c *patternPredicateCollector) add(pred *xpath.Expr) { func isFocusFreePredicate(compiled *xpath.Expression, nsBindings map[string]string) bool { for _, fn := range compiled.StaticReferences(nsBindings).FunctionNames { switch fn.Name { - case "position", "last", "function-lookup": + case lexicon.FnPosition, lexicon.FnLast, fnNameFunctionLookup: return false } } @@ -1720,7 +1789,7 @@ func (c *compiler) compilePattern(s string, elem *xdm.Element, xpathDefaultNS st expr: ast, compiled: compiledExpr, priority: computeDefaultPriority(ast), - neverMatches: isNeverMatchingPattern(alt), + neverMatches: isNeverMatchingPattern(ast), } p.Alternatives = append(p.Alternatives, pa) } @@ -1862,24 +1931,31 @@ func patternCompilationError(ctx context.Context, source string, cause error) er // semantically can never match any node. For example, child::document-node() // never matches because document nodes cannot be children; similarly, // child::attribute() never matches because attributes are not children. -func isNeverMatchingPattern(alt string) bool { - // NOTE: this is a heuristic based on source text, not AST analysis. - // It may miss patterns with unusual whitespace around "::" but is - // safe in the false-negative direction (worst case: we compile a - // pattern that never matches at runtime). - s := strings.TrimSpace(alt) - // child::document-node() — document nodes are never children - if strings.HasPrefix(s, "child::document-node") || strings.Contains(s, "/child::document-node") { - return true - } - // child::attribute() / child::schema-attribute() — attributes are never children - if strings.HasPrefix(s, "child::attribute") || strings.Contains(s, "/child::attribute") || - strings.HasPrefix(s, "child::schema-attribute") || strings.Contains(s, "/child::schema-attribute") { - return true - } - // child::namespace-node() — namespace nodes are never children - if strings.HasPrefix(s, "child::namespace-node") || strings.Contains(s, "/child::namespace-node") { - return true +func isNeverMatchingPattern(expr xpath.Expr) bool { + switch e := expr.(type) { + case *xpath.LocationPath: + return isNeverMatchingPattern(*e) + case xpath.LocationPath: + for i, step := range e.Steps { + if step.Axis != xpath.AxisChild { + continue + } + + switch step.NodeTest.(type) { + case xdm.AttributeTest, xdm.SchemaAttributeTest, xdm.NamespaceNodeTest: + return true + case xdm.DocumentTest: + // XSLT permits an initial document-node() kind test, while + // an explicitly written child axis can never select a document. + if step.ExplicitAxis || e.Absolute || i > 0 { + return true + } + } + } + case xpath.PathStepExpr: + return isNeverMatchingPattern(e.Left) || isNeverMatchingPattern(e.Right) + case xpath.UnionExpr: + return isNeverMatchingPattern(e.Left) && isNeverMatchingPattern(e.Right) } return false @@ -1887,15 +1963,15 @@ func isNeverMatchingPattern(alt string) bool { func validatePatternAlternative(compiled *xpath.Expression, alt string, nsBindings map[string]string, version int) error { ast := compiled.AST() - if version == 20 { + if version == processorXSLT20 { if err := checkPattern20(ast); err != nil { return err } } - if version == 10 { + if version == processorXSLT10 { if len(compiled.StaticReferences(nsBindings).FreeVariables) > 0 { - return staticError("XTSE0340", "variables are unavailable in XSLT 1.0 patterns") + return staticError(errCodeXTSE0340, "variables are unavailable in XSLT 1.0 patterns") } if err := checkPattern10(ast); err != nil { @@ -1941,13 +2017,13 @@ func checkPattern20(expr xpath.Expr) error { } func checkPatternFunction20(call xpath.FunctionCall) error { - if call.Prefix != "" || call.Name != "id" && call.Name != nameKey2 { + if call.Prefix != "" || call.Name != fnNameID && call.Name != keyLocalName { return staticError(errCodeXTSE0340, "only unprefixed id/key calls can start an XSLT 2.0 pattern") } argument := 0 - if call.Name == nameKey2 { + if call.Name == keyLocalName { if len(call.Args) != 2 { return staticError(errCodeXTSE0340, "key pattern requires two arguments") } @@ -1970,7 +2046,7 @@ func checkPatternFunction20(call xpath.FunctionCall) error { case xpath.VariableExpr: return nil case xpath.LiteralExpr: - if call.Name == nameKey2 { + if call.Name == keyLocalName { return nil } @@ -2249,6 +2325,12 @@ func isValidPatternStep(expr xpath.Expr) bool { // so the namespace URI must be the functions namespace before the local-name // switch is consulted. func isAllowedPatternFunction(fc xpath.FunctionCall, nsBindings map[string]string) bool { + const ( + fnNameElementWithID = "element-with-id" + fnNameIDRef = "idref" + fnNameRoot = "root" + ) + // Resolve the call's (namespace URI, local name) honoring the EQName braced // form, explicit xmlns bindings, and the predeclared fn: default. A function // outside the functions namespace can never be a built-in pattern function, @@ -2259,7 +2341,7 @@ func isAllowedPatternFunction(fc xpath.FunctionCall, nsBindings map[string]strin } switch local { - case nameKey2, "id", "idref", nameDoc, "element-with-id", "root": + case keyLocalName, fnNameID, fnNameIDRef, nameDoc, fnNameElementWithID, fnNameRoot: for _, arg := range fc.Args { if !isPatternFunctionArg(arg) { return false @@ -2347,6 +2429,11 @@ func checkPatternForbiddenFunctions(ast xpath.Expr, nsBindings map[string]string // computeDefaultPriority computes the default priority for a pattern // alternative per XSLT 3.0 Section 6.5. func computeDefaultPriority(expr xpath.Expr) float64 { + const ( + patternPriorityContextItem = -1 + patternPriorityPredicate = 1 + ) + var steps []xpath.Step switch e := expr.(type) { @@ -2356,9 +2443,9 @@ func computeDefaultPriority(expr xpath.Expr) float64 { steps = e.Steps case xpath.RootExpr, *xpath.RootExpr: // "/" matches document nodes; default priority is -0.5 - return -0.5 + return patternPriorityNodeKind case xpath.ContextItemExpr: - return -1 + return patternPriorityContextItem case xpath.IntersectExceptExpr: return computeDefaultPriority(e.Left) case xpath.UnionExpr: @@ -2374,32 +2461,32 @@ func computeDefaultPriority(expr xpath.Expr) float64 { case xpath.FilterExpr: // PredicatePattern priority is one, independently of predicate count. if _, ok := e.Expr.(xpath.ContextItemExpr); ok { - return 1 + return patternPriorityPredicate } - return 0.5 + return patternPriorityComplex default: - return 0.5 + return patternPriorityComplex } if len(steps) == 0 { // Absolute path with no steps is "/" — document root, priority -0.5 if isAbsolute(expr) { - return -0.5 + return patternPriorityNodeKind } - return 0.5 + return patternPriorityComplex } // Per XSLT 3.0 §6.5: path patterns with multiple steps get priority 0.5. // An absolute path like "/doc" counts as multi-step (root + step). if len(steps) > 1 || (len(steps) == 1 && isAbsolute(expr)) { - return 0.5 + return patternPriorityComplex } lastStep := steps[0] // A step with predicates gets priority 0.5 (more specific) if len(lastStep.Predicates) > 0 { - return 0.5 + return patternPriorityComplex } return stepPriority(lastStep) @@ -2417,68 +2504,77 @@ func isAbsolute(expr xpath.Expr) bool { } func stepPriority(step xpath.Step) float64 { + const ( + patternPriorityName = 0 + patternPriorityTypedName = 0.25 + patternPriorityWildcardComponent = -0.25 + ) + switch nt := step.NodeTest.(type) { case xdm.TypeTest, xdm.NamespaceNodeTest: // node(), text(), comment(), processing-instruction() - return -0.5 + return patternPriorityNodeKind case xdm.PITest: if nt.HasTarget { - return 0 + return patternPriorityName } - return -0.5 + return patternPriorityNodeKind case xdm.NameTest: - if nt.Local == "*" && nt.Prefix == "" && nt.URI == "" { - return -0.5 + if nt.Local == "*" && nt.Prefix == "" && !nt.URIQualified && nt.URI == "" { + return patternPriorityNodeKind } if nt.Local == "*" || nt.Prefix == "*" { // prefix:* or *:local — one component is wildcard - return -0.25 + return patternPriorityWildcardComponent } - return 0 + return patternPriorityName case xdm.ElementTest: if nt.TypeName != "" { if nt.Name == "" || nt.Name == "*" { - return 0 // element(*, type) — type constraint, priority 0 + return patternPriorityName // element(*, type) — type constraint, priority 0 } - return 0.25 // element(name, type) + return patternPriorityTypedName // element(name, type) } if nt.Name == "" || nt.Name == "*" { - return -0.5 // element() or element(*) + return patternPriorityNodeKind // element() or element(*) } - return 0 // element(specific-name) + return patternPriorityName // element(specific-name) case xdm.AttributeTest: if nt.TypeName != "" { if nt.Name == "" || nt.Name == "*" { - return 0 // attribute(*, type) — type constraint, priority 0 + return patternPriorityName // attribute(*, type) — type constraint, priority 0 } - return 0.25 // attribute(name, type) + return patternPriorityTypedName // attribute(name, type) } if nt.Name == "" || nt.Name == "*" { - return -0.5 // attribute() or attribute(*) + return patternPriorityNodeKind // attribute() or attribute(*) } - return 0 // attribute(specific-name) + return patternPriorityName // attribute(specific-name) case xdm.DocumentTest: if nt.Inner == nil { - return -0.5 + return patternPriorityNodeKind } // Priority of document-node(test) derives from the inner test return stepPriority(xpath.Step{NodeTest: nt.Inner}) case xdm.SchemaElementTest, xdm.SchemaAttributeTest: - return 0.25 + return patternPriorityTypedName default: - return 0.5 + return patternPriorityComplex } } +// resolvePatternFunctionNamespace uses the XPath functions namespace for an +// unprefixed call. Explicit prefix bindings take precedence over predeclared +// XPath bindings; an unresolved prefix returns an empty string. func resolvePatternFunctionNamespace(prefix string, nsBindings map[string]string) string { if prefix == "" { return lexicon.NamespaceFn @@ -2511,12 +2607,6 @@ func patternFunctionIdentity(fc xpath.FunctionCall, nsBindings map[string]string return resolvePatternFunctionNamespace(fc.Prefix, nsBindings), fc.Name } -// resolvePatternFunctionNamespace resolves a function-call prefix to its -// namespace URI using the same precedence everywhere in pattern validation: -// an unprefixed call names the XPath functions namespace; a prefixed call -// resolves through the EXPLICIT in-scope xmlns bindings first, falling back to -// the XPath 3.0 predeclared bindings (fn:, math:, map:, array:, err:, xs:) only -// when the prefix is otherwise undeclared. An unresolved prefix returns "". // resolvePatternPrefix resolves a non-empty namespace prefix used inside a // match pattern against the pattern's lexical namespace snapshot, then the // predeclared XPath prefixes (fn:, math:, map:, ...), then the universally diff --git a/transform/internal/xslt/compile_scope.go b/transform/internal/xslt/compile_scope.go index 83f8b26..c68fe00 100644 --- a/transform/internal/xslt/compile_scope.go +++ b/transform/internal/xslt/compile_scope.go @@ -31,7 +31,7 @@ func (c *compiler) recordVariableReferences(names []string, namespaces map[strin // Processor 1.0 retains its evaluation-time variable lookup. A nil position // denotes a static host expression (for example a shadow attribute), whose // restricted static-variable context is checked by its compile-time evaluator. - if c.stylesheet.processorLanguage() < 20 || node == nil { + if c.stylesheet.processorLanguage() < processorXSLT20 || node == nil { return nil } @@ -121,7 +121,7 @@ func (s *variableScopeChecker) visible(ctx context.Context, reference pendingVar element, isElement := node.(*xdm.Element) if isElement && element.URI() == lexicon.NamespaceXSLT { - if scope := element.LocalName(); scope == nameStylesheet || scope == fnNameTransform || scope == namePackage { + if scope := element.LocalName(); scope == nameStylesheet || scope == lexicon.XSLTElementTransform || scope == namePackage { break } @@ -169,7 +169,7 @@ func isGlobalBindingElement(element *xdm.Element) bool { } switch parent.LocalName() { - case nameStylesheet, fnNameTransform, namePackage, xslElemOverride: + case nameStylesheet, lexicon.XSLTElementTransform, namePackage, xslElemOverride: return true default: return false @@ -183,7 +183,7 @@ func isScopeBarrier(element *xdm.Element) bool { func implicitVariableVisible(element *xdm.Element, reference pendingVariableReference) bool { switch element.LocalName() { case lexicon.XSLTElementAccumulatorRule: - return reference.name == lexicon.AttrValue && !(reference.accumulatorRuleMatch && element == reference.node) + return reference.name == accumulatorVariableValue && !(reference.accumulatorRuleMatch && element == reference.node) case lexicon.XSLTElementCatch: local, inErrors := strings.CutPrefix(reference.name, "{"+xpath.NSErr+"}") if !inErrors { @@ -191,7 +191,7 @@ func implicitVariableVisible(element *xdm.Element, reference pendingVariableRefe } switch local { - case "code", "description", lexicon.AttrValue, "module", "line-number", "column-number": + case catchVariableCode, catchVariableDescription, catchVariableValue, catchVariableModule, catchVariableLine, catchVariableColumn: return true } } diff --git a/transform/internal/xslt/compile_static.go b/transform/internal/xslt/compile_static.go index 8e5f768..fa27a38 100644 --- a/transform/internal/xslt/compile_static.go +++ b/transform/internal/xslt/compile_static.go @@ -78,7 +78,7 @@ func (c *compiler) preprocessConditionalElement(ctx context.Context, elem *xdm.E return false, err } - if c.stylesheet.processorLanguage() < 20 || !hasConditionalAttributes(elem, c.stylesheet.processorLanguage()) { + if c.stylesheet.processorLanguage() < processorXSLT20 || !hasConditionalAttributes(elem, c.stylesheet.processorLanguage()) { return true, nil } @@ -99,8 +99,8 @@ func (c *compiler) preprocessConditionalElement(ctx context.Context, elem *xdm.E return false, err } - if elem.URI() == lexicon.NamespaceXSLT && c.stylesheet.processorLanguage() >= 30 { - if err := c.resolveSingleShadowAttribute(ctx, elem, nameXpathDefaultNamespace); err != nil { + if elem.URI() == lexicon.NamespaceXSLT && c.stylesheet.processorLanguage() >= processorXSLT30 { + if err := c.resolveSingleShadowAttribute(ctx, elem, nameXPathDefaultNamespace); err != nil { return false, err } @@ -120,7 +120,7 @@ func (c *compiler) preprocessConditionalElement(ctx context.Context, elem *xdm.E } if elem.URI() == lexicon.NamespaceXSLT { - if c.stylesheet.processorLanguage() >= 30 { + if c.stylesheet.processorLanguage() >= processorXSLT30 { if err := c.resolveShadowAttributes(ctx, elem); err != nil { return false, err } @@ -139,7 +139,7 @@ func (c *compiler) preprocessConditionalElement(ctx context.Context, elem *xdm.E } func (c *compiler) validatePreparedLanguage(root *xdm.Element) error { - if _, deferred := c.deferredLanguageChecks[root]; c.stylesheet.processorLanguage() >= 20 && deferred { + if _, deferred := c.deferredLanguageChecks[root]; c.stylesheet.processorLanguage() >= processorXSLT20 && deferred { if err := checkModernLanguage(root, elementXSLTVersion(root), c.stylesheet.processorLanguage()); err != nil { return err } @@ -288,7 +288,7 @@ func hasConditionalAttributes(elem *xdm.Element, processor int) bool { return true } - if processor >= 30 { + if processor >= processorXSLT30 { for _, attr := range elem.Attributes() { if attr.URI() == "" && isShadowAttributeName(attr.LocalName()) { return true @@ -309,7 +309,7 @@ func isUserDefinedDataElement(element *xdm.Element) bool { parent, ok := element.Parent().(*xdm.Element) return ok && parent.URI() == lexicon.NamespaceXSLT && - (parent.LocalName() == nameStylesheet || parent.LocalName() == fnNameTransform || parent.LocalName() == namePackage) + (parent.LocalName() == nameStylesheet || parent.LocalName() == lexicon.XSLTElementTransform || parent.LocalName() == namePackage) } // Remove excluded siblings in one pass, avoiding repeated slice shifts and diff --git a/transform/internal/xslt/compile_streaming.go b/transform/internal/xslt/compile_streaming.go index ac524df..f36c42a 100644 --- a/transform/internal/xslt/compile_streaming.go +++ b/transform/internal/xslt/compile_streaming.go @@ -263,7 +263,7 @@ func (c *compiler) compileIterateBody(ctx context.Context, inst *iterateInst, ch // compileIterateParam compiles an xsl:param inside xsl:iterate. func (c *compiler) compileIterateParam(ctx context.Context, elem *xdm.Element) (*iterateParam, error) { - for _, attribute := range []string{ctxItemRequired, nameTunnel, nameStatic} { + for _, attribute := range []string{xslAttrRequired, nameTunnel, nameStatic} { if xsdBoolTrue(getAttr(elem, attribute)) { return nil, staticError(errCodeXTSE0020, "xsl:iterate parameter must not specify %s=\"yes\"", attribute) } @@ -483,7 +483,7 @@ func (c *compiler) compileAccumulator(ctx context.Context, elem *xdm.Element) er acc := &accumulatorDef{ Name: expandedName, - As: getAttr(elem, "as"), + As: getAttr(elem, nameAs), Streamable: xsdBoolTrue(getAttr(elem, nameStreamable)), ImportPrec: c.importPrec, } @@ -505,7 +505,7 @@ func (c *compiler) compileAccumulator(ctx context.Context, elem *xdm.Element) er } // Read initial-value attribute (required per XSLT 3.0 §10.4) - iv, hasInitial := elem.GetAttribute("initial-value") + iv, hasInitial := elem.GetAttribute(nameInitialValue) if !hasInitial { return staticError(errCodeXTSE0010, "xsl:accumulator %q requires initial-value attribute", expandedName) } @@ -575,6 +575,10 @@ func (c *compiler) compileAccumulator(ctx context.Context, elem *xdm.Element) er // compileAccumulatorRule compiles an xsl:accumulator-rule element. func (c *compiler) compileAccumulatorRule(ctx context.Context, parent *accumulatorDef, elem *xdm.Element) error { + const ( + nameAccumulatorNewValue = "new-value" + ) + defer c.pushElementVersion(elem)() matchAttr, hasMatch := elem.GetAttribute(lexicon.AttrMatch) @@ -589,13 +593,13 @@ func (c *compiler) compileAccumulatorRule(ctx context.Context, parent *accumulat rule := &accumulatorRule{ Match: matchPat, - Phase: getAttr(elem, "phase"), - New: getAttr(elem, "new-value") == lexicon.ValueYes, + Phase: getAttr(elem, namePhase), + New: getAttr(elem, nameAccumulatorNewValue) == lexicon.ValueYes, } // Default phase is "start" per XSLT 3.0 spec §14 if rule.Phase == "" { - rule.Phase = "start" + rule.Phase = accumulatorPhaseStart } selectAttr, hasSelect := elem.GetAttribute(xslAttrSelect) @@ -775,7 +779,7 @@ func (c *compiler) compileMergeSourceSelection(elem *xdm.Element, src *mergeSour } } - if sortAttr, hasSBM := elem.GetAttribute("sort-before-merge"); hasSBM { + if sortAttr, hasSBM := elem.GetAttribute(nameSortBeforeMerge); hasSBM { sortVal := strings.TrimSpace(sortAttr) if v, ok := parseXSDBool(sortVal); ok { src.SortBeforeMerge = v @@ -785,7 +789,7 @@ func (c *compiler) compileMergeSourceSelection(elem *xdm.Element, src *mergeSour } // for-each-source: XPath expression evaluating to sequence of URI strings - if fes, present := elem.GetAttribute("for-each-source"); present { + if fes, present := elem.GetAttribute(nameForEachSource); present { expr, err := c.compileXPath(fes, c.nsBindings) if err != nil { return err @@ -795,7 +799,7 @@ func (c *compiler) compileMergeSourceSelection(elem *xdm.Element, src *mergeSour } // for-each-item: XPath expression evaluating to sequence of items (nodes) - if fei, present := elem.GetAttribute("for-each-item"); present { + if fei, present := elem.GetAttribute(nameForEachItem); present { expr, err := c.compileXPath(fei, c.nsBindings) if err != nil { return err @@ -987,6 +991,8 @@ func instructionChildren(inst instruction) [][]instruction { return result case *sequenceInst: return [][]instruction{v.Body} + case *wherePopulatedInst: + return [][]instruction{v.Body} case *variableInst: return [][]instruction{v.Body} case *literalResultElement: @@ -1132,6 +1138,11 @@ func validateMergeKeyAttrs(elem *xdm.Element) error { // Cyclic dependencies (including self-references) are detected and the // involved accumulators are marked with CyclicDeps=true for XTDE3400. func sortAccumulatorOrder(ss *Stylesheet) { + const ( + accumulatorVisitActive = 1 + accumulatorVisitDone = 2 + ) + deps := accumulatorDependencies(ss) // Detect self-cycles: an accumulator that references itself. @@ -1160,13 +1171,13 @@ func sortAccumulatorOrder(ss *Stylesheet) { visit = func(name string) bool { switch state[name] { - case 2: + case accumulatorVisitDone: return false // already processed - case 1: + case accumulatorVisitActive: return true // cycle detected } - state[name] = 1 + state[name] = accumulatorVisitActive for dep := range deps[name] { if visit(dep) { // Mark all accumulators in the cycle @@ -1180,7 +1191,7 @@ func sortAccumulatorOrder(ss *Stylesheet) { } } - state[name] = 2 + state[name] = accumulatorVisitDone sorted = append(sorted, name) return false @@ -1200,72 +1211,50 @@ func accumulatorDependencies(ss *Stylesheet) map[string]map[string]struct{} { deps[name] = make(map[string]struct{}) for _, rule := range acc.Rules { - var src string - if rule.Select != nil { - src = rule.Select.String() - } - - if src == "" { + if rule.Select == nil { continue } - for _, ref := range extractAccumulatorRefs(src) { - resolved := resolveQName(ref, ss.namespaces) - if _, ok := ss.accumulators[resolved]; ok { - deps[name][resolved] = struct{}{} + // The match and select attributes share the rule's lexical namespaces. + bindings := rule.Match.nsBindings + xpath.WalkExpr(rule.Select.AST(), func(expr xpath.Expr) bool { + call, ok := expr.(xpath.FunctionCall) + if !ok || len(call.Args) != 1 { + return true } - } - } - } - return deps -} - -// extractAccumulatorRefs extracts accumulator names referenced by -// accumulator-before('name') and accumulator-after('name') calls -// from an XPath expression source string. -func extractAccumulatorRefs(src string) []string { - var refs []string - - for _, prefix := range []string{"accumulator-before(", "accumulator-after("} { - s := src - for { - idx := strings.Index(s, prefix) - if idx < 0 { - break - } - - s = s[idx+len(prefix):] + uri, local := patternFunctionIdentity(call, bindings) + if uri != lexicon.NamespaceFn || (local != nameAccumulatorBefore && local != nameAccumulatorAfter) { + return true + } - s = strings.TrimSpace(s) - if len(s) == 0 { - break - } - // Expect a quote character - q := s[0] - if q != '\'' && q != '"' { - break - } + literal, ok := call.Args[0].(xpath.LiteralExpr) + if !ok { + return true + } - s = s[1:] + ref, ok := literal.Value.(string) + if !ok { + return true + } - end := strings.IndexByte(s, q) - if end < 0 { - break - } + resolved := resolveQName(ref, bindings) + if _, ok := ss.accumulators[resolved]; ok { + deps[name][resolved] = struct{}{} + } - refs = append(refs, s[:end]) - s = s[end+1:] + return true + }) } } - return refs + return deps } // mergeSourceAllowedAttrs reports attributes permitted on xsl:merge-source. func mergeSourceAllowedAttrs(name string) bool { switch name { - case xslAttrName, "for-each-item", "for-each-source", xslAttrSelect, nameStreamable, "sort-before-merge", nameUseAccumulators, nameValidation, lexicon.AttrType: + case xslAttrName, nameForEachItem, nameForEachSource, xslAttrSelect, nameStreamable, nameSortBeforeMerge, nameUseAccumulators, nameValidation, lexicon.AttrType: return true default: return false diff --git a/transform/internal/xslt/compile_templates.go b/transform/internal/xslt/compile_templates.go index 8845705..8dc1653 100644 --- a/transform/internal/xslt/compile_templates.go +++ b/transform/internal/xslt/compile_templates.go @@ -18,7 +18,7 @@ import ( func templateAllowedAttrs(key string) bool { switch key { - case lexicon.AttrMatch, xslAttrName, "priority", lexicon.AttrMode, "as", xslAttrVisibility, xslAttrUseWhen: + case lexicon.AttrMatch, xslAttrName, namePriority, lexicon.AttrMode, nameAs, xslAttrVisibility, xslAttrUseWhen: return true } @@ -48,7 +48,7 @@ func (c *compiler) compileTemplate(ctx context.Context, elem *xdm.Element) error savedXPathDefaultNS := c.xpathDefaultNS savedHasXPathDefaultNS := c.hasXPathDefaultNS - if xdn, ok := elem.GetAttribute(nameXpathDefaultNamespace); ok { + if xdn, ok := elem.GetAttribute(nameXPathDefaultNamespace); ok { c.xpathDefaultNS = xdn c.hasXPathDefaultNS = true } @@ -75,7 +75,7 @@ func (c *compiler) compileTemplate(ctx context.Context, elem *xdm.Element) error // Only check the template element itself (not ancestors), since c.baseURI // already accounts for xml:base on the stylesheet root and includes. templateBaseURI := c.baseURI - if xmlBase, ok := elem.GetAttributeNS("base", lexicon.NamespaceXML); ok && xmlBase != "" { + if xmlBase, ok := elem.GetAttributeNS(lexicon.AttrBase, lexicon.NamespaceXML); ok && xmlBase != "" { templateBaseURI = xdm.BuildURI(xmlBase, c.baseURI) } @@ -136,7 +136,7 @@ func (c *compiler) compileTemplate(ctx context.Context, elem *xdm.Element) error tmpl.contextItemType = ctxDecl.itemType } - tmplAs := getAttr(elem, "as") + tmplAs := getAttr(elem, nameAs) if err := c.validateAsSequenceType(ctx, elem, tmplAs, "xsl:template"); err != nil { return err } @@ -152,7 +152,7 @@ func (c *compiler) compileTemplate(ctx context.Context, elem *xdm.Element) error // Only set Version when the template has its own version attribute. // Stylesheet-level version is NOT propagated to templates because // shadow attributes (_version) may override the stylesheet version. - tmpl.Version = getAttr(elem, lexicon.SerializationVersion) + tmpl.Version = getAttr(elem, xslAttrVersion) if c.stylesheet.isPackage { tmpl.OwnerPackage = c.stylesheet } @@ -173,7 +173,7 @@ func (c *compiler) compileTemplateModeAndPriority(ctx context.Context, elem *xdm c.recordModeUsage(ctx, tmpl.Mode) } - if prio := getAttr(elem, "priority"); prio != "" { + if prio := getAttr(elem, namePriority); prio != "" { f, err := parseTemplatePriority(prio) if err != nil { return err @@ -257,7 +257,7 @@ func (c *compiler) checkTemplateName(ctx context.Context, elem *xdm.Element, mat // XTSE0080: template name must not be in the XSLT namespace // Exception: xsl:initial-template is explicitly allowed (XSLT 3.0 §3.11). - if name != "" && strings.HasPrefix(name, "{"+lexicon.NamespaceXSLT+"}") && name != xdm.ClarkName(lexicon.NamespaceXSLT, "initial-template") { + if name != "" && strings.HasPrefix(name, "{"+lexicon.NamespaceXSLT+"}") && name != xdm.ClarkName(lexicon.NamespaceXSLT, initialTemplateLocalName) { return "", staticError(errCodeXTSE0080, "xsl:template name %q is in the XSLT namespace", getAttr(elem, xslAttrName)) } @@ -271,7 +271,7 @@ func (c *compiler) checkTemplateName(ctx context.Context, elem *xdm.Element, mat return "", staticError(errCodeXTSE0500, "xsl:template without @match must not have @mode") } - if _, hasPrio := elem.GetAttribute("priority"); hasPrio { + if _, hasPrio := elem.GetAttribute(namePriority); hasPrio { return "", staticError(errCodeXTSE0500, "xsl:template without @match must not have @priority") } } @@ -370,7 +370,7 @@ func (c *compiler) compileTemplateBodyEx(ctx context.Context, elem *xdm.Element, return nil, nil, nil, err } - return body.context, body.body, body.params, nil + return body.context, body.sequence.body, body.params, nil } // validateContextItem checks compile-time constraints on xsl:context-item. @@ -380,8 +380,8 @@ func (c *compiler) validateContextItem(ctx context.Context, elem *xdm.Element) e return err } - asAttr := getAttr(elem, "as") - if _, present := elem.GetAttribute("as"); present { + asAttr := getAttr(elem, nameAs) + if _, present := elem.GetAttribute(nameAs); present { st, err := xpath.ParseSequenceType(asAttr) if err != nil || st.Void || st.Occurrence != xdm.OccurrenceExactlyOne { return staticError(errCodeXTSE0020, "xsl:context-item/@as must be an ItemType: %q", asAttr) @@ -399,7 +399,7 @@ func (c *compiler) validateContextItem(ctx context.Context, elem *xdm.Element) e "xsl:context-item/@use must be 'required', 'optional', or 'absent': %q", useAttr) } - if _, hasAs := elem.GetAttribute("as"); useAttr == ctxItemAbsent && hasAs { + if _, hasAs := elem.GetAttribute(nameAs); useAttr == ctxItemAbsent && hasAs { return staticError(errCodeXTSE3088, "xsl:context-item with use=\"absent\" must not have @as") } @@ -454,8 +454,8 @@ func (c *compiler) compileParamDef(ctx context.Context, elem *xdm.Element) (*par } // Validate boolean attribute values (including empty string) - if reqAttr, hasReq := elem.GetAttribute(ctxItemRequired); hasReq { - if err := validateBooleanAttr("xsl:param", ctxItemRequired, reqAttr); err != nil { + if reqAttr, hasReq := elem.GetAttribute(xslAttrRequired); hasReq { + if err := validateBooleanAttr("xsl:param", xslAttrRequired, reqAttr); err != nil { return nil, err } } @@ -466,7 +466,7 @@ func (c *compiler) compileParamDef(ctx context.Context, elem *xdm.Element) (*par } } - required := xsdBoolTrue(getAttr(elem, ctxItemRequired)) + required := xsdBoolTrue(getAttr(elem, xslAttrRequired)) if required { if err := validateRequiredParamDefault(elem); err != nil { @@ -496,7 +496,7 @@ func (c *compiler) compileParamDef(ctx context.Context, elem *xdm.Element) (*par "xsl:param %q with static='yes' must not have content (use select attribute instead)", name) } - asAttr := getAttr(elem, "as") + asAttr := getAttr(elem, nameAs) if err := c.validateAsSequenceType(ctx, elem, asAttr, "xsl:param "+name); err != nil { return nil, err } @@ -612,7 +612,7 @@ func (c *compiler) compileGlobalVariable(ctx context.Context, elem *xdm.Element) } } - asAttr := getAttr(elem, "as") + asAttr := getAttr(elem, nameAs) if err := c.validateAsSequenceType(ctx, elem, asAttr, "xsl:variable "+name); err != nil { return err } @@ -660,7 +660,7 @@ func (c *compiler) compileGlobalVariable(ctx context.Context, elem *xdm.Element) } // XPST0008: a global variable is out of scope within its own // declaration. Detect self-references in the select expression. - if c.stylesheet.processorLanguage() < 20 && xpathExprReferencesVar(expr.AST(), name, c.nsBindings) { + if c.stylesheet.processorLanguage() < processorXSLT20 && xpathExprReferencesVar(expr.AST(), name, c.nsBindings) { return staticError(lexicon.ErrXPST0008, "global variable %q references itself in its select expression", name) } @@ -797,7 +797,7 @@ func parseTemplatePriority(prio string) (float64, error) { } // xs:decimal ignores leading and trailing whitespace; isXSDecimal already // trims it, so ParseFloat must see the trimmed form too. - f, err := strconv.ParseFloat(strings.TrimSpace(prio), 64) + f, err := strconv.ParseFloat(strings.TrimSpace(prio), float64ParseBits) if err != nil { return 0, staticError(errCodeXTSE0530, "invalid priority %q: %v", prio, err) } @@ -887,7 +887,7 @@ func defaultModeForElement(ctx context.Context, elem *xdm.Element) (string, erro continue } - name := strings.Trim(raw, " \t\r\n") + name := strings.Trim(raw, lexicon.XMLWhitespace) if name == modeUnnamed { return "", nil } @@ -1200,7 +1200,7 @@ type templateBodyCompiler struct { compiler *compiler context *contextItemDecl params []*param - body []instruction + sequence sequenceConstructorState isFunction bool inParams bool sawContent bool @@ -1252,7 +1252,7 @@ func (b *templateBodyCompiler) compileElement(ctx context.Context, elem *xdm.Ele } if inst != nil { - b.body = append(b.body, inst) + return b.sequence.add(inst) } return nil @@ -1279,7 +1279,7 @@ func (b *templateBodyCompiler) compileContextItem(ctx context.Context, elem *xdm return err } - as := defaultContextItemType(getAttr(elem, "as")) + as := defaultContextItemType(getAttr(elem, nameAs)) st, err := xpath.ParseSequenceType(as) if err != nil { @@ -1295,7 +1295,7 @@ func (b *templateBodyCompiler) compileContextItem(ctx context.Context, elem *xdm // An omitted use stays empty; runtime selects the named/match default. b.context = &contextItemDecl{ - as: getAttr(elem, "as"), use: strings.TrimSpace(getAttr(elem, lexicon.AttrUse)), + as: getAttr(elem, nameAs), use: strings.TrimSpace(getAttr(elem, lexicon.AttrUse)), typeKey: b.compiler.declarationTypeKey(elem, as), itemType: &bound, } @@ -1336,15 +1336,13 @@ func (b *templateBodyCompiler) appendText(text string) error { inst.TVT = avt } - b.body = append(b.body, inst) - - return nil + return b.sequence.add(inst) } // contextItemAllowedAttrs lists the valid attributes for xsl:context-item. func contextItemAllowedAttrs(key string) bool { switch key { - case "as", lexicon.AttrUse: + case nameAs, lexicon.AttrUse: return true } diff --git a/transform/internal/xslt/compile_toplevel.go b/transform/internal/xslt/compile_toplevel.go index 31b0d49..df44161 100644 --- a/transform/internal/xslt/compile_toplevel.go +++ b/transform/internal/xslt/compile_toplevel.go @@ -58,7 +58,7 @@ func (c *compiler) compileTopLevel(ctx context.Context, root *xdm.Element) error dv := c.stylesheet.defaultValidation if requiresSchemaAttribute(nameDefaultValidation, dv, c.stylesheet.processorLanguage()) && !c.stylesheet.schemaAware { - return staticError("XTSE1660", "default-validation=%q requires a schema-aware processor", dv) + return staticError(errCodeXTSE1660, "default-validation=%q requires a schema-aware processor", dv) } if err := c.collectTopLevelImports(ctx, root); err != nil { @@ -234,13 +234,13 @@ func (c *compiler) compileStaticSelect(ctx context.Context, elem *xdm.Element) ( // Static values must be converted before another declaration, use-when or // shadow attribute observes them, rather than only when the stylesheet runs. func (c *compiler) convertStaticDeclarationValue(ctx context.Context, elem *xdm.Element, value xdm.Sequence) (xdm.Sequence, error) { - if elem.LocalName() == nameParam && xsdBoolTrue(getAttr(elem, ctxItemRequired)) { + if elem.LocalName() == nameParam && xsdBoolTrue(getAttr(elem, xslAttrRequired)) { if err := validateRequiredParamDefault(elem); err != nil { return nil, err } } - as := getAttr(elem, "as") + as := getAttr(elem, nameAs) if err := c.validateAsSequenceType(ctx, elem, as, "static "+elem.LocalName()); err != nil { return nil, err } @@ -283,12 +283,12 @@ func (c *compiler) bindImplicitStaticParam(ctx context.Context, elem *xdm.Elemen return nil } - as := getAttr(elem, "as") + as := getAttr(elem, nameAs) if err := c.validateAsSequenceType(ctx, elem, as, "static parameter"); err != nil { return err } - if xsdBoolTrue(getAttr(elem, ctxItemRequired)) || as != "" && !allowsEmptySequence(as) { + if xsdBoolTrue(getAttr(elem, xslAttrRequired)) || as != "" && !allowsEmptySequence(as) { return staticError(errCodeXTDE0050, "required static parameter $%s was not supplied", getAttr(elem, xslAttrName)) } @@ -462,8 +462,8 @@ func (c *compiler) topLevelUseWhen(ctx context.Context, elem *xdm.Element) (bool } func (c *compiler) ignoredTopLevelDeclaration(elem *xdm.Element) bool { - if c.stylesheet.processorLanguage() >= 30 { - if _, shadow := elem.GetAttribute("_version"); shadow { + if c.stylesheet.processorLanguage() >= processorXSLT30 { + if _, shadow := elem.GetAttribute(shadowAttrVersion); shadow { return false } } @@ -472,7 +472,11 @@ func (c *compiler) ignoredTopLevelDeclaration(elem *xdm.Element) bool { } func (c *compiler) validateModuleImportOrder(ctx context.Context, root *xdm.Element) error { - if c.stylesheet.processorLanguage() >= 30 { + const ( + errCodeXTSE0200 = "XTSE0200" + ) + + if c.stylesheet.processorLanguage() >= processorXSLT30 { return nil } @@ -503,7 +507,7 @@ func (c *compiler) validateModuleImportOrder(ctx context.Context, root *xdm.Elem if elem.URI() == lexicon.NamespaceXSLT && elem.LocalName() == nameImport { if seenOther { - return staticError("XTSE0200", "xsl:import must precede other module elements in XSLT 1.0/2.0") + return staticError(errCodeXTSE0200, "xsl:import must precede other module elements in XSLT 1.0/2.0") } } else { seenOther = true @@ -583,7 +587,7 @@ func (c *compiler) topLevelDeclarationIncluded(ctx context.Context, elem *xdm.El } if !isKnownXSLTElement(ln) || !isTopLevelXSLTElement(ln) { - elemVer := getAttr(elem, lexicon.SerializationVersion) + elemVer := getAttr(elem, xslAttrVersion) if c.forwardsCompatible(c.effectiveVersion) || c.forwardsCompatible(elemVer) { return false, nil } @@ -651,7 +655,7 @@ func (c *compiler) compileTopLevelDeclaration(ctx context.Context, elem *xdm.Ele case lexicon.XSLTElementGlobalContextItem: return c.compileGlobalContextItem(ctx, elem) default: - elemVer := getAttr(elem, lexicon.SerializationVersion) + elemVer := getAttr(elem, xslAttrVersion) if !c.forwardsCompatible(c.effectiveVersion) && !c.forwardsCompatible(elemVer) { return staticError(errCodeXTSE0010, "element xsl:%s is not allowed as a top-level declaration", elem.LocalName()) @@ -734,12 +738,12 @@ func (c *compiler) checkConflictingNamespaceAliases(_ context.Context) error { // compileNamespaceAlias compiles an xsl:namespace-alias declaration. func (c *compiler) compileNamespaceAlias(ctx context.Context, elem *xdm.Element) error { - if err := c.validateXSLTAttrs(ctx, elem, attributeNames("stylesheet-prefix", "result-prefix", xslAttrUseWhen)); err != nil { + if err := c.validateXSLTAttrs(ctx, elem, attributeNames(nameStylesheetPrefix, nameResultPrefix, xslAttrUseWhen)); err != nil { return err } - stylesheetPrefix, hasStylesheetPrefix := elem.GetAttribute("stylesheet-prefix") - resultPrefix, hasResultPrefix := elem.GetAttribute("result-prefix") + stylesheetPrefix, hasStylesheetPrefix := elem.GetAttribute(nameStylesheetPrefix) + resultPrefix, hasResultPrefix := elem.GetAttribute(nameResultPrefix) if !hasStylesheetPrefix { return staticError(errCodeXTSE0010, "xsl:namespace-alias requires stylesheet-prefix attribute") diff --git a/transform/internal/xslt/compile_types.go b/transform/internal/xslt/compile_types.go index 868fada..fc5c86a 100644 --- a/transform/internal/xslt/compile_types.go +++ b/transform/internal/xslt/compile_types.go @@ -14,7 +14,11 @@ import ( ) func (c *compiler) compileImportSchema(ctx context.Context, elem *xdm.Element) error { - return staticError("XTSE1650", "schema imports require a schema-aware processor") + const ( + errCodeXTSE1650 = "XTSE1650" + ) + + return staticError(errCodeXTSE1650, "schema imports require a schema-aware processor") } // schemaDeclsForValidation returns the in-scope schema declarations for @@ -35,7 +39,7 @@ func (c *compiler) schemaDeclsForValidation() xpath.SchemaDeclarations { return // using schema-element() or schema-attribute() with an undeclared name. func (c *compiler) validateAsSequenceType(ctx context.Context, elem *xdm.Element, as string, context string) error { if as == "" { - if _, present := elem.GetAttribute("as"); present { + if _, present := elem.GetAttribute(nameAs); present { return staticError(lexicon.ErrXPST0003, "%s: empty 'as' type", context) } } @@ -70,7 +74,7 @@ func (c *compiler) validateAsSequenceTypeWithNS(_ context.Context, as string, co } if requiresSchemaNodeTest(st.ItemTest, ns, defaultNS, hasDefaultNS) { - return staticError("XTSE1660", "%s: 'as' requires schema-annotated nodes", context) + return staticError(errCodeXTSE1660, "%s: 'as' requires schema-annotated nodes", context) } if err := xpath.ValidateSequenceTypeForVersion(st, ns, defaultNS, nil, c.stylesheet.xpathVersion()); err != nil { @@ -109,10 +113,10 @@ func requiresSchemaNodeTest(test xdm.NodeTest, ns map[string]string, defaultNS s } if attribute { - return local != "untypedAtomic" && local != "anyAtomicType" && local != "anySimpleType" + return local != lexicon.TypeUntypedAtomic && local != lexicon.TypeAnyAtomicType && local != lexicon.TypeAnySimpleType } - return local != strings.TrimPrefix(lexicon.XSUntyped, "xs:") && local != "anyType" + return local != strings.TrimPrefix(lexicon.XSUntyped, lexicon.PrefixXS+":") && local != lexicon.TypeAnyType } // checkTypedModePatterns validates XTSE3105: for modes declared with @@ -147,7 +151,7 @@ func resolveXSDTypeNameNS(qname string, nsBindings map[string]string, xpathDefau local := qname[closeIdx+1:] if uri == lexicon.NamespaceXSD { - return "xs:" + local + return lexicon.PrefixXS + ":" + local } return qname @@ -155,13 +159,13 @@ func resolveXSDTypeNameNS(qname string, nsBindings map[string]string, xpathDefau } if prefix, local, ok := strings.Cut(qname, ":"); ok { - if prefix == "xs" || prefix == nameXsd { - return "xs:" + local + if prefix == lexicon.PrefixXS || prefix == lexicon.PrefixXSD { + return lexicon.PrefixXS + ":" + local } if uri, ok := nsBindings[prefix]; ok { if uri == lexicon.NamespaceXSD { - return "xs:" + local + return lexicon.PrefixXS + ":" + local } // User-defined type: resolve to Q{ns}local canonical form. return xdm.QAnnotation(uri, local) diff --git a/transform/internal/xslt/dispatch_index.go b/transform/internal/xslt/dispatch_index.go index 52ad2bd..eec8a73 100644 --- a/transform/internal/xslt/dispatch_index.go +++ b/transform/internal/xslt/dispatch_index.go @@ -261,7 +261,8 @@ func locationPathSignature(p *pattern, path xpath.LocationPath) signature { // nodeMatchesStep's gates (compile_patterns.go) exactly: predicates are // ignored (they can only reject, never widen a match), the axis restricts the // node-test's own kind set (attribute:: to {attribute}, namespace:: to -// {namespace}, every other axis excludes attribute/namespace/document except +// {namespace}, self axes may preserve attribute/namespace selected by an +// earlier step, and other axes exclude attribute/namespace/document except // where a DocumentTest or self::node() explicitly re-admits document), and a // concrete NameTest on a kind set narrowed to exactly {element} or // {attribute} additionally narrows to a name key. @@ -276,7 +277,11 @@ func stepSignature(p *pattern, step xpath.Step) signature { case xpath.AxisNamespace: ks = tk & kindBit(kindNamespace) default: - ks = tk &^ (kindBit(kindAttribute) | kindBit(kindNamespace) | kindBit(kindDocument)) + ks = tk &^ kindBit(kindDocument) + if step.Axis != xpath.AxisSelf && step.Axis != xpath.AxisDescendantOrSelf { + ks &^= kindBit(kindAttribute) | kindBit(kindNamespace) + } + if isDocumentTest(step.NodeTest) || (step.Axis == xpath.AxisSelf && isNodeKindNodeTest(step.NodeTest)) { ks |= kindBit(kindDocument) } @@ -365,7 +370,7 @@ func isConcreteName(nt xdm.NameTest) bool { // exactly p.nsBindings/p.xpathDefaultNS) so the two resolution paths cannot // drift apart. func resolveStaticPatternName(nt xdm.NameTest, nsBindings map[string]string, xpathDefaultNS string, isElem bool) string { - if nt.URI != "" { + if nt.URIQualified || nt.URI != "" { return nt.URI } diff --git a/transform/internal/xslt/dispatch_index_test.go b/transform/internal/xslt/dispatch_index_test.go index 94fd83e..b16b947 100644 --- a/transform/internal/xslt/dispatch_index_test.go +++ b/transform/internal/xslt/dispatch_index_test.go @@ -1,6 +1,10 @@ package xslt //nolint:testpackage // Exercises private candidate ordering and conservative pattern indexing. import ( + "context" + "errors" + "math" + "math/big" "slices" "testing" @@ -208,7 +212,8 @@ func TestPatternIndexAxisAndKindGuards(t *testing.T) { {"schema attribute stays in the attribute bucket", xpath.Step{Axis: xpath.AxisAttribute, NodeTest: xdm.SchemaAttributeTest{Name: "item"}}, []sigEntry{{kind: kindAttribute}}}, {"schema element stays in the element bucket", xpath.Step{Axis: xpath.AxisChild, NodeTest: xdm.SchemaElementTest{Name: "item"}}, []sigEntry{{kind: kindElement}}}, {"self document test retains documents", xpath.Step{Axis: xpath.AxisSelf, NodeTest: xdm.DocumentTest{}}, []sigEntry{{kind: kindDocument}}}, - {"self node test retains documents", xpath.Step{Axis: xpath.AxisSelf, NodeTest: xdm.TypeTest{Kind: xdm.NodeKindNode}}, []sigEntry{{kind: kindElement}, {kind: kindText}, {kind: kindComment}, {kind: kindPI}, {kind: kindDocument}}}, + {"self node test retains documents and side nodes", xpath.Step{Axis: xpath.AxisSelf, NodeTest: xdm.TypeTest{Kind: xdm.NodeKindNode}}, []sigEntry{{kind: kindElement}, {kind: kindAttribute}, {kind: kindText}, {kind: kindComment}, {kind: kindPI}, {kind: kindDocument}, {kind: kindNamespace}}}, + {"descendant or self retains side nodes", xpath.Step{Axis: xpath.AxisDescendantOrSelf, NodeTest: xdm.TypeTest{Kind: xdm.NodeKindNode}}, []sigEntry{{kind: kindElement}, {kind: kindAttribute}, {kind: kindText}, {kind: kindComment}, {kind: kindPI}, {kind: kindNamespace}}}, {"child node test excludes documents attributes and namespaces", xpath.Step{Axis: xpath.AxisChild, NodeTest: xdm.TypeTest{Kind: xdm.NodeKindNode}}, []sigEntry{{kind: kindElement}, {kind: kindText}, {kind: kindComment}, {kind: kindPI}}}, {"PI kind test", xpath.Step{Axis: xpath.AxisChild, NodeTest: xdm.TypeTest{Kind: xdm.NodeKindProcessingInstruction}}, []sigEntry{{kind: kindPI}}}, } { @@ -244,3 +249,155 @@ func TestPatternIndexConsidersEveryAlternative(t *testing.T) { }) } } + +func TestIntegerIndexClassifierPreservesExactDomain(t *testing.T) { + for _, test := range []struct { + name string + value xdm.AtomicValue + want int64 + ok bool + }{ + {"zero", xdm.AtomicValue{TypeName: xdm.TypeInteger, Value: int64(0)}, 0, true}, + {"min", xdm.AtomicValue{TypeName: xdm.TypeInteger, Value: int64(math.MinInt64)}, math.MinInt64, true}, + {"max", xdm.AtomicValue{TypeName: xdm.TypeInteger, Value: int64(math.MaxInt64)}, math.MaxInt64, true}, + {"fitting big integer", xdm.AtomicValue{TypeName: xdm.TypeInteger, Value: big.NewInt(23)}, 23, true}, + {"builtin subtype", xdm.AtomicValue{TypeName: xdm.TypeUnsignedByte, Value: int64(23)}, 23, true}, + {"schema base", xdm.AtomicValue{TypeName: "Q{urn:test}integer", BaseType: xdm.TypeLong, Value: int64(23)}, 23, true}, + {"decimal", xdm.AtomicValue{TypeName: xdm.TypeDecimal, Value: big.NewRat(23, 1)}, 0, false}, + {"double", xdm.AtomicValue{TypeName: xdm.TypeDouble, Value: xdm.NewDouble(23)}, 0, false}, + {"float", xdm.AtomicValue{TypeName: xdm.TypeFloat, Value: xdm.NewFloat(23)}, 0, false}, + {"oversized integer", xdm.AtomicValue{TypeName: xdm.TypeInteger, Value: new(big.Int).Lsh(big.NewInt(1), 80)}, 0, false}, + {"string", xdm.AtomicValue{TypeName: xdm.TypeString, Value: "23"}, 0, false}, + } { + t.Run(test.name, func(t *testing.T) { + got, ok := indexInt64(test.value) + if ok != test.ok || got != test.want { + t.Fatal(got, ok, test.want, test.ok) + } + }) + } +} + +func TestIntegerKeyIndexIsLazyOrderedAndQuerySpecific(t *testing.T) { + ctx := t.Context() + entries := integerIndexEntries(128) + for i := range entries { + entries[i].key.Value = int64(i % 64) + } + var table keyTable + query := xdm.AtomicValue{TypeName: xdm.TypeInteger, Value: int64(5)} + for range 2 { + positions, indexed, err := table.integerCandidates(ctx, query, entries) + if err != nil || indexed || positions != nil || table.integerIndex != nil { + t.Fatal("eager index", positions, indexed, err) + } + } + positions, indexed, err := table.integerCandidates(ctx, query, entries) + if err != nil || !indexed || !slices.Equal(positions, []int{5, 69}) { + t.Fatal("lost candidate order or multiplicity", positions, indexed, err) + } + for _, mixed := range []xdm.AtomicValue{ + {TypeName: xdm.TypeFloat, Value: xdm.NewFloat(5)}, + {TypeName: xdm.TypeDecimal, Value: big.NewRat(5, 1)}, + {TypeName: xdm.TypeInteger, Value: new(big.Int).Lsh(big.NewInt(1), 80)}, + } { + positions, indexed, err := table.integerCandidates(ctx, mixed, entries) + if err != nil || indexed || positions != nil || table.integerIndex == nil { + t.Fatal("mixed query must use broad bucket", positions, indexed, err) + } + } + positions, indexed, err = table.integerCandidates(ctx, query, entries) + if err != nil || !indexed || !slices.Equal(positions, []int{5, 69}) { + t.Fatal("integer index changed after fallback queries", positions, indexed, err) + } +} + +func TestIntegerKeyIndexRejectsMixedEntriesAndOversizedCache(t *testing.T) { + for _, test := range []struct { + name string + entries []keyEntry + ctx context.Context + }{ + {"mixed last entry", integerIndexEntries(64), t.Context()}, + {"collection bound", integerIndexEntries(64), xpath.WithBudget(t.Context(), 1000, 32, 10000, 128)}, + } { + t.Run(test.name, func(t *testing.T) { + if test.name == "mixed last entry" { + test.entries[len(test.entries)-1].key = xdm.AtomicValue{TypeName: xdm.TypeFloat, Value: xdm.NewFloat(1)} + } + table := keyTable{integerLookups: 2} + query := xdm.AtomicValue{TypeName: xdm.TypeInteger, Value: int64(1)} + for range 2 { + positions, indexed, err := table.integerCandidates(test.ctx, query, test.entries) + if err != nil || indexed || positions != nil || table.integerIndex != nil || !table.integerDisabled { + t.Fatal("unsafe or repeated partial index", positions, indexed, err) + } + } + }) + } + for _, size := range []int{1, 63} { + var table keyTable + for range 4 { + _, indexed, err := table.integerCandidates(t.Context(), xdm.AtomicValue{TypeName: xdm.TypeInteger, Value: int64(1)}, integerIndexEntries(size)) + if indexed || err != nil || table.integerIndex != nil { + t.Fatal("small table built auxiliary index", size, indexed, err) + } + } + } +} + +func TestIntegerKeyIndexBuildFailureDoesNotPublish(t *testing.T) { + canceled, cancel := context.WithCancel(t.Context()) + cancel() + for _, test := range []struct { + name string + ctx context.Context + want error + }{ + {"work during build", xpath.WithBudget(t.Context(), 3, 1000, 10000, 128), xpath.ErrBudget}, + {"canceled", canceled, context.Canceled}, + } { + t.Run(test.name, func(t *testing.T) { + table := keyTable{integerLookups: 2} + positions, indexed, err := table.integerCandidates(test.ctx, xdm.AtomicValue{TypeName: xdm.TypeInteger, Value: int64(1)}, integerIndexEntries(64)) + if !errors.Is(err, test.want) || indexed || positions != nil || table.integerIndex != nil { + t.Fatal("partial index escaped failure", positions, indexed, err) + } + }) + } +} + +func TestIntegerGroupingDisablesPrefixBeforeCardinalityExpansion(t *testing.T) { + for _, keys := range []xdm.Sequence{nil, xdm.ItemSlice{integerIndexAtomic(1), integerIndexAtomic(1), integerIndexAtomic(3)}} { + ctx := t.Context() + var execution execContext + groups := groupByAccumulator{ + byKey: make(map[string][]*groupByEntry), limit: 100, int64Prefix: true, + equal: func(a, b xdm.Sequence) (bool, error) { return execution.groupingKeysEqual(ctx, a, b, nil) }, + } + if err := groups.addKeys(ctx, integerIndexAtomic(1), xdm.ItemSlice{integerIndexAtomic(1)}); err != nil { + t.Fatal(err) + } + if err := groups.addKeys(ctx, integerIndexAtomic(2), keys); err != nil { + t.Fatal(err) + } + if groups.int64Prefix || groups.byInt64 != nil { + t.Fatal("non-singleton key retained pure-prefix map") + } + if xdm.SequenceLen(keys) > 0 && (len(groups.order) != 2 || len(groups.order[0].items) != 2 || len(groups.order[1].items) != 1) { + t.Fatal("multiple keys duplicated membership", groups.order) + } + } +} + +func integerIndexEntries(size int) []keyEntry { + entries := make([]keyEntry, size) + for i := range entries { + entries[i].key = integerIndexAtomic(int64(i)) + } + return entries +} + +func integerIndexAtomic(value int64) xdm.AtomicValue { + return xdm.AtomicValue{TypeName: xdm.TypeInteger, Value: value} +} diff --git a/transform/internal/xslt/element_info_data.go b/transform/internal/xslt/element_info_data.go index b8c1781..f8cd00d 100644 --- a/transform/internal/xslt/element_info_data.go +++ b/transform/internal/xslt/element_info_data.go @@ -128,8 +128,7 @@ func lookupConstructorElementInfo(name string) (xsltElementInfo, bool) { minVersion: lexicon.XSLTVersion20, context: elementInstruction, implemented: true, }, true case lexicon.XSLTElementMap, - lexicon.XSLTElementMapEntry, - lexicon.XSLTElementArray: + lexicon.XSLTElementMapEntry: return xsltElementInfo{ minVersion: lexicon.XSLTVersion30, context: elementInstruction, implemented: true, }, true diff --git a/transform/internal/xslt/errors.go b/transform/internal/xslt/errors.go index faff90d..7e263dd 100644 --- a/transform/internal/xslt/errors.go +++ b/transform/internal/xslt/errors.go @@ -11,9 +11,13 @@ import ( "github.com/skarm/xmlsec/transform/internal/xdm" ) -// XSLT error codes per the W3C XSLT 3.0 specification. -// Sorted by prefix (FODC, SEPM, SERE, SESU, XPST, XSLT, XTDE, XTMM, XTRE, XTSE, XTTE) -// then numerically within each prefix. +// Exact diagnostic identifiers shared by these XSLT checks; naming a code does not change its selected error path or message. +const ( + errCodeXTDE0610 = "XTDE0610" + errCodeXTSE1660 = "XTSE1660" +) + +// Selected XSLT diagnostic conditions; call sites choose the reported code. const ( // FOXT — Functions and Operators: XSLT transformation errCodeFOXT0001 = "FOXT0001" // no suitable XSLT processor satisfies the request @@ -31,8 +35,8 @@ const ( errCodeXSLT0000 = "XSLT0000" // generic catch code for non-XSLT errors // XTDE — Dynamic Errors - errCodeXTDE0030 = "XTDE0030" // invalid language code (xsl:number, xsl:sort, etc.) - errCodeXTDE0290 = "XTDE0290" // invalid URI for xsl:result-document output + errCodeXTDE0030 = "XTDE0030" // invalid evaluated attribute value + errCodeXTDE0290 = "XTDE0290" // unbound prefix in evaluated QName errCodeXTDE0040 = "XTDE0040" // initial template not public errCodeXTDE0041 = "XTDE0041" // initial function not found or not public errCodeXTDE0044 = "XTDE0044" // initial mode without an initial match selection @@ -40,18 +44,18 @@ const ( errCodeXTDE0050 = "XTDE0050" // required stylesheet parameter not supplied errCodeXTDE0160 = "XTDE0160" // multiple output documents same URI errCodeXTDE0410 = "XTDE0410" // attribute or namespace node after child content - errCodeXTDE0420 = "XTDE0420" // namespace conflict - errCodeXTDE0430 = "XTDE0430" // variable type error + errCodeXTDE0420 = "XTDE0420" // attribute/namespace inside document content + errCodeXTDE0430 = "XTDE0430" // conflicting bindings for one prefix errCodeXTDE0440 = "XTDE0440" // default namespace on element in no namespace errCodeXTDE0450 = "XTDE0450" // non-node item in result tree errCodeXTDE0540 = "XTDE0540" // ambiguous rule match (on-multiple-match=fail) errCodeXTDE0555 = "XTDE0555" // on-no-match=fail with no matching template - errCodeXTDE0560 = "XTDE0560" // copy-of type error + errCodeXTDE0560 = "XTDE0560" // absent rule for next-match/apply-imports errCodeXTDE0640 = "XTDE0640" // circular reference errCodeXTDE0700 = "XTDE0700" // required template parameter was not supplied - errCodeXTDE0820 = "XTDE0820" // dynamic type error in template match + errCodeXTDE0820 = "XTDE0820" // invalid computed element QName errCodeXTDE0830 = "XTDE0830" // undeclared prefix in computed element name - errCodeXTDE0835 = "XTDE0835" // xsl:message terminate=yes + errCodeXTDE0835 = "XTDE0835" // invalid computed element namespace URI errCodeXTDE0850 = "XTDE0850" // xsl:attribute name is not a valid QName errCodeXTDE0855 = "XTDE0855" // xsl:attribute name evaluates to "xmlns" errCodeXTDE0860 = "XTDE0860" // undeclared prefix in computed attribute name @@ -95,7 +99,7 @@ const ( errCodeXTDE3175 = "XTDE3175" // xsl:evaluate is disabled errCodeXTDE3340 = "XTDE3340" // unknown accumulator name errCodeXTDE3350 = "XTDE3350" // accumulator-before/after called with no context item - errCodeXTDE3362 = "XTDE3362" // streaming accumulator error + errCodeXTDE3362 = "XTDE3362" // accumulator unavailable for this tree errCodeXTDE3365 = "XTDE3365" // duplicate key in xsl:map construction errCodeXTDE3400 = "XTDE3400" // cyclic accumulator dependency errCodeXTDE3480 = "XTDE3480" // current-merge-group unavailable outside merge-action @@ -110,16 +114,16 @@ const ( errCodeXTMM9001 = "XTMM9001" // xsl:assert default error code // XTSE — Static Errors - errCodeXTSE0010 = "XTSE0010" // generic static error - errCodeXTSE0020 = "XTSE0020" // reserved namespace - errCodeXTSE0080 = "XTSE0080" // duplicate template name - errCodeXTSE0090 = "XTSE0090" // unknown XSLT element - errCodeXTSE0110 = "XTSE0110" // required attribute missing - errCodeXTSE0120 = "XTSE0120" // unsupported version - errCodeXTSE0125 = "XTSE0125" // duplicate package version with same package-version - errCodeXTSE0130 = "XTSE0130" // duplicate global variable - errCodeXTSE0150 = "XTSE0150" // xsl:import-schema after non-import children - errCodeXTSE0165 = "XTSE0165" // invalid xpath expression + errCodeXTSE0010 = "XTSE0010" // invalid placement, attributes or content + errCodeXTSE0020 = "XTSE0020" // invalid fixed attribute value + errCodeXTSE0080 = "XTSE0080" // reserved namespace in component name + errCodeXTSE0090 = "XTSE0090" // disallowed stylesheet attribute + errCodeXTSE0110 = "XTSE0110" // nondecimal stylesheet version + errCodeXTSE0120 = "XTSE0120" // text child of stylesheet/package + errCodeXTSE0125 = "XTSE0125" // no recognized default collation + errCodeXTSE0130 = "XTSE0130" // unnamespaced top-level child + errCodeXTSE0150 = "XTSE0150" // simplified stylesheet lacks xsl:version + errCodeXTSE0165 = "XTSE0165" // module retrieval/representation failure errCodeXTSE0170 = "XTSE0170" // xsl:include is not top-level errCodeXTSE0180 = "XTSE0180" // include cycle errCodeXTSE0190 = "XTSE0190" // xsl:import is not top-level @@ -129,7 +133,7 @@ const ( errCodeXTSE0260 = "XTSE0260" // element required to be empty has content errCodeXTSE0265 = "XTSE0265" // conflicting input-type-annotations across modules errCodeXTSE0270 = "XTSE0270" // conflicting strip-space/preserve-space - errCodeXTSE0280 = "XTSE0280" // element not allowed in context + errCodeXTSE0280 = "XTSE0280" // unbound lexical QName prefix errCodeXTSE0340 = "XTSE0340" // invalid pattern syntax errCodeXTSE0350 = "XTSE0350" // unmatched opening brace in value template errCodeXTSE0370 = "XTSE0370" // unescaped closing brace in value template @@ -139,37 +143,37 @@ const ( errCodeXTSE0550 = "XTSE0550" // invalid mode name on xsl:template errCodeXTSE0580 = "XTSE0580" // duplicate parameter name errCodeXTSE0590 = "XTSE0590" // as= references undeclared schema type/element/attribute - errCodeXTSE0620 = "XTSE0620" // cannot write to output + errCodeXTSE0620 = "XTSE0620" // binding combines select and content errCodeXTSE0630 = "XTSE0630" // duplicate global variable or parameter errCodeXTSE0650 = "XTSE0650" // unavailable named template reference errCodeXTSE0660 = "XTSE0660" // duplicate effective named template errCodeXTSE0670 = "XTSE0670" // duplicate sibling xsl:with-param name - errCodeXTSE0680 = "XTSE0680" // invalid xsl:number + errCodeXTSE0680 = "XTSE0680" // undeclared non-tunnel call-template parameter errCodeXTSE0690 = "XTSE0690" // required parameter not supplied in call-template errCodeXTSE0710 = "XTSE0710" // undeclared attribute-set reference errCodeXTSE0720 = "XTSE0720" // cyclic use-attribute-sets - errCodeXTSE0730 = "XTSE0730" // xsl:attribute-set circular reference + errCodeXTSE0730 = "XTSE0730" // referenced attribute set lacks streamability errCodeXTSE0740 = "XTSE0740" // function name has no namespace - errCodeXTSE0760 = "XTSE0760" // conflicting xsl:mode property values - errCodeXTSE0770 = "XTSE0770" // override type mismatch + errCodeXTSE0760 = "XTSE0760" // default value on function parameter + errCodeXTSE0770 = "XTSE0770" // duplicate function name/arity/precedence errCodeXTSE0800 = "XTSE0800" // reserved namespace used as extension namespace - errCodeXTSE0805 = "XTSE0805" // conflicting namespace binding on LRE - errCodeXTSE0808 = "XTSE0808" // invalid combination of xsl:on-empty/xsl:on-non-empty + errCodeXTSE0805 = "XTSE0805" // disallowed XSLT attribute on LRE + errCodeXTSE0808 = "XTSE0808" // unbound excluded namespace prefix errCodeXTSE0809 = "XTSE0809" // #default in exclude-result-prefixes with no default namespace errCodeXTSE0810 = "XTSE0810" // conflicting namespace-alias declarations errCodeXTSE0812 = "XTSE0812" // namespace-alias prefix is unbound errCodeXTSE0840 = "XTSE0840" // xsl:attribute with select and non-empty content errCodeXTSE0870 = "XTSE0870" // xsl:value-of with select and non-empty content errCodeXTSE0910 = "XTSE0910" // xsl:namespace select/content exclusivity - errCodeXTSE0940 = "XTSE0940" // select+content exclusivity on comment/PI + errCodeXTSE0940 = "XTSE0940" // xsl:comment combines select and content errCodeXTSE0975 = "XTSE0975" // xsl:number value with select/level/count/from errCodeXTSE1015 = "XTSE1015" // xsl:sort with select must be empty errCodeXTSE1017 = "XTSE1017" // stable attribute only on first xsl:sort - errCodeXTSE1040 = "XTSE1040" // xsl:merge-source child position constraint - errCodeXTSE1060 = "XTSE1060" // for-each-group missing grouping attribute - errCodeXTSE1070 = "XTSE1070" // for-each-group multiple grouping attributes - errCodeXTSE1080 = "XTSE1080" // for-each-group invalid attribute combination - errCodeXTSE1090 = "XTSE1090" // xsl:merge-source must be empty + errCodeXTSE1040 = "XTSE1040" // invalid perform-sort content with select + errCodeXTSE1060 = "XTSE1060" // current-group inside a pattern + errCodeXTSE1070 = "XTSE1070" // current-grouping-key inside a pattern + errCodeXTSE1080 = "XTSE1080" // missing or multiple grouping selectors + errCodeXTSE1090 = "XTSE1090" // collation/composite without key-based grouping errCodeXTSE1130 = "XTSE1130" // xsl:analyze-string missing matching branches errCodeXTSE1205 = "XTSE1205" // xsl:key use/content exclusivity errCodeXTSE1210 = "XTSE1210" // invalid collation URI on xsl:key @@ -178,7 +182,7 @@ const ( errCodeXTSE1290 = "XTSE1290" // conflicting decimal-format declarations errCodeXTSE1295 = "XTSE1295" // zero-digit is not a Unicode digit-zero character errCodeXTSE1300 = "XTSE1300" // decimal-format characters conflict (same char for two roles) - errCodeXTSE1430 = "XTSE1430" // xsl:on-empty/xsl:on-non-empty ordering constraint + errCodeXTSE1430 = "XTSE1430" // unbound extension-element-prefixes token errCodeXTSE1560 = "XTSE1560" // conflicting xsl:output declarations errCodeXTSE1570 = "XTSE1570" // invalid output method errCodeXTSE1580 = "XTSE1580" // duplicate character-map declaration with same name @@ -243,14 +247,14 @@ const ( errCodeXTTE1535 = "XTTE1535" // type annotation not valid for text/comment/PI/attribute nodes errCodeXTTE1540 = "XTTE1540" // validation of result document fails (xsl:type on element/attribute) errCodeXTTE1550 = "XTTE1550" // document node is not a valid document (multiple roots or text nodes) - errCodeXTTE1555 = "XTTE1555" // attribute validation fails (no matching global attribute declaration) + errCodeXTTE1555 = "XTTE1555" // document-level validation constraints fail errCodeXTTE2230 = "XTTE2230" // merge keys are not comparable errCodeXTTE3090 = "XTTE3090" // required template context item is absent errCodeXTTE3100 = "XTTE3100" // typed mode: untyped node in typed mode errCodeXTTE3110 = "XTTE3110" // typed="no" mode: typed node in untyped mode errCodeXTTE3165 = "XTTE3165" // xsl:evaluate with-params map key is not xs:QName errCodeXTTE3170 = "XTTE3170" // xsl:evaluate namespace-context not a single node - errCodeXTTE3180 = "XTTE3180" // type error in streaming + errCodeXTTE3180 = "XTTE3180" // xsl:copy selects multiple items errCodeXTTE3210 = "XTTE3210" // xsl:evaluate context-item is not a single item errCodeXTTE3360 = "XTTE3360" // accumulator-before/after with non-node context ) diff --git a/transform/internal/xslt/execute.go b/transform/internal/xslt/execute.go index b37a893..30be9f3 100644 --- a/transform/internal/xslt/execute.go +++ b/transform/internal/xslt/execute.go @@ -18,10 +18,23 @@ import ( "github.com/skarm/xmlsec/transform/internal/xpath" ) +// Initial binding map capacity within a variable scope; it does not limit the number of variables. +const ( + variableScopeInitialCapacity = 4 +) + // baseEvalSlots is the number of base evaluators baseXPathEvaluator keeps per // mode (outside pattern matching, and during it). const baseEvalSlots = 2 +// Fixed evaluator-cache row identities separate ordinary expressions from +// pattern matching. The row count is independent of the eviction slots per row. +const ( + baseEvalModeNormal = 0 + baseEvalModePattern = 1 + baseEvalModeCount = 2 +) + // scopedEvalSlots is the number of scoped evaluators scopedXPathEvaluator // keeps. const scopedEvalSlots = 4 @@ -229,8 +242,8 @@ type execContext struct { // evicts neither. Each row is refilled round-robin from baseEvalNext. // baseEvalGen is the generation number handed to the last evaluator // built; scopedXPathEvaluator keys its own cache on it. - baseEvals [2][baseEvalSlots]baseEvalEntry - baseEvalNext [2]int + baseEvals [baseEvalModeCount][baseEvalSlots]baseEvalEntry + baseEvalNext [baseEvalModeCount]int baseEvalGen uint64 // scopedEvals caches the base evaluator plus the per-scope overlays @@ -274,12 +287,16 @@ func (ec *execContext) ResolveFunction(_ context.Context, uri, name string, arit // ResolveCompatibilityFunction implements the special XSLT backwards binding // for every unavailable function outside the standard fn namespace (§3.9.1). func (ec *execContext) ResolveCompatibilityFunction(_ context.Context, uri, name string, arity int) (xpath.Function, bool, error) { + const ( + errCodeXTDE1425 = "XTDE1425" + ) + if uri == xpath.NSFn { return nil, false, nil } return &xsltFunc{min: arity, max: arity, fn: func(context.Context, []xdm.Sequence) (xdm.Sequence, error) { - return nil, dynamicError("XTDE1425", "extension function Q{%s}%s#%d is unavailable", uri, name, arity) + return nil, dynamicError(errCodeXTDE1425, "extension function Q{%s}%s#%d is unavailable", uri, name, arity) }}, true, nil } @@ -288,11 +305,13 @@ func (ec *execContext) ResolveCompatibilityFunction(_ context.Context, uri, name // XPath expressions to reference globals that aren't in the snapshot built // by collectAllVars (e.g. during circular-but-unused variable scenarios). func (ec *execContext) ResolveVariable(ctx context.Context, name string) (xdm.Sequence, bool, error) { + const originalVariableLexicalName = "xsl:original" + // Handle $xsl:original — resolve to the original overridden variable's value. // The name may come as either the expanded form "{uri}original" or the // prefixed form "xsl:original" depending on whether namespace resolution // happened in the XPath evaluator. - isXSLOriginal := name == xdm.ClarkName(lexicon.NamespaceXSLT, nameOriginal) || name == "xsl:original" + isXSLOriginal := name == xdm.ClarkName(lexicon.NamespaceXSLT, nameOriginal) || name == originalVariableLexicalName if isXSLOriginal && ec.originalGlobal != (globalBinding{}) { return ec.resolveOriginalVariable(ctx) } @@ -822,7 +841,7 @@ func (ec *execContext) setVar(name string, value xdm.Sequence) { } if ec.localVars.vars == nil { - ec.localVars.vars = make(map[string]xdm.Sequence, 4) + ec.localVars.vars = make(map[string]xdm.Sequence, variableScopeInitialCapacity) } ec.localVars.vars[name] = value @@ -833,17 +852,21 @@ func (ec *execContext) setVar(name string, value xdm.Sequence) { // is set to an empty sequence; the error is raised only when the variable // is actually referenced via ResolveVariable. func (ec *execContext) setVarDeferred(name string, err error) { + const ( + deferredErrorInitialCapacity = 2 + ) + if ec.localVars == nil { ec.localVars = ec.acquireVarScope() } if ec.localVars.vars == nil { - ec.localVars.vars = make(map[string]xdm.Sequence, 4) + ec.localVars.vars = make(map[string]xdm.Sequence, variableScopeInitialCapacity) } ec.localVars.vars[name] = xdm.EmptySequence() if ec.localVars.deferredErrors == nil { - ec.localVars.deferredErrors = make(map[string]error, 2) + ec.localVars.deferredErrors = make(map[string]error, deferredErrorInitialCapacity) } ec.localVars.deferredErrors[name] = err @@ -1073,6 +1096,11 @@ func (ec *execContext) executeSequenceConstructor(ctx context.Context, body []in return nil } + // A local binding is visible to following siblings and their descendants, + // never to instructions following the containing sequence constructor. + ec.pushVarScope() + defer ec.popVarScope() + out := ec.currentOutput() out.seqConstructorGen++ scopeIdx := len(out.conditionalScopes) @@ -1246,6 +1274,8 @@ func (ec *execContext) spliceConditionalSequence(placeholder xdm.MutableNode, se case xdm.AtomicValue: nodes, err = ec.appendConditionalAtomic(nodes, value, prevAtomic) prevAtomic = true + default: + return dynamicError(errCodeXTDE0450, "cannot add a %T to the result tree", item) } if err != nil { @@ -1387,12 +1417,12 @@ func (ec *execContext) baseXPathEvaluator() (xpath.Evaluator, uint64) { pkg: ec.currentPackage, inPattern: ec.inPatternMatch, } - mode := 0 + mode := baseEvalModeNormal var patternNS map[string]string if ec.inPatternMatch { - mode = 1 + mode = baseEvalModePattern patternNS = ec.patternNamespaces key.patternNS = mapIdentity(patternNS) } @@ -1424,7 +1454,7 @@ func (ec *execContext) buildBaseXPathEvaluator(baseURI string) xpath.Evaluator { ImplicitTimezone(ec.dynamicProperties.Timezone). AllowXML11Chars(). TraceWriter(ec.traceWriter) - if ec.stylesheet.processorLanguage() >= 30 { + if ec.stylesheet.processorLanguage() >= processorXSLT30 { eval = eval.ResourceReads(ec) } @@ -1650,8 +1680,12 @@ func (ec *execContext) effectiveAccumulators() map[string]*accumulatorDef { // storage. When executing in a used package, the key includes the package // pointer to isolate same-named accumulators from different packages. func (ec *execContext) accumulatorStateKey(name string) string { + const ( + accumulatorIdentityFormat = "%s@%p" + ) + if ec.currentPackage != nil && ec.currentPackage != ec.stylesheet { - return fmt.Sprintf("%s@%p", name, ec.currentPackage) + return fmt.Sprintf(accumulatorIdentityFormat, name, ec.currentPackage) } return name @@ -2232,8 +2266,12 @@ func (ec *execContext) effectivePreserveSpace() []nameTest { // executing in a used package with different strip-space rules, the key // includes the package pointer so each package gets its own stripped copy. func (ec *execContext) docCacheKey(uri string) string { + const ( + documentIdentityFormat = "%s@%p" + ) + if ec.currentPackage != nil && ec.currentPackage != ec.stylesheet { - return fmt.Sprintf("%s@%p", uri, ec.currentPackage) + return fmt.Sprintf(documentIdentityFormat, uri, ec.currentPackage) } return uri @@ -2241,7 +2279,7 @@ func (ec *execContext) docCacheKey(uri string) string { func preserveAttributeIDType(attr *xdm.Attribute, annotation string) { switch annotation { - case "xs:ID": + case xdm.TypeID: attr.SetIDType(xdm.IDAttribute) if parent, ok := attr.Parent().(*xdm.Element); ok { @@ -2249,19 +2287,19 @@ func preserveAttributeIDType(attr *xdm.Attribute, annotation string) { doc.RegisterID(strings.TrimSpace(attr.Value()), parent) } } - case "xs:IDREF": + case xdm.TypeIDREF: attr.SetIDType(xdm.IDRefAttribute) - case "xs:IDREFS": + case xdm.TypeIDREFS: attr.SetIDType(xdm.IDRefsAttribute) } } func isIDRefType(ann string) bool { - return ann == "xs:IDREF" + return ann == xdm.TypeIDREF } func isIDRefsType(ann string) bool { - return ann == "xs:IDREFS" + return ann == xdm.TypeIDREFS } func conditionalActionForItem(actions map[xdm.Node]conditionalAction, item xdm.Item) (conditionalAction, bool) { @@ -2327,7 +2365,7 @@ func collectPackageNamespaces(ss *Stylesheet, ns map[string]string) { } func isIDType(ann string) bool { - return ann == "xs:ID" + return ann == xdm.TypeID } // isAdaptiveStandaloneMarkupNode reports whether node can remain in a @@ -2456,6 +2494,10 @@ func (r executionCollectionResolver) ResolveURICollection(ctx context.Context, u } func (r executionCollectionResolver) prepareNode(ctx context.Context, node xdm.Node) (xdm.Node, error) { + const ( + errCodeFODC0003 = "FODC0003" + ) + if err := xpath.Charge(ctx, 1); err != nil { return nil, err } @@ -2470,7 +2512,7 @@ func (r executionCollectionResolver) prepareNode(ctx context.Context, node xdm.N if doc, ok := xdm.DocumentRoot(node).(*xdm.Document); ok && !doc.HasProperty(xdm.DocInternal) && doc.URL() != "" { cacheKey := r.ec.docCacheKey(doc.URL()) if previous, found := r.ec.docCache[cacheKey]; found && previous != doc { - return nil, &xdm.Error{Code: "FODC0003", Message: "collection supplied conflicting document identities for " + doc.URL()} + return nil, &xdm.Error{Code: errCodeFODC0003, Message: "collection supplied conflicting document identities for " + doc.URL()} } r.ec.docCache[cacheKey] = doc diff --git a/transform/internal/xslt/execute_apply.go b/transform/internal/xslt/execute_apply.go index 9dcb84a..b60188a 100644 --- a/transform/internal/xslt/execute_apply.go +++ b/transform/internal/xslt/execute_apply.go @@ -132,6 +132,10 @@ func (ec *execContext) execApplyTemplates(ctx context.Context, inst *applyTempla // selectApplyTemplateItems preserves sequence order and array/map identity. func (ec *execContext) selectApplyTemplateItems(ctx context.Context, inst *applyTemplatesInst) (xdm.ItemSlice, error) { + const ( + errCodeXTTE0520 = "XTTE0520" + ) + var items xdm.ItemSlice if inst.Select != nil { @@ -144,9 +148,9 @@ func (ec *execContext) selectApplyTemplateItems(ctx context.Context, inst *apply // match=".[. instance of array(*)]"). Flattening would destroy the // array before template matching can see it. items = xdm.Materialize(result.Sequence()) - if ec.stylesheet.processorLanguage() == 20 { + if ec.stylesheet.processorLanguage() == processorXSLT20 { if _, nodesOnly := xdm.NodesFrom(items); !nodesOnly { - return nil, dynamicError("XTTE0520", "XSLT 2.0 apply-templates selection must contain only nodes") + return nil, dynamicError(errCodeXTTE0520, "XSLT 2.0 apply-templates selection must contain only nodes") } } } else { @@ -272,6 +276,10 @@ func (ec *execContext) applyBuiltinTemplateItem(ctx context.Context, item xdm.It // The implicit ?* selection concatenates members without recursively flattening // nested arrays. Matching sees the whole selection's position and size. func (ec *execContext) applyBuiltinArray(ctx context.Context, array xdm.ArrayItem, mode string, params map[string]xdm.Sequence) error { + const ( + builtinArrayNodeFallback = 1_000_000 + ) + var items xdm.ItemSlice for _, member := range array.MembersView() { @@ -280,7 +288,7 @@ func (ec *execContext) applyBuiltinArray(ctx context.Context, array xdm.ArrayIte return err } - if len(items) >= xpath.NodeLimit(ctx, 1_000_000) { + if len(items) >= xpath.NodeLimit(ctx, builtinArrayNodeFallback) { return xpath.ErrNodeSetLimit } @@ -625,13 +633,20 @@ func (ec *execContext) execNextAtomicMatch(ctx context.Context, mode string, pv templates := ec.activeModePackage().modeTemplates[mode] foundCurrent := false - for _, tmpl := range templates { + failOnMultiple := ec.onMultipleMatchFail(mode) + for i, tmpl := range templates { if tmpl == ec.currentTemplate { foundCurrent = true continue } if foundCurrent && tmpl.Match != nil && ec.matchAtomicTemplate(ctx, tmpl, ec.contextItem) { + if failOnMultiple { + if err := ec.checkNextAtomicMatchConflicts(ctx, templates[i+1:], tmpl, mode); err != nil { + return err + } + } + return ec.executeAtomicTemplate(ctx, tmpl, ec.contextItem, mode, pv) } } @@ -639,6 +654,25 @@ func (ec *execContext) execNextAtomicMatch(ctx context.Context, mode string, pv return ec.applyBuiltinTemplateItem(ctx, ec.contextItem, mode, pv) } +func (ec *execContext) checkNextAtomicMatchConflicts(ctx context.Context, candidates []*template, best *template, mode string) error { + for _, other := range candidates { + if other.ImportPrec < best.ImportPrec || (other.ImportPrec == best.ImportPrec && other.Priority < best.Priority) { + break + } + + if other == best || (other.splitOriginID != nil && other.splitOriginID == best.splitOriginID) { + continue + } + + if other.Match != nil && ec.matchAtomicTemplate(ctx, other, ec.contextItem) { + return dynamicError(errCodeXTDE0540, + "ambiguous rule match for atomic item in mode %q (on-multiple-match=fail)", mode) + } + } + + return nil +} + func (ec *execContext) execApplyImports(ctx context.Context, inst *applyImportsInst) error { // xsl:apply-imports: find a matching template with lower import precedence // than the currently executing template. diff --git a/transform/internal/xslt/execute_builtin.go b/transform/internal/xslt/execute_builtin.go index 2bea54e..724f720 100644 --- a/transform/internal/xslt/execute_builtin.go +++ b/transform/internal/xslt/execute_builtin.go @@ -270,7 +270,7 @@ func (ec *execContext) xmlSpacePreserve(elem *xdm.Element) bool { break } - if v, ok := e.GetAttribute("xml:space"); ok { + if v, ok := e.GetAttribute(lexicon.QNameXMLSpace); ok { preserve = v == lexicon.SpacePreserve chain = append(chain, e) @@ -363,8 +363,12 @@ func (ec *execContext) stripWhitespaceFromNode(root xdm.Node) { // per node, and keeps this pre-pass consistent with shouldStripWhitespace // (both ultimately consult stripVerdict) without calling it directly. func (ec *execContext) stripWhitespaceFromNodeInto(root xdm.Node, removed map[xdm.Node]struct{}) { + const ( + stripTraversalInitialCapacity = 32 + ) + // Use an explicit stack to avoid deep recursion on large documents. - stack := make([]stripStackFrame, 0, 32) + stack := make([]stripStackFrame, 0, stripTraversalInitialCapacity) stack = append(stack, stripStackFrame{node: root, preserve: inheritedXMLSpace(root) == lexicon.SpacePreserve}) for len(stack) > 0 { @@ -483,7 +487,11 @@ func rankSpaceRule(nt nameTest) int { } func packSpaceRank(importPrec, priority int) int { - return importPrec<<2 | priority + const ( + spaceRulePriorityBits = 2 + ) + + return importPrec< prefix:* or *:NCName > * func nameTestPriority(nt nameTest) int { + const ( + spaceRuleAnyName = 0 + spaceRuleSpecificName = 2 + spaceRuleWildcardComponent = 1 + ) + if nt.Local == "*" && nt.Prefix == "" && !nt.HasURI { - return 0 // "*" (matches any element in any namespace) + return spaceRuleAnyName // "*" (matches any element in any namespace) } if nt.Local == "*" || nt.Prefix == "*" { // "prefix:*" / "Q{uri}*" (namespace wildcard) or "*:NCName" - return 1 + return spaceRuleWildcardComponent } - return 2 // specific name + return spaceRuleSpecificName // specific name } // whitespaceStripParent returns the parent element to evaluate strip/preserve @@ -556,7 +570,7 @@ func inheritedXMLSpace(node xdm.Node) string { continue } - if v, ok := e.GetAttribute("xml:space"); ok { + if v, ok := e.GetAttribute(lexicon.QNameXMLSpace); ok { return v } } @@ -573,7 +587,7 @@ func childXMLSpacePreserve(node xdm.Node, inherited bool) bool { return inherited } - if v, ok := elem.GetAttribute("xml:space"); ok { + if v, ok := elem.GetAttribute(lexicon.QNameXMLSpace); ok { return v == lexicon.SpacePreserve } diff --git a/transform/internal/xslt/execute_control.go b/transform/internal/xslt/execute_control.go index 6ef37cc..78a63ed 100644 --- a/transform/internal/xslt/execute_control.go +++ b/transform/internal/xslt/execute_control.go @@ -10,6 +10,7 @@ import ( "strconv" "strings" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/transform/internal/xdm" "github.com/skarm/xmlsec/transform/internal/xpath" ) @@ -214,23 +215,24 @@ func (ec *execContext) sortForEachItems(ctx context.Context, seq xdm.Sequence, k } func (ec *execContext) executeScopedSequenceConstructor(ctx context.Context, body []instruction) error { - ec.pushVarScope() - defer ec.popVarScope() - return ec.executeSequenceConstructor(ctx, body) } func (ec *execContext) selectGroupPopulation(ctx context.Context, inst *forEachGroupInst) (xdm.Sequence, error) { + const ( + errCodeXTTE1120 = "XTTE1120" + ) + result, err := ec.evalXPath(ctx, inst.Select, ec.contextNode) if err != nil { return nil, err } seq := result.Sequence() - if ec.stylesheet.processorLanguage() == 20 && (inst.GroupStartingWith != nil || inst.GroupEndingWith != nil) { + if ec.stylesheet.processorLanguage() == processorXSLT20 && (inst.GroupStartingWith != nil || inst.GroupEndingWith != nil) { for i := range xdm.SequenceLen(seq) { if _, ok := seq.Get(i).(xdm.NodeItem); !ok { - return nil, dynamicError("XTTE1120", "XSLT 2.0 pattern grouping requires a population of nodes") + return nil, dynamicError(errCodeXTTE1120, "XSLT 2.0 pattern grouping requires a population of nodes") } } } @@ -414,7 +416,7 @@ func (ec *execContext) withSortGroupContext(groups []fegGroup, hasKey bool, fn f // groupBy compares atomized keys using deep-equal's atomic comparison rules. // Collations affect string-like values only, not numeric, QName or temporal keys. func (ec *execContext) groupBy(ctx context.Context, seq xdm.Sequence, groupByExpr *xpath.Expression, composite bool, collationKeyFn func(string) string) ([]fegGroup, error) { - groups := groupByAccumulator{byKey: make(map[string][]*groupByEntry), collationKey: collationKeyFn, limit: xpath.NodeLimit(ctx, xdm.DefaultMaxNodeSetLength)} + groups := groupByAccumulator{byKey: make(map[string][]*groupByEntry), collationKey: collationKeyFn, limit: xpath.NodeLimit(ctx, xdm.DefaultMaxNodeSetLength), int64Prefix: !composite} groups.equal = func(a, b xdm.Sequence) (bool, error) { return ec.groupingKeysEqual(ctx, a, b, collationKeyFn) } @@ -670,9 +672,9 @@ func (ec *execContext) evaluateVariableValue(ctx context.Context, inst *variable return ec.evaluateVariableBody(ctx, inst) } - // An empty constructor yields an empty sequence only when the declared - // type permits it; otherwise its default value is the empty string. - if inst.As != "" && (strings.HasSuffix(inst.As, "?") || strings.HasSuffix(inst.As, "*")) { + // An explicit type retains the empty constructor; the type check decides + // whether that empty sequence is permitted (XSLT 3.0 section 9.3). + if inst.As != "" { return nil, nil } @@ -688,23 +690,7 @@ func (ec *execContext) evaluateVariableBody(ctx context.Context, inst *variableI return ec.evaluateBodyAsDocument(ctx, inst.Body) } - if !strings.HasPrefix(inst.As, "document-node") || strings.HasSuffix(inst.As, "*") || strings.HasSuffix(inst.As, "+") { - // Repeated document types retain individual documents; other declared - // types use a sequence constructor with separate node items. - return ec.evaluateBodyAsSequence(ctx, inst.Body) - } - - val, err := ec.evaluateBodyAsDocument(ctx, inst.Body) - if err != nil { - return nil, err - } - - // A filtered-away optional document is absent, rather than an empty tree. - if strings.HasSuffix(inst.As, "?") && isEmptyVariableDocument(val) { - return nil, nil - } - - return val, nil + return ec.evaluateBodyAsSequence(ctx, inst.Body) } func (ec *execContext) execParam(ctx context.Context, inst *paramInst) error { @@ -752,21 +738,6 @@ func appendAdjacentGroup(groups []fegGroup, keySeq xdm.Sequence, items xdm.ItemS return append(groups, fegGroup{key: keySeq, items: items}) } -func isEmptyVariableDocument(val xdm.Sequence) bool { - if xdm.SequenceLen(val) != 1 { - return false - } - - item, ok := val.Get(0).(xdm.NodeItem) - if !ok { - return false - } - - doc, ok := item.Node.(*xdm.Document) - - return ok && doc.FirstChild() == nil -} - type fegGroup struct { key xdm.Sequence items xdm.ItemSlice @@ -788,6 +759,8 @@ type groupByAccumulator struct { limit int members int retainedKeys int + int64Prefix bool + byInt64 map[int64]*groupByEntry } func (groups *groupByAccumulator) addComposite(ctx context.Context, item xdm.Item, keys xdm.Sequence) error { @@ -800,6 +773,10 @@ func (groups *groupByAccumulator) addComposite(ctx context.Context, item xdm.Ite } func (groups *groupByAccumulator) addKeys(ctx context.Context, item xdm.Item, keys xdm.Sequence) error { + if groups.int64Prefix && xdm.SequenceLen(keys) != 1 { + groups.disableInt64Prefix() + } + added := make(map[int]struct{}) for key := range xdm.SequenceItems(keys) { @@ -843,14 +820,38 @@ func (groups *groupByAccumulator) entryForKeys(ctx context.Context, keys xdm.Seq return nil, err } - for _, entry := range groups.byKey[bucket] { - equal, err := groups.equal(keys, entry.keySeq) - if err != nil { - return nil, err + integerKey, useInt64, err := groups.int64Key(ctx, keys) + if err != nil { + return nil, err + } + + if useInt64 { + if entry := groups.byInt64[integerKey]; entry != nil { + equal, err := groups.equal(keys, entry.keySeq) + if err != nil { + return nil, err + } + + if equal { + return entry, nil + } + + groups.disableInt64Prefix() + + useInt64 = false } + } + + if !useInt64 { + for _, entry := range groups.byKey[bucket] { + equal, err := groups.equal(keys, entry.keySeq) + if err != nil { + return nil, err + } - if equal { - return entry, nil + if equal { + return entry, nil + } } } @@ -862,11 +863,47 @@ func (groups *groupByAccumulator) entryForKeys(ctx context.Context, keys xdm.Seq entry := &groupByEntry{keySeq: keys, orderIdx: len(groups.order)} groups.retainedKeys += length groups.byKey[bucket] = append(groups.byKey[bucket], entry) + groups.order = append(groups.order, entry) + if useInt64 { + if groups.byInt64 == nil { + groups.byInt64 = make(map[int64]*groupByEntry) + } + + groups.byInt64[integerKey] = entry + } + return entry, nil } +func (groups *groupByAccumulator) disableInt64Prefix() { + groups.int64Prefix = false + groups.byInt64 = nil +} + +func (groups *groupByAccumulator) int64Key(ctx context.Context, keys xdm.Sequence) (int64, bool, error) { + if !groups.int64Prefix { + return 0, false, nil + } + + if xdm.SequenceLen(keys) == 1 { + if value, ok := keys.Get(0).(xdm.AtomicValue); ok { + if key, eligible := indexInt64(value); eligible { + if err := xpath.Charge(ctx, 1); err != nil { + return 0, false, err + } + + return key, true, nil + } + } + } + + groups.disableInt64Prefix() + + return 0, false, nil +} + func (groups *groupByAccumulator) result() []fegGroup { result := make([]fegGroup, len(groups.order)) for i, entry := range groups.order { @@ -877,12 +914,17 @@ func (groups *groupByAccumulator) result() []fegGroup { } func groupingBucket(ctx context.Context, keys xdm.Sequence, collationKey func(string) string) (string, error) { + const ( + groupingNumericBucket = "N" + groupingStringPrefix = "S:" + ) + var bucket strings.Builder for item := range xdm.SequenceItems(keys) { value, ok := item.(xdm.AtomicValue) if !ok { - return "", dynamicError("XPTY0004", "grouping key must be atomic") + return "", dynamicError(lexicon.ErrXPTY0004, "grouping key must be atomic") } value = xdm.PromoteSchemaType(value) @@ -897,9 +939,9 @@ func groupingBucket(ctx context.Context, keys xdm.Sequence, collationKey func(st text = collationKey(text) } - part = "S:" + text + part = groupingStringPrefix + text } else if value.IsNumeric() { - part = "N" + part = groupingNumericBucket } else { switch value.TypeName { case xdm.TypeDateTimeStamp: diff --git a/transform/internal/xslt/execute_copy.go b/transform/internal/xslt/execute_copy.go index 1e7455b..47623c3 100644 --- a/transform/internal/xslt/execute_copy.go +++ b/transform/internal/xslt/execute_copy.go @@ -71,10 +71,9 @@ func (ec *execContext) execCopy(ctx context.Context, inst *copyInst) error { } if contextNode == nil { - // An atomic context item is copied as text without evaluating the body. - if av, ok := ec.contextItem.(xdm.AtomicValue); ok { - out := ec.currentOutput() - return ec.emitAtomicText(av, &out.prevWasAtomic) + // Non-node items are returned unchanged without evaluating the body. + if ec.contextItem != nil { + return ec.outputSequence(xdm.ItemSlice{ec.contextItem}) } return dynamicError(errCodeXTTE0945, "xsl:copy: no context item") @@ -115,9 +114,8 @@ func (ec *execContext) execCopySelected(ctx context.Context, inst *copyInst, con switch value := item.(type) { case xdm.NodeItem: return ec.execCopySelectedNode(ctx, inst, value.Node, opts) - case xdm.AtomicValue: - out := ec.currentOutput() - return ec.emitAtomicText(value, &out.prevWasAtomic) + default: + return ec.outputSequence(xdm.ItemSlice{item}) } } @@ -452,7 +450,7 @@ func (ec *execContext) execCopyOf(ctx context.Context, inst *copyOfInst) error { seq := result.Sequence() - capture := out.captureItems && ec.stylesheet.processorLanguage() >= 20 + capture := out.captureItems && ec.stylesheet.processorLanguage() >= processorXSLT20 if !capture { seq = flattenArraysInSequence(seq) } @@ -760,7 +758,7 @@ func (ec *execContext) copyNodeToOutput(node xdm.Node, copyNamespaces ...bool) e func (ec *execContext) copyDocumentToOutput(node xdm.Node, copyNS bool) error { out := ec.currentOutput() if out.sequenceMode || out.captureItems { - return ec.captureCopiedDocument(node) + return ec.captureCopiedDocument(node, copyNS) } // A document copy contributes its children and preserves DTD metadata @@ -781,12 +779,20 @@ func (ec *execContext) copyDocumentToOutput(node xdm.Node, copyNS bool) error { } // copyDocumentNode preserves one document item and its separate output metadata. -func (ec *execContext) copyDocumentNode(source xdm.Node) (*xdm.Document, error) { +func (ec *execContext) copyDocumentNode(source xdm.Node, copyNamespaces ...bool) (*xdm.Document, error) { if err := xpath.ChargeTree(ec.budgetCtx, source); err != nil { return nil, err } doc := newCopyDocument(source) + if len(copyNamespaces) > 0 && !copyNamespaces[0] { + if err := ec.copyDocumentChildrenNoNamespaces(source, doc); err != nil { + return nil, err + } + + return doc, nil + } + for child := range xdm.Children(source) { if !xdm.IsXDMChild(child) { continue @@ -805,8 +811,30 @@ func (ec *execContext) copyDocumentNode(source xdm.Node) (*xdm.Document, error) return doc, nil } -func (ec *execContext) captureCopiedDocument(source xdm.Node) error { - doc, err := ec.copyDocumentNode(source) +// Reuse the element copy constructor so only required namespace bindings survive. +func (ec *execContext) copyDocumentChildrenNoNamespaces(source xdm.Node, doc *xdm.Document) error { + savedResult := ec.resultDoc + ec.resultDoc = doc + + ec.outputStack = append(ec.outputStack, &outputFrame{doc: doc, current: doc}) + defer func() { + ec.resultDoc = savedResult + ec.outputStack = ec.outputStack[:len(ec.outputStack)-1] + }() + + for child := range xdm.Children(source) { + if xdm.IsXDMChild(child) { + if err := ec.copyNodeToOutput(child, false); err != nil { + return err + } + } + } + + return nil +} + +func (ec *execContext) captureCopiedDocument(source xdm.Node, copyNS bool) error { + doc, err := ec.copyDocumentNode(source, copyNS) if err != nil { return err } @@ -1121,7 +1149,7 @@ func propagateAncestorNamespaces(src, dst *xdm.Element) { for _, ns := range pElem.Namespaces() { prefix := ns.Prefix() - if prefix == lexicon.MethodXML { + if prefix == lexicon.PrefixXML { continue } @@ -1172,7 +1200,7 @@ func undeclareParentCopyNS(parent *xdm.Element) { for _, ns := range parentNS { prefix := ns.Prefix() - if prefix == lexicon.MethodXML { + if prefix == lexicon.PrefixXML { continue } @@ -1194,9 +1222,9 @@ func undeclareParentCopyNS(parent *xdm.Element) { // xs:NOTATION, or common derived forms. func isNamespaceSensitiveType(typeName string) bool { switch typeName { - case "xs:QName", "xs:NOTATION", - "Q{http://www.w3.org/2001/XMLSchema}QName", - "Q{http://www.w3.org/2001/XMLSchema}NOTATION": + case xdm.TypeQName, xdm.TypeNOTATION, + "Q{" + lexicon.NamespaceXSD + "}" + lexicon.TypeQName, + "Q{" + lexicon.NamespaceXSD + "}" + lexicon.TypeNotation: return true } diff --git a/transform/internal/xslt/execute_element.go b/transform/internal/xslt/execute_element.go index eabfbe4..801cdce 100644 --- a/transform/internal/xslt/execute_element.go +++ b/transform/internal/xslt/execute_element.go @@ -254,7 +254,7 @@ func (ec *execContext) validateConstructedElement(_ context.Context, elem *xdm.E // Per XSLT 3.0 §19.2: validation="preserve" assigns type // annotation xs:anyType to the constructed element node, // while preserving existing annotations on child nodes. - ec.annotateNode(elem, "xs:anyType") + ec.annotateNode(elem, xdm.TypeAnyType) return nil case validationStrict, validationLax: return ErrLanguageUnsupported @@ -276,7 +276,7 @@ func (ec *execContext) validateConstructedElementWithIDCheck(_ context.Context, // Per XSLT 3.0 §19.2: validation="preserve" assigns type // annotation xs:anyType to the constructed element node, // while preserving existing annotations on child nodes. - ec.annotateNode(elem, "xs:anyType") + ec.annotateNode(elem, xdm.TypeAnyType) return nil case validationStrict, validationLax: return ErrLanguageUnsupported @@ -322,6 +322,10 @@ func (ec *execContext) annotateAttributesFromType(elem *xdm.Element, typeName st // (sequence-mode capture, item capture, and element output) so a name such as // name="p:a" namespace="urn:p" yields an attribute in urn:p in all modes. func (ec *execContext) resolveComputedAttributeName(ctx context.Context, name string, inst *attributeInst) (resolvedAttrName, error) { + const ( + provisionalAttributePrefix = "ns0" + ) + prefix := "" localName := name @@ -349,7 +353,7 @@ func (ec *execContext) resolveComputedAttributeName(ctx context.Context, name st // Attributes in a namespace require a non-empty prefix (unlike // elements, the default namespace does not apply to attributes). if prefix == "" { - prefix = "ns0" + prefix = provisionalAttributePrefix } return resolvedAttrName{localName: localName, nsURI: nsURI, prefix: prefix}, nil @@ -360,7 +364,7 @@ func (ec *execContext) resolveComputedAttributeName(ctx context.Context, name st return resolvedAttrName{localName: localName}, nil } - nsURI := ec.resolvePrefix(prefix) + nsURI := inst.Namespaces[prefix] if nsURI == "" { // XTDE0860: prefix in computed attribute name is undeclared. return resolvedAttrName{}, dynamicError(errCodeXTDE0860, @@ -537,7 +541,7 @@ func (ec *execContext) outputNonElementAttribute(out *outputFrame, lexicalName s // the existing parent and namespace behavior of this output mode. tmpDoc := xdm.NewDefaultDocument() - tmpElem, err := tmpDoc.CreateElement("_tmp") + tmpElem, err := tmpDoc.CreateElement(temporaryOutputWrapper) if err != nil { return err } @@ -591,7 +595,7 @@ func (ec *execContext) attachComputedAttribute(elem *xdm.Element, name resolvedA // (XTDE1480) when the stylesheet declares an XSLT version below 3.0. XSLT 3.0 // relaxes this restriction, so the depth is left untouched there. func (ec *execContext) withV2TempOutput(fn func() error) error { - isV2TempOutput := ec.stylesheet.processorLanguage() < 30 + isV2TempOutput := ec.stylesheet.processorLanguage() < processorXSLT30 if isV2TempOutput { ec.temporaryOutputDepth++ } @@ -666,7 +670,7 @@ func (ec *execContext) execPI(ctx context.Context, inst *piInst) error { "xsl:processing-instruction name %q is not a valid NCName", name) } - if strings.EqualFold(name, lexicon.MethodXML) { + if strings.EqualFold(name, lexicon.ReservedXMLPITarget) { return dynamicError(errCodeXTDE0890, "xsl:processing-instruction name must not be %q", name) } @@ -1060,7 +1064,7 @@ func validateComputedNamespace(ctx context.Context, name, value string) error { } func validComputedNamespaceURI(ctx context.Context, value string) bool { - if xpath.XMLSchemaVersion(ctx) != 10 { + if xpath.XMLSchemaVersion(ctx) != xmlSchemaProfile10 { return true } @@ -1085,9 +1089,14 @@ func checkComputedNamespaceConflict(elem *xdm.Element, name, value string, fixup return nil } +// namespaceCollisionSeedSuffix starts namespace-fixup searches for computed +// and literal elements and copied attributes. The search still checks every +// generated prefix against the element's in-scope bindings. +const namespaceCollisionSeedSuffix = "_0" + func renameElementNamespacePrefix(elem *xdm.Element, name string) error { uri := elem.URI() - prefix := uniqueNSPrefix(elem, name+"_0", uri) + prefix := uniqueNSPrefix(elem, name+namespaceCollisionSeedSuffix, uri) elem.RemoveNamespaceByPrefix(name) if err := elem.DeclareNamespace(prefix, uri); err != nil { @@ -1097,10 +1106,6 @@ func renameElementNamespacePrefix(elem *xdm.Element, name string) error { return elem.SetActiveNamespace(prefix, uri) } -// validateConstructedAttribute validates an attribute value against the schema -// when validation="strict" or validation="lax". Returns XTTE1510 when the -// value is invalid, XTTE1555 when no matching global declaration is found -// (strict only). // firstNonEmptyType returns explicit if non-empty, else fallback. An explicit // type= on xsl:attribute takes precedence over a validation-resolved type. func firstNonEmptyType(explicit, fallback string) string { @@ -1162,7 +1167,7 @@ func copyAttributeToElement(elem *xdm.Element, attr *xdm.Attribute) { // generate a unique prefix to avoid conflict. if prefix != "" { if conflictingAttrPrefix(elem, prefix, uri) { - prefix = uniqueNSPrefix(elem, prefix+"_0", uri) + prefix = uniqueNSPrefix(elem, prefix+namespaceCollisionSeedSuffix, uri) } } diff --git a/transform/internal/xslt/execute_globals.go b/transform/internal/xslt/execute_globals.go index 63d25b7..694d4cf 100644 --- a/transform/internal/xslt/execute_globals.go +++ b/transform/internal/xslt/execute_globals.go @@ -10,11 +10,17 @@ import ( "slices" "strings" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/transform/internal/xdm" uripath "github.com/skarm/xmlsec/transform/internal/xdm/uri" "github.com/skarm/xmlsec/transform/internal/xpath" ) +// Private construction wrapper or provisional prefix; existing ownership and namespace fixup decide which names can reach a result. +const ( + temporaryOutputWrapper = "_tmp" +) + // Params with caller-provided values are set immediately; all others are // evaluated on first access to support arbitrary declaration order. func (ec *execContext) initGlobalVars(ctx context.Context, cfg *transformConfig) error { @@ -80,8 +86,8 @@ func (ec *execContext) initGlobalVars(ctx context.Context, cfg *transformConfig) func (ec *execContext) missingGlobalParam(p *param) error { code := errCodeXTDE0050 - if ec.stylesheet.processorLanguage() == 20 && !p.Required { - code = "XTDE0610" + if ec.stylesheet.processorLanguage() == processorXSLT20 && !p.Required { + code = errCodeXTDE0610 } return dynamicError(code, "required stylesheet parameter $%s was not supplied", p.Name) @@ -293,6 +299,10 @@ func (ec *execContext) evaluateGlobalParam(ctx context.Context, p *param) (xdm.S } func (ec *execContext) evaluateGlobalBinding(ctx context.Context, binding globalBinding) (xdm.Sequence, error) { + const ( + globalEvaluationDepthFallback = 128 + ) + if value, ok := ec.globalValueCache[binding]; ok { ec.publishGlobalValue(binding, value) return value, nil @@ -306,7 +316,7 @@ func (ec *execContext) evaluateGlobalBinding(ctx context.Context, binding global return nil, err } - if len(ec.globalEvaluating) >= xpath.DepthLimit(ctx, 128) { + if len(ec.globalEvaluating) >= xpath.DepthLimit(ctx, globalEvaluationDepthFallback) { return nil, xpath.ErrBudget } @@ -483,10 +493,7 @@ func (ec *execContext) computeGlobalVar(ctx context.Context, v *variable) (xdm.S ec.temporaryOutputDepth++ var err error - if strings.HasPrefix(v.As, "document-node") && !strings.HasSuffix(v.As, "*") && !strings.HasSuffix(v.As, "+") { - // document-node() or document-node()?: wrap body in document node - val, err = ec.evaluateBodyAsDocument(ctx, v.Body) - } else if v.As != "" { + if v.As != "" { // With as attribute: evaluate as sequence constructor, // keeping each node as a separate item val, err = ec.evaluateBodyAsSequence(ctx, v.Body) @@ -503,9 +510,8 @@ func (ec *execContext) computeGlobalVar(ctx context.Context, v *variable) (xdm.S } } else { // No select, no body (or empty body after whitespace stripping). - // XSLT 3.0 §9.3: if as specifies a sequence type whose occurrence - // indicator is ? or *, the effective value is an empty sequence. - if v.As != "" && (strings.HasSuffix(v.As, "?") || strings.HasSuffix(v.As, "*")) { + // An explicit as retains the empty sequence constructor. + if v.As != "" { val = nil } else { val = xdm.SingleString("") @@ -621,7 +627,7 @@ func (ec *execContext) computeGlobalParam(ctx context.Context, p *param) (xdm.Se func (ec *execContext) evaluateBodyForAttr(ctx context.Context, body []instruction) (xdm.Sequence, error) { tmpDoc := xdm.NewDefaultDocument() - tmpRoot, err := tmpDoc.CreateElement("_tmp") + tmpRoot, err := tmpDoc.CreateElement(temporaryOutputWrapper) if err != nil { return nil, err } @@ -656,54 +662,6 @@ func (ec *execContext) evaluateBodyForAttr(ctx context.Context, body []instructi return xdm.EmptySequence(), nil } -// evaluateBodySeparateText keeps each produced -// text node as a separate string item instead of letting the DOM merge -// adjacent text nodes. This is needed by xsl:value-of with separator -// so that each text value is a distinct item for separator insertion. -func (ec *execContext) evaluateBodySeparateText(ctx context.Context, body []instruction) (xdm.Sequence, error) { - tmpDoc := xdm.NewDefaultDocument() - - tmpRoot, err := tmpDoc.CreateElement("_tmp") - if err != nil { - return nil, err - } - - if err := tmpDoc.AddChild(tmpRoot); err != nil { - return nil, err - } - - frame := &outputFrame{doc: tmpDoc, current: tmpRoot, captureItems: true, separateTextNodes: true} - - ec.outputStack = append(ec.outputStack, frame) - defer func() { - ec.outputStack = ec.outputStack[:len(ec.outputStack)-1] - }() - - if err := ec.executeSequenceConstructor(ctx, body); err != nil { - return nil, err - } - - if tmpRoot.FirstChild() != nil { - var seq xdm.ItemSlice - for child := range xdm.Children(tmpRoot) { - seq = append(seq, xdm.NodeItem{Node: child}) - } - - seq = append(seq, frame.pendingItems...) - - return seq, nil - } - - if len(frame.pendingItems) > 0 { - return frame.pendingItems, nil - } - - return xdm.EmptySequence(), nil -} - -// evaluateBodyAsDocument executes instructions and wraps the result in a -// document node (temporary tree), as required by the XSLT spec for variables -// and params with content body and no select/as attributes. // effectiveStaticBaseURI returns the static base URI for the current // execution context, considering xml:base overrides, template base URIs, // and the stylesheet base URI. @@ -732,6 +690,9 @@ func (ec *execContext) effectiveStaticBaseURI() string { return ec.stylesheet.baseURI } +// evaluateBodyAsDocument executes instructions and wraps the result in a +// document node (temporary tree), as required by the XSLT spec for variables +// and params with content body and no select/as attributes. func (ec *execContext) evaluateBodyAsDocument(ctx context.Context, body []instruction) (xdm.Sequence, error) { tmpDoc := xdm.NewDefaultDocument() // Mark as internal so document-uri() returns empty for temporary trees. @@ -774,7 +735,9 @@ func (ec *execContext) evaluateBodyAsDocument(ctx context.Context, body []instru // entities, notations) that would be lost if we atomized it. if first := tmpDoc.FirstChild(); first != nil && first.NextSibling() == nil { if items, ok := frame.seqPlaceholders[first]; ok { - if seq, ok := capturedInternalDocument(items); ok { + if seq, ok, err := capturedInternalDocument(ctx, tmpDoc, items); err != nil { + return nil, err + } else if ok { return seq, nil } } @@ -795,7 +758,9 @@ func (ec *execContext) evaluateBodyAsDocument(ctx context.Context, body []instru // DTD information (unparsed entities, notations) that would be lost // if we atomized the document into text. if len(frame.pendingItems) == 1 && tmpDoc.FirstChild() == nil { - if seq, ok := capturedInternalDocument(frame.pendingItems); ok { + if seq, ok, err := capturedInternalDocument(ctx, tmpDoc, frame.pendingItems); err != nil { + return nil, err + } else if ok { return seq, nil } } @@ -809,7 +774,7 @@ func (ec *execContext) evaluateBodyAsDocument(ctx context.Context, body []instru // in the tree whose string-value is zero-length is deleted." removeEmptyDocumentText(tmpDoc) - return xdm.ItemSlice{xdm.NodeItem{Node: tmpDoc, ResultTreeFragment: ec.stylesheet.processorLanguage() == 10}}, nil + return xdm.ItemSlice{xdm.NodeItem{Node: tmpDoc, ResultTreeFragment: ec.stylesheet.processorLanguage() == processorXSLT10}}, nil } // resolveSequencePlaceholders replaces the xsl:sequence placeholder PIs in the @@ -883,7 +848,7 @@ func (ec *execContext) evaluateBodyAsSequence(ctx context.Context, body []instru tmpDoc.SetURL(baseURI) } - tmpRoot, err := tmpDoc.CreateElement("_tmp") + tmpRoot, err := tmpDoc.CreateElement(temporaryOutputWrapper) if err != nil { return nil, err } @@ -944,24 +909,36 @@ func validateUsedPackageGlobalContextItem(ss *Stylesheet, seen map[*Stylesheet]s return nil } -func capturedInternalDocument(items xdm.ItemSlice) (xdm.Sequence, bool) { +// Copy the captured document, retaining its DTD metadata while giving the +// implicit document constructor fresh identity and its own static base URI. +func capturedInternalDocument(ctx context.Context, target *xdm.Document, items xdm.ItemSlice) (xdm.Sequence, bool, error) { if len(items) != 1 { - return nil, false + return nil, false, nil } ni, ok := items[0].(xdm.NodeItem) if !ok { - return nil, false + return nil, false, nil } doc, ok := ni.Node.(*xdm.Document) if !ok { - return nil, false + return nil, false, nil + } + + if err := xpath.ChargeTree(ctx, doc); err != nil { + return nil, false, err + } + + owned, err := xdm.CopyDoc(doc) + if err != nil { + return nil, false, err } - doc.SetProperties(doc.Properties() | xdm.DocInternal) + owned.SetProperties(owned.Properties() | xdm.DocInternal) + owned.SetURL(target.URL()) - return xdm.ItemSlice{xdm.NodeItem{Node: doc}}, true + return xdm.ItemSlice{xdm.NodeItem{Node: owned}}, true, nil } func appendPendingDocumentItems(doc *xdm.Document, items xdm.ItemSlice) error { @@ -1050,7 +1027,7 @@ func ensureFileURI(path string) string { } if strings.HasPrefix(path, "/") { - return "file://" + path + return lexicon.SchemeFile + "://" + path } return path diff --git a/transform/internal/xslt/execute_lre.go b/transform/internal/xslt/execute_lre.go index 14bf6d8..4168542 100644 --- a/transform/internal/xslt/execute_lre.go +++ b/transform/internal/xslt/execute_lre.go @@ -232,7 +232,7 @@ func (ec *execContext) applyOneAttributeSet(ctx context.Context, asDef *attribut if _, ok := active[asDef]; ok { code := errCodeXTSE0720 - if ec.stylesheet.processorLanguage() >= 30 { + if ec.stylesheet.processorLanguage() >= processorXSLT30 { code = errCodeXTDE0640 } @@ -412,7 +412,7 @@ func (ec *execContext) fixupExcludedElementNS(elem *xdm.Element, origPrefix, ori return // already correct } // Prefix is claimed by a different URI — invent a new prefix - newPrefix := uniqueNSPrefix(elem, origPrefix+"_0", origURI) + newPrefix := uniqueNSPrefix(elem, origPrefix+namespaceCollisionSeedSuffix, origURI) _ = elem.DeclareNamespace(newPrefix, origURI) _ = elem.SetActiveNamespace(newPrefix, origURI) @@ -600,7 +600,11 @@ func inheritConstructedNamespaces(ctx context.Context, parent *xdm.Element) erro } func inheritConstructedNamespacesWalk(ctx context.Context, parent *xdm.Element, bindings map[string]string, depth int) error { - if depth > xpath.DepthLimit(ctx, 128) { + const ( + namespaceInheritanceDepthFallback = 128 + ) + + if depth > xpath.DepthLimit(ctx, namespaceInheritanceDepthFallback) { return xpath.ErrBudget } diff --git a/transform/internal/xslt/execute_misc.go b/transform/internal/xslt/execute_misc.go index c04cdef..e453f03 100644 --- a/transform/internal/xslt/execute_misc.go +++ b/transform/internal/xslt/execute_misc.go @@ -15,6 +15,21 @@ import ( "github.com/skarm/xmlsec/transform/internal/xpath" ) +type assertionsKey struct{} + +type dynamicEvaluationDisabledKey struct{} + +// WithDynamicEvaluationDisabled selects a static feature restriction inherited +// by nested compilation. It cannot enable evaluation in a disabled outer scope. +func WithDynamicEvaluationDisabled(ctx context.Context) context.Context { + return context.WithValue(ctx, dynamicEvaluationDisabledKey{}, true) +} + +// WithAssertions selects the invocation's external XSLT assertion policy. +func WithAssertions(ctx context.Context, enabled bool) context.Context { + return context.WithValue(ctx, assertionsKey{}, enabled) +} + type analyzeStringSource struct { text string version2 bool @@ -22,6 +37,12 @@ type analyzeStringSource struct { } func (ec *execContext) execAnalyzeString(ctx context.Context, inst *analyzeStringInst) error { + const unboundedRegexMatches = -1 + + const ( + errCodeFORX0001 = "FORX0001" + ) + input, err := ec.analyzeStringInput(ctx, inst) if err != nil { return err @@ -65,7 +86,7 @@ func (ec *execContext) execAnalyzeString(ctx context.Context, inst *analyzeStrin // Map XPath regex errors to XSLT error codes var detail *xdm.Error - if errors.As(err, &detail) && detail.Code == "FORX0001" { + if errors.As(err, &detail) && detail.Code == errCodeFORX0001 { return dynamicErrorCause(errCodeXTDE1145, err, "xsl:analyze-string invalid flags: %v", err) } @@ -96,7 +117,7 @@ func (ec *execContext) execAnalyzeString(ctx context.Context, inst *analyzeStrin // caps how many they will surface; the default cap is far above any // legitimate match count. The shared pattern engine checks cancellation // and operation/storage limits throughout both streaming passes. - maxMatches := -1 + maxMatches := unboundedRegexMatches if limit := resolveResourceLimit(ec.resourceLimit()); limit >= 0 { maxMatches = max(clampInt64ToInt(limit), 1) } @@ -106,7 +127,7 @@ func (ec *execContext) execAnalyzeString(ctx context.Context, inst *analyzeStrin // to surface that one extra match; passing maxMatches+1 lets the callback see // it. A non-positive maxMatches (unbounded cap) and an int overflow both fall // back to -1 (no enumeration limit). - findLimit := -1 + findLimit := unboundedRegexMatches if maxMatches >= 0 { if n := maxMatches + 1; n > 0 { @@ -136,7 +157,7 @@ func (ec *execContext) analyzeStringInput(ctx context.Context, inst *analyzeStri // The select expression must produce a single xs:string value. // A sequence of more than one item, or a non-string item, is XPTY0004. - isV2 := ec.stylesheet.processorLanguage() < 30 + isV2 := ec.stylesheet.processorLanguage() < processorXSLT30 if seq == nil || xdm.SequenceLen(seq) == 0 { if isV2 { // XSLT 2.0: empty sequence is XPTY0004 @@ -369,20 +390,24 @@ func (ec *execContext) registerConditionalOutput(ctx context.Context, selectExpr // execMap combines a sequence of maps using the shared duplicate-key rules. func (ec *execContext) execMap(ctx context.Context, inst *mapInst) error { - maps, err := ec.evaluateBodyAsSequence(ctx, inst.Body) + const ( + errCodeXTTE3375 = "XTTE3375" + ) + + sequence, err := ec.evaluateBodyAsSequence(ctx, inst.Body) if err != nil { return err } - builder := xdm.NewMapBuilder(xdm.MergeReject, xdm.SequenceLen(maps)) - for item := range xdm.SequenceItems(maps) { + builder := xdm.NewMapBuilder(xdm.MergeReject, xdm.SequenceLen(sequence)) + for item := range xdm.SequenceItems(sequence) { if err := xpath.Charge(ctx, 1); err != nil { return err } m, ok := item.(xdm.MapItem) if !ok { - return dynamicError("XTTE3375", "xsl:map body produced non-map item %T", item) + return dynamicError(errCodeXTTE3375, "xsl:map body produced non-map item %T", item) } if err := m.ForEachView(func(key xdm.AtomicValue, value xdm.Sequence) error { @@ -414,7 +439,7 @@ func (ec *execContext) execMapEntry(ctx context.Context, inst *mapEntryInst) err return err } - key, err := ec.convertSequenceType(ctx, keyResult.Sequence(), xdm.SequenceType{ItemTest: xdm.AtomicOrUnionType{Name: "Q{http://www.w3.org/2001/XMLSchema}anyAtomicType"}}) + key, err := ec.convertSequenceType(ctx, keyResult.Sequence(), xdm.SequenceType{ItemTest: xdm.AtomicOrUnionType{Name: "Q{" + lexicon.NamespaceXSD + "}" + lexicon.TypeAnyAtomicType}}) if err != nil { return err } @@ -497,7 +522,7 @@ func (ec *execContext) execEvaluate(ctx context.Context, inst *evaluateInst) err return err } - dynExpr, compileErr := xpath.NewCompiler().Context(ctx).Language(31, nsBindings).Compile(xpathStr) + dynExpr, compileErr := xpath.NewCompiler().Context(ctx).Language(xpathProfile31, nsBindings).Compile(xpathStr) if compileErr != nil { if failure := preserveControlFailure(ctx, compileErr); failure != nil { return failure @@ -589,7 +614,7 @@ func (ec *execContext) evaluateXPathSource(ctx context.Context, inst *evaluateIn return "", err } - value, err := ec.convertSequenceType(ctx, xpathResult.Sequence(), xdm.SequenceType{ItemTest: xdm.AtomicOrUnionType{Name: "Q{http://www.w3.org/2001/XMLSchema}string"}}) + value, err := ec.convertSequenceType(ctx, xpathResult.Sequence(), xdm.SequenceType{ItemTest: xdm.AtomicOrUnionType{Name: "Q{" + lexicon.NamespaceXSD + "}" + lexicon.TypeString}}) if err != nil { return "", err } @@ -785,7 +810,7 @@ func (ec *execContext) dynamicXPathEvaluator(vars map[string]xdm.Sequence, nsBin eval := xpath.NewEvaluator(xpath.EvalBorrowing). Variables(vars). Functions(evalFns, ec.xsltEvaluateFunctionsNS()) - if ec.stylesheet.processorLanguage() >= 30 { + if ec.stylesheet.processorLanguage() >= processorXSLT30 { eval = eval.ResourceReads(ec) } @@ -916,6 +941,10 @@ func countAnalyzeStringSegments(ctx context.Context, re *xpath.Regex, input stri // xs:untypedAtomic, or a schema-defined simple type derived from xs:string (whose // BaseType is xs:string, so its string-backed value substitutes for xs:string). func analyzeStringSelectTypeOK(av xdm.AtomicValue) bool { + if xdm.IsStringDerived(av.TypeName) { + return true + } + switch av.TypeName { case xdm.TypeString, xdm.TypeUntypedAtomic, xdm.TypeAnyURI: return true @@ -965,11 +994,15 @@ const ( // conditionalItemEmpty keeps the distinct definitions in sections 8.4.1 and // 8.4.2 while sharing bounded recursive array flattening and atomic conversion. func conditionalItemEmpty(ctx context.Context, item xdm.Item, rule conditionalEmptiness, depth int) (bool, error) { + const ( + conditionalArrayDepthFallback = 128 + ) + if err := xpath.Charge(ctx, 1); err != nil { return false, err } - if depth > xpath.DepthLimit(ctx, 128) { + if depth > xpath.DepthLimit(ctx, conditionalArrayDepthFallback) { return false, xpath.ErrBudget } @@ -1021,21 +1054,6 @@ func conditionalArrayEmpty(ctx context.Context, array xdm.ArrayItem, rule condit return true, nil } -type assertionsKey struct{} - -type dynamicEvaluationDisabledKey struct{} - -// WithDynamicEvaluationDisabled selects a static feature restriction inherited -// by nested compilation. It cannot enable evaluation in a disabled outer scope. -func WithDynamicEvaluationDisabled(ctx context.Context) context.Context { - return context.WithValue(ctx, dynamicEvaluationDisabledKey{}, true) -} - -// WithAssertions selects the invocation's external XSLT assertion policy. -func WithAssertions(ctx context.Context, enabled bool) context.Context { - return context.WithValue(ctx, assertionsKey{}, enabled) -} - func dynamicEvaluationDisabled(ctx context.Context) bool { disabled, _ := ctx.Value(dynamicEvaluationDisabledKey{}).(bool) return disabled diff --git a/transform/internal/xslt/execute_number.go b/transform/internal/xslt/execute_number.go index 6a7b8dc..dd7b6b0 100644 --- a/transform/internal/xslt/execute_number.go +++ b/transform/internal/xslt/execute_number.go @@ -19,6 +19,37 @@ import ( "github.com/skarm/xmlsec/transform/internal/xpath" ) +// ASCII decimal padding digit before any separately selected digit-system localization. +const ( + numberZeroPaddingDigit = "0" +) + +// Default decimal number picture used when no format AVT or no alphanumeric picture token is supplied; it is not a boolean lexical alias. +const ( + numberDecimalPicture = "1" +) + +// Exact xsl:number picture selector; the selected formatting algorithm and fallback behavior remain unchanged. +const ( + numberPictureLowerAlpha = "a" + numberPictureLowerRoman = "i" + numberPictureUpperAlpha = "A" + numberPictureUpperRoman = "I" +) + +// Explicit base ten for integer lexical parsing and formatting APIs; ordinary decimal place-value arithmetic and Unicode range lengths are independent. +const ( + numberDecimalRadix = 10 +) + +// Closed xsl:number selectors shared by grammar validation and number formatting; these do not select an XPath atomic type. +const ( + numberAlphabetic = "alphabetic" + numberAny = "any" + numberMultiple = "multiple" + numberTraditional = "traditional" +) + // ordinalSystem describes a Unicode numbering system with potentially // non-contiguous ranges and a special zero character. type ordinalSystem struct { @@ -168,9 +199,9 @@ func (ec *execContext) evaluateNumberValues(ctx context.Context, inst *numberIns func (ec *execContext) numberNodeValues(ctx context.Context, inst *numberInst, node xdm.Node) []int { switch inst.Level { - case "multiple": + case numberMultiple: return ec.numberMultiple(ctx, inst, node) - case "any": + case numberAny: return ec.numberAny(ctx, inst, node) default: return ec.numberSingle(ctx, inst, node) @@ -182,7 +213,7 @@ func (ec *execContext) evaluateNumberFormatOptions(ctx context.Context, inst *nu var err error - options.picture, err = ec.evaluateNumberAVT(ctx, inst.Format, "1") + options.picture, err = ec.evaluateNumberAVT(ctx, inst.Format, numberDecimalPicture) if err != nil { return numberFormatOptions{}, err } @@ -198,7 +229,7 @@ func (ec *execContext) evaluateNumberFormatOptions(ctx context.Context, inst *nu return numberFormatOptions{}, err } - groupSize = strings.Trim(groupSize, " \t\r\n") + groupSize = strings.Trim(groupSize, lexicon.XMLWhitespace) if !validNumberOption(nameGroupingSize, groupSize) { return numberFormatOptions{}, dynamicError(errCodeXTDE0030, "invalid xsl:number grouping-size %q", groupSize) } @@ -217,7 +248,7 @@ func (ec *execContext) evaluateNumberFormatOptions(ctx context.Context, inst *nu return numberFormatOptions{}, err } - options.lang = strings.Trim(options.lang, " \t\r\n") + options.lang = strings.Trim(options.lang, lexicon.XMLWhitespace) if !validNumberOption(lexicon.AttrLang, options.lang) { return numberFormatOptions{}, dynamicError(errCodeXTDE0030, "xsl:number lang attribute is not a valid language code: %q", options.lang) @@ -239,9 +270,9 @@ func (ec *execContext) evaluateNumberFormatOptions(ctx context.Context, inst *nu return numberFormatOptions{}, err } - if ec.stylesheet.processorLanguage() >= 30 { - switch strings.Trim(options.ordinal, " \t\r\n") { - case lexicon.ValueNo, lexicon.ValueFalse, "0": + if ec.stylesheet.processorLanguage() >= processorXSLT30 { + switch strings.Trim(options.ordinal, lexicon.XMLWhitespace) { + case lexicon.ValueNo, lexicon.ValueFalse, booleanDigitFalse: options.ordinal = "" } } @@ -536,7 +567,7 @@ func (ec *execContext) numberFindAncestorOrSelf(ctx context.Context, inst *numbe } func parseStartAt(value string) ([]*big.Int, bool) { - parts := strings.FieldsFunc(strings.Trim(value, " \t\r\n"), func(r rune) bool { return r == ' ' || r == '\t' || r == '\r' || r == '\n' }) + parts := strings.FieldsFunc(strings.Trim(value, lexicon.XMLWhitespace), func(r rune) bool { return r == ' ' || r == '\t' || r == '\r' || r == '\n' }) if len(parts) == 0 { return nil, false } @@ -547,7 +578,7 @@ func parseStartAt(value string) ([]*big.Int, bool) { return nil, false } - numbers[i], _ = new(big.Int).SetString(part, 10) + numbers[i], _ = new(big.Int).SetString(part, numberDecimalRadix) } return numbers, true @@ -576,13 +607,13 @@ func decimalIntegerLexical(value string, allowPlus bool) bool { } func validNumberOption(name, value string) bool { - value = strings.Trim(value, " \t\r\n") + value = strings.Trim(value, lexicon.XMLWhitespace) switch name { case nameGroupingSize: return decimalIntegerLexical(value, true) case nameLetterValue: - return value == "alphabetic" || value == "traditional" + return value == numberAlphabetic || value == numberTraditional case lexicon.AttrLang: return value == "" || lexical.ValidLanguage(value) case nameStartAt: @@ -598,7 +629,7 @@ func isAlphanumeric(r rune) bool { return true } - return r > 127 && (unicode.IsLetter(r) || unicode.IsNumber(r)) + return r > utf8.RuneSelf-1 && (unicode.IsLetter(r) || unicode.IsNumber(r)) } func formatSingleNumber(num int, token string, groupSep string, groupSize int, lang string, ordinal string) string { @@ -607,13 +638,13 @@ func formatSingleNumber(num int, token string, groupSep string, groupSize int, l } switch token { - case "a": + case numberPictureLowerAlpha: return toLowerAlpha(int64(num)) - case "A": + case numberPictureUpperAlpha: return toUpperAlpha(int64(num)) - case "i": + case numberPictureLowerRoman: return strings.ToLower(toRoman(num)) - case "I": + case numberPictureUpperRoman: return toRoman(num) default: runes := []rune(token) @@ -621,7 +652,7 @@ func formatSingleNumber(num int, token string, groupSep string, groupSize int, l // Non-ASCII digit: use as base of a decimal numbering system // (e.g., ٠ = U+0660 for Arabic-Indic digits) - if firstRune > 127 && unicode.IsDigit(firstRune) { + if firstRune > utf8.RuneSelf-1 && unicode.IsDigit(firstRune) { s := formatWithDigitSystem(num, digitZeroOf(firstRune), len(runes)) if groupSep != "" && groupSize > 0 { s = applyGroupingSeparator(s, groupSep, groupSize) @@ -631,12 +662,12 @@ func formatSingleNumber(num int, token string, groupSep string, groupSize int, l } // Non-ASCII number (not a digit): ordinal numbering from that codepoint // (e.g., ① = U+2460 for circled digits) - if firstRune > 127 && unicode.IsNumber(firstRune) { + if firstRune > utf8.RuneSelf-1 && unicode.IsNumber(firstRune) { return formatWithOrdinalSystem(num, firstRune) } // Non-ASCII letter: alphabetic numbering from that codepoint // (e.g., α = U+03B1 for Greek lowercase: α, β, γ, ..., ω, αα, αβ, ...) - if firstRune > 127 && unicode.IsLetter(firstRune) { + if firstRune > utf8.RuneSelf-1 && unicode.IsLetter(firstRune) { return formatWithAlphaSystem(int64(num), firstRune) } @@ -645,7 +676,7 @@ func formatSingleNumber(num int, token string, groupSep string, groupSize int, l s := strconv.Itoa(num) if padding := minWidth - len(s); padding > 0 { - s = strings.Repeat("0", padding) + s + s = strings.Repeat(numberZeroPaddingDigit, padding) + s } if groupSep != "" && groupSize > 0 { @@ -661,13 +692,20 @@ func formatSingleNumber(num int, token string, groupSep string, groupSize int, l } func formatWordNumber(num int64, token string, lang string, ordinal string) (string, bool) { + const ( + numberPictureLowerWords = "w" + numberPictureTitleWords = "Ww" + numberPictureUpperWords = "W" + wordCaseLower = "lower" + ) + switch token { - case "w": - return numberToWordsLang(num, "lower", lang, ordinal), true - case "W": - return numberToWordsLang(num, "upper", lang, ordinal), true - case "Ww": - return numberToWordsLang(num, "title", lang, ordinal), true + case numberPictureLowerWords: + return numberToWordsLang(num, wordCaseLower, lang, ordinal), true + case numberPictureUpperWords: + return numberToWordsLang(num, wordCaseUpper, lang, ordinal), true + case numberPictureTitleWords: + return numberToWordsLang(num, wordCaseTitle, lang, ordinal), true default: return "", false } @@ -675,14 +713,14 @@ func formatWordNumber(num int64, token string, lang string, ordinal string) (str func formatAlphabeticNumber(num int64, token string) (string, bool) { switch token { - case "a": + case numberPictureLowerAlpha: return toLowerAlpha(num), true - case "A": + case numberPictureUpperAlpha: return toUpperAlpha(num), true } runes := []rune(token) - if len(runes) > 0 && runes[0] > 127 && unicode.IsLetter(runes[0]) { + if len(runes) > 0 && runes[0] > utf8.RuneSelf-1 && unicode.IsLetter(runes[0]) { return formatWithAlphaSystem(num, runes[0]), true } @@ -745,12 +783,16 @@ func lookupAlphabetLength(key rune) (int, bool) { // Greek numbering uses the shared 24-letter alphabet; other alphabets use // their configured contiguous range. func formatWithAlphaSystem(num int64, start rune) string { + const ( + alphabetDetectionStopThreshold = 100 + ) + if start == 'α' || start == 'Α' { return xpath.FormatGreekAlphabetic(big.NewInt(num), start == 'Α') } if num <= 0 { - return strconv.FormatInt(num, 10) + return strconv.FormatInt(num, numberDecimalRadix) } // Use known alphabet length if available, otherwise detect @@ -759,14 +801,14 @@ func formatWithAlphaSystem(num int64, start rune) string { seqLen = 0 for r := start; unicode.IsLetter(r); r++ { seqLen++ - if seqLen > 100 { + if seqLen > alphabetDetectionStopThreshold { break // safety cap } } } if seqLen == 0 { - return strconv.FormatInt(num, 10) + return strconv.FormatInt(num, numberDecimalRadix) } // Convert to base-N alphabetic representation (1-based, like a=1, b=2, ..., z=26, aa=27) @@ -806,6 +848,10 @@ func formatBigNumberList(bigNums []*big.Int, format string, groupSep string, gro } func parseNumberListPicture(format string) numberListPicture { + const ( + numberDefaultSeparator = "." + ) + runes := []rune(format) var ( @@ -848,15 +894,15 @@ func parseNumberListPicture(format string) numberListPicture { } if len(tokens) == 0 { - tokens = []numberListToken{{format: "1"}} + tokens = []numberListToken{{format: numberDecimalPicture}} suffix = prefix } - defaultSep := "." + defaultSep := numberDefaultSeparator if len(tokens) > 1 { defaultSep = tokens[len(tokens)-2].separator if defaultSep == "" { - defaultSep = "." + defaultSep = numberDefaultSeparator } } @@ -864,8 +910,13 @@ func parseNumberListPicture(format string) numberListPicture { } func formatNumberListToken(num *big.Int, token, groupSep string, groupSize int, lang, ordinal string) string { - if token == "α" || token == "Α" { - return xpath.FormatGreekAlphabetic(num, token == "Α") + const ( + numberPictureLowerGreek = "\u03b1" + numberPictureUpperGreek = "\u0391" + ) + + if token == numberPictureLowerGreek || token == numberPictureUpperGreek { + return xpath.FormatGreekAlphabetic(num, token == numberPictureUpperGreek) } // Word and alphabetic formats support all int64 values on any architecture. // Other non-decimal formats require a native int. @@ -893,12 +944,12 @@ func formatBigSingleNumber(num *big.Int, token string, groupSep string, groupSiz runes := []rune(token) firstRune := runes[0] // Roman numbering falls back to plain decimal outside its bounded range. - if token == "i" || token == "I" { + if token == numberPictureLowerRoman || token == numberPictureUpperRoman { return num.String() } // Non-ASCII digit system (e.g., Arabic-Indic ٠) - if firstRune > 127 && unicode.IsDigit(firstRune) { + if firstRune > utf8.RuneSelf-1 && unicode.IsDigit(firstRune) { s := formatBigWithDigitSystem(num, digitZeroOf(firstRune), len(runes)) if groupSep != "" && groupSize > 0 { s = applyGroupingSeparator(s, groupSep, groupSize) @@ -908,7 +959,7 @@ func formatBigSingleNumber(num *big.Int, token string, groupSep string, groupSiz } // Non-ASCII number tokens use a bounded ordinal system. A number beyond // that range falls back to plain decimal without a numeric ordinal suffix. - if firstRune > 127 && unicode.IsNumber(firstRune) { + if firstRune > utf8.RuneSelf-1 && unicode.IsNumber(firstRune) { return num.String() } @@ -917,7 +968,7 @@ func formatBigSingleNumber(num *big.Int, token string, groupSep string, groupSiz s := num.String() if padding := minWidth - len(s); padding > 0 { - s = strings.Repeat("0", padding) + s + s = strings.Repeat(numberZeroPaddingDigit, padding) + s } if groupSep != "" && groupSize > 0 { @@ -943,21 +994,13 @@ func formatBigWithDigitSystem(num *big.Int, zero rune, minWidth int) string { // digitZeroOf returns the zero digit for the Unicode digit block containing r. func digitZeroOf(r rune) rune { - // Unicode digit blocks are groups of 10 consecutive codepoints. - // The zero digit is always at an offset of (r - digit_value) from r. - // For standard digit blocks, digit_value = r % 10 when aligned at 0. - // Use unicode.Digit to get the numeric value. - for d := rune(0); d <= 9; d++ { - if r-d >= 0 && unicode.IsDigit(r-d) { - // Verify this is actually the zero of the block - candidate := r - d - if !unicode.IsDigit(candidate-1) || candidate == 0 { - return candidate - } + for offset := range rune(10) { + if zero := r - offset; xpath.DecimalZeroDigit(zero) { + return zero } } - // Fallback: assume digit value is r mod 10 offset - return r - (r % 10) + + return '0' } // formatWithDigitSystem formats a number using a decimal digit system @@ -972,7 +1015,7 @@ func formatWithDigitSystem(num int, zero rune, minWidth int) string { magnitude = uint(num) } - formatted := localizeDecimalDigits(strconv.FormatUint(uint64(magnitude), 10), zero, minWidth) + formatted := localizeDecimalDigits(strconv.FormatUint(uint64(magnitude), numberDecimalRadix), zero, minWidth) if negative { return "-" + formatted } @@ -1122,6 +1165,10 @@ func formatWithOrdinalSystem(num int, start rune) string { // is capped at 10 to avoid accidentally including characters from adjacent // but unrelated numbering systems. func ordinalRangeLength(r rune) int { + const ( + unknownOrdinalRangeRunes = 10 + ) + isNum := unicode.IsNumber(r) count := 0 @@ -1137,7 +1184,7 @@ func ordinalRangeLength(r rune) int { } count++ - if count >= 10 { + if count >= unknownOrdinalRangeRunes { break // cap unknown systems at 10 } } @@ -1175,16 +1222,20 @@ func applyGroupingSeparator(s string, sep string, size int) string { } func toLowerAlpha(n int64) string { + const ( + latinAlphabetRunes = 26 + ) + if n <= 0 { - return strconv.FormatInt(n, 10) + return strconv.FormatInt(n, numberDecimalRadix) } var buf []byte for n > 0 { n-- - buf = append([]byte{byte('a' + n%26)}, buf...) - n /= 26 + buf = append([]byte{byte('a' + n%latinAlphabetRunes)}, buf...) + n /= latinAlphabetRunes } return string(buf) @@ -1195,7 +1246,11 @@ func toUpperAlpha(n int64) string { } func toRoman(n int) string { - if n <= 0 || n >= 4000 { + const ( + romanExclusiveUpperBound = 4000 + ) + + if n <= 0 || n >= romanExclusiveUpperBound { return strconv.Itoa(n) } @@ -1282,13 +1337,17 @@ func compatNumberValue(seq xdm.Sequence) (*big.Int, error) { // atomicToBigInt implements xs:integer(round(number($V))) (REC §12.2). // Conversion to xs:double precedes rounding, including for integers/decimals. func atomicToBigInt(value xdm.AtomicValue) (*big.Int, error) { + const ( + numberNegativeHalfTie = -0.5 + ) + double, err := xpath.CastAtomic(value, xdm.TypeDouble) if err != nil { return nil, dynamicError(errCodeXTDE0980, "xsl:number value cannot be converted to a number") } rounded := math.Round(double.DoubleVal()) - if double.DoubleVal() == -0.5 { + if double.DoubleVal() == numberNegativeHalfTie { rounded = 0 // fn:round resolves ties toward positive infinity. } @@ -1401,7 +1460,7 @@ func englishNumberWords(n int64, ones, tens []string) string { case n < numberTrillion: return englishScaledNumber(n, numberBillion, "billion", ones, tens) default: - return strconv.FormatInt(n, 10) + return strconv.FormatInt(n, numberDecimalRadix) } } diff --git a/transform/internal/xslt/execute_resultdoc.go b/transform/internal/xslt/execute_resultdoc.go index 95a226d..8716c4c 100644 --- a/transform/internal/xslt/execute_resultdoc.go +++ b/transform/internal/xslt/execute_resultdoc.go @@ -15,6 +15,11 @@ import ( "github.com/skarm/xmlsec/transform/internal/xpath" ) +// Exact diagnostic identifiers shared by these XSLT checks; naming a code does not change its selected error path or message. +const ( + errCodeXTDE1500 = "XTDE1500" +) + // paramDocPresence records every explicitly supplied canonical parameter, // including false booleans, empty strings/lists and an empty character map. type paramDocPresence map[string]struct{} @@ -33,7 +38,7 @@ func (ec *execContext) CheckRead(ctx context.Context, uri string) error { return err } - if ec.stylesheet.processorLanguage() < 30 { + if ec.stylesheet.processorLanguage() < processorXSLT30 { return nil } @@ -48,20 +53,24 @@ func (ec *execContext) CheckRead(ctx context.Context, uri string) error { if key == ec.canonicalPrimaryURI { if ec.primaryClaimedImplicitly { - return dynamicError("XTDE1500", "resource %q is both read and written", key) + return dynamicError(errCodeXTDE1500, "resource %q is both read and written", key) } key = "" } if _, written := ec.usedResultURIs[key]; written { - return dynamicError("XTDE1500", "resource %q is both read and written", uri) + return dynamicError(errCodeXTDE1500, "resource %q is both read and written", uri) } return nil } func (ec *execContext) RecordRead(ctx context.Context, uri string) error { + const ( + readURIAccountingBytes = 32 + ) + if uri == "" { return ctx.Err() } @@ -74,7 +83,7 @@ func (ec *execContext) RecordRead(ctx context.Context, uri string) error { return err } - if ec.stylesheet.processorLanguage() < 30 { + if ec.stylesheet.processorLanguage() < processorXSLT30 { return nil } @@ -87,7 +96,7 @@ func (ec *execContext) RecordRead(ctx context.Context, uri string) error { return nil } - if err := xpath.ChargeString(ctx, len(key)+32); err != nil { + if err := xpath.ChargeString(ctx, len(key)+readURIAccountingBytes); err != nil { return err } @@ -148,16 +157,21 @@ func (ec *execContext) getParamDocPresence(inst *resultDocumentInst) paramDocPre // the result of executing the body. func (ec *execContext) execDocument(ctx context.Context, inst *documentInst) error { tmpDoc := xdm.NewDefaultDocument() + tmpDoc.SetProperties(tmpDoc.Properties() | xdm.DocInternal) + tmpDoc.SetURL(ec.effectiveStaticBaseURI()) frame := &outputFrame{doc: tmpDoc, current: tmpDoc} + savedResultDoc := ec.resultDoc + ec.resultDoc = tmpDoc ec.outputStack = append(ec.outputStack, frame) - if err := ec.executeSequenceConstructor(ctx, inst.Body); err != nil { - ec.outputStack = ec.outputStack[:len(ec.outputStack)-1] + err := ec.executeSequenceConstructor(ctx, inst.Body) + ec.outputStack = ec.outputStack[:len(ec.outputStack)-1] + ec.resultDoc = savedResultDoc + + if err != nil { return err } - ec.outputStack = ec.outputStack[:len(ec.outputStack)-1] - // Apply type validation (xsl:document type="..."). if inst.TypeName != "" { // XTTE1550: document must have exactly one element child. @@ -510,7 +524,7 @@ func (ec *execContext) checkResourceWrite(uri string) error { return ec.resourceOwner.checkResourceWrite(uri) } - if ec.stylesheet.processorLanguage() < 30 { + if ec.stylesheet.processorLanguage() < processorXSLT30 { return nil } @@ -519,7 +533,7 @@ func (ec *execContext) checkResourceWrite(uri string) error { } if _, read := ec.readResourceURIs[resourceURIKey(uri)]; read { - return dynamicError("XTDE1500", "resource %q is both read and written", uri) + return dynamicError(errCodeXTDE1500, "resource %q is both read and written", uri) } return nil @@ -687,7 +701,7 @@ func (ec *execContext) validatePrimaryResultItems(items xdm.Sequence, method str func (ec *execContext) execPrimaryItemDocument(ctx context.Context, inst *resultDocumentInst, effectiveMethod string, itemSep *string, primaryOverrides *OutputDef) error { tmpDoc := xdm.NewDefaultDocument() - tmpRoot, err := tmpDoc.CreateElement("_tmp") + tmpRoot, err := tmpDoc.CreateElement(temporaryOutputWrapper) if err != nil { return err } @@ -920,7 +934,7 @@ func (ec *execContext) evalBoolSerializationAVT(ctx context.Context, a *avt, par b, ok := parseXSLTBoolean(v, ec.stylesheet.processorLanguage()) if !ok { code := lexicon.ErrSEPM0016 - if ec.stylesheet.processorLanguage() == 20 { + if ec.stylesheet.processorLanguage() == processorXSLT20 { code = errCodeXTDE0030 } @@ -937,17 +951,17 @@ func (ec *execContext) evalStandaloneSerializationAVT(ctx context.Context, attri return "", err } - value = strings.Trim(value, " \t\r\n") - if ec.stylesheet.processorLanguage() == 20 && value != lexicon.ValueOmit { - if _, valid := parseXSLTBoolean(value, 20); !valid { + value = strings.Trim(value, lexicon.XMLWhitespace) + if ec.stylesheet.processorLanguage() == processorXSLT20 && value != lexicon.ValueOmit { + if _, valid := parseXSLTBoolean(value, processorXSLT20); !valid { return "", dynamicError(errCodeXTDE0030, "%q is not a valid XSLT 2.0 value for xsl:result-document/@standalone", value) } } switch value { - case lexicon.ValueTrue, "1": + case lexicon.ValueTrue, booleanDigitTrue: return lexicon.ValueYes, nil - case lexicon.ValueFalse, "0": + case lexicon.ValueFalse, booleanDigitFalse: return lexicon.ValueNo, nil default: return value, nil diff --git a/transform/internal/xslt/execute_sequence.go b/transform/internal/xslt/execute_sequence.go index b64f7bc..752a9ab 100644 --- a/transform/internal/xslt/execute_sequence.go +++ b/transform/internal/xslt/execute_sequence.go @@ -85,7 +85,7 @@ func (ec *execContext) evaluateValueOfSelect(ctx context.Context, inst *valueOfI filtered := filterZeroLengthTextNodes(seq) filtered = mergeAdjacentTextNodes(filtered) - if ec.stylesheet.processorLanguage() == 10 { + if ec.stylesheet.processorLanguage() == processorXSLT10 { value, err = xpath.XPath10String(filtered) if err != nil { return "", false, err @@ -114,12 +114,12 @@ func (ec *execContext) evaluateValueOfBody(ctx context.Context, inst *valueOfIns } // XSLT 2.0: value-of body is temporary output state (XTDE1480). // XSLT 3.0 relaxes this restriction. - isV2TempOutput := ec.stylesheet.processorLanguage() < 30 + isV2TempOutput := ec.stylesheet.processorLanguage() < processorXSLT30 if isV2TempOutput { ec.temporaryOutputDepth++ } - val, err := ec.evaluateBodySeparateText(ctx, inst.Body) + val, err := ec.evaluateBodyAsSequence(ctx, inst.Body) if isV2TempOutput { ec.temporaryOutputDepth-- } @@ -139,7 +139,7 @@ func (ec *execContext) evaluateValueOfBody(ctx context.Context, inst *valueOfIns func (ec *execContext) writeConstructedText(value string, disableEscaping bool) error { if disableEscaping { // Insert DOE marker PI so the serializer writes this text raw. - pi := ec.resultDoc.CreatePI(nameDisableOutputEscaping, "") + pi := ec.resultDoc.CreatePI(lexicon.DisableOutputEscapingPITarget, "") if err := ec.addNode(pi); err != nil { return err } @@ -149,7 +149,7 @@ func (ec *execContext) writeConstructedText(value string, disableEscaping bool) return err } - piEnd := ec.resultDoc.CreatePI("enable-output-escaping", "") + piEnd := ec.resultDoc.CreatePI(lexicon.EnableOutputEscapingPITarget, "") return ec.addNode(piEnd) } diff --git a/transform/internal/xslt/execute_streaming.go b/transform/internal/xslt/execute_streaming.go index 981da14..efd85aa 100644 --- a/transform/internal/xslt/execute_streaming.go +++ b/transform/internal/xslt/execute_streaming.go @@ -16,6 +16,17 @@ import ( "github.com/skarm/xmlsec/transform/internal/xpath" ) +// Implicit accumulator $value is unqualified and shared by declaration-scope visibility and rule evaluation; xsl:catch variables and XML value attributes use separate names. +const ( + accumulatorVariableValue = "value" +) + +// Accumulator rule phases shared by compilation and execution: start observes the pre-descent value, end observes the post-descent value. +const ( + accumulatorPhaseEnd = "end" + accumulatorPhaseStart = "start" +) + const ( mergeAttrOrder uint8 = 1 << iota mergeAttrDataType @@ -422,10 +433,7 @@ func (ec *execContext) execNextIteration(ctx context.Context, inst *nextIteratio return errNextIter } -// execMerge executes xsl:merge by loading, sorting, and merging items from -// multiple sources, then executing the merge-action for each group of items -// sharing the same key. -// Each merge evaluates its five key attributes in the containing merge focus. +// resolveMergeDefinitions evaluates merge-key attributes in the containing focus. // Resolve into private definitions so concurrent executions never mutate the // compiled stylesheet or compare unevaluated AVT source text. func (ec *execContext) resolveMergeDefinitions(ctx context.Context, definitions []*mergeSource) ([]*mergeSource, error) { @@ -478,7 +486,7 @@ func (ec *execContext) resolveMergeKeyOptions(ctx context.Context, key *mergeKey return err } - value = strings.Trim(value, " \t\r\n") + value = strings.Trim(value, lexicon.XMLWhitespace) if key.specified&mergeAttrCollation == 0 || option.name != lexicon.AttrLang && option.name != nameCaseOrder { if err := validateLiteralSortOption(option.name, value); err != nil { return dynamicError(errCodeXTDE0030, "invalid %s value %q in xsl:merge-key", option.name, value) @@ -496,6 +504,8 @@ func (ec *execContext) resolveMergeKeyOptions(ctx context.Context, key *mergeKey return nil } +// execMerge loads, sorts and merges source items, executing merge-action +// for each group sharing the same key. func (ec *execContext) execMerge(ctx context.Context, inst *mergeInst) error { definitions, err := ec.resolveMergeDefinitions(ctx, inst.Sources) if err != nil { @@ -631,6 +641,7 @@ func (ec *execContext) resolveMergeCollations(ctx context.Context, keyDefs []*me func (ec *execContext) installMergeFunctions(sources []*mergeSource) (*mergeActionState, func()) { ec.xsltFunctions() + ec.xsltFunctionsNS() state := &mergeActionState{ec: ec, validSourceNames: make(map[string]struct{})} for _, source := range sources { @@ -641,10 +652,16 @@ func (ec *execContext) installMergeFunctions(sources []*mergeSource) (*mergeActi savedGroup := ec.cachedFns[nameCurrentMergeGroup] savedKey := ec.cachedFns[nameCurrentMergeKey] + groupName := xpath.QualifiedName{URI: lexicon.NamespaceFn, Name: nameCurrentMergeGroup} + keyName := xpath.QualifiedName{URI: lexicon.NamespaceFn, Name: nameCurrentMergeKey} + savedGroupNS, savedKeyNS := ec.cachedFnsNS[groupName], ec.cachedFnsNS[keyName] ec.cachedFns[nameCurrentMergeGroup] = &xsltFunc{min: 0, max: 1, fn: state.currentGroup} ec.cachedFns[nameCurrentMergeKey] = &xsltFunc{min: 0, max: 0, fn: state.currentKey} + ec.cachedFnsNS[groupName] = ec.cachedFns[nameCurrentMergeGroup] + ec.cachedFnsNS[keyName] = ec.cachedFns[nameCurrentMergeKey] return state, func() { + ec.cachedFnsNS[groupName], ec.cachedFnsNS[keyName] = savedGroupNS, savedKeyNS if savedGroup != nil { ec.cachedFns[nameCurrentMergeGroup] = savedGroup } else { @@ -672,6 +689,8 @@ func (ec *execContext) setMergeFocus(item xdm.Item) { } func (ec *execContext) executeMergeGroups(ctx context.Context, inst *mergeInst, groups []mergeGroup) error { + const absentMergeContextSize = -1 + state, restoreFunctions := ec.installMergeFunctions(inst.Sources) defer restoreFunctions() @@ -687,7 +706,7 @@ func (ec *execContext) executeMergeGroups(ctx context.Context, inst *mergeInst, for _, source := range inst.Sources { if source.StreamableAttr { - ec.size = -1 // XSLT 3.0 §15.7: context size is absent. + ec.size = absentMergeContextSize // XSLT 3.0 §15.7: context size is absent. break } } @@ -762,7 +781,7 @@ func (ec *execContext) gatherMergeDocuments(ctx context.Context, src *mergeSourc ctx, uris.Sequence(), xdm.SequenceType{ - ItemTest: xdm.AtomicOrUnionType{Name: "Q{" + lexicon.NamespaceXSD + "}string"}, + ItemTest: xdm.AtomicOrUnionType{Name: "Q{" + lexicon.NamespaceXSD + "}" + lexicon.TypeString}, Occurrence: xdm.OccurrenceZeroOrMore, }, ) @@ -1002,6 +1021,16 @@ func (ec *execContext) prepareSourceDocumentAccumulators(ctx context.Context, in } func (ec *execContext) computeAccumulatorStates(ctx context.Context, doc xdm.Node, names []string) error { + // Accumulator declarations see globals, never the caller's local bindings. + locals := ec.localVars + ec.localVars = nil + + ec.localVarsVer++ + defer func() { + ec.localVars = locals + ec.localVarsVer++ + }() + if ec.accumulatorBeforeByNode == nil { ec.accumulatorBeforeByNode = make(map[xdm.Node]map[string]xdm.Sequence) } @@ -1099,7 +1128,7 @@ func (ec *execContext) walkAccumulatorTree(ctx context.Context, node xdm.Node, n // Per XSLT 3.0 §14.1, accumulator-before() returns the value AFTER start-phase // rules for the current node have fired (i.e., the value "before" descending into // the node's children), not the value before the node's own rules run. - if err := ec.applyAccumulatorPhase(ctx, node, names, "start"); err != nil { + if err := ec.applyAccumulatorPhase(ctx, node, names, accumulatorPhaseStart); err != nil { return err } @@ -1117,7 +1146,7 @@ func (ec *execContext) walkAccumulatorTree(ctx context.Context, node xdm.Node, n } } - if err := ec.applyAccumulatorPhase(ctx, node, names, "end"); err != nil { + if err := ec.applyAccumulatorPhase(ctx, node, names, accumulatorPhaseEnd); err != nil { return err } @@ -1197,7 +1226,7 @@ func (ec *execContext) evaluateAccumulatorRule(ctx context.Context, node xdm.Nod } ec.pushVarScope() - ec.setVar("value", currentValue) + ec.setVar(accumulatorVariableValue, currentValue) ec.temporaryOutputDepth++ defer func() { @@ -1514,24 +1543,24 @@ func (ec *execContext) resolveMergeKeyCollationURI(ctx context.Context, key *mer return nil, "", err } - collation = resolveAgainstBaseURI(strings.Trim(value, " \t\r\n"), key.BaseURI) + collation = resolveAgainstBaseURI(strings.Trim(value, lexicon.XMLWhitespace), key.BaseURI) } if key.specified&mergeAttrCollation == 0 { var parameters []string if key.Lang != "" { - parameters = append(parameters, "lang="+key.Lang) + parameters = append(parameters, ucaLanguageParameter+key.Lang) } switch key.CaseOrder { case nameUpperFirst: - parameters = append(parameters, "caseFirst=upper") + parameters = append(parameters, ucaUpperFirstParameter) case nameLowerFirst: - parameters = append(parameters, "caseFirst=lower") + parameters = append(parameters, ucaLowerFirstParameter) } if len(parameters) > 0 { - collation = "http://www.w3.org/2013/collation/UCA?" + strings.Join(parameters, ";") + collation = lexicon.CollationUCA + "?" + strings.Join(parameters, ";") } } diff --git a/transform/internal/xslt/execute_templates.go b/transform/internal/xslt/execute_templates.go index 3a88b7c..44a3b05 100644 --- a/transform/internal/xslt/execute_templates.go +++ b/transform/internal/xslt/execute_templates.go @@ -11,7 +11,9 @@ import ( "github.com/skarm/xmlsec/transform/internal/xpath" ) -// executeTemplate executes a template with the given node as context. +// maxRecursionDepth is the fallback ceiling for nested template, function and +// attribute-set execution, and for the HTML serializer namespace stack. +// An active XPath depth budget replaces this fallback through DepthLimit. const maxRecursionDepth = 2000 // conflictCandidateResult classifies one candidate template probed by @@ -58,12 +60,12 @@ func (ec *execContext) applyTemplates(ctx context.Context, node xdm.Node, mode s // XTTE3110: typed="no" mode rejects typed nodes if md := ec.lookupModeDef(mode); md != nil { switch md.Typed { - case lexicon.ValueYes, lexicon.ValueTrue, "1", validationStrict, validationLax: + case lexicon.ValueYes, lexicon.ValueTrue, booleanDigitTrue, validationStrict, validationLax: if !ec.nodeIsTyped(node) { return dynamicError(errCodeXTTE3100, "mode %q has typed=%q but node is untyped", mode, md.Typed) } - case lexicon.ValueNo, lexicon.ValueFalse, "0": + case lexicon.ValueNo, lexicon.ValueFalse, booleanDigitFalse: if ec.nodeIsTyped(node) { return dynamicError(errCodeXTTE3110, "mode %q has typed=%q but node is typed", mode, md.Typed) @@ -489,6 +491,14 @@ func (ec *execContext) executeAtomicTemplate(ctx context.Context, tmpl *template ec.pushVarScope() defer ec.popVarScope() + if tmpl.ContextItemUse == ctxItemAbsent { + ec.contextItem = nil + } + + if err := ec.checkContextItemType(tmpl); err != nil { + return err + } + // Collect param overrides supplied by the caller (via xsl:with-param). var po map[string]xdm.Sequence if len(paramOverrides) > 0 { @@ -541,6 +551,26 @@ func (ec *execContext) executeTemplate(ctx context.Context, tmpl *template, node savedXPathDefaultNS := ec.xpathDefaultNS savedHasXPathDefaultNS := ec.hasXPathDefaultNS savedDefaultCollation := ec.defaultCollation + // Track overriding template for xsl:original support + savedOverridingTemplate := ec.overridingTemplate + if tmpl.OriginalTemplate != nil { + ec.overridingTemplate = tmpl + } + defer func() { + ec.currentNode = savedCurrent + ec.contextNode = savedContext + ec.contextItem = savedItem + ec.currentMode = savedMode + ec.setCurrentTemplate(savedTemplate) + ec.overridingTemplate = savedOverridingTemplate + ec.xpathDefaultNS = savedXPathDefaultNS + ec.hasXPathDefaultNS = savedHasXPathDefaultNS + ec.defaultCollation = savedDefaultCollation + ec.position = savedPos + ec.size = savedSize + ec.tunnelParams = savedTunnel + }() + ec.currentNode = node ec.contextNode = node ec.currentMode = mode @@ -566,25 +596,6 @@ func (ec *execContext) executeTemplate(ctx context.Context, tmpl *template, node if err := ec.checkContextItemType(tmpl); err != nil { return err } - // Track overriding template for xsl:original support - savedOverridingTemplate := ec.overridingTemplate - if tmpl.OriginalTemplate != nil { - ec.overridingTemplate = tmpl - } - defer func() { - ec.currentNode = savedCurrent - ec.contextNode = savedContext - ec.contextItem = savedItem - ec.currentMode = savedMode - ec.setCurrentTemplate(savedTemplate) - ec.overridingTemplate = savedOverridingTemplate - ec.xpathDefaultNS = savedXPathDefaultNS - ec.hasXPathDefaultNS = savedHasXPathDefaultNS - ec.defaultCollation = savedDefaultCollation - ec.position = savedPos - ec.size = savedSize - ec.tunnelParams = savedTunnel - }() ec.pushVarScope() defer ec.popVarScope() @@ -666,8 +677,12 @@ func (ec *execContext) checkTemplateParam(ctx context.Context, p *param, val xdm } func (ec *execContext) parameterDefaultErrorCode() string { - if ec.stylesheet.processorLanguage() == 20 { - return "XTTE0600" + const ( + errCodeXTTE0600 = "XTTE0600" + ) + + if ec.stylesheet.processorLanguage() == processorXSLT20 { + return errCodeXTTE0600 } return errCodeXTTE0590 @@ -679,8 +694,8 @@ func (ec *execContext) missingTemplateParam(p *param) error { } code := errCodeXTDE0700 - if !p.Required && ec.stylesheet.processorLanguage() == 20 { - code = "XTDE0610" + if !p.Required && ec.stylesheet.processorLanguage() == processorXSLT20 { + code = errCodeXTDE0610 } return dynamicError(code, "required template parameter $%s was not supplied", p.Name) @@ -826,7 +841,7 @@ func (ec *execContext) writeTypedTemplateNode(v xdm.NodeItem, out *outputFrame, // (comments, attributes, PIs) per XSLT 3.0 §20.1. emitDOE := isDOE && ec.temporaryOutputDepth == 0 && !out.captureItems && !out.sequenceMode if emitDOE { - pi := ec.resultDoc.CreatePI(lexicon.OutputDisableEscaping, "") + pi := ec.resultDoc.CreatePI(lexicon.DisableOutputEscapingPITarget, "") if err := ec.addNode(pi); err != nil { return err } @@ -844,7 +859,7 @@ func (ec *execContext) writeTypedTemplateNode(v xdm.NodeItem, out *outputFrame, } if emitDOE { - piEnd := ec.resultDoc.CreatePI("enable-output-escaping", "") + piEnd := ec.resultDoc.CreatePI(lexicon.EnableOutputEscapingPITarget, "") if err := ec.addNode(piEnd); err != nil { return err } @@ -923,12 +938,12 @@ func stripDOEMarkers(seq xdm.Sequence) (xdm.Sequence, map[int]struct{}) { for item := range xdm.SequenceItems(seq) { if ni, ok := item.(xdm.NodeItem); ok && ni.Node.Type() == xdm.ProcessingInstructionNode { piName := ni.Node.Name() - if piName == lexicon.OutputDisableEscaping { + if piName == lexicon.DisableOutputEscapingPITarget { doeActive = true continue } - if piName == "enable-output-escaping" { + if piName == lexicon.EnableOutputEscapingPITarget { doeActive = false continue } diff --git a/transform/internal/xslt/execute_transform.go b/transform/internal/xslt/execute_transform.go index 650dba6..21118c8 100644 --- a/transform/internal/xslt/execute_transform.go +++ b/transform/internal/xslt/execute_transform.go @@ -430,6 +430,10 @@ func (ec *execContext) executeInitialFunction(ctx context.Context, cfg *transfor } func (ec *execContext) initialTemplateName(cfg *transformConfig) string { + const ( + initialTemplateLexicalName = "xsl:initial-template" + ) + initialTemplateName := "" if cfg != nil && cfg.initialTemplate != "" { initialTemplateName = resolveQName(cfg.initialTemplate, ec.stylesheet.namespaces) @@ -437,11 +441,11 @@ func (ec *execContext) initialTemplateName(cfg *transformConfig) string { // XSLT 3.0: if no explicit initial template, check for xsl:initial-template if initialTemplateName == "" { - xsltInitial := xdm.ClarkName(lexicon.NamespaceXSLT, "initial-template") + xsltInitial := xdm.ClarkName(lexicon.NamespaceXSLT, initialTemplateLocalName) if _, ok := ec.stylesheet.namedTemplates[xsltInitial]; ok { initialTemplateName = xsltInitial - } else if _, ok := ec.stylesheet.namedTemplates["xsl:initial-template"]; ok { - initialTemplateName = "xsl:initial-template" + } else if _, ok := ec.stylesheet.namedTemplates[initialTemplateLexicalName]; ok { + initialTemplateName = initialTemplateLexicalName } } @@ -606,13 +610,17 @@ func (ec *execContext) applyInitialSelection(ctx context.Context, selection xdm. } func (ec *execContext) checkInitialTemplateParameters(tmpl *template) error { - if ec.stylesheet.processorLanguage() != 20 { + const ( + errCodeXTDE0060 = "XTDE0060" + ) + + if ec.stylesheet.processorLanguage() != processorXSLT20 { return nil } for _, parameter := range tmpl.Params { if parameter.Required { - return dynamicError("XTDE0060", "XSLT 2.0 initial template must not declare required parameter $%s", parameter.Name) + return dynamicError(errCodeXTDE0060, "XSLT 2.0 initial template must not declare required parameter $%s", parameter.Name) } } @@ -772,7 +780,7 @@ func (ec *execContext) resolvePrimaryOutputDef() *OutputDef { } } - if outDef == nil && ss.processorLanguage() <= 20 { + if outDef == nil && ss.processorLanguage() <= processorXSLT20 { outDef = defaultOutputDef() } diff --git a/transform/internal/xslt/execute_trycatch.go b/transform/internal/xslt/execute_trycatch.go index 7f7e8f3..a92c89e 100644 --- a/transform/internal/xslt/execute_trycatch.go +++ b/transform/internal/xslt/execute_trycatch.go @@ -14,6 +14,16 @@ import ( "github.com/skarm/xmlsec/transform/internal/xpath" ) +// Implicit xsl:catch variable local names in the errors namespace; static scope visibility and runtime bindings must agree. +const ( + catchVariableCode = "code" + catchVariableColumn = "column-number" + catchVariableDescription = "description" + catchVariableLine = "line-number" + catchVariableModule = "module" + catchVariableValue = "value" +) + func (ec *execContext) execMessage(ctx context.Context, inst *messageInst) error { return ec.execMessageWithDefaults(ctx, inst, false, errCodeXTMM9000) } @@ -51,7 +61,7 @@ func (ec *execContext) execMessageWithDefaults(ctx context.Context, inst *messag var valid bool - terminate, valid = parseMessageTerminate(termStr, ec.stylesheet.processorLanguage() >= 30) + terminate, valid = parseMessageTerminate(termStr, ec.stylesheet.processorLanguage() >= processorXSLT30) if !valid { return dynamicError(errCodeXTDE0030, "invalid xsl:message/@terminate value %q", termStr) } @@ -95,7 +105,7 @@ func (ec *execContext) messageErrorCode(ctx context.Context, inst *messageInst, return defaultName, preserveControlFailure(ctx, err) } - value = strings.Trim(value, " \t\r\n") + value = strings.Trim(value, lexicon.XMLWhitespace) if isValidEQName(value) { uri, local, _ := strings.Cut(value[2:], "}") if !strings.ContainsAny(uri, "{}") { @@ -123,7 +133,7 @@ func (ec *execContext) messageDocument(ctx context.Context, inst *messageInst) ( body = append([]instruction{©OfInst{Select: inst.Select, CopyNamespaces: true}}, body...) } - temporary := ec.stylesheet.processorLanguage() < 30 + temporary := ec.stylesheet.processorLanguage() < processorXSLT30 if temporary { ec.temporaryOutputDepth++ defer func() { ec.temporaryOutputDepth-- }() @@ -182,7 +192,7 @@ func (ec *execContext) execTryCatch(ctx context.Context, inst *tryCatchInst) err } if tryErr != nil && (errors.Is(tryErr, errBreak) || errors.Is(tryErr, errNextIter)) { - if err := ec.copyTryChildren(root); err != nil { + if err := ec.commitTryOutput(root, tryFrame); err != nil { return err } @@ -199,9 +209,13 @@ func (ec *execContext) execTryCatch(ctx context.Context, inst *tryCatchInst) err } func (ec *execContext) beginTryOutput() (*xdm.Element, error) { + const ( + tryOutputWrapper = "_try" + ) + doc := xdm.NewDefaultDocument() - root, err := doc.CreateElement("_try") + root, err := doc.CreateElement(tryOutputWrapper) if err != nil { return nil, err } @@ -210,9 +224,13 @@ func (ec *execContext) beginTryOutput() (*xdm.Element, error) { return nil, err } - // Inherit capture so maps, arrays and function items can flow through. - capture := ec.currentOutput().captureItems - ec.outputStack = append(ec.outputStack, &outputFrame{doc: doc, current: root, captureItems: capture, implicitPrimary: ec.currentOutput().implicitPrimary}) + // A captured transaction keeps all items in one ordered sequence. + parent := ec.currentOutput() + ec.outputStack = append(ec.outputStack, &outputFrame{ + doc: doc, current: root, captureItems: parent.captureItems, + sequenceMode: parent.captureItems || parent.sequenceMode, + itemSeparator: parent.itemSeparator, implicitPrimary: parent.implicitPrimary, + }) return root, nil } @@ -231,13 +249,7 @@ func (ec *execContext) executeTryBody(ctx context.Context, inst *tryCatchInst) e return ec.checkImplicitPrimaryOutput() } - for _, child := range inst.Try { - if err := ec.executeInstruction(ctx, child); err != nil { - return err - } - } - - return nil + return ec.executeSequenceConstructor(ctx, inst.Try) } func (ec *execContext) commitTryOutput(root *xdm.Element, frame *outputFrame) error { @@ -283,9 +295,9 @@ func (ec *execContext) executeMatchedCatch(ctx context.Context, clauses []*catch } func (ec *execContext) bindCatchVariables(namespace, description string, code xdm.QNameValue, value xdm.Sequence) { - ec.setVar(xdm.ClarkName(namespace, "code"), xdm.ItemSlice{xdm.AtomicValue{TypeName: xdm.TypeQName, Value: code}}) - ec.setVar(xdm.ClarkName(namespace, "description"), xdm.SingleString(description)) - ec.setVar(xdm.ClarkName(namespace, "value"), value) + ec.setVar(xdm.ClarkName(namespace, catchVariableCode), xdm.ItemSlice{xdm.AtomicValue{TypeName: xdm.TypeQName, Value: code}}) + ec.setVar(xdm.ClarkName(namespace, catchVariableDescription), xdm.SingleString(description)) + ec.setVar(xdm.ClarkName(namespace, catchVariableValue), value) var module, line xdm.Sequence if ec.errSourceModule != "" { @@ -296,9 +308,9 @@ func (ec *execContext) bindCatchVariables(namespace, description string, code xd line = xdm.SingleInteger(int64(ec.errSourceLine)) } - ec.setVar(xdm.ClarkName(namespace, "module"), module) - ec.setVar(xdm.ClarkName(namespace, "line-number"), line) - ec.setVar(xdm.ClarkName(namespace, "column-number"), xdm.EmptySequence()) + ec.setVar(xdm.ClarkName(namespace, catchVariableModule), module) + ec.setVar(xdm.ClarkName(namespace, catchVariableLine), line) + ec.setVar(xdm.ClarkName(namespace, catchVariableColumn), xdm.EmptySequence()) } // execTryCatchNoRollback writes directly to the parent output and rejects @@ -381,15 +393,15 @@ func preserveControlFailure(ctx context.Context, err error) error { // Boolean synonyms are introduced by the XSLT 3.0 processor. XML whitespace // stripping must not silently admit NBSP or other non-XML separators. func parseMessageTerminate(value string, modern bool) (bool, bool) { - value = strings.Trim(value, " \t\r\n") + value = strings.Trim(value, lexicon.XMLWhitespace) switch value { case lexicon.ValueYes: return true, true case lexicon.ValueNo: return false, true - case lexicon.ValueTrue, "1": + case lexicon.ValueTrue, booleanDigitTrue: return true, modern - case lexicon.ValueFalse, "0": + case lexicon.ValueFalse, booleanDigitFalse: return false, modern default: return false, false diff --git a/transform/internal/xslt/execution_test.go b/transform/internal/xslt/execution_test.go index d2725c8..9bddc74 100644 --- a/transform/internal/xslt/execution_test.go +++ b/transform/internal/xslt/execution_test.go @@ -3,6 +3,8 @@ package xslt //nolint:testpackage // Tests the compiler and invocation contracts import ( "context" "errors" + "io" + "reflect" "strings" "testing" @@ -196,7 +198,8 @@ func TestInvocationOutputSnapshot(t *testing.T) { t.Fatalf("caller changed retained output: %+v", got) } clone := inv.BaseOutputURI("https://output.example/result") - if clone.ResolvedOutputDef() != nil || inv.ResolvedOutputDef().Method != "text" { + retained := inv.ResolvedOutputDef() + if clone.ResolvedOutputDef() != nil || retained == nil || retained.Method != "text" { t.Fatal("fluent branch shares resolved output state") } ctx, cancel := context.WithCancel(t.Context()) @@ -205,3 +208,54 @@ func TestInvocationOutputSnapshot(t *testing.T) { t.Fatalf("canceled invocation = %q, %v", got, err) } } + +type streamTestResolver struct { + source string +} + +func (r streamTestResolver) Resolve(ctx context.Context, _ string) (io.ReadCloser, error) { + if err := ctx.Err(); err != nil { + return nil, err + } + return io.NopCloser(strings.NewReader(r.source)), nil +} + +func TestStreamAvailableUsesDefaultParseOptions(t *testing.T) { + t.Parallel() + ss := compileTestStylesheet(t, "", `unused`) + for _, test := range []struct { + name, source string + limit int64 + want bool + }{ + {"XML", ``, 0, true}, + {"DTD rejected", `]>&x;`, 0, false}, + {"byte limit", strings.Repeat(" ", 64) + ``, 8, false}, + } { + t.Run(test.name, func(t *testing.T) { + t.Parallel() + ec := &execContext{stylesheet: ss, transformConfig: &transformConfig{uriResolver: streamTestResolver{source: test.source}, maxResourceBytes: test.limit}} + if ec.inputParseOptions() != nil { + t.Fatal("test requires the default parse policy") + } + result, err := ec.fnStreamAvailable(t.Context(), []xdm.Sequence{xdm.SingleString("https://source.example/input.xml")}) + if err != nil { + t.Fatal(err) + } + if xdm.SequenceLen(result) != 1 || !reflect.DeepEqual(result.Get(0), xdm.AtomicValue{TypeName: xdm.TypeBoolean, Value: test.want}) { + t.Fatalf("stream-available = %v; want %v", result, test.want) + } + }) + } +} + +func TestDocumentNodeFromAbsentNode(t *testing.T) { + t.Parallel() + var doc *xdm.Document + var element *xdm.Element + for _, node := range []xdm.Node{nil, doc, element} { + if got := documentNodeFromNode(node); got != nil { + t.Fatalf("absent node acquired document %v", got) + } + } +} diff --git a/transform/internal/xslt/function_signatures.go b/transform/internal/xslt/function_signatures.go index aaa1599..ed2e7fc 100644 --- a/transform/internal/xslt/function_signatures.go +++ b/transform/internal/xslt/function_signatures.go @@ -96,6 +96,14 @@ func (f *legacyTextFunction) FuncParamTypesForArity(arity int) []xdm.SequenceTyp } func (f *legacyTextFunction) Call(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { + const ( + errCodeFOUT1170 = "FOUT1170" + errCodeFOUT1190 = "FOUT1190" + errCodeFOUT1200 = "FOUT1200" + errCodeXTDE1190 = "XTDE1190" + errCodeXTDE1200 = "XTDE1200" + ) + if f.name == fnNameUnparsedTextAvailable && len(args) == 2 && xdm.SequenceLen(args[1]) == 0 { args = args[:1] } @@ -109,15 +117,15 @@ func (f *legacyTextFunction) Call(ctx context.Context, args []xdm.Sequence) (xdm code := "" switch detail.Code { - case "FOUT1170": - code = "XTDE1170" - case "FOUT1190": - code = "XTDE1190" + case errCodeFOUT1170: + code = errCodeXTDE1170 + case errCodeFOUT1190: + code = errCodeXTDE1190 if errors.Is(err, unparsedtext.ErrImplicitUTF8) { - code = "XTDE1200" + code = errCodeXTDE1200 } - case "FOUT1200": - code = "XTDE1200" + case errCodeFOUT1200: + code = errCodeXTDE1200 } if code != "" { @@ -129,7 +137,7 @@ func (f *legacyTextFunction) Call(ctx context.Context, args []xdm.Sequence) (xdm } func newLegacyTextFunction(name string) xpath.Function { - return &legacyTextFunction{TypedFunctionByArity: xpath.HostBuiltin(20, name), name: name} + return &legacyTextFunction{TypedFunctionByArity: xpath.HostBuiltin(xpathProfile20, name), name: name} } type functionTypeKey struct { diff --git a/transform/internal/xslt/functions.go b/transform/internal/xslt/functions.go index 02f59e3..cf1f27a 100644 --- a/transform/internal/xslt/functions.go +++ b/transform/internal/xslt/functions.go @@ -21,6 +21,32 @@ import ( "github.com/skarm/xmlsec/transform/internal/xpath" ) +// XPath function local names used by XSLT expression/pattern handling, distinct from identically spelled XML attributes. +const ( + fnNameFormatDate = "format-date" + fnNameFormatDateTime = "format-dateTime" + fnNameFormatTime = "format-time" + fnNameFunctionLookup = "function-lookup" + fnNameUnparsedText = "unparsed-text" +) + +// Exact diagnostic identifiers shared by these XSLT checks; naming a code does not change its selected error path or message. +const ( + errCodeFODF1280 = "FODF1280" + errCodeFODF1310 = "FODF1310" + errCodeFOFD1340 = "FOFD1340" + errCodeFOFD1350 = "FOFD1350" + errCodeXTDE1280 = "XTDE1280" + errCodeXTDE1310 = "XTDE1310" + errCodeXTDE1340 = "XTDE1340" + errCodeXTDE1350 = "XTDE1350" +) + +// XPath function local names used by XSLT expression/pattern handling, distinct from identically spelled XML attributes. +const ( + fnNameID = "id" +) + // XSLT/XPath function names referenced from multiple sites. Kept as constants // alongside lookupXSLTFunctionArity (the single source of truth for arities) so the // repeated literals stay consistent across the package. @@ -50,7 +76,7 @@ func (ec *execContext) xsltFunctions() map[string]xpath.Function { ec.cachedFns = map[string]xpath.Function{ fnNameCurrent: &xsltFunc{min: 0, max: 0, fn: ec.fnCurrent, noDynRef: true, dynRefError: errCodeXTDE1360}, fnNameDocument: &xsltFunc{min: 1, max: 2, fn: ec.fnDocument}, - nameKey2: &keyFunction{ec: ec, scope: ec.effectiveStylesheet()}, + keyLocalName: &keyFunction{ec: ec, scope: ec.effectiveStylesheet()}, lexicon.FnGenerateID: xpath.HostBuiltin(ec.stylesheet.xpathVersion(), lexicon.FnGenerateID), funcSystemProperty: newIntrospectionFunc(funcSystemProperty, ec.fnSystemProperty), nameUnparsedEntityURI: &xsltFunc{min: 1, max: 2, fn: ec.fnUnparsedEntityURI}, @@ -70,19 +96,19 @@ func (ec *execContext) xsltFunctions() map[string]xpath.Function { nameStreamAvailable: &xsltFunc{min: 1, max: 1, fn: ec.fnStreamAvailable}, fnNameCurrentOutputURI: &xsltFunc{min: 0, max: 0, fn: ec.fnCurrentOutputURI}, } - if ec.stylesheet.processorLanguage() <= 20 { - for _, name := range []string{lexicon.FnFormatNumber, "format-date", "format-dateTime", "format-time"} { + if ec.stylesheet.processorLanguage() <= processorXSLT20 { + for _, name := range []string{lexicon.FnFormatNumber, fnNameFormatDate, fnNameFormatDateTime, fnNameFormatTime} { ec.cachedFns[name] = newLegacyFormatFunction(ec.stylesheet.xpathVersion(), name) } } - if ec.stylesheet.processorLanguage() == 20 { - for _, name := range []string{"unparsed-text", fnNameUnparsedTextAvailable} { + if ec.stylesheet.processorLanguage() == processorXSLT20 { + for _, name := range []string{fnNameUnparsedText, fnNameUnparsedTextAvailable} { ec.cachedFns[name] = newLegacyTextFunction(name) } } - if ec.stylesheet.processorLanguage() == 10 { + if ec.stylesheet.processorLanguage() == processorXSLT10 { for name, fn := range ec.cachedFns { if _, ok := lookupXSLT10FunctionArity(name); ok { ec.cachedFns[name] = function10{name, fn} @@ -92,7 +118,7 @@ func (ec *execContext) xsltFunctions() map[string]xpath.Function { } } - if ec.stylesheet.processorLanguage() == 20 { + if ec.stylesheet.processorLanguage() == processorXSLT20 { for name := range ec.cachedFns { if xslt30Function(name) { delete(ec.cachedFns, name) @@ -111,7 +137,13 @@ func (ec *execContext) xsltFunctions() map[string]xpath.Function { func (ec *execContext) prepareHostFunction(name string, function *xsltFunc) { function.name = name - if ec.stylesheet.processorLanguage() < 30 && (name == nameUnparsedEntityURI || name == nameUnparsedEntityPublicID) { + switch name { + case nameDoc, fnNameDocument, fnNameTransform, nameStreamAvailable: + function.resourceOwner = ec + function.resourceScope = ec.effectiveStylesheet() + } + + if ec.stylesheet.processorLanguage() < processorXSLT30 && (name == nameUnparsedEntityURI || name == nameUnparsedEntityPublicID) { function.max = 1 } @@ -510,7 +542,7 @@ func (ec *execContext) retrieveDocumentReader(ctx context.Context, uri string) ( reader, err := openDocumentVia(ctx, uri, ec.transformConfig.uriResolver) if err != nil { - if !nilcheck.IsNil(reader) { + if reader != nil && !nilcheck.IsNil(reader) { err = errors.Join(err, reader.Close()) } @@ -620,7 +652,7 @@ func (f *legacyFormatFunction) Call(ctx context.Context, args []xdm.Sequence) (x } if !xmlchar.IsValidQName(name) { - return nil, dynamicError("XTDE1280", "invalid decimal format QName %q", name) + return nil, dynamicError(errCodeXTDE1280, "invalid decimal format QName %q", name) } } @@ -633,14 +665,14 @@ func (f *legacyFormatFunction) Call(ctx context.Context, args []xdm.Sequence) (x code := "" switch detail.Code { - case "FODF1280": - code = "XTDE1280" - case "FODF1310": - code = "XTDE1310" - case "FOFD1340": - code = "XTDE1340" - case "FOFD1350": - code = "XTDE1350" + case errCodeFODF1280: + code = errCodeXTDE1280 + case errCodeFODF1310: + code = errCodeXTDE1310 + case errCodeFOFD1340: + code = errCodeXTDE1340 + case errCodeFOFD1350: + code = errCodeXTDE1350 } if code != "" { @@ -795,7 +827,7 @@ func lookupXSLTFunctionArity(key string) ([2]int, bool) { return [2]int{1, 2}, true case nameDoc, funcSystemProperty, fnNameElementAvailable, fnNameTypeAvailable, nameAccumulatorBefore, nameAccumulatorAfter, nameRegexGroup, fnNameTransform, nameStreamAvailable: return [2]int{1, 1}, true - case nameKey2: + case keyLocalName: return [2]int{2, 3}, true } @@ -805,7 +837,7 @@ func lookupXSLTFunctionArity(key string) ([2]int, bool) { } func xsltFunctionAcceptsVersionArity(version int, name string, arity int) bool { - if version < 30 && (name == nameUnparsedEntityURI || name == nameUnparsedEntityPublicID) && arity > 1 { + if version < processorXSLT30 && (name == nameUnparsedEntityURI || name == nameUnparsedEntityPublicID) && arity > 1 { return false } @@ -824,13 +856,15 @@ func xsltFunctionAcceptsArity(name string, arity int) bool { } type xsltFunc struct { - name string - min int - max int - fn func(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) - noDynRef bool // true if the function must not be called via dynamic function reference - dynRefError string // error code to raise on dynamic call - signature xpath.TypedFunctionByArity + name string + min int + max int + fn func(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) + noDynRef bool // true if the function must not be called via dynamic function reference + dynRefError string // error code to raise on dynamic call + signature xpath.TypedFunctionByArity + resourceOwner *execContext + resourceScope *Stylesheet } func (f *xsltFunc) FuncParamTypesForArity(arity int) []xdm.SequenceType { @@ -854,9 +888,43 @@ func (f *xsltFunc) MinArity() int { return f.min } func (f *xsltFunc) MaxArity() int { return f.max } func (f *xsltFunc) Call(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { + if f.resourceOwner != nil { + ec, err := functionExecution(ctx, f.resourceOwner) + if err != nil { + return nil, err + } + + ctx = withExecContext(ctx, ec) + defer ec.pushPackage(f.resourceScope)() + + switch f.name { + case nameDoc: + return ec.fnDoc(ctx, args) + case fnNameDocument: + return ec.fnDocument(ctx, args) + case fnNameTransform: + return ec.fnTransform(ctx, args) + case nameStreamAvailable: + return ec.fnStreamAvailable(ctx, args) + } + } + return f.fn(ctx, args) } +// BindFunctionReference captures lexical state while XPath retains the static +// context of this reference. Runtime resources and caches come from its caller. +func (f *xsltFunc) BindFunctionReference() xpath.Function { + if f.resourceOwner == nil { + return f + } + + bound := *f + bound.resourceOwner = f.resourceOwner.captureFunctionContext() + + return &bound +} + // NoDynamicRef returns true if this function should not be available // for named function references (e.g. current-group#0). func (f *xsltFunc) NoDynamicRef() bool { return f.noDynRef } @@ -871,6 +939,10 @@ type introspectionFunc struct { result xdm.SequenceType } +// BindFunctionReference keeps this wrapper's specific type metadata. Its +// introspection closure already retains the static context it describes. +func (f *introspectionFunc) BindFunctionReference() xpath.Function { return f } + func (f *introspectionFunc) FuncParamTypesForArity(arity int) []xdm.SequenceType { return f.params[:arity] } @@ -882,34 +954,23 @@ func newIntrospectionFunc(name string, call func(context.Context, []xdm.Sequence return xdm.SequenceType{ItemTest: xdm.AtomicOrUnionType{Name: "Q{" + lexicon.NamespaceXSD + "}" + local}} } - params := []xdm.SequenceType{atomic("string")} + params := []xdm.SequenceType{atomic(lexicon.TypeString)} if name == fnNameFunctionAvailable { - params = append(params, atomic("integer")) + params = append(params, atomic(lexicon.TypeInteger)) } - result := atomic("boolean") + result := atomic(lexicon.TypeBoolean) if name == funcSystemProperty { - result = atomic("string") + result = atomic(lexicon.TypeString) } return &introspectionFunc{xsltFunc: &xsltFunc{min: 1, max: len(params), fn: call}, params: params, result: result} } -// regexGroupFunc implements xpath.Function and DynamicRefSnapshotProvider -// for regex-group(). Dynamic references clear the captured-substring context. -// resolveAgainstBaseURI resolves a relative URI against an effective base URI. -// The base URI may be a file path (e.g., /a/b/style.xsl) or a directory-like -// path from xml:base processing (e.g., /a/b). For file paths (containing a -// dot-extension in the last segment), the directory part is extracted via -// filepath.Dir. For directory-like paths, the path is used directly. -// -// Absoluteness is decided with [uripath.URIScheme] (RFC 3986), not filepath.IsAbs -// or a "://" substring check: an absolute-URI reference may carry a scheme with -// no "//" authority (e.g. "urn:shared", "file:/docs/d.xml") and must be returned -// unchanged, while a relative reference against a URI base must keep the base -// scheme/authority. Only when both base and ref are local filesystem paths is -// filepath.Join used. Resolution of the URI cases is delegated to the shared -// canonical [uripath.ResolveSchemaURI] helper. +// resolveAgainstBaseURI resolves URI references with uripath.ResolveSchemaURI. +// If both arguments are local paths, it joins them with portable slash semantics, +// treating a base whose last segment contains a dot as a file. Absolute references +// remain unchanged; a failed URI resolution returns the original reference. func resolveAgainstBaseURI(uri string, baseURI string) string { if uri == "" || baseURI == "" { return uri diff --git a/transform/internal/xslt/functions_introspect.go b/transform/internal/xslt/functions_introspect.go index e47e0c0..038aec8 100644 --- a/transform/internal/xslt/functions_introspect.go +++ b/transform/internal/xslt/functions_introspect.go @@ -34,7 +34,7 @@ func (ec *execContext) fnFunctionAvailable(ctx context.Context, args []xdm.Seque } func (ec *execContext) functionAvailable(ctx context.Context, name xdm.QNameValue, arity int) bool { - if ec.stylesheet.processorLanguage() == 10 { + if ec.stylesheet.processorLanguage() == processorXSLT10 { if name.URI != xpath.NSFn { function := xpath.FnRegisteredFunction(ctx, name.URI, name.Local) return function != nil && functionAvailableAtArity(function, arity) @@ -76,7 +76,7 @@ func (ec *execContext) fnTypeAvailable(ctx context.Context, args []xdm.Sequence) // current-group() returns the items in the current group during for-each-group. func (ec *execContext) fnCurrentGroup(ctx context.Context, _ []xdm.Sequence) (xdm.Sequence, error) { if !ec.inGroupContext || xpath.IsDynamicCall(ctx) { - if ec.stylesheet.processorLanguage() < 30 { + if ec.stylesheet.processorLanguage() < processorXSLT30 { return xdm.EmptySequence(), nil } @@ -96,7 +96,7 @@ func (ec *execContext) fnCurrentGroup(ctx context.Context, _ []xdm.Sequence) (xd // not establish a grouping key), or when called outside xsl:for-each-group. func (ec *execContext) fnCurrentGroupingKey(ctx context.Context, _ []xdm.Sequence) (xdm.Sequence, error) { if !ec.inGroupContext || xpath.IsDynamicCall(ctx) { - if ec.stylesheet.processorLanguage() < 30 { + if ec.stylesheet.processorLanguage() < processorXSLT30 { return xdm.EmptySequence(), nil } @@ -104,7 +104,7 @@ func (ec *execContext) fnCurrentGroupingKey(ctx context.Context, _ []xdm.Sequenc } if !ec.groupHasKey { - if ec.stylesheet.processorLanguage() < 30 { + if ec.stylesheet.processorLanguage() < processorXSLT30 { return xdm.EmptySequence(), nil } @@ -264,7 +264,8 @@ func (ec *execContext) checkAccumulatorAccess(name string) error { // so that an accumulator rule referencing a non-applicable peer // (e.g. one not listed in use-accumulators) is rejected. if ec.activeAccumulators != nil { - if _, ok := ec.activeAccumulators[name]; !ok { + _, all := ec.activeAccumulators[lexicon.ModeAll] + if _, ok := ec.activeAccumulators[name]; !ok && !all { return dynamicError(errCodeXTDE3362, "accumulator %q is not applicable in the current source-document", name) } } @@ -302,6 +303,10 @@ func (ec *execContext) checkAccumulatorAccess(name string) error { // ensureAccumulatorStates lazily computes accumulator states for the document // tree containing the given node, if not already computed. func (ec *execContext) ensureAccumulatorStates(ctx context.Context, node xdm.Node) error { + const ( + accumulatorDocumentPackageFormat = "%p@%p" + ) + if xdm.IsNilNode(node) || len(ec.effectiveAccumulators()) == 0 { return nil } @@ -317,7 +322,7 @@ func (ec *execContext) ensureAccumulatorStates(ctx context.Context, node xdm.Nod if ec.currentPackage != nil && ec.currentPackage != ec.stylesheet { // Create a synthetic key node per package to avoid collision. // We reuse the root but track per-package with a separate map entry. - pkgKey := fmt.Sprintf("%p@%p", root, ec.currentPackage) + pkgKey := fmt.Sprintf(accumulatorDocumentPackageFormat, root, ec.currentPackage) if ec.accumulatorComputedPkgs == nil { ec.accumulatorComputedPkgs = make(map[string]struct{}) } @@ -364,7 +369,7 @@ func introspectionArgumentName(ctx context.Context, args []xdm.Sequence, default return xdm.QNameValue{}, err } - if processor < 30 && !xmlchar.IsValidQName(name) { + if processor < processorXSLT30 && !xmlchar.IsValidQName(name) { return xdm.QNameValue{}, dynamicError(code, "%q is not a valid QName", name) } @@ -380,15 +385,15 @@ func elementAvailable(name xdm.QNameValue, processor int, disabled bool) bool { return false } - if processor < 30 && !isXSLTInstruction(name.Local) { + if processor < processorXSLT30 && !isXSLTInstruction(name.Local) { return false } switch minXSLTElementVersion(name.Local) { - case "3.0": - return processor >= 30 - case "2.0": - return processor >= 20 + case lexicon.XSLTVersion30: + return processor >= processorXSLT30 + case lexicon.XSLTVersion20: + return processor >= processorXSLT20 default: return true } @@ -432,11 +437,11 @@ func introspectionTypeAvailable(ctx context.Context, args []xdm.Sequence, proces } func typeAvailable(name xdm.QNameValue, processor int) bool { - if name.URI != lexicon.NamespaceXSD || !xdm.IsKnownXSDType("xs:"+name.Local) { + if name.URI != lexicon.NamespaceXSD || !xdm.IsKnownXSDType(lexicon.PrefixXS+":"+name.Local) { return false } - if name.Local == lexicon.TypeNumeric && processor < 30 { + if name.Local == lexicon.TypeNumeric && processor < processorXSLT30 { return false } diff --git a/transform/internal/xslt/functions_key.go b/transform/internal/xslt/functions_key.go index 46d3a02..46afab1 100644 --- a/transform/internal/xslt/functions_key.go +++ b/transform/internal/xslt/functions_key.go @@ -17,6 +17,16 @@ import ( "github.com/skarm/xmlsec/transform/internal/xpath" ) +// Local names of system-property and available-system-properties QNames in the XSLT namespace; output serialization parameters use their own vocabulary. +const ( + systemPropertyProductVersion = "product-version" + systemPropertyVendor = "vendor" + systemPropertyVendorURL = "vendor-url" + systemPropertyVersion = "version" + systemPropertyXPathVersion = "xpath-version" + systemPropertyXSDVersion = "xsd-version" +) + type keyFunction struct { ec *execContext scope *Stylesheet @@ -27,11 +37,11 @@ func (f *keyFunction) MinArity() int { return 2 } func (f *keyFunction) MaxArity() int { return 3 } func (f *keyFunction) FuncParamTypesForArity(arity int) []xdm.SequenceType { - return xsltFunctionTypes()[functionTypeKey{nameKey2, arity}].params + return xsltFunctionTypes()[functionTypeKey{keyLocalName, arity}].params } func (f *keyFunction) FuncReturnTypeForArity(arity int) *xdm.SequenceType { - return xsltFunctionTypes()[functionTypeKey{nameKey2, arity}].result + return xsltFunctionTypes()[functionTypeKey{keyLocalName, arity}].result } func (f *keyFunction) BindFunctionReference() xpath.Function { @@ -157,11 +167,14 @@ func (ec *execContext) fnSystemProperty(ctx context.Context, args []xdm.Sequence return introspectionSystemProperty(ctx, args, ec.stylesheet.processorLanguage(), ec.stylesheet.dynamicEvaluationDisabled) } -// available-system-properties() returns a sequence of QNames for all -// available system properties in the XSLT namespace. -// fnStreamAvailable implements fn:stream-available — returns true when the -// URI identifies a resource that can be streamed. +// fnStreamAvailable implements XSLT stream-available(): it checks read policy +// and the resource prolog without loading a complete source tree. A true result +// does not imply W3C streamable execution support. func (ec *execContext) fnStreamAvailable(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { + const ( + errCodeFODC0005 = "FODC0005" + ) + if xdm.SequenceLen(args[0]) == 0 { return xdm.SingleBoolean(false), nil } @@ -178,7 +191,11 @@ func (ec *execContext) fnStreamAvailable(ctx context.Context, args []xdm.Sequenc resolved, err := xpath.ResolveDocumentURI(ctx, uri) if err != nil { - if detail, ok := errors.AsType[*xdm.Error](err); ok && detail.Code == "FODC0005" { + if failure := preserveControlFailure(ctx, err); failure != nil { + return nil, failure + } + + if detail, ok := errors.AsType[*xdm.Error](err); ok && detail.Code == errCodeFODC0005 { return nil, err } @@ -196,6 +213,10 @@ func (ec *execContext) fnStreamAvailable(ctx context.Context, args []xdm.Sequenc reader, err := ec.retrieveDocumentReader(ctx, resolved) if err != nil { + if failure := preserveControlFailure(ctx, err); failure != nil { + return nil, failure + } + if _, hostFailure := errors.AsType[*xdm.ResourceAccessError](err); hostFailure { return nil, err } @@ -203,9 +224,18 @@ func (ec *execContext) fnStreamAvailable(ctx context.Context, args []xdm.Sequenc return xdm.SingleBoolean(false), nil } - options := ec.inputParseOptions().WithByteLimit(ec.resourceLimit()) + var options xdm.ParseOptions + if configured := ec.inputParseOptions(); configured != nil { + options = *configured + } + + options = options.WithByteLimit(ec.resourceLimit()) err = options.CheckXMLProlog(ctx, reader) + err = errors.Join(err, reader.Close()) + if failure := preserveControlFailure(ctx, err); failure != nil { + return nil, failure + } return xdm.SingleBoolean(err == nil), nil } @@ -224,12 +254,12 @@ func (ec *execContext) fnCurrentOutputURI(ctx context.Context, _ []xdm.Sequence) // unparsed-entity-uri(name [, doc]) returns the URI of an unparsed entity. func (ec *execContext) fnUnparsedEntityURI(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { - return ec.unparsedEntity(ctx, args, "unparsed-entity-uri", errCodeXTDE1370) + return ec.unparsedEntity(ctx, args, nameUnparsedEntityURI, errCodeXTDE1370) } // unparsed-entity-public-id(name) returns the public identifier of an unparsed entity. func (ec *execContext) fnUnparsedEntityPublicID(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { - return ec.unparsedEntity(ctx, args, "unparsed-entity-public-id", errCodeXTDE1380) + return ec.unparsedEntity(ctx, args, nameUnparsedEntityPublicID, errCodeXTDE1380) } func documentRoot(node xdm.Node) xdm.Node { @@ -266,7 +296,7 @@ func keyArgumentName(ctx context.Context, arg xdm.Sequence, processor int) (stri } } - if processor < 30 && !xmlchar.IsValidQName(name) { + if processor < processorXSLT30 && !xmlchar.IsValidQName(name) { return "", dynamicError(errCodeXTDE1260, "invalid key name %q", name) } @@ -306,21 +336,37 @@ func introspectionSystemProperty(ctx context.Context, args []xdm.Sequence, proce } func systemPropertyValue(ctx context.Context, local string, processor int) xdm.Sequence { + const ( + processorMajorVersionFormat = "%d.0" + processorProductVersion = "0.1" + processorVendorURL = "https://github.com/skarm/xmlsec" + ) + + // Text advertised by the version properties is distinct from encoded selector + // integers and from XML output declaration versions. + const ( + xpathVersion10Text = "1.0" + xpathVersion20Text = "2.0" + xpathVersion31Text = "3.1" + xmlSchemaVersion10Text = "1.0" + xmlSchemaVersion11Text = "1.1" + ) + switch local { - case lexicon.SerializationVersion: - if processor == 10 { - return xdm.SingleDouble(1) + case systemPropertyVersion: + if processor == processorXSLT10 { + return xdm.SingleDouble(xsltMajorVersion1) } - return xdm.SingleString(fmt.Sprintf("%d.0", processor/10)) - case "vendor": + return xdm.SingleString(fmt.Sprintf(processorMajorVersionFormat, processor/processorVersionScale)) + case systemPropertyVendor: return xdm.SingleString(processorProductName) - case "vendor-url": - return xdm.SingleString("https://github.com/skarm/xmlsec") + case systemPropertyVendorURL: + return xdm.SingleString(processorVendorURL) case nameProductName: return xdm.SingleString(processorProductName) - case "product-version": - return xdm.SingleString("0.1") + case systemPropertyProductVersion: + return xdm.SingleString(processorProductVersion) case nameIsSchemaAware: return xdm.SingleString(lexicon.ValueNo) case nameSupportsSerialization: @@ -339,33 +385,37 @@ func systemPropertyValue(ctx context.Context, local string, processor int) xdm.S return xdm.SingleString(lexicon.ValueYes) case textSupportsHigherOrderFunctions: return xdm.SingleString(lexicon.ValueYes) - case "xpath-version": - return xdm.SingleString(map[int]string{10: lexicon.XSLTVersion10, 20: lexicon.XSLTVersion20, 30: "3.1"}[processor]) - case "xsd-version": - if xpath.XMLSchemaVersion(ctx) == 10 { - return xdm.SingleString(lexicon.XSLTVersion10) + case systemPropertyXPathVersion: + return xdm.SingleString(map[int]string{processorXSLT10: xpathVersion10Text, processorXSLT20: xpathVersion20Text, processorXSLT30: xpathVersion31Text}[processor]) + case systemPropertyXSDVersion: + if xpath.XMLSchemaVersion(ctx) == xmlSchemaProfile10 { + return xdm.SingleString(xmlSchemaVersion10Text) } - return xdm.SingleString("1.1") + return xdm.SingleString(xmlSchemaVersion11Text) default: return xdm.SingleString("") } } func fnAvailableSystemProperties(_ context.Context, _ []xdm.Sequence) (xdm.Sequence, error) { + const ( + systemPropertyQNamePrefix = "xsl" + ) + props := []string{ - lexicon.SerializationVersion, "vendor", "vendor-url", nameProductName, "product-version", + systemPropertyVersion, systemPropertyVendor, systemPropertyVendorURL, nameProductName, systemPropertyProductVersion, nameIsSchemaAware, nameSupportsSerialization, textSupportsBackwardsCompatibility, nameSupportsNamespaceAxis, nameSupportsStreaming, nameSupportsDynamicEvaluation, - textSupportsHigherOrderFunctions, "xpath-version", "xsd-version", + textSupportsHigherOrderFunctions, systemPropertyXPathVersion, systemPropertyXSDVersion, } seq := make(xdm.ItemSlice, 0, len(props)) for _, p := range props { seq = append(seq, xdm.AtomicValue{ TypeName: xdm.TypeQName, - Value: xdm.QNameValue{Prefix: "xsl", URI: lexicon.NamespaceXSLT, Local: p}, + Value: xdm.QNameValue{Prefix: systemPropertyQNamePrefix, URI: lexicon.NamespaceXSLT, Local: p}, }) } @@ -386,7 +436,7 @@ func (ec *execContext) unparsedEntity(ctx context.Context, args []xdm.Sequence, item, err = xpath.FnContextItem(ctx) if err != nil { - if ec.stylesheet.processorLanguage() < 30 { + if ec.stylesheet.processorLanguage() < processorXSLT30 { return nil, dynamicError(errorCode, "%s requires a context node", functionName) } @@ -396,7 +446,7 @@ func (ec *execContext) unparsedEntity(ctx context.Context, args []xdm.Sequence, node, ok := item.(xdm.NodeItem) if !ok { - if ec.stylesheet.processorLanguage() < 30 { + if ec.stylesheet.processorLanguage() < processorXSLT30 { return nil, dynamicError(errorCode, "%s requires a context node", functionName) } @@ -441,8 +491,18 @@ func (ec *execContext) unparsedEntity(ctx context.Context, args []xdm.Sequence, // documentNodeFromNode walks up to the root of a node's tree and returns // the document node if the root is a document, or nil otherwise. func documentNodeFromNode(n xdm.Node) *xdm.Document { - for n.Parent() != nil { - n = n.Parent() + n = normalizeNode(n) + if n == nil { + return nil + } + + for { + parent := normalizeNode(n.Parent()) + if parent == nil { + break + } + + n = parent } if doc, ok := n.(*xdm.Document); ok { diff --git a/transform/internal/xslt/functions_transform.go b/transform/internal/xslt/functions_transform.go index 6ceb6fa..10c8e8b 100644 --- a/transform/internal/xslt/functions_transform.go +++ b/transform/internal/xslt/functions_transform.go @@ -8,6 +8,7 @@ import ( "context" "errors" "io" + "maps" "path" "github.com/skarm/xmlsec/internal/lexicon" @@ -17,6 +18,36 @@ import ( "github.com/skarm/xmlsec/transform/internal/xpath" ) +// String keys of the fn:transform options map; required-type conversion and present-versus-empty semantics are enforced separately. +const ( + transformOptionBaseOutputURI = "base-output-uri" + transformOptionDeliveryFormat = "delivery-format" + transformOptionEnableAssertions = "enable-assertions" + transformOptionFunctionParams = "function-params" + transformOptionGlobalContextItem = "global-context-item" + transformOptionInitialFunction = "initial-function" + transformOptionInitialMatchSelection = "initial-match-selection" + transformOptionInitialMode = "initial-mode" + transformOptionInitialTemplate = "initial-template" + transformOptionPackageName = "package-name" + transformOptionPackageVersion = "package-version" + transformOptionPostProcess = "post-process" + transformOptionRequestedProperties = "requested-properties" + transformOptionSerializationParams = "serialization-params" + transformOptionSourceNode = "source-node" + transformOptionStaticParams = "static-params" + transformOptionStylesheetBaseURI = "stylesheet-base-uri" + transformOptionStylesheetLocation = "stylesheet-location" + transformOptionStylesheetNode = "stylesheet-node" + transformOptionStylesheetParams = "stylesheet-params" + transformOptionStylesheetText = "stylesheet-text" + transformOptionTemplateParams = "template-params" + transformOptionTunnelParams = "tunnel-params" + transformOptionXSLTVersion = "xslt-version" +) + +// Hard ceiling on active fn:transform calls in the inherited invocation context; +// cumulative resource and program budgets remain independent. const maxTransformDepth = 10 type transformProgramLimitKey struct{} @@ -122,7 +153,7 @@ func TransformFunction(options ...TransformOption) xpath.TypedFunctionByArity { cfg.nestedCompiler = c - return &xsltFunc{min: 1, max: 1, fn: cfg.run, name: fnNameTransform, signature: xpath.HostBuiltin(31, fnNameTransform)} + return &xsltFunc{min: 1, max: 1, fn: cfg.run, name: fnNameTransform, signature: xpath.HostBuiltin(xpathProfile31, fnNameTransform)} } // paramKeyToClark converts an XPath map key to the Clark notation string @@ -162,17 +193,17 @@ func (ec *execContext) xsltFunctionsNS() map[xpath.QualifiedName]xpath.Function ec.cachedFnsNS[xpath.QualifiedName{URI: xpath.NSFn, Name: nameStreamAvailable}] = &xsltFunc{min: 1, max: 1, fn: ec.fnStreamAvailable} ec.cachedFnsNS[xpath.QualifiedName{URI: xpath.NSFn, Name: nameUnparsedEntityURI}] = &xsltFunc{min: 1, max: 2, fn: ec.fnUnparsedEntityURI} ec.cachedFnsNS[xpath.QualifiedName{URI: xpath.NSFn, Name: nameUnparsedEntityPublicID}] = &xsltFunc{min: 1, max: 2, fn: ec.fnUnparsedEntityPublicID} - ec.cachedFnsNS[xpath.QualifiedName{URI: xpath.NSFn, Name: nameKey2}] = &keyFunction{ec: ec, scope: ec.effectiveStylesheet()} + ec.cachedFnsNS[xpath.QualifiedName{URI: xpath.NSFn, Name: keyLocalName}] = &keyFunction{ec: ec, scope: ec.effectiveStylesheet()} ec.cachedFnsNS[xpath.QualifiedName{URI: xpath.NSFn, Name: lexicon.FnGenerateID}] = xpath.HostBuiltin(ec.stylesheet.xpathVersion(), lexicon.FnGenerateID) - if ec.stylesheet.processorLanguage() <= 20 { - for _, name := range []string{lexicon.FnFormatNumber, "format-date", "format-dateTime", "format-time"} { + if ec.stylesheet.processorLanguage() <= processorXSLT20 { + for _, name := range []string{lexicon.FnFormatNumber, fnNameFormatDate, fnNameFormatDateTime, fnNameFormatTime} { ec.cachedFnsNS[xpath.QualifiedName{URI: xpath.NSFn, Name: name}] = newLegacyFormatFunction(ec.stylesheet.xpathVersion(), name) } } - if ec.stylesheet.processorLanguage() == 20 { - for _, name := range []string{"unparsed-text", fnNameUnparsedTextAvailable} { + if ec.stylesheet.processorLanguage() == processorXSLT20 { + for _, name := range []string{fnNameUnparsedText, fnNameUnparsedTextAvailable} { ec.cachedFnsNS[xpath.QualifiedName{URI: xpath.NSFn, Name: name}] = newLegacyTextFunction(name) } } @@ -194,8 +225,8 @@ func (ec *execContext) xsltFunctionsNS() map[xpath.QualifiedName]xpath.Function // Dynamic lookup sees declarations in the calling package, excluding // abstract declarations and components accepted from other packages. - if ec.stylesheet.processorLanguage() >= 30 { - ec.cachedFnsNS[xpath.QualifiedName{URI: xpath.NSFn, Name: "function-lookup"}] = &xsltFunc{min: 2, max: 2, fn: ec.fnFunctionLookupPackage} + if ec.stylesheet.processorLanguage() >= processorXSLT30 { + ec.cachedFnsNS[xpath.QualifiedName{URI: xpath.NSFn, Name: fnNameFunctionLookup}] = &xsltFunc{min: 2, max: 2, fn: ec.fnFunctionLookupPackage} } if ec.currentPackage != nil { @@ -211,7 +242,7 @@ func (ec *execContext) xsltFunctionsNS() map[xpath.QualifiedName]xpath.Function } } - if ec.stylesheet.processorLanguage() == 20 { + if ec.stylesheet.processorLanguage() == processorXSLT20 { for name := range ec.cachedFnsNS { if name.URI == xpath.NSFn && xslt30Function(name.Name) { delete(ec.cachedFnsNS, name) @@ -382,12 +413,17 @@ func (ec *execContext) xsltEvaluateFunctions() map[string]xpath.Function { func (ec *execContext) fnTransform(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { ctx = ec.xpathContext(ctx) + baseURI := ec.effectiveStaticBaseURI() + if captured, present := xpath.ContextBaseURI(ctx); present { + baseURI = captured + } + cfg := &transformFnConfig{ // The static base URI of the fn:transform CALL — used to resolve a // relative stylesheet-location, stylesheet-base-uri, and base-output-uri — // is the effective static base URI at the call site (honoring an xml:base // on the calling template/element), NOT the bare module URI. - baseURI: ec.effectiveStaticBaseURI(), + baseURI: baseURI, nestedCompiler: ec.stylesheet.newNestedCompiler().MaxResourceBytes(ec.resourceLimit()), stylesheetResolver: ec.stylesheet.uriResolver, packageResolver: ec.stylesheet.packageResolver, @@ -452,7 +488,13 @@ func (ss *Stylesheet) newNestedCompiler() Compiler { type transformDepthKey struct{} -const transformSerialized = "serialized" +// Delivery-format selectors and the default principal-result map key belong to +// fn:transform, independently of document() and the xsl:output declaration. +const ( + transformSerialized = "serialized" + transformDeliveryDocument = "document" + transformPrimaryResultKey = "output" +) // transformFnConfig carries the resolvers, resource caps and XML limits for // fn:transform. It @@ -486,6 +528,8 @@ type transformFnConfig struct { // executes an XSLT stylesheet from the options map, using the resolvers and // resource policy carried on cfg. func (cfg *transformFnConfig) run(ctx context.Context, args []xdm.Sequence) (xdm.Sequence, error) { + config := cfg.invocationConfig(ctx) + depth, _ := ctx.Value(transformDepthKey{}).(int) if depth >= maxTransformDepth { return nil, dynamicErrorCause(errCodeFOXT0004, xpath.ErrBudget, "fn:transform: maximum recursion depth (%d) exceeded", maxTransformDepth) @@ -502,28 +546,33 @@ func (cfg *transformFnConfig) run(ctx context.Context, args []xdm.Sequence) (xdm return nil, dynamicError(lexicon.ErrXPTY0004, "fn:transform argument must be a map") } - request := newTransformRequest(options, cfg.baseURI) + options, err := convertTransformOptions(ctx, options) + if err != nil { + return nil, err + } + + request := newTransformRequest(options, config.baseURI) if err := validateTransformOptions(ctx, options, request.deliveryFormat); err != nil { return nil, err } - assertions, err := transformBooleanOption(ctx, options, "enable-assertions") + assertions, err := transformBooleanOption(ctx, options, transformOptionEnableAssertions) if err != nil { return nil, err } ctx = WithAssertions(ctx, assertions) - compiler, staticParams := transformStaticParameters(cfg.nestedCompiler, transformMapSeq(options, "static-params")) + compiler, staticParams := transformStaticParameters(config.nestedCompiler, transformMapSeq(options, transformOptionStaticParams)) processor, err := transformProcessorVersion(options) if err != nil { return nil, err } - compiler = compiler.ProcessorVersion(processor).MaxResourceBytes(cfg.programLimit(ctx)) + compiler = compiler.ProcessorVersion(processor).MaxResourceBytes(config.programLimit(ctx)) - stylesheet, err := cfg.compileTransformStylesheet(ctx, compiler, request) + stylesheet, err := config.compileTransformStylesheet(ctx, compiler, request) if err != nil { return nil, err } @@ -532,7 +581,7 @@ func (cfg *transformFnConfig) run(ctx context.Context, args []xdm.Sequence) (xdm return nil, dynamicError(errCodeFOXT0002, "fn:transform: no stylesheet specified (stylesheet-location, stylesheet-node, stylesheet-text, or package-name required)") } - invocation := request.newInvocation(ctx, cfg, staticParams) + invocation := request.newInvocation(ctx, config, staticParams) source := request.prepareSource(invocation) resultDoc, err := executeTransform(ctx, source, stylesheet, invocation) @@ -564,11 +613,11 @@ func (cfg *transformFnConfig) compileTransformStylesheet(ctx context.Context, co return cfg.compileTransformPackage(ctx, compiler, request.packageName, request.packageVersion) } - if text := transformStringOption(request.options, "stylesheet-text"); text != "" { + if text := transformStringOption(request.options, transformOptionStylesheetText); text != "" { return cfg.compileTransformText(ctx, compiler, text, request.stylesheetBaseURI) } - return cfg.compileTransformNode(ctx, compiler, transformMapSeq(request.options, "stylesheet-node"), request.stylesheetBaseURI) + return cfg.compileTransformNode(ctx, compiler, transformMapSeq(request.options, transformOptionStylesheetNode), request.stylesheetBaseURI) } func (cfg *transformFnConfig) programLimit(ctx context.Context) int64 { @@ -770,7 +819,7 @@ func transformStaticParameters(nestedCompiler Compiler, staticParamsSeq xdm.Sequ // fn:transform options map, or ok=false when the option is absent or is not a // function item. func transformPostProcess(m xdm.MapItem) (xdm.FunctionItem, bool) { - seq := transformMapSeq(m, "post-process") + seq := transformMapSeq(m, transformOptionPostProcess) if !transformOptionPresent(seq) { return xdm.FunctionItem{}, false } @@ -795,7 +844,7 @@ func applyTransformPostProcess(ctx context.Context, fi xdm.FunctionItem, result value, _ := result.Get(key) uri := baseOutputURI - if s, err := xpath.AtomicToString(key); err == nil && s != nameOutput { + if s, err := xpath.AtomicToString(key); err == nil && s != transformPrimaryResultKey { uri = s } @@ -828,6 +877,122 @@ func lookupTransformCapability(ctx context.Context, key string) (bool, bool) { return zero, false } +// transformOptionType records the F&O 3.1 section 16.3.2 required types. +// Unknown keys remain implementation extensions and are not standard aliases. +func transformOptionType(name string) string { + const ( + transformAtomicSequenceMapType = "map(xs:anyAtomicType, item()*)" + transformDecimalType = lexicon.PrefixXS + ":" + lexicon.TypeDecimal + transformItemSequenceType = lexicon.NodeTestItem + "*" + transformOptionEnableMessages = "enable-messages" + transformOptionEnableTrace = "enable-trace" + transformOptionPackageLocation = "package-location" + transformOptionPackageNode = "package-node" + transformOptionPackageText = "package-text" + transformOptionVendorOptions = "vendor-options" + transformPostProcessType = "function(xs:string, item()*) as item()*" + transformQNameAtomicMapType = "map(xs:QName, xs:anyAtomicType)" + transformQNameSequenceMapType = "map(xs:QName, item()*)" + transformQNameType = lexicon.PrefixXS + ":" + lexicon.TypeQName + transformSequenceArrayType = "array(item()*)" + ) + + const ( + transformOptionCache = "cache" + ) + + switch name { + case transformOptionBaseOutputURI, transformOptionDeliveryFormat, transformOptionPackageName, transformOptionPackageVersion, transformOptionPackageLocation, transformOptionPackageText, transformOptionStylesheetBaseURI, transformOptionStylesheetLocation, transformOptionStylesheetText: + return lexicon.XSString + case transformOptionCache, transformOptionEnableAssertions, transformOptionEnableMessages, transformOptionEnableTrace: + return lexicon.XSBoolean + case transformOptionInitialTemplate, transformOptionInitialMode, transformOptionInitialFunction: + return transformQNameType + case transformOptionSourceNode, transformOptionStylesheetNode, transformOptionPackageNode: + return lexicon.NodeTestNode + case transformOptionStylesheetParams, transformOptionStaticParams, transformOptionTemplateParams, transformOptionTunnelParams, transformOptionVendorOptions: + return transformQNameSequenceMapType + case transformOptionSerializationParams: + return transformAtomicSequenceMapType + case transformOptionRequestedProperties: + return transformQNameAtomicMapType + case transformOptionFunctionParams: + return transformSequenceArrayType + case transformOptionGlobalContextItem: + return lexicon.NodeTestItem + case transformOptionInitialMatchSelection: + return transformItemSequenceType + case transformOptionPostProcess: + return transformPostProcessType + case transformOptionXSLTVersion: + return transformDecimalType + default: + return "" + } +} + +func convertTransformOptions(ctx context.Context, options xdm.MapItem) (xdm.MapItem, error) { + builder := xdm.NewMapBuilder(xdm.MergeReject, options.Size()) + + err := options.ForEachView(func(key xdm.AtomicValue, value xdm.Sequence) error { + if err := xpath.Charge(ctx, 1); err != nil { + return err + } + + name := "" + if xdm.IsStringDerived(key.TypeName) || key.TypeName == xdm.TypeUntypedAtomic || key.TypeName == xdm.TypeAnyURI { + name, _ = xpath.AtomicToString(key) + } + + if err := prevalidateTransformParamMap(name, value); err != nil { + return err + } + + if source := transformOptionType(name); source != "" { + st, err := xpath.ParseSequenceType(source) + if err != nil { + return err + } + + st, err = xpath.BindSequenceType(ctx, st, xpath.PredeclaredNamespaces(), "") + if err != nil { + return err + } + + value, err = xpath.ConvertToSequenceType(ctx, value, st) + if err != nil { + return err + } + } + + return builder.Add(key, value) + }) + if err != nil { + return xdm.MapItem{}, err + } + + return builder.Build(), nil +} + +// Invalid parameter names use FOXT0002. Leave outer type and cardinality errors +// to the required-type conversion, without inspecting keys of multiple maps. +func prevalidateTransformParamMap(name string, value xdm.Sequence) error { + switch name { + case transformOptionStylesheetParams, transformOptionStaticParams, transformOptionTemplateParams, transformOptionTunnelParams: + if xdm.SequenceLen(value) != 1 { + return nil + } + + if _, ok := value.Get(0).(xdm.MapItem); !ok { + return nil + } + + return validateTransformParamMap(name, value) + default: + return nil + } +} + // validateTransformOptions enforces the fn:transform option-map constraints that // are independent of the compiled stylesheet (F&O 3.1 §16.3.2): exactly one // stylesheet source, mutually-exclusive invocation options, a legal @@ -837,7 +1002,7 @@ func lookupTransformCapability(ctx context.Context, key string) (bool, bool) { func validateTransformOptions(ctx context.Context, m xdm.MapItem, deliveryFormat string) error { sources := 0 - for _, k := range []string{"stylesheet-location", "stylesheet-node", "stylesheet-text", "package-name"} { + for _, k := range []string{transformOptionStylesheetLocation, transformOptionStylesheetNode, transformOptionStylesheetText, transformOptionPackageName} { if transformOptionPresent(transformMapSeq(m, k)) { sources++ } @@ -849,7 +1014,7 @@ func validateTransformOptions(ctx context.Context, m xdm.MapItem, deliveryFormat entryPoints := 0 - for _, k := range []string{"initial-template", "initial-mode", "initial-function"} { + for _, k := range []string{transformOptionInitialTemplate, transformOptionInitialMode, transformOptionInitialFunction} { if transformOptionPresent(transformMapSeq(m, k)) { entryPoints++ } @@ -859,18 +1024,18 @@ func validateTransformOptions(ctx context.Context, m xdm.MapItem, deliveryFormat return dynamicError(errCodeFOXT0002, "fn:transform: at most one of initial-template, initial-mode, initial-function may be supplied") } - if transformOptionPresent(transformMapSeq(m, "initial-function")) { - if _, present := m.Get(xdm.AtomicValue{TypeName: xdm.TypeString, Value: "function-params"}); !present { + if transformOptionPresent(transformMapSeq(m, transformOptionInitialFunction)) { + if _, present := m.Get(xdm.AtomicValue{TypeName: xdm.TypeString, Value: transformOptionFunctionParams}); !present { return dynamicError(errCodeFOXT0002, "fn:transform: initial-function requires function-params") } } - if transformOptionPresent(transformMapSeq(m, "source-node")) && transformOptionPresent(transformMapSeq(m, "initial-match-selection")) { + if transformOptionPresent(transformMapSeq(m, transformOptionSourceNode)) && transformOptionPresent(transformMapSeq(m, transformOptionInitialMatchSelection)) { return dynamicError(errCodeFOXT0002, "fn:transform: source-node and initial-match-selection are mutually exclusive") } switch deliveryFormat { - case "", fnNameDocument, transformSerialized, lexicon.OutputRaw: + case "", transformDeliveryDocument, transformSerialized, lexicon.OutputRaw: default: return dynamicError(errCodeFOXT0002, "fn:transform: delivery-format %q is not one of document, serialized, raw", deliveryFormat) } @@ -883,13 +1048,7 @@ func validateTransformOptions(ctx context.Context, m xdm.MapItem, deliveryFormat return err } - for _, name := range []string{"stylesheet-params", "static-params", "template-params", "tunnel-params"} { - if err := validateTransformParamMap(name, transformMapSeq(m, name)); err != nil { - return err - } - } - - return checkTransformCapabilities(ctx, transformMapSeq(m, "requested-properties")) + return checkTransformCapabilities(ctx, transformMapSeq(m, transformOptionRequestedProperties)) } func transformBooleanOption(ctx context.Context, options xdm.MapItem, key string) (bool, error) { @@ -898,7 +1057,7 @@ func transformBooleanOption(ctx context.Context, options xdm.MapItem, key string return false, nil } - value, err := xpath.ConvertToSequenceType(ctx, seq, xdm.SequenceType{ItemTest: xdm.AtomicOrUnionType{Prefix: "xs", Name: lexicon.TypeBoolean}}) + value, err := xpath.ConvertToSequenceType(ctx, seq, xdm.SequenceType{ItemTest: xdm.AtomicOrUnionType{Prefix: lexicon.PrefixXS, Name: lexicon.TypeBoolean}}) if err != nil { return false, err } @@ -918,7 +1077,7 @@ func transformBooleanOption(ctx context.Context, options xdm.MapItem, key string // truncated to its first item. An omitted key is fine (the global context item // then defaults to the source-node root, or is absent). func validateTransformGlobalContextItem(m xdm.MapItem) error { - seq, ok := m.Get(xdm.AtomicValue{TypeName: xdm.TypeString, Value: lexicon.XSLTElementGlobalContextItem}) + seq, ok := m.Get(xdm.AtomicValue{TypeName: xdm.TypeString, Value: transformOptionGlobalContextItem}) if !ok { return nil } @@ -937,9 +1096,9 @@ func validateTransformXSLTVersion(m xdm.MapItem) error { } func transformProcessorVersion(m xdm.MapItem) (int, error) { - seq, present := m.Get(xdm.AtomicValue{TypeName: xdm.TypeString, Value: "xslt-version"}) + seq, present := m.Get(xdm.AtomicValue{TypeName: xdm.TypeString, Value: transformOptionXSLTVersion}) if !present { - return 30, nil + return processorXSLT30, nil } if xdm.SequenceLen(seq) != 1 { @@ -954,13 +1113,13 @@ func transformProcessorVersion(m xdm.MapItem) (int, error) { // The option requests a minimum, not an exact processor. Use the modern // processor's backwards-compatible behavior for every supported // minimum, consistently with an omitted option. - compatible, err := xpath.ValueCompare(xpath.TokenLe, av, xdm.AtomicValue{TypeName: xdm.TypeInteger, Value: int64(3)}) + compatible, err := xpath.ValueCompare(xpath.TokenLe, av, xdm.AtomicValue{TypeName: xdm.TypeInteger, Value: int64(xsltMajorVersion3)}) if err != nil { return 0, err } if compatible { - return 30, nil + return processorXSLT30, nil } text, err := xpath.AtomicToString(av) @@ -1122,7 +1281,7 @@ func stylesheetNodeXMLSpace(ctx context.Context, node xdm.Node, source *xdm.Docu return source, nil } - if _, own := elem.GetAttributeNS("space", lexicon.NamespaceXML); own { + if _, own := elem.GetAttributeNS(lexicon.AttrSpace, lexicon.NamespaceXML); own { return source, nil } @@ -1239,18 +1398,18 @@ type transformRequest struct { func newTransformRequest(options xdm.MapItem, baseURI string) transformRequest { request := transformRequest{ options: options, - stylesheetLoc: transformStringOption(options, "stylesheet-location"), - packageName: transformStringOption(options, "package-name"), - packageVersion: transformStringOption(options, namePackageVersion), - initialTemplate: transformQNameOption(options, "initial-template"), - initialMode: transformStringOption(options, "initial-mode"), - initialFunction: transformQNameOption(options, "initial-function"), - deliveryFormat: transformStringOption(options, "delivery-format"), - baseOutputURI: transformStringOption(options, "base-output-uri"), - stylesheetBaseURI: transformStringOption(options, "stylesheet-base-uri"), - initialMatchSelection: transformMapSeq(options, "initial-match-selection"), - sourceNode: transformMapSeq(options, "source-node"), - globalContextItem: transformMapSeq(options, lexicon.XSLTElementGlobalContextItem), + stylesheetLoc: transformStringOption(options, transformOptionStylesheetLocation), + packageName: transformStringOption(options, transformOptionPackageName), + packageVersion: transformStringOption(options, transformOptionPackageVersion), + initialTemplate: transformQNameOption(options, transformOptionInitialTemplate), + initialMode: transformQNameOption(options, transformOptionInitialMode), + initialFunction: transformQNameOption(options, transformOptionInitialFunction), + deliveryFormat: transformStringOption(options, transformOptionDeliveryFormat), + baseOutputURI: transformStringOption(options, transformOptionBaseOutputURI), + stylesheetBaseURI: transformStringOption(options, transformOptionStylesheetBaseURI), + initialMatchSelection: transformMapSeq(options, transformOptionInitialMatchSelection), + sourceNode: transformMapSeq(options, transformOptionSourceNode), + globalContextItem: transformMapSeq(options, transformOptionGlobalContextItem), } if request.baseOutputURI != "" { request.baseOutputURI = canonicalResultURIKey(request.baseOutputURI, baseURI) @@ -1293,10 +1452,10 @@ func (request transformRequest) newInvocation(ctx context.Context, cfg *transfor } func (request transformRequest) applyParameters(fnTransformCfg *transformConfig, staticParamValues map[string]xdm.Sequence) { - stylesheetParamsSeq := transformMapSeq(request.options, "stylesheet-params") - templateParamsSeq := transformMapSeq(request.options, "template-params") - tunnelParamsSeq := transformMapSeq(request.options, "tunnel-params") - functionParamsSeq := transformMapSeq(request.options, "function-params") + stylesheetParamsSeq := transformMapSeq(request.options, transformOptionStylesheetParams) + templateParamsSeq := transformMapSeq(request.options, transformOptionTemplateParams) + tunnelParamsSeq := transformMapSeq(request.options, transformOptionTunnelParams) + functionParamsSeq := transformMapSeq(request.options, transformOptionFunctionParams) // Apply map-valued options from the fn:transform options map. for _, mp := range []struct { seq xdm.Sequence @@ -1364,7 +1523,7 @@ func (request transformRequest) prepareSource(invocation *transformConfig) *xdm. } } - if _, provided := request.options.Get(xdm.AtomicValue{TypeName: xdm.TypeString, Value: "initial-match-selection"}); provided { + if _, provided := request.options.Get(xdm.AtomicValue{TypeName: xdm.TypeString, Value: transformOptionInitialMatchSelection}); provided { if xdm.SequenceLen(request.initialMatchSelection) == 1 { if item, ok := request.initialMatchSelection.Get(0).(xdm.NodeItem); ok { if owner := owningDocument(item.Node); owner != nil { @@ -1397,7 +1556,7 @@ func (request transformRequest) prepareSource(invocation *transformConfig) *xdm. } func (request transformRequest) deliver(ctx context.Context, resultDoc *xdm.Document, cfg *transformConfig) (xdm.MapItem, error) { - key := xdm.AtomicValue{TypeName: xdm.TypeString, Value: nameOutput} + key := xdm.AtomicValue{TypeName: xdm.TypeString, Value: transformPrimaryResultKey} if request.baseOutputURI != "" { key.Value = request.baseOutputURI } @@ -1411,7 +1570,7 @@ func (request transformRequest) deliver(ctx context.Context, resultDoc *xdm.Docu if request.deliveryFormat != lexicon.OutputRaw { var forceTree *bool - if request.deliveryFormat == fnNameDocument { + if request.deliveryFormat == transformDeliveryDocument { enabled := true forceTree = &enabled } @@ -1435,8 +1594,13 @@ func (request transformRequest) deliver(ctx context.Context, resultDoc *xdm.Docu // deliverSerialized uses the same complete value sequences and serializer as // the public XSLT API, including JSON/adaptive results with no document tree. func (request transformRequest) deliverSerialized(ctx context.Context, doc *xdm.Document, cfg *transformConfig, key xdm.AtomicValue) (xdm.MapItem, error) { - params := transformMapSeq(request.options, "serialization-params") - output := applySerializationParams(cfg.resolvedOutputDef, params) + params := transformMapSeq(request.options, transformOptionSerializationParams) + + output, err := applySerializationParams(ctx, cfg.resolvedOutputDef, params) + if err != nil { + return xdm.MapItem{}, err + } + result := xdm.MapItem{} value := ValueResult{Document: doc, Items: cfg.rawCapturedItems, Output: output} @@ -1498,18 +1662,22 @@ func serializeDeliveredValue(ctx context.Context, value ValueResult) (string, er return buf.String(), nil } -// applySerializationParams returns an OutputDef with the fn:transform -// serialization-params option applied over base. base is returned unchanged when -// no params are supplied. Recognized keys (string or fn/output-namespace QName) -// map to the corresponding OutputDef fields. -func applySerializationParams(base *OutputDef, seq xdm.Sequence) *OutputDef { +// applySerializationParams applies only validated supplied parameters over base. +// Empty-sequence values reset inherited values; empty maps and explicit false +// remain supplied values. QName/name sets and character maps retain augmentation. +func applySerializationParams(ctx context.Context, base *OutputDef, seq xdm.Sequence) (*OutputDef, error) { if !transformOptionPresent(seq) { - return base + return base, nil } - sm, ok := seq.Get(0).(xdm.MapItem) - if !ok { - return base + params, ok := seq.Get(0).(xdm.MapItem) + if !ok || xdm.SequenceLen(seq) != 1 { + return nil, dynamicError(lexicon.ErrXPTY0004, "serialization-params requires one map") + } + + validated, err := xpath.ReadSerializationParameterMap(ctx, params, xpathProfile31) + if err != nil { + return nil, err } out := cloneOutputDef(base) @@ -1517,47 +1685,36 @@ func applySerializationParams(base *OutputDef, seq xdm.Sequence) *OutputDef { out = defaultOutputDef() } - versionExplicit := false + _, versionExplicit := validated.Strings[lexicon.SerializationVersion] + for _, name := range validated.Reset { + versionExplicit = versionExplicit || name == lexicon.SerializationVersion + resetSerializationParam(out, name) + } - for _, key := range sm.Keys() { - name := serializationParamName(key) - if name == "" { - continue - } + for name, value := range validated.Booleans { + (¶meterDocumentValues{output: out}).applyBoolean(name, value) + } - if name == lexicon.SerializationVersion { - versionExplicit = true - } + for name, value := range validated.Strings { + (¶meterDocumentValues{output: out}).applyString(name, value) + } - val, ok := sm.Get(key) - if !ok { - continue - } - // A recognized parameter present with an empty-sequence value overrides - // the inherited xsl:output value by resetting the parameter to its - // serialization default (F&O 3.1 §16.3.2), leaving no - // inherited value in place. - if !transformOptionPresent(val) { - resetSerializationParam(out, name) - continue + out.CDATASections = unionOutputNames(out.CDATASections, validated.Names[lexicon.SerializationCDATASectionElements]) + + out.SuppressIndentation = unionOutputNames(out.SuppressIndentation, validated.Names[lexicon.SerializationSuppressIndentation]) + if validated.CharactersSet { + if out.ResolvedCharMap == nil { + out.ResolvedCharMap = make(map[rune]string, len(validated.Characters)) } - applySerializationParam(out, name, val) - } - // The version parameter's default is method-dependent, so an inherited xml - // version="1.0" must not carry into an html/xhtml method selected via - // serialization-params. When these params switch the method to html/xhtml - // without supplying an explicit version — and the base stylesheet did not - // declare one either (VersionRaw empty) — unset Version so the - // method-appropriate serialization default applies instead of the inherited - // "1.0" (which the html method rejects with SESU0007). An explicit base - // version (e.g. ) is preserved so - // it still raises SESU0007. + maps.Copy(out.ResolvedCharMap, validated.Characters) + } + if !versionExplicit && out.VersionRaw == "" && out.MethodExplicit && (out.Method == lexicon.MethodHTML || out.Method == lexicon.MethodXHTML) { out.Version = "" } - return out + return out, nil } // resetSerializationParam resets a recognized serialization parameter (by local @@ -1569,6 +1726,12 @@ func resetSerializationParam(out *OutputDef, name string) { out.MethodExplicit = false case lexicon.SerializationIndent: out.Indent = false + case lexicon.SerializationAllowDuplicateNames: + out.AllowDuplicateNames = false + case lexicon.SerializationEscapeURIAttributes: + out.EscapeURIAttributes = nil + case lexicon.SerializationIncludeContentType: + out.IncludeContentType = nil case lexicon.SerializationOmitXMLDeclaration: out.OmitDeclaration = false out.OmitDeclarationExplicit = false @@ -1579,7 +1742,7 @@ func resetSerializationParam(out *OutputDef, name string) { case lexicon.SerializationEncoding: out.Encoding = lexicon.EncodingUTF8U case lexicon.SerializationVersion: - out.Version = lexicon.XSLTVersion10 + out.Version, out.VersionRaw = "", "" case lexicon.SerializationStandalone: out.Standalone = "" case lexicon.SerializationMediaType: @@ -1589,177 +1752,20 @@ func resetSerializationParam(out *OutputDef, name string) { case lexicon.SerializationDoctypeSystem: out.DoctypeSystem = "" case lexicon.SerializationItemSeparator: - out.ItemSeparator = nil + out.ItemSeparator, out.ItemSeparatorAbsent = nil, true case lexicon.SerializationCDATASectionElements: out.CDATASections = nil case lexicon.SerializationSuppressIndentation: out.SuppressIndentation = nil case lexicon.SerializationUseCharacterMaps: - out.ResolvedCharMap = nil - } -} - -// serializationParamName returns the local name of a serialization-params map -// key, which may be supplied either as an xs:string or as an xs:QName in the -// serialization-parameter (fn/output) namespace. -func serializationParamName(key xdm.AtomicValue) string { - if qv, ok := key.Value.(xdm.QNameValue); ok { - return qv.Local + out.UseCharacterMaps, out.ResolvedCharMap = nil, nil + case lexicon.SerializationNormalizationForm: + out.NormalizationForm = "" + case lexicon.SerializationHTMLVersion: + out.HTMLVersion = "" + case lexicon.SerializationJSONNodeOutputMethod: + out.JSONNodeOutputMethod = lexicon.MethodXML } - - s, err := xpath.AtomicToString(key) - if err != nil { - return "" - } - - return s -} - -// applySerializationParam applies a single serialization parameter (identified -// by its local name) to out. -func applySerializationParam(out *OutputDef, name string, val xdm.Sequence) { - switch name { - case lexicon.SerializationMethod: - if s, ok := transformSeqString(val); ok { - out.Method = s - out.MethodExplicit = true - } - case lexicon.SerializationIndent: - if b, ok := transformSeqBool(val); ok { - out.Indent = b - } - case lexicon.SerializationOmitXMLDeclaration: - if b, ok := transformSeqBool(val); ok { - out.OmitDeclaration = b - out.OmitDeclarationExplicit = true - } - case lexicon.SerializationByteOrderMark: - if b, ok := transformSeqBool(val); ok { - out.ByteOrderMark = b - } - case lexicon.SerializationUndeclarePrefixes: - if b, ok := transformSeqBool(val); ok { - out.UndeclarePrefixes = b - } - case lexicon.SerializationEncoding: - if s, ok := transformSeqString(val); ok { - out.Encoding = s - } - case lexicon.SerializationVersion: - if s, ok := transformSeqString(val); ok { - out.Version = s - } - case lexicon.SerializationStandalone: - if s, ok := transformSeqString(val); ok { - out.Standalone = s - } - case lexicon.SerializationMediaType: - if s, ok := transformSeqString(val); ok { - out.MediaType = s - } - case lexicon.SerializationDoctypePublic: - if s, ok := transformSeqString(val); ok { - out.DoctypePublic = s - } - case lexicon.SerializationDoctypeSystem: - if s, ok := transformSeqString(val); ok { - out.DoctypeSystem = s - } - case lexicon.SerializationItemSeparator: - if s, ok := transformSeqString(val); ok { - out.ItemSeparator = &s - } - case lexicon.SerializationCDATASectionElements: - out.CDATASections = mergeSerializationQNames(out.CDATASections, val) - case lexicon.SerializationSuppressIndentation: - out.SuppressIndentation = mergeSerializationQNames(out.SuppressIndentation, val) - case lexicon.SerializationUseCharacterMaps: - applyUseCharacterMapsParam(out, val) - } -} - -// serializationQNameClarkName returns the Clark-notation name ({uri}local, or -// the bare local name when there is no namespace) of a serialization-params -// value item that is an xs:QName. Non-QName items yield "". -func serializationQNameClarkName(item xdm.Item) string { - av, err := xpath.AtomizeItem(item) - if err != nil { - return "" - } - - qv, ok := av.Value.(xdm.QNameValue) - if !ok { - return "" - } - - if qv.URI != "" { - return xdm.ClarkName(qv.URI, qv.Local) - } - - return qv.Local -} - -// mergeSerializationQNames unions the Clark names of the xs:QName items in val -// into base. The cdata-section-elements / suppress-indentation values supplied -// via serialization-params accumulate onto the stylesheet's xsl:output set, -// replacing none of it (F&O 3.1 §16.3.2), skipping duplicates. -func mergeSerializationQNames(base []string, val xdm.Sequence) []string { - seen := make(map[string]struct{}, len(base)) - for _, n := range base { - seen[n] = struct{}{} - } - - for i := range xdm.SequenceLen(val) { - name := serializationQNameClarkName(val.Get(i)) - if name == "" { - continue - } - - if _, ok := seen[name]; ok { - continue - } - - seen[name] = struct{}{} - base = append(base, name) - } - - return base -} - -// applyUseCharacterMapsParam merges the (character → replacement) entries of a -// use-character-maps serialization-param map into out.ResolvedCharMap. Its -// value is a map(xs:string, xs:string) keyed by single characters; entries -// override any inherited mapping for the same character. -func applyUseCharacterMapsParam(out *OutputDef, val xdm.Sequence) { - sm, ok := val.Get(0).(xdm.MapItem) - if !ok { - return - } - - if out.ResolvedCharMap == nil { - out.ResolvedCharMap = make(map[rune]string, sm.Size()) - } - - _ = sm.ForEach(func(key xdm.AtomicValue, value xdm.Sequence) error { - ks, err := xpath.AtomicToString(key) - if err != nil { - return nil //nolint:nilerr // skip unparseable character-map keys - } - - runes := []rune(ks) - if len(runes) != 1 { - return nil // character-map keys are single characters - } - - rs, ok := transformSeqString(value) - if !ok { - return nil - } - - out.ResolvedCharMap[runes[0]] = rs - - return nil - }) } func transformStringOption(options xdm.MapItem, key string) string { diff --git a/transform/internal/xslt/functions_user.go b/transform/internal/xslt/functions_user.go index 86ab2af..329b9a3 100644 --- a/transform/internal/xslt/functions_user.go +++ b/transform/internal/xslt/functions_user.go @@ -113,7 +113,7 @@ func (f *xslUserFunc) DynamicRefSnapshot(_ context.Context, arity int) (xdm.Func func (f *xslUserFunc) FuncParamTypes() []xdm.SequenceType { // The XSLT 2.0 call boundary performs conversion itself so that a mismatch // raises XTTE0790. XPath's generic precheck would mask it with XPTY0004. - if f.ec.stylesheet.processorLanguage() == 20 { + if f.ec.stylesheet.processorLanguage() == processorXSLT20 { return nil } @@ -250,10 +250,8 @@ func (f *xslUserFunc) Call(ctx context.Context, args []xdm.Sequence) (xdm.Sequen ec.outputStack = ec.outputStack[:len(ec.outputStack)-1] }() - for _, inst := range f.def.Body { - if err := ec.executeInstruction(ctx, inst); err != nil { - return nil, err - } + if err := ec.executeSequenceConstructor(ctx, f.def.Body); err != nil { + return nil, err } if f.def.selectOnly && tmpRoot.FirstChild() != nil { @@ -350,7 +348,7 @@ func (ec *execContext) convertSequenceType(ctx context.Context, value xdm.Sequen func (ec *execContext) convertFunctionArgument(ctx context.Context, value xdm.Sequence, st xdm.SequenceType) (xdm.Sequence, error) { converted, err := ec.convertSequenceType(ctx, value, st) - if err != nil && ec.stylesheet.processorLanguage() == 20 { + if err != nil && ec.stylesheet.processorLanguage() == processorXSLT20 { var detail *xdm.Error if errors.As(err, &detail) && detail.Code == lexicon.ErrXPTY0004 { return nil, dynamicError(errCodeXTTE0790, "function argument: %s", detail.Message) @@ -401,13 +399,17 @@ func (ec *execContext) captureFunctionContext() *execContext { // typeAnnotations, ...) keyed by node identity, which a recycled slab would // alias. func (ec *execContext) functionOutputRoot(def *xslFunction) (*xdm.Document, *xdm.Element, error) { + const ( + functionResultWrapper = "_xsl_fn_result" + ) + if def.selectOnly && ec.fnScratchRoot != nil { return ec.fnScratchDoc, ec.fnScratchRoot, nil } doc := xdm.NewDefaultDocument() - root, err := doc.CreateElement("_xsl_fn_result") + root, err := doc.CreateElement(functionResultWrapper) if err != nil { return nil, nil, err } @@ -431,6 +433,7 @@ func (ec *execContext) functionInvocation(ctx context.Context) *execContext { config.rawCapturedItems, config.primaryItems = nil, nil config.valueResults = make(map[string]ValueResult) cfg = &config + rebaseFunctionResources(ctx, cfg) } local := newTransformExecContext(ctx, ec.sourceDoc, ec.stylesheet, cfg) @@ -470,6 +473,10 @@ func (ec *execContext) functionInvocation(ctx context.Context) *execContext { } func (ec *execContext) functionCacheKey(def *xslFunction, args []xdm.Sequence) (string, bool) { + const ( + memoPackageIdentityFormat = "pkg:%p|" + ) + if ec == nil || def == nil { return "", false } @@ -481,12 +488,11 @@ func (ec *execContext) functionCacheKey(def *xslFunction, args []xdm.Sequence) ( var b strings.Builder if def.OwnerPackage != nil { - fmt.Fprintf(&b, "pkg:%p|", def.OwnerPackage) + fmt.Fprintf(&b, memoPackageIdentityFormat, def.OwnerPackage) } - b.WriteString(def.Name.URI) - b.WriteByte('|') - b.WriteString(def.Name.Name) + writeFunctionCacheString(&b, def.Name.URI) + writeFunctionCacheString(&b, def.Name.Name) b.WriteByte('#') b.WriteString(strconv.Itoa(len(args))) @@ -501,6 +507,12 @@ func (ec *execContext) functionCacheKey(def *xslFunction, args []xdm.Sequence) ( return b.String(), true } +func writeFunctionCacheString(b *strings.Builder, value string) { + b.WriteString(strconv.Itoa(len(value))) + b.WriteByte(':') + b.WriteString(value) +} + func (ec *execContext) writeFunctionCacheSequence(b *strings.Builder, seq xdm.Sequence) bool { b.WriteByte('[') b.WriteString(strconv.Itoa(xdm.SequenceLen(seq))) @@ -519,6 +531,17 @@ func (ec *execContext) writeFunctionCacheSequence(b *strings.Builder, seq xdm.Se } func (ec *execContext) writeFunctionCacheItem(b *strings.Builder, item xdm.Item) bool { + const ( + memoArrayPrefix = "r[" + memoAtomicPrefix = "a:" + memoMapPrefix = "m{" + memoNodePrefix = "n:" + ) + + const ( + memoNodeDecimalRadix = 10 + ) + switch v := item.(type) { case xdm.AtomicValue: s, err := xpath.AtomicToString(v) @@ -526,39 +549,37 @@ func (ec *execContext) writeFunctionCacheItem(b *strings.Builder, item xdm.Item) return false } - b.WriteString("a:") - b.WriteString(v.TypeName) + b.WriteString(memoAtomicPrefix) + writeFunctionCacheString(b, v.TypeName) b.WriteByte('=') // AtomicToString for QName returns prefix:local, which omits the // namespace URI. Two QNames can share a local name but differ in URI, // so we must include the URI in the cache key. if q, ok := v.Value.(xdm.QNameValue); ok { - b.WriteString("{") - b.WriteString(q.URI) - b.WriteString("}") - b.WriteString(q.Local) + writeFunctionCacheString(b, q.URI) + writeFunctionCacheString(b, q.Local) } else { - b.WriteString(s) + writeFunctionCacheString(b, s) } return true case xdm.NodeItem: - b.WriteString("n:") - b.WriteString(strconv.FormatUint(ec.memoNodeID(v.Node), 10)) + b.WriteString(memoNodePrefix) + b.WriteString(strconv.FormatUint(ec.memoNodeID(v.Node), memoNodeDecimalRadix)) if v.TypeAnnotation != "" { b.WriteByte('@') - b.WriteString(v.TypeAnnotation) + writeFunctionCacheString(b, v.TypeAnnotation) } if v.AtomizedType != "" { b.WriteByte('!') - b.WriteString(v.AtomizedType) + writeFunctionCacheString(b, v.AtomizedType) } return true case xdm.MapItem: - b.WriteString("m{") + b.WriteString(memoMapPrefix) for _, key := range v.Keys() { if !ec.writeFunctionCacheItem(b, key) { @@ -579,7 +600,7 @@ func (ec *execContext) writeFunctionCacheItem(b *strings.Builder, item xdm.Item) return true case xdm.ArrayItem: - b.WriteString("r[") + b.WriteString(memoArrayPrefix) for _, member := range v.Members() { if !ec.writeFunctionCacheSequence(b, member) { @@ -809,7 +830,7 @@ func (fn *xslFunction) prepareCall(ctx context.Context, namespaces map[string]st for _, p := range fn.Params { as := p.As if as == "" { - as = "item()*" + as = lexicon.NodeTestItem + "*" } st, err := xpath.ParseSequenceType(as) diff --git a/transform/internal/xslt/grammar.go b/transform/internal/xslt/grammar.go index 91a1360..f8c65f3 100644 --- a/transform/internal/xslt/grammar.go +++ b/transform/internal/xslt/grammar.go @@ -5,6 +5,12 @@ package xslt import "github.com/skarm/xmlsec/internal/lexicon" +// Numeric XML Schema boolean aliases shared by grammar validation and runtime parsing; each language profile still decides whether those aliases are admissible, and format pictures keep their independent spellings. +const ( + booleanDigitFalse = "0" + booleanDigitTrue = "1" +) + // elementDef describes XSLT element syntax from the normative XSLT 3.0 // summaries (§2.2): allowed attributes, required fields, AVTs and closed values. // The table supports XTSE0010 (invalid element), XTSE0020 (invalid attribute @@ -50,7 +56,7 @@ type attrDef struct { // componentKinds is the closed REC H.1 vocabulary for xsl:expose and xsl:accept. func componentKinds() []string { - return []string{nameTemplate, "function", nameVariable, nameAttributeSet, lexicon.AttrMode, "*"} + return []string{nameTemplate, lexicon.XSLTElementFunction, nameVariable, nameAttributeSet, lexicon.AttrMode, "*"} } // lookupXSLTElement constructs the requested element's grammar definition. @@ -82,25 +88,25 @@ func lookupRootXSLTElement(name string) (elementDef, bool) { switch name { case nameStylesheet: return elementDef{attrs: map[string]attrDef{ - "id": {}, + lexicon.AttrID: {}, nameExtensionElementPrefixes: {}, nameExcludeResultPrefixes: {}, - lexicon.SerializationVersion: {required: true}, - nameXpathDefaultNamespace: {}, + xslAttrVersion: {required: true}, + nameXPathDefaultNamespace: {}, nameDefaultValidation: {values: []string{validationPreserve, validationStrip}}, nameDefaultCollation: {}, nameInputTypeAnnotations: {values: []string{validationPreserve, validationStrip, validationUnspecified}}, }}, true case namePackage: return elementDef{since30: true, attrs: map[string]attrDef{ - "id": {}, + lexicon.AttrID: {}, xslAttrName: {}, namePackageVersion: {}, - lexicon.SerializationVersion: {required: true}, - "declared-modes": {values: []string{lexicon.ValueYes, "no", lexicon.ValueTrue, lexicon.ValueFalse, "1", "0"}}, + xslAttrVersion: {required: true}, + nameDeclaredModes: {values: []string{lexicon.ValueYes, lexicon.ValueNo, lexicon.ValueTrue, lexicon.ValueFalse, booleanDigitTrue, booleanDigitFalse}}, nameExtensionElementPrefixes: {}, nameExcludeResultPrefixes: {}, - nameXpathDefaultNamespace: {}, + nameXPathDefaultNamespace: {}, nameDefaultValidation: {values: []string{validationPreserve, validationStrip}}, nameDefaultCollation: {}, nameDefaultMode: {}, @@ -108,13 +114,13 @@ func lookupRootXSLTElement(name string) (elementDef, bool) { // Packages are composed through xsl:use-package, not this draft attribute. nameUsePackage: {removed30: true}, }}, true - case fnNameTransform: + case lexicon.XSLTElementTransform: return elementDef{attrs: map[string]attrDef{ - "id": {}, + lexicon.AttrID: {}, nameExtensionElementPrefixes: {}, nameExcludeResultPrefixes: {}, - lexicon.SerializationVersion: {required: true}, - nameXpathDefaultNamespace: {}, + xslAttrVersion: {required: true}, + nameXPathDefaultNamespace: {}, nameDefaultValidation: {values: []string{validationPreserve, validationStrip}}, nameDefaultCollation: {}, nameInputTypeAnnotations: {values: []string{validationPreserve, validationStrip, validationUnspecified}}, @@ -132,21 +138,21 @@ func lookupPackageXSLTElement(name string) (elementDef, bool) { xslAttrName: {required: true}, namePackageVersion: {}, }}, true - case "expose": + case lexicon.XSLTElementExpose: return elementDef{since30: true, attrs: map[string]attrDef{ // REC H.1 restricts @component to the six component kinds. - "component": {required: true, values: componentKinds()}, - "names": {required: true}, + xslAttrComponent: {required: true, values: componentKinds()}, + xslAttrNames: {required: true}, // The summary (section 3.5.3.1) stops at "abstract"; unlike // xsl:accept's, it carries no "hidden". xslAttrVisibility: {required: true, values: []string{ visPublic, visPrivate, visFinal, visAbstract, }}, }}, true - case "accept": + case lexicon.XSLTElementAccept: return elementDef{since30: true, attrs: map[string]attrDef{ - "component": {required: true, values: componentKinds()}, - "names": {required: true}, + xslAttrComponent: {required: true, values: componentKinds()}, + xslAttrNames: {required: true}, xslAttrVisibility: {required: true, values: []string{ visPublic, visPrivate, visFinal, visAbstract, visHidden, }}, @@ -161,23 +167,30 @@ func lookupPackageXSLTElement(name string) (elementDef, bool) { } func lookupDeclarationXSLTElement(name string) (elementDef, bool) { + const ( + nameAppliesTo = "applies-to" + nameExport = "export" + nameSchemaLocation = "schema-location" + streamDeepDescent = "deep-descent" + ) + switch name { // The engine accepts xsl:mode in 2.0 modules as configuration whose // defaults already match 2.0. Attributes are still validated; streamable does // not enable W3C streaming execution. case lexicon.AttrMode: return elementDef{attrs: map[string]attrDef{ - xslAttrName: {}, - nameStreamable: {values: []string{lexicon.ValueYes, "no"}}, - nameUseAccumulators: {}, - "on-no-match": {values: []string{"deep-copy", "shallow-copy", "deep-skip", "shallow-skip", "text-only-copy", onNoMatchFail}}, - "on-multiple-match": {values: []string{"use-last", onNoMatchFail}}, - "warning-on-no-match": {values: []string{lexicon.ValueYes, "no"}}, - "warning-on-multiple-match": {values: []string{lexicon.ValueYes, "no"}}, + xslAttrName: {}, + nameStreamable: {values: []string{lexicon.ValueYes, lexicon.ValueNo}}, + nameUseAccumulators: {}, + nameOnNoMatch: {values: []string{onNoMatchDeepCopy, onNoMatchShallowCopy, onNoMatchDeepSkip, onNoMatchShallowSkip, onNoMatchTextOnlyCopy, onNoMatchFail}}, + nameOnMultipleMatch: {values: []string{onMultipleMatchUseLast, onNoMatchFail}}, + nameWarningOnNoMatch: {values: []string{lexicon.ValueYes, lexicon.ValueNo}}, + nameWarningOnMultipleMatch: {values: []string{lexicon.ValueYes, lexicon.ValueNo}}, // XSLT 3.0 section 6.6: @typed says whether the mode expects typed // input. "lax" and "strict" join the booleans, so the vocabulary is // not the plain yes/no one. - nameTyped: {values: []string{lexicon.ValueYes, "no", lexicon.ValueTrue, lexicon.ValueFalse, "1", "0", validationLax, validationStrict, validationUnspecified}}, + nameTyped: {values: []string{lexicon.ValueYes, lexicon.ValueNo, lexicon.ValueTrue, lexicon.ValueFalse, booleanDigitTrue, booleanDigitFalse, validationLax, validationStrict, validationUnspecified}}, // Modes cannot declare abstract or hidden visibility (§6.6). // Hidden visibility is acquired through xsl:accept or xsl:expose. xslAttrVisibility: {values: []string{visPublic, visPrivate, visFinal}}, @@ -190,10 +203,10 @@ func lookupDeclarationXSLTElement(name string) (elementDef, bool) { return elementDef{attrs: map[string]attrDef{ lexicon.AttrHref: {required: true}, }}, true - case "import-schema": + case lexicon.XSLTElementImportSchema: return elementDef{attrs: map[string]attrDef{ - nameNamespace: {}, - "schema-location": {}, + nameNamespace: {}, + nameSchemaLocation: {}, }}, true case nameStripSpace: return elementDef{attrs: map[string]attrDef{ @@ -203,30 +216,30 @@ func lookupDeclarationXSLTElement(name string) (elementDef, bool) { return elementDef{attrs: map[string]attrDef{ nameElements: {required: true}, }}, true - case "accumulator": + case lexicon.XSLTElementAccumulator: return elementDef{since30: true, attrs: map[string]attrDef{ - xslAttrName: {required: true}, - "initial-value": {required: true}, - "as": {}, - nameStreamable: {values: []string{lexicon.ValueYes, "no", lexicon.ValueTrue, lexicon.ValueFalse, "1", "0"}}, + xslAttrName: {required: true}, + nameInitialValue: {required: true}, + nameAs: {}, + nameStreamable: {values: []string{lexicon.ValueYes, lexicon.ValueNo, lexicon.ValueTrue, lexicon.ValueFalse, booleanDigitTrue, booleanDigitFalse}}, // Reject the draft attribute absent from the §18.2.1 summary. - "applies-to": {removed30: true}, + nameAppliesTo: {removed30: true}, }}, true // An accumulator rule allows only match, phase and select (§18.2.1). - case "accumulator-rule": + case lexicon.XSLTElementAccumulatorRule: return elementDef{since30: true, attrs: map[string]attrDef{ lexicon.AttrMatch: {required: true}, - "phase": {values: []string{"start", "end"}}, + namePhase: {values: []string{accumulatorPhaseStart, accumulatorPhaseEnd}}, xslAttrSelect: {}, }}, true case nameTemplate: return elementDef{attrs: map[string]attrDef{ lexicon.AttrMatch: {}, xslAttrName: {}, - "priority": {}, + namePriority: {}, lexicon.AttrMode: {}, - "as": {}, + nameAs: {}, // Declared visibility excludes hidden, which is acquired through // xsl:accept or xsl:expose (§6.1). xslAttrVisibility: {since30: true, values: []string{visPublic, visPrivate, visFinal, visAbstract}}, @@ -238,8 +251,8 @@ func lookupDeclarationXSLTElement(name string) (elementDef, bool) { return elementDef{attrs: map[string]attrDef{ xslAttrName: {required: true}, xslAttrSelect: {}, - "as": {}, - nameStatic: {since30: true, values: []string{lexicon.ValueYes, "no", lexicon.ValueTrue, lexicon.ValueFalse, "1", "0"}}, + nameAs: {}, + nameStatic: {since30: true, values: []string{lexicon.ValueYes, lexicon.ValueNo, lexicon.ValueTrue, lexicon.ValueFalse, booleanDigitTrue, booleanDigitFalse}}, // Unlike xsl:param, xsl:variable declares visibility (§9.1). // Hidden visibility is acquired rather than declared. xslAttrVisibility: {since30: true, values: []string{visPublic, visPrivate, visFinal, visAbstract}}, @@ -248,15 +261,15 @@ func lookupDeclarationXSLTElement(name string) (elementDef, bool) { return elementDef{attrs: map[string]attrDef{ xslAttrName: {required: true}, xslAttrSelect: {}, - "as": {}, - ctxItemRequired: {values: []string{lexicon.ValueYes, "no"}}, - nameTunnel: {values: []string{lexicon.ValueYes, "no"}}, + nameAs: {}, + xslAttrRequired: {values: []string{lexicon.ValueYes, lexicon.ValueNo}}, + nameTunnel: {values: []string{lexicon.ValueYes, lexicon.ValueNo}}, // Reject the draft export attribute absent from §9.2. - "export": {removed30: true}, + nameExport: {removed30: true}, // Static parameters follow the processor's version, not the module's. nameStatic: { processor30: true, - values: []string{lexicon.ValueYes, "no", lexicon.ValueTrue, lexicon.ValueFalse, "1", "0"}, + values: []string{lexicon.ValueYes, lexicon.ValueNo, lexicon.ValueTrue, lexicon.ValueFalse, booleanDigitTrue, booleanDigitFalse}, }, }}, true case nameAttributeSet: @@ -268,16 +281,16 @@ func lookupDeclarationXSLTElement(name string) (elementDef, bool) { visPublic, visPrivate, visFinal, visAbstract, }}, nameStreamable: {processor30: true, values: []string{ - lexicon.ValueYes, "no", lexicon.ValueTrue, lexicon.ValueFalse, "1", "0", + lexicon.ValueYes, lexicon.ValueNo, lexicon.ValueTrue, lexicon.ValueFalse, booleanDigitTrue, booleanDigitFalse, }}, }}, true - case "function": + case lexicon.XSLTElementFunction: return elementDef{attrs: map[string]attrDef{ xslAttrName: {required: true}, - "as": {}, + nameAs: {}, // A 2.0 attribute; a selected 3.0 processor also admits the four // boolean aliases, regardless of the module's version. - xslElemOverride: {values: []string{lexicon.ValueYes, "no"}}, + xslElemOverride: {values: []string{lexicon.ValueYes, lexicon.ValueNo}}, // A function cannot declare hidden visibility; xsl:expose may make it // hidden (REC H.1, xsl:visibility-not-hidden-type). xslAttrVisibility: {since30: true, values: []string{ @@ -286,53 +299,53 @@ func lookupDeclarationXSLTElement(name string) (elementDef, bool) { // REC H.1 permits a namespaced EQName as a processor-defined streamability // classification. This attribute is not an AVT; shadow attributes are expanded // before grammar validation. - "streamability": { + nameStreamability: { since30: true, eqnameOK: true, values: []string{ - "unclassified", "absorbing", "inspection", - "filter", "shallow-descent", "deep-descent", "ascent", + lexicon.StreamUnclassified, lexicon.StreamAbsorbing, lexicon.StreamInspection, + lexicon.StreamFilter, lexicon.StreamShallowDescent, streamDeepDescent, lexicon.StreamAscent, }, }, // Reject the draft identity-sensitive attribute absent from §10.3. - "identity-sensitive": {removed30: true}, + nameIdentitySensitive: {removed30: true}, // These attributes govern processor behavior, so their availability // follows the processor's version. - "override-extension-function": { + nameOverrideExtensionFunction: { processor30: true, - values: []string{lexicon.ValueYes, "no", lexicon.ValueTrue, lexicon.ValueFalse, "1", "0"}, + values: []string{lexicon.ValueYes, lexicon.ValueNo, lexicon.ValueTrue, lexicon.ValueFalse, booleanDigitTrue, booleanDigitFalse}, }, // The default "maybe" permits result reuse. This attribute is not an AVT. - "new-each-time": { + nameNewEachTime: { processor30: true, - values: []string{lexicon.ValueYes, "no", lexicon.ValueTrue, lexicon.ValueFalse, "1", "0", nameMaybe}, + values: []string{lexicon.ValueYes, lexicon.ValueNo, lexicon.ValueTrue, lexicon.ValueFalse, booleanDigitTrue, booleanDigitFalse, nameMaybe}, }, // @cache is a boolean memoization hint, not a result-reuse guarantee (§10.3). - "cache": { + nameCache: { processor30: true, - values: []string{lexicon.ValueYes, "no", lexicon.ValueTrue, lexicon.ValueFalse, "1", "0"}, + values: []string{lexicon.ValueYes, lexicon.ValueNo, lexicon.ValueTrue, lexicon.ValueFalse, booleanDigitTrue, booleanDigitFalse}, }, }}, true - case "namespace-alias": + case lexicon.XSLTElementNamespaceAlias: return elementDef{attrs: map[string]attrDef{ - "stylesheet-prefix": {required: true}, - "result-prefix": {required: true}, + nameStylesheetPrefix: {required: true}, + nameResultPrefix: {required: true}, }}, true // XSLT 3.0 section 3.10, a top-level declaration. case lexicon.XSLTElementGlobalContextItem: return elementDef{since30: true, attrs: map[string]attrDef{ - "as": {}, - lexicon.AttrUse: {values: []string{ctxItemRequired, "optional", "absent"}}, + nameAs: {}, + lexicon.AttrUse: {values: []string{ctxItemRequired, ctxItemOptional, ctxItemAbsent}}, // Global context declarations are validated through the context-item entry. }}, true // XSLT 3.0 section 10.1.1, a child of xsl:template only. case nameContextItem: return elementDef{since30: true, attrs: map[string]attrDef{ - "as": {}, - lexicon.AttrUse: {values: []string{ctxItemRequired, "optional", "absent"}}, + nameAs: {}, + lexicon.AttrUse: {values: []string{ctxItemRequired, ctxItemOptional, ctxItemAbsent}}, }}, true - case nameKey2: + case keyLocalName: return elementDef{attrs: map[string]attrDef{ xslAttrName: {required: true}, lexicon.AttrMatch: {required: true}, @@ -340,21 +353,21 @@ func lookupDeclarationXSLTElement(name string) (elementDef, bool) { nameCollation: {}, // composite selects one key for the entire use sequence instead of one // key per item (§6.3). - nameComposite: {since30: true, values: []string{lexicon.ValueYes, "no", lexicon.ValueTrue, lexicon.ValueFalse, "1", "0"}}, - }}, true - case "decimal-format": - return elementDef{attrs: map[string]attrDef{ - xslAttrName: {}, - nameDecimalSeparator: {}, - nameGroupingSeparator: {}, - nameInfinity: {}, - nameMinusSign: {}, - lexicon.FloatNaN: {}, - namePercent: {}, - namePerMille: {}, - nameZeroDigit: {}, - nameDigit: {}, - namePatternSeparator: {}, + nameComposite: {since30: true, values: []string{lexicon.ValueYes, lexicon.ValueNo, lexicon.ValueTrue, lexicon.ValueFalse, booleanDigitTrue, booleanDigitFalse}}, + }}, true + case lexicon.XSLTElementDecimalFormat: + return elementDef{attrs: map[string]attrDef{ + xslAttrName: {}, + nameDecimalSeparator: {}, + nameGroupingSeparator: {}, + nameInfinity: {}, + nameMinusSign: {}, + lexicon.DecimalFormatNaN: {}, + namePercent: {}, + namePerMille: {}, + nameZeroDigit: {}, + nameDigit: {}, + namePatternSeparator: {}, // exponent-separator follows the processor's version, not the module's. nameExponentSeparator: {processor30: true}, }}, true @@ -365,40 +378,40 @@ func lookupDeclarationXSLTElement(name string) (elementDef, bool) { func lookupConstructorXSLTElement(name string) (elementDef, bool) { switch name { - case "element": + case lexicon.XSLTElementElement: return elementDef{attrs: map[string]attrDef{ xslAttrName: {required: true, avt: true}, nameNamespace: {avt: true}, - nameInheritNamespaces: {values: []string{lexicon.ValueYes, "no"}}, + nameInheritNamespaces: {values: []string{lexicon.ValueYes, lexicon.ValueNo}}, nameUseAttributeSets: {}, lexicon.AttrType: {}, nameValidation: {values: []string{validationStrict, validationLax, validationPreserve, validationStrip}}, }}, true - case "attribute": + case lexicon.XSLTElementAttribute: return elementDef{attrs: map[string]attrDef{ xslAttrName: {required: true, avt: true}, nameNamespace: {avt: true}, xslAttrSelect: {}, - "separator": {avt: true}, + nameSeparator: {avt: true}, lexicon.AttrType: {}, nameValidation: {values: []string{validationStrict, validationLax, validationPreserve, validationStrip}}, }}, true case lexicon.MethodText: return elementDef{attrs: map[string]attrDef{ - nameDisableOutputEscaping: {values: []string{lexicon.ValueYes, "no"}}, + nameDisableOutputEscaping: {values: []string{lexicon.ValueYes, lexicon.ValueNo}}, }}, true - case "value-of": + case lexicon.XSLTElementValueOf: return elementDef{attrs: map[string]attrDef{ xslAttrSelect: {}, - "separator": {avt: true}, - nameDisableOutputEscaping: {values: []string{lexicon.ValueYes, "no"}}, + nameSeparator: {avt: true}, + nameDisableOutputEscaping: {values: []string{lexicon.ValueYes, lexicon.ValueNo}}, }}, true case fnNameDocument: return elementDef{attrs: map[string]attrDef{ nameValidation: {values: []string{validationStrict, validationLax, validationPreserve, validationStrip}}, lexicon.AttrType: {}, }}, true - case "processing-instruction": + case lexicon.XSLTElementProcessingInstruction: return elementDef{attrs: map[string]attrDef{ xslAttrName: {required: true, avt: true}, xslAttrSelect: {}, @@ -408,28 +421,28 @@ func lookupConstructorXSLTElement(name string) (elementDef, bool) { xslAttrName: {required: true, avt: true}, xslAttrSelect: {}, }}, true - case "comment": + case lexicon.XSLTElementComment: return elementDef{attrs: map[string]attrDef{ xslAttrSelect: {}, }}, true - case "copy": + case lexicon.XSLTElementCopy: return elementDef{attrs: map[string]attrDef{ xslAttrSelect: {since30: true}, - "copy-namespaces": {values: []string{lexicon.ValueYes, "no"}}, - nameInheritNamespaces: {values: []string{lexicon.ValueYes, "no"}}, + nameCopyNamespaces: {values: []string{lexicon.ValueYes, lexicon.ValueNo}}, + nameInheritNamespaces: {values: []string{lexicon.ValueYes, lexicon.ValueNo}}, nameUseAttributeSets: {}, lexicon.AttrType: {}, nameValidation: {values: []string{validationStrict, validationLax, validationPreserve, validationStrip}}, }}, true - case "copy-of": + case lexicon.XSLTElementCopyOf: return elementDef{attrs: map[string]attrDef{ - xslAttrSelect: {required: true}, - "copy-namespaces": {values: []string{lexicon.ValueYes, "no"}}, + xslAttrSelect: {required: true}, + nameCopyNamespaces: {values: []string{lexicon.ValueYes, lexicon.ValueNo}}, // XSLT 3.0 section 18.3: the copy carries the accumulator values of // the node it was copied from, which nothing else can supply — the // copy's own tree is all the document the rules would see. - "copy-accumulators": {since30: true, values: []string{lexicon.ValueYes, "no"}}, - lexicon.AttrType: {}, + nameCopyAccumulators: {since30: true, values: []string{lexicon.ValueYes, lexicon.ValueNo}}, + lexicon.AttrType: {}, nameValidation: {avt: true, values: []string{ validationStrict, validationLax, validationPreserve, validationStrip, }}, @@ -441,28 +454,28 @@ func lookupConstructorXSLTElement(name string) (elementDef, bool) { // The map constructor instructions of section 14. xsl:map takes no // attributes; on xsl:map-entry the key is required and select is the // alternative to a sequence constructor. - case "map": + case lexicon.XSLTElementMap: return elementDef{since30: true, attrs: map[string]attrDef{}}, true - case "map-entry": + case lexicon.XSLTElementMapEntry: return elementDef{since30: true, attrs: map[string]attrDef{ - nameKey2: {required: true}, + keyLocalName: {required: true}, xslAttrSelect: {}, }}, true - case "sequence": + case lexicon.XSLTElementSequence: return elementDef{attrs: map[string]attrDef{ xslAttrSelect: {required: true, optional30: true}, }}, true case lexicon.TypeNumber: return elementDef{attrs: map[string]attrDef{ - "value": {}, + lexicon.AttrValue: {}, xslAttrSelect: {}, - "level": {values: []string{nameSingle, "multiple", "any"}}, - "count": {}, - "from": {}, + nameLevel: {values: []string{nameSingle, numberMultiple, numberAny}}, + lexicon.AttrCount: {}, + lexicon.AttrFrom: {}, nameFormat: {avt: true}, lexicon.AttrLang: {avt: true}, - nameLetterValue: {values: []string{"alphabetic", "traditional"}, avt: true}, - "ordinal": {avt: true}, + nameLetterValue: {values: []string{numberAlphabetic, numberTraditional}, avt: true}, + nameOrdinal: {avt: true}, nameGroupingSeparator: {avt: true}, nameGroupingSize: {avt: true}, nameStartAt: {avt: true, since30: true}, @@ -474,41 +487,41 @@ func lookupConstructorXSLTElement(name string) (elementDef, bool) { func lookupTemplateControlXSLTElement(name string) (elementDef, bool) { switch name { - case "apply-templates": + case lexicon.XSLTElementApplyTemplates: return elementDef{attrs: map[string]attrDef{ xslAttrSelect: {}, lexicon.AttrMode: {}, }}, true - case "apply-imports": + case lexicon.XSLTElementApplyImports: return elementDef{attrs: map[string]attrDef{}}, true - case "next-match": + case lexicon.XSLTElementNextMatch: return elementDef{attrs: map[string]attrDef{}}, true - case "for-each": + case lexicon.XSLTElementForEach: return elementDef{attrs: map[string]attrDef{ xslAttrSelect: {required: true}, }}, true - case "if": + case lexicon.XSLTElementIf: return elementDef{attrs: map[string]attrDef{ lexicon.AttrTest: {required: true}, }}, true - case "choose": + case lexicon.XSLTElementChoose: return elementDef{attrs: map[string]attrDef{}}, true - case "when": + case lexicon.XSLTElementWhen: return elementDef{attrs: map[string]attrDef{ lexicon.AttrTest: {required: true}, }}, true - case "otherwise": + case lexicon.XSLTElementOtherwise: return elementDef{attrs: map[string]attrDef{}}, true - case "call-template": + case lexicon.XSLTElementCallTemplate: return elementDef{attrs: map[string]attrDef{ xslAttrName: {required: true}, }}, true - case "with-param": + case lexicon.XSLTElementWithParam: return elementDef{attrs: map[string]attrDef{ xslAttrName: {required: true}, xslAttrSelect: {}, - "as": {}, - nameTunnel: {values: []string{lexicon.ValueYes, "no"}}, + nameAs: {}, + nameTunnel: {values: []string{lexicon.ValueYes, lexicon.ValueNo}}, }}, true } @@ -516,117 +529,123 @@ func lookupTemplateControlXSLTElement(name string) (elementDef, bool) { } func lookupSequenceControlXSLTElement(name string) (elementDef, bool) { + const ( + nameBindGroup = "bind-group" + nameBindGroupingKey = "bind-grouping-key" + nameForEachStream = "for-each-stream" + ) + switch name { // The engine also supports evaluate and iterate in 2.0 modules through // ordinary XPath evaluation and sequence execution. - case "iterate": + case lexicon.XSLTElementIterate: return elementDef{attrs: map[string]attrDef{ xslAttrSelect: {required: true}, }}, true - case "next-iteration": + case lexicon.XSLTElementNextIteration: return elementDef{attrs: map[string]attrDef{}}, true - case "break": + case lexicon.XSLTElementBreak: return elementDef{attrs: map[string]attrDef{ xslAttrSelect: {}, }}, true - case "on-completion": + case lexicon.XSLTElementOnCompletion: return elementDef{attrs: map[string]attrDef{ xslAttrSelect: {}, }}, true - case "on-empty": + case lexicon.XSLTElementOnEmpty: return elementDef{since30: true, attrs: map[string]attrDef{ xslAttrSelect: {}, }}, true - case "on-non-empty": + case lexicon.XSLTElementOnNonEmpty: return elementDef{since30: true, attrs: map[string]attrDef{ xslAttrSelect: {}, }}, true - case "where-populated": + case lexicon.XSLTElementWherePopulated: return elementDef{since30: true, attrs: map[string]attrDef{}}, true // xsl:fork, section 16. It has no attributes; its content model is the // alternation in 16.1, which compileFork checks. - case "fork": + case lexicon.XSLTElementFork: return elementDef{since30: true, attrs: map[string]attrDef{}}, true // Merge instructions require XSLT 3.0 (§15). - case "merge": + case lexicon.XSLTElementMerge: return elementDef{since30: true, attrs: map[string]attrDef{}}, true - case "merge-source": + case lexicon.XSLTElementMergeSource: return elementDef{since30: true, attrs: map[string]attrDef{ xslAttrName: {}, - "for-each-item": {}, + nameForEachItem: {}, // The Recommendation uses for-each-source. Reject the draft spelling // for-each-stream even under forwards-compatible processing. - "for-each-source": {}, - "for-each-stream": {removed30: true}, + nameForEachSource: {}, + nameForEachStream: {removed30: true}, xslAttrSelect: {required: true}, - nameStreamable: {values: []string{lexicon.ValueYes, "no", lexicon.ValueTrue, lexicon.ValueFalse, "1", "0"}}, + nameStreamable: {values: []string{lexicon.ValueYes, lexicon.ValueNo, lexicon.ValueTrue, lexicon.ValueFalse, booleanDigitTrue, booleanDigitFalse}}, nameUseAccumulators: {}, - "sort-before-merge": { - values: []string{lexicon.ValueYes, "no", lexicon.ValueTrue, lexicon.ValueFalse, "1", "0"}, + nameSortBeforeMerge: { + values: []string{lexicon.ValueYes, lexicon.ValueNo, lexicon.ValueTrue, lexicon.ValueFalse, booleanDigitTrue, booleanDigitFalse}, }, nameValidation: {values: []string{validationStrict, validationLax, validationPreserve, validationStrip}}, lexicon.AttrType: {}, }}, true // Unlike xsl:sort, xsl:merge-key does not allow @stable (§15.5). - case "merge-key": + case lexicon.XSLTElementMergeKey: return elementDef{since30: true, attrs: map[string]attrDef{ xslAttrSelect: {}, lexicon.AttrLang: {avt: true}, - nameOrder: {values: []string{"ascending", "descending"}, avt: true}, + nameOrder: {values: []string{sortOrderAscending, sortOrderDescending}, avt: true}, nameCollation: {avt: true}, nameCaseOrder: {values: []string{nameUpperFirst, nameLowerFirst}, avt: true}, // { "text" | "number" | eqname }: a prefixed name selects an // implementation-defined type, so it passes as eqnameOK. nameDataType: {values: []string{lexicon.MethodText, lexicon.TypeNumber}, eqnameOK: true, avt: true}, }}, true - case "merge-action": + case lexicon.XSLTElementMergeAction: return elementDef{since30: true, attrs: map[string]attrDef{}}, true case nameSort: return elementDef{attrs: map[string]attrDef{ xslAttrSelect: {}, lexicon.AttrLang: {avt: true}, - nameOrder: {values: []string{"ascending", "descending"}, avt: true}, + nameOrder: {values: []string{sortOrderAscending, sortOrderDescending}, avt: true}, nameCollation: {avt: true}, - "stable": {values: []string{lexicon.ValueYes, "no"}, avt: true}, + nameStable: {values: []string{lexicon.ValueYes, lexicon.ValueNo}, avt: true}, nameCaseOrder: {values: []string{nameUpperFirst, nameLowerFirst}, avt: true}, // { "text" | "number" | eqname }: a prefixed name selects an // implementation-defined type, so it passes as eqnameOK. nameDataType: {values: []string{lexicon.MethodText, lexicon.TypeNumber}, eqnameOK: true, avt: true}, }}, true - case "perform-sort": + case lexicon.XSLTElementPerformSort: return elementDef{attrs: map[string]attrDef{ xslAttrSelect: {}, }}, true - case "for-each-group": + case lexicon.XSLTElementForEachGroup: return elementDef{attrs: map[string]attrDef{ xslAttrSelect: {required: true}, - "group-by": {}, - "group-adjacent": {}, - "group-starting-with": {}, - "group-ending-with": {}, + nameGroupBy: {}, + nameGroupAdjacent: {}, + nameGroupStartingWith: {}, + nameGroupEndingWith: {}, nameCollation: {avt: true}, // composite="yes" makes the whole key sequence one grouping key. // XSLT 3.0 only: a 2.0 stylesheet naming it must still be told so. nameComposite: { since30: true, - values: []string{lexicon.ValueYes, "no", lexicon.ValueTrue, lexicon.ValueFalse, "1", "0"}, + values: []string{lexicon.ValueYes, lexicon.ValueNo, lexicon.ValueTrue, lexicon.ValueFalse, booleanDigitTrue, booleanDigitFalse}, }, // These withdrawn binding attributes are unknown in the published REC. - "bind-group": {removed30: true}, - "bind-grouping-key": {removed30: true}, + nameBindGroup: {removed30: true}, + nameBindGroupingKey: {removed30: true}, }}, true - case "analyze-string": + case lexicon.XSLTElementAnalyzeString: return elementDef{attrs: map[string]attrDef{ xslAttrSelect: {required: true}, - "regex": {required: true, avt: true}, - "flags": {avt: true}, + nameRegex: {required: true, avt: true}, + nameFlags: {avt: true}, }}, true - case "matching-substring": + case lexicon.XSLTElementMatchingSubstring: return elementDef{attrs: map[string]attrDef{}}, true - case "non-matching-substring": + case lexicon.XSLTElementNonMatchingSubstring: return elementDef{attrs: map[string]attrDef{}}, true } @@ -634,46 +653,50 @@ func lookupSequenceControlXSLTElement(name string) (elementDef, bool) { } func lookupDynamicXSLTElement(name string) (elementDef, bool) { + const ( + nameStream = "stream" + ) + switch name { - case "evaluate": + case lexicon.XSLTElementEvaluate: return elementDef{attrs: map[string]attrDef{ - "xpath": {required: true}, - "as": {}, - "base-uri": {avt: true}, - "with-params": {}, - nameContextItem: {}, - "namespace-context": {}, - "schema-aware": {avt: true}, + nameXPath: {required: true}, + nameAs: {}, + nameBaseURI: {avt: true}, + nameWithParams: {}, + nameContextItem: {}, + nameNamespaceContext: {}, + nameSchemaAware: {avt: true}, }}, true - case "message": + case lexicon.XSLTElementMessage: return elementDef{attrs: map[string]attrDef{ xslAttrSelect: {}, // Both editions support AVTs. The 2.0 vocabulary is yes/no; a // selected 3.0 processor also accepts the four boolean aliases. - "terminate": {values: []string{lexicon.ValueYes, "no"}, avt: true}, + nameTerminate: {values: []string{lexicon.ValueYes, lexicon.ValueNo}, avt: true}, // @error-code follows the processor's version, not the module's. - "error-code": {processor30: true, avt: true}, + nameErrorCode: {processor30: true, avt: true}, }}, true // xsl:assert requires XSLT 3.0 (§22.2). - case "assert": + case lexicon.XSLTElementAssert: return elementDef{since30: true, attrs: map[string]attrDef{ lexicon.AttrTest: {required: true}, xslAttrSelect: {}, // Written as {eqname} in the summary, so an attribute value template // exactly as it is on xsl:message. - "error-code": {avt: true}, + nameErrorCode: {avt: true}, }}, true case nameFallback: return elementDef{attrs: map[string]attrDef{}}, true - case "try": + case lexicon.XSLTElementTry: return elementDef{since30: true, attrs: map[string]attrDef{ - xslAttrSelect: {}, - "rollback-output": {values: []string{lexicon.ValueYes, "no"}}, + xslAttrSelect: {}, + nameRollbackOutput: {values: []string{lexicon.ValueYes, lexicon.ValueNo}}, }}, true - case "catch": + case lexicon.XSLTElementCatch: return elementDef{since30: true, attrs: map[string]attrDef{ - "errors": {}, + nameErrors: {}, xslAttrSelect: {}, }}, true @@ -681,17 +704,17 @@ func lookupDynamicXSLTElement(name string) (elementDef, bool) { // streamable is accepted, but execution builds a tree (§18.1). // The draft xsl:stream spelling is also supported with its own attribute set; // it has no @streamable attribute. - case "stream": + case nameStream: return elementDef{since30: true, attrs: map[string]attrDef{ lexicon.AttrHref: {required: true, avt: true}, nameUseAccumulators: {}, nameValidation: {values: []string{validationStrict, validationLax, validationPreserve, validationStrip}}, lexicon.AttrType: {}, }}, true - case "source-document": + case lexicon.XSLTElementSourceDocument: return elementDef{since30: true, attrs: map[string]attrDef{ lexicon.AttrHref: {required: true, avt: true}, - nameStreamable: {values: []string{lexicon.ValueYes, "no", lexicon.ValueTrue, lexicon.ValueFalse, "1", "0"}}, + nameStreamable: {values: []string{lexicon.ValueYes, lexicon.ValueNo, lexicon.ValueTrue, lexicon.ValueFalse, booleanDigitTrue, booleanDigitFalse}}, nameUseAccumulators: {}, nameValidation: {values: []string{validationStrict, validationLax, validationPreserve, validationStrip}}, lexicon.AttrType: {}, @@ -703,13 +726,13 @@ func lookupDynamicXSLTElement(name string) (elementDef, bool) { func lookupSerializationXSLTElement(name string) (elementDef, bool) { switch name { - case "result-document": + case lexicon.XSLTElementResultDocument: return elementDef{attrs: map[string]attrDef{ // build-tree says whether the raw result is normalised into a final // result tree or serialised as the sequence it is; see 2.3.6. XSLT // 3.0 added it, along with the two JSON serialisation parameters. - lexicon.SerializationBuildTree: {processor30: true, avt: true, values: []string{lexicon.ValueYes, "no", lexicon.ValueTrue, lexicon.ValueFalse, "1", "0"}}, - lexicon.SerializationAllowDuplicateNames: {processor30: true, avt: true, values: []string{lexicon.ValueYes, "no", lexicon.ValueTrue, lexicon.ValueFalse, "1", "0"}}, + lexicon.SerializationBuildTree: {processor30: true, avt: true, values: []string{lexicon.ValueYes, lexicon.ValueNo, lexicon.ValueTrue, lexicon.ValueFalse, booleanDigitTrue, booleanDigitFalse}}, + lexicon.SerializationAllowDuplicateNames: {processor30: true, avt: true, values: []string{lexicon.ValueYes, lexicon.ValueNo, lexicon.ValueTrue, lexicon.ValueFalse, booleanDigitTrue, booleanDigitFalse}}, // Both enumerate as on xsl:output, plus braces: a literal outside // the set is XTSE0020 here, a computed one XTDE0030 in resultdoc.go. lexicon.SerializationJSONNodeOutputMethod: { @@ -725,23 +748,23 @@ func lookupSerializationXSLTElement(name string) (elementDef, bool) { nameValidation: {values: []string{validationStrict, validationLax, validationPreserve, validationStrip}}, lexicon.AttrType: {}, lexicon.SerializationMethod: {avt: true, eqnameOK: true, values: []string{ - lexicon.MethodXML, lexicon.MethodHTML, lexicon.MethodXHTML, lexicon.MethodText, "json", "adaptive", + lexicon.MethodXML, lexicon.MethodHTML, lexicon.MethodXHTML, lexicon.MethodText, lexicon.MethodJSON, lexicon.MethodAdaptive, }}, - lexicon.SerializationByteOrderMark: {values: []string{lexicon.ValueYes, "no"}, avt: true}, + lexicon.SerializationByteOrderMark: {values: []string{lexicon.ValueYes, lexicon.ValueNo}, avt: true}, lexicon.SerializationCDATASectionElements: {avt: true}, lexicon.SerializationDoctypePublic: {avt: true}, lexicon.SerializationDoctypeSystem: {avt: true}, lexicon.SerializationEncoding: {avt: true}, - lexicon.SerializationEscapeURIAttributes: {values: []string{lexicon.ValueYes, "no"}, avt: true}, - lexicon.SerializationIncludeContentType: {values: []string{lexicon.ValueYes, "no"}, avt: true}, - lexicon.SerializationIndent: {values: []string{lexicon.ValueYes, "no"}, avt: true}, + lexicon.SerializationEscapeURIAttributes: {values: []string{lexicon.ValueYes, lexicon.ValueNo}, avt: true}, + lexicon.SerializationIncludeContentType: {values: []string{lexicon.ValueYes, lexicon.ValueNo}, avt: true}, + lexicon.SerializationIndent: {values: []string{lexicon.ValueYes, lexicon.ValueNo}, avt: true}, lexicon.SerializationMediaType: {avt: true}, lexicon.SerializationNormalizationForm: {avt: true, nmtoken: true}, - lexicon.SerializationOmitXMLDeclaration: {values: []string{lexicon.ValueYes, "no"}, avt: true}, - lexicon.SerializationStandalone: {values: []string{lexicon.ValueYes, "no", lexicon.ValueOmit}, avt: true}, - lexicon.SerializationUndeclarePrefixes: {values: []string{lexicon.ValueYes, "no"}, avt: true}, + lexicon.SerializationOmitXMLDeclaration: {values: []string{lexicon.ValueYes, lexicon.ValueNo}, avt: true}, + lexicon.SerializationStandalone: {values: []string{lexicon.ValueYes, lexicon.ValueNo, lexicon.ValueOmit}, avt: true}, + lexicon.SerializationUndeclarePrefixes: {values: []string{lexicon.ValueYes, lexicon.ValueNo}, avt: true}, lexicon.SerializationUseCharacterMaps: {}, - "output-version": {avt: true}, + nameOutputVersion: {avt: true}, }}, true case nameOutput: return elementDef{attrs: map[string]attrDef{ @@ -756,40 +779,40 @@ func lookupSerializationXSLTElement(name string) (elementDef, bool) { }, // parameter-document is a literal URI reference, not an AVT. lexicon.SerializationParameterDocument: {processor30: true, uri: true}, - lexicon.SerializationAllowDuplicateNames: {processor30: true, values: []string{lexicon.ValueYes, "no", lexicon.ValueTrue, lexicon.ValueFalse, "1", "0"}}, - lexicon.SerializationBuildTree: {processor30: true, values: []string{lexicon.ValueYes, "no", lexicon.ValueTrue, lexicon.ValueFalse, "1", "0"}}, + lexicon.SerializationAllowDuplicateNames: {processor30: true, values: []string{lexicon.ValueYes, lexicon.ValueNo, lexicon.ValueTrue, lexicon.ValueFalse, booleanDigitTrue, booleanDigitFalse}}, + lexicon.SerializationBuildTree: {processor30: true, values: []string{lexicon.ValueYes, lexicon.ValueNo, lexicon.ValueTrue, lexicon.ValueFalse, booleanDigitTrue, booleanDigitFalse}}, lexicon.SerializationHTMLVersion: {processor30: true}, lexicon.SerializationSuppressIndentation: {processor30: true}, // item-separator is applied during sequence normalization (§5.7.1), not // just serialization: inserted text nodes participate in result validation. lexicon.SerializationItemSeparator: {processor30: true}, - lexicon.SerializationByteOrderMark: {values: []string{lexicon.ValueYes, "no"}}, + lexicon.SerializationByteOrderMark: {values: []string{lexicon.ValueYes, lexicon.ValueNo}}, lexicon.SerializationCDATASectionElements: {}, lexicon.SerializationDoctypePublic: {}, lexicon.SerializationDoctypeSystem: {}, lexicon.SerializationEncoding: {}, - lexicon.SerializationEscapeURIAttributes: {values: []string{lexicon.ValueYes, "no"}}, - lexicon.SerializationIncludeContentType: {values: []string{lexicon.ValueYes, "no"}}, - lexicon.SerializationIndent: {values: []string{lexicon.ValueYes, "no"}}, + lexicon.SerializationEscapeURIAttributes: {values: []string{lexicon.ValueYes, lexicon.ValueNo}}, + lexicon.SerializationIncludeContentType: {values: []string{lexicon.ValueYes, lexicon.ValueNo}}, + lexicon.SerializationIndent: {values: []string{lexicon.ValueYes, lexicon.ValueNo}}, lexicon.SerializationMediaType: {}, lexicon.SerializationNormalizationForm: {nmtoken: true}, - lexicon.SerializationOmitXMLDeclaration: {values: []string{lexicon.ValueYes, "no"}}, + lexicon.SerializationOmitXMLDeclaration: {values: []string{lexicon.ValueYes, lexicon.ValueNo}}, // The enumeration retains the 2.0 vocabulary. A selected 3.0 processor // also admits the boolean synonyms in a 2.0 module. - lexicon.SerializationStandalone: {values: []string{lexicon.ValueYes, "no", lexicon.ValueOmit}}, - lexicon.SerializationUndeclarePrefixes: {values: []string{lexicon.ValueYes, "no"}}, + lexicon.SerializationStandalone: {values: []string{lexicon.ValueYes, lexicon.ValueNo, lexicon.ValueOmit}}, + lexicon.SerializationUndeclarePrefixes: {values: []string{lexicon.ValueYes, lexicon.ValueNo}}, lexicon.SerializationUseCharacterMaps: {}, lexicon.SerializationVersion: {}, }}, true - case "character-map": + case lexicon.XSLTElementCharacterMap: return elementDef{attrs: map[string]attrDef{ xslAttrName: {required: true}, lexicon.SerializationUseCharacterMaps: {}, }}, true - case "output-character": + case lexicon.XSLTElementOutputCharacter: return elementDef{attrs: map[string]attrDef{ - "character": {required: true}, - "string": {required: true}, + nameCharacter: {required: true}, + nameOutputCharacterString: {required: true}, }}, true } diff --git a/transform/internal/xslt/instruction.go b/transform/internal/xslt/instruction.go index a61aa38..c33ae07 100644 --- a/transform/internal/xslt/instruction.go +++ b/transform/internal/xslt/instruction.go @@ -136,6 +136,7 @@ func (*elementInst) instructionTag() {} type attributeInst struct { sourceInfo + Namespaces map[string]string // immutable declaration-site namespace bindings Name *avt Namespace *avt Select *xpath.Expression diff --git a/transform/internal/xslt/invocation.go b/transform/internal/xslt/invocation.go index bb5b973..59fab56 100644 --- a/transform/internal/xslt/invocation.go +++ b/transform/internal/xslt/invocation.go @@ -31,7 +31,7 @@ const ( func (m OnMultipleMatchMode) String() string { switch m { case OnMultipleMatchUseLast: - return "use-last" + return onMultipleMatchUseLast case OnMultipleMatchFail: return onNoMatchFail default: @@ -653,24 +653,27 @@ type Execution struct { // terminal method. The mutex lives behind a pointer so copying // invocationConfig (in clone) never copies a lock. type resolvedOutputState struct { - mu sync.Mutex + mu sync.Mutex + // def is an immutable owned snapshot; store only replaces the pointer. def *OutputDef } // store records the resolved primary output def. It deep-clones so the stored // snapshot is independent of the per-call transformConfig. func (r *resolvedOutputState) store(def *OutputDef) { - r.mu.Lock() - defer r.mu.Unlock() + def = cloneOutputDef(def) - r.def = cloneOutputDef(def) + r.mu.Lock() + r.def = def + r.mu.Unlock() } // snapshot returns an independent clone of the recorded output def, or nil if // no terminal method has recorded one yet. func (r *resolvedOutputState) snapshot() *OutputDef { r.mu.Lock() - defer r.mu.Unlock() + def := r.def + r.mu.Unlock() - return cloneOutputDef(r.def) + return cloneOutputDef(def) } diff --git a/transform/internal/xslt/invocation_output_test.go b/transform/internal/xslt/invocation_output_test.go new file mode 100644 index 0000000..969b6c2 --- /dev/null +++ b/transform/internal/xslt/invocation_output_test.go @@ -0,0 +1,48 @@ +package xslt //nolint:testpackage // Exercises concurrent ownership of recorded output definitions. + +import ( + "sync" + "testing" +) + +func TestResolvedOutputConcurrentSnapshotOwnership(t *testing.T) { + t.Parallel() + + var state resolvedOutputState + var workers sync.WaitGroup + + for range 8 { + workers.Go(func() { + for range 64 { + separator := "original" + def := &OutputDef{ + ResolvedCharMap: map[rune]string{'x': "original"}, + UseCharacterMaps: []string{"original"}, + ItemSeparator: &separator, + } + state.store(def) + + def.ResolvedCharMap['x'] = "changed input" + def.UseCharacterMaps[0] = "changed input" + separator = "changed input" + + snapshot := state.snapshot() + if snapshot == nil || snapshot.ResolvedCharMap['x'] != "original" || len(snapshot.UseCharacterMaps) != 1 || snapshot.UseCharacterMaps[0] != "original" || snapshot.ItemSeparator == nil || *snapshot.ItemSeparator != "original" { + t.Error("recorded output changed through another caller's input or snapshot") + return + } + + snapshot.ResolvedCharMap['x'] = "changed snapshot" + snapshot.UseCharacterMaps[0] = "changed snapshot" + *snapshot.ItemSeparator = "changed snapshot" + } + }) + } + + workers.Wait() + + snapshot := state.snapshot() + if snapshot.ResolvedCharMap['x'] != "original" || snapshot.UseCharacterMaps[0] != "original" || *snapshot.ItemSeparator != "original" { + t.Fatal("recorded output retained mutable snapshot storage") + } +} diff --git a/transform/internal/xslt/invocation_resources.go b/transform/internal/xslt/invocation_resources.go new file mode 100644 index 0000000..df3d102 --- /dev/null +++ b/transform/internal/xslt/invocation_resources.go @@ -0,0 +1,74 @@ +package xslt + +import ( + "context" + + "github.com/skarm/xmlsec/transform/internal/xdm" + "github.com/skarm/xmlsec/transform/internal/xpath" +) + +// InvocationResources is the live resource policy for one language invocation. +// Nil resolvers explicitly deny access; static language context remains captured +// by the compiled program or function value. +type InvocationResources struct { + URIResolver xpath.URIResolver + Packages PackageResolver + Collections xpath.CollectionResolver + ParseOptions xdm.ParseOptions + MaxResourceBytes int64 +} + +type invocationResourcesKey struct{} + +// WithInvocationResources supplies caller policy without retaining mutable +// execution caches or changing the lexical context of a saved function. +func WithInvocationResources(ctx context.Context, resources InvocationResources) context.Context { + return context.WithValue(ctx, invocationResourcesKey{}, resources) +} + +func invocationResources(ctx context.Context) (InvocationResources, bool) { + resources, ok := ctx.Value(invocationResourcesKey{}).(InvocationResources) + return resources, ok +} + +func rebaseFunctionResources(ctx context.Context, cfg *transformConfig) { + resources, ok := invocationResources(ctx) + if !ok || cfg == nil { + return + } + + cfg.uriResolver = resources.URIResolver + cfg.collectionResolver = resources.Collections + cfg.maxResourceBytes = resources.MaxResourceBytes + options := resources.ParseOptions + cfg.parseOptions = &options +} + +func (cfg *transformFnConfig) invocationConfig(ctx context.Context) *transformFnConfig { + resources, ok := invocationResources(ctx) + if !ok { + return cfg + } + + local := *cfg + local.innerURIResolver = resources.URIResolver + local.stylesheetResolver = resources.URIResolver + local.packageResolver = resources.Packages + local.collectionResolver = resources.Collections + local.maxResourceBytes = resources.MaxResourceBytes + options := resources.ParseOptions + local.parseOptions = &options + + compiler := NewCompiler().MaxResourceBytes(local.maxResourceBytes).ParseOptions(options) + if local.stylesheetResolver != nil { + compiler = compiler.URIResolver(local.stylesheetResolver) + } + + if local.packageResolver != nil { + compiler = compiler.PackageResolver(local.packageResolver) + } + + local.nestedCompiler = compiler + + return &local +} diff --git a/transform/internal/xslt/keys.go b/transform/internal/xslt/keys.go index 26ec4dd..7d17d22 100644 --- a/transform/internal/xslt/keys.go +++ b/transform/internal/xslt/keys.go @@ -6,8 +6,6 @@ package xslt import ( "context" "fmt" - "math" - "strconv" "strings" "time" @@ -16,6 +14,13 @@ import ( "github.com/skarm/xmlsec/transform/internal/xpath" ) +// Invocation-local canonical key encoding: family tags retain broad typed-comparison candidates, NUL separates components, and table identity includes the document and owning package. These bytes are not an external wire format. +const ( + canonicalKeySeparator = "\u0000" + canonicalNumericBucket = "N" + canonicalStringPrefix = "S:" +) + type keyEntry struct { key xdm.AtomicValue node xdm.Node @@ -28,20 +33,33 @@ type compositeKeyEntry struct { node xdm.Node } -type keyTable struct { - defs []*keyDef - entries map[string][]keyEntry // canonicalKey -> entries (non-composite) - compositeEntry map[string][]compositeKeyEntry // compositeCanonical -> entries (composite) - collationKey func(string) string // collation key function (nil = codepoint) - collationCmp func(string, string) int // collation compare function (nil = codepoint) - compat bool // any definition enables backwards-compatible processing - building bool - built bool -} +// Build the pure-int64 scalar index only after repeated lookups in a sufficiently +// large bucket. These measured setup thresholds are a memory/work compromise, +// not a maximum key count or an asserted optimum. Mixed/composite keys retain +// generic typed refinement and invocation-local ownership. +const ( + keyIntegerIndexMinEntries = 64 + keyIntegerIndexLookups = 3 +) -func newKeyTable(defs []*keyDef) (*keyTable, error) { +type keyTable struct { + entries map[string][]keyEntry // canonicalKey -> entries (non-composite) + compositeEntry map[string][]compositeKeyEntry // compositeCanonical -> entries (composite) + collationKey func(string) string // collation key function (nil = codepoint) + collationCmp func(string, string) int // collation compare function (nil = codepoint) + implicitTimezone *time.Location + integerIndex map[int64][]int + defs []*keyDef + compat bool // any definition enables backwards-compatible processing + building bool + built bool + integerLookups uint8 + integerDisabled bool +} + +func newKeyTable(defs []*keyDef, implicitTimezone *time.Location) (*keyTable, error) { table := &keyTable{ - defs: defs, entries: make(map[string][]keyEntry), + defs: defs, implicitTimezone: implicitTimezone, entries: make(map[string][]keyEntry), compat: !defs[0].Composite && keyDefinitionsCompatible(defs), building: true, } if err := buildKeyTableCollation(defs, table); err != nil { @@ -108,9 +126,9 @@ func keyCompatCast(av xdm.AtomicValue, xpath10 bool) xdm.AtomicValue { // collationCanonicalKey produces a canonical key using the collation key // function for string/untypedAtomic types. Falls back to canonicalKey // for non-string types. -func collationCanonicalKey(av xdm.AtomicValue, keyFn func(string) string) string { +func collationCanonicalKey(av xdm.AtomicValue, keyFn func(string) string, implicitTimezone *time.Location) string { if keyFn == nil { - return canonicalKey(av) + return canonicalKey(av, implicitTimezone) } switch av.TypeName { @@ -121,9 +139,9 @@ func collationCanonicalKey(av xdm.AtomicValue, keyFn func(string) string) string xdm.TypeENTITY, xdm.TypeID, xdm.TypeIDREF, xdm.TypeIDREFS, xdm.TypeAnyURI: s, _ := xpath.AtomicToString(av) - return "S:" + keyFn(s) + return canonicalStringPrefix + keyFn(s) default: - return canonicalKey(av) + return canonicalKey(av, implicitTimezone) } } @@ -131,46 +149,50 @@ func collationCanonicalKey(av xdm.AtomicValue, keyFn func(string) string) string // into the same bucket. Typed comparison refines matches within a bucket. // Values that could be equal under XPath eq semantics (including // untypedAtomic promotion) must share the same canonical key. -func canonicalKey(av xdm.AtomicValue) string { +func canonicalKey(av xdm.AtomicValue, implicitTimezone *time.Location) string { + const ( + canonicalDatePrefix = "D:" + canonicalDateTimePrefix = "DT:" + canonicalDurationPrefix = "DU:" + canonicalTimePrefix = "T:" + ) + + if duration, ok := av.Value.(xdm.Duration); ok { + return canonicalDurationPrefix + xdm.DurationToRat(duration, true).RatString() + ":" + xdm.DurationToRat(duration, false).RatString() + } + if qv, ok := canonicalQNameValue(av); ok { // QName equality is based on URI and local name, not lexical prefix. - return xdm.TypeQName + ":" + qv.URI + "\x00" + qv.Local + return xdm.TypeQName + ":" + qv.URI + canonicalKeySeparator + qv.Local } - // For date/time types, normalize to UTC so that equivalent instants - // in different timezones share the same canonical key. - switch av.TypeName { + // Use the same reference dates and invocation timezone as typed equality. + temporal := xdm.PromoteSchemaType(av) + switch temporal.TypeName { case xdm.TypeDateTime, xdm.TypeDateTimeStamp: - if t, ok := av.Value.(time.Time); ok { - return "DT:" + t.UTC().Format(time.RFC3339Nano) + if t, ok := temporal.Value.(time.Time); ok { + return canonicalDateTimePrefix + xpath.ApplyImplicitTZ(t, implicitTimezone).UTC().Format(time.RFC3339Nano) } case xdm.TypeDate: - if t, ok := av.Value.(time.Time); ok { - return "D:" + t.UTC().Format("2006-01-02Z07:00") + if t, ok := temporal.Value.(time.Time); ok { + return canonicalDatePrefix + xpath.ApplyImplicitTZ(t, implicitTimezone).UTC().Format(time.RFC3339Nano) } case xdm.TypeTime: - if t, ok := av.Value.(time.Time); ok { - return "T:" + t.UTC().Format("15:04:05.999999999Z07:00") + if t, ok := temporal.Value.(time.Time); ok { + return canonicalTimePrefix + xpath.TimeToReferenceDateTime(xpath.ApplyImplicitTZ(t, implicitTimezone)).UTC().Format(time.RFC3339Nano) + } + case xdm.TypeGDay, xdm.TypeGMonth, xdm.TypeGMonthDay, xdm.TypeGYear, xdm.TypeGYearMonth: + if t, ok := xpath.GregorianToReferenceDateTime(temporal); ok { + return temporal.TypeName + ":" + xpath.ApplyImplicitTZ(t, implicitTimezone).UTC().Format(time.RFC3339Nano) } } s, _ := xpath.AtomicToString(av) - // Group numerics together so integer 4 and double 4.0 can be - // compared in the same bucket. The bucket deliberately loses precision; - // typed comparison must refine collisions before a node is selected. + // Numeric equality can promote exact values to either float or double. + // Double rounding can separate equal values even in a float32 hash, so + // retain all numeric candidates and refine them by typed comparison. if av.IsNumeric() { - // Value comparison can promote either operand to xs:float. Keep all - // potentially equal values together, then refine using their original types. - f := float64(float32(av.ToFloat64())) - if math.IsNaN(f) { - return "N:NaN" - } - - if f == 0 { - return "N:0" - } - - return "N:" + strconv.FormatFloat(f, 'g', -1, 64) + return canonicalNumericBucket } // Group untypedAtomic, string, and all string-derived types together // since untypedAtomic is promoted to string for eq comparison. @@ -181,12 +203,22 @@ func canonicalKey(av xdm.AtomicValue) string { xdm.TypeNMTOKEN, xdm.TypeNMTOKENS, xdm.TypeENTITY, xdm.TypeID, xdm.TypeIDREF, xdm.TypeIDREFS, xdm.TypeAnyURI: - return "S:" + s + return canonicalStringPrefix + s } return av.TypeName + ":" + s } +// indexInt64 accepts only exact integer values, without numeric promotion. +func indexInt64(value xdm.AtomicValue) (int64, bool) { + value = xdm.PromoteSchemaType(value) + if !xdm.IsIntegerDerived(value.TypeName) { + return 0, false + } + + return value.Int64Val() +} + func canonicalQNameValue(av xdm.AtomicValue) (xdm.QNameValue, bool) { if qv, ok := av.Value.(xdm.QNameValue); ok { return qv, true @@ -205,24 +237,28 @@ func canonicalQNameValue(av xdm.AtomicValue) (xdm.QNameValue, bool) { // compositeCanonicalKey produces a canonical key for a sequence of atomic values // used as a composite key. Individual canonical keys are joined with NUL bytes. // When keyFn is non-nil, string-like values use the collation key function. -func compositeCanonicalKey(avs []xdm.AtomicValue, keyFn func(string) string) string { +func compositeCanonicalKey(avs []xdm.AtomicValue, keyFn func(string) string, implicitTimezone *time.Location) string { parts := make([]string, len(avs)) for i, av := range avs { - parts[i] = collationCanonicalKey(av, keyFn) + parts[i] = collationCanonicalKey(av, keyFn, implicitTimezone) } - return strings.Join(parts, "\x00") + return strings.Join(parts, canonicalKeySeparator) } // buildKeyTable builds or retrieves a key table for the given key name // scoped to the given document root. func (ec *execContext) buildKeyTable(ctx context.Context, name string, root xdm.Node) (*keyTable, error) { + const ( + keyTableIdentityFormat = "%s@%p@%p" + ) + if ec.keyTables == nil { ec.keyTables = make(map[string]*keyTable) } // Isolate each key by document identity and owning package. - cacheKey := fmt.Sprintf("%s@%p@%p", name, root, ec.currentPackage) + cacheKey := fmt.Sprintf(keyTableIdentityFormat, name, root, ec.currentPackage) table, err := ec.cachedKeyTable(cacheKey, name) if err != nil { @@ -238,7 +274,7 @@ func (ec *execContext) buildKeyTable(ctx context.Context, name string, root xdm. return nil, dynamicError(errCodeXTDE1260, "unknown key %q", name) } - table, err = newKeyTable(defs) + table, err = newKeyTable(defs, ec.dynamicProperties.Timezone) if err != nil { return nil, err } @@ -354,7 +390,7 @@ func (ec *execContext) lookupKey(ctx context.Context, name string, value xdm.Ato // Backwards-compatible key: the lookup value is compared as a string. if kt.compat { - value = keyCompatCast(value, ec.stylesheet.processorLanguage() == 10) + value = keyCompatCast(value, ec.stylesheet.processorLanguage() == processorXSLT10) } // NaN lookup never matches. @@ -362,7 +398,7 @@ func (ec *execContext) lookupKey(ctx context.Context, name string, value xdm.Ato return nil, nil } - ck := collationCanonicalKey(value, kt.collationKey) + ck := collationCanonicalKey(value, kt.collationKey, kt.implicitTimezone) if err := xpath.ChargeString(ctx, len(ck)); err != nil { return nil, err } @@ -372,15 +408,101 @@ func (ec *execContext) lookupKey(ctx context.Context, name string, value xdm.Ato return nil, nil } + var ( + positions []int + indexed bool + ) + if ck == canonicalNumericBucket { + positions, indexed, err = kt.integerCandidates(ctx, value, candidates) + if err != nil { + return nil, err + } + } + + return kt.filterCandidates(ctx, value, candidates, positions, indexed) +} + +// integerCandidates builds an invocation-local index only after repeated exact +// integer lookups. Other numeric queries still examine the complete N bucket. +func (kt *keyTable) integerCandidates(ctx context.Context, value xdm.AtomicValue, candidates []keyEntry) ([]int, bool, error) { + if kt.integerDisabled || kt.compat || kt.compositeEntry != nil || len(candidates) < keyIntegerIndexMinEntries { + return nil, false, nil + } + + if len(candidates) > xpath.NodeLimit(ctx, xdm.DefaultMaxNodeSetLength) { + kt.integerDisabled = true + + return nil, false, nil + } + + key, eligible := indexInt64(value) + if !eligible { + return nil, false, nil + } + + if err := xpath.Charge(ctx, 1); err != nil { + return nil, false, err + } + + if kt.integerIndex != nil { + return kt.integerIndex[key], true, nil + } + + if kt.integerLookups < keyIntegerIndexLookups { + kt.integerLookups++ + } + + if kt.integerLookups < keyIntegerIndexLookups { + return nil, false, nil + } + + index := make(map[int64][]int) + + for position, entry := range candidates { + if err := xpath.Charge(ctx, 1); err != nil { + return nil, false, err + } + + entryKey, ok := indexInt64(entry.key) + if !ok { + kt.integerDisabled = true + + return nil, false, nil + } + + index[entryKey] = append(index[entryKey], position) + } + + if err := ctx.Err(); err != nil { + return nil, false, err + } + + kt.integerIndex = index + + return index[key], true, nil +} + +func (kt *keyTable) filterCandidates(ctx context.Context, value xdm.AtomicValue, candidates []keyEntry, positions []int, indexed bool) ([]xdm.Node, error) { // Filter candidates using typed eq comparison (collation-aware). var result []xdm.Node - for _, entry := range candidates { + count := len(candidates) + if indexed { + count = len(positions) + } + + for i := range count { if err := xpath.Charge(ctx, 1); err != nil { return nil, err } - if collationAtomicEquals(value, entry.key, kt.collationCmp) { + position := i + if indexed { + position = positions[i] + } + + entry := candidates[position] + if collationAtomicEquals(value, entry.key, kt.collationCmp, kt.implicitTimezone) { result = append(result, entry.node) } } @@ -425,7 +547,7 @@ func (ec *execContext) lookupCompositeKey(ctx context.Context, name string, valu return nil, nil } - ck := compositeCanonicalKey(values, kt.collationKey) + ck := compositeCanonicalKey(values, kt.collationKey, kt.implicitTimezone) if err := xpath.ChargeString(ctx, len(ck)); err != nil { return nil, err } @@ -444,7 +566,7 @@ func (ec *execContext) lookupCompositeKey(ctx context.Context, name string, valu return nil, err } - if compositeAtomicEquals(values, entry.keys, kt.collationCmp) { + if compositeAtomicEquals(values, entry.keys, kt.collationCmp, kt.implicitTimezone) { if _, duplicate := seen[entry.node]; !duplicate { seen[entry.node] = struct{}{} result = append(result, entry.node) @@ -575,13 +697,13 @@ func collectInScopeNSNodes(elem *xdm.Element) []xdm.Node { // compositeAtomicEquals tests whether two composite keys are equal element-by-element. // When cmpFn is non-nil, string-like values use the collation compare function. -func compositeAtomicEquals(a, b []xdm.AtomicValue, cmpFn func(string, string) int) bool { +func compositeAtomicEquals(a, b []xdm.AtomicValue, cmpFn func(string, string) int, implicitTimezone *time.Location) bool { if len(a) != len(b) { return false } for i := range a { - if !(a[i].IsNaN() && b[i].IsNaN()) && !collationAtomicEquals(a[i], b[i], cmpFn) { + if !(a[i].IsNaN() && b[i].IsNaN()) && !collationAtomicEquals(a[i], b[i], cmpFn, implicitTimezone) { return false } } @@ -591,22 +713,21 @@ func compositeAtomicEquals(a, b []xdm.AtomicValue, cmpFn func(string, string) in // collationAtomicEquals tests whether two atomic values are equal using // the collation compare function for string/untypedAtomic types. -func collationAtomicEquals(a, b xdm.AtomicValue, cmpFn func(string, string) int) bool { - if cmpFn == nil { - return xpath.AtomicEquals(a, b) - } +func collationAtomicEquals(a, b xdm.AtomicValue, cmpFn func(string, string) int, implicitTimezone *time.Location) bool { // Only use collation for string-like types aIsStr := isStringLikeType(a.TypeName) bIsStr := isStringLikeType(b.TypeName) - if aIsStr && bIsStr { + if cmpFn != nil && aIsStr && bIsStr { sa, _ := xpath.AtomicToString(a) sb, _ := xpath.AtomicToString(b) return cmpFn(sa, sb) == 0 } - return xpath.AtomicEquals(a, b) + equal, err := xpath.ValueCompareWithImplicitTimezone(xpath.TokenEq, a, b, implicitTimezone) + + return err == nil && equal } func isStringLikeType(tn string) bool { @@ -670,12 +791,12 @@ func (b *keyIndexBuilder) indexDefinition(ctx context.Context, def *keyDef, node if b.table.compat { for i, atom := range atoms { - atoms[i] = keyCompatCast(atom, b.ec.stylesheet.processorLanguage() == 10) + atoms[i] = keyCompatCast(atom, b.ec.stylesheet.processorLanguage() == processorXSLT10) } } if b.composite { - key := compositeCanonicalKey(atoms, b.table.collationKey) + key := compositeCanonicalKey(atoms, b.table.collationKey, b.table.implicitTimezone) if record, err := b.record(ctx, key, node, atoms); err != nil { return err } else if record { @@ -690,7 +811,7 @@ func (b *keyIndexBuilder) indexDefinition(ctx context.Context, def *keyDef, node continue } - key := collationCanonicalKey(atom, b.table.collationKey) + key := collationCanonicalKey(atom, b.table.collationKey, b.table.implicitTimezone) if record, err := b.record(ctx, key, node, []xdm.AtomicValue{atom}); err != nil { return err } else if record { @@ -702,7 +823,11 @@ func (b *keyIndexBuilder) indexDefinition(ctx context.Context, def *keyDef, node } func (b *keyIndexBuilder) record(ctx context.Context, canonical string, node xdm.Node, atoms []xdm.AtomicValue) (bool, error) { - if err := xpath.ChargeString(ctx, len(canonical), 32); err != nil { + const ( + keyRecordAccountingBytes = 32 + ) + + if err := xpath.ChargeString(ctx, len(canonical), keyRecordAccountingBytes); err != nil { return false, err } @@ -713,7 +838,7 @@ func (b *keyIndexBuilder) record(ctx context.Context, canonical string, node xdm } // Cross-type numeric equality is not transitive: removing an equal // xs:float can lose a later integer match. Deduplicate equal types only. - if sameKeyValueTypes(atoms, previous) && compositeAtomicEquals(atoms, previous, b.table.collationCmp) { + if sameKeyValueTypes(atoms, previous) && compositeAtomicEquals(atoms, previous, b.table.collationCmp, b.table.implicitTimezone) { return false, nil } } diff --git a/transform/internal/xslt/number_words.go b/transform/internal/xslt/number_words.go index 99537b8..027db20 100644 --- a/transform/internal/xslt/number_words.go +++ b/transform/internal/xslt/number_words.go @@ -9,6 +9,24 @@ import ( "unicode" ) +// German eight lexical data shared by the cardinal table and irregular ordinal stem; it is not an XSLT attribute or selector. +const ( + germanEight = "acht" +) + +// Internal casing mode passed from a word-number picture to spellout; it is independent of sort case-order or language selection. +const ( + wordCaseTitle = "title" + wordCaseUpper = "upper" +) + +// Normalized base language tags supported by the existing word-number dispatch; unsupported tags continue to select its English fallback. +const ( + numberLanguageFrench = "fr" + numberLanguageGerman = "de" + numberLanguageItalian = "it" +) + // germanOne is the German cardinal for the digit one ("eins"). Used in the // ones-table and in special-case logic that switches to the prefix form "ein" // when followed by another component. @@ -25,7 +43,7 @@ func minInt64MagnitudeString(n int64) (string, bool) { return "", false } - return strconv.FormatUint(uint64(1)<<63, 10), true + return strconv.FormatUint(uint64(1)<<63, numberDecimalRadix), true } // numberToWordsLang converts a number to words in the given language and case. @@ -46,9 +64,9 @@ func numberToWordsLang(n int64, caseMode string, lang string, ordinal string) st } switch caseMode { - case "upper": + case wordCaseUpper: return germanAwareToUpper(result) - case "title": + case wordCaseTitle: return toTitleCase(result) default: return result @@ -77,11 +95,11 @@ func toTitleCase(s string) string { func numberToCardinalWords(n int64, lang string) string { switch lang { - case "de": + case numberLanguageGerman: return germanCardinal(n) - case "it": + case numberLanguageItalian: return italianCardinal(n) - case "fr": + case numberLanguageFrench: return frenchCardinal(n) default: return numberToWords(n, false) @@ -90,11 +108,11 @@ func numberToCardinalWords(n int64, lang string) string { func numberToOrdinalWords(n int64, lang string, ordinal string) string { switch lang { - case "de": + case numberLanguageGerman: return germanOrdinal(n, ordinal) - case "it": + case numberLanguageItalian: return italianOrdinal(n, ordinal) - case "fr": + case numberLanguageFrench: return frenchOrdinal(n, ordinal) default: return englishOrdinal(n) @@ -188,7 +206,7 @@ func englishOnesOrdinal(n int64) string { return ords[int(n)] } - return strconv.FormatInt(n, 10) + "th" + return strconv.FormatInt(n, numberDecimalRadix) + "th" } func englishTensOrdinal(n int64) string { @@ -200,7 +218,7 @@ func englishTensOrdinal(n int64) string { return ords[int(n)] } - return strconv.FormatInt(n*10, 10) + "th" + return strconv.FormatInt(n*10, numberDecimalRadix) + "th" } // ============================================================ @@ -259,12 +277,12 @@ func germanCardinal(n int64) string { return w } - return strconv.FormatInt(n, 10) + return strconv.FormatInt(n, numberDecimalRadix) } func germanCardinalUnderThousand(n int64) string { ones := []string{ - "", germanOne, "zwei", "drei", "vier", "fünf", "sechs", "sieben", nameAcht, "neun", + "", germanOne, "zwei", "drei", "vier", "fünf", "sechs", "sieben", germanEight, "neun", "zehn", "elf", "zwölf", "dreizehn", "vierzehn", "fünfzehn", "sechzehn", "siebzehn", "achtzehn", "neunzehn", } tens := []string{"", "", "zwanzig", "dreißig", "vierzig", "fünfzig", "sechzig", "siebzig", "achtzig", "neunzig"} @@ -362,7 +380,7 @@ func germanOrdinalStem(n int64) string { if n >= 100 { q := n / 100 r := n % 100 - ones := []string{"", germanOne, "zwei", "drei", "vier", "fünf", "sechs", "sieben", nameAcht, "neun"} + ones := []string{"", germanOne, "zwei", "drei", "vier", "fünf", "sechs", "sieben", germanEight, "neun"} w := ones[int(q)] if w == germanOne { @@ -386,7 +404,7 @@ func germanOrdinalStem(n int64) string { case 7: return "siebt" case 8: - return nameAcht + return germanEight } card := germanCardinal(n) @@ -475,16 +493,20 @@ func italianCardinal(n int64) string { return w } - return strconv.FormatInt(n, 10) + return strconv.FormatInt(n, numberDecimalRadix) } func italianOrdinal(n int64, ordinal string) string { + const ( + ordinalFeminineHint = "feminine" + ) + if n <= 0 { return italianCardinal(n) } // Detect gender from ordinal hint - feminine := strings.Contains(ordinal, "feminine") + feminine := strings.Contains(ordinal, ordinalFeminineHint) // Italian ordinals 1-10 are irregular if n <= 10 { @@ -592,7 +614,7 @@ func frenchCardinal(n int64) string { return w } - return strconv.FormatInt(n, 10) + return strconv.FormatInt(n, numberDecimalRadix) } func frenchOrdinal(n int64, _ string) string { diff --git a/transform/internal/xslt/output.go b/transform/internal/xslt/output.go index 927534f..799b521 100644 --- a/transform/internal/xslt/output.go +++ b/transform/internal/xslt/output.go @@ -25,6 +25,33 @@ import ( "github.com/skarm/xmlsec/transform/internal/xpath" ) +// Bounded reference-decoder states for numeric prefix, predefined entity name and digits; these tags do not change the XML characters accepted by the parser. +const ( + referencePhaseDigits = 'd' +) + +// Internal fully-normalized preview states. normalizationModeText denotes +// character data; input markup characters are tested independently of these tags. +const ( + normalizationModeText = 0 + normalizationModeCDATA = 'd' + normalizationModeDeclarationStart = '!' + normalizationModePI = 'p' + normalizationModeRawHTML = 'r' + normalizationModeTag = 't' +) + +// Fully-normalized output charges one work unit per this many source bytes, plus the existing per-call unit; this is not a size ceiling. +const ( + normalizationWorkBytesPerUnit = 64 +) + +// Namespace-state categories in the fully-normalized HTML preview; XHTML is treated as HTML only under the existing HTML5 selection. +const ( + htmlNamespaceAbsent = 0 + htmlNamespaceXHTML = 1 +) + // SerializeItems writes a sequence of items (maps, arrays, atomics, nodes) // using the specified output definition's method (json or adaptive). // This is used for result-documents with method="json" or method="adaptive". @@ -77,7 +104,11 @@ func SerializeResult(w io.Writer, doc *xdm.Document, outDef *OutputDef) error { // serializeItemsWithSeparator serializes a sequence of items using the specified // output method, joining them with the item-separator. func serializeItemsWithSeparator(w io.Writer, items xdm.Sequence, _ *xdm.Document, outDef *OutputDef) error { - sep := "\n" + const ( + defaultSeparatedItemSeparator = "\n" + ) + + sep := defaultSeparatedItemSeparator if outDef.ItemSeparator != nil { sep = *outDef.ItemSeparator } else if outDef.ItemSeparatorAbsent { @@ -219,7 +250,7 @@ func serializeResult(ctx context.Context, w io.Writer, doc *xdm.Document, outDef } enc := strings.ToLower(outDef.Encoding) - isUTF16 := enc == lexicon.EncodingUTF16 || enc == nameUtf16 + isUTF16 := enc == lexicon.EncodingUTF16 || enc == nameUTF16 needsEncodingConversion := enc != "" && enc != lexicon.EncodingUTF8 && enc != lexicon.EncodingUTF8Alt && !isUTF16 needsNormalization := outDef.NormalizationForm != "" && outDef.NormalizationForm != normalizationFormNone @@ -395,7 +426,7 @@ func validateSerializationParams(ctx context.Context, outDef *OutputDef, doc *xd } // SEPM0010: undeclare-prefixes="yes" with version="1.0" - if outDef.UndeclarePrefixes && (method == lexicon.MethodXML || method == lexicon.MethodXHTML) && (outDef.Version == "" || outDef.Version == lexicon.XSLTVersion10) { + if outDef.UndeclarePrefixes && (method == lexicon.MethodXML || method == lexicon.MethodXHTML) && (outDef.Version == "" || outDef.Version == lexicon.XMLVersion10) { return dynamicError(lexicon.ErrSEPM0010, "undeclare-prefixes=\"yes\" is not allowed with version=\"1.0\"") } @@ -405,7 +436,7 @@ func validateSerializationParams(ctx context.Context, outDef *OutputDef, doc *xd } enc := strings.ToLower(outDef.Encoding) - if enc != "" && enc != lexicon.EncodingUTF8 && enc != lexicon.EncodingUTF8Alt && enc != lexicon.EncodingUTF16 && enc != nameUtf16 { + if enc != "" && enc != lexicon.EncodingUTF8 && enc != lexicon.EncodingUTF8Alt && enc != lexicon.EncodingUTF16 && enc != nameUTF16 { _, encErr := htmlindex.Get(enc) if encErr != nil { return dynamicError(errCodeSESU0007, @@ -421,7 +452,7 @@ func validateSerializationParams(ctx context.Context, outDef *OutputDef, doc *xd // SESU0011: unsupported normalization-form if outDef.NormalizationForm != "" && outDef.NormalizationForm != normalizationFormNone { switch outDef.NormalizationForm { - case normalizationFormNFC, "NFD", "NFKC", "NFKD", lexicon.NormFullyNormalized: + case lexicon.NormalizationFormNFC, lexicon.NormalizationFormNFD, lexicon.NormalizationFormNFKC, lexicon.NormalizationFormNFKD, lexicon.NormFullyNormalized: // supported default: return dynamicError(lexicon.ErrSESU0011, @@ -468,11 +499,11 @@ func isValidPublicID(s string) bool { return xmloutput.ValidPublicID(s) } func checkFullyNormalized(ctx context.Context, doc *xdm.Document, definition *OutputDef) error { preview := cloneOutputDef(definition) - preview.NormalizationForm = normalizationFormNFC + preview.NormalizationForm = lexicon.NormalizationFormNFC if len(preview.ResolvedCharMap) != 0 { preview.ResolvedCharMap = make(map[rune]string, len(definition.ResolvedCharMap)) for character, replacement := range definition.ResolvedCharMap { - if err := xpath.Charge(ctx, 1+len(replacement)/64); err != nil { + if err := xpath.Charge(ctx, 1+len(replacement)/normalizationWorkBytesPerUnit); err != nil { return err } // Map-generated references are deliberately opaque (SER 5.1.9). @@ -507,7 +538,7 @@ type fullyNormalizedWriter struct { } func (writer *fullyNormalizedWriter) Write(p []byte) (int, error) { - if err := xpath.Charge(writer.ctx, 1+len(p)/64); err != nil { + if err := xpath.Charge(writer.ctx, 1+len(p)/normalizationWorkBytesPerUnit); err != nil { return 0, err } @@ -521,7 +552,7 @@ func (writer *fullyNormalizedWriter) Write(p []byte) (int, error) { } func (writer *fullyNormalizedWriter) WriteString(s string) (int, error) { - if err := xpath.Charge(writer.ctx, 1+len(s)/64); err != nil { + if err := xpath.Charge(writer.ctx, 1+len(s)/normalizationWorkBytesPerUnit); err != nil { return 0, err } @@ -543,7 +574,11 @@ func (writer *fullyNormalizedWriter) finish() error { } func (writer *fullyNormalizedWriter) consumeByte(b byte) error { - if writer.checked%4096 == 0 { + const ( + normalizationCancelPollBytes = 4096 + ) + + if writer.checked%normalizationCancelPollBytes == 0 { if err := writer.ctx.Err(); err != nil { return err } @@ -585,7 +620,7 @@ func (writer *fullyNormalizedWriter) consumeRune(r rune) error { return nil } - if r == '&' && writer.state.mode == 0 && !writer.state.plain { + if r == '&' && writer.state.mode == normalizationModeText && !writer.state.plain { writer.reference.active = true return nil } @@ -615,27 +650,40 @@ type fullyNormalizedReference struct { } func (reference *fullyNormalizedReference) add(r rune) { + const ( + referencePhaseStart = 0 + referencePhaseName = 'n' + referencePhaseNumericStart = '#' + ) + + const ( + maximumPredefinedEntityNameBytes = 4 + referenceDecimalRadix = 10 + referenceDigitParseBits = 8 + referenceHexadecimalRadix = 16 + ) + switch reference.phase { - case 0: + case referencePhaseStart: if r == '#' { - reference.phase = '#' + reference.phase = referencePhaseNumericStart return } - reference.phase = 'n' - case '#': + reference.phase = referencePhaseName + case referencePhaseNumericStart: if r == 'x' { reference.hexadecimal = true - reference.phase = 'd' + reference.phase = referencePhaseDigits return } - reference.phase = 'd' + reference.phase = referencePhaseDigits } - if reference.phase == 'n' { - if len(reference.name) < 4 { + if reference.phase == referencePhaseName { + if len(reference.name) < maximumPredefinedEntityNameBytes { reference.name += string(r) } else { reference.invalid = true @@ -644,12 +692,12 @@ func (reference *fullyNormalizedReference) add(r rune) { return } - base := uint32(10) + base := uint32(referenceDecimalRadix) if reference.hexadecimal { - base = 16 + base = referenceHexadecimalRadix } - digit, err := strconv.ParseUint(string(r), int(base), 8) + digit, err := strconv.ParseUint(string(r), int(base), referenceDigitParseBits) if err != nil || reference.number > (utf8.MaxRune-uint32(digit))/base { reference.invalid = true return @@ -660,19 +708,26 @@ func (reference *fullyNormalizedReference) add(r rune) { } func (reference *fullyNormalizedReference) value() rune { - if !reference.invalid && reference.phase == 'd' && reference.digits != 0 { + const ( + predefinedEntityApostrophe = "apos" + predefinedEntityGreaterThan = "gt" + predefinedEntityLessThan = "lt" + predefinedEntityQuote = "quot" + ) + + if !reference.invalid && reference.phase == referencePhaseDigits && reference.digits != 0 { return rune(reference.number) // #nosec G115 -- add rejects references above utf8.MaxRune before updating number. } if !reference.invalid { switch reference.name { - case "lt": + case predefinedEntityLessThan: return '<' - case "gt": + case predefinedEntityGreaterThan: return '>' - case "quot": + case predefinedEntityQuote: return '"' - case "apos": + case predefinedEntityApostrophe: return '\'' } } @@ -727,40 +782,46 @@ func (state *fullyNormalizedText) data(r rune) error { } func (state *fullyNormalizedText) consume(r rune) error { + const ( + normalizationModeComment = 'c' + normalizationModeDeclaration = 'v' + normalizationModeMarkupStart = '<' + ) + if state.plain { return state.data(r) } switch state.mode { - case 0: + case normalizationModeText: if r == '<' { - state.mode = '<' + state.mode = normalizationModeMarkupStart return nil } return state.data(r) - case '<': + case normalizationModeMarkupStart: return state.markupStart(r) - case '!': + case normalizationModeDeclarationStart: state.prefix += string(r) switch state.prefix { case "--": - state.mode, state.tail = 'c', 0 + state.mode, state.tail = normalizationModeComment, 0 case "[CDATA[": - state.mode, state.tail, state.started = 'd', 0, false + state.mode, state.tail, state.started = normalizationModeCDATA, 0, false default: if !strings.HasPrefix("--", state.prefix) && !strings.HasPrefix("[CDATA[", state.prefix) { //nolint:gocritic // The candidate delimiters must start with the partial prefix. - state.mode = 'v' + state.mode = normalizationModeDeclaration state.declaration(r) } } - case 't': + case normalizationModeTag: return state.tag(r) - case 'c', 'p', 'd': + case normalizationModeComment, normalizationModePI, normalizationModeCDATA: return state.delimited(r) - case 'v': + case normalizationModeDeclaration: state.declaration(r) - case 'r': + case normalizationModeRawHTML: return state.htmlRawRune(r) } @@ -770,14 +831,14 @@ func (state *fullyNormalizedText) consume(r rune) error { func (state *fullyNormalizedText) markupStart(r rune) error { switch r { case '!': - state.mode, state.prefix = '!', "" + state.mode, state.prefix = normalizationModeDeclarationStart, "" case '?': - state.mode, state.tail, state.nameStart = 'p', 0, true + state.mode, state.tail, state.nameStart = normalizationModePI, 0, true case '/': - state.mode, state.nameStart = 't', true + state.mode, state.nameStart = normalizationModeTag, true state.beginHTMLTag(true) default: - state.mode, state.nameStart = 't', true + state.mode, state.nameStart = normalizationModeTag, true state.beginHTMLTag(false) return state.tag(r) @@ -787,16 +848,21 @@ func (state *fullyNormalizedText) markupStart(r rune) error { } func (state *fullyNormalizedText) tag(r rune) error { + const ( + htmlNamespaceForeign = 2 + normalizationHTMLTokenBytes = 9 + ) + if state.quote != 0 { if state.html && state.htmlAttributeNS { if r == state.quote { switch state.htmlURI { case "": - state.htmlNamespace = 0 + state.htmlNamespace = htmlNamespaceAbsent case lexicon.NamespaceXHTML: - state.htmlNamespace = 1 + state.htmlNamespace = htmlNamespaceXHTML default: - state.htmlNamespace = 2 + state.htmlNamespace = htmlNamespaceForeign } state.htmlAttributeNS = false @@ -818,7 +884,7 @@ func (state *fullyNormalizedText) tag(r rune) error { state.quote = r case '>': state.finishHTMLToken() - state.mode, state.started, state.nameStart = 0, false, false + state.mode, state.started, state.nameStart = normalizationModeText, false, false state.finishHTMLTag() case ' ', '\t', '\n', '\r': state.finishHTMLToken() @@ -831,7 +897,7 @@ func (state *fullyNormalizedText) tag(r rune) error { state.htmlSelfClosing = true state.nameStart = false default: - if state.html && len(state.htmlToken) < 9 { + if state.html && len(state.htmlToken) < normalizationHTMLTokenBytes { state.htmlToken += string(r) } @@ -853,7 +919,7 @@ func (state *fullyNormalizedText) beginHTMLTag(closing bool) { state.htmlName, state.htmlToken, state.htmlURI = "", "", "" state.htmlHadName, state.htmlAttributeNS, state.htmlSelfClosing = false, false, false - state.htmlNamespace = 0 + state.htmlNamespace = htmlNamespaceAbsent if len(state.htmlNamespaces) > 0 { state.htmlNamespace = state.htmlNamespaces[len(state.htmlNamespaces)-1] } @@ -868,7 +934,7 @@ func (state *fullyNormalizedText) finishHTMLToken() { state.htmlName = htmlpkg.LowerASCII(state.htmlToken) state.htmlHadName = true } else { - state.htmlAttributeNS = state.htmlToken == "xmlns" + state.htmlAttributeNS = state.htmlToken == lexicon.PrefixXMLNS state.htmlURI = "" } @@ -876,6 +942,11 @@ func (state *fullyNormalizedText) finishHTMLToken() { } func (state *fullyNormalizedText) finishHTMLTag() { + const ( + htmlRawTextScript = "script" + htmlRawTextStyle = "style" + ) + if !state.html { return } @@ -885,14 +956,14 @@ func (state *fullyNormalizedText) finishHTMLTag() { return } - isHTML := state.htmlNamespace == 0 || state.html5 && state.htmlNamespace == 1 + isHTML := state.htmlNamespace == htmlNamespaceAbsent || state.html5 && state.htmlNamespace == htmlNamespaceXHTML if state.htmlSelfClosing || isHTML && htmlpkg.SerializationVoidElement(state.htmlName, state.html5, state.edition) { return } state.htmlNamespaces = append(state.htmlNamespaces, state.htmlNamespace) - if isHTML && (state.htmlName == "script" || state.htmlName == "style") { - state.mode = 'r' + if isHTML && (state.htmlName == htmlRawTextScript || state.htmlName == htmlRawTextStyle) { + state.mode = normalizationModeRawHTML state.htmlRaw = "': - state.mode, state.started = 0, false + state.mode, state.started = normalizationModeText, false state.popHTMLNamespace() return nil case ' ', '\t', '\n', '\r', '/': - state.mode = 't' + state.mode = normalizationModeTag state.htmlClosing = true state.htmlHadName = true state.htmlToken = "" @@ -943,7 +1014,7 @@ func (state *fullyNormalizedText) htmlRawRune(r rune) error { } func (state *fullyNormalizedText) delimited(r rune) error { - if state.mode == 'p' && state.nameStart { + if state.mode == normalizationModePI && state.nameStart { state.nameStart = false if err := fullyNormalizedName(string(r)); err != nil { @@ -954,14 +1025,14 @@ func (state *fullyNormalizedText) delimited(r rune) error { delimiter, needed := '-', 2 switch state.mode { - case 'p': + case normalizationModePI: delimiter, needed = '?', 1 - case 'd': + case normalizationModeCDATA: delimiter = ']' } if r == '>' && state.tail >= needed { - state.mode, state.tail, state.started = 0, 0, false + state.mode, state.tail, state.started = normalizationModeText, 0, false return nil } @@ -971,7 +1042,7 @@ func (state *fullyNormalizedText) delimited(r rune) error { state.tail = 0 } - if state.mode == 'd' { + if state.mode == normalizationModeCDATA { return state.data(r) } @@ -998,7 +1069,7 @@ func (state *fullyNormalizedText) declaration(r rune) { } case '>': if state.depth == 0 { - state.mode, state.started = 0, false + state.mode, state.started = normalizationModeText, false } } } @@ -1016,16 +1087,20 @@ func effectiveHTMLVersion5(definition *OutputDef) bool { // isHTMLVersion5 returns true when the html-version string represents // version 5 or higher (e.g. "5", "5.0", "5.00", "5.1"). func isHTMLVersion5(v string) bool { + const ( + html5MinimumVersion = 5.0 + ) + if v == "" { return false } - f, err := strconv.ParseFloat(v, 64) + f, err := strconv.ParseFloat(v, float64ParseBits) if err != nil { return false } - return f >= 5.0 + return f >= html5MinimumVersion } // detectOutputMethod applies the host-language default for an unspecified method. @@ -1036,7 +1111,7 @@ func detectOutputMethod(outDef *OutputDef, doc *xdm.Document, autoHTML bool) { // Root is "html" in no namespace → HTML output method. outDef.Method = lexicon.MethodHTML outDef.OmitDeclaration = true - } else if outDef.languageVersion != 10 && !outDef.implicitXMLMethod && root.LocalName() == lexicon.MethodHTML && root.URI() == lexicon.NamespaceXHTML { + } else if outDef.languageVersion != processorXSLT10 && !outDef.implicitXMLMethod && root.LocalName() == lexicon.MethodHTML && root.URI() == lexicon.NamespaceXHTML { // Root is "html" in XHTML namespace → XHTML output method. outDef.Method = lexicon.MethodXHTML } @@ -1059,13 +1134,13 @@ func validateXMLOutputVersion(method, version string) error { } func validateSerializationParamsLanguageVersion(outDef *OutputDef, method string) error { - if outDef.languageVersion != 10 && outDef.OmitDeclaration && (method == lexicon.MethodXML || method == lexicon.MethodXHTML) { + if outDef.languageVersion != processorXSLT10 && outDef.OmitDeclaration && (method == lexicon.MethodXML || method == lexicon.MethodXHTML) { if outDef.Standalone == lexicon.ValueYes || outDef.Standalone == lexicon.ValueNo { return dynamicError(lexicon.ErrSEPM0009, "omit-xml-declaration=\"yes\" conflicts with standalone=%q", outDef.Standalone) } - if outDef.DoctypeSystem != "" && outDef.Version != "" && outDef.Version != lexicon.XSLTVersion10 { + if outDef.DoctypeSystem != "" && outDef.Version != "" && outDef.Version != lexicon.XMLVersion10 { return dynamicError(lexicon.ErrSEPM0009, "omit-xml-declaration=\"yes\" conflicts with doctype-system and version=%q", outDef.Version) } @@ -1078,12 +1153,12 @@ func serializeResultByteOrderMark(outDef *OutputDef, isUTF16 bool, w io.Writer) if outDef.ByteOrderMark || isUTF16 { if isUTF16 { // UTF-16 BE BOM - if werr := writeFullBytes(w, []byte{0xFE, 0xFF}); werr != nil { + if werr := writeFullBytes(w, []byte(lexicon.BOMUTF16BE)); werr != nil { return werr } } else { // UTF-8 BOM - if werr := writeFullBytes(w, []byte{0xEF, 0xBB, 0xBF}); werr != nil { + if werr := writeFullBytes(w, []byte(lexicon.BOMUTF8)); werr != nil { return werr } } @@ -1109,7 +1184,7 @@ func validateSerializationParamsStandalone(outDef *OutputDef, method string, doc // allowsHTMLAutoDetection excludes XSLT 1.0 results with leading non-whitespace // text; newer language versions inspect the document element directly. func allowsHTMLAutoDetection(outDef *OutputDef, doc *xdm.Document) bool { - if outDef.languageVersion == 10 { + if outDef.languageVersion == processorXSLT10 { for child := range xdm.Children(doc) { if child.Type() == xdm.ElementNode { break @@ -1307,7 +1382,11 @@ func validOutputXMLVersion(v string) string { // isValidOutputXMLVersion reports whether v is a valid XML VersionNum // ("1." followed by one or more digits, XML §2.8). func isValidOutputXMLVersion(v string) bool { - rest, ok := strings.CutPrefix(v, "1.") + const ( + xmlVersionNumberPrefix = "1." + ) + + rest, ok := strings.CutPrefix(v, xmlVersionNumberPrefix) if !ok || rest == "" { return false } diff --git a/transform/internal/xslt/output_adaptive.go b/transform/internal/xslt/output_adaptive.go index f19693a..e5d18c8 100644 --- a/transform/internal/xslt/output_adaptive.go +++ b/transform/internal/xslt/output_adaptive.go @@ -19,7 +19,7 @@ func serializeAdaptiveItems(ctx context.Context, w io.Writer, items xdm.Sequence options := streamXMLWriteOptions(definition, strings.ToLower(definition.Encoding)) if options.Normalization == lexicon.NormFullyNormalized { - options.Normalization = normalizationFormNFC + options.Normalization = lexicon.NormalizationFormNFC } writer := xdm.XMLSerialization{XMLWriteOptions: options, Encoding: definition.Encoding, OmitDeclaration: definition.OmitDeclaration, OmitDoctype: definition.DoctypeSystem == "", CompactDeclaration: !definition.Indent} diff --git a/transform/internal/xslt/output_charmap.go b/transform/internal/xslt/output_charmap.go index fc62134..2012588 100644 --- a/transform/internal/xslt/output_charmap.go +++ b/transform/internal/xslt/output_charmap.go @@ -19,22 +19,25 @@ import ( ) const ( - normalizationFormNFC = "NFC" + // normalizationFormNone is the case-folded serialization selector that + // leaves character data unchanged rather than applying a Unicode form. normalizationFormNone = "NONE" - encodingASCII = "us-ascii" + // encodingASCII is the lowercased encoding label used by the serializer + // after case-insensitive matching; it does not select normalization. + encodingASCII = "us-ascii" ) // resolveNormForm returns the norm.Form for the given normalization form name. // Returns (form, true) on success or (0, false) for unknown/NONE forms. func resolveNormForm(form string) (norm.Form, bool) { switch form { - case normalizationFormNFC, lexicon.NormFullyNormalized: + case lexicon.NormalizationFormNFC, lexicon.NormFullyNormalized: return norm.NFC, true - case "NFD": + case lexicon.NormalizationFormNFD: return norm.NFD, true - case "NFKC": + case lexicon.NormalizationFormNFKC: return norm.NFKC, true - case "NFKD": + case lexicon.NormalizationFormNFKD: return norm.NFKD, true default: return 0, false @@ -168,6 +171,10 @@ func transcodeToUTF16(w io.Writer, utf8Data []byte) error { // transcodeToEncoding converts UTF-8 bytes to the target encoding, // replacing characters that cannot be represented with XML character references. func transcodeToEncoding(w io.Writer, utf8Data []byte, encName string) error { + const ( + hexCharacterReferenceFormat = "&#x%X;" + ) + codec, err := htmlindex.Get(encName) if err != nil { // Unknown encoding — fall back to writing UTF-8 @@ -188,7 +195,7 @@ func transcodeToEncoding(w io.Writer, utf8Data []byte, encName string) error { encoded, err := encoder.Bytes(utf8Data[:size]) if err != nil { // Character cannot be encoded — use character reference - ref := fmt.Sprintf("&#x%X;", r) + ref := fmt.Sprintf(hexCharacterReferenceFormat, r) if werr := writeFullString(w, ref); werr != nil { return werr } @@ -224,7 +231,7 @@ func hasDOEMarkers(doc xdm.Node) bool { // The result tree is freshly built via guarded AddChild, so it is acyclic // and Walk cannot return ErrWalkCycle here; the error is safely ignored. _ = xdm.Walk(doc, xdm.NodeWalkerFunc(func(n xdm.Node) error { - if n.Type() == xdm.ProcessingInstructionNode && n.Name() == nameDisableOutputEscaping { + if n.Type() == xdm.ProcessingInstructionNode && n.Name() == lexicon.DisableOutputEscapingPITarget { found = true } diff --git a/transform/internal/xslt/output_html.go b/transform/internal/xslt/output_html.go index cef8c8c..4fe13b8 100644 --- a/transform/internal/xslt/output_html.go +++ b/transform/internal/xslt/output_html.go @@ -13,6 +13,12 @@ import ( htmlpkg "github.com/skarm/xmlsec/transform/internal/xdm/html" ) +// Default HTML/XHTML Content-Type emission: the media type and charset delimiter describe serialized bytes, not XML parser permissions. +const ( + contentTypeCharsetParameter = "; charset=" + defaultHTMLMediaType = "text/html" +) + func serializeHTML(w io.Writer, doc *xdm.Document, outDef *OutputDef, charMap map[rune]string) error { writer := htmlResultWriter(outDef).DOEMarkers(true).CharacterMap(charMap) return serializeHTMLChildren(w, doc, writer, outDef) @@ -84,14 +90,14 @@ func serializeOwnedXHTML(w io.Writer, owned *xdm.Document, definition *OutputDef if definition.IncludeContentType == nil || *definition.IncludeContentType { media, encoding := definition.MediaType, definition.Encoding if media == "" { - media = "text/html" + media = defaultHTMLMediaType } if encoding == "" { encoding = lexicon.EncodingUTF8U } - contentType = media + "; charset=" + encoding + contentType = media + contentTypeCharsetParameter + encoding } if err := htmlpkg.PrepareXHTML(context.Background(), owned, html5, contentType); err != nil { diff --git a/transform/internal/xslt/output_json_stream.go b/transform/internal/xslt/output_json_stream.go index 518b261..6d2a5dd 100644 --- a/transform/internal/xslt/output_json_stream.go +++ b/transform/internal/xslt/output_json_stream.go @@ -12,18 +12,32 @@ import ( // JSON serialization does not normalize sequences into arrays (SER31 §9). // The same cardinality rules apply to the result and every map/array member. func writeJSONItems(ctx context.Context, w io.Writer, seq xdm.Sequence, out *OutputDef, depth int) error { + const ( + jsonNullLiteral = "null" + ) + + const ( + errCodeSERE0023 = "SERE0023" + ) + switch xdm.SequenceLen(seq) { case 0: - return writeFullString(w, "null") + return writeFullString(w, jsonNullLiteral) case 1: return writeJSONItem(ctx, w, seq.Get(0), out, depth) default: - return dynamicError("SERE0023", "JSON serialization requires at most one item") + return dynamicError(errCodeSERE0023, "JSON serialization requires at most one item") } } func writeJSONItem(ctx context.Context, w io.Writer, item xdm.Item, out *OutputDef, depth int) error { - if depth > xpath.DepthLimit(ctx, 128) { + const ( + jsonOutputDepthFallback = 128 + errCodeSERE0020 = "SERE0020" + errCodeSERE0021 = "SERE0021" + ) + + if depth > xpath.DepthLimit(ctx, jsonOutputDepthFallback) { return xpath.ErrBudget } @@ -66,7 +80,7 @@ func writeJSONItem(ctx context.Context, w io.Writer, item xdm.Item, out *OutputD value, ok := item.(xdm.AtomicValue) if !ok { - return dynamicError("SERE0021", "cannot serialize %T as JSON", item) + return dynamicError(errCodeSERE0021, "cannot serialize %T as JSON", item) } text, err := xpath.AtomicToString(value) @@ -74,8 +88,8 @@ func writeJSONItem(ctx context.Context, w io.Writer, item xdm.Item, out *OutputD return err } - if value.IsNumeric() && (text == lexicon.FloatNaN || text == "INF" || text == "-INF" || text == "+INF") { - return dynamicError("SERE0020", "cannot serialize NaN or infinity as JSON") + if value.IsNumeric() && (text == lexicon.FloatNaN || text == lexicon.FloatINF || text == lexicon.FloatNegINF || text == lexicon.FloatPosINF) { + return dynamicError(errCodeSERE0020, "cannot serialize NaN or infinity as JSON") } return writeFullString(w, text) diff --git a/transform/internal/xslt/package_override.go b/transform/internal/xslt/package_override.go index f273a3b..78abedb 100644 --- a/transform/internal/xslt/package_override.go +++ b/transform/internal/xslt/package_override.go @@ -107,7 +107,7 @@ func (c *compiler) pushStandardAttrs(ctx context.Context, elem *xdm.Element) (st c.effectiveVersion = ver } - if xdn, ok := elem.GetAttribute(nameXpathDefaultNamespace); ok { + if xdn, ok := elem.GetAttribute(nameXPathDefaultNamespace); ok { c.xpathDefaultNS = xdn c.hasXPathDefaultNS = true } @@ -368,14 +368,14 @@ func (c *compiler) compileOverrideFunction(ctx context.Context, elem *xdm.Elemen Name: qn, Params: params, Body: body, - As: getAttr(elem, "as"), + As: getAttr(elem, nameAs), Visibility: getAttr(elem, xslAttrVisibility), IsOverride: true, OwnerPackage: c.stylesheet, } fn.signatureTypeKey = c.declarationTypeKey(elem, fn.As) - fn.NewEachTime = getAttr(elem, "new-each-time") - fn.Streamability = getAttr(elem, "streamability") + fn.NewEachTime = getAttr(elem, nameNewEachTime) + fn.Streamability = getAttr(elem, nameStreamability) pkgFn := pkg.functions[funcKey{Name: qn, Arity: len(params)}] if pkgFn == nil { @@ -450,7 +450,7 @@ func (c *compiler) compileOverrideTemplate(ctx context.Context, elem *xdm.Elemen return nil, err } - if prio := getAttr(elem, "priority"); prio != "" { + if prio := getAttr(elem, namePriority); prio != "" { f, err := parseTemplatePriority(prio) if err != nil { return nil, err @@ -501,7 +501,7 @@ func (c *compiler) compileOverrideTemplate(ctx context.Context, elem *xdm.Elemen tmpl.Params = params tmpl.Body = body - tmpl.As = getAttr(elem, "as") + tmpl.As = getAttr(elem, nameAs) tmpl.signatureTypeKey = c.declarationTypeKey(elem, tmpl.As) tmpl.contextTypeKey = c.declarationTypeKey(elem, lexicon.NodeTestItem) @@ -559,7 +559,7 @@ func (c *compiler) compileOverrideVariable(ctx context.Context, elem *xdm.Elemen c.originalVariableVisibility = effectiveVis defer func() { c.originalVariableVisibility = savedOriginal }() - v := &variable{Name: resolvedName, As: getAttr(elem, "as"), Visibility: getAttr(elem, xslAttrVisibility), OwnerPackage: c.stylesheet} + v := &variable{Name: resolvedName, As: getAttr(elem, nameAs), Visibility: getAttr(elem, xslAttrVisibility), OwnerPackage: c.stylesheet} if err := c.validateAsSequenceType(ctx, elem, v.As, "overriding variable"); err != nil { return nil, err } diff --git a/transform/internal/xslt/package_resource.go b/transform/internal/xslt/package_resource.go index 0f6d68c..8344eaa 100644 --- a/transform/internal/xslt/package_resource.go +++ b/transform/internal/xslt/package_resource.go @@ -8,6 +8,11 @@ import ( "github.com/skarm/xmlsec/transform/internal/xpath" ) +// Absent package-version selects lexical package version 1; it is independent of the stylesheet language version. +const ( + defaultPackageVersion = "1" +) + type ( packageStackKey struct{} packageIdentity struct { @@ -17,6 +22,10 @@ type ( ) func enterPackage(ctx context.Context, doc *xdm.Document) (context.Context, error) { + const ( + packageDependencyDepthFallback = 128 + ) + root := doc.DocumentElement() if root == nil || root.URI() != lexicon.NamespaceXSLT || root.LocalName() != namePackage { return ctx, nil @@ -24,7 +33,7 @@ func enterPackage(ctx context.Context, doc *xdm.Document) (context.Context, erro version, present := root.GetAttribute(namePackageVersion) if !present { - version = "1" + version = defaultPackageVersion } parsed := ParsePackageVersion(version) @@ -35,13 +44,13 @@ func enterPackage(ctx context.Context, doc *xdm.Document) (context.Context, erro identity := packageIdentity{name: getAttr(root, xslAttrName), version: parsed} stack, _ := ctx.Value(packageStackKey{}).([]packageIdentity) - if len(stack) >= xpath.DepthLimit(ctx, 128) { + if len(stack) >= xpath.DepthLimit(ctx, packageDependencyDepthFallback) { return nil, xpath.ErrBudget } for _, parent := range stack { if parent.name == identity.name && parent.version.Compare(parsed) == 0 { - return nil, staticError("XTSE3005", "cyclic package dependency %q", identity.name) + return nil, staticError(errCodeXTSE3005, "cyclic package dependency %q", identity.name) } } @@ -55,22 +64,26 @@ func enterPackage(ctx context.Context, doc *xdm.Document) (context.Context, erro } func checkResolvedPackage(doc *xdm.Document, name, version string) error { + const ( + shadowAttrPackageVersion = "_package-version" + ) + root := doc.DocumentElement() if root == nil || root.URI() != lexicon.NamespaceXSLT || root.LocalName() != namePackage { return staticError(errCodeXTSE3000, "resolved package does not match %q", name) } - if _, shadow := root.GetAttribute("_name"); !shadow && getAttr(root, xslAttrName) != name { + if _, shadow := root.GetAttribute(shadowAttrName); !shadow && getAttr(root, xslAttrName) != name { return staticError(errCodeXTSE3000, "resolved package does not match %q", name) } - if _, shadow := root.GetAttribute("_package-version"); shadow { + if _, shadow := root.GetAttribute(shadowAttrPackageVersion); shadow { return nil } actual := getAttr(root, namePackageVersion) if actual == "" { - actual = "1" + actual = defaultPackageVersion } if !ParseVersionConstraint(version).Matches(ParsePackageVersion(actual)) { diff --git a/transform/internal/xslt/package_visibility.go b/transform/internal/xslt/package_visibility.go index 351bf01..080ac73 100644 --- a/transform/internal/xslt/package_visibility.go +++ b/transform/internal/xslt/package_visibility.go @@ -14,6 +14,13 @@ import ( "github.com/skarm/xmlsec/transform/internal/xpath" ) +// Acceptance and exposure selection distinguish absent declarations from +// wildcard, declared-visibility and exact-name precedence tiers. +const ( + exposePriorityAbsent = -1 + exposePriorityExact = 3 +) + // componentVisibility tracks the effective visibility of a component. // The default visibility for package components is "private" per XSLT 3.0 §3.6.1. // The default for stylesheet (non-package) components is "public". @@ -85,8 +92,8 @@ func parseComponentRules(ctx context.Context, elem *xdm.Element, accept bool) ([ } } - component := strings.Trim(getAttr(elem, xslAttrComponent), " \t\r\n") - visibility := strings.Trim(getAttr(elem, xslAttrVisibility), " \t\r\n") + component := strings.Trim(getAttr(elem, xslAttrComponent), lexicon.XMLWhitespace) + visibility := strings.Trim(getAttr(elem, xslAttrVisibility), lexicon.XMLWhitespace) switch component { case xslElemTemplate, xslElemFunction, xslElemVariable, xslElemAttributeSet, xslElemMode, xslWildcard: @@ -136,6 +143,10 @@ func parseComponentRules(ctx context.Context, elem *xdm.Element, accept bool) ([ } func resolveComponentReference(name, component string, elem *xdm.Element) (string, error) { + const ( + wildcardProbeLocalName = "x" + ) + base, arity, present := splitComponentArity(name) if present { if component != xslElemFunction || arity == "" { @@ -170,7 +181,7 @@ func resolveComponentReference(name, component string, elem *xdm.Element) (strin return xdm.ClarkName(ns[prefix], "*"), nil } case strings.HasPrefix(base, "Q{") && strings.HasSuffix(base, "}*"): - if isValidEQName(base[:len(base)-1] + "x") { + if isValidEQName(base[:len(base)-1] + wildcardProbeLocalName) { return base[1:], nil } default: @@ -209,7 +220,7 @@ func applyAcceptRules(compType, compName string, rules []acceptRule, declared st bestVis = visPrivate } - bestPriority := -1 + bestPriority := exposePriorityAbsent for _, rule := range rules { if rule.component != xslWildcard && rule.component != compType || !componentNameMatches(compName, rule.names) || !isAcceptVisibilityCompatible(declared, rule.visibility) { @@ -508,11 +519,15 @@ func (component exposedComponent) matches(rule acceptRule) bool { } func (component exposedComponent) exposedVisibility(ctx context.Context, rules []acceptRule) (string, bool, error) { + const ( + exposePriorityDeclared = 2 + ) + visibility := component.declared - priority := -1 + priority := exposePriorityAbsent if visibility != "" { - priority = 2 + priority = exposePriorityDeclared } else { visibility = visPrivate } @@ -533,7 +548,7 @@ func (component exposedComponent) exposedVisibility(ctx context.Context, rules [ candidate := componentRulePriority(rule.names) if candidate >= priority { priority, visibility = candidate, rule.visibility - explicit = candidate == 3 + explicit = candidate == exposePriorityExact } } @@ -550,15 +565,20 @@ func (component exposedComponent) exposedVisibility(ctx context.Context, rules [ } func componentRulePriority(name string) int { + const ( + exposePriorityPartialWildcard = 1 + exposePriorityWildcard = 0 + ) + if name == xslWildcard { - return 0 + return exposePriorityWildcard } if isWildcard(name) { - return 1 + return exposePriorityPartialWildcard } - return 3 + return exposePriorityExact } func collectOverrideBlockNames(block *xdm.Element, bindings map[string]string, excluded map[*xdm.Element]struct{}, names map[string]struct{}) { @@ -579,6 +599,10 @@ func collectOverrideBlockNames(block *xdm.Element, bindings map[string]string, e } func overrideComponentKey(elem *xdm.Element, bindings map[string]string) string { + const ( + componentArityFormat = "#%d" + ) + kind := elem.LocalName() switch kind { case xslElemTemplate, xslElemVariable, xslElemParam, xslElemAttributeSet: @@ -595,7 +619,7 @@ func overrideComponentKey(elem *xdm.Element, bindings map[string]string) string case xslElemFunction: name := getAttr(elem, xslAttrName) if name != "" { - return xslElemFunction + ":" + resolveQName(name, bindings) + fmt.Sprintf("#%d", overrideFunctionArity(elem)) + return xslElemFunction + ":" + resolveQName(name, bindings) + fmt.Sprintf(componentArityFormat, overrideFunctionArity(elem)) } } @@ -709,5 +733,9 @@ func isWildcard(pattern string) bool { // functionVisKey creates a visibility map key for a function (name#arity). func functionVisKey(qn xpath.QualifiedName, arity int) string { - return fmt.Sprintf("{%s}%s#%d", qn.URI, qn.Name, arity) + const ( + functionVisibilityKeyFormat = "{%s}%s#%d" + ) + + return fmt.Sprintf(functionVisibilityKeyFormat, qn.URI, qn.Name, arity) } diff --git a/transform/internal/xslt/profile.go b/transform/internal/xslt/profile.go index 6173ae5..ca76ccb 100644 --- a/transform/internal/xslt/profile.go +++ b/transform/internal/xslt/profile.go @@ -14,6 +14,11 @@ import ( "github.com/skarm/xmlsec/transform/internal/xpath" ) +// XML Schema selection returned by xpath.XMLSchemaVersion uses 10/11, independently of the zero-based lexical.Version enum. +const ( + xmlSchemaProfile10 = 10 +) + // ErrLanguageUnsupported identifies features outside the modern Basic profile. var ErrLanguageUnsupported = errors.New("unsupported XSLT language profile") @@ -35,22 +40,22 @@ func CheckProcessorLanguage(d *xdm.Document, want, processor int) (int, error) { root := d.DocumentElement() declaration, hasDeclaration := elementXSLTVersionAttribute(root) - if _, shadow := root.GetAttribute("_version"); shadow && root.URI() == lexicon.NamespaceXSLT && (processor == 0 || processor == 30) { + if _, shadow := root.GetAttribute(shadowAttrVersion); shadow && root.URI() == lexicon.NamespaceXSLT && (processor == 0 || processor == processorXSLT30) { declaration = lexicon.XSLTVersion30 hasDeclaration = true } if !hasDeclaration { if root.URI() == lexicon.NamespaceXSLT { - return 0, staticError("XTSE0010", "stylesheet requires a version attribute") + return 0, staticError(errCodeXTSE0010, "stylesheet requires a version attribute") } - return 0, staticError("XTSE0150", "simplified stylesheet requires xsl:version") + return 0, staticError(errCodeXTSE0150, "simplified stylesheet requires xsl:version") } version, valid := declaredLanguageProfile(declaration) if !valid { - return 0, staticError("XTSE0110", "invalid XSLT version") + return 0, staticError(errCodeXTSE0110, "invalid XSLT version") } if want != 0 && want != version { @@ -61,15 +66,15 @@ func CheckProcessorLanguage(d *xdm.Document, want, processor int) (int, error) { processor = version } - if processor != 10 && processor != 20 && processor != 30 { + if processor != processorXSLT10 && processor != processorXSLT20 && processor != processorXSLT30 { return 0, ErrLanguageUnsupported } - if processor == 20 && root.URI() == lexicon.NamespaceXSLT && root.LocalName() == namePackage { - return 0, staticError("XTSE0010", "xsl:package requires an XSLT 3.0 processor") + if processor == processorXSLT20 && root.URI() == lexicon.NamespaceXSLT && root.LocalName() == namePackage { + return 0, staticError(errCodeXTSE0010, "xsl:package requires an XSLT 3.0 processor") } - if processor == 10 { + if processor == processorXSLT10 { return processor, checkLanguage10(root) } @@ -77,7 +82,7 @@ func CheckProcessorLanguage(d *xdm.Document, want, processor int) (int, error) { } func compareLanguageVersion(version string, boundary int) (int, bool) { - version = strings.Trim(version, " \t\r\n") + version = strings.Trim(version, lexicon.XMLWhitespace) for number, text := range []string{lexicon.XSLTVersion10, lexicon.XSLTVersion20, lexicon.XSLTVersion30} { if version == text { return number + 1 - boundary, true @@ -97,17 +102,17 @@ func compareLanguageVersion(version string, boundary int) (int, bool) { } func declaredLanguageProfile(version string) (int, bool) { - if comparison, valid := compareLanguageVersion(version, 2); !valid { + if comparison, valid := compareLanguageVersion(version, xsltMajorVersion2); !valid { return 0, false } else if comparison < 0 { - return 10, true + return processorXSLT10, true } - if comparison, _ := compareLanguageVersion(version, 3); comparison < 0 { - return 20, true + if comparison, _ := compareLanguageVersion(version, xsltMajorVersion3); comparison < 0 { + return processorXSLT20, true } - return 30, true + return processorXSLT30, true } func languageVersionAttribute(elem *xdm.Element) string { @@ -126,20 +131,20 @@ func checkModernLanguage(elem *xdm.Element, effective string, selected int) erro } own, hasOwn := elementXSLTVersionAttribute(elem) - if _, shadow := elem.GetAttribute("_version"); shadow && elem.URI() == lexicon.NamespaceXSLT && selected >= 30 { + if _, shadow := elem.GetAttribute(shadowAttrVersion); shadow && elem.URI() == lexicon.NamespaceXSLT && selected >= processorXSLT30 { own = lexicon.XSLTVersion30 hasOwn = true } if hasOwn { if _, valid := declaredLanguageProfile(own); !valid { - return staticError("XTSE0110", "invalid XSLT version") + return staticError(errCodeXTSE0110, "invalid XSLT version") } effective = own } - comparison, _ := compareLanguageVersion(effective, selected/10) + comparison, _ := compareLanguageVersion(effective, selected/processorVersionScale) forwards := comparison > 0 if elem.URI() == lexicon.NamespaceXSLT && unavailableInstruction(elem.LocalName(), selected) && forwards { @@ -150,7 +155,7 @@ func checkModernLanguage(elem *xdm.Element, effective string, selected int) erro return err } - if selected >= 20 { + if selected >= processorXSLT20 { if err := checkExclusionNamespaceAttribute(elem); err != nil { return err } @@ -173,7 +178,7 @@ func checkModernLanguage(elem *xdm.Element, effective string, selected int) erro func checkFutureInstructionFallbacks(elem *xdm.Element, effective string, selected int) error { for child := range xdm.ChildElements(elem) { - if child.URI() == lexicon.NamespaceXSLT && child.LocalName() == "fallback" { + if child.URI() == lexicon.NamespaceXSLT && child.LocalName() == nameFallback { if err := checkModernLanguage(child, effective, selected); err != nil { return err } @@ -208,6 +213,10 @@ func checkExclusionNamespaceAttribute(elem *xdm.Element) error { } func checkExtensionNamespaceAttribute(elem *xdm.Element, selected int) error { + const ( + errCodeXTSE0085 = "XTSE0085" + ) + value := getAttr(elem, nameExtensionElementPrefixes) if elem.URI() != lexicon.NamespaceXSLT { @@ -231,8 +240,8 @@ func checkExtensionNamespaceAttribute(elem *xdm.Element, selected int) error { return staticError(errCodeXTSE1430, "undeclared extension namespace prefix %q", prefix) } - if selected >= 30 && isReservedNamespace(uri) { - return staticError("XTSE0085", "reserved namespace %q must not be used as an extension namespace", uri) + if selected >= processorXSLT30 && isReservedNamespace(uri) { + return staticError(errCodeXTSE0085, "reserved namespace %q must not be used as an extension namespace", uri) } } @@ -243,7 +252,7 @@ func unavailableInstruction(name string, selected int) bool { def, known := lookupXSLTElement(name) minimum := minXSLTElementVersion(name) - return !known || selected < 30 && (def.since30 || minimum == lexicon.XSLTVersion30) || selected < 20 && minimum == lexicon.XSLTVersion20 + return !known || selected < processorXSLT30 && (def.since30 || minimum == lexicon.XSLTVersion30) || selected < processorXSLT20 && minimum == lexicon.XSLTVersion20 } func checkModernElementLanguage(elem *xdm.Element, effective int) error { @@ -254,8 +263,8 @@ func checkModernElementLanguage(elem *xdm.Element, effective int) error { name := elem.LocalName() def, known := lookupXSLTElement(name) - if known && (def.since30 || minXSLTElementVersion(name) == lexicon.XSLTVersion30) && effective < 30 { - return staticError("XTSE0010", "xsl:%s requires XSLT 3.0", name) + if known && (def.since30 || minXSLTElementVersion(name) == lexicon.XSLTVersion30) && effective < processorXSLT30 { + return staticError(errCodeXTSE0010, "xsl:%s requires XSLT 3.0", name) } if err := checkComponentNames(elem, effective); err != nil { @@ -267,11 +276,11 @@ func checkModernElementLanguage(elem *xdm.Element, effective int) error { continue } - if effective < 30 && unknownAttribute20(elem, attribute) { + if effective < processorXSLT30 && unknownAttribute20(elem, attribute) { continue } - if _, shadow := elem.GetAttribute("_" + attribute.LocalName()); shadow && effective >= 30 { + if _, shadow := elem.GetAttribute(shadowAttributePrefix + attribute.LocalName()); shadow && effective >= processorXSLT30 { continue } @@ -280,10 +289,10 @@ func checkModernElementLanguage(elem *xdm.Element, effective int) error { } } - if effective < 30 { + if effective < processorXSLT30 { for attribute, metadata := range def.attrs { if _, present := elem.GetAttribute(attribute); metadata.optional30 && !present { - return staticError("XTSE0010", "xsl:%s requires %s in XSLT 2.0", name, attribute) + return staticError(errCodeXTSE0010, "xsl:%s requires %s in XSLT 2.0", name, attribute) } } } @@ -293,7 +302,7 @@ func checkModernElementLanguage(elem *xdm.Element, effective int) error { func checkStaticBindingPosition(elem *xdm.Element, processor int) error { name := elem.LocalName() - if processor >= 30 && (name == nameVariable || name == nameParam) && xsdBoolTrue(getAttr(elem, nameStatic)) { + if processor >= processorXSLT30 && (name == nameVariable || name == nameParam) && xsdBoolTrue(getAttr(elem, nameStatic)) { parent, ok := elem.Parent().(*xdm.Element) if !ok || parent.URI() != lexicon.NamespaceXSLT || !isStaticBindingParent(parent.LocalName()) { return staticError(errCodeXTSE0020, "static=\"yes\" is permitted only on a top-level variable or parameter") @@ -315,13 +324,13 @@ func checkComponentNames(element *xdm.Element, processor int) error { } switch element.LocalName() { - case nameTemplate, lexicon.XSLTElementMode, nameAttributeSet, nameKey2, lexicon.XSLTElementDecimalFormat, + case nameTemplate, lexicon.XSLTElementMode, nameAttributeSet, keyLocalName, lexicon.XSLTElementDecimalFormat, nameVariable, nameParam, lexicon.XSLTElementFunction, nameOutput, lexicon.XSLTElementCharacterMap, lexicon.XSLTElementAccumulator: default: return nil } - if _, shadow := element.GetAttribute("_name"); shadow && processor >= 30 { + if _, shadow := element.GetAttribute(shadowAttrName); shadow && processor >= processorXSLT30 { return nil } @@ -335,7 +344,7 @@ func checkComponentNames(element *xdm.Element, processor int) error { return nil } - if !xmlchar.IsValidQName(name) && !(processor >= 30 && isValidEQName(name)) { + if !xmlchar.IsValidQName(name) && !(processor >= processorXSLT30 && isValidEQName(name)) { return staticError(errCodeXTSE0020, "invalid name %q on xsl:%s", name, element.LocalName()) } @@ -344,7 +353,7 @@ func checkComponentNames(element *xdm.Element, processor int) error { func checkReservedComponentName(element *xdm.Element, name string, processor int, namedTemplate bool) error { expanded := resolveQName(name, inScopeNamespaces(element)) - if namedTemplate && processor >= 30 && expanded == xdm.ClarkName(lexicon.NamespaceXSLT, "initial-template") { + if namedTemplate && processor >= processorXSLT30 && expanded == xdm.ClarkName(lexicon.NamespaceXSLT, initialTemplateLocalName) { return nil } @@ -373,14 +382,14 @@ func checkLiteralElementLanguage(elem *xdm.Element, effective int) error { } if requiresSchemaAttribute(attribute.LocalName(), xdm.CharacterData(attribute), effective) { - return staticError("XTSE1660", "xsl:%s requires a schema-aware processor", attribute.LocalName()) + return staticError(errCodeXTSE1660, "xsl:%s requires a schema-aware processor", attribute.LocalName()) } - if effective < 30 && attribute.LocalName() == nameExpandText { - return staticError("XTSE0090", "expand-text requires XSLT 3.0") + if effective < processorXSLT30 && attribute.LocalName() == nameExpandText { + return staticError(errCodeXTSE0090, "expand-text requires XSLT 3.0") } - if effective == 20 && attribute.LocalName() == nameInheritNamespaces { + if effective == processorXSLT20 && attribute.LocalName() == nameInheritNamespaces { if err := checkBoolean20Attribute(nameInheritNamespaces, xdm.CharacterData(attribute), elementXSLTDef(lexicon.XSLTElementElement)); err != nil { return err } @@ -398,7 +407,7 @@ func elementForwardsCompatible(elem *xdm.Element, processor int) bool { } if version := languageVersionAttribute(parent); version != "" { - comparison, valid := compareLanguageVersion(version, processor/10) + comparison, valid := compareLanguageVersion(version, processor/processorVersionScale) return valid && comparison > 0 } } @@ -409,7 +418,7 @@ func elementForwardsCompatible(elem *xdm.Element, processor int) bool { // unknownAttribute20 identifies future vocabulary ignored by a 2.0 processor // only when this element explicitly inherits forwards-compatible processing. func unknownAttribute20(elem *xdm.Element, attr *xdm.Attribute) bool { - if !elementForwardsCompatible(elem, 20) { + if !elementForwardsCompatible(elem, processorXSLT20) { return false } @@ -422,7 +431,7 @@ func unknownAttribute20(elem *xdm.Element, attr *xdm.Attribute) bool { } name := attr.LocalName() - if strings.HasPrefix(name, "_") || name == nameExpandText || name == nameDefaultMode || name == nameUseAccumulators { + if strings.HasPrefix(name, shadowAttributePrefix) || name == nameExpandText || name == nameDefaultMode || name == nameUseAccumulators { return true } @@ -437,24 +446,24 @@ func unknownAttribute20(elem *xdm.Element, attr *xdm.Attribute) bool { func checkModernAttributeLanguage(attribute, value, element string, def elementDef, effective int) error { if requiresSchemaAttribute(attribute, value, effective) { - return staticError("XTSE1660", "%s on xsl:%s requires a schema-aware processor", attribute, element) + return staticError(errCodeXTSE1660, "%s on xsl:%s requires a schema-aware processor", attribute, element) } - if effective < 30 && attribute == lexicon.SerializationMethod && (value == "json" || value == "adaptive") { - return staticError("XTSE0020", "serialization method requires XSLT 3.0") + if effective < processorXSLT30 && attribute == lexicon.SerializationMethod && (value == lexicon.MethodJSON || value == lexicon.MethodAdaptive) { + return staticError(errCodeXTSE0020, "serialization method requires XSLT 3.0") } - if strings.HasPrefix(attribute, "_") && effective < 30 { - return staticError("XTSE0090", "shadow attributes require XSLT 3.0") + if strings.HasPrefix(attribute, shadowAttributePrefix) && effective < processorXSLT30 { + return staticError(errCodeXTSE0090, "shadow attributes require XSLT 3.0") } - if effective < 30 { + if effective < processorXSLT30 { if metadata, ok := def.attrs[attribute]; ok && (metadata.since30 || metadata.processor30) { - return staticError("XTSE0090", "%s on xsl:%s requires XSLT 3.0", attribute, element) + return staticError(errCodeXTSE0090, "%s on xsl:%s requires XSLT 3.0", attribute, element) } if attribute == nameExpandText || attribute == nameDefaultMode || attribute == nameUseAccumulators { - return staticError("XTSE0090", "%s requires XSLT 3.0", attribute) + return staticError(errCodeXTSE0090, "%s requires XSLT 3.0", attribute) } if err := checkBoolean20Attribute(attribute, value, def); err != nil { @@ -462,7 +471,7 @@ func checkModernAttributeLanguage(attribute, value, element string, def elementD } } - if attribute == "as" { + if attribute == nameAs { sequenceType, err := xpath.ParseSequenceType(value) if element == nameContextItem || element == lexicon.XSLTElementGlobalContextItem { if err != nil || sequenceType.Void || sequenceType.Occurrence != xdm.OccurrenceExactlyOne { @@ -474,7 +483,7 @@ func checkModernAttributeLanguage(attribute, value, element string, def elementD return staticErrorCause(lexicon.ErrXPST0003, err, "invalid xsl:%s/@as: %v", element, err) } - return xpath.CheckTypeLanguage(sequenceType, map[int]int{20: 20, 30: 31}[effective]) + return xpath.CheckTypeLanguage(sequenceType, map[int]int{processorXSLT20: xpathProfile20, processorXSLT30: xpathProfile31}[effective]) } return nil @@ -483,8 +492,8 @@ func checkModernAttributeLanguage(attribute, value, element string, def elementD // XSLT 2.0 enumerates yes/no; XSLT 3.0 admits the four xs:boolean aliases, // including when a 3.0 processor evaluates a version="2.0" stylesheet. func parseXSLTBoolean(value string, processor int) (bool, bool) { - if processor == 20 { - switch strings.Trim(value, " \t\r\n") { + if processor == processorXSLT20 { + switch strings.Trim(value, lexicon.XMLWhitespace) { case lexicon.ValueYes: return true, true case lexicon.ValueNo: @@ -507,12 +516,12 @@ func checkBoolean20Attribute(name, value string, def elementDef) error { return nil // Computed values are checked after AVT evaluation. } - value = strings.Trim(value, " \t\r\n") + value = strings.Trim(value, lexicon.XMLWhitespace) if value == lexicon.ValueOmit && slices.Contains(metadata.values, lexicon.ValueOmit) { return nil } - if _, valid := parseXSLTBoolean(value, 20); !valid { + if _, valid := parseXSLTBoolean(value, processorXSLT20); !valid { return staticError(errCodeXTSE0020, "%q is not a valid XSLT 2.0 value for @%s", value, name) } @@ -524,11 +533,11 @@ func requiresSchemaAttribute(attribute, value string, processor int) bool { switch attribute { case nameValidation, nameDefaultValidation: - return value == validationStrict || processor == 20 && (value == validationLax || value == validationPreserve) + return value == validationStrict || processor == processorXSLT20 && (value == validationLax || value == validationPreserve) case lexicon.AttrType: return true case nameTyped: - return value == validationStrict || value == lexicon.ValueYes || value == lexicon.ValueTrue || value == "1" + return value == validationStrict || value == lexicon.ValueYes || value == lexicon.ValueTrue || value == booleanDigitTrue default: return false } diff --git a/transform/internal/xslt/profile10.go b/transform/internal/xslt/profile10.go index 91e84d4..cffcd82 100644 --- a/transform/internal/xslt/profile10.go +++ b/transform/internal/xslt/profile10.go @@ -34,52 +34,52 @@ func XSLT10ElementAttributes(name string) (string, bool) { func lookupXSLT10Attribute(key string) (string, bool) { switch key { - case nameStylesheet, fnNameTransform: - return "version id extension-element-prefixes exclude-result-prefixes", true + case nameStylesheet, lexicon.XSLTElementTransform: + return xslAttrVersion + " " + lexicon.AttrID + " " + nameExtensionElementPrefixes + " " + nameExcludeResultPrefixes, true case nameImport, nameInclude: return lexicon.AttrHref, true - case nameFallback, "apply-imports", "choose", "otherwise", "comment": + case nameFallback, lexicon.XSLTElementApplyImports, lexicon.XSLTElementChoose, lexicon.XSLTElementOtherwise, lexicon.XSLTElementComment: return "", true case nameTemplate: - return "match name priority mode", true + return lexicon.AttrMatch + " " + xslAttrName + " " + namePriority + " " + lexicon.AttrMode, true case nameOutput: - return "method version encoding omit-xml-declaration standalone doctype-public doctype-system cdata-section-elements indent media-type", true + return lexicon.SerializationMethod + " " + lexicon.SerializationVersion + " " + lexicon.SerializationEncoding + " " + lexicon.SerializationOmitXMLDeclaration + " " + lexicon.SerializationStandalone + " " + lexicon.SerializationDoctypePublic + " " + lexicon.SerializationDoctypeSystem + " " + lexicon.SerializationCDATASectionElements + " " + lexicon.SerializationIndent + " " + lexicon.SerializationMediaType, true case nameStripSpace, namePreserveSpace: return nameElements, true - case nameKey2: - return "name match use", true - case "decimal-format": - return "name decimal-separator grouping-separator infinity minus-sign NaN percent per-mille zero-digit digit pattern-separator", true - case "namespace-alias": - return "stylesheet-prefix result-prefix", true + case keyLocalName: + return xslAttrName + " " + lexicon.AttrMatch + " " + lexicon.AttrUse, true + case lexicon.XSLTElementDecimalFormat: + return xslAttrName + " " + nameDecimalSeparator + " " + nameGroupingSeparator + " " + nameInfinity + " " + nameMinusSign + " " + lexicon.DecimalFormatNaN + " " + namePercent + " " + namePerMille + " " + nameZeroDigit + " " + nameDigit + " " + namePatternSeparator, true + case lexicon.XSLTElementNamespaceAlias: + return nameStylesheetPrefix + " " + nameResultPrefix, true case nameAttributeSet: - return "name use-attribute-sets", true - case nameVariable, nameParam, "with-param": + return xslAttrName + " " + nameUseAttributeSets, true + case nameVariable, nameParam, lexicon.XSLTElementWithParam: return textNameSelect, true - case "apply-templates": - return "select mode", true - case "call-template", "processing-instruction": + case lexicon.XSLTElementApplyTemplates: + return xslAttrSelect + " " + lexicon.AttrMode, true + case lexicon.XSLTElementCallTemplate, lexicon.XSLTElementProcessingInstruction: return xslAttrName, true - case "for-each", "copy-of": + case lexicon.XSLTElementForEach, lexicon.XSLTElementCopyOf: return xslAttrSelect, true case nameSort: - return "select lang data-type order case-order", true - case "if", "when": + return xslAttrSelect + " " + lexicon.AttrLang + " " + nameDataType + " " + nameOrder + " " + nameCaseOrder, true + case lexicon.XSLTElementIf, lexicon.XSLTElementWhen: return lexicon.AttrTest, true - case "value-of": - return "select disable-output-escaping", true + case lexicon.XSLTElementValueOf: + return xslAttrSelect + " " + nameDisableOutputEscaping, true case lexicon.MethodText: return nameDisableOutputEscaping, true - case "copy": + case lexicon.XSLTElementCopy: return nameUseAttributeSets, true - case "element": - return "name namespace use-attribute-sets", true - case "attribute": - return "name namespace", true + case lexicon.XSLTElementElement: + return xslAttrName + " " + nameNamespace + " " + nameUseAttributeSets, true + case lexicon.XSLTElementAttribute: + return xslAttrName + " " + nameNamespace, true case lexicon.TypeNumber: - return "level count from value format lang letter-value grouping-separator grouping-size", true - case "message": - return "terminate", true + return nameLevel + " " + lexicon.AttrCount + " " + lexicon.AttrFrom + " " + lexicon.AttrValue + " " + nameFormat + " " + lexicon.AttrLang + " " + nameLetterValue + " " + nameGroupingSeparator + " " + nameGroupingSize, true + case lexicon.XSLTElementMessage: + return nameTerminate, true } var zero string @@ -94,14 +94,14 @@ func forwards10(e *xdm.Element) bool { if elem, ok := n.(*xdm.Element); ok { version := "" if elem.URI() != lexicon.NamespaceXSLT { - version, _ = elem.GetAttributeNS(lexicon.SerializationVersion, lexicon.NamespaceXSLT) - } else if elem.LocalName() == nameStylesheet || elem.LocalName() == fnNameTransform { - version = getAttr(elem, lexicon.SerializationVersion) + version, _ = elem.GetAttributeNS(xslAttrVersion, lexicon.NamespaceXSLT) + } else if elem.LocalName() == nameStylesheet || elem.LocalName() == lexicon.XSLTElementTransform { + version = getAttr(elem, xslAttrVersion) } if version != "" { - v, err := strconv.ParseFloat(version, 64) - return err == nil && v > 1 + v, err := strconv.ParseFloat(version, float64ParseBits) + return err == nil && v > xsltMajorVersion1 } } } @@ -120,9 +120,9 @@ func ignoredOptional10(e *xdm.Element, a *xdm.Attribute) bool { def := elementXSLTDef(e.LocalName()).attrs[a.LocalName()] // Only these closed enumerations are AVTs in 1.0. In particular, // xsl:message/@terminate became an AVT in a later language version. - avt10 := e.LocalName() == nameSort && contains10Word("order case-order", a.LocalName()) || e.LocalName() == lexicon.TypeNumber && a.LocalName() == nameLetterValue + avt10 := e.LocalName() == nameSort && contains10Word(nameOrder+" "+nameCaseOrder, a.LocalName()) || e.LocalName() == lexicon.TypeNumber && a.LocalName() == nameLetterValue if e.LocalName() == nameOutput && a.LocalName() == lexicon.SerializationStandalone { - def.values = []string{lexicon.ValueYes, "no"} + def.values = []string{lexicon.ValueYes, lexicon.ValueNo} } if e.LocalName() == nameOutput && a.LocalName() == lexicon.SerializationMethod && !strings.Contains(a.Value(), ":") { @@ -149,13 +149,13 @@ func prepareModule(ctx context.Context, doc *xdm.Document, processor int) (*xdm. root := doc.DocumentElement() ignored := unknownAttribute20 - if processor >= 30 { + if processor >= processorXSLT30 { ignored = func(elem *xdm.Element, attr *xdm.Attribute) bool { - return elem.URI() == lexicon.NamespaceXSLT && attr.URI() == "" && strings.HasPrefix(attr.LocalName(), "_") + return elem.URI() == lexicon.NamespaceXSLT && attr.URI() == "" && strings.HasPrefix(attr.LocalName(), shadowAttributePrefix) } } - if processor == 10 { + if processor == processorXSLT10 { if err := validateModuleRoot10(root); err != nil { return nil, err } @@ -165,20 +165,20 @@ func prepareModule(ctx context.Context, doc *xdm.Document, processor int) (*xdm. needed, err := inspectModulePreparation(ctx, root, false, func(elem *xdm.Element, attr *xdm.Attribute) bool { conditional := attr.LocalName() == xslAttrUseWhen && (elem.URI() == lexicon.NamespaceXSLT && attr.URI() == "" || elem.URI() != lexicon.NamespaceXSLT && attr.URI() == lexicon.NamespaceXSLT) - return ignored(elem, attr) || processor >= 20 && conditional + return ignored(elem, attr) || processor >= processorXSLT20 && conditional }) if err != nil { return nil, err } if root.URI() != lexicon.NamespaceXSLT { - if _, simplified := root.GetAttributeNS(lexicon.SerializationVersion, lexicon.NamespaceXSLT); simplified { + if _, simplified := root.GetAttributeNS(xslAttrVersion, lexicon.NamespaceXSLT); simplified { prepared, err := expandSimplifiedModule(ctx, doc, root) if err != nil { return nil, err } - if processor < 30 { + if processor < processorXSLT30 { filterModuleAttributes(prepared.DocumentElement(), ignored) } @@ -206,7 +206,7 @@ func prepareModule(ctx context.Context, doc *xdm.Document, processor int) (*xdm. prepared.SetURL(doc.URL()) - if processor < 30 { + if processor < processorXSLT30 { filterModuleAttributes(prepared.DocumentElement(), ignored) } @@ -224,7 +224,7 @@ func unknownAttribute10(elem *xdm.Element, attr *xdm.Attribute) bool { return attr.URI() == "" && !contains10Word(xslt10Attributes(elem.LocalName()), attr.LocalName()) } - return attr.URI() == lexicon.NamespaceXSLT && !contains10Word("version extension-element-prefixes exclude-result-prefixes use-attribute-sets", attr.LocalName()) + return attr.URI() == lexicon.NamespaceXSLT && !contains10Word(xslAttrVersion+" "+nameExtensionElementPrefixes+" "+nameExcludeResultPrefixes+" "+nameUseAttributeSets, attr.LocalName()) } func inspectModulePreparation(ctx context.Context, elem *xdm.Element, needed bool, ignored func(*xdm.Element, *xdm.Attribute) bool) (bool, error) { @@ -371,7 +371,7 @@ func stylesheetWhitespaceParent(parent *tree.Node) bool { } switch parent.Name.Local { - case lexicon.XSLTElementAccumulator, lexicon.XSLTElementAnalyzeString, lexicon.XSLTElementApplyImports, lexicon.XSLTElementApplyTemplates, lexicon.XSLTElementAttributeSet, lexicon.XSLTElementCallTemplate, lexicon.XSLTElementCharacterMap, lexicon.XSLTElementChoose, lexicon.XSLTElementEvaluate, lexicon.XSLTElementFork, lexicon.XSLTElementMerge, lexicon.XSLTElementMergeSource, lexicon.XSLTElementMode, lexicon.XSLTElementNextIteration, lexicon.XSLTElementNextMatch, lexicon.XSLTElementOverride, lexicon.XSLTElementPackage, nameStylesheet, fnNameTransform, lexicon.XSLTElementUsePackage: + case lexicon.XSLTElementAccumulator, lexicon.XSLTElementAnalyzeString, lexicon.XSLTElementApplyImports, lexicon.XSLTElementApplyTemplates, lexicon.XSLTElementAttributeSet, lexicon.XSLTElementCallTemplate, lexicon.XSLTElementCharacterMap, lexicon.XSLTElementChoose, lexicon.XSLTElementEvaluate, lexicon.XSLTElementFork, lexicon.XSLTElementMerge, lexicon.XSLTElementMergeSource, lexicon.XSLTElementMode, lexicon.XSLTElementNextIteration, lexicon.XSLTElementNextMatch, lexicon.XSLTElementOverride, lexicon.XSLTElementPackage, nameStylesheet, lexicon.XSLTElementTransform, lexicon.XSLTElementUsePackage: return true } @@ -399,25 +399,25 @@ func (c *compiler) sourceModule(doc *xdm.Document) *xdm.Document { } func (c *compiler) checkFragment10(doc *xdm.Document, fragment string) error { - if fragment == "" || c.stylesheet.processorLanguage() != 10 { + if fragment == "" || c.stylesheet.processorLanguage() != processorXSLT10 { return nil } root := findElementByID(c.sourceModule(doc), fragment) if root == nil { - return staticError("XTSE0010", "no embedded stylesheet with id=%q", fragment) + return staticError(errCodeXTSE0010, "no embedded stylesheet with id=%q", fragment) } return checkLanguage10(root) } func (c *compiler) forwardsCompatible(version string) bool { - comparison, valid := compareLanguageVersion(version, c.stylesheet.processorLanguage()/10) + comparison, valid := compareLanguageVersion(version, c.stylesheet.processorLanguage()/processorVersionScale) return valid && comparison > 0 } func checkLanguage10(root *xdm.Element) error { - return checkElementLanguage10(root, root, 1) + return checkElementLanguage10(root, root, xsltMajorVersion1) } func checkElementLanguage10(root, elem *xdm.Element, inherited float64) error { @@ -446,17 +446,17 @@ func checkElementLanguage10(root, elem *xdm.Element, inherited float64) error { func elementLanguageVersion10(root, elem *xdm.Element, inherited float64) (float64, error) { effective := inherited - if version, ok := elem.GetAttributeNS(lexicon.SerializationVersion, lexicon.NamespaceXSLT); ok { - n, err := strconv.ParseFloat(version, 64) + if version, ok := elem.GetAttributeNS(xslAttrVersion, lexicon.NamespaceXSLT); ok { + n, err := strconv.ParseFloat(version, float64ParseBits) if err != nil || !(n > 0) || math.IsInf(n, 0) { - return 0, staticError("XTSE0110", "invalid XSLT version") + return 0, staticError(errCodeXTSE0110, "invalid XSLT version") } effective = n } if elem == root && elem.URI() == lexicon.NamespaceXSLT { - effective, _ = strconv.ParseFloat(getAttr(root, lexicon.SerializationVersion), 64) + effective, _ = strconv.ParseFloat(getAttr(root, xslAttrVersion), float64ParseBits) } return effective, nil @@ -464,8 +464,8 @@ func elementLanguageVersion10(root, elem *xdm.Element, inherited float64) (float func checkXSLTElement10(elem *xdm.Element, effective float64) error { allowed, known := lookupXSLT10Attribute(elem.LocalName()) - if !known && effective <= 1 { - return staticError("XTSE0010", "xsl:%s is unavailable in XSLT 1.0", elem.LocalName()) + if !known && effective <= xsltMajorVersion1 { + return staticError(errCodeXTSE0010, "xsl:%s is unavailable in XSLT 1.0", elem.LocalName()) } for _, attr := range elem.Attributes() { @@ -474,11 +474,11 @@ func checkXSLTElement10(elem *xdm.Element, effective float64) error { } if attr.URI() == lexicon.NamespaceXSLT { - return staticError("XTSE0090", "XSLT attributes on XSLT elements") + return staticError(errCodeXTSE0090, "XSLT attributes on XSLT elements") } - if attr.URI() == "" && effective <= 1 && !contains10Word(allowed, attr.LocalName()) { - return staticError("XTSE0090", "%s on xsl:%s is unavailable in XSLT 1.0", attr.LocalName(), elem.LocalName()) + if attr.URI() == "" && effective <= xsltMajorVersion1 && !contains10Word(allowed, attr.LocalName()) { + return staticError(errCodeXTSE0090, "%s on xsl:%s is unavailable in XSLT 1.0", attr.LocalName(), elem.LocalName()) } } @@ -487,8 +487,8 @@ func checkXSLTElement10(elem *xdm.Element, effective float64) error { func checkLiteralResultAttributes10(elem *xdm.Element, effective float64) error { for _, attr := range elem.Attributes() { - if attr.URI() == lexicon.NamespaceXSLT && effective <= 1 && !contains10Word("version extension-element-prefixes exclude-result-prefixes use-attribute-sets", attr.LocalName()) { - return staticError("XTSE0090", "unknown XSLT 1.0 literal-result attribute %s", attr.LocalName()) + if attr.URI() == lexicon.NamespaceXSLT && effective <= xsltMajorVersion1 && !contains10Word(xslAttrVersion+" "+nameExtensionElementPrefixes+" "+nameExcludeResultPrefixes+" "+nameUseAttributeSets, attr.LocalName()) { + return staticError(errCodeXTSE0090, "unknown XSLT 1.0 literal-result attribute %s", attr.LocalName()) } } @@ -507,15 +507,15 @@ func contains10Word(words, word string) bool { func lookupXSLT10FunctionArity(key string) ([2]int, bool) { switch key { - case "current": + case fnNameCurrent: return [2]int{0, 0}, true case fnNameDocument: return [2]int{1, 2}, true - case nameKey2: + case keyLocalName: return [2]int{2, 2}, true case lexicon.FnFormatNumber: return [2]int{2, 3}, true - case lexicon.FnGenerateID, nameUnparsedEntityURI, funcSystemProperty, "element-available", "function-available": + case lexicon.FnGenerateID, nameUnparsedEntityURI, funcSystemProperty, fnNameElementAvailable, fnNameFunctionAvailable: return [2]int{1, 1}, true } @@ -541,7 +541,7 @@ func (f function10) Call(ctx context.Context, args []xdm.Sequence) (xdm.Sequence if err := prepareDocumentArguments10(ctx, args); err != nil { return nil, err } - case nameKey2: + case keyLocalName: if err := prepareKeyArguments10(ctx, args); err != nil { return nil, err } @@ -553,7 +553,7 @@ func (f function10) Call(ctx context.Context, args []xdm.Sequence) (xdm.Sequence if err := prepareFormatNumberArguments10(ctx, args); err != nil { return nil, err } - case funcSystemProperty, "element-available", "function-available", nameUnparsedEntityURI: + case funcSystemProperty, fnNameElementAvailable, fnNameFunctionAvailable, nameUnparsedEntityURI: if err := convertStringArgument10(ctx, args, 0); err != nil { return nil, err } @@ -670,7 +670,7 @@ func checkPattern10(ast xpath.Expr) error { case xpath.LocationPath: for _, step := range e.Steps { if step.Axis != xpath.AxisChild && step.Axis != xpath.AxisAttribute && step.Axis != xpath.AxisDescendantOrSelf { - return staticError("XTSE0340", "invalid XSLT 1.0 pattern axis") + return staticError(errCodeXTSE0340, "invalid XSLT 1.0 pattern axis") } } @@ -689,31 +689,31 @@ func checkPattern10(ast xpath.Expr) error { return nil } - return staticError("XTSE0340", "invalid XSLT 1.0 match pattern") + return staticError(errCodeXTSE0340, "invalid XSLT 1.0 match pattern") } func checkFunctionPattern10(e xpath.FunctionCall) error { - if e.Prefix != "" || e.Name != "id" && e.Name != nameKey2 { - return staticError("XTSE0340", "invalid XSLT 1.0 match pattern") + if e.Prefix != "" || e.Name != fnNameID && e.Name != keyLocalName { + return staticError(errCodeXTSE0340, "invalid XSLT 1.0 match pattern") } want := 1 - if e.Name == nameKey2 { + if e.Name == keyLocalName { want = 2 } if len(e.Args) != want { - return staticError("XTSE0340", "invalid XSLT 1.0 match pattern") + return staticError(errCodeXTSE0340, "invalid XSLT 1.0 match pattern") } for _, arg := range e.Args { v, ok := arg.(xpath.LiteralExpr) if !ok { - return staticError("XTSE0340", "id/key patterns require string literals") + return staticError(errCodeXTSE0340, "id/key patterns require string literals") } if _, ok := v.Value.(string); !ok { - return staticError("XTSE0340", "id/key patterns require string literals") + return staticError(errCodeXTSE0340, "id/key patterns require string literals") } } @@ -721,7 +721,9 @@ func checkFunctionPattern10(e xpath.FunctionCall) error { } func finalizeOutput10(s *Stylesheet) { - if s.processorLanguage() != 10 { + const legacyHTMLDefaultVersion = "4.0" + + if s.processorLanguage() != processorXSLT10 { return } @@ -730,9 +732,9 @@ func finalizeOutput10(s *Stylesheet) { } for _, out := range s.outputs { - out.languageVersion = 10 + out.languageVersion = processorXSLT10 if out.HTMLVersion == "" { - out.HTMLVersion = "4.0" + out.HTMLVersion = legacyHTMLDefaultVersion } } } @@ -740,7 +742,7 @@ func finalizeOutput10(s *Stylesheet) { // validateModuleRoot10 leaves embedded modules for validation after their // fragment root is selected, while checking ordinary and simplified modules now. func validateModuleRoot10(root *xdm.Element) error { - _, simplified := root.GetAttributeNS(lexicon.SerializationVersion, lexicon.NamespaceXSLT) + _, simplified := root.GetAttributeNS(xslAttrVersion, lexicon.NamespaceXSLT) if root.URI() == lexicon.NamespaceXSLT || simplified { return checkLanguage10(root) } diff --git a/transform/internal/xslt/qname_resolve.go b/transform/internal/xslt/qname_resolve.go index 535831d..9e8676b 100644 --- a/transform/internal/xslt/qname_resolve.go +++ b/transform/internal/xslt/qname_resolve.go @@ -105,7 +105,7 @@ func resolveSequenceTypeQName(qname string, kind qnameKind, resolve nsResolver, // xs:/xsd: are conventional aliases for the XSD namespace when naming a // type; for element/attribute names they are ordinary prefixes resolved // through the binding map (so xsd: works only if declared). - if kind == qnameTypeName && (prefix == "xs" || prefix == nameXsd) { + if kind == qnameTypeName && (prefix == lexicon.PrefixXS || prefix == lexicon.PrefixXSD) { return loc, lexicon.NamespaceXSD } // The xml prefix is predeclared everywhere; an explicit binding overrides. diff --git a/transform/internal/xslt/serialize_encoding.go b/transform/internal/xslt/serialize_encoding.go index 582034e..1b6077c 100644 --- a/transform/internal/xslt/serialize_encoding.go +++ b/transform/internal/xslt/serialize_encoding.go @@ -20,6 +20,12 @@ import ( // encodedOutput keeps encoder state across chunks and never closes dst. func encodedOutput(ctx context.Context, dst io.Writer, definition *OutputDef) (io.Writer, func() error, error) { + const ( + outputEncodingASCII = "ascii" + outputEncodingUTF16BE = "utf-16be" + outputEncodingUTF16LE = "utf-16le" + ) + name := strings.ToLower(definition.Encoding) var ( @@ -30,24 +36,24 @@ func encodedOutput(ctx context.Context, dst io.Writer, definition *OutputDef) (i switch name { case "", lexicon.EncodingUTF8, lexicon.EncodingUTF8Alt: if definition.ByteOrderMark { - bom = []byte{0xef, 0xbb, 0xbf} + bom = []byte(lexicon.BOMUTF8) } - case lexicon.EncodingUTF16, nameUtf16: + case lexicon.EncodingUTF16, nameUTF16: encoder = textunicode.UTF16(textunicode.BigEndian, textunicode.IgnoreBOM).NewEncoder() - bom = []byte{0xfe, 0xff} - case "utf-16be": + bom = []byte(lexicon.BOMUTF16BE) + case outputEncodingUTF16BE: encoder = textunicode.UTF16(textunicode.BigEndian, textunicode.IgnoreBOM).NewEncoder() if definition.ByteOrderMark { - bom = []byte{0xfe, 0xff} + bom = []byte(lexicon.BOMUTF16BE) } - case "utf-16le": + case outputEncodingUTF16LE: encoder = textunicode.UTF16(textunicode.LittleEndian, textunicode.IgnoreBOM).NewEncoder() if definition.ByteOrderMark { - bom = []byte{0xff, 0xfe} + bom = []byte(lexicon.BOMUTF16LE) } - case "ascii", "us-ascii": + case outputEncodingASCII, encodingASCII: encoder = &encoding.Encoder{Transformer: asciiEncoder{}} definition.canEncode = func(r rune) bool { return r < utf8.RuneSelf } default: @@ -81,7 +87,7 @@ type asciiEncoder struct{ texttransform.NopResetter } func (asciiEncoder) Transform(dst, src []byte, _ bool) (int, int, error) { for i, c := range src { if c >= utf8.RuneSelf { - return i, i, dynamicError("SERE0008", "character cannot be represented in ASCII") + return i, i, dynamicError(lexicon.ErrSERE0008, "character cannot be represented in ASCII") } if i == len(dst) { diff --git a/transform/internal/xslt/serialize_html.go b/transform/internal/xslt/serialize_html.go index e50f47c..d3a9372 100644 --- a/transform/internal/xslt/serialize_html.go +++ b/transform/internal/xslt/serialize_html.go @@ -14,10 +14,14 @@ import ( ) func streamHTMLValue(ctx context.Context, w io.Writer, result ValueResult, definition *OutputDef) error { + const ( + htmlSerializationBufferBytes = 4096 + ) + writer := htmlResultWriter(definition) explicitDoctype := definition.DoctypePublic != "" || definition.DoctypeSystem != "" - buffer := bufio.NewWriterSize(w, 4096) + buffer := bufio.NewWriterSize(w, htmlSerializationBufferBytes) sequence := serializationSequence{ctx: ctx, definition: definition} items := func(yield func(xdm.XMLItem, error) bool) { emitted := false @@ -62,7 +66,7 @@ func htmlResultWriter(definition *OutputDef) htmlpkg.Writer { normalization := definition.NormalizationForm if normalization == lexicon.NormFullyNormalized { - normalization = normalizationFormNFC + normalization = lexicon.NormalizationFormNFC } writer := htmlpkg.NewWriter().DefaultDTD(false).Format(false).PreserveCase(true). @@ -79,15 +83,15 @@ func htmlResultWriter(definition *OutputDef) htmlpkg.Writer { if definition.IncludeContentType == nil || *definition.IncludeContentType { media := definition.MediaType if media == "" { - media = "text/html" + media = defaultHTMLMediaType } encoding := definition.Encoding if encoding == "" { - encoding = "UTF-8" + encoding = lexicon.EncodingUTF8U } - writer = writer.ContentType(media + "; charset=" + encoding) + writer = writer.ContentType(media + contentTypeCharsetParameter + encoding) } return writer @@ -115,7 +119,7 @@ func effectiveStreamMethod(result ValueResult, definition *OutputDef) string { return lexicon.MethodHTML } - if definition.languageVersion != 10 && !definition.implicitXMLMethod && root.URI() == lexicon.NamespaceXHTML { + if definition.languageVersion != processorXSLT10 && !definition.implicitXMLMethod && root.URI() == lexicon.NamespaceXHTML { return lexicon.MethodXHTML } } diff --git a/transform/internal/xslt/serialize_sequence.go b/transform/internal/xslt/serialize_sequence.go index 755945e..ee43969 100644 --- a/transform/internal/xslt/serialize_sequence.go +++ b/transform/internal/xslt/serialize_sequence.go @@ -20,7 +20,12 @@ type serializationSequence struct { } func (s *serializationSequence) items(seq xdm.Sequence) iter.Seq2[xdm.XMLItem, error] { - coalesce := s.definition.NormalizationForm != "" && s.definition.NormalizationForm != "none" && s.definition.NormalizationForm != "NONE" + const ( + normalizationFormNoneLower = "none" + ) + + coalesce := s.definition.NormalizationForm != "" && s.definition.NormalizationForm != normalizationFormNoneLower && s.definition.NormalizationForm != normalizationFormNone + return xpath.SerializationItems(s.ctx, seq, s.definition.ItemSeparator, coalesce) } @@ -66,9 +71,14 @@ func streamXMLValue(ctx context.Context, w io.Writer, result ValueResult, defini } func writeXMLProlog(w io.Writer, definition *OutputDef) error { + const ( + xmlDeclarationFormat = "" @@ -98,12 +108,12 @@ func writeXMLProlog(w io.Writer, definition *OutputDef) error { func streamXMLWriteOptions(definition *OutputDef, encoding string) tree.XMLWriteOptions { version := validOutputXMLVersion(definition.Version) if version == "" { - version = lexicon.XSLTVersion10 + version = lexicon.XMLVersion10 } options := tree.XMLWriteOptions{EscapeControlChars: true, CanEncode: definition.canEncode, Version: version, Indent: definition.Indent, ASCII: encoding == encodingASCII, CharacterMap: definition.ResolvedCharMap, Normalization: definition.NormalizationForm, PrefixUndeclarations: definition.UndeclarePrefixes} if options.Normalization == lexicon.NormFullyNormalized { - options.Normalization = "NFC" + options.Normalization = lexicon.NormalizationFormNFC } if len(definition.CDATASections) > 0 { diff --git a/transform/internal/xslt/serialize_text.go b/transform/internal/xslt/serialize_text.go index c55ab9f..2ec521a 100644 --- a/transform/internal/xslt/serialize_text.go +++ b/transform/internal/xslt/serialize_text.go @@ -59,6 +59,10 @@ type textValueWriter struct { } func (w *textValueWriter) plain(value string) error { + const ( + textSerializationChunkBytes = 4096 + ) + target := w.destination if w.form != nil { if err := xpath.ChargeString(w.ctx, len(value)); err != nil { @@ -73,7 +77,7 @@ func (w *textValueWriter) plain(value string) error { } for len(value) > 0 { - end := min(4096, len(value)) + end := min(textSerializationChunkBytes, len(value)) if err := writeFullString(target, value[:end]); err != nil { return err } diff --git a/transform/internal/xslt/shared_constants.go b/transform/internal/xslt/shared_constants.go index 680e38a..7f6903f 100644 --- a/transform/internal/xslt/shared_constants.go +++ b/transform/internal/xslt/shared_constants.go @@ -2,6 +2,80 @@ package xslt import "github.com/skarm/xmlsec/internal/lexicon" +// xslAttrRequired names the unqualified boolean attribute on parameter +// declarations. It is separate from the required value of context-item/@use. +const xslAttrRequired = "required" + +// Draft identity-sensitive attribute spelling shared by legacy compiler checks and the grammar that rejects it in XSLT 3.0; this naming does not broaden support. +const ( + nameIdentitySensitive = "identity-sensitive" +) + +// Reserved initial-template local name in the XSLT namespace; explicit invocation names still resolve through their namespace context. +const ( + initialTemplateLocalName = "initial-template" +) + +// The XSLT language-version attribute (unqualified on instructions, XSLT-qualified on literal result elements); serialization version and system-property names retain their separate roles. +const ( + xslAttrVersion = "version" +) + +// Unqualified xsl:output-character/@string supplies replacement text; it is independent of the xs:string atomic type. +const ( + nameOutputCharacterString = "string" +) + +// strconv.ParseFloat result precision for binary64 host values; it is independent of operation-accounting granularity and index cardinality. +const ( + float64ParseBits = 64 +) + +// Unqualified XSLT attribute and closed selector names shared by grammar and compilation; namespace, version and allowed-value checks remain at their call sites. +const ( + nameAs = "as" + nameBaseURI = "base-uri" + nameCache = "cache" + nameCharacter = "character" + nameCopyAccumulators = "copy-accumulators" + nameCopyNamespaces = "copy-namespaces" + nameDeclaredModes = "declared-modes" + nameErrorCode = "error-code" + nameErrors = "errors" + nameFlags = "flags" + nameForEachItem = "for-each-item" + nameForEachSource = "for-each-source" + nameGroupAdjacent = "group-adjacent" + nameGroupBy = "group-by" + nameGroupEndingWith = "group-ending-with" + nameGroupStartingWith = "group-starting-with" + nameInitialValue = "initial-value" + nameLevel = "level" + nameNamespaceContext = "namespace-context" + nameNewEachTime = "new-each-time" + nameOnMultipleMatch = "on-multiple-match" + nameOnNoMatch = "on-no-match" + nameOrdinal = "ordinal" + nameOutputVersion = "output-version" + nameOverrideExtensionFunction = "override-extension-function" + namePhase = "phase" + namePriority = "priority" + nameRegex = "regex" + nameResultPrefix = "result-prefix" + nameRollbackOutput = "rollback-output" + nameSchemaAware = "schema-aware" + nameSeparator = "separator" + nameSortBeforeMerge = "sort-before-merge" + nameStable = "stable" + nameStreamability = "streamability" + nameStylesheetPrefix = "stylesheet-prefix" + nameTerminate = "terminate" + nameWarningOnMultipleMatch = "warning-on-multiple-match" + nameWarningOnNoMatch = "warning-on-no-match" + nameWithParams = "with-params" + nameXPath = "xpath" +) + const ( nameFormat = "format" nameLetterValue = "letter-value" @@ -12,7 +86,6 @@ const ( nameProductName = "product-name" nameAccumulatorAfter = "accumulator-after" nameAccumulatorBefore = "accumulator-before" - nameAcht = "acht" nameAttributeSet = lexicon.XSLTElementAttributeSet nameCaseOrder = "case-order" nameCollation = "collation" @@ -21,41 +94,41 @@ const ( nameCurrentMergeGroup = "current-merge-group" nameCurrentMergeKey = "current-merge-key" nameDataType = "data-type" - nameDecimalSeparator = "decimal-separator" + nameDecimalSeparator = lexicon.DecimalFormatDecimalSeparator nameDefaultCollation = "default-collation" nameDefaultMode = "default-mode" nameDefaultValidation = "default-validation" - nameDigit = "digit" + nameDigit = lexicon.DecimalFormatDigit nameDisableOutputEscaping = "disable-output-escaping" nameDoc = "doc" nameElements = "elements" nameEmptySequence = "empty-sequence()" nameExcludeResultPrefixes = "exclude-result-prefixes" nameExpandText = "expand-text" - nameExponentSeparator = "exponent-separator" + nameExponentSeparator = lexicon.DecimalFormatExponentSeparator nameExtensionElementPrefixes = "extension-element-prefixes" nameFallback = lexicon.XSLTElementFallback - nameGroupingSeparator = "grouping-separator" + nameGroupingSeparator = lexicon.DecimalFormatGroupingSeparator nameImport = lexicon.XSLTElementImport nameInclude = lexicon.XSLTElementInclude - nameInfinity = "infinity" + nameInfinity = lexicon.DecimalFormatInfinity nameInheritNamespaces = "inherit-namespaces" nameInputTypeAnnotations = "input-type-annotations" nameIsSchemaAware = "is-schema-aware" - nameKey2 = lexicon.XSLTElementKey + keyLocalName = lexicon.XSLTElementKey nameLowerFirst = "lower-first" nameMaybe = "maybe" - nameMinusSign = "minus-sign" - textNameSelect = "name select" + nameMinusSign = lexicon.DecimalFormatMinusSign + textNameSelect = xslAttrName + " " + xslAttrSelect nameNamespace = lexicon.XSLTElementNamespace nameOrder = "order" nameOutput = lexicon.XSLTElementOutput namePackage = lexicon.XSLTElementPackage namePackageVersion = "package-version" nameParam = lexicon.XSLTElementParam - namePatternSeparator = "pattern-separator" - namePerMille = "per-mille" - namePercent = "percent" + namePatternSeparator = lexicon.DecimalFormatPatternSeparator + namePerMille = lexicon.DecimalFormatPerMille + namePercent = lexicon.DecimalFormatPercent namePreserveSpace = lexicon.XSLTElementPreserveSpace nameRegexGroup = "regex-group" nameSingle = "single" @@ -80,10 +153,9 @@ const ( nameUseAccumulators = "use-accumulators" nameUseAttributeSets = "use-attribute-sets" nameUsePackage = lexicon.XSLTElementUsePackage - nameUtf16 = "utf16" + nameUTF16 = "utf16" nameValidation = "validation" nameVariable = lexicon.XSLTElementVariable - nameXpathDefaultNamespace = "xpath-default-namespace" - nameXsd = "xsd" - nameZeroDigit = "zero-digit" + nameXPathDefaultNamespace = "xpath-default-namespace" + nameZeroDigit = lexicon.DecimalFormatZeroDigit ) diff --git a/transform/internal/xslt/sort.go b/transform/internal/xslt/sort.go index d0f025d..d5ea691 100644 --- a/transform/internal/xslt/sort.go +++ b/transform/internal/xslt/sort.go @@ -20,6 +20,24 @@ import ( "github.com/skarm/xmlsec/transform/internal/xpath" ) +// UCA URI query parameters synthesized from XSLT lang/case-order in sort and merge; explicit collation URIs retain their independent resolution path. +const ( + ucaLanguageParameter = "lang=" + ucaLowerFirstParameter = "caseFirst=lower" + ucaUpperFirstParameter = "caseFirst=upper" +) + +// Convert subsecond nanoseconds to seconds for both approximate and exact temporal sort keys. +const ( + temporalNanosecondsPerSecond = 1e9 +) + +// Whole sort-level promotion category: exact values, binary32, or binary64; distinct from the broader atomic conversion rank. +const ( + sortNumericExact = 0 + sortNumericFloat = 1 +) + const ( sortOrderAscending = "ascending" sortOrderDescending = "descending" @@ -118,7 +136,7 @@ type keyedN[T any] struct { type keyedSlice[T any] []keyedN[T] func (ks keyedSlice[T]) convertAutoNumeric(level int, exact bool) int { - rank := 0 + rank := sortNumericExact for j := range ks { sv := &ks[j].keys[level] @@ -131,7 +149,7 @@ func (ks keyedSlice[T]) convertAutoNumeric(level int, exact bool) int { } } - if exact && rank == 1 { + if exact && rank == sortNumericFloat { for i := range ks { sv := &ks[i].keys[level] sv.num = sortFloat32(*sv) @@ -165,7 +183,7 @@ func compareSortValues(a, b sortValue, level resolvedLevel) int { case sortModeBinary: c = bytes.Compare(a.binary, b.binary) case sortModeNumber: - if level.exact && level.floatRank == 0 && a.precise != nil && b.precise != nil { + if level.exact && level.floatRank == sortNumericExact && a.precise != nil && b.precise != nil { c = a.precise.Cmp(b.precise) } else { c = compareFloat64(a, b) @@ -278,7 +296,7 @@ func (ec *execContext) sortSequenceValue(ctx context.Context, seq xdm.Sequence, "sort key value is a sequence of %d items; a single value is required", seqLen) } - if ec.stylesheet.processorLanguage() == 10 { + if ec.stylesheet.processorLanguage() == processorXSLT10 { strVal, err = xpath.XPath10String(seq) if err != nil { return sortValue{}, err @@ -440,6 +458,57 @@ func (ec *execContext) outputSortedNodes(ctx context.Context, nodes []xdm.Node, return nil } +func (ec *execContext) outputSortedAtomicItems(seq xdm.Sequence) error { + // Output atomic items separated by spaces + idx := 0 + for item := range xdm.SequenceItems(seq) { + if idx > 0 { + sep := ec.resultDoc.CreateText(" ") + if err := ec.addNode(sep); err != nil { + return err + } + } + + av, ok := item.(xdm.AtomicValue) + if !ok { + continue + } + + s, err := xpath.AtomicToString(av) + if err != nil { + continue + } + + text := ec.resultDoc.CreateText(s) + if err := ec.addNode(text); err != nil { + return err + } + + idx++ + } + + return nil +} + +// performSortInput evaluates a select or sequence constructor without forcing +// materialization before sort-attribute validation. +func (ec *execContext) performSortInput(ctx context.Context, inst *performSortInst) (xdm.Sequence, error) { + if inst.Select != nil { + result, err := ec.evalXPath(ctx, inst.Select, ec.contextNode) + if err != nil { + return nil, err + } + + return result.Sequence(), nil + } + + if len(inst.Body) > 0 { + return ec.evaluateBodyAsSequence(ctx, inst.Body) + } + + return nil, nil +} + func numericSortValue10(seq xdm.Sequence) (sortValue, error) { number, err := xpath.XPath10Number(seq) if err != nil { @@ -469,6 +538,11 @@ func sortItems(ctx context.Context, ec *execContext, items xdm.Sequence, sortKey return items, nil } + focus := ec.captureExecutionFocus() + defer ec.restoreExecutionFocus(focus) + + ec.size = xdm.SequenceLen(items) + if len(sortKeys) == 1 { return sortItems1(ctx, ec, items, sortKeys[0]) } @@ -499,6 +573,7 @@ func sortItems1(ctx context.Context, ec *execContext, items xdm.Sequence, sk *so for i := range xdm.SequenceLen(items) { evalState.SetPosition(i + 1) + ec.position = i + 1 item := items.Get(i) ec.contextItem = item @@ -572,6 +647,7 @@ func sortItemsN(ctx context.Context, ec *execContext, items xdm.Sequence, sortKe for i := range xdm.SequenceLen(items) { evalState.SetPosition(i + 1) + ec.position = i + 1 item := items.Get(i) ec.contextItem = item @@ -632,6 +708,11 @@ func sortNodes(ctx context.Context, ec *execContext, nodes []xdm.Node, sortKeys return nodes, nil } + focus := ec.captureExecutionFocus() + defer ec.restoreExecutionFocus(focus) + + ec.size = len(nodes) + if len(sortKeys) == 1 { return sortNodes1(ctx, ec, nodes, sortKeys[0]) } @@ -658,6 +739,7 @@ func sortNodes1(ctx context.Context, ec *execContext, nodes []xdm.Node, sk *sort entries := make([]keyed1[xdm.Node], len(nodes)) for i, node := range nodes { evalState.SetPosition(i + 1) + ec.position = i + 1 ec.currentNode = node @@ -713,6 +795,7 @@ func sortNodesN(ctx context.Context, ec *execContext, nodes []xdm.Node, sortKeys entries := make(keyedSlice[xdm.Node], len(nodes)) for i, node := range nodes { evalState.SetPosition(i + 1) + ec.position = i + 1 ec.currentNode = node @@ -747,62 +830,11 @@ func sortNodesN(ctx context.Context, ec *execContext, nodes []xdm.Node, sortKeys return nodes, nil } -func (ec *execContext) outputSortedAtomicItems(seq xdm.Sequence) error { - // Output atomic items separated by spaces - idx := 0 - for item := range xdm.SequenceItems(seq) { - if idx > 0 { - sep := ec.resultDoc.CreateText(" ") - if err := ec.addNode(sep); err != nil { - return err - } - } - - av, ok := item.(xdm.AtomicValue) - if !ok { - continue - } - - s, err := xpath.AtomicToString(av) - if err != nil { - continue - } - - text := ec.resultDoc.CreateText(s) - if err := ec.addNode(text); err != nil { - return err - } - - idx++ - } - - return nil -} - -// performSortInput evaluates a select or sequence constructor without forcing -// materialization before sort-attribute validation. -func (ec *execContext) performSortInput(ctx context.Context, inst *performSortInst) (xdm.Sequence, error) { - if inst.Select != nil { - result, err := ec.evalXPath(ctx, inst.Select, ec.contextNode) - if err != nil { - return nil, err - } - - return result.Sequence(), nil - } - - if len(inst.Body) > 0 { - return ec.evaluateBodyAsSequence(ctx, inst.Body) - } - - return nil, nil -} - // --- Sort level resolution --- func evaluateSortOption(ctx context.Context, ec *execContext, option *avt) (string, error) { value, err := option.evaluate(ctx, ec.contextNode) - return strings.Trim(value, " \t\r\n"), err + return strings.Trim(value, lexicon.XMLWhitespace), err } // resolveSortOrder evaluates the order AVT (defaulting to ascending) and @@ -850,7 +882,7 @@ func buildResolvedSort(ctx context.Context, ec *execContext, sortKeys []*sortKey return resolvedSort{}, err } - uri = resolveAgainstBaseURI(strings.Trim(uri, " \t\r\n"), sk.BaseURI) + uri = resolveAgainstBaseURI(strings.Trim(uri, lexicon.XMLWhitespace), sk.BaseURI) cmpFn, err := xpath.ResolveCollationCompareFunc(uri) if err != nil { @@ -903,7 +935,7 @@ func resolveLevel1(ctx context.Context, ec *execContext, sk *sortKey) (resolvedL return resolvedLevel{}, err } - uri = resolveAgainstBaseURI(strings.Trim(uri, " \t\r\n"), sk.BaseURI) + uri = resolveAgainstBaseURI(strings.Trim(uri, lexicon.XMLWhitespace), sk.BaseURI) cmpFn, err := xpath.ResolveCollationCompareFunc(uri) if err != nil { @@ -942,9 +974,9 @@ func buildImplicitCollationURI(ctx context.Context, ec *execContext, sk *sortKey return "", err } - lang = strings.Trim(lang, " \t\r\n") + lang = strings.Trim(lang, lexicon.XMLWhitespace) if lang != "" { - params = append(params, "lang="+lang) + params = append(params, ucaLanguageParameter+lang) } } @@ -954,11 +986,11 @@ func buildImplicitCollationURI(ctx context.Context, ec *execContext, sk *sortKey return "", err } - switch strings.Trim(co, " \t\r\n") { + switch strings.Trim(co, lexicon.XMLWhitespace) { case nameUpperFirst: - params = append(params, "caseFirst=upper") + params = append(params, ucaUpperFirstParameter) case nameLowerFirst: - params = append(params, "caseFirst=lower") + params = append(params, ucaLowerFirstParameter) default: if sk.forwards10 { break @@ -973,7 +1005,7 @@ func buildImplicitCollationURI(ctx context.Context, ec *execContext, sk *sortKey return "", nil } - return "http://www.w3.org/2013/collation/UCA?" + strings.Join(params, ";"), nil + return lexicon.CollationUCA + "?" + strings.Join(params, ";"), nil } // validateSortKeyAttrs validates sort key attribute values (order, data-type, @@ -1040,7 +1072,7 @@ func validateSortKeyAttrs(ctx context.Context, ec *execContext, sk *sortKey) err return err } - uri = resolveAgainstBaseURI(strings.Trim(uri, " \t\r\n"), sk.BaseURI) + uri = resolveAgainstBaseURI(strings.Trim(uri, lexicon.XMLWhitespace), sk.BaseURI) if _, err := xpath.ResolveCollationCompareFunc(uri); err != nil { return dynamicError(errCodeXTDE1035, "unknown collation URI %q", uri) @@ -1062,7 +1094,7 @@ func validateSortKeyAttrs(ctx context.Context, ec *execContext, sk *sortKey) err // `eq` but raise XPTY0004 on `lt`) and genuinely incomparable families (e.g. // xs:date vs xs:integer) are correctly rejected with XTDE1030. func sortKeyTypesComparable(a, b xdm.AtomicValue, version int) bool { - if version < 31 && (binarySortType(a) || binarySortType(b)) { + if version < xpathProfile31 && (binarySortType(a) || binarySortType(b)) { return false } @@ -1163,7 +1195,7 @@ func isValidSortDataType(s string) bool { } func validateLiteralSortOption(name, value string) error { - value = strings.Trim(value, " \t\r\n") + value = strings.Trim(value, lexicon.XMLWhitespace) valid := true switch name { @@ -1190,7 +1222,7 @@ func isValidLanguageTag(s string) bool { return true // empty = implementation default } - _, err := xpath.CastFromString(strings.Trim(s, " \t\r\n"), xdm.TypeLanguage) + _, err := xpath.CastFromString(strings.Trim(s, lexicon.XMLWhitespace), xdm.TypeLanguage) return err == nil } @@ -1425,14 +1457,14 @@ func atomicToNumericSortValue(av xdm.AtomicValue, implicitTZ *time.Location) (so t := prim.TimeVal() t = xpath.ApplyImplicitTZ(t, implicitTZ) // Use Unix seconds + fractional nanoseconds to avoid int64 overflow for large years - f := float64(t.Unix()) + float64(t.Nanosecond())/1e9 + f := float64(t.Unix()) + float64(t.Nanosecond())/temporalNanosecondsPerSecond return temporalSortValue(av.TypeName, t, f), true case xdm.TypeTime: t := prim.TimeVal() // xs:time comparison uses reference date 1972-12-31 per F&O §10.4.4 t = xpath.TimeToReferenceDateTime(xpath.ApplyImplicitTZ(t, implicitTZ)) - f := float64(t.Unix()) + float64(t.Nanosecond())/1e9 + f := float64(t.Unix()) + float64(t.Nanosecond())/temporalNanosecondsPerSecond return temporalSortValue(av.TypeName, t, f), true } @@ -1442,7 +1474,7 @@ func atomicToNumericSortValue(av xdm.AtomicValue, implicitTZ *time.Location) (so func temporalSortValue(typeName string, t time.Time, approximate float64) sortValue { precise := new(big.Rat).SetInt64(t.Unix()) - precise.Add(precise, new(big.Rat).SetFrac64(int64(t.Nanosecond()), 1e9)) + precise.Add(precise, new(big.Rat).SetFrac64(int64(t.Nanosecond()), temporalNanosecondsPerSecond)) return sortValue{kind: sortValueNumber, num: approximate, precise: precise, typeName: typeName} } @@ -1527,7 +1559,7 @@ func finalizeLevel1[T any](level *resolvedLevel, dtMode dataTypeMode, entries [] } } - if level.exact && level.floatRank == 1 { + if level.exact && level.floatRank == sortNumericFloat { for i := range entries { entries[i].key.num = sortFloat32(entries[i].key) } @@ -1549,13 +1581,17 @@ func parseToNumericSortValue(s string) sortValue { // Numeric promotion applies to the entire sort level, including comparisons // between two decimals when a third key is a float or double. func sortFloatRank(atom xdm.AtomicValue) int { + const ( + sortNumericDouble = 2 + ) + switch xdm.PromoteSchemaType(atom).TypeName { case xdm.TypeDouble: - return 2 + return sortNumericDouble case xdm.TypeFloat: - return 1 + return sortNumericFloat default: - return 0 + return sortNumericExact } } @@ -1670,7 +1706,7 @@ func sortGroupsN(ctx context.Context, ec *execContext, groups []fegGroup, sortKe func parseNumber(s string) float64 { s = strings.TrimSpace(s) - f, err := strconv.ParseFloat(s, 64) + f, err := strconv.ParseFloat(s, float64ParseBits) if err != nil { return math.NaN() } diff --git a/transform/internal/xslt/stylesheet.go b/transform/internal/xslt/stylesheet.go index c75d2f0..8e3cb13 100644 --- a/transform/internal/xslt/stylesheet.go +++ b/transform/internal/xslt/stylesheet.go @@ -8,6 +8,32 @@ import ( "github.com/skarm/xmlsec/transform/internal/xpath" ) +// Encoded XSLT processor selections are ten times their major version; this scale is not a decimal radix or an XML Schema lexical enum. +const ( + processorVersionScale = 10 +) + +// Encoded XSLT processor selections, independent of a module declaration and the XPath edition selected for its expressions. +const ( + processorXSLT10 = 10 + processorXSLT20 = 20 + processorXSLT30 = 30 +) + +// Major-version boundaries for XSLT compatibility and the fn:transform minimum-processor request, before encoding into processor selections. +const ( + xsltMajorVersion1 = 1 + xsltMajorVersion2 = 2 + xsltMajorVersion3 = 3 +) + +// XPath compiler/evaluator edition selectors; XSLT processor profiles map to these explicitly rather than sharing one version policy. +const ( + xpathProfile10 = 10 + xpathProfile20 = 20 + xpathProfile31 = 31 +) + const ( // Mode sentinel values per the XSLT 3.0 specification. modeDefault = "#default" // the default (unnamed) mode, used as internal key @@ -182,6 +208,7 @@ type modeDef struct { ImportPrec int Implicit bool // created from a template rule, with no matching xsl:expose Streamable bool + HasStreamable bool // streamable was explicitly specified conflictStreamable bool // deferred: conflicting streamable at same prec conflictOnNoMatch bool conflictVisibility bool @@ -279,7 +306,7 @@ func (ss *Stylesheet) DefaultOutputDef() *OutputDef { } out := cloneOutputDef(ss.outputs[""]) - if out == nil && ss.processorLanguage() <= 20 { + if out == nil && ss.processorLanguage() <= processorXSLT20 { out = defaultOutputDef() } @@ -342,7 +369,7 @@ func (ss *Stylesheet) defaultItemSeparator() *string { // processorLanguage also covers internal zero-value stylesheet helpers. func (ss *Stylesheet) processorLanguage() int { if ss.processorVersion == 0 { - return 30 + return processorXSLT30 } return ss.processorVersion @@ -350,12 +377,12 @@ func (ss *Stylesheet) processorLanguage() int { func (ss *Stylesheet) xpathVersion() int { switch ss.processorLanguage() { - case 10: - return 10 - case 20: - return 20 + case processorXSLT10: + return xpathProfile10 + case processorXSLT20: + return xpathProfile20 default: - return 31 + return xpathProfile31 } } diff --git a/transform/internal/xslt/types.go b/transform/internal/xslt/types.go index 4435e56..6f7dd59 100644 --- a/transform/internal/xslt/types.go +++ b/transform/internal/xslt/types.go @@ -13,6 +13,13 @@ import ( "github.com/skarm/xmlsec/transform/internal/xpath" ) +// Sequence-type grammar prefixes shared by canonical rendering and item coercion; these identify type tests, not functions in the XPath function namespace. +const ( + sequenceTypeArrayStart = "array(" + sequenceTypeFunctionStart = "function(" + sequenceTypeMapStart = "map(" +) + // XSLT type error codes. const ( errCodeXTTE0505 = "XTTE0505" // template return type mismatch @@ -21,6 +28,13 @@ const ( errCodeXTTE0790 = "XTTE0790" // function parameter type mismatch ) +// Internal cardinality markers distinguish no occurrence suffix (exactly one) +// from empty-sequence(). The latter marker is not part of the public grammar. +const ( + occurrenceExactlyOne rune = 0 + occurrenceEmptySequence rune = 'E' +) + // sequenceType represents a parsed XSLT/XPath sequence type declaration // from the "as" attribute (e.g., "xs:string*", "element()+", "item()?"). type sequenceType struct { @@ -38,7 +52,7 @@ func parseSequenceType(as string) sequenceType { // empty-sequence() is a special type that matches only the empty sequence if s == nameEmptySequence { - return sequenceType{ItemType: nameEmptySequence, Occurrence: 'E'} + return sequenceType{ItemType: nameEmptySequence, Occurrence: occurrenceEmptySequence} } // Check for occurrence indicator at the end @@ -97,13 +111,13 @@ func checkSequenceType(ctx context.Context, seq xdm.Sequence, st sequenceType, e } switch st.Occurrence { - case 'E': // empty-sequence() — must be empty + case occurrenceEmptySequence: // empty-sequence() — must be empty if count != 0 { return nil, dynamicError(errCode, "%s: required empty-sequence(), got %d items", context, count) } return seq, nil - case 0: // exactly one + case occurrenceExactlyOne: // exactly one if count != 1 { return nil, dynamicError(errCode, "%s: required exactly 1 item, got %d", context, count) } @@ -148,19 +162,19 @@ func isAtomicTargetType(itemType string) bool { return false } // Node tests, function tests, map/array tests are not atomic. - if strings.HasPrefix(itemType, "node(") || - strings.HasPrefix(itemType, "element(") || - strings.HasPrefix(itemType, "attribute(") || - strings.HasPrefix(itemType, "document-node(") || - strings.HasPrefix(itemType, "text(") || - strings.HasPrefix(itemType, "comment(") || - strings.HasPrefix(itemType, "processing-instruction(") || - strings.HasPrefix(itemType, "namespace-node(") || - strings.HasPrefix(itemType, "schema-element(") || - strings.HasPrefix(itemType, "schema-attribute(") || - strings.HasPrefix(itemType, "function(") || - strings.HasPrefix(itemType, "map(") || - strings.HasPrefix(itemType, "array(") { + if strings.HasPrefix(itemType, lexicon.KindNode+"(") || + strings.HasPrefix(itemType, lexicon.KindElement+"(") || + strings.HasPrefix(itemType, lexicon.KindAttribute+"(") || + strings.HasPrefix(itemType, lexicon.KindDocumentNode+"(") || + strings.HasPrefix(itemType, lexicon.KindText+"(") || + strings.HasPrefix(itemType, lexicon.KindComment+"(") || + strings.HasPrefix(itemType, lexicon.KindPI+"(") || + strings.HasPrefix(itemType, lexicon.KindNamespaceNode+"(") || + strings.HasPrefix(itemType, lexicon.KindSchemaElement+"(") || + strings.HasPrefix(itemType, lexicon.KindSchemaAttribute+"(") || + strings.HasPrefix(itemType, sequenceTypeFunctionStart) || + strings.HasPrefix(itemType, sequenceTypeMapStart) || + strings.HasPrefix(itemType, sequenceTypeArrayStart) { return false } @@ -201,43 +215,70 @@ func coerceItemWithContext(ctx context.Context, item xdm.Item, itemType string, } switch { - case strings.HasPrefix(itemType, "document-node("): + case strings.HasPrefix(itemType, lexicon.KindDocumentNode+"("): return coerceDocumentNodeTest(ctx, item, itemType, ec) - case strings.HasPrefix(itemType, "attribute("): - return coerceNodeKind(item, itemType, xdm.AttributeNode) - case strings.HasPrefix(itemType, "map("): + case strings.HasPrefix(itemType, sequenceTypeMapStart): return checkMapItemType(item, itemType) - case strings.HasPrefix(itemType, "array("): - // Retain the existing permissive array test. - return item, nil - case strings.HasPrefix(itemType, "function("): + case strings.HasPrefix(itemType, sequenceTypeArrayStart), strings.HasPrefix(itemType, lexicon.KindAttribute+"("): + return coerceParsedItemType(ctx, item, itemType, ec) + case strings.HasPrefix(itemType, sequenceTypeFunctionStart): return coerceFunctionItemType(ctx, item, itemType, ec) - case strings.HasPrefix(itemType, "element("): + case strings.HasPrefix(itemType, lexicon.KindElement+"("): return coerceElementNodeTest(item, itemType, ec) - case strings.HasPrefix(itemType, "schema-element("): + case strings.HasPrefix(itemType, lexicon.KindSchemaElement+"("): return coerceSchemaElementTest(item, itemType, ec) - case strings.HasPrefix(itemType, "schema-attribute("): + case strings.HasPrefix(itemType, lexicon.KindSchemaAttribute+"("): return coerceSchemaAttributeTest(item, itemType, ec) default: return coerceToAtomicType(ctx, item, itemType, ec) } } +func coerceParsedItemType(ctx context.Context, item xdm.Item, itemType string, ec *execContext) (xdm.Item, error) { + st, err := xpath.ParseSequenceType(itemType) + if err != nil { + return nil, err + } + + namespaces, defaultNS := xpath.PredeclaredNamespaces(), "" + if ec != nil { + namespaces, defaultNS = ec.effectiveXPathNamespaces(), ec.xpathDefaultNS + } + + st, err = xpath.BindSequenceType(ctx, st, namespaces, defaultNS) + if err != nil { + return nil, err + } + + var checked xdm.Sequence + if ec != nil { + checked, err = ec.convertSequenceType(ctx, xdm.ItemSlice{item}, st) + } else { + checked, err = xpath.ConvertToSequenceType(ctx, xdm.ItemSlice{item}, st) + } + + if err != nil { + return nil, err + } + + return checked.Get(0), nil +} + func simpleNodeTestKind(itemType string) (xdm.ElementType, bool) { switch itemType { - case "element()": + case lexicon.NodeTestElement: return xdm.ElementNode, true - case "attribute()": + case lexicon.NodeTestAttribute: return xdm.AttributeNode, true - case "text()": + case lexicon.NodeTestText: return xdm.TextNode, true - case "comment()": + case lexicon.NodeTestComment: return xdm.CommentNode, true case lexicon.NodeTestPI: return xdm.ProcessingInstructionNode, true - case "namespace-node()": + case lexicon.KindNamespaceNode + "()": return xdm.NamespaceNode, true - case "document-node()": + case lexicon.NodeTestDocumentNode: return xdm.DocumentNode, true default: return 0, false @@ -261,7 +302,7 @@ func coerceDocumentNodeTest(ctx context.Context, item xdm.Item, itemType string, return nil, fmt.Errorf("expected %s, got %s", itemType, describeItem(item)) } - inner := strings.TrimSpace(itemType[len("document-node(") : len(itemType)-1]) + inner := strings.TrimSpace(itemType[len(lexicon.KindDocumentNode+"(") : len(itemType)-1]) if inner == "" { return item, nil } @@ -284,7 +325,7 @@ func coerceDocumentNodeTest(ctx context.Context, item xdm.Item, itemType string, } func coerceFunctionItemType(ctx context.Context, item xdm.Item, itemType string, ec *execContext) (xdm.Item, error) { - if itemType == "function(*)" { + if itemType == sequenceTypeFunctionStart+"*)" { switch item.(type) { case xdm.FunctionItem, xdm.MapItem, *xdm.ArrayItem: return item, nil @@ -324,7 +365,7 @@ func coerceElementNodeTest(item xdm.Item, itemType string, ec *execContext) (xdm return nil, fmt.Errorf("expected %s, got %s", itemType, describeItem(item)) } - inner := strings.TrimSpace(itemType[len("element(") : len(itemType)-1]) + inner := strings.TrimSpace(itemType[len(lexicon.KindElement+"(") : len(itemType)-1]) if inner == "" || inner == "*" { return item, nil } @@ -356,7 +397,7 @@ func coerceSchemaElementTest(item xdm.Item, itemType string, ec *execContext) (x return nil, fmt.Errorf("expected %s, got %s", itemType, describeItem(item)) } - inner := strings.TrimSpace(itemType[len("schema-element(") : len(itemType)-1]) + inner := strings.TrimSpace(itemType[len(lexicon.KindSchemaElement+"(") : len(itemType)-1]) local, ns := resolveSchemaQName(inner, qnameElementName, ec) elem, ok := ni.Node.(*xdm.Element) @@ -377,7 +418,7 @@ func coerceSchemaAttributeTest(item xdm.Item, itemType string, ec *execContext) return nil, fmt.Errorf("expected %s, got %s", itemType, describeItem(item)) } - inner := strings.TrimSpace(itemType[len("schema-attribute(") : len(itemType)-1]) + inner := strings.TrimSpace(itemType[len(lexicon.KindSchemaAttribute+"(") : len(itemType)-1]) local, ns := resolveSchemaQName(inner, qnameAttributeName, ec) attr, ok := ni.Node.(*xdm.Attribute) @@ -429,7 +470,7 @@ func castAtomicToType(ctx context.Context, av xdm.AtomicValue, targetType string } // xs:anyAtomicType matches any atomic value - if target == "xs:anyAtomicType" { + if target == xdm.TypeAnyAtomicType { return av, nil } @@ -508,7 +549,7 @@ func trySchemaCast(ctx context.Context, s, target string, ec ...*execContext) (x // canonicalized to xs:. Other prefixed names are resolved to Q{ns}local // format using the exec context's namespace bindings (if available). func normalizeTypeName(name string, ec ...*execContext) string { - if strings.HasPrefix(name, "xs:") { + if strings.HasPrefix(name, lexicon.PrefixXS+":") { return name } @@ -516,8 +557,8 @@ func normalizeTypeName(name string, ec ...*execContext) string { return name } // Normalize xsd: prefix to xs: (both map to XML Schema namespace) - if strings.HasPrefix(name, "xsd:") { - return "xs:" + name[4:] + if strings.HasPrefix(name, lexicon.PrefixXSD+":") { + return lexicon.PrefixXS + ":" + name[len(lexicon.PrefixXSD+":"):] } // Resolve other prefix:local names to Q{ns}local using namespace bindings. if idx := strings.IndexByte(name, ':'); idx > 0 { @@ -527,7 +568,7 @@ func normalizeTypeName(name string, ec ...*execContext) string { if len(ec) > 0 && ec[0] != nil { if ns, ok := ec[0].lookupTypeNamespaceOK(prefix); ok { if ns == lexicon.NamespaceXSD { - return "xs:" + local + return lexicon.PrefixXS + ":" + local } return xdm.QAnnotation(ns, local) @@ -548,7 +589,7 @@ func normalizeTypeName(name string, ec ...*execContext) string { return xdm.TypeFloat case lexicon.TypeBoolean: return xdm.TypeBoolean - case "date": + case lexicon.TypeDate: return xdm.TypeDate case lexicon.TypeDateTime: return xdm.TypeDateTime @@ -560,7 +601,7 @@ func normalizeTypeName(name string, ec ...*execContext) string { return xdm.TypeDayTimeDuration case lexicon.TypeYearMonthDuration: return xdm.TypeYearMonthDuration - case "anyURI": + case lexicon.TypeAnyURI: return xdm.TypeAnyURI case lexicon.TypeUntypedAtomic: return xdm.TypeUntypedAtomic @@ -584,26 +625,34 @@ func normalizeTypeName(name string, ec ...*execContext) string { // Higher rank = wider type. Promotion is only valid from lower to higher rank. // Derived integer types (xs:int, xs:long, etc.) have the same rank as xs:integer. func numericRank(t string) int { + const ( + numericPromotionDecimal = 2 + numericPromotionDouble = 4 + numericPromotionFloat = 3 + numericPromotionInteger = 1 + numericPromotionNone = 0 + ) + switch t { case xdm.TypeInteger: - return 1 + return numericPromotionInteger case xdm.TypeDecimal: - return 2 + return numericPromotionDecimal case xdm.TypeFloat: - return 3 + return numericPromotionFloat case xdm.TypeDouble: - return 4 + return numericPromotionDouble } // Derived integer types all promote at the same rank as xs:integer. if xdm.BuiltinIsSubtypeOf(t, xdm.TypeInteger) { - return 1 + return numericPromotionInteger } // Derived decimal types (not integer) promote at decimal rank. if xdm.BuiltinIsSubtypeOf(t, xdm.TypeDecimal) { - return 2 + return numericPromotionDecimal } - return 0 + return numericPromotionNone } // isNumericType returns true for xs:integer, xs:decimal, xs:float, xs:double @@ -718,7 +767,7 @@ func checkMapItemType(item xdm.Item, itemType string) (xdm.Item, error) { } func parseMapItemType(itemType string) (string, sequenceType, bool, error) { - inner := strings.TrimSpace(itemType[len("map(") : len(itemType)-1]) + inner := strings.TrimSpace(itemType[len(sequenceTypeMapStart) : len(itemType)-1]) if inner == "*" { return "", sequenceType{}, false, nil } @@ -763,7 +812,7 @@ func sequenceMatchesTypeStrict(seq xdm.Sequence, st sequenceType) bool { } switch st.Occurrence { - case 0: + case occurrenceExactlyOne: if count != 1 { return false } @@ -793,25 +842,25 @@ func itemMatchesTypeStrict(item xdm.Item, itemType string) bool { case lexicon.NodeTestNode: _, ok := item.(xdm.NodeItem) return ok - case "element()": + case lexicon.NodeTestElement: if ni, ok := item.(xdm.NodeItem); ok { return ni.Node.Type() == xdm.ElementNode } return false - case "attribute()": + case lexicon.NodeTestAttribute: if ni, ok := item.(xdm.NodeItem); ok { return ni.Node.Type() == xdm.AttributeNode } return false - case "text()": + case lexicon.NodeTestText: if ni, ok := item.(xdm.NodeItem); ok { return ni.Node.Type() == xdm.TextNode || ni.Node.Type() == xdm.CDATASectionNode } return false - case "comment()": + case lexicon.NodeTestComment: if ni, ok := item.(xdm.NodeItem); ok { return ni.Node.Type() == xdm.CommentNode } @@ -823,7 +872,7 @@ func itemMatchesTypeStrict(item xdm.Item, itemType string) bool { } return false - case "document-node()": + case lexicon.NodeTestDocumentNode: if ni, ok := item.(xdm.NodeItem); ok { return ni.Node.Type() == xdm.DocumentNode } @@ -831,12 +880,12 @@ func itemMatchesTypeStrict(item xdm.Item, itemType string) bool { return false } - if strings.HasPrefix(itemType, "map(") { + if strings.HasPrefix(itemType, sequenceTypeMapStart) { _, err := checkMapItemType(item, itemType) return err == nil } - if strings.HasPrefix(itemType, "array(") || strings.HasPrefix(itemType, "function(") { + if strings.HasPrefix(itemType, sequenceTypeArrayStart) || strings.HasPrefix(itemType, sequenceTypeFunctionStart) { return true } @@ -851,9 +900,9 @@ func itemMatchesTypeStrict(item xdm.Item, itemType string) bool { func atomicMatchesType(av xdm.AtomicValue, targetType string) bool { target := normalizeTypeName(strings.TrimSpace(targetType)) switch target { - case "xs:anyAtomicType": + case xdm.TypeAnyAtomicType: return true - case "xs:numeric": + case xdm.TypeNumeric: return isNumericType(av.TypeName) } diff --git a/transform/internal/xslt/types_test.go b/transform/internal/xslt/types_test.go index 33eb7f6..f6cd785 100644 --- a/transform/internal/xslt/types_test.go +++ b/transform/internal/xslt/types_test.go @@ -2,6 +2,7 @@ package xslt //nolint:testpackage // Tests cardinality after atomization and XSL import ( "errors" + "strings" "testing" "github.com/skarm/xmlsec/transform/internal/xdm" @@ -133,3 +134,32 @@ func TestSequenceTypeAtomicConversions(t *testing.T) { t.Fatalf("partial type failure = %v, %v", got, err) } } + +func TestCanonicalNameTestExplicitNamespace(t *testing.T) { + for _, tc := range []struct{ source, want string }{{"Q{}n", "Q{}n"}, {"Q{urn:other}n", "Q{urn:other}n"}, {"n", "Q{urn:default}n"}} { + t.Run(tc.source, func(t *testing.T) { + ast, err := xpath.Parse(tc.source) + if err != nil { + t.Fatal(err) + } + var path xpath.LocationPath + switch v := ast.(type) { + case xpath.LocationPath: + path = v + case *xpath.LocationPath: + path = *v + default: + t.Fatalf("path=%#v", ast) + } + if len(path.Steps) != 1 { + t.Fatalf("steps=%#v", path) + } + var b strings.Builder + w := canonicalTypeWriter{b: &b, defaultElemNS: "urn:default", hasDefaultElemNS: true} + w.nodeTest(path.Steps[0].NodeTest) + if b.String() != tc.want { + t.Errorf("actual %q; expected %q", b.String(), tc.want) + } + }) + } +} diff --git a/transform/normalization_benchmark_test.go b/transform/normalization_benchmark_test.go new file mode 100644 index 0000000..acf123e --- /dev/null +++ b/transform/normalization_benchmark_test.go @@ -0,0 +1,55 @@ +package transform_test + +import ( + "strconv" + "strings" + "testing" + + "github.com/skarm/xmlsec/transform" +) + +// Explicit JSON normalization must also handle the common case where the input +// already has the requested form, including large ASCII and composed Unicode. +func BenchmarkJSONNormalization(b *testing.B) { + for name, unit := range map[string]string{"ASCII": "x", "Composed": "é", "Decomposed": "e\u0301"} { + b.Run(name, func(b *testing.B) { + for _, size := range []int{16, 65536} { + b.Run(strconv.Itoa(size), func(b *testing.B) { + e := engine(b, transform.Config{}) + q := query(b, e, `serialize($source, map{'method':'json', 'normalization-form':'NFC'})`, transform.XPath31) + input := strings.Repeat(unit, size) + options := transform.XPathContext{Variables: transform.Bindings{{Local: "source"}: transform.Values(transform.String(input))}} + b.ReportAllocs() + b.SetBytes(int64(len(input))) + for b.Loop() { + if _, err := q.Evaluate(b.Context(), nil, options); err != nil { + b.Fatal(err) + } + } + }) + } + }) + } +} + +func BenchmarkJSONNormalizationParallel(b *testing.B) { + e := engine(b, transform.Config{}) + q := query(b, e, `serialize($source, map{'method':'json', 'normalization-form':'NFC', 'use-character-maps':map{'|':'mapped'}})`, transform.XPath31) + inputs := make([]transform.XPathContext, 0, 3) + for _, source := range []string{strings.Repeat("x", 4096), strings.Repeat("é e\u0301 | ", 512), "a" + strings.Repeat("\u0301", 128)} { + inputs = append(inputs, transform.XPathContext{Variables: transform.Bindings{{Local: "source"}: transform.Values(transform.String(source))}}) + } + b.ReportAllocs() + b.SetParallelism(3) + b.ResetTimer() + b.RunParallel(func(pb *testing.PB) { + i := 0 + for pb.Next() { + if _, err := q.Evaluate(b.Context(), nil, inputs[i%len(inputs)]); err != nil { + b.Error(err) + return + } + i++ + } + }) +} diff --git a/transform/records.go b/transform/records.go index 6758579..bdc4b06 100644 --- a/transform/records.go +++ b/transform/records.go @@ -47,7 +47,7 @@ func (s *Stylesheet) WriteRecords(ctx context.Context, dst io.Writer, src io.Rea ctx, cancel, err := s.engine.invocation(ctx) if err != nil { - return 0, err + return 0, wrap("stream XSLT", err) } defer cancel() @@ -112,6 +112,13 @@ func (s *Stylesheet) recordLimits(limits document.Limits) document.Limits { limits.MaxBytes = min(limits.MaxBytes, s.engine.limits.MaxInputBytes) limits.MaxNodes = min(limits.MaxNodes, s.engine.limits.MaxNodes) limits.MaxDepth = min(limits.MaxDepth, s.engine.limits.MaxDepth) + limits.MaxAttributesPerElement = min(limits.MaxAttributesPerElement, s.engine.limits.MaxAttributesPerElement) + // Resolve the document default before intersecting the effective namespace limits. + if limits.MaxInScopeNamespaces == 0 { + limits.MaxInScopeNamespaces = document.DefaultLimits().MaxInScopeNamespaces + } + + limits.MaxInScopeNamespaces = min(limits.MaxInScopeNamespaces, s.engine.limits.MaxInScopeNamespaces) return limits } diff --git a/transform/resource.go b/transform/resource.go index b59562b..b88c2aa 100644 --- a/transform/resource.go +++ b/transform/resource.go @@ -14,6 +14,11 @@ import ( "github.com/skarm/xmlsec/transform/internal/xpath" ) +// Only consecutive zero-byte/no-error reads count as stalled; progress resets the counter before the normal close/error path reports io.ErrNoProgress. +const ( + maximumStalledResourceReads = 100 +) + type boundedBuffer struct { bytes.Buffer ctx context.Context @@ -86,7 +91,7 @@ func (e *Engine) openResource(ctx context.Context, limit int, open func() (io.Re r, err := open() if err != nil { - if !nilcheck.IsNil(r) { + if r != nil && !nilcheck.IsNil(r) { err = errors.Join(err, r.Close()) } @@ -179,7 +184,7 @@ func (r *resourceStream) Read(p []byte) (int, error) { r.stalled = 0 } else if err == nil { r.stalled++ - if r.stalled >= 100 { + if r.stalled >= maximumStalledResourceReads { err = io.ErrNoProgress } } diff --git a/transform/resource_test.go b/transform/resource_test.go index 327eb86..f4bf8ec 100644 --- a/transform/resource_test.go +++ b/transform/resource_test.go @@ -8,12 +8,16 @@ import ( "errors" "io" "io/fs" + "math/big" "strconv" "strings" + "sync" + "sync/atomic" "testing" "testing/fstest" "unicode/utf16" + "github.com/skarm/xmlsec/reference" "github.com/skarm/xmlsec/resource" "github.com/skarm/xmlsec/transform" ) @@ -638,12 +642,12 @@ func TestApplicationFunctionTypes(t *testing.T) { calls++ return args[0], nil }}}}) - for _, reference := range []string{`Q{urn:callback}echo#1`, `Q{urn:callback}echo(?)`, `function-lookup(QName('urn:callback','echo'),1)`} { + for _, ref := range []string{`Q{urn:callback}echo#1`, `Q{urn:callback}echo(?)`, `function-lookup(QName('urn:callback','echo'),1)`} { for _, test := range []struct { result string want string }{{`item()*`, nameTrue}, {`xs:string`, nameFalse}} { - expression := reference + ` instance of function(xs:string) as ` + test.result + expression := ref + ` instance of function(xs:string) as ` + test.result if got := text(t, evaluate(t, e, expression, transform.XPath31)); got != test.want { t.Fatal(expression, got, test.want) } @@ -1200,38 +1204,6 @@ func TestCollectionStabilityAcrossXSLT(t *testing.T) { } } -func resourceStylesheetResult(t *testing.T, e *transform.Engine, source string, nested bool, assertions ...bool) (string, error) { - t.Helper() - - if nested { - option := "" - if len(assertions) > 0 && assertions[0] { - option = ",'enable-assertions':true()" - } - - q := query(t, e, `transform(map{'stylesheet-text':$stylesheet,'initial-template':QName('', 'start'),'delivery-format':'serialized'`+option+`})?output`, transform.XPath31) - - result, err := q.Evaluate(t.Context(), nil, transform.XPathContext{Variables: transform.Bindings{{Local: variableStylesheet}: transform.Values(transform.String(source))}}) - if err != nil { - return "", err - } - - return text(t, result), nil - } - - compiled, err := e.CompileXSLT(t.Context(), []byte(source), transform.XSLTOptions{}) - if err != nil { - return "", err - } - - result, err := compiled.Transform(t.Context(), nil, transform.XSLTContext{Template: transform.Name{Local: nameStart}}) - if err != nil { - return "", err - } - - return string(result.Primary), nil -} - func TestMergeSourceContracts(t *testing.T) { for _, test := range []struct { name string @@ -1319,7 +1291,7 @@ func TestXPathTransformResources(t *testing.T) { result, err := program.Evaluate(t.Context(), nil, transform.XPathContext{}) if maxResources == 1 { - if !errors.Is(err, transform.ErrLimit) || result.Len() != 0 { + if !errors.Is(err, transform.ErrLimit) || result != (transform.Sequence{}) { t.Fatal("nested resource budget", result, err) } } else if err != nil || text(t, result) != "payload" { @@ -1543,41 +1515,6 @@ func TestEscapedFunctionResourceScopes(t *testing.T) { } } -func documentFunctionContractCases() []struct { - expression string - want string -} { - tests := make([]struct { - expression string - want string - }, 0, 24) - for _, call := range []string{`doc`, `(doc#1)`, `function-lookup(QName('http://www.w3.org/2005/xpath-functions','doc'),1)`, `doc(?)`} { - for _, test := range []struct { - argument string - want string - }{ - {`42`, errCodeXPTY0004}, - {`('urn:shared','urn:other')`, errCodeXPTY0004}, - {`'urn:shared#id'`, "FODC0005"}, - {`':/'`, "FODC0005"}, - {`[]`, ""}, - {`xs:untypedAtomic('urn:shared')`, "1"}, - } { - tests = append(tests, struct { - expression string - want string - }{call + `(` + test.argument + `)/r`, test.want}) - } - } - return tests -} - -func valueOfExpression(expression string) string { - var escaped strings.Builder - _ = xml.EscapeText(&escaped, []byte(expression)) - return `` -} - func TestXSLTDocumentFunctionContracts(t *testing.T) { e := engine(t, transform.Config{Resolver: transform.ResolverFunc(func(ctx context.Context, _ string) (io.ReadCloser, error) { return io.NopCloser(strings.NewReader(`1`)), ctx.Err() @@ -2124,13 +2061,6 @@ func TestXSLTTextResourceMediaType(t *testing.T) { } } -type typedTextReader struct { - lifecycleReader - mediaType string -} - -func (r *typedTextReader) MediaType() string { return r.mediaType } - func TestXPathTransformProgramBudget(t *testing.T) { e := engine(t, transform.Config{Limits: transform.Limits{MaxProgramBytes: 512}}) @@ -2142,7 +2072,7 @@ func TestXPathTransformProgramBudget(t *testing.T) { result, err := query(t, e, expression, transform.XPath31).Evaluate(t.Context(), nil, transform.XPathContext{Variables: transform.Bindings{ {Local: variableStylesheet}: transform.Values(transform.String(source)), }}) - if !errors.Is(err, transform.ErrLimit) || result.Len() != 0 { + if !errors.Is(err, transform.ErrLimit) || result != (transform.Sequence{}) { t.Fatal("dynamic stylesheet program budget", result, err) } } @@ -2185,7 +2115,7 @@ func TestJSONDocResourceErrorCodes(t *testing.T) { })}) result, err := query(t, e, `json-doc('urn:json')`, transform.XPath31).Evaluate(t.Context(), nil, transform.XPathContext{}) - if !errors.Is(err, transform.ErrLimit) || result.Len() != 0 { + if !errors.Is(err, transform.ErrLimit) || result != (transform.Sequence{}) { t.Fatal("resource budget lost", result, err) } } @@ -2540,37 +2470,6 @@ func TestXSLTProcessorVersionModules(t *testing.T) { } } -type lifecycleReader struct { - io.Reader - closed bool -} - -func (r *lifecycleReader) Close() error { - r.closed = true - return nil -} - -type cancelReadResource struct { - lifecycleReader - cancel context.CancelFunc - reads int -} - -func (r *cancelReadResource) Read(p []byte) (int, error) { - r.reads++ - n, err := r.Reader.Read(p) - r.cancel() - - return n, err -} - -type trackedReader struct { - io.Reader - closed *bool -} - -func (r trackedReader) Close() error { *r.closed = true; return nil } - func TestXPathFunctionConversionBudgets(t *testing.T) { e := engine(t, transform.Config{Limits: transform.Limits{MaxOperations: 1000}}) for _, expression := range []string{`function-arity(concat#1000000000)`, `function-arity(function-lookup(xs:QName('fn:concat'),1000000000))`} { @@ -2587,7 +2486,7 @@ func TestXPathFunctionConversionBudgets(t *testing.T) { `function-lookup(xs:QName('fn:codepoints-to-string'),1)(33 to 10033)`, } { value, err := query(t, e, expression, transform.XPath31).Evaluate(t.Context(), nil, transform.XPathContext{}) - if !errors.Is(err, transform.ErrLimit) || value.Len() != 0 { + if !errors.Is(err, transform.ErrLimit) || value != (transform.Sequence{}) { t.Fatal("function conversion ignored the operation budget", expression, value, err) } } @@ -3770,7 +3669,9 @@ func TestXSLTPackageCollectionViews(t *testing.T) { } for range 2 { result, err := compiled.Transform(t.Context(), nil, transform.XSLTContext{Template: transform.Name{Local: nameStart}}) - if err != nil || string(result.Primary) != test.want { + if err != nil { + t.Fatal(err) + } else if string(result.Primary) != test.want { t.Fatal(string(result.Primary), err, test.want) } } @@ -3826,9 +3727,9 @@ func rootXMLBaseCases() []transformExpressionCase { } program := `` cases = append(cases, transformExpressionCase{"empty with fragment", `transform(map{'stylesheet-text':'` + program + `','stylesheet-base-uri':'` + base + `#fragment','initial-template':QName('', 'start'),'delivery-format':'serialized'})?output`, base}) - for _, reference := range []string{"?query", "#fragment", ""} { - program := `` - cases = append(cases, transformExpressionCase{"file " + reference, `transform(map{'stylesheet-text':'` + program + `','stylesheet-base-uri':'file:///modules/main.xsl','initial-template':QName('', 'start'),'delivery-format':'serialized'})?output`, "file:///modules/main.xsl" + reference}) + for _, ref := range []string{"?query", "#fragment", ""} { + program := `` + cases = append(cases, transformExpressionCase{"file " + ref, `transform(map{'stylesheet-text':'` + program + `','stylesheet-base-uri':'file:///modules/main.xsl','initial-template':QName('', 'start'),'delivery-format':'serialized'})?output`, "file:///modules/main.xsl" + ref}) } return cases } @@ -3953,8 +3854,8 @@ func TestLanguageDTDStylesheets(t *testing.T) { } func TestLanguageDTDUnparsedEntities(t *testing.T) { - const xml = `]>` - selectValue := `parse-xml('` + strings.ReplaceAll(xml, "'", "''") + `')` + const x = `]>` + selectValue := `parse-xml('` + strings.ReplaceAll(x, "'", "''") + `')` body := `` e := engine(t, transform.Config{}) compiled, err := e.CompileXSLT(t.Context(), []byte(sheet(version30, body)), transform.XSLTOptions{BaseURI: "https://example.test/style.xsl"}) @@ -3967,23 +3868,6 @@ func TestLanguageDTDUnparsedEntities(t *testing.T) { } } -type dtdFailureReader struct { - io.Reader - readError error - closeError error - closed bool -} - -func (r *dtdFailureReader) Read(p []byte) (int, error) { - n, err := r.Reader.Read(p) - if n > 0 && r.readError != nil { - return n, r.readError - } - return n, err -} - -func (r *dtdFailureReader) Close() error { r.closed = true; return r.closeError } - func TestLanguageDTDFailures(t *testing.T) { const expression = `parse-xml(']>&e;')` failure := errors.New("entity stream failure") @@ -4003,7 +3887,7 @@ func TestLanguageDTDFailures(t *testing.T) { return reader, nil })}) value, err := query(t, e, expression, transform.XPath31).Evaluate(t.Context(), nil, transform.XPathContext{}) - if !errors.Is(err, failure) || !reader.closed || value.Len() != 0 { + if !errors.Is(err, failure) || !reader.closed || value != (transform.Sequence{}) { t.Fatal(stage, value, err, reader.closed) } }) @@ -4013,7 +3897,7 @@ func TestLanguageDTDFailures(t *testing.T) { reader := &cancelReadResource{Reader: strings.NewReader(``), cancel: cancel} e := engine(t, transform.Config{Resolver: transform.ResolverFunc(func(context.Context, string) (io.ReadCloser, error) { return reader, nil })}) value, err := query(t, e, expression, transform.XPath31).Evaluate(ctx, nil, transform.XPathContext{}) - if !errors.Is(err, context.Canceled) || !reader.closed || value.Len() != 0 { + if !errors.Is(err, context.Canceled) || !reader.closed || value != (transform.Sequence{}) { t.Fatal("entity cancellation", value, err, reader.closed) } @@ -4032,7 +3916,7 @@ func TestLanguageDTDFailures(t *testing.T) { } { e = engine(t, transform.Config{Limits: transform.Limits{MaxInputBytes: 256, MaxDepth: 4}}) value, err = query(t, e, `parse-xml($source)`, transform.XPath31).Evaluate(t.Context(), nil, transform.XPathContext{Variables: transform.Bindings{{Local: sourceName}: transform.Values(transform.String(source))}}) - if !errors.Is(err, transform.ErrLimit) || value.Len() != 0 { + if !errors.Is(err, transform.ErrLimit) || value != (transform.Sequence{}) { t.Fatal("entity expansion budget", value, err) } } @@ -4118,7 +4002,7 @@ func TestSerializationOutputBudgets(t *testing.T) { t.Run(method, func(t *testing.T) { expression := `serialize($input,map{'method':'` + method + `','use-character-maps':map{'a':$replacement}})` got, err := query(t, e, expression, transform.XPath31).Evaluate(t.Context(), nil, transform.XPathContext{Variables: variables}) - if !errors.Is(err, transform.ErrLimit) || got.Len() != 0 { + if !errors.Is(err, transform.ErrLimit) || got != (transform.Sequence{}) { t.Fatal(got, err) } }) @@ -4528,7 +4412,7 @@ func TestXSLTGroupingAndCopyBudgets(t *testing.T) { body string limits transform.Limits }{ - {"numeric comparisons", ``, transform.Limits{MaxOperations: 2000}}, + {"numeric comparisons", ``, transform.Limits{MaxOperations: 2000}}, {"group memberships", ``, transform.Limits{MaxNodes: 200}}, {"copied document trees", ``, transform.Limits{MaxNodes: 200}}, } { @@ -4677,7 +4561,7 @@ func TestDocumentAvailabilityControls(t *testing.T) { if errors.Is(cause, transform.ErrLimit) && version != transform.XPath20 { wantCode = "XPDY0130" } - if !errors.Is(err, cause) || result.Len() != 0 || !errors.As(err, &detail) || detail.Code != wantCode { + if !errors.Is(err, cause) || result != (transform.Sequence{}) || !errors.As(err, &detail) || detail.Code != wantCode { t.Fatal(result, err, cause, wantCode) } }) @@ -4692,7 +4576,7 @@ func TestDocumentAvailabilityControls(t *testing.T) { if version != transform.XPath20 { wantCode = "XPDY0130" } - if !errors.Is(err, transform.ErrLimit) || result.Len() != 0 || !errors.As(err, &detail) || detail.Code != wantCode { + if !errors.Is(err, transform.ErrLimit) || result != (transform.Sequence{}) || !errors.As(err, &detail) || detail.Code != wantCode { t.Fatal(result, err, wantCode) } }) @@ -4772,89 +4656,6 @@ func TestDocumentSnapshotLiveReadPolicy(t *testing.T) { } } -type parameterDocumentHostCase struct { - name string - parameters string - attributes string - declarations string - body string - want string -} - -func parameterDocumentHostCases() []parameterDocumentHostCase { - const xmlOutput = `method="xml" omit-xml-declaration="yes"` - const root = `
` - return []parameterDocumentHostCase{ - {"XHTML method retained", ``, `method="text"`, "", ``, ``}, - {"URI escaping false overrides true", ``, `escape-uri-attributes="yes"`, "", `x`, `x`}, - {"absent URI escaping inherits true", ``, `escape-uri-attributes="yes"`, "", `x`, `x`}, - {"normalization none overrides NFC", ``, `normalization-form="NFC"`, "", "e\u0301", "e\u0301"}, - {"fully normalized stays byte host supported", ``, "", "", "e\u0301", "é"}, - {"fully normalized leading combining", ``, "", "", ``, "SERE0012"}, - {"extension method rejected by host choice", ``, `omit-xml-declaration="yes"`, "", `x`, errCodeSEPM0016}, - {"method beats direct attribute", ``, xmlOutput, "", `x`, "x"}, - {"false beats true", ``, xmlOutput + ` indent="yes"`, "", root, root}, - {"BOM survives byte host", ``, `byte-order-mark="no"`, "", `x`, "\ufeffx"}, - {"empty separator beats attribute", ``, `item-separator="|"`, "", ``, "12"}, - {"separator beats absent", ``, `item-separator="#absent"`, "", ``, "1-2"}, - {"empty doctype removes inherited", ``, xmlOutput + ` doctype-system="old.dtd"`, "", ``, ``}, - {"QName list union", ``, xmlOutput + ` cdata-section-elements="b"`, "", `x<yx<y`, ``}, - {"parameter namespace default", ``, xmlOutput, "", `x<y`, `x<y`}, - {"QName parameter binding", ``, xmlOutput, "", `x<yx<y`, `x<y`}, - {"QName list absent preserves local", ``, xmlOutput + ` cdata-section-elements="b"`, "", `x<yx<y`, `x<y`}, - {"foreign character map ignored", ``, "", "", `x`, "x"}, - {"QName unknown prefix", ``, xmlOutput, "", root, errCodeSEPM0017}, - {"character map namespace and precedence", ``, `method="text" use-character-maps="m"`, ``, `x`, "P"}, - {"empty character map clears direct", ``, `method="text" use-character-maps="m"`, ``, `x`, "x"}, - {"invalid boolean", ``, `method="text"`, "", `x`, errCodeSEPM0017}, - {"duplicate scalar", ``, "", "", `x`, errCodeSEPM0019}, - {"duplicate character", ``, "", "", `x`, errCodeSEPM0018}, - {"unknown attribute", ``, "", "", `x`, errCodeSEPM0017}, - {"inherited output method", ``, `omit-xml-declaration="yes"`, ``, `x`, "x"}, - {"default host XML declaration", ``, "", "", ``, ``}, - } -} - -func parameterDocumentHostSheet(route, href string, test parameterDocumentHostCase) string { - output := `` - body := test.body - if route != "output" { - output = `` - if test.name == "default host XML declaration" { - output = `` - } - attribute := href - if route == "dynamic" { - attribute = `{$parameterURI}` - } - resultURI := "" - if route == "secondary" || route == "dynamic" { - resultURI = ` href="urn:result"` - } - body = `` + body + `` - } - return sheetStart + version30 + `" exclude-result-prefixes="xs">` + test.declarations + output + `` + body + `` -} - -func evaluateParameterDocumentHost(t *testing.T, route, source, parameters string) (string, error) { - t.Helper() - e := engine(t, transform.Config{Resolver: transform.ResolverFunc(func(ctx context.Context, _ string) (io.ReadCloser, error) { - return io.NopCloser(strings.NewReader(parameters)), ctx.Err() - })}) - compiled, err := e.CompileXSLT(t.Context(), []byte(source), transform.XSLTOptions{ProcessorVersion: transform.XSLT30}) - if err != nil { - return "", err - } - result, err := compiled.Transform(t.Context(), nil, transform.XSLTContext{Template: transform.Name{Local: nameStart}}) - if err != nil { - return "", err - } - if route == "secondary" || route == "dynamic" { - return string(result.Secondary["urn:result"]), nil - } - return string(result.Primary), nil -} - func TestXSLTParameterDocumentContracts(t *testing.T) { for _, test := range parameterDocumentHostCases() { for _, route := range []string{"output", "primary", "secondary", "dynamic"} { @@ -5054,3 +4855,873 @@ func TestXSLTImplicitTextEncodingErrors(t *testing.T) { } } } + +func resourceStylesheetResult(t *testing.T, e *transform.Engine, source string, nested bool, assertions ...bool) (string, error) { + t.Helper() + + if nested { + option := "" + if len(assertions) > 0 && assertions[0] { + option = ",'enable-assertions':true()" + } + + q := query(t, e, `transform(map{'stylesheet-text':$stylesheet,'initial-template':QName('', 'start'),'delivery-format':'serialized'`+option+`})?output`, transform.XPath31) + + result, err := q.Evaluate(t.Context(), nil, transform.XPathContext{Variables: transform.Bindings{{Local: variableStylesheet}: transform.Values(transform.String(source))}}) + if err != nil { + return "", err + } + + return text(t, result), nil + } + + compiled, err := e.CompileXSLT(t.Context(), []byte(source), transform.XSLTOptions{}) + if err != nil { + return "", err + } + + result, err := compiled.Transform(t.Context(), nil, transform.XSLTContext{Template: transform.Name{Local: nameStart}}) + if err != nil { + return "", err + } + + return string(result.Primary), nil +} + +func documentFunctionContractCases() []struct { + expression string + want string +} { + tests := make([]struct { + expression string + want string + }, 0, 24) + for _, call := range []string{`doc`, `(doc#1)`, `function-lookup(QName('http://www.w3.org/2005/xpath-functions','doc'),1)`, `doc(?)`} { + for _, test := range []struct { + argument string + want string + }{ + {`42`, errCodeXPTY0004}, + {`('urn:shared','urn:other')`, errCodeXPTY0004}, + {`'urn:shared#id'`, "FODC0005"}, + {`':/'`, "FODC0005"}, + {`[]`, ""}, + {`xs:untypedAtomic('urn:shared')`, "1"}, + } { + tests = append(tests, struct { + expression string + want string + }{call + `(` + test.argument + `)/r`, test.want}) + } + } + return tests +} + +func valueOfExpression(expression string) string { + var escaped strings.Builder + _ = xml.EscapeText(&escaped, []byte(expression)) + return `` +} + +type typedTextReader struct { + lifecycleReader + mediaType string +} + +func (r *typedTextReader) MediaType() string { return r.mediaType } + +type lifecycleReader struct { + io.Reader + closed bool +} + +func (r *lifecycleReader) Close() error { + r.closed = true + return nil +} + +type cancelReadResource struct { + lifecycleReader + cancel context.CancelFunc + reads int +} + +func (r *cancelReadResource) Read(p []byte) (int, error) { + r.reads++ + n, err := r.Reader.Read(p) + r.cancel() + + return n, err +} + +type trackedReader struct { + io.Reader + closed *bool +} + +func (r trackedReader) Close() error { *r.closed = true; return nil } + +type dtdFailureReader struct { + io.Reader + readError error + closeError error + closed bool +} + +func (r *dtdFailureReader) Read(p []byte) (int, error) { + n, err := r.Reader.Read(p) + if n > 0 && r.readError != nil { + return n, r.readError + } + return n, err +} + +func (r *dtdFailureReader) Close() error { r.closed = true; return r.closeError } + +type parameterDocumentHostCase struct { + name string + parameters string + attributes string + declarations string + body string + want string +} + +func parameterDocumentHostCases() []parameterDocumentHostCase { + const xmlOutput = `method="xml" omit-xml-declaration="yes"` + const root = `` + return []parameterDocumentHostCase{ + {"XHTML method retained", ``, `method="text"`, "", ``, ``}, + {"URI escaping false overrides true", ``, `escape-uri-attributes="yes"`, "", `x`, `x`}, + {"absent URI escaping inherits true", ``, `escape-uri-attributes="yes"`, "", `x`, `x`}, + {"normalization none overrides NFC", ``, `normalization-form="NFC"`, "", "e\u0301", "e\u0301"}, + {"fully normalized stays byte host supported", ``, "", "", "e\u0301", "é"}, + {"fully normalized leading combining", ``, "", "", ``, "SERE0012"}, + {"extension method rejected by host choice", ``, `omit-xml-declaration="yes"`, "", `x`, errCodeSEPM0016}, + {"method beats direct attribute", ``, xmlOutput, "", `x`, "x"}, + {"false beats true", ``, xmlOutput + ` indent="yes"`, "", root, root}, + {"BOM survives byte host", ``, `byte-order-mark="no"`, "", `x`, "\ufeffx"}, + {"empty separator beats attribute", ``, `item-separator="|"`, "", ``, "12"}, + {"separator beats absent", ``, `item-separator="#absent"`, "", ``, "1-2"}, + {"empty doctype removes inherited", ``, xmlOutput + ` doctype-system="old.dtd"`, "", ``, ``}, + {"QName list union", ``, xmlOutput + ` cdata-section-elements="b"`, "", `x<yx<y`, ``}, + {"parameter namespace default", ``, xmlOutput, "", `x<y`, `x<y`}, + {"QName parameter binding", ``, xmlOutput, "", `x<yx<y`, `x<y`}, + {"QName list absent preserves local", ``, xmlOutput + ` cdata-section-elements="b"`, "", `x<yx<y`, `x<y`}, + {"foreign character map ignored", ``, "", "", `x`, "x"}, + {"QName unknown prefix", ``, xmlOutput, "", root, errCodeSEPM0017}, + {"character map namespace and precedence", ``, `method="text" use-character-maps="m"`, ``, `x`, "P"}, + {"empty character map clears direct", ``, `method="text" use-character-maps="m"`, ``, `x`, "x"}, + {"invalid boolean", ``, `method="text"`, "", `x`, errCodeSEPM0017}, + {"duplicate scalar", ``, "", "", `x`, errCodeSEPM0019}, + {"duplicate character", ``, "", "", `x`, errCodeSEPM0018}, + {"unknown attribute", ``, "", "", `x`, errCodeSEPM0017}, + {"inherited output method", ``, `omit-xml-declaration="yes"`, ``, `x`, "x"}, + {"default host XML declaration", ``, "", "", ``, ``}, + } +} + +func parameterDocumentHostSheet(route, href string, test parameterDocumentHostCase) string { + output := `` + body := test.body + if route != "output" { + output = `` + if test.name == "default host XML declaration" { + output = `` + } + attribute := href + if route == "dynamic" { + attribute = `{$parameterURI}` + } + resultURI := "" + if route == "secondary" || route == "dynamic" { + resultURI = ` href="urn:result"` + } + body = `` + body + `` + } + return sheetStart + version30 + `" exclude-result-prefixes="xs">` + test.declarations + output + `` + body + `` +} + +func evaluateParameterDocumentHost(t *testing.T, route, source, parameters string) (string, error) { + t.Helper() + e := engine(t, transform.Config{Resolver: transform.ResolverFunc(func(ctx context.Context, _ string) (io.ReadCloser, error) { + return io.NopCloser(strings.NewReader(parameters)), ctx.Err() + })}) + compiled, err := e.CompileXSLT(t.Context(), []byte(source), transform.XSLTOptions{ProcessorVersion: transform.XSLT30}) + if err != nil { + return "", err + } + result, err := compiled.Transform(t.Context(), nil, transform.XSLTContext{Template: transform.Name{Local: nameStart}}) + if err != nil { + return "", err + } + if route == "secondary" || route == "dynamic" { + return string(result.Secondary["urn:result"]), nil + } + return string(result.Primary), nil +} + +const integrationTransformOptions = `map{'stylesheet-text':$s,'initial-template':QName('','start'),'delivery-format':'serialized'}` + +func integrationStyle(body string) string { + return `` + body + `` +} + +func integrationResolver(value string, loads *atomic.Int32) transform.ResolverFunc { + return func(_ context.Context, uri string) (io.ReadCloser, error) { + loads.Add(1) + body := value + if uri == "urn:doc" { + body = "" + value + "" + } + return io.NopCloser(strings.NewReader(body)), nil + } +} + +func integrationSavedXPath(t *testing.T, e *transform.Engine, expression, base string) transform.Sequence { + t.Helper() + q, err := e.CompileXPath(t.Context(), expression, transform.XPathOptions{BaseURI: base}) + if err != nil { + t.Fatal(err) + } + f, err := q.Evaluate(t.Context(), nil, transform.XPathContext{}) + if err != nil { + t.Fatal(err) + } + return f +} + +func integrationTransformCall(t *testing.T, e *transform.Engine, f transform.Sequence, style, options, base string) (transform.Sequence, error) { + t.Helper() + q, err := e.CompileXPath(t.Context(), `$f(`+options+`)?output`, transform.XPathOptions{BaseURI: base}) + if err != nil { + t.Fatal(err) + } + return q.Evaluate(t.Context(), nil, transform.XPathContext{Variables: transform.Bindings{ + {Local: "f"}: f, {Local: "s"}: transform.Values(transform.String(style)), + }}) +} + +func TestIntegrationSavedTransformCallerResources(t *testing.T) { + style := integrationStyle(``) + for _, expression := range []string{`transform#1`, `function($o){transform($o)}`, `function-lookup(QName('http://www.w3.org/2005/xpath-functions','transform'),1)`, `transform(?)`} { + t.Run(expression, func(t *testing.T) { + var aLoads, bLoads atomic.Int32 + a := engine(t, transform.Config{Resolver: integrationResolver("producer", &aLoads)}) + f := integrationSavedXPath(t, a, expression, "") + b := engine(t, transform.Config{Resolver: integrationResolver("consumer", &bLoads)}) + got, err := integrationTransformCall(t, b, f, style, integrationTransformOptions, "") + if err != nil || text(t, got) != "consumer" || aLoads.Load() != 0 || bLoads.Load() != 1 { + t.Fatal(got, err, aLoads.Load(), bLoads.Load()) + } + denied := engine(t, transform.Config{}) + got, err = integrationTransformCall(t, denied, f, style, integrationTransformOptions, "") + if err == nil || got != (transform.Sequence{}) || aLoads.Load() != 0 { + t.Fatal("denied caller retained producer resolver", got, err, aLoads.Load()) + } + }) + } +} + +func TestIntegrationTransformCallerLimitsAndStaticBase(t *testing.T) { + style := integrationStyle(``) + for _, tc := range []struct { + name string + producerMax, callerMax int + producerResolver, wantLimit bool + }{ + {name: "caller grants resolver"}, + {name: "caller narrows bytes", callerMax: 8, producerResolver: true, wantLimit: true}, + {name: "caller expands bytes", producerMax: 64, callerMax: 4096, producerResolver: true}, + } { + t.Run(tc.name, func(t *testing.T) { + var aLoads, bLoads atomic.Int32 + config := transform.Config{Limits: transform.Limits{MaxInputBytes: tc.producerMax}} + if tc.producerResolver { + config.Resolver = integrationResolver(strings.Repeat("p", 64), &aLoads) + } + a := engine(t, config) + f := integrationSavedXPath(t, a, `transform#1`, "https://producer.invalid/base/query.xq") + b := engine(t, transform.Config{Limits: transform.Limits{MaxInputBytes: tc.callerMax}, Resolver: transform.ResolverFunc(func(_ context.Context, uri string) (io.ReadCloser, error) { + bLoads.Add(1) + if uri != "https://producer.invalid/base/payload.txt" { + t.Errorf("static base changed: %q", uri) + } + return io.NopCloser(strings.NewReader(strings.Repeat("c", 64))), nil + })}) + options := `map{'stylesheet-text':$s,'stylesheet-base-uri':'./nested.xsl','initial-template':QName('','start'),'delivery-format':'serialized'}` + got, err := integrationTransformCall(t, b, f, style, options, "https://consumer.invalid/other/query.xq") + if tc.wantLimit { + if !errors.Is(err, transform.ErrLimit) || got != (transform.Sequence{}) { + t.Fatal(got, err) + } + return + } + if err != nil || text(t, got) != strings.Repeat("c", 64) || aLoads.Load() != 0 || bLoads.Load() != 1 { + t.Fatal(got, err, aLoads.Load(), bLoads.Load()) + } + }) + } +} + +type integrationCollection string + +func (c integrationCollection) ResolveCollection(context.Context, string) (transform.Sequence, error) { + return transform.Values(transform.String(string(c))), nil +} + +func (c integrationCollection) ResolveURICollection(context.Context, string) ([]string, error) { + return []string{string(c)}, nil +} + +func integrationSecondaryPolicy(kind, value string, allowed bool, loads *atomic.Int32) transform.Config { + cfg := transform.Config{} + switch kind { + case "collection", "uri-collection": + if allowed { + cfg.CollectionResolver = integrationCollection(value) + } + case "package": + if allowed { + cfg.PackageResolver = transform.PackageResolverFunc(func(context.Context, string, string) (transform.PackageResource, error) { + loads.Add(1) + source := `` + value + `` + return transform.PackageResource{Reader: io.NopCloser(strings.NewReader(source)), BaseURI: "urn:p"}, nil + }) + } + case "encoding": + cfg.AllowSingleByteEncodings = allowed + cfg.Resolver = integrationResolver("\xe9", loads) + default: + if allowed { + cfg.Resolver = integrationResolver(value, loads) + } + } + return cfg +} + +func integrationSecondaryBody(kind string) string { + switch kind { + case "uri-collection": + return `` + case "DTD": + return `` + case "encoding": + return `` + default: + return `` + } +} + +func TestIntegrationTransformCallerSecondaryPolicies(t *testing.T) { + for _, kind := range []string{"collection", "uri-collection", "package", "DTD", "encoding"} { + for _, producerAllowed := range []bool{true, false} { + name := kind + "/caller grants" + if producerAllowed { + name = kind + "/caller denies" + } + t.Run(name, func(t *testing.T) { + var aLoads, bLoads atomic.Int32 + a := engine(t, integrationSecondaryPolicy(kind, "producer", producerAllowed, &aLoads)) + f := integrationSavedXPath(t, a, `transform#1`, "") + b := engine(t, integrationSecondaryPolicy(kind, "consumer", !producerAllowed, &bLoads)) + options := integrationTransformOptions + if kind == "package" { + options = `map{'package-name':'urn:p','initial-template':QName('','start'),'delivery-format':'serialized'}` + } + got, err := integrationTransformCall(t, b, f, integrationStyle(integrationSecondaryBody(kind)), options, "") + if producerAllowed { + if err == nil || got != (transform.Sequence{}) || aLoads.Load() != 0 { + t.Fatal("caller denial ignored", got, err, aLoads.Load()) + } + return + } + want := "consumer" + if kind == "encoding" { + want = "é" + } + if err != nil || text(t, got) != want || aLoads.Load() != 0 { + t.Fatal(got, err, aLoads.Load(), bLoads.Load()) + } + }) + } + } +} + +func integrationSavedXSLT(t *testing.T, e *transform.Engine, body string) transform.Sequence { + t.Helper() + source := `` + sheet, err := e.CompileXSLT(t.Context(), []byte(source), transform.XSLTOptions{BaseURI: "https://producer.invalid/base/main.xsl"}) + if err != nil { + t.Fatal(err) + } + result, err := sheet.CallTemplate(t.Context(), nil, transform.Name{Local: "start"}, transform.XSLTContext{}) + if err != nil { + t.Fatal(err) + } + return result.Primary() +} + +func TestIntegrationSavedXSLTFunctionCallerResources(t *testing.T) { + for _, tc := range []struct{ name, body, call string }{ + {"user text", "f:read#0", "string($f())"}, + {"inline text", `function(){unparsed-text('urn:data')}`, "string($f())"}, + {"builtin text", `unparsed-text#1`, "string($f('urn:data'))"}, + {"builtin doc", `doc#1`, "string($f('urn:doc'))"}, + {"inline doc", `function(){doc('urn:doc')}`, "string($f())"}, + {"builtin document", `document#1`, "string($f('urn:doc'))"}, + } { + t.Run(tc.name, func(t *testing.T) { + var aLoads, bLoads atomic.Int32 + a := engine(t, transform.Config{Resolver: integrationResolver("producer", &aLoads)}) + f := integrationSavedXSLT(t, a, tc.body) + b := engine(t, transform.Config{Resolver: integrationResolver("consumer", &bLoads)}) + q := query(t, b, tc.call, transform.XPath31) + got, err := q.Evaluate(t.Context(), nil, transform.XPathContext{Variables: transform.Bindings{{Local: "f"}: f}}) + if err != nil || text(t, got) != "consumer" || aLoads.Load() != 0 || bLoads.Load() != 1 { + t.Fatal(got, err, aLoads.Load(), bLoads.Load()) + } + }) + } +} + +func TestIntegrationEscapedDocumentConcurrentCallers(t *testing.T) { + var loads [3]atomic.Int32 + engines := []*transform.Engine{ + engine(t, transform.Config{Resolver: integrationResolver("A", &loads[0])}), + engine(t, transform.Config{Resolver: integrationResolver("B", &loads[1])}), + engine(t, transform.Config{Resolver: integrationResolver("C", &loads[2])}), + } + f := integrationSavedXSLT(t, engines[0], `doc#1`) + queries := make([]*transform.XPath, len(engines)) + for i, e := range engines { + queries[i] = query(t, e, `string($f('urn:doc'))`, transform.XPath31) + } + var wg sync.WaitGroup + for i := range 12 { + wg.Go(func() { + index := i % len(queries) + got, err := queries[index].Evaluate(t.Context(), nil, transform.XPathContext{Variables: transform.Bindings{{Local: "f"}: f}}) + value, _ := got.Text() + if err != nil || value != string(rune('A'+index)) { + t.Errorf("caller%d got %q,%v", index, value, err) + } + }) + } + wg.Wait() + for i := range loads { + if loads[i].Load() != 4 { + t.Errorf("caller%d loads%d want4", i, loads[i].Load()) + } + } +} + +func TestIntegrationCallbackInputWork(t *testing.T) { + for _, shape := range []string{"sequence", "map"} { + for _, tc := range []struct { + name string + items, calls, operations int + wantLimit bool + }{ + {"small", 2, 20, 500, false}, {"first callback blocked", 100, 1, 50, true}, {"cumulative", 100, 20, 500, true}, + } { + t.Run(shape+"/"+tc.name, func(t *testing.T) { + values := make([]transform.Value, tc.items) + entries := make([]transform.MapEntry, tc.items) + for i := range values { + values[i] = transform.Integer(int64(i)) + entries[i] = transform.MapEntry{Key: values[i]} + } + argument := transform.Values(values...) + if shape == "map" { + m, err := transform.Map(entries...) + if err != nil { + t.Fatal(err) + } + argument = transform.Values(m) + } + calls := 0 + e := engine(t, transform.Config{Limits: transform.Limits{MaxOperations: tc.operations, MaxNodes: 1000}, Functions: []transform.Function{{Name: transform.Name{Space: "urn:cb", Local: "discard"}, Arity: 1, Call: func(context.Context, []transform.Sequence) (transform.Sequence, error) { + calls++ + return transform.Sequence{}, nil + }}}}) + q := query(t, e, `count(for $i in 1 to $n return Q{urn:cb}discard($m))`, transform.XPath31) + got, err := q.Evaluate(t.Context(), nil, transform.XPathContext{Variables: transform.Bindings{{Local: "m"}: argument, {Local: "n"}: transform.Values(transform.Integer(int64(tc.calls)))}}) + if tc.wantLimit { + if !errors.Is(err, transform.ErrLimit) || got != (transform.Sequence{}) || calls >= tc.calls || tc.name == "cumulative" && calls == 0 { + t.Fatal(got, err, calls) + } + return + } + if err != nil || text(t, got) != "0" || calls != tc.calls { + t.Fatal(got, err, calls) + } + }) + } + } +} + +func TestIntegrationCallbackCallerNodeBudget(t *testing.T) { + values := make([]transform.Value, 11) + for i := range values { + values[i] = transform.String("x") + } + a := engine(t, transform.Config{Functions: []transform.Function{{Name: transform.Name{Space: "urn:cb", Local: "large"}, Arity: 0, Call: func(context.Context, []transform.Sequence) (transform.Sequence, error) { + return transform.Values(values...), nil + }}}}) + f := integrationSavedXPath(t, a, `Q{urn:cb}large#0`, "") + for _, limit := range []int{10, 11} { + b := engine(t, transform.Config{Limits: transform.Limits{MaxNodes: limit}}) + q := query(t, b, `count($f())`, transform.XPath31) + got, err := q.Evaluate(t.Context(), nil, transform.XPathContext{Variables: transform.Bindings{{Local: "f"}: f}}) + if limit == 10 { + if !errors.Is(err, transform.ErrLimit) || got != (transform.Sequence{}) { + t.Fatal(got, err) + } + continue + } + if err != nil || text(t, got) != "11" { + t.Fatal(got, err) + } + } +} + +func TestIntegrationEscapedTransformStaticBase(t *testing.T) { + style := integrationStyle(``) + for _, expression := range []string{`transform#1`, `function($o){transform($o)}`, `function-lookup(QName('http://www.w3.org/2005/xpath-functions','transform'),1)`, `transform(?)`} { + t.Run(expression, func(t *testing.T) { + var loads atomic.Int32 + config := transform.Config{Resolver: transform.ResolverFunc(func(_ context.Context, uri string) (io.ReadCloser, error) { + loads.Add(1) + if uri != "https://producer.invalid/base/entry/payload.txt" { + t.Errorf("lost definition xml:base: %q", uri) + } + return io.NopCloser(strings.NewReader("ok")), nil + })} + a := engine(t, config) + f := integrationSavedXSLT(t, a, expression) + b := engine(t, config) + options := `map{'stylesheet-text':$s,'stylesheet-base-uri':'./nested.xsl','initial-template':QName('','start'),'delivery-format':'serialized'}` + for _, caller := range []*transform.Engine{a, b} { + got, err := integrationTransformCall(t, caller, f, style, options, "https://consumer.invalid/other/query.xq") + if err != nil || text(t, got) != "ok" { + t.Fatal(got, err) + } + } + if loads.Load() != 2 { + t.Fatal(loads.Load()) + } + }) + } +} + +func TestIntegrationNestedTransformSharedBudgets(t *testing.T) { + var aLoads atomic.Int32 + a := engine(t, transform.Config{Resolver: integrationResolver("producer", &aLoads)}) + f := integrationSavedXPath(t, a, `transform#1`, "") + for _, tc := range []struct { + name string + limits transform.Limits + prefix string + payload int + wantLimit bool + }{ + {name: "per-resource overflow", limits: transform.Limits{MaxInputBytes: 256}, payload: 257, wantLimit: true}, + {name: "per-resource exact", limits: transform.Limits{MaxInputBytes: 257}, payload: 257}, + {name: "shared load count", limits: transform.Limits{MaxResources: 1}, prefix: `unparsed-text('urn:before') || `, payload: 32, wantLimit: true}, + {name: "shared aggregate overflow", limits: transform.Limits{MaxResourceBytes: 63}, prefix: `unparsed-text('urn:before') || `, payload: 32, wantLimit: true}, + {name: "shared aggregate exact", limits: transform.Limits{MaxResourceBytes: 64}, prefix: `unparsed-text('urn:before') || `, payload: 32}, + } { + t.Run(tc.name, func(t *testing.T) { + var bLoads atomic.Int32 + b := engine(t, transform.Config{Limits: tc.limits, Resolver: integrationResolver(strings.Repeat("c", tc.payload), &bLoads)}) + q := query(t, b, tc.prefix+`$f(`+integrationTransformOptions+`)?output`, transform.XPath31) + style := integrationStyle(``) + got, err := q.Evaluate(t.Context(), nil, transform.XPathContext{Variables: transform.Bindings{{Local: "f"}: f, {Local: "s"}: transform.Values(transform.String(style))}}) + if tc.wantLimit { + if !errors.Is(err, transform.ErrLimit) || got != (transform.Sequence{}) { + t.Fatal(got, err, bLoads.Load()) + } + return + } + want := tc.payload + if tc.prefix != "" { + want *= 2 + } + if err != nil || text(t, got) != strings.Repeat("c", want) { + t.Fatal(got, err, bLoads.Load()) + } + }) + } + if aLoads.Load() != 0 { + t.Fatal("producer resource capabilities retained", aLoads.Load()) + } +} + +func TestIntegrationNestedTransformExecutionLimits(t *testing.T) { + a := engine(t, transform.Config{}) + f := integrationSavedXPath(t, a, `transform#1`, "") + for _, tc := range []struct { + name, style string + limits transform.Limits + }{ + {name: "program", limits: transform.Limits{MaxProgramBytes: 256}, style: integrationStyle(strings.Repeat("x", 257))}, + {name: "operations", limits: transform.Limits{MaxOperations: 500}, style: integrationStyle(``)}, + {name: "depth", limits: transform.Limits{MaxDepth: 4}, style: ``}, + } { + t.Run(tc.name, func(t *testing.T) { + b := engine(t, transform.Config{Limits: tc.limits}) + got, err := integrationTransformCall(t, b, f, tc.style, integrationTransformOptions, "") + if !errors.Is(err, transform.ErrLimit) || got != (transform.Sequence{}) { + t.Fatal(got, err) + } + }) + } +} + +func TestIntegrationEscapedResourceControlCauses(t *testing.T) { + a := engine(t, transform.Config{}) + f := integrationSavedXPath(t, a, `transform#1`, "") + style := integrationStyle(``) + for _, cause := range []error{transform.ErrLimit, context.Canceled, context.DeadlineExceeded} { + t.Run(cause.Error(), func(t *testing.T) { + b := engine(t, transform.Config{Resolver: transform.ResolverFunc(func(context.Context, string) (io.ReadCloser, error) { return nil, cause })}) + got, err := integrationTransformCall(t, b, f, style, integrationTransformOptions, "") + if !errors.Is(err, cause) || got != (transform.Sequence{}) { + t.Fatal(got, err, cause) + } + }) + } +} + +func TestIntegrationCallbackCallerValueLimits(t *testing.T) { + nested := transform.Values(transform.String("x")) + for range 3 { + nested = transform.Values(transform.Array(nested)) + } + for _, tc := range []struct { + name string + value transform.Sequence + limits transform.Limits + }{ + {name: "bytes", value: transform.Values(transform.String(strings.Repeat("x", 11))), limits: transform.Limits{MaxOutputBytes: 10}}, + {name: "depth", value: nested, limits: transform.Limits{MaxDepth: 2}}, + } { + t.Run(tc.name, func(t *testing.T) { + a := engine(t, transform.Config{Functions: []transform.Function{{Name: transform.Name{Space: "urn:cb", Local: "value"}, Arity: 0, Call: func(context.Context, []transform.Sequence) (transform.Sequence, error) { return tc.value, nil }}}}) + f := integrationSavedXPath(t, a, `Q{urn:cb}value#0`, "") + b := engine(t, transform.Config{Limits: tc.limits}) + got, err := query(t, b, `count($f())`, transform.XPath31).Evaluate(t.Context(), nil, transform.XPathContext{Variables: transform.Bindings{{Local: "f"}: f}}) + if !errors.Is(err, transform.ErrLimit) || got != (transform.Sequence{}) { + t.Fatal(got, err) + } + }) + } +} + +func TestIntegrationXSLTFunctionDefinitionBase(t *testing.T) { + for _, tc := range []struct{ name, body, call string }{ + {"user function", `f:relative#0`, `string($f())`}, + {"nested transform reference", `f:nested()`, `$f(map{'stylesheet-text':$s,'stylesheet-base-uri':'./nested.xsl','initial-template':QName('','start'),'delivery-format':'serialized'})?output`}, + } { + t.Run(tc.name, func(t *testing.T) { + a := engine(t, transform.Config{}) + f := integrationSavedXSLT(t, a, tc.body) + var loads atomic.Int32 + b := engine(t, transform.Config{Resolver: transform.ResolverFunc(func(_ context.Context, uri string) (io.ReadCloser, error) { + loads.Add(1) + if uri != "https://producer.invalid/base/func/payload.txt" { + t.Errorf("lost function definition base: %q", uri) + } + return io.NopCloser(strings.NewReader("ok")), nil + })}) + q, err := b.CompileXPath(t.Context(), tc.call, transform.XPathOptions{BaseURI: "https://consumer.invalid/other/query.xq"}) + if err != nil { + t.Fatal(err) + } + style := integrationStyle(``) + got, err := q.Evaluate(t.Context(), nil, transform.XPathContext{Variables: transform.Bindings{{Local: "f"}: f, {Local: "s"}: transform.Values(transform.String(style))}}) + if err != nil || text(t, got) != "ok" || loads.Load() != 1 { + t.Fatal(got, err, loads.Load()) + } + }) + } +} + +func TestIntegrationApprovalCapabilitiesStaySeparate(t *testing.T) { + var resources, callbacks atomic.Int32 + cfg := transform.Config{Resolver: integrationResolver("allowed", &resources), Functions: []transform.Function{{Name: transform.Name{Space: "urn:cb", Local: "value"}, Arity: 0, Call: func(context.Context, []transform.Sequence) (transform.Sequence, error) { + callbacks.Add(1) + return transform.Values(transform.String("app")), nil + }}}} + for _, body := range []string{ + ``, + ``, + ``, + } { + cfg.AllowedPrograms = []reference.Transform{style(body)} + if _, err := transform.NewEngine(cfg); err == nil { + t.Fatal("language capability approved XMLDSig program", body) + } + } + tr := style(`safe`) + cfg.AllowedPrograms = []reference.Transform{tr} + e := engine(t, cfg) + for _, route := range []string{"Apply", "WriteTransform"} { + t.Run(route, func(t *testing.T) { + source := `]>&e;` + if route == "Apply" { + got, err := e.Apply(t.Context(), tr, reference.Octets([]byte(source)), reference.ExtensionContext{}) + if err == nil { + t.Fatal("XMLDSig input accepted DTD", got, err) + } + requireInvalidReferenceResult(t, got) + return + } + var output strings.Builder + err := e.WriteTransform(t.Context(), &output, strings.NewReader(source), tr, reference.ExtensionContext{}) + if err == nil || output.Len() != 0 { + t.Fatal("XMLDSig reader input accepted DTD", output.String(), err) + } + }) + } + if resources.Load() != 0 || callbacks.Load() != 0 { + t.Fatal("preflight/security parse invoked capabilities", resources.Load(), callbacks.Load()) + } + if got := text(t, evaluate(t, e, `Q{urn:cb}value()`, transform.XPath31)); got != "app" { + t.Fatal(got) + } + if err := e.Validate(t.Context(), xp(trueExpression)); !errors.Is(err, reference.ErrPolicy) { + t.Fatal("standalone capability approved another program", err) + } +} + +func TestIntegrationResourceBinderPreservesIntrospectionTypes(t *testing.T) { + a := engine(t, transform.Config{}) + b := engine(t, transform.Config{}) + for _, tc := range []struct{ expression, signature string }{ + {`system-property#1`, `function(xs:string) as xs:string`}, + {`element-available#1`, `function(xs:string) as xs:boolean`}, + {`function-lookup(QName('http://www.w3.org/2005/xpath-functions','system-property'),1)`, `function(xs:string) as xs:string`}, + {`function-lookup(QName('http://www.w3.org/2005/xpath-functions','element-available'),1)`, `function(xs:string) as xs:boolean`}, + } { + t.Run(tc.expression, func(t *testing.T) { + f := integrationSavedXSLT(t, a, tc.expression) + q := query(t, b, `$f instance of `+tc.signature+` and not($f instance of function(xs:integer) as item()*) and not($f instance of function(xs:string) as xs:integer)`, transform.XPath31) + got, err := q.Evaluate(t.Context(), nil, transform.XPathContext{Variables: transform.Bindings{{Local: "f"}: f}}) + if err != nil || text(t, got) != "true" { + t.Fatal("resource binder changed non-resource function metadata", got, err) + } + }) + } +} + +func TestIntegrationCallbackNumericCopyBudget(t *testing.T) { + large := transform.BigInteger(new(big.Int).SetBit(new(big.Int), 511, 1)) + pair := transform.Values(large, large) + m, err := transform.Map(transform.MapEntry{Key: transform.String("a"), Value: pair}) + if err != nil { + t.Fatal(err) + } + decimal, err := transform.Decimal(new(big.Int).SetBit(new(big.Int), 511, 1).String()) + if err != nil { + t.Fatal(err) + } + a := engine(t, transform.Config{}) + floats := integrationSavedXPath(t, a, `let $v:=xs:double('1.5') return ($v,$v)`, "") + for _, tc := range []struct { + name string + argument transform.Sequence + calls, bytes int + limit, zero bool + }{ + {"singleton occurrence", transform.Values(large), 32, 1024, true, false}, + {"flat copies cumulative", pair, 32, 1024, true, false}, + {"flat copies fit", pair, 2, 1024, false, false}, + {"per-call input blocked", pair, 1, 127, true, true}, + {"first copy blocked", pair, 1, 129, true, true}, + {"small ints shared", transform.Values(transform.Integer(1), transform.Integer(2)), 32, 1024, false, false}, + {"immutable strings shared", transform.Values(transform.String(strings.Repeat("x", 64)), transform.String(strings.Repeat("y", 64))), 32, 1024, false, false}, + {"immutable map shared", transform.Values(m), 32, 1024, false, false}, + {"immutable array shared", transform.Values(transform.Array(pair)), 32, 1024, false, false}, + {"decimal copies cumulative", transform.Values(decimal, decimal), 32, 1024, true, false}, + {"decimal copies fit", transform.Values(decimal, decimal), 2, 1024, false, false}, + {"float pointer fit", floats, 2, 1024, false, false}, + {"float pointer cumulative", floats, 80, 1024, true, false}, + } { + t.Run(tc.name, func(t *testing.T) { + calls := 0 + e := engine(t, transform.Config{Limits: transform.Limits{MaxOutputBytes: tc.bytes, MaxOperations: 5000}, Functions: []transform.Function{{Name: transform.Name{Space: "urn:cb", Local: "discard"}, Arity: 1, Call: func(context.Context, []transform.Sequence) (transform.Sequence, error) { + calls++ + return transform.Sequence{}, nil + }}}}) + q := query(t, e, `count(for $i in 1 to $n return Q{urn:cb}discard($v))`, transform.XPath31) + got, err := q.Evaluate(t.Context(), nil, transform.XPathContext{Variables: transform.Bindings{{Local: "v"}: tc.argument, {Local: "n"}: transform.Values(transform.Integer(int64(tc.calls)))}}) + if tc.limit { + if !errors.Is(err, transform.ErrLimit) || got != (transform.Sequence{}) || calls >= tc.calls || tc.zero && calls != 0 || !tc.zero && calls == 0 { + t.Fatal(got, err, calls, tc.calls) + } + return + } + if err != nil || text(t, got) != "0" || calls != tc.calls { + t.Fatal(got, err, calls, tc.calls) + } + }) + } +} + +func TestIntegrationCallbackNonNumericCopyBudget(t *testing.T) { + a := engine(t, transform.Config{}) + for _, tc := range []struct{ name, expression string }{ + {"binary", `let $v:=xs:base64Binary('` + strings.Repeat("QUFB", 21) + `QQ==') return ($v,$v)`}, + {"duration", `let $v:=xs:dayTimeDuration('PT0.` + strings.Repeat("1", 72) + `S') return ($v,$v)`}, + } { + t.Run(tc.name, func(t *testing.T) { + pair := integrationSavedXPath(t, a, tc.expression, "") + first, ok := pair.At(0) + if !ok { + t.Fatal("missing atomic value") + } + canonical, err := first.Text() + if err != nil { + t.Fatal(err) + } + m, err := transform.Map(transform.MapEntry{Key: transform.String("a"), Value: pair}) + if err != nil { + t.Fatal(err) + } + for _, vector := range []struct { + name string + argument transform.Sequence + calls, bytes int + limit, zero bool + }{ + {"flat copies cumulative", pair, 32, 1024, true, false}, + {"flat copies fit", pair, 2, 1024, false, false}, + {"per-call input blocked", pair, 1, 2*len(canonical) - 1, true, true}, + {"immutable strings shared", transform.Values(transform.String(canonical), transform.String(canonical)), 32, 1024, false, false}, + {"immutable map shared", transform.Values(m), 32, 1024, false, false}, + {"immutable array shared", transform.Values(transform.Array(pair)), 32, 1024, false, false}, + } { + t.Run(vector.name, func(t *testing.T) { + calls := 0 + b := engine(t, transform.Config{Limits: transform.Limits{MaxOutputBytes: vector.bytes, MaxOperations: 5000}, Functions: []transform.Function{{Name: transform.Name{Space: "urn:cb", Local: "discard"}, Arity: 1, Call: func(context.Context, []transform.Sequence) (transform.Sequence, error) { + calls++ + return transform.Sequence{}, nil + }}}}) + q := query(t, b, `count(for $i in 1 to $n return Q{urn:cb}discard($v))`, transform.XPath31) + got, err := q.Evaluate(t.Context(), nil, transform.XPathContext{Variables: transform.Bindings{{Local: "v"}: vector.argument, {Local: "n"}: transform.Values(transform.Integer(int64(vector.calls)))}}) + if vector.limit { + if !errors.Is(err, transform.ErrLimit) || got != (transform.Sequence{}) || calls >= vector.calls || vector.zero && calls != 0 || !vector.zero && calls == 0 { + t.Fatal(got, err, calls, vector.calls) + } + return + } + if err != nil || text(t, got) != "0" || calls != vector.calls { + t.Fatal(got, err, calls, vector.calls) + } + }) + } + }) + } +} diff --git a/transform/result.go b/transform/result.go index f22a914..ea66ad5 100644 --- a/transform/result.go +++ b/transform/result.go @@ -7,6 +7,7 @@ import ( "maps" "slices" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/nilcheck" "github.com/skarm/xmlsec/internal/streamio" "github.com/skarm/xmlsec/transform/internal/xdm" @@ -16,11 +17,16 @@ import ( // Result is a complete immutable XSLT result before serialization. It supports // concurrent reads and reuse as XPath/XSLT input without an XML round trip. type Result struct { - engine *Engine - native *xslt.Execution - primary Sequence + // engine retains the policy used when serializing or reusing results. + engine *Engine + // native retains execution metadata and serialization settings. + native *xslt.Execution + // primary is the immutable principal XDM result sequence. + primary Sequence + // secondary maps result URIs to immutable secondary sequences. secondary map[string]Sequence - messages []Message + // messages retains xsl:message diagnostics in execution order. + messages []Message } // Primary returns the primary XDM sequence, preserving order and atomic types. @@ -71,8 +77,8 @@ func (r *Result) OutputEncoding(uri string) (string, error) { } } - if value.Output == nil || value.Output.Encoding == "" || value.Output.Method == "adaptive" { - return "UTF-8", nil + if value.Output == nil || value.Output.Encoding == "" || value.Output.Method == lexicon.MethodAdaptive { + return lexicon.EncodingUTF8U, nil } return value.Output.Encoding, nil @@ -150,7 +156,7 @@ func (r *Result) WriteSecondary(ctx context.Context, uri string, dst io.Writer) return 0, wrap("write secondary XSLT", ErrInput) } - return r.writeValue(ctx, dst, value) + return r.writeValue(ctx, dst, value, "write secondary XSLT") } // Document converts a single document or element result to a complete XML @@ -194,14 +200,14 @@ func (r *Result) Document(ctx context.Context) (*Document, error) { return doc, nil } -func (r *Result) writeValue(ctx context.Context, dst io.Writer, value xslt.ValueResult) (int64, error) { +func (r *Result) writeValue(ctx context.Context, dst io.Writer, value xslt.ValueResult, operation string) (int64, error) { if nilcheck.IsNil(dst) { - return 0, wrap("write XSLT", ErrInput) + return 0, wrap(operation, ErrInput) } ctx, cancel, err := r.engine.invocation(ctx) if err != nil { - return 0, err + return 0, wrap(operation, err) } defer cancel() @@ -212,7 +218,7 @@ func (r *Result) writeValue(ctx context.Context, dst io.Writer, value xslt.Value err = ctx.Err() } - return writer.Written, xsltError("write XSLT", err) + return writer.Written, xsltError(operation, err) } // Execute runs the selected template or mode and returns typed XDM results. @@ -319,7 +325,7 @@ func (w resultWriterTo) WriteTo(dst io.Writer) (int64, error) { return 0, wrap("write XSLT", ErrInput) } - return r.writeValue(w.ctx, dst, r.native.Primary) + return r.writeValue(w.ctx, dst, r.native.Primary, "write XSLT") } type nodePublication struct { diff --git a/transform/serialization_test.go b/transform/serialization_test.go index 363ed4e..3b74542 100644 --- a/transform/serialization_test.go +++ b/transform/serialization_test.go @@ -49,7 +49,7 @@ func markupSerializationCases() []markupSerializationCase { const before = `leadv` const xmlOutput = `` const xhtml5 = `` - const maps = `` + const ms = `` tests := []markupSerializationCase{ {"XML default omits prefix undeclarations", ``, ``, ``, ""}, {"XML 1.1 keeps requested prefix undeclarations", ``, ``, ``, ""}, @@ -74,7 +74,7 @@ func markupSerializationCases() []markupSerializationCase { {"HTML5 leading text", ``, `text`, `text`, ""}, {"XHTML5 automatic doctype", ``, before + ``, ``, ""}, {"XHTML5 leading text", ``, `text`, `text`, ""}, - {"CDATA maps excluded", `` + maps, `aaaaaa ]]>`, `AAA ]]>`, `contains($s,'AAA') and contains($s,'` + ms, `aaaaaa ]]>`, `AAA ]]>`, `contains($s,'AAA') and contains($s,'`, `x&yz<tx&y`, `x&y`, ""}, {"CDATA no namespace stays exact", ``, `x&yx&y`, `x&y`, ""}, {"CDATA fallback namespace stays exact", ``, `xx&y`, `xx&y`, ""}, @@ -431,25 +431,6 @@ func TestOutputNameListsAcrossImports(t *testing.T) { } } -type markupCutWriter struct { - bytes.Buffer - remaining int -} - -func (w *markupCutWriter) WriteString(data string) (int, error) { - return w.Write([]byte(data)) -} - -func (w *markupCutWriter) Write(data []byte) (int, error) { - n := min(w.remaining, len(data)) - w.remaining -= n - _, _ = w.Buffer.Write(data[:n]) - if n != len(data) { - return n, io.ErrClosedPipe - } - return n, nil -} - func TestMarkupSerializationDestinationErrors(t *testing.T) { e := engine(t, transform.Config{}) for _, test := range slices.Concat(markupSerializationCases(), jsonCharacterExpansionMarkupCases()) { @@ -808,10 +789,6 @@ func TestSerializationRawHTMLDescendants(t *testing.T) { } } -func fullyNormalizedSerializationSource(version string, test fullyNormalizedSerializationCase) string { - return sheetStart + version + `" exclude-result-prefixes="xs">` + test.body + `` -} - func TestSerializationFullyNormalizedCharacterMaps(t *testing.T) { for _, processor := range []transform.XSLTVersion{transform.XSLT20, transform.XSLT30} { version := version30 @@ -979,17 +956,6 @@ func TestJSONSerializationRules(t *testing.T) { } } -func assertSerializationSequence(t *testing.T, path string, value transform.Sequence, err error, want, code string) { - t.Helper() - - got := "" - if err == nil { - got = text(t, value) - } - - assertSerializationResult(t, path, got, err, want, code) -} - func TestSerializationEscapeBytes(t *testing.T) { e := engine(t, transform.Config{}) input := "\x00\x01\x1f\"\\/\b\f\n\r\t\x7f\u009fé😀" @@ -1028,30 +994,6 @@ func TestSerializationEscapeBytes(t *testing.T) { } } -func assertSerializationOutput(t *testing.T, path string, output *transform.Output, err error, want, code string) { - t.Helper() - - got := "" - if output != nil { - got = string(output.Primary) - } - - assertSerializationResult(t, path, got, err, want, code) -} - -func assertSerializationResult(t *testing.T, path, got string, err error, want, code string) { - t.Helper() - - if code != "" { - var detail *transform.Error - if !errors.As(err, &detail) || detail.Code != code { - t.Errorf("%s: %q, %v; want %s", path, got, err, code) - } - } else if err != nil || got != want { - t.Errorf("%s: %q, %v; want %q", path, got, err, want) - } -} - // TestSerializeParameterDocuments keeps argument type errors separate from // parameter extraction, duplicate parameters and duplicate character mappings. func TestSerializeParameterDocuments(t *testing.T) { @@ -1492,60 +1434,6 @@ func TestJSONHTMLHeadMetadata(t *testing.T) { } } -type markupSerializationErrorCase struct { - name string - attributes string - expression string - options string - code string -} - -func markupSerializationErrorCases() []markupSerializationErrorCase { - cases := make([]markupSerializationErrorCase, 0, 42) - cases = append(cases, []markupSerializationErrorCase{ - {"XML standalone attribute", `method="xml"`, `parse-xml('')/r/@a`, `map{'method':'xml'}`, "SENR0001"}, - {"XHTML standalone attribute", `method="xhtml"`, `parse-xml('')/r/@a`, `map{'method':'xhtml'}`, "SENR0001"}, - - {"XML unsupported valid VersionNum", `method="xml" version="1.2"`, `parse-xml('')`, `map{'method':'xml','version':'1.2'}`, "SESU0013"}, - {"XHTML unsupported valid VersionNum", `method="xhtml" version="1.2"`, `parse-xml('')`, `map{'method':'xhtml','version':'1.2'}`, "SESU0013"}, - - {"XML unsupported output version", `method="xml" version="0.0"`, `parse-xml('')`, `map{'method':'xml','version':'0.0'}`, "SESU0013"}, - {"XHTML unsupported output version", `method="xhtml" version="0.0"`, `parse-xml('')`, `map{'method':'xhtml','version':'0.0'}`, "SESU0013"}, - {"XML standalone declaration conflict", `method="xml" standalone="yes" omit-xml-declaration="yes"`, `parse-xml('')`, `map{'method':'xml','standalone':true(),'omit-xml-declaration':true()}`, "SEPM0009"}, - {"XHTML standalone declaration conflict", `method="xhtml" standalone="yes" omit-xml-declaration="yes"`, `parse-xml('')`, `map{'method':'xhtml','standalone':true(),'omit-xml-declaration':true()}`, "SEPM0009"}, - - {"HTML PI", `method="html"`, `parse-xml('b?>')/r/processing-instruction()`, `map{'method':'html'}`, "SERE0015"}, - {"HTML nested PI", `method="html"`, `parse-xml('b?>')/r`, `map{'method':'html'}`, "SERE0015"}, - {"HTML4 control", `method="html" html-version="4.01"`, `codepoints-to-string(127)`, `map{'method':'html','html-version':4.01}`, "SERE0014"}, - {"HTML4 NEL", `method="html" html-version="4.01"`, `codepoints-to-string(133)`, `map{'method':'html','html-version':4.01}`, "SERE0014"}, - {"HTML4 attribute", `method="html" html-version="4.01"`, `parse-xml('')/r`, `map{'method':'html','html-version':4.01}`, "SERE0014"}, - {"HTML4 version parameter", `method="html" version="4.01"`, `codepoints-to-string(127)`, ``, "SERE0014"}, - {"HTML4 separator", `method="html" html-version="4.01" item-separator=""`, `('a','b')`, `map{'method':'html','html-version':4.01,'item-separator':codepoints-to-string(127)}`, "SERE0014"}, - {"unavailable HTML version", `method="html" version="0.0"`, `'text'`, ``, "SESU0013"}, - {"XML default prefix undeclaration", `method="xml" undeclare-prefixes="yes"`, `parse-xml('')`, `map{'method':'xml','undeclare-prefixes':true()}`, "SEPM0010"}, - {"unsupported HTML6", `method="html" html-version="6"`, `'text'`, `map{'method':'html','html-version':6}`, "SESU0013"}, - {"XML ASCII name", `method="xml" encoding="US-ASCII" omit-xml-declaration="yes"`, `parse-xml('<é/>')`, `map{'method':'xml','encoding':'US-ASCII'}`, "SERE0008"}, - {"XML ASCII comment", `method="xml" encoding="US-ASCII" omit-xml-declaration="yes"`, `parse-xml('')/comment()`, `map{'method':'xml','encoding':'US-ASCII'}`, "SERE0008"}, - {"XML ASCII PI", `method="xml" encoding="US-ASCII" omit-xml-declaration="yes"`, `parse-xml('')/processing-instruction()`, `map{'method':'xml','encoding':'US-ASCII'}`, "SERE0008"}, - {"XHTML ASCII name", `method="xhtml" encoding="US-ASCII" omit-xml-declaration="yes"`, `parse-xml('<é/>')`, `map{'method':'xhtml','encoding':'US-ASCII'}`, "SERE0008"}, - {"XHTML ASCII comment", `method="xhtml" encoding="US-ASCII" omit-xml-declaration="yes"`, `parse-xml('')/comment()`, `map{'method':'xhtml','encoding':'US-ASCII'}`, "SERE0008"}, - {"XHTML ASCII PI", `method="xhtml" encoding="US-ASCII" omit-xml-declaration="yes"`, `parse-xml('')/processing-instruction()`, `map{'method':'xhtml','encoding':'US-ASCII'}`, "SERE0008"}, - {"HTML ASCII island name", `method="html" encoding="US-ASCII"`, `parse-xml('')`, `map{'method':'html','encoding':'US-ASCII'}`, "SERE0008"}, - {"HTML ASCII attribute name", `method="html" encoding="US-ASCII"`, `parse-xml('')`, `map{'method':'html','encoding':'US-ASCII'}`, "SERE0008"}, - {"HTML detached island PI", `method="html"`, `parse-xml('b?>')/*/processing-instruction()`, `map{'method':'html'}`, "SERE0015"}, - {"XML doctype fragment shape", `method="xml" omit-xml-declaration="yes" doctype-system="d.dtd"`, `(parse-xml(''),parse-xml(''))`, `map{'method':'xml','doctype-system':'d.dtd'}`, "SEPM0004"}, - {"XHTML doctype fragment shape", `method="xhtml" omit-xml-declaration="yes" doctype-system="d.dtd"`, `(parse-xml(''),parse-xml(''))`, `map{'method':'xhtml','doctype-system':'d.dtd'}`, "SEPM0004"}, - {"XML composing start", `method="xml" normalization-form="fully-normalized" omit-xml-declaration="yes"`, `codepoints-to-string(769)`, ``, "SERE0012"}, - }...) - for _, method := range []string{"xml", "xhtml", "html", "text", "json", "adaptive"} { - cases = append(cases, - markupSerializationErrorCase{method + " invalid public identifier", `method="` + method + `" doctype-public="é"`, `parse-xml('x')`, `map{'method':'` + method + `','doctype-public':'é'}`, "SEPM0016"}, - markupSerializationErrorCase{method + " invalid system identifier", `method="` + method + `" doctype-system=""'"`, `parse-xml('x')`, `map{'method':'` + method + `','doctype-system':concat(codepoints-to-string(34),codepoints-to-string(39))}`, "SEPM0016"}, - ) - } - return cases -} - func TestMarkupSerializationContentErrors(t *testing.T) { e := engine(t, transform.Config{}) for _, test := range markupSerializationErrorCases() { @@ -2397,313 +2285,81 @@ func TestSerializationIgnoredParameters(t *testing.T) { } } -// selectedSerializationVector passes the same already constructed XDM value -// through each selected host API. SER30 uses its required parameter document, -// rather than silently exercising the newer map-option signature. -type serializationMetadataNode struct { - XMLName xml.Name - Attributes []xml.Attr `xml:",any,attr"` - Children []serializationMetadataNode `xml:",any"` -} - -func serializationMetadataAttribute(node serializationMetadataNode, name string) string { - for _, attribute := range node.Attributes { - if attribute.Name.Space == "" && attribute.Name.Local == name { - return attribute.Value +func TestSerializationSelectedEditionVectors(t *testing.T) { + e := engine(t, transform.Config{}) + for _, edition := range []string{"20", "30", "31"} { + for _, test := range markupSerializationCases() { + if !serializationVectorAdmitted(edition, test) { + continue + } + t.Run("SER"+edition+"/"+test.name, func(t *testing.T) { + constructor := stylesheet(t, e, markupSerializationSheet(markupSerializationCase{body: test.body}, false)) + result, err := constructor.Execute(t.Context(), nil, transform.XSLTContext{Template: transform.Name{Local: nameStart}, ResultMode: transform.ResultRaw}) + if err != nil { + t.Fatal(err) + } + got, err := selectedSerializationVector(t, edition, test, result.Primary()) + if err != nil { + t.Fatal(err) + } + want := test.want + if edition == "30" { + want = strings.TrimPrefix(want, "\uFEFF") + } + if got != want { + if strings.HasPrefix(test.want, "\uFEFF") { + t.Fatal("selected BOM contract", got, want) + } + check, err := query(t, e, markupSerializationCheck(test), transform.XPath31).Evaluate(t.Context(), nil, transform.XPathContext{Variables: transform.Bindings{{Local: "s"}: transform.Values(transform.String(got))}}) + if err != nil || text(t, check) != nameTrue { + t.Fatal("selected serialization vector", got, "want", test.want, "permitted-output check", check, err) + } + } + publishedProofVector(t, want) + if edition == "31" { + publishedProofVector(t, strings.TrimPrefix(want, "\uFEFF")) + } + }) } } - return "" } -func serializationParameterDocumentFromOutput(t *testing.T, declarations string) string { - t.Helper() - var root serializationMetadataNode - if err := xml.Unmarshal([]byte(``+declarations+``), &root); err != nil { - t.Fatal(err) - } - values := map[string]string{} - prefixes := map[string]string{} - characterMaps := map[string]map[string]string{} - for _, node := range root.Children { - if node.XMLName.Local != "character-map" { - continue - } - characters := map[string]string{} - for _, entry := range node.Children { - if entry.XMLName.Local == "output-character" { - characters[serializationMetadataAttribute(entry, "character")] = serializationMetadataAttribute(entry, "string") - } - } - characterMaps[serializationMetadataAttribute(node, "name")] = characters - } - for _, node := range root.Children { - if node.XMLName.Local != "output" { - continue - } - namespaces := map[string]string{} - for _, attribute := range node.Attributes { - if attribute.Name.Space == "xmlns" { - namespaces[attribute.Name.Local] = attribute.Value - } else if attribute.Name.Space == "" && attribute.Name.Local == "xmlns" { - namespaces[""] = attribute.Value +func TestSerializationSelectedEditionErrors(t *testing.T) { + e := engine(t, transform.Config{}) + for _, edition := range []string{"20", "30", "31"} { + for _, test := range markupSerializationErrorCases() { + if strings.Contains(test.attributes, `method="json"`) || strings.Contains(test.attributes, `method="adaptive"`) { + continue } - } - for _, attribute := range node.Attributes { - if attribute.Name.Space != "" || attribute.Name.Local == "xmlns" || attribute.Name.Local == "name" { + if edition == "20" && (strings.Contains(test.attributes, "html-version=") || strings.Contains(test.attributes, "item-separator=")) { continue } - value := attribute.Value - if attribute.Name.Local == "cdata-section-elements" || attribute.Name.Local == "suppress-indentation" { - fields := strings.Fields(value) - names := make([]string, 0, len(fields)) - for _, name := range fields { - prefix, local, qualified := strings.Cut(name, ":") - if !qualified { - local, prefix = prefix, "" + t.Run("SER"+edition+"/"+test.name, func(t *testing.T) { + input, err := query(t, e, test.expression, transform.XPath31).Evaluate(t.Context(), nil, transform.XPathContext{}) + if err != nil { + t.Fatal(err) + } + vector := markupSerializationCase{name: test.name, declarations: ``} + got, err := selectedSerializationVector(t, edition, vector, input) + var detail *transform.Error + want := test.code + if edition == "30" { + // The published node-parameter schema rejects these values + // before serialization; the map/stylesheet paths differ. + if strings.Contains(test.name, "invalid public identifier") || strings.Contains(test.name, "invalid system identifier") { + want = "SEPM0017" } - uri := namespaces[prefix] - if uri != "" { - stable := "p" + strconv.Itoa(len(prefixes)) - prefixes[stable] = uri - local = stable + ":" + local + // fn:serialize admits four Unicode normalization forms; + // fully-normalized is an optional XSLT-host capability. + if test.name == "XML composing start" { + want = "SESU0011" } - names = append(names, local) } - value = strings.TrimSpace(values[attribute.Name.Local] + " " + strings.Join(names, " ")) - } - values[attribute.Name.Local] = value - } - } - if values["method"] == "" { - values["method"] = "xml" - } - if _, present := values["omit-xml-declaration"]; !present && (values["method"] == "xml" || values["method"] == "xhtml") { - values["omit-xml-declaration"] = "no" - } - var document strings.Builder - document.WriteString(``) - keys = keys[:0] - for name := range values { - keys = append(keys, name) - } - slices.Sort(keys) - for _, name := range keys { - if name == "use-character-maps" { - characters := map[string]string{} - for mapName := range strings.FieldsSeq(values[name]) { - mapping, found := characterMaps[mapName] - if !found { - t.Fatal("missing literal character map", mapName) - } - maps.Copy(characters, mapping) - } - document.WriteString(``) - chars := make([]string, 0, len(characters)) - for character := range characters { - chars = append(chars, character) - } - slices.Sort(chars) - for _, character := range chars { - document.WriteString(``) - } - document.WriteString(``) - continue - } - document.WriteString(``) - } - document.WriteString(``) - return document.String() -} - -func selectedSerializationXPath(t *testing.T, e *transform.Engine, version transform.XPathVersion, test markupSerializationCase, input transform.Sequence) (string, error) { - t.Helper() - parameters := serializationParameterDocumentFromOutput(t, test.declarations) - value, err := query(t, e, `serialize($input,parse-xml($parameters)/*)`, version).Evaluate(t.Context(), nil, transform.XPathContext{Variables: transform.Bindings{{Local: "input"}: input, {Local: "parameters"}: transform.Values(transform.String(parameters))}}) - if err != nil { - return "", err - } - return text(t, value), nil -} - -func selectedSerializationXPathError(t *testing.T, test markupSerializationCase, hostErr, fnErr error) { - t.Helper() - var host, fn *transform.Error - if !errors.As(hostErr, &host) || !errors.As(fnErr, &fn) { - t.Fatal("selected host/parameter-document errors", hostErr, fnErr) - } - expected := host.Code - // Published parameter documents validate lexical domains before extraction; - // fn:serialize only admits its four Unicode normalization forms. - if strings.Contains(test.name, "invalid public identifier") || strings.Contains(test.name, "invalid system identifier") { - expected = "SEPM0017" - } - if test.name == "XML composing start" { - expected = "SESU0011" - } - if fn.Code != expected { - t.Fatal("selected XPath31 parameter-document contract", fnErr, expected) - } - publishedProofVector(t, expected) -} - -func selectedSerializationVector(t *testing.T, edition string, test markupSerializationCase, input transform.Sequence) (string, error) { - t.Helper() - e := engine(t, transform.Config{}) - if edition == "30" { - return selectedSerializationXPath(t, e, transform.XPath30, test, input) - } - version, processor := version30, transform.XSLT30 - if edition == "20" { - version, processor = version20, transform.XSLT20 - } - source := `` + test.declarations + `` - compiled, err := e.CompileXSLT(t.Context(), []byte(source), transform.XSLTOptions{ProcessorVersion: processor}) - if err != nil { - return "", err - } - result, err := compiled.Execute(t.Context(), nil, transform.XSLTContext{Template: transform.Name{Local: nameStart}, ResultMode: transform.ResultRaw, Parameters: transform.Bindings{{Local: "input"}: input}}) - if err != nil { - return "", err - } - output, err := result.Serialize(t.Context()) - if edition == "31" && test.name != "HTML auto method version" && test.name != "HTML empty output declaration" { - value, fnErr := selectedSerializationXPath(t, e, transform.XPath31, test, input) - if err != nil { - selectedSerializationXPathError(t, test, err, fnErr) - return "", err - } - expected := strings.TrimPrefix(string(output.Primary), "\uFEFF") - // HTML permits whitespace between the generated DOCTYPE and its root. - // Compare only that specific lexical freedom; content whitespace is exact. - matchedValue := value - if strings.Contains(expected, "<") { - matchedValue = strings.Replace(matchedValue, "\n<", "<", 1) - } - if fnErr != nil || matchedValue != expected { - t.Fatal("selected XPath31/XSLT30 host contract", value, fnErr, expected) - } - } - if err != nil { - return "", err - } - return string(output.Primary), nil -} - -func serializationVectorAdmitted(edition string, test markupSerializationCase) bool { - if strings.Contains(test.body, "disable-output-escaping") || strings.Contains(test.declarations, "Q{") { - return false - } - // fn:serialize has an explicit default method, whereas these two vectors - // test the XSLT host's automatic method selection from its result element. - if edition == "30" && (test.name == "HTML auto method version" || test.name == "HTML empty output declaration") { - return false - } - if edition == "20" && (strings.Contains(test.declarations, "html-version=") || strings.Contains(test.declarations, "item-separator=") || strings.Contains(test.declarations, "suppress-indentation=")) { - return false - } - return true -} - -func TestSerializationSelectedEditionVectors(t *testing.T) { - e := engine(t, transform.Config{}) - for _, edition := range []string{"20", "30", "31"} { - for _, test := range markupSerializationCases() { - if !serializationVectorAdmitted(edition, test) { - continue - } - t.Run("SER"+edition+"/"+test.name, func(t *testing.T) { - constructor := stylesheet(t, e, markupSerializationSheet(markupSerializationCase{body: test.body}, false)) - result, err := constructor.Execute(t.Context(), nil, transform.XSLTContext{Template: transform.Name{Local: nameStart}, ResultMode: transform.ResultRaw}) - if err != nil { - t.Fatal(err) - } - got, err := selectedSerializationVector(t, edition, test, result.Primary()) - if err != nil { - t.Fatal(err) - } - want := test.want - if edition == "30" { - want = strings.TrimPrefix(want, "\uFEFF") - } - if got != want { - if strings.HasPrefix(test.want, "\uFEFF") { - t.Fatal("selected BOM contract", got, want) - } - check, err := query(t, e, markupSerializationCheck(test), transform.XPath31).Evaluate(t.Context(), nil, transform.XPathContext{Variables: transform.Bindings{{Local: "s"}: transform.Values(transform.String(got))}}) - if err != nil || text(t, check) != nameTrue { - t.Fatal("selected serialization vector", got, "want", test.want, "permitted-output check", check, err) - } - } - publishedProofVector(t, want) - if edition == "31" { - publishedProofVector(t, strings.TrimPrefix(want, "\uFEFF")) - } - }) - } - } -} - -func TestSerializationSelectedEditionErrors(t *testing.T) { - e := engine(t, transform.Config{}) - for _, edition := range []string{"20", "30", "31"} { - for _, test := range markupSerializationErrorCases() { - if strings.Contains(test.attributes, `method="json"`) || strings.Contains(test.attributes, `method="adaptive"`) { - continue - } - if edition == "20" && (strings.Contains(test.attributes, "html-version=") || strings.Contains(test.attributes, "item-separator=")) { - continue - } - t.Run("SER"+edition+"/"+test.name, func(t *testing.T) { - input, err := query(t, e, test.expression, transform.XPath31).Evaluate(t.Context(), nil, transform.XPathContext{}) - if err != nil { - t.Fatal(err) - } - vector := markupSerializationCase{name: test.name, declarations: ``} - got, err := selectedSerializationVector(t, edition, vector, input) - var detail *transform.Error - want := test.code - if edition == "30" { - // The published node-parameter schema rejects these values - // before serialization; the map/stylesheet paths differ. - if strings.Contains(test.name, "invalid public identifier") || strings.Contains(test.name, "invalid system identifier") { - want = "SEPM0017" - } - // fn:serialize admits four Unicode normalization forms; - // fully-normalized is an optional XSLT-host capability. - if test.name == "XML composing start" { - want = "SESU0011" - } - } - if !errors.As(err, &detail) || detail.Code != want { - t.Fatal(got, err, "want", want) - } - publishedProofVector(t, want) - }) + if !errors.As(err, &detail) || detail.Code != want { + t.Fatal(got, err, "want", want) + } + publishedProofVector(t, want) + }) } } } @@ -3239,7 +2895,7 @@ func namedOutputCharacterMapCases() []struct { attributes string want string } { - const maps = `` + const ms = `` const format = `` + format + ` use-character-maps="b"/>` + maps, "", `

B

`}, - {"empty named map excludes default map", `` + format + `/>` + maps, "", `

x

`}, - {"local map overrides named map", `` + format + ` use-character-maps="b"/>` + maps, `use-character-maps="c"`, `

C

`}, - {"merged named map order", `` + format + ` use-character-maps="b"/>` + maps, "", `

B

`}, + {"named map overrides unnamed output", `` + format + ` use-character-maps="b"/>` + ms, "", `

B

`}, + {"empty named map excludes default map", `` + format + `/>` + ms, "", `

x

`}, + {"local map overrides named map", `` + format + ` use-character-maps="b"/>` + ms, `use-character-maps="c"`, `

C

`}, + {"merged named map order", `` + format + ` use-character-maps="b"/>` + ms, "", `

B

`}, } } @@ -3284,64 +2940,6 @@ func BenchmarkSpecialSerialization(b *testing.B) { benchmarkSpecialSerialization func BenchmarkSpecialSerializationCollect(b *testing.B) { benchmarkSpecialSerialization(b, true) } -func benchmarkSpecialSerialization(b *testing.B, collect bool) { - for _, tc := range []struct { - name string - method string - body string - extra string - }{ - {"JSONNode", "json", ``, ""}, - {"XHTML", "xhtml", ``, ` include-content-type="no"`}, - {"XHTMLManyNodes", "xhtml", ``, ` include-content-type="no"`}, - {"JSONMapNorm", "json", ``, ` normalization-form="NFC" use-character-maps="m"`}, - {"Adaptive", "adaptive", ``, ""}, - {"AdaptiveASCII", "adaptive", ``, ""}, - {"TextASCIINorm", "text", ``, ` normalization-form="NFC"`}, - {"AdaptiveMapNorm", "adaptive", ``, ` normalization-form="NFC" use-character-maps="m"`}, - } { - for _, size := range []int{64, 1 << 20} { - b.Run(tc.name+"/"+strconv.Itoa(size), func(b *testing.B) { - e := engine(b, transform.Config{}) - payload := strings.Repeat(`hello "é é | `, max(1, size/16)) - if tc.name == "AdaptiveASCII" || tc.name == "TextASCIINorm" { - payload = strings.Repeat("hello world ", max(1, size/12)) - } - if tc.name == "XHTMLManyNodes" { - payload = strings.Repeat(`

hello é

`, max(1, size/128)) - } - input := `` + payload + `` - d, err := e.Parse(b.Context(), []byte(input)) - if err != nil { - b.Fatal(err) - } - source := `` + tc.body + `` - s, err := e.CompileXSLT(b.Context(), []byte(source), transform.XSLTOptions{}) - if err != nil { - b.Fatal(err) - } - result, err := s.Execute(b.Context(), d, transform.XSLTContext{}) - if err != nil { - b.Fatal(err) - } - b.ReportAllocs() - b.SetBytes(int64(len(input))) - for b.Loop() { - if collect { - if _, err := result.Serialize(b.Context()); err != nil { - b.Fatal(err) - } - continue - } - if _, err := result.WriterTo(b.Context()).WriteTo(io.Discard); err != nil { - b.Fatal(err) - } - } - }) - } - } -} - func TestAdaptiveLargeWriterOwnership(t *testing.T) { e := engine(t, transform.Config{}) style := stylesheet(t, e, sheetStart+version30+`">`) @@ -3410,3 +3008,638 @@ func TestXHTMLWriterPreservesResultNodes(t *testing.T) { t.Fatalf("XHTML serialization mutated reusable result: %q -> %q", before, after) } } + +type markupCutWriter struct { + bytes.Buffer + remaining int +} + +func (w *markupCutWriter) WriteString(data string) (int, error) { + return w.Write([]byte(data)) +} + +func (w *markupCutWriter) Write(data []byte) (int, error) { + n := min(w.remaining, len(data)) + w.remaining -= n + _, _ = w.Buffer.Write(data[:n]) + if n != len(data) { + return n, io.ErrClosedPipe + } + return n, nil +} + +func fullyNormalizedSerializationSource(version string, test fullyNormalizedSerializationCase) string { + return sheetStart + version + `" exclude-result-prefixes="xs">` + test.body + `` +} + +func assertSerializationSequence(t *testing.T, path string, value transform.Sequence, err error, want, code string) { + t.Helper() + + got := "" + if err == nil { + got = text(t, value) + } + + assertSerializationResult(t, path, got, err, want, code) +} + +func assertSerializationOutput(t *testing.T, path string, output *transform.Output, err error, want, code string) { + t.Helper() + + got := "" + if output != nil { + got = string(output.Primary) + } + + assertSerializationResult(t, path, got, err, want, code) +} + +func assertSerializationResult(t *testing.T, path, got string, err error, want, code string) { + t.Helper() + + if code != "" { + var detail *transform.Error + if !errors.As(err, &detail) || detail.Code != code { + t.Errorf("%s: %q, %v; want %s", path, got, err, code) + } + } else if err != nil || got != want { + t.Errorf("%s: %q, %v; want %q", path, got, err, want) + } +} + +type markupSerializationErrorCase struct { + name string + attributes string + expression string + options string + code string +} + +func markupSerializationErrorCases() []markupSerializationErrorCase { + cases := make([]markupSerializationErrorCase, 0, 42) + cases = append(cases, []markupSerializationErrorCase{ + {"XML standalone attribute", `method="xml"`, `parse-xml('')/r/@a`, `map{'method':'xml'}`, "SENR0001"}, + {"XHTML standalone attribute", `method="xhtml"`, `parse-xml('')/r/@a`, `map{'method':'xhtml'}`, "SENR0001"}, + + {"XML unsupported valid VersionNum", `method="xml" version="1.2"`, `parse-xml('')`, `map{'method':'xml','version':'1.2'}`, "SESU0013"}, + {"XHTML unsupported valid VersionNum", `method="xhtml" version="1.2"`, `parse-xml('')`, `map{'method':'xhtml','version':'1.2'}`, "SESU0013"}, + + {"XML unsupported output version", `method="xml" version="0.0"`, `parse-xml('')`, `map{'method':'xml','version':'0.0'}`, "SESU0013"}, + {"XHTML unsupported output version", `method="xhtml" version="0.0"`, `parse-xml('')`, `map{'method':'xhtml','version':'0.0'}`, "SESU0013"}, + {"XML standalone declaration conflict", `method="xml" standalone="yes" omit-xml-declaration="yes"`, `parse-xml('')`, `map{'method':'xml','standalone':true(),'omit-xml-declaration':true()}`, "SEPM0009"}, + {"XHTML standalone declaration conflict", `method="xhtml" standalone="yes" omit-xml-declaration="yes"`, `parse-xml('')`, `map{'method':'xhtml','standalone':true(),'omit-xml-declaration':true()}`, "SEPM0009"}, + + {"HTML PI", `method="html"`, `parse-xml('b?>')/r/processing-instruction()`, `map{'method':'html'}`, "SERE0015"}, + {"HTML nested PI", `method="html"`, `parse-xml('b?>')/r`, `map{'method':'html'}`, "SERE0015"}, + {"HTML4 control", `method="html" html-version="4.01"`, `codepoints-to-string(127)`, `map{'method':'html','html-version':4.01}`, "SERE0014"}, + {"HTML4 NEL", `method="html" html-version="4.01"`, `codepoints-to-string(133)`, `map{'method':'html','html-version':4.01}`, "SERE0014"}, + {"HTML4 attribute", `method="html" html-version="4.01"`, `parse-xml('')/r`, `map{'method':'html','html-version':4.01}`, "SERE0014"}, + {"HTML4 version parameter", `method="html" version="4.01"`, `codepoints-to-string(127)`, ``, "SERE0014"}, + {"HTML4 separator", `method="html" html-version="4.01" item-separator=""`, `('a','b')`, `map{'method':'html','html-version':4.01,'item-separator':codepoints-to-string(127)}`, "SERE0014"}, + {"unavailable HTML version", `method="html" version="0.0"`, `'text'`, ``, "SESU0013"}, + {"XML default prefix undeclaration", `method="xml" undeclare-prefixes="yes"`, `parse-xml('')`, `map{'method':'xml','undeclare-prefixes':true()}`, "SEPM0010"}, + {"unsupported HTML6", `method="html" html-version="6"`, `'text'`, `map{'method':'html','html-version':6}`, "SESU0013"}, + {"XML ASCII name", `method="xml" encoding="US-ASCII" omit-xml-declaration="yes"`, `parse-xml('<é/>')`, `map{'method':'xml','encoding':'US-ASCII'}`, "SERE0008"}, + {"XML ASCII comment", `method="xml" encoding="US-ASCII" omit-xml-declaration="yes"`, `parse-xml('')/comment()`, `map{'method':'xml','encoding':'US-ASCII'}`, "SERE0008"}, + {"XML ASCII PI", `method="xml" encoding="US-ASCII" omit-xml-declaration="yes"`, `parse-xml('')/processing-instruction()`, `map{'method':'xml','encoding':'US-ASCII'}`, "SERE0008"}, + {"XHTML ASCII name", `method="xhtml" encoding="US-ASCII" omit-xml-declaration="yes"`, `parse-xml('<é/>')`, `map{'method':'xhtml','encoding':'US-ASCII'}`, "SERE0008"}, + {"XHTML ASCII comment", `method="xhtml" encoding="US-ASCII" omit-xml-declaration="yes"`, `parse-xml('')/comment()`, `map{'method':'xhtml','encoding':'US-ASCII'}`, "SERE0008"}, + {"XHTML ASCII PI", `method="xhtml" encoding="US-ASCII" omit-xml-declaration="yes"`, `parse-xml('')/processing-instruction()`, `map{'method':'xhtml','encoding':'US-ASCII'}`, "SERE0008"}, + {"HTML ASCII island name", `method="html" encoding="US-ASCII"`, `parse-xml('')`, `map{'method':'html','encoding':'US-ASCII'}`, "SERE0008"}, + {"HTML ASCII attribute name", `method="html" encoding="US-ASCII"`, `parse-xml('')`, `map{'method':'html','encoding':'US-ASCII'}`, "SERE0008"}, + {"HTML detached island PI", `method="html"`, `parse-xml('b?>')/*/processing-instruction()`, `map{'method':'html'}`, "SERE0015"}, + {"XML doctype fragment shape", `method="xml" omit-xml-declaration="yes" doctype-system="d.dtd"`, `(parse-xml('
'),parse-xml(''))`, `map{'method':'xml','doctype-system':'d.dtd'}`, "SEPM0004"}, + {"XHTML doctype fragment shape", `method="xhtml" omit-xml-declaration="yes" doctype-system="d.dtd"`, `(parse-xml(''),parse-xml(''))`, `map{'method':'xhtml','doctype-system':'d.dtd'}`, "SEPM0004"}, + {"XML composing start", `method="xml" normalization-form="fully-normalized" omit-xml-declaration="yes"`, `codepoints-to-string(769)`, ``, "SERE0012"}, + }...) + for _, method := range []string{"xml", "xhtml", "html", "text", "json", "adaptive"} { + cases = append(cases, + markupSerializationErrorCase{method + " invalid public identifier", `method="` + method + `" doctype-public="é"`, `parse-xml('x')`, `map{'method':'` + method + `','doctype-public':'é'}`, "SEPM0016"}, + markupSerializationErrorCase{method + " invalid system identifier", `method="` + method + `" doctype-system=""'"`, `parse-xml('x')`, `map{'method':'` + method + `','doctype-system':concat(codepoints-to-string(34),codepoints-to-string(39))}`, "SEPM0016"}, + ) + } + return cases +} + +// selectedSerializationVector passes the same already constructed XDM value +// through each selected host API. SER30 uses its required parameter document, +// rather than silently exercising the newer map-option signature. +type serializationMetadataNode struct { + XMLName xml.Name + Attributes []xml.Attr `xml:",any,attr"` + Children []serializationMetadataNode `xml:",any"` +} + +func serializationMetadataAttribute(node serializationMetadataNode, name string) string { + for _, attribute := range node.Attributes { + if attribute.Name.Space == "" && attribute.Name.Local == name { + return attribute.Value + } + } + return "" +} + +func serializationParameterDocumentFromOutput(t *testing.T, declarations string) string { + t.Helper() + var root serializationMetadataNode + if err := xml.Unmarshal([]byte(``+declarations+``), &root); err != nil { + t.Fatal(err) + } + values := map[string]string{} + prefixes := map[string]string{} + characterMaps := map[string]map[string]string{} + for _, node := range root.Children { + if node.XMLName.Local != "character-map" { + continue + } + characters := map[string]string{} + for _, entry := range node.Children { + if entry.XMLName.Local == "output-character" { + characters[serializationMetadataAttribute(entry, "character")] = serializationMetadataAttribute(entry, "string") + } + } + characterMaps[serializationMetadataAttribute(node, "name")] = characters + } + for _, node := range root.Children { + if node.XMLName.Local != "output" { + continue + } + namespaces := map[string]string{} + for _, attribute := range node.Attributes { + if attribute.Name.Space == "xmlns" { + namespaces[attribute.Name.Local] = attribute.Value + } else if attribute.Name.Space == "" && attribute.Name.Local == "xmlns" { + namespaces[""] = attribute.Value + } + } + for _, attribute := range node.Attributes { + if attribute.Name.Space != "" || attribute.Name.Local == "xmlns" || attribute.Name.Local == "name" { + continue + } + value := attribute.Value + if attribute.Name.Local == "cdata-section-elements" || attribute.Name.Local == "suppress-indentation" { + fields := strings.Fields(value) + names := make([]string, 0, len(fields)) + for _, name := range fields { + prefix, local, qualified := strings.Cut(name, ":") + if !qualified { + local, prefix = prefix, "" + } + uri := namespaces[prefix] + if uri != "" { + stable := "p" + strconv.Itoa(len(prefixes)) + prefixes[stable] = uri + local = stable + ":" + local + } + names = append(names, local) + } + value = strings.TrimSpace(values[attribute.Name.Local] + " " + strings.Join(names, " ")) + } + values[attribute.Name.Local] = value + } + } + if values["method"] == "" { + values["method"] = "xml" + } + if _, present := values["omit-xml-declaration"]; !present && (values["method"] == "xml" || values["method"] == "xhtml") { + values["omit-xml-declaration"] = "no" + } + var document strings.Builder + document.WriteString(``) + keys = keys[:0] + for name := range values { + keys = append(keys, name) + } + slices.Sort(keys) + for _, name := range keys { + if name == "use-character-maps" { + characters := map[string]string{} + for mapName := range strings.FieldsSeq(values[name]) { + mapping, found := characterMaps[mapName] + if !found { + t.Fatal("missing literal character map", mapName) + } + maps.Copy(characters, mapping) + } + document.WriteString(``) + chars := make([]string, 0, len(characters)) + for character := range characters { + chars = append(chars, character) + } + slices.Sort(chars) + for _, character := range chars { + document.WriteString(``) + } + document.WriteString(``) + continue + } + document.WriteString(``) + } + document.WriteString(``) + return document.String() +} + +func selectedSerializationXPath(t *testing.T, e *transform.Engine, version transform.XPathVersion, test markupSerializationCase, input transform.Sequence) (string, error) { + t.Helper() + parameters := serializationParameterDocumentFromOutput(t, test.declarations) + value, err := query(t, e, `serialize($input,parse-xml($parameters)/*)`, version).Evaluate(t.Context(), nil, transform.XPathContext{Variables: transform.Bindings{{Local: "input"}: input, {Local: "parameters"}: transform.Values(transform.String(parameters))}}) + if err != nil { + return "", err + } + return text(t, value), nil +} + +func selectedSerializationXPathError(t *testing.T, test markupSerializationCase, hostErr, fnErr error) { + t.Helper() + var host, fn *transform.Error + if !errors.As(hostErr, &host) || !errors.As(fnErr, &fn) { + t.Fatal("selected host/parameter-document errors", hostErr, fnErr) + } + expected := host.Code + // Published parameter documents validate lexical domains before extraction; + // fn:serialize only admits its four Unicode normalization forms. + if strings.Contains(test.name, "invalid public identifier") || strings.Contains(test.name, "invalid system identifier") { + expected = "SEPM0017" + } + if test.name == "XML composing start" { + expected = "SESU0011" + } + if fn.Code != expected { + t.Fatal("selected XPath31 parameter-document contract", fnErr, expected) + } + publishedProofVector(t, expected) +} + +func selectedSerializationVector(t *testing.T, edition string, test markupSerializationCase, input transform.Sequence) (string, error) { + t.Helper() + e := engine(t, transform.Config{}) + if edition == "30" { + return selectedSerializationXPath(t, e, transform.XPath30, test, input) + } + version, processor := version30, transform.XSLT30 + if edition == "20" { + version, processor = version20, transform.XSLT20 + } + source := `` + test.declarations + `` + compiled, err := e.CompileXSLT(t.Context(), []byte(source), transform.XSLTOptions{ProcessorVersion: processor}) + if err != nil { + return "", err + } + result, err := compiled.Execute(t.Context(), nil, transform.XSLTContext{Template: transform.Name{Local: nameStart}, ResultMode: transform.ResultRaw, Parameters: transform.Bindings{{Local: "input"}: input}}) + if err != nil { + return "", err + } + output, err := result.Serialize(t.Context()) + if edition == "31" && test.name != "HTML auto method version" && test.name != "HTML empty output declaration" { + value, fnErr := selectedSerializationXPath(t, e, transform.XPath31, test, input) + if err != nil { + selectedSerializationXPathError(t, test, err, fnErr) + return "", err + } + expected := strings.TrimPrefix(string(output.Primary), "\uFEFF") + // HTML permits whitespace between the generated DOCTYPE and its root. + // Compare only that specific lexical freedom; content whitespace is exact. + matchedValue := value + if strings.Contains(expected, "<") { + matchedValue = strings.Replace(matchedValue, "\n<", "<", 1) + } + if fnErr != nil || matchedValue != expected { + t.Fatal("selected XPath31/XSLT30 host contract", value, fnErr, expected) + } + } + if err != nil { + return "", err + } + return string(output.Primary), nil +} + +func serializationVectorAdmitted(edition string, test markupSerializationCase) bool { + if strings.Contains(test.body, "disable-output-escaping") || strings.Contains(test.declarations, "Q{") { + return false + } + // fn:serialize has an explicit default method, whereas these two vectors + // test the XSLT host's automatic method selection from its result element. + if edition == "30" && (test.name == "HTML auto method version" || test.name == "HTML empty output declaration") { + return false + } + if edition == "20" && (strings.Contains(test.declarations, "html-version=") || strings.Contains(test.declarations, "item-separator=") || strings.Contains(test.declarations, "suppress-indentation=")) { + return false + } + return true +} + +func benchmarkSpecialSerialization(b *testing.B, collect bool) { + for _, tc := range []struct { + name string + method string + body string + extra string + }{ + {"JSONNode", "json", ``, ""}, + {"XHTML", "xhtml", ``, ` include-content-type="no"`}, + {"XHTMLManyNodes", "xhtml", ``, ` include-content-type="no"`}, + {"JSONMapNorm", "json", ``, ` normalization-form="NFC" use-character-maps="m"`}, + {"Adaptive", "adaptive", ``, ""}, + {"AdaptiveASCII", "adaptive", ``, ""}, + {"TextASCIINorm", "text", ``, ` normalization-form="NFC"`}, + {"AdaptiveMapNorm", "adaptive", ``, ` normalization-form="NFC" use-character-maps="m"`}, + } { + for _, size := range []int{64, 1 << 20} { + b.Run(tc.name+"/"+strconv.Itoa(size), func(b *testing.B) { + e := engine(b, transform.Config{}) + payload := strings.Repeat(`hello "é é | `, max(1, size/16)) + if tc.name == "AdaptiveASCII" || tc.name == "TextASCIINorm" { + payload = strings.Repeat("hello world ", max(1, size/12)) + } + if tc.name == "XHTMLManyNodes" { + payload = strings.Repeat(`

hello é

`, max(1, size/128)) + } + input := `` + payload + `` + d, err := e.Parse(b.Context(), []byte(input)) + if err != nil { + b.Fatal(err) + } + source := `` + tc.body + `` + s, err := e.CompileXSLT(b.Context(), []byte(source), transform.XSLTOptions{}) + if err != nil { + b.Fatal(err) + } + result, err := s.Execute(b.Context(), d, transform.XSLTContext{}) + if err != nil { + b.Fatal(err) + } + b.ReportAllocs() + b.SetBytes(int64(len(input))) + for b.Loop() { + if collect { + if _, err := result.Serialize(b.Context()); err != nil { + b.Fatal(err) + } + continue + } + if _, err := result.WriterTo(b.Context()).WriteTo(io.Discard); err != nil { + b.Fatal(err) + } + } + }) + } + } +} + +func TestSerializationIBM1141EscapesUnrepresentableCharacters(t *testing.T) { + e := engine(t, transform.Config{}) + expression := `serialize(parse-xml('中€'), map{'encoding':'ibm1141', 'omit-xml-declaration':true()})` + got := text(t, evaluate(t, e, expression, transform.XPath31)) + want := `中€` + if got != want { + t.Fatalf("serialized %q; want %q", got, want) + } +} + +func TestTextFunctionsRejectMalformedLegacyEncoding(t *testing.T) { + for _, test := range []struct{ name, invalid string }{ + {"euc-jp", "\xff"}, + {"shift-jis", "\xff"}, + {"iso-2022-jp", "\x1b"}, + {"big5", "\xff"}, + {"euc-kr", "\xff"}, + {"hz-gb-2312", "\xff"}, + } { + t.Run(test.name, func(t *testing.T) { + e := engine(t, transform.Config{Resolver: transform.ResolverFunc(func(context.Context, string) (io.ReadCloser, error) { + return io.NopCloser(strings.NewReader(test.invalid)), nil + })}) + for _, function := range []string{"unparsed-text", "unparsed-text-lines"} { + expression := function + `('urn:malformed','` + test.name + `')` + _, err := query(t, e, expression, transform.XPath31).Evaluate(t.Context(), nil, transform.XPathContext{}) + var detail *transform.Error + if !errors.As(err, &detail) || detail.Code != "FOUT1190" { + t.Fatalf("%s: %v; want FOUT1190", function, err) + } + } + expression := `unparsed-text-available('urn:malformed','` + test.name + `')` + if got := text(t, evaluate(t, e, expression, transform.XPath31)); got != nameFalse { + t.Fatal("malformed encoded text reported available", got) + } + }) + } +} + +func TestXPathHTMLMappedMalformedUTF8Bindings(t *testing.T) { + t.Parallel() + + e, err := transform.NewEngine(transform.Config{}) + if err != nil { + t.Fatal(err) + } + + for _, tc := range []struct { + name, source, form, mapping, want string + }{ + {"one malformed byte", "\xff", "NFC", `map{codepoints-to-string(65533):'x'}`, "x"}, + {"two malformed bytes", "\xff\xfe", "NFC", `map{codepoints-to-string(65533):'x'}`, "xx"}, + {"malformed middle", "A\xffB", "NFC", `map{codepoints-to-string(65533):'x'}`, "AxB"}, + {"encoded replacement", "\uFFFD", "NFC", `map{codepoints-to-string(65533):'x'}`, "x"}, + {"without map", "\xff", "NFC", `map{}`, "\xff"}, + {"unrelated map", "\xff", "NFC", `map{'Q':'y'}`, "\xff"}, + {"without normalization", "\xff", "none", `map{codepoints-to-string(65533):'x'}`, "x"}, + {"valid composition", "A\u030A", "NFC", `map{codepoints-to-string(65533):'x'}`, "Å"}, + } { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + defer func() { + if p := recover(); p != nil { + t.Errorf("public HTML serialization must not panic for a String binding: %v", p) + } + }() + + expression := `serialize($v,map{'method':'html','include-content-type':false(),'normalization-form':'` + tc.form + `','use-character-maps':` + tc.mapping + `})` + query, err := e.CompileXPath(context.Background(), expression, transform.XPathOptions{}) + if err != nil { + t.Fatal(err) + } + + result, err := query.Evaluate(context.Background(), nil, transform.XPathContext{Variables: transform.Bindings{{Local: "v"}: transform.Values(transform.String(tc.source))}}) + if err != nil { + t.Fatal(err) + } + + got, err := result.Text() + if err != nil { + t.Fatal(err) + } + + if got != tc.want { + t.Fatalf("got %q, want %q", got, tc.want) + } + }) + } +} + +func TestXPathHTMLMappedMalformedUTF8Cancellation(t *testing.T) { + t.Parallel() + + e, err := transform.NewEngine(transform.Config{}) + if err != nil { + t.Fatal(err) + } + + query, err := e.CompileXPath(context.Background(), `serialize($v,map{'method':'html','normalization-form':'NFC','use-character-maps':map{codepoints-to-string(65533):'x'}})`, transform.XPathOptions{}) + if err != nil { + t.Fatal(err) + } + + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + result, err := query.Evaluate(ctx, nil, transform.XPathContext{Variables: transform.Bindings{{Local: "v"}: transform.Values(transform.String("\xff"))}}) + if !errors.Is(err, context.Canceled) || result != (transform.Sequence{}) { + t.Fatalf("got result %+v, error %v; want empty result and cancellation", result, err) + } +} + +func TestHTMLSerializationEscapesNamespaceValues(t *testing.T) { + e := engine(t, transform.Config{}) + for _, source := range []string{ + ``, + ``, + ``, + } { + expression := `let $s := serialize(parse-xml($source),map{'method':'html','indent':false()}) return namespace-uri(parse-xml($s)/*) = namespace-uri(parse-xml($source)/*) and contains($s,'urn:a&b')` + value, err := query(t, e, expression, transform.XPath31).Evaluate(t.Context(), nil, transform.XPathContext{Variables: transform.Bindings{ + {Local: sourceName}: transform.Values(transform.String(source)), + }}) + if err != nil || text(t, value) != nameTrue { + t.Fatal("HTML namespace serialization changed namespace identity", source, value, err) + } + } +} + +func TestXPathSerializationMalformedUTF8Bindings(t *testing.T) { + t.Parallel() + e, err := transform.NewEngine(transform.Config{}) + if err != nil { + t.Fatal(err) + } + type fixture struct{ name, source, options, want string } + cases := make([]fixture, 0, 34) + for _, method := range []string{"text", "json"} { + for _, form := range []string{"none", "NFC"} { + for _, v := range []struct{ name, source, want string }{ + {"one invalid byte", "\xff", "x"}, + {"two invalid bytes", "\xff\xfe", "xx"}, + {"invalid middle", "A\xffB", "AxB"}, + {"valid encoded replacement", "\uFFFD", "x"}, + {"valid ASCII", "ABC", "ABC"}, + } { + want := v.want + if method == "json" { + want = `"` + want + `"` + } + cases = append(cases, fixture{method + "/" + form + "/" + v.name, v.source, `map{'method':'` + method + `','normalization-form':'` + form + `','use-character-maps':map{codepoints-to-string(65533):'x'}}`, want}) + } + } + } + for _, form := range []string{"none", "NFC"} { + for _, v := range []struct{ name, source, want string }{ + {"one invalid byte", "\xff", `"\uFFFD"`}, + {"two invalid bytes", "\xff\xfe", `"\uFFFD\uFFFD"`}, + {"invalid middle", "A\xffB", `"A\uFFFDB"`}, + {"valid encoded replacement", "\uFFFD", `"\uFFFD"`}, + {"valid ASCII", "ABC", `"ABC"`}, + } { + cases = append(cases, fixture{"JSON ASCII/" + form + "/" + v.name, v.source, `map{'method':'json','encoding':'US-ASCII','normalization-form':'` + form + `'}`, v.want}) + } + } + for _, method := range []string{"text", "json"} { + for _, mapping := range []string{`map{}`, `map{'Q':'y'}`} { + want := "\xff" + if method == "json" { + want = `"` + want + `"` + } + cases = append(cases, fixture{method + "/unmapped/" + mapping, "\xff", `map{'method':'` + method + `','normalization-form':'NFC','use-character-maps':` + mapping + `}`, want}) + } + } + if len(cases) != 34 { + t.Fatalf("fixture count %d", len(cases)) + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + defer func() { + if p := recover(); p != nil { + t.Errorf("public serialization panic for source %x: %v", tc.source, p) + } + }() + q, err := e.CompileXPath(t.Context(), `serialize($v,`+tc.options+`)`, transform.XPathOptions{}) + if err != nil { + t.Fatal(err) + } + result, err := q.Evaluate(t.Context(), nil, transform.XPathContext{Variables: transform.Bindings{{Local: "v"}: transform.Values(transform.String(tc.source))}}) + if err != nil { + t.Fatal(err) + } + actual, err := result.Text() + if err != nil { + t.Fatal(err) + } + if actual != tc.want { + t.Errorf("actual %q bytes %x; want %q bytes %x", actual, actual, tc.want, tc.want) + } + }) + } +} + +func TestXPathSerializationMalformedUTF8Cancellation(t *testing.T) { + t.Parallel() + e, err := transform.NewEngine(transform.Config{}) + if err != nil { + t.Fatal(err) + } + for _, options := range []string{ + `map{'method':'text','use-character-maps':map{codepoints-to-string(65533):'x'}}`, + `map{'method':'json','use-character-maps':map{codepoints-to-string(65533):'x'}}`, + `map{'method':'json','encoding':'US-ASCII'}`, + } { + t.Run(options, func(t *testing.T) { + t.Parallel() + q, err := e.CompileXPath(t.Context(), `serialize($v,`+options+`)`, transform.XPathOptions{}) + if err != nil { + t.Fatal(err) + } + ctx, cancel := context.WithCancel(t.Context()) + cancel() + result, err := q.Evaluate(ctx, nil, transform.XPathContext{Variables: transform.Bindings{{Local: "v"}: transform.Values(transform.String("\xff"))}}) + if !errors.Is(err, context.Canceled) || result != (transform.Sequence{}) { + t.Fatalf("result %+v/error %v; want empty and context.Canceled", result, err) + } + }) + } +} diff --git a/transform/v1.go b/transform/v1.go index 5f702a5..1c72e70 100644 --- a/transform/v1.go +++ b/transform/v1.go @@ -10,6 +10,7 @@ import ( "time" "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/tree" "github.com/skarm/xmlsec/reference" "github.com/skarm/xmlsec/transform/internal/xdm" @@ -17,6 +18,13 @@ import ( "github.com/skarm/xmlsec/transform/internal/xslt" ) +// Approved XMLDSig programs have a count cap and a compilation-time floor separate from the caller's runtime timeout. ID lookup also bounds the number of input tokens. +const ( + maximumApprovedPrograms = 64 + approvedCompilationTimeout = 3 * time.Second + maximumIDTokens = 65_536 +) + var _ reference.ExtensionProcessor = (*Engine)(nil) // Fingerprint identifies the exact approved program, excluding runtime Here handles. @@ -103,7 +111,7 @@ type v1Processor struct { // newV1Processor compiles the approved XMLDSig compatibility programs. func newV1Processor(e *Engine, programs []reference.Transform) (*v1Processor, error) { - if len(programs) == 0 || len(programs) > 64 { + if len(programs) == 0 || len(programs) > maximumApprovedPrograms { return nil, fmt.Errorf("%w: 1..64 approved programs required", reference.ErrPolicy) } @@ -121,6 +129,10 @@ func newV1Processor(e *Engine, programs []reference.Transform) (*v1Processor, er } if t.Algorithm == reference.XSLT { + if len(t.Stylesheet) > limits.MaxProgramBytes { + return nil, ErrLimit + } + sheet, err := compileV1Stylesheet(e, t.Stylesheet) if err != nil { return nil, err @@ -131,45 +143,67 @@ func newV1Processor(e *Engine, programs []reference.Transform) (*v1Processor, er continue } - ctx := xpath.WithBudget(context.Background(), limits.MaxOperations, limits.MaxNodes, limits.MaxOutputBytes, limits.MaxDepth) + expressions, err := compileV1XPathProgram(e, t) + if err != nil { + return nil, err + } - var expressions []compiled + p.programs[key] = expressions + } - for _, x := range t.XPath { - ns := x.Namespaces - if ns == nil { - ns = make(map[string]string) - } + return p, nil +} - ns["xml"] = document.XMLNamespace - functions := map[string]xpath.Function{"here": hereFunction(document.Element{}, nil)} +// Approved programs are trusted setup; preserve the historical XSLT timeout +// floor independently of the shorter timeout an application can use at runtime. +func (e *Engine) approvedCompilationContext() (context.Context, context.CancelFunc) { + limits := e.limits + ctx := xpath.WithBudget(context.Background(), limits.MaxOperations, limits.MaxNodes, limits.MaxOutputBytes, limits.MaxDepth) - expr, err := xpath.NewCompiler().Context(ctx).Language(10, ns).Compile(x.Expression) - if err != nil { - return nil, executionError(err) - } + return context.WithTimeout(ctx, max(limits.Timeout, approvedCompilationTimeout)) +} - if err := expr.Validate(ns); err != nil { - return nil, executionError(err) - } +func compileV1XPathProgram(e *Engine, t reference.Transform) ([]compiled, error) { + ctx, cancel := e.approvedCompilationContext() + defer cancel() - if err := xpath.CheckStandaloneFunctions(expr, ns, functions); err != nil { - return nil, executionError(err) - } + var expressions []compiled - if len(expr.StaticReferences(ns).FreeVariables) > 0 { - return nil, fmt.Errorf("%w: XMLDSig XPath has no variable bindings", reference.ErrInput) - } + for _, x := range t.XPath { + if len(x.Expression) > e.limits.MaxProgramBytes { + return nil, ErrLimit + } + + ns := x.Namespaces + if ns == nil { + ns = make(map[string]string) + } - ev := xpath.NewEvaluator(xpath.DefaultEvaluatorOptions).Namespaces(ns).Functions(functions, nil).OpLimit(p.operations) + ns[lexicon.PrefixXML] = document.XMLNamespace + functions := map[string]xpath.Function{"here": hereFunction(document.Element{}, nil)} - expressions = append(expressions, compiled{expr, ev}) + expr, err := xpath.NewCompiler().Context(ctx).Language(int(XPath10), ns).Compile(x.Expression) + if err != nil { + return nil, executionError(err) } - p.programs[key] = expressions + if err := expr.Validate(ns); err != nil { + return nil, executionError(err) + } + + if err := xpath.CheckStandaloneFunctions(expr, ns, functions); err != nil { + return nil, executionError(err) + } + + if len(expr.StaticReferences(ns).FreeVariables) > 0 { + return nil, fmt.Errorf("%w: XMLDSig XPath has no variable bindings", reference.ErrInput) + } + + ev := xpath.NewEvaluator(xpath.DefaultEvaluatorOptions).Namespaces(ns).Functions(functions, nil).OpLimit(e.limits.MaxOperations) + expressions = append(expressions, compiled{expr, ev}) } - return p, nil + return expressions, ctx.Err() } // Validate preflights the compiled allowed transform before reference I/O. @@ -206,10 +240,9 @@ func (p *v1Processor) Apply(ctx context.Context, t reference.Transform, in refer } defer cancel() - limits := document.DefaultLimits() + limits := p.engine.limits.xmlLimits() limits.MaxBytes = p.input limits.MaxNodes = p.nodes - limits.MaxDepth = p.engine.limits.MaxDepth if err := tree.CheckSnapshotLimits(ctx, doc, limits); err != nil { return reference.Data{}, executionError(err) @@ -241,7 +274,7 @@ func (p *v1Processor) Apply(ctx context.Context, t reference.Transform, in refer order := xpath.NewDocOrderCache() for i := range program { - functions := map[string]xpath.Function{"here": hereFunction(t.XPath[i].Here, view), "id": idFunction(resolve, min(p.nodes, 65536))} + functions := map[string]xpath.Function{"here": hereFunction(t.XPath[i].Here, view), "id": idFunction(resolve, min(p.nodes, maximumIDTokens))} evaluators[i] = program[i].evaluator.Functions(functions, nil).DocOrderCache(order) } diff --git a/transform/v1_test.go b/transform/v1_test.go index a4428bb..0cad6ff 100644 --- a/transform/v1_test.go +++ b/transform/v1_test.go @@ -3,6 +3,7 @@ package transform_test import ( "context" "errors" + "fmt" "strings" "sync" "testing" @@ -225,9 +226,10 @@ func TestApprovalAndLimits(t *testing.T) { } out, e := p.Apply(t.Context(), tr, in, reference.ExtensionContext{}) - if !errors.Is(e, reference.ErrLimit) || out.Kind() != reference.Invalid { - t.Fatal("bounded execution", c, out.Kind(), e) + if !errors.Is(e, reference.ErrLimit) { + t.Fatal("bounded execution", c, out, e) } + requireInvalidReferenceResult(t, out) } value := xp(`string(.) != 'x'`) @@ -297,9 +299,10 @@ func TestIDTokenPreallocationBound(t *testing.T) { d := parse(t, ""+strings.Repeat("a ", 70000)+"") out, e := p.Apply(t.Context(), tr, input(t, d), reference.ExtensionContext{}) - if !errors.Is(e, reference.ErrLimit) || out.Kind() != reference.Invalid { + if !errors.Is(e, reference.ErrLimit) { t.Fatal("unbounded ID tokens", e) } + requireInvalidReferenceResult(t, out) } func FuzzApprovedXPath(f *testing.F) { @@ -364,9 +367,7 @@ func FuzzXPathCompilation(f *testing.F) { out, e := p.Apply(t.Context(), tr, in, reference.ExtensionContext{}) if e != nil { - if out.Kind() != reference.Invalid { - t.Fatal("partial result") - } + requireInvalidReferenceResult(t, out) return } @@ -457,3 +458,41 @@ func apply(t testing.TB, p *transform.Engine, tr reference.Transform, in referen return ns } + +func TestApprovedProgramSourceByteLimits(t *testing.T) { + stylesheet := style(``) + for _, tc := range []struct { + name string + program reference.Transform + size int + }{ + {"XPath", xp(`true()`), len(`true()`)}, + {"Filter2 later expression", f2(filter("intersect", `/`), filter("intersect", `/r`)), len(`/r`)}, + {"XSLT", stylesheet, len(stylesheet.Stylesheet)}, + } { + for _, limit := range []int{tc.size - 1, tc.size} { + t.Run(fmt.Sprintf("%s/%d", tc.name, limit), func(t *testing.T) { + e, err := transform.NewEngine(transform.Config{ + Limits: transform.Limits{MaxProgramBytes: limit}, + AllowedPrograms: []reference.Transform{tc.program}, + }) + if limit < tc.size { + var detail *transform.Error + if e != nil || !errors.Is(err, transform.ErrLimit) || !errors.As(err, &detail) || detail.Operation != "approve XMLDSig transforms" { + t.Fatalf("engine=%v, error=%v; want approval ErrLimit", e != nil, err) + } + + return + } + + if err != nil || e == nil { + t.Fatalf("source exactly at limit: engine=%v, error=%v", e != nil, err) + } + + if err := e.Validate(t.Context(), tc.program); err != nil { + t.Fatal("approved source at limit", err) + } + }) + } + } +} diff --git a/transform/v1_xslt.go b/transform/v1_xslt.go index 98b22d6..78c04cc 100644 --- a/transform/v1_xslt.go +++ b/transform/v1_xslt.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "strings" - "time" "github.com/skarm/xmlsec/document" "github.com/skarm/xmlsec/internal/lexicon" @@ -16,14 +15,21 @@ import ( "github.com/skarm/xmlsec/transform/internal/xslt" ) +// The XMLDSig XSLT 1.0 syntax preflight has separate source/expression byte caps. Its language-version and namespace-attribute names are distinct from XML declarations and serialization settings. +const ( + maximumV1StylesheetBytes = 1 << 20 + maximumV1XPathBytes = 16 << 10 + stylesheetVersionAttribute = "version" + stylesheetNamespaceAttribute = "namespace" + v1StylesheetLanguageVersion = "1.0" +) + func compileV1Stylesheet(e *Engine, source []byte) (*xslt.Stylesheet, error) { - ctx, cancel := context.WithTimeout(context.Background(), max(e.limits.Timeout, 3*time.Second)) + ctx, cancel := e.approvedCompilationContext() defer cancel() - ctx = xpath.WithBudget(ctx, e.limits.MaxOperations, e.limits.MaxNodes, e.limits.MaxOutputBytes, e.limits.MaxDepth) - limits := document.DefaultLimits() - limits.MaxBytes = 1 << 20 + limits.MaxBytes = maximumV1StylesheetBytes d, err := document.Parse(ctx, source, limits) if err != nil { @@ -68,7 +74,7 @@ func checkStylesheetElement(ctx context.Context, e document.Element) error { name := e.Name() attrs, known := xslt.XSLT10ElementAttributes(name.Local) - if name.Space == lexicon.NamespaceXSLT && (name.Local == "include" || name.Local == "import") { + if name.Space == lexicon.NamespaceXSLT && (name.Local == lexicon.XSLTElementInclude || name.Local == lexicon.XSLTElementImport) { return fmt.Errorf("%w: XMLDSig stylesheet resources are disabled", reference.ErrUnsupported) } @@ -138,13 +144,13 @@ func containsWord(words, word string) bool { func isStylesheetAVT(element, attribute string) bool { switch element { - case "element", "attribute": - return attribute == "name" || attribute == "namespace" - case "processing-instruction": - return attribute == "name" - case "sort": + case lexicon.XSLTElementElement, lexicon.XSLTElementAttribute: + return attribute == lexicon.AttrName || attribute == stylesheetNamespaceAttribute + case lexicon.XSLTElementProcessingInstruction: + return attribute == lexicon.AttrName + case lexicon.XSLTElementSort: return containsWord("lang data-type order case-order", attribute) - case "number": + case lexicon.XSLTElementNumber: return containsWord("format lang letter-value grouping-separator grouping-size", attribute) } @@ -152,7 +158,7 @@ func isStylesheetAVT(element, attribute string) bool { } func checkStylesheetXPath(ctx context.Context, code string, ns map[string]string) error { - if len(code) > 16<<10 { + if len(code) > maximumV1XPathBytes { return reference.ErrLimit } @@ -268,7 +274,7 @@ func v1Error(err error) error { } func staticStylesheetXPath(ctx context.Context, x reference.XPathExpression) error { - expr, err := xpath.NewCompiler().Context(ctx).Language(10, x.Namespaces).Compile(x.Expression) + expr, err := xpath.NewCompiler().Context(ctx).Language(int(XPath10), x.Namespaces).Compile(x.Expression) if err != nil { return executionError(err) } @@ -292,7 +298,7 @@ func checkStylesheetProfileAttribute(a tree.Attribute, name tree.Name) error { return nil } - if a.Name.Local == "version" && (name.Local == "stylesheet" || name.Local == "transform" || a.Name.Space == lexicon.NamespaceXSLT) && a.Value != "1.0" { + if a.Name.Local == stylesheetVersionAttribute && (name.Local == lexicon.XSLTElementStylesheet || name.Local == lexicon.XSLTElementTransform || a.Name.Space == lexicon.NamespaceXSLT) && a.Value != v1StylesheetLanguageVersion { return fmt.Errorf("%w: XSLT 1.0 required", reference.ErrUnsupported) } diff --git a/transform/v1_xslt_test.go b/transform/v1_xslt_test.go index 4915904..4430fe1 100644 --- a/transform/v1_xslt_test.go +++ b/transform/v1_xslt_test.go @@ -74,9 +74,10 @@ func TestXSLTLimitsAndContext(t *testing.T) { } out, err := p.Apply(ctx, text, reference.Octets([]byte(`long`)), reference.ExtensionContext{}) - if !errors.Is(err, reference.ErrLimit) || out.Kind() != reference.Invalid { - t.Fatalf("input=%d nodes=%d output=%d: %v, %v", cfg.Limits.MaxInputBytes, cfg.Limits.MaxNodes, cfg.Limits.MaxOutputBytes, out.Kind(), err) + if !errors.Is(err, reference.ErrLimit) { + t.Fatalf("input=%d nodes=%d output=%d: %v, %v", cfg.Limits.MaxInputBytes, cfg.Limits.MaxNodes, cfg.Limits.MaxOutputBytes, out, err) } + requireInvalidReferenceResult(t, out) } recursive := style(``) @@ -87,9 +88,10 @@ func TestXSLTLimitsAndContext(t *testing.T) { } out, err := p.Apply(ctx, recursive, reference.Octets([]byte(``)), reference.ExtensionContext{}) - if !errors.Is(err, reference.ErrLimit) || out.Kind() != reference.Invalid { - t.Fatalf("recursion: %v, %v", out.Kind(), err) + if !errors.Is(err, reference.ErrLimit) { + t.Fatalf("recursion: %v, %v", out, err) } + requireInvalidReferenceResult(t, out) p = newProcessor(t, text) cancelled, cancel := context.WithCancel(ctx) @@ -159,7 +161,8 @@ func TestXSLTExecutionTimeout(t *testing.T) { } out, err := p.Apply(t.Context(), tr, reference.Octets([]byte(``)), reference.ExtensionContext{}) - if !errors.Is(err, context.DeadlineExceeded) || out.Kind() != reference.Invalid { - t.Fatalf("unbounded execution: %v, %v", out.Kind(), err) + if !errors.Is(err, context.DeadlineExceeded) { + t.Fatalf("unbounded execution: %v, %v", out, err) } + requireInvalidReferenceResult(t, out) } diff --git a/transform/value.go b/transform/value.go index f651497..197ddb7 100644 --- a/transform/value.go +++ b/transform/value.go @@ -6,6 +6,7 @@ import ( "time" "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/transform/internal/xdm" "github.com/skarm/xmlsec/transform/internal/xpath" ) @@ -38,6 +39,14 @@ const ( // Value retains one immutable XDM item. Its zero value denotes an absent item. type Value struct{ item xdm.Item } +// MapEntry associates one atomic XDM key with an immutable sequence value. +type MapEntry struct { + // Key is the atomic XDM key. Non-atomic values are rejected by Map. + Key Value + // Value is the sequence associated with Key, including an empty sequence. + Value Sequence +} + // String creates an xs:string item sharing the immutable value storage. // Pass strings.Clone(value) to detach a caller-created substring from its source. func String(value string) Value { @@ -65,7 +74,7 @@ func BigInteger(value *big.Int) Value { func Decimal(value string) (Value, error) { a, err := xpath.CastAtomic(xdm.AtomicValue{TypeName: xdm.TypeString, Value: value}, xdm.TypeDecimal) if err != nil { - return Value{}, wrap("decimal", err) + return Value{}, wrap(lexicon.TypeDecimal, err) } return Value{item: a}, nil @@ -155,7 +164,7 @@ func (v Value) Text() (string, error) { func (v Value) Boolean() (bool, error) { a, ok := v.item.(xdm.AtomicValue) if !ok || a.TypeName != xdm.TypeBoolean { - return false, wrap("boolean", ErrInput) + return false, wrap(lexicon.TypeBoolean, ErrInput) } return a.BooleanVal(), nil @@ -165,7 +174,7 @@ func (v Value) Boolean() (bool, error) { func (v Value) Integer() (*big.Int, error) { a, ok := v.item.(xdm.AtomicValue) if !ok || !xdm.BuiltinIsSubtypeOf(a.TypeName, xdm.TypeInteger) { - return nil, wrap("integer", ErrInput) + return nil, wrap(lexicon.TypeInteger, ErrInput) } return new(big.Int).Set(a.BigInt()), nil @@ -175,7 +184,7 @@ func (v Value) Integer() (*big.Int, error) { func (v Value) Decimal() (*big.Rat, error) { a, ok := v.item.(xdm.AtomicValue) if !ok || !xdm.BuiltinIsSubtypeOf(a.TypeName, xdm.TypeDecimal) { - return nil, wrap("decimal", ErrInput) + return nil, wrap(lexicon.TypeDecimal, ErrInput) } return new(big.Rat).Set(a.BigRat()), nil @@ -240,7 +249,7 @@ func (v Value) Size() (int, error) { func (v Value) Double() (float64, error) { a, ok := v.item.(xdm.AtomicValue) if !ok || a.TypeName != xdm.TypeDouble && a.TypeName != xdm.TypeFloat { - return 0, wrap("double", ErrInput) + return 0, wrap(lexicon.TypeDouble, ErrInput) } return a.DoubleVal(), nil @@ -250,7 +259,7 @@ func (v Value) Double() (float64, error) { func (v Value) DateTime() (time.Time, error) { a, ok := v.item.(xdm.AtomicValue) if !ok || !xdm.BuiltinIsSubtypeOf(a.TypeName, xdm.TypeDateTime) { - return time.Time{}, wrap("dateTime", ErrInput) + return time.Time{}, wrap(lexicon.TypeDateTime, ErrInput) } return a.TimeVal(), nil @@ -273,23 +282,6 @@ func (v Value) Keys() ([]Value, error) { return values, nil } -func stringsCutXS(s string) (string, bool) { - if len(s) > 3 && s[:3] == "xs:" { - return s[3:], true - } - - return s, false -} - -func lexical(value xdm.AtomicValue) (string, error) { - converted, err := xpath.CastAtomic(value, xdm.TypeString) - if err != nil { - return "", wrap("lexical value", err) - } - - return converted.StringVal(), nil -} - // Sequence retains an immutable XDM sequence. Its zero value is empty. type Sequence struct{ items xdm.Sequence } @@ -350,12 +342,21 @@ func (s Sequence) EffectiveBoolean() (bool, error) { return value, wrap("effective boolean", err) } -// MapEntry associates one atomic XDM key with an immutable sequence value. -type MapEntry struct { - // Key is the atomic XDM key. Non-atomic values are rejected by Map. - Key Value - // Value is the sequence associated with Key, including an empty sequence. - Value Sequence +func stringsCutXS(s string) (string, bool) { + if len(s) > 3 && s[:3] == "xs:" { + return s[3:], true + } + + return s, false +} + +func lexical(value xdm.AtomicValue) (string, error) { + converted, err := xpath.CastAtomic(value, xdm.TypeString) + if err != nil { + return "", wrap("lexical value", err) + } + + return converted.StringVal(), nil } func expandedName(q document.Name) string { diff --git a/transform/value_test.go b/transform/value_test.go index bc0ac2c..5f96b8c 100644 --- a/transform/value_test.go +++ b/transform/value_test.go @@ -27,7 +27,9 @@ func TestTypedValues(t *testing.T) { publishedProofVector(t, strconv.Itoa(want)) e := engine(t, transform.Config{}) result, err := query(t, e, source, version).Evaluate(t.Context(), nil, transform.XPathContext{}) - if err != nil || result.Len() != want { + if err != nil { + t.Fatal(err) + } else if result.Len() != want { t.Fatal(result.Len(), err, want) } // Text is a singleton convenience conversion; evaluation exposes the XDM @@ -823,7 +825,9 @@ func TestXSLT20TextFunctionContracts(t *testing.T) { if !errors.As(err, &detail) || detail.Code != want { t.Fatalf("got %v, want %s", err, want) } - } else if err != nil || string(result.Primary) != want { + } else if err != nil { + t.Fatal(err) + } else if string(result.Primary) != want { t.Fatalf("got %q (%v), want %q", result.Primary, err, want) } }) @@ -882,6 +886,10 @@ func xmlToJSONSchemaCases() []struct { {"invalid key boolean", ``, ".", "FOJS0006"}, {"normalized duplicate", ``, ".", "FOJS0006"}, {"foreign attributes ignored", `\q`, ".", `{"x":"\\q"}`}, + {"foreign boolean attributes", `true`, ".", "true"}, + {"foreign number attributes", `1`, ".", "1"}, + {"unqualified extra attribute", `true`, ".", "FOJS0006"}, + {"function namespace attribute", `true`, ".", "FOJS0006"}, {"array whitespace", ` a `, ".", `["a",null]`}, {"escaped string", `\u0041`, ".", `"\u0041"`}, {"invalid escape", `\q`, ".", "FOJS0007"}, @@ -1378,3 +1386,47 @@ func TestReturnedIntrospectionStaticContexts(t *testing.T) { } } } + +func TestNamespaceNodeDocumentOrderIsTotalAndUnionStable(t *testing.T) { + e := engine(t, transform.Config{}) + doc, err := e.Parse(t.Context(), []byte(``)) + if err != nil { + t.Fatal(err) + } + const prefix = `for $p in /*/namespace::p, $q in /*/namespace::q return ` + for _, version := range []transform.XPathVersion{transform.XPath20, transform.XPath30, transform.XPath31} { + for _, expression := range []string{ + `not($p is $q) and (($p << $q) != ($q << $p))`, + `(($p | $q)[1]) is (($q | $p)[1])`, + `($p | $q)[1] << ($p | $q)[2]`, + `every $n in /*/namespace::* satisfies $n is /*/namespace::*[name()=name($n)]`, + } { + value, err := query(t, e, prefix+expression, version).Evaluate(t.Context(), doc, transform.XPathContext{}) + if err != nil || text(t, value) != nameTrue { + t.Fatal("namespace ordering invariant failed", version, expression, value, err) + } + } + } +} + +func TestDateTimeStampMapKeysUseDateTimeValueEquality(t *testing.T) { + e := engine(t, transform.Config{}) + for _, expression := range []string{ + `map:contains(map{xs:dateTimeStamp('2000-01-01T00:00:00Z'):1},xs:dateTime('2000-01-01T01:00:00+01:00'))`, + `map:contains(map{xs:dateTime('2000-01-01T00:00:00Z'):1},xs:dateTimeStamp('2000-01-01T00:00:00Z'))`, + `map:size(map:merge((map{xs:dateTimeStamp('2000-01-01T00:00:00Z'):1},map{xs:dateTime('2000-01-01T00:00:00Z'):2}))) = 1`, + `map:size(map:put(map{xs:dateTimeStamp('2000-01-01T00:00:00Z'):1},xs:dateTime('2000-01-01T00:00:00Z'),2)) = 1`, + `not(map:contains(map{xs:dateTimeStamp('2000-01-01T00:00:00Z'):1},xs:date('2000-01-01Z')))`, + `not(map:contains(map{xs:dateTimeStamp('2000-01-01T00:00:00Z'):1},xs:gYear('2000Z')))`, + `not(map:contains(map{xs:dateTimeStamp('2000-01-01T00:00:00Z'):1},xs:dateTime('2000-01-01T00:00:00')))`, + } { + if got := text(t, evaluate(t, e, expression, transform.XPath31)); got != nameTrue { + t.Fatal("dateTime map key domain equality changed", expression, got) + } + } + _, err := query(t, e, `map{xs:dateTimeStamp('2000-01-01T00:00:00Z'):1,xs:dateTime('2000-01-01T00:00:00Z'):2}`, transform.XPath31).Evaluate(t.Context(), nil, transform.XPathContext{}) + var detail *transform.Error + if !errors.As(err, &detail) || detail.Code != "XQDY0137" { + t.Fatal("equal dateTime keys did not reject duplicate constructor", err) + } +} diff --git a/transform/xpath.go b/transform/xpath.go index 114cda7..1972491 100644 --- a/transform/xpath.go +++ b/transform/xpath.go @@ -83,12 +83,18 @@ type XPathContext struct { // XPath retains an immutable compiled expression and the engine policy that governs // its execution. type XPath struct { - namespaces map[string]string + // namespaces retains the compiled expression's static prefix bindings. + namespaces map[string]string + // freeVariables lists external variables required by the compiled expression. freeVariables []string - engine *Engine - expression *xpath.Expression - evaluator xpath.Evaluator - version XPathVersion + // engine retains immutable policy and application adapters for evaluation. + engine *Engine + // expression is the compiled XPath program. + expression *xpath.Expression + // evaluator retains the evaluation strategy selected for this program. + evaluator xpath.Evaluator + // version selects the expression's XPath language semantics. + version XPathVersion } // Version reports the compiled language version. @@ -271,7 +277,7 @@ func (e *Engine) CompileXPath(ctx context.Context, source string, options XPathO } } - namespaces["xml"] = lexicon.NamespaceXML + namespaces[lexicon.PrefixXML] = lexicon.NamespaceXML if version == XPath10 && namespaces[""] != "" { return nil, wrapXPath("compile XPath", fmt.Errorf("%w: XPath 1.0 has no default element namespace", ErrInput), options.Version) } diff --git a/transform/xpath_test.go b/transform/xpath_test.go index 5814b9f..cfeeb36 100644 --- a/transform/xpath_test.go +++ b/transform/xpath_test.go @@ -12,6 +12,7 @@ import ( "strconv" "strings" "testing" + "time" "github.com/skarm/xmlsec/document" "github.com/skarm/xmlsec/transform" @@ -66,20 +67,6 @@ const namespaceNodeTypeXML = `` const basicAccessorXML = `` -// publishedProofVector records the fixture's literal reference value, rather -// than the implementation's observed output, for the executable proof ledger. -func publishedProofVector(t *testing.T, want string) { - t.Helper() - if os.Getenv("XMLSEC_PROOF_CAPTURE") != "1" { - return - } - raw, err := json.Marshal(want) - if err != nil { - t.Fatal(err) - } - t.Log("P3_VECTOR " + string(raw)) -} - func TestXPathCalendarOverflowCodes(t *testing.T) { e := engine(t, transform.Config{}) for expression, code := range map[string]string{ @@ -2141,7 +2128,9 @@ func TestSubsequenceWorkBudget(t *testing.T) { t.Fatal(err) } result, err := q.Evaluate(t.Context(), nil, transform.XPathContext{}) - if err != nil || result.Len() != 5 { + if err != nil { + t.Fatal(err) + } else if result.Len() != 5 { t.Fatal("skipped range prefix must require no traversal", result.Len(), err) } for i := range result.Len() { @@ -3825,3 +3814,1027 @@ func TestXPathErrorCodeNamespaceContracts(t *testing.T) { } } } + +// publishedProofVector records the fixture's literal reference value, rather +// than the implementation's observed output, for the executable proof ledger. +func publishedProofVector(t *testing.T, want string) { + t.Helper() + if os.Getenv("XMLSEC_PROOF_CAPTURE") != "1" { + return + } + raw, err := json.Marshal(want) + if err != nil { + t.Fatal(err) + } + t.Log("P3_VECTOR " + string(raw)) +} + +func TestFormatNumberRetainsLargeExactValues(t *testing.T) { + e := engine(t, transform.Config{}) + large := "1" + strings.Repeat("0", 400) + for expression, want := range map[string]string{ + `format-number(xs:integer('` + large + `'), '0')`: large, + `format-number(xs:decimal('` + large + `'), '0')`: large, + `format-number(xs:integer('-` + large + `'), '0')`: "-" + large, + `format-number(xs:decimal('` + large + `'), '0%')`: large + "00%", + `format-number(xs:double('1e308'), '0%')`: "Infinity%", + `format-number(xs:double('-0'), '0.0')`: "-0.0", + `format-number(1 div 7, '0.0000')`: "0.1429", + } { + if got := text(t, evaluate(t, e, expression, transform.XPath31)); got != want { + t.Fatalf("formatted value as %q; want %q", got, want) + } + } + value := "0." + strings.Repeat("0", 1000) + "59" + picture := "0." + strings.Repeat("0", 1001) + want := "0." + strings.Repeat("0", 1000) + "6" + expression := `format-number(xs:decimal('` + value + `'), '` + picture + `')` + if got := text(t, evaluate(t, e, expression, transform.XPath31)); got != want { + t.Fatalf("wide picture rounded to %q; want %q", got, want) + } +} + +func TestFormatNumberRejectsUnboundedPictureWork(t *testing.T) { + e := engine(t, transform.Config{Limits: transform.Limits{MaxOutputBytes: 1024, MaxNodes: 1024}}) + ctx, cancel := context.WithTimeout(t.Context(), time.Second) + defer cancel() + + // fn:round-half-to-even takes an arbitrary integer precision, and decides + // that this already finite decimal is unchanged without exponentiation. + value, err := query(t, e, `round-half-to-even(1 div 3, 9223372036854775806) eq (1 div 3)`, transform.XPath31).Evaluate(ctx, nil, transform.XPathContext{}) + if err != nil || text(t, value) != nameTrue { + t.Fatal("huge precision changed the finite quotient", err) + } + // format-number's precision instead comes from a materialized picture. + // Both a huge dynamic construction and an existing oversized binding must + // hit the invocation budget before decimal powers can be allocated. + for _, expression := range []string{ + `format-number(1 div 3, string-join((1 to 9223372036854775806) ! '0', ''))`, + `format-number(1 div 3, $picture)`, + } { + program := query(t, e, expression, transform.XPath31) + invocation := transform.XPathContext{} + if strings.Contains(expression, "$picture") { + invocation.Variables = transform.Bindings{ + {Local: "picture"}: transform.Values(transform.String("0." + strings.Repeat("0", 4096))), + } + } + _, err := program.Evaluate(ctx, nil, invocation) + if !errors.Is(err, transform.ErrLimit) { + t.Fatalf("unbounded picture: %v; want resource limit", err) + } + } +} + +func TestRegexCharacterClassGroupLookalikes(t *testing.T) { + e := engine(t, transform.Config{}) + for _, version := range []transform.XPathVersion{transform.XPath20, transform.XPath30, transform.XPath31} { + for expression, want := range map[string]string{ + `matches('(', '[(?]')`: nameTrue, + `matches('?', '[(?]')`: nameTrue, + `matches('a', '[(?]')`: nameFalse, + `matches('(', '[(?-[?]]')`: nameTrue, + `matches('?', '[(?-[?]]')`: nameFalse, + } { + if got := text(t, evaluate(t, e, expression, version)); got != want { + t.Fatal(version, expression, got, want) + } + } + for _, expression := range []string{`matches('a', '[a](?=b)')`, `matches('a', '\[(?=a)')`} { + _, err := query(t, e, expression, version).Evaluate(t.Context(), nil, transform.XPathContext{}) + var detail *transform.Error + if !errors.As(err, &detail) || detail.Code != "FORX0002" { + t.Fatal("lookaround was accepted", version, expression, err) + } + } + } +} + +func TestUCAMalformedBindingSubstringBoundaries(t *testing.T) { + engine, err := transform.NewEngine(transform.Config{}) + if err != nil { + t.Fatal(err) + } + queries := make(map[string]*transform.XPath, 2) + for _, fn := range []string{"substring-before", "substring-after"} { + query, err := engine.CompileXPath(t.Context(), fn+"($s,$needle,$uri)", transform.XPathOptions{}) + if err != nil { + t.Fatal(err) + } + queries[fn] = query + } + const base = "http://www.w3.org/2013/collation/UCA" + profiles := []struct { + name, uri string + projected bool + }{ + {"Default", base, false}, + {"Numeric", base + "?numeric=yes", false}, + {"ShiftedPrimary", base + "?strength=primary;alternate=shifted", true}, + {"BlankedPrimary", base + "?strength=primary;alternate=blanked", true}, + {"ShiftedIdentical", base + "?strength=identical;alternate=shifted", false}, + } + cases := []struct{ name, source, needle, before, after, projectedBefore, projectedAfter string }{ + {"InvalidPrefix", "\xffa", "a", "\xff", "", "\xff", ""}, + {"InvalidMiddle", "L\xffaR", "a", "L\xff", "R", "L\xff", "R"}, + {"TruncatedPrefix", "\xe2\x82a", "a", "\xe2\x82", "", "\xe2\x82", ""}, + {"OverlongPrefix", "\xc0\xafaR", "a", "\xc0\xaf", "R", "\xc0\xaf", "R"}, + {"SurrogatePrefix", "\xed\xa0\x80a", "a", "\xed\xa0\x80", "", "\xed\xa0\x80", ""}, + {"InvalidMatch", "L\xffR", "\ufffd", "L", "R", "", "L\xffR"}, + {"InvalidMatchAtEnd", "a\xff", "\ufffd", "a", "", "", "a\xff"}, + {"InvalidSuffixPreserved", "La\xff", "a", "L", "\xff", "L", "\xff"}, + {"InvalidNeedleMiss", "aé", "\xff", "", "", "", "aé"}, + {"InvalidNeedleAndMatch", "L\xffR", "\xff", "", "", "", "L\xffR"}, + {"InvalidAlternateMatch", "-.L\xffR.-", "\ufffd", "-.L", "R.-", "", "-.L\xffR.-"}, + {"LiteralReplacementControl", "L\ufffdR", "\ufffd", "L", "R", "", "L\ufffdR"}, + {"ComposedControl", "😀éZ", "e\u0301", "😀", "Z", "😀", "Z"}, + {"DecomposedControl", "😀e\u0301Z", "é", "😀", "Z", "😀", "\u0301Z"}, + {"EmptyNeedleControl", "\xffa", "", "", "\xffa", "", "\xffa"}, + {"EmptySourceControl", "", "\xff", "", "", "", ""}, + } + for _, profile := range profiles { + for _, tc := range cases { + for _, fn := range []string{"substring-before", "substring-after"} { + t.Run(profile.name+"/"+tc.name+"/"+fn, func(t *testing.T) { + // A panic is always a failed public evaluation, never an accepted legacy result. + defer func() { + if p := recover(); p != nil { + t.Fatalf("Evaluate panicked: %v", p) + } + }() + want := tc.before + if profile.projected { + want = tc.projectedBefore + } + if fn == "substring-after" { + want = tc.after + if profile.projected { + want = tc.projectedAfter + } + } + seq, err := queries[fn].Evaluate(t.Context(), nil, transform.XPathContext{Variables: transform.Bindings{ + {Local: "s"}: transform.Values(transform.String(tc.source)), + {Local: "needle"}: transform.Values(transform.String(tc.needle)), + {Local: "uri"}: transform.Values(transform.String(profile.uri)), + }}) + if err != nil { + t.Fatalf("Evaluate: %v", err) + } + text, err := seq.Text() + if err != nil { + t.Fatal(err) + } else if seq.Len() != 1 || text != want { + t.Fatalf("got %q count%d error%v; want %q", text, seq.Len(), err, want) + } + }) + } + } + } +} + +func TestUCAMalformedBindingInputAndErrorControls(t *testing.T) { + engine, err := transform.NewEngine(transform.Config{}) + if err != nil { + t.Fatal(err) + } + query, err := engine.CompileXPath(t.Context(), "$s", transform.XPathOptions{}) + if err != nil { + t.Fatal(err) + } + options := transform.XPathContext{Variables: transform.Bindings{{Local: "s"}: transform.Values(transform.String("\xffa"))}} + seq, err := query.Evaluate(t.Context(), nil, options) + if err != nil { + t.Fatal(err) + } + text, err := seq.Text() + if err != nil { + t.Fatal(err) + } else if seq.Len() != 1 || text != "\xffa" { + t.Fatalf("public binding changed bytes: %q count%d err%v", text, seq.Len(), err) + } + for _, tc := range []struct { + name string + ctx context.Context + want error + }{ + {"NilContext", nil, transform.ErrInput}, + {"Canceled", func() context.Context { ctx, cancel := context.WithCancel(context.Background()); cancel(); return ctx }(), context.Canceled}, + } { + t.Run(tc.name, func(t *testing.T) { + value, err := query.Evaluate(tc.ctx, nil, options) + if !errors.Is(err, tc.want) || value != (transform.Sequence{}) { + t.Fatalf("got result %+v error%v; want no result/%v", value, err, tc.want) + } + }) + } + limited, err := transform.NewEngine(transform.Config{Limits: transform.Limits{MaxOutputBytes: 256}}) + if err != nil { + t.Fatal(err) + } + limitedQuery, err := limited.CompileXPath(t.Context(), "substring-after($s,'a','http://www.w3.org/2013/collation/UCA')", transform.XPathOptions{}) + if err != nil { + t.Fatal(err) + } + value, err := limitedQuery.Evaluate(t.Context(), nil, transform.XPathContext{Variables: transform.Bindings{{Local: "s"}: transform.Values(transform.String(strings.Repeat("\xff", 257)))}}) + if !errors.Is(err, transform.ErrLimit) || value != (transform.Sequence{}) { + t.Fatalf("limit got result %+v error%v", value, err) + } +} + +func TestUCAMalformedBindingConcurrentQueryAndStylesheetReuse(t *testing.T) { + t.Parallel() + + engine, err := transform.NewEngine(transform.Config{}) + if err != nil { + t.Fatal(err) + } + query, err := engine.CompileXPath(t.Context(), "(substring-before($s,$needle,$uri),substring-after($s,$needle,$uri))", transform.XPathOptions{}) + if err != nil { + t.Fatal(err) + } + style, err := engine.CompileXSLT(t.Context(), []byte(``), transform.XSLTOptions{}) + if err != nil { + t.Fatal(err) + } + const base = "http://www.w3.org/2013/collation/UCA" + cases := []struct{ source, needle, uri, before, after string }{ + {"\xffa", "a", base, "\xff", ""}, + {"L\xffR", "\ufffd", base, "L", "R"}, + {"\xffaR", "a", base + "?strength=primary;alternate=shifted", "\xff", "R"}, + {"L\xffR", "\xff", base + "?strength=primary;alternate=shifted", "", "L\xffR"}, + } + for worker := range 8 { + t.Run(strconv.Itoa(worker), func(t *testing.T) { + t.Parallel() + defer func() { + if p := recover(); p != nil { + t.Fatalf("concurrent public invocation panicked: %v", p) + } + }() + for _, tc := range cases { + bindings := transform.Bindings{ + {Local: "s"}: transform.Values(transform.String(tc.source)), + {Local: "needle"}: transform.Values(transform.String(tc.needle)), + {Local: "uri"}: transform.Values(transform.String(tc.uri)), + } + value, err := query.Evaluate(t.Context(), nil, transform.XPathContext{Variables: bindings}) + if err != nil { + t.Fatal(err) + } + checkUCAMalformedSequencePair(t, value, tc.before, tc.after) + result, err := style.Execute(t.Context(), nil, transform.XSLTContext{Template: transform.Name{Local: "run"}, Parameters: bindings, ResultMode: transform.ResultRaw}) + if err != nil { + t.Fatal(err) + } + checkUCAMalformedSequencePair(t, result.Primary(), tc.before, tc.after) + } + }) + } +} + +func checkUCAMalformedSequencePair(t *testing.T, value transform.Sequence, before, after string) { + t.Helper() + if value.Len() != 2 { + t.Fatalf("got count%d; want2", value.Len()) + } + for i, want := range []string{before, after} { + item, ok := value.At(i) + if !ok { + t.Fatal("missing pair item") + } + text, err := item.Text() + if err != nil || text != want { + t.Fatalf("pair item%d got%q err%v want%q", i, text, err, want) + } + } +} + +func FuzzUCAMalformedBindingSubstrings(f *testing.F) { + engine, err := transform.NewEngine(transform.Config{}) + if err != nil { + f.Fatal(err) + } + query, err := engine.CompileXPath(context.Background(), "(substring-before($s,'a',$uri),substring-after($s,'a',$uri))", transform.XPathOptions{}) + if err != nil { + f.Fatal(err) + } + for _, seed := range []string{"", "a", "abc", "\xffa", "L\xffaR", "a\xff", "\xe2\x82a", "\xc0\xafa", "\xed\xa0\x80a", "😀éa", "ae\u0301", "\x00a"} { + f.Add(seed) + } + f.Fuzz(func(t *testing.T, source string) { + if len(source) > 64 { + t.Skip("bounded raw string representation") + } + for _, uri := range []string{ + "http://www.w3.org/2013/collation/UCA", + "http://www.w3.org/2013/collation/UCA?numeric=yes", + "http://www.w3.org/2013/collation/UCA?strength=primary;alternate=shifted", + } { + value, err := query.Evaluate(t.Context(), nil, transform.XPathContext{Variables: transform.Bindings{ + {Local: "s"}: transform.Values(transform.String(source)), + {Local: "uri"}: transform.Values(transform.String(uri)), + }}) + if err != nil { + t.Fatalf("Evaluate: %v", err) + } + if value.Len() != 2 { + t.Fatalf("got count%d; want2", value.Len()) + } + for i := range 2 { + item, ok := value.At(i) + if !ok { + t.Fatal("missing substring result") + } + text, err := item.Text() + if err != nil || len(text) > len(source) { + t.Fatalf("substring%d %q err%v exceeds raw input%q", i, text, err, source) + } + if i == 0 && !strings.HasPrefix(source, text) || i == 1 && !strings.HasSuffix(source, text) { + t.Fatalf("substring%d %q is not original prefix/suffix of%q", i, text, source) + } + } + } + }) +} + +func TestUCACandidatePublicProfilesAndOriginalOffsets(t *testing.T) { + engine, err := transform.NewEngine(transform.Config{}) + if err != nil { + t.Fatal(err) + } + query, err := engine.CompileXPath(t.Context(), "(contains($s,$n,$uri),starts-with($s,$n,$uri),ends-with($s,$n,$uri),substring-before($s,$n,$uri),substring-after($s,$n,$uri))", transform.XPathOptions{}) + if err != nil { + t.Fatal(err) + } + const base = "http://www.w3.org/2013/collation/UCA" + cases := []struct { + name, source, needle, uri string + contains, prefix, suffix bool + before, after string + }{ + {"EarlyASCII", "zaaaa", "z", base, true, true, false, "", "aaaa"}, + {"LateASCII", "aaaaz", "z", base, true, false, true, "aaaa", ""}, + {"MissASCII", "aaaa", "z", base, false, false, false, "", ""}, + {"Composed", "😀éZ", "e\u0301", base, true, false, false, "😀", "Z"}, + {"Decomposed", "😀e\u0301Z", "é", base, true, false, false, "😀", "Z"}, + {"NumericSplit", "100", "10", base + "?numeric=yes", false, false, false, "", ""}, + {"NonNumericSplit", "100", "10", base, true, true, false, "", "0"}, + {"NumericWhole", "x10y", "10", base + "?numeric=yes", true, false, false, "x", "y"}, + {"ProjectedBothEnds", "-.a.-", "a", base + "?strength=primary;alternate=shifted", true, true, true, "-.", ".-"}, + {"ProjectedOriginalOffsets", "-a é.-z", "e\u0301", base + "?strength=primary;alternate=shifted", true, false, false, "-a ", ".-z"}, + {"ProjectedIgnorable", "abc", "\u0301", base + "?strength=primary;alternate=shifted", true, true, true, "", "abc"}, + {"MalformedSource", "\xffa", "a", base, true, false, true, "\xff", ""}, + {"MalformedNeedle", "abc", "\xff", base, false, false, false, "", ""}, + {"MalformedProjectedNeedle", "abc", "\xff", base + "?strength=primary;alternate=shifted", true, true, true, "", "abc"}, + {"ValidReplacement", "\ufffda", "a", base, true, false, true, "\ufffd", ""}, + {"EmptyNeedle", "abc", "", base, true, true, true, "", "abc"}, + {"EmptySource", "", "a", base, false, false, false, "", ""}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + value, err := query.Evaluate(t.Context(), nil, transform.XPathContext{Variables: transform.Bindings{ + {Local: "s"}: transform.Values(transform.String(tc.source)), + {Local: "n"}: transform.Values(transform.String(tc.needle)), + {Local: "uri"}: transform.Values(transform.String(tc.uri)), + }}) + if err != nil { + t.Fatal(err) + } + if value.Len() != 5 { + t.Fatalf("got%d items; want5", value.Len()) + } + wants := []string{strconv.FormatBool(tc.contains), strconv.FormatBool(tc.prefix), strconv.FormatBool(tc.suffix), tc.before, tc.after} + for i, want := range wants { + item, ok := value.At(i) + if !ok { + t.Fatal("missing string operation result") + } + text, err := item.Text() + if err != nil || text != want { + t.Fatalf("item%d got%q err%v; want%q", i, text, err, want) + } + } + }) + } +} + +func TestXPathComprehensiveSemantics(t *testing.T) { + engine, err := transform.NewEngine(transform.Config{}) + if err != nil { + t.Fatal(err) + } + cases := []struct { + name, source, want string + compileCode, evalCode string + }{ + {name: "JSON placeholder escaped supplementary", source: `string-join(string-to-codepoints(parse-json('"\u0000\uDB80\uDC00"')) ! string(.),' ')`, want: "65533 983040"}, + {name: "JSON placeholder literal supplementary control", source: `string-join(string-to-codepoints(parse-json(concat('"\u0000',codepoints-to-string(983040),'"'))) ! string(.),' ')`, want: "65533 983040"}, + {name: "JSON escaped supplementary alone control", source: `string-join(string-to-codepoints(parse-json('"\uDB80\uDC00"')) ! string(.),' ')`, want: "983040"}, + {name: "JSON XML duplicates retain", source: `count(json-to-xml('{"a":1,"a":2}')/*/*)`, want: "2"}, + {name: "JSON XML duplicates usefirst control", source: `count(json-to-xml('{"a":1,"a":2}',map{'duplicates':'use-first'})/*/*)`, want: "1"}, + {name: "XML JSON invalid plus", source: `xml-to-json(parse-xml('+'))`, evalCode: "FOJS0006"}, + {name: "XML JSON invalid dot", source: `xml-to-json(parse-xml('.'))`, evalCode: "FOJS0006"}, + {name: "XML JSON valid plus integer control", source: `xml-to-json(parse-xml('+2'))`, want: "2"}, + {name: "EQName function local name", source: `local-name-from-QName(function-name(Q{http://www.w3.org/2005/xpath-functions/math}sqrt#1))`, want: "sqrt"}, + {name: "EQName function namespace", source: `namespace-uri-from-QName(function-name(Q{http://www.w3.org/2005/xpath-functions/math}sqrt#1))`, want: "http://www.w3.org/2005/xpath-functions/math"}, + {name: "prefixed function name control", source: `local-name-from-QName(function-name(math:sqrt#1))`, want: "sqrt"}, + {name: "dynamic array conversion", source: `[10]([(),1])`, want: "10"}, + {name: "array get conversion control", source: `array:get([10],[(),1])`, want: "10"}, + {name: "dynamic map conversion", source: `map{1:'v'}([(),1])`, want: "v"}, + {name: "map get conversion control", source: `map:get(map{1:'v'},[(),1])`, want: "v"}, + {name: "arrow local whitespace", source: `1 => fn: string()`, compileCode: "XPST0003"}, + {name: "arrow local comment", source: `1 => fn:(:x:)string()`, compileCode: "XPST0003"}, + {name: "arrow joined control", source: `1 => fn:string()`, want: "1"}, + {name: "keyword element type", source: `parse-xml('')/* instance of element(map)`, want: "true"}, + {name: "function without return grammar", source: `1 instance of function()`, compileCode: "XPST0003"}, + {name: "function with return control", source: `1 instance of function() as item()*`, want: "false"}, + {name: "reverse path order", source: `string-join(reverse(parse-xml('
')/*/*)/@id,',')`, want: "1,2"}, + {name: "reverse sequence control", source: `string-join(reverse(parse-xml('
')/*/*/@id),',')`, want: "2,1"}, + {name: "UCA prefix compressed", source: `starts-with('é','é','http://www.w3.org/2013/collation/UCA')`, want: "true"}, + {name: "UCA suffix compressed", source: `ends-with('é','é','http://www.w3.org/2013/collation/UCA')`, want: "true"}, + {name: "UCA ignorable empty prefix", source: `starts-with('',codepoints-to-string(769),'http://www.w3.org/2013/collation/UCA?strength=primary;alternate=shifted')`, want: "true"}, + {name: "UCA ignorable empty suffix", source: `ends-with('',codepoints-to-string(769),'http://www.w3.org/2013/collation/UCA?strength=primary;alternate=shifted')`, want: "true"}, + {name: "UCA ignorable punctuation prefix", source: `starts-with('-.',codepoints-to-string(769),'http://www.w3.org/2013/collation/UCA?strength=primary;alternate=shifted')`, want: "true"}, + {name: "UCA ignorable punctuation suffix", source: `ends-with('-.',codepoints-to-string(769),'http://www.w3.org/2013/collation/UCA?strength=primary;alternate=shifted')`, want: "true"}, + {name: "UCA empty compare control", source: `compare('',codepoints-to-string(769),'http://www.w3.org/2013/collation/UCA?strength=primary;alternate=shifted')`, want: "0"}, + {name: "UCA empty mismatch control", source: `starts-with('','a','http://www.w3.org/2013/collation/UCA?strength=primary;alternate=shifted')`, want: "false"}, + {name: "UCA contains composed control", source: `contains('é','é','http://www.w3.org/2013/collation/UCA')`, want: "true"}, + {name: "mathematical double struck", source: `format-integer(12,'𝟘𝟘')`, want: "𝟙𝟚"}, + {name: "mathematical bold control", source: `format-integer(12,'𝟎𝟎')`, want: "𝟏𝟐"}, + {name: "roman huge integer", source: `format-integer(18446744073709551616,'I')`, want: "18446744073709551616"}, + {name: "roman small control", source: `format-integer(12,'I')`, want: "XII"}, + {name: "JSON escaped supplementary first", source: `string-join(string-to-codepoints(parse-json('"\uDB80\uDC00\u0000"')) ! string(.),' ')`, want: "983040 65533"}, + {name: "JSON placeholders consecutive", source: `string-join(string-to-codepoints(parse-json('"\u0000\uDB80\uDC00\u0001\uDB80\uDC01"')) ! string(.),' ')`, want: "65533 983040 65533 983041"}, + {name: "JSON escape option", source: `parse-json('"\u0000\uDB80\uDC00"',map{'escape':true()}) eq concat('\u0000',codepoints-to-string(983040))`, want: "true"}, + {name: "JSON fallback option", source: `string-join(string-to-codepoints(parse-json('"\u0000\uDB80\uDC00"',map{'fallback':function($s){'X'}})) ! string(.),' ')`, want: "88 983040"}, + {name: "JSON invalid short and valid pair", source: `string-length(parse-json('"\b\f\uD800\uDFFF\u0000"'))`, want: "4"}, + {name: "JSON invalid fallback raw case", source: `parse-json('"\b\f\uD80A\u0000"',map{'fallback':function($s){concat('[',$s,']')}})`, want: `[\b][\f][\uD80A][\u0000]`}, + {name: "JSON invalid short escape control", source: `parse-json('"\b\f\u0000"',map{'escape':true()})`, want: `\b\f\u0000`}, + {name: "JSON literal backslash control", source: `parse-json('"\\u0000"')`, want: `\u0000`}, + {name: "JSON replacement duplicate key reject", source: `parse-json('{"\u0000":1,"\u0001":2}',map{'duplicates':'reject'})`, evalCode: "FOJS0003"}, + {name: "JSON replacement duplicate key first", source: `map:size(parse-json('{"\u0000":1,"\u0001":2}'))`, want: "1"}, + {name: "JSON escaped distinct keys control", source: `map:size(parse-json('{"\u0000":1,"\u0001":2}',map{'escape':true()}))`, want: "2"}, + {name: "JSON invalid key control", source: `string-to-codepoints(map:keys(parse-json('{"\u0000":0}')))`, want: "65533"}, + {name: "JSON liberal invalid fallback control", source: `parse-json('"\u0000"',map{'liberal':true(),'fallback':function($s){'X'}})`, want: "X"}, + {name: "JSON XML placeholder", source: `string-join(string-to-codepoints(string(json-to-xml('"\u0000\uDB80\uDC00"')/*)) ! string(.),' ')`, want: "65533 983040"}, + {name: "XML JSON invalid exponent mantissa", source: `xml-to-json(parse-xml('e2'))`, evalCode: "FOJS0006"}, + {name: "XML JSON valid leading point", source: `xml-to-json(parse-xml('.2'))`, want: "0.2"}, + {name: "XML JSON valid trailing point", source: `xml-to-json(parse-xml('2.'))`, want: "2"}, + {name: "partial array conversion", source: `let $f := [10](?) return $f([(),1])`, want: "10"}, + {name: "apply array conversion", source: `apply([10],[[(),1]])`, want: "10"}, + {name: "partial map conversion", source: `let $f := map{1:'v'}(?) return $f([(),1])`, want: "v"}, + {name: "apply map conversion", source: `apply(map{1:'v'},[[(),1]])`, want: "v"}, + {name: "array cardinality error", source: `[10]([1,2])`, evalCode: "XPTY0004"}, + {name: "array atomization error", source: `[10](map{})`, evalCode: "FOTY0013"}, + {name: "array type error", source: `[10]('1')`, evalCode: "XPTY0004"}, + {name: "array put bound error", source: `array:put([10],0,())`, evalCode: "FOAY0001"}, + {name: "array insert bound error", source: `array:insert-before([10],0,())`, evalCode: "FOAY0001"}, + {name: "array bound error", source: `[10](0)`, evalCode: "FOAY0001"}, + {name: "map cardinality error", source: `map{1:'v'}([1,2])`, evalCode: "XPTY0004"}, + {name: "keyword attribute type", source: `parse-xml('')/*/@* instance of attribute(if)`, want: "true"}, + {name: "prefixed keyword element type", source: `parse-xml('')/* instance of element(Q{urn:test}map)`, want: "true"}, + {name: "wrong kind name control", source: `parse-xml('')/* instance of element(other)`, want: "false"}, + {name: "sort path order", source: `string-join(sort(parse-xml('
')/*/*,(),function($x){-xs:integer($x/@id)})/@id,',')`, want: "1,2"}, + {name: "atomic path order control", source: `string-join(reverse(parse-xml('
')/*/*)/string(@id),',')`, want: "2,1"}, + {name: "UCA longer mismatch", source: `ends-with('é','xyz','http://www.w3.org/2013/collation/UCA')`, want: "false"}, + {name: "mathematical date digits", source: `format-date(xs:date('2000-01-12'),'[D𝟘𝟘]')`, want: "𝟙𝟚"}, + {name: "arabic decimal digits control", source: `format-integer(12,'٠٠')`, want: "١٢"}, + {name: "roman negative huge", source: `format-integer(-18446744073709551616,'i')`, want: "-18446744073709551616"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + q, err := engine.CompileXPath(context.Background(), tc.source, transform.XPathOptions{Version: transform.XPath31}) + if tc.compileCode != "" { + requireComprehensiveXPathCode(t, err, tc.compileCode) + return + } + if err != nil { + t.Fatal(err) + } + value, err := q.Evaluate(context.Background(), nil, transform.XPathContext{}) + if tc.evalCode != "" { + requireComprehensiveXPathCode(t, err, tc.evalCode) + return + } + if err != nil { + t.Fatal(err) + } + text, err := value.Text() + if err != nil { + t.Fatal(err) + } + if text != tc.want { + t.Errorf("got %q; want %q", text, tc.want) + } + }) + } +} + +func TestXPathComprehensiveIntermediateBudgets(t *testing.T) { + cases := []struct { + name, source string + limit transform.Limits + vars transform.Bindings + }{ + {name: "quantified every operations", source: `every $i in 1 to 5000 satisfies true()`, limit: transform.Limits{MaxOperations: 1000}}, + {name: "quantified some operations", source: `some $i in 1 to 5000 satisfies false()`, limit: transform.Limits{MaxOperations: 1000}}, + {name: "FLWOR empty return operations", source: `count(for $i in 1 to 5000 return ())`, limit: transform.Limits{MaxOperations: 1000}}, + {name: "FLWOR materialized return control", source: `count(for $i in 1 to 5000 return $i)`, limit: transform.Limits{MaxOperations: 1000}}, + {name: "array put intermediate item cap", source: `array:size(array:put([1,2,3,4,5,6],1,1 to 6))`, limit: transform.Limits{MaxNodes: 10}}, + {name: "array insert intermediate member cap", source: `array:size(array:insert-before(fold-left(1 to 10,[],function($a,$i){array:append($a,())}),1,()))`, limit: transform.Limits{MaxNodes: 10}}, + {name: "array insert intermediate item cap", source: `array:size(array:insert-before([1,2,3,4,5],1,1 to 6))`, limit: transform.Limits{MaxNodes: 10}}, + {name: "JSON string intermediate bytes", source: `count(for $i in 1 to 100 return string-length(parse-json('"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"')))`, limit: transform.Limits{MaxOutputBytes: 1024}}, + {name: "JSON escaped string intermediate bytes", source: `count(for $i in 1 to 100 return string-length(parse-json('"\u0000\u0001"',map{'escape':true()})))`, limit: transform.Limits{MaxOutputBytes: 1024}}, + {name: "JSON fallback string intermediate bytes", source: `count(for $i in 1 to 100 return string-length(parse-json('"\u0000"',map{'fallback':function($s){'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'}})))`, limit: transform.Limits{MaxOutputBytes: 1024}}, + {name: "format integer intermediate bytes", source: `count(for $i in 1 to 100 return string-length(format-integer($i,'00000000000000000000000000000000')))`, limit: transform.Limits{MaxOutputBytes: 1024}}, + {name: "format number intermediate bytes control", source: `count(for $i in 1 to 100 return string-length(format-number($i,'00000000000000000000000000000000')))`, limit: transform.Limits{MaxOutputBytes: 1024}}, + {name: "array append intermediate cap", source: `array:size(fold-left(1 to 10,[1],function($a,$i){array:append($a,())}))`, limit: transform.Limits{MaxNodes: 10}}, + {name: "array join intermediate cap control", source: `array:size(array:join(([1,2,3,4,5,6],[7,8,9,10,11])))`, limit: transform.Limits{MaxNodes: 10}}, + {name: "map constructor intermediate cap", source: `map:size(map{0:(),1:(),2:(),3:(),4:(),5:(),6:(),7:(),8:(),9:(),10:()})`, limit: transform.Limits{MaxNodes: 10}}, + {name: "map merge intermediate cap control", source: `map:size(map:merge((map{0:()},map{1:()},map{2:()},map{3:()},map{4:()},map{5:()},map{6:()},map{7:()},map{8:()},map{9:()},map{10:()})))`, limit: transform.Limits{MaxNodes: 10}}, + {name: "general duration constructor intermediate bytes", source: `count(xs:duration('PT0.` + strings.Repeat("123456789", 100) + `S'))`, limit: transform.Limits{MaxOutputBytes: 512}}, + {name: "year month duration constructor cumulative bytes", source: `count(for $i in 1 to 300 return xs:yearMonthDuration('P1Y'))`, limit: transform.Limits{MaxOutputBytes: 512}}, + {name: "day time sum intermediate bytes", source: `let $d:=xs:dayTimeDuration('PT0.123456789S') return count(for $i in 1 to 100 return sum(($d,$d)))`, limit: transform.Limits{MaxOutputBytes: 512}}, + {name: "year month sum intermediate bytes", source: `let $d:=xs:yearMonthDuration('P1Y') return count(for $i in 1 to 300 return sum(($d,$d)))`, limit: transform.Limits{MaxOutputBytes: 512}}, + {name: "year month average intermediate bytes", source: `let $d:=xs:yearMonthDuration('P1Y') return count(for $i in 1 to 300 return avg(($d,$d)))`, limit: transform.Limits{MaxOutputBytes: 512}}, + {name: "day time average intermediate bytes", source: `let $d:=xs:dayTimeDuration('PT0.123456789S') return count(for $i in 1 to 100 return avg(($d,$d)))`, limit: transform.Limits{MaxOutputBytes: 512}}, + {name: "date time timezone intermediate bytes", source: `let $d:=xs:dateTime('2000-01-01T00:00:00+01:00') return count(for $i in 1 to 200 return timezone-from-dateTime($d))`, limit: transform.Limits{MaxOutputBytes: 512}}, + {name: "date timezone intermediate bytes", source: `let $d:=xs:date('2000-01-01+01:00') return count(for $i in 1 to 200 return timezone-from-date($d))`, limit: transform.Limits{MaxOutputBytes: 512}}, + {name: "time timezone intermediate bytes", source: `let $d:=xs:time('00:00:00+01:00') return count(for $i in 1 to 200 return timezone-from-time($d))`, limit: transform.Limits{MaxOutputBytes: 512}}, + {name: "implicit timezone intermediate bytes", source: `count(for $i in 1 to 200 return implicit-timezone())`, limit: transform.Limits{MaxOutputBytes: 512}}, + {name: "day time addition intermediate bytes", source: `let $d:=xs:dayTimeDuration('PT0.123456789S') return count(for $i in 1 to 100 return $d+$d)`, limit: transform.Limits{MaxOutputBytes: 512}}, + {name: "year month addition intermediate bytes", source: `let $d:=xs:yearMonthDuration('P1Y') return count(for $i in 1 to 300 return $d+$d)`, limit: transform.Limits{MaxOutputBytes: 512}}, + {name: "duration multiply existing bytes control", source: `let $d:=xs:dayTimeDuration('PT0.123456789S') return count(for $i in 1 to 100 return $d*2)`, limit: transform.Limits{MaxOutputBytes: 512}}, + {name: "duration divide existing bytes control", source: `let $d:=xs:dayTimeDuration('PT0.123456789S') return count(for $i in 1 to 100 return $d div 2)`, limit: transform.Limits{MaxOutputBytes: 512}}, + {name: "duration constructor intermediate bytes", source: `count(xs:dayTimeDuration('PT0.` + strings.Repeat("123456789", 100) + `S'))`, limit: transform.Limits{MaxOutputBytes: 512}}, + {name: "duration constructor full result control", source: `xs:dayTimeDuration('PT0.` + strings.Repeat("123456789", 100) + `S')`, limit: transform.Limits{MaxOutputBytes: 512}}, + {name: "duration constructor cumulative bytes", source: `count(for $i in 1 to 100 return xs:dayTimeDuration('PT0.123456789S'))`, limit: transform.Limits{MaxOutputBytes: 512}}, + {name: "big integer range construction bytes", source: `count(100000000000000000000 to 100000000000000000099)`, limit: transform.Limits{MaxOutputBytes: 512}}, + {name: "map put intermediate cap", source: `map:size(fold-left(1 to 10,map{0:0},function($m,$i){map:put($m,$i,())}))`, limit: transform.Limits{MaxNodes: 10}}, + {name: "tokenize intermediate cap", source: `count(tokenize($s))`, limit: transform.Limits{MaxNodes: 10}, vars: transform.Bindings{{Local: "s"}: transform.Values(transform.String("a a a a a a a a a a a a a a a a a a a a"))}}, + {name: "regex tokenize intermediate cap", source: `count(tokenize($s,' '))`, limit: transform.Limits{MaxNodes: 10}, vars: transform.Bindings{{Local: "s"}: transform.Values(transform.String("a a a a a a a a a a a a a a a a a a a a"))}}, + {name: "RNG permute operations", source: `count(random-number-generator(1)?permute(1 to 2000))`, limit: transform.Limits{MaxOperations: 1000}}, + {name: "reverse operations control", source: `count(reverse(1 to 2000))`, limit: transform.Limits{MaxOperations: 1000}}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + e, err := transform.NewEngine(transform.Config{Limits: tc.limit}) + if err != nil { + t.Fatal(err) + } + q, err := e.CompileXPath(context.Background(), tc.source, transform.XPathOptions{}) + if err != nil { + t.Fatal("compile", err) + } + got, err := q.Evaluate(context.Background(), nil, transform.XPathContext{Variables: tc.vars}) + if err == nil { + text, _ := got.Text() + t.Errorf("accepted oversized intermediate/work: %s", text) + } else if !errors.Is(err, transform.ErrLimit) { + t.Fatal("limit category lost", err) + } + }) + } +} + +func TestXPathComprehensivePredicateCollation(t *testing.T) { + e, err := transform.NewEngine(transform.Config{}) + if err != nil { + t.Fatal(err) + } + for _, source := range []string{`count(parse-xml('
')/*/*[@id='a'])`, `count(parse-xml('
')/*/*[string(@id)='a'])`, `count(parse-xml('
')//*[@id='a'])`} { + t.Run(source, func(t *testing.T) { + q, err := e.CompileXPath(context.Background(), source, transform.XPathOptions{DefaultCollation: "http://www.w3.org/2005/xpath-functions/collation/html-ascii-case-insensitive"}) + if err != nil { + t.Fatal(err) + } + got, err := q.Evaluate(context.Background(), nil, transform.XPathContext{}) + if err != nil { + t.Fatal(err) + } + text, _ := got.Text() + if text != "1" { + t.Errorf("got %s, want 1", text) + } + }) + } +} + +func requireComprehensiveXPathCode(t *testing.T, err error, code string) { + t.Helper() + var detail *transform.Error + if !errors.As(err, &detail) || detail.Code != code { + t.Fatalf("got %v; want %s", err, code) + } +} + +func TestXPathComprehensiveBudgetBoundaries(t *testing.T) { + engine, err := transform.NewEngine(transform.Config{Limits: transform.Limits{MaxNodes: 10, MaxOperations: 1000}}) + if err != nil { + t.Fatal(err) + } + for _, source := range []string{ + `array:size(fold-left(1 to 10,[],function($a,$i){array:append($a,())}))=10`, + `array:size(array:put([1,2,3,4,5],1,1 to 6))=5`, + `array:size(array:insert-before([1,2,3,4],1,1 to 6))=5`, + `array:size(array:put(fold-left(1 to 10,[],function($a,$i){array:append($a,())}),1,()))=10`, + `format-integer(12,'0000')='0012'`, + `string-length(parse-json('"abcdefghij"'))=10`, + `map:size(map{0:(),1:(),2:(),3:(),4:(),5:(),6:(),7:(),8:(),9:()})=10`, + `map:size(fold-left(1 to 10,map:merge(for $i in 1 to 10 return map{$i:()}),function($m,$i){map:put($m,$i,())}))=10`, + `count(tokenize('a a a a a a a a a a'))=10`, + `count(tokenize('a a a a a a a a a a',' '))=10`, + `count(random-number-generator(1)?permute(1 to 10))=10`, + `every $i in 1 to 10 satisfies true()`, + `not(some $i in 1 to 10 satisfies false())`, + `count(for $i in 1 to 10 return ())=0`, + } { + t.Run(source, func(t *testing.T) { + program, err := engine.CompileXPath(context.Background(), source, transform.XPathOptions{}) + if err != nil { + t.Fatal(err) + } + value, err := program.Evaluate(context.Background(), nil, transform.XPathContext{}) + if err != nil { + t.Fatal(err) + } + text, err := value.Text() + if err != nil || text != "true" { + t.Fatalf("got %q, %v", text, err) + } + }) + } +} + +func TestXPathComprehensiveJSONCapacity(t *testing.T) { + engine, err := transform.NewEngine(transform.Config{}) + if err != nil { + t.Fatal(err) + } + program, err := engine.CompileXPath(context.Background(), `string-length(parse-json($s))`, transform.XPathOptions{}) + if err != nil { + t.Fatal(err) + } + var privateUse strings.Builder + for r := rune(0xF0000); r <= 0xFFFFD; r++ { + privateUse.WriteRune(r) + } + for _, tc := range []struct { + name, input string + want int + }{ + {"small replacement control", `"` + strings.Repeat(`\u0000`, 16) + `"`, 16}, + {"repeated invalid escape", `"` + strings.Repeat(`\u0000`, 65535) + `"`, 65535}, + {"all prior private use markers", `"` + privateUse.String() + `\u0000"`, 65535}, + } { + t.Run(tc.name, func(t *testing.T) { + value, err := program.Evaluate(context.Background(), nil, transform.XPathContext{Variables: transform.Bindings{{Local: "s"}: transform.Values(transform.String(tc.input))}}) + if err != nil { + t.Fatal(err) + } + text, err := value.Text() + if err != nil || text != strconv.Itoa(tc.want) { + t.Fatalf("got %q, %v; want %d", text, err, tc.want) + } + }) + } + for _, liberal := range []bool{false, true} { + t.Run("raw control liberal="+strconv.FormatBool(liberal), func(t *testing.T) { + query, err := engine.CompileXPath(context.Background(), `parse-json($s,map{'liberal':$liberal})`, transform.XPathOptions{}) + if err != nil { + t.Fatal(err) + } + _, err = query.Evaluate(context.Background(), nil, transform.XPathContext{Variables: transform.Bindings{ + {Local: "s"}: transform.Values(transform.String("\"\x00\"")), + {Local: "liberal"}: transform.Values(transform.Boolean(liberal)), + }}) + requireComprehensiveXPathCode(t, err, "FOJS0001") + }) + } +} + +func TestXPathComprehensiveStringBudgetBoundaries(t *testing.T) { + engine, err := transform.NewEngine(transform.Config{Limits: transform.Limits{MaxOutputBytes: 1024}}) + if err != nil { + t.Fatal(err) + } + for _, tc := range []struct{ source, want string }{ + {`count(100000000000000000000 to 100000000000000000002)`, "3"}, + {`count(1 to 2000)`, "2000"}, + {`string-length(parse-json('"` + strings.Repeat("a", 1000) + `"'))`, "1000"}, + {`string-length(parse-json('"\u0000"',map{'fallback':function($s){'` + strings.Repeat("a", 1000) + `'}}))`, "1000"}, + {`string-length(format-integer(12,'` + strings.Repeat("0", 500) + `'))`, "500"}, + } { + t.Run(tc.want+tc.source[:min(24, len(tc.source))], func(t *testing.T) { + program, err := engine.CompileXPath(context.Background(), tc.source, transform.XPathOptions{}) + if err != nil { + t.Fatal(err) + } + value, err := program.Evaluate(context.Background(), nil, transform.XPathContext{}) + if err != nil { + t.Fatal(err) + } + text, err := value.Text() + if err != nil || text != tc.want { + t.Fatalf("got %q,%v; want %q", text, err, tc.want) + } + }) + } +} + +func TestXPathComprehensiveUCAIgnorable(t *testing.T) { + engine, err := transform.NewEngine(transform.Config{}) + if err != nil { + t.Fatal(err) + } + for _, suffix := range []string{"?strength=primary", "?strength=primary;alternate=shifted"} { + uri := "http://www.w3.org/2013/collation/UCA" + suffix + for _, tc := range []struct{ source, want string }{ + {`contains('',codepoints-to-string(769),'` + uri + `')`, "true"}, + {`contains('abc',codepoints-to-string(769),'` + uri + `')`, "true"}, + {`starts-with('abc',codepoints-to-string(769),'` + uri + `')`, "true"}, + {`ends-with('abc',codepoints-to-string(769),'` + uri + `')`, "true"}, + {`substring-before('abc',codepoints-to-string(769),'` + uri + `')`, ""}, + {`substring-after('abc',codepoints-to-string(769),'` + uri + `')`, "abc"}, + {`substring-after('-.',codepoints-to-string(769),'` + uri + `')`, "-."}, + {`compare('',codepoints-to-string(769),'` + uri + `')`, "0"}, + {`contains('abc',codepoints-to-string(769),'http://www.w3.org/2005/xpath-functions/collation/codepoint')`, "false"}, + {`contains('abc',codepoints-to-string(769),'http://www.w3.org/2013/collation/UCA?strength=tertiary')`, "false"}, + } { + t.Run(tc.source, func(t *testing.T) { + query, err := engine.CompileXPath(context.Background(), tc.source, transform.XPathOptions{}) + if err != nil { + t.Fatal(err) + } + value, err := query.Evaluate(context.Background(), nil, transform.XPathContext{}) + if err != nil { + t.Fatal(err) + } + text, err := value.Text() + if err != nil || text != tc.want { + t.Fatalf("got %q,%v; want %q", text, err, tc.want) + } + }) + } + } +} + +func TestXPathComprehensiveDurationConstructorBoundaries(t *testing.T) { + engine, err := transform.NewEngine(transform.Config{Limits: transform.Limits{MaxOutputBytes: 512}}) + if err != nil { + t.Fatal(err) + } + for _, tc := range []struct{ name, source, want string }{ + {"canonical short despite long input", `string(xs:dayTimeDuration('PT0.` + strings.Repeat("0", 900) + `S'))`, "PT0S"}, + {"exact day time fraction", `string(xs:dayTimeDuration('PT0.1234567890123456789S'))`, "PT0.1234567890123456789S"}, + {"day time exact addition", `let $d:=xs:dayTimeDuration('PT0.123456789S') return string($d+$d)`, "PT0.246913578S"}, + {"year month addition", `let $d:=xs:yearMonthDuration('P1Y') return string($d+$d)`, "P2Y"}, + {"duration ratio numeric type", `(xs:dayTimeDuration('PT1S') div xs:dayTimeDuration('PT2S')) instance of xs:decimal`, "true"}, + {"duration result type", `(xs:dayTimeDuration('PT1S')+xs:dayTimeDuration('PT2S')) instance of xs:dayTimeDuration`, "true"}, + {"date result type", `(xs:date('2000-01-01')+xs:dayTimeDuration('P1D')) instance of xs:date`, "true"}, + {"day time exact sum", `string(sum((xs:dayTimeDuration('PT0.123456789S'),xs:dayTimeDuration('PT0.123456789S'))))`, "PT0.246913578S"}, + {"day time exact average", `string(avg((xs:dayTimeDuration('PT0.123456789S'),xs:dayTimeDuration('PT0.123456789S'))))`, "PT0.123456789S"}, + {"year month sum", `string(sum((xs:yearMonthDuration('P1Y'),xs:yearMonthDuration('P2Y'))))`, "P3Y"}, + {"year month average", `string(avg((xs:yearMonthDuration('P1Y'),xs:yearMonthDuration('P2Y'))))`, "P1Y6M"}, + {"negative date time timezone", `string(timezone-from-dateTime(xs:dateTime('2000-01-01T00:00:00-03:30')))`, "-PT3H30M"}, + {"negative date timezone", `string(timezone-from-date(xs:date('2000-01-01-03:30')))`, "-PT3H30M"}, + {"negative time timezone", `string(timezone-from-time(xs:time('00:00:00-03:30')))`, "-PT3H30M"}, + {"absent timezone", `count(timezone-from-dateTime(xs:dateTime('2000-01-01T00:00:00')))`, "0"}, + {"empty timezone argument", `count(timezone-from-dateTime(()))`, "0"}, + {"implicit timezone default", `string(implicit-timezone())`, "PT0S"}, + {"year month canonical", `string(xs:yearMonthDuration('P12M'))`, "P1Y"}, + {"general duration fraction", `string(xs:duration('P1Y2MT0.1234567890123456789S'))`, "P1Y2MT0.1234567890123456789S"}, + } { + t.Run(tc.name, func(t *testing.T) { + query, err := engine.CompileXPath(context.Background(), tc.source, transform.XPathOptions{}) + if err != nil { + t.Fatal(err) + } + value, err := query.Evaluate(context.Background(), nil, transform.XPathContext{}) + if err != nil { + t.Fatal(err) + } + text, err := value.Text() + if err != nil || text != tc.want { + t.Fatalf("got %q,%v; want %q", text, err, tc.want) + } + }) + } +} + +func TestXPathComprehensiveReuseContext(t *testing.T) { + for _, tc := range []struct{ name, scope, sortAttr, sortKey, extraSort, want string }{ + {"local sort static collation", "", `default-collation="http://www.w3.org/2005/xpath-functions/collation/html-ascii-case-insensitive"`, `if ('A'='a') then xs:integer(@n) else -xs:integer(@n)`, "", "12"}, + {"parent collation control", `default-collation="http://www.w3.org/2005/xpath-functions/collation/html-ascii-case-insensitive"`, "", `if ('A'='a') then xs:integer(@n) else -xs:integer(@n)`, "", "12"}, + {"codepoint control", "", "", `if ('A'='a') then xs:integer(@n) else -xs:integer(@n)`, "", "21"}, + {"explicit codepoint override", `default-collation="http://www.w3.org/2005/xpath-functions/collation/html-ascii-case-insensitive"`, `default-collation="http://www.w3.org/2005/xpath-functions/collation/codepoint"`, `if ('A'='a') then xs:integer(@n) else -xs:integer(@n)`, "", "21"}, + {"restore base between sort keys", "", `default-collation="http://www.w3.org/2005/xpath-functions/collation/html-ascii-case-insensitive"`, `if ('A'='a') then 0 else 0`, ``, "21"}, + {"pinned time control", "", "", `if (year-from-date(current-date())=2000) then xs:integer(@n) else -xs:integer(@n)`, "", "12"}, + } { + t.Run(tc.name, func(t *testing.T) { + engine, err := transform.NewEngine(transform.Config{}) + if err != nil { + t.Fatal(err) + } + source := `` + tc.extraSort + `` + sheet, err := engine.CompileXSLT(context.Background(), []byte(source), transform.XSLTOptions{}) + if err != nil { + t.Fatal(err) + } + doc, err := engine.Parse(context.Background(), []byte(`
`)) + if err != nil { + t.Fatal(err) + } + result, err := sheet.Transform(context.Background(), doc, transform.XSLTContext{Context: transform.DynamicContext{CurrentTime: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)}}) + if err != nil { + t.Fatal(err) + } + if string(result.Primary) != tc.want { + t.Fatalf("got %q; want %q", result.Primary, tc.want) + } + }) + } +} + +func TestXPathISOWeekWithinMonthCalendarDates(t *testing.T) { + e, err := transform.NewEngine(transform.Config{}) + if err != nil { + t.Fatal(err) + } + for _, tc := range []struct { + name, source, place string + want int + }{ + {"March3_NY", "2025-03-03T12:00:00-05:00", "America/New_York", 1}, + {"March10_NY", "2025-03-10T12:00:00-04:00", "America/New_York", 2}, + {"March17_NY", "2025-03-17T12:00:00-04:00", "America/New_York", 3}, + {"March3_UTC", "2025-03-03T12:00:00-05:00", "UTC", 1}, + {"March10_UTC", "2025-03-10T12:00:00-04:00", "UTC", 2}, + {"March17_UTC", "2025-03-17T12:00:00-04:00", "UTC", 3}, + {"March3_Fixed", "2025-03-03T12:00:00-05:00", "Etc/GMT+4", 1}, + {"March10_Fixed", "2025-03-10T12:00:00-04:00", "Etc/GMT+4", 2}, + {"March17_Fixed", "2025-03-17T12:00:00-04:00", "Etc/GMT+4", 3}, + {"FallOctober27", "2025-10-27T12:00:00-04:00", "America/New_York", 5}, + {"FallNovember3", "2025-11-03T12:00:00-05:00", "America/New_York", 1}, + {"FallNovember10", "2025-11-10T12:00:00-05:00", "America/New_York", 2}, + {"December30", "2024-12-30T12:00:00-05:00", "America/New_York", 1}, + {"January1", "2025-01-01T12:00:00-05:00", "America/New_York", 1}, + {"W3CJanuary29", "2013-01-29T12:00:00-05:00", "America/New_York", 5}, + {"W3CFebruary1", "2013-02-01T12:00:00-05:00", "America/New_York", 5}, + {"LeapFebruary29", "2024-02-29T12:00:00-05:00", "America/New_York", 5}, + {"AfterLeapMarch1", "2024-03-01T12:00:00-05:00", "America/New_York", 5}, + } { + t.Run(tc.name, func(t *testing.T) { + if _, err := time.LoadLocation(tc.place); err != nil { + t.Fatal(err) + } + expression := `format-dateTime(xs:dateTime('` + tc.source + `'),'[w]','en','ISO','` + tc.place + `')` + query, err := e.CompileXPath(t.Context(), expression, transform.XPathOptions{}) + if err != nil { + t.Fatal(err) + } + result, err := query.Evaluate(t.Context(), nil, transform.XPathContext{}) + if err != nil { + t.Fatal(err) + } + text, err := result.Text() + if err != nil { + t.Fatal(err) + } else if result.Len() != 1 || text != strconv.Itoa(tc.want) { + t.Fatalf("got %q (length %d), %v; want %d", text, result.Len(), err, tc.want) + } + }) + } +} + +func TestXPathNameTestsEmptyNamespace(t *testing.T) { + e, err := transform.NewEngine(transform.Config{}) + if err != nil { + t.Fatal(err) + } + d, err := e.Parse(t.Context(), []byte(`plainnamespaced`)) + if err != nil { + t.Fatal(err) + } + for _, defaultNS := range []string{"", "urn:other"} { + bare := "plain" + if defaultNS != "" { + bare = "namespaced" + } + for _, tc := range []struct{ name, source, want string }{ + {"explicit empty exact", `string-join(/*/Q{}n/string(), ',')`, "plain"}, + {"explicit empty wildcard", `count(/*/Q{}*)`, "1"}, + {"nonempty exact control", `string-join(/*/Q{urn:other}n/string(), ',')`, "namespaced"}, + {"nonempty wildcard control", `count(/*/Q{urn:other}*)`, "1"}, + {"missing namespace control", `count(/*/Q{urn:missing}*)`, "0"}, + {"bare namespace control", `string-join(/*/n/string(), ',')`, bare}, + {"universal wildcard control", `count(/*/*)`, "2"}, + } { + t.Run(fmt.Sprintf("default=%s/%s", defaultNS, tc.name), func(t *testing.T) { + q, err := e.CompileXPath(t.Context(), tc.source, transform.XPathOptions{Version: transform.XPath31, Namespaces: map[string]string{"": defaultNS}}) + if err != nil { + t.Fatal(err) + } + got, err := q.Evaluate(t.Context(), d, transform.XPathContext{}) + if err != nil { + t.Fatal(err) + } + s, err := got.Text() + if err != nil { + t.Fatal(err) + } + t.Logf("source=%q defaultNS=%q actual=%q expected=%q", tc.source, defaultNS, s, tc.want) + if s != tc.want { + t.Errorf("actual %q; expected %q", s, tc.want) + } + }) + } + } +} + +func TestXPathKindTestGrammar(t *testing.T) { + e, err := transform.NewEngine(transform.Config{}) + if err != nil { + t.Fatal(err) + } + for _, version := range []transform.XPathVersion{transform.XPath20, transform.XPath30, transform.XPath31} { + for _, tc := range []struct{ source, code string }{ + {`element(,xs:untyped)`, "XPST0003"}, + {`element(n,)`, "XPST0003"}, + {`element(*,)`, "XPST0003"}, + {`attribute(,xs:untypedAtomic)`, "XPST0003"}, + {`attribute(a,)`, "XPST0003"}, + {`attribute(*,)`, "XPST0003"}, + {`document-node(element(n,))`, "XPST0003"}, + {`() instance of element(n,)`, "XPST0003"}, + {`element(n,,xs:untyped)`, "XPST0003"}, + {`element()`, ""}, + {`element(n)`, ""}, + {`element(*)`, ""}, + {`element(*,xs:untyped)`, ""}, + {`attribute()`, ""}, + {`attribute(a)`, ""}, + {`attribute(*)`, ""}, + {`attribute(*,xs:untypedAtomic)`, ""}, + {`document-node(element(n))`, ""}, + {`() instance of element(n)`, ""}, + } { + t.Run(fmt.Sprintf("%d/%s", version, tc.source), func(t *testing.T) { + q, err := e.CompileXPath(t.Context(), tc.source, transform.XPathOptions{Version: version}) + gotCode := "" + if detail, ok := errors.AsType[*transform.Error](err); ok { + gotCode = detail.Code + } + t.Logf("source=%q actualError=%v actualCode=%q expectedCode=%q queryNil=%t", tc.source, err, gotCode, tc.code, q == nil) + if tc.code == "" { + if err != nil || q == nil { + t.Errorf("valid control rejected: query=%v error=%v", q, err) + } + } else if err == nil || gotCode != tc.code || q != nil { + t.Errorf("actual queryNil=%t/code=%q/error=%v; expected nil query/%s", q == nil, gotCode, err, tc.code) + } + }) + } + } +} + +func TestXSLTNameTestsEmptyNamespace(t *testing.T) { + e, err := transform.NewEngine(transform.Config{}) + if err != nil { + t.Fatal(err) + } + doc, err := e.Parse(t.Context(), []byte(`plainnamespaced`)) + if err != nil { + t.Fatal(err) + } + for _, padding := range []int{0, 16} { + var extra strings.Builder + for i := range padding { + fmt.Fprintf(&extra, `unused`, i) + } + for _, tc := range []struct{ name, decl, body, want string }{ + {"explicit empty exact", `wild|other|empty|`, ``, "empty|other|"}, + {"explicit empty wildcard", `wild|empty|`, ``, "empty|wild|"}, + {"wildcard default priority", `empty|wild|`, ``, "empty|wild|"}, + {"key namespace", ``, `|`, "1|0"}, + {"nonempty exact control", `wild|other|`, ``, "wild|other|"}, + {"universal wildcard control", `wild|`, ``, "wild|wild|"}, + } { + t.Run(fmt.Sprintf("padding%d/%s", padding, tc.name), func(t *testing.T) { + style := `` + extra.String() + tc.decl + `` + tc.body + `` + s, err := e.CompileXSLT(t.Context(), []byte(style), transform.XSLTOptions{}) + if err != nil { + t.Fatal(err) + } + out, err := s.Transform(t.Context(), doc, transform.XSLTContext{}) + if err != nil || out == nil { + t.Fatalf("output=%v error=%v", out, err) + } + t.Logf("actual=%q expected=%q", out.Primary, tc.want) + if string(out.Primary) != tc.want { + t.Errorf("actual %q; expected %q", out.Primary, tc.want) + } + }) + } + } +} diff --git a/transform/xslt.go b/transform/xslt.go index e942940..42380df 100644 --- a/transform/xslt.go +++ b/transform/xslt.go @@ -77,15 +77,43 @@ type XSLTContext struct { // BaseOutputURI resolves secondary result URIs. Without a base, relative // result URIs remain relative. No file is written implicitly. BaseOutputURI string - selection *Sequence - function Name - arguments []Sequence + // selection optionally replaces the initial match selection independently of + // global context. + selection *Sequence + // function identifies a named function entry point when selected by the + // invocation API. + function Name + // arguments retains positional arguments for the named function entry point. + arguments []Sequence +} + +// Message retains xsl:message output without publishing it to a global stream. +type Message struct { + // Text is the serialized xsl:message content. + Text string + // Terminate reports whether the message requested termination. + Terminate bool +} + +// Output contains owned serialized primary and secondary result bytes and messages. +// The application receives secondary documents; serialization does not write files +// for them. +type Output struct { + // Primary contains the owned serialized principal result bytes. + Primary []byte + // Secondary maps resolved result URIs to owned bytes. Entries never write files. + Secondary map[string][]byte + // Messages contains xsl:message output in execution order. + Messages []Message } // Stylesheet retains immutable compiled XSLT and supports concurrent transformations. type Stylesheet struct { - engine *Engine - native *xslt.Stylesheet + // engine retains immutable execution policy and application adapters. + engine *Engine + // native is the reusable compiled stylesheet. + native *xslt.Stylesheet + // version records the selected XSLT processor language. version XSLTVersion } @@ -290,34 +318,6 @@ func (s *Stylesheet) initialInvocation(ctx context.Context, source *xdm.Document return invocation, nil } -func traceWriter(w io.Writer) io.Writer { - if w == nil { - return io.Discard - } - - return w -} - -// Message retains xsl:message output without publishing it to a global stream. -type Message struct { - // Text is the serialized xsl:message content. - Text string - // Terminate reports whether the message requested termination. - Terminate bool -} - -// Output contains owned serialized primary and secondary result bytes and messages. -// The application receives secondary documents; serialization does not write files -// for them. -type Output struct { - // Primary contains the owned serialized principal result bytes. - Primary []byte - // Secondary maps resolved result URIs to owned bytes. Entries never write files. - Secondary map[string][]byte - // Messages contains xsl:message output in execution order. - Messages []Message -} - // CompileXSLT compiles a stylesheet under the same policy as CompileXPath. func (e *Engine) CompileXSLT(ctx context.Context, source []byte, options XSLTOptions) (*Stylesheet, error) { ctx, cancel, err := e.invocation(ctx) @@ -337,18 +337,19 @@ func (e *Engine) CompileXSLT(ctx context.Context, source []byte, options XSLTOpt native, err := parse.Parse(ctx, source) if err != nil { - return nil, err + return nil, wrap("compile XSLT", err) } return e.compileStylesheet(ctx, native, options) } -// CompileXSLTReader incrementally reads a stylesheet under the program byte limit. +// CompileXSLTReader reads a stylesheet under the program byte limit. Language +// parsing can retain decoded and DTD-expanded input alongside the parsed tree. // It does not close src. The compiled stylesheet can be reused concurrently. func (e *Engine) CompileXSLTReader(ctx context.Context, src io.Reader, options XSLTOptions) (*Stylesheet, error) { ctx, cancel, err := e.invocation(ctx) if err != nil { - return nil, err + return nil, wrap("compile XSLT", err) } defer cancel() @@ -374,7 +375,7 @@ func (e *Engine) compileStylesheet(ctx context.Context, doc *xdm.Document, optio return nil, xsltError("compile XSLT", err) } - if len(options.StaticParameters) > 0 && version < 30 { + if len(options.StaticParameters) > 0 && version < int(XSLT30) { return nil, wrap("compile XSLT", ErrUnsupported) } @@ -402,7 +403,15 @@ func (e *Engine) compileStylesheet(ctx context.Context, doc *xdm.Document, optio return nil, xsltError("compile XSLT", err) } - return &Stylesheet{engine: e, native: sheet, version: map[int]XSLTVersion{10: XSLT10, 20: XSLT20, 30: XSLT30}[version]}, nil + return &Stylesheet{engine: e, native: sheet, version: map[int]XSLTVersion{int(XSLT10): XSLT10, int(XSLT20): XSLT20, int(XSLT30): XSLT30}[version]}, nil +} + +func traceWriter(w io.Writer) io.Writer { + if nilcheck.IsNil(w) { + return io.Discard + } + + return w } func xsltError(operation string, err error) error { diff --git a/transform/xslt_test.go b/transform/xslt_test.go index 6991ab8..6d633c5 100644 --- a/transform/xslt_test.go +++ b/transform/xslt_test.go @@ -5,12 +5,18 @@ import ( "context" "encoding/xml" "errors" + "fmt" "io" "strconv" "strings" "testing" + "testing/iotest" + "time" + "github.com/skarm/xmlsec/document" "github.com/skarm/xmlsec/transform" + "github.com/skarm/xmlsec/transform/internal/xdm" + "github.com/skarm/xmlsec/transform/internal/xpath" ) const nameKept = "kept" @@ -1145,7 +1151,9 @@ func TestXSLTStandaloneAttributeValues(t *testing.T) { q := query(t, e, `count(/out/@*) = 5 and /out/@count = '3' and /out/@parents = '0' and /out/@plain = 'p&q`, `ok`, "XTSE0090", "XTSE3155"}, - {"streamability attribute set reference", ``, `ok`, "XTSE0090", "XTSE0730"}, - {"accumulator local declaration namespace", ``, ``, "XTSE0010", "12"}, - {"accumulator absent context", ``, ``, "XTSE0010", "XTDE3350"}, - {"accumulator atomic context", ``, ``, "XTSE0010", "XTTE3360"}, - {"accumulator unbound name prefix", ``, ``, "XTSE0010", "XTDE3340"}, - {"copy function mixed sequence", ``, ``, "XPST0017", "true"}, - {"copy function atomic context", "", ``, "XPST0017", "42"}, - {"copy function absent context", "", ``, "XPST0017", "XPDY0002"}, - {"snapshot document accumulator values", ``, ``, "XTSE0010", "true"}, - {"snapshot function atomic context", "", ``, "XPST0017", "42"}, - {"snapshot function absent context", "", ``, "XPST0017", "XPDY0002"}, - {"snapshot function mixed sequence", ``, ``, "XPST0017", "true"}, - {"sort binary octets", "", ``, "XTDE1030", "AAAA////"}, - {"sort binary explicit text", "", ``, "////AAAA", "////AAAA"}, - {"sort binary multiple keys", "", ``, "XTDE1030", "AAAA////"}, - {"merge binary octets", "", ``, "XTSE0010", "AAAA////"}, - {"sort relative collation", ``, ``, "ab", "ab"}, - {"sort explicit collation ignores dynamic case", ``, ``, "ab", "ab"}, - {"sort ignored option static function", ``, ``, "XPST0017", "XPST0017"}, - {"sort ignored option malformed AVT", ``, ``, "XTSE0350", "XTSE0350"}, - {"sort language lexical value", ``, ``, "XTDE0030", "XTDE0030"}, - {"sort empty string ordering", ``, ``, "emptystring", "emptystring"}, - {"sort empty numeric ordering", ``, ``, "emptyNaNzero", "emptyNaNzero"}, - {"group lexical default collation", ``, ``, "2", "2"}, - {"merge order difference", ``, ``, "XTSE0010", "XTDE2210"}, - {"merge effective collation", ``, ``, "XTSE0010", "ab"}, - {"merge case avt", ``, ``, "XTSE0010", "Aa"}, - {"merge bad order", ``, ``, "XTSE0010", "XTDE0030"}, - {"merge text number", ``, ``, "XTSE0010", "1,10,2,"}, - {"merge collation equality", ``, ``, "XTSE0010", "2"}, - {"merge presort collation", ``, ``, "XTSE0010", "aA"}, - {"merge empty string", ``, ``, "XTSE0010", "0:1:0;1:1:1;"}, - {"merge nan group", ``, ``, "XTSE0010", "2"}, - {"merge relative collation", ``, ``, "XTSE0010", "ab"}, - {"merge option presence differs", ``, ``, "XTSE0010", "XTDE2210"}, - {"merge ignored language differs", ``, ``, "XTSE0010", "XTDE2210"}, - {"merge source local namespace", ``, ``, "XTSE0010", "12"}, - {"merge key local namespace", ``, ``, "XTSE0010", "12"}, - } -} - -func numberingContractCases() []hostFunctionCase { - return []hostFunctionCase{ - {"number array atomization", "", ``, "XPST0003", "1.2"}, - {"number large integer double conversion", "", ``, "9007199254740992", "9007199254740992"}, - {"number negative decimal half", "", ``, "0", "0"}, - {"number negative double half", "", ``, "0", "0"}, - {"number negative value rejected", "", ``, "XTDE0980", "XTDE0980"}, - {"number node position single", ``, ``, "123", "123"}, - {"number node position multiple", `
`, `;`, "1.1;1.2;2.1;", "1.1;1.2;2.1;"}, - {"number node position any", ``, ``, "121", "121"}, - {"number grouping size dynamic", "", ``, "XTDE0030", "XTDE0030"}, - {"number grouping size empty", "", ``, "XTSE0020", "XTSE0020"}, - {"number grouping size XML whitespace", "", ``, "1,234", "1,234"}, - {"number grouping size large", "", ``, "1234", "1234"}, - {"number grouping size negative", "", ``, "1234", "1234"}, - {"number grouping incomplete pair ignored", "", ``, "1234", "1234"}, - {"number letter value dynamic", "", ``, "XTDE0030", "XTDE0030"}, - {"number letter value evaluated", "", ``, "FOER0000", "FOER0000"}, - {"number letter value empty", "", ``, "XTSE0020", "XTSE0020"}, - {"number empty format", "", ``, "1234", "1234"}, - {"number ordinal no", "", ``, "1234th", "1234"}, - {"number ordinal false", "", ``, "1234th", "1234"}, - {"number ordinal zero", "", ``, "1234th", "1234"}, - {"number ordinal yes", "", ``, "1234th", "1234th"}, - {"number start at plus rejected", "", ``, "XTSE0090", "XTDE0030"}, - {"number start at XML whitespace", "", ``, "XTSE0090", "2"}, - {"number start at nonXML whitespace", "", ``, "XTSE0090", "XTDE0030"}, - } -} - -func groupFunctionCases() []hostFunctionCase { - nodes := `` - group := `` - target := `:` - function := `` - return []hostFunctionCase{ - {"group absent", "", ``, nameTrue, "XTDE1061"}, - {"group key absent", "", ``, nameTrue, "XTDE1071"}, - {"starting group key", nodes, ``, "truetrue", "XTDE1071"}, - {"ending group key", nodes, ``, "truetrue", "XTDE1071"}, - {"starting atomic population", "", ``, "XTTE1120", "2"}, - {"ending mixed population", nodes, ``, "XTTE1120", "11"}, - {"group through named template", nodes + target, group + ``, "2:k", "2:k"}, - {"group through matched template", nodes + target, group + ``, "2:k", "2:k"}, - {"group with absent template focus", nodes + strings.Replace(target, ``, "XTSE0010", "2:k"}, - {"function clears group", nodes + function, group + ``, nameTrue, "XTDE1061"}, - {"function clears atomic focus", ``, ``, "XPDY0002", "XPDY0002"}, - {"function global scope", ``, ``, "global", "global"}, - {"required function parameter", ``, "ok", "XTSE0090", "ok"}, - {"optional function parameter", ``, "ok", "XTSE0090", "XTSE0020"}, - {"non tunnel function parameter", ``, "ok", "ok", "ok"}, - {"tunnel function parameter", ``, "ok", "XTSE0020", "XTSE0020"}, - {"default function parameter", ``, "ok", "XTSE0760", "XTSE0760"}, - } -} - -func hostFunctionSheet(version string, test hostFunctionCase) string { - return sheet(version, test.global+``+test.body+``) -} - -// Boolean spellings are selected by the processor, including a 2.0 -// declaration under a 3.0 processor and a future declaration under 2.0. -func booleanProfileCases() []hostFunctionCase { - return []hostFunctionCase{ - {"boolean copy alias", "", `ok`, "XTSE0020", "ok"}, - {"boolean inherit alias", "", `ok`, "XTSE0020", "ok"}, - {"boolean inherit false semantics", "", ``, "XTSE0020", "true"}, - {"boolean inherit zero semantics", "", ``, "XTSE0020", "true"}, - {"boolean inherit whitespace semantics", "", ``, "true", "true"}, - {"boolean composite whitespace semantics", "", ``, "XTSE0090", "2"}, - {"boolean literal result alias", "", `ok`, "XTSE0020", "ok"}, - {"boolean required alias", ``, ``, "XTSE0020", "ok"}, - {"boolean tunnel alias", ``, ``, "XTSE0020", "ok"}, - {"boolean function override alias", ``, ``, "XTSE0020", "ok"}, - {"boolean sort literal alias", "", ``, "XTSE0020", "12"}, - {"boolean output alias", ``, "ok", "XTSE0020", "ok"}, - {"boolean standalone alias", ``, "ok", "XTSE0020", "ok"}, - {"boolean escaping alias", "", `ok`, "XTSE0020", "ok"}, - {"boolean message literal alias", "", `ok`, "XTSE0020", "ok"}, - {"boolean message computed alias", "", `ok`, "XTDE0030", "ok"}, - {"boolean message computed no", "", `ok`, "ok", "ok"}, - {"boolean sort computed alias", "", ``, "XTDE0030", "12"}, - {"boolean result document computed alias", "", `secondaryok`, "XTDE0030", "ok"}, - {"boolean standalone computed alias", "", `secondaryok`, "XTDE0030", "ok"}, - {"boolean yes no literals", "", `ok`, "ok", "ok"}, - {"boolean XML whitespace", "", `ok`, "ok", "ok"}, - {"boolean sort computed yes", "", ``, "12", "12"}, - } -} - -func instructionContractCases() []hostFunctionCase { - return []hostFunctionCase{ - {"local variable empty select", "", `ok`, "XPST0003", "XPST0003"}, - {"attribute empty select", "", ``, "XPST0003", "XPST0003"}, - {"comment empty select", "", ``, "XPST0003", "XPST0003"}, - {"PI empty select", "", ``, "XPST0003", "XPST0003"}, - {"copy empty select", "", ``, "XTSE0090", "XPST0003"}, - {"copy-of empty select", "", ``, "XPST0003", "XPST0003"}, - {"copy-of missing select", "", ``, "XTSE0010", "XTSE0010"}, - {"number empty select", "", ``, "XPST0003", "XPST0003"}, - {"number empty value", "", ``, "XPST0003", "XPST0003"}, - {"namespace empty select", "", ``, "XPST0003", "XPST0003"}, - {"sequence empty select", "", ``, "XPST0003", "XPST0003"}, - {"key empty use", ``, "ok", "XPST0003", "XPST0003"}, - {"character map text child", `invalid`, "ok", "XTSE0010", "XTSE0010"}, - {"character map foreign child", ``, "ok", "XTSE0010", "XTSE0010"}, - {"character map empty declaration", ``, "ok", "ok", "ok"}, - {"accumulator text child", `invalid`, "ok", "XTSE0010", "XTSE0010"}, - {"accumulator foreign child", ``, "ok", "XTSE0010", "XTSE0010"}, - {"accumulator empty initial value", ``, "ok", "XTSE0010", "XPST0003"}, - {"accumulator rule empty select", ``, "ok", "XTSE0010", "XPST0003"}, - {"accumulator rule select and body", `invalid`, "ok", "XTSE0010", "XTSE0260"}, - {"atomic template selection", "", ``, "XTTE0520", "42"}, - {"empty template selection", "", `ok`, "ok", "ok"}, - {"function argument type error", ``, ``, "XTTE0790", "XPTY0004"}, - {"static transform argument type", ``, "ok", "XTSE0090", "XPTY0004"}, - {"static property function type", ``, ``, "XTSE0090", "false"}, - {"unparsed entity absent context", "", ``, "XTDE1370", "XPDY0002"}, - {"unparsed entity explicit node", "", ``, "XPST0017", "true"}, - {"unparsed entity arity availability", "", ``, "false", "true"}, - {"unknown grouping attribute", "", `ok`, "XTSE0090", "XTSE0090"}, - {"unknown instruction attribute", "", `ok`, "XTSE0090", "XTSE0090"}, - {"unknown declaration attribute", ``, "ok", "XTSE0090", "XTSE0090"}, - {"excluded unknown attribute", "", `badok`, "ok", "ok"}, - {"future instruction attribute", "", `ok`, "ok", "ok"}, - {"foreign instruction attribute", "", `ok`, "ok", "ok"}, - {"unknown shadow attribute", "", `ok`, "XTSE0090", "XTSE0090"}, - - {"function argument cast error", ``, ``, "FORG0001", "FORG0001"}, - {"empty value of", "", ``, "XTSE0870", ""}, - {"PI select and content", "", `body`, "XTSE0880", "XTSE0880"}, - {"number select type", "", ``, "XTTE1000", "XTTE1000"}, - {"sort select error", "", `ok`, "FOAR0001", "FOAR0001"}, - {"sort body error", "", `ok`, "FOAR0001", "FOAR0001"}, - {"unknown grouping collation", "", `ok`, "XTDE1110", "XTDE1110"}, - {"zero length analyze regex", "", ``, "XTDE1150", ""}, - {"invalid analyze flags", "", ``, "XTDE1145", "XTDE1145"}, - {"basic preserve validation", ``, "ok", "XTSE1660", "ok"}, - {"basic lax validation", ``, "ok", "XTSE1660", "ok"}, - {"missing extension deferred", "", ``, "XTDE1425", "XTDE1425"}, - {"unused missing extension", "", `ok`, "ok", "ok"}, - {"backwards missing standard function", "", `ok`, "XPST0017", "XPST0017"}, - {"backwards standard function arity", "", `ok`, "XPST0017", "XPST0017"}, - {"backwards unbound XPath prefix", "", `ok`, "XPST0081", "XPST0081"}, - {"copy select clears template rule", ``, ``, "XTSE0090", "XTDE0560"}, - {"implicit copy preserves template rule", `next`, ``, "next", "next"}, - {"copy select replaces atomic focus", `node`, ``, "XTSE0090", "node"}, - {"copy select restores atomic focus", ``, ``, "XTSE0090", "42"}, - } -} - -// Each row preserves the edition-sensitive construct behind a named publisher -// residual. The pinned original is tested separately by the proof manifest; -// these paired controls isolate the language-version cause. -func residualEditionProofCases() []hostFunctionCase { - const initial = `` - const chars = `` - return []hostFunctionCase{ - {"backwards-046", initial, "ok", "XTSE0080", "ok"}, - {"choose-0202", initial, `ok`, "XTSE0080", "ok"}, - {"doe-0409", initial, ``, "XTSE0080", "ab"}, - {"for-each-group-091", initial, ``, "XTSE0080", "2"}, - {"collations-0128", "", ``, "XTSE0010", "2"}, - {"collection-005", ``, "ok", "XTSE0010", "ok"}, - {"for-each-group-084", "", ``, "XPST0003", "11"}, - {"function-0302", "", ``, "XPST0003", "true"}, - {"function-1902", "", `{1+1}ok`, "XTSE0090", "ok"}, - {"function-1902 library availability", "", ``, "false", "true"}, - {"id-041", "", ``, "XPST0003", "a b"}, - {"match-248", "", chars, "XPST0003", "ab"}, - {"match-254", "", chars, "XPST0003", "ab"}, - {"number-1004", "", ``, "XTSE0010", "XTTE0990"}, - {"output-0725", ``, "ok", "XTSE0090", "ok"}, - {"output-0726", ``, "ok", "XTSE0090", "ok"}, - {"result-document-0286", ``, "ok", "XTSE0020", "ok"}, - {"result-document-0287", ``, "ok", "XTSE0020", "ok"}, - {"result-document-1006", "", ``, "XPST0017", "true"}, - {"sequence-0132", "", ``, "XTSE0010", "XTTE0570"}, - {"system-property-022", "", ``, "XTSE0010", "2"}, - {"error-0600a", "", ``, "XTTE0600", "XTTE0590"}, - {"error-1370a", "", ``, "XTDE1370", "XPDY0002"}, - {"error-1380a", "", ``, "XTDE1380", "XPDY0002"}, - } -} - -func residualEditionProofSource(test hostFunctionCase) string { - if test.name == "error-0600a" { - test.global = `` - } - return hostFunctionSheet(version20, test) -} - func TestXSLTResidualEditionProofs(t *testing.T) { e := engine(t, transform.Config{}) for _, test := range residualEditionProofCases() { @@ -3476,95 +3248,6 @@ func TestMessageHTMLContent(t *testing.T) { } } -type parameterContractCase struct { - name string - global string - param string - call string - check string - code20 string - code30 string -} - -func parameterContractCases() []parameterContractCase { - return []parameterContractCase{ - {"forward XPath syntax error", ``, ``, ``, `true()`, "XPST0003", "XPST0003"}, - {"forward XPath missing function", ``, ``, ``, `true()`, "XPST0017", "XPST0017"}, - {"forward XPath unbound prefix", ``, ``, ``, `true()`, "XPST0081", "XPST0081"}, - {"forward unused missing template", ``, ``, ``, `true()`, "XTSE0650", "XTSE0650"}, - {"forward evaluated missing template", ``, ``, ``, `true()`, "XTSE0650", "XTSE0650"}, - {"forward conditional missing template", ``, ``, ``, `true()`, "XTSE0650", "XTSE0650"}, - {"global default", ``, ``, ``, `$p`, "XTTE0600", "XTTE0590"}, - {"global implicit mandatory", ``, ``, ``, `true()`, "XTDE0610", "XTDE0050"}, - {"global variable select and preserved body", ` `, ``, ``, `$p`, "XTSE0620", "XTSE0620"}, - {"global select and body", `ignored`, ``, ``, `$p`, "XTSE0620", "XTSE0620"}, - {"named select and body", ``, `ignored`, ``, `$p`, "XTSE0620", "XTSE0620"}, - {"global unbound name prefix", ``, ``, ``, `true()`, "XTSE0280", "XTSE0280"}, - {"named unbound name prefix", ``, ``, ``, `true()`, "XTSE0280", "XTSE0280"}, - {"global variable unbound name prefix", ``, ``, ``, `true()`, "XTSE0280", "XTSE0280"}, - {"local variable unbound name prefix", ``, ``, ``, `true()`, "XTSE0280", "XTSE0280"}, - {"with parameter unbound name prefix", ``, ``, ``, `true()`, "XTSE0280", "XTSE0280"}, - {"local variable invalid name", ``, ``, ``, `true()`, "XTSE0020", "XTSE0020"}, - {"with parameter invalid name", ``, ``, ``, `true()`, "XTSE0020", "XTSE0020"}, - {"local nonstatic parameter", ``, ``, ``, `$p eq 1`, "XTSE0090", ""}, - {"local nonstatic variable", ``, ``, ``, `$p eq 1`, "XTSE0090", ""}, - {"local static parameter", ``, ``, ``, `$p`, "XTSE0090", "XTSE0020"}, - {"local static variable", ``, ``, ``, `true()`, "XTSE0090", "XTSE0020"}, - {"named default", ``, ``, ``, `$p`, "XTTE0600", "XTTE0590"}, - {"node default", ``, ``, ``, `$p`, "XTTE0600", "XTTE0590"}, - {"named implicit mandatory", ``, ``, ``, `$p`, "XTDE0610", "XTDE0700"}, - {"node implicit mandatory", ``, ``, ``, `$p`, "XTDE0610", "XTDE0700"}, - {"empty explicit default", ``, ``, ``, `$p`, "XTTE0600", "XTTE0590"}, - {"empty supplied value", ``, ``, ``, `$p`, "XTTE0590", "XTTE0590"}, - {"empty optional default", ``, ``, ``, `empty($p)`, "", ""}, - {"default string", ``, ``, ``, `exists($p) and $p instance of xs:string and $p eq ''`, "", ""}, - {"default document", ``, ``, ``, `$p instance of document-node() and exists($p/n)`, "", ""}, - {"declaration type namespace", ``, ``, ``, `$p eq 1`, "", ""}, - {"default type namespace", ``, ``, ``, `local-name($p) eq 'n' and namespace-uri($p) eq 'urn:local'`, "", ""}, - {"unused invalid default", ``, ``, ``, `$p eq 1`, "", ""}, - } -} - -func parameterContractSheet(version string, test parameterContractCase) string { - call := test.call - if call == "" { - call = `` - } - source := sheet(version, test.global+``+call+``+test.param+``) - if test.name == "declaration type namespace" { - source = strings.Replace(source, ``) -} - func TestVersionedFractionalSeconds(t *testing.T) { e := engine(t, transform.Config{}) for _, version := range []string{version20, version30} { @@ -4265,52 +3948,6 @@ func TestXSLTXMLIDConstruction(t *testing.T) { } } -func dynamicXPathCases() []dynamicXPathCase { - functions := `` - return []dynamicXPathCase{ - {"global not in scope", ``, ``, "XTDE3160"}, - {"local not in scope", "", ``, "XTDE3160"}, - {"unselected free variable", ``, ``, "XTDE3160"}, - {"explicit parameter", "", ``, nameTrue}, - {"map overrides parameter", "", ``, "42"}, - {"empty parameter map", "", ``, "XTTE3165"}, - {"non map parameter", "", ``, "XTTE3165"}, - {"wrong map key", "", ``, "XTTE3165"}, - {"unknown unselected function", "", ``, "XTDE3160"}, - {"public overload", functions, ``, "42"}, - {"private overload", functions, ``, "XTDE3160"}, - {"private lookup absent", functions, ``, nameTrue}, - {"XSLT function unavailable", "", ``, "XTDE3160"}, - {"XSLT lookup absent", "", ``, nameTrue}, - {"compatibility disabled", "", ``, errCodeXPTY0004}, - {"result numeric promotion", "", ``, nameTrue}, - {"result typed promotion", "", ``, nameTrue}, - {"result cardinality", "", ``, errCodeXPTY0004}, - {"result invalid cast", "", ``, "FORG0001"}, - {"source type", "", ``, errCodeXPTY0004}, - {"source cardinality", "", ``, errCodeXPTY0004}, - {"source URI", "", ``, "42"}, - {"lexical namespace", "", ``, "urn:local"}, - {"namespace context replaces prefixes", "", ``, "XTDE3160"}, - {"namespace context default", "", ``, "1"}, - {"bound variable", "", ``, "42"}, - {"absent position", "", ``, "XPDY0002"}, - {"singleton position", "", ``, "2"}, - {"schema type in signature", "", ``, "XTDE3160"}, - {"schema component", "", ``, "XTDE3160"}, - {"namespace context excludes xs", "", ``, "XTDE3160"}, - {"result declaration alias", "", ``, nameTrue}, - {"changing parameter map", "", ``, "XTDE3160"}, - {"parameter type failure", "", ``, "XTTE0590"}, - {"parameter type alias", "", ``, nameTrue}, - {"parameter array atomization", "", ``, nameTrue}, - } -} - -func dynamicXPathSheet(test dynamicXPathCase) string { - return sheet(version30, test.global+``+test.body+``) -} - func TestDynamicXPathContexts(t *testing.T) { e := engine(t, transform.Config{}) for _, test := range dynamicXPathCases() { @@ -5210,3 +4847,1907 @@ func BenchmarkXSLTNumberWidth(b *testing.B) { } } } + +func sortMergeContractCases() []hostFunctionCase { + return []hostFunctionCase{ + {"streamability zero arity function", ``, `ok`, "XTSE0090", "XTSE3155"}, + {"streamability attribute set reference", ``, `ok`, "XTSE0090", "XTSE0730"}, + {"accumulator local declaration namespace", ``, ``, "XTSE0010", "12"}, + {"accumulator absent context", ``, ``, "XTSE0010", "XTDE3350"}, + {"accumulator atomic context", ``, ``, "XTSE0010", "XTTE3360"}, + {"accumulator unbound name prefix", ``, ``, "XTSE0010", "XTDE3340"}, + {"copy function mixed sequence", ``, ``, "XPST0017", "true"}, + {"copy function atomic context", "", ``, "XPST0017", "42"}, + {"copy function absent context", "", ``, "XPST0017", "XPDY0002"}, + {"snapshot document accumulator values", ``, ``, "XTSE0010", "true"}, + {"snapshot function atomic context", "", ``, "XPST0017", "42"}, + {"snapshot function absent context", "", ``, "XPST0017", "XPDY0002"}, + {"snapshot function mixed sequence", ``, ``, "XPST0017", "true"}, + {"sort binary octets", "", ``, "XTDE1030", "AAAA////"}, + {"sort binary explicit text", "", ``, "////AAAA", "////AAAA"}, + {"sort binary multiple keys", "", ``, "XTDE1030", "AAAA////"}, + {"merge binary octets", "", ``, "XTSE0010", "AAAA////"}, + {"sort relative collation", ``, ``, "ab", "ab"}, + {"sort explicit collation ignores dynamic case", ``, ``, "ab", "ab"}, + {"sort ignored option static function", ``, ``, "XPST0017", "XPST0017"}, + {"sort ignored option malformed AVT", ``, ``, "XTSE0350", "XTSE0350"}, + {"sort language lexical value", ``, ``, "XTDE0030", "XTDE0030"}, + {"sort empty string ordering", ``, ``, "emptystring", "emptystring"}, + {"sort empty numeric ordering", ``, ``, "emptyNaNzero", "emptyNaNzero"}, + {"group lexical default collation", ``, ``, "2", "2"}, + {"merge order difference", ``, ``, "XTSE0010", "XTDE2210"}, + {"merge effective collation", ``, ``, "XTSE0010", "ab"}, + {"merge case avt", ``, ``, "XTSE0010", "Aa"}, + {"merge bad order", ``, ``, "XTSE0010", "XTDE0030"}, + {"merge text number", ``, ``, "XTSE0010", "1,10,2,"}, + {"merge collation equality", ``, ``, "XTSE0010", "2"}, + {"merge presort collation", ``, ``, "XTSE0010", "aA"}, + {"merge empty string", ``, ``, "XTSE0010", "0:1:0;1:1:1;"}, + {"merge nan group", ``, ``, "XTSE0010", "2"}, + {"merge relative collation", ``, ``, "XTSE0010", "ab"}, + {"merge option presence differs", ``, ``, "XTSE0010", "XTDE2210"}, + {"merge ignored language differs", ``, ``, "XTSE0010", "XTDE2210"}, + {"merge source local namespace", ``, ``, "XTSE0010", "12"}, + {"merge key local namespace", ``, ``, "XTSE0010", "12"}, + } +} + +func numberingContractCases() []hostFunctionCase { + return []hostFunctionCase{ + {"number array atomization", "", ``, "XPST0003", "1.2"}, + {"number large integer double conversion", "", ``, "9007199254740992", "9007199254740992"}, + {"number negative decimal half", "", ``, "0", "0"}, + {"number negative double half", "", ``, "0", "0"}, + {"number negative value rejected", "", ``, "XTDE0980", "XTDE0980"}, + {"number node position single", ``, ``, "123", "123"}, + {"number node position multiple", ``, `;`, "1.1;1.2;2.1;", "1.1;1.2;2.1;"}, + {"number node position any", ``, ``, "121", "121"}, + {"number grouping size dynamic", "", ``, "XTDE0030", "XTDE0030"}, + {"number grouping size empty", "", ``, "XTSE0020", "XTSE0020"}, + {"number grouping size XML whitespace", "", ``, "1,234", "1,234"}, + {"number grouping size large", "", ``, "1234", "1234"}, + {"number grouping size negative", "", ``, "1234", "1234"}, + {"number grouping incomplete pair ignored", "", ``, "1234", "1234"}, + {"number letter value dynamic", "", ``, "XTDE0030", "XTDE0030"}, + {"number letter value evaluated", "", ``, "FOER0000", "FOER0000"}, + {"number letter value empty", "", ``, "XTSE0020", "XTSE0020"}, + {"number empty format", "", ``, "1234", "1234"}, + {"number ordinal no", "", ``, "1234th", "1234"}, + {"number ordinal false", "", ``, "1234th", "1234"}, + {"number ordinal zero", "", ``, "1234th", "1234"}, + {"number ordinal yes", "", ``, "1234th", "1234th"}, + {"number start at plus rejected", "", ``, "XTSE0090", "XTDE0030"}, + {"number start at XML whitespace", "", ``, "XTSE0090", "2"}, + {"number start at nonXML whitespace", "", ``, "XTSE0090", "XTDE0030"}, + } +} + +func groupFunctionCases() []hostFunctionCase { + nodes := `` + group := `` + target := `:` + function := `` + return []hostFunctionCase{ + {"group absent", "", ``, nameTrue, "XTDE1061"}, + {"group key absent", "", ``, nameTrue, "XTDE1071"}, + {"starting group key", nodes, ``, "truetrue", "XTDE1071"}, + {"ending group key", nodes, ``, "truetrue", "XTDE1071"}, + {"starting atomic population", "", ``, "XTTE1120", "2"}, + {"ending mixed population", nodes, ``, "XTTE1120", "11"}, + {"group through named template", nodes + target, group + ``, "2:k", "2:k"}, + {"group through matched template", nodes + target, group + ``, "2:k", "2:k"}, + {"group with absent template focus", nodes + strings.Replace(target, ``, "XTSE0010", "2:k"}, + {"function clears group", nodes + function, group + ``, nameTrue, "XTDE1061"}, + {"function clears atomic focus", ``, ``, "XPDY0002", "XPDY0002"}, + {"function global scope", ``, ``, "global", "global"}, + {"required function parameter", ``, "ok", "XTSE0090", "ok"}, + {"optional function parameter", ``, "ok", "XTSE0090", "XTSE0020"}, + {"non tunnel function parameter", ``, "ok", "ok", "ok"}, + {"tunnel function parameter", ``, "ok", "XTSE0020", "XTSE0020"}, + {"default function parameter", ``, "ok", "XTSE0760", "XTSE0760"}, + } +} + +func hostFunctionSheet(version string, test hostFunctionCase) string { + return sheet(version, test.global+``+test.body+``) +} + +// Boolean spellings are selected by the processor, including a 2.0 +// declaration under a 3.0 processor and a future declaration under 2.0. +func booleanProfileCases() []hostFunctionCase { + return []hostFunctionCase{ + {"boolean copy alias", "", `ok`, "XTSE0020", "ok"}, + {"boolean inherit alias", "", `ok`, "XTSE0020", "ok"}, + {"boolean inherit false semantics", "", ``, "XTSE0020", "true"}, + {"boolean inherit zero semantics", "", ``, "XTSE0020", "true"}, + {"boolean inherit whitespace semantics", "", ``, "true", "true"}, + {"boolean composite whitespace semantics", "", ``, "XTSE0090", "2"}, + {"boolean literal result alias", "", `ok`, "XTSE0020", "ok"}, + {"boolean required alias", ``, ``, "XTSE0020", "ok"}, + {"boolean tunnel alias", ``, ``, "XTSE0020", "ok"}, + {"boolean function override alias", ``, ``, "XTSE0020", "ok"}, + {"boolean sort literal alias", "", ``, "XTSE0020", "12"}, + {"boolean output alias", ``, "ok", "XTSE0020", "ok"}, + {"boolean standalone alias", ``, "ok", "XTSE0020", "ok"}, + {"boolean escaping alias", "", `ok`, "XTSE0020", "ok"}, + {"boolean message literal alias", "", `ok`, "XTSE0020", "ok"}, + {"boolean message computed alias", "", `ok`, "XTDE0030", "ok"}, + {"boolean message computed no", "", `ok`, "ok", "ok"}, + {"boolean sort computed alias", "", ``, "XTDE0030", "12"}, + {"boolean result document computed alias", "", `secondaryok`, "XTDE0030", "ok"}, + {"boolean standalone computed alias", "", `secondaryok`, "XTDE0030", "ok"}, + {"boolean yes no literals", "", `ok`, "ok", "ok"}, + {"boolean XML whitespace", "", `ok`, "ok", "ok"}, + {"boolean sort computed yes", "", ``, "12", "12"}, + } +} + +func instructionContractCases() []hostFunctionCase { + return []hostFunctionCase{ + {"local variable empty select", "", `ok`, "XPST0003", "XPST0003"}, + {"attribute empty select", "", ``, "XPST0003", "XPST0003"}, + {"comment empty select", "", ``, "XPST0003", "XPST0003"}, + {"PI empty select", "", ``, "XPST0003", "XPST0003"}, + {"copy empty select", "", ``, "XTSE0090", "XPST0003"}, + {"copy-of empty select", "", ``, "XPST0003", "XPST0003"}, + {"copy-of missing select", "", ``, "XTSE0010", "XTSE0010"}, + {"number empty select", "", ``, "XPST0003", "XPST0003"}, + {"number empty value", "", ``, "XPST0003", "XPST0003"}, + {"namespace empty select", "", ``, "XPST0003", "XPST0003"}, + {"sequence empty select", "", ``, "XPST0003", "XPST0003"}, + {"key empty use", ``, "ok", "XPST0003", "XPST0003"}, + {"character map text child", `invalid`, "ok", "XTSE0010", "XTSE0010"}, + {"character map foreign child", ``, "ok", "XTSE0010", "XTSE0010"}, + {"character map empty declaration", ``, "ok", "ok", "ok"}, + {"accumulator text child", `invalid`, "ok", "XTSE0010", "XTSE0010"}, + {"accumulator foreign child", ``, "ok", "XTSE0010", "XTSE0010"}, + {"accumulator empty initial value", ``, "ok", "XTSE0010", "XPST0003"}, + {"accumulator rule empty select", ``, "ok", "XTSE0010", "XPST0003"}, + {"accumulator rule select and body", `invalid`, "ok", "XTSE0010", "XTSE0260"}, + {"atomic template selection", "", ``, "XTTE0520", "42"}, + {"empty template selection", "", `ok`, "ok", "ok"}, + {"function argument type error", ``, ``, "XTTE0790", "XPTY0004"}, + {"static transform argument type", ``, "ok", "XTSE0090", "XPTY0004"}, + {"static property function type", ``, ``, "XTSE0090", "false"}, + {"unparsed entity absent context", "", ``, "XTDE1370", "XPDY0002"}, + {"unparsed entity explicit node", "", ``, "XPST0017", "true"}, + {"unparsed entity arity availability", "", ``, "false", "true"}, + {"unknown grouping attribute", "", `ok`, "XTSE0090", "XTSE0090"}, + {"unknown instruction attribute", "", `ok`, "XTSE0090", "XTSE0090"}, + {"unknown declaration attribute", ``, "ok", "XTSE0090", "XTSE0090"}, + {"excluded unknown attribute", "", `badok`, "ok", "ok"}, + {"future instruction attribute", "", `ok`, "ok", "ok"}, + {"foreign instruction attribute", "", `ok`, "ok", "ok"}, + {"unknown shadow attribute", "", `ok`, "XTSE0090", "XTSE0090"}, + + {"function argument cast error", ``, ``, "FORG0001", "FORG0001"}, + {"empty value of", "", ``, "XTSE0870", ""}, + {"PI select and content", "", `body`, "XTSE0880", "XTSE0880"}, + {"number select type", "", ``, "XTTE1000", "XTTE1000"}, + {"sort select error", "", `ok`, "FOAR0001", "FOAR0001"}, + {"sort body error", "", `ok`, "FOAR0001", "FOAR0001"}, + {"unknown grouping collation", "", `ok`, "XTDE1110", "XTDE1110"}, + {"zero length analyze regex", "", ``, "XTDE1150", ""}, + {"invalid analyze flags", "", ``, "XTDE1145", "XTDE1145"}, + {"basic preserve validation", ``, "ok", "XTSE1660", "ok"}, + {"basic lax validation", ``, "ok", "XTSE1660", "ok"}, + {"missing extension deferred", "", ``, "XTDE1425", "XTDE1425"}, + {"unused missing extension", "", `ok`, "ok", "ok"}, + {"backwards missing standard function", "", `ok`, "XPST0017", "XPST0017"}, + {"backwards standard function arity", "", `ok`, "XPST0017", "XPST0017"}, + {"backwards unbound XPath prefix", "", `ok`, "XPST0081", "XPST0081"}, + {"copy select clears template rule", ``, ``, "XTSE0090", "XTDE0560"}, + {"implicit copy preserves template rule", `next`, ``, "next", "next"}, + {"copy select replaces atomic focus", `node`, ``, "XTSE0090", "node"}, + {"copy select restores atomic focus", ``, ``, "XTSE0090", "42"}, + } +} + +// Each row preserves the edition-sensitive construct behind a named publisher +// residual. The pinned original is tested separately by the proof manifest; +// these paired controls isolate the language-version cause. +func residualEditionProofCases() []hostFunctionCase { + const initial = `` + const chars = `` + return []hostFunctionCase{ + {"backwards-046", initial, "ok", "XTSE0080", "ok"}, + {"choose-0202", initial, `ok`, "XTSE0080", "ok"}, + {"doe-0409", initial, ``, "XTSE0080", "ab"}, + {"for-each-group-091", initial, ``, "XTSE0080", "2"}, + {"collations-0128", "", ``, "XTSE0010", "2"}, + {"collection-005", ``, "ok", "XTSE0010", "ok"}, + {"for-each-group-084", "", ``, "XPST0003", "11"}, + {"function-0302", "", ``, "XPST0003", "true"}, + {"function-1902", "", `{1+1}ok`, "XTSE0090", "ok"}, + {"function-1902 library availability", "", ``, "false", "true"}, + {"id-041", "", ``, "XPST0003", "a b"}, + {"match-248", "", chars, "XPST0003", "ab"}, + {"match-254", "", chars, "XPST0003", "ab"}, + {"number-1004", "", ``, "XTSE0010", "XTTE0990"}, + {"output-0725", ``, "ok", "XTSE0090", "ok"}, + {"output-0726", ``, "ok", "XTSE0090", "ok"}, + {"result-document-0286", ``, "ok", "XTSE0020", "ok"}, + {"result-document-0287", ``, "ok", "XTSE0020", "ok"}, + {"result-document-1006", "", ``, "XPST0017", "true"}, + {"sequence-0132", "", ``, "XTSE0010", "XTTE0570"}, + {"system-property-022", "", ``, "XTSE0010", "2"}, + {"error-0600a", "", ``, "XTTE0600", "XTTE0590"}, + {"error-1370a", "", ``, "XTDE1370", "XPDY0002"}, + {"error-1380a", "", ``, "XTDE1380", "XPDY0002"}, + } +} + +func residualEditionProofSource(test hostFunctionCase) string { + if test.name == "error-0600a" { + test.global = `` + } + return hostFunctionSheet(version20, test) +} + +type parameterContractCase struct { + name string + global string + param string + call string + check string + code20 string + code30 string +} + +func parameterContractCases() []parameterContractCase { + return []parameterContractCase{ + {"forward XPath syntax error", ``, ``, ``, `true()`, "XPST0003", "XPST0003"}, + {"forward XPath missing function", ``, ``, ``, `true()`, "XPST0017", "XPST0017"}, + {"forward XPath unbound prefix", ``, ``, ``, `true()`, "XPST0081", "XPST0081"}, + {"forward unused missing template", ``, ``, ``, `true()`, "XTSE0650", "XTSE0650"}, + {"forward evaluated missing template", ``, ``, ``, `true()`, "XTSE0650", "XTSE0650"}, + {"forward conditional missing template", ``, ``, ``, `true()`, "XTSE0650", "XTSE0650"}, + {"global default", ``, ``, ``, `$p`, "XTTE0600", "XTTE0590"}, + {"global implicit mandatory", ``, ``, ``, `true()`, "XTDE0610", "XTDE0050"}, + {"global variable select and preserved body", ` `, ``, ``, `$p`, "XTSE0620", "XTSE0620"}, + {"global select and body", `ignored`, ``, ``, `$p`, "XTSE0620", "XTSE0620"}, + {"named select and body", ``, `ignored`, ``, `$p`, "XTSE0620", "XTSE0620"}, + {"global unbound name prefix", ``, ``, ``, `true()`, "XTSE0280", "XTSE0280"}, + {"named unbound name prefix", ``, ``, ``, `true()`, "XTSE0280", "XTSE0280"}, + {"global variable unbound name prefix", ``, ``, ``, `true()`, "XTSE0280", "XTSE0280"}, + {"local variable unbound name prefix", ``, ``, ``, `true()`, "XTSE0280", "XTSE0280"}, + {"with parameter unbound name prefix", ``, ``, ``, `true()`, "XTSE0280", "XTSE0280"}, + {"local variable invalid name", ``, ``, ``, `true()`, "XTSE0020", "XTSE0020"}, + {"with parameter invalid name", ``, ``, ``, `true()`, "XTSE0020", "XTSE0020"}, + {"local nonstatic parameter", ``, ``, ``, `$p eq 1`, "XTSE0090", ""}, + {"local nonstatic variable", ``, ``, ``, `$p eq 1`, "XTSE0090", ""}, + {"local static parameter", ``, ``, ``, `$p`, "XTSE0090", "XTSE0020"}, + {"local static variable", ``, ``, ``, `true()`, "XTSE0090", "XTSE0020"}, + {"named default", ``, ``, ``, `$p`, "XTTE0600", "XTTE0590"}, + {"node default", ``, ``, ``, `$p`, "XTTE0600", "XTTE0590"}, + {"named implicit mandatory", ``, ``, ``, `$p`, "XTDE0610", "XTDE0700"}, + {"node implicit mandatory", ``, ``, ``, `$p`, "XTDE0610", "XTDE0700"}, + {"empty explicit default", ``, ``, ``, `$p`, "XTTE0600", "XTTE0590"}, + {"empty supplied value", ``, ``, ``, `$p`, "XTTE0590", "XTTE0590"}, + {"empty optional default", ``, ``, ``, `empty($p)`, "", ""}, + {"default string", ``, ``, ``, `exists($p) and $p instance of xs:string and $p eq ''`, "", ""}, + {"default document", ``, ``, ``, `$p instance of document-node() and exists($p/n)`, "", ""}, + {"declaration type namespace", ``, ``, ``, `$p eq 1`, "", ""}, + {"default type namespace", ``, ``, ``, `local-name($p) eq 'n' and namespace-uri($p) eq 'urn:local'`, "", ""}, + {"unused invalid default", ``, ``, ``, `$p eq 1`, "", ""}, + } +} + +func parameterContractSheet(version string, test parameterContractCase) string { + call := test.call + if call == "" { + call = `` + } + source := sheet(version, test.global+``+call+``+test.param+``) + if test.name == "declaration type namespace" { + source = strings.Replace(source, ``) +} + +func dynamicXPathCases() []dynamicXPathCase { + functions := `` + return []dynamicXPathCase{ + {"global not in scope", ``, ``, "XTDE3160"}, + {"local not in scope", "", ``, "XTDE3160"}, + {"unselected free variable", ``, ``, "XTDE3160"}, + {"explicit parameter", "", ``, nameTrue}, + {"map overrides parameter", "", ``, "42"}, + {"empty parameter map", "", ``, "XTTE3165"}, + {"non map parameter", "", ``, "XTTE3165"}, + {"wrong map key", "", ``, "XTTE3165"}, + {"unknown unselected function", "", ``, "XTDE3160"}, + {"public overload", functions, ``, "42"}, + {"private overload", functions, ``, "XTDE3160"}, + {"private lookup absent", functions, ``, nameTrue}, + {"XSLT function unavailable", "", ``, "XTDE3160"}, + {"XSLT lookup absent", "", ``, nameTrue}, + {"compatibility disabled", "", ``, errCodeXPTY0004}, + {"result numeric promotion", "", ``, nameTrue}, + {"result typed promotion", "", ``, nameTrue}, + {"result cardinality", "", ``, errCodeXPTY0004}, + {"result invalid cast", "", ``, "FORG0001"}, + {"source type", "", ``, errCodeXPTY0004}, + {"source cardinality", "", ``, errCodeXPTY0004}, + {"source URI", "", ``, "42"}, + {"lexical namespace", "", ``, "urn:local"}, + {"namespace context replaces prefixes", "", ``, "XTDE3160"}, + {"namespace context default", "", ``, "1"}, + {"bound variable", "", ``, "42"}, + {"absent position", "", ``, "XPDY0002"}, + {"singleton position", "", ``, "2"}, + {"schema type in signature", "", ``, "XTDE3160"}, + {"schema component", "", ``, "XTDE3160"}, + {"namespace context excludes xs", "", ``, "XTDE3160"}, + {"result declaration alias", "", ``, nameTrue}, + {"changing parameter map", "", ``, "XTDE3160"}, + {"parameter type failure", "", ``, "XTTE0590"}, + {"parameter type alias", "", ``, nameTrue}, + {"parameter array atomization", "", ``, nameTrue}, + } +} + +func dynamicXPathSheet(test dynamicXPathCase) string { + return sheet(version30, test.global+``+test.body+``) +} + +func TestXSLTCompilationErrorParity(t *testing.T) { + const source = `` + ordinary := engine(t, transform.Config{}) + canceled, cancel := context.WithCancel(t.Context()) + cancel() + + for _, route := range []struct { + name string + compile func(*transform.Engine, context.Context, string) (*transform.Stylesheet, error) + }{ + {"bytes", func(e *transform.Engine, ctx context.Context, source string) (*transform.Stylesheet, error) { + return e.CompileXSLT(ctx, []byte(source), transform.XSLTOptions{}) + }}, + {"reader", func(e *transform.Engine, ctx context.Context, source string) (*transform.Stylesheet, error) { + return e.CompileXSLTReader(ctx, strings.NewReader(source), transform.XSLTOptions{}) + }}, + } { + t.Run(route.name, func(t *testing.T) { + for _, tc := range []struct { + name string + engine *transform.Engine + ctx context.Context + source string + cause error + }{ + {"malformed XML", ordinary, t.Context(), ``)) + result, err := s.Transform(t.Context(), nil, transform.XSLTContext{Template: transform.Name{Local: nameStart}, Context: options}) + if err != nil { + return "", err + } + + return string(result.Primary), nil + } + + versions := map[string]transform.XPathVersion{"XPath20": transform.XPath20, "XPath30": transform.XPath30, "XPath31": transform.XPath31} + q := query(t, e, `trace(42, 'review-trace')`, versions[language]) + result, err := q.Evaluate(t.Context(), nil, transform.XPathContext{Context: options}) + if err != nil { + return "", err + } + + return result.Text() + } + + for _, tc := range []struct { + name string + writer io.Writer + }{ + {"nil", nil}, + {"typed nil", typedNil}, + } { + t.Run(tc.name, func(t *testing.T) { + defer func() { + if p := recover(); p != nil { + t.Errorf("nil trace writer panicked: %v", p) + } + }() + got, err := invoke(tc.writer) + if err != nil || got != "42" { + t.Fatalf("got %q, error=%v", got, err) + } + }) + } + + t.Run("output", func(t *testing.T) { + var buffer bytes.Buffer + got, err := invoke(&buffer) + if err != nil || got != "42" || !strings.Contains(buffer.String(), "review-trace") || !strings.Contains(buffer.String(), "42") { + t.Fatalf("got %q, trace=%q, error=%v", got, buffer.String(), err) + } + }) + + t.Run("writer error", func(t *testing.T) { + got, err := invoke(traceFailureWriter{cause: failure}) + if got != "" || !errors.Is(err, failure) { + t.Fatalf("got %q, error=%v; want %v", got, err, failure) + } + }) + }) + } +} + +type traceFailureWriter struct{ cause error } + +func (w traceFailureWriter) Write([]byte) (int, error) { + return 0, fmt.Errorf("trace output: %w", w.cause) +} + +func TestXSLTWriterInvocationErrors(t *testing.T) { + e := engine(t, transform.Config{}) + s := stylesheet(t, e, sheet(version30, `secondary outputprimary output`)) + result, err := s.Execute(t.Context(), nil, transform.XSLTContext{Template: transform.Name{Local: nameStart}}) + if err != nil { + t.Fatal(err) + } + recordStyle := stylesheet(t, e, sheet(version30, `record output`)) + + canceled, cancel := context.WithCancel(t.Context()) + cancel() + for _, route := range []struct { + name string + operation string + want string + write func(context.Context, io.Writer) (int64, error) + }{ + {"primary", "write XSLT", "primary output", func(ctx context.Context, w io.Writer) (int64, error) { + return result.WriterTo(ctx).WriteTo(w) + }}, + {"secondary", "write secondary XSLT", "secondary output", func(ctx context.Context, w io.Writer) (int64, error) { + return result.WriteSecondary(ctx, "urn:secondary", w) + }}, + {"records", "stream XSLT", "record output", func(ctx context.Context, w io.Writer) (int64, error) { + return recordStyle.WriteRecords(ctx, w, strings.NewReader(``), transform.StreamOptions{ + Records: document.RecordOptions{Path: []document.Name{{Local: "records"}, {Local: "record"}}}, + }) + }}, + } { + t.Run(route.name, func(t *testing.T) { + for _, tc := range []struct { + name string + ctx context.Context + cause error + }{ + {"nil context", nil, transform.ErrInput}, + {"canceled context", canceled, context.Canceled}, + } { + t.Run(tc.name, func(t *testing.T) { + var output bytes.Buffer + n, err := route.write(tc.ctx, &output) + var detail *transform.Error + if n != 0 || output.Len() != 0 || !errors.Is(err, tc.cause) || !errors.As(err, &detail) || detail.Operation != route.operation { + t.Fatalf("count=%d, output=%q, error=%v; want %s retaining %v", n, output.String(), err, route.operation, tc.cause) + } + }) + } + + var output bytes.Buffer + n, err := route.write(t.Context(), &output) + if err != nil || n != int64(len(route.want)) || output.String() != route.want { + t.Fatalf("valid writer count=%d, output=%q, error=%v; want %q", n, output.String(), err, route.want) + } + }) + } +} + +func TestXSLTCompilerSequenceContracts(t *testing.T) { + e, err := transform.NewEngine(transform.Config{}) + if err != nil { + t.Fatal(err) + } + doc, err := e.Parse(t.Context(), []byte(``)) + if err != nil { + t.Fatal(err) + } + const format = `` + const changedCollation = ` default-collation="http://www.w3.org/2005/xpath-functions/collation/html-ascii-case-insensitive"` + const noParam = `target` + const withParam = `` + const call = `` + for _, tc := range []struct{ name, decl, body, compileCode, primary, secondary string }{ + {"A absent inherits", format, ``, "", "", "1|2"}, + {"A literal empty overrides", format, ``, "", "", "12"}, + {"A AVT empty overrides", format, ``, "", "", "12"}, + {"B unwrapped nonterminal", "", `tail`, "XTSE0010", "", ""}, + {"B wrapped nonterminal", "", `tail`, "XTSE0010", "", ""}, + {"B unwrapped terminal", "", ``, "", "fallback", ""}, + {"B wrapped terminal", "", ``, "", "fallback", ""}, + {"B unwrapped nonempty terminal", "", `real`, "", "real", ""}, + {"B wrapped nonempty terminal", "", `real`, "", "real", ""}, + {"C direct undeclared parameter", noParam, call, "XTSE0680", "", ""}, + {"C nested undeclared parameter", noParam, `` + call + ``, "XTSE0680", "", ""}, + {"C nested declared parameter", withParam, `` + call + ``, "", "2", ""}, + } { + t.Run(tc.name, func(t *testing.T) { + source := `` + tc.decl + `` + tc.body + `` + q, err := e.CompileXSLT(t.Context(), []byte(source), transform.XSLTOptions{}) + code := "" + if detail, ok := errors.AsType[*transform.Error](err); ok { + code = detail.Code + } + t.Logf("compileCode=%q expectedCompileCode=%q stylesheetNil=%t error=%v", code, tc.compileCode, q == nil, err) + if tc.compileCode != "" { + if err == nil || code != tc.compileCode || q != nil { + t.Errorf("compile actualCode=%q error=%v stylesheetNil=%t; expected=%s/nil", code, err, q == nil, tc.compileCode) + } + return + } + if err != nil || q == nil { + t.Fatalf("valid compile control stylesheet=%v error=%v", q, err) + } + out, err := q.Transform(t.Context(), doc, transform.XSLTContext{}) + if err != nil { + if out != nil { + t.Errorf("non-nil output on failure") + } + t.Fatal(err) + } + if out == nil { + t.Fatal("nil output on success") + } + t.Logf("primary=%q expectedPrimary=%q secondary=%q expectedSecondary=%q secondaryCount=%d", out.Primary, tc.primary, out.Secondary["urn:secondary"], tc.secondary, len(out.Secondary)) + if string(out.Primary) != tc.primary { + t.Errorf("primary actual=%q expected=%q", out.Primary, tc.primary) + } + if tc.secondary != "" { + if len(out.Secondary) != 1 || string(out.Secondary["urn:secondary"]) != tc.secondary { + t.Errorf("secondary actual=%q expected=%q", out.Secondary, tc.secondary) + } + } else if len(out.Secondary) != 0 { + t.Errorf("unexpected secondary output=%q", out.Secondary) + } + }) + } +} + +// These cases exercise the public adapter as well as the XSLT implementation. +func TestXSLTComprehensiveRegressions(t *testing.T) { + e := engine(t, transform.Config{}) + doc, err := e.Parse(t.Context(), []byte(`21`)) + if err != nil { + t.Fatal(err) + } + for _, tc := range []struct{ name, declarations, body, want, code string }{ + {"scope if", "", "", "outer", ""}, + {"scope choose", "", "", "outer", ""}, + {"scope LRE positive", "", "", "outer", ""}, + {"scope computed element", "", "", "outer", ""}, + {"document copy identity", "", "", "false|false", ""}, + {"typed doc rejects element", "", "", "", "XTTE0570"}, + {"typed empty string rejects empty seq", "", "", "", "XTTE0570"}, + {"typed optional empty positive", "", "", "0", ""}, + {"cache delimiter", "", "", "a;a:xs:string=b|c#a|b;a:xs:string=c", ""}, + {"pattern attributeFoo", "hit", "", "hit", ""}, + {"pattern literal childattribute", "hit", "", "hit", ""}, + {"key dot attribute", "", "", "1", ""}, + {"key attribute positive", "", "", "1", ""}, + {"sort body position", "", "", "321", ""}, + {"sort select position positive", "", "", "321", ""}, + {"function position absent", "", "", "", "XPDY0002"}, + {"function last absent", "", "", "", "XPDY0002"}, + {"merge explicit fn prefix", "", "", "12", ""}, + {"merge unprefixed positive", "", "", "12", ""}, + {"computed attribute local NS", "", "v", "urn:inner", ""}, + {"otherwise local NS", "", "", "1", ""}, + {"when local NS positive", "", "", "1", ""}, + {"sort local NS", "", "", "ab", ""}, + {"analyze token subtype", "", "hit", "hit", ""}, + {"analyze string positive", "", "hit", "hit", ""}, + {"typed array members", "", "", "", "XTTE0570"}, + {"try onempty", "", "error", "fallback", ""}, + {"if onempty positive", "", "", "fallback", ""}, + {"valueof mixed body order", "", "abc", "a|b|c", ""}, + {"accumulator literal no dependency", "", "", "accumulator-after('a')", ""}, + } { + t.Run(tc.name, func(t *testing.T) { + source := `` + tc.declarations + `` + tc.body + `` + ss, err := e.CompileXSLT(t.Context(), []byte(source), transform.XSLTOptions{}) + if err != nil { + t.Fatal(err) + } + out, err := ss.Transform(t.Context(), doc, transform.XSLTContext{}) + if tc.code != "" { + var detail *transform.Error + if out != nil || !errors.As(err, &detail) || detail.Code != tc.code { + t.Fatalf("result=%v, error=%v; want %s", out, err, tc.code) + } + return + } + if err != nil { + t.Fatal(err) + } + if got := string(out.Primary); got != tc.want { + t.Fatalf("output=%q; want %q", got, tc.want) + } + }) + } +} + +func TestXSLTComprehensiveFollowups(t *testing.T) { + cases := []struct{ name, decl, body, want, code, source string }{ + {"atomic context type", "bad", "", "", "XTTE0590", ""}, + {"template focus restore", "", "", "", "", ""}, + {"try raw sequence order", "", "", "true|e|true", "", ""}, + {"copy atomic type", "", "", "true", "", ""}, + {"copy map select", "", "", "true", "", ""}, + {"copy map context", "", "", "true", "", ""}, + {"document static base", "", "", "https://sheet.test/base", "", ""}, + {"function onempty", "", "", "fallback", "", ""}, + {"analyze onempty", "", "", "fallback", "", ""}, + {"iterate onempty", "", "", "fallback", "", ""}, + {"merge onempty", "", "", "fallback", "", ""}, + {"source onempty", "", "", "fallback", "", ""}, + {"intermediate desc self node excludes", `hit`, `|`, "false|x", "", `x`}, + {"intermediate desc self predicate excludes", `hit`, `|`, "false|x", "", `x`}, + {"intermediate desc self positive", `hit`, `|`, "true|hit", "", `x`}, + {"intermediate desc self positional excludes", `hit`, `|`, "false|y", "", `xy`}, + {"intermediate desc self positional positive", `hit`, `|`, "true|hit", "", `xy`}, + {"intermediate descendant positive", `hit`, `|`, "true|hit", "", `x`}, + {"intermediate descendant excludes", `hit`, `|`, "false|x", "", `x`}, + {"synthetic descendant positive", `hit`, `|`, "true|hit", "", `x`}, + {"synthetic descendant document positive", `hit`, `|`, "true|hit", "", `x`}, + {"accumulator source all", "", "", "1", "", ""}, + {"accumulator source explicit positive", "", "", "1", "", ""}, + {"accumulator lazy locals", "", "", "0", "", ""}, + {"typed attribute name", "", "", "", "XTTE0570", ""}, + {"typed element name positive", "", "", "", "XTTE0570", ""}, + {"typed string no cast", "", "", "", "XTTE0570", ""}, + {"typed integer no decimal narrowing", "", "", "", "XTTE0570", ""}, + {"typed array not atomic", "", "", "", "XTTE0570", ""}, + {"typed document name", "", "", "", "XTTE0570", ""}, + {"typed map value", "", "", "", "XTTE0570", ""}, + {"computed attribute shadow", "", "", "urn:A|urn:B", "", ""}, + {"computed attribute dynamic shadow", "", "", "urn:A|urn:B", "", ""}, + {"number valid math digit", "", "", "𝟚", "", ""}, + {"number valid Arabic positive", "", "", "٢", "", ""}, + {"charmap invalid character length", "", "ok", "", "XTSE0020", ""}, + {"output BOM merging", "", "abc", "\ufeffabc", "", ""}, + {"mode stream declaration merge", "", "ok", "ok", "", ""}, + {"copy namespaces no document", "", "')\" copy-namespaces=\"no\"/>", "false", "", ""}, + {"key duration cross subtype", "", "", "true|1", "", ""}, + {"key duration same type positive", "", "", "1", "", ""}, + {"typed template context array", "bad", "", "", "XTTE0590", ""}, + {"implicit document source ownership", "", ``, "false|false|true|true", "", ""}, + {"try rollback raw sequence", "", ``, "true", "", ""}, + {"typed array positive", "", ``, "2", "", ""}, + {"typed attribute positive", "", ``, "needle", "", ``}, + {"charmap supplementary scalar positive", ``, `𝟙`, "Z", "", ""}, + {"charmap empty character", ``, "ok", "", "XTSE0020", ""}, + {"mode explicit before absent", ``, "ok", "ok", "", ""}, + {"mode explicit conflict preserved", ``, "ok", "", "XTSE0545", ""}, + {"copy namespaces source ownership", "", ``, "true|false|false", "", ""}, + {"copy namespaces yes positive", "", ``, "true", "", ""}, + {"key duration negative day subtype", ``, ``, "1|0", "", ""}, + {"pattern document shorthand", `hit`, ``, "hit", "", ""}, + {"pattern document grouped shorthand", `hit`, ``, "hit", "", ""}, + {"pattern document explicit child", `badok`, ``, "ok", "", ""}, + {"pattern document explicit grouped comments", `badok`, ``, "ok", "", ""}, + {"pattern attribute explicit child", `badok`, ``, "ok", "", ``}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + ctx, cancel := context.WithTimeout(t.Context(), 3*time.Second) + defer cancel() + e := engine(t, transform.Config{Resolver: transform.ResolverFunc(func(context.Context, string) (io.ReadCloser, error) { + return io.NopCloser(strings.NewReader(``)), nil + })}) + full := `` + tc.decl + `` + tc.body + `` + ss, err := e.CompileXSLT(ctx, []byte(full), transform.XSLTOptions{}) + if err != nil { + if tc.code != "" && matchesComprehensiveCode(err, tc.code) { + return + } + t.Fatalf("COMPILE %v", err) + } + doc, err := e.Parse(ctx, []byte(tc.source)) + if err != nil { + t.Fatal(err) + } + out, err := ss.Transform(ctx, doc, transform.XSLTContext{}) + got := "" + if out != nil { + got = string(out.Primary) + } + if tc.code != "" { + if out != nil || !matchesComprehensiveCode(err, tc.code) { + t.Fatalf("got %q err %v want %s", got, err, tc.code) + } + return + } + if err != nil || got != tc.want { + t.Fatalf("got %q err %v want %q", got, err, tc.want) + } + }) + } +} + +func matchesComprehensiveCode(err error, code string) bool { + var detail *transform.Error + return errors.As(err, &detail) && detail.Code == code +} + +func TestXSLTComprehensiveConditionalItems(t *testing.T) { + for _, tc := range []struct { + name, body, code string + kind transform.ValueKind + }{ + {"array compile unsupported", ``, "XTSE0010", transform.InvalidValue}, + {"array metadata false", ``, "", transform.NodeValue}, + {"conditional map raw", ``, "", transform.MapValue}, + {"sequence map raw positive", ``, "", transform.MapValue}, + {"conditional map constructed", ``, "XTDE0450", transform.InvalidValue}, + {"sequence map constructed positive", ``, "XTDE0450", transform.InvalidValue}, + {"conditional array constructed positive", ``, "", transform.NodeValue}, + {"sequence array constructed positive", ``, "", transform.NodeValue}, + } { + t.Run(tc.name, func(t *testing.T) { + ctx, cancel := context.WithTimeout(t.Context(), 3*time.Second) + defer cancel() + e := engine(t, transform.Config{}) + source := `` + tc.body + `` + ss, err := e.CompileXSLT(ctx, []byte(source), transform.XSLTOptions{}) + if err != nil { + if tc.code != "" && matchesComprehensiveCode(err, tc.code) { + return + } + t.Fatal(err) + } + result, err := ss.Execute(ctx, nil, transform.XSLTContext{Template: transform.Name{Local: "start"}}) + if tc.code != "" { + if result != nil || !matchesComprehensiveCode(err, tc.code) { + t.Fatalf("result %v error %v; want %s", result, err, tc.code) + } + return + } + if err != nil { + t.Fatal(err) + } + value, err := result.Primary().Single() + if err != nil { + t.Fatal(err) + } + if value.Kind() != tc.kind { + t.Fatalf("kind %v want %v", value.Kind(), tc.kind) + } + want := "" + if tc.name == "array metadata false" { + want = "false" + } + if strings.Contains(tc.name, "array constructed") { + want = "1 2" + } + if want != "" { + got, err := value.Text() + if err != nil || got != want { + t.Fatalf("text %q err %v want %q", got, err, want) + } + } + }) + } +} + +func remainingSheet(decl, body string) string { + return `` + decl + `` + body + `` +} + +func remainingTransform(t *testing.T, decl, body string, dyn transform.DynamicContext, want, code string) { + t.Helper() + ctx, cancel := context.WithTimeout(t.Context(), 3*time.Second) + defer cancel() + e := engine(t, transform.Config{}) + ss, err := e.CompileXSLT(ctx, []byte(remainingSheet(decl, body)), transform.XSLTOptions{}) + if err != nil { + if code != "" && matchesComprehensiveCode(err, code) { + return + } + t.Fatal(err) + } + doc, err := e.Parse(ctx, []byte(``)) + if err != nil { + t.Fatal(err) + } + out, err := ss.Transform(ctx, doc, transform.XSLTContext{Context: dyn}) + if code != "" { + if out != nil || !matchesComprehensiveCode(err, code) { + t.Fatalf("out %v err %v want %s", out, err, code) + } + return + } + if err != nil { + t.Fatal(err) + } + if got := string(out.Primary); got != want { + t.Fatalf("got %q want %q", got, want) + } +} + +func TestXSLTRemainingRegressions(t *testing.T) { + t.Run("options", func(t *testing.T) { + const source = "oknamespacedunprefixed" + for _, tc := range []struct{ name, options, want, code string }{ + {"valid named template", "'stylesheet-text':$sheet,'initial-template':QName('','start'),'delivery-format':'serialized'", "ok", ""}, + {"stylesheet text cardinality", "'stylesheet-text':($sheet,$sheet),'initial-template':QName('','start'),'delivery-format':'serialized'", "", "XPTY0004"}, + {"initial template string type", "'stylesheet-text':$sheet,'initial-template':'start','delivery-format':'serialized'", "", "XPTY0004"}, + {"initial template cardinality", "'stylesheet-text':$sheet,'initial-template':(QName('','start'),QName('','start')),'delivery-format':'serialized'", "", "XPTY0004"}, + {"initial mode QName namespace", "'stylesheet-text':$sheet,'source-node':parse-xml(''),'initial-mode':QName('urn:m','q:m'),'delivery-format':'serialized'", "namespaced", ""}, + {"initial mode unnamespaced positive", "'stylesheet-text':$sheet,'source-node':parse-xml(''),'initial-mode':QName('','m'),'delivery-format':'serialized'", "unprefixed", ""}, + {"parameter map cardinality", "'stylesheet-text':$sheet,'initial-template':QName('','start'),'stylesheet-params':(map{},map{}),'delivery-format':'serialized'", "", "XPTY0004"}, + {"parameter map empty required type", "'stylesheet-text':$sheet,'initial-template':QName('','start'),'stylesheet-params':(),'delivery-format':'serialized'", "", "XPTY0004"}, + {"parameter empty map positive", "'stylesheet-text':$sheet,'initial-template':QName('','start'),'stylesheet-params':map{},'delivery-format':'serialized'", "ok", ""}, + {"stylesheet base wrong type", "'stylesheet-text':$sheet,'initial-template':QName('','start'),'stylesheet-base-uri':42,'delivery-format':'serialized'", "", "XPTY0004"}, + {"source node atomic type", "'stylesheet-text':$sheet,'initial-template':QName('','start'),'source-node':42,'delivery-format':'serialized'", "", "XPTY0004"}, + {"source node cardinality", "'stylesheet-text':$sheet,'initial-template':QName('','start'),'source-node':(parse-xml(''),parse-xml('')),'delivery-format':'serialized'", "", "XPTY0004"}, + {"function params array positive", "'stylesheet-text':$sheet,'initial-function':QName('urn:f','f:echo'),'function-params':[7],'delivery-format':'serialized'", "7", ""}, + {"function params not array", "'stylesheet-text':$sheet,'initial-function':QName('urn:f','f:echo'),'function-params':1,'delivery-format':'serialized'", "", "XPTY0004"}, + {"postprocess coerced argument rejects string", "'stylesheet-text':$sheet,'initial-template':QName('','start'),'post-process':function($u as xs:integer,$v as item()*) as item()* {$v},'delivery-format':'serialized'", "", "XPTY0004"}, + {"unknown QName option ignored", "'stylesheet-text':$sheet,'initial-template':QName('','start'),QName('urn:foreign','v:initial-template'):42,'delivery-format':'serialized'", "ok", ""}, + + {"serialization option not map", "'stylesheet-text':$sheet,'initial-template':QName('','start'),'serialization-params':'bad','delivery-format':'serialized'", "", "XPTY0004"}, + {"serialization option cardinality", "'stylesheet-text':$sheet,'initial-template':QName('','start'),'serialization-params':(map{},map{}),'delivery-format':'serialized'", "", "XPTY0004"}, + {"serialization indent string type", "'stylesheet-text':$sheet,'initial-template':QName('','start'),'serialization-params':map{'indent':'yes'},'delivery-format':'serialized'", "", "XPTY0004"}, + {"serialization indent cardinality", "'stylesheet-text':$sheet,'initial-template':QName('','start'),'serialization-params':map{'indent':(true(),false())},'delivery-format':'serialized'", "", "XPTY0004"}, + {"serialization method wrong type", "'stylesheet-text':$sheet,'initial-template':QName('','start'),'serialization-params':map{'method':1},'delivery-format':'serialized'", "", "XPTY0004"}, + {"serialization extension QName ignored", "'stylesheet-text':$sheet,'initial-template':QName('','start'),'serialization-params':map{QName('urn:foreign','v:method'):'text'},'delivery-format':'serialized'", "ok", ""}, + {"serialization valid boolean positive", "'stylesheet-text':$sheet,'initial-template':QName('','start'),'serialization-params':map{'indent':false()},'delivery-format':'serialized'", "ok", ""}, + } { + t.Run(tc.name, func(t *testing.T) { + ctx, cancel := context.WithTimeout(t.Context(), 3*time.Second) + defer cancel() + e := engine(t, transform.Config{}) + p, err := e.CompileXPath(ctx, `transform(map{`+tc.options+`})?output`, transform.XPathOptions{Version: transform.XPath31}) + if err != nil { + t.Fatal(err) + } + value, err := p.Evaluate(ctx, nil, transform.XPathContext{Variables: transform.Bindings{{Local: "sheet"}: transform.Values(transform.String(source))}}) + if tc.code != "" { + if value.Len() != 0 || !matchesComprehensiveCode(err, tc.code) { + t.Fatalf("value %v err %v want %s", value, err, tc.code) + } + return + } + if err != nil { + t.Fatal(err) + } + got, err := value.Text() + if err != nil || got != tc.want { + t.Fatalf("got %q err %v want %q", got, err, tc.want) + } + }) + } + t.Run("serialize bad boolean control", func(t *testing.T) { + ctx, cancel := context.WithTimeout(t.Context(), 3*time.Second) + defer cancel() + e := engine(t, transform.Config{}) + p, err := e.CompileXPath(ctx, `serialize(parse-xml(''),map{'indent':'yes'})`, transform.XPathOptions{Version: transform.XPath31}) + if err != nil { + t.Fatal(err) + } + value, err := p.Evaluate(ctx, nil, transform.XPathContext{}) + if value.Len() != 0 || !matchesComprehensiveCode(err, "XPTY0004") { + t.Fatalf("got %v err %v", value, err) + } + }) + t.Run("serialize extension namespace ignored control", func(t *testing.T) { + ctx, cancel := context.WithTimeout(t.Context(), 3*time.Second) + defer cancel() + e := engine(t, transform.Config{}) + p, err := e.CompileXPath(ctx, `serialize(parse-xml('ok'),map{QName('urn:foreign','v:method'):'text'})`, transform.XPathOptions{Version: transform.XPath31}) + if err != nil { + t.Fatal(err) + } + value, err := p.Evaluate(ctx, nil, transform.XPathContext{}) + got, textErr := value.Text() + if err != nil || textErr != nil || got != "ok" { + t.Fatalf("got %q err %v textErr %v", got, err, textErr) + } + }) + }) + t.Run("temporal", func(t *testing.T) { + dyn := transform.DynamicContext{CurrentTime: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC), ImplicitTimezone: time.FixedZone("plus2", 2*60*60)} + for _, tc := range []struct{ name, decl, body, want string }{ + {"sort honors explicit implicit zone", "", "||", "true|2000-01-01T00:00:00|1999-12-31T23:00:00Z|"}, + {"merge honors explicit implicit zone", "", "|", "2000-01-01T00:00:00|1999-12-31T23:00:00Z|"}, + {"key dateTime implicit equality bucket", "", "", "true|1"}, + {"key dateTime implicit false equality", "", "", "false|0"}, + {"key date implicit zone", ``, ``, "true|1"}, + {"key time implicit zone", ``, ``, "true|1"}, + {"key composite implicit zone", ``, ``, "1"}, + {"key Gregorian implicit zone", ``, ``, "true|1"}, + + {"key nozone same value positive", "", "", "1"}, + {"key explicit zones positive", "", "", "1"}, + } { + t.Run(tc.name, func(t *testing.T) { remainingTransform(t, tc.decl, tc.body, dyn, tc.want, "") }) + } + }) + t.Run("Gregorian keys", func(t *testing.T) { + dyn := transform.DynamicContext{CurrentTime: time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC), ImplicitTimezone: time.FixedZone("plus2", 2*60*60)} + for _, tc := range []struct{ kind, lexical string }{ + {"gYear", "2000"}, {"gYearMonth", "2000-01"}, {"gMonth", "--01"}, {"gDay", "---15"}, {"gMonthDay", "--01-15"}, {"gYear", "-0001"}, + } { + t.Run(tc.kind+tc.lexical, func(t *testing.T) { + for _, zone := range []struct{ suffix, want string }{{"+02:00", "true|1"}, {"+03:00", "false|0"}} { + t.Run(zone.suffix, func(t *testing.T) { + plain := "xs:" + tc.kind + "('" + tc.lexical + "')" + zoned := "xs:" + tc.kind + "('" + tc.lexical + zone.suffix + "')" + decl := `` + body := `` + remainingTransform(t, decl, body, dyn, zone.want, "") + }) + } + }) + } + for _, tc := range []struct{ name, decl, body, want string }{ + {"composite", ``, ``, "1"}, + {"different types", ``, ``, "0"}, + } { + t.Run(tc.name, func(t *testing.T) { remainingTransform(t, tc.decl, tc.body, dyn, tc.want, "") }) + } + }) + + t.Run("key axes", func(t *testing.T) { + for _, tc := range []struct{ name, decl, body, want, code string }{ + {"key self node parented excluded", "", "", "0", ""}, + {"key descendant or self parented excluded", "", "", "0", ""}, + {"self key equivalent expression excludes parented attr", "", ``, "false", ""}, + {"self key attribute path eligibility", ``, ``, "1", ""}, + {"descendant self attribute path eligibility", ``, ``, "1", ""}, + {"direct attribute self path pattern", `hit`, ``, "hit", ""}, + + {"attribute self path singleton focus", `hit`, ``, "hit", ""}, + {"attribute repeated self path", `hit`, ``, "hit", ""}, + {"namespace self path key", ``, ``, "1", ""}, + {"namespace self path equivalent positive", "", ``, "true", ""}, + {"namespace parented self excluded", ``, ``, "0", ""}, + {"attribute self path equivalent expression positive", "", ``, "true", ""}, + {"self pattern parentless attribute", `hit`, `x`, "hit", ""}, + {"self pattern parentless equivalent positive", "", `x`, "true", ""}, + + {"key attribute wildcard positive", "", "", "1", ""}, + {"direct self node parented excluded", "hit", "", "x", ""}, + {"context item self filter positive", "hit", "", "hit", ""}, + {"reverse axis unsupported", "", "", "", "XTSE0340"}, + } { + t.Run(tc.name, func(t *testing.T) { + remainingTransform(t, tc.decl, tc.body, transform.DynamicContext{}, tc.want, tc.code) + }) + } + }) + t.Run("accumulator references", func(t *testing.T) { + for _, tc := range []struct{ name, decl, body, want, code string }{ + {"unprefixed", "", "", "", "XTDE3400"}, + {"explicit fn", "", "", "", "XTDE3400"}, + {"local alias", "", "", "", "XTDE3400"}, + {"EQName", "", "", "", "XTDE3400"}, + {"alias local foreign shadows global fn", "", "", "42", ""}, + {"alias local fn shadows global foreign", "", "", "", "XTDE3400"}, + } { + t.Run(tc.name, func(t *testing.T) { + remainingTransform(t, tc.decl, tc.body, transform.DynamicContext{}, tc.want, tc.code) + }) + } + }) + t.Run("resource boundaries", remainingResourceProbes) +} + +type remainingReader struct { + data *strings.Reader + readErr, closeErr error + closed bool + onRead func() +} + +func (r *remainingReader) Read(p []byte) (int, error) { + if r == nil { + return 0, errors.New("typed nil invoked") + } + if r.onRead != nil { + r.onRead() + r.onRead = nil + } + if r.data == nil { + return 0, r.readErr + } + n, err := r.data.Read(p) + if err == io.EOF && r.readErr != nil { + err = r.readErr + } + return n, err +} + +func (r *remainingReader) Close() error { + if r == nil { + return errors.New("typed nil close invoked") + } + r.closed = true + return r.closeErr +} + +func remainingResourceProbes(t *testing.T) { + sentinel := errors.New("ordinary read failure") + for _, tc := range []struct { + name, data string + readErr, closeErr, resolveErr, wantErr error + limit int + want string + cancelRead bool + }{ + {name: "stream valid positive", data: "", want: "true"}, + {name: "stream ordinary read false", readErr: sentinel, want: "false"}, + {name: "stream ordinary close false", data: "", closeErr: sentinel, want: "false"}, + {name: "stream canceled read", readErr: context.Canceled, wantErr: context.Canceled}, + {name: "stream canceled close", data: "", closeErr: context.Canceled, wantErr: context.Canceled}, + {name: "stream limit read", readErr: transform.ErrLimit, wantErr: transform.ErrLimit}, + {name: "stream resource byte limit", data: strings.Repeat(" ", 1024) + "", limit: 512, wantErr: transform.ErrLimit}, + {name: "stream actual cancellation", data: "", cancelRead: true, wantErr: context.Canceled}, + {name: "stream ordinary resolve false", resolveErr: sentinel, want: "false"}, + {name: "stream canceled resolve", resolveErr: context.Canceled, wantErr: context.Canceled}, + } { + t.Run(tc.name, func(t *testing.T) { + ctx, cancel := context.WithTimeout(t.Context(), 3*time.Second) + defer cancel() + r := &remainingReader{readErr: tc.readErr, closeErr: tc.closeErr} + if tc.data != "" { + r.data = strings.NewReader(tc.data) + } + if tc.cancelRead { + r.onRead = cancel + } + e := engine(t, transform.Config{Limits: transform.Limits{MaxInputBytes: tc.limit}, Resolver: transform.ResolverFunc(func(context.Context, string) (io.ReadCloser, error) { return r, tc.resolveErr })}) + ss, err := e.CompileXSLT(ctx, []byte(remainingSheet("", ``)), transform.XSLTOptions{}) + if err != nil { + t.Fatal(err) + } + d, err := e.Parse(ctx, []byte("")) + if err != nil { + t.Fatal(err) + } + out, err := ss.Transform(ctx, d, transform.XSLTContext{}) + if !r.closed { + t.Error("reader not closed") + } + if tc.wantErr != nil { + if out != nil || !errors.Is(err, tc.wantErr) { + t.Fatalf("out %v err %v want cause %v", out, err, tc.wantErr) + } + return + } + if err != nil || out == nil || string(out.Primary) != tc.want { + t.Fatalf("out %v err %v want %q", out, err, tc.want) + } + }) + } + for _, kind := range []string{"success", "reader and error", "typed nil"} { + t.Run("transform resolver "+kind, func(t *testing.T) { + ctx, cancel := context.WithTimeout(t.Context(), 3*time.Second) + defer cancel() + r := &remainingReader{data: strings.NewReader(remainingSheet("", `ok`))} + e := engine(t, transform.Config{Resolver: transform.ResolverFunc(func(context.Context, string) (io.ReadCloser, error) { + if kind == "typed nil" { + var missing *remainingReader + return missing, nil + } + if kind == "reader and error" { + return r, sentinel + } + return r, nil + })}) + p, err := e.CompileXPath(ctx, `transform(map{'stylesheet-location':'urn:s','source-node':parse-xml(''),'delivery-format':'serialized'})?output`, transform.XPathOptions{Version: transform.XPath31}) + if err != nil { + t.Fatal(err) + } + value, err := p.Evaluate(ctx, nil, transform.XPathContext{}) + if kind == "typed nil" { + if value != (transform.Sequence{}) || !errors.Is(err, transform.ErrResource) { + t.Fatalf("value %v err %v", value, err) + } + return + } + if !r.closed { + t.Fatal("reader not closed") + } + if kind == "reader and error" { + if value != (transform.Sequence{}) || !errors.Is(err, sentinel) { + t.Fatalf("value %v err %v", value, err) + } + return + } + got, textErr := value.Text() + if err != nil || textErr != nil || got != "ok" { + t.Fatalf("value %q err %v textErr %v", got, err, textErr) + } + }) + } +} + +func TestSerializationParameterMapSharedBoundary(t *testing.T) { + textKey := func(name string) xdm.AtomicValue { return xdm.AtomicValue{TypeName: xdm.TypeString, Value: name} } + for _, tc := range []struct { + name string + params xdm.MapItem + code string + check func(xpath.SerializationParameters) bool + }{ + {"no defaults", xdm.MapItem{}, "", func(p xpath.SerializationParameters) bool { + return len(p.Strings)+len(p.Booleans)+len(p.Names)+len(p.Reset) == 0 && !p.CharactersSet + }}, + {"explicit false", xdm.MapItem{}.Put(textKey("indent"), xdm.SingleBoolean(false)), "", func(p xpath.SerializationParameters) bool { + v, ok := p.Booleans["indent"] + return ok && !v && len(p.Reset) == 0 && len(p.Strings) == 0 + }}, + {"explicit BOM false", xdm.MapItem{}.Put(textKey("byte-order-mark"), xdm.SingleBoolean(false)), "", func(p xpath.SerializationParameters) bool { v, ok := p.Booleans["byte-order-mark"]; return ok && !v }}, + {"empty sequence reset", xdm.MapItem{}.Put(textKey("indent"), nil), "", func(p xpath.SerializationParameters) bool { + return len(p.Reset) == 1 && p.Reset[0] == "indent" && len(p.Booleans) == 0 + }}, + {"empty character map supplied", xdm.MapItem{}.Put(textKey("use-character-maps"), xdm.ItemSlice{xdm.MapItem{}}), "", func(p xpath.SerializationParameters) bool { + return p.CharactersSet && len(p.Characters) == 0 && len(p.Reset) == 0 + }}, + {"empty name list reset", xdm.MapItem{}.Put(textKey("cdata-section-elements"), nil), "", func(p xpath.SerializationParameters) bool { + return len(p.Reset) == 1 && p.Reset[0] == "cdata-section-elements" && len(p.Names) == 0 + }}, + {"foreign QName ignored", xdm.MapItem{}.Put(xdm.AtomicValue{TypeName: xdm.TypeQName, Value: xdm.QNameValue{URI: "urn:foreign", Local: "method"}}, xdm.SingleString("text")), "", func(p xpath.SerializationParameters) bool { + return len(p.Strings)+len(p.Booleans)+len(p.Names)+len(p.Reset) == 0 + }}, + {"boolean string rejected", xdm.MapItem{}.Put(textKey("indent"), xdm.SingleString("yes")), "XPTY0004", nil}, + {"method union untyped rejected", xdm.MapItem{}.Put(textKey("method"), xdm.ItemSlice{xdm.AtomicValue{TypeName: xdm.TypeUntypedAtomic, Value: "text"}}), "XPTY0117", nil}, + {"method numeric rejected", xdm.MapItem{}.Put(textKey("method"), xdm.SingleInteger(1)), "XPTY0004", nil}, + } { + t.Run(tc.name, func(t *testing.T) { + got, err := xpath.ReadSerializationParameterMap(t.Context(), tc.params, 31) + if tc.code != "" { + var detail *xdm.Error + if !errors.As(err, &detail) || detail.Code != tc.code { + t.Fatalf("value %#v err %v want %s", got, err, tc.code) + } + return + } + if err != nil || !tc.check(got) { + t.Fatalf("value %#v err %v", got, err) + } + }) + } +} + +func TestTransformSerializationInheritance(t *testing.T) { + for _, tc := range []struct{ name, decl, body, params, want string }{ + {"omitted retains output", ``, ``, `map{}`, "1|2"}, + {"empty separator resets", ``, ``, `map{'item-separator':()}`, "1 2"}, + {"empty string separator overrides", ``, ``, `map{'item-separator':''}`, "12"}, + {"false overrides BOM", ``, `abc`, `map{'byte-order-mark':false()}`, "abc"}, + {"empty resets BOM", ``, `abc`, `map{'byte-order-mark':()}`, "abc"}, + {"empty charmap augments inherited", ``, `a`, `map{'use-character-maps':map{}}`, "A"}, + {"empty charmap sequence resets", ``, `a`, `map{'use-character-maps':()}`, "a"}, + {"charmap entries augment", ``, `ab`, `map{'use-character-maps':map{'b':'B'}}`, "AB"}, + {"cdata names augment", ``, `xy`, `map{'cdata-section-elements':QName('','b')}`, ""}, + {"cdata names empty reset", ``, `x`, `map{'cdata-section-elements':()}`, "x"}, + } { + t.Run(tc.name, func(t *testing.T) { + ctx, cancel := context.WithTimeout(t.Context(), 3*time.Second) + defer cancel() + e := engine(t, transform.Config{}) + source := `` + tc.decl + `` + tc.body + `` + q, err := e.CompileXPath(ctx, `transform(map{'stylesheet-text':$sheet,'initial-template':QName('','start'),'delivery-format':'serialized','serialization-params':`+tc.params+`})?output`, transform.XPathOptions{Version: transform.XPath31}) + if err != nil { + t.Fatal(err) + } + result, err := q.Evaluate(ctx, nil, transform.XPathContext{Variables: transform.Bindings{{Local: "sheet"}: transform.Values(transform.String(source))}}) + if err != nil { + t.Fatal(err) + } + got, err := result.Text() + if err != nil || got != tc.want { + t.Fatalf("got %q err %v want %q", got, err, tc.want) + } + }) + } +} + +func TestTransformSelectedProcessorApplicability(t *testing.T) { + const source = `` + for _, extra := range []string{"", ",'initial-function':42,'function-params':[]"} { + t.Run(extra, func(t *testing.T) { + ctx, cancel := context.WithTimeout(t.Context(), 3*time.Second) + defer cancel() + e := engine(t, transform.Config{}) + q, err := e.CompileXPath(ctx, `transform(map{'stylesheet-text':$sheet,'source-node':parse-xml(''),'xslt-version':1,'delivery-format':'serialized'`+extra+`})?output`, transform.XPathOptions{Version: transform.XPath31}) + if err != nil { + t.Fatal(err) + } + result, err := q.Evaluate(ctx, nil, transform.XPathContext{Variables: transform.Bindings{{Local: "sheet"}: transform.Values(transform.String(source))}}) + if extra != "" { + if result.Len() != 0 || !matchesComprehensiveCode(err, "XPTY0004") { + t.Fatalf("result %v err %v", result, err) + } + return + } + got, textErr := result.Text() + if err != nil || textErr != nil || got != "3.0" { + t.Fatalf("got %q err %v textErr %v", got, err, textErr) + } + }) + } +} + +func TestGregorianReferenceDateSharedBoundary(t *testing.T) { + for _, tc := range []struct { + name string + value xdm.AtomicValue + want string + valid bool + }{ + {"year", xdm.AtomicValue{TypeName: xdm.TypeGYear, Value: "2000+02:00"}, "2000-01-01T00:00:00+02:00", true}, + {"signed year", xdm.AtomicValue{TypeName: xdm.TypeGYear, Value: "-0001Z"}, "-0001-01-01T00:00:00Z", true}, + {"year month", xdm.AtomicValue{TypeName: xdm.TypeGYearMonth, Value: "2000-02Z"}, "2000-02-01T00:00:00Z", true}, + {"month", xdm.AtomicValue{TypeName: xdm.TypeGMonth, Value: "--02Z"}, "1972-02-01T00:00:00Z", true}, + {"day", xdm.AtomicValue{TypeName: xdm.TypeGDay, Value: "---15Z"}, "1972-12-15T00:00:00Z", true}, + {"month day", xdm.AtomicValue{TypeName: xdm.TypeGMonthDay, Value: "--02-29Z"}, "1972-02-29T00:00:00Z", true}, + {"schema derived", xdm.AtomicValue{TypeName: "Q{urn:test}year", BaseType: xdm.TypeGYear, Value: "2000Z"}, "2000-01-01T00:00:00Z", true}, + {"other atomic", xdm.AtomicValue{TypeName: xdm.TypeString, Value: "2000Z"}, "", false}, + {"non lexical payload", xdm.AtomicValue{TypeName: xdm.TypeGYear, Value: int64(2000)}, "", false}, + {"invalid lexical", xdm.AtomicValue{TypeName: xdm.TypeGYear, Value: "invalid"}, "", false}, + } { + t.Run(tc.name, func(t *testing.T) { + got, valid := xpath.GregorianToReferenceDateTime(tc.value) + if valid != tc.valid || valid && got.Format(time.RFC3339Nano) != tc.want { + t.Fatalf("got %v valid %v want %q valid %v", got, valid, tc.want, tc.valid) + } + }) + } +} + +func TestTransformSerializationVersionReset(t *testing.T) { + ctx, cancel := context.WithTimeout(t.Context(), 3*time.Second) + defer cancel() + e := engine(t, transform.Config{}) + serialize := func(t *testing.T, output, params string) string { + t.Helper() + style := `` + p, err := e.CompileXPath(ctx, `transform(map{'stylesheet-text':$s,'initial-template':QName('','start'),'delivery-format':'serialized','serialization-params':`+params+`})?output`, transform.XPathOptions{Version: transform.XPath31}) + if err != nil { + t.Fatal(err) + } + value, err := p.Evaluate(ctx, nil, transform.XPathContext{Variables: transform.Bindings{{Local: "s"}: transform.Values(transform.String(style))}}) + if err != nil { + if value.Len() != 0 { + t.Fatalf("error with result %v: %v", value, err) + } + t.Fatal(err) + } + got, err := value.Text() + if err != nil { + t.Fatal(err) + } + return got + } + for _, tc := range []struct{ method, explicit string }{{"xml", "1.1"}, {"html", "4.0"}, {"xhtml", "1.1"}} { + t.Run(tc.method, func(t *testing.T) { + baseline := serialize(t, `method="`+tc.method+`"`, `map{}`) + inherited := serialize(t, `method="`+tc.method+`" version="`+tc.explicit+`"`, `map{}`) + if inherited == baseline { + t.Fatalf("explicit version failed to affect output: %q", inherited) + } + got := serialize(t, `method="`+tc.method+`" version="`+tc.explicit+`"`, `map{'version':()}`) + if got != baseline { + t.Fatalf("reset %q want default %q", got, baseline) + } + }) + } + for _, tc := range []struct{ name, output, params, baseline string }{ + {"method then reset version", `method="xml" version="1.1"`, `map{'method':'html','version':()}`, `method="html"`}, + {"reset version then method", `method="xml" version="1.1"`, `map{'version':(),'method':'html'}`, `method="html"`}, + {"reset method and version", `method="html" version="4.0"`, `map{'method':(),'version':()}`, ""}, + {"html version inherited override", `method="html" version="4.0" html-version="4.0"`, `map{'version':()}`, `method="html" html-version="4.0"`}, + {"html version supplied override", `method="html" version="4.0"`, `map{'version':(),'html-version':5}`, `method="html"`}, + } { + t.Run(tc.name, func(t *testing.T) { + baseline := serialize(t, tc.baseline, `map{}`) + got := serialize(t, tc.output, tc.params) + if got != baseline { + t.Fatalf("reset %q want default %q", got, baseline) + } + }) + } +} + +func TestXSLTIndexedSelfAxisSideNodes(t *testing.T) { + t.Parallel() + for _, route := range []struct{ name, pattern, selectExpr string }{ + {"attribute self", "@*/self::node()", "r/@a"}, + {"attribute descendant or self", "@*/descendant-or-self::node()", "r/@a"}, + {"namespace self", "namespace::*/self::node()", "r/namespace::xml"}, + {"namespace descendant or self", "namespace::*/descendant-or-self::node()", "r/namespace::xml"}, + {"direct attribute control", "@*", "r/@a"}, + {"direct namespace control", "namespace::*", "r/namespace::xml"}, + {"element self control", "*/self::node()", "r"}, + } { + for _, size := range []int{15, 16} { + t.Run(fmt.Sprintf("%s/%d", route.name, size), func(t *testing.T) { + t.Parallel() + var declarations strings.Builder + declarations.WriteString(``) + fmt.Fprintf(&declarations, `H`, route.pattern) + for i := 1; i < size; i++ { + fmt.Fprintf(&declarations, `unexpected`, i) + } + source := dispatchRegressionSheet(declarations.String(), ``) + dispatchRegressionCheck(t, source, "H", "") + }) + } + } +} + +func TestXSLTAtomicNextMatchConflicts(t *testing.T) { + t.Parallel() + for _, tc := range []struct { + name, pattern, selection, mode string + high, second bool + want, code string + }{ + {"atomic next conflict", `.[. instance of xs:integer]`, "1", "fail", true, true, "", "XTDE0540"}, + {"node next conflict control", "r", "r", "fail", true, true, "", "XTDE0540"}, + {"atomic unique next control", `.[. instance of xs:integer]`, "1", "fail", true, false, "A", ""}, + {"atomic initial conflict control", `.[. instance of xs:integer]`, "1", "fail", false, true, "", "XTDE0540"}, + {"atomic use last control", `.[. instance of xs:integer]`, "1", "use-last", true, true, "B", ""}, + } { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + var declarations strings.Builder + fmt.Fprintf(&declarations, ``, tc.mode) + if tc.high { + fmt.Fprintf(&declarations, ``, tc.pattern) + } + fmt.Fprintf(&declarations, `A`, tc.pattern) + if tc.second { + fmt.Fprintf(&declarations, `B`, tc.pattern) + } + source := dispatchRegressionSheet(declarations.String(), ``) + dispatchRegressionCheck(t, source, tc.want, tc.code) + }) + } +} + +func dispatchRegressionSheet(declarations, body string) string { + return `` + declarations + `` + body + `` +} + +func dispatchRegressionCheck(t *testing.T, source, want, code string) { + t.Helper() + e, err := transform.NewEngine(transform.Config{}) + if err != nil { + t.Fatal(err) + } + // Exactly three stored document/element/attribute nodes; namespace wrappers + // are enumerated only by the namespace-axis cases. + doc, err := e.Parse(t.Context(), []byte(``)) + if err != nil { + t.Fatal(err) + } + sheet, err := e.CompileXSLT(t.Context(), []byte(source), transform.XSLTOptions{}) + if err != nil { + t.Fatal(err) + } + result, err := sheet.Transform(t.Context(), doc, transform.XSLTContext{Template: transform.Name{Local: "start"}}) + if code != "" { + var detail *transform.Error + if result != nil || !errors.As(err, &detail) || detail.Code != code { + t.Fatalf("result=%v error=%v; want nil result and %s", result, err, code) + } + return + } + if err != nil || result == nil { + t.Fatalf("result=%v error=%v", result, err) + } + if got := string(result.Primary); got != want { + t.Fatalf("output=%q; want %q", got, want) + } +} + +func TestXSLTAtomicNextMatchFallback(t *testing.T) { + t.Parallel() + for _, test := range []struct{ name, declarations, want string }{ + {"no next rule", ``, ""}, + } { + t.Run(test.name, func(t *testing.T) { + t.Parallel() + source := dispatchRegressionSheet(test.declarations, ``) + dispatchRegressionCheck(t, source, test.want, "") + }) + } +} + +func TestXSLTIntegerGroupingFitsSharedWorkBudget(t *testing.T) { + e := engine(t, transform.Config{Limits: transform.Limits{MaxOperations: 2000}}) + // This is the original pure-integer budget fixture. Exact indexing removes + // its quadratic comparisons; mixed fallback keeps the negative budget test. + body := `` + got := integerIndexOutput(t, e, nil, "", body) + if got != strings.Repeat("1", 100) { + t.Fatal("pure integer groups changed", got) + } +} + +func TestXSLTIntegerGroupingPreservesFallback(t *testing.T) { + const summary = `concat(xs:integer(current-grouping-key()),':',count(current-group()),':',current-grouping-key() instance of xs:integer,':',current-grouping-key() instance of xs:unsignedByte)` + for _, test := range []struct { + name string + population string + key string + summary string + want string + }{ + {"first appearance", "(3,1,3,2,1)", ".", summary, "3:2:true:false|1:2:true:false|2:1:true:false"}, + {"first derived type", "(xs:unsignedByte(2),xs:integer(2),xs:short(1))", ".", summary, "2:2:true:true|1:1:true:false"}, + {"signed int64 boundaries", "(-9223372036854775808,9223372036854775807,-9223372036854775808)", ".", summary, "-9223372036854775808:2:true:false|9223372036854775807:1:true:false"}, + {"late float owns group", "(1,xs:float(16777216),16777217)", ".", summary, "1:1:true:false|16777216:2:false:false"}, + {"late float first matching group", "(1,16777216,16777217,xs:float(16777216),16777217)", ".", summary, "1:1:true:false|16777216:2:true:false|16777217:2:true:false"}, + {"late decimal owns group", "(1,xs:decimal('2.0'),2)", ".", summary, "1:1:true:false|2:2:false:false"}, + {"late double owns group", "(1,xs:double('9007199254740992'),9007199254740993)", ".", summary, "1:1:true:false|9007199254740992:2:false:false"}, + {"late oversized integer", "(1,9223372036854775808,9223372036854775808)", ".", summary, "1:1:true:false|9223372036854775808:2:true:false"}, + {"nontransitive after prefix", "(9,xs:float('1'),xs:decimal('1.0000000000100000000001'),xs:double('1.00000000001'))", ".", summary, "9:1:true:false|1:2:false:false|1:1:false:false"}, + {"empty key interrupts prefix", "(1,2,3)", "if (. eq 2) then () else .", summary, "1:1:true:false|3:1:true:false"}, + {"multiple keys preserve membership", "(1,2,3)", "if (. eq 2) then (1,1,3) else .", summary, "1:2:true:false|3:2:true:false"}, + {"string stays distinct", "(1,'1',1)", ".", summary, "1:2:true:false|1:1:false:false"}, + {"NaN fallback", "(1,xs:float('NaN'),xs:double('NaN'))", ".", `concat(string(current-grouping-key()),':',count(current-group()),':',current-grouping-key() instance of xs:float)`, "1:1:false|NaN:2:true"}, + } { + t.Run(test.name, func(t *testing.T) { + e := engine(t, transform.Config{}) + body := `` + if got := integerIndexOutput(t, e, nil, "", body); got != test.want { + t.Fatal(got, test.want) + } + }) + } +} + +func TestXSLTIntegerKeyCandidatesPreserveQueries(t *testing.T) { + for _, test := range []struct { + name string + extras []string + use string + queries string + want string + }{ + {"float after index", []string{"16777216", "16777217"}, `xs:integer(@v)`, `(1,2,3,xs:float(16777216),16777217)`, "1|2|3|65,66|66"}, + {"double after index", []string{"9007199254740992", "9007199254740993"}, `xs:integer(@v)`, `(1,2,3,xs:double('9007199254740992'),9007199254740993)`, "1|2|3|65,66|66"}, + {"decimal after index", nil, `xs:integer(@v)`, `(1,2,3,xs:decimal('4.0'),4)`, "1|2|3|4|4"}, + {"oversized lookup after index", nil, `xs:integer(@v)`, `(1,2,3,9223372036854775808,4)`, "1|2|3||4"}, + {"mixed late entry disables", []string{"16777217", "16777216"}, `if (@v eq '16777216') then xs:float(@v) else xs:integer(@v)`, `(1,2,3,16777217,16777217)`, "1|2|3|65,66|65,66"}, + {"integer subtype entries", nil, `xs:unsignedLong(@v)`, `(1,2,3,64)`, "1|2|3|64"}, + {"multiple original types", nil, `(xs:integer(@v),xs:short(@v))`, `(1,2,3,64)`, "1|2|3|64"}, + } { + t.Run(test.name, func(t *testing.T) { + e := engine(t, transform.Config{}) + doc := integerIndexDocument(t, e, 64, test.extras, "") + declaration := `` + body := `` + if got := integerIndexOutput(t, e, doc, declaration, body); got != test.want { + t.Fatal(got, test.want) + } + }) + } +} + +func TestXSLTIntegerKeyOrderThresholdAndComposite(t *testing.T) { + for _, size := range []int{63, 64, 65} { + for _, composite := range []bool{false, true} { + t.Run(fmt.Sprintf("%d/composite=%t", size, composite), func(t *testing.T) { + e := engine(t, transform.Config{}) + doc := integerIndexDocument(t, e, size, nil, "") + attribute := "" + if composite { + attribute = ` composite="yes"` + } + declaration := `` + body := fmt.Sprintf(``, size) + want := "1|2|3|" + strconv.Itoa(size) + if got := integerIndexOutput(t, e, doc, declaration, body); got != want { + t.Fatal(got, want) + } + if !composite { + body = fmt.Sprintf(``, size) + want = "1,2,3," + strconv.Itoa(size) + if got := integerIndexOutput(t, e, doc, declaration, body); got != want { + t.Fatal(got, want) + } + } + }) + } + } +} + +func TestXSLTIntegerKeyConcurrentInvocationIsolation(t *testing.T) { + t.Parallel() + + e := engine(t, transform.Config{}) + body := `` + compiled := stylesheet(t, e, sheet(version30, body)) + for _, prefix := range []string{"a", "b"} { + d := integerIndexDocument(t, e, 64, nil, prefix) + for repeat := range 4 { + t.Run(fmt.Sprintf("%s/%d", prefix, repeat), func(t *testing.T) { + t.Parallel() + result, err := compiled.Transform(t.Context(), d, transform.XSLTContext{Template: transform.Name{Local: "start"}}) + want := prefix + "1|" + prefix + "2|" + prefix + "3|" + prefix + "64" + if err != nil || string(result.Primary) != want { + t.Fatal(result, err, want) + } + }) + } + } +} + +func TestXSLTIntegerKeySharedWorkStillBounded(t *testing.T) { + e := engine(t, transform.Config{Limits: transform.Limits{MaxOperations: 5000}}) + d := integerIndexDocument(t, e, 64, nil, "") + body := `` + compiled := stylesheet(t, e, sheet(version30, body)) + result, err := compiled.Execute(t.Context(), d, transform.XSLTContext{Template: transform.Name{Local: "start"}}) + if result != nil || !errors.Is(err, transform.ErrLimit) { + t.Fatal("indexed misses bypassed shared work", result, err) + } +} + +func integerIndexOutput(t *testing.T, e *transform.Engine, source *transform.Document, declarations, body string) string { + t.Helper() + compiled := stylesheet(t, e, sheet(version30, declarations+``+body+``)) + result, err := compiled.Transform(t.Context(), source, transform.XSLTContext{Template: transform.Name{Local: "start"}}) + if err != nil { + t.Fatal(err) + } + return string(result.Primary) +} + +func integerIndexDocument(t *testing.T, e *transform.Engine, size int, extras []string, prefix string) *transform.Document { + t.Helper() + var builder strings.Builder + builder.WriteString("") + for i := 1; i <= size; i++ { + fmt.Fprintf(&builder, ``, i, prefix, i) + } + for i, value := range extras { + fmt.Fprintf(&builder, ``, value, prefix, size+i+1) + } + builder.WriteString("") + d, err := e.Parse(t.Context(), []byte(builder.String())) + if err != nil { + t.Fatal(err) + } + return d +} + +func TestXSLTNumericKeyRounding(t *testing.T) { + const ( + decimalPlus = "xs:decimal('1.000000059604644775390625000000001')" + decimalMid = "xs:decimal('1.000000059604644775390625')" + decimalMinus = "xs:decimal('1.000000059604644775390624999999999')" + floatHigh = "xs:float('1.00000011920928955078125')" + floatLow = "xs:float('1')" + doubleMid = "xs:double('1.000000059604644775390625')" + largeInteger = "72057598332895233" + largeFloatHigh = "xs:float('72057602627862528')" + ) + for _, composite := range []bool{false, true} { + t.Run(fmt.Sprintf("Composite=%t", composite), func(t *testing.T) { + for _, test := range []struct { + name string + indexed string + lookup string + equal bool + count int64 + }{ + {"decimal-above-vs-float-high", decimalPlus, floatHigh, true, 1}, + {"float-high-vs-decimal-above", floatHigh, decimalPlus, true, 1}, + {"negative-decimal-above-vs-float-high", "-" + decimalPlus, "-" + floatHigh, true, 1}, + {"integer-above-vs-float-high", largeInteger, largeFloatHigh, true, 1}, + {"float-high-vs-integer-above", largeFloatHigh, largeInteger, true, 1}, + {"exact-midpoint-vs-float-low", decimalMid, floatLow, true, 1}, + {"below-midpoint-vs-float-low", decimalMinus, floatLow, true, 1}, + {"above-midpoint-vs-float-low", decimalPlus, floatLow, false, 0}, + {"exact-midpoint-vs-float-high", decimalMid, floatHigh, false, 0}, + {"below-midpoint-vs-float-high", decimalMinus, floatHigh, false, 0}, + {"float-high-self", floatHigh, floatHigh, true, 1}, + {"decimal-above-self", decimalPlus, decimalPlus, true, 1}, + {"decimal-above-vs-double-midpoint", decimalPlus, doubleMid, true, 1}, + {"double-midpoint-vs-decimal-above", doubleMid, decimalPlus, true, 1}, + {"decimal-below-vs-double-midpoint", decimalMinus, doubleMid, true, 1}, + {"decimal-collision-refines-not-equal", decimalPlus, decimalMid, false, 0}, + {"old-integer-float-promotion", "16777217", "xs:float('16777216')", true, 1}, + {"signed-zero", "xs:double('-0')", "0", true, 1}, + {"integer-above-vs-double-midpoint", largeInteger, "xs:double('72057598332895232')", true, 1}, + {"large-distinct-integers-refine", "9007199254740992", "9007199254740993", false, 0}, + } { + t.Run(test.name, func(t *testing.T) { + checkNumericKeyLookupRounding(t, composite, test.indexed, test.lookup, "("+test.indexed+") eq ("+test.lookup+")", test.equal, test.count) + }) + } + }) + } +} + +func TestXSLTNumericKeyMixedControls(t *testing.T) { + t.Run("ordinary-multiple-indexed-types", func(t *testing.T) { + checkNumericKeyLookupRounding(t, false, "(16777217,xs:float('16777216'))", "16777216", "some $v in (16777217,xs:float('16777216')) satisfies $v eq 16777216", true, 1) + }) + t.Run("composite-multiple-position-rounding", func(t *testing.T) { + checkNumericKeyLookupRounding(t, true, "(xs:decimal('1.000000059604644775390625000000001'),7)", "(xs:float('1.00000011920928955078125'),7)", "xs:decimal('1.000000059604644775390625000000001') eq xs:float('1.00000011920928955078125')", true, 1) + }) + t.Run("ordinary-nan-excluded", func(t *testing.T) { + checkNumericKeyLookupRounding(t, false, "xs:double('NaN')", "xs:float('NaN')", "xs:double('NaN') eq xs:float('NaN')", false, 0) + }) + t.Run("composite-nan-deep-equal", func(t *testing.T) { + checkNumericKeyLookupRounding(t, true, "xs:double('NaN')", "xs:float('NaN')", "xs:double('NaN') eq xs:float('NaN')", false, 1) + }) +} + +func checkNumericKeyLookupRounding(t *testing.T, composite bool, indexed, lookup, equality string, wantEqual bool, wantCount int64) { + t.Helper() + engine, err := transform.NewEngine(transform.Config{}) + if err != nil { + t.Fatal(err) + } + compositeAttribute := "" + if composite { + compositeAttribute = ` composite="yes"` + } + source := `` + compiled, err := engine.CompileXSLT(t.Context(), []byte(source), transform.XSLTOptions{Version: transform.XSLT30}) + if err != nil { + t.Fatal("compile", err) + } + result, err := compiled.Execute(t.Context(), nil, transform.XSLTContext{Template: transform.Name{Local: "probe"}, ResultMode: transform.ResultRaw}) + if err != nil { + t.Fatal("execute", err) + } + if result.Primary().Len() != 2 { + t.Fatal("expected equality and count", result.Primary().Len()) + } + equalityItem, _ := result.Primary().At(0) + gotEqual, err := equalityItem.Boolean() + if err != nil || gotEqual != wantEqual { + t.Fatal("pairwise equality control", gotEqual, err, wantEqual) + } + countItem, _ := result.Primary().At(1) + gotCount, err := countItem.Integer() + if err != nil || !gotCount.IsInt64() || gotCount.Int64() != wantCount { + t.Fatal("key lost or added equality candidate", gotCount, err, wantCount) + } +} + +func TestXSLTStaticOutputAttributes(t *testing.T) { + t.Parallel() + for _, version := range []string{"2.0", "3.0"} { + falseValue := "no" + if version == "3.0" { + falseValue = "false" + } + for _, tc := range []struct{ name, decl, want, code string }{ + {"missing character-map string", ``, "", "XTSE0010"}, + {"explicit empty character-map string", ``, "y", ""}, + {"ordinary character-map string", ``, "Xy", ""}, + {"missing character control", ``, "", "XTSE0010"}, + {"empty boolean", ``, "", "SEPM0016"}, + {"absent boolean", ``, "xy", ""}, + {"false boolean", ``, "xy", ""}, + {"invalid boolean word control", ``, "", "SEPM0016"}, + } { + if version == "2.0" && tc.code == "SEPM0016" { + tc.code = "XTSE0020" + } + t.Run(fmt.Sprintf("%s/%s", version, tc.name), func(t *testing.T) { + t.Parallel() + e, err := transform.NewEngine(transform.Config{}) + if err != nil { + t.Fatal(err) + } + source := `` + tc.decl + `xy` + sheet, err := e.CompileXSLT(t.Context(), []byte(source), transform.XSLTOptions{}) + if tc.code != "" { + var detail *transform.Error + if sheet != nil || !errors.As(err, &detail) || detail.Code != tc.code { + t.Fatalf("compiled=%v error=%v; want nil program and %s", sheet, err, tc.code) + } + return + } + if err != nil || sheet == nil { + t.Fatalf("compiled=%v error=%v", sheet, err) + } + doc, err := e.Parse(t.Context(), []byte(``)) + if err != nil { + t.Fatal(err) + } + result, err := sheet.Transform(t.Context(), doc, transform.XSLTContext{Template: transform.Name{Local: "start"}}) + if err != nil || result == nil { + t.Fatalf("result=%v error=%v", result, err) + } + if string(result.Primary) != tc.want { + t.Fatalf("output=%q; want %q", result.Primary, tc.want) + } + }) + } + } +} + +func TestXSLTTransformParameterMapErrors(t *testing.T) { + const header = `` + for _, name := range []string{"stylesheet-params", "static-params", "template-params", "tunnel-params"} { + t.Run(name, func(t *testing.T) { + declaration, parameter := ``, "" + switch name { + case "static-params": + declaration = `` + case "template-params": + declaration, parameter = "", `` + case "tunnel-params": + declaration, parameter = "", `` + } + sheet := header + declaration + `` + parameter + `` + for _, tc := range []struct{ name, value, want, code string }{ + {"bad_key", `map{'debug':true()}`, "", "FOXT0002"}, + {"qname_key", `map{QName('','debug'):true()}`, "true", ""}, + {"empty_map", `map{}`, "false", ""}, + {"absent", "", "false", ""}, + {"empty_value", `()`, "", "XPTY0004"}, + {"non_map", `1`, "", "XPTY0004"}, + {"multiple_maps", `(map{},map{})`, "", "XPTY0004"}, + {"multiple_maps_bad_key", `(map{'debug':true()},map{})`, "", "XPTY0004"}, + } { + t.Run(tc.name, func(t *testing.T) { + option := "" + if tc.value != "" { + option = ",'" + name + "':" + tc.value + } + e := engine(t, transform.Config{}) + p, err := e.CompileXPath(t.Context(), `transform(map{'stylesheet-text':$sheet,'initial-template':QName('','start'),'delivery-format':'serialized'`+option+`})?output`, transform.XPathOptions{Version: transform.XPath31}) + if err != nil { + t.Fatal(err) + } + value, err := p.Evaluate(t.Context(), nil, transform.XPathContext{Variables: transform.Bindings{{Local: "sheet"}: transform.Values(transform.String(sheet))}}) + if tc.code != "" { + var detail *transform.Error + if value.Len() != 0 || !errors.As(err, &detail) || detail.Code != tc.code { + t.Fatalf("result=%v error=%v; want %s", value, err, tc.code) + } + return + } + if err != nil { + t.Fatal(err) + } + got, err := value.Text() + if err != nil || got != tc.want { + t.Fatalf("text=%q error=%v; want %q", got, err, tc.want) + } + }) + } + }) + } + t.Run("initial_mode_type", func(t *testing.T) { + e := engine(t, transform.Config{}) + sheet := header + `mode` + p, err := e.CompileXPath(t.Context(), `transform(map{'stylesheet-text':$sheet,'initial-match-selection':parse-xml(''),'initial-mode':'m','delivery-format':'serialized'})?output`, transform.XPathOptions{Version: transform.XPath31}) + if err != nil { + t.Fatal(err) + } + value, err := p.Evaluate(t.Context(), nil, transform.XPathContext{Variables: transform.Bindings{{Local: "sheet"}: transform.Values(transform.String(sheet))}}) + var detail *transform.Error + if value.Len() != 0 || !errors.As(err, &detail) || detail.Code != "XPTY0004" { + t.Fatalf("result=%v error=%v; want XPTY0004", value, err) + } + }) + t.Run("control_cancellation", func(t *testing.T) { + e := engine(t, transform.Config{}) + p, err := e.CompileXPath(t.Context(), `transform(map{'stylesheet-text':'bad','stylesheet-params':map{'debug':true()}})?output`, transform.XPathOptions{Version: transform.XPath31}) + if err != nil { + t.Fatal(err) + } + ctx, cancel := context.WithCancel(t.Context()) + cancel() + value, err := p.Evaluate(ctx, nil, transform.XPathContext{}) + if value != (transform.Sequence{}) || !errors.Is(err, context.Canceled) { + t.Fatalf("result=%v error=%v; want cancellation cause", value, err) + } + }) +} diff --git a/xades/archive.go b/xades/archive.go index 2e9495d..be5ed44 100644 --- a/xades/archive.go +++ b/xades/archive.go @@ -237,7 +237,7 @@ func (v *Validator) validatePreserved(ctx context.Context, doc *document.Documen func (v *Validator) appendArchive(ctx context.Context, doc *document.Document, selected document.Element, certificate []byte, at time.Time, method string, token *timestamp.Token) (*document.Document, *ValidationResult, error) { limit := v.maxTokenBytes if limit == 0 { - limit = 4 << 20 + limit = defaultTimestampTokenBytes } if token.Size() > limit { @@ -249,14 +249,14 @@ func (v *Validator) appendArchive(ctx context.Context, doc *document.Document, s return nil, nil, e } - archive := node141("ArchiveTimeStamp") - archive.NS = append(archive.NS, tree.Namespace{Prefix: "ds", URI: xmldsig.Namespace}, tree.Namespace{Prefix: nameXades, URI: Namespace}) + archive := node141(nameArchiveTimeStamp) + archive.NS = append(archive.NS, tree.Namespace{Prefix: lexicon.PrefixDSig, URI: xmldsig.Namespace}, tree.Namespace{Prefix: prefixXAdES, URI: Namespace}) cm := dsnode(lexicon.DSigElementCanonicalizationMethod) - attr(cm, "Algorithm", method) + attr(cm, lexicon.SecurityAttrAlgorithm, method) add(archive, cm) - enc := xnode("EncapsulatedTimeStamp") - attr(enc, "Encoding", encodingDER) + enc := xnode(nameEncapsulatedTimeStamp) + attr(enc, lexicon.SecurityAttrEncoding, encodingDER) encoded, e := encodeBase64(ctx, token.Size(), token.WriterTo(ctx)) if e != nil { diff --git a/xades/archive_test.go b/xades/archive_test.go index 149afeb..c873d8c 100644 --- a/xades/archive_test.go +++ b/xades/archive_test.go @@ -167,15 +167,6 @@ func TestArchiveProtectsUnsignedMaterialAndNoFallback(t *testing.T) { } } -func verifyOnlyXML(t *testing.T, d *document.Document, c testcert.Chain) { - t.Helper() - - _, e := xmldsig.VerifySignature(t.Context(), d, xmldsig.Signatures(d)[0], c.Leaf.PublicKey, xmldsig.SignatureOptions{IDAttribute: document.Name{Local: "ID"}, AdditionalIDRules: []reference.IDAttributeRule{{Attribute: document.Name{Local: "Id"}, Elements: []document.Name{{Space: xades.Namespace, Local: signedPropertiesLocal}}}}, ExpectedURIs: nil}) - if e != nil { - t.Fatalf("test mutation damaged primary XMLDSig: %v", e) - } -} - func TestArchiveBudgetsAndWrongToken(t *testing.T) { c, lt, lr, _ := preparedLT(t) small := preservationValidator(t, c, xades.LevelLT, 128) @@ -624,51 +615,6 @@ func TestPreservationContainerOrderAndSchema(t *testing.T) { } } -func preservationValidator(t *testing.T, c testcert.Chain, level xades.Level, limit int) *xades.Validator { - t.Helper() - tsa, _ := archivePolicy(t, c) - - v, e := xades.NewValidator(xades.ValidatorConfig{SignerPKI: pki.Config{Roots: [][]byte{c.Root.Raw}, KeyUsages: nil, Revocation: pki.RequireCRL}, TSA: tsa, MinimumLevel: level, MaxArchiveBytes: limit}) - if e != nil { - t.Fatal(e) - } - - return v -} - -func preparedLT(t *testing.T) (testcert.Chain, *document.Document, *xades.ValidationResult, *xades.Validator) { - t.Helper() - c := testcert.New(t) - tsa, proof := archivePolicy(t, c) - doc := basicDocument(t, c) - timed := xades.TimedOptions{TSA: tsa, At: testcert.Now(), Evidence: proof} - tr := &archiveTransport{key: c.TSAKey, cert: c.TSA.Raw, root: c.Root.Raw, now: testcert.Now().Add(-time.Second)} - - bt, _, e := xades.AddSignatureTimestamp(t.Context(), doc, xmldsig.Signatures(doc)[0], c.Leaf.Raw, tr, xades.AddTimestampOptions{Validation: timed}) - if e != nil { - t.Fatal(e) - } - - v := preservationValidator(t, c, xades.LevelLT, 0) - rawBefore, _ := bt.XML(t.Context()) - - lt, r, e := v.EmbedValidationMaterial(t.Context(), bt, xmldsig.Signatures(bt)[0], c.Leaf.Raw, testcert.Now(), xades.ValidationEvidence{Signer: proof, TSA: proof}) - if e != nil { - t.Fatal(e) - } - - after, _ := bt.XML(t.Context()) - if !bytes.Equal(rawBefore, after) { - t.Fatal("input changed") - } - - if r.Level() != xades.LevelLT || len(r.EmbeddedMaterial().Certificates) == 0 || len(r.EmbeddedMaterial().CRLs) == 0 { - t.Fatal("missing material") - } - - return c, lt, r, v -} - func TestValidationMaterialOCSPRoundTrip(t *testing.T) { c := testcert.New(t) response := func(certificate *x509.Certificate) []byte { @@ -718,6 +664,8 @@ func TestValidationMaterialOCSPRoundTrip(t *testing.T) { if checked, err := v.ValidateEnveloped(t.Context(), doc, xmldsig.Signatures(doc)[0], c.Leaf.Raw, testcert.Now(), xades.ValidationEvidence{}); checked == nil || err != nil || checked.Level() != xades.LevelLT { t.Fatal("embedded-only OCSP validation or getter ownership failed", err) } + + checkRevocationGroupContainerText(t, doc, c, v, "OCSPValues") } func archivePolicy(t *testing.T, c testcert.Chain) (*timestamp.Verifier, pki.Evidence) { @@ -772,12 +720,16 @@ func TestHistoricalArchiveChronology(t *testing.T) { } result, report, err := validator.Validate(t.Context(), changed, xmldsig.Signatures(changed)[0], chain.Leaf.Raw, at, xades.ValidationEvidence{}) if test.want != "" { + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if result != nil || err == nil || report.Indication() != xades.Indeterminate || report.SubIndication() != test.want { t.Fatalf("indication=%v sub=%s result=%v err=%v", report.Indication(), report.SubIndication(), result != nil, err) } return } - if result == nil || err != nil || report.Indication() != xades.Passed || !report.BestSignatureTime().Equal(testcert.Now().Add(-time.Second)) { + if err != nil { + t.Fatal(err) + } else if result == nil || report.Indication() != xades.Passed || !report.BestSignatureTime().Equal(testcert.Now().Add(-time.Second)) { t.Fatalf("indication=%v sub=%s best=%s err=%v", report.Indication(), report.SubIndication(), report.BestSignatureTime(), err) } stamps := report.Timestamps() @@ -813,7 +765,9 @@ func TestHistoricalArchiveChronology(t *testing.T) { t.Fatal(err) } result, report, err := validator.Validate(t.Context(), changed, xmldsig.Signatures(changed)[0], chain.Leaf.Raw, at, xades.ValidationEvidence{TSA: pki.Evidence{CRLs: [][]byte{historicalLongCRL(t, other)}}}) - if result == nil || err != nil || report.Indication() != xades.Passed || !report.BestSignatureTime().Equal(archiveTime) { + if err != nil { + t.Fatal(err) + } else if result == nil || report.Indication() != xades.Passed || !report.BestSignatureTime().Equal(archiveTime) { t.Fatalf("optional foreign clock poisoned archive: %v/%s best=%s err=%v", report.Indication(), report.SubIndication(), report.BestSignatureTime(), err) } stamps := report.Timestamps() @@ -832,8 +786,8 @@ func exportHistoricalChronology(t testing.TB, chain testcert.Chain, documents ma if err := os.MkdirAll(directory, 0o700); err != nil { t.Fatal(err) } - for name, document := range documents { - raw, err := document.XML(t.Context()) + for name, doc := range documents { + raw, err := doc.XML(t.Context()) if err != nil { t.Fatal(err) } @@ -988,3 +942,124 @@ func FuzzValidatePreservation(f *testing.F) { _, _ = verifier.ValidateEnveloped(t.Context(), d, sigs[0], cert, at, xades.ValidationEvidence{}) }) } + +func verifyOnlyXML(t *testing.T, d *document.Document, c testcert.Chain) { + t.Helper() + + _, e := xmldsig.VerifySignature(t.Context(), d, xmldsig.Signatures(d)[0], c.Leaf.PublicKey, xmldsig.SignatureOptions{IDAttribute: document.Name{Local: "ID"}, AdditionalIDRules: []reference.IDAttributeRule{{Attribute: document.Name{Local: "Id"}, Elements: []document.Name{{Space: xades.Namespace, Local: signedPropertiesLocal}}}}, ExpectedURIs: nil}) + if e != nil { + t.Fatalf("test mutation damaged primary XMLDSig: %v", e) + } +} + +func preservationValidator(t *testing.T, c testcert.Chain, level xades.Level, limit int) *xades.Validator { + t.Helper() + tsa, _ := archivePolicy(t, c) + + v, e := xades.NewValidator(xades.ValidatorConfig{SignerPKI: pki.Config{Roots: [][]byte{c.Root.Raw}, KeyUsages: nil, Revocation: pki.RequireCRL}, TSA: tsa, MinimumLevel: level, MaxArchiveBytes: limit}) + if e != nil { + t.Fatal(e) + } + + return v +} + +func preparedLT(t *testing.T) (testcert.Chain, *document.Document, *xades.ValidationResult, *xades.Validator) { + t.Helper() + c := testcert.New(t) + tsa, proof := archivePolicy(t, c) + doc := basicDocument(t, c) + timed := xades.TimedOptions{TSA: tsa, At: testcert.Now(), Evidence: proof} + tr := &archiveTransport{key: c.TSAKey, cert: c.TSA.Raw, root: c.Root.Raw, now: testcert.Now().Add(-time.Second)} + + bt, _, e := xades.AddSignatureTimestamp(t.Context(), doc, xmldsig.Signatures(doc)[0], c.Leaf.Raw, tr, xades.AddTimestampOptions{Validation: timed}) + if e != nil { + t.Fatal(e) + } + + v := preservationValidator(t, c, xades.LevelLT, 0) + rawBefore, _ := bt.XML(t.Context()) + + lt, r, e := v.EmbedValidationMaterial(t.Context(), bt, xmldsig.Signatures(bt)[0], c.Leaf.Raw, testcert.Now(), xades.ValidationEvidence{Signer: proof, TSA: proof}) + if e != nil { + t.Fatal(e) + } + + after, _ := bt.XML(t.Context()) + if !bytes.Equal(rawBefore, after) { + t.Fatal("input changed") + } + + if r.Level() != xades.LevelLT || len(r.EmbeddedMaterial().Certificates) == 0 || len(r.EmbeddedMaterial().CRLs) == 0 { + t.Fatal("missing material") + } + + return c, lt, r, v +} + +func TestHistoricalEvidenceLimitsBeforeMaterialParsing(t *testing.T) { + chain, doc, _, tsa := historicalArchivePair(t) + zero := time.Duration(0) + at := testcert.Now().Add(48 * time.Hour) + validator, err := xades.NewHistoricalValidator(xades.HistoricalConfig{SignerPKI: pki.HistoricalConfig{PKI: pki.Config{Roots: [][]byte{chain.Root.Raw}, Revocation: pki.RequireCRL, MaxEvidenceBytes: 32 << 10}}, TSA: tsa, MissingTSAAccuracy: &zero, RequireAllTimestamps: true, Baseline: xades.BaselineVerifyOptions{Signature: xmldsig.SignatureOptions{IDAttribute: document.Name{Local: "ID"}}}}) + if err != nil { + t.Fatal(err) + } + many := make([][]byte, 65) + for i := range many { + many[i] = chain.Root.Raw + } + padding, err := asn1.Marshal(make([]byte, 40<<10)) + if err != nil { + t.Fatal(err) + } + template := &x509.Certificate{SerialNumber: big.NewInt(30000), Subject: pkix.Name{CommonName: "bounded oversized historical evidence"}, NotBefore: chain.Leaf.NotBefore, NotAfter: chain.Leaf.NotAfter, KeyUsage: x509.KeyUsageDigitalSignature, ExtraExtensions: []pkix.Extension{{Id: asn1.ObjectIdentifier{1, 2, 3, 4, 5, 6, 9}, Value: padding}}} + large := testcert.Issue(t, template, chain.Root, chain.LeafKey.Public(), chain.RootKey).Raw + if len(large) <= 32<<10 || len(large) > 128<<10 { + t.Fatal("invalid bounded byte fixture", len(large)) + } + before := mustXML(t, doc) + for _, tc := range []struct { + name string + input xades.ValidationEvidence + limit, parse bool + }{ + {name: "valid archive"}, + {name: "signer count", input: xades.ValidationEvidence{Signer: pki.Evidence{Intermediates: many}}, limit: true}, + {name: "TSA count", input: xades.ValidationEvidence{TSA: pki.Evidence{Intermediates: many}}, limit: true}, + {name: "configured signer bytes", input: xades.ValidationEvidence{Signer: pki.Evidence{Intermediates: [][]byte{large}}}, limit: true}, + {name: "count before malformed material", input: xades.ValidationEvidence{Signer: pki.Evidence{Intermediates: many, OCSPResponses: [][]byte{{0x30, 0}}}}, limit: true}, + {name: "under limit material still parsed", input: xades.ValidationEvidence{Signer: pki.Evidence{OCSPResponses: [][]byte{{0x30, 0}}}}, parse: true}, + } { + t.Run(tc.name, func(t *testing.T) { + rawBefore := bytes.Clone(chain.Root.Raw) + largeBefore := bytes.Clone(large) + result, report, err := validator.Validate(t.Context(), doc, xmldsig.Signatures(doc)[0], chain.Leaf.Raw, at, tc.input) + if report == nil { + t.Fatal("missing report") + } + switch { + case tc.limit: + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil + if result != nil || !errors.Is(err, xades.ErrHistorical) || !errors.Is(err, pki.ErrLimit) || !errors.Is(report.Cause(), pki.ErrLimit) { + t.Fatal("limit envelope", result, report, err) + } + if report.Indication() != xades.Indeterminate || !report.VerificationTime().Equal(at) || !report.BestSignatureTime().Equal(at) || !report.ValidationTime().IsZero() || report.SubIndication() != "" || len(report.Proofs()) != 0 || len(report.Timestamps()) != 0 || len(report.Steps()) != 0 { + t.Fatal("early limit published acceptance or lost time", report) + } + case tc.parse: + if result != nil || !errors.Is(err, pki.ErrEvidence) || errors.Is(err, pki.ErrLimit) { + t.Fatal("material parser bypassed", result, err) + } + default: + if err != nil || result == nil || report.Indication() != xades.Passed { + t.Fatal("positive archive", result, report, err) + } + } + if !bytes.Equal(before, mustXML(t, doc)) || !bytes.Equal(rawBefore, chain.Root.Raw) || !bytes.Equal(largeBefore, large) { + t.Fatal("caller input changed") + } + }) + } +} diff --git a/xades/baseline.go b/xades/baseline.go index 25dce0f..89e090a 100644 --- a/xades/baseline.go +++ b/xades/baseline.go @@ -20,6 +20,26 @@ import ( "github.com/skarm/xmlsec/xmldsig" ) +const ( + // Fallback SignedProperties ID used when the caller omits an explicit identifier. + defaultSignedPropertiesID = "signed-properties" + nameQualifyingPropertiesReference = "QualifyingPropertiesReference" + dataReferenceIDSuffix = "-data-reference-" + maximumAdditionalCertificates = 63 + // Caps each certificate encoding before XAdES binding or evidence checks, in bytes. + maximumCertificateBytes = 1 << 20 + maximumCertificateValues = 64 + maximumCommitments = 64 + maximumCoverageRequirements = 64 + maximumDataObjectFormats = 256 + // Bounds a supplied signature-policy document before transforms or digest verification, in bytes. + maximumPolicyDocumentBytes = 8 << 20 +) + +const ( + octetStreamMediaType = "application/octet-stream" +) + const countersignedSignatureType = "http://uri.etsi.org/01903#CountersignedSignature" type baselineLayout struct { @@ -64,7 +84,7 @@ func VerifyBaseline(ctx context.Context, doc *document.Document, selected docume } opt := baselineCanonicalizationOptions(options.Signature) - opt.AdditionalIDRules = append(opt.AdditionalIDRules, reference.IDAttributeRule{Attribute: document.Name{Local: "Id"}, Elements: []document.Name{name("SignedProperties")}}) + opt.AdditionalIDRules = append(opt.AdditionalIDRules, reference.IDAttributeRule{Attribute: document.Name{Local: lexicon.SecurityAttrID}, Elements: []document.Name{name(nameSignedProperties)}}) opt.RequiredElements = append(opt.RequiredElements, layout.signed) verified, err := xmldsig.VerifySignature(ctx, doc, selected, certificate.PublicKey, opt) @@ -142,7 +162,7 @@ func SignBaseline(ctx context.Context, doc *document.Document, parent document.E return nil, nil, err } - validation.AdditionalIDRules = append(validation.AdditionalIDRules, reference.IDAttributeRule{Attribute: document.Name{Local: "Id"}, Elements: []document.Name{name("SignedProperties")}}) + validation.AdditionalIDRules = append(validation.AdditionalIDRules, reference.IDAttributeRule{Attribute: document.Name{Local: lexicon.SecurityAttrID}, Elements: []document.Name{name(nameSignedProperties)}}) validation.RequiredElements = append(validation.RequiredElements, layout.signed) preserve, err := remapBaselinePreservation(doc, draft, options.Preserve) @@ -205,12 +225,12 @@ func baselineSignerCertificate(ctx context.Context, signer crypto.Signer, certif func prepareBaselineTemplate(ctx context.Context, certificate *x509.Certificate, options BaselineSignOptions) (xmldsig.SignatureTemplate, BaselineVerifyOptions, error) { template := options.Template - if len(options.Properties.AdditionalCertificates) > 63 || len(options.Properties.DataObjects) > 256 || len(options.Properties.Commitments) > 64 { + if len(options.Properties.AdditionalCertificates) > maximumAdditionalCertificates || len(options.Properties.DataObjects) > maximumDataObjectFormats || len(options.Properties.Commitments) > maximumCommitments { return template, options.Validation, ErrProfile } for _, value := range options.Properties.AdditionalCertificates { - if len(value.DER) == 0 || len(value.DER) > 1<<20 { + if len(value.DER) == 0 || len(value.DER) > maximumCertificateBytes { return template, options.Validation, ErrCertificate } @@ -222,7 +242,7 @@ func prepareBaselineTemplate(ctx context.Context, certificate *x509.Certificate, } if options.SignedPropertiesID == "" { - options.SignedPropertiesID = "signed-properties" + options.SignedPropertiesID = defaultSignedPropertiesID } if !tree.NCName(template.ID) || !tree.NCName(options.SignedPropertiesID) { @@ -246,7 +266,7 @@ func prepareBaselineTemplate(ctx context.Context, certificate *x509.Certificate, continue } - options.Properties.DataObjects = append(options.Properties.DataObjects, DataObjectFormat{ObjectReference: "#" + ref.ID, MIMEType: "application/octet-stream"}) + options.Properties.DataObjects = append(options.Properties.DataObjects, DataObjectFormat{ObjectReference: "#" + ref.ID, MIMEType: octetStreamMediaType}) } } @@ -269,7 +289,7 @@ func prepareBaselineTemplate(ctx context.Context, certificate *x509.Certificate, } func remapBaselinePreservation(from, to *document.Document, requirements []xmldsig.SignatureRequirement) ([]xmldsig.SignatureRequirement, error) { - if len(requirements) > 64 { + if len(requirements) > maximumCoverageRequirements { return nil, ErrProfile } @@ -296,7 +316,7 @@ func remapBaselinePreservation(from, to *document.Document, requirements []xmlds func baselineLayoutFor(selected document.Element) (baselineLayout, error) { var out baselineLayout - id := localAttribute(selected, "Id") + id := localAttribute(selected, lexicon.SecurityAttrID) if !tree.NCName(id) { return out, ErrProfile } @@ -308,7 +328,7 @@ func baselineLayoutFor(selected document.Element) (baselineLayout, error) { return out, err } - if !out.qualifying.Valid() || baselineAttrs(out.qualifying, []string{"Target"}, "Id") != nil || localAttribute(out.qualifying, "Target") != "#"+id { + if !out.qualifying.Valid() || baselineAttrs(out.qualifying, []string{lexicon.SecurityAttrTarget}, lexicon.SecurityAttrID) != nil || localAttribute(out.qualifying, lexicon.SecurityAttrTarget) != "#"+id { return out, ErrProfile } @@ -317,17 +337,17 @@ func baselineLayoutFor(selected document.Element) (baselineLayout, error) { return out, err } - if len(children) < 1 || len(children) > 2 || children[0].Name() != name("SignedProperties") { + if len(children) < 1 || len(children) > 2 || children[0].Name() != name(nameSignedProperties) { return out, ErrProfile } out.signed = children[0] - if baselineAttrs(out.signed, []string{"Id"}) != nil || !tree.NCName(localAttribute(out.signed, "Id")) { + if baselineAttrs(out.signed, []string{lexicon.SecurityAttrID}) != nil || !tree.NCName(localAttribute(out.signed, lexicon.SecurityAttrID)) { return out, ErrProfile } if len(children) == 2 { - if children[1].Name() != name("UnsignedProperties") || baselineAttrs(children[1], nil, "Id") != nil || len(children[1].Children()) == 0 { + if children[1].Name() != name(nameUnsignedProperties) || baselineAttrs(children[1], nil, lexicon.SecurityAttrID) != nil || len(children[1].Children()) == 0 { return out, ErrProfile } @@ -343,17 +363,17 @@ func baselineLayoutFor(selected document.Element) (baselineLayout, error) { return out, err } - if len(properties) < 1 || len(properties) > 2 || properties[0].Name() != name("SignedSignatureProperties") { + if len(properties) < 1 || len(properties) > 2 || properties[0].Name() != name(nameSignedSignatureProperties) { return out, ErrProfile } out.signature = properties[0] - if baselineAttrs(out.signature, nil, "Id") != nil || len(out.signature.Children()) == 0 { + if baselineAttrs(out.signature, nil, lexicon.SecurityAttrID) != nil || len(out.signature.Children()) == 0 { return out, ErrProfile } if len(properties) == 2 { - if properties[1].Name() != name("SignedDataObjectProperties") || baselineAttrs(properties[1], nil, "Id") != nil || len(properties[1].Children()) == 0 { + if properties[1].Name() != name(nameSignedDataObjectProperties) || baselineAttrs(properties[1], nil, lexicon.SecurityAttrID) != nil || len(properties[1].Children()) == 0 { return out, ErrProfile } @@ -371,7 +391,7 @@ func baselineAttrs(element document.Element, required []string, optional ...stri return ErrProfile } - if attr.Name.Local == "Id" && !tree.NCName(attr.Value) { + if attr.Name.Local == lexicon.SecurityAttrID && !tree.NCName(attr.Value) { return ErrProfile } @@ -433,14 +453,14 @@ func cloneDERValues(values [][]byte) [][]byte { } func baselineReferenceID(signatureID string, index int) string { - return signatureID + "-data-reference-" + strconv.Itoa(index+1) + return signatureID + dataReferenceIDSuffix + strconv.Itoa(index+1) } func signatureByID(doc *document.Document, id string) (document.Element, error) { var found document.Element for _, selected := range xmldsig.Signatures(doc) { - if localAttribute(selected, "Id") == id { + if localAttribute(selected, lexicon.SecurityAttrID) == id { if found.Valid() { return found, ErrProfile } @@ -522,8 +542,8 @@ func checkBaselineUnsigned(element document.Element) error { previous := -1 for _, child := range values { - index := slices.Index([]document.Name{name("UnsignedSignatureProperties"), name("UnsignedDataObjectProperties")}, child.Name()) - if index < 0 || index <= previous || baselineAttrs(child, nil, "Id") != nil { + index := slices.Index([]document.Name{name(nameUnsignedSignatureProperties), name(nameUnsignedDataObjectProperties)}, child.Name()) + if index < 0 || index <= previous || baselineAttrs(child, nil, lexicon.SecurityAttrID) != nil { return ErrProfile } @@ -535,7 +555,7 @@ func checkBaselineUnsigned(element document.Element) error { } for _, entry := range entries { - if entry.Name() == name("CounterSignature") { + if entry.Name() == name(nameCounterSignature) { if err := checkCounterContainer(entry); err != nil { return err } @@ -575,16 +595,16 @@ func findBaselineQualifying(selected document.Element) (document.Element, error) var qualifying document.Element for _, object := range selected.Children() { - if object.Name() != ds("Object") { + if object.Name() != ds(lexicon.DSigElementObject) { continue } for _, child := range object.Children() { - if child.Name() == name("QualifyingPropertiesReference") { + if child.Name() == name(nameQualifyingPropertiesReference) { return document.Element{}, ErrProfile } - if child.Name() != name("QualifyingProperties") { + if child.Name() != name(nameQualifyingProperties) { continue } @@ -600,12 +620,12 @@ func findBaselineQualifying(selected document.Element) (document.Element, error) } func normalizeBaselineVerifyOptions(options BaselineVerifyOptions) (BaselineVerifyOptions, error) { - if len(options.CertificateValues) > 64 || len(options.PolicyDocument) > 8<<20 { + if len(options.CertificateValues) > maximumCertificateValues || len(options.PolicyDocument) > maximumPolicyDocumentBytes { return options, ErrProfile } for _, value := range options.CertificateValues { - if len(value) == 0 || len(value) > 1<<20 { + if len(value) == 0 || len(value) > maximumCertificateBytes { return options, ErrCertificate } } diff --git a/xades/baseline_counter.go b/xades/baseline_counter.go index 972f9d4..fa8d4b5 100644 --- a/xades/baseline_counter.go +++ b/xades/baseline_counter.go @@ -3,9 +3,8 @@ package xades import ( "context" - "github.com/skarm/xmlsec/internal/lexicon" - "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/tree" "github.com/skarm/xmlsec/xmldsig" ) @@ -25,7 +24,7 @@ func (r *BaselineResult) CounterSignatures() []document.Element { var out []document.Element for _, property := range r.UnsignedSignatureProperties() { - if property.Name() != name("CounterSignature") { + if property.Name() != name(nameCounterSignature) { continue } @@ -52,7 +51,7 @@ func VerifyCounterSignature(ctx context.Context, enclosing *BasicResult, counter owner, node := tree.Unwrap(child) parentOwner, _ := tree.Unwrap(parent) - if owner != parentOwner || node == nil || node.Parent == nil || node.Parent.Name != name("CounterSignature") { + if owner != parentOwner || node == nil || node.Parent == nil || node.Parent.Name != name(nameCounterSignature) { return ErrProfile } @@ -62,22 +61,22 @@ func VerifyCounterSignature(ctx context.Context, enclosing *BasicResult, counter } usp := node.Parent.Parent - if usp == nil || usp.Name != name("UnsignedSignatureProperties") { + if usp == nil || usp.Name != name(nameUnsignedSignatureProperties) { return ErrProfile } unsigned := usp.Parent - if unsigned == nil || unsigned.Name != name("UnsignedProperties") { + if unsigned == nil || unsigned.Name != name(nameUnsignedProperties) { return ErrProfile } qp := unsigned.Parent - if qp == nil || qp.Name != name("QualifyingProperties") { + if qp == nil || qp.Name != name(nameQualifyingProperties) { return ErrProfile } object := qp.Parent - if object == nil || object.Name != ds("Object") || object.Parent == nil || !tree.Wrap(owner, object.Parent).Same(parent) { + if object == nil || object.Name != ds(lexicon.DSigElementObject) || object.Parent == nil || !tree.Wrap(owner, object.Parent).Same(parent) { return ErrProfile } @@ -100,7 +99,7 @@ func VerifyCounterSignature(ctx context.Context, enclosing *BasicResult, counter } func checkCounterContainer(element document.Element) error { - if baselineAttrs(element, nil, "Id") != nil { + if baselineAttrs(element, nil, lexicon.SecurityAttrID) != nil { return ErrProfile } diff --git a/xades/baseline_data.go b/xades/baseline_data.go index dd2d9c6..9fa3ed7 100644 --- a/xades/baseline_data.go +++ b/xades/baseline_data.go @@ -12,27 +12,46 @@ import ( "github.com/skarm/xmlsec/xmldsig" ) +const ( + attrQualifier = "Qualifier" + nameAllSignedDataObjects = "AllSignedDataObjects" + nameCommitmentTypeID = "CommitmentTypeId" + nameCommitmentTypeQualifier = "CommitmentTypeQualifier" + nameCommitmentTypeQualifiers = "CommitmentTypeQualifiers" + nameDescription = "Description" + nameDocumentationReference = "DocumentationReference" + nameDocumentationReferences = "DocumentationReferences" + nameIdentifier = "Identifier" + nameObjectIdentifier = "ObjectIdentifier" + oidURNPrefix = "urn:oid:" + schemeURN = "urn" + maximumDocumentationReferences = 64 +) + func parseObjectIdentifier(element document.Element) (ObjectIdentifier, error) { + // Identifier is required; Description and DocumentationReferences are optional. + const maximumObjectIdentifierChildren = 3 + var out ObjectIdentifier if attrs(element) != nil { return out, ErrProfile } values, err := baselineContent(element) - if err != nil || len(values) == 0 || len(values) > 3 || values[0].Name() != name("Identifier") { + if err != nil || len(values) == 0 || len(values) > maximumObjectIdentifierChildren || values[0].Name() != name(nameIdentifier) { return out, ErrProfile } identifier := values[0] - if baselineAttrs(identifier, nil, "Qualifier") != nil || len(identifier.Children()) != 0 { + if baselineAttrs(identifier, nil, attrQualifier) != nil || len(identifier.Children()) != 0 { return out, ErrProfile } out.Identifier = identifier.Text() - out.Qualifier = localAttribute(identifier, "Qualifier") + out.Qualifier = localAttribute(identifier, attrQualifier) at := 1 - if at < len(values) && values[at].Name() == name("Description") { + if at < len(values) && values[at].Name() == name(nameDescription) { out.Description, err = simple(values[at]) if err != nil { return out, err @@ -41,7 +60,7 @@ func parseObjectIdentifier(element document.Element) (ObjectIdentifier, error) { at++ } - if at < len(values) && values[at].Name() == name("DocumentationReferences") { + if at < len(values) && values[at].Name() == name(nameDocumentationReferences) { docs := values[at] if attrs(docs) != nil { return out, ErrProfile @@ -53,7 +72,7 @@ func parseObjectIdentifier(element document.Element) (ObjectIdentifier, error) { } for _, entry := range entries { - if entry.Name() != name("DocumentationReference") { + if entry.Name() != name(nameDocumentationReference) { return out, ErrProfile } @@ -76,22 +95,22 @@ func parseObjectIdentifier(element document.Element) (ObjectIdentifier, error) { } func checkObjectIdentifier(value ObjectIdentifier) error { - if len(value.Documentation) > 64 || !baselineURI(value.Identifier) { + if len(value.Documentation) > maximumDocumentationReferences || !baselineURI(value.Identifier) { return ErrProfile } switch value.Qualifier { case "": - if strings.HasPrefix(strings.ToLower(value.Identifier), "urn:oid:") { + if strings.HasPrefix(strings.ToLower(value.Identifier), oidURNPrefix) { return ErrProfile } case qualifierOIDAsURN: - if !strings.HasPrefix(value.Identifier, "urn:oid:") || !baselineOID(value.Identifier[len("urn:oid:"):]) { + if !strings.HasPrefix(value.Identifier, oidURNPrefix) || !baselineOID(value.Identifier[len(oidURNPrefix):]) { return ErrProfile } case qualifierOIDAsURI: uri, err := url.Parse(value.Identifier) - if err != nil || !uri.IsAbs() || strings.EqualFold(uri.Scheme, "urn") { + if err != nil || !uri.IsAbs() || strings.EqualFold(uri.Scheme, schemeURN) { return ErrProfile } default: @@ -143,19 +162,19 @@ func buildObjectIdentifier(local string, value ObjectIdentifier) (*tree.Node, er out := xnode(local) - identifier := leaf(out, "Identifier", value.Identifier) + identifier := leaf(out, nameIdentifier, value.Identifier) if value.Qualifier != "" { - attr(identifier, "Qualifier", value.Qualifier) + attr(identifier, attrQualifier, value.Qualifier) } if value.Description != "" { - leaf(out, "Description", value.Description) + leaf(out, nameDescription, value.Description) } if len(value.Documentation) > 0 { - docs := xnode("DocumentationReferences") + docs := xnode(nameDocumentationReferences) for _, value := range value.Documentation { - leaf(docs, "DocumentationReference", value) + leaf(docs, nameDocumentationReference, value) } add(out, docs) @@ -165,8 +184,8 @@ func buildObjectIdentifier(local string, value ObjectIdentifier) (*tree.Node, er } func parseDataObjectFormat(element document.Element) (DataObjectFormat, error) { - out := DataObjectFormat{ObjectReference: localAttribute(element, "ObjectReference")} - if baselineAttrs(element, []string{"ObjectReference"}) != nil { + out := DataObjectFormat{ObjectReference: localAttribute(element, attrObjectReference)} + if baselineAttrs(element, []string{attrObjectReference}) != nil { return out, ErrProfile } @@ -178,7 +197,7 @@ func parseDataObjectFormat(element document.Element) (DataObjectFormat, error) { previous := -1 for _, child := range values { - index := slices.Index([]document.Name{name("Description"), name("ObjectIdentifier"), name("MimeType"), name(attributeEncoding)}, child.Name()) + index := slices.Index([]document.Name{name(nameDescription), name(nameObjectIdentifier), name(lexicon.SecurityAttrMimeType), name(attributeEncoding)}, child.Name()) if index < 0 || index <= previous { return out, ErrProfile } @@ -203,7 +222,7 @@ func parseDataObjectFormat(element document.Element) (DataObjectFormat, error) { } } - hasDescription := slices.ContainsFunc(values, func(child document.Element) bool { return child.Name() == name("Description") }) + hasDescription := slices.ContainsFunc(values, func(child document.Element) bool { return child.Name() == name(nameDescription) }) if !hasDescription && out.ObjectIdentifier == nil && out.MIMEType == "" { return out, ErrProfile } @@ -240,15 +259,15 @@ func buildDataObjectFormat(value DataObjectFormat) (*tree.Node, error) { return nil, err } - node := xnode("DataObjectFormat") - attr(node, "ObjectReference", value.ObjectReference) + node := xnode(nameDataObjectFormat) + attr(node, attrObjectReference, value.ObjectReference) if value.Description != "" { - leaf(node, "Description", value.Description) + leaf(node, nameDescription, value.Description) } if value.ObjectIdentifier != nil { - identifier, err := buildObjectIdentifier("ObjectIdentifier", *value.ObjectIdentifier) + identifier, err := buildObjectIdentifier(nameObjectIdentifier, *value.ObjectIdentifier) if err != nil { return nil, err } @@ -257,7 +276,7 @@ func buildDataObjectFormat(value DataObjectFormat) (*tree.Node, error) { } if value.MIMEType != "" { - leaf(node, "MimeType", value.MIMEType) + leaf(node, lexicon.SecurityAttrMimeType, value.MIMEType) } if value.Encoding != "" { @@ -274,7 +293,7 @@ func parseCommitment(element document.Element) (CommitmentType, error) { } values, err := baselineContent(element) - if err != nil || len(values) < 2 || values[0].Name() != name("CommitmentTypeId") { + if err != nil || len(values) < 2 || values[0].Name() != name(nameCommitmentTypeID) { return out, ErrProfile } @@ -284,7 +303,7 @@ func parseCommitment(element document.Element) (CommitmentType, error) { } at := 1 - if values[at].Name() == name("AllSignedDataObjects") { + if values[at].Name() == name(nameAllSignedDataObjects) { if _, err := children(values[at]); err != nil || attrs(values[at]) != nil { return out, ErrProfile } @@ -292,7 +311,7 @@ func parseCommitment(element document.Element) (CommitmentType, error) { out.All = true at++ } else { - for at < len(values) && values[at].Name() == name("ObjectReference") { + for at < len(values) && values[at].Name() == name(attrObjectReference) { value, err := simple(values[at]) if err != nil || value == "" || !baselineURI(value) { return out, ErrProfile @@ -307,7 +326,7 @@ func parseCommitment(element document.Element) (CommitmentType, error) { } } - if at < len(values) && values[at].Name() == name("CommitmentTypeQualifiers") { + if at < len(values) && values[at].Name() == name(nameCommitmentTypeQualifiers) { out.Qualifiers, err = parseCommitmentQualifiers(values[at]) if err != nil { return out, err @@ -328,9 +347,9 @@ func buildCommitment(ctx context.Context, value CommitmentType) (*tree.Node, err return nil, ErrProfile } - node := xnode("CommitmentTypeIndication") + node := xnode(nameCommitmentTypeIndication) - identifier, err := buildObjectIdentifier("CommitmentTypeId", value.Identifier) + identifier, err := buildObjectIdentifier(nameCommitmentTypeID, value.Identifier) if err != nil { return nil, err } @@ -338,22 +357,22 @@ func buildCommitment(ctx context.Context, value CommitmentType) (*tree.Node, err add(node, identifier) if value.All { - add(node, xnode("AllSignedDataObjects")) + add(node, xnode(nameAllSignedDataObjects)) } else { for _, uri := range value.ObjectReferences { if uri == "" || !baselineURI(uri) { return nil, ErrProfile } - leaf(node, "ObjectReference", uri) + leaf(node, attrObjectReference, uri) } } if len(value.Qualifiers) > 0 { - qualifiers := xnode("CommitmentTypeQualifiers") + qualifiers := xnode(nameCommitmentTypeQualifiers) for _, qualifier := range value.Qualifiers { - n, err := baselinePropertyValue(ctx, qualifier, name("CommitmentTypeQualifier")) + n, err := baselinePropertyValue(ctx, qualifier, name(nameCommitmentTypeQualifier)) if err != nil { return nil, err } @@ -395,7 +414,7 @@ func baselineDataReferences(verified *xmldsig.SignatureResult) (map[string]basel result := results[index] index++ - id := localAttribute(element, "Id") + id := localAttribute(element, lexicon.SecurityAttrID) if id == "" { continue } @@ -469,12 +488,12 @@ func checkBaselineDataBindings(layout baselineLayout, verified *xmldsig.Signatur continue } - typ := localAttribute(element, "Type") + typ := localAttribute(element, lexicon.SecurityAttrType) if typ == SignedPropertiesType || typ == countersignedSignatureType { continue } - if _, exists := covered["#"+localAttribute(element, "Id")]; !exists { + if _, exists := covered["#"+localAttribute(element, lexicon.SecurityAttrID)]; !exists { return ErrProfile } } @@ -553,7 +572,7 @@ func parseCommitmentQualifiers(element document.Element) ([]PropertyValue, error out := make([]PropertyValue, 0, len(values)) for _, value := range values { - if value.Name() != name("CommitmentTypeQualifier") { + if value.Name() != name(nameCommitmentTypeQualifier) { return nil, ErrProfile } @@ -564,11 +583,11 @@ func parseCommitmentQualifiers(element document.Element) ([]PropertyValue, error } func checkBaselineObjectFormat(target document.Element, format DataObjectFormat) error { - if target.Name() != ds("Object") { + if target.Name() != ds(lexicon.DSigElementObject) { return nil } - for _, pair := range [][2]string{{"MimeType", format.MIMEType}, {attributeEncoding, format.Encoding}} { + for _, pair := range [][2]string{{lexicon.SecurityAttrMimeType, format.MIMEType}, {attributeEncoding, format.Encoding}} { value, exists := target.Attribute(document.Name{Local: pair[0]}) if exists && pair[1] != "" && value != pair[1] { return ErrProfile @@ -617,7 +636,7 @@ func baselineReferenceKey(value string, element document.Element, options Baseli for _, n := range slices.Backward(path) { for _, attribute := range n.Attrs { - if attribute.Name == (document.Name{Space: document.XMLNamespace, Local: "base"}) { + if attribute.Name == (document.Name{Space: document.XMLNamespace, Local: lexicon.AttrBase}) { part, err := url.Parse(attribute.Value) if err != nil { return "", ErrProfile diff --git a/xades/baseline_data_time.go b/xades/baseline_data_time.go index 767598f..c0a885f 100644 --- a/xades/baseline_data_time.go +++ b/xades/baseline_data_time.go @@ -20,6 +20,20 @@ import ( "github.com/skarm/xmlsec/xmldsig" ) +const ( + attrReferencedData = "referencedData" + nameInclude = "Include" + defaultDataTimestampInputBytes = 16 << 20 + defaultDataTimestamps = 16 + maximumDataTimestampInputBytes = 64 << 20 + maximumDataTimestamps = 64 + maximumTimestampObjectReferences = 256 +) + +const ( + booleanTrueNumeric = "1" +) + // DataTimestampProperty supplies a pre-existing RFC3161 content timestamp. // When Individual is true, ObjectReferences selects ordered Reference URIs. // Otherwise, the timestamp covers direct SignedInfo data References in their @@ -153,7 +167,7 @@ func (value *DataTimestamp) bindReferences(verified *xmldsig.SignatureResult, in } value.references = append(value.references, ref) - value.identifiers = append(value.identifiers, "#"+localAttribute(element, "Id")) + value.identifiers = append(value.identifiers, "#"+localAttribute(element, lexicon.SecurityAttrID)) } } @@ -176,18 +190,18 @@ func (r *BaselineResult) DataTimestamps() []DataTimestamp { func normalizeDataTimestampLimits(options DataTimestampLimits) (DataTimestampLimits, error) { if options.MaxTimestamps == 0 { - options.MaxTimestamps = 16 + options.MaxTimestamps = defaultDataTimestamps } if options.MaxTokenBytes == 0 { - options.MaxTokenBytes = 4 << 20 + options.MaxTokenBytes = defaultTimestampTokenBytes } if options.MaxInputBytes == 0 { - options.MaxInputBytes = 16 << 20 + options.MaxInputBytes = defaultDataTimestampInputBytes } - if options.MaxTimestamps < 1 || options.MaxTimestamps > 64 || options.MaxTokenBytes < 1 || options.MaxTokenBytes > 16<<20 || options.MaxInputBytes < 1 || options.MaxInputBytes > 64<<20 { + if options.MaxTimestamps < 1 || options.MaxTimestamps > maximumDataTimestamps || options.MaxTokenBytes < 1 || options.MaxTokenBytes > maximumTimestampTokenBytes || options.MaxInputBytes < 1 || options.MaxInputBytes > maximumDataTimestampInputBytes { return options, ErrProfile } @@ -196,7 +210,7 @@ func normalizeDataTimestampLimits(options DataTimestampLimits) (DataTimestampLim func parseBaselineDataTimestamp(ctx context.Context, element document.Element, verified *xmldsig.SignatureResult, limits DataTimestampLimits, options BaselineVerifyOptions) (DataTimestamp, error) { out := DataTimestamp{element: element, method: reference.Transform{Algorithm: c14n.Canonical10}, maximum: limits.MaxInputBytes} - if baselineAttrs(element, nil, "Id") != nil { + if baselineAttrs(element, nil, lexicon.SecurityAttrID) != nil { return out, ErrProfile } @@ -206,9 +220,9 @@ func parseBaselineDataTimestamp(ctx context.Context, element document.Element, v } at := 0 - for at < len(values) && values[at].Name() == name("Include") { + for at < len(values) && values[at].Name() == name(nameInclude) { include := values[at] - if baselineAttrs(include, []string{"URI", "referencedData"}) != nil { + if baselineAttrs(include, []string{lexicon.SecurityAttrURI, attrReferencedData}) != nil { return out, ErrProfile } @@ -216,20 +230,20 @@ func parseBaselineDataTimestamp(ctx context.Context, element document.Element, v return out, err } - truth := strings.Trim(localAttribute(include, "referencedData"), " \t\r\n") - if truth != "true" && truth != "1" { + truth := strings.Trim(localAttribute(include, attrReferencedData), lexicon.XMLWhitespace) + if truth != lexicon.ValueTrue && truth != booleanTrueNumeric { return out, ErrProfile } - if len(out.identifiers) >= 256 { + if len(out.identifiers) >= maximumTimestampObjectReferences { return out, ErrProfile } - out.identifiers = append(out.identifiers, localAttribute(include, "URI")) + out.identifiers = append(out.identifiers, localAttribute(include, lexicon.SecurityAttrURI)) at++ } - individual := element.Name() == name("IndividualDataObjectsTimeStamp") + individual := element.Name() == name(nameIndividualDataObjectsTimeStamp) if individual != (len(out.identifiers) > 0) { return out, ErrProfile } @@ -250,7 +264,7 @@ func parseBaselineDataTimestamp(ctx context.Context, element document.Element, v } token := values[at] - if token.Name() != name("EncapsulatedTimeStamp") || baselineAttrs(token, nil, "Id", attributeEncoding) != nil { + if token.Name() != name(nameEncapsulatedTimeStamp) || baselineAttrs(token, nil, lexicon.SecurityAttrID, attributeEncoding) != nil { return out, ErrProfile } @@ -277,12 +291,12 @@ func parseBaselineDataTimestamp(ctx context.Context, element document.Element, v } func buildBaselineDataTimestamp(ctx context.Context, value DataTimestampProperty, limits DataTimestampLimits) (*tree.Node, error) { - local := "AllDataObjectsTimeStamp" + local := nameAllDataObjectsTimeStamp if value.Individual { - local = "IndividualDataObjectsTimeStamp" + local = nameIndividualDataObjectsTimeStamp } - if len(value.ObjectReferences) > 256 || value.Individual != (len(value.ObjectReferences) > 0) || len(value.TokenDER) == 0 || len(value.TokenDER) > limits.MaxTokenBytes { + if len(value.ObjectReferences) > maximumTimestampObjectReferences || value.Individual != (len(value.ObjectReferences) > 0) || len(value.TokenDER) == 0 || len(value.TokenDER) > limits.MaxTokenBytes { return nil, ErrProfile } @@ -297,16 +311,16 @@ func buildBaselineDataTimestamp(ctx context.Context, value DataTimestampProperty return nil, ErrProfile } - include := xnode("Include") - attr(include, "URI", identifier) - attr(include, "referencedData", "true") + include := xnode(nameInclude) + attr(include, lexicon.SecurityAttrURI, identifier) + attr(include, attrReferencedData, lexicon.ValueTrue) add(node, include) } method := value.Canonicalization if method.Algorithm != "" { canonical := dsnode(lexicon.DSigElementCanonicalizationMethod) - attr(canonical, "Algorithm", method.Algorithm) + attr(canonical, lexicon.SecurityAttrAlgorithm, method.Algorithm) if err := transformxml.AppendParameters(ctx, canonical, method); err != nil { return nil, err @@ -321,7 +335,7 @@ func buildBaselineDataTimestamp(ctx context.Context, value DataTimestampProperty return nil, ErrProfile } - leaf(node, "EncapsulatedTimeStamp", base64.StdEncoding.EncodeToString(value.TokenDER)) + leaf(node, nameEncapsulatedTimeStamp, base64.StdEncoding.EncodeToString(value.TokenDER)) return node, nil } diff --git a/xades/baseline_policy.go b/xades/baseline_policy.go index 9dd252b..d954f6f 100644 --- a/xades/baseline_policy.go +++ b/xades/baseline_policy.go @@ -19,11 +19,39 @@ import ( "github.com/skarm/xmlsec/xmldsig" ) +const ( + nameExplicitText = "ExplicitText" + nameNoticeNumber = "int" + nameNoticeNumbers = "NoticeNumbers" + nameNoticeRef = "NoticeRef" + nameOrganization = "Organization" + nameSPDocSpecification = "SPDocSpecification" + nameSPURI = "SPURI" + nameSPUserNotice = "SPUserNotice" + nameSigPolicyHash = "SigPolicyHash" + nameSigPolicyID = "SigPolicyId" + nameSigPolicyQualifier = "SigPolicyQualifier" + nameSigPolicyQualifiers = "SigPolicyQualifiers" + nameSignaturePolicyID = "SignaturePolicyId" + nameSignaturePolicyImplied = "SignaturePolicyImplied" + // Base used for exact decimal integer parsing of signed property values. + decimalRadix = 10 + maximumNoticeNumberBytes = 8192 + maximumPolicyTransforms = 16 + maximumSignaturePolicies = 16 +) + // PolicyDocumentDigestAsSpecified is confined to SignaturePolicyId; it is never // registered as a general XMLDSig transform. const PolicyDocumentDigestAsSpecified = "http://uri.etsi.org/01903/v1.3.2/SignaturePolicy/SPDocDigestAsInSpecification" func parseBaselinePolicy(ctx context.Context, element document.Element, options BaselineVerifyOptions) (*SignaturePolicy, error) { + // SigPolicyId and SigPolicyHash are required; Transforms and qualifiers are optional. + const ( + minimumSignaturePolicyChildren = 2 + maximumSignaturePolicyChildren = 4 + ) + if attrs(element) != nil { return nil, ErrProfile } @@ -33,7 +61,7 @@ func parseBaselinePolicy(ctx context.Context, element document.Element, options return nil, ErrProfile } - if values[0].Name() == name("SignaturePolicyImplied") { + if values[0].Name() == name(nameSignaturePolicyImplied) { if _, err := children(values[0]); err != nil || attrs(values[0]) != nil { return nil, ErrProfile } @@ -41,12 +69,12 @@ func parseBaselinePolicy(ctx context.Context, element document.Element, options return &SignaturePolicy{Implied: true}, nil } - if values[0].Name() != name("SignaturePolicyId") || attrs(values[0]) != nil { + if values[0].Name() != name(nameSignaturePolicyID) || attrs(values[0]) != nil { return nil, ErrProfile } values, err = baselineContent(values[0]) - if err != nil || len(values) < 2 || len(values) > 4 || values[0].Name() != name("SigPolicyId") { + if err != nil || len(values) < minimumSignaturePolicyChildren || len(values) > maximumSignaturePolicyChildren || values[0].Name() != name(nameSigPolicyID) { return nil, ErrProfile } @@ -67,7 +95,7 @@ func parseBaselinePolicy(ctx context.Context, element document.Element, options at++ } - if at >= len(values) || values[at].Name() != name("SigPolicyHash") { + if at >= len(values) || values[at].Name() != name(nameSigPolicyHash) { return nil, ErrProfile } @@ -77,7 +105,7 @@ func parseBaselinePolicy(ctx context.Context, element document.Element, options } at++ - if at < len(values) && values[at].Name() == name("SigPolicyQualifiers") { + if at < len(values) && values[at].Name() == name(nameSigPolicyQualifiers) { policy.Qualifiers, err = parsePolicyQualifiers(values[at]) if err != nil { return nil, err @@ -103,14 +131,14 @@ func parseBaselinePolicy(ctx context.Context, element document.Element, options } func buildBaselinePolicy(ctx context.Context, policy SignaturePolicy, options BaselineVerifyOptions) (*tree.Node, error) { - out := xnode("SignaturePolicyIdentifier") + out := xnode(nameSignaturePolicyIdentifier) if policy.Implied { if (policy.Identifier.Identifier != "" || policy.Identifier.Qualifier != "" || policy.Identifier.Description != "" || len(policy.Identifier.Documentation) != 0) || policy.DigestMethod != "" || len(policy.Digest) != 0 || len(policy.Transforms) != 0 || len(policy.Qualifiers) != 0 { return nil, ErrProfile } - add(out, xnode("SignaturePolicyImplied")) + add(out, xnode(nameSignaturePolicyImplied)) return out, nil } @@ -128,10 +156,10 @@ func buildBaselinePolicy(ctx context.Context, policy SignaturePolicy, options Ba return nil, ErrProfile } - explicit := xnode("SignaturePolicyId") + explicit := xnode(nameSignaturePolicyID) add(out, explicit) - identifier, err := buildObjectIdentifier("SigPolicyId", policy.Identifier) + identifier, err := buildObjectIdentifier(nameSigPolicyID, policy.Identifier) if err != nil { return nil, err } @@ -142,7 +170,7 @@ func buildBaselinePolicy(ctx context.Context, policy SignaturePolicy, options Ba transforms := dsnode(lexicon.DSigElementTransforms) for _, tr := range policy.Transforms { n := dsnode(lexicon.DSigElementTransform) - attr(n, "Algorithm", tr.Algorithm) + attr(n, lexicon.SecurityAttrAlgorithm, tr.Algorithm) if tr.Algorithm == PolicyDocumentDigestAsSpecified { if len(tr.XPath) != 0 || len(tr.InclusivePrefixes) != 0 || len(tr.Stylesheet) != 0 { @@ -158,9 +186,9 @@ func buildBaselinePolicy(ctx context.Context, policy SignaturePolicy, options Ba add(explicit, transforms) } - hash := xnode("SigPolicyHash") + hash := xnode(nameSigPolicyHash) method := dsnode(lexicon.DSigElementDigestMethod) - attr(method, "Algorithm", policy.DigestMethod) + attr(method, lexicon.SecurityAttrAlgorithm, policy.DigestMethod) add(hash, method) dv := dsnode(lexicon.DSigElementDigestValue) @@ -169,7 +197,7 @@ func buildBaselinePolicy(ctx context.Context, policy SignaturePolicy, options Ba add(explicit, hash) if len(policy.Qualifiers) > 0 { - qualifiers := xnode("SigPolicyQualifiers") + qualifiers := xnode(nameSigPolicyQualifiers) for _, qualifier := range policy.Qualifiers { value, err := buildPolicyQualifier(qualifier) @@ -191,7 +219,7 @@ func baselinePolicyDigest(ctx context.Context, policy SignaturePolicy, options B return nil, err } - if options.PolicyDocument == nil || len(options.PolicyDocument) > 8<<20 || len(policy.Transforms) > 16 { + if options.PolicyDocument == nil || len(options.PolicyDocument) > maximumPolicyDocumentBytes || len(policy.Transforms) > maximumPolicyTransforms { return nil, ErrProfile } @@ -230,7 +258,7 @@ func baselinePolicyDigest(ctx context.Context, policy SignaturePolicy, options B func parsePolicyQualifier(element document.Element) (PolicyQualifier, error) { var out PolicyQualifier - if element.Name() != name("SigPolicyQualifier") { + if element.Name() != name(nameSigPolicyQualifier) { return out, ErrProfile } @@ -249,7 +277,7 @@ func parsePolicyQualifier(element document.Element) (PolicyQualifier, error) { child := values[0] switch child.Name() { - case name("SPURI"): + case name(nameSPURI): value, err := simple(child) if err != nil || !baselineURI(value) { return out, ErrProfile @@ -260,13 +288,13 @@ func parsePolicyQualifier(element document.Element) (PolicyQualifier, error) { } else { out.URI = value } - case name("SPUserNotice"): + case name(nameSPUserNotice): var err error out.Notice, err = parsePolicyNotice(child) return out, err - case name141("SPDocSpecification"): + case name141(nameSPDocSpecification): value, err := parseObjectIdentifier(child) if err != nil || value.Qualifier == qualifierOIDAsURI { return out, ErrProfile @@ -282,7 +310,7 @@ func parsePolicyQualifier(element document.Element) (PolicyQualifier, error) { func buildPolicyQualifier(value PolicyQualifier) (*tree.Node, error) { if value.XML.Valid() { - if value.URI != "" || value.Notice != nil || value.Specification != nil || value.XML.Name() != name("SigPolicyQualifier") { + if value.URI != "" || value.Notice != nil || value.Specification != nil || value.XML.Name() != name(nameSigPolicyQualifier) { return nil, ErrProfile } @@ -308,14 +336,14 @@ func buildPolicyQualifier(value PolicyQualifier) (*tree.Node, error) { return nil, ErrProfile } - out := xnode("SigPolicyQualifier") + out := xnode(nameSigPolicyQualifier) if value.URI != "" { if !baselineURI(value.URI) { return nil, ErrProfile } - leaf(out, "SPURI", value.URI) + leaf(out, nameSPURI, value.URI) } if value.Specification != nil { @@ -323,13 +351,13 @@ func buildPolicyQualifier(value PolicyQualifier) (*tree.Node, error) { return nil, ErrProfile } - n, err := buildObjectIdentifier("SPDocSpecification", *value.Specification) + n, err := buildObjectIdentifier(nameSPDocSpecification, *value.Specification) if err != nil { return nil, err } - n.Name = name141("SPDocSpecification") - n.Prefix = prefixXades141 + n.Name = name141(nameSPDocSpecification) + n.Prefix = prefixXAdES141 add(out, n) } @@ -358,12 +386,12 @@ func parsePolicyNotice(element document.Element) (*PolicyNotice, error) { out := &PolicyNotice{} at := 0 - if at < len(values) && values[at].Name() == name("NoticeRef") { + if at < len(values) && values[at].Name() == name(nameNoticeRef) { if attrs(values[at]) != nil { return nil, ErrProfile } - fields, err := children(values[at], name("Organization"), name("NoticeNumbers")) + fields, err := children(values[at], name(nameOrganization), name(nameNoticeNumbers)) if err != nil { return nil, err } @@ -381,7 +409,7 @@ func parsePolicyNotice(element document.Element) (*PolicyNotice, error) { } for _, number := range numbers { - if number.Name() != name("int") { + if number.Name() != name(nameNoticeNumber) { return nil, ErrProfile } @@ -396,7 +424,7 @@ func parsePolicyNotice(element document.Element) (*PolicyNotice, error) { at++ } - if at < len(values) && values[at].Name() == name("ExplicitText") { + if at < len(values) && values[at].Name() == name(nameExplicitText) { out.ExplicitText, err = simple(values[at]) if err != nil { return nil, err @@ -413,19 +441,19 @@ func parsePolicyNotice(element document.Element) (*PolicyNotice, error) { } func buildPolicyNotice(value PolicyNotice) (*tree.Node, error) { - out := xnode("SPUserNotice") + out := xnode(nameSPUserNotice) if value.Organization != "" || value.NoticeNumbers != nil { - ref := xnode("NoticeRef") - leaf(ref, "Organization", value.Organization) + ref := xnode(nameNoticeRef) + leaf(ref, nameOrganization, value.Organization) - numbers := xnode("NoticeNumbers") + numbers := xnode(nameNoticeNumbers) for _, number := range value.NoticeNumbers { if !baselineInteger(number) { return nil, ErrProfile } - leaf(numbers, "int", number) + leaf(numbers, nameNoticeNumber, number) } add(ref, numbers) @@ -433,19 +461,19 @@ func buildPolicyNotice(value PolicyNotice) (*tree.Node, error) { } if value.ExplicitText != "" { - leaf(out, "ExplicitText", value.ExplicitText) + leaf(out, nameExplicitText, value.ExplicitText) } return out, nil } func baselineInteger(value string) bool { - value = strings.Trim(value, " \t\r\n") - if value == "" || len(value) > 8192 { + value = strings.Trim(value, lexicon.XMLWhitespace) + if value == "" || len(value) > maximumNoticeNumberBytes { return false } - _, ok := new(big.Int).SetString(value, 10) + _, ok := new(big.Int).SetString(value, decimalRadix) return ok } @@ -456,7 +484,7 @@ func parsePolicyTransforms(ctx context.Context, element document.Element) ([]ref } entries, err := baselineContent(element) - if err != nil || len(entries) == 0 || len(entries) > 16 { + if err != nil || len(entries) == 0 || len(entries) > maximumSignaturePolicies { return nil, ErrProfile } @@ -562,7 +590,7 @@ func baselinePolicySpecifications(qualifiers []PolicyQualifier) ([]ObjectIdentif } for _, child := range qualifier.XML.Children() { - if child.Name() != name141("SPDocSpecification") { + if child.Name() != name141(nameSPDocSpecification) { continue } diff --git a/xades/baseline_properties.go b/xades/baseline_properties.go index 6db2e82..01f8b22 100644 --- a/xades/baseline_properties.go +++ b/xades/baseline_properties.go @@ -24,6 +24,26 @@ import ( "github.com/skarm/xmlsec/xmldsig" ) +const ( + nameCity = "City" + nameCountryName = "CountryName" + nameIssuerSerialV2 = "IssuerSerialV2" + namePostalCode = "PostalCode" + nameSignatureProductionPlaceV2 = "SignatureProductionPlaceV2" + nameStateOrProvince = "StateOrProvince" + nameStreetAddress = "StreetAddress" + maximumAttributeCertificates = 64 + maximumCertificateCandidates = 256 + maximumDigestLexicalBytes = 256 + maximumIssuerNames = 32 + maximumIssuerSerialBytes = 16384 + maximumIssuerSerialLexicalBytes = 32768 + // Bounds Base64 text for embedded attribute certificates before decoding, in bytes. + maximumRoleCertificateLexicalBytes = 2 << 20 + maximumSigningCertificateIDs = 64 + maximumTextPropertyBytes = 8192 +) + func parseBaselineSigned(ctx context.Context, layout baselineLayout, certificate *x509.Certificate, options BaselineVerifyOptions, verified *xmldsig.SignatureResult) (*BaselineResult, error) { limits, err := normalizeDataTimestampLimits(options.DataTimestamps) if err != nil { @@ -69,7 +89,7 @@ func parseBaselineSignatureClaims(ctx context.Context, element document.Element, return err } - order := slices.Index([]document.Name{name("SigningTime"), name("SigningCertificateV2"), name("SignaturePolicyIdentifier"), name("SignatureProductionPlaceV2"), name("SignerRoleV2")}, child.Name()) + order := slices.Index([]document.Name{name(nameSigningTime), name(nameSigningCertificateV2), name(nameSignaturePolicyIdentifier), name(nameSignatureProductionPlaceV2), name(nameSignerRoleV2)}, child.Name()) if order < 0 || order <= previous { return ErrProfile } @@ -122,7 +142,7 @@ func parseBaselineDataClaims(ctx context.Context, element document.Element, veri func parseBaselineDataClaim(ctx context.Context, child document.Element, verified *xmldsig.SignatureResult, limits DataTimestampLimits, options BaselineVerifyOptions, result *BaselineResult) (int, error) { switch child.Name() { - case name("DataObjectFormat"): + case name(nameDataObjectFormat): value, err := parseDataObjectFormat(child) if err != nil { return 0, err @@ -131,7 +151,7 @@ func parseBaselineDataClaim(ctx context.Context, child document.Element, verifie result.data = append(result.data, value) return 0, nil - case name("CommitmentTypeIndication"): + case name(nameCommitmentTypeIndication): value, err := parseCommitment(child) if err != nil { return 0, err @@ -140,7 +160,7 @@ func parseBaselineDataClaim(ctx context.Context, child document.Element, verifie result.commitments = append(result.commitments, value) return 1, nil - case name("AllDataObjectsTimeStamp"), name("IndividualDataObjectsTimeStamp"): + case name(nameAllDataObjectsTimeStamp), name(nameIndividualDataObjectsTimeStamp): if len(result.dataTimestamps) >= limits.MaxTimestamps { return 0, ErrProfile } @@ -152,7 +172,7 @@ func parseBaselineDataClaim(ctx context.Context, child document.Element, verifie result.dataTimestamps = append(result.dataTimestamps, value) - if child.Name() == name("IndividualDataObjectsTimeStamp") { + if child.Name() == name(nameIndividualDataObjectsTimeStamp) { return 3, nil } @@ -163,12 +183,12 @@ func parseBaselineDataClaim(ctx context.Context, child document.Element, verifie } func buildBaselineProperties(ctx context.Context, certificate *x509.Certificate, signatureID, propertiesID string, properties BaselineProperties, options BaselineVerifyOptions) (*document.Document, error) { - qp := xnode("QualifyingProperties") - qp.NS = []tree.Namespace{{Prefix: nameXades, URI: Namespace}, {Prefix: "ds", URI: xmldsig.Namespace}, {Prefix: prefixXades141, URI: Namespace141}} - attr(qp, "Target", "#"+signatureID) + qp := xnode(nameQualifyingProperties) + qp.NS = []tree.Namespace{{Prefix: prefixXAdES, URI: Namespace}, {Prefix: lexicon.PrefixDSig, URI: xmldsig.Namespace}, {Prefix: prefixXAdES141, URI: Namespace141}} + attr(qp, lexicon.SecurityAttrTarget, "#"+signatureID) - sp := xnode("SignedProperties") - attr(sp, "Id", propertiesID) + sp := xnode(nameSignedProperties) + attr(sp, lexicon.SecurityAttrID, propertiesID) add(qp, sp) ssp, err := buildBaselineSignatureClaims(ctx, certificate, properties, options) @@ -196,13 +216,13 @@ func buildBaselineSignatureClaims(ctx context.Context, certificate *x509.Certifi return nil, err } - ssp := xnode("SignedSignatureProperties") - leaf(ssp, "SigningTime", value) + ssp := xnode(nameSignedSignatureProperties) + leaf(ssp, nameSigningTime, value) - sc := xnode("SigningCertificateV2") + sc := xnode(nameSigningCertificateV2) identifiers := append([]CertificateIdentifier{{DER: certificate.Raw, DigestMethod: properties.SigningCertificateDigest}}, properties.AdditionalCertificates...) - if len(identifiers) > 64 { + if len(identifiers) > maximumSigningCertificateIDs { return nil, ErrProfile } @@ -276,9 +296,9 @@ func buildBaselineCertificate(ctx context.Context, identifier CertificateIdentif return nil, err } - out := xnode("Cert") + out := xnode(nameCert) - digest, err := baselineDigestNode(ctx, "CertDigest", identifier.DigestMethod, c.Raw, options) + digest, err := baselineDigestNode(ctx, nameCertDigest, identifier.DigestMethod, c.Raw, options) if err != nil { return nil, err } @@ -291,14 +311,14 @@ func buildBaselineCertificate(ctx context.Context, identifier CertificateIdentif return nil, err } - leaf(out, "IssuerSerialV2", base64.StdEncoding.EncodeToString(encoded)) + leaf(out, nameIssuerSerialV2, base64.StdEncoding.EncodeToString(encoded)) } return out, nil } func buildProductionPlace(value ProductionPlace) (*tree.Node, error) { - out := xnode("SignatureProductionPlaceV2") + out := xnode(nameSignatureProductionPlaceV2) names := productionPlaceNames() for i, field := range []string{value.City, value.StreetAddress, value.StateOrProvince, value.PostalCode, value.CountryName} { @@ -324,7 +344,7 @@ func buildBaselineDataClaims(ctx context.Context, properties BaselineProperties, return nil, ErrProfile } - out := xnode("SignedDataObjectProperties") + out := xnode(nameSignedDataObjectProperties) for _, format := range properties.DataObjects { value, err := buildDataObjectFormat(format) @@ -386,7 +406,7 @@ func baselineContent(element document.Element) ([]document.Element, error) { } func baselineURI(value string) bool { - if len(value) > 8192 { + if len(value) > maximumTextPropertyBytes { return false } @@ -422,7 +442,7 @@ func baselinePropertyValue(ctx context.Context, value PropertyValue, expected do n.Name = expected if expected.Space == Namespace141 { - n.Prefix = prefixXades141 + n.Prefix = prefixXAdES141 } text(n, value.Text) @@ -468,7 +488,7 @@ func baselineDigestNode(ctx context.Context, local, method string, data []byte, node := xnode(local) dm := dsnode(lexicon.DSigElementDigestMethod) - attr(dm, "Algorithm", method) + attr(dm, lexicon.SecurityAttrAlgorithm, method) add(node, dm) dv := dsnode(lexicon.DSigElementDigestValue) @@ -488,11 +508,11 @@ func parseBaselineDigest(ctx context.Context, element document.Element, options return "", nil, err } - if attrs(elements[0], "Algorithm") != nil || len(elements[0].Children()) != 0 || !tree.IsXMLSpace(elements[0].Text()) { + if attrs(elements[0], lexicon.SecurityAttrAlgorithm) != nil || len(elements[0].Children()) != 0 || !tree.IsXMLSpace(elements[0].Text()) { return "", nil, ErrProfile } - method := localAttribute(elements[0], "Algorithm") + method := localAttribute(elements[0], lexicon.SecurityAttrAlgorithm) h, err := baselineHash(method, options) if err != nil { @@ -503,7 +523,7 @@ func parseBaselineDigest(ctx context.Context, element document.Element, options return "", nil, ErrProfile } - sum, err := decodeElementBase64(ctx, elements[1], 256, h.Size()) + sum, err := decodeElementBase64(ctx, elements[1], maximumDigestLexicalBytes, h.Size()) if err != nil || len(sum) != h.Size() { return "", nil, errors.Join(ErrProfile, err) } @@ -517,7 +537,7 @@ func parseBaselineCertificates(ctx context.Context, element document.Element, si } entries, err := baselineContent(element) - if err != nil || len(entries) == 0 || len(entries) > 64 { + if err != nil || len(entries) == 0 || len(entries) > maximumSigningCertificateIDs { return nil, ErrProfile } @@ -531,7 +551,7 @@ func parseBaselineCertificates(ctx context.Context, element document.Element, si candidates = append(candidates, options.CertificateValues...) candidates = append(candidates, embedded...) - if len(candidates) > 256 { + if len(candidates) > maximumCertificateCandidates { return nil, ErrProfile } @@ -561,7 +581,7 @@ func parseBaselineCertificate(ctx context.Context, entry document.Element, candi } values, err := baselineContent(entry) - if entry.Name() != name("Cert") || baselineAttrs(entry, nil, "URI") != nil || err != nil || len(values) < 1 || len(values) > 2 || values[0].Name() != name("CertDigest") { + if entry.Name() != name(nameCert) || baselineAttrs(entry, nil, lexicon.SecurityAttrURI) != nil || err != nil || len(values) < 1 || len(values) > 2 || values[0].Name() != name(nameCertDigest) { return out, ErrProfile } @@ -575,17 +595,17 @@ func parseBaselineCertificate(ctx context.Context, entry document.Element, candi return out, err } - out = CertificateReference{digestMethod: method, digest: sum, certificate: matched, uri: localAttribute(entry, "URI")} + out = CertificateReference{digestMethod: method, digest: sum, certificate: matched, uri: localAttribute(entry, lexicon.SecurityAttrURI)} if !baselineURI(out.uri) { return out, ErrProfile } if len(values) == 2 { - if values[1].Name() != name("IssuerSerialV2") || attrs(values[1]) != nil { + if values[1].Name() != name(nameIssuerSerialV2) || attrs(values[1]) != nil { return out, ErrProfile } - out.issuerSerial, err = decodeElementBase64(ctx, values[1], 32768, 16384) + out.issuerSerial, err = decodeElementBase64(ctx, values[1], maximumIssuerSerialLexicalBytes, maximumIssuerSerialBytes) if err != nil { return out, err } @@ -605,7 +625,7 @@ func matchBaselineCertificate(candidates [][]byte, method string, sum []byte, op } for _, candidate := range candidates { - if len(candidate) > 1<<20 { + if len(candidate) > maximumCertificateBytes { return nil, ErrCertificate } @@ -628,7 +648,7 @@ func matchBaselineCertificate(candidates [][]byte, method string, sum []byte, op } func baselineIssuerSerial(certificate *x509.Certificate) ([]byte, error) { - issuer, err := asn1.Marshal(asn1.RawValue{Class: 2, Tag: 4, IsCompound: true, Bytes: certificate.RawIssuer}) + issuer, err := asn1.Marshal(asn1.RawValue{Class: asn1.ClassContextSpecific, Tag: lexicon.GeneralNameDirectory, IsCompound: true, Bytes: certificate.RawIssuer}) if err != nil { return nil, err } @@ -647,28 +667,30 @@ func baselineIssuerSerial(certificate *x509.Certificate) ([]byte, error) { } func checkBaselineIssuerSerial(raw []byte) error { - value, err := der.Read(raw, 16384) + const issuerSerialFields = 2 // GeneralNames and certificate serial number + + value, err := der.Read(raw, maximumIssuerSerialBytes) if err != nil { return errors.Join(ErrProfile, err) } - fields, err := der.Sequence(value, 2, 2) + fields, err := der.Sequence(value, issuerSerialFields, issuerSerialFields) if err != nil { return ErrProfile } - names, err := der.Sequence(fields[0], 1, 32) + names, err := der.Sequence(fields[0], 1, maximumIssuerNames) if err != nil { return ErrProfile } for _, name := range names { - if name.Class != 2 || name.Tag != 4 || !name.IsCompound { + if name.Class != asn1.ClassContextSpecific || name.Tag != lexicon.GeneralNameDirectory || !name.IsCompound { return ErrProfile } - v, err := der.Explicit(name, 4) - if err != nil || !der.Is(v, 0, asn1.TagSequence, true) { + v, err := der.Explicit(name, lexicon.GeneralNameDirectory) + if err != nil || !der.Is(v, asn1.ClassUniversal, asn1.TagSequence, true) { return ErrProfile } } @@ -682,7 +704,7 @@ func checkBaselineIssuerSerial(raw []byte) error { } func productionPlaceNames() []string { - return []string{"City", "StreetAddress", "StateOrProvince", "PostalCode", "CountryName"} + return []string{nameCity, nameStreetAddress, nameStateOrProvince, namePostalCode, nameCountryName} } func parseProductionPlace(element document.Element) (*ProductionPlace, error) { @@ -755,7 +777,7 @@ func baselineEmbeddedCertificates(ctx context.Context, sig document.Element, sig continue } - raw, err := decodeElementBase64(ctx, value, 2<<20, 1<<20) + raw, err := decodeElementBase64(ctx, value, maximumRoleCertificateLexicalBytes, maximumCertificateBytes) if err != nil { return nil, err } @@ -765,7 +787,7 @@ func baselineEmbeddedCertificates(ctx context.Context, sig document.Element, sig found = true } - if len(out) > 64 { + if len(out) > maximumAttributeCertificates { return nil, ErrProfile } } diff --git a/xades/baseline_role.go b/xades/baseline_role.go index cdaa184..20ef14c 100644 --- a/xades/baseline_role.go +++ b/xades/baseline_role.go @@ -10,9 +10,25 @@ import ( "github.com/skarm/xmlsec/document" "github.com/skarm/xmlsec/internal/der" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/tree" ) +const ( + nameCertifiedRole = "CertifiedRole" + nameCertifiedRolesV2 = "CertifiedRolesV2" + nameClaimedRole = "ClaimedRole" + nameClaimedRoles = "ClaimedRoles" + nameOtherAttributeCertificate = "OtherAttributeCertificate" + nameSignedAssertion = "SignedAssertion" + nameSignedAssertions = "SignedAssertions" + nameX509AttributeCertificate = "X509AttributeCertificate" + maximumAttributeCertificateAttributes = 256 + maximumAttributeCertificateExtensions = 256 + maximumAttributeCertificateValues = 256 + maximumRoleValues = 64 +) + func parseBaselineRoles(ctx context.Context, element document.Element, result *BaselineResult) error { if attrs(element) != nil { return ErrProfile @@ -26,7 +42,7 @@ func parseBaselineRoles(ctx context.Context, element document.Element, result *B previous := -1 for _, group := range groups { - index := slices.Index([]document.Name{name("ClaimedRoles"), name("CertifiedRolesV2"), name("SignedAssertions")}, group.Name()) + index := slices.Index([]document.Name{name(nameClaimedRoles), name(nameCertifiedRolesV2), name(nameSignedAssertions)}, group.Name()) if index < 0 || index <= previous || attrs(group) != nil { return ErrProfile } @@ -41,7 +57,7 @@ func parseBaselineRoles(ctx context.Context, element document.Element, result *B for _, value := range values { switch index { case 0: - if value.Name() != name("ClaimedRole") { + if value.Name() != name(nameClaimedRole) { return ErrProfile } @@ -53,7 +69,7 @@ func parseBaselineRoles(ctx context.Context, element document.Element, result *B result.certifiedRoles = append(result.certifiedRoles, value) case 2: - if value.Name() != name("SignedAssertion") { + if value.Name() != name(nameSignedAssertion) { return ErrProfile } @@ -66,16 +82,16 @@ func parseBaselineRoles(ctx context.Context, element document.Element, result *B } func buildBaselineRoles(ctx context.Context, properties BaselineProperties) (*tree.Node, error) { - out := xnode("SignerRoleV2") + out := xnode(nameSignerRoleV2) groups := []struct { local string child string values []PropertyValue }{ - {"ClaimedRoles", "ClaimedRole", properties.ClaimedRoles}, - {"CertifiedRolesV2", "CertifiedRole", properties.CertifiedRoles}, - {"SignedAssertions", "SignedAssertion", properties.SignedAssertions}, + {nameClaimedRoles, nameClaimedRole, properties.ClaimedRoles}, + {nameCertifiedRolesV2, nameCertifiedRole, properties.CertifiedRoles}, + {nameSignedAssertions, nameSignedAssertion, properties.SignedAssertions}, } for _, group := range groups { @@ -83,7 +99,7 @@ func buildBaselineRoles(ctx context.Context, properties BaselineProperties) (*tr continue } - if len(group.values) > 64 { + if len(group.values) > maximumRoleValues { return nil, ErrProfile } @@ -94,7 +110,7 @@ func buildBaselineRoles(ctx context.Context, properties BaselineProperties) (*tr return nil, err } - if group.child == "CertifiedRole" { + if group.child == nameCertifiedRole { doc := tree.FromNodes([]*tree.Node{n}, document.DefaultLimits()) if err := checkCertifiedRole(ctx, doc.Root()); err != nil { return nil, err @@ -115,7 +131,7 @@ func buildBaselineRoles(ctx context.Context, properties BaselineProperties) (*tr } func checkCertifiedRole(ctx context.Context, element document.Element) error { - if element.Name() != name("CertifiedRole") || attrs(element) != nil { + if element.Name() != name(nameCertifiedRole) || attrs(element) != nil { return ErrProfile } @@ -125,11 +141,11 @@ func checkCertifiedRole(ctx context.Context, element document.Element) error { } switch values[0].Name() { - case name("OtherAttributeCertificate"): + case name(nameOtherAttributeCertificate): return nil // Domain syntax and authority remain explicitly external. - case name("X509AttributeCertificate"): + case name(nameX509AttributeCertificate): value := values[0] - if baselineAttrs(value, nil, "Id", attributeEncoding) != nil { + if baselineAttrs(value, nil, lexicon.SecurityAttrID, attributeEncoding) != nil { return ErrProfile } @@ -138,7 +154,7 @@ func checkCertifiedRole(ctx context.Context, element document.Element) error { return ErrProfile } - raw, err := decodeElementBase64(ctx, value, 2<<20, 1<<20) + raw, err := decodeElementBase64(ctx, value, maximumRoleCertificateLexicalBytes, maximumCertificateBytes) if err != nil { return err } @@ -153,23 +169,33 @@ func checkCertifiedRole(ctx context.Context, element document.Element) error { // signed-field structure. Issuer trust, holder matching, signatures and // attribute-specific authorization are separate caller policy decisions. func checkAttributeCertificate(raw []byte) error { - value, err := der.Read(raw, 1<<20) + // RFC 5755's v2 envelope has three fields. Its signed info has seven + // mandatory fields and optional issuerUniqueID/extensions; v2 is encoded as 1. + const ( + attributeCertificateFields = 3 + attributeCertificateRequiredFields = 7 + attributeCertificateMaximumFields = 9 + attributeCertificateVersion2 = 1 + attributeCertificateValidityFields = 2 + ) + + value, err := der.Read(raw, maximumCertificateBytes) if err != nil { return errors.Join(ErrProfile, err) } - fields, err := der.Sequence(value, 3, 3) + fields, err := der.Sequence(value, attributeCertificateFields, attributeCertificateFields) if err != nil { return ErrProfile } - info, err := der.Sequence(fields[0], 7, 9) + info, err := der.Sequence(fields[0], attributeCertificateRequiredFields, attributeCertificateMaximumFields) if err != nil { return ErrProfile } var version int - if der.Decode(info[0], &version) != nil || version != 1 { + if der.Decode(info[0], &version) != nil || version != attributeCertificateVersion2 { return ErrProfile } @@ -177,7 +203,7 @@ func checkAttributeCertificate(raw []byte) error { return err } - if !(der.Is(info[2], 0, asn1.TagSequence, true) || der.Is(info[2], 2, 0, true)) || len(info[2].Bytes) == 0 { + if !(der.Is(info[2], asn1.ClassUniversal, asn1.TagSequence, true) || der.Is(info[2], asn1.ClassContextSpecific, 0, true)) || len(info[2].Bytes) == 0 { return ErrProfile } @@ -194,8 +220,8 @@ func checkAttributeCertificate(raw []byte) error { return ErrProfile } - validity, err := der.Sequence(info[5], 2, 2) - if err != nil || !der.Is(validity[0], 0, asn1.TagGeneralizedTime, false) || !der.Is(validity[1], 0, asn1.TagGeneralizedTime, false) { + validity, err := der.Sequence(info[5], attributeCertificateValidityFields, attributeCertificateValidityFields) + if err != nil || !der.Is(validity[0], asn1.ClassUniversal, asn1.TagGeneralizedTime, false) || !der.Is(validity[1], asn1.ClassUniversal, asn1.TagGeneralizedTime, false) { return ErrProfile } @@ -203,20 +229,20 @@ func checkAttributeCertificate(raw []byte) error { return err } - at := 7 - if at < len(info) && der.Is(info[at], 0, asn1.TagBitString, false) { + at := attributeCertificateRequiredFields + if at < len(info) && der.Is(info[at], asn1.ClassUniversal, asn1.TagBitString, false) { at++ } if at < len(info) { - if _, err := der.Sequence(info[at], 1, 256); err != nil { + if _, err := der.Sequence(info[at], 1, maximumAttributeCertificateExtensions); err != nil { return ErrProfile } at++ } - if at != len(info) || !der.Is(fields[2], 0, asn1.TagBitString, false) || len(fields[2].Bytes) < 2 || fields[2].Bytes[0] != 0 { + if at != len(info) || !der.Is(fields[2], asn1.ClassUniversal, asn1.TagBitString, false) || len(fields[2].Bytes) < 2 || fields[2].Bytes[0] != 0 { return ErrProfile } @@ -224,8 +250,14 @@ func checkAttributeCertificate(raw []byte) error { } func baselineAlgorithmIdentifier(value asn1.RawValue) error { - fields, err := der.Sequence(value, 1, 2) - if err != nil || !der.Is(fields[0], 0, asn1.TagOID, false) { + // AlgorithmIdentifier has a required OID and optional parameters. + const ( + algorithmIdentifierMinimumFields = 1 + algorithmIdentifierMaximumFields = 2 + ) + + fields, err := der.Sequence(value, algorithmIdentifierMinimumFields, algorithmIdentifierMaximumFields) + if err != nil || !der.Is(fields[0], asn1.ClassUniversal, asn1.TagOID, false) { return ErrProfile } @@ -233,18 +265,20 @@ func baselineAlgorithmIdentifier(value asn1.RawValue) error { } func checkAttributeValues(value asn1.RawValue) error { - attributes, err := der.Sequence(value, 1, 256) + const attributeFields = 2 // type OID and SET OF values + + attributes, err := der.Sequence(value, 1, maximumAttributeCertificateAttributes) if err != nil { return ErrProfile } for _, attribute := range attributes { - pair, err := der.Sequence(attribute, 2, 2) - if err != nil || !der.Is(pair[0], 0, asn1.TagOID, false) { + pair, err := der.Sequence(attribute, attributeFields, attributeFields) + if err != nil || !der.Is(pair[0], asn1.ClassUniversal, asn1.TagOID, false) { return ErrProfile } - if _, err := der.SetFields(pair[1], 0, asn1.TagSet, 256); err != nil || len(pair[1].Bytes) == 0 { + if _, err := der.SetFields(pair[1], asn1.ClassUniversal, asn1.TagSet, maximumAttributeCertificateValues); err != nil || len(pair[1].Bytes) == 0 { return ErrProfile } } @@ -253,14 +287,22 @@ func checkAttributeValues(value asn1.RawValue) error { } func checkAttributeHolder(value asn1.RawValue) error { - holder, err := der.Sequence(value, 1, 3) + // Holder permits nonempty selections of its three context-specific fields. + const ( + holderMinimumFields = 1 + holderMaximumFields = 3 + holderFirstFieldTag = 0 + holderLastFieldTag = 2 + ) + + holder, err := der.Sequence(value, holderMinimumFields, holderMaximumFields) if err != nil { return ErrProfile } previous := -1 for _, field := range holder { - if field.Class != 2 || !field.IsCompound || field.Tag < 0 || field.Tag > 2 || field.Tag <= previous || len(field.Bytes) == 0 { + if field.Class != asn1.ClassContextSpecific || !field.IsCompound || field.Tag < holderFirstFieldTag || field.Tag > holderLastFieldTag || field.Tag <= previous || len(field.Bytes) == 0 { return ErrProfile } diff --git a/xades/baseline_time.go b/xades/baseline_time.go index a072dd3..db9ea5a 100644 --- a/xades/baseline_time.go +++ b/xades/baseline_time.go @@ -7,6 +7,22 @@ import ( "time" "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/lexicon" +) + +const ( + hoursPerDay = 24 + leapYearInterval = 4 + maximumSigningTimeBytes = 4096 + // Largest positive year representable by the bounded time.Time conversion path. + maximumTimeYear = 9999 + minutesPerHour = 60 + monthsPerYear = 12 + nanosecondFractionDigits = 9 + nanosecondsPerSecond = 1_000_000_000 + secondsPerMinute = 60 + yearsPerCentury = 100 + yearsPerGregorianEra = 400 ) // baselineUTC retains decimal precision and unbounded XML Schema 1.0 year @@ -28,7 +44,7 @@ func (value *baselineUTC) monthDays() int { return 30 case 2: var mod big.Int - if mod.Mod(&value.year, big.NewInt(400)).Sign() == 0 || mod.Mod(&value.year, big.NewInt(100)).Sign() != 0 && mod.Mod(&value.year, big.NewInt(4)).Sign() == 0 { + if mod.Mod(&value.year, big.NewInt(yearsPerGregorianEra)).Sign() == 0 || mod.Mod(&value.year, big.NewInt(yearsPerCentury)).Sign() != 0 && mod.Mod(&value.year, big.NewInt(leapYearInterval)).Sign() == 0 { return 29 } @@ -39,18 +55,18 @@ func (value *baselineUTC) monthDays() int { } func (value *baselineUTC) convenienceTime() time.Time { - if !value.year.IsInt64() || value.year.Int64() < 1 || value.year.Int64() > 9999 || len(value.fraction) > 9 { + if !value.year.IsInt64() || value.year.Int64() < 1 || value.year.Int64() > maximumTimeYear || len(value.fraction) > nanosecondFractionDigits { return time.Time{} } - fraction := value.fraction + strings.Repeat("0", 9-len(value.fraction)) + fraction := value.fraction + strings.Repeat("0", nanosecondFractionDigits-len(value.fraction)) nanos, _ := strconv.Atoi(fraction) return time.Date(int(value.year.Int64()), time.Month(value.month), value.day, value.hour, value.minute, value.second, nanos, time.UTC) } func (value *baselineUTC) normalizeMidnight() error { - if value.hour == 24 { + if value.hour == hoursPerDay { if value.minute != 0 || value.second != 0 || value.fraction != "" { return ErrProfile } @@ -62,7 +78,7 @@ func (value *baselineUTC) normalizeMidnight() error { value.day = 1 value.month++ - if value.month == 13 { + if value.month == monthsPerYear+1 { value.month = 1 value.year.Add(&value.year, big.NewInt(1)) } @@ -78,7 +94,7 @@ func parseBaselineTime(element document.Element) (string, time.Time, error) { return "", time.Time{}, err } - value = strings.Trim(value, " \t\r\n") + value = strings.Trim(value, lexicon.XMLWhitespace) parsed, err := parseBaselineUTC(value) if err != nil { @@ -89,16 +105,25 @@ func parseBaselineTime(element document.Element) (string, time.Time, error) { } func parseBaselineUTC(value string) (baselineUTC, error) { + // Byte widths describe the accepted UTC lexical form, independently of + // the overall resource cap and the year value's supported magnitude. + const ( + utcOffsetBytes = 6 // +00:00 or -00:00 + minimumUTCDateTimeBytes = 20 + unfractionatedTailBytes = 16 // -MM-DDThh:mm:ssZ + minimumFractionalTailBytes = 18 + ) + var out baselineUTC - if len(value) > 4096 { + if len(value) > maximumSigningTimeBytes { return out, ErrProfile } if strings.HasSuffix(value, "+00:00") || strings.HasSuffix(value, "-00:00") { - value = value[:len(value)-6] + "Z" + value = value[:len(value)-utcOffsetBytes] + "Z" } - if len(value) < 20 || len(value) > 4096 || !strings.HasSuffix(value, "Z") { + if len(value) < minimumUTCDateTimeBytes || len(value) > maximumSigningTimeBytes || !strings.HasSuffix(value, "Z") { return out, ErrProfile } @@ -108,7 +133,7 @@ func parseBaselineUTC(value string) (baselineUTC, error) { } tail := value[yearEnd:] - if len(tail) < 16 || tail[0] != '-' || tail[3] != '-' || tail[6] != 'T' || tail[9] != ':' || tail[12] != ':' { + if len(tail) < unfractionatedTailBytes || tail[0] != '-' || tail[3] != '-' || tail[6] != 'T' || tail[9] != ':' || tail[12] != ':' { return out, ErrProfile } @@ -123,15 +148,15 @@ func parseBaselineUTC(value string) (baselineUTC, error) { *fields[i] = int(part[0]-'0')*10 + int(part[1]-'0') } - if len(tail) > 16 { - if tail[15] != '.' || len(tail) < 18 || !baselineDigits(tail[16:len(tail)-1]) { + if len(tail) > unfractionatedTailBytes { + if tail[15] != '.' || len(tail) < minimumFractionalTailBytes || !baselineDigits(tail[16:len(tail)-1]) { return out, ErrProfile } out.fraction = strings.TrimRight(tail[16:len(tail)-1], "0") } - if out.month < 1 || out.month > 12 || out.day < 1 || out.day > out.monthDays() || out.hour > 24 || out.minute > 59 || out.second > 59 { + if out.month < 1 || out.month > monthsPerYear || out.day < 1 || out.day > out.monthDays() || out.hour > hoursPerDay || out.minute > minutesPerHour-1 || out.second > secondsPerMinute-1 { return out, ErrProfile } @@ -200,7 +225,7 @@ func (r *BaselineResult) CompareSigningTime(other time.Time) (int, error) { } } - fraction := strconv.Itoa(other.Nanosecond() + 1_000_000_000)[1:] + fraction := strconv.Itoa(other.Nanosecond() + nanosecondsPerSecond)[1:] for i := range max(len(fraction), len(value.fraction)) { a, b := byte('0'), byte('0') if i < len(value.fraction) { @@ -224,6 +249,9 @@ func (r *BaselineResult) CompareSigningTime(other time.Time) (int, error) { } func baselineTimeYear(value string, yearValue *big.Int) (int, error) { + // XML Schema dateTime years need at least four digits, excluding the sign. + const minimumYearDigits = 4 + yearEnd := strings.IndexByte(value, '-') if yearEnd == 0 { next := strings.IndexByte(value[1:], '-') @@ -234,7 +262,7 @@ func baselineTimeYear(value string, yearValue *big.Int) (int, error) { yearEnd = next + 1 } - if yearEnd < 4 { + if yearEnd < minimumYearDigits { return 0, ErrProfile } @@ -245,11 +273,11 @@ func baselineTimeYear(value string, yearValue *big.Int) (int, error) { digits = digits[1:] } - if len(digits) < 4 || len(digits) > 4 && digits[0] == '0' || !baselineDigits(digits) { + if len(digits) < minimumYearDigits || len(digits) > minimumYearDigits && digits[0] == '0' || !baselineDigits(digits) { return 0, ErrProfile } - if _, ok := yearValue.SetString(year, 10); !ok || yearValue.Sign() == 0 { + if _, ok := yearValue.SetString(year, decimalRadix); !ok || yearValue.Sign() == 0 { return 0, ErrProfile } diff --git a/xades/baseline_types.go b/xades/baseline_types.go index 6ff88a9..ca04ee6 100644 --- a/xades/baseline_types.go +++ b/xades/baseline_types.go @@ -12,7 +12,7 @@ import ( ) const ( - prefixXades141 = "xades141" + prefixXAdES141 = "xades141" qualifierOIDAsURI = "OIDAsURI" qualifierOIDAsURN = "OIDAsURN" attributeEncoding = "Encoding" @@ -254,18 +254,36 @@ func (r CertificateReference) URI() string { return r.uri } // grants no PKIX, timestamp, role, policy or legal trust. Closed enveloped APIs // retain their separate, stricter contracts. type BaselineResult struct { + // dataTimestamps retains signed data-timestamp bindings awaiting timestamp + // trust checks. dataTimestamps []DataTimestamp - basic *BasicResult - signingTime string - certificates []CertificateReference - data []DataObjectFormat - place *ProductionPlace - roles []document.Element + // basic retains the authenticated signature, properties and certificate + // binding. + basic *BasicResult + // signingTime preserves the complete signed UTC lexical value without precision + // loss. + signingTime string + // certificates retains authenticated signing-certificate references without + // granting PKIX trust. + certificates []CertificateReference + // data retains signed data-object format claims and their reference bindings. + data []DataObjectFormat + // place retains the claimed signature production place, if present. + place *ProductionPlace + // roles retains signed claimed roles without authorizing those roles. + roles []document.Element + // certifiedRoles retains role certificates requiring independent validation. certifiedRoles []document.Element - assertions []document.Element - commitments []CommitmentType - policy *SignaturePolicy - unsigned document.Element + // assertions retains signed assertion properties requiring independent + // validation. + assertions []document.Element + // commitments retains signed commitment-type claims. + commitments []CommitmentType + // policy retains the signed signature-policy claim. + policy *SignaturePolicy + // unsigned identifies unsigned properties; signature verification does not + // authenticate them. + unsigned document.Element } // Basic returns the authenticated common signature/property/certificate core @@ -309,7 +327,7 @@ func (r *BaselineResult) UnsignedSignatureProperties() []document.Element { } for _, child := range r.unsigned.Children() { - if child.Name() == name("UnsignedSignatureProperties") { + if child.Name() == name(nameUnsignedSignatureProperties) { return child.Children() } } diff --git a/xades/basic.go b/xades/basic.go index 965c42a..affe21c 100644 --- a/xades/basic.go +++ b/xades/basic.go @@ -29,6 +29,14 @@ import ( "github.com/skarm/xmlsec/xmldsig" ) +const ( + defaultDataReferenceID = "data-reference" + defaultIDAttribute = "ID" + base64CancellationMask = 4095 + base64LexicalReadBytes = 4096 + maximumIDBytes = 256 +) + const ( // Namespace is the XAdES 1.3.2 namespace URI. Namespace = "http://uri.etsi.org/01903/v1.3.2#" @@ -79,11 +87,17 @@ type VerifyOptions struct { // supplied certificate's key. It does not check certificate dates, PKIX trust, // revocation, claimed time, legal status or other AdES evidence. type BasicResult struct { - signature *xmldsig.SignatureResult - properties document.Element + // signature retains the verified XML signature and reference results. + signature *xmldsig.SignatureResult + // properties identifies the authenticated SignedProperties element. + properties document.Element + // certificate retains the exact signer certificate bound by signed properties. certificate []byte + // claimedTime is the signed signing-time claim, not an independently trusted + // time. claimedTime time.Time - mimeType string + // mimeType retains the authenticated data format claim when unambiguous. + mimeType string } type basicPropertySet struct { @@ -166,7 +180,7 @@ func SignBasicEnveloped(ctx context.Context, doc *document.Document, signer cryp return nil, nil, err } - if len(xmldsig.Signatures(doc)) != 0 || o.ClaimedSigningTime.IsZero() || o.ClaimedSigningTime.UTC().Year() < 1 || o.ClaimedSigningTime.UTC().Year() > 9999 { + if len(xmldsig.Signatures(doc)) != 0 || o.ClaimedSigningTime.IsZero() || o.ClaimedSigningTime.UTC().Year() < 1 || o.ClaimedSigningTime.UTC().Year() > maximumTimeYear { return nil, nil, ErrProfile } // Use a DER pin rather than relying on a provider-specific key representation. @@ -184,15 +198,15 @@ func SignBasicEnveloped(ctx context.Context, doc *document.Document, signer cryp } if o.SignatureID == "" { - o.SignatureID = "signature" + o.SignatureID = defaultBaselineSignatureID } if o.SignedPropertiesID == "" { - o.SignedPropertiesID = "signed-properties" + o.SignedPropertiesID = defaultSignedPropertiesID } if o.DataReferenceID == "" { - o.DataReferenceID = "data-reference" + o.DataReferenceID = defaultDataReferenceID } for _, id := range []string{o.SignatureID, o.SignedPropertiesID, o.DataReferenceID} { @@ -202,7 +216,7 @@ func SignBasicEnveloped(ctx context.Context, doc *document.Document, signer cryp } if o.MIMEType == "" { - o.MIMEType = "application/xml" + o.MIMEType = lexicon.MediaTypeXML } if !validMime(o.MIMEType) { @@ -216,26 +230,26 @@ func SignBasicEnveloped(ctx context.Context, doc *document.Document, signer cryp return nil, nil, ErrProfile } - qp := xnode("QualifyingProperties") - qp.NS = []tree.Namespace{{Prefix: nameXades, URI: Namespace}, {Prefix: "ds", URI: xmldsig.Namespace}} - attr(qp, "Target", "#"+o.SignatureID) + qp := xnode(nameQualifyingProperties) + qp.NS = []tree.Namespace{{Prefix: prefixXAdES, URI: Namespace}, {Prefix: lexicon.PrefixDSig, URI: xmldsig.Namespace}} + attr(qp, lexicon.SecurityAttrTarget, "#"+o.SignatureID) - sp := xnode("SignedProperties") - attr(sp, "Id", o.SignedPropertiesID) + sp := xnode(nameSignedProperties) + attr(sp, lexicon.SecurityAttrID, o.SignedPropertiesID) add(qp, sp) - ssp := xnode("SignedSignatureProperties") + ssp := xnode(nameSignedSignatureProperties) add(sp, ssp) - st := xnode("SigningTime") + st := xnode(nameSigningTime) text(st, o.ClaimedSigningTime.UTC().Format(time.RFC3339Nano)) add(ssp, st) - sc := xnode("SigningCertificateV2") - ce := xnode("Cert") - cd := xnode("CertDigest") + sc := xnode(nameSigningCertificateV2) + ce := xnode(nameCert) + cd := xnode(nameCertDigest) dm := dsnode(lexicon.DSigElementDigestMethod) - attr(dm, "Algorithm", xmldsig.SHA256) + attr(dm, lexicon.SecurityAttrAlgorithm, xmldsig.SHA256) dv := dsnode(lexicon.DSigElementDigestValue) sum := sha256.Sum256(c.Raw) @@ -246,11 +260,11 @@ func SignBasicEnveloped(ctx context.Context, doc *document.Document, signer cryp add(sc, ce) add(ssp, sc) - sdop := xnode("SignedDataObjectProperties") - dof := xnode("DataObjectFormat") - attr(dof, "ObjectReference", "#"+o.DataReferenceID) + sdop := xnode(nameSignedDataObjectProperties) + dof := xnode(nameDataObjectFormat) + attr(dof, attrObjectReference, "#"+o.DataReferenceID) - mt := xnode("MimeType") + mt := xnode(lexicon.SecurityAttrMimeType) text(mt, o.MIMEType) add(dof, mt) add(sdop, dof) @@ -271,17 +285,17 @@ func SignBasicEnveloped(ctx context.Context, doc *document.Document, signer cryp return nil, nil, err } - object, err := childNamed(selected, ds("Object")) + object, err := childNamed(selected, ds(lexicon.DSigElementObject)) if err != nil { return nil, nil, err } - properties, err := childNamed(object, name("QualifyingProperties")) + properties, err := childNamed(object, name(nameQualifyingProperties)) if err != nil { return nil, nil, err } - props, err := childNamed(properties, name("SignedProperties")) + props, err := childNamed(properties, name(nameSignedProperties)) if err != nil { return nil, nil, err } @@ -426,6 +440,10 @@ func decodeElementBase64(ctx context.Context, element document.Element, encodedL // unsigned=true is only used by higher profiles which MUST validate every // unsigned property before returning their result. The public basic API stays closed. func verifyBasicEnveloped(ctx context.Context, doc *document.Document, selected document.Element, trustedCertificateDER []byte, o VerifyOptions, unsigned bool) (*BasicResult, error) { + // This profile requires canonicalization, signature method, and exactly + // two references: the data object and SignedProperties. + const basicSignedInfoChildren = 4 + if ctx == nil || doc == nil || !selected.Valid() { return nil, ErrProfile } @@ -449,16 +467,16 @@ func verifyBasicEnveloped(ctx context.Context, doc *document.Document, selected return nil, err } - if attrs(selected, "Id") != nil { + if attrs(selected, lexicon.SecurityAttrID) != nil { return nil, ErrProfile } - sigID := localAttribute(selected, "Id") + sigID := localAttribute(selected, lexicon.SecurityAttrID) if !tree.NCName(sigID) { return nil, ErrProfile } - sc, err := children(selected, ds(lexicon.DSigElementSignedInfo), ds(lexicon.DSigElementSignatureValue), ds(lexicon.DSigElementKeyInfo), ds("Object")) + sc, err := children(selected, ds(lexicon.DSigElementSignedInfo), ds(lexicon.DSigElementSignatureValue), ds(lexicon.DSigElementKeyInfo), ds(lexicon.DSigElementObject)) if err != nil { return nil, err } @@ -474,7 +492,7 @@ func verifyBasicEnveloped(ctx context.Context, doc *document.Document, selected sp, spID := properties.signed, properties.id - ssp, err := children(properties.signature, name("SigningTime"), name("SigningCertificateV2")) + ssp, err := children(properties.signature, name(nameSigningTime), name(nameSigningCertificateV2)) if err != nil { return nil, err } @@ -494,7 +512,7 @@ func verifyBasicEnveloped(ctx context.Context, doc *document.Document, selected } siChildren := sc[0].Children() - if len(siChildren) != 4 { + if len(siChildren) != basicSignedInfoChildren { return nil, ErrProfile } @@ -505,12 +523,12 @@ func verifyBasicEnveloped(ctx context.Context, doc *document.Document, selected refs := siChildren[2:] - dataID := localAttribute(refs[0], "Id") + dataID := localAttribute(refs[0], lexicon.SecurityAttrID) if !tree.NCName(dataID) || fmtRef != "#"+dataID { return nil, ErrProfile } - if localAttribute(refs[0], "URI") != "#"+rootID || localAttribute(refs[0], "Type") != "" || localAttribute(refs[1], "URI") != "#"+spID || localAttribute(refs[1], "Type") != SignedPropertiesType { + if localAttribute(refs[0], lexicon.SecurityAttrURI) != "#"+rootID || localAttribute(refs[0], lexicon.SecurityAttrType) != "" || localAttribute(refs[1], lexicon.SecurityAttrURI) != "#"+spID || localAttribute(refs[1], lexicon.SecurityAttrType) != SignedPropertiesType { return nil, ErrProfile } // Exact transforms preserve original-node coverage and exclude only our Signature. @@ -542,9 +560,12 @@ func xmlSpace(s string) bool { } func validateBasicTransforms(refs []tree.Element) error { + // Each Reference has Transforms, DigestMethod and DigestValue. + const basicReferenceChildren = 3 + for i, r := range refs { a := r.Children() - if len(a) != 3 || a[0].Name() != ds(lexicon.DSigElementTransforms) { + if len(a) != basicReferenceChildren || a[0].Name() != ds(lexicon.DSigElementTransforms) { return ErrProfile } @@ -560,7 +581,7 @@ func validateBasicTransforms(refs []tree.Element) error { } for j, t := range trs { - if t.Name() != ds(lexicon.DSigElementTransform) || attrs(t, "Algorithm") != nil || len(t.Children()) != 0 || localAttribute(t, "Algorithm") != want[j] { + if t.Name() != ds(lexicon.DSigElementTransform) || attrs(t, lexicon.SecurityAttrAlgorithm) != nil || len(t.Children()) != 0 || localAttribute(t, lexicon.SecurityAttrAlgorithm) != want[j] { return ErrProfile } } @@ -599,12 +620,12 @@ func verifyEmbeddedCertificate(ctx context.Context, keyInfo document.Element, ce } func verifySigningCertificate(ctx context.Context, property document.Element, certificate *x509.Certificate) error { - cv, err := children(property, name("Cert")) + cv, err := children(property, name(nameCert)) if err != nil || attrs(property) != nil { return ErrProfile } - cd, err := children(cv[0], name("CertDigest")) + cd, err := children(cv[0], name(nameCertDigest)) if err != nil || attrs(cv[0]) != nil { return ErrProfile } @@ -614,7 +635,7 @@ func verifySigningCertificate(ctx context.Context, property document.Element, ce return ErrProfile } - if attrs(cvd[0], "Algorithm") != nil || len(cvd[0].Children()) != 0 || !xmlSpace(cvd[0].Text()) || localAttribute(cvd[0], "Algorithm") != xmldsig.SHA256 { + if attrs(cvd[0], lexicon.SecurityAttrAlgorithm) != nil || len(cvd[0].Children()) != 0 || !xmlSpace(cvd[0].Text()) || localAttribute(cvd[0], lexicon.SecurityAttrAlgorithm) != xmldsig.SHA256 { return ErrProfile } @@ -652,19 +673,19 @@ func parseClaimedSigningTime(property document.Element) (time.Time, error) { } func parseBasicProperties(object document.Element, sigID string, unsigned bool) (basicPropertySet, error) { - ob, err := children(object, name("QualifyingProperties")) + ob, err := children(object, name(nameQualifyingProperties)) if err != nil || attrs(object) != nil { return basicPropertySet{}, ErrProfile } qp := ob[0] - if attrs(qp, "Target") != nil || localAttribute(qp, "Target") != "#"+sigID { + if attrs(qp, lexicon.SecurityAttrTarget) != nil || localAttribute(qp, lexicon.SecurityAttrTarget) != "#"+sigID { return basicPropertySet{}, ErrProfile } - qpNames := []document.Name{name("SignedProperties")} + qpNames := []document.Name{name(nameSignedProperties)} if unsigned && len(qp.Children()) == 2 { - qpNames = append(qpNames, name("UnsignedProperties")) + qpNames = append(qpNames, name(nameUnsignedProperties)) } qpChildren, err := children(qp, qpNames...) @@ -673,16 +694,16 @@ func parseBasicProperties(object document.Element, sigID string, unsigned bool) } sp := qpChildren[0] - if attrs(sp, "Id") != nil { + if attrs(sp, lexicon.SecurityAttrID) != nil { return basicPropertySet{}, ErrProfile } - spID := localAttribute(sp, "Id") + spID := localAttribute(sp, lexicon.SecurityAttrID) if !tree.NCName(spID) { return basicPropertySet{}, ErrProfile } - spChildren, err := children(sp, name("SignedSignatureProperties"), name("SignedDataObjectProperties")) + spChildren, err := children(sp, name(nameSignedSignatureProperties), name(nameSignedDataObjectProperties)) if err != nil { return basicPropertySet{}, err } @@ -695,18 +716,18 @@ func parseBasicProperties(object document.Element, sigID string, unsigned bool) } func parseBasicDataFormat(data document.Element) (string, string, error) { - formats, err := children(data, name("DataObjectFormat")) + formats, err := children(data, name(nameDataObjectFormat)) if err != nil { return "", "", err } - if attrs(formats[0], "ObjectReference") != nil { + if attrs(formats[0], attrObjectReference) != nil { return "", "", ErrProfile } - fmtRef := localAttribute(formats[0], "ObjectReference") + fmtRef := localAttribute(formats[0], attrObjectReference) - mts, err := children(formats[0], name("MimeType")) + mts, err := children(formats[0], name(lexicon.SecurityAttrMimeType)) if err != nil { return "", "", err } @@ -737,7 +758,7 @@ func base64TextLength(ctx context.Context, nodes []*tree.Node, encodedLimit int) remaining -= len(child.Data) for i := range len(child.Data) { - if i&4095 == 0 { + if i&base64CancellationMask == 0 { if err := ctx.Err(); err != nil { return 0, err } @@ -753,11 +774,11 @@ func base64TextLength(ctx context.Context, nodes []*tree.Node, encodedLimit int) } func xnode(local string) *tree.Node { - return &tree.Node{Kind: tree.ElementNode, Name: document.Name{Space: Namespace, Local: local}, Prefix: nameXades} + return &tree.Node{Kind: tree.ElementNode, Name: document.Name{Space: Namespace, Local: local}, Prefix: prefixXAdES} } func dsnode(local string) *tree.Node { - return &tree.Node{Kind: tree.ElementNode, Name: document.Name{Space: xmldsig.Namespace, Local: local}, Prefix: "ds"} + return &tree.Node{Kind: tree.ElementNode, Name: document.Name{Space: xmldsig.Namespace, Local: local}, Prefix: lexicon.PrefixDSig} } func add(p, c *tree.Node) { p.Children = append(p.Children, c); c.Parent = p } @@ -790,14 +811,14 @@ func childNamed(e document.Element, n document.Name) (document.Element, error) { func normalizeID(n document.Name) document.Name { if n == (document.Name{}) { - return document.Name{Local: "ID"} + return document.Name{Local: defaultIDAttribute} } return n } func cert(der []byte) (*x509.Certificate, error) { - if len(der) == 0 || len(der) > 1<<20 { + if len(der) == 0 || len(der) > maximumCertificateBytes { return nil, ErrCertificate } @@ -810,7 +831,7 @@ func cert(der []byte) (*x509.Certificate, error) { } func validMime(s string) bool { - if len(s) == 0 || len(s) > 256 { + if len(s) == 0 || len(s) > maximumIDBytes { return false } @@ -822,7 +843,7 @@ func validMime(s string) bool { func profileOptions(id document.Name, policy xmldsig.Policy, root, props document.Element, rootID, propsID string) xmldsig.SignatureOptions { return xmldsig.SignatureOptions{ IDAttribute: normalizeID(id), Policy: policy, - AdditionalIDRules: []reference.IDAttributeRule{{Attribute: document.Name{Local: "Id"}, Elements: []document.Name{{Space: Namespace, Local: "SignedProperties"}}}}, + AdditionalIDRules: []reference.IDAttributeRule{{Attribute: document.Name{Local: lexicon.SecurityAttrID}, Elements: []document.Name{{Space: Namespace, Local: nameSignedProperties}}}}, RequiredElements: []document.Element{root, props}, ExpectedURIs: []string{"#" + rootID, "#" + propsID}, } } @@ -868,7 +889,7 @@ func (r *base64TextReader) Read(dst []byte) (int, error) { } } - span := r.text[:min(len(r.text), 4096)] + span := r.text[:min(len(r.text), base64LexicalReadBytes)] taken := 0 for taken < len(span) && used < len(dst) { diff --git a/xades/basic_test.go b/xades/basic_test.go index 571f5e9..cbeacd8 100644 --- a/xades/basic_test.go +++ b/xades/basic_test.go @@ -46,7 +46,7 @@ func (s baselineRecordingSigner) Sign(random io.Reader, digest []byte, options c } func TestBasicRoundTrip(t *testing.T) { - d, r, _, der := sign(t) + d, r, _, dr := sign(t) if len(r.Signature().References()) != 2 || !r.Signature().CoversElement(d.Root()) || !r.Signature().CoversElement(r.SignedProperties()) { t.Fatal("coverage") } @@ -55,12 +55,12 @@ func TestBasicRoundTrip(t *testing.T) { t.Fatal("properties") } - if !bytes.Equal(r.CertificateDER(), der) { + if !bytes.Equal(r.CertificateDER(), dr) { t.Fatal("cert") } r.CertificateDER()[0] ^= 1 - if !bytes.Equal(r.CertificateDER(), der) { + if !bytes.Equal(r.CertificateDER(), dr) { t.Fatal("mutable cert") } @@ -71,12 +71,15 @@ func TestBasicRoundTrip(t *testing.T) { t.Fatal(e) } - r, e = xades.VerifyBasicEnveloped(t.Context(), parsed, xmldsig.Signatures(parsed)[0], der, xades.VerifyOptions{}) + r, e = xades.VerifyBasicEnveloped(t.Context(), parsed, xmldsig.Signatures(parsed)[0], dr, xades.VerifyOptions{}) if e != nil || r == nil { t.Fatal(e) } // Expired at the claimed time is intentionally not a trust check here. - cert, _ := x509.ParseCertificate(der) + cert, err := x509.ParseCertificate(dr) + if err != nil { + t.Fatal(err) + } if !r.ClaimedSigningTime().After(cert.NotAfter) { t.Fatal("fixture") } @@ -87,7 +90,7 @@ func TestBasicRoundTrip(t *testing.T) { } func TestBasicProfileRejects(t *testing.T) { - d, _, k, der := sign(t) + d, _, k, dr := sign(t) raw, _ := d.XML(t.Context()) other := certificate(t, k, 2) @@ -97,14 +100,14 @@ func TestBasicProfileRejects(t *testing.T) { to string cert []byte }{ - {"target", `Target="#signature"`, `Target="#other"`, der}, - {"wrong-type", `Type="` + xades.SignedPropertiesType + `"`, `Type="urn:wrong"`, der}, - {"format", `ObjectReference="#data-reference"`, `ObjectReference="#signed-properties"`, der}, - {"unsigned-augmentation", ``, ``, der}, + {"target", `Target="#signature"`, `Target="#other"`, dr}, + {"wrong-type", `Type="` + xades.SignedPropertiesType + `"`, `Type="urn:wrong"`, dr}, + {"format", `ObjectReference="#data-reference"`, `ObjectReference="#signed-properties"`, dr}, + {"unsigned-augmentation", ``, ``, dr}, {"wrong-certificate", "", "", other}, - {"swap-keyinfo", base64.StdEncoding.EncodeToString(der), base64.StdEncoding.EncodeToString(other), der}, - {"payload", `>123<`, `>999<`, der}, - {"no-utc", "13:30:15.000123456Z", "13:30:15.000123456+00:00", der}, + {"swap-keyinfo", base64.StdEncoding.EncodeToString(dr), base64.StdEncoding.EncodeToString(other), dr}, + {"payload", `>123<`, `>999<`, dr}, + {"no-utc", "13:30:15.000123456Z", "13:30:15.000123456+00:00", dr}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -259,96 +262,6 @@ func TestAuthenticatedSigningTimeLexicalForm(t *testing.T) { } } -func signingTimeTemplate(t *testing.T, d *document.Document, lexical string) *document.Document { - t.Helper() - - draft, err := tree.Clone(t.Context(), d) - if err != nil { - t.Fatal(err) - } - - clearSignatureValues(xmldsig.Signatures(draft)[0]) - changeSigningTime(tree.Root(draft), lexical) - - raw, err := draft.XML(t.Context()) - if err != nil { - t.Fatal(err) - } - - template, err := document.Parse(t.Context(), raw, document.DefaultLimits()) - if err != nil { - t.Fatal(err) - } - - return template -} - -func clearSignatureValues(sig document.Element) { - _, value := tree.Unwrap(sig.Children()[1]) - value.Children = nil - - for _, ref := range sig.Children()[0].Children()[2:] { - fields := ref.Children() - _, digest := tree.Unwrap(fields[len(fields)-1]) - digest.Children = nil - } -} - -func changeSigningTime(n *tree.Node, value string) { - if n.Name == (document.Name{Space: xades.Namespace, Local: "SigningTime"}) { - n.Children = []*tree.Node{{Kind: tree.TextNode, Data: value, Parent: n}} - - return - } - - for _, child := range n.Children { - changeSigningTime(child, value) - } -} - -func certificate(t testing.TB, key *ecdsa.PrivateKey, serial int64) []byte { - t.Helper() - - at := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC) - c := &x509.Certificate{SerialNumber: big.NewInt(serial), Subject: pkix.Name{CommonName: "xades test"}, NotBefore: at, NotAfter: at.Add(time.Hour), KeyUsage: x509.KeyUsageDigitalSignature} - - der, e := x509.CreateCertificate(rand.Reader, c, c, &key.PublicKey, key) - if e != nil { - t.Fatal(e) - } - - return der -} - -func input(t testing.TB) *document.Document { - t.Helper() - - d, e := document.Parse(t.Context(), []byte(`123`), document.DefaultLimits()) - if e != nil { - t.Fatal(e) - } - - return d -} - -func sign(t testing.TB) (*document.Document, *xades.BasicResult, *ecdsa.PrivateKey, []byte) { - t.Helper() - - k, e := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) - if e != nil { - t.Fatal(e) - } - - der := certificate(t, k, 1) - - d, r, e := xades.SignBasicEnveloped(t.Context(), input(t), k, der, xades.SignOptions{SignatureMethod: xmldsig.ECDSASHA256, ClaimedSigningTime: time.Date(2026, 10, 7, 18, 30, 15, 123456, time.FixedZone("+05", 5*3600))}) - if e != nil { - t.Fatal(e) - } - - return d, r, k, der -} - func TestCertificateEncodingConsistency(t *testing.T) { chain := testcert.New(t) for _, test := range testcert.CertificateEncodings(t, chain) { @@ -453,7 +366,11 @@ func TestBaselineSignedProperties(t *testing.T) { } } - if result.ProductionPlace().StreetAddress != "Main street" || len(result.ClaimedRoles()) != 2 || len(result.Commitments()) != 2 || len(result.CertificateReferences()) != 2 || result.SignaturePolicy().DigestMethod != xmldsig.SHA384 { + place, signaturePolicy := result.ProductionPlace(), result.SignaturePolicy() + if place == nil || signaturePolicy == nil { + t.Fatal("production place or signature policy missing") + } + if place.StreetAddress != "Main street" || len(result.ClaimedRoles()) != 2 || len(result.Commitments()) != 2 || len(result.CertificateReferences()) != 2 || signaturePolicy.DigestMethod != xmldsig.SHA384 { t.Fatal("properties missing") } if !bytes.Equal(result.CertificateReferences()[1].CertificateDER(), extra) || len(result.CertificateReferences()[1].IssuerSerialV2()) == 0 { @@ -462,9 +379,10 @@ func TestBaselineSignedProperties(t *testing.T) { // Public accessors own mutable collections and bytes; XML handles are immutable. result.CertificateReferences()[0].Digest()[0] ^= 1 result.DataObjects()[0].ObjectIdentifier.Documentation[0] = "changed" - result.SignaturePolicy().Digest[0] ^= 1 - result.SignaturePolicy().Qualifiers[1].Notice.NoticeNumbers[0] = "999" - if result.DataObjects()[0].ObjectIdentifier.Documentation[0] != "https://example.test/format" || result.SignaturePolicy().Qualifiers[1].Notice.NoticeNumbers[0] != "-1" { + signaturePolicy.Digest[0] ^= 1 + signaturePolicy.Qualifiers[1].Notice.NoticeNumbers[0] = "999" + retainedPolicy := result.SignaturePolicy() + if retainedPolicy == nil || result.DataObjects()[0].ObjectIdentifier.Documentation[0] != "https://example.test/format" || retainedPolicy.Qualifiers[1].Notice.NoticeNumbers[0] != "-1" { t.Fatal("mutable result") } for _, wrong := range []xades.BaselineVerifyOptions{{PolicyDocument: []byte("different")}, {}} { @@ -504,15 +422,6 @@ func TestBaselineIndependentSignatures(t *testing.T) { } } -func baselineDocument(t testing.TB, source string) *document.Document { - t.Helper() - doc, err := document.Parse(t.Context(), []byte(source), document.DefaultLimits()) - if err != nil { - t.Fatal(err) - } - return doc -} - func TestBaselineAuthenticatedPropertyRejects(t *testing.T) { key, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) der := certificate(t, key, 1) @@ -652,43 +561,6 @@ func TestBaselineTypedPropertyReferenceCoverage(t *testing.T) { } } -func baselineTestNode(root *tree.Node, namespace, local string) *tree.Node { - if root.Name == (document.Name{Space: namespace, Local: local}) { - return root - } - for _, child := range root.Children { - if found := baselineTestNode(child, namespace, local); found != nil { - return found - } - } - return nil -} - -func baselineResign(t *testing.T, source *document.Document, key *ecdsa.PrivateKey, mutate func(*tree.Node), validation xmldsig.SignatureOptions) (*document.Document, document.Element, xmldsig.SignatureOptions) { - t.Helper() - draft, err := tree.Clone(t.Context(), source) - if err != nil { - t.Fatal(err) - } - mutate(tree.Root(draft)) - selected := xmldsig.Signatures(draft)[0] - clearSignatureValues(selected) - raw, err := draft.XML(t.Context()) - if err != nil { - t.Fatal(err) - } - draft, err = document.Parse(t.Context(), raw, document.DefaultLimits()) - if err != nil { - t.Fatal(err) - } - validation.AdditionalIDRules = append(validation.AdditionalIDRules, reference.IDAttributeRule{Attribute: document.Name{Local: "Id"}, Elements: []document.Name{{Space: xades.Namespace, Local: signedPropertiesLocal}}}) - signed, err := xmldsig.SignTemplate(t.Context(), draft, xmldsig.Signatures(draft)[0], key, xmldsig.TemplateSignOptions{Validation: validation}) - if err != nil { - t.Fatal(err) - } - return signed, xmldsig.Signatures(signed)[0], validation -} - func TestBaselineSigningTimePrecision(t *testing.T) { key, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) der := certificate(t, key, 1) @@ -871,6 +743,9 @@ func TestBaselineCounterSignatureBinding(t *testing.T) { t.Fatal(err) } signatureValue := baselineTestNode(tree.Root(draft), xmldsig.Namespace, "SignatureValue") + if signatureValue == nil { + t.Fatal("parent signature value missing") + } signatureValue.Attrs = append(signatureValue.Attrs, tree.Attribute{Name: document.Name{Local: "Id"}, Value: "parent-value"}) qp := baselineTestNode(tree.Root(draft), xades.Namespace, "QualifyingProperties") unsigned := baselineTestAppend(qp, xades.Namespace, "UnsignedProperties", "xades") @@ -912,6 +787,9 @@ func TestBaselineCounterSignatureBinding(t *testing.T) { counterHandle := xmldsig.Signatures(changed)[1] _, counterNode := tree.Unwrap(counterHandle) si := baselineTestNode(counterNode, xmldsig.Namespace, "SignedInfo") + if si == nil { + t.Fatal("counter-signature SignedInfo missing") + } for i, a := range si.Children[2].Attrs { if a.Name.Local == "URI" { si.Children[2].Attrs[i].Value = "#signed-properties" @@ -964,44 +842,6 @@ func TestBaselineThirdPartyRoleContainers(t *testing.T) { } } -func baselineTestAppend(parent *tree.Node, namespace, local, prefix string) *tree.Node { - node := &tree.Node{Kind: tree.ElementNode, Name: document.Name{Space: namespace, Local: local}, Prefix: prefix, Parent: parent} - parent.Children = append(parent.Children, node) - return node -} - -func baselineAttributeCertificate(t *testing.T, chain testcert.Chain) []byte { - t.Helper() - algorithm := der.Seq(der.OID(asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 2})) - holderName := der.Wrap(2, 4, true, chain.Leaf.RawSubject) - holder := der.Seq(der.Wrap(2, 1, true, holderName)) - issuerName := der.Seq(der.Wrap(2, 4, true, chain.Root.RawSubject)) - issuer := der.Wrap(2, 0, true, issuerName) - before, err := asn1.MarshalWithParams(testcert.Now().Add(-time.Hour), "generalized") - if err != nil { - t.Fatal(err) - } - after, err := asn1.MarshalWithParams(testcert.Now().Add(time.Hour), "generalized") - if err != nil { - t.Fatal(err) - } - attributeValue, err := asn1.MarshalWithParams("director", "utf8") - if err != nil { - t.Fatal(err) - } - info := der.Seq(der.Int(1), holder, issuer, algorithm, der.Int(42), der.Seq(before, after), der.Seq(der.Seq(der.OID(asn1.ObjectIdentifier{1, 2, 3, 4}), der.Set(attributeValue)))) - digest := sha256.Sum256(info) - signature, err := ecdsa.SignASN1(rand.Reader, chain.RootKey, digest[:]) - if err != nil { - t.Fatal(err) - } - value, err := asn1.Marshal(asn1.BitString{Bytes: signature, BitLength: len(signature) * 8}) - if err != nil { - t.Fatal(err) - } - return der.Seq(info, algorithm, value) -} - func TestBaselineAttributeCertificateSchema(t *testing.T) { chain := testcert.New(t) value, err := der.Read(baselineAttributeCertificate(t, chain), 1<<20) @@ -1223,3 +1063,177 @@ func BenchmarkVerifyBasic(b *testing.B) { } } } + +func signingTimeTemplate(t *testing.T, d *document.Document, lexical string) *document.Document { + t.Helper() + + draft, err := tree.Clone(t.Context(), d) + if err != nil { + t.Fatal(err) + } + + clearSignatureValues(xmldsig.Signatures(draft)[0]) + changeSigningTime(tree.Root(draft), lexical) + + raw, err := draft.XML(t.Context()) + if err != nil { + t.Fatal(err) + } + + template, err := document.Parse(t.Context(), raw, document.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + + return template +} + +func clearSignatureValues(sig document.Element) { + _, value := tree.Unwrap(sig.Children()[1]) + value.Children = nil + + for _, ref := range sig.Children()[0].Children()[2:] { + fields := ref.Children() + _, digest := tree.Unwrap(fields[len(fields)-1]) + digest.Children = nil + } +} + +func changeSigningTime(n *tree.Node, value string) { + if n.Name == (document.Name{Space: xades.Namespace, Local: "SigningTime"}) { + n.Children = []*tree.Node{{Kind: tree.TextNode, Data: value, Parent: n}} + + return + } + + for _, child := range n.Children { + changeSigningTime(child, value) + } +} + +func certificate(t testing.TB, key *ecdsa.PrivateKey, serial int64) []byte { + t.Helper() + + at := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC) + c := &x509.Certificate{SerialNumber: big.NewInt(serial), Subject: pkix.Name{CommonName: "xades test"}, NotBefore: at, NotAfter: at.Add(time.Hour), KeyUsage: x509.KeyUsageDigitalSignature} + + der, e := x509.CreateCertificate(rand.Reader, c, c, &key.PublicKey, key) + if e != nil { + t.Fatal(e) + } + + return der +} + +func input(t testing.TB) *document.Document { + t.Helper() + + d, e := document.Parse(t.Context(), []byte(`123`), document.DefaultLimits()) + if e != nil { + t.Fatal(e) + } + + return d +} + +func sign(t testing.TB) (*document.Document, *xades.BasicResult, *ecdsa.PrivateKey, []byte) { + t.Helper() + + k, e := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + if e != nil { + t.Fatal(e) + } + + der := certificate(t, k, 1) + + d, r, e := xades.SignBasicEnveloped(t.Context(), input(t), k, der, xades.SignOptions{SignatureMethod: xmldsig.ECDSASHA256, ClaimedSigningTime: time.Date(2026, 10, 7, 18, 30, 15, 123456, time.FixedZone("+05", 5*3600))}) + if e != nil { + t.Fatal(e) + } + + return d, r, k, der +} + +func baselineDocument(t testing.TB, source string) *document.Document { + t.Helper() + doc, err := document.Parse(t.Context(), []byte(source), document.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + return doc +} + +func baselineTestNode(root *tree.Node, namespace, local string) *tree.Node { + if root.Name == (document.Name{Space: namespace, Local: local}) { + return root + } + for _, child := range root.Children { + if found := baselineTestNode(child, namespace, local); found != nil { + return found + } + } + return nil +} + +func baselineResign(t *testing.T, source *document.Document, key *ecdsa.PrivateKey, mutate func(*tree.Node), validation xmldsig.SignatureOptions) (*document.Document, document.Element, xmldsig.SignatureOptions) { + t.Helper() + draft, err := tree.Clone(t.Context(), source) + if err != nil { + t.Fatal(err) + } + mutate(tree.Root(draft)) + selected := xmldsig.Signatures(draft)[0] + clearSignatureValues(selected) + raw, err := draft.XML(t.Context()) + if err != nil { + t.Fatal(err) + } + draft, err = document.Parse(t.Context(), raw, document.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + validation.AdditionalIDRules = append(validation.AdditionalIDRules, reference.IDAttributeRule{Attribute: document.Name{Local: "Id"}, Elements: []document.Name{{Space: xades.Namespace, Local: signedPropertiesLocal}}}) + signed, err := xmldsig.SignTemplate(t.Context(), draft, xmldsig.Signatures(draft)[0], key, xmldsig.TemplateSignOptions{Validation: validation}) + if err != nil { + t.Fatal(err) + } + return signed, xmldsig.Signatures(signed)[0], validation +} + +func baselineTestAppend(parent *tree.Node, namespace, local, prefix string) *tree.Node { + node := &tree.Node{Kind: tree.ElementNode, Name: document.Name{Space: namespace, Local: local}, Prefix: prefix, Parent: parent} + parent.Children = append(parent.Children, node) + return node +} + +func baselineAttributeCertificate(t *testing.T, chain testcert.Chain) []byte { + t.Helper() + algorithm := der.Seq(der.OID(asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 2})) + holderName := der.Wrap(2, 4, true, chain.Leaf.RawSubject) + holder := der.Seq(der.Wrap(2, 1, true, holderName)) + issuerName := der.Seq(der.Wrap(2, 4, true, chain.Root.RawSubject)) + issuer := der.Wrap(2, 0, true, issuerName) + before, err := asn1.MarshalWithParams(testcert.Now().Add(-time.Hour), "generalized") + if err != nil { + t.Fatal(err) + } + after, err := asn1.MarshalWithParams(testcert.Now().Add(time.Hour), "generalized") + if err != nil { + t.Fatal(err) + } + attributeValue, err := asn1.MarshalWithParams("director", "utf8") + if err != nil { + t.Fatal(err) + } + info := der.Seq(der.Int(1), holder, issuer, algorithm, der.Int(42), der.Seq(before, after), der.Seq(der.Seq(der.OID(asn1.ObjectIdentifier{1, 2, 3, 4}), der.Set(attributeValue)))) + digest := sha256.Sum256(info) + signature, err := ecdsa.SignASN1(rand.Reader, chain.RootKey, digest[:]) + if err != nil { + t.Fatal(err) + } + value, err := asn1.Marshal(asn1.BitString{Bytes: signature, BitLength: len(signature) * 8}) + if err != nil { + t.Fatal(err) + } + return der.Seq(info, algorithm, value) +} diff --git a/xades/historical.go b/xades/historical.go index 0501ceb..a7ec822 100644 --- a/xades/historical.go +++ b/xades/historical.go @@ -2,17 +2,23 @@ package xades import ( "context" + "crypto/sha256" "encoding/hex" "errors" "time" "github.com/skarm/xmlsec/document" "github.com/skarm/xmlsec/internal/historical" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/pki" "github.com/skarm/xmlsec/timestamp" "github.com/skarm/xmlsec/xmldsig" ) +const ( + codeFormatFailure = "FORMAT_FAILURE" +) + // ErrHistorical reports a non-passing historical workflow. Missing evidence is // an indeterminate outcome; only demonstrated invalidity is a failed outcome. var ErrHistorical = historical.ErrValidation @@ -106,10 +112,21 @@ func (v *HistoricalValidator) Validate(ctx context.Context, doc *document.Docume if err != nil { report.state.Cause = err - report.state.SubIndication = "FORMAT_FAILURE" + report.state.SubIndication = codeFormatFailure if errors.Is(err, xmldsig.ErrDigest) || errors.Is(err, xmldsig.ErrSignature) { report.state.Indication = historical.Failed - report.state.SubIndication = "SIG_CRYPTO_FAILURE" + report.state.SubIndication = lexicon.HistoricalCodeSigCryptoFailure + } + + return nil, report, err + } + + if err := v.checkEvidenceLimits(ctx, input); err != nil { + report.state.Cause = err + if errors.Is(err, pki.ErrLimit) { + report.state.Best = at + + return nil, report, errors.Join(ErrHistorical, err) } return nil, report, err @@ -118,7 +135,7 @@ func (v *HistoricalValidator) Validate(ctx context.Context, doc *document.Docume model, evidence, err := historicalInput(ctx, baseline, v.baseline, v.workflow, input) if err != nil { report.state.Cause = err - report.state.SubIndication = "FORMAT_FAILURE" + report.state.SubIndication = codeFormatFailure return nil, report, err } @@ -133,6 +150,14 @@ func (v *HistoricalValidator) Validate(ctx context.Context, doc *document.Docume return &HistoricalResult{baseline: baseline, report: report}, report, nil } +func (v *HistoricalValidator) checkEvidenceLimits(ctx context.Context, input ValidationEvidence) error { + if err := v.workflow.Signer.CheckEvidenceLimits(ctx, input.Signer); err != nil { + return err + } + + return v.workflow.TSA.CheckEvidenceLimits(ctx, input.TSA) +} + // HistoricalReport records workflow diagnostics without granting acceptance. // After Validate returns, accessors copy their values and support concurrent use. type HistoricalReport struct{ state *historical.Report } @@ -205,7 +230,7 @@ func (r *HistoricalReport) Proofs() []HistoricalProof { out := make([]HistoricalProof, 0, len(facts)) for _, fact := range facts { source := "" - if fact.Source != ([32]byte{}) { + if fact.Source != ([sha256.Size]byte{}) { source = hex.EncodeToString(fact.Source[:]) } diff --git a/xades/historical_data_time.go b/xades/historical_data_time.go index 7b3fc9e..abed8ae 100644 --- a/xades/historical_data_time.go +++ b/xades/historical_data_time.go @@ -71,7 +71,7 @@ func appendHistoricalDataTimestamps(ctx context.Context, baseline *BaselineResul // only when canonicalizing those actual protected bytes reproduces a known // original encoding exactly. No original ancestor or unprotected namespace is // borrowed to manufacture the link. -func linkDataTimestampCanonicalEncoding(ctx context.Context, objects *historicalObjects, parent [32]byte, raw []byte, maximum int) error { +func linkDataTimestampCanonicalEncoding(ctx context.Context, objects *historicalObjects, parent [sha256.Size]byte, raw []byte, maximum int) error { doc, err := document.Parse(ctx, raw, document.DefaultLimits()) if err != nil { return ctx.Err() diff --git a/xades/historical_input.go b/xades/historical_input.go index d9ab3c6..f200156 100644 --- a/xades/historical_input.go +++ b/xades/historical_input.go @@ -27,6 +27,14 @@ import ( "github.com/skarm/xmlsec/xmldsig" ) +const ( + maximumHistoricalSignatureBytes = 1 << 20 + maximumHistoricalSignatureLexicalBytes = 1 << 20 + // Caps preservation properties examined while building or validating historical evidence. + maximumPreservationProperties = 140 + maximumPreservationTimestampChildren = 32 +) + const ( historicalSignedInfoElement = lexicon.DSigElementSignedInfo historicalSignatureValueElement = lexicon.DSigElementSignatureValue @@ -39,13 +47,13 @@ type historicalObjects struct { maximum int remaining int objects []historical.Object - index map[[32]byte]int - references [][32]byte - signedInfo [32]byte + index map[[sha256.Size]byte]int + references [][sha256.Size]byte + signedInfo [sha256.Size]byte signedInfoOptions c14n.Options } -func (b *historicalObjects) add(raw []byte, kind historicalproof.Kind, contains [][32]byte) ([32]byte, error) { +func (b *historicalObjects) add(raw []byte, kind historicalproof.Kind, contains [][sha256.Size]byte) ([sha256.Size]byte, error) { key := sha256.Sum256(raw) if previous, ok := b.index[key]; ok { object := &b.objects[previous] @@ -88,15 +96,15 @@ func (b *historicalObjects) write(write func(io.Writer) error) ([]byte, error) { return output.Bytes(), nil } -func (b *historicalObjects) canonical(e document.Element, options c14n.Options) ([32]byte, []byte, error) { +func (b *historicalObjects) canonical(e document.Element, options c14n.Options) ([sha256.Size]byte, []byte, error) { raw, err := b.write(func(dst io.Writer) error { return c14n.WriteElement(b.ctx, dst, e, options) }) if err != nil { - return [32]byte{}, nil, err + return [sha256.Size]byte{}, nil, err } contained, err := b.contained(raw) if err != nil { - return [32]byte{}, nil, err + return [sha256.Size]byte{}, nil, err } key, err := b.add(raw, historicalDataObject, contained) @@ -106,7 +114,7 @@ func (b *historicalObjects) canonical(e document.Element, options c14n.Options) // contained parses the actual canonical/processed octets, rather than assuming // that transforms retained the original XML's embedded certificate/token bytes. -func (b *historicalObjects) contained(raw []byte) ([][32]byte, error) { +func (b *historicalObjects) contained(raw []byte) ([][sha256.Size]byte, error) { if len(raw) == 0 || raw[0] != '<' { return nil, nil } @@ -120,7 +128,7 @@ func (b *historicalObjects) contained(raw []byte) ([][32]byte, error) { return nil, nil // Sparse C14N need not be a well-formed XML document. } - var out [][32]byte + var out [][sha256.Size]byte stack := []document.Element{doc.Root()} @@ -146,29 +154,29 @@ func (b *historicalObjects) contained(raw []byte) ([][32]byte, error) { return out, nil } -func (b *historicalObjects) containedNode(node document.Element) ([32]byte, bool, error) { +func (b *historicalObjects) containedNode(node document.Element) ([sha256.Size]byte, bool, error) { kind := historicalContainedKind(node) if kind == "" { - return [32]byte{}, false, nil + return [sha256.Size]byte{}, false, nil } value, err := decodeElementBase64(b.ctx, node, b.remaining*2, b.remaining) if err != nil { // Arbitrary signed data may contain lookalike invalid values. - return [32]byte{}, false, b.ctx.Err() + return [sha256.Size]byte{}, false, b.ctx.Err() } certificates, valid, err := b.containedCertificates(kind, value) if err != nil || !valid { - return [32]byte{}, false, err + return [sha256.Size]byte{}, false, err } - var children [][32]byte + var children [][sha256.Size]byte for _, raw := range certificates { key, err := b.add(raw, historicalproof.Certificate, nil) if err != nil { - return [32]byte{}, false, err + return [sha256.Size]byte{}, false, err } children = append(children, key) @@ -213,7 +221,7 @@ func (b *historicalObjects) containedCertificates(kind historicalproof.Kind, val // A timestamped serialization also proves an exact deterministic encoding // obtainable from those protected bytes alone. Ancestor context is never // borrowed from the original XML when checking this relation. -func (b *historicalObjects) linkCanonicalEncoding(parent, original [32]byte, options c14n.Options) error { +func (b *historicalObjects) linkCanonicalEncoding(parent, original [sha256.Size]byte, options c14n.Options) error { if parent == original { return nil } @@ -251,13 +259,13 @@ func historicalContainedKind(node document.Element) historicalproof.Kind { switch node.Name() { case ds(historicalSignatureValueElement): return historicalproof.Signature - case ds(lexicon.DSigElementX509Certificate), name("EncapsulatedX509Certificate"): + case ds(lexicon.DSigElementX509Certificate), name(nameEncapsulatedX509Certificate): return historicalproof.Certificate - case name("EncapsulatedCRLValue"): + case name(nameEncapsulatedCRLValue): return historicalproof.CRL - case name("EncapsulatedOCSPValue"): + case name(nameEncapsulatedOCSPValue): return historicalproof.OCSP - case name("EncapsulatedTimeStamp"): + case name(nameEncapsulatedTimeStamp): return historicalproof.Timestamp } @@ -286,7 +294,7 @@ func (b *historicalInputBuilder) initializeSignature() error { b.value = value - signatureValue, err := decodeElementBase64(b.ctx, value, 1<<20, 1<<20) + signatureValue, err := decodeElementBase64(b.ctx, value, maximumHistoricalSignatureLexicalBytes, maximumHistoricalSignatureBytes) if err != nil { return err } @@ -405,7 +413,11 @@ func (b *historicalInputBuilder) recordPolicy() error { func (b *historicalInputBuilder) prepareUnsigned() error { for _, child := range b.baseline.UnsignedProperties().Children() { - if child.Name() == name("UnsignedSignatureProperties") { + if child.Name() == name(nameUnsignedDataObjectProperties) { + return fmt.Errorf("%w: unsupported historical unsigned data properties", ErrProfile) + } + + if child.Name() == name(nameUnsignedSignatureProperties) { if b.unsigned.Valid() { return ErrProfile } @@ -415,7 +427,7 @@ func (b *historicalInputBuilder) prepareUnsigned() error { } b.entries = b.baseline.UnsignedSignatureProperties() - if len(b.entries) > 140 { + if len(b.entries) > maximumPreservationProperties { return historical.ErrLimit } @@ -431,13 +443,13 @@ func (b *historicalInputBuilder) recordUnsignedMaterial() error { var err error switch entry.Name() { - case name("CertificateValues"): + case name(nameCertificateValues): material.Certificates, err = parseCertificateValues(b.ctx, entry) - case name("RevocationValues"): + case name(nameRevocationValues): material.CRLs, material.OCSPResponses, err = parseRevocationValues(b.ctx, entry) - case name141("TimeStampValidationData"): + case name141(nameTimeStampValidationData): material, err = parseTSVD(b.ctx, entry) - case name("SignatureTimeStamp"), name141("ArchiveTimeStamp"): + case name(nameSignatureTimeStamp), name141(nameArchiveTimeStamp): continue default: return fmt.Errorf("%w: unsupported historical unsigned property %s", ErrProfile, entry.Name().Local) @@ -468,13 +480,13 @@ func (b *historicalInputBuilder) recordCertificateReferences() error { return err } - hash, err := historicalHash(reference.DigestMethod()) + h, err := historicalHash(reference.DigestMethod()) if err != nil { return err } - b.result.Hashes = append(b.result.Hashes, hash) - b.result.Uses = append(b.result.Uses, historical.MaterialUse{Object: key, Hash: hash}) + b.result.Hashes = append(b.result.Hashes, h) + b.result.Uses = append(b.result.Uses, historical.MaterialUse{Object: key, Hash: h}) } return nil @@ -482,7 +494,7 @@ func (b *historicalInputBuilder) recordCertificateReferences() error { func (b *historicalInputBuilder) recordUnsignedTimestamps() error { for _, entry := range b.entries { - if entry.Name() != name("SignatureTimeStamp") && entry.Name() != name141("ArchiveTimeStamp") { + if entry.Name() != name(nameSignatureTimeStamp) && entry.Name() != name141(nameArchiveTimeStamp) { continue } @@ -511,7 +523,7 @@ func (b *historicalInputBuilder) recordTimestamp(entry document.Element, method return err } - candidate := historical.Candidate{Token: token, Archive: entry.Name() == name141("ArchiveTimeStamp")} + candidate := historical.Candidate{Token: token, Archive: entry.Name() == name141(nameArchiveTimeStamp)} if candidate.Archive { candidate.Imprint, candidate.Covered, err = historicalArchiveInput(b.ctx, b.objects, b.baseline, b.unsigned, entry, method, token.UnverifiedInfo().Imprint().Hash) } else { @@ -527,7 +539,7 @@ func (b *historicalInputBuilder) recordTimestamp(entry document.Element, method return nil } -func (b *historicalInputBuilder) signatureTimestampInput(method string, hash crypto.Hash) (timestamp.Imprint, [][32]byte, error) { +func (b *historicalInputBuilder) signatureTimestampInput(method string, hash crypto.Hash) (timestamp.Imprint, [][sha256.Size]byte, error) { key, canonical, err := b.objects.canonical(b.value, c14n.Options{Algorithm: method, MaxOutputBytes: b.config.MaxBytes}) if err != nil { return timestamp.Imprint{}, nil, err @@ -536,7 +548,7 @@ func (b *historicalInputBuilder) signatureTimestampInput(method string, hash cry state := hash.New() _, _ = state.Write(canonical) - return timestamp.Imprint{Hash: hash, Digest: state.Sum(nil)}, [][32]byte{key}, nil + return timestamp.Imprint{Hash: hash, Digest: state.Sum(nil)}, [][sha256.Size]byte{key}, nil } func historicalInput(ctx context.Context, baseline *BaselineResult, baselineOptions BaselineVerifyOptions, config historical.Config, input ValidationEvidence) (historical.Input, ValidationEvidence, error) { @@ -549,7 +561,7 @@ func historicalInput(ctx context.Context, baseline *BaselineResult, baselineOpti builder := historicalInputBuilder{ ctx: ctx, baseline: baseline, options: baselineOptions, config: config, evidence: input, - objects: &historicalObjects{ctx: ctx, maximum: config.MaxObjects, remaining: config.MaxBytes, index: make(map[[32]byte]int)}, + objects: &historicalObjects{ctx: ctx, maximum: config.MaxObjects, remaining: config.MaxBytes, index: make(map[[sha256.Size]byte]int)}, sig: baseline.Basic().Signature().Signature(), } if err := builder.initializeSignature(); err != nil { @@ -603,13 +615,13 @@ func historicalSignedInfoOptions(element document.Element, maximum int) (c14n.Op return c14n.Options{}, err } - options := c14n.Options{Algorithm: localAttribute(cm, "Algorithm"), MaxOutputBytes: maximum} + options := c14n.Options{Algorithm: localAttribute(cm, lexicon.SecurityAttrAlgorithm), MaxOutputBytes: maximum} for _, parameter := range cm.Children() { - if parameter.Name() != (document.Name{Space: c14n.Exclusive, Local: "InclusiveNamespaces"}) { + if parameter.Name() != (document.Name{Space: c14n.Exclusive, Local: lexicon.C14NElementInclusiveNamespaces}) { return options, ErrProfile } - options.InclusivePrefixes = strings.Fields(localAttribute(parameter, "PrefixList")) + options.InclusivePrefixes = strings.Fields(localAttribute(parameter, lexicon.C14NAttrPrefixList)) } return options, nil @@ -629,7 +641,7 @@ type historicalArchiveBuilder struct { baseline *BaselineResult method string state hash.Hash - covered [][32]byte + covered [][sha256.Size]byte } func (b *historicalArchiveBuilder) references() error { @@ -681,7 +693,7 @@ func (b *historicalArchiveBuilder) reference(reference xmldsig.ReferenceResult, return nil } -func (b *historicalArchiveBuilder) linkCertificateReferences(key [32]byte) error { +func (b *historicalArchiveBuilder) linkCertificateReferences(key [sha256.Size]byte) error { for _, certificate := range b.baseline.CertificateReferences() { digest, err := historicalHash(certificate.DigestMethod()) if err != nil { @@ -715,7 +727,7 @@ func (b *historicalArchiveBuilder) signatureElements() error { return err } - if local == historicalSignedInfoElement && b.objects.signedInfo != ([32]byte{}) { + if local == historicalSignedInfoElement && b.objects.signedInfo != ([sha256.Size]byte{}) { if err := b.objects.linkCanonicalEncoding(key, b.objects.signedInfo, b.objects.signedInfoOptions); err != nil { return err } @@ -758,7 +770,7 @@ func (b *historicalArchiveBuilder) otherObjects() error { // EN319132-1 §5.5.2.3(6) covers ALL other ds:Object elements, including // referenced Objects, in document order. The property-bearing Object is excluded. for _, object := range b.baseline.Basic().Signature().Signature().Children() { - if object.Name() != ds("Object") || historicalObjectHasProperties(object) { + if object.Name() != ds(lexicon.DSigElementObject) || historicalObjectHasProperties(object) { continue } @@ -770,7 +782,7 @@ func (b *historicalArchiveBuilder) otherObjects() error { return nil } -func historicalArchiveInput(ctx context.Context, objects *historicalObjects, baseline *BaselineResult, unsigned, before document.Element, method string, hash crypto.Hash) (timestamp.Imprint, [][32]byte, error) { +func historicalArchiveInput(ctx context.Context, objects *historicalObjects, baseline *BaselineResult, unsigned, before document.Element, method string, hash crypto.Hash) (timestamp.Imprint, [][sha256.Size]byte, error) { if hash == 0 || !hash.Available() { return timestamp.Imprint{}, nil, ErrTimestamp } @@ -817,7 +829,7 @@ func historicalArchiveElement(signature document.Element, local string) (documen func historicalObjectHasProperties(object document.Element) bool { for _, child := range object.Children() { - if child.Name() == name("QualifyingProperties") { + if child.Name() == name(nameQualifyingProperties) { return true } } @@ -838,27 +850,27 @@ func parseHistoricalTimeStamp(ctx context.Context, element document.Element, max method := c14n.Canonical10 if len(children) > 0 && children[0].Name() == ds(lexicon.DSigElementCanonicalizationMethod) { - if attrs(children[0], "Algorithm") != nil || len(children[0].Children()) != 0 || !xmlSpace(children[0].Text()) { + if attrs(children[0], lexicon.SecurityAttrAlgorithm) != nil || len(children[0].Children()) != 0 || !xmlSpace(children[0].Text()) { return "", nil, ErrTimestamp } - method = localAttribute(children[0], "Algorithm") + method = localAttribute(children[0], lexicon.SecurityAttrAlgorithm) children = children[1:] } - if !timestampCanonicalization(method) || len(children) == 0 || len(children) > 32 { + if !timestampCanonicalization(method) || len(children) == 0 || len(children) > maximumPreservationTimestampChildren { return "", nil, ErrTimestamp } // EN319132-1 6.3(n) restricts SignatureTimeStamp to one token. // ArchiveTimeStamp141 may contain tokens from multiple TSAs (6.3(z)). - if element.Name() == name("SignatureTimeStamp") && len(children) != 1 { + if element.Name() == name(nameSignatureTimeStamp) && len(children) != 1 { return "", nil, ErrProfile } var tokens [][]byte for _, child := range children { - if child.Name() != name("EncapsulatedTimeStamp") { + if child.Name() != name(nameEncapsulatedTimeStamp) { return "", nil, ErrTimestamp } @@ -898,14 +910,14 @@ func historicalAllReferences(basic *BasicResult) []xmldsig.ReferenceResult { } func historicalSignatureMethod(basic *BasicResult) (x509.SignatureAlgorithm, []crypto.Hash, error) { - algorithm, hash, err := historicalSignatureAlgorithm(basic) + algorithm, h, err := historicalSignatureAlgorithm(basic) if err != nil { return 0, nil, err } var hashes []crypto.Hash - if hash != 0 { - hashes = append(hashes, hash) + if h != 0 { + hashes = append(hashes, h) } for _, reference := range historicalAllReferences(basic) { diff --git a/xades/material.go b/xades/material.go index a8c904c..3b09891 100644 --- a/xades/material.go +++ b/xades/material.go @@ -10,6 +10,7 @@ import ( "github.com/skarm/xmlsec/document" "github.com/skarm/xmlsec/internal/der" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/tree" "github.com/skarm/xmlsec/internal/x509cert" "github.com/skarm/xmlsec/internal/x509crl" @@ -17,6 +18,18 @@ import ( "github.com/skarm/xmlsec/xmldsig" ) +const ( + nameCRLValues = "CRLValues" + nameOCSPValues = "OCSPValues" + maximumMaterialBytes = 16 << 20 + maximumMaterialCRLs = 64 + maximumMaterialCertificates = 64 + maximumMaterialGroupEntries = 64 + maximumMaterialLexicalBytes = 6 << 20 + maximumMaterialOCSPResponses = 64 + maximumMaterialValueBytes = 4 << 20 +) + // Namespace141 is the XAdES 1.4.1 namespace URI. const Namespace141 = "http://uri.etsi.org/01903/v1.4.1#" @@ -90,7 +103,7 @@ func (v *Validator) EmbedValidationMaterial(ctx context.Context, doc *document.D for _, n := range original { un.Children = append(un.Children, n) - if n.Kind != tree.ElementNode || n.Name != name("SignatureTimeStamp") { + if n.Kind != tree.ElementNode || n.Name != name(nameSignatureTimeStamp) { continue } @@ -108,7 +121,7 @@ func (v *Validator) EmbedValidationMaterial(ctx context.Context, doc *document.D return nil, nil, ErrLevel } - td := node141("TimeStampValidationData") + td := node141(nameTimeStampValidationData) if err := appendValidationMaterial(ctx, td, tm); err != nil { return nil, nil, err } @@ -141,11 +154,11 @@ func (v *Validator) canEmbedMaterial(ctx context.Context) bool { } func node141(local string) *tree.Node { - return &tree.Node{Kind: tree.ElementNode, Name: name141(local), Prefix: "xades141", NS: []tree.Namespace{{Prefix: "xades141", URI: Namespace141}}} + return &tree.Node{Kind: tree.ElementNode, Name: name141(local), Prefix: prefixXAdES141, NS: []tree.Namespace{{Prefix: prefixXAdES141, URI: Namespace141}}} } func knownPreservation(n document.Name) bool { - return n == name("CertificateValues") || n == name("RevocationValues") || n == name141("TimeStampValidationData") || n == name141("ArchiveTimeStamp") + return n == name(nameCertificateValues) || n == name(nameRevocationValues) || n == name141(nameTimeStampValidationData) || n == name141(nameArchiveTimeStamp) } func hasPreservation(sig document.Element) (bool, error) { @@ -168,8 +181,8 @@ func makeCertificateValues(ctx context.Context, entries [][]byte) (*tree.Node, e return nil, nil } - n := xnode("CertificateValues") - n.NS = []tree.Namespace{{Prefix: nameXades, URI: Namespace}} + n := xnode(nameCertificateValues) + n.NS = []tree.Namespace{{Prefix: prefixXAdES, URI: Namespace}} for _, b := range entries { encoded, err := encodeBase64(ctx, len(b), bytes.NewBuffer(b)) @@ -177,7 +190,7 @@ func makeCertificateValues(ctx context.Context, entries [][]byte) (*tree.Node, e return nil, err } - c := xnode("EncapsulatedX509Certificate") + c := xnode(nameEncapsulatedX509Certificate) text(c, encoded) add(n, c) } @@ -190,17 +203,17 @@ func makeRevocationValues(ctx context.Context, e pki.Evidence) (*tree.Node, erro return nil, nil } - n := xnode("RevocationValues") - n.NS = []tree.Namespace{{Prefix: nameXades, URI: Namespace}} + n := xnode(nameRevocationValues) + n.NS = []tree.Namespace{{Prefix: prefixXAdES, URI: Namespace}} for i, entries := range [][][]byte{e.CRLs, e.OCSPResponses} { if len(entries) == 0 { continue } - group, local := "CRLValues", "EncapsulatedCRLValue" + group, local := nameCRLValues, nameEncapsulatedCRLValue if i == 1 { - group, local = "OCSPValues", "EncapsulatedOCSPValue" + group, local = nameOCSPValues, nameEncapsulatedOCSPValue } g := xnode(group) @@ -308,7 +321,7 @@ func subtractDER(in [][]byte, omit ...[]byte) [][]byte { } func checkRawMaterial(ctx context.Context, m ValidationMaterial) error { - if len(m.Certificates) > 64 || len(m.CRLs) > 64 || len(m.OCSPResponses) > 64 { + if len(m.Certificates) > maximumMaterialCertificates || len(m.CRLs) > maximumMaterialCRLs || len(m.OCSPResponses) > maximumMaterialOCSPResponses { return ErrProfile } @@ -323,7 +336,7 @@ func checkRawMaterial(ctx context.Context, m ValidationMaterial) error { } total += len(b) - if len(b) == 0 || len(b) > 4<<20 || total > 16<<20 { + if len(b) == 0 || len(b) > maximumMaterialValueBytes || total > maximumMaterialBytes { return ErrProfile } @@ -366,7 +379,7 @@ func parsePreservation(ctx context.Context, sig document.Element, maxArchives in } entries := u.Children() - if len(entries) > 140 { + if len(entries) > maximumPreservationProperties { return p, ErrProfile } @@ -395,20 +408,28 @@ func parsePreservation(ctx context.Context, sig document.Element, maxArchives in } func validateEmbeddedOCSP(b []byte) error { + // A successful OCSPResponse carries status and responseBytes; the latter + // contains a response-type OID and its encoded response octets. + const ( + ocspResponseFields = 2 + ocspResponseBytesFields = 2 + ocspSuccessfulStatus = 0 + ) + // XAdES uses the entire DER OCSPResponse (ETSI 5.4.3), unlike // CMS revocation formats that sometimes carry BasicOCSPResponse. - v, e := der.Read(b, 4<<20) + v, e := der.Read(b, maximumMaterialValueBytes) if e != nil { return ErrProfile } - f, e := der.Sequence(v, 2, 2) + f, e := der.Sequence(v, ocspResponseFields, ocspResponseFields) if e != nil { return ErrProfile } var status asn1.Enumerated - if der.Decode(f[0], &status) != nil || status != 0 { + if der.Decode(f[0], &status) != nil || status != ocspSuccessfulStatus { return ErrProfile } @@ -417,16 +438,16 @@ func validateEmbeddedOCSP(b []byte) error { return ErrProfile } - parts, e := der.Sequence(body, 2, 2) + parts, e := der.Sequence(body, ocspResponseBytesFields, ocspResponseBytesFields) if e != nil || !der.OIDEqual(parts[0], asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 48, 1, 1}) { return ErrProfile } - if !der.Is(parts[1], 0, asn1.TagOctetString, false) { + if !der.Is(parts[1], asn1.ClassUniversal, asn1.TagOctetString, false) { return ErrProfile } - if _, e = der.Read(parts[1].Bytes, 4<<20); e != nil { + if _, e = der.Read(parts[1].Bytes, maximumMaterialValueBytes); e != nil { return ErrProfile } @@ -445,6 +466,15 @@ type preservationParser struct { } func (r *preservationParser) accept(x document.Element) error { + // Ordered unsigned-property phases permit timestamps before validation + // material, then certificates, revocation values and finally archives. + const ( + preservationPhaseTimestamps = 0 + preservationPhaseCertificates = 1 + preservationPhaseRevocation = 2 + preservationPhaseArchives = 3 + ) + if err := r.ctx.Err(); err != nil { return err } @@ -452,14 +482,14 @@ func (r *preservationParser) accept(x document.Element) error { var e error switch x.Name() { - case name("SignatureTimeStamp"): - if r.phase != 0 { + case name(nameSignatureTimeStamp): + if r.phase != preservationPhaseTimestamps { return ErrProfile } r.haveTimestamp = true - case name141("TimeStampValidationData"): - if r.previous != name("SignatureTimeStamp") && r.previous != name141("ArchiveTimeStamp") { + case name141(nameTimeStampValidationData): + if r.previous != name(nameSignatureTimeStamp) && r.previous != name141(nameArchiveTimeStamp) { return ErrProfile } @@ -471,36 +501,36 @@ func (r *preservationParser) accept(x document.Element) error { r.value.tsa.Certificates = appendUnique(r.value.tsa.Certificates, m.Certificates...) r.value.tsa.CRLs = appendUnique(r.value.tsa.CRLs, m.CRLs...) r.value.tsa.OCSPResponses = appendUnique(r.value.tsa.OCSPResponses, m.OCSPResponses...) - case name("CertificateValues"): - if r.haveCV || r.phase > 1 { + case name(nameCertificateValues): + if r.haveCV || r.phase > preservationPhaseCertificates { return ErrProfile } - r.phase = 1 + r.phase = preservationPhaseCertificates r.haveCV = true r.value.signer.Certificates, e = parseCertificateValues(r.ctx, x) if e != nil { return e } - case name("RevocationValues"): - if r.haveRV || r.phase > 2 { + case name(nameRevocationValues): + if r.haveRV || r.phase > preservationPhaseRevocation { return ErrProfile } - r.phase = 2 + r.phase = preservationPhaseRevocation r.haveRV = true r.value.signer.CRLs, r.value.signer.OCSPResponses, e = parseRevocationValues(r.ctx, x) if e != nil { return e } - case name141("ArchiveTimeStamp"): + case name141(nameArchiveTimeStamp): if !r.haveRV || !r.haveCV || len(r.value.archives) >= r.maxArchives { return ErrProfile } - r.phase = 3 + r.phase = preservationPhaseArchives r.value.archives = append(r.value.archives, x) default: @@ -518,29 +548,29 @@ func encapsulatedBytes(ctx context.Context, e document.Element) ([]byte, error) } for _, a := range e.Attributes() { - if a.Name == (document.Name{Local: "Id"}) && tree.NCName(a.Value) { + if a.Name == (document.Name{Local: lexicon.SecurityAttrID}) && tree.NCName(a.Value) { continue } - if a.Name == (document.Name{Local: "Encoding"}) && a.Value == encodingDER { + if a.Name == (document.Name{Local: lexicon.SecurityAttrEncoding}) && a.Value == encodingDER { continue } return nil, ErrProfile } - return decodeElementBase64(ctx, e, 6<<20, 4<<20) + return decodeElementBase64(ctx, e, maximumMaterialLexicalBytes, maximumMaterialValueBytes) } func parseCertificateValues(ctx context.Context, e document.Element) ([][]byte, error) { - if elementContent(e) != nil || e.Name() != name("CertificateValues") || optionalID(e) != nil || len(e.Children()) < 1 || len(e.Children()) > 64 { + if elementContent(e) != nil || e.Name() != name(nameCertificateValues) || optionalID(e) != nil || len(e.Children()) < 1 || len(e.Children()) > maximumMaterialCertificates { return nil, ErrProfile } var out [][]byte for _, c := range e.Children() { - if c.Name() != name("EncapsulatedX509Certificate") { + if c.Name() != name(nameEncapsulatedX509Certificate) { return nil, ErrProfile } @@ -556,7 +586,7 @@ func parseCertificateValues(ctx context.Context, e document.Element) ([][]byte, } func parseRevocationValues(ctx context.Context, e document.Element) ([][]byte, [][]byte, error) { - if elementContent(e) != nil || e.Name() != name("RevocationValues") || optionalID(e) != nil { + if elementContent(e) != nil || e.Name() != name(nameRevocationValues) || optionalID(e) != nil { return nil, nil, ErrProfile } @@ -575,17 +605,17 @@ func parseRevocationValues(ctx context.Context, e document.Element) ([][]byte, [ local := "" switch g.Name() { - case name("CRLValues"): + case name(nameCRLValues): kind = 0 - local = "EncapsulatedCRLValue" - case name("OCSPValues"): + local = nameEncapsulatedCRLValue + case name(nameOCSPValues): kind = 1 - local = "EncapsulatedOCSPValue" + local = nameEncapsulatedOCSPValue default: return nil, nil, ErrProfile } - if kind <= last || attrs(g) != nil || len(g.Children()) < 1 || len(g.Children()) > 64 { + if kind <= last || attrs(g) != nil || elementContent(g) != nil || len(g.Children()) < 1 || len(g.Children()) > maximumMaterialGroupEntries { return nil, nil, ErrProfile } @@ -615,7 +645,7 @@ func parseRevocationValues(ctx context.Context, e document.Element) ([][]byte, [ func parseTSVD(ctx context.Context, e document.Element) (ValidationMaterial, error) { var m ValidationMaterial // Direct incorporation immediately after its timestamp: URI is not used. - if elementContent(e) != nil || e.Name() != name141("TimeStampValidationData") || optionalID(e) != nil || len(e.Children()) < 1 || len(e.Children()) > 2 { + if elementContent(e) != nil || e.Name() != name141(nameTimeStampValidationData) || optionalID(e) != nil || len(e.Children()) < 1 || len(e.Children()) > 2 { return m, ErrProfile } @@ -623,7 +653,7 @@ func parseTSVD(ctx context.Context, e document.Element) (ValidationMaterial, err for _, c := range e.Children() { switch c.Name() { - case name("CertificateValues"): + case name(nameCertificateValues): if last >= 0 { return m, ErrProfile } @@ -636,7 +666,7 @@ func parseTSVD(ctx context.Context, e document.Element) (ValidationMaterial, err } m.Certificates = a - case name("RevocationValues"): + case name(nameRevocationValues): if last >= 1 { return m, ErrProfile } @@ -684,7 +714,7 @@ func (c *timestampMaterialCursor) certificates(ctx context.Context, children []d var tsaCerts [][]byte for _, enc := range children { - if enc.Name() != name("EncapsulatedTimeStamp") { + if enc.Name() != name(nameEncapsulatedTimeStamp) { continue } diff --git a/xades/proof.go b/xades/proof.go index 744548e..758265e 100644 --- a/xades/proof.go +++ b/xades/proof.go @@ -13,6 +13,12 @@ import ( "time" ) +const ( + indicationFailed = "failed" + indicationIndeterminate = "indeterminate" + indicationPassed = "passed" +) + // Indication describes the outcome within the report's named profile. The zero // value is Indeterminate and must never be treated as an acceptable signature. type Indication uint8 @@ -30,11 +36,11 @@ const ( func (i Indication) String() string { switch i { case Passed: - return "passed" + return indicationPassed case Failed: - return "failed" + return indicationFailed default: - return "indeterminate" + return indicationIndeterminate } } diff --git a/xades/shared_constants.go b/xades/shared_constants.go index f76c568..1c7bf60 100644 --- a/xades/shared_constants.go +++ b/xades/shared_constants.go @@ -1,5 +1,93 @@ package xades const ( - nameXades = "xades" + // Unqualified attribute that binds signed format metadata to an exact ds:Reference. + attrObjectReference = "ObjectReference" + // Timestamp property covering all selected signed data objects. Match the applicable XAdES + // namespace at use sites. + nameAllDataObjectsTimeStamp = "AllDataObjectsTimeStamp" + // Archive timestamp property used by preservation and historical validation. Match the applicable + // XAdES namespace at use sites. + nameArchiveTimeStamp = "ArchiveTimeStamp" + // One digest-qualified signing-certificate reference inside SigningCertificateV2. Match the + // applicable XAdES namespace at use sites. + nameCert = "Cert" + // Digest binding for a certificate selected by an XAdES property. Match the applicable XAdES + // namespace at use sites. + nameCertDigest = "CertDigest" + // Certificate evidence container; trust is established by subsequent certificate validation. Match + // the applicable XAdES namespace at use sites. + nameCertificateValues = "CertificateValues" + // Signed commitment claim associated with referenced data objects. Match the applicable XAdES + // namespace at use sites. + nameCommitmentTypeIndication = "CommitmentTypeIndication" + // Unsigned property containing a separate signature over the owning SignatureValue. Match the + // applicable XAdES namespace at use sites. + nameCounterSignature = "CounterSignature" + // Signed format metadata linked to a Reference by ObjectReference. Match the applicable XAdES + // namespace at use sites. + nameDataObjectFormat = "DataObjectFormat" + // Original CRL bytes carried inside RevocationValues. Match the applicable XAdES namespace at use + // sites. + nameEncapsulatedCRLValue = "EncapsulatedCRLValue" + // Original OCSP response bytes carried inside RevocationValues. Match the applicable XAdES + // namespace at use sites. + nameEncapsulatedOCSPValue = "EncapsulatedOCSPValue" + // Encoded RFC 3161 token carried by an XAdES timestamp property. Match the applicable XAdES + // namespace at use sites. + nameEncapsulatedTimeStamp = "EncapsulatedTimeStamp" + // Original DER certificate bytes carried by CertificateValues. Match the applicable XAdES + // namespace at use sites. + nameEncapsulatedX509Certificate = "EncapsulatedX509Certificate" + // Timestamp property covering explicitly listed signed data-object references. Match the + // applicable XAdES namespace at use sites. + nameIndividualDataObjectsTimeStamp = "IndividualDataObjectsTimeStamp" + // Root XAdES property container bound to the selected XML signature. Match the applicable XAdES + // namespace at use sites. + nameQualifyingProperties = "QualifyingProperties" + // Container for original CRL and OCSP evidence encodings. Match the applicable XAdES namespace at + // use sites. + nameRevocationValues = "RevocationValues" + // Signed choice of an explicit or implied signature policy. Match the applicable XAdES namespace + // at use sites. + nameSignaturePolicyIdentifier = "SignaturePolicyIdentifier" + // Timestamp property covering the owning ds:SignatureValue. Match the applicable XAdES namespace + // at use sites. + nameSignatureTimeStamp = "SignatureTimeStamp" + // Authenticated data-object property container under SignedProperties. Match the applicable XAdES + // namespace at use sites. + nameSignedDataObjectProperties = "SignedDataObjectProperties" + // Properties node covered by an explicit SignedInfo reference. Match the applicable XAdES + // namespace at use sites. + nameSignedProperties = "SignedProperties" + // Authenticated properties about the signature and signing identity. Match the applicable XAdES + // namespace at use sites. + nameSignedSignatureProperties = "SignedSignatureProperties" + // Signed role claims whose authorization meaning is selected by application policy. Match the + // applicable XAdES namespace at use sites. + nameSignerRoleV2 = "SignerRoleV2" + // Digest-qualified signing-certificate binding for the selected signature. Match the applicable + // XAdES namespace at use sites. + nameSigningCertificateV2 = "SigningCertificateV2" + // Claimed signing time; trusted time comes from timestamp verification. Match the applicable XAdES + // namespace at use sites. + nameSigningTime = "SigningTime" + // Evidence association for a timestamp in the XAdES 1.4.1 namespace. Match the applicable XAdES + // namespace at use sites. + nameTimeStampValidationData = "TimeStampValidationData" + // Unsigned properties associated with signed data objects. Match the applicable XAdES namespace at + // use sites. + nameUnsignedDataObjectProperties = "UnsignedDataObjectProperties" + // Root container for timestamp and preservation properties. Match the applicable XAdES namespace + // at use sites. + nameUnsignedProperties = "UnsignedProperties" + // Unsigned properties attached to the selected signature. Match the applicable XAdES namespace at + // use sites. + nameUnsignedSignatureProperties = "UnsignedSignatureProperties" +) + +const ( + // Suggested prefix for generated qualifying properties; the XAdES namespace URI determines + // identity. + prefixXAdES = "xades" ) diff --git a/xades/timestamp.go b/xades/timestamp.go index 6cea551..192997e 100644 --- a/xades/timestamp.go +++ b/xades/timestamp.go @@ -20,6 +20,20 @@ import ( "github.com/skarm/xmlsec/xmldsig" ) +const ( + // Token byte cap selected by default in signature and data-object timestamp verification. + defaultTimestampTokenBytes = 4 << 20 + // Hard ceiling on a caller-selected timestamp token byte limit. + maximumTimestampTokenBytes = 16 << 20 +) + +const ( + defaultSignatureTimestamps = 8 + maximumEncapsulatedTimestampChildren = 33 + maximumSignatureCanonicalBytes = 1 << 20 + maximumSignatureTimestamps = 32 +) + const ( encodingDER = "http://uri.etsi.org/01903/v1.2.2#DER" encodingBER = "http://uri.etsi.org/01903/v1.2.2#BER" @@ -135,7 +149,7 @@ func AddSignatureTimestamp(ctx context.Context, doc *document.Document, selected method = c14n.Exclusive } - imprint, e := canonicalImprint(ctx, selected, method, 1<<20, o.Request.Hash) + imprint, e := canonicalImprint(ctx, selected, method, maximumSignatureCanonicalBytes, o.Request.Hash) if e != nil { return nil, nil, e } @@ -193,7 +207,7 @@ func AttachSignatureTimestamp(ctx context.Context, doc *document.Document, selec return nil, nil, e } - imprint, e := canonicalImprint(ctx, selected, method, 1<<20, token.UnverifiedInfo().Imprint().Hash) + imprint, e := canonicalImprint(ctx, selected, method, maximumSignatureCanonicalBytes, token.UnverifiedInfo().Imprint().Hash) if e != nil { return nil, nil, e } @@ -216,14 +230,14 @@ func normalizeTimed(o TimedOptions) (TimedOptions, error) { func normalizeTimedLimits(o TimedOptions) (TimedOptions, error) { if o.MaxTimestamps == 0 { - o.MaxTimestamps = 8 + o.MaxTimestamps = defaultSignatureTimestamps } if o.MaxTokenBytes == 0 { - o.MaxTokenBytes = 4 << 20 + o.MaxTokenBytes = defaultTimestampTokenBytes } - if o.MaxTimestamps < 1 || o.MaxTimestamps > 32 || o.MaxTokenBytes < 1 || o.MaxTokenBytes > 16<<20 { + if o.MaxTimestamps < 1 || o.MaxTimestamps > maximumSignatureTimestamps || o.MaxTokenBytes < 1 || o.MaxTokenBytes > maximumTimestampTokenBytes { return o, ErrTimestamp } @@ -231,12 +245,12 @@ func normalizeTimedLimits(o TimedOptions) (TimedOptions, error) { } func qualifying(sig document.Element) (document.Element, error) { - o, e := childNamed(sig, ds("Object")) + o, e := childNamed(sig, ds(lexicon.DSigElementObject)) if e != nil { return document.Element{}, e } - return childNamed(o, name("QualifyingProperties")) + return childNamed(o, name(nameQualifyingProperties)) } func unsignedProperties(sig document.Element) (document.Element, error) { @@ -250,11 +264,11 @@ func unsignedProperties(sig document.Element) (document.Element, error) { return document.Element{}, nil } - if len(cs) != 2 || cs[1].Name() != name("UnsignedProperties") || optionalID(cs[1]) != nil { + if len(cs) != 2 || cs[1].Name() != name(nameUnsignedProperties) || optionalID(cs[1]) != nil { return document.Element{}, ErrProfile } - f, e := children(cs[1], name("UnsignedSignatureProperties")) + f, e := children(cs[1], name(nameUnsignedSignatureProperties)) if e != nil || optionalID(f[0]) != nil { return document.Element{}, ErrProfile } @@ -268,7 +282,7 @@ func optionalID(e document.Element) error { return nil } - if len(a) != 1 || a[0].Name != (document.Name{Local: "Id"}) || !tree.NCName(a[0].Value) { + if len(a) != 1 || a[0].Name != (document.Name{Local: lexicon.SecurityAttrID}) || !tree.NCName(a[0].Value) { return ErrProfile } @@ -323,16 +337,16 @@ func canonicalImprint(ctx context.Context, sig document.Element, method string, // algorithms, as required when generating/augmenting current EN 319 132-1 data. // XMLTimeStamp, Include, transform parameters and unknown encodings fail closed. func parseTimeStamp(ctx context.Context, e document.Element, maximum int) (string, [][]byte, error) { - if optionalID(e) != nil { + if optionalID(e) != nil || elementContent(e) != nil { return "", nil, ErrProfile } cs := e.Children() - if len(cs) < 2 || len(cs) > 33 || cs[0].Name() != ds(lexicon.DSigElementCanonicalizationMethod) || attrs(cs[0], "Algorithm") != nil || len(cs[0].Children()) != 0 || !xmlSpace(cs[0].Text()) { + if len(cs) < 2 || len(cs) > maximumEncapsulatedTimestampChildren || cs[0].Name() != ds(lexicon.DSigElementCanonicalizationMethod) || attrs(cs[0], lexicon.SecurityAttrAlgorithm) != nil || len(cs[0].Children()) != 0 || !xmlSpace(cs[0].Text()) { return "", nil, ErrTimestamp } - method := localAttribute(cs[0], "Algorithm") + method := localAttribute(cs[0], lexicon.SecurityAttrAlgorithm) if !timestampCanonicalization(method) { return "", nil, ErrTimestamp } @@ -340,7 +354,7 @@ func parseTimeStamp(ctx context.Context, e document.Element, maximum int) (strin var tokens [][]byte for _, c := range cs[1:] { - if c.Name() != name("EncapsulatedTimeStamp") || len(c.Children()) != 0 { + if c.Name() != name(nameEncapsulatedTimeStamp) || len(c.Children()) != 0 { return "", nil, ErrTimestamp } @@ -350,11 +364,11 @@ func parseTimeStamp(ctx context.Context, e document.Element, maximum int) (strin } switch a.Name.Local { - case "Id": + case lexicon.SecurityAttrID: if !tree.NCName(a.Value) { return "", nil, ErrProfile } - case "Encoding": + case lexicon.SecurityAttrEncoding: if a.Value != encodingDER && a.Value != encodingBER { return "", nil, ErrTimestamp } @@ -396,8 +410,12 @@ func verifySignatureTimestampsMode(ctx context.Context, sig document.Element, o return nil, ErrTimestamp } + if err := elementContent(usp); err != nil { + return nil, err + } + props := usp.Children() - if len(props) == 0 || (!preservation && len(props) > o.MaxTimestamps) || len(props) > 140 { + if len(props) == 0 || (!preservation && len(props) > o.MaxTimestamps) || len(props) > maximumPreservationProperties { return nil, ErrTimestamp } @@ -448,8 +466,8 @@ func unsignedDraft(ctx context.Context, doc *document.Document, signature docume _, original := tree.Unwrap(properties) owned := *original owned.Children = append([]*tree.Node(nil), original.Children...) - container := xnode("UnsignedProperties") - unsignedNode := xnode("UnsignedSignatureProperties") + container := xnode(nameUnsignedProperties) + unsignedNode := xnode(nameUnsignedSignatureProperties) add(container, unsignedNode) add(&owned, container) draft, err := tree.ReplaceDraft(ctx, doc, original, &owned) @@ -463,11 +481,11 @@ func appendVerifiedTimestamp(ctx context.Context, doc *document.Document, select return nil, nil, e } - stamp := xnode("SignatureTimeStamp") + stamp := xnode(nameSignatureTimeStamp) canonical := dsnode(lexicon.DSigElementCanonicalizationMethod) - attr(canonical, "Algorithm", method) + attr(canonical, lexicon.SecurityAttrAlgorithm, method) - enc := xnode("EncapsulatedTimeStamp") + enc := xnode(nameEncapsulatedTimeStamp) encoded, e := encodeBase64(ctx, token.Size(), token.WriterTo(ctx)) if e != nil { @@ -498,7 +516,7 @@ func verifySignatureTimestampsModeProps(ctx context.Context, props []tree.Elemen continue } - if p.Name() != name("SignatureTimeStamp") { + if p.Name() != name(nameSignatureTimeStamp) { return fmt.Errorf("%w: unsupported unsigned property %s", ErrProfile, p.Name().Local), true } @@ -517,7 +535,7 @@ func verifySignatureTimestampsModeProps(ctx context.Context, props []tree.Elemen return e, true } - imprint, e := canonicalImprint(ctx, *sig, method, 1<<20, token.UnverifiedInfo().Imprint().Hash) + imprint, e := canonicalImprint(ctx, *sig, method, maximumSignatureCanonicalBytes, token.UnverifiedInfo().Imprint().Hash) if e != nil { return e, true } diff --git a/xades/timestamp_test.go b/xades/timestamp_test.go index 0c02c2c..ed4649c 100644 --- a/xades/timestamp_test.go +++ b/xades/timestamp_test.go @@ -236,6 +236,8 @@ func TestTSARevocationUsesVerificationTimeAndExactTarget(t *testing.T) { result, err := xades.VerifyTimedEnveloped(t.Context(), stamped, xmldsig.Signatures(stamped)[0], chain.Leaf.Raw, verify) trusted, report, trustErr := withTime.ValidateEnveloped(t.Context(), stamped, xmldsig.Signatures(stamped)[0], chain.Leaf.Raw, verifiedAt, xades.ValidationEvidence{Signer: pki.Evidence{CRLs: [][]byte{datedCRL(t, chain, generated, 1)}}, TSA: verify.Evidence}) if want != nil { + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if result != nil || trusted != nil || !errors.Is(err, want) || !errors.Is(trustErr, want) || report.Cause() == nil { t.Fatalf("TSA rejection: timed=%v trusted=%v errors=%v, %v; want %v", result != nil, trusted != nil, err, trustErr, want) } @@ -520,8 +522,12 @@ func TestHistoricalSignedContentTimestampRequired(t *testing.T) { } evidence := xades.ValidationEvidence{Signer: tsa.Evidence, TSA: tsa.Evidence} trusted, report, err := verifier.Validate(t.Context(), signed, result.Basic().Signature().Signature(), chain.Leaf.Raw, at, evidence) + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if correct { - if err != nil || trusted == nil || report.Indication() != xades.Passed || len(report.Timestamps()) != 1 || !report.Timestamps()[0].Content || !report.BestSignatureTime().Equal(at) { + if err != nil { + t.Fatal(err) + } else if trusted == nil || report.Indication() != xades.Passed || len(report.Timestamps()) != 1 || !report.Timestamps()[0].Content || !report.BestSignatureTime().Equal(at) { t.Fatalf("content timestamp current proof: %v %s %v %+v", report.Indication(), report.SubIndication(), err, report.Timestamps()) } signatureHash := sha256.Sum256(mustSignatureValue(t, signed)) diff --git a/xades/validate.go b/xades/validate.go index 71ba2c4..2899305 100644 --- a/xades/validate.go +++ b/xades/validate.go @@ -11,6 +11,14 @@ import ( "github.com/skarm/xmlsec/timestamp" ) +const ( + profileEnvelopedCurrentTime = "xmlsec-enveloped-current-time-v1" + defaultArchiveBytes = 16 << 20 + defaultArchiveTimestamps = 8 + maximumArchiveBytes = 64 << 20 + maximumArchiveTimestamps = 16 +) + // ErrLevel reports missing evidence or an unavailable required XAdES profile level. var ErrLevel = errors.New("required XAdES profile level or evidence unavailable") @@ -57,14 +65,23 @@ type ValidatorConfig struct { // Validator retains immutable policy for closed-profile XAdES signatures and offline // evidence validation. type Validator struct { - signer *pki.Verifier - tsa *timestamp.Verifier - minimum Level - unchecked bool - basic VerifyOptions - maxTimestamps int - maxTokenBytes int - maxArchives int + // signer retains explicit signer certificate and revocation policy. + signer *pki.Verifier + // tsa retains timestamp authority verification policy. + tsa *timestamp.Verifier + // minimum is the least XAdES level accepted by this validator. + minimum Level + // unchecked explicitly permits signer verification without revocation checking. + unchecked bool + // basic retains the closed basic signature profile and algorithm policy. + basic VerifyOptions + // maxTimestamps bounds signature timestamp tokens. + maxTimestamps int + // maxTokenBytes bounds each encoded timestamp token in bytes. + maxTokenBytes int + // maxArchives bounds archive timestamps. + maxArchives int + // maxArchiveBytes bounds archive timestamp input in bytes. maxArchiveBytes int } @@ -92,14 +109,14 @@ func NewValidator(c ValidatorConfig) (*Validator, error) { } if c.MaxArchiveTimestamps == 0 { - c.MaxArchiveTimestamps = 8 + c.MaxArchiveTimestamps = defaultArchiveTimestamps } if c.MaxArchiveBytes == 0 { - c.MaxArchiveBytes = 16 << 20 + c.MaxArchiveBytes = defaultArchiveBytes } - if c.MaxArchiveTimestamps < 1 || c.MaxArchiveTimestamps > 16 || c.MaxArchiveBytes < 1 || c.MaxArchiveBytes > 64<<20 { + if c.MaxArchiveTimestamps < 1 || c.MaxArchiveTimestamps > maximumArchiveTimestamps || c.MaxArchiveBytes < 1 || c.MaxArchiveBytes > maximumArchiveBytes { return nil, ErrProfile } @@ -177,13 +194,20 @@ type ValidationEvidence struct { // ValidationResult retains an immutable accepted XAdES signature, its achieved level, // trust results and evidence. type ValidationResult struct { - basic *BasicResult + // basic retains the accepted basic signature and signed properties. + basic *BasicResult + // timestamps retains verified signature timestamp results. timestamps []*timestamp.Result - trust *pki.Result - level Level - at time.Time - archives []*timestamp.Result - material ValidationMaterial + // trust retains the accepted signer certificate and revocation result. + trust *pki.Result + // level records the XAdES level achieved under this validator's policy. + level Level + // at is the explicit validation time. + at time.Time + // archives retains verified archive timestamp results. + archives []*timestamp.Result + // material retains the validation material used by the accepted profile. + material ValidationMaterial } // Basic returns the immutable basic XAdES result. @@ -258,5 +282,5 @@ func (r *ValidationResult) Profile() string { return "" } - return "xmlsec-enveloped-current-time-v1" + return profileEnvelopedCurrentTime } diff --git a/xades/with_time.go b/xades/with_time.go index 9e0c486..a96befb 100644 --- a/xades/with_time.go +++ b/xades/with_time.go @@ -13,6 +13,10 @@ import ( "github.com/skarm/xmlsec/xmldsig" ) +const ( + profileEnvelopedWithTime = "xmlsec-enveloped-with-time-v1" +) + // WithTimeConfig selects the separate B-T profile. It checks signer PKIX and // revocation at the earliest proven existence of SignatureValue, with evidence // fresh at that proof time. TSA trust and evidence freshness are checked at @@ -222,7 +226,7 @@ func (r *WithTimeReport) Profile() string { return "" } - return "xmlsec-enveloped-with-time-v1" + return profileEnvelopedWithTime } // WithTimeResult retains an immutable signature accepted under the configured closed @@ -286,5 +290,5 @@ func (r *WithTimeResult) Profile() string { return "" } - return "xmlsec-enveloped-with-time-v1" + return profileEnvelopedWithTime } diff --git a/xades/with_time_crl.go b/xades/with_time_crl.go index 631e735..a76748b 100644 --- a/xades/with_time_crl.go +++ b/xades/with_time_crl.go @@ -8,6 +8,7 @@ import ( "fmt" "time" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/x509cert" "github.com/skarm/xmlsec/internal/x509crl" "github.com/skarm/xmlsec/internal/x509name" @@ -70,7 +71,7 @@ func recoveryCRLs(ctx context.Context, raw [][]byte) ([]*x509.RevocationList, er func recoveryCRLScope(ctx context.Context, list *x509.RevocationList) error { for _, extension := range list.Extensions { switch extension.Id.String() { - case "2.5.29.27", "2.5.29.28": // Delta indicator and issuing distribution point. + case lexicon.X509OIDDeltaCRLIndicator, lexicon.X509OIDIssuingDistributionPoint: // Delta indicator and issuing distribution point. return fmt.Errorf("%w: with-time requires direct complete CRLs", ErrProfile) } } @@ -81,7 +82,7 @@ func recoveryCRLScope(ctx context.Context, list *x509.RevocationList) error { } for _, extension := range entry.Extensions { - if extension.Id.String() == "2.5.29.29" { // certificateIssuer. + if extension.Id.String() == lexicon.X509OIDCertificateIssuer { // certificateIssuer. return fmt.Errorf("%w: with-time requires direct CRL entries", ErrProfile) } } diff --git a/xades/with_time_test.go b/xades/with_time_test.go index f5d1905..efe24c5 100644 --- a/xades/with_time_test.go +++ b/xades/with_time_test.go @@ -66,7 +66,9 @@ func TestHistoricalExpiredTSAArchiveEvidence(t *testing.T) { at := testcert.Now().Add(48 * time.Hour) exportHistoricalPair(t, chain, sufficient, insufficient, at, at) result, report, err := validator.Validate(t.Context(), sufficient, xmldsig.Signatures(sufficient)[0], chain.Leaf.Raw, at, xades.ValidationEvidence{}) - if err != nil || result == nil || report.Indication() != xades.Passed || !report.BestSignatureTime().Equal(testcert.Now().Add(-time.Second)) || result.PastSignerTrust() == nil || result.CurrentSignerTrust() != nil { + if err != nil { + t.Fatal(err) + } else if result == nil || report.Indication() != xades.Passed || !report.BestSignatureTime().Equal(testcert.Now().Add(-time.Second)) || result.PastSignerTrust() == nil || result.CurrentSignerTrust() != nil { t.Fatalf("sufficient archive: indication=%v sub=%s best=%s err=%v steps=%+v stamps=%+v", report.Indication(), report.SubIndication(), report.BestSignatureTime(), err, report.Steps(), report.Timestamps()) } if len(report.Proofs()) == 0 || len(report.Timestamps()) != 2 { @@ -102,6 +104,8 @@ func TestHistoricalExpiredTSAArchiveEvidence(t *testing.T) { } verifyOnlyXML(t, draft, chain) result, report, err := validator.Validate(t.Context(), draft, xmldsig.Signatures(draft)[0], chain.Leaf.Raw, testcert.Now(), xades.ValidationEvidence{}) + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if result != nil || !errors.Is(err, xades.ErrProfile) || report.Indication() == xades.Passed { t.Fatal("multiple baseline tokens accepted", err) } @@ -117,6 +121,8 @@ func TestHistoricalExpiredTSAArchiveEvidence(t *testing.T) { t.Fatal("mutable historical facts") } result, negative, err := validator.Validate(t.Context(), insufficient, xmldsig.Signatures(insufficient)[0], chain.Leaf.Raw, at, xades.ValidationEvidence{}) + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if result != nil || err == nil || negative.Indication() != xades.Indeterminate || negative.SubIndication() != "OUT_OF_BOUNDS_NO_POE" { t.Fatalf("missing archive: %v %s %v", negative.Indication(), negative.SubIndication(), err) } @@ -131,6 +137,8 @@ func TestHistoricalExpiredTSAArchiveEvidence(t *testing.T) { t.Fatal(err) } result, withSource, err := validator.Validate(t.Context(), insufficient, xmldsig.Signatures(insufficient)[0], chain.Leaf.Raw, at, xades.ValidationEvidence{Signer: pki.Evidence{CRLs: [][]byte{crl}, Sources: []*evidence.Observation{source}}}) + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if result != nil || err == nil || withSource.Indication() != xades.Indeterminate || withSource.SubIndication() != negative.SubIndication() { t.Fatal("retrieval metadata minted PoE", err) } @@ -140,6 +148,8 @@ func TestHistoricalExpiredTSAArchiveEvidence(t *testing.T) { if err != nil { t.Fatal(err) } + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if r, report, err := unknownAccuracy.Validate(t.Context(), sufficient, xmldsig.Signatures(sufficient)[0], chain.Leaf.Raw, at, xades.ValidationEvidence{}); r != nil || err == nil || report.Indication() == xades.Passed { t.Fatal("invented missing accuracy", err) } @@ -149,11 +159,15 @@ func TestHistoricalExpiredTSAArchiveEvidence(t *testing.T) { if err != nil { t.Fatal(err) } + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if r, report, err := obsolete.Validate(t.Context(), sufficient, xmldsig.Signatures(sufficient)[0], chain.Leaf.Raw, at, xades.ValidationEvidence{}); r != nil || err == nil || report.Indication() == xades.Passed { t.Fatal("timestamp backdated its own obsolete hash", err) } canceled, cancel := context.WithCancel(t.Context()) cancel() + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if r, report, err := validator.Validate(canceled, sufficient, xmldsig.Signatures(sufficient)[0], chain.Leaf.Raw, at, xades.ValidationEvidence{}); r != nil || !errors.Is(err, context.Canceled) || report.Indication() == xades.Passed { t.Fatal("cancellation", err) } @@ -203,7 +217,9 @@ func TestHistoricalTSARevocationNeedsIndependentArchive(t *testing.T) { crl := datedCRL(t, chain, testcert.Now().Add(time.Hour), 2, x509.RevocationListEntry{SerialNumber: chain.TSA.SerialNumber, RevocationTime: tc.revoked, ReasonCode: reason}) result, report, err := validator.Validate(t.Context(), tc.doc, xmldsig.Signatures(tc.doc)[0], chain.Leaf.Raw, at, xades.ValidationEvidence{TSA: pki.Evidence{CRLs: [][]byte{crl}}}) if tc.pass { - if result == nil || err != nil || report.Indication() != xades.Passed || !report.BestSignatureTime().Equal(testcert.Now().Add(-time.Second)) || !report.VerificationTime().Equal(at) { + if err != nil { + t.Fatal(err) + } else if result == nil || report.Indication() != xades.Passed || !report.BestSignatureTime().Equal(testcert.Now().Add(-time.Second)) || !report.VerificationTime().Equal(at) { t.Fatal("pre-revocation archive did not recover TSA", report.Indication(), report.SubIndication(), err) } found := false @@ -223,6 +239,8 @@ func TestHistoricalTSARevocationNeedsIndependentArchive(t *testing.T) { if reason == 6 { want = "TRY_LATER" } + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if result != nil || !errors.Is(err, xades.ErrHistorical) || report.Indication() != xades.Indeterminate || report.SubIndication() != want { t.Fatal("late or absent archive repaired revoked TSA", report.Indication(), report.SubIndication(), err) } @@ -246,6 +264,9 @@ func TestHistoricalTSADelegatedOCSPResponder(t *testing.T) { responder := testcert.Issue(t, &template, chain.Root, chain.ResponderKey.Public(), chain.RootKey) node := baselineTestNode(tree.Root(archived), xades.Namespace141, "ArchiveTimeStamp") enc := baselineTestNode(node, xades.Namespace, "EncapsulatedTimeStamp") + if enc == nil { + t.Fatal("archive timestamp token missing") + } var encoded strings.Builder for _, child := range enc.Children { if child.Kind == tree.TextNode { @@ -316,12 +337,16 @@ func TestHistoricalTSADelegatedOCSPResponder(t *testing.T) { verifyOnlyXML(t, tc.doc, chain) result, report, err := validator.Validate(t.Context(), tc.doc, xmldsig.Signatures(tc.doc)[0], chain.Leaf.Raw, at, xades.ValidationEvidence{TSA: proof}) if !tc.pass { + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if result != nil || !errors.Is(err, xades.ErrHistorical) || report.Indication() != xades.Indeterminate || report.SubIndication() != "REVOKED_NO_POE" { t.Fatal("unproven response from revoked responder repaired TSA", report.SubIndication(), err) } return } - if result == nil || err != nil || report.Indication() != xades.Passed { + if err != nil { + t.Fatal(err) + } else if result == nil || report.Indication() != xades.Passed { t.Fatal("independent archive failed to recover delegated status", report.SubIndication(), err) } for _, stamp := range report.Timestamps() { @@ -407,10 +432,14 @@ func TestHistoricalLocalOCSPGrantKeepsIssuerAndProofBoundaries(t *testing.T) { verifyOnlyXML(t, tc.doc, chain) result, report, err := validator.Validate(t.Context(), tc.doc, xmldsig.Signatures(tc.doc)[0], chain.Leaf.Raw, at, xades.ValidationEvidence{TSA: proof}) if !tc.pass { + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if result != nil || !errors.Is(err, xades.ErrHistorical) || report.Indication() != xades.Indeterminate || report.SubIndication() != "OUT_OF_BOUNDS_NO_POE" { t.Fatal("issuer scope or independent proof was bypassed", report.Indication(), report.SubIndication(), err) } - } else if result == nil || err != nil || report.Indication() != xades.Passed || result.CurrentSignerTrust() != nil || result.PastSignerTrust() == nil || !report.BestSignatureTime().Equal(testcert.Now().Add(-time.Second)) { + } else if err != nil { + t.Fatal(err) + } else if result == nil || report.Indication() != xades.Passed || result.CurrentSignerTrust() != nil || result.PastSignerTrust() == nil || !report.BestSignatureTime().Equal(testcert.Now().Add(-time.Second)) { t.Fatal("scoped authority failed historical recovery", report.SubIndication(), err) } if tc.doc == archived { @@ -458,12 +487,16 @@ func TestHistoricalTrustAnchorSunsetNeedsMaterialProof(t *testing.T) { } } if !tc.pass { + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if result != nil || !errors.Is(err, xades.ErrHistorical) || report.Indication() != xades.Indeterminate || report.SubIndication() != "NO_CERTIFICATE_CHAIN_FOUND_NO_POE" { t.Fatal("late material proof bypassed anchor sunset", report.SubIndication(), err) } return } - if result == nil || err != nil || report.Indication() != xades.Passed { + if err != nil { + t.Fatal(err) + } else if result == nil || report.Indication() != xades.Passed { t.Fatal("valid anchor policy rejected", report.SubIndication(), err) } if tc.sunset.IsZero() { @@ -580,10 +613,14 @@ func TestHistoricalTSAResponderCyclesAndAuthorizationDepth(t *testing.T) { verifyOnlyXML(t, timed, chain) result, report, err := validator.Validate(t.Context(), timed, xmldsig.Signatures(timed)[0], chain.Leaf.Raw, now, xades.ValidationEvidence{Signer: timeOptions(t, chain).Evidence, TSA: graph(t, tc.cyclic)}) if tc.want == nil { - if result == nil || err != nil || report.Indication() != xades.Passed || len(report.Timestamps()) != 1 || report.Timestamps()[0].Indication != xades.Passed { + if err != nil { + t.Fatal(err) + } else if result == nil || report.Indication() != xades.Passed || len(report.Timestamps()) != 1 || report.Timestamps()[0].Indication != xades.Passed { t.Fatal("valid TSA dependency rejected at exact depth", report.SubIndication(), err) } } else { + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if result != nil || !errors.Is(err, xades.ErrHistorical) || !errors.Is(err, tc.want) || !errors.Is(report.Cause(), tc.want) || report.Indication() != xades.Indeterminate { t.Fatal("TSA dependency failure lost its cause or returned partial trust", result != nil, report.SubIndication(), err) } @@ -602,80 +639,6 @@ func TestHistoricalTSAResponderCyclesAndAuthorizationDepth(t *testing.T) { } } -func historicalEmbeddedCRL(t testing.TB, doc *document.Document) []byte { - t.Helper() - stack := []document.Element{doc.Root()} - for len(stack) > 0 { - element := stack[len(stack)-1] - stack = stack[:len(stack)-1] - if element.Name() == (document.Name{Space: xades.Namespace, Local: "EncapsulatedCRLValue"}) { - raw, err := base64.StdEncoding.DecodeString(element.Text()) - if err != nil { - t.Fatal(err) - } - return raw - } - stack = append(stack, element.Children()...) - } - t.Fatal("missing embedded CRL") - return nil -} - -func historicalArchivePair(t testing.TB, digest ...string) (testcert.Chain, *document.Document, *document.Document, *timestamp.Verifier) { - t.Helper() - return historicalArchivePairWithArchiveKey(t, nil, digest...) -} - -func historicalArchivePairWithArchiveKey(t testing.TB, archiveKey crypto.Signer, digest ...string) (testcert.Chain, *document.Document, *document.Document, *timestamp.Verifier) { - t.Helper() - chain := testcert.New(t) - if archiveKey == nil { - archiveKey = chain.TSAKey - } - newTSA := *chain.TSA - newTSA.SerialNumber = big.NewInt(103) - newTSA.NotAfter = testcert.Now().Add(8 * 24 * time.Hour) - eku, err := asn1.Marshal([]asn1.ObjectIdentifier{{1, 3, 6, 1, 5, 5, 7, 3, 8}}) - if err != nil { - t.Fatal(err) - } - newTSA.ExtraExtensions = []pkix.Extension{{Id: asn1.ObjectIdentifier{2, 5, 29, 37}, Critical: true, Value: eku}} - newTSA = *testcert.Issue(t, &newTSA, chain.Root, archiveKey.Public(), chain.RootKey) - tsa, err := timestamp.NewVerifier(timestamp.Config{PKI: pki.Config{Roots: [][]byte{chain.Root.Raw}, Revocation: pki.RequireCRL}, Certificates: [][]byte{newTSA.Raw}, AllowedPolicies: []asn1.ObjectIdentifier{tsaPolicy()}}) - if err != nil { - t.Fatal(err) - } - input, err := document.Parse(t.Context(), []byte(`123`), document.DefaultLimits()) - if err != nil { - t.Fatal(err) - } - basic, _, err := xades.SignBasicEnveloped(t.Context(), input, chain.LeafKey, chain.Leaf.Raw, xades.SignOptions{SignatureMethod: xmldsig.ECDSASHA256, ClaimedSigningTime: testcert.Now()}) - if len(digest) != 0 { - basic, _, err = xades.SignBaseline(t.Context(), input, input.Root(), chain.LeafKey, chain.Leaf.Raw, xades.BaselineSignOptions{Template: xmldsig.SignatureTemplate{SignatureMethod: xmldsig.ECDSASHA256, References: []xmldsig.ReferenceTemplate{{URI: "#data", DigestMethod: digest[0], Transforms: []reference.Transform{{Algorithm: reference.EnvelopedSignature}, {Algorithm: c14n.Exclusive}}}}}, Properties: xades.BaselineProperties{SigningTime: testcert.Now()}, Validation: xades.BaselineVerifyOptions{Signature: xmldsig.SignatureOptions{IDAttribute: document.Name{Local: "ID"}}}}) - } - if err != nil { - t.Fatal(err) - } - proof := pki.Evidence{CRLs: [][]byte{historicalLongCRL(t, chain)}} - stamped, _, err := xades.AddSignatureTimestamp(t.Context(), basic, xmldsig.Signatures(basic)[0], chain.Leaf.Raw, &archiveTransport{key: chain.TSAKey, cert: chain.TSA.Raw, root: chain.Root.Raw, now: testcert.Now().Add(-time.Second)}, xades.AddTimestampOptions{Validation: xades.TimedOptions{TSA: tsa, At: testcert.Now(), Evidence: proof}}) - if err != nil { - t.Fatal(err) - } - closed, err := xades.NewValidator(xades.ValidatorConfig{SignerPKI: pki.Config{Roots: [][]byte{chain.Root.Raw}, Revocation: pki.RequireCRL}, TSA: tsa, MinimumLevel: xades.LevelLT}) - if err != nil { - t.Fatal(err) - } - material, _, err := closed.EmbedValidationMaterial(t.Context(), stamped, xmldsig.Signatures(stamped)[0], chain.Leaf.Raw, testcert.Now(), xades.ValidationEvidence{Signer: proof, TSA: proof}) - if err != nil { - t.Fatal(err) - } - archived, _, err := closed.AddArchiveTimestamp(t.Context(), material, xmldsig.Signatures(material)[0], chain.Leaf.Raw, testcert.Now().Add(30*time.Minute), &archiveTransport{key: archiveKey, cert: newTSA.Raw, root: chain.Root.Raw, now: testcert.Now().Add(30 * time.Minute)}, xades.AddArchiveOptions{Canonicalization: c14n.Canonical11}) - if err != nil { - t.Fatal(err) - } - return chain, archived, material, tsa -} - func TestHistoricalDigestMaterialRequiresItsOwnProof(t *testing.T) { chain, archived, onlySignature, tsa := historicalArchivePair(t, xmldsig.SHA384) zero := time.Duration(0) @@ -685,11 +648,15 @@ func TestHistoricalDigestMaterialRequiresItsOwnProof(t *testing.T) { } at := testcert.Now().Add(12 * time.Hour) result, report, err := v.Validate(t.Context(), onlySignature, xmldsig.Signatures(onlySignature)[0], chain.Leaf.Raw, at, xades.ValidationEvidence{}) + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if result != nil || err == nil || report.Indication() != xades.Indeterminate || report.SubIndication() != "CRYPTO_CONSTRAINTS_FAILURE_NO_POE" { t.Fatalf("borrowed SignatureValue proof for reference input: %v %s %v", report.Indication(), report.SubIndication(), err) } result, report, err = v.Validate(t.Context(), archived, xmldsig.Signatures(archived)[0], chain.Leaf.Raw, at, xades.ValidationEvidence{}) - if result == nil || err != nil || report.Indication() != xades.Passed { + if err != nil { + t.Fatal(err) + } else if result == nil || report.Indication() != xades.Passed { t.Fatalf("exact archive reference proof: %v %s %v", report.Indication(), report.SubIndication(), err) } } @@ -729,10 +696,14 @@ func TestHistoricalExplicitPolicyDigestConstraints(t *testing.T) { } result, report, err := verifier.Validate(t.Context(), signed, baseline.Basic().Signature().Signature(), chain.Leaf.Raw, testcert.Now(), xades.ValidationEvidence{Signer: pki.Evidence{CRLs: [][]byte{testcert.CRL(t, chain)}}}) if tc.want != nil { + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if result != nil || !errors.Is(err, tc.want) || report.SubIndication() != tc.sub || report.Cause() == nil { t.Fatalf("historical policy: %v, %s, %v; want %s, %v", result != nil, report.SubIndication(), err, tc.sub, tc.want) } - } else if result == nil || err != nil || report.Indication() != xades.Passed || result.CurrentSignerTrust() == nil { + } else if err != nil { + t.Fatal(err) + } else if result == nil || report.Indication() != xades.Passed || result.CurrentSignerTrust() == nil { t.Fatal("reliable explicit policy rejected", report.SubIndication(), err) } }) @@ -761,7 +732,9 @@ func TestHistoricalEffectivePSSParameters(t *testing.T) { t.Fatal(err) } input := xades.ValidationEvidence{Signer: pki.Evidence{CRLs: [][]byte{historicalLongCRL(t, chain)}}} - if result, report, err := v.Validate(t.Context(), signed, xmldsig.Signatures(signed)[0], leaf.Raw, testcert.Now(), input); err != nil || result == nil || report.Indication() != xades.Passed { + if result, report, err := v.Validate(t.Context(), signed, xmldsig.Signatures(signed)[0], leaf.Raw, testcert.Now(), input); err != nil { + t.Fatal(err) + } else if result == nil || report.Indication() != xades.Passed { t.Fatal("effective PSS profile", report.SubIndication(), err) } config.SignerPKI.CryptoConstraints = []pki.CryptoConstraint{{Hash: crypto.SHA512, ReliableUntil: testcert.Now()}} @@ -769,6 +742,8 @@ func TestHistoricalEffectivePSSParameters(t *testing.T) { if err != nil { t.Fatal(err) } + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if result, report, err := v.Validate(t.Context(), signed, xmldsig.Signatures(signed)[0], leaf.Raw, testcert.Now(), input); err == nil || result != nil || report.SubIndication() != "CRYPTO_CONSTRAINTS_FAILURE_NO_POE" { t.Fatal("PSS parameters silently used default hash", report.SubIndication(), err) } @@ -888,6 +863,8 @@ func TestWithTimeNoUntrustedRecovery(t *testing.T) { t.Helper() t.Run(name, func(t *testing.T) { r, report, e := v.ValidateEnveloped(t.Context(), doc, xmldsig.Signatures(doc)[0], c.Leaf.Raw, testcert.Now(), evidence) + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if r != nil || e == nil || report.Indication() != status || want != nil && !errors.Is(e, want) { t.Fatal(r, report.Indication(), e) } @@ -967,22 +944,13 @@ func TestWithTimeNoUntrustedRecovery(t *testing.T) { ctx, cancel := context.WithCancel(t.Context()) cancel() + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if r, report, e := v.ValidateEnveloped(ctx, d, xmldsig.Signatures(d)[0], c.Leaf.Raw, testcert.Now(), good); r != nil || !errors.Is(e, context.Canceled) || report.Indication() == xades.Passed { t.Fatal(e) } } -func mustXML(t testing.TB, d *document.Document) []byte { - t.Helper() - - raw, e := d.XML(t.Context()) - if e != nil { - t.Fatal(e) - } - - return raw -} - func TestWithTimeAccuracyAndEarliestProof(t *testing.T) { c := expiredChain(t) gen := c.Leaf.NotAfter.Add(-time.Second) @@ -991,6 +959,8 @@ func TestWithTimeAccuracyAndEarliestProof(t *testing.T) { evidence := xades.ValidationEvidence{Signer: pki.Evidence{CRLs: [][]byte{datedCRL(t, c, gen, 1)}}, TSA: timeOptions(t, c).Evidence} v := timeValidator(t, c) + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if r, report, e := v.ValidateEnveloped(t.Context(), d, xmldsig.Signatures(d)[0], c.Leaf.Raw, testcert.Now(), evidence); r != nil || !errors.Is(e, xades.ErrLevel) || report.Indication() != xades.Indeterminate || !report.ProofTime().Equal(gen.Add(accuracy)) { t.Fatal("accuracy ignored", e) } @@ -1015,51 +985,6 @@ func TestWithTimeAccuracyAndEarliestProof(t *testing.T) { } } -func datedCRL(t testing.TB, c testcert.Chain, at time.Time, number int64, entries ...x509.RevocationListEntry) []byte { - t.Helper() - - b, e := x509.CreateRevocationList(rand.Reader, &x509.RevocationList{Number: big.NewInt(number), ThisUpdate: at.Add(-time.Minute), NextUpdate: at.Add(time.Hour), RevokedCertificateEntries: entries}, c.Root, c.RootKey) - if e != nil { - t.Fatal(e) - } - - return b -} - -func timeValidator(t testing.TB, c testcert.Chain) *xades.WithTimeValidator { - t.Helper() - o := timeOptions(t, c) - - v, e := xades.NewWithTimeValidator(xades.WithTimeConfig{SignerPKI: pki.Config{Roots: [][]byte{c.Root.Raw}, Revocation: pki.RequireCRL}, TSA: o.TSA}) - if e != nil { - t.Fatal(e) - } - - return v -} - -func timedPastDocument(t *testing.T, c testcert.Chain, gen time.Time, accuracy *time.Duration) *document.Document { - t.Helper() - base := basicDocument(t, c) - - d, _, e := xades.AddSignatureTimestamp(t.Context(), base, xmldsig.Signatures(base)[0], c.Leaf.Raw, datedTSA{c, gen, accuracy}, xades.AddTimestampOptions{Validation: timeOptions(t, c), Request: timestamp.RequestOptions{Policy: tsaPolicy()}}) - if e != nil { - t.Fatal(e) - } - - return d -} - -func expiredChain(t testing.TB) testcert.Chain { - t.Helper() - c := testcert.New(t) - leaf := *c.Leaf - leaf.NotAfter = testcert.Now().Add(-time.Hour) - c.Leaf = testcert.Issue(t, &leaf, c.Root, &c.LeafKey.PublicKey, c.RootKey) - - return c -} - func TestWithTimeRelativeDistributionPointCertificate(t *testing.T) { chain := testcert.New(t) var relative []byte @@ -1085,55 +1010,6 @@ func TestWithTimeRelativeDistributionPointCertificate(t *testing.T) { } } -func withTimeCRLIssuerEncoding(t *testing.T, raw []byte, c testcert.Chain, commonName string, version1 bool) []byte { - t.Helper() - value, err := der.Read(raw, len(raw)) - if err != nil { - t.Fatal(err) - } - outer, err := der.Sequence(value, 3, 3) - if err != nil { - t.Fatal(err) - } - body, err := der.Sequence(outer[0], 3, 7) - if err != nil { - t.Fatal(err) - } - issuerIndex := 1 - if body[0].Tag == asn1.TagInteger { - issuerIndex++ - } - name, err := asn1.Marshal(pkix.RDNSequence{{{Type: asn1.ObjectIdentifier{2, 5, 4, 3}, Value: commonName}}}) - if err != nil { - t.Fatal(err) - } - parts := make([][]byte, 0, len(body)) - for i, field := range body { - if version1 && (i == 0 && issuerIndex == 2 || field.Class == 2 && field.Tag == 0) { - continue - } - if i == issuerIndex { - parts = append(parts, name) - } else { - parts = append(parts, field.FullBytes) - } - } - tbs := der.Seq(parts...) - algorithm, err := signature.Algorithm(outer[1]) - if err != nil { - t.Fatal(err) - } - signed, err := signature.Sign(t.Context(), c.RootKey, algorithm, tbs) - if err != nil { - t.Fatal(err) - } - bits, err := asn1.Marshal(asn1.BitString{Bytes: signed, BitLength: len(signed) * 8}) - if err != nil { - t.Fatal(err) - } - return der.Seq(tbs, outer[1].FullBytes, bits) -} - func TestWithTimeV1CRLEvidence(t *testing.T) { c := expiredChain(t) proof := testcert.Now().Add(-2 * time.Hour) @@ -1225,6 +1101,8 @@ func TestWithTimeAdvancedCRLInputScope(t *testing.T) { doc := timedPastDocument(t, c, proof, nil) v := timeValidator(t, c) result, report, e := v.ValidateEnveloped(t.Context(), doc, xmldsig.Signatures(doc)[0], c.Leaf.Raw, testcert.Now(), evidence) + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if result != nil || !errors.Is(e, xades.ErrProfile) || report.Indication() != xades.Indeterminate { t.Fatal("broader current PKI input silently enabled historical recovery", result, report, e) } @@ -1253,13 +1131,13 @@ func TestWithTimeDirectCRLFreshestAdvertisementPolicy(t *testing.T) { t.Fatal(err) } doc := timedPastDocument(t, c, proof, nil) - evidence := xades.ValidationEvidence{Signer: pki.Evidence{CRLs: [][]byte{raw}}, TSA: timeOptions(t, c).Evidence} + e := xades.ValidationEvidence{Signer: pki.Evidence{CRLs: [][]byte{raw}}, TSA: timeOptions(t, c).Evidence} for _, disable := range []bool{false, true} { validator, err := xades.NewWithTimeValidator(xades.WithTimeConfig{SignerPKI: pki.Config{Roots: [][]byte{c.Root.Raw}, Revocation: pki.RequireCRL, CRL: pki.CRLOptions{DisableDeltaCRLs: disable}}, TSA: timeOptions(t, c).TSA}) if err != nil { t.Fatal(err) } - result, report, err := validator.ValidateEnveloped(t.Context(), doc, xmldsig.Signatures(doc)[0], c.Leaf.Raw, testcert.Now(), evidence) + result, report, err := validator.ValidateEnveloped(t.Context(), doc, xmldsig.Signatures(doc)[0], c.Leaf.Raw, testcert.Now(), e) if disable { if err != nil || result == nil || report.Indication() != xades.Passed { t.Fatal("unused advertisement changed the direct complete input scope", report, err) @@ -1295,10 +1173,424 @@ func BenchmarkHistoricalValidation(b *testing.B) { if err != nil || result == nil || report.Indication() != xades.Passed { b.Fatal("historical archive rejected", err) } - } else if err == nil || result != nil || report.SubIndication() != "OUT_OF_BOUNDS_NO_POE" { - b.Fatal("missing proof accepted", err) + } else { + // Verification reports retain diagnostics on failure. + // noinspection GoDfaErrorMayBeNotNil + if err == nil || result != nil || report.SubIndication() != "OUT_OF_BOUNDS_NO_POE" { + b.Fatal("missing proof accepted", err) + } } } }) } } + +func historicalEmbeddedCRL(t testing.TB, doc *document.Document) []byte { + t.Helper() + stack := []document.Element{doc.Root()} + for len(stack) > 0 { + element := stack[len(stack)-1] + stack = stack[:len(stack)-1] + if element.Name() == (document.Name{Space: xades.Namespace, Local: "EncapsulatedCRLValue"}) { + raw, err := base64.StdEncoding.DecodeString(element.Text()) + if err != nil { + t.Fatal(err) + } + return raw + } + stack = append(stack, element.Children()...) + } + t.Fatal("missing embedded CRL") + return nil +} + +func historicalArchivePair(t testing.TB, digest ...string) (testcert.Chain, *document.Document, *document.Document, *timestamp.Verifier) { + t.Helper() + return historicalArchivePairWithArchiveKey(t, nil, digest...) +} + +func historicalArchivePairWithArchiveKey(t testing.TB, archiveKey crypto.Signer, digest ...string) (testcert.Chain, *document.Document, *document.Document, *timestamp.Verifier) { + t.Helper() + chain := testcert.New(t) + if archiveKey == nil { + archiveKey = chain.TSAKey + } + newTSA := *chain.TSA + newTSA.SerialNumber = big.NewInt(103) + newTSA.NotAfter = testcert.Now().Add(8 * 24 * time.Hour) + eku, err := asn1.Marshal([]asn1.ObjectIdentifier{{1, 3, 6, 1, 5, 5, 7, 3, 8}}) + if err != nil { + t.Fatal(err) + } + newTSA.ExtraExtensions = []pkix.Extension{{Id: asn1.ObjectIdentifier{2, 5, 29, 37}, Critical: true, Value: eku}} + newTSA = *testcert.Issue(t, &newTSA, chain.Root, archiveKey.Public(), chain.RootKey) + tsa, err := timestamp.NewVerifier(timestamp.Config{PKI: pki.Config{Roots: [][]byte{chain.Root.Raw}, Revocation: pki.RequireCRL}, Certificates: [][]byte{newTSA.Raw}, AllowedPolicies: []asn1.ObjectIdentifier{tsaPolicy()}}) + if err != nil { + t.Fatal(err) + } + input, err := document.Parse(t.Context(), []byte(`123`), document.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + basic, _, err := xades.SignBasicEnveloped(t.Context(), input, chain.LeafKey, chain.Leaf.Raw, xades.SignOptions{SignatureMethod: xmldsig.ECDSASHA256, ClaimedSigningTime: testcert.Now()}) + if len(digest) != 0 { + basic, _, err = xades.SignBaseline(t.Context(), input, input.Root(), chain.LeafKey, chain.Leaf.Raw, xades.BaselineSignOptions{Template: xmldsig.SignatureTemplate{SignatureMethod: xmldsig.ECDSASHA256, References: []xmldsig.ReferenceTemplate{{URI: "#data", DigestMethod: digest[0], Transforms: []reference.Transform{{Algorithm: reference.EnvelopedSignature}, {Algorithm: c14n.Exclusive}}}}}, Properties: xades.BaselineProperties{SigningTime: testcert.Now()}, Validation: xades.BaselineVerifyOptions{Signature: xmldsig.SignatureOptions{IDAttribute: document.Name{Local: "ID"}}}}) + } + if err != nil { + t.Fatal(err) + } + proof := pki.Evidence{CRLs: [][]byte{historicalLongCRL(t, chain)}} + stamped, _, err := xades.AddSignatureTimestamp(t.Context(), basic, xmldsig.Signatures(basic)[0], chain.Leaf.Raw, &archiveTransport{key: chain.TSAKey, cert: chain.TSA.Raw, root: chain.Root.Raw, now: testcert.Now().Add(-time.Second)}, xades.AddTimestampOptions{Validation: xades.TimedOptions{TSA: tsa, At: testcert.Now(), Evidence: proof}}) + if err != nil { + t.Fatal(err) + } + closed, err := xades.NewValidator(xades.ValidatorConfig{SignerPKI: pki.Config{Roots: [][]byte{chain.Root.Raw}, Revocation: pki.RequireCRL}, TSA: tsa, MinimumLevel: xades.LevelLT}) + if err != nil { + t.Fatal(err) + } + material, _, err := closed.EmbedValidationMaterial(t.Context(), stamped, xmldsig.Signatures(stamped)[0], chain.Leaf.Raw, testcert.Now(), xades.ValidationEvidence{Signer: proof, TSA: proof}) + if err != nil { + t.Fatal(err) + } + archived, _, err := closed.AddArchiveTimestamp(t.Context(), material, xmldsig.Signatures(material)[0], chain.Leaf.Raw, testcert.Now().Add(30*time.Minute), &archiveTransport{key: archiveKey, cert: newTSA.Raw, root: chain.Root.Raw, now: testcert.Now().Add(30 * time.Minute)}, xades.AddArchiveOptions{Canonicalization: c14n.Canonical11}) + if err != nil { + t.Fatal(err) + } + return chain, archived, material, tsa +} + +func mustXML(t testing.TB, d *document.Document) []byte { + t.Helper() + + raw, e := d.XML(t.Context()) + if e != nil { + t.Fatal(e) + } + + return raw +} + +func datedCRL(t testing.TB, c testcert.Chain, at time.Time, number int64, entries ...x509.RevocationListEntry) []byte { + t.Helper() + + b, e := x509.CreateRevocationList(rand.Reader, &x509.RevocationList{Number: big.NewInt(number), ThisUpdate: at.Add(-time.Minute), NextUpdate: at.Add(time.Hour), RevokedCertificateEntries: entries}, c.Root, c.RootKey) + if e != nil { + t.Fatal(e) + } + + return b +} + +func timeValidator(t testing.TB, c testcert.Chain) *xades.WithTimeValidator { + t.Helper() + o := timeOptions(t, c) + + v, e := xades.NewWithTimeValidator(xades.WithTimeConfig{SignerPKI: pki.Config{Roots: [][]byte{c.Root.Raw}, Revocation: pki.RequireCRL}, TSA: o.TSA}) + if e != nil { + t.Fatal(e) + } + + return v +} + +func timedPastDocument(t *testing.T, c testcert.Chain, gen time.Time, accuracy *time.Duration) *document.Document { + t.Helper() + base := basicDocument(t, c) + + d, _, e := xades.AddSignatureTimestamp(t.Context(), base, xmldsig.Signatures(base)[0], c.Leaf.Raw, datedTSA{c, gen, accuracy}, xades.AddTimestampOptions{Validation: timeOptions(t, c), Request: timestamp.RequestOptions{Policy: tsaPolicy()}}) + if e != nil { + t.Fatal(e) + } + + return d +} + +func expiredChain(t testing.TB) testcert.Chain { + t.Helper() + c := testcert.New(t) + leaf := *c.Leaf + leaf.NotAfter = testcert.Now().Add(-time.Hour) + c.Leaf = testcert.Issue(t, &leaf, c.Root, &c.LeafKey.PublicKey, c.RootKey) + + return c +} + +func withTimeCRLIssuerEncoding(t *testing.T, raw []byte, c testcert.Chain, commonName string, version1 bool) []byte { + t.Helper() + value, err := der.Read(raw, len(raw)) + if err != nil { + t.Fatal(err) + } + outer, err := der.Sequence(value, 3, 3) + if err != nil { + t.Fatal(err) + } + body, err := der.Sequence(outer[0], 3, 7) + if err != nil { + t.Fatal(err) + } + issuerIndex := 1 + if body[0].Tag == asn1.TagInteger { + issuerIndex++ + } + name, err := asn1.Marshal(pkix.RDNSequence{{{Type: asn1.ObjectIdentifier{2, 5, 4, 3}, Value: commonName}}}) + if err != nil { + t.Fatal(err) + } + parts := make([][]byte, 0, len(body)) + for i, field := range body { + if version1 && (i == 0 && issuerIndex == 2 || field.Class == 2 && field.Tag == 0) { + continue + } + if i == issuerIndex { + parts = append(parts, name) + } else { + parts = append(parts, field.FullBytes) + } + } + tbs := der.Seq(parts...) + algorithm, err := signature.Algorithm(outer[1]) + if err != nil { + t.Fatal(err) + } + signed, err := signature.Sign(t.Context(), c.RootKey, algorithm, tbs) + if err != nil { + t.Fatal(err) + } + bits, err := asn1.Marshal(asn1.BitString{Bytes: signed, BitLength: len(signed) * 8}) + if err != nil { + t.Fatal(err) + } + return der.Seq(tbs, outer[1].FullBytes, bits) +} + +func TestHistoricalRejectsUnsupportedUnsignedDataProperties(t *testing.T) { + chain := testcert.New(t) + basic := basicDocument(t, chain) + options := timeOptions(t, chain) + timed, _, err := xades.AddSignatureTimestamp(t.Context(), basic, xmldsig.Signatures(basic)[0], chain.Leaf.Raw, &localTSA{chain: chain}, xades.AddTimestampOptions{Validation: options}) + if err != nil { + t.Fatal(err) + } + + zero := time.Duration(0) + validator, err := xades.NewHistoricalValidator(xades.HistoricalConfig{SignerPKI: pki.HistoricalConfig{PKI: pki.Config{Roots: [][]byte{chain.Root.Raw}, Revocation: pki.RequireCRL}}, TSA: options.TSA, MissingTSAAccuracy: &zero}) + if err != nil { + t.Fatal(err) + } + + for _, tc := range []struct { + name string + doc *document.Document + container string + }{ + {"basic control", basic, ""}, + {"timestamp control", timed, ""}, + {"unsupported signature property", basic, "UnsignedSignatureProperties"}, + {"unsupported data property alone", basic, "UnsignedDataObjectProperties"}, + {"unsupported data property after timestamp", timed, "UnsignedDataObjectProperties"}, + } { + t.Run(tc.name, func(t *testing.T) { + before := mustXML(t, tc.doc) + doc := tc.doc + if tc.container != "" { + doc = changeUnsignedXML(t, doc, func(root *tree.Node) { + unsigned := baselineTestNode(root, xades.Namespace, "UnsignedProperties") + if unsigned == nil { + unsigned = baselineTestAppend(baselineTestNode(root, xades.Namespace, "QualifyingProperties"), xades.Namespace, "UnsignedProperties", "xades") + } + + container := baselineTestAppend(unsigned, xades.Namespace, tc.container, "xades") + property := baselineTestAppend(container, xades.Namespace, "UnsignedDataObjectProperty", "xades") + property.Children = []*tree.Node{{Kind: tree.TextNode, Data: "unsupported domain evidence", Parent: property}} + }) + } + + verifyOnlyXML(t, doc, chain) + selected := xmldsig.Signatures(doc)[0] + if result, err := xades.VerifyBaseline(t.Context(), doc, selected, chain.Leaf.Raw, xades.BaselineVerifyOptions{}); err != nil || result == nil { + t.Fatal("baseline must retain unverified unsigned containers", err) + } + + result, report, err := validator.Validate(t.Context(), doc, selected, chain.Leaf.Raw, testcert.Now(), xades.ValidationEvidence{Signer: options.Evidence, TSA: options.Evidence}) + if tc.container == "" { + if err != nil { + t.Fatal(err) + } else if result == nil || report.Indication() != xades.Passed { + t.Fatal("valid historical control", report.SubIndication(), err) + } + } else { + // Verification reports retain diagnostics on failure. + // noinspection GoDfaErrorMayBeNotNil + if result != nil || !errors.Is(err, xades.ErrProfile) || report.Indication() != xades.Indeterminate || report.SubIndication() != "FORMAT_FAILURE" || !errors.Is(report.Cause(), xades.ErrProfile) { + t.Fatal("unsupported historical unsigned property accepted", result != nil, report.Indication(), report.SubIndication(), err) + } + } + + if !bytes.Equal(before, mustXML(t, tc.doc)) { + t.Fatal("validation changed source document") + } + }) + } +} + +func TestClosedTimestampRejectsMixedContainerText(t *testing.T) { + chain := testcert.New(t) + options := timeOptions(t, chain) + basic := basicDocument(t, chain) + timed, _, err := xades.AddSignatureTimestamp(t.Context(), basic, xmldsig.Signatures(basic)[0], chain.Leaf.Raw, &localTSA{chain: chain}, xades.AddTimestampOptions{Validation: options}) + if err != nil { + t.Fatal(err) + } + + current, err := xades.NewValidator(xades.ValidatorConfig{SignerPKI: pki.Config{Roots: [][]byte{chain.Root.Raw}, Revocation: pki.RequireCRL}, TSA: options.TSA, MinimumLevel: xades.LevelBT}) + if err != nil { + t.Fatal(err) + } + + withTime := timeValidator(t, chain) + before := mustXML(t, timed) + + for _, local := range []string{"UnsignedSignatureProperties", "SignatureTimeStamp"} { + for _, mixed := range []bool{false, true} { + name := local + "/XML whitespace and comments" + if mixed { + name = local + "/non-whitespace text" + } + + t.Run(name, func(t *testing.T) { + doc := changeUnsignedXML(t, timed, func(root *tree.Node) { + container := baselineTestNode(root, xades.Namespace, local) + appendContainerText(container, mixed) + }) + verifyOnlyXML(t, doc, chain) + selected := xmldsig.Signatures(doc)[0] + e := xades.ValidationEvidence{Signer: options.Evidence, TSA: options.Evidence} + result, err := xades.VerifyTimedEnveloped(t.Context(), doc, selected, chain.Leaf.Raw, options) + trusted, trustErr := current.ValidateEnveloped(t.Context(), doc, selected, chain.Leaf.Raw, testcert.Now(), e) + past, report, pastErr := withTime.ValidateEnveloped(t.Context(), doc, selected, chain.Leaf.Raw, testcert.Now(), e) + if mixed { + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil + if result != nil || trusted != nil || past != nil || !errors.Is(err, xades.ErrProfile) || !errors.Is(trustErr, xades.ErrProfile) || !errors.Is(pastErr, xades.ErrProfile) || report.Indication() != xades.Indeterminate { + t.Fatal("mixed timestamp text accepted", result != nil, trusted != nil, past != nil, err, trustErr, pastErr) + } + + server := &localTSA{chain: chain} + if out, augmented, err := xades.AddSignatureTimestamp(t.Context(), doc, selected, chain.Leaf.Raw, server, xades.AddTimestampOptions{Validation: options}); out != nil || augmented != nil || !errors.Is(err, xades.ErrProfile) || server.calls != 0 { + t.Fatal("invalid existing container reached TSA", server.calls, err) + } + } else if result == nil || trusted == nil || past == nil || err != nil || trustErr != nil || pastErr != nil || report.Indication() != xades.Passed { + t.Fatal("XML whitespace/comments/PI rejected", err, trustErr, pastErr) + } + }) + } + } + + if !bytes.Equal(before, mustXML(t, timed)) { + t.Fatal("timestamp validation changed source document") + } +} + +func TestClosedArchiveRejectsMixedContainerText(t *testing.T) { + chain, lt, _, validator := preparedLT(t) + server := &archiveTransport{key: chain.TSAKey, cert: chain.TSA.Raw, root: chain.Root.Raw, now: testcert.Now()} + archived, _, err := validator.AddArchiveTimestamp(t.Context(), lt, xmldsig.Signatures(lt)[0], chain.Leaf.Raw, testcert.Now(), server, xades.AddArchiveOptions{}) + if err != nil { + t.Fatal(err) + } + + for _, mixed := range []bool{false, true} { + name := "XML whitespace and comments" + if mixed { + name = "non-whitespace text" + } + + t.Run(name, func(t *testing.T) { + doc := changeUnsignedXML(t, archived, func(root *tree.Node) { + appendContainerText(baselineTestNode(root, xades.Namespace141, "ArchiveTimeStamp"), mixed) + }) + verifyOnlyXML(t, doc, chain) + result, err := validator.ValidateEnveloped(t.Context(), doc, xmldsig.Signatures(doc)[0], chain.Leaf.Raw, testcert.Now(), xades.ValidationEvidence{}) + if mixed { + if result != nil || !errors.Is(err, xades.ErrProfile) { + t.Fatal("mixed archive text accepted", result != nil, err) + } + } else if result == nil || err != nil || result.Level() != xades.LevelLTA { + t.Fatal("XML whitespace/comments/PI rejected", err) + } + }) + } +} + +func TestClosedCRLValuesRejectsMixedContainerText(t *testing.T) { + chain, doc, _, validator := preparedLT(t) + checkRevocationGroupContainerText(t, doc, chain, validator, "CRLValues") +} + +func checkRevocationGroupContainerText(t *testing.T, original *document.Document, chain testcert.Chain, validator *xades.Validator, local string) { + t.Helper() + + before := mustXML(t, original) + + for _, mixed := range []bool{false, true} { + name := local + "/XML whitespace and comments" + if mixed { + name = local + "/non-whitespace text" + } + + t.Run(name, func(t *testing.T) { + doc := changeUnsignedXML(t, original, func(root *tree.Node) { + appendContainerText(baselineTestNode(root, xades.Namespace, local), mixed) + }) + verifyOnlyXML(t, doc, chain) + result, err := validator.ValidateEnveloped(t.Context(), doc, xmldsig.Signatures(doc)[0], chain.Leaf.Raw, testcert.Now(), xades.ValidationEvidence{}) + if mixed { + if result != nil || !errors.Is(err, xades.ErrProfile) { + t.Fatal("mixed revocation group text accepted", result != nil, err) + } + } else if result == nil || err != nil || result.Level() != xades.LevelLT { + t.Fatal("XML whitespace/comments/PI rejected", err) + } + }) + } + + if !bytes.Equal(before, mustXML(t, original)) { + t.Fatal("validation changed source document") + } +} + +func changeUnsignedXML(t *testing.T, source *document.Document, change func(*tree.Node)) *document.Document { + t.Helper() + + draft, err := tree.Clone(t.Context(), source) + if err != nil { + t.Fatal(err) + } + + change(tree.Root(draft)) + + raw, err := draft.XML(t.Context()) + if err != nil { + t.Fatal(err) + } + + doc, err := document.Parse(t.Context(), raw, document.DefaultLimits()) + if err != nil { + t.Fatal(err) + } + + return doc +} + +func appendContainerText(container *tree.Node, mixed bool) { + value := " \t\r\n" + if mixed { + value = "unsupported text" + } + + container.Children = append(container.Children, + &tree.Node{Kind: tree.TextNode, Data: value, Parent: container}, + &tree.Node{Kind: tree.CommentNode, Data: "permitted comment", Parent: container}, + &tree.Node{Kind: tree.PINode, Name: document.Name{Local: "allowed"}, Data: "instruction", Parent: container}) +} diff --git a/xmldsig/algorithms.go b/xmldsig/algorithms.go index 86b728c..bbfe9cf 100644 --- a/xmldsig/algorithms.go +++ b/xmldsig/algorithms.go @@ -26,6 +26,19 @@ import ( "github.com/skarm/xmlsec/internal/signature" ) +const ( + // signatureOctetBits converts encoded key, digest and signature bytes to bit lengths. + signatureOctetBits = 8 + maximumRSAExponent = 1<<31 - 1 + // Caps decoded SignatureValue bytes before cryptographic verification or publication. + maximumSignatureBytes = 2048 + // Smallest permitted odd RSA public exponent in the supported signature profile. + minimumRSAExponent = 3 + legacyDSAComponentBytes = 20 + legacyDSASignatureBytes = 2 * legacyDSAComponentBytes + legacySHA1DigestBytes = 20 +) + const ( // Namespace is the XMLDSig 1.0 namespace URI. Namespace = lexicon.NamespaceDSig @@ -35,7 +48,7 @@ const ( SHA1 = lexicon.DigestSHA1 // DSASHA1 is the legacy DSA-SHA1 URI, supported for verification only. DSASHA1 = Namespace + "dsa-sha1" - // RSASHA1 is the legacy RSA-SHA1 URI, supported for verification only. + // RSASHA1 is the legacy RSA-SHA1 URI. Generation requires AllowLegacySigning. RSASHA1 = Namespace + "rsa-sha1" // SHA256 is the SHA-256 digest algorithm URI. SHA256 = lexicon.DigestSHA256 @@ -155,7 +168,7 @@ func defaultSignature(key crypto.PublicKey) string { func validateKey(key crypto.PublicKey, p Policy) error { switch k := key.(type) { case *rsa.PublicKey: - if k == nil || k.N == nil || k.N.Sign() <= 0 || k.N.BitLen() < p.MinRSABits || k.N.BitLen() > p.MaxRSABits || k.N.Bit(0) == 0 || k.E < 3 || k.E%2 == 0 || k.E > 0x7fffffff { + if k == nil || k.N == nil || k.N.Sign() <= 0 || k.N.BitLen() < p.MinRSABits || k.N.BitLen() > p.MaxRSABits || k.N.Bit(0) == 0 || k.E < minimumRSAExponent || k.E%2 == 0 || k.E > maximumRSAExponent { return fmt.Errorf("%w: RSA key outside policy", ErrKey) } case *ecdsa.PublicKey: @@ -220,7 +233,7 @@ func signPreparedValue(ctx context.Context, uri string, signer crypto.Signer, pu return nil, err } - if uri == DSASHA1 || uri == RSASHA1 { + if uri == DSASHA1 { return nil, fmt.Errorf("%w: legacy signature generation is disabled", ErrPolicy) } @@ -238,7 +251,7 @@ func signPreparedValue(ctx context.Context, uri string, signer crypto.Signer, pu return nil, fmt.Errorf("signer: %w", err) } - if len(raw) > 2048 { + if len(raw) > maximumSignatureBytes { return nil, fmt.Errorf("%w: signer output size", ErrSignature) } @@ -253,7 +266,7 @@ func signPreparedValue(ctx context.Context, uri string, signer crypto.Signer, pu return nil, fmt.Errorf("%w: signer returned invalid ECDSA DER", ErrSignature) } - n := (k.Params().BitSize + 7) / 8 + n := (k.Params().BitSize + signatureOctetBits - 1) / signatureOctetBits raw = make([]byte, 2*n) pair.R.FillBytes(raw[:n]) pair.S.FillBytes(raw[n:]) @@ -285,7 +298,7 @@ func verifyPreparedValue(uri string, pub crypto.PublicKey, h crypto.Hash, digest return fmt.Errorf("%w: Ed25519", ErrSignature) } case *ecdsa.PublicKey: - n := (k.Params().BitSize + 7) / 8 + n := (k.Params().BitSize + signatureOctetBits - 1) / signatureOctetBits if len(sig) != 2*n { return fmt.Errorf("%w: ECDSA must be fixed-width r || s", ErrSignature) } @@ -295,11 +308,11 @@ func verifyPreparedValue(uri string, pub crypto.PublicKey, h crypto.Hash, digest return fmt.Errorf("%w: ECDSA", ErrSignature) } case *dsa.PublicKey: - if uri != DSASHA1 || h != crypto.SHA1 || len(digest) != 20 || len(sig) != 40 { + if uri != DSASHA1 || h != crypto.SHA1 || len(digest) != legacySHA1DigestBytes || len(sig) != legacyDSASignatureBytes { return fmt.Errorf("%w: DSA-SHA1 requires fixed-width r || s", ErrSignature) } - if !dsa.Verify(k, digest, new(big.Int).SetBytes(sig[:20]), new(big.Int).SetBytes(sig[20:])) { + if !dsa.Verify(k, digest, new(big.Int).SetBytes(sig[:legacyDSAComponentBytes]), new(big.Int).SetBytes(sig[legacyDSAComponentBytes:])) { return fmt.Errorf("%w: DSA", ErrSignature) } default: diff --git a/xmldsig/builder.go b/xmldsig/builder.go index 3d63c7a..3128f69 100644 --- a/xmldsig/builder.go +++ b/xmldsig/builder.go @@ -12,6 +12,16 @@ import ( "github.com/skarm/xmlsec/reference" ) +const ( + // Fallback unqualified application ID attribute used by builders and reference resolution. + defaultIDAttribute = "ID" + maximumBuilderReferences = 64 + maximumCertificateBytes = 1 << 20 + maximumCertificateGroupBytes = 4 << 20 + maximumEmbeddedCertificates = 16 + maximumSignatureObjects = 256 +) + // ReferenceTemplate describes one direct or Manifest reference to a same-document // target or an allowlisted external URI. An empty transform list follows XMLDSig // input-type conversion rules and does not select Exclusive C14N. @@ -114,7 +124,7 @@ func BuildTemplate(ctx context.Context, doc *document.Document, parent document. return fail(err) } - if len(spec.References) < 1 || len(spec.References) > 64 || len(spec.Objects) > 256 { + if len(spec.References) < 1 || len(spec.References) > maximumBuilderReferences || len(spec.Objects) > maximumSignatureObjects { return fail(fmt.Errorf("%w: template size", ErrPolicy)) } @@ -144,7 +154,7 @@ func BuildTemplate(ctx context.Context, doc *document.Document, parent document. func signatureTemplateNode(ctx context.Context, spec SignatureTemplate, cm reference.Transform) (*tree.Node, error) { sig := newDS(lexicon.DSigElementSignature) - sig.NS = []tree.Namespace{{Prefix: "ds", URI: Namespace}} + sig.NS = []tree.Namespace{{Prefix: lexicon.PrefixDSig, URI: Namespace}} if err := templateID(sig, spec.ID); err != nil { return nil, err } @@ -153,7 +163,7 @@ func signatureTemplateNode(ctx context.Context, spec SignatureTemplate, cm refer add(sig, si) c := newDS(lexicon.DSigElementCanonicalizationMethod) - setAttr(c, "Algorithm", cm.Algorithm) + setAttr(c, lexicon.SecurityAttrAlgorithm, cm.Algorithm) addInclusive(c, cm.InclusivePrefixes) add(si, c) @@ -206,14 +216,14 @@ func appendTemplateKeyInfo(ctx context.Context, sig *tree.Node, spec SignatureTe return fmt.Errorf("%w: expected a KeyInfo document", ErrStructure) } - if _, err := structure(n, lexicon.DSigElementKeyInfo, "Id"); err != nil { + if _, err := structure(n, lexicon.DSigElementKeyInfo, lexicon.SecurityAttrID); err != nil { return err } add(sig, tree.ImportSubtree(n)) } - if len(spec.Certificates) > 16 { + if len(spec.Certificates) > maximumEmbeddedCertificates { return fmt.Errorf("%w: too many embedded certificates", ErrPolicy) } @@ -227,7 +237,7 @@ func appendTemplateKeyInfo(ctx context.Context, sig *tree.Node, spec SignatureTe total := 0 for _, der := range spec.Certificates { total += len(der) - if len(der) == 0 || len(der) > 1<<20 || total > 4<<20 { + if len(der) == 0 || len(der) > maximumCertificateBytes || total > maximumCertificateGroupBytes { return fmt.Errorf("%w: certificate size", ErrPolicy) } @@ -257,13 +267,13 @@ func objectTemplateNode(ctx context.Context, spec ObjectTemplate) (*tree.Node, e return nil, fmt.Errorf("%w: Object needs exactly one content variant", ErrStructure) } - object := newDS("Object") + object := newDS(lexicon.DSigElementObject) if err := templateID(object, spec.ID); err != nil { return nil, err } if spec.MIMEType != "" { - setAttr(object, "MimeType", spec.MIMEType) + setAttr(object, lexicon.SecurityAttrMimeType, spec.MIMEType) } switch { @@ -316,12 +326,12 @@ func objectTemplateVariants(spec ObjectTemplate) int { } func manifestTemplateNode(ctx context.Context, spec ManifestTemplate) (*tree.Node, error) { - manifest := newDS("Manifest") + manifest := newDS(nameManifest) if err := templateID(manifest, spec.ID); err != nil { return nil, err } - if len(spec.References) < 1 || len(spec.References) > 64 { + if len(spec.References) < 1 || len(spec.References) > maximumBuilderReferences { return nil, fmt.Errorf("%w: Manifest template reference count", ErrPolicy) } @@ -430,7 +440,7 @@ func parseTemplateDraft(ctx context.Context, draft signatureTemplateDraft) (*doc } // Detect ds:Id/default ID collisions early. Additional application ID rules // are still checked by SignTemplate's validation configuration. - if _, err := reference.NewSameDocumentResolverWithRules(ctx, final, coreIDRules(SignatureOptions{IDAttribute: document.Name{Local: "ID"}})); err != nil { + if _, err := reference.NewSameDocumentResolverWithRules(ctx, final, coreIDRules(SignatureOptions{IDAttribute: document.Name{Local: defaultIDAttribute}})); err != nil { return fail(err) } @@ -446,7 +456,7 @@ func templateID(n *tree.Node, id string) error { return fmt.Errorf("%w: invalid template ID", ErrReference) } - setAttr(n, "Id", id) + setAttr(n, lexicon.SecurityAttrID, id) return nil } @@ -483,17 +493,17 @@ func referenceTemplateNode(ctx context.Context, r ReferenceTemplate) (*tree.Node return nil, err } - setAttr(n, "URI", r.URI) + setAttr(n, lexicon.SecurityAttrURI, r.URI) if r.Type != "" { - setAttr(n, "Type", r.Type) + setAttr(n, lexicon.SecurityAttrType, r.Type) } if len(r.Transforms) > 0 { ts := newDS(lexicon.DSigElementTransforms) for _, tr := range r.Transforms { t := newDS(lexicon.DSigElementTransform) - setAttr(t, "Algorithm", tr.Algorithm) + setAttr(t, lexicon.SecurityAttrAlgorithm, tr.Algorithm) addInclusive(t, tr.InclusivePrefixes) if reference.IsExtension(tr.Algorithm) { @@ -509,7 +519,7 @@ func referenceTemplateNode(ctx context.Context, r ReferenceTemplate) (*tree.Node } dm := newDS(lexicon.DSigElementDigestMethod) - setAttr(dm, "Algorithm", r.DigestMethod) + setAttr(dm, lexicon.SecurityAttrAlgorithm, r.DigestMethod) add(n, dm) add(n, newDS(lexicon.DSigElementDigestValue)) diff --git a/xmldsig/core.go b/xmldsig/core.go index 6fb5fb8..af31b92 100644 --- a/xmldsig/core.go +++ b/xmldsig/core.go @@ -18,6 +18,31 @@ import ( "github.com/skarm/xmlsec/reference" ) +// SignedInfo begins with CanonicalizationMethod and SignatureMethod, followed +// by at least one Reference. All signature entry points use this same shape. +const ( + signedInfoHeaderChildren = 2 + signedInfoMinimumChildren = 3 +) + +const ( + stageKey = "key" + stageManifests = "manifests" + stageReferences = "references" + stageSignatureValue = "signature-value" + stageSignedInfo = "signed-info" + defaultMaxSignatures = 16 + initialIDElementCapacity = 9 + // Caps transforms parsed from one reference independently of execution policy defaults. + maximumReferenceTransforms = 64 + maximumSignatures = 64 +) + +const ( + nameSignatureProperties = "SignatureProperties" + nameSignatureProperty = "SignatureProperty" +) + // SignatureOptions verifies an explicitly selected signature. Same-document // references are enabled; external resources and recursive Manifest checks require // opt-in. Zero options do not require root coverage. Applications should set @@ -58,13 +83,79 @@ type SignatureOptions struct { // and XAdES requirements remain separate. The result has no Root convenience // method because core verification does not imply root coverage. type SignatureResult struct { - doc *document.Document - signature document.Element - algorithm string + // doc retains the exact immutable document snapshot that was checked. + doc *document.Document + // signature identifies the accepted Signature element in doc. + signature document.Element + // algorithm is the authenticated SignatureMethod URI. + algorithm string + // fingerprint identifies the verified key's SubjectPublicKeyInfo by SHA-256. fingerprint string - parameters SignatureParameters - references []ReferenceResult - manifests []ManifestResult + // parameters retains authenticated signature algorithm parameters. + parameters SignatureParameters + // references contains verified direct SignedInfo references. + references []ReferenceResult + // manifests contains results from explicitly requested Manifest traversal. + manifests []ManifestResult +} + +// CheckStatus records a diagnostic outcome. It grants no partial trust in returned +// data. +type CheckStatus string + +const ( + // NotChecked indicates that a check was not performed. + NotChecked CheckStatus = "not-checked" + // Valid indicates a successful check. + Valid CheckStatus = "valid" + // Invalid indicates invalid data or a failed check. + Invalid CheckStatus = "invalid" + // Unsupported indicates an unsupported feature. + Unsupported CheckStatus = "unsupported" + // Disallowed indicates a policy rejection. + Disallowed CheckStatus = "disallowed" + // Indeterminate indicates that validation cannot establish the required result. + Indeterminate CheckStatus = "indeterminate" +) + +// ReferenceReport records one reference digest outcome and its completed transform +// steps. +type ReferenceReport struct { + // Index is the zero-based reference position within its SignedInfo or Manifest. + Index int `json:"index"` + // URI preserves the signed reference URI spelling. + URI string `json:"uri"` + // ResourceURI is the authorized resolver URI; URI preserves signed spelling. + ResourceURI string `json:"resource_uri,omitempty"` + // Status records the reference digest check outcome. + Status CheckStatus `json:"status"` + // Stage identifies the failed reference processing stage, when present. + Stage string `json:"stage,omitempty"` + // Error describes the reference failure, or is empty on success. + Error string `json:"error,omitempty"` + // Steps records completed transforms and implicit conversions. + Steps []reference.Step `json:"steps,omitempty"` +} + +// VerificationReport contains signature, reference and Manifest diagnostics. Check the +// returned error before accepting any result. +type VerificationReport struct { + // ManifestValidation records the requested Manifest validation outcome. + ManifestValidation CheckStatus `json:"manifest_validation"` + // Manifests contains diagnostics for traversed Manifests in recursive mode. + Manifests []ManifestReport `json:"manifests,omitempty"` + // Status records the overall signature verification outcome. + Status CheckStatus `json:"status"` + // SignatureValue records the cryptographic signature check outcome. + SignatureValue CheckStatus `json:"signature_value"` + // SignatureMethod is the declared XMLDSig signature algorithm URI. + SignatureMethod string `json:"signature_method,omitempty"` + // References contains direct SignedInfo reference diagnostics in document order. + References []ReferenceReport `json:"references,omitempty"` + // FailureStage identifies the stage at which verification failed, when present. + FailureStage string `json:"failure_stage,omitempty"` + // Error describes the verification failure, or is empty on success. + Error string `json:"error,omitempty"` } // VerifySignature validates ONLY the explicitly selected signature with the @@ -156,6 +247,23 @@ func (r *SignatureResult) CoversElement(e document.Element) bool { return false } +// Signatures lists UNTRUSTED handles in document order, including nested ones. +// Neither this enumeration nor selecting a handle establishes trust. +func Signatures(doc *document.Document) []document.Element { + if doc == nil || !doc.Root().Valid() { + return nil + } + + found := findSignatures(doc) + + out := make([]document.Element, len(found)) + for i, n := range found { + out[i] = tree.Wrap(doc, n) + } + + return out +} + func failureStatus(err error) CheckStatus { switch { case errors.Is(err, context.Canceled), errors.Is(err, context.DeadlineExceeded): @@ -181,10 +289,10 @@ func coreOptions(doc *document.Document, opt SignatureOptions) (SignatureOptions } if opt.MaxSignatures == 0 { - opt.MaxSignatures = 16 + opt.MaxSignatures = defaultMaxSignatures } - if opt.MaxSignatures < 1 || opt.MaxSignatures > 64 { + if opt.MaxSignatures < 1 || opt.MaxSignatures > maximumSignatures { return opt, p, fmt.Errorf("%w: signature count limit", ErrPolicy) } @@ -218,19 +326,19 @@ func coreOptions(doc *document.Document, opt SignatureOptions) (SignatureOptions } func coreIDRules(opt SignatureOptions) []reference.IDAttributeRule { - names := make([]document.Name, 0, 9) - for _, local := range []string{lexicon.DSigElementSignature, lexicon.DSigElementSignedInfo, lexicon.DSigElementSignatureValue, lexicon.DSigElementReference, lexicon.DSigElementKeyInfo, "Object", "Manifest", "SignatureProperties", "SignatureProperty"} { + names := make([]document.Name, 0, initialIDElementCapacity) + for _, local := range []string{lexicon.DSigElementSignature, lexicon.DSigElementSignedInfo, lexicon.DSigElementSignatureValue, lexicon.DSigElementReference, lexicon.DSigElementKeyInfo, lexicon.DSigElementObject, nameManifest, nameSignatureProperties, nameSignatureProperty} { names = append(names, document.Name{Space: Namespace, Local: local}) } - rules := []reference.IDAttributeRule{{Attribute: opt.IDAttribute}, {Attribute: document.Name{Local: "Id"}, Elements: names}} + rules := []reference.IDAttributeRule{{Attribute: opt.IDAttribute}, {Attribute: document.Name{Local: lexicon.SecurityAttrID}, Elements: names}} rules = append(rules, keyinfo.DefaultIDRules()...) return append(rules, opt.AdditionalIDRules...) } func signatureParts(sig *tree.Node) (si, sv *tree.Node, err error) { - children, err := structure(sig, lexicon.DSigElementSignature, "Id") + children, err := structure(sig, lexicon.DSigElementSignature, lexicon.SecurityAttrID) if err != nil { return nil, nil, err } @@ -241,7 +349,7 @@ func signatureParts(sig *tree.Node) (si, sv *tree.Node, err error) { rest := children[2:] if len(rest) > 0 && isDS(rest[0], lexicon.DSigElementKeyInfo) { - if err := attributes(rest[0], "Id"); err != nil { + if err := attributes(rest[0], lexicon.SecurityAttrID); err != nil { return nil, nil, err } @@ -249,11 +357,11 @@ func signatureParts(sig *tree.Node) (si, sv *tree.Node, err error) { } for _, object := range rest { - if !isDS(object, "Object") { + if !isDS(object, lexicon.DSigElementObject) { return nil, nil, fmt.Errorf("%w: expected Object after SignatureValue/KeyInfo", ErrStructure) } - if err := attributes(object, "Id", "MimeType", "Encoding"); err != nil { + if err := attributes(object, lexicon.SecurityAttrID, lexicon.SecurityAttrMimeType, lexicon.SecurityAttrEncoding); err != nil { return nil, nil, err } // Object content is arbitrary XML. No XAdES/Manifest interpretation here. @@ -294,12 +402,12 @@ func prepareCore(ctx context.Context, doc *document.Document, selected document. return nil, err } - signed, err := structure(si, lexicon.DSigElementSignedInfo, "Id") + signed, err := structure(si, lexicon.DSigElementSignedInfo, lexicon.SecurityAttrID) if err != nil { return nil, err } - if len(signed) < 3 || len(signed)-2 > p.MaxReferences || !isDS(signed[0], lexicon.DSigElementCanonicalizationMethod) || !isDS(signed[1], lexicon.DSigElementSignatureMethod) { + if len(signed) < signedInfoMinimumChildren || len(signed)-signedInfoHeaderChildren > p.MaxReferences || !isDS(signed[0], lexicon.DSigElementCanonicalizationMethod) || !isDS(signed[1], lexicon.DSigElementSignatureMethod) { return nil, fmt.Errorf("%w: SignedInfo children/reference count", ErrStructure) } @@ -325,16 +433,16 @@ func prepareCore(ctx context.Context, doc *document.Document, selected document. report.SignatureMethod = plan.method if template { - err = emptyValue(sv, lexicon.DSigElementSignatureValue, "Id") + err = emptyValue(sv, lexicon.DSigElementSignatureValue, lexicon.SecurityAttrID) } else { - plan.value, err = base64Value(ctx, sv, lexicon.DSigElementSignatureValue, 2048, "Id") + plan.value, err = base64Value(ctx, sv, lexicon.DSigElementSignatureValue, maximumSignatureBytes, lexicon.SecurityAttrID) } if err != nil { return nil, err } - if err := plan.prepareReferences(ctx, signed[2:], opt, template, report); err != nil { + if err := plan.prepareReferences(ctx, signed[signedInfoHeaderChildren:], opt, template, report); err != nil { return nil, err } @@ -362,7 +470,7 @@ func prepareCore(ctx context.Context, doc *document.Document, selected document. func verifySignatureDetailed(ctx context.Context, doc *document.Document, selected document.Element, credential signatureCredential, opt SignatureOptions) (result *SignatureResult, report *VerificationReport, resultErr error) { report = &VerificationReport{Status: NotChecked, SignatureValue: NotChecked, ManifestValidation: NotChecked} - stage := "structure" + stage := stageStructure defer func() { if resultErr != nil { @@ -377,21 +485,21 @@ func verifySignatureDetailed(ctx context.Context, doc *document.Document, select return nil, report, err } - stage = "key" + stage = stageKey if err := credential.validate(plan.spec, plan.policy); err != nil { return nil, report, err } budget := newCoreBudget(plan) - stage = "signed-info" + stage = stageSignedInfo canonical, err := credential.prepareInput(ctx, plan.spec, func(dst io.Writer) error { return budget.signedInfo(ctx, dst, doc, plan) }) if err != nil { return nil, report, err } - stage = "signature-value" + stage = stageSignatureValue if err := ctx.Err(); err != nil { return nil, report, err @@ -403,17 +511,17 @@ func verifySignatureDetailed(ctx context.Context, doc *document.Document, select report.SignatureValue = Valid result = &SignatureResult{doc: doc, signature: selected, algorithm: plan.method, parameters: plan.spec.parameters()} - stage = "references" + stage = stageReferences if err := plan.graph.verify(ctx, doc, plan, budget, result, report); err != nil { if _, ok := errors.AsType[*ManifestReferenceError](err); ok { - stage = "manifests" + stage = stageManifests } return nil, report, err } - stage = "coverage" + stage = stageCoverage for _, e := range opt.RequiredElements { if !result.CoversElement(e) { @@ -451,7 +559,7 @@ func finalizeVerificationReport(report *VerificationReport, stage string, err er report.FailureStage = stage report.Error = err.Error() - if stage == "signature-value" { + if stage == stageSignatureValue { report.SignatureValue = status } @@ -491,82 +599,6 @@ func recordReferenceFailure(references []ReferenceReport, re *ReferenceError, st r.Error = re.Err.Error() } -// CheckStatus records a diagnostic outcome. It grants no partial trust in returned -// data. -type CheckStatus string - -const ( - // NotChecked indicates that a check was not performed. - NotChecked CheckStatus = "not-checked" - // Valid indicates a successful check. - Valid CheckStatus = "valid" - // Invalid indicates invalid data or a failed check. - Invalid CheckStatus = "invalid" - // Unsupported indicates an unsupported feature. - Unsupported CheckStatus = "unsupported" - // Disallowed indicates a policy rejection. - Disallowed CheckStatus = "disallowed" - // Indeterminate indicates that validation cannot establish the required result. - Indeterminate CheckStatus = "indeterminate" -) - -// ReferenceReport records one reference digest outcome and its completed transform -// steps. -type ReferenceReport struct { - // Index is the zero-based reference position within its SignedInfo or Manifest. - Index int `json:"index"` - // URI preserves the signed reference URI spelling. - URI string `json:"uri"` - // ResourceURI is the authorized resolver URI; URI preserves signed spelling. - ResourceURI string `json:"resource_uri,omitempty"` - // Status records the reference digest check outcome. - Status CheckStatus `json:"status"` - // Stage identifies the failed reference processing stage, when present. - Stage string `json:"stage,omitempty"` - // Error describes the reference failure, or is empty on success. - Error string `json:"error,omitempty"` - // Steps records completed transforms and implicit conversions. - Steps []reference.Step `json:"steps,omitempty"` -} - -// VerificationReport contains signature, reference and Manifest diagnostics. Check the -// returned error before accepting any result. -type VerificationReport struct { - // ManifestValidation records the requested Manifest validation outcome. - ManifestValidation CheckStatus `json:"manifest_validation"` - // Manifests contains diagnostics for traversed Manifests in recursive mode. - Manifests []ManifestReport `json:"manifests,omitempty"` - // Status records the overall signature verification outcome. - Status CheckStatus `json:"status"` - // SignatureValue records the cryptographic signature check outcome. - SignatureValue CheckStatus `json:"signature_value"` - // SignatureMethod is the declared XMLDSig signature algorithm URI. - SignatureMethod string `json:"signature_method,omitempty"` - // References contains direct SignedInfo reference diagnostics in document order. - References []ReferenceReport `json:"references,omitempty"` - // FailureStage identifies the stage at which verification failed, when present. - FailureStage string `json:"failure_stage,omitempty"` - // Error describes the verification failure, or is empty on success. - Error string `json:"error,omitempty"` -} - -// Signatures lists UNTRUSTED handles in document order, including nested ones. -// Neither this enumeration nor selecting a handle establishes trust. -func Signatures(doc *document.Document) []document.Element { - if doc == nil || !doc.Root().Valid() { - return nil - } - - found := findSignatures(doc) - - out := make([]document.Element, len(found)) - for i, n := range found { - out[i] = tree.Wrap(doc, n) - } - - return out -} - type coreReference struct { uri string typeURI string @@ -595,7 +627,7 @@ type corePlan struct { func (plan *corePlan) prepareReferences(ctx context.Context, nodes []*tree.Node, opt SignatureOptions, template bool, report *VerificationReport) error { report.References = make([]ReferenceReport, len(nodes)) for i, n := range nodes { - uri, _ := getAttr(n, document.Name{Local: "URI"}) + uri, _ := getAttr(n, document.Name{Local: lexicon.SecurityAttrURI}) report.References[i] = ReferenceReport{Index: i, URI: uri, Status: NotChecked} } @@ -607,7 +639,7 @@ func (plan *corePlan) prepareReferences(ctx context.Context, nodes []*tree.Node, for i, n := range nodes { ref, err := parseCoreReference(ctx, n, plan.policy, plan.processing, template) if err != nil { - return &ReferenceError{i, ref.uri, "structure", err} + return &ReferenceError{i, report.References[i].URI, stageStructure, err} } if opt.ExpectedURIs != nil { @@ -631,17 +663,17 @@ func (plan *corePlan) prepareReferences(ctx context.Context, nodes []*tree.Node, func parseCoreReference(ctx context.Context, n *tree.Node, p Policy, opt reference.Options, template bool) (coreReference, error) { var ref coreReference - children, err := structure(n, lexicon.DSigElementReference, "URI", "Id", "Type") + children, err := structure(n, lexicon.DSigElementReference, lexicon.SecurityAttrURI, lexicon.SecurityAttrID, lexicon.SecurityAttrType) if err != nil { return ref, err } - ref.uri, err = requiredAttribute(n, "URI") + ref.uri, err = requiredAttribute(n, lexicon.SecurityAttrURI) if err != nil { return ref, err } - ref.typeURI, _ = getAttr(n, document.Name{Local: "Type"}) + ref.typeURI, _ = getAttr(n, document.Name{Local: lexicon.SecurityAttrType}) if len(children) > 0 && isDS(children[0], lexicon.DSigElementTransforms) { ref.transforms, err = parseCoreTransforms(ctx, children[0], opt) @@ -693,7 +725,7 @@ func parseCoreTransforms(ctx context.Context, n *tree.Node, opt reference.Option return nil, err } - if len(ts) == 0 || len(ts) > 64 { + if len(ts) == 0 || len(ts) > maximumReferenceTransforms { return nil, fmt.Errorf("%w: Transforms count", ErrStructure) } @@ -716,7 +748,7 @@ func parseCoreTransform(ctx context.Context, tr *tree.Node, opt reference.Option } func parseTransform(ctx context.Context, tr *tree.Node, doc *document.Document) (reference.Transform, error) { - algorithm, err := requiredAttribute(tr, "Algorithm") + algorithm, err := requiredAttribute(tr, lexicon.SecurityAttrAlgorithm) if err != nil { return reference.Transform{}, err } diff --git a/xmldsig/core_test.go b/xmldsig/core_test.go index d456530..cec416f 100644 --- a/xmldsig/core_test.go +++ b/xmldsig/core_test.go @@ -62,7 +62,7 @@ func testLegacyXMLDSigFixture(t *testing.T, name, method string) { t.Fatal("default accepted legacy", err) } - opt := xmldsig.SignatureOptions{Policy: xmldsig.LegacyVerificationPolicy(), ExpectedURIs: []string{"#root", nameData}, RequiredElements: []document.Element{d.Root(), d.Root().Children()[0]}} + opt := xmldsig.SignatureOptions{Policy: xmldsig.LegacyVerificationPolicy(), ExpectedURIs: []string{"#root", dataReferenceURI}, RequiredElements: []document.Element{d.Root(), d.Root().Children()[0]}} result, report, err := xmldsig.VerifySignatureDetailed(t.Context(), d, selected, key, opt) if err != nil || result == nil || report == nil { @@ -275,17 +275,17 @@ func TestCoreForms(t *testing.T) { bytes string canonical bool }{ - {"enveloping", coreTemplate("s", xmldsig.ECDSASHA256, c14n.Exclusive, coreRef(nameData, c14n.Exclusive), obj), nameObject, nameData2, false, true, "", true}, - {"sibling", `original` + coreTemplate("s", xmldsig.ECDSASHA256, c14n.Exclusive, coreRef(nameData, c14n.Exclusive), "") + `mutable`, "item", nameData2, false, true, "", true}, - {"nested", `original` + coreTemplate("s", xmldsig.ECDSASHA256, c14n.Exclusive, coreRef(nameData, c14n.Canonical11), "") + ``, "item", nameData2, false, true, "", true}, - {"whole", `original` + coreTemplate("s", xmldsig.ECDSASHA256, c14n.Exclusive, coreRef("", xmldsig.EnvelopedSignature, c14n.Exclusive), "") + ``, nameRoot, nameData2, true, true, "", true}, - {"root-id", `` + coreTemplate("s", xmldsig.ECDSASHA256, c14n.Exclusive, coreRef(nameData, xmldsig.EnvelopedSignature, c14n.Exclusive), "") + ``, nameRoot, nameData2, true, true, "", true}, - {"xpointer-document", `` + coreTemplate("s", xmldsig.ECDSASHA256, c14n.Exclusive, coreRef("#xpointer(/)", xmldsig.EnvelopedSignature, c14n.Canonical11WithComments), "") + ``, nameRoot, nameData2, true, true, "", true}, - {"xpointer-id", coreTemplate("s", xmldsig.ECDSASHA256, c14n.Exclusive, coreRef("#xpointer(id('data'))", c14n.ExclusiveWithComments), obj), nameObject, nameData2, false, true, "", true}, - {"implicit-c14n", coreTemplate("s", xmldsig.ECDSASHA256, c14n.Exclusive, coreRef(nameData), obj), nameObject, nameData2, false, true, "", true}, - {"base64", coreTemplate("s", xmldsig.ECDSASHA256, c14n.Exclusive, coreRef(nameData, reference.Base64), `aGVsbG8=`), nameObject, nameData2, false, false, "hello", false}, - {"base64-xml", coreTemplate("s", xmldsig.ECDSASHA256, c14n.Exclusive, coreRef(nameData, reference.Base64, c14n.Canonical11), ``+base64.StdEncoding.EncodeToString([]byte(``))+``), nameObject, nameData2, false, false, "", true}, - {"reparse", coreTemplate("s", xmldsig.ECDSASHA256, c14n.Exclusive, coreRef(nameData, c14n.Exclusive, c14n.Exclusive), obj), nameObject, nameData2, false, false, "", true}, + {"enveloping", coreTemplate("s", xmldsig.ECDSASHA256, c14n.Exclusive, coreRef(dataReferenceURI, c14n.Exclusive), obj), nameObject, dataLiteral, false, true, "", true}, + {"sibling", `original` + coreTemplate("s", xmldsig.ECDSASHA256, c14n.Exclusive, coreRef(dataReferenceURI, c14n.Exclusive), "") + `mutable`, "item", dataLiteral, false, true, "", true}, + {"nested", `original` + coreTemplate("s", xmldsig.ECDSASHA256, c14n.Exclusive, coreRef(dataReferenceURI, c14n.Canonical11), "") + ``, "item", dataLiteral, false, true, "", true}, + {"whole", `original` + coreTemplate("s", xmldsig.ECDSASHA256, c14n.Exclusive, coreRef("", xmldsig.EnvelopedSignature, c14n.Exclusive), "") + ``, nameRoot, dataLiteral, true, true, "", true}, + {"root-id", `` + coreTemplate("s", xmldsig.ECDSASHA256, c14n.Exclusive, coreRef(dataReferenceURI, xmldsig.EnvelopedSignature, c14n.Exclusive), "") + ``, nameRoot, dataLiteral, true, true, "", true}, + {"xpointer-document", `` + coreTemplate("s", xmldsig.ECDSASHA256, c14n.Exclusive, coreRef("#xpointer(/)", xmldsig.EnvelopedSignature, c14n.Canonical11WithComments), "") + ``, nameRoot, dataLiteral, true, true, "", true}, + {"xpointer-id", coreTemplate("s", xmldsig.ECDSASHA256, c14n.Exclusive, coreRef("#xpointer(id('data'))", c14n.ExclusiveWithComments), obj), nameObject, dataLiteral, false, true, "", true}, + {"implicit-c14n", coreTemplate("s", xmldsig.ECDSASHA256, c14n.Exclusive, coreRef(dataReferenceURI), obj), nameObject, dataLiteral, false, true, "", true}, + {"base64", coreTemplate("s", xmldsig.ECDSASHA256, c14n.Exclusive, coreRef(dataReferenceURI, reference.Base64), `aGVsbG8=`), nameObject, dataLiteral, false, false, "hello", false}, + {"base64-xml", coreTemplate("s", xmldsig.ECDSASHA256, c14n.Exclusive, coreRef(dataReferenceURI, reference.Base64, c14n.Canonical11), ``+base64.StdEncoding.EncodeToString([]byte(``))+``), nameObject, dataLiteral, false, false, "", true}, + {"reparse", coreTemplate("s", xmldsig.ECDSASHA256, c14n.Exclusive, coreRef(dataReferenceURI, c14n.Exclusive, c14n.Exclusive), obj), nameObject, dataLiteral, false, false, "", true}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { testCoreFormsCase(t, &tc, &key) }) @@ -388,8 +388,8 @@ func TestCoreAlgorithmsAndCanonicalization(t *testing.T) { for i, tc := range cases { for j, cm := range cms { t.Run(fmt.Sprintf("key%d-cm%d", i, j), func(t *testing.T) { - d := coreParse(t, `original`+coreTemplate("s", tc.method, cm, coreRef(nameData, cm), "")+``) - opt := xmldsig.SignatureOptions{AllowedCanonicalizationMethods: []string{cm}, ExpectedURIs: []string{nameData}} + d := coreParse(t, `original`+coreTemplate("s", tc.method, cm, coreRef(dataReferenceURI, cm), "")+``) + opt := xmldsig.SignatureOptions{AllowedCanonicalizationMethods: []string{cm}, ExpectedURIs: []string{dataReferenceURI}} signed := coreSign(t, d, tc.key, opt) if _, err := xmldsig.VerifySignature(t.Context(), signed, xmldsig.Signatures(signed)[0], tc.key.Public(), opt); err != nil { @@ -408,11 +408,11 @@ func TestCoreMultisignaturesAndPreservation(t *testing.T) { } t.Run("independent", func(t *testing.T) { - d := coreParse(t, `payload`+tmpl("s1", nameData, c14n.Exclusive)+tmpl("s2", nameData, c14n.Canonical11)+``) + d := coreParse(t, `payload`+tmpl("s1", dataReferenceURI, c14n.Exclusive)+tmpl("s2", dataReferenceURI, c14n.Canonical11)+``) first := coreSign(t, d, key1, xmldsig.SignatureOptions{}) sigs := xmldsig.Signatures(first) - target := coreFind(first, "item", nameData2) - ownOpt := xmldsig.SignatureOptions{RequiredElements: []document.Element{target}, ExpectedURIs: []string{nameData}} + target := coreFind(first, "item", dataLiteral) + ownOpt := xmldsig.SignatureOptions{RequiredElements: []document.Element{target}, ExpectedURIs: []string{dataReferenceURI}} denied, err := xmldsig.SignTemplate(ctx, first, sigs[1], key2, xmldsig.TemplateSignOptions{Validation: ownOpt}) if denied != nil || !errors.Is(err, xmldsig.ErrPolicy) { @@ -428,7 +428,7 @@ func TestCoreMultisignaturesAndPreservation(t *testing.T) { for i, key := range []*ecdsa.PrivateKey{key1, key2} { r, err := xmldsig.VerifySignature(ctx, both, xmldsig.Signatures(both)[i], &key.PublicKey, xmldsig.SignatureOptions{}) - if err != nil || !r.CoversElement(coreFind(both, "item", nameData2)) { + if err != nil || !r.CoversElement(coreFind(both, "item", dataLiteral)) { t.Fatal(err) } } @@ -438,6 +438,8 @@ func TestCoreMultisignaturesAndPreservation(t *testing.T) { } // Wrong key for the chosen signature is not rescued by another signature. r, report, err := xmldsig.VerifySignatureDetailed(ctx, both, xmldsig.Signatures(both)[1], &key1.PublicKey, xmldsig.SignatureOptions{}) + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if r != nil || err == nil || report.SignatureValue != xmldsig.Invalid { t.Fatal("another signature/key trusted") } @@ -454,7 +456,7 @@ func TestCoreMultisignaturesAndPreservation(t *testing.T) { } }) t.Run("would-invalidate", func(t *testing.T) { - d := coreParse(t, `payload`+tmpl("s1", "", xmldsig.EnvelopedSignature, c14n.Exclusive)+tmpl("s2", nameData, c14n.Exclusive)+``) + d := coreParse(t, `payload`+tmpl("s1", "", xmldsig.EnvelopedSignature, c14n.Exclusive)+tmpl("s2", dataReferenceURI, c14n.Exclusive)+``) first := coreSign(t, d, key1, xmldsig.SignatureOptions{}) before := coreXML(t, first) sigs := xmldsig.Signatures(first) @@ -523,6 +525,8 @@ func TestCoreDiagnosticsAndWrapping(t *testing.T) { bad := coreParse(t, strings.Replace(coreXML(t, d), `URI="#second"`, `URI="https://invalid.example/never-fetched"`, 1)) r, report, err := xmldsig.VerifySignatureDetailed(ctx, bad, xmldsig.Signatures(bad)[0], &key.PublicKey, xmldsig.SignatureOptions{}) + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if r != nil || !errors.Is(err, xmldsig.ErrPolicy) || report.SignatureValue != xmldsig.NotChecked || report.References[1].Status != xmldsig.Disallowed { t.Fatal(err, report) } @@ -531,12 +535,12 @@ func TestCoreDiagnosticsAndWrapping(t *testing.T) { func TestCoreRepeatedURIAndExpectedSet(t *testing.T) { key := coreEC(t) - refs := coreRef(nameData, c14n.Exclusive) + coreRef(nameData, c14n.Canonical11) + refs := coreRef(dataReferenceURI, c14n.Exclusive) + coreRef(dataReferenceURI, c14n.Canonical11) d := coreParse(t, coreTemplate("s", xmldsig.ECDSASHA256, c14n.Exclusive, refs, `payload`)) - opt := xmldsig.SignatureOptions{ExpectedURIs: []string{nameData, nameData}} + opt := xmldsig.SignatureOptions{ExpectedURIs: []string{dataReferenceURI, dataReferenceURI}} signed := coreSign(t, d, key, opt) - for _, uris := range [][]string{{nameData}, {nameData, "#other"}, {}, {nameData, nameData, nameData}} { + for _, uris := range [][]string{{dataReferenceURI}, {dataReferenceURI, "#other"}, {}, {dataReferenceURI, dataReferenceURI, dataReferenceURI}} { r, e := xmldsig.VerifySignature(t.Context(), signed, xmldsig.Signatures(signed)[0], &key.PublicKey, xmldsig.SignatureOptions{ExpectedURIs: uris}) if r != nil || !errors.Is(e, xmldsig.ErrPolicy) { t.Fatal("URI multiset accepted", e) @@ -593,7 +597,7 @@ func TestCoreTemplateSafety(t *testing.T) { } } }) - base := coreParse(t, coreTemplate("s", xmldsig.ECDSASHA256, c14n.Exclusive, coreRef(nameData, c14n.Exclusive), `payload`)) + base := coreParse(t, coreTemplate("s", xmldsig.ECDSASHA256, c14n.Exclusive, coreRef(dataReferenceURI, c14n.Exclusive), `payload`)) signed := coreSign(t, base, key, xmldsig.SignatureOptions{}) t.Run("no-overwrite", func(t *testing.T) { out, e := xmldsig.SignTemplate(ctx, signed, xmldsig.Signatures(signed)[0], key, xmldsig.TemplateSignOptions{}) @@ -610,7 +614,7 @@ func TestCoreTemplateSafety(t *testing.T) { } }) t.Run("untyped-id-is-not-guessed", func(t *testing.T) { - bad := coreParse(t, ``+coreTemplate("s", xmldsig.ECDSASHA256, c14n.Exclusive, coreRef(nameData, c14n.Exclusive), "")+``) + bad := coreParse(t, ``+coreTemplate("s", xmldsig.ECDSASHA256, c14n.Exclusive, coreRef(dataReferenceURI, c14n.Exclusive), "")+``) out, e := xmldsig.SignTemplate(ctx, bad, xmldsig.Signatures(bad)[0], key, xmldsig.TemplateSignOptions{}) if out != nil || !errors.Is(e, reference.ErrInput) { @@ -647,7 +651,7 @@ func TestCoreTemplateSafety(t *testing.T) { func TestCoreInvalidOptions(t *testing.T) { key := coreEC(t) ctx := t.Context() - base := coreParse(t, coreTemplate("s", xmldsig.ECDSASHA256, c14n.Exclusive, coreRef(nameData, c14n.Exclusive), `payload`)) + base := coreParse(t, coreTemplate("s", xmldsig.ECDSASHA256, c14n.Exclusive, coreRef(dataReferenceURI, c14n.Exclusive), `payload`)) d := coreSign(t, base, key, xmldsig.SignatureOptions{}) sig := xmldsig.Signatures(d)[0] @@ -706,14 +710,14 @@ func TestCoreCommentAndManifestSemantics(t *testing.T) { key := coreEC(t) ctx := t.Context() - for _, uri := range []string{nameData, "#xpointer(id('data'))"} { + for _, uri := range []string{dataReferenceURI, "#xpointer(id('data'))"} { t.Run(uri, func(t *testing.T) { raw := coreTemplate("s", xmldsig.ECDSASHA256, c14n.Exclusive, coreRef(uri, c14n.ExclusiveWithComments), `text`) signed := coreSign(t, coreParse(t, raw), key, xmldsig.SignatureOptions{}) bad := coreParse(t, strings.Replace(coreXML(t, signed), "", "", 1)) r, e := xmldsig.VerifySignature(ctx, bad, xmldsig.Signatures(bad)[0], &key.PublicKey, xmldsig.SignatureOptions{}) - if uri == nameData { + if uri == dataReferenceURI { if e != nil { t.Fatal(e) } @@ -794,14 +798,9 @@ func FuzzVerifySignature(f *testing.F) { }) } -func coreFixtureKey(t testing.TB, name string) crypto.PublicKey { - t.Helper() - return testutil.ReadPublicKey(t, filepath.Join("..", "testdata", "core", name)) -} - func FuzzSignTemplate(f *testing.F) { key := coreEC(f) - f.Add([]byte(coreTemplate("s", xmldsig.ECDSASHA256, "http://www.w3.org/2001/10/xml-exc-c14n#", coreRef(nameData, "http://www.w3.org/2001/10/xml-exc-c14n#"), `payload`))) + f.Add([]byte(coreTemplate("s", xmldsig.ECDSASHA256, "http://www.w3.org/2001/10/xml-exc-c14n#", coreRef(dataReferenceURI, "http://www.w3.org/2001/10/xml-exc-c14n#"), `payload`))) f.Fuzz(func(t *testing.T, raw []byte) { if len(raw) > 16<<10 { t.Skip() @@ -843,6 +842,11 @@ func FuzzSignTemplate(f *testing.F) { }) } +func coreFixtureKey(t testing.TB, name string) crypto.PublicKey { + t.Helper() + return testutil.ReadPublicKey(t, filepath.Join("..", "testdata", "core", name)) +} + func coreParse(t testing.TB, s string) *document.Document { t.Helper() diff --git a/xmldsig/detached.go b/xmldsig/detached.go index 28ff84c..11a29a6 100644 --- a/xmldsig/detached.go +++ b/xmldsig/detached.go @@ -21,6 +21,11 @@ import ( "github.com/skarm/xmlsec/reference" ) +const ( + defaultMaxResourceBytes = 8 << 20 + defaultMaxTotalResourceBytes = 32 << 20 +) + // Resolver supplies original octets for an explicitly authorized URI. The library // does not automatically fetch HTTP or file resources or select XML fragments. // ExternalReferences.BaseURI enables relative URI resolution only when configured @@ -110,6 +115,71 @@ type DetachedResult struct { references []ReferenceResult } +// SignDetached produces a standalone ds:Signature, with one or more external +// octet-stream references. ALL recipes are checked before any resolver call. +// The same processing engine is used by SignEnveloped, Verify and VerifyDetached. +func SignDetached(ctx context.Context, refs []DetachedReference, signer crypto.Signer, resolver Resolver, opt DetachedSignOptions) (*document.Document, error) { + if ctx == nil { + return nil, fmt.Errorf("%w: nil context", ErrPolicy) + } + + if err := ctx.Err(); err != nil { + return nil, err + } + + if nilcheck.IsNil(signer) { + return nil, ErrKey + } + + if nilcheck.IsNil(resolver) { + return nil, fmt.Errorf("%w: resolver required", ErrReference) + } + + p, err := prepareDetachedSigning(refs, signer, opt) + if err != nil { + return nil, err + } + + draft, err := buildDetachedSignature(ctx, p) + if err != nil { + return nil, err + } + + remaining := p.policy.MaxCanonicalBytes + if err := fillDetachedDigests(ctx, draft, p, resolver, &remaining); err != nil { + return nil, err + } + + spec := signatureSpec{algorithm: p.method} + credential := signatureCredential{public: p.key, signer: signer} + + canonicalSI, err := credential.prepareInput(ctx, spec, func(dst io.Writer) error { + return writeCanonicalElement(ctx, dst, tree.Wrap(draft.doc, draft.si), p.cm, &remaining) + }) + if err != nil { + return nil, err + } + + if err := ctx.Err(); err != nil { + return nil, err + } + + value, err := credential.signPrepared(ctx, spec, p.policy, canonicalSI) + if err != nil { + return nil, err + } + + if err := ctx.Err(); err != nil { + return nil, err + } + + sv := newDS(lexicon.DSigElementSignatureValue) + text(sv, base64.StdEncoding.EncodeToString(value)) + add(draft.sig, sv) + + return tree.Reparse(ctx, draft.doc, p.xmlLimits) +} + // VerifyDetached authenticates SignedInfo BEFORE calling resolver. It checks the // whole expected reference set and every digest. KeyInfo is ignored for trust. // Any failure returns nil, never a partially authenticated DetachedResult. @@ -269,7 +339,7 @@ func verifyDetachedResources(ctx context.Context, resolver Resolver, p *detached } if subtle.ConstantTimeCompare(hash.Sum(nil), ref.digest) != 1 { - return &ReferenceError{i, ref.URI, "digest", ErrDigest} + return &ReferenceError{i, ref.URI, stageDigest, ErrDigest} } result.references = append(result.references, ReferenceResult{uri: ref.URI, typeURI: ref.Type, digestMethod: ref.DigestMethod, processed: processed, external: source, processing: p.processing, digest: hash.Sum(nil), transforms: ref.Transforms}) @@ -312,71 +382,6 @@ type detachedSignatureDraft struct { digestNodes []*tree.Node } -// SignDetached produces a standalone ds:Signature, with one or more external -// octet-stream references. ALL recipes are checked before any resolver call. -// The same processing engine is used by SignEnveloped, Verify and VerifyDetached. -func SignDetached(ctx context.Context, refs []DetachedReference, signer crypto.Signer, resolver Resolver, opt DetachedSignOptions) (*document.Document, error) { - if ctx == nil { - return nil, fmt.Errorf("%w: nil context", ErrPolicy) - } - - if err := ctx.Err(); err != nil { - return nil, err - } - - if nilcheck.IsNil(signer) { - return nil, ErrKey - } - - if nilcheck.IsNil(resolver) { - return nil, fmt.Errorf("%w: resolver required", ErrReference) - } - - p, err := prepareDetachedSigning(refs, signer, opt) - if err != nil { - return nil, err - } - - draft, err := buildDetachedSignature(ctx, p) - if err != nil { - return nil, err - } - - remaining := p.policy.MaxCanonicalBytes - if err := fillDetachedDigests(ctx, draft, p, resolver, &remaining); err != nil { - return nil, err - } - - spec := signatureSpec{algorithm: p.method} - credential := signatureCredential{public: p.key, signer: signer} - - canonicalSI, err := credential.prepareInput(ctx, spec, func(dst io.Writer) error { - return writeCanonicalElement(ctx, dst, tree.Wrap(draft.doc, draft.si), p.cm, &remaining) - }) - if err != nil { - return nil, err - } - - if err := ctx.Err(); err != nil { - return nil, err - } - - value, err := credential.signPrepared(ctx, spec, canonicalSI) - if err != nil { - return nil, err - } - - if err := ctx.Err(); err != nil { - return nil, err - } - - sv := newDS(lexicon.DSigElementSignatureValue) - text(sv, base64.StdEncoding.EncodeToString(value)) - add(draft.sig, sv) - - return tree.Reparse(ctx, draft.doc, p.xmlLimits) -} - func normalizeCanonicalization(tr reference.Transform) (reference.Transform, error) { if tr.Algorithm == "" { tr.Algorithm = c14n.Exclusive @@ -440,6 +445,15 @@ func prepareDetachedSigning(refs []DetachedReference, signer crypto.Signer, opt return nil, err } + if _, mac := macHash(method); mac || method == RSAPSS { + return nil, ErrAlgorithm + } + + credential := signatureCredential{public: key, signer: signer} + if err := credential.validateSigner(signatureSpec{algorithm: method}, p); err != nil { + return nil, err + } + if err := validateDetachedReferenceCount(refs, p.MaxReferences); err != nil { return nil, err } @@ -492,18 +506,18 @@ func normalizeDetachedReferences(refs []DetachedReference, p Policy, processing // buildDetachedSignature validates all serialized metadata before resource I/O. func buildDetachedSignature(ctx context.Context, p *detachedSigningPlan) (*detachedSignatureDraft, error) { sig := newDS(lexicon.DSigElementSignature) - sig.NS = []tree.Namespace{{Prefix: "ds", URI: Namespace}} + sig.NS = []tree.Namespace{{Prefix: lexicon.PrefixDSig, URI: Namespace}} doc := tree.FromNodes([]*tree.Node{sig}, p.xmlLimits) si := newDS(lexicon.DSigElementSignedInfo) add(sig, si) methodNode := newDS(lexicon.DSigElementCanonicalizationMethod) - setAttr(methodNode, "Algorithm", p.cm.Algorithm) + setAttr(methodNode, lexicon.SecurityAttrAlgorithm, p.cm.Algorithm) addInclusive(methodNode, p.cm.InclusivePrefixes) add(si, methodNode) sm := newDS(lexicon.DSigElementSignatureMethod) - setAttr(sm, "Algorithm", p.method) + setAttr(sm, lexicon.SecurityAttrAlgorithm, p.method) add(si, sm) // Build ALL metadata before resolving any data. Reparse the temporary template // to catch invalid Type strings, prefixes, and document limits before I/O. @@ -585,11 +599,11 @@ func validateDetachedReferenceCount(refs []DetachedReference, maximum int) error func normalizeResourceLimits(l ResourceLimits) (ResourceLimits, error) { if l.MaxResourceBytes == 0 { - l.MaxResourceBytes = 8 << 20 + l.MaxResourceBytes = defaultMaxResourceBytes } if l.MaxTotalResourceBytes == 0 { - l.MaxTotalResourceBytes = 32 << 20 + l.MaxTotalResourceBytes = defaultMaxTotalResourceBytes } if l.MaxResourceBytes < 1 || l.MaxTotalResourceBytes < 1 { @@ -670,7 +684,7 @@ func (p *detachedVerificationPlan) parseSignature(ctx context.Context, doc *docu return fmt.Errorf("%w: standalone Signature required", ErrStructure) } - children, err := structure(sig, lexicon.DSigElementSignature, "Id") + children, err := structure(sig, lexicon.DSigElementSignature, lexicon.SecurityAttrID) if err != nil { return err } @@ -681,7 +695,7 @@ func (p *detachedVerificationPlan) parseSignature(ctx context.Context, doc *docu si := children[0] - signed, err := structure(si, lexicon.DSigElementSignedInfo, "Id") + signed, err := structure(si, lexicon.DSigElementSignedInfo, lexicon.SecurityAttrID) if err != nil { return err } @@ -712,16 +726,16 @@ func (p *detachedVerificationPlan) parseSignature(ctx context.Context, doc *docu return fmt.Errorf("%w: signature method", ErrPolicy) } - value, err := base64Value(ctx, children[1], lexicon.DSigElementSignatureValue, 2048, "Id") + value, err := base64Value(ctx, children[1], lexicon.DSigElementSignatureValue, maximumSignatureBytes, lexicon.SecurityAttrID) if err != nil { return err } - if len(signed)-2 != len(p.expected) { + if len(signed)-signedInfoHeaderChildren != len(p.expected) { return fmt.Errorf("%w: expected reference set size", ErrPolicy) } - refs, err := parseDetachedReferenceSet(ctx, doc, signed[2:], p.policy, p.processing, p.expected) + refs, err := parseDetachedReferenceSet(ctx, doc, signed[signedInfoHeaderChildren:], p.policy, p.processing, p.expected) if err != nil { return err } @@ -734,12 +748,12 @@ func (p *detachedVerificationPlan) parseSignature(ctx context.Context, doc *docu func parseDetachedReference(ctx context.Context, doc *document.Document, n *tree.Node, p Policy, processing reference.Options) (parsedDetachedReference, error) { var ref parsedDetachedReference - children, err := structure(n, lexicon.DSigElementReference, "URI", "Id", "Type") + children, err := structure(n, lexicon.DSigElementReference, lexicon.SecurityAttrURI, lexicon.SecurityAttrID, lexicon.SecurityAttrType) if err != nil { return ref, err } - ref.URI, err = requiredAttribute(n, "URI") + ref.URI, err = requiredAttribute(n, lexicon.SecurityAttrURI) if err != nil { return ref, err } @@ -748,7 +762,7 @@ func parseDetachedReference(ctx context.Context, doc *document.Document, n *tree return ref, err } - ref.Type, _ = getAttr(n, document.Name{Local: "Type"}) + ref.Type, _ = getAttr(n, document.Name{Local: lexicon.SecurityAttrType}) if len(children) != 0 && isDS(children[0], lexicon.DSigElementTransforms) { transforms, err := structure(children[0], lexicon.DSigElementTransforms) @@ -756,7 +770,7 @@ func parseDetachedReference(ctx context.Context, doc *document.Document, n *tree return ref, err } // Schema requires at least one Transform when Transforms exists. - if len(transforms) == 0 || len(transforms) > 64 { + if len(transforms) == 0 || len(transforms) > maximumReferenceTransforms { return ref, fmt.Errorf("%w: Transforms count", ErrStructure) } @@ -813,7 +827,9 @@ func parseDetachedReferenceSet(ctx context.Context, doc *document.Document, node for i, n := range nodes { ref, err := parseDetachedReference(ctx, doc, n, policy, processing) if err != nil { - return nil, &ReferenceError{i, ref.URI, nameTransform, err} + uri, _ := getAttr(n, document.Name{Local: lexicon.SecurityAttrURI}) + + return nil, &ReferenceError{i, uri, nameTransform, err} } if _, exists := seen[ref.URI]; exists { @@ -832,7 +848,7 @@ func parseDetachedReferenceSet(ctx context.Context, doc *document.Document, node } func validateDetachedSignedInfo(signed []*tree.Node, maximum int) error { - if len(signed) < 3 || len(signed)-2 > maximum || !isDS(signed[0], lexicon.DSigElementCanonicalizationMethod) || !isDS(signed[1], lexicon.DSigElementSignatureMethod) { + if len(signed) < signedInfoMinimumChildren || len(signed)-signedInfoHeaderChildren > maximum || !isDS(signed[0], lexicon.DSigElementCanonicalizationMethod) || !isDS(signed[1], lexicon.DSigElementSignatureMethod) { return fmt.Errorf("%w: SignedInfo children", ErrStructure) } diff --git a/xmldsig/detached_test.go b/xmldsig/detached_test.go index 356790f..2fc1c18 100644 --- a/xmldsig/detached_test.go +++ b/xmldsig/detached_test.go @@ -7,6 +7,7 @@ import ( "crypto/ecdsa" "crypto/ed25519" "crypto/elliptic" + "crypto/fips140" "crypto/rand" "crypto/sha256" "encoding/asn1" @@ -377,6 +378,59 @@ func TestDetachedValidationBeforeSigningIO(t *testing.T) { } } +func TestDetachedSigningMethodPreflight(t *testing.T) { + key := rsaKey(t) + for _, test := range []struct { + name string + method string + policy xmldsig.Policy + want error + }{ + {"generic-PSS", xmldsig.RSAPSS, xmldsig.ParameterizedPolicy(), xmldsig.ErrAlgorithm}, + {"HMAC", xmldsig.HMACSHA256, xmldsig.HMACPolicy(), xmldsig.ErrAlgorithm}, + {"legacy-generation-denied", xmldsig.RSASHA1, xmldsig.LegacyVerificationPolicy(), xmldsig.ErrPolicy}, + } { + t.Run(test.name, func(t *testing.T) { + calls := 0 + resolver := xmldsig.ResolverFunc(func(context.Context, string) (io.ReadCloser, error) { + calls++ + return io.NopCloser(strings.NewReader("payload")), nil + }) + signer := &controlledSigner{Signer: key} + + out, err := xmldsig.SignDetached(t.Context(), []xmldsig.DetachedReference{{URI: uriUrnPayload}}, signer, resolver, xmldsig.DetachedSignOptions{SignatureMethod: test.method, Policy: test.policy}) + if out != nil || !errors.Is(err, test.want) || calls != 0 || signer.calls != 0 { + t.Fatalf("out=%t err=%v resolver=%d signer=%d", out != nil, err, calls, signer.calls) + } + }) + } +} + +func TestDetachedLegacyGenerationExplicitOptIn(t *testing.T) { + if fips140.Enabled() { + t.Skip("legacy compatibility is disabled in FIPS mode") + } + + key := rsaKey(t) + signer := &controlledSigner{Signer: key} + calls := 0 + resolver := xmldsig.ResolverFunc(func(context.Context, string) (io.ReadCloser, error) { + calls++ + return io.NopCloser(strings.NewReader("payload")), nil + }) + policy := xmldsig.LegacyVerificationPolicy() + policy.AllowLegacySigning = true + + out, err := xmldsig.SignDetached(t.Context(), []xmldsig.DetachedReference{{URI: uriUrnPayload}}, signer, resolver, xmldsig.DetachedSignOptions{SignatureMethod: xmldsig.RSASHA1, Policy: policy}) + if err != nil || out == nil || calls != 1 || signer.calls != 1 { + t.Fatalf("out=%t err=%v resolver=%d signer=%d", out != nil, err, calls, signer.calls) + } + + if _, err := xmldsig.VerifyDetached(t.Context(), out, key.Public(), resolver, xmldsig.DetachedVerifyOptions{ExpectedURIs: []string{uriUrnPayload}, Policy: policy}); err != nil { + t.Fatal(err) + } +} + func TestDetachedByteBudgetAndExternalSigner(t *testing.T) { key := ecKey(t, elliptic.P256()) adapter := &controlledSigner{Signer: key} @@ -751,6 +805,56 @@ func FuzzVerifyDetached(f *testing.F) { }) } +func TestCanonicalizedReferenceReadsAndTransformsOnce(t *testing.T) { + source := `text` + calls := 0 + resolver := xmldsig.ResolverFunc(func(context.Context, string) (io.ReadCloser, error) { + calls++ + value := source + if calls > 1 { + value = `changed` + } + return io.NopCloser(strings.NewReader(value)), nil + }) + program := reference.Transform{Algorithm: reference.XPath, XPath: []reference.XPathExpression{{Expression: "true()", Namespaces: map[string]string{"xml": document.XMLNamespace, "ds": xmldsig.Namespace}}}} + engine, err := transform.NewEngine(transform.Config{AllowedPrograms: []reference.Transform{program}}) + if err != nil { + t.Fatal(err) + } + options := xmldsig.SignatureOptions{External: &xmldsig.ExternalReferences{Resolver: resolver, AllowedURIs: []string{uriUrnXML}}, Processing: reference.DefaultOptions()} + options.Processing.Extension = engine + options.Processing.AllowedTransforms = append(options.Processing.AllowedTransforms, reference.XPath) + draft, selected, err := xmldsig.BuildTemplate(t.Context(), nil, document.Element{}, xmldsig.SignatureTemplate{SignatureMethod: xmldsig.ECDSASHA256, References: []xmldsig.ReferenceTemplate{{URI: uriUrnXML, Transforms: []reference.Transform{program}}}}) + if err != nil { + t.Fatal(err) + } + // Signing and its self-verification each require the same stable resource. + options.External.Resolver = xmldsig.ResolverFunc(func(context.Context, string) (io.ReadCloser, error) { + return io.NopCloser(strings.NewReader(source)), nil + }) + key := coreEC(t) + signed, err := xmldsig.SignTemplate(t.Context(), draft, selected, key, xmldsig.TemplateSignOptions{Validation: options}) + if err != nil { + t.Fatal(err) + } + options.External.Resolver = resolver + calls = 0 + checked, err := xmldsig.VerifySignature(t.Context(), signed, signed.Root(), key.Public(), options) + if err != nil { + t.Fatal(err) + } + calls = 0 + var alternate bytes.Buffer + _, err = checked.References()[0].WriteCanonicalizedInput(t.Context(), &alternate, reference.Transform{Algorithm: c14n.Exclusive}) + if err != nil || calls != 1 || alternate.String() != `text` { + t.Fatal(alternate.String(), calls, err) + } + alternate.Reset() + if _, err := checked.References()[0].WriteCanonicalizedInput(t.Context(), &alternate, reference.Transform{Algorithm: c14n.Exclusive}); !errors.Is(err, xmldsig.ErrDigest) { + t.Fatal("changed resource was accepted", err) + } +} + func loadDetachedResources(t testing.TB) ([]string, xmldsig.Resolver) { t.Helper() @@ -839,53 +943,3 @@ func (stuckReader) Read([]byte) (int, error) { return 0, nil } type badCountReader struct{} func (badCountReader) Read([]byte) (int, error) { return -1, nil } - -func TestCanonicalizedReferenceReadsAndTransformsOnce(t *testing.T) { - source := `text` - calls := 0 - resolver := xmldsig.ResolverFunc(func(context.Context, string) (io.ReadCloser, error) { - calls++ - value := source - if calls > 1 { - value = `changed` - } - return io.NopCloser(strings.NewReader(value)), nil - }) - program := reference.Transform{Algorithm: reference.XPath, XPath: []reference.XPathExpression{{Expression: "true()", Namespaces: map[string]string{"xml": document.XMLNamespace, "ds": xmldsig.Namespace}}}} - engine, err := transform.NewEngine(transform.Config{AllowedPrograms: []reference.Transform{program}}) - if err != nil { - t.Fatal(err) - } - options := xmldsig.SignatureOptions{External: &xmldsig.ExternalReferences{Resolver: resolver, AllowedURIs: []string{uriUrnXML}}, Processing: reference.DefaultOptions()} - options.Processing.Extension = engine - options.Processing.AllowedTransforms = append(options.Processing.AllowedTransforms, reference.XPath) - draft, selected, err := xmldsig.BuildTemplate(t.Context(), nil, document.Element{}, xmldsig.SignatureTemplate{SignatureMethod: xmldsig.ECDSASHA256, References: []xmldsig.ReferenceTemplate{{URI: uriUrnXML, Transforms: []reference.Transform{program}}}}) - if err != nil { - t.Fatal(err) - } - // Signing and its self-verification each require the same stable resource. - options.External.Resolver = xmldsig.ResolverFunc(func(context.Context, string) (io.ReadCloser, error) { - return io.NopCloser(strings.NewReader(source)), nil - }) - key := coreEC(t) - signed, err := xmldsig.SignTemplate(t.Context(), draft, selected, key, xmldsig.TemplateSignOptions{Validation: options}) - if err != nil { - t.Fatal(err) - } - options.External.Resolver = resolver - calls = 0 - checked, err := xmldsig.VerifySignature(t.Context(), signed, signed.Root(), key.Public(), options) - if err != nil { - t.Fatal(err) - } - calls = 0 - var alternate bytes.Buffer - _, err = checked.References()[0].WriteCanonicalizedInput(t.Context(), &alternate, reference.Transform{Algorithm: c14n.Exclusive}) - if err != nil || calls != 1 || alternate.String() != `text` { - t.Fatal(alternate.String(), calls, err) - } - alternate.Reset() - if _, err := checked.References()[0].WriteCanonicalizedInput(t.Context(), &alternate, reference.Transform{Algorithm: c14n.Exclusive}); !errors.Is(err, xmldsig.ErrDigest) { - t.Fatal("changed resource was accepted", err) - } -} diff --git a/xmldsig/doc.go b/xmldsig/doc.go index 023cc01..a6b9275 100644 --- a/xmldsig/doc.go +++ b/xmldsig/doc.go @@ -68,8 +68,8 @@ // can produce the same MAC. [LegacyVerificationPolicy] explicitly enables SHA-1 // reference digests and RSA-SHA1/DSA-SHA1 verification, with complete 1024/160 // DSA public parameters and fixed-width r || s signature values. SHA-1 reference -// generation is available under that policy, but RSA-SHA1/DSA-SHA1 signature -// generation is always rejected before calling a private signer. +// generation is available under that policy. RSA-SHA1 signature generation also +// requires Policy.AllowLegacySigning; DSA-SHA1 generation remains unsupported. // [LegacyHMACPolicy] separately opts into HMAC-SHA1 with the existing key/tag // bounds. Both legacy policies are disabled in FIPS mode; defaults remain SHA-2. // Approved XPath/Filter2/XSLT transforms use diff --git a/xmldsig/helpers_test.go b/xmldsig/helpers_test.go index a2c1f11..183e215 100644 --- a/xmldsig/helpers_test.go +++ b/xmldsig/helpers_test.go @@ -16,14 +16,14 @@ import ( ) const ( - programTrueXPath = "true()" - nameData = "#data" - namePayload = "#payload" - nameObject = "Object" - nameRoot = "root" - uriUrnAttachment = "urn:attachment" - uriUrnData = "urn:data" - nameData2 = "data" + programTrueXPath = "true()" + dataReferenceURI = "#data" + payloadReferenceURI = "#payload" + nameObject = "Object" + nameRoot = "root" + uriUrnAttachment = "urn:attachment" + uriUrnData = "urn:data" + dataLiteral = "data" ) func parse(t testing.TB, s string) *document.Document { diff --git a/xmldsig/hmac.go b/xmldsig/hmac.go index 32e22a0..61cba99 100644 --- a/xmldsig/hmac.go +++ b/xmldsig/hmac.go @@ -13,6 +13,11 @@ import ( "github.com/skarm/xmlsec/internal/streamio" ) +const ( + // Hard byte ceiling for an explicitly supplied HMAC key. + maximumHMACKeyBytes = 1 << 20 +) + // HMACResult authenticates references with a shared secret. Every holder of the // secret can produce the same MAC; it does NOT identify a unique signer or // provide non-repudiation. No key fingerprint, key bytes or key-derived identifier @@ -236,7 +241,7 @@ func (c signatureCredential) validate(s signatureSpec, p Policy) error { return s.validatePublicKey(c.public, p) } -func (c signatureCredential) validateSigner(s signatureSpec) error { +func (c signatureCredential) validateSigner(s signatureSpec, p Policy) error { if c.mac { return nil } @@ -245,6 +250,10 @@ func (c signatureCredential) validateSigner(s signatureSpec) error { return ErrKey } + if s.algorithm == RSASHA1 && !p.AllowLegacySigning { + return fmt.Errorf("%w: RSA-SHA1 generation requires AllowLegacySigning", ErrPolicy) + } + return s.validateSigner(c.signer) } @@ -262,7 +271,7 @@ func (c signatureCredential) prepareInput(ctx context.Context, s signatureSpec, } full := mac.Sum(nil) - n := s.hmacBits / 8 + n := s.hmacBits / signatureOctetBits clear(full[n:]) return full[:n:n], ctx.Err() @@ -331,7 +340,11 @@ func (c signatureCredential) verifyPrepared(s signatureSpec, digest, value []byt return s.verifyPrepared(c.public, digest, value) } -func (c signatureCredential) signPrepared(ctx context.Context, s signatureSpec, digest []byte) ([]byte, error) { +func (c signatureCredential) signPrepared(ctx context.Context, s signatureSpec, p Policy, digest []byte) ([]byte, error) { + if err := c.validateSigner(s, p); err != nil { + return nil, err + } + if err := ctx.Err(); err != nil { return nil, err } @@ -357,7 +370,7 @@ func (s signatureSpec) hash() crypto.Hash { // copySecret bounds allocation before cloning, independently of caller policy. // Key length is not a test of entropy. Applications must generate random secrets. func copySecret(secret []byte) ([]byte, error) { - if len(secret) < 32 || len(secret) > 1<<20 { + if len(secret) < minimumHMACKeyBytes || len(secret) > maximumHMACKeyBytes { return nil, fmt.Errorf("%w: HMAC secret length", ErrKey) } diff --git a/xmldsig/hmac_test.go b/xmldsig/hmac_test.go index 55ca2c2..bcfb3a7 100644 --- a/xmldsig/hmac_test.go +++ b/xmldsig/hmac_test.go @@ -73,7 +73,7 @@ func TestLegacyHMACSHA1(t *testing.T) { } d := coreParse(t, string(raw)) - opt := xmldsig.SignatureOptions{Policy: xmldsig.LegacyHMACPolicy(), ExpectedURIs: []string{"#root", nameData}, RequiredElements: []document.Element{d.Root()}} + opt := xmldsig.SignatureOptions{Policy: xmldsig.LegacyHMACPolicy(), ExpectedURIs: []string{"#root", dataReferenceURI}, RequiredElements: []document.Element{d.Root()}} result, err := xmldsig.VerifyHMACSignature(t.Context(), d, xmldsig.Signatures(d)[0], secret, opt) if err != nil || result == nil || len(result.References()) != 2 || !result.CoversElement(d.Root()) { @@ -213,6 +213,8 @@ func TestHMACTemplateChecksAllReferences(t *testing.T) { o.RequiredElements = nil r, report, e = xmldsig.VerifyHMACSignatureDetailed(ctx, bad, bad.Root(), key, o) + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if r != nil || !errors.Is(e, xmldsig.ErrDigest) || report.SignatureValue != xmldsig.Valid || report.References[0].Status != xmldsig.Valid || report.References[1].Status != xmldsig.Invalid { t.Fatalf("bad second ref %v %+v", e, report) } @@ -287,6 +289,8 @@ func TestHMACExternalAuthenticationGate(t *testing.T) { calls = 0 r, report, e := xmldsig.VerifyHMACSignatureDetailed(ctx, out, out.Root(), wrong, opt) + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if r != nil || !errors.Is(e, xmldsig.ErrSignature) || calls != 0 || report.References[0].Status != xmldsig.NotChecked { t.Fatalf("read before MAC %v calls=%d %+v", e, calls, report) } @@ -300,7 +304,7 @@ func TestHMACPreservationAndManifests(t *testing.T) { ctx := t.Context() key := testMACKey() root := coreParse(t, `value`) - template := xmldsig.SignatureTemplate{SignatureMethod: xmldsig.HMACSHA256, References: []xmldsig.ReferenceTemplate{{URI: nameData, Transforms: []reference.Transform{{Algorithm: c14n.Exclusive}}}}} + template := xmldsig.SignatureTemplate{SignatureMethod: xmldsig.HMACSHA256, References: []xmldsig.ReferenceTemplate{{URI: dataReferenceURI, Transforms: []reference.Transform{{Algorithm: c14n.Exclusive}}}}} d, s, e := xmldsig.BuildTemplate(ctx, root, root.Root(), template) if e != nil { @@ -441,6 +445,8 @@ func TestHMACRequiresExplicitCoverageAndTrustKind(t *testing.T) { o := xmldsig.SignatureOptions{Policy: p, RequiredElements: []document.Element{signed.Root()}} r, report, e := xmldsig.VerifyHMACSignatureDetailed(ctx, signed, signed.Root(), key, o) + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if r != nil || !errors.Is(e, xmldsig.ErrPolicy) || report.FailureStage != "coverage" { t.Fatalf("root was trusted %v", e) } diff --git a/xmldsig/keyinfo_test.go b/xmldsig/keyinfo_test.go index d044dd2..fad05a1 100644 --- a/xmldsig/keyinfo_test.go +++ b/xmldsig/keyinfo_test.go @@ -217,6 +217,8 @@ func testStage4VectorMutations(t *testing.T, test stage4Case, key crypto.PublicK result, report, err := xmldsig.VerifySignatureDetailed(t.Context(), doc, xmldsig.Signatures(doc)[0], key, opt) test.Resources[uriUrnAttachment] = original + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if result != nil || !errors.Is(err, xmldsig.ErrDigest) || report.References[0].Status != xmldsig.Valid || report.ManifestValidation == xmldsig.Valid { t.Fatal("independent nested-content mutation", report, err) } @@ -290,7 +292,7 @@ func TestVerifySignatureWithExplicitKeyInfoTrust(t *testing.T) { signed, sel := makeKeyInfoSigned(t, k, ki) before := coreXML(t, signed) - opt := xmldsig.KeyInfoVerifyOptions{Signature: xmldsig.SignatureOptions{ExpectedURIs: []string{namePayload}, RequiredElements: []document.Element{signed.Root().Children()[0]}}, SelectTrustedKey: keySelector(t, &k.PublicKey)} + opt := xmldsig.KeyInfoVerifyOptions{Signature: xmldsig.SignatureOptions{ExpectedURIs: []string{payloadReferenceURI}, RequiredElements: []document.Element{signed.Root().Children()[0]}}, SelectTrustedKey: keySelector(t, &k.PublicKey)} r, e := xmldsig.VerifySignatureWithKeyInfo(t.Context(), signed, sel, opt) if e != nil || !r.CoversElement(signed.Root().Children()[0]) || r.CoversElement(signed.Root()) || coreXML(t, signed) != before { @@ -349,7 +351,7 @@ func TestVerifySignatureWithExplicitKeyInfoTrust(t *testing.T) { }) t.Run("freeze-reference-policy", func(t *testing.T) { o := opt - o.Signature.ExpectedURIs = []string{namePayload} + o.Signature.ExpectedURIs = []string{payloadReferenceURI} o.SelectTrustedKey = func(context.Context, *keyinfo.Result) (int, error) { o.Signature.ExpectedURIs[0] = "#tampered" @@ -427,7 +429,7 @@ func TestKeyInfoLinkedFromSignature(t *testing.T) { t.Fatal(e) } - draft, sel, e := xmldsig.BuildTemplate(ctx, d, d.Root(), xmldsig.SignatureTemplate{SignatureMethod: xmldsig.ECDSASHA256, KeyInfo: linked, References: []xmldsig.ReferenceTemplate{{URI: namePayload}}}) + draft, sel, e := xmldsig.BuildTemplate(ctx, d, d.Root(), xmldsig.SignatureTemplate{SignatureMethod: xmldsig.ECDSASHA256, KeyInfo: linked, References: []xmldsig.ReferenceTemplate{{URI: payloadReferenceURI}}}) if e != nil { t.Fatal(e) } @@ -654,7 +656,7 @@ func TestKeyInfoBuilderMutualExclusion(t *testing.T) { certs [][]byte }{{"both", ki, [][]byte{{1}}}, {"wrong-root", coreParse(t, ``), nil}} { t.Run(test.name, func(t *testing.T) { - _, _, e := xmldsig.BuildTemplate(t.Context(), nil, document.Element{}, xmldsig.SignatureTemplate{SignatureMethod: xmldsig.ECDSASHA256, KeyInfo: test.ki, Certificates: test.certs, References: []xmldsig.ReferenceTemplate{{URI: nameData}}, Objects: []xmldsig.ObjectTemplate{{ID: nameData2, Binary: []byte("x")}}}) + _, _, e := xmldsig.BuildTemplate(t.Context(), nil, document.Element{}, xmldsig.SignatureTemplate{SignatureMethod: xmldsig.ECDSASHA256, KeyInfo: test.ki, Certificates: test.certs, References: []xmldsig.ReferenceTemplate{{URI: dataReferenceURI}}, Objects: []xmldsig.ObjectTemplate{{ID: dataLiteral, Binary: []byte("x")}}}) if e == nil { t.Fatal("accepted") } @@ -733,7 +735,7 @@ func FuzzVerifyWithKeyInfo(f *testing.F) { return } - options := xmldsig.KeyInfoVerifyOptions{Signature: xmldsig.SignatureOptions{ExpectedURIs: []string{nameData}}, SelectTrustedKey: selectKey} + options := xmldsig.KeyInfoVerifyOptions{Signature: xmldsig.SignatureOptions{ExpectedURIs: []string{dataReferenceURI}}, SelectTrustedKey: selectKey} if len(b) > 0 && int(b[0]) < len(vectors.Cases) { options.Signature = independentOptions[int(b[0])] options.SelectTrustedKey = independentSelector @@ -763,6 +765,29 @@ func FuzzVerifyWithKeyInfo(f *testing.F) { }) } +func TestTemplateCertificateEncodingConsistency(t *testing.T) { + chain := testcert.New(t) + for _, test := range testcert.CertificateEncodings(t, chain) { + t.Run(test.Name, func(t *testing.T) { + before := bytes.Clone(test.DER) + draft, selected, err := xmldsig.BuildTemplate(t.Context(), nil, document.Element{}, xmldsig.SignatureTemplate{SignatureMethod: xmldsig.ECDSASHA256, References: []xmldsig.ReferenceTemplate{{URI: "#data"}}, Certificates: [][]byte{test.DER}}) + if !test.Valid { + if draft != nil || selected.Valid() || !errors.Is(err, xmldsig.ErrStructure) { + t.Fatal("template accepted invalid certificate DER", err) + } + return + } + if err != nil || draft == nil || !selected.Valid() { + t.Fatal(err) + } + wire, err := draft.XML(t.Context()) + if err != nil || !bytes.Contains(wire, []byte(base64.StdEncoding.EncodeToString(test.DER))) || !bytes.Equal(before, test.DER) { + t.Fatal("template changed original certificate bytes", err) + } + }) + } +} + func keySelector(t testing.TB, expected crypto.PublicKey) xmldsig.TrustedKeySelector { t.Helper() @@ -794,7 +819,7 @@ func makeKeyInfoSigned(t testing.TB, k crypto.Signer, ki *document.Document) (*d ctx := t.Context() d := coreParse(t, `hello`) - draft, selected, e := xmldsig.BuildTemplate(ctx, d, d.Root(), xmldsig.SignatureTemplate{SignatureMethod: xmldsig.ECDSASHA256, KeyInfo: ki, References: []xmldsig.ReferenceTemplate{{URI: namePayload, Transforms: []reference.Transform{{Algorithm: c14n.Exclusive}}}}}) + draft, selected, e := xmldsig.BuildTemplate(ctx, d, d.Root(), xmldsig.SignatureTemplate{SignatureMethod: xmldsig.ECDSASHA256, KeyInfo: ki, References: []xmldsig.ReferenceTemplate{{URI: payloadReferenceURI, Transforms: []reference.Transform{{Algorithm: c14n.Exclusive}}}}}) if e != nil { t.Fatal(e) } @@ -827,26 +852,3 @@ func assertTemplateKeyInfoCount(t testing.TB, doc *document.Document, present bo t.Fatalf("KeyInfo count %d, want %d", count, want) } } - -func TestTemplateCertificateEncodingConsistency(t *testing.T) { - chain := testcert.New(t) - for _, test := range testcert.CertificateEncodings(t, chain) { - t.Run(test.Name, func(t *testing.T) { - before := bytes.Clone(test.DER) - draft, selected, err := xmldsig.BuildTemplate(t.Context(), nil, document.Element{}, xmldsig.SignatureTemplate{SignatureMethod: xmldsig.ECDSASHA256, References: []xmldsig.ReferenceTemplate{{URI: "#data"}}, Certificates: [][]byte{test.DER}}) - if !test.Valid { - if draft != nil || selected.Valid() || !errors.Is(err, xmldsig.ErrStructure) { - t.Fatal("template accepted invalid certificate DER", err) - } - return - } - if err != nil || draft == nil || !selected.Valid() { - t.Fatal(err) - } - wire, err := draft.XML(t.Context()) - if err != nil || !bytes.Contains(wire, []byte(base64.StdEncoding.EncodeToString(test.DER))) || !bytes.Equal(before, test.DER) { - t.Fatal("template changed original certificate bytes", err) - } - }) - } -} diff --git a/xmldsig/manifest.go b/xmldsig/manifest.go index f142003..9d341d4 100644 --- a/xmldsig/manifest.go +++ b/xmldsig/manifest.go @@ -8,10 +8,23 @@ import ( "fmt" "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/tree" "github.com/skarm/xmlsec/reference" ) +const ( + stageDependency = "dependency" + stageGraph = "graph" + stageManifest = "manifest" + defaultManifestCount = 64 + defaultManifestDepth = 8 + defaultManifestReferences = 256 + maximumManifestCount = 256 + maximumManifestDepth = 32 + maximumManifestReferences = 1024 +) + // ManifestType identifies a Reference to a ds:Manifest. const ManifestType = Namespace + "Manifest" @@ -139,18 +152,18 @@ func normalizeManifestPolicy(p ManifestPolicy) (ManifestPolicy, error) { } if p.MaxDepth == 0 { - p.MaxDepth = 8 + p.MaxDepth = defaultManifestDepth } if p.MaxManifests == 0 { - p.MaxManifests = 64 + p.MaxManifests = defaultManifestCount } if p.MaxReferences == 0 { - p.MaxReferences = 256 + p.MaxReferences = defaultManifestReferences } - if p.MaxDepth < 1 || p.MaxDepth > 32 || p.MaxManifests < 1 || p.MaxManifests > 256 || p.MaxReferences < 1 || p.MaxReferences > 1024 { + if p.MaxDepth < 1 || p.MaxDepth > maximumManifestDepth || p.MaxManifests < 1 || p.MaxManifests > maximumManifestCount || p.MaxReferences < 1 || p.MaxReferences > maximumManifestReferences { return p, fmt.Errorf("%w: Manifest limits", ErrPolicy) } @@ -195,7 +208,7 @@ type manifestScope struct { type manifestResourceKey struct { uri string - output [32]byte + output [sha256.Size]byte } type manifestGraph struct { @@ -279,7 +292,7 @@ func (b *manifestGraphBuilder) buildReference(ref coreReference, owner *manifest } if len(b.graph.tasks) >= b.graph.policy.MaxReferences { - return nil, t.wrap("graph", fmt.Errorf("%w: total Reference count", ErrPolicy)) + return nil, t.wrap(stageGraph, fmt.Errorf("%w: total Reference count", ErrPolicy)) } b.graph.tasks = append(b.graph.tasks, t) @@ -297,11 +310,11 @@ func (b *manifestGraphBuilder) buildReference(ref coreReference, owner *manifest if t.external && ref.typeURI == ManifestType { if !b.graph.policy.AllowExternal { - return nil, t.wrap("graph", fmt.Errorf("%w: external Manifest traversal is not enabled", ErrPolicy)) + return nil, t.wrap(stageGraph, fmt.Errorf("%w: external Manifest traversal is not enabled", ErrPolicy)) } if !externalManifestProvenance(ref.transforms) && !b.graph.policy.AllowTransformed { - return nil, t.wrap("graph", fmt.Errorf("%w: external Manifest edge must preserve the complete XML source", ErrPolicy)) + return nil, t.wrap(stageGraph, fmt.Errorf("%w: external Manifest edge must preserve the complete XML source", ErrPolicy)) } return t, nil // discover contents only after the incoming digest is authenticated @@ -309,7 +322,7 @@ func (b *manifestGraphBuilder) buildReference(ref coreReference, owner *manifest if transformedManifestEdge(t) { if !b.graph.policy.AllowTransformed { - return nil, t.wrap("graph", fmt.Errorf("%w: transformed Manifest traversal is not enabled", ErrPolicy)) + return nil, t.wrap(stageGraph, fmt.Errorf("%w: transformed Manifest traversal is not enabled", ErrPolicy)) } return t, nil @@ -317,7 +330,7 @@ func (b *manifestGraphBuilder) buildReference(ref coreReference, owner *manifest traverse, err := manifestEdgeTarget(t) if err != nil { - return nil, t.wrap("graph", err) + return nil, t.wrap(stageGraph, err) } if traverse { @@ -332,16 +345,16 @@ func (b *manifestGraphBuilder) buildReference(ref coreReference, owner *manifest func (b *manifestGraphBuilder) attachManifest(t *coreTask, depth int) error { g := b.graph if depth > g.policy.MaxDepth { - return t.wrap("graph", fmt.Errorf("%w: Manifest depth", ErrPolicy)) + return t.wrap(stageGraph, fmt.Errorf("%w: Manifest depth", ErrPolicy)) } if _, exists := g.active[t.target]; exists { - return t.wrap("graph", fmt.Errorf("%w: cyclic Manifest graph", ErrReference)) + return t.wrap(stageGraph, fmt.Errorf("%w: cyclic Manifest graph", ErrReference)) } if m, ok := g.byNode[t.target]; ok { if depth+m.height-1 > g.policy.MaxDepth { - return t.wrap("graph", fmt.Errorf("%w: shared Manifest depth", ErrPolicy)) + return t.wrap(stageGraph, fmt.Errorf("%w: shared Manifest depth", ErrPolicy)) } t.child = m @@ -351,7 +364,7 @@ func (b *manifestGraphBuilder) attachManifest(t *coreTask, depth int) error { children, err := b.registerManifest(t) if err != nil { - return t.wrap("graph", err) + return t.wrap(stageGraph, err) } if err := b.buildManifestReferences(t.child, children, depth); err != nil { @@ -369,7 +382,7 @@ func (b *manifestGraphBuilder) registerManifest(t *coreTask) ([]*tree.Node, erro return nil, fmt.Errorf("%w: Manifest count", ErrPolicy) } - children, err := structure(t.target, "Manifest", "Id") + children, err := structure(t.target, nameManifest, lexicon.SecurityAttrID) if err != nil { return nil, err } @@ -378,7 +391,7 @@ func (b *manifestGraphBuilder) registerManifest(t *coreTask) ([]*tree.Node, erro return nil, fmt.Errorf("%w: Manifest Reference count", ErrPolicy) } - id, _ := getAttr(t.target, document.Name{Local: "Id"}) + id, _ := getAttr(t.target, document.Name{Local: lexicon.SecurityAttrID}) m := &manifestPlan{node: t.target, id: id, index: len(g.manifests), height: 1, scope: t.scope} t.child = m g.byNode[t.target] = m @@ -387,7 +400,7 @@ func (b *manifestGraphBuilder) registerManifest(t *coreTask) ([]*tree.Node, erro mr := ManifestReport{Index: m.index, ID: id, ResourceURI: m.scope.uri, Transformed: m.scope.transformed, Status: NotChecked} for i, n := range children { - uri, _ := getAttr(n, document.Name{Local: "URI"}) + uri, _ := getAttr(n, document.Name{Local: lexicon.SecurityAttrURI}) mr.References = append(mr.References, ReferenceReport{Index: i, URI: uri, Status: NotChecked}) } @@ -400,12 +413,12 @@ func (b *manifestGraphBuilder) buildManifestReferences(m *manifestPlan, children for i, n := range children { r, err := parseCoreReference(b.ctx, n, b.plan.policy, b.plan.processing, b.template) if err != nil { - return (&coreTask{ref: r, owner: m, index: i}).wrap("structure", err) + return (&coreTask{ref: r, owner: m, index: i}).wrap(stageStructure, err) } if m.scope.uri != "" { if err := reference.PrepareTransforms(b.ctx, r.transforms, b.plan.processing); err != nil { - return (&coreTask{ref: r, owner: m, index: i}).wrap("preflight", processingError(err)) + return (&coreTask{ref: r, owner: m, index: i}).wrap(stagePreflight, processingError(err)) } } @@ -433,7 +446,7 @@ func manifestEdgeTarget(t *coreTask) (bool, error) { return false, nil } - isManifest := isDS(t.target, "Manifest") + isManifest := isDS(t.target, nameManifest) if t.ref.typeURI == ManifestType && !isManifest { return false, fmt.Errorf("%w: Manifest Type does not target ds:Manifest", ErrStructure) } @@ -484,7 +497,7 @@ func transformedManifestEdge(t *coreTask) bool { return !externalManifestProvenance(t.ref.transforms) } - return !isDS(t.target, "Manifest") || !manifestProvenance(t.ref.transforms) + return !isDS(t.target, nameManifest) || !manifestProvenance(t.ref.transforms) } // Generation validates complete discovered graphs before asking a private signer @@ -567,7 +580,7 @@ func (d *digestDependencyTraversal) visit(t *coreTask) error { } if d.states[t] == 1 { - return t.wrap("dependency", fmt.Errorf("%w: cyclic digest dependency", ErrPolicy)) + return t.wrap(stageDependency, fmt.Errorf("%w: cyclic digest dependency", ErrPolicy)) } if d.states[t] == 2 { @@ -590,7 +603,7 @@ func (d *digestDependencyTraversal) visit(t *coreTask) error { func (d *digestDependencyTraversal) visitDependencies(t *coreTask) error { excludes := excludesContaining(t.ref.transforms) if within(d.plan.sv, t.target) && !excludes { - return t.wrap("dependency", fmt.Errorf("%w: reference depends on SignatureValue", ErrPolicy)) + return t.wrap(stageDependency, fmt.Errorf("%w: reference depends on SignatureValue", ErrPolicy)) } for _, other := range d.tasks { @@ -657,20 +670,20 @@ func (v *manifestVerifier) checkReferenceAt(t *coreTask, depth int) error { if t.external && t.ref.typeURI == ManifestType && v.plan.graph.policy.AllowExternal && !transformedManifestEdge(t) { if scope := v.plan.graph.externalScopes[manifestResourceKey{uri: t.resourceURI}]; scope != nil { if _, active := v.active[v.plan.graph.byNode[tree.Root(scope.doc)]]; active { - return v.fail(t, "graph", fmt.Errorf("%w: cyclic external Manifest graph", ErrReference)) + return v.fail(t, stageGraph, fmt.Errorf("%w: cyclic external Manifest graph", ErrReference)) } } } processed, rr, err := processCoreTask(v.ctx, v.doc, v.plan, v.budget, t) if err != nil { - return v.fail(t, "process", err) + return v.fail(t, stageProcess, err) } rrpt.Steps = processed.Steps() if subtle.ConstantTimeCompare(rr.digest, t.ref.digest) != 1 { - return v.fail(t, "digest", ErrDigest) + return v.fail(t, stageDigest, ErrDigest) } rrpt.Status = Valid @@ -689,7 +702,7 @@ func (v *manifestVerifier) checkReferenceAt(t *coreTask, depth int) error { return err } - return &ManifestReferenceError{Manifest: -1, ID: t.ref.uri, Reference: &ReferenceError{Index: t.index, URI: t.ref.uri, Stage: "manifest", Err: err}} + return &ManifestReferenceError{Manifest: -1, ID: t.ref.uri, Reference: &ReferenceError{Index: t.index, URI: t.ref.uri, Stage: stageManifest, Err: err}} } } @@ -698,7 +711,7 @@ func (v *manifestVerifier) checkReferenceAt(t *coreTask, depth int) error { } if !t.external && !transformedManifestEdge(t) && !rr.CoversElement(tree.Wrap(t.child.scope.doc, t.child.node)) { - return v.fail(t, "coverage", fmt.Errorf("%w: Manifest source not fully covered", ErrPolicy)) + return v.fail(t, stageCoverage, fmt.Errorf("%w: Manifest source not fully covered", ErrPolicy)) } return v.checkManifest(t.child, depth) @@ -794,7 +807,7 @@ func (v *manifestVerifier) attachExternalManifest(t *coreTask, rr ReferenceResul return fmt.Errorf("%w: external Manifest XML: %w", ErrReference, err) } - if !isDS(tree.Root(doc), "Manifest") { + if !isDS(tree.Root(doc), nameManifest) { return fmt.Errorf("%w: external root must be ds:Manifest", ErrStructure) } diff --git a/xmldsig/manifest_test.go b/xmldsig/manifest_test.go index fc1ea66..557c501 100644 --- a/xmldsig/manifest_test.go +++ b/xmldsig/manifest_test.go @@ -112,6 +112,8 @@ func testTransformedManifestSnapshot(t *testing.T, local bool) { // Child content mutation does not alter the incoming Manifest digest. resources[uriUrnAttachment] = []byte("modified nested payload") + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if result, report, err := xmldsig.VerifySignatureDetailed(t.Context(), signed, xmldsig.Signatures(signed)[0], key.Public(), options); result != nil || !errors.Is(err, xmldsig.ErrDigest) || report.References[0].Status != xmldsig.Valid || report.ManifestValidation == xmldsig.Valid { t.Fatal("nested mutation", report, err) } @@ -222,6 +224,8 @@ func TestTransformedManifestAuthenticationAndLimits(t *testing.T) { resources[uriExternalManifest] = []byte(base64.StdEncoding.EncodeToString([]byte(``))) + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if result, report, err := xmldsig.VerifySignatureDetailed(t.Context(), signed, signed.Root(), key.Public(), options); result != nil || !errors.Is(err, xmldsig.ErrDigest) || len(calls) != 1 || len(report.Manifests) != 0 { t.Fatal("discovery before parent digest", report, err, calls) } @@ -281,6 +285,8 @@ func TestTransformedManifestResultIdentity(t *testing.T) { resources["urn:second-result"] = []byte("modified second result") + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if result, report, err := xmldsig.VerifySignatureDetailed(t.Context(), signed, signed.Root(), key.Public(), options); result != nil || !errors.Is(err, xmldsig.ErrDigest) || report.References[0].Status != xmldsig.Valid || report.References[1].Status != xmldsig.Valid || report.Manifests[1].References[0].Status != xmldsig.Invalid { t.Fatal("second transform result was not traversed", report, err) } @@ -353,6 +359,8 @@ func TestExternalManifestURIContext(t *testing.T) { clear(calls) result, report, err = xmldsig.VerifySignatureDetailed(t.Context(), signed, xmldsig.Signatures(signed)[0], key.Public(), opt) + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if !errors.Is(err, xmldsig.ErrDigest) || result != nil || len(calls) != 1 || calls[outer] != 1 || len(report.Manifests) != 0 { t.Fatal("nested discovery preceded parent authentication", result, report, err, calls) } @@ -457,6 +465,8 @@ func TestExternalManifestResolvedCycle(t *testing.T) { opt.Manifests = xmldsig.ManifestPolicy{Mode: xmldsig.ManifestRecursive, AllowExternal: true} result, report, err := xmldsig.VerifySignatureDetailed(t.Context(), signed, signed.Root(), key.Public(), opt) + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if !errors.Is(err, xmldsig.ErrReference) || result != nil || report.References[0].Status != xmldsig.Valid || calls[resolved] != 1 || len(calls) != 1 { t.Fatal("resolved cycle was reread or misclassified", result, report, err, calls) } @@ -646,6 +656,8 @@ func TestExternalManifestIndependentVector(t *testing.T) { resources[uriUrnAttachment] = []byte("changed independent vector") result, report, err = xmldsig.VerifySignatureDetailed(t.Context(), doc, doc.Root(), key, options) + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if !errors.Is(err, xmldsig.ErrDigest) || result != nil || report.SignatureValue != xmldsig.Valid || report.References[0].Status != xmldsig.Valid { t.Fatal(result, report, err) } @@ -696,142 +708,6 @@ func TestExternalManifestGraph(t *testing.T) { } } -func testExternalManifestGraph(t *testing.T, canonical bool) { - t.Helper() - resources := externalManifestResources(t) - options, calls, closed := externalManifestOptions(resources) - key := graphKey(t) - signed := signExternalManifest(t, key, options, canonical) - - clear(calls) - - *closed = 0 - - result, report, err := xmldsig.VerifySignatureDetailed(t.Context(), signed, signed.Root(), key.Public(), options) - if err != nil || result == nil { - t.Fatal(result, report, err) - } - - if len(result.Manifests()) != 2 || report.ManifestValidation != xmldsig.Valid { - t.Fatal("incomplete graph", report) - } - - for _, uri := range []string{uriExternalManifest, uriLeafManifest, uriUrnAttachment} { - if calls[uri] != 1 { - t.Fatalf("%s fetched %d times", uri, calls[uri]) - } - } - - if *closed != 3 { - t.Fatal("resource readers not closed", *closed) - } - - root := result.Manifests()[0] - if root.ResourceURI() != uriExternalManifest || report.Manifests[0].ResourceURI != uriExternalManifest || len(root.References()) != 3 { - t.Fatal(root, report) - } - - local := root.References()[1] - if local.Target().Name().Local != "Reference" || !local.CoversElement(local.Target()) || result.CoversElement(local.Target()) { - t.Fatal("external/local scope or coverage was confused") - } - - if result.Manifests()[1].ResourceURI() != uriLeafManifest || len(result.Manifests()[1].References()) != 1 { - t.Fatal("leaf result missing") - } - - var replay bytes.Buffer - if _, err := local.WriteDigestInput(t.Context(), &replay); err != nil || replay.Len() == 0 { - t.Fatal("remote-scope replay", err) - } - - for range 2 { - if _, err := result.References()[0].WriteDigestInput(t.Context(), io.Discard); err != nil { - t.Fatal("external replay", err) - } - } -} - -func externalManifestResources(t *testing.T) map[string][]byte { - t.Helper() - - payload := []byte("external payload") - leaf := []byte(`` + externalManifestReference(uriUrnAttachment, "", payload) + ``) - first := strings.Replace(externalManifestReference(uriLeafManifest, xmldsig.ManifestType, leaf), ``+first+``) - - var canonical bytes.Buffer - if err := c14n.WriteElement(t.Context(), &canonical, doc.Root().Children()[0], c14n.Options{Algorithm: c14n.Exclusive}); err != nil { - t.Fatal(err) - } - - second := externalManifestReference("#scoped", "", canonical.Bytes(), c14n.Exclusive) - third := externalManifestReference(uriLeafManifest, xmldsig.ManifestType, leaf) - root := []byte(`` + first + second + third + ``) - - return map[string][]byte{uriExternalManifest: root, uriLeafManifest: leaf, uriUrnAttachment: payload} -} - -func externalManifestReference(uri, typ string, input []byte, transforms ...string) string { - digest := sha256.Sum256(input) - - ref := coreRef(uri, transforms...) - if typ != "" { - ref = strings.Replace(ref, ``, ``+base64.StdEncoding.EncodeToString(digest[:])+``, 1) -} - -func externalManifestOptions(resources map[string][]byte) (xmldsig.SignatureOptions, map[string]int, *int) { - calls := make(map[string]int) - closed := new(int) - opt := xmldsig.SignatureOptions{Manifests: xmldsig.ManifestPolicy{Mode: xmldsig.ManifestRecursive, AllowExternal: true}, ExpectedURIs: []string{uriExternalManifest}} - opt.External = &xmldsig.ExternalReferences{AllowedURIs: []string{uriExternalManifest, uriLeafManifest, uriUrnAttachment}, Resolver: xmldsig.ResolverFunc(func(ctx context.Context, uri string) (io.ReadCloser, error) { - calls[uri]++ - - if err := ctx.Err(); err != nil { - return nil, err - } - - value, ok := resources[uri] - if !ok { - return nil, xmldsig.ErrReference - } - - return &countClose{Reader: bytes.NewReader(value), n: closed}, nil - })} - - return opt, calls, closed -} - -func signExternalManifest(t *testing.T, key crypto.Signer, options xmldsig.SignatureOptions, canonical bool) *document.Document { - t.Helper() - - var transforms []reference.Transform - if canonical { - transforms = graphC14N() - } - - shadow := "signature document scope" - - doc, selected, err := xmldsig.BuildTemplate(t.Context(), nil, document.Element{}, xmldsig.SignatureTemplate{ - SignatureMethod: xmldsig.ECDSASHA256, - References: []xmldsig.ReferenceTemplate{{URI: uriExternalManifest, Type: xmldsig.ManifestType, Transforms: transforms}}, - Objects: []xmldsig.ObjectTemplate{{ID: "scoped", Text: &shadow}}, - }) - if err != nil { - t.Fatal(err) - } - - signed, err := xmldsig.SignTemplate(t.Context(), doc, selected, key, xmldsig.TemplateSignOptions{Validation: options}) - if err != nil { - t.Fatal(err) - } - - return signed -} - func TestExternalManifestAuthenticationOrder(t *testing.T) { resources := externalManifestResources(t) opt, calls, _ := externalManifestOptions(resources) @@ -860,6 +736,8 @@ func TestExternalManifestAuthenticationOrder(t *testing.T) { clear(calls) result, report, err := xmldsig.VerifySignatureDetailed(t.Context(), signed, signed.Root(), key.Public(), opt) + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if !errors.Is(err, xmldsig.ErrDigest) || result != nil || calls[uriExternalManifest] != 1 || len(calls) != 1 || len(report.Manifests) != 0 { t.Fatal(result, report, err, calls) } @@ -933,6 +811,8 @@ func TestExternalManifestPolicyAndFailures(t *testing.T) { options.Manifests = opt.Manifests result, report, err = xmldsig.VerifySignatureDetailed(t.Context(), signed, signed.Root(), key.Public(), options) + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if !errors.Is(err, xmldsig.ErrDigest) || result != nil || report.SignatureValue != xmldsig.Valid || report.References[0].Status != xmldsig.Valid { t.Fatal(result, report, err) } @@ -962,6 +842,8 @@ func TestExternalManifestMalformedAndCyclic(t *testing.T) { *closed = 0 result, report, err := xmldsig.VerifySignatureDetailed(t.Context(), signed, signed.Root(), key.Public(), opt) + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if !errors.Is(err, test.want) || result != nil || report.SignatureValue != xmldsig.Valid || report.References[0].Status != xmldsig.Valid { t.Fatal(result, report, err) } @@ -1184,7 +1066,7 @@ func TestBuilderAppendAndDuplicateIDs(t *testing.T) { ctx := t.Context() d := graphDoc(t, `v`) raw, _ := d.XML(ctx) - spec := xmldsig.SignatureTemplate{ID: "s", SignatureMethod: xmldsig.ECDSASHA256, References: []xmldsig.ReferenceTemplate{{URI: nameData, Transforms: graphC14N()}}} + spec := xmldsig.SignatureTemplate{ID: "s", SignatureMethod: xmldsig.ECDSASHA256, References: []xmldsig.ReferenceTemplate{{URI: dataReferenceURI, Transforms: graphC14N()}}} draft, s, e := xmldsig.BuildTemplate(ctx, d, d.Root(), spec) if e != nil { @@ -1281,6 +1163,142 @@ func FuzzVerifyManifest(f *testing.F) { }) } +func testExternalManifestGraph(t *testing.T, canonical bool) { + t.Helper() + resources := externalManifestResources(t) + options, calls, closed := externalManifestOptions(resources) + key := graphKey(t) + signed := signExternalManifest(t, key, options, canonical) + + clear(calls) + + *closed = 0 + + result, report, err := xmldsig.VerifySignatureDetailed(t.Context(), signed, signed.Root(), key.Public(), options) + if err != nil || result == nil { + t.Fatal(result, report, err) + } + + if len(result.Manifests()) != 2 || report.ManifestValidation != xmldsig.Valid { + t.Fatal("incomplete graph", report) + } + + for _, uri := range []string{uriExternalManifest, uriLeafManifest, uriUrnAttachment} { + if calls[uri] != 1 { + t.Fatalf("%s fetched %d times", uri, calls[uri]) + } + } + + if *closed != 3 { + t.Fatal("resource readers not closed", *closed) + } + + root := result.Manifests()[0] + if root.ResourceURI() != uriExternalManifest || report.Manifests[0].ResourceURI != uriExternalManifest || len(root.References()) != 3 { + t.Fatal(root, report) + } + + local := root.References()[1] + if local.Target().Name().Local != "Reference" || !local.CoversElement(local.Target()) || result.CoversElement(local.Target()) { + t.Fatal("external/local scope or coverage was confused") + } + + if result.Manifests()[1].ResourceURI() != uriLeafManifest || len(result.Manifests()[1].References()) != 1 { + t.Fatal("leaf result missing") + } + + var replay bytes.Buffer + if _, err := local.WriteDigestInput(t.Context(), &replay); err != nil || replay.Len() == 0 { + t.Fatal("remote-scope replay", err) + } + + for range 2 { + if _, err := result.References()[0].WriteDigestInput(t.Context(), io.Discard); err != nil { + t.Fatal("external replay", err) + } + } +} + +func externalManifestResources(t *testing.T) map[string][]byte { + t.Helper() + + payload := []byte("external payload") + leaf := []byte(`` + externalManifestReference(uriUrnAttachment, "", payload) + ``) + first := strings.Replace(externalManifestReference(uriLeafManifest, xmldsig.ManifestType, leaf), ``+first+``) + + var canonical bytes.Buffer + if err := c14n.WriteElement(t.Context(), &canonical, doc.Root().Children()[0], c14n.Options{Algorithm: c14n.Exclusive}); err != nil { + t.Fatal(err) + } + + second := externalManifestReference("#scoped", "", canonical.Bytes(), c14n.Exclusive) + third := externalManifestReference(uriLeafManifest, xmldsig.ManifestType, leaf) + root := []byte(`` + first + second + third + ``) + + return map[string][]byte{uriExternalManifest: root, uriLeafManifest: leaf, uriUrnAttachment: payload} +} + +func externalManifestReference(uri, typ string, input []byte, transforms ...string) string { + digest := sha256.Sum256(input) + + ref := coreRef(uri, transforms...) + if typ != "" { + ref = strings.Replace(ref, ``, ``+base64.StdEncoding.EncodeToString(digest[:])+``, 1) +} + +func externalManifestOptions(resources map[string][]byte) (xmldsig.SignatureOptions, map[string]int, *int) { + calls := make(map[string]int) + closed := new(int) + opt := xmldsig.SignatureOptions{Manifests: xmldsig.ManifestPolicy{Mode: xmldsig.ManifestRecursive, AllowExternal: true}, ExpectedURIs: []string{uriExternalManifest}} + opt.External = &xmldsig.ExternalReferences{AllowedURIs: []string{uriExternalManifest, uriLeafManifest, uriUrnAttachment}, Resolver: xmldsig.ResolverFunc(func(ctx context.Context, uri string) (io.ReadCloser, error) { + calls[uri]++ + + if err := ctx.Err(); err != nil { + return nil, err + } + + value, ok := resources[uri] + if !ok { + return nil, xmldsig.ErrReference + } + + return &countClose{Reader: bytes.NewReader(value), n: closed}, nil + })} + + return opt, calls, closed +} + +func signExternalManifest(t *testing.T, key crypto.Signer, options xmldsig.SignatureOptions, canonical bool) *document.Document { + t.Helper() + + var transforms []reference.Transform + if canonical { + transforms = graphC14N() + } + + shadow := "signature document scope" + + doc, selected, err := xmldsig.BuildTemplate(t.Context(), nil, document.Element{}, xmldsig.SignatureTemplate{ + SignatureMethod: xmldsig.ECDSASHA256, + References: []xmldsig.ReferenceTemplate{{URI: uriExternalManifest, Type: xmldsig.ManifestType, Transforms: transforms}}, + Objects: []xmldsig.ObjectTemplate{{ID: "scoped", Text: &shadow}}, + }) + if err != nil { + t.Fatal(err) + } + + signed, err := xmldsig.SignTemplate(t.Context(), doc, selected, key, xmldsig.TemplateSignOptions{Validation: options}) + if err != nil { + t.Fatal(err) + } + + return signed +} + func graphKey(t *testing.T) *ecdsa.PrivateKey { t.Helper() diff --git a/xmldsig/parameters.go b/xmldsig/parameters.go index 1d45fd6..2945390 100644 --- a/xmldsig/parameters.go +++ b/xmldsig/parameters.go @@ -10,12 +10,22 @@ import ( "encoding/binary" "fmt" "io" + "math" "math/big" "github.com/skarm/xmlsec/internal/nilcheck" "github.com/skarm/xmlsec/internal/signature" ) +const ( + absoluteMinimumHMACOutputBits = 80 + defaultPSSSaltBytes = 32 + // Hard byte ceiling for accepted or configured RSA-PSS salt lengths. + maximumPSSSaltBytes = 2048 + pssTrailerByte = 0xbc + pssZeroPrefixBytes = 8 +) + const ( // PSSNamespace is the namespace for parameterized RSA-PSS and MGF1 identifiers. PSSNamespace = "http://www.w3.org/2007/05/xmldsig-more#" @@ -115,7 +125,7 @@ func SignMessageWithParametersReader(ctx context.Context, src io.Reader, signer } credential := signatureCredential{public: key, signer: signer} - if err := credential.validateSigner(s); err != nil { + if err := credential.validateSigner(s, p); err != nil { return nil, err } @@ -124,7 +134,7 @@ func SignMessageWithParametersReader(ctx context.Context, src io.Reader, signer return nil, err } - return credential.signPrepared(ctx, s, digest) + return credential.signPrepared(ctx, s, p, digest) } // VerifyMessageWithParameters verifies a raw message with explicit signature parameters @@ -212,7 +222,7 @@ func (s signatureSpec) validatePolicy(p Policy) error { return fmt.Errorf("%w: HMAC hash", ErrPolicy) } - if s.hmacBits < p.MinHMACOutputBits || (!p.AllowHMACTruncation && s.hmacBits != h.Size()*8) { + if s.hmacBits < p.MinHMACOutputBits || (!p.AllowHMACTruncation && s.hmacBits != h.Size()*signatureOctetBits) { return fmt.Errorf("%w: HMAC truncation", ErrPolicy) } } @@ -239,7 +249,7 @@ func (s signatureSpec) validatePublicKey(key crypto.PublicKey, p Policy) error { return ErrKey } - emLen := (k.N.BitLen() - 1 + 7) / 8 + emLen := (k.N.BitLen() - 1 + signatureOctetBits - 1) / signatureOctetBits if s.pss.SaltLength > emLen-s.pss.Hash.Size()-2 { return fmt.Errorf("%w: PSS salt does not fit the RSA modulus", ErrPolicy) } @@ -249,7 +259,7 @@ func (s signatureSpec) validatePublicKey(key crypto.PublicKey, p Policy) error { } func (s signatureSpec) validateSigner(signer crypto.Signer) error { - if s.algorithm == DSASHA1 || s.algorithm == RSASHA1 { + if s.algorithm == DSASHA1 { return fmt.Errorf("%w: legacy signature generation is disabled", ErrPolicy) } @@ -329,12 +339,12 @@ func copySignatureParameters(p SignatureParameters) SignatureParameters { // validation; bounds are rechecked here before slicing or allocating. func verifyParameterizedPSS(k *rsa.PublicKey, digest, signature []byte, p PSSParameters) error { invalid := func() error { return fmt.Errorf("%w: RSA-PSS", ErrSignature) } - if k == nil || k.N == nil || k.N.Sign() <= 0 || k.E < 3 || len(signature) != (k.N.BitLen()+7)/8 { + if k == nil || k.N == nil || k.N.Sign() <= 0 || k.E < minimumRSAExponent || len(signature) != (k.N.BitLen()+signatureOctetBits-1)/signatureOctetBits { return invalid() } emBits := k.N.BitLen() - 1 - emLen := (emBits + 7) / 8 + emLen := (emBits + signatureOctetBits - 1) / signatureOctetBits hLen := p.Hash.Size() if p.SaltLength < 0 || emLen < hLen+p.SaltLength+2 { @@ -357,21 +367,21 @@ func verifyParameterizedPSS(k *rsa.PublicKey, digest, signature []byte, p PSSPar } m := new(big.Int).Exp(sig, big.NewInt(int64(k.E)), k.N) - if m.BitLen() > emLen*8 { + if m.BitLen() > emLen*signatureOctetBits { return invalid() } em := make([]byte, emLen) m.FillBytes(em) - if em[emLen-1] != 0xbc { + if em[emLen-1] != pssTrailerByte { return invalid() } dbLen := emLen - hLen - 1 - unused := uint(8*emLen - emBits) - if em[0] & ^byte(0xff>>unused) != 0 { + unused := uint(signatureOctetBits*emLen - emBits) + if em[0] & ^byte(math.MaxUint8>>unused) != 0 { return invalid() } @@ -381,7 +391,10 @@ func verifyParameterizedPSS(k *rsa.PublicKey, digest, signature []byte, p PSSPar mask := p.MGFHash.New() - var counter [4]byte + // MGF1 uses an unsigned 32-bit big-endian block counter. + const mgfCounterBytes = 4 + + var counter [mgfCounterBytes]byte off := 0 for i := uint32(0); off < dbLen; i++ { @@ -401,7 +414,7 @@ func verifyParameterizedPSS(k *rsa.PublicKey, digest, signature []byte, p PSSPar } } - db[0] &= 0xff >> unused + db[0] &= math.MaxUint8 >> unused psLen := dbLen - p.SaltLength - 1 bad := byte(0) @@ -414,7 +427,7 @@ func verifyParameterizedPSS(k *rsa.PublicKey, digest, signature []byte, p PSSPar } hash := p.Hash.New() - _, _ = hash.Write(make([]byte, 8)) + _, _ = hash.Write(make([]byte, pssZeroPrefixBytes)) _, _ = hash.Write(digest) _, _ = hash.Write(db[psLen+1:]) @@ -462,7 +475,7 @@ func normalizeSignatureSpec(method string, params SignatureParameters) (signatur return s, fmt.Errorf("%w: HMAC parameter on RSA-PSS", ErrStructure) } - p := PSSParameters{Hash: crypto.SHA256, MGFHash: crypto.SHA256, SaltLength: 32, TrailerField: 1} + p := PSSParameters{Hash: crypto.SHA256, MGFHash: crypto.SHA256, SaltLength: defaultPSSSaltBytes, TrailerField: 1} normalizeSignatureSpecPSS(¶ms, &p) if p.Hash == crypto.SHA1 || p.MGFHash == crypto.SHA1 { @@ -481,7 +494,7 @@ func normalizeSignatureSpec(method string, params SignatureParameters) (signatur return s, fmt.Errorf("%w: PSS trailer must be 1", ErrAlgorithm) } - if p.SaltLength < 0 || p.SaltLength > 2048 { + if p.SaltLength < 0 || p.SaltLength > maximumPSSSaltBytes { return s, fmt.Errorf("%w: PSS salt bounds", ErrPolicy) } @@ -497,11 +510,11 @@ func normalizeSignatureSpec(method string, params SignatureParameters) (signatur bits := params.HMACOutputLength if bits == 0 { - bits = h.Size() * 8 + bits = h.Size() * signatureOctetBits } // XMLDSig 1.1 sections 4.4.2 and 6.3.1: whole bytes, >= half // the hash output and >=80 bits, never longer than the hash output. - if bits%8 != 0 || bits < h.Size()*4 || bits < 80 || bits > h.Size()*8 { + if bits%signatureOctetBits != 0 || bits < h.Size()*(signatureOctetBits/2) || bits < absoluteMinimumHMACOutputBits || bits > h.Size()*signatureOctetBits { return s, fmt.Errorf("%w: invalid HMAC output length", ErrPolicy) } diff --git a/xmldsig/parameters_test.go b/xmldsig/parameters_test.go index a601de7..f906b98 100644 --- a/xmldsig/parameters_test.go +++ b/xmldsig/parameters_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "crypto" + "crypto/fips140" "crypto/rand" "crypto/rsa" "crypto/x509" @@ -12,6 +13,8 @@ import ( "io" "os" "path/filepath" + "reflect" + "slices" "strings" "testing" @@ -128,7 +131,7 @@ func TestParameterizedProviderAndPreflight(t *testing.T) { reads := 0 opt := xmldsig.SignatureOptions{Policy: xmldsig.ParameterizedPolicy(), External: &xmldsig.ExternalReferences{AllowedURIs: []string{uriUrnData}, Resolver: xmldsig.ResolverFunc(func(context.Context, string) (io.ReadCloser, error) { reads++ - return io.NopCloser(strings.NewReader(nameData2)), nil + return io.NopCloser(strings.NewReader(dataLiteral)), nil })}} out, e := xmldsig.SignTemplate(ctx, d, s, key, xmldsig.TemplateSignOptions{Validation: opt}) @@ -179,6 +182,8 @@ func TestParameterizedPSSXML(t *testing.T) { opt.RequiredElements = nil r, report, e = xmldsig.VerifySignatureDetailed(ctx, modified, modified.Root(), &key.PublicKey, opt) + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if r != nil || !errors.Is(e, xmldsig.ErrDigest) || report.SignatureValue != xmldsig.Valid || report.References[0].Status != xmldsig.Valid || report.References[1].Status != xmldsig.Invalid { t.Fatalf("second digest bypass %v %+v", e, report) } @@ -466,3 +471,192 @@ func (s *contextPSSProvider) Sign(ctx context.Context, r io.Reader, d []byte, o return s.pssProvider.Sign(r, d, o) } + +func TestIntersectPoliciesConstraints(t *testing.T) { + first := xmldsig.Policy{ + AllowLegacySigning: true, + AllowedSignatureMethods: []string{xmldsig.RSASHA512, xmldsig.ECDSASHA256, xmldsig.RSASHA256}, + AllowedDigestMethods: []string{xmldsig.SHA512, xmldsig.SHA256, xmldsig.SHA384}, + MinRSABits: 2048, + MaxRSABits: 6144, + MaxReferences: 4, + MaxCanonicalBytes: 2000, + MinPSSSaltBytes: 8, + MaxPSSSaltBytes: 80, + MinHMACKeyBytes: 64, + MaxHMACKeyBytes: 256, + MinHMACOutputBits: 192, + AllowHMACTruncation: true, + } + second := xmldsig.Policy{ + AllowedSignatureMethods: []string{xmldsig.RSASHA256, xmldsig.RSASHA512}, + AllowedDigestMethods: []string{xmldsig.SHA256, xmldsig.SHA512}, + MinRSABits: 3072, + MaxRSABits: 8192, + MaxReferences: 8, + MaxCanonicalBytes: 1000, + MinPSSSaltBytes: 16, + MaxPSSSaltBytes: 128, + MinHMACKeyBytes: 32, + MaxHMACKeyBytes: 128, + MinHMACOutputBits: 256, + } + want := xmldsig.Policy{ + AllowedSignatureMethods: []string{xmldsig.RSASHA512, xmldsig.RSASHA256}, + AllowedDigestMethods: []string{xmldsig.SHA512, xmldsig.SHA256}, + MinRSABits: 3072, + MaxRSABits: 6144, + MaxReferences: 4, + MaxCanonicalBytes: 1000, + MinPSSSaltBytes: 16, + MaxPSSSaltBytes: 80, + MinHMACKeyBytes: 64, + MaxHMACKeyBytes: 128, + MinHMACOutputBits: 256, + } + got, err := xmldsig.IntersectPolicies(first, second) + if err != nil || !reflect.DeepEqual(got, want) { + t.Fatalf("intersection = %+v, %v; want %+v", got, err, want) + } + + // Reversing the policies must retain the same bounds and permissions; + // only the explicitly documented first-input algorithm order changes. + reverse, err := xmldsig.IntersectPolicies(second, first) + want.AllowedSignatureMethods = []string{xmldsig.RSASHA256, xmldsig.RSASHA512} + want.AllowedDigestMethods = []string{xmldsig.SHA256, xmldsig.SHA512} + if err != nil || !reflect.DeepEqual(reverse, want) { + t.Fatalf("reverse intersection = %+v, %v; want %+v", reverse, err, want) + } + + // Force a review of the security composition contract when Policy gains a + // field, so a new restriction cannot silently fall back to its zero default. + covered := []string{ + "AllowLegacySigning", "AllowedSignatureMethods", "AllowedDigestMethods", + "MinRSABits", "MaxRSABits", "MaxReferences", "MaxCanonicalBytes", + "MinPSSSaltBytes", "MaxPSSSaltBytes", "MinHMACKeyBytes", + "MaxHMACKeyBytes", "MinHMACOutputBits", "AllowHMACTruncation", + } + typ := reflect.TypeFor[xmldsig.Policy]() + if typ.NumField() != len(covered) { + t.Fatalf("Policy has %d fields; composition tests cover %d", typ.NumField(), len(covered)) + } + + for field := range typ.Fields() { + if !slices.Contains(covered, field.Name) { + t.Errorf("unreviewed Policy field %s", field.Name) + } + } +} + +func TestIntersectPoliciesDefaultsAndPermissions(t *testing.T) { + for _, empty := range []xmldsig.Policy{{}, {AllowedSignatureMethods: []string{}, AllowedDigestMethods: []string{}}} { + got, err := xmldsig.IntersectPolicies(empty, xmldsig.Policy{}) + if err != nil || !reflect.DeepEqual(got, xmldsig.DefaultPolicy()) { + t.Fatalf("zero/default intersection = %+v, %v", got, err) + } + } + + for _, left := range []bool{false, true} { + for _, right := range []bool{false, true} { + first := xmldsig.DefaultPolicy() + first.AllowLegacySigning = left + first.AllowHMACTruncation = left + second := xmldsig.DefaultPolicy() + second.AllowLegacySigning = right + second.AllowHMACTruncation = right + got, err := xmldsig.IntersectPolicies(first, second) + if err != nil || got.AllowLegacySigning != (left && right) || got.AllowHMACTruncation != (left && right) { + t.Fatalf("permissions %v/%v: %+v, %v", left, right, got, err) + } + } + } + + legacy := xmldsig.LegacyVerificationPolicy() + got, err := xmldsig.IntersectPolicies(legacy, legacy) + if fips140.Enabled() { + if !errors.Is(err, xmldsig.ErrPolicy) || !reflect.DeepEqual(got, xmldsig.Policy{}) { + t.Fatalf("FIPS legacy intersection = %+v, %v; want zero and policy error", got, err) + } + + return + } + + if err != nil || !slices.Contains(got.AllowedSignatureMethods, xmldsig.RSASHA1) || !slices.Contains(got.AllowedDigestMethods, xmldsig.SHA1) { + t.Fatalf("explicit legacy algorithms were lost: %+v, %v", got, err) + } +} + +func TestIntersectPoliciesRejectsIncompatible(t *testing.T) { + cases := []struct { + name string + first xmldsig.Policy + second xmldsig.Policy + want error + }{ + { + name: "disjoint signatures", + first: xmldsig.Policy{AllowedSignatureMethods: []string{xmldsig.RSASHA256}}, + second: xmldsig.Policy{AllowedSignatureMethods: []string{xmldsig.ECDSASHA256}}, + want: xmldsig.ErrPolicy, + }, + { + name: "disjoint digests", + first: xmldsig.Policy{AllowedDigestMethods: []string{xmldsig.SHA256}}, + second: xmldsig.Policy{AllowedDigestMethods: []string{xmldsig.SHA512}}, + want: xmldsig.ErrPolicy, + }, + {name: "RSA range", first: xmldsig.Policy{MaxRSABits: 2048}, second: xmldsig.Policy{MinRSABits: 3072}, want: xmldsig.ErrPolicy}, + {name: "PSS range", first: xmldsig.Policy{MaxPSSSaltBytes: 16}, second: xmldsig.Policy{MinPSSSaltBytes: 32}, want: xmldsig.ErrPolicy}, + {name: "HMAC range", first: xmldsig.Policy{MaxHMACKeyBytes: 32}, second: xmldsig.Policy{MinHMACKeyBytes: 64}, want: xmldsig.ErrPolicy}, + {name: "invalid first", first: xmldsig.Policy{MaxReferences: -1}, want: xmldsig.ErrPolicy}, + {name: "invalid second", second: xmldsig.Policy{MaxCanonicalBytes: -1}, want: xmldsig.ErrPolicy}, + {name: "unknown signature", first: xmldsig.Policy{AllowedSignatureMethods: []string{"urn:unknown:signature"}}, want: xmldsig.ErrAlgorithm}, + {name: "unknown digest", second: xmldsig.Policy{AllowedDigestMethods: []string{"urn:unknown:digest"}}, want: xmldsig.ErrAlgorithm}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + got, err := xmldsig.IntersectPolicies(tc.first, tc.second) + if !errors.Is(err, tc.want) || !reflect.DeepEqual(got, xmldsig.Policy{}) { + t.Fatalf("intersection = %+v, %v; want zero and %v", got, err, tc.want) + } + }) + } +} + +func TestIntersectPoliciesOwnsAllowlists(t *testing.T) { + first := xmldsig.Policy{ + AllowedSignatureMethods: []string{xmldsig.RSASHA512, xmldsig.RSASHA256}, + AllowedDigestMethods: []string{xmldsig.SHA512, xmldsig.SHA256}, + } + second := xmldsig.Policy{ + AllowedSignatureMethods: []string{xmldsig.RSASHA256, xmldsig.RSASHA512}, + AllowedDigestMethods: []string{xmldsig.SHA256, xmldsig.SHA512}, + } + beforeFirst, beforeSecond := first, second + beforeFirst.AllowedSignatureMethods = slices.Clone(first.AllowedSignatureMethods) + beforeFirst.AllowedDigestMethods = slices.Clone(first.AllowedDigestMethods) + beforeSecond.AllowedSignatureMethods = slices.Clone(second.AllowedSignatureMethods) + beforeSecond.AllowedDigestMethods = slices.Clone(second.AllowedDigestMethods) + got, err := xmldsig.IntersectPolicies(first, second) + if err != nil { + t.Fatal(err) + } + + if !reflect.DeepEqual(first, beforeFirst) || !reflect.DeepEqual(second, beforeSecond) { + t.Fatal("intersection mutated an input policy") + } + + first.AllowedSignatureMethods[0] = xmldsig.ECDSASHA256 + first.AllowedDigestMethods[0] = xmldsig.SHA384 + second.AllowedSignatureMethods[1] = xmldsig.ECDSASHA384 + second.AllowedDigestMethods[1] = xmldsig.SHA384 + if !slices.Equal(got.AllowedSignatureMethods, beforeFirst.AllowedSignatureMethods) || !slices.Equal(got.AllowedDigestMethods, beforeFirst.AllowedDigestMethods) { + t.Fatal("intersection result aliases an input allowlist") + } + + got.AllowedSignatureMethods[1] = xmldsig.ECDSASHA512 + got.AllowedDigestMethods[1] = xmldsig.SHA384 + if first.AllowedSignatureMethods[1] != xmldsig.RSASHA256 || first.AllowedDigestMethods[1] != xmldsig.SHA256 || second.AllowedSignatureMethods[0] != xmldsig.RSASHA256 || second.AllowedDigestMethods[0] != xmldsig.SHA256 { + t.Fatal("mutating the result changed an input allowlist") + } +} diff --git a/xmldsig/parameters_xml.go b/xmldsig/parameters_xml.go index 6340c9d..7c9062d 100644 --- a/xmldsig/parameters_xml.go +++ b/xmldsig/parameters_xml.go @@ -11,12 +11,31 @@ import ( "github.com/skarm/xmlsec/internal/tree" ) +const ( + decimalRadix = 10 + maximumParameterTextBytes = 64 + parameterIntegerBits = 32 +) + +const ( + pssTrailerFieldLexical = "1" +) + +const ( + nameHMACOutputLength = "HMACOutputLength" + nameMaskGenerationFunction = "MaskGenerationFunction" + nameRSAPSSParams = "RSAPSSParams" + nameSaltLength = "SaltLength" + nameTrailerField = "TrailerField" + prefixRSAPSS = "pss" +) + func pssElement(n *tree.Node, local string) bool { return n != nil && n.Kind == tree.ElementNode && n.Name == (document.Name{Space: PSSNamespace, Local: local}) } func newPSS(local string) *tree.Node { - return &tree.Node{Kind: tree.ElementNode, Name: document.Name{Space: PSSNamespace, Local: local}, Prefix: "pss"} + return &tree.Node{Kind: tree.ElementNode, Name: document.Name{Space: PSSNamespace, Local: local}, Prefix: prefixRSAPSS} } // xmlInteger accepts the XML Schema signed integer lexical form, but bounds @@ -33,16 +52,16 @@ func xmlInteger(n *tree.Node) (int, error) { return 0, fmt.Errorf("%w: integer must contain only text", ErrStructure) } - if value.Len()+len(c.Data) > 64 { + if value.Len()+len(c.Data) > maximumParameterTextBytes { return 0, fmt.Errorf("%w: integer too long", ErrStructure) } value.WriteString(c.Data) } - v := strings.Trim(value.String(), " \t\r\n") + v := strings.Trim(value.String(), lexicon.XMLWhitespace) - number, err := strconv.ParseInt(v, 10, 32) + number, err := strconv.ParseInt(v, decimalRadix, parameterIntegerBits) if err != nil { return 0, fmt.Errorf("%w: invalid integer", ErrStructure) } @@ -51,12 +70,12 @@ func xmlInteger(n *tree.Node) (int, error) { } func parseSignatureMethod(n *tree.Node, p Policy) (signatureSpec, error) { - children, err := structure(n, lexicon.DSigElementSignatureMethod, "Algorithm") + children, err := structure(n, lexicon.DSigElementSignatureMethod, lexicon.SecurityAttrAlgorithm) if err != nil { return signatureSpec{}, err } - method, err := requiredAttribute(n, "Algorithm") + method, err := requiredAttribute(n, lexicon.SecurityAttrAlgorithm) if err != nil { return signatureSpec{}, err } @@ -75,7 +94,7 @@ func parseSignatureMethod(n *tree.Node, p Policy) (signatureSpec, error) { } } } else if _, ok := macHash(method); ok { - if len(children) > 1 || len(children) == 1 && !isDS(children[0], "HMACOutputLength") { + if len(children) > 1 || len(children) == 1 && !isDS(children[0], nameHMACOutputLength) { return signatureSpec{}, fmt.Errorf("%w: HMAC parameters", ErrStructure) } @@ -102,7 +121,7 @@ func parseSignatureMethod(n *tree.Node, p Policy) (signatureSpec, error) { } func parsePSS(n *tree.Node) (*PSSParameters, error) { - if !pssElement(n, "RSAPSSParams") { + if !pssElement(n, nameRSAPSSParams) { return nil, fmt.Errorf("%w: expected pss:RSAPSSParams", ErrStructure) } @@ -134,7 +153,7 @@ func parsePSS(n *tree.Node) (*PSSParameters, error) { p.MGFHash = p.Hash - if pos < len(children) && pssElement(children[pos], "MaskGenerationFunction") { + if pos < len(children) && pssElement(children[pos], nameMaskGenerationFunction) { mgf := children[pos] pos++ @@ -145,7 +164,7 @@ func parsePSS(n *tree.Node) (*PSSParameters, error) { } p.SaltLength = p.Hash.Size() - if pos < len(children) && pssElement(children[pos], "SaltLength") { + if pos < len(children) && pssElement(children[pos], nameSaltLength) { p.SaltLength, err = xmlInteger(children[pos]) if err != nil { return nil, err @@ -154,7 +173,7 @@ func parsePSS(n *tree.Node) (*PSSParameters, error) { pos++ } - if pos < len(children) && pssElement(children[pos], "TrailerField") { + if pos < len(children) && pssElement(children[pos], nameTrailerField) { p.TrailerField, err = xmlInteger(children[pos]) if err != nil { return nil, err @@ -181,39 +200,39 @@ func signatureMethodNode(method string, params SignatureParameters) (*tree.Node, } sm := newDS(lexicon.DSigElementSignatureMethod) - setAttr(sm, "Algorithm", method) + setAttr(sm, lexicon.SecurityAttrAlgorithm, method) // Keep omission vs explicit defaults stable in a caller's template. When // supplied, emit every effective PSS parameter to avoid ambiguous output. if spec.pss != nil && params.PSS != nil { p := spec.pss - root := newPSS("RSAPSSParams") - root.NS = []tree.Namespace{{Prefix: "pss", URI: PSSNamespace}} + root := newPSS(nameRSAPSSParams) + root.NS = []tree.Namespace{{Prefix: prefixRSAPSS, URI: PSSNamespace}} dm := newDS(lexicon.DSigElementDigestMethod) uri, _ := digestURI(p.Hash) - setAttr(dm, "Algorithm", uri) + setAttr(dm, lexicon.SecurityAttrAlgorithm, uri) add(root, dm) - mgf := newPSS("MaskGenerationFunction") - setAttr(mgf, "Algorithm", MGF1) + mgf := newPSS(nameMaskGenerationFunction) + setAttr(mgf, lexicon.SecurityAttrAlgorithm, MGF1) md := newDS(lexicon.DSigElementDigestMethod) uri, _ = digestURI(p.MGFHash) - setAttr(md, "Algorithm", uri) + setAttr(md, lexicon.SecurityAttrAlgorithm, uri) add(mgf, md) add(root, mgf) - salt := newPSS("SaltLength") + salt := newPSS(nameSaltLength) text(salt, strconv.Itoa(p.SaltLength)) add(root, salt) - trailer := newPSS("TrailerField") - text(trailer, "1") + trailer := newPSS(nameTrailerField) + text(trailer, pssTrailerFieldLexical) add(root, trailer) add(sm, root) } if spec.hmacBits != 0 && params.HMACOutputLength != 0 { - length := newDS("HMACOutputLength") + length := newDS(nameHMACOutputLength) text(length, strconv.Itoa(spec.hmacBits)) add(sm, length) } @@ -223,11 +242,11 @@ func signatureMethodNode(method string, params SignatureParameters) (*tree.Node, func parseMGFHash(mgf *tree.Node, fallback crypto.Hash) (crypto.Hash, error) { hash := fallback - if err := attributes(mgf, "Algorithm"); err != nil { + if err := attributes(mgf, lexicon.SecurityAttrAlgorithm); err != nil { return fallback, err } - if uri, exists := getAttr(mgf, document.Name{Local: "Algorithm"}); exists && uri != MGF1 { + if uri, exists := getAttr(mgf, document.Name{Local: lexicon.SecurityAttrAlgorithm}); exists && uri != MGF1 { return fallback, fmt.Errorf("%w: unsupported mask generation function", ErrAlgorithm) } diff --git a/xmldsig/policy.go b/xmldsig/policy.go index fe521ca..c5e023d 100644 --- a/xmldsig/policy.go +++ b/xmldsig/policy.go @@ -9,6 +9,21 @@ import ( "github.com/skarm/xmlsec/internal/tree" ) +const ( + defaultMaxCanonicalBytes = 32 << 20 + defaultMaxHMACKeyBytes = 4096 + defaultMaxPSSSaltBytes = 1024 + defaultMaxRSAKeyBits = 8192 + defaultMaxReferences = 16 + maximumHMACOutputBits = 512 + maximumRSAKeyBits = 16384 + maximumReferences = 64 + // Minimum byte length accepted for an explicitly supplied HMAC key. + minimumHMACKeyBytes = 32 + minimumHMACOutputBits = 128 + minimumRSAKeyBits = 2048 +) + // Policy restricts signature algorithms, key sizes and processing work. // Zero numeric fields select DefaultPolicy, except MinPSSSaltBytes: zero permits // zero-length salt. Each operation copies the policy. Callers must keep supplied @@ -41,6 +56,11 @@ type Policy struct { // MinHMACOutputBits sets the minimum accepted tag length; zero selects 128. // Tags must also cover at least half the hash output, in whole bytes. MinHMACOutputBits int + // AllowLegacySigning permits RSA-SHA1 generation only when the signature + // allowlist also permits it. SHA-1 reference digests need a separate digest + // allowlist entry. Verification permission alone never enables legacy signing. + // SHA-1 remains disabled in FIPS mode. + AllowLegacySigning bool // AllowHMACTruncation permits shorter tags subject to MinHMACOutputBits. // False requires a full tag. AllowHMACTruncation bool @@ -50,7 +70,7 @@ type Policy struct { func DefaultPolicy() Policy { return Policy{ AllowedSignatureMethods: []string{RSASHA256, RSASHA384, RSASHA512, ECDSASHA256, ECDSASHA384, ECDSASHA512}, - AllowedDigestMethods: []string{SHA256, SHA384, SHA512}, MinRSABits: 2048, MaxRSABits: 8192, MaxReferences: 16, MaxCanonicalBytes: 32 << 20, MaxPSSSaltBytes: 1024, MinHMACKeyBytes: 32, MaxHMACKeyBytes: 4096, MinHMACOutputBits: 128, + AllowedDigestMethods: []string{SHA256, SHA384, SHA512}, MinRSABits: minimumRSAKeyBits, MaxRSABits: defaultMaxRSAKeyBits, MaxReferences: defaultMaxReferences, MaxCanonicalBytes: defaultMaxCanonicalBytes, MaxPSSSaltBytes: defaultMaxPSSSaltBytes, MinHMACKeyBytes: minimumHMACKeyBytes, MaxHMACKeyBytes: defaultMaxHMACKeyBytes, MinHMACOutputBits: minimumHMACOutputBits, } } @@ -81,7 +101,8 @@ func HMACPolicy() Policy { // LegacyVerificationPolicy opts into SHA-1 reference digests and RSA-SHA1/DSA-SHA1 // verification. DSA is limited to the 1024/160 profile. Legacy signature -// generation remains disabled; modern defaults and RSA size limits are retained. +// generation remains disabled unless AllowLegacySigning is separately enabled; +// modern defaults and RSA size limits are retained. // This policy does not enable HMAC. Legacy policies are disabled in FIPS mode. func LegacyVerificationPolicy() Policy { p := ModernPolicy() @@ -102,6 +123,69 @@ func LegacyHMACPolicy() Policy { return p } +// IntersectPolicies returns a policy that enforces both input policies. Each +// input is validated and its zero fields select the usual Policy defaults. +// The result owns its algorithm lists and retains the order of the first input. +// Neither input is modified. +// +// Incompatible bounds or disjoint signature or digest allowlists return an +// error and a zero policy. A disjoint digest allowlist is rejected even for +// message-only uses: Policy cannot express an empty allowlist without restoring +// the default algorithms. Callers must not use the returned policy on error. +func IntersectPolicies(first, second Policy) (Policy, error) { + a, err := normalizePolicy(first) + if err != nil { + return Policy{}, fmt.Errorf("first policy: %w", err) + } + + b, err := normalizePolicy(second) + if err != nil { + return Policy{}, fmt.Errorf("second policy: %w", err) + } + + p := Policy{ + AllowLegacySigning: a.AllowLegacySigning && b.AllowLegacySigning, + AllowedSignatureMethods: intersectPolicyMethods(a.AllowedSignatureMethods, b.AllowedSignatureMethods), + AllowedDigestMethods: intersectPolicyMethods(a.AllowedDigestMethods, b.AllowedDigestMethods), + MinRSABits: max(a.MinRSABits, b.MinRSABits), + MaxRSABits: min(a.MaxRSABits, b.MaxRSABits), + MaxReferences: min(a.MaxReferences, b.MaxReferences), + MaxCanonicalBytes: min(a.MaxCanonicalBytes, b.MaxCanonicalBytes), + MinPSSSaltBytes: max(a.MinPSSSaltBytes, b.MinPSSSaltBytes), + MaxPSSSaltBytes: min(a.MaxPSSSaltBytes, b.MaxPSSSaltBytes), + MinHMACKeyBytes: max(a.MinHMACKeyBytes, b.MinHMACKeyBytes), + MaxHMACKeyBytes: min(a.MaxHMACKeyBytes, b.MaxHMACKeyBytes), + MinHMACOutputBits: max(a.MinHMACOutputBits, b.MinHMACOutputBits), + AllowHMACTruncation: a.AllowHMACTruncation && b.AllowHMACTruncation, + } + if len(p.AllowedSignatureMethods) == 0 || len(p.AllowedDigestMethods) == 0 { + return Policy{}, fmt.Errorf("%w: disjoint algorithm allowlists", ErrPolicy) + } + + p, err = normalizePolicy(p) + if err != nil { + return Policy{}, fmt.Errorf("intersected policy: %w", err) + } + + return p, nil +} + +func intersectPolicyMethods(first, second []string) []string { + allowed := make(map[string]struct{}, len(second)) + for _, method := range second { + allowed[method] = struct{}{} + } + + result := make([]string, 0, min(len(first), len(second))) + for _, method := range first { + if _, ok := allowed[method]; ok { + result = append(result, method) + } + } + + return result +} + func normalizePolicy(p Policy) (Policy, error) { d := DefaultPolicy() if len(p.AllowedSignatureMethods) == 0 { @@ -132,7 +216,7 @@ func normalizePolicy(p Policy) (Policy, error) { p.MaxCanonicalBytes = d.MaxCanonicalBytes } - if p.MinRSABits < 2048 || p.MaxRSABits < p.MinRSABits || p.MaxRSABits > 16384 || p.MaxReferences < 1 || p.MaxReferences > 64 || p.MaxCanonicalBytes < 1 { + if p.MinRSABits < minimumRSAKeyBits || p.MaxRSABits < p.MinRSABits || p.MaxRSABits > maximumRSAKeyBits || p.MaxReferences < 1 || p.MaxReferences > maximumReferences || p.MaxCanonicalBytes < 1 { return p, fmt.Errorf("%w: invalid limits", ErrPolicy) } @@ -152,7 +236,7 @@ func normalizePolicy(p Policy) (Policy, error) { p.MinHMACOutputBits = d.MinHMACOutputBits } - if p.MinPSSSaltBytes < 0 || p.MaxPSSSaltBytes < p.MinPSSSaltBytes || p.MaxPSSSaltBytes > 2048 || p.MinHMACKeyBytes < 32 || p.MaxHMACKeyBytes < p.MinHMACKeyBytes || p.MaxHMACKeyBytes > 1<<20 || p.MinHMACOutputBits < 128 || p.MinHMACOutputBits > 512 || p.MinHMACOutputBits%8 != 0 { + if p.MinPSSSaltBytes < 0 || p.MaxPSSSaltBytes < p.MinPSSSaltBytes || p.MaxPSSSaltBytes > maximumPSSSaltBytes || p.MinHMACKeyBytes < minimumHMACKeyBytes || p.MaxHMACKeyBytes < p.MinHMACKeyBytes || p.MaxHMACKeyBytes > maximumHMACKeyBytes || p.MinHMACOutputBits < minimumHMACOutputBits || p.MinHMACOutputBits > maximumHMACOutputBits || p.MinHMACOutputBits%signatureOctetBits != 0 { return p, fmt.Errorf("%w: invalid PSS/HMAC limits", ErrPolicy) } @@ -177,7 +261,7 @@ func permits(allowed []string, value string) bool { func normalizeID(name document.Name) (document.Name, error) { if name == (document.Name{}) { - name.Local = "ID" + name.Local = defaultIDAttribute } if !tree.NCName(name.Local) || name.Space == tree.XMLNSNamespace { diff --git a/xmldsig/provider.go b/xmldsig/provider.go index 898cb25..4a690f7 100644 --- a/xmldsig/provider.go +++ b/xmldsig/provider.go @@ -93,7 +93,7 @@ func SignMessageReader(ctx context.Context, src io.Reader, signer crypto.Signer, return nil, err } - return credential.signPrepared(ctx, spec, digest) + return credential.signPrepared(ctx, spec, p, digest) } // VerifyMessage validates only a signature over exact message bytes, not XML, diff --git a/xmldsig/reference.go b/xmldsig/reference.go index 2f8e75e..9335d71 100644 --- a/xmldsig/reference.go +++ b/xmldsig/reference.go @@ -64,8 +64,8 @@ func processTo(ctx context.Context, dst io.Writer, data reference.Data, ts []ref return out, nil } -// processData shares an aggregate octet-production budget across SignedInfo and -// every reference. Intermediate outputs count, not only the last representation. +// writeCanonicalElement streams a complete subtree through the selected +// canonicalization transform, charging the shared octet-production budget. func writeCanonicalElement(ctx context.Context, dst io.Writer, e document.Element, method reference.Transform, remaining *int) error { set, err := e.Subtree(ctx) if err != nil { diff --git a/xmldsig/resource_stream.go b/xmldsig/resource_stream.go index 1c7d420..06c7164 100644 --- a/xmldsig/resource_stream.go +++ b/xmldsig/resource_stream.go @@ -18,12 +18,18 @@ import ( // Explicit external Manifest traversal also retains its bounded original bytes // until they can be authenticated and parsed in their own document scope. type referenceSource struct { - resolver Resolver - uri string - limits ResourceLimits - rawDigest []byte + // resolver reopens the explicitly authorized external resource. + resolver Resolver + // uri is the exact resolved URI passed to the resolver. + uri string + // limits retains the resource bounds used for verification and replay. + limits ResourceLimits + // rawDigest binds replay to the originally verified raw resource bytes. + rawDigest []byte + // captureManifest permits retaining bounded original Manifest bytes. captureManifest bool - rawManifest []byte + // rawManifest retains those original bytes when Manifest capture is enabled. + rawManifest []byte } func (s *referenceSource) write(ctx context.Context, dst io.Writer, ts []reference.Transform, opt reference.Options, remaining, resources *int) (*reference.Report, error) { diff --git a/xmldsig/resources.go b/xmldsig/resources.go index 6566522..7c5b729 100644 --- a/xmldsig/resources.go +++ b/xmldsig/resources.go @@ -18,6 +18,11 @@ import ( "github.com/skarm/xmlsec/reference" ) +const ( + maximumAllowedURIs = 1024 + resourceDigestChunkBytes = 32 << 10 +) + // ExternalReferences authorizes external octets for the general Signature API. // It supplies an adapter rather than enabling implicit network access. URI spelling // is exact unless BaseURI enables relative resolution. KeyInfo discovery uses @@ -51,15 +56,25 @@ type ExternalReferences struct { } type resourceSession struct { - source Resolver - allowed map[string]struct{} - cache map[[32]byte]ReferenceResult - fingerprints map[string][]byte - limits ResourceLimits - remaining int - captureManifests bool + // source is the application-owned external resolver. + source Resolver + // allowed contains the exact external URIs authorized by policy. + allowed map[string]struct{} + // cache reuses results only for matching reference-processing fingerprints. + cache map[[sha256.Size]byte]ReferenceResult + // fingerprints binds each fetched URI to its original raw-resource digest. + fingerprints map[string][]byte + // limits retains normalized per-resource and aggregate bounds. + limits ResourceLimits + // remaining is the aggregate raw external-resource byte budget still available. + remaining int + // captureManifests enables retaining original resource bytes for Manifest + // traversal. + captureManifests bool + // captureTransformed enables retaining transformed Manifest input. captureTransformed bool - baseURI *url.URL + // baseURI optionally enables trusted relative URI resolution. + baseURI *url.URL } func newResourceSession(opt *ExternalReferences) (*resourceSession, error) { @@ -67,7 +82,7 @@ func newResourceSession(opt *ExternalReferences) (*resourceSession, error) { return nil, nil } - if nilcheck.IsNil(opt.Resolver) || len(opt.AllowedURIs) == 0 || len(opt.AllowedURIs) > 1024 { + if nilcheck.IsNil(opt.Resolver) || len(opt.AllowedURIs) == 0 || len(opt.AllowedURIs) > maximumAllowedURIs { return nil, fmt.Errorf("%w: external resolver and explicit bounded URI allowlist required", ErrPolicy) } @@ -76,7 +91,7 @@ func newResourceSession(opt *ExternalReferences) (*resourceSession, error) { return nil, err } - s := &resourceSession{source: opt.Resolver, allowed: make(map[string]struct{}), cache: make(map[[32]byte]ReferenceResult), fingerprints: make(map[string][]byte), limits: limits, remaining: limits.MaxTotalResourceBytes} + s := &resourceSession{source: opt.Resolver, allowed: make(map[string]struct{}), cache: make(map[[sha256.Size]byte]ReferenceResult), fingerprints: make(map[string][]byte), limits: limits, remaining: limits.MaxTotalResourceBytes} s.baseURI, err = resourceBaseURI(opt.BaseURI) if err != nil { @@ -89,7 +104,11 @@ func newResourceSession(opt *ExternalReferences) (*resourceSession, error) { } if s.baseURI != nil { - parsed, _ := url.Parse(uri) // validated above + parsed, err := url.Parse(uri) + if err != nil { + return nil, fmt.Errorf("%w: invalid external URI: %w", ErrPolicy, err) + } + if !parsed.IsAbs() { return nil, fmt.Errorf("%w: resolved absolute URI allowlist required with BaseURI", ErrPolicy) } @@ -248,7 +267,9 @@ func cacheableExternalTransforms(transforms []reference.Transform) bool { // Keep only a fixed-size cache key, without Base64/JSON copies of stylesheets. // Length-delimited source bytes preserve exact transform identity; application // allowlist fingerprints remain the separate public ProgramFingerprint format. -func referenceCacheKey(ctx context.Context, ref coreReference) ([32]byte, error) { +func referenceCacheKey(ctx context.Context, ref coreReference) ([sha256.Size]byte, error) { + const stylesheetLengthFieldBytes = 8 // big-endian Uint64 framing before bytes + metadata := append([]reference.Transform(nil), ref.transforms...) for i := range metadata { metadata[i].Stylesheet = nil @@ -261,29 +282,29 @@ func referenceCacheKey(ctx context.Context, ref coreReference) ([32]byte, error) Transforms []reference.Transform }{ref.uri, ref.method, ref.typeURI, metadata}) if err != nil { - return [32]byte{}, err + return [sha256.Size]byte{}, err } hash := sha256.New() _, _ = hash.Write(encoded) - var length [8]byte + var length [stylesheetLengthFieldBytes]byte for _, tr := range ref.transforms { binary.BigEndian.PutUint64(length[:], uint64(len(tr.Stylesheet))) _, _ = hash.Write(length[:]) for data := tr.Stylesheet; len(data) != 0; { if err := ctx.Err(); err != nil { - return [32]byte{}, err + return [sha256.Size]byte{}, err } - size := min(len(data), 32<<10) + size := min(len(data), resourceDigestChunkBytes) _, _ = hash.Write(data[:size]) data = data[size:] } } - var result [32]byte + var result [sha256.Size]byte hash.Sum(result[:0]) return result, ctx.Err() diff --git a/xmldsig/shared_constants.go b/xmldsig/shared_constants.go index a07e181..4b53630 100644 --- a/xmldsig/shared_constants.go +++ b/xmldsig/shared_constants.go @@ -1,6 +1,23 @@ package xmldsig const ( - nameResolve = "resolve" + // Identifies the reference-coverage check in detailed verification diagnostics. + stageCoverage = "coverage" + // Identifies reference digest verification in detailed diagnostics. + stageDigest = "digest" + // Identifies transform-policy checks performed before reference resource I/O. + stagePreflight = "preflight" + // Identifies reference transform execution in detailed verification diagnostics. + stageProcess = "process" + // Identifies XML structure checks that precede cryptographic verification. + stageStructure = "structure" + // Names a ds:Manifest; recursive checking follows the caller's ManifestPolicy. + nameManifest = "Manifest" +) + +const ( + // Diagnostic stage label for explicitly authorized resource resolution. + nameResolve = "resolve" + // Diagnostic stage label for reference transform processing. nameTransform = "transform" ) diff --git a/xmldsig/sign.go b/xmldsig/sign.go index 39becfd..4ffb609 100644 --- a/xmldsig/sign.go +++ b/xmldsig/sign.go @@ -106,18 +106,18 @@ func SignEnveloped(ctx context.Context, d *document.Document, signer crypto.Sign } signature := newDS(lexicon.DSigElementSignature) - signature.NS = []tree.Namespace{{Prefix: "ds", URI: Namespace}} + signature.NS = []tree.Namespace{{Prefix: lexicon.PrefixDSig, URI: Namespace}} signature.Parent = root si := newDS(lexicon.DSigElementSignedInfo) add(signature, si) cm := newDS(lexicon.DSigElementCanonicalizationMethod) - setAttr(cm, "Algorithm", c14n.Exclusive) + setAttr(cm, lexicon.SecurityAttrAlgorithm, c14n.Exclusive) addInclusive(cm, opt.InclusivePrefixes) add(si, cm) sm := newDS(lexicon.DSigElementSignatureMethod) - setAttr(sm, "Algorithm", opt.SignatureMethod) + setAttr(sm, lexicon.SecurityAttrAlgorithm, opt.SignatureMethod) add(si, sm) h, err := hashForDigest(opt.DigestMethod) @@ -148,23 +148,23 @@ func SignEnveloped(ctx context.Context, d *document.Document, signer crypto.Sign } ref := newDS(lexicon.DSigElementReference) - setAttr(ref, "URI", "#"+id) + setAttr(ref, lexicon.SecurityAttrURI, "#"+id) ts := newDS(lexicon.DSigElementTransforms) if target == root { tr := newDS(lexicon.DSigElementTransform) - setAttr(tr, "Algorithm", EnvelopedSignature) + setAttr(tr, lexicon.SecurityAttrAlgorithm, EnvelopedSignature) add(ts, tr) } tr := newDS(lexicon.DSigElementTransform) - setAttr(tr, "Algorithm", c14n.Exclusive) + setAttr(tr, lexicon.SecurityAttrAlgorithm, c14n.Exclusive) addInclusive(tr, opt.InclusivePrefixes) add(ts, tr) add(ref, ts) dm := newDS(lexicon.DSigElementDigestMethod) - setAttr(dm, "Algorithm", opt.DigestMethod) + setAttr(dm, lexicon.SecurityAttrAlgorithm, opt.DigestMethod) add(ref, dm) dv := newDS(lexicon.DSigElementDigestValue) @@ -197,7 +197,7 @@ func SignEnveloped(ctx context.Context, d *document.Document, signer crypto.Sign return nil, err } - value, err := credential.signPrepared(ctx, spec, canonical) + value, err := credential.signPrepared(ctx, spec, p, canonical) if err != nil { return nil, err } @@ -214,7 +214,7 @@ func SignEnveloped(ctx context.Context, d *document.Document, signer crypto.Sign } func newDS(local string) *tree.Node { - return &tree.Node{Kind: tree.ElementNode, Name: document.Name{Space: Namespace, Local: local}, Prefix: "ds"} + return &tree.Node{Kind: tree.ElementNode, Name: document.Name{Space: Namespace, Local: local}, Prefix: lexicon.PrefixDSig} } func setAttr(n *tree.Node, name, value string) { @@ -231,8 +231,8 @@ func addInclusive(parent *tree.Node, prefixes []string) { return } - n := &tree.Node{Kind: tree.ElementNode, Name: document.Name{Space: c14n.Exclusive, Local: "InclusiveNamespaces"}, Prefix: "ec", NS: []tree.Namespace{{Prefix: "ec", URI: c14n.Exclusive}}} - setAttr(n, "PrefixList", strings.Join(prefixes, " ")) + n := &tree.Node{Kind: tree.ElementNode, Name: document.Name{Space: c14n.Exclusive, Local: lexicon.C14NElementInclusiveNamespaces}, Prefix: lexicon.PrefixExclusiveC14N, NS: []tree.Namespace{{Prefix: lexicon.PrefixExclusiveC14N, URI: c14n.Exclusive}}} + setAttr(n, lexicon.C14NAttrPrefixList, strings.Join(prefixes, " ")) add(parent, n) } @@ -272,7 +272,7 @@ func normalizeEnvelopedAlgorithms(pub crypto.PublicKey, opt SignOptions, p Polic opt.DigestMethod = SHA256 } - if opt.SignatureMethod == DSASHA1 || opt.SignatureMethod == RSASHA1 { + if opt.SignatureMethod == DSASHA1 || opt.SignatureMethod == RSASHA1 && !p.AllowLegacySigning { return opt, fmt.Errorf("%w: legacy signature generation is disabled", ErrPolicy) } diff --git a/xmldsig/template.go b/xmldsig/template.go index 46ed387..3a40c18 100644 --- a/xmldsig/template.go +++ b/xmldsig/template.go @@ -14,6 +14,11 @@ import ( "github.com/skarm/xmlsec/internal/tree" ) +const ( + maximumCoverageRequirements = 64 + maximumPreservedNodes = 64 +) + // SignatureRequirement supplies explicit trust for an existing signature that must // remain valid during template signing. Signature and Options.RequiredElements // handles must belong to the original input snapshot. @@ -85,7 +90,7 @@ func signTemplate(ctx context.Context, doc *document.Document, selected document return nil, err } - if len(opt.Preserve) > 64 { + if len(opt.Preserve) > maximumPreservedNodes { return nil, fmt.Errorf("%w: preservation count", ErrPolicy) } @@ -119,7 +124,7 @@ func signTemplate(ctx context.Context, doc *document.Document, selected document return nil, err } // Validate the method/key match before any expensive provider operation. - if err := credential.validateSigner(original.spec); err != nil { + if err := credential.validateSigner(original.spec, original.policy); err != nil { return nil, err } @@ -169,7 +174,7 @@ func signTemplate(ctx context.Context, doc *document.Document, selected document return nil, err } - value, err := credential.signPrepared(ctx, plan.spec, canonical) + value, err := credential.signPrepared(ctx, plan.spec, plan.policy, canonical) if err != nil { return nil, err } @@ -216,7 +221,7 @@ func signTemplate(ctx context.Context, doc *document.Document, selected document } func checkPreservation(ctx context.Context, doc *document.Document, selected document.Element, requirements []SignatureRequirement) error { - if len(requirements) > 64 { + if len(requirements) > maximumCoverageRequirements { return fmt.Errorf("%w: preservation count", ErrPolicy) } @@ -247,7 +252,7 @@ func checkPreservation(ctx context.Context, doc *document.Document, selected doc } req, hasRequirement := required[n] - if emptyValue(sv, lexicon.DSigElementSignatureValue, "Id") == nil { + if emptyValue(sv, lexicon.DSigElementSignatureValue, lexicon.SecurityAttrID) == nil { if hasRequirement { return fmt.Errorf("%w: cannot preserve an unsigned template", ErrPolicy) } @@ -346,7 +351,7 @@ func fillTemplateDigests(ctx context.Context, out *document.Document, plan *core for _, task := range order { _, checked, err := processCoreTask(ctx, out, plan, budget, task) if err != nil { - return nil, task.wrap("process", err) + return nil, task.wrap(stageProcess, err) } replaceValue(task.ref.digestNode, base64.StdEncoding.EncodeToString(checked.digest)) diff --git a/xmldsig/transforms.go b/xmldsig/transforms.go index de55439..1c30e8b 100644 --- a/xmldsig/transforms.go +++ b/xmldsig/transforms.go @@ -31,7 +31,7 @@ func transformXMLError(err error) error { func prepareProgramReferences(ctx context.Context, p *corePlan) error { for _, task := range p.graph.tasks { if err := reference.PrepareTransforms(ctx, task.ref.transforms, p.processing); err != nil { - return task.wrap("preflight", processingError(err)) + return task.wrap(stagePreflight, processingError(err)) } } diff --git a/xmldsig/transforms_test.go b/xmldsig/transforms_test.go index 8ec8f82..8f1e4a5 100644 --- a/xmldsig/transforms_test.go +++ b/xmldsig/transforms_test.go @@ -62,6 +62,8 @@ func TestExternalExtensionContextIsNotReused(t *testing.T) { opt.Processing.Extension = scopedExtension{base: engine, blocked: blocked} result, report, err := xmldsig.VerifySignatureDetailed(t.Context(), signed, signed.Root(), key.Public(), opt) + // Verification reports retain diagnostics on failure. + //noinspection GoDfaErrorMayBeNotNil if !errors.Is(err, reference.ErrPolicy) || result != nil || report.SignatureValue != xmldsig.Valid || report.References[0].Status != xmldsig.Valid || report.References[1].Status != xmldsig.Disallowed { t.Fatalf("cached extension bypassed context: result=%t signature=%s references=%v error=%v", result != nil, report.SignatureValue, report.References, err) } @@ -114,7 +116,7 @@ func testTransformXMLDSigCase(t *testing.T, tt *struct { t.Fatal(err) } - opt := xmldsig.SignatureOptions{ExpectedURIs: []string{namePayload}, Processing: reference.DefaultOptions()} + opt := xmldsig.SignatureOptions{ExpectedURIs: []string{payloadReferenceURI}, Processing: reference.DefaultOptions()} opt.Processing.Extension = p opt.Processing.AllowedTransforms = append(opt.Processing.AllowedTransforms, tt.tr.Algorithm) input := coreParse(t, `SIGNED-VALUEold-value`) @@ -124,7 +126,7 @@ func testTransformXMLDSigCase(t *testing.T, tt *struct { ts = append(ts, reference.Transform{Algorithm: c14n.Exclusive}) } - template, selected, e := xmldsig.BuildTemplate(ctx, input, input.Root(), xmldsig.SignatureTemplate{SignatureMethod: xmldsig.ECDSASHA256, References: []xmldsig.ReferenceTemplate{{URI: namePayload, Transforms: ts}}}) + template, selected, e := xmldsig.BuildTemplate(ctx, input, input.Root(), xmldsig.SignatureTemplate{SignatureMethod: xmldsig.ECDSASHA256, References: []xmldsig.ReferenceTemplate{{URI: payloadReferenceURI, Transforms: ts}}}) if e != nil { t.Fatal(e) } @@ -185,12 +187,12 @@ func TestV1TransformURIsRequireV1Processor(t *testing.T) { } } - template, selected, err := xmldsig.BuildTemplate(t.Context(), d, d.Root(), xmldsig.SignatureTemplate{SignatureMethod: xmldsig.ECDSASHA256, References: []xmldsig.ReferenceTemplate{{URI: namePayload, Transforms: []reference.Transform{tr}}}}) + template, selected, err := xmldsig.BuildTemplate(t.Context(), d, d.Root(), xmldsig.SignatureTemplate{SignatureMethod: xmldsig.ECDSASHA256, References: []xmldsig.ReferenceTemplate{{URI: payloadReferenceURI, Transforms: []reference.Transform{tr}}}}) if err != nil { t.Fatal(err) } - options := xmldsig.SignatureOptions{ExpectedURIs: []string{namePayload}, Processing: reference.DefaultOptions()} + options := xmldsig.SignatureOptions{ExpectedURIs: []string{payloadReferenceURI}, Processing: reference.DefaultOptions()} options.Processing.AllowedTransforms = append(options.Processing.AllowedTransforms, algorithm) result, err := xmldsig.SignTemplate(t.Context(), template, selected, coreEC(t), xmldsig.TemplateSignOptions{Validation: options}) @@ -214,7 +216,7 @@ func TestTransformBuilderRejectsReservedPrefixRebinding(t *testing.T) { doc := coreParse(t, `value`) - template, _, err := xmldsig.BuildTemplate(t.Context(), doc, doc.Root(), xmldsig.SignatureTemplate{SignatureMethod: xmldsig.ECDSASHA256, References: []xmldsig.ReferenceTemplate{{URI: namePayload, Transforms: []reference.Transform{{Algorithm: test.algorithm, XPath: []reference.XPathExpression{parameter}}}}}}) + template, _, err := xmldsig.BuildTemplate(t.Context(), doc, doc.Root(), xmldsig.SignatureTemplate{SignatureMethod: xmldsig.ECDSASHA256, References: []xmldsig.ReferenceTemplate{{URI: payloadReferenceURI, Transforms: []reference.Transform{{Algorithm: test.algorithm, XPath: []reference.XPathExpression{parameter}}}}}}) if template != nil || !errors.Is(err, xmldsig.ErrPolicy) { t.Fatal("reserved transform prefix changed meaning", err) } @@ -234,7 +236,7 @@ func TestTemplateStylesheetNamespaceImport(t *testing.T) { host := coreParse(t, ``) built, selected, err := xmldsig.BuildTemplate(t.Context(), host, host.Root(), xmldsig.SignatureTemplate{ - SignatureMethod: xmldsig.ECDSASHA256, References: []xmldsig.ReferenceTemplate{{URI: nameData, Transforms: []reference.Transform{program}}}, + SignatureMethod: xmldsig.ECDSASHA256, References: []xmldsig.ReferenceTemplate{{URI: dataReferenceURI, Transforms: []reference.Transform{program}}}, }) if err != nil { t.Fatal(err) @@ -251,7 +253,7 @@ func TestTemplateStylesheetNamespaceImport(t *testing.T) { processing := reference.DefaultOptions() processing.Extension = e processing.AllowedTransforms = append(processing.AllowedTransforms, reference.XSLT) - options := xmldsig.SignatureOptions{ExpectedURIs: []string{nameData}, Processing: processing} + options := xmldsig.SignatureOptions{ExpectedURIs: []string{dataReferenceURI}, Processing: processing} key := coreEC(t) signed, err := xmldsig.SignTemplate(t.Context(), built, selected, key, xmldsig.TemplateSignOptions{Validation: options}) diff --git a/xmldsig/verify.go b/xmldsig/verify.go index efdb50f..0ea1940 100644 --- a/xmldsig/verify.go +++ b/xmldsig/verify.go @@ -92,7 +92,7 @@ func Verify(ctx context.Context, d *document.Document, key crypto.PublicKey, opt return nil, err } - children, err := structure(sig, lexicon.DSigElementSignature, "Id") + children, err := structure(sig, lexicon.DSigElementSignature, lexicon.SecurityAttrID) if err != nil { return nil, err } @@ -103,7 +103,7 @@ func Verify(ctx context.Context, d *document.Document, key crypto.PublicKey, opt si, sv := children[0], children[1] - signed, err := structure(si, lexicon.DSigElementSignedInfo, "Id") + signed, err := structure(si, lexicon.DSigElementSignedInfo, lexicon.SecurityAttrID) if err != nil { return nil, err } @@ -130,12 +130,12 @@ func Verify(ctx context.Context, d *document.Document, key crypto.PublicKey, opt return nil, fmt.Errorf("%w: signature algorithm", ErrPolicy) } - signatureValue, err := base64Value(ctx, sv, lexicon.DSigElementSignatureValue, 2048, "Id") + signatureValue, err := base64Value(ctx, sv, lexicon.DSigElementSignatureValue, maximumSignatureBytes, lexicon.SecurityAttrID) if err != nil { return nil, err } - refs, err := parseEnvelopedReferenceSet(ctx, d, idAttr, signed[2:], sig, p) + refs, err := parseEnvelopedReferenceSet(ctx, d, idAttr, signed[signedInfoHeaderChildren:], sig, p) if err != nil { return nil, err } @@ -367,7 +367,7 @@ func requiredAttribute(n *tree.Node, name string) (string, error) { } func simpleAlgorithm(n *tree.Node, local string) (string, error) { - children, err := structure(n, local, "Algorithm") + children, err := structure(n, local, lexicon.SecurityAttrAlgorithm) if err != nil { return "", err } @@ -376,7 +376,7 @@ func simpleAlgorithm(n *tree.Node, local string) (string, error) { return "", fmt.Errorf("%w: algorithm parameters unsupported", ErrAlgorithm) } - return requiredAttribute(n, "Algorithm") + return requiredAttribute(n, lexicon.SecurityAttrAlgorithm) } func parseCanonicalization(n *tree.Node, local string) ([]string, error) { @@ -429,18 +429,20 @@ func base64Value(ctx context.Context, n *tree.Node, local string, maxBytes int, } func parseReference(ctx context.Context, n *tree.Node, ids map[string]*tree.Node, root, sig *tree.Node, p Policy) (parsedReference, error) { + const referenceChildren = 3 // Transforms, DigestMethod and DigestValue + var ref parsedReference - children, err := structure(n, lexicon.DSigElementReference, "URI", "Id") + children, err := structure(n, lexicon.DSigElementReference, lexicon.SecurityAttrURI, lexicon.SecurityAttrID) if err != nil { return ref, err } - if len(children) != 3 || !isDS(children[0], lexicon.DSigElementTransforms) || !isDS(children[1], lexicon.DSigElementDigestMethod) || !isDS(children[2], lexicon.DSigElementDigestValue) { + if len(children) != referenceChildren || !isDS(children[0], lexicon.DSigElementTransforms) || !isDS(children[1], lexicon.DSigElementDigestMethod) || !isDS(children[2], lexicon.DSigElementDigestValue) { return ref, fmt.Errorf("%w: Reference children", ErrStructure) } - uri, err := requiredAttribute(n, "URI") + uri, err := requiredAttribute(n, lexicon.SecurityAttrURI) if err != nil { return ref, err } @@ -519,7 +521,12 @@ func parseReference(ctx context.Context, n *tree.Node, ids map[string]*tree.Node // validateSimpleSignatureChildren restricts this profile to SignedInfo, // SignatureValue, and an optional untrusted KeyInfo. func validateSimpleSignatureChildren(children []*tree.Node) error { - if len(children) < 2 || len(children) > 3 || !isDS(children[0], lexicon.DSigElementSignedInfo) || !isDS(children[1], lexicon.DSigElementSignatureValue) || len(children) == 3 && !isDS(children[2], lexicon.DSigElementKeyInfo) { + const ( + signatureRequiredChildren = 2 + signatureMaximumChildren = 3 + ) + + if len(children) < signatureRequiredChildren || len(children) > signatureMaximumChildren || !isDS(children[0], lexicon.DSigElementSignedInfo) || !isDS(children[1], lexicon.DSigElementSignatureValue) || len(children) == signatureMaximumChildren && !isDS(children[2], lexicon.DSigElementKeyInfo) { return fmt.Errorf("%w: Signature children", ErrStructure) } @@ -552,7 +559,7 @@ func verifyEnvelopedReferences(ctx context.Context, refs []parsedReference, reso } if subtle.ConstantTimeCompare(digest.Sum(nil), ref.digest) != 1 { - return &ReferenceError{i, ref.uri, "digest", ErrDigest} + return &ReferenceError{i, ref.uri, stageDigest, ErrDigest} } result.references = append(result.references, ReferenceResult{uri: ref.uri, digestMethod: ref.digestMethod, target: tree.Wrap(d, ref.target), processed: processed, resolver: resolver, processing: processing, digest: digest.Sum(nil), transforms: ref.transforms}) @@ -562,7 +569,7 @@ func verifyEnvelopedReferences(ctx context.Context, refs []parsedReference, reso } func validateEnvelopedSignedInfo(signed []*tree.Node, maximum int) error { - if len(signed) < 3 || len(signed)-2 > maximum || !isDS(signed[0], lexicon.DSigElementCanonicalizationMethod) || !isDS(signed[1], lexicon.DSigElementSignatureMethod) { + if len(signed) < signedInfoMinimumChildren || len(signed)-signedInfoHeaderChildren > maximum || !isDS(signed[0], lexicon.DSigElementCanonicalizationMethod) || !isDS(signed[1], lexicon.DSigElementSignatureMethod) { return fmt.Errorf("%w: SignedInfo children or reference count", ErrStructure) } @@ -574,19 +581,36 @@ func validateEnvelopedSignedInfo(signed []*tree.Node, maximum int) error { // subresource bytes. Target is valid only for a same-document element reference; // it is invalid for whole-document and external octet references. type ReferenceResult struct { - uri string - digestMethod string - typeURI string - target document.Element - processed *reference.Report - resolver *reference.SameDocumentResolver - external *referenceSource - processing reference.Options - digest []byte - manifestInput []byte - transforms []reference.Transform - coverageDoc *document.Document - coverageRoot *tree.Node + // uri retains the original signed Reference URI spelling. + uri string + // digestMethod is the authenticated DigestMethod URI. + digestMethod string + // typeURI retains the signed Reference Type URI. + typeURI string + // target identifies a same-document element target; other reference forms leave + // it invalid. + target document.Element + // processed retains immutable transform execution diagnostics. + processed *reference.Report + // resolver retains the same-document snapshot and ID index for approved replay. + resolver *reference.SameDocumentResolver + // external retains the authorized source and raw-byte binding for external + // replay. + external *referenceSource + // processing retains the approved transform policy for replay. + processing reference.Options + // digest owns the authenticated digest of the processed reference input. + digest []byte + // manifestInput retains bounded original bytes for explicit Manifest traversal. + manifestInput []byte + // transforms retains the authenticated transform chain in execution order. + transforms []reference.Transform + // coverageDoc binds coverage claims to the exact checked document snapshot. + coverageDoc *document.Document + // coverageRoot identifies the conservatively proven covered subtree, if any. + coverageRoot *tree.Node + // coverageExclude identifies the Signature subtree actually removed by an + // enveloped transform. coverageExclude *tree.Node } diff --git a/xmlenc/agreement.go b/xmlenc/agreement.go index 6f83fa7..6f75cc8 100644 --- a/xmlenc/agreement.go +++ b/xmlenc/agreement.go @@ -18,6 +18,24 @@ import ( "github.com/skarm/xmlsec/internal/tree" ) +const ( + // Bounds logical key-name metadata before UTF-8 validation or graph matching, in bytes. + maximumKeyNameBytes = 1024 + p256CoordinateBytes = 32 + p384CoordinateBytes = 48 + p521CoordinateBytes = 66 + uncompressedECPointPrefix = 4 + // Algorithm-qualified key-agreement parameters checked against caller policy. Compare the expected + // XML Encryption namespace when matching it. + nameAgreementMethod = "AgreementMethod" + // Peer public-key material for a key-agreement step. Compare the expected XML Encryption namespace + // when matching it. + nameOriginatorKeyInfo = "OriginatorKeyInfo" + // Recipient-side key information accompanying a key agreement. Compare the expected XML Encryption + // namespace when matching it. + nameRecipientKeyInfo = "RecipientKeyInfo" +) + const ( // ECDHES is XML Encryption 1.1 ephemeral-static elliptic-curve agreement. ECDHES = Namespace11 + "ECDH-ES" @@ -57,16 +75,6 @@ type ECDHRecipient interface { ECDH(*ecdh.PublicKey) ([]byte, error) } -// AdaptContextECDHRecipient adapts an explicit contextual provider. XML decrypt -// passes the invocation context; calling the adapter directly uses Background. -func AdaptContextECDHRecipient(provider ContextECDHRecipient) (ECDHRecipient, error) { - if nilcheck.IsNil(provider) { - return nil, ErrPolicy - } - - return &contextECDHAdapter{provider}, nil -} - // ContextECDHRecipient accepts the caller's cancellation and deadline context for // key agreement. Its returned secret follows ECDHRecipient's ownership and // full-width encoding contract. @@ -77,27 +85,14 @@ type ContextECDHRecipient interface { ECDH(context.Context, *ecdh.PublicKey) ([]byte, error) } -type contextECDHAdapter struct{ ContextECDHRecipient } - -func (a *contextECDHAdapter) ECDH(peer *ecdh.PublicKey) ([]byte, error) { - return a.ContextECDHRecipient.ECDH(context.Background(), peer) -} - -func (a *contextECDHAdapter) agreement(ctx context.Context, peer *ecdh.PublicKey) ([]byte, error) { - return a.ContextECDHRecipient.ECDH(ctx, peer) -} - -type contextualAgreement interface { - agreement(context.Context, *ecdh.PublicKey) ([]byte, error) -} +// AdaptContextECDHRecipient adapts an explicit contextual provider. XML decrypt +// passes the invocation context; calling the adapter directly uses Background. +func AdaptContextECDHRecipient(provider ContextECDHRecipient) (ECDHRecipient, error) { + if nilcheck.IsNil(provider) { + return nil, ErrPolicy + } -type agreementParameters struct { - options ECDHOptions - kdf ConcatKDFParameters - curve ecdh.Curve - curveURI string - secretBytes int - kekBytes int + return &contextECDHAdapter{provider}, nil } // EncryptBytesWithECDH uses an explicit static ECDH recipient and AES key wrap. @@ -204,8 +199,31 @@ func DecryptContentWithECDH(ctx context.Context, doc *document.Document, selecte return decryptContent(ctx, doc, node, p) } +type contextECDHAdapter struct{ ContextECDHRecipient } + +func (a *contextECDHAdapter) ECDH(peer *ecdh.PublicKey) ([]byte, error) { + return a.ContextECDHRecipient.ECDH(context.Background(), peer) +} + +func (a *contextECDHAdapter) agreement(ctx context.Context, peer *ecdh.PublicKey) ([]byte, error) { + return a.ContextECDHRecipient.ECDH(ctx, peer) +} + +type contextualAgreement interface { + agreement(context.Context, *ecdh.PublicKey) ([]byte, error) +} + +type agreementParameters struct { + options ECDHOptions + kdf ConcatKDFParameters + curve ecdh.Curve + curveURI string + secretBytes int + kekBytes int +} + func agreementProfile(ctx context.Context, recipient *ecdh.PublicKey, options ECDHOptions) (profile, agreementParameters, error) { - if options.Data.OAEP != nil { + if options.Data.OAEP != nil || options.Data.KeyTransportAlgorithm != "" { return profile{}, agreementParameters{}, ErrPolicy } @@ -235,11 +253,11 @@ func agreementProfile(ctx context.Context, recipient *ecdh.PublicKey, options EC switch options.KeyAlgorithm { case AES128KeyWrap: - kekBytes = 16 + kekBytes = aes128KeyBytes case AES192KeyWrap: - kekBytes = 24 + kekBytes = aes192KeyBytes case AES256KeyWrap: - kekBytes = 32 + kekBytes = aes256KeyBytes default: return profile{}, agreementParameters{}, ErrPolicy } @@ -266,7 +284,7 @@ func agreementProfile(ctx context.Context, recipient *ecdh.PublicKey, options EC } } // Check the complete KDF policy before obtaining a private operation. - probe, err := DeriveConcatKey(ctx, []byte{1}, kekBytes*8, kdf) + probe, err := DeriveConcatKey(ctx, []byte{1}, kekBytes*keyOctetBits, kdf) clear(probe) if err != nil { @@ -279,23 +297,23 @@ func agreementProfile(ctx context.Context, recipient *ecdh.PublicKey, options EC func agreementCurve(curve ecdh.Curve) (ecdh.Curve, string, int) { switch curve { case ecdh.P256(): - return curve, lexicon.CurveP256, 32 + return curve, lexicon.CurveP256, p256CoordinateBytes case ecdh.P384(): - return curve, lexicon.CurveP384, 48 + return curve, lexicon.CurveP384, p384CoordinateBytes case ecdh.P521(): - return curve, lexicon.CurveP521, 66 + return curve, lexicon.CurveP521, p521CoordinateBytes default: return nil, "", 0 } } func agreementKeyName(name string) bool { - if len(name) > 1024 || !utf8.ValidString(name) || strings.TrimSpace(name) != name { + if len(name) > maximumKeyNameBytes || !utf8.ValidString(name) || strings.TrimSpace(name) != name { return false } for _, r := range name { - if r < 0x20 || r == 0x7f || r == 0xfffe || r == 0xffff { + if r < minimumKeyNameRune || r == keyNameDeleteRune || r == keyNameNoncharacterFFFE || r == keyNameNoncharacterFFFF { return false } } @@ -320,7 +338,7 @@ func ecdhEncryptProfile(ctx context.Context, recipient *ecdh.PublicKey, options } defer clear(secret) - kek, err := DeriveConcatKey(ctx, secret, parameters.kekBytes*8, parameters.kdf) + kek, err := DeriveConcatKey(ctx, secret, parameters.kekBytes*keyOctetBits, parameters.kdf) if err != nil { return profile{}, err } @@ -356,7 +374,7 @@ func ecdhDecryptProfile(ctx context.Context, recipient ECDHRecipient, options EC } func ecdhDecryptKeyTransport(ctx context.Context, recipient ECDHRecipient, options ECDHOptions, keyBytes int) (keyTransport, error) { - if keyBytes != 16 && keyBytes != 24 && keyBytes != 32 { + if keyBytes != aes128KeyBytes && keyBytes != aes192KeyBytes && keyBytes != aes256KeyBytes { return keyTransport{}, ErrPolicy } @@ -374,7 +392,7 @@ func ecdhDecryptKeyTransport(ctx context.Context, recipient ECDHRecipient, optio var originator *ecdh.PublicKey transport := keyTransport{ - algorithm: parameters.options.KeyAlgorithm, wrappedBytes: keyBytes + 8, keyBits: parameters.kekBytes * 8, + algorithm: parameters.options.KeyAlgorithm, wrappedBytes: keyBytes + keyWrapBlockBytes, keyBits: parameters.kekBytes * keyOctetBits, checkKeyInfo: func(ctx context.Context, info document.Element) error { var err error @@ -410,7 +428,7 @@ func ecdhDecryptKeyTransport(ctx context.Context, recipient ECDHRecipient, optio return nil, ErrDecrypt } - kek, err := DeriveConcatKey(ctx, secret, parameters.kekBytes*8, parameters.kdf) + kek, err := DeriveConcatKey(ctx, secret, parameters.kekBytes*keyOctetBits, parameters.kdf) if err != nil { return nil, err } @@ -434,19 +452,19 @@ func encodeAgreement(ctx context.Context, originator, recipient *ecdh.PublicKey, return nil, err } - info := newElement(SignatureNamespace, "ds", lexicon.DSigElementKeyInfo) - method := encryptedElement("AgreementMethod") - setAttribute(method, "Algorithm", ECDHES) + info := newElement(SignatureNamespace, lexicon.PrefixDSig, lexicon.DSigElementKeyInfo) + method := encryptedElement(nameAgreementMethod) + setAttribute(method, lexicon.SecurityAttrAlgorithm, ECDHES) appendChild(method, kdf) - sender := encryptedElement("OriginatorKeyInfo") + sender := encryptedElement(nameOriginatorKeyInfo) appendChild(sender, agreementKeyValue(originator, p.curveURI)) appendChild(method, sender) - receiver := encryptedElement("RecipientKeyInfo") + receiver := encryptedElement(nameRecipientKeyInfo) if p.options.RecipientKeyName != "" { - name := newElement(SignatureNamespace, "ds", lexicon.DSigElementKeyName) + name := newElement(SignatureNamespace, lexicon.PrefixDSig, lexicon.DSigElementKeyName) appendChild(name, &tree.Node{Kind: tree.TextNode, Data: p.options.RecipientKeyName}) appendChild(receiver, name) } @@ -459,13 +477,13 @@ func encodeAgreement(ctx context.Context, originator, recipient *ecdh.PublicKey, } func agreementKeyValue(public *ecdh.PublicKey, uri string) *tree.Node { - value := newElement(SignatureNamespace, "ds", lexicon.DSigElementKeyValue) - ec := newElement(SignatureNamespace11, "dsig11", "ECKeyValue") - ec.NS = []tree.Namespace{{Prefix: "dsig11", URI: SignatureNamespace11}} - curve := newElement(SignatureNamespace11, "dsig11", "NamedCurve") - setAttribute(curve, "URI", uri) + value := newElement(SignatureNamespace, lexicon.PrefixDSig, lexicon.DSigElementKeyValue) + ec := newElement(SignatureNamespace11, lexicon.PrefixDSig11, lexicon.DSigElementECKeyValue) + ec.NS = []tree.Namespace{{Prefix: lexicon.PrefixDSig11, URI: SignatureNamespace11}} + curve := newElement(SignatureNamespace11, lexicon.PrefixDSig11, lexicon.DSigElementNamedCurve) + setAttribute(curve, lexicon.SecurityAttrURI, uri) - point := newElement(SignatureNamespace11, "dsig11", "PublicKey") + point := newElement(SignatureNamespace11, lexicon.PrefixDSig11, lexicon.DSigElementPublicKey) appendChild(point, &tree.Node{Kind: tree.TextNode, Data: base64.StdEncoding.EncodeToString(public.Bytes())}) appendChild(ec, curve) appendChild(ec, point) @@ -475,7 +493,13 @@ func agreementKeyValue(public *ecdh.PublicKey, uri string) *tree.Node { } func decodeAgreement(ctx context.Context, info document.Element, recipient *ecdh.PublicKey, p agreementParameters) (*ecdh.PublicKey, error) { - children, err := childElements(info, elementName(Namespace, "AgreementMethod")) + // KeyDerivationMethod and OriginatorKeyInfo are required; RecipientKeyInfo is optional. + const ( + agreementRequiredChildren = 2 + agreementMaximumChildren = 3 + ) + + children, err := childElements(info, elementName(Namespace, nameAgreementMethod)) if err != nil || info.Name() != elementName(SignatureNamespace, lexicon.DSigElementKeyInfo) || !exactAttributes(info) { return nil, ErrFormat } @@ -486,13 +510,13 @@ func decodeAgreement(ctx context.Context, info document.Element, recipient *ecdh } children = method.Children() - if len(children) < 2 || len(children) > 3 { + if len(children) < agreementRequiredChildren || len(children) > agreementMaximumChildren { return nil, ErrFormat } - names := []document.Name{elementName(Namespace11, "KeyDerivationMethod"), elementName(Namespace, "OriginatorKeyInfo")} - if len(children) == 3 { - names = append(names, elementName(Namespace, "RecipientKeyInfo")) + names := []document.Name{elementName(Namespace11, nameKeyDerivationMethod), elementName(Namespace, nameOriginatorKeyInfo)} + if len(children) == agreementMaximumChildren { + names = append(names, elementName(Namespace, nameRecipientKeyInfo)) } children, err = childElements(method, names...) @@ -514,7 +538,7 @@ func decodeAgreement(ctx context.Context, info document.Element, recipient *ecdh return nil, err } - if len(children) == 3 { + if len(children) == agreementMaximumChildren { if err := checkAgreementRecipient(ctx, children[2], recipient, p); err != nil { return nil, err } @@ -582,13 +606,13 @@ func decodeAgreementKey(ctx context.Context, info document.Element, p agreementP } func decodeAgreementKeyValue(ctx context.Context, value document.Element, p agreementParameters) (*ecdh.PublicKey, error) { - ec, err := childElements(value, elementName(SignatureNamespace11, "ECKeyValue")) + ec, err := childElements(value, elementName(SignatureNamespace11, lexicon.DSigElementECKeyValue)) if err != nil || !exactAttributes(value) || !exactAttributes(ec[0]) { return nil, ErrFormat } - fields, err := childElements(ec[0], elementName(SignatureNamespace11, "NamedCurve"), elementName(SignatureNamespace11, "PublicKey")) - if err != nil || !exactAttributes(fields[0], "URI") || attributeValue(fields[0], "URI") != p.curveURI || len(fields[0].Children()) != 0 || !xmlSpace(fields[0].Text()) || !exactAttributes(fields[1]) || len(fields[1].Children()) != 0 { + fields, err := childElements(ec[0], elementName(SignatureNamespace11, lexicon.DSigElementNamedCurve), elementName(SignatureNamespace11, lexicon.DSigElementPublicKey)) + if err != nil || !exactAttributes(fields[0], lexicon.SecurityAttrURI) || attributeValue(fields[0], lexicon.SecurityAttrURI) != p.curveURI || len(fields[0].Children()) != 0 || !xmlSpace(fields[0].Text()) || !exactAttributes(fields[1]) || len(fields[1].Children()) != 0 { return nil, ErrFormat } @@ -597,7 +621,7 @@ func decodeAgreementKeyValue(ctx context.Context, value document.Element, p agre return nil, err } - if len(encoded) != 1+2*p.secretBytes || encoded[0] != 4 { + if len(encoded) != 1+2*p.secretBytes || encoded[0] != uncompressedECPointPrefix { return nil, ErrFormat } diff --git a/xmlenc/codec.go b/xmlenc/codec.go index fab962d..a52beb2 100644 --- a/xmlenc/codec.go +++ b/xmlenc/codec.go @@ -12,11 +12,35 @@ import ( "github.com/skarm/xmlsec/internal/tree" ) +const ( + maxTransportKeyInfoBytes = 64 << 10 + profileMarkupBytes = 8192 + // Extra lexical byte allowance for whitespace around a bounded Base64 CipherValue. + cipherValueWhitespaceBytes = 4096 + maximumWrappedRSAKeyBytes = 2048 + // Container selecting one CipherValue or CipherReference representation. Compare the expected XML + // Encryption namespace when matching it. + nameCipherData = "CipherData" + // Base64 ciphertext whose decoded size is checked against the byte budget. Compare the expected + // XML Encryption namespace when matching it. + nameCipherValue = "CipherValue" + // Encrypted element/content wrapper; Type controls plaintext interpretation. Compare the expected + // XML Encryption namespace when matching it. + nameEncryptedData = "EncryptedData" + // On-wire cipher or key-transport declaration matched to configured algorithms. Compare the + // expected XML Encryption namespace when matching it. + nameEncryptionMethod = "EncryptionMethod" + // Suggested prefix for generated XML Encryption 1.0 elements; identity comes from Namespace. + prefixXEnc = "xenc" +) + func newElement(namespace, prefix, local string) *tree.Node { return &tree.Node{Kind: tree.ElementNode, Name: document.Name{Space: namespace, Local: local}, Prefix: prefix} } -func encryptedElement(local string) *tree.Node { return newElement(Namespace, "xenc", local) } +func encryptedElement(local string) *tree.Node { + return newElement(Namespace, prefixXEnc, local) +} func appendChild(parent, child *tree.Node) { parent.Children = append(parent.Children, child) @@ -33,8 +57,8 @@ func encodeCipherData(ctx context.Context, ciphertext []byte) (*tree.Node, error return nil, err } - data := encryptedElement("CipherData") - value := encryptedElement("CipherValue") + data := encryptedElement(nameCipherData) + value := encryptedElement(nameCipherValue) appendChild(value, &tree.Node{Kind: tree.TextNode, Data: encoded}) appendChild(data, value) @@ -42,17 +66,17 @@ func encodeCipherData(ctx context.Context, ciphertext []byte) (*tree.Node, error } func encodeProfile(ctx context.Context, wrapped, encrypted []byte, p profile, typ string) (*document.Document, error) { - root := encryptedElement("EncryptedData") + root := encryptedElement(nameEncryptedData) - method := encryptedElement("EncryptionMethod") - setAttribute(method, "Algorithm", p.options.DataAlgorithm) + method := encryptedElement(nameEncryptionMethod) + setAttribute(method, lexicon.SecurityAttrAlgorithm, p.options.DataAlgorithm) appendChild(root, method) if p.direct == nil { - keyInfo := newElement(SignatureNamespace, "ds", lexicon.DSigElementKeyInfo) - encryptedKey := encryptedElement("EncryptedKey") - keyMethod := encryptedElement("EncryptionMethod") - setAttribute(keyMethod, "Algorithm", p.transport.algorithm) + keyInfo := newElement(SignatureNamespace, lexicon.PrefixDSig, lexicon.DSigElementKeyInfo) + encryptedKey := encryptedElement(nameEncryptedKey) + keyMethod := encryptedElement(nameEncryptionMethod) + setAttribute(keyMethod, lexicon.SecurityAttrAlgorithm, p.transport.algorithm) if rsaOAEPAlgorithm(p.transport.algorithm) { encodeOAEP(keyMethod, p.transport.oaep) @@ -73,8 +97,8 @@ func encodeProfile(ctx context.Context, wrapped, encrypted []byte, p profile, ty appendChild(keyInfo, encryptedKey) appendChild(root, keyInfo) } else if p.keyName != "" { - keyInfo := newElement(SignatureNamespace, "ds", lexicon.DSigElementKeyInfo) - name := newElement(SignatureNamespace, "ds", lexicon.DSigElementKeyName) + keyInfo := newElement(SignatureNamespace, lexicon.PrefixDSig, lexicon.DSigElementKeyInfo) + name := newElement(SignatureNamespace, lexicon.PrefixDSig, lexicon.DSigElementKeyName) appendChild(name, &tree.Node{Kind: tree.TextNode, Data: p.keyName}) appendChild(keyInfo, name) appendChild(root, keyInfo) @@ -82,9 +106,9 @@ func encodeProfile(ctx context.Context, wrapped, encrypted []byte, p profile, ty limits := document.DefaultLimits() - limits.MaxBytes = base64.StdEncoding.EncodedLen(p.options.MaxPlaintextBytes+p.data.overhead()) + 8192 + limits.MaxBytes = base64.StdEncoding.EncodedLen(p.options.MaxPlaintextBytes+p.data.overhead()) + profileMarkupBytes if p.transport.keyInfo != nil { - size, err := tree.WriteXMLFragment(ctx, io.Discard, []*tree.Node{p.transport.keyInfo}, 64<<10) + size, err := tree.WriteXMLFragment(ctx, io.Discard, []*tree.Node{p.transport.keyInfo}, maxTransportKeyInfoBytes) if err != nil { return nil, errors.Join(ErrPolicy, err) } @@ -194,12 +218,12 @@ func childElements(element document.Element, names ...document.Name) ([]document } func matchesMethod(element document.Element, algorithm string, empty bool) bool { - return exactAttributes(element, "Algorithm") && attributeValue(element, "Algorithm") == algorithm && + return exactAttributes(element, lexicon.SecurityAttrAlgorithm) && attributeValue(element, lexicon.SecurityAttrAlgorithm) == algorithm && (!empty || (len(element.Children()) == 0 && xmlSpace(element.Text()))) } func decodeCipherValue(ctx context.Context, element document.Element, maxBytes int) ([]byte, error) { - children, err := childElements(element, elementName(Namespace, "CipherValue")) + children, err := childElements(element, elementName(Namespace, nameCipherValue)) if err != nil || !exactAttributes(element) || !exactAttributes(children[0]) || len(children[0].Children()) != 0 { return nil, ErrFormat } @@ -210,7 +234,7 @@ func decodeCipherValue(ctx context.Context, element document.Element, maxBytes i func decodeBase64Text(ctx context.Context, element document.Element, maxBytes int) ([]byte, error) { _, node := tree.Unwrap(element) - value, err := tree.DecodeBase64(ctx, node, base64.StdEncoding.EncodedLen(maxBytes)+4096, maxBytes) + value, err := tree.DecodeBase64(ctx, node, base64.StdEncoding.EncodedLen(maxBytes)+cipherValueWhitespaceBytes, maxBytes) if errors.Is(err, tree.ErrLimit) { return nil, errors.Join(ErrPolicy, err) } @@ -223,7 +247,7 @@ func decodeBase64Text(ctx context.Context, element document.Element, maxBytes in } func decodeProfile(ctx context.Context, doc *document.Document, typ string, p profile) ([]byte, []byte, error) { - if doc == nil || doc.Root().Name() != elementName(Namespace, "EncryptedData") { + if doc == nil || doc.Root().Name() != elementName(Namespace, nameEncryptedData) { return nil, nil, ErrFormat } @@ -232,7 +256,7 @@ func decodeProfile(ctx context.Context, doc *document.Document, typ string, p pr if !exactAttributes(root) { return nil, nil, ErrFormat } - } else if !exactAttributes(root, "Type") || attributeValue(root, "Type") != typ { + } else if !exactAttributes(root, lexicon.SecurityAttrType) || attributeValue(root, lexicon.SecurityAttrType) != typ { return nil, nil, ErrFormat } @@ -240,26 +264,26 @@ func decodeProfile(ctx context.Context, doc *document.Document, typ string, p pr return decodeDirectProfile(ctx, root, p) } - children, err := childElements(root, elementName(Namespace, "EncryptionMethod"), elementName(SignatureNamespace, lexicon.DSigElementKeyInfo), elementName(Namespace, "CipherData")) + children, err := childElements(root, elementName(Namespace, nameEncryptionMethod), elementName(SignatureNamespace, lexicon.DSigElementKeyInfo), elementName(Namespace, nameCipherData)) if err != nil { return nil, nil, ErrFormat } - if err := checkEncryptionMethod(ctx, children[0], p.options.DataAlgorithm, p.keyBytes*8, false); err != nil { + if err := checkEncryptionMethod(ctx, children[0], p.options.DataAlgorithm, p.keyBytes*keyOctetBits, false); err != nil { return nil, nil, err } - keyInfo, err := childElements(children[1], elementName(Namespace, "EncryptedKey")) + keyInfo, err := childElements(children[1], elementName(Namespace, nameEncryptedKey)) if err != nil || !exactAttributes(children[1]) { return nil, nil, ErrFormat } - keyNames := []document.Name{elementName(Namespace, "EncryptionMethod")} + keyNames := []document.Name{elementName(Namespace, nameEncryptionMethod)} if p.transport.checkKeyInfo != nil { keyNames = append(keyNames, elementName(SignatureNamespace, lexicon.DSigElementKeyInfo)) } - keyNames = append(keyNames, elementName(Namespace, "CipherData")) + keyNames = append(keyNames, elementName(Namespace, nameCipherData)) key, err := childElements(keyInfo[0], keyNames...) if err != nil || !exactAttributes(keyInfo[0]) { @@ -287,7 +311,7 @@ func decodeProfile(ctx context.Context, doc *document.Document, typ string, p pr } // At most a 16384-bit RSA ciphertext. Key Wrap additionally requires the // exact session-key length plus eight bytes before any unwrap operations. - wrapped, err := decodeCipherValue(ctx, key[cipherIndex], 2048) + wrapped, err := decodeCipherValue(ctx, key[cipherIndex], maximumWrappedRSAKeyBytes) if err != nil { return nil, nil, err } diff --git a/xmlenc/content.go b/xmlenc/content.go index ddaf027..399fa9a 100644 --- a/xmlenc/content.go +++ b/xmlenc/content.go @@ -90,7 +90,7 @@ func contentParent(doc *document.Document, parent document.Element) (*tree.Node, return nil, err } - if node.Name.Space == Namespace && (node.Name.Local == "EncryptedData" || node.Name.Local == "EncryptedKey") { + if node.Name.Space == Namespace && (node.Name.Local == nameEncryptedData || node.Name.Local == nameEncryptedKey) { return nil, ErrFormat } diff --git a/xmlenc/content_test.go b/xmlenc/content_test.go index 992e13c..bdf9532 100644 --- a/xmlenc/content_test.go +++ b/xmlenc/content_test.go @@ -142,7 +142,7 @@ func TestContentAuthenticatedXMLAndLimits(t *testing.T) { before := xmlBytes(t, encrypted) result, handle, err := xmlenc.DecryptContentWithKeyWrap(ctx, encrypted, encrypted.Root().Children()[0], kek, xmlenc.KeyWrapOptions{}) - if result != nil || handle.Valid() || !errors.Is(err, xmlenc.ErrDecrypt) || !bytes.Equal(before, xmlBytes(t, encrypted)) { + if result != nil || handle != (document.Element{}) || !errors.Is(err, xmlenc.ErrDecrypt) || !bytes.Equal(before, xmlBytes(t, encrypted)) { t.Fatal("invalid content leaked a result or mutated source", err) } }) @@ -168,7 +168,7 @@ func TestContentAuthenticatedXMLAndLimits(t *testing.T) { encrypted := authenticatedContent(t, []byte(``), prefix, suffix) result, handle, err := xmlenc.DecryptContentWithKeyWrap(ctx, encrypted, encrypted.Root().Children()[0], kek, xmlenc.KeyWrapOptions{Data: xmlenc.Options{XMLLimits: limits}}) - if result != nil || handle.Valid() || !errors.Is(err, xmlenc.ErrDecrypt) { + if result != nil || handle != (document.Element{}) || !errors.Is(err, xmlenc.ErrDecrypt) { t.Fatal("plaintext limit bypassed", err) } }) @@ -184,7 +184,7 @@ func TestContentSelectionAndSuperEncryption(t *testing.T) { t.Fatal(err) } - if result, handle, err := xmlenc.EncryptContentWithKeyWrap(ctx, inner, cipherElement, kek, xmlenc.KeyWrapOptions{}); result != nil || handle.Valid() || !errors.Is(err, xmlenc.ErrFormat) { + if result, handle, err := xmlenc.EncryptContentWithKeyWrap(ctx, inner, cipherElement, kek, xmlenc.KeyWrapOptions{}); result != nil || handle != (document.Element{}) || !errors.Is(err, xmlenc.ErrFormat) { t.Fatal("partial super-encryption accepted", err) } @@ -216,7 +216,7 @@ func TestContentSelectionAndSuperEncryption(t *testing.T) { sameXML(t, source, full) for _, foreign := range []document.Element{source.Root(), {}} { - if result, handle, err := xmlenc.DecryptContentWithKeyWrap(ctx, outer, foreign, kek, xmlenc.KeyWrapOptions{}); result != nil || handle.Valid() || !errors.Is(err, xmlenc.ErrFormat) { + if result, handle, err := xmlenc.DecryptContentWithKeyWrap(ctx, outer, foreign, kek, xmlenc.KeyWrapOptions{}); result != nil || handle != (document.Element{}) || !errors.Is(err, xmlenc.ErrFormat) { t.Fatal("foreign/empty handle accepted", err) } } @@ -255,7 +255,7 @@ func FuzzContentPlaintext(f *testing.F) { } if err != nil { - if result != nil || parent.Valid() || !errors.Is(err, xmlenc.ErrDecrypt) { + if result != nil || parent != (document.Element{}) || !errors.Is(err, xmlenc.ErrDecrypt) { t.Fatal("failure exposed a result", err) } @@ -271,27 +271,6 @@ func FuzzContentPlaintext(f *testing.F) { }) } -// Correctly authenticated bytes force XML processing, independently of tags. -func authenticatedContent(t testing.TB, raw []byte, prefix, suffix string) *document.Document { - t.Helper() - - encrypted, err := xmlenc.EncryptBytesWithKeyWrap(t.Context(), raw, make([]byte, 32), xmlenc.KeyWrapOptions{}) - if err != nil { - t.Fatal(err) - } - - wire := bytes.Replace(xmlBytes(t, encrypted), []byte("e\u0301")) diff --git a/xmlenc/fragment.go b/xmlenc/fragment.go index fc4755d..fa98e79 100644 --- a/xmlenc/fragment.go +++ b/xmlenc/fragment.go @@ -9,9 +9,15 @@ import ( "strings" "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/tree" ) +const ( + fragmentContextName = "xmlsec-context" + maximumXMLDepth = 512 +) + // The wrapper carries only the actual parent's namespace context. EncryptedData // declarations and synthetic namespace prefixes must not leak into plaintext: // even an unused added prefix can invalidate an inclusive XML signature. @@ -25,7 +31,7 @@ func namespaceContext(ctx context.Context, node *tree.Node) ([]tree.Namespace, e } for _, ns := range node.NS { - if _, exists := seen[ns.Prefix]; ns.Prefix != "xml" && !exists { + if _, exists := seen[ns.Prefix]; ns.Prefix != lexicon.PrefixXML && !exists { bindings[ns.Prefix] = ns.URI seen[ns.Prefix] = struct{}{} } @@ -87,7 +93,7 @@ func parseContentPlaintext(ctx context.Context, raw []byte, parent *tree.Node, l return nil, err } - frame := &tree.Node{Kind: tree.ElementNode, Name: document.Name{Local: "xmlsec-context"}, NS: namespaces} + frame := &tree.Node{Kind: tree.ElementNode, Name: document.Name{Local: fragmentContextName}, NS: namespaces} frameDoc := tree.FromNodes([]*tree.Node{frame}, limits) // Stream escaping of inherited namespace values under the complete source // byte budget; a small plaintext budget must not hide unbounded wrapper I/O. @@ -101,7 +107,7 @@ func parseContentPlaintext(ctx context.Context, raw []byte, parent *tree.Node, l parseLimits := limits parseLimits.MaxBytes += wrapped.Len() + len(closing) - if parseLimits.MaxDepth < 512 { + if parseLimits.MaxDepth < maximumXMLDepth { parseLimits.MaxDepth++ } diff --git a/xmlenc/graph.go b/xmlenc/graph.go index 555f9e7..8f5e8f2 100644 --- a/xmlenc/graph.go +++ b/xmlenc/graph.go @@ -12,6 +12,22 @@ import ( "github.com/skarm/xmlsec/internal/tree" ) +const ( + maximumKDFFieldTextBytes = 8194 + // XML Encryption 1.1 declaration of a configured key-derivation step. Compare the expected XML + // Encryption namespace when matching it. + nameKeyDerivationMethod = "KeyDerivationMethod" +) + +const ( + attrAlgorithmID = "AlgorithmID" + attrPartyUInfo = "PartyUInfo" + attrPartyVInfo = "PartyVInfo" + attrSuppPrivInfo = "SuppPrivInfo" + attrSuppPubInfo = "SuppPubInfo" + nameConcatKDFParams = "ConcatKDFParams" +) + // These codecs implement XMLENC11 5.4.1's padding-count-prefixed hexBinary // bitstrings. AgreementMethod and DerivedKey use the same representation. func encodeConcatKDFParameters(ctx context.Context, parameters ConcatKDFParameters) (*tree.Node, error) { @@ -35,10 +51,10 @@ func encodeConcatKDFParameters(ctx context.Context, parameters ConcatKDFParamete return nil, ErrPolicy } - method := newElement(Namespace11, "xenc11", "KeyDerivationMethod") - setAttribute(method, "Algorithm", ConcatKDF) + method := newElement(Namespace11, prefixXEnc11, nameKeyDerivationMethod) + setAttribute(method, lexicon.SecurityAttrAlgorithm, ConcatKDF) - params := newElement(Namespace11, "xenc11", "ConcatKDFParams") + params := newElement(Namespace11, prefixXEnc11, nameConcatKDFParams) for _, field := range concatKDFFields(parameters) { if _, valid := concatFieldBits(field.value); !valid { @@ -50,8 +66,8 @@ func encodeConcatKDFParameters(ctx context.Context, parameters ConcatKDFParamete } } - digest := newElement(SignatureNamespace, "ds", lexicon.DSigElementDigestMethod) - setAttribute(digest, "Algorithm", algorithm) + digest := newElement(SignatureNamespace, lexicon.PrefixDSig, lexicon.DSigElementDigestMethod) + setAttribute(digest, lexicon.SecurityAttrAlgorithm, algorithm) appendChild(params, digest) appendChild(method, params) @@ -68,21 +84,21 @@ func decodeConcatKDFParameters(ctx context.Context, method document.Element) (Co return parameters, err } - if method.Name() != elementName(Namespace11, "KeyDerivationMethod") || !exactAttributes(method, "Algorithm") || attributeValue(method, "Algorithm") != ConcatKDF { + if method.Name() != elementName(Namespace11, nameKeyDerivationMethod) || !exactAttributes(method, lexicon.SecurityAttrAlgorithm) || attributeValue(method, lexicon.SecurityAttrAlgorithm) != ConcatKDF { return parameters, ErrFormat } - children, err := childElements(method, elementName(Namespace11, "ConcatKDFParams")) - if err != nil || !graphAttributes(children[0], "AlgorithmID", "PartyUInfo", "PartyVInfo", "SuppPubInfo", "SuppPrivInfo") { + children, err := childElements(method, elementName(Namespace11, nameConcatKDFParams)) + if err != nil || !graphAttributes(children[0], attrAlgorithmID, attrPartyUInfo, attrPartyVInfo, attrSuppPubInfo, attrSuppPrivInfo) { return parameters, ErrFormat } digests, err := childElements(children[0], elementName(SignatureNamespace, lexicon.DSigElementDigestMethod)) - if err != nil || !exactAttributes(digests[0], "Algorithm") || len(digests[0].Children()) != 0 || !xmlSpace(digests[0].Text()) { + if err != nil || !exactAttributes(digests[0], lexicon.SecurityAttrAlgorithm) || len(digests[0].Children()) != 0 || !xmlSpace(digests[0].Text()) { return parameters, ErrFormat } - switch attributeValue(digests[0], "Algorithm") { + switch attributeValue(digests[0], lexicon.SecurityAttrAlgorithm) { case SHA256: parameters.Hash = crypto.SHA256 case SHA1: @@ -97,8 +113,8 @@ func decodeConcatKDFParameters(ctx context.Context, method document.Element) (Co return ConcatKDFParameters{}, err } - value := strings.Trim(attributeValue(children[0], field.name), " \t\r\n") - if len(value) > 8194 { + value := strings.Trim(attributeValue(children[0], field.name), lexicon.XMLWhitespace) + if len(value) > maximumKDFFieldTextBytes { return ConcatKDFParameters{}, ErrPolicy } @@ -125,11 +141,11 @@ func concatKDFFields(parameters ConcatKDFParameters) []struct { name string value []byte }{ - {"AlgorithmID", parameters.AlgorithmID}, - {"PartyUInfo", parameters.PartyUInfo}, - {"PartyVInfo", parameters.PartyVInfo}, - {"SuppPubInfo", parameters.SuppPubInfo}, - {"SuppPrivInfo", parameters.SuppPrivInfo}, + {attrAlgorithmID, parameters.AlgorithmID}, + {attrPartyUInfo, parameters.PartyUInfo}, + {attrPartyVInfo, parameters.PartyVInfo}, + {attrSuppPubInfo, parameters.SuppPubInfo}, + {attrSuppPrivInfo, parameters.SuppPrivInfo}, } } diff --git a/xmlenc/graph_agreement.go b/xmlenc/graph_agreement.go index 850dcce..252845a 100644 --- a/xmlenc/graph_agreement.go +++ b/xmlenc/graph_agreement.go @@ -98,21 +98,21 @@ func (g *keyGraph) agreementEncryptedPlan(node graphNode, fields graphEncryptedF } func graphAgreementName(info document.Element) (string, error) { - if !graphAttributes(info, "Id") || !graphStructuralText(info) { + if !graphAttributes(info, lexicon.SecurityAttrID) || !graphStructuralText(info) { return "", ErrFormat } - methods, err := childElements(info, elementName(Namespace, "AgreementMethod")) + methods, err := childElements(info, elementName(Namespace, nameAgreementMethod)) if err != nil { return "", err } - if attributeValue(methods[0], "Algorithm") != ECDHES { + if attributeValue(methods[0], lexicon.SecurityAttrAlgorithm) != ECDHES { return "", ErrPolicy } for _, child := range methods[0].Children() { - if child.Name() != elementName(Namespace, "RecipientKeyInfo") { + if child.Name() != elementName(Namespace, nameRecipientKeyInfo) { continue } @@ -128,7 +128,7 @@ func graphAgreementName(info document.Element) (string, error) { func graphHasAgreement(info document.Element) bool { for _, child := range info.Children() { - if child.Name() == elementName(Namespace, "AgreementMethod") { + if child.Name() == elementName(Namespace, nameAgreementMethod) { return true } } @@ -141,7 +141,7 @@ func graphHasAgreement(info document.Element) bool { // supported schema attributes removed; caller nodes and namespaces are retained. func graphAgreementInfo(info document.Element) (document.Element, error) { owner, node := tree.Unwrap(info) - if !graphAttributes(info, "Id") || len(info.Children()) != 1 { + if !graphAttributes(info, lexicon.SecurityAttrID) || len(info.Children()) != 1 { return document.Element{}, ErrFormat } @@ -158,11 +158,11 @@ func graphAgreementInfo(info document.Element) (document.Element, error) { cloned.Children = append([]*tree.Node(nil), method.Children...) for j, child := range cloned.Children { - if child.Name != elementName(Namespace, "OriginatorKeyInfo") && child.Name != elementName(Namespace, "RecipientKeyInfo") { + if child.Name != elementName(Namespace, nameOriginatorKeyInfo) && child.Name != elementName(Namespace, nameRecipientKeyInfo) { continue } - if !graphAttributes(tree.Wrap(owner, child), "Id") { + if !graphAttributes(tree.Wrap(owner, child), lexicon.SecurityAttrID) { return document.Element{}, ErrFormat } diff --git a/xmlenc/graph_cipher.go b/xmlenc/graph_cipher.go index 29480b4..20e5b11 100644 --- a/xmlenc/graph_cipher.go +++ b/xmlenc/graph_cipher.go @@ -17,6 +17,10 @@ import ( "github.com/skarm/xmlsec/resource" ) +const ( + maximumCipherReferenceTransforms = 8 +) + type graphCipherPlan struct { document *graphDocument element document.Element @@ -73,7 +77,7 @@ func (g *keyGraph) referenceOptions(maximum int) (reference.Options, error) { } } - return reference.Options{AllowedTransforms: allowed, MaxTransforms: 8, MaxInputBytes: g.remaining.MaxBytes, MaxOutputBytes: min(maximum, g.remaining.MaxBytes), MaxProducedBytes: g.remaining.MaxBytes, MaxNodeVisits: g.remaining.MaxNodes, XMLLimits: g.profile.options.XMLLimits}, nil + return reference.Options{AllowedTransforms: allowed, MaxTransforms: maximumCipherReferenceTransforms, MaxInputBytes: g.remaining.MaxBytes, MaxOutputBytes: min(maximum, g.remaining.MaxBytes), MaxProducedBytes: g.remaining.MaxBytes, MaxNodeVisits: g.remaining.MaxNodes, XMLLimits: g.profile.options.XMLLimits}, nil } func (g *keyGraph) cipherPlan(doc *graphDocument, data document.Element, maximum int) (*graphCipherPlan, error) { @@ -84,7 +88,7 @@ func (g *keyGraph) cipherPlan(doc *graphDocument, data document.Element, maximum p := &graphCipherPlan{document: doc, element: data, maximum: maximum} child := data.Children()[0] - if child.Name() == elementName(Namespace, "CipherValue") { + if child.Name() == elementName(Namespace, nameCipherValue) { if !exactAttributes(child) || len(child.Children()) != 0 { return nil, ErrFormat } @@ -94,11 +98,11 @@ func (g *keyGraph) cipherPlan(doc *graphDocument, data document.Element, maximum return p, nil } - if child.Name() != elementName(Namespace, "CipherReference") || !exactAttributes(child, "URI") || !graphStructuralText(child) { + if child.Name() != elementName(Namespace, nameCipherReference) || !exactAttributes(child, lexicon.SecurityAttrURI) || !graphStructuralText(child) { return nil, ErrFormat } - p.uri = attributeValue(child, "URI") + p.uri = attributeValue(child, lexicon.SecurityAttrURI) if !validGraphReference(p.uri) { return nil, ErrFormat } @@ -275,21 +279,21 @@ func graphCipherTransforms(child document.Element) ([]reference.Transform, error } func graphCipherTransform(item document.Element) (reference.Transform, error) { - if item.Name() != elementName(SignatureNamespace, lexicon.DSigElementTransform) || !exactAttributes(item, "Algorithm") || !graphStructuralText(item) { + if item.Name() != elementName(SignatureNamespace, lexicon.DSigElementTransform) || !exactAttributes(item, lexicon.SecurityAttrAlgorithm) || !graphStructuralText(item) { return reference.Transform{}, ErrFormat } - t := reference.Transform{Algorithm: attributeValue(item, "Algorithm")} + t := reference.Transform{Algorithm: attributeValue(item, lexicon.SecurityAttrAlgorithm)} if len(item.Children()) == 0 { return t, nil } - children, err := childElements(item, elementName(c14n.Exclusive, "InclusiveNamespaces")) - if err != nil || !exactAttributes(children[0], "PrefixList") || len(children[0].Children()) != 0 || !xmlSpace(children[0].Text()) { + children, err := childElements(item, elementName(c14n.Exclusive, lexicon.C14NElementInclusiveNamespaces)) + if err != nil || !exactAttributes(children[0], lexicon.C14NAttrPrefixList) || len(children[0].Children()) != 0 || !xmlSpace(children[0].Text()) { return reference.Transform{}, ErrFormat } - t.InclusivePrefixes = strings.Fields(attributeValue(children[0], "PrefixList")) + t.InclusivePrefixes = strings.Fields(attributeValue(children[0], lexicon.C14NAttrPrefixList)) return t, nil } diff --git a/xmlenc/graph_decrypt.go b/xmlenc/graph_decrypt.go index ff38cc4..4dc5cea 100644 --- a/xmlenc/graph_decrypt.go +++ b/xmlenc/graph_decrypt.go @@ -7,6 +7,7 @@ import ( "io" "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/tree" ) @@ -114,7 +115,7 @@ func graphPlainError(ctx context.Context) error { func decryptGraph(ctx context.Context, doc *document.Document, selected document.Element, options KeyGraphOptions, typ string) ([]byte, profile, error) { node, err := selectedNode(doc, selected) - if err != nil || node.Name != elementName(Namespace, "EncryptedData") { + if err != nil || node.Name != elementName(Namespace, nameEncryptedData) { return nil, profile{}, ErrFormat } @@ -133,12 +134,12 @@ func decryptGraph(ctx context.Context, doc *document.Document, selected document p := g.profile - if typ != "" && attributeValue(selected, "Type") != typ { + if typ != "" && attributeValue(selected, lexicon.SecurityAttrType) != typ { return nil, profile{}, ErrFormat } if fields.method.Valid() { - if err := checkEncryptionMethod(ctx, fields.method, p.options.DataAlgorithm, p.keyBytes*8, false); err != nil { + if err := checkEncryptionMethod(ctx, fields.method, p.options.DataAlgorithm, p.keyBytes*keyOctetBits, false); err != nil { return nil, profile{}, err } } diff --git a/xmlenc/graph_generate.go b/xmlenc/graph_generate.go index 038dff6..a4b099d 100644 --- a/xmlenc/graph_generate.go +++ b/xmlenc/graph_generate.go @@ -12,12 +12,27 @@ import ( "crypto/des" // #nosec G502 -- Explicit legacy XMLENC key-wrap policy. - "github.com/skarm/xmlsec/internal/lexicon" - "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/tree" ) +const ( + maximumGeneratedGraphBytes = 128 << 10 + maximumGeneratedReferences = 64 + maximumGraphMetadataBytes = 1024 + maximumIDBytes = 1024 + maximumRecipientNameBytes = 1024 + // Container for encryption metadata preserved with the protected value. Compare the expected XML + // Encryption namespace when matching it. + nameEncryptionProperties = "EncryptionProperties" + // Logical reference to an explicitly configured master key. Compare the expected XML Encryption + // namespace when matching it. + nameMasterKeyName = "MasterKeyName" + // Suggested prefix for generated XML Encryption 1.1 elements; identity comes from Namespace11. + prefixXEnc11 = "xenc11" +) + // KeyGraphReference identifies data or an encrypted or derived key that uses a // generated key. Key selects KeyReference; false selects DataReference. The URI // grants no resource retrieval permission. @@ -53,6 +68,24 @@ type EncryptedKeyOptions struct { Properties *document.Document } +// DerivedKeyOptions supplies ConcatKDF metadata and key relationships. MasterKeyName +// identifies the master by an exact application label. Building this metadata does +// not resolve, derive, store or publish secret material. +type DerivedKeyOptions struct { + // Parameters supplies the explicit ConcatKDF context to encode. + Parameters ConcatKDFParameters + // ID supplies an optional XML Id for the DerivedKey element. + ID string + // Recipient supplies an optional recipient label without establishing identity. + Recipient string + // DerivedKeyName is the label assigned to the derived key. + DerivedKeyName string + // MasterKeyName identifies the application-selected master key by its exact label. + MasterKeyName string + // References lists the data and key objects that use the derived key. + References []KeyGraphReference +} + // EncryptKey wraps rawKey into standalone EncryptedKey. The recipient supplies // exactly one RSA PublicKey or symmetric Secret; Decrypter is never accepted. // Raw keys are 16/24/32 bytes. TripleDES wrap accepts only a 24-byte TripleDES @@ -66,17 +99,17 @@ func EncryptKey(ctx context.Context, rawKey []byte, recipient KeyGraphKey, optio return nil, err } - if !sessionKeySize(len(rawKey)) || recipient.Algorithm == "" || options.Algorithm != recipient.Algorithm || recipient.Decrypter != nil || len(recipient.Name) > 1024 || !utf8.ValidString(recipient.Name) { + if !sessionKeySize(len(rawKey)) || recipient.Algorithm == "" || options.Algorithm != recipient.Algorithm || recipient.Decrypter != nil || len(recipient.Name) > maximumRecipientNameBytes || !utf8.ValidString(recipient.Name) { return nil, ErrPolicy } - root := encryptedElement("EncryptedKey") + root := encryptedElement(nameEncryptedKey) if err := graphGeneratedMetadata(root, options.ID, options.Recipient); err != nil { return nil, err } - method := encryptedElement("EncryptionMethod") - setAttribute(method, "Algorithm", options.Algorithm) + method := encryptedElement(nameEncryptionMethod) + setAttribute(method, lexicon.SecurityAttrAlgorithm, options.Algorithm) appendChild(root, method) wrapped, info, err := graphGenerateKey(ctx, recipient, rawKey, options, method) @@ -92,8 +125,8 @@ func EncryptKey(ctx context.Context, rawKey []byte, recipient KeyGraphKey, optio if info != nil { appendChild(root, info) } else if recipient.Name != "" { - info := newElement(SignatureNamespace, "ds", lexicon.DSigElementKeyInfo) - name := newElement(SignatureNamespace, "ds", lexicon.DSigElementKeyName) + info := newElement(SignatureNamespace, lexicon.PrefixDSig, lexicon.DSigElementKeyInfo) + name := newElement(SignatureNamespace, lexicon.PrefixDSig, lexicon.DSigElementKeyName) appendChild(name, &tree.Node{Kind: tree.TextNode, Data: recipient.Name}) appendChild(info, name) appendChild(root, info) @@ -107,7 +140,7 @@ func EncryptKey(ctx context.Context, rawKey []byte, recipient KeyGraphKey, optio appendChild(root, data) if options.Properties != nil { - if options.Properties.Root().Name() != elementName(Namespace, "EncryptionProperties") || checkGraphProperties(options.Properties.Root()) != nil { + if options.Properties.Root().Name() != elementName(Namespace, nameEncryptionProperties) || checkGraphProperties(options.Properties.Root()) != nil { return nil, ErrFormat } @@ -118,12 +151,12 @@ func EncryptKey(ctx context.Context, rawKey []byte, recipient KeyGraphKey, optio return nil, err } - if len(options.CarriedKeyName) > 1024 || !utf8.ValidString(options.CarriedKeyName) { + if len(options.CarriedKeyName) > maximumKeyNameBytes || !utf8.ValidString(options.CarriedKeyName) { return nil, ErrPolicy } if options.CarriedKeyName != "" { - name := encryptedElement("CarriedKeyName") + name := encryptedElement(nameCarriedKeyName) appendChild(name, &tree.Node{Kind: tree.TextNode, Data: options.CarriedKeyName}) appendChild(root, name) } @@ -131,6 +164,50 @@ func EncryptKey(ctx context.Context, rawKey []byte, recipient KeyGraphKey, optio return graphGeneratedDocument(ctx, root) } +// BuildDerivedKey produces a standalone XMLENC1.1 DerivedKey with ConcatKDF. +func BuildDerivedKey(ctx context.Context, options DerivedKeyOptions) (*document.Document, error) { + if ctx == nil { + return nil, ErrPolicy + } + + if err := ctx.Err(); err != nil { + return nil, err + } + + root := newElement(Namespace11, prefixXEnc11, nameDerivedKey) + if err := graphGeneratedMetadata(root, options.ID, options.Recipient); err != nil { + return nil, err + } + + method, err := encodeConcatKDFParameters(ctx, options.Parameters) + if err != nil { + return nil, err + } + + appendChild(root, method) + + if err := graphAppendReferences(root, options.References); err != nil { + return nil, err + } + + for _, item := range []struct { + name string + value string + }{{nameDerivedKeyName, options.DerivedKeyName}, {nameMasterKeyName, options.MasterKeyName}} { + if len(item.value) > maximumGraphMetadataBytes || !utf8.ValidString(item.value) { + return nil, ErrPolicy + } + + if item.value != "" { + name := newElement(Namespace11, prefixXEnc11, item.name) + appendChild(name, &tree.Node{Kind: tree.TextNode, Data: item.value}) + appendChild(root, name) + } + } + + return graphGeneratedDocument(ctx, root) +} + func graphGenerateKey(ctx context.Context, recipient KeyGraphKey, raw []byte, options EncryptedKeyOptions, method *tree.Node) ([]byte, *tree.Node, error) { if recipient.ECDHPublicKey == nil { if recipient.ECDHRecipient != nil || recipient.ECDH != nil { @@ -188,7 +265,7 @@ func graphWrapMaterial(recipient KeyGraphKey, rawKey []byte, options EncryptedKe encodeOAEP(method, parameters) wrapped, err = rsa.EncryptOAEPWithOptions(rand.Reader, recipient.PublicKey, rawKey, &rsa.OAEPOptions{Hash: parameters.Hash, MGFHash: parameters.MGFHash, Label: parameters.Label}) case options.Algorithm == TripleDESKeyWrap: - if recipient.PublicKey != nil || options.OAEP != nil || !options.AllowLegacy || fips140.Enabled() || len(recipient.Secret) != 24 || len(rawKey) != 24 { + if recipient.PublicKey != nil || options.OAEP != nil || !options.AllowLegacy || fips140.Enabled() || len(recipient.Secret) != tripleDESKeyBytes || len(rawKey) != tripleDESKeyBytes { return nil, ErrPolicy } @@ -214,86 +291,24 @@ func graphWrapMaterial(recipient KeyGraphKey, rawKey []byte, options EncryptedKe return wrapped, err } -// DerivedKeyOptions supplies ConcatKDF metadata and key relationships. MasterKeyName -// identifies the master by an exact application label. Building this metadata does -// not resolve, derive, store or publish secret material. -type DerivedKeyOptions struct { - // Parameters supplies the explicit ConcatKDF context to encode. - Parameters ConcatKDFParameters - // ID supplies an optional XML Id for the DerivedKey element. - ID string - // Recipient supplies an optional recipient label without establishing identity. - Recipient string - // DerivedKeyName is the label assigned to the derived key. - DerivedKeyName string - // MasterKeyName identifies the application-selected master key by its exact label. - MasterKeyName string - // References lists the data and key objects that use the derived key. - References []KeyGraphReference -} - -// BuildDerivedKey produces a standalone XMLENC1.1 DerivedKey with ConcatKDF. -func BuildDerivedKey(ctx context.Context, options DerivedKeyOptions) (*document.Document, error) { - if ctx == nil { - return nil, ErrPolicy - } - - if err := ctx.Err(); err != nil { - return nil, err - } - - root := newElement(Namespace11, "xenc11", "DerivedKey") - if err := graphGeneratedMetadata(root, options.ID, options.Recipient); err != nil { - return nil, err - } - - method, err := encodeConcatKDFParameters(ctx, options.Parameters) - if err != nil { - return nil, err - } - - appendChild(root, method) - - if err := graphAppendReferences(root, options.References); err != nil { - return nil, err - } - - for _, item := range []struct { - name string - value string - }{{"DerivedKeyName", options.DerivedKeyName}, {"MasterKeyName", options.MasterKeyName}} { - if len(item.value) > 1024 || !utf8.ValidString(item.value) { - return nil, ErrPolicy - } - - if item.value != "" { - name := newElement(Namespace11, "xenc11", item.name) - appendChild(name, &tree.Node{Kind: tree.TextNode, Data: item.value}) - appendChild(root, name) - } - } - - return graphGeneratedDocument(ctx, root) -} - func graphGeneratedMetadata(root *tree.Node, id, recipient string) error { - if len(id) > 1024 || id != "" && !tree.NCName(id) || len(recipient) > 1024 || !utf8.ValidString(recipient) { + if len(id) > maximumIDBytes || id != "" && !tree.NCName(id) || len(recipient) > maximumRecipientNameBytes || !utf8.ValidString(recipient) { return ErrPolicy } if id != "" { - setAttribute(root, "Id", id) + setAttribute(root, lexicon.SecurityAttrID, id) } if recipient != "" { - setAttribute(root, "Recipient", recipient) + setAttribute(root, attrRecipient, recipient) } return nil } func graphAppendReferences(root *tree.Node, references []KeyGraphReference) error { - if len(references) > 64 { + if len(references) > maximumGeneratedReferences { return ErrPolicy } @@ -301,20 +316,20 @@ func graphAppendReferences(root *tree.Node, references []KeyGraphReference) erro return nil } - list := encryptedElement("ReferenceList") + list := encryptedElement(nameReferenceList) for _, ref := range references { if !validGraphReference(ref.URI) { return ErrPolicy } - name := "DataReference" + name := nameDataReference if ref.Key { - name = "KeyReference" + name = nameKeyReference } child := encryptedElement(name) - setAttribute(child, "URI", ref.URI) + setAttribute(child, lexicon.SecurityAttrURI, ref.URI) appendChild(list, child) } @@ -328,9 +343,9 @@ func graphGeneratedDocument(ctx context.Context, root *tree.Node) (*document.Doc return nil, ErrPolicy } - root.NS = []tree.Namespace{{Prefix: "xenc", URI: Namespace}, {Prefix: "xenc11", URI: Namespace11}, {Prefix: "ds", URI: SignatureNamespace}} + root.NS = []tree.Namespace{{Prefix: prefixXEnc, URI: Namespace}, {Prefix: prefixXEnc11, URI: Namespace11}, {Prefix: lexicon.PrefixDSig, URI: SignatureNamespace}} limits := document.DefaultLimits() - limits.MaxBytes = 128 << 10 + limits.MaxBytes = maximumGeneratedGraphBytes return tree.ParseProduced(ctx, func(dst io.Writer) error { _, err := tree.WriteXMLFragment(ctx, dst, []*tree.Node{root}, limits.MaxBytes) diff --git a/xmlenc/graph_keys.go b/xmlenc/graph_keys.go index 35edb5b..66e04f4 100644 --- a/xmlenc/graph_keys.go +++ b/xmlenc/graph_keys.go @@ -5,6 +5,7 @@ import ( "context" "crypto" "crypto/aes" + "crypto/des" // #nosec G502 -- Explicit legacy XMLENC key-wrap policy only. "crypto/fips140" "crypto/rand" "crypto/rsa" @@ -12,15 +13,40 @@ import ( "errors" "strings" - "crypto/des" // #nosec G502 -- Explicit legacy XMLENC key-wrap policy only. - - "github.com/skarm/xmlsec/internal/lexicon" - "github.com/skarm/xmlsec/document" - + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/tree" ) +const ( + // Unqualified recipient label resolved through caller-supplied graph bindings. + attrRecipient = "Recipient" + // Logical name carried with an encrypted key for graph resolution. Compare the expected XML + // Encryption namespace when matching it. + nameCarriedKeyName = "CarriedKeyName" + // Reference-list entry targeting encrypted application data. Compare the expected XML Encryption + // namespace when matching it. + nameDataReference = "DataReference" + // XML Encryption 1.1 derived-key node in the explicit key graph. Compare the expected XML + // Encryption namespace when matching it. + nameDerivedKey = "DerivedKey" + // Logical name associated with a derived key. Compare the expected XML Encryption namespace when + // matching it. + nameDerivedKeyName = "DerivedKeyName" + // Wrapped-key node resolved under the configured key-graph policy. Compare the expected XML + // Encryption namespace when matching it. + nameEncryptedKey = "EncryptedKey" + // One metadata record within EncryptionProperties. Compare the expected XML Encryption namespace + // when matching it. + nameEncryptionProperty = "EncryptionProperty" + // Reference-list entry linking to another encrypted key. Compare the expected XML Encryption + // namespace when matching it. + nameKeyReference = "KeyReference" + // Targets associated with one EncryptedKey. Compare the expected XML Encryption namespace when + // matching it. + nameReferenceList = "ReferenceList" +) + type graphPlanID struct { document *graphDocument node *tree.Node @@ -54,19 +80,30 @@ type graphEncryptedFields struct { } func graphEncrypted(node graphNode) (graphEncryptedFields, error) { + // These positions match values below. EncryptedData ends at optional + // EncryptionProperties; only EncryptedKey may carry the final two fields. + const ( + encryptionMethodPosition = 0 + keyInfoPosition = 1 + cipherDataPosition = 2 + encryptionPropertiesPosition = 3 + referenceListPosition = 4 + carriedKeyNamePosition = 5 + ) + var fields graphEncryptedFields element := node.element() - key := element.Name() == elementName(Namespace, "EncryptedKey") - if !key && element.Name() != elementName(Namespace, "EncryptedData") || !graphAttributes(element, "Id", "Type", "MimeType", "Encoding", "Recipient") || !key && graphHasAttribute(element, "Recipient") { + key := element.Name() == elementName(Namespace, nameEncryptedKey) + if !key && element.Name() != elementName(Namespace, nameEncryptedData) || !graphAttributes(element, lexicon.SecurityAttrID, lexicon.SecurityAttrType, lexicon.SecurityAttrMimeType, lexicon.SecurityAttrEncoding, attrRecipient) || !key && graphHasAttribute(element, attrRecipient) { return fields, ErrFormat } positions := map[document.Name]int{ - elementName(Namespace, "EncryptionMethod"): 0, elementName(SignatureNamespace, lexicon.DSigElementKeyInfo): 1, - elementName(Namespace, "CipherData"): 2, elementName(Namespace, "EncryptionProperties"): 3, - elementName(Namespace, "ReferenceList"): 4, elementName(Namespace, "CarriedKeyName"): 5, + elementName(Namespace, nameEncryptionMethod): encryptionMethodPosition, elementName(SignatureNamespace, lexicon.DSigElementKeyInfo): keyInfoPosition, + elementName(Namespace, nameCipherData): cipherDataPosition, elementName(Namespace, nameEncryptionProperties): encryptionPropertiesPosition, + elementName(Namespace, nameReferenceList): referenceListPosition, elementName(Namespace, nameCarriedKeyName): carriedKeyNamePosition, } values := []*document.Element{&fields.method, &fields.keyInfo, &fields.cipherData, &fields.properties, &fields.references, &fields.carried} previous := -1 @@ -79,7 +116,7 @@ func graphEncrypted(node graphNode) (graphEncryptedFields, error) { for _, child := range element.Children() { position, known := positions[child.Name()] - if !known || position <= previous || !key && position > 3 { + if !known || position <= previous || !key && position > encryptionPropertiesPosition { return fields, ErrFormat } @@ -113,7 +150,7 @@ func graphEncrypted(node graphNode) (graphEncryptedFields, error) { } func checkGraphProperties(properties document.Element) error { - if !graphAttributes(properties, "Id") || len(properties.Children()) == 0 { + if !graphAttributes(properties, lexicon.SecurityAttrID) || len(properties.Children()) == 0 { return ErrFormat } @@ -125,13 +162,13 @@ func checkGraphProperties(properties document.Element) error { } for _, property := range properties.Children() { - if property.Name() != elementName(Namespace, "EncryptionProperty") || len(property.Children()) == 0 { + if property.Name() != elementName(Namespace, nameEncryptionProperty) || len(property.Children()) == 0 { return ErrFormat } _, node := tree.Unwrap(property) for _, attribute := range node.Attrs { - if attribute.Name.Space != document.XMLNamespace && attribute.Name != (document.Name{Local: "Id"}) && attribute.Name != (document.Name{Local: "Target"}) { + if attribute.Name.Space != document.XMLNamespace && attribute.Name != (document.Name{Local: lexicon.SecurityAttrID}) && attribute.Name != (document.Name{Local: lexicon.SecurityAttrTarget}) { return ErrFormat } } @@ -160,7 +197,7 @@ func graphReferences(list document.Element) ([]document.Element, error) { children := list.Children() for _, child := range children { - if child.Name() != elementName(Namespace, "DataReference") && child.Name() != elementName(Namespace, "KeyReference") || !exactAttributes(child, "URI") || !validGraphReference(attributeValue(child, "URI")) { + if child.Name() != elementName(Namespace, nameDataReference) && child.Name() != elementName(Namespace, nameKeyReference) || !exactAttributes(child, lexicon.SecurityAttrURI) || !validGraphReference(attributeValue(child, lexicon.SecurityAttrURI)) { return nil, ErrFormat } @@ -189,9 +226,9 @@ func (g *keyGraph) namedMaterial(name, algorithm string, size int) (*graphKeyPla } func (g *keyGraph) keyName(node graphNode) (string, bool, error) { - name := elementName(Namespace, "CarriedKeyName") - if node.node.Name == elementName(Namespace11, "DerivedKey") { - name = elementName(Namespace11, "DerivedKeyName") + name := elementName(Namespace, nameCarriedKeyName) + if node.node.Name == elementName(Namespace11, nameDerivedKey) { + name = elementName(Namespace11, nameDerivedKeyName) } for _, child := range node.element().Children() { @@ -239,6 +276,8 @@ func (g *keyGraph) consumerKey(node graphNode, info document.Element, algorithm func (g *keyGraph) keyPlan(node graphNode, consumer string, size, depth int) (*graphKeyPlan, error) { identity := graphPlanID{node.document, node.node, consumer, size} + // A plan still being built is a cycle. Completed plans may be shared, so + // check the active path separately from the reusable plan cache below. if _, exists := g.active[identity]; exists { return nil, ErrFormat } @@ -262,9 +301,9 @@ func (g *keyGraph) keyPlan(node graphNode, consumer string, size, depth int) (*g plan *graphKeyPlan err error ) - if node.node.Name == elementName(Namespace11, "DerivedKey") { + if node.node.Name == elementName(Namespace11, nameDerivedKey) { plan, err = g.derivedPlan(node, size, depth) - } else if node.node.Name == elementName(Namespace, "EncryptedKey") { + } else if node.node.Name == elementName(Namespace, nameEncryptedKey) { plan, err = g.encryptedPlan(node, consumer, size, depth) } else { return nil, ErrFormat @@ -342,7 +381,7 @@ func (g *keyGraph) wrappedEncryptedPlan(node graphNode, fields graphEncryptedFie } if fields.method.Valid() { - if err := checkEncryptionMethod(g.ctx, fields.method, algorithm, kekBytes*8, false); err != nil { + if err := checkEncryptionMethod(g.ctx, fields.method, algorithm, kekBytes*keyOctetBits, false); err != nil { return nil, err } } @@ -352,9 +391,9 @@ func (g *keyGraph) wrappedEncryptedPlan(node graphNode, fields graphEncryptedFie return nil, err } - maximum := size + 8 + maximum := size + keyWrapBlockBytes if algorithm == TripleDESKeyWrap { - maximum = 40 + maximum = tripleDESWrappedKeyBytes } cipher, err := g.cipherPlan(node.document, fields.cipherData, maximum) @@ -367,7 +406,7 @@ func (g *keyGraph) wrappedEncryptedPlan(node graphNode, fields graphEncryptedFie func (g *keyGraph) encryptionAlgorithm(fields graphEncryptedFields) (string, error) { if fields.method.Valid() { - algorithm := attributeValue(fields.method, "Algorithm") + algorithm := attributeValue(fields.method, lexicon.SecurityAttrAlgorithm) if _, exists := g.methods[algorithm]; !exists || algorithm == ConcatKDF { return "", ErrPolicy } @@ -442,7 +481,7 @@ func (g *keyGraph) rsaMaterial(info document.Element, algorithm string) (KeyGrap names := []string{""} if info.Valid() { - if !graphAttributes(info, "Id") || len(info.Children()) == 0 || !graphStructuralText(info) { + if !graphAttributes(info, lexicon.SecurityAttrID) || len(info.Children()) == 0 || !graphStructuralText(info) { return KeyGraphKey{}, ErrFormat } @@ -493,13 +532,22 @@ func (g *keyGraph) rsaMaterial(info document.Element, algorithm string) (KeyGrap } func (g *keyGraph) derivedPlan(node graphNode, size, depth int) (*graphKeyPlan, error) { - if _, exists := g.methods[ConcatKDF]; g.options.ConcatKDF == nil || !exists || !sessionKeySize(size) || !graphAttributes(node.element(), "Id", "Type", "Recipient") { + // DerivedKey grammar positions select derivation parameters and + // distinguish the derived-key label from the master-key lookup label. + const ( + derivationMethodPosition = 0 + derivedReferenceListPosition = 1 + derivedKeyNamePosition = 2 + masterKeyNamePosition = 3 + ) + + if _, exists := g.methods[ConcatKDF]; g.options.ConcatKDF == nil || !exists || !sessionKeySize(size) || !graphAttributes(node.element(), lexicon.SecurityAttrID, lexicon.SecurityAttrType, attrRecipient) { return nil, ErrPolicy } method, master := document.Element{}, "" position := -1 - positions := map[document.Name]int{elementName(Namespace11, "KeyDerivationMethod"): 0, elementName(Namespace, "ReferenceList"): 1, elementName(Namespace11, "DerivedKeyName"): 2, elementName(Namespace11, "MasterKeyName"): 3} + positions := map[document.Name]int{elementName(Namespace11, nameKeyDerivationMethod): derivationMethodPosition, elementName(Namespace, nameReferenceList): derivedReferenceListPosition, elementName(Namespace11, nameDerivedKeyName): derivedKeyNamePosition, elementName(Namespace11, nameMasterKeyName): masterKeyNamePosition} for _, raw := range node.node.Children { if raw.Kind == tree.TextNode && !xmlSpace(raw.Data) { @@ -515,19 +563,19 @@ func (g *keyGraph) derivedPlan(node graphNode, size, depth int) (*graphKeyPlan, position = index switch index { - case 0: + case derivationMethodPosition: method = child - case 1: + case derivedReferenceListPosition: if _, err := graphReferences(child); err != nil { return nil, err } - case 2, 3: + case derivedKeyNamePosition, masterKeyNamePosition: name, err := graphNameText(child) if err != nil { return nil, err } - if index == 3 { + if index == masterKeyNamePosition { master = name } } @@ -543,8 +591,8 @@ func (g *keyGraph) derivedPlan(node graphNode, size, depth int) (*graphKeyPlan, key, err := g.namedMaterial(master, ConcatKDF, 0) if errors.Is(err, errGraphKeyUnavailable) { - info := newElement(SignatureNamespace, "ds", lexicon.DSigElementKeyInfo) - name := newElement(SignatureNamespace, "ds", lexicon.DSigElementKeyName) + info := newElement(SignatureNamespace, lexicon.PrefixDSig, lexicon.DSigElementKeyInfo) + name := newElement(SignatureNamespace, lexicon.PrefixDSig, lexicon.DSigElementKeyName) appendChild(name, &tree.Node{Kind: tree.TextNode, Data: master}) appendChild(info, name) key, err = g.consumerKey(node, tree.Wrap(node.document.doc, info), ConcatKDF, g.options.MasterKeyBytes, depth+1) @@ -623,7 +671,7 @@ func (g *keyGraph) runKey(plan *graphKeyPlan) ([]byte, error) { case plan.algorithm == "": value = input case plan.algorithm == ConcatKDF: - value, err = DeriveConcatKey(g.ctx, input, plan.keyBytes*8, *plan.kdf) + value, err = DeriveConcatKey(g.ctx, input, plan.keyBytes*keyOctetBits, *plan.kdf) case rsaOAEPAlgorithm(plan.algorithm): parameters := &rsa.OAEPOptions{Hash: plan.oaep.Hash, MGFHash: plan.oaep.MGFHash, Label: bytes.Clone(plan.oaep.Label)} if provider, ok := plan.provider.(contextualDecrypter); ok { @@ -672,11 +720,11 @@ func (g *keyGraph) runKey(plan *graphKeyPlan) ([]byte, error) { } func (g *keyGraph) retrievedKey(doc *graphDocument, method document.Element) (graphNode, error) { - if !graphAttributes(method, "URI", "Type") || len(method.Children()) != 0 || !xmlSpace(method.Text()) { + if !graphAttributes(method, lexicon.SecurityAttrURI, lexicon.SecurityAttrType) || len(method.Children()) != 0 || !xmlSpace(method.Text()) { return graphNode{}, ErrFormat } - uri, exists := method.Attribute(document.Name{Local: "URI"}) + uri, exists := method.Attribute(document.Name{Local: lexicon.SecurityAttrURI}) if !exists { uri = "" } @@ -687,7 +735,7 @@ func (g *keyGraph) retrievedKey(doc *graphDocument, method document.Element) (gr } typ := graphRetrievalType(target.node.Name) - if typ == "" || attributeValue(method, "Type") != "" && attributeValue(method, "Type") != typ { + if typ == "" || attributeValue(method, lexicon.SecurityAttrType) != "" && attributeValue(method, lexicon.SecurityAttrType) != typ { return graphNode{}, ErrFormat } @@ -698,7 +746,7 @@ func (g *keyGraph) referenceListMatches(key, target graphNode) (bool, error) { var list document.Element for _, child := range key.element().Children() { - if child.Name() == elementName(Namespace, "ReferenceList") { + if child.Name() == elementName(Namespace, nameReferenceList) { if list.Valid() { return false, ErrFormat } @@ -716,9 +764,9 @@ func (g *keyGraph) referenceListMatches(key, target graphNode) (bool, error) { return false, err } - wanted := elementName(Namespace, "DataReference") + wanted := elementName(Namespace, nameDataReference) if graphRetrievalType(target.node.Name) != "" { - wanted = elementName(Namespace, "KeyReference") + wanted = elementName(Namespace, nameKeyReference) } for _, ref := range refs { @@ -730,7 +778,7 @@ func (g *keyGraph) referenceListMatches(key, target graphNode) (bool, error) { continue } - matched, err := g.referenceMatches(key.document, ref, target, attributeValue(ref, "URI")) + matched, err := g.referenceMatches(key.document, ref, target, attributeValue(ref, lexicon.SecurityAttrURI)) if err != nil { return false, err } @@ -805,7 +853,7 @@ func (b *graphSources) keyInfo(info document.Element) error { return b.add(g.namedMaterial("", b.algorithm, b.size)) } - if !graphAttributes(info, "Id") || len(info.Children()) == 0 || !graphStructuralText(info) { + if !graphAttributes(info, lexicon.SecurityAttrID) || len(info.Children()) == 0 || !graphStructuralText(info) { return ErrFormat } @@ -820,12 +868,12 @@ func (b *graphSources) keyInfo(info document.Element) error { if err := b.named(name); err != nil { return err } - case elementName(Namespace, "EncryptedKey"), elementName(Namespace11, "DerivedKey"): + case elementName(Namespace, nameEncryptedKey), elementName(Namespace11, nameDerivedKey): _, raw := tree.Unwrap(child) if err := b.add(g.keyPlan(graphNode{b.target.document, raw}, b.algorithm, b.size, b.depth+1)); err != nil { return err } - case elementName(SignatureNamespace, "RetrievalMethod"): + case elementName(SignatureNamespace, lexicon.DSigElementRetrievalMethod): candidate, err := g.retrievedKey(b.target.document, child) if err != nil { return err @@ -901,12 +949,12 @@ func (b *graphSources) references() error { } func graphRetrievalType(name document.Name) string { - if name == elementName(Namespace, "EncryptedKey") { - return Namespace + "EncryptedKey" + if name == elementName(Namespace, nameEncryptedKey) { + return Namespace + nameEncryptedKey } - if name == elementName(Namespace11, "DerivedKey") { - return Namespace11 + "DerivedKey" + if name == elementName(Namespace11, nameDerivedKey) { + return Namespace11 + nameDerivedKey } return "" diff --git a/xmlenc/graph_state.go b/xmlenc/graph_state.go index ade0351..fd85d9d 100644 --- a/xmlenc/graph_state.go +++ b/xmlenc/graph_state.go @@ -22,6 +22,27 @@ import ( "github.com/skarm/xmlsec/resource" ) +const ( + defaultGraphBytes = 32 << 20 + defaultGraphDepth = 8 + defaultGraphNodes = 8192 + defaultGraphResources = 8 + defaultMasterKeyBytes = 32 + // Bounds cipher-resource URI spellings before validation and explicit resolver authorization. + maximumCipherURIBytes = 4096 + maximumGraphAllowedURIs = 64 + maximumGraphBindings = 64 + maximumGraphBytes = 128 << 20 + maximumGraphDepth = 32 + maximumGraphIDRules = 28 + maximumGraphKeyAlgorithms = 32 + maximumGraphKeyURIs = 64 + maximumGraphNodes = 100000 + maximumGraphResources = 64 + maximumGraphSecretBytes = 1024 + maximumGraphTextBytes = 1024 +) + var errGraphKeyUnavailable = errors.New("no configured key for this graph path") // KeyGraphKey binds application-owned key material to an exact name and algorithm. @@ -144,30 +165,48 @@ type graphDocument struct { func (n graphNode) element() document.Element { return tree.Wrap(n.document.doc, n.node) } type keyGraph struct { - ctx context.Context - options KeyGraphOptions - profile profile - limits KeyGraphLimits - bindings map[graphBinding]KeyGraphKey - methods map[string]struct{} - allowed map[string]struct{} - documents map[string]*graphDocument - ordered []*graphDocument + // ctx supplies cancellation for graph construction and key resolution. + ctx context.Context + // options retains the snapshotted application key and resource policy. + options KeyGraphOptions + // profile retains normalized data-encryption algorithm policy. + profile profile + // limits retains normalized graph resource bounds. + limits KeyGraphLimits + // bindings indexes explicitly trusted keys by name and algorithm. + bindings map[graphBinding]KeyGraphKey + // methods contains permitted key-management algorithm URIs. + methods map[string]struct{} + // allowed contains exact external key resource URIs authorized by policy. + allowed map[string]struct{} + // documents caches immutable key documents by resource URI. + documents map[string]*graphDocument + // ordered preserves document discovery order. + ordered []*graphDocument + // standalone retains separately supplied encrypted-key nodes. standalone []graphNode - active map[graphPlanID]struct{} - plans map[graphPlanID]*graphKeyPlan - baseURI *url.URL - remaining KeyGraphLimits + // active tracks the current resolution stack to reject key-reference cycles. + active map[graphPlanID]struct{} + // plans caches completed key-resolution plans by their full identity. + plans map[graphPlanID]*graphKeyPlan + // baseURI optionally enables trusted relative resource URI resolution. + baseURI *url.URL + // remaining shares cumulative work budgets across the complete graph operation. + remaining KeyGraphLimits } func newKeyGraph(ctx context.Context, doc *document.Document, options KeyGraphOptions) (*keyGraph, *graphDocument, error) { + if options.Data.KeyTransportAlgorithm != "" { + return nil, nil, ErrPolicy + } + p, err := normalize(ctx, options.Data) if err != nil { return nil, nil, err } limits, err := normalizeGraphLimits(options.Limits) - if err != nil || len(options.Keys) > 64 || len(options.KeyAlgorithms) > 32 || len(options.KeyURIs) > 64 || len(options.IDRules) > 28 { + if err != nil || len(options.Keys) > maximumGraphBindings || len(options.KeyAlgorithms) > maximumGraphKeyAlgorithms || len(options.KeyURIs) > maximumGraphKeyURIs || len(options.IDRules) > maximumGraphIDRules { return nil, nil, ErrPolicy } @@ -203,7 +242,7 @@ func newKeyGraph(ctx context.Context, doc *document.Document, options KeyGraphOp } if options.MasterKeyBytes == 0 { - options.MasterKeyBytes = 32 + options.MasterKeyBytes = defaultMasterKeyBytes } if !sessionKeySize(options.MasterKeyBytes) { @@ -235,6 +274,9 @@ func newKeyGraph(ctx context.Context, doc *document.Document, options KeyGraphOp } func (g *keyGraph) configure() error { + // Derive one output octet only to validate configured KDF parameters. + const concatKDFValidationOutputBits = 8 + seen := make(map[graphBinding]struct{}) for _, binding := range g.options.Keys { @@ -260,7 +302,7 @@ func (g *keyGraph) configure() error { for _, binding := range g.options.Keys { identity := graphBinding{binding.Name, binding.Algorithm} - if len(binding.Name) > 1024 || !utf8.ValidString(binding.Name) || binding.Algorithm == "" { + if len(binding.Name) > maximumKeyNameBytes || !utf8.ValidString(binding.Name) || binding.Algorithm == "" { return ErrPolicy } @@ -294,7 +336,7 @@ func (g *keyGraph) configure() error { return err } - probe, err := DeriveConcatKey(g.ctx, []byte{1}, 8, *g.options.ConcatKDF) + probe, err := DeriveConcatKey(g.ctx, []byte{1}, concatKDFValidationOutputBits, *g.options.ConcatKDF) clear(probe) if err != nil { @@ -311,7 +353,7 @@ func (g *keyGraph) configureExternal() error { return nil } - if nilcheck.IsNil(external.Resolver) || len(external.AllowedURIs) > 64 || external.BaseURI != "" && !validCipherURI(external.BaseURI) { + if nilcheck.IsNil(external.Resolver) || len(external.AllowedURIs) > maximumGraphAllowedURIs || external.BaseURI != "" && !validCipherURI(external.BaseURI) { return ErrPolicy } @@ -363,7 +405,7 @@ func (g *keyGraph) validateBinding(binding KeyGraphKey) error { } if binding.Algorithm == ConcatKDF { - if len(binding.Secret) > 1024 { + if len(binding.Secret) > maximumGraphSecretBytes { return ErrPolicy } @@ -439,7 +481,7 @@ func (g *keyGraph) addDocument(doc *document.Document, uri string) (*graphDocume } } - if node.Name == elementName(Namespace, "EncryptedKey") || node.Name == elementName(Namespace11, "DerivedKey") { + if node.Name == elementName(Namespace, nameEncryptedKey) || node.Name == elementName(Namespace11, nameDerivedKey) { d.keys = append(d.keys, graphNode{d, node}) } @@ -557,7 +599,7 @@ func (g *keyGraph) localTarget(doc *graphDocument, uri string) (graphNode, error } func (g *keyGraph) chargeAttribute(attribute tree.Attribute) error { - if g.baseURI != nil && attribute.Name == (document.Name{Space: document.XMLNamespace, Local: "base"}) { + if g.baseURI != nil && attribute.Name == (document.Name{Space: document.XMLNamespace, Local: lexicon.AttrBase}) { if !validGraphReference(attribute.Value) || uriref.Validate(attribute.Value) != nil || strings.Contains(attribute.Value, "#") { return ErrFormat } @@ -573,22 +615,22 @@ func (g *keyGraph) chargeAttribute(attribute tree.Attribute) error { func normalizeGraphLimits(limits KeyGraphLimits) (KeyGraphLimits, error) { if limits.MaxDepth == 0 { - limits.MaxDepth = 8 + limits.MaxDepth = defaultGraphDepth } if limits.MaxNodes == 0 { - limits.MaxNodes = 8192 + limits.MaxNodes = defaultGraphNodes } if limits.MaxBytes == 0 { - limits.MaxBytes = 32 << 20 + limits.MaxBytes = defaultGraphBytes } if limits.MaxResources == 0 { - limits.MaxResources = 8 + limits.MaxResources = defaultGraphResources } - if limits.MaxDepth < 1 || limits.MaxDepth > 32 || limits.MaxNodes < 1 || limits.MaxNodes > 100000 || limits.MaxBytes < 1 || limits.MaxBytes > 128<<20 || limits.MaxResources < 1 || limits.MaxResources > 64 { + if limits.MaxDepth < 1 || limits.MaxDepth > maximumGraphDepth || limits.MaxNodes < 1 || limits.MaxNodes > maximumGraphNodes || limits.MaxBytes < 1 || limits.MaxBytes > maximumGraphBytes || limits.MaxResources < 1 || limits.MaxResources > maximumGraphResources { return KeyGraphLimits{}, ErrPolicy } @@ -598,11 +640,13 @@ func normalizeGraphLimits(limits KeyGraphLimits) (KeyGraphLimits, error) { func graphWrappingKeyBytes(algorithm string) int { switch algorithm { case AES128KeyWrap: - return 16 - case AES192KeyWrap, TripleDESKeyWrap: - return 24 + return aes128KeyBytes + case AES192KeyWrap: + return aes192KeyBytes + case TripleDESKeyWrap: + return tripleDESKeyBytes case AES256KeyWrap: - return 32 + return aes256KeyBytes } return 0 @@ -617,18 +661,18 @@ func graphLimitError() error { return errors.Join(ErrPolicy, document.ErrLimit) func graphIDRules(extra []reference.IDAttributeRule) []reference.IDAttributeRule { rules := make([]reference.IDAttributeRule, 0, 2+len(extra)) rules = append(rules, reference.IDAttributeRule{ - Attribute: document.Name{Local: "Id"}, Elements: []document.Name{ - elementName(Namespace, "EncryptedData"), elementName(Namespace, "EncryptedKey"), elementName(Namespace11, "DerivedKey"), - elementName(Namespace, "EncryptionProperties"), elementName(Namespace, "EncryptionProperty"), elementName(SignatureNamespace, lexicon.DSigElementKeyInfo), - elementName(Namespace, "OriginatorKeyInfo"), elementName(Namespace, "RecipientKeyInfo"), + Attribute: document.Name{Local: lexicon.SecurityAttrID}, Elements: []document.Name{ + elementName(Namespace, nameEncryptedData), elementName(Namespace, nameEncryptedKey), elementName(Namespace11, nameDerivedKey), + elementName(Namespace, nameEncryptionProperties), elementName(Namespace, nameEncryptionProperty), elementName(SignatureNamespace, lexicon.DSigElementKeyInfo), + elementName(Namespace, nameOriginatorKeyInfo), elementName(Namespace, nameRecipientKeyInfo), }, - }, reference.IDAttributeRule{Attribute: document.Name{Space: document.XMLNamespace, Local: "id"}}) + }, reference.IDAttributeRule{Attribute: document.Name{Space: document.XMLNamespace, Local: lexicon.AttrID}}) return append(rules, extra...) } func validGraphReference(value string) bool { - if len(value) > 4096 || !utf8.ValidString(value) || strings.Contains(value, "\\") { + if len(value) > maximumCipherURIBytes || !utf8.ValidString(value) || strings.Contains(value, "\\") { return false } @@ -647,7 +691,7 @@ func graphNameText(element document.Element) (string, error) { } text := element.Text() - if len(text) > 1024 { + if len(text) > maximumGraphTextBytes { return "", graphLimitError() } diff --git a/xmlenc/kdf.go b/xmlenc/kdf.go index 85e5175..4c14e78 100644 --- a/xmlenc/kdf.go +++ b/xmlenc/kdf.go @@ -7,9 +7,32 @@ import ( "crypto/sha1" // #nosec G505 -- XMLENC requires SHA-1 only under explicit legacy policy. "crypto/sha256" "encoding/binary" + "math" "math/bits" ) +const ( + maximumDerivedKeyBits = 8192 + maximumKDFFieldBytes = 4097 + maximumKDFSecretBytes = 1024 +) + +// keyOctetBits converts key and digest byte widths to bits across this package. +const keyOctetBits = 8 + +// Representation widths shared by ConcatKDF assembly and bit-oriented SHA. +// The counter and hash word are distinct four-byte fields; length fields below +// count message bits rather than bytes or accepted derivation work. +const ( + kdfLastOctetBit = keyOctetBits - 1 + kdfCounterBytes = 4 + kdfHashWordBytes = 4 + kdfFieldHeaderBytes = 1 + concatSHA256StateWords = sha256.Size / kdfHashWordBytes + concatSHA1StateWords = sha1.Size / kdfHashWordBytes + concatInitialBlockWords = sha256.BlockSize / kdfHashWordBytes +) + // ConcatKDF is the XML Encryption 1.1 concatenation key derivation URI. const ConcatKDF = Namespace11 + "ConcatKDF" @@ -51,7 +74,7 @@ func DeriveConcatKey(ctx context.Context, secret []byte, keyBits int, parameters return nil, err } - if len(secret) == 0 || len(secret) > 1024 || keyBits < 1 || keyBits > 8192 { + if len(secret) == 0 || len(secret) > maximumKDFSecretBytes || keyBits < 1 || keyBits > maximumDerivedKeyBits { return nil, ErrPolicy } @@ -76,28 +99,28 @@ func DeriveConcatKey(ctx context.Context, secret []byte, keyBits int, parameters otherBits += n } - if otherBits%8 != 0 && fips140.Enabled() { + if otherBits%keyOctetBits != 0 && fips140.Enabled() { return nil, ErrPolicy } - messageBits := (4+len(secret))*8 + otherBits + messageBits := (kdfCounterBytes+len(secret))*keyOctetBits + otherBits - message := make([]byte, (messageBits+7)/8) + message := make([]byte, (messageBits+kdfLastOctetBit)/keyOctetBits) defer clear(message) - copy(message[4:], secret) - offset := (4 + len(secret)) * 8 + copy(message[kdfCounterBytes:], secret) + offset := (kdfCounterBytes + len(secret)) * keyOctetBits for _, field := range fields { n, _ := concatFieldBits(field) for i := range n { - message[(offset+i)/8] |= ((field[1+i/8] >> (7 - i%8)) & 1) << (7 - (offset+i)%8) + message[(offset+i)/keyOctetBits] |= ((field[kdfFieldHeaderBytes+i/keyOctetBits] >> (kdfLastOctetBit - i%keyOctetBits)) & 1) << (kdfLastOctetBit - (offset+i)%keyOctetBits) } offset += n } - output := make([]byte, (keyBits+7)/8) + output := make([]byte, (keyBits+kdfLastOctetBit)/keyOctetBits) for offset, counter := 0, uint32(1); offset < len(output); counter++ { if err := ctx.Err(); err != nil { clear(output) @@ -116,8 +139,8 @@ func DeriveConcatKey(ctx context.Context, secret []byte, keyBits int, parameters clear(digest) } - if keyBits%8 != 0 { - output[len(output)-1] &= 0xff << (8 - keyBits%8) + if keyBits%keyOctetBits != 0 { + output[len(output)-1] &= math.MaxUint8 << (keyOctetBits - keyBits%keyOctetBits) } return output, nil @@ -128,7 +151,7 @@ func concatFieldBits(field []byte) (int, bool) { return 0, true } - if len(field) > 4097 || field[0] > 7 || len(field) == 1 && field[0] != 0 { + if len(field) > maximumKDFFieldBytes || field[0] > kdfLastOctetBit || len(field) == kdfFieldHeaderBytes && field[0] != 0 { return 0, false } @@ -137,14 +160,20 @@ func concatFieldBits(field []byte) (int, bool) { return 0, false } - return (len(field)-1)*8 - padding, true + return (len(field)-kdfFieldHeaderBytes)*keyOctetBits - padding, true } // Whole-octet messages use the standard library. Only XMLENC's arbitrary-bit // OtherInfo requires the FIPS 180-4 compression and bit-length padding below. // It is tested against NIST SHAVS bit-oriented SHA-1 and SHA-256 vectors. func concatDigest(ctx context.Context, algorithm crypto.Hash, message []byte, length int) ([]byte, error) { - if length%8 == 0 { + const ( + lengthFieldBytes = 8 + lengthFieldBits = lengthFieldBytes * keyOctetBits + blockBits = sha256.BlockSize * keyOctetBits + ) + + if length%keyOctetBits == 0 { if algorithm == crypto.SHA1 { digest := sha1.Sum(message) // #nosec G401 -- Explicit legacy policy is enforced by DeriveConcatKey. return digest[:], nil @@ -155,53 +184,58 @@ func concatDigest(ctx context.Context, algorithm crypto.Hash, message []byte, le return digest[:], nil } - padded := make([]byte, ((length+1+64+511)/512)*64) + padded := make([]byte, ((length+1+lengthFieldBits+blockBits-1)/blockBits)*sha256.BlockSize) defer clear(padded) copy(padded, message) - padded[length/8] &= 0xff << (8 - length%8) - padded[length/8] |= 1 << (7 - length%8) - binary.BigEndian.PutUint64(padded[len(padded)-8:], uint64(length)) // #nosec G115 -- DeriveConcatKey bounds this nonnegative bit length before allocation. + padded[length/keyOctetBits] &= math.MaxUint8 << (keyOctetBits - length%keyOctetBits) + padded[length/keyOctetBits] |= 1 << (kdfLastOctetBit - length%keyOctetBits) + binary.BigEndian.PutUint64(padded[len(padded)-lengthFieldBytes:], uint64(length)) // #nosec G115 -- DeriveConcatKey bounds this nonnegative bit length before allocation. - state := [8]uint32{0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19} + state := [concatSHA256StateWords]uint32{0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19} defer clear(state[:]) - words := 8 + words := concatSHA256StateWords if algorithm == crypto.SHA1 { - state = [8]uint32{0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0} - words = 5 + state = [concatSHA256StateWords]uint32{0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0} + words = concatSHA1StateWords } - for offset := 0; offset < len(padded); offset += 64 { + for offset := 0; offset < len(padded); offset += sha256.BlockSize { if err := ctx.Err(); err != nil { return nil, err } if algorithm == crypto.SHA1 { - concatSHA1Block(&state, padded[offset:offset+64]) + concatSHA1Block(&state, padded[offset:offset+sha256.BlockSize]) } else { - concatSHA256Block(&state, padded[offset:offset+64]) + concatSHA256Block(&state, padded[offset:offset+sha256.BlockSize]) } } - output := make([]byte, words*4) + output := make([]byte, words*kdfHashWordBytes) for i := range words { - binary.BigEndian.PutUint32(output[4*i:], state[i]) + binary.BigEndian.PutUint32(output[kdfHashWordBytes*i:], state[i]) } return output, nil } -func concatSHA1Block(state *[8]uint32, block []byte) { - var schedule [80]uint32 +func concatSHA1Block(state *[concatSHA256StateWords]uint32, block []byte) { + const ( + rounds = 80 + roundsPerPhase = 20 + ) + + var schedule [rounds]uint32 defer clear(schedule[:]) - for i := range 16 { - schedule[i] = binary.BigEndian.Uint32(block[4*i:]) + for i := range concatInitialBlockWords { + schedule[i] = binary.BigEndian.Uint32(block[kdfHashWordBytes*i:]) } - for i := 16; i < 80; i++ { + for i := concatInitialBlockWords; i < rounds; i++ { schedule[i] = bits.RotateLeft32(schedule[i-3]^schedule[i-8]^schedule[i-14]^schedule[i-16], 1) } @@ -211,11 +245,11 @@ func concatSHA1Block(state *[8]uint32, block []byte) { var f, k uint32 switch { - case i < 20: + case i < roundsPerPhase: f, k = b&c|(^b)&d, 0x5a827999 - case i < 40: + case i < 2*roundsPerPhase: f, k = b^c^d, 0x6ed9eba1 - case i < 60: + case i < 3*roundsPerPhase: f, k = b&c|b&d|c&d, 0x8f1bbcdc default: f, k = b^c^d, 0xca62c1d6 @@ -231,7 +265,7 @@ func concatSHA1Block(state *[8]uint32, block []byte) { state[4] += e } -func concatSHA256Block(state *[8]uint32, block []byte) { +func concatSHA256Block(state *[concatSHA256StateWords]uint32, block []byte) { constants := [...]uint32{ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, @@ -243,14 +277,14 @@ func concatSHA256Block(state *[8]uint32, block []byte) { 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2, } - var schedule [64]uint32 + var schedule [len(constants)]uint32 defer clear(schedule[:]) - for i := range 16 { - schedule[i] = binary.BigEndian.Uint32(block[4*i:]) + for i := range concatInitialBlockWords { + schedule[i] = binary.BigEndian.Uint32(block[kdfHashWordBytes*i:]) } - for i := 16; i < 64; i++ { + for i := concatInitialBlockWords; i < len(schedule); i++ { x, y := schedule[i-15], schedule[i-2] s0 := bits.RotateLeft32(x, -7) ^ bits.RotateLeft32(x, -18) ^ (x >> 3) s1 := bits.RotateLeft32(y, -17) ^ bits.RotateLeft32(y, -19) ^ (y >> 10) diff --git a/xmlenc/keywrap.go b/xmlenc/keywrap.go index 1daf535..4f8b15c 100644 --- a/xmlenc/keywrap.go +++ b/xmlenc/keywrap.go @@ -32,13 +32,19 @@ import ( "encoding/binary" ) +const ( + // Size of one RFC 3394 register, in bytes; also the wrapped-key overhead. + keyWrapBlockBytes = 8 + keyWrapRounds = 6 +) + const keyWrapIV uint64 = 0xa6a6a6a6a6a6a6a6 // keyWrapProfile selects an authorized key-wrapping algorithm, validates the // key-encryption key size and binds wrapping functions to the normalized // data-encryption profile. func keyWrapProfile(ctx context.Context, kek []byte, options KeyWrapOptions) (profile, error) { - if options.Data.OAEP != nil { + if options.Data.OAEP != nil || options.Data.KeyTransportAlgorithm != "" { return profile{}, ErrPolicy } @@ -60,11 +66,11 @@ func keyWrapProfile(ctx context.Context, kek []byte, options KeyWrapOptions) (pr switch algorithm { case AES128KeyWrap: - kekBytes = 16 + kekBytes = aes128KeyBytes case AES192KeyWrap: - kekBytes = 24 + kekBytes = aes192KeyBytes case AES256KeyWrap: - kekBytes = 32 + kekBytes = aes256KeyBytes default: return profile{}, ErrPolicy } @@ -80,7 +86,7 @@ func keyWrapProfile(ctx context.Context, kek []byte, options KeyWrapOptions) (pr keyBytes := p.keyBytes p.transport = keyTransport{ - algorithm: algorithm, wrappedBytes: keyBytes + 8, keyBits: kekBytes * 8, + algorithm: algorithm, wrappedBytes: keyBytes + keyWrapBlockBytes, keyBits: kekBytes * keyOctetBits, wrap: func(key []byte) ([]byte, error) { return wrapKey(block, key) }, unwrap: func(_ context.Context, wrapped []byte) ([]byte, error) { return unwrapKey(block, wrapped, keyBytes) @@ -90,7 +96,9 @@ func keyWrapProfile(ctx context.Context, kek []byte, options KeyWrapOptions) (pr return p, nil } -func sessionKeySize(size int) bool { return size == 16 || size == 24 || size == 32 } +func sessionKeySize(size int) bool { + return size == aes128KeyBytes || size == aes192KeyBytes || size == aes256KeyBytes +} // wrapKey applies RFC 3394 wrapping to a supported session-key size using the // supplied AES block. It returns newly allocated wrapped bytes. @@ -99,8 +107,8 @@ func wrapKey(block cipher.Block, key []byte) ([]byte, error) { return nil, ErrPolicy } - out := make([]byte, len(key)+8) - copy(out[8:], key) + out := make([]byte, len(key)+keyWrapBlockBytes) + copy(out[keyWrapBlockBytes:], key) a := keyWrapIV @@ -109,19 +117,19 @@ func wrapKey(block cipher.Block, key []byte) ([]byte, error) { var counter uint64 - for range 6 { - for offset := 8; offset < len(out); offset += 8 { + for range keyWrapRounds { + for offset := keyWrapBlockBytes; offset < len(out); offset += keyWrapBlockBytes { counter++ - binary.BigEndian.PutUint64(buffer[:8], a) - copy(buffer[8:], out[offset:offset+8]) + binary.BigEndian.PutUint64(buffer[:keyWrapBlockBytes], a) + copy(buffer[keyWrapBlockBytes:], out[offset:offset+keyWrapBlockBytes]) block.Encrypt(buffer[:], buffer[:]) - a = binary.BigEndian.Uint64(buffer[:8]) ^ counter - copy(out[offset:offset+8], buffer[8:]) + a = binary.BigEndian.Uint64(buffer[:keyWrapBlockBytes]) ^ counter + copy(out[offset:offset+keyWrapBlockBytes], buffer[keyWrapBlockBytes:]) } } - binary.BigEndian.PutUint64(out[:8], a) + binary.BigEndian.PutUint64(out[:keyWrapBlockBytes], a) return out, nil } @@ -132,34 +140,34 @@ func wrapKey(block cipher.Block, key []byte) ([]byte, error) { func unwrapKey(block cipher.Block, wrapped []byte, keyBytes int) ([]byte, error) { // Check the expected session-key size before allocation or AES operations. // General RFC 3394 inputs can be much larger than the bounded data-key profile. - if !sessionKeySize(keyBytes) || len(wrapped) != keyBytes+8 { + if !sessionKeySize(keyBytes) || len(wrapped) != keyBytes+keyWrapBlockBytes { return nil, ErrDecrypt } out := make([]byte, keyBytes) - copy(out, wrapped[8:]) - a := binary.BigEndian.Uint64(wrapped[:8]) + copy(out, wrapped[keyWrapBlockBytes:]) + a := binary.BigEndian.Uint64(wrapped[:keyWrapBlockBytes]) var buffer [aes.BlockSize]byte defer clear(buffer[:]) - counter := uint64(len(out) / 8 * 6) - for range 6 { - for offset := len(out) - 8; offset >= 0; offset -= 8 { - binary.BigEndian.PutUint64(buffer[:8], a^counter) + counter := uint64(len(out) / keyWrapBlockBytes * keyWrapRounds) + for range keyWrapRounds { + for offset := len(out) - keyWrapBlockBytes; offset >= 0; offset -= keyWrapBlockBytes { + binary.BigEndian.PutUint64(buffer[:keyWrapBlockBytes], a^counter) counter-- - copy(buffer[8:], out[offset:offset+8]) + copy(buffer[keyWrapBlockBytes:], out[offset:offset+keyWrapBlockBytes]) block.Decrypt(buffer[:], buffer[:]) - a = binary.BigEndian.Uint64(buffer[:8]) - copy(out[offset:offset+8], buffer[8:]) + a = binary.BigEndian.Uint64(buffer[:keyWrapBlockBytes]) + copy(out[offset:offset+keyWrapBlockBytes], buffer[keyWrapBlockBytes:]) } } - binary.BigEndian.PutUint64(buffer[:8], a) - binary.BigEndian.PutUint64(buffer[8:], keyWrapIV) + binary.BigEndian.PutUint64(buffer[:keyWrapBlockBytes], a) + binary.BigEndian.PutUint64(buffer[keyWrapBlockBytes:], keyWrapIV) - if subtle.ConstantTimeCompare(buffer[:8], buffer[8:]) != 1 { + if subtle.ConstantTimeCompare(buffer[:keyWrapBlockBytes], buffer[keyWrapBlockBytes:]) != 1 { clear(out) return nil, ErrDecrypt } diff --git a/xmlenc/keywrap_des.go b/xmlenc/keywrap_des.go index 1fc2890..65df57f 100644 --- a/xmlenc/keywrap_des.go +++ b/xmlenc/keywrap_des.go @@ -13,11 +13,18 @@ import ( "slices" ) +const ( + tripleDESWrappedKeyBytes = des.BlockSize + tripleDESKeyBytes + tripleDESWrapChecksumBytes + tripleDESWrapChecksumBytes = 8 + tripleDESWrapChecksumOffset = des.BlockSize + tripleDESKeyBytes + tripleDESKeyBits = tripleDESKeyBytes * keyOctetBits +) + const tripleDESWrapIV = "\x4a\xdd\xa2\x2c\x79\xe8\x21\x05" func tripleDESWrapProfile(p profile, kek []byte) (profile, error) { // RFC3217 wraps a TripleDES CEK; wrapping other key types is optional. - if !p.options.AllowLegacy || fips140.Enabled() || len(kek) != 24 || !p.data.tripleDES { + if !p.options.AllowLegacy || fips140.Enabled() || len(kek) != tripleDESKeyBytes || !p.data.tripleDES { return profile{}, ErrPolicy } @@ -27,7 +34,7 @@ func tripleDESWrapProfile(p profile, kek []byte) (profile, error) { } p.transport = keyTransport{ - algorithm: TripleDESKeyWrap, wrappedBytes: 40, keyBits: 192, + algorithm: TripleDESKeyWrap, wrappedBytes: tripleDESWrappedKeyBytes, keyBits: tripleDESKeyBits, wrap: func(key []byte) ([]byte, error) { return wrapTripleDES(block, key) }, unwrap: func(_ context.Context, wrapped []byte) ([]byte, error) { return unwrapTripleDES(block, wrapped) @@ -38,29 +45,35 @@ func tripleDESWrapProfile(p profile, kek []byte) (profile, error) { } func oddDESParity(key []byte) { + // DES stores odd parity in the low bit of each octet; the other bits carry key data. + const ( + desParityBit = 0x01 + desKeyDataMask = 0xfe + ) + for i, b := range key { - key[i] = b & 0xfe + key[i] = b & desKeyDataMask if bits.OnesCount8(key[i])%2 == 0 { - key[i] |= 1 + key[i] |= desParityBit } } } func wrapTripleDES(block cipher.Block, key []byte) ([]byte, error) { - if len(key) != 24 { + if len(key) != tripleDESKeyBytes { return nil, ErrPolicy } - out := make([]byte, 40) + out := make([]byte, tripleDESWrappedKeyBytes) if _, err := io.ReadFull(rand.Reader, out[:des.BlockSize]); err != nil { return nil, err } - copy(out[8:32], key) - oddDESParity(out[8:32]) - checksum := sha1.Sum(out[8:32]) // #nosec G401 -- RFC3217 integrity checksum, not a signature. - copy(out[32:], checksum[:8]) - cipher.NewCBCEncrypter(block, out[:8]).CryptBlocks(out[8:], out[8:]) + copy(out[des.BlockSize:tripleDESWrapChecksumOffset], key) + oddDESParity(out[des.BlockSize:tripleDESWrapChecksumOffset]) + checksum := sha1.Sum(out[des.BlockSize:tripleDESWrapChecksumOffset]) // #nosec G401 -- RFC3217 integrity checksum, not a signature. + copy(out[tripleDESWrapChecksumOffset:], checksum[:tripleDESWrapChecksumBytes]) + cipher.NewCBCEncrypter(block, out[:des.BlockSize]).CryptBlocks(out[des.BlockSize:], out[des.BlockSize:]) slices.Reverse(out) cipher.NewCBCEncrypter(block, []byte(tripleDESWrapIV)).CryptBlocks(out, out) // #nosec G407 -- RFC3217§3.1step8 mandates this second-pass IV. @@ -68,7 +81,7 @@ func wrapTripleDES(block cipher.Block, key []byte) ([]byte, error) { } func unwrapTripleDES(block cipher.Block, wrapped []byte) ([]byte, error) { - if len(wrapped) != 40 { + if len(wrapped) != tripleDESWrappedKeyBytes { return nil, ErrDecrypt } @@ -77,17 +90,17 @@ func unwrapTripleDES(block cipher.Block, wrapped []byte) ([]byte, error) { cipher.NewCBCDecrypter(block, []byte(tripleDESWrapIV)).CryptBlocks(buffer, buffer) slices.Reverse(buffer) - cipher.NewCBCDecrypter(block, buffer[:8]).CryptBlocks(buffer[8:], buffer[8:]) - checksum := sha1.Sum(buffer[8:32]) // #nosec G401 -- RFC3217 requires SHA-1. + cipher.NewCBCDecrypter(block, buffer[:des.BlockSize]).CryptBlocks(buffer[des.BlockSize:], buffer[des.BlockSize:]) + checksum := sha1.Sum(buffer[des.BlockSize:tripleDESWrapChecksumOffset]) // #nosec G401 -- RFC3217 requires SHA-1. parity := 0 - for _, b := range buffer[8:32] { + for _, b := range buffer[des.BlockSize:tripleDESWrapChecksumOffset] { parity |= (bits.OnesCount8(b) + 1) % 2 } - if subtle.ConstantTimeCompare(checksum[:8], buffer[32:]) != 1 || parity != 0 { + if subtle.ConstantTimeCompare(checksum[:tripleDESWrapChecksumBytes], buffer[tripleDESWrapChecksumOffset:]) != 1 || parity != 0 { return nil, ErrDecrypt } - return slices.Clone(buffer[8:32]), nil + return slices.Clone(buffer[des.BlockSize:tripleDESWrapChecksumOffset]), nil } diff --git a/xmlenc/keywrap_test.go b/xmlenc/keywrap_test.go index 108fa2e..a932747 100644 --- a/xmlenc/keywrap_test.go +++ b/xmlenc/keywrap_test.go @@ -160,6 +160,10 @@ func TestConcatBitDigest(t *testing.T) { {crypto.SHA256, 809, "8219618b7728ac89237705ecf84012cc7c80293c4cf171d86139449d9361d8fe5b881f33ddd9ebd526ba56a8b24661b831fedba78abb854521e8736156edb5df4eee370bf5b6e62d43ee145ebf931e9942a74c15fe26f8d2be3bf3726fb4244be0b472bac680", "b857f827ec3a28a9997600f545cd8e189eb75185c9c8c8e0d046c9247e3c109e"}, } { t.Run(tc.algorithm.String()+"/"+strconv.Itoa(tc.length), func(t *testing.T) { + if tc.algorithm == crypto.SHA1 && fips140.Enabled() { + t.Skip("legacy SHA-1 vectors are outside the FIPS ConcatKDF profile") + } + data, err := hex.DecodeString(tc.message) if err != nil { t.Fatal(err) diff --git a/xmlenc/method.go b/xmlenc/method.go index 78525d9..b19d356 100644 --- a/xmlenc/method.go +++ b/xmlenc/method.go @@ -6,9 +6,17 @@ import ( "strings" "github.com/skarm/xmlsec/document" + "github.com/skarm/xmlsec/internal/lexicon" "github.com/skarm/xmlsec/internal/tree" ) +const ( + keySizeContextCheckInterval = 1024 + // Declared key length in bits, compared with the selected key. Compare the expected XML Encryption + // namespace when matching it. + nameKeySize = "KeySize" +) + // KeySize is optional even for fixed-size algorithms, but never changes the // caller-selected key or parameters. The schema places it before all others. func checkEncryptionMethod(ctx context.Context, method document.Element, algorithm string, bits int, parameters bool) error { @@ -24,7 +32,7 @@ func checkEncryptionMethod(ctx context.Context, method document.Element, algorit } for i, child := range method.Children() { - if child.Name() == elementName(Namespace, "KeySize") { + if child.Name() == elementName(Namespace, nameKeySize) { if i != 0 { return ErrFormat } @@ -49,7 +57,7 @@ func checkMethodKeySize(ctx context.Context, element document.Element, bits int) return ErrFormat } - value := strings.Trim(element.Text(), " \t\r\n") + value := strings.Trim(element.Text(), lexicon.XMLWhitespace) value = strings.TrimPrefix(value, "+") if value == "" { @@ -62,7 +70,7 @@ func checkMethodKeySize(ctx context.Context, element document.Element, bits int) return ErrFormat } - if i%1024 == 0 { + if i%keySizeContextCheckInterval == 0 { if err := ctx.Err(); err != nil { return err } diff --git a/xmlenc/oaep.go b/xmlenc/oaep.go index 840eebc..82c38c1 100644 --- a/xmlenc/oaep.go +++ b/xmlenc/oaep.go @@ -12,6 +12,15 @@ import ( "github.com/skarm/xmlsec/internal/tree" ) +const ( + maximumOAEPLabelBytes = 1024 +) + +const ( + nameMGF = "MGF" + nameOAEPparams = "OAEPparams" +) + func normalizeOAEP(parameters *OAEPParameters) (OAEPParameters, error) { p := OAEPParameters{} if parameters != nil { @@ -34,7 +43,7 @@ func normalizeOAEP(parameters *OAEPParameters) (OAEPParameters, error) { } if !rsaOAEPAlgorithm(p.Algorithm) || p.Algorithm == RSAOAEP10 && p.MGFHash != crypto.SHA1 || - !oaepHash(p.Hash) || !oaepHash(p.MGFHash) || len(p.Label) > 1024 { + !oaepHash(p.Hash) || !oaepHash(p.MGFHash) || len(p.Label) > maximumOAEPLabelBytes { return OAEPParameters{}, ErrPolicy } @@ -72,21 +81,21 @@ func oaepURIs(hash crypto.Hash) (string, string) { func encodeOAEP(method *tree.Node, p OAEPParameters) { if len(p.Label) != 0 { - label := encryptedElement("OAEPparams") + label := encryptedElement(nameOAEPparams) appendChild(label, &tree.Node{Kind: tree.TextNode, Data: base64.StdEncoding.EncodeToString(p.Label)}) appendChild(method, label) } digestURI, _ := oaepURIs(p.Hash) - digest := newElement(SignatureNamespace, "ds", lexicon.DSigElementDigestMethod) - setAttribute(digest, "Algorithm", digestURI) + digest := newElement(SignatureNamespace, lexicon.PrefixDSig, lexicon.DSigElementDigestMethod) + setAttribute(digest, lexicon.SecurityAttrAlgorithm, digestURI) appendChild(method, digest) if p.Algorithm == RSAOAEP { _, mgfURI := oaepURIs(p.MGFHash) - mgf := newElement(Namespace11, "xenc11", "MGF") - setAttribute(mgf, "Algorithm", mgfURI) + mgf := newElement(Namespace11, prefixXEnc11, nameMGF) + setAttribute(mgf, lexicon.SecurityAttrAlgorithm, mgfURI) appendChild(method, mgf) } } @@ -107,7 +116,7 @@ func checkOAEP(ctx context.Context, method document.Element, expected OAEPParame for _, child := range method.Children() { switch child.Name() { - case elementName(Namespace, "KeySize"): + case elementName(Namespace, nameKeySize): // The common EncryptionMethod checker validated value and order. continue case elementName(SignatureNamespace, lexicon.DSigElementDigestMethod): @@ -116,13 +125,13 @@ func checkOAEP(ctx context.Context, method document.Element, expected OAEPParame } digest = true - case elementName(Namespace11, "MGF"): + case elementName(Namespace11, nameMGF): if expected.Algorithm == RSAOAEP10 || mgf || !matchesMethod(child, mgfURI, true) { return ErrFormat } mgf = true - case elementName(Namespace, "OAEPparams"): + case elementName(Namespace, nameOAEPparams): if label || digest || mgf { return ErrFormat } @@ -149,7 +158,7 @@ func checkOAEPLabel(ctx context.Context, child document.Element, expected []byte return ErrFormat } - value, err := decodeBase64Text(ctx, child, 1024) + value, err := decodeBase64Text(ctx, child, maximumOAEPLabelBytes) if err != nil { return err } diff --git a/xmlenc/policy.go b/xmlenc/policy.go index e22988b..0e8e613 100644 --- a/xmlenc/policy.go +++ b/xmlenc/policy.go @@ -9,6 +9,15 @@ import ( "github.com/skarm/xmlsec/internal/tree" ) +const ( + defaultMaxPlaintextBytes = 8 << 20 + maximumPlaintextBytes = 32 << 20 + maximumRSAExponent = 1<<31 - 1 + maximumRSAKeyBits = 16384 + minimumRSAExponent = 3 + minimumRSAKeyBits = 2048 +) + const ( gcmNonceBytes = 12 gcmTagBytes = 16 @@ -53,10 +62,10 @@ func normalize(ctx context.Context, options Options) (profile, error) { } if options.MaxPlaintextBytes == 0 { - options.MaxPlaintextBytes = 8 << 20 + options.MaxPlaintextBytes = defaultMaxPlaintextBytes } - if options.MaxPlaintextBytes < 1 || options.MaxPlaintextBytes > 32<<20 { + if options.MaxPlaintextBytes < 1 || options.MaxPlaintextBytes > maximumPlaintextBytes { return profile{}, ErrPolicy } @@ -85,7 +94,7 @@ func normalize(ctx context.Context, options Options) (profile, error) { } func validateRSAKey(key *rsa.PublicKey) error { - if key == nil || key.N == nil || key.N.Sign() <= 0 || key.N.Bit(0) == 0 || key.N.BitLen() < 2048 || key.N.BitLen() > 16384 || key.E < 3 || key.E%2 == 0 || key.E > 0x7fffffff { + if key == nil || key.N == nil || key.N.Sign() <= 0 || key.N.Bit(0) == 0 || key.N.BitLen() < minimumRSAKeyBits || key.N.BitLen() > maximumRSAKeyBits || key.E < minimumRSAExponent || key.E%2 == 0 || key.E > maximumRSAExponent { return ErrPolicy } diff --git a/xmlenc/provider.go b/xmlenc/provider.go index 8f50145..670c4bf 100644 --- a/xmlenc/provider.go +++ b/xmlenc/provider.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "crypto" + "crypto/fips140" "crypto/rand" "crypto/rsa" _ "crypto/sha1" // #nosec G505 -- Explicit legacy OAEP policy only. @@ -53,6 +54,21 @@ func rsaEncryptProfile(ctx context.Context, options Options, recipient *rsa.Publ return profile{}, err } + if options.KeyTransportAlgorithm != "" { + if !legacyRSAAllowed(options) { + return profile{}, ErrPolicy + } + + p.transport = keyTransport{ + algorithm: RSA15, wrappedBytes: recipient.Size(), keyBits: recipient.N.BitLen(), + wrap: func(key []byte) ([]byte, error) { + return rsa.EncryptPKCS1v15(rand.Reader, recipient, key) //nolint:staticcheck // Explicit SAML 2.0 Conformance 4.2 legacy interoperability; never selected by default. + }, + } + + return p, nil + } + oaep, err := normalizeOAEP(options.OAEP) if err != nil { return profile{}, err @@ -90,6 +106,28 @@ func rsaDecryptProfile(ctx context.Context, options Options, recipient crypto.De return profile{}, ErrPolicy } + if options.KeyTransportAlgorithm != "" { + if !legacyRSAAllowed(options) { + return profile{}, ErrPolicy + } + + p.transport = keyTransport{ + algorithm: RSA15, wrappedBytes: public.Size(), keyBits: public.N.BitLen(), + unwrap: func(ctx context.Context, ciphertext []byte) ([]byte, error) { + // SessionKeyLen requests implicit rejection from the provider: + // invalid padding yields a random key, not a padding error. + parameters := &rsa.PKCS1v15DecryptOptions{SessionKeyLen: p.keyBytes} //nolint:staticcheck // Explicit legacy transport requires implicit rejection and authenticated ciphertext. + if provider, ok := recipient.(contextualDecrypter); ok { + return provider.decrypt(ctx, rand.Reader, ciphertext, parameters) + } + + return recipient.Decrypt(rand.Reader, ciphertext, parameters) + }, + } + + return p, nil + } + oaep, err := normalizeOAEP(options.OAEP) if err != nil { return profile{}, err @@ -112,3 +150,7 @@ func rsaDecryptProfile(ctx context.Context, options Options, recipient crypto.De return p, nil } + +func legacyRSAAllowed(options Options) bool { + return options.KeyTransportAlgorithm == RSA15 && options.AllowLegacy && options.OAEP == nil && !fips140.Enabled() +} diff --git a/xmlenc/reference.go b/xmlenc/reference.go index 3089c37..6f84b36 100644 --- a/xmlenc/reference.go +++ b/xmlenc/reference.go @@ -15,9 +15,20 @@ import ( "github.com/skarm/xmlsec/internal/nilcheck" "github.com/skarm/xmlsec/internal/streamio" "github.com/skarm/xmlsec/internal/tree" + "github.com/skarm/xmlsec/internal/uriref" "github.com/skarm/xmlsec/resource" ) +const ( + cipherEncodingBase64 = "base64" + externalCipherChunkBytes = 32 << 10 + maximumCipherDocumentBytes = 64 << 20 + maximumReferencedCiphertextBytes = (32 << 20) + 32 + // Ciphertext reference resolved only through explicitly configured resource policy. Compare the + // expected XML Encryption namespace when matching it. + nameCipherReference = "CipherReference" +) + // CipherResolver supplies ciphertext for an exact application-approved URI. // Resolve and the returned reader must honor ctx, including blocked reads. // The library closes every non-nil returned reader, even when Resolve fails. @@ -58,14 +69,14 @@ func ExternalizeCiphertext(ctx context.Context, doc *document.Document, selected } node, err := selectedNode(doc, selected) - if err != nil || selected.Name() != elementName(Namespace, "EncryptedData") { + if err != nil || selected.Name() != elementName(Namespace, nameEncryptedData) { return nil, document.Element{}, nil, ErrFormat } var cipherData *tree.Node for _, child := range node.Children { - if child.Name == elementName(Namespace, "CipherData") { + if child.Name == elementName(Namespace, nameCipherData) { if cipherData != nil { return nil, document.Element{}, nil, ErrFormat } @@ -79,7 +90,7 @@ func ExternalizeCiphertext(ctx context.Context, doc *document.Document, selected } // AES-CBC needs an IV plus up to one complete padding block. - ciphertext, err := decodeCipherValue(ctx, tree.Wrap(doc, cipherData), (32<<20)+32) + ciphertext, err := decodeCipherValue(ctx, tree.Wrap(doc, cipherData), maximumReferencedCiphertextBytes) if err != nil { return nil, document.Element{}, nil, err } @@ -89,8 +100,8 @@ func ExternalizeCiphertext(ctx context.Context, doc *document.Document, selected root.Children = append([]*tree.Node(nil), node.Children...) for i, child := range root.Children { if child == cipherData { - ref := newElement(Namespace, child.Prefix, "CipherReference") - setAttribute(ref, "URI", uri) + ref := newElement(Namespace, child.Prefix, nameCipherReference) + setAttribute(ref, lexicon.SecurityAttrURI, uri) copyData := *child copyData.Children = nil @@ -108,7 +119,7 @@ func ExternalizeCiphertext(ctx context.Context, doc *document.Document, selected } func validCipherURI(value string) bool { - if value == "" || len(value) > 4096 || !utf8.ValidString(value) || strings.Contains(value, "\\") { + if value == "" || len(value) > maximumCipherURIBytes || !utf8.ValidString(value) || strings.Contains(value, "\\") || uriref.Validate(value) != nil { return false } @@ -129,10 +140,10 @@ func normalizeExternal(p ExternalCipherReference, maxCiphertext int) (ExternalCi } if p.MaxBytes == 0 { - p.MaxBytes = base64.StdEncoding.EncodedLen(maxCiphertext) + 4096 + p.MaxBytes = base64.StdEncoding.EncodedLen(maxCiphertext) + cipherValueWhitespaceBytes } - if p.MaxBytes < 1 || p.MaxBytes > 64<<20 { + if p.MaxBytes < 1 || p.MaxBytes > maximumCipherDocumentBytes { return ExternalCipherReference{}, ErrPolicy } @@ -145,15 +156,15 @@ func decodeCipherData(ctx context.Context, data document.Element, maxBytes int, } child := data.Children()[0] - if child.Name() == elementName(Namespace, "CipherValue") { + if child.Name() == elementName(Namespace, nameCipherValue) { return decodeCipherValue(ctx, data, maxBytes) } - if _, err := childElements(data, elementName(Namespace, "CipherReference")); err != nil || !exactAttributes(child, "URI") { + if _, err := childElements(data, elementName(Namespace, nameCipherReference)); err != nil || !exactAttributes(child, lexicon.SecurityAttrURI) { return nil, ErrFormat } - if external == nil || attributeValue(child, "URI") != external.URI { + if external == nil || attributeValue(child, lexicon.SecurityAttrURI) != external.URI { return nil, ErrPolicy } @@ -170,7 +181,7 @@ func decodeCipherData(ctx context.Context, data document.Element, maxBytes int, } transforms, err := childElements(children[0], elementName(SignatureNamespace, lexicon.DSigElementTransform)) - if err != nil || !matchesMethod(transforms[0], SignatureNamespace+"base64", true) { + if err != nil || !matchesMethod(transforms[0], SignatureNamespace+cipherEncodingBase64, true) { return nil, ErrFormat } @@ -245,7 +256,7 @@ func readCiphertext(ctx context.Context, source io.Reader, maximum int) ([]byte, total := 0 for { - chunk := make([]byte, min(32<<10, maximum-total+1)) + chunk := make([]byte, min(externalCipherChunkBytes, maximum-total+1)) n, err := io.ReadFull(reader, chunk) if n > 0 { diff --git a/xmlenc/reference_test.go b/xmlenc/reference_test.go index 3c12686..c8df30e 100644 --- a/xmlenc/reference_test.go +++ b/xmlenc/reference_test.go @@ -21,7 +21,7 @@ import ( const ( nameCancel = "cancel" - nameRsa = "rsa" + nameRSA = "rsa" ) type cipherReferenceProfile struct { @@ -32,7 +32,7 @@ type cipherReferenceProfile struct { func (p cipherReferenceProfile) encrypt(ctx context.Context, plain []byte) (*document.Document, error) { switch p.mode { - case nameRsa: + case nameRSA: return xmlenc.EncryptBytes(ctx, plain, &p.rsaKey.PublicKey, xmlenc.Options{}) case nameWrap: return xmlenc.EncryptBytesWithKeyWrap(ctx, plain, p.secret, xmlenc.KeyWrapOptions{}) @@ -43,7 +43,7 @@ func (p cipherReferenceProfile) encrypt(ctx context.Context, plain []byte) (*doc func (p cipherReferenceProfile) decrypt(ctx context.Context, input *document.Document, options xmlenc.Options) ([]byte, error) { switch p.mode { - case nameRsa: + case nameRSA: return xmlenc.DecryptBytes(ctx, input, p.rsaKey, options) case nameWrap: return xmlenc.DecryptBytesWithKeyWrap(ctx, input, p.secret, xmlenc.KeyWrapOptions{Data: options}) @@ -91,7 +91,7 @@ func TestCipherReferenceProfiles(t *testing.T) { rsaKey := key(t) uri := "urn:example:ciphertext:1" - for _, mode := range []string{nameRsa, nameWrap, "direct"} { + for _, mode := range []string{nameRSA, nameWrap, "direct"} { profile := cipherReferenceProfile{mode: mode, rsaKey: rsaKey, secret: secret} for _, xml := range []bool{false, true} { checkCipherReferenceProfile(t, ctx, profile, uri, xml) @@ -243,11 +243,11 @@ func TestCipherReferenceSelectedSnapshots(t *testing.T) { t.Fatal("context changed", string(got)) } - if d, h, b, err := xmlenc.ExternalizeCiphertext(ctx, encrypted, source.Root(), uri); d != nil || h.Valid() || b != nil || !errors.Is(err, xmlenc.ErrFormat) { + if d, h, b, err := xmlenc.ExternalizeCiphertext(ctx, encrypted, source.Root(), uri); d != nil || h != (document.Element{}) || b != nil || !errors.Is(err, xmlenc.ErrFormat) { t.Fatal("foreign handle accepted", err) } - if d, h, b, err := xmlenc.ExternalizeCiphertext(ctx, external, handle, uri); d != nil || h.Valid() || b != nil || !errors.Is(err, xmlenc.ErrFormat) { + if d, h, b, err := xmlenc.ExternalizeCiphertext(ctx, external, handle, uri); d != nil || h != (document.Element{}) || b != nil || !errors.Is(err, xmlenc.ErrFormat) { t.Fatal("external reference externalized again", err) } } @@ -317,6 +317,92 @@ func TestCipherReferenceRejectsBeforeIO(t *testing.T) { } } +func TestCipherURIQueryEncoding(t *testing.T) { + secret := make([]byte, 32) + encrypted, err := xmlenc.EncryptBytesWithKey(t.Context(), []byte("URI payload"), secret, xmlenc.SymmetricOptions{}) + if err != nil { + t.Fatal(err) + } + + const originalURI = "https://example.test/cipher?x=valid" + external, _, body, err := xmlenc.ExternalizeCiphertext(t.Context(), encrypted, encrypted.Root(), originalURI) + if err != nil { + t.Fatal(err) + } + wire := xmlBytes(t, external) + + for _, uri := range []string{"https://example.test/cipher?x=%zz", "https://example.test/cipher?x=%", "https://example.test/cipher?x=%0"} { + t.Run(uri, func(t *testing.T) { + if doc, selected, value, err := xmlenc.ExternalizeCiphertext(t.Context(), encrypted, encrypted.Root(), uri); doc != nil || selected != (document.Element{}) || value != nil || !errors.Is(err, xmlenc.ErrPolicy) { + t.Error("malformed query was externalized", err) + } + + changed := parse(t, bytes.Replace(wire, []byte(originalURI), []byte(uri), 1)) + calls := 0 + options := xmlenc.SymmetricOptions{Data: xmlenc.Options{External: &xmlenc.ExternalCipherReference{URI: uri, Resolver: xmlenc.CipherResolverFunc(func(context.Context, string) (io.ReadCloser, error) { + calls++ + return io.NopCloser(bytes.NewReader(body)), nil + })}}} + if plain, err := xmlenc.DecryptBytesWithKey(t.Context(), changed, secret, options); plain != nil || !errors.Is(err, xmlenc.ErrPolicy) || calls != 0 { + t.Fatal("malformed query reached resolver", err, calls) + } + }) + } + + for _, query := range []string{"%23", "%2f", "%25"} { + uri := "https://example.test/cipher?x=" + query + external, _, body, err := xmlenc.ExternalizeCiphertext(t.Context(), encrypted, encrypted.Root(), uri) + if err != nil { + t.Fatal("encoded query rejected", err) + } + calls := 0 + options := xmlenc.SymmetricOptions{Data: xmlenc.Options{External: &xmlenc.ExternalCipherReference{URI: uri, Resolver: xmlenc.CipherResolverFunc(func(_ context.Context, got string) (io.ReadCloser, error) { + calls++ + if got != uri { + t.Fatal("URI spelling changed", got) + } + return io.NopCloser(bytes.NewReader(body)), nil + })}}} + if plain, err := xmlenc.DecryptBytesWithKey(t.Context(), external, secret, options); err != nil || string(plain) != "URI payload" || calls != 1 { + t.Fatal("encoded query roundtrip", err, calls) + } + } +} + +func TestKeyGraphResourceURIQueryEncoding(t *testing.T) { + secret := bytes.Repeat([]byte{0x21}, 32) + data := graphData(t, secret, "") + for _, query := range []string{"%zz", "%", "%0", "%23", "%2f", "%25"} { + for _, base := range []bool{false, true} { + t.Run(query+"/base="+strconv.FormatBool(base), func(t *testing.T) { + uri := "https://example.test/key.xml?x=" + query + calls := 0 + external := &xmlenc.KeyGraphResources{AllowedURIs: []string{uri}, Resolver: xmlenc.CipherResolverFunc(func(context.Context, string) (io.ReadCloser, error) { + calls++ + return nil, errors.New("unneeded key resource") + })} + if base { + external.BaseURI = uri + external.AllowedURIs = nil + } + options := xmlenc.KeyGraphOptions{Keys: []xmlenc.KeyGraphKey{{Algorithm: xmlenc.AES256GCM, Secret: secret}}, External: external} + plain, err := xmlenc.DecryptBytesWithKeyGraph(t.Context(), data, data.Root(), options) + valid := query == "%23" || query == "%2f" || query == "%25" + if valid { + if err != nil || string(plain) != "graph payload\x00\xff" { + t.Fatal("valid graph URI policy rejected", err) + } + } else if plain != nil || !errors.Is(err, xmlenc.ErrPolicy) { + t.Fatal("malformed graph URI policy accepted", err) + } + if calls != 0 { + t.Fatal("URI policy validation invoked resolver", calls) + } + }) + } + } +} + func TestCipherReferenceReaderLifecycle(t *testing.T) { ctx := t.Context() secret := make([]byte, 32) @@ -488,78 +574,6 @@ func TestCipherReferenceRetainsErrorOnFullChunk(t *testing.T) { } } -func externalOptions(uri string, body []byte) xmlenc.Options { - return xmlenc.Options{External: &xmlenc.ExternalCipherReference{ - URI: uri, - Resolver: xmlenc.CipherResolverFunc(func(context.Context, string) (io.ReadCloser, error) { - return io.NopCloser(bytes.NewReader(body)), nil - }), - }} -} - -func decryptReader(t *testing.T, source io.ReadCloser) ([]byte, error) { - t.Helper() - - key := make([]byte, 32) - - encrypted, err := xmlenc.EncryptBytesWithKey(t.Context(), []byte("secret"), key, xmlenc.SymmetricOptions{}) - if err != nil { - t.Fatal(err) - } - - const uri = "urn:review:ciphertext" - - external, _, _, err := xmlenc.ExternalizeCiphertext(t.Context(), encrypted, encrypted.Root(), uri) - if err != nil { - t.Fatal(err) - } - - return xmlenc.DecryptBytesWithKey(t.Context(), external, key, xmlenc.SymmetricOptions{Data: xmlenc.Options{External: &xmlenc.ExternalCipherReference{URI: uri, Resolver: xmlenc.CipherResolverFunc(func(context.Context, string) (io.ReadCloser, error) { return source, nil })}}}) -} - -func graphXML(t *testing.T, doc *document.Document) string { - t.Helper() - wire, err := doc.XML(t.Context()) - if err != nil { - t.Fatal(err) - } - return string(wire) -} - -func graphData(t *testing.T, secret []byte, info string) *document.Document { - t.Helper() - doc, err := xmlenc.EncryptBytesWithKey(t.Context(), []byte("graph payload\x00\xff"), secret, xmlenc.SymmetricOptions{}) - if err != nil { - t.Fatal(err) - } - wire := strings.Replace(graphXML(t, doc), "", ``+info+``, 1) - } - return parse(t, []byte(wire)) -} - -func graphWrap(t *testing.T, secret, kek []byte, name, carried, id string, refs []xmlenc.KeyGraphReference) *document.Document { - t.Helper() - doc, err := xmlenc.EncryptKey(t.Context(), secret, xmlenc.KeyGraphKey{Name: name, Algorithm: xmlenc.AES256KeyWrap, Secret: kek}, xmlenc.EncryptedKeyOptions{Algorithm: xmlenc.AES256KeyWrap, ID: id, CarriedKeyName: carried, References: refs}) - if err != nil { - t.Fatal(err) - } - return doc -} - -func graphAssert(t *testing.T, doc *document.Document, selected document.Element, options xmlenc.KeyGraphOptions) { - t.Helper() - before := graphXML(t, doc) - got, err := xmlenc.DecryptBytesWithKeyGraph(t.Context(), doc, selected, options) - if err != nil || string(got) != "graph payload\x00\xff" { - t.Fatalf("graph plaintext %q: %v", got, err) - } - if graphXML(t, doc) != before { - t.Fatal("graph changed source snapshot") - } -} - func TestKeyGraphRelationships(t *testing.T) { secret, kek := bytes.Repeat([]byte{0x52}, 32), bytes.Repeat([]byte{0x93}, 32) options := xmlenc.KeyGraphOptions{Keys: []xmlenc.KeyGraphKey{{Name: "master", Algorithm: xmlenc.AES256KeyWrap, Secret: kek}}} @@ -1013,7 +1027,7 @@ func TestKeyGraphAuthenticatedPlaintextGrammar(t *testing.T) { if content && !handle.Same(restored.Root()) || !content && !handle.Same(restored.Root().Children()[1]) { t.Fatal("returned handle lost the restored snapshot") } - } else if restored != nil || handle.Valid() || !errors.Is(err, xmlenc.ErrDecrypt) { + } else if restored != nil || handle != (document.Element{}) || !errors.Is(err, xmlenc.ErrDecrypt) { t.Fatal("invalid decrypted XML returned a partial snapshot", content, restored != nil, handle.Valid(), err) } if graphXML(t, container) != before { @@ -1540,3 +1554,75 @@ func BenchmarkExternalCiphertext(b *testing.B) { }) } } + +func externalOptions(uri string, body []byte) xmlenc.Options { + return xmlenc.Options{External: &xmlenc.ExternalCipherReference{ + URI: uri, + Resolver: xmlenc.CipherResolverFunc(func(context.Context, string) (io.ReadCloser, error) { + return io.NopCloser(bytes.NewReader(body)), nil + }), + }} +} + +func decryptReader(t *testing.T, source io.ReadCloser) ([]byte, error) { + t.Helper() + + key := make([]byte, 32) + + encrypted, err := xmlenc.EncryptBytesWithKey(t.Context(), []byte("secret"), key, xmlenc.SymmetricOptions{}) + if err != nil { + t.Fatal(err) + } + + const uri = "urn:review:ciphertext" + + external, _, _, err := xmlenc.ExternalizeCiphertext(t.Context(), encrypted, encrypted.Root(), uri) + if err != nil { + t.Fatal(err) + } + + return xmlenc.DecryptBytesWithKey(t.Context(), external, key, xmlenc.SymmetricOptions{Data: xmlenc.Options{External: &xmlenc.ExternalCipherReference{URI: uri, Resolver: xmlenc.CipherResolverFunc(func(context.Context, string) (io.ReadCloser, error) { return source, nil })}}}) +} + +func graphXML(t *testing.T, doc *document.Document) string { + t.Helper() + wire, err := doc.XML(t.Context()) + if err != nil { + t.Fatal(err) + } + return string(wire) +} + +func graphData(t *testing.T, secret []byte, info string) *document.Document { + t.Helper() + doc, err := xmlenc.EncryptBytesWithKey(t.Context(), []byte("graph payload\x00\xff"), secret, xmlenc.SymmetricOptions{}) + if err != nil { + t.Fatal(err) + } + wire := strings.Replace(graphXML(t, doc), "", ``+info+``, 1) + } + return parse(t, []byte(wire)) +} + +func graphWrap(t *testing.T, secret, kek []byte, name, carried, id string, refs []xmlenc.KeyGraphReference) *document.Document { + t.Helper() + doc, err := xmlenc.EncryptKey(t.Context(), secret, xmlenc.KeyGraphKey{Name: name, Algorithm: xmlenc.AES256KeyWrap, Secret: kek}, xmlenc.EncryptedKeyOptions{Algorithm: xmlenc.AES256KeyWrap, ID: id, CarriedKeyName: carried, References: refs}) + if err != nil { + t.Fatal(err) + } + return doc +} + +func graphAssert(t *testing.T, doc *document.Document, selected document.Element, options xmlenc.KeyGraphOptions) { + t.Helper() + before := graphXML(t, doc) + got, err := xmlenc.DecryptBytesWithKeyGraph(t.Context(), doc, selected, options) + if err != nil || string(got) != "graph payload\x00\xff" { + t.Fatalf("graph plaintext %q: %v", got, err) + } + if graphXML(t, doc) != before { + t.Fatal("graph changed source snapshot") + } +} diff --git a/xmlenc/replace.go b/xmlenc/replace.go index 88923a9..7933a3b 100644 --- a/xmlenc/replace.go +++ b/xmlenc/replace.go @@ -96,7 +96,7 @@ func selectedNode(doc *document.Document, selected document.Element) (*tree.Node // XMLENC 1.1 section 2.1.5 permits super-encryption of complete structures; // inserting EncryptedData into one of their descendants is outside this profile. for parent := node.Parent; parent != nil; parent = parent.Parent { - if parent.Name.Space == Namespace && (parent.Name.Local == "EncryptedData" || parent.Name.Local == "EncryptedKey") { + if parent.Name.Space == Namespace && (parent.Name.Local == nameEncryptedData || parent.Name.Local == nameEncryptedKey) { return nil, ErrFormat } } diff --git a/xmlenc/replace_test.go b/xmlenc/replace_test.go index a8f23d7..d5aabb2 100644 --- a/xmlenc/replace_test.go +++ b/xmlenc/replace_test.go @@ -188,7 +188,7 @@ func TestSelectedElementSuperEncryption(t *testing.T) { before := xmlBytes(t, inner) result, handle, err := xmlenc.EncryptElementWithKeyWrap(ctx, inner, selected, kek, xmlenc.KeyWrapOptions{}) - if result != nil || handle.Valid() || !errors.Is(err, xmlenc.ErrFormat) || !bytes.Equal(before, xmlBytes(t, inner)) { + if result != nil || handle != (document.Element{}) || !errors.Is(err, xmlenc.ErrFormat) || !bytes.Equal(before, xmlBytes(t, inner)) { t.Fatal("partial super-encryption accepted or source changed", err) } } @@ -298,7 +298,7 @@ func TestSelectedElementAuthenticatedXMLAndLimits(t *testing.T) { t.Fatal(err) } - if result, handle, err := xmlenc.DecryptElementWithKeyWrap(ctx, bounded, bounded.Root().Children()[0], kek, xmlenc.KeyWrapOptions{}); result != nil || handle.Valid() || !errors.Is(err, xmlenc.ErrDecrypt) || !bytes.Equal(wire, xmlBytes(t, bounded)) { + if result, handle, err := xmlenc.DecryptElementWithKeyWrap(ctx, bounded, bounded.Root().Children()[0], kek, xmlenc.KeyWrapOptions{}); result != nil || handle != (document.Element{}) || !errors.Is(err, xmlenc.ErrDecrypt) || !bytes.Equal(wire, xmlBytes(t, bounded)) { t.Fatal("complete document byte budget bypassed or source changed", err) } } @@ -331,11 +331,11 @@ func TestSelectedElementRejectsHandlesTamperingAndCancellation(t *testing.T) { foreign := parse(t, []byte(`foreign`)) for _, selected := range []document.Element{{}, foreign.Root(), foreign.Root().Children()[0]} { - if result, handle, err := xmlenc.EncryptElementWithKeyWrap(ctx, source, selected, kek, xmlenc.KeyWrapOptions{}); result != nil || handle.Valid() || !errors.Is(err, xmlenc.ErrFormat) { + if result, handle, err := xmlenc.EncryptElementWithKeyWrap(ctx, source, selected, kek, xmlenc.KeyWrapOptions{}); result != nil || handle != (document.Element{}) || !errors.Is(err, xmlenc.ErrFormat) { t.Fatal("foreign selection accepted", err) } - if result, handle, err := xmlenc.DecryptElementWithKeyWrap(ctx, source, selected, kek, xmlenc.KeyWrapOptions{}); result != nil || handle.Valid() || !errors.Is(err, xmlenc.ErrFormat) { + if result, handle, err := xmlenc.DecryptElementWithKeyWrap(ctx, source, selected, kek, xmlenc.KeyWrapOptions{}); result != nil || handle != (document.Element{}) || !errors.Is(err, xmlenc.ErrFormat) { t.Fatal("foreign decryption selection accepted", err) } } @@ -429,7 +429,7 @@ func FuzzSelectedElementPlaintext(f *testing.F) { } if err != nil { - if result != nil || handle.Valid() || !errors.Is(err, xmlenc.ErrDecrypt) { + if result != nil || handle != (document.Element{}) || !errors.Is(err, xmlenc.ErrDecrypt) { t.Fatal("error exposed a partial result", err) } diff --git a/xmlenc/rsa15_test.go b/xmlenc/rsa15_test.go new file mode 100644 index 0000000..8fe9884 --- /dev/null +++ b/xmlenc/rsa15_test.go @@ -0,0 +1,73 @@ +package xmlenc_test + +import ( + "bytes" + "crypto" + "crypto/fips140" + "crypto/rand" + "crypto/rsa" + "encoding/base64" + "errors" + "io" + "testing" + + "github.com/skarm/xmlsec/xmlenc" +) + +func TestRSA15ExplicitPolicyAndSessionKey(t *testing.T) { + if fips140.Enabled() { + t.Skip("legacy RSA transport is disabled in FIPS mode") + } + k := key(t) + options := xmlenc.Options{KeyTransportAlgorithm: xmlenc.RSA15, AllowLegacy: true} + d, err := xmlenc.EncryptBytes(t.Context(), []byte("legacy recipient"), &k.PublicKey, options) + if err != nil { + t.Fatal(err) + } + raw, err := d.XML(t.Context()) + if err != nil { + t.Fatal(err) + } + wrappedValue := d.Root().Children()[1].Children()[0].Children()[1].Children()[0].Text() + wrapped, err := base64.StdEncoding.DecodeString(wrappedValue) + if err != nil { + t.Fatal(err) + } + // Decode the transport with the standard library, independently of XMLENC. + secret, err := rsa.DecryptPKCS1v15(rand.Reader, k, wrapped) //nolint:staticcheck // Independent decoding of locally generated known-valid legacy test ciphertext. + if err != nil || len(secret) != 32 { + t.Fatalf("PKCS#1 v1.5 session key: %v", err) + } + clear(secret) + provider := &rsa15Provider{PrivateKey: k} + plain, err := xmlenc.DecryptBytes(t.Context(), d, provider, options) + if err != nil || string(plain) != "legacy recipient" || provider.sessionBytes != 32 { + t.Fatalf("session-key provider contract: %q %v (%d)", plain, err, provider.sessionBytes) + } + for _, policy := range []xmlenc.Options{{}, {AllowLegacy: true}, {KeyTransportAlgorithm: xmlenc.RSA15}, {KeyTransportAlgorithm: xmlenc.RSA15, AllowLegacy: true, OAEP: &xmlenc.OAEPParameters{}}} { + if out, err := xmlenc.DecryptBytes(t.Context(), d, provider, policy); out != nil || err == nil { + t.Fatal("RSA15 accepted without exact explicit policy") + } + } + corrupt := bytes.Replace(raw, []byte(wrappedValue), []byte(base64.StdEncoding.EncodeToString(make([]byte, k.Size()))), 1) + if out, err := xmlenc.DecryptBytes(t.Context(), parse(t, corrupt), provider, options); out != nil || !errors.Is(err, xmlenc.ErrDecrypt) { + t.Fatalf("bad padding returned plaintext or a padding oracle: %v", err) + } + if _, err := xmlenc.EncryptBytesWithKey(t.Context(), []byte("x"), make([]byte, 32), xmlenc.SymmetricOptions{Data: options}); !errors.Is(err, xmlenc.ErrPolicy) { + t.Fatal("RSA policy silently ignored by direct-key API", err) + } +} + +type rsa15Provider struct { + *rsa.PrivateKey + sessionBytes int +} + +func (p *rsa15Provider) Decrypt(random io.Reader, ciphertext []byte, options crypto.DecrypterOpts) ([]byte, error) { + parameters, ok := options.(*rsa.PKCS1v15DecryptOptions) //nolint:staticcheck // Checks the explicit legacy provider's implicit rejection contract. + if !ok || parameters.SessionKeyLen == 0 { + return nil, errors.New("implicit session-key rejection required") + } + p.sessionBytes = parameters.SessionKeyLen + return p.PrivateKey.Decrypt(random, ciphertext, options) +} diff --git a/xmlenc/suites_test.go b/xmlenc/suites_test.go index 56ff2bd..6b342d3 100644 --- a/xmlenc/suites_test.go +++ b/xmlenc/suites_test.go @@ -79,60 +79,6 @@ func TestKeyWrapSuites(t *testing.T) { } } -func wrapAlgorithms() []algorithmSpec { - return []algorithmSpec{{xmlenc.AES128KeyWrap, 16}, {xmlenc.AES192KeyWrap, 24}, {xmlenc.AES256KeyWrap, 32}} -} - -func testKeyWrapSuitesCase(t *testing.T, keyAlgorithm *algorithmSpec, dataAlgorithm *algorithmSpec, ctx *context.Context, source **document.Document, before *[]byte) { - kek := bytes.Repeat([]byte{0x27}, keyAlgorithm.keyBytes) - originalKEK := bytes.Clone(kek) - - options := xmlenc.KeyWrapOptions{Data: xmlenc.Options{DataAlgorithm: dataAlgorithm.uri}, KeyAlgorithm: keyAlgorithm.uri} - for _, plaintext := range [][]byte{{}, []byte("binary\x00\xff"), bytes.Repeat([]byte{1, 2, 3}, 1000)} { - doc, err := xmlenc.EncryptBytesWithKeyWrap(*ctx, plaintext, kek, options) - if err != nil { - t.Fatal(err) - } - - wire, _ := doc.XML(t.Context()) - doc = parse(t, wire) - - out, err := xmlenc.DecryptBytesWithKeyWrap(*ctx, doc, kek, options) - if err != nil || !bytes.Equal(out, plaintext) { - t.Fatalf("roundtrip: %v", err) - } - - wrapped, _ := base64.StdEncoding.DecodeString(doc.Root().Children()[1].Children()[0].Children()[1].Children()[0].Text()) - if len(wrapped) != dataAlgorithm.keyBytes+8 { - t.Fatal("wrong wrapped length") - } - } - - doc, err := xmlenc.EncryptDocumentWithKeyWrap(*ctx, *source, kek, options) - if err != nil { - t.Fatal(err) - } - - out, err := xmlenc.DecryptDocumentWithKeyWrap(*ctx, doc, kek, options) - if err != nil { - t.Fatal(err) - } - - wire, _ := out.XML(t.Context()) - if bytes.Contains(wire, []byte("outside")) || !bytes.Contains(wire, []byte("")) { - t.Fatalf("element scope: %s", wire) - } - - if out, err := xmlenc.DecryptBytesWithKeyWrap(*ctx, doc, kek, options); out != nil || !errors.Is(err, xmlenc.ErrFormat) { - t.Fatal("type confusion", err) - } - - after, _ := (*source).XML(t.Context()) - if !bytes.Equal(*before, after) || !bytes.Equal(kek, originalKEK) { - t.Fatal("source document or KEK mutated") - } -} - func TestKeyWrapRejectsTamperingAndConfusion(t *testing.T) { ctx := t.Context() kek := bytes.Repeat([]byte{0x42}, 32) @@ -359,27 +305,6 @@ func FuzzDecryptWithKeyWrap(f *testing.F) { }) } -type algorithmSpec struct { - uri string - keyBytes int -} - -func gcmAlgorithms() []algorithmSpec { - return []algorithmSpec{{xmlenc.AES128GCM, 16}, {xmlenc.AES192GCM, 24}, {xmlenc.AES256GCM, 32}} -} - -type failingProvider struct { - public *rsa.PublicKey - result []byte - err error -} - -func (p *failingProvider) Public() crypto.PublicKey { return p.public } - -func (p *failingProvider) Decrypt(_ io.Reader, _ []byte, _ crypto.DecrypterOpts) ([]byte, error) { - return p.result, p.err -} - func TestLegacyCBCKeyTransports(t *testing.T) { if fips140.Enabled() { t.Skip("legacy CBC is forbidden in FIPS mode") @@ -504,25 +429,6 @@ func TestLegacyCBCProviderAndPlaintextLimits(t *testing.T) { } } -func methodKeySizeFixture(t *testing.T, raw []byte, algorithm, content string, last bool) []byte { - t.Helper() - start := []byte(``) - i := bytes.Index(raw, start) - if i < 0 { - t.Fatal("missing EncryptionMethod fixture", algorithm) - } - position := i + len(start) - if last { - end := bytes.Index(raw[position:], []byte(``)) - if end < 0 { - t.Fatal("missing method close") - } - position += end - } - out := append(bytes.Clone(raw[:position]), []byte(content)...) - return append(out, raw[position:]...) -} - func TestEncryptionMethodKeySize(t *testing.T) { algorithms := gcmAlgorithms() if !fips140.Enabled() { @@ -883,3 +789,97 @@ func TestKeyGraphImplicitCanonicalization(t *testing.T) { }) } } + +func wrapAlgorithms() []algorithmSpec { + return []algorithmSpec{{xmlenc.AES128KeyWrap, 16}, {xmlenc.AES192KeyWrap, 24}, {xmlenc.AES256KeyWrap, 32}} +} + +func testKeyWrapSuitesCase(t *testing.T, keyAlgorithm *algorithmSpec, dataAlgorithm *algorithmSpec, ctx *context.Context, source **document.Document, before *[]byte) { + kek := bytes.Repeat([]byte{0x27}, keyAlgorithm.keyBytes) + originalKEK := bytes.Clone(kek) + + options := xmlenc.KeyWrapOptions{Data: xmlenc.Options{DataAlgorithm: dataAlgorithm.uri}, KeyAlgorithm: keyAlgorithm.uri} + for _, plaintext := range [][]byte{{}, []byte("binary\x00\xff"), bytes.Repeat([]byte{1, 2, 3}, 1000)} { + doc, err := xmlenc.EncryptBytesWithKeyWrap(*ctx, plaintext, kek, options) + if err != nil { + t.Fatal(err) + } + + wire, _ := doc.XML(t.Context()) + doc = parse(t, wire) + + out, err := xmlenc.DecryptBytesWithKeyWrap(*ctx, doc, kek, options) + if err != nil || !bytes.Equal(out, plaintext) { + t.Fatalf("roundtrip: %v", err) + } + + wrapped, _ := base64.StdEncoding.DecodeString(doc.Root().Children()[1].Children()[0].Children()[1].Children()[0].Text()) + if len(wrapped) != dataAlgorithm.keyBytes+8 { + t.Fatal("wrong wrapped length") + } + } + + doc, err := xmlenc.EncryptDocumentWithKeyWrap(*ctx, *source, kek, options) + if err != nil { + t.Fatal(err) + } + + out, err := xmlenc.DecryptDocumentWithKeyWrap(*ctx, doc, kek, options) + if err != nil { + t.Fatal(err) + } + + wire, _ := out.XML(t.Context()) + if bytes.Contains(wire, []byte("outside")) || !bytes.Contains(wire, []byte("")) { + t.Fatalf("element scope: %s", wire) + } + + if out, err := xmlenc.DecryptBytesWithKeyWrap(*ctx, doc, kek, options); out != nil || !errors.Is(err, xmlenc.ErrFormat) { + t.Fatal("type confusion", err) + } + + after, _ := (*source).XML(t.Context()) + if !bytes.Equal(*before, after) || !bytes.Equal(kek, originalKEK) { + t.Fatal("source document or KEK mutated") + } +} + +type algorithmSpec struct { + uri string + keyBytes int +} + +func gcmAlgorithms() []algorithmSpec { + return []algorithmSpec{{xmlenc.AES128GCM, 16}, {xmlenc.AES192GCM, 24}, {xmlenc.AES256GCM, 32}} +} + +type failingProvider struct { + public *rsa.PublicKey + result []byte + err error +} + +func (p *failingProvider) Public() crypto.PublicKey { return p.public } + +func (p *failingProvider) Decrypt(_ io.Reader, _ []byte, _ crypto.DecrypterOpts) ([]byte, error) { + return p.result, p.err +} + +func methodKeySizeFixture(t *testing.T, raw []byte, algorithm, content string, last bool) []byte { + t.Helper() + start := []byte(``) + i := bytes.Index(raw, start) + if i < 0 { + t.Fatal("missing EncryptionMethod fixture", algorithm) + } + position := i + len(start) + if last { + end := bytes.Index(raw[position:], []byte(``)) + if end < 0 { + t.Fatal("missing method close") + } + position += end + } + out := append(bytes.Clone(raw[:position]), []byte(content)...) + return append(out, raw[position:]...) +} diff --git a/xmlenc/symmetric.go b/xmlenc/symmetric.go index 7478ebc..8f14938 100644 --- a/xmlenc/symmetric.go +++ b/xmlenc/symmetric.go @@ -9,6 +9,15 @@ import ( "github.com/skarm/xmlsec/internal/lexicon" ) +// Scalar boundaries shared by symmetric and agreement KeyName profiles. KeyName +// remains an untrusted lookup hint; these checks do not authenticate a key. +const ( + minimumKeyNameRune = 0x20 + keyNameDeleteRune = 0x7f + keyNameNoncharacterFFFE = 0xfffe + keyNameNoncharacterFFFF = 0xffff +) + // EncryptBytesWithKey encrypts bytes with an explicitly supplied symmetric data key. // GCM creates a fresh random 96-bit nonce and a 128-bit tag. Explicit legacy // CBC uses a fresh IV and XMLENC padding, without authentication. No EncryptedKey @@ -124,7 +133,7 @@ func DecryptContentWithKey(ctx context.Context, doc *document.Document, selected } func symmetricProfile(ctx context.Context, key []byte, options SymmetricOptions) (profile, error) { - if options.Data.OAEP != nil { + if options.Data.OAEP != nil || options.Data.KeyTransportAlgorithm != "" { return profile{}, ErrPolicy } @@ -133,12 +142,12 @@ func symmetricProfile(ctx context.Context, key []byte, options SymmetricOptions) return profile{}, err } - if len(key) != p.keyBytes || len(options.KeyName) > 1024 || !utf8.ValidString(options.KeyName) || strings.TrimSpace(options.KeyName) != options.KeyName { + if len(key) != p.keyBytes || len(options.KeyName) > maximumKeyNameBytes || !utf8.ValidString(options.KeyName) || strings.TrimSpace(options.KeyName) != options.KeyName { return profile{}, ErrPolicy } for _, r := range options.KeyName { - if r < 0x20 || r == 0x7f || r == 0xfffe || r == 0xffff { + if r < minimumKeyNameRune || r == keyNameDeleteRune || r == keyNameNoncharacterFFFE || r == keyNameNoncharacterFFFF { return profile{}, ErrPolicy } } @@ -156,19 +165,19 @@ func symmetricProfile(ctx context.Context, key []byte, options SymmetricOptions) } func decodeDirectProfile(ctx context.Context, root document.Element, p profile) ([]byte, []byte, error) { - names := []document.Name{elementName(Namespace, "EncryptionMethod")} + names := []document.Name{elementName(Namespace, nameEncryptionMethod)} if p.keyName != "" { names = append(names, elementName(SignatureNamespace, lexicon.DSigElementKeyInfo)) } - names = append(names, elementName(Namespace, "CipherData")) + names = append(names, elementName(Namespace, nameCipherData)) children, err := childElements(root, names...) if err != nil { return nil, nil, ErrFormat } - if err := checkEncryptionMethod(ctx, children[0], p.options.DataAlgorithm, p.keyBytes*8, false); err != nil { + if err := checkEncryptionMethod(ctx, children[0], p.options.DataAlgorithm, p.keyBytes*keyOctetBits, false); err != nil { return nil, nil, err } diff --git a/xmlenc/symmetric_test.go b/xmlenc/symmetric_test.go index dbf597a..a2733fb 100644 --- a/xmlenc/symmetric_test.go +++ b/xmlenc/symmetric_test.go @@ -226,25 +226,6 @@ func FuzzDecryptWithKey(f *testing.F) { }) } -func cbcAlgorithms() []algorithmSpec { - return []algorithmSpec{{xmlenc.AES128CBC, 16}, {xmlenc.AES192CBC, 24}, {xmlenc.AES256CBC, 32}, {xmlenc.TripleDESCBC, 24}} -} - -func legacyBlock(t *testing.T, algorithm string, secret []byte) cipher.Block { - t.Helper() - var block cipher.Block - var err error - if algorithm == xmlenc.TripleDESCBC { - block, err = des.NewTripleDESCipher(secret) - } else { - block, err = aes.NewCipher(secret) - } - if err != nil { - t.Fatal(err) - } - return block -} - func TestLegacyCBCDataAlgorithms(t *testing.T) { for _, algorithm := range cbcAlgorithms() { t.Run(algorithm.uri, func(t *testing.T) { @@ -397,3 +378,22 @@ func TestLegacyCBCElementContentAndReferences(t *testing.T) { }) } } + +func cbcAlgorithms() []algorithmSpec { + return []algorithmSpec{{xmlenc.AES128CBC, 16}, {xmlenc.AES192CBC, 24}, {xmlenc.AES256CBC, 32}, {xmlenc.TripleDESCBC, 24}} +} + +func legacyBlock(t *testing.T, algorithm string, secret []byte) cipher.Block { + t.Helper() + var block cipher.Block + var err error + if algorithm == xmlenc.TripleDESCBC { + block, err = des.NewTripleDESCipher(secret) + } else { + block, err = aes.NewCipher(secret) + } + if err != nil { + t.Fatal(err) + } + return block +} diff --git a/xmlenc/xmlenc.go b/xmlenc/xmlenc.go index 96a857f..078ea53 100644 --- a/xmlenc/xmlenc.go +++ b/xmlenc/xmlenc.go @@ -41,6 +41,8 @@ const ( RSAOAEP = Namespace11 + "rsa-oaep" // RSAOAEP10 is the legacy XML Encryption RSA-OAEP URI with fixed MGF1-SHA1. RSAOAEP10 = Namespace + "rsa-oaep-mgf1p" + // RSA15 is legacy RSA PKCS#1 v1.5 key transport. It requires AllowLegacy. + RSA15 = Namespace + "rsa-1_5" // MGF1SHA1 is the MGF1-SHA1 mask-generation URI, requiring AllowLegacy. MGF1SHA1 = Namespace11 + "mgf1sha1" // MGF1SHA224 is the MGF1-SHA224 mask-generation URI. @@ -83,7 +85,8 @@ var ( // the selected cipher, even when another cipher is supported. An optional // EncryptionMethod/KeySize must match the data key, wrapping key or RSA modulus. type Options struct { - // AllowLegacy explicitly permits CBC data encryption and TripleDES key wrap. + // AllowLegacy explicitly permits CBC data encryption, TripleDES key wrap + // and explicitly selected RSA15 key transport. // CBC plaintext is unauthenticated. Callers must establish any required // ciphertext integrity before decryption and keep legacy keys separate from // GCM keys. This flag changes no algorithm choice and grants no trust. @@ -93,6 +96,12 @@ type Options struct { // DataAlgorithm selects the expected data cipher; empty selects AES256GCM. // Decryption rejects ciphertext declaring a different algorithm. DataAlgorithm string + // KeyTransportAlgorithm selects RSA15 explicitly for legacy interoperability. + // Empty uses RSA-OAEP with OAEP parameters. RSA15 requires AllowLegacy, + // rejects OAEP parameters and is disabled in FIPS mode. It must be used + // behind ciphertext authentication to avoid decryption-oracle exposure. + // Only the RSA APIs accept this field; key graphs use their own bindings. + KeyTransportAlgorithm string // OAEP selects RSA-OAEP parameters. Nil uses SHA-256 and an empty label. // These parameters are rejected by direct-key and AES Key Wrap APIs. OAEP *OAEPParameters diff --git a/xmlenc/xmlenc_test.go b/xmlenc/xmlenc_test.go index d1c9400..67af2ea 100644 --- a/xmlenc/xmlenc_test.go +++ b/xmlenc/xmlenc_test.go @@ -235,120 +235,6 @@ func BenchmarkDirectKey(b *testing.B) { ) } -func key(t *testing.T) *rsa.PrivateKey { - t.Helper() - - k, e := rsa.GenerateKey(rand.Reader, 2048) - if e != nil { - t.Fatal(e) - } - - return k -} - -func parse(t *testing.T, b []byte) *document.Document { - t.Helper() - - d, e := document.Parse(t.Context(), b, document.DefaultLimits()) - if e != nil { - t.Fatal(e) - } - - return d -} - -type provider struct { - k *rsa.PrivateKey - want context.Context - calls int - cancel context.CancelFunc -} - -func (p *provider) Public() crypto.PublicKey { return &p.k.PublicKey } - -func (p *provider) Decrypt(ctx context.Context, r io.Reader, c []byte, o crypto.DecrypterOpts) ([]byte, error) { - if ctx != p.want { - return nil, errors.New("context not passed") - } - - p.calls++ - if p.cancel != nil { - p.cancel() - } - - return p.k.Decrypt(r, c, o) -} - -func benchmarkByteEncryption(b *testing.B, encrypt func([]byte) (*document.Document, error), decrypt func(*document.Document) ([]byte, error)) { - b.Helper() - - plain := make([]byte, 4096) - - encrypted, err := encrypt(plain) - if err != nil { - b.Fatal(err) - } - - for _, operation := range []struct { - name string - run func() error - }{ - {"Encrypt", func() error { _, err := encrypt(plain); return err }}, - {"Decrypt", func() error { _, err := decrypt(encrypted); return err }}, - } { - b.Run(operation.name, func(b *testing.B) { - b.ReportAllocs() - - for b.Loop() { - if err := operation.run(); err != nil { - b.Fatal(err) - } - } - }) - } -} - -func agreementChild(t testing.TB, parent document.Element, namespace, local string) document.Element { - t.Helper() - var result document.Element - for _, child := range parent.Children() { - if child.Name() == (document.Name{Space: namespace, Local: local}) { - if result.Valid() { - t.Fatal("duplicate", local) - } - result = child - } - } - if !result.Valid() { - t.Fatal("missing", local) - } - return result -} - -func agreementMethod(t testing.TB, doc *document.Document) document.Element { - t.Helper() - info := agreementChild(t, doc.Root(), xmlenc.SignatureNamespace, "KeyInfo") - wrapped := agreementChild(t, info, xmlenc.Namespace, "EncryptedKey") - info = agreementChild(t, wrapped, xmlenc.SignatureNamespace, "KeyInfo") - return agreementChild(t, info, xmlenc.Namespace, "AgreementMethod") -} - -func agreementPoint(t testing.TB, info document.Element) document.Element { - t.Helper() - value := agreementChild(t, info, xmlenc.SignatureNamespace, "KeyValue") - value = agreementChild(t, value, xmlenc.SignatureNamespace11, "ECKeyValue") - return agreementChild(t, value, xmlenc.SignatureNamespace11, "PublicKey") -} - -func agreementKey(t testing.TB, curve ecdh.Curve) *ecdh.PrivateKey { - t.Helper() - key, err := curve.GenerateKey(rand.Reader) - if err != nil { - t.Fatal(err) - } - return key -} - func TestECDHRoundTripAndWire(t *testing.T) { for _, curve := range []ecdh.Curve{ecdh.P256(), ecdh.P384(), ecdh.P521()} { recipient := agreementKey(t, curve) @@ -475,30 +361,6 @@ func TestECDHRejectsMetadataBeforeProvider(t *testing.T) { } } -type agreementProvider struct { - key *ecdh.PrivateKey - want context.Context - calls int - cancel context.CancelFunc - trim bool -} - -func (p *agreementProvider) PublicKey() *ecdh.PublicKey { return p.key.PublicKey() } -func (p *agreementProvider) ECDH(ctx context.Context, peer *ecdh.PublicKey) ([]byte, error) { - if ctx != p.want { - return nil, errors.New("context not passed") - } - p.calls++ - if p.cancel != nil { - p.cancel() - } - secret, err := p.key.ECDH(peer) - if p.trim && len(secret) > 0 { - secret = secret[1:] - } - return secret, err -} - func TestECDHPolicyAndContext(t *testing.T) { recipient := agreementKey(t, ecdh.P256()) doc, err := xmlenc.EncryptBytesWithECDH(t.Context(), []byte("secret"), recipient.PublicKey(), xmlenc.ECDHOptions{}) @@ -540,22 +402,6 @@ func TestECDHPolicyAndContext(t *testing.T) { } } -func agreementCurveLabel(curve ecdh.Curve) string { return fmt.Sprint(curve) } - -func agreementRecipientXML(t testing.TB, raw []byte, replacement string) []byte { - t.Helper() - start := bytes.Index(raw, []byte("")) - if end < 0 { - t.Fatal("recipient close absent") - } - end += start + len("") - return append(append(append([]byte{}, raw[:start]...), []byte(replacement)...), raw[end:]...) -} - func TestECDHLeadingZeroSecret(t *testing.T) { scalar := make([]byte, 32) scalar[31] = 1 @@ -747,3 +593,157 @@ func TestECDHLegacyDataPolicy(t *testing.T) { }) } } + +func key(t *testing.T) *rsa.PrivateKey { + t.Helper() + + k, e := rsa.GenerateKey(rand.Reader, 2048) + if e != nil { + t.Fatal(e) + } + + return k +} + +func parse(t *testing.T, b []byte) *document.Document { + t.Helper() + + d, e := document.Parse(t.Context(), b, document.DefaultLimits()) + if e != nil { + t.Fatal(e) + } + + return d +} + +type provider struct { + k *rsa.PrivateKey + want context.Context + calls int + cancel context.CancelFunc +} + +func (p *provider) Public() crypto.PublicKey { return &p.k.PublicKey } + +func (p *provider) Decrypt(ctx context.Context, r io.Reader, c []byte, o crypto.DecrypterOpts) ([]byte, error) { + if ctx != p.want { + return nil, errors.New("context not passed") + } + + p.calls++ + if p.cancel != nil { + p.cancel() + } + + return p.k.Decrypt(r, c, o) +} + +func benchmarkByteEncryption(b *testing.B, encrypt func([]byte) (*document.Document, error), decrypt func(*document.Document) ([]byte, error)) { + b.Helper() + + plain := make([]byte, 4096) + + encrypted, err := encrypt(plain) + if err != nil { + b.Fatal(err) + } + + for _, operation := range []struct { + name string + run func() error + }{ + {"Encrypt", func() error { _, err := encrypt(plain); return err }}, + {"Decrypt", func() error { _, err := decrypt(encrypted); return err }}, + } { + b.Run(operation.name, func(b *testing.B) { + b.ReportAllocs() + + for b.Loop() { + if err := operation.run(); err != nil { + b.Fatal(err) + } + } + }) + } +} + +func agreementChild(t testing.TB, parent document.Element, namespace, local string) document.Element { + t.Helper() + var result document.Element + for _, child := range parent.Children() { + if child.Name() == (document.Name{Space: namespace, Local: local}) { + if result.Valid() { + t.Fatal("duplicate", local) + } + result = child + } + } + if !result.Valid() { + t.Fatal("missing", local) + } + return result +} + +func agreementMethod(t testing.TB, doc *document.Document) document.Element { + t.Helper() + info := agreementChild(t, doc.Root(), xmlenc.SignatureNamespace, "KeyInfo") + wrapped := agreementChild(t, info, xmlenc.Namespace, "EncryptedKey") + info = agreementChild(t, wrapped, xmlenc.SignatureNamespace, "KeyInfo") + return agreementChild(t, info, xmlenc.Namespace, "AgreementMethod") +} + +func agreementPoint(t testing.TB, info document.Element) document.Element { + t.Helper() + value := agreementChild(t, info, xmlenc.SignatureNamespace, "KeyValue") + value = agreementChild(t, value, xmlenc.SignatureNamespace11, "ECKeyValue") + return agreementChild(t, value, xmlenc.SignatureNamespace11, "PublicKey") +} + +func agreementKey(t testing.TB, curve ecdh.Curve) *ecdh.PrivateKey { + t.Helper() + key, err := curve.GenerateKey(rand.Reader) + if err != nil { + t.Fatal(err) + } + return key +} + +type agreementProvider struct { + key *ecdh.PrivateKey + want context.Context + calls int + cancel context.CancelFunc + trim bool +} + +func (p *agreementProvider) PublicKey() *ecdh.PublicKey { return p.key.PublicKey() } +func (p *agreementProvider) ECDH(ctx context.Context, peer *ecdh.PublicKey) ([]byte, error) { + if ctx != p.want { + return nil, errors.New("context not passed") + } + p.calls++ + if p.cancel != nil { + p.cancel() + } + secret, err := p.key.ECDH(peer) + if p.trim && len(secret) > 0 { + secret = secret[1:] + } + return secret, err +} + +func agreementCurveLabel(curve ecdh.Curve) string { return fmt.Sprint(curve) } + +func agreementRecipientXML(t testing.TB, raw []byte, replacement string) []byte { + t.Helper() + start := bytes.Index(raw, []byte("")) + if end < 0 { + t.Fatal("recipient close absent") + } + end += start + len("") + return append(append(append([]byte{}, raw[:start]...), []byte(replacement)...), raw[end:]...) +} From e37974bef622ef23ce30355cb8c3ededdb06a591 Mon Sep 17 00:00:00 2001 From: Skarm Date: Fri, 6 Nov 2026 23:36:03 +0500 Subject: [PATCH 2/4] feat: expand SAML protocols and strengthen XML security validation - Add encrypted SAML claims, logout, NameID management, Artifact/SOAP and HoK ECP - Add authority queries, assertion retrieval and NameID Mapping with replay protection - Expose bounded nonvalidating XML parsing and fix XPath/XSLT processing edge cases - Tighten cryptographic and evidence validation; add explicit legacy RSA policies - Expand pinned interoperability checks with OpenSAML, Shibboleth, OpenAM, GSA and DSS - Consolidate shared constants and update documentation and comments --- .gitattributes | 6 ++++++ build/interop-inputs.txt | 4 ++-- docs/language-suites.md | 7 ++++--- tests/cmd/dev/run_test.go | 4 ++-- transform/comparison_v1_test.go | 4 ++-- 5 files changed, 16 insertions(+), 9 deletions(-) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..95dcaa4 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,6 @@ +# Go tooling writes module files with LF on every platform. +go.mod text eol=lf +go.sum text eol=lf + +# Preserve the checksum-pinned Unicode corpus byte for byte. +/tests/testdata/unicode/NormalizationTest-17.0.0.txt -text diff --git a/build/interop-inputs.txt b/build/interop-inputs.txt index 34feb95..a3652b8 100644 --- a/build/interop-inputs.txt +++ b/build/interop-inputs.txt @@ -3,8 +3,8 @@ f269b7b98691a48647e54ca5bce9d1042c0eb5f12a0887879974f418f70baa23 https://repo.maven.apache.org/maven2/org/eclipse/jetty/jetty-home/12.1.14/jetty-home-12.1.14.tar.gz jetty.tar.gz 1c7db9f726df835349c64edd631de0310eca31291100230064eba153f607b0be https://github.com/Saxonica/Saxon-HE/releases/download/SaxonHE12-10/SaxonHE12-10J.zip saxon.zip c52862d33cd5bb34ed20d48a5ff62bc475e4279c16fd13460a19f726d1dcf62f https://repo.maven.apache.org/maven2/net/sf/saxon/Saxon-HE/9.4.0-9/Saxon-HE-9.4.0-9.jar saxon20.jar -1cbcc49ac5ed8942ba58276699254469ca2144e229f68faeeb48f784c1037953 https://www.w3.org/2014/04/xslt-xquery-serialization/schema-for-serialization-parameters.xsd schemas/ser30.xsd -256736a2f4e7923e888047dee757c17bc3ee707e285f00639fd8fb1d962d11b0 https://www.w3.org/2017/01/xslt-xquery-serialization/schema-for-serialization-parameters.xsd schemas/ser31.xsd +1b95a61f1f1ee5905898544542e7b283c7780ed63dd6313010c078183d626242 https://www.w3.org/2014/04/xslt-xquery-serialization/schema-for-serialization-parameters.xsd schemas/ser30.xsd +f191b0dc69513670883c0abae97d1dab76965b8bfe3fb0b08a2df1b85d228cd3 https://www.w3.org/2017/01/xslt-xquery-serialization/schema-for-serialization-parameters.xsd schemas/ser31.xsd 34407695050d165be14931c0f378f832d24abe3de682a069d2d6121f07661ec3 https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/schema-for-json.xsd schemas/json.xsd c5301a890b0265a705f0806269aa37345aa47d7245a78900b941af65290b16fa https://www.w3.org/TR/2013/REC-xmlenc-core1-20130411/xenc-schema.xsd xmlenc/xenc-schema.xsd 2b48432f036262345638536601d4ea3224897e2d41901163c04eebf350477958 https://www.w3.org/TR/2013/REC-xmlenc-core1-20130411/xenc-schema-11.xsd xmlenc/xenc-schema-11.xsd diff --git a/docs/language-suites.md b/docs/language-suites.md index a1c64bb..142b9c4 100644 --- a/docs/language-suites.md +++ b/docs/language-suites.md @@ -87,9 +87,10 @@ Prepare both Saxon classpaths, `SERIALIZATION_SCHEMA_DIR`, `JSON_SCHEMA_FILE`, Serialization schemas come from the W3C [3.0](https://www.w3.org/TR/2014/REC-xslt-xquery-serialization-30-20140408/#serparams-schema) and [3.1](https://www.w3.org/TR/2017/REC-xslt-xquery-serialization-31-20170321/#serparams-schema) -Recommendations, with SHA-256 pins -`1cbcc49ac5ed8942ba58276699254469ca2144e229f68faeeb48f784c1037953` and -`256736a2f4e7923e888047dee757c17bc3ee707e285f00639fd8fb1d962d11b0`. +Recommendations, with SHA-256 pins over the downloaded bytes, including the +final newline: +`1b95a61f1f1ee5905898544542e7b283c7780ed63dd6313010c078183d626242` and +`f191b0dc69513670883c0abae97d1dab76965b8bfe3fb0b08a2df1b85d228cd3`. For XPath 3.1, use the [F&O 3.1 Appendix C.2 JSON schema](https://www.w3.org/TR/2017/REC-xpath-functions-31-20170321/schema-for-json.xsd). Its pin is `34407695050d165be14931c0f378f832d24abe3de682a069d2d6121f07661ec3`. The older XSLT 3.0 Appendix B.1 schema has different attribute declarations and diff --git a/tests/cmd/dev/run_test.go b/tests/cmd/dev/run_test.go index 220c09d..45b5fae 100644 --- a/tests/cmd/dev/run_test.go +++ b/tests/cmd/dev/run_test.go @@ -1855,8 +1855,8 @@ func qtResidualSerializationControls(t *testing.T, options suiteOptions, item su version string checksum string }{ - {"30", "1cbcc49ac5ed8942ba58276699254469ca2144e229f68faeeb48f784c1037953"}, - {"31", "256736a2f4e7923e888047dee757c17bc3ee707e285f00639fd8fb1d962d11b0"}, + {"30", "1b95a61f1f1ee5905898544542e7b283c7780ed63dd6313010c078183d626242"}, + {"31", "f191b0dc69513670883c0abae97d1dab76965b8bfe3fb0b08a2df1b85d228cd3"}, } { schema := filepath.Join(residualRequired(t, "SERIALIZATION_SCHEMA_DIR"), "ser"+edition.version+".xsd") data, err := os.ReadFile(schema) diff --git a/transform/comparison_v1_test.go b/transform/comparison_v1_test.go index d70a097..e76f96d 100644 --- a/transform/comparison_v1_test.go +++ b/transform/comparison_v1_test.go @@ -3252,8 +3252,8 @@ func TestCompareSerializationParameterSchemas(t *testing.T) { filename string checksum string }{ - {"30", "ser30.xsd", "1cbcc49ac5ed8942ba58276699254469ca2144e229f68faeeb48f784c1037953"}, - {"31", "ser31.xsd", "256736a2f4e7923e888047dee757c17bc3ee707e285f00639fd8fb1d962d11b0"}, + {"30", "ser30.xsd", "1b95a61f1f1ee5905898544542e7b283c7780ed63dd6313010c078183d626242"}, + {"31", "ser31.xsd", "f191b0dc69513670883c0abae97d1dab76965b8bfe3fb0b08a2df1b85d228cd3"}, } { schema := filepath.Join(directory, edition.filename) data, err := os.ReadFile(schema) From 3469ce31534d62ded7918df8784c95a98962f25f Mon Sep 17 00:00:00 2001 From: Skarm Date: Sat, 7 Nov 2026 01:57:14 +0500 Subject: [PATCH 3/4] feat: expand SAML protocols and strengthen XML security validation - Add encrypted SAML claims, logout, NameID management, Artifact/SOAP and HoK ECP - Add authority queries, assertion retrieval and NameID Mapping with replay protection - Expose bounded nonvalidating XML parsing and fix XPath/XSLT processing edge cases - Tighten cryptographic and evidence validation; add explicit legacy RSA policies - Expand pinned interoperability checks with OpenSAML, Shibboleth, OpenAM, GSA and DSS - Consolidate shared constants and update documentation and comments --- build/interop.Dockerfile | 3 +- build/interop.sh | 26 +++++++++++----- docs/testing.md | 8 +++-- transform/comparison_v1_test.go | 54 +++++++++++++++++++++++++++++++-- 4 files changed, 78 insertions(+), 13 deletions(-) diff --git a/build/interop.Dockerfile b/build/interop.Dockerfile index f34c673..c007a4b 100644 --- a/build/interop.Dockerfile +++ b/build/interop.Dockerfile @@ -51,7 +51,8 @@ ENV PATH="/usr/local/go/bin:${PATH}" \ XADES_SCHEMA_DIR=/opt/inputs/xades \ RFC9879_FILE=/opt/inputs/rfc9879.txt \ XMLSEC_REQUIRE_INTEROP=1 \ - XMLSEC_INTEROP_SHARDS=4 + XMLSEC_INTEROP_SHARDS=2 \ + XMLSEC_INTEROP_PARALLEL=2 RUN apt-get update \ && apt-get install -y --no-install-recommends \ diff --git a/build/interop.sh b/build/interop.sh index 3ee8194..8059000 100644 --- a/build/interop.sh +++ b/build/interop.sh @@ -24,22 +24,34 @@ run() { shards=${XMLSEC_INTEROP_SHARDS:-1} case "$shards" in [1-8]) ;; *) printf 'XMLSEC_INTEROP_SHARDS must be 1..8\n' >&2; exit 1 ;; esac +parallel=${XMLSEC_INTEROP_PARALLEL:-1} +case "$parallel" in [1-8]) ;; *) printf 'XMLSEC_INTEROP_PARALLEL must be 1..8\n' >&2; exit 1 ;; esac names=$("$go_bin" test -tags=interop ./transform -list '^TestCompare') -filters=() -for ((slot=0; slot> "$reports/transform-tests" index=$((index + 1)) done <<< "$names" [[ $index -gt 0 ]] || { printf 'no transform comparisons discovered\n' >&2; exit 1; } -printf 'Running %d transform comparisons in %d isolated processes\n' "$index" "$shards" +printf 'Running %d transform comparisons with %d workers and %d parallel subtests each\n' "$index" "$shards" "$parallel" + +run_transform() { + local name failed=0 + while IFS= read -r name; do + # Claim the next test atomically. Workers take more work as they finish, + # so large test tables cannot leave other workers idle for a whole shard. + mkdir "$reports/claim-$name" 2>/dev/null || continue + # Bound each comparison, rather than the accumulated time of unrelated + # comparisons. Each invocation also has its own complete JSON stream. + run "$reports/$name.jsonl" -tags=interop -count=1 -timeout=45m "-parallel=$parallel" ./transform "-run=^${name}$" || failed=1 + done < "$reports/transform-tests" + return "$failed" +} + pids=() for ((slot=0; slot` + namespaceNodeTypeXML + `` + valueOfExpression(expression) + `` compareSaxonStylesheet(t, t.Context(), e, binary, classpath, body, want) }) } for expression, want := range standardFunctionTypeCases() { t.Run(expression, func(t *testing.T) { + t.Parallel() oracle := want if expression == `base-uri#0 instance of function() as xs:anyURI?` { oracle = nameFalse @@ -516,16 +521,19 @@ func TestCompareSaxonFunctionTypes(t *testing.T) { } for expression, want := range mathFunctionContractCases() { t.Run("math contract "+expression, func(t *testing.T) { + t.Parallel() compareSaxonExpression(t, t.Context(), e, binary, classpath, expression, want) }) } for expression, want := range sequenceConcatenationContractCases() { t.Run("sequence contract "+expression, func(t *testing.T) { + t.Parallel() compareSaxonExpression(t, t.Context(), e, binary, classpath, expression, want) }) } for expression, want := range basicNodeAccessorCases() { t.Run("node accessor "+expression, func(t *testing.T) { + t.Parallel() expression = "let $d := parse-xml('" + basicAccessorXML + "') return " + expression compareSaxonExpression(t, t.Context(), e, binary, classpath, expression, want) }) @@ -533,6 +541,7 @@ func TestCompareSaxonFunctionTypes(t *testing.T) { compareSaxonURICollections(t, binary, classpath) for expression, want := range publishedFunctionArityCases(transform.XPath31) { t.Run("published arity "+expression, func(t *testing.T) { + t.Parallel() oracle := want if expression == "fn:deep-equal((),(),(),())" { // The pinned Saxon also exposes the later four-argument @@ -544,6 +553,7 @@ func TestCompareSaxonFunctionTypes(t *testing.T) { } for expression, want := range sequenceSubtypeCases() { t.Run(expression, func(t *testing.T) { + t.Parallel() oracle := want // REC 3.0/3.1 §2.5.6.1 explicitly makes xs:error and xs:error+ // bottom sequence types. The pinned Saxon misses these table cells. @@ -568,6 +578,7 @@ func TestCompareSaxonFunctionTypes(t *testing.T) { } for expression, want := range exactFloatCastCases() { t.Run(expression, func(t *testing.T) { + t.Parallel() oracle := want // F&O §19.1.2.1 requires the nearest supported float. Saxon // narrows through double and loses the side of these midpoints. @@ -586,6 +597,7 @@ func TestCompareSaxonFunctionTypes(t *testing.T) { for _, version := range []transform.XPathVersion{transform.XPath30, transform.XPath31} { for expression, want := range xpathLexicalCases(version) { t.Run(fmt.Sprintf("lexical %d/%s", version, strconv.QuoteToASCII(expression)), func(t *testing.T) { + t.Parallel() source := sheet(version30, ``+valueOfExpression(expression)+``) compareSaxonCompiler(t, binary, classpath, source, xpathLexicalXML, want, transform.XSLT30) }) @@ -597,12 +609,14 @@ func TestCompareSaxonFunctionTypes(t *testing.T) { for _, cases := range []map[string]string{mapKeyAtomizationCases(), predicateNumericCases(), xpathCoreClauseCases()} { for expression, want := range cases { t.Run(expression, func(t *testing.T) { + t.Parallel() compareSaxonExpression(t, t.Context(), e, binary, classpath, expression, want) }) } } for expression, want := range qnameWhitespaceCases() { t.Run("QName whitespace "+expression, func(t *testing.T) { + t.Parallel() oracle := want if expression == "xs:QName('\u00a0p:x')" { // The value has both invalid lexical syntax and an unbound @@ -614,12 +628,14 @@ func TestCompareSaxonFunctionTypes(t *testing.T) { } for expression, want := range untypedNodeTypeCases() { t.Run("untyped node "+expression, func(t *testing.T) { + t.Parallel() source := `parse-xml('` + untypedNodeTypeXML + `') ! (` + expression + `)` compareSaxonExpression(t, t.Context(), e, binary, classpath, source, want) }) } for expression, want := range xsltFunctionTypeCases() { t.Run(expression, func(t *testing.T) { + t.Parallel() oracle := want switch expression { case `unparsed-entity-uri('x')`: @@ -663,12 +679,14 @@ func compareSaxonURICollections(t *testing.T, binary, classpath string) { "stable view": {`deep-equal(uri-collection($uri),uri-collection($uri))`, nameTrue}, } { t.Run("URI collection "+name, func(t *testing.T) { + t.Parallel() expression := "let $uri := '" + uri + "' return " + test.expression compareSaxonExpression(t, t.Context(), e, binary, classpath, expression, test.want) }) } for _, expression := range xpathSyntaxErrorSources() { t.Run("XPath syntax "+expression, func(t *testing.T) { + t.Parallel() source := sheet(version30, ``+valueOfExpression(expression)+``) compareSaxonCompiler(t, binary, classpath, source, sheet(version30, ""), errCodeXPST0003, transform.XSLT30) }) @@ -677,6 +695,7 @@ func compareSaxonURICollections(t *testing.T, binary, classpath string) { for _, name := range xpathReservedNames(transform.XPath31) { for _, prefixed := range []bool{false, true} { t.Run(fmt.Sprintf("reserved reference/%s/%t", name, prefixed), func(t *testing.T) { + t.Parallel() want := errCodeXPST0003 if prefixed { want = "registered" @@ -690,6 +709,7 @@ func compareSaxonURICollections(t *testing.T, binary, classpath string) { for _, name := range xpathReservedNames(transform.XPath31) { for _, prefixed := range []bool{false, true} { t.Run(fmt.Sprintf("reserved/%s/%t", name, prefixed), func(t *testing.T) { + t.Parallel() want := errCodeXPST0003 if prefixed { want = "registered" @@ -904,6 +924,7 @@ func TestCompareSaxonSequenceConstructorContracts(t *testing.T) { } func TestCompareSaxon20CompilerContracts(t *testing.T) { + t.Parallel() classpath := os.Getenv("SAXON20_CLASSPATH") if classpath == "" { t.Skip("SAXON20_CLASSPATH is not configured") @@ -1211,6 +1232,7 @@ func TestCompareSaxon20CompilerContracts(t *testing.T) { } for _, test := range tests { t.Run(test.name, func(t *testing.T) { + t.Parallel() oracle := test.want if test.name == "text implicit invalid XML character" || test.name == "text detected UTF8 malformed" { // The old processor groups no-argument decoding failures under1200, @@ -1310,6 +1332,7 @@ func TestCompareSaxonResidualEditionProofs(t *testing.T) { } func TestCompareSaxonConditionalCompilation(t *testing.T) { + t.Parallel() classpath, binary := saxonRuntime(t) // REC 9.9 excludes a global binding from its own declaration. The pinned // Saxon CLI diagnoses the same condition without exposing a standard code. @@ -1317,6 +1340,7 @@ func TestCompareSaxonConditionalCompilation(t *testing.T) { for _, test := range conditionalCompilationCases() { t.Run(test.name, func(t *testing.T) { + t.Parallel() oracle := test.want30 if test.name == "shadow type namespace" { // Saxon evaluates this condition before its computed type namespace. @@ -1328,6 +1352,7 @@ func TestCompareSaxonConditionalCompilation(t *testing.T) { } for _, test := range staticModuleCompilationCases() { t.Run(test.name, func(t *testing.T) { + t.Parallel() oracle := test.want if test.name == "include embedded simplified xml space preserve " || test.name == "import embedded simplified xml space preserve " { // REC 3.0 §3.11 leaves module construction implementation-defined. @@ -1350,6 +1375,7 @@ func TestCompareSaxonConditionalCompilation(t *testing.T) { } for _, test := range variableScopeCases() { t.Run("variable scope "+test.name, func(t *testing.T) { + t.Parallel() version := version20 if test.modern { version = version30 @@ -1364,6 +1390,7 @@ func TestCompareSaxonConditionalCompilation(t *testing.T) { } for _, test := range compilerClauseCases() { t.Run("clause "+test.name, func(t *testing.T) { + t.Parallel() oracle := test.want switch test.name { case "backwards named foreign reference", "backwards named declared arity": @@ -1377,6 +1404,7 @@ func TestCompareSaxonConditionalCompilation(t *testing.T) { } for _, test := range groupingCopyRegressionCases() { t.Run("typed group copy "+test.name, func(t *testing.T) { + t.Parallel() oracle := test.want switch test.name { case "adjacent numeric predecessor": @@ -1453,11 +1481,13 @@ func TestCompareSaxonModuleUseWhenNamespaces(t *testing.T) { } func TestCompareSaxonHostFunctionContracts(t *testing.T) { + t.Parallel() classpath, binary := saxonRuntime(t) e := engine(t, transform.Config{}) for _, test := range hostFunctionCases() { for _, version := range []string{version20, version30} { t.Run(test.name+version, func(t *testing.T) { + t.Parallel() source := hostFunctionSheet(version, test) expression := `transform(map{'stylesheet-text':'` + strings.ReplaceAll(source, "'", "''") + `','initial-template':QName('','start'),'delivery-format':'serialized'})?output` oracle := test.want30 @@ -1523,6 +1553,7 @@ func TestCompareSaxonHostFunctionContracts(t *testing.T) { } for _, test := range xsltHostNameCases() { t.Run("host EQName "+test.expression, func(t *testing.T) { + t.Parallel() source := sheet(version20, ``+valueOfExpression(test.expression)+``) compareSaxonCompiler(t, binary, classpath, source, sheet(version20, ""), test.want30, transform.XSLT30) }) @@ -1544,13 +1575,16 @@ func TestCompareSaxonNumberPictureDiagnostic(t *testing.T) { } func TestCompareSaxonStaticParameterContracts(t *testing.T) { + t.Parallel() classpath, binary := saxonRuntime(t) - ctx, cancel := context.WithTimeout(t.Context(), time.Minute) - defer cancel() e := engine(t, transform.Config{}) for _, test := range staticParameterCases() { t.Run(test.name, func(t *testing.T) { + t.Parallel() + ctx, cancel := context.WithTimeout(t.Context(), time.Minute) + defer cancel() + expression := strings.ReplaceAll(staticParameterExpression(test.argument), "$stylesheet", "'"+strings.ReplaceAll(staticParameterSheet(test), "'", "''")+"'") oracle := test.want switch test.name { @@ -1577,6 +1611,10 @@ func TestCompareSaxonStaticParameterContracts(t *testing.T) { } for _, test := range staticVariableCases() { t.Run("variable/"+test.name, func(t *testing.T) { + t.Parallel() + ctx, cancel := context.WithTimeout(t.Context(), time.Minute) + defer cancel() + expression := strings.ReplaceAll(staticParameterExpression(""), "$stylesheet", "'"+strings.ReplaceAll(staticVariableSheet(test), "'", "''")+"'") oracle := test.want switch test.name { @@ -3844,6 +3882,7 @@ func TestCompareSerializationRawHTMLDescendants(t *testing.T) { } func TestCompareSerializationVoidElementTables(t *testing.T) { + t.Parallel() classpath, binary := saxonRuntime(t) for _, edition := range []string{"20", "30", "31"} { selectedClasspath := classpath @@ -3861,6 +3900,7 @@ func TestCompareSerializationVoidElementTables(t *testing.T) { for _, name := range serializationVoidElementNames() { for _, nullNamespace := range serializationVoidNamespaceCases(method, htmlVersion, name) { t.Run("SER"+edition+"/"+method+"/HTML"+htmlVersion+"/"+serializationVoidLabel(name, nullNamespace), func(t *testing.T) { + t.Parallel() vector := serializationVoidVector(edition, method, htmlVersion, name, nullNamespace) source := markupSerializationSheet(vector, false) if edition == "20" { @@ -3880,6 +3920,7 @@ func TestCompareSerializationVoidElementTables(t *testing.T) { } func TestCompareSerializationBooleanAttributeTables(t *testing.T) { + t.Parallel() classpath, binary := saxonRuntime(t) for _, edition := range []string{"20", "30", "31"} { selectedClasspath := classpath @@ -3895,6 +3936,7 @@ func TestCompareSerializationBooleanAttributeTables(t *testing.T) { } for _, cell := range publishedSerializationBooleanCells() { t.Run("SER"+edition+"/HTML"+htmlVersion+"/"+cell.element+"/"+cell.attribute, func(t *testing.T) { + t.Parallel() vector := serializationBooleanCellVector(htmlVersion, cell, strings.ToUpper(cell.attribute)) var body strings.Builder body.WriteString(strings.TrimSuffix(strings.TrimPrefix(vector.body, ""), "")) @@ -3954,6 +3996,7 @@ func TestCompareSerializationContainerParameters(t *testing.T) { } func TestCompareSerializationSelectedEditions(t *testing.T) { + t.Parallel() classpath, binary := saxonRuntime(t) for _, edition := range []string{"20", "30"} { selectedClasspath := classpath @@ -3967,7 +4010,10 @@ func TestCompareSerializationSelectedEditions(t *testing.T) { if !serializationVectorAdmitted(edition, test) || !serializationSelectedOracleAdmitted(test) { continue } - t.Run("SER"+edition+"/"+test.name, func(t *testing.T) { compareSelectedSerializationVector(t, binary, selectedClasspath, edition, test) }) + t.Run("SER"+edition+"/"+test.name, func(t *testing.T) { + t.Parallel() + compareSelectedSerializationVector(t, binary, selectedClasspath, edition, test) + }) } } } @@ -4039,6 +4085,7 @@ func serializationReviewedOracleDiagnostic(name string) bool { } func TestCompareSerializationURIAttributeTables(t *testing.T) { + t.Parallel() classpath, binary := saxonRuntime(t) oldClasspath := os.Getenv("SAXON20_CLASSPATH") if oldClasspath == "" { @@ -4052,6 +4099,7 @@ func TestCompareSerializationURIAttributeTables(t *testing.T) { for _, method := range []string{"html", "xhtml"} { for _, cell := range publishedSerializationURICells() { t.Run("SER"+edition+"/"+method+"/"+cell.element+"/"+cell.attribute, func(t *testing.T) { + t.Parallel() compareSerializationURICell(t, binary, selectedClasspath, edition, method, cell) }) } From 8e62dbaa43f4604f1bc1a94255e7ef979d9daeb2 Mon Sep 17 00:00:00 2001 From: Skarm Date: Sat, 7 Nov 2026 03:29:18 +0500 Subject: [PATCH 4/4] feat: expand SAML protocols and strengthen XML security validation - Add encrypted SAML claims, logout, NameID management, Artifact/SOAP and HoK ECP - Add authority queries, assertion retrieval and NameID Mapping with replay protection - Expose bounded nonvalidating XML parsing and fix XPath/XSLT processing edge cases - Tighten cryptographic and evidence validation; add explicit legacy RSA policies - Expand pinned interoperability checks with OpenSAML, Shibboleth, OpenAM, GSA and DSS - Consolidate shared constants and update documentation and comments --- .github/workflows/ci.yml | 16 ++ docs/testing.md | 16 +- tests/cmd/dev/interop_test.go | 28 +++ tests/cmd/dev/suites.go | 2 +- transform/comparison_v1_test.go | 43 ++-- transform/saxon_worker_test.go | 340 ++++++++++++++++++++++++++++ transform/testdata/SaxonWorker.java | 75 ++++++ 7 files changed, 494 insertions(+), 26 deletions(-) create mode 100644 transform/saxon_worker_test.go create mode 100644 transform/testdata/SaxonWorker.java diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 47c26d2..1ec8a68 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,6 +2,7 @@ name: check on: push: + branches: [master] pull_request: merge_group: workflow_dispatch: @@ -82,5 +83,20 @@ jobs: uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false + - name: Restore pinned interop inputs + id: interop-inputs + uses: actions/cache/restore@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3 + with: + path: /tmp/xmlsec-interop-inputs + key: ${{ runner.os }}-interop-inputs-v1-${{ hashFiles('build/interop-inputs.txt') }} + restore-keys: ${{ runner.os }}-interop-inputs-v1- + - name: Prepare and verify pinned interop inputs + run: sh build/prepare-interop.sh /tmp/xmlsec-interop-inputs + - name: Save verified interop inputs + if: steps.interop-inputs.outputs.cache-hit != 'true' + uses: actions/cache/save@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3 + with: + path: /tmp/xmlsec-interop-inputs + key: ${{ steps.interop-inputs.outputs.cache-primary-key }} - name: Run required external checks and Shibboleth/OpenAM/GSA SOAP servers run: make interop-docker diff --git a/docs/testing.md b/docs/testing.md index 0b91250..9a9159c 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -55,7 +55,10 @@ The [image](../build/interop.Dockerfile) supplies native tools, Saxon, released Shibboleth/Jetty, OpenAM/Tomcat, Google GSA and DSS runtimes, and published schemas/vectors. The build step downloads the [pinned inputs](../build/interop-inputs.txt) with retries and verifies SHA-256 on the host and again inside Docker. Override `INTEROP_INPUT_CACHE` to -reuse a prepared cache. System package versions follow Debian repositories. +reuse a prepared cache. CI restores these downloads by the hash of +`build/interop-inputs.txt`, verifies their checksums, and saves the verified cache +before starting the tests. A dependency update can reuse unchanged archives from +the preceding cache. System package versions follow Debian repositories. Runtime networking is disabled except for the container's loopback. The full recipe runs race tests, generates local historical fixtures, starts official DSS @@ -65,8 +68,15 @@ Transform comparisons use two workers in the image (`XMLSEC_INTEROP_SHARDS`, 1–8). Each worker takes the next comparison when free and runs it in an isolated Go process with a 45-minute limit. Independent cases in the largest tables run in parallel, bounded by `XMLSEC_INTEROP_PARALLEL` (1–8, two in the image). -Together these settings allow at most four comparison JVMs at once in the image. -Both settings default to one on a prepared host. +Together these settings allow at most four active comparison JVMs at once in the +image. Saxon 12.10 query and serialization helpers reuse a bounded pool of JVMs +within each comparison. Every request loads isolated Saxon classes and creates a +fresh CLI processor, configuration and document pool, including fresh static +serializer tables; output bytes and failure diagnostics are still checked for +every case. Saxon 9.4 and the specialized compiler invocations retain their +separate processes. Set `XMLSEC_SAXON_PROCESS=1` to use separate processes for all +Saxon requests when comparing behavior or timing. Both parallelism settings +default to one on a prepared host. For a prepared host: diff --git a/tests/cmd/dev/interop_test.go b/tests/cmd/dev/interop_test.go index 1dc82e6..fa857d1 100644 --- a/tests/cmd/dev/interop_test.go +++ b/tests/cmd/dev/interop_test.go @@ -1,10 +1,38 @@ package main import ( + "os/exec" + "path/filepath" "strings" "testing" ) +func TestInteropOracleSourceIdentity(t *testing.T) { + root, err := filepath.EvalSymlinks(t.TempDir()) + if err != nil { + t.Fatal(err) + } + if output, err := exec.CommandContext(t.Context(), "git", "init", "-q", root).CombinedOutput(); err != nil { + t.Fatal(string(output), err) + } + writeFixture(t, root, "go.mod", "module example.test/oracle\n") + writeFixture(t, root, "transform/testdata/SaxonWorker.java", "class SaxonWorker {}\n") + before, err := suiteSourceDigest(t.Context(), root) + if err != nil { + t.Fatal(err) + } + writeFixture(t, root, "transform/testdata/SaxonWorker.java", "class SaxonWorker { int changed; }\n") + after, err := suiteSourceDigest(t.Context(), root) + if err != nil || before == after { + t.Fatal("oracle change retained the old proof source identity", before, after, err) + } + writeFixture(t, root, "oracle-output.log", "disposable run output") + current, err := suiteSourceDigest(t.Context(), root) + if err != nil || current != after { + t.Fatal("disposable logs changed the proof source identity", after, current, err) + } +} + func TestInteropCompletionGate(t *testing.T) { start := `{"Action":"start","Package":"p"}` + "\n" test := `{"Action":"pass","Package":"p","Test":"TestExternal"}` + "\n" diff --git a/tests/cmd/dev/suites.go b/tests/cmd/dev/suites.go index caf8b1c..9b05c9f 100644 --- a/tests/cmd/dev/suites.go +++ b/tests/cmd/dev/suites.go @@ -270,7 +270,7 @@ func runSuites(ctx context.Context, root string, args []string, out, stderr io.W } func suiteSourceDigest(ctx context.Context, root string) (string, error) { - listing, err := suiteGit(ctx, root, "ls-files", "-z", "--cached", "--others", "--exclude-standard", "--", "*.go", "go.mod", "go.sum", "build/w3c-suites.json") + listing, err := suiteGit(ctx, root, "ls-files", "-z", "--cached", "--others", "--exclude-standard", "--", "*.go", "go.mod", "go.sum", "build/w3c-suites.json", "transform/testdata/SaxonWorker.java") if err != nil { return "", err } diff --git a/transform/comparison_v1_test.go b/transform/comparison_v1_test.go index 165a9ef..370c497 100644 --- a/transform/comparison_v1_test.go +++ b/transform/comparison_v1_test.go @@ -2345,8 +2345,8 @@ func TestCompareSaxonDeclarationDiagnostics(t *testing.T) { expression := `transform(map{'stylesheet-text':'` + strings.ReplaceAll(source, "'", "''") + `','initial-template':QName('', 'start')})` _, err := saxonQuery(t, ctx, binary, classpath, expression) - var diagnostic *exec.ExitError - if !errors.As(err, &diagnostic) || !strings.Contains(string(diagnostic.Stderr), code) { + stderr, failed := saxonFailureStderr(err) + if !failed || !strings.Contains(string(stderr), code) { t.Fatal(name, err) } } @@ -4424,19 +4424,19 @@ func compareSaxonConfiguredExpression(t *testing.T, ctx context.Context, e *tran output, err := saxonQuery(t, ctx, binary, classpath, oracle, options...) if diagnostic, separate := strings.CutPrefix(expected, "diagnostic:"); separate { - var failure *exec.ExitError - if !errors.As(err, &failure) || !strings.Contains(string(failure.Stderr), diagnostic) { + stderr, failed := saxonFailureStderr(err) + if !failed || !strings.Contains(string(stderr), diagnostic) { t.Fatalf("independent diagnostic: %q, %v; want %q", output, err, diagnostic) } return } if target, crash := strings.CutPrefix(expected, "crash:"); crash { - var failure *exec.ExitError - if !errors.As(err, &failure) { + stderr, failed := saxonFailureStderr(err) + if !failed { t.Fatalf("expected pinned Saxon crash diagnostic: %q, %v", output, err) } - diagnostic := string(failure.Stderr) + diagnostic := string(stderr) if !strings.Contains(diagnostic, "java.lang.ClassCastException") || !strings.Contains(diagnostic, target) { t.Fatal("unexpected independent engine failure", diagnostic) } @@ -4451,9 +4451,8 @@ func compareSaxonConfiguredExpression(t *testing.T, ctx context.Context, e *tran } if err != nil || string(output) != expected { - var failure *exec.ExitError - if errors.As(err, &failure) { - t.Log(string(failure.Stderr)) + if stderr, failed := saxonFailureStderr(err); failed { + t.Log(string(stderr)) } t.Fatalf("Saxon %s: %q, %v; want %q", expression, output, err, expected) @@ -4510,6 +4509,8 @@ func saxonRuntime(t *testing.T) (string, string) { t.Fatal("configured Saxon requires java", err) } + prepareSaxonWorkers(t, binary, classpath) + return classpath, binary } @@ -4523,10 +4524,10 @@ func saxonQuery(t *testing.T, ctx context.Context, binary, classpath, source str } arguments := make([]string, 0, 5+len(options)) - arguments = append(arguments, "-cp", classpath, "net.sf.saxon.Query", "-q:"+queryPath, "!method=text") + arguments = append(arguments, "-q:"+queryPath, "!method=text") arguments = append(arguments, options...) - return exec.CommandContext(ctx, binary, arguments...).Output() + return saxonOutput(t, ctx, binary, classpath, "Query", arguments...) } func compareSaxonStylesheet(t *testing.T, ctx context.Context, e *transform.Engine, binary, classpath, body, want string, oracleWant ...string) { @@ -4569,9 +4570,8 @@ func compareSaxonStylesheet(t *testing.T, ctx context.Context, e *transform.Engi } if got != expected { - var diagnostic *exec.ExitError - if errors.As(err, &diagnostic) { - t.Fatalf("Saxon %s: %q, %s; want %q", body, output, diagnostic.Stderr, expected) + if stderr, failed := saxonFailureStderr(err); failed { + t.Fatalf("Saxon %s: %q, %s; want %q", body, output, stderr, expected) } t.Fatalf("Saxon %s: %q, %v; want %q", body, output, err, expected) @@ -4581,12 +4581,12 @@ func compareSaxonStylesheet(t *testing.T, ctx context.Context, e *transform.Engi // Static type errors may be diagnosed before XQuery try/catch can run. func saxonDiagnosticCode(err error) string { - var diagnostic *exec.ExitError - if !errors.As(err, &diagnostic) { + stderr, failed := saxonFailureStderr(err) + if !failed { return "" } - for _, line := range strings.Split(string(diagnostic.Stderr), "\n") { + for _, line := range strings.Split(string(stderr), "\n") { fields := strings.Fields(line) if len(fields) > 0 { code := strings.TrimSuffix(fields[0], ":") @@ -4635,11 +4635,10 @@ func serializationURIOracle(t *testing.T, binary, classpath, source string) stri if err := os.WriteFile(file, []byte(source), 0o600); err != nil { t.Fatal(err) } - output, err := exec.CommandContext(t.Context(), binary, "-cp", classpath, "net.sf.saxon.Transform", "-xsl:"+file, "-it:start").Output() + output, err := saxonOutput(t, t.Context(), binary, classpath, "Transform", "-xsl:"+file, "-it:start") if err != nil { - var failure *exec.ExitError - if errors.As(err, &failure) { - t.Fatalf("independent URI table: %s: %v", failure.Stderr, err) + if stderr, failed := saxonFailureStderr(err); failed { + t.Fatalf("independent URI table: %s: %v", stderr, err) } t.Fatal(err) } diff --git a/transform/saxon_worker_test.go b/transform/saxon_worker_test.go new file mode 100644 index 0000000..426a1d0 --- /dev/null +++ b/transform/saxon_worker_test.go @@ -0,0 +1,340 @@ +//go:build interop + +package transform_test + +import ( + "bufio" + "bytes" + "context" + _ "embed" + "encoding/base64" + "errors" + "flag" + "fmt" + "io" + "net/http" + "net/http/httptest" + "os" + "os/exec" + "path/filepath" + "strconv" + "strings" + "sync" + "testing" + "time" +) + +//go:embed testdata/SaxonWorker.java +var saxonWorkerSource string + +var saxonWorkerPools sync.Map //nolint:gochecknoglobals // Each pool belongs to one top-level comparison and is removed by its cleanup. + +type saxonWorkerPool struct { + slots chan struct{} + idle chan *saxonWorker + source string +} + +type saxonWorker struct { + cmd *exec.Cmd + cancel context.CancelFunc + input io.WriteCloser + output *bufio.Reader + stderr bytes.Buffer +} + +type saxonWorkerError struct { + status int + stderr []byte +} + +func (e *saxonWorkerError) Error() string { + return fmt.Sprintf("Saxon exit status %d: %s", e.status, e.stderr) +} + +func saxonPoolKey(t *testing.T, binary, classpath string) string { + t.Helper() + return strings.SplitN(t.Name(), "/", 2)[0] + "\x00" + binary + "\x00" + classpath +} + +func prepareSaxonWorkers(t *testing.T, binary, classpath string) { + t.Helper() + if os.Getenv("XMLSEC_SAXON_PROCESS") == "1" || saxonProofOracle(t, binary, classpath) != "saxon-12.10" { + return + } + key := saxonPoolKey(t, binary, classpath) + parallel, err := strconv.Atoi(flag.Lookup("test.parallel").Value.String()) + if err != nil || parallel < 1 { + t.Fatal("invalid test parallelism", parallel, err) + } + pool := &saxonWorkerPool{ + slots: make(chan struct{}, parallel), + idle: make(chan *saxonWorker, parallel), + source: filepath.Join(t.TempDir(), "SaxonWorker.java"), + } + if err := os.WriteFile(pool.source, []byte(saxonWorkerSource), 0o600); err != nil { + t.Fatal(err) + } + if _, loaded := saxonWorkerPools.LoadOrStore(key, pool); loaded { + return + } + t.Cleanup(func() { + saxonWorkerPools.Delete(key) + for len(pool.idle) > 0 { + (<-pool.idle).stop() + } + }) +} + +func startSaxonWorker(binary, classpath, source string) (*saxonWorker, error) { + ctx, cancel := context.WithCancel(context.Background()) + worker := &saxonWorker{cmd: exec.CommandContext(ctx, binary, "-cp", classpath, source), cancel: cancel} + worker.cmd.Stderr = &worker.stderr + worker.cmd.WaitDelay = 5 * time.Second + var err error + worker.input, err = worker.cmd.StdinPipe() + if err != nil { + cancel() + return nil, err + } + stdout, err := worker.cmd.StdoutPipe() + if err != nil { + worker.input.Close() + cancel() + return nil, err + } + worker.output = bufio.NewReader(stdout) + if err := worker.cmd.Start(); err != nil { + worker.input.Close() + stdout.Close() + cancel() + return nil, err + } + return worker, nil +} + +func (w *saxonWorker) stop() { + w.cancel() + w.input.Close() + w.cmd.Wait() +} + +func (w *saxonWorker) exchange(mode string, args []string) ([]byte, error) { + var request strings.Builder + request.WriteString(mode) + for _, arg := range args { + request.WriteByte('\t') + request.WriteString(base64.StdEncoding.EncodeToString([]byte(arg))) + } + request.WriteByte('\n') + if _, err := io.WriteString(w.input, request.String()); err != nil { + return nil, err + } + line, err := w.output.ReadString('\n') + if err != nil { + return nil, err + } + fields := strings.Split(strings.TrimSuffix(line, "\n"), "\t") + if len(fields) != 3 { + return nil, fmt.Errorf("invalid Saxon response: %q", line) + } + status, err := strconv.Atoi(fields[0]) + if err != nil { + return nil, err + } + stdout, err := base64.StdEncoding.DecodeString(fields[1]) + if err != nil { + return nil, err + } + stderr, err := base64.StdEncoding.DecodeString(fields[2]) + if err != nil { + return nil, err + } + if status != 0 { + return stdout, &saxonWorkerError{status: status, stderr: stderr} + } + return stdout, nil +} + +func saxonOutput(t *testing.T, ctx context.Context, binary, classpath, mode string, args ...string) ([]byte, error) { + t.Helper() + entry, found := saxonWorkerPools.Load(saxonPoolKey(t, binary, classpath)) + if !found { + params := append([]string{"-cp", classpath, "net.sf.saxon." + mode}, args...) + output, err := exec.CommandContext(ctx, binary, params...).Output() + if ctx.Err() != nil { + return output, ctx.Err() + } + return output, err + } + pool := entry.(*saxonWorkerPool) + select { + case pool.slots <- struct{}{}: + case <-ctx.Done(): + return nil, ctx.Err() + } + var worker *saxonWorker + defer func() { + if worker != nil { + pool.idle <- worker + } + <-pool.slots + }() + select { + case worker = <-pool.idle: + default: + } + if worker == nil { + var err error + worker, err = startSaxonWorker(binary, classpath, pool.source) + if err != nil { + return nil, err + } + } + type response struct { + output []byte + err error + } + ready := make(chan response, 1) + go func() { + output, err := worker.exchange(mode, args) + ready <- response{output, err} + }() + select { + case result := <-ready: + var failure *saxonWorkerError + if result.err != nil && !errors.As(result.err, &failure) { + worker.stop() + result.err = fmt.Errorf("Saxon worker: %w: %s", result.err, worker.stderr.String()) + worker = nil + } + return result.output, result.err + case <-ctx.Done(): + worker.stop() + <-ready + worker = nil + return nil, ctx.Err() + } +} + +func saxonFailureStderr(err error) ([]byte, bool) { + var worker *saxonWorkerError + if errors.As(err, &worker) { + return worker.stderr, true + } + var process *exec.ExitError + if errors.As(err, &process) { + return process.Stderr, true + } + return nil, false +} + +func TestCompareSaxonWorkerContracts(t *testing.T) { + classpath, binary := saxonRuntime(t) + for _, test := range []struct { + name string + query string + options []string + }{ + {"unicode and framing", `string-join(('café 中',codepoints-to-string((9,10))),'')`, nil}, + {"static error", `1 +`, nil}, + {"recovery after error", `42`, nil}, + {"serialization option", `(1,2,3)`, []string{"!item-separator=|"}}, + {"fresh serialization options", `(1,2,3)`, nil}, + {"dynamic error", `error()`, nil}, + {"large response", `string-join(for $i in 1 to 100000 return 'x','')`, nil}, + } { + t.Run(test.name, func(t *testing.T) { + file := filepath.Join(t.TempDir(), "query.xq") + if err := os.WriteFile(file, []byte(test.query), 0o600); err != nil { + t.Fatal(err) + } + args := append([]string{"-q:" + file, "!method=text"}, test.options...) + pooled, pooledErr := saxonOutput(t, t.Context(), binary, classpath, "Query", args...) + params := append([]string{"-cp", classpath, "net.sf.saxon.Query"}, args...) + separate, separateErr := exec.CommandContext(t.Context(), binary, params...).Output() + pooledDiagnostic, pooledFailed := saxonFailureStderr(pooledErr) + separateDiagnostic, separateFailed := saxonFailureStderr(separateErr) + if !bytes.Equal(pooled, separate) || (pooledErr == nil) != (separateErr == nil) || pooledFailed != separateFailed || !bytes.Equal(pooledDiagnostic, separateDiagnostic) { + t.Fatalf("worker: %q, %v, %s; separate process: %q, %v, %s", pooled, pooledErr, pooledDiagnostic, separate, separateErr, separateDiagnostic) + } + if pooledErr != nil && !pooledFailed { + t.Fatal("oracle transport failed", pooledErr) + } + }) + } + t.Run("fresh static serializer tables", func(t *testing.T) { + for _, version := range []string{"5", "4.01", "5", "4.01"} { + file := filepath.Join(t.TempDir(), "serialize.xsl") + source := sheetStart + version30 + `">` + if err := os.WriteFile(file, []byte(source), 0o600); err != nil { + t.Fatal(err) + } + args := []string{"-xsl:" + file, "-it:start"} + pooled, pooledErr := saxonOutput(t, t.Context(), binary, classpath, "Transform", args...) + params := append([]string{"-cp", classpath, "net.sf.saxon.Transform"}, args...) + separate, separateErr := exec.CommandContext(t.Context(), binary, params...).Output() + if pooledErr != nil || separateErr != nil || !bytes.Equal(pooled, separate) { + t.Fatalf("HTML%s serializer state leaked: worker=%q (%v), separate=%q (%v)", version, pooled, pooledErr, separate, separateErr) + } + } + }) + t.Run("parallel request isolation", func(t *testing.T) { + for i := range 8 { + t.Run(strconv.Itoa(i), func(t *testing.T) { + t.Parallel() + want := fmt.Sprintf("request-%d", i) + output, err := saxonQuery(t, t.Context(), binary, classpath, "'"+want+"'") + if err != nil || string(output) != want { + t.Fatal("response belongs to another request", string(output), err, want) + } + }) + } + }) + t.Run("fresh document pool", func(t *testing.T) { + file := filepath.Join(t.TempDir(), "value.xml") + for _, want := range []string{"first", "second", "third", "fourth"} { + if err := os.WriteFile(file, []byte(""+want+""), 0o600); err != nil { + t.Fatal(err) + } + output, err := saxonQuery(t, t.Context(), binary, classpath, "string(doc('"+file+"')/r)") + if err != nil || string(output) != want { + t.Fatalf("stale document: %q, %v; want %q", output, err, want) + } + } + }) + t.Run("cancellation and replacement", func(t *testing.T) { + requested := make(chan struct{}, 1) + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + requested <- struct{}{} + <-r.Context().Done() + })) + defer server.Close() + ctx, cancel := context.WithTimeout(t.Context(), 20*time.Second) + defer cancel() + file := filepath.Join(t.TempDir(), "query.xq") + if err := os.WriteFile(file, []byte("doc('"+server.URL+"')"), 0o600); err != nil { + t.Fatal(err) + } + completed := make(chan error, 1) + go func() { + _, err := saxonOutput(t, ctx, binary, classpath, "Query", "-q:"+file) + completed <- err + }() + select { + case <-requested: + cancel() + case err := <-completed: + t.Fatal("oracle exited before the blocking request", err) + case <-ctx.Done(): + t.Fatal("oracle did not request the blocking document", ctx.Err()) + } + if err := <-completed; !errors.Is(err, context.Canceled) { + t.Fatal("cancellation did not terminate the request", err) + } + output, err := saxonQuery(t, t.Context(), binary, classpath, "'recovered'") + if err != nil || string(output) != "recovered" { + t.Fatal("oracle did not recover after cancellation", string(output), err) + } + }) +} diff --git a/transform/testdata/SaxonWorker.java b/transform/testdata/SaxonWorker.java new file mode 100644 index 0000000..4e4930e --- /dev/null +++ b/transform/testdata/SaxonWorker.java @@ -0,0 +1,75 @@ +import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; +import java.io.InputStreamReader; +import java.io.PrintStream; +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.net.URL; +import java.net.URLClassLoader; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Base64; + +// Reuse the JVM, never Saxon classes, processors or document pools. Saxon keeps +// mutable static serializer tables, so even a fresh Configuration is insufficient. +// Invoke the same CLI implementation as the one-shot oracle, with unchanged args. +// Saxon 12.10 doQuery/doTransform throw CommandExitException on failure. The +// legacy 9.4 CLI can call System.exit and deliberately remains a separate process. +class SaxonWorker { + public static void main(String[] args) throws Exception { + PrintStream protocol = System.out; + PrintStream diagnostics = System.err; + BufferedReader requests = new BufferedReader( + new InputStreamReader(System.in, StandardCharsets.UTF_8)); + String[] entries = System.getProperty("java.class.path").split(File.pathSeparator); + URL[] classpath = new URL[entries.length]; + for (int i = 0; i < entries.length; i++) { + classpath[i] = new File(entries[i]).toURI().toURL(); + } + String request; + while ((request = requests.readLine()) != null) { + String[] fields = request.split("\t", -1); + String mode = fields[0]; + String[] options = Arrays.stream(fields).skip(1) + .map(value -> new String(Base64.getDecoder().decode(value), StandardCharsets.UTF_8)) + .toArray(String[]::new); + ByteArrayOutputStream stdout = new ByteArrayOutputStream(); + ByteArrayOutputStream stderr = new ByteArrayOutputStream(); + int status = 0; + ClassLoader previous = Thread.currentThread().getContextClassLoader(); + try (URLClassLoader loader = new URLClassLoader(classpath, ClassLoader.getPlatformClassLoader()); + PrintStream out = new PrintStream(stdout, true, StandardCharsets.UTF_8); + PrintStream err = new PrintStream(stderr, true, StandardCharsets.UTF_8)) { + System.setOut(out); + System.setErr(err); + Thread.currentThread().setContextClassLoader(loader); + try { + if (!mode.equals("Query") && !mode.equals("Transform")) { + throw new IllegalArgumentException("unsupported oracle mode: " + mode); + } + Class cli = loader.loadClass("net.sf.saxon." + mode); + cli.getMethod("do" + mode, String[].class, String.class) + .invoke(cli.getConstructor().newInstance(), options, "net.sf.saxon." + mode); + } catch (InvocationTargetException failure) { + Throwable cause = failure.getCause(); + if (cause.getClass().getName().equals("net.sf.saxon.CommandExitException")) { + status = (Integer) cause.getClass().getMethod("getCode").invoke(cause); + } else { + status = 1; + cause.printStackTrace(err); + } + } catch (Exception failure) { + status = 1; + failure.printStackTrace(err); + } finally { + System.setOut(protocol); + System.setErr(diagnostics); + Thread.currentThread().setContextClassLoader(previous); + } + } + protocol.println(status + "\t" + Base64.getEncoder().encodeToString(stdout.toByteArray()) + + "\t" + Base64.getEncoder().encodeToString(stderr.toByteArray())); + protocol.flush(); + } + } +}