Search in sources :

Example 86 with DatabaseException

use of org.parosproxy.paros.db.DatabaseException in project zaproxy by zaproxy.

the class ParosTableHistory method deleteTemporary.

/**
     * Deletes all records whose history type was marked as temporary (by calling {@code setHistoryTypeTemporary(int)}).
     * <p>
     * By default the only temporary history types are {@code HistoryReference#TYPE_TEMPORARY} and
     * {@code HistoryReference#TYPE_SCANNER_TEMPORARY}.
     * </p>
     *
     * @throws DatabaseException if an error occurred while deleting the temporary history records
     * @see HistoryReference#getTemporaryTypes()
     */
@Override
public void deleteTemporary() throws DatabaseException {
    try {
        for (Integer type : HistoryReference.getTemporaryTypes()) {
            while (true) {
                psDeleteTemp.setInt(1, type);
                int result = psDeleteTemp.executeUpdate();
                if (result == 0) {
                    break;
                }
            }
        }
    } catch (SQLException e) {
        throw new DatabaseException(e);
    }
}
Also used : SQLException(java.sql.SQLException) DatabaseException(org.parosproxy.paros.db.DatabaseException)

Example 87 with DatabaseException

use of org.parosproxy.paros.db.DatabaseException in project zaproxy by zaproxy.

the class ParosTableHistory method getHistoryList.

@Override
public List<Integer> getHistoryList(long sessionId, int histType, String filter, boolean isRequest) throws DatabaseException {
    try {
        PreparedStatement psReadSearch = getConnection().prepareStatement("SELECT * FROM HISTORY WHERE " + SESSIONID + " = ? AND " + HISTTYPE + " = ? ORDER BY " + HISTORYID);
        ResultSet rs = null;
        Vector<Integer> v = new Vector<>();
        try {
            Pattern pattern = Pattern.compile(filter, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
            Matcher matcher = null;
            psReadSearch.setLong(1, sessionId);
            psReadSearch.setInt(2, histType);
            rs = psReadSearch.executeQuery();
            while (rs.next()) {
                if (isRequest) {
                    matcher = pattern.matcher(rs.getString(REQHEADER));
                    if (matcher.find()) {
                        // ZAP: Changed to use the method Integer.valueOf.
                        v.add(Integer.valueOf(rs.getInt(HISTORYID)));
                        continue;
                    }
                    matcher = pattern.matcher(rs.getString(REQBODY));
                    if (matcher.find()) {
                        // ZAP: Changed to use the method Integer.valueOf.
                        v.add(Integer.valueOf(rs.getInt(HISTORYID)));
                        continue;
                    }
                } else {
                    matcher = pattern.matcher(rs.getString(RESHEADER));
                    if (matcher.find()) {
                        // ZAP: Changed to use the method Integer.valueOf.
                        v.add(Integer.valueOf(rs.getInt(HISTORYID)));
                        continue;
                    }
                    matcher = pattern.matcher(rs.getString(RESBODY));
                    if (matcher.find()) {
                        // ZAP: Changed to use the method Integer.valueOf.
                        v.add(Integer.valueOf(rs.getInt(HISTORYID)));
                        continue;
                    }
                }
            }
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception e) {
                // Ignore
                }
            }
            psReadSearch.close();
        }
        return v;
    } catch (SQLException e) {
        throw new DatabaseException(e);
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Vector(java.util.Vector) DatabaseException(org.parosproxy.paros.db.DatabaseException) HttpMalformedHeaderException(org.parosproxy.paros.network.HttpMalformedHeaderException) SQLException(java.sql.SQLException) DatabaseException(org.parosproxy.paros.db.DatabaseException)

Example 88 with DatabaseException

use of org.parosproxy.paros.db.DatabaseException in project zaproxy by zaproxy.

the class ParosTableHistory method containsURI.

@Override
public synchronized boolean containsURI(long sessionId, int historyType, String method, String uri, byte[] body) throws DatabaseException {
    try {
        psContainsURI.setString(1, uri);
        psContainsURI.setString(2, method);
        if (bodiesAsBytes) {
            psContainsURI.setBytes(3, body);
        } else {
            psContainsURI.setString(3, new String(body));
        }
        psContainsURI.setLong(4, sessionId);
        psContainsURI.setInt(5, historyType);
        try (ResultSet rs = psContainsURI.executeQuery()) {
            if (rs.next()) {
                return true;
            }
        }
        return false;
    } catch (SQLException e) {
        throw new DatabaseException(e);
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) DatabaseException(org.parosproxy.paros.db.DatabaseException)

Example 89 with DatabaseException

use of org.parosproxy.paros.db.DatabaseException in project zaproxy by zaproxy.

the class HostProcess method traverse.

private void traverse(StructuralNode node, boolean incRelatedSiblings, TraverseAction action) {
    if (node == null || isStop()) {
        return;
    }
    Set<StructuralNode> parentNodes = new HashSet<>();
    parentNodes.add(node);
    action.apply(node);
    if (!action.isStopTraversing() && parentScanner.scanChildren()) {
        if (incRelatedSiblings) {
            // Note that this is only done for the top level
            try {
                Iterator<StructuralNode> iter = node.getParent().getChildIterator();
                String nodeName = SessionStructure.getCleanRelativeName(node, false);
                while (iter.hasNext()) {
                    StructuralNode sibling = iter.next();
                    if (!node.isSameAs(sibling) && nodeName.equals(SessionStructure.getCleanRelativeName(sibling, false))) {
                        log.debug("traverse: including related sibling " + sibling.getName());
                        parentNodes.add(sibling);
                    }
                }
            } catch (DatabaseException e) {
            // Ignore - if we cant connect to the db there will be plenty of other errors logged ;)
            }
        }
        for (StructuralNode pNode : parentNodes) {
            Iterator<StructuralNode> iter = pNode.getChildIterator();
            while (iter.hasNext() && !isStop() && !action.isStopTraversing()) {
                StructuralNode child = iter.next();
                // ZAP: Implement pause and resume
                while (parentScanner.isPaused() && !isStop()) {
                    Util.sleep(500);
                }
                try {
                    traverse(child, action);
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
    }
}
Also used : StructuralNode(org.zaproxy.zap.model.StructuralNode) DatabaseException(org.parosproxy.paros.db.DatabaseException) DatabaseException(org.parosproxy.paros.db.DatabaseException) HashSet(java.util.HashSet)

Example 90 with DatabaseException

use of org.parosproxy.paros.db.DatabaseException in project zaproxy by zaproxy.

the class Analyser method isFileExist.

public boolean isFileExist(HttpMessage msg) {
    if (msg.getResponseHeader().isEmpty()) {
        return false;
    }
    // RFC
    if (msg.getResponseHeader().getStatusCode() == HttpStatusCode.NOT_FOUND) {
        return false;
    }
    // ZAP: catch CloneNotSupportedException as introduced with version 3.1 of HttpClient
    URI uri = null;
    String sUri = null;
    try {
        uri = (URI) msg.getRequestHeader().getURI().clone();
        // strip off last part of path - use folder only
        uri.setQuery(null);
        String path = uri.getPath();
        path = path.replaceAll("/[^/]*$", "");
        uri.setPath(path);
    } catch (Exception e) {
    } finally {
        if (uri != null) {
            sUri = uri.toString();
        }
    }
    // get sample with same relative path position when possible.
    // if not exist, use the host only	
    // ZAP: Removed unnecessary cast.
    SampleResponse sample = mapVisited.get(sUri);
    if (sample == null) {
        try {
            uri.setPath(null);
        } catch (URIException e2) {
        }
        String sHostOnly = uri.toString();
        // ZAP: Removed unnecessary cast.
        sample = mapVisited.get(sHostOnly);
    }
    // check if any analysed result.
    if (sample == null) {
        if (msg.getResponseHeader().getStatusCode() == HttpStatusCode.OK) {
            // no anlaysed result to confirm, assume file exist and return
            return true;
        } else {
            return false;
        }
    }
    // check for redirect response.  If redirect to same location, then file does not exist
    if (HttpStatusCode.isRedirection(msg.getResponseHeader().getStatusCode())) {
        try {
            if (sample.getMessage().getResponseHeader().getStatusCode() == msg.getResponseHeader().getStatusCode()) {
                String location = msg.getResponseHeader().getHeader(HttpHeader.LOCATION);
                if (location != null && location.equals(sample.getMessage().getResponseHeader().getHeader(HttpHeader.LOCATION))) {
                    return false;
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return true;
    }
    // Not success code
    if (msg.getResponseHeader().getStatusCode() != HttpStatusCode.OK) {
        return false;
    }
    // remain only OK response here
    // nothing more to determine.  Check for possible not found page pattern.
    Matcher matcher = patternNotFound.matcher(msg.getResponseBody().toString());
    if (matcher.find()) {
        return false;
    }
    // static response
    String body = msg.getResponseBody().toString().replaceAll(p_REMOVE_HEADER, "");
    if (sample.getErrorPageType() == SampleResponse.ERROR_PAGE_STATIC) {
        try {
            if (sample.getMessage().getResponseBody().toString().equals(body)) {
                return false;
            }
        } catch (HttpMalformedHeaderException | DatabaseException e) {
            logger.error("Failed to read the message: " + e.getMessage(), e);
        }
        return true;
    }
    uri = msg.getRequestHeader().getURI();
    try {
        if (sample.getErrorPageType() == SampleResponse.ERROR_PAGE_DYNAMIC_BUT_DETERMINISTIC) {
            body = msg.getResponseBody().toString().replaceAll(getPathRegex(uri), "").replaceAll("\\s[012]\\d:[0-5]\\d:[0-5]\\d\\s", "");
            // ZAP: FindBugs fix - added call to HttpBody.toString() 
            if (sample.getMessage().getResponseBody().toString().equals(body)) {
                return false;
            }
            return true;
        }
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
    return true;
}
Also used : URIException(org.apache.commons.httpclient.URIException) Matcher(java.util.regex.Matcher) HttpMalformedHeaderException(org.parosproxy.paros.network.HttpMalformedHeaderException) URI(org.apache.commons.httpclient.URI) DatabaseException(org.parosproxy.paros.db.DatabaseException) IOException(java.io.IOException) DatabaseException(org.parosproxy.paros.db.DatabaseException) URIException(org.apache.commons.httpclient.URIException) HttpMalformedHeaderException(org.parosproxy.paros.network.HttpMalformedHeaderException) HttpException(org.apache.commons.httpclient.HttpException)

Aggregations

DatabaseException (org.parosproxy.paros.db.DatabaseException)153 SQLException (java.sql.SQLException)113 ResultSet (java.sql.ResultSet)61 ArrayList (java.util.ArrayList)28 HttpMalformedHeaderException (org.parosproxy.paros.network.HttpMalformedHeaderException)19 PreparedStatement (java.sql.PreparedStatement)11 Session (org.parosproxy.paros.model.Session)11 HttpMessage (org.parosproxy.paros.network.HttpMessage)11 RecordHistory (org.parosproxy.paros.db.RecordHistory)9 RecordAlert (org.parosproxy.paros.db.RecordAlert)7 RecordContext (org.parosproxy.paros.db.RecordContext)7 Vector (java.util.Vector)6 URIException (org.apache.commons.httpclient.URIException)6 IOException (java.io.IOException)5 TableHistory (org.parosproxy.paros.db.TableHistory)5 HistoryReference (org.parosproxy.paros.model.HistoryReference)5 StructuralSiteNode (org.zaproxy.zap.model.StructuralSiteNode)5 Matcher (java.util.regex.Matcher)4 PatternSyntaxException (java.util.regex.PatternSyntaxException)4 JSONException (net.sf.json.JSONException)4