-
Notifications
You must be signed in to change notification settings - Fork 2k
/
index.js
986 lines (840 loc) · 29.8 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
/**
* External dependencies
*/
import express from 'express';
import fs from 'fs';
import path from 'path';
import { stringify } from 'qs';
import crypto from 'crypto';
import { execSync } from 'child_process';
import cookieParser from 'cookie-parser';
import debugFactory from 'debug';
import {
defaults,
endsWith,
get,
includes,
pick,
flatten,
groupBy,
intersection,
snakeCase,
split,
} from 'lodash';
import bodyParser from 'body-parser';
// eslint-disable-next-line no-restricted-imports
import superagent from 'superagent'; // Don't have Node.js fetch lib yet.
import { matchesUA } from 'browserslist-useragent';
/**
* Internal dependencies
*/
import config from 'config';
import sanitize from 'server/sanitize';
import utils from 'server/bundler/utils';
import { pathToRegExp } from 'utils';
import sections from 'sections';
import isSectionEnabled from 'sections-filter';
import loginRouter, { LOGIN_SECTION_DEFINITION } from 'login';
import { serverRouter, getNormalizedPath } from 'server/isomorphic-routing';
import {
serverRender,
renderJsx,
attachBuildTimestamp,
attachHead,
attachI18n,
} from 'server/render';
import stateCache from 'server/state-cache';
import getBootstrappedUser from 'server/user-bootstrap';
import { createReduxStore } from 'state';
import { setStore } from 'state/redux-store';
import initialReducer from 'state/reducer';
import { DESERIALIZE, LOCALE_SET } from 'state/action-types';
import { setCurrentUser } from 'state/current-user/actions';
import { login } from 'lib/paths';
import { logSectionResponse } from './analytics';
import analytics from 'server/lib/analytics';
import { getLanguage, filterLanguageRevisions } from 'lib/i18n-utils';
import { isWooOAuth2Client } from 'lib/oauth2-clients';
import GUTENBOARDING_BASE_NAME from 'landing/gutenboarding/basename.json';
import { GUTENBOARDING_SECTION_DEFINITION } from 'landing/gutenboarding/section';
import wooDnaConfig from 'jetpack-connect/woo-dna-config';
const debug = debugFactory( 'calypso:pages' );
const SERVER_BASE_PATH = '/public';
const calypsoEnv = config( 'env_id' );
const staticFiles = [ { path: 'editor.css' }, { path: 'tinymce/skins/wordpress/wp-content.css' } ];
const staticFilesUrls = staticFiles.reduce( ( result, file ) => {
if ( ! file.hash ) {
file.hash = utils.hashFile( process.cwd() + SERVER_BASE_PATH + '/' + file.path );
}
result[ file.path ] = utils.getUrl( file.path, file.hash );
return result;
}, {} );
// List of browser languages to show pride styling for.
// Add a '*' element to show the styling for all visitors.
const prideLanguages = [];
// List of geolocated locations to show pride styling for.
// Geolocation may not be 100% accurate.
const prideLocations = [];
// TODO: Re-use (a modified version of) client/state/initial-state#getInitialServerState here
function getInitialServerState( serializedServerState ) {
// Bootstrapped state from a server-render
const serverState = initialReducer( serializedServerState, { type: DESERIALIZE } );
return pick( serverState, Object.keys( serializedServerState ) );
}
/**
* Checks whether a user agent is included in the browser list for an environment.
*
* @param {string} userAgentString The user agent string.
* @param {string} environment The `browserslist` environment.
*
* @returns {boolean} Whether the user agent is included in the browser list.
*/
function isUAInBrowserslist( userAgentString, environment = 'defaults' ) {
return matchesUA( userAgentString, {
env: environment,
ignorePatch: true,
ignoreMinor: true,
allowHigherVersions: true,
} );
}
function getBuildTargetFromRequest( request ) {
const isDevelopment = process.env.NODE_ENV === 'development';
const isDesktop = calypsoEnv === 'desktop' || calypsoEnv === 'desktop-development';
const devTarget = process.env.DEV_TARGET || 'evergreen';
const uaTarget = isUAInBrowserslist( request.useragent.source, 'evergreen' )
? 'evergreen'
: 'fallback';
// Did the user force fallback, via query parameter?
const prodTarget = request.query.forceFallback ? 'fallback' : uaTarget;
let target = isDevelopment ? devTarget : prodTarget;
if ( isDesktop ) {
target = 'fallback';
}
return target === 'fallback' ? null : target;
}
const ASSETS_PATH = path.join( __dirname, '../', 'bundler' );
function getAssetsPath( target ) {
const result = path.join(
ASSETS_PATH,
target ? `assets-${ target }.json` : 'assets-fallback.json'
);
return result;
}
const cachedAssets = {};
const getAssets = ( target ) => {
if ( ! cachedAssets[ target ] ) {
cachedAssets[ target ] = JSON.parse( fs.readFileSync( getAssetsPath( target ), 'utf8' ) );
}
return cachedAssets[ target ];
};
const EMPTY_ASSETS = { js: [], 'css.ltr': [], 'css.rtl': [] };
const getAssetType = ( asset ) => {
if ( asset.endsWith( '.rtl.css' ) ) {
return 'css.rtl';
}
if ( asset.endsWith( '.css' ) ) {
return 'css.ltr';
}
return 'js';
};
const groupAssetsByType = ( assets ) => defaults( groupBy( assets, getAssetType ), EMPTY_ASSETS );
const getFilesForChunk = ( chunkName, request ) => {
const target = getBuildTargetFromRequest( request );
const assets = getAssets( target );
function getChunkByName( _chunkName ) {
return assets.chunks.find( ( chunk ) => chunk.names.some( ( name ) => name === _chunkName ) );
}
function getChunkById( chunkId ) {
return assets.chunks.find( ( chunk ) => chunk.id === chunkId );
}
const chunk = getChunkByName( chunkName );
if ( ! chunk ) {
console.warn( 'cannot find the chunk ' + chunkName );
console.warn( 'available chunks:' );
assets.chunks.forEach( ( c ) => {
console.log( ' ' + c.id + ': ' + c.names.join( ',' ) );
} );
return EMPTY_ASSETS;
}
const allTheFiles = chunk.files.concat(
flatten( chunk.siblings.map( ( sibling ) => getChunkById( sibling ).files ) )
);
return groupAssetsByType( allTheFiles );
};
const getFilesForEntrypoint = ( target, name ) => {
const entrypointAssets = getAssets( target ).entrypoints[ name ].assets.filter(
( asset ) => ! asset.startsWith( 'manifest' )
);
return groupAssetsByType( entrypointAssets );
};
function getCurrentBranchName() {
try {
return execSync( 'git rev-parse --abbrev-ref HEAD' ).toString().replace( /\s/gm, '' );
} catch ( err ) {
return undefined;
}
}
function getCurrentCommitShortChecksum() {
try {
return execSync( 'git rev-parse --short HEAD' ).toString().replace( /\s/gm, '' );
} catch ( err ) {
return undefined;
}
}
/**
* Given the content of an 'Accept-Language' request header, returns an array of the languages.
*
* This differs slightly from other language functions, as it doesn't try to validate the language codes,
* or merge similar language codes.
*
* @param {string} header - The content of the AcceptedLanguages header.
* @returns {Array} An array of language codes in the header, all in lowercase.
*/
function getAcceptedLanguagesFromHeader( header ) {
if ( ! header ) {
return [];
}
return header
.split( ',' )
.map( ( lang ) => {
const match = lang.match( /^[A-Z]{2,3}(-[A-Z]{2,3})?/i );
if ( ! match ) {
return false;
}
return match[ 0 ].toLowerCase();
} )
.filter( ( lang ) => lang );
}
/*
* Look at the request headers and determine if the request is logged in or logged out or if
* it's a support session. Set `req.context.isLoggedIn` and `req.context.isSupportSession` flags
* accordingly. The handler is called very early (immediately after parsing the cookies) and
* all following handlers (including the locale and redirect ones) can rely on the context values.
*/
function setupLoggedInContext( req, res, next ) {
const isSupportSession = !! req.get( 'x-support-session' ) || !! req.cookies.support_session_id;
const isLoggedIn = !! req.cookies.wordpress_logged_in;
req.context = {
...req.context,
isSupportSession,
isLoggedIn,
};
next();
}
function getDefaultContext( request, entrypoint = 'entry-main' ) {
let initialServerState = {};
let lang = config( 'i18n_default_locale_slug' );
const bodyClasses = [];
// We don't compare context.query against an allowed list here. Explicit allowance lists are route-specific,
// i.e. they can be created by route-specific middleware. `getDefaultContext` is always
// called before route-specific middleware, so it's up to the cache *writes* in server
// render to make sure that Redux state and markup are only cached for specified query args.
const cacheKey = getNormalizedPath( request.path, request.query );
const geoLocation = ( request.headers[ 'x-geoip-country-code' ] || '' ).toLowerCase();
const devEnvironments = [ 'development', 'jetpack-cloud-development' ];
const isDebug = devEnvironments.includes( calypsoEnv ) || request.query.debug !== undefined;
if ( cacheKey ) {
const serializeCachedServerState = stateCache.get( cacheKey ) || {};
initialServerState = getInitialServerState( serializeCachedServerState );
}
// Note: The x-geoip-country-code header should *not* be considered 100% accurate.
// It should only be used for guestimating the visitor's location.
const acceptedLanguages = getAcceptedLanguagesFromHeader( request.headers[ 'accept-language' ] );
if (
prideLanguages.indexOf( '*' ) > -1 ||
intersection( prideLanguages, acceptedLanguages ).length > 0 ||
prideLocations.indexOf( '*' ) > -1 ||
prideLocations.indexOf( geoLocation ) > -1
) {
bodyClasses.push( 'pride' );
}
// We assign request.context.lang in the handleLocaleSubdomains()
// middleware function if we detect a language slug in subdomain
if ( request.context && request.context.lang ) {
lang = request.context.lang;
}
const target = getBuildTargetFromRequest( request );
const oauthClientId = request.query.oauth2_client_id || request.query.client_id;
const isWCComConnect =
config.isEnabled( 'woocommerce/onboarding-oauth' ) &&
( 'login' === request.context.sectionName || 'signup' === request.context.sectionName ) &&
request.query[ 'wccom-from' ] &&
isWooOAuth2Client( { id: parseInt( oauthClientId ) } );
const reduxStore = createReduxStore( initialServerState );
setStore( reduxStore );
const flags = ( request.query.flags || '' ).split( ',' );
const context = Object.assign( {}, request.context, {
commitSha: process.env.hasOwnProperty( 'COMMIT_SHA' ) ? process.env.COMMIT_SHA : '(unknown)',
compileDebug: process.env.NODE_ENV === 'development',
user: false,
env: calypsoEnv,
sanitize: sanitize,
isRTL: config( 'rtl' ),
requestFrom: request.query.from,
isWCComConnect,
isWooDna: wooDnaConfig( request.query ).isWooDnaFlow(),
badge: false,
lang,
entrypoint: getFilesForEntrypoint( target, entrypoint ),
manifest: getAssets( target ).manifests.manifest,
faviconURL: config( 'favicon_url' ),
abTestHelper: !! config.isEnabled( 'dev/test-helper' ),
preferencesHelper: !! config.isEnabled( 'dev/preferences-helper' ),
devDocsURL: '/devdocs',
store: reduxStore,
bodyClasses,
addEvergreenCheck: target === 'evergreen' && calypsoEnv !== 'development',
target: target || 'fallback',
useTranslationChunks:
config.isEnabled( 'use-translation-chunks' ) ||
flags.includes( 'use-translation-chunks' ) ||
request.query.hasOwnProperty( 'useTranslationChunks' ),
} );
context.app = {
// use ipv4 address when is ipv4 mapped address
clientIp: request.ip ? request.ip.replace( '::ffff:', '' ) : request.ip,
isDebug,
staticUrls: staticFilesUrls,
};
if ( calypsoEnv === 'wpcalypso' ) {
context.badge = calypsoEnv;
context.devDocs = true;
context.feedbackURL = 'https://github.com/Automattic/wp-calypso/issues/';
// this is for calypso.live, so that branchName can be available while rendering the page
if ( request.query.branch ) {
context.branchName = request.query.branch;
}
}
if ( calypsoEnv === 'horizon' ) {
context.badge = 'feedback';
context.feedbackURL = 'https://horizonfeedback.wordpress.com/';
}
if ( calypsoEnv === 'stage' ) {
context.badge = 'staging';
context.feedbackURL = 'https://github.com/Automattic/wp-calypso/issues/';
}
if ( calypsoEnv === 'development' ) {
context.badge = 'dev';
context.devDocs = true;
context.feedbackURL = 'https://github.com/Automattic/wp-calypso/issues/';
context.branchName = getCurrentBranchName();
context.commitChecksum = getCurrentCommitShortChecksum();
}
if ( calypsoEnv === 'jetpack-cloud-stage' ) {
context.badge = 'jetpack-cloud-staging';
context.feedbackURL = 'https://github.com/Automattic/wp-calypso/issues/';
}
if ( calypsoEnv === 'jetpack-cloud-development' ) {
context.badge = 'jetpack-cloud-dev';
context.feedbackURL = 'https://github.com/Automattic/wp-calypso/issues/';
context.branchName = getCurrentBranchName();
context.commitChecksum = getCurrentCommitShortChecksum();
}
return context;
}
const setupDefaultContext = ( entrypoint ) => ( req, res, next ) => {
req.context = getDefaultContext( req, entrypoint );
next();
};
function setUpLocalLanguageRevisions( req ) {
const targetFromRequest = getBuildTargetFromRequest( req );
const target = targetFromRequest === null ? 'fallback' : targetFromRequest;
const rootPath = path.join( __dirname, '..', '..', '..' );
const langRevisionsPath = path.join(
rootPath,
'public',
target,
'languages',
'lang-revisions.json'
);
const langPromise = fs.promises
.readFile( langRevisionsPath, 'utf8' )
.then( ( languageRevisions ) => {
req.context.languageRevisions = JSON.parse( languageRevisions );
return languageRevisions;
} )
.catch( ( error ) => {
console.error( 'Failed to read the language revision files.', error );
throw error;
} );
return langPromise;
}
function setUpLoggedOutRoute( req, res, next ) {
res.set( {
'X-Frame-Options': 'SAMEORIGIN',
} );
const setupRequests = [];
if ( req.context.useTranslationChunks ) {
setupRequests.push( setUpLocalLanguageRevisions( req ) );
}
Promise.all( setupRequests )
.then( () => next() )
.catch( ( error ) => next( error ) );
}
function setUpLoggedInRoute( req, res, next ) {
let redirectUrl, start;
res.set( {
'X-Frame-Options': 'SAMEORIGIN',
} );
const setupRequests = [];
if ( req.context.useTranslationChunks ) {
setupRequests.push( setUpLocalLanguageRevisions( req ) );
} else {
const LANG_REVISION_FILE_URL = 'https://widgets.wp.com/languages/calypso/lang-revisions.json';
const langPromise = superagent
.get( LANG_REVISION_FILE_URL )
.then( ( response ) => {
const languageRevisions = filterLanguageRevisions( response.body );
req.context.languageRevisions = languageRevisions;
return languageRevisions;
} )
.catch( ( error ) => {
console.error( 'Failed to fetch the language revision files.', error );
throw error;
} );
setupRequests.push( langPromise );
}
if ( config.isEnabled( 'wpcom-user-bootstrap' ) ) {
const protocol = req.get( 'X-Forwarded-Proto' ) === 'https' ? 'https' : 'http';
redirectUrl = login( {
isNative: config.isEnabled( 'login/native-login-links' ),
redirectTo: protocol + '://' + config( 'hostname' ) + req.originalUrl,
} );
if ( ! req.context.isLoggedIn ) {
debug( 'User not logged in. Redirecting to %s', redirectUrl );
res.redirect( redirectUrl );
return;
}
start = new Date().getTime();
debug( 'Issuing API call to fetch user object' );
const userPromise = getBootstrappedUser( req )
.then( ( data ) => {
const end = new Date().getTime() - start;
debug( 'Rendering with bootstrapped user object. Fetched in %d ms', end );
req.context.user = data;
// Setting user in the state is safe as long as we don't cache it
req.context.store.dispatch( setCurrentUser( data ) );
if ( data.localeSlug ) {
req.context.lang = data.localeSlug;
req.context.store.dispatch( {
type: LOCALE_SET,
localeSlug: data.localeSlug,
localeVariant: data.localeVariant,
} );
}
if ( req.path === '/' && req.query ) {
const searchParam = req.query.s || req.query.q;
if ( searchParam ) {
res.redirect(
'https://' +
req.context.lang +
'.search.wordpress.com/?q=' +
encodeURIComponent( searchParam )
);
return;
}
if ( req.query.newuseremail ) {
debug( 'Detected legacy email verification action. Redirecting...' );
res.redirect( 'https://wordpress.com/verify-email/?' + stringify( req.query ) );
return;
}
if ( req.query.action === 'wpcom-invite-users' ) {
debug( 'Detected legacy invite acceptance action. Redirecting...' );
res.redirect( 'https://wordpress.com/accept-invite/?' + stringify( req.query ) );
return;
}
}
} )
.catch( ( error ) => {
if ( error.error === 'authorization_required' ) {
debug( 'User public API authorization required. Redirecting to %s', redirectUrl );
res.clearCookie( 'wordpress_logged_in', {
path: '/',
httpOnly: true,
domain: '.wordpress.com',
} );
res.redirect( redirectUrl );
} else {
let errorMessage;
if ( error.error ) {
errorMessage = error.error + ' ' + error.message;
} else {
errorMessage = error.message;
}
console.error( 'API Error: ' + errorMessage );
}
throw error;
} );
setupRequests.push( userPromise );
}
Promise.all( setupRequests )
.then( () => next() )
.catch( ( error ) => next( error ) );
}
/**
* Sets up a Content Security Policy header
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP
* @param {object} req Express request object
* @param {object} res Express response object
* @param {Function} next a callback to call when done
*/
function setUpCSP( req, res, next ) {
const originalUrlPathname = req.originalUrl.split( '?' )[ 0 ];
// We only setup CSP for /log-in* for now
if ( ! /^\/log-in/.test( originalUrlPathname ) ) {
next();
return;
}
// This is calculated by taking the contents of the script text from between the tags,
// and calculating SHA256 hash on it, encoded in base64, example:
// `sha256-${ base64( sha256( 'window.AppBoot();' ) ) }` === sha256-3yiQswl88knA3EhjrG5tj5gmV6EUdLYFvn2dygc0xUQ
// you can also just run it in Chrome, chrome will give you the hash of the violating scripts
const inlineScripts = [
'sha256-3yiQswl88knA3EhjrG5tj5gmV6EUdLYFvn2dygc0xUQ=',
'sha256-ZKTuGaoyrLu2lwYpcyzib+xE4/2mCN8PKv31uXS3Eg4=',
];
req.context.inlineScriptNonce = crypto.randomBytes( 48 ).toString( 'hex' );
const policy = {
'default-src': [ "'self'" ],
'script-src': [
"'self'",
"'report-sample'",
"'unsafe-eval'",
'stats.wp.com',
'https://widgets.wp.com',
'*.wordpress.com',
'https://apis.google.com',
'https://appleid.cdn-apple.com',
`'nonce-${ req.context.inlineScriptNonce }'`,
'www.google-analytics.com',
...inlineScripts.map( ( hash ) => `'${ hash }'` ),
],
'base-uri': [ "'none'" ],
'style-src': [ "'self'", '*.wp.com', 'https://fonts.googleapis.com' ],
'form-action': [ "'self'" ],
'object-src': [ "'none'" ],
'img-src': [
"'self'",
'data',
'*.wp.com',
'*.files.wordpress.com',
'*.gravatar.com',
'https://www.google-analytics.com',
'https://amplifypixel.outbrain.com',
'https://img.youtube.com',
],
'frame-src': [ "'self'", 'https://public-api.wordpress.com', 'https://accounts.google.com/' ],
'font-src': [
"'self'",
'*.wp.com',
'https://fonts.gstatic.com',
'data:', // should remove 'data:' ASAP
],
'media-src': [ "'self'" ],
'connect-src': [ "'self'", 'https://*.wordpress.com/', 'https://*.wp.com' ],
'report-uri': [ '/cspreport' ],
};
const policyString = Object.keys( policy )
.map( ( key ) => `${ key } ${ policy[ key ].join( ' ' ) }` )
.join( '; ' );
// For now we're just logging policy violations and not blocking them
// so we won't actually break anything, later we'll remove the 'Report-Only'
// part so browsers will block violating content.
res.set( { 'Content-Security-Policy-Report-Only': policyString } );
next();
}
function setUpRoute( req, res, next ) {
setUpCSP( req, res, () =>
req.context.isLoggedIn
? setUpLoggedInRoute( req, res, next )
: setUpLoggedOutRoute( req, res, next )
);
}
const render404 = ( entrypoint = 'entry-main' ) => ( req, res ) => {
const ctx = {
faviconURL: config( 'favicon_url' ),
entrypoint: getFilesForEntrypoint( getBuildTargetFromRequest( req ), entrypoint ),
};
res.status( 404 ).send( renderJsx( '404', ctx ) );
};
/* We don't use `next` but need to add it for express.js to
recognize this function as an error handler, hence the
eslint-disable. */
// eslint-disable-next-line no-unused-vars
const renderServerError = ( entrypoint = 'entry-main' ) => ( err, req, res, next ) => {
if ( process.env.NODE_ENV !== 'production' ) {
console.error( err );
}
const ctx = {
faviconURL: config( 'favicon_url' ),
entrypoint: getFilesForEntrypoint( getBuildTargetFromRequest( req ), entrypoint ),
};
res.status( err.status || 500 ).send( renderJsx( '500', ctx ) );
};
/**
* Sets language properties to context if
* a WordPress.com language slug is detected in the hostname
*
* @param {object} req Express request object
* @param {object} res Express response object
* @param {Function} next a callback to call when done
* @returns {Function|undefined} res.redirect if not logged in
*/
function handleLocaleSubdomains( req, res, next ) {
const langSlug = endsWith( req.hostname, config( 'hostname' ) )
? split( req.hostname, '.' )[ 0 ]
: null;
if ( langSlug && includes( config( 'magnificent_non_en_locales' ), langSlug ) ) {
// Retrieve the language object for the RTL information.
const language = getLanguage( langSlug );
// Switch locales only in a logged-out state.
if ( language && ! req.context.isLoggedIn ) {
req.context = {
...req.context,
lang: language.langSlug,
isRTL: !! language.rtl,
};
} else {
// Strip the langSlug and redirect using hostname
// so that the user's locale preferences take priority.
const protocol = req.get( 'X-Forwarded-Proto' ) === 'https' ? 'https' : 'http';
const port = process.env.PORT || config( 'port' ) || '';
const hostname = req.hostname.substr( langSlug.length + 1 );
const redirectUrl = `${ protocol }://${ hostname }:${ port }${ req.path }`;
return res.redirect( redirectUrl );
}
}
next();
}
export default function pages() {
const app = express();
app.set( 'views', __dirname );
app.use( logSectionResponse );
app.use( cookieParser() );
app.use( setupLoggedInContext );
app.use( handleLocaleSubdomains );
// redirect homepage if the Reader is disabled
app.get( '/', function ( request, response, next ) {
if ( ! config.isEnabled( 'reader' ) && config.isEnabled( 'stats' ) ) {
response.redirect( '/stats' );
} else {
next();
}
} );
// redirects to handle old newdash formats
app.use( '/sites/:site/:section', function ( req, res, next ) {
const redirectedSections = [
'posts',
'pages',
'sharing',
'upgrade',
'checkout',
'change-theme',
];
let redirectUrl;
if ( -1 === redirectedSections.indexOf( req.params.section ) ) {
next();
return;
}
if ( 'change-theme' === req.params.section ) {
redirectUrl = req.originalUrl.replace( /^\/sites\/[0-9a-zA-Z\-.]+\/change-theme/, '/themes' );
} else {
redirectUrl = req.originalUrl.replace(
/^\/sites\/[0-9a-zA-Z\-.]+\/\w+/,
'/' + req.params.section + '/' + req.params.site
);
}
res.redirect( redirectUrl );
} );
if ( process.env.NODE_ENV !== 'development' ) {
app.get( '/discover', function ( req, res, next ) {
if ( ! req.context.isLoggedIn ) {
res.redirect( config( 'discover_logged_out_redirect_url' ) );
} else {
next();
}
} );
// redirect logged-out searches to en.search.wordpress.com
app.get( '/read/search', function ( req, res, next ) {
if ( ! req.context.isLoggedIn ) {
res.redirect( 'https://en.search.wordpress.com/?q=' + encodeURIComponent( req.query.q ) );
} else {
next();
}
} );
app.get( '/plans', function ( req, res, next ) {
if ( ! req.context.isLoggedIn ) {
const queryFor = req.query && req.query.for;
if ( queryFor && 'jetpack' === queryFor ) {
res.redirect(
'https://wordpress.com/wp-login.php?redirect_to=https%3A%2F%2Fwordpress.com%2Fplans'
);
} else {
res.redirect( 'https://wordpress.com/pricing' );
}
} else {
next();
}
} );
}
// Redirect legacy `/menus` routes to the corresponding Customizer panel
// TODO: Move to `my-sites/customize` route defs once that section is isomorphic
app.get( [ '/menus', '/menus/:site?' ], ( req, res ) => {
const siteSlug = get( req.params, 'site', '' );
const newRoute = '/customize/menus/' + siteSlug;
res.redirect( 301, newRoute );
} );
app.get( [ '/domains', '/start/domain-first' ], function ( req, res ) {
let redirectUrl = '/start/domain';
const domain = get( req, 'query.new', false );
if ( domain ) {
redirectUrl += '?new=' + encodeURIComponent( domain );
}
res.redirect( redirectUrl );
} );
// Landing pages for domains-related emails
app.get(
'/domain-services/:action',
setupDefaultContext( 'entry-domains-landing' ),
( req, res ) => {
const ctx = req.context;
attachBuildTimestamp( ctx );
attachHead( ctx );
attachI18n( ctx );
ctx.clientData = config.clientData;
ctx.domainsLandingData = {
action: get( req, [ 'params', 'action' ], 'unknown-action' ),
query: get( req, 'query', {} ),
};
const pageHtml = renderJsx( 'domains-landing', ctx );
res.send( pageHtml );
}
);
function handleSectionPath( section, sectionPath, entrypoint ) {
const pathRegex = pathToRegExp( sectionPath );
app.get( pathRegex, setupDefaultContext( entrypoint ), function ( req, res, next ) {
req.context.sectionName = section.name;
if ( ! entrypoint ) {
req.context.chunkFiles = getFilesForChunk( section.name, req );
} else {
req.context.chunkFiles = EMPTY_ASSETS;
}
if ( section.secondary && req.context ) {
req.context.hasSecondary = true;
}
if ( section.group && req.context ) {
req.context.sectionGroup = section.group;
}
next();
} );
if ( ! section.isomorphic ) {
app.get( pathRegex, setUpRoute, serverRender );
}
}
sections
.filter( ( section ) => ! section.envId || section.envId.indexOf( config( 'env_id' ) ) > -1 )
.filter( isSectionEnabled )
.forEach( ( section ) => {
section.paths.forEach( ( sectionPath ) => handleSectionPath( section, sectionPath ) );
if ( section.isomorphic ) {
// section.load() uses require on the server side so we also need to access the
// default export of it. See webpack/bundler/sections-loader.js
// TODO: section initialization is async function since #28301. At the moment when
// some isomorphic section really starts doing something async, we should start
// awaiting the result here. Will be solved together with server-side dynamic reducers.
section.load().default( serverRouter( app, setUpRoute, section ) );
}
} );
// Set up login routing.
handleSectionPath( LOGIN_SECTION_DEFINITION, '/log-in', 'entry-login' );
loginRouter( serverRouter( app, setUpRoute, null ) );
handleSectionPath(
GUTENBOARDING_SECTION_DEFINITION,
`/${ GUTENBOARDING_BASE_NAME }`,
'entry-gutenboarding'
);
// Handle redirection from development basename
// TODO: Remove after a few months
handleSectionPath( GUTENBOARDING_SECTION_DEFINITION, `/gutenboarding`, 'entry-gutenboarding' );
// This is used to log to tracks Content Security Policy violation reports sent by browsers
app.post(
'/cspreport',
bodyParser.json( { type: [ 'json', 'application/csp-report' ] } ),
function ( req, res ) {
const cspReport = req.body[ 'csp-report' ] || {};
const cspReportSnakeCase = Object.keys( cspReport ).reduce( ( report, key ) => {
report[ snakeCase( key ) ] = cspReport[ key ];
return report;
}, {} );
if ( calypsoEnv !== 'development' ) {
analytics.tracks.recordEvent( 'calypso_csp_report', cspReportSnakeCase, req );
}
res.status( 200 ).send( 'Got it!' );
},
// eslint-disable-next-line no-unused-vars
function ( err, req, res, next ) {
res.status( 500 ).send( 'Bad report!' );
}
);
app.get( '/browsehappy', setupDefaultContext(), setUpRoute, function ( req, res ) {
const wpcomRe = /^https?:\/\/[A-z0-9_-]+\.wordpress\.com$/;
const primaryBlogUrl = get( req, 'context.user.primary_blog_url', '' );
const isWpcom = wpcomRe.test( primaryBlogUrl );
req.context.dashboardUrl = isWpcom
? primaryBlogUrl + '/wp-admin'
: 'https://dashboard.wordpress.com/wp-admin/';
res.send( renderJsx( 'browsehappy', req.context ) );
} );
app.get( '/support-user', function ( req, res ) {
// Do not iframe
res.set( {
'X-Frame-Options': 'DENY',
} );
if ( calypsoEnv === 'development' ) {
return res.send(
renderJsx( 'support-user', {
authorized: true,
supportUser: req.query.support_user,
supportToken: req.query._support_token,
supportPath: req.query.support_path,
} )
);
}
if ( ! config.isEnabled( 'wpcom-user-bootstrap' ) || ! req.cookies.wordpress_logged_in ) {
return res.send( renderJsx( 'support-user' ) );
}
// Maybe not logged in, note that you need docker to test this properly
debug( 'Issuing API call to fetch user object' );
getBootstrappedUser( req )
.then( ( data ) => {
const activeFlags = get( data, 'meta.data.flags.active_flags', [] );
// A8C check
if ( ! includes( activeFlags, 'calypso_support_user' ) ) {
return res.send( renderJsx( 'support-user' ) );
}
// Passed all checks, prepare support user session
res.send(
renderJsx( 'support-user', {
authorized: true,
supportUser: req.query.support_user,
supportToken: req.query._support_token,
supportPath: req.query.support_path,
} )
);
} )
.catch( () => {
res.clearCookie( 'wordpress_logged_in', {
path: '/',
httpOnly: true,
domain: '.wordpress.com',
} );
res.send( renderJsx( 'support-user' ) );
} );
} );
// catchall to render 404 for all routes not explicitly allowed in client/sections
app.use( render404() );
// Error handling middleware for displaying the server error 500 page must be the very last middleware defined
app.use( renderServerError() );
return app;
}