did:web resolver percent-decodes path segments, so the DID document URL is wrong, and the document id is not checked

Abierto
#6,293 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
54/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
java

Línea de trabajo

Start with WebDidUrlResolver.apply() and WebDidResolver.toDidDocument() in extensions/common/iam/decentralized-identity/identity-did-web; read the did:web Read steps cited in the issue. The reproduction cases in WebDidUrlResolverTest show the URL behavior to check, and the resolver reproduction describes the missing document ID check. Done means encoded path segments remain encoded and a document with a different ID is rejected; the issue notes related IdentityHub work is separate.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

triage

Bug Report

Describe the Bug

WebDidUrlResolver (module extensions/common/iam/decentralized-identity/identity-did-web) percent-decodes the
whole DID document URL. The did:web specification decodes only the port colon of the domain.

So a DID with a percent-encoded character in a path segment resolves to a different URL:

  • %28 becomes (. The server gets a different path.
  • %3F becomes ?. The rest of the path becomes a query string. The request goes to the parent path, and that
    can be the DID document of a different DID.

WebDidResolver then returns the fetched document without checking that its id equals the requested DID.
did:web "Read (Resolve)" step 7 requires this check.

The code is the same on v1.0.0-RC8 and on main.

This is one of three related issues about percent-encoded did:web path segments (see Context Information).

Expected Behavior
  • did:web:example.com%3A8443:participants:acme%28EU%29 resolves to
    https://example.com:8443/participants/acme%28EU%29/did.json.
  • did:web:example.com%3A8443:participants:acme%3Fbeta resolves to
    https://example.com:8443/participants/acme%3Fbeta/did.json.
  • If the fetched document has an id that is not the requested DID, resolution fails.
Observed Behavior
  • did:web:example.com%3A8443:participants:acme%28EU%29 resolves to
    https://example.com:8443/participants/acme(EU)/did.json.
  • did:web:example.com%3A8443:participants:acme%3Fbeta resolves to
    https://example.com:8443/participants/acme?beta/did.json. The HTTP request has path /participants/acme and
    query beta/did.json. An IdentityHub serves a DID document at /<path> (see DidWebParser), so this returns the
    document of did:web:example.com%3A8443:participants:acme.
  • WebDidResolver accepts that document. DidResolverRegistryImpl caches it under the requested DID.
    DidPublicKeyResolverImpl then selects a verification method from it. None of them compares the document id
    with the requested DID.

Steps to Reproduce

Steps to reproduce the behavior:

  1. Add this test to WebDidUrlResolverTest (same package, because the class is package-private):
@ParameterizedTest
@CsvSource({
        "did:web:example.com%3A8443:participants:acme%28EU%29, https://example.com:8443/participants/acme%28EU%29/did.json",
        "did:web:example.com%3A8443:participants:acme%3Fbeta,  https://example.com:8443/participants/acme%3Fbeta/did.json"
})
void apply_keepsPercentEncodedPathSegments(String did, String expectedUrl) {
    var resolver = new WebDidUrlResolver(true);

    assertThat(resolver.apply(did)).isEqualTo(expectedUrl);
}
  1. Run it. Both cases fail:
expected: "https://example.com:8443/participants/acme%28EU%29/did.json"
 but was: "https://example.com:8443/participants/acme(EU)/did.json"

expected: "https://example.com:8443/participants/acme%3Fbeta/did.json"
 but was: "https://example.com:8443/participants/acme?beta/did.json"
  1. For the missing id check: serve, at https://<host>/participants/acme, a DID document with
    "id": "did:web:<host>:participants:acme". Call WebDidResolver.resolve("did:web:<host>:participants:acme%3Fbeta").
    The resolver requests https://<host>/participants/acme?beta/did.json. By code reading, the result succeeds and
    contains the document of the other DID: no code path compares the document id with the requested DID.

Context Information

  • Used version: EDC Connector 1.0.0-RC8. Also present on main (checked 2026-11-30).
  • OS: not OS-specific.
  • Related issues: this is one of three issues about percent-encoded did:web path segments. All three must be
    fixed for such DIDs to work end to end:
    • This issue: Connector WebDidUrlResolver decodes path segments, and WebDidResolver does not
      check the document id (resolving side).
    • eclipse-edc/IdentityHub#1296: IdentityHub DidWebParser builds the DID from the decoded request path (serving side).
    • eclipse-edc/IdentityHub#1297: IdentityHub SelfIssuedTokenPrecheck rejects such DIDs (token verification).
  • Affected code: WebDidUrlResolver.apply() line 65, and WebDidResolver.toDidDocument().
  • We run EDC Connector and IdentityHub in a dataspace where each participant has a did:web DID. The last path
    segment is the participant's identifier from an external registry. Such identifiers can contain characters outside
    [A-Za-z0-9._-], for example acme(EU) or acme?beta, so the segment is percent-encoded (acme%28EU%29,
    acme%3Fbeta).

Detailed Description

The current code on main:

// WebDidUrlResolver.java
var identifier = extractIdentifier(did);
var url = identifier.toString();
if (identifier.getPath().length() == 0) {
    url += WELL_KNOWN;
}
return URLDecoder.decode(url + "/" + DID_DOCUMENT, StandardCharsets.UTF_8);   // line 65
// WebDidResolver.java, toDidDocument()
var didDocument = mapper.readValue(content.getContent(), DidDocument.class);
return Result.success(didDocument);

Specification. DID Core 1.0, section 3.1 "DID Syntax" (https://www.w3.org/TR/did-1.0/#did-syntax):

method-specific-id = *( *idchar ":" ) 1*idchar
idchar             = ALPHA / DIGIT / "." / "-" / "_" / pct-encoded
pct-encoded        = "%" HEXDIG HEXDIG

So acme%28EU%29 and acme%3Fbeta are valid DID path segments.

did:web Method Specification, "Read (Resolve)" (https://w3c-ccg.github.io/did-method-web/#read-resolve):

  1. Replace ":" with "/" in the method specific identifier to obtain the fully qualified domain name and optional path.
  2. If the domain contains a port percent decode the colon.
  3. Generate an HTTPS URL to the expected location of the DID document by prepending https://.
  4. If no path has been specified in the URL, append /.well-known.
  5. Append /did.json to complete the URL.
  6. Perform an HTTP GET request to the URL ...
  7. Verify that the ID of the resolved DID document matches the Web DID being resolved.

Step 2 decodes only the port colon. No step decodes path segments, so %28 stays %28 in the URL. Step 7 is not
implemented.

RFC 3986, section 2.2 (https://www.rfc-editor.org/rfc/rfc3986#section-2.2) also says that URIs which differ by
replacing a reserved character with its percent-encoded octet are not equivalent. /participants/acme%28EU%29 and
/participants/acme(EU) are different paths for the server.

Why the decoding is there (history). We think the decoding of path segments is not intended:

  • The first version (DidFunctions.resolveDidDocumentUrl) used URI.getSchemeSpecificPart(), which also decodes.
  • Issue #1652 ("WebDidResolver does not support domain port") reported that did:web:example.com%3A3000:user:alice
    resolved to https://example.com/3000/user/alice. PR #1701 fixed it: it reads the raw part, replaces : with /,
    and then URL-decodes the full URL. That decodes the port colon, but also every other escape.
  • The tests cover only the port ("HTTPS WITH DOMAIN PORT"). No test covers an escape in a path segment.
  • We found no decision record about this.

Impact.

  • DIDs with an encoded path segment cannot be resolved. With %3F, %23 or %2F, the request goes to another URL.
  • With no id check, a DID can resolve to the document of another DID. Its verification methods are then used for
    the requested DID. Whether a token is then accepted depends on the verification method ids in that document.
  • IdentityHub added a strict did:web shape check (eclipse-edc/IdentityHub#1200, eclipse-edc/IdentityHub#1203) because escapes such as %2F, %3F,
    %23 and %40 "are decoded into the resolution URL, where they can change the path or host". That check now
    rejects valid DIDs such as ...:acme%28EU%29 (see eclipse-edc/IdentityHub#1297). When the resolver keeps path
    escapes and checks the document id, that protection can live in the resolver, and the IdentityHub check can
    accept DID Core idchar.

Possible Implementation

Decode only the port colon in the domain. Keep path segments as they are. Reject a path segment that is not
1*idchar, and reject any other escape in the domain. Then check the document id.

--- a/extensions/common/iam/decentralized-identity/identity-did-web/src/main/java/org/eclipse/edc/iam/did/web/resolution/WebDidUrlResolver.java
+++ b/extensions/common/iam/decentralized-identity/identity-did-web/src/main/java/org/eclipse/edc/iam/did/web/resolution/WebDidUrlResolver.java
@@
-import java.net.URLDecoder;
-import java.nio.charset.StandardCharsets;
 import java.util.Objects;
 import java.util.function.UnaryOperator;
+import java.util.regex.Pattern;
 
 @SuppressWarnings("HttpUrlsUsage")
 class WebDidUrlResolver implements UnaryOperator<String> {
     private static final String DID_SCHEME = "did";
     private static final String DID_METHOD_PREFIX = "web:";
     private static final String DID_DOCUMENT = "did.json";
     private static final String WELL_KNOWN = "/.well-known";
+    // DID Core: 1*idchar, idchar = ALPHA / DIGIT / "." / "-" / "_" / pct-encoded
+    private static final Pattern PATH_SEGMENT = Pattern.compile("(?:[A-Za-z0-9._-]|%[0-9A-Fa-f]{2})+");
@@
             if (identifier.getPath().length() == 0) {
                 url += WELL_KNOWN;
             }
-            return URLDecoder.decode(url + "/" + DID_DOCUMENT, StandardCharsets.UTF_8);
+            return url + "/" + DID_DOCUMENT;
@@
-        var host = part.substring(DID_METHOD_PREFIX.length()).replace(':', '/');
-        return new URL(scheme + "://" + host);
+        var segments = part.substring(DID_METHOD_PREFIX.length()).split(":", -1);
+        // did:web Read step 2: only the port colon of the domain is percent-decoded
+        var domain = segments[0].replaceFirst("%3[Aa]", ":");
+        if (domain.contains("%")) {
+            throw new IllegalArgumentException("Invalid DID format, the domain may only percent-encode the port colon: " + did);
+        }
+        var path = new StringBuilder();
+        for (var i = 1; i < segments.length; i++) {
+            if (!PATH_SEGMENT.matcher(segments[i]).matches()) {
+                throw new IllegalArgumentException("Invalid DID format, invalid path segment '%s': %s".formatted(segments[i], did));
+            }
+            path.append('/').append(segments[i]);
+        }
+        return new URL(scheme + "://" + domain + path);
     }
--- a/extensions/common/iam/decentralized-identity/identity-did-web/src/main/java/org/eclipse/edc/iam/did/web/resolution/WebDidResolver.java
+++ b/extensions/common/iam/decentralized-identity/identity-did-web/src/main/java/org/eclipse/edc/iam/did/web/resolution/WebDidResolver.java
@@
             var didDocument = mapper.readValue(content.getContent(), DidDocument.class);
+            // did:web Read step 7
+            if (!didKey.equals(didDocument.getId())) {
+                return Result.failure("DID document id '%s' does not match the resolved DID '%s'".formatted(didDocument.getId(), didKey));
+            }
             return Result.success(didDocument);

We checked the patched mapping with OkHttp HttpUrl.get():

DID URL OkHttp encoded path
did:web:w3c-ccg.github.io https://w3c-ccg.github.io/.well-known/did.json /.well-known/did.json
did:web:example.com%3A3000:user:alice https://example.com:3000/user/alice/did.json /user/alice/did.json
did:web:example.com%3A8443:participants:acme%28EU%29 https://example.com:8443/participants/acme%28EU%29/did.json same, no query
did:web:example.com%3A8443:participants:acme%3Fbeta https://example.com:8443/participants/acme%3Fbeta/did.json same, no query
did:web:user%40internal.host rejected
did:web:example.com%3A80%3A81 rejected

The existing WebDidUrlResolverTest cases still pass.

Note: the id check can break deployments that serve a document whose id differs from its DID. Such documents
are already invalid under did:web. A setting to turn the check off could ease the migration, if needed.

The serving side has the matching problem: IdentityHub DidWebParser builds the DID from the decoded request path
(eclipse-edc/IdentityHub#1296).

Lenguaje dominante
Java
Estrellas
429
Forks
302
Merge medio
10 h 46 min
PR fusionados (30 d)
75

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de eclipse-edc/Connector

Todos los issues de eclipse-edc/Connector

Issues similares

Más issues de Java

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.