界: API Abuse

API は、呼び出し元と呼び出し先の間のコントラクトです。最も一般的な API の不正使用の形態は、呼び出し元がこのコントラクトの終わりを守らないことによって発生します。たとえば、プログラムが chroot() を呼び出した後に chdir() を呼び出すのに失敗すると、アクティブなルート ディレクトリを安全に変更する方法を指定したコントラクトに違反することになります。ライブラリの悪用のもう 1 つの良い例は、呼び出し先が信頼できる DNS 情報を呼び出し元に返すことを期待することです。この場合、呼び出し元は、呼び出し先の API の動作 (戻り値が認証目的に使用できること) についてある種の仮定をすることで、呼び出し先の API を悪用します。また、相手側から、呼び出し元と呼び出し先のコントラクトを違反することもできます。例えば、コーダーが SecureRandom をサブクラス化し、ランダムではない値を返した場合、コントラクトに違反することになります。

Code Correctness: Multiple Stream Commits

Abstract
サーブレットの出力ストリームがすでにコミットされた後には、ストリームバッファをリセットしたり、ストリームを再コミットする他のアクションを実行するとエラーが発生しやすくなります。同様に、getOutputStream をコールした後に、getWriter() をコールする場合もエラーが発生しやすくなります。
Explanation
HttpServletRequest の転送、HttpServletResponse のリダイレクト、またはサーブレットの出力ストリームバッファーのフラッシュによって、関連ストリームがコミットされます。後続のバッファーがストリームをリセットまたはコミットすると (追加のフラッシュまたはリダイレクトなど)、IllegalStateException となります。

さらに、Java サーブレットによって、ServletOutputStream または PrintWriter のいずれか (両方ではありません) を使用して、レスポンスストリームへのデータの書き込みが許可されます。getOutputStream() をコールした後に、getWriter() をコールする、また逆の処理を行った場合、IllegalStateException となります。



実行時には、IllegalStateException によって、レスポンスハンドラの実行が防止され、レスポンスがドロップすることになります。これによりサーバーの動作が不安定になります。また、サーブレットが適切に実装されていないことを示します。

例 1: 次のコードは、出力ストリームバッファーがフラッシュされた後に、サーブレットのレスポンスをリダイレクトしています。

public class RedirectServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
...
OutputStream out = res.getOutputStream();
...
// flushes, and thereby commits, the output stream
out.flush();
out.close(); // redirecting the response causes an IllegalStateException
res.sendRedirect("http://www.acme.com");
}
}
例 2: 逆に、次のコードは、リクエストが転送された後に、書き込みを行い、PrintWriter のバッファーをフラッシュしようとしています。

public class FlushServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
...
// forwards the request, implicitly committing the stream
getServletConfig().getServletContext().getRequestDispatcher("/jsp/boom.jsp").forward(req, res);
...

// IllegalStateException; cannot redirect after forwarding
res.sendRedirect("http://www.acme.com/jsp/boomboom.jsp");

PrintWriter out = res.getWriter();

// writing to an already-committed stream will not cause an exception,
// but will not apply these changes to the final output, either
out.print("Writing here does nothing");

// IllegalStateException; cannot flush a response's buffer after forwarding the request
out.flush();
out.close();
}
}
References
[1] IllegalStateException in a Servlet - when & why do we get?
[2] Standards Mapping - Common Weakness Enumeration CWE ID 398
[3] Standards Mapping - DISA Control Correlation Identifier Version 2 CCI-001094
[4] Standards Mapping - NIST Special Publication 800-53 Revision 4 SC-5 Denial of Service Protection (P1)
[5] Standards Mapping - NIST Special Publication 800-53 Revision 5 SC-5 Denial of Service Protection
[6] Standards Mapping - Security Technical Implementation Guide Version 4.2 APSC-DV-002400 CAT II
[7] Standards Mapping - Security Technical Implementation Guide Version 4.3 APSC-DV-002400 CAT II
[8] Standards Mapping - Security Technical Implementation Guide Version 4.4 APSC-DV-002400 CAT II
[9] Standards Mapping - Security Technical Implementation Guide Version 4.5 APSC-DV-002400 CAT II
[10] Standards Mapping - Security Technical Implementation Guide Version 4.6 APSC-DV-002400 CAT II
[11] Standards Mapping - Security Technical Implementation Guide Version 4.7 APSC-DV-002400 CAT II
[12] Standards Mapping - Security Technical Implementation Guide Version 4.8 APSC-DV-002400 CAT II
[13] Standards Mapping - Security Technical Implementation Guide Version 4.9 APSC-DV-002400 CAT II
[14] Standards Mapping - Security Technical Implementation Guide Version 4.10 APSC-DV-002400 CAT II
[15] Standards Mapping - Security Technical Implementation Guide Version 4.11 APSC-DV-002400 CAT II
[16] Standards Mapping - Security Technical Implementation Guide Version 4.1 APSC-DV-002400 CAT II
[17] Standards Mapping - Security Technical Implementation Guide Version 5.1 APSC-DV-002400 CAT II
[18] Standards Mapping - Security Technical Implementation Guide Version 5.2 APSC-DV-002400 CAT II
[19] Standards Mapping - Security Technical Implementation Guide Version 5.3 APSC-DV-002400 CAT II
desc.controlflow.java.code_correctness_multiple_stream_commits