Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use std::str::FromStr;
use errstr;
use Error;

use MAX_RECURSION_DEPTH;

#[derive(Debug)]
/// A token of the form `x(...)` or `x`
pub struct Tree<'a> {
Expand All @@ -34,7 +36,14 @@ pub trait FromTree: Sized {
}

impl<'a> Tree<'a> {
fn from_slice(mut sl: &'a str) -> Result<(Tree<'a>, &'a str), Error> {
fn from_slice(sl: &'a str) -> Result<(Tree<'a>, &'a str), Error> {
Self::from_slice_helper(sl, 0u32)
}

fn from_slice_helper(mut sl: &'a str, depth: u32) -> Result<(Tree<'a>, &'a str), Error> {
if depth >= MAX_RECURSION_DEPTH {
return Err(Error::MaxRecursiveDepthExceeded);
}
enum Found {
Nothing,
Lparen(usize),
Expand Down Expand Up @@ -87,7 +96,7 @@ impl<'a> Tree<'a> {

sl = &sl[n + 1..];
loop {
let (arg, new_sl) = Tree::from_slice(sl)?;
let (arg, new_sl) = Tree::from_slice_helper(sl, depth + 1)?;
ret.args.push(arg);

if new_sl.is_empty() {
Expand Down
19 changes: 19 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,10 @@ pub enum Error {
///Incorrect Script pubkey Hash for the descriptor. This is used for both
/// `Sh` and `Wsh` descriptors
IncorrectScriptHash,
/// Recursion depth exceeded when parsing policy/miniscript from string
MaxRecursiveDepthExceeded,
/// Recursion depth exceeded when parsing policy/miniscript from string
ScriptSizeTooLarge,
}

#[doc(hidden)]
Expand Down Expand Up @@ -361,6 +365,11 @@ impl error::Error for Error {
}
}

// https://github.com/sipa/miniscript/pull/5 for discussion on this number
const MAX_RECURSION_DEPTH: u32 = 402;
// https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki
const MAX_SCRIPT_SIZE: u32 = 10000;

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Expand Down Expand Up @@ -413,6 +422,16 @@ impl fmt::Display for Error {
Error::IncorrectPubkeyHash => {
f.write_str("Incorrect pubkey hash for given descriptor pkh/wpkh")
}
Error::MaxRecursiveDepthExceeded => write!(
f,
"Recusive depth over {} not permitted",
MAX_RECURSION_DEPTH
),
Error::ScriptSizeTooLarge => write!(
f,
"Standardness rules imply bitcoin than {} bytes",
MAX_SCRIPT_SIZE
),
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/miniscript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ impl<Pk: MiniscriptKey> Miniscript<Pk> {
impl Miniscript<bitcoin::PublicKey> {
/// Attempt to parse a script into a Miniscript representation
pub fn parse(script: &script::Script) -> Result<Miniscript<bitcoin::PublicKey>, Error> {
// Transactions more than 100Kb are non-standard
if script.len() > 10000 {}
Copy link
Contributor

@RCasatta RCasatta Jun 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this missing to return Err?
Is it missing a 0 to be 100k?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the reivew :) . Good catch, will fixup shortly

let tokens = lex(script)?;
let mut iter = TokenIter::new(tokens);

Expand Down
Loading