Skip to content

Commit

Permalink
stellar#153: throw error if invalid rpc json, added test to verify
Browse files Browse the repository at this point in the history
  • Loading branch information
sreuland committed Oct 11, 2023
1 parent 6cda5ba commit f582dcc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -765,11 +765,10 @@ function mergeResponseExpirationLedgers(ledgerEntriesResponse: SorobanRpc.RawGet
requestedKey.toXDR('base64')));

(ledgerEntriesResponse.entries ?? []).forEach(rawEntryResult => {
if (!rawEntryResult.key) {
// don't interpret raw ledger entry data here, just pass it through to the outgoing response as-is
expirationKeyToRawEntryResult.set(Math.random().toString(), rawEntryResult)
return;
if (!rawEntryResult.key || !rawEntryResult.xdr) {
throw new TypeError(`invalid ledger entry: ${JSON.stringify(rawEntryResult)}`);
}

const parsedKey = xdr.LedgerKey.fromXDR(rawEntryResult.key, 'base64');

if (parsedKey.switch().value !== xdr.LedgerEntryType.expiration().value ||
Expand Down
24 changes: 24 additions & 0 deletions test/unit/server/get_ledger_entries_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,28 @@ describe('Server#getLedgerEntries', function () {
})
.catch((err) => done(err));
});

it('throws when invalid rpc response', function (done) {
// these are simulating invalid json, missing `xdr` and `key`
mockRPC(
this.axiosMock,
[ledgerKeyXDR, ledgerExpirationKeyXDR],
[
{
lastModifiedLedgerSeq: 2
},
{
lastModifiedLedgerSeq: 1
}
]
);

this.server
.getLedgerEntries(ledgerKey)
.then((reply) => done(new Error(`should have failed, got: ${reply}`)))
.catch((error) => {
expect(error).to.contain(/invalid ledger entry/i);
done();
});
});
});

0 comments on commit f582dcc

Please sign in to comment.