jelly.py 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  1. # -*- test-case-name: twisted.spread.test.test_jelly -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. S-expression-based persistence of python objects.
  6. It does something very much like L{Pickle<pickle>}; however, pickle's main goal
  7. seems to be efficiency (both in space and time); jelly's main goals are
  8. security, human readability, and portability to other environments.
  9. This is how Jelly converts various objects to s-expressions.
  10. Boolean::
  11. True --> ['boolean', 'true']
  12. Integer::
  13. 1 --> 1
  14. List::
  15. [1, 2] --> ['list', 1, 2]
  16. String::
  17. \"hello\" --> \"hello\"
  18. Float::
  19. 2.3 --> 2.3
  20. Dictionary::
  21. {'a': 1, 'b': 'c'} --> ['dictionary', ['b', 'c'], ['a', 1]]
  22. Module::
  23. UserString --> ['module', 'UserString']
  24. Class::
  25. UserString.UserString --> ['class', ['module', 'UserString'], 'UserString']
  26. Function::
  27. string.join --> ['function', 'join', ['module', 'string']]
  28. Instance: s is an instance of UserString.UserString, with a __dict__
  29. {'data': 'hello'}::
  30. [\"UserString.UserString\", ['dictionary', ['data', 'hello']]]
  31. Class Method: UserString.UserString.center::
  32. ['method', 'center', ['None'], ['class', ['module', 'UserString'],
  33. 'UserString']]
  34. Instance Method: s.center, where s is an instance of UserString.UserString::
  35. ['method', 'center', ['instance', ['reference', 1, ['class',
  36. ['module', 'UserString'], 'UserString']], ['dictionary', ['data', 'd']]],
  37. ['dereference', 1]]
  38. The C{set} builtin and the C{sets.Set} class are serialized to the same
  39. thing, and unserialized to C{set} if available, else to C{sets.Set}. It means
  40. that there's a possibility of type switching in the serialization process. The
  41. solution is to always use C{set}.
  42. The same rule applies for C{frozenset} and C{sets.ImmutableSet}.
  43. @author: Glyph Lefkowitz
  44. """
  45. # System Imports
  46. import types
  47. import warnings
  48. import decimal
  49. from functools import reduce
  50. import copy
  51. import datetime
  52. try:
  53. from types import (ClassType as _OldStyleClass,
  54. InstanceType as _OldStyleInstance)
  55. except ImportError:
  56. # On Python 3 and higher, ClassType and InstanceType
  57. # are gone. Use an empty tuple to pass to isinstance()
  58. # tests without throwing an exception.
  59. _OldStyleClass = ()
  60. _OldStyleInstance = ()
  61. _SetTypes = [set]
  62. _ImmutableSetTypes = [frozenset]
  63. with warnings.catch_warnings():
  64. warnings.simplefilter("ignore", category=DeprecationWarning)
  65. try:
  66. import sets as _sets
  67. except ImportError:
  68. # sets module is deprecated in Python 2.6, and gone in
  69. # Python 3
  70. _sets = None
  71. else:
  72. _SetTypes.append(_sets.Set)
  73. _ImmutableSetTypes.append(_sets.ImmutableSet)
  74. from zope.interface import implementer
  75. # Twisted Imports
  76. from twisted.python.compat import unicode, long, nativeString
  77. from twisted.python.reflect import namedObject, qual, namedAny
  78. from twisted.persisted.crefutil import NotKnown, _Tuple, _InstanceMethod
  79. from twisted.persisted.crefutil import _DictKeyAndValue, _Dereference
  80. from twisted.persisted.crefutil import _Container
  81. from twisted.spread.interfaces import IJellyable, IUnjellyable
  82. from twisted.python.compat import _PY3
  83. from twisted.python.deprecate import deprecatedModuleAttribute
  84. from incremental import Version
  85. DictTypes = (dict,)
  86. None_atom = b"None" # N
  87. # code
  88. class_atom = b"class" # c
  89. module_atom = b"module" # m
  90. function_atom = b"function" # f
  91. # references
  92. dereference_atom = b'dereference' # D
  93. persistent_atom = b'persistent' # p
  94. reference_atom = b'reference' # r
  95. # mutable collections
  96. dictionary_atom = b"dictionary" # d
  97. list_atom = b'list' # l
  98. set_atom = b'set'
  99. # immutable collections
  100. # (assignment to __dict__ and __class__ still might go away!)
  101. tuple_atom = b"tuple" # t
  102. instance_atom = b'instance' # i
  103. frozenset_atom = b'frozenset'
  104. deprecatedModuleAttribute(
  105. Version("Twisted", 15, 0, 0),
  106. "instance_atom is unused within Twisted.",
  107. "twisted.spread.jelly", "instance_atom")
  108. # errors
  109. unpersistable_atom = b"unpersistable"# u
  110. unjellyableRegistry = {}
  111. unjellyableFactoryRegistry = {}
  112. def _createBlank(cls):
  113. """
  114. Given an object, if that object is a type (or a legacy old-style class),
  115. return a new, blank instance of that type which has not had C{__init__}
  116. called on it. If the object is not a type, return C{None}.
  117. @param cls: The type (or class) to create an instance of.
  118. @type cls: L{_OldStyleClass}, L{type}, or something else that cannot be
  119. instantiated.
  120. @return: a new blank instance or L{None} if C{cls} is not a class or type.
  121. """
  122. if isinstance(cls, type):
  123. return cls.__new__(cls)
  124. if not _PY3 and isinstance(cls, _OldStyleClass):
  125. return _OldStyleInstance(cls)
  126. def _newInstance(cls, state):
  127. """
  128. Make a new instance of a class without calling its __init__ method.
  129. Supports both new- and old-style classes.
  130. @param state: A C{dict} used to update C{inst.__dict__} either directly or
  131. via C{__setstate__}, if available.
  132. @return: A new instance of C{cls}.
  133. """
  134. instance = _createBlank(cls)
  135. def defaultSetter(state):
  136. instance.__dict__ = state
  137. setter = getattr(instance, "__setstate__", defaultSetter)
  138. setter(state)
  139. return instance
  140. def _maybeClass(classnamep):
  141. isObject = isinstance(classnamep, type)
  142. if isObject or ((not _PY3) and isinstance(classnamep, _OldStyleClass)):
  143. classnamep = qual(classnamep)
  144. if not isinstance(classnamep, bytes):
  145. classnamep = classnamep.encode('utf-8')
  146. return classnamep
  147. def setUnjellyableForClass(classname, unjellyable):
  148. """
  149. Set which local class will represent a remote type.
  150. If you have written a Copyable class that you expect your client to be
  151. receiving, write a local "copy" class to represent it, then call::
  152. jellier.setUnjellyableForClass('module.package.Class', MyCopier).
  153. Call this at the module level immediately after its class
  154. definition. MyCopier should be a subclass of RemoteCopy.
  155. The classname may be a special tag returned by
  156. 'Copyable.getTypeToCopyFor' rather than an actual classname.
  157. This call is also for cached classes, since there will be no
  158. overlap. The rules are the same.
  159. """
  160. global unjellyableRegistry
  161. classname = _maybeClass(classname)
  162. unjellyableRegistry[classname] = unjellyable
  163. globalSecurity.allowTypes(classname)
  164. def setUnjellyableFactoryForClass(classname, copyFactory):
  165. """
  166. Set the factory to construct a remote instance of a type::
  167. jellier.setUnjellyableFactoryForClass('module.package.Class', MyFactory)
  168. Call this at the module level immediately after its class definition.
  169. C{copyFactory} should return an instance or subclass of
  170. L{RemoteCopy<pb.RemoteCopy>}.
  171. Similar to L{setUnjellyableForClass} except it uses a factory instead
  172. of creating an instance.
  173. """
  174. global unjellyableFactoryRegistry
  175. classname = _maybeClass(classname)
  176. unjellyableFactoryRegistry[classname] = copyFactory
  177. globalSecurity.allowTypes(classname)
  178. def setUnjellyableForClassTree(module, baseClass, prefix=None):
  179. """
  180. Set all classes in a module derived from C{baseClass} as copiers for
  181. a corresponding remote class.
  182. When you have a hierarchy of Copyable (or Cacheable) classes on one
  183. side, and a mirror structure of Copied (or RemoteCache) classes on the
  184. other, use this to setUnjellyableForClass all your Copieds for the
  185. Copyables.
  186. Each copyTag (the \"classname\" argument to getTypeToCopyFor, and
  187. what the Copyable's getTypeToCopyFor returns) is formed from
  188. adding a prefix to the Copied's class name. The prefix defaults
  189. to module.__name__. If you wish the copy tag to consist of solely
  190. the classname, pass the empty string \'\'.
  191. @param module: a module object from which to pull the Copied classes.
  192. (passing sys.modules[__name__] might be useful)
  193. @param baseClass: the base class from which all your Copied classes derive.
  194. @param prefix: the string prefixed to classnames to form the
  195. unjellyableRegistry.
  196. """
  197. if prefix is None:
  198. prefix = module.__name__
  199. if prefix:
  200. prefix = "%s." % prefix
  201. for name in dir(module):
  202. loaded = getattr(module, name)
  203. try:
  204. yes = issubclass(loaded, baseClass)
  205. except TypeError:
  206. "It's not a class."
  207. else:
  208. if yes:
  209. setUnjellyableForClass('%s%s' % (prefix, name), loaded)
  210. def getInstanceState(inst, jellier):
  211. """
  212. Utility method to default to 'normal' state rules in serialization.
  213. """
  214. if hasattr(inst, "__getstate__"):
  215. state = inst.__getstate__()
  216. else:
  217. state = inst.__dict__
  218. sxp = jellier.prepare(inst)
  219. sxp.extend([qual(inst.__class__).encode('utf-8'), jellier.jelly(state)])
  220. return jellier.preserve(inst, sxp)
  221. def setInstanceState(inst, unjellier, jellyList):
  222. """
  223. Utility method to default to 'normal' state rules in unserialization.
  224. """
  225. state = unjellier.unjelly(jellyList[1])
  226. if hasattr(inst, "__setstate__"):
  227. inst.__setstate__(state)
  228. else:
  229. inst.__dict__ = state
  230. return inst
  231. class Unpersistable:
  232. """
  233. This is an instance of a class that comes back when something couldn't be
  234. unpersisted.
  235. """
  236. def __init__(self, reason):
  237. """
  238. Initialize an unpersistable object with a descriptive C{reason} string.
  239. """
  240. self.reason = reason
  241. def __repr__(self):
  242. return "Unpersistable(%s)" % repr(self.reason)
  243. @implementer(IJellyable)
  244. class Jellyable:
  245. """
  246. Inherit from me to Jelly yourself directly with the `getStateFor'
  247. convenience method.
  248. """
  249. def getStateFor(self, jellier):
  250. return self.__dict__
  251. def jellyFor(self, jellier):
  252. """
  253. @see: L{twisted.spread.interfaces.IJellyable.jellyFor}
  254. """
  255. sxp = jellier.prepare(self)
  256. sxp.extend([
  257. qual(self.__class__).encode('utf-8'),
  258. jellier.jelly(self.getStateFor(jellier))])
  259. return jellier.preserve(self, sxp)
  260. @implementer(IUnjellyable)
  261. class Unjellyable:
  262. """
  263. Inherit from me to Unjelly yourself directly with the
  264. C{setStateFor} convenience method.
  265. """
  266. def setStateFor(self, unjellier, state):
  267. self.__dict__ = state
  268. def unjellyFor(self, unjellier, jellyList):
  269. """
  270. Perform the inverse operation of L{Jellyable.jellyFor}.
  271. @see: L{twisted.spread.interfaces.IUnjellyable.unjellyFor}
  272. """
  273. state = unjellier.unjelly(jellyList[1])
  274. self.setStateFor(unjellier, state)
  275. return self
  276. class _Jellier:
  277. """
  278. (Internal) This class manages state for a call to jelly()
  279. """
  280. def __init__(self, taster, persistentStore, invoker):
  281. """
  282. Initialize.
  283. """
  284. self.taster = taster
  285. # `preserved' is a dict of previously seen instances.
  286. self.preserved = {}
  287. # `cooked' is a dict of previously backreferenced instances to their
  288. # `ref' lists.
  289. self.cooked = {}
  290. self.cooker = {}
  291. self._ref_id = 1
  292. self.persistentStore = persistentStore
  293. self.invoker = invoker
  294. def _cook(self, object):
  295. """
  296. (internal) Backreference an object.
  297. Notes on this method for the hapless future maintainer: If I've already
  298. gone through the prepare/preserve cycle on the specified object (it is
  299. being referenced after the serializer is \"done with\" it, e.g. this
  300. reference is NOT circular), the copy-in-place of aList is relevant,
  301. since the list being modified is the actual, pre-existing jelly
  302. expression that was returned for that object. If not, it's technically
  303. superfluous, since the value in self.preserved didn't need to be set,
  304. but the invariant that self.preserved[id(object)] is a list is
  305. convenient because that means we don't have to test and create it or
  306. not create it here, creating fewer code-paths. that's why
  307. self.preserved is always set to a list.
  308. Sorry that this code is so hard to follow, but Python objects are
  309. tricky to persist correctly. -glyph
  310. """
  311. aList = self.preserved[id(object)]
  312. newList = copy.copy(aList)
  313. # make a new reference ID
  314. refid = self._ref_id
  315. self._ref_id = self._ref_id + 1
  316. # replace the old list in-place, so that we don't have to track the
  317. # previous reference to it.
  318. aList[:] = [reference_atom, refid, newList]
  319. self.cooked[id(object)] = [dereference_atom, refid]
  320. return aList
  321. def prepare(self, object):
  322. """
  323. (internal) Create a list for persisting an object to. This will allow
  324. backreferences to be made internal to the object. (circular
  325. references).
  326. The reason this needs to happen is that we don't generate an ID for
  327. every object, so we won't necessarily know which ID the object will
  328. have in the future. When it is 'cooked' ( see _cook ), it will be
  329. assigned an ID, and the temporary placeholder list created here will be
  330. modified in-place to create an expression that gives this object an ID:
  331. [reference id# [object-jelly]].
  332. """
  333. # create a placeholder list to be preserved
  334. self.preserved[id(object)] = []
  335. # keep a reference to this object around, so it doesn't disappear!
  336. # (This isn't always necessary, but for cases where the objects are
  337. # dynamically generated by __getstate__ or getStateToCopyFor calls, it
  338. # is; id() will return the same value for a different object if it gets
  339. # garbage collected. This may be optimized later.)
  340. self.cooker[id(object)] = object
  341. return []
  342. def preserve(self, object, sexp):
  343. """
  344. (internal) Mark an object's persistent list for later referral.
  345. """
  346. # if I've been cooked in the meanwhile,
  347. if id(object) in self.cooked:
  348. # replace the placeholder empty list with the real one
  349. self.preserved[id(object)][2] = sexp
  350. # but give this one back.
  351. sexp = self.preserved[id(object)]
  352. else:
  353. self.preserved[id(object)] = sexp
  354. return sexp
  355. constantTypes = {bytes: 1, unicode: 1, int: 1, float: 1, long: 1}
  356. def _checkMutable(self,obj):
  357. objId = id(obj)
  358. if objId in self.cooked:
  359. return self.cooked[objId]
  360. if objId in self.preserved:
  361. self._cook(obj)
  362. return self.cooked[objId]
  363. def jelly(self, obj):
  364. if isinstance(obj, Jellyable):
  365. preRef = self._checkMutable(obj)
  366. if preRef:
  367. return preRef
  368. return obj.jellyFor(self)
  369. objType = type(obj)
  370. if self.taster.isTypeAllowed(qual(objType).encode('utf-8')):
  371. # "Immutable" Types
  372. if ((objType is bytes) or
  373. (objType is int) or
  374. (objType is long) or
  375. (objType is float)):
  376. return obj
  377. elif objType is types.MethodType:
  378. aSelf = obj.__self__ if _PY3 else obj.im_self
  379. aFunc = obj.__func__ if _PY3 else obj.im_func
  380. aClass = aSelf.__class__ if _PY3 else obj.im_class
  381. return [b"method", aFunc.__name__, self.jelly(aSelf),
  382. self.jelly(aClass)]
  383. elif objType is unicode:
  384. return [b'unicode', obj.encode('UTF-8')]
  385. elif objType is type(None):
  386. return [b'None']
  387. elif objType is types.FunctionType:
  388. return [b'function', obj.__module__ + '.' +
  389. (obj.__qualname__ if _PY3 else obj.__name__)]
  390. elif objType is types.ModuleType:
  391. return [b'module', obj.__name__]
  392. elif objType is bool:
  393. return [b'boolean', obj and b'true' or b'false']
  394. elif objType is datetime.datetime:
  395. if obj.tzinfo:
  396. raise NotImplementedError(
  397. "Currently can't jelly datetime objects with tzinfo")
  398. return [b'datetime', ' '.join([unicode(x) for x in (
  399. obj.year, obj.month, obj.day, obj.hour,
  400. obj.minute, obj.second, obj.microsecond)]
  401. ).encode('utf-8')]
  402. elif objType is datetime.time:
  403. if obj.tzinfo:
  404. raise NotImplementedError(
  405. "Currently can't jelly datetime objects with tzinfo")
  406. return [b'time', '%s %s %s %s' % (obj.hour, obj.minute,
  407. obj.second, obj.microsecond)]
  408. elif objType is datetime.date:
  409. return [b'date', '%s %s %s' % (obj.year, obj.month, obj.day)]
  410. elif objType is datetime.timedelta:
  411. return [b'timedelta', '%s %s %s' % (obj.days, obj.seconds,
  412. obj.microseconds)]
  413. elif issubclass(objType, (type, _OldStyleClass)):
  414. return [b'class', qual(obj).encode('utf-8')]
  415. elif objType is decimal.Decimal:
  416. return self.jelly_decimal(obj)
  417. else:
  418. preRef = self._checkMutable(obj)
  419. if preRef:
  420. return preRef
  421. # "Mutable" Types
  422. sxp = self.prepare(obj)
  423. if objType is list:
  424. sxp.extend(self._jellyIterable(list_atom, obj))
  425. elif objType is tuple:
  426. sxp.extend(self._jellyIterable(tuple_atom, obj))
  427. elif objType in DictTypes:
  428. sxp.append(dictionary_atom)
  429. for key, val in obj.items():
  430. sxp.append([self.jelly(key), self.jelly(val)])
  431. elif objType in _SetTypes:
  432. sxp.extend(self._jellyIterable(set_atom, obj))
  433. elif objType in _ImmutableSetTypes:
  434. sxp.extend(self._jellyIterable(frozenset_atom, obj))
  435. else:
  436. className = qual(obj.__class__).encode('utf-8')
  437. persistent = None
  438. if self.persistentStore:
  439. persistent = self.persistentStore(obj, self)
  440. if persistent is not None:
  441. sxp.append(persistent_atom)
  442. sxp.append(persistent)
  443. elif self.taster.isClassAllowed(obj.__class__):
  444. sxp.append(className)
  445. if hasattr(obj, "__getstate__"):
  446. state = obj.__getstate__()
  447. else:
  448. state = obj.__dict__
  449. sxp.append(self.jelly(state))
  450. else:
  451. self.unpersistable(
  452. "instance of class %s deemed insecure" %
  453. qual(obj.__class__), sxp)
  454. return self.preserve(obj, sxp)
  455. else:
  456. if objType is _OldStyleInstance:
  457. raise InsecureJelly("Class not allowed for instance: %s %s" %
  458. (obj.__class__, obj))
  459. raise InsecureJelly("Type not allowed for object: %s %s" %
  460. (objType, obj))
  461. def _jellyIterable(self, atom, obj):
  462. """
  463. Jelly an iterable object.
  464. @param atom: the identifier atom of the object.
  465. @type atom: C{str}
  466. @param obj: any iterable object.
  467. @type obj: C{iterable}
  468. @return: a generator of jellied data.
  469. @rtype: C{generator}
  470. """
  471. yield atom
  472. for item in obj:
  473. yield self.jelly(item)
  474. def jelly_decimal(self, d):
  475. """
  476. Jelly a decimal object.
  477. @param d: a decimal object to serialize.
  478. @type d: C{decimal.Decimal}
  479. @return: jelly for the decimal object.
  480. @rtype: C{list}
  481. """
  482. sign, guts, exponent = d.as_tuple()
  483. value = reduce(lambda left, right: left * 10 + right, guts)
  484. if sign:
  485. value = -value
  486. return [b'decimal', value, exponent]
  487. def unpersistable(self, reason, sxp=None):
  488. """
  489. (internal) Returns an sexp: (unpersistable "reason"). Utility method
  490. for making note that a particular object could not be serialized.
  491. """
  492. if sxp is None:
  493. sxp = []
  494. sxp.append(unpersistable_atom)
  495. if isinstance(reason, unicode):
  496. reason = reason.encode("utf-8")
  497. sxp.append(reason)
  498. return sxp
  499. class _Unjellier:
  500. def __init__(self, taster, persistentLoad, invoker):
  501. self.taster = taster
  502. self.persistentLoad = persistentLoad
  503. self.references = {}
  504. self.postCallbacks = []
  505. self.invoker = invoker
  506. def unjellyFull(self, obj):
  507. o = self.unjelly(obj)
  508. for m in self.postCallbacks:
  509. m()
  510. return o
  511. def _maybePostUnjelly(self, unjellied):
  512. """
  513. If the given object has support for the C{postUnjelly} hook, set it up
  514. to be called at the end of deserialization.
  515. @param unjellied: an object that has already been unjellied.
  516. @return: C{unjellied}
  517. """
  518. if hasattr(unjellied, 'postUnjelly'):
  519. self.postCallbacks.append(unjellied.postUnjelly)
  520. return unjellied
  521. def unjelly(self, obj):
  522. if type(obj) is not list:
  523. return obj
  524. jelTypeBytes = obj[0]
  525. if not self.taster.isTypeAllowed(jelTypeBytes):
  526. raise InsecureJelly(jelTypeBytes)
  527. regClass = unjellyableRegistry.get(jelTypeBytes)
  528. if regClass is not None:
  529. method = getattr(_createBlank(regClass), "unjellyFor", regClass)
  530. return self._maybePostUnjelly(method(self, obj))
  531. regFactory = unjellyableFactoryRegistry.get(jelTypeBytes)
  532. if regFactory is not None:
  533. return self._maybePostUnjelly(regFactory(self.unjelly(obj[1])))
  534. jelTypeText = nativeString(jelTypeBytes)
  535. thunk = getattr(self, '_unjelly_%s' % jelTypeText, None)
  536. if thunk is not None:
  537. return thunk(obj[1:])
  538. else:
  539. nameSplit = jelTypeText.split('.')
  540. modName = '.'.join(nameSplit[:-1])
  541. if not self.taster.isModuleAllowed(modName):
  542. raise InsecureJelly(
  543. "Module %s not allowed (in type %s)." % (modName, jelTypeText))
  544. clz = namedObject(jelTypeText)
  545. if not self.taster.isClassAllowed(clz):
  546. raise InsecureJelly("Class %s not allowed." % jelTypeText)
  547. return self._genericUnjelly(clz, obj[1])
  548. def _genericUnjelly(self, cls, state):
  549. """
  550. Unjelly a type for which no specific unjellier is registered, but which
  551. is nonetheless allowed.
  552. @param cls: the class of the instance we are unjellying.
  553. @type cls: L{_OldStyleClass} or L{type}
  554. @param state: The jellied representation of the object's state; its
  555. C{__dict__} unless it has a C{__setstate__} that takes something
  556. else.
  557. @type state: L{list}
  558. @return: the new, unjellied instance.
  559. """
  560. return self._maybePostUnjelly(_newInstance(cls, self.unjelly(state)))
  561. def _unjelly_None(self, exp):
  562. return None
  563. def _unjelly_unicode(self, exp):
  564. return unicode(exp[0], "UTF-8")
  565. def _unjelly_decimal(self, exp):
  566. """
  567. Unjelly decimal objects.
  568. """
  569. value = exp[0]
  570. exponent = exp[1]
  571. if value < 0:
  572. sign = 1
  573. else:
  574. sign = 0
  575. guts = decimal.Decimal(value).as_tuple()[1]
  576. return decimal.Decimal((sign, guts, exponent))
  577. def _unjelly_boolean(self, exp):
  578. if bool:
  579. assert exp[0] in (b'true', b'false')
  580. return exp[0] == b'true'
  581. else:
  582. return Unpersistable("Could not unpersist boolean: %s" % (exp[0],))
  583. def _unjelly_datetime(self, exp):
  584. return datetime.datetime(*map(int, exp[0].split()))
  585. def _unjelly_date(self, exp):
  586. return datetime.date(*map(int, exp[0].split()))
  587. def _unjelly_time(self, exp):
  588. return datetime.time(*map(int, exp[0].split()))
  589. def _unjelly_timedelta(self, exp):
  590. days, seconds, microseconds = map(int, exp[0].split())
  591. return datetime.timedelta(
  592. days=days, seconds=seconds, microseconds=microseconds)
  593. def unjellyInto(self, obj, loc, jel):
  594. o = self.unjelly(jel)
  595. if isinstance(o, NotKnown):
  596. o.addDependant(obj, loc)
  597. obj[loc] = o
  598. return o
  599. def _unjelly_dereference(self, lst):
  600. refid = lst[0]
  601. x = self.references.get(refid)
  602. if x is not None:
  603. return x
  604. der = _Dereference(refid)
  605. self.references[refid] = der
  606. return der
  607. def _unjelly_reference(self, lst):
  608. refid = lst[0]
  609. exp = lst[1]
  610. o = self.unjelly(exp)
  611. ref = self.references.get(refid)
  612. if (ref is None):
  613. self.references[refid] = o
  614. elif isinstance(ref, NotKnown):
  615. ref.resolveDependants(o)
  616. self.references[refid] = o
  617. else:
  618. assert 0, "Multiple references with same ID!"
  619. return o
  620. def _unjelly_tuple(self, lst):
  621. l = list(range(len(lst)))
  622. finished = 1
  623. for elem in l:
  624. if isinstance(self.unjellyInto(l, elem, lst[elem]), NotKnown):
  625. finished = 0
  626. if finished:
  627. return tuple(l)
  628. else:
  629. return _Tuple(l)
  630. def _unjelly_list(self, lst):
  631. l = list(range(len(lst)))
  632. for elem in l:
  633. self.unjellyInto(l, elem, lst[elem])
  634. return l
  635. def _unjellySetOrFrozenset(self, lst, containerType):
  636. """
  637. Helper method to unjelly set or frozenset.
  638. @param lst: the content of the set.
  639. @type lst: C{list}
  640. @param containerType: the type of C{set} to use.
  641. """
  642. l = list(range(len(lst)))
  643. finished = True
  644. for elem in l:
  645. data = self.unjellyInto(l, elem, lst[elem])
  646. if isinstance(data, NotKnown):
  647. finished = False
  648. if not finished:
  649. return _Container(l, containerType)
  650. else:
  651. return containerType(l)
  652. def _unjelly_set(self, lst):
  653. """
  654. Unjelly set using the C{set} builtin.
  655. """
  656. return self._unjellySetOrFrozenset(lst, set)
  657. def _unjelly_frozenset(self, lst):
  658. """
  659. Unjelly frozenset using the C{frozenset} builtin.
  660. """
  661. return self._unjellySetOrFrozenset(lst, frozenset)
  662. def _unjelly_dictionary(self, lst):
  663. d = {}
  664. for k, v in lst:
  665. kvd = _DictKeyAndValue(d)
  666. self.unjellyInto(kvd, 0, k)
  667. self.unjellyInto(kvd, 1, v)
  668. return d
  669. def _unjelly_module(self, rest):
  670. moduleName = nativeString(rest[0])
  671. if type(moduleName) != str:
  672. raise InsecureJelly(
  673. "Attempted to unjelly a module with a non-string name.")
  674. if not self.taster.isModuleAllowed(moduleName):
  675. raise InsecureJelly(
  676. "Attempted to unjelly module named %r" % (moduleName,))
  677. mod = __import__(moduleName, {}, {},"x")
  678. return mod
  679. def _unjelly_class(self, rest):
  680. cname = nativeString(rest[0])
  681. clist = cname.split(nativeString('.'))
  682. modName = nativeString('.').join(clist[:-1])
  683. if not self.taster.isModuleAllowed(modName):
  684. raise InsecureJelly("module %s not allowed" % modName)
  685. klaus = namedObject(cname)
  686. objType = type(klaus)
  687. if objType not in (_OldStyleClass, type):
  688. raise InsecureJelly(
  689. "class %r unjellied to something that isn't a class: %r" % (
  690. cname, klaus))
  691. if not self.taster.isClassAllowed(klaus):
  692. raise InsecureJelly("class not allowed: %s" % qual(klaus))
  693. return klaus
  694. def _unjelly_function(self, rest):
  695. fname = nativeString(rest[0])
  696. modSplit = fname.split(nativeString('.'))
  697. modName = nativeString('.').join(modSplit[:-1])
  698. if not self.taster.isModuleAllowed(modName):
  699. raise InsecureJelly("Module not allowed: %s" % modName)
  700. # XXX do I need an isFunctionAllowed?
  701. function = namedAny(fname)
  702. return function
  703. def _unjelly_persistent(self, rest):
  704. if self.persistentLoad:
  705. pload = self.persistentLoad(rest[0], self)
  706. return pload
  707. else:
  708. return Unpersistable("Persistent callback not found")
  709. def _unjelly_instance(self, rest):
  710. """
  711. (internal) Unjelly an instance.
  712. Called to handle the deprecated I{instance} token.
  713. @param rest: The s-expression representing the instance.
  714. @return: The unjellied instance.
  715. """
  716. warnings.warn_explicit(
  717. "Unjelly support for the instance atom is deprecated since "
  718. "Twisted 15.0.0. Upgrade peer for modern instance support.",
  719. category=DeprecationWarning, filename="", lineno=0)
  720. clz = self.unjelly(rest[0])
  721. if not _PY3 and type(clz) is not _OldStyleClass:
  722. raise InsecureJelly("Legacy 'instance' found with new-style class")
  723. return self._genericUnjelly(clz, rest[1])
  724. def _unjelly_unpersistable(self, rest):
  725. return Unpersistable("Unpersistable data: %s" % (rest[0],))
  726. def _unjelly_method(self, rest):
  727. """
  728. (internal) Unjelly a method.
  729. """
  730. im_name = rest[0]
  731. im_self = self.unjelly(rest[1])
  732. im_class = self.unjelly(rest[2])
  733. if not isinstance(im_class, (type, _OldStyleClass)):
  734. raise InsecureJelly("Method found with non-class class.")
  735. if im_name in im_class.__dict__:
  736. if im_self is None:
  737. im = getattr(im_class, im_name)
  738. elif isinstance(im_self, NotKnown):
  739. im = _InstanceMethod(im_name, im_self, im_class)
  740. else:
  741. im = types.MethodType(im_class.__dict__[im_name], im_self,
  742. *([im_class] * (not _PY3)))
  743. else:
  744. raise TypeError('instance method changed')
  745. return im
  746. #### Published Interface.
  747. class InsecureJelly(Exception):
  748. """
  749. This exception will be raised when a jelly is deemed `insecure'; e.g. it
  750. contains a type, class, or module disallowed by the specified `taster'
  751. """
  752. class DummySecurityOptions:
  753. """
  754. DummySecurityOptions() -> insecure security options
  755. Dummy security options -- this class will allow anything.
  756. """
  757. def isModuleAllowed(self, moduleName):
  758. """
  759. DummySecurityOptions.isModuleAllowed(moduleName) -> boolean
  760. returns 1 if a module by that name is allowed, 0 otherwise
  761. """
  762. return 1
  763. def isClassAllowed(self, klass):
  764. """
  765. DummySecurityOptions.isClassAllowed(class) -> boolean
  766. Assumes the module has already been allowed. Returns 1 if the given
  767. class is allowed, 0 otherwise.
  768. """
  769. return 1
  770. def isTypeAllowed(self, typeName):
  771. """
  772. DummySecurityOptions.isTypeAllowed(typeName) -> boolean
  773. Returns 1 if the given type is allowed, 0 otherwise.
  774. """
  775. return 1
  776. class SecurityOptions:
  777. """
  778. This will by default disallow everything, except for 'none'.
  779. """
  780. basicTypes = ["dictionary", "list", "tuple",
  781. "reference", "dereference", "unpersistable",
  782. "persistent", "long_int", "long", "dict"]
  783. def __init__(self):
  784. """
  785. SecurityOptions() initialize.
  786. """
  787. # I don't believe any of these types can ever pose a security hazard,
  788. # except perhaps "reference"...
  789. self.allowedTypes = {
  790. b"None": 1, b"bool": 1, b"boolean": 1, b"string": 1, b"str": 1,
  791. b"int": 1, b"float": 1, b"datetime": 1, b"time": 1, b"date": 1,
  792. b"timedelta": 1, b"NoneType": 1, b'unicode': 1, b'decimal': 1,
  793. b'set': 1, b'frozenset': 1,
  794. }
  795. self.allowedModules = {}
  796. self.allowedClasses = {}
  797. def allowBasicTypes(self):
  798. """
  799. Allow all `basic' types. (Dictionary and list. Int, string, and float
  800. are implicitly allowed.)
  801. """
  802. self.allowTypes(*self.basicTypes)
  803. def allowTypes(self, *types):
  804. """
  805. SecurityOptions.allowTypes(typeString): Allow a particular type, by its
  806. name.
  807. """
  808. for typ in types:
  809. if isinstance(typ, unicode):
  810. typ = typ.encode('utf-8')
  811. if not isinstance(typ, bytes):
  812. typ = qual(typ)
  813. self.allowedTypes[typ] = 1
  814. def allowInstancesOf(self, *classes):
  815. """
  816. SecurityOptions.allowInstances(klass, klass, ...): allow instances
  817. of the specified classes
  818. This will also allow the 'instance', 'class' (renamed 'classobj' in
  819. Python 2.3), and 'module' types, as well as basic types.
  820. """
  821. self.allowBasicTypes()
  822. self.allowTypes("instance", "class", "classobj", "module")
  823. for klass in classes:
  824. self.allowTypes(qual(klass))
  825. self.allowModules(klass.__module__)
  826. self.allowedClasses[klass] = 1
  827. def allowModules(self, *modules):
  828. """
  829. SecurityOptions.allowModules(module, module, ...): allow modules by
  830. name. This will also allow the 'module' type.
  831. """
  832. for module in modules:
  833. if type(module) == types.ModuleType:
  834. module = module.__name__
  835. if not isinstance(module, bytes):
  836. module = module.encode('utf-8')
  837. self.allowedModules[module] = 1
  838. def isModuleAllowed(self, moduleName):
  839. """
  840. SecurityOptions.isModuleAllowed(moduleName) -> boolean
  841. returns 1 if a module by that name is allowed, 0 otherwise
  842. """
  843. if not isinstance(moduleName, bytes):
  844. moduleName = moduleName.encode('utf-8')
  845. return moduleName in self.allowedModules
  846. def isClassAllowed(self, klass):
  847. """
  848. SecurityOptions.isClassAllowed(class) -> boolean
  849. Assumes the module has already been allowed. Returns 1 if the given
  850. class is allowed, 0 otherwise.
  851. """
  852. return klass in self.allowedClasses
  853. def isTypeAllowed(self, typeName):
  854. """
  855. SecurityOptions.isTypeAllowed(typeName) -> boolean
  856. Returns 1 if the given type is allowed, 0 otherwise.
  857. """
  858. if not isinstance(typeName, bytes):
  859. typeName = typeName.encode('utf-8')
  860. return (typeName in self.allowedTypes or b'.' in typeName)
  861. globalSecurity = SecurityOptions()
  862. globalSecurity.allowBasicTypes()
  863. def jelly(object, taster=DummySecurityOptions(), persistentStore=None,
  864. invoker=None):
  865. """
  866. Serialize to s-expression.
  867. Returns a list which is the serialized representation of an object. An
  868. optional 'taster' argument takes a SecurityOptions and will mark any
  869. insecure objects as unpersistable rather than serializing them.
  870. """
  871. return _Jellier(taster, persistentStore, invoker).jelly(object)
  872. def unjelly(sexp, taster=DummySecurityOptions(), persistentLoad=None,
  873. invoker=None):
  874. """
  875. Unserialize from s-expression.
  876. Takes a list that was the result from a call to jelly() and unserializes
  877. an arbitrary object from it. The optional 'taster' argument, an instance
  878. of SecurityOptions, will cause an InsecureJelly exception to be raised if a
  879. disallowed type, module, or class attempted to unserialize.
  880. """
  881. return _Unjellier(taster, persistentLoad, invoker).unjellyFull(sexp)