All posts

Iterate and convert an XSS collection to hash in Ruby

I was working with a collection of elements in an XSS payload. Here’s the payload:

<fields>
  <entry>
    <key>Email</key>
    <value>terry.blogger@blogs.com</value>
  </entry>
  <entry>
    <key>GivenName</key>
    <value>Terry</value>
  </entry>
  <entry>
    <key>FamilyName</key>
    <value>Blogger</value>
  </entry>
</fields>

In order to work with this payload I first used Nokogiri to turn the payload into an XML document: document = Nokogiri::XML(response_string)

Then I passed that document to a class method that converted the values to a formatted hash that would be easier to work with. I used the css method to iterate ones over each field and then a second time to the entry field name and values for the hash:

def fields_to_hash(document)
  account_hash = {}

  document.css("fields").map do |additional_params|
    additional_params.css("entry").map do |param|
      account_hash[param.css("key").text] = param.css("value").text
    end
  end

  account_hash
end

I hope this helped you in solving whichever problem brought you here.

Postscript:

I built two products to help fund continued efforts for this website, it would mean a lot if you would check them out:
1. 2500+ Words for the Undermotivated Software Engineer - A short essay I wrote on the titled topic ($3).
2. Build 3 Chrome Extensions in Under 2 Hours - A course I built to teach chrome extension development ($9-20).