Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions grails-doc/src/en/guide/upgrading/upgrading80x.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -5109,3 +5109,7 @@ only shows in a schema that Hibernate creates: an application that manages its s
keeps the columns it has, and `hbm2ddl.auto: update` does not change an existing column.
The one difference from a `String` property is a `maxSize` shorter than the longest constant name: `maxSize` does not validate an
enum, so the column is made as long as the longest name, and no value of the enum fails at the database.

==== An Escaped GSP Expression in Long Text Is Written Without Its Backslash

`\${` in the text of a page is written as `${`. In Grails 8.0.0 an escaped expression was written with its backslash, as `\${`, when it came 63,998 or 63,999 characters, or that many and a multiple of 64,000 more, after the start of the page or after the last expression, scriptlet or GSP tag in front of it: the text is handed on in pieces of 64,000 characters, and the piece was cut inside the escape. The text is no longer cut there, so such an expression is written as `${`, as it is anywhere else. No setting keeps the old output, since it depended on where in the text the escape fell, and no page could rely on it. For a page to write `\${`, write `\\${` in it.
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,11 @@ else if (isStartOfGExpression(c, c1) && !isEscapedGExpression()) {
}

if (tokenLength > maxHtmlLength) {
return found(HTML, 0);
int cut = htmlCutPosition();
if (cut != -1) {
end1 = cut + 1;
return found(HTML, 0);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A page of accented letters and plain letters ends in an escaped expression. It should compile and write the expression as text. It fails, because the piece before the escape is still one byte too long. Cut that piece shorter.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is not from the escape. A piece is cut at 64,000 characters, and a compiled page stores a piece in at most 65,535 bytes, so any text with more than 1,535 two-byte characters in one piece fails to compile, with or without \${.

I compiled pages of 63,996 to 64,002 characters with 1,534 to 1,538 é on this branch and on the commit before it. The page with 1,537 é in 63,999 characters and \${a} at the end fails on both: the piece is 65,536 bytes here and one byte more before. The same text with plain text in place of the escape fails the same way, and 7.0.x has the same cut and the same writeUTF call. No page that compiled before fails now; this PR only makes the piece in front of an escape one or two characters shorter.

Cutting by encoded length is a fix of its own, and it belongs on 7.0.x, so I am leaving it out of this PR.

}
}

break;
Expand Down Expand Up @@ -284,7 +288,30 @@ private boolean isStartOfGExpression(char c, char c1) {
* escaped this way.
*/
private boolean isEscapedGExpression() {
return end1 > 1 && text.charAt(end1 - 2) == '\\';
return isEscapedGExpressionAt(end1 - 2);
}

private boolean isEscapedGExpressionAt(int backslash) {
return backslash >= 0 && text.startsWith("\\${", backslash);
}

/**
* Long text is handed on in pieces of at most {@link #getMaxHtmlLength()} characters, and the backslash
* of an escaped expression is only taken out of a piece that has the whole of <code>\${</code>. A cut
* that would fall inside one is moved to before its backslash, which makes the piece shorter. A piece
* that starts with the escaped expression has no such place, so the text is not cut there and the piece
* takes all three characters: it is longer than a limit below 3.
*
* @return the position the text is cut at, or -1 when the piece starts with the escaped expression
Comment thread
jdaugherty marked this conversation as resolved.
Resolved
*/
private int htmlCutPosition() {
int current = end1 - 1;
for (int backslash = current - 2; backslash < current; backslash++) {
if (isEscapedGExpressionAt(backslash)) {
return backslash > begin1 ? backslash : -1;
}
}
return current;
}

private boolean skipComment(char c3, char c4) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.grails.gsp

import spock.lang.Specification
import spock.lang.TempDir
import spock.lang.Unroll

import org.grails.gsp.compiler.GroovyPageCompiler
import org.grails.gsp.io.GroovyPageCompiledScriptSource

/**
* Long text is handed from the scanner to the parser in pieces of 64,000 characters. An escaped
* expression at the end of a piece is still one.
*/
class GspLongTextEscapeSpec extends Specification {

@TempDir
File tempDir

GroovyPagesTemplateEngine engine

void setup() {
engine = new GroovyPagesTemplateEngine()
engine.afterPropertiesSet()
}

@Unroll
void "an escaped expression after #length characters of text is written without its backslash"() {
given:
String text = 'x' * length

expect:
render(text + '\\${a} and ${1 + 1}') == text + '${a} and 2'

where:
length << (63_990..64_005) + (127_990..128_005)
}

@Unroll
void "a backslash in front of an escaped expression after #length characters of text is written, and the expression as text"() {
given:
String text = 'x' * length

expect:
render(text + '\\\\${a} and ${1 + 1}') == text + '\\${a} and 2'

where:
length << (63_995..64_001)
}

@Unroll
void "#lookalike after #length characters of text is not an escaped expression and is written as it is"() {
given:
String text = 'x' * length

expect:
render(text + lookalike + ' and ${1 + 1}') == text + lookalike + ' and 2'

where:
[length, lookalike] << [63_997..64_000, ['\\$ {a}', '\\{a}', '$\\{a}']].combinations()
}

@Unroll
void "a compiled page with an escaped expression after #length characters, #twoByte of them two bytes long, is written without its backslash"() {
given: 'text whose first 64,000 characters take up to the 65,535 bytes a compiled page can store as one piece'
String text = ('é' * twoByte) + ('x' * (length - twoByte))

expect:
renderCompiled(text + '\\${a} and ${1 + 1}') == text + '${a} and 2'

where:
length | twoByte
63_998 | 0
63_999 | 0
63_998 | 1_535
63_999 | 1_535
64_000 | 1_535
}

private String render(String source) {
StringWriter out = new StringWriter()
engine.createTemplate(source, "long${source.hashCode().abs()}").make([:]).writeTo(out)
out.toString()
}

private String renderCompiled(String source) {
File viewsDir = new File(tempDir, 'views')
File classesDir = new File(tempDir, 'classes')
viewsDir.mkdirs()
classesDir.mkdirs()
File page = new File(viewsDir, 'page.gsp')
page.setText(source, 'UTF-8')

GroovyPageCompiler compiler = new GroovyPageCompiler()
compiler.viewsDir = viewsDir
compiler.targetDir = classesDir
compiler.generatedGroovyPagesDirectory = new File(tempDir, 'generated')
compiler.srcFiles = [page]
Map compiled = compiler.compile()

new URLClassLoader([classesDir.toURI().toURL()] as URL[], getClass().classLoader).withCloseable { URLClassLoader loader ->
Class<?> pageClass = loader.loadClass(compiled['/page.gsp'] as String)
StringWriter out = new StringWriter()
engine.createTemplate(new GroovyPageCompiledScriptSource('/page.gsp', '/page.gsp', pageClass)).make([:]).writeTo(out)
out.toString()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,50 @@ public void testMaxHtmlLength() {
Assertions.assertEquals("K", scanner.getToken());
Assertions.assertEquals(GroovyPageScanner.EOF, scanner.nextToken());
}

@Test
public void testMaxHtmlLengthCutsBeforeAnEscapedExpression() {
// the cut falls on the $ of the first escape and on the { of the second
String gsp = "012345678\\${a}xyz\\${b}";
GroovyPageScanner scanner = new GroovyPageScanner(gsp);
scanner.setMaxHtmlLength(10);
Assertions.assertEquals(GroovyPageScanner.HTML, scanner.nextToken());
Assertions.assertEquals("012345678", scanner.getToken());
Assertions.assertEquals(GroovyPageScanner.HTML, scanner.nextToken());
Assertions.assertEquals("\\${a}xyz", scanner.getToken());
Assertions.assertEquals(GroovyPageScanner.HTML, scanner.nextToken());
Assertions.assertEquals("\\${b}", scanner.getToken());
Assertions.assertEquals(GroovyPageScanner.EOF, scanner.nextToken());
}

@Test
public void testMaxHtmlLengthKeepsAPieceThatStartsWithAnEscapedExpressionWhole() {
GroovyPageScanner scanner = new GroovyPageScanner("\\${a}");
scanner.setMaxHtmlLength(1);
Assertions.assertEquals(GroovyPageScanner.HTML, scanner.nextToken());
Assertions.assertEquals("\\${", scanner.getToken());
Assertions.assertEquals(GroovyPageScanner.HTML, scanner.nextToken());
Assertions.assertEquals("a", scanner.getToken());
Assertions.assertEquals(GroovyPageScanner.HTML, scanner.nextToken());
Assertions.assertEquals("}", scanner.getToken());
Assertions.assertEquals(GroovyPageScanner.EOF, scanner.nextToken());
}

@Test
public void testMaxHtmlLengthCutsWhereTextOnlyLooksLikeAnEscapedExpression() {
for (String lookalike : new String[] {"\\$ {a}", "\\{a}", "$\\{a}", "\\$"}) {
for (int digits = 7; digits <= 10; digits++) {
String gsp = "0123456789".substring(0, digits) + lookalike;
GroovyPageScanner scanner = new GroovyPageScanner(gsp);
scanner.setMaxHtmlLength(10);
Assertions.assertEquals(GroovyPageScanner.HTML, scanner.nextToken());
Assertions.assertEquals(gsp.substring(0, Math.min(10, gsp.length())), scanner.getToken(), gsp);
if (gsp.length() > 10) {
Assertions.assertEquals(GroovyPageScanner.HTML, scanner.nextToken());
Assertions.assertEquals(gsp.substring(10), scanner.getToken(), gsp);
}
Assertions.assertEquals(GroovyPageScanner.EOF, scanner.nextToken());
}
}
}
}
6 changes: 6 additions & 0 deletions grails-skills/upgrade/skills/grails-upgrade/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -1731,6 +1731,12 @@ With GORM for Hibernate 5, a where query, a detached criteria, a dynamic finder
- To keep the previous result of a query, remove the join type that it requests, or request `JoinType.LEFT`.
- Check queries that store a sort with `sort()` before passing a `fetch` argument to `list` or `find`. `Book.where { ... }.sort('author.name').list(fetch: [author: JoinType.LEFT])` now also returns books without an author. Request `JoinType.INNER` to keep leaving them out; nested paths follow the same rule.

## GSP: An Escaped Expression in Long Text

- **`\${` in the text of a page is written as `${` wherever it falls.** In Grails 8.0.0 an escaped expression was written with its backslash, as `\${`, when it came 63,998 or 63,999 characters, or that many and a multiple of 64,000 more, after the start of the page or after the last expression, scriptlet or GSP tag in front of it. It is now written as `${`, as anywhere else in a page.
- Search GSP pages with long runs of text without an expression, scriptlet or GSP tag, such as inline JavaScript, for `\${`, and check tests that compare the rendered output of such a page.
- No setting keeps the old output, since it depended on where in the text the escape fell. For a page to write `\${`, write `\\${` in it.

# After the Upgrade

## Verification Checklist
Expand Down
Loading