From 8a38dace76b53d10e750debd7b30c899e93a7eb2 Mon Sep 17 00:00:00 2001 From: Hector Sanjuan Date: Wed, 18 Oct 2023 19:35:03 +0200 Subject: [PATCH] Handle IPNS names without dots When checking if an ipfs name is blocked, we may get an `/ipns/{dnslink_name}` where the `{dnslink_name}` has been encoded according to https://specs.ipfs.tech/http-gateways/subdomain-gateway/#host-request-header That is, "." has been replaced with "-" and "-" has been replaced with "--". Because of this, when the given dns name has no dots, we undo things are replace "--" with "-" and "-" with ".". --- denylist.go | 26 ++++++++++++++++++++++++++ tester/tester.go | 2 ++ 2 files changed, 28 insertions(+) diff --git a/denylist.go b/denylist.go index b6ba739..ec0a835 100644 --- a/denylist.go +++ b/denylist.go @@ -544,6 +544,32 @@ func (dl *Denylist) IsIPNSPathBlocked(name, subpath string) StatusResponse { c, err := cid.Decode(key) if err == nil { key = c.Hash().B58String() + } else if !strings.ContainsRune(key, '.') { + // not a CID. It must be a ipns-dnslink name if it does not + // contain ".", maybe they got replaced by "-" + // https://specs.ipfs.tech/http-gateways/subdomain-gateway/#host-request-header + var result strings.Builder + for i := 0; i < len(key); i++ { + char := rune(key[i]) + nextChar := rune(0) + if i < len(key)-1 { + nextChar = rune(key[i+1]) + } + + if char == '-' && nextChar == '-' { + result.WriteRune('-') + i++ + continue + } + + if char == '-' { + result.WriteRune('.') + continue + } + + result.WriteRune(char) + } + key = result.String() } logger.Debugf("IsIPNSPathBlocked load: %s %s", key, subpath) entries, _ := dl.IPNSBlocksDB.Load(key) diff --git a/tester/tester.go b/tester/tester.go index 4cee1c1..faa5c69 100644 --- a/tester/tester.go +++ b/tester/tester.go @@ -265,10 +265,12 @@ func (s *Suite) testIPNSPath() error { // rule6 rule6 := []string{ "/ipns/domain.example", + "/ipns/domain-example", } rule6allowed := []string{ "/ipns/domainaefa.example", "/ipns/domain.example/path", + "/ipns/domain--example", } if err := s.testPaths(rule6, n, "rule6", false); err != nil {