def testTooOld(self): tmp = self.write_with_version('min-injector-version="1000"') try: reader.check_readable(foo_iface_uri, tmp.name) self.fail() except reader.InvalidInterface as ex: assert "1000" in str(ex)
def testTooOld(self): tmp = self.write_with_version('min-injector-version="1000"') try: reader.check_readable(foo_iface_uri, tmp.name) self.fail() except reader.InvalidInterface, ex: assert "1000" in str(ex)
def testCantUseBothInsertAndValueInEnvironmentBinding(self): tmp = self.write_with_bindings(""" <environment name="DATA" value="" insert=""/> """) try: reader.check_readable(foo_iface_uri, tmp.name) self.fail() except reader.InvalidInterface as ex: assert "Binding contains both 'insert' and 'value'" in str(ex)
def testCantUseBothInsertAndValueInEnvironmentBinding(self): tmp = self.write_with_bindings(""" <environment name="DATA" value="" insert=""/> """) try: reader.check_readable(foo_iface_uri, tmp.name) self.fail() except reader.InvalidInterface, ex: assert "Binding contains both 'insert' and 'value'" in str(ex)
def _import_new_feed(self, feed_url, new_xml, modified_time): """Write new_xml into the cache. @param feed_url: the URL for the feed being updated @param new_xml: the data to write @param modified_time: when new_xml was modified @raises ReplayAttack: if the new mtime is older than the current one """ assert modified_time assert isinstance(new_xml, bytes), repr(new_xml) upstream_dir = basedir.save_cache_path(config_site, 'interfaces') cached = os.path.join(upstream_dir, escape(feed_url)) old_modified = None if os.path.exists(cached): with open(cached, 'rb') as stream: old_xml = stream.read() if old_xml == new_xml: logger.debug(_("No change")) # Update in-memory copy, in case someone else updated the disk copy self.get_feed(feed_url, force=True) return old_modified = int(os.stat(cached).st_mtime) # Do we need to write this temporary file now? try: with open(cached + '.new', 'wb') as stream: stream.write(new_xml) os.utime(cached + '.new', (modified_time, modified_time)) new_mtime = reader.check_readable(feed_url, cached + '.new') assert new_mtime == modified_time old_modified = self._get_signature_date(feed_url) or old_modified if old_modified: if new_mtime < old_modified: raise ReplayAttack( _("New feed's modification time is " "before old version!\nInterface: %(iface)s\nOld time: %(old_time)s\nNew time: %(new_time)s\n" "Refusing update.") % { 'iface': feed_url, 'old_time': _pretty_time(old_modified), 'new_time': _pretty_time(new_mtime) }) if new_mtime == old_modified: # You used to have to update the modification time manually. # Now it comes from the signature, this check isn't useful # and often causes problems when the stored format changes # (e.g., when we stopped writing last-modified attributes) pass #raise SafeException("Interface has changed, but modification time " # "hasn't! Refusing update.") except: os.unlink(cached + '.new') raise portable_rename(cached + '.new', cached) logger.debug(_("Saved as %s") % cached) self.get_feed(feed_url, force=True)
def _import_new_feed(self, feed_url, new_xml, modified_time): """Write new_xml into the cache. @param feed_url: the URL for the feed being updated @param new_xml: the data to write @param modified_time: when new_xml was modified @raises ReplayAttack: if the new mtime is older than the current one """ assert modified_time assert isinstance(new_xml, bytes), repr(new_xml) upstream_dir = basedir.save_cache_path(config_site, 'interfaces') cached = os.path.join(upstream_dir, escape(feed_url)) old_modified = None if os.path.exists(cached): with open(cached, 'rb') as stream: old_xml = stream.read() if old_xml == new_xml: logger.debug(_("No change")) # Update in-memory copy, in case someone else updated the disk copy self.get_feed(feed_url, force = True) return old_modified = int(os.stat(cached).st_mtime) # Do we need to write this temporary file now? try: with open(cached + '.new', 'wb') as stream: stream.write(new_xml) os.utime(cached + '.new', (modified_time, modified_time)) new_mtime = reader.check_readable(feed_url, cached + '.new') assert new_mtime == modified_time old_modified = self._get_signature_date(feed_url) or old_modified if old_modified: if new_mtime < old_modified: raise ReplayAttack(_("New feed's modification time is " "before old version!\nInterface: %(iface)s\nOld time: %(old_time)s\nNew time: %(new_time)s\n" "Refusing update.") % {'iface': feed_url, 'old_time': _pretty_time(old_modified), 'new_time': _pretty_time(new_mtime)}) if new_mtime == old_modified: # You used to have to update the modification time manually. # Now it comes from the signature, this check isn't useful # and often causes problems when the stored format changes # (e.g., when we stopped writing last-modified attributes) pass #raise SafeException("Interface has changed, but modification time " # "hasn't! Refusing update.") except: os.unlink(cached + '.new') raise portable_rename(cached + '.new', cached) logger.debug(_("Saved as %s") % cached) self.get_feed(feed_url, force = True)
def _import_new_interface(self, interface, new_xml, modified_time): """Write new_xml into the cache. @param interface: updated once the new XML is written @param new_xml: the data to write @param modified_time: when new_xml was modified @raises ReplayAttack: if the new mtime is older than the current one """ assert modified_time upstream_dir = basedir.save_cache_path(config_site, 'interfaces') cached = os.path.join(upstream_dir, escape(interface.uri)) if os.path.exists(cached): old_xml = file(cached).read() if old_xml == new_xml: debug(_("No change")) reader.update_from_cache(interface) return stream = file(cached + '.new', 'w') stream.write(new_xml) stream.close() os.utime(cached + '.new', (modified_time, modified_time)) new_mtime = reader.check_readable(interface.uri, cached + '.new') assert new_mtime == modified_time old_modified = self._get_signature_date(interface.uri) if old_modified is None: old_modified = interface.last_modified if old_modified: if new_mtime < old_modified: os.unlink(cached + '.new') raise ReplayAttack( _("New interface's modification time is " "before old version!\nOld time: %(old_time)s\nNew time: %(new_time)s\n" "Refusing update.") % { 'old_time': _pretty_time(old_modified), 'new_time': _pretty_time(new_mtime) }) if new_mtime == old_modified: # You used to have to update the modification time manually. # Now it comes from the signature, this check isn't useful # and often causes problems when the stored format changes # (e.g., when we stopped writing last-modified attributes) pass #raise SafeException("Interface has changed, but modification time " # "hasn't! Refusing update.") os.rename(cached + '.new', cached) debug(_("Saved as %s") % cached) reader.update_from_cache(interface)
def testNewEnough(self): tmp = self.write_with_version('min-injector-version="0.19"') reader.check_readable(foo_iface_uri, tmp.name)
def testNoVersion(self): tmp = self.write_with_version('') reader.check_readable(foo_iface_uri, tmp.name)
def _import_new_feed(self, feed_url, new_xml, modified_time, dry_run): """Write new_xml into the cache. @param feed_url: the URL for the feed being updated @type feed_url: str @param new_xml: the data to write @type new_xml: str @param modified_time: when new_xml was modified @type modified_time: int @type dry_run: bool @raises ReplayAttack: if the new mtime is older than the current one""" assert modified_time assert isinstance(new_xml, bytes), repr(new_xml) upstream_dir = basedir.save_cache_path(config_site, "interfaces") cached = os.path.join(upstream_dir, escape(feed_url)) old_modified = None if os.path.exists(cached): with open(cached, "rb") as stream: old_xml = stream.read() if old_xml == new_xml: logger.debug(_("No change")) # Update in-memory copy, in case someone else updated the disk copy self.get_feed(feed_url, force=True) return old_modified = int(os.stat(cached).st_mtime) if dry_run: print(_("[dry-run] would cache feed {url} as {cached}").format(url=feed_url, cached=cached)) from io import BytesIO from zeroinstall.injector import qdom root = qdom.parse(BytesIO(new_xml), filter_for_version=True) feed = model.ZeroInstallFeed(root) reader.update_user_feed_overrides(feed) self._feeds[feed_url] = feed return # Do we need to write this temporary file now? try: with open(cached + ".new", "wb") as stream: stream.write(new_xml) os.utime(cached + ".new", (modified_time, modified_time)) new_mtime = reader.check_readable(feed_url, cached + ".new") assert new_mtime == modified_time old_modified = self._get_signature_date(feed_url) or old_modified if old_modified: if new_mtime < old_modified: raise ReplayAttack( _( "New feed's modification time is " "before old version!\nInterface: %(iface)s\nOld time: %(old_time)s\nNew time: %(new_time)s\n" "Refusing update." ) % { "iface": feed_url, "old_time": _pretty_time(old_modified), "new_time": _pretty_time(new_mtime), } ) if new_mtime == old_modified: # You used to have to update the modification time manually. # Now it comes from the signature, this check isn't useful # and often causes problems when the stored format changes # (e.g., when we stopped writing last-modified attributes) pass # raise SafeException("Interface has changed, but modification time " # "hasn't! Refusing update.") except: os.unlink(cached + ".new") raise portable_rename(cached + ".new", cached) logger.debug(_("Saved as %s") % cached) self.get_feed(feed_url, force=True)