@@ -48,6 +48,131 @@ type ConsolePluginSpec struct {
4848 // i18n is the configuration of plugin's localization resources.
4949 // +optional
5050 I18n ConsolePluginI18n `json:"i18n"`
51+ // contentSecurityPolicy is a list of Content-Security-Policy (CSP) directives for the plugin.
52+ // Each directive specifies a list of values, appropriate for the given directive type,
53+ // for example a list of remote endpoints for fetch directives such as ScriptSrc.
54+ // Console web application uses CSP to detect and mitigate certain types of attacks,
55+ // such as cross-site scripting (XSS) and data injection attacks.
56+ // Dynamic plugins should specify this field if need to load assets from outside
57+ // the cluster or if violation reports are observed. Dynamic plugins should always prefer
58+ // loading their assets from within the cluster, either by vendoring them, or fetching
59+ // from a cluster service.
60+ // CSP violation reports can be viewed in the browser's console logs during development and
61+ // testing of the plugin in the OpenShift web console.
62+ // Available directive types are DefaultSrc, ScriptSrc, StyleSrc, ImgSrc and FontSrc.
63+ // Each of the available directives may be defined only once in the list.
64+ // The value 'self' is automatically included in all fetch directives by the OpenShift web
65+ // console's backend.
66+ // For more information about the CSP directives, see:
67+ // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy
68+ //
69+ // The OpenShift web console server aggregates the CSP directives and values across
70+ // its own default values and all enabled ConsolePlugin CRs, merging them into a single
71+ // policy string that is sent to the browser via `Content-Security-Policy` HTTP response header.
72+ //
73+ // Example:
74+ // ConsolePlugin A directives:
75+ // script-src: https://script1.com/, https://script2.com/
76+ // font-src: https://font1.com/
77+ //
78+ // ConsolePlugin B directives:
79+ // script-src: https://script2.com/, https://script3.com/
80+ // font-src: https://font2.com/
81+ // img-src: https://img1.com/
82+ //
83+ // Unified set of CSP directives, passed to the OpenShift web console server:
84+ // script-src: https://script1.com/, https://script2.com/, https://script3.com/
85+ // font-src: https://font1.com/, https://font2.com/
86+ // img-src: https://img1.com/
87+ //
88+ // OpenShift web console server CSP response header:
89+ // Content-Security-Policy: default-src 'self'; base-uri 'self'; script-src 'self' https://script1.com/ https://script2.com/ https://script3.com/; font-src 'self' https://font1.com/ https://font2.com/; img-src 'self' https://img1.com/; style-src 'self'; frame-src 'none'; object-src 'none'
90+ //
91+ // +openshift:enable:FeatureGate=ConsolePluginContentSecurityPolicy
92+ // +kubebuilder:validation:MaxItems=5
93+ // +kubebuilder:validation:XValidation:rule="self.map(x, x.values.map(y, y.size()).sum()).sum() < 8192",message="the total combined size of values of all directives must not exceed 8192 (8kb)"
94+ // +listType=map
95+ // +listMapKey=directive
96+ // +optional
97+ ContentSecurityPolicy []ConsolePluginCSP `json:"contentSecurityPolicy"`
98+ }
99+
100+ // DirectiveType is an enumeration of OpenShift web console supported CSP directives.
101+ // LoadType is an enumeration of i18n loading types.
102+ // +kubebuilder:validation:Enum:="DefaultSrc";"ScriptSrc";"StyleSrc";"ImgSrc";"FontSrc"
103+ // +enum
104+ type DirectiveType string
105+
106+ const (
107+ // DefaultSrc directive serves as a fallback for the other CSP fetch directives.
108+ // For more information about the DefaultSrc directive, see:
109+ // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/default-src
110+ DefaultSrc DirectiveType = "DefaultSrc"
111+ // ScriptSrc directive specifies valid sources for JavaScript.
112+ // For more information about the ScriptSrc directive, see:
113+ // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src
114+ ScriptSrc DirectiveType = "ScriptSrc"
115+ // StyleSrc directive specifies valid sources for stylesheets.
116+ // For more information about the StyleSrc directive, see:
117+ // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/style-src
118+ StyleSrc DirectiveType = "StyleSrc"
119+ // ImgSrc directive specifies a valid sources of images and favicons.
120+ // For more information about the ImgSrc directive, see:
121+ // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/img-src
122+ ImgSrc DirectiveType = "ImgSrc"
123+ // FontSrc directive specifies valid sources for fonts loaded using @font-face.
124+ // For more information about the FontSrcdirective, see:
125+ // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/font-src
126+ FontSrc DirectiveType = "FontSrc"
127+ )
128+
129+ // CSPDirectiveValue is single value for a Content-Security-Policy directive.
130+ // Each directive value must have a maximum length of 1024 characters and must not contain
131+ // whitespace, commas (,), semicolons (;) or single quotes ('). The value '*' is not permitted.
132+ // +kubebuilder:validation:MinLength=1
133+ // +kubebuilder:validation:MaxLength=1024
134+ // +kubebuilder:validation:XValidation:rule="!self.contains(\"'\")",message="CSP directive value cannot contain a quote"
135+ // +kubebuilder:validation:XValidation:rule="!self.matches('\\\\s')",message="CSP directive value cannot contain a whitespace"
136+ // +kubebuilder:validation:XValidation:rule="!self.contains(',')",message="CSP directive value cannot contain a comma"
137+ // +kubebuilder:validation:XValidation:rule="!self.contains(';')",message="CSP directive value cannot contain a semi-colon"
138+ // +kubebuilder:validation:XValidation:rule="self != '*'",message="CSP directive value cannot be a wildcard"
139+ type CSPDirectiveValue string
140+
141+ // ConsolePluginCSP holds configuration for a specific CSP directive
142+ type ConsolePluginCSP struct {
143+ // directive specifies which Content-Security-Policy directive to configure.
144+ // Available directive types are DefaultSrc, ScriptSrc, StyleSrc, ImgSrc and FontSrc.
145+ // DefaultSrc directive serves as a fallback for the other CSP fetch directives.
146+ // For more information about the DefaultSrc directive, see:
147+ // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/default-src
148+ // ScriptSrc directive specifies valid sources for JavaScript.
149+ // For more information about the ScriptSrc directive, see:
150+ // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src
151+ // StyleSrc directive specifies valid sources for stylesheets.
152+ // For more information about the StyleSrc directive, see:
153+ // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/style-src
154+ // ImgSrc directive specifies a valid sources of images and favicons.
155+ // For more information about the ImgSrc directive, see:
156+ // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/img-src
157+ // FontSrc directive specifies valid sources for fonts loaded using @font-face.
158+ // For more information about the FontSrc directive, see:
159+ // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/font-src
160+ // +kubebuilder:validation:Required
161+ Directive DirectiveType `json:"directive"`
162+ // values defines an array of values to append to the console defaults for this directive.
163+ // Each ConsolePlugin may define their own directives with their values. These will be set
164+ // by the OpenShift web console's backend, as part of its Content-Security-Policy header.
165+ // The array can contain at most 16 values. Each directive value must have a maximum length
166+ // of 1024 characters and must not contain whitespace, commas (,), semicolons (;) or single
167+ // quotes ('). The value '*' is not permitted.
168+ // Each value in the array must be unique.
169+ //
170+ // +kubebuilder:validation:Required
171+ // +kubebuilder:validation:MinItems=1
172+ // +kubebuilder:validation:MaxItems=16
173+ // +kubebuilder:validation:XValidation:rule="self.all(x, self.exists_one(y, x == y))",message="each CSP directive value must be unique"
174+ // +listType=atomic
175+ Values []CSPDirectiveValue `json:"values"`
51176}
52177
53178// LoadType is an enumeration of i18n loading types
0 commit comments