Skip to content

Commit

Permalink
fix: protect GET /wallet/:id and GET /wallet/:id/trust_relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavkparti committed Apr 12, 2024
1 parent b05008b commit e053be2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
26 changes: 17 additions & 9 deletions server/handlers/walletHandler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,13 @@ const walletSingleGet = async (req, res) => {
abortEarly: false,
});

const { wallet_id } = validatedParams;
const { wallet_id: requestedWalletId } = validatedParams;
const { wallet_id: loggedInWalletId } = req;
const walletService = new WalletService();
const wallet = await walletService.getWallet(wallet_id);
const wallet = await walletService.getWallet(
loggedInWalletId,
requestedWalletId,
);
res.status(200).send(wallet);
};

Expand All @@ -74,15 +78,19 @@ const walletGetTrustRelationships = async (req, res) => {
},
);

const { wallet_id } = validatedParams;
const { wallet_id: walletId } = validatedParams;
const { wallet_id: loggedInWalletId } = req;
const { state, type, request_type } = validatedQuery;
const trustService = new TrustService();
const trust_relationships = await trustService.getTrustRelationships({
walletId: wallet_id,
state,
type,
request_type,
});
const trust_relationships = await trustService.getTrustRelationships(
loggedInWalletId,
{
walletId,
state,
type,
request_type,
},
);
res.status(200).json({
trust_relationships,
});
Expand Down
8 changes: 7 additions & 1 deletion server/models/Wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,14 @@ class Wallet {
return this._walletRepository.getById(id);
}

async getWallet(walletId) {
async getWallet(loggedInWalletId, walletId) {
const wallet = await this._walletRepository.getById(walletId);

// requested wallet is not managed by currently logged-in user
if (!(await this.hasControlOver(loggedInWalletId, walletId))) {
throw new HttpError(403, 'Have no permission to access this wallet');
}

const tokenCount = await this._tokenRepository.countByFilter({
wallet_id: walletId,
});
Expand Down
23 changes: 13 additions & 10 deletions server/services/TrustService.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,14 @@ class TrustService {
this._eventService = new EventService();
}

async getTrustRelationships({
walletId,
state,
type,
request_type,
offset,
limit,
}) {
async getTrustRelationships(
loggedInWalletId,
{ walletId, state, type, request_type, offset, limit },
) {
// check if wallet exists first
// throws error if no wallet matching walletId exists
const walletService = new WalletService();
await walletService.getWallet(walletId);
await walletService.getWallet(loggedInWalletId, walletId);

return this._trust.getTrustRelationships({
walletId,
Expand All @@ -34,7 +30,14 @@ class TrustService {
});
}

async getAllTrustRelationships({ walletId, state, type, request_type, offset, limit }) {
async getAllTrustRelationships({
walletId,
state,
type,
request_type,
offset,
limit,
}) {
return this._trust.getAllTrustRelationships({
walletId,
state,
Expand Down
4 changes: 2 additions & 2 deletions server/services/WalletService.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class WalletService {
return this._wallet.getByName(name);
}

async getWallet(walletId) {
return this._wallet.getWallet(walletId);
async getWallet(loggedInWalletId, walletId) {
return this._wallet.getWallet(loggedInWalletId, walletId);
}

async getByIdOrName(idOrName) {
Expand Down

0 comments on commit e053be2

Please sign in to comment.