config_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package base
  2. import (
  3. "testing"
  4. )
  5. var allowlistRegexTests = map[string]struct {
  6. invalid []string
  7. valid []string
  8. }{
  9. "general placeholders": {
  10. invalid: []string{
  11. `true`, `True`, `false`, `False`, `null`, `NULL`,
  12. },
  13. },
  14. "general placeholders - repeated characters": {
  15. invalid: []string{
  16. `aaaaaaaaaaaaaaaaa`, `BBBBBBBBBBbBBBBBBBbBB`, `********************`,
  17. },
  18. valid: []string{`aaaaaaaaaaaaaaaaaaabaa`, `pas*************d`},
  19. },
  20. "environment variables": {
  21. invalid: []string{`$2`, `$GIT_PASSWORD`, `${GIT_PASSWORD}`, `$password`},
  22. valid: []string{`$yP@R.@=ibxI`, `$2a6WCust9aE`, `${not_complete1`},
  23. },
  24. "interpolated variables - ansible": {
  25. invalid: []string{
  26. `{{ x }}`, `{{ password }}`, `{{password}}`, `{{ data.proxy_password }}`,
  27. `{{ dict1 | ansible.builtin.combine(dict2) }}`,
  28. },
  29. },
  30. "interpolated variables - github actions": {
  31. invalid: []string{
  32. `${{ env.First_Name }}`,
  33. `${{ env.DAY_OF_WEEK == 'Monday' }}`,
  34. `${{env.JAVA_VERSION}}`,
  35. `${{ github.event.issue.title }}`,
  36. `${{ github.repository == "Gattocrucco/lsqfitgp" }}`,
  37. `${{ github.event.pull_request.number || github.ref }}`,
  38. `${{ github.event_name == 'pull_request' && github.event.action == 'unassigned' }}`,
  39. `${{ secrets.SuperSecret }}`,
  40. `${{ vars.JOB_NAME }}`,
  41. `${{ vars.USE_VARIABLES == 'true' }}`,
  42. },
  43. },
  44. "interpolated variables - nuget": {
  45. invalid: []string{
  46. `%MY_PASSWORD%`, `%password%`,
  47. },
  48. },
  49. "interpolated variables - string fmt - golang": {
  50. invalid: []string{
  51. `%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`,
  52. },
  53. },
  54. "interpolated variables - string fmt - python": {
  55. invalid: []string{
  56. `{}`, `{0}`, `{10}`,
  57. },
  58. },
  59. "interpolated variables - ucd": {
  60. invalid: []string{`@password@`, `@LDAP_PASS@`},
  61. valid: []string{`@username@mastodon.example`},
  62. },
  63. }
  64. func TestConfigAllowlistRegexes(t *testing.T) {
  65. cfg := CreateGlobalConfig()
  66. allowlist := cfg.Allowlist
  67. for name, cases := range allowlistRegexTests {
  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. }
  82. func BenchmarkConfigAllowlistRegexes(b *testing.B) {
  83. cfg := CreateGlobalConfig()
  84. allowlist := cfg.Allowlist
  85. for n := 0; n < b.N; n++ {
  86. for _, cases := range allowlistRegexTests {
  87. for _, c := range cases.invalid {
  88. allowlist.RegexAllowed(c)
  89. }
  90. for _, c := range cases.valid {
  91. allowlist.RegexAllowed(c)
  92. }
  93. }
  94. }
  95. }
  96. var allowlistPathsTests = map[string]struct {
  97. invalid []string
  98. valid []string
  99. }{
  100. "javascript - common static assets": {
  101. invalid: []string{
  102. `tests/e2e/nuget/wwwroot/lib/bootstrap/dist/js/bootstrap.esm.min.js`,
  103. `src/main/static/lib/angular.1.2.16.min.js`,
  104. `src/main/resources/static/jquery-ui-1.12.1/jquery-ui-min.js`,
  105. `src/main/resources/static/js/jquery-ui-1.10.4.min.js`,
  106. `src-static/js/plotly.min.js`,
  107. `swagger/swaggerui/swagger-ui-bundle.js.map`,
  108. `swagger/swaggerui/swagger-ui-es-bundle.js.map`,
  109. `src/main/static/swagger-ui.min.js`,
  110. `swagger/swaggerui/swagger-ui.js`,
  111. },
  112. },
  113. "python": {
  114. invalid: []string{
  115. // lock files
  116. `Pipfile.lock`, `poetry.lock`,
  117. // virtual environments
  118. "env/lib/python3.7/site-packages/urllib3/util/url.py",
  119. "venv/Lib/site-packages/regex-2018.08.29.dist-info/DESCRIPTION.rst",
  120. "venv/lib64/python3.5/site-packages/pynvml.py",
  121. "python/python3/virtualenv/Lib/site-packages/pyphonetics/utils.py",
  122. "virtualenv/lib64/python3.7/base64.py",
  123. // packages
  124. "cde-root/usr/lib64/python2.4/site-packages/Numeric.pth",
  125. "lib/python3.9/site-packages/setuptools/_distutils/msvccompiler.py",
  126. "lib/python3.8/site-packages/botocore/data/alexaforbusiness/2017-11-09/service-2.json",
  127. "code/python/3.7.4/Lib/site-packages/dask/bytes/tests/test_bytes_utils.py",
  128. "python/3.7.4/Lib/site-packages/fsspec/utils.py",
  129. "python/2.7.16.32/Lib/bsddb/test/test_dbenv.py",
  130. "python/lib/python3.8/site-packages/boto3/data/ec2/2016-04-01/resources-1.json",
  131. // distinfo
  132. "libs/PyX-0.15.dist-info/AUTHORS",
  133. },
  134. },
  135. }
  136. func TestConfigAllowlistPaths(t *testing.T) {
  137. cfg := CreateGlobalConfig()
  138. allowlist := cfg.Allowlist
  139. for name, cases := range allowlistPathsTests {
  140. t.Run(name, func(t *testing.T) {
  141. for _, c := range cases.invalid {
  142. if !allowlist.PathAllowed(c) {
  143. t.Errorf("invalid path not marked as allowed: %s", c)
  144. }
  145. }
  146. for _, c := range cases.valid {
  147. if allowlist.PathAllowed(c) {
  148. t.Errorf("valid path marked as allowed: %s", c)
  149. }
  150. }
  151. })
  152. }
  153. }
  154. func BenchmarkConfigAllowlistPaths(b *testing.B) {
  155. cfg := CreateGlobalConfig()
  156. allowlist := cfg.Allowlist
  157. for n := 0; n < b.N; n++ {
  158. for _, cases := range allowlistPathsTests {
  159. for _, c := range cases.invalid {
  160. allowlist.PathAllowed(c)
  161. }
  162. for _, c := range cases.valid {
  163. allowlist.PathAllowed(c)
  164. }
  165. }
  166. }
  167. }