mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-11-16 23:04:24 +08:00
feat(domain): 支持通配符域名及关联功能适配 (#63)
* feat(domain): 支持通配符域名及关联功能适配 * fix(domain): preserve legacy Unicode imports and address review findings * fix(domain): canonicalize legacy IDNs and exclude empty host labels * fix(domain): normalize equivalent Unicode IDN spellings --------- Co-authored-by: Ryan <63696351+Rain-kl@users.noreply.github.com> Co-authored-by: ryan <ryan@arctel.net>
This commit is contained in:
@@ -1,34 +1,118 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import { resolveZoneDomainInput } from '@/app/(main)/websites/components/resolve-zone-domain-input';
|
||||
import {
|
||||
previewZoneDomainInput,
|
||||
resolveZoneDomainInput,
|
||||
} from '@/app/(main)/websites/components/resolve-zone-domain-input';
|
||||
|
||||
const root = 'example.com';
|
||||
|
||||
describe('resolveZoneDomainInput', () => {
|
||||
it('maps short label and apex @ under zone root', () => {
|
||||
expect(resolveZoneDomainInput('api', 'example.com')).toEqual({
|
||||
domain: 'api.example.com',
|
||||
});
|
||||
expect(resolveZoneDomainInput('@', 'example.com')).toEqual({
|
||||
domain: 'example.com',
|
||||
it.each([
|
||||
['api', 'api.example.com'],
|
||||
['@', root],
|
||||
[root, root],
|
||||
['www.api.example.com', 'www.api.example.com'],
|
||||
['API.Example.COM', 'api.example.com'],
|
||||
['*', '*.example.com'],
|
||||
['*.example.com', '*.example.com'],
|
||||
['*.s3.example.com', '*.s3.example.com'],
|
||||
['*.S3.Example.COM', '*.s3.example.com'],
|
||||
[' *.Example.COM ', '*.example.com'],
|
||||
[' api ', 'api.example.com'],
|
||||
['*.example.com\n', '*.example.com'],
|
||||
['\t*.example.com', '*.example.com'],
|
||||
[`${'a'.repeat(63)}.example.com`, `${'a'.repeat(63)}.example.com`],
|
||||
])('resolves %s to %s', (input, domain) => {
|
||||
expect(resolveZoneDomainInput(input, root)).toEqual({ domain });
|
||||
expect(previewZoneDomainInput(input, root)).toBe(domain);
|
||||
});
|
||||
|
||||
it.each([
|
||||
'evil.com',
|
||||
'*.evil.com',
|
||||
'*.notexample.com',
|
||||
'*.example.com.evil.com',
|
||||
'*.s3',
|
||||
'api.s3',
|
||||
])(
|
||||
'rejects %s outside the zone without expanding dotted shorthand',
|
||||
(input) => {
|
||||
expect(resolveZoneDomainInput(input, root)).toEqual({
|
||||
domain: '',
|
||||
error: 'mustBelongToZone',
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it.each([
|
||||
'foo*',
|
||||
'foo*.example.com',
|
||||
'a.*',
|
||||
'a.*.example.com',
|
||||
'**',
|
||||
'**.example.com',
|
||||
'*.*.example.com',
|
||||
'*.s*.example.com',
|
||||
])('rejects wildcard outside the single leftmost label: %s', (input) => {
|
||||
expect(resolveZoneDomainInput(input, root)).toEqual({
|
||||
domain: '',
|
||||
error: 'noWildcard',
|
||||
});
|
||||
});
|
||||
|
||||
it('accepts full FQDN under the zone', () => {
|
||||
expect(
|
||||
resolveZoneDomainInput('www.api.example.com', 'example.com'),
|
||||
).toEqual({
|
||||
domain: 'www.api.example.com',
|
||||
});
|
||||
expect(resolveZoneDomainInput('example.com', 'example.com')).toEqual({
|
||||
domain: 'example.com',
|
||||
});
|
||||
it.each([
|
||||
'-api',
|
||||
'api-',
|
||||
'_api',
|
||||
'-api.example.com',
|
||||
'api-.example.com',
|
||||
'_api.example.com',
|
||||
'*.bad_.example.com',
|
||||
'*.-bad.example.com',
|
||||
'*.bad-.example.com',
|
||||
'..example.com',
|
||||
'*.example..com',
|
||||
'*.example.com.',
|
||||
'*.example.com:443',
|
||||
'*.example.com;other',
|
||||
'*.example.com/path',
|
||||
'*.example.com?query',
|
||||
'*.example.com#fragment',
|
||||
'https://*.example.com',
|
||||
'https://api.example.com',
|
||||
'user@api.example.com',
|
||||
'api.example.com:80',
|
||||
'api.example.com;other',
|
||||
'a b.example.com',
|
||||
'*.s3 .example.com',
|
||||
'*.exa\nmple.com',
|
||||
'*.s3\t.example.com',
|
||||
`${'a'.repeat(64)}.example.com`,
|
||||
`*.${'a'.repeat(64)}.example.com`,
|
||||
`${`${'a'.repeat(63)}.`.repeat(4)}example.com`,
|
||||
])('rejects malformed DNS names and unsafe input: %s', (input) => {
|
||||
expect(resolveZoneDomainInput(input, root).error).toBeTruthy();
|
||||
expect(previewZoneDomainInput(input, root)).toBe('');
|
||||
});
|
||||
|
||||
it('rejects foreign domains and wildcards', () => {
|
||||
expect(
|
||||
resolveZoneDomainInput('evil.com', 'example.com').error,
|
||||
).toBeTruthy();
|
||||
expect(
|
||||
resolveZoneDomainInput('*.example.com', 'example.com').error,
|
||||
).toBeTruthy();
|
||||
it('checks total length after expanding shorthand', () => {
|
||||
const longRoot = `${'a'.repeat(63)}.${'b'.repeat(63)}.${'c'.repeat(63)}.${'d'.repeat(61)}`;
|
||||
expect(longRoot).toHaveLength(253);
|
||||
expect(resolveZoneDomainInput('@', longRoot)).toEqual({ domain: longRoot });
|
||||
expect(resolveZoneDomainInput('*', longRoot).error).toBe('invalidFormat');
|
||||
expect(resolveZoneDomainInput('api', longRoot).error).toBe('invalidFormat');
|
||||
});
|
||||
|
||||
it('requires input and a selected, non-wildcard zone root', () => {
|
||||
expect(resolveZoneDomainInput('*', '').error).toBe('selectZone');
|
||||
expect(resolveZoneDomainInput('', root).error).toBe('enterDomain');
|
||||
expect(resolveZoneDomainInput(' ', root).error).toBe('enterDomain');
|
||||
expect(resolveZoneDomainInput('*', '*.example.com').error).toBe(
|
||||
'invalidFormat',
|
||||
);
|
||||
expect(resolveZoneDomainInput('*', 'Example.COM')).toEqual({
|
||||
domain: '*.example.com',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user