虽然做web开发有一段时间了,但是对于同源策略和csrf安全策略理解一直不深刻,特抽出时间做了简单的实验进行理解。实验过程如下,与大家一起分享。
实验目的:验证同源策略和csrf安全策略的关系和区别
实验方案:1.Linux搭建django框架的python服务器(a);Windows搭建简单的js服务器(b)
2.b的首页中做了如下内容:(1)通过post\get方式提交向a表单
(2)通过ajax方式(post\get)向a请求数据
实验结果:1.a没有开启csrf安全策略的情况下,打开b的首页,b的页面可以通过post\get方式正常的向a提交表单并得到回复页面;但是通过ajax的get\post方式无法请求到a的数据。观察a的log日志,发现b的request请求在a上能够收到,但是b的request附带的数据均加载不成功,b的ajax(get\post)请求均能得到a的response,在下发到打开b的浏览器时报出了同源策略的警告。
2.a开启csrf安全策略的情况下,打开b的首页,b的页面可以通过get方式正常的向a提交表单并得到回复页面,但是并不能通过post方式提交;b页面通过ajax的get\post方式均无法请求到a的数据。
结论:1.同源策略:js语言的设计安全考虑,只允许同源访问。非同源访问也能向对应服务器发送请求,但是浏览器request中附带的数据全部丢失,服务器能回传request的response。但是在浏览器解析服务器下发的response阶段会有同源策略的警告。(解释基于js的ajax技术)
2.csrf安全策略:搭建服务器时的安全考虑,需要普通开发者进行相关的设计(框架一般自带csrf安全策略的设计)。csrf策略过程为:在请求服务器的页面时,服务器的response会向浏览器设置一个cookie,当有post方式的表单向服务器提交时,服务器设置的cookie会附加在浏览器的request中一起提交,服务器在接收request时,会验证附加的cookie是否正确(每个用户与服务器的通讯连接只有一个唯一的cookie,连接中断后,下次连接时服务器会向浏览器设置新的cookie),只有cookie验证通过才能下发正确的response,验证失败会有“403”错误码下发。
1 # --------------Django服务器部分代码-------------- 2 # -*- coding:utf-8 -*- 3 from django.shortcuts import render 4 from django.http import HttpResponse, HttpResponseRedirect, JsonResponse 5 6 # Create your views here. 7 8 9 def index(request): 10 11 context = {'contents': 'hello world'} 12 # return HttpResponse("ok") 13 response= render(request, 'booktest/index.html', context) 14 return response 15 16 17 def args(request, id1, id2): 18 19 string = '%s--%s' % (id1, id2) 20 return HttpResponse(string) 21 22 23 def get1(request): 24 25 mode = request.encoding 26 dict = request.GET 27 a = dict.get('a') 28 b = dict.get('b') 29 c = dict.get('c') 30 string = 'method:%s--%s--%s--%s' % (mode, a, b, c) 31 return HttpResponse(string) 32 33 34 def get2(request): 35 36 dict = request.GET 37 a = dict.getlist('a') 38 b = dict.get('b') 39 c = dict.get('c') 40 d = dict.get('d', 'have no') 41 string = '%s--%s--%s--%s' % (a, b, c, d) 42 return HttpResponse(string) 43 44 45 def post(requst): 46 47 str_data = '---%s---%s' % (requst.method, requst.POST.get('uname')) 48 49 return HttpResponse(str_data) 50 51 52 def get3(request): 53 54 dict = request.GET 55 a = dict.get('a') 56 b = dict.get('b') 57 c = dict.get('c') 58 context = {'1': a, '2': b, '3': c} 59 # return HttpResponse("ok") 60 return HttpResponse(context) 61 # return render(request, 'booktest/get1.html', context) 62 63 64 def get4(request): 65 66 return HttpResponseRedirect('/admin/') 67 68 69 def ajax(request): 70 71 # return HttpResponse('ok') 72 return render(request, 'booktest/ajax.html/') 73 74 75 def json(request): 76 77 data1 = request.POST.get('csrfmiddlewaretoken') 78 data2 = request.POST.get('data') 79 print('------------%s------------%s---' % (data1, data2)) 80 a = {'h1': 'hello', 'h2': 'world', 'method': request.method, 'csrf': data1, 'data': data2} 81 82 return JsonResponse(a) 83 84 85 def cookie_set(request): 86 print('123') 87 cookie_value = 'hello' 88 89 response = HttpResponse("设置Cookie,请查看响应报文头
") 90 # response = HttpResponse("hello") 91 # Cookie中设置汉字键值对失败 92 response.set_cookie('h1', cookie_value) 93 # return HttpResponse('ok') 94 return response 95 96 97 def cookie_get(request): 98 99 response = HttpResponse('读取Cookie,数据如下:
')100 cookies = request.COOKIES101 if cookies.get('h1'):102 response.write(''+cookies['h1']+'
')103 104 return response
1 <--Django服务器template部分的index.html代码--> 2 3 4 5 6 732 33index 8 9 10 {# #}11 返回主页12 13
14参数
15 get一键传一值16
17 get一键传多值18
19
34
GET属性
35 一键传一值3637 一键传多值38 39
40
JsonResponse
41 ajax42 4344
Cookie
45 设置Cookie4647 获取Cookie48 49
1 <--Django服务器ajax.html代码--> 2 3 4 5 6 7ajax 8 9 10 25 26 27hello world!28 {% csrf_token %}29 30
1 <--JS搭建的服务器首页代码--> 2 3 4 5 6 752 53index 8 9 10 11 26 27 28 29 30 {# #}31 返回主页32 33
34参数
35 get一键传一值36
37 get一键传多值38
39
54
GET属性
55 一键传一值5657 一键传多值58 59
60
JsonResponse
61 ajax62 6364
Cookie
65 设置Cookie6667 获取Cookie68 69
70










