test_plugin.py 113 KB

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