ESAPIがリソースファイルを取得することについて



About Esapi Getting Resource Files



最近のプロジェクトでは、コンポーネントパッケージESAPI(ESAPIはowaspによって提供されるAPIレベルのWebアプリケーションソリューションのセットです)が必要です。公式サイトは次のとおりです。 https://www.owasp.org/、 興味のある友達が見つけることができます。これはこの記事の焦点では​​ありません。
この記事では、このコンポーネントを使用するときに発生するリソースの読み込みの問題に焦点を当てています。

jarの紹介



org.owasp.esapi esapi 2.1.0.1

このAPIを使用する場合、ESAPI.propertiesの3つのリソースファイルを導入する必要があります。 、validation.propertiesantisamy-esapi.xml

とても簡単で、プロジェクトのリソースディレクトリのesapiに直接入ると、単体テストが実行されます



@Test public void esapiTest() { String input = '

test this alert(document.cookie) right now

'
try { String safe2 = ESAPI.validator().getValidSafeHTML('get safe html', input, Integer.MAX_VALUE, true) logger.info('getValidSafeHTML:{}', safe2) } catch (ValidationException e) { logger.error('error', e) } }

運転結果

System property [org.owasp.esapi.opsteam] is not set Attempting to load ESAPI.properties via file I/O. Attempting to load ESAPI.properties as resource file via file I/O. Not found in 'org.owasp.esapi.resources' directory or file not readable: E:intelliGitTiffanyXSSESAPI.properties Found in SystemResource Directory/resourceDirectory: E:intelliGitTiffanyXSS argetclassesesapiESAPI.properties Loaded 'ESAPI.properties' properties file SecurityConfiguration for Validator.ConfigurationFile.MultiValued not found in ESAPI.properties. Using default: false Attempting to load validation.properties via file I/O. Attempting to load validation.properties as resource file via file I/O. Not found in 'org.owasp.esapi.resources' directory or file not readable: E:intelliGitTiffanyXSSvalidation.properties Found in SystemResource Directory/resourceDirectory: E:intelliGitTiffanyXSS argetclassesesapivalidation.properties Loaded 'validation.properties' properties file System property [org.owasp.esapi.devteam] is not set Attempting to load antisamy-esapi.xml as resource file via file I/O. Not found in 'org.owasp.esapi.resources' directory or file not readable: E:intelliGitTiffanyXSSantisamy-esapi.xml Found in SystemResource Directory/resourceDirectory: E:intelliGitTiffanyXSS argetclassesesapiantisamy-esapi.xml November 08, 2018 10:39:59 AM org.owasp.esapi.reference.JavaLogFactory$JavaLogger log 2018-11-08 10:39:59 [main] INFO com.tiffany.xss.EsapiTest - getValidSafeHTML:

test this right now



了解しました。ウェブプロジェクトの紹介、桟橋の打ち上げ、結果へのアクセス

Not found in 'org.owasp.esapi.resources' directory or file not readable: Not found in SystemResource Directory/resourceDirectory: .esapiESAPI.properties Not found in 'user.home' (C:Userswangxinguo) directory: C:UserswangxinguoesapiESAPI.properties Not found in 'org.owasp.esapi.resources' directory or file not readable: Not found in SystemResource Directory/resourceDirectory: .esapivalidation.properties Not found in 'user.home' (C:Userswangxinguo) directory: C:Userswangxinguoesapivalidation.properties Not found in 'org.owasp.esapi.resources' directory or file not readable: Not found in SystemResource Directory/resourceDirectory: .esapiantisamy-esapi.xml Not found in 'user.home' (C:Userswangxinguo) directory: C:Userswangxinguoesapiantisamy-esapi.xml Caused by: org.owasp.esapi.errors.ConfigurationException: Couldn't find antisamy-esapi.xml at org.owasp.esapi.reference.validation.HTMLValidationRule.(HTMLValidationRule.java:55) ... 33 more Caused by: java.io.FileNotFoundException at org.owasp.esapi.reference.DefaultSecurityConfiguration.getResourceStream(DefaultSecurityConfiguration.java:532) at org.owasp.esapi.reference.validation.HTMLValidationRule.(HTMLValidationRule.java:53) ... 33 more

なんてこったい?リソースファイルが見つかりませんか? ?すでにクラスパスにありますか?

  • 梱包しませんでしたか?

    パッケージwarファイルを直接表示します。
    /webapp/WEB-INF/classes/esapi 3つのドキュメントが存在し、単体テストに合格できますが、これは冗談ではありませんか?

  • 突堤コンテナの減圧は有毒ですか? (合理的な疑い)

    サーバーの桟橋作業ディレクトリに直接/webapp/WEB-INF/classes/esapi 3つのドキュメントが存在します。

まさか、例外ログを見てください

Not found in 'org.owasp.esapi.resources' directory or file not readable

リソースファイルへのパスは、jvm起動環境変数を指定することで指定できます。

-Dorg.owasp.esapi.resources={path}

再起動し、ファイルの解像度を見つけます

しかし、これは私が望む結果ではありません。プログラムがクラスパスの直下にリソースファイルをロードすることを望んでいます。
このように、コンテナの起動時にリソースパスを強制的に宣言する必要はありません。

次に、ソースコードを直接見て、waveを直接デバッグします
org.owasp.esapi.reference.DefaultSecurityConfiguration in getResourceFileメソッド

public File getResourceFile(String filename) { logSpecial('Attempting to load ' + filename + ' as resource file via file I/O.') if (filename == null) { logSpecial('Failed to load properties via FileIO. Filename is null.') return null // not found. } File f = null // first, allow command line overrides. -Dorg.owasp.esapi.resources // directory f = new File(customDirectory, filename) if (customDirectory != null && f.canRead()) { logSpecial('Found in 'org.owasp.esapi.resources' directory: ' + f.getAbsolutePath()) return f } else { logSpecial('Not found in 'org.owasp.esapi.resources' directory or file not readable: ' + f.getAbsolutePath()) } // if not found, then try the programmatically set resource directory // (this defaults to SystemResource directory/resourceFile URL fileUrl = ClassLoader.getSystemResource(resourceDirectory + '/' + filename) if ( fileUrl == null ) { fileUrl = ClassLoader.getSystemResource('esapi/' + filename) } if (fileUrl != null) { String fileLocation = fileUrl.getFile() f = new File(fileLocation) if (f.exists()) { logSpecial('Found in SystemResource Directory/resourceDirectory: ' + f.getAbsolutePath()) return f } else { logSpecial('Not found in SystemResource Directory/resourceDirectory (this should never happen): ' + f.getAbsolutePath()) } } else { logSpecial('Not found in SystemResource Directory/resourceDirectory: ' + resourceDirectory + File.separator + filename) } // If not found, then try immediately under user's home directory first in // userHome + '/.esapi' and secondly under // userHome + '/esapi' // We look in that order because of backward compatibility issues. String homeDir = userHome if ( homeDir == null ) { homeDir = '' // Without this, homeDir + '/.esapi' would produce // the string 'null/.esapi' which surely is not intended. } // First look under '.esapi' (for reasons of backward compatibility). f = new File(homeDir + '/.esapi', filename) if ( f.canRead() ) { logSpecial('[Compatibility] Found in 'user.home' directory: ' + f.getAbsolutePath()) return f } else { // Didn't find it under old directory '.esapi' so now look under the 'esapi' directory. f = new File(homeDir + '/esapi', filename) if ( f.canRead() ) { logSpecial('Found in 'user.home' directory: ' + f.getAbsolutePath()) return f } else { logSpecial('Not found in 'user.home' (' + homeDir + ') directory: ' + f.getAbsolutePath()) } } // return null if not found return null }

ソースコードのプロセスも非常に簡単です

  • 最初のパス-Dorg.owasp.esapi.resources取得するパスを指定します(存在する場合は、直接戻ります)

  • 上記のプロセスは取得できません、渡されますClassLoader.getSystemResource('esapi/' + filename)取得するには、存在する場合は直接戻ります

  • クラスローダーも利用できません。userHomeがあるかどうかを確認してください

デバッグ分析

  • ユニットテストの開始

ClassLoader.getSystemClassLoader()

画像

Thread.currentThread().getContextClassLoader()

画像

Jvm ClassLoaderが使用され、リソースファイルが見つかります。

  • Jettyコンテナの起動

ClassLoader.getSystemClassLoader()

画像

画像

Thread.currentThread().getContextClassLoader()

画像

画像

現在のスレッドのクラスローダーは、jvmクラスローダーではなく、桟橋コンテナWebAppClassLoader Getを経由していることがわかりました

問題を見つける方法については、漠然とした想起によるものです JAVAクラスローダー
親のパターンの破壊について言及しました、 TomcatのWebappClassLoaderは、最初に独自のクラスをロードし、次にデリゲートを見つけることができません。

ここで見たときに見ましたが、よくわかりませんでした。私は心にとどまっただけでした。

インターネット上で大きな神を見つけるために、この状況に沿った説明がなされているので、よく読んでコンテナクラスローダーの理解を深め、今後このような不思議な問題を回避します。
Jettyのソースコードの読み取り-JettyのクラスローダーWebAppClassLoader

小さなパートナーの共有も良いので、ここに記録してください。 getSystemResource、getResourceの概要