Skip to content

Commit

Permalink
do not rerun expensive sanitizer against already processed DNs
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Schiwon <[email protected]>
  • Loading branch information
blizzz committed Apr 17, 2020
1 parent 32000dd commit ab550d6
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions apps/user_ldap/lib/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,25 @@

namespace OCA\User_LDAP;

use OC\Cache\CappedMemoryCache;
use OCP\IConfig;

class Helper {

/** @var IConfig */
private $config;

/** @var CappedMemoryCache */
protected $sanitizeDnCache;

/**
* Helper constructor.
*
* @param IConfig $config
*/
public function __construct(IConfig $config) {
$this->config = $config;
$this->sanitizeDnCache = new CappedMemoryCache(10000);
}

/**
Expand Down Expand Up @@ -242,12 +247,20 @@ public function sanitizeDN($dn) {
return $result;
}

if(!is_string($dn)) {
throw new \LogicException('String expected ' . \gettype($dn) . ' given');
}

if (($sanitizedDn = $this->sanitizeDnCache->get($dn)) !== null) {
return $sanitizedDn;
}

//OID sometimes gives back DNs with whitespace after the comma
// a la "uid=foo, cn=bar, dn=..." We need to tackle this!
$dn = preg_replace('/([^\\\]),(\s+)/u', '\1,', $dn);
$sanitizedDn = preg_replace('/([^\\\]),(\s+)/u', '\1,', $dn);

//make comparisons and everything work
$dn = mb_strtolower($dn, 'UTF-8');
$sanitizedDn = mb_strtolower($sanitizedDn, 'UTF-8');

//escape DN values according to RFC 2253 – this is already done by ldap_explode_dn
//to use the DN in search filters, \ needs to be escaped to \5c additionally
Expand All @@ -265,9 +278,10 @@ public function sanitizeDN($dn) {
')' => '\29',
'*' => '\2A',
];
$dn = str_replace(array_keys($replacements), array_values($replacements), $dn);
$sanitizedDn = str_replace(array_keys($replacements), array_values($replacements), $sanitizedDn);
$this->sanitizeDnCache->set($dn, $sanitizedDn);

return $dn;
return $sanitizedDn;
}

/**
Expand Down

0 comments on commit ab550d6

Please sign in to comment.