engine_test.go 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878
  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. WithPermittedDNSDomain("*.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. WithPermittedDNSDomain("*.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. WithPermittedDNSDomain("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. WithPermittedDNSDomain("*.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. WithPermittedDNSDomain("*.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. WithPermittedDNSDomain("*.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. WithPermittedDNSDomain("*.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. WithPermittedDNSDomain("*.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. WithPermittedDNSDomain("*.豆.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. WithPermittedIPRanges(
  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. WithPermittedIPRanges(
  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. WithPermittedEmailAddress("@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. WithPermittedEmailAddress("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. WithPermittedEmailAddress("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. WithPermittedEmailAddress("@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. WithPermittedEmailAddress("@例.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. WithPermittedEmailAddress("@例.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. WithPermittedEmailAddress("@例.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. WithPermittedURIDomain("*.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. WithPermittedURIDomain("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. WithPermittedURIDomain("*.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. WithPermittedURIDomain("*.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. WithExcludedDNSDomain("*.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. WithExcludedDNSDomain("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. WithExcludedIPRanges(
  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. WithExcludedIPRanges(
  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. WithExcludedEmailAddress("@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. WithExcludedURIDomain("*.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. WithExcludedURIDomain("*.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. WithPermittedDNSDomain("*.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. WithExcludedDNSDomain("*.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. WithPermittedIPRanges(
  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. WithExcludedIPRanges(
  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. WithPermittedIPRanges(
  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. WithExcludedIPRanges(
  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. WithPermittedEmailAddress("@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. WithExcludedEmailAddress("@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. WithPermittedURIDomain("*.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. WithExcludedURIDomain("*.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. WithPermittedDNSDomain("*.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. WithPermittedDNSDomain("*.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. WithPermittedDNSDomain("*.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. WithPermittedIPRanges(
  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. WithPermittedIPRanges(
  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. WithPermittedIPRanges(
  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. WithPermittedEmailAddress("@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. WithPermittedEmailAddress("@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. WithPermittedEmailAddress("@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. WithPermittedURIDomain("*.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. WithPermittedURIDomain("*.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. WithPermittedURIDomain("*.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. WithPermittedDNSDomain("*.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. WithPermittedDNSDomains([]string{"*.local", "*.x509local"}),
  1492. WithAllowLiteralWildcardNames(),
  1493. },
  1494. cert: &x509.Certificate{
  1495. DNSNames: []string{
  1496. "host.local",
  1497. "test.x509local",
  1498. },
  1499. },
  1500. want: true,
  1501. wantErr: false,
  1502. },
  1503. {
  1504. name: "ok/dns-permitted-wildcard-literal",
  1505. options: []NamePolicyOption{
  1506. WithPermittedDNSDomains([]string{"*.local", "*.x509local"}),
  1507. WithAllowLiteralWildcardNames(),
  1508. },
  1509. cert: &x509.Certificate{
  1510. DNSNames: []string{
  1511. "*.local",
  1512. "*.x509local",
  1513. },
  1514. },
  1515. want: true,
  1516. wantErr: false,
  1517. },
  1518. {
  1519. name: "ok/dns-permitted-combined",
  1520. options: []NamePolicyOption{
  1521. WithPermittedDNSDomains([]string{"*.local", "*.x509local", "host.example.com"}),
  1522. },
  1523. cert: &x509.Certificate{
  1524. DNSNames: []string{
  1525. "example.local",
  1526. "example.x509local",
  1527. "host.example.com",
  1528. },
  1529. },
  1530. want: true,
  1531. wantErr: false,
  1532. },
  1533. {
  1534. name: "ok/dns-permitted-idna-internationalized-domain",
  1535. options: []NamePolicyOption{
  1536. WithPermittedDNSDomain("*.例.jp"),
  1537. },
  1538. cert: &x509.Certificate{
  1539. DNSNames: []string{
  1540. "JP納豆.例.jp", // Example value from https://www.w3.org/International/articles/idn-and-iri/
  1541. },
  1542. },
  1543. want: true,
  1544. wantErr: false,
  1545. },
  1546. {
  1547. name: "ok/ipv4-permitted",
  1548. options: []NamePolicyOption{
  1549. WithPermittedCIDR("127.0.0.1/24"),
  1550. },
  1551. cert: &x509.Certificate{
  1552. IPAddresses: []net.IP{net.ParseIP("127.0.0.20")},
  1553. },
  1554. want: true,
  1555. wantErr: false,
  1556. },
  1557. {
  1558. name: "ok/ipv6-permitted",
  1559. options: []NamePolicyOption{
  1560. WithPermittedCIDR("2001:0db8:85a3:0000:0000:8a2e:0370:7334/120"),
  1561. },
  1562. cert: &x509.Certificate{
  1563. IPAddresses: []net.IP{net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7339")},
  1564. },
  1565. want: true,
  1566. wantErr: false,
  1567. },
  1568. {
  1569. name: "ok/mail-permitted-wildcard",
  1570. options: []NamePolicyOption{
  1571. WithPermittedEmailAddress("@example.com"),
  1572. },
  1573. cert: &x509.Certificate{
  1574. EmailAddresses: []string{
  1575. "test@example.com",
  1576. },
  1577. },
  1578. want: true,
  1579. wantErr: false,
  1580. },
  1581. {
  1582. name: "ok/mail-permitted-plain-domain",
  1583. options: []NamePolicyOption{
  1584. WithPermittedEmailAddress("example.com"),
  1585. },
  1586. cert: &x509.Certificate{
  1587. EmailAddresses: []string{
  1588. "test@example.com",
  1589. },
  1590. },
  1591. want: true,
  1592. wantErr: false,
  1593. },
  1594. {
  1595. name: "ok/mail-permitted-specific-mailbox",
  1596. options: []NamePolicyOption{
  1597. WithPermittedEmailAddress("test@local.com"),
  1598. },
  1599. cert: &x509.Certificate{
  1600. EmailAddresses: []string{
  1601. "test@local.com",
  1602. },
  1603. },
  1604. want: true,
  1605. wantErr: false,
  1606. },
  1607. {
  1608. name: "ok/mail-permitted-idna-internationalized-domain",
  1609. options: []NamePolicyOption{
  1610. WithPermittedEmailAddress("@例.jp"),
  1611. },
  1612. cert: &x509.Certificate{
  1613. EmailAddresses: []string{},
  1614. },
  1615. want: true,
  1616. wantErr: false,
  1617. },
  1618. {
  1619. name: "ok/uri-permitted-domain-wildcard",
  1620. options: []NamePolicyOption{
  1621. WithPermittedURIDomain("*.local"),
  1622. },
  1623. cert: &x509.Certificate{
  1624. URIs: []*url.URL{
  1625. {
  1626. Scheme: "https",
  1627. Host: "example.local",
  1628. },
  1629. },
  1630. },
  1631. want: true,
  1632. wantErr: false,
  1633. },
  1634. {
  1635. name: "ok/uri-permitted-specific-uri",
  1636. options: []NamePolicyOption{
  1637. WithPermittedURIDomain("test.local"),
  1638. },
  1639. cert: &x509.Certificate{
  1640. URIs: []*url.URL{
  1641. {
  1642. Scheme: "https",
  1643. Host: "test.local",
  1644. },
  1645. },
  1646. },
  1647. want: true,
  1648. wantErr: false,
  1649. },
  1650. {
  1651. name: "ok/uri-permitted-with-port",
  1652. options: []NamePolicyOption{
  1653. WithPermittedURIDomain("*.example.com"),
  1654. },
  1655. cert: &x509.Certificate{
  1656. URIs: []*url.URL{
  1657. {
  1658. Scheme: "https",
  1659. Host: "www.example.com:8080",
  1660. },
  1661. },
  1662. },
  1663. want: true,
  1664. wantErr: false,
  1665. },
  1666. {
  1667. name: "ok/uri-permitted-idna-internationalized-domain",
  1668. options: []NamePolicyOption{
  1669. WithPermittedURIDomain("*.bücher.example.com"),
  1670. },
  1671. cert: &x509.Certificate{
  1672. URIs: []*url.URL{
  1673. {
  1674. Scheme: "https",
  1675. Host: "abc.xn--bcher-kva.example.com",
  1676. },
  1677. },
  1678. },
  1679. want: true,
  1680. wantErr: false,
  1681. },
  1682. {
  1683. name: "ok/uri-permitted-idna-internationalized-domain",
  1684. options: []NamePolicyOption{
  1685. WithPermittedURIDomain("bücher.example.com"),
  1686. },
  1687. cert: &x509.Certificate{
  1688. URIs: []*url.URL{
  1689. {
  1690. Scheme: "https",
  1691. Host: "xn--bcher-kva.example.com",
  1692. },
  1693. },
  1694. },
  1695. want: true,
  1696. wantErr: false,
  1697. },
  1698. // SINGLE SAN TYPE EXCLUDED SUCCESS TESTS
  1699. {
  1700. name: "ok/dns-excluded",
  1701. options: []NamePolicyOption{
  1702. WithExcludedDNSDomain("*.notlocal"),
  1703. },
  1704. cert: &x509.Certificate{
  1705. DNSNames: []string{"example.local"},
  1706. },
  1707. want: true,
  1708. wantErr: false,
  1709. },
  1710. {
  1711. name: "ok/ipv4-excluded",
  1712. options: []NamePolicyOption{
  1713. WithExcludedIPRanges(
  1714. []*net.IPNet{
  1715. {
  1716. IP: net.ParseIP("127.0.0.1"),
  1717. Mask: net.IPv4Mask(255, 255, 255, 0),
  1718. },
  1719. },
  1720. ),
  1721. },
  1722. cert: &x509.Certificate{
  1723. IPAddresses: []net.IP{net.ParseIP("10.10.10.10")},
  1724. },
  1725. want: true,
  1726. wantErr: false,
  1727. },
  1728. {
  1729. name: "ok/ipv6-excluded",
  1730. options: []NamePolicyOption{
  1731. WithExcludedCIDR("2001:0db8:85a3:0000:0000:8a2e:0370:7334/120"),
  1732. },
  1733. cert: &x509.Certificate{
  1734. IPAddresses: []net.IP{net.ParseIP("2003:0db8:85a3:0000:0000:8a2e:0370:7334")},
  1735. },
  1736. want: true,
  1737. wantErr: false,
  1738. },
  1739. {
  1740. name: "ok/mail-excluded",
  1741. options: []NamePolicyOption{
  1742. WithExcludedEmailAddress("@notlocal"),
  1743. },
  1744. cert: &x509.Certificate{
  1745. EmailAddresses: []string{"mail@local"},
  1746. },
  1747. want: true,
  1748. wantErr: false,
  1749. },
  1750. {
  1751. name: "ok/mail-excluded-with-subdomain",
  1752. options: []NamePolicyOption{
  1753. WithExcludedEmailAddress("@local"),
  1754. },
  1755. cert: &x509.Certificate{
  1756. EmailAddresses: []string{"mail@example.local"},
  1757. },
  1758. want: true,
  1759. wantErr: false,
  1760. },
  1761. {
  1762. name: "ok/uri-excluded",
  1763. options: []NamePolicyOption{
  1764. WithExcludedURIDomain("*.google.com"),
  1765. },
  1766. cert: &x509.Certificate{
  1767. URIs: []*url.URL{
  1768. {
  1769. Scheme: "https",
  1770. Host: "www.example.com",
  1771. },
  1772. },
  1773. },
  1774. want: true,
  1775. wantErr: false,
  1776. },
  1777. // SUBJECT SUCCESS TESTS
  1778. {
  1779. name: "ok/subject-empty",
  1780. options: []NamePolicyOption{
  1781. WithSubjectCommonNameVerification(),
  1782. WithPermittedDNSDomain("*.local"),
  1783. },
  1784. cert: &x509.Certificate{
  1785. Subject: pkix.Name{
  1786. CommonName: "",
  1787. },
  1788. DNSNames: []string{"example.local"},
  1789. },
  1790. want: true,
  1791. wantErr: false,
  1792. },
  1793. {
  1794. name: "ok/subject-dns-permitted",
  1795. options: []NamePolicyOption{
  1796. WithSubjectCommonNameVerification(),
  1797. WithPermittedDNSDomain("*.local"),
  1798. },
  1799. cert: &x509.Certificate{
  1800. Subject: pkix.Name{
  1801. CommonName: "example.local",
  1802. },
  1803. },
  1804. want: true,
  1805. wantErr: false,
  1806. },
  1807. {
  1808. name: "ok/subject-dns-excluded",
  1809. options: []NamePolicyOption{
  1810. WithSubjectCommonNameVerification(),
  1811. WithExcludedDNSDomain("*.notlocal"),
  1812. },
  1813. cert: &x509.Certificate{
  1814. Subject: pkix.Name{
  1815. CommonName: "example.local",
  1816. },
  1817. },
  1818. want: true,
  1819. wantErr: false,
  1820. },
  1821. {
  1822. name: "ok/subject-ipv4-permitted",
  1823. options: []NamePolicyOption{
  1824. WithSubjectCommonNameVerification(),
  1825. WithPermittedIPRanges(
  1826. []*net.IPNet{
  1827. {
  1828. IP: net.ParseIP("127.0.0.1"),
  1829. Mask: net.IPv4Mask(255, 255, 255, 0),
  1830. },
  1831. },
  1832. ),
  1833. },
  1834. cert: &x509.Certificate{
  1835. Subject: pkix.Name{
  1836. CommonName: "127.0.0.20",
  1837. },
  1838. },
  1839. want: true,
  1840. wantErr: false,
  1841. },
  1842. {
  1843. name: "ok/subject-ipv4-excluded",
  1844. options: []NamePolicyOption{
  1845. WithSubjectCommonNameVerification(),
  1846. WithExcludedIPRanges(
  1847. []*net.IPNet{
  1848. {
  1849. IP: net.ParseIP("128.0.0.1"),
  1850. Mask: net.IPv4Mask(255, 255, 255, 0),
  1851. },
  1852. },
  1853. ),
  1854. },
  1855. cert: &x509.Certificate{
  1856. Subject: pkix.Name{
  1857. CommonName: "127.0.0.1",
  1858. },
  1859. },
  1860. want: true,
  1861. wantErr: false,
  1862. },
  1863. {
  1864. name: "ok/subject-ipv6-permitted",
  1865. options: []NamePolicyOption{
  1866. WithSubjectCommonNameVerification(),
  1867. WithPermittedIPRanges(
  1868. []*net.IPNet{
  1869. {
  1870. IP: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  1871. Mask: net.CIDRMask(120, 128),
  1872. },
  1873. },
  1874. ),
  1875. },
  1876. cert: &x509.Certificate{
  1877. Subject: pkix.Name{
  1878. CommonName: "2001:0db8:85a3:0000:0000:8a2e:0370:7339",
  1879. },
  1880. },
  1881. want: true,
  1882. wantErr: false,
  1883. },
  1884. {
  1885. name: "ok/subject-ipv6-excluded",
  1886. options: []NamePolicyOption{
  1887. WithSubjectCommonNameVerification(),
  1888. WithExcludedIPRanges(
  1889. []*net.IPNet{
  1890. {
  1891. IP: net.ParseIP("2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
  1892. Mask: net.CIDRMask(120, 128),
  1893. },
  1894. },
  1895. ),
  1896. },
  1897. cert: &x509.Certificate{
  1898. Subject: pkix.Name{
  1899. CommonName: "2009:0db8:85a3:0000:0000:8a2e:0370:7339",
  1900. },
  1901. },
  1902. want: true,
  1903. wantErr: false,
  1904. },
  1905. {
  1906. name: "ok/subject-email-permitted",
  1907. options: []NamePolicyOption{
  1908. WithSubjectCommonNameVerification(),
  1909. WithPermittedEmailAddress("@example.local"),
  1910. },
  1911. cert: &x509.Certificate{
  1912. Subject: pkix.Name{
  1913. CommonName: "mail@example.local",
  1914. },
  1915. },
  1916. want: true,
  1917. wantErr: false,
  1918. },
  1919. {
  1920. name: "ok/subject-email-excluded",
  1921. options: []NamePolicyOption{
  1922. WithSubjectCommonNameVerification(),
  1923. WithExcludedEmailAddress("@example.notlocal"),
  1924. },
  1925. cert: &x509.Certificate{
  1926. Subject: pkix.Name{
  1927. CommonName: "mail@example.local",
  1928. },
  1929. },
  1930. want: true,
  1931. wantErr: false,
  1932. },
  1933. {
  1934. name: "ok/subject-uri-permitted",
  1935. options: []NamePolicyOption{
  1936. WithSubjectCommonNameVerification(),
  1937. WithPermittedURIDomain("*.example.com"),
  1938. },
  1939. cert: &x509.Certificate{
  1940. Subject: pkix.Name{
  1941. CommonName: "https://www.example.com",
  1942. },
  1943. },
  1944. want: true,
  1945. wantErr: false,
  1946. },
  1947. {
  1948. name: "ok/subject-uri-excluded",
  1949. options: []NamePolicyOption{
  1950. WithSubjectCommonNameVerification(),
  1951. WithExcludedURIDomain("*.smallstep.com"),
  1952. },
  1953. cert: &x509.Certificate{
  1954. Subject: pkix.Name{
  1955. CommonName: "https://www.example.com",
  1956. },
  1957. },
  1958. want: true,
  1959. wantErr: false,
  1960. },
  1961. // DIFFERENT SAN TYPE EXCLUDED SUCCESS TESTS
  1962. {
  1963. name: "ok/dns-excluded-with-ip-name", // when only DNS is exluded, we allow anything else
  1964. options: []NamePolicyOption{
  1965. WithExcludedDNSDomain("*.local"),
  1966. },
  1967. cert: &x509.Certificate{
  1968. IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
  1969. },
  1970. want: true,
  1971. wantErr: false,
  1972. },
  1973. {
  1974. name: "ok/dns-excluded-with-mail", // when only DNS is exluded, we allow anything else
  1975. options: []NamePolicyOption{
  1976. WithExcludedDNSDomain("*.local"),
  1977. },
  1978. cert: &x509.Certificate{
  1979. EmailAddresses: []string{"mail@example.com"},
  1980. },
  1981. want: true,
  1982. wantErr: false,
  1983. },
  1984. {
  1985. name: "ok/dns-excluded-with-mail", // when only DNS is exluded, we allow anything else
  1986. options: []NamePolicyOption{
  1987. WithExcludedDNSDomain("*.local"),
  1988. },
  1989. cert: &x509.Certificate{
  1990. URIs: []*url.URL{
  1991. {
  1992. Scheme: "https",
  1993. Host: "www.example.com",
  1994. },
  1995. },
  1996. },
  1997. want: true,
  1998. wantErr: false,
  1999. },
  2000. {
  2001. name: "ok/ip-excluded-with-dns", // when only IP is exluded, we allow anything else
  2002. options: []NamePolicyOption{
  2003. WithExcludedCIDR("127.0.0.1/24"),
  2004. },
  2005. cert: &x509.Certificate{
  2006. DNSNames: []string{"test.local"},
  2007. },
  2008. want: true,
  2009. wantErr: false,
  2010. },
  2011. {
  2012. name: "ok/ip-excluded-with-mail", // when only IP is exluded, we allow anything else
  2013. options: []NamePolicyOption{
  2014. WithExcludedCIDR("127.0.0.1/24"),
  2015. },
  2016. cert: &x509.Certificate{
  2017. EmailAddresses: []string{"mail@example.com"},
  2018. },
  2019. want: true,
  2020. wantErr: false,
  2021. },
  2022. {
  2023. name: "ok/ip-excluded-with-mail", // when only IP is exluded, we allow anything else
  2024. options: []NamePolicyOption{
  2025. WithExcludedCIDR("127.0.0.1/24"),
  2026. },
  2027. cert: &x509.Certificate{
  2028. URIs: []*url.URL{
  2029. {
  2030. Scheme: "https",
  2031. Host: "www.example.com",
  2032. },
  2033. },
  2034. },
  2035. want: true,
  2036. wantErr: false,
  2037. },
  2038. {
  2039. name: "ok/mail-excluded-with-dns", // when only mail is exluded, we allow anything else
  2040. options: []NamePolicyOption{
  2041. WithExcludedEmailAddress("@example.com"),
  2042. },
  2043. cert: &x509.Certificate{
  2044. DNSNames: []string{"test.local"},
  2045. },
  2046. want: true,
  2047. wantErr: false,
  2048. },
  2049. {
  2050. name: "ok/mail-excluded-with-ip", // when only mail is exluded, we allow anything else
  2051. options: []NamePolicyOption{
  2052. WithExcludedEmailAddress("@example.com"),
  2053. },
  2054. cert: &x509.Certificate{
  2055. IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
  2056. },
  2057. want: true,
  2058. wantErr: false,
  2059. },
  2060. {
  2061. name: "ok/mail-excluded-with-uri", // when only mail is exluded, we allow anything else
  2062. options: []NamePolicyOption{
  2063. WithExcludedEmailAddress("@example.com"),
  2064. },
  2065. cert: &x509.Certificate{
  2066. URIs: []*url.URL{
  2067. {
  2068. Scheme: "https",
  2069. Host: "www.example.com",
  2070. },
  2071. },
  2072. },
  2073. want: true,
  2074. wantErr: false,
  2075. },
  2076. {
  2077. name: "ok/uri-excluded-with-dns", // when only URI is exluded, we allow anything else
  2078. options: []NamePolicyOption{
  2079. WithExcludedURIDomain("*.example.local"),
  2080. },
  2081. cert: &x509.Certificate{
  2082. DNSNames: []string{"test.example.local"},
  2083. },
  2084. want: true,
  2085. wantErr: false,
  2086. },
  2087. {
  2088. name: "ok/uri-excluded-with-dns", // when only URI is exluded, we allow anything else
  2089. options: []NamePolicyOption{
  2090. WithExcludedURIDomain("*.example.local"),
  2091. },
  2092. cert: &x509.Certificate{
  2093. IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
  2094. },
  2095. want: true,
  2096. wantErr: false,
  2097. },
  2098. {
  2099. name: "ok/uri-excluded-with-mail", // when only URI is exluded, we allow anything else
  2100. options: []NamePolicyOption{
  2101. WithExcludedURIDomain("*.example.local"),
  2102. },
  2103. cert: &x509.Certificate{
  2104. EmailAddresses: []string{"mail@example.local"},
  2105. },
  2106. want: true,
  2107. wantErr: false,
  2108. },
  2109. {
  2110. name: "ok/dns-excluded-with-subject-ip-name", // when only DNS is exluded, we allow anything else
  2111. options: []NamePolicyOption{
  2112. WithSubjectCommonNameVerification(),
  2113. WithExcludedDNSDomain("*.local"),
  2114. },
  2115. cert: &x509.Certificate{
  2116. Subject: pkix.Name{
  2117. CommonName: "127.0.0.1",
  2118. },
  2119. IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
  2120. },
  2121. want: true,
  2122. wantErr: false,
  2123. },
  2124. // COMBINED SUCCESS TESTS
  2125. {
  2126. name: "ok/combined-simple-permitted",
  2127. options: []NamePolicyOption{
  2128. WithSubjectCommonNameVerification(),
  2129. WithPermittedDNSDomain("*.local"),
  2130. WithPermittedCIDR("127.0.0.1/24"),
  2131. WithPermittedEmailAddress("@example.local"),
  2132. WithPermittedURIDomain("*.example.local"),
  2133. },
  2134. cert: &x509.Certificate{
  2135. Subject: pkix.Name{
  2136. CommonName: "somehost.local",
  2137. },
  2138. DNSNames: []string{"example.local"},
  2139. IPAddresses: []net.IP{net.ParseIP("127.0.0.15")},
  2140. EmailAddresses: []string{"mail@example.local"},
  2141. URIs: []*url.URL{
  2142. {
  2143. Scheme: "https",
  2144. Host: "www.example.local",
  2145. },
  2146. },
  2147. },
  2148. want: true,
  2149. wantErr: false,
  2150. },
  2151. {
  2152. name: "ok/combined-simple-permitted-without-subject-verification",
  2153. options: []NamePolicyOption{
  2154. WithPermittedDNSDomain("*.local"),
  2155. WithPermittedCIDR("127.0.0.1/24"),
  2156. WithPermittedEmailAddress("@example.local"),
  2157. WithPermittedURIDomain("*.example.local"),
  2158. },
  2159. cert: &x509.Certificate{
  2160. Subject: pkix.Name{
  2161. CommonName: "forbidden-but-non-verified-domain.example.com",
  2162. },
  2163. DNSNames: []string{"example.local"},
  2164. IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
  2165. EmailAddresses: []string{"mail@example.local"},
  2166. URIs: []*url.URL{
  2167. {
  2168. Scheme: "https",
  2169. Host: "www.example.local",
  2170. },
  2171. },
  2172. },
  2173. want: true,
  2174. wantErr: false,
  2175. },
  2176. {
  2177. name: "ok/combined-simple-all",
  2178. options: []NamePolicyOption{
  2179. WithSubjectCommonNameVerification(),
  2180. WithPermittedDNSDomain("*.local"),
  2181. WithPermittedCIDR("127.0.0.1/24"),
  2182. WithPermittedEmailAddress("@example.local"),
  2183. WithPermittedURIDomain("*.example.local"),
  2184. WithExcludedDNSDomain("badhost.local"),
  2185. WithExcludedCIDR("127.0.0.128/25"),
  2186. WithExcludedEmailAddress("badmail@example.local"),
  2187. WithExcludedURIDomain("badwww.example.local"),
  2188. },
  2189. cert: &x509.Certificate{
  2190. Subject: pkix.Name{
  2191. CommonName: "somehost.local",
  2192. },
  2193. DNSNames: []string{"example.local"},
  2194. IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
  2195. EmailAddresses: []string{"mail@example.local"},
  2196. URIs: []*url.URL{
  2197. {
  2198. Scheme: "https",
  2199. Host: "www.example.local",
  2200. },
  2201. },
  2202. },
  2203. want: true,
  2204. wantErr: false,
  2205. },
  2206. }
  2207. for _, tt := range tests {
  2208. t.Run(tt.name, func(t *testing.T) {
  2209. engine, err := New(tt.options...)
  2210. assert.FatalError(t, err)
  2211. got, err := engine.IsX509CertificateAllowed(tt.cert)
  2212. if (err != nil) != tt.wantErr {
  2213. t.Errorf("NamePolicyEngine.IsX509CertificateAllowed() error = %v, wantErr %v", err, tt.wantErr)
  2214. return
  2215. }
  2216. if err != nil {
  2217. assert.NotEquals(t, "", err.Error()) // TODO(hs): implement a more specific error comparison?
  2218. }
  2219. if got != tt.want {
  2220. t.Errorf("NamePolicyEngine.IsX509CertificateAllowed() = %v, want %v", got, tt.want)
  2221. }
  2222. // Perform the same tests for a CSR, which are similar to Certificates
  2223. csr := &x509.CertificateRequest{
  2224. Subject: tt.cert.Subject,
  2225. DNSNames: tt.cert.DNSNames,
  2226. EmailAddresses: tt.cert.EmailAddresses,
  2227. IPAddresses: tt.cert.IPAddresses,
  2228. URIs: tt.cert.URIs,
  2229. }
  2230. got, err = engine.IsX509CertificateRequestAllowed(csr)
  2231. if (err != nil) != tt.wantErr {
  2232. t.Errorf("NamePolicyEngine.AreCSRNamesAllowed() error = %v, wantErr %v", err, tt.wantErr)
  2233. return
  2234. }
  2235. if err != nil {
  2236. assert.NotEquals(t, "", err.Error())
  2237. }
  2238. if got != tt.want {
  2239. t.Errorf("NamePolicyEngine.AreCSRNamesAllowed() = %v, want %v", got, tt.want)
  2240. }
  2241. // Perform the same tests for a slice of SANs
  2242. includeSubject := engine.verifySubjectCommonName // copy behavior of the engine when Subject has to be included as a SAN
  2243. sans := extractSANs(tt.cert, includeSubject)
  2244. got, err = engine.AreSANsAllowed(sans)
  2245. if (err != nil) != tt.wantErr {
  2246. t.Errorf("NamePolicyEngine.AreSANsAllowed() error = %v, wantErr %v", err, tt.wantErr)
  2247. return
  2248. }
  2249. if err != nil {
  2250. assert.NotEquals(t, "", err.Error())
  2251. }
  2252. if got != tt.want {
  2253. t.Errorf("NamePolicyEngine.AreSANsAllowed() = %v, want %v", got, tt.want)
  2254. }
  2255. })
  2256. }
  2257. }
  2258. func TestNamePolicyEngine_SSH_ArePrincipalsAllowed(t *testing.T) {
  2259. tests := []struct {
  2260. name string
  2261. options []NamePolicyOption
  2262. cert *ssh.Certificate
  2263. want bool
  2264. wantErr bool
  2265. }{
  2266. {
  2267. name: "fail/host-with-permitted-dns-domain",
  2268. options: []NamePolicyOption{
  2269. WithPermittedDNSDomain("*.local"),
  2270. },
  2271. cert: &ssh.Certificate{
  2272. CertType: ssh.HostCert,
  2273. ValidPrincipals: []string{
  2274. "host.example.com",
  2275. },
  2276. },
  2277. want: false,
  2278. wantErr: true,
  2279. },
  2280. {
  2281. name: "fail/host-with-excluded-dns-domain",
  2282. options: []NamePolicyOption{
  2283. WithExcludedDNSDomain("*.local"),
  2284. },
  2285. cert: &ssh.Certificate{
  2286. CertType: ssh.HostCert,
  2287. ValidPrincipals: []string{
  2288. "host.local",
  2289. },
  2290. },
  2291. want: false,
  2292. wantErr: true,
  2293. },
  2294. {
  2295. name: "fail/host-with-permitted-ip",
  2296. options: []NamePolicyOption{
  2297. WithPermittedCIDR("127.0.0.1/24"),
  2298. },
  2299. cert: &ssh.Certificate{
  2300. CertType: ssh.HostCert,
  2301. ValidPrincipals: []string{
  2302. "192.168.0.22",
  2303. },
  2304. },
  2305. want: false,
  2306. wantErr: true,
  2307. },
  2308. {
  2309. name: "fail/host-with-excluded-ip",
  2310. options: []NamePolicyOption{
  2311. WithExcludedCIDR("127.0.0.1/24"),
  2312. },
  2313. cert: &ssh.Certificate{
  2314. CertType: ssh.HostCert,
  2315. ValidPrincipals: []string{
  2316. "127.0.0.0",
  2317. },
  2318. },
  2319. want: false,
  2320. wantErr: true,
  2321. },
  2322. {
  2323. name: "fail/user-with-permitted-email",
  2324. options: []NamePolicyOption{
  2325. WithPermittedEmailAddress("@example.com"),
  2326. },
  2327. cert: &ssh.Certificate{
  2328. CertType: ssh.UserCert,
  2329. ValidPrincipals: []string{
  2330. "mail@local",
  2331. },
  2332. },
  2333. want: false,
  2334. wantErr: true,
  2335. },
  2336. {
  2337. name: "fail/user-with-excluded-email",
  2338. options: []NamePolicyOption{
  2339. WithExcludedEmailAddress("@example.com"),
  2340. },
  2341. cert: &ssh.Certificate{
  2342. CertType: ssh.UserCert,
  2343. ValidPrincipals: []string{
  2344. "mail@example.com",
  2345. },
  2346. },
  2347. want: false,
  2348. wantErr: true,
  2349. },
  2350. {
  2351. name: "fail/host-with-permitted-principals",
  2352. options: []NamePolicyOption{
  2353. WithPermittedPrincipals([]string{"localhost"}),
  2354. },
  2355. cert: &ssh.Certificate{
  2356. CertType: ssh.HostCert,
  2357. ValidPrincipals: []string{
  2358. "host",
  2359. },
  2360. },
  2361. want: false,
  2362. wantErr: true,
  2363. },
  2364. {
  2365. name: "fail/host-with-excluded-principals",
  2366. options: []NamePolicyOption{
  2367. WithExcludedPrincipals([]string{"localhost"}),
  2368. },
  2369. cert: &ssh.Certificate{
  2370. ValidPrincipals: []string{
  2371. "localhost",
  2372. },
  2373. },
  2374. want: false,
  2375. wantErr: true,
  2376. },
  2377. {
  2378. name: "fail/user-with-permitted-principals",
  2379. options: []NamePolicyOption{
  2380. WithPermittedPrincipals([]string{"user"}),
  2381. },
  2382. cert: &ssh.Certificate{
  2383. CertType: ssh.UserCert,
  2384. ValidPrincipals: []string{
  2385. "root",
  2386. },
  2387. },
  2388. want: false,
  2389. wantErr: true,
  2390. },
  2391. {
  2392. name: "fail/user-with-excluded-principals",
  2393. options: []NamePolicyOption{
  2394. WithExcludedPrincipals([]string{"user"}),
  2395. },
  2396. cert: &ssh.Certificate{
  2397. CertType: ssh.UserCert,
  2398. ValidPrincipals: []string{
  2399. "user",
  2400. },
  2401. },
  2402. want: false,
  2403. wantErr: true,
  2404. },
  2405. {
  2406. name: "fail/user-with-permitted-principal-as-mail",
  2407. options: []NamePolicyOption{
  2408. WithPermittedPrincipals([]string{"ops"}),
  2409. },
  2410. cert: &ssh.Certificate{
  2411. CertType: ssh.UserCert,
  2412. ValidPrincipals: []string{
  2413. "ops@work", // this is (currently) parsed as an email-like principal; not allowed with just "ops" as the permitted principal
  2414. },
  2415. },
  2416. want: false,
  2417. wantErr: true,
  2418. },
  2419. {
  2420. name: "fail/host-principal-with-permitted-dns-domain", // when only DNS is permitted, username principals are not allowed.
  2421. options: []NamePolicyOption{
  2422. WithPermittedDNSDomain("*.local"),
  2423. },
  2424. cert: &ssh.Certificate{
  2425. CertType: ssh.HostCert,
  2426. ValidPrincipals: []string{
  2427. "user",
  2428. },
  2429. },
  2430. want: false,
  2431. wantErr: true,
  2432. },
  2433. {
  2434. name: "fail/host-principal-with-permitted-ip-range", // when only IPs are permitted, username principals are not allowed.
  2435. options: []NamePolicyOption{
  2436. WithPermittedCIDR("127.0.0.1/24"),
  2437. },
  2438. cert: &ssh.Certificate{
  2439. CertType: ssh.HostCert,
  2440. ValidPrincipals: []string{
  2441. "user",
  2442. },
  2443. },
  2444. want: false,
  2445. wantErr: true,
  2446. },
  2447. {
  2448. name: "fail/user-principal-with-permitted-email", // when only emails are permitted, username principals are not allowed.
  2449. options: []NamePolicyOption{
  2450. WithPermittedEmailAddress("@example.com"),
  2451. },
  2452. cert: &ssh.Certificate{
  2453. CertType: ssh.UserCert,
  2454. ValidPrincipals: []string{
  2455. "user",
  2456. },
  2457. },
  2458. want: false,
  2459. wantErr: true,
  2460. },
  2461. {
  2462. name: "fail/combined-user",
  2463. options: []NamePolicyOption{
  2464. WithPermittedEmailAddress("@smallstep.com"),
  2465. WithExcludedEmailAddress("root@smallstep.com"),
  2466. },
  2467. cert: &ssh.Certificate{
  2468. CertType: ssh.UserCert,
  2469. ValidPrincipals: []string{
  2470. "someone@smallstep.com",
  2471. "someone",
  2472. },
  2473. },
  2474. want: false,
  2475. wantErr: true,
  2476. },
  2477. {
  2478. name: "fail/combined-user-with-excluded-user-principal",
  2479. options: []NamePolicyOption{
  2480. WithPermittedEmailAddress("@smallstep.com"),
  2481. WithExcludedPrincipals([]string{"root"}),
  2482. },
  2483. cert: &ssh.Certificate{
  2484. CertType: ssh.UserCert,
  2485. ValidPrincipals: []string{
  2486. "someone@smallstep.com",
  2487. "root",
  2488. },
  2489. },
  2490. want: false,
  2491. wantErr: true,
  2492. },
  2493. {
  2494. name: "ok/host-with-permitted-user-principals",
  2495. options: []NamePolicyOption{
  2496. WithPermittedEmailAddress("@work"),
  2497. },
  2498. cert: &ssh.Certificate{
  2499. CertType: ssh.HostCert,
  2500. ValidPrincipals: []string{
  2501. "example.work",
  2502. },
  2503. },
  2504. want: false,
  2505. wantErr: true,
  2506. },
  2507. {
  2508. name: "ok/user-with-permitted-user-principals",
  2509. options: []NamePolicyOption{
  2510. WithPermittedDNSDomain("*.local"),
  2511. },
  2512. cert: &ssh.Certificate{
  2513. CertType: ssh.UserCert,
  2514. ValidPrincipals: []string{
  2515. "herman@work",
  2516. },
  2517. },
  2518. want: false,
  2519. wantErr: true,
  2520. },
  2521. {
  2522. name: "ok/host-with-permitted-dns-domain",
  2523. options: []NamePolicyOption{
  2524. WithPermittedDNSDomain("*.local"),
  2525. },
  2526. cert: &ssh.Certificate{
  2527. CertType: ssh.HostCert,
  2528. ValidPrincipals: []string{
  2529. "host.local",
  2530. },
  2531. },
  2532. want: true,
  2533. wantErr: false,
  2534. },
  2535. {
  2536. name: "ok/host-with-excluded-dns-domain",
  2537. options: []NamePolicyOption{
  2538. WithExcludedDNSDomain("*.example.com"),
  2539. },
  2540. cert: &ssh.Certificate{
  2541. CertType: ssh.HostCert,
  2542. ValidPrincipals: []string{
  2543. "host.local",
  2544. },
  2545. },
  2546. want: true,
  2547. wantErr: false,
  2548. },
  2549. {
  2550. name: "ok/host-with-permitted-ip",
  2551. options: []NamePolicyOption{
  2552. WithPermittedCIDR("127.0.0.1/24"),
  2553. },
  2554. cert: &ssh.Certificate{
  2555. CertType: ssh.HostCert,
  2556. ValidPrincipals: []string{
  2557. "127.0.0.33",
  2558. },
  2559. },
  2560. want: true,
  2561. wantErr: false,
  2562. },
  2563. {
  2564. name: "ok/host-with-excluded-ip",
  2565. options: []NamePolicyOption{
  2566. WithExcludedCIDR("127.0.0.1/24"),
  2567. },
  2568. cert: &ssh.Certificate{
  2569. CertType: ssh.HostCert,
  2570. ValidPrincipals: []string{
  2571. "192.168.0.35",
  2572. },
  2573. },
  2574. want: true,
  2575. wantErr: false,
  2576. },
  2577. {
  2578. name: "ok/user-with-permitted-email",
  2579. options: []NamePolicyOption{
  2580. WithPermittedEmailAddress("@example.com"),
  2581. },
  2582. cert: &ssh.Certificate{
  2583. CertType: ssh.UserCert,
  2584. ValidPrincipals: []string{
  2585. "mail@example.com",
  2586. },
  2587. },
  2588. want: true,
  2589. wantErr: false,
  2590. },
  2591. {
  2592. name: "ok/user-with-excluded-email",
  2593. options: []NamePolicyOption{
  2594. WithExcludedEmailAddress("@example.com"),
  2595. },
  2596. cert: &ssh.Certificate{
  2597. CertType: ssh.UserCert,
  2598. ValidPrincipals: []string{
  2599. "mail@local",
  2600. },
  2601. },
  2602. want: true,
  2603. wantErr: false,
  2604. },
  2605. {
  2606. name: "ok/user-with-permitted-principals",
  2607. options: []NamePolicyOption{
  2608. WithPermittedPrincipals([]string{"*"}),
  2609. },
  2610. cert: &ssh.Certificate{
  2611. CertType: ssh.UserCert,
  2612. ValidPrincipals: []string{
  2613. "user",
  2614. },
  2615. },
  2616. want: true,
  2617. wantErr: false,
  2618. },
  2619. {
  2620. name: "ok/user-with-excluded-principals",
  2621. options: []NamePolicyOption{
  2622. WithExcludedPrincipals([]string{"user"}),
  2623. },
  2624. cert: &ssh.Certificate{
  2625. CertType: ssh.UserCert,
  2626. ValidPrincipals: []string{
  2627. "root",
  2628. },
  2629. },
  2630. want: true,
  2631. wantErr: false,
  2632. },
  2633. {
  2634. name: "ok/combined-user",
  2635. options: []NamePolicyOption{
  2636. WithPermittedEmailAddress("@smallstep.com"),
  2637. WithPermittedPrincipals([]string{"*"}), // without specifying the wildcard, "someone" would not be allowed.
  2638. WithExcludedEmailAddress("root@smallstep.com"),
  2639. },
  2640. cert: &ssh.Certificate{
  2641. CertType: ssh.UserCert,
  2642. ValidPrincipals: []string{
  2643. "someone@smallstep.com",
  2644. "someone",
  2645. },
  2646. },
  2647. want: true,
  2648. wantErr: false,
  2649. },
  2650. {
  2651. name: "ok/combined-user-with-excluded-user-principal",
  2652. options: []NamePolicyOption{
  2653. WithPermittedEmailAddress("@smallstep.com"),
  2654. WithExcludedEmailAddress("root@smallstep.com"),
  2655. WithExcludedPrincipals([]string{"root"}), // unlike the previous test, this implicitly allows any other username principal
  2656. },
  2657. cert: &ssh.Certificate{
  2658. CertType: ssh.UserCert,
  2659. ValidPrincipals: []string{
  2660. "someone@smallstep.com",
  2661. "someone",
  2662. },
  2663. },
  2664. want: true,
  2665. wantErr: false,
  2666. },
  2667. {
  2668. name: "ok/combined-host",
  2669. options: []NamePolicyOption{
  2670. WithPermittedDNSDomain("*.local"),
  2671. WithPermittedCIDR("127.0.0.1/24"),
  2672. WithExcludedDNSDomain("badhost.local"),
  2673. WithExcludedCIDR("127.0.0.128/25"),
  2674. },
  2675. cert: &ssh.Certificate{
  2676. CertType: ssh.HostCert,
  2677. ValidPrincipals: []string{
  2678. "example.local",
  2679. "127.0.0.31",
  2680. },
  2681. },
  2682. want: true,
  2683. wantErr: false,
  2684. },
  2685. }
  2686. for _, tt := range tests {
  2687. t.Run(tt.name, func(t *testing.T) {
  2688. engine, err := New(tt.options...)
  2689. assert.FatalError(t, err)
  2690. got, err := engine.IsSSHCertificateAllowed(tt.cert)
  2691. if (err != nil) != tt.wantErr {
  2692. t.Errorf("NamePolicyEngine.IsSSHCertificateAllowed() error = %v, wantErr %v", err, tt.wantErr)
  2693. return
  2694. }
  2695. if got != tt.want {
  2696. t.Errorf("NamePolicyEngine.IsSSHCertificateAllowed() = %v, want %v", got, tt.want)
  2697. }
  2698. })
  2699. }
  2700. }
  2701. type result struct {
  2702. wantDNSNames []string
  2703. wantIps []net.IP
  2704. wantEmails []string
  2705. wantUsernames []string
  2706. }
  2707. func emptyResult() result {
  2708. return result{
  2709. wantDNSNames: []string{},
  2710. wantIps: []net.IP{},
  2711. wantEmails: []string{},
  2712. wantUsernames: []string{},
  2713. }
  2714. }
  2715. func Test_splitSSHPrincipals(t *testing.T) {
  2716. type test struct {
  2717. cert *ssh.Certificate
  2718. r result
  2719. wantErr bool
  2720. }
  2721. var tests = map[string]func(t *testing.T) test{
  2722. "fail/unexpected-cert-type": func(t *testing.T) test {
  2723. r := emptyResult()
  2724. return test{
  2725. cert: &ssh.Certificate{
  2726. CertType: uint32(0),
  2727. },
  2728. r: r,
  2729. wantErr: true,
  2730. }
  2731. },
  2732. "fail/user-ip": func(t *testing.T) test {
  2733. r := emptyResult()
  2734. r.wantIps = []net.IP{net.ParseIP("127.0.0.1")}
  2735. return test{
  2736. cert: &ssh.Certificate{
  2737. CertType: ssh.UserCert,
  2738. ValidPrincipals: []string{"127.0.0.1"},
  2739. },
  2740. r: r,
  2741. wantErr: true,
  2742. }
  2743. },
  2744. "fail/user-uri": func(t *testing.T) test {
  2745. r := emptyResult()
  2746. return test{
  2747. cert: &ssh.Certificate{
  2748. CertType: ssh.UserCert,
  2749. ValidPrincipals: []string{"https://host.local/"},
  2750. },
  2751. r: r,
  2752. wantErr: true,
  2753. }
  2754. },
  2755. "fail/host-uri": func(t *testing.T) test {
  2756. r := emptyResult()
  2757. return test{
  2758. cert: &ssh.Certificate{
  2759. CertType: ssh.HostCert,
  2760. ValidPrincipals: []string{"https://host.local/"},
  2761. },
  2762. r: r,
  2763. wantErr: true,
  2764. }
  2765. },
  2766. "ok/host-dns": func(t *testing.T) test {
  2767. r := emptyResult()
  2768. r.wantDNSNames = []string{"host.example.com"}
  2769. return test{
  2770. cert: &ssh.Certificate{
  2771. CertType: ssh.HostCert,
  2772. ValidPrincipals: []string{"host.example.com"},
  2773. },
  2774. r: r,
  2775. wantErr: false,
  2776. }
  2777. },
  2778. "ok/host-ip": func(t *testing.T) test {
  2779. r := emptyResult()
  2780. r.wantIps = []net.IP{net.ParseIP("127.0.0.1")}
  2781. return test{
  2782. cert: &ssh.Certificate{
  2783. CertType: ssh.HostCert,
  2784. ValidPrincipals: []string{"127.0.0.1"},
  2785. },
  2786. r: r,
  2787. wantErr: false,
  2788. }
  2789. },
  2790. "ok/host-email": func(t *testing.T) test {
  2791. r := emptyResult()
  2792. r.wantEmails = []string{"ops@work"}
  2793. return test{
  2794. cert: &ssh.Certificate{
  2795. CertType: ssh.HostCert,
  2796. ValidPrincipals: []string{"ops@work"},
  2797. },
  2798. r: r,
  2799. wantErr: false,
  2800. }
  2801. },
  2802. "ok/user-localhost": func(t *testing.T) test {
  2803. r := emptyResult()
  2804. r.wantUsernames = []string{"localhost"} // when type is User cert, this is considered a username; not a DNS
  2805. return test{
  2806. cert: &ssh.Certificate{
  2807. CertType: ssh.UserCert,
  2808. ValidPrincipals: []string{"localhost"},
  2809. },
  2810. r: r,
  2811. wantErr: false,
  2812. }
  2813. },
  2814. "ok/user-username-with-period": func(t *testing.T) test {
  2815. r := emptyResult()
  2816. r.wantUsernames = []string{"x.joe"}
  2817. return test{
  2818. cert: &ssh.Certificate{
  2819. CertType: ssh.UserCert,
  2820. ValidPrincipals: []string{"x.joe"},
  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. wantErr: false,
  2836. }
  2837. },
  2838. }
  2839. for name, prep := range tests {
  2840. tt := prep(t)
  2841. t.Run(name, func(t *testing.T) {
  2842. gotDNSNames, gotIps, gotEmails, gotUsernames, err := splitSSHPrincipals(tt.cert)
  2843. if (err != nil) != tt.wantErr {
  2844. t.Errorf("splitSSHPrincipals() error = %v, wantErr %v", err, tt.wantErr)
  2845. return
  2846. }
  2847. if !cmp.Equal(tt.r.wantDNSNames, gotDNSNames) {
  2848. t.Errorf("splitSSHPrincipals() DNS names diff =\n%s", cmp.Diff(tt.r.wantDNSNames, gotDNSNames))
  2849. }
  2850. if !cmp.Equal(tt.r.wantIps, gotIps) {
  2851. t.Errorf("splitSSHPrincipals() IPs diff =\n%s", cmp.Diff(tt.r.wantIps, gotIps))
  2852. }
  2853. if !cmp.Equal(tt.r.wantEmails, gotEmails) {
  2854. t.Errorf("splitSSHPrincipals() Emails diff =\n%s", cmp.Diff(tt.r.wantEmails, gotEmails))
  2855. }
  2856. if !cmp.Equal(tt.r.wantUsernames, gotUsernames) {
  2857. t.Errorf("splitSSHPrincipals() Usernames diff =\n%s", cmp.Diff(tt.r.wantUsernames, gotUsernames))
  2858. }
  2859. })
  2860. }
  2861. }