Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Correctly check import target URL IP
- Loading branch information
1 parent
4afec73
commit f33e515
Showing
6 changed files
with
57 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { lookup } from 'dns' | ||
import { parse as parseIP } from 'ipaddr.js' | ||
|
||
function dnsLookupAll (hostname: string) { | ||
return new Promise<string[]>((res, rej) => { | ||
lookup(hostname, { family: 0, all: true }, (err, adresses) => { | ||
if (err) return rej(err) | ||
|
||
return res(adresses.map(a => a.address)) | ||
}) | ||
}) | ||
} | ||
|
||
async function isResolvingToUnicastOnly (hostname: string) { | ||
const addresses = await dnsLookupAll(hostname) | ||
|
||
for (const address of addresses) { | ||
const parsed = parseIP(address) | ||
|
||
if (parsed.range() !== 'unicast') return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
export { | ||
dnsLookupAll, | ||
isResolvingToUnicastOnly | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | ||
|
||
import 'mocha' | ||
import { expect } from 'chai' | ||
import { isResolvingToUnicastOnly } from '@server/helpers/dns' | ||
|
||
describe('DNS helpers', function () { | ||
|
||
it('Should correctly check unicast IPs', async function () { | ||
expect(await isResolvingToUnicastOnly('cpy.re')).to.be.true | ||
expect(await isResolvingToUnicastOnly('framasoft.org')).to.be.true | ||
expect(await isResolvingToUnicastOnly('8.8.8.8')).to.be.true | ||
|
||
expect(await isResolvingToUnicastOnly('127.0.0.1')).to.be.false | ||
expect(await isResolvingToUnicastOnly('127.0.0.1.cpy.re')).to.be.false | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import './image' | ||
import './core-utils' | ||
import './dns' | ||
import './comment-model' | ||
import './markdown' | ||
import './request' |