cvs: presentations /slides/intro nz2_ref.xml php_opcache_hints1.xml php_opcache_hints2.xml php_opcache_hints3.xml php_opcache_hints4.xml
xss_charset.xml xss_common.xml
| From: | Rasmus Lerdorf | Date: | Thu, 29 Dec 2005 18:25:22 +0000 |
| Subject: | cvs: presentations /slides/intro nz2_ref.xml php_opcache_hints1.xml php_opcache_hints2.xml php_opcache_hints3.xml php_opcache_hints4.xml xss_charset.xml xss_common.xml |
||
| Groups: | php.pres | ||
| Request: | Send a blank email to pres+get-1233@lists.php.net to get a copy of this message | ||
rasmus Thu Dec 29 18:25:22 2005 EDT
Added files:
/presentations/slides/intro nz2_ref.xml php_opcache_hints4.xml
php_opcache_hints3.xml
php_opcache_hints2.xml
php_opcache_hints1.xml xss_charset.xml
Modified files:
/presentations/slides/intro xss_common.xml
Log:
More new slides
http://cvs.php.net/viewcvs.cgi/presentations/slides/intro/xss_common.xml?r1=1.1&r2=1.2&diff_format=u Index: presentations/slides/intro/xss_common.xml diff -u presentations/slides/intro/xss_common.xml:1.1 presentations/slides/intro/xss_common.xml:1.2 --- presentations/slides/intro/xss_common.xml:1.1 Thu Sep 15 03:24:22 2005 +++ presentations/slides/intro/xss_common.xml Thu Dec 29 18:25:22 2005 @@ -8,7 +8,7 @@ <bullet>Host header </bullet> <bullet>$_GET, $_POST, $_COOKIE</bullet> <bullet>$_SERVER</bullet> -<bullet>?var=1 vs. ?var[]=1</bullet> +<bullet>Charset detection stupidity in IE</bullet> </list> </slide> http://cvs.php.net/viewcvs.cgi/presentations/slides/intro/nz2_ref.xml?view=markup&rev=1.1 Index: presentations/slides/intro/nz2_ref.xml +++ presentations/slides/intro/nz2_ref.xml <slide title="Resources"> <blurb> </blurb> <blurb> </blurb> <blurb> </blurb> <blurb> </blurb> <link fontsize="3em" marginleft="3em" leader="Slides: " href="http://talks.lerdorf.com/show/acon05"/> <link fontsize="3em" marginleft="3em" leader="APC: " href="http://pecl.php.net/package/APC"/> <link fontsize="3em" marginleft="3em" leader="Xdebug: " href="http://xdebug.org/"/> <link fontsize="3em" marginleft="3em" leader="Yahoo: " href="http://developer.yahoo.net/"/> <link fontsize="3em" marginleft="3em" leader="Flickr: " href="http://flickr.com/services/api/"/> </slide> http://cvs.php.net/viewcvs.cgi/presentations/slides/intro/php_opcache_hints4.xml?view=markup&rev=1.1 Index: presentations/slides/intro/php_opcache_hints4.xml +++ presentations/slides/intro/php_opcache_hints4.xml <slide title="Opcode Cache Hints"> <blurb title="Tend towards fewer includes"> With an opcode cache you are better off including fewer files that are larger because each include results in a disk-touching %stat()% system call. </blurb> <example title="Don't"><![CDATA[<?php include 'a.inc'; include 'b.inc'; include 'c.inc'; include 'd.inc'; include 'e.inc'; ?>]]></example> <example title="Do"><![CDATA[<?php include './abcde.inc'; ?>]]></example> </slide> http://cvs.php.net/viewcvs.cgi/presentations/slides/intro/php_opcache_hints3.xml?view=markup&rev=1.1 Index: presentations/slides/intro/php_opcache_hints3.xml +++ presentations/slides/intro/php_opcache_hints3.xml <slide title="Opcode Cache Hints"> <blurb title="Take advantage of your cache"> A common thing people do is to have some sort of %config.ini% file that they include on every request. Typically such a file looks like this: </blurb> <example><![CDATA[<?php $config['install_path'] = "/usr/share/htdocs"; $config['db_type'] = "mysql"; $config['db_user'] = "nobody"; $config['db_pwd'] = ""; ?>]]></example> <blurb> APC has *apc_store()* and *apc_fetch()* which is perfect for something like this and it is trivial to implement. </blurb> <example><![CDATA[<?php if(!$config = apc_fetch('config')) { include './config.inc'; apc_store('config',$config); } ?>]]></example> <blurb> We break our own rule of avoiding conditional includes, but this is one that will only be done on the very first request after server startup. And you can always update the config without restarting the server by doing an *apc_store()* on top of it. </blurb> <blurb> The other way you can do this, which doesn't require an opcode cache nor any shared memory is to make use of the %--with-config-file-scan-dir% mechanism which allows you to specify a directory that will be scanned for ini files. Any *foo.ini* file in this directory will be read on server startup and you then use *get_cfg_var()* to access the settings. The one downside to this approach is that you have to restart the server to make any changes to the config. </blurb> </slide> http://cvs.php.net/viewcvs.cgi/presentations/slides/intro/php_opcache_hints2.xml?view=markup&rev=1.1 Index: presentations/slides/intro/php_opcache_hints2.xml +++ presentations/slides/intro/php_opcache_hints2.xml <slide title="Opcode Cache Hints"> <blurb title="Push everything you can into the compile phase"> This means that you should never have conditional function or class definitions. So avoid code like this: </blurb> <example><![CDATA[<?php if(!function_exists('foobar')) { function foobar() { echo "foobar"; } } ?>]]></example> <blurb> This also means you should avoid implicit conditional operations such as *autoload* and once again the *include_once* and *require_once* mechanisms. So, always do includes, function declarations and class definitions at the top level of your scripts. Keep your include trees simple and straightforward and hopefully relative to your including script, or at the very least include from the first directory of your include_path list. </blurb> </slide> http://cvs.php.net/viewcvs.cgi/presentations/slides/intro/php_opcache_hints1.xml?view=markup&rev=1.1 Index: presentations/slides/intro/php_opcache_hints1.xml +++ presentations/slides/intro/php_opcache_hints1.xml <slide title="Opcode Cache Hints"> <blurb> There are a number of things you can do to make sure your cached scripts run fast. </blurb> <blurb title="Never use include_once or require_once"> The code for include/require looks like this: </blurb> <example fontsize="1.4em"><![CDATA[op_array = compile_filename(opline->op2.u.constant.value.lval, inc_filename);]]></example> <blurb> That is, we simply call the compiler which in an opcode cached architecture will be intercepted and the opcodes will be pulled from shared memory and fed directly to the executor. For the include_once case the code looks like this: </blurb> <example fontsize="1.4em" type="C"><![CDATA[ int dummy = 1; zend_file_handle file_handle; if(SUCCESS == zend_stream_open(inc_filename->value.str.val, &file_handle)) { if(!file_handle.opened_path) { file_handle.opened_path = estrndup(inc_filename->value.str.val, inc_filename->value.str.len); } if(zend_hash_add(&EG(included_files), file_handle.opened_path, strlen(file_handle.opened_path)+1, (void *)&dummy, sizeof(int), NULL)==SUCCESS) { op_array = zend_compile_file(&file_handle, ZEND_INCLUDE); zend_destroy_file_handle(&file_handle); } else { zend_file_handle_dtor(&file_handle); failure_retval=1; } } else { if(opline->op2.u.constant.value.lval==ZEND_INCLUDE_ONCE) { zend_message_dispatcher(ZMSG_FAILED_INCLUDE_FOPEN, inc_filename->value.str.val); } else { zend_message_dispatcher(ZMSG_FAILED_REQUIRE_FOPEN, inc_filename->value.str.val); } }?>]]></example> <blurb> This rather complex-looking code makes sense in a non-opcode cached architecture because in order to figure out if we have included this file already we need to go through all the same steps as we do when we subsequently need to open the file, so it makes sense to just go ahead an open in it and then feed the opened file handle to the compiler. This saves a lot of work, but unfortunately it makes very little sense when we plug in an opcode cache. </blurb> </slide> http://cvs.php.net/viewcvs.cgi/presentations/slides/intro/xss_charset.xml?view=markup&rev=1.1 Index: presentations/slides/intro/xss_charset.xml +++ presentations/slides/intro/xss_charset.xml <slide title="Charset Detection in IE"> <break lines="1" /> <blurb>Earlier we gave this as an example of how to treat user data for display purposes</blurb> <example fontsize="1.4em" title="Safe Form Handling" result="0"><![CDATA[<form action="<?php echo htmlspecialchars($PHP_SELF)?>" method="POST"> Your name: <input type="text" name="name" /><br> You age: <input type="text" name="age" /><br> <input type="submit" /> </form> <?php if($_SERVER['REQUEST_METHOD']=='POST') { $Oname = htmlspecialchars($_POST['name']); $Oage = (int)$_POST['age']; echo "Hi $Oname, you are $Oage years old."; }?>]]></example> <blurb>We probably all know how braindead Internet Explorer is about how it deals with mime types, but it is just as braindead in how it handles character set detection. If you don't specify a character set in the Content-Type response header or in the page itself in an http-equiv META tag, it will scan the first 4096 bytes of the reponse body looking for bytes it recognizes as a specific character set. So the above form which is safe on all other browsers is not safe on IE for input like this:</blurb> <example><![CDATA[%2BADw-script%2BAD4-alert(document.cookie)%2BADw-/script%2BAD4-]]></example> <blurb><![CDATA[This is the string: %|EE2222|<script>alert(document.cookie)</script>|% written in UTF-7 with %|EE2222|+|% converted to %|EE2222|\%2B|%.]]></blurb> <blurb> For PHP you can get around this by specifying a default charset in your %php.ini% file. </blurb> <example><![CDATA[default_charset="UTF-8"]]></example> <blurb>For non-PHP requests, you can use Apache's %AddDefaultCharset% configuration directive which will add the specified charset to any response that does not specify one already. If you simply turn it on, instead of specifying a charset you get Apache's internal default of %iso-8859-1%.</blurb> <example><![CDATA[AddDefaultCharset On]]></example> </slide>
http://cvs.php.net/viewcvs.cgi/presentations/slides/intro/xss_common.xml?r1=1.1&r2=1.2&diff_format=u Index: presentations/slides/intro/xss_common.xml diff -u presentations/slides/intro/xss_common.xml:1.1 presentations/slides/intro/xss_common.xml:1.2 --- presentations/slides/intro/xss_common.xml:1.1 Thu Sep 15 03:24:22 2005 +++ presentations/slides/intro/xss_common.xml Thu Dec 29 18:25:22 2005 @@ -8,7 +8,7 @@ <bullet>Host header </bullet> <bullet>$_GET, $_POST, $_COOKIE</bullet> <bullet>$_SERVER</bullet> -<bullet>?var=1 vs. ?var[]=1</bullet> +<bullet>Charset detection stupidity in IE</bullet> </list> </slide> http://cvs.php.net/viewcvs.cgi/presentations/slides/intro/nz2_ref.xml?view=markup&rev=1.1 Index: presentations/slides/intro/nz2_ref.xml +++ presentations/slides/intro/nz2_ref.xml <slide title="Resources"> <blurb> </blurb> <blurb> </blurb> <blurb> </blurb> <blurb> </blurb> <link fontsize="3em" marginleft="3em" leader="Slides: " href="http://talks.lerdorf.com/show/acon05"/> <link fontsize="3em" marginleft="3em" leader="APC: " href="http://pecl.php.net/package/APC"/> <link fontsize="3em" marginleft="3em" leader="Xdebug: " href="http://xdebug.org/"/> <link fontsize="3em" marginleft="3em" leader="Yahoo: " href="http://developer.yahoo.net/"/> <link fontsize="3em" marginleft="3em" leader="Flickr: " href="http://flickr.com/services/api/"/> </slide> http://cvs.php.net/viewcvs.cgi/presentations/slides/intro/php_opcache_hints4.xml?view=markup&rev=1.1 Index: presentations/slides/intro/php_opcache_hints4.xml +++ presentations/slides/intro/php_opcache_hints4.xml <slide title="Opcode Cache Hints"> <blurb title="Tend towards fewer includes"> With an opcode cache you are better off including fewer files that are larger because each include results in a disk-touching %stat()% system call. </blurb> <example title="Don't"><![CDATA[<?php include 'a.inc'; include 'b.inc'; include 'c.inc'; include 'd.inc'; include 'e.inc'; ?>]]></example> <example title="Do"><![CDATA[<?php include './abcde.inc'; ?>]]></example> </slide> http://cvs.php.net/viewcvs.cgi/presentations/slides/intro/php_opcache_hints3.xml?view=markup&rev=1.1 Index: presentations/slides/intro/php_opcache_hints3.xml +++ presentations/slides/intro/php_opcache_hints3.xml <slide title="Opcode Cache Hints"> <blurb title="Take advantage of your cache"> A common thing people do is to have some sort of %config.ini% file that they include on every request. Typically such a file looks like this: </blurb> <example><![CDATA[<?php $config['install_path'] = "/usr/share/htdocs"; $config['db_type'] = "mysql"; $config['db_user'] = "nobody"; $config['db_pwd'] = ""; ?>]]></example> <blurb> APC has *apc_store()* and *apc_fetch()* which is perfect for something like this and it is trivial to implement. </blurb> <example><![CDATA[<?php if(!$config = apc_fetch('config')) { include './config.inc'; apc_store('config',$config); } ?>]]></example> <blurb> We break our own rule of avoiding conditional includes, but this is one that will only be done on the very first request after server startup. And you can always update the config without restarting the server by doing an *apc_store()* on top of it. </blurb> <blurb> The other way you can do this, which doesn't require an opcode cache nor any shared memory is to make use of the %--with-config-file-scan-dir% mechanism which allows you to specify a directory that will be scanned for ini files. Any *foo.ini* file in this directory will be read on server startup and you then use *get_cfg_var()* to access the settings. The one downside to this approach is that you have to restart the server to make any changes to the config. </blurb> </slide> http://cvs.php.net/viewcvs.cgi/presentations/slides/intro/php_opcache_hints2.xml?view=markup&rev=1.1 Index: presentations/slides/intro/php_opcache_hints2.xml +++ presentations/slides/intro/php_opcache_hints2.xml <slide title="Opcode Cache Hints"> <blurb title="Push everything you can into the compile phase"> This means that you should never have conditional function or class definitions. So avoid code like this: </blurb> <example><![CDATA[<?php if(!function_exists('foobar')) { function foobar() { echo "foobar"; } } ?>]]></example> <blurb> This also means you should avoid implicit conditional operations such as *autoload* and once again the *include_once* and *require_once* mechanisms. So, always do includes, function declarations and class definitions at the top level of your scripts. Keep your include trees simple and straightforward and hopefully relative to your including script, or at the very least include from the first directory of your include_path list. </blurb> </slide> http://cvs.php.net/viewcvs.cgi/presentations/slides/intro/php_opcache_hints1.xml?view=markup&rev=1.1 Index: presentations/slides/intro/php_opcache_hints1.xml +++ presentations/slides/intro/php_opcache_hints1.xml <slide title="Opcode Cache Hints"> <blurb> There are a number of things you can do to make sure your cached scripts run fast. </blurb> <blurb title="Never use include_once or require_once"> The code for include/require looks like this: </blurb> <example fontsize="1.4em"><![CDATA[op_array = compile_filename(opline->op2.u.constant.value.lval, inc_filename);]]></example> <blurb> That is, we simply call the compiler which in an opcode cached architecture will be intercepted and the opcodes will be pulled from shared memory and fed directly to the executor. For the include_once case the code looks like this: </blurb> <example fontsize="1.4em" type="C"><![CDATA[ int dummy = 1; zend_file_handle file_handle; if(SUCCESS == zend_stream_open(inc_filename->value.str.val, &file_handle)) { if(!file_handle.opened_path) { file_handle.opened_path = estrndup(inc_filename->value.str.val, inc_filename->value.str.len); } if(zend_hash_add(&EG(included_files), file_handle.opened_path, strlen(file_handle.opened_path)+1, (void *)&dummy, sizeof(int), NULL)==SUCCESS) { op_array = zend_compile_file(&file_handle, ZEND_INCLUDE); zend_destroy_file_handle(&file_handle); } else { zend_file_handle_dtor(&file_handle); failure_retval=1; } } else { if(opline->op2.u.constant.value.lval==ZEND_INCLUDE_ONCE) { zend_message_dispatcher(ZMSG_FAILED_INCLUDE_FOPEN, inc_filename->value.str.val); } else { zend_message_dispatcher(ZMSG_FAILED_REQUIRE_FOPEN, inc_filename->value.str.val); } }?>]]></example> <blurb> This rather complex-looking code makes sense in a non-opcode cached architecture because in order to figure out if we have included this file already we need to go through all the same steps as we do when we subsequently need to open the file, so it makes sense to just go ahead an open in it and then feed the opened file handle to the compiler. This saves a lot of work, but unfortunately it makes very little sense when we plug in an opcode cache. </blurb> </slide> http://cvs.php.net/viewcvs.cgi/presentations/slides/intro/xss_charset.xml?view=markup&rev=1.1 Index: presentations/slides/intro/xss_charset.xml +++ presentations/slides/intro/xss_charset.xml <slide title="Charset Detection in IE"> <break lines="1" /> <blurb>Earlier we gave this as an example of how to treat user data for display purposes</blurb> <example fontsize="1.4em" title="Safe Form Handling" result="0"><![CDATA[<form action="<?php echo htmlspecialchars($PHP_SELF)?>" method="POST"> Your name: <input type="text" name="name" /><br> You age: <input type="text" name="age" /><br> <input type="submit" /> </form> <?php if($_SERVER['REQUEST_METHOD']=='POST') { $Oname = htmlspecialchars($_POST['name']); $Oage = (int)$_POST['age']; echo "Hi $Oname, you are $Oage years old."; }?>]]></example> <blurb>We probably all know how braindead Internet Explorer is about how it deals with mime types, but it is just as braindead in how it handles character set detection. If you don't specify a character set in the Content-Type response header or in the page itself in an http-equiv META tag, it will scan the first 4096 bytes of the reponse body looking for bytes it recognizes as a specific character set. So the above form which is safe on all other browsers is not safe on IE for input like this:</blurb> <example><![CDATA[%2BADw-script%2BAD4-alert(document.cookie)%2BADw-/script%2BAD4-]]></example> <blurb><![CDATA[This is the string: %|EE2222|<script>alert(document.cookie)</script>|% written in UTF-7 with %|EE2222|+|% converted to %|EE2222|\%2B|%.]]></blurb> <blurb> For PHP you can get around this by specifying a default charset in your %php.ini% file. </blurb> <example><![CDATA[default_charset="UTF-8"]]></example> <blurb>For non-PHP requests, you can use Apache's %AddDefaultCharset% configuration directive which will add the specified charset to any response that does not specify one already. If you simply turn it on, instead of specifying a charset you get Apache's internal default of %iso-8859-1%.</blurb> <example><![CDATA[AddDefaultCharset On]]></example> </slide>