Search in sources :

Example 6 with Attribute

use of net.htmlparser.jericho.Attribute in project CFLint by cflint.

the class CFLint method unpackTagExpressions.

private Map<String, CFExpression> unpackTagExpressions(final Element elem) {
    // Use LinkedHashMap to preserve the order
    final Map<String, CFExpression> expressions = new LinkedHashMap<>();
    if (!elem.getName().toLowerCase().startsWith("cf") || elem.getAttributes() == null) {
        return expressions;
    }
    // variable attribute
    for (final Attribute attr : elem.getAttributes()) {
        if (detectScript(attr)) {
            // Try wrapping the expression in single or double quotes for
            // parsing.
            final List<String> literalChar = attr.getValue().contains("'") ? Arrays.asList("\"", "'") : Arrays.asList("'", "\"");
            try {
                final List<String> errors = new ArrayList<>();
                final ANTLRErrorListener errorReporter = new ArrayErrorListener(errors);
                final CFExpression exp = cfmlParser.parseCFMLExpression(literalChar.get(0) + attr.getValue() + literalChar.get(0), errorReporter);
                if (errors.isEmpty()) {
                    expressions.put(attr.getName().toLowerCase(), exp);
                    continue;
                }
            } catch (final Exception e) {
            }
            // Try other quotes before reporting a failure
            try {
                final CFExpression exp = cfmlParser.parseCFMLExpression(literalChar.get(1) + attr.getValue() + literalChar.get(1), this);
                expressions.put(attr.getName().toLowerCase(), exp);
            } catch (final Exception e2) {
                if (!quiet) {
                    System.err.println("Error in parsing : " + attr.getValue() + " on tag " + elem.getName());
                }
            }
        } else if (tagInfo.isExpressionAttribute(elem, attr.getName())) {
            try {
                final CFExpression exp = cfmlParser.parseCFMLExpression(attr.getValue(), this);
                expressions.put(attr.getName().toLowerCase(), exp);
            } catch (final Exception e2) {
                if (!quiet) {
                    System.err.println("Error in parsing : " + attr.getValue() + " on tag " + elem.getName());
                }
            }
        }
    }
    // Parse the body
    if (elem.getName().toLowerCase().equals("cfoutput")) {
        final String content = elem.getContent().getTextExtractor().toString();
        if (content != null && content.length() > 0 && content.contains("#")) {
            final List<String> errors = new ArrayList<>();
            final ANTLRErrorListener errorReporter = new ArrayErrorListener(errors);
            try {
                final CFExpression exp = cfmlParser.parseCFMLExpression(content, errorReporter);
                if (errors.isEmpty()) {
                    expressions.put("body", exp);
                }
            } catch (final Exception e) {
            }
        }
    }
    return expressions;
}
Also used : Attribute(net.htmlparser.jericho.Attribute) ArrayList(java.util.ArrayList) ArrayErrorListener(cfml.parsing.reporting.ArrayErrorListener) ANTLRErrorListener(org.antlr.v4.runtime.ANTLRErrorListener) ParseException(cfml.parsing.reporting.ParseException) IOException(java.io.IOException) CFLintScanException(com.cflint.exception.CFLintScanException) CFExpression(cfml.parsing.cfscript.CFExpression) LinkedHashMap(java.util.LinkedHashMap)

Example 7 with Attribute

use of net.htmlparser.jericho.Attribute in project CFLint by cflint.

the class VariableNameChecker method checkCFName.

// private final List<String> exclusions = new ArrayList<>();
private void checkCFName(final Element element, final Context context, final BugList bugs, final int begLine, int offset, final String name) {
    if (element.getAttributeValue(name) != null) {
        final Attribute attribute = element.getAttributes().get(name);
        String varName;
        if (attribute != null) {
            varName = CFScopes.descope(attribute.getValue());
            offset = attribute.getValueSegment().getBegin();
        } else {
            varName = "";
        }
        if (varName.length() > 0 && !varName.contains("#")) {
            checkNameForBugs(context, varName, varName, context.getFilename(), context.getFunctionName(), begLine, offset, bugs, null);
        }
    }
}
Also used : Attribute(net.htmlparser.jericho.Attribute)

Example 8 with Attribute

use of net.htmlparser.jericho.Attribute in project vue-gwt by Axellience.

the class TemplateParser method processElement.

/**
 * Recursive method that will process the whole template DOM tree.
 * @param element Current element being processed
 */
private void processElement(Element element) {
    context.setCurrentSegment(element);
    currentProp = null;
    currentAttribute = null;
    Attributes attributes = element.getAttributes();
    Attribute vForAttribute = attributes != null ? attributes.get("v-for") : null;
    if (vForAttribute != null) {
        // Add a context layer for our v-for
        context.addContextLayer();
        // Process the v-for expression, and update our attribute
        String processedVForValue = processVForValue(vForAttribute.getValue());
        outputDocument.replace(vForAttribute.getValueSegment(), processedVForValue);
    }
    // Process the element
    if (attributes != null)
        processElementAttributes(element);
    // Process text segments
    StreamSupport.stream(((Iterable<Segment>) element::getNodeIterator).spliterator(), false).filter(segment -> !(segment instanceof Tag) && !(segment instanceof CharacterReference)).filter(segment -> {
        for (Element child : element.getChildElements()) if (child.encloses(segment))
            return false;
        return true;
    }).forEach(this::processTextNode);
    // Recurse downwards
    element.getChildElements().forEach(this::processElement);
    // After downward recursion, pop the context layer
    if (vForAttribute != null)
        context.popContextLayer();
}
Also used : Attributes(net.htmlparser.jericho.Attributes) TemplateExpression(com.axellience.vuegwt.processors.component.template.parser.result.TemplateExpression) CastExpr(com.github.javaparser.ast.expr.CastExpr) Any(jsinterop.base.Any) OutputDocument(net.htmlparser.jericho.OutputDocument) HashSet(java.util.HashSet) Matcher(java.util.regex.Matcher) Type(com.github.javaparser.ast.type.Type) GeneratorsUtil.stringTypeToTypeName(com.axellience.vuegwt.processors.utils.GeneratorsUtil.stringTypeToTypeName) Prop(com.axellience.vuegwt.core.annotations.component.Prop) VariableInfo(com.axellience.vuegwt.processors.component.template.parser.variable.VariableInfo) CharacterReference(net.htmlparser.jericho.CharacterReference) Expression(com.github.javaparser.ast.expr.Expression) StreamSupport(java.util.stream.StreamSupport) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) Source(net.htmlparser.jericho.Source) LinkedList(java.util.LinkedList) Messager(javax.annotation.processing.Messager) NodeWithType(com.github.javaparser.ast.nodeTypes.NodeWithType) Element(net.htmlparser.jericho.Element) GeneratorsNameUtil.propNameToAttributeName(com.axellience.vuegwt.processors.utils.GeneratorsNameUtil.propNameToAttributeName) TemplateParserLoggerProvider(com.axellience.vuegwt.processors.component.template.parser.jericho.TemplateParserLoggerProvider) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) TemplateParserResult(com.axellience.vuegwt.processors.component.template.parser.result.TemplateParserResult) Set(java.util.Set) NameExpr(com.github.javaparser.ast.expr.NameExpr) LocalComponent(com.axellience.vuegwt.processors.component.template.parser.context.localcomponents.LocalComponent) Config(net.htmlparser.jericho.Config) Collectors(java.util.stream.Collectors) LocalVariableInfo(com.axellience.vuegwt.processors.component.template.parser.variable.LocalVariableInfo) Attribute(net.htmlparser.jericho.Attribute) List(java.util.List) ParseProblemException(com.github.javaparser.ParseProblemException) Tag(net.htmlparser.jericho.Tag) TypeName(com.squareup.javapoet.TypeName) Optional(java.util.Optional) LocalComponentProp(com.axellience.vuegwt.processors.component.template.parser.context.localcomponents.LocalComponentProp) Pattern(java.util.regex.Pattern) TemplateParserContext(com.axellience.vuegwt.processors.component.template.parser.context.TemplateParserContext) Segment(net.htmlparser.jericho.Segment) JavaParser(com.github.javaparser.JavaParser) Attribute(net.htmlparser.jericho.Attribute) Element(net.htmlparser.jericho.Element) Attributes(net.htmlparser.jericho.Attributes) CharacterReference(net.htmlparser.jericho.CharacterReference) Tag(net.htmlparser.jericho.Tag)

Example 9 with Attribute

use of net.htmlparser.jericho.Attribute in project zaproxy by zaproxy.

the class SpiderHtmlFormParser method parseResource.

@Override
public boolean parseResource(HttpMessage message, Source source, int depth) {
    getLogger().debug("Parsing an HTML message for forms...");
    // If form processing is disabled, don't parse anything
    if (!param.isProcessForm()) {
        return false;
    }
    // Prepare the source, if not provided
    if (source == null) {
        source = new Source(message.getResponseBody().toString());
    }
    // Get the context (base url)
    String baseURL = message.getRequestHeader().getURI().toString();
    uri = message.getRequestHeader().getURI();
    // Try to see if there's any BASE tag that could change the base URL
    Element base = source.getFirstElement(HTMLElementName.BASE);
    if (base != null) {
        if (getLogger().isDebugEnabled()) {
            getLogger().debug("Base tag was found in HTML: " + base.getDebugInfo());
        }
        String href = base.getAttributeValue("href");
        if (href != null && !href.isEmpty()) {
            baseURL = URLCanonicalizer.getCanonicalURL(href, baseURL);
        }
    }
    // Go through the forms
    List<Element> forms = source.getAllElements(HTMLElementName.FORM);
    for (Element form : forms) {
        // Clear the attributes for each form and store their key and values
        envAttributes.clear();
        for (Attribute att : form.getAttributes()) {
            envAttributes.put(att.getKey(), att.getValue());
        }
        // Get method and action
        String method = form.getAttributeValue("method");
        String action = form.getAttributeValue("action");
        getLogger().debug("Found new form with method: '" + method + "' and action: " + action);
        // If no action, skip the form
        if (action == null) {
            getLogger().debug("No form 'action' defined. Using base URL: " + baseURL);
            action = baseURL;
        }
        // If POSTing forms is not enabled, skip processing of forms with POST method
        if (!param.isPostForm() && method != null && method.trim().equalsIgnoreCase(METHOD_POST)) {
            getLogger().debug("Skipping form with POST method because of user settings.");
            continue;
        }
        // Clear the fragment, if any, as it does not have any relevance for the server
        if (action.contains("#")) {
            int fs = action.lastIndexOf("#");
            action = action.substring(0, fs);
        }
        url = URLCanonicalizer.getCanonicalURL(action, baseURL);
        FormData formData = prepareFormDataSet(source, form);
        // Process the case of a POST method
        if (method != null && method.trim().equalsIgnoreCase(METHOD_POST)) {
            // Build the absolute canonical URL
            String fullURL = URLCanonicalizer.getCanonicalURL(action, baseURL);
            if (fullURL == null) {
                return false;
            }
            getLogger().debug("Canonical URL constructed using '" + action + "': " + fullURL);
            for (String submitData : formData) {
                notifyPostResourceFound(message, depth, fullURL, submitData);
            }
        } else // Process anything else as a GET method
        {
            // Process the final URL
            if (action.contains("?")) {
                if (action.endsWith("?")) {
                    processGetForm(message, depth, action, baseURL, formData);
                } else {
                    processGetForm(message, depth, action + "&", baseURL, formData);
                }
            } else {
                processGetForm(message, depth, action + "?", baseURL, formData);
            }
        }
    }
    return false;
}
Also used : Attribute(net.htmlparser.jericho.Attribute) Element(net.htmlparser.jericho.Element) Source(net.htmlparser.jericho.Source)

Aggregations

Attribute (net.htmlparser.jericho.Attribute)9 Attributes (net.htmlparser.jericho.Attributes)4 Element (net.htmlparser.jericho.Element)4 Source (net.htmlparser.jericho.Source)3 LocalComponent (com.axellience.vuegwt.processors.component.template.parser.context.localcomponents.LocalComponent)2 LocalComponentProp (com.axellience.vuegwt.processors.component.template.parser.context.localcomponents.LocalComponentProp)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 OutputDocument (net.htmlparser.jericho.OutputDocument)2 CFExpression (cfml.parsing.cfscript.CFExpression)1 ArrayErrorListener (cfml.parsing.reporting.ArrayErrorListener)1 ParseException (cfml.parsing.reporting.ParseException)1 Prop (com.axellience.vuegwt.core.annotations.component.Prop)1 TemplateParserContext (com.axellience.vuegwt.processors.component.template.parser.context.TemplateParserContext)1 TemplateParserLoggerProvider (com.axellience.vuegwt.processors.component.template.parser.jericho.TemplateParserLoggerProvider)1 TemplateExpression (com.axellience.vuegwt.processors.component.template.parser.result.TemplateExpression)1 TemplateParserResult (com.axellience.vuegwt.processors.component.template.parser.result.TemplateParserResult)1 LocalVariableInfo (com.axellience.vuegwt.processors.component.template.parser.variable.LocalVariableInfo)1 VariableInfo (com.axellience.vuegwt.processors.component.template.parser.variable.VariableInfo)1 GeneratorsNameUtil.propNameToAttributeName (com.axellience.vuegwt.processors.utils.GeneratorsNameUtil.propNameToAttributeName)1