jelly.py 35 KB

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