Skip to content

Commit e599a72

Browse files
committed
[Yaml] deprecated non-escaped \ in double-quoted strings when parsing
1 parent 6907498 commit e599a72

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

src/Symfony/Component/Yaml/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
2.8.0
5+
-----
6+
7+
* Deprecated non-escaped \ in double-quoted strings when parsing Yaml
8+
("Foo\Var" is not valid whereas "Foo\\Var" is)
9+
410
2.1.0
511
-----
612

src/Symfony/Component/Yaml/Tests/Fixtures/escapedCharacters.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,11 @@ php: |
145145
array(
146146
'double' => "some value\n \"some quoted string\" and 'some single quotes one'"
147147
)
148+
---
149+
test: Backslashes
150+
yaml: |
151+
{ single: 'foo\Var', no-quotes: foo\Var, double: "foo\\Var" }
152+
php: |
153+
array(
154+
'single' => 'foo\Var', 'no-quotes' => 'foo\Var', 'double' => 'foo\Var'
155+
)

src/Symfony/Component/Yaml/Tests/InlineTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,23 @@ public function testHashStringsResemblingExponentialNumericsShouldNotBeChangedTo
7272
$this->assertSame($value, Inline::parse(Inline::dump($value)));
7373
}
7474

75+
/**
76+
* @group legacy
77+
* throws \Symfony\Component\Yaml\Exception\ParseException in 3.0
78+
*/
79+
public function testParseScalarWithNonEscapedBlackslashShouldThrowException()
80+
{
81+
$this->assertSame('Foo\Var', Inline::parse('"Foo\Var"'));
82+
}
83+
84+
/**
85+
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
86+
*/
87+
public function testParseScalarWithNonEscapedBlackslashAtTheEndShouldThrowException()
88+
{
89+
Inline::parse('"Foo\\"');
90+
}
91+
7592
/**
7693
* @expectedException \Symfony\Component\Yaml\Exception\ParseException
7794
*/

src/Symfony/Component/Yaml/Unescaper.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Unescaper
3232
/**
3333
* Regex fragment that matches an escaped character in a double quoted string.
3434
*/
35-
const REGEX_ESCAPED_CHARACTER = "\\\\([0abt\tnvfre \\\"\\/\\\\N_LP]|x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4}|U[0-9a-fA-F]{8})";
35+
const REGEX_ESCAPED_CHARACTER = "\\\\(x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4}|U[0-9a-fA-F]{8}|.)";
3636

3737
/**
3838
* Unescapes a single quoted string.
@@ -70,10 +70,13 @@ public function unescapeDoubleQuotedString($value)
7070
* @param string $value An escaped character
7171
*
7272
* @return string The unescaped character
73+
*
74+
* @internal This method is public to be usable as callback. It should not
75+
* be used in user code. Should be changed in 3.0.
7376
*/
7477
public function unescapeCharacter($value)
7578
{
76-
switch ($value{1}) {
79+
switch ($value[1]) {
7780
case '0':
7881
return "\x0";
7982
case 'a':
@@ -120,6 +123,10 @@ public function unescapeCharacter($value)
120123
return self::utf8chr(hexdec(substr($value, 2, 4)));
121124
case 'U':
122125
return self::utf8chr(hexdec(substr($value, 2, 8)));
126+
default:
127+
@trigger_error('Not escaping a backslash in a double-quoted string is deprecated since Symfony 2.8 and will throw a ParseException in 3.0.', E_USER_DEPRECATED);
128+
129+
return $value;
123130
}
124131
}
125132

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy