小编Sim*_*mon的帖子

如何检查目录是否是另一个目录的子目录

我喜欢用Python编写一个模板系统,它允许包含文件.

例如

    This is a template
    You can safely include files with safe_include`othertemplate.rst`

如您所知,包含文件可能很危险.例如,如果我在允许用户创建自己的模板的Web应用程序中使用模板系统,他们可能会做类似的事情

I want your passwords: safe_include`/etc/password`

因此,我必须限制将文件包含在例如某个子目录中的文件中(例如/home/user/templates)

现在的问题是:我如何检查,是否/home/user/templates/includes/inc1.rst在子目录中/home/user/templates

以下代码是否有效且安全?

import os.path

def in_directory(file, directory, allow_symlink = False):
    #make both absolute    
    directory = os.path.abspath(directory)
    file = os.path.abspath(file)

    #check whether file is a symbolic link, if yes, return false if they are not allowed
    if not allow_symlink and os.path.islink(file):
        return False

    #return true, if the common prefix of both is equal to directory …
Run Code Online (Sandbox Code Playgroud)

python filesystems security validation

38
推荐指数
7
解决办法
3万
查看次数

将CouchDB javascript视图转换为erlang

我需要一些帮助,将以下CouchDB视图从javascript转换为erlang.我在erlang中需要它们,因为在javascript中视图使用所有可用的堆栈内存并崩溃couchjs(请参阅此bugreport https://issues.apache.org/jira/browse/COUCHDB-893).

我在javascript中的当前地图函数是:

同步/ transaction_keys

function(doc) {
  if(doc.doc_type == "Device") {
      for(key in doc.transactions)
          emit(key, null);
  }
}
Run Code Online (Sandbox Code Playgroud)

同步/交易

function(doc) {
  if(doc.doc_type == "Device") {
      for(key in doc.transactions) {
          t = doc.transactions[key];
          t.device = doc.device;
          emit(key, t);
     }
  }
}
Run Code Online (Sandbox Code Playgroud)

一个示例文档是:

{
   "_id": "fcef7b5c-cbe6-31af-8363-2b446a7e4cf2",
   "_rev": "3-c90abd075404a75744fd3e5e4f04ebad",
   "device": "fcef7b5c-cbe6-31af-8363-2b446a7e4cf2",
   "doc_type": "Device",
   "transactions": {
       "79fe8630-c0c0-30c6-9913-79b2f93e3e6e": {
           "timestamp": 1309489169533,
           "version": 10008,
           "some_more_data" : "more_data"
       }
       "e4678930-c465-76a6-8821-75a3e888765a": {
           "timestamp": 1309489169533,
           "version": 10008,
           "some_more_data" : "more_data"
       }
   }
}
Run Code Online (Sandbox Code Playgroud)

基本上sync/transaction_keys发出事务字典的所有键,sync/transaction确实发出事务字典中的所有条目.

不幸的是我之前从未使用过Erlang,我很快就需要重写代码,所以任何帮助都非常受欢迎.

提前致谢.

javascript erlang couchdb

9
推荐指数
1
解决办法
1092
查看次数

Python中的Hopcroft-Karp算法

我正在尝试使用networkx作为图形表示在Python中实现Hopcroft Karp算法.

目前我就是这样:

#Algorithms for bipartite graphs

import networkx as nx
import collections

class HopcroftKarp(object):
    INFINITY = -1

    def __init__(self, G):
        self.G = G

    def match(self):
        self.N1, self.N2 = self.partition()
        self.pair = {}
        self.dist = {}
        self.q = collections.deque()

        #init
        for v in self.G:
            self.pair[v] = None
            self.dist[v] = HopcroftKarp.INFINITY

        matching = 0

        while self.bfs():
            for v in self.N1:
                if self.pair[v] and self.dfs(v):
                    matching = matching + 1

        return matching

    def dfs(self, v):
        if v != None:
            for …
Run Code Online (Sandbox Code Playgroud)

python algorithm graph bipartite graph-algorithm

7
推荐指数
1
解决办法
5325
查看次数

用设计白名单

我正在使用设计来管理我的rails应用程序中的用户身份验证.设计真的很棒.

但是我对我的应用程序有特殊要求:用户必须先列入白名单才能注册为用户.

因此,有一个管理员可以创建允许的电子邮件列表.用户使用电子邮件注册,如果电子邮件在白名单表中,则他将被注册.但是,如果邮件不在白名单中,则应使用"您尚未被邀请"之类的消息中止注册.

你知道如何用设计解决这个问题吗?

提前致谢.

authentication ruby-on-rails whitelist devise

7
推荐指数
2
解决办法
3384
查看次数

在钩子中使用git pull时错误的文件权限

当新的更改被推送到存储库时,我创建了以下git钩子来更新我的Web应用程序

#!/bin/sh
#Update the server version to HEAD

echo "Updating webapp..."
unset $(git rev-parse --local-env-vars)
(cd /var/www/webapp && git pull -q)
Run Code Online (Sandbox Code Playgroud)

但是,如果我添加新文件,则会获得错误的权限.它们只能由所有者读取,而不能由组或其他用户读取.但我需要每个人都能阅读它们.在本地,他们有正确的权限位.即使我从shell手动运行钩子,它也能正常工作.当脚本被称为钩子时,它才起作用.

任何想法如何解决?

PS:我正在使用git 1.7

git file-permissions githooks

5
推荐指数
1
解决办法
3286
查看次数

在python中计算IP校验和

我需要计算IP数据包的校验和,如http://www.faqs.org/rfcs/rfc1071.html中所述.

我已经有以下代码:

#!/usr/bin/python
import struct

data = "45 00 00 47 73 88 40 00 40 06 a2 c4 83 9f 0e 85 83 9f 0e a1"

# a test for the checksum calculation

def _checksum(data):
    #calculate the header sum
    ip_header_sum = sum(struct.unpack_from("6H", data))
    #add the carry
    ip_header_sum = (ip_header_sum & 0xFFFF) + (ip_header_sum >> 16 & 0xFFFF)
    #invert the sum, python does not support inversion (~a is -a + 1) so we have to do
    #little trick: …
Run Code Online (Sandbox Code Playgroud)

python checksum network-protocols

1
推荐指数
2
解决办法
2万
查看次数