Skip to content

Commit 0a3fae7

Browse files
authored
react-scripts: v8.5.5 - Fix proxy to work with Russian test user (#351)
* Add router function to proxy config to work with Russian data localization * Omit test files from react-scripts package publish
1 parent c532e56 commit 0a3fae7

File tree

5 files changed

+120
-2
lines changed

5 files changed

+120
-2
lines changed

CHANGELOG-FRONTIER.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 8.5.5
2+
3+
- Fix proxy to work with Russian test user
4+
15
## 8.5.4
26

37
- Fix coalesceLocales to handle Windows/WSL paths correctly

packages/react-scripts/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@fs/react-scripts",
3-
"version": "8.5.4",
3+
"version": "8.5.5",
44
"upstreamVersion": "5.0.1",
55
"description": "Configuration and scripts for Create React App.",
66
"repository": {
@@ -30,7 +30,8 @@
3030
"template-typescript",
3131
"utils",
3232
"polyfills.js",
33-
"per-locale-loader"
33+
"per-locale-loader",
34+
"!**/*.test.*"
3435
],
3536
"bin": {
3637
"react-scripts": "./bin/react-scripts.js"
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Gets a router function for http-proxy-middleware that
3+
* checks the session in the auth header and modifies the target
4+
* based on the session ruleSet for data localization cases
5+
* (e.g. Russia: https://beta.familysearch.org -> https://beta-ru.familysearch.org).
6+
*/
7+
8+
module.exports = ({ proxyConfig, target }) =>
9+
function changeTargetBasedOnSessionRuleSet(req) {
10+
// only return a target string from this function if the target needs changing
11+
// otherwise the middleware logs an unnecessary "[HPM] Router new target:"
12+
if (proxyConfig.options?.target) {
13+
// Do not interfere with custom target
14+
return
15+
}
16+
// Check session and change target based on ruleSet for data localization cases (e.g. Russia)
17+
// WARNING: Do not directly parse the session in your app.
18+
// We have to do it here for localhost development
19+
// since the Lambda@Edge function will enforce a redirect which will end up dropping the auth header.
20+
const auth = req.get('Authorization')
21+
if (!auth) {
22+
return
23+
}
24+
// https://fhconfluence.churchofjesuschrist.org/display/Product/Session+ID+Format
25+
// example header value: "Bearer p0-Adeq12~_tmc.Qed~3_4ZuIY"
26+
const ruleSet = /bearer\s+\w{1}(?<ruleSet>\d+)/i.exec(auth)?.groups?.ruleSet
27+
let subdomainSuffix
28+
if (ruleSet === '1') {
29+
// Russian data localization
30+
subdomainSuffix = '-ru'
31+
console.log('Rewriting target for Russian data localization')
32+
}
33+
return subdomainSuffix && target.replace('.familysearch.org', `${subdomainSuffix}.familysearch.org`)
34+
};
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// eslint-disable-next-line strict
2+
const getDataLocalizationRouter = require("./getDataLocalizationRouter.js");
3+
4+
const DEFAULT_TARGET = 'https://beta.familysearch.org'
5+
6+
test('does not modify custom target', () => {
7+
const proxyConfig = {
8+
route: '/foo',
9+
options: {
10+
target: 'https://example.com'
11+
}
12+
}
13+
const fn = getDataLocalizationRouter({ proxyConfig, target: DEFAULT_TARGET })
14+
const newTarget = fn()
15+
expect(newTarget).toBeUndefined()
16+
})
17+
18+
test('does not modify target if no auth', () => {
19+
const proxyConfig = {
20+
route: '/foo',
21+
}
22+
const req = { get: () => undefined }
23+
const fn = getDataLocalizationRouter({ proxyConfig, target: DEFAULT_TARGET })
24+
const newTarget = fn(req)
25+
expect(newTarget).toBeUndefined()
26+
})
27+
28+
test('does not modify target for regular session', () => {
29+
const proxyConfig = {
30+
route: '/foo',
31+
}
32+
const req = { get: () => 'Bearer b0-87987987' }
33+
const fn = getDataLocalizationRouter({ proxyConfig, target: DEFAULT_TARGET })
34+
const newTarget = fn(req)
35+
expect(newTarget).toBeUndefined()
36+
})
37+
38+
test('does not modify target for session missing rule set', () => {
39+
const proxyConfig = {
40+
route: '/foo',
41+
}
42+
const req = { get: () => 'Bearer b-87987987' }
43+
const fn = getDataLocalizationRouter({ proxyConfig, target: DEFAULT_TARGET })
44+
const newTarget = fn(req)
45+
expect(newTarget).toBeUndefined()
46+
})
47+
48+
test('rewrites target for Russian session (beta)', () => {
49+
const proxyConfig = {
50+
route: '/foo',
51+
}
52+
const req = { get: () => 'bearer b1-87987987' }
53+
const fn = getDataLocalizationRouter({ proxyConfig, target: DEFAULT_TARGET })
54+
const newTarget = fn(req)
55+
expect(newTarget).toEqual('https://beta-ru.familysearch.org')
56+
})
57+
58+
test('rewrites target for Russian session (integration)', () => {
59+
const proxyConfig = {
60+
route: '/foo',
61+
}
62+
const req = { get: () => 'Bearer i1-87987987' }
63+
const fn = getDataLocalizationRouter({ proxyConfig, target: 'https://integration.familysearch.org' })
64+
const newTarget = fn(req)
65+
expect(newTarget).toEqual('https://integration-ru.familysearch.org')
66+
})
67+
68+
test('rewrites target for Russian session (production)', () => {
69+
const proxyConfig = {
70+
route: '/foo',
71+
}
72+
const req = { get: () => 'Bearer p1-87987987' }
73+
const fn = getDataLocalizationRouter({ proxyConfig, target: 'https://www.familysearch.org' })
74+
const newTarget = fn(req)
75+
expect(newTarget).toEqual('https://www-ru.familysearch.org')
76+
})
77+

packages/react-scripts/proxy/setupProxy.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const setProxies = (app, customProxies = []) => {
1414
const auth = require('@fs/auth-middleware')
1515
const resolver = require('./resolver')
1616
const proxyList = require('./proxies')
17+
const getDataLocalizationRouter = require('./getDataLocalizationRouter')
1718

1819
// middleware required for auth middleware
1920
app.use(metric())
@@ -35,6 +36,7 @@ const setProxies = (app, customProxies = []) => {
3536
changeOrigin: true,
3637
logLevel: 'debug',
3738
timeout: 5000,
39+
router: getDataLocalizationRouter({ proxyConfig, target }),
3840
...proxyConfig.options,
3941
}
4042

0 commit comments

Comments
 (0)