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

Fix ipfs address generation #655

Merged
merged 9 commits into from
Dec 3, 2021
Merged

Fix ipfs address generation #655

merged 9 commits into from
Dec 3, 2021

Conversation

adonesky1
Copy link
Contributor

@adonesky1 adonesky1 commented Dec 1, 2021

  • CHANGED:

    -BREAKING Change IPFS URL generation to use subdomains and cidV1s over cidV0s, in order to enhance origin based security in our use of IPFS assets

    • Consumers using IPFS Gateway which do not support subdomain formats will need to set the new config value 'useIPFSSubdomains' on CollectiblesController to false in order to have continued IPFS resolution support.

@adonesky1 adonesky1 requested a review from a team as a code owner December 1, 2021 21:21
@adonesky1 adonesky1 closed this Dec 2, 2021
@adonesky1 adonesky1 deleted the fix-ipfs-address-generation branch December 2, 2021 22:02
@adonesky1 adonesky1 restored the fix-ipfs-address-generation branch December 2, 2021 22:03
@adonesky1 adonesky1 reopened this Dec 2, 2021
wachunei
wachunei previously approved these changes Dec 2, 2021
Copy link
Member

@wachunei wachunei left a comment

Choose a reason for hiding this comment

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

LGTM

src/util.ts Outdated
// (CID is everything preceding first forward slash, path is everything after)
const index = url.indexOf('/');
const cid = index !== -1 ? url.substring(0, index) : url;
const path = index !== -1 ? url.substring(index) : '';
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Could we default to undefined here for path instead, if it's not present? That would match the type signature for this function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done here: d4c8ade

src/util.ts Outdated
Comment on lines 841 to 843
const gatewayHost = new URL(addUrlProtocolPrefix(ipfsGateway)).host;
const { cid, path } = getIpfsCIDv1AndPath(ipfsUrl);
return `https://${cid}.ipfs.${gatewayHost}${path}`;
Copy link
Member

Choose a reason for hiding this comment

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

It would be better to extract the protocol from the IPFS gateway as well, if it's specified. Users might want to use alternative protocols.

Suggested change
const gatewayHost = new URL(addUrlProtocolPrefix(ipfsGateway)).host;
const { cid, path } = getIpfsCIDv1AndPath(ipfsUrl);
return `https://${cid}.ipfs.${gatewayHost}${path}`;
const { host, protocol } = new URL(addUrlProtocolPrefix(ipfsGateway));
const { cid, path } = getIpfsCIDv1AndPath(ipfsUrl);
return `${protocol}//${cid}.ipfs.${host}${path}`;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done here: d4c8ade

src/util.ts Outdated
/**
* Formats url correctly for use retrieving assets hosted on IPFS.
*
* @param ipfsGateway - the user preferred ipfsGateway.
Copy link
Member

Choose a reason for hiding this comment

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

Nit: These descriptions could be better. Keep in mind that these aren't just code comments, they are inline documentation. This gets used to build our API documentation (which isn't automatically built/hosted right now, but it will be soon, and it can be generated manually). Better to use proper case (start the sentence with a capital letter, capitalize acronyms like IPFS, use words and not variable names, etc.), and to include relevant details like what sort of string this is.

For example, it looks like this is expected to be the URL for the IPFS gateway, except that the protocol is optional, and the trailing /ipfs path is optional.

The same general point applies to most of the other inline docs as well.

src/util.ts Outdated Show resolved Hide resolved
src/util.ts Outdated Show resolved Hide resolved
src/util.ts Show resolved Hide resolved
@Gudahtt
Copy link
Member

Gudahtt commented Dec 3, 2021

Overall this looks good! I left some suggestions for improvements

Copy link
Member

@Gudahtt Gudahtt left a comment

Choose a reason for hiding this comment

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

LGTM!

* @returns A URL with a https:// prepended.
*/
export function addUrlProtocolPrefix(urlString: string): string {
if (!urlString.match(/(^http:\/\/)|(^https:\/\/)/u)) {
Copy link
Member

Choose a reason for hiding this comment

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

NIt: This still only allows http and https protocols, so the protocol support added below only helps with HTTP support. We could instead check here to ensure any protocol is set.

This might be an extreme edge case though. I don't know of any IPFS gateways run on non-HTTP/HTTPS protocols 🤔 I don't know much about this space though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will research and handle in follow up PR!

);
});

it('should return a URL as is if https:// is already prepended', () => {
Copy link
Member

Choose a reason for hiding this comment

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

Nit: It's probably also worth ensuring that http URLs are handled correctly as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will handle in follow up PR!

@adonesky1 adonesky1 merged commit 06fb887 into main Dec 3, 2021
@adonesky1 adonesky1 deleted the fix-ipfs-address-generation branch December 3, 2021 17:43
MajorLift pushed a commit that referenced this pull request Oct 11, 2023
* use ipfs subdomain routes and convert cid's from v0 to v1

* add a couple more tests

* wip

* refactor

* wip

* tests fixed

* tests fixed

* addressing feedback

* improve comment
MajorLift pushed a commit that referenced this pull request Oct 11, 2023
* use ipfs subdomain routes and convert cid's from v0 to v1

* add a couple more tests

* wip

* refactor

* wip

* tests fixed

* tests fixed

* addressing feedback

* improve comment
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