Skip to content

Commit

Permalink
Fixed Code injection vulnerability by using illegal function names
Browse files Browse the repository at this point in the history
  • Loading branch information
wisskid committed Jan 24, 2021
1 parent fedc127 commit 165f1bd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Security
- Code injection vulnerability by using illegal function names in `{function name='blah'}{/function}`

## [3.1.38] - 2021-01-08

### Fixed
Expand Down
5 changes: 5 additions & 0 deletions libs/sysplugins/smarty_internal_compile_function.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
}
unset($_attr[ 'nocache' ]);
$_name = trim($_attr[ 'name' ], '\'"');

if (!preg_match('/^[a-zA-Z0-9_\x80-\xff]+$/', $_name)) {
$compiler->trigger_template_error("Function name contains invalid characters: {$_name}", null, true);
}

$compiler->parent_compiler->tpl_function[ $_name ] = array();
$save = array(
$_attr, $compiler->parser->current_buffer, $compiler->template->compiled->has_nocache_code,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,5 +431,14 @@ public function dataTestSpacing()
array("{function name=simple}A{\$foo}\nC{/function}{call name='simple'}", "Abar\nC", 'T14', $i++),
array("{function name=simple}A\n{\$foo}\nC{/function}{call name='simple'}", "A\nbar\nC", 'T15', $i++),
);
}
}

/**
* Test handling of function names that are a security risk
*/
public function testIllegalFunctionName() {
$this->expectException(SmartyCompilerException::class);
$this->smarty->fetch('string:{function name=\'rce(){};echo "hi";function \'}{/function}');
}

}

0 comments on commit 165f1bd

Please sign in to comment.