config_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. }
  82. func TestConfigAllowlistPaths(t *testing.T) {
  83. tests := map[string]struct {
  84. invalid []string
  85. valid []string
  86. }{
  87. "javascript - common static assets": {
  88. invalid: []string{
  89. `tests/e2e/nuget/wwwroot/lib/bootstrap/dist/js/bootstrap.esm.min.js`,
  90. `src/main/static/lib/angular.1.2.16.min.js`,
  91. `src/main/resources/static/jquery-ui-1.12.1/jquery-ui-min.js`,
  92. `src/main/resources/static/js/jquery-ui-1.10.4.min.js`,
  93. `src-static/js/plotly.min.js`,
  94. `swagger/swaggerui/swagger-ui-bundle.js.map`,
  95. `swagger/swaggerui/swagger-ui-es-bundle.js.map`,
  96. `src/main/static/swagger-ui.min.js`,
  97. `swagger/swaggerui/swagger-ui.js`,
  98. },
  99. },
  100. "python": {
  101. invalid: []string{
  102. // lock files
  103. `Pipfile.lock`, `poetry.lock`,
  104. // virtual environments
  105. "env/lib/python3.7/site-packages/urllib3/util/url.py",
  106. "venv/Lib/site-packages/regex-2018.08.29.dist-info/DESCRIPTION.rst",
  107. "venv/lib64/python3.5/site-packages/pynvml.py",
  108. "python/python3/virtualenv/Lib/site-packages/pyphonetics/utils.py",
  109. "virtualenv/lib64/python3.7/base64.py",
  110. // packages
  111. "cde-root/usr/lib64/python2.4/site-packages/Numeric.pth",
  112. "lib/python3.9/site-packages/setuptools/_distutils/msvccompiler.py",
  113. "lib/python3.8/site-packages/botocore/data/alexaforbusiness/2017-11-09/service-2.json",
  114. "code/python/3.7.4/Lib/site-packages/dask/bytes/tests/test_bytes_utils.py",
  115. "python/3.7.4/Lib/site-packages/fsspec/utils.py",
  116. "python/2.7.16.32/Lib/bsddb/test/test_dbenv.py",
  117. "python/lib/python3.8/site-packages/boto3/data/ec2/2016-04-01/resources-1.json",
  118. // distinfo
  119. "libs/PyX-0.15.dist-info/AUTHORS",
  120. },
  121. },
  122. }
  123. cfg := CreateGlobalConfig()
  124. allowlist := cfg.Allowlist
  125. for name, cases := range tests {
  126. t.Run(name, func(t *testing.T) {
  127. for _, c := range cases.invalid {
  128. if !allowlist.PathAllowed(c) {
  129. t.Errorf("invalid path not marked as allowed: %s", c)
  130. }
  131. }
  132. for _, c := range cases.valid {
  133. if allowlist.PathAllowed(c) {
  134. t.Errorf("valid path marked as allowed: %s", c)
  135. }
  136. }
  137. })
  138. }
  139. }