Skip to content

Commit

Permalink
(spyglass/lenses) allow configuration sandbox permissions
Browse files Browse the repository at this point in the history
This provides the ability to configure iframe sandbox permissions pr lense.
This allows the operator of the prow installation to define which permissions
it trust to each lense.

PR comes from the ideas and discussions in
kubernetes-sigs#294

Signed-off-by: Roy Sindre Norangshol <[email protected]>
  • Loading branch information
norrs committed Oct 10, 2024
1 parent 24e7653 commit 2377f0e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
4 changes: 4 additions & 0 deletions cmd/checkconfig/testdata/combined.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ deck:
- (FAIL|Failure \[)\b
- panic\b
- ^E\d{4} \d\d:\d\d:\d\d\.\d\d\d]
iframe_sandbox_permissions:
- allow-scripts
- allow-popups
- allow-popups-to-escape-sandbox
required_files:
- build-log.txt
- lens:
Expand Down
2 changes: 1 addition & 1 deletion cmd/deck/template/spyglass.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<div class="mdl-card__title lens-title"><h3 class="mdl-card__title-text">{{$config.Title}}</h3></div>
<div id="{{$config.Name}}-view-container" class="lens-view-content mdl-card__supporting-text">
<img src="/static/kubernetes-wheel.svg?v={{deckVersion}}" alt="loading spinner" class="loading-spinner is-active lens-card-loading" id="{{$config.Name}}-loading">
<iframe class="lens-container" style="visibility: hidden;" id="iframe-{{$index}}" sandbox="allow-scripts allow-top-navigation allow-popups allow-same-origin" data-lens-index="{{$index}}" data-lens-name="{{$config.Name}}"{{if $config.HideTitle}} data-hide-title="true"{{end}}></iframe>
<iframe class="lens-container" style="visibility: hidden;" id="iframe-{{$index}}" sandbox="{{$config.IframeSandboxPermissions}}" data-lens-index="{{$index}}" data-lens-name="{{$config.Name}}"{{if $config.HideTitle}} data-hide-title="true"{{end}}></iframe>
</div>
</div>
{{end}}
Expand Down
41 changes: 31 additions & 10 deletions pkg/spyglass/lenses/buildlog/lens.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ const (
var defaultHighlightLineLengthMax = 10000 // Default maximum length of a line worth highlighting

type config struct {
HighlightRegexes []string `json:"highlight_regexes"`
HideRawLog bool `json:"hide_raw_log,omitempty"`
Highlighter *highlightConfig `json:"highlighter,omitempty"`
HighlightLengthMax *int `json:"highlight_line_length_max,omitempty"`
HighlightRegexes []string `json:"highlight_regexes"`
HideRawLog bool `json:"hide_raw_log,omitempty"`
Highlighter *highlightConfig `json:"highlighter,omitempty"`
HighlightLengthMax *int `json:"highlight_line_length_max,omitempty"`
IframeSandboxPermissions []string `json:"iframe_sandbox_permissions,omitempty"`
}

type highlightConfig struct {
Expand All @@ -68,10 +69,11 @@ type highlightConfig struct {
}

type parsedConfig struct {
highlightRegex *regexp.Regexp
showRawLog bool
highlighter *highlightConfig
highlightLengthMax int
highlightRegex *regexp.Regexp
showRawLog bool
highlighter *highlightConfig
highlightLengthMax int
IframeSandboxPermissions string
}

var _ api.Lens = Lens{}
Expand All @@ -97,6 +99,17 @@ func (lens Lens) Header(artifacts []api.Artifact, resourceDir string, config jso
// It is only used if highlight_regexes is not specified in the lens config.
var defaultErrRE = regexp.MustCompile(`timed out|ERROR:|(FAIL|Failure \[)\b|panic\b|^E\d{4} \d\d:\d\d:\d\d\.\d\d\d]`)

// defaultSandboxPermissions is the default value for iframe_sandbox_permissions lense config if it is not specified.
var defaultSandboxPermissions = strings.Join(
[]string{
"allow-scripts",
"allow-top-navigation",
"allow-popups",
"allow-same-origin",
},
" ",
)

func init() {
lenses.RegisterLens(Lens{})
}
Expand Down Expand Up @@ -170,8 +183,9 @@ type buildLogsView struct {

func getConfig(rawConfig json.RawMessage) parsedConfig {
conf := parsedConfig{
highlightRegex: defaultErrRE,
showRawLog: true,
highlightRegex: defaultErrRE,
showRawLog: true,
IframeSandboxPermissions: defaultSandboxPermissions,
}

// No config at all is fine.
Expand All @@ -189,6 +203,13 @@ func getConfig(rawConfig json.RawMessage) parsedConfig {
conf.highlighter = nil
}
conf.showRawLog = !c.HideRawLog

if c.IframeSandboxPermissions == nil {
conf.IframeSandboxPermissions = defaultSandboxPermissions
} else {
conf.IframeSandboxPermissions = strings.Join(c.IframeSandboxPermissions, " ")
}

if len(c.HighlightRegexes) == 0 {
return conf
}
Expand Down
11 changes: 10 additions & 1 deletion pkg/spyglass/lenses/buildlog/lens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ import (

func TestGetConfig(t *testing.T) {
def := parsedConfig{
showRawLog: true,
showRawLog: true,
IframeSandboxPermissions: defaultSandboxPermissions,
}
cases := []struct {
name string
Expand All @@ -61,6 +62,14 @@ func TestGetConfig(t *testing.T) {
}
return d
}(),
}, {
name: "configure iframe sandbox permissions",
raw: `{"iframe_sandbox_permissions": ["allow-scripts", "allow-downloads"]}`,
want: func() parsedConfig {
d := def
d.IframeSandboxPermissions = "allow-scripts allow-downloads"
return d
}(),
},
}

Expand Down

0 comments on commit 2377f0e

Please sign in to comment.