test_plugin.py 117 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056
  1. import os.path
  2. import zipfile
  3. from base64 import b64encode
  4. from io import BytesIO
  5. from unittest.mock import patch
  6. from uuid import uuid4
  7. import pytest
  8. import responses
  9. from django.utils.encoding import force_bytes
  10. from sentry.models import (
  11. ArtifactBundle,
  12. DebugIdArtifactBundle,
  13. File,
  14. ProjectArtifactBundle,
  15. Release,
  16. ReleaseArtifactBundle,
  17. ReleaseFile,
  18. SourceFileType,
  19. )
  20. from sentry.models.releasefile import update_artifact_index
  21. from sentry.testutils import RelayStoreHelper
  22. from sentry.testutils.helpers import override_options
  23. from sentry.testutils.helpers.datetime import before_now, iso_format
  24. from sentry.testutils.skips import requires_symbolicator
  25. from sentry.utils import json
  26. # IMPORTANT:
  27. #
  28. # This test suite requires Symbolicator in order to run correctly.
  29. # Set `symbolicator.enabled: true` in your `~/.sentry/config.yml` and run `sentry devservices up`
  30. #
  31. # If you are using a local instance of Symbolicator, you need to either change `system.url-prefix` to `system.internal-url-prefix`
  32. # inside `process_with_symbolicator` fixture inside `src/sentry/utils/pytest/fixtures.py`,
  33. # or add `127.0.0.1 host.docker.internal` entry to your `/etc/hosts`
  34. BASE64_SOURCEMAP = "data:application/json;base64," + (
  35. b64encode(
  36. b'{"version":3,"file":"generated.js","sources":["/test.js"],"names":[],"mappings":"AAAA","sourcesContent":['
  37. b'"console.log(\\"hello, World!\\")"]}'
  38. )
  39. .decode("utf-8")
  40. .replace("\n", "")
  41. )
  42. INVALID_BASE64_SOURCEMAP = "data:application/json;base64,A"
  43. def get_fixture_path(name):
  44. return os.path.join(os.path.dirname(__file__), "fixtures", name)
  45. def load_fixture(name):
  46. with open(get_fixture_path(name), "rb") as fp:
  47. return fp.read()
  48. @pytest.mark.django_db(transaction=True)
  49. class TestJavascriptIntegration(RelayStoreHelper):
  50. @pytest.fixture(autouse=True)
  51. def initialize(self, default_projectkey, default_project):
  52. self.project = default_project
  53. self.projectkey = default_projectkey
  54. self.organization = self.project.organization
  55. self.min_ago = iso_format(before_now(minutes=1))
  56. # We disable scraping per-test when necessary.
  57. self.project.update_option("sentry:scrape_javascript", True)
  58. @requires_symbolicator
  59. @pytest.mark.symbolicator
  60. def test_adds_contexts_without_device(self, process_with_symbolicator):
  61. data = {
  62. "timestamp": self.min_ago,
  63. "message": "hello",
  64. "platform": "javascript",
  65. "request": {
  66. "url": "http://example.com",
  67. "headers": [
  68. [
  69. "User-Agent",
  70. "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) "
  71. "Chrome/28.0.1500.72 Safari/537.36",
  72. ]
  73. ],
  74. },
  75. }
  76. event = self.post_and_retrieve_event(data)
  77. contexts = event.interfaces["contexts"].to_json()
  78. assert contexts.get("os") == {"name": "Windows", "version": "8", "type": "os"}
  79. assert contexts.get("device") is None
  80. @requires_symbolicator
  81. @pytest.mark.symbolicator
  82. def test_adds_contexts_with_device(self, process_with_symbolicator):
  83. data = {
  84. "timestamp": self.min_ago,
  85. "message": "hello",
  86. "platform": "javascript",
  87. "request": {
  88. "url": "http://example.com",
  89. "headers": [
  90. [
  91. "User-Agent",
  92. "Mozilla/5.0 (Linux; U; Android 4.3; en-us; SCH-R530U Build/JSS15J) AppleWebKit/534.30 ("
  93. "KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 USCC-R530U",
  94. ]
  95. ],
  96. },
  97. }
  98. event = self.post_and_retrieve_event(data)
  99. contexts = event.interfaces["contexts"].to_json()
  100. assert contexts.get("os") == {"name": "Android", "type": "os", "version": "4.3"}
  101. assert contexts.get("browser") == {"name": "Android", "type": "browser", "version": "4.3"}
  102. assert contexts.get("device") == {
  103. "family": "Samsung SCH-R530U",
  104. "type": "device",
  105. "model": "SCH-R530U",
  106. "name": "Galaxy S3",
  107. "brand": "Samsung",
  108. }
  109. @requires_symbolicator
  110. @pytest.mark.symbolicator
  111. def test_adds_contexts_with_ps4_device(self, process_with_symbolicator):
  112. data = {
  113. "timestamp": self.min_ago,
  114. "message": "hello",
  115. "platform": "javascript",
  116. "request": {
  117. "url": "http://example.com",
  118. "headers": [
  119. [
  120. "User-Agent",
  121. "Mozilla/5.0 (PlayStation 4 3.55) AppleWebKit/537.78 (KHTML, like Gecko)",
  122. ]
  123. ],
  124. },
  125. }
  126. event = self.post_and_retrieve_event(data)
  127. contexts = event.interfaces["contexts"].to_json()
  128. assert contexts.get("os") is None
  129. assert contexts.get("browser") is None
  130. assert contexts.get("device") == {
  131. "family": "PlayStation 4",
  132. "type": "device",
  133. "model": "PlayStation 4",
  134. "brand": "Sony",
  135. }
  136. @patch("sentry.lang.javascript.processor.Fetcher.fetch_by_url")
  137. def test_source_expansion(self, mock_fetch_by_url):
  138. data = {
  139. "timestamp": self.min_ago,
  140. "message": "hello",
  141. "platform": "javascript",
  142. "exception": {
  143. "values": [
  144. {
  145. "type": "Error",
  146. "stacktrace": {
  147. "frames": [
  148. {
  149. "abs_path": "http://example.com/foo.js",
  150. "filename": "foo.js",
  151. "lineno": 4,
  152. "colno": 0,
  153. },
  154. {
  155. "abs_path": "http://example.com/foo.js",
  156. "filename": "foo.js",
  157. "lineno": 1,
  158. "colno": 0,
  159. },
  160. ]
  161. },
  162. }
  163. ]
  164. },
  165. }
  166. mock_fetch_by_url.return_value.body = force_bytes("\n".join("hello world"))
  167. mock_fetch_by_url.return_value.encoding = None
  168. mock_fetch_by_url.return_value.headers = {}
  169. event = self.post_and_retrieve_event(data)
  170. mock_fetch_by_url.assert_called_once_with("http://example.com/foo.js")
  171. exception = event.interfaces["exception"]
  172. frame_list = exception.values[0].stacktrace.frames
  173. frame = frame_list[0]
  174. assert frame.pre_context == ["h", "e", "l"]
  175. assert frame.context_line == "l"
  176. assert frame.post_context == ["o", " ", "w", "o", "r"]
  177. frame = frame_list[1]
  178. assert not frame.pre_context
  179. assert frame.context_line == "h"
  180. assert frame.post_context == ["e", "l", "l", "o", " "]
  181. # no source map means no raw_stacktrace
  182. assert exception.values[0].raw_stacktrace is None
  183. @patch("sentry.lang.javascript.processor.Fetcher.fetch_by_url")
  184. @patch("sentry.lang.javascript.processor.discover_sourcemap")
  185. def test_inlined_sources(self, mock_discover_sourcemap, mock_fetch_by_url):
  186. data = {
  187. "timestamp": self.min_ago,
  188. "message": "hello",
  189. "platform": "javascript",
  190. "exception": {
  191. "values": [
  192. {
  193. "type": "Error",
  194. "stacktrace": {
  195. "frames": [
  196. {
  197. "abs_path": "http://example.com/test.min.js",
  198. "filename": "test.js",
  199. "lineno": 1,
  200. "colno": 1,
  201. }
  202. ]
  203. },
  204. }
  205. ]
  206. },
  207. }
  208. mock_discover_sourcemap.return_value = BASE64_SOURCEMAP
  209. mock_fetch_by_url.return_value.url = "http://example.com/test.min.js"
  210. mock_fetch_by_url.return_value.body = force_bytes("\n".join("<generated source>"))
  211. mock_fetch_by_url.return_value.encoding = None
  212. event = self.post_and_retrieve_event(data)
  213. mock_fetch_by_url.assert_called_once_with("http://example.com/test.min.js")
  214. exception = event.interfaces["exception"]
  215. frame_list = exception.values[0].stacktrace.frames
  216. frame = frame_list[0]
  217. assert not frame.pre_context
  218. assert frame.context_line == 'console.log("hello, World!")'
  219. assert not frame.post_context
  220. assert frame.data["sourcemap"] == "http://example.com/test.min.js"
  221. @patch("sentry.lang.javascript.processor.Fetcher.fetch_by_url")
  222. @patch("sentry.lang.javascript.processor.discover_sourcemap")
  223. def test_invalid_base64_sourcemap_returns_an_error(
  224. self, mock_discover_sourcemap, mock_fetch_by_url
  225. ):
  226. data = {
  227. "timestamp": self.min_ago,
  228. "message": "hello",
  229. "platform": "javascript",
  230. "exception": {
  231. "values": [
  232. {
  233. "type": "Error",
  234. "stacktrace": {
  235. "frames": [
  236. {
  237. "abs_path": "http://example.com/test.min.js",
  238. "filename": "test.js",
  239. "lineno": 1,
  240. "colno": 1,
  241. },
  242. ]
  243. },
  244. }
  245. ]
  246. },
  247. }
  248. mock_discover_sourcemap.return_value = INVALID_BASE64_SOURCEMAP
  249. mock_fetch_by_url.return_value.url = "http://example.com/test.min.js"
  250. mock_fetch_by_url.return_value.body = force_bytes("\n".join("<generated source>"))
  251. mock_fetch_by_url.return_value.encoding = None
  252. event = self.post_and_retrieve_event(data)
  253. mock_fetch_by_url.assert_called_once_with("http://example.com/test.min.js")
  254. assert len(event.data["errors"]) == 1
  255. assert event.data["errors"][0] == {
  256. "url": "<base64>",
  257. "reason": "Invalid base64-encoded string: "
  258. "number of data characters (1) cannot be 1 more than a multiple of 4",
  259. "type": "js_invalid_source",
  260. }
  261. @patch("sentry.lang.javascript.processor.SmCache.from_bytes")
  262. @patch("sentry.lang.javascript.processor.Fetcher.fetch_by_url")
  263. @patch("sentry.lang.javascript.processor.discover_sourcemap")
  264. def test_sourcemap_cache_is_constructed_only_once_if_an_error_is_raised(
  265. self, mock_discover_sourcemap, mock_fetch_by_url, mock_from_bytes
  266. ):
  267. data = {
  268. "timestamp": self.min_ago,
  269. "message": "hello",
  270. "platform": "javascript",
  271. "exception": {
  272. "values": [
  273. {
  274. "type": "Error",
  275. "stacktrace": {
  276. "frames": [
  277. {
  278. "abs_path": "http://example.com/test.min.js",
  279. "filename": "test.js",
  280. "lineno": 1,
  281. "colno": 1,
  282. },
  283. {
  284. "abs_path": "http://example.com/test.min.js",
  285. "filename": "test.js",
  286. "lineno": 1,
  287. "colno": 1,
  288. },
  289. {
  290. "abs_path": "http://example.com/test.min.js",
  291. "filename": "test.js",
  292. "lineno": 1,
  293. "colno": 1,
  294. },
  295. ]
  296. },
  297. }
  298. ]
  299. },
  300. }
  301. mock_discover_sourcemap.return_value = BASE64_SOURCEMAP
  302. mock_fetch_by_url.return_value.url = "http://example.com/test.min.js"
  303. mock_fetch_by_url.return_value.body = force_bytes("\n".join("<generated source>"))
  304. mock_fetch_by_url.return_value.encoding = None
  305. mock_from_bytes.side_effect = Exception()
  306. self.post_and_retrieve_event(data)
  307. mock_fetch_by_url.assert_called_once_with("http://example.com/test.min.js")
  308. mock_from_bytes.assert_called_once()
  309. @requires_symbolicator
  310. @pytest.mark.symbolicator
  311. def test_error_message_translations(self, process_with_symbolicator):
  312. data = {
  313. "timestamp": self.min_ago,
  314. "message": "hello",
  315. "platform": "javascript",
  316. "logentry": {
  317. "formatted": "ReferenceError: Impossible de d\xe9finir une propri\xe9t\xe9 \xab foo \xbb : objet non "
  318. "extensible"
  319. },
  320. "exception": {
  321. "values": [
  322. {"type": "Error", "value": "P\u0159\xedli\u0161 mnoho soubor\u016f"},
  323. {
  324. "type": "Error",
  325. "value": "foo: wyst\u0105pi\u0142 nieoczekiwany b\u0142\u0105d podczas pr\xf3by uzyskania "
  326. "informacji o metadanych",
  327. },
  328. ]
  329. },
  330. }
  331. event = self.post_and_retrieve_event(data)
  332. message = event.interfaces["logentry"]
  333. assert (
  334. message.formatted
  335. == "ReferenceError: Cannot define property 'foo': object is not extensible"
  336. )
  337. exception = event.interfaces["exception"]
  338. assert exception.values[0].value == "Too many files"
  339. assert (
  340. exception.values[1].value
  341. == "foo: an unexpected failure occurred while trying to obtain metadata information"
  342. )
  343. @requires_symbolicator
  344. @pytest.mark.symbolicator
  345. def test_nonhandled_frames_inapp_normalization(self, process_with_symbolicator):
  346. data = {
  347. "timestamp": self.min_ago,
  348. "message": "hello",
  349. "platform": "node",
  350. "exception": {
  351. "values": [
  352. {
  353. "type": "Error",
  354. "stacktrace": {
  355. "frames": [
  356. {
  357. "abs_path": "native",
  358. "lineno": 1,
  359. "colno": 1,
  360. "in_app": True,
  361. },
  362. {
  363. "abs_path": "[native code]",
  364. "lineno": 1,
  365. "colno": 1,
  366. "in_app": True,
  367. },
  368. {
  369. "abs_path": "app://dist/bundle/file.min.js",
  370. "lineno": 1,
  371. "colno": 1,
  372. "in_app": True,
  373. },
  374. ]
  375. },
  376. }
  377. ]
  378. },
  379. }
  380. event = self.post_and_retrieve_event(data)
  381. exception = event.interfaces["exception"]
  382. if process_with_symbolicator:
  383. frame_list = exception.values[0].stacktrace.frames
  384. assert not frame_list[0].in_app # should be overwritten due to `native` abs_path
  385. assert not frame_list[1].in_app # should be overwritten due to `[native code]` abs_path
  386. assert frame_list[2].in_app # should not be touched and retain `in_app: true`
  387. raw_frame_list = exception.values[0].raw_stacktrace.frames
  388. # none of the raw frames should be altered
  389. assert raw_frame_list[0].in_app
  390. assert raw_frame_list[1].in_app
  391. assert raw_frame_list[2].in_app
  392. @requires_symbolicator
  393. @pytest.mark.symbolicator
  394. def test_sourcemap_source_expansion(self, process_with_symbolicator):
  395. self.project.update_option("sentry:scrape_javascript", False)
  396. release = Release.objects.create(
  397. organization_id=self.project.organization_id, version="abc"
  398. )
  399. release.add_project(self.project)
  400. for file in ["file.min.js", "file1.js", "file2.js", "file.sourcemap.js"]:
  401. with open(get_fixture_path(file), "rb") as f:
  402. f1 = File.objects.create(
  403. name=file,
  404. type="release.file",
  405. headers={},
  406. )
  407. f1.putfile(f)
  408. ReleaseFile.objects.create(
  409. name=f"http://example.com/{f1.name}",
  410. release_id=release.id,
  411. organization_id=self.project.organization_id,
  412. file=f1,
  413. )
  414. data = {
  415. "timestamp": self.min_ago,
  416. "message": "hello",
  417. "platform": "javascript",
  418. "release": "abc",
  419. "exception": {
  420. "values": [
  421. {
  422. "type": "Error",
  423. "stacktrace": {
  424. "frames": [
  425. {
  426. "abs_path": "http://example.com/file.min.js",
  427. "filename": "file.min.js",
  428. "lineno": 1,
  429. "colno": 39,
  430. },
  431. # NOTE: Intentionally source is not retrieved from this HTML file
  432. {
  433. "function": 'function: "HTMLDocument.<anonymous>"',
  434. "abs_path": "http//example.com/index.html",
  435. "filename": "index.html",
  436. "lineno": 283,
  437. "colno": 17,
  438. "in_app": False,
  439. },
  440. # NOTE: a mixed stack trace with a native frame:
  441. {
  442. "instruction_addr": "0xd10349",
  443. },
  444. ]
  445. },
  446. }
  447. ]
  448. },
  449. }
  450. event = self.post_and_retrieve_event(data)
  451. assert event.data["errors"] == [
  452. {"type": "js_no_source", "url": "http//example.com/index.html"}
  453. ]
  454. exception = event.interfaces["exception"]
  455. frame_list = exception.values[0].stacktrace.frames
  456. frame = frame_list[0]
  457. if process_with_symbolicator:
  458. assert frame.data["resolved_with"] == "release-old"
  459. assert frame.data["symbolicated"]
  460. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  461. expected = "\treturn a + b; // fôo"
  462. assert frame.context_line == expected
  463. if process_with_symbolicator:
  464. assert frame.post_context == ["}"]
  465. else:
  466. assert frame.post_context == ["}", ""]
  467. raw_frame_list = exception.values[0].raw_stacktrace.frames
  468. raw_frame = raw_frame_list[0]
  469. assert not raw_frame.pre_context
  470. assert (
  471. raw_frame.context_line
  472. == 'function add(a,b){"use strict";return a+b}function multiply(a,b){"use strict";return a*b}function '
  473. 'divide(a,b){"use strict";try{return multip {snip}'
  474. )
  475. if process_with_symbolicator:
  476. assert raw_frame.post_context == ["//@ sourceMappingURL=file.sourcemap.js"]
  477. else:
  478. assert raw_frame.post_context == ["//@ sourceMappingURL=file.sourcemap.js", ""]
  479. assert raw_frame.lineno == 1
  480. # Since we couldn't expand source for the 2nd frame, both
  481. # its raw and original form should be identical
  482. assert raw_frame_list[1] == frame_list[1]
  483. # The second non-js frame should be untouched
  484. assert raw_frame_list[2] == frame_list[2]
  485. @requires_symbolicator
  486. @pytest.mark.symbolicator
  487. def test_sourcemap_webpack(self, process_with_symbolicator):
  488. self.project.update_option("sentry:scrape_javascript", False)
  489. release = Release.objects.create(
  490. organization_id=self.project.organization_id, version="abc"
  491. )
  492. release.add_project(self.project)
  493. for file in [
  494. "webpack1.min.js",
  495. "webpack2.min.js",
  496. "webpack1.min.js.map",
  497. "webpack2.min.js.map",
  498. ]:
  499. with open(get_fixture_path(file), "rb") as f:
  500. f1 = File.objects.create(
  501. name=file,
  502. type="release.file",
  503. headers={},
  504. )
  505. f1.putfile(f)
  506. ReleaseFile.objects.create(
  507. name=f"http://example.com/{f1.name}",
  508. release_id=release.id,
  509. organization_id=self.project.organization_id,
  510. file=f1,
  511. )
  512. data = {
  513. "timestamp": self.min_ago,
  514. "message": "hello",
  515. "platform": "javascript",
  516. "release": "abc",
  517. "exception": {
  518. "values": [
  519. {
  520. "type": "Error",
  521. "stacktrace": {
  522. "frames": [
  523. {
  524. "abs_path": "http://example.com/webpack1.min.js",
  525. "filename": "webpack1.min.js",
  526. "lineno": 1,
  527. "colno": 183,
  528. "function": "i",
  529. },
  530. {
  531. "abs_path": "http://example.com/webpack2.min.js",
  532. "filename": "webpack2.min.js",
  533. "lineno": 1,
  534. "colno": 183,
  535. "function": "i",
  536. },
  537. ]
  538. },
  539. }
  540. ]
  541. },
  542. }
  543. event = self.post_and_retrieve_event(data)
  544. exception = event.interfaces["exception"]
  545. frame_list = exception.values[0].stacktrace.frames
  546. # The first frame should be in_app.
  547. first_frame = frame_list[0]
  548. assert first_frame.in_app
  549. assert first_frame.function == "test"
  550. assert first_frame.pre_context == [
  551. " cb(data);",
  552. " }",
  553. "",
  554. " function test() {",
  555. " var data = {failed: true, value: 42};",
  556. ]
  557. assert first_frame.context_line == " invoke(data);"
  558. if process_with_symbolicator:
  559. assert first_frame.post_context == [
  560. " }",
  561. "",
  562. " return test;",
  563. "})();",
  564. ]
  565. else:
  566. assert first_frame.post_context == [" }", "", " return test;", "})();", ""]
  567. # The second frame should be identical to the first, except not in_app.
  568. second_frame = frame_list[1]
  569. assert not second_frame.in_app
  570. assert second_frame.function == first_frame.function
  571. assert second_frame.context_line == first_frame.context_line
  572. assert second_frame.pre_context == first_frame.pre_context
  573. assert second_frame.post_context == first_frame.post_context
  574. @requires_symbolicator
  575. @pytest.mark.symbolicator
  576. def test_sourcemap_source_expansion_ab_test(self, process_with_symbolicator):
  577. self.project.update_option("sentry:scrape_javascript", False)
  578. release = Release.objects.create(
  579. organization_id=self.project.organization_id, version="abc"
  580. )
  581. release.add_project(self.project)
  582. for file in ["file.min.js", "file1.js", "file2.js", "file.sourcemap.js"]:
  583. with open(get_fixture_path(file), "rb") as f:
  584. f1 = File.objects.create(
  585. name=file,
  586. type="release.file",
  587. headers={},
  588. )
  589. f1.putfile(f)
  590. ReleaseFile.objects.create(
  591. name=f"http://example.com/{f1.name}",
  592. release_id=release.id,
  593. organization_id=self.project.organization_id,
  594. file=f1,
  595. )
  596. data = {
  597. "timestamp": self.min_ago,
  598. "message": "hello",
  599. "platform": "javascript",
  600. "release": "abc",
  601. "exception": {
  602. "values": [
  603. {
  604. "type": "Error",
  605. "stacktrace": {
  606. "frames": [
  607. {
  608. "abs_path": "http://example.com/file.min.js",
  609. "filename": "file.min.js",
  610. "lineno": 1,
  611. "colno": 39,
  612. },
  613. # NOTE: Intentionally source is not retrieved from this HTML file
  614. {
  615. "function": 'function: "HTMLDocument.<anonymous>"',
  616. "abs_path": "http//example.com/index.html",
  617. "filename": "index.html",
  618. "lineno": 283,
  619. "colno": 17,
  620. "in_app": False,
  621. },
  622. ]
  623. },
  624. }
  625. ]
  626. },
  627. }
  628. with override_options({"symbolicator.sourcemaps-processing-ab-test": 1.0}):
  629. event = self.post_and_retrieve_event(data)
  630. assert event.data["errors"] == [
  631. {"type": "js_no_source", "url": "http//example.com/index.html"}
  632. ]
  633. exception = event.interfaces["exception"]
  634. frame_list = exception.values[0].stacktrace.frames
  635. frame = frame_list[0]
  636. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  637. expected = "\treturn a + b; // fôo"
  638. assert frame.context_line == expected
  639. assert frame.post_context == ["}", ""]
  640. raw_frame_list = exception.values[0].raw_stacktrace.frames
  641. raw_frame = raw_frame_list[0]
  642. assert not raw_frame.pre_context
  643. assert (
  644. raw_frame.context_line
  645. == 'function add(a,b){"use strict";return a+b}function multiply(a,b){"use strict";return a*b}function '
  646. 'divide(a,b){"use strict";try{return multip {snip}'
  647. )
  648. assert raw_frame.post_context == ["//@ sourceMappingURL=file.sourcemap.js", ""]
  649. assert raw_frame.lineno == 1
  650. # Since we couldn't expand source for the 2nd frame, both
  651. # its raw and original form should be identical
  652. assert raw_frame_list[1] == frame_list[1]
  653. @requires_symbolicator
  654. @pytest.mark.symbolicator
  655. def test_sourcemap_embedded_source_expansion(self, process_with_symbolicator):
  656. self.project.update_option("sentry:scrape_javascript", False)
  657. release = Release.objects.create(
  658. organization_id=self.project.organization_id, version="abc"
  659. )
  660. release.add_project(self.project)
  661. for file in ["embedded.js", "embedded.js.map"]:
  662. with open(get_fixture_path(file), "rb") as f:
  663. f1 = File.objects.create(
  664. name=file,
  665. type="release.file",
  666. headers={},
  667. )
  668. f1.putfile(f)
  669. ReleaseFile.objects.create(
  670. name=f"http://example.com/{f1.name}",
  671. release_id=release.id,
  672. organization_id=self.project.organization_id,
  673. file=f1,
  674. )
  675. data = {
  676. "timestamp": self.min_ago,
  677. "message": "hello",
  678. "platform": "javascript",
  679. "release": "abc",
  680. "exception": {
  681. "values": [
  682. {
  683. "type": "Error",
  684. "stacktrace": {
  685. "frames": [
  686. {
  687. "abs_path": "http://example.com/embedded.js",
  688. "filename": "file.min.js",
  689. "lineno": 1,
  690. "colno": 39,
  691. },
  692. # NOTE: Intentionally source is not retrieved from this HTML file
  693. {
  694. "function": 'function: "HTMLDocument.<anonymous>"',
  695. "abs_path": "http//example.com/index.html",
  696. "filename": "index.html",
  697. "lineno": 283,
  698. "colno": 17,
  699. "in_app": False,
  700. },
  701. ]
  702. },
  703. }
  704. ]
  705. },
  706. }
  707. event = self.post_and_retrieve_event(data)
  708. assert event.data["errors"] == [
  709. {"type": "js_no_source", "url": "http//example.com/index.html"}
  710. ]
  711. exception = event.interfaces["exception"]
  712. frame_list = exception.values[0].stacktrace.frames
  713. frame = frame_list[0]
  714. if process_with_symbolicator:
  715. assert frame.data["resolved_with"] == "release-old"
  716. assert frame.data["symbolicated"]
  717. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  718. expected = "\treturn a + b; // fôo"
  719. assert frame.context_line == expected
  720. if process_with_symbolicator:
  721. assert frame.post_context == ["}"]
  722. else:
  723. assert frame.post_context == ["}", ""]
  724. @requires_symbolicator
  725. @pytest.mark.symbolicator
  726. def test_sourcemap_nofiles_source_expansion(self, process_with_symbolicator):
  727. project = self.project
  728. release = Release.objects.create(organization_id=project.organization_id, version="abc")
  729. release.add_project(project)
  730. with open(get_fixture_path("nofiles.js"), "rb") as f:
  731. f_minified = File.objects.create(
  732. name="nofiles.js", type="release.file", headers={"Content-Type": "application/json"}
  733. )
  734. f_minified.putfile(f)
  735. ReleaseFile.objects.create(
  736. name=f"~/{f_minified.name}",
  737. release_id=release.id,
  738. organization_id=project.organization_id,
  739. file=f_minified,
  740. )
  741. with open(get_fixture_path("nofiles.js.map"), "rb") as f:
  742. f_sourcemap = File.objects.create(
  743. name="nofiles.js.map",
  744. type="release.file",
  745. headers={"Content-Type": "application/json"},
  746. )
  747. f_sourcemap.putfile(f)
  748. ReleaseFile.objects.create(
  749. name=f"app:///{f_sourcemap.name}",
  750. release_id=release.id,
  751. organization_id=project.organization_id,
  752. file=f_sourcemap,
  753. )
  754. data = {
  755. "timestamp": self.min_ago,
  756. "message": "hello",
  757. "platform": "javascript",
  758. "release": "abc",
  759. "exception": {
  760. "values": [
  761. {
  762. "type": "Error",
  763. "stacktrace": {
  764. "frames": [{"abs_path": "app:///nofiles.js", "lineno": 1, "colno": 39}]
  765. },
  766. }
  767. ]
  768. },
  769. }
  770. event = self.post_and_retrieve_event(data)
  771. assert "errors" not in event.data
  772. exception = event.interfaces["exception"]
  773. frame_list = exception.values[0].stacktrace.frames
  774. assert len(frame_list) == 1
  775. frame = frame_list[0]
  776. if process_with_symbolicator:
  777. assert frame.data["resolved_with"] == "release-old"
  778. assert frame.data["symbolicated"]
  779. assert frame.abs_path == "app:///nofiles.js"
  780. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  781. assert frame.context_line == "\treturn a + b; // fôo"
  782. if process_with_symbolicator:
  783. assert frame.post_context == ["}"]
  784. else:
  785. assert frame.post_context == ["}", ""]
  786. @requires_symbolicator
  787. @pytest.mark.symbolicator
  788. def test_indexed_sourcemap_source_expansion(self, process_with_symbolicator):
  789. self.project.update_option("sentry:scrape_javascript", False)
  790. release = Release.objects.create(
  791. organization_id=self.project.organization_id, version="abc"
  792. )
  793. release.add_project(self.project)
  794. for file in ["indexed.min.js", "file1.js", "file2.js", "indexed.sourcemap.js"]:
  795. with open(get_fixture_path(file), "rb") as f:
  796. f1 = File.objects.create(
  797. name=file,
  798. type="release.file",
  799. headers={},
  800. )
  801. f1.putfile(f)
  802. ReleaseFile.objects.create(
  803. name=f"http://example.com/{f1.name}",
  804. release_id=release.id,
  805. organization_id=self.project.organization_id,
  806. file=f1,
  807. )
  808. data = {
  809. "timestamp": self.min_ago,
  810. "message": "hello",
  811. "platform": "javascript",
  812. "release": "abc",
  813. "exception": {
  814. "values": [
  815. {
  816. "type": "Error",
  817. "stacktrace": {
  818. "frames": [
  819. {
  820. "abs_path": "http://example.com/indexed.min.js",
  821. "filename": "indexed.min.js",
  822. "lineno": 1,
  823. "colno": 39,
  824. },
  825. {
  826. "abs_path": "http://example.com/indexed.min.js",
  827. "filename": "indexed.min.js",
  828. "lineno": 2,
  829. "colno": 44,
  830. },
  831. ]
  832. },
  833. }
  834. ]
  835. },
  836. }
  837. event = self.post_and_retrieve_event(data)
  838. assert "errors" not in event.data
  839. exception = event.interfaces["exception"]
  840. frame_list = exception.values[0].stacktrace.frames
  841. frame = frame_list[0]
  842. if process_with_symbolicator:
  843. assert frame.data["resolved_with"] == "release-old"
  844. assert frame.data["symbolicated"]
  845. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  846. expected = "\treturn a + b; // fôo"
  847. assert frame.context_line == expected
  848. if process_with_symbolicator:
  849. assert frame.post_context == ["}"]
  850. else:
  851. assert frame.post_context == ["}", ""]
  852. raw_frame_list = exception.values[0].raw_stacktrace.frames
  853. raw_frame = raw_frame_list[0]
  854. assert not raw_frame.pre_context
  855. assert raw_frame.context_line == 'function add(a,b){"use strict";return a+b}'
  856. if process_with_symbolicator:
  857. assert raw_frame.post_context == [
  858. 'function multiply(a,b){"use strict";return a*b}function divide(a,b){"use strict";try{return multiply('
  859. "add(a,b),a,b)/c}catch(e){Raven.captureE {snip}",
  860. "//# sourceMappingURL=indexed.sourcemap.js",
  861. ]
  862. else:
  863. assert raw_frame.post_context == [
  864. 'function multiply(a,b){"use strict";return a*b}function divide(a,b){"use strict";try{return multiply('
  865. "add(a,b),a,b)/c}catch(e){Raven.captureE {snip}",
  866. "//# sourceMappingURL=indexed.sourcemap.js",
  867. "",
  868. ]
  869. assert raw_frame.lineno == 1
  870. frame = frame_list[1]
  871. if process_with_symbolicator:
  872. assert frame.data["resolved_with"] == "release-old"
  873. assert frame.data["symbolicated"]
  874. assert frame.pre_context == ["function multiply(a, b) {", '\t"use strict";']
  875. assert frame.context_line == "\treturn a * b;"
  876. assert frame.post_context == [
  877. "}",
  878. "function divide(a, b) {",
  879. '\t"use strict";',
  880. "\ttry {",
  881. "\t\treturn multiply(add(a, b), a, b) / c;",
  882. ]
  883. raw_frame = raw_frame_list[1]
  884. assert raw_frame.pre_context == ['function add(a,b){"use strict";return a+b}']
  885. assert (
  886. raw_frame.context_line
  887. == 'function multiply(a,b){"use strict";return a*b}function divide(a,b){"use strict";try{return multiply('
  888. "add(a,b),a,b)/c}catch(e){Raven.captureE {snip}"
  889. )
  890. if process_with_symbolicator:
  891. assert raw_frame.post_context == ["//# sourceMappingURL=indexed.sourcemap.js"]
  892. else:
  893. assert raw_frame.post_context == ["//# sourceMappingURL=indexed.sourcemap.js", ""]
  894. assert raw_frame.lineno == 2
  895. @requires_symbolicator
  896. @pytest.mark.symbolicator
  897. def test_expansion_via_debug(self, process_with_symbolicator):
  898. project = self.project
  899. release = Release.objects.create(organization_id=project.organization_id, version="abc")
  900. release.add_project(project)
  901. # file.min.js
  902. # ------------
  903. with open(get_fixture_path("file.min.js"), "rb") as f:
  904. f_minified = File.objects.create(
  905. name="file.min.js",
  906. type="release.file",
  907. headers={"Content-Type": "application/json"},
  908. )
  909. f_minified.putfile(f)
  910. # Intentionally omit hostname - use alternate artifact path lookup instead
  911. # /file1.js vs http://example.com/file1.js
  912. ReleaseFile.objects.create(
  913. name=f"~/{f_minified.name}?foo=bar",
  914. release_id=release.id,
  915. organization_id=project.organization_id,
  916. file=f_minified,
  917. )
  918. # file1.js
  919. # ---------
  920. with open(get_fixture_path("file1.js"), "rb") as f:
  921. f1 = File.objects.create(
  922. name="file1.js", type="release.file", headers={"Content-Type": "application/json"}
  923. )
  924. f1.putfile(f)
  925. ReleaseFile.objects.create(
  926. name=f"http://example.com/{f1.name}",
  927. release_id=release.id,
  928. organization_id=project.organization_id,
  929. file=f1,
  930. )
  931. # file2.js
  932. # ----------
  933. with open(get_fixture_path("file2.js"), "rb") as f:
  934. f2 = File.objects.create(
  935. name="file2.js", type="release.file", headers={"Content-Type": "application/json"}
  936. )
  937. f2.putfile(f)
  938. ReleaseFile.objects.create(
  939. name=f"http://example.com/{f2.name}",
  940. release_id=release.id,
  941. organization_id=project.organization_id,
  942. file=f2,
  943. )
  944. # To verify that the full url has priority over the relative url,
  945. # we will also add a second ReleaseFile alias for file2.js (f3) w/o
  946. # hostname that points to an empty file. If the processor chooses
  947. # this empty file over the correct file2.js, it will not locate
  948. # context for the 2nd frame.
  949. with open(get_fixture_path("empty.js"), "rb") as f:
  950. f2_empty = File.objects.create(
  951. name="empty.js", type="release.file", headers={"Content-Type": "application/json"}
  952. )
  953. f2_empty.putfile(f)
  954. ReleaseFile.objects.create(
  955. name=f"~/{f2.name}", # intentionally using f2.name ("file2.js")
  956. release_id=release.id,
  957. organization_id=project.organization_id,
  958. file=f2_empty,
  959. )
  960. # sourcemap
  961. # ----------
  962. with open(get_fixture_path("file.sourcemap.js"), "rb") as f:
  963. f_sourcemap = File.objects.create(
  964. name="file.sourcemap.js",
  965. type="release.file",
  966. headers={"Content-Type": "application/json"},
  967. )
  968. f_sourcemap.putfile(f)
  969. ReleaseFile.objects.create(
  970. name=f"http://example.com/{f_sourcemap.name}",
  971. release_id=release.id,
  972. organization_id=project.organization_id,
  973. file=f_sourcemap,
  974. )
  975. data = {
  976. "timestamp": self.min_ago,
  977. "message": "hello",
  978. "platform": "javascript",
  979. "release": "abc",
  980. "exception": {
  981. "values": [
  982. {
  983. "type": "Error",
  984. "stacktrace": {
  985. "frames": [
  986. {
  987. "abs_path": "http://example.com/file.min.js?foo=bar",
  988. "filename": "file.min.js",
  989. "lineno": 1,
  990. "colno": 39,
  991. },
  992. {
  993. "abs_path": "http://example.com/file.min.js?foo=bar",
  994. "filename": "file.min.js",
  995. "lineno": 1,
  996. "colno": 79,
  997. },
  998. ]
  999. },
  1000. }
  1001. ]
  1002. },
  1003. }
  1004. event = self.post_and_retrieve_event(data)
  1005. assert "errors" not in event.data
  1006. exception = event.interfaces["exception"]
  1007. frame_list = exception.values[0].stacktrace.frames
  1008. frame = frame_list[0]
  1009. if process_with_symbolicator:
  1010. assert frame.data["resolved_with"] == "release-old"
  1011. assert frame.data["symbolicated"]
  1012. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  1013. assert frame.context_line == "\treturn a + b; // fôo"
  1014. if process_with_symbolicator:
  1015. assert frame.post_context == ["}"]
  1016. else:
  1017. assert frame.post_context == ["}", ""]
  1018. frame = frame_list[1]
  1019. if process_with_symbolicator:
  1020. assert frame.data["resolved_with"] == "release-old"
  1021. assert frame.data["symbolicated"]
  1022. assert frame.pre_context == ["function multiply(a, b) {", '\t"use strict";']
  1023. assert frame.context_line == "\treturn a * b;"
  1024. assert frame.post_context == [
  1025. "}",
  1026. "function divide(a, b) {",
  1027. '\t"use strict";',
  1028. "\ttry {",
  1029. "\t\treturn multiply(add(a, b), a, b) / c;",
  1030. ]
  1031. @requires_symbolicator
  1032. @pytest.mark.symbolicator
  1033. def test_expansion_via_distribution_release_artifacts(self, process_with_symbolicator):
  1034. project = self.project
  1035. release = Release.objects.create(organization_id=project.organization_id, version="abc")
  1036. release.add_project(project)
  1037. dist = release.add_dist("foo")
  1038. # file.min.js
  1039. # ------------
  1040. with open(get_fixture_path("file.min.js"), "rb") as f:
  1041. f_minified = File.objects.create(
  1042. name="file.min.js",
  1043. type="release.file",
  1044. headers={"Content-Type": "application/json"},
  1045. )
  1046. f_minified.putfile(f)
  1047. # Intentionally omit hostname - use alternate artifact path lookup instead
  1048. # /file1.js vs http://example.com/file1.js
  1049. ReleaseFile.objects.create(
  1050. name=f"~/{f_minified.name}?foo=bar",
  1051. release_id=release.id,
  1052. dist_id=dist.id,
  1053. organization_id=project.organization_id,
  1054. file=f_minified,
  1055. )
  1056. # file1.js
  1057. # ---------
  1058. with open(get_fixture_path("file1.js"), "rb") as f:
  1059. f1 = File.objects.create(
  1060. name="file1.js",
  1061. type="release.file",
  1062. headers={"Content-Type": "application/json"},
  1063. )
  1064. f1.putfile(f)
  1065. ReleaseFile.objects.create(
  1066. name=f"http://example.com/{f1.name}",
  1067. release_id=release.id,
  1068. dist_id=dist.id,
  1069. organization_id=project.organization_id,
  1070. file=f1,
  1071. )
  1072. # file2.js
  1073. # ----------
  1074. with open(get_fixture_path("file2.js"), "rb") as f:
  1075. f2 = File.objects.create(
  1076. name="file2.js",
  1077. type="release.file",
  1078. headers={"Content-Type": "application/json"},
  1079. )
  1080. f2.putfile(f)
  1081. ReleaseFile.objects.create(
  1082. name=f"http://example.com/{f2.name}",
  1083. release_id=release.id,
  1084. dist_id=dist.id,
  1085. organization_id=project.organization_id,
  1086. file=f2,
  1087. )
  1088. # To verify that the full url has priority over the relative url,
  1089. # we will also add a second ReleaseFile alias for file2.js (f3) w/o
  1090. # hostname that points to an empty file. If the processor chooses
  1091. # this empty file over the correct file2.js, it will not locate
  1092. # context for the 2nd frame.
  1093. with open(get_fixture_path("empty.js"), "rb") as f:
  1094. f2_empty = File.objects.create(
  1095. name="empty.js",
  1096. type="release.file",
  1097. headers={"Content-Type": "application/json"},
  1098. )
  1099. f2_empty.putfile(f)
  1100. ReleaseFile.objects.create(
  1101. name=f"~/{f2.name}", # intentionally using f2.name ("file2.js")
  1102. release_id=release.id,
  1103. dist_id=dist.id,
  1104. organization_id=project.organization_id,
  1105. file=f2_empty,
  1106. )
  1107. # sourcemap
  1108. # ----------
  1109. with open(get_fixture_path("file.sourcemap.js"), "rb") as f:
  1110. f_sourcemap = File.objects.create(
  1111. name="file.sourcemap.js",
  1112. type="release.file",
  1113. headers={"Content-Type": "application/json"},
  1114. )
  1115. f_sourcemap.putfile(f)
  1116. ReleaseFile.objects.create(
  1117. name=f"http://example.com/{f_sourcemap.name}",
  1118. release_id=release.id,
  1119. dist_id=dist.id,
  1120. organization_id=project.organization_id,
  1121. file=f_sourcemap,
  1122. )
  1123. data = {
  1124. "timestamp": self.min_ago,
  1125. "message": "hello",
  1126. "platform": "javascript",
  1127. "release": "abc",
  1128. "dist": "foo",
  1129. "exception": {
  1130. "values": [
  1131. {
  1132. "type": "Error",
  1133. "stacktrace": {
  1134. "frames": [
  1135. {
  1136. "abs_path": "http://example.com/file.min.js?foo=bar",
  1137. "filename": "file.min.js",
  1138. "lineno": 1,
  1139. "colno": 39,
  1140. },
  1141. {
  1142. "abs_path": "http://example.com/file.min.js?foo=bar",
  1143. "filename": "file.min.js",
  1144. "lineno": 1,
  1145. "colno": 79,
  1146. },
  1147. ]
  1148. },
  1149. }
  1150. ]
  1151. },
  1152. }
  1153. event = self.post_and_retrieve_event(data)
  1154. assert "errors" not in event.data
  1155. exception = event.interfaces["exception"]
  1156. frame_list = exception.values[0].stacktrace.frames
  1157. frame = frame_list[0]
  1158. if process_with_symbolicator:
  1159. assert frame.data["resolved_with"] == "release-old"
  1160. assert frame.data["symbolicated"]
  1161. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  1162. assert frame.context_line == "\treturn a + b; // fôo"
  1163. if process_with_symbolicator:
  1164. assert frame.post_context == ["}"]
  1165. else:
  1166. assert frame.post_context == ["}", ""]
  1167. frame = frame_list[1]
  1168. if process_with_symbolicator:
  1169. assert frame.data["resolved_with"] == "release-old"
  1170. assert frame.data["symbolicated"]
  1171. assert frame.pre_context == ["function multiply(a, b) {", '\t"use strict";']
  1172. assert frame.context_line == "\treturn a * b;"
  1173. assert frame.post_context == [
  1174. "}",
  1175. "function divide(a, b) {",
  1176. '\t"use strict";',
  1177. "\ttry {",
  1178. "\t\treturn multiply(add(a, b), a, b) / c;",
  1179. ]
  1180. @responses.activate
  1181. def test_sourcemap_expansion_with_missing_source(self):
  1182. """
  1183. Tests a successful sourcemap expansion that points to source files
  1184. that are not found.
  1185. """
  1186. responses.add(
  1187. responses.GET,
  1188. "http://example.com/file.min.js",
  1189. body=load_fixture("file.min.js"),
  1190. content_type="application/javascript; charset=utf-8",
  1191. )
  1192. responses.add(
  1193. responses.GET,
  1194. "http://example.com/file.sourcemap.js",
  1195. body=load_fixture("file.sourcemap.js"),
  1196. content_type="application/json; charset=utf-8",
  1197. )
  1198. responses.add(responses.GET, "http://example.com/file1.js", body="Not Found", status=404)
  1199. data = {
  1200. "timestamp": self.min_ago,
  1201. "message": "hello",
  1202. "platform": "javascript",
  1203. "exception": {
  1204. "values": [
  1205. {
  1206. "type": "Error",
  1207. "stacktrace": {
  1208. # Add two frames. We only want to see the
  1209. # error once though.
  1210. "frames": [
  1211. {
  1212. "abs_path": "http://example.com/file.min.js",
  1213. "filename": "file.min.js",
  1214. "lineno": 1,
  1215. "colno": 39,
  1216. },
  1217. {
  1218. "abs_path": "http://example.com/file.min.js",
  1219. "filename": "file.min.js",
  1220. "lineno": 1,
  1221. "colno": 39,
  1222. },
  1223. ]
  1224. },
  1225. }
  1226. ]
  1227. },
  1228. }
  1229. event = self.post_and_retrieve_event(data)
  1230. assert event.data["errors"] == [
  1231. {"url": "http://example.com/file1.js", "type": "fetch_invalid_http_code", "value": 404}
  1232. ]
  1233. exception = event.interfaces["exception"]
  1234. frame_list = exception.values[0].stacktrace.frames
  1235. frame = frame_list[0]
  1236. # no context information ...
  1237. assert not frame.pre_context
  1238. assert not frame.context_line
  1239. assert not frame.post_context
  1240. # ... but line, column numbers are still correctly mapped
  1241. assert frame.lineno == 3
  1242. assert frame.colno == 9
  1243. @responses.activate
  1244. def test_failed_sourcemap_expansion(self):
  1245. """
  1246. Tests attempting to parse an indexed source map where each section has a "url"
  1247. property - this is unsupported and should fail.
  1248. """
  1249. responses.add(
  1250. responses.GET,
  1251. "http://example.com/unsupported.min.js",
  1252. body=load_fixture("unsupported.min.js"),
  1253. content_type="application/javascript; charset=utf-8",
  1254. )
  1255. responses.add(
  1256. responses.GET,
  1257. "http://example.com/unsupported.sourcemap.js",
  1258. body=load_fixture("unsupported.sourcemap.js"),
  1259. content_type="application/json; charset=utf-8",
  1260. )
  1261. data = {
  1262. "timestamp": self.min_ago,
  1263. "message": "hello",
  1264. "platform": "javascript",
  1265. "exception": {
  1266. "values": [
  1267. {
  1268. "type": "Error",
  1269. "stacktrace": {
  1270. "frames": [
  1271. {
  1272. "abs_path": "http://example.com/unsupported.min.js",
  1273. "filename": "indexed.min.js",
  1274. "lineno": 1,
  1275. "colno": 39,
  1276. }
  1277. ]
  1278. },
  1279. }
  1280. ]
  1281. },
  1282. }
  1283. event = self.post_and_retrieve_event(data)
  1284. assert event.data["errors"] == [
  1285. {"url": "http://example.com/unsupported.sourcemap.js", "type": "js_invalid_source"}
  1286. ]
  1287. def test_failed_sourcemap_expansion_data_url(self):
  1288. data = {
  1289. "timestamp": self.min_ago,
  1290. "message": "hello",
  1291. "platform": "javascript",
  1292. "exception": {
  1293. "values": [
  1294. {
  1295. "type": "Error",
  1296. "stacktrace": {
  1297. "frames": [
  1298. {
  1299. "abs_path": "data:application/javascript,base46,asfasf",
  1300. "filename": "indexed.min.js",
  1301. "lineno": 1,
  1302. "colno": 39,
  1303. }
  1304. ]
  1305. },
  1306. }
  1307. ]
  1308. },
  1309. }
  1310. event = self.post_and_retrieve_event(data)
  1311. assert event.data["errors"] == [{"url": "<data url>", "type": "js_no_source"}]
  1312. @responses.activate
  1313. def test_failed_sourcemap_expansion_missing_location_entirely(self):
  1314. responses.add(
  1315. responses.GET,
  1316. "http://example.com/indexed.min.js",
  1317. body="//# sourceMappingURL=indexed.sourcemap.js",
  1318. )
  1319. responses.add(responses.GET, "http://example.com/indexed.sourcemap.js", body="{}")
  1320. data = {
  1321. "timestamp": self.min_ago,
  1322. "message": "hello",
  1323. "platform": "javascript",
  1324. "exception": {
  1325. "values": [
  1326. {
  1327. "type": "Error",
  1328. "stacktrace": {
  1329. "frames": [
  1330. {
  1331. "abs_path": "http://example.com/indexed.min.js",
  1332. "filename": "indexed.min.js",
  1333. "lineno": 1,
  1334. "colno": 1,
  1335. },
  1336. {
  1337. "abs_path": "http://example.com/indexed.min.js",
  1338. "filename": "indexed.min.js",
  1339. },
  1340. ]
  1341. },
  1342. }
  1343. ]
  1344. },
  1345. }
  1346. event = self.post_and_retrieve_event(data)
  1347. assert "errors" not in event.data
  1348. @responses.activate
  1349. def test_html_response_for_js(self):
  1350. responses.add(
  1351. responses.GET,
  1352. "http://example.com/invalid_file1.js",
  1353. body=" <!DOCTYPE html><html><head></head><body></body></html>",
  1354. )
  1355. responses.add(
  1356. responses.GET,
  1357. "http://example.com/invalid_file2.js",
  1358. body="<!doctype html><html><head></head><body></body></html>",
  1359. )
  1360. responses.add(
  1361. responses.GET,
  1362. "http://example.com/valid_file.html",
  1363. body=(
  1364. "<!doctype html><html><head></head><body><script>/*legit case*/</script></body></html>"
  1365. ),
  1366. )
  1367. data = {
  1368. "timestamp": self.min_ago,
  1369. "message": "hello",
  1370. "platform": "javascript",
  1371. "exception": {
  1372. "values": [
  1373. {
  1374. "type": "Error",
  1375. "stacktrace": {
  1376. "frames": [
  1377. {
  1378. "abs_path": "http://example.com/invalid_file1.js",
  1379. "filename": "invalid_file1.js",
  1380. "lineno": 1,
  1381. "colno": 39,
  1382. },
  1383. {
  1384. "abs_path": "http://example.com/invalid_file2.js",
  1385. "filename": "invalid_file2.js",
  1386. "lineno": 1,
  1387. "colno": 39,
  1388. },
  1389. {
  1390. "abs_path": "http://example.com/valid_file.html",
  1391. "filename": "valid_file.html",
  1392. "lineno": 1,
  1393. "colno": 1,
  1394. },
  1395. ]
  1396. },
  1397. }
  1398. ]
  1399. },
  1400. }
  1401. event = self.post_and_retrieve_event(data)
  1402. assert event.data["errors"] == [
  1403. {"url": "http://example.com/invalid_file1.js", "type": "js_invalid_content"},
  1404. {"url": "http://example.com/invalid_file2.js", "type": "js_invalid_content"},
  1405. ]
  1406. def _test_expansion_via_release_archive(
  1407. self, link_sourcemaps: bool, process_with_symbolicator: bool
  1408. ):
  1409. project = self.project
  1410. release = Release.objects.create(organization_id=project.organization_id, version="abc")
  1411. release.add_project(project)
  1412. manifest = {
  1413. "org": self.organization.slug,
  1414. "release": release.version,
  1415. "files": {
  1416. "files/_/_/file.min.js": {
  1417. "url": "http://example.com/file.min.js",
  1418. "type": "minified_source",
  1419. },
  1420. "files/_/_/file1.js": {
  1421. "url": "http://example.com/file1.js",
  1422. "type": "source",
  1423. },
  1424. "files/_/_/file2.js": {
  1425. "url": "http://example.com/file2.js",
  1426. "type": "source",
  1427. },
  1428. "files/_/_/file.sourcemap.js": {
  1429. "url": "http://example.com/file.sourcemap.js",
  1430. "type": "source_map",
  1431. },
  1432. },
  1433. }
  1434. compressed = BytesIO(b"SYSB")
  1435. with zipfile.ZipFile(compressed, "a") as zip_file:
  1436. for rel_path, entry in manifest["files"].items():
  1437. name = os.path.basename(rel_path)
  1438. content = load_fixture(name)
  1439. if name == "file.min.js" and not link_sourcemaps:
  1440. # Remove link to source map, add to header instead
  1441. content = content.replace(b"//@ sourceMappingURL=file.sourcemap.js", b"")
  1442. entry["headers"] = {"Sourcemap": "file.sourcemap.js"}
  1443. zip_file.writestr(rel_path, content)
  1444. zip_file.writestr("manifest.json", json.dumps(manifest))
  1445. compressed.seek(0)
  1446. file = File.objects.create(name="doesnt_matter", type="release.bundle")
  1447. file.putfile(compressed)
  1448. update_artifact_index(release, None, file)
  1449. data = {
  1450. "timestamp": self.min_ago,
  1451. "message": "hello",
  1452. "platform": "javascript",
  1453. "release": "abc",
  1454. "exception": {
  1455. "values": [
  1456. {
  1457. "type": "Error",
  1458. "stacktrace": {
  1459. "frames": [
  1460. {
  1461. "abs_path": "http://example.com/file.min.js",
  1462. "filename": "file.min.js",
  1463. "lineno": 1,
  1464. "colno": 39,
  1465. },
  1466. {
  1467. "abs_path": "http://example.com/file.min.js",
  1468. "filename": "file.min.js",
  1469. "lineno": 1,
  1470. "colno": 79,
  1471. },
  1472. ]
  1473. },
  1474. }
  1475. ]
  1476. },
  1477. }
  1478. event = self.post_and_retrieve_event(data)
  1479. assert "errors" not in event.data
  1480. exception = event.interfaces["exception"]
  1481. frame_list = exception.values[0].stacktrace.frames
  1482. frame = frame_list[0]
  1483. if process_with_symbolicator:
  1484. assert frame.data["resolved_with"] == "release-old"
  1485. assert frame.data["symbolicated"]
  1486. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  1487. assert frame.context_line == "\treturn a + b; // fôo"
  1488. if process_with_symbolicator:
  1489. assert frame.post_context == ["}"]
  1490. else:
  1491. assert frame.post_context == ["}", ""]
  1492. frame = frame_list[1]
  1493. if process_with_symbolicator:
  1494. assert frame.data["resolved_with"] == "release-old"
  1495. assert frame.data["symbolicated"]
  1496. assert frame.pre_context == ["function multiply(a, b) {", '\t"use strict";']
  1497. assert frame.context_line == "\treturn a * b;"
  1498. assert frame.post_context == [
  1499. "}",
  1500. "function divide(a, b) {",
  1501. '\t"use strict";',
  1502. "\ttry {",
  1503. "\t\treturn multiply(add(a, b), a, b) / c;",
  1504. ]
  1505. @requires_symbolicator
  1506. @pytest.mark.symbolicator
  1507. def test_expansion_via_release_archive(self, process_with_symbolicator):
  1508. self._test_expansion_via_release_archive(
  1509. link_sourcemaps=True, process_with_symbolicator=process_with_symbolicator
  1510. )
  1511. @requires_symbolicator
  1512. @pytest.mark.symbolicator
  1513. def test_expansion_via_release_archive_no_sourcemap_link(self, process_with_symbolicator):
  1514. self._test_expansion_via_release_archive(
  1515. link_sourcemaps=False, process_with_symbolicator=process_with_symbolicator
  1516. )
  1517. @requires_symbolicator
  1518. @pytest.mark.symbolicator
  1519. def test_node_processing(self, process_with_symbolicator):
  1520. project = self.project
  1521. release = Release.objects.create(
  1522. organization_id=project.organization_id, version="nodeabc123"
  1523. )
  1524. release.add_project(project)
  1525. with open(get_fixture_path("dist.bundle.js"), "rb") as f:
  1526. f_minified = File.objects.create(
  1527. name="dist.bundle.js",
  1528. type="release.file",
  1529. headers={"Content-Type": "application/javascript"},
  1530. )
  1531. f_minified.putfile(f)
  1532. ReleaseFile.objects.create(
  1533. name=f"~/{f_minified.name}",
  1534. release_id=release.id,
  1535. organization_id=project.organization_id,
  1536. file=f_minified,
  1537. )
  1538. with open(get_fixture_path("dist.bundle.js.map"), "rb") as f:
  1539. f_sourcemap = File.objects.create(
  1540. name="dist.bundle.js.map",
  1541. type="release.file",
  1542. headers={"Content-Type": "application/javascript"},
  1543. )
  1544. f_sourcemap.putfile(f)
  1545. ReleaseFile.objects.create(
  1546. name=f"~/{f_sourcemap.name}",
  1547. release_id=release.id,
  1548. organization_id=project.organization_id,
  1549. file=f_sourcemap,
  1550. )
  1551. data = {
  1552. "timestamp": self.min_ago,
  1553. "message": "hello",
  1554. "platform": "node",
  1555. "release": "nodeabc123",
  1556. "exception": {
  1557. "values": [
  1558. {
  1559. "type": "Error",
  1560. "stacktrace": {
  1561. "frames": [
  1562. {
  1563. "filename": "app:///dist.bundle.js",
  1564. "function": "bar",
  1565. "lineno": 9,
  1566. "colno": 2321,
  1567. },
  1568. {
  1569. "filename": "app:///dist.bundle.js",
  1570. "function": "foo",
  1571. "lineno": 3,
  1572. "colno": 2308,
  1573. },
  1574. {
  1575. "filename": "app:///dist.bundle.js",
  1576. "function": "App",
  1577. "lineno": 3,
  1578. "colno": 1011,
  1579. },
  1580. {
  1581. "filename": "app:///dist.bundle.js",
  1582. "function": "Object.<anonymous>",
  1583. "lineno": 1,
  1584. "colno": 1014,
  1585. },
  1586. {
  1587. "filename": "app:///dist.bundle.js",
  1588. "function": "__webpack_require__",
  1589. "lineno": 20,
  1590. "colno": 30,
  1591. },
  1592. {
  1593. "filename": "app:///dist.bundle.js",
  1594. "function": "<unknown>",
  1595. "lineno": 18,
  1596. "colno": 63,
  1597. },
  1598. ]
  1599. },
  1600. }
  1601. ]
  1602. },
  1603. }
  1604. event = self.post_and_retrieve_event(data)
  1605. exception = event.interfaces["exception"]
  1606. frame_list = exception.values[0].stacktrace.frames
  1607. assert len(frame_list) == 6
  1608. def assert_abs_path(abs_path):
  1609. # This makes the test assertion forward compatible with percent-encoded URLs
  1610. # See https://github.com/getsentry/symbolicator/pull/1137
  1611. assert abs_path in (
  1612. "webpack:///webpack/bootstrap d9a5a31d9276b73873d3",
  1613. "webpack:///webpack/bootstrap%20d9a5a31d9276b73873d3",
  1614. )
  1615. assert_abs_path(frame_list[0].abs_path)
  1616. assert frame_list[0].function == "bar"
  1617. assert frame_list[0].lineno == 8
  1618. assert_abs_path(frame_list[1].abs_path)
  1619. assert frame_list[1].function == "foo"
  1620. assert frame_list[1].lineno == 2
  1621. assert_abs_path(frame_list[2].abs_path)
  1622. assert frame_list[2].function == "App"
  1623. assert frame_list[2].lineno == 2
  1624. assert_abs_path(frame_list[3].abs_path)
  1625. assert frame_list[3].function == "Object.<anonymous>"
  1626. assert frame_list[3].lineno == 1
  1627. assert_abs_path(frame_list[4].abs_path)
  1628. assert frame_list[4].function == "__webpack_require__"
  1629. assert frame_list[4].lineno == 19
  1630. assert_abs_path(frame_list[5].abs_path)
  1631. assert frame_list[5].function == "<unknown>"
  1632. assert frame_list[5].lineno == 16
  1633. @responses.activate
  1634. def test_no_fetch_from_http(self):
  1635. responses.add(
  1636. responses.GET,
  1637. "http://example.com/node_app.min.js",
  1638. body=load_fixture("node_app.min.js"),
  1639. content_type="application/javascript; charset=utf-8",
  1640. )
  1641. responses.add(
  1642. responses.GET,
  1643. "http://example.com/node_app.min.js.map",
  1644. body=load_fixture("node_app.min.js.map"),
  1645. content_type="application/javascript; charset=utf-8",
  1646. )
  1647. data = {
  1648. "timestamp": self.min_ago,
  1649. "message": "hello",
  1650. "platform": "node",
  1651. "exception": {
  1652. "values": [
  1653. {
  1654. "type": "Error",
  1655. "stacktrace": {
  1656. "frames": [
  1657. {
  1658. "abs_path": "node_bootstrap.js",
  1659. "filename": "node_bootstrap.js",
  1660. "lineno": 1,
  1661. "colno": 38,
  1662. },
  1663. {
  1664. "abs_path": "timers.js",
  1665. "filename": "timers.js",
  1666. "lineno": 1,
  1667. "colno": 39,
  1668. },
  1669. {
  1670. "abs_path": "webpack:///internal",
  1671. "filename": "internal",
  1672. "lineno": 1,
  1673. "colno": 43,
  1674. },
  1675. {
  1676. "abs_path": "webpack:///~/some_dep/file.js",
  1677. "filename": "file.js",
  1678. "lineno": 1,
  1679. "colno": 41,
  1680. },
  1681. {
  1682. "abs_path": "webpack:///./node_modules/file.js",
  1683. "filename": "file.js",
  1684. "lineno": 1,
  1685. "colno": 42,
  1686. },
  1687. {
  1688. "abs_path": "http://example.com/node_app.min.js",
  1689. "filename": "node_app.min.js",
  1690. "lineno": 1,
  1691. "colno": 40,
  1692. },
  1693. ]
  1694. },
  1695. }
  1696. ]
  1697. },
  1698. }
  1699. event = self.post_and_retrieve_event(data)
  1700. exception = event.interfaces["exception"]
  1701. frame_list = exception.values[0].stacktrace.frames
  1702. # This one should not process, so this one should be none.
  1703. assert exception.values[0].raw_stacktrace is None
  1704. # None of the in app should update
  1705. for x in range(6):
  1706. assert not frame_list[x].in_app
  1707. @responses.activate
  1708. def test_html_file_with_query_param_ending_with_js_extension(self):
  1709. responses.add(
  1710. responses.GET,
  1711. "http://example.com/file.html",
  1712. body=(
  1713. "<!doctype html><html><head></head><body><script>/*legit case*/</script></body></html>"
  1714. ),
  1715. )
  1716. data = {
  1717. "timestamp": self.min_ago,
  1718. "message": "hello",
  1719. "platform": "javascript",
  1720. "exception": {
  1721. "values": [
  1722. {
  1723. "type": "Error",
  1724. "stacktrace": {
  1725. "frames": [
  1726. {
  1727. "abs_path": "http://example.com/file.html?sw=iddqd1337.js",
  1728. "filename": "file.html",
  1729. "lineno": 1,
  1730. "colno": 1,
  1731. },
  1732. ]
  1733. },
  1734. }
  1735. ]
  1736. },
  1737. }
  1738. event = self.post_and_retrieve_event(data)
  1739. assert "errors" not in event.data
  1740. def test_expansion_with_allow_scraping_false(self):
  1741. project = self.project
  1742. project.organization.update_option("sentry:scrape_javascript", False)
  1743. data = {
  1744. "timestamp": self.min_ago,
  1745. "message": "hello",
  1746. "platform": "javascript",
  1747. "release": "1.0",
  1748. "exception": {
  1749. "values": [
  1750. {
  1751. "type": "Error",
  1752. "stacktrace": {
  1753. "frames": [
  1754. {
  1755. "abs_path": "http://example.com/file.min.js",
  1756. "filename": "file.min.js",
  1757. "lineno": 1,
  1758. "colno": 39,
  1759. },
  1760. ]
  1761. },
  1762. }
  1763. ]
  1764. },
  1765. }
  1766. event = self.post_and_retrieve_event(data)
  1767. assert len(event.data["errors"]) == 1
  1768. assert event.data["errors"][0] == {
  1769. "type": "js_scraping_disabled",
  1770. "url": "http://example.com/file.min.js",
  1771. }
  1772. @requires_symbolicator
  1773. @pytest.mark.symbolicator
  1774. def test_expansion_with_debug_id(self, process_with_symbolicator):
  1775. project = self.project
  1776. release = Release.objects.create(organization_id=project.organization_id, version="abc")
  1777. release.add_project(project)
  1778. debug_id = "c941d872-af1f-4f0c-a7ff-ad3d295fe153"
  1779. compressed = BytesIO(b"SYSB")
  1780. with zipfile.ZipFile(compressed, "a") as zip_file:
  1781. zip_file.writestr("files/_/_/file.min.js", load_fixture("file.min.js"))
  1782. zip_file.writestr("files/_/_/file1.js", load_fixture("file1.js"))
  1783. zip_file.writestr("files/_/_/file2.js", load_fixture("file2.js"))
  1784. zip_file.writestr("files/_/_/empty.js", load_fixture("empty.js"))
  1785. zip_file.writestr(
  1786. "files/_/_/file.wc.sourcemap.js", load_fixture("file.wc.sourcemap.js")
  1787. )
  1788. zip_file.writestr(
  1789. "manifest.json",
  1790. json.dumps(
  1791. {
  1792. "org": self.organization.slug,
  1793. "release": release.version,
  1794. "files": {
  1795. "files/_/_/file.min.js": {
  1796. "url": "~/file.min.js",
  1797. "type": "minified_source",
  1798. "headers": {
  1799. "content-type": "application/json",
  1800. "debug-id": debug_id,
  1801. "sourcemap": "file.sourcemap.js",
  1802. },
  1803. },
  1804. "files/_/_/file1.js": {
  1805. "url": "~/file1.js",
  1806. "type": "source",
  1807. "headers": {
  1808. "content-type": "application/json",
  1809. },
  1810. },
  1811. "files/_/_/file2.js": {
  1812. "url": "~/file2.js",
  1813. "type": "source",
  1814. "headers": {
  1815. "content-type": "application/json",
  1816. },
  1817. },
  1818. "files/_/_/empty.js": {
  1819. "url": "~/empty.js",
  1820. "type": "source",
  1821. "headers": {
  1822. "content-type": "application/json",
  1823. },
  1824. },
  1825. "files/_/_/file.wc.sourcemap.js": {
  1826. "url": "~/file.wc.sourcemap.js",
  1827. "type": "source_map",
  1828. "headers": {
  1829. "content-type": "application/json",
  1830. "debug-id": debug_id,
  1831. },
  1832. },
  1833. },
  1834. }
  1835. ),
  1836. )
  1837. compressed.seek(0)
  1838. file = File.objects.create(name="bundle.zip", type="artifact.bundle")
  1839. file.putfile(compressed)
  1840. # We want to also store the release files for this bundle, to check if they work together.
  1841. compressed.seek(0)
  1842. file_for_release = File.objects.create(name="bundle.zip", type="release.bundle")
  1843. file_for_release.putfile(compressed)
  1844. update_artifact_index(release, None, file_for_release)
  1845. artifact_bundle = ArtifactBundle.objects.create(
  1846. organization_id=self.organization.id, bundle_id=uuid4(), file=file, artifact_count=5
  1847. )
  1848. ProjectArtifactBundle.objects.create(
  1849. organization_id=self.organization.id,
  1850. project_id=self.project.id,
  1851. artifact_bundle=artifact_bundle,
  1852. )
  1853. DebugIdArtifactBundle.objects.create(
  1854. organization_id=self.organization.id,
  1855. debug_id=debug_id,
  1856. artifact_bundle=artifact_bundle,
  1857. source_file_type=SourceFileType.MINIFIED_SOURCE.value,
  1858. )
  1859. DebugIdArtifactBundle.objects.create(
  1860. organization_id=self.organization.id,
  1861. debug_id=debug_id,
  1862. artifact_bundle=artifact_bundle,
  1863. source_file_type=SourceFileType.SOURCE_MAP.value,
  1864. )
  1865. data = {
  1866. "timestamp": self.min_ago,
  1867. "message": "hello",
  1868. "platform": "javascript",
  1869. "release": "abc",
  1870. "exception": {
  1871. "values": [
  1872. {
  1873. "type": "Error",
  1874. "stacktrace": {
  1875. "frames": [
  1876. {
  1877. "abs_path": "http://example.com/file.min.js",
  1878. "filename": "file.min.js",
  1879. "lineno": 1,
  1880. "colno": 39,
  1881. },
  1882. {
  1883. "abs_path": "http://example.com/file.min.js",
  1884. "filename": "file.min.js",
  1885. "lineno": 1,
  1886. "colno": 79,
  1887. },
  1888. # We want also to test the source without minification.
  1889. {
  1890. "abs_path": "http://example.com/file1.js",
  1891. "filename": "file1.js",
  1892. "lineno": 3,
  1893. "colno": 12,
  1894. },
  1895. ]
  1896. },
  1897. }
  1898. ]
  1899. },
  1900. "debug_meta": {
  1901. "images": [
  1902. {
  1903. "type": "sourcemap",
  1904. "debug_id": debug_id,
  1905. "code_file": "http://example.com/file.min.js",
  1906. }
  1907. ]
  1908. },
  1909. }
  1910. event = self.post_and_retrieve_event(data)
  1911. assert "errors" not in event.data
  1912. exception = event.interfaces["exception"]
  1913. frame_list = exception.values[0].stacktrace.frames
  1914. frame = frame_list[0]
  1915. if process_with_symbolicator:
  1916. assert frame.data["resolved_with"] == "debug-id"
  1917. assert frame.data["symbolicated"]
  1918. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  1919. assert frame.context_line == "\treturn a + b; // fôo"
  1920. if process_with_symbolicator:
  1921. assert frame.post_context == ["}"]
  1922. else:
  1923. assert frame.post_context == ["}", ""]
  1924. frame = frame_list[1]
  1925. if process_with_symbolicator:
  1926. assert frame.data["resolved_with"] == "debug-id"
  1927. assert frame.data["symbolicated"]
  1928. assert frame.pre_context == ["function multiply(a, b) {", '\t"use strict";']
  1929. assert frame.context_line == "\treturn a * b;"
  1930. assert frame.post_context == [
  1931. "}",
  1932. "function divide(a, b) {",
  1933. '\t"use strict";',
  1934. "\ttry {",
  1935. "\t\treturn multiply(add(a, b), a, b) / c;",
  1936. ]
  1937. frame = frame_list[2]
  1938. if process_with_symbolicator:
  1939. assert "resolved_with" not in frame.data
  1940. assert "symbolicated" not in frame.data
  1941. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  1942. assert frame.context_line == "\treturn a + b; // fôo"
  1943. if process_with_symbolicator:
  1944. assert frame.post_context == ["}"]
  1945. else:
  1946. assert frame.post_context == ["}", ""]
  1947. @requires_symbolicator
  1948. @pytest.mark.symbolicator
  1949. def test_expansion_with_debug_id_and_sourcemap_without_sources_content(
  1950. self, process_with_symbolicator
  1951. ):
  1952. debug_id = "c941d872-af1f-4f0c-a7ff-ad3d295fe153"
  1953. compressed = BytesIO(b"SYSB")
  1954. with zipfile.ZipFile(compressed, "a") as zip_file:
  1955. zip_file.writestr("files/_/_/file.min.js", load_fixture("file.min.js"))
  1956. zip_file.writestr("files/_/_/file1.js", load_fixture("file1.js"))
  1957. zip_file.writestr("files/_/_/file2.js", load_fixture("file2.js"))
  1958. zip_file.writestr("files/_/_/empty.js", load_fixture("empty.js"))
  1959. zip_file.writestr("files/_/_/file.sourcemap.js", load_fixture("file.sourcemap.js"))
  1960. zip_file.writestr(
  1961. "manifest.json",
  1962. json.dumps(
  1963. {
  1964. "files": {
  1965. "files/_/_/file.min.js": {
  1966. "url": "~/file.min.js",
  1967. "type": "minified_source",
  1968. "headers": {
  1969. "content-type": "application/json",
  1970. "debug-id": debug_id,
  1971. "sourcemap": "file.sourcemap.js",
  1972. },
  1973. },
  1974. "files/_/_/file1.js": {
  1975. "url": "~/file1.js",
  1976. "type": "source",
  1977. "headers": {
  1978. "content-type": "application/json",
  1979. },
  1980. },
  1981. "files/_/_/file2.js": {
  1982. "url": "~/file2.js",
  1983. "type": "source",
  1984. "headers": {
  1985. "content-type": "application/json",
  1986. },
  1987. },
  1988. "files/_/_/empty.js": {
  1989. "url": "~/empty.js",
  1990. "type": "source",
  1991. "headers": {
  1992. "content-type": "application/json",
  1993. },
  1994. },
  1995. "files/_/_/file.sourcemap.js": {
  1996. "url": "~/file.sourcemap.js",
  1997. "type": "source_map",
  1998. "headers": {
  1999. "content-type": "application/json",
  2000. "debug-id": debug_id,
  2001. },
  2002. },
  2003. }
  2004. }
  2005. ),
  2006. )
  2007. compressed.seek(0)
  2008. file = File.objects.create(name="bundle.zip", type="artifact.bundle")
  2009. file.putfile(compressed)
  2010. artifact_bundle = ArtifactBundle.objects.create(
  2011. organization_id=self.organization.id, bundle_id=uuid4(), file=file, artifact_count=5
  2012. )
  2013. ProjectArtifactBundle.objects.create(
  2014. organization_id=self.organization.id,
  2015. project_id=self.project.id,
  2016. artifact_bundle=artifact_bundle,
  2017. )
  2018. DebugIdArtifactBundle.objects.create(
  2019. organization_id=self.organization.id,
  2020. debug_id=debug_id,
  2021. artifact_bundle=artifact_bundle,
  2022. source_file_type=SourceFileType.MINIFIED_SOURCE.value,
  2023. )
  2024. DebugIdArtifactBundle.objects.create(
  2025. organization_id=self.organization.id,
  2026. debug_id=debug_id,
  2027. artifact_bundle=artifact_bundle,
  2028. source_file_type=SourceFileType.SOURCE_MAP.value,
  2029. )
  2030. data = {
  2031. "timestamp": self.min_ago,
  2032. "message": "hello",
  2033. "platform": "javascript",
  2034. "release": "abc",
  2035. "exception": {
  2036. "values": [
  2037. {
  2038. "type": "Error",
  2039. "stacktrace": {
  2040. "frames": [
  2041. {
  2042. "abs_path": "http://example.com/file.min.js",
  2043. "filename": "file.min.js",
  2044. "lineno": 1,
  2045. "colno": 39,
  2046. },
  2047. {
  2048. "abs_path": "http://example.com/file.min.js",
  2049. "filename": "file.min.js",
  2050. "lineno": 1,
  2051. "colno": 79,
  2052. },
  2053. ]
  2054. },
  2055. }
  2056. ]
  2057. },
  2058. "debug_meta": {
  2059. "images": [
  2060. {
  2061. "type": "sourcemap",
  2062. "debug_id": debug_id,
  2063. "code_file": "http://example.com/file.min.js",
  2064. }
  2065. ]
  2066. },
  2067. }
  2068. event = self.post_and_retrieve_event(data)
  2069. # NOTE: Symbolicator processor has a better fallback to pull specific files for source context
  2070. if process_with_symbolicator:
  2071. assert "errors" not in event.data
  2072. exception = event.interfaces["exception"]
  2073. frame_list = exception.values[0].stacktrace.frames
  2074. frame = frame_list[0]
  2075. if process_with_symbolicator:
  2076. assert frame.data["resolved_with"] == "debug-id"
  2077. assert frame.data["symbolicated"]
  2078. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  2079. assert frame.context_line == "\treturn a + b; // fôo"
  2080. if process_with_symbolicator:
  2081. assert frame.post_context == ["}"]
  2082. else:
  2083. assert frame.post_context == ["}", ""]
  2084. frame = frame_list[1]
  2085. if process_with_symbolicator:
  2086. assert frame.data["resolved_with"] == "debug-id"
  2087. assert frame.data["symbolicated"]
  2088. assert frame.pre_context == ["function multiply(a, b) {", '\t"use strict";']
  2089. assert frame.context_line == "\treturn a * b;"
  2090. assert frame.post_context == [
  2091. "}",
  2092. "function divide(a, b) {",
  2093. '\t"use strict";',
  2094. "\ttry {",
  2095. "\t\treturn multiply(add(a, b), a, b) / c;",
  2096. ]
  2097. else:
  2098. assert len(event.data["errors"]) == 3
  2099. assert event.data["errors"][0] == {
  2100. "type": "js_missing_sources_content",
  2101. "source": "http://example.com/file.min.js",
  2102. "sourcemap": f"debug-id://{debug_id}/~/file.sourcemap.js",
  2103. }
  2104. @requires_symbolicator
  2105. @pytest.mark.symbolicator
  2106. def test_expansion_with_debug_id_and_malformed_sourcemap(self, process_with_symbolicator):
  2107. debug_id = "c941d872-af1f-4f0c-a7ff-ad3d295fe153"
  2108. compressed = BytesIO(b"SYSB")
  2109. with zipfile.ZipFile(compressed, "a") as zip_file:
  2110. zip_file.writestr("files/_/_/file.min.js", load_fixture("file.min.js"))
  2111. zip_file.writestr("files/_/_/file1.js", load_fixture("file1.js"))
  2112. zip_file.writestr("files/_/_/file2.js", load_fixture("file2.js"))
  2113. zip_file.writestr("files/_/_/empty.js", load_fixture("empty.js"))
  2114. zip_file.writestr(
  2115. "files/_/_/file.malformed.sourcemap.js", load_fixture("file.malformed.sourcemap.js")
  2116. )
  2117. zip_file.writestr(
  2118. "manifest.json",
  2119. json.dumps(
  2120. {
  2121. "files": {
  2122. "files/_/_/file.min.js": {
  2123. "url": "~/file.min.js",
  2124. "type": "minified_source",
  2125. "headers": {
  2126. "content-type": "application/json",
  2127. "debug-id": debug_id,
  2128. "sourcemap": "file.malformed.sourcemap.js",
  2129. },
  2130. },
  2131. "files/_/_/file1.js": {
  2132. "url": "~/file1.js",
  2133. "type": "source",
  2134. "headers": {
  2135. "content-type": "application/json",
  2136. },
  2137. },
  2138. "files/_/_/file2.js": {
  2139. "url": "~/file2.js",
  2140. "type": "source",
  2141. "headers": {
  2142. "content-type": "application/json",
  2143. },
  2144. },
  2145. "files/_/_/empty.js": {
  2146. "url": "~/empty.js",
  2147. "type": "source",
  2148. "headers": {
  2149. "content-type": "application/json",
  2150. },
  2151. },
  2152. "files/_/_/file.malformed.sourcemap.js": {
  2153. "url": "~/file.malformed.sourcemap.js",
  2154. "type": "source_map",
  2155. "headers": {
  2156. "content-type": "application/json",
  2157. "debug-id": debug_id,
  2158. },
  2159. },
  2160. }
  2161. }
  2162. ),
  2163. )
  2164. compressed.seek(0)
  2165. file = File.objects.create(name="bundle.zip", type="artifact.bundle")
  2166. file.putfile(compressed)
  2167. artifact_bundle = ArtifactBundle.objects.create(
  2168. organization_id=self.organization.id, bundle_id=uuid4(), file=file, artifact_count=5
  2169. )
  2170. ProjectArtifactBundle.objects.create(
  2171. organization_id=self.organization.id,
  2172. project_id=self.project.id,
  2173. artifact_bundle=artifact_bundle,
  2174. )
  2175. DebugIdArtifactBundle.objects.create(
  2176. organization_id=self.organization.id,
  2177. debug_id=debug_id,
  2178. artifact_bundle=artifact_bundle,
  2179. source_file_type=SourceFileType.MINIFIED_SOURCE.value,
  2180. )
  2181. DebugIdArtifactBundle.objects.create(
  2182. organization_id=self.organization.id,
  2183. debug_id=debug_id,
  2184. artifact_bundle=artifact_bundle,
  2185. source_file_type=SourceFileType.SOURCE_MAP.value,
  2186. )
  2187. data = {
  2188. "timestamp": self.min_ago,
  2189. "message": "hello",
  2190. "platform": "javascript",
  2191. "release": "abc",
  2192. "exception": {
  2193. "values": [
  2194. {
  2195. "type": "Error",
  2196. "stacktrace": {
  2197. "frames": [
  2198. {
  2199. "abs_path": "http://example.com/file.min.js",
  2200. "filename": "file.min.js",
  2201. "lineno": 1,
  2202. "colno": 39,
  2203. },
  2204. {
  2205. "abs_path": "http://example.com/file.min.js",
  2206. "filename": "file.min.js",
  2207. "lineno": 1,
  2208. "colno": 79,
  2209. },
  2210. ]
  2211. },
  2212. }
  2213. ]
  2214. },
  2215. "debug_meta": {
  2216. "images": [
  2217. {
  2218. "type": "sourcemap",
  2219. "debug_id": debug_id,
  2220. "code_file": "http://example.com/file.min.js",
  2221. }
  2222. ]
  2223. },
  2224. }
  2225. event = self.post_and_retrieve_event(data)
  2226. assert len(event.data["errors"]) == 1
  2227. # NOTE: Its not obvious what data we want here yet.
  2228. if process_with_symbolicator:
  2229. assert event.data["errors"][0] == {
  2230. "type": "js_invalid_source",
  2231. "url": "http://example.com/file.malformed.sourcemap.js",
  2232. }
  2233. else:
  2234. assert event.data["errors"][0] == {
  2235. "type": "js_invalid_source",
  2236. "debug_id": f"debug-id://{debug_id}/~/file.malformed.sourcemap.js",
  2237. }
  2238. @requires_symbolicator
  2239. @pytest.mark.symbolicator
  2240. def test_expansion_with_debug_id_not_found(self, process_with_symbolicator):
  2241. project = self.project
  2242. release = Release.objects.create(organization_id=project.organization_id, version="abc")
  2243. release.add_project(project)
  2244. manifest = {
  2245. "org": self.organization.slug,
  2246. "release": release.version,
  2247. "files": {
  2248. "files/_/_/file.min.js": {
  2249. "url": "http://example.com/file.min.js",
  2250. "type": "minified_source",
  2251. },
  2252. "files/_/_/file1.js": {
  2253. "url": "http://example.com/file1.js",
  2254. "type": "source",
  2255. },
  2256. "files/_/_/file2.js": {
  2257. "url": "http://example.com/file2.js",
  2258. "type": "source",
  2259. },
  2260. "files/_/_/file.sourcemap.js": {
  2261. "url": "http://example.com/file.sourcemap.js",
  2262. "type": "source_map",
  2263. },
  2264. },
  2265. }
  2266. compressed = BytesIO(b"SYSB")
  2267. with zipfile.ZipFile(compressed, "a") as zip_file:
  2268. for rel_path, entry in manifest["files"].items():
  2269. name = os.path.basename(rel_path)
  2270. content = load_fixture(name)
  2271. if name == "file.min.js":
  2272. # Remove link to source map, add to header instead
  2273. content = content.replace(b"//@ sourceMappingURL=file.sourcemap.js", b"")
  2274. entry["headers"] = {"Sourcemap": "file.sourcemap.js"}
  2275. zip_file.writestr(rel_path, content)
  2276. zip_file.writestr("manifest.json", json.dumps(manifest))
  2277. compressed.seek(0)
  2278. file = File.objects.create(name="release_bundle.zip", type="release.bundle")
  2279. file.putfile(compressed)
  2280. update_artifact_index(release, None, file)
  2281. debug_id = "c941d872-af1f-4f0c-a7ff-ad3d295fe153"
  2282. data = {
  2283. "timestamp": self.min_ago,
  2284. "message": "hello",
  2285. "platform": "javascript",
  2286. "release": "abc",
  2287. "exception": {
  2288. "values": [
  2289. {
  2290. "type": "Error",
  2291. "stacktrace": {
  2292. "frames": [
  2293. {
  2294. "abs_path": "http://example.com/file.min.js",
  2295. "filename": "file.min.js",
  2296. "lineno": 1,
  2297. "colno": 39,
  2298. },
  2299. {
  2300. "abs_path": "http://example.com/file.min.js",
  2301. "filename": "file.min.js",
  2302. "lineno": 1,
  2303. "colno": 79,
  2304. },
  2305. # We want also to test the source without minification.
  2306. {
  2307. "abs_path": "http://example.com/file1.js",
  2308. "filename": "file1.js",
  2309. "lineno": 3,
  2310. "colno": 12,
  2311. },
  2312. ]
  2313. },
  2314. }
  2315. ]
  2316. },
  2317. "debug_meta": {
  2318. "images": [
  2319. {
  2320. "type": "sourcemap",
  2321. "debug_id": debug_id,
  2322. "code_file": "http://example.com/file.min.js",
  2323. }
  2324. ]
  2325. },
  2326. }
  2327. event = self.post_and_retrieve_event(data)
  2328. exception = event.interfaces["exception"]
  2329. frame_list = exception.values[0].stacktrace.frames
  2330. frame = frame_list[0]
  2331. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  2332. assert frame.context_line == "\treturn a + b; // fôo"
  2333. if process_with_symbolicator:
  2334. assert frame.post_context == ["}"]
  2335. else:
  2336. assert frame.post_context == ["}", ""]
  2337. frame = frame_list[1]
  2338. assert frame.pre_context == ["function multiply(a, b) {", '\t"use strict";']
  2339. assert frame.context_line == "\treturn a * b;"
  2340. assert frame.post_context == [
  2341. "}",
  2342. "function divide(a, b) {",
  2343. '\t"use strict";',
  2344. "\ttry {",
  2345. "\t\treturn multiply(add(a, b), a, b) / c;",
  2346. ]
  2347. frame = frame_list[2]
  2348. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  2349. assert frame.context_line == "\treturn a + b; // fôo"
  2350. if process_with_symbolicator:
  2351. assert frame.post_context == ["}"]
  2352. else:
  2353. assert frame.post_context == ["}", ""]
  2354. @requires_symbolicator
  2355. @pytest.mark.symbolicator
  2356. def test_expansion_with_release_dist_pair_x(self, process_with_symbolicator):
  2357. project = self.project
  2358. release = Release.objects.create(organization_id=project.organization_id, version="abc")
  2359. release.add_project(project)
  2360. dist = release.add_dist("android")
  2361. # We want to also add debug_id information inside the manifest but not in the stack trace to replicate a
  2362. # real edge case that we can incur in.
  2363. debug_id = "c941d872-af1f-4f0c-a7ff-ad3d295fe153"
  2364. compressed = BytesIO(b"SYSB")
  2365. with zipfile.ZipFile(compressed, "a") as zip_file:
  2366. zip_file.writestr("files/_/_/file.min.js", load_fixture("file.min.js"))
  2367. zip_file.writestr("files/_/_/file1.js", load_fixture("file1.js"))
  2368. zip_file.writestr("files/_/_/file2.js", load_fixture("file2.js"))
  2369. zip_file.writestr("files/_/_/empty.js", load_fixture("empty.js"))
  2370. zip_file.writestr(
  2371. "files/_/_/file.wc.sourcemap.js", load_fixture("file.wc.sourcemap.js")
  2372. )
  2373. zip_file.writestr(
  2374. "manifest.json",
  2375. json.dumps(
  2376. {
  2377. "files": {
  2378. "files/_/_/file.min.js": {
  2379. "url": "~/file.min.js",
  2380. "type": "minified_source",
  2381. "headers": {
  2382. "content-type": "application/json",
  2383. "sourcemap": "file.wc.sourcemap.js",
  2384. "debug-id": debug_id,
  2385. },
  2386. },
  2387. "files/_/_/file1.js": {
  2388. "url": "~/file1.js",
  2389. "type": "source",
  2390. "headers": {
  2391. "content-type": "application/json",
  2392. },
  2393. },
  2394. "files/_/_/file2.js": {
  2395. "url": "~/file2.js",
  2396. "type": "source",
  2397. "headers": {
  2398. "content-type": "application/json",
  2399. },
  2400. },
  2401. "files/_/_/empty.js": {
  2402. "url": "~/empty.js",
  2403. "type": "source",
  2404. "headers": {
  2405. "content-type": "application/json",
  2406. },
  2407. },
  2408. "files/_/_/file.wc.sourcemap.js": {
  2409. "url": "~/file.wc.sourcemap.js",
  2410. "type": "source_map",
  2411. "headers": {
  2412. "content-type": "application/json",
  2413. "debug-id": debug_id,
  2414. },
  2415. },
  2416. },
  2417. }
  2418. ),
  2419. )
  2420. compressed.seek(0)
  2421. file = File.objects.create(name="bundle.zip", type="artifact.bundle")
  2422. file.putfile(compressed)
  2423. artifact_bundle = ArtifactBundle.objects.create(
  2424. organization_id=self.organization.id, bundle_id=uuid4(), file=file, artifact_count=5
  2425. )
  2426. ProjectArtifactBundle.objects.create(
  2427. organization_id=self.organization.id,
  2428. project_id=self.project.id,
  2429. artifact_bundle=artifact_bundle,
  2430. )
  2431. ReleaseArtifactBundle.objects.create(
  2432. organization_id=self.organization.id,
  2433. release_name=release.version,
  2434. dist_name=dist.name,
  2435. artifact_bundle=artifact_bundle,
  2436. )
  2437. data = {
  2438. "timestamp": self.min_ago,
  2439. "message": "hello",
  2440. "platform": "javascript",
  2441. "release": release.version,
  2442. "dist": dist.name,
  2443. "exception": {
  2444. "values": [
  2445. {
  2446. "type": "Error",
  2447. "stacktrace": {
  2448. "frames": [
  2449. {
  2450. "abs_path": "http://example.com/file.min.js",
  2451. "filename": "file.min.js",
  2452. "lineno": 1,
  2453. "colno": 39,
  2454. },
  2455. {
  2456. "abs_path": "http://example.com/file.min.js",
  2457. "filename": "file.min.js",
  2458. "lineno": 1,
  2459. "colno": 79,
  2460. },
  2461. # We want also to test the source without minification.
  2462. {
  2463. "abs_path": "http://example.com/file1.js",
  2464. "filename": "file1.js",
  2465. "lineno": 3,
  2466. "colno": 12,
  2467. },
  2468. ]
  2469. },
  2470. }
  2471. ]
  2472. },
  2473. }
  2474. event = self.post_and_retrieve_event(data)
  2475. assert "errors" not in event.data
  2476. exception = event.interfaces["exception"]
  2477. frame_list = exception.values[0].stacktrace.frames
  2478. frame = frame_list[0]
  2479. if process_with_symbolicator:
  2480. assert frame.data["resolved_with"] == "release"
  2481. assert frame.data["symbolicated"]
  2482. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  2483. assert frame.context_line == "\treturn a + b; // fôo"
  2484. if process_with_symbolicator:
  2485. assert frame.post_context == ["}"]
  2486. else:
  2487. assert frame.post_context == ["}", ""]
  2488. frame = frame_list[1]
  2489. if process_with_symbolicator:
  2490. assert frame.data["resolved_with"] == "release"
  2491. assert frame.data["symbolicated"]
  2492. assert frame.pre_context == ["function multiply(a, b) {", '\t"use strict";']
  2493. assert frame.context_line == "\treturn a * b;"
  2494. assert frame.post_context == [
  2495. "}",
  2496. "function divide(a, b) {",
  2497. '\t"use strict";',
  2498. "\ttry {",
  2499. "\t\treturn multiply(add(a, b), a, b) / c;",
  2500. ]
  2501. frame = frame_list[2]
  2502. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  2503. assert frame.context_line == "\treturn a + b; // fôo"
  2504. if process_with_symbolicator:
  2505. assert frame.post_context == ["}"]
  2506. else:
  2507. assert frame.post_context == ["}", ""]
  2508. @requires_symbolicator
  2509. @pytest.mark.symbolicator
  2510. def test_expansion_with_release_dist_pair_and_sourcemap_without_sources_content(
  2511. self, process_with_symbolicator
  2512. ):
  2513. project = self.project
  2514. release = Release.objects.create(organization_id=project.organization_id, version="abc")
  2515. release.add_project(project)
  2516. dist = release.add_dist("android")
  2517. compressed = BytesIO(b"SYSB")
  2518. with zipfile.ZipFile(compressed, "a") as zip_file:
  2519. zip_file.writestr("files/_/_/file.min.js", load_fixture("file.min.js"))
  2520. zip_file.writestr("files/_/_/file1.js", load_fixture("file1.js"))
  2521. zip_file.writestr("files/_/_/file2.js", load_fixture("file2.js"))
  2522. zip_file.writestr("files/_/_/empty.js", load_fixture("empty.js"))
  2523. zip_file.writestr("files/_/_/file.sourcemap.js", load_fixture("file.sourcemap.js"))
  2524. zip_file.writestr(
  2525. "manifest.json",
  2526. json.dumps(
  2527. {
  2528. "files": {
  2529. "files/_/_/file.min.js": {
  2530. "url": "~/file.min.js",
  2531. "type": "minified_source",
  2532. "headers": {
  2533. "content-type": "application/json",
  2534. "sourcemap": "file.sourcemap.js",
  2535. },
  2536. },
  2537. "files/_/_/file1.js": {
  2538. "url": "~/file1.js",
  2539. "type": "source",
  2540. "headers": {
  2541. "content-type": "application/json",
  2542. },
  2543. },
  2544. "files/_/_/file2.js": {
  2545. "url": "~/file2.js",
  2546. "type": "source",
  2547. "headers": {
  2548. "content-type": "application/json",
  2549. },
  2550. },
  2551. "files/_/_/empty.js": {
  2552. "url": "~/empty.js",
  2553. "type": "source",
  2554. "headers": {
  2555. "content-type": "application/json",
  2556. },
  2557. },
  2558. "files/_/_/file.sourcemap.js": {
  2559. "url": "~/file.sourcemap.js",
  2560. "type": "source_map",
  2561. "headers": {
  2562. "content-type": "application/json",
  2563. },
  2564. },
  2565. }
  2566. }
  2567. ),
  2568. )
  2569. compressed.seek(0)
  2570. file = File.objects.create(name="bundle.zip", type="artifact.bundle")
  2571. file.putfile(compressed)
  2572. artifact_bundle = ArtifactBundle.objects.create(
  2573. organization_id=self.organization.id, bundle_id=uuid4(), file=file, artifact_count=5
  2574. )
  2575. ProjectArtifactBundle.objects.create(
  2576. organization_id=self.organization.id,
  2577. project_id=self.project.id,
  2578. artifact_bundle=artifact_bundle,
  2579. )
  2580. ReleaseArtifactBundle.objects.create(
  2581. organization_id=self.organization.id,
  2582. release_name=release.version,
  2583. dist_name=dist.name,
  2584. artifact_bundle=artifact_bundle,
  2585. )
  2586. data = {
  2587. "timestamp": self.min_ago,
  2588. "message": "hello",
  2589. "platform": "javascript",
  2590. "release": release.version,
  2591. "dist": dist.name,
  2592. "exception": {
  2593. "values": [
  2594. {
  2595. "type": "Error",
  2596. "stacktrace": {
  2597. "frames": [
  2598. {
  2599. "abs_path": "http://example.com/file.min.js",
  2600. "filename": "file.min.js",
  2601. "lineno": 1,
  2602. "colno": 39,
  2603. },
  2604. {
  2605. "abs_path": "http://example.com/file.min.js",
  2606. "filename": "file.min.js",
  2607. "lineno": 1,
  2608. "colno": 79,
  2609. },
  2610. ]
  2611. },
  2612. }
  2613. ]
  2614. },
  2615. }
  2616. event = self.post_and_retrieve_event(data)
  2617. # NOTE: Symbolicator processor has a better fallback to pull specific files for source context
  2618. if process_with_symbolicator:
  2619. assert "errors" not in event.data
  2620. exception = event.interfaces["exception"]
  2621. frame_list = exception.values[0].stacktrace.frames
  2622. frame = frame_list[0]
  2623. if process_with_symbolicator:
  2624. assert frame.data["resolved_with"] == "release"
  2625. assert frame.data["symbolicated"]
  2626. assert frame.pre_context == ["function add(a, b) {", '\t"use strict";']
  2627. assert frame.context_line == "\treturn a + b; // fôo"
  2628. if process_with_symbolicator:
  2629. assert frame.post_context == ["}"]
  2630. else:
  2631. assert frame.post_context == ["}", ""]
  2632. frame = frame_list[1]
  2633. if process_with_symbolicator:
  2634. assert frame.data["resolved_with"] == "release"
  2635. assert frame.data["symbolicated"]
  2636. assert frame.pre_context == ["function multiply(a, b) {", '\t"use strict";']
  2637. assert frame.context_line == "\treturn a * b;"
  2638. assert frame.post_context == [
  2639. "}",
  2640. "function divide(a, b) {",
  2641. '\t"use strict";',
  2642. "\ttry {",
  2643. "\t\treturn multiply(add(a, b), a, b) / c;",
  2644. ]
  2645. else:
  2646. assert len(event.data["errors"]) == 3
  2647. assert event.data["errors"][0] == {
  2648. "type": "js_missing_sources_content",
  2649. "source": "http://example.com/file.min.js",
  2650. "sourcemap": "http://example.com/file.sourcemap.js",
  2651. }
  2652. @requires_symbolicator
  2653. @pytest.mark.symbolicator
  2654. def test_expansion_with_release_and_malformed_sourcemap(self, process_with_symbolicator):
  2655. project = self.project
  2656. release = Release.objects.create(organization_id=project.organization_id, version="abc")
  2657. release.add_project(project)
  2658. compressed = BytesIO(b"SYSB")
  2659. with zipfile.ZipFile(compressed, "a") as zip_file:
  2660. zip_file.writestr("files/_/_/file.min.js", load_fixture("file.min.js"))
  2661. zip_file.writestr("files/_/_/file1.js", load_fixture("file1.js"))
  2662. zip_file.writestr("files/_/_/file2.js", load_fixture("file2.js"))
  2663. zip_file.writestr("files/_/_/empty.js", load_fixture("empty.js"))
  2664. zip_file.writestr(
  2665. "files/_/_/file.malformed.sourcemap.js", load_fixture("file.malformed.sourcemap.js")
  2666. )
  2667. zip_file.writestr(
  2668. "manifest.json",
  2669. json.dumps(
  2670. {
  2671. "files": {
  2672. "files/_/_/file.min.js": {
  2673. "url": "~/file.min.js",
  2674. "type": "minified_source",
  2675. "headers": {
  2676. "content-type": "application/json",
  2677. "sourcemap": "file.malformed.sourcemap.js",
  2678. },
  2679. },
  2680. "files/_/_/file1.js": {
  2681. "url": "~/file1.js",
  2682. "type": "source",
  2683. "headers": {
  2684. "content-type": "application/json",
  2685. },
  2686. },
  2687. "files/_/_/file2.js": {
  2688. "url": "~/file2.js",
  2689. "type": "source",
  2690. "headers": {
  2691. "content-type": "application/json",
  2692. },
  2693. },
  2694. "files/_/_/empty.js": {
  2695. "url": "~/empty.js",
  2696. "type": "source",
  2697. "headers": {
  2698. "content-type": "application/json",
  2699. },
  2700. },
  2701. "files/_/_/file.malformed.sourcemap.js": {
  2702. "url": "~/file.malformed.sourcemap.js",
  2703. "type": "source_map",
  2704. "headers": {
  2705. "content-type": "application/json",
  2706. },
  2707. },
  2708. }
  2709. }
  2710. ),
  2711. )
  2712. compressed.seek(0)
  2713. file = File.objects.create(name="bundle.zip", type="artifact.bundle")
  2714. file.putfile(compressed)
  2715. artifact_bundle = ArtifactBundle.objects.create(
  2716. organization_id=self.organization.id, bundle_id=uuid4(), file=file, artifact_count=5
  2717. )
  2718. ProjectArtifactBundle.objects.create(
  2719. organization_id=self.organization.id,
  2720. project_id=self.project.id,
  2721. artifact_bundle=artifact_bundle,
  2722. )
  2723. ReleaseArtifactBundle.objects.create(
  2724. organization_id=self.organization.id,
  2725. release_name=release.version,
  2726. artifact_bundle=artifact_bundle,
  2727. )
  2728. data = {
  2729. "timestamp": self.min_ago,
  2730. "message": "hello",
  2731. "platform": "javascript",
  2732. "release": release.version,
  2733. "exception": {
  2734. "values": [
  2735. {
  2736. "type": "Error",
  2737. "stacktrace": {
  2738. "frames": [
  2739. {
  2740. "abs_path": "http://example.com/file.min.js",
  2741. "filename": "file.min.js",
  2742. "lineno": 1,
  2743. "colno": 39,
  2744. },
  2745. {
  2746. "abs_path": "http://example.com/file.min.js",
  2747. "filename": "file.min.js",
  2748. "lineno": 1,
  2749. "colno": 79,
  2750. },
  2751. ]
  2752. },
  2753. }
  2754. ]
  2755. },
  2756. }
  2757. event = self.post_and_retrieve_event(data)
  2758. assert len(event.data["errors"]) == 1
  2759. assert event.data["errors"][0] == {
  2760. "type": "js_invalid_source",
  2761. "url": "http://example.com/file.malformed.sourcemap.js",
  2762. }