Skip to content

Commit

Permalink
Check on decode() too.
Browse files Browse the repository at this point in the history
  • Loading branch information
yunosh committed Jan 24, 2017
1 parent 36b0073 commit 555177c
Showing 1 changed file with 70 additions and 56 deletions.
126 changes: 70 additions & 56 deletions framework/Idna/lib/Horde/Idna.php
Expand Up @@ -40,61 +40,7 @@ public static function encode($data)

case 'INTL_UTS46':
$result = idn_to_ascii($data, 0, INTL_IDNA_VARIANT_UTS46, $info);
switch (true) {
case $info['errors'] & IDNA_ERROR_EMPTY_LABEL:
throw new Horde_Idna_Exception(Horde_Idna_Translation::t(
"Domain name is empty"
));
case $info['errors'] & IDNA_ERROR_LABEL_TOO_LONG:
case $info['errors'] & IDNA_ERROR_DOMAIN_NAME_TOO_LONG:
throw new Horde_Idna_Exception(Horde_Idna_Translation::t(
"Domain name is too long"
));
case $info['errors'] & IDNA_ERROR_LEADING_HYPHEN:
throw new Horde_Idna_Exception(Horde_Idna_Translation::t(
"Starts with a hyphen"
));
case $info['errors'] & IDNA_ERROR_TRAILING_HYPHEN:
throw new Horde_Idna_Exception(Horde_Idna_Translation::t(
"Ends with a hyphen"
));
case $info['errors'] & IDNA_ERROR_HYPHEN_3_4:
throw new Horde_Idna_Exception(Horde_Idna_Translation::t(
"Contains hyphen in the third and fourth positions"
));
case $info['errors'] & IDNA_ERROR_LEADING_COMBINING_MARK:
throw new Horde_Idna_Exception(Horde_Idna_Translation::t(
"Starts with a combining mark"
));
case $info['errors'] & IDNA_ERROR_DISALLOWED:
throw new Horde_Idna_Exception(Horde_Idna_Translation::t(
"Contains disallowed characters"
));
case $info['errors'] & IDNA_ERROR_PUNYCODE:
throw new Horde_Idna_Exception(Horde_Idna_Translation::t(
"Starts with \"xn--\" but does not contain valid Punycode"
));
case $info['errors'] & IDNA_ERROR_LABEL_HAS_DOT:
throw new Horde_Idna_Exception(Horde_Idna_Translation::t(
"Contains a dot"
));
case $info['errors'] & IDNA_ERROR_INVALID_ACE_LABEL:
throw new Horde_Idna_Exception(Horde_Idna_Translation::t(
"ACE label does not contain a valid label string"
));
case $info['errors'] & IDNA_ERROR_BIDI:
throw new Horde_Idna_Exception(Horde_Idna_Translation::t(
"Does not meet the IDNA BiDi requirements (for right-to-left characters)"
));
case $info['errors'] & IDNA_ERROR_CONTEXTJ:
throw new Horde_Idna_Exception(Horde_Idna_Translation::t(
"Does not meet the IDNA CONTEXTJ requirements"
));
case $info['errors']:
throw new Horde_Idna_Exception(Horde_Idna_Translation::t(
"Unknown error"
));
}
$this->_checkForError($info);
return $result;

default:
Expand All @@ -119,7 +65,8 @@ public static function decode($data)
break;

case 'INTL_UTS46':
$part = idn_to_utf8($part, 0, INTL_IDNA_VARIANT_UTS46);
$part = idn_to_utf8($part, 0, INTL_IDNA_VARIANT_UTS46, $info);
$this->_checkForError($info);
break;
}
}
Expand All @@ -131,6 +78,73 @@ public static function decode($data)
}
}

/**
* Checks if the $idna_info parameter of idn_to_ascii() or idn_to_utf8()
* contains errors.
*
* @param array $info Fourth parameter to idn_to_ascii() or idn_to_utf8().
*
* @throws Horde_Idna_Exception
*/
protected function _checkForError($info)
{
switch (true) {
case $info['errors'] & IDNA_ERROR_EMPTY_LABEL:
throw new Horde_Idna_Exception(Horde_Idna_Translation::t(
"Domain name is empty"
));
case $info['errors'] & IDNA_ERROR_LABEL_TOO_LONG:
case $info['errors'] & IDNA_ERROR_DOMAIN_NAME_TOO_LONG:
throw new Horde_Idna_Exception(Horde_Idna_Translation::t(
"Domain name is too long"
));
case $info['errors'] & IDNA_ERROR_LEADING_HYPHEN:
throw new Horde_Idna_Exception(Horde_Idna_Translation::t(
"Starts with a hyphen"
));
case $info['errors'] & IDNA_ERROR_TRAILING_HYPHEN:
throw new Horde_Idna_Exception(Horde_Idna_Translation::t(
"Ends with a hyphen"
));
case $info['errors'] & IDNA_ERROR_HYPHEN_3_4:
throw new Horde_Idna_Exception(Horde_Idna_Translation::t(
"Contains hyphen in the third and fourth positions"
));
case $info['errors'] & IDNA_ERROR_LEADING_COMBINING_MARK:
throw new Horde_Idna_Exception(Horde_Idna_Translation::t(
"Starts with a combining mark"
));
case $info['errors'] & IDNA_ERROR_DISALLOWED:
throw new Horde_Idna_Exception(Horde_Idna_Translation::t(
"Contains disallowed characters"
));
case $info['errors'] & IDNA_ERROR_PUNYCODE:
throw new Horde_Idna_Exception(Horde_Idna_Translation::t(
"Starts with \"xn--\" but does not contain valid Punycode"
));
case $info['errors'] & IDNA_ERROR_LABEL_HAS_DOT:
throw new Horde_Idna_Exception(Horde_Idna_Translation::t(
"Contains a dot"
));
case $info['errors'] & IDNA_ERROR_INVALID_ACE_LABEL:
throw new Horde_Idna_Exception(Horde_Idna_Translation::t(
"ACE label does not contain a valid label string"
));
case $info['errors'] & IDNA_ERROR_BIDI:
throw new Horde_Idna_Exception(Horde_Idna_Translation::t(
"Does not meet the IDNA BiDi requirements (for right-to-left characters)"
));
case $info['errors'] & IDNA_ERROR_CONTEXTJ:
throw new Horde_Idna_Exception(Horde_Idna_Translation::t(
"Does not meet the IDNA CONTEXTJ requirements"
));
case $info['errors']:
throw new Horde_Idna_Exception(Horde_Idna_Translation::t(
"Unknown error"
));
}
}

/**
* Return the IDNA backend.
*
Expand Down

0 comments on commit 555177c

Please sign in to comment.