# CGI:Session and security

**URL:** <https://rubytalk.org/t/cgi-session-and-security/7459>\
**Category:** ruby-talk\
**Created:** [17 August 2003 19:00 UTC](https://rubytalk.org/t/cgi-session-and-security/7459 "2003-08-17T19:00:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Volker\_Grabsch](https://avatars.discourse-cdn.com/v4/letter/v/59ef9b/32.png) [@Volker\_Grabsch](https://rubytalk.org/u/Volker_Grabsch)\
**Post date:** [17 August 2003 19:00 UTC](https://rubytalk.org/t/cgi-session-and-security/7459/1 "2003-08-17T19:00:17Z")

</div>

Hello Folks,

There are some simple standard problems which crowd my brain 🙂

- creating a session using a hidden field like cgi[‘sess\_id’] instead  
of a cookie … or first try the cookie and if it’s rejected, go  
the other way

This should be easy to achieve. Either put automatically a hidden field  
after each with the content that would be otherwise written into  
the cookie. This is somewhat kludge, but in PHP it works very well. At  
least ERuby should be able to do that.  
Or override “cgi.form { … }” to “old\_form { hidden { … } }”

The better way would be to give the programmer a chance to manage  
the sess\_id for himself … having an option not to store the sess\_id  
into a cookie and having an attribut like Session.sess\_id

… but as far as I see, none of these ways are implemented. Why not?

- since session files are mostly readable by other scripts/users,  
passwords shouldn’t be stored directly. So it should be stored  
encrypted. This is especially useful if the CGI script doesn’t  
contain any passwords (the ideal case ;-)) and uses the password  
the user entered to authorize against LDAP, a database, or whatever.

The best way would be to created a random key and encrypt the entered  
password with it. The one is stored in the session (at the server),  
the other is stored in a cookie or a hidden field (at the client).  
So the attacker must catch the session file _and_ have access to the  
browser of the user. And both, the session file and the cookie, are  
removed after the session, so the attacker must catch these two  
_during_ the session. As long as HTTPS is used, and noone can be  
at two places at the same time, this would be secure.

An even better way would be to automatically encrypt the whole  
session, so that the sess\_id consists of two parts: The id of  
the session file (to find it), and the (initially) random key  
used to encrypt/decrypt the session file. At least _this_ could  
be possible in a transparent way. A kind of  
CGI::Session::EncryptedFileStore should be possible, isn’t it?

Probably CGI::Session::MemoryStore is already the solution of this  
problem, but it’s only shortly mentioned in the Ruby Book. So  
I don’t know …

Of course, using SuExec the session file would have another owner  
etc., so then it would be secure, too. But secure sessions should  
be possible without an extra user for each CGI script 🙂

So my thoughts about these issues. Does anyone know in what way  
they are implemented or why they better shouldn’t?

Thanks,

> **···**
>
> –  
> Volker Grabsch  
> —\<\<(())\>\>—  
> \frac{\left|\vartheta\_0\times{\ell,\kappa\in\Re}\right|}{\sqrt  
> [G]{-\Gamma(\alpha)\cdot\mathcal{B}^{\left[\oint!c\_\hbar\right]}}}

---

<div class="post-metadata">

**Author:** ![Volker\_Grabsch](https://avatars.discourse-cdn.com/v4/letter/v/59ef9b/32.png) [@Volker\_Grabsch](https://rubytalk.org/u/Volker_Grabsch)\
**Post date:** [2 September 2003 07:32 UTC](https://rubytalk.org/t/cgi-session-and-security/7459/2 "2003-09-02T07:32:30Z")

</div>

Volker Grabsch wrote:

> There are some simple standard problems which crowd my brain 🙂

… MMh, and no answer yet.

Isn’t there anyone dealing with this kind of issues?

> So my thoughts about these issues. Does anyone know in what way  
> they are implemented or why they better shouldn’t?

Well, I should ask this in another way 🙂

Did anyone actually _implement_ this kind of session security?

… or …

Did anyone consider that, but didn’t do so for good reasons?

Greets,

> **···**
>
> –  
> Volker Grabsch  
> —\<\<(())\>\>—  
> \frac{\left|\vartheta\_0\times{\ell,\kappa\in\Re}\right|}{\sqrt  
> [G]{-\Gamma(\alpha)\cdot\mathcal{B}^{\left[\oint!c\_\hbar\right]}}}

---

<div class="post-metadata">

**Author:** ![Claudio\_Jeker](https://avatars.discourse-cdn.com/v4/letter/c/e8c25b/32.png) [@Claudio\_Jeker](https://rubytalk.org/u/Claudio_Jeker)\
**Post date:** [2 September 2003 09:33 UTC](https://rubytalk.org/t/cgi-session-and-security/7459/3 "2003-09-02T09:33:16Z")

</div>

> Volker Grabsch wrote:
> 
> > There are some simple standard problems which crowd my brain 🙂
> 
> … MMh, and no answer yet.
> 
> Isn’t there anyone dealing with this kind of issues?

Have you ever tried it? Makeing a hidden form field form a session is not  
so complicated:

puts “\<input type="hidden" name="id" value="#{@session.session\_id}"\>”

and earlier init the session with something like:

puts cgi.header(“text/html”)  
@session = CGI::Session.new({“session\_key” =\> “id”})

works like a charm. To disable the cookie I put the cgi.header call befor  
the CGI::Session.new call. I’m not sure if there is a better sollution to  
this (I started working with ruby just a few days ago.)

> > So my thoughts about these issues. Does anyone know in what way  
> > they are implemented or why they better shouldn’t?
> 
> Well, I should ask this in another way 🙂
> 
> Did anyone actually _implement_ this kind of session security?
> 
> … or …
> 
> Did anyone consider that, but didn’t do so for good reasons?

Remember sessions can be hijacked so it is uesful to store some additional  
info in the session file (like the IP address of the client and a session  
timeout). This does not matter if it is a cookie or a get/post field.  
IMHO storing sensitive data in a cookie is a bad idea.

> **···**
>
> On Tue, Sep 02, 2003 at 04:32:30PM +0900, Volker Grabsch wrote:
> 
> –  
> :wq Claudio

---

<div class="post-metadata">

**Author:** ![Xavier\_Noria](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/xavier_noria/32/1867_2.png) [@Xavier\_Noria](https://rubytalk.org/u/Xavier_Noria)\
**Post date:** [2 September 2003 09:48 UTC](https://rubytalk.org/t/cgi-session-and-security/7459/4 "2003-09-02T09:48:23Z")

</div>

Just for the record, %Q makes that kind of stuff more readable and less error  
prone:

puts %Q()

– fxn

> **···**
>
> On Tuesday 02 September 2003 11:33, Claudio Jeker wrote:
> 
> > puts “\<input type="hidden" name="id" value="#{@session.session\_id}"\>”
