/* Copyright (c) 2020 Contrast Security, Inc. See * https://www.contrastsecurity.com/enduser-terms-0317a for more details. */ #include "cs__assess_module.h" #include "../cs__common/cs__common.h" #include void contrast_assess_eval_trigger_check(VALUE module, VALUE source, VALUE ret) { VALUE has_trigger_check = rb_respond_to(module, trigger_check_method); if (RTEST( rb_funcall(contrast_patcher(), rb_sym_skip_contrast_analysis, 0))) { return; } int nested_scope = RTEST(rb_funcall(contrast_patcher(), rb_sym_in_scope, 0)); rb_funcall(contrast_patcher(), rb_sym_enter_scope, 0); if (!nested_scope && has_trigger_check) { VALUE method = rb_funcall(rb_mKernel, rb_sym_method, 0); /* If this method ever throws an exception, the scope-leave * needs to be moved within a rescue call. */ rb_funcall(module, trigger_check_method, 3, source, ret, method); } rb_funcall(contrast_patcher(), rb_sym_exit_scope, 0); } VALUE contrast_assess_module_class_eval(const int argc, const VALUE *argv, const VALUE mod) { VALUE ret = rb_mod_module_eval(argc, argv, mod); if (argc > 0) { VALUE data = argv[0]; contrast_assess_eval_trigger_check(mod, data, ret); } rb_funcall(assess_patcher, rb_sym_assess_patch_eval, 1, mod); return ret; } VALUE contrast_assess_module_module_eval(const int argc, const VALUE *argv, const VALUE mod) { VALUE ret = rb_mod_module_eval(argc, argv, mod); if (argc > 0) { VALUE data = argv[0]; contrast_assess_eval_trigger_check(mod, data, ret); } rb_funcall(assess_patcher, rb_sym_assess_patch_eval, 1, mod); return ret; } void Init_cs__assess_module(void) { rb_sym_assess_patch_eval = rb_intern("patch_assess_on_eval"); VALUE assess_policy = rb_define_module_under(assess, "Policy"); assess_patcher = rb_define_module_under(assess_policy, "Patcher"); trigger_check_method = rb_intern("eval_trigger_check"); contrast_alias_method(rb_cModule, "cs__patched_class_eval", "class_eval"); rb_define_method(rb_cModule, "class_eval", contrast_assess_module_class_eval, -1); contrast_alias_method(rb_cModule, "cs__patched_module_eval", "module_eval"); rb_define_method(rb_cModule, "module_eval", contrast_assess_module_module_eval, -1); }