Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support subdomains in isIPFS.url(url) #32

Merged
merged 10 commits into from
Apr 5, 2020
Merged

Conversation

lidel
Copy link
Member

@lidel lidel commented Mar 22, 2020

This PR updates isIPFS.url(url) to return true if isIPFS.subdomain(url) does,
and improves isIPFS.ipnsSubdomain(url) to match potential DNSLink just like isIPFS.ipnsPath(url) already did.

This is needed for ipfs/ipfs-companion#853:

BREAKING CHANGES

  • BREAKING CHANGE: isIPFS.subdomain now returns true for <domain.tld>.ipns.localhost
  • BREAKING CHANGE: isIPFS.subdomainPattern|pathPattern|urlPattern are replaced with pathPattern|pathGatewayPattern|subdomainGatewayPattern
  • BREAKING CHANGE: isIPFS.url now returns true if isIPFS.subdomain did

Due to this I plan to release this as v1.0.0

This change adds support for DNSLink subdomains on localhost gateway
(ipfs/kubo#6096)

Example: en.wikipedia-on-ipfs.org.ipfs.localhost:8080

BREAKING CHANGE: `isIPFS.subdomain` now returns true for <domain.tld>.ipns.localhost
BREAKING CHANGE: `isIPFS.subdomainPattern` changed

License: MIT
Signed-off-by: Marcin Rataj <lidel@lidel.org>
Context: libp2p/libp2p#79

License: MIT
Signed-off-by: Marcin Rataj <lidel@lidel.org>
.url and .path now return true when validating:
https://ipfs.io/ipfs/<CID>?filename=name.png#foo

License: MIT
Signed-off-by: Marcin Rataj <lidel@lidel.org>
License: MIT
Signed-off-by: Marcin Rataj <lidel@lidel.org>
README.md Outdated Show resolved Hide resolved

### `isIPFS.ipfsSubdomain(url)`

Returns `true` if the provided string includes a valid IPFS subdomain or `false` otherwise.
Returns `true` if the provided `url` string includes a valid IPFS subdomain (case-insensitive CIDv1) or `false` otherwise.
Copy link
Member

Choose a reason for hiding this comment

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

Does it check for a case insensitive encoding or is this actually just base32?

Copy link
Member Author

Choose a reason for hiding this comment

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

Web browsers force-lowercase hostname in URL and is-ipfs does the same before we check if CID in subdomain is valid:
https://github.com/ipfs/is-ipfs/blob/f1823cc0b12e97ac1f202ba7c06663b5b9b5d265/src/index.js#L83-L87

README.md Outdated

isIPFS.path('/ipfs/QmYjtig7VJQ6XsnUjqqJvj7QaMcCAwtrgNdahSiFofrE7o') // true
isIPFS.path('/ipfs/QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR?filename=guardian.jpg') // true
Copy link
Member

Choose a reason for hiding this comment

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

Is this a thing now?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, ?filename= sets proper Content-Disposition header and all that jazz. Folks use it as more efficient way of sharing single file, as it keeps the same CID while enables filename tweaks (does not require wrapping in a directory)

I've added it to README as its important to show that URL params do not impact CID validation.

Copy link
Member

Choose a reason for hiding this comment

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

I know it does that for URLs on the gateways but I didn't know if we'd formalised it in IPFS paths.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, I get your point now.

No, it is not formalized in the context of IPFS paths – ?filename=foo.jpg is just a valid filename in IPFS.

See this example:

$ ipfs object patch add-link QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn "?filename=test.jpg" QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR
QmbcBPAwCDxRMB1Qe7CRQmxdrTSkxKwM9y6rZw2FjGtbsb

$ ipfs ls /ipfs/QmbcBPAwCDxRMB1Qe7CRQmxdrTSkxKwM9y6rZw2FjGtbsb
QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR 119762 ?filename=test.jpg

When exposed on the gateway, filenames are URIencoded:
https://ipfs.io/ipfs/QmbcBPAwCDxRMB1Qe7CRQmxdrTSkxKwM9y6rZw2FjGtbsb/%3Ffilename=test.jpg

Here I just forgot / before ?, but I agree the naming overlap is confusing, so I changed it to be something else:

Suggested change
isIPFS.path('/ipfs/QmbWqxBEKC3P8tqsKc98xmWNzrzDtRLMiMPL8wBuTGsMnR?filename=guardian.jpg') // true
isIPFS.path('/ipfs/QmbcBPAwCDxRMB1Qe7CRQmxdrTSkxKwM9y6rZw2FjGtbsb/?weird-filename=test.jpg') // true

lidel added a commit that referenced this pull request Mar 24, 2020
When .url was created we only had path gateways.  When .subdomain was
added, we did not update .url to test for subdomain gateways, which in
the long run will confuse people and feels like a bug.

Let's fix this: .url() will now check for both subdomain and path gateways

#32 (comment)

BREAKING CHANGE: .url(url) now returns true if .subdomain(url) is true

License: MIT
Signed-off-by: Marcin Rataj <lidel@lidel.org>
When .url was created we only had path gateways.  When .subdomain was
added, we did not update .url to test for subdomain gateways, which in
the long run will confuse people and feels like a bug.

Let's fix this: .url() will now check for both subdomain and path gateways

#32 (comment)

BREAKING CHANGE: .url(url) now returns true if .subdomain(url) is true

License: MIT
Signed-off-by: Marcin Rataj <lidel@lidel.org>
This makes subdomain checks follow what path gateway checks do, removing
confusion.

In both cases (IPNS and DNSLink) user needs to perform online record
check, so this is just a handy way of detecting potential matches.

License: MIT
Signed-off-by: Marcin Rataj <lidel@lidel.org>
@lidel lidel changed the title feat: isIPFS.dnslinkSubdomain(url) feat: support subdomains in isIPFS.url(url) Mar 25, 2020
License: MIT
Signed-off-by: Marcin Rataj <lidel@lidel.org>
src/index.js Show resolved Hide resolved
License: MIT
Signed-off-by: Marcin Rataj <lidel@lidel.org>
License: MIT
Signed-off-by: Marcin Rataj <lidel@lidel.org>
License: MIT
Signed-off-by: Marcin Rataj <lidel@lidel.org>
@lidel lidel merged commit 22d001d into master Apr 5, 2020
@lidel lidel deleted the feat/dnslink-subdomains branch April 5, 2020 15:48
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.

3 participants