Repository navigation
fix: write the calendar labels as valid JSON - #433
ChristianMurphy wants to merge 1 commit into
Conversation
The two calendar views pass their labels to <calendar-portlet> as JSON in a script element, each written with spring:message and javaScriptEscape. That escaping writes an apostrophe as \', which JSON doesn't allow. The French bundle has "Voir plus d'évènements", so for a French user JSON.parse failed, the element logged "invalid i18n JSON" and showed its English labels. A new tag, upcal:jsonMessage, resolves the message as spring:message does and escapes it for a JSON string. It also escapes < and >, as the old escaping did, so a label can't end the script element. Both views use it for every label. The tag needs the JSP API to compile, which the parent pom already manages as a provided dependency.
Naenyn
left a comment
There was a problem hiding this comment.
Thanks, this looks good to me. Approving.
The failed macOS build isn't from your change. It's the same thing as on NewsReaderPortlet#450: this branch still has the duplicate maven-antrun-plugin declaration, and CI ran a bare mvn, so the macOS runner's Maven 3.10 failed while the 3.9 runners passed. master has since fixed the duplicate (#435) and pins Maven 3.10.0 through the wrapper (#429). The branch merges cleanly with current master. On that merge, ./mvnw -B package passes on Java 11 with all 33 tests, and license:check passes. Merging master into the branch should turn CI green.
The fix.
- Extending
MessageTagand overriding onlywriteMessagekeeps message resolution identical, andJsonStringEncoderplus the\u003C/\u003Ereplacement covers both JSON validity and ending the<script>element. - Exposing only
codein the TLD means nobody can turnjavaScriptEscapeback on for this tag. tomcat-jsp-apistays out of the WAR (provided scope via the parent). The built WAR hasJsonMessageTag.classandWEB-INF/tag/calendar.tld.
One small thing to be aware of, not a change request: MessageTag applies HTML escaping before writeMessage, so if defaultHtmlEscape were ever enabled for the portlet, labels would get HTML entities inside the JSON. Nothing enables it today.
I didn't run it in a portal myself; the above is from building and testing locally on Java 11.
Checklist
Description of change
Problem. Since 2.7.4, a user whose language is French gets the Calendar with English labels, and the browser console shows
calendar-portlet: invalid i18n JSON SyntaxError: Bad escaped character in JSON.calendarNarrowView.jspandcalendarWideView.jsppass the labels to<calendar-portlet>as JSON in a<script type="application/json">, and write each label with<spring:message javaScriptEscape='true'/>. Spring's JavaScript escaping writes an apostrophe as\', and JSON has no such escape.messages_fr.propertieshasview.more.events=Voir plus d'évènements, soJSON.parsefails in the narrow view and the element falls back to its English labels. The wide view has the same pattern. It works today only because none of its French labels has an apostrophe.Fix. A new tag,
<upcal:jsonMessage code="..."/>, takes the place ofspring:messagein both label blocks.JsonMessageTagextends Spring'sMessageTag, so the message is found the same way as before, and the tag writes it escaped for a JSON string with Jackson'sJsonStringEncoder. It also escapes<and>, as the old escaping did, so a label can't end the script element. The TLD isWEB-INF/tag/calendar.tld, next touportal.tld, andinclude.jspdeclares the prefix. The tag needs the JSP API to compile. The parent pom already managestomcat-jsp-apias a provided dependency. The portlet's pom now names it, without a version. No other JSP usesjavaScriptEscape.Test.
JsonMessageTagTestrendersview.more.eventsfrom the portlet's own French bundle through the tag and puts the result in a JSON string. Jackson parses that string, and the test expectsVoir plus d'évènements. A second test escapes a text with an apostrophe, quotes, a backslash,</script>, a tab, a line break and a vertical tab, and checks that it reads back unchanged and has no<or>left. When I ran the first test withspring:messageandjavaScriptEscapein place of the new tag, it failed withJsonParseException: Unrecognized character escape ''' (code 39).Verified.
mvn -B package, the command CI runs, passes on this branch on JDK 11 with 33 tests. I also checked the change in a portal, in GitHub Actions: uPortal-start'smasteras it is (uPortal 5.17.9, HSQLDB, Tomcat 8.5, JDK 11). A browser signed in as student, chose French in the language portlet, and opened the Calendar on the Welcome tab (the narrow view) and maximized (the wide view). With the released 2.7.4, the narrow view showed Day, Week, Month and "View more events", and the console had the error above. The wide view was French. Then the job deployed this branch's war in the same portal. Both views showed Jour, Semaine and Mois, the narrow view's link read "Voir plus d'évènements", and the element didn't log an error. So Tomcat finds the new TLD and the tag works in both JSPs. The workflow and scripts for this run are on my uPortal-start fork's branchtest/verify-portlet-fixes.Not verified. The portal check used Chromium and Tomcat 8.5 only. French is the only translation the portlet has, so no other language was checked.
Where this comes from. I'm working on a fork that upgrades uPortal's dependencies (Spring 5.3 and Hibernate 5.6 so far, heading for Spring 7, Hibernate 7 and Jakarta EE 11). That fork's CI runs uPortal-start's Playwright suite against HSQLDB, PostgreSQL, MySQL, MariaDB, SQL Server and Oracle. A test there that expects an empty console failed on the Welcome page while its user's language was French. The bug isn't caused by the upgrade: the portlet in that portal was the released 2.7.4, and the two JSPs are the same in
CalendarPortlet-2.7.4andmaster, so I prepared the fix here on its own. Until a release has it, my uPortal-start fork overlays the two JSPs with copies that repair the JSON.