Skip to content

php-unserializer unable to parse pipe/semi-colon formatted strings #2

Description

@arlowhite

I'm working with a PHP app that uses Predis for Sessions.

        $client = new Predis\Client(REDIS_SERVER, array('prefix' => 'myprefix:'));
        $handler = new Predis\Session\Handler($client);

The serialized values look like this:

uid|s:11:"foo@bar.com";uname|s:10:"Arlo White";SUPER-USER|i:1;ADMIN|i:1;USER|i:1;past_if_in_env|s:4:"true";ALERT_COUNT|s:1:"0";LAST_ALERT_CHECK|i:1475694861;

I'm not sure what this pipe/semi-colon syntax is, but php-unserializer can't parse it. (Neither can https://github.com/kayahr/pherialize)

I wrote some code to deal with it, feel free to add it to php-unserializer.

  def parse(value: String): Map[String, Any] = {
    val map = new mutable.HashMap[String, Any]()
    var fieldStartIndex = 0
    var nextIndex = 0
    var pipeIndex = 0

    while (fieldStartIndex < value.length) {
      pipeIndex = value.indexOf('|', fieldStartIndex)
      // PHP Session Key
      val key: String = value.substring(fieldStartIndex, pipeIndex)
      nextIndex = value.indexOf('|', pipeIndex + 1)
      if (nextIndex == -1) {
        // Not found, we should be on the last key/value
        nextIndex = value.length - 1
        if (value.charAt(nextIndex) != ';') {
          Logger.warn(s"Session value does not end with ';' key $key may have parse error. Value: $value")
        }
      }
      else {
        // Pipe found, search backwards for semi-colon
        nextIndex = value.lastIndexOf(';', nextIndex)
      }

      // Looks like s:10:"Arlo White"; OR i:1234;
      val serializedValue = value.substring(pipeIndex + 1, nextIndex + 1)
      map.put(key, PhpUnserializer.parse(serializedValue))

      fieldStartIndex = nextIndex + 1
    }

    map.toMap
  }

You'll have to change the Logger line, throw an Exception there I suppose.

Activity

  1. changed the title [-]php-[/-] [+]php-unserializer unable to parse pipe/semi-colon formatted strings[/+] on Oct 5, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions