Skip to content

Invalid Color Input Causes Crash #1704

@aaricantto

Description

@aaricantto

🐛 Bug Report: complementary() crashes with ValueError on invalid hex color input

Summary

WebODM throws an unhandled exception when theme color input is not a valid 6-digit hex string. The error occurs in the complementary() function located in webodm/app/templatetags/settings.py.


🔥 Stack Trace

ValueError: invalid literal for int() with base 16: 'bl'

📍 Location of Error

The issue originates from the following line:

comp = ['%02X' % (255 - int(a, 16)) for a in rgb]

It assumes the input is a valid hex string (e.g., #AABBCC), but if the input is "blue", "blabla", "bl", or anything invalid, it causes a crash.


💥 Problematic Code

Here is the current version of the function:

@register.simple_tag
def complementary(hexcolor):
    """Returns complementary RGB color
    Example: complementaryColor('#FFFFFF') --> '#000000'
    """
    if hexcolor[0] == '#':
        hexcolor = hexcolor[1:]
    rgb = (hexcolor[0:2], hexcolor[2:4], hexcolor[4:6])
    comp = ['%02X' % (255 - int(a, 16)) for a in rgb]
    return '#' + ''.join(comp)

✅ Recommended Fix

Here’s a more robust version that validates the input and prevents crashes:

@register.simple_tag
def complementary(hexcolor):
    """Returns complementary RGB color"""
    try:
        if hexcolor.startswith('#'):
            hexcolor = hexcolor[1:]
        if len(hexcolor) != 6:
            raise ValueError("Invalid hex color length")
        rgb = (hexcolor[0:2], hexcolor[2:4], hexcolor[4:6])
        comp = ['%02X' % (255 - int(a, 16)) for a in rgb]
        return '#' + ''.join(comp)
    except Exception as e:
        logger.warning(f"complementary() failed with input '{hexcolor}': {e}")
        return '#FFFFFF'  # fallback to white

🧪 Steps to Reproduce

  1. Set a theme color in the environment, database, or UI to blue, blabla, or other invalid value.
  2. Load a page that uses {% complementary theme_color %}.
  3. Observe a ValueError traceback in the logs and a 500 error in the browser.

💻 Environment

  • WebODM Version: webodm_webapp:latest (Docker)
  • Deployment: Docker Swarm
  • Python: 3.8+
  • Customization: Using THEME_COLOR_PRIMARY=blue in .env

💡 Suggested Action

  • Add the validation fix shown above to complementary().
  • Optionally sanitize theme color values wherever they are configured or stored.
  • Consider applying a default/fallback color (e.g., #FFFFFF) when inputs are invalid.

❤️ Thanks!

Thank you for the awesome project! WebODM is an incredibly valuable open-source tool for drone data processing. I'd be happy to submit a PR with this fix if you'd prefer it that way

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions