-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathbuild-remote-server.js
More file actions
1813 lines (1624 loc) · 70.9 KB
/
build-remote-server.js
File metadata and controls
1813 lines (1624 loc) · 70.9 KB
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
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2022, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import path from 'path'
import cookie from 'cookie'
import {
BUILD,
CONTENT_TYPE,
X_MOBIFY_QUERYSTRING,
SET_COOKIE,
CACHE_CONTROL,
NO_CACHE,
X_ENCODED_HEADERS,
CONTENT_SECURITY_POLICY,
SLAS_TOKEN_RESPONSE_ENDPOINTS,
SLAS_ENDPOINTS_REQUIRING_ACCESS_TOKEN
} from './constants'
import {
catchAndLog,
getHashForString,
isRemote,
MetricsSender,
outgoingRequestHook,
parseRequestUrl,
processLambdaResponse,
responseSend,
configureProxyConfigs,
setQuiet,
localDevLog
} from '../../utils/ssr-server'
import dns from 'dns'
import express from 'express'
import {PersistentCache} from '../../utils/ssr-cache'
import merge from 'merge-descriptors'
import {Headers, X_HEADERS_TO_REMOVE_ORIGIN, X_MOBIFY_REQUEST_CLASS} from '../../utils/ssr-proxying'
import assert from 'assert'
import semver from 'semver'
import pkg from '../../../package.json'
import fs from 'fs'
import {RESOLVED_PROMISE} from './express'
import http from 'http'
import https from 'https'
import {proxyConfigs, updatePackageMobify} from '../../utils/ssr-shared'
import {
getEnvBasePath,
proxyBasePath,
bundleBasePath,
healthCheckPath,
slasPrivateProxyPath
} from '../../utils/ssr-namespace-paths'
import {applyProxyRequestHeaders} from '../../utils/ssr-server/configure-proxy'
import expressLogging from 'morgan'
import logger from '../../utils/logger-instance'
import {createProxyMiddleware, responseInterceptor} from 'http-proxy-middleware'
import {hybridProxy} from '../../utils/ssr-server/hybrid-proxy'
import {convertExpressRouteToRegex} from '../../utils/ssr-server/convert-express-route'
import {ServerlessAdapter} from '@h4ad/serverless-adapter'
import {DefaultHandler} from '@h4ad/serverless-adapter/lib/handlers/default'
import {CallbackResolver} from '@h4ad/serverless-adapter/lib/resolvers/callback'
import {ApiGatewayV1Adapter} from '@h4ad/serverless-adapter/lib/adapters/aws'
import {ExpressFramework} from '@h4ad/serverless-adapter/lib/frameworks/express'
import {is as typeis} from 'type-is'
import {getConfig} from '../../utils/ssr-config'
import {applyHttpOnlySessionCookies} from './process-token-response'
/**
* An Array of mime-types (Content-Type values) that are considered
* as binary by serverless-adapter when processing responses.
* We intentionally exclude all text/* values since we assume UTF8
* encoding and there's no reason to bulk up the response by base64
* encoding the result.
*
* We can use '*' in these types as a wildcard - see
* https://www.npmjs.com/package/type-is#type--typeisismediatype-types
* @private
*/
const binaryMimeTypes = ['application/*', 'audio/*', 'font/*', 'image/*', 'video/*']
export const isContentTypeBinary = (headers) => {
// Replicating the aws-serverless-express behavior
let contentType = headers['content-type'] || headers['Content-Type']
if (!contentType) {
return false
}
// Remove the encoding from the content type
contentType = contentType.split(';')[0]
return !!typeis(contentType, binaryMimeTypes)
}
export const isBinary = (headers) => {
return isContentTypeBinary(headers)
}
/**
* Environment variables that must be set for the Express app to run remotely.
*
* @private
*/
export const REMOTE_REQUIRED_ENV_VARS = [
'BUNDLE_ID',
'DEPLOY_TARGET',
'EXTERNAL_DOMAIN_NAME',
'MOBIFY_PROPERTY_ID'
]
const METRIC_DIMENSIONS = {
Project: process.env.MOBIFY_PROPERTY_ID,
Target: process.env.DEPLOY_TARGET
}
/**
* @private
*/
export const RemoteServerFactory = {
/**
* @private
*/
_configure(options) {
/**
* Not all of these options are documented. Some exist to allow for
* testing, or to handle non-standard projects.
*/
const defaults = {
// For test only – allow the project dir to be overridden.
projectDir: process.cwd(),
// Absolute path to the build directory
buildDir: path.resolve(process.cwd(), BUILD),
// The cache time for SSR'd pages (defaults to 600 seconds)
defaultCacheTimeSeconds: 600,
// The port that the local dev server listens on
port: 3443,
// The protocol that the local dev server listens on
protocol: 'https',
// Whether or not to use a keep alive agent for proxy connections.
proxyKeepAliveAgent: true,
// Quiet flag (suppresses output if true)
quiet: false,
// Suppress SSL checks - can be used for local dev server
// test code. Undocumented at present because there should
// be no use-case for SDK users to set this.
strictSSL: true,
mobify: undefined,
// Toggle cookies being passed and set
localAllowCookies: false,
// Toggle for setting up the custom SLAS private client secret handler
useSLASPrivateClient: false,
// A regex for identifying which SLAS endpoints the custom SLAS private
// client secret handler will inject an Authorization header.
// To allow additional SLAS endpoints, users can override this value in
// their project's ssr.js.
applySLASPrivateClientToEndpoints:
/\/oauth2\/(token|passwordless\/(login|token)|password\/(reset|action))/,
// A regex for identifying which SLAS endpoints return tokens (access_token, refresh_token)
// in the response body. Used to determine which responses should have HttpOnly session
// cookies applied when that feature is enabled. Users can override this in ssr.js.
tokenResponseEndpoints: SLAS_TOKEN_RESPONSE_ENDPOINTS,
// A regex for identifying which SLAS auth endpoints (/shopper/auth/) require the
// shopper's access token in the Authorization header (Bearer token from HttpOnly cookie).
// Most SLAS auth endpoints use Basic Auth with client credentials, but some like logout
// require the shopper's Bearer token. Users can override this in ssr.js.
slasEndpointsRequiringAccessToken: SLAS_ENDPOINTS_REQUIRING_ACCESS_TOKEN,
// Custom callback to modify the SLAS private client proxy request. This callback is invoked
// after the built-in proxy request handling. Users can provide additional
// request modifications (e.g., custom headers).
// Signature: (proxyRequest, incomingRequest, res) => void
onSLASPrivateProxyReq: undefined,
// Custom callback to modify the SLAS private client proxy response. This callback is invoked
// after the built-in proxy response handling (including HttpOnly session cookie handling when enabled).
// When HttpOnly session cookies are enabled (MRT_DISABLE_HTTPONLY_SESSION_COOKIES=false), the callback
// receives the response with tokens already moved to HttpOnly cookies and stripped from the body.
// Custom callbacks must not rely on token fields in the response body in that case; read from
// response headers (e.g. Set-Cookie) if needed.
// Signature: (responseBuffer, proxyRes, req, res) => Buffer
onSLASPrivateProxyRes: undefined
}
options = Object.assign({}, defaults, options)
setQuiet(options.quiet || process.env.SSR_QUIET)
// Set the protocol for the Express app listener - defaults to https on remote
options.protocol = this._getProtocol(options)
// Local dev server doesn't cache by default
options.defaultCacheControl = this._getDefaultCacheControl(options)
// Ensure this is a boolean, and is always true for a remote server.
options.strictSSL = this._strictSSL(options)
// This is the external HOSTNAME under which we are serving the page.
// The EXTERNAL_DOMAIN_NAME value technically only applies to remote
// operation, but we allow it to be used for a local dev server also.
options.appHostname = process.env.EXTERNAL_DOMAIN_NAME || `localhost:${options.port}`
options.devServerHostName = process.env.LISTEN_ADDRESS || `localhost:${options.port}`
// This is the ORIGIN under which we are serving the page.
// because it's an origin, it does not end with a slash.
options.appOrigin = process.env.APP_ORIGIN = `${options.protocol}://${options.appHostname}`
// Toggle cookies being passed and set. Can be overridden locally,
// always uses MRT_ALLOW_COOKIES env remotely
options.allowCookies = this._getAllowCookies(options)
// For test only – configure the SLAS private client secret proxy endpoint
options.slasHostName = this._getSlasEndpoint(options)
options.slasTarget = options.slasTarget || `https://${options.slasHostName}`
// Add extra condition to regex to only allow SLAS endpoints
options.slasApiPath = /\/shopper\/auth\/.*/
options.applySLASPrivateClientToEndpoints = new RegExp(
`${options.slasApiPath.source}(${options.applySLASPrivateClientToEndpoints.source})`
)
// Note: HttpOnly session cookies are controlled by the MRT_DISABLE_HTTPONLY_SESSION_COOKIES
// env var (set by MRT in production, pwa-kit-dev locally). Read directly where needed.
// Extract siteId from app configuration for SCAPI auth
// This will be used to read the correct access token cookie
try {
const config = getConfig({buildDirectory: options.buildDir})
options.siteId = config?.app?.commerceAPI?.parameters?.siteId || null
} catch (e) {
// Config may not be available yet (e.g., during build), that's okay
options.siteId = null
}
return options
},
/**
* @private
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_logStartupMessage(options) {
// Hook for the DevServer
},
/**
* @private
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_getAllowCookies(options) {
return 'MRT_ALLOW_COOKIES' in process.env ? process.env.MRT_ALLOW_COOKIES == 'true' : false
},
/**
* @private
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_getProtocol(options) {
return 'https'
},
/**
* @private
*/
_getDefaultCacheControl(options) {
return `max-age=${options.defaultCacheTimeSeconds}, s-maxage=${options.defaultCacheTimeSeconds}`
},
/**
* @private
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_strictSSL(options) {
return true
},
/**
* @private
*/
_isBundleOrProxyPath(url) {
return url.startsWith(proxyBasePath) || url.startsWith(bundleBasePath)
},
/**
* @private
*/
_getSlasEndpoint(options) {
if (!options.useSLASPrivateClient) return undefined
const shortCode = options.mobify?.app?.commerceAPI?.parameters?.shortCode
return `${shortCode}.api.commercecloud.salesforce.com`
},
/**
* @private
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_setCompression(app) {
// Let the CDN do it
},
/**
* @private
*/
_setupLogging(app) {
const morganLoggerFormat = function (tokens, req, res) {
const contentLength = tokens.res(req, res, 'content-length')
return [
`(${res.locals.requestId})`,
tokens.method(req, res),
tokens.url(req, res),
tokens.status(req, res),
tokens['response-time'](req, res),
'ms',
contentLength && `- ${contentLength}`
].join(' ')
}
// Morgan stream for logging status codes less than 400
app.use(
expressLogging(morganLoggerFormat, {
skip: function (req, res) {
return res.statusCode >= 400
},
stream: {
write: (message) => {
logger.info(message, {
namespace: 'httprequest'
})
}
}
})
)
// Morgan stream for logging status codes 400 and above
app.use(
expressLogging(morganLoggerFormat, {
skip: function (req, res) {
return res.statusCode < 400
},
stream: {
write: (message) => {
logger.error(message, {
namespace: 'httprequest'
})
}
}
})
)
},
/**
* Passing the correlation Id from MRT to locals
* @private
*/
_setRequestId(app) {
app.use((req, res, next) => {
const correlationId = req.headers['x-correlation-id']
const requestId = correlationId ? correlationId : req.headers['x-apigateway-event']
if (!requestId) {
logger.error('Both x-correlation-id and x-apigateway-event headers are missing', {
namespace: '_setRequestId'
})
next()
return
}
res.locals.requestId = requestId
next()
})
},
/**
* @private
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_setupMetricsFlushing(app) {
// Hook for the dev-server
},
/**
* @private
*/
_updatePackageMobify(options) {
updatePackageMobify(options.mobify)
},
/**
* @private
*/
_configureProxyConfigs(options) {
const siteId = options.siteId || null
configureProxyConfigs(options.appHostname, options.protocol, siteId)
},
/**
* @private
*/
_createApp(options) {
options = this._configure(options)
this._logStartupMessage(options)
// To gain a small speed increase in the event that this
// server needs to make a proxy request back to itself,
// we kick off a DNS lookup for the appHostname. We don't
// wait for it to complete, or care if it fails, so the
// callback is a no-op.
dns.lookup(options.appHostname, () => null)
this._validateConfiguration(options)
this._updatePackageMobify(options)
this._configureProxyConfigs(options)
const app = this._createExpressApp(options)
// Do this first – we want compression applied to
// everything when it's enabled at all.
this._setCompression(app)
this._setRequestId(app)
// this._addEventContext(app)
// We want to remove any base paths that have made it this far.
// Base paths are used to route requests to the correct server.
// If the request has reached the server, it is no longer needed.
// Note: We use envBasePath as the feature flag for this middleware
// If envBasePath is `/`, '', or undefined when the server starts, we don't need to
// initialize this middleware.
if (getEnvBasePath()) {
this._setupBasePathMiddleware(app)
}
// Ordering of the next two calls are vital - we don't
// want request-processors applied to development views.
this._addSDKInternalHandlers(app)
this._setupSSRRequestProcessorMiddleware(app)
this._setForwardedHeaders(app, options)
this._setupLogging(app)
this._setupMetricsFlushing(app)
this._setupHealthcheck(app)
this._setupProxying(app, options)
this._setupSlasPrivateClientProxy(app, options)
this._setupHybridProxy(app, options)
// Beyond this point, we know that this is not a proxy request
// and not a bundle request, so we can apply specific
// processing.
this._setupCommonMiddleware(app, options)
this._addStaticAssetServing(app)
this._addDevServerGarbageCollection(app)
return app
},
_setupHybridProxy(app, options) {
if (options.hybridProxy?.enabled) {
app.use(hybridProxy(options))
}
},
/**
* @private
*/
_createExpressApp(options) {
const app = express()
app.disable('x-powered-by')
const mixin = {
options,
// Forcing a GC is no longer necessary, and will be
// skipped by default (unless FORCE_GC env-var is set).
_collectGarbage() {
// Do global.gc in a separate 'then' handler so
// that all major variables are out of scope and
// eligible for garbage collection.
/* istanbul ignore next */
let gcTime = 0
/* istanbul ignore next */
if (global.gc) {
const start = Date.now()
global.gc()
gcTime = Date.now() - start
}
this.sendMetric('GCTime', gcTime, 'Milliseconds')
},
_requestMonitor: new RequestMonitor(),
metrics: MetricsSender.getSender(),
/**
* Send a metric with fixed dimensions. See MetricsSender.send for more details.
*
* @private
* @param name {String} metric name
* @param [value] {Number} metric value (defaults to 1)
* @param [unit] {String} defaults to 'Count'
* @param [dimensions] {Object} optional extra dimensions
*/
sendMetric(name, value = 1, unit = 'Count', dimensions) {
this.metrics.send([
{
name,
value,
timestamp: Date.now(),
unit,
dimensions: Object.assign({}, dimensions || {}, METRIC_DIMENSIONS)
}
])
},
get applicationCache() {
if (!this._applicationCache) {
this._applicationCache = new PersistentCache()
}
return this._applicationCache
}
}
merge(app, mixin)
return app
},
/**
* @private
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_addSDKInternalHandlers(app) {
// This method is used by the dev server, but is not needed here.
},
/**
* Set x-forward-* headers into locals, this is primarily used to facilitate react sdk hook `useOrigin`
* @private
*/
_setForwardedHeaders(app, options) {
app.use((req, res, next) => {
const xForwardedHost = req.headers?.['x-forwarded-host']
const xForwardedProto = req.headers?.['x-forwarded-proto']
if (xForwardedHost) {
// prettier-ignore
res.locals.xForwardedOrigin = `${xForwardedProto || options.protocol}://${xForwardedHost}`
}
next()
})
},
/**
* @private
*/
_setupBasePathMiddleware(app) {
// Cache the express route regexes to avoid re-calculating them on every request.
let expressRouteRegexes
/**
* Remove the base path from a path.
*
* If path is like '/basepath/something', this returns '/something'
* If path is exactly '/basepath', this returns '/'
* If path doesn't start with base path or if there is no base path defined,
* returns the unmodified path
*
* @param path {string} the path to remove the base path from
* @returns {string} the path with the base path removed
*/
const removeBasePathFromPath = (path) => {
const basePath = getEnvBasePath()
if (!basePath) {
return path
}
if (path.startsWith(basePath + '/')) {
return path.substring(basePath.length)
} else if (path === basePath) {
return '/'
}
return path
}
/**
* Initializes a cache of regexes that correspond to the registered express routes
*
* This specifically omits the generic wildcard from the express routes where we
* want to remove the base path from since it is mapped to the app render. This is
* because routes sent to the render are handled by React Router
*/
const initializeExpressRouteRegexes = () => {
const expressRoutes = app._router.stack
.filter((layer) => layer.route && layer.route.path && layer.route.path !== '*')
.map((layer) => layer.route.path)
expressRouteRegexes = expressRoutes.map((route) => convertExpressRouteToRegex(route))
}
/**
* Very early request processing.
*
* If the server receives a request containing the base path, remove it before allowing
* the request through to the other express endpoints
*
* We scope base path removal to /mobify routes and routes defined by the express app
* (For example /callback or /worker.js)
* This is to avoid affecting React Router routes where a site id or locale might be present
* that is equal to the base path.
*
* For example, if you have a base path of /us and a site id of /us we don't want
* to remove the /us from www.example.com/us/en-US/category/... as this route is handled by
* React Router and the PWA multisite implementation.
*
* @param req {express.req} the incoming request - modified in-place
* @private
*/
const removeBasePathMiddleware = (req, res, next) => {
const basePath = getEnvBasePath()
// Fast path: /mobify routes always get the base path removed
if (req.path.startsWith(`${basePath}/mobify`)) {
const cleanPath = removeBasePathFromPath(req.path)
const {search} = parseRequestUrl(req)
req.url = cleanPath + search
return next()
}
// For other routes, only proceed if path actually starts with base path
if (!req.path.startsWith(basePath)) {
return next()
}
// Now we know path starts with base path, so we can remove it
const cleanPath = removeBasePathFromPath(req.path)
// Initialize express route regexes if needed
// We do this here since we want to ensure that any express route defined
// after the app is created, such as routes defined in ssr.js, are included.
if (!expressRouteRegexes) {
initializeExpressRouteRegexes()
}
// Next we check if the clean path matches any existing express routes
// (like /callback or /worker.js)
const matchesExpressRoute = expressRouteRegexes.some((routeRegex) => {
try {
return routeRegex.test(cleanPath)
} catch (error) {
logger.warn(
`Invalid express route pattern: ${routeRegex}`,
/* istanbul ignore next */
{
namespace: 'removeBasePathMiddleware',
additionalProperties: {
error: error
}
}
)
return false
}
})
// Only update URL if our clean path matches an Express route
// This leaves React Router paths (like /en-US/category) unchanged
if (matchesExpressRoute) {
const {search} = parseRequestUrl(req)
req.url = cleanPath + search
}
next()
}
app.use(removeBasePathMiddleware)
},
/**
* @private
*/
_setupSSRRequestProcessorMiddleware(app) {
// Attach this middleware as early as possible. It does timing
// and applies some early processing that must occur before
// anything else.
/**
* Incoming request processing.
*
* For the local dev server, if there is a request processor, use it to
* process all non-proxy, non-bundle requests, in the same way that
* CloudFront will do for a deployed bundle.
*
* If there is an x-querystring header in the incoming request, use
* that as the definitive querystring.
*
* @param req {express.req} the incoming request - modified in-place
* @param res {express.res} the response object
* @private
*/
const processIncomingRequest = (req, res) => {
const options = req.app.options
// If the request is for a proxy or bundle path, do nothing
if (this._isBundleOrProxyPath(req.originalUrl)) {
return
}
// Apply the request processor
// `this` is bound to the calling context, usually RemoteServerFactory
const requestProcessor = this._getRequestProcessor(req)
let {search, query: originalQuerystring} = parseRequestUrl(req)
let updatedQuerystring = originalQuerystring
let updatedPath = req.path
// If there's an x-querystring header, use that as the definitive
// querystring. This header is used in production, not in local dev,
// but we always handle it here to allow for testing.
const xQueryString = req.headers[X_MOBIFY_QUERYSTRING]
if (xQueryString) {
updatedQuerystring = xQueryString
// Hide the header from any other code
delete req.headers[X_MOBIFY_QUERYSTRING]
}
if (requestProcessor) {
// Allow the processor to handle this request. Because this code
// runs only in the local development server, we intentionally do
// not swallow errors - we want them to happen and show up on the
// console because that's how developers can test the processor.
const headers = new Headers(req.headers, 'http')
const processed = requestProcessor.processRequest({
headers,
path: req.path,
querystring: updatedQuerystring,
getRequestClass: () => headers.getHeader(X_MOBIFY_REQUEST_CLASS),
setRequestClass: (value) => headers.setHeader(X_MOBIFY_REQUEST_CLASS, value),
// This matches the set of parameters passed in the
// Lambda@Edge context.
parameters: {
deployTarget: `${process.env.DEPLOY_TARGET || 'local'}`,
appHostname: options.appHostname,
proxyConfigs
}
})
// Aid debugging by checking the return value
assert(
processed && 'path' in processed && 'querystring' in processed,
'Expected processRequest to return an object with ' +
'"path" and "querystring" properties, ' +
`but got ${JSON.stringify(processed, null, 2)}`
)
// Update the request.
updatedQuerystring = processed.querystring
updatedPath = processed.path
if (headers.modified) {
req.headers = headers.toObject()
}
}
// Update the request.
if (updatedQuerystring !== originalQuerystring) {
// Update the search string to reflect the new querystring
search = updatedQuerystring ? `?${updatedQuerystring}` : ''
// Let Express re-parse the parameters
if (updatedQuerystring) {
const queryStringParser = req.app.set('query parser fn')
req.query = queryStringParser(updatedQuerystring)
} else {
req.query = {}
}
}
// This will update the request's URL with the new path
// and querystring.
req.url = updatedPath + search
// Get the request class and store it for general use. We
// must do this AFTER the request-processor, because that's
// what may set the request class.
res.locals.requestClass = req.headers[X_MOBIFY_REQUEST_CLASS]
}
const ssrRequestProcessorMiddleware = (req, res, next) => {
const locals = res.locals
locals.requestStart = Date.now()
locals.afterResponseCalled = false
locals.responseCaching = {}
locals.originalUrl = req.originalUrl
// Track this response
req.app._requestMonitor._responseStarted(res)
// If the path is /, we enforce that the only methods
// allowed are GET, HEAD or OPTIONS. This is a restriction
// imposed by API Gateway: we enforce it here so that the
// local dev server has the same behaviour.
if (req.path === '/' && !['GET', 'HEAD', 'OPTIONS'].includes(req.method)) {
res.sendStatus(405)
return
}
// Apply custom query parameter parsing.
processIncomingRequest(req, res)
const afterResponse = () => {
/* istanbul ignore else */
if (!locals.afterResponseCalled) {
locals.afterResponseCalled = true
// Emit timing unless the request is for a proxy
// or bundle path. We don't want to emit metrics
// for those requests. We test req.originalUrl
// because it is consistently available across
// different types of the 'req' object, and will
// always contain the original full path.
/* istanbul ignore else */
if (!this._isBundleOrProxyPath(req.originalUrl)) {
req.app.sendMetric(
'RequestTime',
Date.now() - locals.requestStart,
'Milliseconds'
)
// We count 4xx and 5xx as errors, everything else is
// a success. 404 is a special case.
let metricName = 'RequestSuccess'
if (res.statusCode === 404) {
metricName = 'RequestFailed404'
} else if (res.statusCode >= 400 && res.statusCode <= 499) {
metricName = 'RequestFailed400'
} else if (res.statusCode >= 500 && res.statusCode <= 599) {
metricName = 'RequestFailed500'
}
req.app.sendMetric(metricName)
}
}
}
// Attach event listeners to the Response (we need to attach
// both to handle all possible cases)
res.on('finish', afterResponse)
res.on('close', afterResponse)
// Strip out API Gateway headers from the incoming request. We
// do that now so that the rest of the code don't have to deal
// with these headers, which can be large and may be accidentally
// forwarded to other servers.
X_HEADERS_TO_REMOVE_ORIGIN.forEach((key) => {
delete req.headers[key]
})
// Hand off to the next middleware
next()
}
app.use(ssrRequestProcessorMiddleware)
},
/**
* @private
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_setupProxying(app, options) {
app.all(`${proxyBasePath}/*`, (_, res) => {
return res.status(501).json({
message:
'Environment proxies are not set: https://developer.salesforce.com/docs/commerce/pwa-kit-managed-runtime/guide/proxying-requests.html'
})
})
},
/**
* @private
*/
_handleMissingSlasPrivateEnvVar(app, slasPrivateProxyPath) {
app.use(slasPrivateProxyPath, (_, res) => {
return res.status(501).json({
message:
'Environment variable PWA_KIT_SLAS_CLIENT_SECRET not set: Please set this environment variable to proceed.'
})
})
},
/**
* @private
*/
_setupSlasPrivateClientProxy(app, options) {
if (!options.useSLASPrivateClient) {
return
}
// This is the full path to the SLAS trusted-system endpoint
// We want to throw an error if the regex defined options.applySLASPrivateClientToEndpoints
// matches this path as an early warning to developers that they should update their regex
// in ssr.js to exclude this path.
const trustedSystemPath = '/shopper/auth/v1/oauth2/trusted-system/token'
if (trustedSystemPath.match(options.applySLASPrivateClientToEndpoints)) {
throw new Error(
'It is not allowed to include /oauth2/trusted-system endpoints in `applySLASPrivateClientToEndpoints`'
)
}
localDevLog(`Proxying ${slasPrivateProxyPath} to ${options.slasTarget}`)
const clientId = options.mobify?.app?.commerceAPI?.parameters?.clientId
const clientSecret = process.env.PWA_KIT_SLAS_CLIENT_SECRET
if (!clientSecret) {
this._handleMissingSlasPrivateEnvVar(app, slasPrivateProxyPath)
return
}
const encodedSlasCredentials = Buffer.from(`${clientId}:${clientSecret}`).toString('base64')
app.use(
slasPrivateProxyPath,
(req, res, next) => {
// Check if the request should be blocked before it reaches the proxy
// We run this outside of the proxy middleware because modifying the response
// to send a 403 in the proxy causes issues with the response interceptor.
if (
!req.path?.match(options.slasApiPath) ||
req.path?.match(/\/oauth2\/trusted-system/)
) {
const message = `Request to ${req.path} is not allowed through the SLAS Private Client Proxy`
logger.error(message)
return res.status(403).json({
message: message
})
}
next()
},
createProxyMiddleware({
target: options.slasTarget,
changeOrigin: true,
// http-proxy-middleware uses the original incoming request path to determine
// both proxyRequest and incomingRequest paths.
// This cannot be modified by any express middleware
// So we need to use the built in pathRewrite to remove the base path
pathRewrite: (path) => {
const basePathRegexEntry = getEnvBasePath() ? `${getEnvBasePath()}?` : ''
const regex = new RegExp(`^${basePathRegexEntry}${slasPrivateProxyPath}`)
return path.replace(regex, '')
},
selfHandleResponse: true,
onProxyReq: (proxyRequest, incomingRequest, res) => {
applyProxyRequestHeaders({
proxyRequest,
incomingRequest,
proxyPath: slasPrivateProxyPath,
targetHost: options.slasHostName,
targetProtocol: 'https'
})
if (incomingRequest.path?.match(/\/oauth2\/trusted-agent\/token/)) {
// /oauth2/trusted-agent/token endpoint auth header comes from Account Manager
// so the SLAS private client is sent via this special header
proxyRequest.setHeader('_sfdc_client_auth', encodedSlasCredentials)
} else if (
incomingRequest.path?.match(options.applySLASPrivateClientToEndpoints)
) {
// We pattern match and add client secrets only to endpoints that
// match the regex specified by options.applySLASPrivateClientToEndpoints.
//
// Other SLAS endpoints, ie. SLAS authenticate (/oauth2/login) and
// SLAS logout (/oauth2/logout), use the Authorization header for a different
// purpose so we don't want to overwrite the header for those calls.
proxyRequest.setHeader('Authorization', `Basic ${encodedSlasCredentials}`)
} else if (
process.env.MRT_DISABLE_HTTPONLY_SESSION_COOKIES === 'false' &&
incomingRequest.path?.match(options.slasEndpointsRequiringAccessToken)
) {
// Inject tokens from HttpOnly cookies for endpoints like /oauth2/logout
const cookieHeader = incomingRequest.headers.cookie
if (cookieHeader) {
const cookies = cookie.parse(cookieHeader)
const siteId = options.mobify?.app?.commerceAPI?.parameters?.siteId
if (siteId) {
const site = siteId.trim()
// Inject Bearer token from access token cookie
const accessToken = cookies[`cc-at_${site}`]
if (accessToken) {
proxyRequest.setHeader('Authorization', `Bearer ${accessToken}`)
}
// Inject refresh_token into query string from HttpOnly cookie
// refresh_token ishouls required for /oauth2/logout
const refreshToken = cookies[`cc-nx_${site}`]