Skip to content
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

Fix parsing of unicode escape sequences in YAML #3604

Merged
merged 3 commits into from
Oct 5, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,18 @@ void multiBytesUnicode() {
)
);
}

@Test
void unicodeEscapes() {
rewriteRun(
json(
"""
{
"nul": "\\u0000",
"reverse-solidus": "\\u005c",
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package org.openrewrite.yaml;

import lombok.Getter;
import org.openrewrite.internal.lang.NonNull;
import org.yaml.snakeyaml.events.Event;

import java.io.IOException;
Expand Down Expand Up @@ -115,7 +114,7 @@ public String readStringFromBuffer(int start, int end) {
}

@Override
public int read(@NonNull char[] cbuf, int off, int len) throws IOException {
public int read(char[] cbuf, int off, int len) throws IOException {
int read = delegate.read(cbuf, off, len);
if (read > 0) {
buffer.ensureCapacity(buffer.size() + read);
Expand Down
51 changes: 35 additions & 16 deletions rewrite-yaml/src/main/java/org/openrewrite/yaml/YamlParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

import lombok.Getter;
import org.intellij.lang.annotations.Language;
import org.openrewrite.*;
import org.openrewrite.ExecutionContext;
import org.openrewrite.FileAttributes;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.SourceFile;
import org.openrewrite.internal.EncodingDetectingInputStream;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.internal.lang.Nullable;
Expand All @@ -36,7 +39,6 @@
import org.yaml.snakeyaml.scanner.ScannerImpl;

import java.io.IOException;
import java.io.StringReader;
import java.io.UncheckedIOException;
import java.nio.file.Path;
import java.util.*;
Expand Down Expand Up @@ -186,15 +188,40 @@ private Yaml.Documents parseFromInput(Path sourceFile, EncodingDetectingInputStr
newLine = "";

ScalarEvent scalar = (ScalarEvent) event;
String scalarValue = scalar.getValue();
if (variableByUuid.containsKey(scalarValue)) {
scalarValue = variableByUuid.get(scalarValue);
}

Yaml.Anchor anchor = null;
int valueStart;
if (scalar.getAnchor() != null) {
anchor = buildYamlAnchor(reader, lastEnd, fmt, scalar.getAnchor(), event.getEndMark().getIndex(), true);
anchors.put(scalar.getAnchor(), anchor);
valueStart = lastEnd + fmt.length() + scalar.getAnchor().length() + 1 + anchor.getPostfix().length();
} else {
valueStart = lastEnd + fmt.length();
}

String scalarValue;
switch (scalar.getScalarStyle()) {
case DOUBLE_QUOTED:
case SINGLE_QUOTED:
scalarValue = reader.readStringFromBuffer(valueStart + 1, event.getEndMark().getIndex() - 2);
break;
case PLAIN:
scalarValue = reader.readStringFromBuffer(valueStart, event.getEndMark().getIndex() - 1);
break;
case LITERAL:
scalarValue = reader.readStringFromBuffer(valueStart + 1, event.getEndMark().getIndex() - 1);
if (scalarValue.endsWith("\n")) {
newLine = "\n";
scalarValue = scalarValue.substring(0, scalarValue.length() - 1);
}
break;
case FOLDED:
default:
scalarValue = reader.readStringFromBuffer(valueStart + 1, event.getEndMark().getIndex() - 1);
break;
}
if (variableByUuid.containsKey(scalarValue)) {
scalarValue = variableByUuid.get(scalarValue);
}

Yaml.Scalar.Style style;
Expand All @@ -207,22 +234,13 @@ private Yaml.Documents parseFromInput(Path sourceFile, EncodingDetectingInputStr
break;
case LITERAL:
style = Yaml.Scalar.Style.LITERAL;
scalarValue = reader.readStringFromBuffer(event.getStartMark().getIndex() + 1, event.getEndMark().getIndex() - 1);
if (scalarValue.endsWith("\n")) {
newLine = "\n";
scalarValue = scalarValue.substring(0, scalarValue.length() - 1);
}
break;
case FOLDED:
style = Yaml.Scalar.Style.FOLDED;
scalarValue = reader.readStringFromBuffer(event.getStartMark().getIndex() + 1, event.getEndMark().getIndex() - 1);
break;
case PLAIN:
default:
style = Yaml.Scalar.Style.PLAIN;
if (!scalarValue.startsWith("@") && event.getStartMark().getIndex() >= reader.getBufferIndex()) {
scalarValue = reader.readStringFromBuffer(event.getStartMark().getIndex(), event.getEndMark().getIndex() - 1);
}
break;
}
BlockBuilder builder = blockStack.isEmpty() ? null : blockStack.peek();
Expand Down Expand Up @@ -347,7 +365,8 @@ private Yaml.Anchor buildYamlAnchor(FormatPreservingReader reader, int lastEnd,
lastEnd + eventPrefix.length() + anchorLength, eventEndIndex);

StringBuilder postFix = new StringBuilder();
for (char c : whitespaceAndScalar.toCharArray()) {
for (int i = 0; i < whitespaceAndScalar.length(); i++) {
char c = whitespaceAndScalar.charAt(i);
if (c != ' ' && c != '\t') {
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,23 +118,7 @@ public Yaml visitScalar(Yaml.Scalar scalar, PrintOutputCapture<P> p) {
switch (scalar.getStyle()) {
case DOUBLE_QUOTED:
p.append('"')
.append(scalar.getValue()
.replace("\\", "\\\\")
.replace("\0", "\\0")
.replace("\u0007", "\\a")
.replace("\b", "\\b")
.replace("\t", "\\t")
.replace("\n", "\\n")
.replace("\u000B", "\\v")
.replace("\f", "\\f")
.replace("\r", "\\r")
.replace("\u001B", "\\e")
.replace("\"", "\\\"")
.replace("\u0085", "\\N")
.replace("\u00A0", "\\_")
.replace("\u2028", "\\L")
.replace("\u2029", "\\P")
)
.append(scalar.getValue())
.append('"');
break;
case SINGLE_QUOTED:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,17 @@ void newlinesCombinedWithUnniCode() {
)
);
}

@Test
void unicodeEscapes() {
rewriteRun(
yaml(
"""
root:
"nul": "\\u0000"
"reverse-solidus": "\\u005c"
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -400,34 +400,30 @@ void mappingAnchor() {
" '\\n' ",
" '\n' ",
" \n ",
" \"\\.\" ",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: I've removed the escape sequences which are illegal according to the SnakeYAML parser.

" \"\\0\" ",
" \"\\0\" ",
" \"\\a\" ",
" \"\\a\" ",
" \"\\b\" ",
" \"\b\" ",
" \"\\t\" ",
" \"\t\" ",
" \"\\n\" ",
" \"\n\" ",
" \"\\v\" ",
" \"\\f\" ",
" \"\f\" ",
" \"\\r\" ",
" \"\r\" ",
" \"\\e\" ",
" \"\\\\\" ",
" \"\\\" ",
" \"\\\"\" ",
" \"\"\" ",
" \"\\N\" ",
" \"\\_\" ",
" \"\\L\" ",
" \"\\P\" ",
})
void escapeSequences() {
void escapeSequences(String str) {
rewriteRun(
yaml("escaped-value: $string"));
yaml("escaped-value: " + str)
);
}
}