test_plugin.py 108 KB

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