caldav-adapter 8.2.8 → 8.2.10

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.
@@ -17,6 +17,10 @@ module.exports = async function (ctx) {
17
17
  if (ctx.request.type.includes('xml')) {
18
18
  try {
19
19
  ctx.request.xml = new DOMParser().parseFromString(ctx.request.body);
20
+ // Ensure we have a valid document, otherwise set to null
21
+ if (!ctx.request.xml || typeof ctx.request.xml !== 'object') {
22
+ ctx.request.xml = null;
23
+ }
20
24
  } catch (err) {
21
25
  if (ctx.logger) ctx.logger.warn(err);
22
26
  else if (ctx?.app?.emit) ctx.app.emit('error', err, ctx);
package/common/xml.js CHANGED
@@ -21,6 +21,11 @@ module.exports.nsLookup = nsLookup;
21
21
  const select = xpath.useNamespaces(namespaces);
22
22
 
23
23
  function get(path, doc) {
24
+ // Validate that doc is a proper XML document
25
+ if (!doc || typeof doc !== 'object') {
26
+ throw new Error('Invalid XML document: document is null or not an object');
27
+ }
28
+
24
29
  return select(path, doc);
25
30
  }
26
31
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "caldav-adapter",
3
3
  "description": "CalDAV server for Node.js and Koa. Modernized and maintained for Forward Email.",
4
- "version": "8.2.8",
4
+ "version": "8.2.10",
5
5
  "author": "Sanders DeNardi and Forward Email LLC",
6
6
  "contributors": [
7
7
  "Sanders DeNardi <sedenardi@gmail.com> (http://www.sandersdenardi.com/)",
@@ -1,5 +1,29 @@
1
1
  const { setMissingMethod } = require('../../../common/response');
2
2
  const winston = require('../../../common/winston');
3
+ const {
4
+ response,
5
+ status,
6
+ build,
7
+ multistatus
8
+ } = require('../../../common/x-build');
9
+
10
+ /**
11
+ * Encode special characters for XML content to prevent parsing errors
12
+ * @param {string} str - String to encode
13
+ * @returns {string} - XML-safe encoded string
14
+ */
15
+ function encodeXMLEntities(str) {
16
+ if (typeof str !== 'string') {
17
+ return str;
18
+ }
19
+
20
+ return str
21
+ .replaceAll('&', '&amp;') // Must be first to avoid double-encoding
22
+ .replaceAll('<', '&lt;')
23
+ .replaceAll('>', '&gt;')
24
+ .replaceAll('"', '&quot;')
25
+ .replaceAll("'", '&#39;');
26
+ }
3
27
 
4
28
  module.exports = function (options) {
5
29
  const log = winston({ ...options, label: 'calendar/get' });
@@ -15,11 +39,29 @@ module.exports = function (options) {
15
39
 
16
40
  const ics = await options.data.buildICS(ctx, events, calendar);
17
41
 
18
- ctx.status = 200;
19
- ctx.remove('DAV');
20
- ctx.set('Content-Type', 'text/calendar; charset=utf-8');
21
- ctx.set('ETag', options.data.getETag(ctx, calendar));
22
- return ics;
42
+ if (
43
+ ctx.accepts('text/calendar') ||
44
+ ctx.accepts('application/ics') ||
45
+ ctx.accepts('text/x-vcalendar') ||
46
+ ctx.accepts('application/octet-stream')
47
+ ) {
48
+ ctx.status = 200;
49
+ ctx.remove('DAV');
50
+ ctx.set('Content-Type', 'text/calendar; charset=utf-8');
51
+ ctx.set('ETag', options.data.getETag(ctx, calendar));
52
+ return ics;
53
+ }
54
+
55
+ // xml
56
+ const responseObj = response(ctx.url, status[200], [
57
+ {
58
+ 'D:getetag': options.data.getETag(ctx, calendar)
59
+ },
60
+ {
61
+ 'CAL:calendar-data': encodeXMLEntities(ics)
62
+ }
63
+ ]);
64
+ return build(multistatus([responseObj]));
23
65
  }
24
66
 
25
67
  const event = await options.data.getEvent(ctx, {
@@ -37,11 +79,28 @@ module.exports = function (options) {
37
79
 
38
80
  const ics = await options.data.buildICS(ctx, event, calendar);
39
81
 
40
- ctx.status = 200;
41
- ctx.remove('DAV');
42
- ctx.set('Content-Type', 'text/calendar; charset=utf-8');
43
- ctx.set('ETag', options.data.getETag(ctx, calendar));
44
- return ics;
82
+ if (
83
+ ctx.accepts('text/calendar') ||
84
+ ctx.accepts('application/ics') ||
85
+ ctx.accepts('text/x-vcalendar') ||
86
+ ctx.accepts('application/octet-stream')
87
+ ) {
88
+ ctx.status = 200;
89
+ ctx.remove('DAV');
90
+ ctx.set('Content-Type', 'text/calendar; charset=utf-8');
91
+ ctx.set('ETag', options.data.getETag(ctx, calendar));
92
+ return ics;
93
+ }
94
+
95
+ const responseObj = response(ctx.url, status[200], [
96
+ {
97
+ 'D:getetag': options.data.getETag(ctx, calendar)
98
+ },
99
+ {
100
+ 'CAL:calendar-data': encodeXMLEntities(ics)
101
+ }
102
+ ]);
103
+ return build(multistatus([responseObj]));
45
104
  };
46
105
 
47
106
  return {
@@ -13,6 +13,7 @@ module.exports = function (options) {
13
13
  const log = winston({ ...options, label: 'principal' });
14
14
  const methods = {
15
15
  propfind: routePropfind(options),
16
+ get: routePropfind(options), // Handle GET same as PROPFIND for redirected requests
16
17
  // report: reportReport(opts)
17
18
  //
18
19
  // TODO: proppatch
@@ -26,7 +27,7 @@ module.exports = function (options) {
26
27
  const method = ctx.method.toLowerCase();
27
28
 
28
29
  if (method === 'options') {
29
- setOptions(ctx, ['OPTIONS', 'PROPFIND']);
30
+ setOptions(ctx, ['OPTIONS', 'PROPFIND', 'GET']);
30
31
  return;
31
32
  }
32
33
 
@@ -11,6 +11,11 @@ const commonTags = require('../../common/tags');
11
11
  module.exports = function (options) {
12
12
  const tags = commonTags(options);
13
13
  return async function (ctx) {
14
+ // Validate XML document before processing
15
+ if (!ctx.request.xml) {
16
+ ctx.throw(400, 'Invalid or missing XML in PROPFIND request');
17
+ }
18
+
14
19
  const { children } = xml.getWithChildren(
15
20
  '/D:propfind/D:prop',
16
21
  ctx.request.xml