reactiveadmin 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +88 -0
- data/LICENSE +21 -0
- data/README.md +81 -0
- data/app/assets/reactive_admin/bridge.js +89 -0
- data/app/controllers/reactive_admin/api_controller.rb +30 -0
- data/app/controllers/reactive_admin/client_errors_controller.rb +38 -0
- data/app/controllers/reactive_admin/manifest_controller.rb +10 -0
- data/app/controllers/reactive_admin/mcp_controller.rb +48 -0
- data/app/controllers/reactive_admin/openapi_controller.rb +13 -0
- data/app/controllers/reactive_admin/session_controller.rb +11 -0
- data/app/views/layouts/reactive_admin/embed.html.erb +30 -0
- data/lib/generators/reactiveadmin/install/install_generator.rb +341 -0
- data/lib/generators/reactiveadmin/install/templates/menu.rb.tt +25 -0
- data/lib/generators/reactiveadmin/install/templates/reactive_admin.rb.tt +53 -0
- data/lib/generators/reactiveadmin/install/templates/reactive_admin_annotated.rb.tt +110 -0
- data/lib/reactive_admin/aa/v3/controller.rb +76 -0
- data/lib/reactive_admin/aa/v3/forms.rb +29 -0
- data/lib/reactive_admin/aa/v3/inputs/filters/typeahead_input.rb +51 -0
- data/lib/reactive_admin/aa/v3/inputs/typeahead_input.rb +8 -0
- data/lib/reactive_admin/aa/v3/install.rb +36 -0
- data/lib/reactive_admin/aa/v3/menu.rb +67 -0
- data/lib/reactive_admin/aa/v3/presenters.rb +102 -0
- data/lib/reactive_admin/aa/v3/registry.rb +78 -0
- data/lib/reactive_admin/aa/v3/views.rb +63 -0
- data/lib/reactive_admin/aa/v3.rb +23 -0
- data/lib/reactive_admin/aa/v4.rb +59 -0
- data/lib/reactive_admin/aa.rb +46 -0
- data/lib/reactive_admin/action_items.rb +39 -0
- data/lib/reactive_admin/action_links.rb +197 -0
- data/lib/reactive_admin/action_safety.rb +19 -0
- data/lib/reactive_admin/actions.rb +95 -0
- data/lib/reactive_admin/api_errors.rb +116 -0
- data/lib/reactive_admin/api_fallback.rb +15 -0
- data/lib/reactive_admin/api_guards.rb +445 -0
- data/lib/reactive_admin/audit.rb +72 -0
- data/lib/reactive_admin/auth/guard.rb +68 -0
- data/lib/reactive_admin/auth/token_requests.rb +15 -0
- data/lib/reactive_admin/auth.rb +135 -0
- data/lib/reactive_admin/authorizer.rb +47 -0
- data/lib/reactive_admin/background_requests.rb +33 -0
- data/lib/reactive_admin/batch_actions.rb +57 -0
- data/lib/reactive_admin/choices.rb +289 -0
- data/lib/reactive_admin/client_error.rb +70 -0
- data/lib/reactive_admin/composite_keys.rb +12 -0
- data/lib/reactive_admin/configuration.rb +221 -0
- data/lib/reactive_admin/controller_extension.rb +229 -0
- data/lib/reactive_admin/csv_injection.rb +26 -0
- data/lib/reactive_admin/date_bound.rb +31 -0
- data/lib/reactive_admin/degradation.rb +72 -0
- data/lib/reactive_admin/diagnostics.rb +16 -0
- data/lib/reactive_admin/doctor.rb +466 -0
- data/lib/reactive_admin/dry_run.rb +147 -0
- data/lib/reactive_admin/dry_run_flag.rb +18 -0
- data/lib/reactive_admin/embed.rb +73 -0
- data/lib/reactive_admin/engine.rb +47 -0
- data/lib/reactive_admin/error_keys.rb +23 -0
- data/lib/reactive_admin/exposure.rb +86 -0
- data/lib/reactive_admin/filters.rb +165 -0
- data/lib/reactive_admin/fixtures/scrubber.rb +1075 -0
- data/lib/reactive_admin/fixtures.rb +161 -0
- data/lib/reactive_admin/form_content.rb +78 -0
- data/lib/reactive_admin/form_field.rb +279 -0
- data/lib/reactive_admin/form_has_many.rb +116 -0
- data/lib/reactive_admin/form_lock.rb +71 -0
- data/lib/reactive_admin/form_payload.rb +57 -0
- data/lib/reactive_admin/form_recorder.rb +192 -0
- data/lib/reactive_admin/form_schema.rb +160 -0
- data/lib/reactive_admin/frame.rb +97 -0
- data/lib/reactive_admin/html_nodes.rb +390 -0
- data/lib/reactive_admin/html_policy.rb +338 -0
- data/lib/reactive_admin/html_rich.rb +186 -0
- data/lib/reactive_admin/implicit_defaults.rb +102 -0
- data/lib/reactive_admin/index_options.rb +41 -0
- data/lib/reactive_admin/index_payload.rb +259 -0
- data/lib/reactive_admin/index_recorder.rb +115 -0
- data/lib/reactive_admin/index_views.rb +85 -0
- data/lib/reactive_admin/inline_style.rb +125 -0
- data/lib/reactive_admin/labels_payload.rb +34 -0
- data/lib/reactive_admin/ladder.rb +268 -0
- data/lib/reactive_admin/layout_classes.rb +167 -0
- data/lib/reactive_admin/legacy_inventory.rb +547 -0
- data/lib/reactive_admin/location.rb +22 -0
- data/lib/reactive_admin/lookup_audit.rb +30 -0
- data/lib/reactive_admin/lookup_payload.rb +41 -0
- data/lib/reactive_admin/manifest.rb +36 -0
- data/lib/reactive_admin/masked_json.rb +62 -0
- data/lib/reactive_admin/masking.rb +100 -0
- data/lib/reactive_admin/mcp/access.rb +111 -0
- data/lib/reactive_admin/mcp/api.rb +123 -0
- data/lib/reactive_admin/mcp/count_tool.rb +22 -0
- data/lib/reactive_admin/mcp/layout.rb +175 -0
- data/lib/reactive_admin/mcp/output.rb +46 -0
- data/lib/reactive_admin/mcp/prompts.rb +111 -0
- data/lib/reactive_admin/mcp/server.rb +134 -0
- data/lib/reactive_admin/mcp/shapes.rb +164 -0
- data/lib/reactive_admin/mcp/tools.rb +386 -0
- data/lib/reactive_admin/mcp.rb +53 -0
- data/lib/reactive_admin/media_nodes.rb +34 -0
- data/lib/reactive_admin/menu.rb +90 -0
- data/lib/reactive_admin/menu_overlay.rb +291 -0
- data/lib/reactive_admin/node_registry.rb +47 -0
- data/lib/reactive_admin/open_api/builder.rb +156 -0
- data/lib/reactive_admin/open_api/context.rb +46 -0
- data/lib/reactive_admin/open_api/core.rb +51 -0
- data/lib/reactive_admin/open_api/operation_ids.rb +18 -0
- data/lib/reactive_admin/open_api/page_doc.rb +75 -0
- data/lib/reactive_admin/open_api/parts.rb +86 -0
- data/lib/reactive_admin/open_api/resource_doc.rb +423 -0
- data/lib/reactive_admin/open_api/security.rb +55 -0
- data/lib/reactive_admin/open_api/write_schema.rb +104 -0
- data/lib/reactive_admin/open_api.rb +41 -0
- data/lib/reactive_admin/option_source.rb +36 -0
- data/lib/reactive_admin/option_sources.rb +14 -0
- data/lib/reactive_admin/options_payload.rb +61 -0
- data/lib/reactive_admin/page_controller_extension.rb +112 -0
- data/lib/reactive_admin/page_forms.rb +181 -0
- data/lib/reactive_admin/page_payload.rb +120 -0
- data/lib/reactive_admin/param_normalizer.rb +89 -0
- data/lib/reactive_admin/paths.rb +63 -0
- data/lib/reactive_admin/permissions.rb +31 -0
- data/lib/reactive_admin/provenance.rb +57 -0
- data/lib/reactive_admin/query_guard.rb +108 -0
- data/lib/reactive_admin/ransack_guard.rb +107 -0
- data/lib/reactive_admin/rate_limit.rb +59 -0
- data/lib/reactive_admin/reactive_dsl.rb +99 -0
- data/lib/reactive_admin/readiness.rb +189 -0
- data/lib/reactive_admin/registry.rb +69 -0
- data/lib/reactive_admin/resource_meta.rb +120 -0
- data/lib/reactive_admin/resource_summary.rb +41 -0
- data/lib/reactive_admin/routes.rb +60 -0
- data/lib/reactive_admin/row_errors.rb +29 -0
- data/lib/reactive_admin/rspec.rb +116 -0
- data/lib/reactive_admin/scrubbed_error.rb +22 -0
- data/lib/reactive_admin/search.rb +79 -0
- data/lib/reactive_admin/server_timing.rb +31 -0
- data/lib/reactive_admin/show_payload.rb +88 -0
- data/lib/reactive_admin/sidebars.rb +34 -0
- data/lib/reactive_admin/site.rb +55 -0
- data/lib/reactive_admin/style_report.rb +129 -0
- data/lib/reactive_admin/style_sources.rb +83 -0
- data/lib/reactive_admin/template_actions.rb +126 -0
- data/lib/reactive_admin/template_content.rb +49 -0
- data/lib/reactive_admin/template_form.rb +442 -0
- data/lib/reactive_admin/test_support.rb +31 -0
- data/lib/reactive_admin/typeahead.rb +33 -0
- data/lib/reactive_admin/ui/assets.rb +91 -0
- data/lib/reactive_admin/ui/client_build.rb +47 -0
- data/lib/reactive_admin/ui/helper.rb +24 -0
- data/lib/reactive_admin/ui/packaged_client.rb +67 -0
- data/lib/reactive_admin/ui/shell.rb +81 -0
- data/lib/reactive_admin/ui/switch.rb +38 -0
- data/lib/reactive_admin/ui.rb +43 -0
- data/lib/reactive_admin/value_payload.rb +147 -0
- data/lib/reactive_admin/value_token.rb +36 -0
- data/lib/reactive_admin/values.rb +43 -0
- data/lib/reactive_admin/version.rb +3 -0
- data/lib/reactive_admin/write_audit.rb +60 -0
- data/lib/reactive_admin/write_guards.rb +142 -0
- data/lib/reactive_admin.rb +153 -0
- data/lib/reactiveadmin.rb +1 -0
- data/lib/tasks/reactiveadmin.rake +36 -0
- data/lib/tasks/reactiveadmin_client.rake +11 -0
- data/lib/tasks/reactiveadmin_legacy.rake +7 -0
- data/lib/tasks/reactiveadmin_styles.rake +6 -0
- data/openapi/core.yaml +1643 -0
- data/vendor/client/assets/ActionPage-XUYorI8E.js +1 -0
- data/vendor/client/assets/ActionView-BsZFcsgp.js +1 -0
- data/vendor/client/assets/CommandPalette-C59HT1ip.js +1 -0
- data/vendor/client/assets/DatePicker-DuaK0iMg.js +1 -0
- data/vendor/client/assets/DateRangeCalendar-B2BDsG-X.js +1 -0
- data/vendor/client/assets/DegradedNotice-DiH5n2DO.js +1 -0
- data/vendor/client/assets/FormDialog-pmYl8Eov.js +1 -0
- data/vendor/client/assets/FormSkeleton-DuSBZO3C.js +1 -0
- data/vendor/client/assets/FrameView-2X8cFvUt.css +1 -0
- data/vendor/client/assets/FrameView-zmUeDO8d.js +1 -0
- data/vendor/client/assets/GroupOverview-DI5T3k5K.js +1 -0
- data/vendor/client/assets/InlineForm-CMEIMgeh.js +1 -0
- data/vendor/client/assets/JsonDiff-o7JWr7CH.js +3 -0
- data/vendor/client/assets/JsonEditor-D4VXdlEx.js +1 -0
- data/vendor/client/assets/JsonView-BrJLDmSN.js +1 -0
- data/vendor/client/assets/MobileMenu-GAeFOeEb.js +1 -0
- data/vendor/client/assets/Node-SQl61V4K.js +2 -0
- data/vendor/client/assets/PageView-C-laNEPZ.js +1 -0
- data/vendor/client/assets/ResourceForm-BPjsoCY6.js +1 -0
- data/vendor/client/assets/ResourceIndex-COezAw5t.js +3 -0
- data/vendor/client/assets/ResourceOrPage-CRZGCY84.js +1 -0
- data/vendor/client/assets/ResourceShow-DweRXn18.js +1 -0
- data/vendor/client/assets/Segmented-BE9TCbph.js +1 -0
- data/vendor/client/assets/Sidebars-CsB9rFqX.js +1 -0
- data/vendor/client/assets/breadcrumb-DSA3Tlyd.js +1 -0
- data/vendor/client/assets/button-CvZ4_DFC.js +1 -0
- data/vendor/client/assets/calendar-BoyMpJIf.js +1 -0
- data/vendor/client/assets/check-C5A3J16N.js +1 -0
- data/vendor/client/assets/command-CMuk0oAt.js +1 -0
- data/vendor/client/assets/dist-CqEI82S_.js +1 -0
- data/vendor/client/assets/dist-IW4yX942.js +1 -0
- data/vendor/client/assets/editor-yqRLzgsJ.js +2 -0
- data/vendor/client/assets/external-link-C2aR4slL.js +1 -0
- data/vendor/client/assets/frames-C9gPCc3N.js +1 -0
- data/vendor/client/assets/humanize-DtU_Kpr4.js +1 -0
- data/vendor/client/assets/ibm-plex-mono-cyrillic-400-normal-BSMlKf0J.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-mono-cyrillic-400-normal-CEL4l2ZJ.woff +0 -0
- data/vendor/client/assets/ibm-plex-mono-cyrillic-500-normal-Ael50iVv.woff +0 -0
- data/vendor/client/assets/ibm-plex-mono-cyrillic-500-normal-Bq9vWWag.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-mono-cyrillic-ext-400-normal-DMdlQ8Kv.woff +0 -0
- data/vendor/client/assets/ibm-plex-mono-cyrillic-ext-400-normal-xuaO2J-f.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-mono-cyrillic-ext-500-normal-BIfNGwUT.woff +0 -0
- data/vendor/client/assets/ibm-plex-mono-cyrillic-ext-500-normal-BqneJy0T.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-mono-latin-400-normal-CvHOgSBP.woff +0 -0
- data/vendor/client/assets/ibm-plex-mono-latin-400-normal-DMJ8VG8y.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-mono-latin-500-normal-CB9ihrfo.woff +0 -0
- data/vendor/client/assets/ibm-plex-mono-latin-500-normal-DSY6xOcd.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-mono-latin-ext-400-normal-BmRBH3aV.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-mono-latin-ext-400-normal-D3D2R8hC.woff +0 -0
- data/vendor/client/assets/ibm-plex-mono-latin-ext-500-normal-CAhNIIs5.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-mono-latin-ext-500-normal-CZ70TYgx.woff +0 -0
- data/vendor/client/assets/ibm-plex-mono-vietnamese-400-normal-BulugwFq.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-mono-vietnamese-400-normal-DDuiU_S-.woff +0 -0
- data/vendor/client/assets/ibm-plex-mono-vietnamese-500-normal-C8zxqsMH.woff +0 -0
- data/vendor/client/assets/ibm-plex-mono-vietnamese-500-normal-DZ4AoWbu.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-sans-cyrillic-ext-wght-normal-d45eAU9y.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-sans-cyrillic-wght-normal-BAAhND-U.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-sans-greek-wght-normal-CmyJS8uq.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-sans-latin-ext-wght-normal-CIII54If.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-sans-latin-wght-normal-IvpUvPa2.woff2 +0 -0
- data/vendor/client/assets/ibm-plex-sans-vietnamese-wght-normal-Dg1JeJN0.woff2 +0 -0
- data/vendor/client/assets/index-B51aUaAY.js +43 -0
- data/vendor/client/assets/index-Cs9rCoWM.css +2 -0
- data/vendor/client/assets/input-DByFmQPa.js +1 -0
- data/vendor/client/assets/json-De3PN-R6.css +1 -0
- data/vendor/client/assets/json-oTp7Q-d-.js +5 -0
- data/vendor/client/assets/media-D4a_En8R.js +1 -0
- data/vendor/client/assets/plus-CfJ9vo8X.js +1 -0
- data/vendor/client/assets/popover-CDtI9R8Q.js +1 -0
- data/vendor/client/assets/purify-G7QKJyJq.js +1 -0
- data/vendor/client/assets/query-CCsZN2fa.js +1 -0
- data/vendor/client/assets/recent-BrzLwL0D.js +1 -0
- data/vendor/client/assets/rich-BQc_q5T7.js +1 -0
- data/vendor/client/assets/rolldown-runtime-XKyaHtl9.js +1 -0
- data/vendor/client/assets/select-C43qDA3k.js +1 -0
- data/vendor/client/assets/sheet-BPl2n-4n.js +1 -0
- data/vendor/client/assets/textarea-BnOphXry.js +1 -0
- data/vendor/client/assets/uiMode-z0ZbRV49.js +1 -0
- data/vendor/client/assets/uiToggle-DwnRQTH1.js +1 -0
- data/vendor/client/assets/useRecordForm-X_DExEP2.js +1 -0
- data/vendor/client/assets/useRecordVisit-BRtfMi-q.js +1 -0
- data/vendor/client/assets/utils-DwBGbDkG.js +1 -0
- data/vendor/client/assets/vendor-calendar-nTZiq9oY.js +1 -0
- data/vendor/client/assets/vendor-datefns-BoC6MCnl.js +1 -0
- data/vendor/client/assets/vendor-forms-D0h8hG_S.js +55 -0
- data/vendor/client/assets/vendor-purify-BXoPR7WT.js +3 -0
- data/vendor/client/assets/vendor-query-DZ6APRNI.js +1 -0
- data/vendor/client/assets/vendor-react-C1iE8mnY.js +9 -0
- data/vendor/client/assets/vendor-router-CI6Vh4k_.js +1 -0
- data/vendor/client/assets/vendor-table-DuQ835by.js +4 -0
- data/vendor/client/index.html +24 -0
- data/vendor/client/manifest.json +867 -0
- data/vendor/client/reactiveadmin-client.json +4 -0
- metadata +350 -0
|
@@ -0,0 +1,1075 @@
|
|
|
1
|
+
require "cgi"
|
|
2
|
+
require "digest"
|
|
3
|
+
|
|
4
|
+
module ReactiveAdmin
|
|
5
|
+
module Fixtures
|
|
6
|
+
# Replaces what a payload says about records with deterministic fakes of the same shape, and
|
|
7
|
+
# keeps what it says about the admin itself. Default deny: a string stays only under a key known
|
|
8
|
+
# to be structural (types, keys, params, labels the host wrote, enum choices); every other
|
|
9
|
+
# string is fake, so a payload field this class has never heard of can't leak. Masked nodes stay
|
|
10
|
+
# masked. Fakes derive from a digest of the original, so equal inputs give equal outputs across
|
|
11
|
+
# runs and files, and ids stay consistent between a row, its paths, refs and option lists.
|
|
12
|
+
class Scrubber
|
|
13
|
+
WORDS = %w[lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua
|
|
14
|
+
enim minim veniam quis nostrud exercitation ullamco laboris nisi aliquip ex ea commodo consequat duis aute irure in reprehenderit
|
|
15
|
+
voluptate velit esse cillum fugiat nulla pariatur excepteur sint occaecat cupidatat non proident sunt culpa qui officia deserunt
|
|
16
|
+
mollit anim id est laborum].freeze
|
|
17
|
+
|
|
18
|
+
# Strings under these keys describe the admin, never a record.
|
|
19
|
+
KEEP = %w[type kind key name param param_key control method variant presentation mode placement resource align sort_key tone direction
|
|
20
|
+
display justify breakpoint width max_width text_align tag language mime class_name custom_key api_guard field as default_order
|
|
21
|
+
default_scope current_scope order sortable view group page from to code locale time_zone date_format datetime_format layout icon parent
|
|
22
|
+
actions marks index_views download_formats trusted_media_hosts jsonrpc].to_set.freeze
|
|
23
|
+
# Written by the host (labels, confirmations, hints); dropped when they quote a record value.
|
|
24
|
+
TEXTS = %w[label title description hint placeholder plural_label menu_parent new_record_label submit_label submit_confirm confirm
|
|
25
|
+
disabled_reason row_actions_label unsupported_reason footer headers reason where].to_set.freeze
|
|
26
|
+
PATHS = %w[href url api_path path meta_path options_path expand_path form_path classic_url show edit api_show api_edit
|
|
27
|
+
index_path_template api_index_path_template title_link title_image favicon src].to_set.freeze
|
|
28
|
+
INPUT_LIMITS = %w[step maxlength rows].to_set.freeze
|
|
29
|
+
RECORD_LIMITS = %w[min max].to_set.freeze
|
|
30
|
+
ROW_TEXTS = %w[label confirm disabled_reason].to_set.freeze
|
|
31
|
+
QUERY_CONTEXTS = %w[filter form page_form template].to_set.freeze
|
|
32
|
+
BOOLEAN_CHOICES = %w[Yes No true false].to_set.freeze
|
|
33
|
+
NUMERIC_QUERY_KEYS = %w[page per_page form].to_set.freeze
|
|
34
|
+
# Query names the API, ActiveAdmin and Active Storage read; any other name can be record data (`?alice%40private.test=1`).
|
|
35
|
+
QUERY_NAMES = %w[
|
|
36
|
+
as batch_action batch_action_inputs classic collection_selection commit context cursor disposition embed field form format
|
|
37
|
+
id ids limit locale order page path per_page q scope term token value view
|
|
38
|
+
].to_set.freeze
|
|
39
|
+
QUERY_NAME = /\A(?<base>[a-z_]+)(?:\[[a-z0-9_]*\])*\z/
|
|
40
|
+
FORM_CONTROLS = %w[form input select textarea button].to_set.freeze
|
|
41
|
+
CSS_TOKEN = /\A-?[a-z_][a-z0-9_-]*\z/i
|
|
42
|
+
CSS_PROPERTY = /\A[a-z][a-z-]*\z/
|
|
43
|
+
CSS_VALUE = /\A[#a-z0-9\s.,%()+-]+\z/i
|
|
44
|
+
FORM_METHODS = %w[get post put patch delete dialog].to_set.freeze
|
|
45
|
+
FORM_ENCODINGS = %w[application/x-www-form-urlencoded multipart/form-data text/plain].to_set.freeze
|
|
46
|
+
# Attributes holding element ids: one, or a space-separated list (aria-labelledby).
|
|
47
|
+
HTML_ID_REFS = %w[id for list form aria-labelledby aria-describedby aria-controls aria-owns].to_set.freeze
|
|
48
|
+
HTML_ELEMENTS = %w[a abbr address area article aside audio b bdi bdo blockquote br button canvas caption cite code col colgroup
|
|
49
|
+
data datalist dd del details dfn dialog div dl dt em fieldset figcaption figure footer form h1 h2 h3 h4 h5 h6 header hgroup hr i img
|
|
50
|
+
input ins kbd label legend li main map mark menu meter nav ol optgroup option output p picture pre progress q rp rt ruby s samp
|
|
51
|
+
search section select slot small source span strong sub summary sup table tbody td template textarea tfoot th thead time tr track u
|
|
52
|
+
ul var video wbr].to_set.freeze
|
|
53
|
+
# Attributes whose values come from a fixed vocabulary keep a value only when it's in that vocabulary;
|
|
54
|
+
# anything else (target="Jane") is record data like any other attribute.
|
|
55
|
+
ENUM_ATTRIBUTES = {
|
|
56
|
+
"type" => %w[text password email number tel url search date time datetime-local month week color range checkbox radio file hidden submit reset button image 1 a A i I],
|
|
57
|
+
"autocomplete" => %w[on off name honorific-prefix given-name additional-name family-name honorific-suffix nickname email username new-password current-password one-time-code organization-title organization street-address address-line1 address-line2 address-line3 address-level1 address-level2 address-level3 address-level4 country country-name postal-code cc-name cc-given-name cc-family-name cc-number cc-exp cc-exp-month cc-exp-year cc-csc cc-type transaction-currency transaction-amount language bday bday-day bday-month bday-year sex tel tel-country-code tel-national tel-area-code tel-local tel-extension impp url photo shipping billing home work mobile fax pager webauthn],
|
|
58
|
+
"inputmode" => %w[none text decimal numeric tel search email url],
|
|
59
|
+
"role" => %w[alert alertdialog application article banner button cell checkbox columnheader combobox complementary contentinfo definition dialog directory document feed figure form grid gridcell group heading img link list listbox listitem log main marquee math menu menubar menuitem menuitemcheckbox menuitemradio navigation none note option presentation progressbar radio radiogroup region row rowgroup rowheader scrollbar search searchbox separator slider spinbutton status switch tab table tablist tabpanel term textbox timer toolbar tooltip tree treegrid treeitem],
|
|
60
|
+
"dir" => %w[ltr rtl auto],
|
|
61
|
+
"align" => %w[left right center justify top middle bottom baseline],
|
|
62
|
+
"target" => %w[_blank _self _parent _top],
|
|
63
|
+
"rel" => %w[alternate author bookmark external help license next nofollow noopener noreferrer opener prev search tag ugc sponsored stylesheet icon preload prefetch preconnect dns-prefetch modulepreload manifest canonical],
|
|
64
|
+
"open" => %w[open]
|
|
65
|
+
}.transform_values(&:to_set).freeze
|
|
66
|
+
# Record html keeps only attribute names HTML defines; `data-patient-jane-doe` can name a record.
|
|
67
|
+
HTML_ATTRIBUTES = %w[
|
|
68
|
+
abbr accept accesskey action align alt autocomplete autofocus bgcolor border cellpadding cellspacing checked cite class
|
|
69
|
+
color cols colspan content contenteditable controls coords datetime decoding default dir disabled download draggable
|
|
70
|
+
enctype for form headers height hidden high href hreflang id inputmode label lang list loading loop low max maxlength
|
|
71
|
+
media method min minlength multiple muted name nowrap open optimum pattern placeholder poster preload readonly rel
|
|
72
|
+
required reversed role rows rowspan scope selected shape size sizes span spellcheck src srclang srcset start step style
|
|
73
|
+
summary tabindex target title translate type valign value width wrap
|
|
74
|
+
aria-checked aria-controls aria-current aria-describedby aria-disabled aria-expanded aria-haspopup aria-hidden
|
|
75
|
+
aria-label aria-labelledby aria-live aria-pressed aria-selected
|
|
76
|
+
].to_set.freeze
|
|
77
|
+
NUMERIC_ATTRIBUTES = %w[colspan rowspan width height start].to_set.freeze
|
|
78
|
+
NUMERIC_ATTRIBUTE = /\A\d{1,4}(?:%|px)?\z/
|
|
79
|
+
LANGUAGE = /\A[a-z]{2,3}(?:-[A-Za-z0-9]{2,8})*\z/
|
|
80
|
+
|
|
81
|
+
UUID = /\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/
|
|
82
|
+
DIGITS = /\A\d+\z/
|
|
83
|
+
INTEGER_ID = /\A-?\d+\z/
|
|
84
|
+
EMAIL = /\A[^@\s<>]+@[^@\s<>]+\z/
|
|
85
|
+
NUMBERLIKE = /\A[\d.,\s$%+-]*\d[\d.,\s$%+-]*\z/
|
|
86
|
+
TIME = /\A(\d{4}-\d{2}-\d{2})(?:([T ])(\d{2}:\d{2}(?::\d{2})?)(\.\d+)?(Z|[+-]\d{2}:?\d{2})?)?\z/
|
|
87
|
+
TIME_OF_DAY = /\A\d{2}:\d{2}(?::\d{2}(\.\d+)?)?\z/
|
|
88
|
+
FILE_EXTENSIONS = %w[png jpg jpeg gif webp svg avif pdf csv txt json xml zip doc docx xls xlsx ppt pptx mp3 mp4 mov wav].to_set.freeze
|
|
89
|
+
MIN_LENGTH = 4
|
|
90
|
+
MIN_QUOTE = 2
|
|
91
|
+
MAX_WORDS = 120
|
|
92
|
+
MAX_KEPT_LIMIT = 9_999
|
|
93
|
+
MAX_TICKS = 100_000
|
|
94
|
+
# The route templates Paths emits; any other braced or colon segment is opaque record data.
|
|
95
|
+
PLACEHOLDERS = [Paths::PARENT_ID, ":id"].to_set.freeze
|
|
96
|
+
ID_RETRIES = 20
|
|
97
|
+
MAX_SALTS = 32
|
|
98
|
+
|
|
99
|
+
# vocabulary: each resource's enum names by attribute ({"posts" => {"status" => %w[draft published]}}), kept only
|
|
100
|
+
# as choices of that attribute (its options, its status tags), so a record's "White" isn't kept by another enum.
|
|
101
|
+
# routes: the literal segments of the app's routes; every other path segment is opaque and faked.
|
|
102
|
+
# seed: the app's private seed (Fixtures.seed). Every fake, and every date and number offset, derives from it,
|
|
103
|
+
# so a fixture can't be reversed without it; there is no public default to fall back on.
|
|
104
|
+
def self.numeric?(value) = value.is_a?(Numeric) || (value.is_a?(String) && value.match?(/\A-?\d+(?:\.\d+)?\z/))
|
|
105
|
+
|
|
106
|
+
def initialize(seed:, vocabulary: {}, routes: [])
|
|
107
|
+
raise ArgumentError, "a private seed is required" if seed.to_s.empty?
|
|
108
|
+
|
|
109
|
+
@vocabulary = vocabulary.to_h { |resource, enums| [resource.to_s, enums.to_h { |attribute, names| [attribute.to_s, choices_of(names)] }] }
|
|
110
|
+
@scope = BOOLEAN_CHOICES
|
|
111
|
+
@routes = routes.to_set
|
|
112
|
+
@seed = seed
|
|
113
|
+
@ids = {}
|
|
114
|
+
@taken = Set.new
|
|
115
|
+
@choices = {}
|
|
116
|
+
@taken_choices = @vocabulary.values.flat_map(&:values).reduce(BOOLEAN_CHOICES, :|).dup
|
|
117
|
+
@originals = Set.new
|
|
118
|
+
@pattern = nil
|
|
119
|
+
@key_pattern = nil
|
|
120
|
+
@keys = {}
|
|
121
|
+
@taken_keys = Set.new
|
|
122
|
+
@varying = Set.new
|
|
123
|
+
@known = Hash.new { |hash, key| hash[key] = Set.new }
|
|
124
|
+
@kept = Set.new
|
|
125
|
+
@html_ids = {}
|
|
126
|
+
@real_ids = Set.new
|
|
127
|
+
@varying_keys = Set.new
|
|
128
|
+
@taken_html_ids = Set.new
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# Host-written strings the last scrub kept verbatim, for a person to review.
|
|
132
|
+
def kept = @kept.sort
|
|
133
|
+
|
|
134
|
+
# entries: [[kind, payload], ...] with kind :manifest, :meta, :index, :show or :form. Scrubs them
|
|
135
|
+
# together so ids and fakes agree across payloads.
|
|
136
|
+
def scrub_all(entries)
|
|
137
|
+
@learning = true
|
|
138
|
+
entries.each { |kind, payload, resource| within(resource) { walk(payload, root: kind) } }
|
|
139
|
+
@learning = false
|
|
140
|
+
@kept.clear
|
|
141
|
+
quoted = @originals.select { it.size >= MIN_QUOTE }.sort_by { -it.size }
|
|
142
|
+
long, short = quoted.partition { it.size >= MIN_LENGTH }
|
|
143
|
+
# Short values only count as whole tokens, so "Amy" is found in "Delete Amy?" but not in "Amyloid".
|
|
144
|
+
parts = [(Regexp.union(long).source if long.any?), ("(?<![[:alnum:]])(?:#{Regexp.union(short).source})(?![[:alnum:]])" if short.any?)].compact
|
|
145
|
+
@pattern = Regexp.new(parts.join("|"), Regexp::IGNORECASE) if parts.any?
|
|
146
|
+
normalized = quoted.filter_map { normalize(it).presence }.uniq
|
|
147
|
+
@key_pattern = /(?<![a-z0-9])(?:#{Regexp.union(normalized).source})(?![a-z0-9])/ if normalized.any?
|
|
148
|
+
entries.map { |kind, payload, resource| within(resource) { walk(payload, root: kind) } }
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
private
|
|
152
|
+
|
|
153
|
+
def walk(node, key = nil, root: nil, parent: nil)
|
|
154
|
+
case node
|
|
155
|
+
when Hash then hash(node, root:)
|
|
156
|
+
when Array then node.map { walk(it, key, parent:) }
|
|
157
|
+
when String then string(node, key, parent, root)
|
|
158
|
+
else node
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def hash(node, root: nil)
|
|
163
|
+
return node if node["type"] == "masked"
|
|
164
|
+
|
|
165
|
+
learn(node) if @learning
|
|
166
|
+
return field(node) if node.key?("control") && (node.key?("param") || node.key?("name"))
|
|
167
|
+
return option(node) if option?(node)
|
|
168
|
+
|
|
169
|
+
node.to_h do |key, value|
|
|
170
|
+
[key, case key
|
|
171
|
+
when "value", "summary" then data_value(node, key, value)
|
|
172
|
+
when "id" then value.is_a?(Integer) ? integer_id(value) : walk(value, key, parent: node, root: (root if value.is_a?(String)))
|
|
173
|
+
when "params" then params(value)
|
|
174
|
+
when "rows" then (node["type"] == "definition_list") ? definition_rows(value) : rows(value, node)
|
|
175
|
+
when "cells" then cells(value, node)
|
|
176
|
+
when "scopes" then scopes(value)
|
|
177
|
+
else walk(value, key, parent: node, root: (root if value.is_a?(String)))
|
|
178
|
+
end]
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
# Query values a path may keep: the scopes, orders, fields and views the payloads declare.
|
|
183
|
+
def learn(node)
|
|
184
|
+
@known["scope"].merge(Array(node["scopes"]).filter_map { it["id"] if it.is_a?(Hash) })
|
|
185
|
+
@known["order"].merge(Array(node["columns"]).filter_map { it["sort_key"] if it.is_a?(Hash) }.product(%w[asc desc]).map { it.join("_") })
|
|
186
|
+
@known["as"].merge(Array(node["index_views"]).filter_map { it["as"] if it.is_a?(Hash) })
|
|
187
|
+
@known["format"].merge(Array(node["download_formats"]))
|
|
188
|
+
{"scope" => %w[default_scope current_scope], "order" => %w[default_order order], "view" => %w[view], "field" => %w[name]}.each do |query, keys|
|
|
189
|
+
next if query == "field" && !node.key?("control")
|
|
190
|
+
|
|
191
|
+
@known[query].merge(node.values_at(*keys).grep(String))
|
|
192
|
+
end
|
|
193
|
+
# Paths the API builds name the field they read (`field=comments.author` for a nested typeahead).
|
|
194
|
+
node.values_at("options_path", "expand_path").grep(String).each do |trusted|
|
|
195
|
+
field = query_field(trusted)
|
|
196
|
+
@known["field"] << field if field.is_a?(String) && field.present?
|
|
197
|
+
end
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
# A query it can't decode names no field; the path itself is still scrubbed like any other.
|
|
201
|
+
def query_field(path)
|
|
202
|
+
Rack::Utils.parse_query(path.split("?", 2)[1].to_s)["field"]
|
|
203
|
+
rescue Rack::QueryParser::InvalidParameterError, ArgumentError
|
|
204
|
+
nil
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
# A scope's id names a declared scope, so it stays even when it looks like a record id ("1"), which keeps
|
|
208
|
+
# default_scope and current_scope pointing at it.
|
|
209
|
+
def scopes(value)
|
|
210
|
+
return walk(value, "scopes") unless value.is_a?(Array)
|
|
211
|
+
|
|
212
|
+
value.map do |scope|
|
|
213
|
+
next walk(scope, "scopes") unless scope.is_a?(Hash)
|
|
214
|
+
|
|
215
|
+
# The id names the scope; its name and label are display text, quote-checked and listed for review.
|
|
216
|
+
scope.to_h do |key, item|
|
|
217
|
+
[key, if key == "id" then item
|
|
218
|
+
elsif %w[name label].include?(key) && item.is_a?(String) && !item.empty? then text(item)
|
|
219
|
+
else walk(item, key, parent: scope)
|
|
220
|
+
end]
|
|
221
|
+
end
|
|
222
|
+
end
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
# What differs between rows (a row_class, an action's label) comes from the record, not the host.
|
|
226
|
+
def rows(value, node)
|
|
227
|
+
if value.is_a?(Array) && value.all?(Hash)
|
|
228
|
+
varying(value.map { it["class_name"] })
|
|
229
|
+
by_key = value.flat_map { Array(it["actions"]).grep(Hash) }.group_by { it["key"] }.values
|
|
230
|
+
# A label-derived key (ActionLinks: "Call Bob" -> call_bob) differs per row, so the same slot is compared too.
|
|
231
|
+
by_slot = value.map { Array(it["actions"]).grep(Hash) }.then { |slots| (0...slots.map(&:size).max.to_i).map { |i| slots.filter_map { it[i] } } }
|
|
232
|
+
(by_key + by_slot).each do |actions|
|
|
233
|
+
ROW_TEXTS.each { |key| varying(actions.map { it[key] }) }
|
|
234
|
+
@varying_keys.merge(actions.filter_map { it["key"] }) if actions.map { it["key"] }.uniq.size > 1
|
|
235
|
+
end
|
|
236
|
+
end
|
|
237
|
+
return attribute_rows(value, node) if node["type"] == "attributes_table" && value.is_a?(Array)
|
|
238
|
+
|
|
239
|
+
walk(value, "rows", parent: node)
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
def varying(values) = (@varying.merge(values.grep(String)) if values.uniq.size > 1)
|
|
243
|
+
|
|
244
|
+
# A definition list comes from stored html, so its terms are record data, unlike an attributes table's labels.
|
|
245
|
+
def definition_rows(rows)
|
|
246
|
+
rows.map { |row| row.to_h { |key, value| [key, (key == "label") ? data(value) : walk(value, key, parent: row)] } }
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
# Keyed by column name, so a column called `value` or `params` is not that payload key.
|
|
250
|
+
def cells(value, parent) = value.is_a?(Hash) ? value.to_h { |column, cell| [column, showing(column) { walk(cell, "cells", parent:) }] } : walk(value, "cells", parent:)
|
|
251
|
+
|
|
252
|
+
# An attributes table row names its attribute only by its label ("Status").
|
|
253
|
+
def attribute_rows(rows, node)
|
|
254
|
+
rows.map do |row|
|
|
255
|
+
label = row["label"] if row.is_a?(Hash)
|
|
256
|
+
attribute = @vocabulary.fetch(@resource, {}).keys.find { [it.humanize, it.titleize].include?(label) }
|
|
257
|
+
showing(attribute) { walk(row, "rows", parent: node) }
|
|
258
|
+
end
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
# A status tag keeps its label only as a name of the enum its column shows, and only if it quotes no record.
|
|
262
|
+
def status_label(value)
|
|
263
|
+
choices = BOOLEAN_CHOICES | @vocabulary.dig(@resource, @showing.to_s).to_a
|
|
264
|
+
return data(value) unless choices.include?(value)
|
|
265
|
+
|
|
266
|
+
@pattern&.match?(value) ? fake(value) : value
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
def showing(attribute)
|
|
270
|
+
was, @showing = @showing, attribute
|
|
271
|
+
yield
|
|
272
|
+
ensure
|
|
273
|
+
@showing = was
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
# Hidden fields of a navigating action: the names are the host's, the values are not.
|
|
277
|
+
def params(value) = value.is_a?(Hash) ? value.transform_values { data_value(nil, nil, it) } : walk(value, "params")
|
|
278
|
+
|
|
279
|
+
def option?(node) = node.key?("label") && node.key?("value") && !node.key?("type") && !node["value"].is_a?(Array) && !node["value"].is_a?(Hash)
|
|
280
|
+
|
|
281
|
+
def option(node)
|
|
282
|
+
node.to_h do |key, value|
|
|
283
|
+
[key, case key
|
|
284
|
+
when "label" then (vocabulary?(node["value"]) && !record_id?(node["value"])) ? enum(value) : data(value)
|
|
285
|
+
when "hint" then data(value)
|
|
286
|
+
when "value" then option_value(value)
|
|
287
|
+
else walk(value, key, parent: node)
|
|
288
|
+
end]
|
|
289
|
+
end
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
def option_value(value)
|
|
293
|
+
return value unless value.is_a?(String)
|
|
294
|
+
|
|
295
|
+
(real_id?(value) || id_like?(value)) ? id(value) : choice(value)
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
# Selects and checkboxes tell choices apart by value, so two choices never share a fake.
|
|
299
|
+
def choice(value)
|
|
300
|
+
return value if value.empty? || value == ValuePayload::MASK || vocabulary?(value)
|
|
301
|
+
|
|
302
|
+
@choices[value] ||= begin
|
|
303
|
+
fake = data(value)
|
|
304
|
+
attempt = 0
|
|
305
|
+
fake = "#{data(value)} #{attempt += 1}" until @taken_choices.add?(fake)
|
|
306
|
+
fake
|
|
307
|
+
end
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
def field(node)
|
|
311
|
+
# A hidden foreign key (author_id) names a record, so it gets the same fake as that record everywhere.
|
|
312
|
+
return hidden_id(node) if node["control"] == "hidden" && (node["name"] == "id" || node["name"].to_s.end_with?("_id"))
|
|
313
|
+
|
|
314
|
+
values = Array(node["options"]).filter_map { it["value"] if it.is_a?(Hash) }.to_set
|
|
315
|
+
choices = BOOLEAN_CHOICES | @vocabulary.dig(@resource, node["name"].to_s).to_a
|
|
316
|
+
with_scope(choices) { with_interval(interval(node)) { field_keys(node, values) } }
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
def field_keys(node, values)
|
|
320
|
+
bounds = number_bounds(node)
|
|
321
|
+
node.to_h do |key, value|
|
|
322
|
+
[key, case key
|
|
323
|
+
when "value"
|
|
324
|
+
if @interval&.key?(key) then @interval[key]
|
|
325
|
+
elsif bounds then bounded_number(value, *bounds)
|
|
326
|
+
else fit_length(field_value(value, values), node["input_html"], value)
|
|
327
|
+
end
|
|
328
|
+
when "options" then value.map { walk(it, key, parent: node) }
|
|
329
|
+
when "summary" then data_value(node, key, value)
|
|
330
|
+
when "input_html" then input_html(value, node["param"])
|
|
331
|
+
else walk(value, key, parent: node)
|
|
332
|
+
end]
|
|
333
|
+
end
|
|
334
|
+
end
|
|
335
|
+
|
|
336
|
+
# A min or max can come from the record (a date it can't precede); one that every form agrees on can't.
|
|
337
|
+
def input_html(value, param)
|
|
338
|
+
return walk(value, "input_html") unless value.is_a?(Hash)
|
|
339
|
+
|
|
340
|
+
value.to_h do |name, limit|
|
|
341
|
+
[name, if RECORD_LIMITS.include?(name) then record_limit(param, name, limit)
|
|
342
|
+
elsif INPUT_LIMITS.include?(name) then limit
|
|
343
|
+
else walk(limit, name, parent: value)
|
|
344
|
+
end]
|
|
345
|
+
end
|
|
346
|
+
end
|
|
347
|
+
|
|
348
|
+
# A min or max can come from the record (`min: current_admin_user.birth_date`) however many forms agree on
|
|
349
|
+
# it, so only a small whole number (a quantity's bounds) is kept; dates and the rest are faked and learned.
|
|
350
|
+
def record_limit(param, name, limit)
|
|
351
|
+
return @interval[name] if @interval&.key?(name)
|
|
352
|
+
|
|
353
|
+
structural_limit?(limit) ? limit : data_value(nil, nil, limit)
|
|
354
|
+
end
|
|
355
|
+
|
|
356
|
+
def structural_limit?(limit) = limit.is_a?(Integer) ? limit.abs <= MAX_KEPT_LIMIT : limit.is_a?(String) && limit.match?(/\A-?\d{1,4}\z/)
|
|
357
|
+
|
|
358
|
+
# `f.input :id, as: :hidden` can carry the id as an Integer; it gets the same mapping as the string form.
|
|
359
|
+
def hidden_id(node)
|
|
360
|
+
node.to_h do |key, value|
|
|
361
|
+
[key, if key != "value" then walk(value, key, parent: node)
|
|
362
|
+
elsif value.is_a?(Integer) then integer_id(value)
|
|
363
|
+
elsif value.is_a?(String) && !value.empty? then id(value)
|
|
364
|
+
else value
|
|
365
|
+
end]
|
|
366
|
+
end
|
|
367
|
+
end
|
|
368
|
+
|
|
369
|
+
def field_value(value, options)
|
|
370
|
+
case value
|
|
371
|
+
when Array then value.map { field_value(it, options) }
|
|
372
|
+
when String then options.include?(value) ? option_value(value) : data(value)
|
|
373
|
+
else data_value(nil, nil, value)
|
|
374
|
+
end
|
|
375
|
+
end
|
|
376
|
+
|
|
377
|
+
def string(value, key, parent, root)
|
|
378
|
+
return value if value.empty?
|
|
379
|
+
return path(value) if PATHS.include?(key)
|
|
380
|
+
return action_key(value) if key == "key" && parent&.key?("kind")
|
|
381
|
+
return data(value) if @varying.include?(value) && (ROW_TEXTS.include?(key) || key == "class_name")
|
|
382
|
+
return row_class(value) if key == "class_name"
|
|
383
|
+
return value if KEEP.include?(key)
|
|
384
|
+
return identifier_id(value, parent) if key == "id"
|
|
385
|
+
return data(value) if root == :show && key == "title"
|
|
386
|
+
return data(value) if html_derived?(key, parent)
|
|
387
|
+
return text_of(value, key, parent) if TEXTS.include?(key)
|
|
388
|
+
return host_markup { data(value) } if key == "html" && %w[frame html].include?(parent&.[]("type"))
|
|
389
|
+
|
|
390
|
+
data(value)
|
|
391
|
+
end
|
|
392
|
+
|
|
393
|
+
# Only markup the host renders (frame and html nodes) keeps its structural attributes. Html inside a record
|
|
394
|
+
# value, a json value included, is record data: every attribute value is faked.
|
|
395
|
+
def host_markup
|
|
396
|
+
was, @host_markup = @host_markup, true
|
|
397
|
+
yield
|
|
398
|
+
ensure
|
|
399
|
+
@host_markup = was
|
|
400
|
+
end
|
|
401
|
+
|
|
402
|
+
# A row_class can be built from the record ("customer-#{record.email}"), even when only one row was sampled.
|
|
403
|
+
def row_class(value)
|
|
404
|
+
return fake(value) if quotes_original?(value)
|
|
405
|
+
|
|
406
|
+
@kept << value unless @learning
|
|
407
|
+
value
|
|
408
|
+
end
|
|
409
|
+
|
|
410
|
+
def quotes_original?(value) = @pattern&.match?(value) || @key_pattern&.match?(normalize(value))
|
|
411
|
+
|
|
412
|
+
# "Jane Doe", "jane-doe" and "jane_doe" all read as jane_doe.
|
|
413
|
+
def normalize(value) = value.parameterize(separator: "_").tr("-", "_")
|
|
414
|
+
|
|
415
|
+
# Nodes built from stored html carry its text in titles and headers.
|
|
416
|
+
def html_derived?(key, parent)
|
|
417
|
+
(key == "title" && %w[text document iframe].include?(parent&.[]("type"))) || (key == "headers" && parent&.[]("type") == "table")
|
|
418
|
+
end
|
|
419
|
+
|
|
420
|
+
# ActionLinks derives a link's key from its label when the host gave none, so it can quote a record.
|
|
421
|
+
def action_key(value) = (@varying_keys.include?(value) || @key_pattern&.match?(value)) ? slug(value).tr("-", "_") : value
|
|
422
|
+
|
|
423
|
+
def text_of(value, key, parent)
|
|
424
|
+
return status_label(value) if key == "label" && parent&.[]("type") == "status"
|
|
425
|
+
return (parent.dig("ref", "id") == value) ? id(value) : data(value) if key == "label" && (parent&.key?("type") || parent&.[]("id") == "current_user")
|
|
426
|
+
|
|
427
|
+
text(value)
|
|
428
|
+
end
|
|
429
|
+
|
|
430
|
+
# Host-written text stays unless it quotes something that was replaced elsewhere.
|
|
431
|
+
def text(value)
|
|
432
|
+
return fake(value) if @pattern&.match?(value)
|
|
433
|
+
|
|
434
|
+
@kept << value unless @learning
|
|
435
|
+
value
|
|
436
|
+
end
|
|
437
|
+
|
|
438
|
+
def identifier_id(value, parent)
|
|
439
|
+
(record?(parent) || id_like?(value) || real_id?(value)) ? id(value) : value
|
|
440
|
+
end
|
|
441
|
+
|
|
442
|
+
# Menu items and scopes have ids too; a record's sits beside what only a record has.
|
|
443
|
+
def record?(parent)
|
|
444
|
+
return false unless parent
|
|
445
|
+
|
|
446
|
+
%w[display_name paths permissions].any? { parent.key?(it) } || (parent.key?("resource") && !parent.key?("kind"))
|
|
447
|
+
end
|
|
448
|
+
|
|
449
|
+
def id_like?(value) = value.match?(INTEGER_ID) || value.match?(UUID)
|
|
450
|
+
|
|
451
|
+
def vocabulary?(value) = value.is_a?(String) && @scope.include?(value)
|
|
452
|
+
|
|
453
|
+
def record_id?(value) = value.is_a?(String) && (real_id?(value) || id_like?(value))
|
|
454
|
+
|
|
455
|
+
def choices_of(names) = names.flat_map { [it.to_s, it.to_s.humanize, it.to_s.titleize] }.to_set
|
|
456
|
+
|
|
457
|
+
def within(resource)
|
|
458
|
+
was, @resource = @resource, resource&.to_s
|
|
459
|
+
yield
|
|
460
|
+
ensure
|
|
461
|
+
@resource = was
|
|
462
|
+
end
|
|
463
|
+
|
|
464
|
+
def with_scope(choices)
|
|
465
|
+
was, @scope = @scope, choices
|
|
466
|
+
yield
|
|
467
|
+
ensure
|
|
468
|
+
@scope = was
|
|
469
|
+
end
|
|
470
|
+
|
|
471
|
+
def enum(value) = vocabulary?(value) ? value : data(value)
|
|
472
|
+
|
|
473
|
+
# Data under a `value` key: a node's value, a lock, a json blob, a field.
|
|
474
|
+
def data_value(parent, key, value)
|
|
475
|
+
case value
|
|
476
|
+
when Hash then (parent && key == "summary") ? summary(value) : blob(value)
|
|
477
|
+
when Array then (parent && parent["type"] != "json" && value.all? { it.is_a?(Hash) && it.key?("type") }) ? value.map { walk(it, key, parent:) } : value.map { blob(it) }
|
|
478
|
+
when String then (key == "value" && parent&.key?("field")) ? lock(value) : data(value)
|
|
479
|
+
when Integer, Float then number(value)
|
|
480
|
+
else value
|
|
481
|
+
end
|
|
482
|
+
end
|
|
483
|
+
|
|
484
|
+
def summary(value)
|
|
485
|
+
value.to_h do |key, item|
|
|
486
|
+
[key, case key
|
|
487
|
+
when "keys" then Array(item).map { data_key(it) }
|
|
488
|
+
when "size" then item
|
|
489
|
+
else walk(item, key)
|
|
490
|
+
end]
|
|
491
|
+
end
|
|
492
|
+
end
|
|
493
|
+
|
|
494
|
+
def blob(value)
|
|
495
|
+
case value
|
|
496
|
+
when Hash then value.to_h { |key, item| [data_key(key), blob(item)] }
|
|
497
|
+
when Array then value.map { blob(it) }
|
|
498
|
+
else data_value(nil, nil, value)
|
|
499
|
+
end
|
|
500
|
+
end
|
|
501
|
+
|
|
502
|
+
# Every key is faked: a key can name a diagnosis as well as a value can. Injective, so no entry is lost.
|
|
503
|
+
# Learned like a value, so host text quoting a key (a panel titled "Contact jane@doe.test") is scrubbed too.
|
|
504
|
+
def data_key(key)
|
|
505
|
+
@originals << key.to_s unless key.to_s.empty?
|
|
506
|
+
@keys[key.to_s] ||= (0..).lazy.map { slug("#{key}\0#{it}") }.find { @taken_keys.add?(it) }
|
|
507
|
+
end
|
|
508
|
+
|
|
509
|
+
def remember(value)
|
|
510
|
+
return unless value.is_a?(String) && !value.empty? && value != ValuePayload::MASK
|
|
511
|
+
|
|
512
|
+
@originals << value
|
|
513
|
+
url_pieces(value).each { @originals << it } if value.match?(%r{\A(?:[a-z][a-z0-9+.-]*:)?//|\A/}i)
|
|
514
|
+
end
|
|
515
|
+
|
|
516
|
+
# The parts of a url host text might quote on their own: decoded path segments, a file's name without its
|
|
517
|
+
# extension, and query values. Route words and short pieces are left out, since they quote nothing.
|
|
518
|
+
def url_pieces(value)
|
|
519
|
+
rest = value.sub(%r{\A(?:[a-z][a-z0-9+.-]*:)?//[^/?#]*}i, "")
|
|
520
|
+
route, query = rest.split("#", 2).first.to_s.split("?", 2)
|
|
521
|
+
segments = route.to_s.split("/").map { CGI.unescape(it) }.reject { it.size < MIN_LENGTH || @routes.include?(it) }
|
|
522
|
+
stems = segments.filter_map { it.rpartition(".").then { |stem, dot, ext| stem if !dot.empty? && FILE_EXTENSIONS.include?(ext.downcase) } }
|
|
523
|
+
values = query.to_s.split("&").filter_map { CGI.unescape(it.split("=", 2)[1].to_s).presence }.reject { it.size < MIN_LENGTH }
|
|
524
|
+
[*segments, *stems, *values]
|
|
525
|
+
end
|
|
526
|
+
|
|
527
|
+
def lock(value)
|
|
528
|
+
time, token = value.split("~", 2)
|
|
529
|
+
[fake(time), (digest("lock", token).unpack1("H#{token.to_s.size.clamp(1, 32)}") if token)].compact.join("~")
|
|
530
|
+
end
|
|
531
|
+
|
|
532
|
+
def data(value)
|
|
533
|
+
return value if value.empty? || value == ValuePayload::MASK
|
|
534
|
+
|
|
535
|
+
fake(value)
|
|
536
|
+
end
|
|
537
|
+
|
|
538
|
+
# Every replacement of a record value goes through here, so the original always feeds quote detection: a
|
|
539
|
+
# replaced value can't then survive where host text quotes it.
|
|
540
|
+
def fake(value)
|
|
541
|
+
remember(value)
|
|
542
|
+
differing(value) { fake_once(value) }
|
|
543
|
+
end
|
|
544
|
+
|
|
545
|
+
# Retries with a salted digest until the fake differs from the original ("lorem" can't stay "lorem").
|
|
546
|
+
def differing(original)
|
|
547
|
+
was = @salt
|
|
548
|
+
result = yield
|
|
549
|
+
(1..MAX_SALTS).each do |salt|
|
|
550
|
+
break unless result.to_s == original.to_s
|
|
551
|
+
|
|
552
|
+
@salt = salt
|
|
553
|
+
result = yield
|
|
554
|
+
end
|
|
555
|
+
(result.to_s == original.to_s) ? altered(result) : result
|
|
556
|
+
ensure
|
|
557
|
+
@salt = was
|
|
558
|
+
end
|
|
559
|
+
|
|
560
|
+
# Past the retries a digest can still land on the original (one digit has nine fakes); one changed character can't.
|
|
561
|
+
def altered(value)
|
|
562
|
+
return value + 1 if value.is_a?(Numeric)
|
|
563
|
+
|
|
564
|
+
value = value.to_s
|
|
565
|
+
# Whitespace or punctuation alone is layout, not data.
|
|
566
|
+
index = value.rindex(/[[:alnum:]]/) or return value
|
|
567
|
+
char = value[index]
|
|
568
|
+
value[0...index] + {"9" => "8", "z" => "y", "Z" => "Y"}.fetch(char) { char.match?(/[0-8a-yA-Y]/) ? char.next : "x" } + value[index + 1..]
|
|
569
|
+
end
|
|
570
|
+
|
|
571
|
+
def fake_once(value)
|
|
572
|
+
case value
|
|
573
|
+
when UUID then uuid(value)
|
|
574
|
+
when /\A[a-z][a-z0-9+.-]*:\/\//i then "https://example.com/#{slug(value)}"
|
|
575
|
+
when EMAIL then "user#{digits("email", value, 5)}@example.com"
|
|
576
|
+
when TIME then time(value)
|
|
577
|
+
when TIME_OF_DAY then time_of_day(value)
|
|
578
|
+
when NUMBERLIKE then numberlike(value)
|
|
579
|
+
when /<\/?[a-z][^>]*>/i then markup?(value) ? html(value) : words(value)
|
|
580
|
+
else words(value)
|
|
581
|
+
end
|
|
582
|
+
end
|
|
583
|
+
|
|
584
|
+
def words(value)
|
|
585
|
+
value.split("\n", -1).first(20).map do |line|
|
|
586
|
+
bytes = digest("words", line).bytes
|
|
587
|
+
line.sub(/\S(?:.*\S)?/) do |text|
|
|
588
|
+
text.split.first(MAX_WORDS).each_with_index.map do |token, index|
|
|
589
|
+
word = WORDS[(bytes[index % bytes.size] + index) % WORDS.size]
|
|
590
|
+
token.match?(/\A\p{Upper}/) ? word.capitalize : word
|
|
591
|
+
end.join(" ")
|
|
592
|
+
end
|
|
593
|
+
end.join("\n")
|
|
594
|
+
end
|
|
595
|
+
|
|
596
|
+
# Record text with angle brackets (`Contact <alice@example.test>`) isn't markup: the "element" would be data.
|
|
597
|
+
def markup?(value)
|
|
598
|
+
fragment = HtmlPolicy.parse(value) or return false
|
|
599
|
+
fragment.css("*").any? { HTML_ELEMENTS.include?(it.name) }
|
|
600
|
+
end
|
|
601
|
+
|
|
602
|
+
def html(value)
|
|
603
|
+
fragment = HtmlPolicy.parse(value) or return words(value)
|
|
604
|
+
# An element the HTML vocabulary doesn't name is learned and becomes a span, never kept by name.
|
|
605
|
+
fragment.css("*").each do |element|
|
|
606
|
+
next if HTML_ELEMENTS.include?(element.name)
|
|
607
|
+
|
|
608
|
+
remember(element.name)
|
|
609
|
+
element.name = "span"
|
|
610
|
+
end
|
|
611
|
+
# Neither text nor elements, so nothing below would scrub them; the client never shows them anyway.
|
|
612
|
+
hidden = []
|
|
613
|
+
fragment.traverse { |node| hidden << node if node.comment? || node.processing_instruction? || node.cdata? }
|
|
614
|
+
hidden.each { discard(it.content) }.each(&:remove)
|
|
615
|
+
fragment.traverse do |node|
|
|
616
|
+
if node.text? then node.content = node.content.sub(/\S.*\S|\S/m) { data(it) }
|
|
617
|
+
elsif node.element?
|
|
618
|
+
limits = input_limits(node)
|
|
619
|
+
node.attribute_nodes.each { |attribute| limits.key?(attribute.name) ? attribute.value = limits[attribute.name] : html_attribute!(node, attribute) }
|
|
620
|
+
end
|
|
621
|
+
end
|
|
622
|
+
fragment.to_html
|
|
623
|
+
end
|
|
624
|
+
|
|
625
|
+
def html_attribute!(element, attribute)
|
|
626
|
+
return attribute.value = html_attribute(element, attribute) if @host_markup || HTML_ATTRIBUTES.include?(attribute.name)
|
|
627
|
+
|
|
628
|
+
remember(attribute.name)
|
|
629
|
+
remember(attribute.name.sub(/\A(?:data|aria)-/, ""))
|
|
630
|
+
discard(attribute.value)
|
|
631
|
+
attribute.remove
|
|
632
|
+
end
|
|
633
|
+
|
|
634
|
+
# What's dropped is never shown, but a label can still quote it: the whole, and its email-, url- and number-like parts.
|
|
635
|
+
def discard(text)
|
|
636
|
+
text = text.to_s.strip
|
|
637
|
+
return if text.empty?
|
|
638
|
+
|
|
639
|
+
remember(text)
|
|
640
|
+
text.split(/[\s"'<>(),;]+/).each { remember(it) if it.size >= MIN_LENGTH && it.match?(%r{[@\d:/]}) }
|
|
641
|
+
end
|
|
642
|
+
|
|
643
|
+
# A frame's <input> keeps its bounds and value by the rule a typed field follows, so the value stays inside them.
|
|
644
|
+
def input_limits(element)
|
|
645
|
+
return {} unless @host_markup && element.name == "input" && element.key?("min") | element.key?("max")
|
|
646
|
+
|
|
647
|
+
html = {"min" => element["min"], "max" => element["max"], "step" => element["step"]}.compact
|
|
648
|
+
node = {"control" => element["type"].to_s.downcase, "name" => element["name"].to_s, "input_html" => html}
|
|
649
|
+
node["value"] = element["value"] if element["value"]
|
|
650
|
+
faked = field(node)
|
|
651
|
+
faked["input_html"].slice("min", "max").merge(faked.slice("value")).transform_values(&:to_s)
|
|
652
|
+
end
|
|
653
|
+
|
|
654
|
+
def html_attribute(element, attribute)
|
|
655
|
+
name = attribute.name
|
|
656
|
+
return %w[href src].include?(name) ? path(attribute.value) : data(attribute.value) unless @host_markup
|
|
657
|
+
return attribute.value if name == "name" && FORM_CONTROLS.include?(element.name)
|
|
658
|
+
# Rails masks the CSRF token afresh on every render.
|
|
659
|
+
return "token" if name == "value" && element["name"] == "authenticity_token"
|
|
660
|
+
return attribute.value if name == "value" && element["name"] == "_method" && FORM_METHODS.include?(attribute.value.downcase)
|
|
661
|
+
return attribute.value if %w[method formmethod data-method].include?(name) && FORM_METHODS.include?(attribute.value.downcase)
|
|
662
|
+
# Choices are told apart by value, so they get the injective mapping JSON options get.
|
|
663
|
+
return option_value(attribute.value) if name == "value" && choice_control?(element)
|
|
664
|
+
return attribute.value if %w[enctype formenctype].include?(name) && FORM_ENCODINGS.include?(attribute.value.downcase)
|
|
665
|
+
return path(attribute.value) if %w[action formaction].include?(name)
|
|
666
|
+
return css_classes(attribute.value) if name == "class"
|
|
667
|
+
return css_style(attribute.value) if name == "style"
|
|
668
|
+
# Enumerated values (type="date", role="dialog") name what an element is, so a learned label "Date"
|
|
669
|
+
# mustn't rewrite them; only a value outside a plain enum's shape is treated as data.
|
|
670
|
+
return attribute.value if structural_attribute?(name, attribute.value)
|
|
671
|
+
return path(attribute.value) if %w[href src].include?(name)
|
|
672
|
+
# The client mounts a date picker on exactly this marker, and drops values it doesn't know.
|
|
673
|
+
return attribute.value if name == HtmlPolicy::WIDGET_ATTRIBUTE && HtmlPolicy::WIDGETS.key?(attribute.value)
|
|
674
|
+
return attribute.value.split.map { html_id(it) }.join(" ") if HTML_ID_REFS.include?(name)
|
|
675
|
+
data(attribute.value)
|
|
676
|
+
end
|
|
677
|
+
|
|
678
|
+
# A boolean attribute may be empty; any other value has to be a word of the attribute's vocabulary.
|
|
679
|
+
def structural_attribute?(name, value)
|
|
680
|
+
return value.empty? || value.split.all? { ENUM_ATTRIBUTES[name].include?(it) } if ENUM_ATTRIBUTES.key?(name)
|
|
681
|
+
return value.match?(NUMERIC_ATTRIBUTE) if NUMERIC_ATTRIBUTES.include?(name)
|
|
682
|
+
|
|
683
|
+
name == "lang" && value.match?(LANGUAGE)
|
|
684
|
+
end
|
|
685
|
+
|
|
686
|
+
def choice_control?(element) = element.name == "option" || (element.name == "input" && %w[radio checkbox].include?(element["type"].to_s.downcase))
|
|
687
|
+
|
|
688
|
+
# A class or style in html can come from the record, even inside an opaque json value, so only what a closed
|
|
689
|
+
# rule allows is kept: plain class names, and declarations of a plain property with a plain value. A class or
|
|
690
|
+
# value that quotes a scrubbed value is faked, and anything else (custom properties, quotes, urls, @) dropped.
|
|
691
|
+
def css_classes(value)
|
|
692
|
+
value.split.map do |token|
|
|
693
|
+
next token if token.match?(CSS_TOKEN) && !quotes_original?(token)
|
|
694
|
+
|
|
695
|
+
remember(token)
|
|
696
|
+
slug(token)
|
|
697
|
+
end.join(" ")
|
|
698
|
+
end
|
|
699
|
+
|
|
700
|
+
def css_style(value)
|
|
701
|
+
value.split(";").filter_map do |declaration|
|
|
702
|
+
property, rule = declaration.split(":", 2).map(&:strip)
|
|
703
|
+
next declaration.strip if property&.match?(CSS_PROPERTY) && rule&.match?(CSS_VALUE) && !quotes_original?(rule)
|
|
704
|
+
|
|
705
|
+
discard(rule)
|
|
706
|
+
nil
|
|
707
|
+
end.join("; ")
|
|
708
|
+
end
|
|
709
|
+
|
|
710
|
+
# One fake per original, never shared, so a label keeps pointing at its own control.
|
|
711
|
+
# An id or a link's fragment can carry record data (`#alice@example.test`), so it's learned like any value.
|
|
712
|
+
def html_id(value)
|
|
713
|
+
remember(value)
|
|
714
|
+
@html_ids[value] ||= (0..).lazy.map { slug("#{value}\0#{it}") }.find { it != value && @taken_html_ids.add?(it) }
|
|
715
|
+
end
|
|
716
|
+
|
|
717
|
+
# The client enforces maxlength and minlength, so a faked value that was valid stays valid.
|
|
718
|
+
def fit_length(value, html, original)
|
|
719
|
+
return value unless value.is_a?(String) && html.is_a?(Hash)
|
|
720
|
+
|
|
721
|
+
max, min = html.values_at("maxlength", "minlength").map { Integer(it, exception: false) if it.is_a?(Integer) || it.is_a?(String) }
|
|
722
|
+
max = nil unless max&.positive?
|
|
723
|
+
return value if (max.nil? || value.size <= max) && (min.nil? || value.size >= min)
|
|
724
|
+
|
|
725
|
+
target = [[value.size, max].compact.min, min].compact.max
|
|
726
|
+
target = max if max && target > max
|
|
727
|
+
# Fitting can land back on the original (u@x.io under a maxlength of 6); then other letters, or filler.
|
|
728
|
+
differing(original) { fitted(value, target) }
|
|
729
|
+
end
|
|
730
|
+
|
|
731
|
+
def fitted(value, target)
|
|
732
|
+
local, domain = @salt ? digest("shape", value).bytes.first(2).map { ("a".."z").to_a[it % 26] } : %w[u x]
|
|
733
|
+
case value
|
|
734
|
+
when EMAIL then shaped(value, target, local, "@#{domain}.io", 6)
|
|
735
|
+
when /\A[a-z][a-z0-9+.-]*:\/\//i then shaped(value, target, "https://#{domain}.io/", "", 14)
|
|
736
|
+
else @salt ? digits("fit", value, target) : value.ljust(target, "x")[0, target]
|
|
737
|
+
end
|
|
738
|
+
end
|
|
739
|
+
|
|
740
|
+
# A short fake of the same kind (u1@x.io, https://x.io/a) rather than a truncated one that no longer parses.
|
|
741
|
+
def shaped(value, target, prefix, suffix, shortest)
|
|
742
|
+
return (@salt ? digits("fit", value, target) : value[0, target]) if target < shortest
|
|
743
|
+
|
|
744
|
+
room = target - prefix.size - suffix.size
|
|
745
|
+
filler = room.positive? ? digits("fit", value, room) : ""
|
|
746
|
+
"#{prefix}#{filler}#{suffix}"
|
|
747
|
+
end
|
|
748
|
+
|
|
749
|
+
# A number field whose bounds are kept (small, structural) keeps a valid value valid: faked within them.
|
|
750
|
+
def number_bounds(node)
|
|
751
|
+
html = node["input_html"]
|
|
752
|
+
return unless html.is_a?(Hash) && %w[number range].include?(node["control"])
|
|
753
|
+
|
|
754
|
+
bounds = html.values_at("min", "max")
|
|
755
|
+
return if bounds.none? || bounds.compact.any? { !structural_limit?(it) }
|
|
756
|
+
|
|
757
|
+
min, max = bounds.map { Float(it) if it }
|
|
758
|
+
step = Float(html["step"], exception: false) if html["step"].is_a?(Numeric) || html["step"].is_a?(String)
|
|
759
|
+
[min, max, step, html["step"].to_s[/\.(\d+)/, 1].to_s.size] if min.nil? || max.nil? || min <= max
|
|
760
|
+
end
|
|
761
|
+
|
|
762
|
+
def bounded_number(value, min, max, step, step_decimals)
|
|
763
|
+
number = Float(value, exception: false) if value.is_a?(Numeric) || value.is_a?(String)
|
|
764
|
+
within = number && (min.nil? || number >= min) && (max.nil? || number <= max)
|
|
765
|
+
return field_value(value, Set.new) unless within
|
|
766
|
+
|
|
767
|
+
min, max = mirrored(number, min, max)
|
|
768
|
+
remember(value.to_s)
|
|
769
|
+
others, decimals = other_ticks(value, number, min, max, step, step_decimals)
|
|
770
|
+
# Off the grid: an independent fake is all that's left.
|
|
771
|
+
return field_value(value, Set.new) if others.empty?
|
|
772
|
+
|
|
773
|
+
fake = others[digest("bounded", value.to_s).unpack1("N") % others.size].round(decimals)
|
|
774
|
+
return fake.to_i if value.is_a?(Integer)
|
|
775
|
+
return (decimals.zero? ? fake.to_i.to_s : format("%.#{decimals}f", fake)) if value.is_a?(String)
|
|
776
|
+
|
|
777
|
+
fake
|
|
778
|
+
end
|
|
779
|
+
|
|
780
|
+
def other_ticks(value, number, min, max, step, step_decimals)
|
|
781
|
+
whole = value.is_a?(Integer) || (value.is_a?(String) && !value.include?("."))
|
|
782
|
+
step = nil unless step&.positive?
|
|
783
|
+
decimals = 0
|
|
784
|
+
decimals = [(step ? step_decimals : 0), value.to_s[/\.(\d+)/, 1].to_s.size].max unless whole
|
|
785
|
+
[grid(min, max, step || 10.0**-decimals, whole).reject { (it - number).abs < 1e-9 }, decimals]
|
|
786
|
+
end
|
|
787
|
+
|
|
788
|
+
# Kept bounds that admit only the original (min = max = 3) reveal it, so they move with it instead.
|
|
789
|
+
def pinned?(node)
|
|
790
|
+
min, max, step, step_decimals = number_bounds(node)
|
|
791
|
+
value = node["value"]
|
|
792
|
+
return false if (min.nil? && max.nil?) || !numeric?(value)
|
|
793
|
+
|
|
794
|
+
number = Float(value)
|
|
795
|
+
return false if (min && number < min) || (max && number > max)
|
|
796
|
+
|
|
797
|
+
other_ticks(value, number, *mirrored(number, min, max), step, step_decimals).first.empty?
|
|
798
|
+
end
|
|
799
|
+
|
|
800
|
+
# With one bound declared, the open side mirrors the value's distance from it, so the fake stays as plausible.
|
|
801
|
+
def mirrored(number, min, max)
|
|
802
|
+
min ||= (number >= 0) ? [0.0, 2 * number - max].max : 2 * number - max
|
|
803
|
+
[min, max || 2 * number - min + 1]
|
|
804
|
+
end
|
|
805
|
+
|
|
806
|
+
# The step's ticks from min, as exact rationals so 0.3 * 26 is 7.8; whole values keep only whole ticks.
|
|
807
|
+
def grid(min, max, step, whole)
|
|
808
|
+
start, unit = Rational(min.to_s), Rational(step.to_s)
|
|
809
|
+
count = ((Rational(max.to_s) - start) / unit).floor
|
|
810
|
+
ticks = (0..[count, MAX_TICKS].min).map { start + it * unit }
|
|
811
|
+
ticks = ticks.select { it.denominator == 1 } if whole
|
|
812
|
+
ticks.map(&:to_f)
|
|
813
|
+
end
|
|
814
|
+
|
|
815
|
+
# A field whose bounds can't be kept (2.5, 25000, any date) gets fake bounds of its own, in order, and a value
|
|
816
|
+
# drawn anywhere on the grid inside them. Nothing ties the fake value to a real bound, so knowing a bound (a
|
|
817
|
+
# hard-coded 1900-01-01) doesn't give the value back.
|
|
818
|
+
def interval(node)
|
|
819
|
+
html = node["input_html"]
|
|
820
|
+
return unless html.is_a?(Hash)
|
|
821
|
+
|
|
822
|
+
limits = html.values_at("min", "max")
|
|
823
|
+
axis = if %w[number range].include?(node["control"]) && limits.any? { numeric?(it) }
|
|
824
|
+
NumberAxis.for(html, node["value"]) unless number_bounds(node) && !pinned?(node)
|
|
825
|
+
elsif limits.any? { it.is_a?(String) && it.match?(TIME) }
|
|
826
|
+
TimeAxis.for(html, node["value"])
|
|
827
|
+
end
|
|
828
|
+
axis && fake_interval(node["param"] || node["name"], {"min" => html["min"], "max" => html["max"], "value" => node["value"]}, axis)
|
|
829
|
+
end
|
|
830
|
+
|
|
831
|
+
def fake_interval(key, raw, axis)
|
|
832
|
+
originals = raw.transform_values { axis.at(it) }.compact
|
|
833
|
+
return if originals.empty?
|
|
834
|
+
|
|
835
|
+
raw.slice(*originals.keys).each_value { remember(it.to_s) }
|
|
836
|
+
fakes = nil
|
|
837
|
+
(0..5).each do |salt|
|
|
838
|
+
fakes = draw(key, salt, *originals.values_at("min", "max", "value"), axis).compact
|
|
839
|
+
break if fakes.none? { |name, at| at == originals[name] }
|
|
840
|
+
end
|
|
841
|
+
fakes.to_h { |name, at| [name, axis.format(raw[name], at)] }
|
|
842
|
+
end
|
|
843
|
+
|
|
844
|
+
def draw(key, salt, min, max, value, axis)
|
|
845
|
+
pick = ->(kind, choices) { digest(kind, "#{key}\0#{salt}").unpack1("Q>") % choices }
|
|
846
|
+
unit, steps = axis.unit, (axis.span / axis.unit).floor
|
|
847
|
+
anchor = axis.base + unit * pick.call("anchor", steps + 1)
|
|
848
|
+
width = (min == max) ? 0 : unit * (1 + pick.call("width", ((max - min) * 2 / unit).floor.clamp(1, steps))) if min && max
|
|
849
|
+
low = anchor if min
|
|
850
|
+
high = min ? anchor + width : anchor if max
|
|
851
|
+
return {"min" => low, "max" => high} unless value
|
|
852
|
+
|
|
853
|
+
from = low || [high - axis.span, axis.base].max
|
|
854
|
+
to = high || from + axis.span
|
|
855
|
+
{"min" => low, "max" => high, "value" => from + unit * pick.call("value", ((to - from) / unit).floor + 1)}
|
|
856
|
+
end
|
|
857
|
+
|
|
858
|
+
def with_interval(fakes)
|
|
859
|
+
was, @interval = @interval, fakes
|
|
860
|
+
yield
|
|
861
|
+
ensure
|
|
862
|
+
@interval = was
|
|
863
|
+
end
|
|
864
|
+
|
|
865
|
+
def numeric?(value) = Scrubber.numeric?(value)
|
|
866
|
+
|
|
867
|
+
def time(value)
|
|
868
|
+
_, _, separator, clock, fraction, zone = *TIME.match(value)
|
|
869
|
+
base = Time.utc(2020, 1, 1) + digest("time", value).unpack1("N") % (5 * 365 * 86_400)
|
|
870
|
+
return base.strftime("%F") unless clock
|
|
871
|
+
|
|
872
|
+
"#{base.strftime("%F")}#{separator}#{clock_of(base, clock)}#{fraction&.then { "." + "0" * (it.size - 1) }}#{zone}"
|
|
873
|
+
end
|
|
874
|
+
|
|
875
|
+
# A datetime-local value can stop at minutes (12:30); the fake keeps the same precision.
|
|
876
|
+
def clock_of(time, clock) = time.strftime((clock.count(":") == 1) ? "%H:%M" : "%T")
|
|
877
|
+
|
|
878
|
+
def time_of_day(value)
|
|
879
|
+
hour, minute, second = digest("clock", value).unpack("CCC").zip([24, 60, 60]).map { |byte, limit| byte % limit }
|
|
880
|
+
clock = (value.count(":") == 2) ? format("%02d:%02d:%02d", hour, minute, second) : format("%02d:%02d", hour, minute)
|
|
881
|
+
(fraction = value[/\.\d+\z/]) ? "#{clock}.#{"0" * (fraction.size - 1)}" : clock
|
|
882
|
+
end
|
|
883
|
+
|
|
884
|
+
def numberlike(value)
|
|
885
|
+
seen = 0
|
|
886
|
+
value.gsub(/\d/) do |digit|
|
|
887
|
+
seen += 1
|
|
888
|
+
fresh = digest("number", "#{value}#{seen}").bytes.first % 10
|
|
889
|
+
if seen == 1 && digit != "0"
|
|
890
|
+
(fresh.zero? ? 1 : fresh).to_s
|
|
891
|
+
else
|
|
892
|
+
fresh.to_s
|
|
893
|
+
end
|
|
894
|
+
end
|
|
895
|
+
end
|
|
896
|
+
|
|
897
|
+
def number(value)
|
|
898
|
+
return value if value.zero?
|
|
899
|
+
|
|
900
|
+
@originals << value.to_s
|
|
901
|
+
differing(value) { number_once(value) }
|
|
902
|
+
end
|
|
903
|
+
|
|
904
|
+
def number_once(value)
|
|
905
|
+
mantissa, exponent = value.abs.to_s.split("e", 2)
|
|
906
|
+
whole, fraction = mantissa.split(".", 2)
|
|
907
|
+
fake = numberlike(whole)
|
|
908
|
+
fake = "#{fake}.#{numberlike("1#{fraction}")[1..]}" if fraction
|
|
909
|
+
fake = "#{fake}e#{exponent}" if exponent
|
|
910
|
+
faked = value.is_a?(Float) ? Float(fake) : Integer(fake)
|
|
911
|
+
(value.negative? ? -1 : 1) * (faked.infinite? ? 1.0 : faked)
|
|
912
|
+
end
|
|
913
|
+
|
|
914
|
+
def uuid(value)
|
|
915
|
+
hex = digest("uuid", value).unpack1("H32")
|
|
916
|
+
[hex[0, 8], hex[8, 4], "4#{hex[13, 3]}", "a#{hex[17, 3]}", hex[20, 12]].join("-")
|
|
917
|
+
end
|
|
918
|
+
|
|
919
|
+
def slug(value) = "#{WORDS[digest("slug", value).bytes.first % WORDS.size]}-#{digits("slug", value, 4)}"
|
|
920
|
+
|
|
921
|
+
def digits(kind, value, length) = digest(kind, value).unpack1("N").to_s.rjust(length, "0")[-length..]
|
|
922
|
+
|
|
923
|
+
def digest(kind, value) = Digest::SHA256.digest("#{@seed}#{"\0#{@salt}" if @salt}\0#{kind}\0#{value}")
|
|
924
|
+
|
|
925
|
+
# Injective: two records never share a fake id, whatever the digest says.
|
|
926
|
+
# The learning pass only collects the real ids; mapping waits for the scrub pass, so a fake is never a real
|
|
927
|
+
# id, its own or another record's.
|
|
928
|
+
def id(value)
|
|
929
|
+
@originals << value
|
|
930
|
+
return @real_ids.add(value) && value if @learning
|
|
931
|
+
|
|
932
|
+
@ids[value] ||= (0..).lazy.map { fake_id(value, it) }.find { !@real_ids.include?(it) && @taken.add?(it) }
|
|
933
|
+
end
|
|
934
|
+
|
|
935
|
+
def real_id?(value) = @real_ids.include?(value)
|
|
936
|
+
|
|
937
|
+
# An Integer id gets the mapping of its string form and stays an Integer.
|
|
938
|
+
def integer_id(value) = id(value.to_s).then { it.match?(INTEGER_ID) ? it.to_i : it }
|
|
939
|
+
|
|
940
|
+
def fake_id(value, attempt)
|
|
941
|
+
seed = "#{value}#{attempt}"
|
|
942
|
+
return uuid(seed) if value.match?(UUID)
|
|
943
|
+
# One more digit every so many collisions, so a crowded space (ids 1..9999) still yields a fresh fake.
|
|
944
|
+
if value.match?(INTEGER_ID)
|
|
945
|
+
sign, number = value.start_with?("-") ? ["-", value[1..]] : ["", value]
|
|
946
|
+
return "#{sign}#{1 + digest("id", seed).unpack1("N") % 9}#{digits("id", seed, [number.size, MIN_LENGTH].max - 1 + attempt / ID_RETRIES)}"
|
|
947
|
+
end
|
|
948
|
+
|
|
949
|
+
slug(seed)
|
|
950
|
+
end
|
|
951
|
+
|
|
952
|
+
def path(value)
|
|
953
|
+
return value if value.empty?
|
|
954
|
+
return contact(value, "mailto:user@example.com") if value.start_with?("mailto:")
|
|
955
|
+
return contact(value, "tel:0000000000") if value.start_with?("tel:")
|
|
956
|
+
return "data:," if value.start_with?("data:")
|
|
957
|
+
return fake(value) if value.match?(%r{\A([a-z][a-z0-9+.-]*:)?//}i)
|
|
958
|
+
|
|
959
|
+
before_fragment, fragment = value.split("#", 2)
|
|
960
|
+
route, query = before_fragment.split("?", 2)
|
|
961
|
+
route = route.to_s.split("/", -1).map { segment(it) }.join("/")
|
|
962
|
+
[route, (query.split("&", -1).map { parameter(it) }.join("&") if query)].compact.join("?") + (fragment ? "##{html_id(CGI.unescape(fragment)) unless fragment.empty?}" : "")
|
|
963
|
+
end
|
|
964
|
+
|
|
965
|
+
# The address or number, and any query values (a subject, a body), are learned before the constant stands in.
|
|
966
|
+
def contact(value, replacement)
|
|
967
|
+
target, query = value.split(":", 2).last.to_s.split("?", 2)
|
|
968
|
+
# In the address or number a + is literal (+15550123456); only query values are form-encoded.
|
|
969
|
+
[decode_target(target.to_s), *query.to_s.split("&").map { CGI.unescape(it.split("=", 2)[1].to_s) }].each { remember(it) }
|
|
970
|
+
replacement
|
|
971
|
+
end
|
|
972
|
+
|
|
973
|
+
# Malformed escapes are learned as written rather than stopping the recording.
|
|
974
|
+
def decode_target(target)
|
|
975
|
+
URI.decode_uri_component(target)
|
|
976
|
+
rescue ArgumentError
|
|
977
|
+
target
|
|
978
|
+
end
|
|
979
|
+
|
|
980
|
+
def segment(value)
|
|
981
|
+
plain = CGI.unescape(value)
|
|
982
|
+
return ERB::Util.url_encode(id(plain)) if real_id?(plain) || id_like?(plain)
|
|
983
|
+
return value if plain.empty? || PLACEHOLDERS.include?(plain) || %w[. ..].include?(plain) || @routes.include?(plain)
|
|
984
|
+
|
|
985
|
+
remember(plain)
|
|
986
|
+
stem, dot, extension = plain.rpartition(".")
|
|
987
|
+
return slug(plain) if dot.empty? || !FILE_EXTENSIONS.include?(extension.downcase)
|
|
988
|
+
|
|
989
|
+
remember(stem)
|
|
990
|
+
"#{slug(stem)}.#{extension.downcase}"
|
|
991
|
+
end
|
|
992
|
+
|
|
993
|
+
def parameter(pair)
|
|
994
|
+
return pair if pair.empty?
|
|
995
|
+
|
|
996
|
+
name, value = pair.split("=", 2)
|
|
997
|
+
key, plain = CGI.unescape(name), CGI.unescape(value.to_s)
|
|
998
|
+
unless structural_name?(key)
|
|
999
|
+
fake = query_name(key)
|
|
1000
|
+
return value ? "#{fake}=#{CGI.escape(data(plain)) unless plain.empty?}" : fake
|
|
1001
|
+
end
|
|
1002
|
+
return pair if plain.empty? || structural_query?(key, plain)
|
|
1003
|
+
return "#{name}=#{ERB::Util.url_encode(id(plain))}" if key == "id"
|
|
1004
|
+
|
|
1005
|
+
# A minted token differs on every run; its fake must not.
|
|
1006
|
+
remember(plain) if name == "token"
|
|
1007
|
+
"#{name}=#{(name == "token") ? "token" : CGI.escape(data(plain))}"
|
|
1008
|
+
end
|
|
1009
|
+
|
|
1010
|
+
def structural_name?(key) = QUERY_NAMES.include?(key[QUERY_NAME, :base])
|
|
1011
|
+
|
|
1012
|
+
def query_name(key)
|
|
1013
|
+
remember(key)
|
|
1014
|
+
CGI.escape(slug(key))
|
|
1015
|
+
end
|
|
1016
|
+
|
|
1017
|
+
def structural_query?(key, value)
|
|
1018
|
+
return value.match?(DIGITS) if NUMERIC_QUERY_KEYS.include?(key)
|
|
1019
|
+
return QUERY_CONTEXTS.include?(value) if key == "context"
|
|
1020
|
+
|
|
1021
|
+
@known[key].include?(value)
|
|
1022
|
+
end
|
|
1023
|
+
|
|
1024
|
+
# A number field's interval as rationals on its step (or its finest decimal), around zero at the bounds' size.
|
|
1025
|
+
NumberAxis = Struct.new(:unit, :base, :span, :decimals) do
|
|
1026
|
+
def self.for(html, value)
|
|
1027
|
+
step = html["step"] if html["step"].is_a?(Numeric) || html["step"].to_s.match?(/\A\d+(?:\.\d+)?\z/)
|
|
1028
|
+
numbers = [html["min"], html["max"], value].select { Scrubber.numeric?(it) }
|
|
1029
|
+
decimals = [*numbers, step].compact.map { it.to_s[/\.(\d+)/, 1].to_s.size }.max
|
|
1030
|
+
unit = (step && Rational(step.to_s).positive?) ? Rational(step.to_s) : Rational(1, 10**decimals)
|
|
1031
|
+
size = ([*numbers.map { Rational(it.to_s).abs }, 1].max / unit).ceil * unit
|
|
1032
|
+
negative = numbers.any? { Rational(it.to_s).negative? }
|
|
1033
|
+
new(unit, negative ? -size : 0, negative ? 2 * size : size, decimals)
|
|
1034
|
+
end
|
|
1035
|
+
|
|
1036
|
+
def at(value) = (Rational(value.to_s) if Scrubber.numeric?(value))
|
|
1037
|
+
|
|
1038
|
+
def format(original, at)
|
|
1039
|
+
return at.to_i if original.is_a?(Integer) && at.denominator == 1
|
|
1040
|
+
return (decimals.zero? ? at.to_i.to_s : Kernel.format("%.#{decimals}f", at)) if original.is_a?(String)
|
|
1041
|
+
|
|
1042
|
+
at.to_f.round(decimals)
|
|
1043
|
+
end
|
|
1044
|
+
end
|
|
1045
|
+
|
|
1046
|
+
# A date or datetime field's interval in seconds: whole days for dates, minutes for a clock without seconds.
|
|
1047
|
+
TimeAxis = Struct.new(:unit, :base, :span) do
|
|
1048
|
+
def self.for(html, value)
|
|
1049
|
+
times = [html["min"], html["max"], value].grep(String).filter_map { TIME.match(it) }
|
|
1050
|
+
clocks = times.filter_map { it[3] }
|
|
1051
|
+
unit = if clocks.size < times.size then 86_400
|
|
1052
|
+
elsif clocks.all? { it.count(":") == 1 } then 60
|
|
1053
|
+
else 1
|
|
1054
|
+
end
|
|
1055
|
+
new(unit, Time.utc(2000).to_i, 25 * 365 * 86_400)
|
|
1056
|
+
end
|
|
1057
|
+
|
|
1058
|
+
def at(value)
|
|
1059
|
+
_, date, _, clock = *TIME.match(value.to_s)
|
|
1060
|
+
Time.utc(*date.split("-").map(&:to_i), *clock.to_s.split(":").map(&:to_i)).to_i if date
|
|
1061
|
+
rescue ArgumentError
|
|
1062
|
+
nil
|
|
1063
|
+
end
|
|
1064
|
+
|
|
1065
|
+
def format(original, at)
|
|
1066
|
+
_, _, separator, clock, fraction, zone = *TIME.match(original)
|
|
1067
|
+
time = Time.at(at).utc
|
|
1068
|
+
return time.strftime("%F") unless clock
|
|
1069
|
+
|
|
1070
|
+
"#{time.strftime("%F")}#{separator}#{time.strftime((clock.count(":") == 1) ? "%H:%M" : "%T")}#{fraction&.then { "." + "0" * (it.size - 1) }}#{zone}"
|
|
1071
|
+
end
|
|
1072
|
+
end
|
|
1073
|
+
end
|
|
1074
|
+
end
|
|
1075
|
+
end
|