Index of /pybridge

Icon  Name                    Last modified      Size  Description
[DIR] Parent Directory - [TXT] README.html 15-Jul-2007 16:14 10K [   ] pybridge-0.1.tar.gz 15-Jul-2007 16:14 17K
PyBridge Manual

README for PyBridge Drupal module

Download PyBridge here

Written by David McNab (rebirth AT orcon DOT net DOT nz)

1. What Is PyBridge?

PyBridge is a framework allowing Drupal page-generating modules to be written in Python.

It is intended for people who, like me, enjoy and respect the Drupal web framework, but enjoy Python much more than PHP.

Also, PyBridge could be helpful for integrating existing Python code into a Drupal environment.

2. Installation

PyBridge installation is pretty straightforward:

3. Usage

Study the file hdlr_default.py (in sites/all/modules/pybridge/python), especially the .handle() method of the Handler class.

This method is the default handler for PyBridge, and internally demonstrates much of what is available in the Python environment for Drupal PyBridge modules.

When you're ready, write your own handler module, eg, 'hdlr_foo.py', and put it into /path/to/drupal/root/sites/all/modules/pybridge/python or into /path/to/drupal/root/python.

Here's a minimal boilerplate for your module:

        from drupal import Drupal

        class Handler(Drupal):

            def handle(self):

                self.funcs.drupal_set_title("My 'foo' handler")
                return "handler not implemented yet"
    
The basic guts of it is:

1. Access to PHP variables

2. Calling PHP functions

3. Evaluating expressions in PHP

4. Executing arbitrary PHP code within PHP

5. Generating page content


4. Python API

Most interaction with Drupal PHP environment is done through the following attributes of your Handler class (inherited from the Drupal class):

5. Choosing which python handler executes

By default, any hit to the uri /pybridge will cause the default python handler (in hdlr_default.py) to run.

You can implement n other handlers, calling them hdlr_foo.py, hdlr_bar.py etc.

PyBridge decides which module to run in the following way:
  1. If there are any path components beyond pybridge, for instance if the URI is /pybridge/foo/fred, then the first path element after pybridge (in this case, foo) will be taken as the name of the desired handler.
  2. If there are no path components beyond pybridge, but if there is a GET variable called hdlr, then the value of this variable will be taken as the name of the desired handler
  3. If a handler has not been chosen by either of the two methods above, or if there is no corresponding file hdlr_<handlername>.py, then pybridge will fall back to executing the default handler, from hdlr_default.py
This scheme, of allowing for several python handlers, allows for several discrete python modules to work together on the one drupal site.

6. Security - IMPORTANT

Note that several PyBridge features, especially those allowing for execution of arbitrary expressions and code, can introduce a potentially devastating security hole into your Drupal installation.

To mitigate the risks:
  1. Ensure both directories: /path/to/site/root/sites/all/modules/pybridge and /path/to/site/root/python directories - both contain .htaccess files with a Deny from all directive, to prevent HTTP access to these directories
  2. Adjust the unix permissions of both these directories to reduce or eliminate the risk of unauthorised access
  3. Don't install PyBridge into a Drupal installation running on a shared host, unless the hosting environment is set up with SUEXEC, so that scripts within your account run with your user id, instead of a common user id such as www-data.
  4. Take extreme care in the coding of your python handler modules, especially with any trust you place in data coming from users via cookies, GET, POST.

    Assume that any such data may be malformed (eg SQL injection attempts) as a deliberate attempt to compromise your site.

    Note that all kinds of crap can turn up in user data, including but not limited to:
    • javascript code and cross-site-scripting attacks
    • browser exploits
    • fiddling with quotes to attempt access to the Drupal database
    • fiddling with cookies to attempt execution of arbitrary python or PHP code
    • other embedded nasties
    Don't store any GET or POST user data into the database without carefully screening it first
  5. Don't use pickled cookies without an HMAC signature
  6. Don't depend on security through obscurity. Assume that thousands of hackers have access to all your python source and have studied it for weaknesses