Skip to content

Commit

Permalink
fix(Message): Honour rcode when serialising message (#135)
Browse files Browse the repository at this point in the history
By working around bug in dns-packet.
  • Loading branch information
gnarea authored Jan 20, 2023
1 parent d34e373 commit f6ef7ae
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/lib/utils/dns/Message.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,12 @@ describe('Message', () => {
describe('serialise', () => {
describe('Header', () => {
test('RCODE should be honoured', () => {
const message = new Message({ rcode: RCODE_IDS.NOERROR }, [], []);
const message = new Message({ rcode: RCODE_IDS.NXDOMAIN }, [], []);

const serialisation = message.serialise();

const messageDeserialised = decode(serialisation);
expect(messageDeserialised.rcode).toBe('NOERROR');
expect(messageDeserialised.rcode).toBe('NXDOMAIN');
});
});

Expand Down
11 changes: 7 additions & 4 deletions src/lib/utils/dns/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { DnsError } from './DnsError.js';
import type { IanaRrTypeName } from './ianaRrTypes.js';
import type { DnsClassName } from './ianaClasses.js';
import type { RcodeName } from './ianaRcodes.js';
import { getRcodeId, getRcodeName } from './ianaRcodes.js';
import { getRcodeId } from './ianaRcodes.js';
import { getRrTypeName } from './ianaRrTypes.js';
import { getDnsClassName } from './ianaClasses.js';

Expand Down Expand Up @@ -46,7 +46,7 @@ export class Message {
answer.type,
answer.class as DnsClassName,
answer.ttl!,
answer.data as object,
answer.data,
),
);
return new Message({ rcode }, questions, answers);
Expand All @@ -59,7 +59,6 @@ export class Message {
) {}

public serialise(): Uint8Array {
const rcode = getRcodeName(this.header.rcode);
const questions: DpQuestion[] = this.questions.map((question) => ({
name: question.name,
type: question.getTypeName(),
Expand All @@ -71,7 +70,11 @@ export class Message {
class: getDnsClassName(answer.classId) as RecordClass,
data: answer.dataFields,
}));
return encode({ rcode, questions, answers });
return encode({
flags: this.header.rcode, // `rcode` field has no effect, so we have to pass it in the flags
questions,
answers,
});
}

/**
Expand Down
17 changes: 1 addition & 16 deletions src/lib/utils/dns/ianaRcodes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { RcodeIdOrName } from './ianaRcodes.js';
import { getRcodeId, getRcodeName, RCODE_IDS } from './ianaRcodes.js';
import { getRcodeId, RCODE_IDS } from './ianaRcodes.js';
import { DnsError } from './DnsError.js';

describe('getRcodeId', () => {
Expand All @@ -24,18 +24,3 @@ describe('getRcodeId', () => {
);
});
});

describe('getRcodeName', () => {
test('Id should be converted to name', () => {
expect(getRcodeName(RCODE_IDS.NOERROR)).toBe('NOERROR');
});

test('Id not defined by IANA should cause an error', () => {
const invalidId = Math.max(...Object.values(RCODE_IDS)) + 1;

expect(() => getRcodeName(invalidId)).toThrowWithMessage(
DnsError,
`DNS RCode id ${invalidId} is not defined by IANA`,
);
});
});
12 changes: 0 additions & 12 deletions src/lib/utils/dns/ianaRcodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ const RCODE_IDS_NORMALISED: { readonly [name: string]: number } = Object.entries
{},
);

const RCODE_NAMES: { readonly [name: number]: RcodeName } = Object.fromEntries(
Object.entries(RCODE_IDS_NORMALISED).map(([name, id]) => [id, name as RcodeName]),
);

export function getRcodeId(codeName: RcodeIdOrName): number {
if (typeof codeName === 'number') {
return codeName;
Expand All @@ -54,11 +50,3 @@ export function getRcodeId(codeName: RcodeIdOrName): number {

return id;
}

export function getRcodeName(codeId: number): RcodeName {
const name = RCODE_NAMES[codeId] as RcodeName | undefined;
if (!name) {
throw new DnsError(`DNS RCode id ${codeId} is not defined by IANA`);
}
return name;
}

0 comments on commit f6ef7ae

Please sign in to comment.