From 12a5e2ac0d47d942327133a996e6065292c4f213 Mon Sep 17 00:00:00 2001 From: Robert Newson Date: Thu, 9 Jun 2016 15:39:04 +0100 Subject: [PATCH 1/2] Ensure _design/_replicator VDU is updated --- src/couch_replicator_manager.erl | 49 +++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/src/couch_replicator_manager.erl b/src/couch_replicator_manager.erl index 342dffb..21d732f 100644 --- a/src/couch_replicator_manager.erl +++ b/src/couch_replicator_manager.erl @@ -795,23 +795,40 @@ ensure_rep_ddoc_exists(RepDb) -> ensure_rep_ddoc_exists(RepDb, DDocId) -> case open_rep_doc(RepDb, DDocId) of - {ok, _Doc} -> - ok; - _ -> - DDoc = couch_doc:from_json_obj({[ - {<<"_id">>, DDocId}, - {<<"language">>, <<"javascript">>}, - {<<"validate_doc_update">>, ?REP_DB_DOC_VALIDATE_FUN} - ]}), - try - {ok, _} = save_rep_doc(RepDb, DDoc) - catch - throw:conflict -> - % NFC what to do about this other than - % not kill the process. - ok + {not_found, _Reason} -> + {ok, DDoc} = replication_design_doc(DDocId), + couch_log:notice("creating replicator ddoc", []), + {ok, _Rev} = save_rep_doc(RepDb, DDoc); + {ok, Doc} -> + {Props} = couch_doc:to_json_obj(Doc, []), + case couch_util:get_value(<<"validate_doc_update">>, Props, []) of + ?REP_DB_DOC_VALIDATE_FUN -> + ok; + _ -> + Props1 = lists:keyreplace(<<"validate_doc_update">>, 1, Props, + {<<"validate_doc_update">>, + ?REP_DB_DOC_VALIDATE_FUN}), + DDoc = couch_doc:from_json_obj({Props1}), + couch_log:notice("updating replicator ddoc", []), + try + {ok, _} = save_rep_doc(RepDb, DDoc) + catch + throw:conflict -> + %% ignore, we'll retry next time + ok + end end - end. + end, + ok. + +replication_design_doc(DDocId) -> + DocProps = [ + {<<"_id">>, DDocId}, + {<<"language">>, <<"javascript">>}, + {<<"validate_doc_update">>, ?REP_DB_DOC_VALIDATE_FUN} + ], + {ok, couch_doc:from_json_obj({DocProps})}. + % pretty-print replication id pp_rep_id(#rep{id = RepId}) -> From 69558f31f52c14ea8bee510e69111b0e00f85fe8 Mon Sep 17 00:00:00 2001 From: Robert Newson Date: Thu, 9 Jun 2016 15:39:19 +0100 Subject: [PATCH 2/2] Insist on http/https url's for source and target --- src/couch_replicator_js_functions.hrl | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/couch_replicator_js_functions.hrl b/src/couch_replicator_js_functions.hrl index f3f7ab6..078f7a9 100644 --- a/src/couch_replicator_js_functions.hrl +++ b/src/couch_replicator_js_functions.hrl @@ -18,6 +18,14 @@ throw({forbidden: error_msg}); } + function validateUrl(endpoint, fieldName) { + if ((endpoint.substr(0, 7) !== 'http://') && + endpoint.substr(0, 8) !== 'https://') { + reportError('The `' + fieldName + '\\' property must be an HTTP' + + ' or HTTPS URL'); + } + } + function validateEndpoint(endpoint, fieldName) { if ((typeof endpoint !== 'string') && ((typeof endpoint !== 'object') || (endpoint === null))) { @@ -26,12 +34,20 @@ ' and be either a string or an object.'); } + if (typeof endpoint === 'string') { + validateUrl(endpoint, fieldName); + } + if (typeof endpoint === 'object') { if ((typeof endpoint.url !== 'string') || !endpoint.url) { reportError('The url property must exist in the `' + fieldName + '\\' field and must be a non-empty string.'); } + if (typeof endpoint.url === 'string') { + validateUrl(endpoint.url, fieldName); + } + if ((typeof endpoint.auth !== 'undefined') && ((typeof endpoint.auth !== 'object') || endpoint.auth === null)) {