// comment_sanitize — a docs `before`/`create` INTERCEPTOR. // Fires for EVERY docs create in the app (posts, pages, comments), so it must // branch on the collection and no-op on everything but comments (F-020). // Receives the op-context as its request body: { service, op, collection, key, value }. // Returns #{ allowed: true, data: }; anything else DENIES. let p = ctx.request.body; if p == () || p.collection != "comments" { return #{ allowed: true }; // not a comment — pass through untouched } let v = p.value; if v == () { return #{ allowed: true }; } // --- sanitize the comment body: escape HTML so it can't inject markup --- let raw = v.body; let body = if raw == () { "" } else { "" + raw }; body.replace("&", "&"); // must be first body.replace("<", "<"); body.replace(">", ">"); body.replace("\"", """); body.replace("'", "'"); // also escape any HTML the display_name might carry let an = v.author_name; let name = if an == () { "anonymous" } else { "" + an }; name.replace("&", "&"); name.replace("<", "<"); name.replace(">", ">"); v.body = body; v.author_name = name; // --- moderation status from the site config var --- let mode = vars::get("comment-moderation"); if mode == "auto" { v.status = "approved"; } else { v.status = "pending"; } #{ allowed: true, data: v }