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

Replace corrupts serialized data if the search and replace are of different length #36

Open
sjinks opened this issue Dec 10, 2022 · 4 comments

Comments

@sjinks
Copy link
Member

sjinks commented Dec 10, 2022

To reproduce:

php -r 'echo serialize("aaaaabbbbbbbbbbaaaaa"), PHP_EOL;' > test.txt
cat test.txt | ./go-search-replace bbbbbbbbbb ccccc
cat test.txt | ./go-search-replace bbbbbbbbbb ccccccccccccccc

Expected result:

s:15:"aaaaacccccaaaaa";
s:25:"aaaaacccccccccccccccaaaaa";

Actual result:

s:20:"aaaaacccccaaaaa";
s:20:"aaaaacccccccccccccccaaaaa";
@trepmal
Copy link

trepmal commented Jan 27, 2023

This is because go-search-replace expects the quote marks to be escaped. Whether that's a bug or not I don't know.

$ php -r 'echo addslashes( serialize("aaaaabbbbbbbbbbaaaaa") ), PHP_EOL;' > test.txt
$ cat test.txt                                                                    
s:20:\"aaaaabbbbbbbbbbaaaaa\";

$ cat test.txt | go-search-replace bbbbbbbbbb ccccc                               
s:15:\"aaaaacccccaaaaa\";

$ cat test.txt | go-search-replace bbbbbbbbbb ccccccccccccccc                     
s:25:\"aaaaacccccccccccccccaaaaa\";

@sjinks
Copy link
Member Author

sjinks commented Jan 27, 2023

But then it does not work on statements like

INSERT INTO `wp_options` (option_name, option_value) VALUES ('test', 's:15:"aaaaacccccaaaaa";');

@sjinks
Copy link
Member Author

sjinks commented Jan 27, 2023

Double quotes inside single quotes do not have to be escaped.

@joshbetz
Copy link
Contributor

I think WP always escapes the quotes, but if you're seeing otherwise it should be fine to change the regex

https://github.com/Automattic/go-search-replace/blob/master/search-replace.go#L17-L18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants