简体   繁体   English

Flash + pyAMF + Django会话cookie安全

[英]Flash + pyAMF + Django session cookie security

First off, if there is a true, official way of having flash/flex's NetConnection s usurp the session/cookie state of the surrounding web page, so that if the user has already logged in, they don't need to provide credentials again just to set up an AMF connection, please stop me now and post the official answer. 首先,如果存在一种真正的,正式的方式来使Flash / Flex的NetConnection篡改周围网页的会话/ cookie状态,那么,如果用户已经登录,则无需再次提供凭据。要建立AMF连接,请立即停止我并发布正式答案。

Barring that, I'm assuming there is not, as I have searched and it seems to not exist. 除非如此,否则我假设没有,因为我已经搜索过了,而且似乎不存在。 I've concocted a means of doing this, but want some feedback as to whether it is secure. 我已经构想了一种方法,但是需要一些关于它是否安全的反馈。

  1. Accessing a wrapper-page for a flash object will always go to secure https due to django middleware 由于django中间件,访问Flash对象的包装页面将始终转到安全的https
  2. When the page view is loaded in Django, it creates a "session alias" object with a unique key that points to the current session in play (in which someone ostensibly logged in) 在Django中加载页面视图时,它会创建一个具有唯一键的“会话别名”对象,该对象指向正在运行的当前会话(表面上有人在其中登录)
  3. That session alias model is saved, and that key is placed into a cookie whose key is another random string, call it randomcookie 保存该会话别名模型,并将该密钥放入另一个密钥为另一个随机字符串的cookie中,将其称为randomcookie
  4. That randomcookie key name is passed as a context variable and written into the html as a flashvar to the swf 该randomcookie密钥名称作为上下文变量传递,并作为Flashvar写入html到swf
  5. The swf is also loaded only via https 瑞士法郎也仅通过https加载
  6. The flash application uses ExternalInterface to call java to grab the value at that randomcookie location, and also deletes the cookie Flash应用程序使用ExternalInterface调用Java来获取该randomcookie位置的值,并删除该cookie。
  7. It then creates a NetConnection to a secure server https location, passing that randomcookie as an argument (data, not in the url) to a login-using-cookie rpc 然后,它创建到安全服务器https位置的NetConnection ,并将该randomcookie作为参数(数据,而不是url中)传递给login-using-cookie rpc
  8. At the gateway side, pyamf looks up the session alias and gets the session it points to, and logs in the user based on that (and deletes the alias, so it can't be reused) 在网关端,pyamf查找会话别名并获取它指向的会话,并根据该别名登录用户(并删除该别名,因此无法重复使用)
  9. (And the gateway request could also set the session cookie and session.session_key to the known session ID, but I could let it make a whole new session key... I'm assuming that doing so should affect the response properly so that it contains the correct session key) (并且网关请求还可以将会话cookie和session.session_key设置为已知的会话ID,但是我可以让它创建一个全新的会话密钥...我假设这样做会适当地影响响应,以便它包含正确的会话密钥)
  10. At this point, the returned cookie values on the flash side should stick to the NetConnection so that further calls are authenticated (if a connection is authenticated using username and password the normal way, this definitely works, so I think this is a safe bet, testing will soon prove or disprove this) 此时,在闪存端返回的cookie值应保留在NetConnection以便对进一步的调用进行身份验证(如果使用正常方式使用用户名和密码对连接进行身份验证,则肯定可以,因此,我认为这是一个安全的选择,测试将很快证明或反对这一点)

So, is this unsafe, or will this work properly? 那么,这是不安全的,还是可以正常工作? As far as I know, since the html page is guaranteed to be over ssl, the key and cookie data should be encrypted and not steal-able. 据我所知,由于html页面保证在ssl之上,因此密钥和cookie数据应该被加密并且不能被窃取。 Then, the info therein should be safe to use one-time as basically a temporary password, sent again over ssl because the gateway is also https. 然后,其中的信息应该可以安全地一次性使用,基本上可以作为临时密码使用,因为网关也是https,所以它通过ssl再次发送。 After that, it's using the normal pyAMF system over https and not doing anything out of the ordinary. 之后,它将通过https使用普通的pyAMF系统,并且不会执行任何异常操作。

No responses on this so far, so the best I can do is confirm that it does in fact physically work. 到目前为止,对此还没有任何回应,所以我能做的最好的事情就是确认它确实在物理上起作用。 For details on how to set up Flex Builder to write html-wrappers that communicate with Django pages templates, see my other post . 有关如何设置Flex Builder来编写与Django页面模板进行通信的html包装程序的详细信息,请参阅我的其他文章 The above was accomplished using a combination of the aforementioned, plus: 以上是结合使用上述各项以及以下内容完成的:

Made a SessionAlias model: 制作了SessionAlias模型:

class SessionAlias(models.Model):
  alias   = models.CharField( max_length=40, primary_key=True )
  session = models.ForeignKey( Session )
  created = models.DateTimeField( auto_now_add=True )

Flex points to a Django page that loads via a view containing: Flex指向通过包含以下内容的视图加载的Django页面:

s = SessionAlias()
s.alias = SessionStore().session_key // generates new 40-char random
s.session = Session.objects.get( session_key=request.session.session_key )
s.save();
randomcookie = SessionStore().session_key // generates new 40-char random
kwargs['extra_context']['randomcookie'] = randomcookie
response = direct_to_template( request, **kwargs )
response.set_cookie( randomcookie, value=alias )

In the flex html-wrapper, where randomcookie is the location to look for the alias: 在flex html-wrapper中,其中randomcookie是查找别名的位置:

<param name="flashVars" value="randomcookie={{randomcookie}}" />

In applicationComplete , where we get randomcookie and find the alias, and log on using that: applicationComplete ,我们获取randomcookie并找到别名,然后使用该别名登录:

var randomcookie:String = this.parameters["randomcookie"];
// randomcookie is something like "abc123"
var js:String = "function get_cookie(){return document.cookie;}";
var cookies:String = ExternalInterface.call(js).toString();
// cookies looks like "abc123=def456; sessionid=ghi789; ..."
var alias:String = // strip out the "def456"
mynetconnection.call( "loginByAlias", alias, successFunc, failureFunc );

Which in turn access this pyamf gateway rpc: 依次访问此pyamf网关rpc:

from django.contrib.auth import SESSION_KEY, load_backend
from django.contrib.auth.models import User
from django.contrib import auth
from django.conf import settings
def loginByAlias( request, alias ):
  a = SessionAlias.objects.get( alias=alias )
  session_engine = __import__( settings.SESSION_ENGINE, {}, {}, [''] )
  session_wrapper = session_engine.SessionStore( a.session.session_key )
  user_id = session_wrapper.get( SESSION_KEY )
  user = User.objects.get( id=user_id )
  user.backend='django.contrib.auth.backends.ModelBackend'
  auth.login( request, user )
  a.delete()
  return whateverToFlash

And at that point, on the flash/flex side, that particular mynetconnection retains the session cookie state that can make future calls such that, inside the gateway, request.user is the properly-authenticated user that logged onto the webpage in the first place. 在那一点上,在flash / flex方面,特定的mynetconnection保留了可以发出将来调用的会话cookie状态,这样,在网关内部, request.user是首先登录到网页的经过正确身份验证的用户。 。

Note again that the run/debug settings for flex must use https, as well as the gateway settings for NetConnection . 再次注意,flex的运行/调试设置必须使用https以及NetConnection的网关设置。 And when releasing this, I have to make sure that authenticated users stay on https. 在发布此代码时,我必须确保已通过身份验证的用户使用https。

Any further info from people would be appreciated, especially if there's real feedback on the security aspects of this... 人们的任何进一步信息将不胜感激,特别是如果对此安全性方面有真实反馈的话……

IE doesn't give access to cookies in local development but if you publish the SWF and put on a domain, it should pickup the session just like ever other browser. IE不允许在本地开发中访问cookie,但是如果您发布SWF并放入域中,则它应该像其他浏览器一样拾取会话。 Use Firefox 3.6 to build your flex apps locally. 使用Firefox 3.6在本地构建Flex应用程序。

Tested in IE8, Firefox using a pyamf gateway on Flex 3 with NetConnection. Firefox在IE8中进行了测试,并在带有NetConnection的Flex 3上使用pyamf网关。 The gateway function was decorated with @login_required 网关功能用@login_required装饰

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM