Skip to content

Commit d902c89

Browse files
authored
Add CSP-compliant variant of undocumented validateButton script feature (#20345)
* Add CSP-compliant variant of undocumented validateButton script feature * Prettier / eslint * Use Function.prototype.apply() as hopefulle HTMLUnit compatible alternative --------- Co-authored-by: Daniel Beck <daniel-beck@users.noreply.github.com>
1 parent 390870c commit d902c89

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

core/src/main/java/jenkins/security/ApiTokenProperty.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ public HttpResponse doChangeToken(@AncestorInPath User u, StaplerResponse rsp) t
560560
p.changeApiToken();
561561
}
562562

563-
rsp.setHeader("script", "document.getElementById('apiToken').value='" + p.getApiToken() + "'");
563+
rsp.setHeader("X-Jenkins-ValidateButton-Callback", "{\"callback\":\"changeTokenCallback\",\"arguments\":[\"" + p.getApiTokenInsecure() + "\"]}");
564564
return HttpResponses.html(p.hasPermissionToSeeToken()
565565
? Messages.ApiTokenProperty_ChangeToken_Success()
566566
: Messages.ApiTokenProperty_ChangeToken_SuccessHidden());

core/src/main/resources/jenkins/security/ApiTokenProperty/resources.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ Behaviour.specify(
3232
},
3333
);
3434

35+
// eslint-disable-next-line no-unused-vars
36+
function changeTokenCallback(newValue) {
37+
document.getElementById("apiToken").value = newValue;
38+
}
39+
3540
function revokeToken(anchorRevoke) {
3641
const tokenRow = anchorRevoke.closest(".token-card");
3742
const confirmMessage = anchorRevoke.getAttribute("data-confirm");

war/src/main/webapp/scripts/hudson-behavior.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2636,6 +2636,16 @@ function validateButton(checkUrl, paramList, button) {
26362636
target.innerHTML = `<div class="validation-error-area" />`;
26372637
updateValidationArea(target.children[0], responseText);
26382638
layoutUpdateCallback.call();
2639+
let json = rsp.headers.get("X-Jenkins-ValidateButton-Callback");
2640+
if (json != null) {
2641+
let callInfo = JSON.parse(json);
2642+
let callback = callInfo["callback"];
2643+
let args = callInfo["arguments"];
2644+
if (window[callback] && typeof window[callback] === "function") {
2645+
window[callback].apply(window, args);
2646+
}
2647+
}
2648+
26392649
var s = rsp.headers.get("script");
26402650
try {
26412651
geval(s);

0 commit comments

Comments
 (0)