nginx / njs

A subset of JavaScript language to use in nginx
http://nginx.org/en/docs/njs/
BSD 2-Clause "Simplified" License
1.14k stars 148 forks source link

r.requestText.replaceAll replaces only first occurence of string #710

Closed amelentjev closed 4 months ago

amelentjev commented 5 months ago

I'm trying to replace all occurences of some string in form input, and discovered, that replaceAll replaces only first occurence example : ngx.log(ngx.ERR,r.requestText); ngx.log(ngx.ERR,r.requestText.replaceAll('potato','cucumber'));

form_submitted=1&csrfKey=32 f3ca73c9253cc8fb3ff3f581c2e845&MAX_FILE_SIZE=20971520&plupload=a2baaac96305d9341ee786a4ee47c950&comment_value=%3Cp%3E%0D%0A%09qqrqu%0D%0A%3C%2Fp%3E%0D%0A%0D%0A%3Cp%3E%0D%0A%09potato%0D%0A%3C%2Fp%3E%0D%0A%0D%0A%3Cp%3E%0D%0A%09potato%0D%0A%3C%2Fp%3E%0D%0A%0D%0A%3Cp%3E%0D%0A%09some+text+here%0D%0A%3C%2Fp%3E%0D%0A%0D%0A%3Cp%3E%0D%0A%09potato%0D%0A%3C%2Fp%3E%0D%0A%0D%0A%3Cp%3E%0D%0A%09%26nbsp%3B%0D%0A%3C%2Fp%3E%0D%0A&comment_value_upload=f42085d16eb480aed41c002c60e0e8c8&comment_edit_reason=&comment_log_edit=0

form_submitted=1&csrfKey=32 f3ca73c9253cc8fb3ff3f581c2e845&MAX_FILE_SIZE=20971520&plupload=a2baaac96305d9341ee786a4ee47c950&comment_value=%3Cp%3E%0D%0A%09qqrqu%0D%0A%3C%2Fp%3E%0D%0A%0D%0A%3Cp%3E%0D%0A%09cucumber%0D%0A%3C%2Fp%3E%0D%0A%0D%0A%3Cp%3E%0D%0A%09potato%0D%0A%3C%2Fp%3E%0D%0A%0D%0A%3Cp%3E%0D%0A%09some+text+here%0D%0A%3C%2Fp%3E%0D%0A%0D%0A%3Cp%3E%0D%0A%09potato%0D%0A%3C%2Fp%3E%0D%0A%0D%0A%3Cp%3E%0D%0A%09%26nbsp%3B%0D%0A%3C%2Fp%3E%0D%0A&comment_value_upload=f42085d16eb480aed41c002c60e0e8c8&comment_edit_reason=&comment_log_edit=0

if I replace pattern string with regex, all is working correctly. Surprised, but I can't reproduce such behaviour in njs-cli

xeioex commented 4 months ago

Hi @amelentjev,

Thank you for the report. I can confirm the issue.

Feel free to test the following patch: https://gist.github.com/xeioex/5d73f0591004c7eec6055177a2b52b87.

It should also fix similar issues and aligns nginx modules with njs CLI.