Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7855201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:18:57+00:00 2026-06-02T20:18:57+00:00

I am getting some data in JSON format via jQuery and then I need

  • 0

I am getting some data in JSON format via jQuery and then I need to display it to users, however it is vulnerable to XSS attack. What are my options here, should I strip chars before I put the data in database? Framework that I am using (Kohana) has a nifty function HTML::Chars();, but since I am displaying data with javascript I can’t use it there.

One option seems to be is to walk through every array element that is being json encoded and apply HTML::Chars(); to it. Is it the only option, and if so than what would be optimal way of doing this?

Example:

  1. User enters some data: title, body
  2. Data is stored into database
  3. Then some other user enters site, data array is taken from database
    and exported to json format
  4. My jQuery script is taking the json and appending new element to
    body of my page.

Code:

$(document).ready(function(){
    $.ajax({
        url: '/timeline/latest/1',
        dataType: 'json',
        success: function(data){
            $.each(data, function(key, val) {
                switch (val.type){
                    case 'post': // I have only made post so far
                        addPost(val);
                        break;
                }
            });
        }
    });
})
function addPost(val){
    $('.content .timeline').prepend(val.title + '<br />' + val.body); // xss vulnerable
}

Getting data from database

<?php

class Controller_Timeline extends Controller{
    public function Action_Latest(){
        $parentID = $this->request->param('id');
        $modelTimeline = new Model_Timeline();

        // Here I get latest entries, big array
        $latest = $modelTimeline->Latest($parentID);

        // Response it and encode with JSON
        $this->response->body(json_encode($latest));
    }
}

My solution so far is this, before I echo out the $latest I walk through array and apply the anti-xss function, I don’t know how optimal it is however:

array_walk($latest, function(&$latest){
    foreach ($latest as &$key){
        $key = HTML::chars($key);
    }
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T20:18:59+00:00Added an answer on June 2, 2026 at 8:18 pm

    I suggest you do it like Drupal.

    Drupal doesn’t filter any input. It stores the text in the database with XSS vulnerabilities if this HTML code were to be displayed without being filtered.

    Instead, it filters on the output. You should definitely do that on the server side.

    You should inspire yourself from the filter_xss() function Drupal uses.

    Make sure it’s applied for each of your entry. Do it globally if you can of course, like the following:

    // In the Model_Timeline class
    public function Latest( $id ) {
    
        // Get your array, and then
        foreach ( $array as $entry ) {
    
            // Filter each entry
            // I use $util->filter_xss but use it how you implemented it
            $entry = $util->filter_xss( $entry );
        }
    
        // And return the filtered array
        return $array;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some trouble getting jquery to post some json data to a rest
I'm having a little dilemma with an iphone project. I'm getting some JSON data
I'm using jQuery to grab some JSON data. I've stored it in a variable
I am getting some data from a database and am encoding it to json:
I am trying to handle some JSON data. But what I am getting seems
I'm having problems deserializing some json data, getting InvalidCastExceptions and the like. Can anyone
I am using jQuery to get back some JSON data from the server. I
I'm having some problems posting JSON data to my rails controller and getting the
I'm getting some data from the web service and saving it in the core
I am getting some data from my database table. The query I use is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.