How to clean array before writing in database?

Have a programming question regarding your component, plug-in, extension or core hacks? Have an interesting tidbit, FAQ or programming tip you’d like to share? This is the place for you.

Moderators: tjay, seadap, Rogue4ngel, matthewhayashida

Post Reply
User avatar
Papillon
Joomla! Fledgling
Joomla! Fledgling
Posts: 4
Joined: Thu Aug 18, 2005 2:08 pm
Location: Good old Germany

How to clean array before writing in database?

Post by Papillon » Sun Aug 12, 2007 4:25 pm

Hello,

in the model file I get some post data from a form:

Code: Select all

$data = JRequest::get( 'post' );


In the controller.php I get the model and store it to the database:

Code: Select all

$model = $this->getModel( 'form' );
if ($model->store())
        {
            $msg = JText::_( 'Empfehlung gesendet' );
        } else ...


After looking into the database I see that html tags are stripped, but backslashes are still there. Do I  have to do something like "addslashes" to make it secure or is this safe enough?

Thanks for your answers and reading my bad english. I hope that you are understanding what I mean.

Papi

User avatar
AmyStephen
Joomla! Guru
Joomla! Guru
Posts: 579
Joined: Wed Nov 22, 2006 3:35 pm
Location: Nebraska
Contact:

Re: How to clean array before writing in database?

Post by AmyStephen » Sun Aug 12, 2007 7:43 pm

Papi -

I am not able to answer that question, but I want to point out How to make your Joomla! addon more secure, a wiki resource that the core developers put together. I am guessing you will find help in that. If you don't though, let us know. Someone else certainly can help with your question.

Thanks!
Amy :)
~*~ Joomla!'s Queen of the Blues - Jennifer Marriott ~*~
http://OpenSourceCommunity.org/node/1719/

User avatar
jlleblanc
Joomla! Apprentice
Joomla! Apprentice
Posts: 30
Joined: Fri Aug 19, 2005 12:37 am
Location: Washington, DC / NoVA
Contact:

Re: How to clean array before writing in database?

Post by jlleblanc » Sun Aug 12, 2007 10:04 pm

If your model is eventually using the store() function of JTable objects to add data to the database, it should automatically escape your variables with slashes.
Joseph L. LeBlanc: http://www.jlleblanc.com
Frontend components start here: /components/com_[name]/[name].php
Backend components start here: /administrator/components/com_[name]/admin.[name].php

User avatar
Papillon
Joomla! Fledgling
Joomla! Fledgling
Posts: 4
Joined: Thu Aug 18, 2005 2:08 pm
Location: Good old Germany

Re: How to clean array before writing in database?

Post by Papillon » Mon Aug 13, 2007 7:08 am

Thank you for your answers!

@Amy:
I like this ressource very much but I think that some of the parts are especially written for Joomla 1.0. The framework of Joomla 1.5 relieves us some of the work, but I don't know exactly which work in my case.

@jlleblanc:
I am using in the model some code from weblinks:

Code: Select all

function store()
    {
        $row =& $this->getTable( );
   
        $data = JRequest::get( 'post' );
        // Bind the form fields to the table
        if (!$row->bind($data)) {
            $this->setError($this->_db->getErrorMsg());
            return false;
        }
   
        // Make sure the record is valid
        if (!$row->check()) {
            $this->setError($this->_db->getErrorMsg());
            return false;
        }
   
        // Store the table to the database
        if (!$row->store()) {
            $this->setError($this->_db->getErrorMsg());
            return false;
        }
   
        return true;
    }


I noticed in weblinks when saving the description the same problem (is it a problem?): html tags are stripped but the special characters are not escaped:
This text

Code: Select all

<strong>This is a path: "../html/joomla/components" and this is a backslash \</strong>

is saved in the database as

Code: Select all

This is a path: "../html/joomla/components" and this is a backslash \

Should'nt it be like this?

Code: Select all

This is a path: "..\/html\/joomla\/components" and this is a backslash \\


Thanks for helping me.

Papi

User avatar
CirTap
Joomla! Intern
Joomla! Intern
Posts: 73
Joined: Mon Dec 12, 2005 5:34 pm
Contact:

Re: How to clean array before writing in database?

Post by CirTap » Mon Aug 13, 2007 9:47 am

Hi,

any requirement to strip special characters depend on the context, input source, and output target.
If you'd dump this description text as an argument to an external program (ie. via shell) there could indeed be issues with the slashes if they're not further escaped/quoted, but the content in this case is supposed to be used as a "text node" in an HTML or XML document, hence / and \ can be considered "save characters" for this target medium.

Have fun,
CirTap
You can have programs written fast, well, and cheap, but you only get to pick 2 ...

"I love deadlines. I like the whooshing sound they make as they fly by." Douglas Adams

User avatar
Papillon
Joomla! Fledgling
Joomla! Fledgling
Posts: 4
Joined: Thu Aug 18, 2005 2:08 pm
Location: Good old Germany

Re: How to clean array before writing in database?

Post by Papillon » Mon Aug 13, 2007 11:53 am

Hello CirTap,

thank you for the answer and the explication. This helps me a lot.

Greetings
Papi

User avatar
jlleblanc
Joomla! Apprentice
Joomla! Apprentice
Posts: 30
Joined: Fri Aug 19, 2005 12:37 am
Location: Washington, DC / NoVA
Contact:

Re: How to clean array before writing in database?

Post by jlleblanc » Mon Aug 13, 2007 12:57 pm

Yeah, you're using JTable, it's escaping them.

When you save the escaped variables to the database, the extra slashes do not appear in them. The SQL string going to the database will contain them, but you won't see them in the final database listing.

To get your HTML to come through, make sure that you pass JREQUEST_ALLOWHTML as your second parameter to get():

Code: Select all

$data = JRequest::get( 'post', JREQUEST_ALLOWHTML);


However, you must make sure that the HTML is from a trusted source (trusted authenticated user, admin, etc...). It is generally not a good idea to accept straight HTML from anonymous sources if the HTML will be redisplayed back on your website (especially with Javascript XSS attacks).
Joseph L. LeBlanc: http://www.jlleblanc.com
Frontend components start here: /components/com_[name]/[name].php
Backend components start here: /administrator/components/com_[name]/admin.[name].php

User avatar
Papillon
Joomla! Fledgling
Joomla! Fledgling
Posts: 4
Joined: Thu Aug 18, 2005 2:08 pm
Location: Good old Germany

Re: How to clean array before writing in database?

Post by Papillon » Mon Aug 13, 2007 2:11 pm

Thank you for the clarification, Joseph. I don't want to accept Html from the form. I only noticed that html is stripped and the rest isn't escaped.
Hence

Code: Select all

$data = JRequest::get( 'post');
is the right thing to me.

User avatar
bascherz
Joomla! Intern
Joomla! Intern
Posts: 86
Joined: Mon Jan 16, 2006 1:33 am
Location: Vienna, VA
Contact:

Re: How to clean array before writing in database?

Post by bascherz » Mon Aug 13, 2007 5:50 pm

AmyStephen wrote:Papi -

I am not able to answer that question, but I want to point out How to make your Joomla! addon more secure, a wiki resource that the core developers put together. I am guessing you will find help in that. If you don't though, let us know. Someone else certainly can help with your question.

Thanks!
Amy :)


Whoa! Great information. BUT GUESS WHAT?! When I went to the link in Amy's post I was not logged in and yet I saw "Edit" links all over the page!! I am sure this was unintentional.

EDIT: This occurs with all pages rooted at http://dev.joomla.org/component/option,com_jd-wiki
I am logged into the forum only, not into dev.joomla.org and I am able to see "Edit" links. Is this intentional? If so, we can start adding content from this group NOW! But my guess is that this is an oversight on someone's part.
Last edited by bascherz on Mon Aug 13, 2007 5:54 pm, edited 1 time in total.
__________________
Bruce Scherzinger

User avatar
Chris Davenport
Joomla! Intern
Joomla! Intern
Posts: 95
Joined: Thu Aug 18, 2005 8:57 am
Location: Shrewsbury, Shropshire, United Kingdom

Re: How to clean array before writing in database?

Post by Chris Davenport » Mon Aug 13, 2007 6:07 pm

Don't panic!!!  The wiki is not wide open.  Although the Edit buttons appear, if you actually try to save any changes, or even preview them, you will get "Permission Denied".

Ian has been looking into changing the access control for the wiki to allow collaborative development of the new template tutorial.  I'd also like to see an area of the dev wiki set aside for this group.  Just give us a bit of time to work through the issues.  We have to be careful as the joomla.org domain is high-volume and is under constant attack by spammers.

Regards,
Chris.
Joomla! Core Team Member | Documentation Working Group Coordinator

"Reality is merely an illusion, although a very persistent one" - Albert Einstein
"We are suspended in language such that we don't know what is up and what is down" - Niels Bohr

User avatar
Rogue4ngel
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 199
Joined: Sun Nov 26, 2006 10:46 pm
Location: New York

Re: How to clean array before writing in database?

Post by Rogue4ngel » Tue Aug 14, 2007 1:04 pm

That would be outstanding Chris.  With the collective work we have so far in this forum, I can forsee it being quite a work that will be a wonderful resource for our joombies!

Thanks for your efforts on this.
If you're not a part of the solution, you're a part of the problem.


Post Reply