Documentation
¶
Overview ¶
Package jsbundle recovers JavaScript source maps from minified bundles. Used by error-reporting services to de-obfuscate stack traces.
The bundle URLs come from pages and error reports, so they are untrusted. By default (RecoverOptions.Client nil) every fetch goes through a safehttp client that refuses internal addresses; supply a client only when you need to reach one on purpose, such as an httptest server in a test.
Package jsbundle recovers original JavaScript source from minified production bundles using source maps, then scans the recovered source. It is the shared pipeline used by go_jsbundle_secrets, go_jsbundle_route_extractor, go_postmessage_listener_finder, and go_prototype_pollution_static.
Pipeline:
- Parse the target HTML.
- Collect every <script src=...> URL (absolute + same-origin relative).
- For each script, GET it (subject to the caller's *http.Client).
- Look for a sourceMappingURL comment, or probe "<bundle>.map" as fallback.
- If a v3 source map is found, decode the "sourcesContent" array and use that as the recovered source (one entry per original file).
- If no map exists, fall back to the minified bundle body as the "source" so callers can still grep for patterns.
The package is intentionally allocation-conservative: each recovered source chunk is returned with its origin URL and file path so callers can cite findings back to a specific source.
Index ¶
Constants ¶
const ( // DefaultMaxScriptBytes caps a single bundle body. Production bundles // over a few MB are exceptional. DefaultMaxScriptBytes int64 = 8 * 1024 * 1024 // DefaultMaxScripts caps how many <script src> tags we follow per page. DefaultMaxScripts = 32 // DefaultMaxConcurrency caps parallel script fetches. DefaultMaxConcurrency = 4 )
Variables ¶
var ErrNotJS = errors.New("response body is not JavaScript or JSON")
ErrNotJS is returned when the response body does not look like JavaScript or JSON. Currently unused but reserved for stricter callers.
Functions ¶
Types ¶
type RecoverOptions ¶
type RecoverOptions struct {
// Client fetches bundles and source maps. If nil, a safehttp client is
// used: it refuses loopback, private, link-local and other internal
// addresses (also after redirects), because the URLs come from pages and
// error reports that the caller does not control. Pass your own client to
// opt out, for example the httptest server's client in a test.
Client *http.Client
UserAgent string
MaxScriptBytes int64
MaxScripts int
MaxConcurrency int
}
RecoverOptions tunes the pipeline.
type Source ¶
type Source struct {
// BundleURL is the URL of the minified bundle this came from.
BundleURL string `json:"bundle_url"`
// FilePath is the "sources[i]" entry from the source map (e.g.
// "webpack:///./src/foo.js"). Empty when the map was unavailable.
FilePath string `json:"file_path,omitempty"`
// FromMap is true when this chunk came from a sourcesContent entry.
// False means the chunk is the raw (likely minified) bundle body.
FromMap bool `json:"from_map"`
// Content is the recovered source text.
Content string `json:"-"`
// SizeBytes is len(Content). Exposed for telemetry without dumping body.
SizeBytes int `json:"size_bytes"`
}
Source is one chunk of recovered source code.
func Recover ¶
Recover downloads bundleURL and any associated .map, returning one Source per original file when a source map is available, or a single Source with the raw bundle body otherwise. With no opt.Client it uses a shared safehttp client; if that cannot be built (FLEET_REQUIRE_PROXY=1 with no proxy variable set) it returns the error and fetches nothing.
func RecoverAll ¶
func RecoverAll(ctx context.Context, bundleURLs []string, opt RecoverOptions) []Source
RecoverAll runs Recover across every bundleURL with bounded concurrency. Failed bundles are silently dropped — partial recovery is the common case. If the default client cannot be built (see Recover) it logs the reason and returns nil.
func RecoverFromPage ¶
func RecoverFromPage(ctx context.Context, pageHTML string, pageURL *url.URL, opt RecoverOptions) []Source
RecoverFromPage convenience wrapper: fetches html (caller may have it already and pass via Source.Content workaround), but this variant takes a pre-fetched body and a base URL.