Add limit protection for Bytes & Text (#2670)

* Add limit protection for Bytes & Text

* Limites as K

* Adjust limits upwards
This commit is contained in:
Jaco Greeff
2020-10-02 13:11:25 +02:00
committed by GitHub
parent ba5f9d1d01
commit 5577723b73
3 changed files with 8 additions and 1 deletions
+1 -1
View File
@@ -9,7 +9,7 @@ import Compact from './Compact';
import { decodeU8a, typeToConstructor } from './utils';
import AbstractArray from './AbstractArray';
const MAX_LENGTH = 32768;
const MAX_LENGTH = 64 * 1024;
/**
* @name Vec
+4
View File
@@ -8,6 +8,9 @@ import { assert, isString, isU8a, u8aToU8a } from '@polkadot/util';
import Compact from '../codec/Compact';
import Raw from '../codec/Raw';
// Bytes are used for things like on-chain code, so it has a healthy limit
const MAX_LENGTH = 10 * 1024 * 1024;
/** @internal */
function decodeBytesU8a (value: Uint8Array): Uint8Array {
if (!value.length) {
@@ -18,6 +21,7 @@ function decodeBytesU8a (value: Uint8Array): Uint8Array {
const [offset, length] = Compact.decodeU8a(value);
const total = offset + length.toNumber();
assert(length.lten(MAX_LENGTH), `Bytes length ${length.toString()} exceeds ${MAX_LENGTH}`);
assert(total <= value.length, `Bytes: required length less than remainder, expected at least ${total}, found ${value.length}`);
return value.subarray(offset, total);
+3
View File
@@ -9,6 +9,8 @@ import { assert, hexToU8a, isHex, isString, stringToU8a, u8aToString, u8aToHex }
import Compact from '../codec/Compact';
import Raw from '../codec/Raw';
const MAX_LENGTH = 128 * 1024;
/** @internal */
function decodeText (value: Text | string | AnyU8a | { toString: () => string }): string {
if (isHex(value)) {
@@ -27,6 +29,7 @@ function decodeText (value: Text | string | AnyU8a | { toString: () => string })
const [offset, length] = Compact.decodeU8a(value);
const total = offset + length.toNumber();
assert(length.lten(MAX_LENGTH), `Text length ${length.toString()} exceeds ${MAX_LENGTH}`);
assert(total <= value.length, `Text: required length less than remainder, expected at least ${total}, found ${value.length}`);
return u8aToString(value.subarray(offset, total));