print.html 652 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740
  1. <!DOCTYPE HTML>
  2. <html lang="en" class="sidebar-visible no-js light">
  3. <head>
  4. <!-- Book generated using mdBook -->
  5. <meta charset="UTF-8">
  6. <title>Synapse</title>
  7. <meta name="robots" content="noindex" />
  8. <!-- Custom HTML head -->
  9. <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  10. <meta name="description" content="">
  11. <meta name="viewport" content="width=device-width, initial-scale=1">
  12. <meta name="theme-color" content="#ffffff" />
  13. <link rel="icon" href="favicon.svg">
  14. <link rel="shortcut icon" href="favicon.png">
  15. <link rel="stylesheet" href="css/variables.css">
  16. <link rel="stylesheet" href="css/general.css">
  17. <link rel="stylesheet" href="css/chrome.css">
  18. <link rel="stylesheet" href="css/print.css" media="print">
  19. <!-- Fonts -->
  20. <link rel="stylesheet" href="FontAwesome/css/font-awesome.css">
  21. <link rel="stylesheet" href="fonts/fonts.css">
  22. <!-- Highlight.js Stylesheets -->
  23. <link rel="stylesheet" href="highlight.css">
  24. <link rel="stylesheet" href="tomorrow-night.css">
  25. <link rel="stylesheet" href="ayu-highlight.css">
  26. <!-- Custom theme stylesheets -->
  27. <link rel="stylesheet" href="docs/website_files/table-of-contents.css">
  28. <link rel="stylesheet" href="docs/website_files/remove-nav-buttons.css">
  29. <link rel="stylesheet" href="docs/website_files/indent-section-headers.css">
  30. <link rel="stylesheet" href="docs/website_files/version-picker.css">
  31. </head>
  32. <body>
  33. <!-- Provide site root to javascript -->
  34. <script type="text/javascript">
  35. var path_to_root = "";
  36. var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
  37. </script>
  38. <!-- Work around some values being stored in localStorage wrapped in quotes -->
  39. <script type="text/javascript">
  40. try {
  41. var theme = localStorage.getItem('mdbook-theme');
  42. var sidebar = localStorage.getItem('mdbook-sidebar');
  43. if (theme.startsWith('"') && theme.endsWith('"')) {
  44. localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
  45. }
  46. if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
  47. localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
  48. }
  49. } catch (e) { }
  50. </script>
  51. <!-- Set the theme before any content is loaded, prevents flash -->
  52. <script type="text/javascript">
  53. var theme;
  54. try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
  55. if (theme === null || theme === undefined) { theme = default_theme; }
  56. var html = document.querySelector('html');
  57. html.classList.remove('no-js')
  58. html.classList.remove('light')
  59. html.classList.add(theme);
  60. html.classList.add('js');
  61. </script>
  62. <!-- Hide / unhide sidebar before it is displayed -->
  63. <script type="text/javascript">
  64. var html = document.querySelector('html');
  65. var sidebar = 'hidden';
  66. if (document.body.clientWidth >= 1080) {
  67. try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
  68. sidebar = sidebar || 'visible';
  69. }
  70. html.classList.remove('sidebar-visible');
  71. html.classList.add("sidebar-" + sidebar);
  72. </script>
  73. <nav id="sidebar" class="sidebar" aria-label="Table of contents">
  74. <div class="sidebar-scrollbox">
  75. <ol class="chapter"><li class="chapter-item expanded affix "><li class="part-title">Introduction</li><li class="chapter-item expanded "><a href="welcome_and_overview.html">Welcome and Overview</a></li><li class="chapter-item expanded affix "><li class="part-title">Setup</li><li class="chapter-item expanded "><a href="setup/installation.html">Installation</a></li><li class="chapter-item expanded "><a href="postgres.html">Using Postgres</a></li><li class="chapter-item expanded "><a href="reverse_proxy.html">Configuring a Reverse Proxy</a></li><li class="chapter-item expanded "><a href="turn-howto.html">Configuring a Turn Server</a></li><li class="chapter-item expanded "><a href="delegate.html">Delegation</a></li><li class="chapter-item expanded affix "><li class="part-title">Upgrading</li><li class="chapter-item expanded "><a href="upgrade.html">Upgrading between Synapse Versions</a></li><li class="chapter-item expanded "><a href="MSC1711_certificates_FAQ.html">Upgrading from pre-Synapse 1.0</a></li><li class="chapter-item expanded affix "><li class="part-title">Usage</li><li class="chapter-item expanded "><a href="federate.html">Federation</a></li><li class="chapter-item expanded "><a href="usage/configuration/index.html">Configuration</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="usage/configuration/homeserver_sample_config.html">Homeserver Sample Config File</a></li><li class="chapter-item expanded "><a href="usage/configuration/logging_sample_config.html">Logging Sample Config File</a></li><li class="chapter-item expanded "><a href="structured_logging.html">Structured Logging</a></li><li class="chapter-item expanded "><a href="usage/configuration/user_authentication/index.html">User Authentication</a></li><li><ol class="section"><li class="chapter-item expanded "><div>Single-Sign On</div></li><li><ol class="section"><li class="chapter-item expanded "><a href="openid.html">OpenID Connect</a></li><li class="chapter-item expanded "><div>SAML</div></li><li class="chapter-item expanded "><div>CAS</div></li><li class="chapter-item expanded "><a href="sso_mapping_providers.html">SSO Mapping Providers</a></li></ol></li><li class="chapter-item expanded "><a href="password_auth_providers.html">Password Auth Providers</a></li><li class="chapter-item expanded "><a href="jwt.html">JSON Web Tokens</a></li></ol></li><li class="chapter-item expanded "><a href="CAPTCHA_SETUP.html">Registration Captcha</a></li><li class="chapter-item expanded "><a href="application_services.html">Application Services</a></li><li class="chapter-item expanded "><a href="server_notices.html">Server Notices</a></li><li class="chapter-item expanded "><a href="consent_tracking.html">Consent Tracking</a></li><li class="chapter-item expanded "><a href="url_previews.html">URL Previews</a></li><li class="chapter-item expanded "><a href="user_directory.html">User Directory</a></li><li class="chapter-item expanded "><a href="message_retention_policies.html">Message Retention Policies</a></li><li class="chapter-item expanded "><a href="modules.html">Pluggable Modules</a></li><li><ol class="section"><li class="chapter-item expanded "><div>Third Party Rules</div></li><li class="chapter-item expanded "><a href="spam_checker.html">Spam Checker</a></li><li class="chapter-item expanded "><a href="presence_router_module.html">Presence Router</a></li><li class="chapter-item expanded "><div>Media Storage Providers</div></li></ol></li><li class="chapter-item expanded "><a href="workers.html">Workers</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="synctl_workers.html">Using synctl with Workers</a></li><li class="chapter-item expanded "><a href="systemd-with-workers/index.html">Systemd</a></li></ol></li></ol></li><li class="chapter-item expanded "><a href="usage/administration/index.html">Administration</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="usage/administration/admin_api/index.html">Admin API</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="admin_api/account_validity.html">Account Validity</a></li><li class="chapter-item expanded "><a href="admin_api/delete_group.html">Delete Group</a></li><li class="chapter-item expanded "><a href="admin_api/event_reports.html">Event Reports</a></li><li class="chapter-item expanded "><a href="admin_api/media_admin_api.html">Media</a></li><li class="chapter-item expanded "><a href="admin_api/purge_history_api.html">Purge History</a></li><li class="chapter-item expanded "><a href="admin_api/purge_room.html">Purge Rooms</a></li><li class="chapter-item expanded "><a href="admin_api/register_api.html">Register Users</a></li><li class="chapter-item expanded "><a href="admin_api/room_membership.html">Manipulate Room Membership</a></li><li class="chapter-item expanded "><a href="admin_api/rooms.html">Rooms</a></li><li class="chapter-item expanded "><a href="admin_api/server_notices.html">Server Notices</a></li><li class="chapter-item expanded "><a href="admin_api/shutdown_room.html">Shutdown Room</a></li><li class="chapter-item expanded "><a href="admin_api/statistics.html">Statistics</a></li><li class="chapter-item expanded "><a href="admin_api/user_admin_api.html">Users</a></li><li class="chapter-item expanded "><a href="admin_api/version_api.html">Server Version</a></li></ol></li><li class="chapter-item expanded "><a href="manhole.html">Manhole</a></li><li class="chapter-item expanded "><a href="metrics-howto.html">Monitoring</a></li><li class="chapter-item expanded "><a href="usage/administration/request_log.html">Request log format</a></li><li class="chapter-item expanded "><div>Scripts</div></li></ol></li><li class="chapter-item expanded "><li class="part-title">Development</li><li class="chapter-item expanded "><a href="development/contributing_guide.html">Contributing Guide</a></li><li class="chapter-item expanded "><a href="code_style.html">Code Style</a></li><li class="chapter-item expanded "><a href="dev/git.html">Git Usage</a></li><li class="chapter-item expanded "><div>Testing</div></li><li class="chapter-item expanded "><a href="opentracing.html">OpenTracing</a></li><li class="chapter-item expanded "><a href="development/database_schema.html">Database Schemas</a></li><li class="chapter-item expanded "><div>Synapse Architecture</div></li><li><ol class="section"><li class="chapter-item expanded "><a href="log_contexts.html">Log Contexts</a></li><li class="chapter-item expanded "><a href="replication.html">Replication</a></li><li class="chapter-item expanded "><a href="tcp_replication.html">TCP Replication</a></li></ol></li><li class="chapter-item expanded "><a href="development/internal_documentation/index.html">Internal Documentation</a></li><li><ol class="section"><li class="chapter-item expanded "><div>Single Sign-On</div></li><li><ol class="section"><li class="chapter-item expanded "><a href="dev/saml.html">SAML</a></li><li class="chapter-item expanded "><a href="dev/cas.html">CAS</a></li></ol></li><li class="chapter-item expanded "><div>State Resolution</div></li><li><ol class="section"><li class="chapter-item expanded "><a href="auth_chain_difference_algorithm.html">The Auth Chain Difference Algorithm</a></li></ol></li><li class="chapter-item expanded "><a href="media_repository.html">Media Repository</a></li><li class="chapter-item expanded "><a href="room_and_user_statistics.html">Room and User Statistics</a></li></ol></li><li class="chapter-item expanded "><div>Scripts</div></li><li class="chapter-item expanded affix "><li class="part-title">Other</li><li class="chapter-item expanded "><a href="deprecation_policy.html">Dependency Deprecation Policy</a></li></ol>
  76. </div>
  77. <div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
  78. </nav>
  79. <div id="page-wrapper" class="page-wrapper">
  80. <div class="page">
  81. <div id="menu-bar-hover-placeholder"></div>
  82. <div id="menu-bar" class="menu-bar sticky bordered">
  83. <div class="left-buttons">
  84. <button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
  85. <i class="fa fa-bars"></i>
  86. </button>
  87. <button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
  88. <i class="fa fa-paint-brush"></i>
  89. </button>
  90. <ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
  91. <li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li>
  92. <li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
  93. <li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
  94. <li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
  95. <li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
  96. </ul>
  97. <button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
  98. <i class="fa fa-search"></i>
  99. </button>
  100. <div class="version-picker">
  101. <div class="dropdown">
  102. <div class="select">
  103. <span></span>
  104. <i class="fa fa-chevron-down"></i>
  105. </div>
  106. <input type="hidden" name="version">
  107. <ul class="dropdown-menu">
  108. <!-- Versions will be added dynamically in version-picker.js -->
  109. </ul>
  110. </div>
  111. </div>
  112. </div>
  113. <h1 class="menu-title">Synapse</h1>
  114. <div class="right-buttons">
  115. <a href="print.html" title="Print this book" aria-label="Print this book">
  116. <i id="print-button" class="fa fa-print"></i>
  117. </a>
  118. <a href="https://github.com/matrix-org/synapse" title="Git repository" aria-label="Git repository">
  119. <i id="git-repository-button" class="fa fa-github"></i>
  120. </a>
  121. </div>
  122. </div>
  123. <div id="search-wrapper" class="hidden">
  124. <form id="searchbar-outer" class="searchbar-outer">
  125. <input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
  126. </form>
  127. <div id="searchresults-outer" class="searchresults-outer hidden">
  128. <div id="searchresults-header" class="searchresults-header"></div>
  129. <ul id="searchresults">
  130. </ul>
  131. </div>
  132. </div>
  133. <!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
  134. <script type="text/javascript">
  135. document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
  136. document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
  137. Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
  138. link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
  139. });
  140. </script>
  141. <div id="content" class="content">
  142. <main>
  143. <!-- Page table of contents -->
  144. <div class="sidetoc">
  145. <nav class="pagetoc"></nav>
  146. </div>
  147. <div style="break-before: page; page-break-before: always;"></div><h1 id="introduction"><a class="header" href="#introduction">Introduction</a></h1>
  148. <p>Welcome to the documentation repository for Synapse, the reference
  149. <a href="https://matrix.org">Matrix</a> homeserver implementation.</p>
  150. <div style="break-before: page; page-break-before: always;"></div><h1 id="installation-instructions"><a class="header" href="#installation-instructions">Installation Instructions</a></h1>
  151. <p>There are 3 steps to follow under <strong>Installation Instructions</strong>.</p>
  152. <ul>
  153. <li><a href="setup/installation.html#installation-instructions">Installation Instructions</a>
  154. <ul>
  155. <li><a href="setup/installation.html#choosing-your-server-name">Choosing your server name</a></li>
  156. <li><a href="setup/installation.html#installing-synapse">Installing Synapse</a>
  157. <ul>
  158. <li><a href="setup/installation.html#installing-from-source">Installing from source</a>
  159. <ul>
  160. <li><a href="setup/installation.html#platform-specific-prerequisites">Platform-specific prerequisites</a>
  161. <ul>
  162. <li><a href="setup/installation.html#debianubunturaspbian">Debian/Ubuntu/Raspbian</a></li>
  163. <li><a href="setup/installation.html#archlinux">ArchLinux</a></li>
  164. <li><a href="setup/installation.html#centosfedora">CentOS/Fedora</a></li>
  165. <li><a href="setup/installation.html#macos">macOS</a></li>
  166. <li><a href="setup/installation.html#opensuse">OpenSUSE</a></li>
  167. <li><a href="setup/installation.html#openbsd">OpenBSD</a></li>
  168. <li><a href="setup/installation.html#windows">Windows</a></li>
  169. </ul>
  170. </li>
  171. </ul>
  172. </li>
  173. <li><a href="setup/installation.html#prebuilt-packages">Prebuilt packages</a>
  174. <ul>
  175. <li><a href="setup/installation.html#docker-images-and-ansible-playbooks">Docker images and Ansible playbooks</a></li>
  176. <li><a href="setup/installation.html#debianubuntu">Debian/Ubuntu</a>
  177. <ul>
  178. <li><a href="setup/installation.html#matrixorg-packages">Matrix.org packages</a></li>
  179. <li><a href="setup/installation.html#downstream-debian-packages">Downstream Debian packages</a></li>
  180. <li><a href="setup/installation.html#downstream-ubuntu-packages">Downstream Ubuntu packages</a></li>
  181. </ul>
  182. </li>
  183. <li><a href="setup/installation.html#fedora">Fedora</a></li>
  184. <li><a href="setup/installation.html#opensuse-1">OpenSUSE</a></li>
  185. <li><a href="setup/installation.html#suse-linux-enterprise-server">SUSE Linux Enterprise Server</a></li>
  186. <li><a href="setup/installation.html#archlinux-1">ArchLinux</a></li>
  187. <li><a href="setup/installation.html#void-linux">Void Linux</a></li>
  188. <li><a href="setup/installation.html#freebsd">FreeBSD</a></li>
  189. <li><a href="setup/installation.html#openbsd-1">OpenBSD</a></li>
  190. <li><a href="setup/installation.html#nixos">NixOS</a></li>
  191. </ul>
  192. </li>
  193. </ul>
  194. </li>
  195. <li><a href="setup/installation.html#setting-up-synapse">Setting up Synapse</a>
  196. <ul>
  197. <li><a href="setup/installation.html#using-postgresql">Using PostgreSQL</a></li>
  198. <li><a href="setup/installation.html#tls-certificates">TLS certificates</a></li>
  199. <li><a href="setup/installation.html#client-well-known-uri">Client Well-Known URI</a></li>
  200. <li><a href="setup/installation.html#email">Email</a></li>
  201. <li><a href="setup/installation.html#registering-a-user">Registering a user</a></li>
  202. <li><a href="setup/installation.html#setting-up-a-turn-server">Setting up a TURN server</a></li>
  203. <li><a href="setup/installation.html#url-previews">URL previews</a></li>
  204. <li><a href="setup/installation.html#troubleshooting-installation">Troubleshooting Installation</a></li>
  205. </ul>
  206. </li>
  207. </ul>
  208. </li>
  209. </ul>
  210. <h2 id="choosing-your-server-name"><a class="header" href="#choosing-your-server-name">Choosing your server name</a></h2>
  211. <p>It is important to choose the name for your server before you install Synapse,
  212. because it cannot be changed later.</p>
  213. <p>The server name determines the &quot;domain&quot; part of user-ids for users on your
  214. server: these will all be of the format <code>@user:my.domain.name</code>. It also
  215. determines how other matrix servers will reach yours for federation.</p>
  216. <p>For a test configuration, set this to the hostname of your server. For a more
  217. production-ready setup, you will probably want to specify your domain
  218. (<code>example.com</code>) rather than a matrix-specific hostname here (in the same way
  219. that your email address is probably <code>user@example.com</code> rather than
  220. <code>user@email.example.com</code>) - but doing so may require more advanced setup: see
  221. <a href="setup/../federate.html">Setting up Federation</a>.</p>
  222. <h2 id="installing-synapse"><a class="header" href="#installing-synapse">Installing Synapse</a></h2>
  223. <h3 id="installing-from-source"><a class="header" href="#installing-from-source">Installing from source</a></h3>
  224. <p>(Prebuilt packages are available for some platforms - see <a href="setup/installation.html#prebuilt-packages">Prebuilt packages</a>.)</p>
  225. <p>When installing from source please make sure that the <a href="setup/installation.html#platform-specific-prerequisites">Platform-specific prerequisites</a> are already installed.</p>
  226. <p>System requirements:</p>
  227. <ul>
  228. <li>POSIX-compliant system (tested on Linux &amp; OS X)</li>
  229. <li>Python 3.5.2 or later, up to Python 3.9.</li>
  230. <li>At least 1GB of free RAM if you want to join large public rooms like #matrix:matrix.org</li>
  231. </ul>
  232. <p>To install the Synapse homeserver run:</p>
  233. <pre><code class="language-sh">mkdir -p ~/synapse
  234. virtualenv -p python3 ~/synapse/env
  235. source ~/synapse/env/bin/activate
  236. pip install --upgrade pip
  237. pip install --upgrade setuptools
  238. pip install matrix-synapse
  239. </code></pre>
  240. <p>This will download Synapse from <a href="https://pypi.org/project/matrix-synapse">PyPI</a>
  241. and install it, along with the python libraries it uses, into a virtual environment
  242. under <code>~/synapse/env</code>. Feel free to pick a different directory if you
  243. prefer.</p>
  244. <p>This Synapse installation can then be later upgraded by using pip again with the
  245. update flag:</p>
  246. <pre><code class="language-sh">source ~/synapse/env/bin/activate
  247. pip install -U matrix-synapse
  248. </code></pre>
  249. <p>Before you can start Synapse, you will need to generate a configuration
  250. file. To do this, run (in your virtualenv, as before):</p>
  251. <pre><code class="language-sh">cd ~/synapse
  252. python -m synapse.app.homeserver \
  253. --server-name my.domain.name \
  254. --config-path homeserver.yaml \
  255. --generate-config \
  256. --report-stats=[yes|no]
  257. </code></pre>
  258. <p>... substituting an appropriate value for <code>--server-name</code>.</p>
  259. <p>This command will generate you a config file that you can then customise, but it will
  260. also generate a set of keys for you. These keys will allow your homeserver to
  261. identify itself to other homeserver, so don't lose or delete them. It would be
  262. wise to back them up somewhere safe. (If, for whatever reason, you do need to
  263. change your homeserver's keys, you may find that other homeserver have the
  264. old key cached. If you update the signing key, you should change the name of the
  265. key in the <code>&lt;server name&gt;.signing.key</code> file (the second word) to something
  266. different. See the <a href="https://matrix.org/docs/spec/server_server/latest.html#retrieving-server-keys">spec</a> for more information on key management).</p>
  267. <p>To actually run your new homeserver, pick a working directory for Synapse to
  268. run (e.g. <code>~/synapse</code>), and:</p>
  269. <pre><code class="language-sh">cd ~/synapse
  270. source env/bin/activate
  271. synctl start
  272. </code></pre>
  273. <h4 id="platform-specific-prerequisites"><a class="header" href="#platform-specific-prerequisites">Platform-specific prerequisites</a></h4>
  274. <p>Synapse is written in Python but some of the libraries it uses are written in
  275. C. So before we can install Synapse itself we need a working C compiler and the
  276. header files for Python C extensions.</p>
  277. <h5 id="debianubunturaspbian"><a class="header" href="#debianubunturaspbian">Debian/Ubuntu/Raspbian</a></h5>
  278. <p>Installing prerequisites on Ubuntu or Debian:</p>
  279. <pre><code class="language-sh">sudo apt install build-essential python3-dev libffi-dev \
  280. python3-pip python3-setuptools sqlite3 \
  281. libssl-dev virtualenv libjpeg-dev libxslt1-dev
  282. </code></pre>
  283. <h5 id="archlinux"><a class="header" href="#archlinux">ArchLinux</a></h5>
  284. <p>Installing prerequisites on ArchLinux:</p>
  285. <pre><code class="language-sh">sudo pacman -S base-devel python python-pip \
  286. python-setuptools python-virtualenv sqlite3
  287. </code></pre>
  288. <h5 id="centosfedora"><a class="header" href="#centosfedora">CentOS/Fedora</a></h5>
  289. <p>Installing prerequisites on CentOS or Fedora Linux:</p>
  290. <pre><code class="language-sh">sudo dnf install libtiff-devel libjpeg-devel libzip-devel freetype-devel \
  291. libwebp-devel libxml2-devel libxslt-devel libpq-devel \
  292. python3-virtualenv libffi-devel openssl-devel python3-devel
  293. sudo dnf groupinstall &quot;Development Tools&quot;
  294. </code></pre>
  295. <h5 id="macos"><a class="header" href="#macos">macOS</a></h5>
  296. <p>Installing prerequisites on macOS:</p>
  297. <pre><code class="language-sh">xcode-select --install
  298. sudo easy_install pip
  299. sudo pip install virtualenv
  300. brew install pkg-config libffi
  301. </code></pre>
  302. <p>On macOS Catalina (10.15) you may need to explicitly install OpenSSL
  303. via brew and inform <code>pip</code> about it so that <code>psycopg2</code> builds:</p>
  304. <pre><code class="language-sh">brew install openssl@1.1
  305. export LDFLAGS=&quot;-L/usr/local/opt/openssl/lib&quot;
  306. export CPPFLAGS=&quot;-I/usr/local/opt/openssl/include&quot;
  307. </code></pre>
  308. <h5 id="opensuse"><a class="header" href="#opensuse">OpenSUSE</a></h5>
  309. <p>Installing prerequisites on openSUSE:</p>
  310. <pre><code class="language-sh">sudo zypper in -t pattern devel_basis
  311. sudo zypper in python-pip python-setuptools sqlite3 python-virtualenv \
  312. python-devel libffi-devel libopenssl-devel libjpeg62-devel
  313. </code></pre>
  314. <h5 id="openbsd"><a class="header" href="#openbsd">OpenBSD</a></h5>
  315. <p>A port of Synapse is available under <code>net/synapse</code>. The filesystem
  316. underlying the homeserver directory (defaults to <code>/var/synapse</code>) has to be
  317. mounted with <code>wxallowed</code> (cf. <code>mount(8)</code>), so creating a separate filesystem
  318. and mounting it to <code>/var/synapse</code> should be taken into consideration.</p>
  319. <p>To be able to build Synapse's dependency on python the <code>WRKOBJDIR</code>
  320. (cf. <code>bsd.port.mk(5)</code>) for building python, too, needs to be on a filesystem
  321. mounted with <code>wxallowed</code> (cf. <code>mount(8)</code>).</p>
  322. <p>Creating a <code>WRKOBJDIR</code> for building python under <code>/usr/local</code> (which on a
  323. default OpenBSD installation is mounted with <code>wxallowed</code>):</p>
  324. <pre><code class="language-sh">doas mkdir /usr/local/pobj_wxallowed
  325. </code></pre>
  326. <p>Assuming <code>PORTS_PRIVSEP=Yes</code> (cf. <code>bsd.port.mk(5)</code>) and <code>SUDO=doas</code> are
  327. configured in <code>/etc/mk.conf</code>:</p>
  328. <pre><code class="language-sh">doas chown _pbuild:_pbuild /usr/local/pobj_wxallowed
  329. </code></pre>
  330. <p>Setting the <code>WRKOBJDIR</code> for building python:</p>
  331. <pre><code class="language-sh">echo WRKOBJDIR_lang/python/3.7=/usr/local/pobj_wxallowed \\nWRKOBJDIR_lang/python/2.7=/usr/local/pobj_wxallowed &gt;&gt; /etc/mk.conf
  332. </code></pre>
  333. <p>Building Synapse:</p>
  334. <pre><code class="language-sh">cd /usr/ports/net/synapse
  335. make install
  336. </code></pre>
  337. <h5 id="windows"><a class="header" href="#windows">Windows</a></h5>
  338. <p>If you wish to run or develop Synapse on Windows, the Windows Subsystem For
  339. Linux provides a Linux environment on Windows 10 which is capable of using the
  340. Debian, Fedora, or source installation methods. More information about WSL can
  341. be found at <a href="https://docs.microsoft.com/en-us/windows/wsl/install-win10">https://docs.microsoft.com/en-us/windows/wsl/install-win10</a> for
  342. Windows 10 and <a href="https://docs.microsoft.com/en-us/windows/wsl/install-on-server">https://docs.microsoft.com/en-us/windows/wsl/install-on-server</a>
  343. for Windows Server.</p>
  344. <h3 id="prebuilt-packages"><a class="header" href="#prebuilt-packages">Prebuilt packages</a></h3>
  345. <p>As an alternative to installing from source, prebuilt packages are available
  346. for a number of platforms.</p>
  347. <h4 id="docker-images-and-ansible-playbooks"><a class="header" href="#docker-images-and-ansible-playbooks">Docker images and Ansible playbooks</a></h4>
  348. <p>There is an official synapse image available at
  349. <a href="https://hub.docker.com/r/matrixdotorg/synapse">https://hub.docker.com/r/matrixdotorg/synapse</a> which can be used with
  350. the docker-compose file available at
  351. <a href="https://github.com/matrix-org/synapse/tree/develop/contrib/docker">contrib/docker</a>.
  352. Further information on this including configuration options is available in the README
  353. on hub.docker.com.</p>
  354. <p>Alternatively, Andreas Peters (previously Silvio Fricke) has contributed a
  355. Dockerfile to automate a synapse server in a single Docker image, at
  356. <a href="https://hub.docker.com/r/avhost/docker-matrix/tags/">https://hub.docker.com/r/avhost/docker-matrix/tags/</a></p>
  357. <p>Slavi Pantaleev has created an Ansible playbook,
  358. which installs the offical Docker image of Matrix Synapse
  359. along with many other Matrix-related services (Postgres database, Element, coturn,
  360. ma1sd, SSL support, etc.).
  361. For more details, see
  362. <a href="https://github.com/spantaleev/matrix-docker-ansible-deploy">https://github.com/spantaleev/matrix-docker-ansible-deploy</a></p>
  363. <h4 id="debianubuntu"><a class="header" href="#debianubuntu">Debian/Ubuntu</a></h4>
  364. <h5 id="matrixorg-packages"><a class="header" href="#matrixorg-packages">Matrix.org packages</a></h5>
  365. <p>Matrix.org provides Debian/Ubuntu packages of the latest stable version of
  366. Synapse via <a href="https://packages.matrix.org/debian/">https://packages.matrix.org/debian/</a>. They are available for Debian
  367. 9 (Stretch), Ubuntu 16.04 (Xenial), and later. To use them:</p>
  368. <pre><code class="language-sh">sudo apt install -y lsb-release wget apt-transport-https
  369. sudo wget -O /usr/share/keyrings/matrix-org-archive-keyring.gpg https://packages.matrix.org/debian/matrix-org-archive-keyring.gpg
  370. echo &quot;deb [signed-by=/usr/share/keyrings/matrix-org-archive-keyring.gpg] https://packages.matrix.org/debian/ $(lsb_release -cs) main&quot; |
  371. sudo tee /etc/apt/sources.list.d/matrix-org.list
  372. sudo apt update
  373. sudo apt install matrix-synapse-py3
  374. </code></pre>
  375. <p><strong>Note</strong>: if you followed a previous version of these instructions which
  376. recommended using <code>apt-key add</code> to add an old key from
  377. <code>https://matrix.org/packages/debian/</code>, you should note that this key has been
  378. revoked. You should remove the old key with <code>sudo apt-key remove C35EB17E1EAE708E6603A9B3AD0592FE47F0DF61</code>, and follow the above instructions to
  379. update your configuration.</p>
  380. <p>The fingerprint of the repository signing key (as shown by <code>gpg /usr/share/keyrings/matrix-org-archive-keyring.gpg</code>) is
  381. <code>AAF9AE843A7584B5A3E4CD2BCF45A512DE2DA058</code>.</p>
  382. <h5 id="downstream-debian-packages"><a class="header" href="#downstream-debian-packages">Downstream Debian packages</a></h5>
  383. <p>We do not recommend using the packages from the default Debian <code>buster</code>
  384. repository at this time, as they are old and suffer from known security
  385. vulnerabilities. You can install the latest version of Synapse from
  386. <a href="setup/installation.html#matrixorg-packages">our repository</a> or from <code>buster-backports</code>. Please
  387. see the <a href="https://backports.debian.org/Instructions/">Debian documentation</a>
  388. for information on how to use backports.</p>
  389. <p>If you are using Debian <code>sid</code> or testing, Synapse is available in the default
  390. repositories and it should be possible to install it simply with:</p>
  391. <pre><code class="language-sh">sudo apt install matrix-synapse
  392. </code></pre>
  393. <h5 id="downstream-ubuntu-packages"><a class="header" href="#downstream-ubuntu-packages">Downstream Ubuntu packages</a></h5>
  394. <p>We do not recommend using the packages in the default Ubuntu repository
  395. at this time, as they are old and suffer from known security vulnerabilities.
  396. The latest version of Synapse can be installed from <a href="setup/installation.html#matrixorg-packages">our repository</a>.</p>
  397. <h4 id="fedora"><a class="header" href="#fedora">Fedora</a></h4>
  398. <p>Synapse is in the Fedora repositories as <code>matrix-synapse</code>:</p>
  399. <pre><code class="language-sh">sudo dnf install matrix-synapse
  400. </code></pre>
  401. <p>Oleg Girko provides Fedora RPMs at
  402. <a href="https://obs.infoserver.lv/project/monitor/matrix-synapse">https://obs.infoserver.lv/project/monitor/matrix-synapse</a></p>
  403. <h4 id="opensuse-1"><a class="header" href="#opensuse-1">OpenSUSE</a></h4>
  404. <p>Synapse is in the OpenSUSE repositories as <code>matrix-synapse</code>:</p>
  405. <pre><code class="language-sh">sudo zypper install matrix-synapse
  406. </code></pre>
  407. <h4 id="suse-linux-enterprise-server"><a class="header" href="#suse-linux-enterprise-server">SUSE Linux Enterprise Server</a></h4>
  408. <p>Unofficial package are built for SLES 15 in the openSUSE:Backports:SLE-15 repository at
  409. <a href="https://download.opensuse.org/repositories/openSUSE:/Backports:/SLE-15/standard/">https://download.opensuse.org/repositories/openSUSE:/Backports:/SLE-15/standard/</a></p>
  410. <h4 id="archlinux-1"><a class="header" href="#archlinux-1">ArchLinux</a></h4>
  411. <p>The quickest way to get up and running with ArchLinux is probably with the community package
  412. <a href="https://www.archlinux.org/packages/community/any/matrix-synapse/">https://www.archlinux.org/packages/community/any/matrix-synapse/</a>, which should pull in most of
  413. the necessary dependencies.</p>
  414. <p>pip may be outdated (6.0.7-1 and needs to be upgraded to 6.0.8-1 ):</p>
  415. <pre><code class="language-sh">sudo pip install --upgrade pip
  416. </code></pre>
  417. <p>If you encounter an error with lib bcrypt causing an Wrong ELF Class:
  418. ELFCLASS32 (x64 Systems), you may need to reinstall py-bcrypt to correctly
  419. compile it under the right architecture. (This should not be needed if
  420. installing under virtualenv):</p>
  421. <pre><code class="language-sh">sudo pip uninstall py-bcrypt
  422. sudo pip install py-bcrypt
  423. </code></pre>
  424. <h4 id="void-linux"><a class="header" href="#void-linux">Void Linux</a></h4>
  425. <p>Synapse can be found in the void repositories as 'synapse':</p>
  426. <pre><code class="language-sh">xbps-install -Su
  427. xbps-install -S synapse
  428. </code></pre>
  429. <h4 id="freebsd"><a class="header" href="#freebsd">FreeBSD</a></h4>
  430. <p>Synapse can be installed via FreeBSD Ports or Packages contributed by Brendan Molloy from:</p>
  431. <ul>
  432. <li>Ports: <code>cd /usr/ports/net-im/py-matrix-synapse &amp;&amp; make install clean</code></li>
  433. <li>Packages: <code>pkg install py37-matrix-synapse</code></li>
  434. </ul>
  435. <h4 id="openbsd-1"><a class="header" href="#openbsd-1">OpenBSD</a></h4>
  436. <p>As of OpenBSD 6.7 Synapse is available as a pre-compiled binary. The filesystem
  437. underlying the homeserver directory (defaults to <code>/var/synapse</code>) has to be
  438. mounted with <code>wxallowed</code> (cf. <code>mount(8)</code>), so creating a separate filesystem
  439. and mounting it to <code>/var/synapse</code> should be taken into consideration.</p>
  440. <p>Installing Synapse:</p>
  441. <pre><code class="language-sh">doas pkg_add synapse
  442. </code></pre>
  443. <h4 id="nixos"><a class="header" href="#nixos">NixOS</a></h4>
  444. <p>Robin Lambertz has packaged Synapse for NixOS at:
  445. <a href="https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/services/misc/matrix-synapse.nix">https://github.com/NixOS/nixpkgs/blob/master/nixos/modules/services/misc/matrix-synapse.nix</a></p>
  446. <h2 id="setting-up-synapse"><a class="header" href="#setting-up-synapse">Setting up Synapse</a></h2>
  447. <p>Once you have installed synapse as above, you will need to configure it.</p>
  448. <h3 id="using-postgresql"><a class="header" href="#using-postgresql">Using PostgreSQL</a></h3>
  449. <p>By default Synapse uses an <a href="https://sqlite.org/">SQLite</a> database and in doing so trades
  450. performance for convenience. Almost all installations should opt to use <a href="https://www.postgresql.org">PostgreSQL</a>
  451. instead. Advantages include:</p>
  452. <ul>
  453. <li>significant performance improvements due to the superior threading and
  454. caching model, smarter query optimiser</li>
  455. <li>allowing the DB to be run on separate hardware</li>
  456. </ul>
  457. <p>For information on how to install and use PostgreSQL in Synapse, please see
  458. <a href="setup/../postgres.html">docs/postgres.md</a></p>
  459. <p>SQLite is only acceptable for testing purposes. SQLite should not be used in
  460. a production server. Synapse will perform poorly when using
  461. SQLite, especially when participating in large rooms.</p>
  462. <h3 id="tls-certificates"><a class="header" href="#tls-certificates">TLS certificates</a></h3>
  463. <p>The default configuration exposes a single HTTP port on the local
  464. interface: <code>http://localhost:8008</code>. It is suitable for local testing,
  465. but for any practical use, you will need Synapse's APIs to be served
  466. over HTTPS.</p>
  467. <p>The recommended way to do so is to set up a reverse proxy on port
  468. <code>8448</code>. You can find documentation on doing so in
  469. <a href="setup/../reverse_proxy.html">docs/reverse_proxy.md</a>.</p>
  470. <p>Alternatively, you can configure Synapse to expose an HTTPS port. To do
  471. so, you will need to edit <code>homeserver.yaml</code>, as follows:</p>
  472. <ul>
  473. <li>First, under the <code>listeners</code> section, uncomment the configuration for the
  474. TLS-enabled listener. (Remove the hash sign (<code>#</code>) at the start of
  475. each line). The relevant lines are like this:</li>
  476. </ul>
  477. <pre><code class="language-yaml"> - port: 8448
  478. type: http
  479. tls: true
  480. resources:
  481. - names: [client, federation]
  482. </code></pre>
  483. <ul>
  484. <li>
  485. <p>You will also need to uncomment the <code>tls_certificate_path</code> and
  486. <code>tls_private_key_path</code> lines under the <code>TLS</code> section. You will need to manage
  487. provisioning of these certificates yourself.</p>
  488. <p>If you are using your own certificate, be sure to use a <code>.pem</code> file that
  489. includes the full certificate chain including any intermediate certificates
  490. (for instance, if using certbot, use <code>fullchain.pem</code> as your certificate, not
  491. <code>cert.pem</code>).</p>
  492. </li>
  493. </ul>
  494. <p>For a more detailed guide to configuring your server for federation, see
  495. <a href="setup/../federate.html">federate.md</a>.</p>
  496. <h3 id="client-well-known-uri"><a class="header" href="#client-well-known-uri">Client Well-Known URI</a></h3>
  497. <p>Setting up the client Well-Known URI is optional but if you set it up, it will
  498. allow users to enter their full username (e.g. <code>@user:&lt;server_name&gt;</code>) into clients
  499. which support well-known lookup to automatically configure the homeserver and
  500. identity server URLs. This is useful so that users don't have to memorize or think
  501. about the actual homeserver URL you are using.</p>
  502. <p>The URL <code>https://&lt;server_name&gt;/.well-known/matrix/client</code> should return JSON in
  503. the following format.</p>
  504. <pre><code class="language-json">{
  505. &quot;m.homeserver&quot;: {
  506. &quot;base_url&quot;: &quot;https://&lt;matrix.example.com&gt;&quot;
  507. }
  508. }
  509. </code></pre>
  510. <p>It can optionally contain identity server information as well.</p>
  511. <pre><code class="language-json">{
  512. &quot;m.homeserver&quot;: {
  513. &quot;base_url&quot;: &quot;https://&lt;matrix.example.com&gt;&quot;
  514. },
  515. &quot;m.identity_server&quot;: {
  516. &quot;base_url&quot;: &quot;https://&lt;identity.example.com&gt;&quot;
  517. }
  518. }
  519. </code></pre>
  520. <p>To work in browser based clients, the file must be served with the appropriate
  521. Cross-Origin Resource Sharing (CORS) headers. A recommended value would be
  522. <code>Access-Control-Allow-Origin: *</code> which would allow all browser based clients to
  523. view it.</p>
  524. <p>In nginx this would be something like:</p>
  525. <pre><code class="language-nginx">location /.well-known/matrix/client {
  526. return 200 '{&quot;m.homeserver&quot;: {&quot;base_url&quot;: &quot;https://&lt;matrix.example.com&gt;&quot;}}';
  527. default_type application/json;
  528. add_header Access-Control-Allow-Origin *;
  529. }
  530. </code></pre>
  531. <p>You should also ensure the <code>public_baseurl</code> option in <code>homeserver.yaml</code> is set
  532. correctly. <code>public_baseurl</code> should be set to the URL that clients will use to
  533. connect to your server. This is the same URL you put for the <code>m.homeserver</code>
  534. <code>base_url</code> above.</p>
  535. <pre><code class="language-yaml">public_baseurl: &quot;https://&lt;matrix.example.com&gt;&quot;
  536. </code></pre>
  537. <h3 id="email"><a class="header" href="#email">Email</a></h3>
  538. <p>It is desirable for Synapse to have the capability to send email. This allows
  539. Synapse to send password reset emails, send verifications when an email address
  540. is added to a user's account, and send email notifications to users when they
  541. receive new messages.</p>
  542. <p>To configure an SMTP server for Synapse, modify the configuration section
  543. headed <code>email</code>, and be sure to have at least the <code>smtp_host</code>, <code>smtp_port</code>
  544. and <code>notif_from</code> fields filled out. You may also need to set <code>smtp_user</code>,
  545. <code>smtp_pass</code>, and <code>require_transport_security</code>.</p>
  546. <p>If email is not configured, password reset, registration and notifications via
  547. email will be disabled.</p>
  548. <h3 id="registering-a-user"><a class="header" href="#registering-a-user">Registering a user</a></h3>
  549. <p>The easiest way to create a new user is to do so from a client like <a href="https://element.io/">Element</a>.</p>
  550. <p>Alternatively, you can do so from the command line. This can be done as follows:</p>
  551. <ol>
  552. <li>If synapse was installed via pip, activate the virtualenv as follows (if Synapse was
  553. installed via a prebuilt package, <code>register_new_matrix_user</code> should already be
  554. on the search path):
  555. <pre><code class="language-sh">cd ~/synapse
  556. source env/bin/activate
  557. synctl start # if not already running
  558. </code></pre>
  559. </li>
  560. <li>Run the following command:
  561. <pre><code class="language-sh">register_new_matrix_user -c homeserver.yaml http://localhost:8008
  562. </code></pre>
  563. </li>
  564. </ol>
  565. <p>This will prompt you to add details for the new user, and will then connect to
  566. the running Synapse to create the new user. For example:</p>
  567. <pre><code>New user localpart: erikj
  568. Password:
  569. Confirm password:
  570. Make admin [no]:
  571. Success!
  572. </code></pre>
  573. <p>This process uses a setting <code>registration_shared_secret</code> in
  574. <code>homeserver.yaml</code>, which is shared between Synapse itself and the
  575. <code>register_new_matrix_user</code> script. It doesn't matter what it is (a random
  576. value is generated by <code>--generate-config</code>), but it should be kept secret, as
  577. anyone with knowledge of it can register users, including admin accounts,
  578. on your server even if <code>enable_registration</code> is <code>false</code>.</p>
  579. <h3 id="setting-up-a-turn-server"><a class="header" href="#setting-up-a-turn-server">Setting up a TURN server</a></h3>
  580. <p>For reliable VoIP calls to be routed via this homeserver, you MUST configure
  581. a TURN server. See
  582. <a href="setup/../turn-howto.html">docs/turn-howto.md</a>
  583. for details.</p>
  584. <h3 id="url-previews"><a class="header" href="#url-previews">URL previews</a></h3>
  585. <p>Synapse includes support for previewing URLs, which is disabled by default. To
  586. turn it on you must enable the <code>url_preview_enabled: True</code> config parameter
  587. and explicitly specify the IP ranges that Synapse is not allowed to spider for
  588. previewing in the <code>url_preview_ip_range_blacklist</code> configuration parameter.
  589. This is critical from a security perspective to stop arbitrary Matrix users
  590. spidering 'internal' URLs on your network. At the very least we recommend that
  591. your loopback and RFC1918 IP addresses are blacklisted.</p>
  592. <p>This also requires the optional <code>lxml</code> python dependency to be installed. This
  593. in turn requires the <code>libxml2</code> library to be available - on Debian/Ubuntu this
  594. means <code>apt-get install libxml2-dev</code>, or equivalent for your OS.</p>
  595. <h3 id="troubleshooting-installation"><a class="header" href="#troubleshooting-installation">Troubleshooting Installation</a></h3>
  596. <p><code>pip</code> seems to leak <em>lots</em> of memory during installation. For instance, a Linux
  597. host with 512MB of RAM may run out of memory whilst installing Twisted. If this
  598. happens, you will have to individually install the dependencies which are
  599. failing, e.g.:</p>
  600. <pre><code class="language-sh">pip install twisted
  601. </code></pre>
  602. <p>If you have any other problems, feel free to ask in
  603. <a href="https://matrix.to/#/#synapse:matrix.org">#synapse:matrix.org</a>.</p>
  604. <div style="break-before: page; page-break-before: always;"></div><h1 id="using-postgres"><a class="header" href="#using-postgres">Using Postgres</a></h1>
  605. <p>Synapse supports PostgreSQL versions 9.6 or later.</p>
  606. <h2 id="install-postgres-client-libraries"><a class="header" href="#install-postgres-client-libraries">Install postgres client libraries</a></h2>
  607. <p>Synapse will require the python postgres client library in order to
  608. connect to a postgres database.</p>
  609. <ul>
  610. <li>
  611. <p>If you are using the <a href="setup/installation.html#matrixorg-packages">matrix.org debian/ubuntu
  612. packages</a>, the necessary python
  613. library will already be installed, but you will need to ensure the
  614. low-level postgres library is installed, which you can do with
  615. <code>apt install libpq5</code>.</p>
  616. </li>
  617. <li>
  618. <p>For other pre-built packages, please consult the documentation from
  619. the relevant package.</p>
  620. </li>
  621. <li>
  622. <p>If you installed synapse <a href="setup/installation.html#installing-from-source">in a
  623. virtualenv</a>, you can install
  624. the library with:</p>
  625. <pre><code>~/synapse/env/bin/pip install &quot;matrix-synapse[postgres]&quot;
  626. </code></pre>
  627. <p>(substituting the path to your virtualenv for <code>~/synapse/env</code>, if
  628. you used a different path). You will require the postgres
  629. development files. These are in the <code>libpq-dev</code> package on
  630. Debian-derived distributions.</p>
  631. </li>
  632. </ul>
  633. <h2 id="set-up-database"><a class="header" href="#set-up-database">Set up database</a></h2>
  634. <p>Assuming your PostgreSQL database user is called <code>postgres</code>, first authenticate as the database user with:</p>
  635. <pre><code>su - postgres
  636. # Or, if your system uses sudo to get administrative rights
  637. sudo -u postgres bash
  638. </code></pre>
  639. <p>Then, create a postgres user and a database with:</p>
  640. <pre><code># this will prompt for a password for the new user
  641. createuser --pwprompt synapse_user
  642. createdb --encoding=UTF8 --locale=C --template=template0 --owner=synapse_user synapse
  643. </code></pre>
  644. <p>The above will create a user called <code>synapse_user</code>, and a database called
  645. <code>synapse</code>.</p>
  646. <p>Note that the PostgreSQL database <em>must</em> have the correct encoding set
  647. (as shown above), otherwise it will not be able to store UTF8 strings.</p>
  648. <p>You may need to enable password authentication so <code>synapse_user</code> can
  649. connect to the database. See
  650. <a href="https://www.postgresql.org/docs/current/auth-pg-hba-conf.html">https://www.postgresql.org/docs/current/auth-pg-hba-conf.html</a>.</p>
  651. <h2 id="synapse-config"><a class="header" href="#synapse-config">Synapse config</a></h2>
  652. <p>When you are ready to start using PostgreSQL, edit the <code>database</code>
  653. section in your config file to match the following lines:</p>
  654. <pre><code class="language-yaml">database:
  655. name: psycopg2
  656. args:
  657. user: &lt;user&gt;
  658. password: &lt;pass&gt;
  659. database: &lt;db&gt;
  660. host: &lt;host&gt;
  661. cp_min: 5
  662. cp_max: 10
  663. </code></pre>
  664. <p>All key, values in <code>args</code> are passed to the <code>psycopg2.connect(..)</code>
  665. function, except keys beginning with <code>cp_</code>, which are consumed by the
  666. twisted adbapi connection pool. See the <a href="https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS">libpq
  667. documentation</a>
  668. for a list of options which can be passed.</p>
  669. <p>You should consider tuning the <code>args.keepalives_*</code> options if there is any danger of
  670. the connection between your homeserver and database dropping, otherwise Synapse
  671. may block for an extended period while it waits for a response from the
  672. database server. Example values might be:</p>
  673. <pre><code class="language-yaml">database:
  674. args:
  675. # ... as above
  676. # seconds of inactivity after which TCP should send a keepalive message to the server
  677. keepalives_idle: 10
  678. # the number of seconds after which a TCP keepalive message that is not
  679. # acknowledged by the server should be retransmitted
  680. keepalives_interval: 10
  681. # the number of TCP keepalives that can be lost before the client's connection
  682. # to the server is considered dead
  683. keepalives_count: 3
  684. </code></pre>
  685. <h2 id="tuning-postgres"><a class="header" href="#tuning-postgres">Tuning Postgres</a></h2>
  686. <p>The default settings should be fine for most deployments. For larger
  687. scale deployments tuning some of the settings is recommended, details of
  688. which can be found at
  689. <a href="https://wiki.postgresql.org/wiki/Tuning_Your_PostgreSQL_Server">https://wiki.postgresql.org/wiki/Tuning_Your_PostgreSQL_Server</a>.</p>
  690. <p>In particular, we've found tuning the following values helpful for
  691. performance:</p>
  692. <ul>
  693. <li><code>shared_buffers</code></li>
  694. <li><code>effective_cache_size</code></li>
  695. <li><code>work_mem</code></li>
  696. <li><code>maintenance_work_mem</code></li>
  697. <li><code>autovacuum_work_mem</code></li>
  698. </ul>
  699. <p>Note that the appropriate values for those fields depend on the amount
  700. of free memory the database host has available.</p>
  701. <h2 id="porting-from-sqlite"><a class="header" href="#porting-from-sqlite">Porting from SQLite</a></h2>
  702. <h3 id="overview"><a class="header" href="#overview">Overview</a></h3>
  703. <p>The script <code>synapse_port_db</code> allows porting an existing synapse server
  704. backed by SQLite to using PostgreSQL. This is done in as a two phase
  705. process:</p>
  706. <ol>
  707. <li>Copy the existing SQLite database to a separate location and run
  708. the port script against that offline database.</li>
  709. <li>Shut down the server. Rerun the port script to port any data that
  710. has come in since taking the first snapshot. Restart server against
  711. the PostgreSQL database.</li>
  712. </ol>
  713. <p>The port script is designed to be run repeatedly against newer snapshots
  714. of the SQLite database file. This makes it safe to repeat step 1 if
  715. there was a delay between taking the previous snapshot and being ready
  716. to do step 2.</p>
  717. <p>It is safe to at any time kill the port script and restart it.</p>
  718. <p>Note that the database may take up significantly more (25% - 100% more)
  719. space on disk after porting to Postgres.</p>
  720. <h3 id="using-the-port-script"><a class="header" href="#using-the-port-script">Using the port script</a></h3>
  721. <p>Firstly, shut down the currently running synapse server and copy its
  722. database file (typically <code>homeserver.db</code>) to another location. Once the
  723. copy is complete, restart synapse. For instance:</p>
  724. <pre><code>./synctl stop
  725. cp homeserver.db homeserver.db.snapshot
  726. ./synctl start
  727. </code></pre>
  728. <p>Copy the old config file into a new config file:</p>
  729. <pre><code>cp homeserver.yaml homeserver-postgres.yaml
  730. </code></pre>
  731. <p>Edit the database section as described in the section <em>Synapse config</em>
  732. above and with the SQLite snapshot located at <code>homeserver.db.snapshot</code>
  733. simply run:</p>
  734. <pre><code>synapse_port_db --sqlite-database homeserver.db.snapshot \
  735. --postgres-config homeserver-postgres.yaml
  736. </code></pre>
  737. <p>The flag <code>--curses</code> displays a coloured curses progress UI.</p>
  738. <p>If the script took a long time to complete, or time has otherwise passed
  739. since the original snapshot was taken, repeat the previous steps with a
  740. newer snapshot.</p>
  741. <p>To complete the conversion shut down the synapse server and run the port
  742. script one last time, e.g. if the SQLite database is at <code>homeserver.db</code>
  743. run:</p>
  744. <pre><code>synapse_port_db --sqlite-database homeserver.db \
  745. --postgres-config homeserver-postgres.yaml
  746. </code></pre>
  747. <p>Once that has completed, change the synapse config to point at the
  748. PostgreSQL database configuration file <code>homeserver-postgres.yaml</code>:</p>
  749. <pre><code>./synctl stop
  750. mv homeserver.yaml homeserver-old-sqlite.yaml
  751. mv homeserver-postgres.yaml homeserver.yaml
  752. ./synctl start
  753. </code></pre>
  754. <p>Synapse should now be running against PostgreSQL.</p>
  755. <h2 id="troubleshooting"><a class="header" href="#troubleshooting">Troubleshooting</a></h2>
  756. <h3 id="alternative-auth-methods"><a class="header" href="#alternative-auth-methods">Alternative auth methods</a></h3>
  757. <p>If you get an error along the lines of <code>FATAL: Ident authentication failed for user &quot;synapse_user&quot;</code>, you may need to use an authentication method other than
  758. <code>ident</code>:</p>
  759. <ul>
  760. <li>
  761. <p>If the <code>synapse_user</code> user has a password, add the password to the <code>database:</code>
  762. section of <code>homeserver.yaml</code>. Then add the following to <code>pg_hba.conf</code>:</p>
  763. <pre><code>host synapse synapse_user ::1/128 md5 # or `scram-sha-256` instead of `md5` if you use that
  764. </code></pre>
  765. </li>
  766. <li>
  767. <p>If the <code>synapse_user</code> user does not have a password, then a password doesn't
  768. have to be added to <code>homeserver.yaml</code>. But the following does need to be added
  769. to <code>pg_hba.conf</code>:</p>
  770. <pre><code>host synapse synapse_user ::1/128 trust
  771. </code></pre>
  772. </li>
  773. </ul>
  774. <p>Note that line order matters in <code>pg_hba.conf</code>, so make sure that if you do add a
  775. new line, it is inserted before:</p>
  776. <pre><code>host all all ::1/128 ident
  777. </code></pre>
  778. <h3 id="fixing-incorrect-collate-or-ctype"><a class="header" href="#fixing-incorrect-collate-or-ctype">Fixing incorrect <code>COLLATE</code> or <code>CTYPE</code></a></h3>
  779. <p>Synapse will refuse to set up a new database if it has the wrong values of
  780. <code>COLLATE</code> and <code>CTYPE</code> set, and will log warnings on existing databases. Using
  781. different locales can cause issues if the locale library is updated from
  782. underneath the database, or if a different version of the locale is used on any
  783. replicas.</p>
  784. <p>The safest way to fix the issue is to dump the database and recreate it with
  785. the correct locale parameter (as shown above). It is also possible to change the
  786. parameters on a live database and run a <code>REINDEX</code> on the entire database,
  787. however extreme care must be taken to avoid database corruption.</p>
  788. <p>Note that the above may fail with an error about duplicate rows if corruption
  789. has already occurred, and such duplicate rows will need to be manually removed.</p>
  790. <h3 id="fixing-inconsistent-sequences-error"><a class="header" href="#fixing-inconsistent-sequences-error">Fixing inconsistent sequences error</a></h3>
  791. <p>Synapse uses Postgres sequences to generate IDs for various tables. A sequence
  792. and associated table can get out of sync if, for example, Synapse has been
  793. downgraded and then upgraded again.</p>
  794. <p>To fix the issue shut down Synapse (including any and all workers) and run the
  795. SQL command included in the error message. Once done Synapse should start
  796. successfully.</p>
  797. <div style="break-before: page; page-break-before: always;"></div><h1 id="using-a-reverse-proxy-with-synapse"><a class="header" href="#using-a-reverse-proxy-with-synapse">Using a reverse proxy with Synapse</a></h1>
  798. <p>It is recommended to put a reverse proxy such as
  799. <a href="https://nginx.org/en/docs/http/ngx_http_proxy_module.html">nginx</a>,
  800. <a href="https://httpd.apache.org/docs/current/mod/mod_proxy_http.html">Apache</a>,
  801. <a href="https://caddyserver.com/docs/quick-starts/reverse-proxy">Caddy</a>,
  802. <a href="https://www.haproxy.org/">HAProxy</a> or
  803. <a href="https://man.openbsd.org/relayd.8">relayd</a> in front of Synapse. One advantage
  804. of doing so is that it means that you can expose the default https port
  805. (443) to Matrix clients without needing to run Synapse with root
  806. privileges.</p>
  807. <p>You should configure your reverse proxy to forward requests to <code>/_matrix</code> or
  808. <code>/_synapse/client</code> to Synapse, and have it set the <code>X-Forwarded-For</code> and
  809. <code>X-Forwarded-Proto</code> request headers.</p>
  810. <p>You should remember that Matrix clients and other Matrix servers do not
  811. necessarily need to connect to your server via the same server name or
  812. port. Indeed, clients will use port 443 by default, whereas servers default to
  813. port 8448. Where these are different, we refer to the 'client port' and the
  814. 'federation port'. See <a href="https://matrix.org/docs/spec/server_server/latest#resolving-server-names">the Matrix
  815. specification</a>
  816. for more details of the algorithm used for federation connections, and
  817. <a href="delegate.html">delegate.md</a> for instructions on setting up delegation.</p>
  818. <p><strong>NOTE</strong>: Your reverse proxy must not <code>canonicalise</code> or <code>normalise</code>
  819. the requested URI in any way (for example, by decoding <code>%xx</code> escapes).
  820. Beware that Apache <em>will</em> canonicalise URIs unless you specify
  821. <code>nocanon</code>.</p>
  822. <p>Let's assume that we expect clients to connect to our server at
  823. <code>https://matrix.example.com</code>, and other servers to connect at
  824. <code>https://example.com:8448</code>. The following sections detail the configuration of
  825. the reverse proxy and the homeserver.</p>
  826. <h2 id="reverse-proxy-configuration-examples"><a class="header" href="#reverse-proxy-configuration-examples">Reverse-proxy configuration examples</a></h2>
  827. <p><strong>NOTE</strong>: You only need one of these.</p>
  828. <h3 id="nginx"><a class="header" href="#nginx">nginx</a></h3>
  829. <pre><code>server {
  830. listen 443 ssl http2;
  831. listen [::]:443 ssl http2;
  832. # For the federation port
  833. listen 8448 ssl http2 default_server;
  834. listen [::]:8448 ssl http2 default_server;
  835. server_name matrix.example.com;
  836. location ~* ^(\/_matrix|\/_synapse\/client) {
  837. proxy_pass http://localhost:8008;
  838. proxy_set_header X-Forwarded-For $remote_addr;
  839. proxy_set_header X-Forwarded-Proto $scheme;
  840. proxy_set_header Host $host;
  841. # Nginx by default only allows file uploads up to 1M in size
  842. # Increase client_max_body_size to match max_upload_size defined in homeserver.yaml
  843. client_max_body_size 50M;
  844. }
  845. }
  846. </code></pre>
  847. <p><strong>NOTE</strong>: Do not add a path after the port in <code>proxy_pass</code>, otherwise nginx will
  848. canonicalise/normalise the URI.</p>
  849. <h3 id="caddy-1"><a class="header" href="#caddy-1">Caddy 1</a></h3>
  850. <pre><code>matrix.example.com {
  851. proxy /_matrix http://localhost:8008 {
  852. transparent
  853. }
  854. proxy /_synapse/client http://localhost:8008 {
  855. transparent
  856. }
  857. }
  858. example.com:8448 {
  859. proxy / http://localhost:8008 {
  860. transparent
  861. }
  862. }
  863. </code></pre>
  864. <h3 id="caddy-2"><a class="header" href="#caddy-2">Caddy 2</a></h3>
  865. <pre><code>matrix.example.com {
  866. reverse_proxy /_matrix/* http://localhost:8008
  867. reverse_proxy /_synapse/client/* http://localhost:8008
  868. }
  869. example.com:8448 {
  870. reverse_proxy http://localhost:8008
  871. }
  872. </code></pre>
  873. <h3 id="apache"><a class="header" href="#apache">Apache</a></h3>
  874. <pre><code>&lt;VirtualHost *:443&gt;
  875. SSLEngine on
  876. ServerName matrix.example.com
  877. RequestHeader set &quot;X-Forwarded-Proto&quot; expr=%{REQUEST_SCHEME}
  878. AllowEncodedSlashes NoDecode
  879. ProxyPreserveHost on
  880. ProxyPass /_matrix http://127.0.0.1:8008/_matrix nocanon
  881. ProxyPassReverse /_matrix http://127.0.0.1:8008/_matrix
  882. ProxyPass /_synapse/client http://127.0.0.1:8008/_synapse/client nocanon
  883. ProxyPassReverse /_synapse/client http://127.0.0.1:8008/_synapse/client
  884. &lt;/VirtualHost&gt;
  885. &lt;VirtualHost *:8448&gt;
  886. SSLEngine on
  887. ServerName example.com
  888. RequestHeader set &quot;X-Forwarded-Proto&quot; expr=%{REQUEST_SCHEME}
  889. AllowEncodedSlashes NoDecode
  890. ProxyPass /_matrix http://127.0.0.1:8008/_matrix nocanon
  891. ProxyPassReverse /_matrix http://127.0.0.1:8008/_matrix
  892. &lt;/VirtualHost&gt;
  893. </code></pre>
  894. <p><strong>NOTE</strong>: ensure the <code>nocanon</code> options are included.</p>
  895. <p><strong>NOTE 2</strong>: It appears that Synapse is currently incompatible with the ModSecurity module for Apache (<code>mod_security2</code>). If you need it enabled for other services on your web server, you can disable it for Synapse's two VirtualHosts by including the following lines before each of the two <code>&lt;/VirtualHost&gt;</code> above:</p>
  896. <pre><code>&lt;IfModule security2_module&gt;
  897. SecRuleEngine off
  898. &lt;/IfModule&gt;
  899. </code></pre>
  900. <p><strong>NOTE 3</strong>: Missing <code>ProxyPreserveHost on</code> can lead to a redirect loop.</p>
  901. <h3 id="haproxy"><a class="header" href="#haproxy">HAProxy</a></h3>
  902. <pre><code>frontend https
  903. bind :::443 v4v6 ssl crt /etc/ssl/haproxy/ strict-sni alpn h2,http/1.1
  904. http-request set-header X-Forwarded-Proto https if { ssl_fc }
  905. http-request set-header X-Forwarded-Proto http if !{ ssl_fc }
  906. http-request set-header X-Forwarded-For %[src]
  907. # Matrix client traffic
  908. acl matrix-host hdr(host) -i matrix.example.com
  909. acl matrix-path path_beg /_matrix
  910. acl matrix-path path_beg /_synapse/client
  911. use_backend matrix if matrix-host matrix-path
  912. frontend matrix-federation
  913. bind :::8448 v4v6 ssl crt /etc/ssl/haproxy/synapse.pem alpn h2,http/1.1
  914. http-request set-header X-Forwarded-Proto https if { ssl_fc }
  915. http-request set-header X-Forwarded-Proto http if !{ ssl_fc }
  916. http-request set-header X-Forwarded-For %[src]
  917. default_backend matrix
  918. backend matrix
  919. server matrix 127.0.0.1:8008
  920. </code></pre>
  921. <h3 id="relayd"><a class="header" href="#relayd">Relayd</a></h3>
  922. <pre><code>table &lt;webserver&gt; { 127.0.0.1 }
  923. table &lt;matrixserver&gt; { 127.0.0.1 }
  924. http protocol &quot;https&quot; {
  925. tls { no tlsv1.0, ciphers &quot;HIGH&quot; }
  926. tls keypair &quot;example.com&quot;
  927. match header set &quot;X-Forwarded-For&quot; value &quot;$REMOTE_ADDR&quot;
  928. match header set &quot;X-Forwarded-Proto&quot; value &quot;https&quot;
  929. # set CORS header for .well-known/matrix/server, .well-known/matrix/client
  930. # httpd does not support setting headers, so do it here
  931. match request path &quot;/.well-known/matrix/*&quot; tag &quot;matrix-cors&quot;
  932. match response tagged &quot;matrix-cors&quot; header set &quot;Access-Control-Allow-Origin&quot; value &quot;*&quot;
  933. pass quick path &quot;/_matrix/*&quot; forward to &lt;matrixserver&gt;
  934. pass quick path &quot;/_synapse/client/*&quot; forward to &lt;matrixserver&gt;
  935. # pass on non-matrix traffic to webserver
  936. pass forward to &lt;webserver&gt;
  937. }
  938. relay &quot;https_traffic&quot; {
  939. listen on egress port 443 tls
  940. protocol &quot;https&quot;
  941. forward to &lt;matrixserver&gt; port 8008 check tcp
  942. forward to &lt;webserver&gt; port 8080 check tcp
  943. }
  944. http protocol &quot;matrix&quot; {
  945. tls { no tlsv1.0, ciphers &quot;HIGH&quot; }
  946. tls keypair &quot;example.com&quot;
  947. block
  948. pass quick path &quot;/_matrix/*&quot; forward to &lt;matrixserver&gt;
  949. pass quick path &quot;/_synapse/client/*&quot; forward to &lt;matrixserver&gt;
  950. }
  951. relay &quot;matrix_federation&quot; {
  952. listen on egress port 8448 tls
  953. protocol &quot;matrix&quot;
  954. forward to &lt;matrixserver&gt; port 8008 check tcp
  955. }
  956. </code></pre>
  957. <h2 id="homeserver-configuration"><a class="header" href="#homeserver-configuration">Homeserver Configuration</a></h2>
  958. <p>You will also want to set <code>bind_addresses: ['127.0.0.1']</code> and
  959. <code>x_forwarded: true</code> for port 8008 in <code>homeserver.yaml</code> to ensure that
  960. client IP addresses are recorded correctly.</p>
  961. <p>Having done so, you can then use <code>https://matrix.example.com</code> (instead
  962. of <code>https://matrix.example.com:8448</code>) as the &quot;Custom server&quot; when
  963. connecting to Synapse from a client.</p>
  964. <h2 id="health-check-endpoint"><a class="header" href="#health-check-endpoint">Health check endpoint</a></h2>
  965. <p>Synapse exposes a health check endpoint for use by reverse proxies.
  966. Each configured HTTP listener has a <code>/health</code> endpoint which always returns
  967. 200 OK (and doesn't get logged).</p>
  968. <h2 id="synapse-administration-endpoints"><a class="header" href="#synapse-administration-endpoints">Synapse administration endpoints</a></h2>
  969. <p>Endpoints for administering your Synapse instance are placed under
  970. <code>/_synapse/admin</code>. These require authentication through an access token of an
  971. admin user. However as access to these endpoints grants the caller a lot of power,
  972. we do not recommend exposing them to the public internet without good reason.</p>
  973. <div style="break-before: page; page-break-before: always;"></div><h1 id="overview-1"><a class="header" href="#overview-1">Overview</a></h1>
  974. <p>This document explains how to enable VoIP relaying on your Home Server with
  975. TURN.</p>
  976. <p>The synapse Matrix Home Server supports integration with TURN server via the
  977. <a href="https://tools.ietf.org/html/draft-uberti-behave-turn-rest-00">TURN server REST API</a>. This
  978. allows the Home Server to generate credentials that are valid for use on the
  979. TURN server through the use of a secret shared between the Home Server and the
  980. TURN server.</p>
  981. <p>The following sections describe how to install <a href="https://github.com/coturn/coturn">coturn</a> (which implements the TURN REST API) and integrate it with synapse.</p>
  982. <h2 id="requirements"><a class="header" href="#requirements">Requirements</a></h2>
  983. <p>For TURN relaying with <code>coturn</code> to work, it must be hosted on a server/endpoint with a public IP.</p>
  984. <p>Hosting TURN behind a NAT (even with appropriate port forwarding) is known to cause issues
  985. and to often not work.</p>
  986. <h2 id="coturn-setup"><a class="header" href="#coturn-setup"><code>coturn</code> setup</a></h2>
  987. <h3 id="initial-installation"><a class="header" href="#initial-installation">Initial installation</a></h3>
  988. <p>The TURN daemon <code>coturn</code> is available from a variety of sources such as native package managers, or installation from source.</p>
  989. <h4 id="debian-installation"><a class="header" href="#debian-installation">Debian installation</a></h4>
  990. <p>Just install the debian package:</p>
  991. <pre><code class="language-sh">apt install coturn
  992. </code></pre>
  993. <p>This will install and start a systemd service called <code>coturn</code>.</p>
  994. <h4 id="source-installation"><a class="header" href="#source-installation">Source installation</a></h4>
  995. <ol>
  996. <li>
  997. <p>Download the <a href="https://github.com/coturn/coturn/releases/latest">latest release</a> from github. Unpack it and <code>cd</code> into the directory.</p>
  998. </li>
  999. <li>
  1000. <p>Configure it:</p>
  1001. <pre><code>./configure
  1002. </code></pre>
  1003. <p>You may need to install <code>libevent2</code>: if so, you should do so in
  1004. the way recommended by your operating system. You can ignore
  1005. warnings about lack of database support: a database is unnecessary
  1006. for this purpose.</p>
  1007. </li>
  1008. <li>
  1009. <p>Build and install it:</p>
  1010. <pre><code>make
  1011. make install
  1012. </code></pre>
  1013. </li>
  1014. </ol>
  1015. <h3 id="configuration"><a class="header" href="#configuration">Configuration</a></h3>
  1016. <ol>
  1017. <li>
  1018. <p>Create or edit the config file in <code>/etc/turnserver.conf</code>. The relevant
  1019. lines, with example values, are:</p>
  1020. <pre><code>use-auth-secret
  1021. static-auth-secret=[your secret key here]
  1022. realm=turn.myserver.org
  1023. </code></pre>
  1024. <p>See <code>turnserver.conf</code> for explanations of the options. One way to generate
  1025. the <code>static-auth-secret</code> is with <code>pwgen</code>:</p>
  1026. <pre><code>pwgen -s 64 1
  1027. </code></pre>
  1028. <p>A <code>realm</code> must be specified, but its value is somewhat arbitrary. (It is
  1029. sent to clients as part of the authentication flow.) It is conventional to
  1030. set it to be your server name.</p>
  1031. </li>
  1032. <li>
  1033. <p>You will most likely want to configure coturn to write logs somewhere. The
  1034. easiest way is normally to send them to the syslog:</p>
  1035. <pre><code>syslog
  1036. </code></pre>
  1037. <p>(in which case, the logs will be available via <code>journalctl -u coturn</code> on a
  1038. systemd system). Alternatively, coturn can be configured to write to a
  1039. logfile - check the example config file supplied with coturn.</p>
  1040. </li>
  1041. <li>
  1042. <p>Consider your security settings. TURN lets users request a relay which will
  1043. connect to arbitrary IP addresses and ports. The following configuration is
  1044. suggested as a minimum starting point:</p>
  1045. <pre><code># VoIP traffic is all UDP. There is no reason to let users connect to arbitrary TCP endpoints via the relay.
  1046. no-tcp-relay
  1047. # don't let the relay ever try to connect to private IP address ranges within your network (if any)
  1048. # given the turn server is likely behind your firewall, remember to include any privileged public IPs too.
  1049. denied-peer-ip=10.0.0.0-10.255.255.255
  1050. denied-peer-ip=192.168.0.0-192.168.255.255
  1051. denied-peer-ip=172.16.0.0-172.31.255.255
  1052. # special case the turn server itself so that client-&gt;TURN-&gt;TURN-&gt;client flows work
  1053. allowed-peer-ip=10.0.0.1
  1054. # consider whether you want to limit the quota of relayed streams per user (or total) to avoid risk of DoS.
  1055. user-quota=12 # 4 streams per video call, so 12 streams = 3 simultaneous relayed calls per user.
  1056. total-quota=1200
  1057. </code></pre>
  1058. </li>
  1059. <li>
  1060. <p>Also consider supporting TLS/DTLS. To do this, add the following settings
  1061. to <code>turnserver.conf</code>:</p>
  1062. <pre><code># TLS certificates, including intermediate certs.
  1063. # For Let's Encrypt certificates, use `fullchain.pem` here.
  1064. cert=/path/to/fullchain.pem
  1065. # TLS private key file
  1066. pkey=/path/to/privkey.pem
  1067. </code></pre>
  1068. <p>In this case, replace the <code>turn:</code> schemes in the <code>turn_uri</code> settings below
  1069. with <code>turns:</code>.</p>
  1070. <p>We recommend that you only try to set up TLS/DTLS once you have set up a
  1071. basic installation and got it working.</p>
  1072. </li>
  1073. <li>
  1074. <p>Ensure your firewall allows traffic into the TURN server on the ports
  1075. you've configured it to listen on (By default: 3478 and 5349 for TURN
  1076. traffic (remember to allow both TCP and UDP traffic), and ports 49152-65535
  1077. for the UDP relay.)</p>
  1078. </li>
  1079. <li>
  1080. <p>We do not recommend running a TURN server behind NAT, and are not aware of
  1081. anyone doing so successfully.</p>
  1082. <p>If you want to try it anyway, you will at least need to tell coturn its
  1083. external IP address:</p>
  1084. <pre><code>external-ip=192.88.99.1
  1085. </code></pre>
  1086. <p>... and your NAT gateway must forward all of the relayed ports directly
  1087. (eg, port 56789 on the external IP must be always be forwarded to port
  1088. 56789 on the internal IP).</p>
  1089. <p>If you get this working, let us know!</p>
  1090. </li>
  1091. <li>
  1092. <p>(Re)start the turn server:</p>
  1093. <ul>
  1094. <li>
  1095. <p>If you used the Debian package (or have set up a systemd unit yourself):</p>
  1096. <pre><code class="language-sh">systemctl restart coturn
  1097. </code></pre>
  1098. </li>
  1099. <li>
  1100. <p>If you installed from source:</p>
  1101. <pre><code class="language-sh">bin/turnserver -o
  1102. </code></pre>
  1103. </li>
  1104. </ul>
  1105. </li>
  1106. </ol>
  1107. <h2 id="synapse-setup"><a class="header" href="#synapse-setup">Synapse setup</a></h2>
  1108. <p>Your home server configuration file needs the following extra keys:</p>
  1109. <ol>
  1110. <li>&quot;<code>turn_uris</code>&quot;: This needs to be a yaml list of public-facing URIs
  1111. for your TURN server to be given out to your clients. Add separate
  1112. entries for each transport your TURN server supports.</li>
  1113. <li>&quot;<code>turn_shared_secret</code>&quot;: This is the secret shared between your
  1114. Home server and your TURN server, so you should set it to the same
  1115. string you used in turnserver.conf.</li>
  1116. <li>&quot;<code>turn_user_lifetime</code>&quot;: This is the amount of time credentials
  1117. generated by your Home Server are valid for (in milliseconds).
  1118. Shorter times offer less potential for abuse at the expense of
  1119. increased traffic between web clients and your home server to
  1120. refresh credentials. The TURN REST API specification recommends
  1121. one day (86400000).</li>
  1122. <li>&quot;<code>turn_allow_guests</code>&quot;: Whether to allow guest users to use the
  1123. TURN server. This is enabled by default, as otherwise VoIP will
  1124. not work reliably for guests. However, it does introduce a
  1125. security risk as it lets guests connect to arbitrary endpoints
  1126. without having gone through a CAPTCHA or similar to register a
  1127. real account.</li>
  1128. </ol>
  1129. <p>As an example, here is the relevant section of the config file for <code>matrix.org</code>. The
  1130. <code>turn_uris</code> are appropriate for TURN servers listening on the default ports, with no TLS.</p>
  1131. <pre><code>turn_uris: [ &quot;turn:turn.matrix.org?transport=udp&quot;, &quot;turn:turn.matrix.org?transport=tcp&quot; ]
  1132. turn_shared_secret: &quot;n0t4ctuAllymatr1Xd0TorgSshar3d5ecret4obvIousreAsons&quot;
  1133. turn_user_lifetime: 86400000
  1134. turn_allow_guests: True
  1135. </code></pre>
  1136. <p>After updating the homeserver configuration, you must restart synapse:</p>
  1137. <ul>
  1138. <li>If you use synctl:
  1139. <pre><code class="language-sh">cd /where/you/run/synapse
  1140. ./synctl restart
  1141. </code></pre>
  1142. </li>
  1143. <li>If you use systemd:
  1144. <pre><code>systemctl restart matrix-synapse.service
  1145. </code></pre>
  1146. </li>
  1147. </ul>
  1148. <p>... and then reload any clients (or wait an hour for them to refresh their
  1149. settings).</p>
  1150. <h2 id="troubleshooting-1"><a class="header" href="#troubleshooting-1">Troubleshooting</a></h2>
  1151. <p>The normal symptoms of a misconfigured TURN server are that calls between
  1152. devices on different networks ring, but get stuck at &quot;call
  1153. connecting&quot;. Unfortunately, troubleshooting this can be tricky.</p>
  1154. <p>Here are a few things to try:</p>
  1155. <ul>
  1156. <li>
  1157. <p>Check that your TURN server is not behind NAT. As above, we're not aware of
  1158. anyone who has successfully set this up.</p>
  1159. </li>
  1160. <li>
  1161. <p>Check that you have opened your firewall to allow TCP and UDP traffic to the
  1162. TURN ports (normally 3478 and 5479).</p>
  1163. </li>
  1164. <li>
  1165. <p>Check that you have opened your firewall to allow UDP traffic to the UDP
  1166. relay ports (49152-65535 by default).</p>
  1167. </li>
  1168. <li>
  1169. <p>Some WebRTC implementations (notably, that of Google Chrome) appear to get
  1170. confused by TURN servers which are reachable over IPv6 (this appears to be
  1171. an unexpected side-effect of its handling of multiple IP addresses as
  1172. defined by
  1173. <a href="https://tools.ietf.org/html/draft-ietf-rtcweb-ip-handling-12"><code>draft-ietf-rtcweb-ip-handling</code></a>).</p>
  1174. <p>Try removing any AAAA records for your TURN server, so that it is only
  1175. reachable over IPv4.</p>
  1176. </li>
  1177. <li>
  1178. <p>Enable more verbose logging in coturn via the <code>verbose</code> setting:</p>
  1179. <pre><code>verbose
  1180. </code></pre>
  1181. <p>... and then see if there are any clues in its logs.</p>
  1182. </li>
  1183. <li>
  1184. <p>If you are using a browser-based client under Chrome, check
  1185. <code>chrome://webrtc-internals/</code> for insights into the internals of the
  1186. negotiation. On Firefox, check the &quot;Connection Log&quot; on <code>about:webrtc</code>.</p>
  1187. <p>(Understanding the output is beyond the scope of this document!)</p>
  1188. </li>
  1189. <li>
  1190. <p>You can test your Matrix homeserver TURN setup with https://test.voip.librepush.net/.
  1191. Note that this test is not fully reliable yet, so don't be discouraged if
  1192. the test fails.
  1193. <a href="https://github.com/matrix-org/voip-tester">Here</a> is the github repo of the
  1194. source of the tester, where you can file bug reports.</p>
  1195. </li>
  1196. <li>
  1197. <p>There is a WebRTC test tool at
  1198. https://webrtc.github.io/samples/src/content/peerconnection/trickle-ice/. To
  1199. use it, you will need a username/password for your TURN server. You can
  1200. either:</p>
  1201. <ul>
  1202. <li>
  1203. <p>look for the <code>GET /_matrix/client/r0/voip/turnServer</code> request made by a
  1204. matrix client to your homeserver in your browser's network inspector. In
  1205. the response you should see <code>username</code> and <code>password</code>. Or:</p>
  1206. </li>
  1207. <li>
  1208. <p>Use the following shell commands:</p>
  1209. <pre><code class="language-sh">secret=staticAuthSecretHere
  1210. u=$((`date +%s` + 3600)):test
  1211. p=$(echo -n $u | openssl dgst -hmac $secret -sha1 -binary | base64)
  1212. echo -e &quot;username: $u\npassword: $p&quot;
  1213. </code></pre>
  1214. <p>Or:</p>
  1215. </li>
  1216. <li>
  1217. <p>Temporarily configure coturn to accept a static username/password. To do
  1218. this, comment out <code>use-auth-secret</code> and <code>static-auth-secret</code> and add the
  1219. following:</p>
  1220. <pre><code>lt-cred-mech
  1221. user=username:password
  1222. </code></pre>
  1223. <p><strong>Note</strong>: these settings will not take effect unless <code>use-auth-secret</code>
  1224. and <code>static-auth-secret</code> are disabled.</p>
  1225. <p>Restart coturn after changing the configuration file.</p>
  1226. <p>Remember to restore the original settings to go back to testing with
  1227. Matrix clients!</p>
  1228. </li>
  1229. </ul>
  1230. <p>If the TURN server is working correctly, you should see at least one <code>relay</code>
  1231. entry in the results.</p>
  1232. </li>
  1233. </ul>
  1234. <div style="break-before: page; page-break-before: always;"></div><h1 id="delegation"><a class="header" href="#delegation">Delegation</a></h1>
  1235. <p>By default, other homeservers will expect to be able to reach yours via
  1236. your <code>server_name</code>, on port 8448. For example, if you set your <code>server_name</code>
  1237. to <code>example.com</code> (so that your user names look like <code>@user:example.com</code>),
  1238. other servers will try to connect to yours at <code>https://example.com:8448/</code>.</p>
  1239. <p>Delegation is a Matrix feature allowing a homeserver admin to retain a
  1240. <code>server_name</code> of <code>example.com</code> so that user IDs, room aliases, etc continue
  1241. to look like <code>*:example.com</code>, whilst having federation traffic routed
  1242. to a different server and/or port (e.g. <code>synapse.example.com:443</code>).</p>
  1243. <h2 id="well-known-delegation"><a class="header" href="#well-known-delegation">.well-known delegation</a></h2>
  1244. <p>To use this method, you need to be able to alter the
  1245. <code>server_name</code> 's https server to serve the <code>/.well-known/matrix/server</code>
  1246. URL. Having an active server (with a valid TLS certificate) serving your
  1247. <code>server_name</code> domain is out of the scope of this documentation.</p>
  1248. <p>The URL <code>https://&lt;server_name&gt;/.well-known/matrix/server</code> should
  1249. return a JSON structure containing the key <code>m.server</code> like so:</p>
  1250. <pre><code class="language-json">{
  1251. &quot;m.server&quot;: &quot;&lt;synapse.server.name&gt;[:&lt;yourport&gt;]&quot;
  1252. }
  1253. </code></pre>
  1254. <p>In our example, this would mean that URL <code>https://example.com/.well-known/matrix/server</code>
  1255. should return:</p>
  1256. <pre><code class="language-json">{
  1257. &quot;m.server&quot;: &quot;synapse.example.com:443&quot;
  1258. }
  1259. </code></pre>
  1260. <p>Note, specifying a port is optional. If no port is specified, then it defaults
  1261. to 8448.</p>
  1262. <p>With .well-known delegation, federating servers will check for a valid TLS
  1263. certificate for the delegated hostname (in our example: <code>synapse.example.com</code>).</p>
  1264. <h2 id="srv-dns-record-delegation"><a class="header" href="#srv-dns-record-delegation">SRV DNS record delegation</a></h2>
  1265. <p>It is also possible to do delegation using a SRV DNS record. However, that is
  1266. considered an advanced topic since it's a bit complex to set up, and <code>.well-known</code>
  1267. delegation is already enough in most cases.</p>
  1268. <p>However, if you really need it, you can find some documentation on how such a
  1269. record should look like and how Synapse will use it in <a href="https://matrix.org/docs/spec/server_server/latest#resolving-server-names">the Matrix
  1270. specification</a>.</p>
  1271. <h2 id="delegation-faq"><a class="header" href="#delegation-faq">Delegation FAQ</a></h2>
  1272. <h3 id="when-do-i-need-delegation"><a class="header" href="#when-do-i-need-delegation">When do I need delegation?</a></h3>
  1273. <p>If your homeserver's APIs are accessible on the default federation port (8448)
  1274. and the domain your <code>server_name</code> points to, you do not need any delegation.</p>
  1275. <p>For instance, if you registered <code>example.com</code> and pointed its DNS A record at a
  1276. fresh server, you could install Synapse on that host, giving it a <code>server_name</code>
  1277. of <code>example.com</code>, and once a reverse proxy has been set up to proxy all requests
  1278. sent to the port <code>8448</code> and serve TLS certificates for <code>example.com</code>, you
  1279. wouldn't need any delegation set up.</p>
  1280. <p><strong>However</strong>, if your homeserver's APIs aren't accessible on port 8448 and on the
  1281. domain <code>server_name</code> points to, you will need to let other servers know how to
  1282. find it using delegation.</p>
  1283. <h3 id="do-you-still-recommend-against-using-a-reverse-proxy-on-the-federation-port"><a class="header" href="#do-you-still-recommend-against-using-a-reverse-proxy-on-the-federation-port">Do you still recommend against using a reverse proxy on the federation port?</a></h3>
  1284. <p>We no longer actively recommend against using a reverse proxy. Many admins will
  1285. find it easier to direct federation traffic to a reverse proxy and manage their
  1286. own TLS certificates, and this is a supported configuration.</p>
  1287. <p>See <a href="reverse_proxy.html">reverse_proxy.md</a> for information on setting up a
  1288. reverse proxy.</p>
  1289. <h3 id="do-i-still-need-to-give-my-tls-certificates-to-synapse-if-i-am-using-a-reverse-proxy"><a class="header" href="#do-i-still-need-to-give-my-tls-certificates-to-synapse-if-i-am-using-a-reverse-proxy">Do I still need to give my TLS certificates to Synapse if I am using a reverse proxy?</a></h3>
  1290. <p>This is no longer necessary. If you are using a reverse proxy for all of your
  1291. TLS traffic, then you can set <code>no_tls: True</code> in the Synapse config.</p>
  1292. <p>In that case, the only reason Synapse needs the certificate is to populate a legacy
  1293. <code>tls_fingerprints</code> field in the federation API. This is ignored by Synapse 0.99.0
  1294. and later, and the only time pre-0.99 Synapses will check it is when attempting to
  1295. fetch the server keys - and generally this is delegated via <code>matrix.org</code>, which
  1296. is running a modern version of Synapse.</p>
  1297. <h3 id="do-i-need-the-same-certificate-for-the-client-and-federation-port"><a class="header" href="#do-i-need-the-same-certificate-for-the-client-and-federation-port">Do I need the same certificate for the client and federation port?</a></h3>
  1298. <p>No. There is nothing stopping you from using different certificates,
  1299. particularly if you are using a reverse proxy.</p>
  1300. <div style="break-before: page; page-break-before: always;"></div><h1 id="upgrading-synapse"><a class="header" href="#upgrading-synapse">Upgrading Synapse</a></h1>
  1301. <p>Before upgrading check if any special steps are required to upgrade from
  1302. the version you currently have installed to the current version of
  1303. Synapse. The extra instructions that may be required are listed later in
  1304. this document.</p>
  1305. <ul>
  1306. <li>
  1307. <p>Check that your versions of Python and PostgreSQL are still
  1308. supported.</p>
  1309. <p>Synapse follows upstream lifecycles for <a href="https://endoflife.date/python">Python</a> and
  1310. <a href="https://endoflife.date/postgresql">PostgreSQL</a>, and removes support for versions
  1311. which are no longer maintained.</p>
  1312. <p>The website <a href="https://endoflife.date">https://endoflife.date</a> also offers convenient
  1313. summaries.</p>
  1314. </li>
  1315. <li>
  1316. <p>If Synapse was installed using <a href="setup/installation.html#prebuilt-packages">prebuilt
  1317. packages</a>, you will need to follow the
  1318. normal process for upgrading those packages.</p>
  1319. </li>
  1320. <li>
  1321. <p>If Synapse was installed from source, then:</p>
  1322. <ol>
  1323. <li>
  1324. <p>Activate the virtualenv before upgrading. For example, if
  1325. Synapse is installed in a virtualenv in <code>~/synapse/env</code> then
  1326. run:</p>
  1327. <pre><code class="language-bash">source ~/synapse/env/bin/activate
  1328. </code></pre>
  1329. </li>
  1330. <li>
  1331. <p>If Synapse was installed using pip then upgrade to the latest
  1332. version by running:</p>
  1333. <pre><code class="language-bash">pip install --upgrade matrix-synapse
  1334. </code></pre>
  1335. <p>If Synapse was installed using git then upgrade to the latest
  1336. version by running:</p>
  1337. <pre><code class="language-bash">git pull
  1338. pip install --upgrade .
  1339. </code></pre>
  1340. </li>
  1341. <li>
  1342. <p>Restart Synapse:</p>
  1343. <pre><code class="language-bash">./synctl restart
  1344. </code></pre>
  1345. </li>
  1346. </ol>
  1347. </li>
  1348. </ul>
  1349. <p>To check whether your update was successful, you can check the running
  1350. server version with:</p>
  1351. <pre><code class="language-bash"># you may need to replace 'localhost:8008' if synapse is not configured
  1352. # to listen on port 8008.
  1353. curl http://localhost:8008/_synapse/admin/v1/server_version
  1354. </code></pre>
  1355. <h2 id="rolling-back-to-older-versions"><a class="header" href="#rolling-back-to-older-versions">Rolling back to older versions</a></h2>
  1356. <p>Rolling back to previous releases can be difficult, due to database
  1357. schema changes between releases. Where we have been able to test the
  1358. rollback process, this will be noted below.</p>
  1359. <p>In general, you will need to undo any changes made during the upgrade
  1360. process, for example:</p>
  1361. <ul>
  1362. <li>
  1363. <p>pip:</p>
  1364. <pre><code class="language-bash">source env/bin/activate
  1365. # replace `1.3.0` accordingly:
  1366. pip install matrix-synapse==1.3.0
  1367. </code></pre>
  1368. </li>
  1369. <li>
  1370. <p>Debian:</p>
  1371. <pre><code class="language-bash"># replace `1.3.0` and `stretch` accordingly:
  1372. wget https://packages.matrix.org/debian/pool/main/m/matrix-synapse-py3/matrix-synapse-py3_1.3.0+stretch1_amd64.deb
  1373. dpkg -i matrix-synapse-py3_1.3.0+stretch1_amd64.deb
  1374. </code></pre>
  1375. </li>
  1376. </ul>
  1377. <h1 id="upgrading-to-v1380"><a class="header" href="#upgrading-to-v1380">Upgrading to v1.38.0</a></h1>
  1378. <h2 id="re-indexing-of-events-table-on-postgres-databases"><a class="header" href="#re-indexing-of-events-table-on-postgres-databases">Re-indexing of <code>events</code> table on Postgres databases</a></h2>
  1379. <p>This release includes a database schema update which requires re-indexing one of
  1380. the larger tables in the database, <code>events</code>. This could result in increased
  1381. disk I/O for several hours or days after upgrading while the migration
  1382. completes. Furthermore, because we have to keep the old indexes until the new
  1383. indexes are ready, it could result in a significant, temporary, increase in
  1384. disk space.</p>
  1385. <p>To get a rough idea of the disk space required, check the current size of one
  1386. of the indexes. For example, from a <code>psql</code> shell, run the following sql:</p>
  1387. <pre><code class="language-sql">SELECT pg_size_pretty(pg_relation_size('events_order_room'));
  1388. </code></pre>
  1389. <p>We need to rebuild <strong>four</strong> indexes, so you will need to multiply this result
  1390. by four to give an estimate of the disk space required. For example, on one
  1391. particular server:</p>
  1392. <pre><code>synapse=# select pg_size_pretty(pg_relation_size('events_order_room'));
  1393. pg_size_pretty
  1394. ----------------
  1395. 288 MB
  1396. (1 row)
  1397. </code></pre>
  1398. <p>On this server, it would be wise to ensure that at least 1152MB are free.</p>
  1399. <p>The additional disk space will be freed once the migration completes.</p>
  1400. <p>SQLite databases are unaffected by this change.</p>
  1401. <h1 id="upgrading-to-v1370"><a class="header" href="#upgrading-to-v1370">Upgrading to v1.37.0</a></h1>
  1402. <h2 id="deprecation-of-the-current-spam-checker-interface"><a class="header" href="#deprecation-of-the-current-spam-checker-interface">Deprecation of the current spam checker interface</a></h2>
  1403. <p>The current spam checker interface is deprecated in favour of a new generic modules system.
  1404. Authors of spam checker modules can refer to <a href="https://matrix-org.github.io/synapse/develop/modules.html#porting-an-existing-module-that-uses-the-old-interface">this
  1405. documentation</a>
  1406. to update their modules. Synapse administrators can refer to <a href="https://matrix-org.github.io/synapse/develop/modules.html#using-modules">this
  1407. documentation</a>
  1408. to update their configuration once the modules they are using have been updated.</p>
  1409. <p>We plan to remove support for the current spam checker interface in August 2021.</p>
  1410. <p>More module interfaces will be ported over to this new generic system in future versions
  1411. of Synapse.</p>
  1412. <h1 id="upgrading-to-v1340"><a class="header" href="#upgrading-to-v1340">Upgrading to v1.34.0</a></h1>
  1413. <h2 id="room_invite_state_types-configuration-setting"><a class="header" href="#room_invite_state_types-configuration-setting"><code>room_invite_state_types</code> configuration setting</a></h2>
  1414. <p>The <code>room_invite_state_types</code> configuration setting has been deprecated
  1415. and replaced with <code>room_prejoin_state</code>. See the <a href="https://github.com/matrix-org/synapse/blob/v1.34.0/docs/sample_config.yaml#L1515">sample configuration
  1416. file</a>.</p>
  1417. <p>If you have set <code>room_invite_state_types</code> to the default value you
  1418. should simply remove it from your configuration file. The default value
  1419. used to be:</p>
  1420. <pre><code class="language-yaml">room_invite_state_types:
  1421. - &quot;m.room.join_rules&quot;
  1422. - &quot;m.room.canonical_alias&quot;
  1423. - &quot;m.room.avatar&quot;
  1424. - &quot;m.room.encryption&quot;
  1425. - &quot;m.room.name&quot;
  1426. </code></pre>
  1427. <p>If you have customised this value, you should remove
  1428. <code>room_invite_state_types</code> and configure <code>room_prejoin_state</code> instead.</p>
  1429. <h1 id="upgrading-to-v1330"><a class="header" href="#upgrading-to-v1330">Upgrading to v1.33.0</a></h1>
  1430. <h2 id="account-validity-html-templates-can-now-display-a-users-expiration-date"><a class="header" href="#account-validity-html-templates-can-now-display-a-users-expiration-date">Account Validity HTML templates can now display a user's expiration date</a></h2>
  1431. <p>This may affect you if you have enabled the account validity feature,
  1432. and have made use of a custom HTML template specified by the
  1433. <code>account_validity.template_dir</code> or
  1434. <code>account_validity.account_renewed_html_path</code> Synapse config options.</p>
  1435. <p>The template can now accept an <code>expiration_ts</code> variable, which
  1436. represents the unix timestamp in milliseconds for the future date of
  1437. which their account has been renewed until. See the <a href="https://github.com/matrix-org/synapse/blob/release-v1.33.0/synapse/res/templates/account_renewed.html">default
  1438. template</a>
  1439. for an example of usage.</p>
  1440. <p>ALso note that a new HTML template, <code>account_previously_renewed.html</code>,
  1441. has been added. This is is shown to users when they attempt to renew
  1442. their account with a valid renewal token that has already been used
  1443. before. The default template contents can been found
  1444. <a href="https://github.com/matrix-org/synapse/blob/release-v1.33.0/synapse/res/templates/account_previously_renewed.html">here</a>,
  1445. and can also accept an <code>expiration_ts</code> variable. This template replaces
  1446. the error message users would previously see upon attempting to use a
  1447. valid renewal token more than once.</p>
  1448. <h1 id="upgrading-to-v1320"><a class="header" href="#upgrading-to-v1320">Upgrading to v1.32.0</a></h1>
  1449. <h2 id="regression-causing-connected-prometheus-instances-to-become-overwhelmed"><a class="header" href="#regression-causing-connected-prometheus-instances-to-become-overwhelmed">Regression causing connected Prometheus instances to become overwhelmed</a></h2>
  1450. <p>This release introduces <a href="https://github.com/matrix-org/synapse/issues/9853">a
  1451. regression</a> that can
  1452. overwhelm connected Prometheus instances. This issue is not present in
  1453. Synapse v1.32.0rc1.</p>
  1454. <p>If you have been affected, please downgrade to 1.31.0. You then may need
  1455. to remove excess writeahead logs in order for Prometheus to recover.
  1456. Instructions for doing so are provided
  1457. <a href="https://github.com/matrix-org/synapse/pull/9854#issuecomment-823472183">here</a>.</p>
  1458. <h2 id="dropping-support-for-old-python-postgres-and-sqlite-versions"><a class="header" href="#dropping-support-for-old-python-postgres-and-sqlite-versions">Dropping support for old Python, Postgres and SQLite versions</a></h2>
  1459. <p>In line with our <a href="https://github.com/matrix-org/synapse/blob/release-v1.32.0/docs/deprecation_policy.md">deprecation
  1460. policy</a>,
  1461. we've dropped support for Python 3.5 and PostgreSQL 9.5, as they are no
  1462. longer supported upstream.</p>
  1463. <p>This release of Synapse requires Python 3.6+ and PostgresSQL 9.6+ or
  1464. SQLite 3.22+.</p>
  1465. <h2 id="removal-of-old-list-accounts-admin-api"><a class="header" href="#removal-of-old-list-accounts-admin-api">Removal of old List Accounts Admin API</a></h2>
  1466. <p>The deprecated v1 &quot;list accounts&quot; admin API
  1467. (<code>GET /_synapse/admin/v1/users/&lt;user_id&gt;</code>) has been removed in this
  1468. version.</p>
  1469. <p>The <a href="https://github.com/matrix-org/synapse/blob/master/docs/admin_api/user_admin_api.rst#list-accounts">v2 list accounts
  1470. API</a>
  1471. has been available since Synapse 1.7.0 (2019-12-13), and is accessible
  1472. under <code>GET /_synapse/admin/v2/users</code>.</p>
  1473. <p>The deprecation of the old endpoint was announced with Synapse 1.28.0
  1474. (released on 2021-02-25).</p>
  1475. <h2 id="application-services-must-use-type-mloginapplication_service-when-registering-users"><a class="header" href="#application-services-must-use-type-mloginapplication_service-when-registering-users">Application Services must use type <code>m.login.application_service</code> when registering users</a></h2>
  1476. <p>In compliance with the <a href="https://matrix.org/docs/spec/application_service/r0.1.2#server-admin-style-permissions">Application Service
  1477. spec</a>,
  1478. Application Services are now required to use the
  1479. <code>m.login.application_service</code> type when registering users via the
  1480. <code>/_matrix/client/r0/register</code> endpoint. This behaviour was deprecated in
  1481. Synapse v1.30.0.</p>
  1482. <p>Please ensure your Application Services are up to date.</p>
  1483. <h1 id="upgrading-to-v1290"><a class="header" href="#upgrading-to-v1290">Upgrading to v1.29.0</a></h1>
  1484. <h2 id="requirement-for-x-forwarded-proto-header"><a class="header" href="#requirement-for-x-forwarded-proto-header">Requirement for X-Forwarded-Proto header</a></h2>
  1485. <p>When using Synapse with a reverse proxy (in particular, when using the
  1486. [x_forwarded]{.title-ref} option on an HTTP listener), Synapse now
  1487. expects to receive an [X-Forwarded-Proto]{.title-ref} header on incoming
  1488. HTTP requests. If it is not set, Synapse will log a warning on each
  1489. received request.</p>
  1490. <p>To avoid the warning, administrators using a reverse proxy should ensure
  1491. that the reverse proxy sets [X-Forwarded-Proto]{.title-ref} header to
  1492. [https]{.title-ref} or [http]{.title-ref} to indicate the protocol used
  1493. by the client.</p>
  1494. <p>Synapse also requires the [Host]{.title-ref} header to be preserved.</p>
  1495. <p>See the <a href="../reverse_proxy.html">reverse proxy documentation</a>, where the
  1496. example configurations have been updated to show how to set these
  1497. headers.</p>
  1498. <p>(Users of <a href="https://caddyserver.com/">Caddy</a> are unaffected, since we
  1499. believe it sets [X-Forwarded-Proto]{.title-ref} by default.)</p>
  1500. <h1 id="upgrading-to-v1270"><a class="header" href="#upgrading-to-v1270">Upgrading to v1.27.0</a></h1>
  1501. <h2 id="changes-to-callback-uri-for-oauth2--openid-connect-and-saml2"><a class="header" href="#changes-to-callback-uri-for-oauth2--openid-connect-and-saml2">Changes to callback URI for OAuth2 / OpenID Connect and SAML2</a></h2>
  1502. <p>This version changes the URI used for callbacks from OAuth2 and SAML2
  1503. identity providers:</p>
  1504. <ul>
  1505. <li>
  1506. <p>If your server is configured for single sign-on via an OpenID
  1507. Connect or OAuth2 identity provider, you will need to add
  1508. <code>[synapse public baseurl]/_synapse/client/oidc/callback</code> to the list
  1509. of permitted &quot;redirect URIs&quot; at the identity provider.</p>
  1510. <p>See the <a href="../openid.html">OpenID docs</a> for more information on setting
  1511. up OpenID Connect.</p>
  1512. </li>
  1513. <li>
  1514. <p>If your server is configured for single sign-on via a SAML2 identity
  1515. provider, you will need to add
  1516. <code>[synapse public baseurl]/_synapse/client/saml2/authn_response</code> as a
  1517. permitted &quot;ACS location&quot; (also known as &quot;allowed callback URLs&quot;)
  1518. at the identity provider.</p>
  1519. <p>The &quot;Issuer&quot; in the &quot;AuthnRequest&quot; to the SAML2 identity
  1520. provider is also updated to
  1521. <code>[synapse public baseurl]/_synapse/client/saml2/metadata.xml</code>. If
  1522. your SAML2 identity provider uses this property to validate or
  1523. otherwise identify Synapse, its configuration will need to be
  1524. updated to use the new URL. Alternatively you could create a new,
  1525. separate &quot;EntityDescriptor&quot; in your SAML2 identity provider with
  1526. the new URLs and leave the URLs in the existing &quot;EntityDescriptor&quot;
  1527. as they were.</p>
  1528. </li>
  1529. </ul>
  1530. <h2 id="changes-to-html-templates"><a class="header" href="#changes-to-html-templates">Changes to HTML templates</a></h2>
  1531. <p>The HTML templates for SSO and email notifications now have <a href="https://jinja.palletsprojects.com/en/2.11.x/api/#autoescaping">Jinja2's
  1532. autoescape</a>
  1533. enabled for files ending in <code>.html</code>, <code>.htm</code>, and <code>.xml</code>. If you have
  1534. customised these templates and see issues when viewing them you might
  1535. need to update them. It is expected that most configurations will need
  1536. no changes.</p>
  1537. <p>If you have customised the templates <em>names</em> for these templates, it is
  1538. recommended to verify they end in <code>.html</code> to ensure autoescape is
  1539. enabled.</p>
  1540. <p>The above applies to the following templates:</p>
  1541. <ul>
  1542. <li><code>add_threepid.html</code></li>
  1543. <li><code>add_threepid_failure.html</code></li>
  1544. <li><code>add_threepid_success.html</code></li>
  1545. <li><code>notice_expiry.html</code></li>
  1546. <li><code>notice_expiry.html</code></li>
  1547. <li><code>notif_mail.html</code> (which, by default, includes <code>room.html</code> and
  1548. <code>notif.html</code>)</li>
  1549. <li><code>password_reset.html</code></li>
  1550. <li><code>password_reset_confirmation.html</code></li>
  1551. <li><code>password_reset_failure.html</code></li>
  1552. <li><code>password_reset_success.html</code></li>
  1553. <li><code>registration.html</code></li>
  1554. <li><code>registration_failure.html</code></li>
  1555. <li><code>registration_success.html</code></li>
  1556. <li><code>sso_account_deactivated.html</code></li>
  1557. <li><code>sso_auth_bad_user.html</code></li>
  1558. <li><code>sso_auth_confirm.html</code></li>
  1559. <li><code>sso_auth_success.html</code></li>
  1560. <li><code>sso_error.html</code></li>
  1561. <li><code>sso_login_idp_picker.html</code></li>
  1562. <li><code>sso_redirect_confirm.html</code></li>
  1563. </ul>
  1564. <h1 id="upgrading-to-v1260"><a class="header" href="#upgrading-to-v1260">Upgrading to v1.26.0</a></h1>
  1565. <h2 id="rolling-back-to-v1250-after-a-failed-upgrade"><a class="header" href="#rolling-back-to-v1250-after-a-failed-upgrade">Rolling back to v1.25.0 after a failed upgrade</a></h2>
  1566. <p>v1.26.0 includes a lot of large changes. If something problematic
  1567. occurs, you may want to roll-back to a previous version of Synapse.
  1568. Because v1.26.0 also includes a new database schema version, reverting
  1569. that version is also required alongside the generic rollback
  1570. instructions mentioned above. In short, to roll back to v1.25.0 you need
  1571. to:</p>
  1572. <ol>
  1573. <li>
  1574. <p>Stop the server</p>
  1575. </li>
  1576. <li>
  1577. <p>Decrease the schema version in the database:</p>
  1578. <pre><code class="language-sql">UPDATE schema_version SET version = 58;
  1579. </code></pre>
  1580. </li>
  1581. <li>
  1582. <p>Delete the ignored users &amp; chain cover data:</p>
  1583. <pre><code class="language-sql">DROP TABLE IF EXISTS ignored_users;
  1584. UPDATE rooms SET has_auth_chain_index = false;
  1585. </code></pre>
  1586. <p>For PostgreSQL run:</p>
  1587. <pre><code class="language-sql">TRUNCATE event_auth_chain_links;
  1588. TRUNCATE event_auth_chains;
  1589. </code></pre>
  1590. <p>For SQLite run:</p>
  1591. <pre><code class="language-sql">DELETE FROM event_auth_chain_links;
  1592. DELETE FROM event_auth_chains;
  1593. </code></pre>
  1594. </li>
  1595. <li>
  1596. <p>Mark the deltas as not run (so they will re-run on upgrade).</p>
  1597. <pre><code class="language-sql">DELETE FROM applied_schema_deltas WHERE version = 59 AND file = &quot;59/01ignored_user.py&quot;;
  1598. DELETE FROM applied_schema_deltas WHERE version = 59 AND file = &quot;59/06chain_cover_index.sql&quot;;
  1599. </code></pre>
  1600. </li>
  1601. <li>
  1602. <p>Downgrade Synapse by following the instructions for your
  1603. installation method in the &quot;Rolling back to older versions&quot;
  1604. section above.</p>
  1605. </li>
  1606. </ol>
  1607. <h1 id="upgrading-to-v1250"><a class="header" href="#upgrading-to-v1250">Upgrading to v1.25.0</a></h1>
  1608. <h2 id="last-release-supporting-python-35"><a class="header" href="#last-release-supporting-python-35">Last release supporting Python 3.5</a></h2>
  1609. <p>This is the last release of Synapse which guarantees support with Python
  1610. 3.5, which passed its upstream End of Life date several months ago.</p>
  1611. <p>We will attempt to maintain support through March 2021, but without
  1612. guarantees.</p>
  1613. <p>In the future, Synapse will follow upstream schedules for ending support
  1614. of older versions of Python and PostgreSQL. Please upgrade to at least
  1615. Python 3.6 and PostgreSQL 9.6 as soon as possible.</p>
  1616. <h2 id="blacklisting-ip-ranges"><a class="header" href="#blacklisting-ip-ranges">Blacklisting IP ranges</a></h2>
  1617. <p>Synapse v1.25.0 includes new settings, <code>ip_range_blacklist</code> and
  1618. <code>ip_range_whitelist</code>, for controlling outgoing requests from Synapse for
  1619. federation, identity servers, push, and for checking key validity for
  1620. third-party invite events. The previous setting,
  1621. <code>federation_ip_range_blacklist</code>, is deprecated. The new
  1622. <code>ip_range_blacklist</code> defaults to private IP ranges if it is not defined.</p>
  1623. <p>If you have never customised <code>federation_ip_range_blacklist</code> it is
  1624. recommended that you remove that setting.</p>
  1625. <p>If you have customised <code>federation_ip_range_blacklist</code> you should update
  1626. the setting name to <code>ip_range_blacklist</code>.</p>
  1627. <p>If you have a custom push server that is reached via private IP space
  1628. you may need to customise <code>ip_range_blacklist</code> or <code>ip_range_whitelist</code>.</p>
  1629. <h1 id="upgrading-to-v1240"><a class="header" href="#upgrading-to-v1240">Upgrading to v1.24.0</a></h1>
  1630. <h2 id="custom-openid-connect-mapping-provider-breaking-change"><a class="header" href="#custom-openid-connect-mapping-provider-breaking-change">Custom OpenID Connect mapping provider breaking change</a></h2>
  1631. <p>This release allows the OpenID Connect mapping provider to perform
  1632. normalisation of the localpart of the Matrix ID. This allows for the
  1633. mapping provider to specify different algorithms, instead of the
  1634. <a href="https://matrix.org/docs/spec/appendices#mapping-from-other-character-sets">default
  1635. way</a>.</p>
  1636. <p>If your Synapse configuration uses a custom mapping provider
  1637. ([oidc_config.user_mapping_provider.module]{.title-ref} is specified and
  1638. not equal to
  1639. [synapse.handlers.oidc_handler.JinjaOidcMappingProvider]{.title-ref})
  1640. then you <em>must</em> ensure that [map_user_attributes]{.title-ref} of the
  1641. mapping provider performs some normalisation of the
  1642. [localpart]{.title-ref} returned. To match previous behaviour you can
  1643. use the [map_username_to_mxid_localpart]{.title-ref} function provided
  1644. by Synapse. An example is shown below:</p>
  1645. <pre><code class="language-python">from synapse.types import map_username_to_mxid_localpart
  1646. class MyMappingProvider:
  1647. def map_user_attributes(self, userinfo, token):
  1648. # ... your custom logic ...
  1649. sso_user_id = ...
  1650. localpart = map_username_to_mxid_localpart(sso_user_id)
  1651. return {&quot;localpart&quot;: localpart}
  1652. </code></pre>
  1653. <h2 id="removal-historical-synapse-admin-api"><a class="header" href="#removal-historical-synapse-admin-api">Removal historical Synapse Admin API</a></h2>
  1654. <p>Historically, the Synapse Admin API has been accessible under:</p>
  1655. <ul>
  1656. <li><code>/_matrix/client/api/v1/admin</code></li>
  1657. <li><code>/_matrix/client/unstable/admin</code></li>
  1658. <li><code>/_matrix/client/r0/admin</code></li>
  1659. <li><code>/_synapse/admin/v1</code></li>
  1660. </ul>
  1661. <p>The endpoints with <code>/_matrix/client/*</code> prefixes have been removed as of
  1662. v1.24.0. The Admin API is now only accessible under:</p>
  1663. <ul>
  1664. <li><code>/_synapse/admin/v1</code></li>
  1665. </ul>
  1666. <p>The only exception is the [/admin/whois]{.title-ref} endpoint, which is
  1667. <a href="https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-admin-whois-userid">also available via the client-server
  1668. API</a>.</p>
  1669. <p>The deprecation of the old endpoints was announced with Synapse 1.20.0
  1670. (released on 2020-09-22) and makes it easier for homeserver admins to
  1671. lock down external access to the Admin API endpoints.</p>
  1672. <h1 id="upgrading-to-v1230"><a class="header" href="#upgrading-to-v1230">Upgrading to v1.23.0</a></h1>
  1673. <h2 id="structured-logging-configuration-breaking-changes"><a class="header" href="#structured-logging-configuration-breaking-changes">Structured logging configuration breaking changes</a></h2>
  1674. <p>This release deprecates use of the <code>structured: true</code> logging
  1675. configuration for structured logging. If your logging configuration
  1676. contains <code>structured: true</code> then it should be modified based on the
  1677. <a href="../structured_logging.html">structured logging
  1678. documentation</a>.</p>
  1679. <p>The <code>structured</code> and <code>drains</code> logging options are now deprecated and
  1680. should be replaced by standard logging configuration of <code>handlers</code> and
  1681. <code>formatters</code>.</p>
  1682. <p>A future will release of Synapse will make using <code>structured: true</code> an
  1683. error.</p>
  1684. <h1 id="upgrading-to-v1220"><a class="header" href="#upgrading-to-v1220">Upgrading to v1.22.0</a></h1>
  1685. <h2 id="thirdpartyeventrules-breaking-changes"><a class="header" href="#thirdpartyeventrules-breaking-changes">ThirdPartyEventRules breaking changes</a></h2>
  1686. <p>This release introduces a backwards-incompatible change to modules
  1687. making use of <code>ThirdPartyEventRules</code> in Synapse. If you make use of a
  1688. module defined under the <code>third_party_event_rules</code> config option, please
  1689. make sure it is updated to handle the below change:</p>
  1690. <p>The <code>http_client</code> argument is no longer passed to modules as they are
  1691. initialised. Instead, modules are expected to make use of the
  1692. <code>http_client</code> property on the <code>ModuleApi</code> class. Modules are now passed
  1693. a <code>module_api</code> argument during initialisation, which is an instance of
  1694. <code>ModuleApi</code>. <code>ModuleApi</code> instances have a <code>http_client</code> property which
  1695. acts the same as the <code>http_client</code> argument previously passed to
  1696. <code>ThirdPartyEventRules</code> modules.</p>
  1697. <h1 id="upgrading-to-v1210"><a class="header" href="#upgrading-to-v1210">Upgrading to v1.21.0</a></h1>
  1698. <h2 id="forwarding-_synapseclient-through-your-reverse-proxy"><a class="header" href="#forwarding-_synapseclient-through-your-reverse-proxy">Forwarding <code>/_synapse/client</code> through your reverse proxy</a></h2>
  1699. <p>The <a href="https://github.com/matrix-org/synapse/blob/develop/docs/reverse_proxy.md">reverse proxy
  1700. documentation</a>
  1701. has been updated to include reverse proxy directives for
  1702. <code>/_synapse/client/*</code> endpoints. As the user password reset flow now uses
  1703. endpoints under this prefix, <strong>you must update your reverse proxy
  1704. configurations for user password reset to work</strong>.</p>
  1705. <p>Additionally, note that the <a href="https://github.com/matrix-org/synapse/blob/develop/docs/workers.md">Synapse worker documentation</a> has been updated to</p>
  1706. <p>: state that the <code>/_synapse/client/password_reset/email/submit_token</code>
  1707. endpoint can be handled</p>
  1708. <p>by all workers. If you make use of Synapse's worker feature, please
  1709. update your reverse proxy configuration to reflect this change.</p>
  1710. <h2 id="new-html-templates"><a class="header" href="#new-html-templates">New HTML templates</a></h2>
  1711. <p>A new HTML template,
  1712. <a href="https://github.com/matrix-org/synapse/blob/develop/synapse/res/templates/password_reset_confirmation.html">password_reset_confirmation.html</a>,
  1713. has been added to the <code>synapse/res/templates</code> directory. If you are
  1714. using a custom template directory, you may want to copy the template
  1715. over and modify it.</p>
  1716. <p>Note that as of v1.20.0, templates do not need to be included in custom
  1717. template directories for Synapse to start. The default templates will be
  1718. used if a custom template cannot be found.</p>
  1719. <p>This page will appear to the user after clicking a password reset link
  1720. that has been emailed to them.</p>
  1721. <p>To complete password reset, the page must include a way to make a
  1722. [POST]{.title-ref} request to
  1723. <code>/_synapse/client/password_reset/{medium}/submit_token</code> with the query
  1724. parameters from the original link, presented as a URL-encoded form. See
  1725. the file itself for more details.</p>
  1726. <h2 id="updated-single-sign-on-html-templates"><a class="header" href="#updated-single-sign-on-html-templates">Updated Single Sign-on HTML Templates</a></h2>
  1727. <p>The <code>saml_error.html</code> template was removed from Synapse and replaced
  1728. with the <code>sso_error.html</code> template. If your Synapse is configured to use
  1729. SAML and a custom <code>sso_redirect_confirm_template_dir</code> configuration then
  1730. any customisations of the <code>saml_error.html</code> template will need to be
  1731. merged into the <code>sso_error.html</code> template. These templates are similar,
  1732. but the parameters are slightly different:</p>
  1733. <ul>
  1734. <li>The <code>msg</code> parameter should be renamed to <code>error_description</code>.</li>
  1735. <li>There is no longer a <code>code</code> parameter for the response code.</li>
  1736. <li>A string <code>error</code> parameter is available that includes a short hint
  1737. of why a user is seeing the error page.</li>
  1738. </ul>
  1739. <h1 id="upgrading-to-v1180"><a class="header" href="#upgrading-to-v1180">Upgrading to v1.18.0</a></h1>
  1740. <h2 id="docker--py3title-ref-suffix-will-be-removed-in-future-versions"><a class="header" href="#docker--py3title-ref-suffix-will-be-removed-in-future-versions">Docker [-py3]{.title-ref} suffix will be removed in future versions</a></h2>
  1741. <p>From 10th August 2020, we will no longer publish Docker images with the
  1742. [-py3]{.title-ref} tag suffix. The images tagged with the
  1743. [-py3]{.title-ref} suffix have been identical to the non-suffixed tags
  1744. since release 0.99.0, and the suffix is obsolete.</p>
  1745. <p>On 10th August, we will remove the [latest-py3]{.title-ref} tag.
  1746. Existing per-release tags (such as [v1.18.0-py3]{.title-ref}) will not
  1747. be removed, but no new [-py3]{.title-ref} tags will be added.</p>
  1748. <p>Scripts relying on the [-py3]{.title-ref} suffix will need to be
  1749. updated.</p>
  1750. <h2 id="redis-replication-is-now-recommended-in-lieu-of-tcp-replication"><a class="header" href="#redis-replication-is-now-recommended-in-lieu-of-tcp-replication">Redis replication is now recommended in lieu of TCP replication</a></h2>
  1751. <p>When setting up worker processes, we now recommend the use of a Redis
  1752. server for replication. <strong>The old direct TCP connection method is
  1753. deprecated and will be removed in a future release.</strong> See
  1754. <a href="../workers.html">workers</a> for more details.</p>
  1755. <h1 id="upgrading-to-v1140"><a class="header" href="#upgrading-to-v1140">Upgrading to v1.14.0</a></h1>
  1756. <p>This version includes a database update which is run as part of the
  1757. upgrade, and which may take a couple of minutes in the case of a large
  1758. server. Synapse will not respond to HTTP requests while this update is
  1759. taking place.</p>
  1760. <h1 id="upgrading-to-v1130"><a class="header" href="#upgrading-to-v1130">Upgrading to v1.13.0</a></h1>
  1761. <h2 id="incorrect-database-migration-in-old-synapse-versions"><a class="header" href="#incorrect-database-migration-in-old-synapse-versions">Incorrect database migration in old synapse versions</a></h2>
  1762. <p>A bug was introduced in Synapse 1.4.0 which could cause the room
  1763. directory to be incomplete or empty if Synapse was upgraded directly
  1764. from v1.2.1 or earlier, to versions between v1.4.0 and v1.12.x.</p>
  1765. <p>This will <em>not</em> be a problem for Synapse installations which were:</p>
  1766. <p>: - created at v1.4.0 or later,
  1767. - upgraded via v1.3.x, or
  1768. - upgraded straight from v1.2.1 or earlier to v1.13.0 or later.</p>
  1769. <p>If completeness of the room directory is a concern, installations which
  1770. are affected can be repaired as follows:</p>
  1771. <ol>
  1772. <li>
  1773. <p>Run the following sql from a [psql]{.title-ref} or
  1774. [sqlite3]{.title-ref} console:</p>
  1775. <pre><code class="language-sql">INSERT INTO background_updates (update_name, progress_json, depends_on) VALUES
  1776. ('populate_stats_process_rooms', '{}', 'current_state_events_membership');
  1777. INSERT INTO background_updates (update_name, progress_json, depends_on) VALUES
  1778. ('populate_stats_process_users', '{}', 'populate_stats_process_rooms');
  1779. </code></pre>
  1780. </li>
  1781. <li>
  1782. <p>Restart synapse.</p>
  1783. </li>
  1784. </ol>
  1785. <h2 id="new-single-sign-on-html-templates"><a class="header" href="#new-single-sign-on-html-templates">New Single Sign-on HTML Templates</a></h2>
  1786. <p>New templates (<code>sso_auth_confirm.html</code>, <code>sso_auth_success.html</code>, and
  1787. <code>sso_account_deactivated.html</code>) were added to Synapse. If your Synapse
  1788. is configured to use SSO and a custom
  1789. <code>sso_redirect_confirm_template_dir</code> configuration then these templates
  1790. will need to be copied from
  1791. <a href="synapse/res/templates">synapse/res/templates</a> into that directory.</p>
  1792. <h2 id="synapse-sso-plugins-method-deprecation"><a class="header" href="#synapse-sso-plugins-method-deprecation">Synapse SSO Plugins Method Deprecation</a></h2>
  1793. <p>Plugins using the <code>complete_sso_login</code> method of
  1794. <code>synapse.module_api.ModuleApi</code> should update to using the async/await
  1795. version <code>complete_sso_login_async</code> which includes additional checks. The
  1796. non-async version is considered deprecated.</p>
  1797. <h2 id="rolling-back-to-v1124-after-a-failed-upgrade"><a class="header" href="#rolling-back-to-v1124-after-a-failed-upgrade">Rolling back to v1.12.4 after a failed upgrade</a></h2>
  1798. <p>v1.13.0 includes a lot of large changes. If something problematic
  1799. occurs, you may want to roll-back to a previous version of Synapse.
  1800. Because v1.13.0 also includes a new database schema version, reverting
  1801. that version is also required alongside the generic rollback
  1802. instructions mentioned above. In short, to roll back to v1.12.4 you need
  1803. to:</p>
  1804. <ol>
  1805. <li>
  1806. <p>Stop the server</p>
  1807. </li>
  1808. <li>
  1809. <p>Decrease the schema version in the database:</p>
  1810. <pre><code class="language-sql">UPDATE schema_version SET version = 57;
  1811. </code></pre>
  1812. </li>
  1813. <li>
  1814. <p>Downgrade Synapse by following the instructions for your
  1815. installation method in the &quot;Rolling back to older versions&quot;
  1816. section above.</p>
  1817. </li>
  1818. </ol>
  1819. <h1 id="upgrading-to-v1120"><a class="header" href="#upgrading-to-v1120">Upgrading to v1.12.0</a></h1>
  1820. <p>This version includes a database update which is run as part of the
  1821. upgrade, and which may take some time (several hours in the case of a
  1822. large server). Synapse will not respond to HTTP requests while this
  1823. update is taking place.</p>
  1824. <p>This is only likely to be a problem in the case of a server which is
  1825. participating in many rooms.</p>
  1826. <ol start="0">
  1827. <li>
  1828. <p>As with all upgrades, it is recommended that you have a recent
  1829. backup of your database which can be used for recovery in the event
  1830. of any problems.</p>
  1831. </li>
  1832. <li>
  1833. <p>As an initial check to see if you will be affected, you can try
  1834. running the following query from the [psql]{.title-ref} or
  1835. [sqlite3]{.title-ref} console. It is safe to run it while Synapse is
  1836. still running.</p>
  1837. <pre><code class="language-sql">SELECT MAX(q.v) FROM (
  1838. SELECT (
  1839. SELECT ej.json AS v
  1840. FROM state_events se INNER JOIN event_json ej USING (event_id)
  1841. WHERE se.room_id=rooms.room_id AND se.type='m.room.create' AND se.state_key=''
  1842. LIMIT 1
  1843. ) FROM rooms WHERE rooms.room_version IS NULL
  1844. ) q;
  1845. </code></pre>
  1846. <p>This query will take about the same amount of time as the upgrade
  1847. process: ie, if it takes 5 minutes, then it is likely that Synapse
  1848. will be unresponsive for 5 minutes during the upgrade.</p>
  1849. <p>If you consider an outage of this duration to be acceptable, no
  1850. further action is necessary and you can simply start Synapse 1.12.0.</p>
  1851. <p>If you would prefer to reduce the downtime, continue with the steps
  1852. below.</p>
  1853. </li>
  1854. <li>
  1855. <p>The easiest workaround for this issue is to manually create a new
  1856. index before upgrading. On PostgreSQL, his can be done as follows:</p>
  1857. <pre><code class="language-sql">CREATE INDEX CONCURRENTLY tmp_upgrade_1_12_0_index
  1858. ON state_events(room_id) WHERE type = 'm.room.create';
  1859. </code></pre>
  1860. <p>The above query may take some time, but is also safe to run while
  1861. Synapse is running.</p>
  1862. <p>We assume that no SQLite users have databases large enough to be
  1863. affected. If you <em>are</em> affected, you can run a similar query,
  1864. omitting the <code>CONCURRENTLY</code> keyword. Note however that this
  1865. operation may in itself cause Synapse to stop running for some time.
  1866. Synapse admins are reminded that <a href="https://github.com/matrix-org/synapse/blob/master/README.rst#using-postgresql">SQLite is not recommended for use
  1867. outside a test
  1868. environment</a>.</p>
  1869. </li>
  1870. <li>
  1871. <p>Once the index has been created, the <code>SELECT</code> query in step 1 above
  1872. should complete quickly. It is therefore safe to upgrade to Synapse
  1873. 1.12.0.</p>
  1874. </li>
  1875. <li>
  1876. <p>Once Synapse 1.12.0 has successfully started and is responding to
  1877. HTTP requests, the temporary index can be removed:</p>
  1878. <pre><code class="language-sql">DROP INDEX tmp_upgrade_1_12_0_index;
  1879. </code></pre>
  1880. </li>
  1881. </ol>
  1882. <h1 id="upgrading-to-v1100"><a class="header" href="#upgrading-to-v1100">Upgrading to v1.10.0</a></h1>
  1883. <p>Synapse will now log a warning on start up if used with a PostgreSQL
  1884. database that has a non-recommended locale set.</p>
  1885. <p>See <a href="../postgres.html">Postgres</a> for details.</p>
  1886. <h1 id="upgrading-to-v180"><a class="header" href="#upgrading-to-v180">Upgrading to v1.8.0</a></h1>
  1887. <p>Specifying a <code>log_file</code> config option will now cause Synapse to refuse
  1888. to start, and should be replaced by with the <code>log_config</code> option.
  1889. Support for the <code>log_file</code> option was removed in v1.3.0 and has since
  1890. had no effect.</p>
  1891. <h1 id="upgrading-to-v170"><a class="header" href="#upgrading-to-v170">Upgrading to v1.7.0</a></h1>
  1892. <p>In an attempt to configure Synapse in a privacy preserving way, the
  1893. default behaviours of <code>allow_public_rooms_without_auth</code> and
  1894. <code>allow_public_rooms_over_federation</code> have been inverted. This means that
  1895. by default, only authenticated users querying the Client/Server API will
  1896. be able to query the room directory, and relatedly that the server will
  1897. not share room directory information with other servers over federation.</p>
  1898. <p>If your installation does not explicitly set these settings one way or
  1899. the other and you want either setting to be <code>true</code> then it will
  1900. necessary to update your homeserver configuration file accordingly.</p>
  1901. <p>For more details on the surrounding context see our
  1902. <a href="https://matrix.org/blog/2019/11/09/avoiding-unwelcome-visitors-on-private-matrix-servers">explainer</a>.</p>
  1903. <h1 id="upgrading-to-v150"><a class="header" href="#upgrading-to-v150">Upgrading to v1.5.0</a></h1>
  1904. <p>This release includes a database migration which may take several
  1905. minutes to complete if there are a large number (more than a million or
  1906. so) of entries in the <code>devices</code> table. This is only likely to a be a
  1907. problem on very large installations.</p>
  1908. <h1 id="upgrading-to-v140"><a class="header" href="#upgrading-to-v140">Upgrading to v1.4.0</a></h1>
  1909. <h2 id="new-custom-templates"><a class="header" href="#new-custom-templates">New custom templates</a></h2>
  1910. <p>If you have configured a custom template directory with the
  1911. <code>email.template_dir</code> option, be aware that there are new templates
  1912. regarding registration and threepid management (see below) that must be
  1913. included.</p>
  1914. <ul>
  1915. <li><code>registration.html</code> and <code>registration.txt</code></li>
  1916. <li><code>registration_success.html</code> and <code>registration_failure.html</code></li>
  1917. <li><code>add_threepid.html</code> and <code>add_threepid.txt</code></li>
  1918. <li><code>add_threepid_failure.html</code> and <code>add_threepid_success.html</code></li>
  1919. </ul>
  1920. <p>Synapse will expect these files to exist inside the configured template
  1921. directory, and <strong>will fail to start</strong> if they are absent. To view the
  1922. default templates, see
  1923. <a href="https://github.com/matrix-org/synapse/tree/master/synapse/res/templates">synapse/res/templates</a>.</p>
  1924. <h2 id="3pid-verification-changes"><a class="header" href="#3pid-verification-changes">3pid verification changes</a></h2>
  1925. <p><strong>Note: As of this release, users will be unable to add phone numbers or
  1926. email addresses to their accounts, without changes to the Synapse
  1927. configuration. This includes adding an email address during
  1928. registration.</strong></p>
  1929. <p>It is possible for a user to associate an email address or phone number
  1930. with their account, for a number of reasons:</p>
  1931. <ul>
  1932. <li>for use when logging in, as an alternative to the user id.</li>
  1933. <li>in the case of email, as an alternative contact to help with account
  1934. recovery.</li>
  1935. <li>in the case of email, to receive notifications of missed messages.</li>
  1936. </ul>
  1937. <p>Before an email address or phone number can be added to a user's
  1938. account, or before such an address is used to carry out a
  1939. password-reset, Synapse must confirm the operation with the owner of the
  1940. email address or phone number. It does this by sending an email or text
  1941. giving the user a link or token to confirm receipt. This process is
  1942. known as '3pid verification'. ('3pid', or 'threepid', stands for
  1943. third-party identifier, and we use it to refer to external identifiers
  1944. such as email addresses and phone numbers.)</p>
  1945. <p>Previous versions of Synapse delegated the task of 3pid verification to
  1946. an identity server by default. In most cases this server is <code>vector.im</code>
  1947. or <code>matrix.org</code>.</p>
  1948. <p>In Synapse 1.4.0, for security and privacy reasons, the homeserver will
  1949. no longer delegate this task to an identity server by default. Instead,
  1950. the server administrator will need to explicitly decide how they would
  1951. like the verification messages to be sent.</p>
  1952. <p>In the medium term, the <code>vector.im</code> and <code>matrix.org</code> identity servers
  1953. will disable support for delegated 3pid verification entirely. However,
  1954. in order to ease the transition, they will retain the capability for a
  1955. limited period. Delegated email verification will be disabled on Monday
  1956. 2nd December 2019 (giving roughly 2 months notice). Disabling delegated
  1957. SMS verification will follow some time after that once SMS verification
  1958. support lands in Synapse.</p>
  1959. <p>Once delegated 3pid verification support has been disabled in the
  1960. <code>vector.im</code> and <code>matrix.org</code> identity servers, all Synapse versions that
  1961. depend on those instances will be unable to verify email and phone
  1962. numbers through them. There are no imminent plans to remove delegated
  1963. 3pid verification from Sydent generally. (Sydent is the identity server
  1964. project that backs the <code>vector.im</code> and <code>matrix.org</code> instances).</p>
  1965. <h3 id="email-1"><a class="header" href="#email-1">Email</a></h3>
  1966. <p>Following upgrade, to continue verifying email (e.g. as part of the
  1967. registration process), admins can either:-</p>
  1968. <ul>
  1969. <li>Configure Synapse to use an email server.</li>
  1970. <li>Run or choose an identity server which allows delegated email
  1971. verification and delegate to it.</li>
  1972. </ul>
  1973. <h4 id="configure-smtp-in-synapse"><a class="header" href="#configure-smtp-in-synapse">Configure SMTP in Synapse</a></h4>
  1974. <p>To configure an SMTP server for Synapse, modify the configuration
  1975. section headed <code>email</code>, and be sure to have at least the
  1976. <code>smtp_host, smtp_port</code> and <code>notif_from</code> fields filled out.</p>
  1977. <p>You may also need to set <code>smtp_user</code>, <code>smtp_pass</code>, and
  1978. <code>require_transport_security</code>.</p>
  1979. <p>See the <a href="docs/sample_config.yaml">sample configuration file</a> for more
  1980. details on these settings.</p>
  1981. <h4 id="delegate-email-to-an-identity-server"><a class="header" href="#delegate-email-to-an-identity-server">Delegate email to an identity server</a></h4>
  1982. <p>Some admins will wish to continue using email verification as part of
  1983. the registration process, but will not immediately have an appropriate
  1984. SMTP server at hand.</p>
  1985. <p>To this end, we will continue to support email verification delegation
  1986. via the <code>vector.im</code> and <code>matrix.org</code> identity servers for two months.
  1987. Support for delegated email verification will be disabled on Monday 2nd
  1988. December.</p>
  1989. <p>The <code>account_threepid_delegates</code> dictionary defines whether the
  1990. homeserver should delegate an external server (typically an <a href="https://matrix.org/docs/spec/identity_service/r0.2.1">identity
  1991. server</a>) to handle
  1992. sending confirmation messages via email and SMS.</p>
  1993. <p>So to delegate email verification, in <code>homeserver.yaml</code>, set
  1994. <code>account_threepid_delegates.email</code> to the base URL of an identity
  1995. server. For example:</p>
  1996. <pre><code class="language-yaml">account_threepid_delegates:
  1997. email: https://example.com # Delegate email sending to example.com
  1998. </code></pre>
  1999. <p>Note that <code>account_threepid_delegates.email</code> replaces the deprecated
  2000. <code>email.trust_identity_server_for_password_resets</code>: if
  2001. <code>email.trust_identity_server_for_password_resets</code> is set to <code>true</code>, and
  2002. <code>account_threepid_delegates.email</code> is not set, then the first entry in
  2003. <code>trusted_third_party_id_servers</code> will be used as the
  2004. <code>account_threepid_delegate</code> for email. This is to ensure compatibility
  2005. with existing Synapse installs that set up external server handling for
  2006. these tasks before v1.4.0. If
  2007. <code>email.trust_identity_server_for_password_resets</code> is <code>true</code> and no
  2008. trusted identity server domains are configured, Synapse will report an
  2009. error and refuse to start.</p>
  2010. <p>If <code>email.trust_identity_server_for_password_resets</code> is <code>false</code> or
  2011. absent and no <code>email</code> delegate is configured in
  2012. <code>account_threepid_delegates</code>, then Synapse will send email verification
  2013. messages itself, using the configured SMTP server (see above). that
  2014. type.</p>
  2015. <h3 id="phone-numbers"><a class="header" href="#phone-numbers">Phone numbers</a></h3>
  2016. <p>Synapse does not support phone-number verification itself, so the only
  2017. way to maintain the ability for users to add phone numbers to their
  2018. accounts will be by continuing to delegate phone number verification to
  2019. the <code>matrix.org</code> and <code>vector.im</code> identity servers (or another identity
  2020. server that supports SMS sending).</p>
  2021. <p>The <code>account_threepid_delegates</code> dictionary defines whether the
  2022. homeserver should delegate an external server (typically an <a href="https://matrix.org/docs/spec/identity_service/r0.2.1">identity
  2023. server</a>) to handle
  2024. sending confirmation messages via email and SMS.</p>
  2025. <p>So to delegate phone number verification, in <code>homeserver.yaml</code>, set
  2026. <code>account_threepid_delegates.msisdn</code> to the base URL of an identity
  2027. server. For example:</p>
  2028. <pre><code class="language-yaml">account_threepid_delegates:
  2029. msisdn: https://example.com # Delegate sms sending to example.com
  2030. </code></pre>
  2031. <p>The <code>matrix.org</code> and <code>vector.im</code> identity servers will continue to
  2032. support delegated phone number verification via SMS until such time as
  2033. it is possible for admins to configure their servers to perform phone
  2034. number verification directly. More details will follow in a future
  2035. release.</p>
  2036. <h2 id="rolling-back-to-v131"><a class="header" href="#rolling-back-to-v131">Rolling back to v1.3.1</a></h2>
  2037. <p>If you encounter problems with v1.4.0, it should be possible to roll
  2038. back to v1.3.1, subject to the following:</p>
  2039. <ul>
  2040. <li>
  2041. <p>The 'room statistics' engine was heavily reworked in this release
  2042. (see <a href="https://github.com/matrix-org/synapse/pull/5971">#5971</a>),
  2043. including significant changes to the database schema, which are not
  2044. easily reverted. This will cause the room statistics engine to stop
  2045. updating when you downgrade.</p>
  2046. <p>The room statistics are essentially unused in v1.3.1 (in future
  2047. versions of Synapse, they will be used to populate the room
  2048. directory), so there should be no loss of functionality. However,
  2049. the statistics engine will write errors to the logs, which can be
  2050. avoided by setting the following in <code>homeserver.yaml</code>:</p>
  2051. <pre><code class="language-yaml">stats:
  2052. enabled: false
  2053. </code></pre>
  2054. <p>Don't forget to re-enable it when you upgrade again, in preparation
  2055. for its use in the room directory!</p>
  2056. </li>
  2057. </ul>
  2058. <h1 id="upgrading-to-v120"><a class="header" href="#upgrading-to-v120">Upgrading to v1.2.0</a></h1>
  2059. <p>Some counter metrics have been renamed, with the old names deprecated.
  2060. See <a href="../metrics-howto.html#renaming-of-metrics--deprecation-of-old-names-in-12">the metrics
  2061. documentation</a>
  2062. for details.</p>
  2063. <h1 id="upgrading-to-v110"><a class="header" href="#upgrading-to-v110">Upgrading to v1.1.0</a></h1>
  2064. <p>Synapse v1.1.0 removes support for older Python and PostgreSQL versions,
  2065. as outlined in <a href="https://matrix.org/blog/2019/04/08/synapse-deprecating-postgres-9-4-and-python-2-x">our deprecation
  2066. notice</a>.</p>
  2067. <h2 id="minimum-python-version"><a class="header" href="#minimum-python-version">Minimum Python Version</a></h2>
  2068. <p>Synapse v1.1.0 has a minimum Python requirement of Python 3.5. Python
  2069. 3.6 or Python 3.7 are recommended as they have improved internal string
  2070. handling, significantly reducing memory usage.</p>
  2071. <p>If you use current versions of the Matrix.org-distributed Debian
  2072. packages or Docker images, action is not required.</p>
  2073. <p>If you install Synapse in a Python virtual environment, please see
  2074. &quot;Upgrading to v0.34.0&quot; for notes on setting up a new virtualenv under
  2075. Python 3.</p>
  2076. <h2 id="minimum-postgresql-version"><a class="header" href="#minimum-postgresql-version">Minimum PostgreSQL Version</a></h2>
  2077. <p>If using PostgreSQL under Synapse, you will need to use PostgreSQL 9.5
  2078. or above. Please see the <a href="https://www.postgresql.org/docs/11/upgrading.html">PostgreSQL
  2079. documentation</a> for
  2080. more details on upgrading your database.</p>
  2081. <h1 id="upgrading-to-v10"><a class="header" href="#upgrading-to-v10">Upgrading to v1.0</a></h1>
  2082. <h2 id="validation-of-tls-certificates"><a class="header" href="#validation-of-tls-certificates">Validation of TLS certificates</a></h2>
  2083. <p>Synapse v1.0 is the first release to enforce validation of TLS
  2084. certificates for the federation API. It is therefore essential that your
  2085. certificates are correctly configured. See the
  2086. <a href="../MSC1711_certificates_FAQ.html">FAQ</a> for more information.</p>
  2087. <p>Note, v1.0 installations will also no longer be able to federate with
  2088. servers that have not correctly configured their certificates.</p>
  2089. <p>In rare cases, it may be desirable to disable certificate checking: for
  2090. example, it might be essential to be able to federate with a given
  2091. legacy server in a closed federation. This can be done in one of two
  2092. ways:-</p>
  2093. <ul>
  2094. <li>Configure the global switch <code>federation_verify_certificates</code> to
  2095. <code>false</code>.</li>
  2096. <li>Configure a whitelist of server domains to trust via
  2097. <code>federation_certificate_verification_whitelist</code>.</li>
  2098. </ul>
  2099. <p>See the <a href="docs/sample_config.yaml">sample configuration file</a> for more
  2100. details on these settings.</p>
  2101. <h2 id="email-2"><a class="header" href="#email-2">Email</a></h2>
  2102. <p>When a user requests a password reset, Synapse will send an email to the
  2103. user to confirm the request.</p>
  2104. <p>Previous versions of Synapse delegated the job of sending this email to
  2105. an identity server. If the identity server was somehow malicious or
  2106. became compromised, it would be theoretically possible to hijack an
  2107. account through this means.</p>
  2108. <p>Therefore, by default, Synapse v1.0 will send the confirmation email
  2109. itself. If Synapse is not configured with an SMTP server, password reset
  2110. via email will be disabled.</p>
  2111. <p>To configure an SMTP server for Synapse, modify the configuration
  2112. section headed <code>email</code>, and be sure to have at least the <code>smtp_host</code>,
  2113. <code>smtp_port</code> and <code>notif_from</code> fields filled out. You may also need to set
  2114. <code>smtp_user</code>, <code>smtp_pass</code>, and <code>require_transport_security</code>.</p>
  2115. <p>If you are absolutely certain that you wish to continue using an
  2116. identity server for password resets, set
  2117. <code>trust_identity_server_for_password_resets</code> to <code>true</code>.</p>
  2118. <p>See the <a href="docs/sample_config.yaml">sample configuration file</a> for more
  2119. details on these settings.</p>
  2120. <h2 id="new-email-templates"><a class="header" href="#new-email-templates">New email templates</a></h2>
  2121. <p>Some new templates have been added to the default template directory for the purpose of
  2122. the homeserver sending its own password reset emails. If you have configured a
  2123. custom <code>template_dir</code> in your Synapse config, these files will need to be added.</p>
  2124. <p><code>password_reset.html</code> and <code>password_reset.txt</code> are HTML and plain text
  2125. templates respectively that contain the contents of what will be emailed
  2126. to the user upon attempting to reset their password via email.
  2127. <code>password_reset_success.html</code> and <code>password_reset_failure.html</code> are HTML
  2128. files that the content of which (assuming no redirect URL is set) will
  2129. be shown to the user after they attempt to click the link in the email
  2130. sent to them.</p>
  2131. <h1 id="upgrading-to-v0990"><a class="header" href="#upgrading-to-v0990">Upgrading to v0.99.0</a></h1>
  2132. <p>Please be aware that, before Synapse v1.0 is released around March 2019,
  2133. you will need to replace any self-signed certificates with those
  2134. verified by a root CA. Information on how to do so can be found at <a href="../ACME.html">the
  2135. ACME docs</a>.</p>
  2136. <p>For more information on configuring TLS certificates see the
  2137. <a href="../MSC1711_certificates_FAQ.html">FAQ</a>.</p>
  2138. <h1 id="upgrading-to-v0340"><a class="header" href="#upgrading-to-v0340">Upgrading to v0.34.0</a></h1>
  2139. <ol>
  2140. <li>
  2141. <p>This release is the first to fully support Python 3. Synapse will
  2142. now run on Python versions 3.5, or 3.6 (as well as 2.7). We
  2143. recommend switching to Python 3, as it has been shown to give
  2144. performance improvements.</p>
  2145. <p>For users who have installed Synapse into a virtualenv, we recommend
  2146. doing this by creating a new virtualenv. For example:</p>
  2147. <pre><code>virtualenv -p python3 ~/synapse/env3
  2148. source ~/synapse/env3/bin/activate
  2149. pip install matrix-synapse
  2150. </code></pre>
  2151. <p>You can then start synapse as normal, having activated the new
  2152. virtualenv:</p>
  2153. <pre><code>cd ~/synapse
  2154. source env3/bin/activate
  2155. synctl start
  2156. </code></pre>
  2157. <p>Users who have installed from distribution packages should see the
  2158. relevant package documentation. See below for notes on Debian
  2159. packages.</p>
  2160. <ul>
  2161. <li>
  2162. <p>When upgrading to Python 3, you <strong>must</strong> make sure that your log
  2163. files are configured as UTF-8, by adding <code>encoding: utf8</code> to the
  2164. <code>RotatingFileHandler</code> configuration (if you have one) in your
  2165. <code>&lt;server&gt;.log.config</code> file. For example, if your <code>log.config</code>
  2166. file contains:</p>
  2167. <pre><code>handlers:
  2168. file:
  2169. class: logging.handlers.RotatingFileHandler
  2170. formatter: precise
  2171. filename: homeserver.log
  2172. maxBytes: 104857600
  2173. backupCount: 10
  2174. filters: [context]
  2175. console:
  2176. class: logging.StreamHandler
  2177. formatter: precise
  2178. filters: [context]
  2179. </code></pre>
  2180. <p>Then you should update this to be:</p>
  2181. <pre><code>handlers:
  2182. file:
  2183. class: logging.handlers.RotatingFileHandler
  2184. formatter: precise
  2185. filename: homeserver.log
  2186. maxBytes: 104857600
  2187. backupCount: 10
  2188. filters: [context]
  2189. encoding: utf8
  2190. console:
  2191. class: logging.StreamHandler
  2192. formatter: precise
  2193. filters: [context]
  2194. </code></pre>
  2195. <p>There is no need to revert this change if downgrading to
  2196. Python 2.</p>
  2197. </li>
  2198. </ul>
  2199. <p>We are also making available Debian packages which will run Synapse
  2200. on Python 3. You can switch to these packages with
  2201. <code>apt-get install matrix-synapse-py3</code>, however, please read
  2202. <a href="https://github.com/matrix-org/synapse/blob/release-v0.34.0/debian/NEWS">debian/NEWS</a>
  2203. before doing so. The existing <code>matrix-synapse</code> packages will
  2204. continue to use Python 2 for the time being.</p>
  2205. </li>
  2206. <li>
  2207. <p>This release removes the <code>riot.im</code> from the default list of trusted
  2208. identity servers.</p>
  2209. <p>If <code>riot.im</code> is in your homeserver's list of
  2210. <code>trusted_third_party_id_servers</code>, you should remove it. It was added
  2211. in case a hypothetical future identity server was put there. If you
  2212. don't remove it, users may be unable to deactivate their accounts.</p>
  2213. </li>
  2214. <li>
  2215. <p>This release no longer installs the (unmaintained) Matrix Console
  2216. web client as part of the default installation. It is possible to
  2217. re-enable it by installing it separately and setting the
  2218. <code>web_client_location</code> config option, but please consider switching
  2219. to another client.</p>
  2220. </li>
  2221. </ol>
  2222. <h1 id="upgrading-to-v0337"><a class="header" href="#upgrading-to-v0337">Upgrading to v0.33.7</a></h1>
  2223. <p>This release removes the example email notification templates from
  2224. <code>res/templates</code> (they are now internal to the python package). This
  2225. should only affect you if you (a) deploy your Synapse instance from a
  2226. git checkout or a github snapshot URL, and (b) have email notifications
  2227. enabled.</p>
  2228. <p>If you have email notifications enabled, you should ensure that
  2229. <code>email.template_dir</code> is either configured to point at a directory where
  2230. you have installed customised templates, or leave it unset to use the
  2231. default templates.</p>
  2232. <h1 id="upgrading-to-v0273"><a class="header" href="#upgrading-to-v0273">Upgrading to v0.27.3</a></h1>
  2233. <p>This release expands the anonymous usage stats sent if the opt-in
  2234. <code>report_stats</code> configuration is set to <code>true</code>. We now capture RSS memory
  2235. and cpu use at a very coarse level. This requires administrators to
  2236. install the optional <code>psutil</code> python module.</p>
  2237. <p>We would appreciate it if you could assist by ensuring this module is
  2238. available and <code>report_stats</code> is enabled. This will let us see if
  2239. performance changes to synapse are having an impact to the general
  2240. community.</p>
  2241. <h1 id="upgrading-to-v0150"><a class="header" href="#upgrading-to-v0150">Upgrading to v0.15.0</a></h1>
  2242. <p>If you want to use the new URL previewing API
  2243. (<code>/_matrix/media/r0/preview_url</code>) then you have to explicitly enable it
  2244. in the config and update your dependencies dependencies. See README.rst
  2245. for details.</p>
  2246. <h1 id="upgrading-to-v0110"><a class="header" href="#upgrading-to-v0110">Upgrading to v0.11.0</a></h1>
  2247. <p>This release includes the option to send anonymous usage stats to
  2248. matrix.org, and requires that administrators explictly opt in or out by
  2249. setting the <code>report_stats</code> option to either <code>true</code> or <code>false</code>.</p>
  2250. <p>We would really appreciate it if you could help our project out by
  2251. reporting anonymized usage statistics from your homeserver. Only very
  2252. basic aggregate data (e.g. number of users) will be reported, but it
  2253. helps us to track the growth of the Matrix community, and helps us to
  2254. make Matrix a success, as well as to convince other networks that they
  2255. should peer with us.</p>
  2256. <h1 id="upgrading-to-v090"><a class="header" href="#upgrading-to-v090">Upgrading to v0.9.0</a></h1>
  2257. <p>Application services have had a breaking API change in this version.</p>
  2258. <p>They can no longer register themselves with a home server using the AS
  2259. HTTP API. This decision was made because a compromised application
  2260. service with free reign to register any regex in effect grants full
  2261. read/write access to the home server if a regex of <code>.*</code> is used. An
  2262. attack where a compromised AS re-registers itself with <code>.*</code> was deemed
  2263. too big of a security risk to ignore, and so the ability to register
  2264. with the HS remotely has been removed.</p>
  2265. <p>It has been replaced by specifying a list of application service
  2266. registrations in <code>homeserver.yaml</code>:</p>
  2267. <pre><code>app_service_config_files: [&quot;registration-01.yaml&quot;, &quot;registration-02.yaml&quot;]
  2268. </code></pre>
  2269. <p>Where <code>registration-01.yaml</code> looks like:</p>
  2270. <pre><code>url: &lt;String&gt; # e.g. &quot;https://my.application.service.com&quot;
  2271. as_token: &lt;String&gt;
  2272. hs_token: &lt;String&gt;
  2273. sender_localpart: &lt;String&gt; # This is a new field which denotes the user_id localpart when using the AS token
  2274. namespaces:
  2275. users:
  2276. - exclusive: &lt;Boolean&gt;
  2277. regex: &lt;String&gt; # e.g. &quot;@prefix_.*&quot;
  2278. aliases:
  2279. - exclusive: &lt;Boolean&gt;
  2280. regex: &lt;String&gt;
  2281. rooms:
  2282. - exclusive: &lt;Boolean&gt;
  2283. regex: &lt;String&gt;
  2284. </code></pre>
  2285. <h1 id="upgrading-to-v080"><a class="header" href="#upgrading-to-v080">Upgrading to v0.8.0</a></h1>
  2286. <p>Servers which use captchas will need to add their public key to:</p>
  2287. <pre><code>static/client/register/register_config.js
  2288. window.matrixRegistrationConfig = {
  2289. recaptcha_public_key: &quot;YOUR_PUBLIC_KEY&quot;
  2290. };
  2291. </code></pre>
  2292. <p>This is required in order to support registration fallback (typically
  2293. used on mobile devices).</p>
  2294. <h1 id="upgrading-to-v070"><a class="header" href="#upgrading-to-v070">Upgrading to v0.7.0</a></h1>
  2295. <p>New dependencies are:</p>
  2296. <ul>
  2297. <li>pydenticon</li>
  2298. <li>simplejson</li>
  2299. <li>syutil</li>
  2300. <li>matrix-angular-sdk</li>
  2301. </ul>
  2302. <p>To pull in these dependencies in a virtual env, run:</p>
  2303. <pre><code>python synapse/python_dependencies.py | xargs -n 1 pip install
  2304. </code></pre>
  2305. <h1 id="upgrading-to-v060"><a class="header" href="#upgrading-to-v060">Upgrading to v0.6.0</a></h1>
  2306. <p>To pull in new dependencies, run:</p>
  2307. <pre><code>python setup.py develop --user
  2308. </code></pre>
  2309. <p>This update includes a change to the database schema. To upgrade you
  2310. first need to upgrade the database by running:</p>
  2311. <pre><code>python scripts/upgrade_db_to_v0.6.0.py &lt;db&gt; &lt;server_name&gt; &lt;signing_key&gt;
  2312. </code></pre>
  2313. <p>Where [<db>]{.title-ref} is the location of the database,
  2314. [&lt;server_name&gt;]{.title-ref} is the server name as specified in the
  2315. synapse configuration, and [&lt;signing_key&gt;]{.title-ref} is the location
  2316. of the signing key as specified in the synapse configuration.</p>
  2317. <p>This may take some time to complete. Failures of signatures and content
  2318. hashes can safely be ignored.</p>
  2319. <h1 id="upgrading-to-v051"><a class="header" href="#upgrading-to-v051">Upgrading to v0.5.1</a></h1>
  2320. <p>Depending on precisely when you installed v0.5.0 you may have ended up
  2321. with a stale release of the reference matrix webclient installed as a
  2322. python module. To uninstall it and ensure you are depending on the
  2323. latest module, please run:</p>
  2324. <pre><code>$ pip uninstall syweb
  2325. </code></pre>
  2326. <h1 id="upgrading-to-v050"><a class="header" href="#upgrading-to-v050">Upgrading to v0.5.0</a></h1>
  2327. <p>The webclient has been split out into a seperate repository/pacakage in
  2328. this release. Before you restart your homeserver you will need to pull
  2329. in the webclient package by running:</p>
  2330. <pre><code>python setup.py develop --user
  2331. </code></pre>
  2332. <p>This release completely changes the database schema and so requires
  2333. upgrading it before starting the new version of the homeserver.</p>
  2334. <p>The script &quot;database-prepare-for-0.5.0.sh&quot; should be used to upgrade
  2335. the database. This will save all user information, such as logins and
  2336. profiles, but will otherwise purge the database. This includes messages,
  2337. which rooms the home server was a member of and room alias mappings.</p>
  2338. <p>If you would like to keep your history, please take a copy of your
  2339. database file and ask for help in #matrix:matrix.org. The upgrade
  2340. process is, unfortunately, non trivial and requires human intervention
  2341. to resolve any resulting conflicts during the upgrade process.</p>
  2342. <p>Before running the command the homeserver should be first completely
  2343. shutdown. To run it, simply specify the location of the database, e.g.:</p>
  2344. <blockquote>
  2345. <p>./scripts/database-prepare-for-0.5.0.sh &quot;homeserver.db&quot;</p>
  2346. </blockquote>
  2347. <p>Once this has successfully completed it will be safe to restart the
  2348. homeserver. You may notice that the homeserver takes a few seconds
  2349. longer to restart than usual as it reinitializes the database.</p>
  2350. <p>On startup of the new version, users can either rejoin remote rooms
  2351. using room aliases or by being reinvited. Alternatively, if any other
  2352. homeserver sends a message to a room that the homeserver was previously
  2353. in the local HS will automatically rejoin the room.</p>
  2354. <h1 id="upgrading-to-v040"><a class="header" href="#upgrading-to-v040">Upgrading to v0.4.0</a></h1>
  2355. <p>This release needs an updated syutil version. Run:</p>
  2356. <pre><code>python setup.py develop
  2357. </code></pre>
  2358. <p>You will also need to upgrade your configuration as the signing key
  2359. format has changed. Run:</p>
  2360. <pre><code>python -m synapse.app.homeserver --config-path &lt;CONFIG&gt; --generate-config
  2361. </code></pre>
  2362. <h1 id="upgrading-to-v030"><a class="header" href="#upgrading-to-v030">Upgrading to v0.3.0</a></h1>
  2363. <p>This registration API now closely matches the login API. This introduces
  2364. a bit more backwards and forwards between the HS and the client, but
  2365. this improves the overall flexibility of the API. You can now GET on
  2366. /register to retrieve a list of valid registration flows. Upon choosing
  2367. one, they are submitted in the same way as login, e.g:</p>
  2368. <pre><code>{
  2369. type: m.login.password,
  2370. user: foo,
  2371. password: bar
  2372. }
  2373. </code></pre>
  2374. <p>The default HS supports 2 flows, with and without Identity Server email
  2375. authentication. Enabling captcha on the HS will add in an extra step to
  2376. all flows: <code>m.login.recaptcha</code> which must be completed before you can
  2377. transition to the next stage. There is a new login type:
  2378. <code>m.login.email.identity</code> which contains the <code>threepidCreds</code> key which
  2379. were previously sent in the original register request. For more
  2380. information on this, see the specification.</p>
  2381. <h2 id="web-client"><a class="header" href="#web-client">Web Client</a></h2>
  2382. <p>The VoIP specification has changed between v0.2.0 and v0.3.0. Users
  2383. should refresh any browser tabs to get the latest web client code. Users
  2384. on v0.2.0 of the web client will not be able to call those on v0.3.0 and
  2385. vice versa.</p>
  2386. <h1 id="upgrading-to-v020"><a class="header" href="#upgrading-to-v020">Upgrading to v0.2.0</a></h1>
  2387. <p>The home server now requires setting up of SSL config before it can run.
  2388. To automatically generate default config use:</p>
  2389. <pre><code>$ python synapse/app/homeserver.py \
  2390. --server-name machine.my.domain.name \
  2391. --bind-port 8448 \
  2392. --config-path homeserver.config \
  2393. --generate-config
  2394. </code></pre>
  2395. <p>This config can be edited if desired, for example to specify a different
  2396. SSL certificate to use. Once done you can run the home server using:</p>
  2397. <pre><code>$ python synapse/app/homeserver.py --config-path homeserver.config
  2398. </code></pre>
  2399. <p>See the README.rst for more information.</p>
  2400. <p>Also note that some config options have been renamed, including:</p>
  2401. <ul>
  2402. <li>&quot;host&quot; to &quot;server-name&quot;</li>
  2403. <li>&quot;database&quot; to &quot;database-path&quot;</li>
  2404. <li>&quot;port&quot; to &quot;bind-port&quot; and &quot;unsecure-port&quot;</li>
  2405. </ul>
  2406. <h1 id="upgrading-to-v001"><a class="header" href="#upgrading-to-v001">Upgrading to v0.0.1</a></h1>
  2407. <p>This release completely changes the database schema and so requires
  2408. upgrading it before starting the new version of the homeserver.</p>
  2409. <p>The script &quot;database-prepare-for-0.0.1.sh&quot; should be used to upgrade
  2410. the database. This will save all user information, such as logins and
  2411. profiles, but will otherwise purge the database. This includes messages,
  2412. which rooms the home server was a member of and room alias mappings.</p>
  2413. <p>Before running the command the homeserver should be first completely
  2414. shutdown. To run it, simply specify the location of the database, e.g.:</p>
  2415. <blockquote>
  2416. <p>./scripts/database-prepare-for-0.0.1.sh &quot;homeserver.db&quot;</p>
  2417. </blockquote>
  2418. <p>Once this has successfully completed it will be safe to restart the
  2419. homeserver. You may notice that the homeserver takes a few seconds
  2420. longer to restart than usual as it reinitializes the database.</p>
  2421. <p>On startup of the new version, users can either rejoin remote rooms
  2422. using room aliases or by being reinvited. Alternatively, if any other
  2423. homeserver sends a message to a room that the homeserver was previously
  2424. in the local HS will automatically rejoin the room.</p>
  2425. <div style="break-before: page; page-break-before: always;"></div><h1 id="msc1711-certificates-faq"><a class="header" href="#msc1711-certificates-faq">MSC1711 Certificates FAQ</a></h1>
  2426. <h2 id="historical-note"><a class="header" href="#historical-note">Historical Note</a></h2>
  2427. <p>This document was originally written to guide server admins through the upgrade
  2428. path towards Synapse 1.0. Specifically,
  2429. <a href="https://github.com/matrix-org/matrix-doc/blob/master/proposals/1711-x509-for-federation.md">MSC1711</a>
  2430. required that all servers present valid TLS certificates on their federation
  2431. API. Admins were encouraged to achieve compliance from version 0.99.0 (released
  2432. in February 2019) ahead of version 1.0 (released June 2019) enforcing the
  2433. certificate checks.</p>
  2434. <p>Much of what follows is now outdated since most admins will have already
  2435. upgraded, however it may be of use to those with old installs returning to the
  2436. project.</p>
  2437. <p>If you are setting up a server from scratch you almost certainly should look at
  2438. the <a href="setup/installation.html">installation guide</a> instead.</p>
  2439. <h2 id="introduction-1"><a class="header" href="#introduction-1">Introduction</a></h2>
  2440. <p>The goal of Synapse 0.99.0 is to act as a stepping stone to Synapse 1.0.0. It
  2441. supports the r0.1 release of the server to server specification, but is
  2442. compatible with both the legacy Matrix federation behaviour (pre-r0.1) as well
  2443. as post-r0.1 behaviour, in order to allow for a smooth upgrade across the
  2444. federation.</p>
  2445. <p>The most important thing to know is that Synapse 1.0.0 will require a valid TLS
  2446. certificate on federation endpoints. Self signed certificates will not be
  2447. sufficient.</p>
  2448. <p>Synapse 0.99.0 makes it easy to configure TLS certificates and will
  2449. interoperate with both &gt;= 1.0.0 servers as well as existing servers yet to
  2450. upgrade.</p>
  2451. <p><strong>It is critical that all admins upgrade to 0.99.0 and configure a valid TLS
  2452. certificate.</strong> Admins will have 1 month to do so, after which 1.0.0 will be
  2453. released and those servers without a valid certificate will not longer be able
  2454. to federate with &gt;= 1.0.0 servers.</p>
  2455. <p>Full details on how to carry out this configuration change is given
  2456. <a href="MSC1711_certificates_FAQ.html#configuring-certificates-for-compatibility-with-synapse-100">below</a>. A
  2457. timeline and some frequently asked questions are also given below.</p>
  2458. <p>For more details and context on the release of the r0.1 Server/Server API and
  2459. imminent Matrix 1.0 release, you can also see our
  2460. <a href="https://matrix.org/blog/2019/02/04/matrix-at-fosdem-2019/">main talk from FOSDEM 2019</a>.</p>
  2461. <h2 id="contents"><a class="header" href="#contents">Contents</a></h2>
  2462. <ul>
  2463. <li>Timeline</li>
  2464. <li>Configuring certificates for compatibility with Synapse 1.0</li>
  2465. <li>FAQ
  2466. <ul>
  2467. <li>Synapse 0.99.0 has just been released, what do I need to do right now?</li>
  2468. <li>How do I upgrade?</li>
  2469. <li>What will happen if I do not set up a valid federation certificate
  2470. immediately?</li>
  2471. <li>What will happen if I do nothing at all?</li>
  2472. <li>When do I need a SRV record or .well-known URI?</li>
  2473. <li>Can I still use an SRV record?</li>
  2474. <li>I have created a .well-known URI. Do I still need an SRV record?</li>
  2475. <li>It used to work just fine, why are you breaking everything?</li>
  2476. <li>Can I manage my own certificates rather than having Synapse renew
  2477. certificates itself?</li>
  2478. <li>Do you still recommend against using a reverse proxy on the federation port?</li>
  2479. <li>Do I still need to give my TLS certificates to Synapse if I am using a
  2480. reverse proxy?</li>
  2481. <li>Do I need the same certificate for the client and federation port?</li>
  2482. <li>How do I tell Synapse to reload my keys/certificates after I replace them?</li>
  2483. </ul>
  2484. </li>
  2485. </ul>
  2486. <h2 id="timeline"><a class="header" href="#timeline">Timeline</a></h2>
  2487. <p><strong>5th Feb 2019 - Synapse 0.99.0 is released.</strong></p>
  2488. <p>All server admins are encouraged to upgrade.</p>
  2489. <p>0.99.0:</p>
  2490. <ul>
  2491. <li>
  2492. <p>provides support for ACME to make setting up Let's Encrypt certs easy, as
  2493. well as .well-known support.</p>
  2494. </li>
  2495. <li>
  2496. <p>does not enforce that a valid CA cert is present on the federation API, but
  2497. rather makes it easy to set one up.</p>
  2498. </li>
  2499. <li>
  2500. <p>provides support for .well-known</p>
  2501. </li>
  2502. </ul>
  2503. <p>Admins should upgrade and configure a valid CA cert. Homeservers that require a
  2504. .well-known entry (see below), should retain their SRV record and use it
  2505. alongside their .well-known record.</p>
  2506. <p><strong>10th June 2019 - Synapse 1.0.0 is released</strong></p>
  2507. <p>1.0.0 is scheduled for release on 10th June. In
  2508. accordance with the the <a href="https://matrix.org/docs/spec/server_server/r0.1.0.html">S2S spec</a>
  2509. 1.0.0 will enforce certificate validity. This means that any homeserver without a
  2510. valid certificate after this point will no longer be able to federate with
  2511. 1.0.0 servers.</p>
  2512. <h2 id="configuring-certificates-for-compatibility-with-synapse-100"><a class="header" href="#configuring-certificates-for-compatibility-with-synapse-100">Configuring certificates for compatibility with Synapse 1.0.0</a></h2>
  2513. <h3 id="if-you-do-not-currently-have-an-srv-record"><a class="header" href="#if-you-do-not-currently-have-an-srv-record">If you do not currently have an SRV record</a></h3>
  2514. <p>In this case, your <code>server_name</code> points to the host where your Synapse is
  2515. running. There is no need to create a <code>.well-known</code> URI or an SRV record, but
  2516. you will need to give Synapse a valid, signed, certificate.</p>
  2517. <h3 id="if-you-do-have-an-srv-record-currently"><a class="header" href="#if-you-do-have-an-srv-record-currently">If you do have an SRV record currently</a></h3>
  2518. <p>If you are using an SRV record, your matrix domain (<code>server_name</code>) may not
  2519. point to the same host that your Synapse is running on (the 'target
  2520. domain'). (If it does, you can follow the recommendation above; otherwise, read
  2521. on.)</p>
  2522. <p>Let's assume that your <code>server_name</code> is <code>example.com</code>, and your Synapse is
  2523. hosted at a target domain of <code>customer.example.net</code>. Currently you should have
  2524. an SRV record which looks like:</p>
  2525. <pre><code>_matrix._tcp.example.com. IN SRV 10 5 8000 customer.example.net.
  2526. </code></pre>
  2527. <p>In this situation, you have three choices for how to proceed:</p>
  2528. <h4 id="option-1-give-synapse-a-certificate-for-your-matrix-domain"><a class="header" href="#option-1-give-synapse-a-certificate-for-your-matrix-domain">Option 1: give Synapse a certificate for your matrix domain</a></h4>
  2529. <p>Synapse 1.0 will expect your server to present a TLS certificate for your
  2530. <code>server_name</code> (<code>example.com</code> in the above example). You can achieve this by acquiring a
  2531. certificate for the <code>server_name</code> yourself (for example, using <code>certbot</code>), and giving it
  2532. and the key to Synapse via <code>tls_certificate_path</code> and <code>tls_private_key_path</code>.</p>
  2533. <h4 id="option-2-run-synapse-behind-a-reverse-proxy"><a class="header" href="#option-2-run-synapse-behind-a-reverse-proxy">Option 2: run Synapse behind a reverse proxy</a></h4>
  2534. <p>If you have an existing reverse proxy set up with correct TLS certificates for
  2535. your domain, you can simply route all traffic through the reverse proxy by
  2536. updating the SRV record appropriately (or removing it, if the proxy listens on
  2537. 8448).</p>
  2538. <p>See <a href="reverse_proxy.html">reverse_proxy.md</a> for information on setting up a
  2539. reverse proxy.</p>
  2540. <h4 id="option-3-add-a-well-known-file-to-delegate-your-matrix-traffic"><a class="header" href="#option-3-add-a-well-known-file-to-delegate-your-matrix-traffic">Option 3: add a .well-known file to delegate your matrix traffic</a></h4>
  2541. <p>This will allow you to keep Synapse on a separate domain, without having to
  2542. give it a certificate for the matrix domain.</p>
  2543. <p>You can do this with a <code>.well-known</code> file as follows:</p>
  2544. <ol>
  2545. <li>
  2546. <p>Keep the SRV record in place - it is needed for backwards compatibility
  2547. with Synapse 0.34 and earlier.</p>
  2548. </li>
  2549. <li>
  2550. <p>Give Synapse a certificate corresponding to the target domain
  2551. (<code>customer.example.net</code> in the above example). You can do this by acquire a
  2552. certificate for the target domain and giving it to Synapse via <code>tls_certificate_path</code>
  2553. and <code>tls_private_key_path</code>.</p>
  2554. </li>
  2555. <li>
  2556. <p>Restart Synapse to ensure the new certificate is loaded.</p>
  2557. </li>
  2558. <li>
  2559. <p>Arrange for a <code>.well-known</code> file at
  2560. <code>https://&lt;server_name&gt;/.well-known/matrix/server</code> with contents:</p>
  2561. <pre><code class="language-json">{&quot;m.server&quot;: &quot;&lt;target server name&gt;&quot;}
  2562. </code></pre>
  2563. <p>where the target server name is resolved as usual (i.e. SRV lookup, falling
  2564. back to talking to port 8448).</p>
  2565. <p>In the above example, where synapse is listening on port 8000,
  2566. <code>https://example.com/.well-known/matrix/server</code> should have <code>m.server</code> set to one of:</p>
  2567. <ol>
  2568. <li>
  2569. <p><code>customer.example.net</code> ─ with a SRV record on
  2570. <code>_matrix._tcp.customer.example.com</code> pointing to port 8000, or:</p>
  2571. </li>
  2572. <li>
  2573. <p><code>customer.example.net</code> ─ updating synapse to listen on the default port
  2574. 8448, or:</p>
  2575. </li>
  2576. <li>
  2577. <p><code>customer.example.net:8000</code> ─ ensuring that if there is a reverse proxy
  2578. on <code>customer.example.net:8000</code> it correctly handles HTTP requests with
  2579. Host header set to <code>customer.example.net:8000</code>.</p>
  2580. </li>
  2581. </ol>
  2582. </li>
  2583. </ol>
  2584. <h2 id="faq"><a class="header" href="#faq">FAQ</a></h2>
  2585. <h3 id="synapse-0990-has-just-been-released-what-do-i-need-to-do-right-now"><a class="header" href="#synapse-0990-has-just-been-released-what-do-i-need-to-do-right-now">Synapse 0.99.0 has just been released, what do I need to do right now?</a></h3>
  2586. <p>Upgrade as soon as you can in preparation for Synapse 1.0.0, and update your
  2587. TLS certificates as <a href="MSC1711_certificates_FAQ.html#configuring-certificates-for-compatibility-with-synapse-100">above</a>.</p>
  2588. <h3 id="what-will-happen-if-i-do-not-set-up-a-valid-federation-certificate-immediately"><a class="header" href="#what-will-happen-if-i-do-not-set-up-a-valid-federation-certificate-immediately">What will happen if I do not set up a valid federation certificate immediately?</a></h3>
  2589. <p>Nothing initially, but once 1.0.0 is in the wild it will not be possible to
  2590. federate with 1.0.0 servers.</p>
  2591. <h3 id="what-will-happen-if-i-do-nothing-at-all"><a class="header" href="#what-will-happen-if-i-do-nothing-at-all">What will happen if I do nothing at all?</a></h3>
  2592. <p>If the admin takes no action at all, and remains on a Synapse &lt; 0.99.0 then the
  2593. homeserver will be unable to federate with those who have implemented
  2594. .well-known. Then, as above, once the month upgrade window has expired the
  2595. homeserver will not be able to federate with any Synapse &gt;= 1.0.0</p>
  2596. <h3 id="when-do-i-need-a-srv-record-or-well-known-uri"><a class="header" href="#when-do-i-need-a-srv-record-or-well-known-uri">When do I need a SRV record or .well-known URI?</a></h3>
  2597. <p>If your homeserver listens on the default federation port (8448), and your
  2598. <code>server_name</code> points to the host that your homeserver runs on, you do not need an
  2599. SRV record or <code>.well-known/matrix/server</code> URI.</p>
  2600. <p>For instance, if you registered <code>example.com</code> and pointed its DNS A record at a
  2601. fresh Upcloud VPS or similar, you could install Synapse 0.99 on that host,
  2602. giving it a server_name of <code>example.com</code>, and it would automatically generate a
  2603. valid TLS certificate for you via Let's Encrypt and no SRV record or
  2604. <code>.well-known</code> URI would be needed.</p>
  2605. <p>This is the common case, although you can add an SRV record or
  2606. <code>.well-known/matrix/server</code> URI for completeness if you wish.</p>
  2607. <p><strong>However</strong>, if your server does not listen on port 8448, or if your <code>server_name</code>
  2608. does not point to the host that your homeserver runs on, you will need to let
  2609. other servers know how to find it.</p>
  2610. <p>In this case, you should see <a href="MSC1711_certificates_FAQ.html#if-you-do-have-an-srv-record-currently">&quot;If you do have an SRV record
  2611. currently&quot;</a> above.</p>
  2612. <h3 id="can-i-still-use-an-srv-record"><a class="header" href="#can-i-still-use-an-srv-record">Can I still use an SRV record?</a></h3>
  2613. <p>Firstly, if you didn't need an SRV record before (because your server is
  2614. listening on port 8448 of your server_name), you certainly don't need one now:
  2615. the defaults are still the same.</p>
  2616. <p>If you previously had an SRV record, you can keep using it provided you are
  2617. able to give Synapse a TLS certificate corresponding to your server name. For
  2618. example, suppose you had the following SRV record, which directs matrix traffic
  2619. for example.com to matrix.example.com:443:</p>
  2620. <pre><code>_matrix._tcp.example.com. IN SRV 10 5 443 matrix.example.com
  2621. </code></pre>
  2622. <p>In this case, Synapse must be given a certificate for example.com - or be
  2623. configured to acquire one from Let's Encrypt.</p>
  2624. <p>If you are unable to give Synapse a certificate for your server_name, you will
  2625. also need to use a .well-known URI instead. However, see also &quot;I have created a
  2626. .well-known URI. Do I still need an SRV record?&quot;.</p>
  2627. <h3 id="i-have-created-a-well-known-uri-do-i-still-need-an-srv-record"><a class="header" href="#i-have-created-a-well-known-uri-do-i-still-need-an-srv-record">I have created a .well-known URI. Do I still need an SRV record?</a></h3>
  2628. <p>As of Synapse 0.99, Synapse will first check for the existence of a <code>.well-known</code>
  2629. URI and follow any delegation it suggests. It will only then check for the
  2630. existence of an SRV record.</p>
  2631. <p>That means that the SRV record will often be redundant. However, you should
  2632. remember that there may still be older versions of Synapse in the federation
  2633. which do not understand <code>.well-known</code> URIs, so if you removed your SRV record you
  2634. would no longer be able to federate with them.</p>
  2635. <p>It is therefore best to leave the SRV record in place for now. Synapse 0.34 and
  2636. earlier will follow the SRV record (and not care about the invalid
  2637. certificate). Synapse 0.99 and later will follow the .well-known URI, with the
  2638. correct certificate chain.</p>
  2639. <h3 id="it-used-to-work-just-fine-why-are-you-breaking-everything"><a class="header" href="#it-used-to-work-just-fine-why-are-you-breaking-everything">It used to work just fine, why are you breaking everything?</a></h3>
  2640. <p>We have always wanted Matrix servers to be as easy to set up as possible, and
  2641. so back when we started federation in 2014 we didn't want admins to have to go
  2642. through the cumbersome process of buying a valid TLS certificate to run a
  2643. server. This was before Let's Encrypt came along and made getting a free and
  2644. valid TLS certificate straightforward. So instead, we adopted a system based on
  2645. <a href="https://en.wikipedia.org/wiki/Convergence_(SSL)">Perspectives</a>: an approach
  2646. where you check a set of &quot;notary servers&quot; (in practice, homeservers) to vouch
  2647. for the validity of a certificate rather than having it signed by a CA. As long
  2648. as enough different notaries agree on the certificate's validity, then it is
  2649. trusted.</p>
  2650. <p>However, in practice this has never worked properly. Most people only use the
  2651. default notary server (matrix.org), leading to inadvertent centralisation which
  2652. we want to eliminate. Meanwhile, we never implemented the full consensus
  2653. algorithm to query the servers participating in a room to determine consensus
  2654. on whether a given certificate is valid. This is fiddly to get right
  2655. (especially in face of sybil attacks), and we found ourselves questioning
  2656. whether it was worth the effort to finish the work and commit to maintaining a
  2657. secure certificate validation system as opposed to focusing on core Matrix
  2658. development.</p>
  2659. <p>Meanwhile, Let's Encrypt came along in 2016, and put the final nail in the
  2660. coffin of the Perspectives project (which was already pretty dead). So, the
  2661. Spec Core Team decided that a better approach would be to mandate valid TLS
  2662. certificates for federation alongside the rest of the Web. More details can be
  2663. found in
  2664. <a href="https://github.com/matrix-org/matrix-doc/blob/master/proposals/1711-x509-for-federation.md#background-the-failure-of-the-perspectives-approach">MSC1711</a>.</p>
  2665. <p>This results in a breaking change, which is disruptive, but absolutely critical
  2666. for the security model. However, the existence of Let's Encrypt as a trivial
  2667. way to replace the old self-signed certificates with valid CA-signed ones helps
  2668. smooth things over massively, especially as Synapse can now automate Let's
  2669. Encrypt certificate generation if needed.</p>
  2670. <h3 id="can-i-manage-my-own-certificates-rather-than-having-synapse-renew-certificates-itself"><a class="header" href="#can-i-manage-my-own-certificates-rather-than-having-synapse-renew-certificates-itself">Can I manage my own certificates rather than having Synapse renew certificates itself?</a></h3>
  2671. <p>Yes, you are welcome to manage your certificates yourself. Synapse will only
  2672. attempt to obtain certificates from Let's Encrypt if you configure it to do
  2673. so.The only requirement is that there is a valid TLS cert present for
  2674. federation end points.</p>
  2675. <h3 id="do-you-still-recommend-against-using-a-reverse-proxy-on-the-federation-port-1"><a class="header" href="#do-you-still-recommend-against-using-a-reverse-proxy-on-the-federation-port-1">Do you still recommend against using a reverse proxy on the federation port?</a></h3>
  2676. <p>We no longer actively recommend against using a reverse proxy. Many admins will
  2677. find it easier to direct federation traffic to a reverse proxy and manage their
  2678. own TLS certificates, and this is a supported configuration.</p>
  2679. <p>See <a href="reverse_proxy.html">reverse_proxy.md</a> for information on setting up a
  2680. reverse proxy.</p>
  2681. <h3 id="do-i-still-need-to-give-my-tls-certificates-to-synapse-if-i-am-using-a-reverse-proxy-1"><a class="header" href="#do-i-still-need-to-give-my-tls-certificates-to-synapse-if-i-am-using-a-reverse-proxy-1">Do I still need to give my TLS certificates to Synapse if I am using a reverse proxy?</a></h3>
  2682. <p>Practically speaking, this is no longer necessary.</p>
  2683. <p>If you are using a reverse proxy for all of your TLS traffic, then you can set
  2684. <code>no_tls: True</code>. In that case, the only reason Synapse needs the certificate is
  2685. to populate a legacy 'tls_fingerprints' field in the federation API. This is
  2686. ignored by Synapse 0.99.0 and later, and the only time pre-0.99 Synapses will
  2687. check it is when attempting to fetch the server keys - and generally this is
  2688. delegated via <code>matrix.org</code>, which is on 0.99.0.</p>
  2689. <p>However, there is a bug in Synapse 0.99.0
  2690. <a href="https://github.com/matrix-org/synapse/issues/4554">4554</a> which prevents
  2691. Synapse from starting if you do not give it a TLS certificate. To work around
  2692. this, you can give it any TLS certificate at all. This will be fixed soon.</p>
  2693. <h3 id="do-i-need-the-same-certificate-for-the-client-and-federation-port-1"><a class="header" href="#do-i-need-the-same-certificate-for-the-client-and-federation-port-1">Do I need the same certificate for the client and federation port?</a></h3>
  2694. <p>No. There is nothing stopping you from using different certificates,
  2695. particularly if you are using a reverse proxy. However, Synapse will use the
  2696. same certificate on any ports where TLS is configured.</p>
  2697. <h3 id="how-do-i-tell-synapse-to-reload-my-keyscertificates-after-i-replace-them"><a class="header" href="#how-do-i-tell-synapse-to-reload-my-keyscertificates-after-i-replace-them">How do I tell Synapse to reload my keys/certificates after I replace them?</a></h3>
  2698. <p>Synapse will reload the keys and certificates when it receives a SIGHUP - for
  2699. example <code>kill -HUP $(cat homeserver.pid)</code>. Alternatively, simply restart
  2700. Synapse, though this will result in downtime while it restarts.</p>
  2701. <div style="break-before: page; page-break-before: always;"></div><h1 id="setting-up-federation"><a class="header" href="#setting-up-federation">Setting up federation</a></h1>
  2702. <p>Federation is the process by which users on different servers can participate
  2703. in the same room. For this to work, those other servers must be able to contact
  2704. yours to send messages.</p>
  2705. <p>The <code>server_name</code> configured in the Synapse configuration file (often
  2706. <code>homeserver.yaml</code>) defines how resources (users, rooms, etc.) will be
  2707. identified (eg: <code>@user:example.com</code>, <code>#room:example.com</code>). By default,
  2708. it is also the domain that other servers will use to try to reach your
  2709. server (via port 8448). This is easy to set up and will work provided
  2710. you set the <code>server_name</code> to match your machine's public DNS hostname.</p>
  2711. <p>For this default configuration to work, you will need to listen for TLS
  2712. connections on port 8448. The preferred way to do that is by using a
  2713. reverse proxy: see <a href="reverse_proxy.html">reverse_proxy.md</a> for instructions
  2714. on how to correctly set one up.</p>
  2715. <p>In some cases you might not want to run Synapse on the machine that has
  2716. the <code>server_name</code> as its public DNS hostname, or you might want federation
  2717. traffic to use a different port than 8448. For example, you might want to
  2718. have your user names look like <code>@user:example.com</code>, but you want to run
  2719. Synapse on <code>synapse.example.com</code> on port 443. This can be done using
  2720. delegation, which allows an admin to control where federation traffic should
  2721. be sent. See <a href="delegate.html">delegate.md</a> for instructions on how to set this up.</p>
  2722. <p>Once federation has been configured, you should be able to join a room over
  2723. federation. A good place to start is <code>#synapse:matrix.org</code> - a room for
  2724. Synapse admins.</p>
  2725. <h2 id="troubleshooting-2"><a class="header" href="#troubleshooting-2">Troubleshooting</a></h2>
  2726. <p>You can use the <a href="https://matrix.org/federationtester">federation tester</a>
  2727. to check if your homeserver is configured correctly. Alternatively try the
  2728. <a href="https://matrix.org/federationtester/api/report?server_name=DOMAIN">JSON API used by the federation tester</a>.
  2729. Note that you'll have to modify this URL to replace <code>DOMAIN</code> with your
  2730. <code>server_name</code>. Hitting the API directly provides extra detail.</p>
  2731. <p>The typical failure mode for federation is that when the server tries to join
  2732. a room, it is rejected with &quot;401: Unauthorized&quot;. Generally this means that other
  2733. servers in the room could not access yours. (Joining a room over federation is
  2734. a complicated dance which requires connections in both directions).</p>
  2735. <p>Another common problem is that people on other servers can't join rooms that
  2736. you invite them to. This can be caused by an incorrectly-configured reverse
  2737. proxy: see <a href="reverse_proxy.html">reverse_proxy.md</a> for instructions on how to correctly
  2738. configure a reverse proxy.</p>
  2739. <h3 id="known-issues"><a class="header" href="#known-issues">Known issues</a></h3>
  2740. <p><strong>HTTP <code>308 Permanent Redirect</code> redirects are not followed</strong>: Due to missing features
  2741. in the HTTP library used by Synapse, 308 redirects are currently not followed by
  2742. federating servers, which can cause <code>M_UNKNOWN</code> or <code>401 Unauthorized</code> errors. This
  2743. may affect users who are redirecting apex-to-www (e.g. <code>example.com</code> -&gt; <code>www.example.com</code>),
  2744. and especially users of the Kubernetes <em>Nginx Ingress</em> module, which uses 308 redirect
  2745. codes by default. For those Kubernetes users, <a href="https://stackoverflow.com/a/52617528/5096871">this Stackoverflow post</a>
  2746. might be helpful. For other users, switching to a <code>301 Moved Permanently</code> code may be
  2747. an option. 308 redirect codes will be supported properly in a future
  2748. release of Synapse.</p>
  2749. <h2 id="running-a-demo-federation-of-synapses"><a class="header" href="#running-a-demo-federation-of-synapses">Running a demo federation of Synapses</a></h2>
  2750. <p>If you want to get up and running quickly with a trio of homeservers in a
  2751. private federation, there is a script in the <code>demo</code> directory. This is mainly
  2752. useful just for development purposes. See <a href="https://github.com/matrix-org/synapse/tree/develop/demo/">demo/README</a>.</p>
  2753. <div style="break-before: page; page-break-before: always;"></div><h1 id="configuration-1"><a class="header" href="#configuration-1">Configuration</a></h1>
  2754. <p>This section contains information on tweaking Synapse via the various options in the configuration file. A configuration
  2755. file should have been generated when you <a href="usage/configuration/../../setup/installation.html">installed Synapse</a>.</p>
  2756. <div style="break-before: page; page-break-before: always;"></div><h1 id="homeserver-sample-configuration-file"><a class="header" href="#homeserver-sample-configuration-file">Homeserver Sample Configuration File</a></h1>
  2757. <p>Below is a sample homeserver configuration file. The homeserver configuration file
  2758. can be tweaked to change the behaviour of your homeserver. A restart of the server is
  2759. generally required to apply any changes made to this file.</p>
  2760. <p>Note that the contents below are <em>not</em> intended to be copied and used as the basis for
  2761. a real homeserver.yaml. Instead, if you are starting from scratch, please generate
  2762. a fresh config using Synapse by following the instructions in
  2763. <a href="usage/configuration/../../setup/installation.html">Installation</a>.</p>
  2764. <pre><code class="language-yaml"># This file is maintained as an up-to-date snapshot of the default
  2765. # homeserver.yaml configuration generated by Synapse.
  2766. #
  2767. # It is intended to act as a reference for the default configuration,
  2768. # helping admins keep track of new options and other changes, and compare
  2769. # their configs with the current default. As such, many of the actual
  2770. # config values shown are placeholders.
  2771. #
  2772. # It is *not* intended to be copied and used as the basis for a real
  2773. # homeserver.yaml. Instead, if you are starting from scratch, please generate
  2774. # a fresh config using Synapse by following the instructions in
  2775. # https://matrix-org.github.io/synapse/latest/setup/installation.html.
  2776. # Configuration options that take a time period can be set using a number
  2777. # followed by a letter. Letters have the following meanings:
  2778. # s = second
  2779. # m = minute
  2780. # h = hour
  2781. # d = day
  2782. # w = week
  2783. # y = year
  2784. # For example, setting redaction_retention_period: 5m would remove redacted
  2785. # messages from the database after 5 minutes, rather than 5 months.
  2786. ################################################################################
  2787. # Configuration file for Synapse.
  2788. #
  2789. # This is a YAML file: see [1] for a quick introduction. Note in particular
  2790. # that *indentation is important*: all the elements of a list or dictionary
  2791. # should have the same indentation.
  2792. #
  2793. # [1] https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html
  2794. ## Modules ##
  2795. # Server admins can expand Synapse's functionality with external modules.
  2796. #
  2797. # See https://matrix-org.github.io/synapse/latest/modules.html for more
  2798. # documentation on how to configure or create custom modules for Synapse.
  2799. #
  2800. modules:
  2801. # - module: my_super_module.MySuperClass
  2802. # config:
  2803. # do_thing: true
  2804. # - module: my_other_super_module.SomeClass
  2805. # config: {}
  2806. ## Server ##
  2807. # The public-facing domain of the server
  2808. #
  2809. # The server_name name will appear at the end of usernames and room addresses
  2810. # created on this server. For example if the server_name was example.com,
  2811. # usernames on this server would be in the format @user:example.com
  2812. #
  2813. # In most cases you should avoid using a matrix specific subdomain such as
  2814. # matrix.example.com or synapse.example.com as the server_name for the same
  2815. # reasons you wouldn't use user@email.example.com as your email address.
  2816. # See https://matrix-org.github.io/synapse/latest/delegate.html
  2817. # for information on how to host Synapse on a subdomain while preserving
  2818. # a clean server_name.
  2819. #
  2820. # The server_name cannot be changed later so it is important to
  2821. # configure this correctly before you start Synapse. It should be all
  2822. # lowercase and may contain an explicit port.
  2823. # Examples: matrix.org, localhost:8080
  2824. #
  2825. server_name: &quot;SERVERNAME&quot;
  2826. # When running as a daemon, the file to store the pid in
  2827. #
  2828. pid_file: DATADIR/homeserver.pid
  2829. # The absolute URL to the web client which /_matrix/client will redirect
  2830. # to if 'webclient' is configured under the 'listeners' configuration.
  2831. #
  2832. # This option can be also set to the filesystem path to the web client
  2833. # which will be served at /_matrix/client/ if 'webclient' is configured
  2834. # under the 'listeners' configuration, however this is a security risk:
  2835. # https://github.com/matrix-org/synapse#security-note
  2836. #
  2837. #web_client_location: https://riot.example.com/
  2838. # The public-facing base URL that clients use to access this Homeserver (not
  2839. # including _matrix/...). This is the same URL a user might enter into the
  2840. # 'Custom Homeserver URL' field on their client. If you use Synapse with a
  2841. # reverse proxy, this should be the URL to reach Synapse via the proxy.
  2842. # Otherwise, it should be the URL to reach Synapse's client HTTP listener (see
  2843. # 'listeners' below).
  2844. #
  2845. #public_baseurl: https://example.com/
  2846. # Set the soft limit on the number of file descriptors synapse can use
  2847. # Zero is used to indicate synapse should set the soft limit to the
  2848. # hard limit.
  2849. #
  2850. #soft_file_limit: 0
  2851. # Presence tracking allows users to see the state (e.g online/offline)
  2852. # of other local and remote users.
  2853. #
  2854. presence:
  2855. # Uncomment to disable presence tracking on this homeserver. This option
  2856. # replaces the previous top-level 'use_presence' option.
  2857. #
  2858. #enabled: false
  2859. # Presence routers are third-party modules that can specify additional logic
  2860. # to where presence updates from users are routed.
  2861. #
  2862. presence_router:
  2863. # The custom module's class. Uncomment to use a custom presence router module.
  2864. #
  2865. #module: &quot;my_custom_router.PresenceRouter&quot;
  2866. # Configuration options of the custom module. Refer to your module's
  2867. # documentation for available options.
  2868. #
  2869. #config:
  2870. # example_option: 'something'
  2871. # Whether to require authentication to retrieve profile data (avatars,
  2872. # display names) of other users through the client API. Defaults to
  2873. # 'false'. Note that profile data is also available via the federation
  2874. # API, unless allow_profile_lookup_over_federation is set to false.
  2875. #
  2876. #require_auth_for_profile_requests: true
  2877. # Uncomment to require a user to share a room with another user in order
  2878. # to retrieve their profile information. Only checked on Client-Server
  2879. # requests. Profile requests from other servers should be checked by the
  2880. # requesting server. Defaults to 'false'.
  2881. #
  2882. #limit_profile_requests_to_users_who_share_rooms: true
  2883. # Uncomment to prevent a user's profile data from being retrieved and
  2884. # displayed in a room until they have joined it. By default, a user's
  2885. # profile data is included in an invite event, regardless of the values
  2886. # of the above two settings, and whether or not the users share a server.
  2887. # Defaults to 'true'.
  2888. #
  2889. #include_profile_data_on_invite: false
  2890. # If set to 'true', removes the need for authentication to access the server's
  2891. # public rooms directory through the client API, meaning that anyone can
  2892. # query the room directory. Defaults to 'false'.
  2893. #
  2894. #allow_public_rooms_without_auth: true
  2895. # If set to 'true', allows any other homeserver to fetch the server's public
  2896. # rooms directory via federation. Defaults to 'false'.
  2897. #
  2898. #allow_public_rooms_over_federation: true
  2899. # The default room version for newly created rooms.
  2900. #
  2901. # Known room versions are listed here:
  2902. # https://matrix.org/docs/spec/#complete-list-of-room-versions
  2903. #
  2904. # For example, for room version 1, default_room_version should be set
  2905. # to &quot;1&quot;.
  2906. #
  2907. #default_room_version: &quot;6&quot;
  2908. # The GC threshold parameters to pass to `gc.set_threshold`, if defined
  2909. #
  2910. #gc_thresholds: [700, 10, 10]
  2911. # The minimum time in seconds between each GC for a generation, regardless of
  2912. # the GC thresholds. This ensures that we don't do GC too frequently.
  2913. #
  2914. # A value of `[1s, 10s, 30s]` indicates that a second must pass between consecutive
  2915. # generation 0 GCs, etc.
  2916. #
  2917. # Defaults to `[1s, 10s, 30s]`.
  2918. #
  2919. #gc_min_interval: [0.5s, 30s, 1m]
  2920. # Set the limit on the returned events in the timeline in the get
  2921. # and sync operations. The default value is 100. -1 means no upper limit.
  2922. #
  2923. # Uncomment the following to increase the limit to 5000.
  2924. #
  2925. #filter_timeline_limit: 5000
  2926. # Whether room invites to users on this server should be blocked
  2927. # (except those sent by local server admins). The default is False.
  2928. #
  2929. #block_non_admin_invites: true
  2930. # Room searching
  2931. #
  2932. # If disabled, new messages will not be indexed for searching and users
  2933. # will receive errors when searching for messages. Defaults to enabled.
  2934. #
  2935. #enable_search: false
  2936. # Prevent outgoing requests from being sent to the following blacklisted IP address
  2937. # CIDR ranges. If this option is not specified then it defaults to private IP
  2938. # address ranges (see the example below).
  2939. #
  2940. # The blacklist applies to the outbound requests for federation, identity servers,
  2941. # push servers, and for checking key validity for third-party invite events.
  2942. #
  2943. # (0.0.0.0 and :: are always blacklisted, whether or not they are explicitly
  2944. # listed here, since they correspond to unroutable addresses.)
  2945. #
  2946. # This option replaces federation_ip_range_blacklist in Synapse v1.25.0.
  2947. #
  2948. #ip_range_blacklist:
  2949. # - '127.0.0.0/8'
  2950. # - '10.0.0.0/8'
  2951. # - '172.16.0.0/12'
  2952. # - '192.168.0.0/16'
  2953. # - '100.64.0.0/10'
  2954. # - '192.0.0.0/24'
  2955. # - '169.254.0.0/16'
  2956. # - '192.88.99.0/24'
  2957. # - '198.18.0.0/15'
  2958. # - '192.0.2.0/24'
  2959. # - '198.51.100.0/24'
  2960. # - '203.0.113.0/24'
  2961. # - '224.0.0.0/4'
  2962. # - '::1/128'
  2963. # - 'fe80::/10'
  2964. # - 'fc00::/7'
  2965. # - '2001:db8::/32'
  2966. # - 'ff00::/8'
  2967. # - 'fec0::/10'
  2968. # List of IP address CIDR ranges that should be allowed for federation,
  2969. # identity servers, push servers, and for checking key validity for
  2970. # third-party invite events. This is useful for specifying exceptions to
  2971. # wide-ranging blacklisted target IP ranges - e.g. for communication with
  2972. # a push server only visible in your network.
  2973. #
  2974. # This whitelist overrides ip_range_blacklist and defaults to an empty
  2975. # list.
  2976. #
  2977. #ip_range_whitelist:
  2978. # - '192.168.1.1'
  2979. # List of ports that Synapse should listen on, their purpose and their
  2980. # configuration.
  2981. #
  2982. # Options for each listener include:
  2983. #
  2984. # port: the TCP port to bind to
  2985. #
  2986. # bind_addresses: a list of local addresses to listen on. The default is
  2987. # 'all local interfaces'.
  2988. #
  2989. # type: the type of listener. Normally 'http', but other valid options are:
  2990. # 'manhole' (see https://matrix-org.github.io/synapse/latest/manhole.html),
  2991. # 'metrics' (see https://matrix-org.github.io/synapse/latest/metrics-howto.html),
  2992. # 'replication' (see https://matrix-org.github.io/synapse/latest/workers.html).
  2993. #
  2994. # tls: set to true to enable TLS for this listener. Will use the TLS
  2995. # key/cert specified in tls_private_key_path / tls_certificate_path.
  2996. #
  2997. # x_forwarded: Only valid for an 'http' listener. Set to true to use the
  2998. # X-Forwarded-For header as the client IP. Useful when Synapse is
  2999. # behind a reverse-proxy.
  3000. #
  3001. # resources: Only valid for an 'http' listener. A list of resources to host
  3002. # on this port. Options for each resource are:
  3003. #
  3004. # names: a list of names of HTTP resources. See below for a list of
  3005. # valid resource names.
  3006. #
  3007. # compress: set to true to enable HTTP compression for this resource.
  3008. #
  3009. # additional_resources: Only valid for an 'http' listener. A map of
  3010. # additional endpoints which should be loaded via dynamic modules.
  3011. #
  3012. # Valid resource names are:
  3013. #
  3014. # client: the client-server API (/_matrix/client), and the synapse admin
  3015. # API (/_synapse/admin). Also implies 'media' and 'static'.
  3016. #
  3017. # consent: user consent forms (/_matrix/consent).
  3018. # See https://matrix-org.github.io/synapse/latest/consent_tracking.html.
  3019. #
  3020. # federation: the server-server API (/_matrix/federation). Also implies
  3021. # 'media', 'keys', 'openid'
  3022. #
  3023. # keys: the key discovery API (/_matrix/keys).
  3024. #
  3025. # media: the media API (/_matrix/media).
  3026. #
  3027. # metrics: the metrics interface.
  3028. # See https://matrix-org.github.io/synapse/latest/metrics-howto.html.
  3029. #
  3030. # openid: OpenID authentication.
  3031. #
  3032. # replication: the HTTP replication API (/_synapse/replication).
  3033. # See https://matrix-org.github.io/synapse/latest/workers.html.
  3034. #
  3035. # static: static resources under synapse/static (/_matrix/static). (Mostly
  3036. # useful for 'fallback authentication'.)
  3037. #
  3038. # webclient: A web client. Requires web_client_location to be set.
  3039. #
  3040. listeners:
  3041. # TLS-enabled listener: for when matrix traffic is sent directly to synapse.
  3042. #
  3043. # Disabled by default. To enable it, uncomment the following. (Note that you
  3044. # will also need to give Synapse a TLS key and certificate: see the TLS section
  3045. # below.)
  3046. #
  3047. #- port: 8448
  3048. # type: http
  3049. # tls: true
  3050. # resources:
  3051. # - names: [client, federation]
  3052. # Unsecure HTTP listener: for when matrix traffic passes through a reverse proxy
  3053. # that unwraps TLS.
  3054. #
  3055. # If you plan to use a reverse proxy, please see
  3056. # https://matrix-org.github.io/synapse/latest/reverse_proxy.html.
  3057. #
  3058. - port: 8008
  3059. tls: false
  3060. type: http
  3061. x_forwarded: true
  3062. bind_addresses: ['::1', '127.0.0.1']
  3063. resources:
  3064. - names: [client, federation]
  3065. compress: false
  3066. # example additional_resources:
  3067. #
  3068. #additional_resources:
  3069. # &quot;/_matrix/my/custom/endpoint&quot;:
  3070. # module: my_module.CustomRequestHandler
  3071. # config: {}
  3072. # Turn on the twisted ssh manhole service on localhost on the given
  3073. # port.
  3074. #
  3075. #- port: 9000
  3076. # bind_addresses: ['::1', '127.0.0.1']
  3077. # type: manhole
  3078. # Forward extremities can build up in a room due to networking delays between
  3079. # homeservers. Once this happens in a large room, calculation of the state of
  3080. # that room can become quite expensive. To mitigate this, once the number of
  3081. # forward extremities reaches a given threshold, Synapse will send an
  3082. # org.matrix.dummy_event event, which will reduce the forward extremities
  3083. # in the room.
  3084. #
  3085. # This setting defines the threshold (i.e. number of forward extremities in the
  3086. # room) at which dummy events are sent. The default value is 10.
  3087. #
  3088. #dummy_events_threshold: 5
  3089. ## Homeserver blocking ##
  3090. # How to reach the server admin, used in ResourceLimitError
  3091. #
  3092. #admin_contact: 'mailto:admin@server.com'
  3093. # Global blocking
  3094. #
  3095. #hs_disabled: false
  3096. #hs_disabled_message: 'Human readable reason for why the HS is blocked'
  3097. # Monthly Active User Blocking
  3098. #
  3099. # Used in cases where the admin or server owner wants to limit to the
  3100. # number of monthly active users.
  3101. #
  3102. # 'limit_usage_by_mau' disables/enables monthly active user blocking. When
  3103. # enabled and a limit is reached the server returns a 'ResourceLimitError'
  3104. # with error type Codes.RESOURCE_LIMIT_EXCEEDED
  3105. #
  3106. # 'max_mau_value' is the hard limit of monthly active users above which
  3107. # the server will start blocking user actions.
  3108. #
  3109. # 'mau_trial_days' is a means to add a grace period for active users. It
  3110. # means that users must be active for this number of days before they
  3111. # can be considered active and guards against the case where lots of users
  3112. # sign up in a short space of time never to return after their initial
  3113. # session.
  3114. #
  3115. # 'mau_limit_alerting' is a means of limiting client side alerting
  3116. # should the mau limit be reached. This is useful for small instances
  3117. # where the admin has 5 mau seats (say) for 5 specific people and no
  3118. # interest increasing the mau limit further. Defaults to True, which
  3119. # means that alerting is enabled
  3120. #
  3121. #limit_usage_by_mau: false
  3122. #max_mau_value: 50
  3123. #mau_trial_days: 2
  3124. #mau_limit_alerting: false
  3125. # If enabled, the metrics for the number of monthly active users will
  3126. # be populated, however no one will be limited. If limit_usage_by_mau
  3127. # is true, this is implied to be true.
  3128. #
  3129. #mau_stats_only: false
  3130. # Sometimes the server admin will want to ensure certain accounts are
  3131. # never blocked by mau checking. These accounts are specified here.
  3132. #
  3133. #mau_limit_reserved_threepids:
  3134. # - medium: 'email'
  3135. # address: 'reserved_user@example.com'
  3136. # Used by phonehome stats to group together related servers.
  3137. #server_context: context
  3138. # Resource-constrained homeserver settings
  3139. #
  3140. # When this is enabled, the room &quot;complexity&quot; will be checked before a user
  3141. # joins a new remote room. If it is above the complexity limit, the server will
  3142. # disallow joining, or will instantly leave.
  3143. #
  3144. # Room complexity is an arbitrary measure based on factors such as the number of
  3145. # users in the room.
  3146. #
  3147. limit_remote_rooms:
  3148. # Uncomment to enable room complexity checking.
  3149. #
  3150. #enabled: true
  3151. # the limit above which rooms cannot be joined. The default is 1.0.
  3152. #
  3153. #complexity: 0.5
  3154. # override the error which is returned when the room is too complex.
  3155. #
  3156. #complexity_error: &quot;This room is too complex.&quot;
  3157. # allow server admins to join complex rooms. Default is false.
  3158. #
  3159. #admins_can_join: true
  3160. # Whether to require a user to be in the room to add an alias to it.
  3161. # Defaults to 'true'.
  3162. #
  3163. #require_membership_for_aliases: false
  3164. # Whether to allow per-room membership profiles through the send of membership
  3165. # events with profile information that differ from the target's global profile.
  3166. # Defaults to 'true'.
  3167. #
  3168. #allow_per_room_profiles: false
  3169. # How long to keep redacted events in unredacted form in the database. After
  3170. # this period redacted events get replaced with their redacted form in the DB.
  3171. #
  3172. # Defaults to `7d`. Set to `null` to disable.
  3173. #
  3174. #redaction_retention_period: 28d
  3175. # How long to track users' last seen time and IPs in the database.
  3176. #
  3177. # Defaults to `28d`. Set to `null` to disable clearing out of old rows.
  3178. #
  3179. #user_ips_max_age: 14d
  3180. # Message retention policy at the server level.
  3181. #
  3182. # Room admins and mods can define a retention period for their rooms using the
  3183. # 'm.room.retention' state event, and server admins can cap this period by setting
  3184. # the 'allowed_lifetime_min' and 'allowed_lifetime_max' config options.
  3185. #
  3186. # If this feature is enabled, Synapse will regularly look for and purge events
  3187. # which are older than the room's maximum retention period. Synapse will also
  3188. # filter events received over federation so that events that should have been
  3189. # purged are ignored and not stored again.
  3190. #
  3191. retention:
  3192. # The message retention policies feature is disabled by default. Uncomment the
  3193. # following line to enable it.
  3194. #
  3195. #enabled: true
  3196. # Default retention policy. If set, Synapse will apply it to rooms that lack the
  3197. # 'm.room.retention' state event. Currently, the value of 'min_lifetime' doesn't
  3198. # matter much because Synapse doesn't take it into account yet.
  3199. #
  3200. #default_policy:
  3201. # min_lifetime: 1d
  3202. # max_lifetime: 1y
  3203. # Retention policy limits. If set, and the state of a room contains a
  3204. # 'm.room.retention' event in its state which contains a 'min_lifetime' or a
  3205. # 'max_lifetime' that's out of these bounds, Synapse will cap the room's policy
  3206. # to these limits when running purge jobs.
  3207. #
  3208. #allowed_lifetime_min: 1d
  3209. #allowed_lifetime_max: 1y
  3210. # Server admins can define the settings of the background jobs purging the
  3211. # events which lifetime has expired under the 'purge_jobs' section.
  3212. #
  3213. # If no configuration is provided, a single job will be set up to delete expired
  3214. # events in every room daily.
  3215. #
  3216. # Each job's configuration defines which range of message lifetimes the job
  3217. # takes care of. For example, if 'shortest_max_lifetime' is '2d' and
  3218. # 'longest_max_lifetime' is '3d', the job will handle purging expired events in
  3219. # rooms whose state defines a 'max_lifetime' that's both higher than 2 days, and
  3220. # lower than or equal to 3 days. Both the minimum and the maximum value of a
  3221. # range are optional, e.g. a job with no 'shortest_max_lifetime' and a
  3222. # 'longest_max_lifetime' of '3d' will handle every room with a retention policy
  3223. # which 'max_lifetime' is lower than or equal to three days.
  3224. #
  3225. # The rationale for this per-job configuration is that some rooms might have a
  3226. # retention policy with a low 'max_lifetime', where history needs to be purged
  3227. # of outdated messages on a more frequent basis than for the rest of the rooms
  3228. # (e.g. every 12h), but not want that purge to be performed by a job that's
  3229. # iterating over every room it knows, which could be heavy on the server.
  3230. #
  3231. # If any purge job is configured, it is strongly recommended to have at least
  3232. # a single job with neither 'shortest_max_lifetime' nor 'longest_max_lifetime'
  3233. # set, or one job without 'shortest_max_lifetime' and one job without
  3234. # 'longest_max_lifetime' set. Otherwise some rooms might be ignored, even if
  3235. # 'allowed_lifetime_min' and 'allowed_lifetime_max' are set, because capping a
  3236. # room's policy to these values is done after the policies are retrieved from
  3237. # Synapse's database (which is done using the range specified in a purge job's
  3238. # configuration).
  3239. #
  3240. #purge_jobs:
  3241. # - longest_max_lifetime: 3d
  3242. # interval: 12h
  3243. # - shortest_max_lifetime: 3d
  3244. # interval: 1d
  3245. # Inhibits the /requestToken endpoints from returning an error that might leak
  3246. # information about whether an e-mail address is in use or not on this
  3247. # homeserver.
  3248. # Note that for some endpoints the error situation is the e-mail already being
  3249. # used, and for others the error is entering the e-mail being unused.
  3250. # If this option is enabled, instead of returning an error, these endpoints will
  3251. # act as if no error happened and return a fake session ID ('sid') to clients.
  3252. #
  3253. #request_token_inhibit_3pid_errors: true
  3254. # A list of domains that the domain portion of 'next_link' parameters
  3255. # must match.
  3256. #
  3257. # This parameter is optionally provided by clients while requesting
  3258. # validation of an email or phone number, and maps to a link that
  3259. # users will be automatically redirected to after validation
  3260. # succeeds. Clients can make use this parameter to aid the validation
  3261. # process.
  3262. #
  3263. # The whitelist is applied whether the homeserver or an
  3264. # identity server is handling validation.
  3265. #
  3266. # The default value is no whitelist functionality; all domains are
  3267. # allowed. Setting this value to an empty list will instead disallow
  3268. # all domains.
  3269. #
  3270. #next_link_domain_whitelist: [&quot;matrix.org&quot;]
  3271. ## TLS ##
  3272. # PEM-encoded X509 certificate for TLS.
  3273. # This certificate, as of Synapse 1.0, will need to be a valid and verifiable
  3274. # certificate, signed by a recognised Certificate Authority.
  3275. #
  3276. # Be sure to use a `.pem` file that includes the full certificate chain including
  3277. # any intermediate certificates (for instance, if using certbot, use
  3278. # `fullchain.pem` as your certificate, not `cert.pem`).
  3279. #
  3280. #tls_certificate_path: &quot;CONFDIR/SERVERNAME.tls.crt&quot;
  3281. # PEM-encoded private key for TLS
  3282. #
  3283. #tls_private_key_path: &quot;CONFDIR/SERVERNAME.tls.key&quot;
  3284. # Whether to verify TLS server certificates for outbound federation requests.
  3285. #
  3286. # Defaults to `true`. To disable certificate verification, uncomment the
  3287. # following line.
  3288. #
  3289. #federation_verify_certificates: false
  3290. # The minimum TLS version that will be used for outbound federation requests.
  3291. #
  3292. # Defaults to `1`. Configurable to `1`, `1.1`, `1.2`, or `1.3`. Note
  3293. # that setting this value higher than `1.2` will prevent federation to most
  3294. # of the public Matrix network: only configure it to `1.3` if you have an
  3295. # entirely private federation setup and you can ensure TLS 1.3 support.
  3296. #
  3297. #federation_client_minimum_tls_version: 1.2
  3298. # Skip federation certificate verification on the following whitelist
  3299. # of domains.
  3300. #
  3301. # This setting should only be used in very specific cases, such as
  3302. # federation over Tor hidden services and similar. For private networks
  3303. # of homeservers, you likely want to use a private CA instead.
  3304. #
  3305. # Only effective if federation_verify_certicates is `true`.
  3306. #
  3307. #federation_certificate_verification_whitelist:
  3308. # - lon.example.com
  3309. # - *.domain.com
  3310. # - *.onion
  3311. # List of custom certificate authorities for federation traffic.
  3312. #
  3313. # This setting should only normally be used within a private network of
  3314. # homeservers.
  3315. #
  3316. # Note that this list will replace those that are provided by your
  3317. # operating environment. Certificates must be in PEM format.
  3318. #
  3319. #federation_custom_ca_list:
  3320. # - myCA1.pem
  3321. # - myCA2.pem
  3322. # - myCA3.pem
  3323. ## Federation ##
  3324. # Restrict federation to the following whitelist of domains.
  3325. # N.B. we recommend also firewalling your federation listener to limit
  3326. # inbound federation traffic as early as possible, rather than relying
  3327. # purely on this application-layer restriction. If not specified, the
  3328. # default is to whitelist everything.
  3329. #
  3330. #federation_domain_whitelist:
  3331. # - lon.example.com
  3332. # - nyc.example.com
  3333. # - syd.example.com
  3334. # Report prometheus metrics on the age of PDUs being sent to and received from
  3335. # the following domains. This can be used to give an idea of &quot;delay&quot; on inbound
  3336. # and outbound federation, though be aware that any delay can be due to problems
  3337. # at either end or with the intermediate network.
  3338. #
  3339. # By default, no domains are monitored in this way.
  3340. #
  3341. #federation_metrics_domains:
  3342. # - matrix.org
  3343. # - example.com
  3344. # Uncomment to disable profile lookup over federation. By default, the
  3345. # Federation API allows other homeservers to obtain profile data of any user
  3346. # on this homeserver. Defaults to 'true'.
  3347. #
  3348. #allow_profile_lookup_over_federation: false
  3349. # Uncomment to disable device display name lookup over federation. By default, the
  3350. # Federation API allows other homeservers to obtain device display names of any user
  3351. # on this homeserver. Defaults to 'true'.
  3352. #
  3353. #allow_device_name_lookup_over_federation: false
  3354. ## Caching ##
  3355. # Caching can be configured through the following options.
  3356. #
  3357. # A cache 'factor' is a multiplier that can be applied to each of
  3358. # Synapse's caches in order to increase or decrease the maximum
  3359. # number of entries that can be stored.
  3360. # The number of events to cache in memory. Not affected by
  3361. # caches.global_factor.
  3362. #
  3363. #event_cache_size: 10K
  3364. caches:
  3365. # Controls the global cache factor, which is the default cache factor
  3366. # for all caches if a specific factor for that cache is not otherwise
  3367. # set.
  3368. #
  3369. # This can also be set by the &quot;SYNAPSE_CACHE_FACTOR&quot; environment
  3370. # variable. Setting by environment variable takes priority over
  3371. # setting through the config file.
  3372. #
  3373. # Defaults to 0.5, which will half the size of all caches.
  3374. #
  3375. #global_factor: 1.0
  3376. # A dictionary of cache name to cache factor for that individual
  3377. # cache. Overrides the global cache factor for a given cache.
  3378. #
  3379. # These can also be set through environment variables comprised
  3380. # of &quot;SYNAPSE_CACHE_FACTOR_&quot; + the name of the cache in capital
  3381. # letters and underscores. Setting by environment variable
  3382. # takes priority over setting through the config file.
  3383. # Ex. SYNAPSE_CACHE_FACTOR_GET_USERS_WHO_SHARE_ROOM_WITH_USER=2.0
  3384. #
  3385. # Some caches have '*' and other characters that are not
  3386. # alphanumeric or underscores. These caches can be named with or
  3387. # without the special characters stripped. For example, to specify
  3388. # the cache factor for `*stateGroupCache*` via an environment
  3389. # variable would be `SYNAPSE_CACHE_FACTOR_STATEGROUPCACHE=2.0`.
  3390. #
  3391. per_cache_factors:
  3392. #get_users_who_share_room_with_user: 2.0
  3393. # Controls how long an entry can be in a cache without having been
  3394. # accessed before being evicted. Defaults to None, which means
  3395. # entries are never evicted based on time.
  3396. #
  3397. #expiry_time: 30m
  3398. ## Database ##
  3399. # The 'database' setting defines the database that synapse uses to store all of
  3400. # its data.
  3401. #
  3402. # 'name' gives the database engine to use: either 'sqlite3' (for SQLite) or
  3403. # 'psycopg2' (for PostgreSQL).
  3404. #
  3405. # 'args' gives options which are passed through to the database engine,
  3406. # except for options starting 'cp_', which are used to configure the Twisted
  3407. # connection pool. For a reference to valid arguments, see:
  3408. # * for sqlite: https://docs.python.org/3/library/sqlite3.html#sqlite3.connect
  3409. # * for postgres: https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS
  3410. # * for the connection pool: https://twistedmatrix.com/documents/current/api/twisted.enterprise.adbapi.ConnectionPool.html#__init__
  3411. #
  3412. #
  3413. # Example SQLite configuration:
  3414. #
  3415. #database:
  3416. # name: sqlite3
  3417. # args:
  3418. # database: /path/to/homeserver.db
  3419. #
  3420. #
  3421. # Example Postgres configuration:
  3422. #
  3423. #database:
  3424. # name: psycopg2
  3425. # args:
  3426. # user: synapse_user
  3427. # password: secretpassword
  3428. # database: synapse
  3429. # host: localhost
  3430. # port: 5432
  3431. # cp_min: 5
  3432. # cp_max: 10
  3433. #
  3434. # For more information on using Synapse with Postgres,
  3435. # see https://matrix-org.github.io/synapse/latest/postgres.html.
  3436. #
  3437. database:
  3438. name: sqlite3
  3439. args:
  3440. database: DATADIR/homeserver.db
  3441. ## Logging ##
  3442. # A yaml python logging config file as described by
  3443. # https://docs.python.org/3.7/library/logging.config.html#configuration-dictionary-schema
  3444. #
  3445. log_config: &quot;CONFDIR/SERVERNAME.log.config&quot;
  3446. ## Ratelimiting ##
  3447. # Ratelimiting settings for client actions (registration, login, messaging).
  3448. #
  3449. # Each ratelimiting configuration is made of two parameters:
  3450. # - per_second: number of requests a client can send per second.
  3451. # - burst_count: number of requests a client can send before being throttled.
  3452. #
  3453. # Synapse currently uses the following configurations:
  3454. # - one for messages that ratelimits sending based on the account the client
  3455. # is using
  3456. # - one for registration that ratelimits registration requests based on the
  3457. # client's IP address.
  3458. # - one for login that ratelimits login requests based on the client's IP
  3459. # address.
  3460. # - one for login that ratelimits login requests based on the account the
  3461. # client is attempting to log into.
  3462. # - one for login that ratelimits login requests based on the account the
  3463. # client is attempting to log into, based on the amount of failed login
  3464. # attempts for this account.
  3465. # - one for ratelimiting redactions by room admins. If this is not explicitly
  3466. # set then it uses the same ratelimiting as per rc_message. This is useful
  3467. # to allow room admins to deal with abuse quickly.
  3468. # - two for ratelimiting number of rooms a user can join, &quot;local&quot; for when
  3469. # users are joining rooms the server is already in (this is cheap) vs
  3470. # &quot;remote&quot; for when users are trying to join rooms not on the server (which
  3471. # can be more expensive)
  3472. # - one for ratelimiting how often a user or IP can attempt to validate a 3PID.
  3473. # - two for ratelimiting how often invites can be sent in a room or to a
  3474. # specific user.
  3475. #
  3476. # The defaults are as shown below.
  3477. #
  3478. #rc_message:
  3479. # per_second: 0.2
  3480. # burst_count: 10
  3481. #
  3482. #rc_registration:
  3483. # per_second: 0.17
  3484. # burst_count: 3
  3485. #
  3486. #rc_login:
  3487. # address:
  3488. # per_second: 0.17
  3489. # burst_count: 3
  3490. # account:
  3491. # per_second: 0.17
  3492. # burst_count: 3
  3493. # failed_attempts:
  3494. # per_second: 0.17
  3495. # burst_count: 3
  3496. #
  3497. #rc_admin_redaction:
  3498. # per_second: 1
  3499. # burst_count: 50
  3500. #
  3501. #rc_joins:
  3502. # local:
  3503. # per_second: 0.1
  3504. # burst_count: 10
  3505. # remote:
  3506. # per_second: 0.01
  3507. # burst_count: 10
  3508. #
  3509. #rc_3pid_validation:
  3510. # per_second: 0.003
  3511. # burst_count: 5
  3512. #
  3513. #rc_invites:
  3514. # per_room:
  3515. # per_second: 0.3
  3516. # burst_count: 10
  3517. # per_user:
  3518. # per_second: 0.003
  3519. # burst_count: 5
  3520. # Ratelimiting settings for incoming federation
  3521. #
  3522. # The rc_federation configuration is made up of the following settings:
  3523. # - window_size: window size in milliseconds
  3524. # - sleep_limit: number of federation requests from a single server in
  3525. # a window before the server will delay processing the request.
  3526. # - sleep_delay: duration in milliseconds to delay processing events
  3527. # from remote servers by if they go over the sleep limit.
  3528. # - reject_limit: maximum number of concurrent federation requests
  3529. # allowed from a single server
  3530. # - concurrent: number of federation requests to concurrently process
  3531. # from a single server
  3532. #
  3533. # The defaults are as shown below.
  3534. #
  3535. #rc_federation:
  3536. # window_size: 1000
  3537. # sleep_limit: 10
  3538. # sleep_delay: 500
  3539. # reject_limit: 50
  3540. # concurrent: 3
  3541. # Target outgoing federation transaction frequency for sending read-receipts,
  3542. # per-room.
  3543. #
  3544. # If we end up trying to send out more read-receipts, they will get buffered up
  3545. # into fewer transactions.
  3546. #
  3547. #federation_rr_transactions_per_room_per_second: 50
  3548. ## Media Store ##
  3549. # Enable the media store service in the Synapse master. Uncomment the
  3550. # following if you are using a separate media store worker.
  3551. #
  3552. #enable_media_repo: false
  3553. # Directory where uploaded images and attachments are stored.
  3554. #
  3555. media_store_path: &quot;DATADIR/media_store&quot;
  3556. # Media storage providers allow media to be stored in different
  3557. # locations.
  3558. #
  3559. #media_storage_providers:
  3560. # - module: file_system
  3561. # # Whether to store newly uploaded local files
  3562. # store_local: false
  3563. # # Whether to store newly downloaded remote files
  3564. # store_remote: false
  3565. # # Whether to wait for successful storage for local uploads
  3566. # store_synchronous: false
  3567. # config:
  3568. # directory: /mnt/some/other/directory
  3569. # The largest allowed upload size in bytes
  3570. #
  3571. # If you are using a reverse proxy you may also need to set this value in
  3572. # your reverse proxy's config. Notably Nginx has a small max body size by default.
  3573. # See https://matrix-org.github.io/synapse/latest/reverse_proxy.html.
  3574. #
  3575. #max_upload_size: 50M
  3576. # Maximum number of pixels that will be thumbnailed
  3577. #
  3578. #max_image_pixels: 32M
  3579. # Whether to generate new thumbnails on the fly to precisely match
  3580. # the resolution requested by the client. If true then whenever
  3581. # a new resolution is requested by the client the server will
  3582. # generate a new thumbnail. If false the server will pick a thumbnail
  3583. # from a precalculated list.
  3584. #
  3585. #dynamic_thumbnails: false
  3586. # List of thumbnails to precalculate when an image is uploaded.
  3587. #
  3588. #thumbnail_sizes:
  3589. # - width: 32
  3590. # height: 32
  3591. # method: crop
  3592. # - width: 96
  3593. # height: 96
  3594. # method: crop
  3595. # - width: 320
  3596. # height: 240
  3597. # method: scale
  3598. # - width: 640
  3599. # height: 480
  3600. # method: scale
  3601. # - width: 800
  3602. # height: 600
  3603. # method: scale
  3604. # Is the preview URL API enabled?
  3605. #
  3606. # 'false' by default: uncomment the following to enable it (and specify a
  3607. # url_preview_ip_range_blacklist blacklist).
  3608. #
  3609. #url_preview_enabled: true
  3610. # List of IP address CIDR ranges that the URL preview spider is denied
  3611. # from accessing. There are no defaults: you must explicitly
  3612. # specify a list for URL previewing to work. You should specify any
  3613. # internal services in your network that you do not want synapse to try
  3614. # to connect to, otherwise anyone in any Matrix room could cause your
  3615. # synapse to issue arbitrary GET requests to your internal services,
  3616. # causing serious security issues.
  3617. #
  3618. # (0.0.0.0 and :: are always blacklisted, whether or not they are explicitly
  3619. # listed here, since they correspond to unroutable addresses.)
  3620. #
  3621. # This must be specified if url_preview_enabled is set. It is recommended that
  3622. # you uncomment the following list as a starting point.
  3623. #
  3624. #url_preview_ip_range_blacklist:
  3625. # - '127.0.0.0/8'
  3626. # - '10.0.0.0/8'
  3627. # - '172.16.0.0/12'
  3628. # - '192.168.0.0/16'
  3629. # - '100.64.0.0/10'
  3630. # - '192.0.0.0/24'
  3631. # - '169.254.0.0/16'
  3632. # - '192.88.99.0/24'
  3633. # - '198.18.0.0/15'
  3634. # - '192.0.2.0/24'
  3635. # - '198.51.100.0/24'
  3636. # - '203.0.113.0/24'
  3637. # - '224.0.0.0/4'
  3638. # - '::1/128'
  3639. # - 'fe80::/10'
  3640. # - 'fc00::/7'
  3641. # - '2001:db8::/32'
  3642. # - 'ff00::/8'
  3643. # - 'fec0::/10'
  3644. # List of IP address CIDR ranges that the URL preview spider is allowed
  3645. # to access even if they are specified in url_preview_ip_range_blacklist.
  3646. # This is useful for specifying exceptions to wide-ranging blacklisted
  3647. # target IP ranges - e.g. for enabling URL previews for a specific private
  3648. # website only visible in your network.
  3649. #
  3650. #url_preview_ip_range_whitelist:
  3651. # - '192.168.1.1'
  3652. # Optional list of URL matches that the URL preview spider is
  3653. # denied from accessing. You should use url_preview_ip_range_blacklist
  3654. # in preference to this, otherwise someone could define a public DNS
  3655. # entry that points to a private IP address and circumvent the blacklist.
  3656. # This is more useful if you know there is an entire shape of URL that
  3657. # you know that will never want synapse to try to spider.
  3658. #
  3659. # Each list entry is a dictionary of url component attributes as returned
  3660. # by urlparse.urlsplit as applied to the absolute form of the URL. See
  3661. # https://docs.python.org/2/library/urlparse.html#urlparse.urlsplit
  3662. # The values of the dictionary are treated as an filename match pattern
  3663. # applied to that component of URLs, unless they start with a ^ in which
  3664. # case they are treated as a regular expression match. If all the
  3665. # specified component matches for a given list item succeed, the URL is
  3666. # blacklisted.
  3667. #
  3668. #url_preview_url_blacklist:
  3669. # # blacklist any URL with a username in its URI
  3670. # - username: '*'
  3671. #
  3672. # # blacklist all *.google.com URLs
  3673. # - netloc: 'google.com'
  3674. # - netloc: '*.google.com'
  3675. #
  3676. # # blacklist all plain HTTP URLs
  3677. # - scheme: 'http'
  3678. #
  3679. # # blacklist http(s)://www.acme.com/foo
  3680. # - netloc: 'www.acme.com'
  3681. # path: '/foo'
  3682. #
  3683. # # blacklist any URL with a literal IPv4 address
  3684. # - netloc: '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$'
  3685. # The largest allowed URL preview spidering size in bytes
  3686. #
  3687. #max_spider_size: 10M
  3688. # A list of values for the Accept-Language HTTP header used when
  3689. # downloading webpages during URL preview generation. This allows
  3690. # Synapse to specify the preferred languages that URL previews should
  3691. # be in when communicating with remote servers.
  3692. #
  3693. # Each value is a IETF language tag; a 2-3 letter identifier for a
  3694. # language, optionally followed by subtags separated by '-', specifying
  3695. # a country or region variant.
  3696. #
  3697. # Multiple values can be provided, and a weight can be added to each by
  3698. # using quality value syntax (;q=). '*' translates to any language.
  3699. #
  3700. # Defaults to &quot;en&quot;.
  3701. #
  3702. # Example:
  3703. #
  3704. # url_preview_accept_language:
  3705. # - en-UK
  3706. # - en-US;q=0.9
  3707. # - fr;q=0.8
  3708. # - *;q=0.7
  3709. #
  3710. url_preview_accept_language:
  3711. # - en
  3712. ## Captcha ##
  3713. # See docs/CAPTCHA_SETUP.md for full details of configuring this.
  3714. # This homeserver's ReCAPTCHA public key. Must be specified if
  3715. # enable_registration_captcha is enabled.
  3716. #
  3717. #recaptcha_public_key: &quot;YOUR_PUBLIC_KEY&quot;
  3718. # This homeserver's ReCAPTCHA private key. Must be specified if
  3719. # enable_registration_captcha is enabled.
  3720. #
  3721. #recaptcha_private_key: &quot;YOUR_PRIVATE_KEY&quot;
  3722. # Uncomment to enable ReCaptcha checks when registering, preventing signup
  3723. # unless a captcha is answered. Requires a valid ReCaptcha
  3724. # public/private key. Defaults to 'false'.
  3725. #
  3726. #enable_registration_captcha: true
  3727. # The API endpoint to use for verifying m.login.recaptcha responses.
  3728. # Defaults to &quot;https://www.recaptcha.net/recaptcha/api/siteverify&quot;.
  3729. #
  3730. #recaptcha_siteverify_api: &quot;https://my.recaptcha.site&quot;
  3731. ## TURN ##
  3732. # The public URIs of the TURN server to give to clients
  3733. #
  3734. #turn_uris: []
  3735. # The shared secret used to compute passwords for the TURN server
  3736. #
  3737. #turn_shared_secret: &quot;YOUR_SHARED_SECRET&quot;
  3738. # The Username and password if the TURN server needs them and
  3739. # does not use a token
  3740. #
  3741. #turn_username: &quot;TURNSERVER_USERNAME&quot;
  3742. #turn_password: &quot;TURNSERVER_PASSWORD&quot;
  3743. # How long generated TURN credentials last
  3744. #
  3745. #turn_user_lifetime: 1h
  3746. # Whether guests should be allowed to use the TURN server.
  3747. # This defaults to True, otherwise VoIP will be unreliable for guests.
  3748. # However, it does introduce a slight security risk as it allows users to
  3749. # connect to arbitrary endpoints without having first signed up for a
  3750. # valid account (e.g. by passing a CAPTCHA).
  3751. #
  3752. #turn_allow_guests: true
  3753. ## Registration ##
  3754. #
  3755. # Registration can be rate-limited using the parameters in the &quot;Ratelimiting&quot;
  3756. # section of this file.
  3757. # Enable registration for new users.
  3758. #
  3759. #enable_registration: false
  3760. # Time that a user's session remains valid for, after they log in.
  3761. #
  3762. # Note that this is not currently compatible with guest logins.
  3763. #
  3764. # Note also that this is calculated at login time: changes are not applied
  3765. # retrospectively to users who have already logged in.
  3766. #
  3767. # By default, this is infinite.
  3768. #
  3769. #session_lifetime: 24h
  3770. # The user must provide all of the below types of 3PID when registering.
  3771. #
  3772. #registrations_require_3pid:
  3773. # - email
  3774. # - msisdn
  3775. # Explicitly disable asking for MSISDNs from the registration
  3776. # flow (overrides registrations_require_3pid if MSISDNs are set as required)
  3777. #
  3778. #disable_msisdn_registration: true
  3779. # Mandate that users are only allowed to associate certain formats of
  3780. # 3PIDs with accounts on this server.
  3781. #
  3782. #allowed_local_3pids:
  3783. # - medium: email
  3784. # pattern: '^[^@]+@matrix\.org$'
  3785. # - medium: email
  3786. # pattern: '^[^@]+@vector\.im$'
  3787. # - medium: msisdn
  3788. # pattern: '\+44'
  3789. # Enable 3PIDs lookup requests to identity servers from this server.
  3790. #
  3791. #enable_3pid_lookup: true
  3792. # If set, allows registration of standard or admin accounts by anyone who
  3793. # has the shared secret, even if registration is otherwise disabled.
  3794. #
  3795. #registration_shared_secret: &lt;PRIVATE STRING&gt;
  3796. # Set the number of bcrypt rounds used to generate password hash.
  3797. # Larger numbers increase the work factor needed to generate the hash.
  3798. # The default number is 12 (which equates to 2^12 rounds).
  3799. # N.B. that increasing this will exponentially increase the time required
  3800. # to register or login - e.g. 24 =&gt; 2^24 rounds which will take &gt;20 mins.
  3801. #
  3802. #bcrypt_rounds: 12
  3803. # Allows users to register as guests without a password/email/etc, and
  3804. # participate in rooms hosted on this server which have been made
  3805. # accessible to anonymous users.
  3806. #
  3807. #allow_guest_access: false
  3808. # The identity server which we suggest that clients should use when users log
  3809. # in on this server.
  3810. #
  3811. # (By default, no suggestion is made, so it is left up to the client.
  3812. # This setting is ignored unless public_baseurl is also set.)
  3813. #
  3814. #default_identity_server: https://matrix.org
  3815. # Handle threepid (email/phone etc) registration and password resets through a set of
  3816. # *trusted* identity servers. Note that this allows the configured identity server to
  3817. # reset passwords for accounts!
  3818. #
  3819. # Be aware that if `email` is not set, and SMTP options have not been
  3820. # configured in the email config block, registration and user password resets via
  3821. # email will be globally disabled.
  3822. #
  3823. # Additionally, if `msisdn` is not set, registration and password resets via msisdn
  3824. # will be disabled regardless, and users will not be able to associate an msisdn
  3825. # identifier to their account. This is due to Synapse currently not supporting
  3826. # any method of sending SMS messages on its own.
  3827. #
  3828. # To enable using an identity server for operations regarding a particular third-party
  3829. # identifier type, set the value to the URL of that identity server as shown in the
  3830. # examples below.
  3831. #
  3832. # Servers handling the these requests must answer the `/requestToken` endpoints defined
  3833. # by the Matrix Identity Service API specification:
  3834. # https://matrix.org/docs/spec/identity_service/latest
  3835. #
  3836. # If a delegate is specified, the config option public_baseurl must also be filled out.
  3837. #
  3838. account_threepid_delegates:
  3839. #email: https://example.com # Delegate email sending to example.com
  3840. #msisdn: http://localhost:8090 # Delegate SMS sending to this local process
  3841. # Whether users are allowed to change their displayname after it has
  3842. # been initially set. Useful when provisioning users based on the
  3843. # contents of a third-party directory.
  3844. #
  3845. # Does not apply to server administrators. Defaults to 'true'
  3846. #
  3847. #enable_set_displayname: false
  3848. # Whether users are allowed to change their avatar after it has been
  3849. # initially set. Useful when provisioning users based on the contents
  3850. # of a third-party directory.
  3851. #
  3852. # Does not apply to server administrators. Defaults to 'true'
  3853. #
  3854. #enable_set_avatar_url: false
  3855. # Whether users can change the 3PIDs associated with their accounts
  3856. # (email address and msisdn).
  3857. #
  3858. # Defaults to 'true'
  3859. #
  3860. #enable_3pid_changes: false
  3861. # Users who register on this homeserver will automatically be joined
  3862. # to these rooms.
  3863. #
  3864. # By default, any room aliases included in this list will be created
  3865. # as a publicly joinable room when the first user registers for the
  3866. # homeserver. This behaviour can be customised with the settings below.
  3867. # If the room already exists, make certain it is a publicly joinable
  3868. # room. The join rule of the room must be set to 'public'.
  3869. #
  3870. #auto_join_rooms:
  3871. # - &quot;#example:example.com&quot;
  3872. # Where auto_join_rooms are specified, setting this flag ensures that the
  3873. # the rooms exist by creating them when the first user on the
  3874. # homeserver registers.
  3875. #
  3876. # By default the auto-created rooms are publicly joinable from any federated
  3877. # server. Use the autocreate_auto_join_rooms_federated and
  3878. # autocreate_auto_join_room_preset settings below to customise this behaviour.
  3879. #
  3880. # Setting to false means that if the rooms are not manually created,
  3881. # users cannot be auto-joined since they do not exist.
  3882. #
  3883. # Defaults to true. Uncomment the following line to disable automatically
  3884. # creating auto-join rooms.
  3885. #
  3886. #autocreate_auto_join_rooms: false
  3887. # Whether the auto_join_rooms that are auto-created are available via
  3888. # federation. Only has an effect if autocreate_auto_join_rooms is true.
  3889. #
  3890. # Note that whether a room is federated cannot be modified after
  3891. # creation.
  3892. #
  3893. # Defaults to true: the room will be joinable from other servers.
  3894. # Uncomment the following to prevent users from other homeservers from
  3895. # joining these rooms.
  3896. #
  3897. #autocreate_auto_join_rooms_federated: false
  3898. # The room preset to use when auto-creating one of auto_join_rooms. Only has an
  3899. # effect if autocreate_auto_join_rooms is true.
  3900. #
  3901. # This can be one of &quot;public_chat&quot;, &quot;private_chat&quot;, or &quot;trusted_private_chat&quot;.
  3902. # If a value of &quot;private_chat&quot; or &quot;trusted_private_chat&quot; is used then
  3903. # auto_join_mxid_localpart must also be configured.
  3904. #
  3905. # Defaults to &quot;public_chat&quot;, meaning that the room is joinable by anyone, including
  3906. # federated servers if autocreate_auto_join_rooms_federated is true (the default).
  3907. # Uncomment the following to require an invitation to join these rooms.
  3908. #
  3909. #autocreate_auto_join_room_preset: private_chat
  3910. # The local part of the user id which is used to create auto_join_rooms if
  3911. # autocreate_auto_join_rooms is true. If this is not provided then the
  3912. # initial user account that registers will be used to create the rooms.
  3913. #
  3914. # The user id is also used to invite new users to any auto-join rooms which
  3915. # are set to invite-only.
  3916. #
  3917. # It *must* be configured if autocreate_auto_join_room_preset is set to
  3918. # &quot;private_chat&quot; or &quot;trusted_private_chat&quot;.
  3919. #
  3920. # Note that this must be specified in order for new users to be correctly
  3921. # invited to any auto-join rooms which have been set to invite-only (either
  3922. # at the time of creation or subsequently).
  3923. #
  3924. # Note that, if the room already exists, this user must be joined and
  3925. # have the appropriate permissions to invite new members.
  3926. #
  3927. #auto_join_mxid_localpart: system
  3928. # When auto_join_rooms is specified, setting this flag to false prevents
  3929. # guest accounts from being automatically joined to the rooms.
  3930. #
  3931. # Defaults to true.
  3932. #
  3933. #auto_join_rooms_for_guests: false
  3934. ## Account Validity ##
  3935. # Optional account validity configuration. This allows for accounts to be denied
  3936. # any request after a given period.
  3937. #
  3938. # Once this feature is enabled, Synapse will look for registered users without an
  3939. # expiration date at startup and will add one to every account it found using the
  3940. # current settings at that time.
  3941. # This means that, if a validity period is set, and Synapse is restarted (it will
  3942. # then derive an expiration date from the current validity period), and some time
  3943. # after that the validity period changes and Synapse is restarted, the users'
  3944. # expiration dates won't be updated unless their account is manually renewed. This
  3945. # date will be randomly selected within a range [now + period - d ; now + period],
  3946. # where d is equal to 10% of the validity period.
  3947. #
  3948. account_validity:
  3949. # The account validity feature is disabled by default. Uncomment the
  3950. # following line to enable it.
  3951. #
  3952. #enabled: true
  3953. # The period after which an account is valid after its registration. When
  3954. # renewing the account, its validity period will be extended by this amount
  3955. # of time. This parameter is required when using the account validity
  3956. # feature.
  3957. #
  3958. #period: 6w
  3959. # The amount of time before an account's expiry date at which Synapse will
  3960. # send an email to the account's email address with a renewal link. By
  3961. # default, no such emails are sent.
  3962. #
  3963. # If you enable this setting, you will also need to fill out the 'email' and
  3964. # 'public_baseurl' configuration sections.
  3965. #
  3966. #renew_at: 1w
  3967. # The subject of the email sent out with the renewal link. '%(app)s' can be
  3968. # used as a placeholder for the 'app_name' parameter from the 'email'
  3969. # section.
  3970. #
  3971. # Note that the placeholder must be written '%(app)s', including the
  3972. # trailing 's'.
  3973. #
  3974. # If this is not set, a default value is used.
  3975. #
  3976. #renew_email_subject: &quot;Renew your %(app)s account&quot;
  3977. # Directory in which Synapse will try to find templates for the HTML files to
  3978. # serve to the user when trying to renew an account. If not set, default
  3979. # templates from within the Synapse package will be used.
  3980. #
  3981. # The currently available templates are:
  3982. #
  3983. # * account_renewed.html: Displayed to the user after they have successfully
  3984. # renewed their account.
  3985. #
  3986. # * account_previously_renewed.html: Displayed to the user if they attempt to
  3987. # renew their account with a token that is valid, but that has already
  3988. # been used. In this case the account is not renewed again.
  3989. #
  3990. # * invalid_token.html: Displayed to the user when they try to renew an account
  3991. # with an unknown or invalid renewal token.
  3992. #
  3993. # See https://github.com/matrix-org/synapse/tree/master/synapse/res/templates for
  3994. # default template contents.
  3995. #
  3996. # The file name of some of these templates can be configured below for legacy
  3997. # reasons.
  3998. #
  3999. #template_dir: &quot;res/templates&quot;
  4000. # A custom file name for the 'account_renewed.html' template.
  4001. #
  4002. # If not set, the file is assumed to be named &quot;account_renewed.html&quot;.
  4003. #
  4004. #account_renewed_html_path: &quot;account_renewed.html&quot;
  4005. # A custom file name for the 'invalid_token.html' template.
  4006. #
  4007. # If not set, the file is assumed to be named &quot;invalid_token.html&quot;.
  4008. #
  4009. #invalid_token_html_path: &quot;invalid_token.html&quot;
  4010. ## Metrics ###
  4011. # Enable collection and rendering of performance metrics
  4012. #
  4013. #enable_metrics: false
  4014. # Enable sentry integration
  4015. # NOTE: While attempts are made to ensure that the logs don't contain
  4016. # any sensitive information, this cannot be guaranteed. By enabling
  4017. # this option the sentry server may therefore receive sensitive
  4018. # information, and it in turn may then diseminate sensitive information
  4019. # through insecure notification channels if so configured.
  4020. #
  4021. #sentry:
  4022. # dsn: &quot;...&quot;
  4023. # Flags to enable Prometheus metrics which are not suitable to be
  4024. # enabled by default, either for performance reasons or limited use.
  4025. #
  4026. metrics_flags:
  4027. # Publish synapse_federation_known_servers, a gauge of the number of
  4028. # servers this homeserver knows about, including itself. May cause
  4029. # performance problems on large homeservers.
  4030. #
  4031. #known_servers: true
  4032. # Whether or not to report anonymized homeserver usage statistics.
  4033. #
  4034. #report_stats: true|false
  4035. # The endpoint to report the anonymized homeserver usage statistics to.
  4036. # Defaults to https://matrix.org/report-usage-stats/push
  4037. #
  4038. #report_stats_endpoint: https://example.com/report-usage-stats/push
  4039. ## API Configuration ##
  4040. # Controls for the state that is shared with users who receive an invite
  4041. # to a room
  4042. #
  4043. room_prejoin_state:
  4044. # By default, the following state event types are shared with users who
  4045. # receive invites to the room:
  4046. #
  4047. # - m.room.join_rules
  4048. # - m.room.canonical_alias
  4049. # - m.room.avatar
  4050. # - m.room.encryption
  4051. # - m.room.name
  4052. # - m.room.create
  4053. #
  4054. # Uncomment the following to disable these defaults (so that only the event
  4055. # types listed in 'additional_event_types' are shared). Defaults to 'false'.
  4056. #
  4057. #disable_default_event_types: true
  4058. # Additional state event types to share with users when they are invited
  4059. # to a room.
  4060. #
  4061. # By default, this list is empty (so only the default event types are shared).
  4062. #
  4063. #additional_event_types:
  4064. # - org.example.custom.event.type
  4065. # A list of application service config files to use
  4066. #
  4067. #app_service_config_files:
  4068. # - app_service_1.yaml
  4069. # - app_service_2.yaml
  4070. # Uncomment to enable tracking of application service IP addresses. Implicitly
  4071. # enables MAU tracking for application service users.
  4072. #
  4073. #track_appservice_user_ips: true
  4074. # a secret which is used to sign access tokens. If none is specified,
  4075. # the registration_shared_secret is used, if one is given; otherwise,
  4076. # a secret key is derived from the signing key.
  4077. #
  4078. #macaroon_secret_key: &lt;PRIVATE STRING&gt;
  4079. # a secret which is used to calculate HMACs for form values, to stop
  4080. # falsification of values. Must be specified for the User Consent
  4081. # forms to work.
  4082. #
  4083. #form_secret: &lt;PRIVATE STRING&gt;
  4084. ## Signing Keys ##
  4085. # Path to the signing key to sign messages with
  4086. #
  4087. signing_key_path: &quot;CONFDIR/SERVERNAME.signing.key&quot;
  4088. # The keys that the server used to sign messages with but won't use
  4089. # to sign new messages.
  4090. #
  4091. old_signing_keys:
  4092. # For each key, `key` should be the base64-encoded public key, and
  4093. # `expired_ts`should be the time (in milliseconds since the unix epoch) that
  4094. # it was last used.
  4095. #
  4096. # It is possible to build an entry from an old signing.key file using the
  4097. # `export_signing_key` script which is provided with synapse.
  4098. #
  4099. # For example:
  4100. #
  4101. #&quot;ed25519:id&quot;: { key: &quot;base64string&quot;, expired_ts: 123456789123 }
  4102. # How long key response published by this server is valid for.
  4103. # Used to set the valid_until_ts in /key/v2 APIs.
  4104. # Determines how quickly servers will query to check which keys
  4105. # are still valid.
  4106. #
  4107. #key_refresh_interval: 1d
  4108. # The trusted servers to download signing keys from.
  4109. #
  4110. # When we need to fetch a signing key, each server is tried in parallel.
  4111. #
  4112. # Normally, the connection to the key server is validated via TLS certificates.
  4113. # Additional security can be provided by configuring a `verify key`, which
  4114. # will make synapse check that the response is signed by that key.
  4115. #
  4116. # This setting supercedes an older setting named `perspectives`. The old format
  4117. # is still supported for backwards-compatibility, but it is deprecated.
  4118. #
  4119. # 'trusted_key_servers' defaults to matrix.org, but using it will generate a
  4120. # warning on start-up. To suppress this warning, set
  4121. # 'suppress_key_server_warning' to true.
  4122. #
  4123. # Options for each entry in the list include:
  4124. #
  4125. # server_name: the name of the server. required.
  4126. #
  4127. # verify_keys: an optional map from key id to base64-encoded public key.
  4128. # If specified, we will check that the response is signed by at least
  4129. # one of the given keys.
  4130. #
  4131. # accept_keys_insecurely: a boolean. Normally, if `verify_keys` is unset,
  4132. # and federation_verify_certificates is not `true`, synapse will refuse
  4133. # to start, because this would allow anyone who can spoof DNS responses
  4134. # to masquerade as the trusted key server. If you know what you are doing
  4135. # and are sure that your network environment provides a secure connection
  4136. # to the key server, you can set this to `true` to override this
  4137. # behaviour.
  4138. #
  4139. # An example configuration might look like:
  4140. #
  4141. #trusted_key_servers:
  4142. # - server_name: &quot;my_trusted_server.example.com&quot;
  4143. # verify_keys:
  4144. # &quot;ed25519:auto&quot;: &quot;abcdefghijklmnopqrstuvwxyzabcdefghijklmopqr&quot;
  4145. # - server_name: &quot;my_other_trusted_server.example.com&quot;
  4146. #
  4147. trusted_key_servers:
  4148. - server_name: &quot;matrix.org&quot;
  4149. # Uncomment the following to disable the warning that is emitted when the
  4150. # trusted_key_servers include 'matrix.org'. See above.
  4151. #
  4152. #suppress_key_server_warning: true
  4153. # The signing keys to use when acting as a trusted key server. If not specified
  4154. # defaults to the server signing key.
  4155. #
  4156. # Can contain multiple keys, one per line.
  4157. #
  4158. #key_server_signing_keys_path: &quot;key_server_signing_keys.key&quot;
  4159. ## Single sign-on integration ##
  4160. # The following settings can be used to make Synapse use a single sign-on
  4161. # provider for authentication, instead of its internal password database.
  4162. #
  4163. # You will probably also want to set the following options to `false` to
  4164. # disable the regular login/registration flows:
  4165. # * enable_registration
  4166. # * password_config.enabled
  4167. #
  4168. # You will also want to investigate the settings under the &quot;sso&quot; configuration
  4169. # section below.
  4170. # Enable SAML2 for registration and login. Uses pysaml2.
  4171. #
  4172. # At least one of `sp_config` or `config_path` must be set in this section to
  4173. # enable SAML login.
  4174. #
  4175. # Once SAML support is enabled, a metadata file will be exposed at
  4176. # https://&lt;server&gt;:&lt;port&gt;/_synapse/client/saml2/metadata.xml, which you may be able to
  4177. # use to configure your SAML IdP with. Alternatively, you can manually configure
  4178. # the IdP to use an ACS location of
  4179. # https://&lt;server&gt;:&lt;port&gt;/_synapse/client/saml2/authn_response.
  4180. #
  4181. saml2_config:
  4182. # `sp_config` is the configuration for the pysaml2 Service Provider.
  4183. # See pysaml2 docs for format of config.
  4184. #
  4185. # Default values will be used for the 'entityid' and 'service' settings,
  4186. # so it is not normally necessary to specify them unless you need to
  4187. # override them.
  4188. #
  4189. sp_config:
  4190. # Point this to the IdP's metadata. You must provide either a local
  4191. # file via the `local` attribute or (preferably) a URL via the
  4192. # `remote` attribute.
  4193. #
  4194. #metadata:
  4195. # local: [&quot;saml2/idp.xml&quot;]
  4196. # remote:
  4197. # - url: https://our_idp/metadata.xml
  4198. # Allowed clock difference in seconds between the homeserver and IdP.
  4199. #
  4200. # Uncomment the below to increase the accepted time difference from 0 to 3 seconds.
  4201. #
  4202. #accepted_time_diff: 3
  4203. # By default, the user has to go to our login page first. If you'd like
  4204. # to allow IdP-initiated login, set 'allow_unsolicited: true' in a
  4205. # 'service.sp' section:
  4206. #
  4207. #service:
  4208. # sp:
  4209. # allow_unsolicited: true
  4210. # The examples below are just used to generate our metadata xml, and you
  4211. # may well not need them, depending on your setup. Alternatively you
  4212. # may need a whole lot more detail - see the pysaml2 docs!
  4213. #description: [&quot;My awesome SP&quot;, &quot;en&quot;]
  4214. #name: [&quot;Test SP&quot;, &quot;en&quot;]
  4215. #ui_info:
  4216. # display_name:
  4217. # - lang: en
  4218. # text: &quot;Display Name is the descriptive name of your service.&quot;
  4219. # description:
  4220. # - lang: en
  4221. # text: &quot;Description should be a short paragraph explaining the purpose of the service.&quot;
  4222. # information_url:
  4223. # - lang: en
  4224. # text: &quot;https://example.com/terms-of-service&quot;
  4225. # privacy_statement_url:
  4226. # - lang: en
  4227. # text: &quot;https://example.com/privacy-policy&quot;
  4228. # keywords:
  4229. # - lang: en
  4230. # text: [&quot;Matrix&quot;, &quot;Element&quot;]
  4231. # logo:
  4232. # - lang: en
  4233. # text: &quot;https://example.com/logo.svg&quot;
  4234. # width: &quot;200&quot;
  4235. # height: &quot;80&quot;
  4236. #organization:
  4237. # name: Example com
  4238. # display_name:
  4239. # - [&quot;Example co&quot;, &quot;en&quot;]
  4240. # url: &quot;http://example.com&quot;
  4241. #contact_person:
  4242. # - given_name: Bob
  4243. # sur_name: &quot;the Sysadmin&quot;
  4244. # email_address&quot;: [&quot;admin@example.com&quot;]
  4245. # contact_type&quot;: technical
  4246. # Instead of putting the config inline as above, you can specify a
  4247. # separate pysaml2 configuration file:
  4248. #
  4249. #config_path: &quot;CONFDIR/sp_conf.py&quot;
  4250. # The lifetime of a SAML session. This defines how long a user has to
  4251. # complete the authentication process, if allow_unsolicited is unset.
  4252. # The default is 15 minutes.
  4253. #
  4254. #saml_session_lifetime: 5m
  4255. # An external module can be provided here as a custom solution to
  4256. # mapping attributes returned from a saml provider onto a matrix user.
  4257. #
  4258. user_mapping_provider:
  4259. # The custom module's class. Uncomment to use a custom module.
  4260. #
  4261. #module: mapping_provider.SamlMappingProvider
  4262. # Custom configuration values for the module. Below options are
  4263. # intended for the built-in provider, they should be changed if
  4264. # using a custom module. This section will be passed as a Python
  4265. # dictionary to the module's `parse_config` method.
  4266. #
  4267. config:
  4268. # The SAML attribute (after mapping via the attribute maps) to use
  4269. # to derive the Matrix ID from. 'uid' by default.
  4270. #
  4271. # Note: This used to be configured by the
  4272. # saml2_config.mxid_source_attribute option. If that is still
  4273. # defined, its value will be used instead.
  4274. #
  4275. #mxid_source_attribute: displayName
  4276. # The mapping system to use for mapping the saml attribute onto a
  4277. # matrix ID.
  4278. #
  4279. # Options include:
  4280. # * 'hexencode' (which maps unpermitted characters to '=xx')
  4281. # * 'dotreplace' (which replaces unpermitted characters with
  4282. # '.').
  4283. # The default is 'hexencode'.
  4284. #
  4285. # Note: This used to be configured by the
  4286. # saml2_config.mxid_mapping option. If that is still defined, its
  4287. # value will be used instead.
  4288. #
  4289. #mxid_mapping: dotreplace
  4290. # In previous versions of synapse, the mapping from SAML attribute to
  4291. # MXID was always calculated dynamically rather than stored in a
  4292. # table. For backwards- compatibility, we will look for user_ids
  4293. # matching such a pattern before creating a new account.
  4294. #
  4295. # This setting controls the SAML attribute which will be used for this
  4296. # backwards-compatibility lookup. Typically it should be 'uid', but if
  4297. # the attribute maps are changed, it may be necessary to change it.
  4298. #
  4299. # The default is 'uid'.
  4300. #
  4301. #grandfathered_mxid_source_attribute: upn
  4302. # It is possible to configure Synapse to only allow logins if SAML attributes
  4303. # match particular values. The requirements can be listed under
  4304. # `attribute_requirements` as shown below. All of the listed attributes must
  4305. # match for the login to be permitted.
  4306. #
  4307. #attribute_requirements:
  4308. # - attribute: userGroup
  4309. # value: &quot;staff&quot;
  4310. # - attribute: department
  4311. # value: &quot;sales&quot;
  4312. # If the metadata XML contains multiple IdP entities then the `idp_entityid`
  4313. # option must be set to the entity to redirect users to.
  4314. #
  4315. # Most deployments only have a single IdP entity and so should omit this
  4316. # option.
  4317. #
  4318. #idp_entityid: 'https://our_idp/entityid'
  4319. # List of OpenID Connect (OIDC) / OAuth 2.0 identity providers, for registration
  4320. # and login.
  4321. #
  4322. # Options for each entry include:
  4323. #
  4324. # idp_id: a unique identifier for this identity provider. Used internally
  4325. # by Synapse; should be a single word such as 'github'.
  4326. #
  4327. # Note that, if this is changed, users authenticating via that provider
  4328. # will no longer be recognised as the same user!
  4329. #
  4330. # (Use &quot;oidc&quot; here if you are migrating from an old &quot;oidc_config&quot;
  4331. # configuration.)
  4332. #
  4333. # idp_name: A user-facing name for this identity provider, which is used to
  4334. # offer the user a choice of login mechanisms.
  4335. #
  4336. # idp_icon: An optional icon for this identity provider, which is presented
  4337. # by clients and Synapse's own IdP picker page. If given, must be an
  4338. # MXC URI of the format mxc://&lt;server-name&gt;/&lt;media-id&gt;. (An easy way to
  4339. # obtain such an MXC URI is to upload an image to an (unencrypted) room
  4340. # and then copy the &quot;url&quot; from the source of the event.)
  4341. #
  4342. # idp_brand: An optional brand for this identity provider, allowing clients
  4343. # to style the login flow according to the identity provider in question.
  4344. # See the spec for possible options here.
  4345. #
  4346. # discover: set to 'false' to disable the use of the OIDC discovery mechanism
  4347. # to discover endpoints. Defaults to true.
  4348. #
  4349. # issuer: Required. The OIDC issuer. Used to validate tokens and (if discovery
  4350. # is enabled) to discover the provider's endpoints.
  4351. #
  4352. # client_id: Required. oauth2 client id to use.
  4353. #
  4354. # client_secret: oauth2 client secret to use. May be omitted if
  4355. # client_secret_jwt_key is given, or if client_auth_method is 'none'.
  4356. #
  4357. # client_secret_jwt_key: Alternative to client_secret: details of a key used
  4358. # to create a JSON Web Token to be used as an OAuth2 client secret. If
  4359. # given, must be a dictionary with the following properties:
  4360. #
  4361. # key: a pem-encoded signing key. Must be a suitable key for the
  4362. # algorithm specified. Required unless 'key_file' is given.
  4363. #
  4364. # key_file: the path to file containing a pem-encoded signing key file.
  4365. # Required unless 'key' is given.
  4366. #
  4367. # jwt_header: a dictionary giving properties to include in the JWT
  4368. # header. Must include the key 'alg', giving the algorithm used to
  4369. # sign the JWT, such as &quot;ES256&quot;, using the JWA identifiers in
  4370. # RFC7518.
  4371. #
  4372. # jwt_payload: an optional dictionary giving properties to include in
  4373. # the JWT payload. Normally this should include an 'iss' key.
  4374. #
  4375. # client_auth_method: auth method to use when exchanging the token. Valid
  4376. # values are 'client_secret_basic' (default), 'client_secret_post' and
  4377. # 'none'.
  4378. #
  4379. # scopes: list of scopes to request. This should normally include the &quot;openid&quot;
  4380. # scope. Defaults to [&quot;openid&quot;].
  4381. #
  4382. # authorization_endpoint: the oauth2 authorization endpoint. Required if
  4383. # provider discovery is disabled.
  4384. #
  4385. # token_endpoint: the oauth2 token endpoint. Required if provider discovery is
  4386. # disabled.
  4387. #
  4388. # userinfo_endpoint: the OIDC userinfo endpoint. Required if discovery is
  4389. # disabled and the 'openid' scope is not requested.
  4390. #
  4391. # jwks_uri: URI where to fetch the JWKS. Required if discovery is disabled and
  4392. # the 'openid' scope is used.
  4393. #
  4394. # skip_verification: set to 'true' to skip metadata verification. Use this if
  4395. # you are connecting to a provider that is not OpenID Connect compliant.
  4396. # Defaults to false. Avoid this in production.
  4397. #
  4398. # user_profile_method: Whether to fetch the user profile from the userinfo
  4399. # endpoint. Valid values are: 'auto' or 'userinfo_endpoint'.
  4400. #
  4401. # Defaults to 'auto', which fetches the userinfo endpoint if 'openid' is
  4402. # included in 'scopes'. Set to 'userinfo_endpoint' to always fetch the
  4403. # userinfo endpoint.
  4404. #
  4405. # allow_existing_users: set to 'true' to allow a user logging in via OIDC to
  4406. # match a pre-existing account instead of failing. This could be used if
  4407. # switching from password logins to OIDC. Defaults to false.
  4408. #
  4409. # user_mapping_provider: Configuration for how attributes returned from a OIDC
  4410. # provider are mapped onto a matrix user. This setting has the following
  4411. # sub-properties:
  4412. #
  4413. # module: The class name of a custom mapping module. Default is
  4414. # 'synapse.handlers.oidc.JinjaOidcMappingProvider'.
  4415. # See https://matrix-org.github.io/synapse/latest/sso_mapping_providers.html#openid-mapping-providers
  4416. # for information on implementing a custom mapping provider.
  4417. #
  4418. # config: Configuration for the mapping provider module. This section will
  4419. # be passed as a Python dictionary to the user mapping provider
  4420. # module's `parse_config` method.
  4421. #
  4422. # For the default provider, the following settings are available:
  4423. #
  4424. # subject_claim: name of the claim containing a unique identifier
  4425. # for the user. Defaults to 'sub', which OpenID Connect
  4426. # compliant providers should provide.
  4427. #
  4428. # localpart_template: Jinja2 template for the localpart of the MXID.
  4429. # If this is not set, the user will be prompted to choose their
  4430. # own username (see 'sso_auth_account_details.html' in the 'sso'
  4431. # section of this file).
  4432. #
  4433. # display_name_template: Jinja2 template for the display name to set
  4434. # on first login. If unset, no displayname will be set.
  4435. #
  4436. # email_template: Jinja2 template for the email address of the user.
  4437. # If unset, no email address will be added to the account.
  4438. #
  4439. # extra_attributes: a map of Jinja2 templates for extra attributes
  4440. # to send back to the client during login.
  4441. # Note that these are non-standard and clients will ignore them
  4442. # without modifications.
  4443. #
  4444. # When rendering, the Jinja2 templates are given a 'user' variable,
  4445. # which is set to the claims returned by the UserInfo Endpoint and/or
  4446. # in the ID Token.
  4447. #
  4448. # It is possible to configure Synapse to only allow logins if certain attributes
  4449. # match particular values in the OIDC userinfo. The requirements can be listed under
  4450. # `attribute_requirements` as shown below. All of the listed attributes must
  4451. # match for the login to be permitted. Additional attributes can be added to
  4452. # userinfo by expanding the `scopes` section of the OIDC config to retrieve
  4453. # additional information from the OIDC provider.
  4454. #
  4455. # If the OIDC claim is a list, then the attribute must match any value in the list.
  4456. # Otherwise, it must exactly match the value of the claim. Using the example
  4457. # below, the `family_name` claim MUST be &quot;Stephensson&quot;, but the `groups`
  4458. # claim MUST contain &quot;admin&quot;.
  4459. #
  4460. # attribute_requirements:
  4461. # - attribute: family_name
  4462. # value: &quot;Stephensson&quot;
  4463. # - attribute: groups
  4464. # value: &quot;admin&quot;
  4465. #
  4466. # See https://matrix-org.github.io/synapse/latest/openid.html
  4467. # for information on how to configure these options.
  4468. #
  4469. # For backwards compatibility, it is also possible to configure a single OIDC
  4470. # provider via an 'oidc_config' setting. This is now deprecated and admins are
  4471. # advised to migrate to the 'oidc_providers' format. (When doing that migration,
  4472. # use 'oidc' for the idp_id to ensure that existing users continue to be
  4473. # recognised.)
  4474. #
  4475. oidc_providers:
  4476. # Generic example
  4477. #
  4478. #- idp_id: my_idp
  4479. # idp_name: &quot;My OpenID provider&quot;
  4480. # idp_icon: &quot;mxc://example.com/mediaid&quot;
  4481. # discover: false
  4482. # issuer: &quot;https://accounts.example.com/&quot;
  4483. # client_id: &quot;provided-by-your-issuer&quot;
  4484. # client_secret: &quot;provided-by-your-issuer&quot;
  4485. # client_auth_method: client_secret_post
  4486. # scopes: [&quot;openid&quot;, &quot;profile&quot;]
  4487. # authorization_endpoint: &quot;https://accounts.example.com/oauth2/auth&quot;
  4488. # token_endpoint: &quot;https://accounts.example.com/oauth2/token&quot;
  4489. # userinfo_endpoint: &quot;https://accounts.example.com/userinfo&quot;
  4490. # jwks_uri: &quot;https://accounts.example.com/.well-known/jwks.json&quot;
  4491. # skip_verification: true
  4492. # user_mapping_provider:
  4493. # config:
  4494. # subject_claim: &quot;id&quot;
  4495. # localpart_template: &quot;{{ user.login }}&quot;
  4496. # display_name_template: &quot;{{ user.name }}&quot;
  4497. # email_template: &quot;{{ user.email }}&quot;
  4498. # attribute_requirements:
  4499. # - attribute: userGroup
  4500. # value: &quot;synapseUsers&quot;
  4501. # Enable Central Authentication Service (CAS) for registration and login.
  4502. #
  4503. cas_config:
  4504. # Uncomment the following to enable authorization against a CAS server.
  4505. # Defaults to false.
  4506. #
  4507. #enabled: true
  4508. # The URL of the CAS authorization endpoint.
  4509. #
  4510. #server_url: &quot;https://cas-server.com&quot;
  4511. # The attribute of the CAS response to use as the display name.
  4512. #
  4513. # If unset, no displayname will be set.
  4514. #
  4515. #displayname_attribute: name
  4516. # It is possible to configure Synapse to only allow logins if CAS attributes
  4517. # match particular values. All of the keys in the mapping below must exist
  4518. # and the values must match the given value. Alternately if the given value
  4519. # is None then any value is allowed (the attribute just must exist).
  4520. # All of the listed attributes must match for the login to be permitted.
  4521. #
  4522. #required_attributes:
  4523. # userGroup: &quot;staff&quot;
  4524. # department: None
  4525. # Additional settings to use with single-sign on systems such as OpenID Connect,
  4526. # SAML2 and CAS.
  4527. #
  4528. sso:
  4529. # A list of client URLs which are whitelisted so that the user does not
  4530. # have to confirm giving access to their account to the URL. Any client
  4531. # whose URL starts with an entry in the following list will not be subject
  4532. # to an additional confirmation step after the SSO login is completed.
  4533. #
  4534. # WARNING: An entry such as &quot;https://my.client&quot; is insecure, because it
  4535. # will also match &quot;https://my.client.evil.site&quot;, exposing your users to
  4536. # phishing attacks from evil.site. To avoid this, include a slash after the
  4537. # hostname: &quot;https://my.client/&quot;.
  4538. #
  4539. # If public_baseurl is set, then the login fallback page (used by clients
  4540. # that don't natively support the required login flows) is whitelisted in
  4541. # addition to any URLs in this list.
  4542. #
  4543. # By default, this list is empty.
  4544. #
  4545. #client_whitelist:
  4546. # - https://riot.im/develop
  4547. # - https://my.custom.client/
  4548. # Uncomment to keep a user's profile fields in sync with information from
  4549. # the identity provider. Currently only syncing the displayname is
  4550. # supported. Fields are checked on every SSO login, and are updated
  4551. # if necessary.
  4552. #
  4553. # Note that enabling this option will override user profile information,
  4554. # regardless of whether users have opted-out of syncing that
  4555. # information when first signing in. Defaults to false.
  4556. #
  4557. #update_profile_information: true
  4558. # Directory in which Synapse will try to find the template files below.
  4559. # If not set, or the files named below are not found within the template
  4560. # directory, default templates from within the Synapse package will be used.
  4561. #
  4562. # Synapse will look for the following templates in this directory:
  4563. #
  4564. # * HTML page to prompt the user to choose an Identity Provider during
  4565. # login: 'sso_login_idp_picker.html'.
  4566. #
  4567. # This is only used if multiple SSO Identity Providers are configured.
  4568. #
  4569. # When rendering, this template is given the following variables:
  4570. # * redirect_url: the URL that the user will be redirected to after
  4571. # login.
  4572. #
  4573. # * server_name: the homeserver's name.
  4574. #
  4575. # * providers: a list of available Identity Providers. Each element is
  4576. # an object with the following attributes:
  4577. #
  4578. # * idp_id: unique identifier for the IdP
  4579. # * idp_name: user-facing name for the IdP
  4580. # * idp_icon: if specified in the IdP config, an MXC URI for an icon
  4581. # for the IdP
  4582. # * idp_brand: if specified in the IdP config, a textual identifier
  4583. # for the brand of the IdP
  4584. #
  4585. # The rendered HTML page should contain a form which submits its results
  4586. # back as a GET request, with the following query parameters:
  4587. #
  4588. # * redirectUrl: the client redirect URI (ie, the `redirect_url` passed
  4589. # to the template)
  4590. #
  4591. # * idp: the 'idp_id' of the chosen IDP.
  4592. #
  4593. # * HTML page to prompt new users to enter a userid and confirm other
  4594. # details: 'sso_auth_account_details.html'. This is only shown if the
  4595. # SSO implementation (with any user_mapping_provider) does not return
  4596. # a localpart.
  4597. #
  4598. # When rendering, this template is given the following variables:
  4599. #
  4600. # * server_name: the homeserver's name.
  4601. #
  4602. # * idp: details of the SSO Identity Provider that the user logged in
  4603. # with: an object with the following attributes:
  4604. #
  4605. # * idp_id: unique identifier for the IdP
  4606. # * idp_name: user-facing name for the IdP
  4607. # * idp_icon: if specified in the IdP config, an MXC URI for an icon
  4608. # for the IdP
  4609. # * idp_brand: if specified in the IdP config, a textual identifier
  4610. # for the brand of the IdP
  4611. #
  4612. # * user_attributes: an object containing details about the user that
  4613. # we received from the IdP. May have the following attributes:
  4614. #
  4615. # * display_name: the user's display_name
  4616. # * emails: a list of email addresses
  4617. #
  4618. # The template should render a form which submits the following fields:
  4619. #
  4620. # * username: the localpart of the user's chosen user id
  4621. #
  4622. # * HTML page allowing the user to consent to the server's terms and
  4623. # conditions. This is only shown for new users, and only if
  4624. # `user_consent.require_at_registration` is set.
  4625. #
  4626. # When rendering, this template is given the following variables:
  4627. #
  4628. # * server_name: the homeserver's name.
  4629. #
  4630. # * user_id: the user's matrix proposed ID.
  4631. #
  4632. # * user_profile.display_name: the user's proposed display name, if any.
  4633. #
  4634. # * consent_version: the version of the terms that the user will be
  4635. # shown
  4636. #
  4637. # * terms_url: a link to the page showing the terms.
  4638. #
  4639. # The template should render a form which submits the following fields:
  4640. #
  4641. # * accepted_version: the version of the terms accepted by the user
  4642. # (ie, 'consent_version' from the input variables).
  4643. #
  4644. # * HTML page for a confirmation step before redirecting back to the client
  4645. # with the login token: 'sso_redirect_confirm.html'.
  4646. #
  4647. # When rendering, this template is given the following variables:
  4648. #
  4649. # * redirect_url: the URL the user is about to be redirected to.
  4650. #
  4651. # * display_url: the same as `redirect_url`, but with the query
  4652. # parameters stripped. The intention is to have a
  4653. # human-readable URL to show to users, not to use it as
  4654. # the final address to redirect to.
  4655. #
  4656. # * server_name: the homeserver's name.
  4657. #
  4658. # * new_user: a boolean indicating whether this is the user's first time
  4659. # logging in.
  4660. #
  4661. # * user_id: the user's matrix ID.
  4662. #
  4663. # * user_profile.avatar_url: an MXC URI for the user's avatar, if any.
  4664. # None if the user has not set an avatar.
  4665. #
  4666. # * user_profile.display_name: the user's display name. None if the user
  4667. # has not set a display name.
  4668. #
  4669. # * HTML page which notifies the user that they are authenticating to confirm
  4670. # an operation on their account during the user interactive authentication
  4671. # process: 'sso_auth_confirm.html'.
  4672. #
  4673. # When rendering, this template is given the following variables:
  4674. # * redirect_url: the URL the user is about to be redirected to.
  4675. #
  4676. # * description: the operation which the user is being asked to confirm
  4677. #
  4678. # * idp: details of the Identity Provider that we will use to confirm
  4679. # the user's identity: an object with the following attributes:
  4680. #
  4681. # * idp_id: unique identifier for the IdP
  4682. # * idp_name: user-facing name for the IdP
  4683. # * idp_icon: if specified in the IdP config, an MXC URI for an icon
  4684. # for the IdP
  4685. # * idp_brand: if specified in the IdP config, a textual identifier
  4686. # for the brand of the IdP
  4687. #
  4688. # * HTML page shown after a successful user interactive authentication session:
  4689. # 'sso_auth_success.html'.
  4690. #
  4691. # Note that this page must include the JavaScript which notifies of a successful authentication
  4692. # (see https://matrix.org/docs/spec/client_server/r0.6.0#fallback).
  4693. #
  4694. # This template has no additional variables.
  4695. #
  4696. # * HTML page shown after a user-interactive authentication session which
  4697. # does not map correctly onto the expected user: 'sso_auth_bad_user.html'.
  4698. #
  4699. # When rendering, this template is given the following variables:
  4700. # * server_name: the homeserver's name.
  4701. # * user_id_to_verify: the MXID of the user that we are trying to
  4702. # validate.
  4703. #
  4704. # * HTML page shown during single sign-on if a deactivated user (according to Synapse's database)
  4705. # attempts to login: 'sso_account_deactivated.html'.
  4706. #
  4707. # This template has no additional variables.
  4708. #
  4709. # * HTML page to display to users if something goes wrong during the
  4710. # OpenID Connect authentication process: 'sso_error.html'.
  4711. #
  4712. # When rendering, this template is given two variables:
  4713. # * error: the technical name of the error
  4714. # * error_description: a human-readable message for the error
  4715. #
  4716. # You can see the default templates at:
  4717. # https://github.com/matrix-org/synapse/tree/master/synapse/res/templates
  4718. #
  4719. #template_dir: &quot;res/templates&quot;
  4720. # JSON web token integration. The following settings can be used to make
  4721. # Synapse JSON web tokens for authentication, instead of its internal
  4722. # password database.
  4723. #
  4724. # Each JSON Web Token needs to contain a &quot;sub&quot; (subject) claim, which is
  4725. # used as the localpart of the mxid.
  4726. #
  4727. # Additionally, the expiration time (&quot;exp&quot;), not before time (&quot;nbf&quot;),
  4728. # and issued at (&quot;iat&quot;) claims are validated if present.
  4729. #
  4730. # Note that this is a non-standard login type and client support is
  4731. # expected to be non-existent.
  4732. #
  4733. # See https://matrix-org.github.io/synapse/latest/jwt.html.
  4734. #
  4735. #jwt_config:
  4736. # Uncomment the following to enable authorization using JSON web
  4737. # tokens. Defaults to false.
  4738. #
  4739. #enabled: true
  4740. # This is either the private shared secret or the public key used to
  4741. # decode the contents of the JSON web token.
  4742. #
  4743. # Required if 'enabled' is true.
  4744. #
  4745. #secret: &quot;provided-by-your-issuer&quot;
  4746. # The algorithm used to sign the JSON web token.
  4747. #
  4748. # Supported algorithms are listed at
  4749. # https://pyjwt.readthedocs.io/en/latest/algorithms.html
  4750. #
  4751. # Required if 'enabled' is true.
  4752. #
  4753. #algorithm: &quot;provided-by-your-issuer&quot;
  4754. # The issuer to validate the &quot;iss&quot; claim against.
  4755. #
  4756. # Optional, if provided the &quot;iss&quot; claim will be required and
  4757. # validated for all JSON web tokens.
  4758. #
  4759. #issuer: &quot;provided-by-your-issuer&quot;
  4760. # A list of audiences to validate the &quot;aud&quot; claim against.
  4761. #
  4762. # Optional, if provided the &quot;aud&quot; claim will be required and
  4763. # validated for all JSON web tokens.
  4764. #
  4765. # Note that if the &quot;aud&quot; claim is included in a JSON web token then
  4766. # validation will fail without configuring audiences.
  4767. #
  4768. #audiences:
  4769. # - &quot;provided-by-your-issuer&quot;
  4770. password_config:
  4771. # Uncomment to disable password login
  4772. #
  4773. #enabled: false
  4774. # Uncomment to disable authentication against the local password
  4775. # database. This is ignored if `enabled` is false, and is only useful
  4776. # if you have other password_providers.
  4777. #
  4778. #localdb_enabled: false
  4779. # Uncomment and change to a secret random string for extra security.
  4780. # DO NOT CHANGE THIS AFTER INITIAL SETUP!
  4781. #
  4782. #pepper: &quot;EVEN_MORE_SECRET&quot;
  4783. # Define and enforce a password policy. Each parameter is optional.
  4784. # This is an implementation of MSC2000.
  4785. #
  4786. policy:
  4787. # Whether to enforce the password policy.
  4788. # Defaults to 'false'.
  4789. #
  4790. #enabled: true
  4791. # Minimum accepted length for a password.
  4792. # Defaults to 0.
  4793. #
  4794. #minimum_length: 15
  4795. # Whether a password must contain at least one digit.
  4796. # Defaults to 'false'.
  4797. #
  4798. #require_digit: true
  4799. # Whether a password must contain at least one symbol.
  4800. # A symbol is any character that's not a number or a letter.
  4801. # Defaults to 'false'.
  4802. #
  4803. #require_symbol: true
  4804. # Whether a password must contain at least one lowercase letter.
  4805. # Defaults to 'false'.
  4806. #
  4807. #require_lowercase: true
  4808. # Whether a password must contain at least one lowercase letter.
  4809. # Defaults to 'false'.
  4810. #
  4811. #require_uppercase: true
  4812. ui_auth:
  4813. # The amount of time to allow a user-interactive authentication session
  4814. # to be active.
  4815. #
  4816. # This defaults to 0, meaning the user is queried for their credentials
  4817. # before every action, but this can be overridden to allow a single
  4818. # validation to be re-used. This weakens the protections afforded by
  4819. # the user-interactive authentication process, by allowing for multiple
  4820. # (and potentially different) operations to use the same validation session.
  4821. #
  4822. # This is ignored for potentially &quot;dangerous&quot; operations (including
  4823. # deactivating an account, modifying an account password, and
  4824. # adding a 3PID).
  4825. #
  4826. # Uncomment below to allow for credential validation to last for 15
  4827. # seconds.
  4828. #
  4829. #session_timeout: &quot;15s&quot;
  4830. # Configuration for sending emails from Synapse.
  4831. #
  4832. email:
  4833. # The hostname of the outgoing SMTP server to use. Defaults to 'localhost'.
  4834. #
  4835. #smtp_host: mail.server
  4836. # The port on the mail server for outgoing SMTP. Defaults to 25.
  4837. #
  4838. #smtp_port: 587
  4839. # Username/password for authentication to the SMTP server. By default, no
  4840. # authentication is attempted.
  4841. #
  4842. #smtp_user: &quot;exampleusername&quot;
  4843. #smtp_pass: &quot;examplepassword&quot;
  4844. # Uncomment the following to require TLS transport security for SMTP.
  4845. # By default, Synapse will connect over plain text, and will then switch to
  4846. # TLS via STARTTLS *if the SMTP server supports it*. If this option is set,
  4847. # Synapse will refuse to connect unless the server supports STARTTLS.
  4848. #
  4849. #require_transport_security: true
  4850. # notif_from defines the &quot;From&quot; address to use when sending emails.
  4851. # It must be set if email sending is enabled.
  4852. #
  4853. # The placeholder '%(app)s' will be replaced by the application name,
  4854. # which is normally 'app_name' (below), but may be overridden by the
  4855. # Matrix client application.
  4856. #
  4857. # Note that the placeholder must be written '%(app)s', including the
  4858. # trailing 's'.
  4859. #
  4860. #notif_from: &quot;Your Friendly %(app)s homeserver &lt;noreply@example.com&gt;&quot;
  4861. # app_name defines the default value for '%(app)s' in notif_from and email
  4862. # subjects. It defaults to 'Matrix'.
  4863. #
  4864. #app_name: my_branded_matrix_server
  4865. # Uncomment the following to enable sending emails for messages that the user
  4866. # has missed. Disabled by default.
  4867. #
  4868. #enable_notifs: true
  4869. # Uncomment the following to disable automatic subscription to email
  4870. # notifications for new users. Enabled by default.
  4871. #
  4872. #notif_for_new_users: false
  4873. # Custom URL for client links within the email notifications. By default
  4874. # links will be based on &quot;https://matrix.to&quot;.
  4875. #
  4876. # (This setting used to be called riot_base_url; the old name is still
  4877. # supported for backwards-compatibility but is now deprecated.)
  4878. #
  4879. #client_base_url: &quot;http://localhost/riot&quot;
  4880. # Configure the time that a validation email will expire after sending.
  4881. # Defaults to 1h.
  4882. #
  4883. #validation_token_lifetime: 15m
  4884. # The web client location to direct users to during an invite. This is passed
  4885. # to the identity server as the org.matrix.web_client_location key. Defaults
  4886. # to unset, giving no guidance to the identity server.
  4887. #
  4888. #invite_client_location: https://app.element.io
  4889. # Directory in which Synapse will try to find the template files below.
  4890. # If not set, or the files named below are not found within the template
  4891. # directory, default templates from within the Synapse package will be used.
  4892. #
  4893. # Synapse will look for the following templates in this directory:
  4894. #
  4895. # * The contents of email notifications of missed events: 'notif_mail.html' and
  4896. # 'notif_mail.txt'.
  4897. #
  4898. # * The contents of account expiry notice emails: 'notice_expiry.html' and
  4899. # 'notice_expiry.txt'.
  4900. #
  4901. # * The contents of password reset emails sent by the homeserver:
  4902. # 'password_reset.html' and 'password_reset.txt'
  4903. #
  4904. # * An HTML page that a user will see when they follow the link in the password
  4905. # reset email. The user will be asked to confirm the action before their
  4906. # password is reset: 'password_reset_confirmation.html'
  4907. #
  4908. # * HTML pages for success and failure that a user will see when they confirm
  4909. # the password reset flow using the page above: 'password_reset_success.html'
  4910. # and 'password_reset_failure.html'
  4911. #
  4912. # * The contents of address verification emails sent during registration:
  4913. # 'registration.html' and 'registration.txt'
  4914. #
  4915. # * HTML pages for success and failure that a user will see when they follow
  4916. # the link in an address verification email sent during registration:
  4917. # 'registration_success.html' and 'registration_failure.html'
  4918. #
  4919. # * The contents of address verification emails sent when an address is added
  4920. # to a Matrix account: 'add_threepid.html' and 'add_threepid.txt'
  4921. #
  4922. # * HTML pages for success and failure that a user will see when they follow
  4923. # the link in an address verification email sent when an address is added
  4924. # to a Matrix account: 'add_threepid_success.html' and
  4925. # 'add_threepid_failure.html'
  4926. #
  4927. # You can see the default templates at:
  4928. # https://github.com/matrix-org/synapse/tree/master/synapse/res/templates
  4929. #
  4930. #template_dir: &quot;res/templates&quot;
  4931. # Subjects to use when sending emails from Synapse.
  4932. #
  4933. # The placeholder '%(app)s' will be replaced with the value of the 'app_name'
  4934. # setting above, or by a value dictated by the Matrix client application.
  4935. #
  4936. # If a subject isn't overridden in this configuration file, the value used as
  4937. # its example will be used.
  4938. #
  4939. #subjects:
  4940. # Subjects for notification emails.
  4941. #
  4942. # On top of the '%(app)s' placeholder, these can use the following
  4943. # placeholders:
  4944. #
  4945. # * '%(person)s', which will be replaced by the display name of the user(s)
  4946. # that sent the message(s), e.g. &quot;Alice and Bob&quot;.
  4947. # * '%(room)s', which will be replaced by the name of the room the
  4948. # message(s) have been sent to, e.g. &quot;My super room&quot;.
  4949. #
  4950. # See the example provided for each setting to see which placeholder can be
  4951. # used and how to use them.
  4952. #
  4953. # Subject to use to notify about one message from one or more user(s) in a
  4954. # room which has a name.
  4955. #message_from_person_in_room: &quot;[%(app)s] You have a message on %(app)s from %(person)s in the %(room)s room...&quot;
  4956. #
  4957. # Subject to use to notify about one message from one or more user(s) in a
  4958. # room which doesn't have a name.
  4959. #message_from_person: &quot;[%(app)s] You have a message on %(app)s from %(person)s...&quot;
  4960. #
  4961. # Subject to use to notify about multiple messages from one or more users in
  4962. # a room which doesn't have a name.
  4963. #messages_from_person: &quot;[%(app)s] You have messages on %(app)s from %(person)s...&quot;
  4964. #
  4965. # Subject to use to notify about multiple messages in a room which has a
  4966. # name.
  4967. #messages_in_room: &quot;[%(app)s] You have messages on %(app)s in the %(room)s room...&quot;
  4968. #
  4969. # Subject to use to notify about multiple messages in multiple rooms.
  4970. #messages_in_room_and_others: &quot;[%(app)s] You have messages on %(app)s in the %(room)s room and others...&quot;
  4971. #
  4972. # Subject to use to notify about multiple messages from multiple persons in
  4973. # multiple rooms. This is similar to the setting above except it's used when
  4974. # the room in which the notification was triggered has no name.
  4975. #messages_from_person_and_others: &quot;[%(app)s] You have messages on %(app)s from %(person)s and others...&quot;
  4976. #
  4977. # Subject to use to notify about an invite to a room which has a name.
  4978. #invite_from_person_to_room: &quot;[%(app)s] %(person)s has invited you to join the %(room)s room on %(app)s...&quot;
  4979. #
  4980. # Subject to use to notify about an invite to a room which doesn't have a
  4981. # name.
  4982. #invite_from_person: &quot;[%(app)s] %(person)s has invited you to chat on %(app)s...&quot;
  4983. # Subject for emails related to account administration.
  4984. #
  4985. # On top of the '%(app)s' placeholder, these one can use the
  4986. # '%(server_name)s' placeholder, which will be replaced by the value of the
  4987. # 'server_name' setting in your Synapse configuration.
  4988. #
  4989. # Subject to use when sending a password reset email.
  4990. #password_reset: &quot;[%(server_name)s] Password reset&quot;
  4991. #
  4992. # Subject to use when sending a verification email to assert an address's
  4993. # ownership.
  4994. #email_validation: &quot;[%(server_name)s] Validate your email&quot;
  4995. # Password providers allow homeserver administrators to integrate
  4996. # their Synapse installation with existing authentication methods
  4997. # ex. LDAP, external tokens, etc.
  4998. #
  4999. # For more information and known implementations, please see
  5000. # https://matrix-org.github.io/synapse/latest/password_auth_providers.html
  5001. #
  5002. # Note: instances wishing to use SAML or CAS authentication should
  5003. # instead use the `saml2_config` or `cas_config` options,
  5004. # respectively.
  5005. #
  5006. password_providers:
  5007. # # Example config for an LDAP auth provider
  5008. # - module: &quot;ldap_auth_provider.LdapAuthProvider&quot;
  5009. # config:
  5010. # enabled: true
  5011. # uri: &quot;ldap://ldap.example.com:389&quot;
  5012. # start_tls: true
  5013. # base: &quot;ou=users,dc=example,dc=com&quot;
  5014. # attributes:
  5015. # uid: &quot;cn&quot;
  5016. # mail: &quot;email&quot;
  5017. # name: &quot;givenName&quot;
  5018. # #bind_dn:
  5019. # #bind_password:
  5020. # #filter: &quot;(objectClass=posixAccount)&quot;
  5021. ## Push ##
  5022. push:
  5023. # Clients requesting push notifications can either have the body of
  5024. # the message sent in the notification poke along with other details
  5025. # like the sender, or just the event ID and room ID (`event_id_only`).
  5026. # If clients choose the former, this option controls whether the
  5027. # notification request includes the content of the event (other details
  5028. # like the sender are still included). For `event_id_only` push, it
  5029. # has no effect.
  5030. #
  5031. # For modern android devices the notification content will still appear
  5032. # because it is loaded by the app. iPhone, however will send a
  5033. # notification saying only that a message arrived and who it came from.
  5034. #
  5035. # The default value is &quot;true&quot; to include message details. Uncomment to only
  5036. # include the event ID and room ID in push notification payloads.
  5037. #
  5038. #include_content: false
  5039. # When a push notification is received, an unread count is also sent.
  5040. # This number can either be calculated as the number of unread messages
  5041. # for the user, or the number of *rooms* the user has unread messages in.
  5042. #
  5043. # The default value is &quot;true&quot;, meaning push clients will see the number of
  5044. # rooms with unread messages in them. Uncomment to instead send the number
  5045. # of unread messages.
  5046. #
  5047. #group_unread_count_by_room: false
  5048. ## Rooms ##
  5049. # Controls whether locally-created rooms should be end-to-end encrypted by
  5050. # default.
  5051. #
  5052. # Possible options are &quot;all&quot;, &quot;invite&quot;, and &quot;off&quot;. They are defined as:
  5053. #
  5054. # * &quot;all&quot;: any locally-created room
  5055. # * &quot;invite&quot;: any room created with the &quot;private_chat&quot; or &quot;trusted_private_chat&quot;
  5056. # room creation presets
  5057. # * &quot;off&quot;: this option will take no effect
  5058. #
  5059. # The default value is &quot;off&quot;.
  5060. #
  5061. # Note that this option will only affect rooms created after it is set. It
  5062. # will also not affect rooms created by other servers.
  5063. #
  5064. #encryption_enabled_by_default_for_room_type: invite
  5065. # Uncomment to allow non-server-admin users to create groups on this server
  5066. #
  5067. #enable_group_creation: true
  5068. # If enabled, non server admins can only create groups with local parts
  5069. # starting with this prefix
  5070. #
  5071. #group_creation_prefix: &quot;unofficial_&quot;
  5072. # User Directory configuration
  5073. #
  5074. user_directory:
  5075. # Defines whether users can search the user directory. If false then
  5076. # empty responses are returned to all queries. Defaults to true.
  5077. #
  5078. # Uncomment to disable the user directory.
  5079. #
  5080. #enabled: false
  5081. # Defines whether to search all users visible to your HS when searching
  5082. # the user directory, rather than limiting to users visible in public
  5083. # rooms. Defaults to false.
  5084. #
  5085. # If you set it true, you'll have to rebuild the user_directory search
  5086. # indexes, see:
  5087. # https://matrix-org.github.io/synapse/latest/user_directory.html
  5088. #
  5089. # Uncomment to return search results containing all known users, even if that
  5090. # user does not share a room with the requester.
  5091. #
  5092. #search_all_users: true
  5093. # Defines whether to prefer local users in search query results.
  5094. # If True, local users are more likely to appear above remote users
  5095. # when searching the user directory. Defaults to false.
  5096. #
  5097. # Uncomment to prefer local over remote users in user directory search
  5098. # results.
  5099. #
  5100. #prefer_local_users: true
  5101. # User Consent configuration
  5102. #
  5103. # for detailed instructions, see
  5104. # https://matrix-org.github.io/synapse/latest/consent_tracking.html
  5105. #
  5106. # Parts of this section are required if enabling the 'consent' resource under
  5107. # 'listeners', in particular 'template_dir' and 'version'.
  5108. #
  5109. # 'template_dir' gives the location of the templates for the HTML forms.
  5110. # This directory should contain one subdirectory per language (eg, 'en', 'fr'),
  5111. # and each language directory should contain the policy document (named as
  5112. # '&lt;version&gt;.html') and a success page (success.html).
  5113. #
  5114. # 'version' specifies the 'current' version of the policy document. It defines
  5115. # the version to be served by the consent resource if there is no 'v'
  5116. # parameter.
  5117. #
  5118. # 'server_notice_content', if enabled, will send a user a &quot;Server Notice&quot;
  5119. # asking them to consent to the privacy policy. The 'server_notices' section
  5120. # must also be configured for this to work. Notices will *not* be sent to
  5121. # guest users unless 'send_server_notice_to_guests' is set to true.
  5122. #
  5123. # 'block_events_error', if set, will block any attempts to send events
  5124. # until the user consents to the privacy policy. The value of the setting is
  5125. # used as the text of the error.
  5126. #
  5127. # 'require_at_registration', if enabled, will add a step to the registration
  5128. # process, similar to how captcha works. Users will be required to accept the
  5129. # policy before their account is created.
  5130. #
  5131. # 'policy_name' is the display name of the policy users will see when registering
  5132. # for an account. Has no effect unless `require_at_registration` is enabled.
  5133. # Defaults to &quot;Privacy Policy&quot;.
  5134. #
  5135. #user_consent:
  5136. # template_dir: res/templates/privacy
  5137. # version: 1.0
  5138. # server_notice_content:
  5139. # msgtype: m.text
  5140. # body: &gt;-
  5141. # To continue using this homeserver you must review and agree to the
  5142. # terms and conditions at %(consent_uri)s
  5143. # send_server_notice_to_guests: true
  5144. # block_events_error: &gt;-
  5145. # To continue using this homeserver you must review and agree to the
  5146. # terms and conditions at %(consent_uri)s
  5147. # require_at_registration: false
  5148. # policy_name: Privacy Policy
  5149. #
  5150. # Settings for local room and user statistics collection. See
  5151. # https://matrix-org.github.io/synapse/latest/room_and_user_statistics.html.
  5152. #
  5153. stats:
  5154. # Uncomment the following to disable room and user statistics. Note that doing
  5155. # so may cause certain features (such as the room directory) not to work
  5156. # correctly.
  5157. #
  5158. #enabled: false
  5159. # The size of each timeslice in the room_stats_historical and
  5160. # user_stats_historical tables, as a time period. Defaults to &quot;1d&quot;.
  5161. #
  5162. #bucket_size: 1h
  5163. # Server Notices room configuration
  5164. #
  5165. # Uncomment this section to enable a room which can be used to send notices
  5166. # from the server to users. It is a special room which cannot be left; notices
  5167. # come from a special &quot;notices&quot; user id.
  5168. #
  5169. # If you uncomment this section, you *must* define the system_mxid_localpart
  5170. # setting, which defines the id of the user which will be used to send the
  5171. # notices.
  5172. #
  5173. # It's also possible to override the room name, the display name of the
  5174. # &quot;notices&quot; user, and the avatar for the user.
  5175. #
  5176. #server_notices:
  5177. # system_mxid_localpart: notices
  5178. # system_mxid_display_name: &quot;Server Notices&quot;
  5179. # system_mxid_avatar_url: &quot;mxc://server.com/oumMVlgDnLYFaPVkExemNVVZ&quot;
  5180. # room_name: &quot;Server Notices&quot;
  5181. # Uncomment to disable searching the public room list. When disabled
  5182. # blocks searching local and remote room lists for local and remote
  5183. # users by always returning an empty list for all queries.
  5184. #
  5185. #enable_room_list_search: false
  5186. # The `alias_creation` option controls who's allowed to create aliases
  5187. # on this server.
  5188. #
  5189. # The format of this option is a list of rules that contain globs that
  5190. # match against user_id, room_id and the new alias (fully qualified with
  5191. # server name). The action in the first rule that matches is taken,
  5192. # which can currently either be &quot;allow&quot; or &quot;deny&quot;.
  5193. #
  5194. # Missing user_id/room_id/alias fields default to &quot;*&quot;.
  5195. #
  5196. # If no rules match the request is denied. An empty list means no one
  5197. # can create aliases.
  5198. #
  5199. # Options for the rules include:
  5200. #
  5201. # user_id: Matches against the creator of the alias
  5202. # alias: Matches against the alias being created
  5203. # room_id: Matches against the room ID the alias is being pointed at
  5204. # action: Whether to &quot;allow&quot; or &quot;deny&quot; the request if the rule matches
  5205. #
  5206. # The default is:
  5207. #
  5208. #alias_creation_rules:
  5209. # - user_id: &quot;*&quot;
  5210. # alias: &quot;*&quot;
  5211. # room_id: &quot;*&quot;
  5212. # action: allow
  5213. # The `room_list_publication_rules` option controls who can publish and
  5214. # which rooms can be published in the public room list.
  5215. #
  5216. # The format of this option is the same as that for
  5217. # `alias_creation_rules`.
  5218. #
  5219. # If the room has one or more aliases associated with it, only one of
  5220. # the aliases needs to match the alias rule. If there are no aliases
  5221. # then only rules with `alias: *` match.
  5222. #
  5223. # If no rules match the request is denied. An empty list means no one
  5224. # can publish rooms.
  5225. #
  5226. # Options for the rules include:
  5227. #
  5228. # user_id: Matches against the creator of the alias
  5229. # room_id: Matches against the room ID being published
  5230. # alias: Matches against any current local or canonical aliases
  5231. # associated with the room
  5232. # action: Whether to &quot;allow&quot; or &quot;deny&quot; the request if the rule matches
  5233. #
  5234. # The default is:
  5235. #
  5236. #room_list_publication_rules:
  5237. # - user_id: &quot;*&quot;
  5238. # alias: &quot;*&quot;
  5239. # room_id: &quot;*&quot;
  5240. # action: allow
  5241. # Server admins can define a Python module that implements extra rules for
  5242. # allowing or denying incoming events. In order to work, this module needs to
  5243. # override the methods defined in synapse/events/third_party_rules.py.
  5244. #
  5245. # This feature is designed to be used in closed federations only, where each
  5246. # participating server enforces the same rules.
  5247. #
  5248. #third_party_event_rules:
  5249. # module: &quot;my_custom_project.SuperRulesSet&quot;
  5250. # config:
  5251. # example_option: 'things'
  5252. ## Opentracing ##
  5253. # These settings enable opentracing, which implements distributed tracing.
  5254. # This allows you to observe the causal chains of events across servers
  5255. # including requests, key lookups etc., across any server running
  5256. # synapse or any other other services which supports opentracing
  5257. # (specifically those implemented with Jaeger).
  5258. #
  5259. opentracing:
  5260. # tracing is disabled by default. Uncomment the following line to enable it.
  5261. #
  5262. #enabled: true
  5263. # The list of homeservers we wish to send and receive span contexts and span baggage.
  5264. # See https://matrix-org.github.io/synapse/latest/opentracing.html.
  5265. #
  5266. # This is a list of regexes which are matched against the server_name of the
  5267. # homeserver.
  5268. #
  5269. # By default, it is empty, so no servers are matched.
  5270. #
  5271. #homeserver_whitelist:
  5272. # - &quot;.*&quot;
  5273. # A list of the matrix IDs of users whose requests will always be traced,
  5274. # even if the tracing system would otherwise drop the traces due to
  5275. # probabilistic sampling.
  5276. #
  5277. # By default, the list is empty.
  5278. #
  5279. #force_tracing_for_users:
  5280. # - &quot;@user1:server_name&quot;
  5281. # - &quot;@user2:server_name&quot;
  5282. # Jaeger can be configured to sample traces at different rates.
  5283. # All configuration options provided by Jaeger can be set here.
  5284. # Jaeger's configuration is mostly related to trace sampling which
  5285. # is documented here:
  5286. # https://www.jaegertracing.io/docs/latest/sampling/.
  5287. #
  5288. #jaeger_config:
  5289. # sampler:
  5290. # type: const
  5291. # param: 1
  5292. # logging:
  5293. # false
  5294. ## Workers ##
  5295. # Disables sending of outbound federation transactions on the main process.
  5296. # Uncomment if using a federation sender worker.
  5297. #
  5298. #send_federation: false
  5299. # It is possible to run multiple federation sender workers, in which case the
  5300. # work is balanced across them.
  5301. #
  5302. # This configuration must be shared between all federation sender workers, and if
  5303. # changed all federation sender workers must be stopped at the same time and then
  5304. # started, to ensure that all instances are running with the same config (otherwise
  5305. # events may be dropped).
  5306. #
  5307. #federation_sender_instances:
  5308. # - federation_sender1
  5309. # When using workers this should be a map from `worker_name` to the
  5310. # HTTP replication listener of the worker, if configured.
  5311. #
  5312. #instance_map:
  5313. # worker1:
  5314. # host: localhost
  5315. # port: 8034
  5316. # Experimental: When using workers you can define which workers should
  5317. # handle event persistence and typing notifications. Any worker
  5318. # specified here must also be in the `instance_map`.
  5319. #
  5320. #stream_writers:
  5321. # events: worker1
  5322. # typing: worker1
  5323. # The worker that is used to run background tasks (e.g. cleaning up expired
  5324. # data). If not provided this defaults to the main process.
  5325. #
  5326. #run_background_tasks_on: worker1
  5327. # A shared secret used by the replication APIs to authenticate HTTP requests
  5328. # from workers.
  5329. #
  5330. # By default this is unused and traffic is not authenticated.
  5331. #
  5332. #worker_replication_secret: &quot;&quot;
  5333. # Configuration for Redis when using workers. This *must* be enabled when
  5334. # using workers (unless using old style direct TCP configuration).
  5335. #
  5336. redis:
  5337. # Uncomment the below to enable Redis support.
  5338. #
  5339. #enabled: true
  5340. # Optional host and port to use to connect to redis. Defaults to
  5341. # localhost and 6379
  5342. #
  5343. #host: localhost
  5344. #port: 6379
  5345. # Optional password if configured on the Redis instance
  5346. #
  5347. #password: &lt;secret_password&gt;
  5348. </code></pre>
  5349. <div style="break-before: page; page-break-before: always;"></div><h1 id="logging-sample-configuration-file"><a class="header" href="#logging-sample-configuration-file">Logging Sample Configuration File</a></h1>
  5350. <p>Below is a sample logging configuration file. This file can be tweaked to control how your
  5351. homeserver will output logs. A restart of the server is generally required to apply any
  5352. changes made to this file.</p>
  5353. <p>Note that the contents below are <em>not</em> intended to be copied and used as the basis for
  5354. a real homeserver.yaml. Instead, if you are starting from scratch, please generate
  5355. a fresh config using Synapse by following the instructions in
  5356. <a href="usage/configuration/../../setup/installation.html">Installation</a>.</p>
  5357. <pre><code class="language-yaml"># Log configuration for Synapse.
  5358. #
  5359. # This is a YAML file containing a standard Python logging configuration
  5360. # dictionary. See [1] for details on the valid settings.
  5361. #
  5362. # Synapse also supports structured logging for machine readable logs which can
  5363. # be ingested by ELK stacks. See [2] for details.
  5364. #
  5365. # [1]: https://docs.python.org/3.7/library/logging.config.html#configuration-dictionary-schema
  5366. # [2]: https://matrix-org.github.io/synapse/latest/structured_logging.html
  5367. version: 1
  5368. formatters:
  5369. precise:
  5370. format: '%(asctime)s - %(name)s - %(lineno)d - %(levelname)s - %(request)s - %(message)s'
  5371. handlers:
  5372. file:
  5373. class: logging.handlers.TimedRotatingFileHandler
  5374. formatter: precise
  5375. filename: /var/log/matrix-synapse/homeserver.log
  5376. when: midnight
  5377. backupCount: 3 # Does not include the current log file.
  5378. encoding: utf8
  5379. # Default to buffering writes to log file for efficiency. This means that
  5380. # will be a delay for INFO/DEBUG logs to get written, but WARNING/ERROR
  5381. # logs will still be flushed immediately.
  5382. buffer:
  5383. class: logging.handlers.MemoryHandler
  5384. target: file
  5385. # The capacity is the number of log lines that are buffered before
  5386. # being written to disk. Increasing this will lead to better
  5387. # performance, at the expensive of it taking longer for log lines to
  5388. # be written to disk.
  5389. capacity: 10
  5390. flushLevel: 30 # Flush for WARNING logs as well
  5391. # A handler that writes logs to stderr. Unused by default, but can be used
  5392. # instead of &quot;buffer&quot; and &quot;file&quot; in the logger handlers.
  5393. console:
  5394. class: logging.StreamHandler
  5395. formatter: precise
  5396. loggers:
  5397. synapse.storage.SQL:
  5398. # beware: increasing this to DEBUG will make synapse log sensitive
  5399. # information such as access tokens.
  5400. level: INFO
  5401. twisted:
  5402. # We send the twisted logging directly to the file handler,
  5403. # to work around https://github.com/matrix-org/synapse/issues/3471
  5404. # when using &quot;buffer&quot; logger. Use &quot;console&quot; to log to stderr instead.
  5405. handlers: [file]
  5406. propagate: false
  5407. root:
  5408. level: INFO
  5409. # Write logs to the `buffer` handler, which will buffer them together in memory,
  5410. # then write them to a file.
  5411. #
  5412. # Replace &quot;buffer&quot; with &quot;console&quot; to log to stderr instead. (Note that you'll
  5413. # also need to update the configuration for the `twisted` logger above, in
  5414. # this case.)
  5415. #
  5416. handlers: [buffer]
  5417. disable_existing_loggers: false
  5418. ``__`</code></pre>
  5419. <div style="break-before: page; page-break-before: always;"></div><h1 id="structured-logging"><a class="header" href="#structured-logging">Structured Logging</a></h1>
  5420. <p>A structured logging system can be useful when your logs are destined for a
  5421. machine to parse and process. By maintaining its machine-readable characteristics,
  5422. it enables more efficient searching and aggregations when consumed by software
  5423. such as the &quot;ELK stack&quot;.</p>
  5424. <p>Synapse's structured logging system is configured via the file that Synapse's
  5425. <code>log_config</code> config option points to. The file should include a formatter which
  5426. uses the <code>synapse.logging.TerseJsonFormatter</code> class included with Synapse and a
  5427. handler which uses the above formatter.</p>
  5428. <p>There is also a <code>synapse.logging.JsonFormatter</code> option which does not include
  5429. a timestamp in the resulting JSON. This is useful if the log ingester adds its
  5430. own timestamp.</p>
  5431. <p>A structured logging configuration looks similar to the following:</p>
  5432. <pre><code class="language-yaml">version: 1
  5433. formatters:
  5434. structured:
  5435. class: synapse.logging.TerseJsonFormatter
  5436. handlers:
  5437. file:
  5438. class: logging.handlers.TimedRotatingFileHandler
  5439. formatter: structured
  5440. filename: /path/to/my/logs/homeserver.log
  5441. when: midnight
  5442. backupCount: 3 # Does not include the current log file.
  5443. encoding: utf8
  5444. loggers:
  5445. synapse:
  5446. level: INFO
  5447. handlers: [remote]
  5448. synapse.storage.SQL:
  5449. level: WARNING
  5450. </code></pre>
  5451. <p>The above logging config will set Synapse as 'INFO' logging level by default,
  5452. with the SQL layer at 'WARNING', and will log to a file, stored as JSON.</p>
  5453. <p>It is also possible to figure Synapse to log to a remote endpoint by using the
  5454. <code>synapse.logging.RemoteHandler</code> class included with Synapse. It takes the
  5455. following arguments:</p>
  5456. <ul>
  5457. <li><code>host</code>: Hostname or IP address of the log aggregator.</li>
  5458. <li><code>port</code>: Numerical port to contact on the host.</li>
  5459. <li><code>maximum_buffer</code>: (Optional, defaults to 1000) The maximum buffer size to allow.</li>
  5460. </ul>
  5461. <p>A remote structured logging configuration looks similar to the following:</p>
  5462. <pre><code class="language-yaml">version: 1
  5463. formatters:
  5464. structured:
  5465. class: synapse.logging.TerseJsonFormatter
  5466. handlers:
  5467. remote:
  5468. class: synapse.logging.RemoteHandler
  5469. formatter: structured
  5470. host: 10.1.2.3
  5471. port: 9999
  5472. loggers:
  5473. synapse:
  5474. level: INFO
  5475. handlers: [remote]
  5476. synapse.storage.SQL:
  5477. level: WARNING
  5478. </code></pre>
  5479. <p>The above logging config will set Synapse as 'INFO' logging level by default,
  5480. with the SQL layer at 'WARNING', and will log JSON formatted messages to a
  5481. remote endpoint at 10.1.2.3:9999.</p>
  5482. <h2 id="upgrading-from-legacy-structured-logging-configuration"><a class="header" href="#upgrading-from-legacy-structured-logging-configuration">Upgrading from legacy structured logging configuration</a></h2>
  5483. <p>Versions of Synapse prior to v1.23.0 included a custom structured logging
  5484. configuration which is deprecated. It used a <code>structured: true</code> flag and
  5485. configured <code>drains</code> instead of <code>handlers</code> and <code>formatters</code>.</p>
  5486. <p>Synapse currently automatically converts the old configuration to the new
  5487. configuration, but this will be removed in a future version of Synapse. The
  5488. following reference can be used to update your configuration. Based on the drain
  5489. <code>type</code>, we can pick a new handler:</p>
  5490. <ol>
  5491. <li>For a type of <code>console</code>, <code>console_json</code>, or <code>console_json_terse</code>: a handler
  5492. with a class of <code>logging.StreamHandler</code> and a <code>stream</code> of <code>ext://sys.stdout</code>
  5493. or <code>ext://sys.stderr</code> should be used.</li>
  5494. <li>For a type of <code>file</code> or <code>file_json</code>: a handler of <code>logging.FileHandler</code> with
  5495. a location of the file path should be used.</li>
  5496. <li>For a type of <code>network_json_terse</code>: a handler of <code>synapse.logging.RemoteHandler</code>
  5497. with the host and port should be used.</li>
  5498. </ol>
  5499. <p>Then based on the drain <code>type</code> we can pick a new formatter:</p>
  5500. <ol>
  5501. <li>For a type of <code>console</code> or <code>file</code> no formatter is necessary.</li>
  5502. <li>For a type of <code>console_json</code> or <code>file_json</code>: a formatter of
  5503. <code>synapse.logging.JsonFormatter</code> should be used.</li>
  5504. <li>For a type of <code>console_json_terse</code> or <code>network_json_terse</code>: a formatter of
  5505. <code>synapse.logging.TerseJsonFormatter</code> should be used.</li>
  5506. </ol>
  5507. <p>For each new handler and formatter they should be added to the logging configuration
  5508. and then assigned to either a logger or the root logger.</p>
  5509. <p>An example legacy configuration:</p>
  5510. <pre><code class="language-yaml">structured: true
  5511. loggers:
  5512. synapse:
  5513. level: INFO
  5514. synapse.storage.SQL:
  5515. level: WARNING
  5516. drains:
  5517. console:
  5518. type: console
  5519. location: stdout
  5520. file:
  5521. type: file_json
  5522. location: homeserver.log
  5523. </code></pre>
  5524. <p>Would be converted into a new configuration:</p>
  5525. <pre><code class="language-yaml">version: 1
  5526. formatters:
  5527. json:
  5528. class: synapse.logging.JsonFormatter
  5529. handlers:
  5530. console:
  5531. class: logging.StreamHandler
  5532. location: ext://sys.stdout
  5533. file:
  5534. class: logging.FileHandler
  5535. formatter: json
  5536. filename: homeserver.log
  5537. loggers:
  5538. synapse:
  5539. level: INFO
  5540. handlers: [console, file]
  5541. synapse.storage.SQL:
  5542. level: WARNING
  5543. </code></pre>
  5544. <p>The new logging configuration is a bit more verbose, but significantly more
  5545. flexible. It allows for configuration that were not previously possible, such as
  5546. sending plain logs over the network, or using different handlers for different
  5547. modules.</p>
  5548. <div style="break-before: page; page-break-before: always;"></div><h1 id="user-authentication"><a class="header" href="#user-authentication">User Authentication</a></h1>
  5549. <p>Synapse supports multiple methods of authenticating users, either out-of-the-box or through custom pluggable
  5550. authentication modules.</p>
  5551. <p>Included in Synapse is support for authenticating users via:</p>
  5552. <ul>
  5553. <li>A username and password.</li>
  5554. <li>An email address and password.</li>
  5555. <li>Single Sign-On through the SAML, Open ID Connect or CAS protocols.</li>
  5556. <li>JSON Web Tokens.</li>
  5557. <li>An administrator's shared secret.</li>
  5558. </ul>
  5559. <p>Synapse can additionally be extended to support custom authentication schemes through optional &quot;password auth provider&quot;
  5560. modules.</p>
  5561. <div style="break-before: page; page-break-before: always;"></div><h1 id="configuring-synapse-to-authenticate-against-an-openid-connect-provider"><a class="header" href="#configuring-synapse-to-authenticate-against-an-openid-connect-provider">Configuring Synapse to authenticate against an OpenID Connect provider</a></h1>
  5562. <p>Synapse can be configured to use an OpenID Connect Provider (OP) for
  5563. authentication, instead of its own local password database.</p>
  5564. <p>Any OP should work with Synapse, as long as it supports the authorization code
  5565. flow. There are a few options for that:</p>
  5566. <ul>
  5567. <li>
  5568. <p>start a local OP. Synapse has been tested with <a href="https://www.ory.sh/docs/hydra/">Hydra</a> and
  5569. <a href="https://github.com/dexidp/dex">Dex</a>. Note that for an OP to work, it should be served under a
  5570. secure (HTTPS) origin. A certificate signed with a self-signed, locally
  5571. trusted CA should work. In that case, start Synapse with a <code>SSL_CERT_FILE</code>
  5572. environment variable set to the path of the CA.</p>
  5573. </li>
  5574. <li>
  5575. <p>set up a SaaS OP, like <a href="https://developers.google.com/identity/protocols/oauth2/openid-connect">Google</a>, <a href="https://auth0.com/">Auth0</a> or
  5576. <a href="https://www.okta.com/">Okta</a>. Synapse has been tested with Auth0 and Google.</p>
  5577. </li>
  5578. </ul>
  5579. <p>It may also be possible to use other OAuth2 providers which provide the
  5580. <a href="https://tools.ietf.org/html/rfc6749#section-4.1">authorization code grant type</a>,
  5581. such as <a href="https://developer.github.com/apps/building-oauth-apps/authorizing-oauth-apps">Github</a>.</p>
  5582. <h2 id="preparing-synapse"><a class="header" href="#preparing-synapse">Preparing Synapse</a></h2>
  5583. <p>The OpenID integration in Synapse uses the
  5584. <a href="https://pypi.org/project/Authlib/"><code>authlib</code></a> library, which must be installed
  5585. as follows:</p>
  5586. <ul>
  5587. <li>
  5588. <p>The relevant libraries are included in the Docker images and Debian packages
  5589. provided by <code>matrix.org</code> so no further action is needed.</p>
  5590. </li>
  5591. <li>
  5592. <p>If you installed Synapse into a virtualenv, run <code>/path/to/env/bin/pip install matrix-synapse[oidc]</code> to install the necessary dependencies.</p>
  5593. </li>
  5594. <li>
  5595. <p>For other installation mechanisms, see the documentation provided by the
  5596. maintainer.</p>
  5597. </li>
  5598. </ul>
  5599. <p>To enable the OpenID integration, you should then add a section to the <code>oidc_providers</code>
  5600. setting in your configuration file (or uncomment one of the existing examples).
  5601. See <a href="./sample_config.yaml">sample_config.yaml</a> for some sample settings, as well as
  5602. the text below for example configurations for specific providers.</p>
  5603. <h2 id="sample-configs"><a class="header" href="#sample-configs">Sample configs</a></h2>
  5604. <p>Here are a few configs for providers that should work with Synapse.</p>
  5605. <h3 id="microsoft-azure-active-directory"><a class="header" href="#microsoft-azure-active-directory">Microsoft Azure Active Directory</a></h3>
  5606. <p>Azure AD can act as an OpenID Connect Provider. Register a new application under
  5607. <em>App registrations</em> in the Azure AD management console. The RedirectURI for your
  5608. application should point to your matrix server:
  5609. <code>[synapse public baseurl]/_synapse/client/oidc/callback</code></p>
  5610. <p>Go to <em>Certificates &amp; secrets</em> and register a new client secret. Make note of your
  5611. Directory (tenant) ID as it will be used in the Azure links.
  5612. Edit your Synapse config file and change the <code>oidc_config</code> section:</p>
  5613. <pre><code class="language-yaml">oidc_providers:
  5614. - idp_id: microsoft
  5615. idp_name: Microsoft
  5616. issuer: &quot;https://login.microsoftonline.com/&lt;tenant id&gt;/v2.0&quot;
  5617. client_id: &quot;&lt;client id&gt;&quot;
  5618. client_secret: &quot;&lt;client secret&gt;&quot;
  5619. scopes: [&quot;openid&quot;, &quot;profile&quot;]
  5620. authorization_endpoint: &quot;https://login.microsoftonline.com/&lt;tenant id&gt;/oauth2/v2.0/authorize&quot;
  5621. token_endpoint: &quot;https://login.microsoftonline.com/&lt;tenant id&gt;/oauth2/v2.0/token&quot;
  5622. userinfo_endpoint: &quot;https://graph.microsoft.com/oidc/userinfo&quot;
  5623. user_mapping_provider:
  5624. config:
  5625. localpart_template: &quot;{{ user.preferred_username.split('@')[0] }}&quot;
  5626. display_name_template: &quot;{{ user.name }}&quot;
  5627. </code></pre>
  5628. <h3 id="dex"><a class="header" href="#dex"><a href="https://github.com/dexidp/dex">Dex</a></a></h3>
  5629. <p><a href="https://github.com/dexidp/dex">Dex</a> is a simple, open-source, certified OpenID Connect Provider.
  5630. Although it is designed to help building a full-blown provider with an
  5631. external database, it can be configured with static passwords in a config file.</p>
  5632. <p>Follow the <a href="https://dexidp.io/docs/getting-started/">Getting Started guide</a>
  5633. to install Dex.</p>
  5634. <p>Edit <code>examples/config-dev.yaml</code> config file from the Dex repo to add a client:</p>
  5635. <pre><code class="language-yaml">staticClients:
  5636. - id: synapse
  5637. secret: secret
  5638. redirectURIs:
  5639. - '[synapse public baseurl]/_synapse/client/oidc/callback'
  5640. name: 'Synapse'
  5641. </code></pre>
  5642. <p>Run with <code>dex serve examples/config-dev.yaml</code>.</p>
  5643. <p>Synapse config:</p>
  5644. <pre><code class="language-yaml">oidc_providers:
  5645. - idp_id: dex
  5646. idp_name: &quot;My Dex server&quot;
  5647. skip_verification: true # This is needed as Dex is served on an insecure endpoint
  5648. issuer: &quot;http://127.0.0.1:5556/dex&quot;
  5649. client_id: &quot;synapse&quot;
  5650. client_secret: &quot;secret&quot;
  5651. scopes: [&quot;openid&quot;, &quot;profile&quot;]
  5652. user_mapping_provider:
  5653. config:
  5654. localpart_template: &quot;{{ user.name }}&quot;
  5655. display_name_template: &quot;{{ user.name|capitalize }}&quot;
  5656. </code></pre>
  5657. <h3 id="keycloak"><a class="header" href="#keycloak"><a href="https://www.keycloak.org/docs/latest/server_admin/#sso-protocols">Keycloak</a></a></h3>
  5658. <p><a href="https://www.keycloak.org/docs/latest/server_admin/#sso-protocols">Keycloak</a> is an opensource IdP maintained by Red Hat.</p>
  5659. <p>Follow the <a href="https://www.keycloak.org/getting-started">Getting Started Guide</a> to install Keycloak and set up a realm.</p>
  5660. <ol>
  5661. <li>
  5662. <p>Click <code>Clients</code> in the sidebar and click <code>Create</code></p>
  5663. </li>
  5664. <li>
  5665. <p>Fill in the fields as below:</p>
  5666. </li>
  5667. </ol>
  5668. <table><thead><tr><th>Field</th><th>Value</th></tr></thead><tbody>
  5669. <tr><td>Client ID</td><td><code>synapse</code></td></tr>
  5670. <tr><td>Client Protocol</td><td><code>openid-connect</code></td></tr>
  5671. </tbody></table>
  5672. <ol start="3">
  5673. <li>Click <code>Save</code></li>
  5674. <li>Fill in the fields as below:</li>
  5675. </ol>
  5676. <table><thead><tr><th>Field</th><th>Value</th></tr></thead><tbody>
  5677. <tr><td>Client ID</td><td><code>synapse</code></td></tr>
  5678. <tr><td>Enabled</td><td><code>On</code></td></tr>
  5679. <tr><td>Client Protocol</td><td><code>openid-connect</code></td></tr>
  5680. <tr><td>Access Type</td><td><code>confidential</code></td></tr>
  5681. <tr><td>Valid Redirect URIs</td><td><code>[synapse public baseurl]/_synapse/client/oidc/callback</code></td></tr>
  5682. </tbody></table>
  5683. <ol start="5">
  5684. <li>Click <code>Save</code></li>
  5685. <li>On the Credentials tab, update the fields:</li>
  5686. </ol>
  5687. <table><thead><tr><th>Field</th><th>Value</th></tr></thead><tbody>
  5688. <tr><td>Client Authenticator</td><td><code>Client ID and Secret</code></td></tr>
  5689. </tbody></table>
  5690. <ol start="7">
  5691. <li>Click <code>Regenerate Secret</code></li>
  5692. <li>Copy Secret</li>
  5693. </ol>
  5694. <pre><code class="language-yaml">oidc_providers:
  5695. - idp_id: keycloak
  5696. idp_name: &quot;My KeyCloak server&quot;
  5697. issuer: &quot;https://127.0.0.1:8443/auth/realms/{realm_name}&quot;
  5698. client_id: &quot;synapse&quot;
  5699. client_secret: &quot;copy secret generated from above&quot;
  5700. scopes: [&quot;openid&quot;, &quot;profile&quot;]
  5701. user_mapping_provider:
  5702. config:
  5703. localpart_template: &quot;{{ user.preferred_username }}&quot;
  5704. display_name_template: &quot;{{ user.name }}&quot;
  5705. </code></pre>
  5706. <h3 id="auth0"><a class="header" href="#auth0"><a href="https://auth0.com/">Auth0</a></a></h3>
  5707. <ol>
  5708. <li>
  5709. <p>Create a regular web application for Synapse</p>
  5710. </li>
  5711. <li>
  5712. <p>Set the Allowed Callback URLs to <code>[synapse public baseurl]/_synapse/client/oidc/callback</code></p>
  5713. </li>
  5714. <li>
  5715. <p>Add a rule to add the <code>preferred_username</code> claim.</p>
  5716. <details>
  5717. <summary>Code sample</summary>
  5718. <pre><code class="language-js">function addPersistenceAttribute(user, context, callback) {
  5719. user.user_metadata = user.user_metadata || {};
  5720. user.user_metadata.preferred_username = user.user_metadata.preferred_username || user.user_id;
  5721. context.idToken.preferred_username = user.user_metadata.preferred_username;
  5722. auth0.users.updateUserMetadata(user.user_id, user.user_metadata)
  5723. .then(function(){
  5724. callback(null, user, context);
  5725. })
  5726. .catch(function(err){
  5727. callback(err);
  5728. });
  5729. }
  5730. </code></pre>
  5731. </li>
  5732. </ol>
  5733. </details>
  5734. <p>Synapse config:</p>
  5735. <pre><code class="language-yaml">oidc_providers:
  5736. - idp_id: auth0
  5737. idp_name: Auth0
  5738. issuer: &quot;https://your-tier.eu.auth0.com/&quot; # TO BE FILLED
  5739. client_id: &quot;your-client-id&quot; # TO BE FILLED
  5740. client_secret: &quot;your-client-secret&quot; # TO BE FILLED
  5741. scopes: [&quot;openid&quot;, &quot;profile&quot;]
  5742. user_mapping_provider:
  5743. config:
  5744. localpart_template: &quot;{{ user.preferred_username }}&quot;
  5745. display_name_template: &quot;{{ user.name }}&quot;
  5746. </code></pre>
  5747. <h3 id="github"><a class="header" href="#github">GitHub</a></h3>
  5748. <p>GitHub is a bit special as it is not an OpenID Connect compliant provider, but
  5749. just a regular OAuth2 provider.</p>
  5750. <p>The <a href="https://developer.github.com/v3/users/#get-the-authenticated-user"><code>/user</code> API endpoint</a>
  5751. can be used to retrieve information on the authenticated user. As the Synapse
  5752. login mechanism needs an attribute to uniquely identify users, and that endpoint
  5753. does not return a <code>sub</code> property, an alternative <code>subject_claim</code> has to be set.</p>
  5754. <ol>
  5755. <li>Create a new OAuth application: https://github.com/settings/applications/new.</li>
  5756. <li>Set the callback URL to <code>[synapse public baseurl]/_synapse/client/oidc/callback</code>.</li>
  5757. </ol>
  5758. <p>Synapse config:</p>
  5759. <pre><code class="language-yaml">oidc_providers:
  5760. - idp_id: github
  5761. idp_name: Github
  5762. idp_brand: &quot;github&quot; # optional: styling hint for clients
  5763. discover: false
  5764. issuer: &quot;https://github.com/&quot;
  5765. client_id: &quot;your-client-id&quot; # TO BE FILLED
  5766. client_secret: &quot;your-client-secret&quot; # TO BE FILLED
  5767. authorization_endpoint: &quot;https://github.com/login/oauth/authorize&quot;
  5768. token_endpoint: &quot;https://github.com/login/oauth/access_token&quot;
  5769. userinfo_endpoint: &quot;https://api.github.com/user&quot;
  5770. scopes: [&quot;read:user&quot;]
  5771. user_mapping_provider:
  5772. config:
  5773. subject_claim: &quot;id&quot;
  5774. localpart_template: &quot;{{ user.login }}&quot;
  5775. display_name_template: &quot;{{ user.name }}&quot;
  5776. </code></pre>
  5777. <h3 id="google"><a class="header" href="#google"><a href="https://developers.google.com/identity/protocols/oauth2/openid-connect">Google</a></a></h3>
  5778. <ol>
  5779. <li>Set up a project in the Google API Console (see
  5780. https://developers.google.com/identity/protocols/oauth2/openid-connect#appsetup).</li>
  5781. <li>add an &quot;OAuth Client ID&quot; for a Web Application under &quot;Credentials&quot;.</li>
  5782. <li>Copy the Client ID and Client Secret, and add the following to your synapse config:
  5783. <pre><code class="language-yaml">oidc_providers:
  5784. - idp_id: google
  5785. idp_name: Google
  5786. idp_brand: &quot;google&quot; # optional: styling hint for clients
  5787. issuer: &quot;https://accounts.google.com/&quot;
  5788. client_id: &quot;your-client-id&quot; # TO BE FILLED
  5789. client_secret: &quot;your-client-secret&quot; # TO BE FILLED
  5790. scopes: [&quot;openid&quot;, &quot;profile&quot;]
  5791. user_mapping_provider:
  5792. config:
  5793. localpart_template: &quot;{{ user.given_name|lower }}&quot;
  5794. display_name_template: &quot;{{ user.name }}&quot;
  5795. </code></pre>
  5796. </li>
  5797. <li>Back in the Google console, add this Authorized redirect URI: <code>[synapse public baseurl]/_synapse/client/oidc/callback</code>.</li>
  5798. </ol>
  5799. <h3 id="twitch"><a class="header" href="#twitch">Twitch</a></h3>
  5800. <ol>
  5801. <li>Setup a developer account on <a href="https://dev.twitch.tv/">Twitch</a></li>
  5802. <li>Obtain the OAuth 2.0 credentials by <a href="https://dev.twitch.tv/console/apps/">creating an app</a></li>
  5803. <li>Add this OAuth Redirect URL: <code>[synapse public baseurl]/_synapse/client/oidc/callback</code></li>
  5804. </ol>
  5805. <p>Synapse config:</p>
  5806. <pre><code class="language-yaml">oidc_providers:
  5807. - idp_id: twitch
  5808. idp_name: Twitch
  5809. issuer: &quot;https://id.twitch.tv/oauth2/&quot;
  5810. client_id: &quot;your-client-id&quot; # TO BE FILLED
  5811. client_secret: &quot;your-client-secret&quot; # TO BE FILLED
  5812. client_auth_method: &quot;client_secret_post&quot;
  5813. user_mapping_provider:
  5814. config:
  5815. localpart_template: &quot;{{ user.preferred_username }}&quot;
  5816. display_name_template: &quot;{{ user.name }}&quot;
  5817. </code></pre>
  5818. <h3 id="gitlab"><a class="header" href="#gitlab">GitLab</a></h3>
  5819. <ol>
  5820. <li>Create a <a href="https://gitlab.com/profile/applications">new application</a>.</li>
  5821. <li>Add the <code>read_user</code> and <code>openid</code> scopes.</li>
  5822. <li>Add this Callback URL: <code>[synapse public baseurl]/_synapse/client/oidc/callback</code></li>
  5823. </ol>
  5824. <p>Synapse config:</p>
  5825. <pre><code class="language-yaml">oidc_providers:
  5826. - idp_id: gitlab
  5827. idp_name: Gitlab
  5828. idp_brand: &quot;gitlab&quot; # optional: styling hint for clients
  5829. issuer: &quot;https://gitlab.com/&quot;
  5830. client_id: &quot;your-client-id&quot; # TO BE FILLED
  5831. client_secret: &quot;your-client-secret&quot; # TO BE FILLED
  5832. client_auth_method: &quot;client_secret_post&quot;
  5833. scopes: [&quot;openid&quot;, &quot;read_user&quot;]
  5834. user_profile_method: &quot;userinfo_endpoint&quot;
  5835. user_mapping_provider:
  5836. config:
  5837. localpart_template: '{{ user.nickname }}'
  5838. display_name_template: '{{ user.name }}'
  5839. </code></pre>
  5840. <h3 id="facebook"><a class="header" href="#facebook">Facebook</a></h3>
  5841. <p>Like Github, Facebook provide a custom OAuth2 API rather than an OIDC-compliant
  5842. one so requires a little more configuration.</p>
  5843. <ol start="0">
  5844. <li>You will need a Facebook developer account. You can register for one
  5845. <a href="https://developers.facebook.com/async/registration/">here</a>.</li>
  5846. <li>On the <a href="https://developers.facebook.com/apps/">apps</a> page of the developer
  5847. console, &quot;Create App&quot;, and choose &quot;Build Connected Experiences&quot;.</li>
  5848. <li>Once the app is created, add &quot;Facebook Login&quot; and choose &quot;Web&quot;. You don't
  5849. need to go through the whole form here.</li>
  5850. <li>In the left-hand menu, open &quot;Products&quot;/&quot;Facebook Login&quot;/&quot;Settings&quot;.
  5851. <ul>
  5852. <li>Add <code>[synapse public baseurl]/_synapse/client/oidc/callback</code> as an OAuth Redirect
  5853. URL.</li>
  5854. </ul>
  5855. </li>
  5856. <li>In the left-hand menu, open &quot;Settings/Basic&quot;. Here you can copy the &quot;App ID&quot;
  5857. and &quot;App Secret&quot; for use below.</li>
  5858. </ol>
  5859. <p>Synapse config:</p>
  5860. <pre><code class="language-yaml"> - idp_id: facebook
  5861. idp_name: Facebook
  5862. idp_brand: &quot;facebook&quot; # optional: styling hint for clients
  5863. discover: false
  5864. issuer: &quot;https://facebook.com&quot;
  5865. client_id: &quot;your-client-id&quot; # TO BE FILLED
  5866. client_secret: &quot;your-client-secret&quot; # TO BE FILLED
  5867. scopes: [&quot;openid&quot;, &quot;email&quot;]
  5868. authorization_endpoint: https://facebook.com/dialog/oauth
  5869. token_endpoint: https://graph.facebook.com/v9.0/oauth/access_token
  5870. user_profile_method: &quot;userinfo_endpoint&quot;
  5871. userinfo_endpoint: &quot;https://graph.facebook.com/v9.0/me?fields=id,name,email,picture&quot;
  5872. user_mapping_provider:
  5873. config:
  5874. subject_claim: &quot;id&quot;
  5875. display_name_template: &quot;{{ user.name }}&quot;
  5876. </code></pre>
  5877. <p>Relevant documents:</p>
  5878. <ul>
  5879. <li>https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow</li>
  5880. <li>Using Facebook's Graph API: https://developers.facebook.com/docs/graph-api/using-graph-api/</li>
  5881. <li>Reference to the User endpoint: https://developers.facebook.com/docs/graph-api/reference/user</li>
  5882. </ul>
  5883. <h3 id="gitea"><a class="header" href="#gitea">Gitea</a></h3>
  5884. <p>Gitea is, like Github, not an OpenID provider, but just an OAuth2 provider.</p>
  5885. <p>The <a href="https://try.gitea.io/api/swagger#/user/userGetCurrent"><code>/user</code> API endpoint</a>
  5886. can be used to retrieve information on the authenticated user. As the Synapse
  5887. login mechanism needs an attribute to uniquely identify users, and that endpoint
  5888. does not return a <code>sub</code> property, an alternative <code>subject_claim</code> has to be set.</p>
  5889. <ol>
  5890. <li>Create a new application.</li>
  5891. <li>Add this Callback URL: <code>[synapse public baseurl]/_synapse/client/oidc/callback</code></li>
  5892. </ol>
  5893. <p>Synapse config:</p>
  5894. <pre><code class="language-yaml">oidc_providers:
  5895. - idp_id: gitea
  5896. idp_name: Gitea
  5897. discover: false
  5898. issuer: &quot;https://your-gitea.com/&quot;
  5899. client_id: &quot;your-client-id&quot; # TO BE FILLED
  5900. client_secret: &quot;your-client-secret&quot; # TO BE FILLED
  5901. client_auth_method: client_secret_post
  5902. scopes: [] # Gitea doesn't support Scopes
  5903. authorization_endpoint: &quot;https://your-gitea.com/login/oauth/authorize&quot;
  5904. token_endpoint: &quot;https://your-gitea.com/login/oauth/access_token&quot;
  5905. userinfo_endpoint: &quot;https://your-gitea.com/api/v1/user&quot;
  5906. user_mapping_provider:
  5907. config:
  5908. subject_claim: &quot;id&quot;
  5909. localpart_template: &quot;{{ user.login }}&quot;
  5910. display_name_template: &quot;{{ user.full_name }}&quot;
  5911. </code></pre>
  5912. <h3 id="xwiki"><a class="header" href="#xwiki">XWiki</a></h3>
  5913. <p>Install <a href="https://extensions.xwiki.org/xwiki/bin/view/Extension/OpenID%20Connect/OpenID%20Connect%20Provider/">OpenID Connect Provider</a> extension in your <a href="https://www.xwiki.org">XWiki</a> instance.</p>
  5914. <p>Synapse config:</p>
  5915. <pre><code class="language-yaml">oidc_providers:
  5916. - idp_id: xwiki
  5917. idp_name: &quot;XWiki&quot;
  5918. issuer: &quot;https://myxwikihost/xwiki/oidc/&quot;
  5919. client_id: &quot;your-client-id&quot; # TO BE FILLED
  5920. client_auth_method: none
  5921. scopes: [&quot;openid&quot;, &quot;profile&quot;]
  5922. user_profile_method: &quot;userinfo_endpoint&quot;
  5923. user_mapping_provider:
  5924. config:
  5925. localpart_template: &quot;{{ user.preferred_username }}&quot;
  5926. display_name_template: &quot;{{ user.name }}&quot;
  5927. </code></pre>
  5928. <h2 id="apple"><a class="header" href="#apple">Apple</a></h2>
  5929. <p>Configuring &quot;Sign in with Apple&quot; (SiWA) requires an Apple Developer account.</p>
  5930. <p>You will need to create a new &quot;Services ID&quot; for SiWA, and create and download a
  5931. private key with &quot;SiWA&quot; enabled.</p>
  5932. <p>As well as the private key file, you will need:</p>
  5933. <ul>
  5934. <li>Client ID: the &quot;identifier&quot; you gave the &quot;Services ID&quot;</li>
  5935. <li>Team ID: a 10-character ID associated with your developer account.</li>
  5936. <li>Key ID: the 10-character identifier for the key.</li>
  5937. </ul>
  5938. <p>https://help.apple.com/developer-account/?lang=en#/dev77c875b7e has more
  5939. documentation on setting up SiWA.</p>
  5940. <p>The synapse config will look like this:</p>
  5941. <pre><code class="language-yaml"> - idp_id: apple
  5942. idp_name: Apple
  5943. issuer: &quot;https://appleid.apple.com&quot;
  5944. client_id: &quot;your-client-id&quot; # Set to the &quot;identifier&quot; for your &quot;ServicesID&quot;
  5945. client_auth_method: &quot;client_secret_post&quot;
  5946. client_secret_jwt_key:
  5947. key_file: &quot;/path/to/AuthKey_KEYIDCODE.p8&quot; # point to your key file
  5948. jwt_header:
  5949. alg: ES256
  5950. kid: &quot;KEYIDCODE&quot; # Set to the 10-char Key ID
  5951. jwt_payload:
  5952. iss: TEAMIDCODE # Set to the 10-char Team ID
  5953. scopes: [&quot;name&quot;, &quot;email&quot;, &quot;openid&quot;]
  5954. authorization_endpoint: https://appleid.apple.com/auth/authorize?response_mode=form_post
  5955. user_mapping_provider:
  5956. config:
  5957. email_template: &quot;{{ user.email }}&quot;
  5958. </code></pre>
  5959. <div style="break-before: page; page-break-before: always;"></div><h1 id="sso-mapping-providers"><a class="header" href="#sso-mapping-providers">SSO Mapping Providers</a></h1>
  5960. <p>A mapping provider is a Python class (loaded via a Python module) that
  5961. works out how to map attributes of a SSO response to Matrix-specific
  5962. user attributes. Details such as user ID localpart, displayname, and even avatar
  5963. URLs are all things that can be mapped from talking to a SSO service.</p>
  5964. <p>As an example, a SSO service may return the email address
  5965. &quot;john.smith@example.com&quot; for a user, whereas Synapse will need to figure out how
  5966. to turn that into a displayname when creating a Matrix user for this individual.
  5967. It may choose <code>John Smith</code>, or <code>Smith, John [Example.com]</code> or any number of
  5968. variations. As each Synapse configuration may want something different, this is
  5969. where SAML mapping providers come into play.</p>
  5970. <p>SSO mapping providers are currently supported for OpenID and SAML SSO
  5971. configurations. Please see the details below for how to implement your own.</p>
  5972. <p>It is up to the mapping provider whether the user should be assigned a predefined
  5973. Matrix ID based on the SSO attributes, or if the user should be allowed to
  5974. choose their own username.</p>
  5975. <p>In the first case - where users are automatically allocated a Matrix ID - it is
  5976. the responsibility of the mapping provider to normalise the SSO attributes and
  5977. map them to a valid Matrix ID. The <a href="https://matrix.org/docs/spec/appendices#user-identifiers">specification for Matrix
  5978. IDs</a> has some
  5979. information about what is considered valid.</p>
  5980. <p>If the mapping provider does not assign a Matrix ID, then Synapse will
  5981. automatically serve an HTML page allowing the user to pick their own username.</p>
  5982. <p>External mapping providers are provided to Synapse in the form of an external
  5983. Python module. You can retrieve this module from <a href="https://pypi.org">PyPI</a> or elsewhere,
  5984. but it must be importable via Synapse (e.g. it must be in the same virtualenv
  5985. as Synapse). The Synapse config is then modified to point to the mapping provider
  5986. (and optionally provide additional configuration for it).</p>
  5987. <h2 id="openid-mapping-providers"><a class="header" href="#openid-mapping-providers">OpenID Mapping Providers</a></h2>
  5988. <p>The OpenID mapping provider can be customized by editing the
  5989. <code>oidc_config.user_mapping_provider.module</code> config option.</p>
  5990. <p><code>oidc_config.user_mapping_provider.config</code> allows you to provide custom
  5991. configuration options to the module. Check with the module's documentation for
  5992. what options it provides (if any). The options listed by default are for the
  5993. user mapping provider built in to Synapse. If using a custom module, you should
  5994. comment these options out and use those specified by the module instead.</p>
  5995. <h3 id="building-a-custom-openid-mapping-provider"><a class="header" href="#building-a-custom-openid-mapping-provider">Building a Custom OpenID Mapping Provider</a></h3>
  5996. <p>A custom mapping provider must specify the following methods:</p>
  5997. <ul>
  5998. <li><code>__init__(self, parsed_config)</code>
  5999. <ul>
  6000. <li>Arguments:
  6001. <ul>
  6002. <li><code>parsed_config</code> - A configuration object that is the return value of the
  6003. <code>parse_config</code> method. You should set any configuration options needed by
  6004. the module here.</li>
  6005. </ul>
  6006. </li>
  6007. </ul>
  6008. </li>
  6009. <li><code>parse_config(config)</code>
  6010. <ul>
  6011. <li>This method should have the <code>@staticmethod</code> decoration.</li>
  6012. <li>Arguments:
  6013. <ul>
  6014. <li><code>config</code> - A <code>dict</code> representing the parsed content of the
  6015. <code>oidc_config.user_mapping_provider.config</code> homeserver config option.
  6016. Runs on homeserver startup. Providers should extract and validate
  6017. any option values they need here.</li>
  6018. </ul>
  6019. </li>
  6020. <li>Whatever is returned will be passed back to the user mapping provider module's
  6021. <code>__init__</code> method during construction.</li>
  6022. </ul>
  6023. </li>
  6024. <li><code>get_remote_user_id(self, userinfo)</code>
  6025. <ul>
  6026. <li>Arguments:
  6027. <ul>
  6028. <li><code>userinfo</code> - A <code>authlib.oidc.core.claims.UserInfo</code> object to extract user
  6029. information from.</li>
  6030. </ul>
  6031. </li>
  6032. <li>This method must return a string, which is the unique, immutable identifier
  6033. for the user. Commonly the <code>sub</code> claim of the response.</li>
  6034. </ul>
  6035. </li>
  6036. <li><code>map_user_attributes(self, userinfo, token, failures)</code>
  6037. <ul>
  6038. <li>This method must be async.</li>
  6039. <li>Arguments:
  6040. <ul>
  6041. <li><code>userinfo</code> - A <code>authlib.oidc.core.claims.UserInfo</code> object to extract user
  6042. information from.</li>
  6043. <li><code>token</code> - A dictionary which includes information necessary to make
  6044. further requests to the OpenID provider.</li>
  6045. <li><code>failures</code> - An <code>int</code> that represents the amount of times the returned
  6046. mxid localpart mapping has failed. This should be used
  6047. to create a deduplicated mxid localpart which should be
  6048. returned instead. For example, if this method returns
  6049. <code>john.doe</code> as the value of <code>localpart</code> in the returned
  6050. dict, and that is already taken on the homeserver, this
  6051. method will be called again with the same parameters but
  6052. with failures=1. The method should then return a different
  6053. <code>localpart</code> value, such as <code>john.doe1</code>.</li>
  6054. </ul>
  6055. </li>
  6056. <li>Returns a dictionary with two keys:
  6057. <ul>
  6058. <li><code>localpart</code>: A string, used to generate the Matrix ID. If this is
  6059. <code>None</code>, the user is prompted to pick their own username. This is only used
  6060. during a user's first login. Once a localpart has been associated with a
  6061. remote user ID (see <code>get_remote_user_id</code>) it cannot be updated.</li>
  6062. <li><code>displayname</code>: An optional string, the display name for the user.</li>
  6063. </ul>
  6064. </li>
  6065. </ul>
  6066. </li>
  6067. <li><code>get_extra_attributes(self, userinfo, token)</code>
  6068. <ul>
  6069. <li>
  6070. <p>This method must be async.</p>
  6071. </li>
  6072. <li>
  6073. <p>Arguments:</p>
  6074. <ul>
  6075. <li><code>userinfo</code> - A <code>authlib.oidc.core.claims.UserInfo</code> object to extract user
  6076. information from.</li>
  6077. <li><code>token</code> - A dictionary which includes information necessary to make
  6078. further requests to the OpenID provider.</li>
  6079. </ul>
  6080. </li>
  6081. <li>
  6082. <p>Returns a dictionary that is suitable to be serialized to JSON. This
  6083. will be returned as part of the response during a successful login.</p>
  6084. <p>Note that care should be taken to not overwrite any of the parameters
  6085. usually returned as part of the <a href="https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-login">login response</a>.</p>
  6086. </li>
  6087. </ul>
  6088. </li>
  6089. </ul>
  6090. <h3 id="default-openid-mapping-provider"><a class="header" href="#default-openid-mapping-provider">Default OpenID Mapping Provider</a></h3>
  6091. <p>Synapse has a built-in OpenID mapping provider if a custom provider isn't
  6092. specified in the config. It is located at
  6093. <a href="https://github.com/matrix-org/synapse/blob/develop/synapse/handlers/oidc.py"><code>synapse.handlers.oidc.JinjaOidcMappingProvider</code></a>.</p>
  6094. <h2 id="saml-mapping-providers"><a class="header" href="#saml-mapping-providers">SAML Mapping Providers</a></h2>
  6095. <p>The SAML mapping provider can be customized by editing the
  6096. <code>saml2_config.user_mapping_provider.module</code> config option.</p>
  6097. <p><code>saml2_config.user_mapping_provider.config</code> allows you to provide custom
  6098. configuration options to the module. Check with the module's documentation for
  6099. what options it provides (if any). The options listed by default are for the
  6100. user mapping provider built in to Synapse. If using a custom module, you should
  6101. comment these options out and use those specified by the module instead.</p>
  6102. <h3 id="building-a-custom-saml-mapping-provider"><a class="header" href="#building-a-custom-saml-mapping-provider">Building a Custom SAML Mapping Provider</a></h3>
  6103. <p>A custom mapping provider must specify the following methods:</p>
  6104. <ul>
  6105. <li><code>__init__(self, parsed_config, module_api)</code>
  6106. <ul>
  6107. <li>Arguments:
  6108. <ul>
  6109. <li><code>parsed_config</code> - A configuration object that is the return value of the
  6110. <code>parse_config</code> method. You should set any configuration options needed by
  6111. the module here.</li>
  6112. <li><code>module_api</code> - a <code>synapse.module_api.ModuleApi</code> object which provides the
  6113. stable API available for extension modules.</li>
  6114. </ul>
  6115. </li>
  6116. </ul>
  6117. </li>
  6118. <li><code>parse_config(config)</code>
  6119. <ul>
  6120. <li>This method should have the <code>@staticmethod</code> decoration.</li>
  6121. <li>Arguments:
  6122. <ul>
  6123. <li><code>config</code> - A <code>dict</code> representing the parsed content of the
  6124. <code>saml_config.user_mapping_provider.config</code> homeserver config option.
  6125. Runs on homeserver startup. Providers should extract and validate
  6126. any option values they need here.</li>
  6127. </ul>
  6128. </li>
  6129. <li>Whatever is returned will be passed back to the user mapping provider module's
  6130. <code>__init__</code> method during construction.</li>
  6131. </ul>
  6132. </li>
  6133. <li><code>get_saml_attributes(config)</code>
  6134. <ul>
  6135. <li>This method should have the <code>@staticmethod</code> decoration.</li>
  6136. <li>Arguments:
  6137. <ul>
  6138. <li><code>config</code> - A object resulting from a call to <code>parse_config</code>.</li>
  6139. </ul>
  6140. </li>
  6141. <li>Returns a tuple of two sets. The first set equates to the SAML auth
  6142. response attributes that are required for the module to function, whereas
  6143. the second set consists of those attributes which can be used if available,
  6144. but are not necessary.</li>
  6145. </ul>
  6146. </li>
  6147. <li><code>get_remote_user_id(self, saml_response, client_redirect_url)</code>
  6148. <ul>
  6149. <li>Arguments:
  6150. <ul>
  6151. <li><code>saml_response</code> - A <code>saml2.response.AuthnResponse</code> object to extract user
  6152. information from.</li>
  6153. <li><code>client_redirect_url</code> - A string, the URL that the client will be
  6154. redirected to.</li>
  6155. </ul>
  6156. </li>
  6157. <li>This method must return a string, which is the unique, immutable identifier
  6158. for the user. Commonly the <code>uid</code> claim of the response.</li>
  6159. </ul>
  6160. </li>
  6161. <li><code>saml_response_to_user_attributes(self, saml_response, failures, client_redirect_url)</code>
  6162. <ul>
  6163. <li>
  6164. <p>Arguments:</p>
  6165. <ul>
  6166. <li><code>saml_response</code> - A <code>saml2.response.AuthnResponse</code> object to extract user
  6167. information from.</li>
  6168. <li><code>failures</code> - An <code>int</code> that represents the amount of times the returned
  6169. mxid localpart mapping has failed. This should be used
  6170. to create a deduplicated mxid localpart which should be
  6171. returned instead. For example, if this method returns
  6172. <code>john.doe</code> as the value of <code>mxid_localpart</code> in the returned
  6173. dict, and that is already taken on the homeserver, this
  6174. method will be called again with the same parameters but
  6175. with failures=1. The method should then return a different
  6176. <code>mxid_localpart</code> value, such as <code>john.doe1</code>.</li>
  6177. <li><code>client_redirect_url</code> - A string, the URL that the client will be
  6178. redirected to.</li>
  6179. </ul>
  6180. </li>
  6181. <li>
  6182. <p>This method must return a dictionary, which will then be used by Synapse
  6183. to build a new user. The following keys are allowed:</p>
  6184. <ul>
  6185. <li><code>mxid_localpart</code> - A string, the mxid localpart of the new user. If this is
  6186. <code>None</code>, the user is prompted to pick their own username. This is only used
  6187. during a user's first login. Once a localpart has been associated with a
  6188. remote user ID (see <code>get_remote_user_id</code>) it cannot be updated.</li>
  6189. <li><code>displayname</code> - The displayname of the new user. If not provided, will default to
  6190. the value of <code>mxid_localpart</code>.</li>
  6191. <li><code>emails</code> - A list of emails for the new user. If not provided, will
  6192. default to an empty list.</li>
  6193. </ul>
  6194. <p>Alternatively it can raise a <code>synapse.api.errors.RedirectException</code> to
  6195. redirect the user to another page. This is useful to prompt the user for
  6196. additional information, e.g. if you want them to provide their own username.
  6197. It is the responsibility of the mapping provider to either redirect back
  6198. to <code>client_redirect_url</code> (including any additional information) or to
  6199. complete registration using methods from the <code>ModuleApi</code>.</p>
  6200. </li>
  6201. </ul>
  6202. </li>
  6203. </ul>
  6204. <h3 id="default-saml-mapping-provider"><a class="header" href="#default-saml-mapping-provider">Default SAML Mapping Provider</a></h3>
  6205. <p>Synapse has a built-in SAML mapping provider if a custom provider isn't
  6206. specified in the config. It is located at
  6207. <a href="https://github.com/matrix-org/synapse/blob/develop/synapse/handlers/saml.py"><code>synapse.handlers.saml.DefaultSamlMappingProvider</code></a>.</p>
  6208. <div style="break-before: page; page-break-before: always;"></div><h1 id="password-auth-provider-modules"><a class="header" href="#password-auth-provider-modules">Password auth provider modules</a></h1>
  6209. <p>Password auth providers offer a way for server administrators to
  6210. integrate their Synapse installation with an existing authentication
  6211. system.</p>
  6212. <p>A password auth provider is a Python class which is dynamically loaded
  6213. into Synapse, and provides a number of methods by which it can integrate
  6214. with the authentication system.</p>
  6215. <p>This document serves as a reference for those looking to implement their
  6216. own password auth providers. Additionally, here is a list of known
  6217. password auth provider module implementations:</p>
  6218. <ul>
  6219. <li><a href="https://github.com/matrix-org/matrix-synapse-ldap3/">matrix-synapse-ldap3</a></li>
  6220. <li><a href="https://github.com/devture/matrix-synapse-shared-secret-auth">matrix-synapse-shared-secret-auth</a></li>
  6221. <li><a href="https://github.com/ma1uta/matrix-synapse-rest-password-provider">matrix-synapse-rest-password-provider</a></li>
  6222. </ul>
  6223. <h2 id="required-methods"><a class="header" href="#required-methods">Required methods</a></h2>
  6224. <p>Password auth provider classes must provide the following methods:</p>
  6225. <ul>
  6226. <li>
  6227. <p><code>parse_config(config)</code>
  6228. This method is passed the <code>config</code> object for this module from the
  6229. homeserver configuration file.</p>
  6230. <p>It should perform any appropriate sanity checks on the provided
  6231. configuration, and return an object which is then passed into
  6232. <code>__init__</code>.</p>
  6233. <p>This method should have the <code>@staticmethod</code> decoration.</p>
  6234. </li>
  6235. <li>
  6236. <p><code>__init__(self, config, account_handler)</code></p>
  6237. <p>The constructor is passed the config object returned by
  6238. <code>parse_config</code>, and a <code>synapse.module_api.ModuleApi</code> object which
  6239. allows the password provider to check if accounts exist and/or create
  6240. new ones.</p>
  6241. </li>
  6242. </ul>
  6243. <h2 id="optional-methods"><a class="header" href="#optional-methods">Optional methods</a></h2>
  6244. <p>Password auth provider classes may optionally provide the following methods:</p>
  6245. <ul>
  6246. <li>
  6247. <p><code>get_db_schema_files(self)</code></p>
  6248. <p>This method, if implemented, should return an Iterable of
  6249. <code>(name, stream)</code> pairs of database schema files. Each file is applied
  6250. in turn at initialisation, and a record is then made in the database
  6251. so that it is not re-applied on the next start.</p>
  6252. </li>
  6253. <li>
  6254. <p><code>get_supported_login_types(self)</code></p>
  6255. <p>This method, if implemented, should return a <code>dict</code> mapping from a
  6256. login type identifier (such as <code>m.login.password</code>) to an iterable
  6257. giving the fields which must be provided by the user in the submission
  6258. to <a href="https://matrix.org/docs/spec/client_server/latest#post-matrix-client-r0-login">the <code>/login</code> API</a>.
  6259. These fields are passed in the <code>login_dict</code> dictionary to <code>check_auth</code>.</p>
  6260. <p>For example, if a password auth provider wants to implement a custom
  6261. login type of <code>com.example.custom_login</code>, where the client is expected
  6262. to pass the fields <code>secret1</code> and <code>secret2</code>, the provider should
  6263. implement this method and return the following dict:</p>
  6264. <pre><code class="language-python">{&quot;com.example.custom_login&quot;: (&quot;secret1&quot;, &quot;secret2&quot;)}
  6265. </code></pre>
  6266. </li>
  6267. <li>
  6268. <p><code>check_auth(self, username, login_type, login_dict)</code></p>
  6269. <p>This method does the real work. If implemented, it
  6270. will be called for each login attempt where the login type matches one
  6271. of the keys returned by <code>get_supported_login_types</code>.</p>
  6272. <p>It is passed the (possibly unqualified) <code>user</code> field provided by the client,
  6273. the login type, and a dictionary of login secrets passed by the
  6274. client.</p>
  6275. <p>The method should return an <code>Awaitable</code> object, which resolves
  6276. to the canonical <code>@localpart:domain</code> user ID if authentication is
  6277. successful, and <code>None</code> if not.</p>
  6278. <p>Alternatively, the <code>Awaitable</code> can resolve to a <code>(str, func)</code> tuple, in
  6279. which case the second field is a callback which will be called with
  6280. the result from the <code>/login</code> call (including <code>access_token</code>,
  6281. <code>device_id</code>, etc.)</p>
  6282. </li>
  6283. <li>
  6284. <p><code>check_3pid_auth(self, medium, address, password)</code></p>
  6285. <p>This method, if implemented, is called when a user attempts to
  6286. register or log in with a third party identifier, such as email. It is
  6287. passed the medium (ex. &quot;email&quot;), an address (ex.
  6288. &quot;<a href="mailto:jdoe@example.com">jdoe@example.com</a>&quot;) and the user's password.</p>
  6289. <p>The method should return an <code>Awaitable</code> object, which resolves
  6290. to a <code>str</code> containing the user's (canonical) User id if
  6291. authentication was successful, and <code>None</code> if not.</p>
  6292. <p>As with <code>check_auth</code>, the <code>Awaitable</code> may alternatively resolve to a
  6293. <code>(user_id, callback)</code> tuple.</p>
  6294. </li>
  6295. <li>
  6296. <p><code>check_password(self, user_id, password)</code></p>
  6297. <p>This method provides a simpler interface than
  6298. <code>get_supported_login_types</code> and <code>check_auth</code> for password auth
  6299. providers that just want to provide a mechanism for validating
  6300. <code>m.login.password</code> logins.</p>
  6301. <p>If implemented, it will be called to check logins with an
  6302. <code>m.login.password</code> login type. It is passed a qualified
  6303. <code>@localpart:domain</code> user id, and the password provided by the user.</p>
  6304. <p>The method should return an <code>Awaitable</code> object, which resolves
  6305. to <code>True</code> if authentication is successful, and <code>False</code> if not.</p>
  6306. </li>
  6307. <li>
  6308. <p><code>on_logged_out(self, user_id, device_id, access_token)</code></p>
  6309. <p>This method, if implemented, is called when a user logs out. It is
  6310. passed the qualified user ID, the ID of the deactivated device (if
  6311. any: access tokens are occasionally created without an associated
  6312. device ID), and the (now deactivated) access token.</p>
  6313. <p>It may return an <code>Awaitable</code> object; the logout request will
  6314. wait for the <code>Awaitable</code> to complete, but the result is ignored.</p>
  6315. </li>
  6316. </ul>
  6317. <div style="break-before: page; page-break-before: always;"></div><h1 id="jwt-login-type"><a class="header" href="#jwt-login-type">JWT Login Type</a></h1>
  6318. <p>Synapse comes with a non-standard login type to support
  6319. <a href="https://en.wikipedia.org/wiki/JSON_Web_Token">JSON Web Tokens</a>. In general the
  6320. documentation for
  6321. <a href="https://matrix.org/docs/spec/client_server/r0.6.1#login">the login endpoint</a>
  6322. is still valid (and the mechanism works similarly to the
  6323. <a href="https://matrix.org/docs/spec/client_server/r0.6.1#token-based">token based login</a>).</p>
  6324. <p>To log in using a JSON Web Token, clients should submit a <code>/login</code> request as
  6325. follows:</p>
  6326. <pre><code class="language-json">{
  6327. &quot;type&quot;: &quot;org.matrix.login.jwt&quot;,
  6328. &quot;token&quot;: &quot;&lt;jwt&gt;&quot;
  6329. }
  6330. </code></pre>
  6331. <p>Note that the login type of <code>m.login.jwt</code> is supported, but is deprecated. This
  6332. will be removed in a future version of Synapse.</p>
  6333. <p>The <code>token</code> field should include the JSON web token with the following claims:</p>
  6334. <ul>
  6335. <li>The <code>sub</code> (subject) claim is required and should encode the local part of the
  6336. user ID.</li>
  6337. <li>The expiration time (<code>exp</code>), not before time (<code>nbf</code>), and issued at (<code>iat</code>)
  6338. claims are optional, but validated if present.</li>
  6339. <li>The issuer (<code>iss</code>) claim is optional, but required and validated if configured.</li>
  6340. <li>The audience (<code>aud</code>) claim is optional, but required and validated if configured.
  6341. Providing the audience claim when not configured will cause validation to fail.</li>
  6342. </ul>
  6343. <p>In the case that the token is not valid, the homeserver must respond with
  6344. <code>403 Forbidden</code> and an error code of <code>M_FORBIDDEN</code>.</p>
  6345. <p>As with other login types, there are additional fields (e.g. <code>device_id</code> and
  6346. <code>initial_device_display_name</code>) which can be included in the above request.</p>
  6347. <h2 id="preparing-synapse-1"><a class="header" href="#preparing-synapse-1">Preparing Synapse</a></h2>
  6348. <p>The JSON Web Token integration in Synapse uses the
  6349. <a href="https://pypi.org/project/pyjwt/"><code>PyJWT</code></a> library, which must be installed
  6350. as follows:</p>
  6351. <ul>
  6352. <li>
  6353. <p>The relevant libraries are included in the Docker images and Debian packages
  6354. provided by <code>matrix.org</code> so no further action is needed.</p>
  6355. </li>
  6356. <li>
  6357. <p>If you installed Synapse into a virtualenv, run <code>/path/to/env/bin/pip install synapse[pyjwt]</code> to install the necessary dependencies.</p>
  6358. </li>
  6359. <li>
  6360. <p>For other installation mechanisms, see the documentation provided by the
  6361. maintainer.</p>
  6362. </li>
  6363. </ul>
  6364. <p>To enable the JSON web token integration, you should then add an <code>jwt_config</code> section
  6365. to your configuration file (or uncomment the <code>enabled: true</code> line in the
  6366. existing section). See <a href="./sample_config.yaml">sample_config.yaml</a> for some
  6367. sample settings.</p>
  6368. <h2 id="how-to-test-jwt-as-a-developer"><a class="header" href="#how-to-test-jwt-as-a-developer">How to test JWT as a developer</a></h2>
  6369. <p>Although JSON Web Tokens are typically generated from an external server, the
  6370. examples below use <a href="https://pyjwt.readthedocs.io/en/latest/">PyJWT</a> directly.</p>
  6371. <ol>
  6372. <li>
  6373. <p>Configure Synapse with JWT logins, note that this example uses a pre-shared
  6374. secret and an algorithm of HS256:</p>
  6375. <pre><code class="language-yaml">jwt_config:
  6376. enabled: true
  6377. secret: &quot;my-secret-token&quot;
  6378. algorithm: &quot;HS256&quot;
  6379. </code></pre>
  6380. </li>
  6381. <li>
  6382. <p>Generate a JSON web token:</p>
  6383. <pre><code class="language-bash">$ pyjwt --key=my-secret-token --alg=HS256 encode sub=test-user
  6384. eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0ZXN0LXVzZXIifQ.Ag71GT8v01UO3w80aqRPTeuVPBIBZkYhNTJJ-_-zQIc
  6385. </code></pre>
  6386. </li>
  6387. <li>
  6388. <p>Query for the login types and ensure <code>org.matrix.login.jwt</code> is there:</p>
  6389. <pre><code class="language-bash">curl http://localhost:8080/_matrix/client/r0/login
  6390. </code></pre>
  6391. </li>
  6392. <li>
  6393. <p>Login used the generated JSON web token from above:</p>
  6394. <pre><code class="language-bash">$ curl http://localhost:8082/_matrix/client/r0/login -X POST \
  6395. --data '{&quot;type&quot;:&quot;org.matrix.login.jwt&quot;,&quot;token&quot;:&quot;eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0ZXN0LXVzZXIifQ.Ag71GT8v01UO3w80aqRPTeuVPBIBZkYhNTJJ-_-zQIc&quot;}'
  6396. {
  6397. &quot;access_token&quot;: &quot;&lt;access token&gt;&quot;,
  6398. &quot;device_id&quot;: &quot;ACBDEFGHI&quot;,
  6399. &quot;home_server&quot;: &quot;localhost:8080&quot;,
  6400. &quot;user_id&quot;: &quot;@test-user:localhost:8480&quot;
  6401. }
  6402. </code></pre>
  6403. </li>
  6404. </ol>
  6405. <p>You should now be able to use the returned access token to query the client API.</p>
  6406. <div style="break-before: page; page-break-before: always;"></div><h1 id="overview-2"><a class="header" href="#overview-2">Overview</a></h1>
  6407. <p>A captcha can be enabled on your homeserver to help prevent bots from registering
  6408. accounts. Synapse currently uses Google's reCAPTCHA service which requires API keys
  6409. from Google.</p>
  6410. <h2 id="getting-api-keys"><a class="header" href="#getting-api-keys">Getting API keys</a></h2>
  6411. <ol>
  6412. <li>Create a new site at <a href="https://www.google.com/recaptcha/admin/create">https://www.google.com/recaptcha/admin/create</a></li>
  6413. <li>Set the label to anything you want</li>
  6414. <li>Set the type to reCAPTCHA v2 using the &quot;I'm not a robot&quot; Checkbox option.
  6415. This is the only type of captcha that works with Synapse.</li>
  6416. <li>Add the public hostname for your server, as set in <code>public_baseurl</code>
  6417. in <code>homeserver.yaml</code>, to the list of authorized domains. If you have not set
  6418. <code>public_baseurl</code>, use <code>server_name</code>.</li>
  6419. <li>Agree to the terms of service and submit.</li>
  6420. <li>Copy your site key and secret key and add them to your <code>homeserver.yaml</code>
  6421. configuration file
  6422. <pre><code>recaptcha_public_key: YOUR_SITE_KEY
  6423. recaptcha_private_key: YOUR_SECRET_KEY
  6424. </code></pre>
  6425. </li>
  6426. <li>Enable the CAPTCHA for new registrations
  6427. <pre><code>enable_registration_captcha: true
  6428. </code></pre>
  6429. </li>
  6430. <li>Go to the settings page for the CAPTCHA you just created</li>
  6431. <li>Uncheck the &quot;Verify the origin of reCAPTCHA solutions&quot; checkbox so that the
  6432. captcha can be displayed in any client. If you do not disable this option then you
  6433. must specify the domains of every client that is allowed to display the CAPTCHA.</li>
  6434. </ol>
  6435. <h2 id="configuring-ip-used-for-auth"><a class="header" href="#configuring-ip-used-for-auth">Configuring IP used for auth</a></h2>
  6436. <p>The reCAPTCHA API requires that the IP address of the user who solved the
  6437. CAPTCHA is sent. If the client is connecting through a proxy or load balancer,
  6438. it may be required to use the <code>X-Forwarded-For</code> (XFF) header instead of the origin
  6439. IP address. This can be configured using the <code>x_forwarded</code> directive in the
  6440. listeners section of the <code>homeserver.yaml</code> configuration file.</p>
  6441. <div style="break-before: page; page-break-before: always;"></div><h1 id="registering-an-application-service"><a class="header" href="#registering-an-application-service">Registering an Application Service</a></h1>
  6442. <p>The registration of new application services depends on the homeserver used.
  6443. In synapse, you need to create a new configuration file for your AS and add it
  6444. to the list specified under the <code>app_service_config_files</code> config
  6445. option in your synapse config.</p>
  6446. <p>For example:</p>
  6447. <pre><code class="language-yaml">app_service_config_files:
  6448. - /home/matrix/.synapse/&lt;your-AS&gt;.yaml
  6449. </code></pre>
  6450. <p>The format of the AS configuration file is as follows:</p>
  6451. <pre><code class="language-yaml">url: &lt;base url of AS&gt;
  6452. as_token: &lt;token AS will add to requests to HS&gt;
  6453. hs_token: &lt;token HS will add to requests to AS&gt;
  6454. sender_localpart: &lt;localpart of AS user&gt;
  6455. namespaces:
  6456. users: # List of users we're interested in
  6457. - exclusive: &lt;bool&gt;
  6458. regex: &lt;regex&gt;
  6459. group_id: &lt;group&gt;
  6460. - ...
  6461. aliases: [] # List of aliases we're interested in
  6462. rooms: [] # List of room ids we're interested in
  6463. </code></pre>
  6464. <p><code>exclusive</code>: If enabled, only this application service is allowed to register users in its namespace(s).
  6465. <code>group_id</code>: All users of this application service are dynamically joined to this group. This is useful for e.g user organisation or flairs.</p>
  6466. <p>See the <a href="https://matrix.org/docs/spec/application_service/unstable.html">spec</a> for further details on how application services work.</p>
  6467. <div style="break-before: page; page-break-before: always;"></div><h1 id="server-notices"><a class="header" href="#server-notices">Server Notices</a></h1>
  6468. <p>'Server Notices' are a new feature introduced in Synapse 0.30. They provide a
  6469. channel whereby server administrators can send messages to users on the server.</p>
  6470. <p>They are used as part of communication of the server polices(see
  6471. <a href="consent_tracking.html">consent_tracking.md</a>), however the intention is that
  6472. they may also find a use for features such as &quot;Message of the day&quot;.</p>
  6473. <p>This is a feature specific to Synapse, but it uses standard Matrix
  6474. communication mechanisms, so should work with any Matrix client.</p>
  6475. <h2 id="user-experience"><a class="header" href="#user-experience">User experience</a></h2>
  6476. <p>When the user is first sent a server notice, they will get an invitation to a
  6477. room (typically called 'Server Notices', though this is configurable in
  6478. <code>homeserver.yaml</code>). They will be <strong>unable to reject</strong> this invitation -
  6479. attempts to do so will receive an error.</p>
  6480. <p>Once they accept the invitation, they will see the notice message in the room
  6481. history; it will appear to have come from the 'server notices user' (see
  6482. below).</p>
  6483. <p>The user is prevented from sending any messages in this room by the power
  6484. levels.</p>
  6485. <p>Having joined the room, the user can leave the room if they want. Subsequent
  6486. server notices will then cause a new room to be created.</p>
  6487. <h2 id="synapse-configuration"><a class="header" href="#synapse-configuration">Synapse configuration</a></h2>
  6488. <p>Server notices come from a specific user id on the server. Server
  6489. administrators are free to choose the user id - something like <code>server</code> is
  6490. suggested, meaning the notices will come from
  6491. <code>@server:&lt;your_server_name&gt;</code>. Once the Server Notices user is configured, that
  6492. user id becomes a special, privileged user, so administrators should ensure
  6493. that <strong>it is not already allocated</strong>.</p>
  6494. <p>In order to support server notices, it is necessary to add some configuration
  6495. to the <code>homeserver.yaml</code> file. In particular, you should add a <code>server_notices</code>
  6496. section, which should look like this:</p>
  6497. <pre><code class="language-yaml">server_notices:
  6498. system_mxid_localpart: server
  6499. system_mxid_display_name: &quot;Server Notices&quot;
  6500. system_mxid_avatar_url: &quot;mxc://server.com/oumMVlgDnLYFaPVkExemNVVZ&quot;
  6501. room_name: &quot;Server Notices&quot;
  6502. </code></pre>
  6503. <p>The only compulsory setting is <code>system_mxid_localpart</code>, which defines the user
  6504. id of the Server Notices user, as above. <code>room_name</code> defines the name of the
  6505. room which will be created.</p>
  6506. <p><code>system_mxid_display_name</code> and <code>system_mxid_avatar_url</code> can be used to set the
  6507. displayname and avatar of the Server Notices user.</p>
  6508. <h2 id="sending-notices"><a class="header" href="#sending-notices">Sending notices</a></h2>
  6509. <p>To send server notices to users you can use the
  6510. <a href="admin_api/server_notices.html">admin_api</a>.</p>
  6511. <div style="break-before: page; page-break-before: always;"></div><h1 id="support-in-synapse-for-tracking-agreement-to-server-terms-and-conditions"><a class="header" href="#support-in-synapse-for-tracking-agreement-to-server-terms-and-conditions">Support in Synapse for tracking agreement to server terms and conditions</a></h1>
  6512. <p>Synapse 0.30 introduces support for tracking whether users have agreed to the
  6513. terms and conditions set by the administrator of a server - and blocking access
  6514. to the server until they have.</p>
  6515. <p>There are several parts to this functionality; each requires some specific
  6516. configuration in <code>homeserver.yaml</code> to be enabled.</p>
  6517. <p>Note that various parts of the configuation and this document refer to the
  6518. &quot;privacy policy&quot;: agreement with a privacy policy is one particular use of this
  6519. feature, but of course adminstrators can specify other terms and conditions
  6520. unrelated to &quot;privacy&quot; per se.</p>
  6521. <h2 id="collecting-policy-agreement-from-a-user"><a class="header" href="#collecting-policy-agreement-from-a-user">Collecting policy agreement from a user</a></h2>
  6522. <p>Synapse can be configured to serve the user a simple policy form with an
  6523. &quot;accept&quot; button. Clicking &quot;Accept&quot; records the user's acceptance in the
  6524. database and shows a success page.</p>
  6525. <p>To enable this, first create templates for the policy and success pages.
  6526. These should be stored on the local filesystem.</p>
  6527. <p>These templates use the <a href="http://jinja.pocoo.org">Jinja2</a> templating language,
  6528. and <a href="https://github.com/matrix-org/synapse/tree/develop/docs/privacy_policy_templates/">docs/privacy_policy_templates</a>
  6529. gives examples of the sort of thing that can be done.</p>
  6530. <p>Note that the templates must be stored under a name giving the language of the
  6531. template - currently this must always be <code>en</code> (for &quot;English&quot;);
  6532. internationalisation support is intended for the future.</p>
  6533. <p>The template for the policy itself should be versioned and named according to
  6534. the version: for example <code>1.0.html</code>. The version of the policy which the user
  6535. has agreed to is stored in the database.</p>
  6536. <p>Once the templates are in place, make the following changes to <code>homeserver.yaml</code>:</p>
  6537. <ol>
  6538. <li>
  6539. <p>Add a <code>user_consent</code> section, which should look like:</p>
  6540. <pre><code class="language-yaml">user_consent:
  6541. template_dir: privacy_policy_templates
  6542. version: 1.0
  6543. </code></pre>
  6544. <p><code>template_dir</code> points to the directory containing the policy
  6545. templates. <code>version</code> defines the version of the policy which will be served
  6546. to the user. In the example above, Synapse will serve
  6547. <code>privacy_policy_templates/en/1.0.html</code>.</p>
  6548. </li>
  6549. <li>
  6550. <p>Add a <code>form_secret</code> setting at the top level:</p>
  6551. <pre><code class="language-yaml">form_secret: &quot;&lt;unique secret&gt;&quot;
  6552. </code></pre>
  6553. <p>This should be set to an arbitrary secret string (try <code>pwgen -y 30</code> to
  6554. generate suitable secrets).</p>
  6555. <p>More on what this is used for below.</p>
  6556. </li>
  6557. <li>
  6558. <p>Add <code>consent</code> wherever the <code>client</code> resource is currently enabled in the
  6559. <code>listeners</code> configuration. For example:</p>
  6560. <pre><code class="language-yaml">listeners:
  6561. - port: 8008
  6562. resources:
  6563. - names:
  6564. - client
  6565. - consent
  6566. </code></pre>
  6567. </li>
  6568. </ol>
  6569. <p>Finally, ensure that <code>jinja2</code> is installed. If you are using a virtualenv, this
  6570. should be a matter of <code>pip install Jinja2</code>. On debian, try <code>apt-get install python-jinja2</code>.</p>
  6571. <p>Once this is complete, and the server has been restarted, try visiting
  6572. <code>https://&lt;server&gt;/_matrix/consent</code>. If correctly configured, this should give
  6573. an error &quot;Missing string query parameter 'u'&quot;. It is now possible to manually
  6574. construct URIs where users can give their consent.</p>
  6575. <h3 id="enabling-consent-tracking-at-registration"><a class="header" href="#enabling-consent-tracking-at-registration">Enabling consent tracking at registration</a></h3>
  6576. <ol>
  6577. <li>
  6578. <p>Add the following to your configuration:</p>
  6579. <pre><code class="language-yaml">user_consent:
  6580. require_at_registration: true
  6581. policy_name: &quot;Privacy Policy&quot; # or whatever you'd like to call the policy
  6582. </code></pre>
  6583. </li>
  6584. <li>
  6585. <p>In your consent templates, make use of the <code>public_version</code> variable to
  6586. see if an unauthenticated user is viewing the page. This is typically
  6587. wrapped around the form that would be used to actually agree to the document:</p>
  6588. <pre><code>{% if not public_version %}
  6589. &lt;!-- The variables used here are only provided when the 'u' param is given to the homeserver --&gt;
  6590. &lt;form method=&quot;post&quot; action=&quot;consent&quot;&gt;
  6591. &lt;input type=&quot;hidden&quot; name=&quot;v&quot; value=&quot;{{version}}&quot;/&gt;
  6592. &lt;input type=&quot;hidden&quot; name=&quot;u&quot; value=&quot;{{user}}&quot;/&gt;
  6593. &lt;input type=&quot;hidden&quot; name=&quot;h&quot; value=&quot;{{userhmac}}&quot;/&gt;
  6594. &lt;input type=&quot;submit&quot; value=&quot;Sure thing!&quot;/&gt;
  6595. &lt;/form&gt;
  6596. {% endif %}
  6597. </code></pre>
  6598. </li>
  6599. <li>
  6600. <p>Restart Synapse to apply the changes.</p>
  6601. </li>
  6602. </ol>
  6603. <p>Visiting <code>https://&lt;server&gt;/_matrix/consent</code> should now give you a view of the privacy
  6604. document. This is what users will be able to see when registering for accounts.</p>
  6605. <h3 id="constructing-the-consent-uri"><a class="header" href="#constructing-the-consent-uri">Constructing the consent URI</a></h3>
  6606. <p>It may be useful to manually construct the &quot;consent URI&quot; for a given user - for
  6607. instance, in order to send them an email asking them to consent. To do this,
  6608. take the base <code>https://&lt;server&gt;/_matrix/consent</code> URL and add the following
  6609. query parameters:</p>
  6610. <ul>
  6611. <li>
  6612. <p><code>u</code>: the user id of the user. This can either be a full MXID
  6613. (<code>@user:server.com</code>) or just the localpart (<code>user</code>).</p>
  6614. </li>
  6615. <li>
  6616. <p><code>h</code>: hex-encoded HMAC-SHA256 of <code>u</code> using the <code>form_secret</code> as a key. It is
  6617. possible to calculate this on the commandline with something like:</p>
  6618. <pre><code class="language-bash">echo -n '&lt;user&gt;' | openssl sha256 -hmac '&lt;form_secret&gt;'
  6619. </code></pre>
  6620. <p>This should result in a URI which looks something like:
  6621. <code>https://&lt;server&gt;/_matrix/consent?u=&lt;user&gt;&amp;h=68a152465a4d...</code>.</p>
  6622. </li>
  6623. </ul>
  6624. <p>Note that not providing a <code>u</code> parameter will be interpreted as wanting to view
  6625. the document from an unauthenticated perspective, such as prior to registration.
  6626. Therefore, the <code>h</code> parameter is not required in this scenario. To enable this
  6627. behaviour, set <code>require_at_registration</code> to <code>true</code> in your <code>user_consent</code> config.</p>
  6628. <h2 id="sending-users-a-server-notice-asking-them-to-agree-to-the-policy"><a class="header" href="#sending-users-a-server-notice-asking-them-to-agree-to-the-policy">Sending users a server notice asking them to agree to the policy</a></h2>
  6629. <p>It is possible to configure Synapse to send a <a href="server_notices.html">server
  6630. notice</a> to anybody who has not yet agreed to the current
  6631. version of the policy. To do so:</p>
  6632. <ul>
  6633. <li>
  6634. <p>ensure that the consent resource is configured, as in the previous section</p>
  6635. </li>
  6636. <li>
  6637. <p>ensure that server notices are configured, as in <a href="server_notices.html">server_notices.md</a>.</p>
  6638. </li>
  6639. <li>
  6640. <p>Add <code>server_notice_content</code> under <code>user_consent</code> in <code>homeserver.yaml</code>. For
  6641. example:</p>
  6642. <pre><code class="language-yaml">user_consent:
  6643. server_notice_content:
  6644. msgtype: m.text
  6645. body: &gt;-
  6646. Please give your consent to the privacy policy at %(consent_uri)s.
  6647. </code></pre>
  6648. <p>Synapse automatically replaces the placeholder <code>%(consent_uri)s</code> with the
  6649. consent uri for that user.</p>
  6650. </li>
  6651. <li>
  6652. <p>ensure that <code>public_baseurl</code> is set in <code>homeserver.yaml</code>, and gives the base
  6653. URI that clients use to connect to the server. (It is used to construct
  6654. <code>consent_uri</code> in the server notice.)</p>
  6655. </li>
  6656. </ul>
  6657. <h2 id="blocking-users-from-using-the-server-until-they-agree-to-the-policy"><a class="header" href="#blocking-users-from-using-the-server-until-they-agree-to-the-policy">Blocking users from using the server until they agree to the policy</a></h2>
  6658. <p>Synapse can be configured to block any attempts to join rooms or send messages
  6659. until the user has given their agreement to the policy. (Joining the server
  6660. notices room is exempted from this).</p>
  6661. <p>To enable this, add <code>block_events_error</code> under <code>user_consent</code>. For example:</p>
  6662. <pre><code class="language-yaml">user_consent:
  6663. block_events_error: &gt;-
  6664. You can't send any messages until you consent to the privacy policy at
  6665. %(consent_uri)s.
  6666. </code></pre>
  6667. <p>Synapse automatically replaces the placeholder <code>%(consent_uri)s</code> with the
  6668. consent uri for that user.</p>
  6669. <p>ensure that <code>public_baseurl</code> is set in <code>homeserver.yaml</code>, and gives the base
  6670. URI that clients use to connect to the server. (It is used to construct
  6671. <code>consent_uri</code> in the error.)</p>
  6672. <div style="break-before: page; page-break-before: always;"></div><h1 id="url-previews-1"><a class="header" href="#url-previews-1">URL Previews</a></h1>
  6673. <p>Design notes on a URL previewing service for Matrix:</p>
  6674. <p>Options are:</p>
  6675. <ol>
  6676. <li>Have an AS which listens for URLs, downloads them, and inserts an event that describes their metadata.</li>
  6677. </ol>
  6678. <ul>
  6679. <li>Pros:
  6680. <ul>
  6681. <li>Decouples the implementation entirely from Synapse.</li>
  6682. <li>Uses existing Matrix events &amp; content repo to store the metadata.</li>
  6683. </ul>
  6684. </li>
  6685. <li>Cons:
  6686. <ul>
  6687. <li>Which AS should provide this service for a room, and why should you trust it?</li>
  6688. <li>Doesn't work well with E2E; you'd have to cut the AS into every room</li>
  6689. <li>the AS would end up subscribing to every room anyway.</li>
  6690. </ul>
  6691. </li>
  6692. </ul>
  6693. <ol start="2">
  6694. <li>Have a generic preview API (nothing to do with Matrix) that provides a previewing service:</li>
  6695. </ol>
  6696. <ul>
  6697. <li>Pros:
  6698. <ul>
  6699. <li>Simple and flexible; can be used by any clients at any point</li>
  6700. </ul>
  6701. </li>
  6702. <li>Cons:
  6703. <ul>
  6704. <li>If each HS provides one of these independently, all the HSes in a room may needlessly DoS the target URI</li>
  6705. <li>We need somewhere to store the URL metadata rather than just using Matrix itself</li>
  6706. <li>We can't piggyback on matrix to distribute the metadata between HSes.</li>
  6707. </ul>
  6708. </li>
  6709. </ul>
  6710. <ol start="3">
  6711. <li>Make the synapse of the sending user responsible for spidering the URL and inserting an event asynchronously which describes the metadata.</li>
  6712. </ol>
  6713. <ul>
  6714. <li>Pros:
  6715. <ul>
  6716. <li>Works transparently for all clients</li>
  6717. <li>Piggy-backs nicely on using Matrix for distributing the metadata.</li>
  6718. <li>No confusion as to which AS</li>
  6719. </ul>
  6720. </li>
  6721. <li>Cons:
  6722. <ul>
  6723. <li>Doesn't work with E2E</li>
  6724. <li>We might want to decouple the implementation of the spider from the HS, given spider behaviour can be quite complicated and evolve much more rapidly than the HS. It's more like a bot than a core part of the server.</li>
  6725. </ul>
  6726. </li>
  6727. </ul>
  6728. <ol start="4">
  6729. <li>Make the sending client use the preview API and insert the event itself when successful.</li>
  6730. </ol>
  6731. <ul>
  6732. <li>Pros:
  6733. <ul>
  6734. <li>Works well with E2E</li>
  6735. <li>No custom server functionality</li>
  6736. <li>Lets the client customise the preview that they send (like on FB)</li>
  6737. </ul>
  6738. </li>
  6739. <li>Cons:
  6740. <ul>
  6741. <li>Entirely specific to the sending client, whereas it'd be nice if /any/ URL was correctly previewed if clients support it.</li>
  6742. </ul>
  6743. </li>
  6744. </ul>
  6745. <ol start="5">
  6746. <li>Have the option of specifying a shared (centralised) previewing service used by a room, to avoid all the different HSes in the room DoSing the target.</li>
  6747. </ol>
  6748. <p>Best solution is probably a combination of both 2 and 4.</p>
  6749. <ul>
  6750. <li>Sending clients do their best to create and send a preview at the point of sending the message, perhaps delaying the message until the preview is computed? (This also lets the user validate the preview before sending)</li>
  6751. <li>Receiving clients have the option of going and creating their own preview if one doesn't arrive soon enough (or if the original sender didn't create one)</li>
  6752. </ul>
  6753. <p>This is a bit magical though in that the preview could come from two entirely different sources - the sending HS or your local one. However, this can always be exposed to users: &quot;Generate your own URL previews if none are available?&quot;</p>
  6754. <p>This is tantamount also to senders calculating their own thumbnails for sending in advance of the main content - we are trusting the sender not to lie about the content in the thumbnail. Whereas currently thumbnails are calculated by the receiving homeserver to avoid this attack.</p>
  6755. <p>However, this kind of phishing attack does exist whether we let senders pick their thumbnails or not, in that a malicious sender can send normal text messages around the attachment claiming it to be legitimate. We could rely on (future) reputation/abuse management to punish users who phish (be it with bogus metadata or bogus descriptions). Bogus metadata is particularly bad though, especially if it's avoidable.</p>
  6756. <p>As a first cut, let's do #2 and have the receiver hit the API to calculate its own previews (as it does currently for image thumbnails). We can then extend/optimise this to option 4 as a special extra if needed.</p>
  6757. <h2 id="api"><a class="header" href="#api">API</a></h2>
  6758. <pre><code>GET /_matrix/media/r0/preview_url?url=http://wherever.com
  6759. 200 OK
  6760. {
  6761. &quot;og:type&quot; : &quot;article&quot;
  6762. &quot;og:url&quot; : &quot;https://twitter.com/matrixdotorg/status/684074366691356672&quot;
  6763. &quot;og:title&quot; : &quot;Matrix on Twitter&quot;
  6764. &quot;og:image&quot; : &quot;https://pbs.twimg.com/profile_images/500400952029888512/yI0qtFi7_400x400.png&quot;
  6765. &quot;og:description&quot; : &quot;“Synapse 0.12 is out! Lots of polishing, performance &amp;amp;amp; bugfixes: /sync API, /r0 prefix, fulltext search, 3PID invites https://t.co/5alhXLLEGP”&quot;
  6766. &quot;og:site_name&quot; : &quot;Twitter&quot;
  6767. }
  6768. </code></pre>
  6769. <ul>
  6770. <li>Downloads the URL
  6771. <ul>
  6772. <li>If HTML, just stores it in RAM and parses it for OG meta tags
  6773. <ul>
  6774. <li>Download any media OG meta tags to the media repo, and refer to them in the OG via mxc:// URIs.</li>
  6775. </ul>
  6776. </li>
  6777. <li>If a media filetype we know we can thumbnail: store it on disk, and hand it to the thumbnailer. Generate OG meta tags from the thumbnailer contents.</li>
  6778. <li>Otherwise, don't bother downloading further.</li>
  6779. </ul>
  6780. </li>
  6781. </ul>
  6782. <div style="break-before: page; page-break-before: always;"></div><h1 id="user-directory-api-implementation"><a class="header" href="#user-directory-api-implementation">User Directory API Implementation</a></h1>
  6783. <p>The user directory is currently maintained based on the 'visible' users
  6784. on this particular server - i.e. ones which your account shares a room with, or
  6785. who are present in a publicly viewable room present on the server.</p>
  6786. <p>The directory info is stored in various tables, which can (typically after
  6787. DB corruption) get stale or out of sync. If this happens, for now the
  6788. solution to fix it is to execute the SQL <a href="https://github.com/matrix-org/synapse/blob/master/synapse/storage/schema/main/delta/53/user_dir_populate.sql">here</a>
  6789. and then restart synapse. This should then start a background task to
  6790. flush the current tables and regenerate the directory.</p>
  6791. <div style="break-before: page; page-break-before: always;"></div><h1 id="message-retention-policies"><a class="header" href="#message-retention-policies">Message retention policies</a></h1>
  6792. <p>Synapse admins can enable support for message retention policies on
  6793. their homeserver. Message retention policies exist at a room level,
  6794. follow the semantics described in
  6795. <a href="https://github.com/matrix-org/matrix-doc/blob/matthew/msc1763/proposals/1763-configurable-retention-periods.md">MSC1763</a>,
  6796. and allow server and room admins to configure how long messages should
  6797. be kept in a homeserver's database before being purged from it.
  6798. <strong>Please note that, as this feature isn't part of the Matrix
  6799. specification yet, this implementation is to be considered as
  6800. experimental.</strong> </p>
  6801. <p>A message retention policy is mainly defined by its <code>max_lifetime</code>
  6802. parameter, which defines how long a message can be kept around after
  6803. it was sent to the room. If a room doesn't have a message retention
  6804. policy, and there's no default one for a given server, then no message
  6805. sent in that room is ever purged on that server.</p>
  6806. <p>MSC1763 also specifies semantics for a <code>min_lifetime</code> parameter which
  6807. defines the amount of time after which an event <em>can</em> get purged (after
  6808. it was sent to the room), but Synapse doesn't currently support it
  6809. beyond registering it.</p>
  6810. <p>Both <code>max_lifetime</code> and <code>min_lifetime</code> are optional parameters.</p>
  6811. <p>Note that message retention policies don't apply to state events.</p>
  6812. <p>Once an event reaches its expiry date (defined as the time it was sent
  6813. plus the value for <code>max_lifetime</code> in the room), two things happen:</p>
  6814. <ul>
  6815. <li>Synapse stops serving the event to clients via any endpoint.</li>
  6816. <li>The message gets picked up by the next purge job (see the &quot;Purge jobs&quot;
  6817. section) and is removed from Synapse's database.</li>
  6818. </ul>
  6819. <p>Since purge jobs don't run continuously, this means that an event might
  6820. stay in a server's database for longer than the value for <code>max_lifetime</code>
  6821. in the room would allow, though hidden from clients.</p>
  6822. <p>Similarly, if a server (with support for message retention policies
  6823. enabled) receives from another server an event that should have been
  6824. purged according to its room's policy, then the receiving server will
  6825. process and store that event until it's picked up by the next purge job,
  6826. though it will always hide it from clients.</p>
  6827. <p>Synapse requires at least one message in each room, so it will never
  6828. delete the last message in a room. It will, however, hide it from
  6829. clients.</p>
  6830. <h2 id="server-configuration"><a class="header" href="#server-configuration">Server configuration</a></h2>
  6831. <p>Support for this feature can be enabled and configured in the
  6832. <code>retention</code> section of the Synapse configuration file (see the
  6833. <a href="https://github.com/matrix-org/synapse/blob/v1.36.0/docs/sample_config.yaml#L451-L518">sample file</a>).</p>
  6834. <p>To enable support for message retention policies, set the setting
  6835. <code>enabled</code> in this section to <code>true</code>.</p>
  6836. <h3 id="default-policy"><a class="header" href="#default-policy">Default policy</a></h3>
  6837. <p>A default message retention policy is a policy defined in Synapse's
  6838. configuration that is used by Synapse for every room that doesn't have a
  6839. message retention policy configured in its state. This allows server
  6840. admins to ensure that messages are never kept indefinitely in a server's
  6841. database. </p>
  6842. <p>A default policy can be defined as such, in the <code>retention</code> section of
  6843. the configuration file:</p>
  6844. <pre><code class="language-yaml"> default_policy:
  6845. min_lifetime: 1d
  6846. max_lifetime: 1y
  6847. </code></pre>
  6848. <p>Here, <code>min_lifetime</code> and <code>max_lifetime</code> have the same meaning and level
  6849. of support as previously described. They can be expressed either as a
  6850. duration (using the units <code>s</code> (seconds), <code>m</code> (minutes), <code>h</code> (hours),
  6851. <code>d</code> (days), <code>w</code> (weeks) and <code>y</code> (years)) or as a number of milliseconds.</p>
  6852. <h3 id="purge-jobs"><a class="header" href="#purge-jobs">Purge jobs</a></h3>
  6853. <p>Purge jobs are the jobs that Synapse runs in the background to purge
  6854. expired events from the database. They are only run if support for
  6855. message retention policies is enabled in the server's configuration. If
  6856. no configuration for purge jobs is configured by the server admin,
  6857. Synapse will use a default configuration, which is described in the
  6858. <a href="https://github.com/matrix-org/synapse/blob/v1.36.0/docs/sample_config.yaml#L451-L518">sample configuration file</a>.</p>
  6859. <p>Some server admins might want a finer control on when events are removed
  6860. depending on an event's room's policy. This can be done by setting the
  6861. <code>purge_jobs</code> sub-section in the <code>retention</code> section of the configuration
  6862. file. An example of such configuration could be:</p>
  6863. <pre><code class="language-yaml"> purge_jobs:
  6864. - longest_max_lifetime: 3d
  6865. interval: 12h
  6866. - shortest_max_lifetime: 3d
  6867. longest_max_lifetime: 1w
  6868. interval: 1d
  6869. - shortest_max_lifetime: 1w
  6870. interval: 2d
  6871. </code></pre>
  6872. <p>In this example, we define three jobs:</p>
  6873. <ul>
  6874. <li>one that runs twice a day (every 12 hours) and purges events in rooms
  6875. which policy's <code>max_lifetime</code> is lower or equal to 3 days.</li>
  6876. <li>one that runs once a day and purges events in rooms which policy's
  6877. <code>max_lifetime</code> is between 3 days and a week.</li>
  6878. <li>one that runs once every 2 days and purges events in rooms which
  6879. policy's <code>max_lifetime</code> is greater than a week.</li>
  6880. </ul>
  6881. <p>Note that this example is tailored to show different configurations and
  6882. features slightly more jobs than it's probably necessary (in practice, a
  6883. server admin would probably consider it better to replace the two last
  6884. jobs with one that runs once a day and handles rooms which which
  6885. policy's <code>max_lifetime</code> is greater than 3 days).</p>
  6886. <p>Keep in mind, when configuring these jobs, that a purge job can become
  6887. quite heavy on the server if it targets many rooms, therefore prefer
  6888. having jobs with a low interval that target a limited set of rooms. Also
  6889. make sure to include a job with no minimum and one with no maximum to
  6890. make sure your configuration handles every policy.</p>
  6891. <p>As previously mentioned in this documentation, while a purge job that
  6892. runs e.g. every day means that an expired event might stay in the
  6893. database for up to a day after its expiry, Synapse hides expired events
  6894. from clients as soon as they expire, so the event is not visible to
  6895. local users between its expiry date and the moment it gets purged from
  6896. the server's database.</p>
  6897. <h3 id="lifetime-limits"><a class="header" href="#lifetime-limits">Lifetime limits</a></h3>
  6898. <p>Server admins can set limits on the values of <code>max_lifetime</code> to use when
  6899. purging old events in a room. These limits can be defined as such in the
  6900. <code>retention</code> section of the configuration file:</p>
  6901. <pre><code class="language-yaml"> allowed_lifetime_min: 1d
  6902. allowed_lifetime_max: 1y
  6903. </code></pre>
  6904. <p>The limits are considered when running purge jobs. If necessary, the
  6905. effective value of <code>max_lifetime</code> will be brought between
  6906. <code>allowed_lifetime_min</code> and <code>allowed_lifetime_max</code> (inclusive).
  6907. This means that, if the value of <code>max_lifetime</code> defined in the room's state
  6908. is lower than <code>allowed_lifetime_min</code>, the value of <code>allowed_lifetime_min</code>
  6909. will be used instead. Likewise, if the value of <code>max_lifetime</code> is higher
  6910. than <code>allowed_lifetime_max</code>, the value of <code>allowed_lifetime_max</code> will be
  6911. used instead.</p>
  6912. <p>In the example above, we ensure Synapse never deletes events that are less
  6913. than one day old, and that it always deletes events that are over a year
  6914. old.</p>
  6915. <p>If a default policy is set, and its <code>max_lifetime</code> value is lower than
  6916. <code>allowed_lifetime_min</code> or higher than <code>allowed_lifetime_max</code>, the same
  6917. process applies.</p>
  6918. <p>Both parameters are optional; if one is omitted Synapse won't use it to
  6919. adjust the effective value of <code>max_lifetime</code>.</p>
  6920. <p>Like other settings in this section, these parameters can be expressed
  6921. either as a duration or as a number of milliseconds.</p>
  6922. <h2 id="room-configuration"><a class="header" href="#room-configuration">Room configuration</a></h2>
  6923. <p>To configure a room's message retention policy, a room's admin or
  6924. moderator needs to send a state event in that room with the type
  6925. <code>m.room.retention</code> and the following content:</p>
  6926. <pre><code class="language-json">{
  6927. &quot;max_lifetime&quot;: ...
  6928. }
  6929. </code></pre>
  6930. <p>In this event's content, the <code>max_lifetime</code> parameter has the same
  6931. meaning as previously described, and needs to be expressed in
  6932. milliseconds. The event's content can also include a <code>min_lifetime</code>
  6933. parameter, which has the same meaning and limited support as previously
  6934. described.</p>
  6935. <p>Note that over every server in the room, only the ones with support for
  6936. message retention policies will actually remove expired events. This
  6937. support is currently not enabled by default in Synapse.</p>
  6938. <h2 id="note-on-reclaiming-disk-space"><a class="header" href="#note-on-reclaiming-disk-space">Note on reclaiming disk space</a></h2>
  6939. <p>While purge jobs actually delete data from the database, the disk space
  6940. used by the database might not decrease immediately on the database's
  6941. host. However, even though the database engine won't free up the disk
  6942. space, it will start writing new data into where the purged data was.</p>
  6943. <p>If you want to reclaim the freed disk space anyway and return it to the
  6944. operating system, the server admin needs to run <code>VACUUM FULL;</code> (or
  6945. <code>VACUUM;</code> for SQLite databases) on Synapse's database (see the related
  6946. <a href="https://www.postgresql.org/docs/current/sql-vacuum.html">PostgreSQL documentation</a>).</p>
  6947. <div style="break-before: page; page-break-before: always;"></div><h1 id="modules"><a class="header" href="#modules">Modules</a></h1>
  6948. <p>Synapse supports extending its functionality by configuring external modules.</p>
  6949. <h2 id="using-modules"><a class="header" href="#using-modules">Using modules</a></h2>
  6950. <p>To use a module on Synapse, add it to the <code>modules</code> section of the configuration file:</p>
  6951. <pre><code class="language-yaml">modules:
  6952. - module: my_super_module.MySuperClass
  6953. config:
  6954. do_thing: true
  6955. - module: my_other_super_module.SomeClass
  6956. config: {}
  6957. </code></pre>
  6958. <p>Each module is defined by a path to a Python class as well as a configuration. This
  6959. information for a given module should be available in the module's own documentation.</p>
  6960. <p><strong>Note</strong>: When using third-party modules, you effectively allow someone else to run
  6961. custom code on your Synapse homeserver. Server admins are encouraged to verify the
  6962. provenance of the modules they use on their homeserver and make sure the modules aren't
  6963. running malicious code on their instance.</p>
  6964. <p>Also note that we are currently in the process of migrating module interfaces to this
  6965. system. While some interfaces might be compatible with it, others still require
  6966. configuring modules in another part of Synapse's configuration file. Currently, only the
  6967. spam checker interface is compatible with this new system.</p>
  6968. <h2 id="writing-a-module"><a class="header" href="#writing-a-module">Writing a module</a></h2>
  6969. <p>A module is a Python class that uses Synapse's module API to interact with the
  6970. homeserver. It can register callbacks that Synapse will call on specific operations, as
  6971. well as web resources to attach to Synapse's web server.</p>
  6972. <p>When instantiated, a module is given its parsed configuration as well as an instance of
  6973. the <code>synapse.module_api.ModuleApi</code> class. The configuration is a dictionary, and is
  6974. either the output of the module's <code>parse_config</code> static method (see below), or the
  6975. configuration associated with the module in Synapse's configuration file.</p>
  6976. <p>See the documentation for the <code>ModuleApi</code> class
  6977. <a href="https://github.com/matrix-org/synapse/blob/master/synapse/module_api/__init__.py">here</a>.</p>
  6978. <h3 id="handling-the-modules-configuration"><a class="header" href="#handling-the-modules-configuration">Handling the module's configuration</a></h3>
  6979. <p>A module can implement the following static method:</p>
  6980. <pre><code class="language-python">@staticmethod
  6981. def parse_config(config: dict) -&gt; dict
  6982. </code></pre>
  6983. <p>This method is given a dictionary resulting from parsing the YAML configuration for the
  6984. module. It may modify it (for example by parsing durations expressed as strings (e.g.
  6985. &quot;5d&quot;) into milliseconds, etc.), and return the modified dictionary. It may also verify
  6986. that the configuration is correct, and raise an instance of
  6987. <code>synapse.module_api.errors.ConfigError</code> if not.</p>
  6988. <h3 id="registering-a-web-resource"><a class="header" href="#registering-a-web-resource">Registering a web resource</a></h3>
  6989. <p>Modules can register web resources onto Synapse's web server using the following module
  6990. API method:</p>
  6991. <pre><code class="language-python">def ModuleApi.register_web_resource(path: str, resource: IResource)
  6992. </code></pre>
  6993. <p>The path is the full absolute path to register the resource at. For example, if you
  6994. register a resource for the path <code>/_synapse/client/my_super_module/say_hello</code>, Synapse
  6995. will serve it at <code>http(s)://[HS_URL]/_synapse/client/my_super_module/say_hello</code>. Note
  6996. that Synapse does not allow registering resources for several sub-paths in the <code>/_matrix</code>
  6997. namespace (such as anything under <code>/_matrix/client</code> for example). It is strongly
  6998. recommended that modules register their web resources under the <code>/_synapse/client</code>
  6999. namespace.</p>
  7000. <p>The provided resource is a Python class that implements Twisted's <a href="https://twistedmatrix.com/documents/current/api/twisted.web.resource.IResource.html">IResource</a>
  7001. interface (such as <a href="https://twistedmatrix.com/documents/current/api/twisted.web.resource.Resource.html">Resource</a>).</p>
  7002. <p>Only one resource can be registered for a given path. If several modules attempt to
  7003. register a resource for the same path, the module that appears first in Synapse's
  7004. configuration file takes priority.</p>
  7005. <p>Modules <strong>must</strong> register their web resources in their <code>__init__</code> method.</p>
  7006. <h3 id="registering-a-callback"><a class="header" href="#registering-a-callback">Registering a callback</a></h3>
  7007. <p>Modules can use Synapse's module API to register callbacks. Callbacks are functions that
  7008. Synapse will call when performing specific actions. Callbacks must be asynchronous, and
  7009. are split in categories. A single module may implement callbacks from multiple categories,
  7010. and is under no obligation to implement all callbacks from the categories it registers
  7011. callbacks for.</p>
  7012. <h4 id="spam-checker-callbacks"><a class="header" href="#spam-checker-callbacks">Spam checker callbacks</a></h4>
  7013. <p>To register one of the callbacks described in this section, a module needs to use the
  7014. module API's <code>register_spam_checker_callbacks</code> method. The callback functions are passed
  7015. to <code>register_spam_checker_callbacks</code> as keyword arguments, with the callback name as the
  7016. argument name and the function as its value. This is demonstrated in the example below.</p>
  7017. <p>The available spam checker callbacks are:</p>
  7018. <pre><code class="language-python">async def check_event_for_spam(event: &quot;synapse.events.EventBase&quot;) -&gt; Union[bool, str]
  7019. </code></pre>
  7020. <p>Called when receiving an event from a client or via federation. The module can return
  7021. either a <code>bool</code> to indicate whether the event must be rejected because of spam, or a <code>str</code>
  7022. to indicate the event must be rejected because of spam and to give a rejection reason to
  7023. forward to clients.</p>
  7024. <pre><code class="language-python">async def user_may_invite(inviter: str, invitee: str, room_id: str) -&gt; bool
  7025. </code></pre>
  7026. <p>Called when processing an invitation. The module must return a <code>bool</code> indicating whether
  7027. the inviter can invite the invitee to the given room. Both inviter and invitee are
  7028. represented by their Matrix user ID (i.e. <code>@alice:example.com</code>).</p>
  7029. <pre><code class="language-python">async def user_may_create_room(user: str) -&gt; bool
  7030. </code></pre>
  7031. <p>Called when processing a room creation request. The module must return a <code>bool</code> indicating
  7032. whether the given user (represented by their Matrix user ID) is allowed to create a room.</p>
  7033. <pre><code class="language-python">async def user_may_create_room_alias(user: str, room_alias: &quot;synapse.types.RoomAlias&quot;) -&gt; bool
  7034. </code></pre>
  7035. <p>Called when trying to associate an alias with an existing room. The module must return a
  7036. <code>bool</code> indicating whether the given user (represented by their Matrix user ID) is allowed
  7037. to set the given alias.</p>
  7038. <pre><code class="language-python">async def user_may_publish_room(user: str, room_id: str) -&gt; bool
  7039. </code></pre>
  7040. <p>Called when trying to publish a room to the homeserver's public rooms directory. The
  7041. module must return a <code>bool</code> indicating whether the given user (represented by their
  7042. Matrix user ID) is allowed to publish the given room.</p>
  7043. <pre><code class="language-python">async def check_username_for_spam(user_profile: Dict[str, str]) -&gt; bool
  7044. </code></pre>
  7045. <p>Called when computing search results in the user directory. The module must return a
  7046. <code>bool</code> indicating whether the given user profile can appear in search results. The profile
  7047. is represented as a dictionary with the following keys:</p>
  7048. <ul>
  7049. <li><code>user_id</code>: The Matrix ID for this user.</li>
  7050. <li><code>display_name</code>: The user's display name.</li>
  7051. <li><code>avatar_url</code>: The <code>mxc://</code> URL to the user's avatar.</li>
  7052. </ul>
  7053. <p>The module is given a copy of the original dictionary, so modifying it from within the
  7054. module cannot modify a user's profile when included in user directory search results.</p>
  7055. <pre><code class="language-python">async def check_registration_for_spam(
  7056. email_threepid: Optional[dict],
  7057. username: Optional[str],
  7058. request_info: Collection[Tuple[str, str]],
  7059. auth_provider_id: Optional[str] = None,
  7060. ) -&gt; &quot;synapse.spam_checker_api.RegistrationBehaviour&quot;
  7061. </code></pre>
  7062. <p>Called when registering a new user. The module must return a <code>RegistrationBehaviour</code>
  7063. indicating whether the registration can go through or must be denied, or whether the user
  7064. may be allowed to register but will be shadow banned.</p>
  7065. <p>The arguments passed to this callback are:</p>
  7066. <ul>
  7067. <li><code>email_threepid</code>: The email address used for registering, if any.</li>
  7068. <li><code>username</code>: The username the user would like to register. Can be <code>None</code>, meaning that
  7069. Synapse will generate one later.</li>
  7070. <li><code>request_info</code>: A collection of tuples, which first item is a user agent, and which
  7071. second item is an IP address. These user agents and IP addresses are the ones that were
  7072. used during the registration process.</li>
  7073. <li><code>auth_provider_id</code>: The identifier of the SSO authentication provider, if any.</li>
  7074. </ul>
  7075. <pre><code class="language-python">async def check_media_file_for_spam(
  7076. file_wrapper: &quot;synapse.rest.media.v1.media_storage.ReadableFileWrapper&quot;,
  7077. file_info: &quot;synapse.rest.media.v1._base.FileInfo&quot;
  7078. ) -&gt; bool
  7079. </code></pre>
  7080. <p>Called when storing a local or remote file. The module must return a boolean indicating
  7081. whether the given file can be stored in the homeserver's media store.</p>
  7082. <h3 id="porting-an-existing-module-that-uses-the-old-interface"><a class="header" href="#porting-an-existing-module-that-uses-the-old-interface">Porting an existing module that uses the old interface</a></h3>
  7083. <p>In order to port a module that uses Synapse's old module interface, its author needs to:</p>
  7084. <ul>
  7085. <li>ensure the module's callbacks are all asynchronous.</li>
  7086. <li>register their callbacks using one or more of the <code>register_[...]_callbacks</code> methods
  7087. from the <code>ModuleApi</code> class in the module's <code>__init__</code> method (see <a href="modules.html#registering-a-callback">this section</a>
  7088. for more info).</li>
  7089. </ul>
  7090. <p>Additionally, if the module is packaged with an additional web resource, the module
  7091. should register this resource in its <code>__init__</code> method using the <code>register_web_resource</code>
  7092. method from the <code>ModuleApi</code> class (see <a href="modules.html#registering-a-web-resource">this section</a> for
  7093. more info).</p>
  7094. <p>The module's author should also update any example in the module's configuration to only
  7095. use the new <code>modules</code> section in Synapse's configuration file (see <a href="modules.html#using-modules">this section</a>
  7096. for more info).</p>
  7097. <h3 id="example"><a class="header" href="#example">Example</a></h3>
  7098. <p>The example below is a module that implements the spam checker callback
  7099. <code>user_may_create_room</code> to deny room creation to user <code>@evilguy:example.com</code>, and registers
  7100. a web resource to the path <code>/_synapse/client/demo/hello</code> that returns a JSON object.</p>
  7101. <pre><code class="language-python">import json
  7102. from twisted.web.resource import Resource
  7103. from twisted.web.server import Request
  7104. from synapse.module_api import ModuleApi
  7105. class DemoResource(Resource):
  7106. def __init__(self, config):
  7107. super(DemoResource, self).__init__()
  7108. self.config = config
  7109. def render_GET(self, request: Request):
  7110. name = request.args.get(b&quot;name&quot;)[0]
  7111. request.setHeader(b&quot;Content-Type&quot;, b&quot;application/json&quot;)
  7112. return json.dumps({&quot;hello&quot;: name})
  7113. class DemoModule:
  7114. def __init__(self, config: dict, api: ModuleApi):
  7115. self.config = config
  7116. self.api = api
  7117. self.api.register_web_resource(
  7118. path=&quot;/_synapse/client/demo/hello&quot;,
  7119. resource=DemoResource(self.config),
  7120. )
  7121. self.api.register_spam_checker_callbacks(
  7122. user_may_create_room=self.user_may_create_room,
  7123. )
  7124. @staticmethod
  7125. def parse_config(config):
  7126. return config
  7127. async def user_may_create_room(self, user: str) -&gt; bool:
  7128. if user == &quot;@evilguy:example.com&quot;:
  7129. return False
  7130. return True
  7131. </code></pre>
  7132. <div style="break-before: page; page-break-before: always;"></div><p><strong>Note: this page of the Synapse documentation is now deprecated. For up to date
  7133. documentation on setting up or writing a spam checker module, please see
  7134. <a href="https://matrix-org.github.io/synapse/develop/modules.html">this page</a>.</strong></p>
  7135. <h1 id="handling-spam-in-synapse"><a class="header" href="#handling-spam-in-synapse">Handling spam in Synapse</a></h1>
  7136. <p>Synapse has support to customize spam checking behavior. It can plug into a
  7137. variety of events and affect how they are presented to users on your homeserver.</p>
  7138. <p>The spam checking behavior is implemented as a Python class, which must be
  7139. able to be imported by the running Synapse.</p>
  7140. <h2 id="python-spam-checker-class"><a class="header" href="#python-spam-checker-class">Python spam checker class</a></h2>
  7141. <p>The Python class is instantiated with two objects:</p>
  7142. <ul>
  7143. <li>Any configuration (see below).</li>
  7144. <li>An instance of <code>synapse.module_api.ModuleApi</code>.</li>
  7145. </ul>
  7146. <p>It then implements methods which return a boolean to alter behavior in Synapse.
  7147. All the methods must be defined.</p>
  7148. <p>There's a generic method for checking every event (<code>check_event_for_spam</code>), as
  7149. well as some specific methods:</p>
  7150. <ul>
  7151. <li><code>user_may_invite</code></li>
  7152. <li><code>user_may_create_room</code></li>
  7153. <li><code>user_may_create_room_alias</code></li>
  7154. <li><code>user_may_publish_room</code></li>
  7155. <li><code>check_username_for_spam</code></li>
  7156. <li><code>check_registration_for_spam</code></li>
  7157. <li><code>check_media_file_for_spam</code></li>
  7158. </ul>
  7159. <p>The details of each of these methods (as well as their inputs and outputs)
  7160. are documented in the <code>synapse.events.spamcheck.SpamChecker</code> class.</p>
  7161. <p>The <code>ModuleApi</code> class provides a way for the custom spam checker class to
  7162. call back into the homeserver internals.</p>
  7163. <p>Additionally, a <code>parse_config</code> method is mandatory and receives the plugin config
  7164. dictionary. After parsing, It must return an object which will be
  7165. passed to <code>__init__</code> later.</p>
  7166. <h3 id="example-1"><a class="header" href="#example-1">Example</a></h3>
  7167. <pre><code class="language-python">from synapse.spam_checker_api import RegistrationBehaviour
  7168. class ExampleSpamChecker:
  7169. def __init__(self, config, api):
  7170. self.config = config
  7171. self.api = api
  7172. @staticmethod
  7173. def parse_config(config):
  7174. return config
  7175. async def check_event_for_spam(self, foo):
  7176. return False # allow all events
  7177. async def user_may_invite(self, inviter_userid, invitee_userid, room_id):
  7178. return True # allow all invites
  7179. async def user_may_create_room(self, userid):
  7180. return True # allow all room creations
  7181. async def user_may_create_room_alias(self, userid, room_alias):
  7182. return True # allow all room aliases
  7183. async def user_may_publish_room(self, userid, room_id):
  7184. return True # allow publishing of all rooms
  7185. async def check_username_for_spam(self, user_profile):
  7186. return False # allow all usernames
  7187. async def check_registration_for_spam(
  7188. self,
  7189. email_threepid,
  7190. username,
  7191. request_info,
  7192. auth_provider_id,
  7193. ):
  7194. return RegistrationBehaviour.ALLOW # allow all registrations
  7195. async def check_media_file_for_spam(self, file_wrapper, file_info):
  7196. return False # allow all media
  7197. </code></pre>
  7198. <h2 id="configuration-2"><a class="header" href="#configuration-2">Configuration</a></h2>
  7199. <p>Modify the <code>spam_checker</code> section of your <code>homeserver.yaml</code> in the following
  7200. manner:</p>
  7201. <p>Create a list entry with the keys <code>module</code> and <code>config</code>.</p>
  7202. <ul>
  7203. <li>
  7204. <p><code>module</code> should point to the fully qualified Python class that implements your
  7205. custom logic, e.g. <code>my_module.ExampleSpamChecker</code>.</p>
  7206. </li>
  7207. <li>
  7208. <p><code>config</code> is a dictionary that gets passed to the spam checker class.</p>
  7209. </li>
  7210. </ul>
  7211. <h3 id="example-2"><a class="header" href="#example-2">Example</a></h3>
  7212. <p>This section might look like:</p>
  7213. <pre><code class="language-yaml">spam_checker:
  7214. - module: my_module.ExampleSpamChecker
  7215. config:
  7216. # Enable or disable a specific option in ExampleSpamChecker.
  7217. my_custom_option: true
  7218. </code></pre>
  7219. <p>More spam checkers can be added in tandem by appending more items to the list. An
  7220. action is blocked when at least one of the configured spam checkers flags it.</p>
  7221. <h2 id="examples"><a class="header" href="#examples">Examples</a></h2>
  7222. <p>The <a href="https://github.com/matrix-org/mjolnir">Mjolnir</a> project is a full fledged
  7223. example using the Synapse spam checking API, including a bot for dynamic
  7224. configuration.</p>
  7225. <div style="break-before: page; page-break-before: always;"></div><h1 id="presence-router-module"><a class="header" href="#presence-router-module">Presence Router Module</a></h1>
  7226. <p>Synapse supports configuring a module that can specify additional users
  7227. (local or remote) to should receive certain presence updates from local
  7228. users.</p>
  7229. <p>Note that routing presence via Application Service transactions is not
  7230. currently supported.</p>
  7231. <p>The presence routing module is implemented as a Python class, which will
  7232. be imported by the running Synapse.</p>
  7233. <h2 id="python-presence-router-class"><a class="header" href="#python-presence-router-class">Python Presence Router Class</a></h2>
  7234. <p>The Python class is instantiated with two objects:</p>
  7235. <ul>
  7236. <li>A configuration object of some type (see below).</li>
  7237. <li>An instance of <code>synapse.module_api.ModuleApi</code>.</li>
  7238. </ul>
  7239. <p>It then implements methods related to presence routing.</p>
  7240. <p>Note that one method of <code>ModuleApi</code> that may be useful is:</p>
  7241. <pre><code class="language-python">async def ModuleApi.send_local_online_presence_to(users: Iterable[str]) -&gt; None
  7242. </code></pre>
  7243. <p>which can be given a list of local or remote MXIDs to broadcast known, online user
  7244. presence to (for those users that the receiving user is considered interested in).
  7245. It does not include state for users who are currently offline, and it can only be
  7246. called on workers that support sending federation. Additionally, this method must
  7247. only be called from the process that has been configured to write to the
  7248. the <a href="workers.html#stream-writers">presence stream</a>.
  7249. By default, this is the main process, but another worker can be configured to do
  7250. so.</p>
  7251. <h3 id="module-structure"><a class="header" href="#module-structure">Module structure</a></h3>
  7252. <p>Below is a list of possible methods that can be implemented, and whether they are
  7253. required.</p>
  7254. <h4 id="parse_config"><a class="header" href="#parse_config"><code>parse_config</code></a></h4>
  7255. <pre><code class="language-python">def parse_config(config_dict: dict) -&gt; Any
  7256. </code></pre>
  7257. <p><strong>Required.</strong> A static method that is passed a dictionary of config options, and
  7258. should return a validated config object. This method is described further in
  7259. <a href="presence_router_module.html#configuration">Configuration</a>.</p>
  7260. <h4 id="get_users_for_states"><a class="header" href="#get_users_for_states"><code>get_users_for_states</code></a></h4>
  7261. <pre><code class="language-python">async def get_users_for_states(
  7262. self,
  7263. state_updates: Iterable[UserPresenceState],
  7264. ) -&gt; Dict[str, Set[UserPresenceState]]:
  7265. </code></pre>
  7266. <p><strong>Required.</strong> An asynchronous method that is passed an iterable of user presence
  7267. state. This method can determine whether a given presence update should be sent to certain
  7268. users. It does this by returning a dictionary with keys representing local or remote
  7269. Matrix User IDs, and values being a python set
  7270. of <code>synapse.handlers.presence.UserPresenceState</code> instances.</p>
  7271. <p>Synapse will then attempt to send the specified presence updates to each user when
  7272. possible.</p>
  7273. <h4 id="get_interested_users"><a class="header" href="#get_interested_users"><code>get_interested_users</code></a></h4>
  7274. <pre><code class="language-python">async def get_interested_users(self, user_id: str) -&gt; Union[Set[str], str]
  7275. </code></pre>
  7276. <p><strong>Required.</strong> An asynchronous method that is passed a single Matrix User ID. This
  7277. method is expected to return the users that the passed in user may be interested in the
  7278. presence of. Returned users may be local or remote. The presence routed as a result of
  7279. what this method returns is sent in addition to the updates already sent between users
  7280. that share a room together. Presence updates are deduplicated.</p>
  7281. <p>This method should return a python set of Matrix User IDs, or the object
  7282. <code>synapse.events.presence_router.PresenceRouter.ALL_USERS</code> to indicate that the passed
  7283. user should receive presence information for <em>all</em> known users.</p>
  7284. <p>For clarity, if the user <code>@alice:example.org</code> is passed to this method, and the Set
  7285. <code>{&quot;@bob:example.com&quot;, &quot;@charlie:somewhere.org&quot;}</code> is returned, this signifies that Alice
  7286. should receive presence updates sent by Bob and Charlie, regardless of whether these
  7287. users share a room.</p>
  7288. <h3 id="example-3"><a class="header" href="#example-3">Example</a></h3>
  7289. <p>Below is an example implementation of a presence router class.</p>
  7290. <pre><code class="language-python">from typing import Dict, Iterable, Set, Union
  7291. from synapse.events.presence_router import PresenceRouter
  7292. from synapse.handlers.presence import UserPresenceState
  7293. from synapse.module_api import ModuleApi
  7294. class PresenceRouterConfig:
  7295. def __init__(self):
  7296. # Config options with their defaults
  7297. # A list of users to always send all user presence updates to
  7298. self.always_send_to_users = [] # type: List[str]
  7299. # A list of users to ignore presence updates for. Does not affect
  7300. # shared-room presence relationships
  7301. self.blacklisted_users = [] # type: List[str]
  7302. class ExamplePresenceRouter:
  7303. &quot;&quot;&quot;An example implementation of synapse.presence_router.PresenceRouter.
  7304. Supports routing all presence to a configured set of users, or a subset
  7305. of presence from certain users to members of certain rooms.
  7306. Args:
  7307. config: A configuration object.
  7308. module_api: An instance of Synapse's ModuleApi.
  7309. &quot;&quot;&quot;
  7310. def __init__(self, config: PresenceRouterConfig, module_api: ModuleApi):
  7311. self._config = config
  7312. self._module_api = module_api
  7313. @staticmethod
  7314. def parse_config(config_dict: dict) -&gt; PresenceRouterConfig:
  7315. &quot;&quot;&quot;Parse a configuration dictionary from the homeserver config, do
  7316. some validation and return a typed PresenceRouterConfig.
  7317. Args:
  7318. config_dict: The configuration dictionary.
  7319. Returns:
  7320. A validated config object.
  7321. &quot;&quot;&quot;
  7322. # Initialise a typed config object
  7323. config = PresenceRouterConfig()
  7324. always_send_to_users = config_dict.get(&quot;always_send_to_users&quot;)
  7325. blacklisted_users = config_dict.get(&quot;blacklisted_users&quot;)
  7326. # Do some validation of config options... otherwise raise a
  7327. # synapse.config.ConfigError.
  7328. config.always_send_to_users = always_send_to_users
  7329. config.blacklisted_users = blacklisted_users
  7330. return config
  7331. async def get_users_for_states(
  7332. self,
  7333. state_updates: Iterable[UserPresenceState],
  7334. ) -&gt; Dict[str, Set[UserPresenceState]]:
  7335. &quot;&quot;&quot;Given an iterable of user presence updates, determine where each one
  7336. needs to go. Returned results will not affect presence updates that are
  7337. sent between users who share a room.
  7338. Args:
  7339. state_updates: An iterable of user presence state updates.
  7340. Returns:
  7341. A dictionary of user_id -&gt; set of UserPresenceState that the user should
  7342. receive.
  7343. &quot;&quot;&quot;
  7344. destination_users = {} # type: Dict[str, Set[UserPresenceState]
  7345. # Ignore any updates for blacklisted users
  7346. desired_updates = set()
  7347. for update in state_updates:
  7348. if update.state_key not in self._config.blacklisted_users:
  7349. desired_updates.add(update)
  7350. # Send all presence updates to specific users
  7351. for user_id in self._config.always_send_to_users:
  7352. destination_users[user_id] = desired_updates
  7353. return destination_users
  7354. async def get_interested_users(
  7355. self,
  7356. user_id: str,
  7357. ) -&gt; Union[Set[str], PresenceRouter.ALL_USERS]:
  7358. &quot;&quot;&quot;
  7359. Retrieve a list of users that `user_id` is interested in receiving the
  7360. presence of. This will be in addition to those they share a room with.
  7361. Optionally, the object PresenceRouter.ALL_USERS can be returned to indicate
  7362. that this user should receive all incoming local and remote presence updates.
  7363. Note that this method will only be called for local users.
  7364. Args:
  7365. user_id: A user requesting presence updates.
  7366. Returns:
  7367. A set of user IDs to return additional presence updates for, or
  7368. PresenceRouter.ALL_USERS to return presence updates for all other users.
  7369. &quot;&quot;&quot;
  7370. if user_id in self._config.always_send_to_users:
  7371. return PresenceRouter.ALL_USERS
  7372. return set()
  7373. </code></pre>
  7374. <h4 id="a-note-on-get_users_for_states-and-get_interested_users"><a class="header" href="#a-note-on-get_users_for_states-and-get_interested_users">A note on <code>get_users_for_states</code> and <code>get_interested_users</code></a></h4>
  7375. <p>Both of these methods are effectively two different sides of the same coin. The logic
  7376. regarding which users should receive updates for other users should be the same
  7377. between them.</p>
  7378. <p><code>get_users_for_states</code> is called when presence updates come in from either federation
  7379. or local users, and is used to either direct local presence to remote users, or to
  7380. wake up the sync streams of local users to collect remote presence.</p>
  7381. <p>In contrast, <code>get_interested_users</code> is used to determine the users that presence should
  7382. be fetched for when a local user is syncing. This presence is then retrieved, before
  7383. being fed through <code>get_users_for_states</code> once again, with only the syncing user's
  7384. routing information pulled from the resulting dictionary.</p>
  7385. <p>Their routing logic should thus line up, else you may run into unintended behaviour.</p>
  7386. <h2 id="configuration-3"><a class="header" href="#configuration-3">Configuration</a></h2>
  7387. <p>Once you've crafted your module and installed it into the same Python environment as
  7388. Synapse, amend your homeserver config file with the following.</p>
  7389. <pre><code class="language-yaml">presence:
  7390. enabled: true
  7391. presence_router:
  7392. module: my_module.ExamplePresenceRouter
  7393. config:
  7394. # Any configuration options for your module. The below is an example.
  7395. # of setting options for ExamplePresenceRouter.
  7396. always_send_to_users: [&quot;@presence_gobbler:example.org&quot;]
  7397. blacklisted_users:
  7398. - &quot;@alice:example.com&quot;
  7399. - &quot;@bob:example.com&quot;
  7400. ...
  7401. </code></pre>
  7402. <p>The contents of <code>config</code> will be passed as a Python dictionary to the static
  7403. <code>parse_config</code> method of your class. The object returned by this method will
  7404. then be passed to the <code>__init__</code> method of your module as <code>config</code>.</p>
  7405. <div style="break-before: page; page-break-before: always;"></div><h1 id="scaling-synapse-via-workers"><a class="header" href="#scaling-synapse-via-workers">Scaling synapse via workers</a></h1>
  7406. <p>For small instances it recommended to run Synapse in the default monolith mode.
  7407. For larger instances where performance is a concern it can be helpful to split
  7408. out functionality into multiple separate python processes. These processes are
  7409. called 'workers', and are (eventually) intended to scale horizontally
  7410. independently.</p>
  7411. <p>Synapse's worker support is under active development and subject to change as
  7412. we attempt to rapidly scale ever larger Synapse instances. However we are
  7413. documenting it here to help admins needing a highly scalable Synapse instance
  7414. similar to the one running <code>matrix.org</code>.</p>
  7415. <p>All processes continue to share the same database instance, and as such,
  7416. workers only work with PostgreSQL-based Synapse deployments. SQLite should only
  7417. be used for demo purposes and any admin considering workers should already be
  7418. running PostgreSQL.</p>
  7419. <p>See also <a href="https://matrix.org/blog/2020/11/03/how-we-fixed-synapses-scalability">Matrix.org blog post</a>
  7420. for a higher level overview.</p>
  7421. <h2 id="main-processworker-communication"><a class="header" href="#main-processworker-communication">Main process/worker communication</a></h2>
  7422. <p>The processes communicate with each other via a Synapse-specific protocol called
  7423. 'replication' (analogous to MySQL- or Postgres-style database replication) which
  7424. feeds streams of newly written data between processes so they can be kept in
  7425. sync with the database state.</p>
  7426. <p>When configured to do so, Synapse uses a
  7427. <a href="https://redis.io/topics/pubsub">Redis pub/sub channel</a> to send the replication
  7428. stream between all configured Synapse processes. Additionally, processes may
  7429. make HTTP requests to each other, primarily for operations which need to wait
  7430. for a reply ─ such as sending an event.</p>
  7431. <p>Redis support was added in v1.13.0 with it becoming the recommended method in
  7432. v1.18.0. It replaced the old direct TCP connections (which is deprecated as of
  7433. v1.18.0) to the main process. With Redis, rather than all the workers connecting
  7434. to the main process, all the workers and the main process connect to Redis,
  7435. which relays replication commands between processes. This can give a significant
  7436. cpu saving on the main process and will be a prerequisite for upcoming
  7437. performance improvements.</p>
  7438. <p>If Redis support is enabled Synapse will use it as a shared cache, as well as a
  7439. pub/sub mechanism.</p>
  7440. <p>See the <a href="workers.html#architectural-diagram">Architectural diagram</a> section at the end for
  7441. a visualisation of what this looks like.</p>
  7442. <h2 id="setting-up-workers"><a class="header" href="#setting-up-workers">Setting up workers</a></h2>
  7443. <p>A Redis server is required to manage the communication between the processes.
  7444. The Redis server should be installed following the normal procedure for your
  7445. distribution (e.g. <code>apt install redis-server</code> on Debian). It is safe to use an
  7446. existing Redis deployment if you have one.</p>
  7447. <p>Once installed, check that Redis is running and accessible from the host running
  7448. Synapse, for example by executing <code>echo PING | nc -q1 localhost 6379</code> and seeing
  7449. a response of <code>+PONG</code>.</p>
  7450. <p>The appropriate dependencies must also be installed for Synapse. If using a
  7451. virtualenv, these can be installed with:</p>
  7452. <pre><code class="language-sh">pip install &quot;matrix-synapse[redis]&quot;
  7453. </code></pre>
  7454. <p>Note that these dependencies are included when synapse is installed with <code>pip install matrix-synapse[all]</code>. They are also included in the debian packages from
  7455. <code>matrix.org</code> and in the docker images at
  7456. https://hub.docker.com/r/matrixdotorg/synapse/.</p>
  7457. <p>To make effective use of the workers, you will need to configure an HTTP
  7458. reverse-proxy such as nginx or haproxy, which will direct incoming requests to
  7459. the correct worker, or to the main synapse instance. See
  7460. <a href="reverse_proxy.html">reverse_proxy.md</a> for information on setting up a reverse
  7461. proxy.</p>
  7462. <p>When using workers, each worker process has its own configuration file which
  7463. contains settings specific to that worker, such as the HTTP listener that it
  7464. provides (if any), logging configuration, etc.</p>
  7465. <p>Normally, the worker processes are configured to read from a shared
  7466. configuration file as well as the worker-specific configuration files. This
  7467. makes it easier to keep common configuration settings synchronised across all
  7468. the processes.</p>
  7469. <p>The main process is somewhat special in this respect: it does not normally
  7470. need its own configuration file and can take all of its configuration from the
  7471. shared configuration file.</p>
  7472. <h3 id="shared-configuration"><a class="header" href="#shared-configuration">Shared configuration</a></h3>
  7473. <p>Normally, only a couple of changes are needed to make an existing configuration
  7474. file suitable for use with workers. First, you need to enable an &quot;HTTP replication
  7475. listener&quot; for the main process; and secondly, you need to enable redis-based
  7476. replication. Optionally, a shared secret can be used to authenticate HTTP
  7477. traffic between workers. For example:</p>
  7478. <pre><code class="language-yaml"># extend the existing `listeners` section. This defines the ports that the
  7479. # main process will listen on.
  7480. listeners:
  7481. # The HTTP replication port
  7482. - port: 9093
  7483. bind_address: '127.0.0.1'
  7484. type: http
  7485. resources:
  7486. - names: [replication]
  7487. # Add a random shared secret to authenticate traffic.
  7488. worker_replication_secret: &quot;&quot;
  7489. redis:
  7490. enabled: true
  7491. </code></pre>
  7492. <p>See the sample config for the full documentation of each option.</p>
  7493. <p>Under <strong>no circumstances</strong> should the replication listener be exposed to the
  7494. public internet; it has no authentication and is unencrypted.</p>
  7495. <h3 id="worker-configuration"><a class="header" href="#worker-configuration">Worker configuration</a></h3>
  7496. <p>In the config file for each worker, you must specify the type of worker
  7497. application (<code>worker_app</code>), and you should specify a unique name for the worker
  7498. (<code>worker_name</code>). The currently available worker applications are listed below.
  7499. You must also specify the HTTP replication endpoint that it should talk to on
  7500. the main synapse process. <code>worker_replication_host</code> should specify the host of
  7501. the main synapse and <code>worker_replication_http_port</code> should point to the HTTP
  7502. replication port. If the worker will handle HTTP requests then the
  7503. <code>worker_listeners</code> option should be set with a <code>http</code> listener, in the same way
  7504. as the <code>listeners</code> option in the shared config.</p>
  7505. <p>For example:</p>
  7506. <pre><code class="language-yaml">worker_app: synapse.app.generic_worker
  7507. worker_name: worker1
  7508. # The replication listener on the main synapse process.
  7509. worker_replication_host: 127.0.0.1
  7510. worker_replication_http_port: 9093
  7511. worker_listeners:
  7512. - type: http
  7513. port: 8083
  7514. resources:
  7515. - names:
  7516. - client
  7517. - federation
  7518. worker_log_config: /home/matrix/synapse/config/worker1_log_config.yaml
  7519. </code></pre>
  7520. <p>...is a full configuration for a generic worker instance, which will expose a
  7521. plain HTTP endpoint on port 8083 separately serving various endpoints, e.g.
  7522. <code>/sync</code>, which are listed below.</p>
  7523. <p>Obviously you should configure your reverse-proxy to route the relevant
  7524. endpoints to the worker (<code>localhost:8083</code> in the above example).</p>
  7525. <h3 id="running-synapse-with-workers"><a class="header" href="#running-synapse-with-workers">Running Synapse with workers</a></h3>
  7526. <p>Finally, you need to start your worker processes. This can be done with either
  7527. <code>synctl</code> or your distribution's preferred service manager such as <code>systemd</code>. We
  7528. recommend the use of <code>systemd</code> where available: for information on setting up
  7529. <code>systemd</code> to start synapse workers, see
  7530. <a href="systemd-with-workers">systemd-with-workers</a>. To use <code>synctl</code>, see
  7531. <a href="synctl_workers.html">synctl_workers.md</a>.</p>
  7532. <h2 id="available-worker-applications"><a class="header" href="#available-worker-applications">Available worker applications</a></h2>
  7533. <h3 id="synapseappgeneric_worker"><a class="header" href="#synapseappgeneric_worker"><code>synapse.app.generic_worker</code></a></h3>
  7534. <p>This worker can handle API requests matching the following regular
  7535. expressions:</p>
  7536. <pre><code># Sync requests
  7537. ^/_matrix/client/(v2_alpha|r0)/sync$
  7538. ^/_matrix/client/(api/v1|v2_alpha|r0)/events$
  7539. ^/_matrix/client/(api/v1|r0)/initialSync$
  7540. ^/_matrix/client/(api/v1|r0)/rooms/[^/]+/initialSync$
  7541. # Federation requests
  7542. ^/_matrix/federation/v1/event/
  7543. ^/_matrix/federation/v1/state/
  7544. ^/_matrix/federation/v1/state_ids/
  7545. ^/_matrix/federation/v1/backfill/
  7546. ^/_matrix/federation/v1/get_missing_events/
  7547. ^/_matrix/federation/v1/publicRooms
  7548. ^/_matrix/federation/v1/query/
  7549. ^/_matrix/federation/v1/make_join/
  7550. ^/_matrix/federation/v1/make_leave/
  7551. ^/_matrix/federation/v1/send_join/
  7552. ^/_matrix/federation/v2/send_join/
  7553. ^/_matrix/federation/v1/send_leave/
  7554. ^/_matrix/federation/v2/send_leave/
  7555. ^/_matrix/federation/v1/invite/
  7556. ^/_matrix/federation/v2/invite/
  7557. ^/_matrix/federation/v1/query_auth/
  7558. ^/_matrix/federation/v1/event_auth/
  7559. ^/_matrix/federation/v1/exchange_third_party_invite/
  7560. ^/_matrix/federation/v1/user/devices/
  7561. ^/_matrix/federation/v1/get_groups_publicised$
  7562. ^/_matrix/key/v2/query
  7563. # Inbound federation transaction request
  7564. ^/_matrix/federation/v1/send/
  7565. # Client API requests
  7566. ^/_matrix/client/(api/v1|r0|unstable)/publicRooms$
  7567. ^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/joined_members$
  7568. ^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/context/.*$
  7569. ^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/members$
  7570. ^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/state$
  7571. ^/_matrix/client/(api/v1|r0|unstable)/account/3pid$
  7572. ^/_matrix/client/(api/v1|r0|unstable)/devices$
  7573. ^/_matrix/client/(api/v1|r0|unstable)/keys/query$
  7574. ^/_matrix/client/(api/v1|r0|unstable)/keys/changes$
  7575. ^/_matrix/client/versions$
  7576. ^/_matrix/client/(api/v1|r0|unstable)/voip/turnServer$
  7577. ^/_matrix/client/(api/v1|r0|unstable)/joined_groups$
  7578. ^/_matrix/client/(api/v1|r0|unstable)/publicised_groups$
  7579. ^/_matrix/client/(api/v1|r0|unstable)/publicised_groups/
  7580. ^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/event/
  7581. ^/_matrix/client/(api/v1|r0|unstable)/joined_rooms$
  7582. ^/_matrix/client/(api/v1|r0|unstable)/search$
  7583. # Registration/login requests
  7584. ^/_matrix/client/(api/v1|r0|unstable)/login$
  7585. ^/_matrix/client/(r0|unstable)/register$
  7586. # Event sending requests
  7587. ^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/redact
  7588. ^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/send
  7589. ^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/state/
  7590. ^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/(join|invite|leave|ban|unban|kick)$
  7591. ^/_matrix/client/(api/v1|r0|unstable)/join/
  7592. ^/_matrix/client/(api/v1|r0|unstable)/profile/
  7593. </code></pre>
  7594. <p>Additionally, the following REST endpoints can be handled for GET requests:</p>
  7595. <pre><code>^/_matrix/federation/v1/groups/
  7596. </code></pre>
  7597. <p>Pagination requests can also be handled, but all requests for a given
  7598. room must be routed to the same instance. Additionally, care must be taken to
  7599. ensure that the purge history admin API is not used while pagination requests
  7600. for the room are in flight:</p>
  7601. <pre><code>^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/messages$
  7602. </code></pre>
  7603. <p>Additionally, the following endpoints should be included if Synapse is configured
  7604. to use SSO (you only need to include the ones for whichever SSO provider you're
  7605. using):</p>
  7606. <pre><code># for all SSO providers
  7607. ^/_matrix/client/(api/v1|r0|unstable)/login/sso/redirect
  7608. ^/_synapse/client/pick_idp$
  7609. ^/_synapse/client/pick_username
  7610. ^/_synapse/client/new_user_consent$
  7611. ^/_synapse/client/sso_register$
  7612. # OpenID Connect requests.
  7613. ^/_synapse/client/oidc/callback$
  7614. # SAML requests.
  7615. ^/_synapse/client/saml2/authn_response$
  7616. # CAS requests.
  7617. ^/_matrix/client/(api/v1|r0|unstable)/login/cas/ticket$
  7618. </code></pre>
  7619. <p>Ensure that all SSO logins go to a single process.
  7620. For multiple workers not handling the SSO endpoints properly, see
  7621. <a href="https://github.com/matrix-org/synapse/issues/7530">#7530</a> and
  7622. <a href="https://github.com/matrix-org/synapse/issues/9427">#9427</a>.</p>
  7623. <p>Note that a HTTP listener with <code>client</code> and <code>federation</code> resources must be
  7624. configured in the <code>worker_listeners</code> option in the worker config.</p>
  7625. <h4 id="load-balancing"><a class="header" href="#load-balancing">Load balancing</a></h4>
  7626. <p>It is possible to run multiple instances of this worker app, with incoming requests
  7627. being load-balanced between them by the reverse-proxy. However, different endpoints
  7628. have different characteristics and so admins
  7629. may wish to run multiple groups of workers handling different endpoints so that
  7630. load balancing can be done in different ways.</p>
  7631. <p>For <code>/sync</code> and <code>/initialSync</code> requests it will be more efficient if all
  7632. requests from a particular user are routed to a single instance. Extracting a
  7633. user ID from the access token or <code>Authorization</code> header is currently left as an
  7634. exercise for the reader. Admins may additionally wish to separate out <code>/sync</code>
  7635. requests that have a <code>since</code> query parameter from those that don't (and
  7636. <code>/initialSync</code>), as requests that don't are known as &quot;initial sync&quot; that happens
  7637. when a user logs in on a new device and can be <em>very</em> resource intensive, so
  7638. isolating these requests will stop them from interfering with other users ongoing
  7639. syncs.</p>
  7640. <p>Federation and client requests can be balanced via simple round robin.</p>
  7641. <p>The inbound federation transaction request <code>^/_matrix/federation/v1/send/</code>
  7642. should be balanced by source IP so that transactions from the same remote server
  7643. go to the same process.</p>
  7644. <p>Registration/login requests can be handled separately purely to help ensure that
  7645. unexpected load doesn't affect new logins and sign ups.</p>
  7646. <p>Finally, event sending requests can be balanced by the room ID in the URI (or
  7647. the full URI, or even just round robin), the room ID is the path component after
  7648. <code>/rooms/</code>. If there is a large bridge connected that is sending or may send lots
  7649. of events, then a dedicated set of workers can be provisioned to limit the
  7650. effects of bursts of events from that bridge on events sent by normal users.</p>
  7651. <h4 id="stream-writers"><a class="header" href="#stream-writers">Stream writers</a></h4>
  7652. <p>Additionally, there is <em>experimental</em> support for moving writing of specific
  7653. streams (such as events) off of the main process to a particular worker. (This
  7654. is only supported with Redis-based replication.)</p>
  7655. <p>Currently supported streams are <code>events</code> and <code>typing</code>.</p>
  7656. <p>To enable this, the worker must have a HTTP replication listener configured,
  7657. have a <code>worker_name</code> and be listed in the <code>instance_map</code> config. For example to
  7658. move event persistence off to a dedicated worker, the shared configuration would
  7659. include:</p>
  7660. <pre><code class="language-yaml">instance_map:
  7661. event_persister1:
  7662. host: localhost
  7663. port: 8034
  7664. stream_writers:
  7665. events: event_persister1
  7666. </code></pre>
  7667. <p>The <code>events</code> stream also experimentally supports having multiple writers, where
  7668. work is sharded between them by room ID. Note that you <em>must</em> restart all worker
  7669. instances when adding or removing event persisters. An example <code>stream_writers</code>
  7670. configuration with multiple writers:</p>
  7671. <pre><code class="language-yaml">stream_writers:
  7672. events:
  7673. - event_persister1
  7674. - event_persister2
  7675. </code></pre>
  7676. <h4 id="background-tasks"><a class="header" href="#background-tasks">Background tasks</a></h4>
  7677. <p>There is also <em>experimental</em> support for moving background tasks to a separate
  7678. worker. Background tasks are run periodically or started via replication. Exactly
  7679. which tasks are configured to run depends on your Synapse configuration (e.g. if
  7680. stats is enabled).</p>
  7681. <p>To enable this, the worker must have a <code>worker_name</code> and can be configured to run
  7682. background tasks. For example, to move background tasks to a dedicated worker,
  7683. the shared configuration would include:</p>
  7684. <pre><code class="language-yaml">run_background_tasks_on: background_worker
  7685. </code></pre>
  7686. <p>You might also wish to investigate the <code>update_user_directory</code> and
  7687. <code>media_instance_running_background_jobs</code> settings.</p>
  7688. <h3 id="synapseapppusher"><a class="header" href="#synapseapppusher"><code>synapse.app.pusher</code></a></h3>
  7689. <p>Handles sending push notifications to sygnal and email. Doesn't handle any
  7690. REST endpoints itself, but you should set <code>start_pushers: False</code> in the
  7691. shared configuration file to stop the main synapse sending push notifications.</p>
  7692. <p>To run multiple instances at once the <code>pusher_instances</code> option should list all
  7693. pusher instances by their worker name, e.g.:</p>
  7694. <pre><code class="language-yaml">pusher_instances:
  7695. - pusher_worker1
  7696. - pusher_worker2
  7697. </code></pre>
  7698. <h3 id="synapseappappservice"><a class="header" href="#synapseappappservice"><code>synapse.app.appservice</code></a></h3>
  7699. <p>Handles sending output traffic to Application Services. Doesn't handle any
  7700. REST endpoints itself, but you should set <code>notify_appservices: False</code> in the
  7701. shared configuration file to stop the main synapse sending appservice notifications.</p>
  7702. <p>Note this worker cannot be load-balanced: only one instance should be active.</p>
  7703. <h3 id="synapseappfederation_sender"><a class="header" href="#synapseappfederation_sender"><code>synapse.app.federation_sender</code></a></h3>
  7704. <p>Handles sending federation traffic to other servers. Doesn't handle any
  7705. REST endpoints itself, but you should set <code>send_federation: False</code> in the
  7706. shared configuration file to stop the main synapse sending this traffic.</p>
  7707. <p>If running multiple federation senders then you must list each
  7708. instance in the <code>federation_sender_instances</code> option by their <code>worker_name</code>.
  7709. All instances must be stopped and started when adding or removing instances.
  7710. For example:</p>
  7711. <pre><code class="language-yaml">federation_sender_instances:
  7712. - federation_sender1
  7713. - federation_sender2
  7714. </code></pre>
  7715. <h3 id="synapseappmedia_repository"><a class="header" href="#synapseappmedia_repository"><code>synapse.app.media_repository</code></a></h3>
  7716. <p>Handles the media repository. It can handle all endpoints starting with:</p>
  7717. <pre><code>/_matrix/media/
  7718. </code></pre>
  7719. <p>... and the following regular expressions matching media-specific administration APIs:</p>
  7720. <pre><code>^/_synapse/admin/v1/purge_media_cache$
  7721. ^/_synapse/admin/v1/room/.*/media.*$
  7722. ^/_synapse/admin/v1/user/.*/media.*$
  7723. ^/_synapse/admin/v1/media/.*$
  7724. ^/_synapse/admin/v1/quarantine_media/.*$
  7725. </code></pre>
  7726. <p>You should also set <code>enable_media_repo: False</code> in the shared configuration
  7727. file to stop the main synapse running background jobs related to managing the
  7728. media repository.</p>
  7729. <p>In the <code>media_repository</code> worker configuration file, configure the http listener to
  7730. expose the <code>media</code> resource. For example:</p>
  7731. <pre><code class="language-yaml"> worker_listeners:
  7732. - type: http
  7733. port: 8085
  7734. resources:
  7735. - names:
  7736. - media
  7737. </code></pre>
  7738. <p>Note that if running multiple media repositories they must be on the same server
  7739. and you must configure a single instance to run the background tasks, e.g.:</p>
  7740. <pre><code class="language-yaml"> media_instance_running_background_jobs: &quot;media-repository-1&quot;
  7741. </code></pre>
  7742. <p>Note that if a reverse proxy is used , then <code>/_matrix/media/</code> must be routed for both inbound client and federation requests (if they are handled separately).</p>
  7743. <h3 id="synapseappuser_dir"><a class="header" href="#synapseappuser_dir"><code>synapse.app.user_dir</code></a></h3>
  7744. <p>Handles searches in the user directory. It can handle REST endpoints matching
  7745. the following regular expressions:</p>
  7746. <pre><code>^/_matrix/client/(api/v1|r0|unstable)/user_directory/search$
  7747. </code></pre>
  7748. <p>When using this worker you must also set <code>update_user_directory: False</code> in the
  7749. shared configuration file to stop the main synapse running background
  7750. jobs related to updating the user directory.</p>
  7751. <h3 id="synapseappfrontend_proxy"><a class="header" href="#synapseappfrontend_proxy"><code>synapse.app.frontend_proxy</code></a></h3>
  7752. <p>Proxies some frequently-requested client endpoints to add caching and remove
  7753. load from the main synapse. It can handle REST endpoints matching the following
  7754. regular expressions:</p>
  7755. <pre><code>^/_matrix/client/(api/v1|r0|unstable)/keys/upload
  7756. </code></pre>
  7757. <p>If <code>use_presence</code> is False in the homeserver config, it can also handle REST
  7758. endpoints matching the following regular expressions:</p>
  7759. <pre><code>^/_matrix/client/(api/v1|r0|unstable)/presence/[^/]+/status
  7760. </code></pre>
  7761. <p>This &quot;stub&quot; presence handler will pass through <code>GET</code> request but make the
  7762. <code>PUT</code> effectively a no-op.</p>
  7763. <p>It will proxy any requests it cannot handle to the main synapse instance. It
  7764. must therefore be configured with the location of the main instance, via
  7765. the <code>worker_main_http_uri</code> setting in the <code>frontend_proxy</code> worker configuration
  7766. file. For example:</p>
  7767. <pre><code>worker_main_http_uri: http://127.0.0.1:8008
  7768. </code></pre>
  7769. <h3 id="historical-apps"><a class="header" href="#historical-apps">Historical apps</a></h3>
  7770. <p><em>Note:</em> Historically there used to be more apps, however they have been
  7771. amalgamated into a single <code>synapse.app.generic_worker</code> app. The remaining apps
  7772. are ones that do specific processing unrelated to requests, e.g. the <code>pusher</code>
  7773. that handles sending out push notifications for new events. The intention is for
  7774. all these to be folded into the <code>generic_worker</code> app and to use config to define
  7775. which processes handle the various proccessing such as push notifications.</p>
  7776. <h2 id="migration-from-old-config"><a class="header" href="#migration-from-old-config">Migration from old config</a></h2>
  7777. <p>There are two main independent changes that have been made: introducing Redis
  7778. support and merging apps into <code>synapse.app.generic_worker</code>. Both these changes
  7779. are backwards compatible and so no changes to the config are required, however
  7780. server admins are encouraged to plan to migrate to Redis as the old style direct
  7781. TCP replication config is deprecated.</p>
  7782. <p>To migrate to Redis add the <code>redis</code> config as above, and optionally remove the
  7783. TCP <code>replication</code> listener from master and <code>worker_replication_port</code> from worker
  7784. config.</p>
  7785. <p>To migrate apps to use <code>synapse.app.generic_worker</code> simply update the
  7786. <code>worker_app</code> option in the worker configs, and where worker are started (e.g.
  7787. in systemd service files, but not required for synctl).</p>
  7788. <h2 id="architectural-diagram"><a class="header" href="#architectural-diagram">Architectural diagram</a></h2>
  7789. <p>The following shows an example setup using Redis and a reverse proxy:</p>
  7790. <pre><code> Clients &amp; Federation
  7791. |
  7792. v
  7793. +-----------+
  7794. | |
  7795. | Reverse |
  7796. | Proxy |
  7797. | |
  7798. +-----------+
  7799. | | |
  7800. | | | HTTP requests
  7801. +-------------------+ | +-----------+
  7802. | +---+ |
  7803. | | |
  7804. v v v
  7805. +--------------+ +--------------+ +--------------+ +--------------+
  7806. | Main | | Generic | | Generic | | Event |
  7807. | Process | | Worker 1 | | Worker 2 | | Persister |
  7808. +--------------+ +--------------+ +--------------+ +--------------+
  7809. ^ ^ | ^ | | ^ | ^ ^
  7810. | | | | | | | | | |
  7811. | | | | | HTTP | | | | |
  7812. | +----------+&lt;--|---|---------+ | | | |
  7813. | | +-------------|--&gt;+----------+ |
  7814. | | | |
  7815. | | | |
  7816. v v v v
  7817. ====================================================================
  7818. Redis pub/sub channel
  7819. </code></pre>
  7820. <div style="break-before: page; page-break-before: always;"></div><h3 id="using-synctl-with-workers"><a class="header" href="#using-synctl-with-workers">Using synctl with workers</a></h3>
  7821. <p>If you want to use <code>synctl</code> to manage your synapse processes, you will need to
  7822. create an an additional configuration file for the main synapse process. That
  7823. configuration should look like this:</p>
  7824. <pre><code class="language-yaml">worker_app: synapse.app.homeserver
  7825. </code></pre>
  7826. <p>Additionally, each worker app must be configured with the name of a &quot;pid file&quot;,
  7827. to which it will write its process ID when it starts. For example, for a
  7828. synchrotron, you might write:</p>
  7829. <pre><code class="language-yaml">worker_pid_file: /home/matrix/synapse/worker1.pid
  7830. </code></pre>
  7831. <p>Finally, to actually run your worker-based synapse, you must pass synctl the <code>-a</code>
  7832. commandline option to tell it to operate on all the worker configurations found
  7833. in the given directory, e.g.:</p>
  7834. <pre><code>synctl -a $CONFIG/workers start
  7835. </code></pre>
  7836. <p>Currently one should always restart all workers when restarting or upgrading
  7837. synapse, unless you explicitly know it's safe not to. For instance, restarting
  7838. synapse without restarting all the synchrotrons may result in broken typing
  7839. notifications.</p>
  7840. <p>To manipulate a specific worker, you pass the -w option to synctl:</p>
  7841. <pre><code>synctl -w $CONFIG/workers/worker1.yaml restart
  7842. </code></pre>
  7843. <div style="break-before: page; page-break-before: always;"></div><h1 id="setting-up-synapse-with-workers-and-systemd"><a class="header" href="#setting-up-synapse-with-workers-and-systemd">Setting up Synapse with Workers and Systemd</a></h1>
  7844. <p>This is a setup for managing synapse with systemd, including support for
  7845. managing workers. It provides a <code>matrix-synapse</code> service for the master, as
  7846. well as a <code>matrix-synapse-worker@</code> service template for any workers you
  7847. require. Additionally, to group the required services, it sets up a
  7848. <code>matrix-synapse.target</code>.</p>
  7849. <p>See the folder <a href="https://github.com/matrix-org/synapse/tree/develop/docs/systemd-with-workers/system/">system</a>
  7850. for the systemd unit files.</p>
  7851. <p>The folder <a href="https://github.com/matrix-org/synapse/tree/develop/docs/systemd-with-workers/workers/">workers</a>
  7852. contains an example configuration for the <code>federation_reader</code> worker.</p>
  7853. <h2 id="synapse-configuration-files"><a class="header" href="#synapse-configuration-files">Synapse configuration files</a></h2>
  7854. <p>See <a href="systemd-with-workers/../workers.html">workers.md</a> for information on how to set up the
  7855. configuration files and reverse-proxy correctly. You can find an example worker
  7856. config in the <a href="https://github.com/matrix-org/synapse/tree/develop/docs/systemd-with-workers/workers/">workers</a>
  7857. folder.</p>
  7858. <p>Systemd manages daemonization itself, so ensure that none of the configuration
  7859. files set either <code>daemonize</code> or <code>worker_daemonize</code>.</p>
  7860. <p>The config files of all workers are expected to be located in
  7861. <code>/etc/matrix-synapse/workers</code>. If you want to use a different location, edit
  7862. the provided <code>*.service</code> files accordingly.</p>
  7863. <p>There is no need for a separate configuration file for the master process.</p>
  7864. <h2 id="set-up"><a class="header" href="#set-up">Set up</a></h2>
  7865. <ol>
  7866. <li>Adjust synapse configuration files as above.</li>
  7867. <li>Copy the <code>*.service</code> and <code>*.target</code> files in <a href="https://github.com/matrix-org/synapse/tree/develop/docs/systemd-with-workers/system/">system</a>
  7868. to <code>/etc/systemd/system</code>.</li>
  7869. <li>Run <code>systemctl daemon-reload</code> to tell systemd to load the new unit files.</li>
  7870. <li>Run <code>systemctl enable matrix-synapse.service</code>. This will configure the
  7871. synapse master process to be started as part of the <code>matrix-synapse.target</code>
  7872. target.</li>
  7873. <li>For each worker process to be enabled, run <code>systemctl enable matrix-synapse-worker@&lt;worker_name&gt;.service</code>. For each <code>&lt;worker_name&gt;</code>, there
  7874. should be a corresponding configuration file.
  7875. <code>/etc/matrix-synapse/workers/&lt;worker_name&gt;.yaml</code>.</li>
  7876. <li>Start all the synapse processes with <code>systemctl start matrix-synapse.target</code>.</li>
  7877. <li>Tell systemd to start synapse on boot with <code>systemctl enable matrix-synapse.target</code>.</li>
  7878. </ol>
  7879. <h2 id="usage"><a class="header" href="#usage">Usage</a></h2>
  7880. <p>Once the services are correctly set up, you can use the following commands
  7881. to manage your synapse installation:</p>
  7882. <pre><code class="language-sh"># Restart Synapse master and all workers
  7883. systemctl restart matrix-synapse.target
  7884. # Stop Synapse and all workers
  7885. systemctl stop matrix-synapse.target
  7886. # Restart the master alone
  7887. systemctl start matrix-synapse.service
  7888. # Restart a specific worker (eg. federation_reader); the master is
  7889. # unaffected by this.
  7890. systemctl restart matrix-synapse-worker@federation_reader.service
  7891. # Add a new worker (assuming all configs are set up already)
  7892. systemctl enable matrix-synapse-worker@federation_writer.service
  7893. systemctl restart matrix-synapse.target
  7894. </code></pre>
  7895. <h2 id="hardening"><a class="header" href="#hardening">Hardening</a></h2>
  7896. <p><strong>Optional:</strong> If further hardening is desired, the file
  7897. <code>override-hardened.conf</code> may be copied from
  7898. <code>contrib/systemd/override-hardened.conf</code> in this repository to the location
  7899. <code>/etc/systemd/system/matrix-synapse.service.d/override-hardened.conf</code> (the
  7900. directory may have to be created). It enables certain sandboxing features in
  7901. systemd to further secure the synapse service. You may read the comments to
  7902. understand what the override file is doing. The same file will need to be copied
  7903. to
  7904. <code>/etc/systemd/system/matrix-synapse-worker@.service.d/override-hardened-worker.conf</code>
  7905. (this directory may also have to be created) in order to apply the same
  7906. hardening options to any worker processes.</p>
  7907. <p>Once these files have been copied to their appropriate locations, simply reload
  7908. systemd's manager config files and restart all Synapse services to apply the hardening options. They will automatically
  7909. be applied at every restart as long as the override files are present at the
  7910. specified locations.</p>
  7911. <pre><code class="language-sh">systemctl daemon-reload
  7912. # Restart services
  7913. systemctl restart matrix-synapse.target
  7914. </code></pre>
  7915. <p>In order to see their effect, you may run <code>systemd-analyze security matrix-synapse.service</code> before and after applying the hardening options to see
  7916. the changes being applied at a glance.</p>
  7917. <div style="break-before: page; page-break-before: always;"></div><h1 id="administration"><a class="header" href="#administration">Administration</a></h1>
  7918. <p>This section contains information on managing your Synapse homeserver. This includes:</p>
  7919. <ul>
  7920. <li>Managing users, rooms and media via the Admin API.</li>
  7921. <li>Setting up metrics and monitoring to give you insight into your homeserver's health.</li>
  7922. <li>Configuring structured logging.</li>
  7923. </ul>
  7924. <div style="break-before: page; page-break-before: always;"></div><h1 id="the-admin-api"><a class="header" href="#the-admin-api">The Admin API</a></h1>
  7925. <h2 id="authenticate-as-a-server-admin"><a class="header" href="#authenticate-as-a-server-admin">Authenticate as a server admin</a></h2>
  7926. <p>Many of the API calls in the admin api will require an <code>access_token</code> for a
  7927. server admin. (Note that a server admin is distinct from a room admin.)</p>
  7928. <p>A user can be marked as a server admin by updating the database directly, e.g.:</p>
  7929. <pre><code class="language-sql">UPDATE users SET admin = 1 WHERE name = '@foo:bar.com';
  7930. </code></pre>
  7931. <p>A new server admin user can also be created using the <code>register_new_matrix_user</code>
  7932. command. This is a script that is located in the <code>scripts/</code> directory, or possibly
  7933. already on your <code>$PATH</code> depending on how Synapse was installed.</p>
  7934. <p>Finding your user's <code>access_token</code> is client-dependent, but will usually be shown in the client's settings.</p>
  7935. <h2 id="making-an-admin-api-request"><a class="header" href="#making-an-admin-api-request">Making an Admin API request</a></h2>
  7936. <p>Once you have your <code>access_token</code>, you will need to authenticate each request to an Admin API endpoint by
  7937. providing the token as either a query parameter or a request header. To add it as a request header in cURL:</p>
  7938. <pre><code class="language-sh">curl --header &quot;Authorization: Bearer &lt;access_token&gt;&quot; &lt;the_rest_of_your_API_request&gt;
  7939. </code></pre>
  7940. <p>For more details on access tokens in Matrix, please refer to the complete
  7941. <a href="https://matrix.org/docs/spec/client_server/r0.6.1#using-access-tokens">matrix spec documentation</a>.</p>
  7942. <div style="break-before: page; page-break-before: always;"></div><h1 id="account-validity-api"><a class="header" href="#account-validity-api">Account validity API</a></h1>
  7943. <p>This API allows a server administrator to manage the validity of an account. To
  7944. use it, you must enable the account validity feature (under
  7945. <code>account_validity</code>) in Synapse's configuration.</p>
  7946. <h2 id="renew-account"><a class="header" href="#renew-account">Renew account</a></h2>
  7947. <p>This API extends the validity of an account by as much time as configured in the
  7948. <code>period</code> parameter from the <code>account_validity</code> configuration.</p>
  7949. <p>The API is:</p>
  7950. <pre><code>POST /_synapse/admin/v1/account_validity/validity
  7951. </code></pre>
  7952. <p>with the following body:</p>
  7953. <pre><code class="language-json">{
  7954. &quot;user_id&quot;: &quot;&lt;user ID for the account to renew&gt;&quot;,
  7955. &quot;expiration_ts&quot;: 0,
  7956. &quot;enable_renewal_emails&quot;: true
  7957. }
  7958. </code></pre>
  7959. <p><code>expiration_ts</code> is an optional parameter and overrides the expiration date,
  7960. which otherwise defaults to now + validity period.</p>
  7961. <p><code>enable_renewal_emails</code> is also an optional parameter and enables/disables
  7962. sending renewal emails to the user. Defaults to true.</p>
  7963. <p>The API returns with the new expiration date for this account, as a timestamp in
  7964. milliseconds since epoch:</p>
  7965. <pre><code class="language-json">{
  7966. &quot;expiration_ts&quot;: 0
  7967. }
  7968. </code></pre>
  7969. <div style="break-before: page; page-break-before: always;"></div><h1 id="delete-a-local-group"><a class="header" href="#delete-a-local-group">Delete a local group</a></h1>
  7970. <p>This API lets a server admin delete a local group. Doing so will kick all
  7971. users out of the group so that their clients will correctly handle the group
  7972. being deleted.</p>
  7973. <p>The API is:</p>
  7974. <pre><code>POST /_synapse/admin/v1/delete_group/&lt;group_id&gt;
  7975. </code></pre>
  7976. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  7977. server admin: see <a href="admin_api/../usage/administration/admin_api">Admin API</a>.</p>
  7978. <div style="break-before: page; page-break-before: always;"></div><h1 id="show-reported-events"><a class="header" href="#show-reported-events">Show reported events</a></h1>
  7979. <p>This API returns information about reported events.</p>
  7980. <p>The api is:</p>
  7981. <pre><code>GET /_synapse/admin/v1/event_reports?from=0&amp;limit=10
  7982. </code></pre>
  7983. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  7984. server admin: see <a href="admin_api/../usage/administration/admin_api">Admin API</a>.</p>
  7985. <p>It returns a JSON body like the following:</p>
  7986. <pre><code class="language-json">{
  7987. &quot;event_reports&quot;: [
  7988. {
  7989. &quot;event_id&quot;: &quot;$bNUFCwGzWca1meCGkjp-zwslF-GfVcXukvRLI1_FaVY&quot;,
  7990. &quot;id&quot;: 2,
  7991. &quot;reason&quot;: &quot;foo&quot;,
  7992. &quot;score&quot;: -100,
  7993. &quot;received_ts&quot;: 1570897107409,
  7994. &quot;canonical_alias&quot;: &quot;#alias1:matrix.org&quot;,
  7995. &quot;room_id&quot;: &quot;!ERAgBpSOcCCuTJqQPk:matrix.org&quot;,
  7996. &quot;name&quot;: &quot;Matrix HQ&quot;,
  7997. &quot;sender&quot;: &quot;@foobar:matrix.org&quot;,
  7998. &quot;user_id&quot;: &quot;@foo:matrix.org&quot;
  7999. },
  8000. {
  8001. &quot;event_id&quot;: &quot;$3IcdZsDaN_En-S1DF4EMCy3v4gNRKeOJs8W5qTOKj4I&quot;,
  8002. &quot;id&quot;: 3,
  8003. &quot;reason&quot;: &quot;bar&quot;,
  8004. &quot;score&quot;: -100,
  8005. &quot;received_ts&quot;: 1598889612059,
  8006. &quot;canonical_alias&quot;: &quot;#alias2:matrix.org&quot;,
  8007. &quot;room_id&quot;: &quot;!eGvUQuTCkHGVwNMOjv:matrix.org&quot;,
  8008. &quot;name&quot;: &quot;Your room name here&quot;,
  8009. &quot;sender&quot;: &quot;@foobar:matrix.org&quot;,
  8010. &quot;user_id&quot;: &quot;@bar:matrix.org&quot;
  8011. }
  8012. ],
  8013. &quot;next_token&quot;: 2,
  8014. &quot;total&quot;: 4
  8015. }
  8016. </code></pre>
  8017. <p>To paginate, check for <code>next_token</code> and if present, call the endpoint again with <code>from</code>
  8018. set to the value of <code>next_token</code>. This will return a new page.</p>
  8019. <p>If the endpoint does not return a <code>next_token</code> then there are no more reports to
  8020. paginate through.</p>
  8021. <p><strong>URL parameters:</strong></p>
  8022. <ul>
  8023. <li><code>limit</code>: integer - Is optional but is used for pagination, denoting the maximum number
  8024. of items to return in this call. Defaults to <code>100</code>.</li>
  8025. <li><code>from</code>: integer - Is optional but used for pagination, denoting the offset in the
  8026. returned results. This should be treated as an opaque value and not explicitly set to
  8027. anything other than the return value of <code>next_token</code> from a previous call. Defaults to <code>0</code>.</li>
  8028. <li><code>dir</code>: string - Direction of event report order. Whether to fetch the most recent
  8029. first (<code>b</code>) or the oldest first (<code>f</code>). Defaults to <code>b</code>.</li>
  8030. <li><code>user_id</code>: string - Is optional and filters to only return users with user IDs that
  8031. contain this value. This is the user who reported the event and wrote the reason.</li>
  8032. <li><code>room_id</code>: string - Is optional and filters to only return rooms with room IDs that
  8033. contain this value.</li>
  8034. </ul>
  8035. <p><strong>Response</strong></p>
  8036. <p>The following fields are returned in the JSON response body:</p>
  8037. <ul>
  8038. <li><code>id</code>: integer - ID of event report.</li>
  8039. <li><code>received_ts</code>: integer - The timestamp (in milliseconds since the unix epoch) when this
  8040. report was sent.</li>
  8041. <li><code>room_id</code>: string - The ID of the room in which the event being reported is located.</li>
  8042. <li><code>name</code>: string - The name of the room.</li>
  8043. <li><code>event_id</code>: string - The ID of the reported event.</li>
  8044. <li><code>user_id</code>: string - This is the user who reported the event and wrote the reason.</li>
  8045. <li><code>reason</code>: string - Comment made by the <code>user_id</code> in this report. May be blank or <code>null</code>.</li>
  8046. <li><code>score</code>: integer - Content is reported based upon a negative score, where -100 is
  8047. &quot;most offensive&quot; and 0 is &quot;inoffensive&quot;. May be <code>null</code>.</li>
  8048. <li><code>sender</code>: string - This is the ID of the user who sent the original message/event that
  8049. was reported.</li>
  8050. <li><code>canonical_alias</code>: string - The canonical alias of the room. <code>null</code> if the room does not
  8051. have a canonical alias set.</li>
  8052. <li><code>next_token</code>: integer - Indication for pagination. See above.</li>
  8053. <li><code>total</code>: integer - Total number of event reports related to the query
  8054. (<code>user_id</code> and <code>room_id</code>).</li>
  8055. </ul>
  8056. <h1 id="show-details-of-a-specific-event-report"><a class="header" href="#show-details-of-a-specific-event-report">Show details of a specific event report</a></h1>
  8057. <p>This API returns information about a specific event report.</p>
  8058. <p>The api is:</p>
  8059. <pre><code>GET /_synapse/admin/v1/event_reports/&lt;report_id&gt;
  8060. </code></pre>
  8061. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  8062. server admin: see <a href="admin_api/../usage/administration/admin_api">Admin API</a>.</p>
  8063. <p>It returns a JSON body like the following:</p>
  8064. <pre><code class="language-jsonc">{
  8065. &quot;event_id&quot;: &quot;$bNUFCwGzWca1meCGkjp-zwslF-GfVcXukvRLI1_FaVY&quot;,
  8066. &quot;event_json&quot;: {
  8067. &quot;auth_events&quot;: [
  8068. &quot;$YK4arsKKcc0LRoe700pS8DSjOvUT4NDv0HfInlMFw2M&quot;,
  8069. &quot;$oggsNXxzPFRE3y53SUNd7nsj69-QzKv03a1RucHu-ws&quot;
  8070. ],
  8071. &quot;content&quot;: {
  8072. &quot;body&quot;: &quot;matrix.org: This Week in Matrix&quot;,
  8073. &quot;format&quot;: &quot;org.matrix.custom.html&quot;,
  8074. &quot;formatted_body&quot;: &quot;&lt;strong&gt;matrix.org&lt;/strong&gt;:&lt;br&gt;&lt;a href=\&quot;https://matrix.org/blog/\&quot;&gt;&lt;strong&gt;This Week in Matrix&lt;/strong&gt;&lt;/a&gt;&quot;,
  8075. &quot;msgtype&quot;: &quot;m.notice&quot;
  8076. },
  8077. &quot;depth&quot;: 546,
  8078. &quot;hashes&quot;: {
  8079. &quot;sha256&quot;: &quot;xK1//xnmvHJIOvbgXlkI8eEqdvoMmihVDJ9J4SNlsAw&quot;
  8080. },
  8081. &quot;origin&quot;: &quot;matrix.org&quot;,
  8082. &quot;origin_server_ts&quot;: 1592291711430,
  8083. &quot;prev_events&quot;: [
  8084. &quot;$YK4arsKKcc0LRoe700pS8DSjOvUT4NDv0HfInlMFw2M&quot;
  8085. ],
  8086. &quot;prev_state&quot;: [],
  8087. &quot;room_id&quot;: &quot;!ERAgBpSOcCCuTJqQPk:matrix.org&quot;,
  8088. &quot;sender&quot;: &quot;@foobar:matrix.org&quot;,
  8089. &quot;signatures&quot;: {
  8090. &quot;matrix.org&quot;: {
  8091. &quot;ed25519:a_JaEG&quot;: &quot;cs+OUKW/iHx5pEidbWxh0UiNNHwe46Ai9LwNz+Ah16aWDNszVIe2gaAcVZfvNsBhakQTew51tlKmL2kspXk/Dg&quot;
  8092. }
  8093. },
  8094. &quot;type&quot;: &quot;m.room.message&quot;,
  8095. &quot;unsigned&quot;: {
  8096. &quot;age_ts&quot;: 1592291711430,
  8097. }
  8098. },
  8099. &quot;id&quot;: &lt;report_id&gt;,
  8100. &quot;reason&quot;: &quot;foo&quot;,
  8101. &quot;score&quot;: -100,
  8102. &quot;received_ts&quot;: 1570897107409,
  8103. &quot;canonical_alias&quot;: &quot;#alias1:matrix.org&quot;,
  8104. &quot;room_id&quot;: &quot;!ERAgBpSOcCCuTJqQPk:matrix.org&quot;,
  8105. &quot;name&quot;: &quot;Matrix HQ&quot;,
  8106. &quot;sender&quot;: &quot;@foobar:matrix.org&quot;,
  8107. &quot;user_id&quot;: &quot;@foo:matrix.org&quot;
  8108. }
  8109. </code></pre>
  8110. <p><strong>URL parameters:</strong></p>
  8111. <ul>
  8112. <li><code>report_id</code>: string - The ID of the event report.</li>
  8113. </ul>
  8114. <p><strong>Response</strong></p>
  8115. <p>The following fields are returned in the JSON response body:</p>
  8116. <ul>
  8117. <li><code>id</code>: integer - ID of event report.</li>
  8118. <li><code>received_ts</code>: integer - The timestamp (in milliseconds since the unix epoch) when this
  8119. report was sent.</li>
  8120. <li><code>room_id</code>: string - The ID of the room in which the event being reported is located.</li>
  8121. <li><code>name</code>: string - The name of the room.</li>
  8122. <li><code>event_id</code>: string - The ID of the reported event.</li>
  8123. <li><code>user_id</code>: string - This is the user who reported the event and wrote the reason.</li>
  8124. <li><code>reason</code>: string - Comment made by the <code>user_id</code> in this report. May be blank.</li>
  8125. <li><code>score</code>: integer - Content is reported based upon a negative score, where -100 is
  8126. &quot;most offensive&quot; and 0 is &quot;inoffensive&quot;.</li>
  8127. <li><code>sender</code>: string - This is the ID of the user who sent the original message/event that
  8128. was reported.</li>
  8129. <li><code>canonical_alias</code>: string - The canonical alias of the room. <code>null</code> if the room does not
  8130. have a canonical alias set.</li>
  8131. <li><code>event_json</code>: object - Details of the original event that was reported.</li>
  8132. </ul>
  8133. <div style="break-before: page; page-break-before: always;"></div><h1 id="contents-1"><a class="header" href="#contents-1">Contents</a></h1>
  8134. <ul>
  8135. <li><a href="admin_api/media_admin_api.html#querying-media">Querying media</a>
  8136. <ul>
  8137. <li><a href="admin_api/media_admin_api.html#list-all-media-in-a-room">List all media in a room</a></li>
  8138. <li><a href="admin_api/media_admin_api.html#list-all-media-uploaded-by-a-user">List all media uploaded by a user</a></li>
  8139. </ul>
  8140. </li>
  8141. <li><a href="admin_api/media_admin_api.html#quarantine-media">Quarantine media</a>
  8142. <ul>
  8143. <li><a href="admin_api/media_admin_api.html#quarantining-media-by-id">Quarantining media by ID</a></li>
  8144. <li><a href="admin_api/media_admin_api.html#remove-media-from-quarantine-by-id">Remove media from quarantine by ID</a></li>
  8145. <li><a href="admin_api/media_admin_api.html#quarantining-media-in-a-room">Quarantining media in a room</a></li>
  8146. <li><a href="admin_api/media_admin_api.html#quarantining-all-media-of-a-user">Quarantining all media of a user</a></li>
  8147. <li><a href="admin_api/media_admin_api.html#protecting-media-from-being-quarantined">Protecting media from being quarantined</a></li>
  8148. <li><a href="admin_api/media_admin_api.html#unprotecting-media-from-being-quarantined">Unprotecting media from being quarantined</a></li>
  8149. </ul>
  8150. </li>
  8151. <li><a href="admin_api/media_admin_api.html#delete-local-media">Delete local media</a>
  8152. <ul>
  8153. <li><a href="admin_api/media_admin_api.html#delete-a-specific-local-media">Delete a specific local media</a></li>
  8154. <li><a href="admin_api/media_admin_api.html#delete-local-media-by-date-or-size">Delete local media by date or size</a></li>
  8155. </ul>
  8156. </li>
  8157. <li><a href="admin_api/media_admin_api.html#purge-remote-media-api">Purge Remote Media API</a></li>
  8158. </ul>
  8159. <h1 id="querying-media"><a class="header" href="#querying-media">Querying media</a></h1>
  8160. <p>These APIs allow extracting media information from the homeserver.</p>
  8161. <h2 id="list-all-media-in-a-room"><a class="header" href="#list-all-media-in-a-room">List all media in a room</a></h2>
  8162. <p>This API gets a list of known media in a room.
  8163. However, it only shows media from unencrypted events or rooms.</p>
  8164. <p>The API is:</p>
  8165. <pre><code>GET /_synapse/admin/v1/room/&lt;room_id&gt;/media
  8166. </code></pre>
  8167. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  8168. server admin: see <a href="admin_api/../usage/administration/admin_api">Admin API</a>.</p>
  8169. <p>The API returns a JSON body like the following:</p>
  8170. <pre><code class="language-json">{
  8171. &quot;local&quot;: [
  8172. &quot;mxc://localhost/xwvutsrqponmlkjihgfedcba&quot;,
  8173. &quot;mxc://localhost/abcdefghijklmnopqrstuvwx&quot;
  8174. ],
  8175. &quot;remote&quot;: [
  8176. &quot;mxc://matrix.org/xwvutsrqponmlkjihgfedcba&quot;,
  8177. &quot;mxc://matrix.org/abcdefghijklmnopqrstuvwx&quot;
  8178. ]
  8179. }
  8180. </code></pre>
  8181. <h2 id="list-all-media-uploaded-by-a-user"><a class="header" href="#list-all-media-uploaded-by-a-user">List all media uploaded by a user</a></h2>
  8182. <p>Listing all media that has been uploaded by a local user can be achieved through
  8183. the use of the <a href="admin_api/user_admin_api.rst#list-media-of-a-user">List media of a user</a>
  8184. Admin API.</p>
  8185. <h1 id="quarantine-media"><a class="header" href="#quarantine-media">Quarantine media</a></h1>
  8186. <p>Quarantining media means that it is marked as inaccessible by users. It applies
  8187. to any local media, and any locally-cached copies of remote media.</p>
  8188. <p>The media file itself (and any thumbnails) is not deleted from the server.</p>
  8189. <h2 id="quarantining-media-by-id"><a class="header" href="#quarantining-media-by-id">Quarantining media by ID</a></h2>
  8190. <p>This API quarantines a single piece of local or remote media.</p>
  8191. <p>Request:</p>
  8192. <pre><code>POST /_synapse/admin/v1/media/quarantine/&lt;server_name&gt;/&lt;media_id&gt;
  8193. {}
  8194. </code></pre>
  8195. <p>Where <code>server_name</code> is in the form of <code>example.org</code>, and <code>media_id</code> is in the
  8196. form of <code>abcdefg12345...</code>.</p>
  8197. <p>Response:</p>
  8198. <pre><code class="language-json">{}
  8199. </code></pre>
  8200. <h2 id="remove-media-from-quarantine-by-id"><a class="header" href="#remove-media-from-quarantine-by-id">Remove media from quarantine by ID</a></h2>
  8201. <p>This API removes a single piece of local or remote media from quarantine.</p>
  8202. <p>Request:</p>
  8203. <pre><code>POST /_synapse/admin/v1/media/unquarantine/&lt;server_name&gt;/&lt;media_id&gt;
  8204. {}
  8205. </code></pre>
  8206. <p>Where <code>server_name</code> is in the form of <code>example.org</code>, and <code>media_id</code> is in the
  8207. form of <code>abcdefg12345...</code>.</p>
  8208. <p>Response:</p>
  8209. <pre><code class="language-json">{}
  8210. </code></pre>
  8211. <h2 id="quarantining-media-in-a-room"><a class="header" href="#quarantining-media-in-a-room">Quarantining media in a room</a></h2>
  8212. <p>This API quarantines all local and remote media in a room.</p>
  8213. <p>Request:</p>
  8214. <pre><code>POST /_synapse/admin/v1/room/&lt;room_id&gt;/media/quarantine
  8215. {}
  8216. </code></pre>
  8217. <p>Where <code>room_id</code> is in the form of <code>!roomid12345:example.org</code>.</p>
  8218. <p>Response:</p>
  8219. <pre><code class="language-json">{
  8220. &quot;num_quarantined&quot;: 10
  8221. }
  8222. </code></pre>
  8223. <p>The following fields are returned in the JSON response body:</p>
  8224. <ul>
  8225. <li><code>num_quarantined</code>: integer - The number of media items successfully quarantined</li>
  8226. </ul>
  8227. <p>Note that there is a legacy endpoint, <code>POST /_synapse/admin/v1/quarantine_media/&lt;room_id&gt;</code>, that operates the same.
  8228. However, it is deprecated and may be removed in a future release.</p>
  8229. <h2 id="quarantining-all-media-of-a-user"><a class="header" href="#quarantining-all-media-of-a-user">Quarantining all media of a user</a></h2>
  8230. <p>This API quarantines all <em>local</em> media that a <em>local</em> user has uploaded. That is to say, if
  8231. you would like to quarantine media uploaded by a user on a remote homeserver, you should
  8232. instead use one of the other APIs.</p>
  8233. <p>Request:</p>
  8234. <pre><code>POST /_synapse/admin/v1/user/&lt;user_id&gt;/media/quarantine
  8235. {}
  8236. </code></pre>
  8237. <p>URL Parameters</p>
  8238. <ul>
  8239. <li><code>user_id</code>: string - User ID in the form of <code>@bob:example.org</code></li>
  8240. </ul>
  8241. <p>Response:</p>
  8242. <pre><code class="language-json">{
  8243. &quot;num_quarantined&quot;: 10
  8244. }
  8245. </code></pre>
  8246. <p>The following fields are returned in the JSON response body:</p>
  8247. <ul>
  8248. <li><code>num_quarantined</code>: integer - The number of media items successfully quarantined</li>
  8249. </ul>
  8250. <h2 id="protecting-media-from-being-quarantined"><a class="header" href="#protecting-media-from-being-quarantined">Protecting media from being quarantined</a></h2>
  8251. <p>This API protects a single piece of local media from being quarantined using the
  8252. above APIs. This is useful for sticker packs and other shared media which you do
  8253. not want to get quarantined, especially when
  8254. <a href="admin_api/media_admin_api.html#quarantining-media-in-a-room">quarantining media in a room</a>.</p>
  8255. <p>Request:</p>
  8256. <pre><code>POST /_synapse/admin/v1/media/protect/&lt;media_id&gt;
  8257. {}
  8258. </code></pre>
  8259. <p>Where <code>media_id</code> is in the form of <code>abcdefg12345...</code>.</p>
  8260. <p>Response:</p>
  8261. <pre><code class="language-json">{}
  8262. </code></pre>
  8263. <h2 id="unprotecting-media-from-being-quarantined"><a class="header" href="#unprotecting-media-from-being-quarantined">Unprotecting media from being quarantined</a></h2>
  8264. <p>This API reverts the protection of a media.</p>
  8265. <p>Request:</p>
  8266. <pre><code>POST /_synapse/admin/v1/media/unprotect/&lt;media_id&gt;
  8267. {}
  8268. </code></pre>
  8269. <p>Where <code>media_id</code> is in the form of <code>abcdefg12345...</code>.</p>
  8270. <p>Response:</p>
  8271. <pre><code class="language-json">{}
  8272. </code></pre>
  8273. <h1 id="delete-local-media"><a class="header" href="#delete-local-media">Delete local media</a></h1>
  8274. <p>This API deletes the <em>local</em> media from the disk of your own server.
  8275. This includes any local thumbnails and copies of media downloaded from
  8276. remote homeservers.
  8277. This API will not affect media that has been uploaded to external
  8278. media repositories (e.g https://github.com/turt2live/matrix-media-repo/).
  8279. See also <a href="admin_api/media_admin_api.html#purge-remote-media-api">Purge Remote Media API</a>.</p>
  8280. <h2 id="delete-a-specific-local-media"><a class="header" href="#delete-a-specific-local-media">Delete a specific local media</a></h2>
  8281. <p>Delete a specific <code>media_id</code>.</p>
  8282. <p>Request:</p>
  8283. <pre><code>DELETE /_synapse/admin/v1/media/&lt;server_name&gt;/&lt;media_id&gt;
  8284. {}
  8285. </code></pre>
  8286. <p>URL Parameters</p>
  8287. <ul>
  8288. <li><code>server_name</code>: string - The name of your local server (e.g <code>matrix.org</code>)</li>
  8289. <li><code>media_id</code>: string - The ID of the media (e.g <code>abcdefghijklmnopqrstuvwx</code>)</li>
  8290. </ul>
  8291. <p>Response:</p>
  8292. <pre><code class="language-json">{
  8293. &quot;deleted_media&quot;: [
  8294. &quot;abcdefghijklmnopqrstuvwx&quot;
  8295. ],
  8296. &quot;total&quot;: 1
  8297. }
  8298. </code></pre>
  8299. <p>The following fields are returned in the JSON response body:</p>
  8300. <ul>
  8301. <li><code>deleted_media</code>: an array of strings - List of deleted <code>media_id</code></li>
  8302. <li><code>total</code>: integer - Total number of deleted <code>media_id</code></li>
  8303. </ul>
  8304. <h2 id="delete-local-media-by-date-or-size"><a class="header" href="#delete-local-media-by-date-or-size">Delete local media by date or size</a></h2>
  8305. <p>Request:</p>
  8306. <pre><code>POST /_synapse/admin/v1/media/&lt;server_name&gt;/delete?before_ts=&lt;before_ts&gt;
  8307. {}
  8308. </code></pre>
  8309. <p>URL Parameters</p>
  8310. <ul>
  8311. <li><code>server_name</code>: string - The name of your local server (e.g <code>matrix.org</code>).</li>
  8312. <li><code>before_ts</code>: string representing a positive integer - Unix timestamp in ms.
  8313. Files that were last used before this timestamp will be deleted. It is the timestamp of
  8314. last access and not the timestamp creation. </li>
  8315. <li><code>size_gt</code>: Optional - string representing a positive integer - Size of the media in bytes.
  8316. Files that are larger will be deleted. Defaults to <code>0</code>.</li>
  8317. <li><code>keep_profiles</code>: Optional - string representing a boolean - Switch to also delete files
  8318. that are still used in image data (e.g user profile, room avatar).
  8319. If <code>false</code> these files will be deleted. Defaults to <code>true</code>.</li>
  8320. </ul>
  8321. <p>Response:</p>
  8322. <pre><code class="language-json">{
  8323. &quot;deleted_media&quot;: [
  8324. &quot;abcdefghijklmnopqrstuvwx&quot;,
  8325. &quot;abcdefghijklmnopqrstuvwz&quot;
  8326. ],
  8327. &quot;total&quot;: 2
  8328. }
  8329. </code></pre>
  8330. <p>The following fields are returned in the JSON response body:</p>
  8331. <ul>
  8332. <li><code>deleted_media</code>: an array of strings - List of deleted <code>media_id</code></li>
  8333. <li><code>total</code>: integer - Total number of deleted <code>media_id</code></li>
  8334. </ul>
  8335. <h1 id="purge-remote-media-api"><a class="header" href="#purge-remote-media-api">Purge Remote Media API</a></h1>
  8336. <p>The purge remote media API allows server admins to purge old cached remote media.</p>
  8337. <p>The API is:</p>
  8338. <pre><code>POST /_synapse/admin/v1/purge_media_cache?before_ts=&lt;unix_timestamp_in_ms&gt;
  8339. {}
  8340. </code></pre>
  8341. <p>URL Parameters</p>
  8342. <ul>
  8343. <li><code>unix_timestamp_in_ms</code>: string representing a positive integer - Unix timestamp in ms.
  8344. All cached media that was last accessed before this timestamp will be removed.</li>
  8345. </ul>
  8346. <p>Response:</p>
  8347. <pre><code class="language-json">{
  8348. &quot;deleted&quot;: 10
  8349. }
  8350. </code></pre>
  8351. <p>The following fields are returned in the JSON response body:</p>
  8352. <ul>
  8353. <li><code>deleted</code>: integer - The number of media items successfully deleted</li>
  8354. </ul>
  8355. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  8356. server admin: see <a href="admin_api/../usage/administration/admin_api">Admin API</a>.</p>
  8357. <p>If the user re-requests purged remote media, synapse will re-request the media
  8358. from the originating server.</p>
  8359. <div style="break-before: page; page-break-before: always;"></div><h1 id="purge-history-api"><a class="header" href="#purge-history-api">Purge History API</a></h1>
  8360. <p>The purge history API allows server admins to purge historic events from their
  8361. database, reclaiming disk space.</p>
  8362. <p>Depending on the amount of history being purged a call to the API may take
  8363. several minutes or longer. During this period users will not be able to
  8364. paginate further back in the room from the point being purged from.</p>
  8365. <p>Note that Synapse requires at least one message in each room, so it will never
  8366. delete the last message in a room.</p>
  8367. <p>The API is:</p>
  8368. <pre><code>POST /_synapse/admin/v1/purge_history/&lt;room_id&gt;[/&lt;event_id&gt;]
  8369. </code></pre>
  8370. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  8371. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  8372. <p>By default, events sent by local users are not deleted, as they may represent
  8373. the only copies of this content in existence. (Events sent by remote users are
  8374. deleted.)</p>
  8375. <p>Room state data (such as joins, leaves, topic) is always preserved.</p>
  8376. <p>To delete local message events as well, set <code>delete_local_events</code> in the body:</p>
  8377. <pre><code>{
  8378. &quot;delete_local_events&quot;: true
  8379. }
  8380. </code></pre>
  8381. <p>The caller must specify the point in the room to purge up to. This can be
  8382. specified by including an event_id in the URI, or by setting a
  8383. <code>purge_up_to_event_id</code> or <code>purge_up_to_ts</code> in the request body. If an event
  8384. id is given, that event (and others at the same graph depth) will be retained.
  8385. If <code>purge_up_to_ts</code> is given, it should be a timestamp since the unix epoch,
  8386. in milliseconds.</p>
  8387. <p>The API starts the purge running, and returns immediately with a JSON body with
  8388. a purge id:</p>
  8389. <pre><code class="language-json">{
  8390. &quot;purge_id&quot;: &quot;&lt;opaque id&gt;&quot;
  8391. }
  8392. </code></pre>
  8393. <h2 id="purge-status-query"><a class="header" href="#purge-status-query">Purge status query</a></h2>
  8394. <p>It is possible to poll for updates on recent purges with a second API;</p>
  8395. <pre><code>GET /_synapse/admin/v1/purge_history_status/&lt;purge_id&gt;
  8396. </code></pre>
  8397. <p>Again, you will need to authenticate by providing an <code>access_token</code> for a
  8398. server admin.</p>
  8399. <p>This API returns a JSON body like the following:</p>
  8400. <pre><code class="language-json">{
  8401. &quot;status&quot;: &quot;active&quot;
  8402. }
  8403. </code></pre>
  8404. <p>The status will be one of <code>active</code>, <code>complete</code>, or <code>failed</code>.</p>
  8405. <h2 id="reclaim-disk-space-postgres"><a class="header" href="#reclaim-disk-space-postgres">Reclaim disk space (Postgres)</a></h2>
  8406. <p>To reclaim the disk space and return it to the operating system, you need to run
  8407. <code>VACUUM FULL;</code> on the database.</p>
  8408. <p><a href="https://www.postgresql.org/docs/current/sql-vacuum.html">https://www.postgresql.org/docs/current/sql-vacuum.html</a></p>
  8409. <div style="break-before: page; page-break-before: always;"></div><h1 id="deprecated-purge-room-api"><a class="header" href="#deprecated-purge-room-api">Deprecated: Purge room API</a></h1>
  8410. <p><strong>The old Purge room API is deprecated and will be removed in a future release.
  8411. See the new <a href="admin_api/rooms.html#delete-room-api">Delete Room API</a> for more details.</strong></p>
  8412. <p>This API will remove all trace of a room from your database.</p>
  8413. <p>All local users must have left the room before it can be removed.</p>
  8414. <p>The API is:</p>
  8415. <pre><code>POST /_synapse/admin/v1/purge_room
  8416. {
  8417. &quot;room_id&quot;: &quot;!room:id&quot;
  8418. }
  8419. </code></pre>
  8420. <p>You must authenticate using the access token of an admin user.</p>
  8421. <div style="break-before: page; page-break-before: always;"></div><h1 id="shared-secret-registration"><a class="header" href="#shared-secret-registration">Shared-Secret Registration</a></h1>
  8422. <p>This API allows for the creation of users in an administrative and
  8423. non-interactive way. This is generally used for bootstrapping a Synapse
  8424. instance with administrator accounts.</p>
  8425. <p>To authenticate yourself to the server, you will need both the shared secret
  8426. (<code>registration_shared_secret</code> in the homeserver configuration), and a
  8427. one-time nonce. If the registration shared secret is not configured, this API
  8428. is not enabled.</p>
  8429. <p>To fetch the nonce, you need to request one from the API:</p>
  8430. <pre><code>&gt; GET /_synapse/admin/v1/register
  8431. &lt; {&quot;nonce&quot;: &quot;thisisanonce&quot;}
  8432. </code></pre>
  8433. <p>Once you have the nonce, you can make a <code>POST</code> to the same URL with a JSON
  8434. body containing the nonce, username, password, whether they are an admin
  8435. (optional, False by default), and a HMAC digest of the content. Also you can
  8436. set the displayname (optional, <code>username</code> by default).</p>
  8437. <p>As an example:</p>
  8438. <pre><code>&gt; POST /_synapse/admin/v1/register
  8439. &gt; {
  8440. &quot;nonce&quot;: &quot;thisisanonce&quot;,
  8441. &quot;username&quot;: &quot;pepper_roni&quot;,
  8442. &quot;displayname&quot;: &quot;Pepper Roni&quot;,
  8443. &quot;password&quot;: &quot;pizza&quot;,
  8444. &quot;admin&quot;: true,
  8445. &quot;mac&quot;: &quot;mac_digest_here&quot;
  8446. }
  8447. &lt; {
  8448. &quot;access_token&quot;: &quot;token_here&quot;,
  8449. &quot;user_id&quot;: &quot;@pepper_roni:localhost&quot;,
  8450. &quot;home_server&quot;: &quot;test&quot;,
  8451. &quot;device_id&quot;: &quot;device_id_here&quot;
  8452. }
  8453. </code></pre>
  8454. <p>The MAC is the hex digest output of the HMAC-SHA1 algorithm, with the key being
  8455. the shared secret and the content being the nonce, user, password, either the
  8456. string &quot;admin&quot; or &quot;notadmin&quot;, and optionally the user_type
  8457. each separated by NULs. For an example of generation in Python:</p>
  8458. <pre><code class="language-python">import hmac, hashlib
  8459. def generate_mac(nonce, user, password, admin=False, user_type=None):
  8460. mac = hmac.new(
  8461. key=shared_secret,
  8462. digestmod=hashlib.sha1,
  8463. )
  8464. mac.update(nonce.encode('utf8'))
  8465. mac.update(b&quot;\x00&quot;)
  8466. mac.update(user.encode('utf8'))
  8467. mac.update(b&quot;\x00&quot;)
  8468. mac.update(password.encode('utf8'))
  8469. mac.update(b&quot;\x00&quot;)
  8470. mac.update(b&quot;admin&quot; if admin else b&quot;notadmin&quot;)
  8471. if user_type:
  8472. mac.update(b&quot;\x00&quot;)
  8473. mac.update(user_type.encode('utf8'))
  8474. return mac.hexdigest()
  8475. </code></pre>
  8476. <div style="break-before: page; page-break-before: always;"></div><h1 id="edit-room-membership-api"><a class="header" href="#edit-room-membership-api">Edit Room Membership API</a></h1>
  8477. <p>This API allows an administrator to join an user account with a given <code>user_id</code>
  8478. to a room with a given <code>room_id_or_alias</code>. You can only modify the membership of
  8479. local users. The server administrator must be in the room and have permission to
  8480. invite users.</p>
  8481. <h2 id="parameters"><a class="header" href="#parameters">Parameters</a></h2>
  8482. <p>The following parameters are available:</p>
  8483. <ul>
  8484. <li><code>user_id</code> - Fully qualified user: for example, <code>@user:server.com</code>.</li>
  8485. <li><code>room_id_or_alias</code> - The room identifier or alias to join: for example,
  8486. <code>!636q39766251:server.com</code>.</li>
  8487. </ul>
  8488. <h2 id="usage-1"><a class="header" href="#usage-1">Usage</a></h2>
  8489. <pre><code>POST /_synapse/admin/v1/join/&lt;room_id_or_alias&gt;
  8490. {
  8491. &quot;user_id&quot;: &quot;@user:server.com&quot;
  8492. }
  8493. </code></pre>
  8494. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  8495. server admin: see <a href="admin_api/../usage/administration/admin_api">Admin API</a>.</p>
  8496. <p>Response:</p>
  8497. <pre><code>{
  8498. &quot;room_id&quot;: &quot;!636q39766251:server.com&quot;
  8499. }
  8500. </code></pre>
  8501. <div style="break-before: page; page-break-before: always;"></div><h1 id="contents-2"><a class="header" href="#contents-2">Contents</a></h1>
  8502. <ul>
  8503. <li><a href="admin_api/rooms.html#list-room-api">List Room API</a>
  8504. <ul>
  8505. <li><a href="admin_api/rooms.html#parameters">Parameters</a></li>
  8506. <li><a href="admin_api/rooms.html#usage">Usage</a></li>
  8507. </ul>
  8508. </li>
  8509. <li><a href="admin_api/rooms.html#room-details-api">Room Details API</a></li>
  8510. <li><a href="admin_api/rooms.html#room-members-api">Room Members API</a></li>
  8511. <li><a href="admin_api/rooms.html#room-state-api">Room State API</a></li>
  8512. <li><a href="admin_api/rooms.html#delete-room-api">Delete Room API</a>
  8513. <ul>
  8514. <li><a href="admin_api/rooms.html#parameters-1">Parameters</a></li>
  8515. <li><a href="admin_api/rooms.html#response">Response</a></li>
  8516. <li><a href="admin_api/rooms.html#undoing-room-shutdowns">Undoing room shutdowns</a></li>
  8517. </ul>
  8518. </li>
  8519. <li><a href="admin_api/rooms.html#make-room-admin-api">Make Room Admin API</a></li>
  8520. <li><a href="admin_api/rooms.html#forward-extremities-admin-api">Forward Extremities Admin API</a></li>
  8521. <li><a href="admin_api/rooms.html#event-context-api">Event Context API</a></li>
  8522. </ul>
  8523. <h1 id="list-room-api"><a class="header" href="#list-room-api">List Room API</a></h1>
  8524. <p>The List Room admin API allows server admins to get a list of rooms on their
  8525. server. There are various parameters available that allow for filtering and
  8526. sorting the returned list. This API supports pagination.</p>
  8527. <h2 id="parameters-1"><a class="header" href="#parameters-1">Parameters</a></h2>
  8528. <p>The following query parameters are available:</p>
  8529. <ul>
  8530. <li><code>from</code> - Offset in the returned list. Defaults to <code>0</code>.</li>
  8531. <li><code>limit</code> - Maximum amount of rooms to return. Defaults to <code>100</code>.</li>
  8532. <li><code>order_by</code> - The method in which to sort the returned list of rooms. Valid values are:
  8533. <ul>
  8534. <li><code>alphabetical</code> - Same as <code>name</code>. This is deprecated.</li>
  8535. <li><code>size</code> - Same as <code>joined_members</code>. This is deprecated.</li>
  8536. <li><code>name</code> - Rooms are ordered alphabetically by room name. This is the default.</li>
  8537. <li><code>canonical_alias</code> - Rooms are ordered alphabetically by main alias address of the room.</li>
  8538. <li><code>joined_members</code> - Rooms are ordered by the number of members. Largest to smallest.</li>
  8539. <li><code>joined_local_members</code> - Rooms are ordered by the number of local members. Largest to smallest.</li>
  8540. <li><code>version</code> - Rooms are ordered by room version. Largest to smallest.</li>
  8541. <li><code>creator</code> - Rooms are ordered alphabetically by creator of the room.</li>
  8542. <li><code>encryption</code> - Rooms are ordered alphabetically by the end-to-end encryption algorithm.</li>
  8543. <li><code>federatable</code> - Rooms are ordered by whether the room is federatable.</li>
  8544. <li><code>public</code> - Rooms are ordered by visibility in room list.</li>
  8545. <li><code>join_rules</code> - Rooms are ordered alphabetically by join rules of the room.</li>
  8546. <li><code>guest_access</code> - Rooms are ordered alphabetically by guest access option of the room.</li>
  8547. <li><code>history_visibility</code> - Rooms are ordered alphabetically by visibility of history of the room.</li>
  8548. <li><code>state_events</code> - Rooms are ordered by number of state events. Largest to smallest.</li>
  8549. </ul>
  8550. </li>
  8551. <li><code>dir</code> - Direction of room order. Either <code>f</code> for forwards or <code>b</code> for backwards. Setting
  8552. this value to <code>b</code> will reverse the above sort order. Defaults to <code>f</code>.</li>
  8553. <li><code>search_term</code> - Filter rooms by their room name. Search term can be contained in any
  8554. part of the room name. Defaults to no filtering.</li>
  8555. </ul>
  8556. <p>The following fields are possible in the JSON response body:</p>
  8557. <ul>
  8558. <li><code>rooms</code> - An array of objects, each containing information about a room.
  8559. <ul>
  8560. <li>Room objects contain the following fields:
  8561. <ul>
  8562. <li><code>room_id</code> - The ID of the room.</li>
  8563. <li><code>name</code> - The name of the room.</li>
  8564. <li><code>canonical_alias</code> - The canonical (main) alias address of the room.</li>
  8565. <li><code>joined_members</code> - How many users are currently in the room.</li>
  8566. <li><code>joined_local_members</code> - How many local users are currently in the room.</li>
  8567. <li><code>version</code> - The version of the room as a string.</li>
  8568. <li><code>creator</code> - The <code>user_id</code> of the room creator.</li>
  8569. <li><code>encryption</code> - Algorithm of end-to-end encryption of messages. Is <code>null</code> if encryption is not active.</li>
  8570. <li><code>federatable</code> - Whether users on other servers can join this room.</li>
  8571. <li><code>public</code> - Whether the room is visible in room directory.</li>
  8572. <li><code>join_rules</code> - The type of rules used for users wishing to join this room. One of: [&quot;public&quot;, &quot;knock&quot;, &quot;invite&quot;, &quot;private&quot;].</li>
  8573. <li><code>guest_access</code> - Whether guests can join the room. One of: [&quot;can_join&quot;, &quot;forbidden&quot;].</li>
  8574. <li><code>history_visibility</code> - Who can see the room history. One of: [&quot;invited&quot;, &quot;joined&quot;, &quot;shared&quot;, &quot;world_readable&quot;].</li>
  8575. <li><code>state_events</code> - Total number of state_events of a room. Complexity of the room.</li>
  8576. </ul>
  8577. </li>
  8578. </ul>
  8579. </li>
  8580. <li><code>offset</code> - The current pagination offset in rooms. This parameter should be
  8581. used instead of <code>next_token</code> for room offset as <code>next_token</code> is
  8582. not intended to be parsed.</li>
  8583. <li><code>total_rooms</code> - The total number of rooms this query can return. Using this
  8584. and <code>offset</code>, you have enough information to know the current
  8585. progression through the list.</li>
  8586. <li><code>next_batch</code> - If this field is present, we know that there are potentially
  8587. more rooms on the server that did not all fit into this response.
  8588. We can use <code>next_batch</code> to get the &quot;next page&quot; of results. To do
  8589. so, simply repeat your request, setting the <code>from</code> parameter to
  8590. the value of <code>next_batch</code>.</li>
  8591. <li><code>prev_batch</code> - If this field is present, it is possible to paginate backwards.
  8592. Use <code>prev_batch</code> for the <code>from</code> value in the next request to
  8593. get the &quot;previous page&quot; of results.</li>
  8594. </ul>
  8595. <h2 id="usage-2"><a class="header" href="#usage-2">Usage</a></h2>
  8596. <p>A standard request with no filtering:</p>
  8597. <pre><code>GET /_synapse/admin/v1/rooms
  8598. {}
  8599. </code></pre>
  8600. <p>Response:</p>
  8601. <pre><code class="language-jsonc">{
  8602. &quot;rooms&quot;: [
  8603. {
  8604. &quot;room_id&quot;: &quot;!OGEhHVWSdvArJzumhm:matrix.org&quot;,
  8605. &quot;name&quot;: &quot;Matrix HQ&quot;,
  8606. &quot;canonical_alias&quot;: &quot;#matrix:matrix.org&quot;,
  8607. &quot;joined_members&quot;: 8326,
  8608. &quot;joined_local_members&quot;: 2,
  8609. &quot;version&quot;: &quot;1&quot;,
  8610. &quot;creator&quot;: &quot;@foo:matrix.org&quot;,
  8611. &quot;encryption&quot;: null,
  8612. &quot;federatable&quot;: true,
  8613. &quot;public&quot;: true,
  8614. &quot;join_rules&quot;: &quot;invite&quot;,
  8615. &quot;guest_access&quot;: null,
  8616. &quot;history_visibility&quot;: &quot;shared&quot;,
  8617. &quot;state_events&quot;: 93534
  8618. },
  8619. ... (8 hidden items) ...
  8620. {
  8621. &quot;room_id&quot;: &quot;!xYvNcQPhnkrdUmYczI:matrix.org&quot;,
  8622. &quot;name&quot;: &quot;This Week In Matrix (TWIM)&quot;,
  8623. &quot;canonical_alias&quot;: &quot;#twim:matrix.org&quot;,
  8624. &quot;joined_members&quot;: 314,
  8625. &quot;joined_local_members&quot;: 20,
  8626. &quot;version&quot;: &quot;4&quot;,
  8627. &quot;creator&quot;: &quot;@foo:matrix.org&quot;,
  8628. &quot;encryption&quot;: &quot;m.megolm.v1.aes-sha2&quot;,
  8629. &quot;federatable&quot;: true,
  8630. &quot;public&quot;: false,
  8631. &quot;join_rules&quot;: &quot;invite&quot;,
  8632. &quot;guest_access&quot;: null,
  8633. &quot;history_visibility&quot;: &quot;shared&quot;,
  8634. &quot;state_events&quot;: 8345
  8635. }
  8636. ],
  8637. &quot;offset&quot;: 0,
  8638. &quot;total_rooms&quot;: 10
  8639. }
  8640. </code></pre>
  8641. <p>Filtering by room name:</p>
  8642. <pre><code>GET /_synapse/admin/v1/rooms?search_term=TWIM
  8643. {}
  8644. </code></pre>
  8645. <p>Response:</p>
  8646. <pre><code class="language-json">{
  8647. &quot;rooms&quot;: [
  8648. {
  8649. &quot;room_id&quot;: &quot;!xYvNcQPhnkrdUmYczI:matrix.org&quot;,
  8650. &quot;name&quot;: &quot;This Week In Matrix (TWIM)&quot;,
  8651. &quot;canonical_alias&quot;: &quot;#twim:matrix.org&quot;,
  8652. &quot;joined_members&quot;: 314,
  8653. &quot;joined_local_members&quot;: 20,
  8654. &quot;version&quot;: &quot;4&quot;,
  8655. &quot;creator&quot;: &quot;@foo:matrix.org&quot;,
  8656. &quot;encryption&quot;: &quot;m.megolm.v1.aes-sha2&quot;,
  8657. &quot;federatable&quot;: true,
  8658. &quot;public&quot;: false,
  8659. &quot;join_rules&quot;: &quot;invite&quot;,
  8660. &quot;guest_access&quot;: null,
  8661. &quot;history_visibility&quot;: &quot;shared&quot;,
  8662. &quot;state_events&quot;: 8
  8663. }
  8664. ],
  8665. &quot;offset&quot;: 0,
  8666. &quot;total_rooms&quot;: 1
  8667. }
  8668. </code></pre>
  8669. <p>Paginating through a list of rooms:</p>
  8670. <pre><code>GET /_synapse/admin/v1/rooms?order_by=size
  8671. {}
  8672. </code></pre>
  8673. <p>Response:</p>
  8674. <pre><code class="language-jsonc">{
  8675. &quot;rooms&quot;: [
  8676. {
  8677. &quot;room_id&quot;: &quot;!OGEhHVWSdvArJzumhm:matrix.org&quot;,
  8678. &quot;name&quot;: &quot;Matrix HQ&quot;,
  8679. &quot;canonical_alias&quot;: &quot;#matrix:matrix.org&quot;,
  8680. &quot;joined_members&quot;: 8326,
  8681. &quot;joined_local_members&quot;: 2,
  8682. &quot;version&quot;: &quot;1&quot;,
  8683. &quot;creator&quot;: &quot;@foo:matrix.org&quot;,
  8684. &quot;encryption&quot;: null,
  8685. &quot;federatable&quot;: true,
  8686. &quot;public&quot;: true,
  8687. &quot;join_rules&quot;: &quot;invite&quot;,
  8688. &quot;guest_access&quot;: null,
  8689. &quot;history_visibility&quot;: &quot;shared&quot;,
  8690. &quot;state_events&quot;: 93534
  8691. },
  8692. ... (98 hidden items) ...
  8693. {
  8694. &quot;room_id&quot;: &quot;!xYvNcQPhnkrdUmYczI:matrix.org&quot;,
  8695. &quot;name&quot;: &quot;This Week In Matrix (TWIM)&quot;,
  8696. &quot;canonical_alias&quot;: &quot;#twim:matrix.org&quot;,
  8697. &quot;joined_members&quot;: 314,
  8698. &quot;joined_local_members&quot;: 20,
  8699. &quot;version&quot;: &quot;4&quot;,
  8700. &quot;creator&quot;: &quot;@foo:matrix.org&quot;,
  8701. &quot;encryption&quot;: &quot;m.megolm.v1.aes-sha2&quot;,
  8702. &quot;federatable&quot;: true,
  8703. &quot;public&quot;: false,
  8704. &quot;join_rules&quot;: &quot;invite&quot;,
  8705. &quot;guest_access&quot;: null,
  8706. &quot;history_visibility&quot;: &quot;shared&quot;,
  8707. &quot;state_events&quot;: 8345
  8708. }
  8709. ],
  8710. &quot;offset&quot;: 0,
  8711. &quot;total_rooms&quot;: 150
  8712. &quot;next_token&quot;: 100
  8713. }
  8714. </code></pre>
  8715. <p>The presence of the <code>next_token</code> parameter tells us that there are more rooms
  8716. than returned in this request, and we need to make another request to get them.
  8717. To get the next batch of room results, we repeat our request, setting the <code>from</code>
  8718. parameter to the value of <code>next_token</code>.</p>
  8719. <pre><code>GET /_synapse/admin/v1/rooms?order_by=size&amp;from=100
  8720. {}
  8721. </code></pre>
  8722. <p>Response:</p>
  8723. <pre><code class="language-jsonc">{
  8724. &quot;rooms&quot;: [
  8725. {
  8726. &quot;room_id&quot;: &quot;!mscvqgqpHYjBGDxNym:matrix.org&quot;,
  8727. &quot;name&quot;: &quot;Music Theory&quot;,
  8728. &quot;canonical_alias&quot;: &quot;#musictheory:matrix.org&quot;,
  8729. &quot;joined_members&quot;: 127,
  8730. &quot;joined_local_members&quot;: 2,
  8731. &quot;version&quot;: &quot;1&quot;,
  8732. &quot;creator&quot;: &quot;@foo:matrix.org&quot;,
  8733. &quot;encryption&quot;: null,
  8734. &quot;federatable&quot;: true,
  8735. &quot;public&quot;: true,
  8736. &quot;join_rules&quot;: &quot;invite&quot;,
  8737. &quot;guest_access&quot;: null,
  8738. &quot;history_visibility&quot;: &quot;shared&quot;,
  8739. &quot;state_events&quot;: 93534
  8740. },
  8741. ... (48 hidden items) ...
  8742. {
  8743. &quot;room_id&quot;: &quot;!twcBhHVdZlQWuuxBhN:termina.org.uk&quot;,
  8744. &quot;name&quot;: &quot;weechat-matrix&quot;,
  8745. &quot;canonical_alias&quot;: &quot;#weechat-matrix:termina.org.uk&quot;,
  8746. &quot;joined_members&quot;: 137,
  8747. &quot;joined_local_members&quot;: 20,
  8748. &quot;version&quot;: &quot;4&quot;,
  8749. &quot;creator&quot;: &quot;@foo:termina.org.uk&quot;,
  8750. &quot;encryption&quot;: null,
  8751. &quot;federatable&quot;: true,
  8752. &quot;public&quot;: true,
  8753. &quot;join_rules&quot;: &quot;invite&quot;,
  8754. &quot;guest_access&quot;: null,
  8755. &quot;history_visibility&quot;: &quot;shared&quot;,
  8756. &quot;state_events&quot;: 8345
  8757. }
  8758. ],
  8759. &quot;offset&quot;: 100,
  8760. &quot;prev_batch&quot;: 0,
  8761. &quot;total_rooms&quot;: 150
  8762. }
  8763. </code></pre>
  8764. <p>Once the <code>next_token</code> parameter is no longer present, we know we've reached the
  8765. end of the list.</p>
  8766. <h1 id="room-details-api"><a class="header" href="#room-details-api">Room Details API</a></h1>
  8767. <p>The Room Details admin API allows server admins to get all details of a room.</p>
  8768. <p>The following fields are possible in the JSON response body:</p>
  8769. <ul>
  8770. <li><code>room_id</code> - The ID of the room.</li>
  8771. <li><code>name</code> - The name of the room.</li>
  8772. <li><code>topic</code> - The topic of the room.</li>
  8773. <li><code>avatar</code> - The <code>mxc</code> URI to the avatar of the room.</li>
  8774. <li><code>canonical_alias</code> - The canonical (main) alias address of the room.</li>
  8775. <li><code>joined_members</code> - How many users are currently in the room.</li>
  8776. <li><code>joined_local_members</code> - How many local users are currently in the room.</li>
  8777. <li><code>joined_local_devices</code> - How many local devices are currently in the room.</li>
  8778. <li><code>version</code> - The version of the room as a string.</li>
  8779. <li><code>creator</code> - The <code>user_id</code> of the room creator.</li>
  8780. <li><code>encryption</code> - Algorithm of end-to-end encryption of messages. Is <code>null</code> if encryption is not active.</li>
  8781. <li><code>federatable</code> - Whether users on other servers can join this room.</li>
  8782. <li><code>public</code> - Whether the room is visible in room directory.</li>
  8783. <li><code>join_rules</code> - The type of rules used for users wishing to join this room. One of: [&quot;public&quot;, &quot;knock&quot;, &quot;invite&quot;, &quot;private&quot;].</li>
  8784. <li><code>guest_access</code> - Whether guests can join the room. One of: [&quot;can_join&quot;, &quot;forbidden&quot;].</li>
  8785. <li><code>history_visibility</code> - Who can see the room history. One of: [&quot;invited&quot;, &quot;joined&quot;, &quot;shared&quot;, &quot;world_readable&quot;].</li>
  8786. <li><code>state_events</code> - Total number of state_events of a room. Complexity of the room.</li>
  8787. </ul>
  8788. <h2 id="usage-3"><a class="header" href="#usage-3">Usage</a></h2>
  8789. <p>A standard request:</p>
  8790. <pre><code>GET /_synapse/admin/v1/rooms/&lt;room_id&gt;
  8791. {}
  8792. </code></pre>
  8793. <p>Response:</p>
  8794. <pre><code class="language-json">{
  8795. &quot;room_id&quot;: &quot;!mscvqgqpHYjBGDxNym:matrix.org&quot;,
  8796. &quot;name&quot;: &quot;Music Theory&quot;,
  8797. &quot;avatar&quot;: &quot;mxc://matrix.org/AQDaVFlbkQoErdOgqWRgiGSV&quot;,
  8798. &quot;topic&quot;: &quot;Theory, Composition, Notation, Analysis&quot;,
  8799. &quot;canonical_alias&quot;: &quot;#musictheory:matrix.org&quot;,
  8800. &quot;joined_members&quot;: 127,
  8801. &quot;joined_local_members&quot;: 2,
  8802. &quot;joined_local_devices&quot;: 2,
  8803. &quot;version&quot;: &quot;1&quot;,
  8804. &quot;creator&quot;: &quot;@foo:matrix.org&quot;,
  8805. &quot;encryption&quot;: null,
  8806. &quot;federatable&quot;: true,
  8807. &quot;public&quot;: true,
  8808. &quot;join_rules&quot;: &quot;invite&quot;,
  8809. &quot;guest_access&quot;: null,
  8810. &quot;history_visibility&quot;: &quot;shared&quot;,
  8811. &quot;state_events&quot;: 93534
  8812. }
  8813. </code></pre>
  8814. <h1 id="room-members-api"><a class="header" href="#room-members-api">Room Members API</a></h1>
  8815. <p>The Room Members admin API allows server admins to get a list of all members of a room.</p>
  8816. <p>The response includes the following fields:</p>
  8817. <ul>
  8818. <li><code>members</code> - A list of all the members that are present in the room, represented by their ids.</li>
  8819. <li><code>total</code> - Total number of members in the room.</li>
  8820. </ul>
  8821. <h2 id="usage-4"><a class="header" href="#usage-4">Usage</a></h2>
  8822. <p>A standard request:</p>
  8823. <pre><code>GET /_synapse/admin/v1/rooms/&lt;room_id&gt;/members
  8824. {}
  8825. </code></pre>
  8826. <p>Response:</p>
  8827. <pre><code class="language-json">{
  8828. &quot;members&quot;: [
  8829. &quot;@foo:matrix.org&quot;,
  8830. &quot;@bar:matrix.org&quot;,
  8831. &quot;@foobar:matrix.org&quot;
  8832. ],
  8833. &quot;total&quot;: 3
  8834. }
  8835. </code></pre>
  8836. <h1 id="room-state-api"><a class="header" href="#room-state-api">Room State API</a></h1>
  8837. <p>The Room State admin API allows server admins to get a list of all state events in a room.</p>
  8838. <p>The response includes the following fields:</p>
  8839. <ul>
  8840. <li><code>state</code> - The current state of the room at the time of request.</li>
  8841. </ul>
  8842. <h2 id="usage-5"><a class="header" href="#usage-5">Usage</a></h2>
  8843. <p>A standard request:</p>
  8844. <pre><code>GET /_synapse/admin/v1/rooms/&lt;room_id&gt;/state
  8845. {}
  8846. </code></pre>
  8847. <p>Response:</p>
  8848. <pre><code class="language-json">{
  8849. &quot;state&quot;: [
  8850. {&quot;type&quot;: &quot;m.room.create&quot;, &quot;state_key&quot;: &quot;&quot;, &quot;etc&quot;: true},
  8851. {&quot;type&quot;: &quot;m.room.power_levels&quot;, &quot;state_key&quot;: &quot;&quot;, &quot;etc&quot;: true},
  8852. {&quot;type&quot;: &quot;m.room.name&quot;, &quot;state_key&quot;: &quot;&quot;, &quot;etc&quot;: true}
  8853. ]
  8854. }
  8855. </code></pre>
  8856. <h1 id="delete-room-api"><a class="header" href="#delete-room-api">Delete Room API</a></h1>
  8857. <p>The Delete Room admin API allows server admins to remove rooms from server
  8858. and block these rooms.</p>
  8859. <p>Shuts down a room. Moves all local users and room aliases automatically to a
  8860. new room if <code>new_room_user_id</code> is set. Otherwise local users only
  8861. leave the room without any information.</p>
  8862. <p>The new room will be created with the user specified by the <code>new_room_user_id</code> parameter
  8863. as room administrator and will contain a message explaining what happened. Users invited
  8864. to the new room will have power level <code>-10</code> by default, and thus be unable to speak.</p>
  8865. <p>If <code>block</code> is <code>True</code> it prevents new joins to the old room.</p>
  8866. <p>This API will remove all trace of the old room from your database after removing
  8867. all local users. If <code>purge</code> is <code>true</code> (the default), all traces of the old room will
  8868. be removed from your database after removing all local users. If you do not want
  8869. this to happen, set <code>purge</code> to <code>false</code>.
  8870. Depending on the amount of history being purged a call to the API may take
  8871. several minutes or longer.</p>
  8872. <p>The local server will only have the power to move local user and room aliases to
  8873. the new room. Users on other servers will be unaffected.</p>
  8874. <p>The API is:</p>
  8875. <pre><code>DELETE /_synapse/admin/v1/rooms/&lt;room_id&gt;
  8876. </code></pre>
  8877. <p>with a body of:</p>
  8878. <pre><code class="language-json">{
  8879. &quot;new_room_user_id&quot;: &quot;@someuser:example.com&quot;,
  8880. &quot;room_name&quot;: &quot;Content Violation Notification&quot;,
  8881. &quot;message&quot;: &quot;Bad Room has been shutdown due to content violations on this server. Please review our Terms of Service.&quot;,
  8882. &quot;block&quot;: true,
  8883. &quot;purge&quot;: true
  8884. }
  8885. </code></pre>
  8886. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  8887. server admin: see <a href="admin_api/../usage/administration/admin_api">Admin API</a>.</p>
  8888. <p>A response body like the following is returned:</p>
  8889. <pre><code class="language-json">{
  8890. &quot;kicked_users&quot;: [
  8891. &quot;@foobar:example.com&quot;
  8892. ],
  8893. &quot;failed_to_kick_users&quot;: [],
  8894. &quot;local_aliases&quot;: [
  8895. &quot;#badroom:example.com&quot;,
  8896. &quot;#evilsaloon:example.com&quot;
  8897. ],
  8898. &quot;new_room_id&quot;: &quot;!newroomid:example.com&quot;
  8899. }
  8900. </code></pre>
  8901. <h2 id="parameters-2"><a class="header" href="#parameters-2">Parameters</a></h2>
  8902. <p>The following parameters should be set in the URL:</p>
  8903. <ul>
  8904. <li><code>room_id</code> - The ID of the room.</li>
  8905. </ul>
  8906. <p>The following JSON body parameters are available:</p>
  8907. <ul>
  8908. <li><code>new_room_user_id</code> - Optional. If set, a new room will be created with this user ID
  8909. as the creator and admin, and all users in the old room will be moved into that
  8910. room. If not set, no new room will be created and the users will just be removed
  8911. from the old room. The user ID must be on the local server, but does not necessarily
  8912. have to belong to a registered user.</li>
  8913. <li><code>room_name</code> - Optional. A string representing the name of the room that new users will be
  8914. invited to. Defaults to <code>Content Violation Notification</code></li>
  8915. <li><code>message</code> - Optional. A string containing the first message that will be sent as
  8916. <code>new_room_user_id</code> in the new room. Ideally this will clearly convey why the
  8917. original room was shut down. Defaults to <code>Sharing illegal content on this server is not permitted and rooms in violation will be blocked.</code></li>
  8918. <li><code>block</code> - Optional. If set to <code>true</code>, this room will be added to a blocking list, preventing
  8919. future attempts to join the room. Defaults to <code>false</code>.</li>
  8920. <li><code>purge</code> - Optional. If set to <code>true</code>, it will remove all traces of the room from your database.
  8921. Defaults to <code>true</code>.</li>
  8922. <li><code>force_purge</code> - Optional, and ignored unless <code>purge</code> is <code>true</code>. If set to <code>true</code>, it
  8923. will force a purge to go ahead even if there are local users still in the room. Do not
  8924. use this unless a regular <code>purge</code> operation fails, as it could leave those users'
  8925. clients in a confused state.</li>
  8926. </ul>
  8927. <p>The JSON body must not be empty. The body must be at least <code>{}</code>.</p>
  8928. <h2 id="response"><a class="header" href="#response">Response</a></h2>
  8929. <p>The following fields are returned in the JSON response body:</p>
  8930. <ul>
  8931. <li><code>kicked_users</code> - An array of users (<code>user_id</code>) that were kicked.</li>
  8932. <li><code>failed_to_kick_users</code> - An array of users (<code>user_id</code>) that that were not kicked.</li>
  8933. <li><code>local_aliases</code> - An array of strings representing the local aliases that were migrated from
  8934. the old room to the new.</li>
  8935. <li><code>new_room_id</code> - A string representing the room ID of the new room.</li>
  8936. </ul>
  8937. <h2 id="undoing-room-shutdowns"><a class="header" href="#undoing-room-shutdowns">Undoing room shutdowns</a></h2>
  8938. <p><em>Note</em>: This guide may be outdated by the time you read it. By nature of room shutdowns being performed at the database level,
  8939. the structure can and does change without notice.</p>
  8940. <p>First, it's important to understand that a room shutdown is very destructive. Undoing a shutdown is not as simple as pretending it
  8941. never happened - work has to be done to move forward instead of resetting the past. In fact, in some cases it might not be possible
  8942. to recover at all:</p>
  8943. <ul>
  8944. <li>If the room was invite-only, your users will need to be re-invited.</li>
  8945. <li>If the room no longer has any members at all, it'll be impossible to rejoin.</li>
  8946. <li>The first user to rejoin will have to do so via an alias on a different server.</li>
  8947. </ul>
  8948. <p>With all that being said, if you still want to try and recover the room:</p>
  8949. <ol>
  8950. <li>For safety reasons, shut down Synapse.</li>
  8951. <li>In the database, run <code>DELETE FROM blocked_rooms WHERE room_id = '!example:example.org';</code>
  8952. <ul>
  8953. <li>For caution: it's recommended to run this in a transaction: <code>BEGIN; DELETE ...;</code>, verify you got 1 result, then <code>COMMIT;</code>.</li>
  8954. <li>The room ID is the same one supplied to the shutdown room API, not the Content Violation room.</li>
  8955. </ul>
  8956. </li>
  8957. <li>Restart Synapse.</li>
  8958. </ol>
  8959. <p>You will have to manually handle, if you so choose, the following:</p>
  8960. <ul>
  8961. <li>Aliases that would have been redirected to the Content Violation room.</li>
  8962. <li>Users that would have been booted from the room (and will have been force-joined to the Content Violation room).</li>
  8963. <li>Removal of the Content Violation room if desired.</li>
  8964. </ul>
  8965. <h2 id="deprecated-endpoint"><a class="header" href="#deprecated-endpoint">Deprecated endpoint</a></h2>
  8966. <p>The previous deprecated API will be removed in a future release, it was:</p>
  8967. <pre><code>POST /_synapse/admin/v1/rooms/&lt;room_id&gt;/delete
  8968. </code></pre>
  8969. <p>It behaves the same way than the current endpoint except the path and the method.</p>
  8970. <h1 id="make-room-admin-api"><a class="header" href="#make-room-admin-api">Make Room Admin API</a></h1>
  8971. <p>Grants another user the highest power available to a local user who is in the room.
  8972. If the user is not in the room, and it is not publicly joinable, then invite the user.</p>
  8973. <p>By default the server admin (the caller) is granted power, but another user can
  8974. optionally be specified, e.g.:</p>
  8975. <pre><code> POST /_synapse/admin/v1/rooms/&lt;room_id_or_alias&gt;/make_room_admin
  8976. {
  8977. &quot;user_id&quot;: &quot;@foo:example.com&quot;
  8978. }
  8979. </code></pre>
  8980. <h1 id="forward-extremities-admin-api"><a class="header" href="#forward-extremities-admin-api">Forward Extremities Admin API</a></h1>
  8981. <p>Enables querying and deleting forward extremities from rooms. When a lot of forward
  8982. extremities accumulate in a room, performance can become degraded. For details, see
  8983. <a href="https://github.com/matrix-org/synapse/issues/1760">#1760</a>.</p>
  8984. <h2 id="check-for-forward-extremities"><a class="header" href="#check-for-forward-extremities">Check for forward extremities</a></h2>
  8985. <p>To check the status of forward extremities for a room:</p>
  8986. <pre><code> GET /_synapse/admin/v1/rooms/&lt;room_id_or_alias&gt;/forward_extremities
  8987. </code></pre>
  8988. <p>A response as follows will be returned:</p>
  8989. <pre><code class="language-json">{
  8990. &quot;count&quot;: 1,
  8991. &quot;results&quot;: [
  8992. {
  8993. &quot;event_id&quot;: &quot;$M5SP266vsnxctfwFgFLNceaCo3ujhRtg_NiiHabcdefgh&quot;,
  8994. &quot;state_group&quot;: 439,
  8995. &quot;depth&quot;: 123,
  8996. &quot;received_ts&quot;: 1611263016761
  8997. }
  8998. ]
  8999. }
  9000. </code></pre>
  9001. <h2 id="deleting-forward-extremities"><a class="header" href="#deleting-forward-extremities">Deleting forward extremities</a></h2>
  9002. <p><strong>WARNING</strong>: Please ensure you know what you're doing and have read
  9003. the related issue <a href="https://github.com/matrix-org/synapse/issues/1760">#1760</a>.
  9004. Under no situations should this API be executed as an automated maintenance task!</p>
  9005. <p>If a room has lots of forward extremities, the extra can be
  9006. deleted as follows:</p>
  9007. <pre><code> DELETE /_synapse/admin/v1/rooms/&lt;room_id_or_alias&gt;/forward_extremities
  9008. </code></pre>
  9009. <p>A response as follows will be returned, indicating the amount of forward extremities
  9010. that were deleted.</p>
  9011. <pre><code class="language-json">{
  9012. &quot;deleted&quot;: 1
  9013. }
  9014. </code></pre>
  9015. <h1 id="event-context-api"><a class="header" href="#event-context-api">Event Context API</a></h1>
  9016. <p>This API lets a client find the context of an event. This is designed primarily to investigate abuse reports.</p>
  9017. <pre><code>GET /_synapse/admin/v1/rooms/&lt;room_id&gt;/context/&lt;event_id&gt;
  9018. </code></pre>
  9019. <p>This API mimmicks <a href="https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-rooms-roomid-context-eventid">GET /_matrix/client/r0/rooms/{roomId}/context/{eventId}</a>. Please refer to the link for all details on parameters and reseponse.</p>
  9020. <p>Example response:</p>
  9021. <pre><code class="language-json">{
  9022. &quot;end&quot;: &quot;t29-57_2_0_2&quot;,
  9023. &quot;events_after&quot;: [
  9024. {
  9025. &quot;content&quot;: {
  9026. &quot;body&quot;: &quot;This is an example text message&quot;,
  9027. &quot;msgtype&quot;: &quot;m.text&quot;,
  9028. &quot;format&quot;: &quot;org.matrix.custom.html&quot;,
  9029. &quot;formatted_body&quot;: &quot;&lt;b&gt;This is an example text message&lt;/b&gt;&quot;
  9030. },
  9031. &quot;type&quot;: &quot;m.room.message&quot;,
  9032. &quot;event_id&quot;: &quot;$143273582443PhrSn:example.org&quot;,
  9033. &quot;room_id&quot;: &quot;!636q39766251:example.com&quot;,
  9034. &quot;sender&quot;: &quot;@example:example.org&quot;,
  9035. &quot;origin_server_ts&quot;: 1432735824653,
  9036. &quot;unsigned&quot;: {
  9037. &quot;age&quot;: 1234
  9038. }
  9039. }
  9040. ],
  9041. &quot;event&quot;: {
  9042. &quot;content&quot;: {
  9043. &quot;body&quot;: &quot;filename.jpg&quot;,
  9044. &quot;info&quot;: {
  9045. &quot;h&quot;: 398,
  9046. &quot;w&quot;: 394,
  9047. &quot;mimetype&quot;: &quot;image/jpeg&quot;,
  9048. &quot;size&quot;: 31037
  9049. },
  9050. &quot;url&quot;: &quot;mxc://example.org/JWEIFJgwEIhweiWJE&quot;,
  9051. &quot;msgtype&quot;: &quot;m.image&quot;
  9052. },
  9053. &quot;type&quot;: &quot;m.room.message&quot;,
  9054. &quot;event_id&quot;: &quot;$f3h4d129462ha:example.com&quot;,
  9055. &quot;room_id&quot;: &quot;!636q39766251:example.com&quot;,
  9056. &quot;sender&quot;: &quot;@example:example.org&quot;,
  9057. &quot;origin_server_ts&quot;: 1432735824653,
  9058. &quot;unsigned&quot;: {
  9059. &quot;age&quot;: 1234
  9060. }
  9061. },
  9062. &quot;events_before&quot;: [
  9063. {
  9064. &quot;content&quot;: {
  9065. &quot;body&quot;: &quot;something-important.doc&quot;,
  9066. &quot;filename&quot;: &quot;something-important.doc&quot;,
  9067. &quot;info&quot;: {
  9068. &quot;mimetype&quot;: &quot;application/msword&quot;,
  9069. &quot;size&quot;: 46144
  9070. },
  9071. &quot;msgtype&quot;: &quot;m.file&quot;,
  9072. &quot;url&quot;: &quot;mxc://example.org/FHyPlCeYUSFFxlgbQYZmoEoe&quot;
  9073. },
  9074. &quot;type&quot;: &quot;m.room.message&quot;,
  9075. &quot;event_id&quot;: &quot;$143273582443PhrSn:example.org&quot;,
  9076. &quot;room_id&quot;: &quot;!636q39766251:example.com&quot;,
  9077. &quot;sender&quot;: &quot;@example:example.org&quot;,
  9078. &quot;origin_server_ts&quot;: 1432735824653,
  9079. &quot;unsigned&quot;: {
  9080. &quot;age&quot;: 1234
  9081. }
  9082. }
  9083. ],
  9084. &quot;start&quot;: &quot;t27-54_2_0_2&quot;,
  9085. &quot;state&quot;: [
  9086. {
  9087. &quot;content&quot;: {
  9088. &quot;creator&quot;: &quot;@example:example.org&quot;,
  9089. &quot;room_version&quot;: &quot;1&quot;,
  9090. &quot;m.federate&quot;: true,
  9091. &quot;predecessor&quot;: {
  9092. &quot;event_id&quot;: &quot;$something:example.org&quot;,
  9093. &quot;room_id&quot;: &quot;!oldroom:example.org&quot;
  9094. }
  9095. },
  9096. &quot;type&quot;: &quot;m.room.create&quot;,
  9097. &quot;event_id&quot;: &quot;$143273582443PhrSn:example.org&quot;,
  9098. &quot;room_id&quot;: &quot;!636q39766251:example.com&quot;,
  9099. &quot;sender&quot;: &quot;@example:example.org&quot;,
  9100. &quot;origin_server_ts&quot;: 1432735824653,
  9101. &quot;unsigned&quot;: {
  9102. &quot;age&quot;: 1234
  9103. },
  9104. &quot;state_key&quot;: &quot;&quot;
  9105. },
  9106. {
  9107. &quot;content&quot;: {
  9108. &quot;membership&quot;: &quot;join&quot;,
  9109. &quot;avatar_url&quot;: &quot;mxc://example.org/SEsfnsuifSDFSSEF&quot;,
  9110. &quot;displayname&quot;: &quot;Alice Margatroid&quot;
  9111. },
  9112. &quot;type&quot;: &quot;m.room.member&quot;,
  9113. &quot;event_id&quot;: &quot;$143273582443PhrSn:example.org&quot;,
  9114. &quot;room_id&quot;: &quot;!636q39766251:example.com&quot;,
  9115. &quot;sender&quot;: &quot;@example:example.org&quot;,
  9116. &quot;origin_server_ts&quot;: 1432735824653,
  9117. &quot;unsigned&quot;: {
  9118. &quot;age&quot;: 1234
  9119. },
  9120. &quot;state_key&quot;: &quot;@alice:example.org&quot;
  9121. }
  9122. ]
  9123. }
  9124. </code></pre>
  9125. <div style="break-before: page; page-break-before: always;"></div><h1 id="server-notices-1"><a class="header" href="#server-notices-1">Server Notices</a></h1>
  9126. <p>The API to send notices is as follows:</p>
  9127. <pre><code>POST /_synapse/admin/v1/send_server_notice
  9128. </code></pre>
  9129. <p>or:</p>
  9130. <pre><code>PUT /_synapse/admin/v1/send_server_notice/{txnId}
  9131. </code></pre>
  9132. <p>You will need to authenticate with an access token for an admin user.</p>
  9133. <p>When using the <code>PUT</code> form, retransmissions with the same transaction ID will be
  9134. ignored in the same way as with <code>PUT /_matrix/client/r0/rooms/{roomId}/send/{eventType}/{txnId}</code>.</p>
  9135. <p>The request body should look something like the following:</p>
  9136. <pre><code class="language-json">{
  9137. &quot;user_id&quot;: &quot;@target_user:server_name&quot;,
  9138. &quot;content&quot;: {
  9139. &quot;msgtype&quot;: &quot;m.text&quot;,
  9140. &quot;body&quot;: &quot;This is my message&quot;
  9141. }
  9142. }
  9143. </code></pre>
  9144. <p>You can optionally include the following additional parameters:</p>
  9145. <ul>
  9146. <li><code>type</code>: the type of event. Defaults to <code>m.room.message</code>.</li>
  9147. <li><code>state_key</code>: Setting this will result in a state event being sent.</li>
  9148. </ul>
  9149. <p>Once the notice has been sent, the API will return the following response:</p>
  9150. <pre><code class="language-json">{
  9151. &quot;event_id&quot;: &quot;&lt;event_id&gt;&quot;
  9152. }
  9153. </code></pre>
  9154. <p>Note that server notices must be enabled in <code>homeserver.yaml</code> before this API
  9155. can be used. See <a href="admin_api/../server_notices.html">server_notices.md</a> for more information.</p>
  9156. <div style="break-before: page; page-break-before: always;"></div><h1 id="deprecated-shutdown-room-api"><a class="header" href="#deprecated-shutdown-room-api">Deprecated: Shutdown room API</a></h1>
  9157. <p><strong>The old Shutdown room API is deprecated and will be removed in a future release.
  9158. See the new <a href="admin_api/rooms.html#delete-room-api">Delete Room API</a> for more details.</strong></p>
  9159. <p>Shuts down a room, preventing new joins and moves local users and room aliases automatically
  9160. to a new room. The new room will be created with the user specified by the
  9161. <code>new_room_user_id</code> parameter as room administrator and will contain a message
  9162. explaining what happened. Users invited to the new room will have power level
  9163. -10 by default, and thus be unable to speak. The old room's power levels will be changed to
  9164. disallow any further invites or joins.</p>
  9165. <p>The local server will only have the power to move local user and room aliases to
  9166. the new room. Users on other servers will be unaffected.</p>
  9167. <h2 id="api-1"><a class="header" href="#api-1">API</a></h2>
  9168. <p>You will need to authenticate with an access token for an admin user.</p>
  9169. <h3 id="url"><a class="header" href="#url">URL</a></h3>
  9170. <p><code>POST /_synapse/admin/v1/shutdown_room/{room_id}</code></p>
  9171. <h3 id="url-parameters"><a class="header" href="#url-parameters">URL Parameters</a></h3>
  9172. <ul>
  9173. <li><code>room_id</code> - The ID of the room (e.g <code>!someroom:example.com</code>)</li>
  9174. </ul>
  9175. <h3 id="json-body-parameters"><a class="header" href="#json-body-parameters">JSON Body Parameters</a></h3>
  9176. <ul>
  9177. <li><code>new_room_user_id</code> - Required. A string representing the user ID of the user that will admin
  9178. the new room that all users in the old room will be moved to.</li>
  9179. <li><code>room_name</code> - Optional. A string representing the name of the room that new users will be
  9180. invited to.</li>
  9181. <li><code>message</code> - Optional. A string containing the first message that will be sent as
  9182. <code>new_room_user_id</code> in the new room. Ideally this will clearly convey why the
  9183. original room was shut down.</li>
  9184. </ul>
  9185. <p>If not specified, the default value of <code>room_name</code> is &quot;Content Violation
  9186. Notification&quot;. The default value of <code>message</code> is &quot;Sharing illegal content on
  9187. othis server is not permitted and rooms in violation will be blocked.&quot;</p>
  9188. <h3 id="response-parameters"><a class="header" href="#response-parameters">Response Parameters</a></h3>
  9189. <ul>
  9190. <li><code>kicked_users</code> - An integer number representing the number of users that
  9191. were kicked.</li>
  9192. <li><code>failed_to_kick_users</code> - An integer number representing the number of users
  9193. that were not kicked.</li>
  9194. <li><code>local_aliases</code> - An array of strings representing the local aliases that were migrated from
  9195. the old room to the new.</li>
  9196. <li><code>new_room_id</code> - A string representing the room ID of the new room.</li>
  9197. </ul>
  9198. <h2 id="example-4"><a class="header" href="#example-4">Example</a></h2>
  9199. <p>Request:</p>
  9200. <pre><code>POST /_synapse/admin/v1/shutdown_room/!somebadroom%3Aexample.com
  9201. {
  9202. &quot;new_room_user_id&quot;: &quot;@someuser:example.com&quot;,
  9203. &quot;room_name&quot;: &quot;Content Violation Notification&quot;,
  9204. &quot;message&quot;: &quot;Bad Room has been shutdown due to content violations on this server. Please review our Terms of Service.&quot;
  9205. }
  9206. </code></pre>
  9207. <p>Response:</p>
  9208. <pre><code>{
  9209. &quot;kicked_users&quot;: 5,
  9210. &quot;failed_to_kick_users&quot;: 0,
  9211. &quot;local_aliases&quot;: [&quot;#badroom:example.com&quot;, &quot;#evilsaloon:example.com],
  9212. &quot;new_room_id&quot;: &quot;!newroomid:example.com&quot;,
  9213. },
  9214. </code></pre>
  9215. <h2 id="undoing-room-shutdowns-1"><a class="header" href="#undoing-room-shutdowns-1">Undoing room shutdowns</a></h2>
  9216. <p><em>Note</em>: This guide may be outdated by the time you read it. By nature of room shutdowns being performed at the database level,
  9217. the structure can and does change without notice.</p>
  9218. <p>First, it's important to understand that a room shutdown is very destructive. Undoing a shutdown is not as simple as pretending it
  9219. never happened - work has to be done to move forward instead of resetting the past. In fact, in some cases it might not be possible
  9220. to recover at all:</p>
  9221. <ul>
  9222. <li>If the room was invite-only, your users will need to be re-invited.</li>
  9223. <li>If the room no longer has any members at all, it'll be impossible to rejoin.</li>
  9224. <li>The first user to rejoin will have to do so via an alias on a different server.</li>
  9225. </ul>
  9226. <p>With all that being said, if you still want to try and recover the room:</p>
  9227. <ol>
  9228. <li>For safety reasons, shut down Synapse.</li>
  9229. <li>In the database, run <code>DELETE FROM blocked_rooms WHERE room_id = '!example:example.org';</code>
  9230. <ul>
  9231. <li>For caution: it's recommended to run this in a transaction: <code>BEGIN; DELETE ...;</code>, verify you got 1 result, then <code>COMMIT;</code>.</li>
  9232. <li>The room ID is the same one supplied to the shutdown room API, not the Content Violation room.</li>
  9233. </ul>
  9234. </li>
  9235. <li>Restart Synapse.</li>
  9236. </ol>
  9237. <p>You will have to manually handle, if you so choose, the following:</p>
  9238. <ul>
  9239. <li>Aliases that would have been redirected to the Content Violation room.</li>
  9240. <li>Users that would have been booted from the room (and will have been force-joined to the Content Violation room).</li>
  9241. <li>Removal of the Content Violation room if desired.</li>
  9242. </ul>
  9243. <div style="break-before: page; page-break-before: always;"></div><h1 id="users-media-usage-statistics"><a class="header" href="#users-media-usage-statistics">Users' media usage statistics</a></h1>
  9244. <p>Returns information about all local media usage of users. Gives the
  9245. possibility to filter them by time and user.</p>
  9246. <p>The API is:</p>
  9247. <pre><code>GET /_synapse/admin/v1/statistics/users/media
  9248. </code></pre>
  9249. <p>To use it, you will need to authenticate by providing an <code>access_token</code>
  9250. for a server admin: see <a href="admin_api/../usage/administration/admin_api">Admin API</a>.</p>
  9251. <p>A response body like the following is returned:</p>
  9252. <pre><code class="language-json">{
  9253. &quot;users&quot;: [
  9254. {
  9255. &quot;displayname&quot;: &quot;foo_user_0&quot;,
  9256. &quot;media_count&quot;: 2,
  9257. &quot;media_length&quot;: 134,
  9258. &quot;user_id&quot;: &quot;@foo_user_0:test&quot;
  9259. },
  9260. {
  9261. &quot;displayname&quot;: &quot;foo_user_1&quot;,
  9262. &quot;media_count&quot;: 2,
  9263. &quot;media_length&quot;: 134,
  9264. &quot;user_id&quot;: &quot;@foo_user_1:test&quot;
  9265. }
  9266. ],
  9267. &quot;next_token&quot;: 3,
  9268. &quot;total&quot;: 10
  9269. }
  9270. </code></pre>
  9271. <p>To paginate, check for <code>next_token</code> and if present, call the endpoint
  9272. again with <code>from</code> set to the value of <code>next_token</code>. This will return a new page.</p>
  9273. <p>If the endpoint does not return a <code>next_token</code> then there are no more
  9274. reports to paginate through.</p>
  9275. <p><strong>Parameters</strong></p>
  9276. <p>The following parameters should be set in the URL:</p>
  9277. <ul>
  9278. <li><code>limit</code>: string representing a positive integer - Is optional but is
  9279. used for pagination, denoting the maximum number of items to return
  9280. in this call. Defaults to <code>100</code>.</li>
  9281. <li><code>from</code>: string representing a positive integer - Is optional but used for pagination,
  9282. denoting the offset in the returned results. This should be treated as an opaque value
  9283. and not explicitly set to anything other than the return value of <code>next_token</code> from a
  9284. previous call. Defaults to <code>0</code>.</li>
  9285. <li><code>order_by</code> - string - The method in which to sort the returned list of users. Valid values are:
  9286. <ul>
  9287. <li><code>user_id</code> - Users are ordered alphabetically by <code>user_id</code>. This is the default.</li>
  9288. <li><code>displayname</code> - Users are ordered alphabetically by <code>displayname</code>.</li>
  9289. <li><code>media_length</code> - Users are ordered by the total size of uploaded media in bytes.
  9290. Smallest to largest.</li>
  9291. <li><code>media_count</code> - Users are ordered by number of uploaded media. Smallest to largest.</li>
  9292. </ul>
  9293. </li>
  9294. <li><code>from_ts</code> - string representing a positive integer - Considers only
  9295. files created at this timestamp or later. Unix timestamp in ms.</li>
  9296. <li><code>until_ts</code> - string representing a positive integer - Considers only
  9297. files created at this timestamp or earlier. Unix timestamp in ms.</li>
  9298. <li><code>search_term</code> - string - Filter users by their user ID localpart <strong>or</strong> displayname.
  9299. The search term can be found in any part of the string.
  9300. Defaults to no filtering.</li>
  9301. <li><code>dir</code> - string - Direction of order. Either <code>f</code> for forwards or <code>b</code> for backwards.
  9302. Setting this value to <code>b</code> will reverse the above sort order. Defaults to <code>f</code>.</li>
  9303. </ul>
  9304. <p><strong>Response</strong></p>
  9305. <p>The following fields are returned in the JSON response body:</p>
  9306. <ul>
  9307. <li><code>users</code> - An array of objects, each containing information
  9308. about the user and their local media. Objects contain the following fields:
  9309. <ul>
  9310. <li><code>displayname</code> - string - Displayname of this user.</li>
  9311. <li><code>media_count</code> - integer - Number of uploaded media by this user.</li>
  9312. <li><code>media_length</code> - integer - Size of uploaded media in bytes by this user.</li>
  9313. <li><code>user_id</code> - string - Fully-qualified user ID (ex. <code>@user:server.com</code>).</li>
  9314. </ul>
  9315. </li>
  9316. <li><code>next_token</code> - integer - Opaque value used for pagination. See above.</li>
  9317. <li><code>total</code> - integer - Total number of users after filtering.</li>
  9318. </ul>
  9319. <div style="break-before: page; page-break-before: always;"></div><h1 id="user-admin-api"><a class="header" href="#user-admin-api">User Admin API</a></h1>
  9320. <h2 id="query-user-account"><a class="header" href="#query-user-account">Query User Account</a></h2>
  9321. <p>This API returns information about a specific user account.</p>
  9322. <p>The api is:</p>
  9323. <pre><code>GET /_synapse/admin/v2/users/&lt;user_id&gt;
  9324. </code></pre>
  9325. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  9326. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  9327. <p>It returns a JSON body like the following:</p>
  9328. <pre><code class="language-json">{
  9329. &quot;displayname&quot;: &quot;User&quot;,
  9330. &quot;threepids&quot;: [
  9331. {
  9332. &quot;medium&quot;: &quot;email&quot;,
  9333. &quot;address&quot;: &quot;&lt;user_mail_1&gt;&quot;
  9334. },
  9335. {
  9336. &quot;medium&quot;: &quot;email&quot;,
  9337. &quot;address&quot;: &quot;&lt;user_mail_2&gt;&quot;
  9338. }
  9339. ],
  9340. &quot;avatar_url&quot;: &quot;&lt;avatar_url&gt;&quot;,
  9341. &quot;admin&quot;: 0,
  9342. &quot;deactivated&quot;: 0,
  9343. &quot;shadow_banned&quot;: 0,
  9344. &quot;password_hash&quot;: &quot;$2b$12$p9B4GkqYdRTPGD&quot;,
  9345. &quot;creation_ts&quot;: 1560432506,
  9346. &quot;appservice_id&quot;: null,
  9347. &quot;consent_server_notice_sent&quot;: null,
  9348. &quot;consent_version&quot;: null,
  9349. &quot;external_ids&quot;: [
  9350. {
  9351. &quot;auth_provider&quot;: &quot;&lt;provider1&gt;&quot;,
  9352. &quot;external_id&quot;: &quot;&lt;user_id_provider_1&gt;&quot;
  9353. },
  9354. {
  9355. &quot;auth_provider&quot;: &quot;&lt;provider2&gt;&quot;,
  9356. &quot;external_id&quot;: &quot;&lt;user_id_provider_2&gt;&quot;
  9357. }
  9358. ]
  9359. }
  9360. </code></pre>
  9361. <p>URL parameters:</p>
  9362. <ul>
  9363. <li><code>user_id</code>: fully-qualified user id: for example, <code>@user:server.com</code>.</li>
  9364. </ul>
  9365. <h2 id="create-or-modify-account"><a class="header" href="#create-or-modify-account">Create or modify Account</a></h2>
  9366. <p>This API allows an administrator to create or modify a user account with a
  9367. specific <code>user_id</code>.</p>
  9368. <p>This api is:</p>
  9369. <pre><code>PUT /_synapse/admin/v2/users/&lt;user_id&gt;
  9370. </code></pre>
  9371. <p>with a body of:</p>
  9372. <pre><code class="language-json">{
  9373. &quot;password&quot;: &quot;user_password&quot;,
  9374. &quot;displayname&quot;: &quot;User&quot;,
  9375. &quot;threepids&quot;: [
  9376. {
  9377. &quot;medium&quot;: &quot;email&quot;,
  9378. &quot;address&quot;: &quot;&lt;user_mail_1&gt;&quot;
  9379. },
  9380. {
  9381. &quot;medium&quot;: &quot;email&quot;,
  9382. &quot;address&quot;: &quot;&lt;user_mail_2&gt;&quot;
  9383. }
  9384. ],
  9385. &quot;avatar_url&quot;: &quot;&lt;avatar_url&gt;&quot;,
  9386. &quot;admin&quot;: false,
  9387. &quot;deactivated&quot;: false
  9388. }
  9389. </code></pre>
  9390. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  9391. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  9392. <p>URL parameters:</p>
  9393. <ul>
  9394. <li><code>user_id</code>: fully-qualified user id: for example, <code>@user:server.com</code>.</li>
  9395. </ul>
  9396. <p>Body parameters:</p>
  9397. <ul>
  9398. <li>
  9399. <p><code>password</code>, optional. If provided, the user's password is updated and all
  9400. devices are logged out.</p>
  9401. </li>
  9402. <li>
  9403. <p><code>displayname</code>, optional, defaults to the value of <code>user_id</code>.</p>
  9404. </li>
  9405. <li>
  9406. <p><code>threepids</code>, optional, allows setting the third-party IDs (email, msisdn)
  9407. belonging to a user.</p>
  9408. </li>
  9409. <li>
  9410. <p><code>avatar_url</code>, optional, must be a
  9411. <a href="https://matrix.org/docs/spec/client_server/r0.6.0#matrix-content-mxc-uris">MXC URI</a>.</p>
  9412. </li>
  9413. <li>
  9414. <p><code>admin</code>, optional, defaults to <code>false</code>.</p>
  9415. </li>
  9416. <li>
  9417. <p><code>deactivated</code>, optional. If unspecified, deactivation state will be left
  9418. unchanged on existing accounts and set to <code>false</code> for new accounts.
  9419. A user cannot be erased by deactivating with this API. For details on
  9420. deactivating users see <a href="admin_api/user_admin_api.html#deactivate-account">Deactivate Account</a>.</p>
  9421. </li>
  9422. </ul>
  9423. <p>If the user already exists then optional parameters default to the current value.</p>
  9424. <p>In order to re-activate an account <code>deactivated</code> must be set to <code>false</code>. If
  9425. users do not login via single-sign-on, a new <code>password</code> must be provided.</p>
  9426. <h2 id="list-accounts"><a class="header" href="#list-accounts">List Accounts</a></h2>
  9427. <p>This API returns all local user accounts.
  9428. By default, the response is ordered by ascending user ID.</p>
  9429. <pre><code>GET /_synapse/admin/v2/users?from=0&amp;limit=10&amp;guests=false
  9430. </code></pre>
  9431. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  9432. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  9433. <p>A response body like the following is returned:</p>
  9434. <pre><code class="language-json">{
  9435. &quot;users&quot;: [
  9436. {
  9437. &quot;name&quot;: &quot;&lt;user_id1&gt;&quot;,
  9438. &quot;is_guest&quot;: 0,
  9439. &quot;admin&quot;: 0,
  9440. &quot;user_type&quot;: null,
  9441. &quot;deactivated&quot;: 0,
  9442. &quot;shadow_banned&quot;: 0,
  9443. &quot;displayname&quot;: &quot;&lt;User One&gt;&quot;,
  9444. &quot;avatar_url&quot;: null
  9445. }, {
  9446. &quot;name&quot;: &quot;&lt;user_id2&gt;&quot;,
  9447. &quot;is_guest&quot;: 0,
  9448. &quot;admin&quot;: 1,
  9449. &quot;user_type&quot;: null,
  9450. &quot;deactivated&quot;: 0,
  9451. &quot;shadow_banned&quot;: 0,
  9452. &quot;displayname&quot;: &quot;&lt;User Two&gt;&quot;,
  9453. &quot;avatar_url&quot;: &quot;&lt;avatar_url&gt;&quot;
  9454. }
  9455. ],
  9456. &quot;next_token&quot;: &quot;100&quot;,
  9457. &quot;total&quot;: 200
  9458. }
  9459. </code></pre>
  9460. <p>To paginate, check for <code>next_token</code> and if present, call the endpoint again
  9461. with <code>from</code> set to the value of <code>next_token</code>. This will return a new page.</p>
  9462. <p>If the endpoint does not return a <code>next_token</code> then there are no more users
  9463. to paginate through.</p>
  9464. <p><strong>Parameters</strong></p>
  9465. <p>The following parameters should be set in the URL:</p>
  9466. <ul>
  9467. <li>
  9468. <p><code>user_id</code> - Is optional and filters to only return users with user IDs
  9469. that contain this value. This parameter is ignored when using the <code>name</code> parameter.</p>
  9470. </li>
  9471. <li>
  9472. <p><code>name</code> - Is optional and filters to only return users with user ID localparts
  9473. <strong>or</strong> displaynames that contain this value.</p>
  9474. </li>
  9475. <li>
  9476. <p><code>guests</code> - string representing a bool - Is optional and if <code>false</code> will <strong>exclude</strong> guest users.
  9477. Defaults to <code>true</code> to include guest users.</p>
  9478. </li>
  9479. <li>
  9480. <p><code>deactivated</code> - string representing a bool - Is optional and if <code>true</code> will <strong>include</strong> deactivated users.
  9481. Defaults to <code>false</code> to exclude deactivated users.</p>
  9482. </li>
  9483. <li>
  9484. <p><code>limit</code> - string representing a positive integer - Is optional but is used for pagination,
  9485. denoting the maximum number of items to return in this call. Defaults to <code>100</code>.</p>
  9486. </li>
  9487. <li>
  9488. <p><code>from</code> - string representing a positive integer - Is optional but used for pagination,
  9489. denoting the offset in the returned results. This should be treated as an opaque value and
  9490. not explicitly set to anything other than the return value of <code>next_token</code> from a previous call.
  9491. Defaults to <code>0</code>.</p>
  9492. </li>
  9493. <li>
  9494. <p><code>order_by</code> - The method by which to sort the returned list of users.
  9495. If the ordered field has duplicates, the second order is always by ascending <code>name</code>,
  9496. which guarantees a stable ordering. Valid values are:</p>
  9497. <ul>
  9498. <li><code>name</code> - Users are ordered alphabetically by <code>name</code>. This is the default.</li>
  9499. <li><code>is_guest</code> - Users are ordered by <code>is_guest</code> status.</li>
  9500. <li><code>admin</code> - Users are ordered by <code>admin</code> status.</li>
  9501. <li><code>user_type</code> - Users are ordered alphabetically by <code>user_type</code>.</li>
  9502. <li><code>deactivated</code> - Users are ordered by <code>deactivated</code> status.</li>
  9503. <li><code>shadow_banned</code> - Users are ordered by <code>shadow_banned</code> status.</li>
  9504. <li><code>displayname</code> - Users are ordered alphabetically by <code>displayname</code>.</li>
  9505. <li><code>avatar_url</code> - Users are ordered alphabetically by avatar URL.</li>
  9506. </ul>
  9507. </li>
  9508. <li>
  9509. <p><code>dir</code> - Direction of media order. Either <code>f</code> for forwards or <code>b</code> for backwards.
  9510. Setting this value to <code>b</code> will reverse the above sort order. Defaults to <code>f</code>.</p>
  9511. </li>
  9512. </ul>
  9513. <p>Caution. The database only has indexes on the columns <code>name</code> and <code>created_ts</code>.
  9514. This means that if a different sort order is used (<code>is_guest</code>, <code>admin</code>,
  9515. <code>user_type</code>, <code>deactivated</code>, <code>shadow_banned</code>, <code>avatar_url</code> or <code>displayname</code>),
  9516. this can cause a large load on the database, especially for large environments.</p>
  9517. <p><strong>Response</strong></p>
  9518. <p>The following fields are returned in the JSON response body:</p>
  9519. <ul>
  9520. <li>
  9521. <p><code>users</code> - An array of objects, each containing information about an user.
  9522. User objects contain the following fields:</p>
  9523. <ul>
  9524. <li><code>name</code> - string - Fully-qualified user ID (ex. <code>@user:server.com</code>).</li>
  9525. <li><code>is_guest</code> - bool - Status if that user is a guest account.</li>
  9526. <li><code>admin</code> - bool - Status if that user is a server administrator.</li>
  9527. <li><code>user_type</code> - string - Type of the user. Normal users are type <code>None</code>.
  9528. This allows user type specific behaviour. There are also types <code>support</code> and <code>bot</code>. </li>
  9529. <li><code>deactivated</code> - bool - Status if that user has been marked as deactivated.</li>
  9530. <li><code>shadow_banned</code> - bool - Status if that user has been marked as shadow banned.</li>
  9531. <li><code>displayname</code> - string - The user's display name if they have set one.</li>
  9532. <li><code>avatar_url</code> - string - The user's avatar URL if they have set one.</li>
  9533. </ul>
  9534. </li>
  9535. <li>
  9536. <p><code>next_token</code>: string representing a positive integer - Indication for pagination. See above.</p>
  9537. </li>
  9538. <li>
  9539. <p><code>total</code> - integer - Total number of media.</p>
  9540. </li>
  9541. </ul>
  9542. <h2 id="query-current-sessions-for-a-user"><a class="header" href="#query-current-sessions-for-a-user">Query current sessions for a user</a></h2>
  9543. <p>This API returns information about the active sessions for a specific user.</p>
  9544. <p>The endpoints are:</p>
  9545. <pre><code>GET /_synapse/admin/v1/whois/&lt;user_id&gt;
  9546. </code></pre>
  9547. <p>and:</p>
  9548. <pre><code>GET /_matrix/client/r0/admin/whois/&lt;userId&gt;
  9549. </code></pre>
  9550. <p>See also: <a href="https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-admin-whois-userid">Client Server
  9551. API Whois</a>.</p>
  9552. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  9553. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  9554. <p>It returns a JSON body like the following:</p>
  9555. <pre><code class="language-json">{
  9556. &quot;user_id&quot;: &quot;&lt;user_id&gt;&quot;,
  9557. &quot;devices&quot;: {
  9558. &quot;&quot;: {
  9559. &quot;sessions&quot;: [
  9560. {
  9561. &quot;connections&quot;: [
  9562. {
  9563. &quot;ip&quot;: &quot;1.2.3.4&quot;,
  9564. &quot;last_seen&quot;: 1417222374433,
  9565. &quot;user_agent&quot;: &quot;Mozilla/5.0 ...&quot;
  9566. },
  9567. {
  9568. &quot;ip&quot;: &quot;1.2.3.10&quot;,
  9569. &quot;last_seen&quot;: 1417222374500,
  9570. &quot;user_agent&quot;: &quot;Dalvik/2.1.0 ...&quot;
  9571. }
  9572. ]
  9573. }
  9574. ]
  9575. }
  9576. }
  9577. }
  9578. </code></pre>
  9579. <p><code>last_seen</code> is measured in milliseconds since the Unix epoch.</p>
  9580. <h2 id="deactivate-account"><a class="header" href="#deactivate-account">Deactivate Account</a></h2>
  9581. <p>This API deactivates an account. It removes active access tokens, resets the
  9582. password, and deletes third-party IDs (to prevent the user requesting a
  9583. password reset).</p>
  9584. <p>It can also mark the user as GDPR-erased. This means messages sent by the
  9585. user will still be visible by anyone that was in the room when these messages
  9586. were sent, but hidden from users joining the room afterwards.</p>
  9587. <p>The api is:</p>
  9588. <pre><code>POST /_synapse/admin/v1/deactivate/&lt;user_id&gt;
  9589. </code></pre>
  9590. <p>with a body of:</p>
  9591. <pre><code class="language-json">{
  9592. &quot;erase&quot;: true
  9593. }
  9594. </code></pre>
  9595. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  9596. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  9597. <p>The erase parameter is optional and defaults to <code>false</code>.
  9598. An empty body may be passed for backwards compatibility.</p>
  9599. <p>The following actions are performed when deactivating an user:</p>
  9600. <ul>
  9601. <li>Try to unpind 3PIDs from the identity server</li>
  9602. <li>Remove all 3PIDs from the homeserver</li>
  9603. <li>Delete all devices and E2EE keys</li>
  9604. <li>Delete all access tokens</li>
  9605. <li>Delete the password hash</li>
  9606. <li>Removal from all rooms the user is a member of</li>
  9607. <li>Remove the user from the user directory</li>
  9608. <li>Reject all pending invites</li>
  9609. <li>Remove all account validity information related to the user</li>
  9610. </ul>
  9611. <p>The following additional actions are performed during deactivation if <code>erase</code>
  9612. is set to <code>true</code>:</p>
  9613. <ul>
  9614. <li>Remove the user's display name</li>
  9615. <li>Remove the user's avatar URL</li>
  9616. <li>Mark the user as erased</li>
  9617. </ul>
  9618. <h2 id="reset-password"><a class="header" href="#reset-password">Reset password</a></h2>
  9619. <p>Changes the password of another user. This will automatically log the user out of all their devices.</p>
  9620. <p>The api is:</p>
  9621. <pre><code>POST /_synapse/admin/v1/reset_password/&lt;user_id&gt;
  9622. </code></pre>
  9623. <p>with a body of:</p>
  9624. <pre><code class="language-json">{
  9625. &quot;new_password&quot;: &quot;&lt;secret&gt;&quot;,
  9626. &quot;logout_devices&quot;: true
  9627. }
  9628. </code></pre>
  9629. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  9630. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  9631. <p>The parameter <code>new_password</code> is required.
  9632. The parameter <code>logout_devices</code> is optional and defaults to <code>true</code>.</p>
  9633. <h2 id="get-whether-a-user-is-a-server-administrator-or-not"><a class="header" href="#get-whether-a-user-is-a-server-administrator-or-not">Get whether a user is a server administrator or not</a></h2>
  9634. <p>The api is:</p>
  9635. <pre><code>GET /_synapse/admin/v1/users/&lt;user_id&gt;/admin
  9636. </code></pre>
  9637. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  9638. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  9639. <p>A response body like the following is returned:</p>
  9640. <pre><code class="language-json">{
  9641. &quot;admin&quot;: true
  9642. }
  9643. </code></pre>
  9644. <h2 id="change-whether-a-user-is-a-server-administrator-or-not"><a class="header" href="#change-whether-a-user-is-a-server-administrator-or-not">Change whether a user is a server administrator or not</a></h2>
  9645. <p>Note that you cannot demote yourself.</p>
  9646. <p>The api is:</p>
  9647. <pre><code>PUT /_synapse/admin/v1/users/&lt;user_id&gt;/admin
  9648. </code></pre>
  9649. <p>with a body of:</p>
  9650. <pre><code class="language-json">{
  9651. &quot;admin&quot;: true
  9652. }
  9653. </code></pre>
  9654. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  9655. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  9656. <h2 id="list-room-memberships-of-a-user"><a class="header" href="#list-room-memberships-of-a-user">List room memberships of a user</a></h2>
  9657. <p>Gets a list of all <code>room_id</code> that a specific <code>user_id</code> is member.</p>
  9658. <p>The API is:</p>
  9659. <pre><code>GET /_synapse/admin/v1/users/&lt;user_id&gt;/joined_rooms
  9660. </code></pre>
  9661. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  9662. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  9663. <p>A response body like the following is returned:</p>
  9664. <pre><code class="language-json"> {
  9665. &quot;joined_rooms&quot;: [
  9666. &quot;!DuGcnbhHGaSZQoNQR:matrix.org&quot;,
  9667. &quot;!ZtSaPCawyWtxfWiIy:matrix.org&quot;
  9668. ],
  9669. &quot;total&quot;: 2
  9670. }
  9671. </code></pre>
  9672. <p>The server returns the list of rooms of which the user and the server
  9673. are member. If the user is local, all the rooms of which the user is
  9674. member are returned.</p>
  9675. <p><strong>Parameters</strong></p>
  9676. <p>The following parameters should be set in the URL:</p>
  9677. <ul>
  9678. <li><code>user_id</code> - fully qualified: for example, <code>@user:server.com</code>.</li>
  9679. </ul>
  9680. <p><strong>Response</strong></p>
  9681. <p>The following fields are returned in the JSON response body:</p>
  9682. <ul>
  9683. <li><code>joined_rooms</code> - An array of <code>room_id</code>.</li>
  9684. <li><code>total</code> - Number of rooms.</li>
  9685. </ul>
  9686. <h2 id="list-media-of-a-user"><a class="header" href="#list-media-of-a-user">List media of a user</a></h2>
  9687. <p>Gets a list of all local media that a specific <code>user_id</code> has created.
  9688. By default, the response is ordered by descending creation date and ascending media ID.
  9689. The newest media is on top. You can change the order with parameters
  9690. <code>order_by</code> and <code>dir</code>.</p>
  9691. <p>The API is:</p>
  9692. <pre><code>GET /_synapse/admin/v1/users/&lt;user_id&gt;/media
  9693. </code></pre>
  9694. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  9695. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  9696. <p>A response body like the following is returned:</p>
  9697. <pre><code class="language-json">{
  9698. &quot;media&quot;: [
  9699. {
  9700. &quot;created_ts&quot;: 100400,
  9701. &quot;last_access_ts&quot;: null,
  9702. &quot;media_id&quot;: &quot;qXhyRzulkwLsNHTbpHreuEgo&quot;,
  9703. &quot;media_length&quot;: 67,
  9704. &quot;media_type&quot;: &quot;image/png&quot;,
  9705. &quot;quarantined_by&quot;: null,
  9706. &quot;safe_from_quarantine&quot;: false,
  9707. &quot;upload_name&quot;: &quot;test1.png&quot;
  9708. },
  9709. {
  9710. &quot;created_ts&quot;: 200400,
  9711. &quot;last_access_ts&quot;: null,
  9712. &quot;media_id&quot;: &quot;FHfiSnzoINDatrXHQIXBtahw&quot;,
  9713. &quot;media_length&quot;: 67,
  9714. &quot;media_type&quot;: &quot;image/png&quot;,
  9715. &quot;quarantined_by&quot;: null,
  9716. &quot;safe_from_quarantine&quot;: false,
  9717. &quot;upload_name&quot;: &quot;test2.png&quot;
  9718. }
  9719. ],
  9720. &quot;next_token&quot;: 3,
  9721. &quot;total&quot;: 2
  9722. }
  9723. </code></pre>
  9724. <p>To paginate, check for <code>next_token</code> and if present, call the endpoint again
  9725. with <code>from</code> set to the value of <code>next_token</code>. This will return a new page.</p>
  9726. <p>If the endpoint does not return a <code>next_token</code> then there are no more
  9727. reports to paginate through.</p>
  9728. <p><strong>Parameters</strong></p>
  9729. <p>The following parameters should be set in the URL:</p>
  9730. <ul>
  9731. <li>
  9732. <p><code>user_id</code> - string - fully qualified: for example, <code>@user:server.com</code>.</p>
  9733. </li>
  9734. <li>
  9735. <p><code>limit</code>: string representing a positive integer - Is optional but is used for pagination,
  9736. denoting the maximum number of items to return in this call. Defaults to <code>100</code>.</p>
  9737. </li>
  9738. <li>
  9739. <p><code>from</code>: string representing a positive integer - Is optional but used for pagination,
  9740. denoting the offset in the returned results. This should be treated as an opaque value and
  9741. not explicitly set to anything other than the return value of <code>next_token</code> from a previous call.
  9742. Defaults to <code>0</code>.</p>
  9743. </li>
  9744. <li>
  9745. <p><code>order_by</code> - The method by which to sort the returned list of media.
  9746. If the ordered field has duplicates, the second order is always by ascending <code>media_id</code>,
  9747. which guarantees a stable ordering. Valid values are:</p>
  9748. <ul>
  9749. <li><code>media_id</code> - Media are ordered alphabetically by <code>media_id</code>.</li>
  9750. <li><code>upload_name</code> - Media are ordered alphabetically by name the media was uploaded with.</li>
  9751. <li><code>created_ts</code> - Media are ordered by when the content was uploaded in ms.
  9752. Smallest to largest. This is the default.</li>
  9753. <li><code>last_access_ts</code> - Media are ordered by when the content was last accessed in ms.
  9754. Smallest to largest.</li>
  9755. <li><code>media_length</code> - Media are ordered by length of the media in bytes.
  9756. Smallest to largest.</li>
  9757. <li><code>media_type</code> - Media are ordered alphabetically by MIME-type.</li>
  9758. <li><code>quarantined_by</code> - Media are ordered alphabetically by the user ID that
  9759. initiated the quarantine request for this media.</li>
  9760. <li><code>safe_from_quarantine</code> - Media are ordered by the status if this media is safe
  9761. from quarantining.</li>
  9762. </ul>
  9763. </li>
  9764. <li>
  9765. <p><code>dir</code> - Direction of media order. Either <code>f</code> for forwards or <code>b</code> for backwards.
  9766. Setting this value to <code>b</code> will reverse the above sort order. Defaults to <code>f</code>.</p>
  9767. </li>
  9768. </ul>
  9769. <p>If neither <code>order_by</code> nor <code>dir</code> is set, the default order is newest media on top
  9770. (corresponds to <code>order_by</code> = <code>created_ts</code> and <code>dir</code> = <code>b</code>).</p>
  9771. <p>Caution. The database only has indexes on the columns <code>media_id</code>,
  9772. <code>user_id</code> and <code>created_ts</code>. This means that if a different sort order is used
  9773. (<code>upload_name</code>, <code>last_access_ts</code>, <code>media_length</code>, <code>media_type</code>,
  9774. <code>quarantined_by</code> or <code>safe_from_quarantine</code>), this can cause a large load on the
  9775. database, especially for large environments.</p>
  9776. <p><strong>Response</strong></p>
  9777. <p>The following fields are returned in the JSON response body:</p>
  9778. <ul>
  9779. <li>
  9780. <p><code>media</code> - An array of objects, each containing information about a media.
  9781. Media objects contain the following fields:</p>
  9782. <ul>
  9783. <li>
  9784. <p><code>created_ts</code> - integer - Timestamp when the content was uploaded in ms.</p>
  9785. </li>
  9786. <li>
  9787. <p><code>last_access_ts</code> - integer - Timestamp when the content was last accessed in ms.</p>
  9788. </li>
  9789. <li>
  9790. <p><code>media_id</code> - string - The id used to refer to the media.</p>
  9791. </li>
  9792. <li>
  9793. <p><code>media_length</code> - integer - Length of the media in bytes.</p>
  9794. </li>
  9795. <li>
  9796. <p><code>media_type</code> - string - The MIME-type of the media.</p>
  9797. </li>
  9798. <li>
  9799. <p><code>quarantined_by</code> - string - The user ID that initiated the quarantine request
  9800. for this media.</p>
  9801. </li>
  9802. <li>
  9803. <p><code>safe_from_quarantine</code> - bool - Status if this media is safe from quarantining.</p>
  9804. </li>
  9805. <li>
  9806. <p><code>upload_name</code> - string - The name the media was uploaded with.</p>
  9807. </li>
  9808. </ul>
  9809. </li>
  9810. <li>
  9811. <p><code>next_token</code>: integer - Indication for pagination. See above.</p>
  9812. </li>
  9813. <li>
  9814. <p><code>total</code> - integer - Total number of media.</p>
  9815. </li>
  9816. </ul>
  9817. <h2 id="login-as-a-user"><a class="header" href="#login-as-a-user">Login as a user</a></h2>
  9818. <p>Get an access token that can be used to authenticate as that user. Useful for
  9819. when admins wish to do actions on behalf of a user.</p>
  9820. <p>The API is:</p>
  9821. <pre><code>POST /_synapse/admin/v1/users/&lt;user_id&gt;/login
  9822. {}
  9823. </code></pre>
  9824. <p>An optional <code>valid_until_ms</code> field can be specified in the request body as an
  9825. integer timestamp that specifies when the token should expire. By default tokens
  9826. do not expire.</p>
  9827. <p>A response body like the following is returned:</p>
  9828. <pre><code class="language-json">{
  9829. &quot;access_token&quot;: &quot;&lt;opaque_access_token_string&gt;&quot;
  9830. }
  9831. </code></pre>
  9832. <p>This API does <em>not</em> generate a new device for the user, and so will not appear
  9833. their <code>/devices</code> list, and in general the target user should not be able to
  9834. tell they have been logged in as.</p>
  9835. <p>To expire the token call the standard <code>/logout</code> API with the token.</p>
  9836. <p>Note: The token will expire if the <em>admin</em> user calls <code>/logout/all</code> from any
  9837. of their devices, but the token will <em>not</em> expire if the target user does the
  9838. same.</p>
  9839. <h2 id="user-devices"><a class="header" href="#user-devices">User devices</a></h2>
  9840. <h3 id="list-all-devices"><a class="header" href="#list-all-devices">List all devices</a></h3>
  9841. <p>Gets information about all devices for a specific <code>user_id</code>.</p>
  9842. <p>The API is:</p>
  9843. <pre><code>GET /_synapse/admin/v2/users/&lt;user_id&gt;/devices
  9844. </code></pre>
  9845. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  9846. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  9847. <p>A response body like the following is returned:</p>
  9848. <pre><code class="language-json">{
  9849. &quot;devices&quot;: [
  9850. {
  9851. &quot;device_id&quot;: &quot;QBUAZIFURK&quot;,
  9852. &quot;display_name&quot;: &quot;android&quot;,
  9853. &quot;last_seen_ip&quot;: &quot;1.2.3.4&quot;,
  9854. &quot;last_seen_ts&quot;: 1474491775024,
  9855. &quot;user_id&quot;: &quot;&lt;user_id&gt;&quot;
  9856. },
  9857. {
  9858. &quot;device_id&quot;: &quot;AUIECTSRND&quot;,
  9859. &quot;display_name&quot;: &quot;ios&quot;,
  9860. &quot;last_seen_ip&quot;: &quot;1.2.3.5&quot;,
  9861. &quot;last_seen_ts&quot;: 1474491775025,
  9862. &quot;user_id&quot;: &quot;&lt;user_id&gt;&quot;
  9863. }
  9864. ],
  9865. &quot;total&quot;: 2
  9866. }
  9867. </code></pre>
  9868. <p><strong>Parameters</strong></p>
  9869. <p>The following parameters should be set in the URL:</p>
  9870. <ul>
  9871. <li><code>user_id</code> - fully qualified: for example, <code>@user:server.com</code>.</li>
  9872. </ul>
  9873. <p><strong>Response</strong></p>
  9874. <p>The following fields are returned in the JSON response body:</p>
  9875. <ul>
  9876. <li>
  9877. <p><code>devices</code> - An array of objects, each containing information about a device.
  9878. Device objects contain the following fields:</p>
  9879. <ul>
  9880. <li><code>device_id</code> - Identifier of device.</li>
  9881. <li><code>display_name</code> - Display name set by the user for this device.
  9882. Absent if no name has been set.</li>
  9883. <li><code>last_seen_ip</code> - The IP address where this device was last seen.
  9884. (May be a few minutes out of date, for efficiency reasons).</li>
  9885. <li><code>last_seen_ts</code> - The timestamp (in milliseconds since the unix epoch) when this
  9886. devices was last seen. (May be a few minutes out of date, for efficiency reasons).</li>
  9887. <li><code>user_id</code> - Owner of device.</li>
  9888. </ul>
  9889. </li>
  9890. <li>
  9891. <p><code>total</code> - Total number of user's devices.</p>
  9892. </li>
  9893. </ul>
  9894. <h3 id="delete-multiple-devices"><a class="header" href="#delete-multiple-devices">Delete multiple devices</a></h3>
  9895. <p>Deletes the given devices for a specific <code>user_id</code>, and invalidates
  9896. any access token associated with them.</p>
  9897. <p>The API is:</p>
  9898. <pre><code>POST /_synapse/admin/v2/users/&lt;user_id&gt;/delete_devices
  9899. {
  9900. &quot;devices&quot;: [
  9901. &quot;QBUAZIFURK&quot;,
  9902. &quot;AUIECTSRND&quot;
  9903. ],
  9904. }
  9905. </code></pre>
  9906. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  9907. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  9908. <p>An empty JSON dict is returned.</p>
  9909. <p><strong>Parameters</strong></p>
  9910. <p>The following parameters should be set in the URL:</p>
  9911. <ul>
  9912. <li><code>user_id</code> - fully qualified: for example, <code>@user:server.com</code>.</li>
  9913. </ul>
  9914. <p>The following fields are required in the JSON request body:</p>
  9915. <ul>
  9916. <li><code>devices</code> - The list of device IDs to delete.</li>
  9917. </ul>
  9918. <h3 id="show-a-device"><a class="header" href="#show-a-device">Show a device</a></h3>
  9919. <p>Gets information on a single device, by <code>device_id</code> for a specific <code>user_id</code>.</p>
  9920. <p>The API is:</p>
  9921. <pre><code>GET /_synapse/admin/v2/users/&lt;user_id&gt;/devices/&lt;device_id&gt;
  9922. </code></pre>
  9923. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  9924. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  9925. <p>A response body like the following is returned:</p>
  9926. <pre><code class="language-json">{
  9927. &quot;device_id&quot;: &quot;&lt;device_id&gt;&quot;,
  9928. &quot;display_name&quot;: &quot;android&quot;,
  9929. &quot;last_seen_ip&quot;: &quot;1.2.3.4&quot;,
  9930. &quot;last_seen_ts&quot;: 1474491775024,
  9931. &quot;user_id&quot;: &quot;&lt;user_id&gt;&quot;
  9932. }
  9933. </code></pre>
  9934. <p><strong>Parameters</strong></p>
  9935. <p>The following parameters should be set in the URL:</p>
  9936. <ul>
  9937. <li><code>user_id</code> - fully qualified: for example, <code>@user:server.com</code>.</li>
  9938. <li><code>device_id</code> - The device to retrieve.</li>
  9939. </ul>
  9940. <p><strong>Response</strong></p>
  9941. <p>The following fields are returned in the JSON response body:</p>
  9942. <ul>
  9943. <li><code>device_id</code> - Identifier of device.</li>
  9944. <li><code>display_name</code> - Display name set by the user for this device.
  9945. Absent if no name has been set.</li>
  9946. <li><code>last_seen_ip</code> - The IP address where this device was last seen.
  9947. (May be a few minutes out of date, for efficiency reasons).</li>
  9948. <li><code>last_seen_ts</code> - The timestamp (in milliseconds since the unix epoch) when this
  9949. devices was last seen. (May be a few minutes out of date, for efficiency reasons).</li>
  9950. <li><code>user_id</code> - Owner of device.</li>
  9951. </ul>
  9952. <h3 id="update-a-device"><a class="header" href="#update-a-device">Update a device</a></h3>
  9953. <p>Updates the metadata on the given <code>device_id</code> for a specific <code>user_id</code>.</p>
  9954. <p>The API is:</p>
  9955. <pre><code>PUT /_synapse/admin/v2/users/&lt;user_id&gt;/devices/&lt;device_id&gt;
  9956. {
  9957. &quot;display_name&quot;: &quot;My other phone&quot;
  9958. }
  9959. </code></pre>
  9960. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  9961. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  9962. <p>An empty JSON dict is returned.</p>
  9963. <p><strong>Parameters</strong></p>
  9964. <p>The following parameters should be set in the URL:</p>
  9965. <ul>
  9966. <li><code>user_id</code> - fully qualified: for example, <code>@user:server.com</code>.</li>
  9967. <li><code>device_id</code> - The device to update.</li>
  9968. </ul>
  9969. <p>The following fields are required in the JSON request body:</p>
  9970. <ul>
  9971. <li><code>display_name</code> - The new display name for this device. If not given,
  9972. the display name is unchanged.</li>
  9973. </ul>
  9974. <h3 id="delete-a-device"><a class="header" href="#delete-a-device">Delete a device</a></h3>
  9975. <p>Deletes the given <code>device_id</code> for a specific <code>user_id</code>,
  9976. and invalidates any access token associated with it.</p>
  9977. <p>The API is:</p>
  9978. <pre><code>DELETE /_synapse/admin/v2/users/&lt;user_id&gt;/devices/&lt;device_id&gt;
  9979. {}
  9980. </code></pre>
  9981. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  9982. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  9983. <p>An empty JSON dict is returned.</p>
  9984. <p><strong>Parameters</strong></p>
  9985. <p>The following parameters should be set in the URL:</p>
  9986. <ul>
  9987. <li><code>user_id</code> - fully qualified: for example, <code>@user:server.com</code>.</li>
  9988. <li><code>device_id</code> - The device to delete.</li>
  9989. </ul>
  9990. <h2 id="list-all-pushers"><a class="header" href="#list-all-pushers">List all pushers</a></h2>
  9991. <p>Gets information about all pushers for a specific <code>user_id</code>.</p>
  9992. <p>The API is:</p>
  9993. <pre><code>GET /_synapse/admin/v1/users/&lt;user_id&gt;/pushers
  9994. </code></pre>
  9995. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  9996. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  9997. <p>A response body like the following is returned:</p>
  9998. <pre><code class="language-json">{
  9999. &quot;pushers&quot;: [
  10000. {
  10001. &quot;app_display_name&quot;:&quot;HTTP Push Notifications&quot;,
  10002. &quot;app_id&quot;:&quot;m.http&quot;,
  10003. &quot;data&quot;: {
  10004. &quot;url&quot;:&quot;example.com&quot;
  10005. },
  10006. &quot;device_display_name&quot;:&quot;pushy push&quot;,
  10007. &quot;kind&quot;:&quot;http&quot;,
  10008. &quot;lang&quot;:&quot;None&quot;,
  10009. &quot;profile_tag&quot;:&quot;&quot;,
  10010. &quot;pushkey&quot;:&quot;a@example.com&quot;
  10011. }
  10012. ],
  10013. &quot;total&quot;: 1
  10014. }
  10015. </code></pre>
  10016. <p><strong>Parameters</strong></p>
  10017. <p>The following parameters should be set in the URL:</p>
  10018. <ul>
  10019. <li><code>user_id</code> - fully qualified: for example, <code>@user:server.com</code>.</li>
  10020. </ul>
  10021. <p><strong>Response</strong></p>
  10022. <p>The following fields are returned in the JSON response body:</p>
  10023. <ul>
  10024. <li>
  10025. <p><code>pushers</code> - An array containing the current pushers for the user</p>
  10026. <ul>
  10027. <li>
  10028. <p><code>app_display_name</code> - string - A string that will allow the user to identify
  10029. what application owns this pusher.</p>
  10030. </li>
  10031. <li>
  10032. <p><code>app_id</code> - string - This is a reverse-DNS style identifier for the application.
  10033. Max length, 64 chars.</p>
  10034. </li>
  10035. <li>
  10036. <p><code>data</code> - A dictionary of information for the pusher implementation itself.</p>
  10037. <ul>
  10038. <li>
  10039. <p><code>url</code> - string - Required if <code>kind</code> is <code>http</code>. The URL to use to send
  10040. notifications to.</p>
  10041. </li>
  10042. <li>
  10043. <p><code>format</code> - string - The format to use when sending notifications to the
  10044. Push Gateway.</p>
  10045. </li>
  10046. </ul>
  10047. </li>
  10048. <li>
  10049. <p><code>device_display_name</code> - string - A string that will allow the user to identify
  10050. what device owns this pusher.</p>
  10051. </li>
  10052. <li>
  10053. <p><code>profile_tag</code> - string - This string determines which set of device specific rules
  10054. this pusher executes.</p>
  10055. </li>
  10056. <li>
  10057. <p><code>kind</code> - string - The kind of pusher. &quot;http&quot; is a pusher that sends HTTP pokes.</p>
  10058. </li>
  10059. <li>
  10060. <p><code>lang</code> - string - The preferred language for receiving notifications
  10061. (e.g. 'en' or 'en-US')</p>
  10062. </li>
  10063. <li>
  10064. <p><code>profile_tag</code> - string - This string determines which set of device specific rules
  10065. this pusher executes.</p>
  10066. </li>
  10067. <li>
  10068. <p><code>pushkey</code> - string - This is a unique identifier for this pusher.
  10069. Max length, 512 bytes.</p>
  10070. </li>
  10071. </ul>
  10072. </li>
  10073. <li>
  10074. <p><code>total</code> - integer - Number of pushers.</p>
  10075. </li>
  10076. </ul>
  10077. <p>See also the
  10078. <a href="https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-pushers">Client-Server API Spec on pushers</a>.</p>
  10079. <h2 id="shadow-banning-users"><a class="header" href="#shadow-banning-users">Shadow-banning users</a></h2>
  10080. <p>Shadow-banning is a useful tool for moderating malicious or egregiously abusive users.
  10081. A shadow-banned users receives successful responses to their client-server API requests,
  10082. but the events are not propagated into rooms. This can be an effective tool as it
  10083. (hopefully) takes longer for the user to realise they are being moderated before
  10084. pivoting to another account.</p>
  10085. <p>Shadow-banning a user should be used as a tool of last resort and may lead to confusing
  10086. or broken behaviour for the client. A shadow-banned user will not receive any
  10087. notification and it is generally more appropriate to ban or kick abusive users.
  10088. A shadow-banned user will be unable to contact anyone on the server.</p>
  10089. <p>The API is:</p>
  10090. <pre><code>POST /_synapse/admin/v1/users/&lt;user_id&gt;/shadow_ban
  10091. </code></pre>
  10092. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  10093. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  10094. <p>An empty JSON dict is returned.</p>
  10095. <p><strong>Parameters</strong></p>
  10096. <p>The following parameters should be set in the URL:</p>
  10097. <ul>
  10098. <li><code>user_id</code> - The fully qualified MXID: for example, <code>@user:server.com</code>. The user must
  10099. be local.</li>
  10100. </ul>
  10101. <h2 id="override-ratelimiting-for-users"><a class="header" href="#override-ratelimiting-for-users">Override ratelimiting for users</a></h2>
  10102. <p>This API allows to override or disable ratelimiting for a specific user.
  10103. There are specific APIs to set, get and delete a ratelimit.</p>
  10104. <h3 id="get-status-of-ratelimit"><a class="header" href="#get-status-of-ratelimit">Get status of ratelimit</a></h3>
  10105. <p>The API is:</p>
  10106. <pre><code>GET /_synapse/admin/v1/users/&lt;user_id&gt;/override_ratelimit
  10107. </code></pre>
  10108. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  10109. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  10110. <p>A response body like the following is returned:</p>
  10111. <pre><code class="language-json">{
  10112. &quot;messages_per_second&quot;: 0,
  10113. &quot;burst_count&quot;: 0
  10114. }
  10115. </code></pre>
  10116. <p><strong>Parameters</strong></p>
  10117. <p>The following parameters should be set in the URL:</p>
  10118. <ul>
  10119. <li><code>user_id</code> - The fully qualified MXID: for example, <code>@user:server.com</code>. The user must
  10120. be local.</li>
  10121. </ul>
  10122. <p><strong>Response</strong></p>
  10123. <p>The following fields are returned in the JSON response body:</p>
  10124. <ul>
  10125. <li><code>messages_per_second</code> - integer - The number of actions that can
  10126. be performed in a second. <code>0</code> mean that ratelimiting is disabled for this user.</li>
  10127. <li><code>burst_count</code> - integer - How many actions that can be performed before
  10128. being limited.</li>
  10129. </ul>
  10130. <p>If <strong>no</strong> custom ratelimit is set, an empty JSON dict is returned.</p>
  10131. <pre><code class="language-json">{}
  10132. </code></pre>
  10133. <h3 id="set-ratelimit"><a class="header" href="#set-ratelimit">Set ratelimit</a></h3>
  10134. <p>The API is:</p>
  10135. <pre><code>POST /_synapse/admin/v1/users/&lt;user_id&gt;/override_ratelimit
  10136. </code></pre>
  10137. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  10138. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  10139. <p>A response body like the following is returned:</p>
  10140. <pre><code class="language-json">{
  10141. &quot;messages_per_second&quot;: 0,
  10142. &quot;burst_count&quot;: 0
  10143. }
  10144. </code></pre>
  10145. <p><strong>Parameters</strong></p>
  10146. <p>The following parameters should be set in the URL:</p>
  10147. <ul>
  10148. <li><code>user_id</code> - The fully qualified MXID: for example, <code>@user:server.com</code>. The user must
  10149. be local.</li>
  10150. </ul>
  10151. <p>Body parameters:</p>
  10152. <ul>
  10153. <li><code>messages_per_second</code> - positive integer, optional. The number of actions that can
  10154. be performed in a second. Defaults to <code>0</code>.</li>
  10155. <li><code>burst_count</code> - positive integer, optional. How many actions that can be performed
  10156. before being limited. Defaults to <code>0</code>.</li>
  10157. </ul>
  10158. <p>To disable users' ratelimit set both values to <code>0</code>.</p>
  10159. <p><strong>Response</strong></p>
  10160. <p>The following fields are returned in the JSON response body:</p>
  10161. <ul>
  10162. <li><code>messages_per_second</code> - integer - The number of actions that can
  10163. be performed in a second.</li>
  10164. <li><code>burst_count</code> - integer - How many actions that can be performed before
  10165. being limited.</li>
  10166. </ul>
  10167. <h3 id="delete-ratelimit"><a class="header" href="#delete-ratelimit">Delete ratelimit</a></h3>
  10168. <p>The API is:</p>
  10169. <pre><code>DELETE /_synapse/admin/v1/users/&lt;user_id&gt;/override_ratelimit
  10170. </code></pre>
  10171. <p>To use it, you will need to authenticate by providing an <code>access_token</code> for a
  10172. server admin: <a href="admin_api/../usage/administration/admin_api">Admin API</a></p>
  10173. <p>An empty JSON dict is returned.</p>
  10174. <pre><code class="language-json">{}
  10175. </code></pre>
  10176. <p><strong>Parameters</strong></p>
  10177. <p>The following parameters should be set in the URL:</p>
  10178. <ul>
  10179. <li><code>user_id</code> - The fully qualified MXID: for example, <code>@user:server.com</code>. The user must
  10180. be local.</li>
  10181. </ul>
  10182. <div style="break-before: page; page-break-before: always;"></div><h1 id="version-api"><a class="header" href="#version-api">Version API</a></h1>
  10183. <p>This API returns the running Synapse version and the Python version
  10184. on which Synapse is being run. This is useful when a Synapse instance
  10185. is behind a proxy that does not forward the 'Server' header (which also
  10186. contains Synapse version information).</p>
  10187. <p>The api is:</p>
  10188. <pre><code>GET /_synapse/admin/v1/server_version
  10189. </code></pre>
  10190. <p>It returns a JSON body like the following:</p>
  10191. <pre><code class="language-json">{
  10192. &quot;server_version&quot;: &quot;0.99.2rc1 (b=develop, abcdef123)&quot;,
  10193. &quot;python_version&quot;: &quot;3.6.8&quot;
  10194. }
  10195. </code></pre>
  10196. <div style="break-before: page; page-break-before: always;"></div><h1 id="using-the-synapse-manhole"><a class="header" href="#using-the-synapse-manhole">Using the synapse manhole</a></h1>
  10197. <p>The &quot;manhole&quot; allows server administrators to access a Python shell on a running
  10198. Synapse installation. This is a very powerful mechanism for administration and
  10199. debugging.</p>
  10200. <p><strong><em>Security Warning</em></strong></p>
  10201. <p>Note that this will give administrative access to synapse to <strong>all users</strong> with
  10202. shell access to the server. It should therefore <strong>not</strong> be enabled in
  10203. environments where untrusted users have shell access.</p>
  10204. <hr />
  10205. <p>To enable it, first uncomment the <code>manhole</code> listener configuration in
  10206. <code>homeserver.yaml</code>. The configuration is slightly different if you're using docker.</p>
  10207. <h4 id="docker-config"><a class="header" href="#docker-config">Docker config</a></h4>
  10208. <p>If you are using Docker, set <code>bind_addresses</code> to <code>['0.0.0.0']</code> as shown:</p>
  10209. <pre><code class="language-yaml">listeners:
  10210. - port: 9000
  10211. bind_addresses: ['0.0.0.0']
  10212. type: manhole
  10213. </code></pre>
  10214. <p>When using <code>docker run</code> to start the server, you will then need to change the command to the following to include the
  10215. <code>manhole</code> port forwarding. The <code>-p 127.0.0.1:9000:9000</code> below is important: it
  10216. ensures that access to the <code>manhole</code> is only possible for local users.</p>
  10217. <pre><code class="language-bash">docker run -d --name synapse \
  10218. --mount type=volume,src=synapse-data,dst=/data \
  10219. -p 8008:8008 \
  10220. -p 127.0.0.1:9000:9000 \
  10221. matrixdotorg/synapse:latest
  10222. </code></pre>
  10223. <h4 id="native-config"><a class="header" href="#native-config">Native config</a></h4>
  10224. <p>If you are not using docker, set <code>bind_addresses</code> to <code>['::1', '127.0.0.1']</code> as shown.
  10225. The <code>bind_addresses</code> in the example below is important: it ensures that access to the
  10226. <code>manhole</code> is only possible for local users).</p>
  10227. <pre><code class="language-yaml">listeners:
  10228. - port: 9000
  10229. bind_addresses: ['::1', '127.0.0.1']
  10230. type: manhole
  10231. </code></pre>
  10232. <h4 id="accessing-synapse-manhole"><a class="header" href="#accessing-synapse-manhole">Accessing synapse manhole</a></h4>
  10233. <p>Then restart synapse, and point an ssh client at port 9000 on localhost, using
  10234. the username <code>matrix</code>:</p>
  10235. <pre><code class="language-bash">ssh -p9000 matrix@localhost
  10236. </code></pre>
  10237. <p>The password is <code>rabbithole</code>.</p>
  10238. <p>This gives a Python REPL in which <code>hs</code> gives access to the
  10239. <code>synapse.server.HomeServer</code> object - which in turn gives access to many other
  10240. parts of the process.</p>
  10241. <p>Note that any call which returns a coroutine will need to be wrapped in <code>ensureDeferred</code>.</p>
  10242. <p>As a simple example, retrieving an event from the database:</p>
  10243. <pre><code class="language-pycon">&gt;&gt;&gt; from twisted.internet import defer
  10244. &gt;&gt;&gt; defer.ensureDeferred(hs.get_datastore().get_event('$1416420717069yeQaw:matrix.org'))
  10245. &lt;Deferred at 0x7ff253fc6998 current result: &lt;FrozenEvent event_id='$1416420717069yeQaw:matrix.org', type='m.room.create', state_key=''&gt;&gt;
  10246. </code></pre>
  10247. <div style="break-before: page; page-break-before: always;"></div><h1 id="how-to-monitor-synapse-metrics-using-prometheus"><a class="header" href="#how-to-monitor-synapse-metrics-using-prometheus">How to monitor Synapse metrics using Prometheus</a></h1>
  10248. <ol>
  10249. <li>
  10250. <p>Install Prometheus:</p>
  10251. <p>Follow instructions at
  10252. <a href="http://prometheus.io/docs/introduction/install/">http://prometheus.io/docs/introduction/install/</a></p>
  10253. </li>
  10254. <li>
  10255. <p>Enable Synapse metrics:</p>
  10256. <p>There are two methods of enabling metrics in Synapse.</p>
  10257. <p>The first serves the metrics as a part of the usual web server and
  10258. can be enabled by adding the &quot;metrics&quot; resource to the existing
  10259. listener as such:</p>
  10260. <pre><code class="language-yaml"> resources:
  10261. - names:
  10262. - client
  10263. - metrics
  10264. </code></pre>
  10265. <p>This provides a simple way of adding metrics to your Synapse
  10266. installation, and serves under <code>/_synapse/metrics</code>. If you do not
  10267. wish your metrics be publicly exposed, you will need to either
  10268. filter it out at your load balancer, or use the second method.</p>
  10269. <p>The second method runs the metrics server on a different port, in a
  10270. different thread to Synapse. This can make it more resilient to
  10271. heavy load meaning metrics cannot be retrieved, and can be exposed
  10272. to just internal networks easier. The served metrics are available
  10273. over HTTP only, and will be available at <code>/_synapse/metrics</code>.</p>
  10274. <p>Add a new listener to homeserver.yaml:</p>
  10275. <pre><code class="language-yaml"> listeners:
  10276. - type: metrics
  10277. port: 9000
  10278. bind_addresses:
  10279. - '0.0.0.0'
  10280. </code></pre>
  10281. <p>For both options, you will need to ensure that <code>enable_metrics</code> is
  10282. set to <code>True</code>.</p>
  10283. </li>
  10284. <li>
  10285. <p>Restart Synapse.</p>
  10286. </li>
  10287. <li>
  10288. <p>Add a Prometheus target for Synapse.</p>
  10289. <p>It needs to set the <code>metrics_path</code> to a non-default value (under
  10290. <code>scrape_configs</code>):</p>
  10291. <pre><code class="language-yaml"> - job_name: &quot;synapse&quot;
  10292. scrape_interval: 15s
  10293. metrics_path: &quot;/_synapse/metrics&quot;
  10294. static_configs:
  10295. - targets: [&quot;my.server.here:port&quot;]
  10296. </code></pre>
  10297. <p>where <code>my.server.here</code> is the IP address of Synapse, and <code>port</code> is
  10298. the listener port configured with the <code>metrics</code> resource.</p>
  10299. <p>If your prometheus is older than 1.5.2, you will need to replace
  10300. <code>static_configs</code> in the above with <code>target_groups</code>.</p>
  10301. </li>
  10302. <li>
  10303. <p>Restart Prometheus.</p>
  10304. </li>
  10305. <li>
  10306. <p>Consider using the <a href="https://github.com/matrix-org/synapse/tree/master/contrib/grafana/">grafana dashboard</a>
  10307. and required <a href="https://github.com/matrix-org/synapse/tree/master/contrib/prometheus/">recording rules</a> </p>
  10308. </li>
  10309. </ol>
  10310. <h2 id="monitoring-workers"><a class="header" href="#monitoring-workers">Monitoring workers</a></h2>
  10311. <p>To monitor a Synapse installation using <a href="workers.html">workers</a>,
  10312. every worker needs to be monitored independently, in addition to
  10313. the main homeserver process. This is because workers don't send
  10314. their metrics to the main homeserver process, but expose them
  10315. directly (if they are configured to do so).</p>
  10316. <p>To allow collecting metrics from a worker, you need to add a
  10317. <code>metrics</code> listener to its configuration, by adding the following
  10318. under <code>worker_listeners</code>:</p>
  10319. <pre><code class="language-yaml"> - type: metrics
  10320. bind_address: ''
  10321. port: 9101
  10322. </code></pre>
  10323. <p>The <code>bind_address</code> and <code>port</code> parameters should be set so that
  10324. the resulting listener can be reached by prometheus, and they
  10325. don't clash with an existing worker.
  10326. With this example, the worker's metrics would then be available
  10327. on <code>http://127.0.0.1:9101</code>.</p>
  10328. <p>Example Prometheus target for Synapse with workers:</p>
  10329. <pre><code class="language-yaml"> - job_name: &quot;synapse&quot;
  10330. scrape_interval: 15s
  10331. metrics_path: &quot;/_synapse/metrics&quot;
  10332. static_configs:
  10333. - targets: [&quot;my.server.here:port&quot;]
  10334. labels:
  10335. instance: &quot;my.server&quot;
  10336. job: &quot;master&quot;
  10337. index: 1
  10338. - targets: [&quot;my.workerserver.here:port&quot;]
  10339. labels:
  10340. instance: &quot;my.server&quot;
  10341. job: &quot;generic_worker&quot;
  10342. index: 1
  10343. - targets: [&quot;my.workerserver.here:port&quot;]
  10344. labels:
  10345. instance: &quot;my.server&quot;
  10346. job: &quot;generic_worker&quot;
  10347. index: 2
  10348. - targets: [&quot;my.workerserver.here:port&quot;]
  10349. labels:
  10350. instance: &quot;my.server&quot;
  10351. job: &quot;media_repository&quot;
  10352. index: 1
  10353. </code></pre>
  10354. <p>Labels (<code>instance</code>, <code>job</code>, <code>index</code>) can be defined as anything.
  10355. The labels are used to group graphs in grafana.</p>
  10356. <h2 id="renaming-of-metrics--deprecation-of-old-names-in-12"><a class="header" href="#renaming-of-metrics--deprecation-of-old-names-in-12">Renaming of metrics &amp; deprecation of old names in 1.2</a></h2>
  10357. <p>Synapse 1.2 updates the Prometheus metrics to match the naming
  10358. convention of the upstream <code>prometheus_client</code>. The old names are
  10359. considered deprecated and will be removed in a future version of
  10360. Synapse.</p>
  10361. <table><thead><tr><th>New Name</th><th>Old Name</th></tr></thead><tbody>
  10362. <tr><td>python_gc_objects_collected_total</td><td>python_gc_objects_collected</td></tr>
  10363. <tr><td>python_gc_objects_uncollectable_total</td><td>python_gc_objects_uncollectable</td></tr>
  10364. <tr><td>python_gc_collections_total</td><td>python_gc_collections</td></tr>
  10365. <tr><td>process_cpu_seconds_total</td><td>process_cpu_seconds</td></tr>
  10366. <tr><td>synapse_federation_client_sent_transactions_total</td><td>synapse_federation_client_sent_transactions</td></tr>
  10367. <tr><td>synapse_federation_client_events_processed_total</td><td>synapse_federation_client_events_processed</td></tr>
  10368. <tr><td>synapse_event_processing_loop_count_total</td><td>synapse_event_processing_loop_count</td></tr>
  10369. <tr><td>synapse_event_processing_loop_room_count_total</td><td>synapse_event_processing_loop_room_count</td></tr>
  10370. <tr><td>synapse_util_metrics_block_count_total</td><td>synapse_util_metrics_block_count</td></tr>
  10371. <tr><td>synapse_util_metrics_block_time_seconds_total</td><td>synapse_util_metrics_block_time_seconds</td></tr>
  10372. <tr><td>synapse_util_metrics_block_ru_utime_seconds_total</td><td>synapse_util_metrics_block_ru_utime_seconds</td></tr>
  10373. <tr><td>synapse_util_metrics_block_ru_stime_seconds_total</td><td>synapse_util_metrics_block_ru_stime_seconds</td></tr>
  10374. <tr><td>synapse_util_metrics_block_db_txn_count_total</td><td>synapse_util_metrics_block_db_txn_count</td></tr>
  10375. <tr><td>synapse_util_metrics_block_db_txn_duration_seconds_total</td><td>synapse_util_metrics_block_db_txn_duration_seconds</td></tr>
  10376. <tr><td>synapse_util_metrics_block_db_sched_duration_seconds_total</td><td>synapse_util_metrics_block_db_sched_duration_seconds</td></tr>
  10377. <tr><td>synapse_background_process_start_count_total</td><td>synapse_background_process_start_count</td></tr>
  10378. <tr><td>synapse_background_process_ru_utime_seconds_total</td><td>synapse_background_process_ru_utime_seconds</td></tr>
  10379. <tr><td>synapse_background_process_ru_stime_seconds_total</td><td>synapse_background_process_ru_stime_seconds</td></tr>
  10380. <tr><td>synapse_background_process_db_txn_count_total</td><td>synapse_background_process_db_txn_count</td></tr>
  10381. <tr><td>synapse_background_process_db_txn_duration_seconds_total</td><td>synapse_background_process_db_txn_duration_seconds</td></tr>
  10382. <tr><td>synapse_background_process_db_sched_duration_seconds_total</td><td>synapse_background_process_db_sched_duration_seconds</td></tr>
  10383. <tr><td>synapse_storage_events_persisted_events_total</td><td>synapse_storage_events_persisted_events</td></tr>
  10384. <tr><td>synapse_storage_events_persisted_events_sep_total</td><td>synapse_storage_events_persisted_events_sep</td></tr>
  10385. <tr><td>synapse_storage_events_state_delta_total</td><td>synapse_storage_events_state_delta</td></tr>
  10386. <tr><td>synapse_storage_events_state_delta_single_event_total</td><td>synapse_storage_events_state_delta_single_event</td></tr>
  10387. <tr><td>synapse_storage_events_state_delta_reuse_delta_total</td><td>synapse_storage_events_state_delta_reuse_delta</td></tr>
  10388. <tr><td>synapse_federation_server_received_pdus_total</td><td>synapse_federation_server_received_pdus</td></tr>
  10389. <tr><td>synapse_federation_server_received_edus_total</td><td>synapse_federation_server_received_edus</td></tr>
  10390. <tr><td>synapse_handler_presence_notified_presence_total</td><td>synapse_handler_presence_notified_presence</td></tr>
  10391. <tr><td>synapse_handler_presence_federation_presence_out_total</td><td>synapse_handler_presence_federation_presence_out</td></tr>
  10392. <tr><td>synapse_handler_presence_presence_updates_total</td><td>synapse_handler_presence_presence_updates</td></tr>
  10393. <tr><td>synapse_handler_presence_timers_fired_total</td><td>synapse_handler_presence_timers_fired</td></tr>
  10394. <tr><td>synapse_handler_presence_federation_presence_total</td><td>synapse_handler_presence_federation_presence</td></tr>
  10395. <tr><td>synapse_handler_presence_bump_active_time_total</td><td>synapse_handler_presence_bump_active_time</td></tr>
  10396. <tr><td>synapse_federation_client_sent_edus_total</td><td>synapse_federation_client_sent_edus</td></tr>
  10397. <tr><td>synapse_federation_client_sent_pdu_destinations_count_total</td><td>synapse_federation_client_sent_pdu_destinations:count</td></tr>
  10398. <tr><td>synapse_federation_client_sent_pdu_destinations_total</td><td>synapse_federation_client_sent_pdu_destinations:total</td></tr>
  10399. <tr><td>synapse_handlers_appservice_events_processed_total</td><td>synapse_handlers_appservice_events_processed</td></tr>
  10400. <tr><td>synapse_notifier_notified_events_total</td><td>synapse_notifier_notified_events</td></tr>
  10401. <tr><td>synapse_push_bulk_push_rule_evaluator_push_rules_invalidation_counter_total</td><td>synapse_push_bulk_push_rule_evaluator_push_rules_invalidation_counter</td></tr>
  10402. <tr><td>synapse_push_bulk_push_rule_evaluator_push_rules_state_size_counter_total</td><td>synapse_push_bulk_push_rule_evaluator_push_rules_state_size_counter</td></tr>
  10403. <tr><td>synapse_http_httppusher_http_pushes_processed_total</td><td>synapse_http_httppusher_http_pushes_processed</td></tr>
  10404. <tr><td>synapse_http_httppusher_http_pushes_failed_total</td><td>synapse_http_httppusher_http_pushes_failed</td></tr>
  10405. <tr><td>synapse_http_httppusher_badge_updates_processed_total</td><td>synapse_http_httppusher_badge_updates_processed</td></tr>
  10406. <tr><td>synapse_http_httppusher_badge_updates_failed_total</td><td>synapse_http_httppusher_badge_updates_failed</td></tr>
  10407. </tbody></table>
  10408. <h2 id="removal-of-deprecated-metrics--time-based-counters-becoming-histograms-in-0310"><a class="header" href="#removal-of-deprecated-metrics--time-based-counters-becoming-histograms-in-0310">Removal of deprecated metrics &amp; time based counters becoming histograms in 0.31.0</a></h2>
  10409. <p>The duplicated metrics deprecated in Synapse 0.27.0 have been removed.</p>
  10410. <p>All time duration-based metrics have been changed to be seconds. This
  10411. affects:</p>
  10412. <table><thead><tr><th>msec -&gt; sec metrics</th></tr></thead><tbody>
  10413. <tr><td>python_gc_time</td></tr>
  10414. <tr><td>python_twisted_reactor_tick_time</td></tr>
  10415. <tr><td>synapse_storage_query_time</td></tr>
  10416. <tr><td>synapse_storage_schedule_time</td></tr>
  10417. <tr><td>synapse_storage_transaction_time</td></tr>
  10418. </tbody></table>
  10419. <p>Several metrics have been changed to be histograms, which sort entries
  10420. into buckets and allow better analysis. The following metrics are now
  10421. histograms:</p>
  10422. <table><thead><tr><th>Altered metrics</th></tr></thead><tbody>
  10423. <tr><td>python_gc_time</td></tr>
  10424. <tr><td>python_twisted_reactor_pending_calls</td></tr>
  10425. <tr><td>python_twisted_reactor_tick_time</td></tr>
  10426. <tr><td>synapse_http_server_response_time_seconds</td></tr>
  10427. <tr><td>synapse_storage_query_time</td></tr>
  10428. <tr><td>synapse_storage_schedule_time</td></tr>
  10429. <tr><td>synapse_storage_transaction_time</td></tr>
  10430. </tbody></table>
  10431. <h2 id="block-and-response-metrics-renamed-for-0270"><a class="header" href="#block-and-response-metrics-renamed-for-0270">Block and response metrics renamed for 0.27.0</a></h2>
  10432. <p>Synapse 0.27.0 begins the process of rationalising the duplicate
  10433. <code>*:count</code> metrics reported for the resource tracking for code blocks and
  10434. HTTP requests.</p>
  10435. <p>At the same time, the corresponding <code>*:total</code> metrics are being renamed,
  10436. as the <code>:total</code> suffix no longer makes sense in the absence of a
  10437. corresponding <code>:count</code> metric.</p>
  10438. <p>To enable a graceful migration path, this release just adds new names
  10439. for the metrics being renamed. A future release will remove the old
  10440. ones.</p>
  10441. <p>The following table shows the new metrics, and the old metrics which
  10442. they are replacing.</p>
  10443. <table><thead><tr><th>New name</th><th>Old name</th></tr></thead><tbody>
  10444. <tr><td>synapse_util_metrics_block_count</td><td>synapse_util_metrics_block_timer:count</td></tr>
  10445. <tr><td>synapse_util_metrics_block_count</td><td>synapse_util_metrics_block_ru_utime:count</td></tr>
  10446. <tr><td>synapse_util_metrics_block_count</td><td>synapse_util_metrics_block_ru_stime:count</td></tr>
  10447. <tr><td>synapse_util_metrics_block_count</td><td>synapse_util_metrics_block_db_txn_count:count</td></tr>
  10448. <tr><td>synapse_util_metrics_block_count</td><td>synapse_util_metrics_block_db_txn_duration:count</td></tr>
  10449. <tr><td>synapse_util_metrics_block_time_seconds</td><td>synapse_util_metrics_block_timer:total</td></tr>
  10450. <tr><td>synapse_util_metrics_block_ru_utime_seconds</td><td>synapse_util_metrics_block_ru_utime:total</td></tr>
  10451. <tr><td>synapse_util_metrics_block_ru_stime_seconds</td><td>synapse_util_metrics_block_ru_stime:total</td></tr>
  10452. <tr><td>synapse_util_metrics_block_db_txn_count</td><td>synapse_util_metrics_block_db_txn_count:total</td></tr>
  10453. <tr><td>synapse_util_metrics_block_db_txn_duration_seconds</td><td>synapse_util_metrics_block_db_txn_duration:total</td></tr>
  10454. <tr><td>synapse_http_server_response_count</td><td>synapse_http_server_requests</td></tr>
  10455. <tr><td>synapse_http_server_response_count</td><td>synapse_http_server_response_time:count</td></tr>
  10456. <tr><td>synapse_http_server_response_count</td><td>synapse_http_server_response_ru_utime:count</td></tr>
  10457. <tr><td>synapse_http_server_response_count</td><td>synapse_http_server_response_ru_stime:count</td></tr>
  10458. <tr><td>synapse_http_server_response_count</td><td>synapse_http_server_response_db_txn_count:count</td></tr>
  10459. <tr><td>synapse_http_server_response_count</td><td>synapse_http_server_response_db_txn_duration:count</td></tr>
  10460. <tr><td>synapse_http_server_response_time_seconds</td><td>synapse_http_server_response_time:total</td></tr>
  10461. <tr><td>synapse_http_server_response_ru_utime_seconds</td><td>synapse_http_server_response_ru_utime:total</td></tr>
  10462. <tr><td>synapse_http_server_response_ru_stime_seconds</td><td>synapse_http_server_response_ru_stime:total</td></tr>
  10463. <tr><td>synapse_http_server_response_db_txn_count</td><td>synapse_http_server_response_db_txn_count:total</td></tr>
  10464. <tr><td>synapse_http_server_response_db_txn_duration_seconds</td><td>synapse_http_server_response_db_txn_duration:total</td></tr>
  10465. </tbody></table>
  10466. <h2 id="standard-metric-names"><a class="header" href="#standard-metric-names">Standard Metric Names</a></h2>
  10467. <p>As of synapse version 0.18.2, the format of the process-wide metrics has
  10468. been changed to fit prometheus standard naming conventions. Additionally
  10469. the units have been changed to seconds, from miliseconds.</p>
  10470. <table><thead><tr><th>New name</th><th>Old name</th></tr></thead><tbody>
  10471. <tr><td>process_cpu_user_seconds_total</td><td>process_resource_utime / 1000</td></tr>
  10472. <tr><td>process_cpu_system_seconds_total</td><td>process_resource_stime / 1000</td></tr>
  10473. <tr><td>process_open_fds (no 'type' label)</td><td>process_fds</td></tr>
  10474. </tbody></table>
  10475. <p>The python-specific counts of garbage collector performance have been
  10476. renamed.</p>
  10477. <table><thead><tr><th>New name</th><th>Old name</th></tr></thead><tbody>
  10478. <tr><td>python_gc_time</td><td>reactor_gc_time</td></tr>
  10479. <tr><td>python_gc_unreachable_total</td><td>reactor_gc_unreachable</td></tr>
  10480. <tr><td>python_gc_counts</td><td>reactor_gc_counts</td></tr>
  10481. </tbody></table>
  10482. <p>The twisted-specific reactor metrics have been renamed.</p>
  10483. <table><thead><tr><th>New name</th><th>Old name</th></tr></thead><tbody>
  10484. <tr><td>python_twisted_reactor_pending_calls</td><td>reactor_pending_calls</td></tr>
  10485. <tr><td>python_twisted_reactor_tick_time</td><td>reactor_tick_time</td></tr>
  10486. </tbody></table>
  10487. <div style="break-before: page; page-break-before: always;"></div><h1 id="request-log-format"><a class="header" href="#request-log-format">Request log format</a></h1>
  10488. <p>HTTP request logs are written by synapse (see <a href="usage/administration/../synapse/http/site.py"><code>site.py</code></a> for details).</p>
  10489. <p>See the following for how to decode the dense data available from the default logging configuration.</p>
  10490. <pre><code>2020-10-01 12:00:00,000 - synapse.access.http.8008 - 311 - INFO - PUT-1000- 192.168.0.1 - 8008 - {another-matrix-server.com} Processed request: 0.100sec/-0.000sec (0.000sec, 0.000sec) (0.001sec/0.090sec/3) 11B !200 &quot;PUT /_matrix/federation/v1/send/1600000000000 HTTP/1.1&quot; &quot;Synapse/1.20.1&quot; [0 dbevts]
  10491. -AAAAAAAAAAAAAAAAAAAAA- -BBBBBBBBBBBBBBBBBBBBBB- -C- -DD- -EEEEEE- -FFFFFFFFF- -GG- -HHHHHHHHHHHHHHHHHHHHHHH- -IIIIII- -JJJJJJJ- -KKKKKK-, -LLLLLL- -MMMMMMM- -NNNNNN- O -P- -QQ- -RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR- -SSSSSSSSSSSS- -TTTTTT-
  10492. </code></pre>
  10493. <table><thead><tr><th>Part</th><th>Explanation</th></tr></thead><tbody>
  10494. <tr><td>AAAA</td><td>Timestamp request was logged (not recieved)</td></tr>
  10495. <tr><td>BBBB</td><td>Logger name (<code>synapse.access.(http\|https).&lt;tag&gt;</code>, where 'tag' is defined in the <code>listeners</code> config section, normally the port)</td></tr>
  10496. <tr><td>CCCC</td><td>Line number in code</td></tr>
  10497. <tr><td>DDDD</td><td>Log Level</td></tr>
  10498. <tr><td>EEEE</td><td>Request Identifier (This identifier is shared by related log lines)</td></tr>
  10499. <tr><td>FFFF</td><td>Source IP (Or X-Forwarded-For if enabled)</td></tr>
  10500. <tr><td>GGGG</td><td>Server Port</td></tr>
  10501. <tr><td>HHHH</td><td>Federated Server or Local User making request (blank if unauthenticated or not supplied)</td></tr>
  10502. <tr><td>IIII</td><td>Total Time to process the request</td></tr>
  10503. <tr><td>JJJJ</td><td>Time to send response over network once generated (this may be negative if the socket is closed before the response is generated)</td></tr>
  10504. <tr><td>KKKK</td><td>Userland CPU time</td></tr>
  10505. <tr><td>LLLL</td><td>System CPU time</td></tr>
  10506. <tr><td>MMMM</td><td>Total time waiting for a free DB connection from the pool across all parallel DB work from this request</td></tr>
  10507. <tr><td>NNNN</td><td>Total time waiting for response to DB queries across all parallel DB work from this request</td></tr>
  10508. <tr><td>OOOO</td><td>Count of DB transactions performed</td></tr>
  10509. <tr><td>PPPP</td><td>Response body size</td></tr>
  10510. <tr><td>QQQQ</td><td>Response status code (prefixed with ! if the socket was closed before the response was generated)</td></tr>
  10511. <tr><td>RRRR</td><td>Request</td></tr>
  10512. <tr><td>SSSS</td><td>User-agent</td></tr>
  10513. <tr><td>TTTT</td><td>Events fetched from DB to service this request (note that this does not include events fetched from the cache)</td></tr>
  10514. </tbody></table>
  10515. <p>MMMM / NNNN can be greater than IIII if there are multiple slow database queries
  10516. running in parallel.</p>
  10517. <p>Some actions can result in multiple identical http requests, which will return
  10518. the same data, but only the first request will report time/transactions in
  10519. <code>KKKK</code>/<code>LLLL</code>/<code>MMMM</code>/<code>NNNN</code>/<code>OOOO</code> - the others will be awaiting the first query to return a
  10520. response and will simultaneously return with the first request, but with very
  10521. small processing times.</p>
  10522. <div style="break-before: page; page-break-before: always;"></div><!--
  10523. Include the contents of CONTRIBUTING.md from the project root (where GitHub likes it
  10524. to be)
  10525. -->
  10526. <h1 id="contributing"><a class="header" href="#contributing">Contributing</a></h1>
  10527. <p>Welcome to Synapse</p>
  10528. <p>This document aims to get you started with contributing to this repo! </p>
  10529. <ul>
  10530. <li><a href="development/contributing_guide.html#1-who-can-contribute-to-synapse">1. Who can contribute to Synapse?</a></li>
  10531. <li><a href="development/contributing_guide.html#2-what-do-i-need">2. What do I need?</a></li>
  10532. <li><a href="development/contributing_guide.html#3-get-the-source">3. Get the source.</a></li>
  10533. <li><a href="development/contributing_guide.html#4-install-the-dependencies">4. Install the dependencies</a>
  10534. <ul>
  10535. <li><a href="development/contributing_guide.html#under-unix-macos-linux-bsd-">Under Unix (macOS, Linux, BSD, ...)</a></li>
  10536. <li><a href="development/contributing_guide.html#under-windows">Under Windows</a></li>
  10537. </ul>
  10538. </li>
  10539. <li><a href="development/contributing_guide.html#5-get-in-touch">5. Get in touch.</a></li>
  10540. <li><a href="development/contributing_guide.html#6-pick-an-issue">6. Pick an issue.</a></li>
  10541. <li><a href="development/contributing_guide.html#7-turn-coffee-and-documentation-into-code-and-documentation">7. Turn coffee and documentation into code and documentation!</a></li>
  10542. <li><a href="development/contributing_guide.html#8-test-test-test">8. Test, test, test!</a>
  10543. <ul>
  10544. <li><a href="development/contributing_guide.html#run-the-linters">Run the linters.</a></li>
  10545. <li><a href="development/contributing_guide.html#run-the-unit-tests">Run the unit tests.</a></li>
  10546. <li><a href="development/contributing_guide.html#run-the-integration-tests">Run the integration tests.</a></li>
  10547. </ul>
  10548. </li>
  10549. <li><a href="development/contributing_guide.html#9-submit-your-patch">9. Submit your patch.</a>
  10550. <ul>
  10551. <li><a href="development/contributing_guide.html#changelog">Changelog</a>
  10552. <ul>
  10553. <li><a href="development/contributing_guide.html#how-do-i-know-what-to-call-the-changelog-file-before-i-create-the-pr">How do I know what to call the changelog file before I create the PR?</a></li>
  10554. <li><a href="development/contributing_guide.html#debian-changelog">Debian changelog</a></li>
  10555. </ul>
  10556. </li>
  10557. <li><a href="development/contributing_guide.html#sign-off">Sign off</a></li>
  10558. </ul>
  10559. </li>
  10560. <li><a href="development/contributing_guide.html#10-turn-feedback-into-better-code">10. Turn feedback into better code.</a></li>
  10561. <li><a href="development/contributing_guide.html#11-find-a-new-issue">11. Find a new issue.</a></li>
  10562. <li><a href="development/contributing_guide.html#notes-for-maintainers-on-merging-prs-etc">Notes for maintainers on merging PRs etc</a></li>
  10563. <li><a href="development/contributing_guide.html#conclusion">Conclusion</a></li>
  10564. </ul>
  10565. <h1 id="1-who-can-contribute-to-synapse"><a class="header" href="#1-who-can-contribute-to-synapse">1. Who can contribute to Synapse?</a></h1>
  10566. <p>Everyone is welcome to contribute code to <a href="https://github.com/matrix-org">matrix.org
  10567. projects</a>, provided that they are willing to
  10568. license their contributions under the same license as the project itself. We
  10569. follow a simple 'inbound=outbound' model for contributions: the act of
  10570. submitting an 'inbound' contribution means that the contributor agrees to
  10571. license the code under the same terms as the project's overall 'outbound'
  10572. license - in our case, this is almost always Apache Software License v2 (see
  10573. <a href="development/LICENSE">LICENSE</a>).</p>
  10574. <h1 id="2-what-do-i-need"><a class="header" href="#2-what-do-i-need">2. What do I need?</a></h1>
  10575. <p>The code of Synapse is written in Python 3. To do pretty much anything, you'll need <a href="https://wiki.python.org/moin/BeginnersGuide/Download">a recent version of Python 3</a>.</p>
  10576. <p>The source code of Synapse is hosted on GitHub. You will also need <a href="https://github.com/git-guides/install-git">a recent version of git</a>.</p>
  10577. <p>For some tests, you will need <a href="https://docs.docker.com/get-docker/">a recent version of Docker</a>.</p>
  10578. <h1 id="3-get-the-source"><a class="header" href="#3-get-the-source">3. Get the source.</a></h1>
  10579. <p>The preferred and easiest way to contribute changes is to fork the relevant
  10580. project on GitHub, and then <a href="https://help.github.com/articles/using-pull-requests/">create a pull request</a> to ask us to pull your
  10581. changes into our repo.</p>
  10582. <p>Please base your changes on the <code>develop</code> branch.</p>
  10583. <pre><code class="language-sh">git clone git@github.com:YOUR_GITHUB_USER_NAME/synapse.git
  10584. git checkout develop
  10585. </code></pre>
  10586. <p>If you need help getting started with git, this is beyond the scope of the document, but you
  10587. can find many good git tutorials on the web.</p>
  10588. <h1 id="4-install-the-dependencies"><a class="header" href="#4-install-the-dependencies">4. Install the dependencies</a></h1>
  10589. <h2 id="under-unix-macos-linux-bsd-"><a class="header" href="#under-unix-macos-linux-bsd-">Under Unix (macOS, Linux, BSD, ...)</a></h2>
  10590. <p>Once you have installed Python 3 and added the source, please open a terminal and
  10591. setup a <em>virtualenv</em>, as follows:</p>
  10592. <pre><code class="language-sh">cd path/where/you/have/cloned/the/repository
  10593. python3 -m venv ./env
  10594. source ./env/bin/activate
  10595. pip install -e &quot;.[all,lint,mypy,test]&quot;
  10596. pip install tox
  10597. </code></pre>
  10598. <p>This will install the developer dependencies for the project.</p>
  10599. <h2 id="under-windows"><a class="header" href="#under-windows">Under Windows</a></h2>
  10600. <p>TBD</p>
  10601. <h1 id="5-get-in-touch"><a class="header" href="#5-get-in-touch">5. Get in touch.</a></h1>
  10602. <p>Join our developer community on Matrix: #synapse-dev:matrix.org !</p>
  10603. <h1 id="6-pick-an-issue"><a class="header" href="#6-pick-an-issue">6. Pick an issue.</a></h1>
  10604. <p>Fix your favorite problem or perhaps find a <a href="https://github.com/matrix-org/synapse/issues?q=is%3Aopen+is%3Aissue+label%3A%22Good+First+Issue%22">Good First Issue</a>
  10605. to work on.</p>
  10606. <h1 id="7-turn-coffee-and-documentation-into-code-and-documentation"><a class="header" href="#7-turn-coffee-and-documentation-into-code-and-documentation">7. Turn coffee and documentation into code and documentation!</a></h1>
  10607. <p>Synapse's code style is documented <a href="development/docs/code_style.html">here</a>. Please follow
  10608. it, including the conventions for the <a href="development/docs/code_style.html#configuration-file-format">sample configuration
  10609. file</a>.</p>
  10610. <p>There is a growing amount of documentation located in the <a href="development/docs">docs</a>
  10611. directory. This documentation is intended primarily for sysadmins running their
  10612. own Synapse instance, as well as developers interacting externally with
  10613. Synapse. <a href="development/docs/dev">docs/dev</a> exists primarily to house documentation for
  10614. Synapse developers. <a href="development/docs/admin_api">docs/admin_api</a> houses documentation
  10615. regarding Synapse's Admin API, which is used mostly by sysadmins and external
  10616. service developers.</p>
  10617. <p>If you add new files added to either of these folders, please use <a href="https://guides.github.com/features/mastering-markdown/">GitHub-Flavoured
  10618. Markdown</a>.</p>
  10619. <p>Some documentation also exists in <a href="https://github.com/matrix-org/synapse/wiki">Synapse's GitHub
  10620. Wiki</a>, although this is primarily
  10621. contributed to by community authors.</p>
  10622. <h1 id="8-test-test-test"><a class="header" href="#8-test-test-test">8. Test, test, test!</a></h1>
  10623. <p><a name="test-test-test"></a></p>
  10624. <p>While you're developing and before submitting a patch, you'll
  10625. want to test your code.</p>
  10626. <h2 id="run-the-linters"><a class="header" href="#run-the-linters">Run the linters.</a></h2>
  10627. <p>The linters look at your code and do two things:</p>
  10628. <ul>
  10629. <li>ensure that your code follows the coding style adopted by the project;</li>
  10630. <li>catch a number of errors in your code.</li>
  10631. </ul>
  10632. <p>They're pretty fast, don't hesitate!</p>
  10633. <pre><code class="language-sh">source ./env/bin/activate
  10634. ./scripts-dev/lint.sh
  10635. </code></pre>
  10636. <p>Note that this script <em>will modify your files</em> to fix styling errors.
  10637. Make sure that you have saved all your files.</p>
  10638. <p>If you wish to restrict the linters to only the files changed since the last commit
  10639. (much faster!), you can instead run:</p>
  10640. <pre><code class="language-sh">source ./env/bin/activate
  10641. ./scripts-dev/lint.sh -d
  10642. </code></pre>
  10643. <p>Or if you know exactly which files you wish to lint, you can instead run:</p>
  10644. <pre><code class="language-sh">source ./env/bin/activate
  10645. ./scripts-dev/lint.sh path/to/file1.py path/to/file2.py path/to/folder
  10646. </code></pre>
  10647. <h2 id="run-the-unit-tests"><a class="header" href="#run-the-unit-tests">Run the unit tests.</a></h2>
  10648. <p>The unit tests run parts of Synapse, including your changes, to see if anything
  10649. was broken. They are slower than the linters but will typically catch more errors.</p>
  10650. <pre><code class="language-sh">source ./env/bin/activate
  10651. trial tests
  10652. </code></pre>
  10653. <p>If you wish to only run <em>some</em> unit tests, you may specify
  10654. another module instead of <code>tests</code> - or a test class or a method:</p>
  10655. <pre><code class="language-sh">source ./env/bin/activate
  10656. trial tests.rest.admin.test_room tests.handlers.test_admin.ExfiltrateData.test_invite
  10657. </code></pre>
  10658. <p>If your tests fail, you may wish to look at the logs (the default log level is <code>ERROR</code>):</p>
  10659. <pre><code class="language-sh">less _trial_temp/test.log
  10660. </code></pre>
  10661. <p>To increase the log level for the tests, set <code>SYNAPSE_TEST_LOG_LEVEL</code>:</p>
  10662. <pre><code class="language-sh">SYNAPSE_TEST_LOG_LEVEL=DEBUG trial tests
  10663. </code></pre>
  10664. <h2 id="run-the-integration-tests"><a class="header" href="#run-the-integration-tests">Run the integration tests.</a></h2>
  10665. <p>The integration tests are a more comprehensive suite of tests. They
  10666. run a full version of Synapse, including your changes, to check if
  10667. anything was broken. They are slower than the unit tests but will
  10668. typically catch more errors.</p>
  10669. <p>The following command will let you run the integration test with the most common
  10670. configuration:</p>
  10671. <pre><code class="language-sh">$ docker run --rm -it -v /path/where/you/have/cloned/the/repository\:/src:ro -v /path/to/where/you/want/logs\:/logs matrixdotorg/sytest-synapse:py37
  10672. </code></pre>
  10673. <p>This configuration should generally cover your needs. For more details about other configurations, see <a href="https://github.com/matrix-org/sytest/blob/develop/docker/README.md">documentation in the SyTest repo</a>.</p>
  10674. <h1 id="9-submit-your-patch"><a class="header" href="#9-submit-your-patch">9. Submit your patch.</a></h1>
  10675. <p>Once you're happy with your patch, it's time to prepare a Pull Request.</p>
  10676. <p>To prepare a Pull Request, please:</p>
  10677. <ol>
  10678. <li>verify that <a href="development/contributing_guide.html#test-test-test">all the tests pass</a>, including the coding style;</li>
  10679. <li><a href="development/contributing_guide.html#sign-off">sign off</a> your contribution;</li>
  10680. <li><code>git push</code> your commit to your fork of Synapse;</li>
  10681. <li>on GitHub, <a href="https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/creating-a-pull-request">create the Pull Request</a>;</li>
  10682. <li>add a <a href="development/contributing_guide.html#changelog">changelog entry</a> and push it to your Pull Request;</li>
  10683. <li>for most contributors, that's all - however, if you are a member of the organization <code>matrix-org</code>, on GitHub, please request a review from <code>matrix.org / Synapse Core</code>.</li>
  10684. </ol>
  10685. <h2 id="changelog"><a class="header" href="#changelog">Changelog</a></h2>
  10686. <p>All changes, even minor ones, need a corresponding changelog / newsfragment
  10687. entry. These are managed by <a href="https://github.com/hawkowl/towncrier">Towncrier</a>.</p>
  10688. <p>To create a changelog entry, make a new file in the <code>changelog.d</code> directory named
  10689. in the format of <code>PRnumber.type</code>. The type can be one of the following:</p>
  10690. <ul>
  10691. <li><code>feature</code></li>
  10692. <li><code>bugfix</code></li>
  10693. <li><code>docker</code> (for updates to the Docker image)</li>
  10694. <li><code>doc</code> (for updates to the documentation)</li>
  10695. <li><code>removal</code> (also used for deprecations)</li>
  10696. <li><code>misc</code> (for internal-only changes)</li>
  10697. </ul>
  10698. <p>This file will become part of our <a href="https://github.com/matrix-org/synapse/blob/master/CHANGES.md">changelog</a> at the next
  10699. release, so the content of the file should be a short description of your
  10700. change in the same style as the rest of the changelog. The file can contain Markdown
  10701. formatting, and should end with a full stop (.) or an exclamation mark (!) for
  10702. consistency.</p>
  10703. <p>Adding credits to the changelog is encouraged, we value your
  10704. contributions and would like to have you shouted out in the release notes!</p>
  10705. <p>For example, a fix in PR #1234 would have its changelog entry in
  10706. <code>changelog.d/1234.bugfix</code>, and contain content like:</p>
  10707. <blockquote>
  10708. <p>The security levels of Florbs are now validated when received
  10709. via the <code>/federation/florb</code> endpoint. Contributed by Jane Matrix.</p>
  10710. </blockquote>
  10711. <p>If there are multiple pull requests involved in a single bugfix/feature/etc,
  10712. then the content for each <code>changelog.d</code> file should be the same. Towncrier will
  10713. merge the matching files together into a single changelog entry when we come to
  10714. release.</p>
  10715. <h3 id="how-do-i-know-what-to-call-the-changelog-file-before-i-create-the-pr"><a class="header" href="#how-do-i-know-what-to-call-the-changelog-file-before-i-create-the-pr">How do I know what to call the changelog file before I create the PR?</a></h3>
  10716. <p>Obviously, you don't know if you should call your newsfile
  10717. <code>1234.bugfix</code> or <code>5678.bugfix</code> until you create the PR, which leads to a
  10718. chicken-and-egg problem.</p>
  10719. <p>There are two options for solving this:</p>
  10720. <ol>
  10721. <li>
  10722. <p>Open the PR without a changelog file, see what number you got, and <em>then</em>
  10723. add the changelog file to your branch (see <a href="development/contributing_guide.html#updating-your-pull-request">Updating your pull
  10724. request</a>), or:</p>
  10725. </li>
  10726. <li>
  10727. <p>Look at the <a href="https://github.com/matrix-org/synapse/issues?q=">list of all
  10728. issues/PRs</a>, add one to the
  10729. highest number you see, and quickly open the PR before somebody else claims
  10730. your number.</p>
  10731. <p><a href="https://github.com/richvdh/scripts/blob/master/next_github_number.sh">This
  10732. script</a>
  10733. might be helpful if you find yourself doing this a lot.</p>
  10734. </li>
  10735. </ol>
  10736. <p>Sorry, we know it's a bit fiddly, but it's <em>really</em> helpful for us when we come
  10737. to put together a release!</p>
  10738. <h3 id="debian-changelog"><a class="header" href="#debian-changelog">Debian changelog</a></h3>
  10739. <p>Changes which affect the debian packaging files (in <code>debian</code>) are an
  10740. exception to the rule that all changes require a <code>changelog.d</code> file.</p>
  10741. <p>In this case, you will need to add an entry to the debian changelog for the
  10742. next release. For this, run the following command:</p>
  10743. <pre><code>dch
  10744. </code></pre>
  10745. <p>This will make up a new version number (if there isn't already an unreleased
  10746. version in flight), and open an editor where you can add a new changelog entry.
  10747. (Our release process will ensure that the version number and maintainer name is
  10748. corrected for the release.)</p>
  10749. <p>If your change affects both the debian packaging <em>and</em> files outside the debian
  10750. directory, you will need both a regular newsfragment <em>and</em> an entry in the
  10751. debian changelog. (Though typically such changes should be submitted as two
  10752. separate pull requests.)</p>
  10753. <h2 id="sign-off"><a class="header" href="#sign-off">Sign off</a></h2>
  10754. <p>In order to have a concrete record that your contribution is intentional
  10755. and you agree to license it under the same terms as the project's license, we've adopted the
  10756. same lightweight approach that the Linux Kernel
  10757. <a href="https://www.kernel.org/doc/html/latest/process/submitting-patches.html#sign-your-work-the-developer-s-certificate-of-origin%3E">submitting patches process</a>,
  10758. <a href="https://github.com/docker/docker/blob/master/CONTRIBUTING.md">Docker</a>, and many other
  10759. projects use: the DCO (Developer Certificate of Origin:
  10760. http://developercertificate.org/). This is a simple declaration that you wrote
  10761. the contribution or otherwise have the right to contribute it to Matrix:</p>
  10762. <pre><code>Developer Certificate of Origin
  10763. Version 1.1
  10764. Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
  10765. 660 York Street, Suite 102,
  10766. San Francisco, CA 94110 USA
  10767. Everyone is permitted to copy and distribute verbatim copies of this
  10768. license document, but changing it is not allowed.
  10769. Developer's Certificate of Origin 1.1
  10770. By making a contribution to this project, I certify that:
  10771. (a) The contribution was created in whole or in part by me and I
  10772. have the right to submit it under the open source license
  10773. indicated in the file; or
  10774. (b) The contribution is based upon previous work that, to the best
  10775. of my knowledge, is covered under an appropriate open source
  10776. license and I have the right under that license to submit that
  10777. work with modifications, whether created in whole or in part
  10778. by me, under the same open source license (unless I am
  10779. permitted to submit under a different license), as indicated
  10780. in the file; or
  10781. (c) The contribution was provided directly to me by some other
  10782. person who certified (a), (b) or (c) and I have not modified
  10783. it.
  10784. (d) I understand and agree that this project and the contribution
  10785. are public and that a record of the contribution (including all
  10786. personal information I submit with it, including my sign-off) is
  10787. maintained indefinitely and may be redistributed consistent with
  10788. this project or the open source license(s) involved.
  10789. </code></pre>
  10790. <p>If you agree to this for your contribution, then all that's needed is to
  10791. include the line in your commit or pull request comment:</p>
  10792. <pre><code>Signed-off-by: Your Name &lt;your@email.example.org&gt;
  10793. </code></pre>
  10794. <p>We accept contributions under a legally identifiable name, such as
  10795. your name on government documentation or common-law names (names
  10796. claimed by legitimate usage or repute). Unfortunately, we cannot
  10797. accept anonymous contributions at this time.</p>
  10798. <p>Git allows you to add this signoff automatically when using the <code>-s</code>
  10799. flag to <code>git commit</code>, which uses the name and email set in your
  10800. <code>user.name</code> and <code>user.email</code> git configs.</p>
  10801. <h1 id="10-turn-feedback-into-better-code"><a class="header" href="#10-turn-feedback-into-better-code">10. Turn feedback into better code.</a></h1>
  10802. <p>Once the Pull Request is opened, you will see a few things:</p>
  10803. <ol>
  10804. <li>our automated CI (Continuous Integration) pipeline will run (again) the linters, the unit tests, the integration tests and more;</li>
  10805. <li>one or more of the developers will take a look at your Pull Request and offer feedback.</li>
  10806. </ol>
  10807. <p>From this point, you should:</p>
  10808. <ol>
  10809. <li>Look at the results of the CI pipeline.
  10810. <ul>
  10811. <li>If there is any error, fix the error.</li>
  10812. </ul>
  10813. </li>
  10814. <li>If a developer has requested changes, make these changes and let us know if it is ready for a developer to review again.</li>
  10815. <li>Create a new commit with the changes.
  10816. <ul>
  10817. <li>Please do NOT overwrite the history. New commits make the reviewer's life easier.</li>
  10818. <li>Push this commits to your Pull Request.</li>
  10819. </ul>
  10820. </li>
  10821. <li>Back to 1.</li>
  10822. </ol>
  10823. <p>Once both the CI and the developers are happy, the patch will be merged into Synapse and released shortly!</p>
  10824. <h1 id="11-find-a-new-issue"><a class="header" href="#11-find-a-new-issue">11. Find a new issue.</a></h1>
  10825. <p>By now, you know the drill!</p>
  10826. <h1 id="notes-for-maintainers-on-merging-prs-etc"><a class="header" href="#notes-for-maintainers-on-merging-prs-etc">Notes for maintainers on merging PRs etc</a></h1>
  10827. <p>There are some notes for those with commit access to the project on how we
  10828. manage git <a href="development/docs/dev/git.html">here</a>.</p>
  10829. <h1 id="conclusion"><a class="header" href="#conclusion">Conclusion</a></h1>
  10830. <p>That's it! Matrix is a very open and collaborative project as you might expect
  10831. given our obsession with open communication. If we're going to successfully
  10832. matrix together all the fragmented communication technologies out there we are
  10833. reliant on contributions and collaboration from the community to do so. So
  10834. please get involved - and we hope you have as much fun hacking on Matrix as we
  10835. do!</p>
  10836. <div style="break-before: page; page-break-before: always;"></div><h1 id="code-style"><a class="header" href="#code-style">Code Style</a></h1>
  10837. <h2 id="formatting-tools"><a class="header" href="#formatting-tools">Formatting tools</a></h2>
  10838. <p>The Synapse codebase uses a number of code formatting tools in order to
  10839. quickly and automatically check for formatting (and sometimes logical)
  10840. errors in code.</p>
  10841. <p>The necessary tools are detailed below.</p>
  10842. <p>First install them with:</p>
  10843. <pre><code>pip install -e &quot;.[lint,mypy]&quot;
  10844. </code></pre>
  10845. <ul>
  10846. <li>
  10847. <p><strong>black</strong></p>
  10848. <p>The Synapse codebase uses <a href="https://pypi.org/project/black/">black</a>
  10849. as an opinionated code formatter, ensuring all comitted code is
  10850. properly formatted.</p>
  10851. <p>Have <code>black</code> auto-format your code (it shouldn't change any
  10852. functionality) with:</p>
  10853. <pre><code>black . --exclude=&quot;\.tox|build|env&quot;
  10854. </code></pre>
  10855. </li>
  10856. <li>
  10857. <p><strong>flake8</strong></p>
  10858. <p><code>flake8</code> is a code checking tool. We require code to pass <code>flake8</code>
  10859. before being merged into the codebase.</p>
  10860. <p>Check all application and test code with:</p>
  10861. <pre><code>flake8 synapse tests
  10862. </code></pre>
  10863. </li>
  10864. <li>
  10865. <p><strong>isort</strong></p>
  10866. <p><code>isort</code> ensures imports are nicely formatted, and can suggest and
  10867. auto-fix issues such as double-importing.</p>
  10868. <p>Auto-fix imports with:</p>
  10869. <pre><code>isort -rc synapse tests
  10870. </code></pre>
  10871. <p><code>-rc</code> means to recursively search the given directories.</p>
  10872. </li>
  10873. </ul>
  10874. <p>It's worth noting that modern IDEs and text editors can run these tools
  10875. automatically on save. It may be worth looking into whether this
  10876. functionality is supported in your editor for a more convenient
  10877. development workflow. It is not, however, recommended to run <code>flake8</code> on
  10878. save as it takes a while and is very resource intensive.</p>
  10879. <h2 id="general-rules"><a class="header" href="#general-rules">General rules</a></h2>
  10880. <ul>
  10881. <li><strong>Naming</strong>:
  10882. <ul>
  10883. <li>Use camel case for class and type names</li>
  10884. <li>Use underscores for functions and variables.</li>
  10885. </ul>
  10886. </li>
  10887. <li><strong>Docstrings</strong>: should follow the <a href="https://google.github.io/styleguide/pyguide.html#38-comments-and-docstrings">google code
  10888. style</a>.
  10889. See the
  10890. <a href="http://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html">examples</a>
  10891. in the sphinx documentation.</li>
  10892. <li><strong>Imports</strong>:
  10893. <ul>
  10894. <li>
  10895. <p>Imports should be sorted by <code>isort</code> as described above.</p>
  10896. </li>
  10897. <li>
  10898. <p>Prefer to import classes and functions rather than packages or
  10899. modules.</p>
  10900. <p>Example:</p>
  10901. <pre><code>from synapse.types import UserID
  10902. ...
  10903. user_id = UserID(local, server)
  10904. </code></pre>
  10905. <p>is preferred over:</p>
  10906. <pre><code>from synapse import types
  10907. ...
  10908. user_id = types.UserID(local, server)
  10909. </code></pre>
  10910. <p>(or any other variant).</p>
  10911. <p>This goes against the advice in the Google style guide, but it
  10912. means that errors in the name are caught early (at import time).</p>
  10913. </li>
  10914. <li>
  10915. <p>Avoid wildcard imports (<code>from synapse.types import *</code>) and
  10916. relative imports (<code>from .types import UserID</code>).</p>
  10917. </li>
  10918. </ul>
  10919. </li>
  10920. </ul>
  10921. <h2 id="configuration-file-format"><a class="header" href="#configuration-file-format">Configuration file format</a></h2>
  10922. <p>The <a href="./sample_config.yaml">sample configuration file</a> acts as a
  10923. reference to Synapse's configuration options for server administrators.
  10924. Remember that many readers will be unfamiliar with YAML and server
  10925. administration in general, so that it is important that the file be as
  10926. easy to understand as possible, which includes following a consistent
  10927. format.</p>
  10928. <p>Some guidelines follow:</p>
  10929. <ul>
  10930. <li>
  10931. <p>Sections should be separated with a heading consisting of a single
  10932. line prefixed and suffixed with <code>##</code>. There should be <strong>two</strong> blank
  10933. lines before the section header, and <strong>one</strong> after.</p>
  10934. </li>
  10935. <li>
  10936. <p>Each option should be listed in the file with the following format:</p>
  10937. <ul>
  10938. <li>
  10939. <p>A comment describing the setting. Each line of this comment
  10940. should be prefixed with a hash (<code>#</code>) and a space.</p>
  10941. <p>The comment should describe the default behaviour (ie, what
  10942. happens if the setting is omitted), as well as what the effect
  10943. will be if the setting is changed.</p>
  10944. <p>Often, the comment end with something like &quot;uncomment the
  10945. following to <do action>&quot;.</p>
  10946. </li>
  10947. <li>
  10948. <p>A line consisting of only <code>#</code>.</p>
  10949. </li>
  10950. <li>
  10951. <p>A commented-out example setting, prefixed with only <code>#</code>.</p>
  10952. <p>For boolean (on/off) options, convention is that this example
  10953. should be the <em>opposite</em> to the default (so the comment will end
  10954. with &quot;Uncomment the following to enable [or disable]
  10955. <feature>.&quot; For other options, the example should give some
  10956. non-default value which is likely to be useful to the reader.</p>
  10957. </li>
  10958. </ul>
  10959. </li>
  10960. <li>
  10961. <p>There should be a blank line between each option.</p>
  10962. </li>
  10963. <li>
  10964. <p>Where several settings are grouped into a single dict, <em>avoid</em> the
  10965. convention where the whole block is commented out, resulting in
  10966. comment lines starting <code># #</code>, as this is hard to read and confusing
  10967. to edit. Instead, leave the top-level config option uncommented, and
  10968. follow the conventions above for sub-options. Ensure that your code
  10969. correctly handles the top-level option being set to <code>None</code> (as it
  10970. will be if no sub-options are enabled).</p>
  10971. </li>
  10972. <li>
  10973. <p>Lines should be wrapped at 80 characters.</p>
  10974. </li>
  10975. <li>
  10976. <p>Use two-space indents.</p>
  10977. </li>
  10978. <li>
  10979. <p><code>true</code> and <code>false</code> are spelt thus (as opposed to <code>True</code>, etc.)</p>
  10980. </li>
  10981. <li>
  10982. <p>Use single quotes (<code>'</code>) rather than double-quotes (<code>&quot;</code>) or backticks
  10983. (<code>`</code>) to refer to configuration options.</p>
  10984. </li>
  10985. </ul>
  10986. <p>Example:</p>
  10987. <pre><code>## Frobnication ##
  10988. # The frobnicator will ensure that all requests are fully frobnicated.
  10989. # To enable it, uncomment the following.
  10990. #
  10991. #frobnicator_enabled: true
  10992. # By default, the frobnicator will frobnicate with the default frobber.
  10993. # The following will make it use an alternative frobber.
  10994. #
  10995. #frobincator_frobber: special_frobber
  10996. # Settings for the frobber
  10997. #
  10998. frobber:
  10999. # frobbing speed. Defaults to 1.
  11000. #
  11001. #speed: 10
  11002. # frobbing distance. Defaults to 1000.
  11003. #
  11004. #distance: 100
  11005. </code></pre>
  11006. <p>Note that the sample configuration is generated from the synapse code
  11007. and is maintained by a script, <code>scripts-dev/generate_sample_config</code>.
  11008. Making sure that the output from this script matches the desired format
  11009. is left as an exercise for the reader!</p>
  11010. <div style="break-before: page; page-break-before: always;"></div><h1 id="some-notes-on-how-we-use-git"><a class="header" href="#some-notes-on-how-we-use-git">Some notes on how we use git</a></h1>
  11011. <h2 id="on-keeping-the-commit-history-clean"><a class="header" href="#on-keeping-the-commit-history-clean">On keeping the commit history clean</a></h2>
  11012. <p>In an ideal world, our git commit history would be a linear progression of
  11013. commits each of which contains a single change building on what came
  11014. before. Here, by way of an arbitrary example, is the top of <code>git log --graph b2dba0607</code>:</p>
  11015. <img src="dev/git/clean.png" alt="clean git graph" width="500px">
  11016. <p>Note how the commit comment explains clearly what is changing and why. Also
  11017. note the <em>absence</em> of merge commits, as well as the absence of commits called
  11018. things like (to pick a few culprits):
  11019. <a href="https://github.com/matrix-org/synapse/commit/84691da6c">“pep8”</a>, <a href="https://github.com/matrix-org/synapse/commit/474810d9d">“fix broken
  11020. test”</a>,
  11021. <a href="https://github.com/matrix-org/synapse/commit/c9d72e457">“oops”</a>,
  11022. <a href="https://github.com/matrix-org/synapse/commit/836358823">“typo”</a>, or <a href="https://github.com/matrix-org/synapse/commit/707374d5d">“Who's
  11023. the president?”</a>.</p>
  11024. <p>There are a number of reasons why keeping a clean commit history is a good
  11025. thing:</p>
  11026. <ul>
  11027. <li>
  11028. <p>From time to time, after a change lands, it turns out to be necessary to
  11029. revert it, or to backport it to a release branch. Those operations are
  11030. <em>much</em> easier when the change is contained in a single commit.</p>
  11031. </li>
  11032. <li>
  11033. <p>Similarly, it's much easier to answer questions like “is the fix for
  11034. <code>/publicRooms</code> on the release branch?” if that change consists of a single
  11035. commit.</p>
  11036. </li>
  11037. <li>
  11038. <p>Likewise: “what has changed on this branch in the last week?” is much
  11039. clearer without merges and “pep8” commits everywhere.</p>
  11040. </li>
  11041. <li>
  11042. <p>Sometimes we need to figure out where a bug got introduced, or some
  11043. behaviour changed. One way of doing that is with <code>git bisect</code>: pick an
  11044. arbitrary commit between the known good point and the known bad point, and
  11045. see how the code behaves. However, that strategy fails if the commit you
  11046. chose is the middle of someone's epic branch in which they broke the world
  11047. before putting it back together again.</p>
  11048. </li>
  11049. </ul>
  11050. <p>One counterargument is that it is sometimes useful to see how a PR evolved as
  11051. it went through review cycles. This is true, but that information is always
  11052. available via the GitHub UI (or via the little-known <a href="https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/checking-out-pull-requests-locally">refs/pull
  11053. namespace</a>).</p>
  11054. <p>Of course, in reality, things are more complicated than that. We have release
  11055. branches as well as <code>develop</code> and <code>master</code>, and we deliberately merge changes
  11056. between them. Bugs often slip through and have to be fixed later. That's all
  11057. fine: this not a cast-iron rule which must be obeyed, but an ideal to aim
  11058. towards.</p>
  11059. <h2 id="merges-squashes-rebases-wtf"><a class="header" href="#merges-squashes-rebases-wtf">Merges, squashes, rebases: wtf?</a></h2>
  11060. <p>Ok, so that's what we'd like to achieve. How do we achieve it?</p>
  11061. <p>The TL;DR is: when you come to merge a pull request, you <em>probably</em> want to
  11062. “squash and merge”:</p>
  11063. <p><img src="dev/git/squash.png" alt="squash and merge" />.</p>
  11064. <p>(This applies whether you are merging your own PR, or that of another
  11065. contributor.)</p>
  11066. <p>“Squash and merge”<sup id="a1"><a href="dev/git.html#f1">1</a></sup> takes all of the changes in the
  11067. PR, and bundles them into a single commit. GitHub gives you the opportunity to
  11068. edit the commit message before you confirm, and normally you should do so,
  11069. because the default will be useless (again: <code>* woops typo</code> is not a useful
  11070. thing to keep in the historical record).</p>
  11071. <p>The main problem with this approach comes when you have a series of pull
  11072. requests which build on top of one another: as soon as you squash-merge the
  11073. first PR, you'll end up with a stack of conflicts to resolve in all of the
  11074. others. In general, it's best to avoid this situation in the first place by
  11075. trying not to have multiple related PRs in flight at the same time. Still,
  11076. sometimes that's not possible and doing a regular merge is the lesser evil.</p>
  11077. <p>Another occasion in which a regular merge makes more sense is a PR where you've
  11078. deliberately created a series of commits each of which makes sense in its own
  11079. right. For example: <a href="https://github.com/matrix-org/synapse/pull/6837">a PR which gradually propagates a refactoring operation
  11080. through the codebase</a>, or <a href="https://github.com/matrix-org/synapse/pull/5987">a
  11081. PR which is the culmination of several other
  11082. PRs</a>. In this case the ability
  11083. to figure out when a particular change/bug was introduced could be very useful.</p>
  11084. <p>Ultimately: <strong>this is not a hard-and-fast-rule</strong>. If in doubt, ask yourself “do
  11085. each of the commits I am about to merge make sense in their own right”, but
  11086. remember that we're just doing our best to balance “keeping the commit history
  11087. clean” with other factors.</p>
  11088. <h2 id="git-branching-model"><a class="header" href="#git-branching-model">Git branching model</a></h2>
  11089. <p>A <a href="https://nvie.com/posts/a-successful-git-branching-model/">lot</a>
  11090. <a href="http://scottchacon.com/2011/08/31/github-flow.html">of</a>
  11091. <a href="https://www.endoflineblog.com/gitflow-considered-harmful">words</a> have been
  11092. written in the past about git branching models (no really, <a href="https://martinfowler.com/articles/branching-patterns.html">a
  11093. lot</a>). I tend to
  11094. think the whole thing is overblown. Fundamentally, it's not that
  11095. complicated. Here's how we do it.</p>
  11096. <p>Let's start with a picture:</p>
  11097. <p><img src="dev/git/branches.jpg" alt="branching model" /></p>
  11098. <p>It looks complicated, but it's really not. There's one basic rule: <em>anyone</em> is
  11099. free to merge from <em>any</em> more-stable branch to <em>any</em> less-stable branch at
  11100. <em>any</em> time<sup id="a2"><a href="dev/git.html#f2">2</a></sup>. (The principle behind this is that if a
  11101. change is good enough for the more-stable branch, then it's also good enough go
  11102. put in a less-stable branch.)</p>
  11103. <p>Meanwhile, merging (or squashing, as per the above) from a less-stable to a
  11104. more-stable branch is a deliberate action in which you want to publish a change
  11105. or a set of changes to (some subset of) the world: for example, this happens
  11106. when a PR is landed, or as part of our release process.</p>
  11107. <p>So, what counts as a more- or less-stable branch? A little reflection will show
  11108. that our active branches are ordered thus, from more-stable to less-stable:</p>
  11109. <ul>
  11110. <li><code>master</code> (tracks our last release).</li>
  11111. <li><code>release-vX.Y</code> (the branch where we prepare the next release)<sup
  11112. id="a3"><a href="dev/git.html#f3">3</a></sup>.</li>
  11113. <li>PR branches which are targeting the release.</li>
  11114. <li><code>develop</code> (our &quot;mainline&quot; branch containing our bleeding-edge).</li>
  11115. <li>regular PR branches.</li>
  11116. </ul>
  11117. <p>The corollary is: if you have a bugfix that needs to land in both
  11118. <code>release-vX.Y</code> <em>and</em> <code>develop</code>, then you should base your PR on
  11119. <code>release-vX.Y</code>, get it merged there, and then merge from <code>release-vX.Y</code> to
  11120. <code>develop</code>. (If a fix lands in <code>develop</code> and we later need it in a
  11121. release-branch, we can of course cherry-pick it, but landing it in the release
  11122. branch first helps reduce the chance of annoying conflicts.)</p>
  11123. <hr />
  11124. <p><b id="f1">[1]</b>: “Squash and merge” is GitHub's term for this
  11125. operation. Given that there is no merge involved, I'm not convinced it's the
  11126. most intuitive name. <a href="dev/git.html#a1">^</a></p>
  11127. <p><b id="f2">[2]</b>: Well, anyone with commit access.<a href="dev/git.html#a2">^</a></p>
  11128. <p><b id="f3">[3]</b>: Very, very occasionally (I think this has happened once in
  11129. the history of Synapse), we've had two releases in flight at once. Obviously,
  11130. <code>release-v1.2</code> is more-stable than <code>release-v1.3</code>. <a href="dev/git.html#a3">^</a></p>
  11131. <div style="break-before: page; page-break-before: always;"></div><h1 id="opentracing"><a class="header" href="#opentracing">OpenTracing</a></h1>
  11132. <h2 id="background"><a class="header" href="#background">Background</a></h2>
  11133. <p>OpenTracing is a semi-standard being adopted by a number of distributed
  11134. tracing platforms. It is a common api for facilitating vendor-agnostic
  11135. tracing instrumentation. That is, we can use the OpenTracing api and
  11136. select one of a number of tracer implementations to do the heavy lifting
  11137. in the background. Our current selected implementation is Jaeger.</p>
  11138. <p>OpenTracing is a tool which gives an insight into the causal
  11139. relationship of work done in and between servers. The servers each track
  11140. events and report them to a centralised server - in Synapse's case:
  11141. Jaeger. The basic unit used to represent events is the span. The span
  11142. roughly represents a single piece of work that was done and the time at
  11143. which it occurred. A span can have child spans, meaning that the work of
  11144. the child had to be completed for the parent span to complete, or it can
  11145. have follow-on spans which represent work that is undertaken as a result
  11146. of the parent but is not depended on by the parent to in order to
  11147. finish.</p>
  11148. <p>Since this is undertaken in a distributed environment a request to
  11149. another server, such as an RPC or a simple GET, can be considered a span
  11150. (a unit or work) for the local server. This causal link is what
  11151. OpenTracing aims to capture and visualise. In order to do this metadata
  11152. about the local server's span, i.e the 'span context', needs to be
  11153. included with the request to the remote.</p>
  11154. <p>It is up to the remote server to decide what it does with the spans it
  11155. creates. This is called the sampling policy and it can be configured
  11156. through Jaeger's settings.</p>
  11157. <p>For OpenTracing concepts see
  11158. <a href="https://opentracing.io/docs/overview/what-is-tracing/">https://opentracing.io/docs/overview/what-is-tracing/</a>.</p>
  11159. <p>For more information about Jaeger's implementation see
  11160. <a href="https://www.jaegertracing.io/docs/">https://www.jaegertracing.io/docs/</a></p>
  11161. <h2 id="setting-up-opentracing"><a class="header" href="#setting-up-opentracing">Setting up OpenTracing</a></h2>
  11162. <p>To receive OpenTracing spans, start up a Jaeger server. This can be done
  11163. using docker like so:</p>
  11164. <pre><code class="language-sh">docker run -d --name jaeger \
  11165. -p 6831:6831/udp \
  11166. -p 6832:6832/udp \
  11167. -p 5778:5778 \
  11168. -p 16686:16686 \
  11169. -p 14268:14268 \
  11170. jaegertracing/all-in-one:1
  11171. </code></pre>
  11172. <p>Latest documentation is probably at
  11173. https://www.jaegertracing.io/docs/latest/getting-started.</p>
  11174. <h2 id="enable-opentracing-in-synapse"><a class="header" href="#enable-opentracing-in-synapse">Enable OpenTracing in Synapse</a></h2>
  11175. <p>OpenTracing is not enabled by default. It must be enabled in the
  11176. homeserver config by uncommenting the config options under <code>opentracing</code>
  11177. as shown in the <a href="./sample_config.yaml">sample config</a>. For example:</p>
  11178. <pre><code class="language-yaml">opentracing:
  11179. enabled: true
  11180. homeserver_whitelist:
  11181. - &quot;mytrustedhomeserver.org&quot;
  11182. - &quot;*.myotherhomeservers.com&quot;
  11183. </code></pre>
  11184. <h2 id="homeserver-whitelisting"><a class="header" href="#homeserver-whitelisting">Homeserver whitelisting</a></h2>
  11185. <p>The homeserver whitelist is configured using regular expressions. A list
  11186. of regular expressions can be given and their union will be compared
  11187. when propagating any spans contexts to another homeserver.</p>
  11188. <p>Though it's mostly safe to send and receive span contexts to and from
  11189. untrusted users since span contexts are usually opaque ids it can lead
  11190. to two problems, namely:</p>
  11191. <ul>
  11192. <li>If the span context is marked as sampled by the sending homeserver
  11193. the receiver will sample it. Therefore two homeservers with wildly
  11194. different sampling policies could incur higher sampling counts than
  11195. intended.</li>
  11196. <li>Sending servers can attach arbitrary data to spans, known as
  11197. 'baggage'. For safety this has been disabled in Synapse but that
  11198. doesn't prevent another server sending you baggage which will be
  11199. logged to OpenTracing's logs.</li>
  11200. </ul>
  11201. <h2 id="configuring-jaeger"><a class="header" href="#configuring-jaeger">Configuring Jaeger</a></h2>
  11202. <p>Sampling strategies can be set as in this document:
  11203. <a href="https://www.jaegertracing.io/docs/latest/sampling/">https://www.jaegertracing.io/docs/latest/sampling/</a>.</p>
  11204. <div style="break-before: page; page-break-before: always;"></div><h1 id="synapse-database-schema-files"><a class="header" href="#synapse-database-schema-files">Synapse database schema files</a></h1>
  11205. <p>Synapse's database schema is stored in the <code>synapse.storage.schema</code> module.</p>
  11206. <h2 id="logical-databases"><a class="header" href="#logical-databases">Logical databases</a></h2>
  11207. <p>Synapse supports splitting its datastore across multiple physical databases (which can
  11208. be useful for large installations), and the schema files are therefore split according
  11209. to the logical database they apply to.</p>
  11210. <p>At the time of writing, the following &quot;logical&quot; databases are supported:</p>
  11211. <ul>
  11212. <li><code>state</code> - used to store Matrix room state (more specifically, <code>state_groups</code>,
  11213. their relationships and contents).</li>
  11214. <li><code>main</code> - stores everything else.</li>
  11215. </ul>
  11216. <p>Additionally, the <code>common</code> directory contains schema files for tables which must be
  11217. present on <em>all</em> physical databases.</p>
  11218. <h2 id="synapse-schema-versions"><a class="header" href="#synapse-schema-versions">Synapse schema versions</a></h2>
  11219. <p>Synapse manages its database schema via &quot;schema versions&quot;. These are mainly used to
  11220. help avoid confusion if the Synapse codebase is rolled back after the database is
  11221. updated. They work as follows:</p>
  11222. <ul>
  11223. <li>
  11224. <p>The Synapse codebase defines a constant <code>synapse.storage.schema.SCHEMA_VERSION</code>
  11225. which represents the expectations made about the database by that version. For
  11226. example, as of Synapse v1.36, this is <code>59</code>.</p>
  11227. </li>
  11228. <li>
  11229. <p>The database stores a &quot;compatibility version&quot; in
  11230. <code>schema_compat_version.compat_version</code> which defines the <code>SCHEMA_VERSION</code> of the
  11231. oldest version of Synapse which will work with the database. On startup, if
  11232. <code>compat_version</code> is found to be newer than <code>SCHEMA_VERSION</code>, Synapse will refuse to
  11233. start.</p>
  11234. <p>Synapse automatically updates this field from
  11235. <code>synapse.storage.schema.SCHEMA_COMPAT_VERSION</code>.</p>
  11236. </li>
  11237. <li>
  11238. <p>Whenever a backwards-incompatible change is made to the database format (normally
  11239. via a <code>delta</code> file), <code>synapse.storage.schema.SCHEMA_COMPAT_VERSION</code> is also updated
  11240. so that administrators can not accidentally roll back to a too-old version of Synapse.</p>
  11241. </li>
  11242. </ul>
  11243. <p>Generally, the goal is to maintain compatibility with at least one or two previous
  11244. releases of Synapse, so any substantial change tends to require multiple releases and a
  11245. bit of forward-planning to get right.</p>
  11246. <p>As a worked example: we want to remove the <code>room_stats_historical</code> table. Here is how it
  11247. might pan out.</p>
  11248. <ol>
  11249. <li>
  11250. <p>Replace any code that <em>reads</em> from <code>room_stats_historical</code> with alternative
  11251. implementations, but keep writing to it in case of rollback to an earlier version.
  11252. Also, increase <code>synapse.storage.schema.SCHEMA_VERSION</code>. In this
  11253. instance, there is no existing code which reads from <code>room_stats_historical</code>, so
  11254. our starting point is:</p>
  11255. <p>v1.36.0: <code>SCHEMA_VERSION=59</code>, <code>SCHEMA_COMPAT_VERSION=59</code></p>
  11256. </li>
  11257. <li>
  11258. <p>Next (say in Synapse v1.37.0): remove the code that <em>writes</em> to
  11259. <code>room_stats_historical</code>, but don’t yet remove the table in case of rollback to
  11260. v1.36.0. Again, we increase <code>synapse.storage.schema.SCHEMA_VERSION</code>, but
  11261. because we have not broken compatibility with v1.36, we do not yet update
  11262. <code>SCHEMA_COMPAT_VERSION</code>. We now have:</p>
  11263. <p>v1.37.0: <code>SCHEMA_VERSION=60</code>, <code>SCHEMA_COMPAT_VERSION=59</code>.</p>
  11264. </li>
  11265. <li>
  11266. <p>Later (say in Synapse v1.38.0): we can remove the table altogether. This will
  11267. break compatibility with v1.36.0, so we must update <code>SCHEMA_COMPAT_VERSION</code> accordingly.
  11268. There is no need to update <code>synapse.storage.schema.SCHEMA_VERSION</code>, since there is no
  11269. change to the Synapse codebase here. So we end up with:</p>
  11270. <p>v1.38.0: <code>SCHEMA_VERSION=60</code>, <code>SCHEMA_COMPAT_VERSION=60</code>.</p>
  11271. </li>
  11272. </ol>
  11273. <p>If in doubt about whether to update <code>SCHEMA_VERSION</code> or not, it is generally best to
  11274. lean towards doing so.</p>
  11275. <h2 id="full-schema-dumps"><a class="header" href="#full-schema-dumps">Full schema dumps</a></h2>
  11276. <p>In the <code>full_schemas</code> directories, only the most recently-numbered snapshot is used
  11277. (<code>54</code> at the time of writing). Older snapshots (eg, <code>16</code>) are present for historical
  11278. reference only.</p>
  11279. <h3 id="building-full-schema-dumps"><a class="header" href="#building-full-schema-dumps">Building full schema dumps</a></h3>
  11280. <p>If you want to recreate these schemas, they need to be made from a database that
  11281. has had all background updates run.</p>
  11282. <p>To do so, use <code>scripts-dev/make_full_schema.sh</code>. This will produce new
  11283. <code>full.sql.postgres</code> and <code>full.sql.sqlite</code> files.</p>
  11284. <p>Ensure postgres is installed, then run:</p>
  11285. <pre><code>./scripts-dev/make_full_schema.sh -p postgres_username -o output_dir/
  11286. </code></pre>
  11287. <p>NB at the time of writing, this script predates the split into separate <code>state</code>/<code>main</code>
  11288. databases so will require updates to handle that correctly.</p>
  11289. <h2 id="boolean-columns"><a class="header" href="#boolean-columns">Boolean columns</a></h2>
  11290. <p>Boolean columns require special treatment, since SQLite treats booleans the
  11291. same as integers.</p>
  11292. <p>There are three separate aspects to this:</p>
  11293. <ul>
  11294. <li>
  11295. <p>Any new boolean column must be added to the <code>BOOLEAN_COLUMNS</code> list in
  11296. <code>scripts/synapse_port_db</code>. This tells the port script to cast the integer
  11297. value from SQLite to a boolean before writing the value to the postgres
  11298. database.</p>
  11299. </li>
  11300. <li>
  11301. <p>Before SQLite 3.23, <code>TRUE</code> and <code>FALSE</code> were not recognised as constants by
  11302. SQLite, and the <code>IS [NOT] TRUE</code>/<code>IS [NOT] FALSE</code> operators were not
  11303. supported. This makes it necessary to avoid using <code>TRUE</code> and <code>FALSE</code>
  11304. constants in SQL commands.</p>
  11305. <p>For example, to insert a <code>TRUE</code> value into the database, write:</p>
  11306. <pre><code class="language-python">txn.execute(&quot;INSERT INTO tbl(col) VALUES (?)&quot;, (True, ))
  11307. </code></pre>
  11308. </li>
  11309. <li>
  11310. <p>Default values for new boolean columns present a particular
  11311. difficulty. Generally it is best to create separate schema files for
  11312. Postgres and SQLite. For example:</p>
  11313. <pre><code class="language-sql"># in 00delta.sql.postgres:
  11314. ALTER TABLE tbl ADD COLUMN col BOOLEAN DEFAULT FALSE;
  11315. </code></pre>
  11316. <pre><code class="language-sql"># in 00delta.sql.sqlite:
  11317. ALTER TABLE tbl ADD COLUMN col BOOLEAN DEFAULT 0;
  11318. </code></pre>
  11319. <p>Note that there is a particularly insidious failure mode here: the Postgres
  11320. flavour will be accepted by SQLite 3.22, but will give a column whose
  11321. default value is the <strong>string</strong> <code>&quot;FALSE&quot;</code> - which, when cast back to a boolean
  11322. in Python, evaluates to <code>True</code>.</p>
  11323. </li>
  11324. </ul>
  11325. <div style="break-before: page; page-break-before: always;"></div><h1 id="log-contexts"><a class="header" href="#log-contexts">Log Contexts</a></h1>
  11326. <p>To help track the processing of individual requests, synapse uses a
  11327. '<code>log context</code>' to track which request it is handling at any given
  11328. moment. This is done via a thread-local variable; a <code>logging.Filter</code> is
  11329. then used to fish the information back out of the thread-local variable
  11330. and add it to each log record.</p>
  11331. <p>Logcontexts are also used for CPU and database accounting, so that we
  11332. can track which requests were responsible for high CPU use or database
  11333. activity.</p>
  11334. <p>The <code>synapse.logging.context</code> module provides a facilities for managing
  11335. the current log context (as well as providing the <code>LoggingContextFilter</code>
  11336. class).</p>
  11337. <p>Deferreds make the whole thing complicated, so this document describes
  11338. how it all works, and how to write code which follows the rules.</p>
  11339. <p>##Logcontexts without Deferreds</p>
  11340. <p>In the absence of any Deferred voodoo, things are simple enough. As with
  11341. any code of this nature, the rule is that our function should leave
  11342. things as it found them:</p>
  11343. <pre><code class="language-python">from synapse.logging import context # omitted from future snippets
  11344. def handle_request(request_id):
  11345. request_context = context.LoggingContext()
  11346. calling_context = context.set_current_context(request_context)
  11347. try:
  11348. request_context.request = request_id
  11349. do_request_handling()
  11350. logger.debug(&quot;finished&quot;)
  11351. finally:
  11352. context.set_current_context(calling_context)
  11353. def do_request_handling():
  11354. logger.debug(&quot;phew&quot;) # this will be logged against request_id
  11355. </code></pre>
  11356. <p>LoggingContext implements the context management methods, so the above
  11357. can be written much more succinctly as:</p>
  11358. <pre><code class="language-python">def handle_request(request_id):
  11359. with context.LoggingContext() as request_context:
  11360. request_context.request = request_id
  11361. do_request_handling()
  11362. logger.debug(&quot;finished&quot;)
  11363. def do_request_handling():
  11364. logger.debug(&quot;phew&quot;)
  11365. </code></pre>
  11366. <h2 id="using-logcontexts-with-deferreds"><a class="header" href="#using-logcontexts-with-deferreds">Using logcontexts with Deferreds</a></h2>
  11367. <p>Deferreds --- and in particular, <code>defer.inlineCallbacks</code> --- break the
  11368. linear flow of code so that there is no longer a single entry point
  11369. where we should set the logcontext and a single exit point where we
  11370. should remove it.</p>
  11371. <p>Consider the example above, where <code>do_request_handling</code> needs to do some
  11372. blocking operation, and returns a deferred:</p>
  11373. <pre><code class="language-python">@defer.inlineCallbacks
  11374. def handle_request(request_id):
  11375. with context.LoggingContext() as request_context:
  11376. request_context.request = request_id
  11377. yield do_request_handling()
  11378. logger.debug(&quot;finished&quot;)
  11379. </code></pre>
  11380. <p>In the above flow:</p>
  11381. <ul>
  11382. <li>The logcontext is set</li>
  11383. <li><code>do_request_handling</code> is called, and returns a deferred</li>
  11384. <li><code>handle_request</code> yields the deferred</li>
  11385. <li>The <code>inlineCallbacks</code> wrapper of <code>handle_request</code> returns a deferred</li>
  11386. </ul>
  11387. <p>So we have stopped processing the request (and will probably go on to
  11388. start processing the next), without clearing the logcontext.</p>
  11389. <p>To circumvent this problem, synapse code assumes that, wherever you have
  11390. a deferred, you will want to yield on it. To that end, whereever
  11391. functions return a deferred, we adopt the following conventions:</p>
  11392. <p><strong>Rules for functions returning deferreds:</strong></p>
  11393. <blockquote>
  11394. <ul>
  11395. <li>If the deferred is already complete, the function returns with the
  11396. same logcontext it started with.</li>
  11397. <li>If the deferred is incomplete, the function clears the logcontext
  11398. before returning; when the deferred completes, it restores the
  11399. logcontext before running any callbacks.</li>
  11400. </ul>
  11401. </blockquote>
  11402. <p>That sounds complicated, but actually it means a lot of code (including
  11403. the example above) &quot;just works&quot;. There are two cases:</p>
  11404. <ul>
  11405. <li>
  11406. <p>If <code>do_request_handling</code> returns a completed deferred, then the
  11407. logcontext will still be in place. In this case, execution will
  11408. continue immediately after the <code>yield</code>; the &quot;finished&quot; line will
  11409. be logged against the right context, and the <code>with</code> block restores
  11410. the original context before we return to the caller.</p>
  11411. </li>
  11412. <li>
  11413. <p>If the returned deferred is incomplete, <code>do_request_handling</code> clears
  11414. the logcontext before returning. The logcontext is therefore clear
  11415. when <code>handle_request</code> yields the deferred. At that point, the
  11416. <code>inlineCallbacks</code> wrapper adds a callback to the deferred, and
  11417. returns another (incomplete) deferred to the caller, and it is safe
  11418. to begin processing the next request.</p>
  11419. <p>Once <code>do_request_handling</code>'s deferred completes, it will reinstate
  11420. the logcontext, before running the callback added by the
  11421. <code>inlineCallbacks</code> wrapper. That callback runs the second half of
  11422. <code>handle_request</code>, so again the &quot;finished&quot; line will be logged
  11423. against the right context, and the <code>with</code> block restores the
  11424. original context.</p>
  11425. </li>
  11426. </ul>
  11427. <p>As an aside, it's worth noting that <code>handle_request</code> follows our rules
  11428. -though that only matters if the caller has its own logcontext which it
  11429. cares about.</p>
  11430. <p>The following sections describe pitfalls and helpful patterns when
  11431. implementing these rules.</p>
  11432. <h2 id="always-yield-your-deferreds"><a class="header" href="#always-yield-your-deferreds">Always yield your deferreds</a></h2>
  11433. <p>Whenever you get a deferred back from a function, you should <code>yield</code> on
  11434. it as soon as possible. (Returning it directly to your caller is ok too,
  11435. if you're not doing <code>inlineCallbacks</code>.) Do not pass go; do not do any
  11436. logging; do not call any other functions.</p>
  11437. <pre><code class="language-python">@defer.inlineCallbacks
  11438. def fun():
  11439. logger.debug(&quot;starting&quot;)
  11440. yield do_some_stuff() # just like this
  11441. d = more_stuff()
  11442. result = yield d # also fine, of course
  11443. return result
  11444. def nonInlineCallbacksFun():
  11445. logger.debug(&quot;just a wrapper really&quot;)
  11446. return do_some_stuff() # this is ok too - the caller will yield on
  11447. # it anyway.
  11448. </code></pre>
  11449. <p>Provided this pattern is followed all the way back up to the callchain
  11450. to where the logcontext was set, this will make things work out ok:
  11451. provided <code>do_some_stuff</code> and <code>more_stuff</code> follow the rules above, then
  11452. so will <code>fun</code> (as wrapped by <code>inlineCallbacks</code>) and
  11453. <code>nonInlineCallbacksFun</code>.</p>
  11454. <p>It's all too easy to forget to <code>yield</code>: for instance if we forgot that
  11455. <code>do_some_stuff</code> returned a deferred, we might plough on regardless. This
  11456. leads to a mess; it will probably work itself out eventually, but not
  11457. before a load of stuff has been logged against the wrong context.
  11458. (Normally, other things will break, more obviously, if you forget to
  11459. <code>yield</code>, so this tends not to be a major problem in practice.)</p>
  11460. <p>Of course sometimes you need to do something a bit fancier with your
  11461. Deferreds - not all code follows the linear A-then-B-then-C pattern.
  11462. Notes on implementing more complex patterns are in later sections.</p>
  11463. <h2 id="where-you-create-a-new-deferred-make-it-follow-the-rules"><a class="header" href="#where-you-create-a-new-deferred-make-it-follow-the-rules">Where you create a new Deferred, make it follow the rules</a></h2>
  11464. <p>Most of the time, a Deferred comes from another synapse function.
  11465. Sometimes, though, we need to make up a new Deferred, or we get a
  11466. Deferred back from external code. We need to make it follow our rules.</p>
  11467. <p>The easy way to do it is with a combination of <code>defer.inlineCallbacks</code>,
  11468. and <code>context.PreserveLoggingContext</code>. Suppose we want to implement
  11469. <code>sleep</code>, which returns a deferred which will run its callbacks after a
  11470. given number of seconds. That might look like:</p>
  11471. <pre><code class="language-python"># not a logcontext-rules-compliant function
  11472. def get_sleep_deferred(seconds):
  11473. d = defer.Deferred()
  11474. reactor.callLater(seconds, d.callback, None)
  11475. return d
  11476. </code></pre>
  11477. <p>That doesn't follow the rules, but we can fix it by wrapping it with
  11478. <code>PreserveLoggingContext</code> and <code>yield</code> ing on it:</p>
  11479. <pre><code class="language-python">@defer.inlineCallbacks
  11480. def sleep(seconds):
  11481. with PreserveLoggingContext():
  11482. yield get_sleep_deferred(seconds)
  11483. </code></pre>
  11484. <p>This technique works equally for external functions which return
  11485. deferreds, or deferreds we have made ourselves.</p>
  11486. <p>You can also use <code>context.make_deferred_yieldable</code>, which just does the
  11487. boilerplate for you, so the above could be written:</p>
  11488. <pre><code class="language-python">def sleep(seconds):
  11489. return context.make_deferred_yieldable(get_sleep_deferred(seconds))
  11490. </code></pre>
  11491. <h2 id="fire-and-forget"><a class="header" href="#fire-and-forget">Fire-and-forget</a></h2>
  11492. <p>Sometimes you want to fire off a chain of execution, but not wait for
  11493. its result. That might look a bit like this:</p>
  11494. <pre><code class="language-python">@defer.inlineCallbacks
  11495. def do_request_handling():
  11496. yield foreground_operation()
  11497. # *don't* do this
  11498. background_operation()
  11499. logger.debug(&quot;Request handling complete&quot;)
  11500. @defer.inlineCallbacks
  11501. def background_operation():
  11502. yield first_background_step()
  11503. logger.debug(&quot;Completed first step&quot;)
  11504. yield second_background_step()
  11505. logger.debug(&quot;Completed second step&quot;)
  11506. </code></pre>
  11507. <p>The above code does a couple of steps in the background after
  11508. <code>do_request_handling</code> has finished. The log lines are still logged
  11509. against the <code>request_context</code> logcontext, which may or may not be
  11510. desirable. There are two big problems with the above, however. The first
  11511. problem is that, if <code>background_operation</code> returns an incomplete
  11512. Deferred, it will expect its caller to <code>yield</code> immediately, so will have
  11513. cleared the logcontext. In this example, that means that 'Request
  11514. handling complete' will be logged without any context.</p>
  11515. <p>The second problem, which is potentially even worse, is that when the
  11516. Deferred returned by <code>background_operation</code> completes, it will restore
  11517. the original logcontext. There is nothing waiting on that Deferred, so
  11518. the logcontext will leak into the reactor and possibly get attached to
  11519. some arbitrary future operation.</p>
  11520. <p>There are two potential solutions to this.</p>
  11521. <p>One option is to surround the call to <code>background_operation</code> with a
  11522. <code>PreserveLoggingContext</code> call. That will reset the logcontext before
  11523. starting <code>background_operation</code> (so the context restored when the
  11524. deferred completes will be the empty logcontext), and will restore the
  11525. current logcontext before continuing the foreground process:</p>
  11526. <pre><code class="language-python">@defer.inlineCallbacks
  11527. def do_request_handling():
  11528. yield foreground_operation()
  11529. # start background_operation off in the empty logcontext, to
  11530. # avoid leaking the current context into the reactor.
  11531. with PreserveLoggingContext():
  11532. background_operation()
  11533. # this will now be logged against the request context
  11534. logger.debug(&quot;Request handling complete&quot;)
  11535. </code></pre>
  11536. <p>Obviously that option means that the operations done in
  11537. <code>background_operation</code> would be not be logged against a logcontext
  11538. (though that might be fixed by setting a different logcontext via a
  11539. <code>with LoggingContext(...)</code> in <code>background_operation</code>).</p>
  11540. <p>The second option is to use <code>context.run_in_background</code>, which wraps a
  11541. function so that it doesn't reset the logcontext even when it returns
  11542. an incomplete deferred, and adds a callback to the returned deferred to
  11543. reset the logcontext. In other words, it turns a function that follows
  11544. the Synapse rules about logcontexts and Deferreds into one which behaves
  11545. more like an external function --- the opposite operation to that
  11546. described in the previous section. It can be used like this:</p>
  11547. <pre><code class="language-python">@defer.inlineCallbacks
  11548. def do_request_handling():
  11549. yield foreground_operation()
  11550. context.run_in_background(background_operation)
  11551. # this will now be logged against the request context
  11552. logger.debug(&quot;Request handling complete&quot;)
  11553. </code></pre>
  11554. <h2 id="passing-synapse-deferreds-into-third-party-functions"><a class="header" href="#passing-synapse-deferreds-into-third-party-functions">Passing synapse deferreds into third-party functions</a></h2>
  11555. <p>A typical example of this is where we want to collect together two or
  11556. more deferred via <code>defer.gatherResults</code>:</p>
  11557. <pre><code class="language-python">d1 = operation1()
  11558. d2 = operation2()
  11559. d3 = defer.gatherResults([d1, d2])
  11560. </code></pre>
  11561. <p>This is really a variation of the fire-and-forget problem above, in that
  11562. we are firing off <code>d1</code> and <code>d2</code> without yielding on them. The difference
  11563. is that we now have third-party code attached to their callbacks. Anyway
  11564. either technique given in the <a href="log_contexts.html#fire-and-forget">Fire-and-forget</a>
  11565. section will work.</p>
  11566. <p>Of course, the new Deferred returned by <code>gatherResults</code> needs to be
  11567. wrapped in order to make it follow the logcontext rules before we can
  11568. yield it, as described in <a href="log_contexts.html#where-you-create-a-new-deferred-make-it-follow-the-rules">Where you create a new Deferred, make it
  11569. follow the
  11570. rules</a>.</p>
  11571. <p>So, option one: reset the logcontext before starting the operations to
  11572. be gathered:</p>
  11573. <pre><code class="language-python">@defer.inlineCallbacks
  11574. def do_request_handling():
  11575. with PreserveLoggingContext():
  11576. d1 = operation1()
  11577. d2 = operation2()
  11578. result = yield defer.gatherResults([d1, d2])
  11579. </code></pre>
  11580. <p>In this case particularly, though, option two, of using
  11581. <code>context.preserve_fn</code> almost certainly makes more sense, so that
  11582. <code>operation1</code> and <code>operation2</code> are both logged against the original
  11583. logcontext. This looks like:</p>
  11584. <pre><code class="language-python">@defer.inlineCallbacks
  11585. def do_request_handling():
  11586. d1 = context.preserve_fn(operation1)()
  11587. d2 = context.preserve_fn(operation2)()
  11588. with PreserveLoggingContext():
  11589. result = yield defer.gatherResults([d1, d2])
  11590. </code></pre>
  11591. <h2 id="was-all-this-really-necessary"><a class="header" href="#was-all-this-really-necessary">Was all this really necessary?</a></h2>
  11592. <p>The conventions used work fine for a linear flow where everything
  11593. happens in series via <code>defer.inlineCallbacks</code> and <code>yield</code>, but are
  11594. certainly tricky to follow for any more exotic flows. It's hard not to
  11595. wonder if we could have done something else.</p>
  11596. <p>We're not going to rewrite Synapse now, so the following is entirely of
  11597. academic interest, but I'd like to record some thoughts on an
  11598. alternative approach.</p>
  11599. <p>I briefly prototyped some code following an alternative set of rules. I
  11600. think it would work, but I certainly didn't get as far as thinking how
  11601. it would interact with concepts as complicated as the cache descriptors.</p>
  11602. <p>My alternative rules were:</p>
  11603. <ul>
  11604. <li>functions always preserve the logcontext of their caller, whether or
  11605. not they are returning a Deferred.</li>
  11606. <li>Deferreds returned by synapse functions run their callbacks in the
  11607. same context as the function was orignally called in.</li>
  11608. </ul>
  11609. <p>The main point of this scheme is that everywhere that sets the
  11610. logcontext is responsible for clearing it before returning control to
  11611. the reactor.</p>
  11612. <p>So, for example, if you were the function which started a
  11613. <code>with LoggingContext</code> block, you wouldn't <code>yield</code> within it --- instead
  11614. you'd start off the background process, and then leave the <code>with</code> block
  11615. to wait for it:</p>
  11616. <pre><code class="language-python">def handle_request(request_id):
  11617. with context.LoggingContext() as request_context:
  11618. request_context.request = request_id
  11619. d = do_request_handling()
  11620. def cb(r):
  11621. logger.debug(&quot;finished&quot;)
  11622. d.addCallback(cb)
  11623. return d
  11624. </code></pre>
  11625. <p>(in general, mixing <code>with LoggingContext</code> blocks and
  11626. <code>defer.inlineCallbacks</code> in the same function leads to slighly
  11627. counter-intuitive code, under this scheme).</p>
  11628. <p>Because we leave the original <code>with</code> block as soon as the Deferred is
  11629. returned (as opposed to waiting for it to be resolved, as we do today),
  11630. the logcontext is cleared before control passes back to the reactor; so
  11631. if there is some code within <code>do_request_handling</code> which needs to wait
  11632. for a Deferred to complete, there is no need for it to worry about
  11633. clearing the logcontext before doing so:</p>
  11634. <pre><code class="language-python">def handle_request():
  11635. r = do_some_stuff()
  11636. r.addCallback(do_some_more_stuff)
  11637. return r
  11638. </code></pre>
  11639. <p>--- and provided <code>do_some_stuff</code> follows the rules of returning a
  11640. Deferred which runs its callbacks in the original logcontext, all is
  11641. happy.</p>
  11642. <p>The business of a Deferred which runs its callbacks in the original
  11643. logcontext isn't hard to achieve --- we have it today, in the shape of
  11644. <code>context._PreservingContextDeferred</code>:</p>
  11645. <pre><code class="language-python">def do_some_stuff():
  11646. deferred = do_some_io()
  11647. pcd = _PreservingContextDeferred(LoggingContext.current_context())
  11648. deferred.chainDeferred(pcd)
  11649. return pcd
  11650. </code></pre>
  11651. <p>It turns out that, thanks to the way that Deferreds chain together, we
  11652. automatically get the property of a context-preserving deferred with
  11653. <code>defer.inlineCallbacks</code>, provided the final Defered the function
  11654. <code>yields</code> on has that property. So we can just write:</p>
  11655. <pre><code class="language-python">@defer.inlineCallbacks
  11656. def handle_request():
  11657. yield do_some_stuff()
  11658. yield do_some_more_stuff()
  11659. </code></pre>
  11660. <p>To conclude: I think this scheme would have worked equally well, with
  11661. less danger of messing it up, and probably made some more esoteric code
  11662. easier to write. But again --- changing the conventions of the entire
  11663. Synapse codebase is not a sensible option for the marginal improvement
  11664. offered.</p>
  11665. <h2 id="a-note-on-garbage-collection-of-deferred-chains"><a class="header" href="#a-note-on-garbage-collection-of-deferred-chains">A note on garbage-collection of Deferred chains</a></h2>
  11666. <p>It turns out that our logcontext rules do not play nicely with Deferred
  11667. chains which get orphaned and garbage-collected.</p>
  11668. <p>Imagine we have some code that looks like this:</p>
  11669. <pre><code class="language-python">listener_queue = []
  11670. def on_something_interesting():
  11671. for d in listener_queue:
  11672. d.callback(&quot;foo&quot;)
  11673. @defer.inlineCallbacks
  11674. def await_something_interesting():
  11675. new_deferred = defer.Deferred()
  11676. listener_queue.append(new_deferred)
  11677. with PreserveLoggingContext():
  11678. yield new_deferred
  11679. </code></pre>
  11680. <p>Obviously, the idea here is that we have a bunch of things which are
  11681. waiting for an event. (It's just an example of the problem here, but a
  11682. relatively common one.)</p>
  11683. <p>Now let's imagine two further things happen. First of all, whatever was
  11684. waiting for the interesting thing goes away. (Perhaps the request times
  11685. out, or something <em>even more</em> interesting happens.)</p>
  11686. <p>Secondly, let's suppose that we decide that the interesting thing is
  11687. never going to happen, and we reset the listener queue:</p>
  11688. <pre><code class="language-python">def reset_listener_queue():
  11689. listener_queue.clear()
  11690. </code></pre>
  11691. <p>So, both ends of the deferred chain have now dropped their references,
  11692. and the deferred chain is now orphaned, and will be garbage-collected at
  11693. some point. Note that <code>await_something_interesting</code> is a generator
  11694. function, and when Python garbage-collects generator functions, it gives
  11695. them a chance to clean up by making the <code>yield</code> raise a <code>GeneratorExit</code>
  11696. exception. In our case, that means that the <code>__exit__</code> handler of
  11697. <code>PreserveLoggingContext</code> will carefully restore the request context, but
  11698. there is now nothing waiting for its return, so the request context is
  11699. never cleared.</p>
  11700. <p>To reiterate, this problem only arises when <em>both</em> ends of a deferred
  11701. chain are dropped. Dropping the the reference to a deferred you're
  11702. supposed to be calling is probably bad practice, so this doesn't
  11703. actually happen too much. Unfortunately, when it does happen, it will
  11704. lead to leaked logcontexts which are incredibly hard to track down.</p>
  11705. <div style="break-before: page; page-break-before: always;"></div><h1 id="replication-architecture"><a class="header" href="#replication-architecture">Replication Architecture</a></h1>
  11706. <h2 id="motivation"><a class="header" href="#motivation">Motivation</a></h2>
  11707. <p>We'd like to be able to split some of the work that synapse does into
  11708. multiple python processes. In theory multiple synapse processes could
  11709. share a single postgresql database and we'd scale up by running more
  11710. synapse processes. However much of synapse assumes that only one process
  11711. is interacting with the database, both for assigning unique identifiers
  11712. when inserting into tables, notifying components about new updates, and
  11713. for invalidating its caches.</p>
  11714. <p>So running multiple copies of the current code isn't an option. One way
  11715. to run multiple processes would be to have a single writer process and
  11716. multiple reader processes connected to the same database. In order to do
  11717. this we'd need a way for the reader process to invalidate its in-memory
  11718. caches when an update happens on the writer. One way to do this is for
  11719. the writer to present an append-only log of updates which the readers
  11720. can consume to invalidate their caches and to push updates to listening
  11721. clients or pushers.</p>
  11722. <p>Synapse already stores much of its data as an append-only log so that it
  11723. can correctly respond to <code>/sync</code> requests so the amount of code changes
  11724. needed to expose the append-only log to the readers should be fairly
  11725. minimal.</p>
  11726. <h2 id="architecture"><a class="header" href="#architecture">Architecture</a></h2>
  11727. <h3 id="the-replication-protocol"><a class="header" href="#the-replication-protocol">The Replication Protocol</a></h3>
  11728. <p>See <a href="tcp_replication.html">tcp_replication.md</a></p>
  11729. <h3 id="the-slaved-datastore"><a class="header" href="#the-slaved-datastore">The Slaved DataStore</a></h3>
  11730. <p>There are read-only version of the synapse storage layer in
  11731. <code>synapse/replication/slave/storage</code> that use the response of the
  11732. replication API to invalidate their caches.</p>
  11733. <div style="break-before: page; page-break-before: always;"></div><h1 id="tcp-replication"><a class="header" href="#tcp-replication">TCP Replication</a></h1>
  11734. <h2 id="motivation-1"><a class="header" href="#motivation-1">Motivation</a></h2>
  11735. <p>Previously the workers used an HTTP long poll mechanism to get updates
  11736. from the master, which had the problem of causing a lot of duplicate
  11737. work on the server. This TCP protocol replaces those APIs with the aim
  11738. of increased efficiency.</p>
  11739. <h2 id="overview-3"><a class="header" href="#overview-3">Overview</a></h2>
  11740. <p>The protocol is based on fire and forget, line based commands. An
  11741. example flow would be (where '&gt;' indicates master to worker and
  11742. '&lt;' worker to master flows):</p>
  11743. <pre><code>&gt; SERVER example.com
  11744. &lt; REPLICATE
  11745. &gt; POSITION events master 53 53
  11746. &gt; RDATA events master 54 [&quot;$foo1:bar.com&quot;, ...]
  11747. &gt; RDATA events master 55 [&quot;$foo4:bar.com&quot;, ...]
  11748. </code></pre>
  11749. <p>The example shows the server accepting a new connection and sending its identity
  11750. with the <code>SERVER</code> command, followed by the client server to respond with the
  11751. position of all streams. The server then periodically sends <code>RDATA</code> commands
  11752. which have the format <code>RDATA &lt;stream_name&gt; &lt;instance_name&gt; &lt;token&gt; &lt;row&gt;</code>, where
  11753. the format of <code>&lt;row&gt;</code> is defined by the individual streams. The
  11754. <code>&lt;instance_name&gt;</code> is the name of the Synapse process that generated the data
  11755. (usually &quot;master&quot;).</p>
  11756. <p>Error reporting happens by either the client or server sending an ERROR
  11757. command, and usually the connection will be closed.</p>
  11758. <p>Since the protocol is a simple line based, its possible to manually
  11759. connect to the server using a tool like netcat. A few things should be
  11760. noted when manually using the protocol:</p>
  11761. <ul>
  11762. <li>The federation stream is only available if federation sending has
  11763. been disabled on the main process.</li>
  11764. <li>The server will only time connections out that have sent a <code>PING</code>
  11765. command. If a ping is sent then the connection will be closed if no
  11766. further commands are receieved within 15s. Both the client and
  11767. server protocol implementations will send an initial PING on
  11768. connection and ensure at least one command every 5s is sent (not
  11769. necessarily <code>PING</code>).</li>
  11770. <li><code>RDATA</code> commands <em>usually</em> include a numeric token, however if the
  11771. stream has multiple rows to replicate per token the server will send
  11772. multiple <code>RDATA</code> commands, with all but the last having a token of
  11773. <code>batch</code>. See the documentation on <code>commands.RdataCommand</code> for
  11774. further details.</li>
  11775. </ul>
  11776. <h2 id="architecture-1"><a class="header" href="#architecture-1">Architecture</a></h2>
  11777. <p>The basic structure of the protocol is line based, where the initial
  11778. word of each line specifies the command. The rest of the line is parsed
  11779. based on the command. For example, the RDATA command is defined as:</p>
  11780. <pre><code>RDATA &lt;stream_name&gt; &lt;instance_name&gt; &lt;token&gt; &lt;row_json&gt;
  11781. </code></pre>
  11782. <p>(Note that &lt;row_json&gt; may contains spaces, but cannot contain
  11783. newlines.)</p>
  11784. <p>Blank lines are ignored.</p>
  11785. <h3 id="keep-alives"><a class="header" href="#keep-alives">Keep alives</a></h3>
  11786. <p>Both sides are expected to send at least one command every 5s or so, and
  11787. should send a <code>PING</code> command if necessary. If either side do not receive
  11788. a command within e.g. 15s then the connection should be closed.</p>
  11789. <p>Because the server may be connected to manually using e.g. netcat, the
  11790. timeouts aren't enabled until an initial <code>PING</code> command is seen. Both
  11791. the client and server implementations below send a <code>PING</code> command
  11792. immediately on connection to ensure the timeouts are enabled.</p>
  11793. <p>This ensures that both sides can quickly realize if the tcp connection
  11794. has gone and handle the situation appropriately.</p>
  11795. <h3 id="start-up"><a class="header" href="#start-up">Start up</a></h3>
  11796. <p>When a new connection is made, the server:</p>
  11797. <ul>
  11798. <li>Sends a <code>SERVER</code> command, which includes the identity of the server,
  11799. allowing the client to detect if its connected to the expected
  11800. server</li>
  11801. <li>Sends a <code>PING</code> command as above, to enable the client to time out
  11802. connections promptly.</li>
  11803. </ul>
  11804. <p>The client:</p>
  11805. <ul>
  11806. <li>Sends a <code>NAME</code> command, allowing the server to associate a human
  11807. friendly name with the connection. This is optional.</li>
  11808. <li>Sends a <code>PING</code> as above</li>
  11809. <li>Sends a <code>REPLICATE</code> to get the current position of all streams.</li>
  11810. <li>On receipt of a <code>SERVER</code> command, checks that the server name
  11811. matches the expected server name.</li>
  11812. </ul>
  11813. <h3 id="error-handling"><a class="header" href="#error-handling">Error handling</a></h3>
  11814. <p>If either side detects an error it can send an <code>ERROR</code> command and close
  11815. the connection.</p>
  11816. <p>If the client side loses the connection to the server it should
  11817. reconnect, following the steps above.</p>
  11818. <h3 id="congestion"><a class="header" href="#congestion">Congestion</a></h3>
  11819. <p>If the server sends messages faster than the client can consume them the
  11820. server will first buffer a (fairly large) number of commands and then
  11821. disconnect the client. This ensures that we don't queue up an unbounded
  11822. number of commands in memory and gives us a potential oppurtunity to
  11823. squawk loudly. When/if the client recovers it can reconnect to the
  11824. server and ask for missed messages.</p>
  11825. <h3 id="reliability"><a class="header" href="#reliability">Reliability</a></h3>
  11826. <p>In general the replication stream should be considered an unreliable
  11827. transport since e.g. commands are not resent if the connection
  11828. disappears.</p>
  11829. <p>The exception to that are the replication streams, i.e. RDATA commands,
  11830. since these include tokens which can be used to restart the stream on
  11831. connection errors.</p>
  11832. <p>The client should keep track of the token in the last RDATA command
  11833. received for each stream so that on reconneciton it can start streaming
  11834. from the correct place. Note: not all RDATA have valid tokens due to
  11835. batching. See <code>RdataCommand</code> for more details.</p>
  11836. <h3 id="example-5"><a class="header" href="#example-5">Example</a></h3>
  11837. <p>An example iteraction is shown below. Each line is prefixed with '&gt;'
  11838. or '&lt;' to indicate which side is sending, these are <em>not</em> included on
  11839. the wire:</p>
  11840. <pre><code>* connection established *
  11841. &gt; SERVER localhost:8823
  11842. &gt; PING 1490197665618
  11843. &lt; NAME synapse.app.appservice
  11844. &lt; PING 1490197665618
  11845. &lt; REPLICATE
  11846. &gt; POSITION events master 1 1
  11847. &gt; POSITION backfill master 1 1
  11848. &gt; POSITION caches master 1 1
  11849. &gt; RDATA caches master 2 [&quot;get_user_by_id&quot;,[&quot;@01register-user:localhost:8823&quot;],1490197670513]
  11850. &gt; RDATA events master 14 [&quot;$149019767112vOHxz:localhost:8823&quot;,
  11851. &quot;!AFDCvgApUmpdfVjIXm:localhost:8823&quot;,&quot;m.room.guest_access&quot;,&quot;&quot;,null]
  11852. &lt; PING 1490197675618
  11853. &gt; ERROR server stopping
  11854. * connection closed by server *
  11855. </code></pre>
  11856. <p>The <code>POSITION</code> command sent by the server is used to set the clients
  11857. position without needing to send data with the <code>RDATA</code> command.</p>
  11858. <p>An example of a batched set of <code>RDATA</code> is:</p>
  11859. <pre><code>&gt; RDATA caches master batch [&quot;get_user_by_id&quot;,[&quot;@test:localhost:8823&quot;],1490197670513]
  11860. &gt; RDATA caches master batch [&quot;get_user_by_id&quot;,[&quot;@test2:localhost:8823&quot;],1490197670513]
  11861. &gt; RDATA caches master batch [&quot;get_user_by_id&quot;,[&quot;@test3:localhost:8823&quot;],1490197670513]
  11862. &gt; RDATA caches master 54 [&quot;get_user_by_id&quot;,[&quot;@test4:localhost:8823&quot;],1490197670513]
  11863. </code></pre>
  11864. <p>In this case the client shouldn't advance their caches token until it
  11865. sees the the last <code>RDATA</code>.</p>
  11866. <h3 id="list-of-commands"><a class="header" href="#list-of-commands">List of commands</a></h3>
  11867. <p>The list of valid commands, with which side can send it: server (S) or
  11868. client (C):</p>
  11869. <h4 id="server-s"><a class="header" href="#server-s">SERVER (S)</a></h4>
  11870. <p>Sent at the start to identify which server the client is talking to</p>
  11871. <h4 id="rdata-s"><a class="header" href="#rdata-s">RDATA (S)</a></h4>
  11872. <p>A single update in a stream</p>
  11873. <h4 id="position-s"><a class="header" href="#position-s">POSITION (S)</a></h4>
  11874. <p>On receipt of a POSITION command clients should check if they have missed any
  11875. updates, and if so then fetch them out of band. Sent in response to a
  11876. REPLICATE command (but can happen at any time).</p>
  11877. <p>The POSITION command includes the source of the stream. Currently all streams
  11878. are written by a single process (usually &quot;master&quot;). If fetching missing
  11879. updates via HTTP API, rather than via the DB, then processes should make the
  11880. request to the appropriate process.</p>
  11881. <p>Two positions are included, the &quot;new&quot; position and the last position sent respectively.
  11882. This allows servers to tell instances that the positions have advanced but no
  11883. data has been written, without clients needlessly checking to see if they
  11884. have missed any updates.</p>
  11885. <h4 id="error-s-c"><a class="header" href="#error-s-c">ERROR (S, C)</a></h4>
  11886. <p>There was an error</p>
  11887. <h4 id="ping-s-c"><a class="header" href="#ping-s-c">PING (S, C)</a></h4>
  11888. <p>Sent periodically to ensure the connection is still alive</p>
  11889. <h4 id="name-c"><a class="header" href="#name-c">NAME (C)</a></h4>
  11890. <p>Sent at the start by client to inform the server who they are</p>
  11891. <h4 id="replicate-c"><a class="header" href="#replicate-c">REPLICATE (C)</a></h4>
  11892. <p>Asks the server for the current position of all streams.</p>
  11893. <h4 id="user_sync-c"><a class="header" href="#user_sync-c">USER_SYNC (C)</a></h4>
  11894. <p>A user has started or stopped syncing on this process.</p>
  11895. <h4 id="clear_user_sync-c"><a class="header" href="#clear_user_sync-c">CLEAR_USER_SYNC (C)</a></h4>
  11896. <p>The server should clear all associated user sync data from the worker.</p>
  11897. <p>This is used when a worker is shutting down.</p>
  11898. <h4 id="federation_ack-c"><a class="header" href="#federation_ack-c">FEDERATION_ACK (C)</a></h4>
  11899. <p>Acknowledge receipt of some federation data</p>
  11900. <h3 id="remote_server_up-s-c"><a class="header" href="#remote_server_up-s-c">REMOTE_SERVER_UP (S, C)</a></h3>
  11901. <p>Inform other processes that a remote server may have come back online.</p>
  11902. <p>See <code>synapse/replication/tcp/commands.py</code> for a detailed description and
  11903. the format of each command.</p>
  11904. <h3 id="cache-invalidation-stream"><a class="header" href="#cache-invalidation-stream">Cache Invalidation Stream</a></h3>
  11905. <p>The cache invalidation stream is used to inform workers when they need
  11906. to invalidate any of their caches in the data store. This is done by
  11907. streaming all cache invalidations done on master down to the workers,
  11908. assuming that any caches on the workers also exist on the master.</p>
  11909. <p>Each individual cache invalidation results in a row being sent down
  11910. replication, which includes the cache name (the name of the function)
  11911. and they key to invalidate. For example:</p>
  11912. <pre><code>&gt; RDATA caches master 550953771 [&quot;get_user_by_id&quot;, [&quot;@bob:example.com&quot;], 1550574873251]
  11913. </code></pre>
  11914. <p>Alternatively, an entire cache can be invalidated by sending down a <code>null</code>
  11915. instead of the key. For example:</p>
  11916. <pre><code>&gt; RDATA caches master 550953772 [&quot;get_user_by_id&quot;, null, 1550574873252]
  11917. </code></pre>
  11918. <p>However, there are times when a number of caches need to be invalidated
  11919. at the same time with the same key. To reduce traffic we batch those
  11920. invalidations into a single poke by defining a special cache name that
  11921. workers understand to mean to expand to invalidate the correct caches.</p>
  11922. <p>Currently the special cache names are declared in
  11923. <code>synapse/storage/_base.py</code> and are:</p>
  11924. <ol>
  11925. <li><code>cs_cache_fake</code> ─ invalidates caches that depend on the current
  11926. state</li>
  11927. </ol>
  11928. <div style="break-before: page; page-break-before: always;"></div><h1 id="internal-documentation"><a class="header" href="#internal-documentation">Internal Documentation</a></h1>
  11929. <p>This section covers implementation documentation for various parts of Synapse.</p>
  11930. <p>If a developer is planning to make a change to a feature of Synapse, it can be useful for
  11931. general documentation of how that feature is implemented to be available. This saves the
  11932. developer time in place of needing to understand how the feature works by reading the
  11933. code.</p>
  11934. <p>Documentation that would be more useful for the perspective of a system administrator,
  11935. rather than a developer who's intending to change to code, should instead be placed
  11936. under the Usage section of the documentation.</p>
  11937. <div style="break-before: page; page-break-before: always;"></div><h1 id="how-to-test-saml-as-a-developer-without-a-server"><a class="header" href="#how-to-test-saml-as-a-developer-without-a-server">How to test SAML as a developer without a server</a></h1>
  11938. <p>https://capriza.github.io/samling/samling.html (https://github.com/capriza/samling) is a great
  11939. resource for being able to tinker with the SAML options within Synapse without needing to
  11940. deploy and configure a complicated software stack.</p>
  11941. <p>To make Synapse (and therefore Riot) use it:</p>
  11942. <ol>
  11943. <li>Use the samling.html URL above or deploy your own and visit the IdP Metadata tab.</li>
  11944. <li>Copy the XML to your clipboard.</li>
  11945. <li>On your Synapse server, create a new file <code>samling.xml</code> next to your <code>homeserver.yaml</code> with
  11946. the XML from step 2 as the contents.</li>
  11947. <li>Edit your <code>homeserver.yaml</code> to include:
  11948. <pre><code class="language-yaml">saml2_config:
  11949. sp_config:
  11950. allow_unknown_attributes: true # Works around a bug with AVA Hashes: https://github.com/IdentityPython/pysaml2/issues/388
  11951. metadata:
  11952. local: [&quot;samling.xml&quot;]
  11953. </code></pre>
  11954. </li>
  11955. <li>Ensure that your <code>homeserver.yaml</code> has a setting for <code>public_baseurl</code>:
  11956. <pre><code class="language-yaml">public_baseurl: http://localhost:8080/
  11957. </code></pre>
  11958. </li>
  11959. <li>Run <code>apt-get install xmlsec1</code> and <code>pip install --upgrade --force 'pysaml2&gt;=4.5.0'</code> to ensure
  11960. the dependencies are installed and ready to go.</li>
  11961. <li>Restart Synapse.</li>
  11962. </ol>
  11963. <p>Then in Riot:</p>
  11964. <ol>
  11965. <li>Visit the login page with a Riot pointing at your homeserver.</li>
  11966. <li>Click the Single Sign-On button.</li>
  11967. <li>On the samling page, enter a Name Identifier and add a SAML Attribute for <code>uid=your_localpart</code>.
  11968. The response must also be signed.</li>
  11969. <li>Click &quot;Next&quot;.</li>
  11970. <li>Click &quot;Post Response&quot; (change nothing).</li>
  11971. <li>You should be logged in.</li>
  11972. </ol>
  11973. <p>If you try and repeat this process, you may be automatically logged in using the information you
  11974. gave previously. To fix this, open your developer console (<code>F12</code> or <code>Ctrl+Shift+I</code>) while on the
  11975. samling page and clear the site data. In Chrome, this will be a button on the Application tab.</p>
  11976. <div style="break-before: page; page-break-before: always;"></div><h1 id="how-to-test-cas-as-a-developer-without-a-server"><a class="header" href="#how-to-test-cas-as-a-developer-without-a-server">How to test CAS as a developer without a server</a></h1>
  11977. <p>The <a href="https://github.com/jbittel/django-mama-cas">django-mama-cas</a> project is an
  11978. easy to run CAS implementation built on top of Django.</p>
  11979. <h2 id="prerequisites"><a class="header" href="#prerequisites">Prerequisites</a></h2>
  11980. <ol>
  11981. <li>Create a new virtualenv: <code>python3 -m venv &lt;your virtualenv&gt;</code></li>
  11982. <li>Activate your virtualenv: <code>source /path/to/your/virtualenv/bin/activate</code></li>
  11983. <li>Install Django and django-mama-cas:
  11984. <pre><code>python -m pip install &quot;django&lt;3&quot; &quot;django-mama-cas==2.4.0&quot;
  11985. </code></pre>
  11986. </li>
  11987. <li>Create a Django project in the current directory:
  11988. <pre><code>django-admin startproject cas_test .
  11989. </code></pre>
  11990. </li>
  11991. <li>Follow the <a href="https://django-mama-cas.readthedocs.io/en/latest/installation.html#configuring">install directions</a> for django-mama-cas</li>
  11992. <li>Setup the SQLite database: <code>python manage.py migrate</code></li>
  11993. <li>Create a user:
  11994. <pre><code>python manage.py createsuperuser
  11995. </code></pre>
  11996. <ol>
  11997. <li>Use whatever you want as the username and password.</li>
  11998. <li>Leave the other fields blank.</li>
  11999. </ol>
  12000. </li>
  12001. <li>Use the built-in Django test server to serve the CAS endpoints on port 8000:
  12002. <pre><code>python manage.py runserver
  12003. </code></pre>
  12004. </li>
  12005. </ol>
  12006. <p>You should now have a Django project configured to serve CAS authentication with
  12007. a single user created.</p>
  12008. <h2 id="configure-synapse-and-element-to-use-cas"><a class="header" href="#configure-synapse-and-element-to-use-cas">Configure Synapse (and Element) to use CAS</a></h2>
  12009. <ol>
  12010. <li>Modify your <code>homeserver.yaml</code> to enable CAS and point it to your locally
  12011. running Django test server:
  12012. <pre><code class="language-yaml">cas_config:
  12013. enabled: true
  12014. server_url: &quot;http://localhost:8000&quot;
  12015. service_url: &quot;http://localhost:8081&quot;
  12016. #displayname_attribute: name
  12017. #required_attributes:
  12018. # name: value
  12019. </code></pre>
  12020. </li>
  12021. <li>Restart Synapse.</li>
  12022. </ol>
  12023. <p>Note that the above configuration assumes the homeserver is running on port 8081
  12024. and that the CAS server is on port 8000, both on localhost.</p>
  12025. <h2 id="testing-the-configuration"><a class="header" href="#testing-the-configuration">Testing the configuration</a></h2>
  12026. <p>Then in Element:</p>
  12027. <ol>
  12028. <li>Visit the login page with a Element pointing at your homeserver.</li>
  12029. <li>Click the Single Sign-On button.</li>
  12030. <li>Login using the credentials created with <code>createsuperuser</code>.</li>
  12031. <li>You should be logged in.</li>
  12032. </ol>
  12033. <p>If you want to repeat this process you'll need to manually logout first:</p>
  12034. <ol>
  12035. <li>http://localhost:8000/admin/</li>
  12036. <li>Click &quot;logout&quot; in the top right.</li>
  12037. </ol>
  12038. <div style="break-before: page; page-break-before: always;"></div><h1 id="auth-chain-difference-algorithm"><a class="header" href="#auth-chain-difference-algorithm">Auth Chain Difference Algorithm</a></h1>
  12039. <p>The auth chain difference algorithm is used by V2 state resolution, where a
  12040. naive implementation can be a significant source of CPU and DB usage.</p>
  12041. <h3 id="definitions"><a class="header" href="#definitions">Definitions</a></h3>
  12042. <p>A <em>state set</em> is a set of state events; e.g. the input of a state resolution
  12043. algorithm is a collection of state sets.</p>
  12044. <p>The <em>auth chain</em> of a set of events are all the events' auth events and <em>their</em>
  12045. auth events, recursively (i.e. the events reachable by walking the graph induced
  12046. by an event's auth events links).</p>
  12047. <p>The <em>auth chain difference</em> of a collection of state sets is the union minus the
  12048. intersection of the sets of auth chains corresponding to the state sets, i.e an
  12049. event is in the auth chain difference if it is reachable by walking the auth
  12050. event graph from at least one of the state sets but not from <em>all</em> of the state
  12051. sets.</p>
  12052. <h2 id="breadth-first-walk-algorithm"><a class="header" href="#breadth-first-walk-algorithm">Breadth First Walk Algorithm</a></h2>
  12053. <p>A way of calculating the auth chain difference without calculating the full auth
  12054. chains for each state set is to do a parallel breadth first walk (ordered by
  12055. depth) of each state set's auth chain. By tracking which events are reachable
  12056. from each state set we can finish early if every pending event is reachable from
  12057. every state set.</p>
  12058. <p>This can work well for state sets that have a small auth chain difference, but
  12059. can be very inefficient for larger differences. However, this algorithm is still
  12060. used if we don't have a chain cover index for the room (e.g. because we're in
  12061. the process of indexing it).</p>
  12062. <h2 id="chain-cover-index"><a class="header" href="#chain-cover-index">Chain Cover Index</a></h2>
  12063. <p>Synapse computes auth chain differences by pre-computing a &quot;chain cover&quot; index
  12064. for the auth chain in a room, allowing efficient reachability queries like &quot;is
  12065. event A in the auth chain of event B&quot;. This is done by assigning every event a
  12066. <em>chain ID</em> and <em>sequence number</em> (e.g. <code>(5,3)</code>), and having a map of <em>links</em>
  12067. between chains (e.g. <code>(5,3) -&gt; (2,4)</code>) such that A is reachable by B (i.e. <code>A</code>
  12068. is in the auth chain of <code>B</code>) if and only if either:</p>
  12069. <ol>
  12070. <li>A and B have the same chain ID and <code>A</code>'s sequence number is less than <code>B</code>'s
  12071. sequence number; or</li>
  12072. <li>there is a link <code>L</code> between <code>B</code>'s chain ID and <code>A</code>'s chain ID such that
  12073. <code>L.start_seq_no</code> &lt;= <code>B.seq_no</code> and <code>A.seq_no</code> &lt;= <code>L.end_seq_no</code>.</li>
  12074. </ol>
  12075. <p>There are actually two potential implementations, one where we store links from
  12076. each chain to every other reachable chain (the transitive closure of the links
  12077. graph), and one where we remove redundant links (the transitive reduction of the
  12078. links graph) e.g. if we have chains <code>C3 -&gt; C2 -&gt; C1</code> then the link <code>C3 -&gt; C1</code>
  12079. would not be stored. Synapse uses the former implementations so that it doesn't
  12080. need to recurse to test reachability between chains.</p>
  12081. <h3 id="example-6"><a class="header" href="#example-6">Example</a></h3>
  12082. <p>An example auth graph would look like the following, where chains have been
  12083. formed based on type/state_key and are denoted by colour and are labelled with
  12084. <code>(chain ID, sequence number)</code>. Links are denoted by the arrows (links in grey
  12085. are those that would be remove in the second implementation described above).</p>
  12086. <p><img src="auth_chain_diff.dot.png" alt="Example" /></p>
  12087. <p>Note that we don't include all links between events and their auth events, as
  12088. most of those links would be redundant. For example, all events point to the
  12089. create event, but each chain only needs the one link from it's base to the
  12090. create event.</p>
  12091. <h2 id="using-the-index"><a class="header" href="#using-the-index">Using the Index</a></h2>
  12092. <p>This index can be used to calculate the auth chain difference of the state sets
  12093. by looking at the chain ID and sequence numbers reachable from each state set:</p>
  12094. <ol>
  12095. <li>For every state set lookup the chain ID/sequence numbers of each state event</li>
  12096. <li>Use the index to find all chains and the maximum sequence number reachable
  12097. from each state set.</li>
  12098. <li>The auth chain difference is then all events in each chain that have sequence
  12099. numbers between the maximum sequence number reachable from <em>any</em> state set and
  12100. the minimum reachable by <em>all</em> state sets (if any).</li>
  12101. </ol>
  12102. <p>Note that steps 2 is effectively calculating the auth chain for each state set
  12103. (in terms of chain IDs and sequence numbers), and step 3 is calculating the
  12104. difference between the union and intersection of the auth chains.</p>
  12105. <h3 id="worked-example"><a class="header" href="#worked-example">Worked Example</a></h3>
  12106. <p>For example, given the above graph, we can calculate the difference between
  12107. state sets consisting of:</p>
  12108. <ol>
  12109. <li><code>S1</code>: Alice's invite <code>(4,1)</code> and Bob's second join <code>(2,2)</code>; and</li>
  12110. <li><code>S2</code>: Alice's second join <code>(4,3)</code> and Bob's first join <code>(2,1)</code>.</li>
  12111. </ol>
  12112. <p>Using the index we see that the following auth chains are reachable from each
  12113. state set:</p>
  12114. <ol>
  12115. <li><code>S1</code>: <code>(1,1)</code>, <code>(2,2)</code>, <code>(3,1)</code> &amp; <code>(4,1)</code></li>
  12116. <li><code>S2</code>: <code>(1,1)</code>, <code>(2,1)</code>, <code>(3,2)</code> &amp; <code>(4,3)</code></li>
  12117. </ol>
  12118. <p>And so, for each the ranges that are in the auth chain difference:</p>
  12119. <ol>
  12120. <li>Chain 1: None, (since everything can reach the create event).</li>
  12121. <li>Chain 2: The range <code>(1, 2]</code> (i.e. just <code>2</code>), as <code>1</code> is reachable by all state
  12122. sets and the maximum reachable is <code>2</code> (corresponding to Bob's second join).</li>
  12123. <li>Chain 3: Similarly the range <code>(1, 2]</code> (corresponding to the second power
  12124. level).</li>
  12125. <li>Chain 4: The range <code>(1, 3]</code> (corresponding to both of Alice's joins).</li>
  12126. </ol>
  12127. <p>So the final result is: Bob's second join <code>(2,2)</code>, the second power level
  12128. <code>(3,2)</code> and both of Alice's joins <code>(4,2)</code> &amp; <code>(4,3)</code>.</p>
  12129. <div style="break-before: page; page-break-before: always;"></div><h1 id="media-repository"><a class="header" href="#media-repository">Media Repository</a></h1>
  12130. <p><em>Synapse implementation-specific details for the media repository</em></p>
  12131. <p>The media repository is where attachments and avatar photos are stored.
  12132. It stores attachment content and thumbnails for media uploaded by local users.
  12133. It caches attachment content and thumbnails for media uploaded by remote users.</p>
  12134. <h2 id="storage"><a class="header" href="#storage">Storage</a></h2>
  12135. <p>Each item of media is assigned a <code>media_id</code> when it is uploaded.
  12136. The <code>media_id</code> is a randomly chosen, URL safe 24 character string.</p>
  12137. <p>Metadata such as the MIME type, upload time and length are stored in the
  12138. sqlite3 database indexed by <code>media_id</code>.</p>
  12139. <p>Content is stored on the filesystem under a <code>&quot;local_content&quot;</code> directory.</p>
  12140. <p>Thumbnails are stored under a <code>&quot;local_thumbnails&quot;</code> directory.</p>
  12141. <p>The item with <code>media_id</code> <code>&quot;aabbccccccccdddddddddddd&quot;</code> is stored under
  12142. <code>&quot;local_content/aa/bb/ccccccccdddddddddddd&quot;</code>. Its thumbnail with width
  12143. <code>128</code> and height <code>96</code> and type <code>&quot;image/jpeg&quot;</code> is stored under
  12144. <code>&quot;local_thumbnails/aa/bb/ccccccccdddddddddddd/128-96-image-jpeg&quot;</code></p>
  12145. <p>Remote content is cached under <code>&quot;remote_content&quot;</code> directory. Each item of
  12146. remote content is assigned a local <code>&quot;filesystem_id&quot;</code> to ensure that the
  12147. directory structure <code>&quot;remote_content/server_name/aa/bb/ccccccccdddddddddddd&quot;</code>
  12148. is appropriate. Thumbnails for remote content are stored under
  12149. <code>&quot;remote_thumbnails/server_name/...&quot;</code></p>
  12150. <div style="break-before: page; page-break-before: always;"></div><h1 id="room-and-user-statistics"><a class="header" href="#room-and-user-statistics">Room and User Statistics</a></h1>
  12151. <p>Synapse maintains room and user statistics (as well as a cache of room state),
  12152. in various tables. These can be used for administrative purposes but are also
  12153. used when generating the public room directory.</p>
  12154. <h1 id="synapse-developer-documentation"><a class="header" href="#synapse-developer-documentation">Synapse Developer Documentation</a></h1>
  12155. <h2 id="high-level-concepts"><a class="header" href="#high-level-concepts">High-Level Concepts</a></h2>
  12156. <h3 id="definitions-1"><a class="header" href="#definitions-1">Definitions</a></h3>
  12157. <ul>
  12158. <li><strong>subject</strong>: Something we are tracking stats about – currently a room or user.</li>
  12159. <li><strong>current row</strong>: An entry for a subject in the appropriate current statistics
  12160. table. Each subject can have only one.</li>
  12161. <li><strong>historical row</strong>: An entry for a subject in the appropriate historical
  12162. statistics table. Each subject can have any number of these.</li>
  12163. </ul>
  12164. <h3 id="overview-4"><a class="header" href="#overview-4">Overview</a></h3>
  12165. <p>Stats are maintained as time series. There are two kinds of column:</p>
  12166. <ul>
  12167. <li>absolute columns – where the value is correct for the time given by <code>end_ts</code>
  12168. in the stats row. (Imagine a line graph for these values)
  12169. <ul>
  12170. <li>They can also be thought of as 'gauges' in Prometheus, if you are familiar.</li>
  12171. </ul>
  12172. </li>
  12173. <li>per-slice columns – where the value corresponds to how many of the occurrences
  12174. occurred within the time slice given by <code>(end_ts − bucket_size)…end_ts</code>
  12175. or <code>start_ts…end_ts</code>. (Imagine a histogram for these values)</li>
  12176. </ul>
  12177. <p>Stats are maintained in two tables (for each type): current and historical.</p>
  12178. <p>Current stats correspond to the present values. Each subject can only have one
  12179. entry.</p>
  12180. <p>Historical stats correspond to values in the past. Subjects may have multiple
  12181. entries.</p>
  12182. <h2 id="concepts-around-the-management-of-stats"><a class="header" href="#concepts-around-the-management-of-stats">Concepts around the management of stats</a></h2>
  12183. <h3 id="current-rows"><a class="header" href="#current-rows">Current rows</a></h3>
  12184. <p>Current rows contain the most up-to-date statistics for a room.
  12185. They only contain absolute columns</p>
  12186. <h3 id="historical-rows"><a class="header" href="#historical-rows">Historical rows</a></h3>
  12187. <p>Historical rows can always be considered to be valid for the time slice and
  12188. end time specified.</p>
  12189. <ul>
  12190. <li>historical rows will not exist for every time slice – they will be omitted
  12191. if there were no changes. In this case, the following assumptions can be
  12192. made to interpolate/recreate missing rows:
  12193. <ul>
  12194. <li>absolute fields have the same values as in the preceding row</li>
  12195. <li>per-slice fields are zero (<code>0</code>)</li>
  12196. </ul>
  12197. </li>
  12198. <li>historical rows will not be retained forever – rows older than a configurable
  12199. time will be purged.</li>
  12200. </ul>
  12201. <h4 id="purge"><a class="header" href="#purge">Purge</a></h4>
  12202. <p>The purging of historical rows is not yet implemented.</p>
  12203. <div style="break-before: page; page-break-before: always;"></div><h1 id="deprecation-policy-for-platform-dependencies"><a class="header" href="#deprecation-policy-for-platform-dependencies">Deprecation Policy for Platform Dependencies</a></h1>
  12204. <p>Synapse has a number of platform dependencies, including Python and PostgreSQL.
  12205. This document outlines the policy towards which versions we support, and when we
  12206. drop support for versions in the future.</p>
  12207. <h2 id="policy"><a class="header" href="#policy">Policy</a></h2>
  12208. <p>Synapse follows the upstream support life cycles for Python and PostgreSQL,
  12209. i.e. when a version reaches End of Life Synapse will withdraw support for that
  12210. version in future releases.</p>
  12211. <p>Details on the upstream support life cycles for Python and PostgreSQL are
  12212. documented at https://endoflife.date/python and
  12213. https://endoflife.date/postgresql.</p>
  12214. <h2 id="context"><a class="header" href="#context">Context</a></h2>
  12215. <p>It is important for system admins to have a clear understanding of the platform
  12216. requirements of Synapse and its deprecation policies so that they can
  12217. effectively plan upgrading their infrastructure ahead of time. This is
  12218. especially important in contexts where upgrading the infrastructure requires
  12219. auditing and approval from a security team, or where otherwise upgrading is a
  12220. long process.</p>
  12221. <p>By following the upstream support life cycles Synapse can ensure that its
  12222. dependencies continue to get security patches, while not requiring system admins
  12223. to constantly update their platform dependencies to the latest versions.</p>
  12224. </main>
  12225. <nav class="nav-wrapper" aria-label="Page navigation">
  12226. <!-- Mobile navigation buttons -->
  12227. <div style="clear: both"></div>
  12228. </nav>
  12229. </div>
  12230. </div>
  12231. <nav class="nav-wide-wrapper" aria-label="Page navigation">
  12232. </nav>
  12233. </div>
  12234. <script type="text/javascript">
  12235. window.playground_copyable = true;
  12236. </script>
  12237. <script src="elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
  12238. <script src="mark.min.js" type="text/javascript" charset="utf-8"></script>
  12239. <script src="searcher.js" type="text/javascript" charset="utf-8"></script>
  12240. <script src="clipboard.min.js" type="text/javascript" charset="utf-8"></script>
  12241. <script src="highlight.js" type="text/javascript" charset="utf-8"></script>
  12242. <script src="book.js" type="text/javascript" charset="utf-8"></script>
  12243. <!-- Custom JS scripts -->
  12244. <script type="text/javascript" src="docs/website_files/table-of-contents.js"></script>
  12245. <script type="text/javascript" src="docs/website_files/version-picker.js"></script>
  12246. <script type="text/javascript" src="docs/website_files/version.js"></script>
  12247. <script type="text/javascript">
  12248. window.addEventListener('load', function() {
  12249. window.setTimeout(window.print, 100);
  12250. });
  12251. </script>
  12252. </body>
  12253. </html>