Skip to content

Commit b971b89

Browse files
committed
Exp validation at FiresbaseServerApp init.
1 parent 25f264f commit b971b89

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

packages/app/src/errors.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ export const enum AppError {
3131
IDB_WRITE = 'idb-set',
3232
IDB_DELETE = 'idb-delete',
3333
FINALIZATION_REGISTRY_NOT_SUPPORTED = 'finalization-registry-not-supported',
34-
INVALID_SERVER_APP_ENVIRONMENT = 'invalid-server-app-environment'
34+
INVALID_SERVER_APP_ENVIRONMENT = 'invalid-server-app-environment',
35+
INVALID_SERVER_APP_TOKEN_FORMAT = 'invalid-server-app-token-format',
36+
SERVER_APP_TOKEN_EXPIRED = 'server-app-token-expired'
3537
}
3638

3739
const ERRORS: ErrorMap<AppError> = {
@@ -61,7 +63,11 @@ const ERRORS: ErrorMap<AppError> = {
6163
[AppError.FINALIZATION_REGISTRY_NOT_SUPPORTED]:
6264
'FirebaseServerApp deleteOnDeref field defined but the JS runtime does not support FinalizationRegistry.',
6365
[AppError.INVALID_SERVER_APP_ENVIRONMENT]:
64-
'FirebaseServerApp is not for use in browser environments.'
66+
'FirebaseServerApp is not for use in browser environments.',
67+
[AppError.INVALID_SERVER_APP_TOKEN_FORMAT]:
68+
'FirebaseServerApp {$tokenName} could not be parsed.',
69+
[AppError.SERVER_APP_TOKEN_EXPIRED]:
70+
'FirebaseServerApp {$tokenName} could not be parsed.'
6571
};
6672

6773
interface ErrorParams {
@@ -75,6 +81,8 @@ interface ErrorParams {
7581
[AppError.IDB_WRITE]: { originalErrorMessage?: string };
7682
[AppError.IDB_DELETE]: { originalErrorMessage?: string };
7783
[AppError.FINALIZATION_REGISTRY_NOT_SUPPORTED]: { appName?: string };
84+
[AppError.INVALID_SERVER_APP_TOKEN_FORMAT]: { tokenName: string };
85+
[AppError.SERVER_APP_TOKEN_EXPIRED]: { tokenName: string };
7886
}
7987

8088
export const ERROR_FACTORY = new ErrorFactory<AppError, ErrorParams>(

packages/app/src/firebaseServerApp.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,33 @@ import { ComponentContainer } from '@firebase/component';
2626
import { FirebaseAppImpl } from './firebaseApp';
2727
import { ERROR_FACTORY, AppError } from './errors';
2828
import { name as packageName, version } from '../package.json';
29+
import { base64Decode } from '@firebase/util';
30+
31+
// Parse the token and check to see if the `exp` claim is in the future.
32+
// Throws an error if the token or claim could not be parsed, or if `exp` is in the past.
33+
function validateTokenTTL(base64Token: string, tokenName: string): void {
34+
const secondPart = base64Decode(base64Token.split('.')[1]);
35+
if (secondPart === null) {
36+
throw ERROR_FACTORY.create(AppError.INVALID_SERVER_APP_TOKEN_FORMAT, {
37+
tokenName
38+
});
39+
}
40+
const expClaim = JSON.parse(secondPart).exp;
41+
if (expClaim === undefined) {
42+
throw ERROR_FACTORY.create(AppError.INVALID_SERVER_APP_TOKEN_FORMAT, {
43+
tokenName
44+
});
45+
}
46+
const exp = JSON.parse(secondPart).exp * 1000;
47+
const now = new Date().getTime();
48+
// const now = new Date(new Date().getDate() - 1).now()
49+
const diff = exp - now;
50+
if (diff <= 0) {
51+
throw ERROR_FACTORY.create(AppError.SERVER_APP_TOKEN_EXPIRED, {
52+
tokenName
53+
});
54+
}
55+
}
2956

3057
export class FirebaseServerAppImpl
3158
extends FirebaseAppImpl
@@ -67,6 +94,16 @@ export class FirebaseServerAppImpl
6794
...serverConfig
6895
};
6996

97+
// Validate the authIdtoken validation window.
98+
if (this._serverConfig.authIdToken) {
99+
validateTokenTTL(this._serverConfig.authIdToken, 'authIdToken');
100+
}
101+
102+
// Validate the appCheckToken validation window.
103+
if (this._serverConfig.appCheckToken) {
104+
validateTokenTTL(this._serverConfig.appCheckToken, 'appCheckToken');
105+
}
106+
70107
this._finalizationRegistry = null;
71108
if (typeof FinalizationRegistry !== 'undefined') {
72109
this._finalizationRegistry = new FinalizationRegistry(() => {

0 commit comments

Comments
 (0)