-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsandbox.go
113 lines (100 loc) · 2.74 KB
/
sandbox.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package twig
import (
"fmt"
)
// SecurityPolicy defines what's allowed in a sandboxed template context
type SecurityPolicy interface {
// Function permissions
IsFunctionAllowed(function string) bool
// Filter permissions
IsFilterAllowed(filter string) bool
// Tag permissions
IsTagAllowed(tag string) bool
}
// DefaultSecurityPolicy implements a simple security policy
type DefaultSecurityPolicy struct {
AllowedFunctions map[string]bool
AllowedFilters map[string]bool
AllowedTags map[string]bool
}
// NewDefaultSecurityPolicy creates a security policy with safe defaults
func NewDefaultSecurityPolicy() *DefaultSecurityPolicy {
return &DefaultSecurityPolicy{
AllowedFunctions: map[string]bool{
// Basic functions
"range": true,
"cycle": true,
"date": true,
"min": true,
"max": true,
"random": true,
"length": true,
"merge": true,
},
AllowedFilters: map[string]bool{
// Basic filters
"escape": true,
"e": true,
"raw": true,
"length": true,
"count": true,
"lower": true,
"upper": true,
"title": true,
"capitalize": true,
"trim": true,
"nl2br": true,
"join": true,
"split": true,
"default": true,
"date": true,
"abs": true,
"first": true,
"last": true,
"reverse": true,
"sort": true,
"slice": true,
},
AllowedTags: map[string]bool{
// Basic control tags
"if": true,
"else": true,
"elseif": true,
"for": true,
"set": true,
"verbatim": true,
},
}
}
// IsFunctionAllowed checks if a function is allowed
func (p *DefaultSecurityPolicy) IsFunctionAllowed(function string) bool {
return p.AllowedFunctions[function]
}
// IsFilterAllowed checks if a filter is allowed
func (p *DefaultSecurityPolicy) IsFilterAllowed(filter string) bool {
return p.AllowedFilters[filter]
}
// IsTagAllowed checks if a tag is allowed
func (p *DefaultSecurityPolicy) IsTagAllowed(tag string) bool {
return p.AllowedTags[tag]
}
// SecurityViolation represents a sandbox security violation
type SecurityViolation struct {
Message string
}
// Error returns the error message
func (v *SecurityViolation) Error() string {
return fmt.Sprintf("Sandbox security violation: %s", v.Message)
}
// NewFunctionViolation creates a function security violation
func NewFunctionViolation(function string) error {
return &SecurityViolation{
Message: fmt.Sprintf("Function '%s' is not allowed in sandbox mode", function),
}
}
// NewFilterViolation creates a filter security violation
func NewFilterViolation(filter string) error {
return &SecurityViolation{
Message: fmt.Sprintf("Filter '%s' is not allowed in sandbox mode", filter),
}
}