|
| 1 | +from collective.volto.formsupport import _ |
| 2 | +from collective.volto.formsupport.interfaces import ICaptchaSupport |
| 3 | +from collective.volto.formsupport.interfaces import IPostAdapter |
| 4 | +from collective.volto.formsupport.utils import get_blocks |
| 5 | +from collective.volto.otp.utils import validate_email_token |
| 6 | +from copy import deepcopy |
| 7 | +from plone import api |
| 8 | +from plone.restapi.deserializer import json_body |
| 9 | +from plone.schema.email import _isemail |
| 10 | +from zExceptions import BadRequest |
| 11 | +from zope.component import adapter |
| 12 | +from zope.component import getMultiAdapter |
| 13 | +from zope.i18n import translate |
| 14 | +from zope.interface import implementer |
| 15 | +from zope.interface import Interface |
| 16 | + |
| 17 | +import math |
| 18 | +import os |
| 19 | + |
| 20 | + |
| 21 | +@implementer(IPostAdapter) |
| 22 | +@adapter(Interface, Interface) |
| 23 | +class PostAdapter: |
| 24 | + block_id = None |
| 25 | + block = {} |
| 26 | + |
| 27 | + def __init__(self, context, request): |
| 28 | + self.context = context |
| 29 | + self.request = request |
| 30 | + self.form_data = self.extract_data_from_request() |
| 31 | + self.block_id = self.form_data.get("block_id", "") |
| 32 | + if self.block_id: |
| 33 | + self.block = self.get_block_data(block_id=self.block_id) |
| 34 | + |
| 35 | + def __call__(self): |
| 36 | + """ |
| 37 | + Avoid XSS injections and other attacks. |
| 38 | +
|
| 39 | + - cleanup HTML with plone transform |
| 40 | + - remove from data, fields not defined in form schema |
| 41 | + """ |
| 42 | + |
| 43 | + self.validate_form() |
| 44 | + |
| 45 | + return self.form_data |
| 46 | + |
| 47 | + def extract_data_from_request(self): |
| 48 | + form_data = json_body(self.request) |
| 49 | + |
| 50 | + fixed_fields = [] |
| 51 | + transforms = api.portal.get_tool(name="portal_transforms") |
| 52 | + |
| 53 | + block = self.get_block_data(block_id=form_data.get("block_id", "")) |
| 54 | + block_fields = [x.get("field_id", "") for x in block.get("subblocks", [])] |
| 55 | + custom_block_fields = [ |
| 56 | + block.get(field_id) for field_id in block_fields if block.get(field_id) |
| 57 | + ] |
| 58 | + |
| 59 | + for form_field in form_data.get("data", []): |
| 60 | + field_id = form_field.get("custom_field_id", form_field.get("field_id", "")) |
| 61 | + if field_id not in block_fields and field_id not in custom_block_fields: |
| 62 | + # unknown field, skip it |
| 63 | + continue |
| 64 | + new_field = deepcopy(form_field) |
| 65 | + value = new_field.get("value", "") |
| 66 | + if isinstance(value, str): |
| 67 | + stream = transforms.convertTo("text/plain", value, mimetype="text/html") |
| 68 | + new_field["value"] = stream.getData().strip() |
| 69 | + fixed_fields.append(new_field) |
| 70 | + |
| 71 | + form_data["data"] = fixed_fields |
| 72 | + |
| 73 | + return form_data |
| 74 | + |
| 75 | + def get_block_data(self, block_id): |
| 76 | + blocks = get_blocks(self.context) |
| 77 | + if not blocks: |
| 78 | + return {} |
| 79 | + for id, block in blocks.items(): |
| 80 | + if id != block_id: |
| 81 | + continue |
| 82 | + block_type = block.get("@type", "") |
| 83 | + if block_type != "form": |
| 84 | + continue |
| 85 | + return block |
| 86 | + return {} |
| 87 | + |
| 88 | + def validate_form(self): |
| 89 | + """ |
| 90 | + check all required fields and parameters |
| 91 | + """ |
| 92 | + if not self.block_id: |
| 93 | + raise BadRequest( |
| 94 | + translate( |
| 95 | + _("missing_blockid_label", default="Missing block_id"), |
| 96 | + context=self.request, |
| 97 | + ) |
| 98 | + ) |
| 99 | + if not self.block: |
| 100 | + raise BadRequest( |
| 101 | + translate( |
| 102 | + _( |
| 103 | + "block_form_not_found_label", |
| 104 | + default='Block with @type "form" and id "$block" not found in this context: $context', |
| 105 | + mapping={ |
| 106 | + "block": self.block_id, |
| 107 | + "context": self.context.absolute_url(), |
| 108 | + }, |
| 109 | + ), |
| 110 | + context=self.request, |
| 111 | + ), |
| 112 | + ) |
| 113 | + |
| 114 | + if not self.block.get("store", False) and not self.block.get("send", []): |
| 115 | + raise BadRequest( |
| 116 | + translate( |
| 117 | + _( |
| 118 | + "missing_action", |
| 119 | + default='You need to set at least one form action between "send" and "store".', # noqa |
| 120 | + ), |
| 121 | + context=self.request, |
| 122 | + ) |
| 123 | + ) |
| 124 | + |
| 125 | + if not self.form_data.get("data", []): |
| 126 | + raise BadRequest( |
| 127 | + translate( |
| 128 | + _( |
| 129 | + "empty_form_data", |
| 130 | + default="Empty form data.", |
| 131 | + ), |
| 132 | + context=self.request, |
| 133 | + ) |
| 134 | + ) |
| 135 | + |
| 136 | + self.validate_attachments() |
| 137 | + if self.block.get("captcha", False): |
| 138 | + getMultiAdapter( |
| 139 | + (self.context, self.request), |
| 140 | + ICaptchaSupport, |
| 141 | + name=self.block["captcha"], |
| 142 | + ).verify(self.form_data.get("captcha")) |
| 143 | + |
| 144 | + self.validate_bcc() |
| 145 | + |
| 146 | + def validate_bcc(self): |
| 147 | + """ |
| 148 | + If otp validation is enabled, check if is valid |
| 149 | + """ |
| 150 | + bcc_fields = [] |
| 151 | + email_otp_verification = self.block.get("email_otp_verification", False) |
| 152 | + block_id = self.form_data.get("block_id", "") |
| 153 | + for field in self.block.get("subblocks", []): |
| 154 | + if field.get("use_as_bcc", False): |
| 155 | + field_id = field.get("field_id", "") |
| 156 | + if field_id not in bcc_fields: |
| 157 | + bcc_fields.append(field_id) |
| 158 | + if not bcc_fields: |
| 159 | + return |
| 160 | + if not email_otp_verification: |
| 161 | + return |
| 162 | + for data in self.form_data.get("data", []): |
| 163 | + value = data.get("value", "") |
| 164 | + if not value: |
| 165 | + continue |
| 166 | + if data.get("field_id", "") not in bcc_fields: |
| 167 | + continue |
| 168 | + otp = data.get("otp", "") |
| 169 | + if not otp: |
| 170 | + raise BadRequest( |
| 171 | + api.portal.translate( |
| 172 | + _( |
| 173 | + "otp_validation_missing_value", |
| 174 | + default="Missing OTP value. Unable to submit the form.", |
| 175 | + ) |
| 176 | + ) |
| 177 | + ) |
| 178 | + if not validate_email_token(block_id, value, otp): |
| 179 | + raise BadRequest( |
| 180 | + api.portal.translate( |
| 181 | + _( |
| 182 | + "otp_validation_wrong_value", |
| 183 | + default="${email}'s OTP is wrong", |
| 184 | + mapping={"email": data["value"]}, |
| 185 | + ) |
| 186 | + ) |
| 187 | + ) |
| 188 | + |
| 189 | + def validate_attachments(self): |
| 190 | + attachments_limit = os.environ.get("FORM_ATTACHMENTS_LIMIT", "") |
| 191 | + if not attachments_limit: |
| 192 | + return |
| 193 | + attachments = self.form_data.get("attachments", {}) |
| 194 | + attachments_len = 0 |
| 195 | + for attachment in attachments.values(): |
| 196 | + data = attachment.get("data", "") |
| 197 | + attachments_len += (len(data) * 3) / 4 - data.count("=", -2) |
| 198 | + if attachments_len > float(attachments_limit) * pow(1024, 2): |
| 199 | + size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB") |
| 200 | + i = int(math.floor(math.log(attachments_len, 1024))) |
| 201 | + p = math.pow(1024, i) |
| 202 | + s = round(attachments_len / p, 2) |
| 203 | + uploaded_str = f"{s} {size_name[i]}" |
| 204 | + raise BadRequest( |
| 205 | + translate( |
| 206 | + _( |
| 207 | + "attachments_too_big", |
| 208 | + default="Attachments too big. You uploaded ${uploaded_str}," |
| 209 | + " but limit is ${max} MB. Try to compress files.", |
| 210 | + mapping={ |
| 211 | + "max": attachments_limit, |
| 212 | + "uploaded_str": uploaded_str, |
| 213 | + }, |
| 214 | + ), |
| 215 | + context=self.request, |
| 216 | + ) |
| 217 | + ) |
| 218 | + |
| 219 | + def filter_parameters(self): |
| 220 | + """ |
| 221 | + do not send attachments fields. |
| 222 | + """ |
| 223 | + result = [] |
| 224 | + |
| 225 | + for field in self.block.get("subblocks", []): |
| 226 | + if field.get("field_type", "") == "attachment": |
| 227 | + continue |
| 228 | + |
| 229 | + for item in self.form_data.get("data", []): |
| 230 | + if item.get("field_id", "") == field.get("field_id", ""): |
| 231 | + result.append(item) |
| 232 | + |
| 233 | + return result |
| 234 | + |
| 235 | + def format_fields(self): |
| 236 | + fields = self.filter_parameters() |
| 237 | + formatted_fields = [] |
| 238 | + field_ids = [field.get("field_id") for field in self.block.get("subblocks", [])] |
| 239 | + |
| 240 | + for field in fields: |
| 241 | + field_id = field.get("field_id", "") |
| 242 | + |
| 243 | + if field_id: |
| 244 | + field_index = field_ids.index(field_id) |
| 245 | + |
| 246 | + if self.block["subblocks"][field_index].get("field_type") == "date": |
| 247 | + field["value"] = api.portal.get_localized_time(field["value"]) |
| 248 | + |
| 249 | + formatted_fields.append(field) |
| 250 | + |
| 251 | + return formatted_fields |
0 commit comments