Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于Dom4j Hook类方法的优化建议 #396

Open
xl1605368195 opened this issue Aug 30, 2022 · 2 comments
Open

关于Dom4j Hook类方法的优化建议 #396

xl1605368195 opened this issue Aug 30, 2022 · 2 comments

Comments

@xl1605368195
Copy link

问题描述:

image

这里会匹配org.dom4j.io.SAXReader#read包括下面的8个,仅方法参数不一样

  1. public Document read(File file) throws DocumentException
  2. public Document read(URL url) throws DocumentException
  3. public Document read(String systemId) throws DocumentException
  4. public Document read(InputStream in) throws DocumentException
  5. public Document read(Reader reader) throws DocumentException
  6. public Document read(InputStream in, String systemId) throws DocumentException
  7. public Document read(Reader reader, String systemId) throws DocumentException
  8. public Document read(InputSource in) throws DocumentException

查看dom4j源码可知,前面的1~7个方法都会调用第8个方法(下面的红色框),列举其中一个的调用关系如下(其他6个都会调用方法8)
image

反编译hook后的类字节码,1-8方法均被插桩:
image

优化:

由于1-7方法都会调用方法8,并且真正处理xml的逻辑在方法8中,仅hook方法8即可,这样可以减少字节码、避免插桩代码2次调用。
(下图为方法8代码)
image

优化后代码:

@Override
protected void hookMethod(CtClass ctClass) throws IOException, CannotCompileException, NotFoundException {
    String src = getInvokeStaticSrc(DisableDom4jXxeEntity.class, "setFeature", "$0", Object.class);
    // insertBefore(ctClass, "read", null, src);
    // 仅匹配指定方法
    insertBefore(ctClass, "read", "(Lorg/xml/sax/InputSource;)Lorg/dom4j/Document;" , src);
}

源码位置:DisableDom4jXxeEntity.java#L44

如果错误,欢迎指正~~

@levie-vans
Copy link

厉害了!

@CaledoniaProject
Copy link
Collaborator

@xl1605368195 你好,我看了下dom4j 1.X、2.X的代码,你这个修改应该没啥问题,可以提交个补丁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants