engine_test.go 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378
  1. package policy
  2. import (
  3. "crypto/x509"
  4. "crypto/x509/pkix"
  5. "errors"
  6. "net"
  7. "net/url"
  8. "reflect"
  9. "testing"
  10. "github.com/google/go-cmp/cmp"
  11. "github.com/stretchr/testify/assert"
  12. "golang.org/x/crypto/ssh"
  13. )
  14. // TODO(hs): the functionality in the policy engine is a nice candidate for trying fuzzing on
  15. // TODO(hs): more complex test use cases that combine multiple names and permitted/excluded entries?
  16. func TestNamePolicyEngine_matchDomainConstraint(t *testing.T) {
  17. tests := []struct {
  18. name string
  19. allowLiteralWildcardNames bool
  20. domain string
  21. constraint string
  22. want bool
  23. wantErr bool
  24. }{
  25. {
  26. name: "fail/wildcard",
  27. domain: "host.local",
  28. constraint: ".example.com", // internally we're using the x509 period prefix as the indicator for exactly one subdomain
  29. want: false,
  30. wantErr: false,
  31. },
  32. {
  33. name: "fail/wildcard-literal",
  34. domain: "*.example.com",
  35. constraint: ".example.com", // internally we're using the x509 period prefix as the indicator for exactly one subdomain
  36. want: false,
  37. wantErr: false,
  38. },
  39. {
  40. name: "fail/specific-domain",
  41. domain: "www.example.com",
  42. constraint: "host.example.com",
  43. want: false,
  44. wantErr: false,
  45. },
  46. {
  47. name: "fail/single-whitespace-domain",
  48. domain: " ",
  49. constraint: "host.example.com",
  50. want: false,
  51. wantErr: false,
  52. },
  53. {
  54. name: "fail/period-domain",
  55. domain: ".host.example.com",
  56. constraint: ".example.com",
  57. want: false,
  58. wantErr: false,
  59. },
  60. {
  61. name: "fail/wrong-asterisk-prefix",
  62. domain: "*Xexample.com",
  63. constraint: ".example.com",
  64. want: false,
  65. wantErr: false,
  66. },
  67. {
  68. name: "fail/asterisk-in-domain",
  69. domain: "e*ample.com",
  70. constraint: ".com",
  71. want: false,
  72. wantErr: false,
  73. },
  74. {
  75. name: "fail/asterisk-label",
  76. domain: "example.*.local",
  77. constraint: ".local",
  78. want: false,
  79. wantErr: false,
  80. },
  81. {
  82. name: "fail/multiple-periods",
  83. domain: "example.local",
  84. constraint: "..local",
  85. want: false,
  86. wantErr: false,
  87. },
  88. {
  89. name: "fail/error-parsing-domain",
  90. domain: string(byte(0)),
  91. constraint: ".local",
  92. want: false,
  93. wantErr: true,
  94. },
  95. {
  96. name: "fail/error-parsing-constraint",
  97. domain: "example.local",
  98. constraint: string(byte(0)),
  99. want: false,
  100. wantErr: true,
  101. },
  102. {
  103. name: "fail/no-subdomain",
  104. domain: "local",
  105. constraint: ".local",
  106. want: false,
  107. wantErr: false,
  108. },
  109. {
  110. name: "fail/too-many-subdomains",
  111. domain: "www.example.local",
  112. constraint: ".local",
  113. want: false,
  114. wantErr: false,
  115. },
  116. {
  117. name: "fail/wrong-domain",
  118. domain: "example.notlocal",
  119. constraint: ".local",
  120. want: false,
  121. wantErr: false,
  122. },
  123. {
  124. name: "false/idna-internationalized-domain-name",
  125. domain: "JP納豆.例.jp", // Example value from https://www.w3.org/International/articles/idn-and-iri/
  126. constraint: ".例.jp",
  127. want: false,
  128. wantErr: true,
  129. },
  130. {
  131. name: "false/idna-internationalized-domain-name-constraint",
  132. domain: "xn--jp-cd2fp15c.xn--fsq.jp", // Example value from https://www.w3.org/International/articles/idn-and-iri/
  133. constraint: ".例.jp",
  134. want: false,
  135. wantErr: true,
  136. },
  137. {
  138. name: "ok/empty-constraint",
  139. domain: "www.example.com",
  140. constraint: "",
  141. want: true,
  142. wantErr: false,
  143. },
  144. {
  145. name: "ok/wildcard",
  146. domain: "www.example.com",
  147. constraint: ".example.com", // internally we're using the x509 period prefix as the indicator for exactly one subdomain
  148. want: true,
  149. wantErr: false,
  150. },
  151. {
  152. name: "ok/wildcard-literal",
  153. allowLiteralWildcardNames: true,
  154. domain: "*.example.com", // specifically allowed using an option on the NamePolicyEngine
  155. constraint: ".example.com", // internally we're using the x509 period prefix as the indicator for exactly one subdomain
  156. want: true,
  157. wantErr: false,
  158. },
  159. {
  160. name: "ok/specific-domain",
  161. domain: "www.example.com",
  162. constraint: "www.example.com",
  163. want: true,
  164. wantErr: false,
  165. },
  166. {
  167. name: "ok/different-case",
  168. domain: "WWW.EXAMPLE.com",
  169. constraint: "www.example.com",
  170. want: true,
  171. wantErr: false,
  172. },
  173. {
  174. name: "ok/idna-internationalized-domain-name-punycode",
  175. domain: "xn--jp-cd2fp15c.xn--fsq.jp", // Example value from https://www.w3.org/International/articles/idn-and-iri/
  176. constraint: ".xn--fsq.jp",
  177. want: true,
  178. wantErr: false,
  179. },
  180. }
  181. for _, tt := range tests {
  182. t.Run(tt.name, func(t *testing.T) {
  183. engine := NamePolicyEngine{
  184. allowLiteralWildcardNames: tt.allowLiteralWildcardNames,
  185. }
  186. got, err := engine.matchDomainConstraint(tt.domain, tt.constraint)
  187. if (err != nil) != tt.wantErr {
  188. t.Errorf("NamePolicyEngine.matchDomainConstraint() error = %v, wantErr %v", err, tt.wantErr)
  189. return
  190. }
  191. if got != tt.want {
  192. t.Errorf("NamePolicyEngine.matchDomainConstraint() = %v, want %v", got, tt.want)
  193. }
  194. })
  195. }
  196. }
  197. func Test_matchIPConstraint(t *testing.T) {
  198. nat64IP, nat64Net, err := net.ParseCIDR("64:ff9b::/96")
  199. assert.NoError(t, err)
  200. tests := []struct {
  201. name string
  202. ip net.IP
  203. constraint *net.IPNet
  204. want bool
  205. wantErr bool
  206. }{
  207. {
  208. name: "false/ipv4-in-ipv6-nat64",
  209. ip: net.ParseIP("192.0.2.128"),
  210. constraint: nat64Net,
  211. want: false,
  212. wantErr: false,
  213. },
  214. {
  215. name: "ok/ipv4",
  216. ip: net.ParseIP("127.0.0.1"),
  217. constraint: &net.IPNet{
  218. IP: net.ParseIP("127.0.0.0"),
  219. Mask: net.IPv4Mask(255, 255, 255, 0),
  220. },
  221. want: true,
  222. wantErr: false,
  223. },
  224. {
  225. name: "ok/ipv6",
  226. ip: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7335"),
  227. constraint: &net.IPNet{
  228. IP: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  229. Mask: net.CIDRMask(120, 128),
  230. },
  231. want: true,
  232. wantErr: false,
  233. },
  234. {
  235. name: "ok/ipv4-in-ipv6", // ipv4 in ipv6 addresses are considered the same in the current implementation, because Go parses them as IPv4
  236. ip: net.ParseIP("::ffff:192.0.2.128"),
  237. constraint: &net.IPNet{
  238. IP: net.ParseIP("192.0.2.0"),
  239. Mask: net.IPv4Mask(255, 255, 255, 0),
  240. },
  241. want: true,
  242. wantErr: false,
  243. },
  244. {
  245. name: "ok/ipv4-in-ipv6-nat64-fixed-ip",
  246. ip: nat64IP,
  247. constraint: nat64Net,
  248. want: true,
  249. wantErr: false,
  250. },
  251. {
  252. name: "ok/ipv4-in-ipv6-nat64",
  253. ip: net.ParseIP("64:ff9b::192.0.2.129"),
  254. constraint: nat64Net,
  255. want: true,
  256. wantErr: false,
  257. },
  258. }
  259. for _, tt := range tests {
  260. t.Run(tt.name, func(t *testing.T) {
  261. got, err := matchIPConstraint(tt.ip, tt.constraint)
  262. if (err != nil) != tt.wantErr {
  263. t.Errorf("matchIPConstraint() error = %v, wantErr %v", err, tt.wantErr)
  264. return
  265. }
  266. if got != tt.want {
  267. t.Errorf("matchIPConstraint() = %v, want %v", got, tt.want)
  268. }
  269. })
  270. }
  271. }
  272. func TestNamePolicyEngine_matchEmailConstraint(t *testing.T) {
  273. tests := []struct {
  274. name string
  275. engine *NamePolicyEngine
  276. mailbox rfc2821Mailbox
  277. constraint string
  278. want bool
  279. wantErr bool
  280. }{
  281. {
  282. name: "fail/asterisk-prefix",
  283. engine: &NamePolicyEngine{},
  284. mailbox: rfc2821Mailbox{
  285. local: "mail",
  286. domain: "local",
  287. },
  288. constraint: "*@example.com",
  289. want: false,
  290. wantErr: true,
  291. },
  292. {
  293. name: "fail/asterisk-label",
  294. engine: &NamePolicyEngine{},
  295. mailbox: rfc2821Mailbox{
  296. local: "mail",
  297. domain: "local",
  298. },
  299. constraint: "@host.*.example.com",
  300. want: false,
  301. wantErr: true,
  302. },
  303. {
  304. name: "fail/asterisk-inside-local",
  305. engine: &NamePolicyEngine{},
  306. mailbox: rfc2821Mailbox{
  307. local: "mail",
  308. domain: "local",
  309. },
  310. constraint: "m*il@local",
  311. want: false,
  312. wantErr: true,
  313. },
  314. {
  315. name: "fail/asterisk-inside-domain",
  316. engine: &NamePolicyEngine{},
  317. mailbox: rfc2821Mailbox{
  318. local: "mail",
  319. domain: "local",
  320. },
  321. constraint: "@h*st.example.com",
  322. want: false,
  323. wantErr: true,
  324. },
  325. {
  326. name: "fail/parse-email",
  327. engine: &NamePolicyEngine{},
  328. mailbox: rfc2821Mailbox{
  329. local: "mail",
  330. domain: "local",
  331. },
  332. constraint: "@example.com",
  333. want: false,
  334. wantErr: true,
  335. },
  336. {
  337. name: "fail/wildcard",
  338. engine: &NamePolicyEngine{},
  339. mailbox: rfc2821Mailbox{
  340. local: "mail",
  341. domain: "local",
  342. },
  343. constraint: "example.com",
  344. want: false,
  345. wantErr: false,
  346. },
  347. {
  348. name: "fail/wildcard-x509-period",
  349. engine: &NamePolicyEngine{},
  350. mailbox: rfc2821Mailbox{
  351. local: "mail",
  352. domain: "local",
  353. },
  354. constraint: ".local", // "wildcard" for the local domain; requires exactly 1 subdomain
  355. want: false,
  356. wantErr: false,
  357. },
  358. {
  359. name: "fail/specific-mail-wrong-domain",
  360. engine: &NamePolicyEngine{},
  361. mailbox: rfc2821Mailbox{
  362. local: "mail",
  363. domain: "local",
  364. },
  365. constraint: "mail@example.com",
  366. want: false,
  367. wantErr: false,
  368. },
  369. {
  370. name: "fail/specific-mail-wrong-local",
  371. engine: &NamePolicyEngine{},
  372. mailbox: rfc2821Mailbox{
  373. local: "root",
  374. domain: "example.com",
  375. },
  376. constraint: "mail@example.com",
  377. want: false,
  378. wantErr: false,
  379. },
  380. {
  381. name: "ok/wildcard",
  382. engine: &NamePolicyEngine{},
  383. mailbox: rfc2821Mailbox{
  384. local: "mail",
  385. domain: "local",
  386. },
  387. constraint: "local", // "wildcard" for the local domain
  388. want: true,
  389. wantErr: false,
  390. },
  391. {
  392. name: "ok/wildcard-x509-period",
  393. engine: &NamePolicyEngine{},
  394. mailbox: rfc2821Mailbox{
  395. local: "mail",
  396. domain: "example.local",
  397. },
  398. constraint: ".local", // "wildcard" for the local domain; requires exactly 1 subdomain
  399. want: true,
  400. wantErr: false,
  401. },
  402. {
  403. name: "ok/specific-mail",
  404. engine: &NamePolicyEngine{},
  405. mailbox: rfc2821Mailbox{
  406. local: "mail",
  407. domain: "local",
  408. },
  409. constraint: "mail@local",
  410. want: true,
  411. wantErr: false,
  412. },
  413. {
  414. name: "ok/wildcard-tld",
  415. engine: &NamePolicyEngine{},
  416. mailbox: rfc2821Mailbox{
  417. local: "mail",
  418. domain: "example.com",
  419. },
  420. constraint: "example.com", // "wildcard" for 'example.com'
  421. want: true,
  422. wantErr: false,
  423. },
  424. {
  425. name: "ok/different-case",
  426. engine: &NamePolicyEngine{},
  427. mailbox: rfc2821Mailbox{
  428. local: "mail",
  429. domain: "EXAMPLE.com",
  430. },
  431. constraint: "example.com", // "wildcard" for 'example.com'
  432. want: true,
  433. wantErr: false,
  434. },
  435. }
  436. for _, tt := range tests {
  437. t.Run(tt.name, func(t *testing.T) {
  438. got, err := tt.engine.matchEmailConstraint(tt.mailbox, tt.constraint)
  439. if (err != nil) != tt.wantErr {
  440. t.Errorf("NamePolicyEngine.matchEmailConstraint() error = %v, wantErr %v", err, tt.wantErr)
  441. return
  442. }
  443. if got != tt.want {
  444. t.Errorf("NamePolicyEngine.matchEmailConstraint() = %v, want %v", got, tt.want)
  445. }
  446. })
  447. }
  448. }
  449. func TestNamePolicyEngine_matchURIConstraint(t *testing.T) {
  450. tests := []struct {
  451. name string
  452. engine *NamePolicyEngine
  453. uri *url.URL
  454. constraint string
  455. want bool
  456. wantErr bool
  457. }{
  458. {
  459. name: "fail/empty-host",
  460. engine: &NamePolicyEngine{},
  461. uri: &url.URL{
  462. Scheme: "https",
  463. Host: "",
  464. },
  465. constraint: ".local",
  466. want: false,
  467. wantErr: true,
  468. },
  469. {
  470. name: "fail/host-with-asterisk-prefix",
  471. engine: &NamePolicyEngine{},
  472. uri: &url.URL{
  473. Scheme: "https",
  474. Host: "*.local",
  475. },
  476. constraint: ".local",
  477. want: false,
  478. wantErr: true,
  479. },
  480. {
  481. name: "fail/host-with-asterisk-label",
  482. engine: &NamePolicyEngine{},
  483. uri: &url.URL{
  484. Scheme: "https",
  485. Host: "host.*.local",
  486. },
  487. constraint: ".local",
  488. want: false,
  489. wantErr: true,
  490. },
  491. {
  492. name: "fail/host-with-asterisk-inside",
  493. engine: &NamePolicyEngine{},
  494. uri: &url.URL{
  495. Scheme: "https",
  496. Host: "h*st.local",
  497. },
  498. constraint: ".local",
  499. want: false,
  500. wantErr: true,
  501. },
  502. {
  503. name: "fail/wildcard",
  504. engine: &NamePolicyEngine{},
  505. uri: &url.URL{
  506. Scheme: "https",
  507. Host: "www.example.notlocal",
  508. },
  509. constraint: ".example.local", // using x509 period as the "wildcard"; expects a single subdomain
  510. want: false,
  511. wantErr: false,
  512. },
  513. {
  514. name: "fail/wildcard-subdomains-too-deep",
  515. engine: &NamePolicyEngine{},
  516. uri: &url.URL{
  517. Scheme: "https",
  518. Host: "www.sub.example.local",
  519. },
  520. constraint: ".example.local", // using x509 period as the "wildcard"; expects a single subdomain
  521. want: false,
  522. wantErr: false,
  523. },
  524. {
  525. name: "fail/host-with-port-split-error",
  526. engine: &NamePolicyEngine{},
  527. uri: &url.URL{
  528. Scheme: "https",
  529. Host: "www.example.local::8080",
  530. },
  531. constraint: ".example.local", // using x509 period as the "wildcard"; expects a single subdomain
  532. want: false,
  533. wantErr: true,
  534. },
  535. {
  536. name: "fail/host-with-ipv4",
  537. engine: &NamePolicyEngine{},
  538. uri: &url.URL{
  539. Scheme: "https",
  540. Host: "127.0.0.1",
  541. },
  542. constraint: ".example.local", // using x509 period as the "wildcard"; expects a single subdomain
  543. want: false,
  544. wantErr: true,
  545. },
  546. {
  547. name: "fail/host-with-ipv6",
  548. engine: &NamePolicyEngine{},
  549. uri: &url.URL{
  550. Scheme: "https",
  551. Host: "[2001:0db8:85a3:0000:0000:8a2e:0370:7334]",
  552. },
  553. constraint: ".example.local", // using x509 period as the "wildcard"; expects a single subdomain
  554. want: false,
  555. wantErr: true,
  556. },
  557. {
  558. name: "ok/wildcard",
  559. engine: &NamePolicyEngine{},
  560. uri: &url.URL{
  561. Scheme: "https",
  562. Host: "www.example.local",
  563. },
  564. constraint: ".example.local", // using x509 period as the "wildcard"; expects a single subdomain
  565. want: true,
  566. wantErr: false,
  567. },
  568. {
  569. name: "ok/host-with-port",
  570. engine: &NamePolicyEngine{},
  571. uri: &url.URL{
  572. Scheme: "https",
  573. Host: "www.example.local:8080",
  574. },
  575. constraint: ".example.local", // using x509 period as the "wildcard"; expects a single subdomain
  576. want: true,
  577. wantErr: false,
  578. },
  579. {
  580. name: "ok/different-case",
  581. engine: &NamePolicyEngine{},
  582. uri: &url.URL{
  583. Scheme: "https",
  584. Host: "www.EXAMPLE.local",
  585. },
  586. constraint: ".example.local", // using x509 period as the "wildcard"; expects a single subdomain
  587. want: true,
  588. wantErr: false,
  589. },
  590. }
  591. for _, tt := range tests {
  592. t.Run(tt.name, func(t *testing.T) {
  593. got, err := tt.engine.matchURIConstraint(tt.uri, tt.constraint)
  594. if (err != nil) != tt.wantErr {
  595. t.Errorf("NamePolicyEngine.matchURIConstraint() error = %v, wantErr %v", err, tt.wantErr)
  596. return
  597. }
  598. if got != tt.want {
  599. t.Errorf("NamePolicyEngine.matchURIConstraint() = %v, want %v", got, tt.want)
  600. }
  601. })
  602. }
  603. }
  604. func TestNamePolicyEngine_X509_AllAllowed(t *testing.T) {
  605. tests := []struct {
  606. name string
  607. options []NamePolicyOption
  608. cert *x509.Certificate
  609. want bool
  610. wantErr *NamePolicyError
  611. }{
  612. // SINGLE SAN TYPE PERMITTED FAILURE TESTS
  613. {
  614. name: "fail/dns-permitted",
  615. options: []NamePolicyOption{
  616. WithPermittedDNSDomains("*.local"),
  617. },
  618. cert: &x509.Certificate{
  619. DNSNames: []string{"www.example.com"},
  620. },
  621. want: false,
  622. wantErr: &NamePolicyError{
  623. Reason: NotAllowed,
  624. NameType: DNSNameType,
  625. Name: "www.example.com",
  626. },
  627. },
  628. {
  629. name: "fail/dns-permitted-wildcard-literal-x509",
  630. options: []NamePolicyOption{
  631. WithPermittedDNSDomains("*.x509local"),
  632. },
  633. cert: &x509.Certificate{
  634. DNSNames: []string{
  635. "*.x509local",
  636. },
  637. },
  638. want: false,
  639. wantErr: &NamePolicyError{
  640. Reason: NotAllowed,
  641. NameType: DNSNameType,
  642. Name: "*.x509local",
  643. },
  644. },
  645. {
  646. name: "fail/dns-permitted-single-host",
  647. options: []NamePolicyOption{
  648. WithPermittedDNSDomains("host.local"),
  649. },
  650. cert: &x509.Certificate{
  651. DNSNames: []string{"differenthost.local"},
  652. },
  653. want: false,
  654. wantErr: &NamePolicyError{
  655. Reason: NotAllowed,
  656. NameType: DNSNameType,
  657. Name: "differenthost.local",
  658. },
  659. },
  660. {
  661. name: "fail/dns-permitted-no-label",
  662. options: []NamePolicyOption{
  663. WithPermittedDNSDomains("*.local"),
  664. },
  665. cert: &x509.Certificate{
  666. DNSNames: []string{"local"},
  667. },
  668. want: false,
  669. wantErr: &NamePolicyError{
  670. Reason: NotAllowed,
  671. NameType: DNSNameType,
  672. Name: "local",
  673. },
  674. },
  675. {
  676. name: "fail/dns-permitted-empty-label",
  677. options: []NamePolicyOption{
  678. WithPermittedDNSDomains("*.local"),
  679. },
  680. cert: &x509.Certificate{
  681. DNSNames: []string{"www..local"},
  682. },
  683. want: false,
  684. wantErr: &NamePolicyError{
  685. Reason: CannotParseDomain,
  686. NameType: DNSNameType,
  687. Name: "www..local",
  688. },
  689. },
  690. {
  691. name: "fail/dns-permitted-dot-domain",
  692. options: []NamePolicyOption{
  693. WithPermittedDNSDomains("*.local"),
  694. },
  695. cert: &x509.Certificate{
  696. DNSNames: []string{
  697. ".local",
  698. },
  699. },
  700. want: false,
  701. wantErr: &NamePolicyError{
  702. Reason: NotAllowed,
  703. NameType: DNSNameType,
  704. Name: ".local",
  705. },
  706. },
  707. {
  708. name: "fail/dns-permitted-wildcard-multiple-subdomains",
  709. options: []NamePolicyOption{
  710. WithPermittedDNSDomains("*.local"),
  711. },
  712. cert: &x509.Certificate{
  713. DNSNames: []string{
  714. "sub.example.local",
  715. },
  716. },
  717. want: false,
  718. wantErr: &NamePolicyError{
  719. Reason: NotAllowed,
  720. NameType: DNSNameType,
  721. Name: "sub.example.local",
  722. },
  723. },
  724. {
  725. name: "fail/dns-permitted-wildcard-literal",
  726. options: []NamePolicyOption{
  727. WithPermittedDNSDomains("*.local"),
  728. },
  729. cert: &x509.Certificate{
  730. DNSNames: []string{
  731. "*.local",
  732. },
  733. },
  734. want: false,
  735. wantErr: &NamePolicyError{
  736. Reason: NotAllowed,
  737. NameType: DNSNameType,
  738. Name: "*.local",
  739. },
  740. },
  741. {
  742. name: "fail/dns-permitted-idna-internationalized-domain",
  743. options: []NamePolicyOption{
  744. WithPermittedDNSDomains("*.豆.jp"),
  745. },
  746. cert: &x509.Certificate{
  747. DNSNames: []string{
  748. string(byte(0)) + ".例.jp",
  749. },
  750. },
  751. want: false,
  752. wantErr: &NamePolicyError{
  753. Reason: CannotParseDomain,
  754. NameType: DNSNameType,
  755. Name: string(byte(0)) + ".例.jp",
  756. },
  757. },
  758. {
  759. name: "fail/ipv4-permitted",
  760. options: []NamePolicyOption{
  761. WithPermittedIPRanges(
  762. &net.IPNet{
  763. IP: net.ParseIP("127.0.0.1"),
  764. Mask: net.IPv4Mask(255, 255, 255, 0),
  765. },
  766. ),
  767. },
  768. cert: &x509.Certificate{
  769. IPAddresses: []net.IP{net.ParseIP("1.1.1.1")},
  770. },
  771. want: false,
  772. wantErr: &NamePolicyError{
  773. Reason: NotAllowed,
  774. NameType: IPNameType,
  775. Name: "1.1.1.1",
  776. },
  777. },
  778. {
  779. name: "fail/ipv6-permitted",
  780. options: []NamePolicyOption{
  781. WithPermittedIPRanges(
  782. &net.IPNet{
  783. IP: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  784. Mask: net.CIDRMask(120, 128),
  785. },
  786. ),
  787. },
  788. cert: &x509.Certificate{
  789. IPAddresses: []net.IP{net.ParseIP("3001:0db8:85a3:0000:0000:8a2e:0370:7334")},
  790. },
  791. want: false,
  792. wantErr: &NamePolicyError{
  793. Reason: NotAllowed,
  794. NameType: IPNameType,
  795. Name: "3001:db8:85a3::8a2e:370:7334", // IPv6 is shortened internally
  796. },
  797. },
  798. {
  799. name: "fail/mail-permitted-wildcard",
  800. options: []NamePolicyOption{
  801. WithPermittedEmailAddresses("@example.com"),
  802. },
  803. cert: &x509.Certificate{
  804. EmailAddresses: []string{
  805. "test@local.com",
  806. },
  807. },
  808. want: false,
  809. wantErr: &NamePolicyError{
  810. Reason: NotAllowed,
  811. NameType: EmailNameType,
  812. Name: "test@local.com",
  813. },
  814. },
  815. {
  816. name: "fail/mail-permitted-wildcard-x509",
  817. options: []NamePolicyOption{
  818. WithPermittedEmailAddresses("example.com"),
  819. },
  820. cert: &x509.Certificate{
  821. EmailAddresses: []string{
  822. "test@local.com",
  823. },
  824. },
  825. want: false,
  826. wantErr: &NamePolicyError{
  827. Reason: NotAllowed,
  828. NameType: EmailNameType,
  829. Name: "test@local.com",
  830. },
  831. },
  832. {
  833. name: "fail/mail-permitted-specific-mailbox",
  834. options: []NamePolicyOption{
  835. WithPermittedEmailAddresses("test@local.com"),
  836. },
  837. cert: &x509.Certificate{
  838. EmailAddresses: []string{
  839. "root@local.com",
  840. },
  841. },
  842. want: false,
  843. wantErr: &NamePolicyError{
  844. Reason: NotAllowed,
  845. NameType: EmailNameType,
  846. Name: "root@local.com",
  847. },
  848. },
  849. {
  850. name: "fail/mail-permitted-wildcard-subdomain",
  851. options: []NamePolicyOption{
  852. WithPermittedEmailAddresses("@example.com"),
  853. },
  854. cert: &x509.Certificate{
  855. EmailAddresses: []string{
  856. "test@sub.example.com",
  857. },
  858. },
  859. want: false,
  860. wantErr: &NamePolicyError{
  861. Reason: NotAllowed,
  862. NameType: EmailNameType,
  863. Name: "test@sub.example.com",
  864. },
  865. },
  866. {
  867. name: "fail/mail-permitted-idna-internationalized-domain",
  868. options: []NamePolicyOption{
  869. WithPermittedEmailAddresses("@例.jp"),
  870. },
  871. cert: &x509.Certificate{
  872. EmailAddresses: []string{"bücher@例.jp"},
  873. },
  874. want: false,
  875. wantErr: &NamePolicyError{
  876. Reason: CannotParseRFC822Name,
  877. NameType: EmailNameType,
  878. Name: "bücher@例.jp",
  879. },
  880. },
  881. {
  882. name: "fail/mail-permitted-idna-internationalized-domain-rfc822",
  883. options: []NamePolicyOption{
  884. WithPermittedEmailAddresses("@例.jp"),
  885. },
  886. cert: &x509.Certificate{
  887. EmailAddresses: []string{"bücher@例.jp" + string(byte(0))},
  888. },
  889. want: false,
  890. wantErr: &NamePolicyError{
  891. Reason: CannotParseRFC822Name,
  892. NameType: EmailNameType,
  893. Name: "bücher@例.jp" + string(byte(0)),
  894. },
  895. },
  896. {
  897. name: "fail/mail-permitted-idna-internationalized-domain-ascii",
  898. options: []NamePolicyOption{
  899. WithPermittedEmailAddresses("@例.jp"),
  900. },
  901. cert: &x509.Certificate{
  902. EmailAddresses: []string{"mail@xn---bla.jp"},
  903. },
  904. want: false,
  905. wantErr: &NamePolicyError{
  906. Reason: CannotParseDomain,
  907. NameType: EmailNameType,
  908. Name: "mail@xn---bla.jp",
  909. },
  910. },
  911. {
  912. name: "fail/uri-permitted-domain-wildcard",
  913. options: []NamePolicyOption{
  914. WithPermittedURIDomains("*.local"),
  915. },
  916. cert: &x509.Certificate{
  917. URIs: []*url.URL{
  918. {
  919. Scheme: "https",
  920. Host: "example.com",
  921. },
  922. },
  923. },
  924. want: false,
  925. wantErr: &NamePolicyError{
  926. Reason: NotAllowed,
  927. NameType: URINameType,
  928. Name: "https://example.com",
  929. },
  930. },
  931. {
  932. name: "fail/uri-permitted",
  933. options: []NamePolicyOption{
  934. WithPermittedURIDomains("test.local"),
  935. },
  936. cert: &x509.Certificate{
  937. URIs: []*url.URL{
  938. {
  939. Scheme: "https",
  940. Host: "bad.local",
  941. },
  942. },
  943. },
  944. want: false,
  945. wantErr: &NamePolicyError{
  946. Reason: NotAllowed,
  947. NameType: URINameType,
  948. Name: "https://bad.local",
  949. },
  950. },
  951. {
  952. name: "fail/uri-permitted-with-literal-wildcard", // don't allow literal wildcard in URI, e.g. xxxx://*.domain.tld
  953. options: []NamePolicyOption{
  954. WithPermittedURIDomains("*.local"),
  955. },
  956. cert: &x509.Certificate{
  957. URIs: []*url.URL{
  958. {
  959. Scheme: "https",
  960. Host: "*.local",
  961. },
  962. },
  963. },
  964. want: false,
  965. wantErr: &NamePolicyError{
  966. Reason: CannotMatchNameToConstraint,
  967. NameType: URINameType,
  968. Name: "https://*.local",
  969. },
  970. },
  971. {
  972. name: "fail/uri-permitted-idna-internationalized-domain",
  973. options: []NamePolicyOption{
  974. WithPermittedURIDomains("*.bücher.example.com"),
  975. },
  976. cert: &x509.Certificate{
  977. URIs: []*url.URL{
  978. {
  979. Scheme: "https",
  980. Host: "abc.bücher.example.com",
  981. },
  982. },
  983. },
  984. want: false,
  985. wantErr: &NamePolicyError{
  986. Reason: CannotMatchNameToConstraint,
  987. NameType: URINameType,
  988. Name: "https://abc.b%C3%BCcher.example.com",
  989. },
  990. },
  991. // SINGLE SAN TYPE EXCLUDED FAILURE TESTS
  992. {
  993. name: "fail/dns-excluded",
  994. options: []NamePolicyOption{
  995. WithExcludedDNSDomains("*.example.com"),
  996. },
  997. cert: &x509.Certificate{
  998. DNSNames: []string{"www.example.com"},
  999. },
  1000. want: false,
  1001. wantErr: &NamePolicyError{
  1002. Reason: NotAllowed,
  1003. NameType: DNSNameType,
  1004. Name: "www.example.com",
  1005. },
  1006. },
  1007. {
  1008. name: "fail/dns-excluded-single-host",
  1009. options: []NamePolicyOption{
  1010. WithExcludedDNSDomains("host.example.com"),
  1011. },
  1012. cert: &x509.Certificate{
  1013. DNSNames: []string{"host.example.com"},
  1014. },
  1015. want: false,
  1016. wantErr: &NamePolicyError{
  1017. Reason: NotAllowed,
  1018. NameType: DNSNameType,
  1019. Name: "host.example.com",
  1020. },
  1021. },
  1022. {
  1023. name: "fail/ipv4-excluded",
  1024. options: []NamePolicyOption{
  1025. WithExcludedIPRanges(
  1026. &net.IPNet{
  1027. IP: net.ParseIP("127.0.0.1"),
  1028. Mask: net.IPv4Mask(255, 255, 255, 0),
  1029. },
  1030. ),
  1031. },
  1032. cert: &x509.Certificate{
  1033. IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
  1034. },
  1035. want: false,
  1036. wantErr: &NamePolicyError{
  1037. Reason: NotAllowed,
  1038. NameType: IPNameType,
  1039. Name: "127.0.0.1",
  1040. },
  1041. },
  1042. {
  1043. name: "fail/ipv6-excluded",
  1044. options: []NamePolicyOption{
  1045. WithExcludedIPRanges(
  1046. &net.IPNet{
  1047. IP: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  1048. Mask: net.CIDRMask(120, 128),
  1049. },
  1050. ),
  1051. },
  1052. cert: &x509.Certificate{
  1053. IPAddresses: []net.IP{net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334")},
  1054. },
  1055. want: false,
  1056. wantErr: &NamePolicyError{
  1057. Reason: NotAllowed,
  1058. NameType: IPNameType,
  1059. Name: "2001:db8:85a3::8a2e:370:7334",
  1060. },
  1061. },
  1062. {
  1063. name: "fail/mail-excluded",
  1064. options: []NamePolicyOption{
  1065. WithExcludedEmailAddresses("@example.com"),
  1066. },
  1067. cert: &x509.Certificate{
  1068. EmailAddresses: []string{"mail@example.com"},
  1069. },
  1070. want: false,
  1071. wantErr: &NamePolicyError{
  1072. Reason: NotAllowed,
  1073. NameType: EmailNameType,
  1074. Name: "mail@example.com",
  1075. },
  1076. },
  1077. {
  1078. name: "fail/uri-excluded",
  1079. options: []NamePolicyOption{
  1080. WithExcludedURIDomains("*.example.com"),
  1081. },
  1082. cert: &x509.Certificate{
  1083. URIs: []*url.URL{
  1084. {
  1085. Scheme: "https",
  1086. Host: "www.example.com",
  1087. },
  1088. },
  1089. },
  1090. want: false,
  1091. wantErr: &NamePolicyError{
  1092. Reason: NotAllowed,
  1093. NameType: URINameType,
  1094. Name: "https://www.example.com",
  1095. },
  1096. },
  1097. {
  1098. name: "fail/uri-excluded-with-literal-wildcard", // don't allow literal wildcard in URI, e.g. xxxx://*.domain.tld
  1099. options: []NamePolicyOption{
  1100. WithExcludedURIDomains("*.local"),
  1101. },
  1102. cert: &x509.Certificate{
  1103. URIs: []*url.URL{
  1104. {
  1105. Scheme: "https",
  1106. Host: "*.local",
  1107. },
  1108. },
  1109. },
  1110. want: false,
  1111. wantErr: &NamePolicyError{
  1112. Reason: CannotMatchNameToConstraint,
  1113. NameType: URINameType,
  1114. Name: "https://*.local",
  1115. },
  1116. },
  1117. // SUBJECT FAILURE TESTS
  1118. {
  1119. name: "fail/subject-permitted-no-match",
  1120. options: []NamePolicyOption{
  1121. WithSubjectCommonNameVerification(),
  1122. WithPermittedCommonNames("this name is allowed", "and this one too"),
  1123. },
  1124. cert: &x509.Certificate{
  1125. Subject: pkix.Name{
  1126. CommonName: "some certificate name",
  1127. },
  1128. },
  1129. want: false,
  1130. wantErr: &NamePolicyError{
  1131. Reason: NotAllowed, // only permitted names allowed
  1132. NameType: CNNameType,
  1133. Name: "some certificate name",
  1134. },
  1135. },
  1136. {
  1137. name: "fail/subject-excluded-match",
  1138. options: []NamePolicyOption{
  1139. WithSubjectCommonNameVerification(),
  1140. WithExcludedCommonNames("this name is not allowed"),
  1141. },
  1142. cert: &x509.Certificate{
  1143. Subject: pkix.Name{
  1144. CommonName: "this name is not allowed",
  1145. },
  1146. },
  1147. want: false,
  1148. wantErr: &NamePolicyError{
  1149. Reason: CannotParseDomain, // CN cannot be parsed as DNS in this case
  1150. NameType: CNNameType,
  1151. Name: "this name is not allowed",
  1152. },
  1153. },
  1154. {
  1155. name: "fail/subject-dns-no-domain",
  1156. options: []NamePolicyOption{
  1157. WithSubjectCommonNameVerification(),
  1158. WithPermittedDNSDomains("*.local"),
  1159. },
  1160. cert: &x509.Certificate{
  1161. Subject: pkix.Name{
  1162. CommonName: "name with space.local",
  1163. },
  1164. },
  1165. want: false,
  1166. wantErr: &NamePolicyError{
  1167. Reason: CannotParseDomain,
  1168. NameType: CNNameType,
  1169. Name: "name with space.local",
  1170. },
  1171. },
  1172. {
  1173. name: "fail/subject-dns-permitted",
  1174. options: []NamePolicyOption{
  1175. WithSubjectCommonNameVerification(),
  1176. WithPermittedDNSDomains("*.local"),
  1177. },
  1178. cert: &x509.Certificate{
  1179. Subject: pkix.Name{
  1180. CommonName: "example.notlocal",
  1181. },
  1182. },
  1183. want: false,
  1184. wantErr: &NamePolicyError{
  1185. Reason: NotAllowed,
  1186. NameType: CNNameType,
  1187. Name: "example.notlocal",
  1188. },
  1189. },
  1190. {
  1191. name: "fail/subject-dns-excluded",
  1192. options: []NamePolicyOption{
  1193. WithSubjectCommonNameVerification(),
  1194. WithExcludedDNSDomains("*.local"),
  1195. },
  1196. cert: &x509.Certificate{
  1197. Subject: pkix.Name{
  1198. CommonName: "example.local",
  1199. },
  1200. },
  1201. want: false,
  1202. wantErr: &NamePolicyError{
  1203. Reason: NotAllowed,
  1204. NameType: CNNameType,
  1205. Name: "example.local",
  1206. },
  1207. },
  1208. {
  1209. name: "fail/subject-ipv4-permitted",
  1210. options: []NamePolicyOption{
  1211. WithSubjectCommonNameVerification(),
  1212. WithPermittedIPRanges(
  1213. &net.IPNet{
  1214. IP: net.ParseIP("127.0.0.1"),
  1215. Mask: net.IPv4Mask(255, 255, 255, 0),
  1216. },
  1217. ),
  1218. },
  1219. cert: &x509.Certificate{
  1220. Subject: pkix.Name{
  1221. CommonName: "10.10.10.10",
  1222. },
  1223. },
  1224. want: false,
  1225. wantErr: &NamePolicyError{
  1226. Reason: NotAllowed,
  1227. NameType: CNNameType,
  1228. Name: "10.10.10.10",
  1229. },
  1230. },
  1231. {
  1232. name: "fail/subject-ipv4-excluded",
  1233. options: []NamePolicyOption{
  1234. WithSubjectCommonNameVerification(),
  1235. WithExcludedIPRanges(
  1236. &net.IPNet{
  1237. IP: net.ParseIP("127.0.0.1"),
  1238. Mask: net.IPv4Mask(255, 255, 255, 0),
  1239. },
  1240. ),
  1241. },
  1242. cert: &x509.Certificate{
  1243. Subject: pkix.Name{
  1244. CommonName: "127.0.0.30",
  1245. },
  1246. },
  1247. want: false,
  1248. wantErr: &NamePolicyError{
  1249. Reason: NotAllowed,
  1250. NameType: CNNameType,
  1251. Name: "127.0.0.30",
  1252. },
  1253. },
  1254. {
  1255. name: "fail/subject-ipv6-permitted",
  1256. options: []NamePolicyOption{
  1257. WithSubjectCommonNameVerification(),
  1258. WithPermittedIPRanges(
  1259. &net.IPNet{
  1260. IP: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  1261. Mask: net.CIDRMask(120, 128),
  1262. },
  1263. ),
  1264. },
  1265. cert: &x509.Certificate{
  1266. Subject: pkix.Name{
  1267. CommonName: "2002:0db8:85a3:0000:0000:8a2e:0370:7339",
  1268. },
  1269. },
  1270. want: false,
  1271. wantErr: &NamePolicyError{
  1272. Reason: NotAllowed,
  1273. NameType: CNNameType,
  1274. Name: "2002:db8:85a3::8a2e:370:7339",
  1275. },
  1276. },
  1277. {
  1278. name: "fail/subject-ipv6-excluded",
  1279. options: []NamePolicyOption{
  1280. WithSubjectCommonNameVerification(),
  1281. WithExcludedIPRanges(
  1282. &net.IPNet{
  1283. IP: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  1284. Mask: net.CIDRMask(120, 128),
  1285. },
  1286. ),
  1287. },
  1288. cert: &x509.Certificate{
  1289. Subject: pkix.Name{
  1290. CommonName: "2001:0db8:85a3:0000:0000:8a2e:0370:7339",
  1291. },
  1292. },
  1293. want: false,
  1294. wantErr: &NamePolicyError{
  1295. Reason: NotAllowed,
  1296. NameType: CNNameType,
  1297. Name: "2001:db8:85a3::8a2e:370:7339",
  1298. },
  1299. },
  1300. {
  1301. name: "fail/subject-email-permitted",
  1302. options: []NamePolicyOption{
  1303. WithSubjectCommonNameVerification(),
  1304. WithPermittedEmailAddresses("@example.local"),
  1305. },
  1306. cert: &x509.Certificate{
  1307. Subject: pkix.Name{
  1308. CommonName: "mail@smallstep.com",
  1309. },
  1310. },
  1311. want: false,
  1312. wantErr: &NamePolicyError{
  1313. Reason: NotAllowed,
  1314. NameType: CNNameType,
  1315. Name: "mail@smallstep.com",
  1316. },
  1317. },
  1318. {
  1319. name: "fail/subject-email-excluded",
  1320. options: []NamePolicyOption{
  1321. WithSubjectCommonNameVerification(),
  1322. WithExcludedEmailAddresses("@example.local"),
  1323. },
  1324. cert: &x509.Certificate{
  1325. Subject: pkix.Name{
  1326. CommonName: "mail@example.local",
  1327. },
  1328. },
  1329. want: false,
  1330. wantErr: &NamePolicyError{
  1331. Reason: NotAllowed,
  1332. NameType: CNNameType,
  1333. Name: "mail@example.local",
  1334. },
  1335. },
  1336. {
  1337. name: "fail/subject-uri-permitted",
  1338. options: []NamePolicyOption{
  1339. WithSubjectCommonNameVerification(),
  1340. WithPermittedURIDomains("*.example.com"),
  1341. },
  1342. cert: &x509.Certificate{
  1343. Subject: pkix.Name{
  1344. CommonName: "https://www.google.com",
  1345. },
  1346. },
  1347. want: false,
  1348. wantErr: &NamePolicyError{
  1349. Reason: NotAllowed,
  1350. NameType: CNNameType,
  1351. Name: "https://www.google.com",
  1352. },
  1353. },
  1354. {
  1355. name: "fail/subject-uri-excluded",
  1356. options: []NamePolicyOption{
  1357. WithSubjectCommonNameVerification(),
  1358. WithExcludedURIDomains("*.example.com"),
  1359. },
  1360. cert: &x509.Certificate{
  1361. Subject: pkix.Name{
  1362. CommonName: "https://www.example.com",
  1363. },
  1364. },
  1365. want: false,
  1366. wantErr: &NamePolicyError{
  1367. Reason: NotAllowed,
  1368. NameType: CNNameType,
  1369. Name: "https://www.example.com",
  1370. },
  1371. },
  1372. // DIFFERENT SAN PERMITTED FAILURE TESTS
  1373. {
  1374. name: "fail/dns-permitted-with-ip-name", // when only DNS is permitted, IPs are not allowed.
  1375. options: []NamePolicyOption{
  1376. WithPermittedDNSDomains("*.local"),
  1377. },
  1378. cert: &x509.Certificate{
  1379. IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
  1380. },
  1381. want: false,
  1382. wantErr: &NamePolicyError{
  1383. Reason: NotAllowed,
  1384. NameType: IPNameType,
  1385. Name: "127.0.0.1",
  1386. },
  1387. },
  1388. {
  1389. name: "fail/dns-permitted-with-mail", // when only DNS is permitted, mails are not allowed.
  1390. options: []NamePolicyOption{
  1391. WithPermittedDNSDomains("*.local"),
  1392. },
  1393. cert: &x509.Certificate{
  1394. EmailAddresses: []string{"mail@smallstep.com"},
  1395. },
  1396. want: false,
  1397. wantErr: &NamePolicyError{
  1398. Reason: NotAllowed,
  1399. NameType: EmailNameType,
  1400. Name: "mail@smallstep.com",
  1401. },
  1402. },
  1403. {
  1404. name: "fail/dns-permitted-with-uri", // when only DNS is permitted, URIs are not allowed.
  1405. options: []NamePolicyOption{
  1406. WithPermittedDNSDomains("*.local"),
  1407. },
  1408. cert: &x509.Certificate{
  1409. URIs: []*url.URL{
  1410. {
  1411. Scheme: "https",
  1412. Host: "www.example.com",
  1413. },
  1414. },
  1415. },
  1416. want: false,
  1417. wantErr: &NamePolicyError{
  1418. Reason: NotAllowed,
  1419. NameType: URINameType,
  1420. Name: "https://www.example.com",
  1421. },
  1422. },
  1423. {
  1424. name: "fail/ip-permitted-with-dns-name", // when only IP is permitted, DNS names are not allowed.
  1425. options: []NamePolicyOption{
  1426. WithPermittedIPRanges(
  1427. &net.IPNet{
  1428. IP: net.ParseIP("127.0.0.1"),
  1429. Mask: net.IPv4Mask(255, 255, 255, 0),
  1430. },
  1431. ),
  1432. },
  1433. cert: &x509.Certificate{
  1434. DNSNames: []string{"www.example.com"},
  1435. },
  1436. want: false,
  1437. wantErr: &NamePolicyError{
  1438. Reason: NotAllowed,
  1439. NameType: DNSNameType,
  1440. Name: "www.example.com",
  1441. },
  1442. },
  1443. {
  1444. name: "fail/ip-permitted-with-mail", // when only IP is permitted, mails are not allowed.
  1445. options: []NamePolicyOption{
  1446. WithPermittedIPRanges(
  1447. &net.IPNet{
  1448. IP: net.ParseIP("127.0.0.1"),
  1449. Mask: net.IPv4Mask(255, 255, 255, 0),
  1450. },
  1451. ),
  1452. },
  1453. cert: &x509.Certificate{
  1454. EmailAddresses: []string{"mail@smallstep.com"},
  1455. },
  1456. want: false,
  1457. wantErr: &NamePolicyError{
  1458. Reason: NotAllowed,
  1459. NameType: EmailNameType,
  1460. Name: "mail@smallstep.com",
  1461. },
  1462. },
  1463. {
  1464. name: "fail/ip-permitted-with-uri", // when only IP is permitted, URIs are not allowed.
  1465. options: []NamePolicyOption{
  1466. WithPermittedIPRanges(
  1467. &net.IPNet{
  1468. IP: net.ParseIP("127.0.0.1"),
  1469. Mask: net.IPv4Mask(255, 255, 255, 0),
  1470. },
  1471. ),
  1472. },
  1473. cert: &x509.Certificate{
  1474. URIs: []*url.URL{
  1475. {
  1476. Scheme: "https",
  1477. Host: "www.example.com",
  1478. },
  1479. },
  1480. },
  1481. want: false,
  1482. wantErr: &NamePolicyError{
  1483. Reason: NotAllowed,
  1484. NameType: URINameType,
  1485. Name: "https://www.example.com",
  1486. },
  1487. },
  1488. {
  1489. name: "fail/mail-permitted-with-dns-name", // when only mail is permitted, DNS names are not allowed.
  1490. options: []NamePolicyOption{
  1491. WithPermittedEmailAddresses("@example.com"),
  1492. },
  1493. cert: &x509.Certificate{
  1494. DNSNames: []string{"www.example.com"},
  1495. },
  1496. want: false,
  1497. wantErr: &NamePolicyError{
  1498. Reason: NotAllowed,
  1499. NameType: DNSNameType,
  1500. Name: "www.example.com",
  1501. },
  1502. },
  1503. {
  1504. name: "fail/mail-permitted-with-ip", // when only mail is permitted, IPs are not allowed.
  1505. options: []NamePolicyOption{
  1506. WithPermittedEmailAddresses("@example.com"),
  1507. },
  1508. cert: &x509.Certificate{
  1509. IPAddresses: []net.IP{
  1510. net.ParseIP("127.0.0.1"),
  1511. },
  1512. },
  1513. want: false,
  1514. wantErr: &NamePolicyError{
  1515. Reason: NotAllowed,
  1516. NameType: IPNameType,
  1517. Name: "127.0.0.1",
  1518. },
  1519. },
  1520. {
  1521. name: "fail/mail-permitted-with-uri", // when only mail is permitted, URIs are not allowed.
  1522. options: []NamePolicyOption{
  1523. WithPermittedEmailAddresses("@example.com"),
  1524. },
  1525. cert: &x509.Certificate{
  1526. URIs: []*url.URL{
  1527. {
  1528. Scheme: "https",
  1529. Host: "www.example.com",
  1530. },
  1531. },
  1532. },
  1533. want: false,
  1534. wantErr: &NamePolicyError{
  1535. Reason: NotAllowed,
  1536. NameType: URINameType,
  1537. Name: "https://www.example.com",
  1538. },
  1539. },
  1540. {
  1541. name: "fail/uri-permitted-with-dns-name", // when only URI is permitted, DNS names are not allowed.
  1542. options: []NamePolicyOption{
  1543. WithPermittedURIDomains("*.local"),
  1544. },
  1545. cert: &x509.Certificate{
  1546. DNSNames: []string{"host.local"},
  1547. },
  1548. want: false,
  1549. wantErr: &NamePolicyError{
  1550. Reason: NotAllowed,
  1551. NameType: DNSNameType,
  1552. Name: "host.local",
  1553. },
  1554. },
  1555. {
  1556. name: "fail/uri-permitted-with-ip-name", // when only URI is permitted, IPs are not allowed.
  1557. options: []NamePolicyOption{
  1558. WithPermittedURIDomains("*.local"),
  1559. },
  1560. cert: &x509.Certificate{
  1561. IPAddresses: []net.IP{
  1562. net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  1563. },
  1564. },
  1565. want: false,
  1566. wantErr: &NamePolicyError{
  1567. Reason: NotAllowed,
  1568. NameType: IPNameType,
  1569. Name: "2001:db8:85a3::8a2e:370:7334",
  1570. },
  1571. },
  1572. {
  1573. name: "fail/uri-permitted-with-ip-name", // when only URI is permitted, mails are not allowed.
  1574. options: []NamePolicyOption{
  1575. WithPermittedURIDomains("*.local"),
  1576. },
  1577. cert: &x509.Certificate{
  1578. EmailAddresses: []string{"mail@smallstep.com"},
  1579. },
  1580. want: false,
  1581. wantErr: &NamePolicyError{
  1582. Reason: NotAllowed,
  1583. NameType: EmailNameType,
  1584. Name: "mail@smallstep.com",
  1585. },
  1586. },
  1587. // COMBINED FAILURE TESTS
  1588. {
  1589. name: "fail/combined-simple-all-badhost.local-common-name",
  1590. options: []NamePolicyOption{
  1591. WithSubjectCommonNameVerification(),
  1592. WithPermittedDNSDomains("*.local"),
  1593. WithPermittedCIDRs("127.0.0.1/24"),
  1594. WithPermittedEmailAddresses("@example.local"),
  1595. WithPermittedURIDomains("*.example.local"),
  1596. WithExcludedDNSDomains("badhost.local"),
  1597. WithExcludedCIDRs("127.0.0.128/25"),
  1598. WithExcludedEmailAddresses("badmail@example.local"),
  1599. WithExcludedURIDomains("badwww.example.local"),
  1600. },
  1601. cert: &x509.Certificate{
  1602. Subject: pkix.Name{
  1603. CommonName: "badhost.local",
  1604. },
  1605. DNSNames: []string{"example.local"},
  1606. IPAddresses: []net.IP{net.ParseIP("127.0.0.40")},
  1607. EmailAddresses: []string{"mail@example.local"},
  1608. URIs: []*url.URL{
  1609. {
  1610. Scheme: "https",
  1611. Host: "www.example.local",
  1612. },
  1613. },
  1614. },
  1615. want: false,
  1616. wantErr: &NamePolicyError{
  1617. Reason: NotAllowed,
  1618. NameType: CNNameType,
  1619. Name: "badhost.local",
  1620. },
  1621. },
  1622. {
  1623. name: "fail/combined-simple-all-anotherbadhost.local-dns",
  1624. options: []NamePolicyOption{
  1625. WithPermittedDNSDomains("*.local"),
  1626. WithPermittedCIDRs("127.0.0.1/24"),
  1627. WithPermittedEmailAddresses("@example.local"),
  1628. WithPermittedURIDomains("*.example.local"),
  1629. WithExcludedDNSDomains("anotherbadhost.local"),
  1630. WithExcludedCIDRs("127.0.0.128/25"),
  1631. WithExcludedEmailAddresses("badmail@example.local"),
  1632. WithExcludedURIDomains("badwww.example.local"),
  1633. },
  1634. cert: &x509.Certificate{
  1635. Subject: pkix.Name{
  1636. CommonName: "badhost.local",
  1637. },
  1638. DNSNames: []string{"anotherbadhost.local"},
  1639. IPAddresses: []net.IP{net.ParseIP("127.0.0.40")},
  1640. EmailAddresses: []string{"mail@example.local"},
  1641. URIs: []*url.URL{
  1642. {
  1643. Scheme: "https",
  1644. Host: "www.example.local",
  1645. },
  1646. },
  1647. },
  1648. want: false,
  1649. wantErr: &NamePolicyError{
  1650. Reason: NotAllowed,
  1651. NameType: DNSNameType,
  1652. Name: "anotherbadhost.local",
  1653. },
  1654. },
  1655. {
  1656. name: "fail/combined-simple-all-badmail@example.local",
  1657. options: []NamePolicyOption{
  1658. WithPermittedDNSDomains("*.local"),
  1659. WithPermittedCIDRs("127.0.0.1/24"),
  1660. WithPermittedEmailAddresses("@example.local"),
  1661. WithPermittedURIDomains("*.example.local"),
  1662. WithExcludedDNSDomains("badhost.local"),
  1663. WithExcludedCIDRs("127.0.0.128/25"),
  1664. WithExcludedEmailAddresses("badmail@example.local"),
  1665. WithExcludedURIDomains("badwww.example.local"),
  1666. },
  1667. cert: &x509.Certificate{
  1668. Subject: pkix.Name{
  1669. CommonName: "badhost.local",
  1670. },
  1671. DNSNames: []string{"example.local"},
  1672. IPAddresses: []net.IP{net.ParseIP("127.0.0.40")},
  1673. EmailAddresses: []string{"mail@example.local", "badmail@example.local"},
  1674. URIs: []*url.URL{
  1675. {
  1676. Scheme: "https",
  1677. Host: "www.example.local",
  1678. },
  1679. },
  1680. },
  1681. want: false,
  1682. wantErr: &NamePolicyError{
  1683. Reason: NotAllowed,
  1684. NameType: EmailNameType,
  1685. Name: "badmail@example.local",
  1686. },
  1687. },
  1688. // NO CONSTRAINT SUCCESS TESTS
  1689. {
  1690. name: "ok/dns-no-constraints",
  1691. options: []NamePolicyOption{},
  1692. cert: &x509.Certificate{
  1693. DNSNames: []string{"www.example.com"},
  1694. },
  1695. want: true,
  1696. },
  1697. {
  1698. name: "ok/ipv4-no-constraints",
  1699. options: []NamePolicyOption{},
  1700. cert: &x509.Certificate{
  1701. IPAddresses: []net.IP{
  1702. net.ParseIP("127.0.0.1"),
  1703. },
  1704. },
  1705. want: true,
  1706. },
  1707. {
  1708. name: "ok/ipv6-no-constraints",
  1709. options: []NamePolicyOption{},
  1710. cert: &x509.Certificate{
  1711. IPAddresses: []net.IP{
  1712. net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  1713. },
  1714. },
  1715. want: true,
  1716. },
  1717. {
  1718. name: "ok/mail-no-constraints",
  1719. options: []NamePolicyOption{},
  1720. cert: &x509.Certificate{
  1721. EmailAddresses: []string{"mail@smallstep.com"},
  1722. },
  1723. want: true,
  1724. },
  1725. {
  1726. name: "ok/uri-no-constraints",
  1727. options: []NamePolicyOption{},
  1728. cert: &x509.Certificate{
  1729. URIs: []*url.URL{
  1730. {
  1731. Scheme: "https",
  1732. Host: "www.example.com",
  1733. },
  1734. },
  1735. },
  1736. want: true,
  1737. },
  1738. {
  1739. name: "ok/subject-no-constraints",
  1740. options: []NamePolicyOption{
  1741. WithSubjectCommonNameVerification(),
  1742. },
  1743. cert: &x509.Certificate{
  1744. Subject: pkix.Name{
  1745. CommonName: "www.example.com",
  1746. },
  1747. },
  1748. want: true,
  1749. },
  1750. {
  1751. name: "ok/subject-empty-no-constraints",
  1752. options: []NamePolicyOption{
  1753. WithSubjectCommonNameVerification(),
  1754. },
  1755. cert: &x509.Certificate{
  1756. Subject: pkix.Name{
  1757. CommonName: "",
  1758. },
  1759. },
  1760. want: true,
  1761. },
  1762. {
  1763. name: "ok/subject-permitted-match",
  1764. options: []NamePolicyOption{
  1765. WithSubjectCommonNameVerification(),
  1766. WithPermittedCommonNames("this name is allowed"),
  1767. },
  1768. cert: &x509.Certificate{
  1769. Subject: pkix.Name{
  1770. CommonName: "this name is allowed",
  1771. },
  1772. },
  1773. want: true,
  1774. },
  1775. {
  1776. name: "ok/subject-excluded-match",
  1777. options: []NamePolicyOption{
  1778. WithSubjectCommonNameVerification(),
  1779. WithExcludedCommonNames("this name is not allowed"),
  1780. },
  1781. cert: &x509.Certificate{
  1782. Subject: pkix.Name{
  1783. CommonName: "some other name",
  1784. },
  1785. },
  1786. want: true,
  1787. },
  1788. // SINGLE SAN TYPE PERMITTED SUCCESS TESTS
  1789. {
  1790. name: "ok/dns-permitted",
  1791. options: []NamePolicyOption{
  1792. WithPermittedDNSDomains("*.local"),
  1793. },
  1794. cert: &x509.Certificate{
  1795. DNSNames: []string{"example.local"},
  1796. },
  1797. want: true,
  1798. },
  1799. {
  1800. name: "ok/dns-permitted-wildcard",
  1801. options: []NamePolicyOption{
  1802. WithPermittedDNSDomains("*.local", "*.x509local"),
  1803. WithAllowLiteralWildcardNames(),
  1804. },
  1805. cert: &x509.Certificate{
  1806. DNSNames: []string{
  1807. "host.local",
  1808. "test.x509local",
  1809. },
  1810. },
  1811. want: true,
  1812. },
  1813. {
  1814. name: "ok/dns-permitted-wildcard-literal",
  1815. options: []NamePolicyOption{
  1816. WithPermittedDNSDomains("*.local", "*.x509local"),
  1817. WithAllowLiteralWildcardNames(),
  1818. },
  1819. cert: &x509.Certificate{
  1820. DNSNames: []string{
  1821. "*.local",
  1822. "*.x509local",
  1823. },
  1824. },
  1825. want: true,
  1826. },
  1827. {
  1828. name: "ok/dns-permitted-combined",
  1829. options: []NamePolicyOption{
  1830. WithPermittedDNSDomains("*.local", "*.x509local", "host.example.com"),
  1831. },
  1832. cert: &x509.Certificate{
  1833. DNSNames: []string{
  1834. "example.local",
  1835. "example.x509local",
  1836. "host.example.com",
  1837. },
  1838. },
  1839. want: true,
  1840. },
  1841. {
  1842. name: "ok/dns-permitted-idna-internationalized-domain",
  1843. options: []NamePolicyOption{
  1844. WithPermittedDNSDomains("*.例.jp"),
  1845. },
  1846. cert: &x509.Certificate{
  1847. DNSNames: []string{
  1848. "JP納豆.例.jp", // Example value from https://www.w3.org/International/articles/idn-and-iri/
  1849. },
  1850. },
  1851. want: true,
  1852. },
  1853. {
  1854. name: "ok/ipv4-permitted",
  1855. options: []NamePolicyOption{
  1856. WithPermittedCIDRs("127.0.0.1/24"),
  1857. },
  1858. cert: &x509.Certificate{
  1859. IPAddresses: []net.IP{net.ParseIP("127.0.0.20")},
  1860. },
  1861. want: true,
  1862. },
  1863. {
  1864. name: "ok/ipv6-permitted",
  1865. options: []NamePolicyOption{
  1866. WithPermittedCIDRs("2001:0db8:85a3:0000:0000:8a2e:0370:7334/120"),
  1867. },
  1868. cert: &x509.Certificate{
  1869. IPAddresses: []net.IP{net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7339")},
  1870. },
  1871. want: true,
  1872. },
  1873. {
  1874. name: "ok/mail-permitted-wildcard",
  1875. options: []NamePolicyOption{
  1876. WithPermittedEmailAddresses("@example.com"),
  1877. },
  1878. cert: &x509.Certificate{
  1879. EmailAddresses: []string{
  1880. "test@example.com",
  1881. },
  1882. },
  1883. want: true,
  1884. },
  1885. {
  1886. name: "ok/mail-permitted-plain-domain",
  1887. options: []NamePolicyOption{
  1888. WithPermittedEmailAddresses("example.com"),
  1889. },
  1890. cert: &x509.Certificate{
  1891. EmailAddresses: []string{
  1892. "test@example.com",
  1893. },
  1894. },
  1895. want: true,
  1896. },
  1897. {
  1898. name: "ok/mail-permitted-specific-mailbox",
  1899. options: []NamePolicyOption{
  1900. WithPermittedEmailAddresses("test@local.com"),
  1901. },
  1902. cert: &x509.Certificate{
  1903. EmailAddresses: []string{
  1904. "test@local.com",
  1905. },
  1906. },
  1907. want: true,
  1908. },
  1909. {
  1910. name: "ok/mail-permitted-idna-internationalized-domain",
  1911. options: []NamePolicyOption{
  1912. WithPermittedEmailAddresses("@例.jp"),
  1913. },
  1914. cert: &x509.Certificate{
  1915. EmailAddresses: []string{},
  1916. },
  1917. want: true,
  1918. },
  1919. {
  1920. name: "ok/uri-permitted-domain-wildcard",
  1921. options: []NamePolicyOption{
  1922. WithPermittedURIDomains("*.local"),
  1923. },
  1924. cert: &x509.Certificate{
  1925. URIs: []*url.URL{
  1926. {
  1927. Scheme: "https",
  1928. Host: "example.local",
  1929. },
  1930. },
  1931. },
  1932. want: true,
  1933. },
  1934. {
  1935. name: "ok/uri-permitted-specific-uri",
  1936. options: []NamePolicyOption{
  1937. WithPermittedURIDomains("test.local"),
  1938. },
  1939. cert: &x509.Certificate{
  1940. URIs: []*url.URL{
  1941. {
  1942. Scheme: "https",
  1943. Host: "test.local",
  1944. },
  1945. },
  1946. },
  1947. want: true,
  1948. },
  1949. {
  1950. name: "ok/uri-permitted-with-port",
  1951. options: []NamePolicyOption{
  1952. WithPermittedURIDomains("*.example.com"),
  1953. },
  1954. cert: &x509.Certificate{
  1955. URIs: []*url.URL{
  1956. {
  1957. Scheme: "https",
  1958. Host: "www.example.com:8080",
  1959. },
  1960. },
  1961. },
  1962. want: true,
  1963. },
  1964. {
  1965. name: "ok/uri-permitted-idna-internationalized-domain",
  1966. options: []NamePolicyOption{
  1967. WithPermittedURIDomains("*.bücher.example.com"),
  1968. },
  1969. cert: &x509.Certificate{
  1970. URIs: []*url.URL{
  1971. {
  1972. Scheme: "https",
  1973. Host: "abc.xn--bcher-kva.example.com",
  1974. },
  1975. },
  1976. },
  1977. want: true,
  1978. },
  1979. {
  1980. name: "ok/uri-permitted-idna-internationalized-domain",
  1981. options: []NamePolicyOption{
  1982. WithPermittedURIDomains("bücher.example.com"),
  1983. },
  1984. cert: &x509.Certificate{
  1985. URIs: []*url.URL{
  1986. {
  1987. Scheme: "https",
  1988. Host: "xn--bcher-kva.example.com",
  1989. },
  1990. },
  1991. },
  1992. want: true,
  1993. },
  1994. // SINGLE SAN TYPE EXCLUDED SUCCESS TESTS
  1995. {
  1996. name: "ok/dns-excluded",
  1997. options: []NamePolicyOption{
  1998. WithExcludedDNSDomains("*.notlocal"),
  1999. },
  2000. cert: &x509.Certificate{
  2001. DNSNames: []string{"example.local"},
  2002. },
  2003. want: true,
  2004. },
  2005. {
  2006. name: "ok/ipv4-excluded",
  2007. options: []NamePolicyOption{
  2008. WithExcludedIPRanges(
  2009. &net.IPNet{
  2010. IP: net.ParseIP("127.0.0.1"),
  2011. Mask: net.IPv4Mask(255, 255, 255, 0),
  2012. },
  2013. ),
  2014. },
  2015. cert: &x509.Certificate{
  2016. IPAddresses: []net.IP{net.ParseIP("10.10.10.10")},
  2017. },
  2018. want: true,
  2019. },
  2020. {
  2021. name: "ok/ipv6-excluded",
  2022. options: []NamePolicyOption{
  2023. WithExcludedCIDRs("2001:0db8:85a3:0000:0000:8a2e:0370:7334/120"),
  2024. },
  2025. cert: &x509.Certificate{
  2026. IPAddresses: []net.IP{net.ParseIP("2003:0db8:85a3:0000:0000:8a2e:0370:7334")},
  2027. },
  2028. want: true,
  2029. },
  2030. {
  2031. name: "ok/mail-excluded",
  2032. options: []NamePolicyOption{
  2033. WithExcludedEmailAddresses("@notlocal"),
  2034. },
  2035. cert: &x509.Certificate{
  2036. EmailAddresses: []string{"mail@local"},
  2037. },
  2038. want: true,
  2039. },
  2040. {
  2041. name: "ok/mail-excluded-with-subdomain",
  2042. options: []NamePolicyOption{
  2043. WithExcludedEmailAddresses("@local"),
  2044. },
  2045. cert: &x509.Certificate{
  2046. EmailAddresses: []string{"mail@example.local"},
  2047. },
  2048. want: true,
  2049. },
  2050. {
  2051. name: "ok/uri-excluded",
  2052. options: []NamePolicyOption{
  2053. WithExcludedURIDomains("*.google.com"),
  2054. },
  2055. cert: &x509.Certificate{
  2056. URIs: []*url.URL{
  2057. {
  2058. Scheme: "https",
  2059. Host: "www.example.com",
  2060. },
  2061. },
  2062. },
  2063. want: true,
  2064. },
  2065. // SUBJECT SUCCESS TESTS
  2066. {
  2067. name: "ok/subject-empty",
  2068. options: []NamePolicyOption{
  2069. WithSubjectCommonNameVerification(),
  2070. WithPermittedDNSDomains("*.local"),
  2071. },
  2072. cert: &x509.Certificate{
  2073. Subject: pkix.Name{
  2074. CommonName: "",
  2075. },
  2076. DNSNames: []string{"example.local"},
  2077. },
  2078. want: true,
  2079. },
  2080. {
  2081. name: "ok/subject-dns-permitted",
  2082. options: []NamePolicyOption{
  2083. WithSubjectCommonNameVerification(),
  2084. WithPermittedDNSDomains("*.local"),
  2085. },
  2086. cert: &x509.Certificate{
  2087. Subject: pkix.Name{
  2088. CommonName: "example.local",
  2089. },
  2090. },
  2091. want: true,
  2092. },
  2093. {
  2094. name: "ok/subject-dns-excluded",
  2095. options: []NamePolicyOption{
  2096. WithSubjectCommonNameVerification(),
  2097. WithExcludedDNSDomains("*.notlocal"),
  2098. },
  2099. cert: &x509.Certificate{
  2100. Subject: pkix.Name{
  2101. CommonName: "example.local",
  2102. },
  2103. },
  2104. want: true,
  2105. },
  2106. {
  2107. name: "ok/subject-ipv4-permitted",
  2108. options: []NamePolicyOption{
  2109. WithSubjectCommonNameVerification(),
  2110. WithPermittedIPRanges(
  2111. &net.IPNet{
  2112. IP: net.ParseIP("127.0.0.1"),
  2113. Mask: net.IPv4Mask(255, 255, 255, 0),
  2114. },
  2115. ),
  2116. },
  2117. cert: &x509.Certificate{
  2118. Subject: pkix.Name{
  2119. CommonName: "127.0.0.20",
  2120. },
  2121. },
  2122. want: true,
  2123. },
  2124. {
  2125. name: "ok/subject-ipv4-excluded",
  2126. options: []NamePolicyOption{
  2127. WithSubjectCommonNameVerification(),
  2128. WithExcludedIPRanges(
  2129. &net.IPNet{
  2130. IP: net.ParseIP("128.0.0.1"),
  2131. Mask: net.IPv4Mask(255, 255, 255, 0),
  2132. },
  2133. ),
  2134. },
  2135. cert: &x509.Certificate{
  2136. Subject: pkix.Name{
  2137. CommonName: "127.0.0.1",
  2138. },
  2139. },
  2140. want: true,
  2141. },
  2142. {
  2143. name: "ok/subject-ipv6-permitted",
  2144. options: []NamePolicyOption{
  2145. WithSubjectCommonNameVerification(),
  2146. WithPermittedIPRanges(
  2147. &net.IPNet{
  2148. IP: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  2149. Mask: net.CIDRMask(120, 128),
  2150. },
  2151. ),
  2152. },
  2153. cert: &x509.Certificate{
  2154. Subject: pkix.Name{
  2155. CommonName: "2001:0db8:85a3:0000:0000:8a2e:0370:7339",
  2156. },
  2157. },
  2158. want: true,
  2159. },
  2160. {
  2161. name: "ok/subject-ipv6-excluded",
  2162. options: []NamePolicyOption{
  2163. WithSubjectCommonNameVerification(),
  2164. WithExcludedIPRanges(
  2165. &net.IPNet{
  2166. IP: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  2167. Mask: net.CIDRMask(120, 128),
  2168. },
  2169. ),
  2170. },
  2171. cert: &x509.Certificate{
  2172. Subject: pkix.Name{
  2173. CommonName: "2009:0db8:85a3:0000:0000:8a2e:0370:7339",
  2174. },
  2175. },
  2176. want: true,
  2177. },
  2178. {
  2179. name: "ok/subject-email-permitted",
  2180. options: []NamePolicyOption{
  2181. WithSubjectCommonNameVerification(),
  2182. WithPermittedEmailAddresses("@example.local"),
  2183. },
  2184. cert: &x509.Certificate{
  2185. Subject: pkix.Name{
  2186. CommonName: "mail@example.local",
  2187. },
  2188. },
  2189. want: true,
  2190. },
  2191. {
  2192. name: "ok/subject-email-excluded",
  2193. options: []NamePolicyOption{
  2194. WithSubjectCommonNameVerification(),
  2195. WithExcludedEmailAddresses("@example.notlocal"),
  2196. },
  2197. cert: &x509.Certificate{
  2198. Subject: pkix.Name{
  2199. CommonName: "mail@example.local",
  2200. },
  2201. },
  2202. want: true,
  2203. },
  2204. {
  2205. name: "ok/subject-uri-permitted",
  2206. options: []NamePolicyOption{
  2207. WithSubjectCommonNameVerification(),
  2208. WithPermittedURIDomains("*.example.com"),
  2209. },
  2210. cert: &x509.Certificate{
  2211. Subject: pkix.Name{
  2212. CommonName: "https://www.example.com",
  2213. },
  2214. },
  2215. want: true,
  2216. },
  2217. {
  2218. name: "ok/subject-uri-excluded",
  2219. options: []NamePolicyOption{
  2220. WithSubjectCommonNameVerification(),
  2221. WithExcludedURIDomains("*.smallstep.com"),
  2222. },
  2223. cert: &x509.Certificate{
  2224. Subject: pkix.Name{
  2225. CommonName: "https://www.example.com",
  2226. },
  2227. },
  2228. want: true,
  2229. },
  2230. // DIFFERENT SAN TYPE EXCLUDED SUCCESS TESTS
  2231. {
  2232. name: "ok/dns-excluded-with-ip-name", // when only DNS is exluded, we allow anything else
  2233. options: []NamePolicyOption{
  2234. WithExcludedDNSDomains("*.local"),
  2235. },
  2236. cert: &x509.Certificate{
  2237. IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
  2238. },
  2239. want: true,
  2240. },
  2241. {
  2242. name: "ok/dns-excluded-with-mail", // when only DNS is exluded, we allow anything else
  2243. options: []NamePolicyOption{
  2244. WithExcludedDNSDomains("*.local"),
  2245. },
  2246. cert: &x509.Certificate{
  2247. EmailAddresses: []string{"mail@example.com"},
  2248. },
  2249. want: true,
  2250. },
  2251. {
  2252. name: "ok/dns-excluded-with-mail", // when only DNS is exluded, we allow anything else
  2253. options: []NamePolicyOption{
  2254. WithExcludedDNSDomains("*.local"),
  2255. },
  2256. cert: &x509.Certificate{
  2257. URIs: []*url.URL{
  2258. {
  2259. Scheme: "https",
  2260. Host: "www.example.com",
  2261. },
  2262. },
  2263. },
  2264. want: true,
  2265. },
  2266. {
  2267. name: "ok/ip-excluded-with-dns", // when only IP is exluded, we allow anything else
  2268. options: []NamePolicyOption{
  2269. WithExcludedCIDRs("127.0.0.1/24"),
  2270. },
  2271. cert: &x509.Certificate{
  2272. DNSNames: []string{"test.local"},
  2273. },
  2274. want: true,
  2275. },
  2276. {
  2277. name: "ok/ip-excluded-with-mail", // when only IP is exluded, we allow anything else
  2278. options: []NamePolicyOption{
  2279. WithExcludedCIDRs("127.0.0.1/24"),
  2280. },
  2281. cert: &x509.Certificate{
  2282. EmailAddresses: []string{"mail@example.com"},
  2283. },
  2284. want: true,
  2285. },
  2286. {
  2287. name: "ok/ip-excluded-with-mail", // when only IP is exluded, we allow anything else
  2288. options: []NamePolicyOption{
  2289. WithExcludedCIDRs("127.0.0.1/24"),
  2290. },
  2291. cert: &x509.Certificate{
  2292. URIs: []*url.URL{
  2293. {
  2294. Scheme: "https",
  2295. Host: "www.example.com",
  2296. },
  2297. },
  2298. },
  2299. want: true,
  2300. },
  2301. {
  2302. name: "ok/mail-excluded-with-dns", // when only mail is exluded, we allow anything else
  2303. options: []NamePolicyOption{
  2304. WithExcludedEmailAddresses("@example.com"),
  2305. },
  2306. cert: &x509.Certificate{
  2307. DNSNames: []string{"test.local"},
  2308. },
  2309. want: true,
  2310. },
  2311. {
  2312. name: "ok/mail-excluded-with-ip", // when only mail is exluded, we allow anything else
  2313. options: []NamePolicyOption{
  2314. WithExcludedEmailAddresses("@example.com"),
  2315. },
  2316. cert: &x509.Certificate{
  2317. IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
  2318. },
  2319. want: true,
  2320. },
  2321. {
  2322. name: "ok/mail-excluded-with-uri", // when only mail is exluded, we allow anything else
  2323. options: []NamePolicyOption{
  2324. WithExcludedEmailAddresses("@example.com"),
  2325. },
  2326. cert: &x509.Certificate{
  2327. URIs: []*url.URL{
  2328. {
  2329. Scheme: "https",
  2330. Host: "www.example.com",
  2331. },
  2332. },
  2333. },
  2334. want: true,
  2335. },
  2336. {
  2337. name: "ok/uri-excluded-with-dns", // when only URI is exluded, we allow anything else
  2338. options: []NamePolicyOption{
  2339. WithExcludedURIDomains("*.example.local"),
  2340. },
  2341. cert: &x509.Certificate{
  2342. DNSNames: []string{"test.example.local"},
  2343. },
  2344. want: true,
  2345. },
  2346. {
  2347. name: "ok/uri-excluded-with-dns", // when only URI is exluded, we allow anything else
  2348. options: []NamePolicyOption{
  2349. WithExcludedURIDomains("*.example.local"),
  2350. },
  2351. cert: &x509.Certificate{
  2352. IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
  2353. },
  2354. want: true,
  2355. },
  2356. {
  2357. name: "ok/uri-excluded-with-mail", // when only URI is exluded, we allow anything else
  2358. options: []NamePolicyOption{
  2359. WithExcludedURIDomains("*.example.local"),
  2360. },
  2361. cert: &x509.Certificate{
  2362. EmailAddresses: []string{"mail@example.local"},
  2363. },
  2364. want: true,
  2365. },
  2366. {
  2367. name: "ok/dns-excluded-with-subject-ip-name", // when only DNS is exluded, we allow anything else
  2368. options: []NamePolicyOption{
  2369. WithSubjectCommonNameVerification(),
  2370. WithExcludedDNSDomains("*.local"),
  2371. },
  2372. cert: &x509.Certificate{
  2373. Subject: pkix.Name{
  2374. CommonName: "127.0.0.1",
  2375. },
  2376. IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
  2377. },
  2378. want: true,
  2379. },
  2380. // COMBINED SUCCESS TESTS
  2381. {
  2382. name: "ok/combined-simple-permitted",
  2383. options: []NamePolicyOption{
  2384. WithSubjectCommonNameVerification(),
  2385. WithPermittedDNSDomains("*.local"),
  2386. WithPermittedCIDRs("127.0.0.1/24"),
  2387. WithPermittedEmailAddresses("@example.local"),
  2388. WithPermittedURIDomains("*.example.local"),
  2389. },
  2390. cert: &x509.Certificate{
  2391. Subject: pkix.Name{
  2392. CommonName: "somehost.local",
  2393. },
  2394. DNSNames: []string{"example.local"},
  2395. IPAddresses: []net.IP{net.ParseIP("127.0.0.15")},
  2396. EmailAddresses: []string{"mail@example.local"},
  2397. URIs: []*url.URL{
  2398. {
  2399. Scheme: "https",
  2400. Host: "www.example.local",
  2401. },
  2402. },
  2403. },
  2404. want: true,
  2405. },
  2406. {
  2407. name: "ok/combined-simple-permitted-without-subject-verification",
  2408. options: []NamePolicyOption{
  2409. WithPermittedDNSDomains("*.local"),
  2410. WithPermittedCIDRs("127.0.0.1/24"),
  2411. WithPermittedEmailAddresses("@example.local"),
  2412. WithPermittedURIDomains("*.example.local"),
  2413. },
  2414. cert: &x509.Certificate{
  2415. Subject: pkix.Name{
  2416. CommonName: "forbidden-but-non-verified-domain.example.com",
  2417. },
  2418. DNSNames: []string{"example.local"},
  2419. IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
  2420. EmailAddresses: []string{"mail@example.local"},
  2421. URIs: []*url.URL{
  2422. {
  2423. Scheme: "https",
  2424. Host: "www.example.local",
  2425. },
  2426. },
  2427. },
  2428. want: true,
  2429. },
  2430. {
  2431. name: "ok/combined-simple-all",
  2432. options: []NamePolicyOption{
  2433. WithSubjectCommonNameVerification(),
  2434. WithPermittedDNSDomains("*.local"),
  2435. WithPermittedCIDRs("127.0.0.1/24"),
  2436. WithPermittedEmailAddresses("@example.local"),
  2437. WithPermittedURIDomains("*.example.local"),
  2438. WithExcludedDNSDomains("badhost.local"),
  2439. WithExcludedCIDRs("127.0.0.128/25"),
  2440. WithExcludedEmailAddresses("badmail@example.local"),
  2441. WithExcludedURIDomains("badwww.example.local"),
  2442. },
  2443. cert: &x509.Certificate{
  2444. Subject: pkix.Name{
  2445. CommonName: "somehost.local",
  2446. },
  2447. DNSNames: []string{"example.local"},
  2448. IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
  2449. EmailAddresses: []string{"mail@example.local"},
  2450. URIs: []*url.URL{
  2451. {
  2452. Scheme: "https",
  2453. Host: "www.example.local",
  2454. },
  2455. },
  2456. },
  2457. want: true,
  2458. },
  2459. }
  2460. for _, tt := range tests {
  2461. t.Run(tt.name, func(t *testing.T) {
  2462. engine, err := New(tt.options...)
  2463. assert.NoError(t, err)
  2464. gotErr := engine.IsX509CertificateAllowed(tt.cert)
  2465. wantErr := tt.wantErr != nil
  2466. if (gotErr != nil) != wantErr {
  2467. t.Errorf("NamePolicyEngine.IsX509CertificateAllowed() error = %v, wantErr %v", gotErr, tt.wantErr)
  2468. return
  2469. }
  2470. if gotErr != nil {
  2471. var npe *NamePolicyError
  2472. assert.True(t, errors.As(gotErr, &npe))
  2473. assert.NotEqual(t, "", npe.Error())
  2474. assert.Equal(t, tt.wantErr.Reason, npe.Reason)
  2475. assert.Equal(t, tt.wantErr.NameType, npe.NameType)
  2476. assert.Equal(t, tt.wantErr.Name, npe.Name)
  2477. assert.NotEqual(t, "", npe.Detail())
  2478. //assert.Equals(t, tt.err.Reason, npe.Reason) // NOTE: reason detail is skipped; it's a detail
  2479. }
  2480. // Perform the same tests for a CSR, which are similar to Certificates
  2481. csr := &x509.CertificateRequest{
  2482. Subject: tt.cert.Subject,
  2483. DNSNames: tt.cert.DNSNames,
  2484. EmailAddresses: tt.cert.EmailAddresses,
  2485. IPAddresses: tt.cert.IPAddresses,
  2486. URIs: tt.cert.URIs,
  2487. }
  2488. gotErr = engine.IsX509CertificateRequestAllowed(csr)
  2489. wantErr = tt.wantErr != nil
  2490. if (gotErr != nil) != wantErr {
  2491. t.Errorf("NamePolicyEngine.AreCSRNamesAllowed() error = %v, wantErr %v", gotErr, tt.wantErr)
  2492. return
  2493. }
  2494. if gotErr != nil {
  2495. var npe *NamePolicyError
  2496. assert.True(t, errors.As(gotErr, &npe))
  2497. assert.NotEqual(t, "", npe.Error())
  2498. assert.Equal(t, tt.wantErr.Reason, npe.Reason)
  2499. assert.Equal(t, tt.wantErr.NameType, npe.NameType)
  2500. assert.Equal(t, tt.wantErr.Name, npe.Name)
  2501. assert.NotEqual(t, "", npe.Detail())
  2502. //assert.Equals(t, tt.err.Reason, npe.Reason) // NOTE: reason detail is skipped; it's a detail
  2503. }
  2504. })
  2505. }
  2506. }
  2507. func TestNamePolicyEngine_SSH_ArePrincipalsAllowed(t *testing.T) {
  2508. tests := []struct {
  2509. name string
  2510. options []NamePolicyOption
  2511. cert *ssh.Certificate
  2512. want bool
  2513. wantErr *NamePolicyError
  2514. }{
  2515. {
  2516. name: "fail/host-with-permitted-dns-domain",
  2517. options: []NamePolicyOption{
  2518. WithPermittedDNSDomains("*.local"),
  2519. },
  2520. cert: &ssh.Certificate{
  2521. CertType: ssh.HostCert,
  2522. ValidPrincipals: []string{
  2523. "host.example.com",
  2524. },
  2525. },
  2526. want: false,
  2527. wantErr: &NamePolicyError{
  2528. Reason: NotAllowed,
  2529. NameType: DNSNameType,
  2530. Name: "host.example.com",
  2531. },
  2532. },
  2533. {
  2534. name: "fail/host-with-excluded-dns-domain",
  2535. options: []NamePolicyOption{
  2536. WithExcludedDNSDomains("*.local"),
  2537. },
  2538. cert: &ssh.Certificate{
  2539. CertType: ssh.HostCert,
  2540. ValidPrincipals: []string{
  2541. "host.local",
  2542. },
  2543. },
  2544. want: false,
  2545. wantErr: &NamePolicyError{
  2546. Reason: NotAllowed,
  2547. NameType: DNSNameType,
  2548. Name: "host.local",
  2549. },
  2550. },
  2551. {
  2552. name: "fail/host-with-permitted-cidr",
  2553. options: []NamePolicyOption{
  2554. WithPermittedCIDRs("127.0.0.1/24"),
  2555. },
  2556. cert: &ssh.Certificate{
  2557. CertType: ssh.HostCert,
  2558. ValidPrincipals: []string{
  2559. "192.168.0.22",
  2560. },
  2561. },
  2562. want: false,
  2563. wantErr: &NamePolicyError{
  2564. Reason: NotAllowed,
  2565. NameType: IPNameType,
  2566. Name: "192.168.0.22",
  2567. },
  2568. },
  2569. {
  2570. name: "fail/host-with-excluded-cidr",
  2571. options: []NamePolicyOption{
  2572. WithExcludedCIDRs("127.0.0.1/24"),
  2573. },
  2574. cert: &ssh.Certificate{
  2575. CertType: ssh.HostCert,
  2576. ValidPrincipals: []string{
  2577. "127.0.0.0",
  2578. },
  2579. },
  2580. want: false,
  2581. wantErr: &NamePolicyError{
  2582. Reason: NotAllowed,
  2583. NameType: IPNameType,
  2584. Name: "127.0.0.0",
  2585. },
  2586. },
  2587. {
  2588. name: "fail/user-with-permitted-email",
  2589. options: []NamePolicyOption{
  2590. WithPermittedEmailAddresses("@example.com"),
  2591. },
  2592. cert: &ssh.Certificate{
  2593. CertType: ssh.UserCert,
  2594. ValidPrincipals: []string{
  2595. "mail@local",
  2596. },
  2597. },
  2598. want: false,
  2599. wantErr: &NamePolicyError{
  2600. Reason: NotAllowed,
  2601. NameType: EmailNameType,
  2602. Name: "mail@local",
  2603. },
  2604. },
  2605. {
  2606. name: "fail/user-with-excluded-email",
  2607. options: []NamePolicyOption{
  2608. WithExcludedEmailAddresses("@example.com"),
  2609. },
  2610. cert: &ssh.Certificate{
  2611. CertType: ssh.UserCert,
  2612. ValidPrincipals: []string{
  2613. "mail@example.com",
  2614. },
  2615. },
  2616. want: false,
  2617. wantErr: &NamePolicyError{
  2618. Reason: NotAllowed,
  2619. NameType: EmailNameType,
  2620. Name: "mail@example.com",
  2621. },
  2622. },
  2623. {
  2624. name: "fail/host-with-permitted-principals",
  2625. options: []NamePolicyOption{
  2626. WithPermittedPrincipals("localhost"),
  2627. },
  2628. cert: &ssh.Certificate{
  2629. CertType: ssh.HostCert,
  2630. ValidPrincipals: []string{
  2631. "host",
  2632. },
  2633. },
  2634. want: false,
  2635. wantErr: &NamePolicyError{
  2636. Reason: NotAllowed,
  2637. NameType: DNSNameType,
  2638. Name: "host",
  2639. },
  2640. },
  2641. {
  2642. name: "fail/user-with-permitted-principals",
  2643. options: []NamePolicyOption{
  2644. WithPermittedPrincipals("user"),
  2645. },
  2646. cert: &ssh.Certificate{
  2647. CertType: ssh.UserCert,
  2648. ValidPrincipals: []string{
  2649. "root",
  2650. },
  2651. },
  2652. want: false,
  2653. wantErr: &NamePolicyError{
  2654. Reason: NotAllowed,
  2655. NameType: PrincipalNameType,
  2656. Name: "root",
  2657. },
  2658. },
  2659. {
  2660. name: "fail/user-with-excluded-principals",
  2661. options: []NamePolicyOption{
  2662. WithExcludedPrincipals("user"),
  2663. },
  2664. cert: &ssh.Certificate{
  2665. CertType: ssh.UserCert,
  2666. ValidPrincipals: []string{
  2667. "user",
  2668. },
  2669. },
  2670. want: false,
  2671. wantErr: &NamePolicyError{
  2672. Reason: NotAllowed,
  2673. NameType: PrincipalNameType,
  2674. Name: "user",
  2675. },
  2676. },
  2677. {
  2678. name: "fail/user-with-permitted-principal-as-mail",
  2679. options: []NamePolicyOption{
  2680. WithPermittedPrincipals("ops"),
  2681. },
  2682. cert: &ssh.Certificate{
  2683. CertType: ssh.UserCert,
  2684. ValidPrincipals: []string{
  2685. "ops@work", // this is (currently) parsed as an email-like principal; not allowed with just "ops" as the permitted principal
  2686. },
  2687. },
  2688. want: false,
  2689. wantErr: &NamePolicyError{
  2690. Reason: NotAllowed,
  2691. NameType: EmailNameType,
  2692. Name: "ops@work",
  2693. },
  2694. },
  2695. {
  2696. name: "fail/host-principal-with-permitted-dns-domain", // when only DNS is permitted, username principals are not allowed.
  2697. options: []NamePolicyOption{
  2698. WithPermittedDNSDomains("*.local"),
  2699. },
  2700. cert: &ssh.Certificate{
  2701. CertType: ssh.HostCert,
  2702. ValidPrincipals: []string{
  2703. "user",
  2704. },
  2705. },
  2706. want: false,
  2707. wantErr: &NamePolicyError{
  2708. Reason: NotAllowed,
  2709. NameType: DNSNameType,
  2710. Name: "user",
  2711. },
  2712. },
  2713. {
  2714. name: "fail/host-principal-with-permitted-ip-range", // when only IPs are permitted, username principals are not allowed.
  2715. options: []NamePolicyOption{
  2716. WithPermittedCIDRs("127.0.0.1/24"),
  2717. },
  2718. cert: &ssh.Certificate{
  2719. CertType: ssh.HostCert,
  2720. ValidPrincipals: []string{
  2721. "user",
  2722. },
  2723. },
  2724. want: false,
  2725. wantErr: &NamePolicyError{
  2726. Reason: NotAllowed,
  2727. NameType: DNSNameType,
  2728. Name: "user",
  2729. },
  2730. },
  2731. {
  2732. name: "fail/user-principal-with-permitted-email", // when only emails are permitted, username principals are not allowed.
  2733. options: []NamePolicyOption{
  2734. WithPermittedEmailAddresses("@example.com"),
  2735. },
  2736. cert: &ssh.Certificate{
  2737. CertType: ssh.UserCert,
  2738. ValidPrincipals: []string{
  2739. "user",
  2740. },
  2741. },
  2742. want: false,
  2743. wantErr: &NamePolicyError{
  2744. Reason: NotAllowed,
  2745. NameType: PrincipalNameType,
  2746. Name: "user",
  2747. },
  2748. },
  2749. {
  2750. name: "fail/combined-user",
  2751. options: []NamePolicyOption{
  2752. WithPermittedEmailAddresses("@smallstep.com"),
  2753. WithExcludedEmailAddresses("root@smallstep.com"),
  2754. },
  2755. cert: &ssh.Certificate{
  2756. CertType: ssh.UserCert,
  2757. ValidPrincipals: []string{
  2758. "someone@smallstep.com",
  2759. "someone",
  2760. },
  2761. },
  2762. want: false,
  2763. wantErr: &NamePolicyError{
  2764. Reason: NotAllowed,
  2765. NameType: PrincipalNameType,
  2766. Name: "someone",
  2767. },
  2768. },
  2769. {
  2770. name: "fail/combined-user-with-excluded-user-principal",
  2771. options: []NamePolicyOption{
  2772. WithPermittedEmailAddresses("@smallstep.com"),
  2773. WithExcludedPrincipals("root"),
  2774. },
  2775. cert: &ssh.Certificate{
  2776. CertType: ssh.UserCert,
  2777. ValidPrincipals: []string{
  2778. "someone@smallstep.com",
  2779. "root",
  2780. },
  2781. },
  2782. want: false,
  2783. wantErr: &NamePolicyError{
  2784. Reason: NotAllowed,
  2785. NameType: PrincipalNameType,
  2786. Name: "root",
  2787. },
  2788. },
  2789. {
  2790. name: "fail/host-with-permitted-user-principals",
  2791. options: []NamePolicyOption{
  2792. WithPermittedEmailAddresses("@work"),
  2793. },
  2794. cert: &ssh.Certificate{
  2795. CertType: ssh.HostCert,
  2796. ValidPrincipals: []string{
  2797. "example.work",
  2798. },
  2799. },
  2800. want: false,
  2801. wantErr: &NamePolicyError{
  2802. Reason: NotAllowed,
  2803. NameType: DNSNameType,
  2804. Name: "example.work",
  2805. },
  2806. },
  2807. {
  2808. name: "fail/user-with-permitted-user-principals",
  2809. options: []NamePolicyOption{
  2810. WithPermittedDNSDomains("*.local"),
  2811. },
  2812. cert: &ssh.Certificate{
  2813. CertType: ssh.UserCert,
  2814. ValidPrincipals: []string{
  2815. "herman@work",
  2816. },
  2817. },
  2818. want: false,
  2819. wantErr: &NamePolicyError{
  2820. Reason: NotAllowed,
  2821. NameType: EmailNameType,
  2822. Name: "herman@work",
  2823. },
  2824. },
  2825. {
  2826. name: "ok/host-with-permitted-dns-domain",
  2827. options: []NamePolicyOption{
  2828. WithPermittedDNSDomains("*.local"),
  2829. },
  2830. cert: &ssh.Certificate{
  2831. CertType: ssh.HostCert,
  2832. ValidPrincipals: []string{
  2833. "host.local",
  2834. },
  2835. },
  2836. want: true,
  2837. },
  2838. {
  2839. name: "ok/host-with-excluded-dns-domain",
  2840. options: []NamePolicyOption{
  2841. WithExcludedDNSDomains("*.example.com"),
  2842. },
  2843. cert: &ssh.Certificate{
  2844. CertType: ssh.HostCert,
  2845. ValidPrincipals: []string{
  2846. "host.local",
  2847. },
  2848. },
  2849. want: true,
  2850. },
  2851. {
  2852. name: "ok/host-with-permitted-ip",
  2853. options: []NamePolicyOption{
  2854. WithPermittedCIDRs("127.0.0.1/24"),
  2855. },
  2856. cert: &ssh.Certificate{
  2857. CertType: ssh.HostCert,
  2858. ValidPrincipals: []string{
  2859. "127.0.0.33",
  2860. },
  2861. },
  2862. want: true,
  2863. },
  2864. {
  2865. name: "ok/host-with-excluded-ip",
  2866. options: []NamePolicyOption{
  2867. WithExcludedCIDRs("127.0.0.1/24"),
  2868. },
  2869. cert: &ssh.Certificate{
  2870. CertType: ssh.HostCert,
  2871. ValidPrincipals: []string{
  2872. "192.168.0.35",
  2873. },
  2874. },
  2875. want: true,
  2876. },
  2877. {
  2878. name: "ok/host-with-excluded-principals",
  2879. options: []NamePolicyOption{
  2880. WithExcludedPrincipals("localhost"),
  2881. },
  2882. cert: &ssh.Certificate{
  2883. CertType: ssh.HostCert,
  2884. ValidPrincipals: []string{
  2885. "localhost",
  2886. },
  2887. },
  2888. want: true,
  2889. },
  2890. {
  2891. name: "ok/user-with-permitted-email",
  2892. options: []NamePolicyOption{
  2893. WithPermittedEmailAddresses("@example.com"),
  2894. },
  2895. cert: &ssh.Certificate{
  2896. CertType: ssh.UserCert,
  2897. ValidPrincipals: []string{
  2898. "mail@example.com",
  2899. },
  2900. },
  2901. want: true,
  2902. },
  2903. {
  2904. name: "ok/user-with-excluded-email",
  2905. options: []NamePolicyOption{
  2906. WithExcludedEmailAddresses("@example.com"),
  2907. },
  2908. cert: &ssh.Certificate{
  2909. CertType: ssh.UserCert,
  2910. ValidPrincipals: []string{
  2911. "mail@local",
  2912. },
  2913. },
  2914. want: true,
  2915. },
  2916. {
  2917. name: "ok/user-with-permitted-principals",
  2918. options: []NamePolicyOption{
  2919. WithPermittedPrincipals("*"),
  2920. },
  2921. cert: &ssh.Certificate{
  2922. CertType: ssh.UserCert,
  2923. ValidPrincipals: []string{
  2924. "user",
  2925. },
  2926. },
  2927. want: true,
  2928. },
  2929. {
  2930. name: "ok/user-with-excluded-principals",
  2931. options: []NamePolicyOption{
  2932. WithExcludedPrincipals("user"),
  2933. },
  2934. cert: &ssh.Certificate{
  2935. CertType: ssh.UserCert,
  2936. ValidPrincipals: []string{
  2937. "root",
  2938. },
  2939. },
  2940. want: true,
  2941. },
  2942. {
  2943. name: "ok/combined-user",
  2944. options: []NamePolicyOption{
  2945. WithPermittedEmailAddresses("@smallstep.com"),
  2946. WithPermittedPrincipals("*"), // without specifying the wildcard, "someone" would not be allowed.
  2947. WithExcludedEmailAddresses("root@smallstep.com"),
  2948. },
  2949. cert: &ssh.Certificate{
  2950. CertType: ssh.UserCert,
  2951. ValidPrincipals: []string{
  2952. "someone@smallstep.com",
  2953. "someone",
  2954. },
  2955. },
  2956. want: true,
  2957. },
  2958. {
  2959. name: "ok/combined-user-with-excluded-user-principal",
  2960. options: []NamePolicyOption{
  2961. WithPermittedEmailAddresses("@smallstep.com"),
  2962. WithExcludedEmailAddresses("root@smallstep.com"),
  2963. WithExcludedPrincipals("root"), // unlike the previous test, this implicitly allows any other username principal
  2964. },
  2965. cert: &ssh.Certificate{
  2966. CertType: ssh.UserCert,
  2967. ValidPrincipals: []string{
  2968. "someone@smallstep.com",
  2969. "someone",
  2970. },
  2971. },
  2972. want: true,
  2973. },
  2974. {
  2975. name: "ok/combined-host",
  2976. options: []NamePolicyOption{
  2977. WithPermittedDNSDomains("*.local"),
  2978. WithPermittedCIDRs("127.0.0.1/24"),
  2979. WithExcludedDNSDomains("badhost.local"),
  2980. WithExcludedCIDRs("127.0.0.128/25"),
  2981. },
  2982. cert: &ssh.Certificate{
  2983. CertType: ssh.HostCert,
  2984. ValidPrincipals: []string{
  2985. "example.local",
  2986. "127.0.0.31",
  2987. },
  2988. },
  2989. want: true,
  2990. },
  2991. }
  2992. for _, tt := range tests {
  2993. t.Run(tt.name, func(t *testing.T) {
  2994. engine, err := New(tt.options...)
  2995. assert.NoError(t, err)
  2996. gotErr := engine.IsSSHCertificateAllowed(tt.cert)
  2997. wantErr := tt.wantErr != nil
  2998. if (gotErr != nil) != wantErr {
  2999. t.Errorf("NamePolicyEngine.IsSSHCertificateAllowed() error = %v, wantErr %v", gotErr, tt.wantErr)
  3000. return
  3001. }
  3002. if gotErr != nil {
  3003. var npe *NamePolicyError
  3004. assert.True(t, errors.As(gotErr, &npe))
  3005. assert.NotEqual(t, "", npe.Error())
  3006. assert.Equal(t, tt.wantErr.Reason, npe.Reason)
  3007. assert.Equal(t, tt.wantErr.NameType, npe.NameType)
  3008. assert.Equal(t, tt.wantErr.Name, npe.Name)
  3009. assert.NotEqual(t, "", npe.Detail())
  3010. //assert.Equals(t, tt.err.Reason, npe.Reason) // NOTE: reason detail is skipped; it's a detail
  3011. }
  3012. })
  3013. }
  3014. }
  3015. type result struct {
  3016. wantDNSNames []string
  3017. wantIps []net.IP
  3018. wantEmails []string
  3019. wantUsernames []string
  3020. }
  3021. func emptyResult() result {
  3022. return result{
  3023. wantDNSNames: []string{},
  3024. wantIps: []net.IP{},
  3025. wantEmails: []string{},
  3026. wantUsernames: []string{},
  3027. }
  3028. }
  3029. func Test_splitSSHPrincipals(t *testing.T) {
  3030. type test struct {
  3031. cert *ssh.Certificate
  3032. r result
  3033. wantErr bool
  3034. }
  3035. var tests = map[string]func(t *testing.T) test{
  3036. "fail/unexpected-cert-type": func(t *testing.T) test {
  3037. r := emptyResult()
  3038. return test{
  3039. cert: &ssh.Certificate{
  3040. CertType: uint32(0),
  3041. },
  3042. r: r,
  3043. wantErr: true,
  3044. }
  3045. },
  3046. "fail/user-ip": func(t *testing.T) test {
  3047. r := emptyResult()
  3048. r.wantIps = []net.IP{net.ParseIP("127.0.0.1")}
  3049. return test{
  3050. cert: &ssh.Certificate{
  3051. CertType: ssh.UserCert,
  3052. ValidPrincipals: []string{"127.0.0.1"},
  3053. },
  3054. r: r,
  3055. wantErr: true,
  3056. }
  3057. },
  3058. "fail/user-uri": func(t *testing.T) test {
  3059. r := emptyResult()
  3060. return test{
  3061. cert: &ssh.Certificate{
  3062. CertType: ssh.UserCert,
  3063. ValidPrincipals: []string{"https://host.local/"},
  3064. },
  3065. r: r,
  3066. wantErr: true,
  3067. }
  3068. },
  3069. "fail/host-uri": func(t *testing.T) test {
  3070. r := emptyResult()
  3071. return test{
  3072. cert: &ssh.Certificate{
  3073. CertType: ssh.HostCert,
  3074. ValidPrincipals: []string{"https://host.local/"},
  3075. },
  3076. r: r,
  3077. wantErr: true,
  3078. }
  3079. },
  3080. "ok/host-dns": func(t *testing.T) test {
  3081. r := emptyResult()
  3082. r.wantDNSNames = []string{"host.example.com"}
  3083. return test{
  3084. cert: &ssh.Certificate{
  3085. CertType: ssh.HostCert,
  3086. ValidPrincipals: []string{"host.example.com"},
  3087. },
  3088. r: r,
  3089. wantErr: false,
  3090. }
  3091. },
  3092. "ok/host-ip": func(t *testing.T) test {
  3093. r := emptyResult()
  3094. r.wantIps = []net.IP{net.ParseIP("127.0.0.1")}
  3095. return test{
  3096. cert: &ssh.Certificate{
  3097. CertType: ssh.HostCert,
  3098. ValidPrincipals: []string{"127.0.0.1"},
  3099. },
  3100. r: r,
  3101. wantErr: false,
  3102. }
  3103. },
  3104. "ok/host-email": func(t *testing.T) test {
  3105. r := emptyResult()
  3106. r.wantEmails = []string{"ops@work"}
  3107. return test{
  3108. cert: &ssh.Certificate{
  3109. CertType: ssh.HostCert,
  3110. ValidPrincipals: []string{"ops@work"},
  3111. },
  3112. r: r,
  3113. wantErr: false,
  3114. }
  3115. },
  3116. "ok/user-localhost": func(t *testing.T) test {
  3117. r := emptyResult()
  3118. r.wantUsernames = []string{"localhost"} // when type is User cert, this is considered a username; not a DNS
  3119. return test{
  3120. cert: &ssh.Certificate{
  3121. CertType: ssh.UserCert,
  3122. ValidPrincipals: []string{"localhost"},
  3123. },
  3124. r: r,
  3125. wantErr: false,
  3126. }
  3127. },
  3128. "ok/user-username-with-period": func(t *testing.T) test {
  3129. r := emptyResult()
  3130. r.wantUsernames = []string{"x.joe"}
  3131. return test{
  3132. cert: &ssh.Certificate{
  3133. CertType: ssh.UserCert,
  3134. ValidPrincipals: []string{"x.joe"},
  3135. },
  3136. r: r,
  3137. wantErr: false,
  3138. }
  3139. },
  3140. "ok/user-maillike": func(t *testing.T) test {
  3141. r := emptyResult()
  3142. r.wantEmails = []string{"ops@work"}
  3143. return test{
  3144. cert: &ssh.Certificate{
  3145. CertType: ssh.UserCert,
  3146. ValidPrincipals: []string{"ops@work"},
  3147. },
  3148. r: r,
  3149. wantErr: false,
  3150. }
  3151. },
  3152. }
  3153. for name, prep := range tests {
  3154. tt := prep(t)
  3155. t.Run(name, func(t *testing.T) {
  3156. gotDNSNames, gotIps, gotEmails, gotUsernames, err := splitSSHPrincipals(tt.cert)
  3157. if (err != nil) != tt.wantErr {
  3158. t.Errorf("splitSSHPrincipals() error = %v, wantErr %v", err, tt.wantErr)
  3159. return
  3160. }
  3161. if !cmp.Equal(tt.r.wantDNSNames, gotDNSNames) {
  3162. t.Errorf("splitSSHPrincipals() DNS names diff =\n%s", cmp.Diff(tt.r.wantDNSNames, gotDNSNames))
  3163. }
  3164. if !cmp.Equal(tt.r.wantIps, gotIps) {
  3165. t.Errorf("splitSSHPrincipals() IPs diff =\n%s", cmp.Diff(tt.r.wantIps, gotIps))
  3166. }
  3167. if !cmp.Equal(tt.r.wantEmails, gotEmails) {
  3168. t.Errorf("splitSSHPrincipals() Emails diff =\n%s", cmp.Diff(tt.r.wantEmails, gotEmails))
  3169. }
  3170. if !cmp.Equal(tt.r.wantUsernames, gotUsernames) {
  3171. t.Errorf("splitSSHPrincipals() Usernames diff =\n%s", cmp.Diff(tt.r.wantUsernames, gotUsernames))
  3172. }
  3173. })
  3174. }
  3175. }
  3176. func Test_removeDuplicates(t *testing.T) {
  3177. tests := []struct {
  3178. name string
  3179. input []string
  3180. want []string
  3181. }{
  3182. {
  3183. name: "empty-slice",
  3184. input: []string{},
  3185. want: []string{},
  3186. },
  3187. {
  3188. name: "single-item",
  3189. input: []string{"x"},
  3190. want: []string{"x"},
  3191. },
  3192. {
  3193. name: "ok",
  3194. input: []string{"x", "y", "x", "z", "x", "z", "y"},
  3195. want: []string{"x", "y", "z"},
  3196. },
  3197. }
  3198. for _, tt := range tests {
  3199. t.Run(tt.name, func(t *testing.T) {
  3200. if got := removeDuplicates(tt.input); !reflect.DeepEqual(got, tt.want) {
  3201. t.Errorf("removeDuplicates() = %v, want %v", got, tt.want)
  3202. }
  3203. })
  3204. }
  3205. }
  3206. func Test_removeDuplicateIPNets(t *testing.T) {
  3207. tests := []struct {
  3208. name string
  3209. input []*net.IPNet
  3210. want []*net.IPNet
  3211. }{
  3212. {
  3213. name: "empty-slice",
  3214. input: []*net.IPNet{},
  3215. want: []*net.IPNet{},
  3216. },
  3217. {
  3218. name: "single-item",
  3219. input: []*net.IPNet{
  3220. {
  3221. IP: net.ParseIP("127.0.0.1"),
  3222. Mask: net.IPv4Mask(255, 255, 255, 255),
  3223. },
  3224. },
  3225. want: []*net.IPNet{
  3226. {
  3227. IP: net.ParseIP("127.0.0.1"),
  3228. Mask: net.IPv4Mask(255, 255, 255, 255),
  3229. },
  3230. },
  3231. },
  3232. {
  3233. name: "multiple",
  3234. input: []*net.IPNet{
  3235. {
  3236. IP: net.ParseIP("127.0.0.1"),
  3237. Mask: net.IPv4Mask(255, 255, 255, 255),
  3238. },
  3239. {
  3240. IP: net.ParseIP("192.168.0.1"),
  3241. Mask: net.IPv4Mask(255, 255, 255, 0),
  3242. },
  3243. {
  3244. IP: net.ParseIP("127.0.0.1"),
  3245. Mask: net.IPv4Mask(255, 255, 255, 255),
  3246. },
  3247. {
  3248. IP: net.ParseIP("10.10.0.0"),
  3249. Mask: net.IPv4Mask(255, 255, 0, 0),
  3250. },
  3251. {
  3252. IP: net.ParseIP("192.168.0.1"),
  3253. Mask: net.IPv4Mask(255, 255, 255, 0),
  3254. },
  3255. {
  3256. IP: net.ParseIP("127.0.0.1"),
  3257. Mask: net.IPv4Mask(255, 255, 255, 255),
  3258. },
  3259. {
  3260. IP: net.ParseIP("192.168.0.1"),
  3261. Mask: net.IPv4Mask(255, 255, 255, 0),
  3262. },
  3263. },
  3264. want: []*net.IPNet{
  3265. {
  3266. IP: net.ParseIP("127.0.0.1"),
  3267. Mask: net.IPv4Mask(255, 255, 255, 255),
  3268. },
  3269. {
  3270. IP: net.ParseIP("192.168.0.1"),
  3271. Mask: net.IPv4Mask(255, 255, 255, 0),
  3272. },
  3273. {
  3274. IP: net.ParseIP("10.10.0.0"),
  3275. Mask: net.IPv4Mask(255, 255, 0, 0),
  3276. },
  3277. },
  3278. },
  3279. }
  3280. for _, tt := range tests {
  3281. t.Run(tt.name, func(t *testing.T) {
  3282. if gotRet := removeDuplicateIPNets(tt.input); !reflect.DeepEqual(gotRet, tt.want) {
  3283. t.Errorf("removeDuplicateIPNets() = %v, want %v", gotRet, tt.want)
  3284. }
  3285. })
  3286. }
  3287. }
  3288. func TestNamePolicyError_Error(t *testing.T) {
  3289. type fields struct {
  3290. Reason NamePolicyReason
  3291. NameType NameType
  3292. Name string
  3293. detail string
  3294. }
  3295. tests := []struct {
  3296. name string
  3297. fields fields
  3298. want string
  3299. }{
  3300. {
  3301. name: "dns-not-allowed",
  3302. fields: fields{
  3303. Reason: NotAllowed,
  3304. NameType: DNSNameType,
  3305. Name: "www.example.com",
  3306. },
  3307. want: "dns name \"www.example.com\" not allowed",
  3308. },
  3309. {
  3310. name: "dns-cannot-parse-domain",
  3311. fields: fields{
  3312. Reason: CannotParseDomain,
  3313. NameType: DNSNameType,
  3314. Name: "www.example.com",
  3315. },
  3316. want: "cannot parse dns domain \"www.example.com\"",
  3317. },
  3318. {
  3319. name: "email-cannot-parse",
  3320. fields: fields{
  3321. Reason: CannotParseRFC822Name,
  3322. NameType: EmailNameType,
  3323. Name: "mail@example.com",
  3324. },
  3325. want: "cannot parse email rfc822Name \"mail@example.com\"",
  3326. },
  3327. {
  3328. name: "uri-cannot-match",
  3329. fields: fields{
  3330. Reason: CannotMatchNameToConstraint,
  3331. NameType: URINameType,
  3332. Name: "https://*.local",
  3333. },
  3334. want: "error matching uri name \"https://*.local\" to constraint",
  3335. },
  3336. {
  3337. name: "unknown",
  3338. fields: fields{
  3339. Reason: -1,
  3340. NameType: DNSNameType,
  3341. Name: "some name",
  3342. detail: "detail string",
  3343. },
  3344. want: "unknown error reason (-1): detail string",
  3345. },
  3346. }
  3347. for _, tt := range tests {
  3348. t.Run(tt.name, func(t *testing.T) {
  3349. e := &NamePolicyError{
  3350. Reason: tt.fields.Reason,
  3351. NameType: tt.fields.NameType,
  3352. Name: tt.fields.Name,
  3353. detail: tt.fields.detail,
  3354. }
  3355. if got := e.Error(); got != tt.want {
  3356. t.Errorf("NamePolicyError.Error() = %v, want %v", got, tt.want)
  3357. }
  3358. })
  3359. }
  3360. }