TCMS.pm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. package TCMS;
  2. use 5.040;
  3. use re '/aasx';
  4. use warnings FATAL => 'all';
  5. use Carp qw{confess};
  6. use Clone qw{clone};
  7. use CGI::Cookie ();
  8. use List::Util ();
  9. use Sub::Util ();
  10. use URI();
  11. #Grab our custom routes
  12. use FindBin::libs;
  13. use Trog::Routes::HTML;
  14. use Trog::Routes::JSON;
  15. use Trog::Log qw{:all};
  16. use Trog::Log::DBI;
  17. use Trog::Auth;
  18. use Trog::Config;
  19. # Loaded here rather than where it is used, so that the vault key comes out of
  20. # the environment once, in the parent, before there is a worker to fork -- see
  21. # Trog::Vault.
  22. use Trog::Vault; ## no critic (ProhibitUnusedImports) -- loaded for what it does at load time, as above
  23. use Trog::Data;
  24. use Trog::Themes;
  25. use Trog::Bundle;
  26. #XXX Return a clone of the routing table ref, because code modifies it later
  27. my sub _routes ( $data = {} ) {
  28. state %routes;
  29. return clone( \%routes ) if %routes;
  30. my $conf = Trog::Config::get();
  31. $data = Trog::Data->new($conf) unless $data;
  32. # Routes in general are going to override earlier, more default ones.
  33. # XXX this is probably bad in the case of the specific content-typed routes, they should be mutex
  34. my %roots = $data->routes();
  35. # OpenAPI has no escape for a brace, so such a path would be a template.
  36. foreach my $href ( grep { m/[{}]/ } keys(%roots) ) {
  37. WARN("Not routing $href: a path with a brace in it cannot be a route");
  38. delete $roots{$href};
  39. }
  40. # Setup the /secure/... versions for logged in users, except for already secured routes.
  41. foreach my $route ( keys(%roots) ) {
  42. next if index( $route, '/secure/' ) == 0;
  43. $roots{"/secure$route"} = $roots{$route};
  44. }
  45. # It is the responsibility of theme authors to setup /secure routes (other than index)
  46. my %themed = Trog::Themes::routes();
  47. %routes = %Trog::Routes::HTML::routes;
  48. @routes{ keys(%Trog::Routes::JSON::routes) } = values(%Trog::Routes::JSON::routes);
  49. @routes{ keys(%roots) } = values(%roots);
  50. @routes{ keys(%themed) } = values(%themed) if %themed;
  51. # A page of posts gets the scripts of one, unless its route names its own.
  52. foreach my $path ( keys(%routes) ) {
  53. my $callback = $routes{$path}{callback} // '';
  54. my $name = ref $callback ? Sub::Util::subname($callback) : $callback;
  55. next if exists $routes{$path}{scripts} || !List::Util::any { $_ eq $name } @Trog::Routes::HTML::post_page_callbacks;
  56. $routes{$path} = { %Trog::Routes::HTML::post_page_scripts, %{ $routes{$path} } };
  57. }
  58. # Add in global routes, here because they *must* know about all other routes
  59. # Also, nobody should ever override these.
  60. $routes{'/robots.txt'} = {
  61. method => 'GET',
  62. callback => \&robots,
  63. };
  64. # Special 'am I authenticated' route for nginx
  65. $routes{'/authenticated'} = {
  66. method => 'GET',
  67. nocache => 1,
  68. noindex => 1,
  69. callback => sub {
  70. my ($query) = @_;
  71. return $query->{tpsgi}->ok($query) if $query->{user};
  72. return $query->{tpsgi}->forbidden($query);
  73. },
  74. };
  75. # Heartbeat route
  76. $routes{'/status'} = {
  77. method => 'GET',
  78. nocache => 1,
  79. noindex => 1,
  80. callback => sub {
  81. my ($query) = @_;
  82. return $query->{tpsgi}->ok($query);
  83. },
  84. };
  85. # The Various aliases for directory indices
  86. foreach my $index (qw{ / /index.html /index.htm}) {
  87. $routes{$index} = $routes{'/index'} unless exists $routes{$index};
  88. $routes{"/secure$index"} = $routes{'/index'} unless exists $routes{"/secure$index"};
  89. }
  90. return clone( \%routes );
  91. }
  92. # Troglodyne philosophy - simple as possible
  93. =head2 build_routes() = HASH
  94. The OpenAPI 3 document that tPSGI routes by, made from the routing table that
  95. _routes() returns. Each route becomes the operation of its method, under its
  96. path. The operation's x-tpsgi-callback is the route's callback, wrapped in what
  97. every tCMS request needs. C<data> becomes x-tpsgi-data, and C<auth>,
  98. C<nocache>, C<noindex>, C<nomap>, C<robot_name> and C<totp_exempt> become
  99. x-tcms-auth and so on, which the callback finds in C<< $query->{dispatcher} >>.
  100. C<parameters> are OpenAPI parameter objects, and each variable of a template
  101. that they leave out is a required string. A route with no method is a GET.
  102. Every route links the scripts of @Theme::scripts before its own C<scripts>.
  103. A route that does not declare its parameters takes the query or the form that
  104. %Trog::Routes::HTML::inputs names for its callback, less what its C<data> fixes and
  105. its path holds. Every GET also takes the parameters of a campaign, utm_source
  106. and the rest. The wrapper gives the callback the input of the request in
  107. C<< $query->{input} >>, and sets C<to> beside it, as a path on this site.
  108. The C<default> route is not a path. The wrapper calls it when there is no
  109. F<config/setup>.
  110. =cut
  111. my @tcms_keys = qw{auth nocache noindex nomap robot_name totp_exempt scripts authenticated_scripts};
  112. # The parameters of a campaign, which any page can be linked with.
  113. my @urchin = qw{utm_source utm_medium utm_campaign utm_term utm_content};
  114. # How a browser sends a form.
  115. my @form_types = qw{application/x-www-form-urlencoded multipart/form-data};
  116. sub build_routes {
  117. my $conf = Trog::Config::get();
  118. my $data = Trog::Data->new($conf);
  119. my %routes = %{ _routes($data) };
  120. # The theme's scripts go on every page, before the page's own. A copy of
  121. # each route, since a route and its /secure copy share one hash.
  122. my @theme_scripts = do { no warnings 'once'; @Theme::scripts };
  123. if (@theme_scripts) {
  124. $routes{$_} = { %{ $routes{$_} }, scripts => [ @theme_scripts, @{ $routes{$_}{scripts} // [] } ] } for keys(%routes);
  125. }
  126. my $default_route = $routes{default}{callback};
  127. my %paths;
  128. foreach my $k ( sort grep { $_ ne 'default' } keys(%routes) ) {
  129. my $v = $routes{$k};
  130. # The bundles that this route's page can link, for a reader and for a
  131. # reader who is logged in.
  132. my @scripts = @{ $v->{scripts} // [] };
  133. Trog::Bundle::declare( \@scripts, [ @scripts, @{ $v->{authenticated_scripts} // [] } ] );
  134. my $method = lc( $v->{method} // 'GET' );
  135. # FlatFile and the tag index name the callback rather than hold it.
  136. my $callback = $v->{callback};
  137. next unless $callback;
  138. my $callback_name = ref $callback ? Sub::Util::subname($callback) : $callback;
  139. $callback = \&{$callback} unless ref $callback;
  140. #XXX For now we will discard the tPSGI object. We might want it later.
  141. my $cb_wrap = sub { ## no critic (ProhibitSingleUseLexicalSubs) -- 71 lines, which would overcomplicate build_routes()
  142. my ( $tpsgi, $query ) = @_;
  143. confess "Cannot determine directory for logs!" unless $tpsgi->{log_dir};
  144. # Make sure the tCMS logs live in the same dir as the tpsgi logs.
  145. log_init( "$tpsgi->{log_dir}/tpsgi.log", $tpsgi->{verbose} ? 'debug' : 'info', $tpsgi->{loggers} );
  146. # Let's open up our default route if needed before we bother thinking any harder
  147. return $default_route->($query) unless -f "config/setup"; ## no critic (ProhibitFiletest_f) -- a flag file, only ever touched
  148. my $input = $query->{input} //= {};
  149. # Set the urchin parameters if necessary.
  150. %$Trog::Log::DBI::urchin = map { $_ => $input->{$_} } @urchin;
  151. # Set the IP of the request so we can fail2ban
  152. $Trog::Log::ip = $query->{ip};
  153. # Set the referer & ua to go into DB logs, but not logs in general.
  154. # The referer/ua largely has no importance beyond being a proto bug report for log messages.
  155. $Trog::Log::DBI::referer = $query->{referer};
  156. $Trog::Log::DBI::ua = $query->{ua};
  157. # Figure out if we have a logged in user, so we can serve them user-specific files
  158. my $cookies = {};
  159. $cookies = CGI::Cookie->parse( $query->{cookies} ) if $query->{cookies};
  160. my $active_user = '';
  161. $Trog::Log::user = 'nobody';
  162. if ( exists $cookies->{tcmslogin} ) {
  163. $active_user = Trog::Auth::session2user( $cookies->{tcmslogin}->value );
  164. $Trog::Log::user = $active_user if $active_user;
  165. }
  166. # Make sure TPSGI can log the user
  167. $tpsgi->{user} = $active_user;
  168. # Enrich the query with tcms specific foo
  169. $query->{user_acls} = [];
  170. $query->{user_acls} = Trog::Auth::acls4user($active_user) // [] if $active_user;
  171. $query->{user} = $active_user;
  172. $query->{body} = '';
  173. $query->{social_meta} = 1;
  174. $query->{primary_post} = {};
  175. $query->{nocache} = $v->{nocache};
  176. # Some routes may just need to do serve()
  177. $query->{tpsgi} = $tpsgi;
  178. # Only ever a path on this site, so that no link can send a visitor to
  179. # another one. One slash, because a browser reads two as a host.
  180. $query->{to} = ( URI->new( $input->{to} )->path() || '/' ) =~ s{\A/+}{/}r if $input->{to};
  181. # Now that we have firmed up the actual routing, let's validate.
  182. return $tpsgi->forbidden($query) if exists $query->{dispatcher}{'x-tcms-auth'} && !$active_user;
  183. if ( needs_enrolment( $active_user, $query->{dispatcher} ) ) {
  184. INFO("$active_user has no TOTP enrolment; sending them to /totp");
  185. return $tpsgi->see_also('/totp');
  186. }
  187. return $callback->($query);
  188. };
  189. my @parameters = @{ $v->{parameters} // [] };
  190. foreach my $variable ( $k =~ m/\{([^{}]+)\}/g ) {
  191. next if List::Util::any { $_->{in} eq 'path' && $_->{name} eq $variable } @parameters;
  192. push( @parameters, { name => $variable, in => 'path', required => true, schema => { type => 'string' } } );
  193. }
  194. # What the callback takes from the request, less what the route fixes in
  195. # its data or holds in its path. A route that declares its parameters
  196. # declares all of its query.
  197. my $inputs = { %{ $Trog::Routes::HTML::inputs{$callback_name} // {} } };
  198. $_ = $_->() for grep { ref eq 'CODE' } @$inputs{qw{query body}};
  199. my %taken = map { $_ => 1 } keys( %{ $v->{data} // {} } ), map { $_->{name} =~ s/[+]\z//r } @parameters;
  200. my %query = ( ( $v->{parameters} ? () : %{ $inputs->{query} // {} } ), ( $method eq 'get' ? map { $_ => { type => 'string' } } @urchin : () ) );
  201. push( @parameters, map { { name => $_, in => 'query', schema => $query{$_} } } grep { !$taken{$_} } sort keys(%query) ) if $method eq 'get';
  202. my $body = $inputs->{body};
  203. my %form = (
  204. type => 'object',
  205. properties => { map { $_ => $body->{$_} } grep { !$taken{$_} } keys( %{ $body // {} } ) },
  206. ( $inputs->{open} ? ( additionalProperties => true ) : () ),
  207. );
  208. my $request_body = $method eq 'post' && ( $body || $inputs->{open} ) ? { content => { map { $_ => { schema => \%form } } @form_types } } : undef;
  209. $paths{$k}{$method} = {
  210. 'x-tpsgi-callback' => $cb_wrap,
  211. ( $v->{data} ? ( 'x-tpsgi-data' => $v->{data} ) : () ),
  212. ( map { ( "x-tcms-$_" => $v->{$_} ) } grep { exists $v->{$_} } @tcms_keys ),
  213. ( @parameters ? ( parameters => \@parameters ) : () ),
  214. ( $request_body ? ( requestBody => $request_body ) : () ),
  215. responses => { default => { description => 'The page, or an error page' } },
  216. };
  217. }
  218. return (
  219. openapi => '3.0.3',
  220. info => { title => 'tCMS', version => '1' },
  221. paths => \%paths,
  222. );
  223. }
  224. =head2 needs_enrolment($user, $dispatcher) = BOOL
  225. Whether this request should be turned around and sent to /totp.
  226. Two factors or nothing. A login is what gets you to the enrollment page -- the QR
  227. is the shared secret, so it cannot be handed to whoever asks for it -- which
  228. leaves a window where somebody is logged in and has no second factor yet. This
  229. is what makes that window one page wide: every route a login can reach, and no
  230. route a logged out visitor can, is refused until they have enrolled.
  231. The enrollment page and the QR on it say so themselves, with totp_exempt, because
  232. the exemption belongs next to the route rather than in a list over here that the
  233. routing table knows nothing about. $dispatcher is the operation that
  234. build_routes() made, so it holds them as x-tcms-auth and x-tcms-totp_exempt.
  235. =cut
  236. sub needs_enrolment ( $user, $dispatcher ) {
  237. return 0 unless $user;
  238. return 0 if !$dispatcher || !$dispatcher->{'x-tcms-auth'} || $dispatcher->{'x-tcms-totp_exempt'};
  239. return Trog::Auth::has_totp($user) ? 0 : 1;
  240. }
  241. # Override the generic error handler to look spiffy
  242. sub generic_route ( $rname, $code, $title, $query ) {
  243. $query->{code} = $code;
  244. $query->{route} //= $rname;
  245. $query->{title} = $title;
  246. $query->{template} = "$rname.tx";
  247. # What the error pages read: 404.tx the path that was not found, and
  248. # 500.tx the message that tPSGI sets when a route dies.
  249. $query->{path} = $query->{fullpath} // $query->{route};
  250. $query->{message} //= undef;
  251. return Trog::Routes::HTML::index($query);
  252. }
  253. our %openapi = build_routes();
  254. our %aliases = aliases();
  255. sub aliases {
  256. my $conf = Trog::Config::get();
  257. my $data = Trog::Data->new($conf);
  258. return $data->aliases();
  259. }
  260. =head2 robots
  261. Return an appropriate robots.txt
  262. This is a "special" route as it needs to know about all the routes in order to disallow noindex=1 routes.
  263. =cut
  264. # TODO anytime that the routing table changes, we need to invalidate the cache.
  265. sub robots ($query) {
  266. state $etag = "robots-" . time();
  267. my $routes = _routes();
  268. # A template's variables can be anything, which robots.txt spells as *.
  269. state @banned = map {
  270. my $robot_name = $routes->{$_}{robot_name} // $_;
  271. $robot_name =~ s/\{[^{}]*\}/*/g;
  272. $robot_name
  273. } grep { $routes->{$_}{noindex} } sort keys(%$routes);
  274. return Trog::Renderer->render(
  275. contenttype => 'text/plain',
  276. template => 'robots.tx',
  277. data => {
  278. etag => $etag,
  279. banned => \@banned,
  280. %$query,
  281. },
  282. code => 200,
  283. );
  284. }
  285. 1;