Coder Social home page Coder Social logo

processus-kindergarten's Introduction

Simple Bootstrapping and Sandboxing of untrusted legacy code

Features:

  • decorate set_error_handler & errorreporting: in sync ! switch'em whenever you want during runtime.
  • decorate set_exception_handler
  • decorate register_shutdown_function: unfortunately all functions get executed. Solution: Callback Proxy and Manager
  • callback functions for all kind of events (onError, onException, onShutdownFailed, ...). No need to override class methods when trying to tweak current business logic.
  • sandbox untrusted legacy code: all error-/exception-/shutdown-settings you changed to make dirty code run will be reverted after executing.

Sandbox Example: let's play with dirty legacy toy ...

// use dirty legacy code: but sandboxed.
var_dump('using dirty code, sandboxed ... ');
$playground = Bootstrap::getInstance()
  ->getPlayground();
$sandbox = $playground->createSimpleSandbox();

$sandbox
  ->setErrorReportingCaptureLevel(
    $sandbox->getErrorReportingCaptureLevelAllStrictNoNotice() // (E_ALL|E_STRICT) ^ E_NOTICE
  )
  ->setDelegateExceptionEnabled(false) // catch exceptions, do not rethrow
  ->setToy(
    function () {
      $data = array();
      $value = $data[0]; // E_NOTICE, but not captured at errorReporting
      var_dump('the value is ...');
      var_dump($value);
      var_dump('this was dirty');

      return true;
    }
   )
  ->setOnError(
     function (Playground $playground, $error) {
       var_dump('ERROR');
     }
    )
  ->setParams(array())
  ->play();
  
 var_dump('--------- SANDBOX HAS EXCEPTION -----------');
 var_dump($sandbox->hasException());
 if ($sandbox->hasException()) {
    var_dump($sandbox->getException()->getMessage());
 }
 var_dump('--------- SANDBOX RESULT -----------');
 var_dump($sandbox->getResult());

processus-kindergarten's People

Contributors

bastman avatar

Stargazers

Dastan avatar

Watchers

 avatar James Cloos avatar

processus-kindergarten's Issues

check to implement more decent error handling

from an older project ..

    // dirty hack for white pages issues

    if (
            ( ((int)$errorNumber) ===8)
            && (fnmatch("Undefined variable*",$errorString, false))
    ) {

        // the bug appears if you d sth like...
        /*

         $x=$bla->blug(); // where bla is undefined
         */
        try {
            $e = new Lib_Application_Exception($errorString);
            $e->setFault(array(
                      $errorNumber,
            $errorString,
            $errorFile,
            $errorLine
                         ));
            throw $e;

        }catch(Exception $e) {
            //var_dump($e->getMessage());
            self::handleUncaughtException($e);
            die("DIED at ".__METHOD__.__LINE__); // just in case
        }
    }

.

shutdown error_get_last on uncaptured error

If raise an error, that has not been catched (by error_reporting_level), it will be available in shutdown function. And finally onShutDownFailed will be called. (But it should not!!!! since I wanted to ignore that error using capture level)

handle output buffering

from an old project ...

/**
 * @static
 * @param  Exception|null $exception
 * @return void
 */
public static function handleUncaughtException($exception)
{
    $errorUserMessage = "An Error occured. Please retry lateron!";

    try {




        $outputBuffer = ob_get_contents();
        ob_end_clean();

        try {
            //if (headers_sent()!==true) {
                //header("HTTP/1.0 500"); // 500 makes trouble in vz env!
            //}
        } catch (Exception $e) {
        }



        if (($exception instanceof Exception) !== true) {
            die(__METHOD__ . " FAILED! Invalid Parameter 'exception'.");
        }

        $isDebugMode = false;
        $isDeveloper = false;
        try {
            $isDeveloper = self::getRegistry()->getDebug()->isDeveloper();
        } catch (Exception $e) {

            var_dump($e);

            die(__METHOD__ . " FAILED! Invalid 'isDebugMode'.");
        }

        try {
            $isDebugMode = self::getRegistry()->getDebug()->isDebugMode();
        } catch (Exception $e) {
            die(__METHOD__ . " FAILED! Invalid 'isDeveloper'.");
        }

        $showDebugInfo = (
        ($isDebugMode === true)
            //    || ($isDeveloper === true)
        );

        if ($showDebugInfo !== true) {
            try {
                if($exception instanceof Lib_Application_Exception) {
                    /**
                     * @var $appException Lib_Application_Exception
                     */
                    $appException = $exception;
                    if(trim(''.$appException->getUserMessage()) !== ""  ) {
                        $errorUserMessage = '' .
                                $appException->getUserMessage();
                    }
                }
            }catch(Exception $ex) {
                //NOP
            }


            die(" " . $errorUserMessage . " (E0021)");

        }


        $errorInfo = array(
            "class" => get_class($exception),
            "message" => $exception->getMessage(),
            "method" => null,
            "methodLine" => null,
            "file" => $exception->getFile(),
            "line" => $exception->getLine(),
            "stackTrace" => $exception->getTrace(),
            "stackTraceAsString" => $exception->getTraceAsString(),
            "fault" => null,
            "lastError" => array(
                "type" => null,
                "file" => null,
                "line" => null,
                "message" => null,
            ),
        );

        if (defined("ROOT_PATH")) {
            $errorInfo["file"] = str_replace(ROOT_PATH, '', $errorInfo['file']);
            $errorInfo["stackTraceAsString"] = str_replace(
                ROOT_PATH, '', $errorInfo['stackTraceAsString']
            );
            $outputBuffer = str_replace(ROOT_PATH, '', $outputBuffer);
        }


        if ($exception instanceof Lib_Application_Exception) {
            /**
             * @var Lib_Application_Exception $exception
             */
            $errorInfo["method"] = $exception->getMethod();
            $errorInfo["fault"] = $exception->getFault();
            $errorInfo["methodLine"] = $exception->getMethodLine();
            $errorInfo["fault"] = $exception->getFault();
        }

        $isShutdownError = false;
        if (
            ($exception instanceof Lib_Application_Exception)
            && ($exception->getMessage() === self::ERROR_SHUTDOWN)
        ) {
            $isShutdownError = true;
        }


        // parse last error
        $lastError = error_get_last();
        if (is_array($lastError)) {

            foreach ($lastError as $key => $value) {
                $errorInfo["lastError"][$key] = $value;
            }

        }

        if (defined("ROOT_PATH")) {
            $errorInfo["lastError"]["file"] = str_replace(
                ROOT_PATH, '', $errorInfo["lastError"]['file']
            );
        }





        // +++++++++++++++++ simple wildfire output ++++++++++++++++++
        try {

            //if (headers_sent()!==true) {
                header('X-Wf-Protocol-1:     http://meta.wildfirehq.org/Protocol/JsonStream/0.2');
                header('X-Wf-1-Plugin-1:     http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/0.3');
                header('X-Wf-1-Structure-1:  http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1');

                $description = $errorInfo['message'] . ' in ' . $errorInfo['file'] . ' line ' . $errorInfo['line'];

                //$msg = '[{"Type":"LOG","File":"'.$error['file'].'","Line":'.$error['line'].'},"'.$description.'"]';

                //NOTICE msg must not have newlines
                $msg = array(
                    "Description" => $description,
                    "Type" => "LOG",
                    "File" => $errorInfo['file'],
                    "Line" => $errorInfo['line'],
                );
                $msg = json_encode($msg);

                header('X-Wf-1-1-1-1: ' . strlen($msg) . '|' . $msg . '|');
            //}
        } catch (Exception $e) {

            // e.g.: "HEADERS ALREADY SENT"
        }

        // ++++++++++++++++++ html nice output ++++++++++++++++++++

        $outHtmlText = '';

        if ($isShutdownError === true) {
            $outHtmlText .= '<b>----------- FATAL ERROR (SHUTDOWN)!  ------------ </b>';
        } else {
            $outHtmlText .= '<b>----------- FATAL ERROR (UNCAUGHT EXCEPTION CATCHED)! ------------ </b>';
        }

        $outHtmlText .= ""
                        . '<br /><br />'
                        . '[' . __METHOD__ . ' '
                        . ' isDebugMode=' . json_encode($isDebugMode)
                        . ' isDeveloper=' . json_encode($isDeveloper)
                        . " ]"
                        . '<br /><br />'

                        . '<b>' . $errorInfo["class"] . "</b>"
                        . ' ' . htmlentities($errorInfo["message"])
                        . '<br /><br />'
                        . ' ' . $errorInfo["method"] . " " . $errorInfo["methodLine"]
                        . ' ' . $errorInfo["file"] . " " . $errorInfo["line"]

                        . '<br /><br />'
                        . htmlentities($errorInfo["stackTraceAsString"])
                        . '<br /><br />';





        ob_start();
        var_dump($errorInfo["lastError"]);
        $lastErrorDump = ob_get_contents();
        ob_clean();
        if (defined("ROOT_PATH")) {
            $lastErrorDump = str_replace(ROOT_PATH, '', $lastErrorDump);
        }
        $outHtmlText .= ''
                        . '<b>------------- LAST ERROR DUMP --------------------</b>'
                        . '<br /><br />'
                        . htmlentities($lastErrorDump)
                        . '<br /><br />';

        ob_start();
        var_dump($errorInfo["fault"]);
        $faultDump = ob_get_contents();
        ob_clean();
        if (defined("ROOT_PATH")) {
            $faultDump = str_replace(ROOT_PATH, '', $faultDump);
        }

        $outHtmlText .= ''
                        . "<b>----------- FAULT DUMP ------------------ </b>"
                        . '<br /><br />'
                        . htmlentities($faultDump)
                        . '<br /><br />';


        if (!$errorInfo["stackTrace"]) {
            $errorInfo["stackTrace"] = debug_backtrace();
        }

        $trace = $errorInfo["stackTrace"];
        foreach ($trace as $key => $stackPoint) {
            // I'm converting arguments to their type
            // (prevents passwords from ever getting logged as anything other than 'string')

            $args = array();
            if (isset($trace[$key]['args'])) {
                $args = $trace[$key]['args'];
            }



            $_args = array();
            foreach ($args as $arg) {
                $argValue = $arg;
                $argType = gettype($arg);
                $argText = "" . $argType;
                if (is_object($arg)) {
                    $argClass = get_class($arg);
                    if ($argClass) {
                        $argText .= " " . $argClass;
                    }
                }

                $_args[] = $argText;
            }
            //$trace[$key]['args'] = array_map('gettype', $trace[$key]['args']);
            $trace[$key]['args'] = $_args;
        }


        ob_start();
        var_dump($trace);
        $stackTraceDump = ob_get_contents();
        ob_clean();
        if (defined("ROOT_PATH")) {
            $stackTraceDump = str_replace(ROOT_PATH, '', $stackTraceDump);
        }

        $outHtmlText .= ''
                        . '<br /><br />'
                        . "<b>--------------- STACKTRACE DUMP--------------</b>"
                        . '<br /><br />'
                        . htmlentities($stackTraceDump)
                        . '<br /><br />';



        if (defined("ROOT_PATH")) {
            $outputBuffer = str_replace(ROOT_PATH, '', $outputBuffer);
        }
        $outHtmlText .= ''
                        . "<b>------------- OUTPUTBUFFER DUMP ------------------</b>"
                        . '<br /><br />'
                        . htmlentities($outputBuffer)
                        . '<br /><br />';



        echo
                '<div style="font-size: 16px; padding: 16px 32px 16px 32px; margin: 16px; border: solid red 3px; background: #000000; color: red;"><pre>'
                . $outHtmlText
                . '</pre></div>';



    } catch (Exception $e) {
        //var_dump($e);
        die("An Error occured. Please retry lateron! (E0022)");
    }


     die("DIED AT ".__METHOD__);
}

/**
     * @static
     * @param  Exception|null $exception
     * @return void
     */
    public static function handleUncaughtExceptionCli($exception)
    {
        var_dump($exception);
        exit(1);
    }

public static function handleShutdown()
{

    $error = error_get_last();

    if ($error === null) {
         // no error, we have a "normal" shut down (script is finished).
        return;
    }

//var_dump(METHOD);
//var_dump($error);
// an error occurred and forced the shut down
$e = new Lib_Application_Exception(self::ERROR_SHUTDOWN);
$e->setMethod(METHOD);
$e->setFault(array(
"lastError" => $error,
));

    self::handleUncaughtException($e);
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.