diff --git a/src/index.js b/src/index.js index b0afdcd..a25ad95 100644 --- a/src/index.js +++ b/src/index.js @@ -1304,14 +1304,31 @@ app.get( postLoaded = true; if (/^\d+$/.test(threadId)) { try { - const locationUrl = buildGraphAPIURL( - threadId, - { [PARAMS__FIELDS]: FIELD__LOCATION_ID }, - req.session.access_token + const locationUrl = new URL( + buildGraphAPIURL( + encodeURIComponent(threadId), + { [PARAMS__FIELDS]: FIELD__LOCATION_ID }, + null + ) + ); + if ( + locationUrl.origin !== 'https://graph.threads.com' || + locationUrl.username || + locationUrl.password || + locationUrl.hash + ) { + throw new Error('Invalid post location request URL.'); + } + if (req.session.access_token) { + locationUrl.searchParams.append( + PARAMS__ACCESS_TOKEN, + req.session.access_token + ); + } + const locationResponse = await axios.get( + locationUrl.toString(), + { httpsAgent: agent, maxRedirects: 0 } ); - const locationResponse = await axios.get(locationUrl, { - httpsAgent: agent, - }); data.location_id = locationResponse.data?.location_id; } catch { data.location_id = undefined; diff --git a/test/own-post-deletion.test.js b/test/own-post-deletion.test.js index 9813ec3..b58e50a 100644 --- a/test/own-post-deletion.test.js +++ b/test/own-post-deletion.test.js @@ -295,7 +295,7 @@ test('owned detail shows Delete after Quote and Repost and reuses the existing g assert.equal(reads[1].searchParams.get('fields'), 'location_id'); assert.deepEqual( readConfigs.map((config) => config.maxRedirects), - [undefined, undefined, 0, 0, 0] + [undefined, 0, 0, 0, 0] ); const quotePosition = html.indexOf('Quote'); const repostPosition = html.indexOf('Repost'); diff --git a/test/post-location.test.js b/test/post-location.test.js index c6b2ea1..053ff1a 100644 --- a/test/post-location.test.js +++ b/test/post-location.test.js @@ -65,6 +65,7 @@ test('post location IDs link to location details while poll and ghost fields kee const calls = []; let locationId = '345'; let locationUnavailable = false; + let locationConfig; const request = await startApp(t, async (url, options) => { const apiUrl = new URL(url); calls.push(apiUrl); @@ -84,6 +85,7 @@ test('post location IDs link to location details while poll and ghost fields kee if (apiUrl.pathname === '/345') return { data: { id: '345', name: 'Example Location' } }; if (apiUrl.searchParams.get('fields') === 'location_id') { + locationConfig = options; if (locationUnavailable) throw new Error('Optional location field unavailable.'); return { data: { location_id: locationId } }; @@ -102,8 +104,11 @@ test('post location IDs link to location details while poll and ghost fields kee assert.ok( !calls[0].searchParams.get('fields').split(',').includes('location_id') ); + assert.equal(calls[1].origin, 'https://graph.threads.com'); assert.equal(calls[1].pathname, '/123'); assert.equal(calls[1].searchParams.get('fields'), 'location_id'); + assert.equal(calls[1].searchParams.get('access_token'), 'test-token'); + assert.equal(locationConfig.maxRedirects, 0); assert.match( html, /Location ID<\/td>345<\/a>/ @@ -157,12 +162,16 @@ test('failed primary loads and signed-out requests never perform an optional loc response: { data: { error: { message: 'Post unavailable.' } } }, }; }); + let signedOutCalls = 0; const signedOut = await startApp( t, - () => assert.fail('Unexpected API request'), + () => { + signedOutCalls++; + }, false ); assert.equal((await signedOut('/threads/123')).status, 302); + assert.equal(signedOutCalls, 0); const html = await (await request('/threads/123')).text(); assert.equal(calls, 1); assert.doesNotMatch(html, /locationId=/);