Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
95b926a9
Commit
95b926a9
authored
Jun 28, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improved Json::encode() security.
parent
20666567
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
6 additions
and
5 deletions
+6
-5
Json.php
framework/yii/helpers/base/Json.php
+6
-5
No files found.
framework/yii/helpers/base/Json.php
View file @
95b926a9
...
@@ -33,7 +33,7 @@ class Json
...
@@ -33,7 +33,7 @@ class Json
public
static
function
encode
(
$value
,
$options
=
0
)
public
static
function
encode
(
$value
,
$options
=
0
)
{
{
$expressions
=
array
();
$expressions
=
array
();
$value
=
static
::
processData
(
$value
,
$expressions
);
$value
=
static
::
processData
(
$value
,
$expressions
,
uniqid
()
);
$json
=
json_encode
(
$value
,
$options
);
$json
=
json_encode
(
$value
,
$options
);
return
empty
(
$expressions
)
?
$json
:
strtr
(
$json
,
$expressions
);
return
empty
(
$expressions
)
?
$json
:
strtr
(
$json
,
$expressions
);
}
}
...
@@ -75,20 +75,21 @@ class Json
...
@@ -75,20 +75,21 @@ class Json
* Pre-processes the data before sending it to `json_encode()`.
* Pre-processes the data before sending it to `json_encode()`.
* @param mixed $data the data to be processed
* @param mixed $data the data to be processed
* @param array $expressions collection of JavaScript expressions
* @param array $expressions collection of JavaScript expressions
* @param string $expPrefix a prefix internally used to handle JS expressions
* @return mixed the processed data
* @return mixed the processed data
*/
*/
protected
static
function
processData
(
$data
,
&
$expressions
)
protected
static
function
processData
(
$data
,
&
$expressions
,
$expPrefix
)
{
{
if
(
is_array
(
$data
))
{
if
(
is_array
(
$data
))
{
foreach
(
$data
as
$key
=>
$value
)
{
foreach
(
$data
as
$key
=>
$value
)
{
if
(
is_array
(
$value
)
||
is_object
(
$value
))
{
if
(
is_array
(
$value
)
||
is_object
(
$value
))
{
$data
[
$key
]
=
static
::
processData
(
$value
,
$expressions
);
$data
[
$key
]
=
static
::
processData
(
$value
,
$expressions
,
$expPrefix
);
}
}
}
}
return
$data
;
return
$data
;
}
elseif
(
is_object
(
$data
))
{
}
elseif
(
is_object
(
$data
))
{
if
(
$data
instanceof
JsExpression
)
{
if
(
$data
instanceof
JsExpression
)
{
$token
=
'!{['
.
count
(
$expressions
)
.
']}!'
;
$token
=
"!{[
$expPrefix
="
.
count
(
$expressions
)
.
']}!'
;
$expressions
[
'"'
.
$token
.
'"'
]
=
$data
->
expression
;
$expressions
[
'"'
.
$token
.
'"'
]
=
$data
->
expression
;
return
$token
;
return
$token
;
}
else
{
}
else
{
...
@@ -96,7 +97,7 @@ class Json
...
@@ -96,7 +97,7 @@ class Json
$result
=
array
();
$result
=
array
();
foreach
(
$data
as
$key
=>
$value
)
{
foreach
(
$data
as
$key
=>
$value
)
{
if
(
is_array
(
$value
)
||
is_object
(
$value
))
{
if
(
is_array
(
$value
)
||
is_object
(
$value
))
{
$result
[
$key
]
=
static
::
processData
(
$value
,
$expressions
);
$result
[
$key
]
=
static
::
processData
(
$value
,
$expressions
,
$expPrefix
);
}
else
{
}
else
{
$result
[
$key
]
=
$value
;
$result
[
$key
]
=
$value
;
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment