Skip to content

Yaml source manipulator - fix access control #487

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

Merged
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
30 changes: 27 additions & 3 deletions src/Util/YamlSourceManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ private function updateData(array $newData)
}

// 3b) value DID change
$this->log('updating value');
$this->log(sprintf('updating value to {%s}', is_array($newVal) ? '<array>' : $newVal));
$this->changeValueInYaml($newVal);
}

Expand Down Expand Up @@ -461,12 +461,30 @@ private function changeValueInYaml($value)
// empty space between key & value
$newYamlValue = ' '.$newYamlValue;
}

$newPosition = $this->currentPosition + \strlen($newYamlValue);
$isNextContentComment = $this->isPreviousLineComment($newPosition);
if ($isNextContentComment) {
$newPosition++;
}
Comment on lines +466 to +469

Choose a reason for hiding this comment

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

Shouldn't it be a while in case of multi-line comment?

Copy link
Member Author

Choose a reason for hiding this comment

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

The $newPosition++ here is just because of the code below that adds 1 new character (\n) if the next line is a comment. I'm moving this pointer/position forward 1 for that 1 \n. It's confusing code :). The test case actually does have 2 lines of comment.


$newContents = substr($this->contents, 0, $this->currentPosition)
.$newYamlValue
/*
* If the next line is a comment, this means we probably had
* a structure that looks like this:
* access_control:
* # - { path: ^/admin, roles: ROLE_ADMIN }
*
* In this odd case, we need to know that the next line
* is a comment, so we can add an extra line break.
* Otherwise, the result is something like:
* access_control:
* - { path: /foo, roles: ROLE_USER } # - { path: ^/admin, roles: ROLE_ADMIN }
*/
.($isNextContentComment ? "\n" : '')
.substr($this->contents, $endValuePosition);

$newPosition = $this->currentPosition + \strlen($newYamlValue);

$newData = $this->currentData;
$newData = $this->setValueAtCurrentPath($value, $newData);

Expand Down Expand Up @@ -765,6 +783,12 @@ private function findEndPositionOfValue($value, $offset = null)

$offset = null === $offset ? $this->currentPosition : $offset;

// a value like "foo:" can simply end a file
// this means the value is null
if ($offset === strlen($this->contents)) {
return $offset;
}

preg_match(sprintf('#%s#', $pattern), $this->contents, $matches, PREG_OFFSET_CAPTURE, $offset);
if (empty($matches)) {
throw new YamlManipulationFailedException(sprintf('Cannot find the original value "%s"', $value));
Expand Down
7 changes: 7 additions & 0 deletions tests/Util/yaml_fixtures/last_value_is_blank.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
security:
access_control:
===
$data['security']['access_control'] = 'foo';
===
security:
access_control: foo
13 changes: 13 additions & 0 deletions tests/Util/yaml_fixtures/simple_sequence_add_array.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
security:
access_control:
===
$data['security']['access_control'] = [];
$data['security']['access_control'][] = ['path' => '^/login$', 'roles' => 'IS_AUTHENTICATED_ANONYMOUSLY'];
===
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
security:
access_control:
-
path: ^/login$
roles: IS_AUTHENTICATED_ANONYMOUSLY
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
security:
access_control:
# - { path: ^/admin, roles: ROLE_ADMIN }
# - { path: ^/profile, roles: ROLE_USER }
===
$data['security']['access_control'] = [];
$data['security']['access_control'][] = ['path' => '^/login$', 'roles' => 'IS_AUTHENTICATED_ANONYMOUSLY'];
===
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
security:
access_control:
-
path: ^/login$
roles: IS_AUTHENTICATED_ANONYMOUSLY
# - { path: ^/admin, roles: ROLE_ADMIN }
# - { path: ^/profile, roles: ROLE_USER }