Skip to content

write multi dim array to valid ini #16

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 5 commits into from
Apr 6, 2021
Merged

Conversation

typomedia
Copy link
Contributor

unit test included ;)

@tsteur tsteur added this to the Current sprint milestone Mar 28, 2021
if (is_int($key)) {
$ini .= $option . '[] = ' . $this->encodeValue($currentValue) . "\n";
} else {
$ini .= $option . '[' . $key . '] = ' . $this->encodeValue($currentValue) . "\n";
Copy link
Member

Choose a reason for hiding this comment

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

I haven't properly reviewed but wondering if $key would need to be encoded as well maybe? Just thinking randomly if $key was to be supplied by user input then not sure if you could break something or cause various security issues eg by defining a key like "] = 'xyz';\n foo =".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll check it up and write a test :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

encodeValue() is not needed for the $key. It's just for booleans and quoted strings as value.

Copy link
Member

Choose a reason for hiding this comment

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

I think what @tsteur means is, can you write a test where the key is defined as "] = 'xyz';\n foo =" and make sure it doesn't inject something into the INI file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@diosmosis, I'll write this test today.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The $key will be sanitized now. But you can still break the INI by injecting malicious crap "] = 'xyz';\n foo =" to the INI section or as value :/

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for writing the test @typomedia! I left one other comment, after that it will be good to merge. If you'd also like to make sure this doesn't happen for sections or values, we'd certainly appreciate it! Otherwise we'll take a look ourselves after merging this PR.

*/
private function encodeKey($key)
{
$key = preg_replace('/[^A-Za-z0-9\-]/', '', $key);
Copy link
Member

Choose a reason for hiding this comment

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

I'd maybe allow underscores here as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@diosmosis
Copy link
Member

Looks good @typomedia! Thanks again for the contribution and thanks for adding tests!

@diosmosis diosmosis merged commit 5226a64 into matomo-org:master Apr 6, 2021
@innocraft-automation innocraft-automation removed this from the Current sprint milestone Jan 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants