ARTS-week13

Algorithms

Climbing Stairs
思路: 斐波那契数列

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
int climbStairs(int n) {
if (n < 0) {
return 0;
}
int step1 = 0;
int step2 = 1;
int tmp = 1;
for (int i = 0; i < n; ++i) {
tmp = step1 + step2;
step1 = step2;
step2 = tmp;
}
return tmp;
}
};

Remove Duplicates from Sorted List

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution{
public:
ListNode *deleteDuplicates(ListNode *head) {
if (head == NULL) {
return NULL;
}

ListNode *node = head;
while (node->next != NULL) {
if (node->val == node->next->val) {
node->next = node->next->next;
} else {
node = node->next;
}
}
return head;
}
};

Review

本周阅读英文文章 Are Apps Tracking You Even After You Uninstall Them?

Technique

本周看了关于线程死锁的经典问题: 哲学家就餐问题

在Python CookBook中一个简单的实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import threading

# The philosopher thread
def philosopher(left, right):
while True:
with acquire(left,right):
print(threading.currentThread(), 'eating')

# The chopsticks (represented by locks)
NSTICKS = 5
chopsticks = [threading.Lock() for n in range(NSTICKS)]

# Create all of the philosophers
for n in range(NSTICKS):
t = threading.Thread(target=philosopher,
args=(chopsticks[n],chopsticks[(n+1) % NSTICKS]))
t.start()

Share

本周分享《HTTPS权威指南》Cookie的部分

默认情况下,无论是80端口还是443端口,Cookie都会被发送.部署了TLS的网站,如果攻击者可以找到一种方法让浏览器向80端口发送数据,就可以使用中间人攻击窃取不安全Cookie。

从安全的观点来看,Cookie的问题有两个方面:(1) 它们从一开始就没有被很好地设计,导致了安全漏洞产生;(2) 它们没有与当今浏览器使用的主流安全机制同步,这指的是同源策略(same-origin policy,SOP).

关于同源策略,在MDN的文档《浏览器的同源策略》看到有详细的介绍。

产生的影响:
1)XSS
2)CSRF防御绕过
3)应用程序状态改变
4)会话固定

Cookie篡改缓解的方法:
1)使用HSTS并覆盖子域名: HTTP严格传输安全(HTTP strict transport security,HSTS)用于强制使用加密来访问启用它的网站。可以将HSTS配置为对所有子域名启用。使用这个方法,中间人攻击在不攻破加密的情况下无法使用DNS欺骗来注入Cookie。HSTS极大地减小了攻击面,但是使用它并不是毫无问题。首先,并不是所有浏览器都支持HSTS。其次,它无法处理下面的情形:正确的(加密的)相关域名被盗用或者由不同的、不可信的实体运营。
2)抵御Cookie注入的最好方法是对Cookie进行完整性验证:确保从客户端收到的Cookie确实是由本网站设置的。这可以通过使用基于散列的消息验证代码(hash-based message authentication code,HMAC)实现。

0%