Package com.google.caja.parser

Examples of com.google.caja.parser.ParseTreeNode


  private static boolean attachScopes(AncestorChain<?> ac, ScopeInfo scope) {
    // We infect scopes root-wards if we see a problematic construct like
    // "eval" or "with" which would change behavior if variables in scope where
    // it is declared were renamed.
    boolean infected = false;
    ParseTreeNode n = ac.node;
    n.getAttributes().set(SCOPE, scope);
    if (n instanceof FunctionConstructor) {
      FunctionConstructor fc = (FunctionConstructor) n;
      scope = new ScopeInfo(scope, Scope.fromFunctionConstructor(scope.s, fc));
      if (fc.getIdentifierName() != null) {
        scope.fns.add(ac.cast(FunctionConstructor.class));
      }
      // A ctor's name is apparent in its scope, unlike a fn declarations name
      // which is apparent in the containing scope.
      n.getAttributes().set(SCOPE, scope);
    } else if (n instanceof CatchStmt) {
      CatchStmt cs = (CatchStmt) n;
      scope = new ScopeInfo(scope, Scope.fromCatchStmt(scope.s, cs));
      // Normally, declaration in a catch block are hoisted to the parent.
      // Since the logic below does that, make sure that the exception
      // declaration is not hoisted.
      scope.decls.add(AncestorChain.instance(ac, cs.getException()));
      cs.getException().getAttributes().set(SCOPE, scope);
      // And recurse to the body manually so as to avoid recursing to the
      // exception declaration.
      attachScopes(AncestorChain.instance(ac, cs.getBody()), scope);
      return false;
    } else if (n instanceof Reference) {
      Reference r = (Reference) n;
      String rName = r.getIdentifierName();
      Scope definingScope = scope.s.thatDefines(rName);
      assert (definingScope != null) || scope.s.isOuter(rName) : rName;
      scope.uses.add(new Use(scope.withScope(definingScope), rName));
      if ("eval".equals(rName)) { infected = true; }
      infected = infected || "eval".equals(rName);
    } else if (n instanceof Declaration) {
      ScopeInfo declaring = scope;
      // Hoist out of catch block scopes.
      while (declaring.s.getType() == ScopeType.CATCH) {
        declaring = declaring.parent;
      }
      declaring.decls.add(ac.cast(Declaration.class));
    } else if (n instanceof WithStmt) {
      // References inside with(...){} could be variable names or they could
      // be property names.
      infected = true;
    } else if (Operation.is(n, Operator.MEMBER_ACCESS)) {
      // Do not let the property name reference be treated as a reference to
      // a var or global.
      attachScopes(AncestorChain.instance(ac, n.children().get(0)), scope);
      return false;
    }
    for (ParseTreeNode child : n.children()) {
      infected |= attachScopes(AncestorChain.instance(ac, child), scope);
    }
    if (infected) { scope.setDynamicUsePossible(); }
    return infected;
  }
View Full Code Here


      final List<FunctionConstructor> inners) {
    final List<Pair<AncestorChain<Statement>, Statement>> changes
        = Lists.newArrayList();
    node.acceptPreOrder(new Visitor() {
      public boolean visit(AncestorChain<?> chain) {
        ParseTreeNode node = chain.node;
        if (node instanceof Declaration
            && !(node instanceof FunctionDeclaration)) {
          if (chain.parent.node instanceof CatchStmt) { return true; }
          Declaration decl = (Declaration) node;
          Identifier id = decl.getIdentifier();
          removedIdents.add(id);
          Expression init = decl.getInitializer();
          Statement replacement;
          if (init != null) {
            replacement = new ExpressionStmt(toAssignment(decl));
          } else if (chain.parent.node instanceof ForEachLoop) {
            replacement = new ExpressionStmt(new Reference(id));
          } else {
            replacement = new Noop(decl.getFilePosition());
          }
          changes.add(Pair.pair(chain.cast(Statement.class), replacement));
          return true;
        } else if (node instanceof MultiDeclaration) {
          List<Expression> replacements = Lists.newArrayList();
          for (Declaration decl : ((MultiDeclaration) node).children()) {
            removedIdents.add(decl.getIdentifier());
            if (decl.getInitializer() == null) { continue; }
            visit(chain.child(decl).child(decl.getInitializer()));
            Expression assign = toAssignment(decl);
            replacements.add(assign);
          }
          Statement replacement;
          if (replacements.isEmpty()) {
            replacement = new Noop(node.getFilePosition());
          } else if (replacements.size() == 1) {
            Expression e = replacements.get(0);
            replacement = new ExpressionStmt(e.getFilePosition(), e);
          } else if (chain.parent.node instanceof Block) {
            List<Statement> stmts = Lists.newArrayList();
            for (Expression e : replacements) {
              stmts.add(new ExpressionStmt(e));
            }
            replacement = new Block(node.getFilePosition(), stmts);
          } else {
            Expression combo = null;
            for (Expression e : replacements) {
              combo = combo == null
                  ? e : Operation.createInfix(Operator.COMMA, combo, e);
            }
            replacement = new ExpressionStmt(node.getFilePosition(), combo);
          }
          changes.add(Pair.pair(chain.cast(Statement.class), replacement));
          return false;
        } else if (node instanceof FunctionConstructor) {
          inners.add((FunctionConstructor) node);
View Full Code Here

                CharProducer.Factory.fromString(
                    "var " + candidate + ";",
                    InputSource.UNKNOWN)),
            InputSource.UNKNOWN),
        mq);
    ParseTreeNode node;
    try { node = parser.parse(); } catch (ParseException e) { return false; }
    if (node == null || !mq.getMessages().isEmpty()) { return false; }
    Map<String, ParseTreeNode> bindings = Maps.newHashMap();
    if (!QuasiBuilder.match("{ var @p; }", node, bindings)) { return false; }
    if (bindings.size() != 1) { return false; }
View Full Code Here

    if (jsonpCallback != null && !checkIdentifier(jsonpCallback)) {
      throw new RuntimeException("Detected XSS attempt; aborting request");
    }

    ParseTreeNode result = (jsonpCallback == null)
        ? obj(props)
        : QuasiBuilder.substV("@c(@o);",
            "c", new Reference(
                     new Identifier(
                         FilePosition.UNKNOWN,
                         jsonpCallback)),
            "o", obj(props));

    IOCallback callback = new IOCallback();
    RenderContext rc = makeRenderContext(output, callback, pretty, true);
    result.render(rc);
    rc.getOut().noMoreTokens();
    if (callback.ex != null) { throw callback.ex; }
  }
View Full Code Here

  private Block jsFromAttrib(AttrValue v) {
    EmbeddedContent c = attributeContent.get(v.src);
    if (c == null) { return null; }
    try {
      ParseTreeNode n = c.parse(meta.getUriFetcher(), mq);
      if (n instanceof Block) { return (Block) n; }
    } catch (ParseException ex) {
      ex.toMessageQueue(mq);
    }
    return null;
View Full Code Here

  private CssTree.DeclarationGroup styleFromAttrib(AttrValue v) {
    EmbeddedContent c = attributeContent.get(v.src);
    if (c == null) { return null; }
    try {
      ParseTreeNode n = c.parse(meta.getUriFetcher(), mq);
      if (n instanceof CssTree.DeclarationGroup) {
        return (CssTree.DeclarationGroup) n;
      }
    } catch (ParseException ex) {
      ex.toMessageQueue(mq);
View Full Code Here

    tainted.clear();
    forSideEffect.clear();
    node.makeImmutable();
    if (taintChecking) {
      flagTainted(node, mq);
      ParseTreeNode result = expand(node, null);
      if (!mq.hasMessageAtLevel(MessageLevel.ERROR)) {
        checkTainted(result, mq);
      }
      result.makeImmutable();
      return result;
    }
    return expand(node, null);
  }
View Full Code Here

  @Override
  protected boolean consumeSpecimens(
      List<ParseTreeNode> specimens, Map<String, ParseTreeNode> bindings) {
    if (specimens.isEmpty()) { return false; }
    ParseTreeNode specimen = specimens.get(0);
    if (!(specimen instanceof StringLiteral)) { return false; }
    StringLiteral lit = (StringLiteral) specimen;
    String ident = lit.getUnquotedValue();
    if (!ParserBase.isJavascriptIdentifier(ident)) { return false; }

    ParseTreeNode existing = bindings.get(bindingName);
    if (existing != null
        && !(existing instanceof Identifier
             && ident.equals(existing.getValue()))) {
      return false;
    } else {
      Identifier identBinding = new Identifier(lit.getFilePosition(), ident);
      bindings.put(bindingName, identBinding);
      specimens.remove(0);
View Full Code Here

  }

  @Override
  protected boolean createSubstitutes(
      List<ParseTreeNode> substitutes, Map<String, ParseTreeNode> bindings) {
    ParseTreeNode binding = bindings.get(bindingName);
    Identifier ident;
    if (binding instanceof Identifier) {
      ident = (Identifier) binding;
    } else if (binding instanceof Reference) {
      ident = ((Reference) binding).getIdentifier();
View Full Code Here

  @Override
  protected boolean consumeSpecimens(
      List<ParseTreeNode> specimens, Map<String, ParseTreeNode> bindings) {
    if (specimens.isEmpty()) { return false; }
    ParseTreeNode specimen = specimens.get(0);
    if (specimen instanceof Identifier && null == specimen.getValue()) {
      specimens.remove(0);
      return true;
    }
    return qn.consumeSpecimens(specimens, bindings);
  }
View Full Code Here

TOP

Related Classes of com.google.caja.parser.ParseTreeNode

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.