Skip to content

Commit

Permalink
Handle IPNS names without dots
Browse files Browse the repository at this point in the history
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 ".".
  • Loading branch information
hsanjuan committed Oct 18, 2023
1 parent 3b5bfd3 commit 5433362
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
30 changes: 30 additions & 0 deletions denylist.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,31 @@ func (dl *Denylist) IsSubpathBlocked(subpath string) StatusResponse {
}
}

func toDNSLinkFQDN(label string) string {
var result strings.Builder
for i := 0; i < len(label); i++ {
char := rune(label[i])
nextChar := rune(0)
if i < len(label)-1 {
nextChar = rune(label[i+1])
}

if char == '-' && nextChar == '-' {
result.WriteRune('-')
i++
continue
}

if char == '-' {
result.WriteRune('.')
continue
}

result.WriteRune(char)
}
return result.String()
}

// IsIPNSPathBlocked returns Blocking Status for a given IPNS name and its
// subpath. The name is NOT an "/ipns/name" path, but just the name.
func (dl *Denylist) IsIPNSPathBlocked(name, subpath string) StatusResponse {
Expand All @@ -534,6 +559,11 @@ 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
key = toDNSLinkFQDN(key)
}
logger.Debugf("IsIPNSPathBlocked load: %s %s", key, subpath)
entries, _ := dl.IPNSBlocksDB.Load(key)
Expand Down
2 changes: 2 additions & 0 deletions tester/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 5433362

Please sign in to comment.