开发者

Is it safe to pass Google App Engine Entity Keys into web pages to maintain context?

开发者 https://www.devze.com 2023-03-22 11:16 出处:网络
I have a simple GAE system that contains models for Account, Project and Transaction. I am using Django to generate a web page that has a list of Projects in a table that belong to a given Account an

I have a simple GAE system that contains models for Account, Project and Transaction.

I am using Django to generate a web page that has a list of Projects in a table that belong to a given Account and I want to create a link to each project's details page. I am generating a link that converts the Project's key to string and includes that in the link to make it easy to lookup the Project object. This giv开发者_Python百科es a link that looks like this:

<a href="/project?key=agxkZAB-bnVpY2VrbXRyDDsSBkNvdXBvbhgBDA">My Project Name</a>
  1. Is it secure to create links like this? Is there a better way? It feels like a bad way to keep context.

  2. The key string shows up in the linked page and is ugly. Is there a way to avoid showing it?

Thanks.


There is few examples, in GAE docs, that uses same approach, and also Key are using characters safe for including in URLs. So, probably, there is no problem.

BTW, I prefer to use numeric ID (obj_key.id()), when my model uses number as identifier, just because it's looks not so ugly.


Whether or not this is 'secure' depends on what you mean by that, and how you implement your app. Let's back off a bit and see exactly what's stored in a Key object. Take your key, go to shell.appspot.com, and enter the following:

db.Key(your_key)

this returns something like the following:

datastore_types.Key.from_path(u'TestKind', 1234, _app=u'shell')

As you can see, the key contains the App ID, the kind name, and the ID or name (along with the kind/id pairs of any parent entities - in this case, none). Nothing here you should be particularly concerned about concealing, so there shouldn't be any significant risk of information leakage here.

You mention as a concern that users could guess other URLs - that's certainly possible, since they could decode the key, modify the ID or name, and re-encode the key. If your security model relies on them not guessing other URLs, though, you might want to do one of a couple of things:

  1. Reconsider your app's security model. You shouldn't rely on 'secret URLs' for any degree of real security if you can avoid it.
  2. Use a key name, and set it to a long, random string that users will not be able to guess.

A final concern is what else users could modify. If you handle keys by passing them to db.get, the user could change the kind name, and cause you to fetch a different entity kind to that which you intended. If that entity kind happens to have similarly named fields, you might do things to the entity (such as revealing data from it) that you did not intend. You can avoid this by passing the key to YourModel.get instead, which will check the key is of the correct kind before fetching it.

All this said, though, a better approach is to pass the key ID or name around. You can extract this by calling .id() on the key object (for an ID - .name() if you're using key names), and you can reconstruct the original key with db.Key.from_path('kind_name', id) - or just fetch the entity directly with YourModel.get_by_id.


After doing some more research, I think I can now answer my own question. I wanted to know if using GAE keys or ids was inherently unsafe.

It is, in fact, unsafe without some additional code, since a user could modify URLs in the returned webpage or visit URL that they build manually. This would potentially let an authenticated user edit another user's data just by changing a key Id in a URL.

So for every resource that you allow access to, you need to ensure that the currently authenticated user has the right to be accessing it in the way they are attempting.

This involves writing extra queries for each operation, since it seems there is no built-in way to just say "Users only have access to objects that are owned by them".


I know this is an old post, but i want to clarify one thing. Sometimes you NEED to work with KEYs.

When you have an entity with a @Parent relationship, you cant get it by its ID, you need to use the whole KEY to get it back form the Datastore. In these cases you need to work with the KEY all the time if you want to retrieve your entity.


They aren't simply increasing; I only have 10 entries in my Datastore and I've already reached 7001. As long as there is some form of protection so users can't simply guess them, there is no reason not to do it.

0

精彩评论

暂无评论...
验证码 换一张
取 消