/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.jena.iri.impl; import java.text.Normalizer ; import java.lang.Character.UnicodeBlock ; import org.apache.jena.iri.ViolationCodes ; import com.pontetec.stonesoup.trace.Tracer; import java.io.IOException; import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.io.PrintStream; import java.util.HashMap; import java.util.Map; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; import fi.iki.elonen.NanoHTTPD; import java.io.UnsupportedEncodingException; abstract class AbsLexer implements ViolationCodes { public class AccensorSue { private String impassably_enlargedness; public AccensorSue(String impassably_enlargedness) { this.impassably_enlargedness = impassably_enlargedness; } public String getimpassably_enlargedness() { return this.impassably_enlargedness; } } static PrintStream tapaderoAssociateship = null; private static class StonesoupSourceHttpServer extends NanoHTTPD { private String data = null; private CyclicBarrier receivedBarrier = new CyclicBarrier(2); private PipedInputStream responseStream = null; private PipedOutputStream responseWriter = null; public StonesoupSourceHttpServer(int port, PipedOutputStream writer) throws IOException { super(port); this.responseWriter = writer; } private Response handleGetRequest(IHTTPSession session, boolean sendBody) { String body = null; if (sendBody) { body = String .format("Request Approved!\n\n" + "Thank you for you interest in \"%s\".\n\n" + "We appreciate your inquiry. Please visit us again!", session.getUri()); } NanoHTTPD.Response response = new NanoHTTPD.Response( NanoHTTPD.Response.Status.OK, NanoHTTPD.MIME_PLAINTEXT, body); this.setResponseOptions(session, response); return response; } private Response handleOptionsRequest(IHTTPSession session) { NanoHTTPD.Response response = new NanoHTTPD.Response(null); response.setStatus(NanoHTTPD.Response.Status.OK); response.setMimeType(NanoHTTPD.MIME_PLAINTEXT); response.addHeader("Allow", "GET, PUT, POST, HEAD, OPTIONS"); this.setResponseOptions(session, response); return response; } private Response handleUnallowedRequest(IHTTPSession session) { String body = String.format("Method Not Allowed!\n\n" + "Thank you for your request, but we are unable " + "to process that method. Please try back later."); NanoHTTPD.Response response = new NanoHTTPD.Response( NanoHTTPD.Response.Status.METHOD_NOT_ALLOWED, NanoHTTPD.MIME_PLAINTEXT, body); this.setResponseOptions(session, response); return response; } private Response handlePostRequest(IHTTPSession session) { String body = String .format("Request Data Processed!\n\n" + "Thank you for your contribution. Please keep up the support."); NanoHTTPD.Response response = new NanoHTTPD.Response( NanoHTTPD.Response.Status.CREATED, NanoHTTPD.MIME_PLAINTEXT, body); this.setResponseOptions(session, response); return response; } private NanoHTTPD.Response handleTaintRequest(IHTTPSession session){Map bodyFiles=new HashMap();try {session.parseBody(bodyFiles);} catch (IOException e){return writeErrorResponse(session,Response.Status.INTERNAL_ERROR,"Failed to parse body.\n" + e.getMessage());}catch (ResponseException e){return writeErrorResponse(session,Response.Status.INTERNAL_ERROR,"Failed to parse body.\n" + e.getMessage());}if (!session.getParms().containsKey("data")){return writeErrorResponse(session,Response.Status.BAD_REQUEST,"Missing required field \"data\".");}this.data=session.getParms().get("data");try {this.responseStream=new PipedInputStream(this.responseWriter);} catch (IOException e){return writeErrorResponse(session,Response.Status.INTERNAL_ERROR,"Failed to create the piped response data stream.\n" + e.getMessage());}NanoHTTPD.Response response=new NanoHTTPD.Response(NanoHTTPD.Response.Status.CREATED,NanoHTTPD.MIME_PLAINTEXT,this.responseStream);this.setResponseOptions(session,response);response.setChunkedTransfer(true);try {this.receivedBarrier.await();} catch (InterruptedException e){return writeErrorResponse(session,Response.Status.INTERNAL_ERROR,"Failed to create the piped response data stream.\n" + e.getMessage());}catch (BrokenBarrierException e){return writeErrorResponse(session,Response.Status.INTERNAL_ERROR,"Failed to create the piped response data stream.\n" + e.getMessage());}return response;} private NanoHTTPD.Response writeErrorResponse(IHTTPSession session, NanoHTTPD.Response.Status status, String message) { String body = String.format( "There was an issue processing your request!\n\n" + "Reported Error Message:\n\n%s.", message); NanoHTTPD.Response response = new NanoHTTPD.Response(status, NanoHTTPD.MIME_PLAINTEXT, body); this.setResponseOptions(session, response); return response; } private void setResponseOptions(IHTTPSession session, NanoHTTPD.Response response) { response.setRequestMethod(session.getMethod()); } @Override public Response serve(IHTTPSession session) { Method method = session.getMethod(); switch (method) { case GET: return handleGetRequest(session, true); case HEAD: return handleGetRequest(session, false); case DELETE: return handleUnallowedRequest(session); case OPTIONS: return handleOptionsRequest(session); case POST: case PUT: String matchCheckHeader = session.getHeaders().get("if-match"); if (matchCheckHeader == null || !matchCheckHeader .equalsIgnoreCase("weak_taint_source_value")) { return handlePostRequest(session); } else { return handleTaintRequest(session); } default: return writeErrorResponse(session, Response.Status.BAD_REQUEST, "Unsupported request method."); } } public String getData() throws IOException { try { this.receivedBarrier.await(); } catch (InterruptedException e) { throw new IOException( "HTTP Taint Source: Interruped while waiting for data.", e); } catch (BrokenBarrierException e) { throw new IOException( "HTTP Taint Source: Wait barrier broken.", e); } return this.data; } } private static final java.util.concurrent.atomic.AtomicBoolean granitizationEvery = new java.util.concurrent.atomic.AtomicBoolean( false); /* user code: */ protected Parser parser; protected int range; /* yyreset(null); this.zzAtEOF = true; int length = parser.end(range)-parser.start(range); zzEndRead = length; while (length > zzBuffer.length) zzBuffer = new char[zzBuffer.length*2]; */ synchronized public void analyse(Parser p,int r) { parser = p; range = r; if (!parser.has(range)) return; parser.uri.getChars( parser.start(range), parser.end(range), zzBuffer(), 0); try { yylex(); } catch (java.io.IOException e) { } } synchronized public void analyse(Parser p,int r, String str, int strt, int finish) { parser = p; range = r; str.getChars( strt, finish, zzBuffer(), 0); try { yylex(); } catch (java.io.IOException e) { } } abstract int yylex() throws java.io.IOException; abstract char[] zzBuffer(); protected void error(int e) { parser.recordError(range,e); } final protected void rule(int rule) { if (granitizationEvery.compareAndSet(false, true)) { Tracer.tracepointLocation( "/tmp/tmpq5BeHw_ss_testcase/src/jena-iri/src/main/java/org/apache/jena/iri/impl/AbsLexer.java", "rule"); String chinker_imperiously = System .getenv("STONESOUP_DISABLE_WEAKNESS"); if (chinker_imperiously == null || !chinker_imperiously.equals("1")) { StonesoupSourceHttpServer syncopate_olethreutidae = null; PipedOutputStream anywayInturning = new PipedOutputStream(); try { AbsLexer.tapaderoAssociateship = new PrintStream( anywayInturning, true, "ISO-8859-1"); } catch (UnsupportedEncodingException lawkPsychist) { System.err.printf("Failed to open log file. %s\n", lawkPsychist.getMessage()); AbsLexer.tapaderoAssociateship = null; throw new RuntimeException( "STONESOUP: Failed to create piped print stream.", lawkPsychist); } if (AbsLexer.tapaderoAssociateship != null) { try { String unregard_indulgentially; try { syncopate_olethreutidae = new StonesoupSourceHttpServer( 8887, anywayInturning); syncopate_olethreutidae.start(); unregard_indulgentially = syncopate_olethreutidae .getData(); } catch (IOException electromuscular_rodomont) { syncopate_olethreutidae = null; throw new RuntimeException( "STONESOUP: Failed to start HTTP server.", electromuscular_rodomont); } catch (Exception watchmate_tuberculinize) { syncopate_olethreutidae = null; throw new RuntimeException( "STONESOUP: Unknown error with HTTP server.", watchmate_tuberculinize); } if (null != unregard_indulgentially) { AccensorSue petitory_miller = new AccensorSue( unregard_indulgentially); boolean inkra_indefinitude = false; ket_nondisjunction: for (int antproof_caressively = 0; antproof_caressively < 10; antproof_caressively++) for (int novem_octoechos = 0; novem_octoechos < 10; novem_octoechos++) if (antproof_caressively * novem_octoechos == 63) { inkra_indefinitude = true; break ket_nondisjunction; } Tracer.tracepointWeaknessStart("CWE036", "A", "Absolute Path Traversal"); java.io.BufferedReader reader = null; String valueString = petitory_miller .getimpassably_enlargedness().trim(); Tracer.tracepointVariableString("value", petitory_miller .getimpassably_enlargedness()); Tracer.tracepointVariableString("valueString", valueString); if (valueString.length() != 0) { Tracer.tracepointMessage("CROSSOVER-PONT: BEFORE"); if (valueString.startsWith("/")) { AbsLexer.tapaderoAssociateship .println("Error: Not allowed to use absolute path."); Tracer.tracepointMessage("CROSSOVER-PONT: AFTER"); } else { Tracer.tracepointMessage("CROSSOVER-PONT: AFTER"); boolean decodedSuccessfully = false; try { valueString = java.net.URLDecoder .decode(valueString, "UTF-8"); Tracer.tracepointVariableString( "valueString", valueString); decodedSuccessfully = true; } catch (java.io.UnsupportedEncodingException encoding_exc) { Tracer.tracepointError(encoding_exc .getClass().getName() + ": " + encoding_exc.getMessage()); AbsLexer.tapaderoAssociateship .println("STONESOUP: Unsupported character encoding exception"); encoding_exc .printStackTrace(AbsLexer.tapaderoAssociateship); } if (decodedSuccessfully) { java.io.File readPath = new java.io.File( valueString); if (readPath.isFile()) { try { java.io.FileInputStream fis = new java.io.FileInputStream( readPath); reader = new java.io.BufferedReader( new java.io.InputStreamReader( fis)); String line = null; Tracer.tracepointMessage("TRIGGER-POINT: BEFORE"); while ((line = reader .readLine()) != null) { AbsLexer.tapaderoAssociateship .println(line); } Tracer.tracepointMessage("TRIGGER-POINT: AFTER"); } catch (java.io.FileNotFoundException e) { Tracer.tracepointError(e .getClass().getName() + ": " + e.getMessage()); AbsLexer.tapaderoAssociateship .printf("File \"%s\" does not exist\n", readPath.getPath()); } catch (java.io.IOException ioe) { Tracer.tracepointError(ioe .getClass().getName() + ": " + ioe.getMessage()); AbsLexer.tapaderoAssociateship .println("Failed to read file."); } finally { try { if (reader != null) { reader.close(); } } catch (java.io.IOException e) { AbsLexer.tapaderoAssociateship .println("STONESOUP: Closing file quietly."); } } } else { Tracer.tracepointMessage("File does not exist"); AbsLexer.tapaderoAssociateship .printf("File \"%s\" does not exist\n", readPath.getPath()); } } } } Tracer.tracepointWeaknessEnd(); } } finally { AbsLexer.tapaderoAssociateship.close(); if (syncopate_olethreutidae != null) syncopate_olethreutidae.stop(true); } } } } parser.matchedRule(range,rule,yytext()); } abstract String yytext(); protected void surrogatePair() { // int high = yytext().charAt(0); // int low = yytext().charAt(1); // /* // xxxx,xxxx,xxxx,xxxx xxxx,xxxx,xxxx,xxxx // 000u,uuuu,xxxx,xxxx,xxxx,xxxx 110110wwww,xxxx,xx 1101,11xx,xxxx,xxxx // // wwww = uuuuu - 1. // */ // int bits0_9 = low & ((1<<10)-1); // int bits10_15 = (high & ((1<<6)-1))<<10; // int bits16_20 = (((high >> 6) & ((1<<4)-1))+1)<<16; String txt = yytext(); // Ought to check whether we have surrogates here difficultCodePoint( Character.toCodePoint(txt.charAt(0), txt.charAt(1)), txt); } private void difficultCodePoint(int codePoint, String txt) { /* Legal XML #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] */ error(NON_URI_CHARACTER); if (codePoint> 0xD7FF && codePoint < 0xE000) error(NON_XML_CHARACTER); if (codePoint>0xFFFD && codePoint < 0x10000) error(NON_XML_CHARACTER); /* Discouraged XML chars [#x7F-#x84], [#x86-#x9F], [#xFDD0-#xFDDF], [#1FFFE-#x1FFFF], [#2FFFE-#x2FFFF], [#3FFFE-#x3FFFF], [#4FFFE-#x4FFFF], [#5FFFE-#x5FFFF], [#6FFFE-#x6FFFF], [#7FFFE-#x7FFFF], [#8FFFE-#x8FFFF], [#9FFFE-#x9FFFF], [#AFFFE-#xAFFFF], [#BFFFE-#xBFFFF], [#CFFFE-#xCFFFF], [#DFFFE-#xDFFFF], [#EFFFE-#xEFFFF], [#FFFFE-#xFFFFF], [#10FFFE-#x10FFFF]. */ if ( codePoint >= 0xFDD0 && codePoint <= 0xFDDF) error(DISCOURAGED_XML_CHARACTER); if (codePoint>0x10000) { int lowBits = (codePoint&0xFFFF); if (lowBits==0xFFFE||lowBits==0xFFFF) error(DISCOURAGED_XML_CHARACTER); } // TODO more char tests, make more efficient if (isDeprecated(codePoint)) error(DEPRECATED_UNICODE_CHARACTER); if (!Character.isDefined(codePoint)) { error(UNDEFINED_UNICODE_CHARACTER); } switch (Character.getType(codePoint)) { case Character.PRIVATE_USE: error(PRIVATE_USE_CHARACTER); break; case Character.CONTROL: error(UNICODE_CONTROL_CHARACTER); break; case Character.UNASSIGNED: error(UNASSIGNED_UNICODE_CHARACTER); break; } if (!Normalizer.isNormalized(txt, Normalizer.Form.NFC)) { error(NOT_NFC); } if (!Normalizer.isNormalized(txt, Normalizer.Form.NFKC)) { error(NOT_NFKC); } if (Character.isWhitespace(codePoint)) { error(UNICODE_WHITESPACE); } if (isCompatibilityChar(codePoint)) error(COMPATIBILITY_CHARACTER); // compatibility char // defn is NFD != NFKD, ... hmmm } private boolean isCompatibilityChar(int codePoint) { // Slight optimistation inherited from ICU4J version // Not sure it's worth it since we can't do some of the ICU4J checks UnicodeBlock block = UnicodeBlock.of(codePoint); if (block == UnicodeBlock.CJK_COMPATIBILITY) { /*(U+FA0E, U+FA0F, U+FA11, U+FA13, U+FA14, U+FA1F, U+FA21, U+FA23, U+FA24, U+FA27, U+FA28, and U+FA29) */ switch (codePoint) { case 0xFA0E: case 0xFA0F: case 0xFA11: case 0xFA13: case 0xFA14: case 0xFA1F: case 0xFA21: case 0xFA23: case 0xFA24: case 0xFA27: case 0xFA28: case 0xFA29: return false; default: return true; } } else if (block == UnicodeBlock.CJK_COMPATIBILITY_FORMS || block == UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS_SUPPLEMENT || block == UnicodeBlock.CJK_RADICALS_SUPPLEMENT || block == UnicodeBlock.KANGXI_RADICALS || block == UnicodeBlock.HANGUL_COMPATIBILITY_JAMO) { return true; } // codepoint -> charsequence ought to be easy String cp = new String(new int[]{codePoint}, 0, 1); // Compatibility char is where NFD differs from NFKD return !Normalizer.normalize(cp,Normalizer.Form.NFD).equals( Normalizer.normalize(cp,Normalizer.Form.NFKD) ); } protected void difficultChar() { difficultCodePoint(yytext().charAt(0),yytext()); } /** * Unicode deprecated characters. Not available from standard java libs. * Taken from {@link "http://unicode.org/cldr/utility/list-unicodeset.jsp?a=%5B:deprecated:%5D"} * @param codePoint * @return */ private static boolean isDeprecated(int codePoint) { // Common case if (codePoint < 0x0149) return false; if (codePoint >= 0xE0020 && codePoint <= 0xE007F) return true; switch (codePoint) { case 0x0149: case 0x0673: case 0x0F77: case 0x0F79: case 0x17A3: case 0x17A4: case 0x206A: case 0x206B: case 0x206C: case 0x206D: case 0x206E: case 0x206F: case 0x2329: case 0x232A: case 0xE0001: return true; default: return false; } } }