Bu verimli ve kullanıcı girişi arındırmak için güvenilir bir yol olabilir mi?

3 Cevap php

im wondering about how to set up a clever way to have all my input 'clean', a procedure to run at the begin of every my script. I thought to create a class to do that, and then, add a 2 letter prefix in the begin of every input to identify the kind of input, for example:

in-mynumber
tx-name
ph-phone
em-email

Yani, benim komut üstündeki ben sadece (örneğin) bir işlevi çalıştırabilirsiniz:

function cleanInputs(){
    foreach($_GET AS $taintedKey => $taintedValue){
        $prefix = substr($taintedKey, 0, 2);
        switch($prefix){
            case 'in':
                //I assume this input is an integer
                $cGet[$taintedKey] = intval($taintedValue);
                break;
            case 'tx':
                //i assume this input is a normal text
                //can contains onely letters, numbers and few symbols
                if(preg_match($regExp, $taintedValue)){
                    $cGet[$taintedKey] = $taintedValue;
                }else{
                    $cGet[$taintedKey] = false;
                }
                break;
            case 'em':
                //i assume this input is a valid email
                if(preg_match('/^[a-zA-Z0-9-_.]+@[a-zA-Z0-9-_.]+.[a-zA-Z]{2,4}$/', $taintedValue)){
                    $cGet[$taintedKey] = $taintedValue;
                }else{
                    $cGet[$taintedKey] = false;
                }
                break;
        }
    }
}

..so i'll create other 2 arrays, $cGet and $cPost with the clean data respectively of $_GET and $_POST, and in my script i'lllook for use those arrays, completely forget the $_GET/$_POST I'm even thinkin about add a second prefix to determinate the input's max lenght... for example: tx-25-name ..but im not pretty sure about that.. and if i take this way, maybe a OOP approach will be better.

What do you think about that? Seem be a good way to use?

The negatives point that i can actually see (i havent still used that way, is just a wonder of this morning) 1. The prefix, and so the procedures, must be many if i want my application not to be much restrictive; 2. My sent variable's names will become little longer (but we are talking of 3-6 chars, shouldnt be a problem)

Herhangi bir öneri gerçekten takdir!

EDIT:

Im not triyn to reinvent the wheel, my post was't about the sistem to sanitizing input, but is about the procedure to do it. I use htmlpurifier to clen the possibly xss injection in html data, and of course i use the parametrized queryes. Im just wondering if is better take input by input, or sanitize them all at the begin and consider they clean in the rest of the script. The method i thougt is not miracolous and nothing new under the sun, but i think that truncate the input if is not in the format that i aspect, can be usefull...

Why check for sql injection in the 'name' field, that must contain just letters and the apostrophe char? Just remove everythings that is not letter or apostophe, add slashes for the last one, and run into a parametrized query. Then, if you aspect an email, just delete everythings that is not an email..

3 Cevap

Fikir gerçekten çok faydalı olacaktır ancak ben merak ediyorum, kendisi de gayet iyi.

Bir kere, SQL enjeksiyonu ve HTML enjeksiyonları başka bir şekilde korunmalıdır (olmalı) olabilir. SQL enjeksiyonu parametrized sorguları tarafından engellenir (bir bu gün ve yaş olması gerekir); ve HTML enjeksiyonlar right before outputting the string to the user aranmalıdır htmlspecialchars() yöntemi ile önlenir. DB veya (daha kötüsü) kodlanmış dizeleri tutmayın - en kısa sürede onları aldıktan olarak kodlamak. Onlarla çalışmak daha sonra bir cehennem olacak.

Bu iki enjeksiyon saldırıları dışında, yöntem ne yapacak? Peki, bu sayılar, telefon numaraları, e-postalar, isimler ve tarihler gibi şeyler için bazı İfadelerinin yapabilirsiniz. Ama bu konuda. Ne yazık ki, sadece yapmanız gereken tüm doğrulamaları bir parçası. Orada doğrulamak olamaz Diğer sık ​​durumlar girişlerin (bitiş tarihinden önce başlangıç ​​tarihi) çapraz kontrolü ve bir değer (bir <select> elemanı için diyelim ki,) izin verilen önceden tanımlanmış değerler listesinde olduğunu kontrol. Ve siz de uygulama olacak özel doğrulama adımlarla sonsuz vardır. Bu "genel tür doğrulama" ve "özel kural doğrulama" tüm doğrulama bölmek için değer mi? Bilmiyorum. Belki. Ya da belki de bu sadece daha büyük bir karışıklık yapacak.

Zaten girişleri sterilize ki well-made PHP tested classes birçok vardır. Neden başka biri yapmak? Ayrıca, giriş sanitasyon sadece veri türlerini doğrulayarak daha fazladır. Vb sql enjeksiyon, XSS saldırıları, kontrol ima ..

Ne yapmaya çalışıyorsun? Eğer veritabanına verileri kaydetmek için girdiyi gerekiyorsa, parametreli sorgular daha iyi bir şey yok.

this bir örnek için bkz.