nitro_kit 2.0.0.beta.3 → 2.0.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 +4 -4
- data/CHANGELOG.md +37 -0
- data/LICENSE +21 -28
- data/README.md +10 -9
- data/app/assets/stylesheets/nitro_kit.css +1660 -1401
- data/app/components/nitro_kit/accordion.rb +1 -13
- data/app/components/nitro_kit/app_navigation.rb +0 -12
- data/app/components/nitro_kit/app_shell.rb +5 -11
- data/app/components/nitro_kit/appearance_picker.rb +1 -7
- data/app/components/nitro_kit/combobox.rb +366 -21
- data/app/components/nitro_kit/command_palette.rb +7 -20
- data/app/components/nitro_kit/details_table.rb +5 -11
- data/app/components/nitro_kit/dropzone.rb +3 -15
- data/app/components/nitro_kit/field.rb +20 -2
- data/app/components/nitro_kit/pagination.rb +2 -8
- data/app/components/nitro_kit/settings_layout.rb +3 -9
- data/app/javascript/controllers/nk/app_shell_controller.js +2 -2
- data/app/javascript/controllers/nk/combobox_controller.js +308 -22
- data/app/javascript/controllers/nk/combobox_remote.js +70 -0
- data/app/javascript/controllers/nk/dropzone_controller.js +2 -2
- data/app/javascript/controllers/nk/tabs_controller.js +2 -2
- data/config/locales/en.yml +3 -0
- data/docs/agent_guide.md +43 -13
- data/docs/component_contracts.md +90 -61
- data/docs/customization.md +173 -35
- data/docs/initialization_prompt.md +4 -5
- data/docs/migration_1_to_2.md +10 -8
- data/docs/patterns/application_foundation.md +5 -0
- data/docs/patterns/composition.md +61 -0
- data/docs/patterns/crud_resource.md +5 -0
- data/docs/patterns/inset_workspace.md +21 -22
- data/docs/patterns/resource_form.md +65 -0
- data/docs/rails_integration.md +214 -2
- data/lib/generators/nitro_kit/install_generator.rb +5 -4
- data/lib/nitro_kit/installation.rb +327 -68
- data/lib/nitro_kit/migration_inventory.rb +18 -3
- data/lib/nitro_kit/version.rb +1 -1
- data/lib/rails/commands/nitro_kit/nitro_kit_command.rb +2 -2
- data/patterns/README.md +21 -0
- data/patterns/account-settings/files/app/components/ui/account_settings/account_deletion.rb +72 -0
- data/patterns/account-settings/files/app/components/ui/account_settings/base.rb +44 -0
- data/patterns/account-settings/files/app/components/ui/account_settings/page.rb +57 -0
- data/patterns/account-settings/files/app/components/ui/account_settings/preferences.rb +68 -0
- data/patterns/account-settings/files/app/components/ui/account_settings/profile.rb +86 -0
- data/patterns/account-settings/files/app/components/ui/account_settings/security.rb +69 -0
- data/patterns/account-settings/files/app/components/ui/account_settings/sessions.rb +86 -0
- data/patterns/account-settings/files/app/controllers/settings/account_deletions_controller.rb +35 -0
- data/patterns/account-settings/files/app/controllers/settings/base_controller.rb +12 -0
- data/patterns/account-settings/files/app/controllers/settings/other_sessions_controller.rb +8 -0
- data/patterns/account-settings/files/app/controllers/settings/preferences_controller.rb +27 -0
- data/patterns/account-settings/files/app/controllers/settings/profiles_controller.rb +20 -0
- data/patterns/account-settings/files/app/controllers/settings/securities_controller.rb +37 -0
- data/patterns/account-settings/files/app/controllers/settings/sessions_controller.rb +31 -0
- data/patterns/account-settings/files/app/models/account_settings/account_closure.rb +33 -0
- data/patterns/account-settings/files/app/models/account_settings/password_change.rb +42 -0
- data/patterns/account-settings/files/app/models/account_settings/session_summary.rb +45 -0
- data/patterns/account-settings/files/app/models/concerns/user/account_configurable.rb +18 -0
- data/patterns/account-settings/files/config/routes/account_settings.rb +10 -0
- data/patterns/account-settings/files/db/migrate/20260731020000_add_account_settings.rb +27 -0
- data/patterns/account-settings/files/test/controllers/settings/account_settings_flow_test.rb +84 -0
- data/patterns/account-settings/files/test/models/account_settings/password_change_test.rb +40 -0
- data/patterns/account-settings/files/test/system/account_settings_test.rb +52 -0
- data/patterns/account-settings/item.yml +4 -0
- data/patterns/account-settings/pattern.md +44 -0
- data/patterns/application-shell/files/app/assets/stylesheets/ui/application_shell.css +267 -0
- data/patterns/application-shell/files/app/components/ui/application_shell.rb +184 -0
- data/patterns/application-shell/item.yml +4 -0
- data/patterns/application-shell/pattern.md +110 -0
- data/patterns/authentication/files/app/components/ui/authentication/base.rb +11 -0
- data/patterns/authentication/files/app/components/ui/authentication/page.rb +53 -0
- data/patterns/authentication/files/app/components/ui/email_verifications/show.rb +62 -0
- data/patterns/authentication/files/app/components/ui/password_resets/new.rb +65 -0
- data/patterns/authentication/files/app/components/ui/passwords/edit.rb +72 -0
- data/patterns/authentication/files/app/components/ui/sessions/new.rb +58 -0
- data/patterns/authentication/files/app/components/ui/verification_emails/new.rb +59 -0
- data/patterns/authentication/files/app/controllers/concerns/authentication.rb +66 -0
- data/patterns/authentication/files/app/controllers/concerns/safe_return_navigation.rb +37 -0
- data/patterns/authentication/files/app/controllers/email_verifications_controller.rb +39 -0
- data/patterns/authentication/files/app/controllers/password_resets_controller.rb +33 -0
- data/patterns/authentication/files/app/controllers/passwords_controller.rb +51 -0
- data/patterns/authentication/files/app/controllers/sessions_controller.rb +42 -0
- data/patterns/authentication/files/app/controllers/verification_emails_controller.rb +33 -0
- data/patterns/authentication/files/app/jobs/authentication_delivery_job.rb +17 -0
- data/patterns/authentication/files/app/mailers/authentication_mailer.rb +15 -0
- data/patterns/authentication/files/app/models/authentication/credentials.rb +16 -0
- data/patterns/authentication/files/app/models/authentication/email_request.rb +17 -0
- data/patterns/authentication/files/app/models/concerns/user/email_verifiable.rb +40 -0
- data/patterns/authentication/files/app/models/session.rb +5 -0
- data/patterns/authentication/files/app/views/authentication_mailer/email_verification.html.erb +3 -0
- data/patterns/authentication/files/app/views/authentication_mailer/email_verification.text.erb +5 -0
- data/patterns/authentication/files/app/views/authentication_mailer/password_reset.html.erb +3 -0
- data/patterns/authentication/files/app/views/authentication_mailer/password_reset.text.erb +5 -0
- data/patterns/authentication/files/config/routes/authentication.rb +11 -0
- data/patterns/authentication/files/db/migrate/20260731000000_add_authentication_and_recovery.rb +13 -0
- data/patterns/authentication/files/test/controllers/authentication_flow_test.rb +103 -0
- data/patterns/authentication/files/test/controllers/safe_return_navigation_test.rb +18 -0
- data/patterns/authentication/files/test/mailers/authentication_mailer_test.rb +21 -0
- data/patterns/authentication/files/test/models/user_email_verifiable_test.rb +36 -0
- data/patterns/authentication/files/test/system/authentication_flow_test.rb +23 -0
- data/patterns/authentication/item.yml +12 -0
- data/patterns/authentication/pattern.md +54 -0
- data/patterns/crud-resource/files/app/assets/stylesheets/ui/projects.css +4 -0
- data/patterns/crud-resource/files/app/components/ui/projects/form.rb +89 -0
- data/patterns/crud-resource/files/app/components/ui/projects/index.rb +161 -0
- data/patterns/crud-resource/files/app/components/ui/projects/show.rb +84 -0
- data/patterns/crud-resource/files/app/controllers/projects/archivals_controller.rb +23 -0
- data/patterns/crud-resource/files/app/controllers/projects_controller.rb +70 -0
- data/patterns/crud-resource/files/app/models/project/archival.rb +8 -0
- data/patterns/crud-resource/files/app/models/project/event.rb +12 -0
- data/patterns/crud-resource/files/app/models/project/filter.rb +41 -0
- data/patterns/crud-resource/files/app/models/project.rb +41 -0
- data/patterns/crud-resource/files/app/views/projects/edit.html.erb +19 -0
- data/patterns/crud-resource/files/app/views/projects/index.html.erb +18 -0
- data/patterns/crud-resource/files/app/views/projects/new.html.erb +16 -0
- data/patterns/crud-resource/files/app/views/projects/show.html.erb +28 -0
- data/patterns/crud-resource/files/config/routes/crud_resource.rb +7 -0
- data/patterns/crud-resource/files/db/migrate/20260720000000_create_crud_resource.rb +37 -0
- data/patterns/crud-resource/files/test/controllers/projects_controller_test.rb +64 -0
- data/patterns/crud-resource/files/test/models/project_test.rb +34 -0
- data/patterns/crud-resource/files/test/support/crud_resource_test_helpers.rb +25 -0
- data/patterns/crud-resource/files/test/system/projects_crud_test.rb +87 -0
- data/patterns/crud-resource/item.yml +6 -0
- data/patterns/crud-resource/pattern.md +48 -0
- data/patterns/file-uploads/files/app/assets/stylesheets/ui/uploads.css +133 -0
- data/patterns/file-uploads/files/app/components/ui/uploads/base.rb +30 -0
- data/patterns/file-uploads/files/app/components/ui/uploads/index.rb +188 -0
- data/patterns/file-uploads/files/app/components/ui/uploads/page.rb +33 -0
- data/patterns/file-uploads/files/app/controllers/uploads/base_controller.rb +29 -0
- data/patterns/file-uploads/files/app/controllers/uploads/direct_uploads_controller.rb +55 -0
- data/patterns/file-uploads/files/app/controllers/uploads/retries_controller.rb +14 -0
- data/patterns/file-uploads/files/app/controllers/uploads_controller.rb +40 -0
- data/patterns/file-uploads/files/app/jobs/upload_blob_cleanup_job.rb +9 -0
- data/patterns/file-uploads/files/app/jobs/upload_processing_job.rb +10 -0
- data/patterns/file-uploads/files/app/jobs/upload_repair_job.rb +17 -0
- data/patterns/file-uploads/files/app/models/concerns/team/uploadable.rb +9 -0
- data/patterns/file-uploads/files/app/models/upload.rb +295 -0
- data/patterns/file-uploads/files/config/recurring/uploads.yml +7 -0
- data/patterns/file-uploads/files/config/routes/uploads.rb +5 -0
- data/patterns/file-uploads/files/db/migrate/20260802000000_add_file_uploads.rb +30 -0
- data/patterns/file-uploads/files/test/controllers/uploads_flow_test.rb +77 -0
- data/patterns/file-uploads/files/test/fixtures/files/workspace.png.base64 +1 -0
- data/patterns/file-uploads/files/test/fixtures/uploads.yml +17 -0
- data/patterns/file-uploads/files/test/models/upload_test.rb +55 -0
- data/patterns/file-uploads/files/test/system/file_uploads_test.rb +30 -0
- data/patterns/file-uploads/item.yml +4 -0
- data/patterns/file-uploads/pattern.md +71 -0
- data/patterns/nitro-kit-1-upgrade/files/docs/nitro_kit_1_upgrade.md +57 -0
- data/patterns/nitro-kit-1-upgrade/item.yml +4 -0
- data/patterns/nitro-kit-1-upgrade/pattern.md +30 -0
- data/patterns/onboarding/files/app/assets/stylesheets/ui/onboarding.css +55 -0
- data/patterns/onboarding/files/app/components/ui/onboarding/base.rb +11 -0
- data/patterns/onboarding/files/app/components/ui/onboarding/branch_form.rb +36 -0
- data/patterns/onboarding/files/app/components/ui/onboarding/details_form.rb +67 -0
- data/patterns/onboarding/files/app/components/ui/onboarding/invitation_plan_form.rb +48 -0
- data/patterns/onboarding/files/app/components/ui/onboarding/page.rb +75 -0
- data/patterns/onboarding/files/app/components/ui/onboarding/review.rb +51 -0
- data/patterns/onboarding/files/app/components/ui/onboarding/show.rb +32 -0
- data/patterns/onboarding/files/app/controllers/concerns/requires_finished_onboarding.rb +14 -0
- data/patterns/onboarding/files/app/controllers/onboarding/base_controller.rb +30 -0
- data/patterns/onboarding/files/app/controllers/onboarding/branches_controller.rb +16 -0
- data/patterns/onboarding/files/app/controllers/onboarding/completions_controller.rb +13 -0
- data/patterns/onboarding/files/app/controllers/onboarding/details_controller.rb +16 -0
- data/patterns/onboarding/files/app/controllers/onboarding/invitation_plans_controller.rb +20 -0
- data/patterns/onboarding/files/app/controllers/onboardings_controller.rb +7 -0
- data/patterns/onboarding/files/app/jobs/onboarding_invitation_delivery_job.rb +15 -0
- data/patterns/onboarding/files/app/models/concerns/team/onboardable.rb +23 -0
- data/patterns/onboarding/files/app/models/onboarding/completion.rb +17 -0
- data/patterns/onboarding/files/app/models/onboarding/invitation_delivery.rb +14 -0
- data/patterns/onboarding/files/app/models/onboarding.rb +174 -0
- data/patterns/onboarding/files/config/routes/onboarding.rb +11 -0
- data/patterns/onboarding/files/db/migrate/20260731010000_create_onboarding.rb +60 -0
- data/patterns/onboarding/files/test/controllers/onboarding_flow_test.rb +62 -0
- data/patterns/onboarding/files/test/fixtures/onboarding_completions.yml +3 -0
- data/patterns/onboarding/files/test/fixtures/onboardings.yml +25 -0
- data/patterns/onboarding/files/test/jobs/onboarding_invitation_delivery_job_test.rb +18 -0
- data/patterns/onboarding/files/test/models/onboarding_test.rb +60 -0
- data/patterns/onboarding/files/test/system/onboarding_flow_test.rb +39 -0
- data/patterns/onboarding/item.yml +4 -0
- data/patterns/onboarding/pattern.md +53 -0
- data/patterns/subscription-billing/files/app/components/ui/billing/base.rb +39 -0
- data/patterns/subscription-billing/files/app/components/ui/billing/checkout_result.rb +64 -0
- data/patterns/subscription-billing/files/app/components/ui/billing/page.rb +33 -0
- data/patterns/subscription-billing/files/app/components/ui/billing/show.rb +149 -0
- data/patterns/subscription-billing/files/app/controllers/billing/base_controller.rb +16 -0
- data/patterns/subscription-billing/files/app/controllers/billing/checkouts_controller.rb +65 -0
- data/patterns/subscription-billing/files/app/controllers/billing/portals_controller.rb +18 -0
- data/patterns/subscription-billing/files/app/controllers/billing_controller.rb +17 -0
- data/patterns/subscription-billing/files/app/controllers/webhooks/stripe_events_controller.rb +21 -0
- data/patterns/subscription-billing/files/app/jobs/billing/stripe_event_job.rb +16 -0
- data/patterns/subscription-billing/files/app/jobs/billing/subscription_reconciliation_job.rb +16 -0
- data/patterns/subscription-billing/files/app/jobs/billing/subscription_reconciliation_sweep_job.rb +9 -0
- data/patterns/subscription-billing/files/app/models/billing/checkout.rb +58 -0
- data/patterns/subscription-billing/files/app/models/billing/plan.rb +19 -0
- data/patterns/subscription-billing/files/app/models/billing/stripe_event.rb +73 -0
- data/patterns/subscription-billing/files/app/models/billing/subscription.rb +106 -0
- data/patterns/subscription-billing/files/app/models/concerns/team/billable.rb +36 -0
- data/patterns/subscription-billing/files/config/initializers/stripe.rb +29 -0
- data/patterns/subscription-billing/files/config/routes/subscription_billing.rb +10 -0
- data/patterns/subscription-billing/files/db/migrate/20260801000000_add_subscription_billing.rb +61 -0
- data/patterns/subscription-billing/files/test/controllers/billing_flow_test.rb +60 -0
- data/patterns/subscription-billing/files/test/models/billing/subscription_test.rb +40 -0
- data/patterns/subscription-billing/files/test/system/subscription_billing_test.rb +28 -0
- data/patterns/subscription-billing/item.yml +4 -0
- data/patterns/subscription-billing/pattern.md +59 -0
- data/patterns/team-management/files/app/components/ui/invitations/acceptance.rb +33 -0
- data/patterns/team-management/files/app/components/ui/teams/index.rb +87 -0
- data/patterns/team-management/files/app/components/ui/teams/invitation_form.rb +51 -0
- data/patterns/team-management/files/app/components/ui/teams/member_form.rb +78 -0
- data/patterns/team-management/files/app/components/ui/teams/members.rb +125 -0
- data/patterns/team-management/files/app/controllers/invitations/acceptances_controller.rb +29 -0
- data/patterns/team-management/files/app/controllers/teams/base_controller.rb +18 -0
- data/patterns/team-management/files/app/controllers/teams/invitations_controller.rb +42 -0
- data/patterns/team-management/files/app/controllers/teams/memberships_controller.rb +53 -0
- data/patterns/team-management/files/app/controllers/teams_controller.rb +30 -0
- data/patterns/team-management/files/app/mailers/team_invitation_mailer.rb +10 -0
- data/patterns/team-management/files/app/models/concerns/current_team.rb +13 -0
- data/patterns/team-management/files/app/models/concerns/user_teams.rb +37 -0
- data/patterns/team-management/files/app/models/invitation.rb +41 -0
- data/patterns/team-management/files/app/models/membership.rb +45 -0
- data/patterns/team-management/files/app/models/team.rb +25 -0
- data/patterns/team-management/files/app/views/invitations/acceptances/show.html.erb +1 -0
- data/patterns/team-management/files/app/views/team_invitation_mailer/invite.html.erb +10 -0
- data/patterns/team-management/files/app/views/team_invitation_mailer/invite.text.erb +7 -0
- data/patterns/team-management/files/app/views/teams/index.html.erb +15 -0
- data/patterns/team-management/files/app/views/teams/invitations/new.html.erb +22 -0
- data/patterns/team-management/files/app/views/teams/memberships/edit.html.erb +22 -0
- data/patterns/team-management/files/app/views/teams/memberships/index.html.erb +20 -0
- data/patterns/team-management/files/config/routes/team_management.rb +11 -0
- data/patterns/team-management/files/db/migrate/20260717000000_create_team_management.rb +31 -0
- data/patterns/team-management/files/test/models/invitation_test.rb +28 -0
- data/patterns/team-management/files/test/models/membership_test.rb +29 -0
- data/patterns/team-management/files/test/models/team_test.rb +31 -0
- data/patterns/team-management/files/test/support/team_management_test_helpers.rb +22 -0
- data/patterns/team-management/item.yml +4 -0
- data/patterns/team-management/pattern.md +47 -0
- data/patterns/webhook-deliveries/files/app/assets/stylesheets/ui/webhooks.css +179 -0
- data/patterns/webhook-deliveries/files/app/components/ui/webhooks/base.rb +38 -0
- data/patterns/webhook-deliveries/files/app/components/ui/webhooks/delivery.rb +160 -0
- data/patterns/webhook-deliveries/files/app/components/ui/webhooks/endpoint.rb +149 -0
- data/patterns/webhook-deliveries/files/app/components/ui/webhooks/form.rb +75 -0
- data/patterns/webhook-deliveries/files/app/components/ui/webhooks/index.rb +142 -0
- data/patterns/webhook-deliveries/files/app/components/ui/webhooks/page.rb +33 -0
- data/patterns/webhook-deliveries/files/app/components/ui/webhooks/secret.rb +38 -0
- data/patterns/webhook-deliveries/files/app/controllers/webhook_deliveries/replays_controller.rb +13 -0
- data/patterns/webhook-deliveries/files/app/controllers/webhook_deliveries_controller.rb +7 -0
- data/patterns/webhook-deliveries/files/app/controllers/webhook_endpoints/secrets_controller.rb +41 -0
- data/patterns/webhook-deliveries/files/app/controllers/webhook_endpoints_controller.rb +67 -0
- data/patterns/webhook-deliveries/files/app/controllers/webhooks/base_controller.rb +15 -0
- data/patterns/webhook-deliveries/files/app/jobs/webhook_delivery_job.rb +9 -0
- data/patterns/webhook-deliveries/files/app/jobs/webhook_delivery_repair_job.rb +11 -0
- data/patterns/webhook-deliveries/files/app/models/concerns/team/webhookable.rb +11 -0
- data/patterns/webhook-deliveries/files/app/models/webhook/attempt.rb +30 -0
- data/patterns/webhook-deliveries/files/app/models/webhook/delivery.rb +268 -0
- data/patterns/webhook-deliveries/files/app/models/webhook/endpoint.rb +58 -0
- data/patterns/webhook-deliveries/files/app/models/webhook/event.rb +98 -0
- data/patterns/webhook-deliveries/files/app/models/webhook/secret_reveal.rb +53 -0
- data/patterns/webhook-deliveries/files/app/models/webhook/signature.rb +22 -0
- data/patterns/webhook-deliveries/files/app/models/webhook/transport.rb +176 -0
- data/patterns/webhook-deliveries/files/app/views/webhook_deliveries/show.html.erb +5 -0
- data/patterns/webhook-deliveries/files/app/views/webhook_endpoints/edit.html.erb +1 -0
- data/patterns/webhook-deliveries/files/app/views/webhook_endpoints/index.html.erb +6 -0
- data/patterns/webhook-deliveries/files/app/views/webhook_endpoints/new.html.erb +1 -0
- data/patterns/webhook-deliveries/files/app/views/webhook_endpoints/secrets/new.html.erb +1 -0
- data/patterns/webhook-deliveries/files/app/views/webhook_endpoints/show.html.erb +7 -0
- data/patterns/webhook-deliveries/files/config/recurring/webhooks.yml +4 -0
- data/patterns/webhook-deliveries/files/config/routes/webhook_deliveries.rb +10 -0
- data/patterns/webhook-deliveries/files/db/migrate/20260803000000_add_webhook_deliveries.rb +126 -0
- data/patterns/webhook-deliveries/files/test/controllers/webhook_endpoints_flow_test.rb +48 -0
- data/patterns/webhook-deliveries/files/test/models/webhook/delivery_test.rb +70 -0
- data/patterns/webhook-deliveries/files/test/system/webhook_deliveries_test.rb +29 -0
- data/patterns/webhook-deliveries/item.yml +4 -0
- data/patterns/webhook-deliveries/pattern.md +120 -0
- data/plugins/nitro-kit/.codex-plugin/plugin.json +4 -4
- data/plugins/nitro-kit/skills/nitro-kit-hotwire/SKILL.md +7 -9
- data/plugins/nitro-kit/skills/nitro-kit-hotwire/agents/openai.yaml +1 -1
- data/plugins/nitro-kit/skills/nitro-kit-rails/SKILL.md +10 -9
- data/plugins/nitro-kit/skills/nitro-kit-rails/agents/openai.yaml +1 -1
- data/plugins/nitro-kit/skills/nitro-kit-ui/SKILL.md +27 -16
- data/plugins/nitro-kit/skills/nitro-kit-ui/agents/openai.yaml +1 -1
- data/src/stylesheets/nitro_kit/components/accordion.css +40 -40
- data/src/stylesheets/nitro_kit/components/alert.css +7 -6
- data/src/stylesheets/nitro_kit/components/app_navigation.css +50 -41
- data/src/stylesheets/nitro_kit/components/app_shell.css +371 -280
- data/src/stylesheets/nitro_kit/components/appearance_picker.css +38 -38
- data/src/stylesheets/nitro_kit/components/avatar.css +3 -3
- data/src/stylesheets/nitro_kit/components/avatar_stack.css +18 -18
- data/src/stylesheets/nitro_kit/components/badge.css +5 -2
- data/src/stylesheets/nitro_kit/components/button.css +57 -49
- data/src/stylesheets/nitro_kit/components/button_group.css +14 -14
- data/src/stylesheets/nitro_kit/components/checkbox.css +24 -24
- data/src/stylesheets/nitro_kit/components/checkbox_group.css +13 -13
- data/src/stylesheets/nitro_kit/components/combobox.css +152 -25
- data/src/stylesheets/nitro_kit/components/command_palette.css +59 -59
- data/src/stylesheets/nitro_kit/components/control_group.css +71 -54
- data/src/stylesheets/nitro_kit/components/details_table.css +9 -9
- data/src/stylesheets/nitro_kit/components/dialog.css +8 -12
- data/src/stylesheets/nitro_kit/components/dropdown.css +22 -26
- data/src/stylesheets/nitro_kit/components/dropzone.css +97 -100
- data/src/stylesheets/nitro_kit/components/empty_state.css +1 -0
- data/src/stylesheets/nitro_kit/components/field.css +26 -30
- data/src/stylesheets/nitro_kit/components/fieldset.css +2 -2
- data/src/stylesheets/nitro_kit/components/input.css +6 -14
- data/src/stylesheets/nitro_kit/components/layout.css +165 -165
- data/src/stylesheets/nitro_kit/components/page_header.css +3 -3
- data/src/stylesheets/nitro_kit/components/pagination.css +66 -49
- data/src/stylesheets/nitro_kit/components/pagination_bar.css +6 -6
- data/src/stylesheets/nitro_kit/components/palette.css +24 -22
- data/src/stylesheets/nitro_kit/components/progressive_image.css +38 -41
- data/src/stylesheets/nitro_kit/components/radio_button.css +19 -19
- data/src/stylesheets/nitro_kit/components/radio_button_group.css +43 -43
- data/src/stylesheets/nitro_kit/components/rich_text_area.css +4 -4
- data/src/stylesheets/nitro_kit/components/select.css +2 -2
- data/src/stylesheets/nitro_kit/components/settings_layout.css +17 -18
- data/src/stylesheets/nitro_kit/components/settings_section.css +12 -12
- data/src/stylesheets/nitro_kit/components/sheet.css +10 -10
- data/src/stylesheets/nitro_kit/components/stat_grid.css +9 -9
- data/src/stylesheets/nitro_kit/components/switch.css +23 -23
- data/src/stylesheets/nitro_kit/components/table.css +27 -27
- data/src/stylesheets/nitro_kit/components/tabs.css +32 -34
- data/src/stylesheets/nitro_kit/components/toolbar.css +6 -6
- data/src/stylesheets/nitro_kit/components/tooltip.css +26 -28
- data/src/stylesheets/nitro_kit/layers.css +2 -1
- data/src/stylesheets/nitro_kit/reset.css +9 -9
- data/src/stylesheets/nitro_kit/tailwind.css +6 -2
- data/src/stylesheets/nitro_kit/tokens.css +18 -9
- metadata +238 -2
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Webhook::Delivery < ApplicationRecord
|
|
4
|
+
self.table_name = "webhook_deliveries"
|
|
5
|
+
|
|
6
|
+
MAX_AUTOMATIC_ATTEMPTS = 6
|
|
7
|
+
MAX_MANUAL_ATTEMPTS = 10
|
|
8
|
+
DISPATCH_LEASE = 15.minutes
|
|
9
|
+
RETRY_DELAYS = [ 1.minute, 5.minutes, 30.minutes, 2.hours, 8.hours ].freeze
|
|
10
|
+
|
|
11
|
+
NotReplayable = Class.new(StandardError)
|
|
12
|
+
|
|
13
|
+
belongs_to :team, inverse_of: :webhook_deliveries
|
|
14
|
+
belongs_to :endpoint, class_name: "Webhook::Endpoint", inverse_of: :deliveries
|
|
15
|
+
belongs_to :event, class_name: "Webhook::Event", inverse_of: :deliveries
|
|
16
|
+
belongs_to :next_attempt_initiator, class_name: "User", optional: true
|
|
17
|
+
has_many :attempts,
|
|
18
|
+
-> { chronologically },
|
|
19
|
+
class_name: "Webhook::Attempt",
|
|
20
|
+
dependent: :destroy,
|
|
21
|
+
inverse_of: :delivery
|
|
22
|
+
|
|
23
|
+
enum :status, {
|
|
24
|
+
pending: "pending",
|
|
25
|
+
delivering: "delivering",
|
|
26
|
+
succeeded: "succeeded",
|
|
27
|
+
failed: "failed"
|
|
28
|
+
}, default: :pending, validate: true
|
|
29
|
+
enum :next_attempt_kind, { automatic: "automatic", manual: "manual" }, prefix: :next, validate: true
|
|
30
|
+
|
|
31
|
+
validates :next_attempt_at, presence: true
|
|
32
|
+
validates :event_id, uniqueness: { scope: :endpoint_id }
|
|
33
|
+
validates :processing_kind, inclusion: { in: %w[automatic manual] }, allow_nil: true
|
|
34
|
+
validate :ownership_is_consistent
|
|
35
|
+
validate :attempt_state_is_consistent
|
|
36
|
+
|
|
37
|
+
after_create_commit :dispatch_later
|
|
38
|
+
|
|
39
|
+
scope :recent_first, -> { order(created_at: :desc) }
|
|
40
|
+
scope :due, -> { pending.where(next_attempt_at: ..Time.current) }
|
|
41
|
+
scope :dispatchable, -> {
|
|
42
|
+
due.where(dispatched_at: nil).or(due.where(dispatched_at: ...DISPATCH_LEASE.ago))
|
|
43
|
+
}
|
|
44
|
+
scope :stale, -> { delivering.where(processing_started_at: ...5.minutes.ago) }
|
|
45
|
+
|
|
46
|
+
def deliver!
|
|
47
|
+
claim = claim_attempt
|
|
48
|
+
return unless claim
|
|
49
|
+
|
|
50
|
+
attempt, token = claim
|
|
51
|
+
timestamp = Time.current.to_i
|
|
52
|
+
headers = Webhook::Signature.headers(
|
|
53
|
+
event:,
|
|
54
|
+
secret: endpoint.signing_secret,
|
|
55
|
+
timestamp:,
|
|
56
|
+
request_id: attempt.request_id
|
|
57
|
+
)
|
|
58
|
+
result = Webhook::Transport.deliver(url: endpoint.url, body: event.body, headers:)
|
|
59
|
+
finish_attempt(attempt, token, result)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def replay!(initiator:)
|
|
63
|
+
with_lock do
|
|
64
|
+
unless (succeeded? || failed?) && endpoint.enabled? && manual_attempts_count < MAX_MANUAL_ATTEMPTS
|
|
65
|
+
raise NotReplayable, "This delivery cannot be replayed"
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
update!(
|
|
69
|
+
status: :pending,
|
|
70
|
+
next_attempt_kind: :manual,
|
|
71
|
+
next_attempt_initiator: initiator,
|
|
72
|
+
next_attempt_at: Time.current,
|
|
73
|
+
completed_at: nil,
|
|
74
|
+
last_response_status: nil,
|
|
75
|
+
last_error_code: nil,
|
|
76
|
+
last_error_message: nil
|
|
77
|
+
)
|
|
78
|
+
end
|
|
79
|
+
dispatch_later
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def replayable?
|
|
83
|
+
(succeeded? || failed?) && endpoint.enabled? && manual_attempts_count < MAX_MANUAL_ATTEMPTS
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def repair!
|
|
87
|
+
schedule = with_lock do
|
|
88
|
+
next false unless delivering? && processing_started_at && processing_started_at < 5.minutes.ago
|
|
89
|
+
|
|
90
|
+
abandoned = attempts.find_by(sequence: automatic_attempts_count + manual_attempts_count)
|
|
91
|
+
abandoned&.update!(
|
|
92
|
+
error_code: "worker_lost",
|
|
93
|
+
error_message: "The delivery worker stopped before recording a result",
|
|
94
|
+
finished_at: Time.current
|
|
95
|
+
) unless abandoned&.finished_at?
|
|
96
|
+
|
|
97
|
+
if processing_kind == "automatic" && automatic_attempts_count < MAX_AUTOMATIC_ATTEMPTS && endpoint.enabled?
|
|
98
|
+
update!(
|
|
99
|
+
status: :pending,
|
|
100
|
+
next_attempt_kind: :automatic,
|
|
101
|
+
next_attempt_at: Time.current,
|
|
102
|
+
processing_token: nil,
|
|
103
|
+
processing_kind: nil,
|
|
104
|
+
processing_started_at: nil,
|
|
105
|
+
last_error_code: "worker_lost",
|
|
106
|
+
last_error_message: "The delivery worker stopped before recording a result"
|
|
107
|
+
)
|
|
108
|
+
true
|
|
109
|
+
else
|
|
110
|
+
update!(
|
|
111
|
+
status: :failed,
|
|
112
|
+
processing_token: nil,
|
|
113
|
+
processing_kind: nil,
|
|
114
|
+
processing_started_at: nil,
|
|
115
|
+
completed_at: Time.current,
|
|
116
|
+
last_error_code: "worker_lost",
|
|
117
|
+
last_error_message: "The delivery worker stopped before recording a result"
|
|
118
|
+
)
|
|
119
|
+
false
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
dispatch_later if schedule
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def attempt_count
|
|
126
|
+
automatic_attempts_count + manual_attempts_count
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def dispatch_later(wait_until: nil)
|
|
130
|
+
leased = with_lock do
|
|
131
|
+
next false unless pending?
|
|
132
|
+
next false if dispatched_at && dispatched_at >= DISPATCH_LEASE.ago
|
|
133
|
+
|
|
134
|
+
update_column(:dispatched_at, Time.current)
|
|
135
|
+
true
|
|
136
|
+
end
|
|
137
|
+
return unless leased
|
|
138
|
+
|
|
139
|
+
job = WebhookDeliveryJob
|
|
140
|
+
job = job.set(wait_until:) if wait_until
|
|
141
|
+
job.perform_later(id)
|
|
142
|
+
rescue StandardError => error
|
|
143
|
+
update_column(:dispatched_at, nil) if persisted? && pending?
|
|
144
|
+
ActiveSupport.error_reporter.report(
|
|
145
|
+
error,
|
|
146
|
+
handled: true,
|
|
147
|
+
context: { webhook_delivery_id: id }
|
|
148
|
+
)
|
|
149
|
+
nil
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
private
|
|
153
|
+
def claim_attempt
|
|
154
|
+
with_lock do
|
|
155
|
+
next unless pending? && next_attempt_at <= Time.current
|
|
156
|
+
|
|
157
|
+
unless endpoint.enabled?
|
|
158
|
+
update!(
|
|
159
|
+
status: :failed,
|
|
160
|
+
completed_at: Time.current,
|
|
161
|
+
dispatched_at: nil,
|
|
162
|
+
next_attempt_initiator: nil,
|
|
163
|
+
last_error_code: "endpoint_disabled",
|
|
164
|
+
last_error_message: "The endpoint is disabled"
|
|
165
|
+
)
|
|
166
|
+
next
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
kind = next_attempt_kind
|
|
170
|
+
count_attribute = kind == "manual" ? :manual_attempts_count : :automatic_attempts_count
|
|
171
|
+
count = public_send(count_attribute) + 1
|
|
172
|
+
token = SecureRandom.uuid
|
|
173
|
+
attempt = attempts.create!(
|
|
174
|
+
sequence: attempt_count + 1,
|
|
175
|
+
trigger: kind,
|
|
176
|
+
request_id: "req_#{SecureRandom.uuid}",
|
|
177
|
+
request_url: endpoint.url,
|
|
178
|
+
initiated_by: next_attempt_initiator,
|
|
179
|
+
started_at: Time.current
|
|
180
|
+
)
|
|
181
|
+
update!(
|
|
182
|
+
status: :delivering,
|
|
183
|
+
processing_token: token,
|
|
184
|
+
processing_kind: kind,
|
|
185
|
+
processing_started_at: Time.current,
|
|
186
|
+
dispatched_at: nil,
|
|
187
|
+
next_attempt_initiator: nil,
|
|
188
|
+
count_attribute => count
|
|
189
|
+
)
|
|
190
|
+
[ attempt, token ]
|
|
191
|
+
end
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
def finish_attempt(attempt, token, result)
|
|
195
|
+
retry_at = nil
|
|
196
|
+
with_lock do
|
|
197
|
+
next unless delivering? && processing_token == token
|
|
198
|
+
|
|
199
|
+
attempt.finish!(result)
|
|
200
|
+
if result.success?
|
|
201
|
+
update!(
|
|
202
|
+
status: :succeeded,
|
|
203
|
+
completed_at: Time.current,
|
|
204
|
+
last_response_status: result.status,
|
|
205
|
+
last_error_code: nil,
|
|
206
|
+
last_error_message: nil,
|
|
207
|
+
processing_token: nil,
|
|
208
|
+
processing_kind: nil,
|
|
209
|
+
processing_started_at: nil
|
|
210
|
+
)
|
|
211
|
+
elsif automatic_retry?(result)
|
|
212
|
+
retry_at = Time.current + RETRY_DELAYS.fetch(automatic_attempts_count - 1)
|
|
213
|
+
update!(
|
|
214
|
+
status: :pending,
|
|
215
|
+
next_attempt_kind: :automatic,
|
|
216
|
+
next_attempt_at: retry_at,
|
|
217
|
+
last_response_status: result.status,
|
|
218
|
+
last_error_code: result.error_code,
|
|
219
|
+
last_error_message: result.error_message,
|
|
220
|
+
processing_token: nil,
|
|
221
|
+
processing_kind: nil,
|
|
222
|
+
processing_started_at: nil
|
|
223
|
+
)
|
|
224
|
+
else
|
|
225
|
+
update!(
|
|
226
|
+
status: :failed,
|
|
227
|
+
completed_at: Time.current,
|
|
228
|
+
last_response_status: result.status,
|
|
229
|
+
last_error_code: result.error_code,
|
|
230
|
+
last_error_message: result.error_message,
|
|
231
|
+
processing_token: nil,
|
|
232
|
+
processing_kind: nil,
|
|
233
|
+
processing_started_at: nil
|
|
234
|
+
)
|
|
235
|
+
end
|
|
236
|
+
end
|
|
237
|
+
dispatch_later(wait_until: retry_at) if retry_at
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
def automatic_retry?(result)
|
|
241
|
+
processing_kind == "automatic" && result.retryable? && automatic_attempts_count < MAX_AUTOMATIC_ATTEMPTS
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
def ownership_is_consistent
|
|
245
|
+
if endpoint && endpoint.team_id != team_id
|
|
246
|
+
errors.add(:endpoint, "must belong to the delivery team")
|
|
247
|
+
end
|
|
248
|
+
if event && event.team_id != team_id
|
|
249
|
+
errors.add(:event, "must belong to the delivery team")
|
|
250
|
+
end
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
def attempt_state_is_consistent
|
|
254
|
+
if delivering?
|
|
255
|
+
unless processing_token.present? && processing_kind.present? && processing_started_at.present?
|
|
256
|
+
errors.add(:status, "requires a complete processing claim")
|
|
257
|
+
end
|
|
258
|
+
elsif processing_token.present? || processing_kind.present? || processing_started_at.present?
|
|
259
|
+
errors.add(:status, "cannot retain a processing claim")
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
if pending? && next_manual? && next_attempt_initiator.nil?
|
|
263
|
+
errors.add(:next_attempt_initiator, "is required for a manual replay")
|
|
264
|
+
elsif pending? && next_automatic? && next_attempt_initiator.present?
|
|
265
|
+
errors.add(:next_attempt_initiator, "must be blank for an automatic attempt")
|
|
266
|
+
end
|
|
267
|
+
end
|
|
268
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "uri"
|
|
4
|
+
|
|
5
|
+
class Webhook::Endpoint < ApplicationRecord
|
|
6
|
+
self.table_name = "webhook_endpoints"
|
|
7
|
+
|
|
8
|
+
belongs_to :team, inverse_of: :webhook_endpoints
|
|
9
|
+
belongs_to :creator, class_name: "User", optional: true
|
|
10
|
+
has_many :deliveries, class_name: "Webhook::Delivery", dependent: :destroy
|
|
11
|
+
has_many :secret_reveals, class_name: "Webhook::SecretReveal", dependent: :destroy
|
|
12
|
+
|
|
13
|
+
encrypts :signing_secret
|
|
14
|
+
normalizes :name, with: ->(name) { name.strip }
|
|
15
|
+
normalizes :url, with: ->(url) { url.strip }
|
|
16
|
+
before_validation :normalize_event_types
|
|
17
|
+
|
|
18
|
+
validates :name, presence: true, length: { maximum: 80 }, uniqueness: { scope: :team_id }
|
|
19
|
+
validates :url, presence: true, length: { maximum: 2_048 }
|
|
20
|
+
validates :signing_secret, presence: true
|
|
21
|
+
validates :event_types, presence: true
|
|
22
|
+
validate :url_is_https
|
|
23
|
+
validate :event_types_are_supported
|
|
24
|
+
|
|
25
|
+
scope :enabled, -> { where(enabled: true) }
|
|
26
|
+
scope :alphabetically, -> { order(name: :asc) }
|
|
27
|
+
|
|
28
|
+
def assign_new_secret
|
|
29
|
+
self.signing_secret = "whsec_#{SecureRandom.urlsafe_base64(32)}"
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def rotate_secret!
|
|
33
|
+
assign_new_secret.tap { save! }
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def subscribed_to?(event_type)
|
|
37
|
+
enabled? && event_types.include?(event_type)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
def url_is_https
|
|
42
|
+
uri = URI.parse(url.to_s)
|
|
43
|
+
unless uri.is_a?(URI::HTTPS) && uri.host.present? && uri.userinfo.nil? && uri.fragment.nil?
|
|
44
|
+
errors.add(:url, "must be an HTTPS URL without credentials or a fragment")
|
|
45
|
+
end
|
|
46
|
+
rescue URI::InvalidURIError
|
|
47
|
+
errors.add(:url, "must be a valid HTTPS URL")
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def event_types_are_supported
|
|
51
|
+
unsupported = Array(event_types).map(&:to_s) - Webhook::Event::TYPES
|
|
52
|
+
errors.add(:event_types, "contains unsupported events") if unsupported.any?
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def normalize_event_types
|
|
56
|
+
self.event_types = Array(event_types).map(&:to_s).reject(&:blank?).uniq
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
require "json"
|
|
5
|
+
|
|
6
|
+
class Webhook::Event < ApplicationRecord
|
|
7
|
+
self.table_name = "webhook_events"
|
|
8
|
+
|
|
9
|
+
TYPES = %w[project.created project.updated project.archived].freeze
|
|
10
|
+
MAX_BODY_BYTES = 256.kilobytes
|
|
11
|
+
IdempotencyConflict = Class.new(StandardError)
|
|
12
|
+
|
|
13
|
+
belongs_to :team, inverse_of: :webhook_events
|
|
14
|
+
has_many :deliveries, class_name: "Webhook::Delivery", dependent: :destroy
|
|
15
|
+
|
|
16
|
+
attr_accessor :data
|
|
17
|
+
attr_readonly :public_id, :idempotency_key, :event_type, :body, :payload_sha256, :occurred_at, :team_id
|
|
18
|
+
|
|
19
|
+
validates :public_id, :idempotency_key, :event_type, :body, :payload_sha256, :occurred_at, presence: true
|
|
20
|
+
validates :public_id, uniqueness: true
|
|
21
|
+
validates :idempotency_key, length: { maximum: 200 }
|
|
22
|
+
validates :event_type, inclusion: { in: TYPES }
|
|
23
|
+
validate :body_is_bounded
|
|
24
|
+
|
|
25
|
+
before_validation :assign_identity, on: :create
|
|
26
|
+
before_validation :serialize_body, on: :create
|
|
27
|
+
|
|
28
|
+
scope :reverse_chronologically, -> { order(occurred_at: :desc) }
|
|
29
|
+
|
|
30
|
+
class << self
|
|
31
|
+
def publish!(team:, event_type:, idempotency_key:, data:, occurred_at: Time.current)
|
|
32
|
+
raise ArgumentError, "webhook event data must be an object" unless data.is_a?(Hash)
|
|
33
|
+
|
|
34
|
+
transaction do
|
|
35
|
+
canonical_data = canonicalize(data)
|
|
36
|
+
payload_sha256 = Digest::SHA256.hexdigest(JSON.generate(type: event_type, data: canonical_data))
|
|
37
|
+
event = create_or_find_by!(team:, idempotency_key:) do |record|
|
|
38
|
+
record.event_type = event_type
|
|
39
|
+
record.data = canonical_data
|
|
40
|
+
record.payload_sha256 = payload_sha256
|
|
41
|
+
record.occurred_at = occurred_at
|
|
42
|
+
end
|
|
43
|
+
unless event.event_type == event_type && event.payload_sha256 == payload_sha256
|
|
44
|
+
raise IdempotencyConflict, "idempotency key was already used for a different webhook event"
|
|
45
|
+
end
|
|
46
|
+
if event.previously_new_record?
|
|
47
|
+
team.webhook_endpoints.enabled.find_each do |endpoint|
|
|
48
|
+
next unless endpoint.subscribed_to?(event_type)
|
|
49
|
+
|
|
50
|
+
event.deliveries.create!(team:, endpoint:, next_attempt_at: Time.current)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
event
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
private
|
|
58
|
+
def canonicalize(value)
|
|
59
|
+
case value
|
|
60
|
+
when Hash
|
|
61
|
+
value.to_h { |key, child| [ key.to_s, canonicalize(child) ] }.sort.to_h
|
|
62
|
+
when Array
|
|
63
|
+
value.map { |child| canonicalize(child) }
|
|
64
|
+
when String, Numeric, true, false, nil
|
|
65
|
+
value
|
|
66
|
+
else
|
|
67
|
+
raise ArgumentError, "webhook event data contains unsupported #{value.class}"
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def payload
|
|
73
|
+
JSON.parse(body)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
private
|
|
77
|
+
def assign_identity
|
|
78
|
+
self.public_id ||= "evt_#{SecureRandom.uuid}"
|
|
79
|
+
self.occurred_at ||= Time.current
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def serialize_body
|
|
83
|
+
return if body.present?
|
|
84
|
+
|
|
85
|
+
self.body = JSON.generate(
|
|
86
|
+
id: public_id,
|
|
87
|
+
type: event_type,
|
|
88
|
+
created_at: occurred_at&.iso8601(3),
|
|
89
|
+
data:
|
|
90
|
+
)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def body_is_bounded
|
|
94
|
+
if body.to_s.bytesize > MAX_BODY_BYTES
|
|
95
|
+
errors.add(:body, "must be no larger than 256 KB")
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
|
|
5
|
+
class Webhook::SecretReveal < ApplicationRecord
|
|
6
|
+
self.table_name = "webhook_secret_reveals"
|
|
7
|
+
|
|
8
|
+
EXPIRES_IN = 10.minutes
|
|
9
|
+
Unavailable = Class.new(StandardError)
|
|
10
|
+
|
|
11
|
+
belongs_to :endpoint, class_name: "Webhook::Endpoint", inverse_of: :secret_reveals
|
|
12
|
+
|
|
13
|
+
validates :token_digest, :secret_fingerprint, :expires_at, presence: true
|
|
14
|
+
validates :token_digest, uniqueness: true
|
|
15
|
+
|
|
16
|
+
scope :prunable, -> { where(expires_at: ...1.day.ago) }
|
|
17
|
+
|
|
18
|
+
class << self
|
|
19
|
+
def issue!(endpoint:)
|
|
20
|
+
token = SecureRandom.urlsafe_base64(32)
|
|
21
|
+
reveal = create!(
|
|
22
|
+
endpoint:,
|
|
23
|
+
token_digest: digest(token),
|
|
24
|
+
secret_fingerprint: digest(endpoint.signing_secret),
|
|
25
|
+
expires_at: EXPIRES_IN.from_now
|
|
26
|
+
)
|
|
27
|
+
[ reveal, token ]
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def consume!(endpoint:, token:)
|
|
31
|
+
reveal = endpoint.secret_reveals.find_by(token_digest: digest(token))
|
|
32
|
+
raise Unavailable, "Signing secret is no longer available" unless reveal
|
|
33
|
+
|
|
34
|
+
endpoint.with_lock { reveal.consume! }
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
private
|
|
38
|
+
def digest(value)
|
|
39
|
+
Digest::SHA256.hexdigest(value.to_s)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def consume!
|
|
44
|
+
with_lock do
|
|
45
|
+
unavailable = consumed_at? || expires_at <= Time.current ||
|
|
46
|
+
secret_fingerprint != Digest::SHA256.hexdigest(endpoint.signing_secret)
|
|
47
|
+
raise Unavailable, "Signing secret is no longer available" if unavailable
|
|
48
|
+
|
|
49
|
+
update!(consumed_at: Time.current)
|
|
50
|
+
endpoint.signing_secret
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "openssl"
|
|
4
|
+
|
|
5
|
+
class Webhook::Signature
|
|
6
|
+
class << self
|
|
7
|
+
def headers(event:, secret:, timestamp:, request_id:)
|
|
8
|
+
value = OpenSSL::HMAC.hexdigest("SHA256", secret, "#{timestamp}.#{event.body}")
|
|
9
|
+
{
|
|
10
|
+
"Content-Type" => "application/json",
|
|
11
|
+
"Accept" => "application/json",
|
|
12
|
+
"Accept-Encoding" => "identity",
|
|
13
|
+
"User-Agent" => "WebhookDelivery/1.0",
|
|
14
|
+
"Idempotency-Key" => event.public_id,
|
|
15
|
+
"X-Webhook-Id" => event.public_id,
|
|
16
|
+
"X-Webhook-Timestamp" => timestamp.to_s,
|
|
17
|
+
"X-Webhook-Signature" => "v1=#{value}",
|
|
18
|
+
"X-Webhook-Request-Id" => request_id
|
|
19
|
+
}
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "ipaddr"
|
|
4
|
+
require "net/http"
|
|
5
|
+
require "resolv"
|
|
6
|
+
require "timeout"
|
|
7
|
+
require "uri"
|
|
8
|
+
|
|
9
|
+
class Webhook::Transport
|
|
10
|
+
UnsafeDestination = Class.new(StandardError)
|
|
11
|
+
ResponseLimitReached = Class.new(StandardError)
|
|
12
|
+
ResponseHeadersExceeded = Class.new(StandardError)
|
|
13
|
+
TotalTimeout = Class.new(StandardError)
|
|
14
|
+
HTTPResponse = Data.define(:status, :headers, :body)
|
|
15
|
+
Result = Data.define(:success, :retryable, :status, :headers, :body, :error_code, :error_message) do
|
|
16
|
+
def success? = success
|
|
17
|
+
def retryable? = retryable
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
RESPONSE_BYTES = 16.kilobytes
|
|
21
|
+
RESPONSE_HEADER_BYTES = 32.kilobytes
|
|
22
|
+
RESPONSE_HEADER_COUNT = 64
|
|
23
|
+
TOTAL_TIMEOUT = 20
|
|
24
|
+
RETRYABLE_STATUSES = [ 408, 425, 429 ].freeze
|
|
25
|
+
FORBIDDEN_NETWORKS = %w[
|
|
26
|
+
0.0.0.0/8 10.0.0.0/8 100.64.0.0/10 127.0.0.0/8 169.254.0.0/16
|
|
27
|
+
172.16.0.0/12 192.0.0.0/24 192.0.2.0/24 192.168.0.0/16
|
|
28
|
+
198.18.0.0/15 198.51.100.0/24 203.0.113.0/24 224.0.0.0/4 240.0.0.0/4
|
|
29
|
+
::/96 ::ffff:0:0/96 64:ff9b::/96 64:ff9b:1::/48 100::/64
|
|
30
|
+
2001::/23 2002::/16 3fff::/20 5f00::/16 fc00::/7 fe80::/10 ff00::/8
|
|
31
|
+
].map { |network| IPAddr.new(network) }.freeze
|
|
32
|
+
|
|
33
|
+
class << self
|
|
34
|
+
def deliver(url:, body:, headers:)
|
|
35
|
+
new(url:, body:, headers:).deliver
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def public_address?(value)
|
|
39
|
+
address = IPAddr.new(value)
|
|
40
|
+
FORBIDDEN_NETWORKS.none? { |network| network.include?(address) }
|
|
41
|
+
rescue IPAddr::InvalidAddressError
|
|
42
|
+
false
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def initialize(url:, body:, headers:)
|
|
47
|
+
@url = url
|
|
48
|
+
@body = body
|
|
49
|
+
@headers = headers
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def deliver
|
|
53
|
+
@uri = URI.parse(@url)
|
|
54
|
+
response = Timeout.timeout(TOTAL_TIMEOUT, TotalTimeout) do
|
|
55
|
+
perform_request(resolved_public_address)
|
|
56
|
+
end
|
|
57
|
+
status = response.status
|
|
58
|
+
success = status.between?(200, 299)
|
|
59
|
+
Result.new(
|
|
60
|
+
success:,
|
|
61
|
+
retryable: !success && retryable_status?(status),
|
|
62
|
+
status:,
|
|
63
|
+
headers: response.headers,
|
|
64
|
+
body: response.body,
|
|
65
|
+
error_code: success ? nil : "http_#{status}",
|
|
66
|
+
error_message: success ? nil : "Endpoint returned HTTP #{status}"
|
|
67
|
+
)
|
|
68
|
+
rescue URI::InvalidURIError
|
|
69
|
+
failure("invalid_url", "Endpoint URL is invalid", retryable: false)
|
|
70
|
+
rescue UnsafeDestination => error
|
|
71
|
+
failure("unsafe_destination", error.message, retryable: false)
|
|
72
|
+
rescue Resolv::ResolvError, SocketError => error
|
|
73
|
+
failure("dns_error", error.message, retryable: true)
|
|
74
|
+
rescue Net::OpenTimeout, Net::ReadTimeout, Net::WriteTimeout, Timeout::Error, TotalTimeout => error
|
|
75
|
+
failure("timeout", error.message, retryable: true)
|
|
76
|
+
rescue Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError, ResponseHeadersExceeded => error
|
|
77
|
+
failure("protocol_error", error.message, retryable: false)
|
|
78
|
+
rescue OpenSSL::SSL::SSLError => error
|
|
79
|
+
failure("tls_error", error.message, retryable: false)
|
|
80
|
+
rescue EOFError, IOError, SystemCallError => error
|
|
81
|
+
failure("network_error", error.message, retryable: true)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
private
|
|
85
|
+
def resolved_public_address
|
|
86
|
+
unless @uri.is_a?(URI::HTTPS) && @uri.host.present? && @uri.userinfo.nil? && @uri.fragment.nil?
|
|
87
|
+
raise URI::InvalidURIError
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
addresses = Resolv.getaddresses(@uri.host).uniq
|
|
91
|
+
raise Resolv::ResolvError, "Endpoint hostname did not resolve" if addresses.empty?
|
|
92
|
+
unless addresses.all? { |address| self.class.public_address?(address) }
|
|
93
|
+
raise UnsafeDestination, "Endpoint resolved to a private or reserved address"
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
addresses.sort.first
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def perform_request(address)
|
|
100
|
+
http = Net::HTTP.new(@uri.host, @uri.port, nil)
|
|
101
|
+
http.ipaddr = address
|
|
102
|
+
http.use_ssl = true
|
|
103
|
+
http.open_timeout = 5
|
|
104
|
+
http.read_timeout = 10
|
|
105
|
+
http.write_timeout = 10
|
|
106
|
+
http.max_retries = 0
|
|
107
|
+
|
|
108
|
+
request = Net::HTTP::Post.new(@uri.request_uri, @headers)
|
|
109
|
+
request.body = @body
|
|
110
|
+
|
|
111
|
+
status = nil
|
|
112
|
+
headers = {}
|
|
113
|
+
body = +""
|
|
114
|
+
begin
|
|
115
|
+
http.start do |connection|
|
|
116
|
+
connection.request(request) do |response|
|
|
117
|
+
status = response.code.to_i
|
|
118
|
+
validate_response_headers!(response)
|
|
119
|
+
headers = response_headers(response)
|
|
120
|
+
response.read_body do |chunk|
|
|
121
|
+
remaining = RESPONSE_BYTES - body.bytesize
|
|
122
|
+
body << chunk.byteslice(0, remaining) if remaining.positive?
|
|
123
|
+
raise ResponseLimitReached if chunk.bytesize >= remaining
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
rescue ResponseLimitReached
|
|
128
|
+
nil
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
HTTPResponse.new(status:, headers:, body: normalized_body(body))
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def response_headers(response)
|
|
135
|
+
%w[content-type retry-after x-request-id].to_h do |name|
|
|
136
|
+
[ name, response[name].to_s.first(512) ]
|
|
137
|
+
end.reject { |_name, value| value.empty? }
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def validate_response_headers!(response)
|
|
141
|
+
count = 0
|
|
142
|
+
bytes = 0
|
|
143
|
+
response.each_header do |name, value|
|
|
144
|
+
count += 1
|
|
145
|
+
bytes += name.bytesize + value.bytesize
|
|
146
|
+
if count > RESPONSE_HEADER_COUNT || bytes > RESPONSE_HEADER_BYTES
|
|
147
|
+
raise ResponseHeadersExceeded, "Endpoint response headers exceeded the configured limit"
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def normalized_body(body)
|
|
153
|
+
body.encode(
|
|
154
|
+
Encoding::UTF_8,
|
|
155
|
+
invalid: :replace,
|
|
156
|
+
undef: :replace,
|
|
157
|
+
replace: "�"
|
|
158
|
+
)
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def retryable_status?(status)
|
|
162
|
+
RETRYABLE_STATUSES.include?(status) || status >= 500
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def failure(code, message, retryable:)
|
|
166
|
+
Result.new(
|
|
167
|
+
success: false,
|
|
168
|
+
retryable:,
|
|
169
|
+
status: nil,
|
|
170
|
+
headers: {},
|
|
171
|
+
body: nil,
|
|
172
|
+
error_code: code,
|
|
173
|
+
error_message: message.to_s.first(1_000)
|
|
174
|
+
)
|
|
175
|
+
end
|
|
176
|
+
end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<%= render UI::Webhooks::Form.new(endpoint: @endpoint) %>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<%= render UI::Webhooks::Form.new(endpoint: @endpoint) %>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<%= render UI::Webhooks::Secret.new(endpoint: @endpoint) %>
|