29
29
import javax .servlet .http .HttpServletResponse ;
30
30
import java .security .SecureRandom ;
31
31
import java .util .Base64 ;
32
- import java .util .Objects ;
33
32
34
33
import static java .lang .String .format ;
35
34
@@ -86,16 +85,18 @@ public void addCspHeaders(HttpServletRequest request, HttpServletResponse respon
86
85
private void addCspHeadersWithSession (HttpServletRequest request , HttpServletResponse response ) {
87
86
if (isSessionActive (request )) {
88
87
LOG .trace ("Session is active, applying CSP settings" );
89
- request .getSession ().setAttribute (NONCE_KEY , generateNonceValue ());
90
- response .setHeader (cspHeader , createPolicyFormat (request ));
88
+ String nonceValue = generateNonceValue ();
89
+ request .getSession ().setAttribute (NONCE_KEY , nonceValue );
90
+ response .setHeader (cspHeader , createPolicyFormat (nonceValue ));
91
91
} else {
92
92
LOG .debug ("Session is not active, ignoring CSP settings" );
93
93
}
94
94
}
95
95
96
96
private void addCspHeadersWithRequest (HttpServletRequest request , HttpServletResponse response ) {
97
- request .setAttribute (NONCE_KEY , generateNonceValue ());
98
- response .setHeader (cspHeader , createPolicyFormat (request ));
97
+ String nonceValue = generateNonceValue ();
98
+ request .setAttribute (NONCE_KEY , nonceValue );
99
+ response .setHeader (cspHeader , createPolicyFormat (nonceValue ));
99
100
}
100
101
101
102
private boolean isSessionActive (HttpServletRequest request ) {
@@ -106,34 +107,45 @@ private String generateNonceValue() {
106
107
return Base64 .getUrlEncoder ().encodeToString (getRandomBytes ());
107
108
}
108
109
109
- protected String createPolicyFormat (HttpServletRequest request ) {
110
- StringBuilder policyFormatBuilder = new StringBuilder ()
110
+ protected String createPolicyFormat (String nonceValue ) {
111
+ StringBuilder builder = new StringBuilder ()
111
112
.append (OBJECT_SRC )
112
113
.append (format (" '%s'; " , NONE ))
113
114
.append (SCRIPT_SRC )
114
- .append (" 'nonce-%s' " ) // nonce placeholder
115
+ .append (format ( " 'nonce-%s' " , nonceValue ))
115
116
.append (format ("'%s' " , STRICT_DYNAMIC ))
116
117
.append (format ("%s %s; " , HTTP , HTTPS ))
117
118
.append (BASE_URI )
118
119
.append (format (" '%s'; " , NONE ));
119
120
120
121
if (reportUri != null ) {
121
- policyFormatBuilder
122
+ builder
122
123
.append (REPORT_URI )
123
- .append (format (" %s; " , reportUri ));
124
+ .append (format (" %s;" , reportUri ));
124
125
if (reportTo != null ) {
125
- policyFormatBuilder
126
+ builder
126
127
.append (REPORT_TO )
127
- .append (format (" %s; " , reportTo ));
128
+ .append (format (" %s;" , reportTo ));
128
129
}
129
130
}
130
131
131
- return format (policyFormatBuilder .toString (), getNonceString (request ));
132
+ return builder .toString ();
133
+ }
134
+
135
+ /**
136
+ * @deprecated since 6.8.0, for removal
137
+ */
138
+ @ Deprecated
139
+ protected String createPolicyFormat (HttpServletRequest request ) {
140
+ throw new UnsupportedOperationException ("Unsupported implementation, use #createPolicyFormat(String) instead!" );
132
141
}
133
142
143
+ /**
144
+ * @deprecated since 6.8.0, for removal
145
+ */
146
+ @ Deprecated
134
147
protected String getNonceString (HttpServletRequest request ) {
135
- Object nonce = request .getSession ().getAttribute (NONCE_KEY );
136
- return Objects .toString (nonce );
148
+ throw new UnsupportedOperationException ("Unsupported implementation, don't use!" );
137
149
}
138
150
139
151
private byte [] getRandomBytes () {
0 commit comments