Edgewall Software
Modify

Opened 3 years ago

Closed 2 years ago

#13485 closed defect (fixed)

Uncaught exception when TRAC_ENV_INDEX_TEMPLATE points to file with no extension

Reported by: pulkomandy@… Owned by: Jun Omae
Priority: low Milestone: 1.4.4
Component: rendering Version: 1.5.3
Severity: normal Keywords:
Cc: Branch:
Release Notes:
  • Fixed IndexError raising when project index template file has no extension.
  • Always enable autoescape for project index template.
API Changes:
Internal Changes:

Description

Hello, I have just migrated my Trac install to Trac 1.5. I had some trouble getting the project index working. The reason was that my template file was named just "template" (without extension).

The new Jinja template system does things differently depending on the file extension (it tests for "html" and a few others). However, if the file has no extension it just raises an exception:

2022-06-05 13:44:31: (mod_fastcgi.c.451) FastCGI-stderr:Traceback (most recent call last):
2022-06-05 13:44:31: (mod_fastcgi.c.451) FastCGI-stderr:  File "/usr/local/lib/python3.10/dist-packages/flup/server/fcgi_base.py", line 576, in run
2022-06-05 13:44:31: (mod_fastcgi.c.451) FastCGI-stderr:    protocolStatus, appStatus = self.server.handler(self)
2022-06-05 13:44:31: (mod_fastcgi.c.451) FastCGI-stderr:  File "/usr/local/lib/python3.10/dist-packages/flup/server/fcgi_base.py", line 1175, in handler
2022-06-05 13:44:31: (mod_fastcgi.c.451) FastCGI-stderr:    result = self.application(environ, start_response)
2022-06-05 13:44:31: (mod_fastcgi.c.451) FastCGI-stderr:  File "/usr/lib/python3/dist-packages/trac/web/fcgi_frontend.py", line 36, in __call__
2022-06-05 13:44:31: (mod_fastcgi.c.451) FastCGI-stderr:    return self.application(environ, start_response)
2022-06-05 13:44:31: (mod_fastcgi.c.451) FastCGI-stderr:  File "/usr/lib/python3/dist-packages/trac/web/main.py", line 528, in dispatch_request
2022-06-05 13:44:31: (mod_fastcgi.c.451) FastCGI-stderr:    send_project_index(environ, start_response, env_parent_dir,
2022-06-05 13:44:31: (mod_fastcgi.c.451) FastCGI-stderr:  File "/usr/lib/python3/dist-packages/trac/web/main.py", line 846, in send_project_index
2022-06-05 13:44:31: (mod_fastcgi.c.451) FastCGI-stderr:    tmpl = jenv.get_template(template)
2022-06-05 13:44:31: (mod_fastcgi.c.451) FastCGI-stderr:  File "/usr/local/lib/python3.10/dist-packages/jinja2/environment.py", line 883, in get_template
2022-06-05 13:44:31: (mod_fastcgi.c.451) FastCGI-stderr:    return self._load_template(name, self.make_globals(globals))
2022-06-05 13:44:31: (mod_fastcgi.c.451) FastCGI-stderr:  File "/usr/local/lib/python3.10/dist-packages/jinja2/environment.py", line 857, in _load_template
2022-06-05 13:44:31: (mod_fastcgi.c.451) FastCGI-stderr:    template = self.loader.load(self, name, globals)
2022-06-05 13:44:31: (mod_fastcgi.c.451) FastCGI-stderr:  File "/usr/local/lib/python3.10/dist-packages/jinja2/loaders.py", line 127, in load
2022-06-05 13:44:31: (mod_fastcgi.c.451) FastCGI-stderr:    code = environment.compile(source, name, filename)
2022-06-05 13:44:31: (mod_fastcgi.c.451) FastCGI-stderr:  File "/usr/local/lib/python3.10/dist-packages/jinja2/environment.py", line 629, in compile
2022-06-05 13:44:31: (mod_fastcgi.c.451) FastCGI-stderr:    source = self._generate(source, name, filename, defer_init=defer_init)
2022-06-05 13:44:31: (mod_fastcgi.c.451) FastCGI-stderr:  File "/usr/local/lib/python3.10/dist-packages/jinja2/environment.py", line 586, in _generate
2022-06-05 13:44:31: (mod_fastcgi.c.451) FastCGI-stderr:    return generate(
2022-06-05 13:44:31: (mod_fastcgi.c.451) FastCGI-stderr:  File "/usr/local/lib/python3.10/dist-packages/jinja2/compiler.py", line 87, in generate
2022-06-05 13:44:31: (mod_fastcgi.c.451) FastCGI-stderr:    generator.visit(node)
2022-06-05 13:44:31: (mod_fastcgi.c.451) FastCGI-stderr:  File "/usr/local/lib/python3.10/dist-packages/jinja2/visitor.py", line 32, in visit
2022-06-05 13:44:31: (mod_fastcgi.c.451) FastCGI-stderr:    return f(node, *args, **kwargs)
2022-06-05 13:44:31: (mod_fastcgi.c.451) FastCGI-stderr:  File "/usr/local/lib/python3.10/dist-packages/jinja2/compiler.py", line 714, in visit_Template
2022-06-05 13:44:31: (mod_fastcgi.c.451) FastCGI-stderr:    eval_ctx = EvalContext(self.environment, self.name)
2022-06-05 13:44:31: (mod_fastcgi.c.451) FastCGI-stderr:  File "/usr/local/lib/python3.10/dist-packages/jinja2/nodes.py", line 69, in __init__
2022-06-05 13:44:31: (mod_fastcgi.c.451) FastCGI-stderr:    self.autoescape = environment.autoescape(template_name)
2022-06-05 13:44:31: (mod_fastcgi.c.451) FastCGI-stderr:  File "/usr/lib/python3/dist-packages/trac/util/text.py", line 60, in autoescape_extensions
2022-06-05 13:44:31: (mod_fastcgi.c.451) FastCGI-stderr:    return template and template.rsplit('.', 1)[1] in exts
2022-06-05 13:44:31: (mod_fastcgi.c.451) FastCGI-stderr:IndexError: list index out of range

I had to look at the sourcecode to understand what was going wrong. Maybe unknown extensions should default to html, or at least the exception should be caught and an error page shown in the web browser so the info is not hidden in webserver logs.

After renaming my template, things are working fine.

Attachments (0)

Change History (3)

comment:1 by Jun Omae, 2 years ago

Component: generalrendering
Milestone: 1.4.4

Thanks for the reporting!

  • trac/util/text.py

    diff --git a/trac/util/text.py b/trac/util/text.py
    index 67a81d91b..f5baf9aa0 100644
    a b def jinja2env(**kwargs):  
    5252    :rtype: `jinja.Environment`
    5353
    5454    """
    55     exts = ('html', 'rss', 'xml')
     55    exts = ('.html', '.rss', '.xml')
    5656    def filterout_none(v):
    5757        return '' if v is None else v
    5858    def autoescape_extensions(template):
    59         return template and template.rsplit('.', 1)[1] in exts
     59        return template and template.endswith(exts)
    6060    defaults = dict(
    6161        variable_start_string='${',
    6262        variable_end_string='}',

comment:2 by Jun Omae, 2 years ago

Owner: set to Jun Omae
Status: newassigned

Another issue, the project index page sends contents as text/html or text/xml. We should enable autoescape even if the template file has no extension.

  • trac/web/main.py

    def send_project_index(environ, start_response, pa  
    870870
    871871    data['projects'] = projects
    872872
    873     jenv = jinja2env(loader=FileSystemLoader(loadpaths))
     873    jenv = jinja2env(loader=FileSystemLoader(loadpaths), autoescape=True)
    874874    jenv.globals.update(translation.functions)
    875875    tmpl = jenv.get_template(template)
    876876    output = valid_html_bytes(tmpl.render(**data).encode('utf-8'))

comment:3 by Jun Omae, 2 years ago

Release Notes: modified (diff)
Resolution: fixed
Status: assignedclosed

Committed and merged in [17642-17643].

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'.
to The owner will be changed from Jun Omae to the specified user.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.