import traceback


def safer_format_traceback(exc_typ, exc_val, exc_tb):
    """
    Safely format an exception traceback into a safe string.

    There are common attacks that try to write arbitrary data to a server's
    log files.  This can happen if, for instance, a malicious user triggers
    a ValueError with a carefully-crafted payload.

    This function formats the traceback using "%r" for the actual exception
    data, which passes it through repr() so that any special chars are
    safely escaped.
    """
    lines = ["Uncaught exception:\n"]
    lines.extend(traceback.format_tb(exc_tb))
    lines.append("%r\n" % (exc_typ,))
    lines.append("%r\n" % (exc_val,))
    return "".join(lines)