Modify

Opened 10 years ago

Closed 9 years ago

Last modified 9 years ago

#11907 closed defect (fixed)

API docs don't display

Reported by: Ryan J Ollos Owned by: Jun Omae
Priority: normal Component: TracDeveloperPlugin
Severity: normal Keywords:
Cc: Trac Release: 1.0

Description

When on the Plugin Registry page (/developer/plugins) and trying to view the documentation for an interface, the documentation does not display and the following error is seen in the logs:

09:02:10 PM Trac[main] ERROR: Internal Server Error: 
Traceback (most recent call last):
  File "/home/user/Workspace/tracdev/teo-rjollos.git/trac/web/main.py", line 543, in _dispatch_request
    dispatcher.dispatch(req)
  File "/home/user/Workspace/tracdev/teo-rjollos.git/trac/web/main.py", line 223, in dispatch
    resp = chosen_handler.process_request(req)
  File "/home/user/Workspace/trachacks.git/tracdeveloperplugin/trunk/tracdeveloper/apidoc.py", line 44, in process_request
    'doc': formatter(req, inspect.getdoc(obj)),
  File "/home/user/Workspace/trachacks.git/tracdeveloperplugin/trunk/tracdeveloper/apidoc.py", line 65, in _format_default
    return linebreaks(text)
  File "/home/user/Workspace/trachacks.git/tracdeveloperplugin/trunk/tracdeveloper/util.py", line 14, in linebreaks
    return HTML(''.join(paras))
  File "/home/user/Workspace/tracdev/local/lib/python2.7/site-packages/Genshi-0.7-py2.7-linux-x86_64.egg/genshi/input.py", line 442, in HTML
    return Stream(list(HTMLParser(BytesIO(text), encoding=encoding)))
  File "/home/user/Workspace/tracdev/local/lib/python2.7/site-packages/Genshi-0.7-py2.7-linux-x86_64.egg/genshi/core.py", line 273, in _ensure
    event = stream.next()
  File "/home/user/Workspace/tracdev/local/lib/python2.7/site-packages/Genshi-0.7-py2.7-linux-x86_64.egg/genshi/input.py", line 449, in _coalesce
    for kind, data, pos in chain(stream, [(None, None, None)]):
  File "/home/user/Workspace/tracdev/local/lib/python2.7/site-packages/Genshi-0.7-py2.7-linux-x86_64.egg/genshi/input.py", line 337, in _generate
    raise UnicodeError("source returned bytes, but no encoding specified")
UnicodeError: source returned bytes, but no encoding specified

Issue is seen in 1.0.2dev and 1.1.2dev. I haven't tested with other versions.

Attachments (1)

t11907.diff (3.0 KB) - added by Jun Omae 10 years ago.

Download all attachments as: .zip

Change History (11)

comment:1 Changed 10 years ago by Ryan J Ollos

Summary: API docs don't display inAPI docs don't display

comment:2 Changed 10 years ago by Ryan J Ollos

Side note: there is an unresolved reference here: tracdeveloperplugin/trunk/tracdeveloper/apidoc.py@3230:62#L51.

comment:3 Changed 10 years ago by Ryan J Ollos

The issue occurs with Genshi 0.7. The following patch fixes the issue, but I am unsure as to whether it is the best way:

  • tracdeveloperplugin/trunk/tracdeveloper/apidoc.py

    diff --git a/tracdeveloperplugin/trunk/tracdeveloper/apidoc.py b/tracdeveloperpl
    index 34873b3..a7f5f64 100644
    a b class APIDocumentation(Component): 
    4646        }
    4747        output = Chrome(self.env).render_template(req, 'developer/apidoc.html',
    4848                                                  data, fragment=True)
    49         req.send(output.render('xhtml'), 'text/html')
     49        req.send(output.render('xhtml').encode('utf-8'), 'text/html')
    5050
    5151    # Internal methods
    5252    def _get_formatter(self, module):
  • tracdeveloperplugin/trunk/tracdeveloper/util.py

    diff --git a/tracdeveloperplugin/trunk/tracdeveloper/util.py b/tracdeveloperplug
    index c9b86c0..ce16f02 100644
    a b def linebreaks(value): 
    1111    value = re.sub(r'\r\n|\r|\n', '\n', value) # normalize newlines
    1212    paras = re.split('\n{2,}', value)
    1313    paras = ['<p>%s</p>' % p.strip().replace('\n', '<br />') for p in paras]
    14     return HTML(''.join(paras))
     14    return HTML(''.join(paras), encoding='utf-8')

comment:4 in reply to:  3 Changed 10 years ago by Jun Omae

Replying to rjollos:

The issue occurs with Genshi 0.7. The following patch fixes the issue, but I am unsure as to whether it is the best way:

I think it would be simple to return template and data from process_request method rather than call directly render_template.

  • tracdeveloper/apidoc.py

     
    4444            'doc': formatter(req, inspect.getdoc(obj)),
    4545            'methods': self._get_methods(req, formatter, obj)
    4646        }
    47         output = Chrome(self.env).render_template(req, 'developer/apidoc.html',
    48                                                   data, fragment=True)
    49         req.send(output.render('xhtml'), 'text/html')
     47        return 'developer/apidoc.html', data, 'text/html'
    5048
    5149    # Internal methods
    5250    def _get_formatter(self, module):

comment:5 Changed 10 years ago by Jun Omae

Also, linebreaks method has an XSS issue. For example, documentation of trac.mimeview.api:IHTMLPreviewRenderer has <object> and <img>. /developer/doc/trac.mimeview.api:IHTMLPreviewRenderer page renders thoes text without escape.

The following patch works well on Genshi 0.6 and 0.7.

  • tracdeveloper/util.py

     
    22
    33import re
    44
    5 from genshi import HTML
     5from genshi.builder import tag
    66
    77def linebreaks(value):
    88    """Converts newlines in strings into <p> and <br />s."""
     
    1010        return ''
    1111    value = re.sub(r'\r\n|\r|\n', '\n', value) # normalize newlines
    1212    paras = re.split('\n{2,}', value)
    13     paras = ['<p>%s</p>' % p.strip().replace('\n', '<br />') for p in paras]
    14     return HTML(''.join(paras))
     13    return tag(tag.p((line, tag.br) for line in para.splitlines())
     14               for para in paras)

comment:6 Changed 10 years ago by Jun Omae

Another thing: I think /developer/api should require TRAC_DEVELOP permission. Currently, anyone can let to import any objects using the feature.

  • tracdeveloper/apidoc.py

     
    2929            return True
    3030
    3131    def process_request(self, req):
     32        req.perm.require('TRAC_DEVELOP')
    3233        modname, attrname = str(req.args['name']).split(':')
    3334        try:
    3435            module = __import__(modname, {}, {}, filter(None, [attrname]))
Last edited 10 years ago by Jun Omae (previous) (diff)

comment:7 Changed 10 years ago by Olemis Lang

Overall , I agree with all the suggestions provided in comment:6 , comment:5 . I'm still not sure about comment:4 , but at first sight it looks fine to me . I'll take a look into this issue in the next few days .

Changed 10 years ago by Jun Omae

Attachment: t11907.diff added

comment:8 in reply to:  6 Changed 10 years ago by Jun Omae

Another thing: I think /developer/api should require TRAC_DEVELOP permission. Currently, anyone can let to import any objects using the feature.

Reconsidered about that, I think it would safe to use sys.modules rather than __import__. See attachment:t11907.diff.

comment:9 Changed 9 years ago by Ryan J Ollos

Resolution: fixed
Status: newclosed

In 14481:

TracDeveloper 0.3.0dev: Fix UnicodeEncode error rendering API docs

Fixes #11907.

comment:10 Changed 9 years ago by Ryan J Ollos

Owner: changed from Olemis Lang to Jun Omae

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain Jun Omae.
The resolution will be deleted. Next status will be 'reopened'.

Add Comment


E-mail address and name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.