wttech / CQ-Unix-Toolkit

CQ Unix Toolkit
46 stars 23 forks source link

URL encoding bugfix #32

Closed jwadolowski closed 9 years ago

jwadolowski commented 9 years ago

Hey,

I encountered an odd issue while I was testing my Chef provider for OSGi configurations. I tried to amend com.day.cq.rewriter.linkchecker.impl.LinkCheckerImpl, but it turned out it was not possible.

This is its default configuration:

PID          com.day.cq.rewriter.linkchecker.impl.LinkCheckerImpl
TITLE        Day CQ Link Checker Service
DESCRIPTION  Performs asynchronous checking of external links.
BUNDLE       Day Communique 5 Rewriter (com.day.cq.cq-rewriter), Version 5.6.4

ID                                   VALUE                                                   NAME                          VALUES  DESCRIPTION
scheduler.period                     5                                                       Scheduler Period
scheduler.concurrent                 false                                                   scheduler.concurrent.name
service.bad_link_tolerance_interval  48                                                      Bad Link Tolerance Interval
service.check_override_patterns      ["^system/"]                                            Link Check Override Patterns
service.cache_broken_internal_links  false                                                   Cache Broken Internal Links
service.special_link_prefix          ["#","${","<!--","data:","javascript:","mailto:","z:"]  Special Link Prefixes
service.special_link_patterns                                                                Special Link Patterns

My provider eventually executes cqcfg command like this:

/opt/scripts/CQ-Unix-Toolkit/cqcfg \
    -i http://localhost:4502 \
    -u admin \
    -p admin \
    -s "scheduler.period" -v "5" \
    -s "scheduler.concurrent" -v "false" \
    -s "service.bad_link_tolerance_interval" -v "24" \
    -s "service.check_override_patterns" -v "^system/" \
    -s "service.cache_broken_internal_links" -v "false" \
    -s "service.special_link_prefix" -v "#" \
    -s "service.special_link_prefix" -v "${" \
    -s "service.special_link_prefix" -v "<!--" \
    -s "service.special_link_prefix" -v "data:" \
    -s "service.special_link_prefix" -v "javascript:" \
    -s "service.special_link_prefix" -v "mailto:" \
    -s "service.special_link_prefix" -v "z:" \
    -s "service.special_link_patterns" -v "" \
    com.day.cq.rewriter.linkchecker.impl.LinkCheckerImpl

Unfortunately I ended with non-zero exit code with the following message:

bash: !--": event not found

The root cause was quite straightforward - all parameters should be enclosed with single quotes and not the double ones (reference). I've updated my code, but cqcfg was still not working:

CURL ERROR 3: URL malformed. The syntax was not correct.

I came up with the idea to encode all the values in my ruby code, but it turned out it was a dead end. I eventually discovered that CQ UNIX Toolkit can do that automatically thanks to _urlencode function from cqapi, so encoding of already encoded values is a bad idea.

All in all I did a comparison of HTTP requests generated by the browser and cqcfg command and the difference was as follows:

Browser cqcfg
service.check_override_patterns=%5Esystem%2F service.check_override_patterns=^system%2F
service.special_link_prefix=%23 service.special_link_prefix=#
service.special_link_prefix=%24%7B service.special_link_prefix=%24{
service.special_link_prefix=%3C!-- service.special_link_prefix=<%21--

As you can see some characters were not encoded, so I decided to dig into the cqcfg and cqapi code.

In this PR:

kitarek commented 9 years ago

Thanks Jakub especially for that PR. I had no chance to test such complicated OSGi config changes.

Arek

jwadolowski commented 9 years ago

Thanks!