Skip to content

Conversation

@tkalmar
Copy link

@tkalmar tkalmar commented Jul 12, 2025

Fixes #33
small fix to respect user provided idAttribute when generating id's

Summary by CodeRabbit

  • New Features
    • Support for namespaced ID attributes (e.g., WS‑Security and xml:id) when signing XML.
    • idAttribute option now accepts either a plain name or a namespaced identifier, so you can provide a custom attribute or a namespaced ID.
    • Generated IDs and reference lookups honor the provided attribute form (plain or namespaced) for consistent signing and verification.

@tkalmar
Copy link
Author

tkalmar commented Jul 12, 2025

Not sure if this needs enhancements to provide support for namespaced id's but then the whole idAttribute has to be changed. But in the long run this can then make the idMode parameter obsolete ...

@cjbarth
Copy link
Contributor

cjbarth commented Oct 17, 2025

@tkalmar , can you add a test showing how this behavior is currently broken?

@coderabbitai
Copy link

coderabbitai bot commented Oct 27, 2025

Walkthrough

Adds namespaced ID support and generalizes ID handling across signing: id attributes can now be plain strings or NamespacedId objects; findAttr and ID assignment logic were updated; a KNOWN_NAMESPACED_IDS constant and NamespacedId type were introduced; tests updated to exercise configurable idAttribute.

Changes

Cohort / File(s) Summary
Signing core
src/signed-xml.ts
Generalized ID handling: idAttributes type widened to `(string
Utilities
src/utils.ts
findAttr signature changed to accept `id: string
Types
src/types.ts
Added NamespacedId interface (prefix, localName, nameSpaceURI) and updated `SignedXmlOptions.idAttribute?: string
Constants
src/constants.ts
New KNOWN_NAMESPACED_IDS exported map containing wssecurity (wsu:Id, WS-Security URI) and xml (xml:id, XML namespace URI).
Tests
test/signature-unit-tests.spec.ts
Tests updated to pass and assert configurable idAttribute (string or namespaced), parameterize constructor, and verify IDs are assigned using the configured attribute.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant Caller as signing code
  participant SignedXml as SignedXml
  participant DOM as XML Element

  Note over SignedXml: idAttributes can be string or NamespacedId
  Caller->>SignedXml: computeSignature(...)
  SignedXml->>DOM: locate reference element
  SignedXml->>SignedXml: ensureHasId(element)
  alt existing id found
    SignedXml-->>SignedXml: use existing attribute (string or namespaced)
  else no id
    SignedXml->>DOM: set attribute
    Note right of DOM #DFF0D8: If NamespacedId, set xmlns + prefixed name\nOtherwise set plain attribute
    DOM-->>SignedXml: element updated with new id
  end
  SignedXml->>SignedXml: create Reference using found/generated id
  SignedXml-->>Caller: signature added
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Heterogeneous changes: type additions, constant file, API surface change (SignedXmlOptions), utility function signature change, and core logic updates.
  • Pay attention to:
    • findAttr behavior for prefixed attributes and namespace lookups.
    • Correct xmlns handling when creating namespaced Id attributes.
    • Backwards compatibility for existing string-only idAttributes in tests and public API.
    • Any call sites of findAttr or SignedXmlOptions elsewhere in the repo.

Possibly related PRs

Suggested labels

bug

Suggested reviewers

  • cjbarth

Poem

🐰 I found an Id, or made one new,
With prefixes neat and namespaces too,
No more hardcoded caps decide the day,
The rabbit signed and hopped away — hooray! ✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title "Respect idAttribute when generating signatures" is concise, specific, and directly aligned with the main objective of the pull request. It clearly communicates that the primary change involves modifying the signature generation logic to honor user-provided idAttribute configuration instead of using hard-coded values. The title accurately captures the core purpose without being vague or misleading.
Linked Issues Check ✅ Passed The pull request successfully addresses the core requirement from issue #33, which requests using the user-provided idAttribute instead of the hard-coded "Id" when setting element id attributes. The implementation goes further by introducing NamespacedId support across multiple files (types.ts, constants.ts, signed-xml.ts, utils.ts), enabling fully namespace-aware ID attributes as suggested by reviewers. The ensureHasId logic now generically iterates idAttributes and supports both plain string and NamespacedId structures, while the constructor options have been extended to accept the idAttribute parameter in tests. These changes fulfill both the primary objective and the enhancement suggestions from the review discussion.
Out of Scope Changes Check ✅ Passed All code changes in this pull request are directly related to the objective of respecting user-provided idAttribute during signature generation. The introduction of the NamespacedId interface and KNOWN_NAMESPACED_IDS constant supports the extended namespace-aware functionality discussed in the review comments. Changes to src/signed-xml.ts, src/utils.ts, and src/types.ts all serve to generalize ID handling and support both string and namespaced ID attributes. The test updates validate the new idAttribute functionality. No extraneous or unrelated modifications are present.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
src/signed-xml.ts (1)

1332-1332: Consider validating the idAttribute value in the constructor.

The change correctly uses this.idAttributes[0] to respect custom ID attributes. However, there's no validation when a custom idAttribute is provided in the constructor (lines 155-157). Invalid values (empty string, whitespace, invalid XML name characters) could cause issues during signing.

Consider adding validation in the constructor:

     if (idAttribute) {
+      if (typeof idAttribute !== 'string' || !idAttribute.trim()) {
+        throw new Error('idAttribute must be a non-empty string');
+      }
       this.idAttributes.unshift(idAttribute);
     }
test/signature-unit-tests.spec.ts (1)

88-88: Fix TypeScript formatting.

Type annotations should have spaces around the colon and type union operator.

Apply this diff:

-    function verifyAddsId(mode, nsMode, idAttribute:string|undefined = undefined) {
+    function verifyAddsId(mode, nsMode, idAttribute: string | undefined = undefined) {
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 73db72d and 26d9b91.

📒 Files selected for processing (2)
  • src/signed-xml.ts (1 hunks)
  • test/signature-unit-tests.spec.ts (2 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: shunkica
PR: node-saml/xml-crypto#506
File: src/signed-xml.ts:1159-1159
Timestamp: 2025-10-22T21:03:38.354Z
Learning: In node-saml/xml-crypto PR #506, the maintainer (shunkica) requested an issue to separate the overloaded Reference interface into distinct SigningReference and ValidationReference types. Initial hypothesis: signing-only (xpath, isEmptyUri, id, type), validation-only (uri, digestValue, validationError, signedReference), shared (transforms, digestAlgorithm, inclusiveNamespacesPrefixList). This should be proposed and designed in a follow-up, not altered in the current PR.
📚 Learning: 2025-10-22T21:50:05.454Z
Learnt from: shunkica
PR: node-saml/xml-crypto#0
File: :0-0
Timestamp: 2025-10-22T21:50:05.454Z
Learning: In src/signed-xml.ts Line 1099, createReferences mutates ref.uri = id during signing. Maintain this behavior for now; remove/refactor in a separate PR as previously requested by the maintainer.

Applied to files:

  • test/signature-unit-tests.spec.ts
  • src/signed-xml.ts
🧬 Code graph analysis (1)
test/signature-unit-tests.spec.ts (1)
src/signed-xml.ts (1)
  • SignedXml (30-1422)
🔇 Additional comments (1)
test/signature-unit-tests.spec.ts (1)

125-127: Good test coverage for custom ID attributes.

The new test case effectively verifies that custom idAttribute values are respected during signing, using 'myIdAttribute' to ensure the generated IDs use the configured attribute name instead of the default 'Id'.

@cjbarth
Copy link
Contributor

cjbarth commented Oct 27, 2025

The code looks OK @tkalmar , but I'm not sure what the use-case is. What is the problem that is solved by writing a different attribute for ID other than the traditional Id? We already liberally accept several different attributes for ID, and I'd imagine other libraries do the same.

@tkalmar
Copy link
Author

tkalmar commented Oct 28, 2025

@cjbarth we are working with an customer endpoint which validates our signed xml, at the moment we have to replace Id in the signed xml with xml:id to make the customer system happy. This is somewhat perf killing as we have large xmls and loading them to replace only three id's is not helping, so with this patch we can generate the signed xml with xml:id already and it works without replacing stuff.

@cjbarth
Copy link
Contributor

cjbarth commented Oct 28, 2025

So, you don't want a different ID, you want a namespace. Do you have a sample XML that you could share? A proper fix would probably be to read the namespace from the existing XML and use that.

@shunkica
Copy link
Contributor

The proper solution would be to make idAttribute/idAttributes of type string | {prefix: string, localName: string, namespaceURI: string}

@tkalmar
Copy link
Author

tkalmar commented Nov 2, 2025

@cjbarth @shunkica for xml:id no namespace is required as the xml namespace is bound/reserved per default. (see https://www.w3.org/TR/xml-names11/#ns-decl namespace constraints) so xml:id should work OOB as described here https://www.w3.org/TR/xml-id/
With this fix and an idAttribute of xml:id the generated documents validate with xmlsec CLI tool, whereas Id or id does not work. Perhaps it would be even a better default for the lib instead of Id ...

@shunkica
Copy link
Contributor

shunkica commented Nov 2, 2025

@tkalmar The XMLDsig spec doesn't prescribe using xml:id specifically—it just requires Id-type attributes. We understand your use case, but I think If there are going to be changes to this it should be to support fully qualified ID attributes. A simple string-based approach is too narrow and doesn't guarantee proper namespace-aware attribute creation.

Proposal: Modify this PR to accept idAttribute as either:

  • A string (for backwards compatibility with simple attributes like "Id" or "id")
  • An object: {prefix: string, localName: string, namespaceURI: string}

For your use case, you'd pass:

  {
    prefix: "xml",
    localName: "id",
    namespaceURI: "http://www.w3.org/XML/1998/namespace"
  }

This would properly create a namespaced attribute and accommodate any ID attribute convention—whether it's xml:id,
wsu:Id, ds:Id, or custom application-specific ones.

@tkalmar
Copy link
Author

tkalmar commented Nov 2, 2025

@shunkica A first draft is done.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/signed-xml.ts (1)

546-546: Fix XPath construction for NamespacedId (duplicate issue).

Similar to line 517, when attr is a NamespacedId, the XPath expression will be malformed. Extract localName before constructing the XPath.

Apply this fix:

       for (const attr of this.idAttributes) {
+        const attrName = typeof attr === "string" ? attr : attr.localName;
-        const tmp_elemXpath = `//*[@*[local-name(.)='${attr}']='${uri}']`;
+        const tmp_elemXpath = `//*[@*[local-name(.)='${attrName}']='${uri}']`;
         const tmp_elem = xpath.select(tmp_elemXpath, doc);
🧹 Nitpick comments (2)
test/signature-unit-tests.spec.ts (1)

127-129: Consider adding test coverage for NamespacedId.

The new test validates custom string idAttribute (e.g., "myIdAttribute"), which addresses the primary use case from issue #33. However, the PR also introduces NamespacedId support. Consider adding a test case that uses a NamespacedId object (e.g., xml:id or wsu:Id) to ensure complete coverage of the new functionality.

Example test structure:

it("signer adds increasing namespaced id attributes to elements", function () {
  const xml = '<root><x xmlns="ns"></x><y attr="value"></y><z><w></w></z></root>';
  const sig = new SignedXml({ 
    idAttribute: { prefix: "xml", localName: "id", nameSpaceURI: "http://www.w3.org/XML/1998/namespace" }
  });
  // ... rest of test
});
src/signed-xml.ts (1)

1321-1334: Namespace prefix declaration is handled correctly by DOM spec; no duplicate issues.

While the code unconditionally declares the namespace prefix without checking if it already exists, the DOM spec's setAttributeNS updates existing attributes with the same namespaceURI+localName rather than creating duplicates, and implementations avoid emitting duplicate prefix→URI declarations during serialization. This means the current implementation works correctly and won't create problematic duplicate declarations.

That said, explicitly checking for an existing prefix before declaration would improve code clarity:

// Optional improvement: check if prefix already bound
if (!node.lookupNamespaceURI(this.idAttributes[0].prefix)) {
  node.setAttributeNS(
    "http://www.w3.org/2000/xmlns/",
    `xmlns:${this.idAttributes[0].prefix}`,
    this.idAttributes[0].nameSpaceURI,
  );
}
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 26d9b91 and 193a6a6.

📒 Files selected for processing (5)
  • src/constants.ts (1 hunks)
  • src/signed-xml.ts (6 hunks)
  • src/types.ts (1 hunks)
  • src/utils.ts (2 hunks)
  • test/signature-unit-tests.spec.ts (2 hunks)
🧰 Additional context used
🧠 Learnings (5)
📓 Common learnings
Learnt from: shunkica
Repo: node-saml/xml-crypto PR: 506
File: src/signed-xml.ts:1159-1159
Timestamp: 2025-10-22T21:03:38.354Z
Learning: In node-saml/xml-crypto PR #506, the maintainer (shunkica) requested an issue to separate the overloaded Reference interface into distinct SigningReference and ValidationReference types. Initial hypothesis: signing-only (xpath, isEmptyUri, id, type), validation-only (uri, digestValue, validationError, signedReference), shared (transforms, digestAlgorithm, inclusiveNamespacesPrefixList). This should be proposed and designed in a follow-up, not altered in the current PR.
Learnt from: shunkica
Repo: node-saml/xml-crypto PR: 506
File: src/signed-xml.ts:1159-1159
Timestamp: 2025-10-22T20:36:00.758Z
Learning: In node-saml/xml-crypto PR #506, the maintainer (shunkica) prefers to address the ref.uri mutation inside addAllReferences in a separate PR; removing the in-loop assignment is the desired fix but may be treated as a breaking change. Future guidance: avoid behavioral changes to ref.uri in the current PR.
Learnt from: shunkica
Repo: node-saml/xml-crypto PR: 0
File: :0-0
Timestamp: 2025-10-22T21:50:05.454Z
Learning: In src/signed-xml.ts Line 1099, createReferences mutates ref.uri = id during signing. Maintain this behavior for now; remove/refactor in a separate PR as previously requested by the maintainer.
📚 Learning: 2025-10-22T21:50:05.454Z
Learnt from: shunkica
Repo: node-saml/xml-crypto PR: 0
File: :0-0
Timestamp: 2025-10-22T21:50:05.454Z
Learning: In src/signed-xml.ts Line 1099, createReferences mutates ref.uri = id during signing. Maintain this behavior for now; remove/refactor in a separate PR as previously requested by the maintainer.

Applied to files:

  • src/types.ts
  • test/signature-unit-tests.spec.ts
  • src/signed-xml.ts
  • src/utils.ts
📚 Learning: 2025-10-22T21:50:05.454Z
Learnt from: shunkica
Repo: node-saml/xml-crypto PR: 0
File: :0-0
Timestamp: 2025-10-22T21:50:05.454Z
Learning: The current Reference fields are defined in src/types.ts Lines 109–168: xpath?, transforms, digestAlgorithm, uri, digestValue?, inclusiveNamespacesPrefixList, isEmptyUri, ancestorNamespaces?, validationError?, getValidatedNode(), signedReference?.

Applied to files:

  • src/types.ts
  • src/signed-xml.ts
📚 Learning: 2025-10-22T21:03:38.354Z
Learnt from: shunkica
Repo: node-saml/xml-crypto PR: 506
File: src/signed-xml.ts:1159-1159
Timestamp: 2025-10-22T21:03:38.354Z
Learning: In node-saml/xml-crypto PR #506, the maintainer (shunkica) requested an issue to separate the overloaded Reference interface into distinct SigningReference and ValidationReference types. Initial hypothesis: signing-only (xpath, isEmptyUri, id, type), validation-only (uri, digestValue, validationError, signedReference), shared (transforms, digestAlgorithm, inclusiveNamespacesPrefixList). This should be proposed and designed in a follow-up, not altered in the current PR.

Applied to files:

  • src/types.ts
  • test/signature-unit-tests.spec.ts
  • src/signed-xml.ts
📚 Learning: 2025-10-25T14:41:13.728Z
Learnt from: shunkica
Repo: node-saml/xml-crypto PR: 519
File: example/new-api-example.js:1-9
Timestamp: 2025-10-25T14:41:13.728Z
Learning: Example files in the node-saml/xml-crypto repository should use `require("xml-crypto")` (the package name) rather than relative paths to build artifacts, since they demonstrate usage from an end-user's perspective.

Applied to files:

  • src/signed-xml.ts
🧬 Code graph analysis (4)
test/signature-unit-tests.spec.ts (1)
src/signed-xml.ts (1)
  • SignedXml (32-1422)
src/signed-xml.ts (2)
src/types.ts (1)
  • NamespacedId (65-72)
src/constants.ts (1)
  • KNOWN_NAMESPACED_IDS (4-18)
src/constants.ts (1)
src/types.ts (1)
  • NamespacedId (65-72)
src/utils.ts (1)
src/types.ts (1)
  • NamespacedId (65-72)
🔇 Additional comments (10)
src/types.ts (2)

62-72: LGTM! Well-structured interface for namespaced attributes.

The NamespacedId interface correctly models XML namespaced attributes with clear field names and documentation.


79-79: LGTM! Backward-compatible API extension.

The union type string | NamespacedId maintains backward compatibility while enabling namespaced ID support as requested in issue #33.

src/utils.ts (2)

23-30: LGTM! Clean handling of union type.

The implementation correctly handles both string and NamespacedId types using a type guard, extracting the appropriate localName and namespace values for attribute matching.


20-20: Confirm this breaking API change is intentional.

findAttr is re-exported from index.ts as part of the public API. The signature change from (element, localName, namespace?) to (element, id: string | NamespacedId) is a breaking change that will impact external users. All internal call sites have been correctly updated. Verify this change is intentional and ensure proper version/changelog documentation if this is a major release.

src/constants.ts (1)

4-11: LGTM! Correctly models WS-Security namespaced ID.

The wssecurity entry accurately represents the wsu:Id attribute from the WS-Security Utility 1.0 specification, centralizing what was previously hardcoded logic.

src/signed-xml.ts (4)

11-11: LGTM! Necessary imports for namespaced ID support.

The imports of NamespacedId and KNOWN_NAMESPACED_IDS enable the new functionality throughout the class.

Also applies to: 30-30


34-34: Public API change: idAttributes now supports NamespacedId.

The type change to (string | NamespacedId)[] extends the public API to support namespaced ID attributes. This is backward compatible for reading but may affect code that writes to this property.


160-162: LGTM! Cleaner wssecurity mode initialization.

Using KNOWN_NAMESPACED_IDS["wssecurity"] centralizes the WS-Security ID attribute configuration, making the code more maintainable than hardcoded values.


1309-1312: LGTM! Simplified attribute checking.

The change to pass idAttribute directly to findAttr simplifies the code and aligns with the updated findAttr signature.

test/signature-unit-tests.spec.ts (1)

117-119: Test helper verifyAddsId does not accept NamespacedId; no issue found.

The test helper's type signature explicitly restricts idAttribute to string | undefined (line 88), and all test calls pass only string or undefined values. The XPath construction is correct for these accepted types. The broader NamespacedId support exists in SignedXmlOptions (src/types.ts line 79) but was intentionally not included in the test helper's signature—this is a deliberate design choice, not an oversight.

Comment on lines +13 to +17
xml: {
prefix: "xml",
localName: "id",
nameSpaceURI: "http://www.w3.org/XML/1998/namespace",
},
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

LGTM! Supports the xml:id use case.

The xml entry correctly models the xml:id attribute, addressing the customer requirement mentioned in the PR comments where they need to generate signatures with xml:id instead of Id.

Verify that the xml constant is actually used in the codebase:


🏁 Script executed:

#!/bin/bash
# Search for usage of KNOWN_NAMESPACED_IDS["xml"]
rg -n 'KNOWN_NAMESPACED_IDS\["xml"\]' --type ts
rg -n "KNOWN_NAMESPACED_IDS\['xml'\]" --type ts

Length of output: 0


🏁 Script executed:

#!/bin/bash
# Search for broader KNOWN_NAMESPACED_IDS usage patterns
echo "=== Search for KNOWN_NAMESPACED_IDS usage ==="
rg -n 'KNOWN_NAMESPACED_IDS' --type ts -C 2

echo ""
echo "=== View the constants.ts file to see the structure ==="
fd 'constants.ts' --type f | xargs head -30

Length of output: 1317


🏁 Script executed:

#!/bin/bash
# Search for idMode usage to understand if xml is supposed to be supported
echo "=== Search for idMode string patterns ==="
rg -n 'idMode\s*===|idMode\s*=\s*["\']' --type ts -C 1

echo ""
echo "=== Check full idMode logic in signed-xml.ts ==="
rg -n 'idMode' src/signed-xml.ts -C 3

Length of output: 110


🏁 Script executed:

#!/bin/bash
# Search for idMode usage with simpler patterns
echo "=== Search for idMode assignments and checks ==="
rg -n 'idMode' src/signed-xml.ts -B 2 -A 2

echo ""
echo "=== Search for any xml mode or xml:id references ==="
rg -n 'xml:id|xml.*mode|mode.*xml' --type ts

Length of output: 815


Add implementation logic to complete the xml:id support feature.

The xml constant has been added to KNOWN_NAMESPACED_IDS, but the feature is incomplete and currently dead code. To make this functional:

  1. Update the idMode type at src/signed-xml.ts:33 to include "xml":

    idMode?: "wssecurity" | "xml";
    
  2. Add the corresponding handler in the constructor (after line 162):

    if (idMode === "xml") {
      this.idAttributes.unshift(KNOWN_NAMESPACED_IDS["xml"]);
    }
    

Without these changes, the xml constant cannot be used and the customer's xml:id use case won't function.

🤖 Prompt for AI Agents
In src/constants.ts around lines 13-17 and in src/signed-xml.ts update the
idMode type and constructor to enable xml:id handling: add "xml" to the idMode
union at or near line 33 (idMode?: "wssecurity" | "xml";) and in the constructor
after line 162, detect idMode === "xml" and unshift KNOWN_NAMESPACED_IDS["xml"]
onto this.idAttributes so the xml namespaced id is actually used; this wires the
new KNOWN_NAMESPACED_IDS entry into runtime behavior.

Comment on lines +512 to +515
const elemId =
typeof attr === "string"
? elem.getAttribute(attr)
: elem.getAttribute(`${attr.prefix}:${attr.localName}`);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major

Consider using getAttributeNS for namespaced attributes.

The code constructs a prefixed attribute name (${attr.prefix}:${attr.localName}) for NamespacedId attributes, but DOM's getAttributeNS(namespaceURI, localName) is the standard method for reading namespaced attributes. This could lead to issues if the prefix varies or is absent.

Apply this pattern for reading namespaced attributes:

 const elemId =
   typeof attr === "string"
     ? elem.getAttribute(attr)
-    : elem.getAttribute(`${attr.prefix}:${attr.localName}`);
+    : elem.getAttributeNS(attr.nameSpaceURI, attr.localName);

This ensures correct attribute resolution regardless of prefix usage in the document.

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In src/signed-xml.ts around lines 512 to 515, the code builds a prefixed name to
read a namespaced attribute which will break if the document uses a different or
no prefix; change the branch that handles non-string attr to call
elem.getAttributeNS(attr.namespaceURI, attr.localName) instead of
elem.getAttribute(`${attr.prefix}:${attr.localName}`), and if getAttributeNS
returns null/undefined fall back to the prefixed lookup only as a last resort;
also guard for the case attr.namespaceURI may be missing (then use the old
getAttribute behavior).

? elem.getAttribute(attr)
: elem.getAttribute(`${attr.prefix}:${attr.localName}`);
if (uri === elemId) {
ref.xpath = `//*[@*[local-name(.)='${attr}']='${uri}']`;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Fix XPath construction for NamespacedId.

When attr is a NamespacedId object, the template literal ${attr} will stringify it as "[object Object]", producing an invalid XPath. Extract the localName for proper XPath construction.

Apply this fix:

+        const attrName = typeof attr === "string" ? attr : attr.localName;
-        if (uri === elemId) {
-          ref.xpath = `//*[@*[local-name(.)='${attr}']='${uri}']`;
+        if (uri === elemId) {
+          ref.xpath = `//*[@*[local-name(.)='${attrName}']='${uri}']`;
           break; // found the correct element, no need to check further
         }

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In src/signed-xml.ts around line 517, the XPath construction uses `${attr}`
which stringifies a NamespacedId to "[object Object]"; change it to detect when
attr is a NamespacedId (e.g. an object with a localName property) and use its
localName instead of the object in the template literal so the xpath becomes
`//*[@*[local-name(.)='${localName}']='${uri}']`; ensure the code handles both
string and NamespacedId inputs before assigning ref.xpath.

@shunkica
Copy link
Contributor

shunkica commented Nov 2, 2025

This does conflict with PR #519 where a functionality like this is already supported.

@tkalmar
Copy link
Author

tkalmar commented Nov 10, 2025

@shunkica @cjbarth
How to proceed from here?
Wait until PR #519 is merged?
Revert to the initial solution?
In my opinion it would be best to revert this to the initial solution.
It fixes exactly one problem.
It is minimal invasive. No breakage no changed params.
It is sufficient for my use case.
WDYT?

@cjbarth
Copy link
Contributor

cjbarth commented Nov 15, 2025

@tkalmar , if @shunkica is able to continue working on he PR, given its size, it would seem reasonable to get that landed first and see what parts of that we can leverage to meet your needs. I'm not thrilled about solving your need for a namespace by allowing for string replacement. Even your current PR seems to fudge the concept of a namespace as some of the comments by coderabbitai points out. If @shunkica is not able to continue, then we'll see what we can do to get you in sooner.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use this.idAttributes[0] when setting id attribute of element?

3 participants