@teleporthq/teleport-plugin-next-workflows 0.45.8 → 0.45.9

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teleporthq/teleport-plugin-next-workflows",
3
- "version": "0.45.8",
3
+ "version": "0.45.9",
4
4
  "description": "A plugin for workflow code generation in Next.js projects.",
5
5
  "author": "teleportHQ",
6
6
  "license": "MIT",
@@ -69,5 +69,5 @@
69
69
  "@teleporthq/teleport-shared": "^0.45.0",
70
70
  "@teleporthq/teleport-types": "^0.45.0"
71
71
  },
72
- "gitHead": "c80bb3e31711590476bd0f8f6c6962948aae20eb"
72
+ "gitHead": "a9b0a45792908ae9e06ac7dc819da99a56febda0"
73
73
  }
@@ -530,8 +530,8 @@ function canSendNewsletterEmail() {
530
530
 
531
531
  // A link to one of the site's pages carries the token in its fragment: a
532
532
  // fragment never reaches a server, a CDN log or a Referer (the page's first
533
- // head script keeps it). The site's own unsubscribe route reads it on the
534
- // server, so a link to it carries it in the query.
533
+ // head script keeps it). The site's own confirm and unsubscribe routes read it
534
+ // on the server, so a link to one of them carries it in the query.
535
535
  function pageLink(path, token) {
536
536
  return path + (path.indexOf('/api/') === 0 ? '?t=' : '#t=') + encodeURIComponent(token);
537
537
  }
@@ -8,13 +8,15 @@
8
8
  * `footer`, `popup`, `form`), else `block`; errors are logged by code and
9
9
  * message only (never a database row); every answer is `{ ok: true, pending }`, after at least
10
10
  * `NEWSLETTER_MIN_RESPONSE_MS`, whatever happened to the address.
11
- * - `confirm` — POST `{ token }` → `{ ok, email? }`.
12
- * - `unsubscribe` — POST `{ token }` → `{ ok, email? }`; a GET with `?t=`
13
- * (a click in a mail client) answers a small page whose button POSTs it, so a
14
- * link scanner that opens every link unsubscribes nobody.
11
+ * - `confirm` and `unsubscribe` — POST `{ token }` → `{ ok, email? }`; a GET
12
+ * with `?t=` (a click in a mail client) answers a small page whose button
13
+ * POSTs it, so a link scanner that opens every link confirms and
14
+ * unsubscribes nobody.
15
15
  */
16
16
  import { securityRuntimeSpecifier } from '../security-runtime/security-runtime-code'
17
+ import { newsletterTokenSpec } from './helper-code'
17
18
  import {
19
+ NEWSLETTER_CONFIRM_URL,
18
20
  NEWSLETTER_UNSUBSCRIBE_URL,
19
21
  NEWSLETTER_VISITOR_LIMIT,
20
22
  NEWSLETTER_VISITOR_SCOPE,
@@ -86,32 +88,7 @@ ${methodGuard(['POST'])}
86
88
  };
87
89
  `
88
90
 
89
- export const generateNewsletterConfirmRoute = (): string => `${ROUTE_PREAMBLE}
90
-
91
- module.exports = async function handler(req, res) {
92
- ${methodGuard(['POST'])}
93
- if (!(await __security.rateLimitRequest(req, res, { scope: 'newsletter-confirm', limit: ${NEWSLETTER_LINK_VISITOR_LIMIT}, windowSeconds: 3600 }))) {
94
- return;
95
- }
96
- var body = readBody(req);
97
- var token = typeof body.token === 'string' ? body.token : '';
98
- if (!token) {
99
- return res.status(200).json({ ok: false });
100
- }
101
- try {
102
- var result = await __newsletter.withNewsletterDb(function (db) {
103
- return __newsletter.confirmNewsletter({ db: db, token: token, req: req });
104
- });
105
- return res.status(200).json(result.ok ? { ok: true, email: result.email } : { ok: false });
106
- } catch (err) {
107
- return __security.sendClientError(res, 503, 'We could not confirm the subscription. Please try again in a moment.', __newsletter.safeError(err));
108
- }
109
- };
110
- `
111
-
112
- export const generateNewsletterUnsubscribeRoute = (): string => `${ROUTE_PREAMBLE}
113
-
114
- function escapeHtml(value) {
91
+ const LINK_PAGE_CODE = `function escapeHtml(value) {
115
92
  return String(value == null ? '' : value)
116
93
  .replace(/&/g, '&')
117
94
  .replace(/</g, '&lt;')
@@ -135,7 +112,66 @@ function sendPage(res, status, title, message, form) {
135
112
  'button{font:inherit;padding:10px 18px;border:0;border-radius:6px;background:#111;color:#fff;cursor:pointer}</style>' +
136
113
  '</head><body><h1>' + escapeHtml(title) + '</h1><p>' + escapeHtml(message) + '</p>' + (form || '') + '</body></html>'
137
114
  );
138
- }
115
+ }`
116
+
117
+ const CONFIRM_LINK_DAYS = newsletterTokenSpec.confirmMaxAgeSeconds / 86400
118
+
119
+ export const generateNewsletterConfirmRoute = (): string => `${ROUTE_PREAMBLE}
120
+
121
+ ${LINK_PAGE_CODE}
122
+
123
+ var NOT_A_LINK = 'This confirmation link does not work. A link works for ${CONFIRM_LINK_DAYS} days: sign up again to get a new one.';
124
+
125
+ module.exports = async function handler(req, res) {
126
+ ${methodGuard(['GET', 'POST'])}
127
+ if (!(await __security.rateLimitRequest(req, res, { scope: 'newsletter-confirm', limit: ${NEWSLETTER_LINK_VISITOR_LIMIT}, windowSeconds: 3600 }))) {
128
+ return;
129
+ }
130
+ if (req.method === 'GET') {
131
+ // Opening the link changes nothing (a mail client's link scanner opens it
132
+ // too, and double opt-in means the person's own action): the button does.
133
+ var linkToken = req.query && typeof req.query.t === 'string' ? req.query.t : '';
134
+ if (!__newsletter.verifyNewsletterToken(linkToken, 'confirm')) {
135
+ return sendPage(res, 200, 'Link not valid', NOT_A_LINK, '');
136
+ }
137
+ return sendPage(
138
+ res,
139
+ 200,
140
+ 'Confirm your subscription',
141
+ 'Receive the newsletter at this address?',
142
+ '<form method="post" action="${NEWSLETTER_CONFIRM_URL}"><input type="hidden" name="t" value="' +
143
+ escapeHtml(linkToken) + '"><button type="submit">Confirm subscription</button></form>'
144
+ );
145
+ }
146
+ var body = readBody(req);
147
+ var fromForm = String((req.headers && req.headers['content-type']) || '').indexOf('application/x-www-form-urlencoded') !== -1;
148
+ var token = typeof body.token === 'string' ? body.token : typeof body.t === 'string' ? body.t : '';
149
+ var result = { ok: false };
150
+ try {
151
+ if (token) {
152
+ result = await __newsletter.withNewsletterDb(function (db) {
153
+ return __newsletter.confirmNewsletter({ db: db, token: token, req: req });
154
+ });
155
+ }
156
+ } catch (err) {
157
+ if (fromForm) {
158
+ console.error('[newsletter] confirm failed:', __newsletter.safeError(err));
159
+ return sendPage(res, 503, 'Please try again', 'We could not confirm your subscription just now. Please try again in a moment.', '');
160
+ }
161
+ return __security.sendClientError(res, 503, 'We could not confirm the subscription. Please try again in a moment.', __newsletter.safeError(err));
162
+ }
163
+ if (fromForm) {
164
+ return result.ok
165
+ ? sendPage(res, 200, "You're subscribed", 'Thank you for confirming. You will receive the newsletter at this address.', '')
166
+ : sendPage(res, 200, 'Link not valid', NOT_A_LINK, '');
167
+ }
168
+ return res.status(200).json(result.ok ? { ok: true, email: result.email } : { ok: false });
169
+ };
170
+ `
171
+
172
+ export const generateNewsletterUnsubscribeRoute = (): string => `${ROUTE_PREAMBLE}
173
+
174
+ ${LINK_PAGE_CODE}
139
175
 
140
176
  var NOT_A_LINK = 'This unsubscribe link does not work. Use the link in a newer email from us.';
141
177