engine_test.go 66 KB

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