config_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. "miscellaneous - file paths": {
  64. invalid: []string{
  65. // MacOS
  66. `/Users/james/Projects/SwiftCode/build/Release`,
  67. // Linux
  68. `/tmp/screen-exchange`,
  69. },
  70. valid: []string{},
  71. },
  72. }
  73. func TestConfigAllowlistRegexes(t *testing.T) {
  74. cfg := CreateGlobalConfig()
  75. allowlists := cfg.Allowlists
  76. for name, cases := range allowlistRegexTests {
  77. t.Run(name, func(t *testing.T) {
  78. for _, c := range cases.invalid {
  79. for _, a := range allowlists {
  80. if !a.RegexAllowed(c) {
  81. t.Errorf("invalid value not marked as allowed: %s", c)
  82. }
  83. }
  84. }
  85. for _, c := range cases.valid {
  86. for _, a := range allowlists {
  87. if a.RegexAllowed(c) {
  88. t.Errorf("valid value marked as allowed: %s", c)
  89. }
  90. }
  91. }
  92. })
  93. }
  94. }
  95. func BenchmarkConfigAllowlistRegexes(b *testing.B) {
  96. cfg := CreateGlobalConfig()
  97. allowlists := cfg.Allowlists
  98. for n := 0; n < b.N; n++ {
  99. for _, cases := range allowlistRegexTests {
  100. for _, c := range cases.invalid {
  101. for _, a := range allowlists {
  102. a.RegexAllowed(c)
  103. }
  104. }
  105. for _, c := range cases.valid {
  106. for _, a := range allowlists {
  107. a.RegexAllowed(c)
  108. }
  109. }
  110. }
  111. }
  112. }
  113. var allowlistPathsTests = map[string]struct {
  114. invalid []string
  115. valid []string
  116. }{
  117. "javascript - common static assets": {
  118. invalid: []string{
  119. `tests/e2e/nuget/wwwroot/lib/bootstrap/dist/js/bootstrap.esm.min.js`,
  120. `src/main/static/lib/angular.1.2.16.min.js`,
  121. `src/main/resources/static/jquery-ui-1.12.1/jquery-ui-min.js`,
  122. `src/main/resources/static/js/jquery-ui-1.10.4.min.js`,
  123. `src-static/js/plotly.min.js`,
  124. `swagger/swaggerui/swagger-ui-bundle.js.map`,
  125. `swagger/swaggerui/swagger-ui-es-bundle.js.map`,
  126. `src/main/static/swagger-ui.min.js`,
  127. `swagger/swaggerui/swagger-ui.js`,
  128. },
  129. },
  130. "python": {
  131. invalid: []string{
  132. // lock files
  133. `Pipfile.lock`, `poetry.lock`,
  134. // virtual environments
  135. "env/lib/python3.7/site-packages/urllib3/util/url.py",
  136. "venv/Lib/site-packages/regex-2018.08.29.dist-info/DESCRIPTION.rst",
  137. "venv/lib64/python3.5/site-packages/pynvml.py",
  138. "python/python3/virtualenv/Lib/site-packages/pyphonetics/utils.py",
  139. "virtualenv/lib64/python3.7/base64.py",
  140. // packages
  141. "cde-root/usr/lib64/python2.4/site-packages/Numeric.pth",
  142. "lib/python3.9/site-packages/setuptools/_distutils/msvccompiler.py",
  143. "lib/python3.8/site-packages/botocore/data/alexaforbusiness/2017-11-09/service-2.json",
  144. "code/python/3.7.4/Lib/site-packages/dask/bytes/tests/test_bytes_utils.py",
  145. "python/3.7.4/Lib/site-packages/fsspec/utils.py",
  146. "python/2.7.16.32/Lib/bsddb/test/test_dbenv.py",
  147. "python/lib/python3.8/site-packages/boto3/data/ec2/2016-04-01/resources-1.json",
  148. // distinfo
  149. "libs/PyX-0.15.dist-info/AUTHORS",
  150. },
  151. },
  152. }
  153. func TestConfigAllowlistPaths(t *testing.T) {
  154. cfg := CreateGlobalConfig()
  155. allowlists := cfg.Allowlists
  156. for name, cases := range allowlistPathsTests {
  157. t.Run(name, func(t *testing.T) {
  158. for _, c := range cases.invalid {
  159. for _, a := range allowlists {
  160. if !a.PathAllowed(c) {
  161. t.Errorf("invalid path not marked as allowed: %s", c)
  162. }
  163. }
  164. }
  165. for _, c := range cases.valid {
  166. for _, a := range allowlists {
  167. if a.PathAllowed(c) {
  168. t.Errorf("valid path marked as allowed: %s", c)
  169. }
  170. }
  171. }
  172. })
  173. }
  174. }
  175. func BenchmarkConfigAllowlistPaths(b *testing.B) {
  176. cfg := CreateGlobalConfig()
  177. allowlists := cfg.Allowlists
  178. for n := 0; n < b.N; n++ {
  179. for _, cases := range allowlistPathsTests {
  180. for _, c := range cases.invalid {
  181. for _, a := range allowlists {
  182. a.PathAllowed(c)
  183. }
  184. }
  185. for _, c := range cases.valid {
  186. for _, a := range allowlists {
  187. a.PathAllowed(c)
  188. }
  189. }
  190. }
  191. }
  192. }