Skip to content

Commit 02c4820

Browse files
committed
PHPC-889: Abort BSON parsing if Javascript scope is invalid
1 parent 5fd5286 commit 02c4820

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

src/bson.c

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -605,30 +605,37 @@ static bool php_phongo_bson_visit_symbol(const bson_iter_t* iter, const char* ke
605605
return false;
606606
} /* }}} */
607607

608-
static void php_phongo_bson_new_javascript_from_javascript_and_scope(zval* object, const char* code, size_t code_len, const bson_t* scope TSRMLS_DC) /* {{{ */
608+
static bool php_phongo_bson_new_javascript_from_javascript_and_scope(zval* object, const char* code, size_t code_len, const bson_t* scope TSRMLS_DC) /* {{{ */
609609
{
610610
php_phongo_javascript_t* intern;
611611

612-
object_init_ex(object, php_phongo_javascript_ce);
613-
614-
intern = Z_JAVASCRIPT_OBJ_P(object);
615-
intern->code = estrndup(code, code_len);
616-
intern->code_len = code_len;
617-
intern->scope = scope ? bson_copy(scope) : NULL;
618-
619612
if (scope) {
620613
php_phongo_bson_state state;
614+
bool valid_scope;
621615

622616
PHONGO_BSON_INIT_STATE(state);
623617

624-
php_phongo_bson_to_zval_ex(bson_get_data(intern->scope), intern->scope->len, &state);
618+
valid_scope = php_phongo_bson_to_zval_ex(bson_get_data(scope), scope->len, &state);
625619
zval_ptr_dtor(&state.zchild);
620+
621+
if (!valid_scope) {
622+
return false;
623+
}
626624
}
625+
626+
object_init_ex(object, php_phongo_javascript_ce);
627+
628+
intern = Z_JAVASCRIPT_OBJ_P(object);
629+
intern->code = estrndup(code, code_len);
630+
intern->code_len = code_len;
631+
intern->scope = scope ? bson_copy(scope) : NULL;
632+
633+
return true;
627634
} /* }}} */
628635

629-
static void php_phongo_bson_new_javascript_from_javascript(zval* object, const char* code, size_t code_len TSRMLS_DC) /* {{{ */
636+
static bool php_phongo_bson_new_javascript_from_javascript(zval* object, const char* code, size_t code_len TSRMLS_DC) /* {{{ */
630637
{
631-
php_phongo_bson_new_javascript_from_javascript_and_scope(object, code, code_len, NULL TSRMLS_CC);
638+
return php_phongo_bson_new_javascript_from_javascript_and_scope(object, code, code_len, NULL TSRMLS_CC);
632639
} /* }}} */
633640

634641
static bool php_phongo_bson_visit_code(const bson_iter_t* iter ARG_UNUSED, const char* key, size_t v_code_len, const char* v_code, void* data) /* {{{ */
@@ -638,7 +645,9 @@ static bool php_phongo_bson_visit_code(const bson_iter_t* iter ARG_UNUSED, const
638645
#if PHP_VERSION_ID >= 70000
639646
zval zchild;
640647

641-
php_phongo_bson_new_javascript_from_javascript(&zchild, v_code, v_code_len TSRMLS_CC);
648+
if (!php_phongo_bson_new_javascript_from_javascript(&zchild, v_code, v_code_len TSRMLS_CC)) {
649+
return true;
650+
}
642651

643652
if (state->is_visiting_array) {
644653
add_next_index_zval(retval, &zchild);
@@ -650,7 +659,10 @@ static bool php_phongo_bson_visit_code(const bson_iter_t* iter ARG_UNUSED, const
650659
TSRMLS_FETCH();
651660

652661
MAKE_STD_ZVAL(zchild);
653-
php_phongo_bson_new_javascript_from_javascript(zchild, v_code, v_code_len TSRMLS_CC);
662+
if (!php_phongo_bson_new_javascript_from_javascript(zchild, v_code, v_code_len TSRMLS_CC)) {
663+
zval_ptr_dtor(&zchild);
664+
return true;
665+
}
654666

655667
if (state->is_visiting_array) {
656668
add_next_index_zval(retval, zchild);
@@ -716,7 +728,9 @@ static bool php_phongo_bson_visit_codewscope(const bson_iter_t* iter ARG_UNUSED,
716728
#if PHP_VERSION_ID >= 70000
717729
zval zchild;
718730

719-
php_phongo_bson_new_javascript_from_javascript_and_scope(&zchild, v_code, v_code_len, v_scope TSRMLS_CC);
731+
if (!php_phongo_bson_new_javascript_from_javascript_and_scope(&zchild, v_code, v_code_len, v_scope TSRMLS_CC)) {
732+
return true;
733+
}
720734

721735
if (state->is_visiting_array) {
722736
add_next_index_zval(retval, &zchild);
@@ -728,7 +742,10 @@ static bool php_phongo_bson_visit_codewscope(const bson_iter_t* iter ARG_UNUSED,
728742
TSRMLS_FETCH();
729743

730744
MAKE_STD_ZVAL(zchild);
731-
php_phongo_bson_new_javascript_from_javascript_and_scope(zchild, v_code, v_code_len, v_scope TSRMLS_CC);
745+
if (!php_phongo_bson_new_javascript_from_javascript_and_scope(zchild, v_code, v_code_len, v_scope TSRMLS_CC)) {
746+
zval_ptr_dtor(&zchild);
747+
return true;
748+
}
732749

733750
if (state->is_visiting_array) {
734751
add_next_index_zval(retval, zchild);

0 commit comments

Comments
 (0)