Skip to content

WAF Bypass && getUser max redirect#310

Merged
clins1994 merged 11 commits intoMathieu2301:mainfrom
OguzBey:main
Apr 11, 2026
Merged

WAF Bypass && getUser max redirect#310
clins1994 merged 11 commits intoMathieu2301:mainfrom
OguzBey:main

Conversation

@OguzBey
Copy link
Copy Markdown
Contributor

@OguzBey OguzBey commented Oct 26, 2025

Summary

  • Add default browser-like HTTP headers for WebSocket connections (required by TradingView WAF)
  • Headers are configurable via clientOptions.headers — user values override defaults
  • Add from=chart query param to WebSocket URL
  • Add a max redirect limit (5) to getUser() to prevent infinite redirect loops caused by WAF or geo-restrictions
  • Add unit test for redirect loop protection

Usage

// Default headers work out of the box
const client = new TradingView.Client();

// Users can override any header (e.g. rotate User-Agent, change language)
const client = new TradingView.Client({
  headers: {
    'User-Agent': 'Custom UA string',
    'Accept-Language': 'fr-FR,fr;q=0.9',
  },
});

Before (infinite redirect loop in getUser)

sequenceDiagram
    participant Client
    participant TradingView

    Client->>TradingView: getUser(session) → GET /
    TradingView-->>Client: 302 → /accounts/signin/
    Client->>TradingView: getUser(session) → GET /accounts/signin/
    TradingView-->>Client: 302 → /
    Client->>TradingView: getUser(session) → GET /
    TradingView-->>Client: 302 → /accounts/signin/
    Note over Client,TradingView: ♻️ Infinite loop → OOM crash
Loading

After (redirect protection)

sequenceDiagram
    participant Client
    participant TradingView

    Client->>TradingView: getUser(session, redirectCount=0) → GET /
    TradingView-->>Client: 302 → /accounts/signin/
    Client->>TradingView: getUser(session, redirectCount=1) → GET /accounts/signin/
    TradingView-->>Client: 302 → /
    Note over Client,TradingView: ...redirectCount increments each hop...
    Client->>TradingView: getUser(session, redirectCount=5) → GET /
    TradingView-->>Client: 302 → /accounts/signin/
    Client->>Client: redirectCount > 5 → throw Error
    Note over Client: "Too many redirects - possible WAF or geo-restriction"
Loading

Test plan

  • Verify WebSocket connection works with default headers
  • Verify clientOptions.headers overrides defaults
  • Verify getUser() works with valid session/signature credentials
  • Verify getUser() throws after 5 redirects instead of looping forever
  • Verify the from=chart query param addition does not break existing chart sessions
  • Unit test for redirect loop protection

Test notes

Key finding: TradingView now requires browser-like headers for WebSocket connections. Without them, connections hang even from non-restricted regions. Default headers are necessary, not optional.

Redirect protection (TDD verified):

Branch Mocked redirect loop Real credentials
main (before) JS heap OOM — infinite recursion Works (returns user)
pr-310 (after) Throws "Too many redirects" after 6 calls, 2ms Works (returns user)

CI failures (builtInIndicator > gets volume profile) are pre-existing and unrelated to this PR.


This description was automatically generated by Claude.

@OguzBey
Copy link
Copy Markdown
Contributor Author

OguzBey commented Jan 11, 2026

where are you @Mathieu2301

@clins1994
Copy link
Copy Markdown
Collaborator

@OguzBey Hey! I went ahead and added a description and test plan to this PR since it was missing one. Let me know if anything needs adjusting.

Generated automatically by Claude.

@clins1994 clins1994 self-assigned this Apr 11, 2026
@clins1994 clins1994 self-requested a review April 11, 2026 09:16
clins1994 and others added 2 commits April 11, 2026 18:34
- Change error message to "possible WAF or geo-restriction" since
  redirect loops are caused by WAF/geo-blocking, not invalid credentials
- Add unit test verifying redirect loop is capped at ≤10 calls

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Replace ++ with += 1 (no-plusplus)
- Add eslint-disable for global-require (needed for cache patching)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
clins1994 and others added 2 commits April 11, 2026 19:07
- Provide default browser-like headers (required by TradingView WAF)
- Allow users to override via clientOptions.headers
- Update User-Agent to Windows/Chrome 131 (more common, more recent)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@clins1994
Copy link
Copy Markdown
Collaborator

clins1994 commented Apr 11, 2026

@OguzBey this should do it. tested it with my TradingView account and added unit tests

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@clins1994 clins1994 merged commit 574a994 into Mathieu2301:main Apr 11, 2026
5 checks passed
@OguzBey
Copy link
Copy Markdown
Contributor Author

OguzBey commented Apr 11, 2026

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants