options.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. package policy
  2. import (
  3. "fmt"
  4. "net"
  5. "strings"
  6. "golang.org/x/net/idna"
  7. )
  8. type NamePolicyOption func(e *NamePolicyEngine) error
  9. // TODO: wrap (more) errors; and prove a set of known (exported) errors
  10. func WithSubjectCommonNameVerification() NamePolicyOption {
  11. return func(e *NamePolicyEngine) error {
  12. e.verifySubjectCommonName = true
  13. return nil
  14. }
  15. }
  16. func WithAllowLiteralWildcardNames() NamePolicyOption {
  17. return func(e *NamePolicyEngine) error {
  18. e.allowLiteralWildcardNames = true
  19. return nil
  20. }
  21. }
  22. func WithPermittedCommonNames(commonNames ...string) NamePolicyOption {
  23. return func(g *NamePolicyEngine) error {
  24. normalizedCommonNames := make([]string, len(commonNames))
  25. for i, commonName := range commonNames {
  26. normalizedCommonName, err := normalizeAndValidateCommonName(commonName)
  27. if err != nil {
  28. return fmt.Errorf("cannot parse permitted common name constraint %q: %w", commonName, err)
  29. }
  30. normalizedCommonNames[i] = normalizedCommonName
  31. }
  32. g.permittedCommonNames = normalizedCommonNames
  33. return nil
  34. }
  35. }
  36. func WithExcludedCommonNames(commonNames ...string) NamePolicyOption {
  37. return func(g *NamePolicyEngine) error {
  38. normalizedCommonNames := make([]string, len(commonNames))
  39. for i, commonName := range commonNames {
  40. normalizedCommonName, err := normalizeAndValidateCommonName(commonName)
  41. if err != nil {
  42. return fmt.Errorf("cannot parse excluded common name constraint %q: %w", commonName, err)
  43. }
  44. normalizedCommonNames[i] = normalizedCommonName
  45. }
  46. g.excludedCommonNames = normalizedCommonNames
  47. return nil
  48. }
  49. }
  50. func WithPermittedDNSDomains(domains ...string) NamePolicyOption {
  51. return func(e *NamePolicyEngine) error {
  52. normalizedDomains := make([]string, len(domains))
  53. for i, domain := range domains {
  54. normalizedDomain, err := normalizeAndValidateDNSDomainConstraint(domain)
  55. if err != nil {
  56. return fmt.Errorf("cannot parse permitted domain constraint %q: %w", domain, err)
  57. }
  58. normalizedDomains[i] = normalizedDomain
  59. }
  60. e.permittedDNSDomains = normalizedDomains
  61. return nil
  62. }
  63. }
  64. func WithExcludedDNSDomains(domains ...string) NamePolicyOption {
  65. return func(e *NamePolicyEngine) error {
  66. normalizedDomains := make([]string, len(domains))
  67. for i, domain := range domains {
  68. normalizedDomain, err := normalizeAndValidateDNSDomainConstraint(domain)
  69. if err != nil {
  70. return fmt.Errorf("cannot parse excluded domain constraint %q: %w", domain, err)
  71. }
  72. normalizedDomains[i] = normalizedDomain
  73. }
  74. e.excludedDNSDomains = normalizedDomains
  75. return nil
  76. }
  77. }
  78. func WithPermittedIPRanges(ipRanges ...*net.IPNet) NamePolicyOption {
  79. return func(e *NamePolicyEngine) error {
  80. e.permittedIPRanges = ipRanges
  81. return nil
  82. }
  83. }
  84. func WithPermittedCIDRs(cidrs ...string) NamePolicyOption {
  85. return func(e *NamePolicyEngine) error {
  86. networks := make([]*net.IPNet, len(cidrs))
  87. for i, cidr := range cidrs {
  88. _, nw, err := net.ParseCIDR(cidr)
  89. if err != nil {
  90. return fmt.Errorf("cannot parse permitted CIDR constraint %q", cidr)
  91. }
  92. networks[i] = nw
  93. }
  94. e.permittedIPRanges = networks
  95. return nil
  96. }
  97. }
  98. func WithExcludedCIDRs(cidrs ...string) NamePolicyOption {
  99. return func(e *NamePolicyEngine) error {
  100. networks := make([]*net.IPNet, len(cidrs))
  101. for i, cidr := range cidrs {
  102. _, nw, err := net.ParseCIDR(cidr)
  103. if err != nil {
  104. return fmt.Errorf("cannot parse excluded CIDR constraint %q", cidr)
  105. }
  106. networks[i] = nw
  107. }
  108. e.excludedIPRanges = networks
  109. return nil
  110. }
  111. }
  112. func WithPermittedIPsOrCIDRs(ipsOrCIDRs ...string) NamePolicyOption {
  113. return func(e *NamePolicyEngine) error {
  114. networks := make([]*net.IPNet, len(ipsOrCIDRs))
  115. for i, ipOrCIDR := range ipsOrCIDRs {
  116. _, nw, err := net.ParseCIDR(ipOrCIDR)
  117. if err == nil {
  118. networks[i] = nw
  119. } else if ip := net.ParseIP(ipOrCIDR); ip != nil {
  120. networks[i] = networkFor(ip)
  121. } else {
  122. return fmt.Errorf("cannot parse permitted constraint %q as IP nor CIDR", ipOrCIDR)
  123. }
  124. }
  125. e.permittedIPRanges = networks
  126. return nil
  127. }
  128. }
  129. func WithExcludedIPsOrCIDRs(ipsOrCIDRs ...string) NamePolicyOption {
  130. return func(e *NamePolicyEngine) error {
  131. networks := make([]*net.IPNet, len(ipsOrCIDRs))
  132. for i, ipOrCIDR := range ipsOrCIDRs {
  133. _, nw, err := net.ParseCIDR(ipOrCIDR)
  134. if err == nil {
  135. networks[i] = nw
  136. } else if ip := net.ParseIP(ipOrCIDR); ip != nil {
  137. networks[i] = networkFor(ip)
  138. } else {
  139. return fmt.Errorf("cannot parse excluded constraint %q as IP nor CIDR", ipOrCIDR)
  140. }
  141. }
  142. e.excludedIPRanges = networks
  143. return nil
  144. }
  145. }
  146. func WithExcludedIPRanges(ipRanges ...*net.IPNet) NamePolicyOption {
  147. return func(e *NamePolicyEngine) error {
  148. e.excludedIPRanges = ipRanges
  149. return nil
  150. }
  151. }
  152. func WithPermittedEmailAddresses(emailAddresses ...string) NamePolicyOption {
  153. return func(e *NamePolicyEngine) error {
  154. normalizedEmailAddresses := make([]string, len(emailAddresses))
  155. for i, email := range emailAddresses {
  156. normalizedEmailAddress, err := normalizeAndValidateEmailConstraint(email)
  157. if err != nil {
  158. return fmt.Errorf("cannot parse permitted email constraint %q: %w", email, err)
  159. }
  160. normalizedEmailAddresses[i] = normalizedEmailAddress
  161. }
  162. e.permittedEmailAddresses = normalizedEmailAddresses
  163. return nil
  164. }
  165. }
  166. func WithExcludedEmailAddresses(emailAddresses ...string) NamePolicyOption {
  167. return func(e *NamePolicyEngine) error {
  168. normalizedEmailAddresses := make([]string, len(emailAddresses))
  169. for i, email := range emailAddresses {
  170. normalizedEmailAddress, err := normalizeAndValidateEmailConstraint(email)
  171. if err != nil {
  172. return fmt.Errorf("cannot parse excluded email constraint %q: %w", email, err)
  173. }
  174. normalizedEmailAddresses[i] = normalizedEmailAddress
  175. }
  176. e.excludedEmailAddresses = normalizedEmailAddresses
  177. return nil
  178. }
  179. }
  180. func WithPermittedURIDomains(uriDomains ...string) NamePolicyOption {
  181. return func(e *NamePolicyEngine) error {
  182. normalizedURIDomains := make([]string, len(uriDomains))
  183. for i, domain := range uriDomains {
  184. normalizedURIDomain, err := normalizeAndValidateURIDomainConstraint(domain)
  185. if err != nil {
  186. return fmt.Errorf("cannot parse permitted URI domain constraint %q: %w", domain, err)
  187. }
  188. normalizedURIDomains[i] = normalizedURIDomain
  189. }
  190. e.permittedURIDomains = normalizedURIDomains
  191. return nil
  192. }
  193. }
  194. func WithExcludedURIDomains(domains ...string) NamePolicyOption {
  195. return func(e *NamePolicyEngine) error {
  196. normalizedURIDomains := make([]string, len(domains))
  197. for i, domain := range domains {
  198. normalizedURIDomain, err := normalizeAndValidateURIDomainConstraint(domain)
  199. if err != nil {
  200. return fmt.Errorf("cannot parse excluded URI domain constraint %q: %w", domain, err)
  201. }
  202. normalizedURIDomains[i] = normalizedURIDomain
  203. }
  204. e.excludedURIDomains = normalizedURIDomains
  205. return nil
  206. }
  207. }
  208. func WithPermittedPrincipals(principals ...string) NamePolicyOption {
  209. return func(g *NamePolicyEngine) error {
  210. g.permittedPrincipals = principals
  211. return nil
  212. }
  213. }
  214. func WithExcludedPrincipals(principals ...string) NamePolicyOption {
  215. return func(g *NamePolicyEngine) error {
  216. g.excludedPrincipals = principals
  217. return nil
  218. }
  219. }
  220. func networkFor(ip net.IP) *net.IPNet {
  221. var mask net.IPMask
  222. if !isIPv4(ip) {
  223. mask = net.CIDRMask(128, 128)
  224. } else {
  225. mask = net.CIDRMask(32, 32)
  226. }
  227. nw := &net.IPNet{
  228. IP: ip,
  229. Mask: mask,
  230. }
  231. return nw
  232. }
  233. func isIPv4(ip net.IP) bool {
  234. return ip.To4() != nil
  235. }
  236. func normalizeAndValidateCommonName(constraint string) (string, error) {
  237. normalizedConstraint := strings.ToLower(strings.TrimSpace(constraint))
  238. if normalizedConstraint == "" {
  239. return "", fmt.Errorf("contraint %q can not be empty or white space string", constraint)
  240. }
  241. if normalizedConstraint == "*" {
  242. return "", fmt.Errorf("wildcard constraint %q is not supported", constraint)
  243. }
  244. return normalizedConstraint, nil
  245. }
  246. func normalizeAndValidateDNSDomainConstraint(constraint string) (string, error) {
  247. normalizedConstraint := strings.ToLower(strings.TrimSpace(constraint))
  248. if normalizedConstraint == "" {
  249. return "", fmt.Errorf("contraint %q can not be empty or white space string", constraint)
  250. }
  251. if strings.Contains(normalizedConstraint, "..") {
  252. return "", fmt.Errorf("domain constraint %q cannot have empty labels", constraint)
  253. }
  254. if strings.HasPrefix(normalizedConstraint, ".") {
  255. return "", fmt.Errorf("domain constraint %q with wildcard should start with *", constraint)
  256. }
  257. if strings.LastIndex(normalizedConstraint, "*") > 0 {
  258. return "", fmt.Errorf("domain constraint %q can only have wildcard as starting character", constraint)
  259. }
  260. if len(normalizedConstraint) >= 2 && normalizedConstraint[0] == '*' && normalizedConstraint[1] != '.' {
  261. return "", fmt.Errorf("wildcard character in domain constraint %q can only be used to match (full) labels", constraint)
  262. }
  263. if strings.HasPrefix(normalizedConstraint, "*.") {
  264. normalizedConstraint = normalizedConstraint[1:] // cut off wildcard character; keep the period
  265. }
  266. normalizedConstraint, err := idna.Lookup.ToASCII(normalizedConstraint)
  267. if err != nil {
  268. return "", fmt.Errorf("domain constraint %q can not be converted to ASCII: %w", constraint, err)
  269. }
  270. if _, ok := domainToReverseLabels(normalizedConstraint); !ok {
  271. return "", fmt.Errorf("cannot parse domain constraint %q", constraint)
  272. }
  273. return normalizedConstraint, nil
  274. }
  275. func normalizeAndValidateEmailConstraint(constraint string) (string, error) {
  276. normalizedConstraint := strings.ToLower(strings.TrimSpace(constraint))
  277. if normalizedConstraint == "" {
  278. return "", fmt.Errorf("email contraint %q can not be empty or white space string", constraint)
  279. }
  280. if strings.Contains(normalizedConstraint, "*") {
  281. return "", fmt.Errorf("email constraint %q cannot contain asterisk wildcard", constraint)
  282. }
  283. if strings.Count(normalizedConstraint, "@") > 1 {
  284. return "", fmt.Errorf("email constraint %q contains too many @ characters", constraint)
  285. }
  286. if normalizedConstraint[0] == '@' {
  287. normalizedConstraint = normalizedConstraint[1:] // remove the leading @ as wildcard for emails
  288. }
  289. if normalizedConstraint[0] == '.' {
  290. return "", fmt.Errorf("email constraint %q cannot start with period", constraint)
  291. }
  292. if strings.Contains(normalizedConstraint, "@") {
  293. mailbox, ok := parseRFC2821Mailbox(normalizedConstraint)
  294. if !ok {
  295. return "", fmt.Errorf("cannot parse email constraint %q as RFC 2821 mailbox", constraint)
  296. }
  297. // According to RFC 5280, section 7.5, emails are considered to match if the local part is
  298. // an exact match and the host (domain) part matches the ASCII representation (case-insensitive):
  299. // https://datatracker.ietf.org/doc/html/rfc5280#section-7.5
  300. domainASCII, err := idna.Lookup.ToASCII(mailbox.domain)
  301. if err != nil {
  302. return "", fmt.Errorf("email constraint %q domain part %q cannot be converted to ASCII: %w", constraint, mailbox.domain, err)
  303. }
  304. normalizedConstraint = mailbox.local + "@" + domainASCII
  305. } else {
  306. var err error
  307. normalizedConstraint, err = idna.Lookup.ToASCII(normalizedConstraint)
  308. if err != nil {
  309. return "", fmt.Errorf("email constraint %q cannot be converted to ASCII: %w", constraint, err)
  310. }
  311. }
  312. if _, ok := domainToReverseLabels(normalizedConstraint); !ok {
  313. return "", fmt.Errorf("cannot parse email domain constraint %q", constraint)
  314. }
  315. return normalizedConstraint, nil
  316. }
  317. func normalizeAndValidateURIDomainConstraint(constraint string) (string, error) {
  318. normalizedConstraint := strings.ToLower(strings.TrimSpace(constraint))
  319. if normalizedConstraint == "" {
  320. return "", fmt.Errorf("URI domain contraint %q cannot be empty or white space string", constraint)
  321. }
  322. if strings.Contains(normalizedConstraint, "://") {
  323. return "", fmt.Errorf("URI domain constraint %q contains scheme (not supported yet)", constraint)
  324. }
  325. if strings.Contains(normalizedConstraint, "..") {
  326. return "", fmt.Errorf("URI domain constraint %q cannot have empty labels", constraint)
  327. }
  328. if strings.HasPrefix(normalizedConstraint, ".") {
  329. return "", fmt.Errorf("URI domain constraint %q with wildcard should start with *", constraint)
  330. }
  331. if strings.LastIndex(normalizedConstraint, "*") > 0 {
  332. return "", fmt.Errorf("URI domain constraint %q can only have wildcard as starting character", constraint)
  333. }
  334. if strings.HasPrefix(normalizedConstraint, "*.") {
  335. normalizedConstraint = normalizedConstraint[1:] // cut off wildcard character; keep the period
  336. }
  337. // we're being strict with square brackets in domains; we don't allow them, no matter what
  338. if strings.Contains(normalizedConstraint, "[") || strings.Contains(normalizedConstraint, "]") {
  339. return "", fmt.Errorf("URI domain constraint %q contains invalid square brackets", constraint)
  340. }
  341. if _, _, err := net.SplitHostPort(normalizedConstraint); err == nil {
  342. // a successful split (likely) with host and port; we don't currently allow ports in the config
  343. return "", fmt.Errorf("URI domain constraint %q cannot contain port", constraint)
  344. }
  345. // check if the host part of the URI domain constraint is an IP
  346. if net.ParseIP(normalizedConstraint) != nil {
  347. return "", fmt.Errorf("URI domain constraint %q cannot be an IP", constraint)
  348. }
  349. normalizedConstraint, err := idna.Lookup.ToASCII(normalizedConstraint)
  350. if err != nil {
  351. return "", fmt.Errorf("URI domain constraint %q cannot be converted to ASCII: %w", constraint, err)
  352. }
  353. _, ok := domainToReverseLabels(normalizedConstraint)
  354. if !ok {
  355. return "", fmt.Errorf("cannot parse URI domain constraint %q", constraint)
  356. }
  357. return normalizedConstraint, nil
  358. }