File tree Expand file tree Collapse file tree 2 files changed +73
-0
lines changed
main/groovy/com/cloudogu/gitops/utils
test/groovy/com/cloudogu/gitops/utils Expand file tree Collapse file tree 2 files changed +73
-0
lines changed Original file line number Diff line number Diff line change 1+ package com.cloudogu.gitops.utils
2+
3+ import freemarker.template.DefaultObjectWrapper
4+ import freemarker.template.TemplateHashModel
5+ import freemarker.template.TemplateModel
6+ import freemarker.template.TemplateModelException
7+ import freemarker.template.Version
8+
9+
10+ class AllowlistFreemarkerObjectWrapper extends DefaultObjectWrapper {
11+
12+ private Set<String > allowlist = []
13+
14+ AllowlistFreemarkerObjectWrapper (Version freemarkerVersion , Set<String > allowlist ) {
15+ super (freemarkerVersion)
16+ this . allowlist = allowlist
17+ }
18+
19+ @Override
20+ public TemplateHashModel getStaticModels () {
21+ // Hole alle statischen Modelle
22+ TemplateHashModel staticModels = super . getStaticModels()
23+
24+ // Filtere die Modelle basierend auf der Allowlist
25+ return new TemplateHashModel () {
26+ @Override
27+ TemplateModel get (String key ) throws TemplateModelException {
28+ if (allowlist. contains(key)) {
29+ return staticModels. get(key)
30+ }
31+ return null
32+ }
33+
34+ @Override
35+ boolean isEmpty () throws TemplateModelException {
36+ return allowlist. isEmpty()
37+ }
38+ }
39+ }
40+ }
Original file line number Diff line number Diff line change 1+ package com.cloudogu.gitops.utils
2+
3+ import freemarker.template.Configuration
4+ import org.junit.jupiter.api.Test
5+
6+ import static org.junit.jupiter.api.Assertions.*
7+
8+ class AllowlistFreemarkerObjectWrapperTest {
9+
10+ @Test
11+ void ' should allow access to whitelisted static models' () {
12+ def wrapper = new AllowlistFreemarkerObjectWrapper (Configuration . VERSION_2_3_32 , [" java.lang.String" ] as Set )
13+ def staticModels = wrapper. getStaticModels()
14+
15+ assertNotNull (staticModels. get(" java.lang.String" ))
16+ }
17+
18+ @Test
19+ void ' should deny access to non-whitelisted static models' () {
20+ def wrapper = new AllowlistFreemarkerObjectWrapper (Configuration . VERSION_2_3_32 , [" java.lang.String" ] as Set )
21+ def staticModels = wrapper. getStaticModels()
22+
23+ assertNull (staticModels. get(" java.lang.Integer" ))
24+ }
25+
26+ @Test
27+ void ' should return true for isEmpty when allowlist is empty' () {
28+ def wrapper = new AllowlistFreemarkerObjectWrapper (Configuration . VERSION_2_3_32 , [] as Set )
29+ def staticModels = wrapper. getStaticModels()
30+
31+ assertTrue (staticModels. isEmpty())
32+ }
33+ }
You can’t perform that action at this time.
0 commit comments