Skip to content

Commit ba2b0a9

Browse files
authored
fix: certificate in Apple Game Center auth adapter not validated; this fixes a security vulnerability in which authentication could be bypassed using a fake certificate; if you are using the Apple Gamer Center auth adapter it is your responsibility to keep its root certificate up-to-date and we advice you read the security advisory ([GHSA-rh9j-f5f8-rvgc](GHSA-rh9j-f5f8-rvgc))
1 parent a8aef82 commit ba2b0a9

File tree

4 files changed

+249
-38
lines changed

4 files changed

+249
-38
lines changed

release.config.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,20 @@ async function config() {
8383
['@semantic-release/git', {
8484
assets: [changelogFile, 'package.json', 'package-lock.json', 'npm-shrinkwrap.json'],
8585
}],
86+
['@semantic-release/github', {
87+
successComment: getReleaseComment(),
88+
labels: ['type:ci'],
89+
releasedLabels: ['state:released<%= nextRelease.channel ? `-\${nextRelease.channel}` : "" %>']
90+
}],
8691
[
8792
"@saithodev/semantic-release-backmerge",
8893
{
8994
"branches": [
9095
{ from: "beta", to: "alpha" },
9196
{ from: "release", to: "beta" },
92-
{ from: "release", to: "alpha" },
9397
]
9498
}
9599
],
96-
['@semantic-release/github', {
97-
successComment: getReleaseComment(),
98-
labels: ['type:ci'],
99-
releasedLabels: ['state:released<%= nextRelease.channel ? `-\${nextRelease.channel}` : "" %>']
100-
}],
101100
],
102101
};
103102

spec/AuthenticationAdapters.spec.js

Lines changed: 136 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,8 +1652,41 @@ describe('apple signin auth adapter', () => {
16521652

16531653
describe('Apple Game Center Auth adapter', () => {
16541654
const gcenter = require('../lib/Adapters/Auth/gcenter');
1655-
1655+
const fs = require('fs');
1656+
const testCert = fs.readFileSync(__dirname + '/support/cert/game_center.pem');
1657+
it('can load adapter', async () => {
1658+
const options = {
1659+
gcenter: {
1660+
rootCertificateUrl:
1661+
'https://cacerts.digicert.com/DigiCertTrustedG4CodeSigningRSA4096SHA3842021CA1.crt.pem',
1662+
},
1663+
};
1664+
const { adapter, appIds, providerOptions } = authenticationLoader.loadAuthAdapter(
1665+
'gcenter',
1666+
options
1667+
);
1668+
await adapter.validateAppId(
1669+
appIds,
1670+
{ publicKeyUrl: 'https://static.gc.apple.com/public-key/gc-prod-4.cer' },
1671+
providerOptions
1672+
);
1673+
});
16561674
it('validateAuthData should validate', async () => {
1675+
const options = {
1676+
gcenter: {
1677+
rootCertificateUrl:
1678+
'https://cacerts.digicert.com/DigiCertTrustedG4CodeSigningRSA4096SHA3842021CA1.crt.pem',
1679+
},
1680+
};
1681+
const { adapter, appIds, providerOptions } = authenticationLoader.loadAuthAdapter(
1682+
'gcenter',
1683+
options
1684+
);
1685+
await adapter.validateAppId(
1686+
appIds,
1687+
{ publicKeyUrl: 'https://static.gc.apple.com/public-key/gc-prod-4.cer' },
1688+
providerOptions
1689+
);
16571690
// real token is used
16581691
const authData = {
16591692
id: 'G:1965586982',
@@ -1664,29 +1697,49 @@ describe('Apple Game Center Auth adapter', () => {
16641697
salt: 'DzqqrQ==',
16651698
bundleId: 'cloud.xtralife.gamecenterauth',
16661699
};
1667-
1700+
gcenter.cache['https://static.gc.apple.com/public-key/gc-prod-4.cer'] = testCert;
16681701
await gcenter.validateAuthData(authData);
16691702
});
16701703

16711704
it('validateAuthData invalid signature id', async () => {
1705+
const { adapter, appIds, providerOptions } = authenticationLoader.loadAuthAdapter(
1706+
'gcenter',
1707+
{}
1708+
);
1709+
await adapter.validateAppId(
1710+
appIds,
1711+
{ publicKeyUrl: 'https://static.gc.apple.com/public-key/gc-prod-4.cer' },
1712+
providerOptions
1713+
);
16721714
const authData = {
16731715
id: 'G:1965586982',
1674-
publicKeyUrl: 'https://static.gc.apple.com/public-key/gc-prod-4.cer',
1716+
publicKeyUrl: 'https://static.gc.apple.com/public-key/gc-prod-6.cer',
16751717
timestamp: 1565257031287,
16761718
signature: '1234',
16771719
salt: 'DzqqrQ==',
1678-
bundleId: 'cloud.xtralife.gamecenterauth',
1720+
bundleId: 'com.example.com',
16791721
};
1680-
1681-
try {
1682-
await gcenter.validateAuthData(authData);
1683-
fail();
1684-
} catch (e) {
1685-
expect(e.message).toBe('Apple Game Center - invalid signature');
1686-
}
1722+
await expectAsync(gcenter.validateAuthData(authData)).toBeRejectedWith(
1723+
new Parse.Error(Parse.Error.SCRIPT_FAILED, 'Apple Game Center - invalid signature')
1724+
);
16871725
});
16881726

16891727
it('validateAuthData invalid public key http url', async () => {
1728+
const options = {
1729+
gcenter: {
1730+
rootCertificateUrl:
1731+
'https://cacerts.digicert.com/DigiCertTrustedG4CodeSigningRSA4096SHA3842021CA1.crt.pem',
1732+
},
1733+
};
1734+
const { adapter, appIds, providerOptions } = authenticationLoader.loadAuthAdapter(
1735+
'gcenter',
1736+
options
1737+
);
1738+
await adapter.validateAppId(
1739+
appIds,
1740+
{ publicKeyUrl: 'https://static.gc.apple.com/public-key/gc-prod-4.cer' },
1741+
providerOptions
1742+
);
16901743
const publicKeyUrls = [
16911744
'example.com',
16921745
'http://static.gc.apple.com/public-key/gc-prod-4.cer',
@@ -1714,6 +1767,78 @@ describe('Apple Game Center Auth adapter', () => {
17141767
)
17151768
);
17161769
});
1770+
1771+
it('should not validate Symantec Cert', async () => {
1772+
const options = {
1773+
gcenter: {
1774+
rootCertificateUrl:
1775+
'https://cacerts.digicert.com/DigiCertTrustedG4CodeSigningRSA4096SHA3842021CA1.crt.pem',
1776+
},
1777+
};
1778+
const { adapter, appIds, providerOptions } = authenticationLoader.loadAuthAdapter(
1779+
'gcenter',
1780+
options
1781+
);
1782+
await adapter.validateAppId(
1783+
appIds,
1784+
{ publicKeyUrl: 'https://static.gc.apple.com/public-key/gc-prod-4.cer' },
1785+
providerOptions
1786+
);
1787+
expect(() =>
1788+
gcenter.verifyPublicKeyIssuer(
1789+
testCert,
1790+
'https://static.gc.apple.com/public-key/gc-prod-4.cer'
1791+
)
1792+
);
1793+
});
1794+
1795+
it('adapter should load default cert', async () => {
1796+
const options = {
1797+
gcenter: {},
1798+
};
1799+
const { adapter, appIds, providerOptions } = authenticationLoader.loadAuthAdapter(
1800+
'gcenter',
1801+
options
1802+
);
1803+
await adapter.validateAppId(
1804+
appIds,
1805+
{ publicKeyUrl: 'https://static.gc.apple.com/public-key/gc-prod-4.cer' },
1806+
providerOptions
1807+
);
1808+
const previous = new Date();
1809+
await adapter.validateAppId(
1810+
appIds,
1811+
{ publicKeyUrl: 'https://static.gc.apple.com/public-key/gc-prod-4.cer' },
1812+
providerOptions
1813+
);
1814+
1815+
const duration = new Date().getTime() - previous.getTime();
1816+
expect(duration).toEqual(0);
1817+
});
1818+
1819+
it('adapter should throw', async () => {
1820+
const options = {
1821+
gcenter: {
1822+
rootCertificateUrl: 'https://example.com',
1823+
},
1824+
};
1825+
const { adapter, appIds, providerOptions } = authenticationLoader.loadAuthAdapter(
1826+
'gcenter',
1827+
options
1828+
);
1829+
await expectAsync(
1830+
adapter.validateAppId(
1831+
appIds,
1832+
{ publicKeyUrl: 'https://static.gc.apple.com/public-key/gc-prod-4.cer' },
1833+
providerOptions
1834+
)
1835+
).toBeRejectedWith(
1836+
new Parse.Error(
1837+
Parse.Error.OBJECT_NOT_FOUND,
1838+
'Apple Game Center auth adapter parameter `rootCertificateURL` is invalid.'
1839+
)
1840+
);
1841+
});
17171842
});
17181843

17191844
describe('phant auth adapter', () => {

spec/support/cert/game_center.pem

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIEvDCCA6SgAwIBAgIQXRHxNXkw1L9z5/3EZ/T/hDANBgkqhkiG9w0BAQsFADB/
3+
MQswCQYDVQQGEwJVUzEdMBsGA1UEChMUU3ltYW50ZWMgQ29ycG9yYXRpb24xHzAd
4+
BgNVBAsTFlN5bWFudGVjIFRydXN0IE5ldHdvcmsxMDAuBgNVBAMTJ1N5bWFudGVj
5+
IENsYXNzIDMgU0hBMjU2IENvZGUgU2lnbmluZyBDQTAeFw0xODA5MTcwMDAwMDBa
6+
F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LmNydDANBgkqhkiG9w0BAQsFAAOCAQEA
22+
I/j/PcCNPebSAGrcqSFBSa2mmbusOX01eVBg8X0G/z8Z+ZWUfGFzDG0GQf89MPxV
23+
woec+nZuqui7o9Bg8s8JbHV0TC52X14CbTj9w/qBF748WbH9gAaTkrJYPm+MlNhu
24+
tjEuQdNl/YXVMvQW4O8UMHTi09GyJQ0NC4q92Wxvx1m/qzjvTLvrXHGQ9pEHhPyz
25+
vfBLxQkWpNoCNKU7UeESyH06XOrGc9MsII9deeKsDJp9a0jtx+pP4MFVtFME9SSQ
26+
tMBs0It7WwEf7qcRLpialxKwY2EzQ9g4WnANHqo18PrDBE10TFpZPzUh7JhMViVr
27+
EEbl0YdElmF8Hlamah/yNw==
28+
-----END CERTIFICATE-----

src/Adapters/Auth/gcenter.js

Lines changed: 80 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ const authData = {
1414
const { Parse } = require('parse/node');
1515
const crypto = require('crypto');
1616
const https = require('https');
17-
17+
const { pki } = require('node-forge');
18+
const ca = { cert: null, url: null };
1819
const cache = {}; // (publicKey -> cert) cache
1920

2021
function verifyPublicKeyUrl(publicKeyUrl) {
@@ -52,39 +53,53 @@ async function getAppleCertificate(publicKeyUrl) {
5253
path: url.pathname,
5354
method: 'HEAD',
5455
};
55-
const headers = await new Promise((resolve, reject) =>
56+
const cert_headers = await new Promise((resolve, reject) =>
5657
https.get(headOptions, res => resolve(res.headers)).on('error', reject)
5758
);
59+
const validContentTypes = ['application/x-x509-ca-cert', 'application/pkix-cert'];
5860
if (
59-
headers['content-type'] !== 'application/pkix-cert' ||
60-
headers['content-length'] == null ||
61-
headers['content-length'] > 10000
61+
!validContentTypes.includes(cert_headers['content-type']) ||
62+
cert_headers['content-length'] == null ||
63+
cert_headers['content-length'] > 10000
6264
) {
6365
throw new Parse.Error(
6466
Parse.Error.OBJECT_NOT_FOUND,
6567
`Apple Game Center - invalid publicKeyUrl: ${publicKeyUrl}`
6668
);
6769
}
70+
const { certificate, headers } = await getCertificate(publicKeyUrl);
71+
if (headers['cache-control']) {
72+
const expire = headers['cache-control'].match(/max-age=([0-9]+)/);
73+
if (expire) {
74+
cache[publicKeyUrl] = certificate;
75+
// we'll expire the cache entry later, as per max-age
76+
setTimeout(() => {
77+
delete cache[publicKeyUrl];
78+
}, parseInt(expire[1], 10) * 1000);
79+
}
80+
}
81+
return verifyPublicKeyIssuer(certificate, publicKeyUrl);
82+
}
83+
84+
function getCertificate(url, buffer) {
6885
return new Promise((resolve, reject) => {
6986
https
70-
.get(publicKeyUrl, res => {
71-
let data = '';
87+
.get(url, res => {
88+
const data = [];
7289
res.on('data', chunk => {
73-
data += chunk.toString('base64');
90+
data.push(chunk);
7491
});
7592
res.on('end', () => {
76-
const cert = convertX509CertToPEM(data);
77-
if (res.headers['cache-control']) {
78-
var expire = res.headers['cache-control'].match(/max-age=([0-9]+)/);
79-
if (expire) {
80-
cache[publicKeyUrl] = cert;
81-
// we'll expire the cache entry later, as per max-age
82-
setTimeout(() => {
83-
delete cache[publicKeyUrl];
84-
}, parseInt(expire[1], 10) * 1000);
85-
}
93+
if (buffer) {
94+
resolve({ certificate: Buffer.concat(data), headers: res.headers });
95+
return;
8696
}
87-
resolve(cert);
97+
let cert = '';
98+
for (const chunk of data) {
99+
cert += chunk.toString('base64');
100+
}
101+
const certificate = convertX509CertToPEM(cert);
102+
resolve({ certificate, headers: res.headers });
88103
});
89104
})
90105
.on('error', reject);
@@ -115,6 +130,30 @@ function verifySignature(publicKey, authData) {
115130
}
116131
}
117132

133+
function verifyPublicKeyIssuer(cert, publicKeyUrl) {
134+
const publicKeyCert = pki.certificateFromPem(cert);
135+
if (!ca.cert) {
136+
throw new Parse.Error(
137+
Parse.Error.OBJECT_NOT_FOUND,
138+
'Apple Game Center auth adapter parameter `rootCertificateURL` is invalid.'
139+
);
140+
}
141+
try {
142+
if (!ca.cert.verify(publicKeyCert)) {
143+
throw new Parse.Error(
144+
Parse.Error.OBJECT_NOT_FOUND,
145+
`Apple Game Center - invalid publicKeyUrl: ${publicKeyUrl}`
146+
);
147+
}
148+
} catch (e) {
149+
throw new Parse.Error(
150+
Parse.Error.OBJECT_NOT_FOUND,
151+
`Apple Game Center - invalid publicKeyUrl: ${publicKeyUrl}`
152+
);
153+
}
154+
return cert;
155+
}
156+
118157
// Returns a promise that fulfills if this user id is valid.
119158
async function validateAuthData(authData) {
120159
if (!authData.id) {
@@ -126,11 +165,31 @@ async function validateAuthData(authData) {
126165
}
127166

128167
// Returns a promise that fulfills if this app id is valid.
129-
function validateAppId() {
130-
return Promise.resolve();
168+
async function validateAppId(appIds, authData, options = {}) {
169+
if (!options.rootCertificateUrl) {
170+
options.rootCertificateUrl =
171+
'https://cacerts.digicert.com/DigiCertTrustedG4CodeSigningRSA4096SHA3842021CA1.crt.pem';
172+
}
173+
if (ca.url === options.rootCertificateUrl) {
174+
return;
175+
}
176+
const { certificate, headers } = await getCertificate(options.rootCertificateUrl, true);
177+
if (
178+
headers['content-type'] !== 'application/x-pem-file' ||
179+
headers['content-length'] == null ||
180+
headers['content-length'] > 10000
181+
) {
182+
throw new Parse.Error(
183+
Parse.Error.OBJECT_NOT_FOUND,
184+
'Apple Game Center auth adapter parameter `rootCertificateURL` is invalid.'
185+
);
186+
}
187+
ca.cert = pki.certificateFromPem(certificate);
188+
ca.url = options.rootCertificateUrl;
131189
}
132190

133191
module.exports = {
134192
validateAppId,
135193
validateAuthData,
194+
cache,
136195
};

0 commit comments

Comments
 (0)