-
Notifications
You must be signed in to change notification settings - Fork 530
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[JENKINS-74011] Extract JavaScript block in PageStatePreloadDecorator/header.jelly
#2588
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 tasks
basil
commented
Nov 19, 2024
Comment on lines
-4
to
+10
// construct the state object parent path inside window.$blueocean. | ||
var stateRoot = window.$blueocean = (window.$blueocean || {}); | ||
(function () { | ||
function setState(statePropertyPath, state) { | ||
var pathTokens = statePropertyPath.split('.'); | ||
var contextObj = stateRoot; | ||
|
||
// Basically an Array shift | ||
function nextToken() { | ||
var nextToken = pathTokens[0]; | ||
pathTokens = pathTokens.slice(1); | ||
return nextToken; | ||
} | ||
|
||
var pathToken = nextToken(); | ||
|
||
// Construct up to, but not including, the last point in the graph. | ||
while (pathTokens.length !== 0) { | ||
if (!contextObj[pathToken]) { | ||
contextObj[pathToken] = {}; | ||
} | ||
contextObj = contextObj[pathToken]; | ||
pathToken = nextToken(); | ||
} | ||
// And set the state on the last object on the graph. | ||
contextObj[pathToken] = state; | ||
} | ||
|
||
<j:forEach var="preloader" items="${it.pageStatePreloaders}"> | ||
// State Preloader: ${preloader.class.name} | ||
<j:set var="stateJson" value="${preloader.stateJson}"/> | ||
<j:if test="${stateJson != null}"> | ||
setState('${preloader.statePropertyPath}', ${stateJson}); | ||
</j:if> | ||
</j:forEach> | ||
})(); | ||
//]]> | ||
<script id="blueocean-page-state-preload-decorator-data" type="application/json"> | ||
{<j:forEach var="preloader" items="${it.pageStatePreloaders}" varStatus="st"> | ||
<!-- State Preloader: ${preloader.class.name} --> | ||
<j:set var="stateJson" value="${preloader.stateJson}"/> | ||
<j:if test="${stateJson != null}"> | ||
"${preloader.statePropertyPath}": ${stateJson}<j:if test="${!st.last}">,</j:if> | ||
</j:if> | ||
</j:forEach>} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Diff:
diff --git b/blueocean-web/src/main/resources/io/jenkins/blueocean/PageStatePreloadDecorator/preloader.js a/blueocean-web/src/main/resources/io/jenkins/blueocean/PageStatePreloadDecorator/preloader.js
index a9658b610..4757a1e9d 100644
--- b/blueocean-web/src/main/resources/io/jenkins/blueocean/PageStatePreloadDecorator/preloader.js
+++ a/blueocean-web/src/main/resources/io/jenkins/blueocean/PageStatePreloadDecorator/preloader.js
@@ -25,11 +25,8 @@ var stateRoot = window.$blueocean = (window.$blueocean || {});
// And set the state on the last object on the graph.
contextObj[pathToken] = state;
}
- <j:forEach var="preloader" items="${it.pageStatePreloaders}">
- // State Preloader: ${preloader.class.name}
- <j:set var="stateJson" value="${preloader.stateJson}"/>
- <j:if test="${stateJson != null}">
- setState('${preloader.statePropertyPath}', ${stateJson});
- </j:if>
- </j:forEach>
+ const data = JSON.parse(document.getElementById('blueocean-page-state-preload-decorator-data').textContent);
+ for (const [key, value] of Object.entries(data)) {
+ setState(key, value);
+ }
})();
7 tasks
yaroslavafenkin
approved these changes
Nov 20, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context
See JENKINS-74011.
Problem
blueocean-plugin/blueocean-web/src/main/resources/io/jenkins/blueocean/PageStatePreloadDecorator/header.jelly
Lines 3 to 41 in be4d0c9
Solution
https://www.jenkins.io/doc/developer/security/csp/#inline-javascript-blocks
Implementation
Follow the general strategy used in JUnit and Theme Manager plugins. Since this data is complex JSON, do not use data attributes in HTML, but rather put the JSON in a
<script>
of typeapplication/json
. Then parse that JSON (without usingeval
!) to get the data into memory in a CSP-compliant fashion.Testing done
Tested together with #2587 and
Note that the above contains
unsafe-eval
to avoid exposing 74883.Confirmed that after the header was added but before this PR and #2587, Blue Ocean blew up with a CSP violation. Confirmed that after the header was added and after this PR and #2587, Blue Ocean loaded correctly.
Stepped through the
setState
function calls in the browser's debugger after this PR to ensure they were being called with the same arguments as before from the parsed JSON.Submitter checklist
Reviewer checklist