Skip to content

Commit

Permalink
wildcard expansion check
Browse files Browse the repository at this point in the history
  • Loading branch information
TateB committed Feb 26, 2024
1 parent f6bc82d commit 67f87d2
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 1 deletion.
8 changes: 8 additions & 0 deletions packages/ensjs/src/errors/dns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ export class DnsDnssecVerificationFailedError extends BaseError {
}
}

export class DnsDnssecWildcardExpansionError extends BaseError {
override name = 'DnsDnssecWildcardExpansionError'

constructor() {
super('DNSSEC wildcard expansion not supported')
}
}

export class DnsNoTxtRecordError extends BaseError {
override name = 'DnsNoTxtRecordError'

Expand Down
100 changes: 100 additions & 0 deletions packages/ensjs/src/functions/dns/getDnsOffchainData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ it('returns offchain data', async () => {
TTL: 0,
data: '"ENS1 0x238A8F792dFA6033814B18618aD4100654aeef01"',
},
{
name: 'example.com',
type: 46,
TTL: 0,
data: 'TXT ALGORITHM 2',
},
],
})

Expand Down Expand Up @@ -82,6 +88,12 @@ it('returns offchain data with extra data as address', async () => {
TTL: 0,
data: '"ENS1 0x238A8F792dFA6033814B18618aD4100654aeef01 0x8e8Db5CcEF88cca9d624701Db544989C996E3216"',
},
{
name: 'example.com',
type: 46,
TTL: 0,
data: 'TXT ALGORITHM 2',
},
],
})

Expand Down Expand Up @@ -109,6 +121,12 @@ it('returns offchain data with extra data as text', async () => {
TTL: 0,
data: '"ENS1 0x238A8F792dFA6033814B18618aD4100654aeef01 hello world"',
},
{
name: 'example.com',
type: 46,
TTL: 0,
data: 'TXT ALGORITHM 2',
},
],
})

Expand Down Expand Up @@ -136,6 +154,12 @@ it('returns offchain data from ens name', async () => {
TTL: 0,
data: '"ENS1 dnsname.ens.eth"',
},
{
name: 'example.com',
type: 46,
TTL: 0,
data: 'TXT ALGORITHM 2',
},
],
})

Expand Down Expand Up @@ -169,6 +193,12 @@ it('returns first offchain data from multiple', async () => {
TTL: 0,
data: '"ENS1 0x8e8Db5CcEF88cca9d624701Db544989C996E3216"',
},
{
name: 'example.com',
type: 46,
TTL: 0,
data: 'TXT ALGORITHM 2',
},
],
})

Expand Down Expand Up @@ -208,6 +238,12 @@ it('returns first valid offchain data when multiple invalid', async () => {
TTL: 0,
data: '"ENS1 0x238A8F792dFA6033814B18618aD4100654aeef01"',
},
{
name: 'example.com',
type: 46,
TTL: 0,
data: 'TXT ALGORITHM 2',
},
],
})

Expand Down Expand Up @@ -235,6 +271,12 @@ it('allows subname input', async () => {
TTL: 0,
data: '"ENS1 0x238A8F792dFA6033814B18618aD4100654aeef01"',
},
{
name: 'sub.example.com',
type: 46,
TTL: 0,
data: 'TXT ALGORITHM 3',
},
],
})

Expand Down Expand Up @@ -365,6 +407,52 @@ describe('no TXT records', () => {
})
})

describe('wildcard expansion', () => {
beforeEach(() => {
createHandlerResponse(handler, {
Status: 0,
AD: true,
Answer: [
{
name: 'example.com',
type: 16,
TTL: 0,
data: '"ENS1 0x238A8F792dFA6033814B18618aD4100654aeef01"',
},
{
name: 'example.com',
type: 46,
TTL: 0,
data: 'TXT ALGORITHM 1',
},
],
})
})

it('strict: throws error', async () => {
await expect(
getDnsOffchainData(mainnetPublicClient, {
name: 'example.com',
endpoint: serverUrl,
strict: true,
}),
).rejects.toThrowErrorMatchingInlineSnapshot(`
[DnsDnssecWildcardExpansionError: DNSSEC wildcard expansion not supported
Version: @ensdomains/[email protected]]
`)
})
it('not strict: returns null', async () => {
await expect(
getDnsOffchainData(mainnetPublicClient, {
name: 'example.com',
endpoint: serverUrl,
strict: false,
}),
).resolves.toBeNull()
})
})

describe('only invalid records', () => {
beforeEach(() => {
createHandlerResponse(handler, {
Expand All @@ -383,6 +471,12 @@ describe('only invalid records', () => {
TTL: 0,
data: '"ENS1 randomnonsense"',
},
{
name: 'example.com',
type: 46,
TTL: 0,
data: 'TXT ALGORITHM 2',
},
],
})
})
Expand Down Expand Up @@ -430,6 +524,12 @@ describe('no eligible invalid records', () => {
TTL: 0,
data: '"random"',
},
{
name: 'example.com',
type: 46,
TTL: 0,
data: 'TXT ALGORITHM 2',
},
],
})
})
Expand Down
13 changes: 13 additions & 0 deletions packages/ensjs/src/functions/dns/getDnsOffchainData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { isAddress, type Address, type Client, type Transport } from 'viem'
import type { ChainWithEns } from '../../contracts/consts.js'
import {
DnsDnssecVerificationFailedError,
DnsDnssecWildcardExpansionError,
DnsInvalidTxtRecordError,
DnsNoTxtRecordError,
DnsResponseStatusError,
Expand Down Expand Up @@ -110,6 +111,18 @@ const getDnsOffchainData = async (

if (!response.Answer?.length) throw new DnsNoTxtRecordError()

const labels = name.split('.')
const rrsigRecord = response.Answer.find((record) => {
if (record.type !== DnsRecordType.RRSIG) return false
if (record.name !== name) return false
if (!record.data.startsWith('TXT')) return false
const [, , labelCount] = record.data.split(' ')
// mismatching label count implies wildcard expansion, which is not supported
return Number(labelCount) === labels.length
})

if (!rrsigRecord) throw new DnsDnssecWildcardExpansionError()

const ensTxtRecords = await Promise.all(
response.Answer.map((record) => checkValidEnsTxtRecord(client, record)),
)
Expand Down
3 changes: 3 additions & 0 deletions packages/ensjs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export { BaseError } from './errors/base.js'
export { NoChainError, UnsupportedChainError } from './errors/contracts.js'
export {
DnsDnssecVerificationFailedError,
DnsDnssecWildcardExpansionError,
DnsInvalidAddressChecksumError,
DnsInvalidTxtRecordError,
DnsNewerRecordTypeAvailableError,
Expand Down Expand Up @@ -62,9 +63,11 @@ export {
FusesOutOfRangeError,
FusesRestrictionNotAllowedError,
FusesValueRequiredError,
InvalidContentHashError,
InvalidEncodedLabelError,
InvalidLabelhashError,
NameWithEmptyLabelsError,
RootNameIncludesOtherLabelsError,
UnknownContentTypeError,
WrappedLabelTooLargeError,
} from './errors/utils.js'
2 changes: 1 addition & 1 deletion packages/ensjs/src/utils/dns/getDnsTxtRecords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const getDnsTxtRecords = async ({
endpoint = 'https://cloudflare-dns.com/dns-query',
}: GetDnsTxtRecordsParameters): Promise<GetDnsTxtRecordsReturnType> => {
const response: DnsResponse = await fetch(
`${endpoint}?name=${name}.&type=TXT`,
`${endpoint}?name=${name}.&type=TXT&do=1`,
{
method: 'GET',
headers: {
Expand Down

0 comments on commit 67f87d2

Please sign in to comment.