diff --git a/packages/ui/certd-client/src/plugin/validator/__tests__/validator.spec.ts b/packages/ui/certd-client/src/plugin/validator/__tests__/validator.spec.ts new file mode 100644 index 00000000..133030c1 --- /dev/null +++ b/packages/ui/certd-client/src/plugin/validator/__tests__/validator.spec.ts @@ -0,0 +1,44 @@ +import { describe, expect, it } from "vitest"; +import { isDomain } from "/@/plugin/validator"; + +describe("domain_validator", () => { + it("ok", () => { + const value = ["a.cc.com", "*.zz.com", "a.cc.com"]; + const v = isDomain({}, value); + expect(v).to.be.true; + }); + + it("allowDotStart", () => { + let value = ["&.cc.com"]; + function test() { + return isDomain({ allowDotStart: true }, value); + } + expect(test).to.throw(Error, "域名有误:&.cc.com,请输入正确的域名"); + + value = ["a,cc.com"]; + expect(test).to.throw(Error, "域名有误:a,cc.com,请输入正确的域名"); + + value = ["&cc.com"]; + expect(test).to.throw(Error, "域名有误:&cc.com,请输入正确的域名"); + + value = [".cc.com"]; + expect(test()).to.be.true; + }); + + it("default", () => { + let value = ["&.cc.com"]; + function test() { + return isDomain({ allowDotStart: false }, value); + } + expect(test).to.throw(Error, "域名有误:&.cc.com,请输入正确的域名"); + + value = ["&cc.com"]; + expect(test).to.throw(Error, "域名有误:&cc.com,请输入正确的域名"); + + value = ["a,cc.com"]; + expect(test).to.throw(Error, "域名有误:a,cc.com,请输入正确的域名"); + + value = [".cc.com"]; + expect(test).to.throw(Error, "域名有误:.cc.com,请输入正确的域名"); + }); +}); diff --git a/packages/ui/certd-client/src/plugin/validator/index.ts b/packages/ui/certd-client/src/plugin/validator/index.ts index b0f0a156..b61d7cf1 100644 --- a/packages/ui/certd-client/src/plugin/validator/index.ts +++ b/packages/ui/certd-client/src/plugin/validator/index.ts @@ -1,6 +1,6 @@ import Validator from "async-validator"; // 自定义验证器函数 -function isDomain(rule, value) { +export function isDomain(rule: any, value: any) { if (value == null) { return true; } @@ -8,9 +8,14 @@ function isDomain(rule, value) { if (typeof value === "string") { domains = value.split(","); } + + const allowDotStart = rule.allowDotStart ? "\\.|" : ""; + const exp = `^(?:${allowDotStart}\\*\\.|[0-9a-zA-Z\u4e00-\u9fa5-]+\\.)+[0-9a-zA-Z\u4e00-\u9fa5-]+$`; + const compiled = new RegExp(exp); for (const domain of domains) { //域名可以是泛域名,中文域名,数字域名,英文域名,域名中可以包含-和. ,可以_开头 - if (!/^(?:\*\.|[0-9a-zA-Z\u4e00-\u9fa5-]+\.)+[0-9a-zA-Z\u4e00-\u9fa5-]+$/.test(domain)) { + + if (!compiled.test(domain)) { throw new Error(`域名有误:${domain},请输入正确的域名`); } } diff --git a/packages/ui/certd-server/src/plugins/plugin-qiniu/plugin/deploy-to-cdn/index.ts b/packages/ui/certd-server/src/plugins/plugin-qiniu/plugin/deploy-to-cdn/index.ts index 338486cf..97f655aa 100644 --- a/packages/ui/certd-server/src/plugins/plugin-qiniu/plugin/deploy-to-cdn/index.ts +++ b/packages/ui/certd-server/src/plugins/plugin-qiniu/plugin/deploy-to-cdn/index.ts @@ -7,7 +7,7 @@ import { CertInfo } from '@certd/plugin-cert'; title: '部署证书至七牛CDN', icon: 'svg:icon-qiniuyun', group: pluginGroups.cdn.key, - desc: '自动部署域名证书至七牛云CDN,七牛云OSS', + desc: '自动部署域名证书至七牛云CDN', default: { strategy: { runStrategy: RunStrategy.SkipWhenSucceed, @@ -25,7 +25,7 @@ export class QiniuDeployCertToCDN extends AbstractTaskPlugin { open: false, tokenSeparators: [',', ' ', ',', '、', '|'], }, - rules: [{ type: 'domains' }], + rules: [{ type: 'domains', allowDotStart: true }], required: true, }) domainName!: string | string[];