~ubuntu-branches/ubuntu/vivid/moin/vivid

« back to all changes in this revision

Viewing changes to MoinMoin/support/passlib/exc.py

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2014-01-07 21:33:21 UTC
  • mfrom: (0.1.34 sid)
  • Revision ID: package-import@ubuntu.com-20140107213321-574mr13z2oebjgms
Tags: 1.9.7-1ubuntu1
* Merge with Debian; remaining changes:
* debian/control:
  - remove python-xml from Suggests field, the package isn't in
    sys.path any more.
  - demote fckeditor from Recommends to Suggests; the code was previously
    embedded in moin, but it was also disabled, so there's no reason for us
    to pull this in by default currently. Note: fckeditor has a number of
    security problems and so this change probably needs to be carried
    indefinitely.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""passlib.exc -- exceptions & warnings raised by passlib"""
 
2
#=============================================================================
 
3
# exceptions
 
4
#=============================================================================
 
5
class MissingBackendError(RuntimeError):
 
6
    """Error raised if multi-backend handler has no available backends;
 
7
    or if specifically requested backend is not available.
 
8
 
 
9
    :exc:`!MissingBackendError` derives
 
10
    from :exc:`RuntimeError`, since it usually indicates
 
11
    lack of an external library or OS feature.
 
12
    This is primarily raised by handlers which depend on
 
13
    external libraries (which is currently just
 
14
    :class:`~passlib.hash.bcrypt`).
 
15
    """
 
16
 
 
17
class PasswordSizeError(ValueError):
 
18
    """Error raised if a password exceeds the maximum size allowed
 
19
    by Passlib (4096 characters).
 
20
 
 
21
    Many password hash algorithms take proportionately larger amounts of time and/or
 
22
    memory depending on the size of the password provided. This could present
 
23
    a potential denial of service (DOS) situation if a maliciously large
 
24
    password is provided to an application. Because of this, Passlib enforces
 
25
    a maximum size limit, but one which should be *much* larger
 
26
    than any legitimate password. :exc:`!PasswordSizeError` derives
 
27
    from :exc:`!ValueError`.
 
28
 
 
29
    .. note::
 
30
        Applications wishing to use a different limit should set the
 
31
        ``PASSLIB_MAX_PASSWORD_SIZE`` environmental variable before
 
32
        Passlib is loaded. The value can be any large positive integer.
 
33
 
 
34
    .. versionadded:: 1.6
 
35
    """
 
36
    def __init__(self):
 
37
        ValueError.__init__(self, "password exceeds maximum allowed size")
 
38
 
 
39
    # this also prevents a glibc crypt segfault issue, detailed here ...
 
40
    # http://www.openwall.com/lists/oss-security/2011/11/15/1
 
41
 
 
42
#=============================================================================
 
43
# warnings
 
44
#=============================================================================
 
45
class PasslibWarning(UserWarning):
 
46
    """base class for Passlib's user warnings.
 
47
 
 
48
    .. versionadded:: 1.6
 
49
    """
 
50
 
 
51
class PasslibConfigWarning(PasslibWarning):
 
52
    """Warning issued when non-fatal issue is found related to the configuration
 
53
    of a :class:`~passlib.context.CryptContext` instance.
 
54
 
 
55
    This occurs primarily in one of two cases:
 
56
 
 
57
    * The CryptContext contains rounds limits which exceed the hard limits
 
58
      imposed by the underlying algorithm.
 
59
    * An explicit rounds value was provided which exceeds the limits
 
60
      imposed by the CryptContext.
 
61
 
 
62
    In both of these cases, the code will perform correctly & securely;
 
63
    but the warning is issued as a sign the configuration may need updating.
 
64
    """
 
65
 
 
66
class PasslibHashWarning(PasslibWarning):
 
67
    """Warning issued when non-fatal issue is found with parameters
 
68
    or hash string passed to a passlib hash class.
 
69
 
 
70
    This occurs primarily in one of two cases:
 
71
 
 
72
    * A rounds value or other setting was explicitly provided which
 
73
      exceeded the handler's limits (and has been clamped
 
74
      by the :ref:`relaxed<relaxed-keyword>` flag).
 
75
 
 
76
    * A malformed hash string was encountered which (while parsable)
 
77
      should be re-encoded.
 
78
    """
 
79
 
 
80
class PasslibRuntimeWarning(PasslibWarning):
 
81
    """Warning issued when something unexpected happens during runtime.
 
82
 
 
83
    The fact that it's a warning instead of an error means Passlib
 
84
    was able to correct for the issue, but that it's anonmalous enough
 
85
    that the developers would love to hear under what conditions it occurred.
 
86
    """
 
87
 
 
88
class PasslibSecurityWarning(PasslibWarning):
 
89
    """Special warning issued when Passlib encounters something
 
90
    that might affect security.
 
91
    """
 
92
 
 
93
#=============================================================================
 
94
# error constructors
 
95
#
 
96
# note: these functions are used by the hashes in Passlib to raise common
 
97
# error messages. They are currently just functions which return ValueError,
 
98
# rather than subclasses of ValueError, since the specificity isn't needed
 
99
# yet; and who wants to import a bunch of error classes when catching
 
100
# ValueError will do?
 
101
#=============================================================================
 
102
 
 
103
def _get_name(handler):
 
104
    return handler.name if handler else "<unnamed>"
 
105
 
 
106
#------------------------------------------------------------------------
 
107
# generic helpers
 
108
#------------------------------------------------------------------------
 
109
def type_name(value):
 
110
    "return pretty-printed string containing name of value's type"
 
111
    cls = value.__class__
 
112
    if cls.__module__ and cls.__module__ not in ["__builtin__", "builtins"]:
 
113
        return "%s.%s" % (cls.__module__, cls.__name__)
 
114
    elif value is None:
 
115
        return 'None'
 
116
    else:
 
117
        return cls.__name__
 
118
 
 
119
def ExpectedTypeError(value, expected, param):
 
120
    "error message when param was supposed to be one type, but found another"
 
121
    # NOTE: value is never displayed, since it may sometimes be a password.
 
122
    name = type_name(value)
 
123
    return TypeError("%s must be %s, not %s" % (param, expected, name))
 
124
 
 
125
def ExpectedStringError(value, param):
 
126
    "error message when param was supposed to be unicode or bytes"
 
127
    return ExpectedTypeError(value, "unicode or bytes", param)
 
128
 
 
129
#------------------------------------------------------------------------
 
130
# encrypt/verify parameter errors
 
131
#------------------------------------------------------------------------
 
132
def MissingDigestError(handler=None):
 
133
    "raised when verify() method gets passed config string instead of hash"
 
134
    name = _get_name(handler)
 
135
    return ValueError("expected %s hash, got %s config string instead" %
 
136
                     (name, name))
 
137
 
 
138
def NullPasswordError(handler=None):
 
139
    "raised by OS crypt() supporting hashes, which forbid NULLs in password"
 
140
    name = _get_name(handler)
 
141
    return ValueError("%s does not allow NULL bytes in password" % name)
 
142
 
 
143
#------------------------------------------------------------------------
 
144
# errors when parsing hashes
 
145
#------------------------------------------------------------------------
 
146
def InvalidHashError(handler=None):
 
147
    "error raised if unrecognized hash provided to handler"
 
148
    return ValueError("not a valid %s hash" % _get_name(handler))
 
149
 
 
150
def MalformedHashError(handler=None, reason=None):
 
151
    "error raised if recognized-but-malformed hash provided to handler"
 
152
    text = "malformed %s hash" % _get_name(handler)
 
153
    if reason:
 
154
        text = "%s (%s)" % (text, reason)
 
155
    return ValueError(text)
 
156
 
 
157
def ZeroPaddedRoundsError(handler=None):
 
158
    "error raised if hash was recognized but contained zero-padded rounds field"
 
159
    return MalformedHashError(handler, "zero-padded rounds")
 
160
 
 
161
#------------------------------------------------------------------------
 
162
# settings / hash component errors
 
163
#------------------------------------------------------------------------
 
164
def ChecksumSizeError(handler, raw=False):
 
165
    "error raised if hash was recognized, but checksum was wrong size"
 
166
    # TODO: if handler.use_defaults is set, this came from app-provided value,
 
167
    # not from parsing a hash string, might want different error msg.
 
168
    checksum_size = handler.checksum_size
 
169
    unit = "bytes" if raw else "chars"
 
170
    reason = "checksum must be exactly %d %s" % (checksum_size, unit)
 
171
    return MalformedHashError(handler, reason)
 
172
 
 
173
#=============================================================================
 
174
# eof
 
175
#=============================================================================