dfe41e138200e4f55fc69919016b4cd4d5a571fe
[ikiwiki.git] / doc / todo / want_to_avoid_ikiwiki_using_http_or_https_in_urls_to_allow_serving_both.mdwn
1 ## current status
2
3 99% of the way there, but:
4
5 * CGI pages, with the exception of edit pages, set `<base>` to
6   `$config{url}`
7
8   I had to revert using `baseurl(undef)` for that, because it needs
9   to be a full url.
10
11   Ideally, baseurl would return an absolute url derived from the url
12   being used to access the cgi, but that needs access to the CGI object,
13   which it does not currently have. Similarly, `misctemplate`
14   does not have access to the CGI object, so it cannot use it to
15   generate a better baseurl. Not sure yet what to do; may have to thread
16   a cgi parameter through all the calls to misctemplate. --[[Joey]]
17
18 * Using `do=goto` to go to a comment or recentchanges item
19   will redirect to the `$config{url}`-based url, since the
20   permalinks are made to be absolute urls now.
21
22   Fixing this would seem to involve making meta force permalinks
23   to absolute urls when fulling out templates, while allowing them
24   to be left as partial urls internally, for use by goto. --[[Joey]]
25
26 ## old attempt
27
28 It looks like all links in websites are absolute paths, this has some limitations:
29
30 * If connecting to website via https://... all links will take you back to http://
31 * Makes it harder to mirror website via HTML version, as all links have to be updated.
32
33 It would be good if relative paths could be used instead, so the transport method isn't changed unless specifically requested.
34
35 -- Brian May
36
37 > Er, which absolute links are you talking about? If you view the source
38 > to this page, you'll find links such as "../favicon.ico", "../style.css",
39 > "../../", and "../". The only absolute links are to CGIs and the w3c DTD.
40 > --[[Joey]]
41
42 >> The problem is within the CGI script. The links within the HTML page are all
43 >> absolute, including links to the css file. Having a http links within a HTML
44 >> page retrieved using https upset most browsers (I think). Also if I push cancel
45 >> on the edit page in https, I end up at at http page. -- Brian May
46
47 >>> Ikiwiki does not hardcode http links anywhere. If you don't want
48 >>> it to use such links, change your configuration to use https
49 >>> consistently. --[[Joey]]
50
51 Errr... That is not a solution, that is a work around. ikiwiki does not hard
52 code the absolute paths, but absolute paths are hard coded in the configuration
53 file. If you want to serve your website so that the majority of users can see
54 it as http, including in rss feeds (this allows proxy caches to cache the
55 contents and has reduced load requirements), but editing is done via https for
56 increased security, it is not possible. I have some ideas how this can be
57 implemented (as ikiwiki has the absolute path to the CGI script and the
58 absolute path to the destination, it should be possible to generate a relative
59 path from one to the other), although some minor issues still need to be
60 resolved. -- Brian May
61
62 I noticed the links to the images on <http://ikiwiki.info/recentchanges/> are
63 also absolute, that is <http://ikiwiki.info/wikiicons/diff.png>; this seems
64 surprising, as the change.tmpl file uses &lt;TMPL_VAR BASEURL&gt; which seems
65 to do the right thing in page.tmpl, but not for change.tmpl. Where is BASEURL
66 set? -- Brian May
67
68 > The use of an absolute baseurl in change.tmpl is a special case. --[[Joey]]
69
70 So I'm facing this same issue. I have a wiki which needs to be accessed on
71 three different URLs(!) and the hard coding of the URL from the setup file is
72 becoming a problem for me. Is there anything I can do here? --[[Perry]]
73
74 > I remain puzzled by the problem that Brian is discussing. I don't see
75 > why you can't just set the cgiurl and url to a https url, and serve
76 > the site using both http and https.
77
78 > Just for example, <https://kitenet.net/> is an ikiwiki, and it is accessible
79 > via https or http, and if you use https, links will remain on https (except
80 > for links using the cgi, which I could fix by changing the cgiurl to https).
81
82 > I think it's possible ikiwiki used to have some
83 > absolute urls that have been fixed since Brian filed the bug. --[[Joey]]  
84
85 [[wishlist]]
86
87 ----
88
89 [[!toggle id="smcv-https" text="Some discussion of a rejected implementation, smcv/https."]]
90 [[!toggleable id="smcv-https" text="""
91
92 [[!template id=gitbranch branch=smcv/https author="[[smcv]]"]]
93
94 For a while I've been using a configuration where each wiki has a HTTP and
95 a HTTPS mirror, and updating one automatically updates the other, but
96 that seems unnecessarily complicated. My `https` branch adds `https_url`
97 and `https_cgiurl` config options which can be used to provide a HTTPS
98 variant of an existing site; the CGI script automatically detects whether
99 it was accessed over HTTPS and switches to the other one.
100
101 This required some refactoring, which might be worth merging even if
102 you don't like my approach:
103
104 * change `IkiWiki::cgiurl` to return the equivalent of `$config{cgiurl}` if
105   called with no parameters, and change all plugins to indirect through it
106   (then I only need to change that one function for the HTTPS hack)
107
108 * `IkiWiki::baseurl` already has similar behaviour, so change nearly all
109   references to the `$config{url}` to call `baseurl` (a couple of references
110   specifically wanted the top-level public URL for Google or Blogspam rather
111   than a URL for the user's browser, so I left those alone)
112
113 --[[smcv]]
114
115 > The justification for your patch seems to be wanting to use a different
116 > domain, like secure.foo.com, for https? Can you really not just configure
117 > both url and cgiurl to use `https://secure.foo.com/...` and rely on
118 > relative links to keep users of `http://insecure.foo.com/` on http until
119 > they need to use the cgi? 
120
121 >> My problem with that is that uses of the CGI aren't all equal (and that
122 >> the CA model is broken). You could put CGI uses in two classes:
123 >>
124 >> - websetup and other "serious" things (for the sites I'm running, which
125 >>   aren't very wiki-like, editing pages is also in this class).
126 >>   I'd like to be able to let privileged users log in over
127 >>   https with httpauth (or possibly even a client certificate), and I don't
128 >>   mind teaching these few people how to do the necessary contortions to
129 >>   enable something like CACert.
130 >>
131 >> - Random users making limited use of the CGI: do=goto, do=404, and
132 >>   commenting with an OpenID. I don't think it's realistic to expect
133 >>   users to jump through all the CA hoops to get CACert installed for that,
134 >>   which leaves their browsers being actively obstructive, unless I either
135 >>   pay the CA tax (per subdomain) to get "real" certificates, or use plain
136 >>   http.
137 >>
138 >> On a more wiki-like wiki, the second group would include normal page edits.
139 >>
140 >>> I see your use case. It still seems to me that for the more common
141 >>> case where CA tax has been paid (getting a cert that is valid for
142 >>> multiple subdomains should be doable?), having anything going through the
143 >>> cgiurl upgrade to https would be ok. In that case, http is just an
144 >>> optimisation for low-value, high-aggregate-bandwidth type uses, so a
145 >>> little extra https on the side is not a big deal. --[[Joey]]
146 >>
147 >> Perhaps I'm doing this backwards, and instead of having the master
148 >> `url`/`cgiurl` be the HTTP version and providing tweakables to override
149 >> these with HTTPS, I should be overriding particular uses to plain HTTP...
150 >>
151 >> --[[smcv]]
152 >>> 
153 >>> Maybe, or I wonder if you could just use RewriteEngine for such selective
154 >>> up/downgrading. Match on `do=(edit|create|prefs)`. --[[Joey]]
155
156 > I'm unconvinced.
157
158 > `Ikiwiki::baseurl()."foo"` just seems to be asking for trouble,
159 > ie being accidentially written as `IkiWiki::baseurl("foo")`,
160 > which will fail when foo is not a page, but some file.
161
162 >> That's a good point. --s
163
164 > I see multiple places (inline.pm, meta.pm, poll.pm, recentchanges.pm)
165 > where it will now put the https url into a static page if the build
166 > happens to be done by the cgi accessed via https, but not otherwise.
167 > I would rather not have to audit for such problems going forward.
168
169 >> Yes, that's a problem with this approach (either way round). Perhaps
170 >> making it easier to run two mostly-synched copies like I was previously
171 >> doing is the only solution... --s
172
173 """]]
174
175 ----
176
177 [[!template id=gitbranch branch=smcv/ready/localurl author="[[smcv]]"]]
178 [[!tag patch]]
179
180 OK, here's an alternative approach, closer in spirit to what was initially
181 requested. I included a regression test for `urlto`, `baseurl` and `cgiurl`,
182 now that they have slightly more complex behaviour.
183
184 The idea is that in the common case, the CGI and the pages will reside on the
185 same server, so they can use "semi-absolute" URLs (`/ikiwiki.cgi`, `/style.css`,
186 `/bugs/done`) to refer to each other. Most redirects, form actions, links etc.
187 can safely use this form rather than the fully-absolute URL.
188
189 The initial version of the branch had config options `local_url` and
190 `local_cgiurl`, but they're now automatically computed by checking
191 whether `url` and `cgiurl` are on the same server with the the same URL
192 scheme. In theory you could use things like `//static.example.com/wiki/`
193 and `//dynamic.example.com/ikiwiki.cgi` to preserve choice of http/https
194 while switching server, but I don't know how consistently browsers
195 support that.
196
197 "local" here is short for "locally valid", because these URLs are neither
198 fully relative nor fully absolute, and there doesn't seem to be a good name
199 for them...
200
201 I've tested this on a demo website with the CGI enabled, and it seemed to
202 work nicely (there might be bugs in some plugins, I didn't try all of them).
203 The branch at [[todo/use secure cookies for SSL logins]] goes well with
204 this one.
205
206 The `$config{url}` and `$config{cgiurl}` are both HTTP, but if I enable
207 `httpauth`, set `cgiauthurl` to a HTTPS version of the same site and log
208 in via that, links all end up in the HTTPS version.
209
210 New API added by this branch:
211
212 * `urlto(x, y, 'local')` uses `$local_url` instead of `$config{url}`
213
214   > Yikes. I see why you wanted to keep it to 3 parameters (4 is too many,
215   > and po overrides it), but I dislike overloading the third parameter
216   > like that.
217   > 
218   > There are fairly few calls to `urlto($foo, $bar)`, so why not
219   > make that always return the semi-local url form, and leave the third
220   > parameter for the cases that need a true fully-qualified url.
221   > The new form for local urls will typically be only a little bit longer,
222   > except in the unusual case where the cgiurl is elsewhere. --[[Joey]]
223
224   >> So, have urlto(x, y) use `$local_url`? There are few calls, but IMO
225   >> they're for the most important things - wikilinks, img, map and
226   >> other ordinary hyperlinks. Using `$local_url` would be fine for
227   >> webserver-based use, but it does stop you browsing your wiki's
228   >> HTML over `file:///` (unless you set that as the base URL, but
229   >> then you can't move it around), and stops you moving simple
230   >> outputs (like the docwiki!) around.
231   >>
232   >> I personally think breaking the docwiki is enough to block that.
233   >>
234   >>> Well, the docwiki doesn't have an url configured at all, so I assumed
235   >>> it would need to fall back to current behavior in that case. I had
236   >>> not thought about browsing wiki's html files though, good point.
237   >>
238   >> How about this?
239   >>
240   >> * `urlto($link, $page)` with `$page` defined: relative
241   >> * `urlto($link, undef)`: local, starts with `/`
242   >> * `urlto($link)`: also local, as a side-effect
243   >> * `urlto($link, $anything, 1)` (but idiomatically, `$anything` is
244   >>   normally undef): absolute, starts with `http[s]://`
245   >>
246   >> --[[smcv]]
247   >> 
248   >>> That makes a great deal of sense, bravo for actually removing
249   >>> parameters in the common case while maintaining backwards
250   >>> compatability! --[[Joey]]
251   >>>
252   >>>> Done in my `localurl` branch; not tested in a whole-wiki way
253   >>>> yet, but I did add a regression test. I've used
254   >>>> `urlto(x, undef)` rather than `urlto(x)` so far, but I could
255   >>>> go back through the codebase using the short form if you'd
256   >>>> prefer. --[[smcv]]
257   >>> 
258   >>> It does highlight that it would be better to have a
259   >>> `absolute_urlto($link)` (or maybe `absolute(urlto($link))` )
260   >>> rather than the 3 parameter form. --[[Joey]]
261   >>>
262   >>> Possibly. I haven't added this.
263
264 * `IkiWiki::baseurl` has a new second argument which works like the
265   third argument of `urlto`
266
267   > I assume you have no objection to this --[[smcv]]
268
269   >> It's so little used that I don't really care if it's a bit ugly.
270   >> (But I assume changes to `urlto` will follow through here anyway.)
271   >> --[[Joey]] 
272
273   >>> I had to use it a bit more, as a replacement for `$config{url}`
274   >>> when doing things like referencing stylesheets or redirecting to
275   >>> the top of the wiki.
276   >>>
277   >>> I ended up redoing this without the extra parameter. Previously,
278   >>> `baseurl(undef)` was the absolute URL; now, `baseurl(undef)` is
279   >>> the local path. I know you objected to me using `baseurl()` in
280   >>> an earlier branch, because `baseurl().$x` looks confusingly
281   >>> similar to `baseurl($x)` but has totally different semantics;
282   >>> I've generally written it `baseurl(undef)` now, to be more
283   >>> explicit. --[[smcv]]
284
285 * `IkiWiki::cgiurl` uses `$local_cgiurl` if passed `local_cgiurl => 1`
286
287   > Now changed to always use the `$local_cgiurl`. --[[smcv]]
288
289 * `IkiWiki::cgiurl` omits the trailing `?` if given no named parameters
290   except `cgiurl` and/or `local_cgiurl`
291
292   > I assume you have no objection to this --[[smcv]]
293   > 
294   >> Nod, although I don't know of a use case. --[[Joey]]
295
296   >>> The use-case is that I can replace `$config{cgiurl}` with
297   >>> `IkiWiki::cgiurl()` for things like the action attribute of
298   >>> forms. --[[smcv]]
299
300 Fixed bugs:
301
302 * I don't think anything except `openid` calls `cgiurl` without also
303   passing in `local_cgiurl => 1`, so perhaps that should be the default;
304   `openid` uses the `cgiurl` named parameter anyway, so there doesn't even
305   necessarily need to be a way to force absolute URLs? Any other module
306   that really needs an absolute URL could use
307   `cgiurl(cgiurl => $config{cgiurl}, ...)`,
308   although that does look a bit strange
309
310   > I agree that makes sense. --[[Joey]]
311
312   >> I'm not completely sure whether you're agreeing with "perhaps do this"
313   >> or "that looks too strange", so please disambiguate:
314   >> would you accept a patch that makes `cgiurl` default to a local
315   >> (starts-with-`/`) result? If you would, that'd reduce the diff. --[[smcv]]
316
317   >>> Yes, I absolutely think it should default to local. (Note that
318   >>> if `absolute()` were implemented as suggested above, it could also
319   >>> be used with cgiurl if necessary.) --[[Joey]]
320
321   >>>> Done (minus `absolute()`). --[[smcv]]
322
323 Potential future things:
324
325 * It occurs to me that `IkiWiki::cgiurl` could probably benefit from being
326   exported? Perhaps also `IkiWiki::baseurl`?
327
328   > Possibly, see [[firm_up_plugin_interface]]. --[[Joey]]
329
330   >> Not really part of this branch, though, so wontfix (unless you ask me
331   >> to do so). --[[smcv]]
332
333 * Or, to reduce use of the unexported `baseurl` function, it might make
334   sense to give `urlto` a special case that references the root of the wiki,
335   with a trailing slash ready to append stuff: perhaps `urlto('/')`,
336   with usage like this?
337
338         do_something(baseurl => urlto('/', undef, local)`);
339         do_something_else(urlto('/').'style.css');
340         IkiWiki::redirect(urlto('/', undef, 1));
341
342   > AFACIS, `baseurl` is only called in 3 places so I don't think that's
343   > needed. --[[Joey]] 
344
345   >> OK, wontfix. For what it's worth, my branch has 6 uses in IkiWiki
346   >> core code (IkiWiki, CGI, Render and the pseudo-core part of editpage)
347   >> and 5 in plugins, since I used it for things like redirection back
348   >> to the top of the wiki --[[smcv]]
349
350 merged|done --[[Joey]] (But reopened, see above.)
351
352 ----
353
354 Update: I had to revert part of 296e5cb2fd3690e998b3824d54d317933c595873,
355 since it broke openid logins. The openid object requires a complete,
356 not a relative cgiurl. I'm not sure if my changing that back to using
357 `$config{cgiurl}` will force users back to eg, the non-https version of a
358 site when logging in via openid.
359
360 > Ok, changed it to use `CGI->url` to get the current absolute cgi url. --[[Joey]]