engine_test.go 78 KB

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