Skip to content

Commit 5c4fda0

Browse files
committed
Item11621: Improve redirecting for http/https sites
- Redirect all scripts in the {AuthScripts} list. They will need https for authentication. - Monkeypatch the TemplateLogin login manager to redirect to https whenver forceAuthenticaiton is called. The https session may already be authenticated.
1 parent b2451e0 commit 5c4fda0

File tree

4 files changed

+195
-107
lines changed

4 files changed

+195
-107
lines changed

data/System/HttpsRedirectPlugin.txt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
%META:TOPICINFO{author="ProjectContributor" date="1458439803" format="1.1" version="1.2"}%
1+
%META:TOPICINFO{author="ProjectContributor" date="1490669264" format="1.1" version="1.2"}%
22
%META:TOPICPARENT{name="Plugins"}%
33
---+!! !HttpsRedirectPlugin
44
<!--
@@ -15,6 +15,16 @@ The HTTPS redirect plug-in is designed to help you run a web site where guest us
1515
Simply activate the plug-in from =configure= to take advantage of its functionality.
1616
Once enabled it will force authenticated users to use HTTPS by redirecting them to HTTPS URL if needed. Guests will be redirected to HTTPS only when accessing the login screen.
1717

18+
Redirect happens on the following conditions:
19+
* Request for the login script.
20+
* Request for any script listed in ={AuthScripts}=
21+
* Any request that triggers the LoginManager::TemplateLogin::forceAuthentication() event.
22+
23+
Note tht as of this release, only the !TemplateLogin method is supported for
24+
forceAuthentication based redirects.
25+
26+
Be sure to set the https:// URL in ={PermittedRedirectHostUrls}=.
27+
1828
---++ Installation Instructions
1929

2030
%$INSTALL_INSTRUCTIONS%
@@ -24,11 +34,12 @@ Once enabled it will force authenticated users to use HTTPS by redirecting them
2434
Many thanks to the following sponsors for supporting this work:
2535
* http://slion.net
2636

27-
| Plugin Author(s): | Foswiki:Main.StephaneLenclud |
28-
| Copyright: | &copy; 2008-2009 St&eacute;phaneLenclud, 2011-2016 Foswiki Contributors |
37+
| Plugin Author(s): | Foswiki:Main.StephaneLenclud Foswiki:Main.GeorgeClark |
38+
| Copyright: | &copy; 2008-2009 St&eacute;phaneLenclud, 2011-2017 Foswiki Contributors |
2939
| License: | [[http://www.gnu.org/licenses/gpl.html][GPL (Gnu General Public License)]] |
3040
| Plugin Version: | %$VERSION% |
3141
| Change History: | <!-- versions below in reverse order -->&nbsp; |
42+
| 26 Mar 2017 | v1.3 Foswikitask:Item11621 - Redirect auth scripts and forceAuthentication events. |
3243
| 19 mar 2016 | v1.2 Foswikitask:Item14030 - Use new version strings, remove non-utf8 characters from topic. Other minor cleanup. |
3344
| 28 Mar 2011 | v1.1 Foswikitask:Item10551 - fixed a problem where foswiki scripts would produce no output if run from CLI |
3445
| 28 Apr 2009 | v1.0 First actually working version for Foswiki |
Lines changed: 102 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,29 @@
1-
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
2-
#
3-
# This program is free software; you can redistribute it and/or
4-
# modify it under the terms of the GNU General Public License
5-
# as published by the Free Software Foundation; either version 2
6-
# of the License, or (at your option) any later version.
7-
#
8-
# This program is distributed in the hope that it will be useful,
9-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11-
# GNU General Public License for more details, published at
12-
# http://www.gnu.org/copyleft/gpl.html
1+
# See bottom of file for notices
132

143
=pod
154
165
---+ package Foswiki::Plugins::HttpsRedirectPlugin
176
18-
To interact with Foswiki use ONLY the official API functions
19-
in the Foswiki::Func module. Do not reference any functions or
20-
variables elsewhere in Foswiki, as these are subject to change
21-
without prior warning, and your plugin may suddenly stop
22-
working.
23-
24-
For increased performance, all handlers except initPlugin are
25-
disabled below. *To enable a handler* remove the leading DISABLE_ from
26-
the function name. For efficiency and clarity, you should comment out or
27-
delete the whole of handlers you don't use before you release your
28-
plugin.
29-
30-
__NOTE:__ When developing a plugin it is important to remember that
31-
Foswiki is tolerant of plugins that do not compile. In this case,
32-
the failure will be silent but the plugin will not be available.
33-
See [[%SYSTEMWEB%.Plugins#FAILEDPLUGINS]] for error messages.
34-
35-
__NOTE:__ Defining deprecated handlers will cause the handlers to be
36-
listed in [[%SYSTEMWEB%.Plugins#FAILEDPLUGINS]]. See
37-
[[%SYSTEMWEB%.Plugins#Handlig_deprecated_functions]]
38-
for information on regarding deprecated handlers that are defined for
39-
compatibility with older Foswiki versions.
40-
41-
__NOTE:__ When writing handlers, keep in mind that these may be invoked
42-
on included topics. For example, if a plugin generates links to the current
43-
topic, these need to be generated before the afterCommonTagsHandler is run,
44-
as at that point in the rendering loop we have lost the information that we
45-
the text had been included from another topic.
7+
Intercept any http: requests that should be done over https
8+
and redirect the requests to an https: URL
9+
* Any request to login
10+
* Requests to any script in the ={AuthScripts}= list
11+
* Any request that triggers a forceAuthentication event.
4612
4713
=cut
4814

4915
package Foswiki::Plugins::HttpsRedirectPlugin;
5016

51-
# Always use strict to enforce variable scoping
5217
use strict;
18+
use warnings;
5319

54-
require Foswiki::Func; # The plugins API
55-
require Foswiki::Plugins; # For the API version
20+
use Foswiki::Func; # The plugins API
21+
use Foswiki::Plugins; # For the API version
5622

57-
our $VERSION = '1.2';
58-
our $RELEASE = '19 Mar 2016';
23+
our $VERSION = '1.3';
24+
our $RELEASE = '26 Mar 2017';
5925

60-
# Short description of this plugin
61-
# One line description, is shown in the %SYSTEMWEB%.TextFormattingRules topic:
62-
our $SHORTDESCRIPTION = 'Redirect authenticated users to HTTPS url.';
63-
64-
# You must set $NO_PREFS_IN_TOPIC to 0 if you want your plugin to use preferences
65-
# stored in the plugin topic.
26+
our $SHORTDESCRIPTION = 'Redirect authenticated users to HTTPS url.';
6627
our $NO_PREFS_IN_TOPIC = 1;
6728

6829
# Name of this Plugin, only used in this module
@@ -72,12 +33,37 @@ our $debug = 0;
7233

7334
=pod
7435
36+
---++ earlyInitPlugin
37+
38+
Determines if TemplateLogin is in use. If it is, enable a hook that
39+
monkey-patches TemplateLogin::forceAuthentication. This hook will force
40+
a redirect to https if any page is accessed that would require authentication.
41+
42+
=cut
43+
44+
sub earlyInitPlugin {
45+
return if !$Foswiki::cfg{Plugins}{HttpsRedirectPlugin}{Enabled};
46+
return undef
47+
unless (
48+
$Foswiki::cfg{LoginManager} eq 'Foswiki::LoginManager::TemplateLogin' );
49+
require Foswiki::Plugins::HttpsRedirectPlugin::CoreHooks;
50+
Foswiki::Plugins::HttpsRedirectPlugin::CoreHooks::hook();
51+
return undef;
52+
}
53+
54+
=pod
55+
7556
---++ initPlugin($topic, $web, $user, $installWeb) -> $boolean
7657
* =$topic= - the name of the topic in the current CGI query
7758
* =$web= - the name of the web in the current CGI query
7859
* =$user= - the login name of the user
7960
* =$installWeb= - the name of the web the plugin is installed in
8061
62+
Redirects requests to https:
63+
* Any request to login
64+
* Requests to any ={AuthScripts}=
65+
* Any authenticated requests.
66+
8167
=cut
8268

8369
sub initPlugin {
@@ -90,74 +76,86 @@ sub initPlugin {
9076
return 0;
9177
}
9278

93-
# Example code of how to get a preference value, register a variable handler
94-
# and register a RESTHandler. (remove code you do not need)
95-
96-
# Set plugin preferences in LocalSite.cfg, like this:
97-
# $Foswiki::cfg{Plugins}{HttpsRedirectPlugin}{ExampleSetting} = 1;
98-
# Always provide a default in case the setting is not defined in
99-
# LocalSite.cfg. See %SYSTEMWEB%.Plugins for help in adding your plugin
100-
# configuration to the =configure= interface.
10179
$debug = $Foswiki::cfg{Plugins}{HttpsRedirectPlugin}{Debug} || 0;
10280

81+
my $query = Foswiki::Func::getCgiQuery();
82+
83+
return 1 if $query->secure(); # Nothing needed if already secure
84+
return 1
85+
if Foswiki::Func::getContext()->{'command_line'}; #Not needed for CLI
86+
10387
if (Foswiki::Func::isGuest) {
10488

105-
#If we are guest, force HTTPS on login
106-
if ( Foswiki::Func::getContext()
107-
->{'login'} ) #If we are on the login script
108-
{
89+
my $actionRegex =
90+
'\b' . Foswiki::Func::getRequestObject()->action() . '\b';
10991

110-
#Build up our URL
111-
my $query = Foswiki::Func::getCgiQuery();
112-
my $url = $query->url() . $query->path_info();
113-
if ( $query->query_string() ) {
114-
$url .= '?' . $query->query_string();
115-
}
116-
117-
unless ( $url =~ /^https/ ) #Unless we are already using HTTPS
118-
{
119-
120-
#Redirect to HTTPS URL and quite
121-
$url =~ s/^http/https/;
122-
Foswiki::Func::writeDebug("HTTPS redirect to: $url")
123-
if ($debug);
124-
Foswiki::Func::redirectCgiQuery( $query, $url );
125-
126-
#$Foswiki::Plugins::SESSION->finish();
127-
#exit(0);
128-
}
92+
#If we are guest, force HTTPS on login, or any script that requires authentication.
93+
if (
94+
Foswiki::Func::getContext()
95+
->{'login'} #If we are on the login script
96+
|| $Foswiki::cfg{AuthScripts} =~ m/$actionRegex/
97+
)
98+
{
99+
_redirectRequest($query);
129100
}
130-
131101
}
132102
else {
103+
# Force redirect on all requests
104+
_redirectRequest($query);
133105

134-
#If the user is no guest always force HTTPS
106+
}
135107

136-
#Get our URL
137-
my $query = Foswiki::Func::getCgiQuery();
138-
my $url = $query->url() . $query->path_info();
139-
if ( $query->query_string() ) {
140-
$url .= '?' . $query->query_string();
141-
}
108+
# Plugin correctly initialized
109+
return 1;
110+
}
142111

143-
#Unless we are already using HTTPS, or running from CLI
144-
unless ( $url =~ /^https/
145-
or Foswiki::Func::getContext()->{'command_line'} )
146-
{
112+
=pod
113+
---++ Private _redirectRequest($query)
147114
148-
#Redirect to HTTPS URL and quite
149-
$url =~ s/^http/https/;
150-
Foswiki::Func::writeDebug("HTTPS redirect to: $url") if ($debug);
151-
Foswiki::Func::redirectCgiQuery( $query, $url );
115+
Convert the URL to https: and redirect.
152116
153-
#$Foswiki::Plugins::SESSION->finish();
154-
#exit(0);
155-
}
156-
}
117+
=cut
157118

119+
sub _redirectRequest {
120+
my $query = shift;
158121

159-
# Plugin correctly initialized
160-
return 1;
122+
#Get our URL
123+
my $url = $query->url() . $query->path_info();
124+
if ( $query->query_string() ) {
125+
$url .= '?' . $query->query_string();
126+
}
127+
128+
#Redirect to HTTPS URL
129+
$url =~ s/^http/https/;
130+
$url = Foswiki::decode_utf8($url) if $Foswiki::UNICODE;
131+
Foswiki::Func::writeDebug("HTTPS redirect to: $url") if ($debug);
132+
Foswiki::Func::redirectCgiQuery( $query, $url );
161133
}
162134

163135
1;
136+
__END__
137+
138+
Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
139+
140+
Copyright (C) 2008-2017 Foswiki Contributors. Foswiki Contributors
141+
are listed in the AUTHORS file in the root of this distribution.
142+
NOTE: Please extend that file, not this notice.
143+
144+
Additional copyrights may apply to some or all of the code in this
145+
file as follows:
146+
147+
Copyright (C) 2013 Modell Aachen GmbH, http://modell-aachen.de
148+
Author: Jan Krueger
149+
150+
Copyright (C) 1999-2007 Peter Thoeny, peter@thoeny.org
151+
This program is free software; you can redistribute it and/or
152+
modify it under the terms of the GNU General Public License
153+
as published by the Free Software Foundation; either version 2
154+
of the License, or (at your option) any later version.
155+
156+
This program is distributed in the hope that it will be useful,
157+
but WITHOUT ANY WARRANTY; without even the implied warranty of
158+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
159+
GNU General Public License for more details, published at
160+
http://www.gnu.org/copyleft/gpl.html
161+
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# See bottom of file for notices
2+
3+
package Foswiki::Plugins::HttpsRedirectPlugin::CoreHooks;
4+
5+
use strict;
6+
use warnings;
7+
use Assert;
8+
use Foswiki::Plugins::HttpsRedirectPlugin;
9+
10+
my $hooked;
11+
my $oldforceAuthentication;
12+
13+
sub hook {
14+
15+
# Prevent nasties on FastCGI/mod_perl
16+
# If the hooks were applied twice, the $old... variables would end up
17+
# containing the hooks themselves and we'd get ourselves stuck in an
18+
# infinite loop...
19+
return if defined $hooked;
20+
21+
# Overwrite the normal Foswiki functions for adding zones.
22+
# This is, sadly, necessary if we want to have the ability to
23+
# magically let through all zone code added directly by plugins
24+
# (e.g. JQueryPlugin's prefs object).
25+
26+
$oldforceAuthentication =
27+
\&Foswiki::LoginManager::TemplateLogin::forceAuthentication;
28+
undef *Foswiki::LoginManager::TemplateLogin::forceAuthentication;
29+
*Foswiki::LoginManager::TemplateLogin::forceAuthentication =
30+
\&Foswiki::Plugins::HttpsRedirectPlugin::CoreHooks::forceAuthentication;
31+
32+
$hooked = 1;
33+
return;
34+
}
35+
36+
sub forceAuthentication {
37+
38+
my $query = Foswiki::Func::getCgiQuery();
39+
40+
# No need to redirect if already a secure request.
41+
return $oldforceAuthentication->(@_) if $query->secure();
42+
43+
Foswiki::Plugins::HttpsRedirectPlugin::_redirectRequest($query);
44+
}
45+
46+
1;
47+
__DATA__
48+
49+
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
50+
51+
Copyright (C) 2008-2017 Foswiki Contributors. Foswiki Contributors
52+
are listed in the AUTHORS file in the root of this distribution.
53+
NOTE: Please extend that file, not this notice.
54+
55+
Additional copyrights may apply to some or all of the code in this
56+
file as follows:
57+
58+
Copyright (C) 2013 Modell Aachen GmbH, http://modell-aachen.de
59+
Author: Jan Krueger
60+
61+
Copyright (C) 1999-2007 Peter Thoeny, peter@thoeny.org
62+
and TWiki Contributors. All Rights Reserved. TWiki Contributors
63+
are listed in the AUTHORS file in the root of this distribution.
64+
Based on parts of Ward Cunninghams original Wiki and JosWiki.
65+
Copyright (C) 1998 Markus Peter - SPiN GmbH (warpi@spin.de)
66+
Some changes by Dave Harris (drh@bhresearch.co.uk) incorporated
67+
68+
This program is free software; you can redistribute it and/or
69+
modify it under the terms of the GNU General Public License
70+
as published by the Free Software Foundation; either version 2
71+
of the License, or (at your option) any later version. For
72+
more details read LICENSE in the root of this distribution.
73+
74+
This program is distributed in the hope that it will be useful,
75+
but WITHOUT ANY WARRANTY; without even the implied warranty of
76+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
77+
78+
As per the GPL, removal of this notice is prohibited.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
data/System/HttpsRedirectPlugin.txt 0644
22
lib/Foswiki/Plugins/HttpsRedirectPlugin.pm 0644
33
lib/Foswiki/Plugins/HttpsRedirectPlugin/Config.spec 0664
4+
lib/Foswiki/Plugins/HttpsRedirectPlugin/CoreHooks.pm 0664

0 commit comments

Comments
 (0)