summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorErik Larkö <erik@mullvad.net>2017-06-07 21:12:21 +0200
committerErik Larkö <erik@mullvad.net>2017-06-07 21:12:21 +0200
commit0ad2ea764a206ff5e90719cba186684bb79802a5 (patch)
tree1fddcb373badb400309c68b83d784df330ad3a69
parent829a8cee89d35b1e485849fa3d2459fa45ab59db (diff)
parent6c6a53cbbcc9e8b223b2b298163917a441518bfc (diff)
downloadmullvadvpn-0ad2ea764a206ff5e90719cba186684bb79802a5.tar.xz
mullvadvpn-0ad2ea764a206ff5e90719cba186684bb79802a5.zip
Merge branch 'validate-ipc-requests'
-rw-r--r--app/lib/ipc-facade.js33
-rw-r--r--app/lib/jsonrpc-ws-ipc.js10
-rw-r--r--package.json3
3 files changed, 37 insertions, 9 deletions
diff --git a/app/lib/ipc-facade.js b/app/lib/ipc-facade.js
index f344c80d7b..57305e9cab 100644
--- a/app/lib/ipc-facade.js
+++ b/app/lib/ipc-facade.js
@@ -1,15 +1,23 @@
// @flow
-import JsonRpcWs from './jsonrpc-ws-ipc';
+import JsonRpcWs, { InvalidReply } from './jsonrpc-ws-ipc';
+import { object, string, number, arrayOf } from 'validated/schema';
+import { validate } from 'validated/json5';
export type AccountData = {paid_until: string};
export type AccountNumber = string;
export type Ip = string;
export type Location = {
- latlong: Array<Number>,
+ latlong: Array<number>,
country: string,
city: string,
};
+const LocationSchema = object({
+ latlong: arrayOf(number),
+ country: string,
+ city: string,
+});
+
export interface IpcFacade {
getAccountData(AccountNumber): Promise<AccountData>,
@@ -32,8 +40,11 @@ export class RealIpc implements IpcFacade {
getAccountData(accountNumber: AccountNumber): Promise<AccountData> {
return this._ipc.send('get_account_data', accountNumber)
.then(raw => {
- // TODO: Validate here
- return raw;
+ if (typeof raw === 'object' && raw && raw.paid_until) {
+ return raw;
+ } else {
+ throw new InvalidReply(raw);
+ }
});
}
@@ -56,16 +67,22 @@ export class RealIpc implements IpcFacade {
getIp(): Promise<Ip> {
return this._ipc.send('get_ip')
.then(raw => {
- // TODO: Validate here
- return raw;
+ if (typeof raw === 'string' && raw) {
+ return raw;
+ } else {
+ throw new InvalidReply(raw);
+ }
});
}
getLocation(): Promise<Location> {
return this._ipc.send('get_location')
.then(raw => {
- // TODO: Validate here
- return raw;
+ try {
+ return validate(LocationSchema, raw);
+ } catch (e) {
+ throw new InvalidReply(raw, e);
+ }
});
}
}
diff --git a/app/lib/jsonrpc-ws-ipc.js b/app/lib/jsonrpc-ws-ipc.js
index 3d028c5a23..b31e683635 100644
--- a/app/lib/jsonrpc-ws-ipc.js
+++ b/app/lib/jsonrpc-ws-ipc.js
@@ -45,6 +45,16 @@ export class TimeOutError extends Error {
}
}
+export class InvalidReply extends Error {
+ reply: mixed;
+
+ constructor(reply: mixed, msg: ?string) {
+ super(msg);
+ this.name = 'InvalidReply';
+ this.reply = reply;
+ }
+}
+
const DEFAULT_TIMEOUT_MILLIS = 750;
export default class Ipc {
diff --git a/package.json b/package.json
index 5df0203bfe..88b0a893cb 100644
--- a/package.json
+++ b/package.json
@@ -30,7 +30,8 @@
"redux-actions": "^2.0.1",
"redux-localstorage": "^0.4.1",
"redux-thunk": "^2.2.0",
- "uuid": "^3.0.1"
+ "uuid": "^3.0.1",
+ "validated": "^1.1.0"
},
"optionalDependencies": {
"nseventmonitor": "git+https://github.com/pronebird/NSEventMonitor.git"