config_test.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package base
  2. import (
  3. "testing"
  4. )
  5. func TestConfigAllowlistRegexes(t *testing.T) {
  6. tests := map[string]struct {
  7. invalid []string
  8. valid []string
  9. }{
  10. "general placeholders": {
  11. invalid: []string{
  12. `true`, `True`, `false`, `False`, `null`, `NULL`,
  13. },
  14. },
  15. "general placeholders - repeated characters": {
  16. invalid: []string{
  17. `aaaaaaaaaaaaaaaaa`, `BBBBBBBBBBbBBBBBBBbBB`, `********************`,
  18. },
  19. valid: []string{`aaaaaaaaaaaaaaaaaaabaa`, `pas*************d`},
  20. },
  21. "environment variables": {
  22. invalid: []string{`$2`, `$GIT_PASSWORD`, `${GIT_PASSWORD}`, `$password`},
  23. valid: []string{`$yP@R.@=ibxI`, `$2a6WCust9aE`, `${not_complete1`},
  24. },
  25. "interpolated variables - ansible": {
  26. invalid: []string{
  27. `{{ x }}`, `{{ password }}`, `{{password}}`, `{{ data.proxy_password }}`,
  28. `{{ dict1 | ansible.builtin.combine(dict2) }}`,
  29. },
  30. },
  31. "interpolated variables - github actions": {
  32. invalid: []string{
  33. `${{ env.First_Name }}`,
  34. `${{ env.DAY_OF_WEEK == 'Monday' }}`,
  35. `${{env.JAVA_VERSION}}`,
  36. `${{ github.event.issue.title }}`,
  37. `${{ github.repository == "Gattocrucco/lsqfitgp" }}`,
  38. `${{ github.event.pull_request.number || github.ref }}`,
  39. `${{ github.event_name == 'pull_request' && github.event.action == 'unassigned' }}`,
  40. `${{ secrets.SuperSecret }}`,
  41. `${{ vars.JOB_NAME }}`,
  42. `${{ vars.USE_VARIABLES == 'true' }}`,
  43. },
  44. },
  45. "interpolated variables - nuget": {
  46. invalid: []string{
  47. `%MY_PASSWORD%`, `%password%`,
  48. },
  49. },
  50. "interpolated variables - string fmt - golang": {
  51. invalid: []string{
  52. `%b`, `%c`, `%d`, `% d`, `%e`, `%E`, `%f`, `%F`, `%g`, `%G`, `%o`, `%O`, `%p`, `%q`, `%-s`, `%s`, `%t`, `%T`, `%U`, `%#U`, `%+v`, `%#v`, `%v`, `%x`, `%X`,
  53. },
  54. },
  55. "interpolated variables - string fmt - python": {
  56. invalid: []string{
  57. `{}`, `{0}`, `{10}`,
  58. },
  59. },
  60. "interpolated variables - ucd": {
  61. invalid: []string{`@password@`, `@LDAP_PASS@`},
  62. valid: []string{`@username@mastodon.example`},
  63. },
  64. }
  65. cfg := CreateGlobalConfig()
  66. allowlist := cfg.Allowlist
  67. for name, cases := range tests {
  68. t.Run(name, func(t *testing.T) {
  69. for _, c := range cases.invalid {
  70. if !allowlist.RegexAllowed(c) {
  71. t.Errorf("invalid value not marked as allowed: %s", c)
  72. }
  73. }
  74. for _, c := range cases.valid {
  75. if allowlist.RegexAllowed(c) {
  76. t.Errorf("valid value marked as allowed: %s", c)
  77. }
  78. }
  79. })
  80. }
  81. }