NATION

PASSWORD

Confusion over API

Bug reports, general help, ideas for improvements, and questions about how things are meant to work.
User avatar
New Rogernomics
Powerbroker
 
Posts: 9510
Founded: Aug 22, 2006
Left-wing Utopia

Confusion over API

Postby New Rogernomics » Mon Apr 30, 2018 7:00 am

To start with, I'll say I have next to no Cross-Origin Resource Sharing (CORS) and XMLHttpRequest knowledge, and I don't implement them in my own coding projects.

It seems that XMLHttpRequest are against site rules, but I need clarification over it's use.

Basically it was implemented briefly on the Lazarus forum by one of our members as a test code. Till we were advised this could be in breach of site rules.

I don't know the full origin of the code, or where it came from. Just that it was hosted on the ZB forum, and technically would break site rules somehow.

The first code was meant to take a nation name [typed in by a member], and then show it in a box, as a link.*

The second code added an image retrieval request of some kind.

Sorry about this. I don't want to make a habit of it obviously, and I'll keep a close eye on what scripts are actually implemented in the future.

It was just hard for me to spot this, since as I said before, I don't use them myself, so don't know what they are all about.

*http://support.zathyus.com/topic/5153198/1/

Edit: Added the link to the first script.
Last edited by New Rogernomics on Mon Apr 30, 2018 7:05 am, edited 5 times in total.
Herald (Vice-Delegate) of Lazarus
"Solidarity forever..."
Hoping for Peace in Israel and Palestine
  • Former First Citizen (PM) of Lazarus
  • Former Proedroi (Minister) of Foreign Affairs of Lazarus
  • Former Lazarus Delegate (Humane Republic of Lazarus, 2015)
  • Minister of Culture & Media (Humane Republic of Lazarus)
  • Foreign Minister of The Ascendancy (RIP, and purged)
  • Senator of The Ascendancy (RIP, and purged)
  • Interior Commissioner of Lazarus (Pre-People's Republic of Lazarus)
  • At some point a member of the Grey family...then father vanished...
  • Foreign Minister of The Last Kingdom (RIP)
  • ADN:DSA Rep for Eastern Roman Empire
  • Honoratus Servant of the Holy Land (Eastern Roman Empire)
  • UN/WA Delegate of Trans Atlantice (RIP)

User avatar
Kutumal
Civil Servant
 
Posts: 9
Founded: Apr 22, 2018
Left-Leaning College State

Postby Kutumal » Mon Apr 30, 2018 7:05 am

A brief info on what the code was (I spotted it and told NR about it):

It was retrieving nation/region flag URLs via the API on the client-side. It was using XMLHttpRequest's setRequestHeader() function to set a User-Agent, but browsers explicltly don't allow overriding the UA in that way and, accordingly, it didn't identify it correctly to NationStates. The correct approach would have been to include a "&user_agent=...."-parameter in the URL.

So basically, the intent for compliance was there, just the execution flawed.

EDIT: What NR posted isn't the offending code, by the way.
Last edited by Kutumal on Mon Apr 30, 2018 7:07 am, edited 1 time in total.
The puppet Roavin uses for the big stuff.

User avatar
New Rogernomics
Powerbroker
 
Posts: 9510
Founded: Aug 22, 2006
Left-wing Utopia

Postby New Rogernomics » Mon Apr 30, 2018 7:11 am

Kutumal wrote:A brief info on what the code was (I spotted it and told NR about it):

It was retrieving nation/region flag URLs via the API on the client-side. It was using XMLHttpRequest's setRequestHeader() function to set a User-Agent, but browsers explicltly don't allow overriding the UA in that way and, accordingly, it didn't identify it correctly to NationStates. The correct approach would have been to include a "&user_agent=...."-parameter in the URL.

So basically, the intent for compliance was there, just the execution flawed.

EDIT: What NR posted isn't the offending code, by the way.
The offending code was removed ASAP. Here is the code:
Code: Select all
<!-- Nation in Lazarus in Profile -->
<script type="text/javascript">
$("dl.user_profile dt:contains('Nation')").each(function(){
var nation=$(this).next('dd').text();

var xhr = new XMLHttpRequest();
xhr.open("GET", "https://www.nationstates.net/cgi-bin/api.cgi?nation="+nation+"&q=flag+region", false);
xhr.setRequestHeader("User-Agent",<redacted>);
xhr.send();
xmlDocument = xhr.responseXML;
var flagurl = xmlDocument.getElementsByTagName("FLAG")[0].textContent;
var region = xmlDocument.getElementsByTagName("REGION")[0].textContent;

if (region == "Lazarus") {
$(this).next('dd').html("<span style='font-style:normal; line-height: 1.75em;'><a href='http://nationstates.net/nation="+nation+"' target='_blank'><img src='"+flagurl+"' height='26px' style='border: 1px solid #80d280;' /> "+nation+"</a></span>");
} else {

var xhr = new XMLHttpRequest();
xhr.open("GET", "https://www.nationstates.net/cgi-bin/api.cgi?region="+region+"&q=flag", false);
xhr.setRequestHeader("User-Agent", <redacted>);
xhr.send();
xmlDocument = xhr.responseXML;
var regionflagurl = xmlDocument.childNodes['0'].textContent;
$(this).next('dd').html("<span style='font-style:normal; line-height: 1.75em;'><a href='http://nationstates.net/nation="+nation+"' target='_blank'><img src='"+flagurl+"' height='26px' style='border: 1px solid #62ce62;' /> "+nation+"</a><div style='font-size:12px;'><i>visiting from</i> <a href='http://nationstates.net/region="+region+"' target='_blank' style='font-family: Nunito Sans;'><img src='"+regionflagurl+"' height='13px' style='border: 1px solid #80d280;' /> "+region+"</a></div></span>");
}
});</script>

Kutumal wrote:EDIT: What NR posted isn't the offending code, by the way.
I think it was one that it was initially based off though, obviously with a lot of changes.
Last edited by New Rogernomics on Mon Apr 30, 2018 7:50 am, edited 5 times in total.
Herald (Vice-Delegate) of Lazarus
"Solidarity forever..."
Hoping for Peace in Israel and Palestine
  • Former First Citizen (PM) of Lazarus
  • Former Proedroi (Minister) of Foreign Affairs of Lazarus
  • Former Lazarus Delegate (Humane Republic of Lazarus, 2015)
  • Minister of Culture & Media (Humane Republic of Lazarus)
  • Foreign Minister of The Ascendancy (RIP, and purged)
  • Senator of The Ascendancy (RIP, and purged)
  • Interior Commissioner of Lazarus (Pre-People's Republic of Lazarus)
  • At some point a member of the Grey family...then father vanished...
  • Foreign Minister of The Last Kingdom (RIP)
  • ADN:DSA Rep for Eastern Roman Empire
  • Honoratus Servant of the Holy Land (Eastern Roman Empire)
  • UN/WA Delegate of Trans Atlantice (RIP)

User avatar
Kutumal
Civil Servant
 
Posts: 9
Founded: Apr 22, 2018
Left-Leaning College State

Postby Kutumal » Mon Apr 30, 2018 7:44 am

Yep, that's the one.

Besides the synchronous XHR that made me cry :P the offending line:

Code: Select all
xhr.setRequestHeader("User-Agent", <redacted>);


Setting the User-Agent programmatically in such a way isn't allowed in most browsers.
The puppet Roavin uses for the big stuff.

User avatar
New Rogernomics
Powerbroker
 
Posts: 9510
Founded: Aug 22, 2006
Left-wing Utopia

Postby New Rogernomics » Mon Apr 30, 2018 8:08 am

Kutumal wrote:Yep, that's the one.

Besides the synchronous XHR that made me cry :P the offending line:

Code: Select all
xhr.setRequestHeader("User-Agent", <redacted>);


Setting the User-Agent programmatically in such a way isn't allowed in most browsers.
Really this is the only thing I found that helps me understand it: https://www.html5rocks.com/en/tutorials/cors/

I always hosted things on one server. So never ran into/knew of this issue.
Herald (Vice-Delegate) of Lazarus
"Solidarity forever..."
Hoping for Peace in Israel and Palestine
  • Former First Citizen (PM) of Lazarus
  • Former Proedroi (Minister) of Foreign Affairs of Lazarus
  • Former Lazarus Delegate (Humane Republic of Lazarus, 2015)
  • Minister of Culture & Media (Humane Republic of Lazarus)
  • Foreign Minister of The Ascendancy (RIP, and purged)
  • Senator of The Ascendancy (RIP, and purged)
  • Interior Commissioner of Lazarus (Pre-People's Republic of Lazarus)
  • At some point a member of the Grey family...then father vanished...
  • Foreign Minister of The Last Kingdom (RIP)
  • ADN:DSA Rep for Eastern Roman Empire
  • Honoratus Servant of the Holy Land (Eastern Roman Empire)
  • UN/WA Delegate of Trans Atlantice (RIP)

User avatar
Mount Seymour
Envoy
 
Posts: 251
Founded: Mar 25, 2016
Left-wing Utopia

Postby Mount Seymour » Mon Apr 30, 2018 10:13 am

^ That was my request butchering. Don't think I have much to add (lesson is basically I need to actually learn things before trying to make them work...). I have next to no background in js or using XHR, and had no intention of avoiding setting a user-agent. Kutumal helpfully explained the (numerous) problems with the code. Apologies to the NS admins (I'll make sure to check with multiple people as well as wait for a while before I venture into some other piece of code) and to all the developers and programmers whose eyes I made bleed.
The Pacific Alpine Commonwealth of Mount Seymour
a.k.a. Somyrion, Aumeltopia
Security Council #212
Issue #640

User avatar
Wolfram and Hart
Secretary
 
Posts: 37
Founded: Jul 06, 2017
Father Knows Best State

Postby Wolfram and Hart » Tue May 01, 2018 5:56 am

Mount Seymour wrote:^ That was my request butchering. Don't think I have much to add (lesson is basically I need to actually learn things before trying to make them work...). I have next to no background in js or using XHR, and had no intention of avoiding setting a user-agent. Kutumal helpfully explained the (numerous) problems with the code. Apologies to the NS admins (I'll make sure to check with multiple people as well as wait for a while before I venture into some other piece of code) and to all the developers and programmers whose eyes I made bleed.

This is where I started for this type of code:
https://www.w3schools.com/xml/ajax_intro.asp

Hope it helps!

User avatar
Mount Seymour
Envoy
 
Posts: 251
Founded: Mar 25, 2016
Left-wing Utopia

Postby Mount Seymour » Tue May 01, 2018 1:03 pm

Wolfram and Hart wrote:
Mount Seymour wrote:^ That was my request butchering. Don't think I have much to add (lesson is basically I need to actually learn things before trying to make them work...). I have next to no background in js or using XHR, and had no intention of avoiding setting a user-agent. Kutumal helpfully explained the (numerous) problems with the code. Apologies to the NS admins (I'll make sure to check with multiple people as well as wait for a while before I venture into some other piece of code) and to all the developers and programmers whose eyes I made bleed.

This is where I started for this type of code:
https://www.w3schools.com/xml/ajax_intro.asp

Hope it helps!

Thanks so much!

*adds to list of mandatory evening reading*
The Pacific Alpine Commonwealth of Mount Seymour
a.k.a. Somyrion, Aumeltopia
Security Council #212
Issue #640


Advertisement

Remove ads

Return to Technical

Who is online

Users browsing this forum: Democratic Nacheadasaan, Ioudaia, Juno-Scorpiris, Le Car, Llanfairp, Tape, Thromsa, Wurttemberger Lande

Advertisement

Remove ads