summaryrefslogtreecommitdiff
path: root/passlib/exc.py
blob: 70347eea5803f6877d9f55a28fc4c8a0eae90f94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""passlib.exc -- exceptions & warnings raised by passlib"""
#==========================================================================
# exceptions
#==========================================================================
class MissingBackendError(RuntimeError):
    """Error raised if multi-backend handler has no available backends;
    or if specifically requested backend is not available.

    :exc:`!MissingBackendError` derives
    from :exc:`RuntimeError`, since this usually indicates
    lack of an external library or OS feature.

    This is primarily used by handlers which derive
    from :class:`~passlib.utils.handlers.HasManyBackends`.
    """

class PasswordSizeError(ValueError):
    """Error raised if the password provided exceeds the limit set by Passlib.

    Many password hashes take proportionately larger amounts of
    time and/or memory depending on the size of the password provided.
    This could present a potential denial of service (DOS) situation
    if a maliciously large password was provided to the application.

    Because of this, Passlib enforces a maximum of 4096 characters.
    This error will be thrown if a password larger than
    this is provided to any of the hashes in Passlib.

    Applications wishing to use a different limit should set the
    ``PASSLIB_MAX_PASSWORD_SIZE`` environmental variable before Passlib
    is loaded.
    """
    def __init__(self):
        ValueError.__init__(self, "password exceeds maximum allowed size")

    # this also prevents a glibc crypt segfault issue, detailed here ...
    # http://www.openwall.com/lists/oss-security/2011/11/15/1

#==========================================================================
# warnings
#==========================================================================
class PasslibWarning(UserWarning):
    """base class for Passlib's user warnings"""

class PasslibConfigWarning(PasslibWarning):
    """Warning issued when non-fatal issue is found related to the configuration
    of a :class:`~passlib.context.CryptContext` instance.

    This occurs primarily in one of two cases:

    * the policy contains rounds limits which exceed the hard limits
      imposed by the underlying algorithm.
    * an explicit rounds value was provided which exceeds the limits
      imposed by the policy.

    In both of these cases, the code will perform correctly & securely;
    but the warning is issued as a sign the configuration may need updating.
    """

class PasslibHashWarning(PasslibWarning):
    """Warning issued when non-fatal issue is found with parameters
    or hash string passed to a passlib hash class.

    This occurs primarily in one of two cases:

    * a rounds value or other setting was explicitly provided which
      exceeded the handler's limits (and has been clamped).

    * a hash malformed hash string was encountered, which while parsable,
      should be re-encoded.
    """

class PasslibRuntimeWarning(PasslibWarning):
    """Warning issued when something unexpected happens during runtime.

    The fact that it's a warning instead of an error means Passlib
    was able to correct for the issue, but that it's anonmalous enough
    that the developers would love to hear under what conditions it occurred.
    """

class PasslibSecurityWarning(PasslibWarning):
    """Special warning issued when Passlib encounters something
    that might affect security.

    The main reason this is issued is when Passlib's pure-python bcrypt
    backend is used, to warn that it's 20x too slow to acheive real security.
    """

#==========================================================================
# error constructors
#
# note: these functions are used by the hashes in Passlib to raise common
# error messages. They are currently just functions which return ValueError,
# rather than subclasses of ValueError, since the specificity isn't needed
# yet; and who wants to import a bunch of error classes when catching
# ValueError will do?
#==========================================================================

def _get_name(handler):
    return handler.name if handler else "<unnamed>"

#----------------------------------------------------------------
# encrypt/verify parameter errors
#----------------------------------------------------------------
def MissingHashError(handler=None):
    "error raised if no hash provided to handler"
    return ValueError("no hash specified")

def MissingDigestError(handler=None):
    "raised when verify() method gets passed config string instead of hash"
    name = _get_name(handler)
    return ValueError("expected %s hash, got %s config string instead" %
                     (name, name))

#----------------------------------------------------------------
# errors when parsing hashes
#----------------------------------------------------------------
def InvalidHashError(handler=None):
    "error raised if unrecognized hash provided to handler"
    raise ValueError("not a valid %s hash" % _get_name(handler))

def MalformedHashError(handler=None, reason=None):
    "error raised if recognized-but-malformed hash provided to handler"
    text = "malformed %s hash" % _get_name(handler)
    if reason:
        text = "%s (%s)" % (text, reason)
    raise ValueError(text)

def ZeroPaddedRoundsError(handler=None):
    "error raised if hash was recognized but contained zero-padded rounds field"
    return MalformedHashError(handler, "zero-padded rounds")

#----------------------------------------------------------------
# settings / hash component errors
#----------------------------------------------------------------
def ChecksumSizeError(handler, raw=False):
    "error raised if hash was recognized, but checksum was wrong size"
    checksum_size = handler.checksum_size
    unit = "bytes" if raw else "chars"
    return ValueError("checksum wrong size (%s checksum must be "
                     "exactly %d %s" % (handler.name, checksum_size, unit))

#==========================================================================
# eof
#==========================================================================