Skip to content

Commit 64da840

Browse files
authored
fix: Google Play Games auth adapter accepts a player ID that the access token does not belong to ([GHSA-r66f-3q22-grc4](GHSA-r66f-3q22-grc4)) (#10878)
1 parent 3e54e5c commit 64da840

3 files changed

Lines changed: 161 additions & 19 deletions

File tree

‎spec/Adapters/Auth/gpgames.spec.js‎

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ describe('GooglePlayGamesServicesAdapter', function () {
8181
it('should fetch user data successfully', async function () {
8282
mockFetch([
8383
{
84-
url: 'https://www.googleapis.com/games/v1/players/mockUserId',
84+
url: 'https://www.googleapis.com/games/v1/players/me',
8585
method: 'GET',
8686
response: {
8787
ok: true,
@@ -103,7 +103,7 @@ describe('GooglePlayGamesServicesAdapter', function () {
103103
it('should throw an error if the response is not ok', async function () {
104104
mockFetch([
105105
{
106-
url: 'https://www.googleapis.com/games/v1/players/mockUserId',
106+
url: 'https://www.googleapis.com/games/v1/players/me',
107107
method: 'GET',
108108
response: {
109109
ok: false,
@@ -123,7 +123,7 @@ describe('GooglePlayGamesServicesAdapter', function () {
123123
it('should throw an error if user data is invalid', async function () {
124124
mockFetch([
125125
{
126-
url: 'https://www.googleapis.com/games/v1/players/mockUserId',
126+
url: 'https://www.googleapis.com/games/v1/players/me',
127127
method: 'GET',
128128
response: {
129129
ok: true,
@@ -140,10 +140,10 @@ describe('GooglePlayGamesServicesAdapter', function () {
140140
);
141141
});
142142

143-
it('should throw an error if playerId does not match the provided user ID', async function () {
143+
it('should return the player who owns the access token instead of the provided user ID', async function () {
144144
mockFetch([
145145
{
146-
url: 'https://www.googleapis.com/games/v1/players/mockUserId',
146+
url: 'https://www.googleapis.com/games/v1/players/me',
147147
method: 'GET',
148148
response: {
149149
ok: true,
@@ -157,10 +157,9 @@ describe('GooglePlayGamesServicesAdapter', function () {
157157

158158
const accessToken = 'validAccessToken';
159159
const authData = { id: 'mockUserId' };
160+
const user = await adapter.getUserFromAccessToken(accessToken, authData);
160161

161-
await expectAsync(adapter.getUserFromAccessToken(accessToken, authData)).toBeRejectedWithError(
162-
'Invalid Google Play Games Services user data received.'
163-
);
162+
expect(user).toEqual({ id: 'anotherUserId' });
164163
});
165164
});
166165

@@ -190,7 +189,7 @@ describe('GooglePlayGamesServicesAdapter', function () {
190189
},
191190
},
192191
{
193-
url: 'https://www.googleapis.com/games/v1/players/mockUserId',
192+
url: 'https://www.googleapis.com/games/v1/players/me',
194193
method: 'GET',
195194
response: {
196195
ok: true,
@@ -216,7 +215,7 @@ describe('GooglePlayGamesServicesAdapter', function () {
216215
jasmine.any(Object)
217216
);
218217
expect(global.fetch).toHaveBeenCalledWith(
219-
'https://www.googleapis.com/games/v1/players/mockUserId',
218+
'https://www.googleapis.com/games/v1/players/me',
220219
jasmine.any(Object)
221220
);
222221
});
@@ -262,7 +261,7 @@ describe('GooglePlayGamesServicesAdapter', function () {
262261
},
263262
},
264263
{
265-
url: 'https://www.googleapis.com/games/v1/players/mockUserId',
264+
url: 'https://www.googleapis.com/games/v1/players/me',
266265
method: 'GET',
267266
response: {
268267
ok: false,
@@ -286,12 +285,12 @@ describe('GooglePlayGamesServicesAdapter', function () {
286285
jasmine.any(Object)
287286
);
288287
expect(global.fetch).toHaveBeenCalledWith(
289-
'https://www.googleapis.com/games/v1/players/mockUserId',
288+
'https://www.googleapis.com/games/v1/players/me',
290289
jasmine.any(Object)
291290
);
292291
});
293292

294-
it('should handle error when user data is invalid', async function () {
293+
it('should reject a user ID that the access token does not belong to', async function () {
295294
mockFetch([
296295
{
297296
url: 'https://oauth2.googleapis.com/token',
@@ -305,7 +304,7 @@ describe('GooglePlayGamesServicesAdapter', function () {
305304
},
306305
},
307306
{
308-
url: 'https://www.googleapis.com/games/v1/players/mockUserId',
307+
url: 'https://www.googleapis.com/games/v1/players/me',
309308
method: 'GET',
310309
response: {
311310
ok: true,
@@ -324,15 +323,15 @@ describe('GooglePlayGamesServicesAdapter', function () {
324323
};
325324

326325
await expectAsync(Parse.User.logInWith('gpgames', { authData })).toBeRejectedWithError(
327-
'Invalid Google Play Games Services user data received.'
326+
'gpgames auth is invalid for this user.'
328327
);
329328

330329
expect(global.fetch).toHaveBeenCalledWith(
331330
'https://oauth2.googleapis.com/token',
332331
jasmine.any(Object)
333332
);
334333
expect(global.fetch).toHaveBeenCalledWith(
335-
'https://www.googleapis.com/games/v1/players/mockUserId',
334+
'https://www.googleapis.com/games/v1/players/me',
336335
jasmine.any(Object)
337336
);
338337
});

‎spec/vulnerabilities.spec.js‎

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13129,4 +13129,146 @@ describe('Vulnerabilities', () => {
1312913129
expect(object.get('settings')).toEqual({ ...stored().settings, a: true });
1313013130
});
1313113131
});
13132+
13133+
describe('(GHSA-r66f-3q22-grc4) Google Play Games auth adapter accepts a player id that the access token does not belong to', () => {
13134+
const tokenUrl = 'https://oauth2.googleapis.com/token';
13135+
const playersUrl = 'https://www.googleapis.com/games/v1/players';
13136+
const victimPlayerId = 'victim-player';
13137+
const attackerPlayerId = 'attacker-player';
13138+
const unlinkedPlayerId = 'unlinked-player';
13139+
const headers = {
13140+
'Content-Type': 'application/json',
13141+
'X-Parse-Application-Id': 'test',
13142+
'X-Parse-REST-API-Key': 'rest',
13143+
};
13144+
13145+
// Stands in for Google: the credential belongs to `owner`; `players/me` returns the token
13146+
// owner and `players/<id>` returns the requested player, as Google documents.
13147+
const mockGoogle = owner =>
13148+
mockFetch([
13149+
{
13150+
url: tokenUrl,
13151+
method: 'POST',
13152+
response: { ok: true, json: () => Promise.resolve({ access_token: 'google-token' }) },
13153+
},
13154+
...['me', victimPlayerId, attackerPlayerId, unlinkedPlayerId].map(id => ({
13155+
url: `${playersUrl}/${id}`,
13156+
method: 'GET',
13157+
response: {
13158+
ok: true,
13159+
json: () => Promise.resolve({ playerId: id === 'me' ? owner : id }),
13160+
},
13161+
})),
13162+
]);
13163+
13164+
const logInWith = authData =>
13165+
request({
13166+
method: 'POST',
13167+
url: 'http://localhost:8378/1/users',
13168+
headers,
13169+
body: JSON.stringify({ authData: { gpgames: authData } }),
13170+
}).catch(e => e);
13171+
13172+
const linkWith = (user, authData) =>
13173+
request({
13174+
method: 'PUT',
13175+
url: `http://localhost:8378/1/users/${user.id}`,
13176+
headers: { ...headers, 'X-Parse-Session-Token': user.getSessionToken() },
13177+
body: JSON.stringify({ authData: { gpgames: authData } }),
13178+
}).catch(e => e);
13179+
13180+
// Reads the row directly so the adapter's afterFind cannot mask what was persisted.
13181+
const storedProviderData = async objectId => {
13182+
const [row] = await Config.get('test').database.find('_User', { objectId });
13183+
return row.authData?.gpgames;
13184+
};
13185+
13186+
let victim;
13187+
13188+
beforeEach(async () => {
13189+
await reconfigureServer({
13190+
auth: { gpgames: { clientId: 'clientId', clientSecret: 'clientSecret' } },
13191+
});
13192+
mockGoogle(victimPlayerId);
13193+
victim = await Parse.User.logInWith('gpgames', {
13194+
authData: { code: 'victim-code', id: victimPlayerId },
13195+
});
13196+
});
13197+
13198+
it('rejects an authorization code with the id of another player', async () => {
13199+
mockGoogle(attackerPlayerId);
13200+
13201+
const response = await logInWith({ code: 'attacker-code', id: victimPlayerId });
13202+
13203+
const body = JSON.parse(response.text);
13204+
expect(body.code).toBe(Parse.Error.OBJECT_NOT_FOUND);
13205+
expect(body.sessionToken).toBeUndefined();
13206+
});
13207+
13208+
it('rejects an access token with the id of another player when insecure auth is enabled', async () => {
13209+
await reconfigureServer({ auth: { gpgames: { enableInsecureAuth: true } } });
13210+
mockGoogle(attackerPlayerId);
13211+
13212+
const response = await logInWith({ access_token: 'attacker-token', id: victimPlayerId });
13213+
13214+
const body = JSON.parse(response.text);
13215+
expect(body.code).toBe(Parse.Error.OBJECT_NOT_FOUND);
13216+
expect(body.sessionToken).toBeUndefined();
13217+
});
13218+
13219+
it('logs in the player who owns the access token when insecure auth is enabled', async () => {
13220+
await reconfigureServer({ auth: { gpgames: { enableInsecureAuth: true } } });
13221+
mockGoogle(victimPlayerId);
13222+
13223+
const response = await logInWith({ access_token: 'victim-token', id: victimPlayerId });
13224+
13225+
expect(response.status).toBe(200);
13226+
expect(response.data.objectId).toBe(victim.id);
13227+
});
13228+
13229+
it('rejects linking the id of another player to an account', async () => {
13230+
const attacker = new Parse.User();
13231+
await attacker.signUp({ username: 'attacker', password: 'attacker-password' });
13232+
mockGoogle(attackerPlayerId);
13233+
13234+
const response = await linkWith(attacker, { code: 'attacker-code', id: unlinkedPlayerId });
13235+
13236+
const body = JSON.parse(response.text);
13237+
expect(body.code).toBe(Parse.Error.SESSION_MISSING);
13238+
expect(global.fetch.calls.allArgs().map(([url]) => url)).toEqual([
13239+
tokenUrl,
13240+
`${playersUrl}/me`,
13241+
]);
13242+
expect(await storedProviderData(attacker.id)).toBeUndefined();
13243+
});
13244+
13245+
it('links the player who owns the authorization code to an account', async () => {
13246+
const user = new Parse.User();
13247+
await user.signUp({ username: 'player', password: 'player-password' });
13248+
mockGoogle(unlinkedPlayerId);
13249+
13250+
const response = await linkWith(user, { code: 'player-code', id: unlinkedPlayerId });
13251+
13252+
expect(response.status).toBe(200);
13253+
expect((await storedProviderData(user.id)).id).toBe(unlinkedPlayerId);
13254+
});
13255+
13256+
it('logs in the player who owns the authorization code', async () => {
13257+
mockGoogle(victimPlayerId);
13258+
13259+
const response = await logInWith({ code: 'victim-code', id: victimPlayerId });
13260+
13261+
expect(response.status).toBe(200);
13262+
expect(response.data.objectId).toBe(victim.id);
13263+
});
13264+
13265+
it('resolves the player from the authorization code when no id is supplied', async () => {
13266+
mockGoogle(victimPlayerId);
13267+
13268+
const response = await logInWith({ code: 'victim-code' });
13269+
13270+
expect(response.status).toBe(200);
13271+
expect(response.data.objectId).toBe(victim.id);
13272+
});
13273+
});
1313213274
});

‎src/Adapters/Auth/gpgames.js‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,9 @@ class GooglePlayGamesServicesAdapter extends BaseCodeAuthAdapter {
104104
return data.access_token;
105105
}
106106

107-
async getUserFromAccessToken(accessToken, authData) {
108-
const userApiUrl = `https://www.googleapis.com/games/v1/players/${authData.id}`;
107+
async getUserFromAccessToken(accessToken) {
108+
// `me` resolves to the player who owns the access token
109+
const userApiUrl = 'https://www.googleapis.com/games/v1/players/me';
109110
const response = await fetch(userApiUrl, {
110111
method: 'GET',
111112
headers: {
@@ -122,7 +123,7 @@ class GooglePlayGamesServicesAdapter extends BaseCodeAuthAdapter {
122123
}
123124

124125
const userData = await response.json();
125-
if (!userData.playerId || userData.playerId !== authData.id) {
126+
if (!userData.playerId) {
126127
throw new Parse.Error(
127128
Parse.Error.VALIDATION_ERROR,
128129
'Invalid Google Play Games Services user data received.'

0 commit comments

Comments
 (0)