1 // Copyright 2021 Code Intelligence GmbH 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package com.example.ldap; 16 17 import com.unboundid.ldap.sdk.DN; 18 import com.unboundid.ldap.sdk.Filter; 19 import com.unboundid.ldap.sdk.LDAPException; 20 import java.util.Hashtable; 21 import javax.naming.*; 22 import javax.naming.directory.*; 23 import javax.naming.ldap.*; 24 25 /** 26 * Mock LdapContex implementation to test LdapInjection hook configuration. 27 * 28 * Only {@code com.example.ldap.MockLdapContext#search(java.lang.String, java.lang.String, 29 * javax.naming.directory.SearchControls)} is implemented to validate DN and filer query. 30 */ 31 public class MockLdapContext implements LdapContext { 32 @Override extendedOperation(ExtendedRequest request)33 public ExtendedResponse extendedOperation(ExtendedRequest request) throws NamingException { 34 return null; 35 } 36 37 @Override newInstance(Control[] requestControls)38 public LdapContext newInstance(Control[] requestControls) throws NamingException { 39 return this; 40 } 41 42 @Override reconnect(Control[] connCtls)43 public void reconnect(Control[] connCtls) throws NamingException {} 44 45 @Override getConnectControls()46 public Control[] getConnectControls() throws NamingException { 47 return new Control[0]; 48 } 49 50 @Override setRequestControls(Control[] requestControls)51 public void setRequestControls(Control[] requestControls) throws NamingException {} 52 53 @Override getRequestControls()54 public Control[] getRequestControls() throws NamingException { 55 return new Control[0]; 56 } 57 58 @Override getResponseControls()59 public Control[] getResponseControls() throws NamingException { 60 return new Control[0]; 61 } 62 63 @Override getAttributes(Name name)64 public Attributes getAttributes(Name name) throws NamingException { 65 return null; 66 } 67 68 @Override getAttributes(String name)69 public Attributes getAttributes(String name) throws NamingException { 70 return null; 71 } 72 73 @Override getAttributes(Name name, String[] attrIds)74 public Attributes getAttributes(Name name, String[] attrIds) throws NamingException { 75 return null; 76 } 77 78 @Override getAttributes(String name, String[] attrIds)79 public Attributes getAttributes(String name, String[] attrIds) throws NamingException { 80 return null; 81 } 82 83 @Override modifyAttributes(Name name, int mod_op, Attributes attrs)84 public void modifyAttributes(Name name, int mod_op, Attributes attrs) throws NamingException {} 85 86 @Override modifyAttributes(String name, int mod_op, Attributes attrs)87 public void modifyAttributes(String name, int mod_op, Attributes attrs) throws NamingException {} 88 89 @Override modifyAttributes(Name name, ModificationItem[] mods)90 public void modifyAttributes(Name name, ModificationItem[] mods) throws NamingException {} 91 92 @Override modifyAttributes(String name, ModificationItem[] mods)93 public void modifyAttributes(String name, ModificationItem[] mods) throws NamingException {} 94 95 @Override bind(Name name, Object obj, Attributes attrs)96 public void bind(Name name, Object obj, Attributes attrs) throws NamingException {} 97 98 @Override bind(String name, Object obj, Attributes attrs)99 public void bind(String name, Object obj, Attributes attrs) throws NamingException {} 100 101 @Override rebind(Name name, Object obj, Attributes attrs)102 public void rebind(Name name, Object obj, Attributes attrs) throws NamingException {} 103 104 @Override rebind(String name, Object obj, Attributes attrs)105 public void rebind(String name, Object obj, Attributes attrs) throws NamingException {} 106 107 @Override createSubcontext(Name name, Attributes attrs)108 public DirContext createSubcontext(Name name, Attributes attrs) throws NamingException { 109 return this; 110 } 111 112 @Override createSubcontext(String name, Attributes attrs)113 public DirContext createSubcontext(String name, Attributes attrs) throws NamingException { 114 return this; 115 } 116 117 @Override getSchema(Name name)118 public DirContext getSchema(Name name) throws NamingException { 119 return this; 120 } 121 122 @Override getSchema(String name)123 public DirContext getSchema(String name) throws NamingException { 124 return this; 125 } 126 127 @Override getSchemaClassDefinition(Name name)128 public DirContext getSchemaClassDefinition(Name name) throws NamingException { 129 return this; 130 } 131 132 @Override getSchemaClassDefinition(String name)133 public DirContext getSchemaClassDefinition(String name) throws NamingException { 134 return this; 135 } 136 137 @Override search(Name name, Attributes matchingAttributes, String[] attributesToReturn)138 public NamingEnumeration<SearchResult> search(Name name, Attributes matchingAttributes, 139 String[] attributesToReturn) throws NamingException { 140 return null; 141 } 142 143 @Override search(String name, Attributes matchingAttributes, String[] attributesToReturn)144 public NamingEnumeration<SearchResult> search(String name, Attributes matchingAttributes, 145 String[] attributesToReturn) throws NamingException { 146 return null; 147 } 148 149 @Override search(Name name, Attributes matchingAttributes)150 public NamingEnumeration<SearchResult> search(Name name, Attributes matchingAttributes) 151 throws NamingException { 152 return null; 153 } 154 155 @Override search(String name, Attributes matchingAttributes)156 public NamingEnumeration<SearchResult> search(String name, Attributes matchingAttributes) 157 throws NamingException { 158 return null; 159 } 160 161 @Override search(Name name, String filter, SearchControls cons)162 public NamingEnumeration<SearchResult> search(Name name, String filter, SearchControls cons) 163 throws NamingException { 164 return null; 165 } 166 167 @Override search(String name, String filter, SearchControls cons)168 public NamingEnumeration<SearchResult> search(String name, String filter, SearchControls cons) 169 throws NamingException { 170 // Use UnboundID LDAP to validate DN and filter 171 if (!DN.isValidDN(name)) { 172 throw new NamingException("Invalid DN " + name); 173 } 174 try { 175 Filter.create(filter); 176 } catch (LDAPException e) { 177 throw new InvalidSearchFilterException("Invalid search filter " + filter); 178 } 179 return null; 180 } 181 182 @Override search(Name name, String filterExpr, Object[] filterArgs, SearchControls cons)183 public NamingEnumeration<SearchResult> search(Name name, String filterExpr, Object[] filterArgs, 184 SearchControls cons) throws NamingException { 185 return null; 186 } 187 188 @Override search(String name, String filterExpr, Object[] filterArgs, SearchControls cons)189 public NamingEnumeration<SearchResult> search(String name, String filterExpr, Object[] filterArgs, 190 SearchControls cons) throws NamingException { 191 return null; 192 } 193 194 @Override lookup(Name name)195 public Object lookup(Name name) throws NamingException { 196 return this; 197 } 198 199 @Override lookup(String name)200 public Object lookup(String name) throws NamingException { 201 return this; 202 } 203 204 @Override bind(Name name, Object obj)205 public void bind(Name name, Object obj) throws NamingException {} 206 207 @Override bind(String name, Object obj)208 public void bind(String name, Object obj) throws NamingException {} 209 210 @Override rebind(Name name, Object obj)211 public void rebind(Name name, Object obj) throws NamingException {} 212 213 @Override rebind(String name, Object obj)214 public void rebind(String name, Object obj) throws NamingException {} 215 216 @Override unbind(Name name)217 public void unbind(Name name) throws NamingException {} 218 219 @Override unbind(String name)220 public void unbind(String name) throws NamingException {} 221 222 @Override rename(Name oldName, Name newName)223 public void rename(Name oldName, Name newName) throws NamingException {} 224 225 @Override rename(String oldName, String newName)226 public void rename(String oldName, String newName) throws NamingException {} 227 228 @Override list(Name name)229 public NamingEnumeration<NameClassPair> list(Name name) throws NamingException { 230 return null; 231 } 232 233 @Override list(String name)234 public NamingEnumeration<NameClassPair> list(String name) throws NamingException { 235 return null; 236 } 237 238 @Override listBindings(Name name)239 public NamingEnumeration<Binding> listBindings(Name name) throws NamingException { 240 return null; 241 } 242 243 @Override listBindings(String name)244 public NamingEnumeration<Binding> listBindings(String name) throws NamingException { 245 return null; 246 } 247 248 @Override destroySubcontext(Name name)249 public void destroySubcontext(Name name) throws NamingException {} 250 251 @Override destroySubcontext(String name)252 public void destroySubcontext(String name) throws NamingException {} 253 254 @Override createSubcontext(Name name)255 public Context createSubcontext(Name name) throws NamingException { 256 return this; 257 } 258 259 @Override createSubcontext(String name)260 public Context createSubcontext(String name) throws NamingException { 261 return this; 262 } 263 264 @Override lookupLink(Name name)265 public Object lookupLink(Name name) throws NamingException { 266 return this; 267 } 268 269 @Override lookupLink(String name)270 public Object lookupLink(String name) throws NamingException { 271 return this; 272 } 273 274 @Override getNameParser(Name name)275 public NameParser getNameParser(Name name) throws NamingException { 276 return null; 277 } 278 279 @Override getNameParser(String name)280 public NameParser getNameParser(String name) throws NamingException { 281 return null; 282 } 283 284 @Override composeName(Name name, Name prefix)285 public Name composeName(Name name, Name prefix) throws NamingException { 286 return null; 287 } 288 289 @Override composeName(String name, String prefix)290 public String composeName(String name, String prefix) throws NamingException { 291 return null; 292 } 293 294 @Override addToEnvironment(String propName, Object propVal)295 public Object addToEnvironment(String propName, Object propVal) throws NamingException { 296 return null; 297 } 298 299 @Override removeFromEnvironment(String propName)300 public Object removeFromEnvironment(String propName) throws NamingException { 301 return null; 302 } 303 304 @Override getEnvironment()305 public Hashtable<?, ?> getEnvironment() throws NamingException { 306 return null; 307 } 308 309 @Override close()310 public void close() throws NamingException {} 311 312 @Override getNameInNamespace()313 public String getNameInNamespace() throws NamingException { 314 return null; 315 } 316 } 317