engine_test.go 66 KB

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