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: treat non-compliant SIP-016 metadata as invalid #266

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions src/token-processor/util/metadata-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,9 @@ function parseJsonMetadata(url: string, content?: string): RawMetadata {
try {
const result = JSON5.parse(content);
if (RawMetadataCType.Check(result)) {
if (result.name === undefined) {
throw new MetadataParseError(`Metadata does not contain a 'name' value: ${url}`);
}
return result;
} else {
throw new MetadataParseError(`Invalid raw metadata JSON schema: ${url}`);
Expand Down Expand Up @@ -356,15 +359,6 @@ export function getFetchableDecentralizedStorageUrl(uri: string): URL {
throw new MetadataParseError(`Unsupported uri protocol: ${uri}`);
}

function isUriFromDecentralizedStorage(uri: string): boolean {
return (
uri.startsWith('ipfs:') ||
uri.startsWith('ipns:') ||
uri.startsWith('ar:') ||
uri.startsWith('https://cloudflare-ipfs.com')
);
}

export function parseDataUrl(
s: string
):
Expand Down
23 changes: 22 additions & 1 deletion tests/token-queue/metadata-helpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MockAgent, setGlobalDispatcher } from 'undici';
import { ENV } from '../../src/env';
import { MetadataHttpError } from '../../src/token-processor/util/errors';
import { MetadataHttpError, MetadataParseError } from '../../src/token-processor/util/errors';
import {
getFetchableDecentralizedStorageUrl,
getMetadataFromUri,
Expand Down Expand Up @@ -100,6 +100,27 @@ describe('Metadata Helpers', () => {
).resolves.not.toThrow();
});

test('throws when metadata does not contain a name', async () => {
const crashPunks1 = {
sip: 16,
image: 'ipfs://Qmb84UcaMr1MUwNbYBnXWHM3kEaDcYrKuPWwyRLVTNKELC/294.png',
};
const agent = new MockAgent();
agent.disableNetConnect();
agent
.get('http://test.io')
.intercept({
path: '/1.json',
method: 'GET',
})
.reply(200, crashPunks1);
setGlobalDispatcher(agent);

await expect(getMetadataFromUri('http://test.io/1.json', 'ABCD.test', 1n)).rejects.toThrow(
MetadataParseError
);
});

test('fetches typed raw metadata', async () => {
const json = {
version: 1,
Expand Down
14 changes: 8 additions & 6 deletions tests/token-queue/process-token-job.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { RetryableJobError } from '../../src/token-processor/queue/errors';
import { TooManyRequestsHttpError } from '../../src/token-processor/util/errors';
import { cycleMigrations } from '@hirosystems/api-toolkit';
import { insertAndEnqueueTestContractWithTokens } from '../helpers';
import { InvalidTokenError } from '../../src/pg/errors';

describe('ProcessTokenJob', () => {
let db: PgStore;
Expand Down Expand Up @@ -683,7 +684,7 @@ describe('ProcessTokenJob', () => {
);
});

test('SIP-016 non-compliant metadata is ignored', async () => {
test('SIP-016 non-compliant metadata throws error', async () => {
const metadata = {
id: '62624cc0065e986192fb9f33',
media: 'https://sf-stage-s3.s3.us-west-1.amazonaws.com/riyasen_suit.png',
Expand Down Expand Up @@ -718,11 +719,12 @@ describe('ProcessTokenJob', () => {

await new ProcessTokenJob({ db, job: tokenJob }).work();

const bundle = await db.getTokenMetadataBundle({
contractPrincipal: 'ABCD.test-nft',
tokenNumber: 1,
});
expect(bundle?.metadataLocale).toBeUndefined();
await expect(
db.getTokenMetadataBundle({
contractPrincipal: 'ABCD.test-nft',
tokenNumber: 1,
})
).rejects.toThrow(InvalidTokenError);
});
});

Expand Down
Loading