https://issues.shibboleth.net/jira/browse/SSPCPP-589
[shibboleth/cpp-sp.git] / plugins / TransformAttributeResolver.cpp
index 1856d3e..e036748 100644 (file)
@@ -28,6 +28,7 @@
 
 #include <algorithm>
 #include <boost/shared_ptr.hpp>
+#include <boost/algorithm/string/trim.hpp>
 #include <boost/tuple/tuple.hpp>
 #include <shibsp/exceptions.h>
 #include <shibsp/SessionCache.h>
@@ -226,18 +227,26 @@ void TransformAttributeResolver::resolveAttributes(ResolutionContext& ctx) const
                 try {
                     auto_arrayptr<XMLCh> srcval(fromUTF8((*a)->getSerializedValues()[i].c_str()));
                     XMLCh* destval = r->get<1>()->replace(srcval.get(), r->get<2>());
-                    if (destval) {
+                    if (!destval)
+                        continue;
+                    // For some reason, it returns the source string if the match doesn't succeed.
+                    if (!XMLString::equals(destval, srcval.get())) {
                         auto_arrayptr<char> narrow(toUTF8(destval));
                         XMLString::release(&destval);
                         if (dest) {
                             // Modify in place.
                             dest->getValues()[i] = narrow.get();
+                            trim(dest->getValues()[i]);
                         }
                         else {
                             // Add to new object.
                             destwrapper->getValues().push_back(narrow.get());
+                            trim(destwrapper->getValues().back());
                         }
                     }
+                    else {
+                        XMLString::release(&destval);
+                    }
                 }
                 catch (XMLException& ex) {
                     auto_ptr_char msg(ex.getMessage());