2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
22 * XMLAttributeFilter.cpp
24 * AttributeFilter based on an XML policy language.
28 #include "exceptions.h"
29 #include "Application.h"
30 #include "ServiceProvider.h"
31 #include "attribute/Attribute.h"
32 #include "attribute/filtering/AttributeFilter.h"
33 #include "attribute/filtering/FilteringContext.h"
34 #include "attribute/filtering/FilterPolicyContext.h"
35 #include "attribute/filtering/MatchFunctor.h"
36 #include "util/SPConstants.h"
38 #include <boost/iterator/indirect_iterator.hpp>
39 #include <boost/tuple/tuple.hpp>
40 #include <xmltooling/util/NDC.h>
41 #include <xmltooling/util/ReloadableXMLFile.h>
42 #include <xmltooling/util/Threads.h>
43 #include <xmltooling/util/XMLHelper.h>
44 #include <xercesc/util/XMLUniDefs.hpp>
46 using shibspconstants::SHIB2ATTRIBUTEFILTER_NS;
47 using namespace shibsp;
48 using namespace opensaml::saml2md;
49 using namespace opensaml;
50 using namespace xmltooling;
51 using namespace boost;
56 #if defined (_MSC_VER)
57 #pragma warning( push )
58 #pragma warning( disable : 4250 )
61 // Each Policy has a functor for determining applicability and a map of
62 // attribute IDs to Accept/Deny functor pairs (which can include nullptrs).
63 struct SHIBSP_DLLLOCAL Policy
65 Policy() : m_applies(nullptr) {}
66 const MatchFunctor* m_applies;
67 typedef multimap< string,pair<const MatchFunctor*,const MatchFunctor*> > rules_t;
71 class SHIBSP_DLLLOCAL XMLFilterImpl
74 XMLFilterImpl(const DOMElement* e, Category& log);
77 m_document->release();
78 for_each(m_policyReqRules.begin(), m_policyReqRules.end(), cleanup_pair<string,MatchFunctor>());
79 for_each(m_permitValRules.begin(), m_permitValRules.end(), cleanup_pair<string,MatchFunctor>());
80 for_each(m_denyValRules.begin(), m_denyValRules.end(), cleanup_pair<string,MatchFunctor>());
83 void setDocument(DOMDocument* doc) {
87 void filterAttributes(const FilteringContext& context, vector<Attribute*>& attributes) const;
90 MatchFunctor* buildFunctor(
91 const DOMElement* e, const FilterPolicyContext& functorMap, const char* logname, bool standalone
93 tuple<string,const MatchFunctor*,const MatchFunctor*> buildAttributeRule(
94 const DOMElement* e, const FilterPolicyContext& permMap, const FilterPolicyContext& denyMap, bool standalone
98 DOMDocument* m_document;
99 vector<Policy> m_policies;
100 map< string,tuple<string,const MatchFunctor*,const MatchFunctor*> > m_attrRules;
101 multimap<string,MatchFunctor*> m_policyReqRules;
102 multimap<string,MatchFunctor*> m_permitValRules;
103 multimap<string,MatchFunctor*> m_denyValRules;
106 class SHIBSP_DLLLOCAL XMLFilter : public AttributeFilter, public ReloadableXMLFile
109 XMLFilter(const DOMElement* e) : ReloadableXMLFile(e, Category::getInstance(SHIBSP_LOGCAT ".AttributeFilter")) {
116 void filterAttributes(const FilteringContext& context, vector<Attribute*>& attributes) const {
117 m_impl->filterAttributes(context, attributes);
121 pair<bool,DOMElement*> background_load();
124 scoped_ptr<XMLFilterImpl> m_impl;
127 #if defined (_MSC_VER)
128 #pragma warning( pop )
131 AttributeFilter* SHIBSP_DLLLOCAL XMLAttributeFilterFactory(const DOMElement* const & e)
133 return new XMLFilter(e);
136 static const XMLCh AttributeFilterPolicyGroup[] = UNICODE_LITERAL_26(A,t,t,r,i,b,u,t,e,F,i,l,t,e,r,P,o,l,i,c,y,G,r,o,u,p);
137 static const XMLCh AttributeFilterPolicy[] = UNICODE_LITERAL_21(A,t,t,r,i,b,u,t,e,F,i,l,t,e,r,P,o,l,i,c,y);
138 static const XMLCh AttributeRule[] = UNICODE_LITERAL_13(A,t,t,r,i,b,u,t,e,R,u,l,e);
139 static const XMLCh AttributeRuleReference[] = UNICODE_LITERAL_22(A,t,t,r,i,b,u,t,e,R,u,l,e,R,e,f,e,r,e,n,c,e);
140 static const XMLCh DenyValueRule[] = UNICODE_LITERAL_13(D,e,n,y,V,a,l,u,e,R,u,l,e);
141 static const XMLCh DenyValueRuleReference[] = UNICODE_LITERAL_22(D,e,n,y,V,a,l,u,e,R,u,l,e,R,e,f,e,r,e,n,c,e);
142 static const XMLCh PermitValueRule[] = UNICODE_LITERAL_15(P,e,r,m,i,t,V,a,l,u,e,R,u,l,e);
143 static const XMLCh PermitValueRuleReference[] = UNICODE_LITERAL_24(P,e,r,m,i,t,V,a,l,u,e,R,u,l,e,R,e,f,e,r,e,n,c,e);
144 static const XMLCh PolicyRequirementRule[] = UNICODE_LITERAL_21(P,o,l,i,c,y,R,e,q,u,i,r,e,m,e,n,t,R,u,l,e);
145 static const XMLCh PolicyRequirementRuleReference[]=UNICODE_LITERAL_30(P,o,l,i,c,y,R,e,q,u,i,r,e,m,e,n,t,R,u,l,e,R,e,f,e,r,e,n,c,e);
146 static const XMLCh attributeID[] = UNICODE_LITERAL_11(a,t,t,r,i,b,u,t,e,I,D);
147 static const XMLCh _id[] = UNICODE_LITERAL_2(i,d);
148 static const XMLCh _ref[] = UNICODE_LITERAL_3(r,e,f);
151 XMLFilterImpl::XMLFilterImpl(const DOMElement* e, Category& log) : m_log(log), m_document(nullptr)
154 xmltooling::NDC ndc("XMLFilterImpl");
157 if (!XMLHelper::isNodeNamed(e, SHIB2ATTRIBUTEFILTER_NS, AttributeFilterPolicyGroup))
158 throw ConfigurationException("XML AttributeFilter requires afp:AttributeFilterPolicyGroup at root of configuration.");
160 FilterPolicyContext reqFunctors(m_policyReqRules);
161 FilterPolicyContext permFunctors(m_permitValRules);
162 FilterPolicyContext denyFunctors(m_denyValRules);
164 DOMElement* child = XMLHelper::getFirstChildElement(e);
166 if (XMLHelper::isNodeNamed(child, SHIB2ATTRIBUTEFILTER_NS, PolicyRequirementRule)) {
167 buildFunctor(child, reqFunctors, "PolicyRequirementRule", true);
169 else if (XMLHelper::isNodeNamed(child, SHIB2ATTRIBUTEFILTER_NS, PermitValueRule)) {
170 buildFunctor(child, permFunctors, "PermitValueRule", true);
172 else if (XMLHelper::isNodeNamed(child, SHIB2ATTRIBUTEFILTER_NS, DenyValueRule)) {
173 buildFunctor(child, denyFunctors, "DenyValueRule", true);
175 else if (XMLHelper::isNodeNamed(child, SHIB2ATTRIBUTEFILTER_NS, AttributeRule)) {
176 buildAttributeRule(child, permFunctors, denyFunctors, true);
178 else if (XMLHelper::isNodeNamed(child, SHIB2ATTRIBUTEFILTER_NS, AttributeFilterPolicy)) {
179 e = XMLHelper::getFirstChildElement(child);
180 MatchFunctor* func = nullptr;
181 if (e && XMLHelper::isNodeNamed(e, SHIB2ATTRIBUTEFILTER_NS, PolicyRequirementRule)) {
182 func = buildFunctor(e, reqFunctors, "PolicyRequirementRule", false);
184 else if (e && XMLHelper::isNodeNamed(e, SHIB2ATTRIBUTEFILTER_NS, PolicyRequirementRuleReference)) {
185 string ref(XMLHelper::getAttrString(e, nullptr, _ref));
187 multimap<string,MatchFunctor*>::const_iterator prr = m_policyReqRules.find(ref);
188 func = (prr!=m_policyReqRules.end()) ? prr->second : nullptr;
192 m_policies.push_back(Policy());
193 m_policies.back().m_applies = func;
194 e = XMLHelper::getNextSiblingElement(e);
196 if (e && XMLHelper::isNodeNamed(e, SHIB2ATTRIBUTEFILTER_NS, AttributeRule)) {
197 tuple<string,const MatchFunctor*,const MatchFunctor*> rule = buildAttributeRule(e, permFunctors, denyFunctors, false);
198 if (rule.get<1>() || rule.get<2>())
199 m_policies.back().m_rules.insert(Policy::rules_t::value_type(rule.get<0>(), make_pair(rule.get<1>(), rule.get<2>())));
201 else if (e && XMLHelper::isNodeNamed(e, SHIB2ATTRIBUTEFILTER_NS, AttributeRuleReference)) {
202 string ref(XMLHelper::getAttrString(e, nullptr, _ref));
204 map< string,tuple<string,const MatchFunctor*,const MatchFunctor*> >::const_iterator ar = m_attrRules.find(ref);
205 if (ar != m_attrRules.end()) {
206 m_policies.back().m_rules.insert(
207 Policy::rules_t::value_type(ar->second.get<0>(), make_pair(ar->second.get<1>(), ar->second.get<2>()))
211 m_log.warn("skipping invalid AttributeRuleReference (%s)", ref.c_str());
215 e = XMLHelper::getNextSiblingElement(e);
219 m_log.warn("skipping AttributeFilterPolicy, PolicyRequirementRule invalid or missing");
222 child = XMLHelper::getNextSiblingElement(child);
226 MatchFunctor* XMLFilterImpl::buildFunctor(
227 const DOMElement* e, const FilterPolicyContext& functorMap, const char* logname, bool standalone
230 string id(XMLHelper::getAttrString(e, nullptr, _id));
232 if (standalone && id.empty()) {
233 m_log.warn("skipping stand-alone %s with no id", logname);
236 else if (!id.empty() && functorMap.getMatchFunctors().count(id)) {
238 m_log.warn("skipping duplicate stand-alone %s with id (%s)", logname, id.c_str());
245 scoped_ptr<xmltooling::QName> type(XMLHelper::getXSIType(e));
248 auto_ptr<MatchFunctor> func(SPConfig::getConfig().MatchFunctorManager.newPlugin(*type, make_pair(&functorMap,e)));
249 functorMap.getMatchFunctors().insert(multimap<string,MatchFunctor*>::value_type(id, func.get()));
250 return func.release();
252 catch (exception& ex) {
253 m_log.error("error building %s with type (%s): %s", logname, type->toString().c_str(), ex.what());
257 m_log.warn("skipping stand-alone %s with no xsi:type", logname);
259 m_log.error("%s with no xsi:type", logname);
264 tuple<string,const MatchFunctor*,const MatchFunctor*> XMLFilterImpl::buildAttributeRule(
265 const DOMElement* e, const FilterPolicyContext& permMap, const FilterPolicyContext& denyMap, bool standalone
268 string id(XMLHelper::getAttrString(e, nullptr, _id));
270 if (standalone && id.empty()) {
271 m_log.warn("skipping stand-alone AttributeRule with no id");
272 return tuple<string,const MatchFunctor*,const MatchFunctor*>(string(),nullptr,nullptr);
274 else if (!id.empty() && m_attrRules.count(id)) {
276 m_log.warn("skipping duplicate stand-alone AttributeRule with id (%s)", id.c_str());
277 return tuple<string,const MatchFunctor*,const MatchFunctor*>(string(),nullptr,nullptr);
283 string attrID(XMLHelper::getAttrString(e, nullptr, attributeID));
285 m_log.warn("skipping AttributeRule with no attributeID");
287 MatchFunctor* perm = nullptr;
288 MatchFunctor* deny = nullptr;
290 e = XMLHelper::getFirstChildElement(e);
291 if (e && XMLHelper::isNodeNamed(e, SHIB2ATTRIBUTEFILTER_NS, PermitValueRule)) {
292 perm = buildFunctor(e, permMap, "PermitValueRule", false);
293 e = XMLHelper::getNextSiblingElement(e);
295 else if (e && XMLHelper::isNodeNamed(e, SHIB2ATTRIBUTEFILTER_NS, PermitValueRuleReference)) {
296 string ref(XMLHelper::getAttrString(e, nullptr, _ref));
298 multimap<string,MatchFunctor*>::const_iterator pvr = m_permitValRules.find(ref);
299 perm = (pvr!=m_permitValRules.end()) ? pvr->second : nullptr;
301 e = XMLHelper::getNextSiblingElement(e);
304 if (e && XMLHelper::isNodeNamed(e, SHIB2ATTRIBUTEFILTER_NS, DenyValueRule)) {
305 deny = buildFunctor(e, denyMap, "DenyValueRule", false);
307 else if (e && XMLHelper::isNodeNamed(e, SHIB2ATTRIBUTEFILTER_NS, DenyValueRuleReference)) {
308 string ref(XMLHelper::getAttrString(e, nullptr, _ref));
310 multimap<string,MatchFunctor*>::const_iterator pvr = m_denyValRules.find(ref);
311 deny = (pvr!=m_denyValRules.end()) ? pvr->second : nullptr;
317 m_attrRules[id] = make_tuple(attrID, perm, deny);
318 return m_attrRules[id];
321 return make_tuple(attrID, perm, deny);
326 m_log.warn("skipping AttributeRule (%s), permit and denial rule(s) invalid or missing", id.c_str());
328 m_log.warn("skipping AttributeRule, permit and denial rule(s) invalid or missing");
329 return tuple<string,const MatchFunctor*,const MatchFunctor*>(string(),nullptr,nullptr);
332 void XMLFilterImpl::filterAttributes(const FilteringContext& context, vector<Attribute*>& attributes) const
334 auto_ptr_char issuer(context.getAttributeIssuer());
336 m_log.debug("filtering %lu attribute(s) from (%s)", attributes.size(), issuer.get() ? issuer.get() : "unknown source");
338 if (m_policies.empty()) {
339 m_log.warn("no filter policies were loaded, filtering out all attributes from (%s)", issuer.get() ? issuer.get() : "unknown source");
340 for_each(attributes.begin(), attributes.end(), xmltooling::cleanup<Attribute>());
345 // We have to evaluate every policy that applies against each attribute before deciding what to keep.
347 // For efficiency, we build an array of the policies that apply in advance.
348 vector<const Policy*> applicablePolicies;
349 for (vector<Policy>::const_iterator p = m_policies.begin(); p != m_policies.end(); ++p) {
350 if (p->m_applies->evaluatePolicyRequirement(context))
351 applicablePolicies.push_back(&(*p));
354 // For further efficiency, we declare arrays to store the applicable rules for an Attribute.
355 vector< pair<const MatchFunctor*,const MatchFunctor*> > applicableRules;
356 vector< pair<const MatchFunctor*,const MatchFunctor*> > wildcardRules;
358 // Store off the wildcards ahead of time.
359 for (indirect_iterator<vector<const Policy*>::const_iterator> pol = make_indirect_iterator(applicablePolicies.begin());
360 pol != make_indirect_iterator(applicablePolicies.end()); ++pol) {
361 pair<Policy::rules_t::const_iterator,Policy::rules_t::const_iterator> rules = pol->m_rules.equal_range("*");
362 for (; rules.first!=rules.second; ++rules.first)
363 wildcardRules.push_back(rules.first->second);
366 // To track what to keep without removing anything from the original set until the end, we maintain
367 // a map of each Attribute object to a boolean array with true flags indicating what to delete.
368 // A single dimension array tracks attributes being removed entirely.
369 vector<bool> deletedAttributes(attributes.size(), false);
370 map< Attribute*, vector<bool> > deletedPositions;
372 // Loop over each attribute to filter them.
373 for (vector<Attribute*>::size_type a = 0; a < attributes.size(); ++a) {
374 Attribute* attr = attributes[a];
376 // Clear the rule store.
377 applicableRules.clear();
379 // Look for rules to run in each policy.
380 for (indirect_iterator<vector<const Policy*>::const_iterator> pol = make_indirect_iterator(applicablePolicies.begin());
381 pol != make_indirect_iterator(applicablePolicies.end()); ++pol) {
382 pair<Policy::rules_t::const_iterator,Policy::rules_t::const_iterator> rules = pol->m_rules.equal_range(attr->getId());
383 for (; rules.first!=rules.second; ++rules.first)
384 applicableRules.push_back(rules.first->second);
387 // If no rules found, apply wildcards.
388 const vector< pair<const MatchFunctor*,const MatchFunctor*> >& rulesToRun =
389 applicableRules.empty() ? wildcardRules : applicableRules;
391 // If no rules apply, remove the attribute entirely.
392 if (rulesToRun.empty()) {
394 "no rule found, will remove attribute (%s) from (%s)",
395 attr->getId(), issuer.get() ? issuer.get() : "unknown source"
397 deletedAttributes[a] = true;
401 // Run each permit/deny rule.
403 "applying filtering rule(s) for attribute (%s) from (%s)",
404 attr->getId(), issuer.get() ? issuer.get() : "unknown source"
409 // Examine each value.
410 for (size_t count = attr->valueCount(), index = 0; index < count; ++index) {
412 // Assume we're kicking it out.
415 for (vector< pair<const MatchFunctor*,const MatchFunctor*> >::const_iterator r = rulesToRun.begin(); r != rulesToRun.end(); ++r) {
416 // If there's a permit rule that passes, don't kick it.
417 if (r->first && r->first->evaluatePermitValue(context, *attr, index))
419 if (!kickit && r->second && r->second->evaluatePermitValue(context, *attr, index))
423 // If we're kicking it, record that in the tracker.
426 "removed value at position (%lu) of attribute (%s) from (%s)",
427 index, attr->getId(), issuer.get() ? issuer.get() : "unknown source"
429 deletedPositions[attr].resize(index+1);
430 deletedPositions[attr][index] = true;
435 // Final step: go over the deletedPositions matrix and apply the actual changes. In order to delete
436 // any attributes that end up with no values, we have to do it by looping over the originals.
437 for (vector<Attribute*>::size_type a = 0; a < attributes.size();) {
438 Attribute* attr = attributes[a];
440 if (deletedAttributes[a]) {
442 "removing filtered attribute (%s) from (%s)",
443 attr->getId(), issuer.get() ? issuer.get() : "unknown source"
446 deletedAttributes.erase(deletedAttributes.begin() + a);
447 attributes.erase(attributes.begin() + a);
450 else if (deletedPositions.count(attr) > 0) {
451 // To do the removal, we loop over the bits backwards so that the
452 // underlying value sequence doesn't get distorted by any removals.
453 // Index has to be offset by one because size_type is unsigned.
454 const vector<bool>& row = deletedPositions[attr];
455 for (vector<bool>::size_type index = row.size(); index > 0; --index) {
457 attr->removeValue(index-1);
461 // Check for no values.
462 if (attr->valueCount() == 0) {
464 "no values left, removing attribute (%s) from (%s)",
465 attr->getId(), issuer.get() ? issuer.get() : "unknown source"
468 deletedAttributes.erase(deletedAttributes.begin() + a);
469 attributes.erase(attributes.begin() + a);
477 pair<bool,DOMElement*> XMLFilter::background_load()
479 // Load from source using base class.
480 pair<bool,DOMElement*> raw = ReloadableXMLFile::load();
482 // If we own it, wrap it.
483 XercesJanitor<DOMDocument> docjanitor(raw.first ? raw.second->getOwnerDocument() : nullptr);
485 scoped_ptr<XMLFilterImpl> impl(new XMLFilterImpl(raw.second, m_log));
487 // If we held the document, transfer it to the impl. If we didn't, it's a no-op.
488 impl->setDocument(docjanitor.release());
490 // Perform the swap inside a lock.
493 SharedLock locker(m_lock, false);
496 return make_pair(false,(DOMElement*)nullptr);