anchor.diff:      end,
   
       NewAttachments = [
  -        {list_to_binary(Name), {list_to_binary(ContentType), Content}} ||
  +        {validate_attachment_name(Name), {list_to_binary(ContentType), Content}} ||
           {Name, {ContentType, _}, Content} <-
           proplists:get_all_values("_attachments", Form)
       ],
  
positive.code: 
  db_attachment_req(#httpd{method=Method}=Req, Db, DocId, FileNameParts)
          when (Method == 'PUT') or (Method == 'DELETE') ->
      FileName = list_to_binary(mochiweb_util:join(lists:map(fun binary_to_list/1, 
          FileNameParts),"/")),
      NewAttachment = case Method of
          'DELETE' ->
              [];
  
negative.code:         {list_to_binary(DocId), [list_to_binary(Rev)]}
      end.
  
  validate_attachment_names(Doc) ->
      lists:foreach(fun({Name, _}) -> 
          validate_attachment_name(Name)
      end, Doc#doc.attachments).
  
  validate_attachment_name(Name) when is_list(Name) ->
      validate_attachment_name(list_to_binary(Name));
  validate_attachment_name(Name) ->
      case is_valid_utf8(Name) of
          true -> 
              case Name of
                  <<"_",_/binary>>=Name -> throw({bad_request, <<"Attachment name can't start with '_'">>});
                  _ -> Name
              end;
          false -> throw({bad_request, <<"Attachment name is not UTF-8 encoded">>})
      end.
  
  %% borrowed from mochijson2:json_bin_is_safe()
  is_valid_utf8(<<>>) ->
      true;
  is_valid_utf8(<<C, Rest/binary>>) ->
      case C of
          $\" ->
              false;
          $\\ ->
              false;
          $\b ->
              false;
          $\f ->
              false;
          $\n ->
              false;
          $\r ->
              false;
          $\t ->
              false;
          C when C >= 0, C < $\s; C >= 16#7f, C =< 16#10FFFF ->
              false;
          C when C < 16#7f ->
              is_valid_utf8(Rest);
          _ ->
              false
      end.
  